card.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. /*
  2. * card.c - contains functions for managing groups of PnP devices
  3. *
  4. * Copyright 2002 Adam Belay <ambx1@neo.rr.com>
  5. */
  6. #include <linux/module.h>
  7. #include <linux/ctype.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(dev->id,
  29. 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. struct pnp_id *pnp_add_card_id(struct pnp_card *card, char *id)
  90. {
  91. struct pnp_id *dev_id, *ptr;
  92. dev_id = kzalloc(sizeof(struct pnp_id), GFP_KERNEL);
  93. if (!dev_id)
  94. return NULL;
  95. dev_id->id[0] = id[0];
  96. dev_id->id[1] = id[1];
  97. dev_id->id[2] = id[2];
  98. dev_id->id[3] = tolower(id[3]);
  99. dev_id->id[4] = tolower(id[4]);
  100. dev_id->id[5] = tolower(id[5]);
  101. dev_id->id[6] = tolower(id[6]);
  102. dev_id->id[7] = '\0';
  103. dev_id->next = NULL;
  104. ptr = card->id;
  105. while (ptr && ptr->next)
  106. ptr = ptr->next;
  107. if (ptr)
  108. ptr->next = dev_id;
  109. else
  110. card->id = dev_id;
  111. return dev_id;
  112. }
  113. static void pnp_free_card_ids(struct pnp_card *card)
  114. {
  115. struct pnp_id *id;
  116. struct pnp_id *next;
  117. id = card->id;
  118. while (id) {
  119. next = id->next;
  120. kfree(id);
  121. id = next;
  122. }
  123. }
  124. static void pnp_release_card(struct device *dmdev)
  125. {
  126. struct pnp_card *card = to_pnp_card(dmdev);
  127. pnp_free_card_ids(card);
  128. kfree(card);
  129. }
  130. static ssize_t pnp_show_card_name(struct device *dmdev,
  131. struct device_attribute *attr, char *buf)
  132. {
  133. char *str = buf;
  134. struct pnp_card *card = to_pnp_card(dmdev);
  135. str += sprintf(str, "%s\n", card->name);
  136. return (str - buf);
  137. }
  138. static DEVICE_ATTR(name, S_IRUGO, pnp_show_card_name, NULL);
  139. static ssize_t pnp_show_card_ids(struct device *dmdev,
  140. struct device_attribute *attr, char *buf)
  141. {
  142. char *str = buf;
  143. struct pnp_card *card = to_pnp_card(dmdev);
  144. struct pnp_id *pos = card->id;
  145. while (pos) {
  146. str += sprintf(str, "%s\n", pos->id);
  147. pos = pos->next;
  148. }
  149. return (str - buf);
  150. }
  151. static DEVICE_ATTR(card_id, S_IRUGO, pnp_show_card_ids, NULL);
  152. static int pnp_interface_attach_card(struct pnp_card *card)
  153. {
  154. int rc = device_create_file(&card->dev, &dev_attr_name);
  155. if (rc)
  156. return rc;
  157. rc = device_create_file(&card->dev, &dev_attr_card_id);
  158. if (rc)
  159. goto err_name;
  160. return 0;
  161. err_name:
  162. device_remove_file(&card->dev, &dev_attr_name);
  163. return rc;
  164. }
  165. /**
  166. * pnp_add_card - adds a PnP card to the PnP Layer
  167. * @card: pointer to the card to add
  168. */
  169. int pnp_add_card(struct pnp_card *card)
  170. {
  171. int error;
  172. struct list_head *pos, *temp;
  173. sprintf(card->dev.bus_id, "%02x:%02x", card->protocol->number,
  174. card->number);
  175. card->dev.parent = &card->protocol->dev;
  176. card->dev.bus = NULL;
  177. card->dev.release = &pnp_release_card;
  178. error = device_register(&card->dev);
  179. if (error) {
  180. dev_err(&card->dev, "could not register (err=%d)\n", error);
  181. return error;
  182. }
  183. pnp_interface_attach_card(card);
  184. spin_lock(&pnp_lock);
  185. list_add_tail(&card->global_list, &pnp_cards);
  186. list_add_tail(&card->protocol_list, &card->protocol->cards);
  187. spin_unlock(&pnp_lock);
  188. /* we wait until now to add devices in order to ensure the drivers
  189. * will be able to use all of the related devices on the card
  190. * without waiting an unreasonable length of time */
  191. list_for_each(pos, &card->devices) {
  192. struct pnp_dev *dev = card_to_pnp_dev(pos);
  193. __pnp_add_device(dev);
  194. }
  195. /* match with card drivers */
  196. list_for_each_safe(pos, temp, &pnp_card_drivers) {
  197. struct pnp_card_driver *drv =
  198. list_entry(pos, struct pnp_card_driver,
  199. global_list);
  200. card_probe(card, drv);
  201. }
  202. return 0;
  203. }
  204. /**
  205. * pnp_remove_card - removes a PnP card from the PnP Layer
  206. * @card: pointer to the card to remove
  207. */
  208. void pnp_remove_card(struct pnp_card *card)
  209. {
  210. struct list_head *pos, *temp;
  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. dev->dev.parent = &card->dev;
  229. dev->card_link = NULL;
  230. snprintf(dev->dev.bus_id, BUS_ID_SIZE, "%02x:%02x.%02x",
  231. dev->protocol->number, card->number, dev->number);
  232. spin_lock(&pnp_lock);
  233. dev->card = card;
  234. list_add_tail(&dev->card_list, &card->devices);
  235. spin_unlock(&pnp_lock);
  236. return 0;
  237. }
  238. /**
  239. * pnp_remove_card_device- removes a device from the specified card
  240. * @dev: pointer to the device to remove
  241. */
  242. void pnp_remove_card_device(struct pnp_dev *dev)
  243. {
  244. spin_lock(&pnp_lock);
  245. dev->card = NULL;
  246. list_del(&dev->card_list);
  247. spin_unlock(&pnp_lock);
  248. __pnp_remove_device(dev);
  249. }
  250. /**
  251. * pnp_request_card_device - Searches for a PnP device under the specified card
  252. * @clink: pointer to the card link, cannot be NULL
  253. * @id: pointer to a PnP ID structure that explains the rules for finding the device
  254. * @from: Starting place to search from. If NULL it will start from the begining.
  255. */
  256. struct pnp_dev *pnp_request_card_device(struct pnp_card_link *clink,
  257. const char *id, struct pnp_dev *from)
  258. {
  259. struct list_head *pos;
  260. struct pnp_dev *dev;
  261. struct pnp_card_driver *drv;
  262. struct pnp_card *card;
  263. if (!clink || !id)
  264. return NULL;
  265. card = clink->card;
  266. drv = clink->driver;
  267. if (!from) {
  268. pos = card->devices.next;
  269. } else {
  270. if (from->card != card)
  271. return NULL;
  272. pos = from->card_list.next;
  273. }
  274. while (pos != &card->devices) {
  275. dev = card_to_pnp_dev(pos);
  276. if ((!dev->card_link) && compare_pnp_id(dev->id, id))
  277. goto found;
  278. pos = pos->next;
  279. }
  280. return NULL;
  281. found:
  282. dev->card_link = clink;
  283. dev->dev.driver = &drv->link.driver;
  284. if (pnp_bus_type.probe(&dev->dev))
  285. goto err_out;
  286. if (device_bind_driver(&dev->dev))
  287. goto err_out;
  288. return dev;
  289. err_out:
  290. dev->dev.driver = NULL;
  291. dev->card_link = NULL;
  292. return NULL;
  293. }
  294. /**
  295. * pnp_release_card_device - call this when the driver no longer needs the device
  296. * @dev: pointer to the PnP device stucture
  297. */
  298. void pnp_release_card_device(struct pnp_dev *dev)
  299. {
  300. struct pnp_card_driver *drv = dev->card_link->driver;
  301. drv->link.remove = &card_remove;
  302. device_release_driver(&dev->dev);
  303. drv->link.remove = &card_remove_first;
  304. }
  305. /*
  306. * suspend/resume callbacks
  307. */
  308. static int card_suspend(struct pnp_dev *dev, pm_message_t state)
  309. {
  310. struct pnp_card_link *link = dev->card_link;
  311. if (link->pm_state.event == state.event)
  312. return 0;
  313. link->pm_state = state;
  314. return link->driver->suspend(link, state);
  315. }
  316. static int card_resume(struct pnp_dev *dev)
  317. {
  318. struct pnp_card_link *link = dev->card_link;
  319. if (link->pm_state.event == PM_EVENT_ON)
  320. return 0;
  321. link->pm_state = PMSG_ON;
  322. link->driver->resume(link);
  323. return 0;
  324. }
  325. /**
  326. * pnp_register_card_driver - registers a PnP card driver with the PnP Layer
  327. * @drv: pointer to the driver to register
  328. */
  329. int pnp_register_card_driver(struct pnp_card_driver *drv)
  330. {
  331. int error;
  332. struct list_head *pos, *temp;
  333. drv->link.name = drv->name;
  334. drv->link.id_table = NULL; /* this will disable auto matching */
  335. drv->link.flags = drv->flags;
  336. drv->link.probe = NULL;
  337. drv->link.remove = &card_remove_first;
  338. drv->link.suspend = drv->suspend ? card_suspend : NULL;
  339. drv->link.resume = drv->resume ? card_resume : NULL;
  340. error = pnp_register_driver(&drv->link);
  341. if (error < 0)
  342. return error;
  343. spin_lock(&pnp_lock);
  344. list_add_tail(&drv->global_list, &pnp_card_drivers);
  345. spin_unlock(&pnp_lock);
  346. list_for_each_safe(pos, temp, &pnp_cards) {
  347. struct pnp_card *card =
  348. list_entry(pos, struct pnp_card, global_list);
  349. card_probe(card, drv);
  350. }
  351. return 0;
  352. }
  353. /**
  354. * pnp_unregister_card_driver - unregisters a PnP card driver from the PnP Layer
  355. * @drv: pointer to the driver to unregister
  356. */
  357. void pnp_unregister_card_driver(struct pnp_card_driver *drv)
  358. {
  359. spin_lock(&pnp_lock);
  360. list_del(&drv->global_list);
  361. spin_unlock(&pnp_lock);
  362. pnp_unregister_driver(&drv->link);
  363. }
  364. EXPORT_SYMBOL(pnp_request_card_device);
  365. EXPORT_SYMBOL(pnp_release_card_device);
  366. EXPORT_SYMBOL(pnp_register_card_driver);
  367. EXPORT_SYMBOL(pnp_unregister_card_driver);