2 * Sony MemoryStick Pro storage support
4 * Copyright (C) 2007 Alex Dubov <oakad@yahoo.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
10 * Special thanks to Carlos Corbacho for providing various MemoryStick cards
11 * that made this driver possible.
15 #include <linux/blkdev.h>
16 #include <linux/idr.h>
17 #include <linux/hdreg.h>
18 #include <linux/kthread.h>
19 #include <linux/delay.h>
20 #include <linux/memstick.h>
22 #define DRIVER_NAME "mspro_block"
25 module_param(major, int, 0644);
27 #define MSPRO_BLOCK_MAX_SEGS 32
28 #define MSPRO_BLOCK_MAX_PAGES ((2 << 16) - 1)
30 #define MSPRO_BLOCK_SIGNATURE 0xa5c3
31 #define MSPRO_BLOCK_MAX_ATTRIBUTES 41
33 #define MSPRO_BLOCK_PART_SHIFT 3
36 MSPRO_BLOCK_ID_SYSINFO = 0x10,
37 MSPRO_BLOCK_ID_MODELNAME = 0x15,
38 MSPRO_BLOCK_ID_MBR = 0x20,
39 MSPRO_BLOCK_ID_PBR16 = 0x21,
40 MSPRO_BLOCK_ID_PBR32 = 0x22,
41 MSPRO_BLOCK_ID_SPECFILEVALUES1 = 0x25,
42 MSPRO_BLOCK_ID_SPECFILEVALUES2 = 0x26,
43 MSPRO_BLOCK_ID_DEVINFO = 0x30
46 struct mspro_sys_attr {
51 struct device_attribute dev_attr;
54 struct mspro_attr_entry {
58 unsigned char reserved[3];
59 } __attribute__((packed));
61 struct mspro_attribute {
62 unsigned short signature;
63 unsigned short version;
65 unsigned char reserved[11];
66 struct mspro_attr_entry entries[];
67 } __attribute__((packed));
69 struct mspro_sys_info {
71 unsigned char reserved0;
72 unsigned short block_size;
73 unsigned short block_count;
74 unsigned short user_block_count;
75 unsigned short page_size;
76 unsigned char reserved1[2];
77 unsigned char assembly_date[8];
78 unsigned int serial_number;
79 unsigned char assembly_maker_code;
80 unsigned char assembly_model_code[3];
81 unsigned short memory_maker_code;
82 unsigned short memory_model_code;
83 unsigned char reserved2[4];
86 unsigned short controller_number;
87 unsigned short controller_function;
88 unsigned short start_sector;
89 unsigned short unit_size;
90 unsigned char ms_sub_class;
91 unsigned char reserved3[4];
92 unsigned char interface_type;
93 unsigned short controller_code;
94 unsigned char format_type;
95 unsigned char reserved4;
96 unsigned char device_type;
97 unsigned char reserved5[7];
98 unsigned char mspro_id[16];
99 unsigned char reserved6[16];
100 } __attribute__((packed));
103 unsigned char boot_partition;
104 unsigned char start_head;
105 unsigned char start_sector;
106 unsigned char start_cylinder;
107 unsigned char partition_type;
108 unsigned char end_head;
109 unsigned char end_sector;
110 unsigned char end_cylinder;
111 unsigned int start_sectors;
112 unsigned int sectors_per_partition;
113 } __attribute__((packed));
115 struct mspro_specfile {
119 unsigned char reserved[10];
122 unsigned short cluster;
124 } __attribute__((packed));
126 struct mspro_devinfo {
127 unsigned short cylinders;
128 unsigned short heads;
129 unsigned short bytes_per_track;
130 unsigned short bytes_per_sector;
131 unsigned short sectors_per_track;
132 unsigned char reserved[6];
133 } __attribute__((packed));
135 struct mspro_block_data {
136 struct memstick_dev *card;
137 unsigned int usage_count;
139 struct gendisk *disk;
140 struct request_queue *queue;
141 struct request *block_req;
144 unsigned short page_size;
145 unsigned short cylinders;
146 unsigned short heads;
147 unsigned short sectors_per_track;
149 unsigned char system;
150 unsigned char read_only:1,
155 unsigned char transfer_cmd;
157 int (*mrq_handler)(struct memstick_dev *card,
158 struct memstick_request **mrq);
160 struct attribute_group attr_group;
162 struct scatterlist req_sg[MSPRO_BLOCK_MAX_SEGS];
163 unsigned int seg_count;
164 unsigned int current_seg;
165 unsigned int current_page;
168 static DEFINE_IDR(mspro_block_disk_idr);
169 static DEFINE_MUTEX(mspro_block_disk_lock);
171 static int mspro_block_complete_req(struct memstick_dev *card, int error);
173 /*** Block device ***/
175 static int mspro_block_bd_open(struct block_device *bdev, fmode_t mode)
177 struct gendisk *disk = bdev->bd_disk;
178 struct mspro_block_data *msb = disk->private_data;
181 mutex_lock(&mspro_block_disk_lock);
183 if (msb && msb->card) {
185 if ((mode & FMODE_WRITE) && msb->read_only)
191 mutex_unlock(&mspro_block_disk_lock);
197 static int mspro_block_disk_release(struct gendisk *disk)
199 struct mspro_block_data *msb = disk->private_data;
200 int disk_id = MINOR(disk_devt(disk)) >> MSPRO_BLOCK_PART_SHIFT;
202 mutex_lock(&mspro_block_disk_lock);
205 if (msb->usage_count)
208 if (!msb->usage_count) {
210 disk->private_data = NULL;
211 idr_remove(&mspro_block_disk_idr, disk_id);
216 mutex_unlock(&mspro_block_disk_lock);
221 static int mspro_block_bd_release(struct gendisk *disk, fmode_t mode)
223 return mspro_block_disk_release(disk);
226 static int mspro_block_bd_getgeo(struct block_device *bdev,
227 struct hd_geometry *geo)
229 struct mspro_block_data *msb = bdev->bd_disk->private_data;
231 geo->heads = msb->heads;
232 geo->sectors = msb->sectors_per_track;
233 geo->cylinders = msb->cylinders;
238 static struct block_device_operations ms_block_bdops = {
239 .open = mspro_block_bd_open,
240 .release = mspro_block_bd_release,
241 .getgeo = mspro_block_bd_getgeo,
245 /*** Information ***/
247 static struct mspro_sys_attr *mspro_from_sysfs_attr(struct attribute *attr)
249 struct device_attribute *dev_attr
250 = container_of(attr, struct device_attribute, attr);
251 return container_of(dev_attr, struct mspro_sys_attr, dev_attr);
254 static const char *mspro_block_attr_name(unsigned char tag)
257 case MSPRO_BLOCK_ID_SYSINFO:
258 return "attr_sysinfo";
259 case MSPRO_BLOCK_ID_MODELNAME:
260 return "attr_modelname";
261 case MSPRO_BLOCK_ID_MBR:
263 case MSPRO_BLOCK_ID_PBR16:
265 case MSPRO_BLOCK_ID_PBR32:
267 case MSPRO_BLOCK_ID_SPECFILEVALUES1:
268 return "attr_specfilevalues1";
269 case MSPRO_BLOCK_ID_SPECFILEVALUES2:
270 return "attr_specfilevalues2";
271 case MSPRO_BLOCK_ID_DEVINFO:
272 return "attr_devinfo";
278 typedef ssize_t (*sysfs_show_t)(struct device *dev,
279 struct device_attribute *attr,
282 static ssize_t mspro_block_attr_show_default(struct device *dev,
283 struct device_attribute *attr,
286 struct mspro_sys_attr *s_attr = container_of(attr,
287 struct mspro_sys_attr,
292 for (cnt = 0; cnt < s_attr->size; cnt++) {
293 if (cnt && !(cnt % 16)) {
298 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "%02x ",
299 ((unsigned char *)s_attr->data)[cnt]);
304 static ssize_t mspro_block_attr_show_sysinfo(struct device *dev,
305 struct device_attribute *attr,
308 struct mspro_sys_attr *x_attr = container_of(attr,
309 struct mspro_sys_attr,
311 struct mspro_sys_info *x_sys = x_attr->data;
313 int date_tz = 0, date_tz_f = 0;
315 if (x_sys->assembly_date[0] > 0x80U) {
316 date_tz = (~x_sys->assembly_date[0]) + 1;
317 date_tz_f = date_tz & 3;
321 } else if (x_sys->assembly_date[0] < 0x80U) {
322 date_tz = x_sys->assembly_date[0];
323 date_tz_f = date_tz & 3;
328 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "class: %x\n",
330 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "block size: %x\n",
331 be16_to_cpu(x_sys->block_size));
332 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "block count: %x\n",
333 be16_to_cpu(x_sys->block_count));
334 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "user block count: %x\n",
335 be16_to_cpu(x_sys->user_block_count));
336 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "page size: %x\n",
337 be16_to_cpu(x_sys->page_size));
338 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "assembly date: "
339 "GMT%+d:%d %04u-%02u-%02u %02u:%02u:%02u\n",
341 be16_to_cpu(*(unsigned short *)
342 &x_sys->assembly_date[1]),
343 x_sys->assembly_date[3], x_sys->assembly_date[4],
344 x_sys->assembly_date[5], x_sys->assembly_date[6],
345 x_sys->assembly_date[7]);
346 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "serial number: %x\n",
347 be32_to_cpu(x_sys->serial_number));
348 rc += scnprintf(buffer + rc, PAGE_SIZE - rc,
349 "assembly maker code: %x\n",
350 x_sys->assembly_maker_code);
351 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "assembly model code: "
352 "%02x%02x%02x\n", x_sys->assembly_model_code[0],
353 x_sys->assembly_model_code[1],
354 x_sys->assembly_model_code[2]);
355 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "memory maker code: %x\n",
356 be16_to_cpu(x_sys->memory_maker_code));
357 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "memory model code: %x\n",
358 be16_to_cpu(x_sys->memory_model_code));
359 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "vcc: %x\n",
361 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "vpp: %x\n",
363 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "controller number: %x\n",
364 be16_to_cpu(x_sys->controller_number));
365 rc += scnprintf(buffer + rc, PAGE_SIZE - rc,
366 "controller function: %x\n",
367 be16_to_cpu(x_sys->controller_function));
368 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "start sector: %x\n",
369 be16_to_cpu(x_sys->start_sector));
370 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "unit size: %x\n",
371 be16_to_cpu(x_sys->unit_size));
372 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "sub class: %x\n",
373 x_sys->ms_sub_class);
374 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "interface type: %x\n",
375 x_sys->interface_type);
376 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "controller code: %x\n",
377 be16_to_cpu(x_sys->controller_code));
378 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "format type: %x\n",
380 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "device type: %x\n",
382 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "mspro id: %s\n",
387 static ssize_t mspro_block_attr_show_modelname(struct device *dev,
388 struct device_attribute *attr,
391 struct mspro_sys_attr *s_attr = container_of(attr,
392 struct mspro_sys_attr,
395 return scnprintf(buffer, PAGE_SIZE, "%s", (char *)s_attr->data);
398 static ssize_t mspro_block_attr_show_mbr(struct device *dev,
399 struct device_attribute *attr,
402 struct mspro_sys_attr *x_attr = container_of(attr,
403 struct mspro_sys_attr,
405 struct mspro_mbr *x_mbr = x_attr->data;
408 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "boot partition: %x\n",
409 x_mbr->boot_partition);
410 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "start head: %x\n",
412 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "start sector: %x\n",
413 x_mbr->start_sector);
414 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "start cylinder: %x\n",
415 x_mbr->start_cylinder);
416 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "partition type: %x\n",
417 x_mbr->partition_type);
418 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "end head: %x\n",
420 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "end sector: %x\n",
422 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "end cylinder: %x\n",
423 x_mbr->end_cylinder);
424 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "start sectors: %x\n",
425 x_mbr->start_sectors);
426 rc += scnprintf(buffer + rc, PAGE_SIZE - rc,
427 "sectors per partition: %x\n",
428 x_mbr->sectors_per_partition);
432 static ssize_t mspro_block_attr_show_specfile(struct device *dev,
433 struct device_attribute *attr,
436 struct mspro_sys_attr *x_attr = container_of(attr,
437 struct mspro_sys_attr,
439 struct mspro_specfile *x_spfile = x_attr->data;
440 char name[9], ext[4];
443 memcpy(name, x_spfile->name, 8);
445 memcpy(ext, x_spfile->ext, 3);
448 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "name: %s\n", name);
449 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "ext: %s\n", ext);
450 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "attribute: %x\n",
452 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "time: %d:%d:%d\n",
453 x_spfile->time >> 11,
454 (x_spfile->time >> 5) & 0x3f,
455 (x_spfile->time & 0x1f) * 2);
456 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "date: %d-%d-%d\n",
457 (x_spfile->date >> 9) + 1980,
458 (x_spfile->date >> 5) & 0xf,
459 x_spfile->date & 0x1f);
460 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "start cluster: %x\n",
462 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "size: %x\n",
467 static ssize_t mspro_block_attr_show_devinfo(struct device *dev,
468 struct device_attribute *attr,
471 struct mspro_sys_attr *x_attr = container_of(attr,
472 struct mspro_sys_attr,
474 struct mspro_devinfo *x_devinfo = x_attr->data;
477 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "cylinders: %x\n",
478 be16_to_cpu(x_devinfo->cylinders));
479 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "heads: %x\n",
480 be16_to_cpu(x_devinfo->heads));
481 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "bytes per track: %x\n",
482 be16_to_cpu(x_devinfo->bytes_per_track));
483 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "bytes per sector: %x\n",
484 be16_to_cpu(x_devinfo->bytes_per_sector));
485 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "sectors per track: %x\n",
486 be16_to_cpu(x_devinfo->sectors_per_track));
490 static sysfs_show_t mspro_block_attr_show(unsigned char tag)
493 case MSPRO_BLOCK_ID_SYSINFO:
494 return mspro_block_attr_show_sysinfo;
495 case MSPRO_BLOCK_ID_MODELNAME:
496 return mspro_block_attr_show_modelname;
497 case MSPRO_BLOCK_ID_MBR:
498 return mspro_block_attr_show_mbr;
499 case MSPRO_BLOCK_ID_SPECFILEVALUES1:
500 case MSPRO_BLOCK_ID_SPECFILEVALUES2:
501 return mspro_block_attr_show_specfile;
502 case MSPRO_BLOCK_ID_DEVINFO:
503 return mspro_block_attr_show_devinfo;
505 return mspro_block_attr_show_default;
509 /*** Protocol handlers ***/
512 * Functions prefixed with "h_" are protocol callbacks. They can be called from
513 * interrupt context. Return value of 0 means that request processing is still
514 * ongoing, while special error value of -EAGAIN means that current request is
515 * finished (and request processor should come back some time later).
518 static int h_mspro_block_req_init(struct memstick_dev *card,
519 struct memstick_request **mrq)
521 struct mspro_block_data *msb = memstick_get_drvdata(card);
523 *mrq = &card->current_mrq;
524 card->next_request = msb->mrq_handler;
528 static int h_mspro_block_default(struct memstick_dev *card,
529 struct memstick_request **mrq)
531 return mspro_block_complete_req(card, (*mrq)->error);
534 static int h_mspro_block_default_bad(struct memstick_dev *card,
535 struct memstick_request **mrq)
540 static int h_mspro_block_get_ro(struct memstick_dev *card,
541 struct memstick_request **mrq)
543 struct mspro_block_data *msb = memstick_get_drvdata(card);
545 if (!(*mrq)->error) {
546 if ((*mrq)->data[offsetof(struct ms_status_register, status0)]
547 & MEMSTICK_STATUS0_WP)
553 return mspro_block_complete_req(card, (*mrq)->error);
556 static int h_mspro_block_wait_for_ced(struct memstick_dev *card,
557 struct memstick_request **mrq)
559 dev_dbg(&card->dev, "wait for ced: value %x\n", (*mrq)->data[0]);
561 if (!(*mrq)->error) {
562 if ((*mrq)->data[0] & (MEMSTICK_INT_CMDNAK | MEMSTICK_INT_ERR))
563 (*mrq)->error = -EFAULT;
564 else if (!((*mrq)->data[0] & MEMSTICK_INT_CED))
568 return mspro_block_complete_req(card, (*mrq)->error);
571 static int h_mspro_block_transfer_data(struct memstick_dev *card,
572 struct memstick_request **mrq)
574 struct mspro_block_data *msb = memstick_get_drvdata(card);
575 unsigned char t_val = 0;
576 struct scatterlist t_sg = { 0 };
580 return mspro_block_complete_req(card, (*mrq)->error);
582 switch ((*mrq)->tpc) {
583 case MS_TPC_WRITE_REG:
584 memstick_init_req(*mrq, MS_TPC_SET_CMD, &msb->transfer_cmd, 1);
585 (*mrq)->need_card_int = 1;
588 t_val = (*mrq)->int_reg;
589 memstick_init_req(*mrq, MS_TPC_GET_INT, NULL, 1);
590 if (msb->caps & MEMSTICK_CAP_AUTO_GET_INT)
594 t_val = (*mrq)->data[0];
596 if (t_val & (MEMSTICK_INT_CMDNAK | MEMSTICK_INT_ERR)) {
597 t_val = MSPRO_CMD_STOP;
598 memstick_init_req(*mrq, MS_TPC_SET_CMD, &t_val, 1);
599 card->next_request = h_mspro_block_default;
603 if (msb->current_page
604 == (msb->req_sg[msb->current_seg].length
606 msb->current_page = 0;
609 if (msb->current_seg == msb->seg_count) {
610 if (t_val & MEMSTICK_INT_CED) {
611 return mspro_block_complete_req(card,
615 = h_mspro_block_wait_for_ced;
616 memstick_init_req(*mrq, MS_TPC_GET_INT,
623 if (!(t_val & MEMSTICK_INT_BREQ)) {
624 memstick_init_req(*mrq, MS_TPC_GET_INT, NULL, 1);
628 t_offset = msb->req_sg[msb->current_seg].offset;
629 t_offset += msb->current_page * msb->page_size;
632 nth_page(sg_page(&(msb->req_sg[msb->current_seg])),
633 t_offset >> PAGE_SHIFT),
634 msb->page_size, offset_in_page(t_offset));
636 memstick_init_req_sg(*mrq, msb->data_dir == READ
637 ? MS_TPC_READ_LONG_DATA
638 : MS_TPC_WRITE_LONG_DATA,
640 (*mrq)->need_card_int = 1;
642 case MS_TPC_READ_LONG_DATA:
643 case MS_TPC_WRITE_LONG_DATA:
645 if (msb->caps & MEMSTICK_CAP_AUTO_GET_INT) {
646 t_val = (*mrq)->int_reg;
649 memstick_init_req(*mrq, MS_TPC_GET_INT, NULL, 1);
658 /*** Data transfer ***/
660 static int mspro_block_issue_req(struct memstick_dev *card, int chunk)
662 struct mspro_block_data *msb = memstick_get_drvdata(card);
665 struct mspro_param_register param;
669 msb->current_page = 0;
670 msb->current_seg = 0;
671 msb->seg_count = blk_rq_map_sg(msb->block_req->q,
675 if (!msb->seg_count) {
676 chunk = __blk_end_request(msb->block_req, -ENOMEM,
677 blk_rq_cur_bytes(msb->block_req));
681 t_sec = msb->block_req->sector << 9;
682 sector_div(t_sec, msb->page_size);
684 count = msb->block_req->nr_sectors << 9;
685 count /= msb->page_size;
687 param.system = msb->system;
688 param.data_count = cpu_to_be16(count);
689 param.data_address = cpu_to_be32((uint32_t)t_sec);
692 msb->data_dir = rq_data_dir(msb->block_req);
693 msb->transfer_cmd = msb->data_dir == READ
694 ? MSPRO_CMD_READ_DATA
695 : MSPRO_CMD_WRITE_DATA;
697 dev_dbg(&card->dev, "data transfer: cmd %x, "
698 "lba %x, count %x\n", msb->transfer_cmd,
699 be32_to_cpu(param.data_address), count);
701 card->next_request = h_mspro_block_req_init;
702 msb->mrq_handler = h_mspro_block_transfer_data;
703 memstick_init_req(&card->current_mrq, MS_TPC_WRITE_REG,
704 ¶m, sizeof(param));
705 memstick_new_req(card->host);
709 dev_dbg(&card->dev, "elv_next\n");
710 msb->block_req = elv_next_request(msb->queue);
711 if (!msb->block_req) {
712 dev_dbg(&card->dev, "issue end\n");
716 dev_dbg(&card->dev, "trying again\n");
721 static int mspro_block_complete_req(struct memstick_dev *card, int error)
723 struct mspro_block_data *msb = memstick_get_drvdata(card);
725 unsigned int t_len = 0;
728 spin_lock_irqsave(&msb->q_lock, flags);
729 dev_dbg(&card->dev, "complete %d, %d\n", msb->has_request ? 1 : 0,
732 if (msb->has_request) {
733 /* Nothing to do - not really an error */
734 if (error == -EAGAIN)
737 if (error || (card->current_mrq.tpc == MSPRO_CMD_STOP)) {
738 if (msb->data_dir == READ) {
739 for (cnt = 0; cnt < msb->current_seg; cnt++)
740 t_len += msb->req_sg[cnt].length
743 if (msb->current_page)
744 t_len += msb->current_page - 1;
746 t_len *= msb->page_size;
749 t_len = msb->block_req->nr_sectors << 9;
751 dev_dbg(&card->dev, "transferred %x (%d)\n", t_len, error);
754 t_len = blk_rq_cur_bytes(msb->block_req);
756 chunk = __blk_end_request(msb->block_req, error, t_len);
758 error = mspro_block_issue_req(card, chunk);
763 msb->has_request = 0;
769 card->next_request = h_mspro_block_default_bad;
770 complete_all(&card->mrq_complete);
772 spin_unlock_irqrestore(&msb->q_lock, flags);
776 static void mspro_block_stop(struct memstick_dev *card)
778 struct mspro_block_data *msb = memstick_get_drvdata(card);
783 spin_lock_irqsave(&msb->q_lock, flags);
784 if (!msb->has_request) {
785 blk_stop_queue(msb->queue);
788 spin_unlock_irqrestore(&msb->q_lock, flags);
793 wait_for_completion(&card->mrq_complete);
797 static void mspro_block_start(struct memstick_dev *card)
799 struct mspro_block_data *msb = memstick_get_drvdata(card);
802 spin_lock_irqsave(&msb->q_lock, flags);
803 blk_start_queue(msb->queue);
804 spin_unlock_irqrestore(&msb->q_lock, flags);
807 static int mspro_block_prepare_req(struct request_queue *q, struct request *req)
809 if (!blk_fs_request(req) && !blk_pc_request(req)) {
810 blk_dump_rq_flags(req, "MSPro unsupported request");
814 req->cmd_flags |= REQ_DONTPREP;
819 static void mspro_block_submit_req(struct request_queue *q)
821 struct memstick_dev *card = q->queuedata;
822 struct mspro_block_data *msb = memstick_get_drvdata(card);
823 struct request *req = NULL;
825 if (msb->has_request)
829 while ((req = elv_next_request(q)) != NULL)
830 __blk_end_request(req, -ENODEV, blk_rq_bytes(req));
835 msb->has_request = 1;
836 if (mspro_block_issue_req(card, 0))
837 msb->has_request = 0;
840 /*** Initialization ***/
842 static int mspro_block_wait_for_ced(struct memstick_dev *card)
844 struct mspro_block_data *msb = memstick_get_drvdata(card);
846 card->next_request = h_mspro_block_req_init;
847 msb->mrq_handler = h_mspro_block_wait_for_ced;
848 memstick_init_req(&card->current_mrq, MS_TPC_GET_INT, NULL, 1);
849 memstick_new_req(card->host);
850 wait_for_completion(&card->mrq_complete);
851 return card->current_mrq.error;
854 static int mspro_block_set_interface(struct memstick_dev *card,
855 unsigned char sys_reg)
857 struct memstick_host *host = card->host;
858 struct mspro_block_data *msb = memstick_get_drvdata(card);
859 struct mspro_param_register param = {
866 card->next_request = h_mspro_block_req_init;
867 msb->mrq_handler = h_mspro_block_default;
868 memstick_init_req(&card->current_mrq, MS_TPC_WRITE_REG, ¶m,
870 memstick_new_req(host);
871 wait_for_completion(&card->mrq_complete);
872 return card->current_mrq.error;
875 static int mspro_block_switch_interface(struct memstick_dev *card)
877 struct memstick_host *host = card->host;
878 struct mspro_block_data *msb = memstick_get_drvdata(card);
882 if (msb->caps & MEMSTICK_CAP_PAR4)
883 rc = mspro_block_set_interface(card, MEMSTICK_SYS_PAR4);
889 "%s: could not switch to 4-bit mode, error %d\n",
890 card->dev.bus_id, rc);
894 msb->system = MEMSTICK_SYS_PAR4;
895 host->set_param(host, MEMSTICK_INTERFACE, MEMSTICK_PAR4);
896 printk(KERN_INFO "%s: switching to 4-bit parallel mode\n",
899 if (msb->caps & MEMSTICK_CAP_PAR8) {
900 rc = mspro_block_set_interface(card, MEMSTICK_SYS_PAR8);
903 msb->system = MEMSTICK_SYS_PAR8;
904 host->set_param(host, MEMSTICK_INTERFACE,
907 "%s: switching to 8-bit parallel mode\n",
911 "%s: could not switch to 8-bit mode, error %d\n",
912 card->dev.bus_id, rc);
915 card->next_request = h_mspro_block_req_init;
916 msb->mrq_handler = h_mspro_block_default;
917 memstick_init_req(&card->current_mrq, MS_TPC_GET_INT, NULL, 1);
918 memstick_new_req(card->host);
919 wait_for_completion(&card->mrq_complete);
920 rc = card->current_mrq.error;
924 "%s: interface error, trying to fall back to serial\n",
926 msb->system = MEMSTICK_SYS_SERIAL;
927 host->set_param(host, MEMSTICK_POWER, MEMSTICK_POWER_OFF);
929 host->set_param(host, MEMSTICK_POWER, MEMSTICK_POWER_ON);
930 host->set_param(host, MEMSTICK_INTERFACE, MEMSTICK_SERIAL);
932 rc = memstick_set_rw_addr(card);
934 rc = mspro_block_set_interface(card, msb->system);
938 rc = mspro_block_wait_for_ced(card);
942 if (msb->caps & MEMSTICK_CAP_PAR8) {
943 msb->caps &= ~MEMSTICK_CAP_PAR8;
951 /* Memory allocated for attributes by this function should be freed by
952 * mspro_block_data_clear, no matter if the initialization process succeded
955 static int mspro_block_read_attributes(struct memstick_dev *card)
957 struct mspro_block_data *msb = memstick_get_drvdata(card);
958 struct mspro_param_register param = {
959 .system = msb->system,
960 .data_count = cpu_to_be16(1),
964 struct mspro_attribute *attr = NULL;
965 struct mspro_sys_attr *s_attr = NULL;
966 unsigned char *buffer = NULL;
967 int cnt, rc, attr_count;
969 unsigned short page_count;
971 attr = kmalloc(msb->page_size, GFP_KERNEL);
975 sg_init_one(&msb->req_sg[0], attr, msb->page_size);
977 msb->current_seg = 0;
978 msb->current_page = 0;
979 msb->data_dir = READ;
980 msb->transfer_cmd = MSPRO_CMD_READ_ATRB;
982 card->next_request = h_mspro_block_req_init;
983 msb->mrq_handler = h_mspro_block_transfer_data;
984 memstick_init_req(&card->current_mrq, MS_TPC_WRITE_REG, ¶m,
986 memstick_new_req(card->host);
987 wait_for_completion(&card->mrq_complete);
988 if (card->current_mrq.error) {
989 rc = card->current_mrq.error;
993 if (be16_to_cpu(attr->signature) != MSPRO_BLOCK_SIGNATURE) {
994 printk(KERN_ERR "%s: unrecognized device signature %x\n",
995 card->dev.bus_id, be16_to_cpu(attr->signature));
1000 if (attr->count > MSPRO_BLOCK_MAX_ATTRIBUTES) {
1001 printk(KERN_WARNING "%s: way too many attribute entries\n",
1003 attr_count = MSPRO_BLOCK_MAX_ATTRIBUTES;
1005 attr_count = attr->count;
1007 msb->attr_group.attrs = kzalloc((attr_count + 1)
1008 * sizeof(struct attribute),
1010 if (!msb->attr_group.attrs) {
1014 msb->attr_group.name = "media_attributes";
1016 buffer = kmalloc(msb->page_size, GFP_KERNEL);
1021 memcpy(buffer, (char *)attr, msb->page_size);
1024 for (cnt = 0; cnt < attr_count; ++cnt) {
1025 s_attr = kzalloc(sizeof(struct mspro_sys_attr), GFP_KERNEL);
1028 goto out_free_buffer;
1031 msb->attr_group.attrs[cnt] = &s_attr->dev_attr.attr;
1032 addr = be32_to_cpu(attr->entries[cnt].address);
1033 rc = be32_to_cpu(attr->entries[cnt].size);
1034 dev_dbg(&card->dev, "adding attribute %d: id %x, address %x, "
1035 "size %x\n", cnt, attr->entries[cnt].id, addr, rc);
1036 s_attr->id = attr->entries[cnt].id;
1037 if (mspro_block_attr_name(s_attr->id))
1038 snprintf(s_attr->name, sizeof(s_attr->name), "%s",
1039 mspro_block_attr_name(attr->entries[cnt].id));
1041 snprintf(s_attr->name, sizeof(s_attr->name),
1042 "attr_x%02x", attr->entries[cnt].id);
1044 s_attr->dev_attr.attr.name = s_attr->name;
1045 s_attr->dev_attr.attr.mode = S_IRUGO;
1046 s_attr->dev_attr.show = mspro_block_attr_show(s_attr->id);
1052 s_attr->data = kmalloc(rc, GFP_KERNEL);
1053 if (!s_attr->data) {
1055 goto out_free_buffer;
1058 if (((addr / msb->page_size)
1059 == be32_to_cpu(param.data_address))
1060 && (((addr + rc - 1) / msb->page_size)
1061 == be32_to_cpu(param.data_address))) {
1062 memcpy(s_attr->data, buffer + addr % msb->page_size,
1067 if (page_count <= (rc / msb->page_size)) {
1069 page_count = (rc / msb->page_size) + 1;
1070 buffer = kmalloc(page_count * msb->page_size,
1078 param.system = msb->system;
1079 param.data_count = cpu_to_be16((rc / msb->page_size) + 1);
1080 param.data_address = cpu_to_be32(addr / msb->page_size);
1081 param.tpc_param = 0;
1083 sg_init_one(&msb->req_sg[0], buffer,
1084 be16_to_cpu(param.data_count) * msb->page_size);
1086 msb->current_seg = 0;
1087 msb->current_page = 0;
1088 msb->data_dir = READ;
1089 msb->transfer_cmd = MSPRO_CMD_READ_ATRB;
1091 dev_dbg(&card->dev, "reading attribute pages %x, %x\n",
1092 be32_to_cpu(param.data_address),
1093 be16_to_cpu(param.data_count));
1095 card->next_request = h_mspro_block_req_init;
1096 msb->mrq_handler = h_mspro_block_transfer_data;
1097 memstick_init_req(&card->current_mrq, MS_TPC_WRITE_REG,
1098 (char *)¶m, sizeof(param));
1099 memstick_new_req(card->host);
1100 wait_for_completion(&card->mrq_complete);
1101 if (card->current_mrq.error) {
1102 rc = card->current_mrq.error;
1103 goto out_free_buffer;
1106 memcpy(s_attr->data, buffer + addr % msb->page_size, rc);
1117 static int mspro_block_init_card(struct memstick_dev *card)
1119 struct mspro_block_data *msb = memstick_get_drvdata(card);
1120 struct memstick_host *host = card->host;
1123 msb->system = MEMSTICK_SYS_SERIAL;
1124 card->reg_addr.r_offset = offsetof(struct mspro_register, status);
1125 card->reg_addr.r_length = sizeof(struct ms_status_register);
1126 card->reg_addr.w_offset = offsetof(struct mspro_register, param);
1127 card->reg_addr.w_length = sizeof(struct mspro_param_register);
1129 if (memstick_set_rw_addr(card))
1132 msb->caps = host->caps;
1135 rc = mspro_block_wait_for_ced(card);
1139 rc = mspro_block_switch_interface(card);
1143 dev_dbg(&card->dev, "card activated\n");
1144 if (msb->system != MEMSTICK_SYS_SERIAL)
1145 msb->caps |= MEMSTICK_CAP_AUTO_GET_INT;
1147 card->next_request = h_mspro_block_req_init;
1148 msb->mrq_handler = h_mspro_block_get_ro;
1149 memstick_init_req(&card->current_mrq, MS_TPC_READ_REG, NULL,
1150 sizeof(struct ms_status_register));
1151 memstick_new_req(card->host);
1152 wait_for_completion(&card->mrq_complete);
1153 if (card->current_mrq.error)
1154 return card->current_mrq.error;
1156 dev_dbg(&card->dev, "card r/w status %d\n", msb->read_only ? 0 : 1);
1158 msb->page_size = 512;
1159 rc = mspro_block_read_attributes(card);
1163 dev_dbg(&card->dev, "attributes loaded\n");
1168 static int mspro_block_init_disk(struct memstick_dev *card)
1170 struct mspro_block_data *msb = memstick_get_drvdata(card);
1171 struct memstick_host *host = card->host;
1172 struct mspro_devinfo *dev_info = NULL;
1173 struct mspro_sys_info *sys_info = NULL;
1174 struct mspro_sys_attr *s_attr = NULL;
1176 u64 limit = BLK_BOUNCE_HIGH;
1177 unsigned long capacity;
1179 if (host->dev.dma_mask && *(host->dev.dma_mask))
1180 limit = *(host->dev.dma_mask);
1182 for (rc = 0; msb->attr_group.attrs[rc]; ++rc) {
1183 s_attr = mspro_from_sysfs_attr(msb->attr_group.attrs[rc]);
1185 if (s_attr->id == MSPRO_BLOCK_ID_DEVINFO)
1186 dev_info = s_attr->data;
1187 else if (s_attr->id == MSPRO_BLOCK_ID_SYSINFO)
1188 sys_info = s_attr->data;
1191 if (!dev_info || !sys_info)
1194 msb->cylinders = be16_to_cpu(dev_info->cylinders);
1195 msb->heads = be16_to_cpu(dev_info->heads);
1196 msb->sectors_per_track = be16_to_cpu(dev_info->sectors_per_track);
1198 msb->page_size = be16_to_cpu(sys_info->unit_size);
1200 if (!idr_pre_get(&mspro_block_disk_idr, GFP_KERNEL))
1203 mutex_lock(&mspro_block_disk_lock);
1204 rc = idr_get_new(&mspro_block_disk_idr, card, &disk_id);
1205 mutex_unlock(&mspro_block_disk_lock);
1210 if ((disk_id << MSPRO_BLOCK_PART_SHIFT) > 255) {
1212 goto out_release_id;
1215 msb->disk = alloc_disk(1 << MSPRO_BLOCK_PART_SHIFT);
1218 goto out_release_id;
1221 msb->queue = blk_init_queue(mspro_block_submit_req, &msb->q_lock);
1227 msb->queue->queuedata = card;
1228 blk_queue_prep_rq(msb->queue, mspro_block_prepare_req);
1230 blk_queue_bounce_limit(msb->queue, limit);
1231 blk_queue_max_sectors(msb->queue, MSPRO_BLOCK_MAX_PAGES);
1232 blk_queue_max_phys_segments(msb->queue, MSPRO_BLOCK_MAX_SEGS);
1233 blk_queue_max_hw_segments(msb->queue, MSPRO_BLOCK_MAX_SEGS);
1234 blk_queue_max_segment_size(msb->queue,
1235 MSPRO_BLOCK_MAX_PAGES * msb->page_size);
1237 msb->disk->major = major;
1238 msb->disk->first_minor = disk_id << MSPRO_BLOCK_PART_SHIFT;
1239 msb->disk->fops = &ms_block_bdops;
1240 msb->usage_count = 1;
1241 msb->disk->private_data = msb;
1242 msb->disk->queue = msb->queue;
1243 msb->disk->driverfs_dev = &card->dev;
1245 sprintf(msb->disk->disk_name, "mspblk%d", disk_id);
1247 blk_queue_hardsect_size(msb->queue, msb->page_size);
1249 capacity = be16_to_cpu(sys_info->user_block_count);
1250 capacity *= be16_to_cpu(sys_info->block_size);
1251 capacity *= msb->page_size >> 9;
1252 set_capacity(msb->disk, capacity);
1253 dev_dbg(&card->dev, "capacity set %ld\n", capacity);
1255 add_disk(msb->disk);
1260 put_disk(msb->disk);
1262 mutex_lock(&mspro_block_disk_lock);
1263 idr_remove(&mspro_block_disk_idr, disk_id);
1264 mutex_unlock(&mspro_block_disk_lock);
1268 static void mspro_block_data_clear(struct mspro_block_data *msb)
1271 struct mspro_sys_attr *s_attr;
1273 if (msb->attr_group.attrs) {
1274 for (cnt = 0; msb->attr_group.attrs[cnt]; ++cnt) {
1275 s_attr = mspro_from_sysfs_attr(msb->attr_group
1277 kfree(s_attr->data);
1280 kfree(msb->attr_group.attrs);
1286 static int mspro_block_check_card(struct memstick_dev *card)
1288 struct mspro_block_data *msb = memstick_get_drvdata(card);
1290 return (msb->active == 1);
1293 static int mspro_block_probe(struct memstick_dev *card)
1295 struct mspro_block_data *msb;
1298 msb = kzalloc(sizeof(struct mspro_block_data), GFP_KERNEL);
1301 memstick_set_drvdata(card, msb);
1303 spin_lock_init(&msb->q_lock);
1305 rc = mspro_block_init_card(card);
1310 rc = sysfs_create_group(&card->dev.kobj, &msb->attr_group);
1314 rc = mspro_block_init_disk(card);
1316 card->check = mspro_block_check_card;
1317 card->stop = mspro_block_stop;
1318 card->start = mspro_block_start;
1322 sysfs_remove_group(&card->dev.kobj, &msb->attr_group);
1324 memstick_set_drvdata(card, NULL);
1325 mspro_block_data_clear(msb);
1330 static void mspro_block_remove(struct memstick_dev *card)
1332 struct mspro_block_data *msb = memstick_get_drvdata(card);
1333 unsigned long flags;
1335 del_gendisk(msb->disk);
1336 dev_dbg(&card->dev, "mspro block remove\n");
1337 spin_lock_irqsave(&msb->q_lock, flags);
1339 blk_start_queue(msb->queue);
1340 spin_unlock_irqrestore(&msb->q_lock, flags);
1342 blk_cleanup_queue(msb->queue);
1345 sysfs_remove_group(&card->dev.kobj, &msb->attr_group);
1347 mutex_lock(&mspro_block_disk_lock);
1348 mspro_block_data_clear(msb);
1349 mutex_unlock(&mspro_block_disk_lock);
1351 mspro_block_disk_release(msb->disk);
1352 memstick_set_drvdata(card, NULL);
1357 static int mspro_block_suspend(struct memstick_dev *card, pm_message_t state)
1359 struct mspro_block_data *msb = memstick_get_drvdata(card);
1360 unsigned long flags;
1362 spin_lock_irqsave(&msb->q_lock, flags);
1363 blk_stop_queue(msb->queue);
1365 spin_unlock_irqrestore(&msb->q_lock, flags);
1370 static int mspro_block_resume(struct memstick_dev *card)
1372 struct mspro_block_data *msb = memstick_get_drvdata(card);
1373 unsigned long flags;
1376 #ifdef CONFIG_MEMSTICK_UNSAFE_RESUME
1378 struct mspro_block_data *new_msb;
1379 struct memstick_host *host = card->host;
1380 struct mspro_sys_attr *s_attr, *r_attr;
1383 mutex_lock(&host->lock);
1384 new_msb = kzalloc(sizeof(struct mspro_block_data), GFP_KERNEL);
1390 new_msb->card = card;
1391 memstick_set_drvdata(card, new_msb);
1392 if (mspro_block_init_card(card))
1395 for (cnt = 0; new_msb->attr_group.attrs[cnt]
1396 && msb->attr_group.attrs[cnt]; ++cnt) {
1397 s_attr = mspro_from_sysfs_attr(new_msb->attr_group.attrs[cnt]);
1398 r_attr = mspro_from_sysfs_attr(msb->attr_group.attrs[cnt]);
1400 if (s_attr->id == MSPRO_BLOCK_ID_SYSINFO
1401 && r_attr->id == s_attr->id) {
1402 if (memcmp(s_attr->data, r_attr->data, s_attr->size))
1411 memstick_set_drvdata(card, msb);
1412 mspro_block_data_clear(new_msb);
1415 mutex_unlock(&host->lock);
1417 #endif /* CONFIG_MEMSTICK_UNSAFE_RESUME */
1419 spin_lock_irqsave(&msb->q_lock, flags);
1420 blk_start_queue(msb->queue);
1421 spin_unlock_irqrestore(&msb->q_lock, flags);
1427 #define mspro_block_suspend NULL
1428 #define mspro_block_resume NULL
1430 #endif /* CONFIG_PM */
1432 static struct memstick_device_id mspro_block_id_tbl[] = {
1433 {MEMSTICK_MATCH_ALL, MEMSTICK_TYPE_PRO, MEMSTICK_CATEGORY_STORAGE_DUO,
1434 MEMSTICK_CLASS_DUO},
1439 static struct memstick_driver mspro_block_driver = {
1441 .name = DRIVER_NAME,
1442 .owner = THIS_MODULE
1444 .id_table = mspro_block_id_tbl,
1445 .probe = mspro_block_probe,
1446 .remove = mspro_block_remove,
1447 .suspend = mspro_block_suspend,
1448 .resume = mspro_block_resume
1451 static int __init mspro_block_init(void)
1455 rc = register_blkdev(major, DRIVER_NAME);
1457 printk(KERN_ERR DRIVER_NAME ": failed to register "
1458 "major %d, error %d\n", major, rc);
1464 rc = memstick_register_driver(&mspro_block_driver);
1466 unregister_blkdev(major, DRIVER_NAME);
1470 static void __exit mspro_block_exit(void)
1472 memstick_unregister_driver(&mspro_block_driver);
1473 unregister_blkdev(major, DRIVER_NAME);
1474 idr_destroy(&mspro_block_disk_idr);
1477 module_init(mspro_block_init);
1478 module_exit(mspro_block_exit);
1480 MODULE_LICENSE("GPL");
1481 MODULE_AUTHOR("Alex Dubov");
1482 MODULE_DESCRIPTION("Sony MemoryStickPro block device driver");
1483 MODULE_DEVICE_TABLE(memstick, mspro_block_id_tbl);