card.c 10.0 KB

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