driver.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /*
  2. * driver.c - device id matching, driver model, etc.
  3. *
  4. * Copyright 2002 Adam Belay <ambx1@neo.rr.com>
  5. *
  6. */
  7. #include <linux/config.h>
  8. #include <linux/string.h>
  9. #include <linux/list.h>
  10. #include <linux/module.h>
  11. #include <linux/ctype.h>
  12. #include <linux/slab.h>
  13. #include <linux/pnp.h>
  14. #include "base.h"
  15. static int compare_func(const char *ida, const char *idb)
  16. {
  17. int i;
  18. /* we only need to compare the last 4 chars */
  19. for (i=3; i<7; i++)
  20. {
  21. if (ida[i] != 'X' &&
  22. idb[i] != 'X' &&
  23. toupper(ida[i]) != toupper(idb[i]))
  24. return 0;
  25. }
  26. return 1;
  27. }
  28. int compare_pnp_id(struct pnp_id *pos, const char *id)
  29. {
  30. if (!pos || !id || (strlen(id) != 7))
  31. return 0;
  32. if (memcmp(id,"ANYDEVS",7)==0)
  33. return 1;
  34. while (pos){
  35. if (memcmp(pos->id,id,3)==0)
  36. if (compare_func(pos->id,id)==1)
  37. return 1;
  38. pos = pos->next;
  39. }
  40. return 0;
  41. }
  42. static const struct pnp_device_id * match_device(struct pnp_driver *drv, struct pnp_dev *dev)
  43. {
  44. const struct pnp_device_id *drv_id = drv->id_table;
  45. if (!drv_id)
  46. return NULL;
  47. while (*drv_id->id) {
  48. if (compare_pnp_id(dev->id, drv_id->id))
  49. return drv_id;
  50. drv_id++;
  51. }
  52. return NULL;
  53. }
  54. int pnp_device_attach(struct pnp_dev *pnp_dev)
  55. {
  56. spin_lock(&pnp_lock);
  57. if(pnp_dev->status != PNP_READY){
  58. spin_unlock(&pnp_lock);
  59. return -EBUSY;
  60. }
  61. pnp_dev->status = PNP_ATTACHED;
  62. spin_unlock(&pnp_lock);
  63. return 0;
  64. }
  65. void pnp_device_detach(struct pnp_dev *pnp_dev)
  66. {
  67. spin_lock(&pnp_lock);
  68. if (pnp_dev->status == PNP_ATTACHED)
  69. pnp_dev->status = PNP_READY;
  70. spin_unlock(&pnp_lock);
  71. pnp_disable_dev(pnp_dev);
  72. }
  73. static int pnp_device_probe(struct device *dev)
  74. {
  75. int error;
  76. struct pnp_driver *pnp_drv;
  77. struct pnp_dev *pnp_dev;
  78. const struct pnp_device_id *dev_id = NULL;
  79. pnp_dev = to_pnp_dev(dev);
  80. pnp_drv = to_pnp_driver(dev->driver);
  81. pnp_dbg("match found with the PnP device '%s' and the driver '%s'", dev->bus_id,pnp_drv->name);
  82. error = pnp_device_attach(pnp_dev);
  83. if (error < 0)
  84. return error;
  85. if (pnp_dev->active == 0) {
  86. if (!(pnp_drv->flags & PNP_DRIVER_RES_DO_NOT_CHANGE)) {
  87. error = pnp_activate_dev(pnp_dev);
  88. if (error < 0)
  89. return error;
  90. }
  91. } else if ((pnp_drv->flags & PNP_DRIVER_RES_DISABLE)
  92. == PNP_DRIVER_RES_DISABLE) {
  93. error = pnp_disable_dev(pnp_dev);
  94. if (error < 0)
  95. return error;
  96. }
  97. error = 0;
  98. if (pnp_drv->probe) {
  99. dev_id = match_device(pnp_drv, pnp_dev);
  100. if (dev_id != NULL)
  101. error = pnp_drv->probe(pnp_dev, dev_id);
  102. }
  103. if (error >= 0){
  104. pnp_dev->driver = pnp_drv;
  105. error = 0;
  106. } else
  107. goto fail;
  108. return error;
  109. fail:
  110. pnp_device_detach(pnp_dev);
  111. return error;
  112. }
  113. static int pnp_device_remove(struct device *dev)
  114. {
  115. struct pnp_dev * pnp_dev = to_pnp_dev(dev);
  116. struct pnp_driver * drv = pnp_dev->driver;
  117. if (drv) {
  118. if (drv->remove)
  119. drv->remove(pnp_dev);
  120. pnp_dev->driver = NULL;
  121. }
  122. pnp_device_detach(pnp_dev);
  123. return 0;
  124. }
  125. static int pnp_bus_match(struct device *dev, struct device_driver *drv)
  126. {
  127. struct pnp_dev * pnp_dev = to_pnp_dev(dev);
  128. struct pnp_driver * pnp_drv = to_pnp_driver(drv);
  129. if (match_device(pnp_drv, pnp_dev) == NULL)
  130. return 0;
  131. return 1;
  132. }
  133. static int pnp_bus_suspend(struct device *dev, pm_message_t state)
  134. {
  135. struct pnp_dev * pnp_dev = to_pnp_dev(dev);
  136. struct pnp_driver * pnp_drv = pnp_dev->driver;
  137. int error;
  138. if (!pnp_drv)
  139. return 0;
  140. if (pnp_drv->suspend) {
  141. error = pnp_drv->suspend(pnp_dev, state);
  142. if (error)
  143. return error;
  144. }
  145. if (!(pnp_drv->flags & PNP_DRIVER_RES_DO_NOT_CHANGE) &&
  146. pnp_can_disable(pnp_dev)) {
  147. error = pnp_stop_dev(pnp_dev);
  148. if (error)
  149. return error;
  150. }
  151. return 0;
  152. }
  153. static int pnp_bus_resume(struct device *dev)
  154. {
  155. struct pnp_dev * pnp_dev = to_pnp_dev(dev);
  156. struct pnp_driver * pnp_drv = pnp_dev->driver;
  157. int error;
  158. if (!pnp_drv)
  159. return 0;
  160. if (!(pnp_drv->flags & PNP_DRIVER_RES_DO_NOT_CHANGE)) {
  161. error = pnp_start_dev(pnp_dev);
  162. if (error)
  163. return error;
  164. }
  165. if (pnp_drv->resume)
  166. return pnp_drv->resume(pnp_dev);
  167. return 0;
  168. }
  169. struct bus_type pnp_bus_type = {
  170. .name = "pnp",
  171. .match = pnp_bus_match,
  172. .probe = pnp_device_probe,
  173. .remove = pnp_device_remove,
  174. .suspend = pnp_bus_suspend,
  175. .resume = pnp_bus_resume,
  176. };
  177. int pnp_register_driver(struct pnp_driver *drv)
  178. {
  179. pnp_dbg("the driver '%s' has been registered", drv->name);
  180. drv->driver.name = drv->name;
  181. drv->driver.bus = &pnp_bus_type;
  182. return driver_register(&drv->driver);
  183. }
  184. void pnp_unregister_driver(struct pnp_driver *drv)
  185. {
  186. driver_unregister(&drv->driver);
  187. pnp_dbg("the driver '%s' has been unregistered", drv->name);
  188. }
  189. /**
  190. * pnp_add_id - adds an EISA id to the specified device
  191. * @id: pointer to a pnp_id structure
  192. * @dev: pointer to the desired device
  193. *
  194. */
  195. int pnp_add_id(struct pnp_id *id, struct pnp_dev *dev)
  196. {
  197. struct pnp_id *ptr;
  198. if (!id)
  199. return -EINVAL;
  200. if (!dev)
  201. return -EINVAL;
  202. id->next = NULL;
  203. ptr = dev->id;
  204. while (ptr && ptr->next)
  205. ptr = ptr->next;
  206. if (ptr)
  207. ptr->next = id;
  208. else
  209. dev->id = id;
  210. return 0;
  211. }
  212. EXPORT_SYMBOL(pnp_register_driver);
  213. EXPORT_SYMBOL(pnp_unregister_driver);
  214. #if 0
  215. EXPORT_SYMBOL(pnp_add_id);
  216. #endif
  217. EXPORT_SYMBOL(pnp_device_attach);
  218. EXPORT_SYMBOL(pnp_device_detach);