device.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 modalias_show(struct device *dev,
  53. struct device_attribute *attr, char *buf)
  54. {
  55. struct of_device *ofdev = to_of_device(dev);
  56. ssize_t len = 0;
  57. len = of_device_get_modalias(ofdev, buf, PAGE_SIZE - 2);
  58. buf[len] = '\n';
  59. buf[len+1] = 0;
  60. return len+1;
  61. }
  62. struct device_attribute of_platform_device_attrs[] = {
  63. __ATTR_RO(devspec),
  64. __ATTR_RO(modalias),
  65. __ATTR_NULL
  66. };
  67. /**
  68. * of_release_dev - free an of device structure when all users of it are finished.
  69. * @dev: device that's been disconnected
  70. *
  71. * Will be called only by the device core when all users of this of device are
  72. * done.
  73. */
  74. void of_release_dev(struct device *dev)
  75. {
  76. struct of_device *ofdev;
  77. ofdev = to_of_device(dev);
  78. of_node_put(ofdev->node);
  79. kfree(ofdev);
  80. }
  81. EXPORT_SYMBOL(of_release_dev);
  82. int of_device_register(struct of_device *ofdev)
  83. {
  84. BUG_ON(ofdev->node == NULL);
  85. return device_register(&ofdev->dev);
  86. }
  87. EXPORT_SYMBOL(of_device_register);
  88. void of_device_unregister(struct of_device *ofdev)
  89. {
  90. device_unregister(&ofdev->dev);
  91. }
  92. EXPORT_SYMBOL(of_device_unregister);
  93. ssize_t of_device_get_modalias(struct of_device *ofdev,
  94. char *str, ssize_t len)
  95. {
  96. const char *compat;
  97. int cplen, i;
  98. ssize_t tsize, csize, repend;
  99. /* Name & Type */
  100. csize = snprintf(str, len, "of:N%sT%s",
  101. ofdev->node->name, ofdev->node->type);
  102. /* Get compatible property if any */
  103. compat = of_get_property(ofdev->node, "compatible", &cplen);
  104. if (!compat)
  105. return csize;
  106. /* Find true end (we tolerate multiple \0 at the end */
  107. for (i = (cplen - 1); i >= 0 && !compat[i]; i--)
  108. cplen--;
  109. if (!cplen)
  110. return csize;
  111. cplen++;
  112. /* Check space (need cplen+1 chars including final \0) */
  113. tsize = csize + cplen;
  114. repend = tsize;
  115. if (csize >= len) /* @ the limit, all is already filled */
  116. return tsize;
  117. if (tsize >= len) { /* limit compat list */
  118. cplen = len - csize - 1;
  119. repend = len;
  120. }
  121. /* Copy and do char replacement */
  122. memcpy(&str[csize + 1], compat, cplen);
  123. for (i = csize; i < repend; i++) {
  124. char c = str[i];
  125. if (c == '\0')
  126. str[i] = 'C';
  127. else if (c == ' ')
  128. str[i] = '_';
  129. }
  130. return tsize;
  131. }