e5356f97d0764e2f96d352c2faf4218648858f36
[linux-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         unsigned int          caps;
137         struct gendisk        *disk;
138         struct request_queue  *queue;
139         spinlock_t            q_lock;
140         wait_queue_head_t     q_wait;
141         struct task_struct    *q_thread;
142
143         unsigned short        page_size;
144         unsigned short        cylinders;
145         unsigned short        heads;
146         unsigned short        sectors_per_track;
147
148         unsigned char         system;
149         unsigned char         read_only:1,
150                               active:1,
151                               has_request:1,
152                               data_dir:1;
153         unsigned char         transfer_cmd;
154
155         int                   (*mrq_handler)(struct memstick_dev *card,
156                                              struct memstick_request **mrq);
157
158         struct attribute_group attr_group;
159
160         struct scatterlist    req_sg[MSPRO_BLOCK_MAX_SEGS];
161         unsigned int          seg_count;
162         unsigned int          current_seg;
163         unsigned short        current_page;
164 };
165
166 static DEFINE_IDR(mspro_block_disk_idr);
167 static DEFINE_MUTEX(mspro_block_disk_lock);
168
169 /*** Block device ***/
170
171 static int mspro_block_bd_open(struct inode *inode, struct file *filp)
172 {
173         struct gendisk *disk = inode->i_bdev->bd_disk;
174         struct mspro_block_data *msb = disk->private_data;
175         int rc = -ENXIO;
176
177         mutex_lock(&mspro_block_disk_lock);
178
179         if (msb && msb->card) {
180                 msb->usage_count++;
181                 if ((filp->f_mode & FMODE_WRITE) && msb->read_only)
182                         rc = -EROFS;
183                 else
184                         rc = 0;
185         }
186
187         mutex_unlock(&mspro_block_disk_lock);
188
189         return rc;
190 }
191
192
193 static int mspro_block_disk_release(struct gendisk *disk)
194 {
195         struct mspro_block_data *msb = disk->private_data;
196         int disk_id = disk->first_minor >> MEMSTICK_PART_SHIFT;
197
198         mutex_lock(&mspro_block_disk_lock);
199
200         if (msb->usage_count) {
201                 msb->usage_count--;
202                 if (!msb->usage_count) {
203                         kfree(msb);
204                         disk->private_data = NULL;
205                         idr_remove(&mspro_block_disk_idr, disk_id);
206                         put_disk(disk);
207                 }
208         }
209
210         mutex_unlock(&mspro_block_disk_lock);
211
212         return 0;
213 }
214
215 static int mspro_block_bd_release(struct inode *inode, struct file *filp)
216 {
217         struct gendisk *disk = inode->i_bdev->bd_disk;
218         return mspro_block_disk_release(disk);
219 }
220
221 static int mspro_block_bd_getgeo(struct block_device *bdev,
222                                  struct hd_geometry *geo)
223 {
224         struct mspro_block_data *msb = bdev->bd_disk->private_data;
225
226         geo->heads = msb->heads;
227         geo->sectors = msb->sectors_per_track;
228         geo->cylinders = msb->cylinders;
229
230         return 0;
231 }
232
233 static struct block_device_operations ms_block_bdops = {
234         .open    = mspro_block_bd_open,
235         .release = mspro_block_bd_release,
236         .getgeo  = mspro_block_bd_getgeo,
237         .owner   = THIS_MODULE
238 };
239
240 /*** Information ***/
241
242 static struct mspro_sys_attr *mspro_from_sysfs_attr(struct attribute *attr)
243 {
244         struct device_attribute *dev_attr
245                 = container_of(attr, struct device_attribute, attr);
246         return container_of(dev_attr, struct mspro_sys_attr, dev_attr);
247 }
248
249 static const char *mspro_block_attr_name(unsigned char tag)
250 {
251         switch (tag) {
252         case MSPRO_BLOCK_ID_SYSINFO:
253                 return "attr_sysinfo";
254         case MSPRO_BLOCK_ID_MODELNAME:
255                 return "attr_modelname";
256         case MSPRO_BLOCK_ID_MBR:
257                 return "attr_mbr";
258         case MSPRO_BLOCK_ID_PBR16:
259                 return "attr_pbr16";
260         case MSPRO_BLOCK_ID_PBR32:
261                 return "attr_pbr32";
262         case MSPRO_BLOCK_ID_SPECFILEVALUES1:
263                 return "attr_specfilevalues1";
264         case MSPRO_BLOCK_ID_SPECFILEVALUES2:
265                 return "attr_specfilevalues2";
266         case MSPRO_BLOCK_ID_DEVINFO:
267                 return "attr_devinfo";
268         default:
269                 return NULL;
270         };
271 }
272
273 typedef ssize_t (*sysfs_show_t)(struct device *dev,
274                                 struct device_attribute *attr,
275                                 char *buffer);
276
277 static ssize_t mspro_block_attr_show_default(struct device *dev,
278                                              struct device_attribute *attr,
279                                              char *buffer)
280 {
281         struct mspro_sys_attr *s_attr = container_of(attr,
282                                                      struct mspro_sys_attr,
283                                                      dev_attr);
284
285         ssize_t cnt, rc = 0;
286
287         for (cnt = 0; cnt < s_attr->size; cnt++) {
288                 if (cnt && !(cnt % 16)) {
289                         if (PAGE_SIZE - rc)
290                                 buffer[rc++] = '\n';
291                 }
292
293                 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "%02x ",
294                                 ((unsigned char *)s_attr->data)[cnt]);
295         }
296         return rc;
297 }
298
299 static ssize_t mspro_block_attr_show_sysinfo(struct device *dev,
300                                              struct device_attribute *attr,
301                                              char *buffer)
302 {
303         struct mspro_sys_attr *x_attr = container_of(attr,
304                                                      struct mspro_sys_attr,
305                                                      dev_attr);
306         struct mspro_sys_info *x_sys = x_attr->data;
307         ssize_t rc = 0;
308         int date_tz = 0, date_tz_f = 0;
309
310         if (x_sys->assembly_date[0] > 0x80U) {
311                 date_tz = (~x_sys->assembly_date[0]) + 1;
312                 date_tz_f = date_tz & 3;
313                 date_tz >>= 2;
314                 date_tz = -date_tz;
315                 date_tz_f *= 15;
316         } else if (x_sys->assembly_date[0] < 0x80U) {
317                 date_tz = x_sys->assembly_date[0];
318                 date_tz_f = date_tz & 3;
319                 date_tz >>= 2;
320                 date_tz_f *= 15;
321         }
322
323         rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "class: %x\n",
324                         x_sys->class);
325         rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "block size: %x\n",
326                         be16_to_cpu(x_sys->block_size));
327         rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "block count: %x\n",
328                         be16_to_cpu(x_sys->block_count));
329         rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "user block count: %x\n",
330                         be16_to_cpu(x_sys->user_block_count));
331         rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "page size: %x\n",
332                         be16_to_cpu(x_sys->page_size));
333         rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "assembly date: "
334                         "GMT%+d:%d %04u-%02u-%02u %02u:%02u:%02u\n",
335                         date_tz, date_tz_f,
336                         be16_to_cpu(*(unsigned short *)
337                                     &x_sys->assembly_date[1]),
338                         x_sys->assembly_date[3], x_sys->assembly_date[4],
339                         x_sys->assembly_date[5], x_sys->assembly_date[6],
340                         x_sys->assembly_date[7]);
341         rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "serial number: %x\n",
342                         be32_to_cpu(x_sys->serial_number));
343         rc += scnprintf(buffer + rc, PAGE_SIZE - rc,
344                         "assembly maker code: %x\n",
345                         x_sys->assembly_maker_code);
346         rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "assembly model code: "
347                         "%02x%02x%02x\n", x_sys->assembly_model_code[0],
348                         x_sys->assembly_model_code[1],
349                         x_sys->assembly_model_code[2]);
350         rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "memory maker code: %x\n",
351                         be16_to_cpu(x_sys->memory_maker_code));
352         rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "memory model code: %x\n",
353                         be16_to_cpu(x_sys->memory_model_code));
354         rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "vcc: %x\n",
355                         x_sys->vcc);
356         rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "vpp: %x\n",
357                         x_sys->vpp);
358         rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "controller number: %x\n",
359                         be16_to_cpu(x_sys->controller_number));
360         rc += scnprintf(buffer + rc, PAGE_SIZE - rc,
361                         "controller function: %x\n",
362                         be16_to_cpu(x_sys->controller_function));
363         rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "start sector: %x\n",
364                         be16_to_cpu(x_sys->start_sector));
365         rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "unit size: %x\n",
366                         be16_to_cpu(x_sys->unit_size));
367         rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "sub class: %x\n",
368                         x_sys->ms_sub_class);
369         rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "interface type: %x\n",
370                         x_sys->interface_type);
371         rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "controller code: %x\n",
372                         be16_to_cpu(x_sys->controller_code));
373         rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "format type: %x\n",
374                         x_sys->format_type);
375         rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "device type: %x\n",
376                         x_sys->device_type);
377         rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "mspro id: %s\n",
378                         x_sys->mspro_id);
379         return rc;
380 }
381
382 static ssize_t mspro_block_attr_show_modelname(struct device *dev,
383                                                struct device_attribute *attr,
384                                                char *buffer)
385 {
386         struct mspro_sys_attr *s_attr = container_of(attr,
387                                                      struct mspro_sys_attr,
388                                                      dev_attr);
389
390         return scnprintf(buffer, PAGE_SIZE, "%s", (char *)s_attr->data);
391 }
392
393 static ssize_t mspro_block_attr_show_mbr(struct device *dev,
394                                          struct device_attribute *attr,
395                                          char *buffer)
396 {
397         struct mspro_sys_attr *x_attr = container_of(attr,
398                                                      struct mspro_sys_attr,
399                                                      dev_attr);
400         struct mspro_mbr *x_mbr = x_attr->data;
401         ssize_t rc = 0;
402
403         rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "boot partition: %x\n",
404                         x_mbr->boot_partition);
405         rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "start head: %x\n",
406                         x_mbr->start_head);
407         rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "start sector: %x\n",
408                         x_mbr->start_sector);
409         rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "start cylinder: %x\n",
410                         x_mbr->start_cylinder);
411         rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "partition type: %x\n",
412                         x_mbr->partition_type);
413         rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "end head: %x\n",
414                         x_mbr->end_head);
415         rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "end sector: %x\n",
416                         x_mbr->end_sector);
417         rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "end cylinder: %x\n",
418                         x_mbr->end_cylinder);
419         rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "start sectors: %x\n",
420                         x_mbr->start_sectors);
421         rc += scnprintf(buffer + rc, PAGE_SIZE - rc,
422                         "sectors per partition: %x\n",
423                         x_mbr->sectors_per_partition);
424         return rc;
425 }
426
427 static ssize_t mspro_block_attr_show_specfile(struct device *dev,
428                                               struct device_attribute *attr,
429                                               char *buffer)
430 {
431         struct mspro_sys_attr *x_attr = container_of(attr,
432                                                      struct mspro_sys_attr,
433                                                      dev_attr);
434         struct mspro_specfile *x_spfile = x_attr->data;
435         char name[9], ext[4];
436         ssize_t rc = 0;
437
438         memcpy(name, x_spfile->name, 8);
439         name[8] = 0;
440         memcpy(ext, x_spfile->ext, 3);
441         ext[3] = 0;
442
443         rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "name: %s\n", name);
444         rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "ext: %s\n", ext);
445         rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "attribute: %x\n",
446                         x_spfile->attr);
447         rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "time: %d:%d:%d\n",
448                         x_spfile->time >> 11,
449                         (x_spfile->time >> 5) & 0x3f,
450                         (x_spfile->time & 0x1f) * 2);
451         rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "date: %d-%d-%d\n",
452                         (x_spfile->date >> 9) + 1980,
453                         (x_spfile->date >> 5) & 0xf,
454                         x_spfile->date & 0x1f);
455         rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "start cluster: %x\n",
456                         x_spfile->cluster);
457         rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "size: %x\n",
458                         x_spfile->size);
459         return rc;
460 }
461
462 static ssize_t mspro_block_attr_show_devinfo(struct device *dev,
463                                              struct device_attribute *attr,
464                                              char *buffer)
465 {
466         struct mspro_sys_attr *x_attr = container_of(attr,
467                                                      struct mspro_sys_attr,
468                                                      dev_attr);
469         struct mspro_devinfo *x_devinfo = x_attr->data;
470         ssize_t rc = 0;
471
472         rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "cylinders: %x\n",
473                         be16_to_cpu(x_devinfo->cylinders));
474         rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "heads: %x\n",
475                         be16_to_cpu(x_devinfo->heads));
476         rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "bytes per track: %x\n",
477                         be16_to_cpu(x_devinfo->bytes_per_track));
478         rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "bytes per sector: %x\n",
479                         be16_to_cpu(x_devinfo->bytes_per_sector));
480         rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "sectors per track: %x\n",
481                         be16_to_cpu(x_devinfo->sectors_per_track));
482         return rc;
483 }
484
485 static sysfs_show_t mspro_block_attr_show(unsigned char tag)
486 {
487         switch (tag) {
488         case MSPRO_BLOCK_ID_SYSINFO:
489                 return mspro_block_attr_show_sysinfo;
490         case MSPRO_BLOCK_ID_MODELNAME:
491                 return mspro_block_attr_show_modelname;
492         case MSPRO_BLOCK_ID_MBR:
493                 return mspro_block_attr_show_mbr;
494         case MSPRO_BLOCK_ID_SPECFILEVALUES1:
495         case MSPRO_BLOCK_ID_SPECFILEVALUES2:
496                 return mspro_block_attr_show_specfile;
497         case MSPRO_BLOCK_ID_DEVINFO:
498                 return mspro_block_attr_show_devinfo;
499         default:
500                 return mspro_block_attr_show_default;
501         }
502 }
503
504 /*** Protocol handlers ***/
505
506 /*
507  * Functions prefixed with "h_" are protocol callbacks. They can be called from
508  * interrupt context. Return value of 0 means that request processing is still
509  * ongoing, while special error value of -EAGAIN means that current request is
510  * finished (and request processor should come back some time later).
511  */
512
513 static int h_mspro_block_req_init(struct memstick_dev *card,
514                                   struct memstick_request **mrq)
515 {
516         struct mspro_block_data *msb = memstick_get_drvdata(card);
517
518         *mrq = &card->current_mrq;
519         card->next_request = msb->mrq_handler;
520         return 0;
521 }
522
523 static int h_mspro_block_default(struct memstick_dev *card,
524                                  struct memstick_request **mrq)
525 {
526         complete(&card->mrq_complete);
527         if (!(*mrq)->error)
528                 return -EAGAIN;
529         else
530                 return (*mrq)->error;
531 }
532
533 static int h_mspro_block_get_ro(struct memstick_dev *card,
534                                 struct memstick_request **mrq)
535 {
536         struct mspro_block_data *msb = memstick_get_drvdata(card);
537
538         if ((*mrq)->error) {
539                 complete(&card->mrq_complete);
540                 return (*mrq)->error;
541         }
542
543         if ((*mrq)->data[offsetof(struct ms_status_register, status0)]
544             & MEMSTICK_STATUS0_WP)
545                 msb->read_only = 1;
546         else
547                 msb->read_only = 0;
548
549         complete(&card->mrq_complete);
550         return -EAGAIN;
551 }
552
553 static int h_mspro_block_wait_for_ced(struct memstick_dev *card,
554                                       struct memstick_request **mrq)
555 {
556         if ((*mrq)->error) {
557                 complete(&card->mrq_complete);
558                 return (*mrq)->error;
559         }
560
561         dev_dbg(&card->dev, "wait for ced: value %x\n", (*mrq)->data[0]);
562
563         if ((*mrq)->data[0] & (MEMSTICK_INT_CMDNAK | MEMSTICK_INT_ERR)) {
564                 card->current_mrq.error = -EFAULT;
565                 complete(&card->mrq_complete);
566                 return card->current_mrq.error;
567         }
568
569         if (!((*mrq)->data[0] & MEMSTICK_INT_CED))
570                 return 0;
571         else {
572                 card->current_mrq.error = 0;
573                 complete(&card->mrq_complete);
574                 return -EAGAIN;
575         }
576 }
577
578 static int h_mspro_block_transfer_data(struct memstick_dev *card,
579                                        struct memstick_request **mrq)
580 {
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)->need_card_int = 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 (msb->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)->need_card_int = 1;
650                 return 0;
651         case MS_TPC_READ_LONG_DATA:
652         case MS_TPC_WRITE_LONG_DATA:
653                 msb->current_page++;
654                 if (msb->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         msb->caps = host->caps;
1056         if (msb->caps & MEMSTICK_CAP_PAR4) {
1057                 if (mspro_block_switch_to_parallel(card))
1058                         printk(KERN_WARNING "%s: could not switch to "
1059                                "parallel interface\n", card->dev.bus_id);
1060         }
1061
1062         rc = mspro_block_wait_for_ced(card);
1063         if (rc)
1064                 return rc;
1065         dev_dbg(&card->dev, "card activated\n");
1066         if (msb->system != MEMSTICK_SYS_SERIAL)
1067                 msb->caps |= MEMSTICK_CAP_AUTO_GET_INT;
1068
1069         card->next_request = h_mspro_block_req_init;
1070         msb->mrq_handler = h_mspro_block_get_ro;
1071         memstick_init_req(&card->current_mrq, MS_TPC_READ_REG, NULL,
1072                           sizeof(struct ms_status_register));
1073         memstick_new_req(card->host);
1074         wait_for_completion(&card->mrq_complete);
1075         if (card->current_mrq.error)
1076                 return card->current_mrq.error;
1077
1078         dev_dbg(&card->dev, "card r/w status %d\n", msb->read_only ? 0 : 1);
1079
1080         msb->page_size = 512;
1081         rc = mspro_block_read_attributes(card);
1082         if (rc)
1083                 return rc;
1084
1085         dev_dbg(&card->dev, "attributes loaded\n");
1086         return 0;
1087
1088 }
1089
1090 static int mspro_block_init_disk(struct memstick_dev *card)
1091 {
1092         struct mspro_block_data *msb = memstick_get_drvdata(card);
1093         struct memstick_host *host = card->host;
1094         struct mspro_devinfo *dev_info = NULL;
1095         struct mspro_sys_info *sys_info = NULL;
1096         struct mspro_sys_attr *s_attr = NULL;
1097         int rc, disk_id;
1098         u64 limit = BLK_BOUNCE_HIGH;
1099         unsigned long capacity;
1100
1101         if (host->cdev.dev->dma_mask && *(host->cdev.dev->dma_mask))
1102                 limit = *(host->cdev.dev->dma_mask);
1103
1104         for (rc = 0; msb->attr_group.attrs[rc]; ++rc) {
1105                 s_attr = mspro_from_sysfs_attr(msb->attr_group.attrs[rc]);
1106
1107                 if (s_attr->id == MSPRO_BLOCK_ID_DEVINFO)
1108                         dev_info = s_attr->data;
1109                 else if (s_attr->id == MSPRO_BLOCK_ID_SYSINFO)
1110                         sys_info = s_attr->data;
1111         }
1112
1113         if (!dev_info || !sys_info)
1114                 return -ENODEV;
1115
1116         msb->cylinders = be16_to_cpu(dev_info->cylinders);
1117         msb->heads = be16_to_cpu(dev_info->heads);
1118         msb->sectors_per_track = be16_to_cpu(dev_info->sectors_per_track);
1119
1120         msb->page_size = be16_to_cpu(sys_info->unit_size);
1121
1122         if (!idr_pre_get(&mspro_block_disk_idr, GFP_KERNEL))
1123                 return -ENOMEM;
1124
1125         mutex_lock(&mspro_block_disk_lock);
1126         rc = idr_get_new(&mspro_block_disk_idr, card, &disk_id);
1127         mutex_unlock(&mspro_block_disk_lock);
1128
1129         if (rc)
1130                 return rc;
1131
1132         if ((disk_id << MEMSTICK_PART_SHIFT) > 255) {
1133                 rc = -ENOSPC;
1134                 goto out_release_id;
1135         }
1136
1137         msb->disk = alloc_disk(1 << MEMSTICK_PART_SHIFT);
1138         if (!msb->disk) {
1139                 rc = -ENOMEM;
1140                 goto out_release_id;
1141         }
1142
1143         spin_lock_init(&msb->q_lock);
1144         init_waitqueue_head(&msb->q_wait);
1145
1146         msb->queue = blk_init_queue(mspro_block_request, &msb->q_lock);
1147         if (!msb->queue) {
1148                 rc = -ENOMEM;
1149                 goto out_put_disk;
1150         }
1151
1152         msb->queue->queuedata = card;
1153
1154         blk_queue_bounce_limit(msb->queue, limit);
1155         blk_queue_max_sectors(msb->queue, MSPRO_BLOCK_MAX_PAGES);
1156         blk_queue_max_phys_segments(msb->queue, MSPRO_BLOCK_MAX_SEGS);
1157         blk_queue_max_hw_segments(msb->queue, MSPRO_BLOCK_MAX_SEGS);
1158         blk_queue_max_segment_size(msb->queue,
1159                                    MSPRO_BLOCK_MAX_PAGES * msb->page_size);
1160
1161         msb->disk->major = major;
1162         msb->disk->first_minor = disk_id << MEMSTICK_PART_SHIFT;
1163         msb->disk->fops = &ms_block_bdops;
1164         msb->usage_count = 1;
1165         msb->disk->private_data = msb;
1166         msb->disk->queue = msb->queue;
1167         msb->disk->driverfs_dev = &card->dev;
1168
1169         sprintf(msb->disk->disk_name, "mspblk%d", disk_id);
1170
1171         blk_queue_hardsect_size(msb->queue, msb->page_size);
1172
1173         capacity = be16_to_cpu(sys_info->user_block_count);
1174         capacity *= be16_to_cpu(sys_info->block_size);
1175         capacity *= msb->page_size >> 9;
1176         set_capacity(msb->disk, capacity);
1177         dev_dbg(&card->dev, "capacity set %ld\n", capacity);
1178         msb->q_thread = kthread_run(mspro_block_queue_thread, card,
1179                                     DRIVER_NAME"d");
1180         if (IS_ERR(msb->q_thread))
1181                 goto out_put_disk;
1182
1183         mutex_unlock(&host->lock);
1184         add_disk(msb->disk);
1185         mutex_lock(&host->lock);
1186         msb->active = 1;
1187         return 0;
1188
1189 out_put_disk:
1190         put_disk(msb->disk);
1191 out_release_id:
1192         mutex_lock(&mspro_block_disk_lock);
1193         idr_remove(&mspro_block_disk_idr, disk_id);
1194         mutex_unlock(&mspro_block_disk_lock);
1195         return rc;
1196 }
1197
1198 static void mspro_block_data_clear(struct mspro_block_data *msb)
1199 {
1200         int cnt;
1201         struct mspro_sys_attr *s_attr;
1202
1203         if (msb->attr_group.attrs) {
1204                 for (cnt = 0; msb->attr_group.attrs[cnt]; ++cnt) {
1205                         s_attr = mspro_from_sysfs_attr(msb->attr_group
1206                                                            .attrs[cnt]);
1207                         kfree(s_attr->data);
1208                         kfree(s_attr);
1209                 }
1210                 kfree(msb->attr_group.attrs);
1211         }
1212
1213         msb->card = NULL;
1214 }
1215
1216 static int mspro_block_check_card(struct memstick_dev *card)
1217 {
1218         struct mspro_block_data *msb = memstick_get_drvdata(card);
1219
1220         return (msb->active == 1);
1221 }
1222
1223 static int mspro_block_probe(struct memstick_dev *card)
1224 {
1225         struct mspro_block_data *msb;
1226         int rc = 0;
1227
1228         msb = kzalloc(sizeof(struct mspro_block_data), GFP_KERNEL);
1229         if (!msb)
1230                 return -ENOMEM;
1231         memstick_set_drvdata(card, msb);
1232         msb->card = card;
1233
1234         rc = mspro_block_init_card(card);
1235
1236         if (rc)
1237                 goto out_free;
1238
1239         rc = sysfs_create_group(&card->dev.kobj, &msb->attr_group);
1240         if (rc)
1241                 goto out_free;
1242
1243         rc = mspro_block_init_disk(card);
1244         if (!rc) {
1245                 card->check = mspro_block_check_card;
1246                 return 0;
1247         }
1248
1249         sysfs_remove_group(&card->dev.kobj, &msb->attr_group);
1250 out_free:
1251         memstick_set_drvdata(card, NULL);
1252         mspro_block_data_clear(msb);
1253         kfree(msb);
1254         return rc;
1255 }
1256
1257 static void mspro_block_remove(struct memstick_dev *card)
1258 {
1259         struct mspro_block_data *msb = memstick_get_drvdata(card);
1260         struct task_struct *q_thread = NULL;
1261         unsigned long flags;
1262
1263         del_gendisk(msb->disk);
1264         dev_dbg(&card->dev, "mspro block remove\n");
1265         spin_lock_irqsave(&msb->q_lock, flags);
1266         q_thread = msb->q_thread;
1267         msb->q_thread = NULL;
1268         msb->active = 0;
1269         spin_unlock_irqrestore(&msb->q_lock, flags);
1270
1271         if (q_thread) {
1272                 mutex_unlock(&card->host->lock);
1273                 kthread_stop(q_thread);
1274                 mutex_lock(&card->host->lock);
1275         }
1276
1277         dev_dbg(&card->dev, "queue thread stopped\n");
1278
1279         blk_cleanup_queue(msb->queue);
1280
1281         sysfs_remove_group(&card->dev.kobj, &msb->attr_group);
1282
1283         mutex_lock(&mspro_block_disk_lock);
1284         mspro_block_data_clear(msb);
1285         mutex_unlock(&mspro_block_disk_lock);
1286
1287         mspro_block_disk_release(msb->disk);
1288         memstick_set_drvdata(card, NULL);
1289 }
1290
1291 #ifdef CONFIG_PM
1292
1293 static int mspro_block_suspend(struct memstick_dev *card, pm_message_t state)
1294 {
1295         struct mspro_block_data *msb = memstick_get_drvdata(card);
1296         struct task_struct *q_thread = NULL;
1297         unsigned long flags;
1298
1299         spin_lock_irqsave(&msb->q_lock, flags);
1300         q_thread = msb->q_thread;
1301         msb->q_thread = NULL;
1302         msb->active = 0;
1303         blk_stop_queue(msb->queue);
1304         spin_unlock_irqrestore(&msb->q_lock, flags);
1305
1306         if (q_thread)
1307                 kthread_stop(q_thread);
1308
1309         return 0;
1310 }
1311
1312 static int mspro_block_resume(struct memstick_dev *card)
1313 {
1314         struct mspro_block_data *msb = memstick_get_drvdata(card);
1315         unsigned long flags;
1316         int rc = 0;
1317
1318 #ifdef CONFIG_MEMSTICK_UNSAFE_RESUME
1319
1320         struct mspro_block_data *new_msb;
1321         struct memstick_host *host = card->host;
1322         struct mspro_sys_attr *s_attr, *r_attr;
1323         unsigned char cnt;
1324
1325         mutex_lock(&host->lock);
1326         new_msb = kzalloc(sizeof(struct mspro_block_data), GFP_KERNEL);
1327         if (!new_msb) {
1328                 rc = -ENOMEM;
1329                 goto out_unlock;
1330         }
1331
1332         new_msb->card = card;
1333         memstick_set_drvdata(card, new_msb);
1334         if (mspro_block_init_card(card))
1335                 goto out_free;
1336
1337         for (cnt = 0; new_msb->attr_group.attrs[cnt]
1338                       && msb->attr_group.attrs[cnt]; ++cnt) {
1339                 s_attr = mspro_from_sysfs_attr(new_msb->attr_group.attrs[cnt]);
1340                 r_attr = mspro_from_sysfs_attr(msb->attr_group.attrs[cnt]);
1341
1342                 if (s_attr->id == MSPRO_BLOCK_ID_SYSINFO
1343                     && r_attr->id == s_attr->id) {
1344                         if (memcmp(s_attr->data, r_attr->data, s_attr->size))
1345                                 break;
1346
1347                         memstick_set_drvdata(card, msb);
1348                         msb->q_thread = kthread_run(mspro_block_queue_thread,
1349                                                     card, DRIVER_NAME"d");
1350                         if (IS_ERR(msb->q_thread))
1351                                 msb->q_thread = NULL;
1352                         else
1353                                 msb->active = 1;
1354
1355                         break;
1356                 }
1357         }
1358
1359 out_free:
1360         memstick_set_drvdata(card, msb);
1361         mspro_block_data_clear(new_msb);
1362         kfree(new_msb);
1363 out_unlock:
1364         mutex_unlock(&host->lock);
1365
1366 #endif /* CONFIG_MEMSTICK_UNSAFE_RESUME */
1367
1368         spin_lock_irqsave(&msb->q_lock, flags);
1369         blk_start_queue(msb->queue);
1370         spin_unlock_irqrestore(&msb->q_lock, flags);
1371         return rc;
1372 }
1373
1374 #else
1375
1376 #define mspro_block_suspend NULL
1377 #define mspro_block_resume NULL
1378
1379 #endif /* CONFIG_PM */
1380
1381 static struct memstick_device_id mspro_block_id_tbl[] = {
1382         {MEMSTICK_MATCH_ALL, MEMSTICK_TYPE_PRO, MEMSTICK_CATEGORY_STORAGE_DUO,
1383          MEMSTICK_CLASS_GENERIC_DUO},
1384         {}
1385 };
1386
1387
1388 static struct memstick_driver mspro_block_driver = {
1389         .driver = {
1390                 .name  = DRIVER_NAME,
1391                 .owner = THIS_MODULE
1392         },
1393         .id_table = mspro_block_id_tbl,
1394         .probe    = mspro_block_probe,
1395         .remove   = mspro_block_remove,
1396         .suspend  = mspro_block_suspend,
1397         .resume   = mspro_block_resume
1398 };
1399
1400 static int __init mspro_block_init(void)
1401 {
1402         int rc = -ENOMEM;
1403
1404         rc = register_blkdev(major, DRIVER_NAME);
1405         if (rc < 0) {
1406                 printk(KERN_ERR DRIVER_NAME ": failed to register "
1407                        "major %d, error %d\n", major, rc);
1408                 return rc;
1409         }
1410         if (!major)
1411                 major = rc;
1412
1413         rc = memstick_register_driver(&mspro_block_driver);
1414         if (rc)
1415                 unregister_blkdev(major, DRIVER_NAME);
1416         return rc;
1417 }
1418
1419 static void __exit mspro_block_exit(void)
1420 {
1421         memstick_unregister_driver(&mspro_block_driver);
1422         unregister_blkdev(major, DRIVER_NAME);
1423         idr_destroy(&mspro_block_disk_idr);
1424 }
1425
1426 module_init(mspro_block_init);
1427 module_exit(mspro_block_exit);
1428
1429 MODULE_LICENSE("GPL");
1430 MODULE_AUTHOR("Alex Dubov");
1431 MODULE_DESCRIPTION("Sony MemoryStickPro block device driver");
1432 MODULE_DEVICE_TABLE(memstick, mspro_block_id_tbl);