Merge git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[wrt350n-kernel.git] / drivers / memstick / core / mspro_block.c
blob00ba289a561bbce9ff76353dfea66906fff36368
1 /*
2 * Sony MemoryStick Pro storage support
4 * Copyright (C) 2007 Alex Dubov <oakad@yahoo.com>
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.
10 * Special thanks to Carlos Corbacho for providing various MemoryStick cards
11 * that made this driver possible.
15 #include <linux/blkdev.h>
16 #include <linux/idr.h>
17 #include <linux/hdreg.h>
18 #include <linux/kthread.h>
19 <<<<<<< HEAD:drivers/memstick/core/mspro_block.c
20 =======
21 #include <linux/delay.h>
22 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/memstick/core/mspro_block.c
23 #include <linux/memstick.h>
25 #define DRIVER_NAME "mspro_block"
26 <<<<<<< HEAD:drivers/memstick/core/mspro_block.c
27 #define DRIVER_VERSION "0.2"
28 =======
29 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/memstick/core/mspro_block.c
31 static int major;
32 module_param(major, int, 0644);
34 #define MSPRO_BLOCK_MAX_SEGS 32
35 #define MSPRO_BLOCK_MAX_PAGES ((2 << 16) - 1)
37 #define MSPRO_BLOCK_SIGNATURE 0xa5c3
38 #define MSPRO_BLOCK_MAX_ATTRIBUTES 41
40 enum {
41 MSPRO_BLOCK_ID_SYSINFO = 0x10,
42 MSPRO_BLOCK_ID_MODELNAME = 0x15,
43 MSPRO_BLOCK_ID_MBR = 0x20,
44 MSPRO_BLOCK_ID_PBR16 = 0x21,
45 MSPRO_BLOCK_ID_PBR32 = 0x22,
46 MSPRO_BLOCK_ID_SPECFILEVALUES1 = 0x25,
47 MSPRO_BLOCK_ID_SPECFILEVALUES2 = 0x26,
48 MSPRO_BLOCK_ID_DEVINFO = 0x30
51 struct mspro_sys_attr {
52 size_t size;
53 void *data;
54 unsigned char id;
55 char name[32];
56 struct device_attribute dev_attr;
59 struct mspro_attr_entry {
60 unsigned int address;
61 unsigned int size;
62 unsigned char id;
63 unsigned char reserved[3];
64 } __attribute__((packed));
66 struct mspro_attribute {
67 unsigned short signature;
68 unsigned short version;
69 unsigned char count;
70 unsigned char reserved[11];
71 struct mspro_attr_entry entries[];
72 } __attribute__((packed));
74 struct mspro_sys_info {
75 unsigned char class;
76 unsigned char reserved0;
77 unsigned short block_size;
78 unsigned short block_count;
79 unsigned short user_block_count;
80 unsigned short page_size;
81 unsigned char reserved1[2];
82 unsigned char assembly_date[8];
83 unsigned int serial_number;
84 unsigned char assembly_maker_code;
85 unsigned char assembly_model_code[3];
86 unsigned short memory_maker_code;
87 unsigned short memory_model_code;
88 unsigned char reserved2[4];
89 unsigned char vcc;
90 unsigned char vpp;
91 unsigned short controller_number;
92 unsigned short controller_function;
93 unsigned short start_sector;
94 unsigned short unit_size;
95 unsigned char ms_sub_class;
96 unsigned char reserved3[4];
97 unsigned char interface_type;
98 unsigned short controller_code;
99 unsigned char format_type;
100 unsigned char reserved4;
101 unsigned char device_type;
102 unsigned char reserved5[7];
103 unsigned char mspro_id[16];
104 unsigned char reserved6[16];
105 } __attribute__((packed));
107 struct mspro_mbr {
108 unsigned char boot_partition;
109 unsigned char start_head;
110 unsigned char start_sector;
111 unsigned char start_cylinder;
112 unsigned char partition_type;
113 unsigned char end_head;
114 unsigned char end_sector;
115 unsigned char end_cylinder;
116 unsigned int start_sectors;
117 unsigned int sectors_per_partition;
118 } __attribute__((packed));
120 <<<<<<< HEAD:drivers/memstick/core/mspro_block.c
121 =======
122 struct mspro_specfile {
123 char name[8];
124 char ext[3];
125 unsigned char attr;
126 unsigned char reserved[10];
127 unsigned short time;
128 unsigned short date;
129 unsigned short cluster;
130 unsigned int size;
131 } __attribute__((packed));
133 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/memstick/core/mspro_block.c
134 struct mspro_devinfo {
135 unsigned short cylinders;
136 unsigned short heads;
137 unsigned short bytes_per_track;
138 unsigned short bytes_per_sector;
139 unsigned short sectors_per_track;
140 unsigned char reserved[6];
141 } __attribute__((packed));
143 struct mspro_block_data {
144 struct memstick_dev *card;
145 unsigned int usage_count;
146 struct gendisk *disk;
147 struct request_queue *queue;
148 spinlock_t q_lock;
149 wait_queue_head_t q_wait;
150 struct task_struct *q_thread;
152 unsigned short page_size;
153 unsigned short cylinders;
154 unsigned short heads;
155 unsigned short sectors_per_track;
157 unsigned char system;
158 unsigned char read_only:1,
159 active:1,
160 has_request:1,
161 data_dir:1;
162 unsigned char transfer_cmd;
164 int (*mrq_handler)(struct memstick_dev *card,
165 struct memstick_request **mrq);
167 struct attribute_group attr_group;
169 struct scatterlist req_sg[MSPRO_BLOCK_MAX_SEGS];
170 unsigned int seg_count;
171 unsigned int current_seg;
172 unsigned short current_page;
175 static DEFINE_IDR(mspro_block_disk_idr);
176 static DEFINE_MUTEX(mspro_block_disk_lock);
178 /*** Block device ***/
180 static int mspro_block_bd_open(struct inode *inode, struct file *filp)
182 struct gendisk *disk = inode->i_bdev->bd_disk;
183 struct mspro_block_data *msb = disk->private_data;
184 int rc = -ENXIO;
186 mutex_lock(&mspro_block_disk_lock);
188 if (msb && msb->card) {
189 msb->usage_count++;
190 if ((filp->f_mode & FMODE_WRITE) && msb->read_only)
191 rc = -EROFS;
192 else
193 rc = 0;
196 mutex_unlock(&mspro_block_disk_lock);
198 return rc;
202 static int mspro_block_disk_release(struct gendisk *disk)
204 struct mspro_block_data *msb = disk->private_data;
205 int disk_id = disk->first_minor >> MEMSTICK_PART_SHIFT;
207 mutex_lock(&mspro_block_disk_lock);
209 if (msb->usage_count) {
210 msb->usage_count--;
211 if (!msb->usage_count) {
212 kfree(msb);
213 disk->private_data = NULL;
214 idr_remove(&mspro_block_disk_idr, disk_id);
215 put_disk(disk);
219 mutex_unlock(&mspro_block_disk_lock);
221 return 0;
224 static int mspro_block_bd_release(struct inode *inode, struct file *filp)
226 struct gendisk *disk = inode->i_bdev->bd_disk;
227 return mspro_block_disk_release(disk);
230 static int mspro_block_bd_getgeo(struct block_device *bdev,
231 struct hd_geometry *geo)
233 struct mspro_block_data *msb = bdev->bd_disk->private_data;
235 geo->heads = msb->heads;
236 geo->sectors = msb->sectors_per_track;
237 geo->cylinders = msb->cylinders;
239 return 0;
242 static struct block_device_operations ms_block_bdops = {
243 .open = mspro_block_bd_open,
244 .release = mspro_block_bd_release,
245 .getgeo = mspro_block_bd_getgeo,
246 .owner = THIS_MODULE
249 /*** Information ***/
251 static struct mspro_sys_attr *mspro_from_sysfs_attr(struct attribute *attr)
253 struct device_attribute *dev_attr
254 = container_of(attr, struct device_attribute, attr);
255 return container_of(dev_attr, struct mspro_sys_attr, dev_attr);
258 static const char *mspro_block_attr_name(unsigned char tag)
260 switch (tag) {
261 case MSPRO_BLOCK_ID_SYSINFO:
262 return "attr_sysinfo";
263 case MSPRO_BLOCK_ID_MODELNAME:
264 return "attr_modelname";
265 case MSPRO_BLOCK_ID_MBR:
266 return "attr_mbr";
267 case MSPRO_BLOCK_ID_PBR16:
268 return "attr_pbr16";
269 case MSPRO_BLOCK_ID_PBR32:
270 return "attr_pbr32";
271 case MSPRO_BLOCK_ID_SPECFILEVALUES1:
272 return "attr_specfilevalues1";
273 case MSPRO_BLOCK_ID_SPECFILEVALUES2:
274 return "attr_specfilevalues2";
275 case MSPRO_BLOCK_ID_DEVINFO:
276 return "attr_devinfo";
277 default:
278 return NULL;
282 typedef ssize_t (*sysfs_show_t)(struct device *dev,
283 struct device_attribute *attr,
284 char *buffer);
286 static ssize_t mspro_block_attr_show_default(struct device *dev,
287 struct device_attribute *attr,
288 char *buffer)
290 struct mspro_sys_attr *s_attr = container_of(attr,
291 struct mspro_sys_attr,
292 dev_attr);
294 ssize_t cnt, rc = 0;
296 for (cnt = 0; cnt < s_attr->size; cnt++) {
297 if (cnt && !(cnt % 16)) {
298 if (PAGE_SIZE - rc)
299 buffer[rc++] = '\n';
302 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "%02x ",
303 ((unsigned char *)s_attr->data)[cnt]);
305 return rc;
308 static ssize_t mspro_block_attr_show_sysinfo(struct device *dev,
309 struct device_attribute *attr,
310 char *buffer)
312 struct mspro_sys_attr *x_attr = container_of(attr,
313 struct mspro_sys_attr,
314 dev_attr);
315 struct mspro_sys_info *x_sys = x_attr->data;
316 ssize_t rc = 0;
317 <<<<<<< HEAD:drivers/memstick/core/mspro_block.c
318 =======
319 int date_tz = 0, date_tz_f = 0;
321 if (x_sys->assembly_date[0] > 0x80U) {
322 date_tz = (~x_sys->assembly_date[0]) + 1;
323 date_tz_f = date_tz & 3;
324 date_tz >>= 2;
325 date_tz = -date_tz;
326 date_tz_f *= 15;
327 } else if (x_sys->assembly_date[0] < 0x80U) {
328 date_tz = x_sys->assembly_date[0];
329 date_tz_f = date_tz & 3;
330 date_tz >>= 2;
331 date_tz_f *= 15;
333 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/memstick/core/mspro_block.c
335 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "class: %x\n",
336 x_sys->class);
337 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "block size: %x\n",
338 be16_to_cpu(x_sys->block_size));
339 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "block count: %x\n",
340 be16_to_cpu(x_sys->block_count));
341 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "user block count: %x\n",
342 be16_to_cpu(x_sys->user_block_count));
343 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "page size: %x\n",
344 be16_to_cpu(x_sys->page_size));
345 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "assembly date: "
346 <<<<<<< HEAD:drivers/memstick/core/mspro_block.c
347 "%d %04u-%02u-%02u %02u:%02u:%02u\n",
348 x_sys->assembly_date[0],
349 =======
350 "GMT%+d:%d %04u-%02u-%02u %02u:%02u:%02u\n",
351 date_tz, date_tz_f,
352 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/memstick/core/mspro_block.c
353 be16_to_cpu(*(unsigned short *)
354 &x_sys->assembly_date[1]),
355 x_sys->assembly_date[3], x_sys->assembly_date[4],
356 x_sys->assembly_date[5], x_sys->assembly_date[6],
357 x_sys->assembly_date[7]);
358 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "serial number: %x\n",
359 be32_to_cpu(x_sys->serial_number));
360 rc += scnprintf(buffer + rc, PAGE_SIZE - rc,
361 "assembly maker code: %x\n",
362 x_sys->assembly_maker_code);
363 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "assembly model code: "
364 "%02x%02x%02x\n", x_sys->assembly_model_code[0],
365 x_sys->assembly_model_code[1],
366 x_sys->assembly_model_code[2]);
367 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "memory maker code: %x\n",
368 be16_to_cpu(x_sys->memory_maker_code));
369 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "memory model code: %x\n",
370 be16_to_cpu(x_sys->memory_model_code));
371 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "vcc: %x\n",
372 x_sys->vcc);
373 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "vpp: %x\n",
374 x_sys->vpp);
375 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "controller number: %x\n",
376 be16_to_cpu(x_sys->controller_number));
377 rc += scnprintf(buffer + rc, PAGE_SIZE - rc,
378 "controller function: %x\n",
379 be16_to_cpu(x_sys->controller_function));
380 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "start sector: %x\n",
381 be16_to_cpu(x_sys->start_sector));
382 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "unit size: %x\n",
383 be16_to_cpu(x_sys->unit_size));
384 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "sub class: %x\n",
385 x_sys->ms_sub_class);
386 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "interface type: %x\n",
387 x_sys->interface_type);
388 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "controller code: %x\n",
389 be16_to_cpu(x_sys->controller_code));
390 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "format type: %x\n",
391 x_sys->format_type);
392 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "device type: %x\n",
393 x_sys->device_type);
394 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "mspro id: %s\n",
395 x_sys->mspro_id);
396 return rc;
399 static ssize_t mspro_block_attr_show_modelname(struct device *dev,
400 struct device_attribute *attr,
401 char *buffer)
403 struct mspro_sys_attr *s_attr = container_of(attr,
404 struct mspro_sys_attr,
405 dev_attr);
407 return scnprintf(buffer, PAGE_SIZE, "%s", (char *)s_attr->data);
410 static ssize_t mspro_block_attr_show_mbr(struct device *dev,
411 struct device_attribute *attr,
412 char *buffer)
414 struct mspro_sys_attr *x_attr = container_of(attr,
415 struct mspro_sys_attr,
416 dev_attr);
417 struct mspro_mbr *x_mbr = x_attr->data;
418 ssize_t rc = 0;
420 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "boot partition: %x\n",
421 x_mbr->boot_partition);
422 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "start head: %x\n",
423 x_mbr->start_head);
424 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "start sector: %x\n",
425 x_mbr->start_sector);
426 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "start cylinder: %x\n",
427 x_mbr->start_cylinder);
428 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "partition type: %x\n",
429 x_mbr->partition_type);
430 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "end head: %x\n",
431 x_mbr->end_head);
432 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "end sector: %x\n",
433 x_mbr->end_sector);
434 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "end cylinder: %x\n",
435 x_mbr->end_cylinder);
436 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "start sectors: %x\n",
437 x_mbr->start_sectors);
438 rc += scnprintf(buffer + rc, PAGE_SIZE - rc,
439 "sectors per partition: %x\n",
440 x_mbr->sectors_per_partition);
441 return rc;
444 <<<<<<< HEAD:drivers/memstick/core/mspro_block.c
445 =======
446 static ssize_t mspro_block_attr_show_specfile(struct device *dev,
447 struct device_attribute *attr,
448 char *buffer)
450 struct mspro_sys_attr *x_attr = container_of(attr,
451 struct mspro_sys_attr,
452 dev_attr);
453 struct mspro_specfile *x_spfile = x_attr->data;
454 char name[9], ext[4];
455 ssize_t rc = 0;
457 memcpy(name, x_spfile->name, 8);
458 name[8] = 0;
459 memcpy(ext, x_spfile->ext, 3);
460 ext[3] = 0;
462 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "name: %s\n", name);
463 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "ext: %s\n", ext);
464 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "attribute: %x\n",
465 x_spfile->attr);
466 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "time: %d:%d:%d\n",
467 x_spfile->time >> 11,
468 (x_spfile->time >> 5) & 0x3f,
469 (x_spfile->time & 0x1f) * 2);
470 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "date: %d-%d-%d\n",
471 (x_spfile->date >> 9) + 1980,
472 (x_spfile->date >> 5) & 0xf,
473 x_spfile->date & 0x1f);
474 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "start cluster: %x\n",
475 x_spfile->cluster);
476 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "size: %x\n",
477 x_spfile->size);
478 return rc;
481 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/memstick/core/mspro_block.c
482 static ssize_t mspro_block_attr_show_devinfo(struct device *dev,
483 struct device_attribute *attr,
484 char *buffer)
486 struct mspro_sys_attr *x_attr = container_of(attr,
487 struct mspro_sys_attr,
488 dev_attr);
489 struct mspro_devinfo *x_devinfo = x_attr->data;
490 ssize_t rc = 0;
492 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "cylinders: %x\n",
493 be16_to_cpu(x_devinfo->cylinders));
494 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "heads: %x\n",
495 be16_to_cpu(x_devinfo->heads));
496 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "bytes per track: %x\n",
497 be16_to_cpu(x_devinfo->bytes_per_track));
498 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "bytes per sector: %x\n",
499 be16_to_cpu(x_devinfo->bytes_per_sector));
500 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "sectors per track: %x\n",
501 be16_to_cpu(x_devinfo->sectors_per_track));
502 return rc;
505 static sysfs_show_t mspro_block_attr_show(unsigned char tag)
507 switch (tag) {
508 case MSPRO_BLOCK_ID_SYSINFO:
509 return mspro_block_attr_show_sysinfo;
510 case MSPRO_BLOCK_ID_MODELNAME:
511 return mspro_block_attr_show_modelname;
512 case MSPRO_BLOCK_ID_MBR:
513 return mspro_block_attr_show_mbr;
514 <<<<<<< HEAD:drivers/memstick/core/mspro_block.c
515 =======
516 case MSPRO_BLOCK_ID_SPECFILEVALUES1:
517 case MSPRO_BLOCK_ID_SPECFILEVALUES2:
518 return mspro_block_attr_show_specfile;
519 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/memstick/core/mspro_block.c
520 case MSPRO_BLOCK_ID_DEVINFO:
521 return mspro_block_attr_show_devinfo;
522 default:
523 return mspro_block_attr_show_default;
527 /*** Protocol handlers ***/
530 * Functions prefixed with "h_" are protocol callbacks. They can be called from
531 * interrupt context. Return value of 0 means that request processing is still
532 * ongoing, while special error value of -EAGAIN means that current request is
533 * finished (and request processor should come back some time later).
536 static int h_mspro_block_req_init(struct memstick_dev *card,
537 struct memstick_request **mrq)
539 struct mspro_block_data *msb = memstick_get_drvdata(card);
541 *mrq = &card->current_mrq;
542 card->next_request = msb->mrq_handler;
543 return 0;
546 static int h_mspro_block_default(struct memstick_dev *card,
547 struct memstick_request **mrq)
549 complete(&card->mrq_complete);
550 if (!(*mrq)->error)
551 return -EAGAIN;
552 else
553 return (*mrq)->error;
556 static int h_mspro_block_get_ro(struct memstick_dev *card,
557 struct memstick_request **mrq)
559 struct mspro_block_data *msb = memstick_get_drvdata(card);
561 if ((*mrq)->error) {
562 complete(&card->mrq_complete);
563 return (*mrq)->error;
566 if ((*mrq)->data[offsetof(struct ms_status_register, status0)]
567 & MEMSTICK_STATUS0_WP)
568 msb->read_only = 1;
569 else
570 msb->read_only = 0;
572 complete(&card->mrq_complete);
573 return -EAGAIN;
576 static int h_mspro_block_wait_for_ced(struct memstick_dev *card,
577 struct memstick_request **mrq)
579 if ((*mrq)->error) {
580 complete(&card->mrq_complete);
581 return (*mrq)->error;
584 dev_dbg(&card->dev, "wait for ced: value %x\n", (*mrq)->data[0]);
586 if ((*mrq)->data[0] & (MEMSTICK_INT_CMDNAK | MEMSTICK_INT_ERR)) {
587 card->current_mrq.error = -EFAULT;
588 complete(&card->mrq_complete);
589 return card->current_mrq.error;
592 if (!((*mrq)->data[0] & MEMSTICK_INT_CED))
593 return 0;
594 else {
595 card->current_mrq.error = 0;
596 complete(&card->mrq_complete);
597 return -EAGAIN;
601 static int h_mspro_block_transfer_data(struct memstick_dev *card,
602 struct memstick_request **mrq)
604 struct memstick_host *host = card->host;
605 struct mspro_block_data *msb = memstick_get_drvdata(card);
606 unsigned char t_val = 0;
607 struct scatterlist t_sg = { 0 };
608 size_t t_offset;
610 if ((*mrq)->error) {
611 complete(&card->mrq_complete);
612 return (*mrq)->error;
615 switch ((*mrq)->tpc) {
616 case MS_TPC_WRITE_REG:
617 memstick_init_req(*mrq, MS_TPC_SET_CMD, &msb->transfer_cmd, 1);
618 (*mrq)->get_int_reg = 1;
619 return 0;
620 case MS_TPC_SET_CMD:
621 t_val = (*mrq)->int_reg;
622 memstick_init_req(*mrq, MS_TPC_GET_INT, NULL, 1);
623 if (host->caps & MEMSTICK_CAP_AUTO_GET_INT)
624 goto has_int_reg;
625 return 0;
626 case MS_TPC_GET_INT:
627 t_val = (*mrq)->data[0];
628 has_int_reg:
629 if (t_val & (MEMSTICK_INT_CMDNAK | MEMSTICK_INT_ERR)) {
630 t_val = MSPRO_CMD_STOP;
631 memstick_init_req(*mrq, MS_TPC_SET_CMD, &t_val, 1);
632 card->next_request = h_mspro_block_default;
633 return 0;
636 if (msb->current_page
637 == (msb->req_sg[msb->current_seg].length
638 / msb->page_size)) {
639 msb->current_page = 0;
640 msb->current_seg++;
642 if (msb->current_seg == msb->seg_count) {
643 if (t_val & MEMSTICK_INT_CED) {
644 complete(&card->mrq_complete);
645 return -EAGAIN;
646 } else {
647 card->next_request
648 = h_mspro_block_wait_for_ced;
649 memstick_init_req(*mrq, MS_TPC_GET_INT,
650 NULL, 1);
651 return 0;
656 if (!(t_val & MEMSTICK_INT_BREQ)) {
657 memstick_init_req(*mrq, MS_TPC_GET_INT, NULL, 1);
658 return 0;
661 t_offset = msb->req_sg[msb->current_seg].offset;
662 t_offset += msb->current_page * msb->page_size;
664 sg_set_page(&t_sg,
665 nth_page(sg_page(&(msb->req_sg[msb->current_seg])),
666 t_offset >> PAGE_SHIFT),
667 msb->page_size, offset_in_page(t_offset));
669 memstick_init_req_sg(*mrq, msb->data_dir == READ
670 ? MS_TPC_READ_LONG_DATA
671 : MS_TPC_WRITE_LONG_DATA,
672 &t_sg);
673 (*mrq)->get_int_reg = 1;
674 return 0;
675 case MS_TPC_READ_LONG_DATA:
676 case MS_TPC_WRITE_LONG_DATA:
677 msb->current_page++;
678 if (host->caps & MEMSTICK_CAP_AUTO_GET_INT) {
679 t_val = (*mrq)->int_reg;
680 goto has_int_reg;
681 } else {
682 memstick_init_req(*mrq, MS_TPC_GET_INT, NULL, 1);
683 return 0;
686 default:
687 BUG();
691 /*** Data transfer ***/
693 static void mspro_block_process_request(struct memstick_dev *card,
694 struct request *req)
696 struct mspro_block_data *msb = memstick_get_drvdata(card);
697 struct mspro_param_register param;
698 int rc, chunk, cnt;
699 unsigned short page_count;
700 sector_t t_sec;
701 unsigned long flags;
703 do {
704 page_count = 0;
705 msb->current_seg = 0;
706 msb->seg_count = blk_rq_map_sg(req->q, req, msb->req_sg);
708 if (msb->seg_count) {
709 msb->current_page = 0;
710 for (rc = 0; rc < msb->seg_count; rc++)
711 page_count += msb->req_sg[rc].length
712 / msb->page_size;
714 t_sec = req->sector;
715 sector_div(t_sec, msb->page_size >> 9);
716 param.system = msb->system;
717 param.data_count = cpu_to_be16(page_count);
718 param.data_address = cpu_to_be32((uint32_t)t_sec);
719 <<<<<<< HEAD:drivers/memstick/core/mspro_block.c
720 param.cmd_param = 0;
721 =======
722 param.tpc_param = 0;
723 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/memstick/core/mspro_block.c
725 msb->data_dir = rq_data_dir(req);
726 msb->transfer_cmd = msb->data_dir == READ
727 ? MSPRO_CMD_READ_DATA
728 : MSPRO_CMD_WRITE_DATA;
730 dev_dbg(&card->dev, "data transfer: cmd %x, "
731 "lba %x, count %x\n", msb->transfer_cmd,
732 be32_to_cpu(param.data_address),
733 page_count);
735 card->next_request = h_mspro_block_req_init;
736 msb->mrq_handler = h_mspro_block_transfer_data;
737 memstick_init_req(&card->current_mrq, MS_TPC_WRITE_REG,
738 &param, sizeof(param));
739 memstick_new_req(card->host);
740 wait_for_completion(&card->mrq_complete);
741 rc = card->current_mrq.error;
743 if (rc || (card->current_mrq.tpc == MSPRO_CMD_STOP)) {
744 for (cnt = 0; cnt < msb->current_seg; cnt++)
745 page_count += msb->req_sg[cnt].length
746 / msb->page_size;
748 if (msb->current_page)
749 page_count += msb->current_page - 1;
751 if (page_count && (msb->data_dir == READ))
752 rc = msb->page_size * page_count;
753 else
754 rc = -EIO;
755 } else
756 rc = msb->page_size * page_count;
757 } else
758 rc = -EFAULT;
760 spin_lock_irqsave(&msb->q_lock, flags);
761 if (rc >= 0)
762 chunk = __blk_end_request(req, 0, rc);
763 else
764 chunk = __blk_end_request(req, rc, 0);
766 dev_dbg(&card->dev, "end chunk %d, %d\n", rc, chunk);
767 spin_unlock_irqrestore(&msb->q_lock, flags);
768 } while (chunk);
771 static int mspro_block_has_request(struct mspro_block_data *msb)
773 int rc = 0;
774 unsigned long flags;
776 spin_lock_irqsave(&msb->q_lock, flags);
777 if (kthread_should_stop() || msb->has_request)
778 rc = 1;
779 spin_unlock_irqrestore(&msb->q_lock, flags);
780 return rc;
783 static int mspro_block_queue_thread(void *data)
785 struct memstick_dev *card = data;
786 struct memstick_host *host = card->host;
787 struct mspro_block_data *msb = memstick_get_drvdata(card);
788 struct request *req;
789 unsigned long flags;
791 while (1) {
792 wait_event(msb->q_wait, mspro_block_has_request(msb));
793 dev_dbg(&card->dev, "thread iter\n");
795 spin_lock_irqsave(&msb->q_lock, flags);
796 req = elv_next_request(msb->queue);
797 dev_dbg(&card->dev, "next req %p\n", req);
798 if (!req) {
799 msb->has_request = 0;
800 if (kthread_should_stop()) {
801 spin_unlock_irqrestore(&msb->q_lock, flags);
802 break;
804 } else
805 msb->has_request = 1;
806 spin_unlock_irqrestore(&msb->q_lock, flags);
808 if (req) {
809 mutex_lock(&host->lock);
810 mspro_block_process_request(card, req);
811 mutex_unlock(&host->lock);
814 dev_dbg(&card->dev, "thread finished\n");
815 return 0;
818 static void mspro_block_request(struct request_queue *q)
820 struct memstick_dev *card = q->queuedata;
821 struct mspro_block_data *msb = memstick_get_drvdata(card);
822 struct request *req = NULL;
824 if (msb->q_thread) {
825 msb->has_request = 1;
826 wake_up_all(&msb->q_wait);
827 } else {
828 while ((req = elv_next_request(q)) != NULL)
829 end_queued_request(req, -ENODEV);
833 /*** Initialization ***/
835 static int mspro_block_wait_for_ced(struct memstick_dev *card)
837 struct mspro_block_data *msb = memstick_get_drvdata(card);
839 card->next_request = h_mspro_block_req_init;
840 msb->mrq_handler = h_mspro_block_wait_for_ced;
841 memstick_init_req(&card->current_mrq, MS_TPC_GET_INT, NULL, 1);
842 memstick_new_req(card->host);
843 wait_for_completion(&card->mrq_complete);
844 return card->current_mrq.error;
847 static int mspro_block_switch_to_parallel(struct memstick_dev *card)
849 struct memstick_host *host = card->host;
850 struct mspro_block_data *msb = memstick_get_drvdata(card);
851 struct mspro_param_register param = {
852 <<<<<<< HEAD:drivers/memstick/core/mspro_block.c
853 .system = 0,
854 =======
855 .system = MEMSTICK_SYS_PAR4,
856 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/memstick/core/mspro_block.c
857 .data_count = 0,
858 .data_address = 0,
859 <<<<<<< HEAD:drivers/memstick/core/mspro_block.c
860 .cmd_param = 0
861 =======
862 .tpc_param = 0
863 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/memstick/core/mspro_block.c
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 if (card->current_mrq.error)
873 return card->current_mrq.error;
875 <<<<<<< HEAD:drivers/memstick/core/mspro_block.c
876 msb->system = 0;
877 host->set_param(host, MEMSTICK_INTERFACE, MEMSTICK_PARALLEL);
878 =======
879 msb->system = MEMSTICK_SYS_PAR4;
880 host->set_param(host, MEMSTICK_INTERFACE, MEMSTICK_PAR4);
881 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/memstick/core/mspro_block.c
883 card->next_request = h_mspro_block_req_init;
884 msb->mrq_handler = h_mspro_block_default;
885 memstick_init_req(&card->current_mrq, MS_TPC_GET_INT, NULL, 1);
886 memstick_new_req(card->host);
887 wait_for_completion(&card->mrq_complete);
889 if (card->current_mrq.error) {
890 <<<<<<< HEAD:drivers/memstick/core/mspro_block.c
891 msb->system = 0x80;
892 =======
893 msb->system = MEMSTICK_SYS_SERIAL;
894 host->set_param(host, MEMSTICK_POWER, MEMSTICK_POWER_OFF);
895 msleep(1000);
896 host->set_param(host, MEMSTICK_POWER, MEMSTICK_POWER_ON);
897 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/memstick/core/mspro_block.c
898 host->set_param(host, MEMSTICK_INTERFACE, MEMSTICK_SERIAL);
899 <<<<<<< HEAD:drivers/memstick/core/mspro_block.c
900 =======
902 if (memstick_set_rw_addr(card))
903 return card->current_mrq.error;
905 param.system = msb->system;
907 card->next_request = h_mspro_block_req_init;
908 msb->mrq_handler = h_mspro_block_default;
909 memstick_init_req(&card->current_mrq, MS_TPC_WRITE_REG, &param,
910 sizeof(param));
911 memstick_new_req(host);
912 wait_for_completion(&card->mrq_complete);
914 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/memstick/core/mspro_block.c
915 return -EFAULT;
918 return 0;
921 /* Memory allocated for attributes by this function should be freed by
922 * mspro_block_data_clear, no matter if the initialization process succeded
923 * or failed.
925 static int mspro_block_read_attributes(struct memstick_dev *card)
927 struct mspro_block_data *msb = memstick_get_drvdata(card);
928 struct mspro_param_register param = {
929 .system = msb->system,
930 .data_count = cpu_to_be16(1),
931 .data_address = 0,
932 <<<<<<< HEAD:drivers/memstick/core/mspro_block.c
933 .cmd_param = 0
934 =======
935 .tpc_param = 0
936 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/memstick/core/mspro_block.c
938 struct mspro_attribute *attr = NULL;
939 struct mspro_sys_attr *s_attr = NULL;
940 unsigned char *buffer = NULL;
941 int cnt, rc, attr_count;
942 unsigned int addr;
943 unsigned short page_count;
945 attr = kmalloc(msb->page_size, GFP_KERNEL);
946 if (!attr)
947 return -ENOMEM;
949 sg_init_one(&msb->req_sg[0], attr, msb->page_size);
950 msb->seg_count = 1;
951 msb->current_seg = 0;
952 msb->current_page = 0;
953 msb->data_dir = READ;
954 msb->transfer_cmd = MSPRO_CMD_READ_ATRB;
956 card->next_request = h_mspro_block_req_init;
957 msb->mrq_handler = h_mspro_block_transfer_data;
958 memstick_init_req(&card->current_mrq, MS_TPC_WRITE_REG, &param,
959 sizeof(param));
960 memstick_new_req(card->host);
961 wait_for_completion(&card->mrq_complete);
962 if (card->current_mrq.error) {
963 rc = card->current_mrq.error;
964 goto out_free_attr;
967 if (be16_to_cpu(attr->signature) != MSPRO_BLOCK_SIGNATURE) {
968 printk(KERN_ERR "%s: unrecognized device signature %x\n",
969 card->dev.bus_id, be16_to_cpu(attr->signature));
970 rc = -ENODEV;
971 goto out_free_attr;
974 if (attr->count > MSPRO_BLOCK_MAX_ATTRIBUTES) {
975 printk(KERN_WARNING "%s: way too many attribute entries\n",
976 card->dev.bus_id);
977 attr_count = MSPRO_BLOCK_MAX_ATTRIBUTES;
978 } else
979 attr_count = attr->count;
981 msb->attr_group.attrs = kzalloc((attr_count + 1)
982 * sizeof(struct attribute),
983 GFP_KERNEL);
984 if (!msb->attr_group.attrs) {
985 rc = -ENOMEM;
986 goto out_free_attr;
988 msb->attr_group.name = "media_attributes";
990 buffer = kmalloc(msb->page_size, GFP_KERNEL);
991 if (!buffer) {
992 rc = -ENOMEM;
993 goto out_free_attr;
995 memcpy(buffer, (char *)attr, msb->page_size);
996 page_count = 1;
998 for (cnt = 0; cnt < attr_count; ++cnt) {
999 s_attr = kzalloc(sizeof(struct mspro_sys_attr), GFP_KERNEL);
1000 if (!s_attr) {
1001 rc = -ENOMEM;
1002 goto out_free_buffer;
1005 msb->attr_group.attrs[cnt] = &s_attr->dev_attr.attr;
1006 addr = be32_to_cpu(attr->entries[cnt].address);
1007 rc = be32_to_cpu(attr->entries[cnt].size);
1008 dev_dbg(&card->dev, "adding attribute %d: id %x, address %x, "
1009 "size %x\n", cnt, attr->entries[cnt].id, addr, rc);
1010 s_attr->id = attr->entries[cnt].id;
1011 if (mspro_block_attr_name(s_attr->id))
1012 snprintf(s_attr->name, sizeof(s_attr->name), "%s",
1013 mspro_block_attr_name(attr->entries[cnt].id));
1014 else
1015 snprintf(s_attr->name, sizeof(s_attr->name),
1016 "attr_x%02x", attr->entries[cnt].id);
1018 s_attr->dev_attr.attr.name = s_attr->name;
1019 s_attr->dev_attr.attr.mode = S_IRUGO;
1020 s_attr->dev_attr.attr.owner = THIS_MODULE;
1021 s_attr->dev_attr.show = mspro_block_attr_show(s_attr->id);
1023 if (!rc)
1024 continue;
1026 s_attr->size = rc;
1027 s_attr->data = kmalloc(rc, GFP_KERNEL);
1028 if (!s_attr->data) {
1029 rc = -ENOMEM;
1030 goto out_free_buffer;
1033 if (((addr / msb->page_size)
1034 == be32_to_cpu(param.data_address))
1035 && (((addr + rc - 1) / msb->page_size)
1036 == be32_to_cpu(param.data_address))) {
1037 memcpy(s_attr->data, buffer + addr % msb->page_size,
1038 rc);
1039 continue;
1042 if (page_count <= (rc / msb->page_size)) {
1043 kfree(buffer);
1044 page_count = (rc / msb->page_size) + 1;
1045 buffer = kmalloc(page_count * msb->page_size,
1046 GFP_KERNEL);
1047 if (!buffer) {
1048 rc = -ENOMEM;
1049 goto out_free_attr;
1053 param.system = msb->system;
1054 param.data_count = cpu_to_be16((rc / msb->page_size) + 1);
1055 param.data_address = cpu_to_be32(addr / msb->page_size);
1056 <<<<<<< HEAD:drivers/memstick/core/mspro_block.c
1057 param.cmd_param = 0;
1058 =======
1059 param.tpc_param = 0;
1060 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/memstick/core/mspro_block.c
1062 sg_init_one(&msb->req_sg[0], buffer,
1063 be16_to_cpu(param.data_count) * msb->page_size);
1064 msb->seg_count = 1;
1065 msb->current_seg = 0;
1066 msb->current_page = 0;
1067 msb->data_dir = READ;
1068 msb->transfer_cmd = MSPRO_CMD_READ_ATRB;
1070 dev_dbg(&card->dev, "reading attribute pages %x, %x\n",
1071 be32_to_cpu(param.data_address),
1072 be16_to_cpu(param.data_count));
1074 card->next_request = h_mspro_block_req_init;
1075 msb->mrq_handler = h_mspro_block_transfer_data;
1076 memstick_init_req(&card->current_mrq, MS_TPC_WRITE_REG,
1077 (char *)&param, sizeof(param));
1078 memstick_new_req(card->host);
1079 wait_for_completion(&card->mrq_complete);
1080 if (card->current_mrq.error) {
1081 rc = card->current_mrq.error;
1082 goto out_free_buffer;
1085 memcpy(s_attr->data, buffer + addr % msb->page_size, rc);
1088 rc = 0;
1089 out_free_buffer:
1090 kfree(buffer);
1091 out_free_attr:
1092 kfree(attr);
1093 return rc;
1096 static int mspro_block_init_card(struct memstick_dev *card)
1098 struct mspro_block_data *msb = memstick_get_drvdata(card);
1099 struct memstick_host *host = card->host;
1100 int rc = 0;
1102 <<<<<<< HEAD:drivers/memstick/core/mspro_block.c
1103 msb->system = 0x80;
1104 =======
1105 msb->system = MEMSTICK_SYS_SERIAL;
1106 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/memstick/core/mspro_block.c
1107 card->reg_addr.r_offset = offsetof(struct mspro_register, status);
1108 card->reg_addr.r_length = sizeof(struct ms_status_register);
1109 card->reg_addr.w_offset = offsetof(struct mspro_register, param);
1110 card->reg_addr.w_length = sizeof(struct mspro_param_register);
1112 if (memstick_set_rw_addr(card))
1113 return -EIO;
1115 <<<<<<< HEAD:drivers/memstick/core/mspro_block.c
1116 if (host->caps & MEMSTICK_CAP_PARALLEL) {
1117 =======
1118 if (host->caps & MEMSTICK_CAP_PAR4) {
1119 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/memstick/core/mspro_block.c
1120 if (mspro_block_switch_to_parallel(card))
1121 printk(KERN_WARNING "%s: could not switch to "
1122 "parallel interface\n", card->dev.bus_id);
1125 rc = mspro_block_wait_for_ced(card);
1126 if (rc)
1127 return rc;
1128 dev_dbg(&card->dev, "card activated\n");
1130 card->next_request = h_mspro_block_req_init;
1131 msb->mrq_handler = h_mspro_block_get_ro;
1132 memstick_init_req(&card->current_mrq, MS_TPC_READ_REG, NULL,
1133 sizeof(struct ms_status_register));
1134 memstick_new_req(card->host);
1135 wait_for_completion(&card->mrq_complete);
1136 if (card->current_mrq.error)
1137 return card->current_mrq.error;
1139 dev_dbg(&card->dev, "card r/w status %d\n", msb->read_only ? 0 : 1);
1141 msb->page_size = 512;
1142 rc = mspro_block_read_attributes(card);
1143 if (rc)
1144 return rc;
1146 dev_dbg(&card->dev, "attributes loaded\n");
1147 return 0;
1151 static int mspro_block_init_disk(struct memstick_dev *card)
1153 struct mspro_block_data *msb = memstick_get_drvdata(card);
1154 struct memstick_host *host = card->host;
1155 struct mspro_devinfo *dev_info = NULL;
1156 struct mspro_sys_info *sys_info = NULL;
1157 struct mspro_sys_attr *s_attr = NULL;
1158 int rc, disk_id;
1159 u64 limit = BLK_BOUNCE_HIGH;
1160 unsigned long capacity;
1162 if (host->cdev.dev->dma_mask && *(host->cdev.dev->dma_mask))
1163 limit = *(host->cdev.dev->dma_mask);
1165 for (rc = 0; msb->attr_group.attrs[rc]; ++rc) {
1166 s_attr = mspro_from_sysfs_attr(msb->attr_group.attrs[rc]);
1168 if (s_attr->id == MSPRO_BLOCK_ID_DEVINFO)
1169 dev_info = s_attr->data;
1170 else if (s_attr->id == MSPRO_BLOCK_ID_SYSINFO)
1171 sys_info = s_attr->data;
1174 if (!dev_info || !sys_info)
1175 return -ENODEV;
1177 msb->cylinders = be16_to_cpu(dev_info->cylinders);
1178 msb->heads = be16_to_cpu(dev_info->heads);
1179 msb->sectors_per_track = be16_to_cpu(dev_info->sectors_per_track);
1181 msb->page_size = be16_to_cpu(sys_info->unit_size);
1183 if (!idr_pre_get(&mspro_block_disk_idr, GFP_KERNEL))
1184 return -ENOMEM;
1186 mutex_lock(&mspro_block_disk_lock);
1187 rc = idr_get_new(&mspro_block_disk_idr, card, &disk_id);
1188 mutex_unlock(&mspro_block_disk_lock);
1190 if (rc)
1191 return rc;
1193 if ((disk_id << MEMSTICK_PART_SHIFT) > 255) {
1194 rc = -ENOSPC;
1195 goto out_release_id;
1198 msb->disk = alloc_disk(1 << MEMSTICK_PART_SHIFT);
1199 if (!msb->disk) {
1200 rc = -ENOMEM;
1201 goto out_release_id;
1204 spin_lock_init(&msb->q_lock);
1205 init_waitqueue_head(&msb->q_wait);
1207 msb->queue = blk_init_queue(mspro_block_request, &msb->q_lock);
1208 if (!msb->queue) {
1209 rc = -ENOMEM;
1210 goto out_put_disk;
1213 msb->queue->queuedata = card;
1215 blk_queue_bounce_limit(msb->queue, limit);
1216 blk_queue_max_sectors(msb->queue, MSPRO_BLOCK_MAX_PAGES);
1217 blk_queue_max_phys_segments(msb->queue, MSPRO_BLOCK_MAX_SEGS);
1218 blk_queue_max_hw_segments(msb->queue, MSPRO_BLOCK_MAX_SEGS);
1219 blk_queue_max_segment_size(msb->queue,
1220 MSPRO_BLOCK_MAX_PAGES * msb->page_size);
1222 msb->disk->major = major;
1223 msb->disk->first_minor = disk_id << MEMSTICK_PART_SHIFT;
1224 msb->disk->fops = &ms_block_bdops;
1225 msb->usage_count = 1;
1226 msb->disk->private_data = msb;
1227 msb->disk->queue = msb->queue;
1228 msb->disk->driverfs_dev = &card->dev;
1230 sprintf(msb->disk->disk_name, "mspblk%d", disk_id);
1232 blk_queue_hardsect_size(msb->queue, msb->page_size);
1234 capacity = be16_to_cpu(sys_info->user_block_count);
1235 capacity *= be16_to_cpu(sys_info->block_size);
1236 capacity *= msb->page_size >> 9;
1237 set_capacity(msb->disk, capacity);
1238 dev_dbg(&card->dev, "capacity set %ld\n", capacity);
1239 msb->q_thread = kthread_run(mspro_block_queue_thread, card,
1240 DRIVER_NAME"d");
1241 if (IS_ERR(msb->q_thread))
1242 goto out_put_disk;
1244 mutex_unlock(&host->lock);
1245 add_disk(msb->disk);
1246 mutex_lock(&host->lock);
1247 msb->active = 1;
1248 return 0;
1250 out_put_disk:
1251 put_disk(msb->disk);
1252 out_release_id:
1253 mutex_lock(&mspro_block_disk_lock);
1254 idr_remove(&mspro_block_disk_idr, disk_id);
1255 mutex_unlock(&mspro_block_disk_lock);
1256 return rc;
1259 static void mspro_block_data_clear(struct mspro_block_data *msb)
1261 int cnt;
1262 struct mspro_sys_attr *s_attr;
1264 if (msb->attr_group.attrs) {
1265 for (cnt = 0; msb->attr_group.attrs[cnt]; ++cnt) {
1266 s_attr = mspro_from_sysfs_attr(msb->attr_group
1267 .attrs[cnt]);
1268 kfree(s_attr->data);
1269 kfree(s_attr);
1271 kfree(msb->attr_group.attrs);
1274 msb->card = NULL;
1277 static int mspro_block_check_card(struct memstick_dev *card)
1279 struct mspro_block_data *msb = memstick_get_drvdata(card);
1281 return (msb->active == 1);
1284 static int mspro_block_probe(struct memstick_dev *card)
1286 struct mspro_block_data *msb;
1287 int rc = 0;
1289 msb = kzalloc(sizeof(struct mspro_block_data), GFP_KERNEL);
1290 if (!msb)
1291 return -ENOMEM;
1292 memstick_set_drvdata(card, msb);
1293 msb->card = card;
1295 rc = mspro_block_init_card(card);
1297 if (rc)
1298 goto out_free;
1300 rc = sysfs_create_group(&card->dev.kobj, &msb->attr_group);
1301 if (rc)
1302 goto out_free;
1304 rc = mspro_block_init_disk(card);
1305 if (!rc) {
1306 card->check = mspro_block_check_card;
1307 return 0;
1310 sysfs_remove_group(&card->dev.kobj, &msb->attr_group);
1311 out_free:
1312 memstick_set_drvdata(card, NULL);
1313 mspro_block_data_clear(msb);
1314 kfree(msb);
1315 return rc;
1318 static void mspro_block_remove(struct memstick_dev *card)
1320 struct mspro_block_data *msb = memstick_get_drvdata(card);
1321 struct task_struct *q_thread = NULL;
1322 unsigned long flags;
1324 del_gendisk(msb->disk);
1325 dev_dbg(&card->dev, "mspro block remove\n");
1326 spin_lock_irqsave(&msb->q_lock, flags);
1327 q_thread = msb->q_thread;
1328 msb->q_thread = NULL;
1329 msb->active = 0;
1330 spin_unlock_irqrestore(&msb->q_lock, flags);
1332 if (q_thread) {
1333 mutex_unlock(&card->host->lock);
1334 kthread_stop(q_thread);
1335 mutex_lock(&card->host->lock);
1338 dev_dbg(&card->dev, "queue thread stopped\n");
1340 blk_cleanup_queue(msb->queue);
1342 sysfs_remove_group(&card->dev.kobj, &msb->attr_group);
1344 mutex_lock(&mspro_block_disk_lock);
1345 mspro_block_data_clear(msb);
1346 mutex_unlock(&mspro_block_disk_lock);
1348 mspro_block_disk_release(msb->disk);
1349 memstick_set_drvdata(card, NULL);
1352 #ifdef CONFIG_PM
1354 static int mspro_block_suspend(struct memstick_dev *card, pm_message_t state)
1356 struct mspro_block_data *msb = memstick_get_drvdata(card);
1357 struct task_struct *q_thread = NULL;
1358 unsigned long flags;
1360 spin_lock_irqsave(&msb->q_lock, flags);
1361 q_thread = msb->q_thread;
1362 msb->q_thread = NULL;
1363 msb->active = 0;
1364 blk_stop_queue(msb->queue);
1365 spin_unlock_irqrestore(&msb->q_lock, flags);
1367 if (q_thread)
1368 kthread_stop(q_thread);
1370 return 0;
1373 static int mspro_block_resume(struct memstick_dev *card)
1375 struct mspro_block_data *msb = memstick_get_drvdata(card);
1376 unsigned long flags;
1377 int rc = 0;
1379 #ifdef CONFIG_MEMSTICK_UNSAFE_RESUME
1381 struct mspro_block_data *new_msb;
1382 struct memstick_host *host = card->host;
1383 struct mspro_sys_attr *s_attr, *r_attr;
1384 unsigned char cnt;
1386 mutex_lock(&host->lock);
1387 new_msb = kzalloc(sizeof(struct mspro_block_data), GFP_KERNEL);
1388 if (!new_msb) {
1389 rc = -ENOMEM;
1390 goto out_unlock;
1393 new_msb->card = card;
1394 memstick_set_drvdata(card, new_msb);
1395 if (mspro_block_init_card(card))
1396 goto out_free;
1398 for (cnt = 0; new_msb->attr_group.attrs[cnt]
1399 && msb->attr_group.attrs[cnt]; ++cnt) {
1400 s_attr = mspro_from_sysfs_attr(new_msb->attr_group.attrs[cnt]);
1401 r_attr = mspro_from_sysfs_attr(msb->attr_group.attrs[cnt]);
1403 if (s_attr->id == MSPRO_BLOCK_ID_SYSINFO
1404 && r_attr->id == s_attr->id) {
1405 if (memcmp(s_attr->data, r_attr->data, s_attr->size))
1406 break;
1408 memstick_set_drvdata(card, msb);
1409 msb->q_thread = kthread_run(mspro_block_queue_thread,
1410 card, DRIVER_NAME"d");
1411 if (IS_ERR(msb->q_thread))
1412 msb->q_thread = NULL;
1413 else
1414 msb->active = 1;
1416 break;
1420 out_free:
1421 memstick_set_drvdata(card, msb);
1422 mspro_block_data_clear(new_msb);
1423 kfree(new_msb);
1424 out_unlock:
1425 mutex_unlock(&host->lock);
1427 #endif /* CONFIG_MEMSTICK_UNSAFE_RESUME */
1429 spin_lock_irqsave(&msb->q_lock, flags);
1430 blk_start_queue(msb->queue);
1431 spin_unlock_irqrestore(&msb->q_lock, flags);
1432 return rc;
1435 #else
1437 #define mspro_block_suspend NULL
1438 #define mspro_block_resume NULL
1440 #endif /* CONFIG_PM */
1442 static struct memstick_device_id mspro_block_id_tbl[] = {
1443 {MEMSTICK_MATCH_ALL, MEMSTICK_TYPE_PRO, MEMSTICK_CATEGORY_STORAGE_DUO,
1444 MEMSTICK_CLASS_GENERIC_DUO},
1449 static struct memstick_driver mspro_block_driver = {
1450 .driver = {
1451 .name = DRIVER_NAME,
1452 .owner = THIS_MODULE
1454 .id_table = mspro_block_id_tbl,
1455 .probe = mspro_block_probe,
1456 .remove = mspro_block_remove,
1457 .suspend = mspro_block_suspend,
1458 .resume = mspro_block_resume
1461 static int __init mspro_block_init(void)
1463 int rc = -ENOMEM;
1465 rc = register_blkdev(major, DRIVER_NAME);
1466 if (rc < 0) {
1467 printk(KERN_ERR DRIVER_NAME ": failed to register "
1468 "major %d, error %d\n", major, rc);
1469 return rc;
1471 if (!major)
1472 major = rc;
1474 rc = memstick_register_driver(&mspro_block_driver);
1475 if (rc)
1476 unregister_blkdev(major, DRIVER_NAME);
1477 return rc;
1480 static void __exit mspro_block_exit(void)
1482 memstick_unregister_driver(&mspro_block_driver);
1483 unregister_blkdev(major, DRIVER_NAME);
1484 idr_destroy(&mspro_block_disk_idr);
1487 module_init(mspro_block_init);
1488 module_exit(mspro_block_exit);
1490 MODULE_LICENSE("GPL");
1491 MODULE_AUTHOR("Alex Dubov");
1492 MODULE_DESCRIPTION("Sony MemoryStickPro block device driver");
1493 MODULE_DEVICE_TABLE(memstick, mspro_block_id_tbl);
1494 <<<<<<< HEAD:drivers/memstick/core/mspro_block.c
1495 MODULE_VERSION(DRIVER_VERSION);
1496 =======
1497 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/memstick/core/mspro_block.c