of_device.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. #include <linux/config.h>
  2. #include <linux/string.h>
  3. #include <linux/kernel.h>
  4. #include <linux/init.h>
  5. #include <linux/module.h>
  6. #include <linux/mod_devicetable.h>
  7. #include <linux/slab.h>
  8. #include <asm/errno.h>
  9. #include <asm/of_device.h>
  10. /**
  11. * of_match_device - Tell if an of_device structure has a matching
  12. * of_match structure
  13. * @ids: array of of device match structures to search in
  14. * @dev: the of device structure to match against
  15. *
  16. * Used by a driver to check whether an of_device present in the
  17. * system is in its list of supported devices.
  18. */
  19. const struct of_device_id *of_match_device(const struct of_device_id *matches,
  20. const struct of_device *dev)
  21. {
  22. if (!dev->node)
  23. return NULL;
  24. while (matches->name[0] || matches->type[0] || matches->compatible[0]) {
  25. int match = 1;
  26. if (matches->name[0])
  27. match &= dev->node->name
  28. && !strcmp(matches->name, dev->node->name);
  29. if (matches->type[0])
  30. match &= dev->node->type
  31. && !strcmp(matches->type, dev->node->type);
  32. if (matches->compatible[0])
  33. match &= of_device_is_compatible(dev->node,
  34. matches->compatible);
  35. if (match)
  36. return matches;
  37. matches++;
  38. }
  39. return NULL;
  40. }
  41. static int of_platform_bus_match(struct device *dev, struct device_driver *drv)
  42. {
  43. struct of_device * of_dev = to_of_device(dev);
  44. struct of_platform_driver * of_drv = to_of_platform_driver(drv);
  45. const struct of_device_id * matches = of_drv->match_table;
  46. if (!matches)
  47. return 0;
  48. return of_match_device(matches, of_dev) != NULL;
  49. }
  50. struct of_device *of_dev_get(struct of_device *dev)
  51. {
  52. struct device *tmp;
  53. if (!dev)
  54. return NULL;
  55. tmp = get_device(&dev->dev);
  56. if (tmp)
  57. return to_of_device(tmp);
  58. else
  59. return NULL;
  60. }
  61. void of_dev_put(struct of_device *dev)
  62. {
  63. if (dev)
  64. put_device(&dev->dev);
  65. }
  66. static int of_device_probe(struct device *dev)
  67. {
  68. int error = -ENODEV;
  69. struct of_platform_driver *drv;
  70. struct of_device *of_dev;
  71. const struct of_device_id *match;
  72. drv = to_of_platform_driver(dev->driver);
  73. of_dev = to_of_device(dev);
  74. if (!drv->probe)
  75. return error;
  76. of_dev_get(of_dev);
  77. match = of_match_device(drv->match_table, of_dev);
  78. if (match)
  79. error = drv->probe(of_dev, match);
  80. if (error)
  81. of_dev_put(of_dev);
  82. return error;
  83. }
  84. static int of_device_remove(struct device *dev)
  85. {
  86. struct of_device * of_dev = to_of_device(dev);
  87. struct of_platform_driver * drv = to_of_platform_driver(dev->driver);
  88. if (dev->driver && drv->remove)
  89. drv->remove(of_dev);
  90. return 0;
  91. }
  92. static int of_device_suspend(struct device *dev, pm_message_t state)
  93. {
  94. struct of_device * of_dev = to_of_device(dev);
  95. struct of_platform_driver * drv = to_of_platform_driver(dev->driver);
  96. int error = 0;
  97. if (dev->driver && drv->suspend)
  98. error = drv->suspend(of_dev, state);
  99. return error;
  100. }
  101. static int of_device_resume(struct device * dev)
  102. {
  103. struct of_device * of_dev = to_of_device(dev);
  104. struct of_platform_driver * drv = to_of_platform_driver(dev->driver);
  105. int error = 0;
  106. if (dev->driver && drv->resume)
  107. error = drv->resume(of_dev);
  108. return error;
  109. }
  110. #ifdef CONFIG_PCI
  111. struct bus_type isa_bus_type = {
  112. .name = "isa",
  113. .match = of_platform_bus_match,
  114. .probe = of_device_probe,
  115. .remove = of_device_remove,
  116. .suspend = of_device_suspend,
  117. .resume = of_device_resume,
  118. };
  119. EXPORT_SYMBOL(isa_bus_type);
  120. struct bus_type ebus_bus_type = {
  121. .name = "ebus",
  122. .match = of_platform_bus_match,
  123. .probe = of_device_probe,
  124. .remove = of_device_remove,
  125. .suspend = of_device_suspend,
  126. .resume = of_device_resume,
  127. };
  128. EXPORT_SYMBOL(ebus_bus_type);
  129. #endif
  130. #ifdef CONFIG_SBUS
  131. struct bus_type sbus_bus_type = {
  132. .name = "sbus",
  133. .match = of_platform_bus_match,
  134. .probe = of_device_probe,
  135. .remove = of_device_remove,
  136. .suspend = of_device_suspend,
  137. .resume = of_device_resume,
  138. };
  139. EXPORT_SYMBOL(sbus_bus_type);
  140. #endif
  141. static int __init of_bus_driver_init(void)
  142. {
  143. int err = 0;
  144. #ifdef CONFIG_PCI
  145. if (!err)
  146. err = bus_register(&isa_bus_type);
  147. if (!err)
  148. err = bus_register(&ebus_bus_type);
  149. #endif
  150. #ifdef CONFIG_SBUS
  151. if (!err)
  152. err = bus_register(&sbus_bus_type);
  153. #endif
  154. return 0;
  155. }
  156. postcore_initcall(of_bus_driver_init);
  157. int of_register_driver(struct of_platform_driver *drv, struct bus_type *bus)
  158. {
  159. /* initialize common driver fields */
  160. drv->driver.name = drv->name;
  161. drv->driver.bus = bus;
  162. /* register with core */
  163. return driver_register(&drv->driver);
  164. }
  165. void of_unregister_driver(struct of_platform_driver *drv)
  166. {
  167. driver_unregister(&drv->driver);
  168. }
  169. static ssize_t dev_show_devspec(struct device *dev, struct device_attribute *attr, char *buf)
  170. {
  171. struct of_device *ofdev;
  172. ofdev = to_of_device(dev);
  173. return sprintf(buf, "%s", ofdev->node->full_name);
  174. }
  175. static DEVICE_ATTR(devspec, S_IRUGO, dev_show_devspec, NULL);
  176. /**
  177. * of_release_dev - free an of device structure when all users of it are finished.
  178. * @dev: device that's been disconnected
  179. *
  180. * Will be called only by the device core when all users of this of device are
  181. * done.
  182. */
  183. void of_release_dev(struct device *dev)
  184. {
  185. struct of_device *ofdev;
  186. ofdev = to_of_device(dev);
  187. kfree(ofdev);
  188. }
  189. int of_device_register(struct of_device *ofdev)
  190. {
  191. int rc;
  192. BUG_ON(ofdev->node == NULL);
  193. rc = device_register(&ofdev->dev);
  194. if (rc)
  195. return rc;
  196. device_create_file(&ofdev->dev, &dev_attr_devspec);
  197. return 0;
  198. }
  199. void of_device_unregister(struct of_device *ofdev)
  200. {
  201. device_remove_file(&ofdev->dev, &dev_attr_devspec);
  202. device_unregister(&ofdev->dev);
  203. }
  204. struct of_device* of_platform_device_create(struct device_node *np,
  205. const char *bus_id,
  206. struct device *parent,
  207. struct bus_type *bus)
  208. {
  209. struct of_device *dev;
  210. dev = kmalloc(sizeof(*dev), GFP_KERNEL);
  211. if (!dev)
  212. return NULL;
  213. memset(dev, 0, sizeof(*dev));
  214. dev->dev.parent = parent;
  215. dev->dev.bus = bus;
  216. dev->dev.release = of_release_dev;
  217. strlcpy(dev->dev.bus_id, bus_id, BUS_ID_SIZE);
  218. if (of_device_register(dev) != 0) {
  219. kfree(dev);
  220. return NULL;
  221. }
  222. return dev;
  223. }
  224. EXPORT_SYMBOL(of_match_device);
  225. EXPORT_SYMBOL(of_register_driver);
  226. EXPORT_SYMBOL(of_unregister_driver);
  227. EXPORT_SYMBOL(of_device_register);
  228. EXPORT_SYMBOL(of_device_unregister);
  229. EXPORT_SYMBOL(of_dev_get);
  230. EXPORT_SYMBOL(of_dev_put);
  231. EXPORT_SYMBOL(of_platform_device_create);
  232. EXPORT_SYMBOL(of_release_dev);