Merge git://git.infradead.org/mtd-2.6
[linux-2.6] / drivers / memstick / core / mspro_block.c
1 /*
2  *  Sony MemoryStick Pro storage support
3  *
4  *  Copyright (C) 2007 Alex Dubov <oakad@yahoo.com>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
10  * Special thanks to Carlos Corbacho for providing various MemoryStick cards
11  * that made this driver possible.
12  *
13  */
14
15 #include <linux/blkdev.h>
16 #include <linux/idr.h>
17 #include <linux/hdreg.h>
18 #include <linux/kthread.h>
19 #include <linux/delay.h>
20 #include <linux/memstick.h>
21
22 #define DRIVER_NAME "mspro_block"
23
24 static int major;
25 module_param(major, int, 0644);
26
27 #define MSPRO_BLOCK_MAX_SEGS  32
28 #define MSPRO_BLOCK_MAX_PAGES ((2 << 16) - 1)
29
30 #define MSPRO_BLOCK_SIGNATURE        0xa5c3
31 #define MSPRO_BLOCK_MAX_ATTRIBUTES   41
32
33 #define MSPRO_BLOCK_PART_SHIFT 3
34
35 enum {
36         MSPRO_BLOCK_ID_SYSINFO         = 0x10,
37         MSPRO_BLOCK_ID_MODELNAME       = 0x15,
38         MSPRO_BLOCK_ID_MBR             = 0x20,
39         MSPRO_BLOCK_ID_PBR16           = 0x21,
40         MSPRO_BLOCK_ID_PBR32           = 0x22,
41         MSPRO_BLOCK_ID_SPECFILEVALUES1 = 0x25,
42         MSPRO_BLOCK_ID_SPECFILEVALUES2 = 0x26,
43         MSPRO_BLOCK_ID_DEVINFO         = 0x30
44 };
45
46 struct mspro_sys_attr {
47         size_t                  size;
48         void                    *data;
49         unsigned char           id;
50         char                    name[32];
51         struct device_attribute dev_attr;
52 };
53
54 struct mspro_attr_entry {
55         unsigned int  address;
56         unsigned int  size;
57         unsigned char id;
58         unsigned char reserved[3];
59 } __attribute__((packed));
60
61 struct mspro_attribute {
62         unsigned short          signature;
63         unsigned short          version;
64         unsigned char           count;
65         unsigned char           reserved[11];
66         struct mspro_attr_entry entries[];
67 } __attribute__((packed));
68
69 struct mspro_sys_info {
70         unsigned char  class;
71         unsigned char  reserved0;
72         unsigned short block_size;
73         unsigned short block_count;
74         unsigned short user_block_count;
75         unsigned short page_size;
76         unsigned char  reserved1[2];
77         unsigned char  assembly_date[8];
78         unsigned int   serial_number;
79         unsigned char  assembly_maker_code;
80         unsigned char  assembly_model_code[3];
81         unsigned short memory_maker_code;
82         unsigned short memory_model_code;
83         unsigned char  reserved2[4];
84         unsigned char  vcc;
85         unsigned char  vpp;
86         unsigned short controller_number;
87         unsigned short controller_function;
88         unsigned short start_sector;
89         unsigned short unit_size;
90         unsigned char  ms_sub_class;
91         unsigned char  reserved3[4];
92         unsigned char  interface_type;
93         unsigned short controller_code;
94         unsigned char  format_type;
95         unsigned char  reserved4;
96         unsigned char  device_type;
97         unsigned char  reserved5[7];
98         unsigned char  mspro_id[16];
99         unsigned char  reserved6[16];
100 } __attribute__((packed));
101
102 struct mspro_mbr {
103         unsigned char boot_partition;
104         unsigned char start_head;
105         unsigned char start_sector;
106         unsigned char start_cylinder;
107         unsigned char partition_type;
108         unsigned char end_head;
109         unsigned char end_sector;
110         unsigned char end_cylinder;
111         unsigned int  start_sectors;
112         unsigned int  sectors_per_partition;
113 } __attribute__((packed));
114
115 struct mspro_specfile {
116         char           name[8];
117         char           ext[3];
118         unsigned char  attr;
119         unsigned char  reserved[10];
120         unsigned short time;
121         unsigned short date;
122         unsigned short cluster;
123         unsigned int   size;
124 } __attribute__((packed));
125
126 struct mspro_devinfo {
127         unsigned short cylinders;
128         unsigned short heads;
129         unsigned short bytes_per_track;
130         unsigned short bytes_per_sector;
131         unsigned short sectors_per_track;
132         unsigned char  reserved[6];
133 } __attribute__((packed));
134
135 struct mspro_block_data {
136         struct memstick_dev   *card;
137         unsigned int          usage_count;
138         unsigned int          caps;
139         struct gendisk        *disk;
140         struct request_queue  *queue;
141         struct request        *block_req;
142         spinlock_t            q_lock;
143
144         unsigned short        page_size;
145         unsigned short        cylinders;
146         unsigned short        heads;
147         unsigned short        sectors_per_track;
148
149         unsigned char         system;
150         unsigned char         read_only:1,
151                               eject:1,
152                               has_request:1,
153                               data_dir:1,
154                               active:1;
155         unsigned char         transfer_cmd;
156
157         int                   (*mrq_handler)(struct memstick_dev *card,
158                                              struct memstick_request **mrq);
159
160         struct attribute_group attr_group;
161
162         struct scatterlist    req_sg[MSPRO_BLOCK_MAX_SEGS];
163         unsigned int          seg_count;
164         unsigned int          current_seg;
165         unsigned int          current_page;
166 };
167
168 static DEFINE_IDR(mspro_block_disk_idr);
169 static DEFINE_MUTEX(mspro_block_disk_lock);
170
171 static int mspro_block_complete_req(struct memstick_dev *card, int error);
172
173 /*** Block device ***/
174
175 static int mspro_block_bd_open(struct block_device *bdev, fmode_t mode)
176 {
177         struct gendisk *disk = bdev->bd_disk;
178         struct mspro_block_data *msb = disk->private_data;
179         int rc = -ENXIO;
180
181         mutex_lock(&mspro_block_disk_lock);
182
183         if (msb && msb->card) {
184                 msb->usage_count++;
185                 if ((mode & FMODE_WRITE) && msb->read_only)
186                         rc = -EROFS;
187                 else
188                         rc = 0;
189         }
190
191         mutex_unlock(&mspro_block_disk_lock);
192
193         return rc;
194 }
195
196
197 static int mspro_block_disk_release(struct gendisk *disk)
198 {
199         struct mspro_block_data *msb = disk->private_data;
200         int disk_id = MINOR(disk_devt(disk)) >> MSPRO_BLOCK_PART_SHIFT;
201
202         mutex_lock(&mspro_block_disk_lock);
203
204         if (msb) {
205                 if (msb->usage_count)
206                         msb->usage_count--;
207
208                 if (!msb->usage_count) {
209                         kfree(msb);
210                         disk->private_data = NULL;
211                         idr_remove(&mspro_block_disk_idr, disk_id);
212                         put_disk(disk);
213                 }
214         }
215
216         mutex_unlock(&mspro_block_disk_lock);
217
218         return 0;
219 }
220
221 static int mspro_block_bd_release(struct gendisk *disk, fmode_t mode)
222 {
223         return mspro_block_disk_release(disk);
224 }
225
226 static int mspro_block_bd_getgeo(struct block_device *bdev,
227                                  struct hd_geometry *geo)
228 {
229         struct mspro_block_data *msb = bdev->bd_disk->private_data;
230
231         geo->heads = msb->heads;
232         geo->sectors = msb->sectors_per_track;
233         geo->cylinders = msb->cylinders;
234
235         return 0;
236 }
237
238 static struct block_device_operations ms_block_bdops = {
239         .open    = mspro_block_bd_open,
240         .release = mspro_block_bd_release,
241         .getgeo  = mspro_block_bd_getgeo,
242         .owner   = THIS_MODULE
243 };
244
245 /*** Information ***/
246
247 static struct mspro_sys_attr *mspro_from_sysfs_attr(struct attribute *attr)
248 {
249         struct device_attribute *dev_attr
250                 = container_of(attr, struct device_attribute, attr);
251         return container_of(dev_attr, struct mspro_sys_attr, dev_attr);
252 }
253
254 static const char *mspro_block_attr_name(unsigned char tag)
255 {
256         switch (tag) {
257         case MSPRO_BLOCK_ID_SYSINFO:
258                 return "attr_sysinfo";
259         case MSPRO_BLOCK_ID_MODELNAME:
260                 return "attr_modelname";
261         case MSPRO_BLOCK_ID_MBR:
262                 return "attr_mbr";
263         case MSPRO_BLOCK_ID_PBR16:
264                 return "attr_pbr16";
265         case MSPRO_BLOCK_ID_PBR32:
266                 return "attr_pbr32";
267         case MSPRO_BLOCK_ID_SPECFILEVALUES1:
268                 return "attr_specfilevalues1";
269         case MSPRO_BLOCK_ID_SPECFILEVALUES2:
270                 return "attr_specfilevalues2";
271         case MSPRO_BLOCK_ID_DEVINFO:
272                 return "attr_devinfo";
273         default:
274                 return NULL;
275         };
276 }
277
278 typedef ssize_t (*sysfs_show_t)(struct device *dev,
279                                 struct device_attribute *attr,
280                                 char *buffer);
281
282 static ssize_t mspro_block_attr_show_default(struct device *dev,
283                                              struct device_attribute *attr,
284                                              char *buffer)
285 {
286         struct mspro_sys_attr *s_attr = container_of(attr,
287                                                      struct mspro_sys_attr,
288                                                      dev_attr);
289
290         ssize_t cnt, rc = 0;
291
292         for (cnt = 0; cnt < s_attr->size; cnt++) {
293                 if (cnt && !(cnt % 16)) {
294                         if (PAGE_SIZE - rc)
295                                 buffer[rc++] = '\n';
296                 }
297
298                 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "%02x ",
299                                 ((unsigned char *)s_attr->data)[cnt]);
300         }
301         return rc;
302 }
303
304 static ssize_t mspro_block_attr_show_sysinfo(struct device *dev,
305                                              struct device_attribute *attr,
306                                              char *buffer)
307 {
308         struct mspro_sys_attr *x_attr = container_of(attr,
309                                                      struct mspro_sys_attr,
310                                                      dev_attr);
311         struct mspro_sys_info *x_sys = x_attr->data;
312         ssize_t rc = 0;
313         int date_tz = 0, date_tz_f = 0;
314
315         if (x_sys->assembly_date[0] > 0x80U) {
316                 date_tz = (~x_sys->assembly_date[0]) + 1;
317                 date_tz_f = date_tz & 3;
318                 date_tz >>= 2;
319                 date_tz = -date_tz;
320                 date_tz_f *= 15;
321         } else if (x_sys->assembly_date[0] < 0x80U) {
322                 date_tz = x_sys->assembly_date[0];
323                 date_tz_f = date_tz & 3;
324                 date_tz >>= 2;
325                 date_tz_f *= 15;
326         }
327
328         rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "class: %x\n",
329                         x_sys->class);
330         rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "block size: %x\n",
331                         be16_to_cpu(x_sys->block_size));
332         rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "block count: %x\n",
333                         be16_to_cpu(x_sys->block_count));
334         rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "user block count: %x\n",
335                         be16_to_cpu(x_sys->user_block_count));
336         rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "page size: %x\n",
337                         be16_to_cpu(x_sys->page_size));
338         rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "assembly date: "
339                         "GMT%+d:%d %04u-%02u-%02u %02u:%02u:%02u\n",
340                         date_tz, date_tz_f,
341                         be16_to_cpu(*(unsigned short *)
342                                     &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(msb->block_req, -ENOMEM,
677                                         blk_rq_cur_bytes(msb->block_req));
678                         continue;
679                 }
680
681                 t_sec = msb->block_req->sector << 9;
682                 sector_div(t_sec, msb->page_size);
683
684                 count = msb->block_req->nr_sectors << 9;
685                 count /= msb->page_size;
686
687                 param.system = msb->system;
688                 param.data_count = cpu_to_be16(count);
689                 param.data_address = cpu_to_be32((uint32_t)t_sec);
690                 param.tpc_param = 0;
691
692                 msb->data_dir = rq_data_dir(msb->block_req);
693                 msb->transfer_cmd = msb->data_dir == READ
694                                     ? MSPRO_CMD_READ_DATA
695                                     : MSPRO_CMD_WRITE_DATA;
696
697                 dev_dbg(&card->dev, "data transfer: cmd %x, "
698                         "lba %x, count %x\n", msb->transfer_cmd,
699                         be32_to_cpu(param.data_address), count);
700
701                 card->next_request = h_mspro_block_req_init;
702                 msb->mrq_handler = h_mspro_block_transfer_data;
703                 memstick_init_req(&card->current_mrq, MS_TPC_WRITE_REG,
704                                   &param, sizeof(param));
705                 memstick_new_req(card->host);
706                 return 0;
707         }
708
709         dev_dbg(&card->dev, "elv_next\n");
710         msb->block_req = elv_next_request(msb->queue);
711         if (!msb->block_req) {
712                 dev_dbg(&card->dev, "issue end\n");
713                 return -EAGAIN;
714         }
715
716         dev_dbg(&card->dev, "trying again\n");
717         chunk = 1;
718         goto try_again;
719 }
720
721 static int mspro_block_complete_req(struct memstick_dev *card, int error)
722 {
723         struct mspro_block_data *msb = memstick_get_drvdata(card);
724         int chunk, cnt;
725         unsigned int t_len = 0;
726         unsigned long flags;
727
728         spin_lock_irqsave(&msb->q_lock, flags);
729         dev_dbg(&card->dev, "complete %d, %d\n", msb->has_request ? 1 : 0,
730                 error);
731
732         if (msb->has_request) {
733                 /* Nothing to do - not really an error */
734                 if (error == -EAGAIN)
735                         error = 0;
736
737                 if (error || (card->current_mrq.tpc == MSPRO_CMD_STOP)) {
738                         if (msb->data_dir == READ) {
739                                 for (cnt = 0; cnt < msb->current_seg; cnt++)
740                                         t_len += msb->req_sg[cnt].length
741                                                  / msb->page_size;
742
743                                         if (msb->current_page)
744                                                 t_len += msb->current_page - 1;
745
746                                         t_len *= msb->page_size;
747                         }
748                 } else
749                         t_len = msb->block_req->nr_sectors << 9;
750
751                 dev_dbg(&card->dev, "transferred %x (%d)\n", t_len, error);
752
753                 if (error && !t_len)
754                         t_len = blk_rq_cur_bytes(msb->block_req);
755
756                 chunk = __blk_end_request(msb->block_req, error, t_len);
757
758                 error = mspro_block_issue_req(card, chunk);
759
760                 if (!error)
761                         goto out;
762                 else
763                         msb->has_request = 0;
764         } else {
765                 if (!error)
766                         error = -EAGAIN;
767         }
768
769         card->next_request = h_mspro_block_default_bad;
770         complete_all(&card->mrq_complete);
771 out:
772         spin_unlock_irqrestore(&msb->q_lock, flags);
773         return error;
774 }
775
776 static void mspro_block_stop(struct memstick_dev *card)
777 {
778         struct mspro_block_data *msb = memstick_get_drvdata(card);
779         int rc = 0;
780         unsigned long flags;
781
782         while (1) {
783                 spin_lock_irqsave(&msb->q_lock, flags);
784                 if (!msb->has_request) {
785                         blk_stop_queue(msb->queue);
786                         rc = 1;
787                 }
788                 spin_unlock_irqrestore(&msb->q_lock, flags);
789
790                 if (rc)
791                         break;
792
793                 wait_for_completion(&card->mrq_complete);
794         }
795 }
796
797 static void mspro_block_start(struct memstick_dev *card)
798 {
799         struct mspro_block_data *msb = memstick_get_drvdata(card);
800         unsigned long flags;
801
802         spin_lock_irqsave(&msb->q_lock, flags);
803         blk_start_queue(msb->queue);
804         spin_unlock_irqrestore(&msb->q_lock, flags);
805 }
806
807 static int mspro_block_prepare_req(struct request_queue *q, struct request *req)
808 {
809         if (!blk_fs_request(req) && !blk_pc_request(req)) {
810                 blk_dump_rq_flags(req, "MSPro unsupported request");
811                 return BLKPREP_KILL;
812         }
813
814         req->cmd_flags |= REQ_DONTPREP;
815
816         return BLKPREP_OK;
817 }
818
819 static void mspro_block_submit_req(struct request_queue *q)
820 {
821         struct memstick_dev *card = q->queuedata;
822         struct mspro_block_data *msb = memstick_get_drvdata(card);
823         struct request *req = NULL;
824
825         if (msb->has_request)
826                 return;
827
828         if (msb->eject) {
829                 while ((req = elv_next_request(q)) != NULL)
830                         __blk_end_request(req, -ENODEV, blk_rq_bytes(req));
831
832                 return;
833         }
834
835         msb->has_request = 1;
836         if (mspro_block_issue_req(card, 0))
837                 msb->has_request = 0;
838 }
839
840 /*** Initialization ***/
841
842 static int mspro_block_wait_for_ced(struct memstick_dev *card)
843 {
844         struct mspro_block_data *msb = memstick_get_drvdata(card);
845
846         card->next_request = h_mspro_block_req_init;
847         msb->mrq_handler = h_mspro_block_wait_for_ced;
848         memstick_init_req(&card->current_mrq, MS_TPC_GET_INT, NULL, 1);
849         memstick_new_req(card->host);
850         wait_for_completion(&card->mrq_complete);
851         return card->current_mrq.error;
852 }
853
854 static int mspro_block_set_interface(struct memstick_dev *card,
855                                      unsigned char sys_reg)
856 {
857         struct memstick_host *host = card->host;
858         struct mspro_block_data *msb = memstick_get_drvdata(card);
859         struct mspro_param_register param = {
860                 .system = sys_reg,
861                 .data_count = 0,
862                 .data_address = 0,
863                 .tpc_param = 0
864         };
865
866         card->next_request = h_mspro_block_req_init;
867         msb->mrq_handler = h_mspro_block_default;
868         memstick_init_req(&card->current_mrq, MS_TPC_WRITE_REG, &param,
869                           sizeof(param));
870         memstick_new_req(host);
871         wait_for_completion(&card->mrq_complete);
872         return card->current_mrq.error;
873 }
874
875 static int mspro_block_switch_interface(struct memstick_dev *card)
876 {
877         struct memstick_host *host = card->host;
878         struct mspro_block_data *msb = memstick_get_drvdata(card);
879         int rc = 0;
880
881 try_again:
882         if (msb->caps & MEMSTICK_CAP_PAR4)
883                 rc = mspro_block_set_interface(card, MEMSTICK_SYS_PAR4);
884         else
885                 return 0;
886
887         if (rc) {
888                 printk(KERN_WARNING
889                        "%s: could not switch to 4-bit mode, error %d\n",
890                        dev_name(&card->dev), rc);
891                 return 0;
892         }
893
894         msb->system = MEMSTICK_SYS_PAR4;
895         host->set_param(host, MEMSTICK_INTERFACE, MEMSTICK_PAR4);
896         printk(KERN_INFO "%s: switching to 4-bit parallel mode\n",
897                dev_name(&card->dev));
898
899         if (msb->caps & MEMSTICK_CAP_PAR8) {
900                 rc = mspro_block_set_interface(card, MEMSTICK_SYS_PAR8);
901
902                 if (!rc) {
903                         msb->system = MEMSTICK_SYS_PAR8;
904                         host->set_param(host, MEMSTICK_INTERFACE,
905                                         MEMSTICK_PAR8);
906                         printk(KERN_INFO
907                                "%s: switching to 8-bit parallel mode\n",
908                                dev_name(&card->dev));
909                 } else
910                         printk(KERN_WARNING
911                                "%s: could not switch to 8-bit mode, error %d\n",
912                                dev_name(&card->dev), rc);
913         }
914
915         card->next_request = h_mspro_block_req_init;
916         msb->mrq_handler = h_mspro_block_default;
917         memstick_init_req(&card->current_mrq, MS_TPC_GET_INT, NULL, 1);
918         memstick_new_req(card->host);
919         wait_for_completion(&card->mrq_complete);
920         rc = card->current_mrq.error;
921
922         if (rc) {
923                 printk(KERN_WARNING
924                        "%s: interface error, trying to fall back to serial\n",
925                        dev_name(&card->dev));
926                 msb->system = MEMSTICK_SYS_SERIAL;
927                 host->set_param(host, MEMSTICK_POWER, MEMSTICK_POWER_OFF);
928                 msleep(10);
929                 host->set_param(host, MEMSTICK_POWER, MEMSTICK_POWER_ON);
930                 host->set_param(host, MEMSTICK_INTERFACE, MEMSTICK_SERIAL);
931
932                 rc = memstick_set_rw_addr(card);
933                 if (!rc)
934                         rc = mspro_block_set_interface(card, msb->system);
935
936                 if (!rc) {
937                         msleep(150);
938                         rc = mspro_block_wait_for_ced(card);
939                         if (rc)
940                                 return rc;
941
942                         if (msb->caps & MEMSTICK_CAP_PAR8) {
943                                 msb->caps &= ~MEMSTICK_CAP_PAR8;
944                                 goto try_again;
945                         }
946                 }
947         }
948         return rc;
949 }
950
951 /* Memory allocated for attributes by this function should be freed by
952  * mspro_block_data_clear, no matter if the initialization process succeded
953  * or failed.
954  */
955 static int mspro_block_read_attributes(struct memstick_dev *card)
956 {
957         struct mspro_block_data *msb = memstick_get_drvdata(card);
958         struct mspro_param_register param = {
959                 .system = msb->system,
960                 .data_count = cpu_to_be16(1),
961                 .data_address = 0,
962                 .tpc_param = 0
963         };
964         struct mspro_attribute *attr = NULL;
965         struct mspro_sys_attr *s_attr = NULL;
966         unsigned char *buffer = NULL;
967         int cnt, rc, attr_count;
968         unsigned int addr;
969         unsigned short page_count;
970
971         attr = kmalloc(msb->page_size, GFP_KERNEL);
972         if (!attr)
973                 return -ENOMEM;
974
975         sg_init_one(&msb->req_sg[0], attr, msb->page_size);
976         msb->seg_count = 1;
977         msb->current_seg = 0;
978         msb->current_page = 0;
979         msb->data_dir = READ;
980         msb->transfer_cmd = MSPRO_CMD_READ_ATRB;
981
982         card->next_request = h_mspro_block_req_init;
983         msb->mrq_handler = h_mspro_block_transfer_data;
984         memstick_init_req(&card->current_mrq, MS_TPC_WRITE_REG, &param,
985                           sizeof(param));
986         memstick_new_req(card->host);
987         wait_for_completion(&card->mrq_complete);
988         if (card->current_mrq.error) {
989                 rc = card->current_mrq.error;
990                 goto out_free_attr;
991         }
992
993         if (be16_to_cpu(attr->signature) != MSPRO_BLOCK_SIGNATURE) {
994                 printk(KERN_ERR "%s: unrecognized device signature %x\n",
995                        dev_name(&card->dev), be16_to_cpu(attr->signature));
996                 rc = -ENODEV;
997                 goto out_free_attr;
998         }
999
1000         if (attr->count > MSPRO_BLOCK_MAX_ATTRIBUTES) {
1001                 printk(KERN_WARNING "%s: way too many attribute entries\n",
1002                        dev_name(&card->dev));
1003                 attr_count = MSPRO_BLOCK_MAX_ATTRIBUTES;
1004         } else
1005                 attr_count = attr->count;
1006
1007         msb->attr_group.attrs = kzalloc((attr_count + 1)
1008                                         * sizeof(struct attribute),
1009                                         GFP_KERNEL);
1010         if (!msb->attr_group.attrs) {
1011                 rc = -ENOMEM;
1012                 goto out_free_attr;
1013         }
1014         msb->attr_group.name = "media_attributes";
1015
1016         buffer = kmalloc(msb->page_size, GFP_KERNEL);
1017         if (!buffer) {
1018                 rc = -ENOMEM;
1019                 goto out_free_attr;
1020         }
1021         memcpy(buffer, (char *)attr, msb->page_size);
1022         page_count = 1;
1023
1024         for (cnt = 0; cnt < attr_count; ++cnt) {
1025                 s_attr = kzalloc(sizeof(struct mspro_sys_attr), GFP_KERNEL);
1026                 if (!s_attr) {
1027                         rc = -ENOMEM;
1028                         goto out_free_buffer;
1029                 }
1030
1031                 msb->attr_group.attrs[cnt] = &s_attr->dev_attr.attr;
1032                 addr = be32_to_cpu(attr->entries[cnt].address);
1033                 rc = be32_to_cpu(attr->entries[cnt].size);
1034                 dev_dbg(&card->dev, "adding attribute %d: id %x, address %x, "
1035                         "size %x\n", cnt, attr->entries[cnt].id, addr, rc);
1036                 s_attr->id = attr->entries[cnt].id;
1037                 if (mspro_block_attr_name(s_attr->id))
1038                         snprintf(s_attr->name, sizeof(s_attr->name), "%s",
1039                                  mspro_block_attr_name(attr->entries[cnt].id));
1040                 else
1041                         snprintf(s_attr->name, sizeof(s_attr->name),
1042                                  "attr_x%02x", attr->entries[cnt].id);
1043
1044                 s_attr->dev_attr.attr.name = s_attr->name;
1045                 s_attr->dev_attr.attr.mode = S_IRUGO;
1046                 s_attr->dev_attr.show = mspro_block_attr_show(s_attr->id);
1047
1048                 if (!rc)
1049                         continue;
1050
1051                 s_attr->size = rc;
1052                 s_attr->data = kmalloc(rc, GFP_KERNEL);
1053                 if (!s_attr->data) {
1054                         rc = -ENOMEM;
1055                         goto out_free_buffer;
1056                 }
1057
1058                 if (((addr / msb->page_size)
1059                      == be32_to_cpu(param.data_address))
1060                     && (((addr + rc - 1) / msb->page_size)
1061                         == be32_to_cpu(param.data_address))) {
1062                         memcpy(s_attr->data, buffer + addr % msb->page_size,
1063                                rc);
1064                         continue;
1065                 }
1066
1067                 if (page_count <= (rc / msb->page_size)) {
1068                         kfree(buffer);
1069                         page_count = (rc / msb->page_size) + 1;
1070                         buffer = kmalloc(page_count * msb->page_size,
1071                                          GFP_KERNEL);
1072                         if (!buffer) {
1073                                 rc = -ENOMEM;
1074                                 goto out_free_attr;
1075                         }
1076                 }
1077
1078                 param.system = msb->system;
1079                 param.data_count = cpu_to_be16((rc / msb->page_size) + 1);
1080                 param.data_address = cpu_to_be32(addr / msb->page_size);
1081                 param.tpc_param = 0;
1082
1083                 sg_init_one(&msb->req_sg[0], buffer,
1084                             be16_to_cpu(param.data_count) * msb->page_size);
1085                 msb->seg_count = 1;
1086                 msb->current_seg = 0;
1087                 msb->current_page = 0;
1088                 msb->data_dir = READ;
1089                 msb->transfer_cmd = MSPRO_CMD_READ_ATRB;
1090
1091                 dev_dbg(&card->dev, "reading attribute pages %x, %x\n",
1092                         be32_to_cpu(param.data_address),
1093                         be16_to_cpu(param.data_count));
1094
1095                 card->next_request = h_mspro_block_req_init;
1096                 msb->mrq_handler = h_mspro_block_transfer_data;
1097                 memstick_init_req(&card->current_mrq, MS_TPC_WRITE_REG,
1098                                   (char *)&param, sizeof(param));
1099                 memstick_new_req(card->host);
1100                 wait_for_completion(&card->mrq_complete);
1101                 if (card->current_mrq.error) {
1102                         rc = card->current_mrq.error;
1103                         goto out_free_buffer;
1104                 }
1105
1106                 memcpy(s_attr->data, buffer + addr % msb->page_size, rc);
1107         }
1108
1109         rc = 0;
1110 out_free_buffer:
1111         kfree(buffer);
1112 out_free_attr:
1113         kfree(attr);
1114         return rc;
1115 }
1116
1117 static int mspro_block_init_card(struct memstick_dev *card)
1118 {
1119         struct mspro_block_data *msb = memstick_get_drvdata(card);
1120         struct memstick_host *host = card->host;
1121         int rc = 0;
1122
1123         msb->system = MEMSTICK_SYS_SERIAL;
1124         card->reg_addr.r_offset = offsetof(struct mspro_register, status);
1125         card->reg_addr.r_length = sizeof(struct ms_status_register);
1126         card->reg_addr.w_offset = offsetof(struct mspro_register, param);
1127         card->reg_addr.w_length = sizeof(struct mspro_param_register);
1128
1129         if (memstick_set_rw_addr(card))
1130                 return -EIO;
1131
1132         msb->caps = host->caps;
1133
1134         msleep(150);
1135         rc = mspro_block_wait_for_ced(card);
1136         if (rc)
1137                 return rc;
1138
1139         rc = mspro_block_switch_interface(card);
1140         if (rc)
1141                 return rc;
1142
1143         dev_dbg(&card->dev, "card activated\n");
1144         if (msb->system != MEMSTICK_SYS_SERIAL)
1145                 msb->caps |= MEMSTICK_CAP_AUTO_GET_INT;
1146
1147         card->next_request = h_mspro_block_req_init;
1148         msb->mrq_handler = h_mspro_block_get_ro;
1149         memstick_init_req(&card->current_mrq, MS_TPC_READ_REG, NULL,
1150                           sizeof(struct ms_status_register));
1151         memstick_new_req(card->host);
1152         wait_for_completion(&card->mrq_complete);
1153         if (card->current_mrq.error)
1154                 return card->current_mrq.error;
1155
1156         dev_dbg(&card->dev, "card r/w status %d\n", msb->read_only ? 0 : 1);
1157
1158         msb->page_size = 512;
1159         rc = mspro_block_read_attributes(card);
1160         if (rc)
1161                 return rc;
1162
1163         dev_dbg(&card->dev, "attributes loaded\n");
1164         return 0;
1165
1166 }
1167
1168 static int mspro_block_init_disk(struct memstick_dev *card)
1169 {
1170         struct mspro_block_data *msb = memstick_get_drvdata(card);
1171         struct memstick_host *host = card->host;
1172         struct mspro_devinfo *dev_info = NULL;
1173         struct mspro_sys_info *sys_info = NULL;
1174         struct mspro_sys_attr *s_attr = NULL;
1175         int rc, disk_id;
1176         u64 limit = BLK_BOUNCE_HIGH;
1177         unsigned long capacity;
1178
1179         if (host->dev.dma_mask && *(host->dev.dma_mask))
1180                 limit = *(host->dev.dma_mask);
1181
1182         for (rc = 0; msb->attr_group.attrs[rc]; ++rc) {
1183                 s_attr = mspro_from_sysfs_attr(msb->attr_group.attrs[rc]);
1184
1185                 if (s_attr->id == MSPRO_BLOCK_ID_DEVINFO)
1186                         dev_info = s_attr->data;
1187                 else if (s_attr->id == MSPRO_BLOCK_ID_SYSINFO)
1188                         sys_info = s_attr->data;
1189         }
1190
1191         if (!dev_info || !sys_info)
1192                 return -ENODEV;
1193
1194         msb->cylinders = be16_to_cpu(dev_info->cylinders);
1195         msb->heads = be16_to_cpu(dev_info->heads);
1196         msb->sectors_per_track = be16_to_cpu(dev_info->sectors_per_track);
1197
1198         msb->page_size = be16_to_cpu(sys_info->unit_size);
1199
1200         if (!idr_pre_get(&mspro_block_disk_idr, GFP_KERNEL))
1201                 return -ENOMEM;
1202
1203         mutex_lock(&mspro_block_disk_lock);
1204         rc = idr_get_new(&mspro_block_disk_idr, card, &disk_id);
1205         mutex_unlock(&mspro_block_disk_lock);
1206
1207         if (rc)
1208                 return rc;
1209
1210         if ((disk_id << MSPRO_BLOCK_PART_SHIFT) > 255) {
1211                 rc = -ENOSPC;
1212                 goto out_release_id;
1213         }
1214
1215         msb->disk = alloc_disk(1 << MSPRO_BLOCK_PART_SHIFT);
1216         if (!msb->disk) {
1217                 rc = -ENOMEM;
1218                 goto out_release_id;
1219         }
1220
1221         msb->queue = blk_init_queue(mspro_block_submit_req, &msb->q_lock);
1222         if (!msb->queue) {
1223                 rc = -ENOMEM;
1224                 goto out_put_disk;
1225         }
1226
1227         msb->queue->queuedata = card;
1228         blk_queue_prep_rq(msb->queue, mspro_block_prepare_req);
1229
1230         blk_queue_bounce_limit(msb->queue, limit);
1231         blk_queue_max_sectors(msb->queue, MSPRO_BLOCK_MAX_PAGES);
1232         blk_queue_max_phys_segments(msb->queue, MSPRO_BLOCK_MAX_SEGS);
1233         blk_queue_max_hw_segments(msb->queue, MSPRO_BLOCK_MAX_SEGS);
1234         blk_queue_max_segment_size(msb->queue,
1235                                    MSPRO_BLOCK_MAX_PAGES * msb->page_size);
1236
1237         msb->disk->major = major;
1238         msb->disk->first_minor = disk_id << MSPRO_BLOCK_PART_SHIFT;
1239         msb->disk->fops = &ms_block_bdops;
1240         msb->usage_count = 1;
1241         msb->disk->private_data = msb;
1242         msb->disk->queue = msb->queue;
1243         msb->disk->driverfs_dev = &card->dev;
1244
1245         sprintf(msb->disk->disk_name, "mspblk%d", disk_id);
1246
1247         blk_queue_hardsect_size(msb->queue, msb->page_size);
1248
1249         capacity = be16_to_cpu(sys_info->user_block_count);
1250         capacity *= be16_to_cpu(sys_info->block_size);
1251         capacity *= msb->page_size >> 9;
1252         set_capacity(msb->disk, capacity);
1253         dev_dbg(&card->dev, "capacity set %ld\n", capacity);
1254
1255         add_disk(msb->disk);
1256         msb->active = 1;
1257         return 0;
1258
1259 out_put_disk:
1260         put_disk(msb->disk);
1261 out_release_id:
1262         mutex_lock(&mspro_block_disk_lock);
1263         idr_remove(&mspro_block_disk_idr, disk_id);
1264         mutex_unlock(&mspro_block_disk_lock);
1265         return rc;
1266 }
1267
1268 static void mspro_block_data_clear(struct mspro_block_data *msb)
1269 {
1270         int cnt;
1271         struct mspro_sys_attr *s_attr;
1272
1273         if (msb->attr_group.attrs) {
1274                 for (cnt = 0; msb->attr_group.attrs[cnt]; ++cnt) {
1275                         s_attr = mspro_from_sysfs_attr(msb->attr_group
1276                                                            .attrs[cnt]);
1277                         kfree(s_attr->data);
1278                         kfree(s_attr);
1279                 }
1280                 kfree(msb->attr_group.attrs);
1281         }
1282
1283         msb->card = NULL;
1284 }
1285
1286 static int mspro_block_check_card(struct memstick_dev *card)
1287 {
1288         struct mspro_block_data *msb = memstick_get_drvdata(card);
1289
1290         return (msb->active == 1);
1291 }
1292
1293 static int mspro_block_probe(struct memstick_dev *card)
1294 {
1295         struct mspro_block_data *msb;
1296         int rc = 0;
1297
1298         msb = kzalloc(sizeof(struct mspro_block_data), GFP_KERNEL);
1299         if (!msb)
1300                 return -ENOMEM;
1301         memstick_set_drvdata(card, msb);
1302         msb->card = card;
1303         spin_lock_init(&msb->q_lock);
1304
1305         rc = mspro_block_init_card(card);
1306
1307         if (rc)
1308                 goto out_free;
1309
1310         rc = sysfs_create_group(&card->dev.kobj, &msb->attr_group);
1311         if (rc)
1312                 goto out_free;
1313
1314         rc = mspro_block_init_disk(card);
1315         if (!rc) {
1316                 card->check = mspro_block_check_card;
1317                 card->stop = mspro_block_stop;
1318                 card->start = mspro_block_start;
1319                 return 0;
1320         }
1321
1322         sysfs_remove_group(&card->dev.kobj, &msb->attr_group);
1323 out_free:
1324         memstick_set_drvdata(card, NULL);
1325         mspro_block_data_clear(msb);
1326         kfree(msb);
1327         return rc;
1328 }
1329
1330 static void mspro_block_remove(struct memstick_dev *card)
1331 {
1332         struct mspro_block_data *msb = memstick_get_drvdata(card);
1333         unsigned long flags;
1334
1335         del_gendisk(msb->disk);
1336         dev_dbg(&card->dev, "mspro block remove\n");
1337         spin_lock_irqsave(&msb->q_lock, flags);
1338         msb->eject = 1;
1339         blk_start_queue(msb->queue);
1340         spin_unlock_irqrestore(&msb->q_lock, flags);
1341
1342         blk_cleanup_queue(msb->queue);
1343         msb->queue = NULL;
1344
1345         sysfs_remove_group(&card->dev.kobj, &msb->attr_group);
1346
1347         mutex_lock(&mspro_block_disk_lock);
1348         mspro_block_data_clear(msb);
1349         mutex_unlock(&mspro_block_disk_lock);
1350
1351         mspro_block_disk_release(msb->disk);
1352         memstick_set_drvdata(card, NULL);
1353 }
1354
1355 #ifdef CONFIG_PM
1356
1357 static int mspro_block_suspend(struct memstick_dev *card, pm_message_t state)
1358 {
1359         struct mspro_block_data *msb = memstick_get_drvdata(card);
1360         unsigned long flags;
1361
1362         spin_lock_irqsave(&msb->q_lock, flags);
1363         blk_stop_queue(msb->queue);
1364         msb->active = 0;
1365         spin_unlock_irqrestore(&msb->q_lock, flags);
1366
1367         return 0;
1368 }
1369
1370 static int mspro_block_resume(struct memstick_dev *card)
1371 {
1372         struct mspro_block_data *msb = memstick_get_drvdata(card);
1373         unsigned long flags;
1374         int rc = 0;
1375
1376 #ifdef CONFIG_MEMSTICK_UNSAFE_RESUME
1377
1378         struct mspro_block_data *new_msb;
1379         struct memstick_host *host = card->host;
1380         struct mspro_sys_attr *s_attr, *r_attr;
1381         unsigned char cnt;
1382
1383         mutex_lock(&host->lock);
1384         new_msb = kzalloc(sizeof(struct mspro_block_data), GFP_KERNEL);
1385         if (!new_msb) {
1386                 rc = -ENOMEM;
1387                 goto out_unlock;
1388         }
1389
1390         new_msb->card = card;
1391         memstick_set_drvdata(card, new_msb);
1392         if (mspro_block_init_card(card))
1393                 goto out_free;
1394
1395         for (cnt = 0; new_msb->attr_group.attrs[cnt]
1396                       && msb->attr_group.attrs[cnt]; ++cnt) {
1397                 s_attr = mspro_from_sysfs_attr(new_msb->attr_group.attrs[cnt]);
1398                 r_attr = mspro_from_sysfs_attr(msb->attr_group.attrs[cnt]);
1399
1400                 if (s_attr->id == MSPRO_BLOCK_ID_SYSINFO
1401                     && r_attr->id == s_attr->id) {
1402                         if (memcmp(s_attr->data, r_attr->data, s_attr->size))
1403                                 break;
1404
1405                         msb->active = 1;
1406                         break;
1407                 }
1408         }
1409
1410 out_free:
1411         memstick_set_drvdata(card, msb);
1412         mspro_block_data_clear(new_msb);
1413         kfree(new_msb);
1414 out_unlock:
1415         mutex_unlock(&host->lock);
1416
1417 #endif /* CONFIG_MEMSTICK_UNSAFE_RESUME */
1418
1419         spin_lock_irqsave(&msb->q_lock, flags);
1420         blk_start_queue(msb->queue);
1421         spin_unlock_irqrestore(&msb->q_lock, flags);
1422         return rc;
1423 }
1424
1425 #else
1426
1427 #define mspro_block_suspend NULL
1428 #define mspro_block_resume NULL
1429
1430 #endif /* CONFIG_PM */
1431
1432 static struct memstick_device_id mspro_block_id_tbl[] = {
1433         {MEMSTICK_MATCH_ALL, MEMSTICK_TYPE_PRO, MEMSTICK_CATEGORY_STORAGE_DUO,
1434          MEMSTICK_CLASS_DUO},
1435         {}
1436 };
1437
1438
1439 static struct memstick_driver mspro_block_driver = {
1440         .driver = {
1441                 .name  = DRIVER_NAME,
1442                 .owner = THIS_MODULE
1443         },
1444         .id_table = mspro_block_id_tbl,
1445         .probe    = mspro_block_probe,
1446         .remove   = mspro_block_remove,
1447         .suspend  = mspro_block_suspend,
1448         .resume   = mspro_block_resume
1449 };
1450
1451 static int __init mspro_block_init(void)
1452 {
1453         int rc = -ENOMEM;
1454
1455         rc = register_blkdev(major, DRIVER_NAME);
1456         if (rc < 0) {
1457                 printk(KERN_ERR DRIVER_NAME ": failed to register "
1458                        "major %d, error %d\n", major, rc);
1459                 return rc;
1460         }
1461         if (!major)
1462                 major = rc;
1463
1464         rc = memstick_register_driver(&mspro_block_driver);
1465         if (rc)
1466                 unregister_blkdev(major, DRIVER_NAME);
1467         return rc;
1468 }
1469
1470 static void __exit mspro_block_exit(void)
1471 {
1472         memstick_unregister_driver(&mspro_block_driver);
1473         unregister_blkdev(major, DRIVER_NAME);
1474         idr_destroy(&mspro_block_disk_idr);
1475 }
1476
1477 module_init(mspro_block_init);
1478 module_exit(mspro_block_exit);
1479
1480 MODULE_LICENSE("GPL");
1481 MODULE_AUTHOR("Alex Dubov");
1482 MODULE_DESCRIPTION("Sony MemoryStickPro block device driver");
1483 MODULE_DEVICE_TABLE(memstick, mspro_block_id_tbl);