tifm_core.c 6.7 KB

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