x86, pat: Fix set_memory_wc related corruption
[linux-2.6/mini2440.git] / drivers / memstick / core / memstick.c
bloba5b448ea4eab54372a2263688cf04d2243775356
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"
22 static unsigned int cmd_retries = 3;
23 module_param(cmd_retries, uint, 0644);
25 static struct workqueue_struct *workqueue;
26 static DEFINE_IDR(memstick_host_idr);
27 static DEFINE_SPINLOCK(memstick_host_lock);
29 static int memstick_dev_match(struct memstick_dev *card,
30 struct memstick_device_id *id)
32 if (id->match_flags & MEMSTICK_MATCH_ALL) {
33 if ((id->type == card->id.type)
34 && (id->category == card->id.category)
35 && (id->class == card->id.class))
36 return 1;
39 return 0;
42 static int memstick_bus_match(struct device *dev, struct device_driver *drv)
44 struct memstick_dev *card = container_of(dev, struct memstick_dev,
45 dev);
46 struct memstick_driver *ms_drv = container_of(drv,
47 struct memstick_driver,
48 driver);
49 struct memstick_device_id *ids = ms_drv->id_table;
51 if (ids) {
52 while (ids->match_flags) {
53 if (memstick_dev_match(card, ids))
54 return 1;
55 ++ids;
58 return 0;
61 static int memstick_uevent(struct device *dev, struct kobj_uevent_env *env)
63 struct memstick_dev *card = container_of(dev, struct memstick_dev,
64 dev);
66 if (add_uevent_var(env, "MEMSTICK_TYPE=%02X", card->id.type))
67 return -ENOMEM;
69 if (add_uevent_var(env, "MEMSTICK_CATEGORY=%02X", card->id.category))
70 return -ENOMEM;
72 if (add_uevent_var(env, "MEMSTICK_CLASS=%02X", card->id.class))
73 return -ENOMEM;
75 return 0;
78 static int memstick_device_probe(struct device *dev)
80 struct memstick_dev *card = container_of(dev, struct memstick_dev,
81 dev);
82 struct memstick_driver *drv = container_of(dev->driver,
83 struct memstick_driver,
84 driver);
85 int rc = -ENODEV;
87 if (dev->driver && drv->probe) {
88 rc = drv->probe(card);
89 if (!rc)
90 get_device(dev);
92 return rc;
95 static int memstick_device_remove(struct device *dev)
97 struct memstick_dev *card = container_of(dev, struct memstick_dev,
98 dev);
99 struct memstick_driver *drv = container_of(dev->driver,
100 struct memstick_driver,
101 driver);
103 if (dev->driver && drv->remove) {
104 drv->remove(card);
105 card->dev.driver = NULL;
108 put_device(dev);
109 return 0;
112 #ifdef CONFIG_PM
114 static int memstick_device_suspend(struct device *dev, pm_message_t state)
116 struct memstick_dev *card = container_of(dev, struct memstick_dev,
117 dev);
118 struct memstick_driver *drv = container_of(dev->driver,
119 struct memstick_driver,
120 driver);
122 if (dev->driver && drv->suspend)
123 return drv->suspend(card, state);
124 return 0;
127 static int memstick_device_resume(struct device *dev)
129 struct memstick_dev *card = container_of(dev, struct memstick_dev,
130 dev);
131 struct memstick_driver *drv = container_of(dev->driver,
132 struct memstick_driver,
133 driver);
135 if (dev->driver && drv->resume)
136 return drv->resume(card);
137 return 0;
140 #else
142 #define memstick_device_suspend NULL
143 #define memstick_device_resume NULL
145 #endif /* CONFIG_PM */
147 #define MEMSTICK_ATTR(name, format) \
148 static ssize_t name##_show(struct device *dev, struct device_attribute *attr, \
149 char *buf) \
151 struct memstick_dev *card = container_of(dev, struct memstick_dev, \
152 dev); \
153 return sprintf(buf, format, card->id.name); \
156 MEMSTICK_ATTR(type, "%02X");
157 MEMSTICK_ATTR(category, "%02X");
158 MEMSTICK_ATTR(class, "%02X");
160 #define MEMSTICK_ATTR_RO(name) __ATTR(name, S_IRUGO, name##_show, NULL)
162 static struct device_attribute memstick_dev_attrs[] = {
163 MEMSTICK_ATTR_RO(type),
164 MEMSTICK_ATTR_RO(category),
165 MEMSTICK_ATTR_RO(class),
166 __ATTR_NULL
169 static struct bus_type memstick_bus_type = {
170 .name = "memstick",
171 .dev_attrs = memstick_dev_attrs,
172 .match = memstick_bus_match,
173 .uevent = memstick_uevent,
174 .probe = memstick_device_probe,
175 .remove = memstick_device_remove,
176 .suspend = memstick_device_suspend,
177 .resume = memstick_device_resume
180 static void memstick_free(struct device *dev)
182 struct memstick_host *host = container_of(dev, struct memstick_host,
183 dev);
184 kfree(host);
187 static struct class memstick_host_class = {
188 .name = "memstick_host",
189 .dev_release = memstick_free
192 static void memstick_free_card(struct device *dev)
194 struct memstick_dev *card = container_of(dev, struct memstick_dev,
195 dev);
196 kfree(card);
199 static int memstick_dummy_check(struct memstick_dev *card)
201 return 0;
205 * memstick_detect_change - schedule media detection on memstick host
206 * @host - host to use
208 void memstick_detect_change(struct memstick_host *host)
210 queue_work(workqueue, &host->media_checker);
212 EXPORT_SYMBOL(memstick_detect_change);
215 * memstick_next_req - called by host driver to obtain next request to process
216 * @host - host to use
217 * @mrq - pointer to stick the request to
219 * Host calls this function from idle state (*mrq == NULL) or after finishing
220 * previous request (*mrq should point to it). If previous request was
221 * unsuccessful, it is retried for predetermined number of times. Return value
222 * of 0 means that new request was assigned to the host.
224 int memstick_next_req(struct memstick_host *host, struct memstick_request **mrq)
226 int rc = -ENXIO;
228 if ((*mrq) && (*mrq)->error && host->retries) {
229 (*mrq)->error = rc;
230 host->retries--;
231 return 0;
234 if (host->card && host->card->next_request)
235 rc = host->card->next_request(host->card, mrq);
237 if (!rc)
238 host->retries = cmd_retries > 1 ? cmd_retries - 1 : 1;
239 else
240 *mrq = NULL;
242 return rc;
244 EXPORT_SYMBOL(memstick_next_req);
247 * memstick_new_req - notify the host that some requests are pending
248 * @host - host to use
250 void memstick_new_req(struct memstick_host *host)
252 if (host->card) {
253 host->retries = cmd_retries;
254 INIT_COMPLETION(host->card->mrq_complete);
255 host->request(host);
258 EXPORT_SYMBOL(memstick_new_req);
261 * memstick_init_req_sg - set request fields needed for bulk data transfer
262 * @mrq - request to use
263 * @tpc - memstick Transport Protocol Command
264 * @sg - TPC argument
266 void memstick_init_req_sg(struct memstick_request *mrq, unsigned char tpc,
267 const struct scatterlist *sg)
269 mrq->tpc = tpc;
270 if (tpc & 8)
271 mrq->data_dir = WRITE;
272 else
273 mrq->data_dir = READ;
275 mrq->sg = *sg;
276 mrq->long_data = 1;
278 if (tpc == MS_TPC_SET_CMD || tpc == MS_TPC_EX_SET_CMD)
279 mrq->need_card_int = 1;
280 else
281 mrq->need_card_int = 0;
283 EXPORT_SYMBOL(memstick_init_req_sg);
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
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.
296 void memstick_init_req(struct memstick_request *mrq, unsigned char tpc,
297 const void *buf, size_t length)
299 mrq->tpc = tpc;
300 if (tpc & 8)
301 mrq->data_dir = WRITE;
302 else
303 mrq->data_dir = READ;
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);
309 mrq->long_data = 0;
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;
316 EXPORT_SYMBOL(memstick_init_req);
319 * Functions prefixed with "h_" are protocol callbacks. They can be called from
320 * interrupt context. Return value of 0 means that request processing is still
321 * ongoing, while special error value of -EAGAIN means that current request is
322 * finished (and request processor should come back some time later).
325 static int h_memstick_read_dev_id(struct memstick_dev *card,
326 struct memstick_request **mrq)
328 struct ms_id_register id_reg;
330 if (!(*mrq)) {
331 memstick_init_req(&card->current_mrq, MS_TPC_READ_REG, NULL,
332 sizeof(struct ms_id_register));
333 *mrq = &card->current_mrq;
334 return 0;
335 } else {
336 if (!(*mrq)->error) {
337 memcpy(&id_reg, (*mrq)->data, sizeof(id_reg));
338 card->id.match_flags = MEMSTICK_MATCH_ALL;
339 card->id.type = id_reg.type;
340 card->id.category = id_reg.category;
341 card->id.class = id_reg.class;
343 complete(&card->mrq_complete);
344 dev_dbg(&card->dev, "if_mode = %02x\n", id_reg.if_mode);
345 return -EAGAIN;
349 static int h_memstick_set_rw_addr(struct memstick_dev *card,
350 struct memstick_request **mrq)
352 if (!(*mrq)) {
353 memstick_init_req(&card->current_mrq, MS_TPC_SET_RW_REG_ADRS,
354 (char *)&card->reg_addr,
355 sizeof(card->reg_addr));
356 *mrq = &card->current_mrq;
357 return 0;
358 } else {
359 complete(&card->mrq_complete);
360 return -EAGAIN;
365 * memstick_set_rw_addr - issue SET_RW_REG_ADDR request and wait for it to
366 * complete
367 * @card - media device to use
369 int memstick_set_rw_addr(struct memstick_dev *card)
371 card->next_request = h_memstick_set_rw_addr;
372 memstick_new_req(card->host);
373 wait_for_completion(&card->mrq_complete);
375 return card->current_mrq.error;
377 EXPORT_SYMBOL(memstick_set_rw_addr);
379 static struct memstick_dev *memstick_alloc_card(struct memstick_host *host)
381 struct memstick_dev *card = kzalloc(sizeof(struct memstick_dev),
382 GFP_KERNEL);
383 struct memstick_dev *old_card = host->card;
384 struct ms_id_register id_reg;
386 if (card) {
387 card->host = host;
388 dev_set_name(&card->dev, "%s", dev_name(&host->dev));
389 card->dev.parent = &host->dev;
390 card->dev.bus = &memstick_bus_type;
391 card->dev.release = memstick_free_card;
392 card->check = memstick_dummy_check;
394 card->reg_addr.r_offset = offsetof(struct ms_register, id);
395 card->reg_addr.r_length = sizeof(id_reg);
396 card->reg_addr.w_offset = offsetof(struct ms_register, id);
397 card->reg_addr.w_length = sizeof(id_reg);
399 init_completion(&card->mrq_complete);
401 host->card = card;
402 if (memstick_set_rw_addr(card))
403 goto err_out;
405 card->next_request = h_memstick_read_dev_id;
406 memstick_new_req(host);
407 wait_for_completion(&card->mrq_complete);
409 if (card->current_mrq.error)
410 goto err_out;
412 host->card = old_card;
413 return card;
414 err_out:
415 host->card = old_card;
416 kfree(card);
417 return NULL;
420 static int memstick_power_on(struct memstick_host *host)
422 int rc = host->set_param(host, MEMSTICK_POWER, MEMSTICK_POWER_ON);
424 if (!rc)
425 rc = host->set_param(host, MEMSTICK_INTERFACE, MEMSTICK_SERIAL);
427 return rc;
430 static void memstick_check(struct work_struct *work)
432 struct memstick_host *host = container_of(work, struct memstick_host,
433 media_checker);
434 struct memstick_dev *card;
436 dev_dbg(&host->dev, "memstick_check started\n");
437 mutex_lock(&host->lock);
438 if (!host->card) {
439 if (memstick_power_on(host))
440 goto out_power_off;
441 } else if (host->card->stop)
442 host->card->stop(host->card);
444 card = memstick_alloc_card(host);
446 if (!card) {
447 if (host->card) {
448 device_unregister(&host->card->dev);
449 host->card = NULL;
451 } else {
452 dev_dbg(&host->dev, "new card %02x, %02x, %02x\n",
453 card->id.type, card->id.category, card->id.class);
454 if (host->card) {
455 if (memstick_set_rw_addr(host->card)
456 || !memstick_dev_match(host->card, &card->id)
457 || !(host->card->check(host->card))) {
458 device_unregister(&host->card->dev);
459 host->card = NULL;
460 } else if (host->card->start)
461 host->card->start(host->card);
464 if (!host->card) {
465 host->card = card;
466 if (device_register(&card->dev)) {
467 kfree(host->card);
468 host->card = NULL;
470 } else
471 kfree(card);
474 out_power_off:
475 if (!host->card)
476 host->set_param(host, MEMSTICK_POWER, MEMSTICK_POWER_OFF);
478 mutex_unlock(&host->lock);
479 dev_dbg(&host->dev, "memstick_check finished\n");
483 * memstick_alloc_host - allocate a memstick_host structure
484 * @extra: size of the user private data to allocate
485 * @dev: parent device of the host
487 struct memstick_host *memstick_alloc_host(unsigned int extra,
488 struct device *dev)
490 struct memstick_host *host;
492 host = kzalloc(sizeof(struct memstick_host) + extra, GFP_KERNEL);
493 if (host) {
494 mutex_init(&host->lock);
495 INIT_WORK(&host->media_checker, memstick_check);
496 host->dev.class = &memstick_host_class;
497 host->dev.parent = dev;
498 device_initialize(&host->dev);
500 return host;
502 EXPORT_SYMBOL(memstick_alloc_host);
505 * memstick_add_host - start request processing on memstick host
506 * @host - host to use
508 int memstick_add_host(struct memstick_host *host)
510 int rc;
512 if (!idr_pre_get(&memstick_host_idr, GFP_KERNEL))
513 return -ENOMEM;
515 spin_lock(&memstick_host_lock);
516 rc = idr_get_new(&memstick_host_idr, host, &host->id);
517 spin_unlock(&memstick_host_lock);
518 if (rc)
519 return rc;
521 dev_set_name(&host->dev, "memstick%u", host->id);
523 rc = device_add(&host->dev);
524 if (rc) {
525 spin_lock(&memstick_host_lock);
526 idr_remove(&memstick_host_idr, host->id);
527 spin_unlock(&memstick_host_lock);
528 return rc;
531 host->set_param(host, MEMSTICK_POWER, MEMSTICK_POWER_OFF);
532 memstick_detect_change(host);
533 return 0;
535 EXPORT_SYMBOL(memstick_add_host);
538 * memstick_remove_host - stop request processing on memstick host
539 * @host - host to use
541 void memstick_remove_host(struct memstick_host *host)
543 flush_workqueue(workqueue);
544 mutex_lock(&host->lock);
545 if (host->card)
546 device_unregister(&host->card->dev);
547 host->card = NULL;
548 host->set_param(host, MEMSTICK_POWER, MEMSTICK_POWER_OFF);
549 mutex_unlock(&host->lock);
551 spin_lock(&memstick_host_lock);
552 idr_remove(&memstick_host_idr, host->id);
553 spin_unlock(&memstick_host_lock);
554 device_del(&host->dev);
556 EXPORT_SYMBOL(memstick_remove_host);
559 * memstick_free_host - free memstick host
560 * @host - host to use
562 void memstick_free_host(struct memstick_host *host)
564 mutex_destroy(&host->lock);
565 put_device(&host->dev);
567 EXPORT_SYMBOL(memstick_free_host);
570 * memstick_suspend_host - notify bus driver of host suspension
571 * @host - host to use
573 void memstick_suspend_host(struct memstick_host *host)
575 mutex_lock(&host->lock);
576 host->set_param(host, MEMSTICK_POWER, MEMSTICK_POWER_OFF);
577 mutex_unlock(&host->lock);
579 EXPORT_SYMBOL(memstick_suspend_host);
582 * memstick_resume_host - notify bus driver of host resumption
583 * @host - host to use
585 void memstick_resume_host(struct memstick_host *host)
587 int rc = 0;
589 mutex_lock(&host->lock);
590 if (host->card)
591 rc = memstick_power_on(host);
592 mutex_unlock(&host->lock);
594 if (!rc)
595 memstick_detect_change(host);
597 EXPORT_SYMBOL(memstick_resume_host);
599 int memstick_register_driver(struct memstick_driver *drv)
601 drv->driver.bus = &memstick_bus_type;
603 return driver_register(&drv->driver);
605 EXPORT_SYMBOL(memstick_register_driver);
607 void memstick_unregister_driver(struct memstick_driver *drv)
609 driver_unregister(&drv->driver);
611 EXPORT_SYMBOL(memstick_unregister_driver);
614 static int __init memstick_init(void)
616 int rc;
618 workqueue = create_freezeable_workqueue("kmemstick");
619 if (!workqueue)
620 return -ENOMEM;
622 rc = bus_register(&memstick_bus_type);
623 if (!rc)
624 rc = class_register(&memstick_host_class);
626 if (!rc)
627 return 0;
629 bus_unregister(&memstick_bus_type);
630 destroy_workqueue(workqueue);
632 return rc;
635 static void __exit memstick_exit(void)
637 class_unregister(&memstick_host_class);
638 bus_unregister(&memstick_bus_type);
639 destroy_workqueue(workqueue);
640 idr_destroy(&memstick_host_idr);
643 module_init(memstick_init);
644 module_exit(memstick_exit);
646 MODULE_AUTHOR("Alex Dubov");
647 MODULE_LICENSE("GPL");
648 MODULE_DESCRIPTION("Sony MemoryStick core driver");