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