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