driver.c 6.4 KB

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