PageRenderTime 57ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 1ms

/drivers/pnp/card.c

https://github.com/andikleen/linux-misc
C | 458 lines | 334 code | 70 blank | 54 comment | 44 complexity | fd21d38d9314268e17b85cd07e84b994 MD5 | raw file
  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 = pnp_alloc(sizeof(*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(sizeof(struct pnp_id), GFP_KERNEL);
  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(sizeof(struct pnp_card), GFP_KERNEL);
  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 pnp_show_card_name(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(name, S_IRUGO, pnp_show_card_name, NULL);
  162. static ssize_t pnp_show_card_ids(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(card_id, S_IRUGO, pnp_show_card_ids, NULL);
  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_remove_card - removes a PnP card from the PnP Layer
  227. * @card: pointer to the card to remove
  228. */
  229. void pnp_remove_card(struct pnp_card *card)
  230. {
  231. struct list_head *pos, *temp;
  232. device_unregister(&card->dev);
  233. mutex_lock(&pnp_lock);
  234. list_del(&card->global_list);
  235. list_del(&card->protocol_list);
  236. mutex_unlock(&pnp_lock);
  237. list_for_each_safe(pos, temp, &card->devices) {
  238. struct pnp_dev *dev = card_to_pnp_dev(pos);
  239. pnp_remove_card_device(dev);
  240. }
  241. }
  242. /**
  243. * pnp_add_card_device - adds a device to the specified card
  244. * @card: pointer to the card to add to
  245. * @dev: pointer to the device to add
  246. */
  247. int pnp_add_card_device(struct pnp_card *card, struct pnp_dev *dev)
  248. {
  249. dev->dev.parent = &card->dev;
  250. dev->card_link = NULL;
  251. dev_set_name(&dev->dev, "%02x:%02x.%02x",
  252. dev->protocol->number, card->number, dev->number);
  253. mutex_lock(&pnp_lock);
  254. dev->card = card;
  255. list_add_tail(&dev->card_list, &card->devices);
  256. mutex_unlock(&pnp_lock);
  257. return 0;
  258. }
  259. /**
  260. * pnp_remove_card_device- removes a device from the specified card
  261. * @dev: pointer to the device to remove
  262. */
  263. void pnp_remove_card_device(struct pnp_dev *dev)
  264. {
  265. mutex_lock(&pnp_lock);
  266. dev->card = NULL;
  267. list_del(&dev->card_list);
  268. mutex_unlock(&pnp_lock);
  269. __pnp_remove_device(dev);
  270. }
  271. /**
  272. * pnp_request_card_device - Searches for a PnP device under the specified card
  273. * @clink: pointer to the card link, cannot be NULL
  274. * @id: pointer to a PnP ID structure that explains the rules for finding the device
  275. * @from: Starting place to search from. If NULL it will start from the beginning.
  276. */
  277. struct pnp_dev *pnp_request_card_device(struct pnp_card_link *clink,
  278. const char *id, struct pnp_dev *from)
  279. {
  280. struct list_head *pos;
  281. struct pnp_dev *dev;
  282. struct pnp_card_driver *drv;
  283. struct pnp_card *card;
  284. if (!clink || !id)
  285. return NULL;
  286. card = clink->card;
  287. drv = clink->driver;
  288. if (!from) {
  289. pos = card->devices.next;
  290. } else {
  291. if (from->card != card)
  292. return NULL;
  293. pos = from->card_list.next;
  294. }
  295. while (pos != &card->devices) {
  296. dev = card_to_pnp_dev(pos);
  297. if ((!dev->card_link) && compare_pnp_id(dev->id, id))
  298. goto found;
  299. pos = pos->next;
  300. }
  301. return NULL;
  302. found:
  303. dev->card_link = clink;
  304. dev->dev.driver = &drv->link.driver;
  305. if (pnp_bus_type.probe(&dev->dev))
  306. goto err_out;
  307. if (device_bind_driver(&dev->dev))
  308. goto err_out;
  309. return dev;
  310. err_out:
  311. dev->dev.driver = NULL;
  312. dev->card_link = NULL;
  313. return NULL;
  314. }
  315. /**
  316. * pnp_release_card_device - call this when the driver no longer needs the device
  317. * @dev: pointer to the PnP device structure
  318. */
  319. void pnp_release_card_device(struct pnp_dev *dev)
  320. {
  321. struct pnp_card_driver *drv = dev->card_link->driver;
  322. drv->link.remove = &card_remove;
  323. device_release_driver(&dev->dev);
  324. drv->link.remove = &card_remove_first;
  325. }
  326. /*
  327. * suspend/resume callbacks
  328. */
  329. static int card_suspend(struct pnp_dev *dev, pm_message_t state)
  330. {
  331. struct pnp_card_link *link = dev->card_link;
  332. if (link->pm_state.event == state.event)
  333. return 0;
  334. link->pm_state = state;
  335. return link->driver->suspend(link, state);
  336. }
  337. static int card_resume(struct pnp_dev *dev)
  338. {
  339. struct pnp_card_link *link = dev->card_link;
  340. if (link->pm_state.event == PM_EVENT_ON)
  341. return 0;
  342. link->pm_state = PMSG_ON;
  343. link->driver->resume(link);
  344. return 0;
  345. }
  346. /**
  347. * pnp_register_card_driver - registers a PnP card driver with the PnP Layer
  348. * @drv: pointer to the driver to register
  349. */
  350. int pnp_register_card_driver(struct pnp_card_driver *drv)
  351. {
  352. int error;
  353. struct list_head *pos, *temp;
  354. drv->link.name = drv->name;
  355. drv->link.id_table = NULL; /* this will disable auto matching */
  356. drv->link.flags = drv->flags;
  357. drv->link.probe = NULL;
  358. drv->link.remove = &card_remove_first;
  359. drv->link.suspend = drv->suspend ? card_suspend : NULL;
  360. drv->link.resume = drv->resume ? card_resume : NULL;
  361. error = pnp_register_driver(&drv->link);
  362. if (error < 0)
  363. return error;
  364. mutex_lock(&pnp_lock);
  365. list_add_tail(&drv->global_list, &pnp_card_drivers);
  366. mutex_unlock(&pnp_lock);
  367. list_for_each_safe(pos, temp, &pnp_cards) {
  368. struct pnp_card *card =
  369. list_entry(pos, struct pnp_card, global_list);
  370. card_probe(card, drv);
  371. }
  372. return 0;
  373. }
  374. /**
  375. * pnp_unregister_card_driver - unregisters a PnP card driver from the PnP Layer
  376. * @drv: pointer to the driver to unregister
  377. */
  378. void pnp_unregister_card_driver(struct pnp_card_driver *drv)
  379. {
  380. mutex_lock(&pnp_lock);
  381. list_del(&drv->global_list);
  382. mutex_unlock(&pnp_lock);
  383. pnp_unregister_driver(&drv->link);
  384. }
  385. EXPORT_SYMBOL(pnp_request_card_device);
  386. EXPORT_SYMBOL(pnp_release_card_device);
  387. EXPORT_SYMBOL(pnp_register_card_driver);
  388. EXPORT_SYMBOL(pnp_unregister_card_driver);