diff options
author | Alex Dubov <oakad@yahoo.com> | 2008-02-09 13:20:54 -0500 |
---|---|---|
committer | Linus Torvalds <torvalds@woody.linux-foundation.org> | 2008-02-09 14:08:34 -0500 |
commit | baf8532a147d5b76681ce040e2c8f25a3f0e718d (patch) | |
tree | 69c228046709295c1152f2063321327789c3b9f4 /drivers/memstick/core | |
parent | 941edd030b9725f9f85bd62dfdb68cde3a50fb66 (diff) |
memstick: initial commit for Sony MemoryStick support
Sony MemoryStick cards are used in many products manufactured by Sony.
They are available both as storage and as IO expansion cards. Currently,
only MemoryStick Pro storage cards are supported via TI FlashMedia
MemoryStick interface.
[mboton@gmail.com: biuld fix]
[akpm@linux-foundation.org: build fix]
Signed-off-by: Alex Dubov <oakad@yahoo.com>
Signed-off-by: Miguel Boton <mboton@gmail.co>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Diffstat (limited to 'drivers/memstick/core')
-rw-r--r-- | drivers/memstick/core/Kconfig | 26 | ||||
-rw-r--r-- | drivers/memstick/core/Makefile | 11 | ||||
-rw-r--r-- | drivers/memstick/core/memstick.c | 614 | ||||
-rw-r--r-- | drivers/memstick/core/mspro_block.c | 1351 |
4 files changed, 2002 insertions, 0 deletions
diff --git a/drivers/memstick/core/Kconfig b/drivers/memstick/core/Kconfig new file mode 100644 index 000000000000..95f1814b5368 --- /dev/null +++ b/drivers/memstick/core/Kconfig | |||
@@ -0,0 +1,26 @@ | |||
1 | # | ||
2 | # MemoryStick core configuration | ||
3 | # | ||
4 | |||
5 | comment "MemoryStick drivers" | ||
6 | |||
7 | config MEMSTICK_UNSAFE_RESUME | ||
8 | bool "Allow unsafe resume (DANGEROUS)" | ||
9 | help | ||
10 | If you say Y here, the MemoryStick layer will assume that all | ||
11 | cards stayed in their respective slots during the suspend. The | ||
12 | normal behaviour is to remove them at suspend and | ||
13 | redetecting them at resume. Breaking this assumption will | ||
14 | in most cases result in data corruption. | ||
15 | |||
16 | This option is usually just for embedded systems which use | ||
17 | a MemoryStick card for rootfs. Most people should say N here. | ||
18 | |||
19 | config MSPRO_BLOCK | ||
20 | tristate "MemoryStick Pro block device driver" | ||
21 | depends on BLOCK | ||
22 | help | ||
23 | Say Y here to enable the MemoryStick Pro block device driver | ||
24 | support. This provides a block device driver, which you can use | ||
25 | to mount the filesystem. Almost everyone wishing MemoryStick | ||
26 | support should say Y or M here. | ||
diff --git a/drivers/memstick/core/Makefile b/drivers/memstick/core/Makefile new file mode 100644 index 000000000000..8b2b5293877e --- /dev/null +++ b/drivers/memstick/core/Makefile | |||
@@ -0,0 +1,11 @@ | |||
1 | # | ||
2 | # Makefile for the kernel MemoryStick core. | ||
3 | # | ||
4 | |||
5 | ifeq ($(CONFIG_MEMSTICK_DEBUG),y) | ||
6 | EXTRA_CFLAGS += -DDEBUG | ||
7 | endif | ||
8 | |||
9 | obj-$(CONFIG_MEMSTICK) += memstick.o | ||
10 | |||
11 | obj-$(CONFIG_MSPRO_BLOCK) += mspro_block.o | ||
diff --git a/drivers/memstick/core/memstick.c b/drivers/memstick/core/memstick.c new file mode 100644 index 000000000000..bba467fe4bce --- /dev/null +++ b/drivers/memstick/core/memstick.c | |||
@@ -0,0 +1,614 @@ | |||
1 | /* | ||
2 | * Sony MemoryStick 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/memstick.h> | ||
16 | #include <linux/idr.h> | ||
17 | #include <linux/fs.h> | ||
18 | #include <linux/delay.h> | ||
19 | |||
20 | #define DRIVER_NAME "memstick" | ||
21 | #define DRIVER_VERSION "0.2" | ||
22 | |||
23 | static unsigned int cmd_retries = 3; | ||
24 | module_param(cmd_retries, uint, 0644); | ||
25 | |||
26 | static struct workqueue_struct *workqueue; | ||
27 | static DEFINE_IDR(memstick_host_idr); | ||
28 | static DEFINE_SPINLOCK(memstick_host_lock); | ||
29 | |||
30 | static int memstick_dev_match(struct memstick_dev *card, | ||
31 | struct memstick_device_id *id) | ||
32 | { | ||
33 | if (id->match_flags & MEMSTICK_MATCH_ALL) { | ||
34 | if ((id->type == card->id.type) | ||
35 | && (id->category == card->id.category) | ||
36 | && (id->class == card->id.class)) | ||
37 | return 1; | ||
38 | } | ||
39 | |||
40 | return 0; | ||
41 | } | ||
42 | |||
43 | static int memstick_bus_match(struct device *dev, struct device_driver *drv) | ||
44 | { | ||
45 | struct memstick_dev *card = container_of(dev, struct memstick_dev, | ||
46 | dev); | ||
47 | struct memstick_driver *ms_drv = container_of(drv, | ||
48 | struct memstick_driver, | ||
49 | driver); | ||
50 | struct memstick_device_id *ids = ms_drv->id_table; | ||
51 | |||
52 | if (ids) { | ||
53 | while (ids->match_flags) { | ||
54 | if (memstick_dev_match(card, ids)) | ||
55 | return 1; | ||
56 | ++ids; | ||
57 | } | ||
58 | } | ||
59 | return 0; | ||
60 | } | ||
61 | |||
62 | static int memstick_uevent(struct device *dev, struct kobj_uevent_env *env) | ||
63 | { | ||
64 | struct memstick_dev *card = container_of(dev, struct memstick_dev, | ||
65 | dev); | ||
66 | |||
67 | if (add_uevent_var(env, "MEMSTICK_TYPE=%02X", card->id.type)) | ||
68 | return -ENOMEM; | ||
69 | |||
70 | if (add_uevent_var(env, "MEMSTICK_CATEGORY=%02X", card->id.category)) | ||
71 | return -ENOMEM; | ||
72 | |||
73 | if (add_uevent_var(env, "MEMSTICK_CLASS=%02X", card->id.class)) | ||
74 | return -ENOMEM; | ||
75 | |||
76 | return 0; | ||
77 | } | ||
78 | |||
79 | static int memstick_device_probe(struct device *dev) | ||
80 | { | ||
81 | struct memstick_dev *card = container_of(dev, struct memstick_dev, | ||
82 | dev); | ||
83 | struct memstick_driver *drv = container_of(dev->driver, | ||
84 | struct memstick_driver, | ||
85 | driver); | ||
86 | int rc = -ENODEV; | ||
87 | |||
88 | if (dev->driver && drv->probe) { | ||
89 | rc = drv->probe(card); | ||
90 | if (!rc) | ||
91 | get_device(dev); | ||
92 | } | ||
93 | return rc; | ||
94 | } | ||
95 | |||
96 | static int memstick_device_remove(struct device *dev) | ||
97 | { | ||
98 | struct memstick_dev *card = container_of(dev, struct memstick_dev, | ||
99 | dev); | ||
100 | struct memstick_driver *drv = container_of(dev->driver, | ||
101 | struct memstick_driver, | ||
102 | driver); | ||
103 | |||
104 | if (dev->driver && drv->remove) { | ||
105 | drv->remove(card); | ||
106 | card->dev.driver = NULL; | ||
107 | } | ||
108 | |||
109 | put_device(dev); | ||
110 | return 0; | ||
111 | } | ||
112 | |||
113 | #ifdef CONFIG_PM | ||
114 | |||
115 | static int memstick_device_suspend(struct device *dev, pm_message_t state) | ||
116 | { | ||
117 | struct memstick_dev *card = container_of(dev, struct memstick_dev, | ||
118 | dev); | ||
119 | struct memstick_driver *drv = container_of(dev->driver, | ||
120 | struct memstick_driver, | ||
121 | driver); | ||
122 | |||
123 | if (dev->driver && drv->suspend) | ||
124 | return drv->suspend(card, state); | ||
125 | return 0; | ||
126 | } | ||
127 | |||
128 | static int memstick_device_resume(struct device *dev) | ||
129 | { | ||
130 | struct memstick_dev *card = container_of(dev, struct memstick_dev, | ||
131 | dev); | ||
132 | struct memstick_driver *drv = container_of(dev->driver, | ||
133 | struct memstick_driver, | ||
134 | driver); | ||
135 | |||
136 | if (dev->driver && drv->resume) | ||
137 | return drv->resume(card); | ||
138 | return 0; | ||
139 | } | ||
140 | |||
141 | #else | ||
142 | |||
143 | #define memstick_device_suspend NULL | ||
144 | #define memstick_device_resume NULL | ||
145 | |||
146 | #endif /* CONFIG_PM */ | ||
147 | |||
148 | #define MEMSTICK_ATTR(name, format) \ | ||
149 | static ssize_t name##_show(struct device *dev, struct device_attribute *attr, \ | ||
150 | char *buf) \ | ||
151 | { \ | ||
152 | struct memstick_dev *card = container_of(dev, struct memstick_dev, \ | ||
153 | dev); \ | ||
154 | return sprintf(buf, format, card->id.name); \ | ||
155 | } | ||
156 | |||
157 | MEMSTICK_ATTR(type, "%02X"); | ||
158 | MEMSTICK_ATTR(category, "%02X"); | ||
159 | MEMSTICK_ATTR(class, "%02X"); | ||
160 | |||
161 | #define MEMSTICK_ATTR_RO(name) __ATTR(name, S_IRUGO, name##_show, NULL) | ||
162 | |||
163 | static struct device_attribute memstick_dev_attrs[] = { | ||
164 | MEMSTICK_ATTR_RO(type), | ||
165 | MEMSTICK_ATTR_RO(category), | ||
166 | MEMSTICK_ATTR_RO(class), | ||
167 | __ATTR_NULL | ||
168 | }; | ||
169 | |||
170 | static struct bus_type memstick_bus_type = { | ||
171 | .name = "memstick", | ||
172 | .dev_attrs = memstick_dev_attrs, | ||
173 | .match = memstick_bus_match, | ||
174 | .uevent = memstick_uevent, | ||
175 | .probe = memstick_device_probe, | ||
176 | .remove = memstick_device_remove, | ||
177 | .suspend = memstick_device_suspend, | ||
178 | .resume = memstick_device_resume | ||
179 | }; | ||
180 | |||
181 | static void memstick_free(struct class_device *cdev) | ||
182 | { | ||
183 | struct memstick_host *host = container_of(cdev, struct memstick_host, | ||
184 | cdev); | ||
185 | kfree(host); | ||
186 | } | ||
187 | |||
188 | static struct class memstick_host_class = { | ||
189 | .name = "memstick_host", | ||
190 | .release = memstick_free | ||
191 | }; | ||
192 | |||
193 | static void memstick_free_card(struct device *dev) | ||
194 | { | ||
195 | struct memstick_dev *card = container_of(dev, struct memstick_dev, | ||
196 | dev); | ||
197 | kfree(card); | ||
198 | } | ||
199 | |||
200 | static int memstick_dummy_check(struct memstick_dev *card) | ||
201 | { | ||
202 | return 0; | ||
203 | } | ||
204 | |||
205 | /** | ||
206 | * memstick_detect_change - schedule media detection on memstick host | ||
207 | * @host - host to use | ||
208 | */ | ||
209 | void memstick_detect_change(struct memstick_host *host) | ||
210 | { | ||
211 | queue_work(workqueue, &host->media_checker); | ||
212 | } | ||
213 | EXPORT_SYMBOL(memstick_detect_change); | ||
214 | |||
215 | /** | ||
216 | * memstick_next_req - called by host driver to obtain next request to process | ||
217 | * @host - host to use | ||
218 | * @mrq - pointer to stick the request to | ||
219 | * | ||
220 | * Host calls this function from idle state (*mrq == NULL) or after finishing | ||
221 | * previous request (*mrq should point to it). If previous request was | ||
222 | * unsuccessful, it is retried for predetermined number of times. Return value | ||
223 | * of 0 means that new request was assigned to the host. | ||
224 | */ | ||
225 | int memstick_next_req(struct memstick_host *host, struct memstick_request **mrq) | ||
226 | { | ||
227 | int rc = -ENXIO; | ||
228 | |||
229 | if ((*mrq) && (*mrq)->error && host->retries) { | ||
230 | (*mrq)->error = rc; | ||
231 | host->retries--; | ||
232 | return 0; | ||
233 | } | ||
234 | |||
235 | if (host->card && host->card->next_request) | ||
236 | rc = host->card->next_request(host->card, mrq); | ||
237 | |||
238 | if (!rc) | ||
239 | host->retries = cmd_retries; | ||
240 | else | ||
241 | *mrq = NULL; | ||
242 | |||
243 | return rc; | ||
244 | } | ||
245 | EXPORT_SYMBOL(memstick_next_req); | ||
246 | |||
247 | /** | ||
248 | * memstick_new_req - notify the host that some requests are pending | ||
249 | * @host - host to use | ||
250 | */ | ||
251 | void memstick_new_req(struct memstick_host *host) | ||
252 | { | ||
253 | host->retries = cmd_retries; | ||
254 | host->request(host); | ||
255 | } | ||
256 | EXPORT_SYMBOL(memstick_new_req); | ||
257 | |||
258 | /** | ||
259 | * memstick_init_req_sg - set request fields needed for bulk data transfer | ||
260 | * @mrq - request to use | ||
261 | * @tpc - memstick Transport Protocol Command | ||
262 | * @sg - TPC argument | ||
263 | */ | ||
264 | void memstick_init_req_sg(struct memstick_request *mrq, unsigned char tpc, | ||
265 | struct scatterlist *sg) | ||
266 | { | ||
267 | mrq->tpc = tpc; | ||
268 | if (tpc & 8) | ||
269 | mrq->data_dir = WRITE; | ||
270 | else | ||
271 | mrq->data_dir = READ; | ||
272 | |||
273 | mrq->sg = *sg; | ||
274 | mrq->io_type = MEMSTICK_IO_SG; | ||
275 | |||
276 | if (tpc == MS_TPC_SET_CMD || tpc == MS_TPC_EX_SET_CMD) | ||
277 | mrq->need_card_int = 1; | ||
278 | else | ||
279 | mrq->need_card_int = 0; | ||
280 | |||
281 | mrq->get_int_reg = 0; | ||
282 | } | ||
283 | EXPORT_SYMBOL(memstick_init_req_sg); | ||
284 | |||
285 | /** | ||
286 | * memstick_init_req - set request fields needed for short data transfer | ||
287 | * @mrq - request to use | ||
288 | * @tpc - memstick Transport Protocol Command | ||
289 | * @buf - TPC argument buffer | ||
290 | * @length - TPC argument size | ||
291 | * | ||
292 | * The intended use of this function (transfer of data items several bytes | ||
293 | * in size) allows us to just copy the value between request structure and | ||
294 | * user supplied buffer. | ||
295 | */ | ||
296 | void memstick_init_req(struct memstick_request *mrq, unsigned char tpc, | ||
297 | void *buf, size_t length) | ||
298 | { | ||
299 | mrq->tpc = tpc; | ||
300 | if (tpc & 8) | ||
301 | mrq->data_dir = WRITE; | ||
302 | else | ||
303 | mrq->data_dir = READ; | ||
304 | |||
305 | mrq->data_len = length > sizeof(mrq->data) ? sizeof(mrq->data) : length; | ||
306 | if (mrq->data_dir == WRITE) | ||
307 | memcpy(mrq->data, buf, mrq->data_len); | ||
308 | |||
309 | mrq->io_type = MEMSTICK_IO_VAL; | ||
310 | |||
311 | if (tpc == MS_TPC_SET_CMD || tpc == MS_TPC_EX_SET_CMD) | ||
312 | mrq->need_card_int = 1; | ||
313 | else | ||
314 | mrq->need_card_int = 0; | ||
315 | |||
316 | mrq->get_int_reg = 0; | ||
317 | } | ||
318 | EXPORT_SYMBOL(memstick_init_req); | ||
319 | |||
320 | /* | ||
321 | * Functions prefixed with "h_" are protocol callbacks. They can be called from | ||
322 | * interrupt context. Return value of 0 means that request processing is still | ||
323 | * ongoing, while special error value of -EAGAIN means that current request is | ||
324 | * finished (and request processor should come back some time later). | ||
325 | */ | ||
326 | |||
327 | static int h_memstick_read_dev_id(struct memstick_dev *card, | ||
328 | struct memstick_request **mrq) | ||
329 | { | ||
330 | struct ms_id_register id_reg; | ||
331 | |||
332 | if (!(*mrq)) { | ||
333 | memstick_init_req(&card->current_mrq, MS_TPC_READ_REG, NULL, | ||
334 | sizeof(struct ms_id_register)); | ||
335 | *mrq = &card->current_mrq; | ||
336 | return 0; | ||
337 | } else { | ||
338 | if (!(*mrq)->error) { | ||
339 | memcpy(&id_reg, (*mrq)->data, sizeof(id_reg)); | ||
340 | card->id.match_flags = MEMSTICK_MATCH_ALL; | ||
341 | card->id.type = id_reg.type; | ||
342 | card->id.category = id_reg.category; | ||
343 | card->id.class = id_reg.class; | ||
344 | } | ||
345 | complete(&card->mrq_complete); | ||
346 | return -EAGAIN; | ||
347 | } | ||
348 | } | ||
349 | |||
350 | static int h_memstick_set_rw_addr(struct memstick_dev *card, | ||
351 | struct memstick_request **mrq) | ||
352 | { | ||
353 | if (!(*mrq)) { | ||
354 | memstick_init_req(&card->current_mrq, MS_TPC_SET_RW_REG_ADRS, | ||
355 | (char *)&card->reg_addr, | ||
356 | sizeof(card->reg_addr)); | ||
357 | *mrq = &card->current_mrq; | ||
358 | return 0; | ||
359 | } else { | ||
360 | complete(&card->mrq_complete); | ||
361 | return -EAGAIN; | ||
362 | } | ||
363 | } | ||
364 | |||
365 | /** | ||
366 | * memstick_set_rw_addr - issue SET_RW_REG_ADDR request and wait for it to | ||
367 | * complete | ||
368 | * @card - media device to use | ||
369 | */ | ||
370 | int memstick_set_rw_addr(struct memstick_dev *card) | ||
371 | { | ||
372 | card->next_request = h_memstick_set_rw_addr; | ||
373 | memstick_new_req(card->host); | ||
374 | wait_for_completion(&card->mrq_complete); | ||
375 | |||
376 | return card->current_mrq.error; | ||
377 | } | ||
378 | EXPORT_SYMBOL(memstick_set_rw_addr); | ||
379 | |||
380 | static struct memstick_dev *memstick_alloc_card(struct memstick_host *host) | ||
381 | { | ||
382 | struct memstick_dev *card = kzalloc(sizeof(struct memstick_dev), | ||
383 | GFP_KERNEL); | ||
384 | struct memstick_dev *old_card = host->card; | ||
385 | struct ms_id_register id_reg; | ||
386 | |||
387 | if (card) { | ||
388 | card->host = host; | ||
389 | snprintf(card->dev.bus_id, sizeof(card->dev.bus_id), | ||
390 | "%s", host->cdev.class_id); | ||
391 | card->dev.parent = host->cdev.dev; | ||
392 | card->dev.bus = &memstick_bus_type; | ||
393 | card->dev.release = memstick_free_card; | ||
394 | card->check = memstick_dummy_check; | ||
395 | |||
396 | card->reg_addr.r_offset = offsetof(struct ms_register, id); | ||
397 | card->reg_addr.r_length = sizeof(id_reg); | ||
398 | card->reg_addr.w_offset = offsetof(struct ms_register, id); | ||
399 | card->reg_addr.w_length = sizeof(id_reg); | ||
400 | |||
401 | init_completion(&card->mrq_complete); | ||
402 | |||
403 | host->card = card; | ||
404 | if (memstick_set_rw_addr(card)) | ||
405 | goto err_out; | ||
406 | |||
407 | card->next_request = h_memstick_read_dev_id; | ||
408 | memstick_new_req(host); | ||
409 | wait_for_completion(&card->mrq_complete); | ||
410 | |||
411 | if (card->current_mrq.error) | ||
412 | goto err_out; | ||
413 | } | ||
414 | host->card = old_card; | ||
415 | return card; | ||
416 | err_out: | ||
417 | host->card = old_card; | ||
418 | kfree(card); | ||
419 | return NULL; | ||
420 | } | ||
421 | |||
422 | static void memstick_power_on(struct memstick_host *host) | ||
423 | { | ||
424 | host->set_param(host, MEMSTICK_POWER, MEMSTICK_POWER_ON); | ||
425 | host->set_param(host, MEMSTICK_INTERFACE, MEMSTICK_SERIAL); | ||
426 | msleep(1); | ||
427 | } | ||
428 | |||
429 | static void memstick_check(struct work_struct *work) | ||
430 | { | ||
431 | struct memstick_host *host = container_of(work, struct memstick_host, | ||
432 | media_checker); | ||
433 | struct memstick_dev *card; | ||
434 | |||
435 | dev_dbg(host->cdev.dev, "memstick_check started\n"); | ||
436 | mutex_lock(&host->lock); | ||
437 | if (!host->card) | ||
438 | memstick_power_on(host); | ||
439 | |||
440 | card = memstick_alloc_card(host); | ||
441 | |||
442 | if (!card) { | ||
443 | if (host->card) { | ||
444 | device_unregister(&host->card->dev); | ||
445 | host->card = NULL; | ||
446 | } | ||
447 | } else { | ||
448 | dev_dbg(host->cdev.dev, "new card %02x, %02x, %02x\n", | ||
449 | card->id.type, card->id.category, card->id.class); | ||
450 | if (host->card) { | ||
451 | if (memstick_set_rw_addr(host->card) | ||
452 | || !memstick_dev_match(host->card, &card->id) | ||
453 | || !(host->card->check(host->card))) { | ||
454 | device_unregister(&host->card->dev); | ||
455 | host->card = NULL; | ||
456 | } | ||
457 | } | ||
458 | |||
459 | if (!host->card) { | ||
460 | host->card = card; | ||
461 | if (device_register(&card->dev)) { | ||
462 | kfree(host->card); | ||
463 | host->card = NULL; | ||
464 | } | ||
465 | } else | ||
466 | kfree(card); | ||
467 | } | ||
468 | |||
469 | if (!host->card) | ||
470 | host->set_param(host, MEMSTICK_POWER, MEMSTICK_POWER_OFF); | ||
471 | |||
472 | mutex_unlock(&host->lock); | ||
473 | dev_dbg(host->cdev.dev, "memstick_check finished\n"); | ||
474 | } | ||
475 | |||
476 | /** | ||
477 | * memstick_alloc_host - allocate a memstick_host structure | ||
478 | * @extra: size of the user private data to allocate | ||
479 | * @dev: parent device of the host | ||
480 | */ | ||
481 | struct memstick_host *memstick_alloc_host(unsigned int extra, | ||
482 | struct device *dev) | ||
483 | { | ||
484 | struct memstick_host *host; | ||
485 | |||
486 | host = kzalloc(sizeof(struct memstick_host) + extra, GFP_KERNEL); | ||
487 | if (host) { | ||
488 | mutex_init(&host->lock); | ||
489 | INIT_WORK(&host->media_checker, memstick_check); | ||
490 | host->cdev.class = &memstick_host_class; | ||
491 | host->cdev.dev = dev; | ||
492 | class_device_initialize(&host->cdev); | ||
493 | } | ||
494 | return host; | ||
495 | } | ||
496 | EXPORT_SYMBOL(memstick_alloc_host); | ||
497 | |||
498 | /** | ||
499 | * memstick_add_host - start request processing on memstick host | ||
500 | * @host - host to use | ||
501 | */ | ||
502 | int memstick_add_host(struct memstick_host *host) | ||
503 | { | ||
504 | int rc; | ||
505 | |||
506 | if (!idr_pre_get(&memstick_host_idr, GFP_KERNEL)) | ||
507 | return -ENOMEM; | ||
508 | |||
509 | spin_lock(&memstick_host_lock); | ||
510 | rc = idr_get_new(&memstick_host_idr, host, &host->id); | ||
511 | spin_unlock(&memstick_host_lock); | ||
512 | if (rc) | ||
513 | return rc; | ||
514 | |||
515 | snprintf(host->cdev.class_id, BUS_ID_SIZE, | ||
516 | "memstick%u", host->id); | ||
517 | |||
518 | rc = class_device_add(&host->cdev); | ||
519 | if (rc) { | ||
520 | spin_lock(&memstick_host_lock); | ||
521 | idr_remove(&memstick_host_idr, host->id); | ||
522 | spin_unlock(&memstick_host_lock); | ||
523 | return rc; | ||
524 | } | ||
525 | |||
526 | host->set_param(host, MEMSTICK_POWER, MEMSTICK_POWER_OFF); | ||
527 | memstick_detect_change(host); | ||
528 | return 0; | ||
529 | } | ||
530 | EXPORT_SYMBOL(memstick_add_host); | ||
531 | |||
532 | /** | ||
533 | * memstick_remove_host - stop request processing on memstick host | ||
534 | * @host - host to use | ||
535 | */ | ||
536 | void memstick_remove_host(struct memstick_host *host) | ||
537 | { | ||
538 | flush_workqueue(workqueue); | ||
539 | mutex_lock(&host->lock); | ||
540 | if (host->card) | ||
541 | device_unregister(&host->card->dev); | ||
542 | host->card = NULL; | ||
543 | host->set_param(host, MEMSTICK_POWER, MEMSTICK_POWER_OFF); | ||
544 | mutex_unlock(&host->lock); | ||
545 | |||
546 | spin_lock(&memstick_host_lock); | ||
547 | idr_remove(&memstick_host_idr, host->id); | ||
548 | spin_unlock(&memstick_host_lock); | ||
549 | class_device_del(&host->cdev); | ||
550 | } | ||
551 | EXPORT_SYMBOL(memstick_remove_host); | ||
552 | |||
553 | /** | ||
554 | * memstick_free_host - free memstick host | ||
555 | * @host - host to use | ||
556 | */ | ||
557 | void memstick_free_host(struct memstick_host *host) | ||
558 | { | ||
559 | mutex_destroy(&host->lock); | ||
560 | class_device_put(&host->cdev); | ||
561 | } | ||
562 | EXPORT_SYMBOL(memstick_free_host); | ||
563 | |||
564 | int memstick_register_driver(struct memstick_driver *drv) | ||
565 | { | ||
566 | drv->driver.bus = &memstick_bus_type; | ||
567 | |||
568 | return driver_register(&drv->driver); | ||
569 | } | ||
570 | EXPORT_SYMBOL(memstick_register_driver); | ||
571 | |||
572 | void memstick_unregister_driver(struct memstick_driver *drv) | ||
573 | { | ||
574 | driver_unregister(&drv->driver); | ||
575 | } | ||
576 | EXPORT_SYMBOL(memstick_unregister_driver); | ||
577 | |||
578 | |||
579 | static int __init memstick_init(void) | ||
580 | { | ||
581 | int rc; | ||
582 | |||
583 | workqueue = create_freezeable_workqueue("kmemstick"); | ||
584 | if (!workqueue) | ||
585 | return -ENOMEM; | ||
586 | |||
587 | rc = bus_register(&memstick_bus_type); | ||
588 | if (!rc) | ||
589 | rc = class_register(&memstick_host_class); | ||
590 | |||
591 | if (!rc) | ||
592 | return 0; | ||
593 | |||
594 | bus_unregister(&memstick_bus_type); | ||
595 | destroy_workqueue(workqueue); | ||
596 | |||
597 | return rc; | ||
598 | } | ||
599 | |||
600 | static void __exit memstick_exit(void) | ||
601 | { | ||
602 | class_unregister(&memstick_host_class); | ||
603 | bus_unregister(&memstick_bus_type); | ||
604 | destroy_workqueue(workqueue); | ||
605 | idr_destroy(&memstick_host_idr); | ||
606 | } | ||
607 | |||
608 | module_init(memstick_init); | ||
609 | module_exit(memstick_exit); | ||
610 | |||
611 | MODULE_AUTHOR("Alex Dubov"); | ||
612 | MODULE_LICENSE("GPL"); | ||
613 | MODULE_DESCRIPTION("Sony MemoryStick core driver"); | ||
614 | MODULE_VERSION(DRIVER_VERSION); | ||
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); | ||