Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
memstick.c
Go to the documentation of this file.
1 /*
2  * Sony MemoryStick support
3  *
4  * Copyright (C) 2007 Alex Dubov <[email protected]>
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 #include <linux/slab.h>
20 #include <linux/module.h>
21 
22 #define DRIVER_NAME "memstick"
23 
24 static unsigned int cmd_retries = 3;
25 module_param(cmd_retries, uint, 0644);
26 
27 static struct workqueue_struct *workqueue;
28 static DEFINE_IDR(memstick_host_idr);
29 static DEFINE_SPINLOCK(memstick_host_lock);
30 
31 static int memstick_dev_match(struct memstick_dev *card,
32  struct memstick_device_id *id)
33 {
34  if (id->match_flags & MEMSTICK_MATCH_ALL) {
35  if ((id->type == card->id.type)
36  && (id->category == card->id.category)
37  && (id->class == card->id.class))
38  return 1;
39  }
40 
41  return 0;
42 }
43 
44 static int memstick_bus_match(struct device *dev, struct device_driver *drv)
45 {
46  struct memstick_dev *card = container_of(dev, struct memstick_dev,
47  dev);
48  struct memstick_driver *ms_drv = container_of(drv,
49  struct memstick_driver,
50  driver);
51  struct memstick_device_id *ids = ms_drv->id_table;
52 
53  if (ids) {
54  while (ids->match_flags) {
55  if (memstick_dev_match(card, ids))
56  return 1;
57  ++ids;
58  }
59  }
60  return 0;
61 }
62 
63 static int memstick_uevent(struct device *dev, struct kobj_uevent_env *env)
64 {
65  struct memstick_dev *card = container_of(dev, struct memstick_dev,
66  dev);
67 
68  if (add_uevent_var(env, "MEMSTICK_TYPE=%02X", card->id.type))
69  return -ENOMEM;
70 
71  if (add_uevent_var(env, "MEMSTICK_CATEGORY=%02X", card->id.category))
72  return -ENOMEM;
73 
74  if (add_uevent_var(env, "MEMSTICK_CLASS=%02X", card->id.class))
75  return -ENOMEM;
76 
77  return 0;
78 }
79 
80 static int memstick_device_probe(struct device *dev)
81 {
82  struct memstick_dev *card = container_of(dev, struct memstick_dev,
83  dev);
84  struct memstick_driver *drv = container_of(dev->driver,
85  struct memstick_driver,
86  driver);
87  int rc = -ENODEV;
88 
89  if (dev->driver && drv->probe) {
90  rc = drv->probe(card);
91  if (!rc)
92  get_device(dev);
93  }
94  return rc;
95 }
96 
97 static int memstick_device_remove(struct device *dev)
98 {
99  struct memstick_dev *card = container_of(dev, struct memstick_dev,
100  dev);
101  struct memstick_driver *drv = container_of(dev->driver,
102  struct memstick_driver,
103  driver);
104 
105  if (dev->driver && drv->remove) {
106  drv->remove(card);
107  card->dev.driver = NULL;
108  }
109 
110  put_device(dev);
111  return 0;
112 }
113 
114 #ifdef CONFIG_PM
115 
116 static int memstick_device_suspend(struct device *dev, pm_message_t state)
117 {
118  struct memstick_dev *card = container_of(dev, struct memstick_dev,
119  dev);
120  struct memstick_driver *drv = container_of(dev->driver,
121  struct memstick_driver,
122  driver);
123 
124  if (dev->driver && drv->suspend)
125  return drv->suspend(card, state);
126  return 0;
127 }
128 
129 static int memstick_device_resume(struct device *dev)
130 {
131  struct memstick_dev *card = container_of(dev, struct memstick_dev,
132  dev);
133  struct memstick_driver *drv = container_of(dev->driver,
134  struct memstick_driver,
135  driver);
136 
137  if (dev->driver && drv->resume)
138  return drv->resume(card);
139  return 0;
140 }
141 
142 #else
143 
144 #define memstick_device_suspend NULL
145 #define memstick_device_resume NULL
146 
147 #endif /* CONFIG_PM */
148 
149 #define MEMSTICK_ATTR(name, format) \
150 static ssize_t name##_show(struct device *dev, struct device_attribute *attr, \
151  char *buf) \
152 { \
153  struct memstick_dev *card = container_of(dev, struct memstick_dev, \
154  dev); \
155  return sprintf(buf, format, card->id.name); \
156 }
157 
158 MEMSTICK_ATTR(type, "%02X");
159 MEMSTICK_ATTR(category, "%02X");
160 MEMSTICK_ATTR(class, "%02X");
161 
162 #define MEMSTICK_ATTR_RO(name) __ATTR(name, S_IRUGO, name##_show, NULL)
163 
164 static struct device_attribute memstick_dev_attrs[] = {
167  MEMSTICK_ATTR_RO(class),
169 };
170 
171 static struct bus_type memstick_bus_type = {
172  .name = "memstick",
173  .dev_attrs = memstick_dev_attrs,
174  .match = memstick_bus_match,
175  .uevent = memstick_uevent,
176  .probe = memstick_device_probe,
177  .remove = memstick_device_remove,
178  .suspend = memstick_device_suspend,
179  .resume = memstick_device_resume
180 };
181 
182 static void memstick_free(struct device *dev)
183 {
184  struct memstick_host *host = container_of(dev, struct memstick_host,
185  dev);
186  kfree(host);
187 }
188 
189 static struct class memstick_host_class = {
190  .name = "memstick_host",
191  .dev_release = memstick_free
192 };
193 
194 static void memstick_free_card(struct device *dev)
195 {
196  struct memstick_dev *card = container_of(dev, struct memstick_dev,
197  dev);
198  kfree(card);
199 }
200 
201 static int memstick_dummy_check(struct memstick_dev *card)
202 {
203  return 0;
204 }
205 
211 {
212  queue_work(workqueue, &host->media_checker);
213 }
215 
227 {
228  int rc = -ENXIO;
229 
230  if ((*mrq) && (*mrq)->error && host->retries) {
231  (*mrq)->error = rc;
232  host->retries--;
233  return 0;
234  }
235 
236  if (host->card && host->card->next_request)
237  rc = host->card->next_request(host->card, mrq);
238 
239  if (!rc)
240  host->retries = cmd_retries > 1 ? cmd_retries - 1 : 1;
241  else
242  *mrq = NULL;
243 
244  return rc;
245 }
247 
253 {
254  if (host->card) {
255  host->retries = cmd_retries;
256  INIT_COMPLETION(host->card->mrq_complete);
257  host->request(host);
258  }
259 }
261 
268 void memstick_init_req_sg(struct memstick_request *mrq, unsigned char tpc,
269  const struct scatterlist *sg)
270 {
271  mrq->tpc = tpc;
272  if (tpc & 8)
273  mrq->data_dir = WRITE;
274  else
275  mrq->data_dir = READ;
276 
277  mrq->sg = *sg;
278  mrq->long_data = 1;
279 
280  if (tpc == MS_TPC_SET_CMD || tpc == MS_TPC_EX_SET_CMD)
281  mrq->need_card_int = 1;
282  else
283  mrq->need_card_int = 0;
284 }
286 
298 void memstick_init_req(struct memstick_request *mrq, unsigned char tpc,
299  const void *buf, size_t length)
300 {
301  mrq->tpc = tpc;
302  if (tpc & 8)
303  mrq->data_dir = WRITE;
304  else
305  mrq->data_dir = READ;
306 
307  mrq->data_len = length > sizeof(mrq->data) ? sizeof(mrq->data) : length;
308  if (mrq->data_dir == WRITE)
309  memcpy(mrq->data, buf, mrq->data_len);
310 
311  mrq->long_data = 0;
312 
313  if (tpc == MS_TPC_SET_CMD || tpc == MS_TPC_EX_SET_CMD)
314  mrq->need_card_int = 1;
315  else
316  mrq->need_card_int = 0;
317 }
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 
327 static 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)) {
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  dev_dbg(&card->dev, "if_mode = %02x\n", id_reg.if_mode);
345  }
346  complete(&card->mrq_complete);
347  return -EAGAIN;
348  }
349 }
350 
351 static int h_memstick_set_rw_addr(struct memstick_dev *card,
352  struct memstick_request **mrq)
353 {
354  if (!(*mrq)) {
356  (char *)&card->reg_addr,
357  sizeof(card->reg_addr));
358  *mrq = &card->current_mrq;
359  return 0;
360  } else {
361  complete(&card->mrq_complete);
362  return -EAGAIN;
363  }
364 }
365 
372 {
373  card->next_request = h_memstick_set_rw_addr;
374  memstick_new_req(card->host);
376 
377  return card->current_mrq.error;
378 }
380 
381 static struct memstick_dev *memstick_alloc_card(struct memstick_host *host)
382 {
383  struct memstick_dev *card = kzalloc(sizeof(struct memstick_dev),
384  GFP_KERNEL);
385  struct memstick_dev *old_card = host->card;
386  struct ms_id_register id_reg;
387 
388  if (card) {
389  card->host = host;
390  dev_set_name(&card->dev, "%s", dev_name(&host->dev));
391  card->dev.parent = &host->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);
410 
411  if (card->current_mrq.error)
412  goto err_out;
413  }
414  host->card = old_card;
415  return card;
416 err_out:
417  host->card = old_card;
418  kfree(card);
419  return NULL;
420 }
421 
422 static int memstick_power_on(struct memstick_host *host)
423 {
424  int rc = host->set_param(host, MEMSTICK_POWER, MEMSTICK_POWER_ON);
425 
426  if (!rc)
427  rc = host->set_param(host, MEMSTICK_INTERFACE, MEMSTICK_SERIAL);
428 
429  return rc;
430 }
431 
432 static void memstick_check(struct work_struct *work)
433 {
434  struct memstick_host *host = container_of(work, struct memstick_host,
435  media_checker);
436  struct memstick_dev *card;
437 
438  dev_dbg(&host->dev, "memstick_check started\n");
439  mutex_lock(&host->lock);
440  if (!host->card) {
441  if (memstick_power_on(host))
442  goto out_power_off;
443  } else if (host->card->stop)
444  host->card->stop(host->card);
445 
446  card = memstick_alloc_card(host);
447 
448  if (!card) {
449  if (host->card) {
450  device_unregister(&host->card->dev);
451  host->card = NULL;
452  }
453  } else {
454  dev_dbg(&host->dev, "new card %02x, %02x, %02x\n",
455  card->id.type, card->id.category, card->id.class);
456  if (host->card) {
457  if (memstick_set_rw_addr(host->card)
458  || !memstick_dev_match(host->card, &card->id)
459  || !(host->card->check(host->card))) {
460  device_unregister(&host->card->dev);
461  host->card = NULL;
462  } else if (host->card->start)
463  host->card->start(host->card);
464  }
465 
466  if (!host->card) {
467  host->card = card;
468  if (device_register(&card->dev)) {
469  put_device(&card->dev);
470  kfree(host->card);
471  host->card = NULL;
472  }
473  } else
474  kfree(card);
475  }
476 
477 out_power_off:
478  if (!host->card)
480 
481  mutex_unlock(&host->lock);
482  dev_dbg(&host->dev, "memstick_check finished\n");
483 }
484 
491  struct device *dev)
492 {
493  struct memstick_host *host;
494 
495  host = kzalloc(sizeof(struct memstick_host) + extra, GFP_KERNEL);
496  if (host) {
497  mutex_init(&host->lock);
498  INIT_WORK(&host->media_checker, memstick_check);
499  host->dev.class = &memstick_host_class;
500  host->dev.parent = dev;
501  device_initialize(&host->dev);
502  }
503  return host;
504 }
506 
512 {
513  int rc;
514 
515  while (1) {
516  if (!idr_pre_get(&memstick_host_idr, GFP_KERNEL))
517  return -ENOMEM;
518 
519  spin_lock(&memstick_host_lock);
520  rc = idr_get_new(&memstick_host_idr, host, &host->id);
521  spin_unlock(&memstick_host_lock);
522  if (!rc)
523  break;
524  else if (rc != -EAGAIN)
525  return rc;
526  }
527 
528  dev_set_name(&host->dev, "memstick%u", host->id);
529 
530  rc = device_add(&host->dev);
531  if (rc) {
532  spin_lock(&memstick_host_lock);
533  idr_remove(&memstick_host_idr, host->id);
534  spin_unlock(&memstick_host_lock);
535  return rc;
536  }
537 
540  return 0;
541 }
543 
549 {
550  flush_workqueue(workqueue);
551  mutex_lock(&host->lock);
552  if (host->card)
553  device_unregister(&host->card->dev);
554  host->card = NULL;
556  mutex_unlock(&host->lock);
557 
558  spin_lock(&memstick_host_lock);
559  idr_remove(&memstick_host_idr, host->id);
560  spin_unlock(&memstick_host_lock);
561  device_del(&host->dev);
562 }
564 
570 {
571  mutex_destroy(&host->lock);
572  put_device(&host->dev);
573 }
575 
581 {
582  mutex_lock(&host->lock);
584  mutex_unlock(&host->lock);
585 }
587 
593 {
594  int rc = 0;
595 
596  mutex_lock(&host->lock);
597  if (host->card)
598  rc = memstick_power_on(host);
599  mutex_unlock(&host->lock);
600 
601  if (!rc)
603 }
605 
607 {
608  drv->driver.bus = &memstick_bus_type;
609 
610  return driver_register(&drv->driver);
611 }
613 
615 {
616  driver_unregister(&drv->driver);
617 }
619 
620 
621 static int __init memstick_init(void)
622 {
623  int rc;
624 
625  workqueue = create_freezable_workqueue("kmemstick");
626  if (!workqueue)
627  return -ENOMEM;
628 
629  rc = bus_register(&memstick_bus_type);
630  if (!rc)
631  rc = class_register(&memstick_host_class);
632 
633  if (!rc)
634  return 0;
635 
636  bus_unregister(&memstick_bus_type);
637  destroy_workqueue(workqueue);
638 
639  return rc;
640 }
641 
642 static void __exit memstick_exit(void)
643 {
644  class_unregister(&memstick_host_class);
645  bus_unregister(&memstick_bus_type);
646  destroy_workqueue(workqueue);
647  idr_destroy(&memstick_host_idr);
648 }
649 
650 module_init(memstick_init);
651 module_exit(memstick_exit);
652 
653 MODULE_AUTHOR("Alex Dubov");
654 MODULE_LICENSE("GPL");
655 MODULE_DESCRIPTION("Sony MemoryStick core driver");