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