de80dba12f9baaddc5aa144434385384958544d2
[sfrench/cifs-2.6.git] / drivers / memstick / core / memstick.c
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
22 static unsigned int cmd_retries = 3;
23 module_param(cmd_retries, uint, 0644);
24
25 static struct workqueue_struct *workqueue;
26 static DEFINE_IDR(memstick_host_idr);
27 static DEFINE_SPINLOCK(memstick_host_lock);
28
29 static 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
42 static 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
61 static 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
78 static 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
95 static 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
114 static 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
127 static 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)                                           \
148 static 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
156 MEMSTICK_ATTR(type, "%02X");
157 MEMSTICK_ATTR(category, "%02X");
158 MEMSTICK_ATTR(class, "%02X");
159
160 #define MEMSTICK_ATTR_RO(name) __ATTR(name, S_IRUGO, name##_show, NULL)
161
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
167 };
168
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
178 };
179
180 static void memstick_free(struct class_device *cdev)
181 {
182         struct memstick_host *host = container_of(cdev, struct memstick_host,
183                                                   cdev);
184         kfree(host);
185 }
186
187 static struct class memstick_host_class = {
188         .name       = "memstick_host",
189         .release    = memstick_free
190 };
191
192 static 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
199 static 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  */
208 void memstick_detect_change(struct memstick_host *host)
209 {
210         queue_work(workqueue, &host->media_checker);
211 }
212 EXPORT_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  */
224 int 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)
238                 host->retries = cmd_retries > 1 ? cmd_retries - 1 : 1;
239         else
240                 *mrq = NULL;
241
242         return rc;
243 }
244 EXPORT_SYMBOL(memstick_next_req);
245
246 /**
247  * memstick_new_req - notify the host that some requests are pending
248  * @host - host to use
249  */
250 void memstick_new_req(struct memstick_host *host)
251 {
252         host->retries = cmd_retries;
253         host->request(host);
254 }
255 EXPORT_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  */
263 void 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;
273         mrq->long_data = 1;
274
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;
279
280         mrq->get_int_reg = 0;
281 }
282 EXPORT_SYMBOL(memstick_init_req_sg);
283
284 /**
285  * memstick_init_req - set request fields needed for short data transfer
286  * @mrq - request to use
287  * @tpc - memstick Transport Protocol Command
288  * @buf - TPC argument buffer
289  * @length - TPC argument size
290  *
291  * The intended use of this function (transfer of data items several bytes
292  * in size) allows us to just copy the value between request structure and
293  * user supplied buffer.
294  */
295 void memstick_init_req(struct memstick_request *mrq, unsigned char tpc,
296                        void *buf, size_t length)
297 {
298         mrq->tpc = tpc;
299         if (tpc & 8)
300                 mrq->data_dir = WRITE;
301         else
302                 mrq->data_dir = READ;
303
304         mrq->data_len = length > sizeof(mrq->data) ? sizeof(mrq->data) : length;
305         if (mrq->data_dir == WRITE)
306                 memcpy(mrq->data, buf, mrq->data_len);
307
308         mrq->long_data = 0;
309
310         if (tpc == MS_TPC_SET_CMD || tpc == MS_TPC_EX_SET_CMD)
311                 mrq->need_card_int = 1;
312         else
313                 mrq->need_card_int = 0;
314
315         mrq->get_int_reg = 0;
316 }
317 EXPORT_SYMBOL(memstick_init_req);
318
319 /*
320  * Functions prefixed with "h_" are protocol callbacks. They can be called from
321  * interrupt context. Return value of 0 means that request processing is still
322  * ongoing, while special error value of -EAGAIN means that current request is
323  * finished (and request processor should come back some time later).
324  */
325
326 static int h_memstick_read_dev_id(struct memstick_dev *card,
327                                   struct memstick_request **mrq)
328 {
329         struct ms_id_register id_reg;
330
331         if (!(*mrq)) {
332                 memstick_init_req(&card->current_mrq, MS_TPC_READ_REG, NULL,
333                                   sizeof(struct ms_id_register));
334                 *mrq = &card->current_mrq;
335                 return 0;
336         } else {
337                 if (!(*mrq)->error) {
338                         memcpy(&id_reg, (*mrq)->data, sizeof(id_reg));
339                         card->id.match_flags = MEMSTICK_MATCH_ALL;
340                         card->id.type = id_reg.type;
341                         card->id.category = id_reg.category;
342                         card->id.class = id_reg.class;
343                 }
344                 complete(&card->mrq_complete);
345                 return -EAGAIN;
346         }
347 }
348
349 static int h_memstick_set_rw_addr(struct memstick_dev *card,
350                                   struct memstick_request **mrq)
351 {
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;
361         }
362 }
363
364 /**
365  * memstick_set_rw_addr - issue SET_RW_REG_ADDR request and wait for it to
366  *                        complete
367  * @card - media device to use
368  */
369 int memstick_set_rw_addr(struct memstick_dev *card)
370 {
371         card->next_request = h_memstick_set_rw_addr;
372         memstick_new_req(card->host);
373         wait_for_completion(&card->mrq_complete);
374
375         return card->current_mrq.error;
376 }
377 EXPORT_SYMBOL(memstick_set_rw_addr);
378
379 static struct memstick_dev *memstick_alloc_card(struct memstick_host *host)
380 {
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;
385
386         if (card) {
387                 card->host = host;
388                 snprintf(card->dev.bus_id, sizeof(card->dev.bus_id),
389                          "%s", host->cdev.class_id);
390                 card->dev.parent = host->cdev.dev;
391                 card->dev.bus = &memstick_bus_type;
392                 card->dev.release = memstick_free_card;
393                 card->check = memstick_dummy_check;
394
395                 card->reg_addr.r_offset = offsetof(struct ms_register, id);
396                 card->reg_addr.r_length = sizeof(id_reg);
397                 card->reg_addr.w_offset = offsetof(struct ms_register, id);
398                 card->reg_addr.w_length = sizeof(id_reg);
399
400                 init_completion(&card->mrq_complete);
401
402                 host->card = card;
403                 if (memstick_set_rw_addr(card))
404                         goto err_out;
405
406                 card->next_request = h_memstick_read_dev_id;
407                 memstick_new_req(host);
408                 wait_for_completion(&card->mrq_complete);
409
410                 if (card->current_mrq.error)
411                         goto err_out;
412         }
413         host->card = old_card;
414         return card;
415 err_out:
416         host->card = old_card;
417         kfree(card);
418         return NULL;
419 }
420
421 static void memstick_power_on(struct memstick_host *host)
422 {
423         host->set_param(host, MEMSTICK_POWER, MEMSTICK_POWER_ON);
424         host->set_param(host, MEMSTICK_INTERFACE, MEMSTICK_SERIAL);
425         msleep(1);
426 }
427
428 static 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
434         dev_dbg(host->cdev.dev, "memstick_check started\n");
435         mutex_lock(&host->lock);
436         if (!host->card)
437                 memstick_power_on(host);
438
439         card = memstick_alloc_card(host);
440
441         if (!card) {
442                 if (host->card) {
443                         device_unregister(&host->card->dev);
444                         host->card = NULL;
445                 }
446         } else {
447                 dev_dbg(host->cdev.dev, "new card %02x, %02x, %02x\n",
448                         card->id.type, card->id.category, card->id.class);
449                 if (host->card) {
450                         if (memstick_set_rw_addr(host->card)
451                             || !memstick_dev_match(host->card, &card->id)
452                             || !(host->card->check(host->card))) {
453                                 device_unregister(&host->card->dev);
454                                 host->card = NULL;
455                         }
456                 }
457
458                 if (!host->card) {
459                         host->card = card;
460                         if (device_register(&card->dev)) {
461                                 kfree(host->card);
462                                 host->card = NULL;
463                         }
464                 } else
465                         kfree(card);
466         }
467
468         if (!host->card)
469                 host->set_param(host, MEMSTICK_POWER, MEMSTICK_POWER_OFF);
470
471         mutex_unlock(&host->lock);
472         dev_dbg(host->cdev.dev, "memstick_check finished\n");
473 }
474
475 /**
476  * memstick_alloc_host - allocate a memstick_host structure
477  * @extra: size of the user private data to allocate
478  * @dev: parent device of the host
479  */
480 struct memstick_host *memstick_alloc_host(unsigned int extra,
481                                           struct device *dev)
482 {
483         struct memstick_host *host;
484
485         host = kzalloc(sizeof(struct memstick_host) + extra, GFP_KERNEL);
486         if (host) {
487                 mutex_init(&host->lock);
488                 INIT_WORK(&host->media_checker, memstick_check);
489                 host->cdev.class = &memstick_host_class;
490                 host->cdev.dev = dev;
491                 class_device_initialize(&host->cdev);
492         }
493         return host;
494 }
495 EXPORT_SYMBOL(memstick_alloc_host);
496
497 /**
498  * memstick_add_host - start request processing on memstick host
499  * @host - host to use
500  */
501 int memstick_add_host(struct memstick_host *host)
502 {
503         int rc;
504
505         if (!idr_pre_get(&memstick_host_idr, GFP_KERNEL))
506                 return -ENOMEM;
507
508         spin_lock(&memstick_host_lock);
509         rc = idr_get_new(&memstick_host_idr, host, &host->id);
510         spin_unlock(&memstick_host_lock);
511         if (rc)
512                 return rc;
513
514         snprintf(host->cdev.class_id, BUS_ID_SIZE,
515                  "memstick%u", host->id);
516
517         rc = class_device_add(&host->cdev);
518         if (rc) {
519                 spin_lock(&memstick_host_lock);
520                 idr_remove(&memstick_host_idr, host->id);
521                 spin_unlock(&memstick_host_lock);
522                 return rc;
523         }
524
525         host->set_param(host, MEMSTICK_POWER, MEMSTICK_POWER_OFF);
526         memstick_detect_change(host);
527         return 0;
528 }
529 EXPORT_SYMBOL(memstick_add_host);
530
531 /**
532  * memstick_remove_host - stop request processing on memstick host
533  * @host - host to use
534  */
535 void memstick_remove_host(struct memstick_host *host)
536 {
537         flush_workqueue(workqueue);
538         mutex_lock(&host->lock);
539         if (host->card)
540                 device_unregister(&host->card->dev);
541         host->card = NULL;
542         host->set_param(host, MEMSTICK_POWER, MEMSTICK_POWER_OFF);
543         mutex_unlock(&host->lock);
544
545         spin_lock(&memstick_host_lock);
546         idr_remove(&memstick_host_idr, host->id);
547         spin_unlock(&memstick_host_lock);
548         class_device_del(&host->cdev);
549 }
550 EXPORT_SYMBOL(memstick_remove_host);
551
552 /**
553  * memstick_free_host - free memstick host
554  * @host - host to use
555  */
556 void memstick_free_host(struct memstick_host *host)
557 {
558         mutex_destroy(&host->lock);
559         class_device_put(&host->cdev);
560 }
561 EXPORT_SYMBOL(memstick_free_host);
562
563 /**
564  * memstick_suspend_host - notify bus driver of host suspension
565  * @host - host to use
566  */
567 void memstick_suspend_host(struct memstick_host *host)
568 {
569         mutex_lock(&host->lock);
570         host->set_param(host, MEMSTICK_POWER, MEMSTICK_POWER_OFF);
571         mutex_unlock(&host->lock);
572 }
573 EXPORT_SYMBOL(memstick_suspend_host);
574
575 /**
576  * memstick_resume_host - notify bus driver of host resumption
577  * @host - host to use
578  */
579 void memstick_resume_host(struct memstick_host *host)
580 {
581         mutex_lock(&host->lock);
582         host->set_param(host, MEMSTICK_POWER, MEMSTICK_POWER_ON);
583         mutex_unlock(&host->lock);
584         memstick_detect_change(host);
585 }
586 EXPORT_SYMBOL(memstick_resume_host);
587
588 int memstick_register_driver(struct memstick_driver *drv)
589 {
590         drv->driver.bus = &memstick_bus_type;
591
592         return driver_register(&drv->driver);
593 }
594 EXPORT_SYMBOL(memstick_register_driver);
595
596 void memstick_unregister_driver(struct memstick_driver *drv)
597 {
598         driver_unregister(&drv->driver);
599 }
600 EXPORT_SYMBOL(memstick_unregister_driver);
601
602
603 static int __init memstick_init(void)
604 {
605         int rc;
606
607         workqueue = create_freezeable_workqueue("kmemstick");
608         if (!workqueue)
609                 return -ENOMEM;
610
611         rc = bus_register(&memstick_bus_type);
612         if (!rc)
613                 rc = class_register(&memstick_host_class);
614
615         if (!rc)
616                 return 0;
617
618         bus_unregister(&memstick_bus_type);
619         destroy_workqueue(workqueue);
620
621         return rc;
622 }
623
624 static void __exit memstick_exit(void)
625 {
626         class_unregister(&memstick_host_class);
627         bus_unregister(&memstick_bus_type);
628         destroy_workqueue(workqueue);
629         idr_destroy(&memstick_host_idr);
630 }
631
632 module_init(memstick_init);
633 module_exit(memstick_exit);
634
635 MODULE_AUTHOR("Alex Dubov");
636 MODULE_LICENSE("GPL");
637 MODULE_DESCRIPTION("Sony MemoryStick core driver");