Merge git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[wrt350n-kernel.git] / drivers / memstick / core / memstick.c
blob1f0fd44327577629367d921a3009c0b47e9bb271
1 /*
2 * Sony MemoryStick support
4 * Copyright (C) 2007 Alex Dubov <oakad@yahoo.com>
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.
10 * Special thanks to Carlos Corbacho for providing various MemoryStick cards
11 * that made this driver possible.
15 #include <linux/memstick.h>
16 #include <linux/idr.h>
17 #include <linux/fs.h>
18 #include <linux/delay.h>
20 #define DRIVER_NAME "memstick"
21 <<<<<<< HEAD:drivers/memstick/core/memstick.c
22 #define DRIVER_VERSION "0.2"
23 =======
24 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/memstick/core/memstick.c
26 static unsigned int cmd_retries = 3;
27 module_param(cmd_retries, uint, 0644);
29 static struct workqueue_struct *workqueue;
30 static DEFINE_IDR(memstick_host_idr);
31 static DEFINE_SPINLOCK(memstick_host_lock);
33 static int memstick_dev_match(struct memstick_dev *card,
34 struct memstick_device_id *id)
36 if (id->match_flags & MEMSTICK_MATCH_ALL) {
37 if ((id->type == card->id.type)
38 && (id->category == card->id.category)
39 && (id->class == card->id.class))
40 return 1;
43 return 0;
46 static int memstick_bus_match(struct device *dev, struct device_driver *drv)
48 struct memstick_dev *card = container_of(dev, struct memstick_dev,
49 dev);
50 struct memstick_driver *ms_drv = container_of(drv,
51 struct memstick_driver,
52 driver);
53 struct memstick_device_id *ids = ms_drv->id_table;
55 if (ids) {
56 while (ids->match_flags) {
57 if (memstick_dev_match(card, ids))
58 return 1;
59 ++ids;
62 return 0;
65 static int memstick_uevent(struct device *dev, struct kobj_uevent_env *env)
67 struct memstick_dev *card = container_of(dev, struct memstick_dev,
68 dev);
70 if (add_uevent_var(env, "MEMSTICK_TYPE=%02X", card->id.type))
71 return -ENOMEM;
73 if (add_uevent_var(env, "MEMSTICK_CATEGORY=%02X", card->id.category))
74 return -ENOMEM;
76 if (add_uevent_var(env, "MEMSTICK_CLASS=%02X", card->id.class))
77 return -ENOMEM;
79 return 0;
82 static int memstick_device_probe(struct device *dev)
84 struct memstick_dev *card = container_of(dev, struct memstick_dev,
85 dev);
86 struct memstick_driver *drv = container_of(dev->driver,
87 struct memstick_driver,
88 driver);
89 int rc = -ENODEV;
91 if (dev->driver && drv->probe) {
92 rc = drv->probe(card);
93 if (!rc)
94 get_device(dev);
96 return rc;
99 static int memstick_device_remove(struct device *dev)
101 struct memstick_dev *card = container_of(dev, struct memstick_dev,
102 dev);
103 struct memstick_driver *drv = container_of(dev->driver,
104 struct memstick_driver,
105 driver);
107 if (dev->driver && drv->remove) {
108 drv->remove(card);
109 card->dev.driver = NULL;
112 put_device(dev);
113 return 0;
116 #ifdef CONFIG_PM
118 static int memstick_device_suspend(struct device *dev, pm_message_t state)
120 struct memstick_dev *card = container_of(dev, struct memstick_dev,
121 dev);
122 struct memstick_driver *drv = container_of(dev->driver,
123 struct memstick_driver,
124 driver);
126 if (dev->driver && drv->suspend)
127 return drv->suspend(card, state);
128 return 0;
131 static int memstick_device_resume(struct device *dev)
133 struct memstick_dev *card = container_of(dev, struct memstick_dev,
134 dev);
135 struct memstick_driver *drv = container_of(dev->driver,
136 struct memstick_driver,
137 driver);
139 if (dev->driver && drv->resume)
140 return drv->resume(card);
141 return 0;
144 #else
146 #define memstick_device_suspend NULL
147 #define memstick_device_resume NULL
149 #endif /* CONFIG_PM */
151 #define MEMSTICK_ATTR(name, format) \
152 static ssize_t name##_show(struct device *dev, struct device_attribute *attr, \
153 char *buf) \
155 struct memstick_dev *card = container_of(dev, struct memstick_dev, \
156 dev); \
157 return sprintf(buf, format, card->id.name); \
160 MEMSTICK_ATTR(type, "%02X");
161 MEMSTICK_ATTR(category, "%02X");
162 MEMSTICK_ATTR(class, "%02X");
164 #define MEMSTICK_ATTR_RO(name) __ATTR(name, S_IRUGO, name##_show, NULL)
166 static struct device_attribute memstick_dev_attrs[] = {
167 MEMSTICK_ATTR_RO(type),
168 MEMSTICK_ATTR_RO(category),
169 MEMSTICK_ATTR_RO(class),
170 __ATTR_NULL
173 static struct bus_type memstick_bus_type = {
174 .name = "memstick",
175 .dev_attrs = memstick_dev_attrs,
176 .match = memstick_bus_match,
177 .uevent = memstick_uevent,
178 .probe = memstick_device_probe,
179 .remove = memstick_device_remove,
180 .suspend = memstick_device_suspend,
181 .resume = memstick_device_resume
184 static void memstick_free(struct class_device *cdev)
186 struct memstick_host *host = container_of(cdev, struct memstick_host,
187 cdev);
188 kfree(host);
191 static struct class memstick_host_class = {
192 .name = "memstick_host",
193 .release = memstick_free
196 static void memstick_free_card(struct device *dev)
198 struct memstick_dev *card = container_of(dev, struct memstick_dev,
199 dev);
200 kfree(card);
203 static int memstick_dummy_check(struct memstick_dev *card)
205 return 0;
209 * memstick_detect_change - schedule media detection on memstick host
210 * @host - host to use
212 void memstick_detect_change(struct memstick_host *host)
214 queue_work(workqueue, &host->media_checker);
216 EXPORT_SYMBOL(memstick_detect_change);
219 * memstick_next_req - called by host driver to obtain next request to process
220 * @host - host to use
221 * @mrq - pointer to stick the request to
223 * Host calls this function from idle state (*mrq == NULL) or after finishing
224 * previous request (*mrq should point to it). If previous request was
225 * unsuccessful, it is retried for predetermined number of times. Return value
226 * of 0 means that new request was assigned to the host.
228 int memstick_next_req(struct memstick_host *host, struct memstick_request **mrq)
230 int rc = -ENXIO;
232 if ((*mrq) && (*mrq)->error && host->retries) {
233 (*mrq)->error = rc;
234 host->retries--;
235 return 0;
238 if (host->card && host->card->next_request)
239 rc = host->card->next_request(host->card, mrq);
241 if (!rc)
242 <<<<<<< HEAD:drivers/memstick/core/memstick.c
243 host->retries = cmd_retries;
244 =======
245 host->retries = cmd_retries > 1 ? cmd_retries - 1 : 1;
246 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/memstick/core/memstick.c
247 else
248 *mrq = NULL;
250 return rc;
252 EXPORT_SYMBOL(memstick_next_req);
255 * memstick_new_req - notify the host that some requests are pending
256 * @host - host to use
258 void memstick_new_req(struct memstick_host *host)
260 host->retries = cmd_retries;
261 host->request(host);
263 EXPORT_SYMBOL(memstick_new_req);
266 * memstick_init_req_sg - set request fields needed for bulk data transfer
267 * @mrq - request to use
268 * @tpc - memstick Transport Protocol Command
269 * @sg - TPC argument
271 void memstick_init_req_sg(struct memstick_request *mrq, unsigned char tpc,
272 struct scatterlist *sg)
274 mrq->tpc = tpc;
275 if (tpc & 8)
276 mrq->data_dir = WRITE;
277 else
278 mrq->data_dir = READ;
280 mrq->sg = *sg;
281 <<<<<<< HEAD:drivers/memstick/core/memstick.c
282 mrq->io_type = MEMSTICK_IO_SG;
283 =======
284 mrq->long_data = 1;
285 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/memstick/core/memstick.c
287 if (tpc == MS_TPC_SET_CMD || tpc == MS_TPC_EX_SET_CMD)
288 mrq->need_card_int = 1;
289 else
290 mrq->need_card_int = 0;
292 mrq->get_int_reg = 0;
294 EXPORT_SYMBOL(memstick_init_req_sg);
297 * memstick_init_req - set request fields needed for short data transfer
298 * @mrq - request to use
299 * @tpc - memstick Transport Protocol Command
300 * @buf - TPC argument buffer
301 * @length - TPC argument size
303 * The intended use of this function (transfer of data items several bytes
304 * in size) allows us to just copy the value between request structure and
305 * user supplied buffer.
307 void memstick_init_req(struct memstick_request *mrq, unsigned char tpc,
308 void *buf, size_t length)
310 mrq->tpc = tpc;
311 if (tpc & 8)
312 mrq->data_dir = WRITE;
313 else
314 mrq->data_dir = READ;
316 mrq->data_len = length > sizeof(mrq->data) ? sizeof(mrq->data) : length;
317 if (mrq->data_dir == WRITE)
318 memcpy(mrq->data, buf, mrq->data_len);
320 <<<<<<< HEAD:drivers/memstick/core/memstick.c
321 mrq->io_type = MEMSTICK_IO_VAL;
322 =======
323 mrq->long_data = 0;
324 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/memstick/core/memstick.c
326 if (tpc == MS_TPC_SET_CMD || tpc == MS_TPC_EX_SET_CMD)
327 mrq->need_card_int = 1;
328 else
329 mrq->need_card_int = 0;
331 mrq->get_int_reg = 0;
333 EXPORT_SYMBOL(memstick_init_req);
336 * Functions prefixed with "h_" are protocol callbacks. They can be called from
337 * interrupt context. Return value of 0 means that request processing is still
338 * ongoing, while special error value of -EAGAIN means that current request is
339 * finished (and request processor should come back some time later).
342 static int h_memstick_read_dev_id(struct memstick_dev *card,
343 struct memstick_request **mrq)
345 struct ms_id_register id_reg;
347 if (!(*mrq)) {
348 memstick_init_req(&card->current_mrq, MS_TPC_READ_REG, NULL,
349 sizeof(struct ms_id_register));
350 *mrq = &card->current_mrq;
351 return 0;
352 } else {
353 if (!(*mrq)->error) {
354 memcpy(&id_reg, (*mrq)->data, sizeof(id_reg));
355 card->id.match_flags = MEMSTICK_MATCH_ALL;
356 card->id.type = id_reg.type;
357 card->id.category = id_reg.category;
358 card->id.class = id_reg.class;
360 complete(&card->mrq_complete);
361 return -EAGAIN;
365 static int h_memstick_set_rw_addr(struct memstick_dev *card,
366 struct memstick_request **mrq)
368 if (!(*mrq)) {
369 memstick_init_req(&card->current_mrq, MS_TPC_SET_RW_REG_ADRS,
370 (char *)&card->reg_addr,
371 sizeof(card->reg_addr));
372 *mrq = &card->current_mrq;
373 return 0;
374 } else {
375 complete(&card->mrq_complete);
376 return -EAGAIN;
381 * memstick_set_rw_addr - issue SET_RW_REG_ADDR request and wait for it to
382 * complete
383 * @card - media device to use
385 int memstick_set_rw_addr(struct memstick_dev *card)
387 card->next_request = h_memstick_set_rw_addr;
388 memstick_new_req(card->host);
389 wait_for_completion(&card->mrq_complete);
391 return card->current_mrq.error;
393 EXPORT_SYMBOL(memstick_set_rw_addr);
395 static struct memstick_dev *memstick_alloc_card(struct memstick_host *host)
397 struct memstick_dev *card = kzalloc(sizeof(struct memstick_dev),
398 GFP_KERNEL);
399 struct memstick_dev *old_card = host->card;
400 struct ms_id_register id_reg;
402 if (card) {
403 card->host = host;
404 snprintf(card->dev.bus_id, sizeof(card->dev.bus_id),
405 "%s", host->cdev.class_id);
406 card->dev.parent = host->cdev.dev;
407 card->dev.bus = &memstick_bus_type;
408 card->dev.release = memstick_free_card;
409 card->check = memstick_dummy_check;
411 card->reg_addr.r_offset = offsetof(struct ms_register, id);
412 card->reg_addr.r_length = sizeof(id_reg);
413 card->reg_addr.w_offset = offsetof(struct ms_register, id);
414 card->reg_addr.w_length = sizeof(id_reg);
416 init_completion(&card->mrq_complete);
418 host->card = card;
419 if (memstick_set_rw_addr(card))
420 goto err_out;
422 card->next_request = h_memstick_read_dev_id;
423 memstick_new_req(host);
424 wait_for_completion(&card->mrq_complete);
426 if (card->current_mrq.error)
427 goto err_out;
429 host->card = old_card;
430 return card;
431 err_out:
432 host->card = old_card;
433 kfree(card);
434 return NULL;
437 static void memstick_power_on(struct memstick_host *host)
439 host->set_param(host, MEMSTICK_POWER, MEMSTICK_POWER_ON);
440 host->set_param(host, MEMSTICK_INTERFACE, MEMSTICK_SERIAL);
441 msleep(1);
444 static void memstick_check(struct work_struct *work)
446 struct memstick_host *host = container_of(work, struct memstick_host,
447 media_checker);
448 struct memstick_dev *card;
450 dev_dbg(host->cdev.dev, "memstick_check started\n");
451 mutex_lock(&host->lock);
452 if (!host->card)
453 memstick_power_on(host);
455 card = memstick_alloc_card(host);
457 if (!card) {
458 if (host->card) {
459 device_unregister(&host->card->dev);
460 host->card = NULL;
462 } else {
463 dev_dbg(host->cdev.dev, "new card %02x, %02x, %02x\n",
464 card->id.type, card->id.category, card->id.class);
465 if (host->card) {
466 if (memstick_set_rw_addr(host->card)
467 || !memstick_dev_match(host->card, &card->id)
468 || !(host->card->check(host->card))) {
469 device_unregister(&host->card->dev);
470 host->card = NULL;
474 if (!host->card) {
475 host->card = card;
476 if (device_register(&card->dev)) {
477 kfree(host->card);
478 host->card = NULL;
480 } else
481 kfree(card);
484 if (!host->card)
485 host->set_param(host, MEMSTICK_POWER, MEMSTICK_POWER_OFF);
487 mutex_unlock(&host->lock);
488 dev_dbg(host->cdev.dev, "memstick_check finished\n");
492 * memstick_alloc_host - allocate a memstick_host structure
493 * @extra: size of the user private data to allocate
494 * @dev: parent device of the host
496 struct memstick_host *memstick_alloc_host(unsigned int extra,
497 struct device *dev)
499 struct memstick_host *host;
501 host = kzalloc(sizeof(struct memstick_host) + extra, GFP_KERNEL);
502 if (host) {
503 mutex_init(&host->lock);
504 INIT_WORK(&host->media_checker, memstick_check);
505 host->cdev.class = &memstick_host_class;
506 host->cdev.dev = dev;
507 class_device_initialize(&host->cdev);
509 return host;
511 EXPORT_SYMBOL(memstick_alloc_host);
514 * memstick_add_host - start request processing on memstick host
515 * @host - host to use
517 int memstick_add_host(struct memstick_host *host)
519 int rc;
521 if (!idr_pre_get(&memstick_host_idr, GFP_KERNEL))
522 return -ENOMEM;
524 spin_lock(&memstick_host_lock);
525 rc = idr_get_new(&memstick_host_idr, host, &host->id);
526 spin_unlock(&memstick_host_lock);
527 if (rc)
528 return rc;
530 snprintf(host->cdev.class_id, BUS_ID_SIZE,
531 "memstick%u", host->id);
533 rc = class_device_add(&host->cdev);
534 if (rc) {
535 spin_lock(&memstick_host_lock);
536 idr_remove(&memstick_host_idr, host->id);
537 spin_unlock(&memstick_host_lock);
538 return rc;
541 host->set_param(host, MEMSTICK_POWER, MEMSTICK_POWER_OFF);
542 memstick_detect_change(host);
543 return 0;
545 EXPORT_SYMBOL(memstick_add_host);
548 * memstick_remove_host - stop request processing on memstick host
549 * @host - host to use
551 void memstick_remove_host(struct memstick_host *host)
553 flush_workqueue(workqueue);
554 mutex_lock(&host->lock);
555 if (host->card)
556 device_unregister(&host->card->dev);
557 host->card = NULL;
558 host->set_param(host, MEMSTICK_POWER, MEMSTICK_POWER_OFF);
559 mutex_unlock(&host->lock);
561 spin_lock(&memstick_host_lock);
562 idr_remove(&memstick_host_idr, host->id);
563 spin_unlock(&memstick_host_lock);
564 class_device_del(&host->cdev);
566 EXPORT_SYMBOL(memstick_remove_host);
569 * memstick_free_host - free memstick host
570 * @host - host to use
572 void memstick_free_host(struct memstick_host *host)
574 mutex_destroy(&host->lock);
575 class_device_put(&host->cdev);
577 EXPORT_SYMBOL(memstick_free_host);
579 <<<<<<< HEAD:drivers/memstick/core/memstick.c
580 =======
582 * memstick_suspend_host - notify bus driver of host suspension
583 * @host - host to use
585 void memstick_suspend_host(struct memstick_host *host)
587 mutex_lock(&host->lock);
588 host->set_param(host, MEMSTICK_POWER, MEMSTICK_POWER_OFF);
589 mutex_unlock(&host->lock);
591 EXPORT_SYMBOL(memstick_suspend_host);
594 * memstick_resume_host - notify bus driver of host resumption
595 * @host - host to use
597 void memstick_resume_host(struct memstick_host *host)
599 mutex_lock(&host->lock);
600 host->set_param(host, MEMSTICK_POWER, MEMSTICK_POWER_ON);
601 mutex_unlock(&host->lock);
602 memstick_detect_change(host);
604 EXPORT_SYMBOL(memstick_resume_host);
606 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/memstick/core/memstick.c
607 int memstick_register_driver(struct memstick_driver *drv)
609 drv->driver.bus = &memstick_bus_type;
611 return driver_register(&drv->driver);
613 EXPORT_SYMBOL(memstick_register_driver);
615 void memstick_unregister_driver(struct memstick_driver *drv)
617 driver_unregister(&drv->driver);
619 EXPORT_SYMBOL(memstick_unregister_driver);
622 static int __init memstick_init(void)
624 int rc;
626 workqueue = create_freezeable_workqueue("kmemstick");
627 if (!workqueue)
628 return -ENOMEM;
630 rc = bus_register(&memstick_bus_type);
631 if (!rc)
632 rc = class_register(&memstick_host_class);
634 if (!rc)
635 return 0;
637 bus_unregister(&memstick_bus_type);
638 destroy_workqueue(workqueue);
640 return rc;
643 static void __exit memstick_exit(void)
645 class_unregister(&memstick_host_class);
646 bus_unregister(&memstick_bus_type);
647 destroy_workqueue(workqueue);
648 idr_destroy(&memstick_host_idr);
651 module_init(memstick_init);
652 module_exit(memstick_exit);
654 MODULE_AUTHOR("Alex Dubov");
655 MODULE_LICENSE("GPL");
656 MODULE_DESCRIPTION("Sony MemoryStick core driver");
657 <<<<<<< HEAD:drivers/memstick/core/memstick.c
658 MODULE_VERSION(DRIVER_VERSION);
659 =======
660 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/memstick/core/memstick.c