of_device.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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 &= of_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. ssize_t of_device_get_modalias(struct of_device *ofdev,
  105. char *str, ssize_t len)
  106. {
  107. const char *compat;
  108. int cplen, i;
  109. ssize_t tsize, csize, repend;
  110. /* Name & Type */
  111. csize = snprintf(str, len, "of:N%sT%s",
  112. ofdev->node->name, ofdev->node->type);
  113. /* Get compatible property if any */
  114. compat = of_get_property(ofdev->node, "compatible", &cplen);
  115. if (!compat)
  116. return csize;
  117. /* Find true end (we tolerate multiple \0 at the end */
  118. for (i=(cplen-1); i>=0 && !compat[i]; i--)
  119. cplen--;
  120. if (!cplen)
  121. return csize;
  122. cplen++;
  123. /* Check space (need cplen+1 chars including final \0) */
  124. tsize = csize + cplen;
  125. repend = tsize;
  126. if (csize>=len) /* @ the limit, all is already filled */
  127. return tsize;
  128. if (tsize>=len) { /* limit compat list */
  129. cplen = len-csize-1;
  130. repend = len;
  131. }
  132. /* Copy and do char replacement */
  133. memcpy(&str[csize+1], compat, cplen);
  134. for (i=csize; i<repend; i++) {
  135. char c = str[i];
  136. if (c=='\0')
  137. str[i] = 'C';
  138. else if (c==' ')
  139. str[i] = '_';
  140. }
  141. return tsize;
  142. }
  143. int of_device_uevent(struct device *dev,
  144. char **envp, int num_envp, char *buffer, int buffer_size)
  145. {
  146. struct of_device *ofdev;
  147. const char *compat;
  148. int i = 0, length = 0, seen = 0, cplen, sl;
  149. if (!dev)
  150. return -ENODEV;
  151. ofdev = to_of_device(dev);
  152. if (add_uevent_var(envp, num_envp, &i,
  153. buffer, buffer_size, &length,
  154. "OF_NAME=%s", ofdev->node->name))
  155. return -ENOMEM;
  156. if (add_uevent_var(envp, num_envp, &i,
  157. buffer, buffer_size, &length,
  158. "OF_TYPE=%s", ofdev->node->type))
  159. return -ENOMEM;
  160. /* Since the compatible field can contain pretty much anything
  161. * it's not really legal to split it out with commas. We split it
  162. * up using a number of environment variables instead. */
  163. compat = of_get_property(ofdev->node, "compatible", &cplen);
  164. while (compat && *compat && cplen > 0) {
  165. if (add_uevent_var(envp, num_envp, &i,
  166. buffer, buffer_size, &length,
  167. "OF_COMPATIBLE_%d=%s", seen, compat))
  168. return -ENOMEM;
  169. sl = strlen (compat) + 1;
  170. compat += sl;
  171. cplen -= sl;
  172. seen++;
  173. }
  174. if (add_uevent_var(envp, num_envp, &i,
  175. buffer, buffer_size, &length,
  176. "OF_COMPATIBLE_N=%d", seen))
  177. return -ENOMEM;
  178. /* modalias is trickier, we add it in 2 steps */
  179. if (add_uevent_var(envp, num_envp, &i,
  180. buffer, buffer_size, &length,
  181. "MODALIAS="))
  182. return -ENOMEM;
  183. sl = of_device_get_modalias(ofdev, &buffer[length-1],
  184. buffer_size-length);
  185. if (sl >= (buffer_size-length))
  186. return -ENOMEM;
  187. length += sl;
  188. envp[i] = NULL;
  189. return 0;
  190. }
  191. EXPORT_SYMBOL(of_match_node);
  192. EXPORT_SYMBOL(of_match_device);
  193. EXPORT_SYMBOL(of_device_register);
  194. EXPORT_SYMBOL(of_device_unregister);
  195. EXPORT_SYMBOL(of_dev_get);
  196. EXPORT_SYMBOL(of_dev_put);
  197. EXPORT_SYMBOL(of_release_dev);
  198. EXPORT_SYMBOL(of_device_uevent);
  199. EXPORT_SYMBOL(of_device_get_modalias);