tifm_core.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. /*
  2. * tifm_core.c - TI FlashMedia driver
  3. *
  4. * Copyright (C) 2006 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. */
  11. #include <linux/tifm.h>
  12. #include <linux/init.h>
  13. #include <linux/idr.h>
  14. #define DRIVER_NAME "tifm_core"
  15. #define DRIVER_VERSION "0.8"
  16. static DEFINE_IDR(tifm_adapter_idr);
  17. static DEFINE_SPINLOCK(tifm_adapter_lock);
  18. static const char *tifm_media_type_name(unsigned char type, unsigned char nt)
  19. {
  20. const char *card_type_name[3][3] = {
  21. { "SmartMedia/xD", "MemoryStick", "MMC/SD" },
  22. { "XD", "MS", "SD"},
  23. { "xd", "ms", "sd"}
  24. };
  25. if (nt > 2 || type < 1 || type > 3)
  26. return NULL;
  27. return card_type_name[nt][type - 1];
  28. }
  29. static int tifm_dev_match(struct tifm_dev *sock, struct tifm_device_id *id)
  30. {
  31. if (sock->type == id->type)
  32. return 1;
  33. return 0;
  34. }
  35. static int tifm_bus_match(struct device *dev, struct device_driver *drv)
  36. {
  37. struct tifm_dev *sock = container_of(dev, struct tifm_dev, dev);
  38. struct tifm_driver *fm_drv = container_of(drv, struct tifm_driver,
  39. driver);
  40. struct tifm_device_id *ids = fm_drv->id_table;
  41. if (ids) {
  42. while (ids->type) {
  43. if (tifm_dev_match(sock, ids))
  44. return 1;
  45. ++ids;
  46. }
  47. }
  48. return 0;
  49. }
  50. static int tifm_uevent(struct device *dev, char **envp, int num_envp,
  51. char *buffer, int buffer_size)
  52. {
  53. struct tifm_dev *sock = container_of(dev, struct tifm_dev, dev);
  54. int i = 0;
  55. int length = 0;
  56. if (add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &length,
  57. "TIFM_CARD_TYPE=%s",
  58. tifm_media_type_name(sock->type, 1)))
  59. return -ENOMEM;
  60. return 0;
  61. }
  62. static int tifm_device_probe(struct device *dev)
  63. {
  64. struct tifm_dev *sock = container_of(dev, struct tifm_dev, dev);
  65. struct tifm_driver *drv = container_of(dev->driver, struct tifm_driver,
  66. driver);
  67. int rc = -ENODEV;
  68. get_device(dev);
  69. if (dev->driver && drv->probe) {
  70. rc = drv->probe(sock);
  71. if (!rc)
  72. return 0;
  73. }
  74. put_device(dev);
  75. return rc;
  76. }
  77. static void tifm_dummy_event(struct tifm_dev *sock)
  78. {
  79. return;
  80. }
  81. static int tifm_device_remove(struct device *dev)
  82. {
  83. struct tifm_dev *sock = container_of(dev, struct tifm_dev, dev);
  84. struct tifm_driver *drv = container_of(dev->driver, struct tifm_driver,
  85. driver);
  86. if (dev->driver && drv->remove) {
  87. sock->card_event = tifm_dummy_event;
  88. sock->data_event = tifm_dummy_event;
  89. drv->remove(sock);
  90. sock->dev.driver = NULL;
  91. }
  92. put_device(dev);
  93. return 0;
  94. }
  95. #ifdef CONFIG_PM
  96. static int tifm_device_suspend(struct device *dev, pm_message_t state)
  97. {
  98. struct tifm_dev *fm_dev = container_of(dev, struct tifm_dev, dev);
  99. struct tifm_driver *drv = container_of(dev->driver, struct tifm_driver,
  100. driver);
  101. if (dev->driver && drv->suspend)
  102. return drv->suspend(fm_dev, state);
  103. return 0;
  104. }
  105. static int tifm_device_resume(struct device *dev)
  106. {
  107. struct tifm_dev *fm_dev = container_of(dev, struct tifm_dev, dev);
  108. struct tifm_driver *drv = container_of(dev->driver, struct tifm_driver,
  109. driver);
  110. if (dev->driver && drv->resume)
  111. return drv->resume(fm_dev);
  112. return 0;
  113. }
  114. #else
  115. #define tifm_device_suspend NULL
  116. #define tifm_device_resume NULL
  117. #endif /* CONFIG_PM */
  118. static struct bus_type tifm_bus_type = {
  119. .name = "tifm",
  120. .match = tifm_bus_match,
  121. .uevent = tifm_uevent,
  122. .probe = tifm_device_probe,
  123. .remove = tifm_device_remove,
  124. .suspend = tifm_device_suspend,
  125. .resume = tifm_device_resume
  126. };
  127. static void tifm_free(struct class_device *cdev)
  128. {
  129. struct tifm_adapter *fm = container_of(cdev, struct tifm_adapter, cdev);
  130. kfree(fm->sockets);
  131. kfree(fm);
  132. }
  133. static struct class tifm_adapter_class = {
  134. .name = "tifm_adapter",
  135. .release = tifm_free
  136. };
  137. struct tifm_adapter *tifm_alloc_adapter(void)
  138. {
  139. struct tifm_adapter *fm;
  140. fm = kzalloc(sizeof(struct tifm_adapter), GFP_KERNEL);
  141. if (fm) {
  142. fm->cdev.class = &tifm_adapter_class;
  143. spin_lock_init(&fm->lock);
  144. class_device_initialize(&fm->cdev);
  145. }
  146. return fm;
  147. }
  148. EXPORT_SYMBOL(tifm_alloc_adapter);
  149. void tifm_free_adapter(struct tifm_adapter *fm)
  150. {
  151. class_device_put(&fm->cdev);
  152. }
  153. EXPORT_SYMBOL(tifm_free_adapter);
  154. int tifm_add_adapter(struct tifm_adapter *fm,
  155. int (*mediathreadfn)(void *data))
  156. {
  157. int rc;
  158. if (!idr_pre_get(&tifm_adapter_idr, GFP_KERNEL))
  159. return -ENOMEM;
  160. spin_lock(&tifm_adapter_lock);
  161. rc = idr_get_new(&tifm_adapter_idr, fm, &fm->id);
  162. spin_unlock(&tifm_adapter_lock);
  163. if (!rc) {
  164. snprintf(fm->cdev.class_id, BUS_ID_SIZE, "tifm%u", fm->id);
  165. fm->media_switcher = kthread_create(mediathreadfn,
  166. fm, "tifm/%u", fm->id);
  167. if (!IS_ERR(fm->media_switcher))
  168. return class_device_add(&fm->cdev);
  169. spin_lock(&tifm_adapter_lock);
  170. idr_remove(&tifm_adapter_idr, fm->id);
  171. spin_unlock(&tifm_adapter_lock);
  172. rc = -ENOMEM;
  173. }
  174. return rc;
  175. }
  176. EXPORT_SYMBOL(tifm_add_adapter);
  177. void tifm_remove_adapter(struct tifm_adapter *fm)
  178. {
  179. class_device_del(&fm->cdev);
  180. spin_lock(&tifm_adapter_lock);
  181. idr_remove(&tifm_adapter_idr, fm->id);
  182. spin_unlock(&tifm_adapter_lock);
  183. }
  184. EXPORT_SYMBOL(tifm_remove_adapter);
  185. void tifm_free_device(struct device *dev)
  186. {
  187. struct tifm_dev *fm_dev = container_of(dev, struct tifm_dev, dev);
  188. kfree(fm_dev);
  189. }
  190. EXPORT_SYMBOL(tifm_free_device);
  191. struct tifm_dev *tifm_alloc_device(struct tifm_adapter *fm)
  192. {
  193. struct tifm_dev *dev = kzalloc(sizeof(struct tifm_dev), GFP_KERNEL);
  194. if (dev) {
  195. spin_lock_init(&dev->lock);
  196. dev->dev.parent = fm->dev;
  197. dev->dev.bus = &tifm_bus_type;
  198. dev->dev.release = tifm_free_device;
  199. dev->card_event = tifm_dummy_event;
  200. dev->data_event = tifm_dummy_event;
  201. }
  202. return dev;
  203. }
  204. EXPORT_SYMBOL(tifm_alloc_device);
  205. void tifm_eject(struct tifm_dev *sock)
  206. {
  207. struct tifm_adapter *fm = dev_get_drvdata(sock->dev.parent);
  208. fm->eject(fm, sock);
  209. }
  210. EXPORT_SYMBOL(tifm_eject);
  211. int tifm_map_sg(struct tifm_dev *sock, struct scatterlist *sg, int nents,
  212. int direction)
  213. {
  214. return pci_map_sg(to_pci_dev(sock->dev.parent), sg, nents, direction);
  215. }
  216. EXPORT_SYMBOL(tifm_map_sg);
  217. void tifm_unmap_sg(struct tifm_dev *sock, struct scatterlist *sg, int nents,
  218. int direction)
  219. {
  220. pci_unmap_sg(to_pci_dev(sock->dev.parent), sg, nents, direction);
  221. }
  222. EXPORT_SYMBOL(tifm_unmap_sg);
  223. int tifm_register_driver(struct tifm_driver *drv)
  224. {
  225. drv->driver.bus = &tifm_bus_type;
  226. return driver_register(&drv->driver);
  227. }
  228. EXPORT_SYMBOL(tifm_register_driver);
  229. void tifm_unregister_driver(struct tifm_driver *drv)
  230. {
  231. driver_unregister(&drv->driver);
  232. }
  233. EXPORT_SYMBOL(tifm_unregister_driver);
  234. static int __init tifm_init(void)
  235. {
  236. int rc = bus_register(&tifm_bus_type);
  237. if (!rc) {
  238. rc = class_register(&tifm_adapter_class);
  239. if (rc)
  240. bus_unregister(&tifm_bus_type);
  241. }
  242. return rc;
  243. }
  244. static void __exit tifm_exit(void)
  245. {
  246. class_unregister(&tifm_adapter_class);
  247. bus_unregister(&tifm_bus_type);
  248. }
  249. subsys_initcall(tifm_init);
  250. module_exit(tifm_exit);
  251. MODULE_LICENSE("GPL");
  252. MODULE_AUTHOR("Alex Dubov");
  253. MODULE_DESCRIPTION("TI FlashMedia core driver");
  254. MODULE_LICENSE("GPL");
  255. MODULE_VERSION(DRIVER_VERSION);