tifm_core.c 8.1 KB

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