tifm_core.c 8.2 KB

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