of_device.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #include <linux/string.h>
  2. #include <linux/kernel.h>
  3. #include <linux/init.h>
  4. #include <linux/module.h>
  5. #include <linux/mod_devicetable.h>
  6. #include <linux/slab.h>
  7. #include <asm/errno.h>
  8. #include <asm/of_device.h>
  9. /**
  10. * of_match_node - Tell if an device_node has a matching of_match structure
  11. * @ids: array of of device match structures to search in
  12. * @node: the of device structure to match against
  13. *
  14. * Low level utility function used by device matching.
  15. */
  16. const struct of_device_id *of_match_node(const struct of_device_id *matches,
  17. const struct device_node *node)
  18. {
  19. while (matches->name[0] || matches->type[0] || matches->compatible[0]) {
  20. int match = 1;
  21. if (matches->name[0])
  22. match &= node->name
  23. && !strcmp(matches->name, node->name);
  24. if (matches->type[0])
  25. match &= node->type
  26. && !strcmp(matches->type, node->type);
  27. if (matches->compatible[0])
  28. match &= device_is_compatible(node,
  29. matches->compatible);
  30. if (match)
  31. return matches;
  32. matches++;
  33. }
  34. return NULL;
  35. }
  36. /**
  37. * of_match_device - Tell if an of_device structure has a matching
  38. * of_match structure
  39. * @ids: array of of device match structures to search in
  40. * @dev: the of device structure to match against
  41. *
  42. * Used by a driver to check whether an of_device present in the
  43. * system is in its list of supported devices.
  44. */
  45. const struct of_device_id *of_match_device(const struct of_device_id *matches,
  46. const struct of_device *dev)
  47. {
  48. if (!dev->node)
  49. return NULL;
  50. return of_match_node(matches, dev->node);
  51. }
  52. struct of_device *of_dev_get(struct of_device *dev)
  53. {
  54. struct device *tmp;
  55. if (!dev)
  56. return NULL;
  57. tmp = get_device(&dev->dev);
  58. if (tmp)
  59. return to_of_device(tmp);
  60. else
  61. return NULL;
  62. }
  63. void of_dev_put(struct of_device *dev)
  64. {
  65. if (dev)
  66. put_device(&dev->dev);
  67. }
  68. static ssize_t dev_show_devspec(struct device *dev,
  69. struct device_attribute *attr, char *buf)
  70. {
  71. struct of_device *ofdev;
  72. ofdev = to_of_device(dev);
  73. return sprintf(buf, "%s", ofdev->node->full_name);
  74. }
  75. static DEVICE_ATTR(devspec, S_IRUGO, dev_show_devspec, NULL);
  76. /**
  77. * of_release_dev - free an of device structure when all users of it are finished.
  78. * @dev: device that's been disconnected
  79. *
  80. * Will be called only by the device core when all users of this of device are
  81. * done.
  82. */
  83. void of_release_dev(struct device *dev)
  84. {
  85. struct of_device *ofdev;
  86. ofdev = to_of_device(dev);
  87. of_node_put(ofdev->node);
  88. kfree(ofdev);
  89. }
  90. int of_device_register(struct of_device *ofdev)
  91. {
  92. int rc;
  93. BUG_ON(ofdev->node == NULL);
  94. rc = device_register(&ofdev->dev);
  95. if (rc)
  96. return rc;
  97. return device_create_file(&ofdev->dev, &dev_attr_devspec);
  98. }
  99. void of_device_unregister(struct of_device *ofdev)
  100. {
  101. device_remove_file(&ofdev->dev, &dev_attr_devspec);
  102. device_unregister(&ofdev->dev);
  103. }
  104. EXPORT_SYMBOL(of_match_node);
  105. EXPORT_SYMBOL(of_match_device);
  106. EXPORT_SYMBOL(of_device_register);
  107. EXPORT_SYMBOL(of_device_unregister);
  108. EXPORT_SYMBOL(of_dev_get);
  109. EXPORT_SYMBOL(of_dev_put);
  110. EXPORT_SYMBOL(of_release_dev);