blob: 7162f772bbfb92d1124042e666faa9d33609c274 [file] [log] [blame]
Alex Dubovbaf85322008-02-09 10:20:54 -08001/*
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"
Alex Dubovbaf85322008-02-09 10:20:54 -080021
22static unsigned int cmd_retries = 3;
23module_param(cmd_retries, uint, 0644);
24
25static struct workqueue_struct *workqueue;
26static DEFINE_IDR(memstick_host_idr);
27static DEFINE_SPINLOCK(memstick_host_lock);
28
29static int memstick_dev_match(struct memstick_dev *card,
30 struct memstick_device_id *id)
31{
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;
37 }
38
39 return 0;
40}
41
42static int memstick_bus_match(struct device *dev, struct device_driver *drv)
43{
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;
50
51 if (ids) {
52 while (ids->match_flags) {
53 if (memstick_dev_match(card, ids))
54 return 1;
55 ++ids;
56 }
57 }
58 return 0;
59}
60
61static int memstick_uevent(struct device *dev, struct kobj_uevent_env *env)
62{
63 struct memstick_dev *card = container_of(dev, struct memstick_dev,
64 dev);
65
66 if (add_uevent_var(env, "MEMSTICK_TYPE=%02X", card->id.type))
67 return -ENOMEM;
68
69 if (add_uevent_var(env, "MEMSTICK_CATEGORY=%02X", card->id.category))
70 return -ENOMEM;
71
72 if (add_uevent_var(env, "MEMSTICK_CLASS=%02X", card->id.class))
73 return -ENOMEM;
74
75 return 0;
76}
77
78static int memstick_device_probe(struct device *dev)
79{
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;
86
87 if (dev->driver && drv->probe) {
88 rc = drv->probe(card);
89 if (!rc)
90 get_device(dev);
91 }
92 return rc;
93}
94
95static int memstick_device_remove(struct device *dev)
96{
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);
102
103 if (dev->driver && drv->remove) {
104 drv->remove(card);
105 card->dev.driver = NULL;
106 }
107
108 put_device(dev);
109 return 0;
110}
111
112#ifdef CONFIG_PM
113
114static int memstick_device_suspend(struct device *dev, pm_message_t state)
115{
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);
121
122 if (dev->driver && drv->suspend)
123 return drv->suspend(card, state);
124 return 0;
125}
126
127static int memstick_device_resume(struct device *dev)
128{
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);
134
135 if (dev->driver && drv->resume)
136 return drv->resume(card);
137 return 0;
138}
139
140#else
141
142#define memstick_device_suspend NULL
143#define memstick_device_resume NULL
144
145#endif /* CONFIG_PM */
146
147#define MEMSTICK_ATTR(name, format) \
148static ssize_t name##_show(struct device *dev, struct device_attribute *attr, \
149 char *buf) \
150{ \
151 struct memstick_dev *card = container_of(dev, struct memstick_dev, \
152 dev); \
153 return sprintf(buf, format, card->id.name); \
154}
155
156MEMSTICK_ATTR(type, "%02X");
157MEMSTICK_ATTR(category, "%02X");
158MEMSTICK_ATTR(class, "%02X");
159
160#define MEMSTICK_ATTR_RO(name) __ATTR(name, S_IRUGO, name##_show, NULL)
161
162static struct device_attribute memstick_dev_attrs[] = {
163 MEMSTICK_ATTR_RO(type),
164 MEMSTICK_ATTR_RO(category),
165 MEMSTICK_ATTR_RO(class),
166 __ATTR_NULL
167};
168
169static 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
178};
179
Greg Kroah-Hartmanc4c66cf2008-03-04 00:13:36 +0100180static void memstick_free(struct device *dev)
Alex Dubovbaf85322008-02-09 10:20:54 -0800181{
Greg Kroah-Hartmanc4c66cf2008-03-04 00:13:36 +0100182 struct memstick_host *host = container_of(dev, struct memstick_host,
183 dev);
Alex Dubovbaf85322008-02-09 10:20:54 -0800184 kfree(host);
185}
186
187static struct class memstick_host_class = {
188 .name = "memstick_host",
Greg Kroah-Hartmanc4c66cf2008-03-04 00:13:36 +0100189 .dev_release = memstick_free
Alex Dubovbaf85322008-02-09 10:20:54 -0800190};
191
192static void memstick_free_card(struct device *dev)
193{
194 struct memstick_dev *card = container_of(dev, struct memstick_dev,
195 dev);
196 kfree(card);
197}
198
199static int memstick_dummy_check(struct memstick_dev *card)
200{
201 return 0;
202}
203
204/**
205 * memstick_detect_change - schedule media detection on memstick host
206 * @host - host to use
207 */
208void memstick_detect_change(struct memstick_host *host)
209{
210 queue_work(workqueue, &host->media_checker);
211}
212EXPORT_SYMBOL(memstick_detect_change);
213
214/**
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
218 *
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.
223 */
224int memstick_next_req(struct memstick_host *host, struct memstick_request **mrq)
225{
226 int rc = -ENXIO;
227
228 if ((*mrq) && (*mrq)->error && host->retries) {
229 (*mrq)->error = rc;
230 host->retries--;
231 return 0;
232 }
233
234 if (host->card && host->card->next_request)
235 rc = host->card->next_request(host->card, mrq);
236
237 if (!rc)
Alex Dubov29196dc2008-03-10 11:43:38 -0700238 host->retries = cmd_retries > 1 ? cmd_retries - 1 : 1;
Alex Dubovbaf85322008-02-09 10:20:54 -0800239 else
240 *mrq = NULL;
241
242 return rc;
243}
244EXPORT_SYMBOL(memstick_next_req);
245
246/**
247 * memstick_new_req - notify the host that some requests are pending
248 * @host - host to use
249 */
250void memstick_new_req(struct memstick_host *host)
251{
252 host->retries = cmd_retries;
253 host->request(host);
254}
255EXPORT_SYMBOL(memstick_new_req);
256
257/**
258 * memstick_init_req_sg - set request fields needed for bulk data transfer
259 * @mrq - request to use
260 * @tpc - memstick Transport Protocol Command
261 * @sg - TPC argument
262 */
263void memstick_init_req_sg(struct memstick_request *mrq, unsigned char tpc,
264 struct scatterlist *sg)
265{
266 mrq->tpc = tpc;
267 if (tpc & 8)
268 mrq->data_dir = WRITE;
269 else
270 mrq->data_dir = READ;
271
272 mrq->sg = *sg;
Alex Dubove1f19992008-03-10 11:43:37 -0700273 mrq->long_data = 1;
Alex Dubovbaf85322008-02-09 10:20:54 -0800274
275 if (tpc == MS_TPC_SET_CMD || tpc == MS_TPC_EX_SET_CMD)
276 mrq->need_card_int = 1;
277 else
278 mrq->need_card_int = 0;
Alex Dubovbaf85322008-02-09 10:20:54 -0800279}
280EXPORT_SYMBOL(memstick_init_req_sg);
281
282/**
283 * memstick_init_req - set request fields needed for short data transfer
284 * @mrq - request to use
285 * @tpc - memstick Transport Protocol Command
286 * @buf - TPC argument buffer
287 * @length - TPC argument size
288 *
289 * The intended use of this function (transfer of data items several bytes
290 * in size) allows us to just copy the value between request structure and
291 * user supplied buffer.
292 */
293void memstick_init_req(struct memstick_request *mrq, unsigned char tpc,
294 void *buf, size_t length)
295{
296 mrq->tpc = tpc;
297 if (tpc & 8)
298 mrq->data_dir = WRITE;
299 else
300 mrq->data_dir = READ;
301
302 mrq->data_len = length > sizeof(mrq->data) ? sizeof(mrq->data) : length;
303 if (mrq->data_dir == WRITE)
304 memcpy(mrq->data, buf, mrq->data_len);
305
Alex Dubove1f19992008-03-10 11:43:37 -0700306 mrq->long_data = 0;
Alex Dubovbaf85322008-02-09 10:20:54 -0800307
308 if (tpc == MS_TPC_SET_CMD || tpc == MS_TPC_EX_SET_CMD)
309 mrq->need_card_int = 1;
310 else
311 mrq->need_card_int = 0;
Alex Dubovbaf85322008-02-09 10:20:54 -0800312}
313EXPORT_SYMBOL(memstick_init_req);
314
315/*
316 * Functions prefixed with "h_" are protocol callbacks. They can be called from
317 * interrupt context. Return value of 0 means that request processing is still
318 * ongoing, while special error value of -EAGAIN means that current request is
319 * finished (and request processor should come back some time later).
320 */
321
322static int h_memstick_read_dev_id(struct memstick_dev *card,
323 struct memstick_request **mrq)
324{
325 struct ms_id_register id_reg;
326
327 if (!(*mrq)) {
328 memstick_init_req(&card->current_mrq, MS_TPC_READ_REG, NULL,
329 sizeof(struct ms_id_register));
330 *mrq = &card->current_mrq;
331 return 0;
332 } else {
333 if (!(*mrq)->error) {
334 memcpy(&id_reg, (*mrq)->data, sizeof(id_reg));
335 card->id.match_flags = MEMSTICK_MATCH_ALL;
336 card->id.type = id_reg.type;
337 card->id.category = id_reg.category;
338 card->id.class = id_reg.class;
339 }
340 complete(&card->mrq_complete);
Alex Dubovead70772008-03-19 17:01:06 -0700341 dev_dbg(&card->dev, "if_mode = %02x\n", id_reg.if_mode);
Alex Dubovbaf85322008-02-09 10:20:54 -0800342 return -EAGAIN;
343 }
344}
345
346static int h_memstick_set_rw_addr(struct memstick_dev *card,
347 struct memstick_request **mrq)
348{
349 if (!(*mrq)) {
350 memstick_init_req(&card->current_mrq, MS_TPC_SET_RW_REG_ADRS,
351 (char *)&card->reg_addr,
352 sizeof(card->reg_addr));
353 *mrq = &card->current_mrq;
354 return 0;
355 } else {
356 complete(&card->mrq_complete);
357 return -EAGAIN;
358 }
359}
360
361/**
362 * memstick_set_rw_addr - issue SET_RW_REG_ADDR request and wait for it to
363 * complete
364 * @card - media device to use
365 */
366int memstick_set_rw_addr(struct memstick_dev *card)
367{
368 card->next_request = h_memstick_set_rw_addr;
369 memstick_new_req(card->host);
370 wait_for_completion(&card->mrq_complete);
371
372 return card->current_mrq.error;
373}
374EXPORT_SYMBOL(memstick_set_rw_addr);
375
376static struct memstick_dev *memstick_alloc_card(struct memstick_host *host)
377{
378 struct memstick_dev *card = kzalloc(sizeof(struct memstick_dev),
379 GFP_KERNEL);
380 struct memstick_dev *old_card = host->card;
381 struct ms_id_register id_reg;
382
383 if (card) {
384 card->host = host;
385 snprintf(card->dev.bus_id, sizeof(card->dev.bus_id),
Greg Kroah-Hartmanc4c66cf2008-03-04 00:13:36 +0100386 "%s", host->dev.bus_id);
387 card->dev.parent = &host->dev;
Alex Dubovbaf85322008-02-09 10:20:54 -0800388 card->dev.bus = &memstick_bus_type;
389 card->dev.release = memstick_free_card;
390 card->check = memstick_dummy_check;
391
392 card->reg_addr.r_offset = offsetof(struct ms_register, id);
393 card->reg_addr.r_length = sizeof(id_reg);
394 card->reg_addr.w_offset = offsetof(struct ms_register, id);
395 card->reg_addr.w_length = sizeof(id_reg);
396
397 init_completion(&card->mrq_complete);
398
399 host->card = card;
400 if (memstick_set_rw_addr(card))
401 goto err_out;
402
403 card->next_request = h_memstick_read_dev_id;
404 memstick_new_req(host);
405 wait_for_completion(&card->mrq_complete);
406
407 if (card->current_mrq.error)
408 goto err_out;
409 }
410 host->card = old_card;
411 return card;
412err_out:
413 host->card = old_card;
414 kfree(card);
415 return NULL;
416}
417
Alex Dubovb7789992008-07-25 19:45:00 -0700418static int memstick_power_on(struct memstick_host *host)
Alex Dubovbaf85322008-02-09 10:20:54 -0800419{
Alex Dubovb7789992008-07-25 19:45:00 -0700420 int rc = host->set_param(host, MEMSTICK_POWER, MEMSTICK_POWER_ON);
421
422 if (!rc)
423 rc = host->set_param(host, MEMSTICK_INTERFACE, MEMSTICK_SERIAL);
424
425 return rc;
Alex Dubovbaf85322008-02-09 10:20:54 -0800426}
427
428static void memstick_check(struct work_struct *work)
429{
430 struct memstick_host *host = container_of(work, struct memstick_host,
431 media_checker);
432 struct memstick_dev *card;
433
Greg Kroah-Hartmanc4c66cf2008-03-04 00:13:36 +0100434 dev_dbg(&host->dev, "memstick_check started\n");
Alex Dubovbaf85322008-02-09 10:20:54 -0800435 mutex_lock(&host->lock);
Alex Dubov17017d82008-07-25 19:45:01 -0700436 if (!host->card) {
437 if (memstick_power_on(host))
438 goto out_power_off;
439 } else
440 host->card->stop(host->card);
Alex Dubovbaf85322008-02-09 10:20:54 -0800441
442 card = memstick_alloc_card(host);
443
444 if (!card) {
445 if (host->card) {
446 device_unregister(&host->card->dev);
447 host->card = NULL;
448 }
449 } else {
Greg Kroah-Hartmanc4c66cf2008-03-04 00:13:36 +0100450 dev_dbg(&host->dev, "new card %02x, %02x, %02x\n",
Alex Dubovbaf85322008-02-09 10:20:54 -0800451 card->id.type, card->id.category, card->id.class);
452 if (host->card) {
453 if (memstick_set_rw_addr(host->card)
454 || !memstick_dev_match(host->card, &card->id)
455 || !(host->card->check(host->card))) {
456 device_unregister(&host->card->dev);
457 host->card = NULL;
Alex Dubov17017d82008-07-25 19:45:01 -0700458 } else
459 host->card->start(host->card);
Alex Dubovbaf85322008-02-09 10:20:54 -0800460 }
461
462 if (!host->card) {
463 host->card = card;
464 if (device_register(&card->dev)) {
465 kfree(host->card);
466 host->card = NULL;
467 }
468 } else
469 kfree(card);
470 }
471
Alex Dubov17017d82008-07-25 19:45:01 -0700472out_power_off:
Alex Dubovbaf85322008-02-09 10:20:54 -0800473 if (!host->card)
474 host->set_param(host, MEMSTICK_POWER, MEMSTICK_POWER_OFF);
475
476 mutex_unlock(&host->lock);
Greg Kroah-Hartmanc4c66cf2008-03-04 00:13:36 +0100477 dev_dbg(&host->dev, "memstick_check finished\n");
Alex Dubovbaf85322008-02-09 10:20:54 -0800478}
479
480/**
481 * memstick_alloc_host - allocate a memstick_host structure
482 * @extra: size of the user private data to allocate
483 * @dev: parent device of the host
484 */
485struct memstick_host *memstick_alloc_host(unsigned int extra,
486 struct device *dev)
487{
488 struct memstick_host *host;
489
490 host = kzalloc(sizeof(struct memstick_host) + extra, GFP_KERNEL);
491 if (host) {
492 mutex_init(&host->lock);
493 INIT_WORK(&host->media_checker, memstick_check);
Greg Kroah-Hartmanc4c66cf2008-03-04 00:13:36 +0100494 host->dev.class = &memstick_host_class;
495 host->dev.parent = dev;
496 device_initialize(&host->dev);
Alex Dubovbaf85322008-02-09 10:20:54 -0800497 }
498 return host;
499}
500EXPORT_SYMBOL(memstick_alloc_host);
501
502/**
503 * memstick_add_host - start request processing on memstick host
504 * @host - host to use
505 */
506int memstick_add_host(struct memstick_host *host)
507{
508 int rc;
509
510 if (!idr_pre_get(&memstick_host_idr, GFP_KERNEL))
511 return -ENOMEM;
512
513 spin_lock(&memstick_host_lock);
514 rc = idr_get_new(&memstick_host_idr, host, &host->id);
515 spin_unlock(&memstick_host_lock);
516 if (rc)
517 return rc;
518
Greg Kroah-Hartmanc4c66cf2008-03-04 00:13:36 +0100519 snprintf(host->dev.bus_id, BUS_ID_SIZE, "memstick%u", host->id);
Alex Dubovbaf85322008-02-09 10:20:54 -0800520
Greg Kroah-Hartmanc4c66cf2008-03-04 00:13:36 +0100521 rc = device_add(&host->dev);
Alex Dubovbaf85322008-02-09 10:20:54 -0800522 if (rc) {
523 spin_lock(&memstick_host_lock);
524 idr_remove(&memstick_host_idr, host->id);
525 spin_unlock(&memstick_host_lock);
526 return rc;
527 }
528
529 host->set_param(host, MEMSTICK_POWER, MEMSTICK_POWER_OFF);
530 memstick_detect_change(host);
531 return 0;
532}
533EXPORT_SYMBOL(memstick_add_host);
534
535/**
536 * memstick_remove_host - stop request processing on memstick host
537 * @host - host to use
538 */
539void memstick_remove_host(struct memstick_host *host)
540{
541 flush_workqueue(workqueue);
542 mutex_lock(&host->lock);
543 if (host->card)
544 device_unregister(&host->card->dev);
545 host->card = NULL;
546 host->set_param(host, MEMSTICK_POWER, MEMSTICK_POWER_OFF);
547 mutex_unlock(&host->lock);
548
549 spin_lock(&memstick_host_lock);
550 idr_remove(&memstick_host_idr, host->id);
551 spin_unlock(&memstick_host_lock);
Greg Kroah-Hartmanc4c66cf2008-03-04 00:13:36 +0100552 device_del(&host->dev);
Alex Dubovbaf85322008-02-09 10:20:54 -0800553}
554EXPORT_SYMBOL(memstick_remove_host);
555
556/**
557 * memstick_free_host - free memstick host
558 * @host - host to use
559 */
560void memstick_free_host(struct memstick_host *host)
561{
562 mutex_destroy(&host->lock);
Greg Kroah-Hartmanc4c66cf2008-03-04 00:13:36 +0100563 put_device(&host->dev);
Alex Dubovbaf85322008-02-09 10:20:54 -0800564}
565EXPORT_SYMBOL(memstick_free_host);
566
Alex Dubovd114ad52008-03-10 11:43:38 -0700567/**
568 * memstick_suspend_host - notify bus driver of host suspension
569 * @host - host to use
570 */
571void memstick_suspend_host(struct memstick_host *host)
572{
573 mutex_lock(&host->lock);
574 host->set_param(host, MEMSTICK_POWER, MEMSTICK_POWER_OFF);
575 mutex_unlock(&host->lock);
576}
577EXPORT_SYMBOL(memstick_suspend_host);
578
579/**
580 * memstick_resume_host - notify bus driver of host resumption
581 * @host - host to use
582 */
583void memstick_resume_host(struct memstick_host *host)
584{
Alex Dubovb7789992008-07-25 19:45:00 -0700585 int rc = 0;
586
Alex Dubovd114ad52008-03-10 11:43:38 -0700587 mutex_lock(&host->lock);
Alex Dubovead70772008-03-19 17:01:06 -0700588 if (host->card)
Alex Dubovb7789992008-07-25 19:45:00 -0700589 rc = memstick_power_on(host);
Alex Dubovd114ad52008-03-10 11:43:38 -0700590 mutex_unlock(&host->lock);
Alex Dubovb7789992008-07-25 19:45:00 -0700591
592 if (!rc)
593 memstick_detect_change(host);
Alex Dubovd114ad52008-03-10 11:43:38 -0700594}
595EXPORT_SYMBOL(memstick_resume_host);
596
Alex Dubovbaf85322008-02-09 10:20:54 -0800597int memstick_register_driver(struct memstick_driver *drv)
598{
599 drv->driver.bus = &memstick_bus_type;
600
601 return driver_register(&drv->driver);
602}
603EXPORT_SYMBOL(memstick_register_driver);
604
605void memstick_unregister_driver(struct memstick_driver *drv)
606{
607 driver_unregister(&drv->driver);
608}
609EXPORT_SYMBOL(memstick_unregister_driver);
610
611
612static int __init memstick_init(void)
613{
614 int rc;
615
616 workqueue = create_freezeable_workqueue("kmemstick");
617 if (!workqueue)
618 return -ENOMEM;
619
620 rc = bus_register(&memstick_bus_type);
621 if (!rc)
622 rc = class_register(&memstick_host_class);
623
624 if (!rc)
625 return 0;
626
627 bus_unregister(&memstick_bus_type);
628 destroy_workqueue(workqueue);
629
630 return rc;
631}
632
633static void __exit memstick_exit(void)
634{
635 class_unregister(&memstick_host_class);
636 bus_unregister(&memstick_bus_type);
637 destroy_workqueue(workqueue);
638 idr_destroy(&memstick_host_idr);
639}
640
641module_init(memstick_init);
642module_exit(memstick_exit);
643
644MODULE_AUTHOR("Alex Dubov");
645MODULE_LICENSE("GPL");
646MODULE_DESCRIPTION("Sony MemoryStick core driver");