diff options
Diffstat (limited to 'drivers/memstick/core/mspro_block.c')
-rw-r--r-- | drivers/memstick/core/mspro_block.c | 1351 |
1 files changed, 1351 insertions, 0 deletions
diff --git a/drivers/memstick/core/mspro_block.c b/drivers/memstick/core/mspro_block.c new file mode 100644 index 000000000000..423ad8cf4bb9 --- /dev/null +++ b/drivers/memstick/core/mspro_block.c | |||
@@ -0,0 +1,1351 @@ | |||
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/memstick.h> | ||
20 | |||
21 | #define DRIVER_NAME "mspro_block" | ||
22 | #define DRIVER_VERSION "0.2" | ||
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 | enum { | ||
34 | MSPRO_BLOCK_ID_SYSINFO = 0x10, | ||
35 | MSPRO_BLOCK_ID_MODELNAME = 0x15, | ||
36 | MSPRO_BLOCK_ID_MBR = 0x20, | ||
37 | MSPRO_BLOCK_ID_PBR16 = 0x21, | ||
38 | MSPRO_BLOCK_ID_PBR32 = 0x22, | ||
39 | MSPRO_BLOCK_ID_SPECFILEVALUES1 = 0x25, | ||
40 | MSPRO_BLOCK_ID_SPECFILEVALUES2 = 0x26, | ||
41 | MSPRO_BLOCK_ID_DEVINFO = 0x30 | ||
42 | }; | ||
43 | |||
44 | struct mspro_sys_attr { | ||
45 | size_t size; | ||
46 | void *data; | ||
47 | unsigned char id; | ||
48 | char name[32]; | ||
49 | struct device_attribute dev_attr; | ||
50 | }; | ||
51 | |||
52 | struct mspro_attr_entry { | ||
53 | unsigned int address; | ||
54 | unsigned int size; | ||
55 | unsigned char id; | ||
56 | unsigned char reserved[3]; | ||
57 | } __attribute__((packed)); | ||
58 | |||
59 | struct mspro_attribute { | ||
60 | unsigned short signature; | ||
61 | unsigned short version; | ||
62 | unsigned char count; | ||
63 | unsigned char reserved[11]; | ||
64 | struct mspro_attr_entry entries[]; | ||
65 | } __attribute__((packed)); | ||
66 | |||
67 | struct mspro_sys_info { | ||
68 | unsigned char class; | ||
69 | unsigned char reserved0; | ||
70 | unsigned short block_size; | ||
71 | unsigned short block_count; | ||
72 | unsigned short user_block_count; | ||
73 | unsigned short page_size; | ||
74 | unsigned char reserved1[2]; | ||
75 | unsigned char assembly_date[8]; | ||
76 | unsigned int serial_number; | ||
77 | unsigned char assembly_maker_code; | ||
78 | unsigned char assembly_model_code[3]; | ||
79 | unsigned short memory_maker_code; | ||
80 | unsigned short memory_model_code; | ||
81 | unsigned char reserved2[4]; | ||
82 | unsigned char vcc; | ||
83 | unsigned char vpp; | ||
84 | unsigned short controller_number; | ||
85 | unsigned short controller_function; | ||
86 | unsigned short start_sector; | ||
87 | unsigned short unit_size; | ||
88 | unsigned char ms_sub_class; | ||
89 | unsigned char reserved3[4]; | ||
90 | unsigned char interface_type; | ||
91 | unsigned short controller_code; | ||
92 | unsigned char format_type; | ||
93 | unsigned char reserved4; | ||
94 | unsigned char device_type; | ||
95 | unsigned char reserved5[7]; | ||
96 | unsigned char mspro_id[16]; | ||
97 | unsigned char reserved6[16]; | ||
98 | } __attribute__((packed)); | ||
99 | |||
100 | struct mspro_mbr { | ||
101 | unsigned char boot_partition; | ||
102 | unsigned char start_head; | ||
103 | unsigned char start_sector; | ||
104 | unsigned char start_cylinder; | ||
105 | unsigned char partition_type; | ||
106 | unsigned char end_head; | ||
107 | unsigned char end_sector; | ||
108 | unsigned char end_cylinder; | ||
109 | unsigned int start_sectors; | ||
110 | unsigned int sectors_per_partition; | ||
111 | } __attribute__((packed)); | ||
112 | |||
113 | struct mspro_devinfo { | ||
114 | unsigned short cylinders; | ||
115 | unsigned short heads; | ||
116 | unsigned short bytes_per_track; | ||
117 | unsigned short bytes_per_sector; | ||
118 | unsigned short sectors_per_track; | ||
119 | unsigned char reserved[6]; | ||
120 | } __attribute__((packed)); | ||
121 | |||
122 | struct mspro_block_data { | ||
123 | struct memstick_dev *card; | ||
124 | unsigned int usage_count; | ||
125 | struct gendisk *disk; | ||
126 | struct request_queue *queue; | ||
127 | spinlock_t q_lock; | ||
128 | wait_queue_head_t q_wait; | ||
129 | struct task_struct *q_thread; | ||
130 | |||
131 | unsigned short page_size; | ||
132 | unsigned short cylinders; | ||
133 | unsigned short heads; | ||
134 | unsigned short sectors_per_track; | ||
135 | |||
136 | unsigned char system; | ||
137 | unsigned char read_only:1, | ||
138 | active:1, | ||
139 | has_request:1, | ||
140 | data_dir:1; | ||
141 | unsigned char transfer_cmd; | ||
142 | |||
143 | int (*mrq_handler)(struct memstick_dev *card, | ||
144 | struct memstick_request **mrq); | ||
145 | |||
146 | struct attribute_group attr_group; | ||
147 | |||
148 | struct scatterlist req_sg[MSPRO_BLOCK_MAX_SEGS]; | ||
149 | unsigned int seg_count; | ||
150 | unsigned int current_seg; | ||
151 | unsigned short current_page; | ||
152 | }; | ||
153 | |||
154 | static DEFINE_IDR(mspro_block_disk_idr); | ||
155 | static DEFINE_MUTEX(mspro_block_disk_lock); | ||
156 | |||
157 | /*** Block device ***/ | ||
158 | |||
159 | static int mspro_block_bd_open(struct inode *inode, struct file *filp) | ||
160 | { | ||
161 | struct gendisk *disk = inode->i_bdev->bd_disk; | ||
162 | struct mspro_block_data *msb = disk->private_data; | ||
163 | int rc = -ENXIO; | ||
164 | |||
165 | mutex_lock(&mspro_block_disk_lock); | ||
166 | |||
167 | if (msb && msb->card) { | ||
168 | msb->usage_count++; | ||
169 | if ((filp->f_mode & FMODE_WRITE) && msb->read_only) | ||
170 | rc = -EROFS; | ||
171 | else | ||
172 | rc = 0; | ||
173 | } | ||
174 | |||
175 | mutex_unlock(&mspro_block_disk_lock); | ||
176 | |||
177 | return rc; | ||
178 | } | ||
179 | |||
180 | |||
181 | static int mspro_block_disk_release(struct gendisk *disk) | ||
182 | { | ||
183 | struct mspro_block_data *msb = disk->private_data; | ||
184 | int disk_id = disk->first_minor >> MEMSTICK_PART_SHIFT; | ||
185 | |||
186 | mutex_lock(&mspro_block_disk_lock); | ||
187 | |||
188 | if (msb->usage_count) { | ||
189 | msb->usage_count--; | ||
190 | if (!msb->usage_count) { | ||
191 | kfree(msb); | ||
192 | disk->private_data = NULL; | ||
193 | idr_remove(&mspro_block_disk_idr, disk_id); | ||
194 | put_disk(disk); | ||
195 | } | ||
196 | } | ||
197 | |||
198 | mutex_unlock(&mspro_block_disk_lock); | ||
199 | |||
200 | return 0; | ||
201 | } | ||
202 | |||
203 | static int mspro_block_bd_release(struct inode *inode, struct file *filp) | ||
204 | { | ||
205 | struct gendisk *disk = inode->i_bdev->bd_disk; | ||
206 | return mspro_block_disk_release(disk); | ||
207 | } | ||
208 | |||
209 | static int mspro_block_bd_getgeo(struct block_device *bdev, | ||
210 | struct hd_geometry *geo) | ||
211 | { | ||
212 | struct mspro_block_data *msb = bdev->bd_disk->private_data; | ||
213 | |||
214 | geo->heads = msb->heads; | ||
215 | geo->sectors = msb->sectors_per_track; | ||
216 | geo->cylinders = msb->cylinders; | ||
217 | |||
218 | return 0; | ||
219 | } | ||
220 | |||
221 | static struct block_device_operations ms_block_bdops = { | ||
222 | .open = mspro_block_bd_open, | ||
223 | .release = mspro_block_bd_release, | ||
224 | .getgeo = mspro_block_bd_getgeo, | ||
225 | .owner = THIS_MODULE | ||
226 | }; | ||
227 | |||
228 | /*** Information ***/ | ||
229 | |||
230 | static struct mspro_sys_attr *mspro_from_sysfs_attr(struct attribute *attr) | ||
231 | { | ||
232 | struct device_attribute *dev_attr | ||
233 | = container_of(attr, struct device_attribute, attr); | ||
234 | return container_of(dev_attr, struct mspro_sys_attr, dev_attr); | ||
235 | } | ||
236 | |||
237 | static const char *mspro_block_attr_name(unsigned char tag) | ||
238 | { | ||
239 | switch (tag) { | ||
240 | case MSPRO_BLOCK_ID_SYSINFO: | ||
241 | return "attr_sysinfo"; | ||
242 | case MSPRO_BLOCK_ID_MODELNAME: | ||
243 | return "attr_modelname"; | ||
244 | case MSPRO_BLOCK_ID_MBR: | ||
245 | return "attr_mbr"; | ||
246 | case MSPRO_BLOCK_ID_PBR16: | ||
247 | return "attr_pbr16"; | ||
248 | case MSPRO_BLOCK_ID_PBR32: | ||
249 | return "attr_pbr32"; | ||
250 | case MSPRO_BLOCK_ID_SPECFILEVALUES1: | ||
251 | return "attr_specfilevalues1"; | ||
252 | case MSPRO_BLOCK_ID_SPECFILEVALUES2: | ||
253 | return "attr_specfilevalues2"; | ||
254 | case MSPRO_BLOCK_ID_DEVINFO: | ||
255 | return "attr_devinfo"; | ||
256 | default: | ||
257 | return NULL; | ||
258 | }; | ||
259 | } | ||
260 | |||
261 | typedef ssize_t (*sysfs_show_t)(struct device *dev, | ||
262 | struct device_attribute *attr, | ||
263 | char *buffer); | ||
264 | |||
265 | static ssize_t mspro_block_attr_show_default(struct device *dev, | ||
266 | struct device_attribute *attr, | ||
267 | char *buffer) | ||
268 | { | ||
269 | struct mspro_sys_attr *s_attr = container_of(attr, | ||
270 | struct mspro_sys_attr, | ||
271 | dev_attr); | ||
272 | |||
273 | ssize_t cnt, rc = 0; | ||
274 | |||
275 | for (cnt = 0; cnt < s_attr->size; cnt++) { | ||
276 | if (cnt && !(cnt % 16)) { | ||
277 | if (PAGE_SIZE - rc) | ||
278 | buffer[rc++] = '\n'; | ||
279 | } | ||
280 | |||
281 | rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "%02x ", | ||
282 | ((unsigned char *)s_attr->data)[cnt]); | ||
283 | } | ||
284 | return rc; | ||
285 | } | ||
286 | |||
287 | static ssize_t mspro_block_attr_show_sysinfo(struct device *dev, | ||
288 | struct device_attribute *attr, | ||
289 | char *buffer) | ||
290 | { | ||
291 | struct mspro_sys_attr *x_attr = container_of(attr, | ||
292 | struct mspro_sys_attr, | ||
293 | dev_attr); | ||
294 | struct mspro_sys_info *x_sys = x_attr->data; | ||
295 | ssize_t rc = 0; | ||
296 | |||
297 | rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "class: %x\n", | ||
298 | x_sys->class); | ||
299 | rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "block size: %x\n", | ||
300 | be16_to_cpu(x_sys->block_size)); | ||
301 | rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "block count: %x\n", | ||
302 | be16_to_cpu(x_sys->block_count)); | ||
303 | rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "user block count: %x\n", | ||
304 | be16_to_cpu(x_sys->user_block_count)); | ||
305 | rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "page size: %x\n", | ||
306 | be16_to_cpu(x_sys->page_size)); | ||
307 | rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "assembly date: " | ||
308 | "%d %04u-%02u-%02u %02u:%02u:%02u\n", | ||
309 | x_sys->assembly_date[0], | ||
310 | be16_to_cpu(*(unsigned short *) | ||
311 | &x_sys->assembly_date[1]), | ||
312 | x_sys->assembly_date[3], x_sys->assembly_date[4], | ||
313 | x_sys->assembly_date[5], x_sys->assembly_date[6], | ||
314 | x_sys->assembly_date[7]); | ||
315 | rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "serial number: %x\n", | ||
316 | be32_to_cpu(x_sys->serial_number)); | ||
317 | rc += scnprintf(buffer + rc, PAGE_SIZE - rc, | ||
318 | "assembly maker code: %x\n", | ||
319 | x_sys->assembly_maker_code); | ||
320 | rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "assembly model code: " | ||
321 | "%02x%02x%02x\n", x_sys->assembly_model_code[0], | ||
322 | x_sys->assembly_model_code[1], | ||
323 | x_sys->assembly_model_code[2]); | ||
324 | rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "memory maker code: %x\n", | ||
325 | be16_to_cpu(x_sys->memory_maker_code)); | ||
326 | rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "memory model code: %x\n", | ||
327 | be16_to_cpu(x_sys->memory_model_code)); | ||
328 | rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "vcc: %x\n", | ||
329 | x_sys->vcc); | ||
330 | rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "vpp: %x\n", | ||
331 | x_sys->vpp); | ||
332 | rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "controller number: %x\n", | ||
333 | be16_to_cpu(x_sys->controller_number)); | ||
334 | rc += scnprintf(buffer + rc, PAGE_SIZE - rc, | ||
335 | "controller function: %x\n", | ||
336 | be16_to_cpu(x_sys->controller_function)); | ||
337 | rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "start sector: %x\n", | ||
338 | be16_to_cpu(x_sys->start_sector)); | ||
339 | rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "unit size: %x\n", | ||
340 | be16_to_cpu(x_sys->unit_size)); | ||
341 | rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "sub class: %x\n", | ||
342 | x_sys->ms_sub_class); | ||
343 | rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "interface type: %x\n", | ||
344 | x_sys->interface_type); | ||
345 | rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "controller code: %x\n", | ||
346 | be16_to_cpu(x_sys->controller_code)); | ||
347 | rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "format type: %x\n", | ||
348 | x_sys->format_type); | ||
349 | rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "device type: %x\n", | ||
350 | x_sys->device_type); | ||
351 | rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "mspro id: %s\n", | ||
352 | x_sys->mspro_id); | ||
353 | return rc; | ||
354 | } | ||
355 | |||
356 | static ssize_t mspro_block_attr_show_modelname(struct device *dev, | ||
357 | struct device_attribute *attr, | ||
358 | char *buffer) | ||
359 | { | ||
360 | struct mspro_sys_attr *s_attr = container_of(attr, | ||
361 | struct mspro_sys_attr, | ||
362 | dev_attr); | ||
363 | |||
364 | return scnprintf(buffer, PAGE_SIZE, "%s", (char *)s_attr->data); | ||
365 | } | ||
366 | |||
367 | static ssize_t mspro_block_attr_show_mbr(struct device *dev, | ||
368 | struct device_attribute *attr, | ||
369 | char *buffer) | ||
370 | { | ||
371 | struct mspro_sys_attr *x_attr = container_of(attr, | ||
372 | struct mspro_sys_attr, | ||
373 | dev_attr); | ||
374 | struct mspro_mbr *x_mbr = x_attr->data; | ||
375 | ssize_t rc = 0; | ||
376 | |||
377 | rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "boot partition: %x\n", | ||
378 | x_mbr->boot_partition); | ||
379 | rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "start head: %x\n", | ||
380 | x_mbr->start_head); | ||
381 | rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "start sector: %x\n", | ||
382 | x_mbr->start_sector); | ||
383 | rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "start cylinder: %x\n", | ||
384 | x_mbr->start_cylinder); | ||
385 | rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "partition type: %x\n", | ||
386 | x_mbr->partition_type); | ||
387 | rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "end head: %x\n", | ||
388 | x_mbr->end_head); | ||
389 | rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "end sector: %x\n", | ||
390 | x_mbr->end_sector); | ||
391 | rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "end cylinder: %x\n", | ||
392 | x_mbr->end_cylinder); | ||
393 | rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "start sectors: %x\n", | ||
394 | x_mbr->start_sectors); | ||
395 | rc += scnprintf(buffer + rc, PAGE_SIZE - rc, | ||
396 | "sectors per partition: %x\n", | ||
397 | x_mbr->sectors_per_partition); | ||
398 | return rc; | ||
399 | } | ||
400 | |||
401 | static ssize_t mspro_block_attr_show_devinfo(struct device *dev, | ||
402 | struct device_attribute *attr, | ||
403 | char *buffer) | ||
404 | { | ||
405 | struct mspro_sys_attr *x_attr = container_of(attr, | ||
406 | struct mspro_sys_attr, | ||
407 | dev_attr); | ||
408 | struct mspro_devinfo *x_devinfo = x_attr->data; | ||
409 | ssize_t rc = 0; | ||
410 | |||
411 | rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "cylinders: %x\n", | ||
412 | be16_to_cpu(x_devinfo->cylinders)); | ||
413 | rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "heads: %x\n", | ||
414 | be16_to_cpu(x_devinfo->heads)); | ||
415 | rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "bytes per track: %x\n", | ||
416 | be16_to_cpu(x_devinfo->bytes_per_track)); | ||
417 | rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "bytes per sector: %x\n", | ||
418 | be16_to_cpu(x_devinfo->bytes_per_sector)); | ||
419 | rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "sectors per track: %x\n", | ||
420 | be16_to_cpu(x_devinfo->sectors_per_track)); | ||
421 | return rc; | ||
422 | } | ||
423 | |||
424 | static sysfs_show_t mspro_block_attr_show(unsigned char tag) | ||
425 | { | ||
426 | switch (tag) { | ||
427 | case MSPRO_BLOCK_ID_SYSINFO: | ||
428 | return mspro_block_attr_show_sysinfo; | ||
429 | case MSPRO_BLOCK_ID_MODELNAME: | ||
430 | return mspro_block_attr_show_modelname; | ||
431 | case MSPRO_BLOCK_ID_MBR: | ||
432 | return mspro_block_attr_show_mbr; | ||
433 | case MSPRO_BLOCK_ID_DEVINFO: | ||
434 | return mspro_block_attr_show_devinfo; | ||
435 | default: | ||
436 | return mspro_block_attr_show_default; | ||
437 | } | ||
438 | } | ||
439 | |||
440 | /*** Protocol handlers ***/ | ||
441 | |||
442 | /* | ||
443 | * Functions prefixed with "h_" are protocol callbacks. They can be called from | ||
444 | * interrupt context. Return value of 0 means that request processing is still | ||
445 | * ongoing, while special error value of -EAGAIN means that current request is | ||
446 | * finished (and request processor should come back some time later). | ||
447 | */ | ||
448 | |||
449 | static int h_mspro_block_req_init(struct memstick_dev *card, | ||
450 | struct memstick_request **mrq) | ||
451 | { | ||
452 | struct mspro_block_data *msb = memstick_get_drvdata(card); | ||
453 | |||
454 | *mrq = &card->current_mrq; | ||
455 | card->next_request = msb->mrq_handler; | ||
456 | return 0; | ||
457 | } | ||
458 | |||
459 | static int h_mspro_block_default(struct memstick_dev *card, | ||
460 | struct memstick_request **mrq) | ||
461 | { | ||
462 | complete(&card->mrq_complete); | ||
463 | if (!(*mrq)->error) | ||
464 | return -EAGAIN; | ||
465 | else | ||
466 | return (*mrq)->error; | ||
467 | } | ||
468 | |||
469 | static int h_mspro_block_get_ro(struct memstick_dev *card, | ||
470 | struct memstick_request **mrq) | ||
471 | { | ||
472 | struct mspro_block_data *msb = memstick_get_drvdata(card); | ||
473 | |||
474 | if ((*mrq)->error) { | ||
475 | complete(&card->mrq_complete); | ||
476 | return (*mrq)->error; | ||
477 | } | ||
478 | |||
479 | if ((*mrq)->data[offsetof(struct ms_status_register, status0)] | ||
480 | & MEMSTICK_STATUS0_WP) | ||
481 | msb->read_only = 1; | ||
482 | else | ||
483 | msb->read_only = 0; | ||
484 | |||
485 | complete(&card->mrq_complete); | ||
486 | return -EAGAIN; | ||
487 | } | ||
488 | |||
489 | static int h_mspro_block_wait_for_ced(struct memstick_dev *card, | ||
490 | struct memstick_request **mrq) | ||
491 | { | ||
492 | if ((*mrq)->error) { | ||
493 | complete(&card->mrq_complete); | ||
494 | return (*mrq)->error; | ||
495 | } | ||
496 | |||
497 | dev_dbg(&card->dev, "wait for ced: value %x\n", (*mrq)->data[0]); | ||
498 | |||
499 | if ((*mrq)->data[0] & (MEMSTICK_INT_CMDNAK | MEMSTICK_INT_ERR)) { | ||
500 | card->current_mrq.error = -EFAULT; | ||
501 | complete(&card->mrq_complete); | ||
502 | return card->current_mrq.error; | ||
503 | } | ||
504 | |||
505 | if (!((*mrq)->data[0] & MEMSTICK_INT_CED)) | ||
506 | return 0; | ||
507 | else { | ||
508 | card->current_mrq.error = 0; | ||
509 | complete(&card->mrq_complete); | ||
510 | return -EAGAIN; | ||
511 | } | ||
512 | } | ||
513 | |||
514 | static int h_mspro_block_transfer_data(struct memstick_dev *card, | ||
515 | struct memstick_request **mrq) | ||
516 | { | ||
517 | struct memstick_host *host = card->host; | ||
518 | struct mspro_block_data *msb = memstick_get_drvdata(card); | ||
519 | unsigned char t_val = 0; | ||
520 | struct scatterlist t_sg = { 0 }; | ||
521 | size_t t_offset; | ||
522 | |||
523 | if ((*mrq)->error) { | ||
524 | complete(&card->mrq_complete); | ||
525 | return (*mrq)->error; | ||
526 | } | ||
527 | |||
528 | switch ((*mrq)->tpc) { | ||
529 | case MS_TPC_WRITE_REG: | ||
530 | memstick_init_req(*mrq, MS_TPC_SET_CMD, &msb->transfer_cmd, 1); | ||
531 | (*mrq)->get_int_reg = 1; | ||
532 | return 0; | ||
533 | case MS_TPC_SET_CMD: | ||
534 | t_val = (*mrq)->int_reg; | ||
535 | memstick_init_req(*mrq, MS_TPC_GET_INT, NULL, 1); | ||
536 | if (host->caps & MEMSTICK_CAP_AUTO_GET_INT) | ||
537 | goto has_int_reg; | ||
538 | return 0; | ||
539 | case MS_TPC_GET_INT: | ||
540 | t_val = (*mrq)->data[0]; | ||
541 | has_int_reg: | ||
542 | if (t_val & (MEMSTICK_INT_CMDNAK | MEMSTICK_INT_ERR)) { | ||
543 | t_val = MSPRO_CMD_STOP; | ||
544 | memstick_init_req(*mrq, MS_TPC_SET_CMD, &t_val, 1); | ||
545 | card->next_request = h_mspro_block_default; | ||
546 | return 0; | ||
547 | } | ||
548 | |||
549 | if (msb->current_page | ||
550 | == (msb->req_sg[msb->current_seg].length | ||
551 | / msb->page_size)) { | ||
552 | msb->current_page = 0; | ||
553 | msb->current_seg++; | ||
554 | |||
555 | if (msb->current_seg == msb->seg_count) { | ||
556 | if (t_val & MEMSTICK_INT_CED) { | ||
557 | complete(&card->mrq_complete); | ||
558 | return -EAGAIN; | ||
559 | } else { | ||
560 | card->next_request | ||
561 | = h_mspro_block_wait_for_ced; | ||
562 | memstick_init_req(*mrq, MS_TPC_GET_INT, | ||
563 | NULL, 1); | ||
564 | return 0; | ||
565 | } | ||
566 | } | ||
567 | } | ||
568 | |||
569 | if (!(t_val & MEMSTICK_INT_BREQ)) { | ||
570 | memstick_init_req(*mrq, MS_TPC_GET_INT, NULL, 1); | ||
571 | return 0; | ||
572 | } | ||
573 | |||
574 | t_offset = msb->req_sg[msb->current_seg].offset; | ||
575 | t_offset += msb->current_page * msb->page_size; | ||
576 | |||
577 | sg_set_page(&t_sg, | ||
578 | nth_page(sg_page(&(msb->req_sg[msb->current_seg])), | ||
579 | t_offset >> PAGE_SHIFT), | ||
580 | msb->page_size, offset_in_page(t_offset)); | ||
581 | |||
582 | memstick_init_req_sg(*mrq, msb->data_dir == READ | ||
583 | ? MS_TPC_READ_LONG_DATA | ||
584 | : MS_TPC_WRITE_LONG_DATA, | ||
585 | &t_sg); | ||
586 | (*mrq)->get_int_reg = 1; | ||
587 | return 0; | ||
588 | case MS_TPC_READ_LONG_DATA: | ||
589 | case MS_TPC_WRITE_LONG_DATA: | ||
590 | msb->current_page++; | ||
591 | if (host->caps & MEMSTICK_CAP_AUTO_GET_INT) { | ||
592 | t_val = (*mrq)->int_reg; | ||
593 | goto has_int_reg; | ||
594 | } else { | ||
595 | memstick_init_req(*mrq, MS_TPC_GET_INT, NULL, 1); | ||
596 | return 0; | ||
597 | } | ||
598 | |||
599 | default: | ||
600 | BUG(); | ||
601 | } | ||
602 | } | ||
603 | |||
604 | /*** Data transfer ***/ | ||
605 | |||
606 | static void mspro_block_process_request(struct memstick_dev *card, | ||
607 | struct request *req) | ||
608 | { | ||
609 | struct mspro_block_data *msb = memstick_get_drvdata(card); | ||
610 | struct mspro_param_register param; | ||
611 | int rc, chunk, cnt; | ||
612 | unsigned short page_count; | ||
613 | sector_t t_sec; | ||
614 | unsigned long flags; | ||
615 | |||
616 | do { | ||
617 | page_count = 0; | ||
618 | msb->current_seg = 0; | ||
619 | msb->seg_count = blk_rq_map_sg(req->q, req, msb->req_sg); | ||
620 | |||
621 | if (msb->seg_count) { | ||
622 | msb->current_page = 0; | ||
623 | for (rc = 0; rc < msb->seg_count; rc++) | ||
624 | page_count += msb->req_sg[rc].length | ||
625 | / msb->page_size; | ||
626 | |||
627 | t_sec = req->sector; | ||
628 | sector_div(t_sec, msb->page_size >> 9); | ||
629 | param.system = msb->system; | ||
630 | param.data_count = cpu_to_be16(page_count); | ||
631 | param.data_address = cpu_to_be32((uint32_t)t_sec); | ||
632 | param.cmd_param = 0; | ||
633 | |||
634 | msb->data_dir = rq_data_dir(req); | ||
635 | msb->transfer_cmd = msb->data_dir == READ | ||
636 | ? MSPRO_CMD_READ_DATA | ||
637 | : MSPRO_CMD_WRITE_DATA; | ||
638 | |||
639 | dev_dbg(&card->dev, "data transfer: cmd %x, " | ||
640 | "lba %x, count %x\n", msb->transfer_cmd, | ||
641 | be32_to_cpu(param.data_address), | ||
642 | page_count); | ||
643 | |||
644 | card->next_request = h_mspro_block_req_init; | ||
645 | msb->mrq_handler = h_mspro_block_transfer_data; | ||
646 | memstick_init_req(&card->current_mrq, MS_TPC_WRITE_REG, | ||
647 | ¶m, sizeof(param)); | ||
648 | memstick_new_req(card->host); | ||
649 | wait_for_completion(&card->mrq_complete); | ||
650 | rc = card->current_mrq.error; | ||
651 | |||
652 | if (rc || (card->current_mrq.tpc == MSPRO_CMD_STOP)) { | ||
653 | for (cnt = 0; cnt < msb->current_seg; cnt++) | ||
654 | page_count += msb->req_sg[cnt].length | ||
655 | / msb->page_size; | ||
656 | |||
657 | if (msb->current_page) | ||
658 | page_count += msb->current_page - 1; | ||
659 | |||
660 | if (page_count && (msb->data_dir == READ)) | ||
661 | rc = msb->page_size * page_count; | ||
662 | else | ||
663 | rc = -EIO; | ||
664 | } else | ||
665 | rc = msb->page_size * page_count; | ||
666 | } else | ||
667 | rc = -EFAULT; | ||
668 | |||
669 | spin_lock_irqsave(&msb->q_lock, flags); | ||
670 | if (rc >= 0) | ||
671 | chunk = __blk_end_request(req, 0, rc); | ||
672 | else | ||
673 | chunk = __blk_end_request(req, rc, 0); | ||
674 | |||
675 | dev_dbg(&card->dev, "end chunk %d, %d\n", rc, chunk); | ||
676 | spin_unlock_irqrestore(&msb->q_lock, flags); | ||
677 | } while (chunk); | ||
678 | } | ||
679 | |||
680 | static int mspro_block_has_request(struct mspro_block_data *msb) | ||
681 | { | ||
682 | int rc = 0; | ||
683 | unsigned long flags; | ||
684 | |||
685 | spin_lock_irqsave(&msb->q_lock, flags); | ||
686 | if (kthread_should_stop() || msb->has_request) | ||
687 | rc = 1; | ||
688 | spin_unlock_irqrestore(&msb->q_lock, flags); | ||
689 | return rc; | ||
690 | } | ||
691 | |||
692 | static int mspro_block_queue_thread(void *data) | ||
693 | { | ||
694 | struct memstick_dev *card = data; | ||
695 | struct memstick_host *host = card->host; | ||
696 | struct mspro_block_data *msb = memstick_get_drvdata(card); | ||
697 | struct request *req; | ||
698 | unsigned long flags; | ||
699 | |||
700 | while (1) { | ||
701 | wait_event(msb->q_wait, mspro_block_has_request(msb)); | ||
702 | dev_dbg(&card->dev, "thread iter\n"); | ||
703 | |||
704 | spin_lock_irqsave(&msb->q_lock, flags); | ||
705 | req = elv_next_request(msb->queue); | ||
706 | dev_dbg(&card->dev, "next req %p\n", req); | ||
707 | if (!req) { | ||
708 | msb->has_request = 0; | ||
709 | if (kthread_should_stop()) { | ||
710 | spin_unlock_irqrestore(&msb->q_lock, flags); | ||
711 | break; | ||
712 | } | ||
713 | } else | ||
714 | msb->has_request = 1; | ||
715 | spin_unlock_irqrestore(&msb->q_lock, flags); | ||
716 | |||
717 | if (req) { | ||
718 | mutex_lock(&host->lock); | ||
719 | mspro_block_process_request(card, req); | ||
720 | mutex_unlock(&host->lock); | ||
721 | } | ||
722 | } | ||
723 | dev_dbg(&card->dev, "thread finished\n"); | ||
724 | return 0; | ||
725 | } | ||
726 | |||
727 | static void mspro_block_request(struct request_queue *q) | ||
728 | { | ||
729 | struct memstick_dev *card = q->queuedata; | ||
730 | struct mspro_block_data *msb = memstick_get_drvdata(card); | ||
731 | struct request *req = NULL; | ||
732 | |||
733 | if (msb->q_thread) { | ||
734 | msb->has_request = 1; | ||
735 | wake_up_all(&msb->q_wait); | ||
736 | } else { | ||
737 | while ((req = elv_next_request(q)) != NULL) | ||
738 | end_queued_request(req, -ENODEV); | ||
739 | } | ||
740 | } | ||
741 | |||
742 | /*** Initialization ***/ | ||
743 | |||
744 | static int mspro_block_wait_for_ced(struct memstick_dev *card) | ||
745 | { | ||
746 | struct mspro_block_data *msb = memstick_get_drvdata(card); | ||
747 | |||
748 | card->next_request = h_mspro_block_req_init; | ||
749 | msb->mrq_handler = h_mspro_block_wait_for_ced; | ||
750 | memstick_init_req(&card->current_mrq, MS_TPC_GET_INT, NULL, 1); | ||
751 | memstick_new_req(card->host); | ||
752 | wait_for_completion(&card->mrq_complete); | ||
753 | return card->current_mrq.error; | ||
754 | } | ||
755 | |||
756 | static int mspro_block_switch_to_parallel(struct memstick_dev *card) | ||
757 | { | ||
758 | struct memstick_host *host = card->host; | ||
759 | struct mspro_block_data *msb = memstick_get_drvdata(card); | ||
760 | struct mspro_param_register param = { | ||
761 | .system = 0, | ||
762 | .data_count = 0, | ||
763 | .data_address = 0, | ||
764 | .cmd_param = 0 | ||
765 | }; | ||
766 | |||
767 | card->next_request = h_mspro_block_req_init; | ||
768 | msb->mrq_handler = h_mspro_block_default; | ||
769 | memstick_init_req(&card->current_mrq, MS_TPC_WRITE_REG, ¶m, | ||
770 | sizeof(param)); | ||
771 | memstick_new_req(host); | ||
772 | wait_for_completion(&card->mrq_complete); | ||
773 | if (card->current_mrq.error) | ||
774 | return card->current_mrq.error; | ||
775 | |||
776 | msb->system = 0; | ||
777 | host->set_param(host, MEMSTICK_INTERFACE, MEMSTICK_PARALLEL); | ||
778 | |||
779 | card->next_request = h_mspro_block_req_init; | ||
780 | msb->mrq_handler = h_mspro_block_default; | ||
781 | memstick_init_req(&card->current_mrq, MS_TPC_GET_INT, NULL, 1); | ||
782 | memstick_new_req(card->host); | ||
783 | wait_for_completion(&card->mrq_complete); | ||
784 | |||
785 | if (card->current_mrq.error) { | ||
786 | msb->system = 0x80; | ||
787 | host->set_param(host, MEMSTICK_INTERFACE, MEMSTICK_SERIAL); | ||
788 | return -EFAULT; | ||
789 | } | ||
790 | |||
791 | return 0; | ||
792 | } | ||
793 | |||
794 | /* Memory allocated for attributes by this function should be freed by | ||
795 | * mspro_block_data_clear, no matter if the initialization process succeded | ||
796 | * or failed. | ||
797 | */ | ||
798 | static int mspro_block_read_attributes(struct memstick_dev *card) | ||
799 | { | ||
800 | struct mspro_block_data *msb = memstick_get_drvdata(card); | ||
801 | struct mspro_param_register param = { | ||
802 | .system = msb->system, | ||
803 | .data_count = cpu_to_be16(1), | ||
804 | .data_address = 0, | ||
805 | .cmd_param = 0 | ||
806 | }; | ||
807 | struct mspro_attribute *attr = NULL; | ||
808 | struct mspro_sys_attr *s_attr = NULL; | ||
809 | unsigned char *buffer = NULL; | ||
810 | int cnt, rc, attr_count; | ||
811 | unsigned int addr; | ||
812 | unsigned short page_count; | ||
813 | |||
814 | attr = kmalloc(msb->page_size, GFP_KERNEL); | ||
815 | if (!attr) | ||
816 | return -ENOMEM; | ||
817 | |||
818 | sg_init_one(&msb->req_sg[0], attr, msb->page_size); | ||
819 | msb->seg_count = 1; | ||
820 | msb->current_seg = 0; | ||
821 | msb->current_page = 0; | ||
822 | msb->data_dir = READ; | ||
823 | msb->transfer_cmd = MSPRO_CMD_READ_ATRB; | ||
824 | |||
825 | card->next_request = h_mspro_block_req_init; | ||
826 | msb->mrq_handler = h_mspro_block_transfer_data; | ||
827 | memstick_init_req(&card->current_mrq, MS_TPC_WRITE_REG, ¶m, | ||
828 | sizeof(param)); | ||
829 | memstick_new_req(card->host); | ||
830 | wait_for_completion(&card->mrq_complete); | ||
831 | if (card->current_mrq.error) { | ||
832 | rc = card->current_mrq.error; | ||
833 | goto out_free_attr; | ||
834 | } | ||
835 | |||
836 | if (be16_to_cpu(attr->signature) != MSPRO_BLOCK_SIGNATURE) { | ||
837 | printk(KERN_ERR "%s: unrecognized device signature %x\n", | ||
838 | card->dev.bus_id, be16_to_cpu(attr->signature)); | ||
839 | rc = -ENODEV; | ||
840 | goto out_free_attr; | ||
841 | } | ||
842 | |||
843 | if (attr->count > MSPRO_BLOCK_MAX_ATTRIBUTES) { | ||
844 | printk(KERN_WARNING "%s: way too many attribute entries\n", | ||
845 | card->dev.bus_id); | ||
846 | attr_count = MSPRO_BLOCK_MAX_ATTRIBUTES; | ||
847 | } else | ||
848 | attr_count = attr->count; | ||
849 | |||
850 | msb->attr_group.attrs = kzalloc((attr_count + 1) | ||
851 | * sizeof(struct attribute), | ||
852 | GFP_KERNEL); | ||
853 | if (!msb->attr_group.attrs) { | ||
854 | rc = -ENOMEM; | ||
855 | goto out_free_attr; | ||
856 | } | ||
857 | msb->attr_group.name = "media_attributes"; | ||
858 | |||
859 | buffer = kmalloc(msb->page_size, GFP_KERNEL); | ||
860 | if (!buffer) { | ||
861 | rc = -ENOMEM; | ||
862 | goto out_free_attr; | ||
863 | } | ||
864 | memcpy(buffer, (char *)attr, msb->page_size); | ||
865 | page_count = 1; | ||
866 | |||
867 | for (cnt = 0; cnt < attr_count; ++cnt) { | ||
868 | s_attr = kzalloc(sizeof(struct mspro_sys_attr), GFP_KERNEL); | ||
869 | if (!s_attr) { | ||
870 | rc = -ENOMEM; | ||
871 | goto out_free_buffer; | ||
872 | } | ||
873 | |||
874 | msb->attr_group.attrs[cnt] = &s_attr->dev_attr.attr; | ||
875 | addr = be32_to_cpu(attr->entries[cnt].address); | ||
876 | rc = be32_to_cpu(attr->entries[cnt].size); | ||
877 | dev_dbg(&card->dev, "adding attribute %d: id %x, address %x, " | ||
878 | "size %x\n", cnt, attr->entries[cnt].id, addr, rc); | ||
879 | s_attr->id = attr->entries[cnt].id; | ||
880 | if (mspro_block_attr_name(s_attr->id)) | ||
881 | snprintf(s_attr->name, sizeof(s_attr->name), "%s", | ||
882 | mspro_block_attr_name(attr->entries[cnt].id)); | ||
883 | else | ||
884 | snprintf(s_attr->name, sizeof(s_attr->name), | ||
885 | "attr_x%02x", attr->entries[cnt].id); | ||
886 | |||
887 | s_attr->dev_attr.attr.name = s_attr->name; | ||
888 | s_attr->dev_attr.attr.mode = S_IRUGO; | ||
889 | s_attr->dev_attr.attr.owner = THIS_MODULE; | ||
890 | s_attr->dev_attr.show = mspro_block_attr_show(s_attr->id); | ||
891 | |||
892 | if (!rc) | ||
893 | continue; | ||
894 | |||
895 | s_attr->size = rc; | ||
896 | s_attr->data = kmalloc(rc, GFP_KERNEL); | ||
897 | if (!s_attr->data) { | ||
898 | rc = -ENOMEM; | ||
899 | goto out_free_buffer; | ||
900 | } | ||
901 | |||
902 | if (((addr / msb->page_size) | ||
903 | == be32_to_cpu(param.data_address)) | ||
904 | && (((addr + rc - 1) / msb->page_size) | ||
905 | == be32_to_cpu(param.data_address))) { | ||
906 | memcpy(s_attr->data, buffer + addr % msb->page_size, | ||
907 | rc); | ||
908 | continue; | ||
909 | } | ||
910 | |||
911 | if (page_count <= (rc / msb->page_size)) { | ||
912 | kfree(buffer); | ||
913 | page_count = (rc / msb->page_size) + 1; | ||
914 | buffer = kmalloc(page_count * msb->page_size, | ||
915 | GFP_KERNEL); | ||
916 | if (!buffer) { | ||
917 | rc = -ENOMEM; | ||
918 | goto out_free_attr; | ||
919 | } | ||
920 | } | ||
921 | |||
922 | param.system = msb->system; | ||
923 | param.data_count = cpu_to_be16((rc / msb->page_size) + 1); | ||
924 | param.data_address = cpu_to_be32(addr / msb->page_size); | ||
925 | param.cmd_param = 0; | ||
926 | |||
927 | sg_init_one(&msb->req_sg[0], buffer, | ||
928 | be16_to_cpu(param.data_count) * msb->page_size); | ||
929 | msb->seg_count = 1; | ||
930 | msb->current_seg = 0; | ||
931 | msb->current_page = 0; | ||
932 | msb->data_dir = READ; | ||
933 | msb->transfer_cmd = MSPRO_CMD_READ_ATRB; | ||
934 | |||
935 | dev_dbg(&card->dev, "reading attribute pages %x, %x\n", | ||
936 | be32_to_cpu(param.data_address), | ||
937 | be16_to_cpu(param.data_count)); | ||
938 | |||
939 | card->next_request = h_mspro_block_req_init; | ||
940 | msb->mrq_handler = h_mspro_block_transfer_data; | ||
941 | memstick_init_req(&card->current_mrq, MS_TPC_WRITE_REG, | ||
942 | (char *)¶m, sizeof(param)); | ||
943 | memstick_new_req(card->host); | ||
944 | wait_for_completion(&card->mrq_complete); | ||
945 | if (card->current_mrq.error) { | ||
946 | rc = card->current_mrq.error; | ||
947 | goto out_free_buffer; | ||
948 | } | ||
949 | |||
950 | memcpy(s_attr->data, buffer + addr % msb->page_size, rc); | ||
951 | } | ||
952 | |||
953 | rc = 0; | ||
954 | out_free_buffer: | ||
955 | kfree(buffer); | ||
956 | out_free_attr: | ||
957 | kfree(attr); | ||
958 | return rc; | ||
959 | } | ||
960 | |||
961 | static int mspro_block_init_card(struct memstick_dev *card) | ||
962 | { | ||
963 | struct mspro_block_data *msb = memstick_get_drvdata(card); | ||
964 | struct memstick_host *host = card->host; | ||
965 | int rc = 0; | ||
966 | |||
967 | msb->system = 0x80; | ||
968 | card->reg_addr.r_offset = offsetof(struct mspro_register, status); | ||
969 | card->reg_addr.r_length = sizeof(struct ms_status_register); | ||
970 | card->reg_addr.w_offset = offsetof(struct mspro_register, param); | ||
971 | card->reg_addr.w_length = sizeof(struct mspro_param_register); | ||
972 | |||
973 | if (memstick_set_rw_addr(card)) | ||
974 | return -EIO; | ||
975 | |||
976 | if (host->caps & MEMSTICK_CAP_PARALLEL) { | ||
977 | if (mspro_block_switch_to_parallel(card)) | ||
978 | printk(KERN_WARNING "%s: could not switch to " | ||
979 | "parallel interface\n", card->dev.bus_id); | ||
980 | } | ||
981 | |||
982 | rc = mspro_block_wait_for_ced(card); | ||
983 | if (rc) | ||
984 | return rc; | ||
985 | dev_dbg(&card->dev, "card activated\n"); | ||
986 | |||
987 | card->next_request = h_mspro_block_req_init; | ||
988 | msb->mrq_handler = h_mspro_block_get_ro; | ||
989 | memstick_init_req(&card->current_mrq, MS_TPC_READ_REG, NULL, | ||
990 | sizeof(struct ms_status_register)); | ||
991 | memstick_new_req(card->host); | ||
992 | wait_for_completion(&card->mrq_complete); | ||
993 | if (card->current_mrq.error) | ||
994 | return card->current_mrq.error; | ||
995 | |||
996 | dev_dbg(&card->dev, "card r/w status %d\n", msb->read_only ? 0 : 1); | ||
997 | |||
998 | msb->page_size = 512; | ||
999 | rc = mspro_block_read_attributes(card); | ||
1000 | if (rc) | ||
1001 | return rc; | ||
1002 | |||
1003 | dev_dbg(&card->dev, "attributes loaded\n"); | ||
1004 | return 0; | ||
1005 | |||
1006 | } | ||
1007 | |||
1008 | static int mspro_block_init_disk(struct memstick_dev *card) | ||
1009 | { | ||
1010 | struct mspro_block_data *msb = memstick_get_drvdata(card); | ||
1011 | struct memstick_host *host = card->host; | ||
1012 | struct mspro_devinfo *dev_info = NULL; | ||
1013 | struct mspro_sys_info *sys_info = NULL; | ||
1014 | struct mspro_sys_attr *s_attr = NULL; | ||
1015 | int rc, disk_id; | ||
1016 | u64 limit = BLK_BOUNCE_HIGH; | ||
1017 | unsigned long capacity; | ||
1018 | |||
1019 | if (host->cdev.dev->dma_mask && *(host->cdev.dev->dma_mask)) | ||
1020 | limit = *(host->cdev.dev->dma_mask); | ||
1021 | |||
1022 | for (rc = 0; msb->attr_group.attrs[rc]; ++rc) { | ||
1023 | s_attr = mspro_from_sysfs_attr(msb->attr_group.attrs[rc]); | ||
1024 | |||
1025 | if (s_attr->id == MSPRO_BLOCK_ID_DEVINFO) | ||
1026 | dev_info = s_attr->data; | ||
1027 | else if (s_attr->id == MSPRO_BLOCK_ID_SYSINFO) | ||
1028 | sys_info = s_attr->data; | ||
1029 | } | ||
1030 | |||
1031 | if (!dev_info || !sys_info) | ||
1032 | return -ENODEV; | ||
1033 | |||
1034 | msb->cylinders = be16_to_cpu(dev_info->cylinders); | ||
1035 | msb->heads = be16_to_cpu(dev_info->heads); | ||
1036 | msb->sectors_per_track = be16_to_cpu(dev_info->sectors_per_track); | ||
1037 | |||
1038 | msb->page_size = be16_to_cpu(sys_info->unit_size); | ||
1039 | |||
1040 | if (!idr_pre_get(&mspro_block_disk_idr, GFP_KERNEL)) | ||
1041 | return -ENOMEM; | ||
1042 | |||
1043 | mutex_lock(&mspro_block_disk_lock); | ||
1044 | rc = idr_get_new(&mspro_block_disk_idr, card, &disk_id); | ||
1045 | mutex_unlock(&mspro_block_disk_lock); | ||
1046 | |||
1047 | if (rc) | ||
1048 | return rc; | ||
1049 | |||
1050 | if ((disk_id << MEMSTICK_PART_SHIFT) > 255) { | ||
1051 | rc = -ENOSPC; | ||
1052 | goto out_release_id; | ||
1053 | } | ||
1054 | |||
1055 | msb->disk = alloc_disk(1 << MEMSTICK_PART_SHIFT); | ||
1056 | if (!msb->disk) { | ||
1057 | rc = -ENOMEM; | ||
1058 | goto out_release_id; | ||
1059 | } | ||
1060 | |||
1061 | spin_lock_init(&msb->q_lock); | ||
1062 | init_waitqueue_head(&msb->q_wait); | ||
1063 | |||
1064 | msb->queue = blk_init_queue(mspro_block_request, &msb->q_lock); | ||
1065 | if (!msb->queue) { | ||
1066 | rc = -ENOMEM; | ||
1067 | goto out_put_disk; | ||
1068 | } | ||
1069 | |||
1070 | msb->queue->queuedata = card; | ||
1071 | |||
1072 | blk_queue_bounce_limit(msb->queue, limit); | ||
1073 | blk_queue_max_sectors(msb->queue, MSPRO_BLOCK_MAX_PAGES); | ||
1074 | blk_queue_max_phys_segments(msb->queue, MSPRO_BLOCK_MAX_SEGS); | ||
1075 | blk_queue_max_hw_segments(msb->queue, MSPRO_BLOCK_MAX_SEGS); | ||
1076 | blk_queue_max_segment_size(msb->queue, | ||
1077 | MSPRO_BLOCK_MAX_PAGES * msb->page_size); | ||
1078 | |||
1079 | msb->disk->major = major; | ||
1080 | msb->disk->first_minor = disk_id << MEMSTICK_PART_SHIFT; | ||
1081 | msb->disk->fops = &ms_block_bdops; | ||
1082 | msb->usage_count = 1; | ||
1083 | msb->disk->private_data = msb; | ||
1084 | msb->disk->queue = msb->queue; | ||
1085 | msb->disk->driverfs_dev = &card->dev; | ||
1086 | |||
1087 | sprintf(msb->disk->disk_name, "mspblk%d", disk_id); | ||
1088 | |||
1089 | blk_queue_hardsect_size(msb->queue, msb->page_size); | ||
1090 | |||
1091 | capacity = be16_to_cpu(sys_info->user_block_count); | ||
1092 | capacity *= be16_to_cpu(sys_info->block_size); | ||
1093 | capacity *= msb->page_size >> 9; | ||
1094 | set_capacity(msb->disk, capacity); | ||
1095 | dev_dbg(&card->dev, "capacity set %ld\n", capacity); | ||
1096 | msb->q_thread = kthread_run(mspro_block_queue_thread, card, | ||
1097 | DRIVER_NAME"d"); | ||
1098 | if (IS_ERR(msb->q_thread)) | ||
1099 | goto out_put_disk; | ||
1100 | |||
1101 | mutex_unlock(&host->lock); | ||
1102 | add_disk(msb->disk); | ||
1103 | mutex_lock(&host->lock); | ||
1104 | msb->active = 1; | ||
1105 | return 0; | ||
1106 | |||
1107 | out_put_disk: | ||
1108 | put_disk(msb->disk); | ||
1109 | out_release_id: | ||
1110 | mutex_lock(&mspro_block_disk_lock); | ||
1111 | idr_remove(&mspro_block_disk_idr, disk_id); | ||
1112 | mutex_unlock(&mspro_block_disk_lock); | ||
1113 | return rc; | ||
1114 | } | ||
1115 | |||
1116 | static void mspro_block_data_clear(struct mspro_block_data *msb) | ||
1117 | { | ||
1118 | int cnt; | ||
1119 | struct mspro_sys_attr *s_attr; | ||
1120 | |||
1121 | if (msb->attr_group.attrs) { | ||
1122 | for (cnt = 0; msb->attr_group.attrs[cnt]; ++cnt) { | ||
1123 | s_attr = mspro_from_sysfs_attr(msb->attr_group | ||
1124 | .attrs[cnt]); | ||
1125 | kfree(s_attr->data); | ||
1126 | kfree(s_attr); | ||
1127 | } | ||
1128 | kfree(msb->attr_group.attrs); | ||
1129 | } | ||
1130 | |||
1131 | msb->card = NULL; | ||
1132 | } | ||
1133 | |||
1134 | static int mspro_block_check_card(struct memstick_dev *card) | ||
1135 | { | ||
1136 | struct mspro_block_data *msb = memstick_get_drvdata(card); | ||
1137 | |||
1138 | return (msb->active == 1); | ||
1139 | } | ||
1140 | |||
1141 | static int mspro_block_probe(struct memstick_dev *card) | ||
1142 | { | ||
1143 | struct mspro_block_data *msb; | ||
1144 | int rc = 0; | ||
1145 | |||
1146 | msb = kzalloc(sizeof(struct mspro_block_data), GFP_KERNEL); | ||
1147 | if (!msb) | ||
1148 | return -ENOMEM; | ||
1149 | memstick_set_drvdata(card, msb); | ||
1150 | msb->card = card; | ||
1151 | |||
1152 | rc = mspro_block_init_card(card); | ||
1153 | |||
1154 | if (rc) | ||
1155 | goto out_free; | ||
1156 | |||
1157 | rc = sysfs_create_group(&card->dev.kobj, &msb->attr_group); | ||
1158 | if (rc) | ||
1159 | goto out_free; | ||
1160 | |||
1161 | rc = mspro_block_init_disk(card); | ||
1162 | if (!rc) { | ||
1163 | card->check = mspro_block_check_card; | ||
1164 | return 0; | ||
1165 | } | ||
1166 | |||
1167 | sysfs_remove_group(&card->dev.kobj, &msb->attr_group); | ||
1168 | out_free: | ||
1169 | memstick_set_drvdata(card, NULL); | ||
1170 | mspro_block_data_clear(msb); | ||
1171 | kfree(msb); | ||
1172 | return rc; | ||
1173 | } | ||
1174 | |||
1175 | static void mspro_block_remove(struct memstick_dev *card) | ||
1176 | { | ||
1177 | struct mspro_block_data *msb = memstick_get_drvdata(card); | ||
1178 | struct task_struct *q_thread = NULL; | ||
1179 | unsigned long flags; | ||
1180 | |||
1181 | del_gendisk(msb->disk); | ||
1182 | dev_dbg(&card->dev, "mspro block remove\n"); | ||
1183 | spin_lock_irqsave(&msb->q_lock, flags); | ||
1184 | q_thread = msb->q_thread; | ||
1185 | msb->q_thread = NULL; | ||
1186 | msb->active = 0; | ||
1187 | spin_unlock_irqrestore(&msb->q_lock, flags); | ||
1188 | |||
1189 | if (q_thread) { | ||
1190 | mutex_unlock(&card->host->lock); | ||
1191 | kthread_stop(q_thread); | ||
1192 | mutex_lock(&card->host->lock); | ||
1193 | } | ||
1194 | |||
1195 | dev_dbg(&card->dev, "queue thread stopped\n"); | ||
1196 | |||
1197 | blk_cleanup_queue(msb->queue); | ||
1198 | |||
1199 | sysfs_remove_group(&card->dev.kobj, &msb->attr_group); | ||
1200 | |||
1201 | mutex_lock(&mspro_block_disk_lock); | ||
1202 | mspro_block_data_clear(msb); | ||
1203 | mutex_unlock(&mspro_block_disk_lock); | ||
1204 | |||
1205 | mspro_block_disk_release(msb->disk); | ||
1206 | memstick_set_drvdata(card, NULL); | ||
1207 | } | ||
1208 | |||
1209 | #ifdef CONFIG_PM | ||
1210 | |||
1211 | static int mspro_block_suspend(struct memstick_dev *card, pm_message_t state) | ||
1212 | { | ||
1213 | struct mspro_block_data *msb = memstick_get_drvdata(card); | ||
1214 | struct task_struct *q_thread = NULL; | ||
1215 | unsigned long flags; | ||
1216 | |||
1217 | spin_lock_irqsave(&msb->q_lock, flags); | ||
1218 | q_thread = msb->q_thread; | ||
1219 | msb->q_thread = NULL; | ||
1220 | msb->active = 0; | ||
1221 | blk_stop_queue(msb->queue); | ||
1222 | spin_unlock_irqrestore(&msb->q_lock, flags); | ||
1223 | |||
1224 | if (q_thread) | ||
1225 | kthread_stop(q_thread); | ||
1226 | |||
1227 | return 0; | ||
1228 | } | ||
1229 | |||
1230 | static int mspro_block_resume(struct memstick_dev *card) | ||
1231 | { | ||
1232 | struct mspro_block_data *msb = memstick_get_drvdata(card); | ||
1233 | unsigned long flags; | ||
1234 | int rc = 0; | ||
1235 | |||
1236 | #ifdef CONFIG_MEMSTICK_UNSAFE_RESUME | ||
1237 | |||
1238 | struct mspro_block_data *new_msb; | ||
1239 | struct memstick_host *host = card->host; | ||
1240 | struct mspro_sys_attr *s_attr, *r_attr; | ||
1241 | unsigned char cnt; | ||
1242 | |||
1243 | mutex_lock(&host->lock); | ||
1244 | new_msb = kzalloc(sizeof(struct mspro_block_data), GFP_KERNEL); | ||
1245 | if (!new_msb) { | ||
1246 | rc = -ENOMEM; | ||
1247 | goto out_unlock; | ||
1248 | } | ||
1249 | |||
1250 | new_msb->card = card; | ||
1251 | memstick_set_drvdata(card, new_msb); | ||
1252 | if (mspro_block_init_card(card)) | ||
1253 | goto out_free; | ||
1254 | |||
1255 | for (cnt = 0; new_msb->attr_group.attrs[cnt] | ||
1256 | && msb->attr_group.attrs[cnt]; ++cnt) { | ||
1257 | s_attr = mspro_from_sysfs_attr(new_msb->attr_group.attrs[cnt]); | ||
1258 | r_attr = mspro_from_sysfs_attr(msb->attr_group.attrs[cnt]); | ||
1259 | |||
1260 | if (s_attr->id == MSPRO_BLOCK_ID_SYSINFO | ||
1261 | && r_attr->id == s_attr->id) { | ||
1262 | if (memcmp(s_attr->data, r_attr->data, s_attr->size)) | ||
1263 | break; | ||
1264 | |||
1265 | memstick_set_drvdata(card, msb); | ||
1266 | msb->q_thread = kthread_run(mspro_block_queue_thread, | ||
1267 | card, DRIVER_NAME"d"); | ||
1268 | if (IS_ERR(msb->q_thread)) | ||
1269 | msb->q_thread = NULL; | ||
1270 | else | ||
1271 | msb->active = 1; | ||
1272 | |||
1273 | break; | ||
1274 | } | ||
1275 | } | ||
1276 | |||
1277 | out_free: | ||
1278 | memstick_set_drvdata(card, msb); | ||
1279 | mspro_block_data_clear(new_msb); | ||
1280 | kfree(new_msb); | ||
1281 | out_unlock: | ||
1282 | mutex_unlock(&host->lock); | ||
1283 | |||
1284 | #endif /* CONFIG_MEMSTICK_UNSAFE_RESUME */ | ||
1285 | |||
1286 | spin_lock_irqsave(&msb->q_lock, flags); | ||
1287 | blk_start_queue(msb->queue); | ||
1288 | spin_unlock_irqrestore(&msb->q_lock, flags); | ||
1289 | return rc; | ||
1290 | } | ||
1291 | |||
1292 | #else | ||
1293 | |||
1294 | #define mspro_block_suspend NULL | ||
1295 | #define mspro_block_resume NULL | ||
1296 | |||
1297 | #endif /* CONFIG_PM */ | ||
1298 | |||
1299 | static struct memstick_device_id mspro_block_id_tbl[] = { | ||
1300 | {MEMSTICK_MATCH_ALL, MEMSTICK_TYPE_PRO, MEMSTICK_CATEGORY_STORAGE_DUO, | ||
1301 | MEMSTICK_CLASS_GENERIC_DUO}, | ||
1302 | {} | ||
1303 | }; | ||
1304 | |||
1305 | |||
1306 | static struct memstick_driver mspro_block_driver = { | ||
1307 | .driver = { | ||
1308 | .name = DRIVER_NAME, | ||
1309 | .owner = THIS_MODULE | ||
1310 | }, | ||
1311 | .id_table = mspro_block_id_tbl, | ||
1312 | .probe = mspro_block_probe, | ||
1313 | .remove = mspro_block_remove, | ||
1314 | .suspend = mspro_block_suspend, | ||
1315 | .resume = mspro_block_resume | ||
1316 | }; | ||
1317 | |||
1318 | static int __init mspro_block_init(void) | ||
1319 | { | ||
1320 | int rc = -ENOMEM; | ||
1321 | |||
1322 | rc = register_blkdev(major, DRIVER_NAME); | ||
1323 | if (rc < 0) { | ||
1324 | printk(KERN_ERR DRIVER_NAME ": failed to register " | ||
1325 | "major %d, error %d\n", major, rc); | ||
1326 | return rc; | ||
1327 | } | ||
1328 | if (!major) | ||
1329 | major = rc; | ||
1330 | |||
1331 | rc = memstick_register_driver(&mspro_block_driver); | ||
1332 | if (rc) | ||
1333 | unregister_blkdev(major, DRIVER_NAME); | ||
1334 | return rc; | ||
1335 | } | ||
1336 | |||
1337 | static void __exit mspro_block_exit(void) | ||
1338 | { | ||
1339 | memstick_unregister_driver(&mspro_block_driver); | ||
1340 | unregister_blkdev(major, DRIVER_NAME); | ||
1341 | idr_destroy(&mspro_block_disk_idr); | ||
1342 | } | ||
1343 | |||
1344 | module_init(mspro_block_init); | ||
1345 | module_exit(mspro_block_exit); | ||
1346 | |||
1347 | MODULE_LICENSE("GPL"); | ||
1348 | MODULE_AUTHOR("Alex Dubov"); | ||
1349 | MODULE_DESCRIPTION("Sony MemoryStickPro block device driver"); | ||
1350 | MODULE_DEVICE_TABLE(memstick, mspro_block_id_tbl); | ||
1351 | MODULE_VERSION(DRIVER_VERSION); | ||