blob: a167938d4517b3d735bf70fb681fdd8121fb12eb [file] [log] [blame]
Alex Dubovbaf85322008-02-09 10:20:54 -08001/*
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>
Alex Dubov59367252008-03-10 11:43:42 -070019#include <linux/delay.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090020#include <linux/slab.h>
Arnd Bergmann2a48fc02010-06-02 14:28:52 +020021#include <linux/mutex.h>
Alex Dubovbaf85322008-02-09 10:20:54 -080022#include <linux/memstick.h>
23
24#define DRIVER_NAME "mspro_block"
Alex Dubovbaf85322008-02-09 10:20:54 -080025
Arnd Bergmann2a48fc02010-06-02 14:28:52 +020026static DEFINE_MUTEX(mspro_block_mutex);
Alex Dubovbaf85322008-02-09 10:20:54 -080027static int major;
28module_param(major, int, 0644);
29
30#define MSPRO_BLOCK_MAX_SEGS 32
31#define MSPRO_BLOCK_MAX_PAGES ((2 << 16) - 1)
32
33#define MSPRO_BLOCK_SIGNATURE 0xa5c3
34#define MSPRO_BLOCK_MAX_ATTRIBUTES 41
35
Alex Dubov8e82f8c2008-09-13 02:33:26 -070036#define MSPRO_BLOCK_PART_SHIFT 3
37
Alex Dubovbaf85322008-02-09 10:20:54 -080038enum {
39 MSPRO_BLOCK_ID_SYSINFO = 0x10,
40 MSPRO_BLOCK_ID_MODELNAME = 0x15,
41 MSPRO_BLOCK_ID_MBR = 0x20,
42 MSPRO_BLOCK_ID_PBR16 = 0x21,
43 MSPRO_BLOCK_ID_PBR32 = 0x22,
44 MSPRO_BLOCK_ID_SPECFILEVALUES1 = 0x25,
45 MSPRO_BLOCK_ID_SPECFILEVALUES2 = 0x26,
46 MSPRO_BLOCK_ID_DEVINFO = 0x30
47};
48
49struct mspro_sys_attr {
50 size_t size;
51 void *data;
52 unsigned char id;
53 char name[32];
54 struct device_attribute dev_attr;
55};
56
57struct mspro_attr_entry {
Harvey Harrison69347a22009-01-09 16:40:56 -080058 __be32 address;
59 __be32 size;
Alex Dubovbaf85322008-02-09 10:20:54 -080060 unsigned char id;
61 unsigned char reserved[3];
62} __attribute__((packed));
63
64struct mspro_attribute {
Harvey Harrison69347a22009-01-09 16:40:56 -080065 __be16 signature;
Alex Dubovbaf85322008-02-09 10:20:54 -080066 unsigned short version;
67 unsigned char count;
68 unsigned char reserved[11];
69 struct mspro_attr_entry entries[];
70} __attribute__((packed));
71
72struct mspro_sys_info {
73 unsigned char class;
74 unsigned char reserved0;
Harvey Harrison69347a22009-01-09 16:40:56 -080075 __be16 block_size;
76 __be16 block_count;
77 __be16 user_block_count;
78 __be16 page_size;
Alex Dubovbaf85322008-02-09 10:20:54 -080079 unsigned char reserved1[2];
80 unsigned char assembly_date[8];
Harvey Harrison69347a22009-01-09 16:40:56 -080081 __be32 serial_number;
Alex Dubovbaf85322008-02-09 10:20:54 -080082 unsigned char assembly_maker_code;
83 unsigned char assembly_model_code[3];
Harvey Harrison69347a22009-01-09 16:40:56 -080084 __be16 memory_maker_code;
85 __be16 memory_model_code;
Alex Dubovbaf85322008-02-09 10:20:54 -080086 unsigned char reserved2[4];
87 unsigned char vcc;
88 unsigned char vpp;
Harvey Harrison69347a22009-01-09 16:40:56 -080089 __be16 controller_number;
90 __be16 controller_function;
91 __be16 start_sector;
92 __be16 unit_size;
Alex Dubovbaf85322008-02-09 10:20:54 -080093 unsigned char ms_sub_class;
94 unsigned char reserved3[4];
95 unsigned char interface_type;
Harvey Harrison69347a22009-01-09 16:40:56 -080096 __be16 controller_code;
Alex Dubovbaf85322008-02-09 10:20:54 -080097 unsigned char format_type;
98 unsigned char reserved4;
99 unsigned char device_type;
100 unsigned char reserved5[7];
101 unsigned char mspro_id[16];
102 unsigned char reserved6[16];
103} __attribute__((packed));
104
105struct mspro_mbr {
106 unsigned char boot_partition;
107 unsigned char start_head;
108 unsigned char start_sector;
109 unsigned char start_cylinder;
110 unsigned char partition_type;
111 unsigned char end_head;
112 unsigned char end_sector;
113 unsigned char end_cylinder;
114 unsigned int start_sectors;
115 unsigned int sectors_per_partition;
116} __attribute__((packed));
117
Alex Dubovefb27422008-03-10 11:43:41 -0700118struct mspro_specfile {
119 char name[8];
120 char ext[3];
121 unsigned char attr;
122 unsigned char reserved[10];
123 unsigned short time;
124 unsigned short date;
125 unsigned short cluster;
126 unsigned int size;
127} __attribute__((packed));
128
Alex Dubovbaf85322008-02-09 10:20:54 -0800129struct mspro_devinfo {
Harvey Harrison69347a22009-01-09 16:40:56 -0800130 __be16 cylinders;
131 __be16 heads;
132 __be16 bytes_per_track;
133 __be16 bytes_per_sector;
134 __be16 sectors_per_track;
Alex Dubovbaf85322008-02-09 10:20:54 -0800135 unsigned char reserved[6];
136} __attribute__((packed));
137
138struct mspro_block_data {
139 struct memstick_dev *card;
140 unsigned int usage_count;
Alex Dubovead70772008-03-19 17:01:06 -0700141 unsigned int caps;
Alex Dubovbaf85322008-02-09 10:20:54 -0800142 struct gendisk *disk;
143 struct request_queue *queue;
Alex Dubovf1d82692008-07-25 19:45:02 -0700144 struct request *block_req;
Alex Dubovbaf85322008-02-09 10:20:54 -0800145 spinlock_t q_lock;
Alex Dubovbaf85322008-02-09 10:20:54 -0800146
147 unsigned short page_size;
148 unsigned short cylinders;
149 unsigned short heads;
150 unsigned short sectors_per_track;
151
152 unsigned char system;
153 unsigned char read_only:1,
Alex Dubovf1d82692008-07-25 19:45:02 -0700154 eject:1,
Alex Dubovbaf85322008-02-09 10:20:54 -0800155 has_request:1,
Alex Dubovf1d82692008-07-25 19:45:02 -0700156 data_dir:1,
157 active:1;
Alex Dubovbaf85322008-02-09 10:20:54 -0800158 unsigned char transfer_cmd;
159
160 int (*mrq_handler)(struct memstick_dev *card,
161 struct memstick_request **mrq);
162
163 struct attribute_group attr_group;
164
165 struct scatterlist req_sg[MSPRO_BLOCK_MAX_SEGS];
166 unsigned int seg_count;
167 unsigned int current_seg;
Alex Dubovf1d82692008-07-25 19:45:02 -0700168 unsigned int current_page;
Alex Dubovbaf85322008-02-09 10:20:54 -0800169};
170
171static DEFINE_IDR(mspro_block_disk_idr);
172static DEFINE_MUTEX(mspro_block_disk_lock);
173
Alex Dubovf1d82692008-07-25 19:45:02 -0700174static int mspro_block_complete_req(struct memstick_dev *card, int error);
175
Alex Dubovbaf85322008-02-09 10:20:54 -0800176/*** Block device ***/
177
Al Viro5d9a54b2008-03-02 10:31:38 -0500178static int mspro_block_bd_open(struct block_device *bdev, fmode_t mode)
Alex Dubovbaf85322008-02-09 10:20:54 -0800179{
Al Viro5d9a54b2008-03-02 10:31:38 -0500180 struct gendisk *disk = bdev->bd_disk;
Alex Dubovbaf85322008-02-09 10:20:54 -0800181 struct mspro_block_data *msb = disk->private_data;
182 int rc = -ENXIO;
183
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200184 mutex_lock(&mspro_block_mutex);
Alex Dubovbaf85322008-02-09 10:20:54 -0800185 mutex_lock(&mspro_block_disk_lock);
186
187 if (msb && msb->card) {
188 msb->usage_count++;
Al Viro5d9a54b2008-03-02 10:31:38 -0500189 if ((mode & FMODE_WRITE) && msb->read_only)
Alex Dubovbaf85322008-02-09 10:20:54 -0800190 rc = -EROFS;
191 else
192 rc = 0;
193 }
194
195 mutex_unlock(&mspro_block_disk_lock);
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200196 mutex_unlock(&mspro_block_mutex);
Alex Dubovbaf85322008-02-09 10:20:54 -0800197
198 return rc;
199}
200
201
202static int mspro_block_disk_release(struct gendisk *disk)
203{
204 struct mspro_block_data *msb = disk->private_data;
Tejun Heof331c022008-09-03 09:01:48 +0200205 int disk_id = MINOR(disk_devt(disk)) >> MSPRO_BLOCK_PART_SHIFT;
Alex Dubovbaf85322008-02-09 10:20:54 -0800206
207 mutex_lock(&mspro_block_disk_lock);
208
Alex Dubovf1d82692008-07-25 19:45:02 -0700209 if (msb) {
210 if (msb->usage_count)
211 msb->usage_count--;
212
Alex Dubovbaf85322008-02-09 10:20:54 -0800213 if (!msb->usage_count) {
214 kfree(msb);
215 disk->private_data = NULL;
216 idr_remove(&mspro_block_disk_idr, disk_id);
217 put_disk(disk);
218 }
219 }
220
221 mutex_unlock(&mspro_block_disk_lock);
222
223 return 0;
224}
225
Al Viro5d9a54b2008-03-02 10:31:38 -0500226static int mspro_block_bd_release(struct gendisk *disk, fmode_t mode)
Alex Dubovbaf85322008-02-09 10:20:54 -0800227{
Arnd Bergmann6e9624b2010-08-07 18:25:34 +0200228 int ret;
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200229 mutex_lock(&mspro_block_mutex);
Arnd Bergmann6e9624b2010-08-07 18:25:34 +0200230 ret = mspro_block_disk_release(disk);
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200231 mutex_unlock(&mspro_block_mutex);
Arnd Bergmann6e9624b2010-08-07 18:25:34 +0200232 return ret;
Alex Dubovbaf85322008-02-09 10:20:54 -0800233}
234
235static int mspro_block_bd_getgeo(struct block_device *bdev,
236 struct hd_geometry *geo)
237{
238 struct mspro_block_data *msb = bdev->bd_disk->private_data;
239
240 geo->heads = msb->heads;
241 geo->sectors = msb->sectors_per_track;
242 geo->cylinders = msb->cylinders;
243
244 return 0;
245}
246
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -0700247static const struct block_device_operations ms_block_bdops = {
Al Viro5d9a54b2008-03-02 10:31:38 -0500248 .open = mspro_block_bd_open,
249 .release = mspro_block_bd_release,
Alex Dubovbaf85322008-02-09 10:20:54 -0800250 .getgeo = mspro_block_bd_getgeo,
251 .owner = THIS_MODULE
252};
253
254/*** Information ***/
255
256static struct mspro_sys_attr *mspro_from_sysfs_attr(struct attribute *attr)
257{
258 struct device_attribute *dev_attr
259 = container_of(attr, struct device_attribute, attr);
260 return container_of(dev_attr, struct mspro_sys_attr, dev_attr);
261}
262
263static const char *mspro_block_attr_name(unsigned char tag)
264{
265 switch (tag) {
266 case MSPRO_BLOCK_ID_SYSINFO:
267 return "attr_sysinfo";
268 case MSPRO_BLOCK_ID_MODELNAME:
269 return "attr_modelname";
270 case MSPRO_BLOCK_ID_MBR:
271 return "attr_mbr";
272 case MSPRO_BLOCK_ID_PBR16:
273 return "attr_pbr16";
274 case MSPRO_BLOCK_ID_PBR32:
275 return "attr_pbr32";
276 case MSPRO_BLOCK_ID_SPECFILEVALUES1:
277 return "attr_specfilevalues1";
278 case MSPRO_BLOCK_ID_SPECFILEVALUES2:
279 return "attr_specfilevalues2";
280 case MSPRO_BLOCK_ID_DEVINFO:
281 return "attr_devinfo";
282 default:
283 return NULL;
284 };
285}
286
287typedef ssize_t (*sysfs_show_t)(struct device *dev,
288 struct device_attribute *attr,
289 char *buffer);
290
291static ssize_t mspro_block_attr_show_default(struct device *dev,
292 struct device_attribute *attr,
293 char *buffer)
294{
295 struct mspro_sys_attr *s_attr = container_of(attr,
296 struct mspro_sys_attr,
297 dev_attr);
298
299 ssize_t cnt, rc = 0;
300
301 for (cnt = 0; cnt < s_attr->size; cnt++) {
302 if (cnt && !(cnt % 16)) {
303 if (PAGE_SIZE - rc)
304 buffer[rc++] = '\n';
305 }
306
307 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "%02x ",
308 ((unsigned char *)s_attr->data)[cnt]);
309 }
310 return rc;
311}
312
313static ssize_t mspro_block_attr_show_sysinfo(struct device *dev,
314 struct device_attribute *attr,
315 char *buffer)
316{
317 struct mspro_sys_attr *x_attr = container_of(attr,
318 struct mspro_sys_attr,
319 dev_attr);
320 struct mspro_sys_info *x_sys = x_attr->data;
321 ssize_t rc = 0;
Alex Dubov251cc9b2008-03-10 11:43:42 -0700322 int date_tz = 0, date_tz_f = 0;
323
324 if (x_sys->assembly_date[0] > 0x80U) {
325 date_tz = (~x_sys->assembly_date[0]) + 1;
326 date_tz_f = date_tz & 3;
327 date_tz >>= 2;
328 date_tz = -date_tz;
329 date_tz_f *= 15;
330 } else if (x_sys->assembly_date[0] < 0x80U) {
331 date_tz = x_sys->assembly_date[0];
332 date_tz_f = date_tz & 3;
333 date_tz >>= 2;
334 date_tz_f *= 15;
335 }
Alex Dubovbaf85322008-02-09 10:20:54 -0800336
337 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "class: %x\n",
338 x_sys->class);
339 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "block size: %x\n",
340 be16_to_cpu(x_sys->block_size));
341 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "block count: %x\n",
342 be16_to_cpu(x_sys->block_count));
343 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "user block count: %x\n",
344 be16_to_cpu(x_sys->user_block_count));
345 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "page size: %x\n",
346 be16_to_cpu(x_sys->page_size));
347 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "assembly date: "
Alex Dubov251cc9b2008-03-10 11:43:42 -0700348 "GMT%+d:%d %04u-%02u-%02u %02u:%02u:%02u\n",
349 date_tz, date_tz_f,
Harvey Harrison69347a22009-01-09 16:40:56 -0800350 be16_to_cpup((__be16 *)&x_sys->assembly_date[1]),
Alex Dubovbaf85322008-02-09 10:20:54 -0800351 x_sys->assembly_date[3], x_sys->assembly_date[4],
352 x_sys->assembly_date[5], x_sys->assembly_date[6],
353 x_sys->assembly_date[7]);
354 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "serial number: %x\n",
355 be32_to_cpu(x_sys->serial_number));
356 rc += scnprintf(buffer + rc, PAGE_SIZE - rc,
357 "assembly maker code: %x\n",
358 x_sys->assembly_maker_code);
359 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "assembly model code: "
360 "%02x%02x%02x\n", x_sys->assembly_model_code[0],
361 x_sys->assembly_model_code[1],
362 x_sys->assembly_model_code[2]);
363 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "memory maker code: %x\n",
364 be16_to_cpu(x_sys->memory_maker_code));
365 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "memory model code: %x\n",
366 be16_to_cpu(x_sys->memory_model_code));
367 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "vcc: %x\n",
368 x_sys->vcc);
369 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "vpp: %x\n",
370 x_sys->vpp);
371 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "controller number: %x\n",
372 be16_to_cpu(x_sys->controller_number));
373 rc += scnprintf(buffer + rc, PAGE_SIZE - rc,
374 "controller function: %x\n",
375 be16_to_cpu(x_sys->controller_function));
376 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "start sector: %x\n",
377 be16_to_cpu(x_sys->start_sector));
378 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "unit size: %x\n",
379 be16_to_cpu(x_sys->unit_size));
380 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "sub class: %x\n",
381 x_sys->ms_sub_class);
382 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "interface type: %x\n",
383 x_sys->interface_type);
384 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "controller code: %x\n",
385 be16_to_cpu(x_sys->controller_code));
386 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "format type: %x\n",
387 x_sys->format_type);
388 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "device type: %x\n",
389 x_sys->device_type);
390 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "mspro id: %s\n",
391 x_sys->mspro_id);
392 return rc;
393}
394
395static ssize_t mspro_block_attr_show_modelname(struct device *dev,
396 struct device_attribute *attr,
397 char *buffer)
398{
399 struct mspro_sys_attr *s_attr = container_of(attr,
400 struct mspro_sys_attr,
401 dev_attr);
402
403 return scnprintf(buffer, PAGE_SIZE, "%s", (char *)s_attr->data);
404}
405
406static ssize_t mspro_block_attr_show_mbr(struct device *dev,
407 struct device_attribute *attr,
408 char *buffer)
409{
410 struct mspro_sys_attr *x_attr = container_of(attr,
411 struct mspro_sys_attr,
412 dev_attr);
413 struct mspro_mbr *x_mbr = x_attr->data;
414 ssize_t rc = 0;
415
416 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "boot partition: %x\n",
417 x_mbr->boot_partition);
418 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "start head: %x\n",
419 x_mbr->start_head);
420 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "start sector: %x\n",
421 x_mbr->start_sector);
422 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "start cylinder: %x\n",
423 x_mbr->start_cylinder);
424 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "partition type: %x\n",
425 x_mbr->partition_type);
426 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "end head: %x\n",
427 x_mbr->end_head);
428 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "end sector: %x\n",
429 x_mbr->end_sector);
430 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "end cylinder: %x\n",
431 x_mbr->end_cylinder);
432 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "start sectors: %x\n",
433 x_mbr->start_sectors);
434 rc += scnprintf(buffer + rc, PAGE_SIZE - rc,
435 "sectors per partition: %x\n",
436 x_mbr->sectors_per_partition);
437 return rc;
438}
439
Alex Dubovefb27422008-03-10 11:43:41 -0700440static ssize_t mspro_block_attr_show_specfile(struct device *dev,
441 struct device_attribute *attr,
442 char *buffer)
443{
444 struct mspro_sys_attr *x_attr = container_of(attr,
445 struct mspro_sys_attr,
446 dev_attr);
447 struct mspro_specfile *x_spfile = x_attr->data;
448 char name[9], ext[4];
449 ssize_t rc = 0;
450
451 memcpy(name, x_spfile->name, 8);
452 name[8] = 0;
453 memcpy(ext, x_spfile->ext, 3);
454 ext[3] = 0;
455
456 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "name: %s\n", name);
457 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "ext: %s\n", ext);
458 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "attribute: %x\n",
459 x_spfile->attr);
460 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "time: %d:%d:%d\n",
461 x_spfile->time >> 11,
462 (x_spfile->time >> 5) & 0x3f,
463 (x_spfile->time & 0x1f) * 2);
464 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "date: %d-%d-%d\n",
465 (x_spfile->date >> 9) + 1980,
466 (x_spfile->date >> 5) & 0xf,
467 x_spfile->date & 0x1f);
468 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "start cluster: %x\n",
469 x_spfile->cluster);
470 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "size: %x\n",
471 x_spfile->size);
472 return rc;
473}
474
Alex Dubovbaf85322008-02-09 10:20:54 -0800475static ssize_t mspro_block_attr_show_devinfo(struct device *dev,
476 struct device_attribute *attr,
477 char *buffer)
478{
479 struct mspro_sys_attr *x_attr = container_of(attr,
480 struct mspro_sys_attr,
481 dev_attr);
482 struct mspro_devinfo *x_devinfo = x_attr->data;
483 ssize_t rc = 0;
484
485 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "cylinders: %x\n",
486 be16_to_cpu(x_devinfo->cylinders));
487 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "heads: %x\n",
488 be16_to_cpu(x_devinfo->heads));
489 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "bytes per track: %x\n",
490 be16_to_cpu(x_devinfo->bytes_per_track));
491 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "bytes per sector: %x\n",
492 be16_to_cpu(x_devinfo->bytes_per_sector));
493 rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "sectors per track: %x\n",
494 be16_to_cpu(x_devinfo->sectors_per_track));
495 return rc;
496}
497
498static sysfs_show_t mspro_block_attr_show(unsigned char tag)
499{
500 switch (tag) {
501 case MSPRO_BLOCK_ID_SYSINFO:
502 return mspro_block_attr_show_sysinfo;
503 case MSPRO_BLOCK_ID_MODELNAME:
504 return mspro_block_attr_show_modelname;
505 case MSPRO_BLOCK_ID_MBR:
506 return mspro_block_attr_show_mbr;
Alex Dubovefb27422008-03-10 11:43:41 -0700507 case MSPRO_BLOCK_ID_SPECFILEVALUES1:
508 case MSPRO_BLOCK_ID_SPECFILEVALUES2:
509 return mspro_block_attr_show_specfile;
Alex Dubovbaf85322008-02-09 10:20:54 -0800510 case MSPRO_BLOCK_ID_DEVINFO:
511 return mspro_block_attr_show_devinfo;
512 default:
513 return mspro_block_attr_show_default;
514 }
515}
516
517/*** Protocol handlers ***/
518
519/*
520 * Functions prefixed with "h_" are protocol callbacks. They can be called from
521 * interrupt context. Return value of 0 means that request processing is still
522 * ongoing, while special error value of -EAGAIN means that current request is
523 * finished (and request processor should come back some time later).
524 */
525
526static int h_mspro_block_req_init(struct memstick_dev *card,
527 struct memstick_request **mrq)
528{
529 struct mspro_block_data *msb = memstick_get_drvdata(card);
530
531 *mrq = &card->current_mrq;
532 card->next_request = msb->mrq_handler;
533 return 0;
534}
535
536static int h_mspro_block_default(struct memstick_dev *card,
537 struct memstick_request **mrq)
538{
Alex Dubovf1d82692008-07-25 19:45:02 -0700539 return mspro_block_complete_req(card, (*mrq)->error);
540}
541
542static int h_mspro_block_default_bad(struct memstick_dev *card,
543 struct memstick_request **mrq)
544{
545 return -ENXIO;
Alex Dubovbaf85322008-02-09 10:20:54 -0800546}
547
548static int h_mspro_block_get_ro(struct memstick_dev *card,
549 struct memstick_request **mrq)
550{
551 struct mspro_block_data *msb = memstick_get_drvdata(card);
552
Alex Dubovf1d82692008-07-25 19:45:02 -0700553 if (!(*mrq)->error) {
554 if ((*mrq)->data[offsetof(struct ms_status_register, status0)]
555 & MEMSTICK_STATUS0_WP)
556 msb->read_only = 1;
557 else
558 msb->read_only = 0;
Alex Dubovbaf85322008-02-09 10:20:54 -0800559 }
560
Alex Dubovf1d82692008-07-25 19:45:02 -0700561 return mspro_block_complete_req(card, (*mrq)->error);
Alex Dubovbaf85322008-02-09 10:20:54 -0800562}
563
564static int h_mspro_block_wait_for_ced(struct memstick_dev *card,
565 struct memstick_request **mrq)
566{
Alex Dubovbaf85322008-02-09 10:20:54 -0800567 dev_dbg(&card->dev, "wait for ced: value %x\n", (*mrq)->data[0]);
568
Alex Dubovf1d82692008-07-25 19:45:02 -0700569 if (!(*mrq)->error) {
570 if ((*mrq)->data[0] & (MEMSTICK_INT_CMDNAK | MEMSTICK_INT_ERR))
571 (*mrq)->error = -EFAULT;
572 else if (!((*mrq)->data[0] & MEMSTICK_INT_CED))
573 return 0;
Alex Dubovbaf85322008-02-09 10:20:54 -0800574 }
575
Alex Dubovf1d82692008-07-25 19:45:02 -0700576 return mspro_block_complete_req(card, (*mrq)->error);
Alex Dubovbaf85322008-02-09 10:20:54 -0800577}
578
579static int h_mspro_block_transfer_data(struct memstick_dev *card,
580 struct memstick_request **mrq)
581{
Alex Dubovbaf85322008-02-09 10:20:54 -0800582 struct mspro_block_data *msb = memstick_get_drvdata(card);
583 unsigned char t_val = 0;
584 struct scatterlist t_sg = { 0 };
585 size_t t_offset;
586
Alex Dubovf1d82692008-07-25 19:45:02 -0700587 if ((*mrq)->error)
588 return mspro_block_complete_req(card, (*mrq)->error);
Alex Dubovbaf85322008-02-09 10:20:54 -0800589
590 switch ((*mrq)->tpc) {
591 case MS_TPC_WRITE_REG:
592 memstick_init_req(*mrq, MS_TPC_SET_CMD, &msb->transfer_cmd, 1);
Alex Dubovead70772008-03-19 17:01:06 -0700593 (*mrq)->need_card_int = 1;
Alex Dubovbaf85322008-02-09 10:20:54 -0800594 return 0;
595 case MS_TPC_SET_CMD:
596 t_val = (*mrq)->int_reg;
597 memstick_init_req(*mrq, MS_TPC_GET_INT, NULL, 1);
Alex Dubovead70772008-03-19 17:01:06 -0700598 if (msb->caps & MEMSTICK_CAP_AUTO_GET_INT)
Alex Dubovbaf85322008-02-09 10:20:54 -0800599 goto has_int_reg;
600 return 0;
601 case MS_TPC_GET_INT:
602 t_val = (*mrq)->data[0];
603has_int_reg:
604 if (t_val & (MEMSTICK_INT_CMDNAK | MEMSTICK_INT_ERR)) {
605 t_val = MSPRO_CMD_STOP;
606 memstick_init_req(*mrq, MS_TPC_SET_CMD, &t_val, 1);
607 card->next_request = h_mspro_block_default;
608 return 0;
609 }
610
611 if (msb->current_page
612 == (msb->req_sg[msb->current_seg].length
613 / msb->page_size)) {
614 msb->current_page = 0;
615 msb->current_seg++;
616
617 if (msb->current_seg == msb->seg_count) {
618 if (t_val & MEMSTICK_INT_CED) {
Alex Dubovf1d82692008-07-25 19:45:02 -0700619 return mspro_block_complete_req(card,
620 0);
Alex Dubovbaf85322008-02-09 10:20:54 -0800621 } else {
622 card->next_request
623 = h_mspro_block_wait_for_ced;
624 memstick_init_req(*mrq, MS_TPC_GET_INT,
625 NULL, 1);
626 return 0;
627 }
628 }
629 }
630
631 if (!(t_val & MEMSTICK_INT_BREQ)) {
632 memstick_init_req(*mrq, MS_TPC_GET_INT, NULL, 1);
633 return 0;
634 }
635
636 t_offset = msb->req_sg[msb->current_seg].offset;
637 t_offset += msb->current_page * msb->page_size;
638
639 sg_set_page(&t_sg,
640 nth_page(sg_page(&(msb->req_sg[msb->current_seg])),
641 t_offset >> PAGE_SHIFT),
642 msb->page_size, offset_in_page(t_offset));
643
644 memstick_init_req_sg(*mrq, msb->data_dir == READ
645 ? MS_TPC_READ_LONG_DATA
646 : MS_TPC_WRITE_LONG_DATA,
647 &t_sg);
Alex Dubovead70772008-03-19 17:01:06 -0700648 (*mrq)->need_card_int = 1;
Alex Dubovbaf85322008-02-09 10:20:54 -0800649 return 0;
650 case MS_TPC_READ_LONG_DATA:
651 case MS_TPC_WRITE_LONG_DATA:
652 msb->current_page++;
Alex Dubovead70772008-03-19 17:01:06 -0700653 if (msb->caps & MEMSTICK_CAP_AUTO_GET_INT) {
Alex Dubovbaf85322008-02-09 10:20:54 -0800654 t_val = (*mrq)->int_reg;
655 goto has_int_reg;
656 } else {
657 memstick_init_req(*mrq, MS_TPC_GET_INT, NULL, 1);
658 return 0;
659 }
660
661 default:
662 BUG();
663 }
664}
665
666/*** Data transfer ***/
667
Alex Dubovf1d82692008-07-25 19:45:02 -0700668static int mspro_block_issue_req(struct memstick_dev *card, int chunk)
Alex Dubovbaf85322008-02-09 10:20:54 -0800669{
670 struct mspro_block_data *msb = memstick_get_drvdata(card);
Alex Dubovbaf85322008-02-09 10:20:54 -0800671 sector_t t_sec;
Alex Dubovf1d82692008-07-25 19:45:02 -0700672 unsigned int count;
673 struct mspro_param_register param;
Alex Dubovbaf85322008-02-09 10:20:54 -0800674
Alex Dubovf1d82692008-07-25 19:45:02 -0700675try_again:
676 while (chunk) {
677 msb->current_page = 0;
Alex Dubovbaf85322008-02-09 10:20:54 -0800678 msb->current_seg = 0;
Alex Dubovf1d82692008-07-25 19:45:02 -0700679 msb->seg_count = blk_rq_map_sg(msb->block_req->q,
680 msb->block_req,
681 msb->req_sg);
Alex Dubovbaf85322008-02-09 10:20:54 -0800682
Alex Dubovf1d82692008-07-25 19:45:02 -0700683 if (!msb->seg_count) {
Tejun Heo296b2f62009-05-08 11:54:15 +0900684 chunk = __blk_end_request_cur(msb->block_req, -ENOMEM);
Alex Dubovf1d82692008-07-25 19:45:02 -0700685 continue;
686 }
Alex Dubovbaf85322008-02-09 10:20:54 -0800687
Tejun Heo83096eb2009-05-07 22:24:39 +0900688 t_sec = blk_rq_pos(msb->block_req) << 9;
Alex Dubovf1d82692008-07-25 19:45:02 -0700689 sector_div(t_sec, msb->page_size);
Alex Dubovbaf85322008-02-09 10:20:54 -0800690
Tejun Heo1011c1b2009-05-07 22:24:45 +0900691 count = blk_rq_bytes(msb->block_req);
Alex Dubovf1d82692008-07-25 19:45:02 -0700692 count /= msb->page_size;
Alex Dubovbaf85322008-02-09 10:20:54 -0800693
Alex Dubovf1d82692008-07-25 19:45:02 -0700694 param.system = msb->system;
695 param.data_count = cpu_to_be16(count);
696 param.data_address = cpu_to_be32((uint32_t)t_sec);
697 param.tpc_param = 0;
Alex Dubovbaf85322008-02-09 10:20:54 -0800698
Alex Dubovf1d82692008-07-25 19:45:02 -0700699 msb->data_dir = rq_data_dir(msb->block_req);
700 msb->transfer_cmd = msb->data_dir == READ
701 ? MSPRO_CMD_READ_DATA
702 : MSPRO_CMD_WRITE_DATA;
Alex Dubovbaf85322008-02-09 10:20:54 -0800703
Alex Dubovf1d82692008-07-25 19:45:02 -0700704 dev_dbg(&card->dev, "data transfer: cmd %x, "
705 "lba %x, count %x\n", msb->transfer_cmd,
706 be32_to_cpu(param.data_address), count);
Alex Dubovbaf85322008-02-09 10:20:54 -0800707
Alex Dubovf1d82692008-07-25 19:45:02 -0700708 card->next_request = h_mspro_block_req_init;
709 msb->mrq_handler = h_mspro_block_transfer_data;
710 memstick_init_req(&card->current_mrq, MS_TPC_WRITE_REG,
711 &param, sizeof(param));
712 memstick_new_req(card->host);
713 return 0;
714 }
Alex Dubovbaf85322008-02-09 10:20:54 -0800715
Tejun Heo9934c8c2009-05-08 11:54:16 +0900716 dev_dbg(&card->dev, "blk_fetch\n");
717 msb->block_req = blk_fetch_request(msb->queue);
Alex Dubovf1d82692008-07-25 19:45:02 -0700718 if (!msb->block_req) {
719 dev_dbg(&card->dev, "issue end\n");
720 return -EAGAIN;
721 }
Alex Dubovbaf85322008-02-09 10:20:54 -0800722
Alex Dubovf1d82692008-07-25 19:45:02 -0700723 dev_dbg(&card->dev, "trying again\n");
724 chunk = 1;
725 goto try_again;
Alex Dubovbaf85322008-02-09 10:20:54 -0800726}
727
Alex Dubovf1d82692008-07-25 19:45:02 -0700728static int mspro_block_complete_req(struct memstick_dev *card, int error)
Alex Dubovbaf85322008-02-09 10:20:54 -0800729{
Alex Dubovf1d82692008-07-25 19:45:02 -0700730 struct mspro_block_data *msb = memstick_get_drvdata(card);
731 int chunk, cnt;
732 unsigned int t_len = 0;
Alex Dubovbaf85322008-02-09 10:20:54 -0800733 unsigned long flags;
734
735 spin_lock_irqsave(&msb->q_lock, flags);
Alex Dubovf1d82692008-07-25 19:45:02 -0700736 dev_dbg(&card->dev, "complete %d, %d\n", msb->has_request ? 1 : 0,
737 error);
738
739 if (msb->has_request) {
740 /* Nothing to do - not really an error */
741 if (error == -EAGAIN)
742 error = 0;
743
744 if (error || (card->current_mrq.tpc == MSPRO_CMD_STOP)) {
745 if (msb->data_dir == READ) {
746 for (cnt = 0; cnt < msb->current_seg; cnt++)
747 t_len += msb->req_sg[cnt].length
748 / msb->page_size;
749
750 if (msb->current_page)
751 t_len += msb->current_page - 1;
752
753 t_len *= msb->page_size;
754 }
755 } else
Tejun Heo1011c1b2009-05-07 22:24:45 +0900756 t_len = blk_rq_bytes(msb->block_req);
Alex Dubovf1d82692008-07-25 19:45:02 -0700757
758 dev_dbg(&card->dev, "transferred %x (%d)\n", t_len, error);
759
760 if (error && !t_len)
761 t_len = blk_rq_cur_bytes(msb->block_req);
762
763 chunk = __blk_end_request(msb->block_req, error, t_len);
764
765 error = mspro_block_issue_req(card, chunk);
766
767 if (!error)
768 goto out;
769 else
770 msb->has_request = 0;
771 } else {
772 if (!error)
773 error = -EAGAIN;
774 }
775
776 card->next_request = h_mspro_block_default_bad;
777 complete_all(&card->mrq_complete);
778out:
Alex Dubovbaf85322008-02-09 10:20:54 -0800779 spin_unlock_irqrestore(&msb->q_lock, flags);
Alex Dubovf1d82692008-07-25 19:45:02 -0700780 return error;
Alex Dubovbaf85322008-02-09 10:20:54 -0800781}
782
Alex Dubov17017d82008-07-25 19:45:01 -0700783static void mspro_block_stop(struct memstick_dev *card)
784{
785 struct mspro_block_data *msb = memstick_get_drvdata(card);
786 int rc = 0;
787 unsigned long flags;
788
789 while (1) {
790 spin_lock_irqsave(&msb->q_lock, flags);
791 if (!msb->has_request) {
792 blk_stop_queue(msb->queue);
793 rc = 1;
794 }
795 spin_unlock_irqrestore(&msb->q_lock, flags);
796
797 if (rc)
798 break;
799
800 wait_for_completion(&card->mrq_complete);
801 }
802}
803
804static void mspro_block_start(struct memstick_dev *card)
805{
806 struct mspro_block_data *msb = memstick_get_drvdata(card);
807 unsigned long flags;
808
809 spin_lock_irqsave(&msb->q_lock, flags);
810 blk_start_queue(msb->queue);
811 spin_unlock_irqrestore(&msb->q_lock, flags);
812}
813
Alex Dubovf1d82692008-07-25 19:45:02 -0700814static int mspro_block_prepare_req(struct request_queue *q, struct request *req)
Alex Dubovbaf85322008-02-09 10:20:54 -0800815{
Christoph Hellwig33659eb2010-08-07 18:17:56 +0200816 if (req->cmd_type != REQ_TYPE_FS &&
817 req->cmd_type != REQ_TYPE_BLOCK_PC) {
Alex Dubovf1d82692008-07-25 19:45:02 -0700818 blk_dump_rq_flags(req, "MSPro unsupported request");
819 return BLKPREP_KILL;
Alex Dubovbaf85322008-02-09 10:20:54 -0800820 }
Alex Dubovf1d82692008-07-25 19:45:02 -0700821
822 req->cmd_flags |= REQ_DONTPREP;
823
824 return BLKPREP_OK;
Alex Dubovbaf85322008-02-09 10:20:54 -0800825}
826
Alex Dubovf1d82692008-07-25 19:45:02 -0700827static void mspro_block_submit_req(struct request_queue *q)
Alex Dubovbaf85322008-02-09 10:20:54 -0800828{
829 struct memstick_dev *card = q->queuedata;
830 struct mspro_block_data *msb = memstick_get_drvdata(card);
831 struct request *req = NULL;
832
Alex Dubovf1d82692008-07-25 19:45:02 -0700833 if (msb->has_request)
834 return;
835
836 if (msb->eject) {
Tejun Heo9934c8c2009-05-08 11:54:16 +0900837 while ((req = blk_fetch_request(q)) != NULL)
Tejun Heo40cbbb72009-04-23 11:05:19 +0900838 __blk_end_request_all(req, -ENODEV);
Alex Dubovf1d82692008-07-25 19:45:02 -0700839
840 return;
Alex Dubovbaf85322008-02-09 10:20:54 -0800841 }
Alex Dubovf1d82692008-07-25 19:45:02 -0700842
843 msb->has_request = 1;
844 if (mspro_block_issue_req(card, 0))
845 msb->has_request = 0;
Alex Dubovbaf85322008-02-09 10:20:54 -0800846}
847
848/*** Initialization ***/
849
850static int mspro_block_wait_for_ced(struct memstick_dev *card)
851{
852 struct mspro_block_data *msb = memstick_get_drvdata(card);
853
854 card->next_request = h_mspro_block_req_init;
855 msb->mrq_handler = h_mspro_block_wait_for_ced;
856 memstick_init_req(&card->current_mrq, MS_TPC_GET_INT, NULL, 1);
857 memstick_new_req(card->host);
858 wait_for_completion(&card->mrq_complete);
859 return card->current_mrq.error;
860}
861
Alex Dubov962ee1b2008-03-19 17:01:07 -0700862static int mspro_block_set_interface(struct memstick_dev *card,
863 unsigned char sys_reg)
Alex Dubovbaf85322008-02-09 10:20:54 -0800864{
865 struct memstick_host *host = card->host;
866 struct mspro_block_data *msb = memstick_get_drvdata(card);
867 struct mspro_param_register param = {
Alex Dubov962ee1b2008-03-19 17:01:07 -0700868 .system = sys_reg,
Alex Dubovbaf85322008-02-09 10:20:54 -0800869 .data_count = 0,
870 .data_address = 0,
Alex Dubove1f19992008-03-10 11:43:37 -0700871 .tpc_param = 0
Alex Dubovbaf85322008-02-09 10:20:54 -0800872 };
873
874 card->next_request = h_mspro_block_req_init;
875 msb->mrq_handler = h_mspro_block_default;
876 memstick_init_req(&card->current_mrq, MS_TPC_WRITE_REG, &param,
877 sizeof(param));
878 memstick_new_req(host);
879 wait_for_completion(&card->mrq_complete);
Alex Dubov962ee1b2008-03-19 17:01:07 -0700880 return card->current_mrq.error;
881}
882
883static int mspro_block_switch_interface(struct memstick_dev *card)
884{
885 struct memstick_host *host = card->host;
886 struct mspro_block_data *msb = memstick_get_drvdata(card);
887 int rc = 0;
888
Alex Dubov8e82f8c2008-09-13 02:33:26 -0700889try_again:
Alex Dubov962ee1b2008-03-19 17:01:07 -0700890 if (msb->caps & MEMSTICK_CAP_PAR4)
891 rc = mspro_block_set_interface(card, MEMSTICK_SYS_PAR4);
892 else
893 return 0;
894
895 if (rc) {
896 printk(KERN_WARNING
897 "%s: could not switch to 4-bit mode, error %d\n",
Kay Sievers0252c3b2009-01-06 10:44:38 -0800898 dev_name(&card->dev), rc);
Alex Dubov962ee1b2008-03-19 17:01:07 -0700899 return 0;
900 }
Alex Dubovbaf85322008-02-09 10:20:54 -0800901
Alex Dubove1f19992008-03-10 11:43:37 -0700902 msb->system = MEMSTICK_SYS_PAR4;
903 host->set_param(host, MEMSTICK_INTERFACE, MEMSTICK_PAR4);
Alex Dubov962ee1b2008-03-19 17:01:07 -0700904 printk(KERN_INFO "%s: switching to 4-bit parallel mode\n",
Kay Sievers0252c3b2009-01-06 10:44:38 -0800905 dev_name(&card->dev));
Alex Dubov962ee1b2008-03-19 17:01:07 -0700906
907 if (msb->caps & MEMSTICK_CAP_PAR8) {
908 rc = mspro_block_set_interface(card, MEMSTICK_SYS_PAR8);
909
910 if (!rc) {
911 msb->system = MEMSTICK_SYS_PAR8;
912 host->set_param(host, MEMSTICK_INTERFACE,
913 MEMSTICK_PAR8);
914 printk(KERN_INFO
915 "%s: switching to 8-bit parallel mode\n",
Kay Sievers0252c3b2009-01-06 10:44:38 -0800916 dev_name(&card->dev));
Alex Dubov962ee1b2008-03-19 17:01:07 -0700917 } else
918 printk(KERN_WARNING
919 "%s: could not switch to 8-bit mode, error %d\n",
Kay Sievers0252c3b2009-01-06 10:44:38 -0800920 dev_name(&card->dev), rc);
Alex Dubov962ee1b2008-03-19 17:01:07 -0700921 }
Alex Dubovbaf85322008-02-09 10:20:54 -0800922
923 card->next_request = h_mspro_block_req_init;
924 msb->mrq_handler = h_mspro_block_default;
925 memstick_init_req(&card->current_mrq, MS_TPC_GET_INT, NULL, 1);
926 memstick_new_req(card->host);
927 wait_for_completion(&card->mrq_complete);
Alex Dubov962ee1b2008-03-19 17:01:07 -0700928 rc = card->current_mrq.error;
Alex Dubovbaf85322008-02-09 10:20:54 -0800929
Alex Dubov962ee1b2008-03-19 17:01:07 -0700930 if (rc) {
931 printk(KERN_WARNING
932 "%s: interface error, trying to fall back to serial\n",
Kay Sievers0252c3b2009-01-06 10:44:38 -0800933 dev_name(&card->dev));
Alex Dubov59367252008-03-10 11:43:42 -0700934 msb->system = MEMSTICK_SYS_SERIAL;
935 host->set_param(host, MEMSTICK_POWER, MEMSTICK_POWER_OFF);
Alex Dubov962ee1b2008-03-19 17:01:07 -0700936 msleep(10);
Alex Dubov59367252008-03-10 11:43:42 -0700937 host->set_param(host, MEMSTICK_POWER, MEMSTICK_POWER_ON);
Alex Dubovbaf85322008-02-09 10:20:54 -0800938 host->set_param(host, MEMSTICK_INTERFACE, MEMSTICK_SERIAL);
Alex Dubov59367252008-03-10 11:43:42 -0700939
Alex Dubov962ee1b2008-03-19 17:01:07 -0700940 rc = memstick_set_rw_addr(card);
941 if (!rc)
942 rc = mspro_block_set_interface(card, msb->system);
Alex Dubov8e82f8c2008-09-13 02:33:26 -0700943
944 if (!rc) {
945 msleep(150);
946 rc = mspro_block_wait_for_ced(card);
947 if (rc)
948 return rc;
949
950 if (msb->caps & MEMSTICK_CAP_PAR8) {
951 msb->caps &= ~MEMSTICK_CAP_PAR8;
952 goto try_again;
953 }
954 }
Alex Dubovbaf85322008-02-09 10:20:54 -0800955 }
Alex Dubov962ee1b2008-03-19 17:01:07 -0700956 return rc;
Alex Dubovbaf85322008-02-09 10:20:54 -0800957}
958
959/* Memory allocated for attributes by this function should be freed by
960 * mspro_block_data_clear, no matter if the initialization process succeded
961 * or failed.
962 */
963static int mspro_block_read_attributes(struct memstick_dev *card)
964{
965 struct mspro_block_data *msb = memstick_get_drvdata(card);
966 struct mspro_param_register param = {
967 .system = msb->system,
968 .data_count = cpu_to_be16(1),
969 .data_address = 0,
Alex Dubove1f19992008-03-10 11:43:37 -0700970 .tpc_param = 0
Alex Dubovbaf85322008-02-09 10:20:54 -0800971 };
972 struct mspro_attribute *attr = NULL;
973 struct mspro_sys_attr *s_attr = NULL;
974 unsigned char *buffer = NULL;
975 int cnt, rc, attr_count;
976 unsigned int addr;
977 unsigned short page_count;
978
979 attr = kmalloc(msb->page_size, GFP_KERNEL);
980 if (!attr)
981 return -ENOMEM;
982
983 sg_init_one(&msb->req_sg[0], attr, msb->page_size);
984 msb->seg_count = 1;
985 msb->current_seg = 0;
986 msb->current_page = 0;
987 msb->data_dir = READ;
988 msb->transfer_cmd = MSPRO_CMD_READ_ATRB;
989
990 card->next_request = h_mspro_block_req_init;
991 msb->mrq_handler = h_mspro_block_transfer_data;
992 memstick_init_req(&card->current_mrq, MS_TPC_WRITE_REG, &param,
993 sizeof(param));
994 memstick_new_req(card->host);
995 wait_for_completion(&card->mrq_complete);
996 if (card->current_mrq.error) {
997 rc = card->current_mrq.error;
998 goto out_free_attr;
999 }
1000
1001 if (be16_to_cpu(attr->signature) != MSPRO_BLOCK_SIGNATURE) {
1002 printk(KERN_ERR "%s: unrecognized device signature %x\n",
Kay Sievers0252c3b2009-01-06 10:44:38 -08001003 dev_name(&card->dev), be16_to_cpu(attr->signature));
Alex Dubovbaf85322008-02-09 10:20:54 -08001004 rc = -ENODEV;
1005 goto out_free_attr;
1006 }
1007
1008 if (attr->count > MSPRO_BLOCK_MAX_ATTRIBUTES) {
1009 printk(KERN_WARNING "%s: way too many attribute entries\n",
Kay Sievers0252c3b2009-01-06 10:44:38 -08001010 dev_name(&card->dev));
Alex Dubovbaf85322008-02-09 10:20:54 -08001011 attr_count = MSPRO_BLOCK_MAX_ATTRIBUTES;
1012 } else
1013 attr_count = attr->count;
1014
1015 msb->attr_group.attrs = kzalloc((attr_count + 1)
1016 * sizeof(struct attribute),
1017 GFP_KERNEL);
1018 if (!msb->attr_group.attrs) {
1019 rc = -ENOMEM;
1020 goto out_free_attr;
1021 }
1022 msb->attr_group.name = "media_attributes";
1023
1024 buffer = kmalloc(msb->page_size, GFP_KERNEL);
1025 if (!buffer) {
1026 rc = -ENOMEM;
1027 goto out_free_attr;
1028 }
1029 memcpy(buffer, (char *)attr, msb->page_size);
1030 page_count = 1;
1031
1032 for (cnt = 0; cnt < attr_count; ++cnt) {
1033 s_attr = kzalloc(sizeof(struct mspro_sys_attr), GFP_KERNEL);
1034 if (!s_attr) {
1035 rc = -ENOMEM;
1036 goto out_free_buffer;
1037 }
1038
1039 msb->attr_group.attrs[cnt] = &s_attr->dev_attr.attr;
1040 addr = be32_to_cpu(attr->entries[cnt].address);
1041 rc = be32_to_cpu(attr->entries[cnt].size);
1042 dev_dbg(&card->dev, "adding attribute %d: id %x, address %x, "
1043 "size %x\n", cnt, attr->entries[cnt].id, addr, rc);
1044 s_attr->id = attr->entries[cnt].id;
1045 if (mspro_block_attr_name(s_attr->id))
1046 snprintf(s_attr->name, sizeof(s_attr->name), "%s",
1047 mspro_block_attr_name(attr->entries[cnt].id));
1048 else
1049 snprintf(s_attr->name, sizeof(s_attr->name),
1050 "attr_x%02x", attr->entries[cnt].id);
1051
Maxim Levitsky21fd0492010-08-11 14:17:52 -07001052 sysfs_attr_init(&s_attr->dev_attr.attr);
Alex Dubovbaf85322008-02-09 10:20:54 -08001053 s_attr->dev_attr.attr.name = s_attr->name;
1054 s_attr->dev_attr.attr.mode = S_IRUGO;
Alex Dubovbaf85322008-02-09 10:20:54 -08001055 s_attr->dev_attr.show = mspro_block_attr_show(s_attr->id);
1056
1057 if (!rc)
1058 continue;
1059
1060 s_attr->size = rc;
1061 s_attr->data = kmalloc(rc, GFP_KERNEL);
1062 if (!s_attr->data) {
1063 rc = -ENOMEM;
1064 goto out_free_buffer;
1065 }
1066
1067 if (((addr / msb->page_size)
1068 == be32_to_cpu(param.data_address))
1069 && (((addr + rc - 1) / msb->page_size)
1070 == be32_to_cpu(param.data_address))) {
1071 memcpy(s_attr->data, buffer + addr % msb->page_size,
1072 rc);
1073 continue;
1074 }
1075
1076 if (page_count <= (rc / msb->page_size)) {
1077 kfree(buffer);
1078 page_count = (rc / msb->page_size) + 1;
1079 buffer = kmalloc(page_count * msb->page_size,
1080 GFP_KERNEL);
1081 if (!buffer) {
1082 rc = -ENOMEM;
1083 goto out_free_attr;
1084 }
1085 }
1086
1087 param.system = msb->system;
1088 param.data_count = cpu_to_be16((rc / msb->page_size) + 1);
1089 param.data_address = cpu_to_be32(addr / msb->page_size);
Alex Dubove1f19992008-03-10 11:43:37 -07001090 param.tpc_param = 0;
Alex Dubovbaf85322008-02-09 10:20:54 -08001091
1092 sg_init_one(&msb->req_sg[0], buffer,
1093 be16_to_cpu(param.data_count) * msb->page_size);
1094 msb->seg_count = 1;
1095 msb->current_seg = 0;
1096 msb->current_page = 0;
1097 msb->data_dir = READ;
1098 msb->transfer_cmd = MSPRO_CMD_READ_ATRB;
1099
1100 dev_dbg(&card->dev, "reading attribute pages %x, %x\n",
1101 be32_to_cpu(param.data_address),
1102 be16_to_cpu(param.data_count));
1103
1104 card->next_request = h_mspro_block_req_init;
1105 msb->mrq_handler = h_mspro_block_transfer_data;
1106 memstick_init_req(&card->current_mrq, MS_TPC_WRITE_REG,
1107 (char *)&param, sizeof(param));
1108 memstick_new_req(card->host);
1109 wait_for_completion(&card->mrq_complete);
1110 if (card->current_mrq.error) {
1111 rc = card->current_mrq.error;
1112 goto out_free_buffer;
1113 }
1114
1115 memcpy(s_attr->data, buffer + addr % msb->page_size, rc);
1116 }
1117
1118 rc = 0;
1119out_free_buffer:
1120 kfree(buffer);
1121out_free_attr:
1122 kfree(attr);
1123 return rc;
1124}
1125
1126static int mspro_block_init_card(struct memstick_dev *card)
1127{
1128 struct mspro_block_data *msb = memstick_get_drvdata(card);
1129 struct memstick_host *host = card->host;
1130 int rc = 0;
1131
Alex Dubove1f19992008-03-10 11:43:37 -07001132 msb->system = MEMSTICK_SYS_SERIAL;
Alex Dubovbaf85322008-02-09 10:20:54 -08001133 card->reg_addr.r_offset = offsetof(struct mspro_register, status);
1134 card->reg_addr.r_length = sizeof(struct ms_status_register);
1135 card->reg_addr.w_offset = offsetof(struct mspro_register, param);
1136 card->reg_addr.w_length = sizeof(struct mspro_param_register);
1137
1138 if (memstick_set_rw_addr(card))
1139 return -EIO;
1140
Alex Dubovead70772008-03-19 17:01:06 -07001141 msb->caps = host->caps;
Alex Dubov8e82f8c2008-09-13 02:33:26 -07001142
1143 msleep(150);
1144 rc = mspro_block_wait_for_ced(card);
1145 if (rc)
1146 return rc;
1147
Alex Dubov962ee1b2008-03-19 17:01:07 -07001148 rc = mspro_block_switch_interface(card);
1149 if (rc)
1150 return rc;
Alex Dubovbaf85322008-02-09 10:20:54 -08001151
Alex Dubovbaf85322008-02-09 10:20:54 -08001152 dev_dbg(&card->dev, "card activated\n");
Alex Dubovead70772008-03-19 17:01:06 -07001153 if (msb->system != MEMSTICK_SYS_SERIAL)
1154 msb->caps |= MEMSTICK_CAP_AUTO_GET_INT;
Alex Dubovbaf85322008-02-09 10:20:54 -08001155
1156 card->next_request = h_mspro_block_req_init;
1157 msb->mrq_handler = h_mspro_block_get_ro;
1158 memstick_init_req(&card->current_mrq, MS_TPC_READ_REG, NULL,
1159 sizeof(struct ms_status_register));
1160 memstick_new_req(card->host);
1161 wait_for_completion(&card->mrq_complete);
1162 if (card->current_mrq.error)
1163 return card->current_mrq.error;
1164
1165 dev_dbg(&card->dev, "card r/w status %d\n", msb->read_only ? 0 : 1);
1166
1167 msb->page_size = 512;
1168 rc = mspro_block_read_attributes(card);
1169 if (rc)
1170 return rc;
1171
1172 dev_dbg(&card->dev, "attributes loaded\n");
1173 return 0;
1174
1175}
1176
1177static int mspro_block_init_disk(struct memstick_dev *card)
1178{
1179 struct mspro_block_data *msb = memstick_get_drvdata(card);
1180 struct memstick_host *host = card->host;
1181 struct mspro_devinfo *dev_info = NULL;
1182 struct mspro_sys_info *sys_info = NULL;
1183 struct mspro_sys_attr *s_attr = NULL;
1184 int rc, disk_id;
1185 u64 limit = BLK_BOUNCE_HIGH;
1186 unsigned long capacity;
1187
Greg Kroah-Hartmanc4c66cf2008-03-04 00:13:36 +01001188 if (host->dev.dma_mask && *(host->dev.dma_mask))
1189 limit = *(host->dev.dma_mask);
Alex Dubovbaf85322008-02-09 10:20:54 -08001190
1191 for (rc = 0; msb->attr_group.attrs[rc]; ++rc) {
1192 s_attr = mspro_from_sysfs_attr(msb->attr_group.attrs[rc]);
1193
1194 if (s_attr->id == MSPRO_BLOCK_ID_DEVINFO)
1195 dev_info = s_attr->data;
1196 else if (s_attr->id == MSPRO_BLOCK_ID_SYSINFO)
1197 sys_info = s_attr->data;
1198 }
1199
1200 if (!dev_info || !sys_info)
1201 return -ENODEV;
1202
1203 msb->cylinders = be16_to_cpu(dev_info->cylinders);
1204 msb->heads = be16_to_cpu(dev_info->heads);
1205 msb->sectors_per_track = be16_to_cpu(dev_info->sectors_per_track);
1206
1207 msb->page_size = be16_to_cpu(sys_info->unit_size);
1208
Alex Dubovbaf85322008-02-09 10:20:54 -08001209 mutex_lock(&mspro_block_disk_lock);
Alex Dubovd8256d42011-01-12 17:01:04 -08001210 if (!idr_pre_get(&mspro_block_disk_idr, GFP_KERNEL)) {
1211 mutex_unlock(&mspro_block_disk_lock);
1212 return -ENOMEM;
1213 }
1214
Alex Dubovbaf85322008-02-09 10:20:54 -08001215 rc = idr_get_new(&mspro_block_disk_idr, card, &disk_id);
1216 mutex_unlock(&mspro_block_disk_lock);
1217
1218 if (rc)
1219 return rc;
1220
Alex Dubov8e82f8c2008-09-13 02:33:26 -07001221 if ((disk_id << MSPRO_BLOCK_PART_SHIFT) > 255) {
Alex Dubovbaf85322008-02-09 10:20:54 -08001222 rc = -ENOSPC;
1223 goto out_release_id;
1224 }
1225
Alex Dubov8e82f8c2008-09-13 02:33:26 -07001226 msb->disk = alloc_disk(1 << MSPRO_BLOCK_PART_SHIFT);
Alex Dubovbaf85322008-02-09 10:20:54 -08001227 if (!msb->disk) {
1228 rc = -ENOMEM;
1229 goto out_release_id;
1230 }
1231
Alex Dubovf1d82692008-07-25 19:45:02 -07001232 msb->queue = blk_init_queue(mspro_block_submit_req, &msb->q_lock);
Alex Dubovbaf85322008-02-09 10:20:54 -08001233 if (!msb->queue) {
1234 rc = -ENOMEM;
1235 goto out_put_disk;
1236 }
1237
1238 msb->queue->queuedata = card;
Alex Dubovf1d82692008-07-25 19:45:02 -07001239 blk_queue_prep_rq(msb->queue, mspro_block_prepare_req);
Alex Dubovbaf85322008-02-09 10:20:54 -08001240
1241 blk_queue_bounce_limit(msb->queue, limit);
Martin K. Petersen086fa5f2010-02-26 00:20:38 -05001242 blk_queue_max_hw_sectors(msb->queue, MSPRO_BLOCK_MAX_PAGES);
Martin K. Petersen8a783622010-02-26 00:20:39 -05001243 blk_queue_max_segments(msb->queue, MSPRO_BLOCK_MAX_SEGS);
Alex Dubovbaf85322008-02-09 10:20:54 -08001244 blk_queue_max_segment_size(msb->queue,
1245 MSPRO_BLOCK_MAX_PAGES * msb->page_size);
1246
1247 msb->disk->major = major;
Alex Dubov8e82f8c2008-09-13 02:33:26 -07001248 msb->disk->first_minor = disk_id << MSPRO_BLOCK_PART_SHIFT;
Alex Dubovbaf85322008-02-09 10:20:54 -08001249 msb->disk->fops = &ms_block_bdops;
1250 msb->usage_count = 1;
1251 msb->disk->private_data = msb;
1252 msb->disk->queue = msb->queue;
1253 msb->disk->driverfs_dev = &card->dev;
1254
1255 sprintf(msb->disk->disk_name, "mspblk%d", disk_id);
1256
Martin K. Petersene1defc42009-05-22 17:17:49 -04001257 blk_queue_logical_block_size(msb->queue, msb->page_size);
Alex Dubovbaf85322008-02-09 10:20:54 -08001258
1259 capacity = be16_to_cpu(sys_info->user_block_count);
1260 capacity *= be16_to_cpu(sys_info->block_size);
1261 capacity *= msb->page_size >> 9;
1262 set_capacity(msb->disk, capacity);
1263 dev_dbg(&card->dev, "capacity set %ld\n", capacity);
Alex Dubovbaf85322008-02-09 10:20:54 -08001264
Alex Dubovbaf85322008-02-09 10:20:54 -08001265 add_disk(msb->disk);
Alex Dubovbaf85322008-02-09 10:20:54 -08001266 msb->active = 1;
1267 return 0;
1268
1269out_put_disk:
1270 put_disk(msb->disk);
1271out_release_id:
1272 mutex_lock(&mspro_block_disk_lock);
1273 idr_remove(&mspro_block_disk_idr, disk_id);
1274 mutex_unlock(&mspro_block_disk_lock);
1275 return rc;
1276}
1277
1278static void mspro_block_data_clear(struct mspro_block_data *msb)
1279{
1280 int cnt;
1281 struct mspro_sys_attr *s_attr;
1282
1283 if (msb->attr_group.attrs) {
1284 for (cnt = 0; msb->attr_group.attrs[cnt]; ++cnt) {
1285 s_attr = mspro_from_sysfs_attr(msb->attr_group
1286 .attrs[cnt]);
1287 kfree(s_attr->data);
1288 kfree(s_attr);
1289 }
1290 kfree(msb->attr_group.attrs);
1291 }
1292
1293 msb->card = NULL;
1294}
1295
1296static int mspro_block_check_card(struct memstick_dev *card)
1297{
1298 struct mspro_block_data *msb = memstick_get_drvdata(card);
1299
1300 return (msb->active == 1);
1301}
1302
1303static int mspro_block_probe(struct memstick_dev *card)
1304{
1305 struct mspro_block_data *msb;
1306 int rc = 0;
1307
1308 msb = kzalloc(sizeof(struct mspro_block_data), GFP_KERNEL);
1309 if (!msb)
1310 return -ENOMEM;
1311 memstick_set_drvdata(card, msb);
1312 msb->card = card;
Alex Dubovf1d82692008-07-25 19:45:02 -07001313 spin_lock_init(&msb->q_lock);
Alex Dubovbaf85322008-02-09 10:20:54 -08001314
1315 rc = mspro_block_init_card(card);
1316
1317 if (rc)
1318 goto out_free;
1319
1320 rc = sysfs_create_group(&card->dev.kobj, &msb->attr_group);
1321 if (rc)
1322 goto out_free;
1323
1324 rc = mspro_block_init_disk(card);
1325 if (!rc) {
1326 card->check = mspro_block_check_card;
Alex Dubov17017d82008-07-25 19:45:01 -07001327 card->stop = mspro_block_stop;
1328 card->start = mspro_block_start;
Alex Dubovbaf85322008-02-09 10:20:54 -08001329 return 0;
1330 }
1331
1332 sysfs_remove_group(&card->dev.kobj, &msb->attr_group);
1333out_free:
1334 memstick_set_drvdata(card, NULL);
1335 mspro_block_data_clear(msb);
1336 kfree(msb);
1337 return rc;
1338}
1339
1340static void mspro_block_remove(struct memstick_dev *card)
1341{
1342 struct mspro_block_data *msb = memstick_get_drvdata(card);
Alex Dubovbaf85322008-02-09 10:20:54 -08001343 unsigned long flags;
1344
Alex Dubovbaf85322008-02-09 10:20:54 -08001345 spin_lock_irqsave(&msb->q_lock, flags);
Alex Dubovf1d82692008-07-25 19:45:02 -07001346 msb->eject = 1;
1347 blk_start_queue(msb->queue);
Alex Dubovbaf85322008-02-09 10:20:54 -08001348 spin_unlock_irqrestore(&msb->q_lock, flags);
1349
Maxim Levitskyd862b132010-08-11 14:17:52 -07001350 del_gendisk(msb->disk);
1351 dev_dbg(&card->dev, "mspro block remove\n");
1352
Alex Dubovbaf85322008-02-09 10:20:54 -08001353 blk_cleanup_queue(msb->queue);
Alex Dubovf1d82692008-07-25 19:45:02 -07001354 msb->queue = NULL;
Alex Dubovbaf85322008-02-09 10:20:54 -08001355
1356 sysfs_remove_group(&card->dev.kobj, &msb->attr_group);
1357
1358 mutex_lock(&mspro_block_disk_lock);
1359 mspro_block_data_clear(msb);
1360 mutex_unlock(&mspro_block_disk_lock);
1361
1362 mspro_block_disk_release(msb->disk);
1363 memstick_set_drvdata(card, NULL);
1364}
1365
1366#ifdef CONFIG_PM
1367
1368static int mspro_block_suspend(struct memstick_dev *card, pm_message_t state)
1369{
1370 struct mspro_block_data *msb = memstick_get_drvdata(card);
Alex Dubovbaf85322008-02-09 10:20:54 -08001371 unsigned long flags;
1372
1373 spin_lock_irqsave(&msb->q_lock, flags);
Alex Dubovbaf85322008-02-09 10:20:54 -08001374 blk_stop_queue(msb->queue);
Alex Dubovf1d82692008-07-25 19:45:02 -07001375 msb->active = 0;
Alex Dubovbaf85322008-02-09 10:20:54 -08001376 spin_unlock_irqrestore(&msb->q_lock, flags);
1377
Alex Dubovbaf85322008-02-09 10:20:54 -08001378 return 0;
1379}
1380
1381static int mspro_block_resume(struct memstick_dev *card)
1382{
1383 struct mspro_block_data *msb = memstick_get_drvdata(card);
1384 unsigned long flags;
1385 int rc = 0;
1386
1387#ifdef CONFIG_MEMSTICK_UNSAFE_RESUME
1388
1389 struct mspro_block_data *new_msb;
1390 struct memstick_host *host = card->host;
1391 struct mspro_sys_attr *s_attr, *r_attr;
1392 unsigned char cnt;
1393
1394 mutex_lock(&host->lock);
1395 new_msb = kzalloc(sizeof(struct mspro_block_data), GFP_KERNEL);
1396 if (!new_msb) {
1397 rc = -ENOMEM;
1398 goto out_unlock;
1399 }
1400
1401 new_msb->card = card;
1402 memstick_set_drvdata(card, new_msb);
1403 if (mspro_block_init_card(card))
1404 goto out_free;
1405
1406 for (cnt = 0; new_msb->attr_group.attrs[cnt]
1407 && msb->attr_group.attrs[cnt]; ++cnt) {
1408 s_attr = mspro_from_sysfs_attr(new_msb->attr_group.attrs[cnt]);
1409 r_attr = mspro_from_sysfs_attr(msb->attr_group.attrs[cnt]);
1410
1411 if (s_attr->id == MSPRO_BLOCK_ID_SYSINFO
1412 && r_attr->id == s_attr->id) {
1413 if (memcmp(s_attr->data, r_attr->data, s_attr->size))
1414 break;
1415
Alex Dubovf1d82692008-07-25 19:45:02 -07001416 msb->active = 1;
Alex Dubovbaf85322008-02-09 10:20:54 -08001417 break;
1418 }
1419 }
1420
1421out_free:
1422 memstick_set_drvdata(card, msb);
1423 mspro_block_data_clear(new_msb);
1424 kfree(new_msb);
1425out_unlock:
1426 mutex_unlock(&host->lock);
1427
1428#endif /* CONFIG_MEMSTICK_UNSAFE_RESUME */
1429
1430 spin_lock_irqsave(&msb->q_lock, flags);
1431 blk_start_queue(msb->queue);
1432 spin_unlock_irqrestore(&msb->q_lock, flags);
1433 return rc;
1434}
1435
1436#else
1437
1438#define mspro_block_suspend NULL
1439#define mspro_block_resume NULL
1440
1441#endif /* CONFIG_PM */
1442
1443static struct memstick_device_id mspro_block_id_tbl[] = {
1444 {MEMSTICK_MATCH_ALL, MEMSTICK_TYPE_PRO, MEMSTICK_CATEGORY_STORAGE_DUO,
Alex Dubov8e82f8c2008-09-13 02:33:26 -07001445 MEMSTICK_CLASS_DUO},
Alex Dubovbaf85322008-02-09 10:20:54 -08001446 {}
1447};
1448
1449
1450static struct memstick_driver mspro_block_driver = {
1451 .driver = {
1452 .name = DRIVER_NAME,
1453 .owner = THIS_MODULE
1454 },
1455 .id_table = mspro_block_id_tbl,
1456 .probe = mspro_block_probe,
1457 .remove = mspro_block_remove,
1458 .suspend = mspro_block_suspend,
1459 .resume = mspro_block_resume
1460};
1461
1462static int __init mspro_block_init(void)
1463{
1464 int rc = -ENOMEM;
1465
1466 rc = register_blkdev(major, DRIVER_NAME);
1467 if (rc < 0) {
1468 printk(KERN_ERR DRIVER_NAME ": failed to register "
1469 "major %d, error %d\n", major, rc);
1470 return rc;
1471 }
1472 if (!major)
1473 major = rc;
1474
1475 rc = memstick_register_driver(&mspro_block_driver);
1476 if (rc)
1477 unregister_blkdev(major, DRIVER_NAME);
1478 return rc;
1479}
1480
1481static void __exit mspro_block_exit(void)
1482{
1483 memstick_unregister_driver(&mspro_block_driver);
1484 unregister_blkdev(major, DRIVER_NAME);
1485 idr_destroy(&mspro_block_disk_idr);
1486}
1487
1488module_init(mspro_block_init);
1489module_exit(mspro_block_exit);
1490
1491MODULE_LICENSE("GPL");
1492MODULE_AUTHOR("Alex Dubov");
1493MODULE_DESCRIPTION("Sony MemoryStickPro block device driver");
1494MODULE_DEVICE_TABLE(memstick, mspro_block_id_tbl);