driver.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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. }
  69. static int pnp_device_probe(struct device *dev)
  70. {
  71. int error;
  72. struct pnp_driver *pnp_drv;
  73. struct pnp_dev *pnp_dev;
  74. const struct pnp_device_id *dev_id = NULL;
  75. pnp_dev = to_pnp_dev(dev);
  76. pnp_drv = to_pnp_driver(dev->driver);
  77. error = pnp_device_attach(pnp_dev);
  78. if (error < 0)
  79. return error;
  80. if (pnp_dev->active == 0) {
  81. if (!(pnp_drv->flags & PNP_DRIVER_RES_DO_NOT_CHANGE)) {
  82. error = pnp_activate_dev(pnp_dev);
  83. if (error < 0)
  84. return error;
  85. }
  86. } else if ((pnp_drv->flags & PNP_DRIVER_RES_DISABLE)
  87. == PNP_DRIVER_RES_DISABLE) {
  88. error = pnp_disable_dev(pnp_dev);
  89. if (error < 0)
  90. return error;
  91. }
  92. error = 0;
  93. if (pnp_drv->probe) {
  94. dev_id = match_device(pnp_drv, pnp_dev);
  95. if (dev_id != NULL)
  96. error = pnp_drv->probe(pnp_dev, dev_id);
  97. }
  98. if (error >= 0) {
  99. pnp_dev->driver = pnp_drv;
  100. error = 0;
  101. } else
  102. goto fail;
  103. return error;
  104. fail:
  105. pnp_device_detach(pnp_dev);
  106. return error;
  107. }
  108. static int pnp_device_remove(struct device *dev)
  109. {
  110. struct pnp_dev *pnp_dev = to_pnp_dev(dev);
  111. struct pnp_driver *drv = pnp_dev->driver;
  112. if (drv) {
  113. if (drv->remove)
  114. drv->remove(pnp_dev);
  115. pnp_dev->driver = NULL;
  116. }
  117. if (pnp_dev->active &&
  118. (!drv || !(drv->flags & PNP_DRIVER_RES_DO_NOT_CHANGE)))
  119. pnp_disable_dev(pnp_dev);
  120. pnp_device_detach(pnp_dev);
  121. return 0;
  122. }
  123. static void pnp_device_shutdown(struct device *dev)
  124. {
  125. struct pnp_dev *pnp_dev = to_pnp_dev(dev);
  126. struct pnp_driver *drv = pnp_dev->driver;
  127. if (drv && drv->shutdown)
  128. drv->shutdown(pnp_dev);
  129. }
  130. static int pnp_bus_match(struct device *dev, struct device_driver *drv)
  131. {
  132. struct pnp_dev *pnp_dev = to_pnp_dev(dev);
  133. struct pnp_driver *pnp_drv = to_pnp_driver(drv);
  134. if (match_device(pnp_drv, pnp_dev) == NULL)
  135. return 0;
  136. return 1;
  137. }
  138. static int __pnp_bus_suspend(struct device *dev, pm_message_t state)
  139. {
  140. struct pnp_dev *pnp_dev = to_pnp_dev(dev);
  141. struct pnp_driver *pnp_drv = pnp_dev->driver;
  142. int error;
  143. if (!pnp_drv)
  144. return 0;
  145. if (pnp_drv->driver.pm && pnp_drv->driver.pm->suspend) {
  146. error = pnp_drv->driver.pm->suspend(dev);
  147. suspend_report_result(pnp_drv->driver.pm->suspend, error);
  148. if (error)
  149. return error;
  150. }
  151. if (pnp_drv->suspend) {
  152. error = pnp_drv->suspend(pnp_dev, state);
  153. if (error)
  154. return error;
  155. }
  156. if (pnp_can_disable(pnp_dev)) {
  157. error = pnp_stop_dev(pnp_dev);
  158. if (error)
  159. return error;
  160. }
  161. if (pnp_can_suspend(pnp_dev))
  162. pnp_dev->protocol->suspend(pnp_dev, state);
  163. return 0;
  164. }
  165. static int pnp_bus_suspend(struct device *dev)
  166. {
  167. return __pnp_bus_suspend(dev, PMSG_SUSPEND);
  168. }
  169. static int pnp_bus_freeze(struct device *dev)
  170. {
  171. return __pnp_bus_suspend(dev, PMSG_FREEZE);
  172. }
  173. static int pnp_bus_poweroff(struct device *dev)
  174. {
  175. return __pnp_bus_suspend(dev, PMSG_HIBERNATE);
  176. }
  177. static int pnp_bus_resume(struct device *dev)
  178. {
  179. struct pnp_dev *pnp_dev = to_pnp_dev(dev);
  180. struct pnp_driver *pnp_drv = pnp_dev->driver;
  181. int error;
  182. if (!pnp_drv)
  183. return 0;
  184. if (pnp_dev->protocol->resume) {
  185. error = pnp_dev->protocol->resume(pnp_dev);
  186. if (error)
  187. return error;
  188. }
  189. if (pnp_can_write(pnp_dev)) {
  190. error = pnp_start_dev(pnp_dev);
  191. if (error)
  192. return error;
  193. }
  194. if (pnp_drv->driver.pm && pnp_drv->driver.pm->resume) {
  195. error = pnp_drv->driver.pm->resume(dev);
  196. if (error)
  197. return error;
  198. }
  199. if (pnp_drv->resume) {
  200. error = pnp_drv->resume(pnp_dev);
  201. if (error)
  202. return error;
  203. }
  204. return 0;
  205. }
  206. static const struct dev_pm_ops pnp_bus_dev_pm_ops = {
  207. /* Suspend callbacks */
  208. .suspend = pnp_bus_suspend,
  209. .resume = pnp_bus_resume,
  210. /* Hibernate callbacks */
  211. .freeze = pnp_bus_freeze,
  212. .thaw = pnp_bus_resume,
  213. .poweroff = pnp_bus_poweroff,
  214. .restore = pnp_bus_resume,
  215. };
  216. struct bus_type pnp_bus_type = {
  217. .name = "pnp",
  218. .match = pnp_bus_match,
  219. .probe = pnp_device_probe,
  220. .remove = pnp_device_remove,
  221. .shutdown = pnp_device_shutdown,
  222. .pm = &pnp_bus_dev_pm_ops,
  223. .dev_groups = pnp_dev_groups,
  224. };
  225. int pnp_register_driver(struct pnp_driver *drv)
  226. {
  227. drv->driver.name = drv->name;
  228. drv->driver.bus = &pnp_bus_type;
  229. return driver_register(&drv->driver);
  230. }
  231. void pnp_unregister_driver(struct pnp_driver *drv)
  232. {
  233. driver_unregister(&drv->driver);
  234. }
  235. /**
  236. * pnp_add_id - adds an EISA id to the specified device
  237. * @dev: pointer to the desired device
  238. * @id: pointer to an EISA id string
  239. */
  240. struct pnp_id *pnp_add_id(struct pnp_dev *dev, const char *id)
  241. {
  242. struct pnp_id *dev_id, *ptr;
  243. dev_id = kzalloc(sizeof(struct pnp_id), GFP_KERNEL);
  244. if (!dev_id)
  245. return NULL;
  246. dev_id->id[0] = id[0];
  247. dev_id->id[1] = id[1];
  248. dev_id->id[2] = id[2];
  249. dev_id->id[3] = tolower(id[3]);
  250. dev_id->id[4] = tolower(id[4]);
  251. dev_id->id[5] = tolower(id[5]);
  252. dev_id->id[6] = tolower(id[6]);
  253. dev_id->id[7] = '\0';
  254. dev_id->next = NULL;
  255. ptr = dev->id;
  256. while (ptr && ptr->next)
  257. ptr = ptr->next;
  258. if (ptr)
  259. ptr->next = dev_id;
  260. else
  261. dev->id = dev_id;
  262. return dev_id;
  263. }
  264. EXPORT_SYMBOL(pnp_register_driver);
  265. EXPORT_SYMBOL(pnp_unregister_driver);
  266. EXPORT_SYMBOL(pnp_device_attach);
  267. EXPORT_SYMBOL(pnp_device_detach);