card.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  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. struct pnp_card *pnp_alloc_card(struct pnp_protocol *protocol, int id, char *pnpid)
  131. {
  132. struct pnp_card *card;
  133. struct pnp_id *dev_id;
  134. card = kzalloc(sizeof(struct pnp_card), GFP_KERNEL);
  135. if (!card)
  136. return NULL;
  137. card->protocol = protocol;
  138. card->number = id;
  139. card->dev.parent = &card->protocol->dev;
  140. sprintf(card->dev.bus_id, "%02x:%02x", card->protocol->number,
  141. card->number);
  142. dev_id = pnp_add_card_id(card, pnpid);
  143. if (!dev_id) {
  144. kfree(card);
  145. return NULL;
  146. }
  147. return card;
  148. }
  149. static ssize_t pnp_show_card_name(struct device *dmdev,
  150. struct device_attribute *attr, char *buf)
  151. {
  152. char *str = buf;
  153. struct pnp_card *card = to_pnp_card(dmdev);
  154. str += sprintf(str, "%s\n", card->name);
  155. return (str - buf);
  156. }
  157. static DEVICE_ATTR(name, S_IRUGO, pnp_show_card_name, NULL);
  158. static ssize_t pnp_show_card_ids(struct device *dmdev,
  159. struct device_attribute *attr, char *buf)
  160. {
  161. char *str = buf;
  162. struct pnp_card *card = to_pnp_card(dmdev);
  163. struct pnp_id *pos = card->id;
  164. while (pos) {
  165. str += sprintf(str, "%s\n", pos->id);
  166. pos = pos->next;
  167. }
  168. return (str - buf);
  169. }
  170. static DEVICE_ATTR(card_id, S_IRUGO, pnp_show_card_ids, NULL);
  171. static int pnp_interface_attach_card(struct pnp_card *card)
  172. {
  173. int rc = device_create_file(&card->dev, &dev_attr_name);
  174. if (rc)
  175. return rc;
  176. rc = device_create_file(&card->dev, &dev_attr_card_id);
  177. if (rc)
  178. goto err_name;
  179. return 0;
  180. err_name:
  181. device_remove_file(&card->dev, &dev_attr_name);
  182. return rc;
  183. }
  184. /**
  185. * pnp_add_card - adds a PnP card to the PnP Layer
  186. * @card: pointer to the card to add
  187. */
  188. int pnp_add_card(struct pnp_card *card)
  189. {
  190. int error;
  191. struct list_head *pos, *temp;
  192. card->dev.bus = NULL;
  193. card->dev.release = &pnp_release_card;
  194. error = device_register(&card->dev);
  195. if (error) {
  196. dev_err(&card->dev, "could not register (err=%d)\n", error);
  197. return error;
  198. }
  199. pnp_interface_attach_card(card);
  200. spin_lock(&pnp_lock);
  201. list_add_tail(&card->global_list, &pnp_cards);
  202. list_add_tail(&card->protocol_list, &card->protocol->cards);
  203. spin_unlock(&pnp_lock);
  204. /* we wait until now to add devices in order to ensure the drivers
  205. * will be able to use all of the related devices on the card
  206. * without waiting an unreasonable length of time */
  207. list_for_each(pos, &card->devices) {
  208. struct pnp_dev *dev = card_to_pnp_dev(pos);
  209. __pnp_add_device(dev);
  210. }
  211. /* match with card drivers */
  212. list_for_each_safe(pos, temp, &pnp_card_drivers) {
  213. struct pnp_card_driver *drv =
  214. list_entry(pos, struct pnp_card_driver,
  215. global_list);
  216. card_probe(card, drv);
  217. }
  218. return 0;
  219. }
  220. /**
  221. * pnp_remove_card - removes a PnP card from the PnP Layer
  222. * @card: pointer to the card to remove
  223. */
  224. void pnp_remove_card(struct pnp_card *card)
  225. {
  226. struct list_head *pos, *temp;
  227. device_unregister(&card->dev);
  228. spin_lock(&pnp_lock);
  229. list_del(&card->global_list);
  230. list_del(&card->protocol_list);
  231. spin_unlock(&pnp_lock);
  232. list_for_each_safe(pos, temp, &card->devices) {
  233. struct pnp_dev *dev = card_to_pnp_dev(pos);
  234. pnp_remove_card_device(dev);
  235. }
  236. }
  237. /**
  238. * pnp_add_card_device - adds a device to the specified card
  239. * @card: pointer to the card to add to
  240. * @dev: pointer to the device to add
  241. */
  242. int pnp_add_card_device(struct pnp_card *card, struct pnp_dev *dev)
  243. {
  244. dev->dev.parent = &card->dev;
  245. dev->card_link = NULL;
  246. snprintf(dev->dev.bus_id, BUS_ID_SIZE, "%02x:%02x.%02x",
  247. dev->protocol->number, card->number, dev->number);
  248. spin_lock(&pnp_lock);
  249. dev->card = card;
  250. list_add_tail(&dev->card_list, &card->devices);
  251. spin_unlock(&pnp_lock);
  252. return 0;
  253. }
  254. /**
  255. * pnp_remove_card_device- removes a device from the specified card
  256. * @dev: pointer to the device to remove
  257. */
  258. void pnp_remove_card_device(struct pnp_dev *dev)
  259. {
  260. spin_lock(&pnp_lock);
  261. dev->card = NULL;
  262. list_del(&dev->card_list);
  263. spin_unlock(&pnp_lock);
  264. __pnp_remove_device(dev);
  265. }
  266. /**
  267. * pnp_request_card_device - Searches for a PnP device under the specified card
  268. * @clink: pointer to the card link, cannot be NULL
  269. * @id: pointer to a PnP ID structure that explains the rules for finding the device
  270. * @from: Starting place to search from. If NULL it will start from the begining.
  271. */
  272. struct pnp_dev *pnp_request_card_device(struct pnp_card_link *clink,
  273. const char *id, struct pnp_dev *from)
  274. {
  275. struct list_head *pos;
  276. struct pnp_dev *dev;
  277. struct pnp_card_driver *drv;
  278. struct pnp_card *card;
  279. if (!clink || !id)
  280. return NULL;
  281. card = clink->card;
  282. drv = clink->driver;
  283. if (!from) {
  284. pos = card->devices.next;
  285. } else {
  286. if (from->card != card)
  287. return NULL;
  288. pos = from->card_list.next;
  289. }
  290. while (pos != &card->devices) {
  291. dev = card_to_pnp_dev(pos);
  292. if ((!dev->card_link) && compare_pnp_id(dev->id, id))
  293. goto found;
  294. pos = pos->next;
  295. }
  296. return NULL;
  297. found:
  298. dev->card_link = clink;
  299. dev->dev.driver = &drv->link.driver;
  300. if (pnp_bus_type.probe(&dev->dev))
  301. goto err_out;
  302. if (device_bind_driver(&dev->dev))
  303. goto err_out;
  304. return dev;
  305. err_out:
  306. dev->dev.driver = NULL;
  307. dev->card_link = NULL;
  308. return NULL;
  309. }
  310. /**
  311. * pnp_release_card_device - call this when the driver no longer needs the device
  312. * @dev: pointer to the PnP device stucture
  313. */
  314. void pnp_release_card_device(struct pnp_dev *dev)
  315. {
  316. struct pnp_card_driver *drv = dev->card_link->driver;
  317. drv->link.remove = &card_remove;
  318. device_release_driver(&dev->dev);
  319. drv->link.remove = &card_remove_first;
  320. }
  321. /*
  322. * suspend/resume callbacks
  323. */
  324. static int card_suspend(struct pnp_dev *dev, pm_message_t state)
  325. {
  326. struct pnp_card_link *link = dev->card_link;
  327. if (link->pm_state.event == state.event)
  328. return 0;
  329. link->pm_state = state;
  330. return link->driver->suspend(link, state);
  331. }
  332. static int card_resume(struct pnp_dev *dev)
  333. {
  334. struct pnp_card_link *link = dev->card_link;
  335. if (link->pm_state.event == PM_EVENT_ON)
  336. return 0;
  337. link->pm_state = PMSG_ON;
  338. link->driver->resume(link);
  339. return 0;
  340. }
  341. /**
  342. * pnp_register_card_driver - registers a PnP card driver with the PnP Layer
  343. * @drv: pointer to the driver to register
  344. */
  345. int pnp_register_card_driver(struct pnp_card_driver *drv)
  346. {
  347. int error;
  348. struct list_head *pos, *temp;
  349. drv->link.name = drv->name;
  350. drv->link.id_table = NULL; /* this will disable auto matching */
  351. drv->link.flags = drv->flags;
  352. drv->link.probe = NULL;
  353. drv->link.remove = &card_remove_first;
  354. drv->link.suspend = drv->suspend ? card_suspend : NULL;
  355. drv->link.resume = drv->resume ? card_resume : NULL;
  356. error = pnp_register_driver(&drv->link);
  357. if (error < 0)
  358. return error;
  359. spin_lock(&pnp_lock);
  360. list_add_tail(&drv->global_list, &pnp_card_drivers);
  361. spin_unlock(&pnp_lock);
  362. list_for_each_safe(pos, temp, &pnp_cards) {
  363. struct pnp_card *card =
  364. list_entry(pos, struct pnp_card, global_list);
  365. card_probe(card, drv);
  366. }
  367. return 0;
  368. }
  369. /**
  370. * pnp_unregister_card_driver - unregisters a PnP card driver from the PnP Layer
  371. * @drv: pointer to the driver to unregister
  372. */
  373. void pnp_unregister_card_driver(struct pnp_card_driver *drv)
  374. {
  375. spin_lock(&pnp_lock);
  376. list_del(&drv->global_list);
  377. spin_unlock(&pnp_lock);
  378. pnp_unregister_driver(&drv->link);
  379. }
  380. EXPORT_SYMBOL(pnp_request_card_device);
  381. EXPORT_SYMBOL(pnp_release_card_device);
  382. EXPORT_SYMBOL(pnp_register_card_driver);
  383. EXPORT_SYMBOL(pnp_unregister_card_driver);