tifm_core.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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 struct workqueue_struct *workqueue;
  17. static DEFINE_IDR(tifm_adapter_idr);
  18. static DEFINE_SPINLOCK(tifm_adapter_lock);
  19. static const char *tifm_media_type_name(unsigned char type, unsigned char nt)
  20. {
  21. const char *card_type_name[3][3] = {
  22. { "SmartMedia/xD", "MemoryStick", "MMC/SD" },
  23. { "XD", "MS", "SD"},
  24. { "xd", "ms", "sd"}
  25. };
  26. if (nt > 2 || type < 1 || type > 3)
  27. return NULL;
  28. return card_type_name[nt][type - 1];
  29. }
  30. static int tifm_dev_match(struct tifm_dev *sock, struct tifm_device_id *id)
  31. {
  32. if (sock->type == id->type)
  33. return 1;
  34. return 0;
  35. }
  36. static int tifm_bus_match(struct device *dev, struct device_driver *drv)
  37. {
  38. struct tifm_dev *sock = container_of(dev, struct tifm_dev, dev);
  39. struct tifm_driver *fm_drv = container_of(drv, struct tifm_driver,
  40. driver);
  41. struct tifm_device_id *ids = fm_drv->id_table;
  42. if (ids) {
  43. while (ids->type) {
  44. if (tifm_dev_match(sock, ids))
  45. return 1;
  46. ++ids;
  47. }
  48. }
  49. return 0;
  50. }
  51. static int tifm_uevent(struct device *dev, char **envp, int num_envp,
  52. char *buffer, int buffer_size)
  53. {
  54. struct tifm_dev *sock = container_of(dev, struct tifm_dev, dev);
  55. int i = 0;
  56. int length = 0;
  57. if (add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &length,
  58. "TIFM_CARD_TYPE=%s",
  59. tifm_media_type_name(sock->type, 1)))
  60. return -ENOMEM;
  61. return 0;
  62. }
  63. static int tifm_device_probe(struct device *dev)
  64. {
  65. struct tifm_dev *sock = container_of(dev, struct tifm_dev, dev);
  66. struct tifm_driver *drv = container_of(dev->driver, struct tifm_driver,
  67. driver);
  68. int rc = -ENODEV;
  69. get_device(dev);
  70. if (dev->driver && drv->probe) {
  71. rc = drv->probe(sock);
  72. if (!rc)
  73. return 0;
  74. }
  75. put_device(dev);
  76. return rc;
  77. }
  78. static void tifm_dummy_event(struct tifm_dev *sock)
  79. {
  80. return;
  81. }
  82. static int tifm_device_remove(struct device *dev)
  83. {
  84. struct tifm_dev *sock = container_of(dev, struct tifm_dev, dev);
  85. struct tifm_driver *drv = container_of(dev->driver, struct tifm_driver,
  86. driver);
  87. if (dev->driver && drv->remove) {
  88. sock->card_event = tifm_dummy_event;
  89. sock->data_event = tifm_dummy_event;
  90. drv->remove(sock);
  91. sock->dev.driver = NULL;
  92. }
  93. put_device(dev);
  94. return 0;
  95. }
  96. #ifdef CONFIG_PM
  97. static int tifm_device_suspend(struct device *dev, pm_message_t state)
  98. {
  99. struct tifm_dev *sock = container_of(dev, struct tifm_dev, dev);
  100. struct tifm_driver *drv = container_of(dev->driver, struct tifm_driver,
  101. driver);
  102. if (dev->driver && drv->suspend)
  103. return drv->suspend(sock, state);
  104. return 0;
  105. }
  106. static int tifm_device_resume(struct device *dev)
  107. {
  108. struct tifm_dev *sock = container_of(dev, struct tifm_dev, dev);
  109. struct tifm_driver *drv = container_of(dev->driver, struct tifm_driver,
  110. driver);
  111. if (dev->driver && drv->resume)
  112. return drv->resume(sock);
  113. return 0;
  114. }
  115. #else
  116. #define tifm_device_suspend NULL
  117. #define tifm_device_resume NULL
  118. #endif /* CONFIG_PM */
  119. static ssize_t type_show(struct device *dev, struct device_attribute *attr,
  120. char *buf)
  121. {
  122. struct tifm_dev *sock = container_of(dev, struct tifm_dev, dev);
  123. return sprintf(buf, "%x", sock->type);
  124. }
  125. static struct device_attribute tifm_dev_attrs[] = {
  126. __ATTR(type, S_IRUGO, type_show, NULL),
  127. __ATTR_NULL
  128. };
  129. static struct bus_type tifm_bus_type = {
  130. .name = "tifm",
  131. .dev_attrs = tifm_dev_attrs,
  132. .match = tifm_bus_match,
  133. .uevent = tifm_uevent,
  134. .probe = tifm_device_probe,
  135. .remove = tifm_device_remove,
  136. .suspend = tifm_device_suspend,
  137. .resume = tifm_device_resume
  138. };
  139. static void tifm_free(struct class_device *cdev)
  140. {
  141. struct tifm_adapter *fm = container_of(cdev, struct tifm_adapter, cdev);
  142. kfree(fm);
  143. }
  144. static struct class tifm_adapter_class = {
  145. .name = "tifm_adapter",
  146. .release = tifm_free
  147. };
  148. struct tifm_adapter *tifm_alloc_adapter(unsigned int num_sockets,
  149. struct device *dev)
  150. {
  151. struct tifm_adapter *fm;
  152. fm = kzalloc(sizeof(struct tifm_adapter)
  153. + sizeof(struct tifm_dev*) * num_sockets, GFP_KERNEL);
  154. if (fm) {
  155. fm->cdev.class = &tifm_adapter_class;
  156. fm->cdev.dev = dev;
  157. class_device_initialize(&fm->cdev);
  158. spin_lock_init(&fm->lock);
  159. fm->num_sockets = num_sockets;
  160. }
  161. return fm;
  162. }
  163. EXPORT_SYMBOL(tifm_alloc_adapter);
  164. int tifm_add_adapter(struct tifm_adapter *fm)
  165. {
  166. int rc;
  167. if (!idr_pre_get(&tifm_adapter_idr, GFP_KERNEL))
  168. return -ENOMEM;
  169. spin_lock(&tifm_adapter_lock);
  170. rc = idr_get_new(&tifm_adapter_idr, fm, &fm->id);
  171. spin_unlock(&tifm_adapter_lock);
  172. if (rc)
  173. return rc;
  174. snprintf(fm->cdev.class_id, BUS_ID_SIZE, "tifm%u", fm->id);
  175. rc = class_device_add(&fm->cdev);
  176. if (rc) {
  177. spin_lock(&tifm_adapter_lock);
  178. idr_remove(&tifm_adapter_idr, fm->id);
  179. spin_unlock(&tifm_adapter_lock);
  180. }
  181. return rc;
  182. }
  183. EXPORT_SYMBOL(tifm_add_adapter);
  184. void tifm_remove_adapter(struct tifm_adapter *fm)
  185. {
  186. unsigned int cnt;
  187. flush_workqueue(workqueue);
  188. for (cnt = 0; cnt < fm->num_sockets; ++cnt) {
  189. if (fm->sockets[cnt])
  190. device_unregister(&fm->sockets[cnt]->dev);
  191. }
  192. spin_lock(&tifm_adapter_lock);
  193. idr_remove(&tifm_adapter_idr, fm->id);
  194. spin_unlock(&tifm_adapter_lock);
  195. class_device_del(&fm->cdev);
  196. }
  197. EXPORT_SYMBOL(tifm_remove_adapter);
  198. void tifm_free_adapter(struct tifm_adapter *fm)
  199. {
  200. class_device_put(&fm->cdev);
  201. }
  202. EXPORT_SYMBOL(tifm_free_adapter);
  203. void tifm_free_device(struct device *dev)
  204. {
  205. struct tifm_dev *sock = container_of(dev, struct tifm_dev, dev);
  206. kfree(sock);
  207. }
  208. EXPORT_SYMBOL(tifm_free_device);
  209. struct tifm_dev *tifm_alloc_device(struct tifm_adapter *fm, unsigned int id,
  210. unsigned char type)
  211. {
  212. struct tifm_dev *sock = NULL;
  213. if (!tifm_media_type_name(type, 0))
  214. return sock;
  215. sock = kzalloc(sizeof(struct tifm_dev), GFP_KERNEL);
  216. if (sock) {
  217. spin_lock_init(&sock->lock);
  218. sock->type = type;
  219. sock->socket_id = id;
  220. sock->card_event = tifm_dummy_event;
  221. sock->data_event = tifm_dummy_event;
  222. sock->dev.parent = fm->cdev.dev;
  223. sock->dev.bus = &tifm_bus_type;
  224. sock->dev.dma_mask = fm->cdev.dev->dma_mask;
  225. sock->dev.release = tifm_free_device;
  226. snprintf(sock->dev.bus_id, BUS_ID_SIZE,
  227. "tifm_%s%u:%u", tifm_media_type_name(type, 2),
  228. fm->id, id);
  229. printk(KERN_INFO DRIVER_NAME
  230. ": %s card detected in socket %u:%u\n",
  231. tifm_media_type_name(type, 0), fm->id, id);
  232. }
  233. return sock;
  234. }
  235. EXPORT_SYMBOL(tifm_alloc_device);
  236. void tifm_eject(struct tifm_dev *sock)
  237. {
  238. struct tifm_adapter *fm = dev_get_drvdata(sock->dev.parent);
  239. fm->eject(fm, sock);
  240. }
  241. EXPORT_SYMBOL(tifm_eject);
  242. int tifm_map_sg(struct tifm_dev *sock, struct scatterlist *sg, int nents,
  243. int direction)
  244. {
  245. return pci_map_sg(to_pci_dev(sock->dev.parent), sg, nents, direction);
  246. }
  247. EXPORT_SYMBOL(tifm_map_sg);
  248. void tifm_unmap_sg(struct tifm_dev *sock, struct scatterlist *sg, int nents,
  249. int direction)
  250. {
  251. pci_unmap_sg(to_pci_dev(sock->dev.parent), sg, nents, direction);
  252. }
  253. EXPORT_SYMBOL(tifm_unmap_sg);
  254. void tifm_queue_work(struct work_struct *work)
  255. {
  256. queue_work(workqueue, work);
  257. }
  258. EXPORT_SYMBOL(tifm_queue_work);
  259. int tifm_register_driver(struct tifm_driver *drv)
  260. {
  261. drv->driver.bus = &tifm_bus_type;
  262. return driver_register(&drv->driver);
  263. }
  264. EXPORT_SYMBOL(tifm_register_driver);
  265. void tifm_unregister_driver(struct tifm_driver *drv)
  266. {
  267. driver_unregister(&drv->driver);
  268. }
  269. EXPORT_SYMBOL(tifm_unregister_driver);
  270. static int __init tifm_init(void)
  271. {
  272. int rc;
  273. workqueue = create_freezeable_workqueue("tifm");
  274. if (!workqueue)
  275. return -ENOMEM;
  276. rc = bus_register(&tifm_bus_type);
  277. if (rc)
  278. goto err_out_wq;
  279. rc = class_register(&tifm_adapter_class);
  280. if (!rc)
  281. return 0;
  282. bus_unregister(&tifm_bus_type);
  283. err_out_wq:
  284. destroy_workqueue(workqueue);
  285. return rc;
  286. }
  287. static void __exit tifm_exit(void)
  288. {
  289. class_unregister(&tifm_adapter_class);
  290. bus_unregister(&tifm_bus_type);
  291. destroy_workqueue(workqueue);
  292. }
  293. subsys_initcall(tifm_init);
  294. module_exit(tifm_exit);
  295. MODULE_LICENSE("GPL");
  296. MODULE_AUTHOR("Alex Dubov");
  297. MODULE_DESCRIPTION("TI FlashMedia core driver");
  298. MODULE_LICENSE("GPL");
  299. MODULE_VERSION(DRIVER_VERSION);