of_device.c 6.3 KB

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