card_sysfs.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. /*
  2. * card_sysfs.c
  3. *
  4. * Copyright (C) 2003 Russell King, All Rights Reserved.
  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. * card sysfs/driver model support.
  11. */
  12. #include <linux/module.h>
  13. #include <linux/init.h>
  14. #include <linux/device.h>
  15. #include <linux/idr.h>
  16. #include <linux/workqueue.h>
  17. #include <linux/err.h>
  18. #include <linux/cardreader/sdio.h>
  19. #include <linux/cardreader/card_block.h>
  20. #define dev_to_memory_card(d) container_of(d, struct memory_card, dev)
  21. #define to_card_driver(d) container_of(d, struct card_driver, drv)
  22. #define cls_dev_to_card_host(d) container_of(d, struct card_host, class_dev)
  23. extern struct completion card_devdel_comp;
  24. extern struct completion card_devadd_comp;
  25. void card_release_card(struct device *dev)
  26. {
  27. struct memory_card *card = dev_to_memory_card(dev);
  28. kfree(card);
  29. }
  30. /*
  31. * This currently matches any card driver to any card card - drivers
  32. * themselves make the decision whether to drive this card in their
  33. * probe method. However, we force "bad" cards to fail.
  34. */
  35. static int card_bus_match(struct device *dev, struct device_driver *drv)
  36. {
  37. struct memory_card *card = dev_to_memory_card(dev);
  38. return !(card->state & CARD_STATE_BAD);
  39. }
  40. static int card_bus_suspend(struct device *dev, pm_message_t state)
  41. {
  42. struct card_driver *drv = to_card_driver(dev->driver);
  43. struct memory_card *card = dev_to_memory_card(dev);
  44. int ret = 0;
  45. if (dev->driver && drv->suspend)
  46. ret = drv->suspend(card, state);
  47. return ret;
  48. }
  49. static int card_bus_resume(struct device *dev)
  50. {
  51. struct card_driver *drv = to_card_driver(dev->driver);
  52. struct memory_card *card = dev_to_memory_card(dev);
  53. int ret = 0;
  54. if (dev->driver && drv->resume)
  55. ret = drv->resume(card);
  56. return ret;
  57. }
  58. static int card_bus_probe(struct device *dev)
  59. {
  60. struct card_driver *drv = to_card_driver(dev->driver);
  61. struct memory_card *card = dev_to_memory_card(dev);
  62. if (card->card_type == CARD_SDIO ||
  63. card->card_type == CARD_INAND_LP)
  64. return 0;
  65. return drv->probe(card);
  66. }
  67. static int card_bus_remove(struct device *dev)
  68. {
  69. struct card_driver *drv = to_card_driver(dev->driver);
  70. struct memory_card *card = dev_to_memory_card(dev);
  71. drv->remove(card);
  72. return 0;
  73. }
  74. static struct bus_type card_bus_type = {
  75. .name = "memorycard",
  76. .match = card_bus_match,
  77. .probe = card_bus_probe,
  78. .remove = card_bus_remove,
  79. .suspend = card_bus_suspend,
  80. .resume = card_bus_resume,
  81. };
  82. /**
  83. * card_register_driver - register a media driver
  84. * @drv: card media driver
  85. */
  86. int card_register_driver(struct card_driver *drv)
  87. {
  88. drv->drv.bus = &card_bus_type;
  89. return driver_register(&drv->drv);
  90. }
  91. EXPORT_SYMBOL(card_register_driver);
  92. /**
  93. * card_unregister_driver - unregister a media driver
  94. * @drv: card media driver
  95. */
  96. void card_unregister_driver(struct card_driver *drv)
  97. {
  98. drv->drv.bus = &card_bus_type;
  99. driver_unregister(&drv->drv);
  100. }
  101. EXPORT_SYMBOL(card_unregister_driver);
  102. /*
  103. * Internal function. Initialise a card card structure.
  104. */
  105. void card_init_card(struct memory_card *card, struct card_host *host)
  106. {
  107. memset(card, 0, sizeof(struct memory_card));
  108. card->host = host;
  109. device_initialize(&card->dev);
  110. card->dev.parent = &card->host->class_dev;
  111. card->dev.bus = &card_bus_type;
  112. card->dev.release = card_release_card;
  113. }
  114. EXPORT_SYMBOL(card_init_card);
  115. #include <linux/delay.h>
  116. /*
  117. * Internal function. Register a new card card with the driver model.
  118. */
  119. int card_register_card(struct memory_card *card)
  120. {
  121. int ret = 0;
  122. dev_set_name(&card->dev, "%s:%s", card_hostname(card->host), card->name);
  123. /*return device_add(&card->dev);*/
  124. ret = device_add(&card->dev);
  125. complete(&card_devadd_comp);
  126. return ret;
  127. }
  128. EXPORT_SYMBOL(card_register_card);
  129. /*
  130. * Internal function. Unregister a new card card with the
  131. * driver model, and (eventually) free it.
  132. */
  133. void card_remove_card(struct memory_card *card)
  134. {
  135. if (card->state & CARD_STATE_PRESENT){
  136. init_completion(&card_devdel_comp);
  137. device_del(&card->dev);
  138. put_device(&card->dev);
  139. wait_for_completion(&card_devdel_comp);
  140. return;
  141. }
  142. put_device(&card->dev);
  143. }
  144. EXPORT_SYMBOL(card_remove_card);
  145. static void card_host_classdev_release(struct device *dev)
  146. {
  147. struct card_host *host = cls_dev_to_card_host(dev);
  148. kfree(host);
  149. }
  150. static struct class card_host_class = {
  151. .name = "card_host",
  152. .dev_release = card_host_classdev_release,
  153. };
  154. static DEFINE_IDR(card_host_idr);
  155. static DEFINE_SPINLOCK(card_host_lock);
  156. /*
  157. * Internal function. Allocate a new card host.
  158. */
  159. struct card_host *card_alloc_host_sysfs(int extra, struct device *dev)
  160. {
  161. struct card_host *host;
  162. host = kmalloc(sizeof(struct card_host) + extra, GFP_KERNEL);
  163. if (host) {
  164. memset(host, 0, sizeof(struct card_host) + extra);
  165. host->parent = dev;
  166. host->class_dev.parent = dev;
  167. host->class_dev.class = &card_host_class;
  168. device_initialize(&host->class_dev);
  169. }
  170. return host;
  171. }
  172. EXPORT_SYMBOL(card_alloc_host_sysfs);
  173. /*
  174. * Internal function. Register a new card host with the card class.
  175. */
  176. int card_add_host_sysfs(struct card_host *host)
  177. {
  178. int err;
  179. if (!idr_pre_get(&card_host_idr, GFP_KERNEL))
  180. return -ENOMEM;
  181. spin_lock(&card_host_lock);
  182. err = idr_get_new(&card_host_idr, host, &host->index);
  183. spin_unlock(&card_host_lock);
  184. if (err)
  185. return err;
  186. dev_set_name(&host->class_dev, "memorycard%d", host->index);
  187. //snprintf(host->class_dev.bus_id, BUS_ID_SIZE, "memorycard%d", host->index);
  188. return device_add(&host->class_dev);
  189. }
  190. /*
  191. * Internal function. Unregister a card host with the card class.
  192. */
  193. void card_remove_host_sysfs(struct card_host *host)
  194. {
  195. device_del(&host->class_dev);
  196. spin_lock(&card_host_lock);
  197. idr_remove(&card_host_idr, host->index);
  198. spin_unlock(&card_host_lock);
  199. }
  200. /*
  201. * Internal function. Free a card host.
  202. */
  203. void card_free_host_sysfs(struct card_host *host)
  204. {
  205. put_device(&host->class_dev);
  206. }
  207. static struct workqueue_struct *workqueue;
  208. /*
  209. * Internal function. Schedule work in the card work queue.
  210. */
  211. int card_schedule_work(struct work_struct *work)
  212. {
  213. return queue_work(workqueue, work);
  214. }
  215. /*
  216. * Internal function. Schedule delayed work in the card work queue.
  217. */
  218. int card_schedule_delayed_work(struct delayed_work *work, unsigned long delay)
  219. {
  220. return queue_delayed_work(workqueue, work, delay);
  221. }
  222. /*
  223. * Internal function. Flush all scheduled work from the card work queue.
  224. */
  225. void card_flush_scheduled_work(void)
  226. {
  227. flush_workqueue(workqueue);
  228. }
  229. /**
  230. * card_align_data_size - pads a transfer size to a more optimal value
  231. * @card: the Memory card associated with the data transfer
  232. * @sz: original transfer size
  233. *
  234. * Pads the original data size with a number of extra bytes in
  235. * order to avoid controller bugs and/or performance hits
  236. * (e.g. some controllers revert to PIO for certain sizes).
  237. *
  238. * Returns the improved size, which might be unmodified.
  239. *
  240. * Note that this function is only relevant when issuing a
  241. * single scatter gather entry.
  242. */
  243. unsigned int card_align_data_size(struct memory_card *card, unsigned int sz)
  244. {
  245. /*
  246. * FIXME: We don't have a system for the controller to tell
  247. * the core about its problems yet, so for now we just 32-bit
  248. * align the size.
  249. */
  250. sz = ((sz + 3) / 4) * 4;
  251. return sz;
  252. }
  253. EXPORT_SYMBOL(card_align_data_size);
  254. int card_register_host_class(void)
  255. {
  256. return class_register(&card_host_class);
  257. }
  258. void card_unregister_host_class(void)
  259. {
  260. class_unregister(&card_host_class);
  261. }
  262. int card_register_bus(void)
  263. {
  264. return bus_register(&card_bus_type);
  265. }
  266. void card_unregister_bus(void)
  267. {
  268. return bus_unregister(&card_bus_type);
  269. }
  270. static int __init card_init(void)
  271. {
  272. int ret = 0;
  273. workqueue = create_singlethread_workqueue("kcardd");
  274. if (!workqueue)
  275. return -ENOMEM;
  276. ret = card_register_bus();
  277. if (ret)
  278. goto destroy_workqueue;
  279. ret = card_register_host_class();
  280. if (ret)
  281. goto unregister_bus;
  282. #ifdef CONFIG_SDIO
  283. ret = sdio_register_bus();
  284. if (ret)
  285. goto unregister_host_class;
  286. #endif
  287. return 0;
  288. unregister_host_class:
  289. card_unregister_host_class();
  290. unregister_bus:
  291. card_unregister_bus();
  292. destroy_workqueue:
  293. destroy_workqueue(workqueue);
  294. return ret;
  295. }
  296. static void __exit card_exit(void)
  297. {
  298. #ifdef CONFIG_SDIO
  299. sdio_unregister_bus();
  300. #endif
  301. card_unregister_host_class();
  302. card_unregister_bus();
  303. destroy_workqueue(workqueue);
  304. }
  305. module_init(card_init);
  306. module_exit(card_exit);