Loading...
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * Sony MemoryStick Pro storage support
4 *
5 * Copyright (C) 2007 Alex Dubov <oakad@yahoo.com>
6 *
7 * Special thanks to Carlos Corbacho for providing various MemoryStick cards
8 * that made this driver possible.
9 */
10
11#include <linux/blk-mq.h>
12#include <linux/idr.h>
13#include <linux/hdreg.h>
14#include <linux/kthread.h>
15#include <linux/delay.h>
16#include <linux/slab.h>
17#include <linux/mutex.h>
18#include <linux/memstick.h>
19#include <linux/module.h>
20
21#define DRIVER_NAME "mspro_block"
22
23static int major;
24module_param(major, int, 0644);
25
26#define MSPRO_BLOCK_MAX_SEGS 32
27#define MSPRO_BLOCK_MAX_PAGES ((2 << 16) - 1)
28
29#define MSPRO_BLOCK_SIGNATURE 0xa5c3
30#define MSPRO_BLOCK_MAX_ATTRIBUTES 41
31
32#define MSPRO_BLOCK_PART_SHIFT 3
33
34enum {
35 MSPRO_BLOCK_ID_SYSINFO = 0x10,
36 MSPRO_BLOCK_ID_MODELNAME = 0x15,
37 MSPRO_BLOCK_ID_MBR = 0x20,
38 MSPRO_BLOCK_ID_PBR16 = 0x21,
39 MSPRO_BLOCK_ID_PBR32 = 0x22,
40 MSPRO_BLOCK_ID_SPECFILEVALUES1 = 0x25,
41 MSPRO_BLOCK_ID_SPECFILEVALUES2 = 0x26,
42 MSPRO_BLOCK_ID_DEVINFO = 0x30
43};
44
45struct mspro_sys_attr {
46 size_t size;
47 void *data;
48 unsigned char id;
49 char name[32];
50 struct device_attribute dev_attr;
51};
52
53struct mspro_attr_entry {
54 __be32 address;
55 __be32 size;
56 unsigned char id;
57 unsigned char reserved[3];
58} __attribute__((packed));
59
60struct mspro_attribute {
61 __be16 signature;
62 unsigned short version;
63 unsigned char count;
64 unsigned char reserved[11];
65 struct mspro_attr_entry entries[];
66} __attribute__((packed));
67
68struct mspro_sys_info {
69 unsigned char class;
70 unsigned char reserved0;
71 __be16 block_size;
72 __be16 block_count;
73 __be16 user_block_count;
74 __be16 page_size;
75 unsigned char reserved1[2];
76 unsigned char assembly_date[8];
77 __be32 serial_number;
78 unsigned char assembly_maker_code;
79 unsigned char assembly_model_code[3];
80 __be16 memory_maker_code;
81 __be16 memory_model_code;
82 unsigned char reserved2[4];
83 unsigned char vcc;
84 unsigned char vpp;
85 __be16 controller_number;
86 __be16 controller_function;
87 __be16 start_sector;
88 __be16 unit_size;
89 unsigned char ms_sub_class;
90 unsigned char reserved3[4];
91 unsigned char interface_type;
92 __be16 controller_code;
93 unsigned char format_type;
94 unsigned char reserved4;
95 unsigned char device_type;
96 unsigned char reserved5[7];
97 unsigned char mspro_id[16];
98 unsigned char reserved6[16];
99} __attribute__((packed));
100
101struct mspro_mbr {
102 unsigned char boot_partition;
103 unsigned char start_head;
104 unsigned char start_sector;
105 unsigned char start_cylinder;
106 unsigned char partition_type;
107 unsigned char end_head;
108 unsigned char end_sector;
109 unsigned char end_cylinder;
110 unsigned int start_sectors;
111 unsigned int sectors_per_partition;
112} __attribute__((packed));
113
114struct mspro_specfile {
115 char name[8];
116 char ext[3];
117 unsigned char attr;
118 unsigned char reserved[10];
119 unsigned short time;
120 unsigned short date;
121 unsigned short cluster;
122 unsigned int size;
123} __attribute__((packed));
124
125struct mspro_devinfo {
126 __be16 cylinders;
127 __be16 heads;
128 __be16 bytes_per_track;
129 __be16 bytes_per_sector;
130 __be16 sectors_per_track;
131 unsigned char reserved[6];
132} __attribute__((packed));
133
134struct mspro_block_data {
135 struct memstick_dev *card;
136 unsigned int usage_count;
137 unsigned int caps;
138 struct gendisk *disk;
139 struct request_queue *queue;
140 struct request *block_req;
141 struct blk_mq_tag_set tag_set;
142 spinlock_t q_lock;
143
144 unsigned short page_size;
145 unsigned short cylinders;
146 unsigned short heads;
147 unsigned short sectors_per_track;
148
149 unsigned char system;
150 unsigned char read_only:1,
151 eject:1,
152 data_dir:1,
153 active:1;
154 unsigned char transfer_cmd;
155
156 int (*mrq_handler)(struct memstick_dev *card,
157 struct memstick_request **mrq);
158
159
160 /* Default request setup function for data access method preferred by
161 * this host instance.
162 */
163 void (*setup_transfer)(struct memstick_dev *card,
164 u64 offset, size_t length);
165
166 struct attribute_group attr_group;
167
168 struct scatterlist req_sg[MSPRO_BLOCK_MAX_SEGS];
169 unsigned int seg_count;
170 unsigned int current_seg;
171 unsigned int current_page;
172};
173
174static DEFINE_IDR(mspro_block_disk_idr);
175static DEFINE_MUTEX(mspro_block_disk_lock);
176
177static int mspro_block_complete_req(struct memstick_dev *card, int error);
178
179/*** Block device ***/
180
181static int mspro_block_bd_open(struct block_device *bdev, fmode_t mode)
182{
183 struct gendisk *disk = bdev->bd_disk;
184 struct mspro_block_data *msb = disk->private_data;
185 int rc = -ENXIO;
186
187 mutex_lock(&mspro_block_disk_lock);
188
189 if (msb && msb->card) {
190 msb->usage_count++;
191 if ((mode & FMODE_WRITE) && msb->read_only)
192 rc = -EROFS;
193 else
194 rc = 0;
195 }
196
197 mutex_unlock(&mspro_block_disk_lock);
198
199 return rc;
200}
201
202
203static void mspro_block_disk_release(struct gendisk *disk)
204{
205 struct mspro_block_data *msb = disk->private_data;
206 int disk_id = MINOR(disk_devt(disk)) >> MSPRO_BLOCK_PART_SHIFT;
207
208 mutex_lock(&mspro_block_disk_lock);
209
210 if (msb) {
211 if (msb->usage_count)
212 msb->usage_count--;
213
214 if (!msb->usage_count) {
215 kfree(msb);
216 disk->private_data = NULL;
217 idr_remove(&mspro_block_disk_idr, disk_id);
218 put_disk(disk);
219 }
220 }
221
222 mutex_unlock(&mspro_block_disk_lock);
223}
224
225static void mspro_block_bd_release(struct gendisk *disk, fmode_t mode)
226{
227 mspro_block_disk_release(disk);
228}
229
230static int mspro_block_bd_getgeo(struct block_device *bdev,
231 struct hd_geometry *geo)
232{
233 struct mspro_block_data *msb = bdev->bd_disk->private_data;
234
235 geo->heads = msb->heads;
236 geo->sectors = msb->sectors_per_track;
237 geo->cylinders = msb->cylinders;
238
239 return 0;
240}
241
242static const struct block_device_operations ms_block_bdops = {
243 .open = mspro_block_bd_open,
244 .release = mspro_block_bd_release,
245 .getgeo = mspro_block_bd_getgeo,
246 .owner = THIS_MODULE
247};
248
249/*** Information ***/
250
251static struct mspro_sys_attr *mspro_from_sysfs_attr(struct attribute *attr)
252{
253 struct device_attribute *dev_attr
254 = container_of(attr, struct device_attribute, attr);
255 return container_of(dev_attr, struct mspro_sys_attr, dev_attr);
256}
257
258static const char *mspro_block_attr_name(unsigned char tag)
259{
260 switch (tag) {
261 case MSPRO_BLOCK_ID_SYSINFO:
262 return "attr_sysinfo";
263 case MSPRO_BLOCK_ID_MODELNAME:
264 return "attr_modelname";
265 case MSPRO_BLOCK_ID_MBR:
266 return "attr_mbr";
267 case MSPRO_BLOCK_ID_PBR16:
268 return "attr_pbr16";
269 case MSPRO_BLOCK_ID_PBR32:
270 return "attr_pbr32";
271 case MSPRO_BLOCK_ID_SPECFILEVALUES1:
272 return "attr_specfilevalues1";
273 case MSPRO_BLOCK_ID_SPECFILEVALUES2:
274 return "attr_specfilevalues2";
275 case MSPRO_BLOCK_ID_DEVINFO:
276 return "attr_devinfo";
277 default:
278 return NULL;
279 };
280}
281
282typedef ssize_t (*sysfs_show_t)(struct device *dev,
283 struct device_attribute *attr,
284 char *buffer);
285
286static ssize_t mspro_block_attr_show_default(struct device *dev,
287 struct device_attribute *attr,
288 char *buffer)
289{
290 struct mspro_sys_attr *s_attr = container_of(attr,
291 struct mspro_sys_attr,
292 dev_attr);
293
294 ssize_t cnt, rc = 0;
295
296 for (cnt = 0; cnt < s_attr->size; cnt++) {
297 if (cnt && !(cnt % 16)) {
298 if (PAGE_SIZE - rc)
299 buffer[rc++] = '\n';
300 }
301
302 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "%02x ",
303 ((unsigned char *)s_attr->data)[cnt]);
304 }
305 return rc;
306}
307
308static ssize_t mspro_block_attr_show_sysinfo(struct device *dev,
309 struct device_attribute *attr,
310 char *buffer)
311{
312 struct mspro_sys_attr *x_attr = container_of(attr,
313 struct mspro_sys_attr,
314 dev_attr);
315 struct mspro_sys_info *x_sys = x_attr->data;
316 ssize_t rc = 0;
317 int date_tz = 0, date_tz_f = 0;
318
319 if (x_sys->assembly_date[0] > 0x80U) {
320 date_tz = (~x_sys->assembly_date[0]) + 1;
321 date_tz_f = date_tz & 3;
322 date_tz >>= 2;
323 date_tz = -date_tz;
324 date_tz_f *= 15;
325 } else if (x_sys->assembly_date[0] < 0x80U) {
326 date_tz = x_sys->assembly_date[0];
327 date_tz_f = date_tz & 3;
328 date_tz >>= 2;
329 date_tz_f *= 15;
330 }
331
332 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "class: %x\n",
333 x_sys->class);
334 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "block size: %x\n",
335 be16_to_cpu(x_sys->block_size));
336 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "block count: %x\n",
337 be16_to_cpu(x_sys->block_count));
338 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "user block count: %x\n",
339 be16_to_cpu(x_sys->user_block_count));
340 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "page size: %x\n",
341 be16_to_cpu(x_sys->page_size));
342 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "assembly date: "
343 "GMT%+d:%d %04u-%02u-%02u %02u:%02u:%02u\n",
344 date_tz, date_tz_f,
345 be16_to_cpup((__be16 *)&x_sys->assembly_date[1]),
346 x_sys->assembly_date[3], x_sys->assembly_date[4],
347 x_sys->assembly_date[5], x_sys->assembly_date[6],
348 x_sys->assembly_date[7]);
349 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "serial number: %x\n",
350 be32_to_cpu(x_sys->serial_number));
351 rc += scnprintf(buffer + rc, PAGE_SIZE - rc,
352 "assembly maker code: %x\n",
353 x_sys->assembly_maker_code);
354 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "assembly model code: "
355 "%02x%02x%02x\n", x_sys->assembly_model_code[0],
356 x_sys->assembly_model_code[1],
357 x_sys->assembly_model_code[2]);
358 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "memory maker code: %x\n",
359 be16_to_cpu(x_sys->memory_maker_code));
360 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "memory model code: %x\n",
361 be16_to_cpu(x_sys->memory_model_code));
362 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "vcc: %x\n",
363 x_sys->vcc);
364 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "vpp: %x\n",
365 x_sys->vpp);
366 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "controller number: %x\n",
367 be16_to_cpu(x_sys->controller_number));
368 rc += scnprintf(buffer + rc, PAGE_SIZE - rc,
369 "controller function: %x\n",
370 be16_to_cpu(x_sys->controller_function));
371 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "start sector: %x\n",
372 be16_to_cpu(x_sys->start_sector));
373 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "unit size: %x\n",
374 be16_to_cpu(x_sys->unit_size));
375 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "sub class: %x\n",
376 x_sys->ms_sub_class);
377 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "interface type: %x\n",
378 x_sys->interface_type);
379 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "controller code: %x\n",
380 be16_to_cpu(x_sys->controller_code));
381 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "format type: %x\n",
382 x_sys->format_type);
383 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "device type: %x\n",
384 x_sys->device_type);
385 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "mspro id: %s\n",
386 x_sys->mspro_id);
387 return rc;
388}
389
390static ssize_t mspro_block_attr_show_modelname(struct device *dev,
391 struct device_attribute *attr,
392 char *buffer)
393{
394 struct mspro_sys_attr *s_attr = container_of(attr,
395 struct mspro_sys_attr,
396 dev_attr);
397
398 return scnprintf(buffer, PAGE_SIZE, "%s", (char *)s_attr->data);
399}
400
401static ssize_t mspro_block_attr_show_mbr(struct device *dev,
402 struct device_attribute *attr,
403 char *buffer)
404{
405 struct mspro_sys_attr *x_attr = container_of(attr,
406 struct mspro_sys_attr,
407 dev_attr);
408 struct mspro_mbr *x_mbr = x_attr->data;
409 ssize_t rc = 0;
410
411 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "boot partition: %x\n",
412 x_mbr->boot_partition);
413 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "start head: %x\n",
414 x_mbr->start_head);
415 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "start sector: %x\n",
416 x_mbr->start_sector);
417 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "start cylinder: %x\n",
418 x_mbr->start_cylinder);
419 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "partition type: %x\n",
420 x_mbr->partition_type);
421 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "end head: %x\n",
422 x_mbr->end_head);
423 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "end sector: %x\n",
424 x_mbr->end_sector);
425 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "end cylinder: %x\n",
426 x_mbr->end_cylinder);
427 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "start sectors: %x\n",
428 x_mbr->start_sectors);
429 rc += scnprintf(buffer + rc, PAGE_SIZE - rc,
430 "sectors per partition: %x\n",
431 x_mbr->sectors_per_partition);
432 return rc;
433}
434
435static ssize_t mspro_block_attr_show_specfile(struct device *dev,
436 struct device_attribute *attr,
437 char *buffer)
438{
439 struct mspro_sys_attr *x_attr = container_of(attr,
440 struct mspro_sys_attr,
441 dev_attr);
442 struct mspro_specfile *x_spfile = x_attr->data;
443 char name[9], ext[4];
444 ssize_t rc = 0;
445
446 memcpy(name, x_spfile->name, 8);
447 name[8] = 0;
448 memcpy(ext, x_spfile->ext, 3);
449 ext[3] = 0;
450
451 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "name: %s\n", name);
452 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "ext: %s\n", ext);
453 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "attribute: %x\n",
454 x_spfile->attr);
455 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "time: %d:%d:%d\n",
456 x_spfile->time >> 11,
457 (x_spfile->time >> 5) & 0x3f,
458 (x_spfile->time & 0x1f) * 2);
459 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "date: %d-%d-%d\n",
460 (x_spfile->date >> 9) + 1980,
461 (x_spfile->date >> 5) & 0xf,
462 x_spfile->date & 0x1f);
463 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "start cluster: %x\n",
464 x_spfile->cluster);
465 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "size: %x\n",
466 x_spfile->size);
467 return rc;
468}
469
470static ssize_t mspro_block_attr_show_devinfo(struct device *dev,
471 struct device_attribute *attr,
472 char *buffer)
473{
474 struct mspro_sys_attr *x_attr = container_of(attr,
475 struct mspro_sys_attr,
476 dev_attr);
477 struct mspro_devinfo *x_devinfo = x_attr->data;
478 ssize_t rc = 0;
479
480 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "cylinders: %x\n",
481 be16_to_cpu(x_devinfo->cylinders));
482 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "heads: %x\n",
483 be16_to_cpu(x_devinfo->heads));
484 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "bytes per track: %x\n",
485 be16_to_cpu(x_devinfo->bytes_per_track));
486 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "bytes per sector: %x\n",
487 be16_to_cpu(x_devinfo->bytes_per_sector));
488 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "sectors per track: %x\n",
489 be16_to_cpu(x_devinfo->sectors_per_track));
490 return rc;
491}
492
493static sysfs_show_t mspro_block_attr_show(unsigned char tag)
494{
495 switch (tag) {
496 case MSPRO_BLOCK_ID_SYSINFO:
497 return mspro_block_attr_show_sysinfo;
498 case MSPRO_BLOCK_ID_MODELNAME:
499 return mspro_block_attr_show_modelname;
500 case MSPRO_BLOCK_ID_MBR:
501 return mspro_block_attr_show_mbr;
502 case MSPRO_BLOCK_ID_SPECFILEVALUES1:
503 case MSPRO_BLOCK_ID_SPECFILEVALUES2:
504 return mspro_block_attr_show_specfile;
505 case MSPRO_BLOCK_ID_DEVINFO:
506 return mspro_block_attr_show_devinfo;
507 default:
508 return mspro_block_attr_show_default;
509 }
510}
511
512/*** Protocol handlers ***/
513
514/*
515 * Functions prefixed with "h_" are protocol callbacks. They can be called from
516 * interrupt context. Return value of 0 means that request processing is still
517 * ongoing, while special error value of -EAGAIN means that current request is
518 * finished (and request processor should come back some time later).
519 */
520
521static int h_mspro_block_req_init(struct memstick_dev *card,
522 struct memstick_request **mrq)
523{
524 struct mspro_block_data *msb = memstick_get_drvdata(card);
525
526 *mrq = &card->current_mrq;
527 card->next_request = msb->mrq_handler;
528 return 0;
529}
530
531static int h_mspro_block_default(struct memstick_dev *card,
532 struct memstick_request **mrq)
533{
534 return mspro_block_complete_req(card, (*mrq)->error);
535}
536
537static int h_mspro_block_default_bad(struct memstick_dev *card,
538 struct memstick_request **mrq)
539{
540 return -ENXIO;
541}
542
543static int h_mspro_block_get_ro(struct memstick_dev *card,
544 struct memstick_request **mrq)
545{
546 struct mspro_block_data *msb = memstick_get_drvdata(card);
547
548 if (!(*mrq)->error) {
549 if ((*mrq)->data[offsetof(struct ms_status_register, status0)]
550 & MEMSTICK_STATUS0_WP)
551 msb->read_only = 1;
552 else
553 msb->read_only = 0;
554 }
555
556 return mspro_block_complete_req(card, (*mrq)->error);
557}
558
559static int h_mspro_block_wait_for_ced(struct memstick_dev *card,
560 struct memstick_request **mrq)
561{
562 dev_dbg(&card->dev, "wait for ced: value %x\n", (*mrq)->data[0]);
563
564 if (!(*mrq)->error) {
565 if ((*mrq)->data[0] & (MEMSTICK_INT_CMDNAK | MEMSTICK_INT_ERR))
566 (*mrq)->error = -EFAULT;
567 else if (!((*mrq)->data[0] & MEMSTICK_INT_CED))
568 return 0;
569 }
570
571 return mspro_block_complete_req(card, (*mrq)->error);
572}
573
574static int h_mspro_block_transfer_data(struct memstick_dev *card,
575 struct memstick_request **mrq)
576{
577 struct mspro_block_data *msb = memstick_get_drvdata(card);
578 unsigned char t_val = 0;
579 struct scatterlist t_sg = { 0 };
580 size_t t_offset;
581
582 if ((*mrq)->error)
583 return mspro_block_complete_req(card, (*mrq)->error);
584
585 switch ((*mrq)->tpc) {
586 case MS_TPC_WRITE_REG:
587 memstick_init_req(*mrq, MS_TPC_SET_CMD, &msb->transfer_cmd, 1);
588 (*mrq)->need_card_int = 1;
589 return 0;
590 case MS_TPC_SET_CMD:
591 t_val = (*mrq)->int_reg;
592 memstick_init_req(*mrq, MS_TPC_GET_INT, NULL, 1);
593 if (msb->caps & MEMSTICK_CAP_AUTO_GET_INT)
594 goto has_int_reg;
595 return 0;
596 case MS_TPC_GET_INT:
597 t_val = (*mrq)->data[0];
598has_int_reg:
599 if (t_val & (MEMSTICK_INT_CMDNAK | MEMSTICK_INT_ERR)) {
600 t_val = MSPRO_CMD_STOP;
601 memstick_init_req(*mrq, MS_TPC_SET_CMD, &t_val, 1);
602 card->next_request = h_mspro_block_default;
603 return 0;
604 }
605
606 if (msb->current_page
607 == (msb->req_sg[msb->current_seg].length
608 / msb->page_size)) {
609 msb->current_page = 0;
610 msb->current_seg++;
611
612 if (msb->current_seg == msb->seg_count) {
613 if (t_val & MEMSTICK_INT_CED) {
614 return mspro_block_complete_req(card,
615 0);
616 } else {
617 card->next_request
618 = h_mspro_block_wait_for_ced;
619 memstick_init_req(*mrq, MS_TPC_GET_INT,
620 NULL, 1);
621 return 0;
622 }
623 }
624 }
625
626 if (!(t_val & MEMSTICK_INT_BREQ)) {
627 memstick_init_req(*mrq, MS_TPC_GET_INT, NULL, 1);
628 return 0;
629 }
630
631 t_offset = msb->req_sg[msb->current_seg].offset;
632 t_offset += msb->current_page * msb->page_size;
633
634 sg_set_page(&t_sg,
635 nth_page(sg_page(&(msb->req_sg[msb->current_seg])),
636 t_offset >> PAGE_SHIFT),
637 msb->page_size, offset_in_page(t_offset));
638
639 memstick_init_req_sg(*mrq, msb->data_dir == READ
640 ? MS_TPC_READ_LONG_DATA
641 : MS_TPC_WRITE_LONG_DATA,
642 &t_sg);
643 (*mrq)->need_card_int = 1;
644 return 0;
645 case MS_TPC_READ_LONG_DATA:
646 case MS_TPC_WRITE_LONG_DATA:
647 msb->current_page++;
648 if (msb->caps & MEMSTICK_CAP_AUTO_GET_INT) {
649 t_val = (*mrq)->int_reg;
650 goto has_int_reg;
651 } else {
652 memstick_init_req(*mrq, MS_TPC_GET_INT, NULL, 1);
653 return 0;
654 }
655
656 default:
657 BUG();
658 }
659}
660
661/*** Transfer setup functions for different access methods. ***/
662
663/** Setup data transfer request for SET_CMD TPC with arguments in card
664 * registers.
665 *
666 * @card Current media instance
667 * @offset Target data offset in bytes
668 * @length Required transfer length in bytes.
669 */
670static void h_mspro_block_setup_cmd(struct memstick_dev *card, u64 offset,
671 size_t length)
672{
673 struct mspro_block_data *msb = memstick_get_drvdata(card);
674 struct mspro_param_register param = {
675 .system = msb->system,
676 .data_count = cpu_to_be16((uint16_t)(length / msb->page_size)),
677 /* ISO C90 warning precludes direct initialization for now. */
678 .data_address = 0,
679 .tpc_param = 0
680 };
681
682 do_div(offset, msb->page_size);
683 param.data_address = cpu_to_be32((uint32_t)offset);
684
685 card->next_request = h_mspro_block_req_init;
686 msb->mrq_handler = h_mspro_block_transfer_data;
687 memstick_init_req(&card->current_mrq, MS_TPC_WRITE_REG,
688 ¶m, sizeof(param));
689}
690
691/*** Data transfer ***/
692
693static int mspro_block_issue_req(struct memstick_dev *card)
694{
695 struct mspro_block_data *msb = memstick_get_drvdata(card);
696 u64 t_off;
697 unsigned int count;
698
699 while (true) {
700 msb->current_page = 0;
701 msb->current_seg = 0;
702 msb->seg_count = blk_rq_map_sg(msb->block_req->q,
703 msb->block_req,
704 msb->req_sg);
705
706 if (!msb->seg_count) {
707 unsigned int bytes = blk_rq_cur_bytes(msb->block_req);
708 bool chunk;
709
710 chunk = blk_update_request(msb->block_req,
711 BLK_STS_RESOURCE,
712 bytes);
713 if (chunk)
714 continue;
715 __blk_mq_end_request(msb->block_req,
716 BLK_STS_RESOURCE);
717 msb->block_req = NULL;
718 return -EAGAIN;
719 }
720
721 t_off = blk_rq_pos(msb->block_req);
722 t_off <<= 9;
723 count = blk_rq_bytes(msb->block_req);
724
725 msb->setup_transfer(card, t_off, count);
726
727 msb->data_dir = rq_data_dir(msb->block_req);
728 msb->transfer_cmd = msb->data_dir == READ
729 ? MSPRO_CMD_READ_DATA
730 : MSPRO_CMD_WRITE_DATA;
731
732 memstick_new_req(card->host);
733 return 0;
734 }
735}
736
737static int mspro_block_complete_req(struct memstick_dev *card, int error)
738{
739 struct mspro_block_data *msb = memstick_get_drvdata(card);
740 int cnt;
741 bool chunk;
742 unsigned int t_len = 0;
743 unsigned long flags;
744
745 spin_lock_irqsave(&msb->q_lock, flags);
746 dev_dbg(&card->dev, "complete %d, %d\n", msb->block_req ? 1 : 0,
747 error);
748
749 if (msb->block_req) {
750 /* Nothing to do - not really an error */
751 if (error == -EAGAIN)
752 error = 0;
753
754 if (error || (card->current_mrq.tpc == MSPRO_CMD_STOP)) {
755 if (msb->data_dir == READ) {
756 for (cnt = 0; cnt < msb->current_seg; cnt++) {
757 t_len += msb->req_sg[cnt].length
758 / msb->page_size;
759
760 if (msb->current_page)
761 t_len += msb->current_page - 1;
762
763 t_len *= msb->page_size;
764 }
765 }
766 } else
767 t_len = blk_rq_bytes(msb->block_req);
768
769 dev_dbg(&card->dev, "transferred %x (%d)\n", t_len, error);
770
771 if (error && !t_len)
772 t_len = blk_rq_cur_bytes(msb->block_req);
773
774 chunk = blk_update_request(msb->block_req,
775 errno_to_blk_status(error), t_len);
776 if (chunk) {
777 error = mspro_block_issue_req(card);
778 if (!error)
779 goto out;
780 } else {
781 __blk_mq_end_request(msb->block_req,
782 errno_to_blk_status(error));
783 msb->block_req = NULL;
784 }
785 } else {
786 if (!error)
787 error = -EAGAIN;
788 }
789
790 card->next_request = h_mspro_block_default_bad;
791 complete_all(&card->mrq_complete);
792out:
793 spin_unlock_irqrestore(&msb->q_lock, flags);
794 return error;
795}
796
797static void mspro_block_stop(struct memstick_dev *card)
798{
799 struct mspro_block_data *msb = memstick_get_drvdata(card);
800 int rc = 0;
801 unsigned long flags;
802
803 while (1) {
804 spin_lock_irqsave(&msb->q_lock, flags);
805 if (!msb->block_req) {
806 blk_mq_stop_hw_queues(msb->queue);
807 rc = 1;
808 }
809 spin_unlock_irqrestore(&msb->q_lock, flags);
810
811 if (rc)
812 break;
813
814 wait_for_completion(&card->mrq_complete);
815 }
816}
817
818static void mspro_block_start(struct memstick_dev *card)
819{
820 struct mspro_block_data *msb = memstick_get_drvdata(card);
821
822 blk_mq_start_hw_queues(msb->queue);
823}
824
825static blk_status_t mspro_queue_rq(struct blk_mq_hw_ctx *hctx,
826 const struct blk_mq_queue_data *bd)
827{
828 struct memstick_dev *card = hctx->queue->queuedata;
829 struct mspro_block_data *msb = memstick_get_drvdata(card);
830
831 spin_lock_irq(&msb->q_lock);
832
833 if (msb->block_req) {
834 spin_unlock_irq(&msb->q_lock);
835 return BLK_STS_DEV_RESOURCE;
836 }
837
838 if (msb->eject) {
839 spin_unlock_irq(&msb->q_lock);
840 blk_mq_start_request(bd->rq);
841 return BLK_STS_IOERR;
842 }
843
844 msb->block_req = bd->rq;
845 blk_mq_start_request(bd->rq);
846
847 if (mspro_block_issue_req(card))
848 msb->block_req = NULL;
849
850 spin_unlock_irq(&msb->q_lock);
851 return BLK_STS_OK;
852}
853
854/*** Initialization ***/
855
856static int mspro_block_wait_for_ced(struct memstick_dev *card)
857{
858 struct mspro_block_data *msb = memstick_get_drvdata(card);
859
860 card->next_request = h_mspro_block_req_init;
861 msb->mrq_handler = h_mspro_block_wait_for_ced;
862 memstick_init_req(&card->current_mrq, MS_TPC_GET_INT, NULL, 1);
863 memstick_new_req(card->host);
864 wait_for_completion(&card->mrq_complete);
865 return card->current_mrq.error;
866}
867
868static int mspro_block_set_interface(struct memstick_dev *card,
869 unsigned char sys_reg)
870{
871 struct memstick_host *host = card->host;
872 struct mspro_block_data *msb = memstick_get_drvdata(card);
873 struct mspro_param_register param = {
874 .system = sys_reg,
875 .data_count = 0,
876 .data_address = 0,
877 .tpc_param = 0
878 };
879
880 card->next_request = h_mspro_block_req_init;
881 msb->mrq_handler = h_mspro_block_default;
882 memstick_init_req(&card->current_mrq, MS_TPC_WRITE_REG, ¶m,
883 sizeof(param));
884 memstick_new_req(host);
885 wait_for_completion(&card->mrq_complete);
886 return card->current_mrq.error;
887}
888
889static int mspro_block_switch_interface(struct memstick_dev *card)
890{
891 struct memstick_host *host = card->host;
892 struct mspro_block_data *msb = memstick_get_drvdata(card);
893 int rc = 0;
894
895try_again:
896 if (msb->caps & MEMSTICK_CAP_PAR4)
897 rc = mspro_block_set_interface(card, MEMSTICK_SYS_PAR4);
898 else
899 return 0;
900
901 if (rc) {
902 printk(KERN_WARNING
903 "%s: could not switch to 4-bit mode, error %d\n",
904 dev_name(&card->dev), rc);
905 return 0;
906 }
907
908 msb->system = MEMSTICK_SYS_PAR4;
909 host->set_param(host, MEMSTICK_INTERFACE, MEMSTICK_PAR4);
910 printk(KERN_INFO "%s: switching to 4-bit parallel mode\n",
911 dev_name(&card->dev));
912
913 if (msb->caps & MEMSTICK_CAP_PAR8) {
914 rc = mspro_block_set_interface(card, MEMSTICK_SYS_PAR8);
915
916 if (!rc) {
917 msb->system = MEMSTICK_SYS_PAR8;
918 host->set_param(host, MEMSTICK_INTERFACE,
919 MEMSTICK_PAR8);
920 printk(KERN_INFO
921 "%s: switching to 8-bit parallel mode\n",
922 dev_name(&card->dev));
923 } else
924 printk(KERN_WARNING
925 "%s: could not switch to 8-bit mode, error %d\n",
926 dev_name(&card->dev), rc);
927 }
928
929 card->next_request = h_mspro_block_req_init;
930 msb->mrq_handler = h_mspro_block_default;
931 memstick_init_req(&card->current_mrq, MS_TPC_GET_INT, NULL, 1);
932 memstick_new_req(card->host);
933 wait_for_completion(&card->mrq_complete);
934 rc = card->current_mrq.error;
935
936 if (rc) {
937 printk(KERN_WARNING
938 "%s: interface error, trying to fall back to serial\n",
939 dev_name(&card->dev));
940 msb->system = MEMSTICK_SYS_SERIAL;
941 host->set_param(host, MEMSTICK_POWER, MEMSTICK_POWER_OFF);
942 msleep(10);
943 host->set_param(host, MEMSTICK_POWER, MEMSTICK_POWER_ON);
944 host->set_param(host, MEMSTICK_INTERFACE, MEMSTICK_SERIAL);
945
946 rc = memstick_set_rw_addr(card);
947 if (!rc)
948 rc = mspro_block_set_interface(card, msb->system);
949
950 if (!rc) {
951 msleep(150);
952 rc = mspro_block_wait_for_ced(card);
953 if (rc)
954 return rc;
955
956 if (msb->caps & MEMSTICK_CAP_PAR8) {
957 msb->caps &= ~MEMSTICK_CAP_PAR8;
958 goto try_again;
959 }
960 }
961 }
962 return rc;
963}
964
965/* Memory allocated for attributes by this function should be freed by
966 * mspro_block_data_clear, no matter if the initialization process succeeded
967 * or failed.
968 */
969static int mspro_block_read_attributes(struct memstick_dev *card)
970{
971 struct mspro_block_data *msb = memstick_get_drvdata(card);
972 struct mspro_attribute *attr = NULL;
973 struct mspro_sys_attr *s_attr = NULL;
974 unsigned char *buffer = NULL;
975 int cnt, rc, attr_count;
976 /* While normally physical device offsets, represented here by
977 * attr_offset and attr_len will be of large numeric types, we can be
978 * sure, that attributes are close enough to the beginning of the
979 * device, to save ourselves some trouble.
980 */
981 unsigned int addr, attr_offset = 0, attr_len = msb->page_size;
982
983 attr = kmalloc(msb->page_size, GFP_KERNEL);
984 if (!attr)
985 return -ENOMEM;
986
987 sg_init_one(&msb->req_sg[0], attr, msb->page_size);
988 msb->seg_count = 1;
989 msb->current_seg = 0;
990 msb->current_page = 0;
991 msb->data_dir = READ;
992 msb->transfer_cmd = MSPRO_CMD_READ_ATRB;
993
994 msb->setup_transfer(card, attr_offset, attr_len);
995
996 memstick_new_req(card->host);
997 wait_for_completion(&card->mrq_complete);
998 if (card->current_mrq.error) {
999 rc = card->current_mrq.error;
1000 goto out_free_attr;
1001 }
1002
1003 if (be16_to_cpu(attr->signature) != MSPRO_BLOCK_SIGNATURE) {
1004 printk(KERN_ERR "%s: unrecognized device signature %x\n",
1005 dev_name(&card->dev), be16_to_cpu(attr->signature));
1006 rc = -ENODEV;
1007 goto out_free_attr;
1008 }
1009
1010 if (attr->count > MSPRO_BLOCK_MAX_ATTRIBUTES) {
1011 printk(KERN_WARNING "%s: way too many attribute entries\n",
1012 dev_name(&card->dev));
1013 attr_count = MSPRO_BLOCK_MAX_ATTRIBUTES;
1014 } else
1015 attr_count = attr->count;
1016
1017 msb->attr_group.attrs = kcalloc(attr_count + 1,
1018 sizeof(*msb->attr_group.attrs),
1019 GFP_KERNEL);
1020 if (!msb->attr_group.attrs) {
1021 rc = -ENOMEM;
1022 goto out_free_attr;
1023 }
1024 msb->attr_group.name = "media_attributes";
1025
1026 buffer = kmemdup(attr, attr_len, GFP_KERNEL);
1027 if (!buffer) {
1028 rc = -ENOMEM;
1029 goto out_free_attr;
1030 }
1031
1032 for (cnt = 0; cnt < attr_count; ++cnt) {
1033 s_attr = kzalloc(sizeof(struct mspro_sys_attr), GFP_KERNEL);
1034 if (!s_attr) {
1035 rc = -ENOMEM;
1036 goto out_free_buffer;
1037 }
1038
1039 msb->attr_group.attrs[cnt] = &s_attr->dev_attr.attr;
1040 addr = be32_to_cpu(attr->entries[cnt].address);
1041 s_attr->size = be32_to_cpu(attr->entries[cnt].size);
1042 dev_dbg(&card->dev, "adding attribute %d: id %x, address %x, "
1043 "size %zx\n", cnt, attr->entries[cnt].id, addr,
1044 s_attr->size);
1045 s_attr->id = attr->entries[cnt].id;
1046 if (mspro_block_attr_name(s_attr->id))
1047 snprintf(s_attr->name, sizeof(s_attr->name), "%s",
1048 mspro_block_attr_name(attr->entries[cnt].id));
1049 else
1050 snprintf(s_attr->name, sizeof(s_attr->name),
1051 "attr_x%02x", attr->entries[cnt].id);
1052
1053 sysfs_attr_init(&s_attr->dev_attr.attr);
1054 s_attr->dev_attr.attr.name = s_attr->name;
1055 s_attr->dev_attr.attr.mode = S_IRUGO;
1056 s_attr->dev_attr.show = mspro_block_attr_show(s_attr->id);
1057
1058 if (!s_attr->size)
1059 continue;
1060
1061 s_attr->data = kmalloc(s_attr->size, GFP_KERNEL);
1062 if (!s_attr->data) {
1063 rc = -ENOMEM;
1064 goto out_free_buffer;
1065 }
1066
1067 if (((addr / msb->page_size) == (attr_offset / msb->page_size))
1068 && (((addr + s_attr->size - 1) / msb->page_size)
1069 == (attr_offset / msb->page_size))) {
1070 memcpy(s_attr->data, buffer + addr % msb->page_size,
1071 s_attr->size);
1072 continue;
1073 }
1074
1075 attr_offset = (addr / msb->page_size) * msb->page_size;
1076
1077 if ((attr_offset + attr_len) < (addr + s_attr->size)) {
1078 kfree(buffer);
1079 attr_len = (((addr + s_attr->size) / msb->page_size)
1080 + 1 ) * msb->page_size - attr_offset;
1081 buffer = kmalloc(attr_len, GFP_KERNEL);
1082 if (!buffer) {
1083 rc = -ENOMEM;
1084 goto out_free_attr;
1085 }
1086 }
1087
1088 sg_init_one(&msb->req_sg[0], buffer, attr_len);
1089 msb->seg_count = 1;
1090 msb->current_seg = 0;
1091 msb->current_page = 0;
1092 msb->data_dir = READ;
1093 msb->transfer_cmd = MSPRO_CMD_READ_ATRB;
1094
1095 dev_dbg(&card->dev, "reading attribute range %x, %x\n",
1096 attr_offset, attr_len);
1097
1098 msb->setup_transfer(card, attr_offset, attr_len);
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;
1104 }
1105
1106 memcpy(s_attr->data, buffer + addr % msb->page_size,
1107 s_attr->size);
1108 }
1109
1110 rc = 0;
1111out_free_buffer:
1112 kfree(buffer);
1113out_free_attr:
1114 kfree(attr);
1115 return rc;
1116}
1117
1118static int mspro_block_init_card(struct memstick_dev *card)
1119{
1120 struct mspro_block_data *msb = memstick_get_drvdata(card);
1121 struct memstick_host *host = card->host;
1122 int rc = 0;
1123
1124 msb->system = MEMSTICK_SYS_SERIAL;
1125 msb->setup_transfer = h_mspro_block_setup_cmd;
1126
1127 card->reg_addr.r_offset = offsetof(struct mspro_register, status);
1128 card->reg_addr.r_length = sizeof(struct ms_status_register);
1129 card->reg_addr.w_offset = offsetof(struct mspro_register, param);
1130 card->reg_addr.w_length = sizeof(struct mspro_param_register);
1131
1132 if (memstick_set_rw_addr(card))
1133 return -EIO;
1134
1135 msb->caps = host->caps;
1136
1137 msleep(150);
1138 rc = mspro_block_wait_for_ced(card);
1139 if (rc)
1140 return rc;
1141
1142 rc = mspro_block_switch_interface(card);
1143 if (rc)
1144 return rc;
1145
1146 dev_dbg(&card->dev, "card activated\n");
1147 if (msb->system != MEMSTICK_SYS_SERIAL)
1148 msb->caps |= MEMSTICK_CAP_AUTO_GET_INT;
1149
1150 card->next_request = h_mspro_block_req_init;
1151 msb->mrq_handler = h_mspro_block_get_ro;
1152 memstick_init_req(&card->current_mrq, MS_TPC_READ_REG, NULL,
1153 sizeof(struct ms_status_register));
1154 memstick_new_req(card->host);
1155 wait_for_completion(&card->mrq_complete);
1156 if (card->current_mrq.error)
1157 return card->current_mrq.error;
1158
1159 dev_dbg(&card->dev, "card r/w status %d\n", msb->read_only ? 0 : 1);
1160
1161 msb->page_size = 512;
1162 rc = mspro_block_read_attributes(card);
1163 if (rc)
1164 return rc;
1165
1166 dev_dbg(&card->dev, "attributes loaded\n");
1167 return 0;
1168
1169}
1170
1171static const struct blk_mq_ops mspro_mq_ops = {
1172 .queue_rq = mspro_queue_rq,
1173};
1174
1175static int mspro_block_init_disk(struct memstick_dev *card)
1176{
1177 struct mspro_block_data *msb = memstick_get_drvdata(card);
1178 struct mspro_devinfo *dev_info = NULL;
1179 struct mspro_sys_info *sys_info = NULL;
1180 struct mspro_sys_attr *s_attr = NULL;
1181 int rc, disk_id;
1182 unsigned long capacity;
1183
1184 for (rc = 0; msb->attr_group.attrs[rc]; ++rc) {
1185 s_attr = mspro_from_sysfs_attr(msb->attr_group.attrs[rc]);
1186
1187 if (s_attr->id == MSPRO_BLOCK_ID_DEVINFO)
1188 dev_info = s_attr->data;
1189 else if (s_attr->id == MSPRO_BLOCK_ID_SYSINFO)
1190 sys_info = s_attr->data;
1191 }
1192
1193 if (!dev_info || !sys_info)
1194 return -ENODEV;
1195
1196 msb->cylinders = be16_to_cpu(dev_info->cylinders);
1197 msb->heads = be16_to_cpu(dev_info->heads);
1198 msb->sectors_per_track = be16_to_cpu(dev_info->sectors_per_track);
1199
1200 msb->page_size = be16_to_cpu(sys_info->unit_size);
1201
1202 mutex_lock(&mspro_block_disk_lock);
1203 disk_id = idr_alloc(&mspro_block_disk_idr, card, 0, 256, GFP_KERNEL);
1204 mutex_unlock(&mspro_block_disk_lock);
1205 if (disk_id < 0)
1206 return disk_id;
1207
1208 msb->disk = alloc_disk(1 << MSPRO_BLOCK_PART_SHIFT);
1209 if (!msb->disk) {
1210 rc = -ENOMEM;
1211 goto out_release_id;
1212 }
1213
1214 msb->queue = blk_mq_init_sq_queue(&msb->tag_set, &mspro_mq_ops, 2,
1215 BLK_MQ_F_SHOULD_MERGE);
1216 if (IS_ERR(msb->queue)) {
1217 rc = PTR_ERR(msb->queue);
1218 msb->queue = NULL;
1219 goto out_put_disk;
1220 }
1221
1222 msb->queue->queuedata = card;
1223
1224 blk_queue_max_hw_sectors(msb->queue, MSPRO_BLOCK_MAX_PAGES);
1225 blk_queue_max_segments(msb->queue, MSPRO_BLOCK_MAX_SEGS);
1226 blk_queue_max_segment_size(msb->queue,
1227 MSPRO_BLOCK_MAX_PAGES * msb->page_size);
1228
1229 msb->disk->major = major;
1230 msb->disk->first_minor = disk_id << MSPRO_BLOCK_PART_SHIFT;
1231 msb->disk->fops = &ms_block_bdops;
1232 msb->usage_count = 1;
1233 msb->disk->private_data = msb;
1234 msb->disk->queue = msb->queue;
1235
1236 sprintf(msb->disk->disk_name, "mspblk%d", disk_id);
1237
1238 blk_queue_logical_block_size(msb->queue, msb->page_size);
1239
1240 capacity = be16_to_cpu(sys_info->user_block_count);
1241 capacity *= be16_to_cpu(sys_info->block_size);
1242 capacity *= msb->page_size >> 9;
1243 set_capacity(msb->disk, capacity);
1244 dev_dbg(&card->dev, "capacity set %ld\n", capacity);
1245
1246 device_add_disk(&card->dev, msb->disk, NULL);
1247 msb->active = 1;
1248 return 0;
1249
1250out_put_disk:
1251 put_disk(msb->disk);
1252out_release_id:
1253 mutex_lock(&mspro_block_disk_lock);
1254 idr_remove(&mspro_block_disk_idr, disk_id);
1255 mutex_unlock(&mspro_block_disk_lock);
1256 return rc;
1257}
1258
1259static void mspro_block_data_clear(struct mspro_block_data *msb)
1260{
1261 int cnt;
1262 struct mspro_sys_attr *s_attr;
1263
1264 if (msb->attr_group.attrs) {
1265 for (cnt = 0; msb->attr_group.attrs[cnt]; ++cnt) {
1266 s_attr = mspro_from_sysfs_attr(msb->attr_group
1267 .attrs[cnt]);
1268 kfree(s_attr->data);
1269 kfree(s_attr);
1270 }
1271 kfree(msb->attr_group.attrs);
1272 }
1273
1274 msb->card = NULL;
1275}
1276
1277static int mspro_block_check_card(struct memstick_dev *card)
1278{
1279 struct mspro_block_data *msb = memstick_get_drvdata(card);
1280
1281 return (msb->active == 1);
1282}
1283
1284static int mspro_block_probe(struct memstick_dev *card)
1285{
1286 struct mspro_block_data *msb;
1287 int rc = 0;
1288
1289 msb = kzalloc(sizeof(struct mspro_block_data), GFP_KERNEL);
1290 if (!msb)
1291 return -ENOMEM;
1292 memstick_set_drvdata(card, msb);
1293 msb->card = card;
1294 spin_lock_init(&msb->q_lock);
1295
1296 rc = mspro_block_init_card(card);
1297
1298 if (rc)
1299 goto out_free;
1300
1301 rc = sysfs_create_group(&card->dev.kobj, &msb->attr_group);
1302 if (rc)
1303 goto out_free;
1304
1305 rc = mspro_block_init_disk(card);
1306 if (!rc) {
1307 card->check = mspro_block_check_card;
1308 card->stop = mspro_block_stop;
1309 card->start = mspro_block_start;
1310 return 0;
1311 }
1312
1313 sysfs_remove_group(&card->dev.kobj, &msb->attr_group);
1314out_free:
1315 memstick_set_drvdata(card, NULL);
1316 mspro_block_data_clear(msb);
1317 kfree(msb);
1318 return rc;
1319}
1320
1321static void mspro_block_remove(struct memstick_dev *card)
1322{
1323 struct mspro_block_data *msb = memstick_get_drvdata(card);
1324 unsigned long flags;
1325
1326 spin_lock_irqsave(&msb->q_lock, flags);
1327 msb->eject = 1;
1328 spin_unlock_irqrestore(&msb->q_lock, flags);
1329 blk_mq_start_hw_queues(msb->queue);
1330
1331 del_gendisk(msb->disk);
1332 dev_dbg(&card->dev, "mspro block remove\n");
1333
1334 blk_cleanup_queue(msb->queue);
1335 blk_mq_free_tag_set(&msb->tag_set);
1336 msb->queue = NULL;
1337
1338 sysfs_remove_group(&card->dev.kobj, &msb->attr_group);
1339
1340 mutex_lock(&mspro_block_disk_lock);
1341 mspro_block_data_clear(msb);
1342 mutex_unlock(&mspro_block_disk_lock);
1343
1344 mspro_block_disk_release(msb->disk);
1345 memstick_set_drvdata(card, NULL);
1346}
1347
1348#ifdef CONFIG_PM
1349
1350static int mspro_block_suspend(struct memstick_dev *card, pm_message_t state)
1351{
1352 struct mspro_block_data *msb = memstick_get_drvdata(card);
1353 unsigned long flags;
1354
1355 blk_mq_stop_hw_queues(msb->queue);
1356
1357 spin_lock_irqsave(&msb->q_lock, flags);
1358 msb->active = 0;
1359 spin_unlock_irqrestore(&msb->q_lock, flags);
1360
1361 return 0;
1362}
1363
1364static int mspro_block_resume(struct memstick_dev *card)
1365{
1366 struct mspro_block_data *msb = memstick_get_drvdata(card);
1367 int rc = 0;
1368
1369#ifdef CONFIG_MEMSTICK_UNSAFE_RESUME
1370
1371 struct mspro_block_data *new_msb;
1372 struct memstick_host *host = card->host;
1373 struct mspro_sys_attr *s_attr, *r_attr;
1374 unsigned char cnt;
1375
1376 mutex_lock(&host->lock);
1377 new_msb = kzalloc(sizeof(struct mspro_block_data), GFP_KERNEL);
1378 if (!new_msb) {
1379 rc = -ENOMEM;
1380 goto out_unlock;
1381 }
1382
1383 new_msb->card = card;
1384 memstick_set_drvdata(card, new_msb);
1385 if (mspro_block_init_card(card))
1386 goto out_free;
1387
1388 for (cnt = 0; new_msb->attr_group.attrs[cnt]
1389 && msb->attr_group.attrs[cnt]; ++cnt) {
1390 s_attr = mspro_from_sysfs_attr(new_msb->attr_group.attrs[cnt]);
1391 r_attr = mspro_from_sysfs_attr(msb->attr_group.attrs[cnt]);
1392
1393 if (s_attr->id == MSPRO_BLOCK_ID_SYSINFO
1394 && r_attr->id == s_attr->id) {
1395 if (memcmp(s_attr->data, r_attr->data, s_attr->size))
1396 break;
1397
1398 msb->active = 1;
1399 break;
1400 }
1401 }
1402
1403out_free:
1404 memstick_set_drvdata(card, msb);
1405 mspro_block_data_clear(new_msb);
1406 kfree(new_msb);
1407out_unlock:
1408 mutex_unlock(&host->lock);
1409
1410#endif /* CONFIG_MEMSTICK_UNSAFE_RESUME */
1411
1412 blk_mq_start_hw_queues(msb->queue);
1413 return rc;
1414}
1415
1416#else
1417
1418#define mspro_block_suspend NULL
1419#define mspro_block_resume NULL
1420
1421#endif /* CONFIG_PM */
1422
1423static struct memstick_device_id mspro_block_id_tbl[] = {
1424 {MEMSTICK_MATCH_ALL, MEMSTICK_TYPE_PRO, MEMSTICK_CATEGORY_STORAGE_DUO,
1425 MEMSTICK_CLASS_DUO},
1426 {}
1427};
1428
1429
1430static struct memstick_driver mspro_block_driver = {
1431 .driver = {
1432 .name = DRIVER_NAME,
1433 .owner = THIS_MODULE
1434 },
1435 .id_table = mspro_block_id_tbl,
1436 .probe = mspro_block_probe,
1437 .remove = mspro_block_remove,
1438 .suspend = mspro_block_suspend,
1439 .resume = mspro_block_resume
1440};
1441
1442static int __init mspro_block_init(void)
1443{
1444 int rc = -ENOMEM;
1445
1446 rc = register_blkdev(major, DRIVER_NAME);
1447 if (rc < 0) {
1448 printk(KERN_ERR DRIVER_NAME ": failed to register "
1449 "major %d, error %d\n", major, rc);
1450 return rc;
1451 }
1452 if (!major)
1453 major = rc;
1454
1455 rc = memstick_register_driver(&mspro_block_driver);
1456 if (rc)
1457 unregister_blkdev(major, DRIVER_NAME);
1458 return rc;
1459}
1460
1461static void __exit mspro_block_exit(void)
1462{
1463 memstick_unregister_driver(&mspro_block_driver);
1464 unregister_blkdev(major, DRIVER_NAME);
1465 idr_destroy(&mspro_block_disk_idr);
1466}
1467
1468module_init(mspro_block_init);
1469module_exit(mspro_block_exit);
1470
1471MODULE_LICENSE("GPL");
1472MODULE_AUTHOR("Alex Dubov");
1473MODULE_DESCRIPTION("Sony MemoryStickPro block device driver");
1474MODULE_DEVICE_TABLE(memstick, mspro_block_id_tbl);
1/*
2 * Sony MemoryStick Pro storage support
3 *
4 * Copyright (C) 2007 Alex Dubov <oakad@yahoo.com>
5 *
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.
9 *
10 * Special thanks to Carlos Corbacho for providing various MemoryStick cards
11 * that made this driver possible.
12 *
13 */
14
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/slab.h>
21#include <linux/mutex.h>
22#include <linux/memstick.h>
23
24#define DRIVER_NAME "mspro_block"
25
26static int major;
27module_param(major, int, 0644);
28
29#define MSPRO_BLOCK_MAX_SEGS 32
30#define MSPRO_BLOCK_MAX_PAGES ((2 << 16) - 1)
31
32#define MSPRO_BLOCK_SIGNATURE 0xa5c3
33#define MSPRO_BLOCK_MAX_ATTRIBUTES 41
34
35#define MSPRO_BLOCK_PART_SHIFT 3
36
37enum {
38 MSPRO_BLOCK_ID_SYSINFO = 0x10,
39 MSPRO_BLOCK_ID_MODELNAME = 0x15,
40 MSPRO_BLOCK_ID_MBR = 0x20,
41 MSPRO_BLOCK_ID_PBR16 = 0x21,
42 MSPRO_BLOCK_ID_PBR32 = 0x22,
43 MSPRO_BLOCK_ID_SPECFILEVALUES1 = 0x25,
44 MSPRO_BLOCK_ID_SPECFILEVALUES2 = 0x26,
45 MSPRO_BLOCK_ID_DEVINFO = 0x30
46};
47
48struct mspro_sys_attr {
49 size_t size;
50 void *data;
51 unsigned char id;
52 char name[32];
53 struct device_attribute dev_attr;
54};
55
56struct mspro_attr_entry {
57 __be32 address;
58 __be32 size;
59 unsigned char id;
60 unsigned char reserved[3];
61} __attribute__((packed));
62
63struct mspro_attribute {
64 __be16 signature;
65 unsigned short version;
66 unsigned char count;
67 unsigned char reserved[11];
68 struct mspro_attr_entry entries[];
69} __attribute__((packed));
70
71struct mspro_sys_info {
72 unsigned char class;
73 unsigned char reserved0;
74 __be16 block_size;
75 __be16 block_count;
76 __be16 user_block_count;
77 __be16 page_size;
78 unsigned char reserved1[2];
79 unsigned char assembly_date[8];
80 __be32 serial_number;
81 unsigned char assembly_maker_code;
82 unsigned char assembly_model_code[3];
83 __be16 memory_maker_code;
84 __be16 memory_model_code;
85 unsigned char reserved2[4];
86 unsigned char vcc;
87 unsigned char vpp;
88 __be16 controller_number;
89 __be16 controller_function;
90 __be16 start_sector;
91 __be16 unit_size;
92 unsigned char ms_sub_class;
93 unsigned char reserved3[4];
94 unsigned char interface_type;
95 __be16 controller_code;
96 unsigned char format_type;
97 unsigned char reserved4;
98 unsigned char device_type;
99 unsigned char reserved5[7];
100 unsigned char mspro_id[16];
101 unsigned char reserved6[16];
102} __attribute__((packed));
103
104struct mspro_mbr {
105 unsigned char boot_partition;
106 unsigned char start_head;
107 unsigned char start_sector;
108 unsigned char start_cylinder;
109 unsigned char partition_type;
110 unsigned char end_head;
111 unsigned char end_sector;
112 unsigned char end_cylinder;
113 unsigned int start_sectors;
114 unsigned int sectors_per_partition;
115} __attribute__((packed));
116
117struct mspro_specfile {
118 char name[8];
119 char ext[3];
120 unsigned char attr;
121 unsigned char reserved[10];
122 unsigned short time;
123 unsigned short date;
124 unsigned short cluster;
125 unsigned int size;
126} __attribute__((packed));
127
128struct mspro_devinfo {
129 __be16 cylinders;
130 __be16 heads;
131 __be16 bytes_per_track;
132 __be16 bytes_per_sector;
133 __be16 sectors_per_track;
134 unsigned char reserved[6];
135} __attribute__((packed));
136
137struct mspro_block_data {
138 struct memstick_dev *card;
139 unsigned int usage_count;
140 unsigned int caps;
141 struct gendisk *disk;
142 struct request_queue *queue;
143 struct request *block_req;
144 spinlock_t q_lock;
145
146 unsigned short page_size;
147 unsigned short cylinders;
148 unsigned short heads;
149 unsigned short sectors_per_track;
150
151 unsigned char system;
152 unsigned char read_only:1,
153 eject:1,
154 has_request:1,
155 data_dir:1,
156 active:1;
157 unsigned char transfer_cmd;
158
159 int (*mrq_handler)(struct memstick_dev *card,
160 struct memstick_request **mrq);
161
162
163 /* Default request setup function for data access method preferred by
164 * this host instance.
165 */
166 void (*setup_transfer)(struct memstick_dev *card,
167 u64 offset, size_t length);
168
169 struct attribute_group attr_group;
170
171 struct scatterlist req_sg[MSPRO_BLOCK_MAX_SEGS];
172 unsigned int seg_count;
173 unsigned int current_seg;
174 unsigned int current_page;
175};
176
177static DEFINE_IDR(mspro_block_disk_idr);
178static DEFINE_MUTEX(mspro_block_disk_lock);
179
180static int mspro_block_complete_req(struct memstick_dev *card, int error);
181
182/*** Block device ***/
183
184static int mspro_block_bd_open(struct block_device *bdev, fmode_t mode)
185{
186 struct gendisk *disk = bdev->bd_disk;
187 struct mspro_block_data *msb = disk->private_data;
188 int rc = -ENXIO;
189
190 mutex_lock(&mspro_block_disk_lock);
191
192 if (msb && msb->card) {
193 msb->usage_count++;
194 if ((mode & FMODE_WRITE) && msb->read_only)
195 rc = -EROFS;
196 else
197 rc = 0;
198 }
199
200 mutex_unlock(&mspro_block_disk_lock);
201
202 return rc;
203}
204
205
206static int mspro_block_disk_release(struct gendisk *disk)
207{
208 struct mspro_block_data *msb = disk->private_data;
209 int disk_id = MINOR(disk_devt(disk)) >> MSPRO_BLOCK_PART_SHIFT;
210
211 mutex_lock(&mspro_block_disk_lock);
212
213 if (msb) {
214 if (msb->usage_count)
215 msb->usage_count--;
216
217 if (!msb->usage_count) {
218 kfree(msb);
219 disk->private_data = NULL;
220 idr_remove(&mspro_block_disk_idr, disk_id);
221 put_disk(disk);
222 }
223 }
224
225 mutex_unlock(&mspro_block_disk_lock);
226
227 return 0;
228}
229
230static int mspro_block_bd_release(struct gendisk *disk, fmode_t mode)
231{
232 return mspro_block_disk_release(disk);
233}
234
235static int mspro_block_bd_getgeo(struct block_device *bdev,
236 struct hd_geometry *geo)
237{
238 struct mspro_block_data *msb = bdev->bd_disk->private_data;
239
240 geo->heads = msb->heads;
241 geo->sectors = msb->sectors_per_track;
242 geo->cylinders = msb->cylinders;
243
244 return 0;
245}
246
247static const struct block_device_operations ms_block_bdops = {
248 .open = mspro_block_bd_open,
249 .release = mspro_block_bd_release,
250 .getgeo = mspro_block_bd_getgeo,
251 .owner = THIS_MODULE
252};
253
254/*** Information ***/
255
256static struct mspro_sys_attr *mspro_from_sysfs_attr(struct attribute *attr)
257{
258 struct device_attribute *dev_attr
259 = container_of(attr, struct device_attribute, attr);
260 return container_of(dev_attr, struct mspro_sys_attr, dev_attr);
261}
262
263static const char *mspro_block_attr_name(unsigned char tag)
264{
265 switch (tag) {
266 case MSPRO_BLOCK_ID_SYSINFO:
267 return "attr_sysinfo";
268 case MSPRO_BLOCK_ID_MODELNAME:
269 return "attr_modelname";
270 case MSPRO_BLOCK_ID_MBR:
271 return "attr_mbr";
272 case MSPRO_BLOCK_ID_PBR16:
273 return "attr_pbr16";
274 case MSPRO_BLOCK_ID_PBR32:
275 return "attr_pbr32";
276 case MSPRO_BLOCK_ID_SPECFILEVALUES1:
277 return "attr_specfilevalues1";
278 case MSPRO_BLOCK_ID_SPECFILEVALUES2:
279 return "attr_specfilevalues2";
280 case MSPRO_BLOCK_ID_DEVINFO:
281 return "attr_devinfo";
282 default:
283 return NULL;
284 };
285}
286
287typedef ssize_t (*sysfs_show_t)(struct device *dev,
288 struct device_attribute *attr,
289 char *buffer);
290
291static ssize_t mspro_block_attr_show_default(struct device *dev,
292 struct device_attribute *attr,
293 char *buffer)
294{
295 struct mspro_sys_attr *s_attr = container_of(attr,
296 struct mspro_sys_attr,
297 dev_attr);
298
299 ssize_t cnt, rc = 0;
300
301 for (cnt = 0; cnt < s_attr->size; cnt++) {
302 if (cnt && !(cnt % 16)) {
303 if (PAGE_SIZE - rc)
304 buffer[rc++] = '\n';
305 }
306
307 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "%02x ",
308 ((unsigned char *)s_attr->data)[cnt]);
309 }
310 return rc;
311}
312
313static ssize_t mspro_block_attr_show_sysinfo(struct device *dev,
314 struct device_attribute *attr,
315 char *buffer)
316{
317 struct mspro_sys_attr *x_attr = container_of(attr,
318 struct mspro_sys_attr,
319 dev_attr);
320 struct mspro_sys_info *x_sys = x_attr->data;
321 ssize_t rc = 0;
322 int date_tz = 0, date_tz_f = 0;
323
324 if (x_sys->assembly_date[0] > 0x80U) {
325 date_tz = (~x_sys->assembly_date[0]) + 1;
326 date_tz_f = date_tz & 3;
327 date_tz >>= 2;
328 date_tz = -date_tz;
329 date_tz_f *= 15;
330 } else if (x_sys->assembly_date[0] < 0x80U) {
331 date_tz = x_sys->assembly_date[0];
332 date_tz_f = date_tz & 3;
333 date_tz >>= 2;
334 date_tz_f *= 15;
335 }
336
337 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "class: %x\n",
338 x_sys->class);
339 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "block size: %x\n",
340 be16_to_cpu(x_sys->block_size));
341 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "block count: %x\n",
342 be16_to_cpu(x_sys->block_count));
343 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "user block count: %x\n",
344 be16_to_cpu(x_sys->user_block_count));
345 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "page size: %x\n",
346 be16_to_cpu(x_sys->page_size));
347 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "assembly date: "
348 "GMT%+d:%d %04u-%02u-%02u %02u:%02u:%02u\n",
349 date_tz, date_tz_f,
350 be16_to_cpup((__be16 *)&x_sys->assembly_date[1]),
351 x_sys->assembly_date[3], x_sys->assembly_date[4],
352 x_sys->assembly_date[5], x_sys->assembly_date[6],
353 x_sys->assembly_date[7]);
354 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "serial number: %x\n",
355 be32_to_cpu(x_sys->serial_number));
356 rc += scnprintf(buffer + rc, PAGE_SIZE - rc,
357 "assembly maker code: %x\n",
358 x_sys->assembly_maker_code);
359 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "assembly model code: "
360 "%02x%02x%02x\n", x_sys->assembly_model_code[0],
361 x_sys->assembly_model_code[1],
362 x_sys->assembly_model_code[2]);
363 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "memory maker code: %x\n",
364 be16_to_cpu(x_sys->memory_maker_code));
365 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "memory model code: %x\n",
366 be16_to_cpu(x_sys->memory_model_code));
367 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "vcc: %x\n",
368 x_sys->vcc);
369 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "vpp: %x\n",
370 x_sys->vpp);
371 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "controller number: %x\n",
372 be16_to_cpu(x_sys->controller_number));
373 rc += scnprintf(buffer + rc, PAGE_SIZE - rc,
374 "controller function: %x\n",
375 be16_to_cpu(x_sys->controller_function));
376 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "start sector: %x\n",
377 be16_to_cpu(x_sys->start_sector));
378 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "unit size: %x\n",
379 be16_to_cpu(x_sys->unit_size));
380 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "sub class: %x\n",
381 x_sys->ms_sub_class);
382 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "interface type: %x\n",
383 x_sys->interface_type);
384 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "controller code: %x\n",
385 be16_to_cpu(x_sys->controller_code));
386 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "format type: %x\n",
387 x_sys->format_type);
388 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "device type: %x\n",
389 x_sys->device_type);
390 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "mspro id: %s\n",
391 x_sys->mspro_id);
392 return rc;
393}
394
395static ssize_t mspro_block_attr_show_modelname(struct device *dev,
396 struct device_attribute *attr,
397 char *buffer)
398{
399 struct mspro_sys_attr *s_attr = container_of(attr,
400 struct mspro_sys_attr,
401 dev_attr);
402
403 return scnprintf(buffer, PAGE_SIZE, "%s", (char *)s_attr->data);
404}
405
406static ssize_t mspro_block_attr_show_mbr(struct device *dev,
407 struct device_attribute *attr,
408 char *buffer)
409{
410 struct mspro_sys_attr *x_attr = container_of(attr,
411 struct mspro_sys_attr,
412 dev_attr);
413 struct mspro_mbr *x_mbr = x_attr->data;
414 ssize_t rc = 0;
415
416 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "boot partition: %x\n",
417 x_mbr->boot_partition);
418 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "start head: %x\n",
419 x_mbr->start_head);
420 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "start sector: %x\n",
421 x_mbr->start_sector);
422 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "start cylinder: %x\n",
423 x_mbr->start_cylinder);
424 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "partition type: %x\n",
425 x_mbr->partition_type);
426 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "end head: %x\n",
427 x_mbr->end_head);
428 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "end sector: %x\n",
429 x_mbr->end_sector);
430 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "end cylinder: %x\n",
431 x_mbr->end_cylinder);
432 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "start sectors: %x\n",
433 x_mbr->start_sectors);
434 rc += scnprintf(buffer + rc, PAGE_SIZE - rc,
435 "sectors per partition: %x\n",
436 x_mbr->sectors_per_partition);
437 return rc;
438}
439
440static ssize_t mspro_block_attr_show_specfile(struct device *dev,
441 struct device_attribute *attr,
442 char *buffer)
443{
444 struct mspro_sys_attr *x_attr = container_of(attr,
445 struct mspro_sys_attr,
446 dev_attr);
447 struct mspro_specfile *x_spfile = x_attr->data;
448 char name[9], ext[4];
449 ssize_t rc = 0;
450
451 memcpy(name, x_spfile->name, 8);
452 name[8] = 0;
453 memcpy(ext, x_spfile->ext, 3);
454 ext[3] = 0;
455
456 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "name: %s\n", name);
457 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "ext: %s\n", ext);
458 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "attribute: %x\n",
459 x_spfile->attr);
460 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "time: %d:%d:%d\n",
461 x_spfile->time >> 11,
462 (x_spfile->time >> 5) & 0x3f,
463 (x_spfile->time & 0x1f) * 2);
464 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "date: %d-%d-%d\n",
465 (x_spfile->date >> 9) + 1980,
466 (x_spfile->date >> 5) & 0xf,
467 x_spfile->date & 0x1f);
468 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "start cluster: %x\n",
469 x_spfile->cluster);
470 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "size: %x\n",
471 x_spfile->size);
472 return rc;
473}
474
475static ssize_t mspro_block_attr_show_devinfo(struct device *dev,
476 struct device_attribute *attr,
477 char *buffer)
478{
479 struct mspro_sys_attr *x_attr = container_of(attr,
480 struct mspro_sys_attr,
481 dev_attr);
482 struct mspro_devinfo *x_devinfo = x_attr->data;
483 ssize_t rc = 0;
484
485 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "cylinders: %x\n",
486 be16_to_cpu(x_devinfo->cylinders));
487 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "heads: %x\n",
488 be16_to_cpu(x_devinfo->heads));
489 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "bytes per track: %x\n",
490 be16_to_cpu(x_devinfo->bytes_per_track));
491 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "bytes per sector: %x\n",
492 be16_to_cpu(x_devinfo->bytes_per_sector));
493 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "sectors per track: %x\n",
494 be16_to_cpu(x_devinfo->sectors_per_track));
495 return rc;
496}
497
498static sysfs_show_t mspro_block_attr_show(unsigned char tag)
499{
500 switch (tag) {
501 case MSPRO_BLOCK_ID_SYSINFO:
502 return mspro_block_attr_show_sysinfo;
503 case MSPRO_BLOCK_ID_MODELNAME:
504 return mspro_block_attr_show_modelname;
505 case MSPRO_BLOCK_ID_MBR:
506 return mspro_block_attr_show_mbr;
507 case MSPRO_BLOCK_ID_SPECFILEVALUES1:
508 case MSPRO_BLOCK_ID_SPECFILEVALUES2:
509 return mspro_block_attr_show_specfile;
510 case MSPRO_BLOCK_ID_DEVINFO:
511 return mspro_block_attr_show_devinfo;
512 default:
513 return mspro_block_attr_show_default;
514 }
515}
516
517/*** Protocol handlers ***/
518
519/*
520 * Functions prefixed with "h_" are protocol callbacks. They can be called from
521 * interrupt context. Return value of 0 means that request processing is still
522 * ongoing, while special error value of -EAGAIN means that current request is
523 * finished (and request processor should come back some time later).
524 */
525
526static int h_mspro_block_req_init(struct memstick_dev *card,
527 struct memstick_request **mrq)
528{
529 struct mspro_block_data *msb = memstick_get_drvdata(card);
530
531 *mrq = &card->current_mrq;
532 card->next_request = msb->mrq_handler;
533 return 0;
534}
535
536static int h_mspro_block_default(struct memstick_dev *card,
537 struct memstick_request **mrq)
538{
539 return mspro_block_complete_req(card, (*mrq)->error);
540}
541
542static int h_mspro_block_default_bad(struct memstick_dev *card,
543 struct memstick_request **mrq)
544{
545 return -ENXIO;
546}
547
548static int h_mspro_block_get_ro(struct memstick_dev *card,
549 struct memstick_request **mrq)
550{
551 struct mspro_block_data *msb = memstick_get_drvdata(card);
552
553 if (!(*mrq)->error) {
554 if ((*mrq)->data[offsetof(struct ms_status_register, status0)]
555 & MEMSTICK_STATUS0_WP)
556 msb->read_only = 1;
557 else
558 msb->read_only = 0;
559 }
560
561 return mspro_block_complete_req(card, (*mrq)->error);
562}
563
564static int h_mspro_block_wait_for_ced(struct memstick_dev *card,
565 struct memstick_request **mrq)
566{
567 dev_dbg(&card->dev, "wait for ced: value %x\n", (*mrq)->data[0]);
568
569 if (!(*mrq)->error) {
570 if ((*mrq)->data[0] & (MEMSTICK_INT_CMDNAK | MEMSTICK_INT_ERR))
571 (*mrq)->error = -EFAULT;
572 else if (!((*mrq)->data[0] & MEMSTICK_INT_CED))
573 return 0;
574 }
575
576 return mspro_block_complete_req(card, (*mrq)->error);
577}
578
579static int h_mspro_block_transfer_data(struct memstick_dev *card,
580 struct memstick_request **mrq)
581{
582 struct mspro_block_data *msb = memstick_get_drvdata(card);
583 unsigned char t_val = 0;
584 struct scatterlist t_sg = { 0 };
585 size_t t_offset;
586
587 if ((*mrq)->error)
588 return mspro_block_complete_req(card, (*mrq)->error);
589
590 switch ((*mrq)->tpc) {
591 case MS_TPC_WRITE_REG:
592 memstick_init_req(*mrq, MS_TPC_SET_CMD, &msb->transfer_cmd, 1);
593 (*mrq)->need_card_int = 1;
594 return 0;
595 case MS_TPC_SET_CMD:
596 t_val = (*mrq)->int_reg;
597 memstick_init_req(*mrq, MS_TPC_GET_INT, NULL, 1);
598 if (msb->caps & MEMSTICK_CAP_AUTO_GET_INT)
599 goto has_int_reg;
600 return 0;
601 case MS_TPC_GET_INT:
602 t_val = (*mrq)->data[0];
603has_int_reg:
604 if (t_val & (MEMSTICK_INT_CMDNAK | MEMSTICK_INT_ERR)) {
605 t_val = MSPRO_CMD_STOP;
606 memstick_init_req(*mrq, MS_TPC_SET_CMD, &t_val, 1);
607 card->next_request = h_mspro_block_default;
608 return 0;
609 }
610
611 if (msb->current_page
612 == (msb->req_sg[msb->current_seg].length
613 / msb->page_size)) {
614 msb->current_page = 0;
615 msb->current_seg++;
616
617 if (msb->current_seg == msb->seg_count) {
618 if (t_val & MEMSTICK_INT_CED) {
619 return mspro_block_complete_req(card,
620 0);
621 } else {
622 card->next_request
623 = h_mspro_block_wait_for_ced;
624 memstick_init_req(*mrq, MS_TPC_GET_INT,
625 NULL, 1);
626 return 0;
627 }
628 }
629 }
630
631 if (!(t_val & MEMSTICK_INT_BREQ)) {
632 memstick_init_req(*mrq, MS_TPC_GET_INT, NULL, 1);
633 return 0;
634 }
635
636 t_offset = msb->req_sg[msb->current_seg].offset;
637 t_offset += msb->current_page * msb->page_size;
638
639 sg_set_page(&t_sg,
640 nth_page(sg_page(&(msb->req_sg[msb->current_seg])),
641 t_offset >> PAGE_SHIFT),
642 msb->page_size, offset_in_page(t_offset));
643
644 memstick_init_req_sg(*mrq, msb->data_dir == READ
645 ? MS_TPC_READ_LONG_DATA
646 : MS_TPC_WRITE_LONG_DATA,
647 &t_sg);
648 (*mrq)->need_card_int = 1;
649 return 0;
650 case MS_TPC_READ_LONG_DATA:
651 case MS_TPC_WRITE_LONG_DATA:
652 msb->current_page++;
653 if (msb->caps & MEMSTICK_CAP_AUTO_GET_INT) {
654 t_val = (*mrq)->int_reg;
655 goto has_int_reg;
656 } else {
657 memstick_init_req(*mrq, MS_TPC_GET_INT, NULL, 1);
658 return 0;
659 }
660
661 default:
662 BUG();
663 }
664}
665
666/*** Transfer setup functions for different access methods. ***/
667
668/** Setup data transfer request for SET_CMD TPC with arguments in card
669 * registers.
670 *
671 * @card Current media instance
672 * @offset Target data offset in bytes
673 * @length Required transfer length in bytes.
674 */
675static void h_mspro_block_setup_cmd(struct memstick_dev *card, u64 offset,
676 size_t length)
677{
678 struct mspro_block_data *msb = memstick_get_drvdata(card);
679 struct mspro_param_register param = {
680 .system = msb->system,
681 .data_count = cpu_to_be16((uint16_t)(length / msb->page_size)),
682 /* ISO C90 warning precludes direct initialization for now. */
683 .data_address = 0,
684 .tpc_param = 0
685 };
686
687 do_div(offset, msb->page_size);
688 param.data_address = cpu_to_be32((uint32_t)offset);
689
690 card->next_request = h_mspro_block_req_init;
691 msb->mrq_handler = h_mspro_block_transfer_data;
692 memstick_init_req(&card->current_mrq, MS_TPC_WRITE_REG,
693 ¶m, sizeof(param));
694}
695
696/*** Data transfer ***/
697
698static int mspro_block_issue_req(struct memstick_dev *card, int chunk)
699{
700 struct mspro_block_data *msb = memstick_get_drvdata(card);
701 u64 t_off;
702 unsigned int count;
703
704try_again:
705 while (chunk) {
706 msb->current_page = 0;
707 msb->current_seg = 0;
708 msb->seg_count = blk_rq_map_sg(msb->block_req->q,
709 msb->block_req,
710 msb->req_sg);
711
712 if (!msb->seg_count) {
713 chunk = __blk_end_request_cur(msb->block_req, -ENOMEM);
714 continue;
715 }
716
717 t_off = blk_rq_pos(msb->block_req);
718 t_off <<= 9;
719 count = blk_rq_bytes(msb->block_req);
720
721 msb->setup_transfer(card, t_off, count);
722
723 msb->data_dir = rq_data_dir(msb->block_req);
724 msb->transfer_cmd = msb->data_dir == READ
725 ? MSPRO_CMD_READ_DATA
726 : MSPRO_CMD_WRITE_DATA;
727
728 memstick_new_req(card->host);
729 return 0;
730 }
731
732 dev_dbg(&card->dev, "blk_fetch\n");
733 msb->block_req = blk_fetch_request(msb->queue);
734 if (!msb->block_req) {
735 dev_dbg(&card->dev, "issue end\n");
736 return -EAGAIN;
737 }
738
739 dev_dbg(&card->dev, "trying again\n");
740 chunk = 1;
741 goto try_again;
742}
743
744static int mspro_block_complete_req(struct memstick_dev *card, int error)
745{
746 struct mspro_block_data *msb = memstick_get_drvdata(card);
747 int chunk, cnt;
748 unsigned int t_len = 0;
749 unsigned long flags;
750
751 spin_lock_irqsave(&msb->q_lock, flags);
752 dev_dbg(&card->dev, "complete %d, %d\n", msb->has_request ? 1 : 0,
753 error);
754
755 if (msb->has_request) {
756 /* Nothing to do - not really an error */
757 if (error == -EAGAIN)
758 error = 0;
759
760 if (error || (card->current_mrq.tpc == MSPRO_CMD_STOP)) {
761 if (msb->data_dir == READ) {
762 for (cnt = 0; cnt < msb->current_seg; cnt++)
763 t_len += msb->req_sg[cnt].length
764 / msb->page_size;
765
766 if (msb->current_page)
767 t_len += msb->current_page - 1;
768
769 t_len *= msb->page_size;
770 }
771 } else
772 t_len = blk_rq_bytes(msb->block_req);
773
774 dev_dbg(&card->dev, "transferred %x (%d)\n", t_len, error);
775
776 if (error && !t_len)
777 t_len = blk_rq_cur_bytes(msb->block_req);
778
779 chunk = __blk_end_request(msb->block_req, error, t_len);
780
781 error = mspro_block_issue_req(card, chunk);
782
783 if (!error)
784 goto out;
785 else
786 msb->has_request = 0;
787 } else {
788 if (!error)
789 error = -EAGAIN;
790 }
791
792 card->next_request = h_mspro_block_default_bad;
793 complete_all(&card->mrq_complete);
794out:
795 spin_unlock_irqrestore(&msb->q_lock, flags);
796 return error;
797}
798
799static void mspro_block_stop(struct memstick_dev *card)
800{
801 struct mspro_block_data *msb = memstick_get_drvdata(card);
802 int rc = 0;
803 unsigned long flags;
804
805 while (1) {
806 spin_lock_irqsave(&msb->q_lock, flags);
807 if (!msb->has_request) {
808 blk_stop_queue(msb->queue);
809 rc = 1;
810 }
811 spin_unlock_irqrestore(&msb->q_lock, flags);
812
813 if (rc)
814 break;
815
816 wait_for_completion(&card->mrq_complete);
817 }
818}
819
820static void mspro_block_start(struct memstick_dev *card)
821{
822 struct mspro_block_data *msb = memstick_get_drvdata(card);
823 unsigned long flags;
824
825 spin_lock_irqsave(&msb->q_lock, flags);
826 blk_start_queue(msb->queue);
827 spin_unlock_irqrestore(&msb->q_lock, flags);
828}
829
830static int mspro_block_prepare_req(struct request_queue *q, struct request *req)
831{
832 if (req->cmd_type != REQ_TYPE_FS &&
833 req->cmd_type != REQ_TYPE_BLOCK_PC) {
834 blk_dump_rq_flags(req, "MSPro unsupported request");
835 return BLKPREP_KILL;
836 }
837
838 req->cmd_flags |= REQ_DONTPREP;
839
840 return BLKPREP_OK;
841}
842
843static void mspro_block_submit_req(struct request_queue *q)
844{
845 struct memstick_dev *card = q->queuedata;
846 struct mspro_block_data *msb = memstick_get_drvdata(card);
847 struct request *req = NULL;
848
849 if (msb->has_request)
850 return;
851
852 if (msb->eject) {
853 while ((req = blk_fetch_request(q)) != NULL)
854 __blk_end_request_all(req, -ENODEV);
855
856 return;
857 }
858
859 msb->has_request = 1;
860 if (mspro_block_issue_req(card, 0))
861 msb->has_request = 0;
862}
863
864/*** Initialization ***/
865
866static int mspro_block_wait_for_ced(struct memstick_dev *card)
867{
868 struct mspro_block_data *msb = memstick_get_drvdata(card);
869
870 card->next_request = h_mspro_block_req_init;
871 msb->mrq_handler = h_mspro_block_wait_for_ced;
872 memstick_init_req(&card->current_mrq, MS_TPC_GET_INT, NULL, 1);
873 memstick_new_req(card->host);
874 wait_for_completion(&card->mrq_complete);
875 return card->current_mrq.error;
876}
877
878static int mspro_block_set_interface(struct memstick_dev *card,
879 unsigned char sys_reg)
880{
881 struct memstick_host *host = card->host;
882 struct mspro_block_data *msb = memstick_get_drvdata(card);
883 struct mspro_param_register param = {
884 .system = sys_reg,
885 .data_count = 0,
886 .data_address = 0,
887 .tpc_param = 0
888 };
889
890 card->next_request = h_mspro_block_req_init;
891 msb->mrq_handler = h_mspro_block_default;
892 memstick_init_req(&card->current_mrq, MS_TPC_WRITE_REG, ¶m,
893 sizeof(param));
894 memstick_new_req(host);
895 wait_for_completion(&card->mrq_complete);
896 return card->current_mrq.error;
897}
898
899static int mspro_block_switch_interface(struct memstick_dev *card)
900{
901 struct memstick_host *host = card->host;
902 struct mspro_block_data *msb = memstick_get_drvdata(card);
903 int rc = 0;
904
905try_again:
906 if (msb->caps & MEMSTICK_CAP_PAR4)
907 rc = mspro_block_set_interface(card, MEMSTICK_SYS_PAR4);
908 else
909 return 0;
910
911 if (rc) {
912 printk(KERN_WARNING
913 "%s: could not switch to 4-bit mode, error %d\n",
914 dev_name(&card->dev), rc);
915 return 0;
916 }
917
918 msb->system = MEMSTICK_SYS_PAR4;
919 host->set_param(host, MEMSTICK_INTERFACE, MEMSTICK_PAR4);
920 printk(KERN_INFO "%s: switching to 4-bit parallel mode\n",
921 dev_name(&card->dev));
922
923 if (msb->caps & MEMSTICK_CAP_PAR8) {
924 rc = mspro_block_set_interface(card, MEMSTICK_SYS_PAR8);
925
926 if (!rc) {
927 msb->system = MEMSTICK_SYS_PAR8;
928 host->set_param(host, MEMSTICK_INTERFACE,
929 MEMSTICK_PAR8);
930 printk(KERN_INFO
931 "%s: switching to 8-bit parallel mode\n",
932 dev_name(&card->dev));
933 } else
934 printk(KERN_WARNING
935 "%s: could not switch to 8-bit mode, error %d\n",
936 dev_name(&card->dev), rc);
937 }
938
939 card->next_request = h_mspro_block_req_init;
940 msb->mrq_handler = h_mspro_block_default;
941 memstick_init_req(&card->current_mrq, MS_TPC_GET_INT, NULL, 1);
942 memstick_new_req(card->host);
943 wait_for_completion(&card->mrq_complete);
944 rc = card->current_mrq.error;
945
946 if (rc) {
947 printk(KERN_WARNING
948 "%s: interface error, trying to fall back to serial\n",
949 dev_name(&card->dev));
950 msb->system = MEMSTICK_SYS_SERIAL;
951 host->set_param(host, MEMSTICK_POWER, MEMSTICK_POWER_OFF);
952 msleep(10);
953 host->set_param(host, MEMSTICK_POWER, MEMSTICK_POWER_ON);
954 host->set_param(host, MEMSTICK_INTERFACE, MEMSTICK_SERIAL);
955
956 rc = memstick_set_rw_addr(card);
957 if (!rc)
958 rc = mspro_block_set_interface(card, msb->system);
959
960 if (!rc) {
961 msleep(150);
962 rc = mspro_block_wait_for_ced(card);
963 if (rc)
964 return rc;
965
966 if (msb->caps & MEMSTICK_CAP_PAR8) {
967 msb->caps &= ~MEMSTICK_CAP_PAR8;
968 goto try_again;
969 }
970 }
971 }
972 return rc;
973}
974
975/* Memory allocated for attributes by this function should be freed by
976 * mspro_block_data_clear, no matter if the initialization process succeeded
977 * or failed.
978 */
979static int mspro_block_read_attributes(struct memstick_dev *card)
980{
981 struct mspro_block_data *msb = memstick_get_drvdata(card);
982 struct mspro_attribute *attr = NULL;
983 struct mspro_sys_attr *s_attr = NULL;
984 unsigned char *buffer = NULL;
985 int cnt, rc, attr_count;
986 /* While normally physical device offsets, represented here by
987 * attr_offset and attr_len will be of large numeric types, we can be
988 * sure, that attributes are close enough to the beginning of the
989 * device, to save ourselves some trouble.
990 */
991 unsigned int addr, attr_offset = 0, attr_len = msb->page_size;
992
993 attr = kmalloc(msb->page_size, GFP_KERNEL);
994 if (!attr)
995 return -ENOMEM;
996
997 sg_init_one(&msb->req_sg[0], attr, msb->page_size);
998 msb->seg_count = 1;
999 msb->current_seg = 0;
1000 msb->current_page = 0;
1001 msb->data_dir = READ;
1002 msb->transfer_cmd = MSPRO_CMD_READ_ATRB;
1003
1004 msb->setup_transfer(card, attr_offset, attr_len);
1005
1006 memstick_new_req(card->host);
1007 wait_for_completion(&card->mrq_complete);
1008 if (card->current_mrq.error) {
1009 rc = card->current_mrq.error;
1010 goto out_free_attr;
1011 }
1012
1013 if (be16_to_cpu(attr->signature) != MSPRO_BLOCK_SIGNATURE) {
1014 printk(KERN_ERR "%s: unrecognized device signature %x\n",
1015 dev_name(&card->dev), be16_to_cpu(attr->signature));
1016 rc = -ENODEV;
1017 goto out_free_attr;
1018 }
1019
1020 if (attr->count > MSPRO_BLOCK_MAX_ATTRIBUTES) {
1021 printk(KERN_WARNING "%s: way too many attribute entries\n",
1022 dev_name(&card->dev));
1023 attr_count = MSPRO_BLOCK_MAX_ATTRIBUTES;
1024 } else
1025 attr_count = attr->count;
1026
1027 msb->attr_group.attrs = kzalloc((attr_count + 1)
1028 * sizeof(struct attribute),
1029 GFP_KERNEL);
1030 if (!msb->attr_group.attrs) {
1031 rc = -ENOMEM;
1032 goto out_free_attr;
1033 }
1034 msb->attr_group.name = "media_attributes";
1035
1036 buffer = kmalloc(attr_len, GFP_KERNEL);
1037 if (!buffer) {
1038 rc = -ENOMEM;
1039 goto out_free_attr;
1040 }
1041 memcpy(buffer, (char *)attr, attr_len);
1042
1043 for (cnt = 0; cnt < attr_count; ++cnt) {
1044 s_attr = kzalloc(sizeof(struct mspro_sys_attr), GFP_KERNEL);
1045 if (!s_attr) {
1046 rc = -ENOMEM;
1047 goto out_free_buffer;
1048 }
1049
1050 msb->attr_group.attrs[cnt] = &s_attr->dev_attr.attr;
1051 addr = be32_to_cpu(attr->entries[cnt].address);
1052 s_attr->size = be32_to_cpu(attr->entries[cnt].size);
1053 dev_dbg(&card->dev, "adding attribute %d: id %x, address %x, "
1054 "size %zx\n", cnt, attr->entries[cnt].id, addr,
1055 s_attr->size);
1056 s_attr->id = attr->entries[cnt].id;
1057 if (mspro_block_attr_name(s_attr->id))
1058 snprintf(s_attr->name, sizeof(s_attr->name), "%s",
1059 mspro_block_attr_name(attr->entries[cnt].id));
1060 else
1061 snprintf(s_attr->name, sizeof(s_attr->name),
1062 "attr_x%02x", attr->entries[cnt].id);
1063
1064 sysfs_attr_init(&s_attr->dev_attr.attr);
1065 s_attr->dev_attr.attr.name = s_attr->name;
1066 s_attr->dev_attr.attr.mode = S_IRUGO;
1067 s_attr->dev_attr.show = mspro_block_attr_show(s_attr->id);
1068
1069 if (!s_attr->size)
1070 continue;
1071
1072 s_attr->data = kmalloc(s_attr->size, GFP_KERNEL);
1073 if (!s_attr->data) {
1074 rc = -ENOMEM;
1075 goto out_free_buffer;
1076 }
1077
1078 if (((addr / msb->page_size) == (attr_offset / msb->page_size))
1079 && (((addr + s_attr->size - 1) / msb->page_size)
1080 == (attr_offset / msb->page_size))) {
1081 memcpy(s_attr->data, buffer + addr % msb->page_size,
1082 s_attr->size);
1083 continue;
1084 }
1085
1086 attr_offset = (addr / msb->page_size) * msb->page_size;
1087
1088 if ((attr_offset + attr_len) < (addr + s_attr->size)) {
1089 kfree(buffer);
1090 attr_len = (((addr + s_attr->size) / msb->page_size)
1091 + 1 ) * msb->page_size - attr_offset;
1092 buffer = kmalloc(attr_len, GFP_KERNEL);
1093 if (!buffer) {
1094 rc = -ENOMEM;
1095 goto out_free_attr;
1096 }
1097 }
1098
1099 sg_init_one(&msb->req_sg[0], buffer, attr_len);
1100 msb->seg_count = 1;
1101 msb->current_seg = 0;
1102 msb->current_page = 0;
1103 msb->data_dir = READ;
1104 msb->transfer_cmd = MSPRO_CMD_READ_ATRB;
1105
1106 dev_dbg(&card->dev, "reading attribute range %x, %x\n",
1107 attr_offset, attr_len);
1108
1109 msb->setup_transfer(card, attr_offset, attr_len);
1110 memstick_new_req(card->host);
1111 wait_for_completion(&card->mrq_complete);
1112 if (card->current_mrq.error) {
1113 rc = card->current_mrq.error;
1114 goto out_free_buffer;
1115 }
1116
1117 memcpy(s_attr->data, buffer + addr % msb->page_size,
1118 s_attr->size);
1119 }
1120
1121 rc = 0;
1122out_free_buffer:
1123 kfree(buffer);
1124out_free_attr:
1125 kfree(attr);
1126 return rc;
1127}
1128
1129static int mspro_block_init_card(struct memstick_dev *card)
1130{
1131 struct mspro_block_data *msb = memstick_get_drvdata(card);
1132 struct memstick_host *host = card->host;
1133 int rc = 0;
1134
1135 msb->system = MEMSTICK_SYS_SERIAL;
1136 msb->setup_transfer = h_mspro_block_setup_cmd;
1137
1138 card->reg_addr.r_offset = offsetof(struct mspro_register, status);
1139 card->reg_addr.r_length = sizeof(struct ms_status_register);
1140 card->reg_addr.w_offset = offsetof(struct mspro_register, param);
1141 card->reg_addr.w_length = sizeof(struct mspro_param_register);
1142
1143 if (memstick_set_rw_addr(card))
1144 return -EIO;
1145
1146 msb->caps = host->caps;
1147
1148 msleep(150);
1149 rc = mspro_block_wait_for_ced(card);
1150 if (rc)
1151 return rc;
1152
1153 rc = mspro_block_switch_interface(card);
1154 if (rc)
1155 return rc;
1156
1157 dev_dbg(&card->dev, "card activated\n");
1158 if (msb->system != MEMSTICK_SYS_SERIAL)
1159 msb->caps |= MEMSTICK_CAP_AUTO_GET_INT;
1160
1161 card->next_request = h_mspro_block_req_init;
1162 msb->mrq_handler = h_mspro_block_get_ro;
1163 memstick_init_req(&card->current_mrq, MS_TPC_READ_REG, NULL,
1164 sizeof(struct ms_status_register));
1165 memstick_new_req(card->host);
1166 wait_for_completion(&card->mrq_complete);
1167 if (card->current_mrq.error)
1168 return card->current_mrq.error;
1169
1170 dev_dbg(&card->dev, "card r/w status %d\n", msb->read_only ? 0 : 1);
1171
1172 msb->page_size = 512;
1173 rc = mspro_block_read_attributes(card);
1174 if (rc)
1175 return rc;
1176
1177 dev_dbg(&card->dev, "attributes loaded\n");
1178 return 0;
1179
1180}
1181
1182static int mspro_block_init_disk(struct memstick_dev *card)
1183{
1184 struct mspro_block_data *msb = memstick_get_drvdata(card);
1185 struct memstick_host *host = card->host;
1186 struct mspro_devinfo *dev_info = NULL;
1187 struct mspro_sys_info *sys_info = NULL;
1188 struct mspro_sys_attr *s_attr = NULL;
1189 int rc, disk_id;
1190 u64 limit = BLK_BOUNCE_HIGH;
1191 unsigned long capacity;
1192
1193 if (host->dev.dma_mask && *(host->dev.dma_mask))
1194 limit = *(host->dev.dma_mask);
1195
1196 for (rc = 0; msb->attr_group.attrs[rc]; ++rc) {
1197 s_attr = mspro_from_sysfs_attr(msb->attr_group.attrs[rc]);
1198
1199 if (s_attr->id == MSPRO_BLOCK_ID_DEVINFO)
1200 dev_info = s_attr->data;
1201 else if (s_attr->id == MSPRO_BLOCK_ID_SYSINFO)
1202 sys_info = s_attr->data;
1203 }
1204
1205 if (!dev_info || !sys_info)
1206 return -ENODEV;
1207
1208 msb->cylinders = be16_to_cpu(dev_info->cylinders);
1209 msb->heads = be16_to_cpu(dev_info->heads);
1210 msb->sectors_per_track = be16_to_cpu(dev_info->sectors_per_track);
1211
1212 msb->page_size = be16_to_cpu(sys_info->unit_size);
1213
1214 mutex_lock(&mspro_block_disk_lock);
1215 if (!idr_pre_get(&mspro_block_disk_idr, GFP_KERNEL)) {
1216 mutex_unlock(&mspro_block_disk_lock);
1217 return -ENOMEM;
1218 }
1219
1220 rc = idr_get_new(&mspro_block_disk_idr, card, &disk_id);
1221 mutex_unlock(&mspro_block_disk_lock);
1222
1223 if (rc)
1224 return rc;
1225
1226 if ((disk_id << MSPRO_BLOCK_PART_SHIFT) > 255) {
1227 rc = -ENOSPC;
1228 goto out_release_id;
1229 }
1230
1231 msb->disk = alloc_disk(1 << MSPRO_BLOCK_PART_SHIFT);
1232 if (!msb->disk) {
1233 rc = -ENOMEM;
1234 goto out_release_id;
1235 }
1236
1237 msb->queue = blk_init_queue(mspro_block_submit_req, &msb->q_lock);
1238 if (!msb->queue) {
1239 rc = -ENOMEM;
1240 goto out_put_disk;
1241 }
1242
1243 msb->queue->queuedata = card;
1244 blk_queue_prep_rq(msb->queue, mspro_block_prepare_req);
1245
1246 blk_queue_bounce_limit(msb->queue, limit);
1247 blk_queue_max_hw_sectors(msb->queue, MSPRO_BLOCK_MAX_PAGES);
1248 blk_queue_max_segments(msb->queue, MSPRO_BLOCK_MAX_SEGS);
1249 blk_queue_max_segment_size(msb->queue,
1250 MSPRO_BLOCK_MAX_PAGES * msb->page_size);
1251
1252 msb->disk->major = major;
1253 msb->disk->first_minor = disk_id << MSPRO_BLOCK_PART_SHIFT;
1254 msb->disk->fops = &ms_block_bdops;
1255 msb->usage_count = 1;
1256 msb->disk->private_data = msb;
1257 msb->disk->queue = msb->queue;
1258 msb->disk->driverfs_dev = &card->dev;
1259
1260 sprintf(msb->disk->disk_name, "mspblk%d", disk_id);
1261
1262 blk_queue_logical_block_size(msb->queue, msb->page_size);
1263
1264 capacity = be16_to_cpu(sys_info->user_block_count);
1265 capacity *= be16_to_cpu(sys_info->block_size);
1266 capacity *= msb->page_size >> 9;
1267 set_capacity(msb->disk, capacity);
1268 dev_dbg(&card->dev, "capacity set %ld\n", capacity);
1269
1270 add_disk(msb->disk);
1271 msb->active = 1;
1272 return 0;
1273
1274out_put_disk:
1275 put_disk(msb->disk);
1276out_release_id:
1277 mutex_lock(&mspro_block_disk_lock);
1278 idr_remove(&mspro_block_disk_idr, disk_id);
1279 mutex_unlock(&mspro_block_disk_lock);
1280 return rc;
1281}
1282
1283static void mspro_block_data_clear(struct mspro_block_data *msb)
1284{
1285 int cnt;
1286 struct mspro_sys_attr *s_attr;
1287
1288 if (msb->attr_group.attrs) {
1289 for (cnt = 0; msb->attr_group.attrs[cnt]; ++cnt) {
1290 s_attr = mspro_from_sysfs_attr(msb->attr_group
1291 .attrs[cnt]);
1292 kfree(s_attr->data);
1293 kfree(s_attr);
1294 }
1295 kfree(msb->attr_group.attrs);
1296 }
1297
1298 msb->card = NULL;
1299}
1300
1301static int mspro_block_check_card(struct memstick_dev *card)
1302{
1303 struct mspro_block_data *msb = memstick_get_drvdata(card);
1304
1305 return (msb->active == 1);
1306}
1307
1308static int mspro_block_probe(struct memstick_dev *card)
1309{
1310 struct mspro_block_data *msb;
1311 int rc = 0;
1312
1313 msb = kzalloc(sizeof(struct mspro_block_data), GFP_KERNEL);
1314 if (!msb)
1315 return -ENOMEM;
1316 memstick_set_drvdata(card, msb);
1317 msb->card = card;
1318 spin_lock_init(&msb->q_lock);
1319
1320 rc = mspro_block_init_card(card);
1321
1322 if (rc)
1323 goto out_free;
1324
1325 rc = sysfs_create_group(&card->dev.kobj, &msb->attr_group);
1326 if (rc)
1327 goto out_free;
1328
1329 rc = mspro_block_init_disk(card);
1330 if (!rc) {
1331 card->check = mspro_block_check_card;
1332 card->stop = mspro_block_stop;
1333 card->start = mspro_block_start;
1334 return 0;
1335 }
1336
1337 sysfs_remove_group(&card->dev.kobj, &msb->attr_group);
1338out_free:
1339 memstick_set_drvdata(card, NULL);
1340 mspro_block_data_clear(msb);
1341 kfree(msb);
1342 return rc;
1343}
1344
1345static void mspro_block_remove(struct memstick_dev *card)
1346{
1347 struct mspro_block_data *msb = memstick_get_drvdata(card);
1348 unsigned long flags;
1349
1350 spin_lock_irqsave(&msb->q_lock, flags);
1351 msb->eject = 1;
1352 blk_start_queue(msb->queue);
1353 spin_unlock_irqrestore(&msb->q_lock, flags);
1354
1355 del_gendisk(msb->disk);
1356 dev_dbg(&card->dev, "mspro block remove\n");
1357
1358 blk_cleanup_queue(msb->queue);
1359 msb->queue = NULL;
1360
1361 sysfs_remove_group(&card->dev.kobj, &msb->attr_group);
1362
1363 mutex_lock(&mspro_block_disk_lock);
1364 mspro_block_data_clear(msb);
1365 mutex_unlock(&mspro_block_disk_lock);
1366
1367 mspro_block_disk_release(msb->disk);
1368 memstick_set_drvdata(card, NULL);
1369}
1370
1371#ifdef CONFIG_PM
1372
1373static int mspro_block_suspend(struct memstick_dev *card, pm_message_t state)
1374{
1375 struct mspro_block_data *msb = memstick_get_drvdata(card);
1376 unsigned long flags;
1377
1378 spin_lock_irqsave(&msb->q_lock, flags);
1379 blk_stop_queue(msb->queue);
1380 msb->active = 0;
1381 spin_unlock_irqrestore(&msb->q_lock, flags);
1382
1383 return 0;
1384}
1385
1386static int mspro_block_resume(struct memstick_dev *card)
1387{
1388 struct mspro_block_data *msb = memstick_get_drvdata(card);
1389 unsigned long flags;
1390 int rc = 0;
1391
1392#ifdef CONFIG_MEMSTICK_UNSAFE_RESUME
1393
1394 struct mspro_block_data *new_msb;
1395 struct memstick_host *host = card->host;
1396 struct mspro_sys_attr *s_attr, *r_attr;
1397 unsigned char cnt;
1398
1399 mutex_lock(&host->lock);
1400 new_msb = kzalloc(sizeof(struct mspro_block_data), GFP_KERNEL);
1401 if (!new_msb) {
1402 rc = -ENOMEM;
1403 goto out_unlock;
1404 }
1405
1406 new_msb->card = card;
1407 memstick_set_drvdata(card, new_msb);
1408 if (mspro_block_init_card(card))
1409 goto out_free;
1410
1411 for (cnt = 0; new_msb->attr_group.attrs[cnt]
1412 && msb->attr_group.attrs[cnt]; ++cnt) {
1413 s_attr = mspro_from_sysfs_attr(new_msb->attr_group.attrs[cnt]);
1414 r_attr = mspro_from_sysfs_attr(msb->attr_group.attrs[cnt]);
1415
1416 if (s_attr->id == MSPRO_BLOCK_ID_SYSINFO
1417 && r_attr->id == s_attr->id) {
1418 if (memcmp(s_attr->data, r_attr->data, s_attr->size))
1419 break;
1420
1421 msb->active = 1;
1422 break;
1423 }
1424 }
1425
1426out_free:
1427 memstick_set_drvdata(card, msb);
1428 mspro_block_data_clear(new_msb);
1429 kfree(new_msb);
1430out_unlock:
1431 mutex_unlock(&host->lock);
1432
1433#endif /* CONFIG_MEMSTICK_UNSAFE_RESUME */
1434
1435 spin_lock_irqsave(&msb->q_lock, flags);
1436 blk_start_queue(msb->queue);
1437 spin_unlock_irqrestore(&msb->q_lock, flags);
1438 return rc;
1439}
1440
1441#else
1442
1443#define mspro_block_suspend NULL
1444#define mspro_block_resume NULL
1445
1446#endif /* CONFIG_PM */
1447
1448static struct memstick_device_id mspro_block_id_tbl[] = {
1449 {MEMSTICK_MATCH_ALL, MEMSTICK_TYPE_PRO, MEMSTICK_CATEGORY_STORAGE_DUO,
1450 MEMSTICK_CLASS_DUO},
1451 {}
1452};
1453
1454
1455static struct memstick_driver mspro_block_driver = {
1456 .driver = {
1457 .name = DRIVER_NAME,
1458 .owner = THIS_MODULE
1459 },
1460 .id_table = mspro_block_id_tbl,
1461 .probe = mspro_block_probe,
1462 .remove = mspro_block_remove,
1463 .suspend = mspro_block_suspend,
1464 .resume = mspro_block_resume
1465};
1466
1467static int __init mspro_block_init(void)
1468{
1469 int rc = -ENOMEM;
1470
1471 rc = register_blkdev(major, DRIVER_NAME);
1472 if (rc < 0) {
1473 printk(KERN_ERR DRIVER_NAME ": failed to register "
1474 "major %d, error %d\n", major, rc);
1475 return rc;
1476 }
1477 if (!major)
1478 major = rc;
1479
1480 rc = memstick_register_driver(&mspro_block_driver);
1481 if (rc)
1482 unregister_blkdev(major, DRIVER_NAME);
1483 return rc;
1484}
1485
1486static void __exit mspro_block_exit(void)
1487{
1488 memstick_unregister_driver(&mspro_block_driver);
1489 unregister_blkdev(major, DRIVER_NAME);
1490 idr_destroy(&mspro_block_disk_idr);
1491}
1492
1493module_init(mspro_block_init);
1494module_exit(mspro_block_exit);
1495
1496MODULE_LICENSE("GPL");
1497MODULE_AUTHOR("Alex Dubov");
1498MODULE_DESCRIPTION("Sony MemoryStickPro block device driver");
1499MODULE_DEVICE_TABLE(memstick, mspro_block_id_tbl);