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