card.c 9.5 KB

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