card.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * card.c - contains functions for managing groups of PnP devices
  4. *
  5. * Copyright 2002 Adam Belay <ambx1@neo.rr.com>
  6. */
  7. #include <linux/module.h>
  8. #include <linux/mutex.h>
  9. #include <linux/ctype.h>
  10. #include <linux/slab.h>
  11. #include <linux/pnp.h>
  12. #include <linux/dma-mapping.h>
  13. #include "base.h"
  14. LIST_HEAD(pnp_cards);
  15. static LIST_HEAD(pnp_card_drivers);
  16. static const struct pnp_card_device_id *match_card(struct pnp_card_driver *drv,
  17. struct pnp_card *card)
  18. {
  19. const struct pnp_card_device_id *drv_id = drv->id_table;
  20. while (*drv_id->id) {
  21. if (compare_pnp_id(card->id, drv_id->id)) {
  22. int i = 0;
  23. for (;;) {
  24. int found;
  25. struct pnp_dev *dev;
  26. if (i == PNP_MAX_DEVICES ||
  27. !*drv_id->devs[i].id)
  28. return drv_id;
  29. found = 0;
  30. card_for_each_dev(card, dev) {
  31. if (compare_pnp_id(dev->id,
  32. drv_id->devs[i].id)) {
  33. found = 1;
  34. break;
  35. }
  36. }
  37. if (!found)
  38. break;
  39. i++;
  40. }
  41. }
  42. drv_id++;
  43. }
  44. return NULL;
  45. }
  46. static void card_remove(struct pnp_dev *dev)
  47. {
  48. dev->card_link = NULL;
  49. }
  50. static void card_remove_first(struct pnp_dev *dev)
  51. {
  52. struct pnp_card_driver *drv = to_pnp_card_driver(dev->driver);
  53. if (!dev->card || !drv)
  54. return;
  55. if (drv->remove)
  56. drv->remove(dev->card_link);
  57. drv->link.remove = &card_remove;
  58. kfree(dev->card_link);
  59. card_remove(dev);
  60. }
  61. static int card_probe(struct pnp_card *card, struct pnp_card_driver *drv)
  62. {
  63. const struct pnp_card_device_id *id;
  64. struct pnp_card_link *clink;
  65. struct pnp_dev *dev;
  66. if (!drv->probe)
  67. return 0;
  68. id = match_card(drv, card);
  69. if (!id)
  70. return 0;
  71. clink = kzalloc_obj(*clink);
  72. if (!clink)
  73. return 0;
  74. clink->card = card;
  75. clink->driver = drv;
  76. clink->pm_state = PMSG_ON;
  77. if (drv->probe(clink, id) >= 0)
  78. return 1;
  79. /* Recovery */
  80. card_for_each_dev(card, dev) {
  81. if (dev->card_link == clink)
  82. pnp_release_card_device(dev);
  83. }
  84. kfree(clink);
  85. return 0;
  86. }
  87. /**
  88. * pnp_add_card_id - adds an EISA id to the specified card
  89. * @id: pointer to a pnp_id structure
  90. * @card: pointer to the desired card
  91. */
  92. static struct pnp_id *pnp_add_card_id(struct pnp_card *card, char *id)
  93. {
  94. struct pnp_id *dev_id, *ptr;
  95. dev_id = kzalloc_obj(struct pnp_id);
  96. if (!dev_id)
  97. return NULL;
  98. dev_id->id[0] = id[0];
  99. dev_id->id[1] = id[1];
  100. dev_id->id[2] = id[2];
  101. dev_id->id[3] = tolower(id[3]);
  102. dev_id->id[4] = tolower(id[4]);
  103. dev_id->id[5] = tolower(id[5]);
  104. dev_id->id[6] = tolower(id[6]);
  105. dev_id->id[7] = '\0';
  106. dev_id->next = NULL;
  107. ptr = card->id;
  108. while (ptr && ptr->next)
  109. ptr = ptr->next;
  110. if (ptr)
  111. ptr->next = dev_id;
  112. else
  113. card->id = dev_id;
  114. return dev_id;
  115. }
  116. static void pnp_free_card_ids(struct pnp_card *card)
  117. {
  118. struct pnp_id *id;
  119. struct pnp_id *next;
  120. id = card->id;
  121. while (id) {
  122. next = id->next;
  123. kfree(id);
  124. id = next;
  125. }
  126. }
  127. static void pnp_release_card(struct device *dmdev)
  128. {
  129. struct pnp_card *card = to_pnp_card(dmdev);
  130. pnp_free_card_ids(card);
  131. kfree(card);
  132. }
  133. struct pnp_card *pnp_alloc_card(struct pnp_protocol *protocol, int id, char *pnpid)
  134. {
  135. struct pnp_card *card;
  136. struct pnp_id *dev_id;
  137. card = kzalloc_obj(struct pnp_card);
  138. if (!card)
  139. return NULL;
  140. card->protocol = protocol;
  141. card->number = id;
  142. card->dev.parent = &card->protocol->dev;
  143. dev_set_name(&card->dev, "%02x:%02x", card->protocol->number, card->number);
  144. card->dev.coherent_dma_mask = DMA_BIT_MASK(24);
  145. card->dev.dma_mask = &card->dev.coherent_dma_mask;
  146. dev_id = pnp_add_card_id(card, pnpid);
  147. if (!dev_id) {
  148. kfree(card);
  149. return NULL;
  150. }
  151. return card;
  152. }
  153. static ssize_t name_show(struct device *dmdev,
  154. struct device_attribute *attr, char *buf)
  155. {
  156. char *str = buf;
  157. struct pnp_card *card = to_pnp_card(dmdev);
  158. str += sprintf(str, "%s\n", card->name);
  159. return (str - buf);
  160. }
  161. static DEVICE_ATTR_RO(name);
  162. static ssize_t card_id_show(struct device *dmdev,
  163. struct device_attribute *attr, char *buf)
  164. {
  165. char *str = buf;
  166. struct pnp_card *card = to_pnp_card(dmdev);
  167. struct pnp_id *pos = card->id;
  168. while (pos) {
  169. str += sprintf(str, "%s\n", pos->id);
  170. pos = pos->next;
  171. }
  172. return (str - buf);
  173. }
  174. static DEVICE_ATTR_RO(card_id);
  175. static int pnp_interface_attach_card(struct pnp_card *card)
  176. {
  177. int rc = device_create_file(&card->dev, &dev_attr_name);
  178. if (rc)
  179. return rc;
  180. rc = device_create_file(&card->dev, &dev_attr_card_id);
  181. if (rc)
  182. goto err_name;
  183. return 0;
  184. err_name:
  185. device_remove_file(&card->dev, &dev_attr_name);
  186. return rc;
  187. }
  188. /**
  189. * pnp_add_card - adds a PnP card to the PnP Layer
  190. * @card: pointer to the card to add
  191. */
  192. int pnp_add_card(struct pnp_card *card)
  193. {
  194. int error;
  195. struct list_head *pos, *temp;
  196. card->dev.bus = NULL;
  197. card->dev.release = &pnp_release_card;
  198. error = device_register(&card->dev);
  199. if (error) {
  200. dev_err(&card->dev, "could not register (err=%d)\n", error);
  201. put_device(&card->dev);
  202. return error;
  203. }
  204. pnp_interface_attach_card(card);
  205. mutex_lock(&pnp_lock);
  206. list_add_tail(&card->global_list, &pnp_cards);
  207. list_add_tail(&card->protocol_list, &card->protocol->cards);
  208. mutex_unlock(&pnp_lock);
  209. /* we wait until now to add devices in order to ensure the drivers
  210. * will be able to use all of the related devices on the card
  211. * without waiting an unreasonable length of time */
  212. list_for_each(pos, &card->devices) {
  213. struct pnp_dev *dev = card_to_pnp_dev(pos);
  214. __pnp_add_device(dev);
  215. }
  216. /* match with card drivers */
  217. list_for_each_safe(pos, temp, &pnp_card_drivers) {
  218. struct pnp_card_driver *drv =
  219. list_entry(pos, struct pnp_card_driver,
  220. global_list);
  221. card_probe(card, drv);
  222. }
  223. return 0;
  224. }
  225. /**
  226. * pnp_add_card_device - adds a device to the specified card
  227. * @card: pointer to the card to add to
  228. * @dev: pointer to the device to add
  229. */
  230. int pnp_add_card_device(struct pnp_card *card, struct pnp_dev *dev)
  231. {
  232. dev->dev.parent = &card->dev;
  233. dev->card_link = NULL;
  234. dev_set_name(&dev->dev, "%02x:%02x.%02x",
  235. dev->protocol->number, card->number, dev->number);
  236. mutex_lock(&pnp_lock);
  237. dev->card = card;
  238. list_add_tail(&dev->card_list, &card->devices);
  239. mutex_unlock(&pnp_lock);
  240. return 0;
  241. }
  242. /**
  243. * pnp_request_card_device - Searches for a PnP device under the specified card
  244. * @clink: pointer to the card link, cannot be NULL
  245. * @id: pointer to a PnP ID structure that explains the rules for finding the device
  246. * @from: Starting place to search from. If NULL it will start from the beginning.
  247. */
  248. struct pnp_dev *pnp_request_card_device(struct pnp_card_link *clink,
  249. const char *id, struct pnp_dev *from)
  250. {
  251. struct list_head *pos;
  252. struct pnp_dev *dev;
  253. struct pnp_card_driver *drv;
  254. struct pnp_card *card;
  255. if (!clink || !id)
  256. return NULL;
  257. card = clink->card;
  258. drv = clink->driver;
  259. if (!from) {
  260. pos = card->devices.next;
  261. } else {
  262. if (from->card != card)
  263. return NULL;
  264. pos = from->card_list.next;
  265. }
  266. while (pos != &card->devices) {
  267. dev = card_to_pnp_dev(pos);
  268. if ((!dev->card_link) && compare_pnp_id(dev->id, id))
  269. goto found;
  270. pos = pos->next;
  271. }
  272. return NULL;
  273. found:
  274. dev->card_link = clink;
  275. dev->dev.driver = &drv->link.driver;
  276. if (pnp_bus_type.probe(&dev->dev))
  277. goto err_out;
  278. if (device_bind_driver(&dev->dev))
  279. goto err_out;
  280. return dev;
  281. err_out:
  282. dev->dev.driver = NULL;
  283. dev->card_link = NULL;
  284. return NULL;
  285. }
  286. EXPORT_SYMBOL(pnp_request_card_device);
  287. /**
  288. * pnp_release_card_device - call this when the driver no longer needs the device
  289. * @dev: pointer to the PnP device structure
  290. */
  291. void pnp_release_card_device(struct pnp_dev *dev)
  292. {
  293. struct pnp_card_driver *drv = dev->card_link->driver;
  294. drv->link.remove = &card_remove;
  295. device_release_driver(&dev->dev);
  296. drv->link.remove = &card_remove_first;
  297. }
  298. EXPORT_SYMBOL(pnp_release_card_device);
  299. /*
  300. * suspend/resume callbacks
  301. */
  302. static int card_suspend(struct pnp_dev *dev, pm_message_t state)
  303. {
  304. struct pnp_card_link *link = dev->card_link;
  305. if (link->pm_state.event == state.event)
  306. return 0;
  307. link->pm_state = state;
  308. return link->driver->suspend(link, state);
  309. }
  310. static int card_resume(struct pnp_dev *dev)
  311. {
  312. struct pnp_card_link *link = dev->card_link;
  313. if (link->pm_state.event == PM_EVENT_ON)
  314. return 0;
  315. link->pm_state = PMSG_ON;
  316. link->driver->resume(link);
  317. return 0;
  318. }
  319. /**
  320. * pnp_register_card_driver - registers a PnP card driver with the PnP Layer
  321. * @drv: pointer to the driver to register
  322. */
  323. int pnp_register_card_driver(struct pnp_card_driver *drv)
  324. {
  325. int error;
  326. struct list_head *pos, *temp;
  327. drv->link.name = drv->name;
  328. drv->link.id_table = NULL; /* this will disable auto matching */
  329. drv->link.flags = drv->flags;
  330. drv->link.probe = NULL;
  331. drv->link.remove = &card_remove_first;
  332. drv->link.suspend = drv->suspend ? card_suspend : NULL;
  333. drv->link.resume = drv->resume ? card_resume : NULL;
  334. error = pnp_register_driver(&drv->link);
  335. if (error < 0)
  336. return error;
  337. mutex_lock(&pnp_lock);
  338. list_add_tail(&drv->global_list, &pnp_card_drivers);
  339. mutex_unlock(&pnp_lock);
  340. list_for_each_safe(pos, temp, &pnp_cards) {
  341. struct pnp_card *card =
  342. list_entry(pos, struct pnp_card, global_list);
  343. card_probe(card, drv);
  344. }
  345. return 0;
  346. }
  347. EXPORT_SYMBOL(pnp_register_card_driver);
  348. /**
  349. * pnp_unregister_card_driver - unregisters a PnP card driver from the PnP Layer
  350. * @drv: pointer to the driver to unregister
  351. */
  352. void pnp_unregister_card_driver(struct pnp_card_driver *drv)
  353. {
  354. mutex_lock(&pnp_lock);
  355. list_del(&drv->global_list);
  356. mutex_unlock(&pnp_lock);
  357. pnp_unregister_driver(&drv->link);
  358. }
  359. EXPORT_SYMBOL(pnp_unregister_card_driver);