tifm_core.c 6.8 KB

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