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