tifm_core.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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.6"
  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. static struct bus_type tifm_bus_type = {
  54. .name = "tifm",
  55. .match = tifm_match,
  56. .uevent = tifm_uevent,
  57. };
  58. static void tifm_free(struct class_device *cdev)
  59. {
  60. struct tifm_adapter *fm = container_of(cdev, struct tifm_adapter, cdev);
  61. kfree(fm->sockets);
  62. if (fm->wq)
  63. destroy_workqueue(fm->wq);
  64. kfree(fm);
  65. }
  66. static struct class tifm_adapter_class = {
  67. .name = "tifm_adapter",
  68. .release = tifm_free
  69. };
  70. struct tifm_adapter *tifm_alloc_adapter(void)
  71. {
  72. struct tifm_adapter *fm;
  73. fm = kzalloc(sizeof(struct tifm_adapter), GFP_KERNEL);
  74. if (fm) {
  75. fm->cdev.class = &tifm_adapter_class;
  76. spin_lock_init(&fm->lock);
  77. class_device_initialize(&fm->cdev);
  78. }
  79. return fm;
  80. }
  81. EXPORT_SYMBOL(tifm_alloc_adapter);
  82. void tifm_free_adapter(struct tifm_adapter *fm)
  83. {
  84. class_device_put(&fm->cdev);
  85. }
  86. EXPORT_SYMBOL(tifm_free_adapter);
  87. int tifm_add_adapter(struct tifm_adapter *fm)
  88. {
  89. int rc;
  90. if (!idr_pre_get(&tifm_adapter_idr, GFP_KERNEL))
  91. return -ENOMEM;
  92. spin_lock(&tifm_adapter_lock);
  93. rc = idr_get_new(&tifm_adapter_idr, fm, &fm->id);
  94. spin_unlock(&tifm_adapter_lock);
  95. if (!rc) {
  96. snprintf(fm->cdev.class_id, BUS_ID_SIZE, "tifm%u", fm->id);
  97. strncpy(fm->wq_name, fm->cdev.class_id, KOBJ_NAME_LEN);
  98. fm->wq = create_singlethread_workqueue(fm->wq_name);
  99. if (fm->wq)
  100. return class_device_add(&fm->cdev);
  101. spin_lock(&tifm_adapter_lock);
  102. idr_remove(&tifm_adapter_idr, fm->id);
  103. spin_unlock(&tifm_adapter_lock);
  104. rc = -ENOMEM;
  105. }
  106. return rc;
  107. }
  108. EXPORT_SYMBOL(tifm_add_adapter);
  109. void tifm_remove_adapter(struct tifm_adapter *fm)
  110. {
  111. class_device_del(&fm->cdev);
  112. spin_lock(&tifm_adapter_lock);
  113. idr_remove(&tifm_adapter_idr, fm->id);
  114. spin_unlock(&tifm_adapter_lock);
  115. }
  116. EXPORT_SYMBOL(tifm_remove_adapter);
  117. void tifm_free_device(struct device *dev)
  118. {
  119. struct tifm_dev *fm_dev = container_of(dev, struct tifm_dev, dev);
  120. if (fm_dev->wq)
  121. destroy_workqueue(fm_dev->wq);
  122. kfree(fm_dev);
  123. }
  124. EXPORT_SYMBOL(tifm_free_device);
  125. struct tifm_dev *tifm_alloc_device(struct tifm_adapter *fm, unsigned int id)
  126. {
  127. struct tifm_dev *dev = kzalloc(sizeof(struct tifm_dev), GFP_KERNEL);
  128. if (dev) {
  129. spin_lock_init(&dev->lock);
  130. snprintf(dev->wq_name, KOBJ_NAME_LEN, "tifm%u:%u", fm->id, id);
  131. dev->wq = create_singlethread_workqueue(dev->wq_name);
  132. if (!dev->wq) {
  133. kfree(dev);
  134. return NULL;
  135. }
  136. dev->dev.parent = fm->dev;
  137. dev->dev.bus = &tifm_bus_type;
  138. dev->dev.release = tifm_free_device;
  139. }
  140. return dev;
  141. }
  142. EXPORT_SYMBOL(tifm_alloc_device);
  143. void tifm_eject(struct tifm_dev *sock)
  144. {
  145. struct tifm_adapter *fm = dev_get_drvdata(sock->dev.parent);
  146. fm->eject(fm, sock);
  147. }
  148. EXPORT_SYMBOL(tifm_eject);
  149. int tifm_map_sg(struct tifm_dev *sock, struct scatterlist *sg, int nents,
  150. int direction)
  151. {
  152. return pci_map_sg(to_pci_dev(sock->dev.parent), sg, nents, direction);
  153. }
  154. EXPORT_SYMBOL(tifm_map_sg);
  155. void tifm_unmap_sg(struct tifm_dev *sock, struct scatterlist *sg, int nents,
  156. int direction)
  157. {
  158. pci_unmap_sg(to_pci_dev(sock->dev.parent), sg, nents, direction);
  159. }
  160. EXPORT_SYMBOL(tifm_unmap_sg);
  161. static int tifm_device_probe(struct device *dev)
  162. {
  163. struct tifm_driver *drv;
  164. struct tifm_dev *fm_dev;
  165. int rc = 0;
  166. const tifm_media_id *id;
  167. drv = container_of(dev->driver, struct tifm_driver, driver);
  168. fm_dev = container_of(dev, struct tifm_dev, dev);
  169. get_device(dev);
  170. if (!fm_dev->drv && drv->probe && drv->id_table) {
  171. rc = -ENODEV;
  172. id = tifm_device_match(drv->id_table, fm_dev);
  173. if (id)
  174. rc = drv->probe(fm_dev);
  175. if (rc >= 0) {
  176. rc = 0;
  177. fm_dev->drv = drv;
  178. }
  179. }
  180. if (rc)
  181. put_device(dev);
  182. return rc;
  183. }
  184. static int tifm_device_remove(struct device *dev)
  185. {
  186. struct tifm_dev *fm_dev = container_of(dev, struct tifm_dev, dev);
  187. struct tifm_driver *drv = fm_dev->drv;
  188. if (drv) {
  189. if (drv->remove) drv->remove(fm_dev);
  190. fm_dev->drv = 0;
  191. }
  192. put_device(dev);
  193. return 0;
  194. }
  195. int tifm_register_driver(struct tifm_driver *drv)
  196. {
  197. drv->driver.bus = &tifm_bus_type;
  198. drv->driver.probe = tifm_device_probe;
  199. drv->driver.remove = tifm_device_remove;
  200. return driver_register(&drv->driver);
  201. }
  202. EXPORT_SYMBOL(tifm_register_driver);
  203. void tifm_unregister_driver(struct tifm_driver *drv)
  204. {
  205. driver_unregister(&drv->driver);
  206. }
  207. EXPORT_SYMBOL(tifm_unregister_driver);
  208. static int __init tifm_init(void)
  209. {
  210. int rc = bus_register(&tifm_bus_type);
  211. if (!rc) {
  212. rc = class_register(&tifm_adapter_class);
  213. if (rc)
  214. bus_unregister(&tifm_bus_type);
  215. }
  216. return rc;
  217. }
  218. static void __exit tifm_exit(void)
  219. {
  220. class_unregister(&tifm_adapter_class);
  221. bus_unregister(&tifm_bus_type);
  222. }
  223. subsys_initcall(tifm_init);
  224. module_exit(tifm_exit);
  225. MODULE_LICENSE("GPL");
  226. MODULE_AUTHOR("Alex Dubov");
  227. MODULE_DESCRIPTION("TI FlashMedia core driver");
  228. MODULE_LICENSE("GPL");
  229. MODULE_VERSION(DRIVER_VERSION);