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