card.c 9.6 KB

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