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