device.c 3.8 KB

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