driver.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. #ifdef CONFIG_PNP_DEBUG
  14. #define DEBUG
  15. #else
  16. #undef DEBUG
  17. #endif
  18. #include <linux/pnp.h>
  19. #include "base.h"
  20. static int compare_func(const char *ida, const char *idb)
  21. {
  22. int i;
  23. /* we only need to compare the last 4 chars */
  24. for (i=3; i<7; i++)
  25. {
  26. if (ida[i] != 'X' &&
  27. idb[i] != 'X' &&
  28. toupper(ida[i]) != toupper(idb[i]))
  29. return 0;
  30. }
  31. return 1;
  32. }
  33. int compare_pnp_id(struct pnp_id *pos, const char *id)
  34. {
  35. if (!pos || !id || (strlen(id) != 7))
  36. return 0;
  37. if (memcmp(id,"ANYDEVS",7)==0)
  38. return 1;
  39. while (pos){
  40. if (memcmp(pos->id,id,3)==0)
  41. if (compare_func(pos->id,id)==1)
  42. return 1;
  43. pos = pos->next;
  44. }
  45. return 0;
  46. }
  47. static const struct pnp_device_id * match_device(struct pnp_driver *drv, struct pnp_dev *dev)
  48. {
  49. const struct pnp_device_id *drv_id = drv->id_table;
  50. if (!drv_id)
  51. return NULL;
  52. while (*drv_id->id) {
  53. if (compare_pnp_id(dev->id, drv_id->id))
  54. return drv_id;
  55. drv_id++;
  56. }
  57. return NULL;
  58. }
  59. int pnp_device_attach(struct pnp_dev *pnp_dev)
  60. {
  61. spin_lock(&pnp_lock);
  62. if(pnp_dev->status != PNP_READY){
  63. spin_unlock(&pnp_lock);
  64. return -EBUSY;
  65. }
  66. pnp_dev->status = PNP_ATTACHED;
  67. spin_unlock(&pnp_lock);
  68. return 0;
  69. }
  70. void pnp_device_detach(struct pnp_dev *pnp_dev)
  71. {
  72. spin_lock(&pnp_lock);
  73. if (pnp_dev->status == PNP_ATTACHED)
  74. pnp_dev->status = PNP_READY;
  75. spin_unlock(&pnp_lock);
  76. pnp_disable_dev(pnp_dev);
  77. }
  78. static int pnp_device_probe(struct device *dev)
  79. {
  80. int error;
  81. struct pnp_driver *pnp_drv;
  82. struct pnp_dev *pnp_dev;
  83. const struct pnp_device_id *dev_id = NULL;
  84. pnp_dev = to_pnp_dev(dev);
  85. pnp_drv = to_pnp_driver(dev->driver);
  86. pnp_dbg("match found with the PnP device '%s' and the driver '%s'", dev->bus_id,pnp_drv->name);
  87. error = pnp_device_attach(pnp_dev);
  88. if (error < 0)
  89. return error;
  90. if (pnp_dev->active == 0) {
  91. if (!(pnp_drv->flags & PNP_DRIVER_RES_DO_NOT_CHANGE)) {
  92. error = pnp_activate_dev(pnp_dev);
  93. if (error < 0)
  94. return error;
  95. }
  96. } else if ((pnp_drv->flags & PNP_DRIVER_RES_DISABLE)
  97. == PNP_DRIVER_RES_DISABLE) {
  98. error = pnp_disable_dev(pnp_dev);
  99. if (error < 0)
  100. return error;
  101. }
  102. error = 0;
  103. if (pnp_drv->probe) {
  104. dev_id = match_device(pnp_drv, pnp_dev);
  105. if (dev_id != NULL)
  106. error = pnp_drv->probe(pnp_dev, dev_id);
  107. }
  108. if (error >= 0){
  109. pnp_dev->driver = pnp_drv;
  110. error = 0;
  111. } else
  112. goto fail;
  113. return error;
  114. fail:
  115. pnp_device_detach(pnp_dev);
  116. return error;
  117. }
  118. static int pnp_device_remove(struct device *dev)
  119. {
  120. struct pnp_dev * pnp_dev = to_pnp_dev(dev);
  121. struct pnp_driver * drv = pnp_dev->driver;
  122. if (drv) {
  123. if (drv->remove)
  124. drv->remove(pnp_dev);
  125. pnp_dev->driver = NULL;
  126. }
  127. pnp_device_detach(pnp_dev);
  128. return 0;
  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. struct bus_type pnp_bus_type = {
  139. .name = "pnp",
  140. .match = pnp_bus_match,
  141. };
  142. static int count_devices(struct device * dev, void * c)
  143. {
  144. int * count = c;
  145. (*count)++;
  146. return 0;
  147. }
  148. int pnp_register_driver(struct pnp_driver *drv)
  149. {
  150. int count;
  151. pnp_dbg("the driver '%s' has been registered", drv->name);
  152. drv->driver.name = drv->name;
  153. drv->driver.bus = &pnp_bus_type;
  154. drv->driver.probe = pnp_device_probe;
  155. drv->driver.remove = pnp_device_remove;
  156. count = driver_register(&drv->driver);
  157. /* get the number of initial matches */
  158. if (count >= 0){
  159. count = 0;
  160. driver_for_each_device(&drv->driver, NULL, &count, count_devices);
  161. }
  162. return count;
  163. }
  164. void pnp_unregister_driver(struct pnp_driver *drv)
  165. {
  166. driver_unregister(&drv->driver);
  167. pnp_dbg("the driver '%s' has been unregistered", drv->name);
  168. }
  169. /**
  170. * pnp_add_id - adds an EISA id to the specified device
  171. * @id: pointer to a pnp_id structure
  172. * @dev: pointer to the desired device
  173. *
  174. */
  175. int pnp_add_id(struct pnp_id *id, struct pnp_dev *dev)
  176. {
  177. struct pnp_id *ptr;
  178. if (!id)
  179. return -EINVAL;
  180. if (!dev)
  181. return -EINVAL;
  182. id->next = NULL;
  183. ptr = dev->id;
  184. while (ptr && ptr->next)
  185. ptr = ptr->next;
  186. if (ptr)
  187. ptr->next = id;
  188. else
  189. dev->id = id;
  190. return 0;
  191. }
  192. EXPORT_SYMBOL(pnp_register_driver);
  193. EXPORT_SYMBOL(pnp_unregister_driver);
  194. EXPORT_SYMBOL(pnp_add_id);
  195. EXPORT_SYMBOL(pnp_device_attach);
  196. EXPORT_SYMBOL(pnp_device_detach);