device.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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_device - Tell if a struct device matches an of_device_id list
  12. * @ids: array of of device match structures to search in
  13. * @dev: the of device structure to match against
  14. *
  15. * Used by a driver to check whether an of_device present in the
  16. * system is in its list of supported devices.
  17. */
  18. const struct of_device_id *of_match_device(const struct of_device_id *matches,
  19. const struct device *dev)
  20. {
  21. if (!dev->of_node)
  22. return NULL;
  23. return of_match_node(matches, dev->of_node);
  24. }
  25. EXPORT_SYMBOL(of_match_device);
  26. struct of_device *of_dev_get(struct of_device *dev)
  27. {
  28. struct device *tmp;
  29. if (!dev)
  30. return NULL;
  31. tmp = get_device(&dev->dev);
  32. if (tmp)
  33. return to_of_device(tmp);
  34. else
  35. return NULL;
  36. }
  37. EXPORT_SYMBOL(of_dev_get);
  38. void of_dev_put(struct of_device *dev)
  39. {
  40. if (dev)
  41. put_device(&dev->dev);
  42. }
  43. EXPORT_SYMBOL(of_dev_put);
  44. static ssize_t devspec_show(struct device *dev,
  45. struct device_attribute *attr, char *buf)
  46. {
  47. struct of_device *ofdev;
  48. ofdev = to_of_device(dev);
  49. return sprintf(buf, "%s\n", ofdev->dev.of_node->full_name);
  50. }
  51. static ssize_t name_show(struct device *dev,
  52. struct device_attribute *attr, char *buf)
  53. {
  54. struct of_device *ofdev;
  55. ofdev = to_of_device(dev);
  56. return sprintf(buf, "%s\n", ofdev->dev.of_node->name);
  57. }
  58. static ssize_t modalias_show(struct device *dev,
  59. struct device_attribute *attr, char *buf)
  60. {
  61. struct of_device *ofdev = to_of_device(dev);
  62. ssize_t len = 0;
  63. len = of_device_get_modalias(ofdev, buf, PAGE_SIZE - 2);
  64. buf[len] = '\n';
  65. buf[len+1] = 0;
  66. return len+1;
  67. }
  68. struct device_attribute of_platform_device_attrs[] = {
  69. __ATTR_RO(devspec),
  70. __ATTR_RO(name),
  71. __ATTR_RO(modalias),
  72. __ATTR_NULL
  73. };
  74. /**
  75. * of_release_dev - free an of device structure when all users of it are finished.
  76. * @dev: device that's been disconnected
  77. *
  78. * Will be called only by the device core when all users of this of device are
  79. * done.
  80. */
  81. void of_release_dev(struct device *dev)
  82. {
  83. struct of_device *ofdev;
  84. ofdev = to_of_device(dev);
  85. of_node_put(ofdev->dev.of_node);
  86. kfree(ofdev);
  87. }
  88. EXPORT_SYMBOL(of_release_dev);
  89. int of_device_register(struct of_device *ofdev)
  90. {
  91. BUG_ON(ofdev->dev.of_node == NULL);
  92. device_initialize(&ofdev->dev);
  93. /* device_add will assume that this device is on the same node as
  94. * the parent. If there is no parent defined, set the node
  95. * explicitly */
  96. if (!ofdev->dev.parent)
  97. set_dev_node(&ofdev->dev, of_node_to_nid(ofdev->dev.of_node));
  98. return device_add(&ofdev->dev);
  99. }
  100. EXPORT_SYMBOL(of_device_register);
  101. void of_device_unregister(struct of_device *ofdev)
  102. {
  103. device_unregister(&ofdev->dev);
  104. }
  105. EXPORT_SYMBOL(of_device_unregister);
  106. ssize_t of_device_get_modalias(struct of_device *ofdev,
  107. char *str, ssize_t len)
  108. {
  109. const char *compat;
  110. int cplen, i;
  111. ssize_t tsize, csize, repend;
  112. /* Name & Type */
  113. csize = snprintf(str, len, "of:N%sT%s", ofdev->dev.of_node->name,
  114. ofdev->dev.of_node->type);
  115. /* Get compatible property if any */
  116. compat = of_get_property(ofdev->dev.of_node, "compatible", &cplen);
  117. if (!compat)
  118. return csize;
  119. /* Find true end (we tolerate multiple \0 at the end */
  120. for (i = (cplen - 1); i >= 0 && !compat[i]; i--)
  121. cplen--;
  122. if (!cplen)
  123. return csize;
  124. cplen++;
  125. /* Check space (need cplen+1 chars including final \0) */
  126. tsize = csize + cplen;
  127. repend = tsize;
  128. if (csize >= len) /* @ the limit, all is already filled */
  129. return tsize;
  130. if (tsize >= len) { /* limit compat list */
  131. cplen = len - csize - 1;
  132. repend = len;
  133. }
  134. /* Copy and do char replacement */
  135. memcpy(&str[csize + 1], compat, cplen);
  136. for (i = csize; i < repend; i++) {
  137. char c = str[i];
  138. if (c == '\0')
  139. str[i] = 'C';
  140. else if (c == ' ')
  141. str[i] = '_';
  142. }
  143. return tsize;
  144. }