aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/memstick
diff options
context:
space:
mode:
authorAlex Dubov <oakad@yahoo.com>2008-02-09 13:20:54 -0500
committerLinus Torvalds <torvalds@woody.linux-foundation.org>2008-02-09 14:08:34 -0500
commitbaf8532a147d5b76681ce040e2c8f25a3f0e718d (patch)
tree69c228046709295c1152f2063321327789c3b9f4 /drivers/memstick
parent941edd030b9725f9f85bd62dfdb68cde3a50fb66 (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')
-rw-r--r--drivers/memstick/Kconfig26
-rw-r--r--drivers/memstick/Makefile11
-rw-r--r--drivers/memstick/core/Kconfig26
-rw-r--r--drivers/memstick/core/Makefile11
-rw-r--r--drivers/memstick/core/memstick.c614
-rw-r--r--drivers/memstick/core/mspro_block.c1351
-rw-r--r--drivers/memstick/host/Kconfig22
-rw-r--r--drivers/memstick/host/Makefile10
-rw-r--r--drivers/memstick/host/tifm_ms.c685
9 files changed, 2756 insertions, 0 deletions
diff --git a/drivers/memstick/Kconfig b/drivers/memstick/Kconfig
new file mode 100644
index 000000000000..1093fdb07297
--- /dev/null
+++ b/drivers/memstick/Kconfig
@@ -0,0 +1,26 @@
1#
2# MemoryStick subsystem configuration
3#
4
5menuconfig MEMSTICK
6 tristate "Sony MemoryStick card support (EXPERIMENTAL)"
7 help
8 Sony MemoryStick is a proprietary storage/extension card protocol.
9
10 If you want MemoryStick support, you should say Y here and also
11 to the specific driver for your MMC interface.
12
13if MEMSTICK
14
15config MEMSTICK_DEBUG
16 bool "MemoryStick debugging"
17 help
18 This is an option for use by developers; most people should
19 say N here. This enables MemoryStick core and driver debugging.
20
21
22source "drivers/memstick/core/Kconfig"
23
24source "drivers/memstick/host/Kconfig"
25
26endif # MEMSTICK
diff --git a/drivers/memstick/Makefile b/drivers/memstick/Makefile
new file mode 100644
index 000000000000..dc160fb43515
--- /dev/null
+++ b/drivers/memstick/Makefile
@@ -0,0 +1,11 @@
1#
2# Makefile for the kernel MemoryStick device drivers.
3#
4
5ifeq ($(CONFIG_MEMSTICK_DEBUG),y)
6 EXTRA_CFLAGS += -DDEBUG
7endif
8
9obj-$(CONFIG_MEMSTICK) += core/
10obj-$(CONFIG_MEMSTICK) += host/
11
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
5comment "MemoryStick drivers"
6
7config 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
19config 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
5ifeq ($(CONFIG_MEMSTICK_DEBUG),y)
6 EXTRA_CFLAGS += -DDEBUG
7endif
8
9obj-$(CONFIG_MEMSTICK) += memstick.o
10
11obj-$(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
23static unsigned int cmd_retries = 3;
24module_param(cmd_retries, uint, 0644);
25
26static struct workqueue_struct *workqueue;
27static DEFINE_IDR(memstick_host_idr);
28static DEFINE_SPINLOCK(memstick_host_lock);
29
30static 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
43static 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
62static 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
79static 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
96static 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
115static 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
128static 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) \
149static 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
157MEMSTICK_ATTR(type, "%02X");
158MEMSTICK_ATTR(category, "%02X");
159MEMSTICK_ATTR(class, "%02X");
160
161#define MEMSTICK_ATTR_RO(name) __ATTR(name, S_IRUGO, name##_show, NULL)
162
163static 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
170static 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
181static 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
188static struct class memstick_host_class = {
189 .name = "memstick_host",
190 .release = memstick_free
191};
192
193static 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
200static 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 */
209void memstick_detect_change(struct memstick_host *host)
210{
211 queue_work(workqueue, &host->media_checker);
212}
213EXPORT_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 */
225int 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}
245EXPORT_SYMBOL(memstick_next_req);
246
247/**
248 * memstick_new_req - notify the host that some requests are pending
249 * @host - host to use
250 */
251void memstick_new_req(struct memstick_host *host)
252{
253 host->retries = cmd_retries;
254 host->request(host);
255}
256EXPORT_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 */
264void 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}
283EXPORT_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 */
296void 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}
318EXPORT_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
327static 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
350static 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 */
370int 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}
378EXPORT_SYMBOL(memstick_set_rw_addr);
379
380static 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;
416err_out:
417 host->card = old_card;
418 kfree(card);
419 return NULL;
420}
421
422static 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
429static 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 */
481struct 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}
496EXPORT_SYMBOL(memstick_alloc_host);
497
498/**
499 * memstick_add_host - start request processing on memstick host
500 * @host - host to use
501 */
502int 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}
530EXPORT_SYMBOL(memstick_add_host);
531
532/**
533 * memstick_remove_host - stop request processing on memstick host
534 * @host - host to use
535 */
536void 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}
551EXPORT_SYMBOL(memstick_remove_host);
552
553/**
554 * memstick_free_host - free memstick host
555 * @host - host to use
556 */
557void memstick_free_host(struct memstick_host *host)
558{
559 mutex_destroy(&host->lock);
560 class_device_put(&host->cdev);
561}
562EXPORT_SYMBOL(memstick_free_host);
563
564int memstick_register_driver(struct memstick_driver *drv)
565{
566 drv->driver.bus = &memstick_bus_type;
567
568 return driver_register(&drv->driver);
569}
570EXPORT_SYMBOL(memstick_register_driver);
571
572void memstick_unregister_driver(struct memstick_driver *drv)
573{
574 driver_unregister(&drv->driver);
575}
576EXPORT_SYMBOL(memstick_unregister_driver);
577
578
579static 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
600static 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
608module_init(memstick_init);
609module_exit(memstick_exit);
610
611MODULE_AUTHOR("Alex Dubov");
612MODULE_LICENSE("GPL");
613MODULE_DESCRIPTION("Sony MemoryStick core driver");
614MODULE_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
24static int major;
25module_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
33enum {
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
44struct 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
52struct mspro_attr_entry {
53 unsigned int address;
54 unsigned int size;
55 unsigned char id;
56 unsigned char reserved[3];
57} __attribute__((packed));
58
59struct 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
67struct 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
100struct 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
113struct 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
122struct 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
154static DEFINE_IDR(mspro_block_disk_idr);
155static DEFINE_MUTEX(mspro_block_disk_lock);
156
157/*** Block device ***/
158
159static 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
181static 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
203static 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
209static 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
221static 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
230static 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
237static 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
261typedef ssize_t (*sysfs_show_t)(struct device *dev,
262 struct device_attribute *attr,
263 char *buffer);
264
265static 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
287static 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
356static 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
367static 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
401static 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
424static 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
449static 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
459static 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
469static 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
489static 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
514static 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];
541has_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
606static 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 &param, 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
680static 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
692static 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
727static 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
744static 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
756static 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, &param,
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 */
798static 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, &param,
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 *)&param, 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;
954out_free_buffer:
955 kfree(buffer);
956out_free_attr:
957 kfree(attr);
958 return rc;
959}
960
961static 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
1008static 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
1107out_put_disk:
1108 put_disk(msb->disk);
1109out_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
1116static 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
1134static 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
1141static 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);
1168out_free:
1169 memstick_set_drvdata(card, NULL);
1170 mspro_block_data_clear(msb);
1171 kfree(msb);
1172 return rc;
1173}
1174
1175static 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
1211static 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
1230static 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
1277out_free:
1278 memstick_set_drvdata(card, msb);
1279 mspro_block_data_clear(new_msb);
1280 kfree(new_msb);
1281out_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
1299static 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
1306static 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
1318static 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
1337static 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
1344module_init(mspro_block_init);
1345module_exit(mspro_block_exit);
1346
1347MODULE_LICENSE("GPL");
1348MODULE_AUTHOR("Alex Dubov");
1349MODULE_DESCRIPTION("Sony MemoryStickPro block device driver");
1350MODULE_DEVICE_TABLE(memstick, mspro_block_id_tbl);
1351MODULE_VERSION(DRIVER_VERSION);
diff --git a/drivers/memstick/host/Kconfig b/drivers/memstick/host/Kconfig
new file mode 100644
index 000000000000..c002fcc3c879
--- /dev/null
+++ b/drivers/memstick/host/Kconfig
@@ -0,0 +1,22 @@
1#
2# MemoryStick host controller drivers
3#
4
5comment "MemoryStick Host Controller Drivers"
6
7config MEMSTICK_TIFM_MS
8 tristate "TI Flash Media MemoryStick Interface support (EXPERIMENTAL)"
9 depends on EXPERIMENTAL && PCI
10 select TIFM_CORE
11 help
12 Say Y here if you want to be able to access MemoryStick cards with
13 the Texas Instruments(R) Flash Media card reader, found in many
14 laptops.
15 This option 'selects' (turns on, enables) 'TIFM_CORE', but you
16 probably also need appropriate card reader host adapter, such as
17 'Misc devices: TI Flash Media PCI74xx/PCI76xx host adapter support
18 (TIFM_7XX1)'.
19
20 To compile this driver as a module, choose M here: the
21 module will be called tifm_ms.
22
diff --git a/drivers/memstick/host/Makefile b/drivers/memstick/host/Makefile
new file mode 100644
index 000000000000..ee666380efa1
--- /dev/null
+++ b/drivers/memstick/host/Makefile
@@ -0,0 +1,10 @@
1#
2# Makefile for MemoryStick host controller drivers
3#
4
5ifeq ($(CONFIG_MEMSTICK_DEBUG),y)
6 EXTRA_CFLAGS += -DDEBUG
7endif
8
9obj-$(CONFIG_MEMSTICK_TIFM_MS) += tifm_ms.o
10
diff --git a/drivers/memstick/host/tifm_ms.c b/drivers/memstick/host/tifm_ms.c
new file mode 100644
index 000000000000..f55b71a4337d
--- /dev/null
+++ b/drivers/memstick/host/tifm_ms.c
@@ -0,0 +1,685 @@
1/*
2 * TI FlashMedia driver
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/tifm.h>
16#include <linux/memstick.h>
17#include <linux/highmem.h>
18#include <linux/scatterlist.h>
19#include <linux/log2.h>
20#include <asm/io.h>
21
22#define DRIVER_NAME "tifm_ms"
23#define DRIVER_VERSION "0.1"
24
25static int no_dma;
26module_param(no_dma, bool, 0644);
27
28#define TIFM_MS_TIMEOUT 0x00100
29#define TIFM_MS_BADCRC 0x00200
30#define TIFM_MS_EOTPC 0x01000
31#define TIFM_MS_INT 0x02000
32
33/* The meaning of the bit majority in this constant is unknown. */
34#define TIFM_MS_SERIAL 0x04010
35
36#define TIFM_MS_SYS_LATCH 0x00100
37#define TIFM_MS_SYS_NOT_RDY 0x00800
38#define TIFM_MS_SYS_DATA 0x10000
39
40/* Hardware flags */
41enum {
42 CMD_READY = 0x0001,
43 FIFO_READY = 0x0002,
44 CARD_READY = 0x0004,
45 DATA_CARRY = 0x0008
46};
47
48struct tifm_ms {
49 struct tifm_dev *dev;
50 unsigned short eject:1,
51 no_dma:1;
52 unsigned short cmd_flags;
53 unsigned int mode_mask;
54 unsigned int block_pos;
55 unsigned long timeout_jiffies;
56
57 struct timer_list timer;
58 struct memstick_request *req;
59 unsigned int io_word;
60};
61
62static void tifm_ms_read_fifo(struct tifm_ms *host, unsigned int fifo_offset,
63 struct page *pg, unsigned int page_off,
64 unsigned int length)
65{
66 struct tifm_dev *sock = host->dev;
67 unsigned int cnt = 0, off = 0;
68 unsigned char *buf = kmap_atomic(pg, KM_BIO_DST_IRQ) + page_off;
69
70 if (host->cmd_flags & DATA_CARRY) {
71 while ((fifo_offset & 3) && length) {
72 buf[off++] = host->io_word & 0xff;
73 host->io_word >>= 8;
74 length--;
75 fifo_offset++;
76 }
77 if (!(fifo_offset & 3))
78 host->cmd_flags &= ~DATA_CARRY;
79 if (!length)
80 return;
81 }
82
83 do {
84 host->io_word = readl(sock->addr + SOCK_FIFO_ACCESS
85 + fifo_offset);
86 cnt = 4;
87 while (length && cnt) {
88 buf[off++] = (host->io_word >> 8) & 0xff;
89 cnt--;
90 length--;
91 }
92 fifo_offset += 4 - cnt;
93 } while (length);
94
95 if (cnt)
96 host->cmd_flags |= DATA_CARRY;
97
98 kunmap_atomic(buf - page_off, KM_BIO_DST_IRQ);
99}
100
101static void tifm_ms_write_fifo(struct tifm_ms *host, unsigned int fifo_offset,
102 struct page *pg, unsigned int page_off,
103 unsigned int length)
104{
105 struct tifm_dev *sock = host->dev;
106 unsigned int cnt = 0, off = 0;
107 unsigned char *buf = kmap_atomic(pg, KM_BIO_SRC_IRQ) + page_off;
108
109 if (host->cmd_flags & DATA_CARRY) {
110 while (fifo_offset & 3) {
111 host->io_word |= buf[off++] << (8 * (fifo_offset & 3));
112 length--;
113 fifo_offset++;
114 }
115 if (!(fifo_offset & 3)) {
116 writel(host->io_word, sock->addr + SOCK_FIFO_ACCESS
117 + fifo_offset - 4);
118
119 host->cmd_flags &= ~DATA_CARRY;
120 }
121 if (!length)
122 return;
123 }
124
125 do {
126 cnt = 4;
127 host->io_word = 0;
128 while (length && cnt) {
129 host->io_word |= buf[off++] << (4 - cnt);
130 cnt--;
131 length--;
132 }
133 fifo_offset += 4 - cnt;
134 if (!cnt)
135 writel(host->io_word, sock->addr + SOCK_FIFO_ACCESS
136 + fifo_offset - 4);
137
138 } while (length);
139
140 if (cnt)
141 host->cmd_flags |= DATA_CARRY;
142
143 kunmap_atomic(buf - page_off, KM_BIO_SRC_IRQ);
144}
145
146static void tifm_ms_move_block(struct tifm_ms *host, unsigned int length)
147{
148 unsigned int t_size;
149 unsigned int off = host->req->sg.offset + host->block_pos;
150 unsigned int p_off, p_cnt;
151 struct page *pg;
152 unsigned long flags;
153
154 dev_dbg(&host->dev->dev, "moving block\n");
155 local_irq_save(flags);
156 t_size = length;
157 while (t_size) {
158 pg = nth_page(sg_page(&host->req->sg), off >> PAGE_SHIFT);
159 p_off = offset_in_page(off);
160 p_cnt = PAGE_SIZE - p_off;
161 p_cnt = min(p_cnt, t_size);
162
163 if (host->req->data_dir == WRITE)
164 tifm_ms_write_fifo(host, length - t_size,
165 pg, p_off, p_cnt);
166 else
167 tifm_ms_read_fifo(host, length - t_size,
168 pg, p_off, p_cnt);
169
170 t_size -= p_cnt;
171 }
172 local_irq_restore(flags);
173}
174
175static int tifm_ms_transfer_data(struct tifm_ms *host, int skip)
176{
177 struct tifm_dev *sock = host->dev;
178 unsigned int length = host->req->sg.length - host->block_pos;
179
180 if (!length)
181 return 1;
182
183 if (length > TIFM_FIFO_SIZE)
184 length = TIFM_FIFO_SIZE;
185
186 if (!skip) {
187 tifm_ms_move_block(host, length);
188 host->block_pos += length;
189 }
190
191 if ((host->req->data_dir == READ)
192 && (host->block_pos == host->req->sg.length))
193 return 1;
194
195 writel(ilog2(length) - 2, sock->addr + SOCK_FIFO_PAGE_SIZE);
196 if (host->req->data_dir == WRITE)
197 writel((1 << 8) | TIFM_DMA_TX, sock->addr + SOCK_DMA_CONTROL);
198 else
199 writel((1 << 8), sock->addr + SOCK_DMA_CONTROL);
200
201 return 0;
202}
203
204static int tifm_ms_issue_cmd(struct tifm_ms *host)
205{
206 struct tifm_dev *sock = host->dev;
207 unsigned char *data;
208 unsigned int data_len = 0, cmd = 0, cmd_mask = 0, cnt, tval = 0;
209
210 host->cmd_flags = 0;
211
212 if (host->req->io_type == MEMSTICK_IO_SG) {
213 if (!host->no_dma) {
214 if (1 != tifm_map_sg(sock, &host->req->sg, 1,
215 host->req->data_dir == READ
216 ? PCI_DMA_FROMDEVICE
217 : PCI_DMA_TODEVICE)) {
218 host->req->error = -ENOMEM;
219 return host->req->error;
220 }
221 data_len = sg_dma_len(&host->req->sg);
222 } else
223 data_len = host->req->sg.length;
224
225 writel(TIFM_FIFO_INT_SETALL,
226 sock->addr + SOCK_DMA_FIFO_INT_ENABLE_CLEAR);
227 writel(TIFM_FIFO_ENABLE,
228 sock->addr + SOCK_FIFO_CONTROL);
229 writel(TIFM_FIFO_INTMASK,
230 sock->addr + SOCK_DMA_FIFO_INT_ENABLE_SET);
231
232 if (!host->no_dma) {
233 writel(ilog2(data_len) - 2,
234 sock->addr + SOCK_FIFO_PAGE_SIZE);
235 writel(sg_dma_address(&host->req->sg),
236 sock->addr + SOCK_DMA_ADDRESS);
237 if (host->req->data_dir == WRITE)
238 writel((1 << 8) | TIFM_DMA_TX | TIFM_DMA_EN,
239 sock->addr + SOCK_DMA_CONTROL);
240 else
241 writel((1 << 8) | TIFM_DMA_EN,
242 sock->addr + SOCK_DMA_CONTROL);
243 } else {
244 tifm_ms_transfer_data(host,
245 host->req->data_dir == READ);
246 }
247
248 cmd_mask = readl(sock->addr + SOCK_MS_SYSTEM);
249 cmd_mask |= TIFM_MS_SYS_DATA | TIFM_MS_SYS_NOT_RDY;
250 writel(cmd_mask, sock->addr + SOCK_MS_SYSTEM);
251 } else if (host->req->io_type == MEMSTICK_IO_VAL) {
252 data = host->req->data;
253 data_len = host->req->data_len;
254
255 cmd_mask = host->mode_mask | 0x2607; /* unknown constant */
256
257 if (host->req->data_dir == WRITE) {
258 cmd_mask |= TIFM_MS_SYS_LATCH;
259 writel(cmd_mask, sock->addr + SOCK_MS_SYSTEM);
260 for (cnt = 0; (data_len - cnt) >= 4; cnt += 4) {
261 writel(TIFM_MS_SYS_LATCH
262 | readl(sock->addr + SOCK_MS_SYSTEM),
263 sock->addr + SOCK_MS_SYSTEM);
264 __raw_writel(*(unsigned int *)(data + cnt),
265 sock->addr + SOCK_MS_DATA);
266 dev_dbg(&sock->dev, "writing %x\n",
267 *(int *)(data + cnt));
268 }
269 switch (data_len - cnt) {
270 case 3:
271 tval |= data[cnt + 2] << 16;
272 case 2:
273 tval |= data[cnt + 1] << 8;
274 case 1:
275 tval |= data[cnt];
276 writel(TIFM_MS_SYS_LATCH
277 | readl(sock->addr + SOCK_MS_SYSTEM),
278 sock->addr + SOCK_MS_SYSTEM);
279 writel(tval, sock->addr + SOCK_MS_DATA);
280 dev_dbg(&sock->dev, "writing %x\n", tval);
281 }
282
283 writel(TIFM_MS_SYS_LATCH
284 | readl(sock->addr + SOCK_MS_SYSTEM),
285 sock + SOCK_MS_SYSTEM);
286 writel(0, sock->addr + SOCK_MS_DATA);
287 dev_dbg(&sock->dev, "writing %x\n", 0);
288
289 } else
290 writel(cmd_mask, sock->addr + SOCK_MS_SYSTEM);
291
292 cmd_mask = readl(sock->addr + SOCK_MS_SYSTEM);
293 cmd_mask &= ~TIFM_MS_SYS_DATA;
294 cmd_mask |= TIFM_MS_SYS_NOT_RDY;
295 dev_dbg(&sock->dev, "mask %x\n", cmd_mask);
296 writel(cmd_mask, sock->addr + SOCK_MS_SYSTEM);
297 } else
298 BUG();
299
300 mod_timer(&host->timer, jiffies + host->timeout_jiffies);
301 writel(TIFM_CTRL_LED | readl(sock->addr + SOCK_CONTROL),
302 sock->addr + SOCK_CONTROL);
303 host->req->error = 0;
304
305 cmd = (host->req->tpc & 0xf) << 12;
306 cmd |= data_len;
307 writel(cmd, sock->addr + SOCK_MS_COMMAND);
308
309 dev_dbg(&sock->dev, "executing TPC %x, %x\n", cmd, cmd_mask);
310 return 0;
311}
312
313static void tifm_ms_complete_cmd(struct tifm_ms *host)
314{
315 struct tifm_dev *sock = host->dev;
316 struct memstick_host *msh = tifm_get_drvdata(sock);
317 unsigned int tval = 0, data_len;
318 unsigned char *data;
319 int rc;
320
321 del_timer(&host->timer);
322 if (host->req->io_type == MEMSTICK_IO_SG) {
323 if (!host->no_dma)
324 tifm_unmap_sg(sock, &host->req->sg, 1,
325 host->req->data_dir == READ
326 ? PCI_DMA_FROMDEVICE
327 : PCI_DMA_TODEVICE);
328 } else if (host->req->io_type == MEMSTICK_IO_VAL) {
329 writel(~TIFM_MS_SYS_DATA & readl(sock->addr + SOCK_MS_SYSTEM),
330 sock->addr + SOCK_MS_SYSTEM);
331
332 data = host->req->data;
333 data_len = host->req->data_len;
334
335 if (host->req->data_dir == READ) {
336 for (rc = 0; (data_len - rc) >= 4; rc += 4)
337 *(int *)(data + rc)
338 = __raw_readl(sock->addr
339 + SOCK_MS_DATA);
340
341 if (data_len - rc)
342 tval = readl(sock->addr + SOCK_MS_DATA);
343 switch (data_len - rc) {
344 case 3:
345 data[rc + 2] = (tval >> 16) & 0xff;
346 case 2:
347 data[rc + 1] = (tval >> 8) & 0xff;
348 case 1:
349 data[rc] = tval & 0xff;
350 }
351 readl(sock->addr + SOCK_MS_DATA);
352 }
353 }
354
355 writel((~TIFM_CTRL_LED) & readl(sock->addr + SOCK_CONTROL),
356 sock->addr + SOCK_CONTROL);
357
358 do {
359 rc = memstick_next_req(msh, &host->req);
360 } while (!rc && tifm_ms_issue_cmd(host));
361}
362
363static int tifm_ms_check_status(struct tifm_ms *host)
364{
365 if (!host->req->error) {
366 if (!(host->cmd_flags & CMD_READY))
367 return 1;
368 if ((host->req->io_type == MEMSTICK_IO_SG)
369 && !(host->cmd_flags & FIFO_READY))
370 return 1;
371 if (host->req->need_card_int
372 && !(host->cmd_flags & CARD_READY))
373 return 1;
374 }
375 return 0;
376}
377
378/* Called from interrupt handler */
379static void tifm_ms_data_event(struct tifm_dev *sock)
380{
381 struct tifm_ms *host;
382 unsigned int fifo_status = 0;
383 int rc = 1;
384
385 spin_lock(&sock->lock);
386 host = memstick_priv((struct memstick_host *)tifm_get_drvdata(sock));
387 fifo_status = readl(sock->addr + SOCK_DMA_FIFO_STATUS);
388 dev_dbg(&sock->dev, "data event: fifo_status %x, flags %x\n",
389 fifo_status, host->cmd_flags);
390
391 if (host->req) {
392 if (fifo_status & TIFM_FIFO_READY) {
393 if (!host->no_dma || tifm_ms_transfer_data(host, 0)) {
394 host->cmd_flags |= FIFO_READY;
395 rc = tifm_ms_check_status(host);
396 }
397 }
398 }
399
400 writel(fifo_status, sock->addr + SOCK_DMA_FIFO_STATUS);
401 if (!rc)
402 tifm_ms_complete_cmd(host);
403
404 spin_unlock(&sock->lock);
405}
406
407
408/* Called from interrupt handler */
409static void tifm_ms_card_event(struct tifm_dev *sock)
410{
411 struct tifm_ms *host;
412 unsigned int host_status = 0;
413 int rc = 1;
414
415 spin_lock(&sock->lock);
416 host = memstick_priv((struct memstick_host *)tifm_get_drvdata(sock));
417 host_status = readl(sock->addr + SOCK_MS_STATUS);
418 dev_dbg(&sock->dev, "host event: host_status %x, flags %x\n",
419 host_status, host->cmd_flags);
420
421 if (host->req) {
422 if (host_status & TIFM_MS_TIMEOUT)
423 host->req->error = -ETIME;
424 else if (host_status & TIFM_MS_BADCRC)
425 host->req->error = -EILSEQ;
426
427 if (host->req->error) {
428 writel(TIFM_FIFO_INT_SETALL,
429 sock->addr + SOCK_DMA_FIFO_INT_ENABLE_CLEAR);
430 writel(TIFM_DMA_RESET, sock->addr + SOCK_DMA_CONTROL);
431 }
432
433 if (host_status & TIFM_MS_EOTPC)
434 host->cmd_flags |= CMD_READY;
435 if (host_status & TIFM_MS_INT)
436 host->cmd_flags |= CARD_READY;
437
438 rc = tifm_ms_check_status(host);
439
440 }
441
442 writel(TIFM_MS_SYS_NOT_RDY | readl(sock->addr + SOCK_MS_SYSTEM),
443 sock->addr + SOCK_MS_SYSTEM);
444 writel((~TIFM_MS_SYS_DATA) & readl(sock->addr + SOCK_MS_SYSTEM),
445 sock->addr + SOCK_MS_SYSTEM);
446
447 if (!rc)
448 tifm_ms_complete_cmd(host);
449
450 spin_unlock(&sock->lock);
451 return;
452}
453
454static void tifm_ms_request(struct memstick_host *msh)
455{
456 struct tifm_ms *host = memstick_priv(msh);
457 struct tifm_dev *sock = host->dev;
458 unsigned long flags;
459 int rc;
460
461 spin_lock_irqsave(&sock->lock, flags);
462 if (host->req) {
463 printk(KERN_ERR "%s : unfinished request detected\n",
464 sock->dev.bus_id);
465 spin_unlock_irqrestore(&sock->lock, flags);
466 tifm_eject(host->dev);
467 return;
468 }
469
470 if (host->eject) {
471 do {
472 rc = memstick_next_req(msh, &host->req);
473 if (!rc)
474 host->req->error = -ETIME;
475 } while (!rc);
476 spin_unlock_irqrestore(&sock->lock, flags);
477 return;
478 }
479
480 do {
481 rc = memstick_next_req(msh, &host->req);
482 } while (!rc && tifm_ms_issue_cmd(host));
483
484 spin_unlock_irqrestore(&sock->lock, flags);
485 return;
486}
487
488static void tifm_ms_set_param(struct memstick_host *msh,
489 enum memstick_param param,
490 int value)
491{
492 struct tifm_ms *host = memstick_priv(msh);
493 struct tifm_dev *sock = host->dev;
494 unsigned long flags;
495
496 spin_lock_irqsave(&sock->lock, flags);
497
498 switch (param) {
499 case MEMSTICK_POWER:
500 /* this is set by card detection mechanism */
501 break;
502 case MEMSTICK_INTERFACE:
503 if (value == MEMSTICK_SERIAL) {
504 host->mode_mask = TIFM_MS_SERIAL;
505 writel((~TIFM_CTRL_FAST_CLK)
506 & readl(sock->addr + SOCK_CONTROL),
507 sock->addr + SOCK_CONTROL);
508 } else if (value == MEMSTICK_PARALLEL) {
509 host->mode_mask = 0;
510 writel(TIFM_CTRL_FAST_CLK
511 | readl(sock->addr + SOCK_CONTROL),
512 sock->addr + SOCK_CONTROL);
513 }
514 break;
515 };
516
517 spin_unlock_irqrestore(&sock->lock, flags);
518}
519
520static void tifm_ms_abort(unsigned long data)
521{
522 struct tifm_ms *host = (struct tifm_ms *)data;
523
524 dev_dbg(&host->dev->dev, "status %x\n",
525 readl(host->dev->addr + SOCK_MS_STATUS));
526 printk(KERN_ERR
527 "%s : card failed to respond for a long period of time "
528 "(%x, %x)\n",
529 host->dev->dev.bus_id, host->req ? host->req->tpc : 0,
530 host->cmd_flags);
531
532 tifm_eject(host->dev);
533}
534
535static int tifm_ms_initialize_host(struct tifm_ms *host)
536{
537 struct tifm_dev *sock = host->dev;
538 struct memstick_host *msh = tifm_get_drvdata(sock);
539
540 host->mode_mask = TIFM_MS_SERIAL;
541 writel(0x8000, sock->addr + SOCK_MS_SYSTEM);
542 writel(0x0200 | TIFM_MS_SYS_NOT_RDY, sock->addr + SOCK_MS_SYSTEM);
543 writel(0xffffffff, sock->addr + SOCK_MS_STATUS);
544 if (tifm_has_ms_pif(sock))
545 msh->caps |= MEMSTICK_CAP_PARALLEL;
546
547 return 0;
548}
549
550static int tifm_ms_probe(struct tifm_dev *sock)
551{
552 struct memstick_host *msh;
553 struct tifm_ms *host;
554 int rc = -EIO;
555
556 if (!(TIFM_SOCK_STATE_OCCUPIED
557 & readl(sock->addr + SOCK_PRESENT_STATE))) {
558 printk(KERN_WARNING "%s : card gone, unexpectedly\n",
559 sock->dev.bus_id);
560 return rc;
561 }
562
563 msh = memstick_alloc_host(sizeof(struct tifm_ms), &sock->dev);
564 if (!msh)
565 return -ENOMEM;
566
567 host = memstick_priv(msh);
568 tifm_set_drvdata(sock, msh);
569 host->dev = sock;
570 host->timeout_jiffies = msecs_to_jiffies(1000);
571 host->no_dma = no_dma;
572
573 setup_timer(&host->timer, tifm_ms_abort, (unsigned long)host);
574
575 msh->request = tifm_ms_request;
576 msh->set_param = tifm_ms_set_param;
577 sock->card_event = tifm_ms_card_event;
578 sock->data_event = tifm_ms_data_event;
579 rc = tifm_ms_initialize_host(host);
580
581 if (!rc)
582 rc = memstick_add_host(msh);
583 if (!rc)
584 return 0;
585
586 memstick_free_host(msh);
587 return rc;
588}
589
590static void tifm_ms_remove(struct tifm_dev *sock)
591{
592 struct memstick_host *msh = tifm_get_drvdata(sock);
593 struct tifm_ms *host = memstick_priv(msh);
594 int rc = 0;
595 unsigned long flags;
596
597 spin_lock_irqsave(&sock->lock, flags);
598 host->eject = 1;
599 if (host->req) {
600 del_timer(&host->timer);
601 writel(TIFM_FIFO_INT_SETALL,
602 sock->addr + SOCK_DMA_FIFO_INT_ENABLE_CLEAR);
603 writel(TIFM_DMA_RESET, sock->addr + SOCK_DMA_CONTROL);
604 if ((host->req->io_type == MEMSTICK_IO_SG) && !host->no_dma)
605 tifm_unmap_sg(sock, &host->req->sg, 1,
606 host->req->data_dir == READ
607 ? PCI_DMA_TODEVICE
608 : PCI_DMA_FROMDEVICE);
609 host->req->error = -ETIME;
610
611 do {
612 rc = memstick_next_req(msh, &host->req);
613 if (!rc)
614 host->req->error = -ETIME;
615 } while (!rc);
616 }
617 spin_unlock_irqrestore(&sock->lock, flags);
618
619 memstick_remove_host(msh);
620
621 writel(0x0200 | TIFM_MS_SYS_NOT_RDY, sock->addr + SOCK_MS_SYSTEM);
622 writel(0xffffffff, sock->addr + SOCK_MS_STATUS);
623
624 memstick_free_host(msh);
625}
626
627#ifdef CONFIG_PM
628
629static int tifm_ms_suspend(struct tifm_dev *sock, pm_message_t state)
630{
631 return 0;
632}
633
634static int tifm_ms_resume(struct tifm_dev *sock)
635{
636 struct memstick_host *msh = tifm_get_drvdata(sock);
637 struct tifm_ms *host = memstick_priv(msh);
638
639 tifm_ms_initialize_host(host);
640 memstick_detect_change(msh);
641
642 return 0;
643}
644
645#else
646
647#define tifm_ms_suspend NULL
648#define tifm_ms_resume NULL
649
650#endif /* CONFIG_PM */
651
652static struct tifm_device_id tifm_ms_id_tbl[] = {
653 { TIFM_TYPE_MS }, { 0 }
654};
655
656static struct tifm_driver tifm_ms_driver = {
657 .driver = {
658 .name = DRIVER_NAME,
659 .owner = THIS_MODULE
660 },
661 .id_table = tifm_ms_id_tbl,
662 .probe = tifm_ms_probe,
663 .remove = tifm_ms_remove,
664 .suspend = tifm_ms_suspend,
665 .resume = tifm_ms_resume
666};
667
668static int __init tifm_ms_init(void)
669{
670 return tifm_register_driver(&tifm_ms_driver);
671}
672
673static void __exit tifm_ms_exit(void)
674{
675 tifm_unregister_driver(&tifm_ms_driver);
676}
677
678MODULE_AUTHOR("Alex Dubov");
679MODULE_DESCRIPTION("TI FlashMedia MemoryStick driver");
680MODULE_LICENSE("GPL");
681MODULE_DEVICE_TABLE(tifm, tifm_ms_id_tbl);
682MODULE_VERSION(DRIVER_VERSION);
683
684module_init(tifm_ms_init);
685module_exit(tifm_ms_exit);