driver.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. /*
  2. * driver.c - device id matching, driver model, etc.
  3. *
  4. * Copyright 2002 Adam Belay <ambx1@neo.rr.com>
  5. */
  6. #include <linux/string.h>
  7. #include <linux/list.h>
  8. #include <linux/module.h>
  9. #include <linux/ctype.h>
  10. #include <linux/slab.h>
  11. #include <linux/pnp.h>
  12. #include "base.h"
  13. static int compare_func(const char *ida, const char *idb)
  14. {
  15. int i;
  16. /* we only need to compare the last 4 chars */
  17. for (i = 3; i < 7; i++) {
  18. if (ida[i] != 'X' &&
  19. idb[i] != 'X' && toupper(ida[i]) != toupper(idb[i]))
  20. return 0;
  21. }
  22. return 1;
  23. }
  24. int compare_pnp_id(struct pnp_id *pos, const char *id)
  25. {
  26. if (!pos || !id || (strlen(id) != 7))
  27. return 0;
  28. if (memcmp(id, "ANYDEVS", 7) == 0)
  29. return 1;
  30. while (pos) {
  31. if (memcmp(pos->id, id, 3) == 0)
  32. if (compare_func(pos->id, id) == 1)
  33. return 1;
  34. pos = pos->next;
  35. }
  36. return 0;
  37. }
  38. static const struct pnp_device_id *match_device(struct pnp_driver *drv,
  39. struct pnp_dev *dev)
  40. {
  41. const struct pnp_device_id *drv_id = drv->id_table;
  42. if (!drv_id)
  43. return NULL;
  44. while (*drv_id->id) {
  45. if (compare_pnp_id(dev->id, drv_id->id))
  46. return drv_id;
  47. drv_id++;
  48. }
  49. return NULL;
  50. }
  51. int pnp_device_attach(struct pnp_dev *pnp_dev)
  52. {
  53. mutex_lock(&pnp_lock);
  54. if (pnp_dev->status != PNP_READY) {
  55. mutex_unlock(&pnp_lock);
  56. return -EBUSY;
  57. }
  58. pnp_dev->status = PNP_ATTACHED;
  59. mutex_unlock(&pnp_lock);
  60. return 0;
  61. }
  62. void pnp_device_detach(struct pnp_dev *pnp_dev)
  63. {
  64. mutex_lock(&pnp_lock);
  65. if (pnp_dev->status == PNP_ATTACHED)
  66. pnp_dev->status = PNP_READY;
  67. mutex_unlock(&pnp_lock);
  68. pnp_disable_dev(pnp_dev);
  69. }
  70. static int pnp_device_probe(struct device *dev)
  71. {
  72. int error;
  73. struct pnp_driver *pnp_drv;
  74. struct pnp_dev *pnp_dev;
  75. const struct pnp_device_id *dev_id = NULL;
  76. pnp_dev = to_pnp_dev(dev);
  77. pnp_drv = to_pnp_driver(dev->driver);
  78. error = pnp_device_attach(pnp_dev);
  79. if (error < 0)
  80. return error;
  81. if (pnp_dev->active == 0) {
  82. if (!(pnp_drv->flags & PNP_DRIVER_RES_DO_NOT_CHANGE)) {
  83. error = pnp_activate_dev(pnp_dev);
  84. if (error < 0)
  85. return error;
  86. }
  87. } else if ((pnp_drv->flags & PNP_DRIVER_RES_DISABLE)
  88. == PNP_DRIVER_RES_DISABLE) {
  89. error = pnp_disable_dev(pnp_dev);
  90. if (error < 0)
  91. return error;
  92. }
  93. error = 0;
  94. if (pnp_drv->probe) {
  95. dev_id = match_device(pnp_drv, pnp_dev);
  96. if (dev_id != NULL)
  97. error = pnp_drv->probe(pnp_dev, dev_id);
  98. }
  99. if (error >= 0) {
  100. pnp_dev->driver = pnp_drv;
  101. error = 0;
  102. } else
  103. goto fail;
  104. return error;
  105. fail:
  106. pnp_device_detach(pnp_dev);
  107. return error;
  108. }
  109. static int pnp_device_remove(struct device *dev)
  110. {
  111. struct pnp_dev *pnp_dev = to_pnp_dev(dev);
  112. struct pnp_driver *drv = pnp_dev->driver;
  113. if (drv) {
  114. if (drv->remove)
  115. drv->remove(pnp_dev);
  116. pnp_dev->driver = NULL;
  117. }
  118. pnp_device_detach(pnp_dev);
  119. return 0;
  120. }
  121. static void pnp_device_shutdown(struct device *dev)
  122. {
  123. struct pnp_dev *pnp_dev = to_pnp_dev(dev);
  124. struct pnp_driver *drv = pnp_dev->driver;
  125. if (drv && drv->shutdown)
  126. drv->shutdown(pnp_dev);
  127. }
  128. static int pnp_bus_match(struct device *dev, struct device_driver *drv)
  129. {
  130. struct pnp_dev *pnp_dev = to_pnp_dev(dev);
  131. struct pnp_driver *pnp_drv = to_pnp_driver(drv);
  132. if (match_device(pnp_drv, pnp_dev) == NULL)
  133. return 0;
  134. return 1;
  135. }
  136. static int __pnp_bus_suspend(struct device *dev, pm_message_t state)
  137. {
  138. struct pnp_dev *pnp_dev = to_pnp_dev(dev);
  139. struct pnp_driver *pnp_drv = pnp_dev->driver;
  140. int error;
  141. if (!pnp_drv)
  142. return 0;
  143. if (pnp_drv->driver.pm && pnp_drv->driver.pm->suspend) {
  144. error = pnp_drv->driver.pm->suspend(dev);
  145. suspend_report_result(pnp_drv->driver.pm->suspend, error);
  146. if (error)
  147. return error;
  148. }
  149. if (pnp_drv->suspend) {
  150. error = pnp_drv->suspend(pnp_dev, state);
  151. if (error)
  152. return error;
  153. }
  154. if (pnp_can_disable(pnp_dev)) {
  155. error = pnp_stop_dev(pnp_dev);
  156. if (error)
  157. return error;
  158. }
  159. if (pnp_can_suspend(pnp_dev))
  160. pnp_dev->protocol->suspend(pnp_dev, state);
  161. return 0;
  162. }
  163. static int pnp_bus_suspend(struct device *dev)
  164. {
  165. return __pnp_bus_suspend(dev, PMSG_SUSPEND);
  166. }
  167. static int pnp_bus_freeze(struct device *dev)
  168. {
  169. return __pnp_bus_suspend(dev, PMSG_FREEZE);
  170. }
  171. static int pnp_bus_poweroff(struct device *dev)
  172. {
  173. return __pnp_bus_suspend(dev, PMSG_HIBERNATE);
  174. }
  175. static int pnp_bus_resume(struct device *dev)
  176. {
  177. struct pnp_dev *pnp_dev = to_pnp_dev(dev);
  178. struct pnp_driver *pnp_drv = pnp_dev->driver;
  179. int error;
  180. if (!pnp_drv)
  181. return 0;
  182. if (pnp_dev->protocol->resume) {
  183. error = pnp_dev->protocol->resume(pnp_dev);
  184. if (error)
  185. return error;
  186. }
  187. if (pnp_can_write(pnp_dev)) {
  188. error = pnp_start_dev(pnp_dev);
  189. if (error)
  190. return error;
  191. }
  192. if (pnp_drv->driver.pm && pnp_drv->driver.pm->resume) {
  193. error = pnp_drv->driver.pm->resume(dev);
  194. if (error)
  195. return error;
  196. }
  197. if (pnp_drv->resume) {
  198. error = pnp_drv->resume(pnp_dev);
  199. if (error)
  200. return error;
  201. }
  202. return 0;
  203. }
  204. static const struct dev_pm_ops pnp_bus_dev_pm_ops = {
  205. /* Suspend callbacks */
  206. .suspend = pnp_bus_suspend,
  207. .resume = pnp_bus_resume,
  208. /* Hibernate callbacks */
  209. .freeze = pnp_bus_freeze,
  210. .thaw = pnp_bus_resume,
  211. .poweroff = pnp_bus_poweroff,
  212. .restore = pnp_bus_resume,
  213. };
  214. struct bus_type pnp_bus_type = {
  215. .name = "pnp",
  216. .match = pnp_bus_match,
  217. .probe = pnp_device_probe,
  218. .remove = pnp_device_remove,
  219. .shutdown = pnp_device_shutdown,
  220. .pm = &pnp_bus_dev_pm_ops,
  221. .dev_groups = pnp_dev_groups,
  222. };
  223. int pnp_register_driver(struct pnp_driver *drv)
  224. {
  225. drv->driver.name = drv->name;
  226. drv->driver.bus = &pnp_bus_type;
  227. return driver_register(&drv->driver);
  228. }
  229. void pnp_unregister_driver(struct pnp_driver *drv)
  230. {
  231. driver_unregister(&drv->driver);
  232. }
  233. /**
  234. * pnp_add_id - adds an EISA id to the specified device
  235. * @dev: pointer to the desired device
  236. * @id: pointer to an EISA id string
  237. */
  238. struct pnp_id *pnp_add_id(struct pnp_dev *dev, const char *id)
  239. {
  240. struct pnp_id *dev_id, *ptr;
  241. dev_id = kzalloc(sizeof(struct pnp_id), GFP_KERNEL);
  242. if (!dev_id)
  243. return NULL;
  244. dev_id->id[0] = id[0];
  245. dev_id->id[1] = id[1];
  246. dev_id->id[2] = id[2];
  247. dev_id->id[3] = tolower(id[3]);
  248. dev_id->id[4] = tolower(id[4]);
  249. dev_id->id[5] = tolower(id[5]);
  250. dev_id->id[6] = tolower(id[6]);
  251. dev_id->id[7] = '\0';
  252. dev_id->next = NULL;
  253. ptr = dev->id;
  254. while (ptr && ptr->next)
  255. ptr = ptr->next;
  256. if (ptr)
  257. ptr->next = dev_id;
  258. else
  259. dev->id = dev_id;
  260. return dev_id;
  261. }
  262. EXPORT_SYMBOL(pnp_register_driver);
  263. EXPORT_SYMBOL(pnp_unregister_driver);
  264. EXPORT_SYMBOL(pnp_device_attach);
  265. EXPORT_SYMBOL(pnp_device_detach);