device.c 3.0 KB

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