device.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. #include "of_private.h"
  11. /**
  12. * of_match_device - Tell if a struct device matches an of_device_id list
  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 platform_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 device *dev)
  21. {
  22. if ((!matches) || (!dev->of_node))
  23. return NULL;
  24. return of_match_node(matches, dev->of_node);
  25. }
  26. EXPORT_SYMBOL(of_match_device);
  27. struct platform_device *of_dev_get(struct platform_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_platform_device(tmp);
  35. else
  36. return NULL;
  37. }
  38. EXPORT_SYMBOL(of_dev_get);
  39. void of_dev_put(struct platform_device *dev)
  40. {
  41. if (dev)
  42. put_device(&dev->dev);
  43. }
  44. EXPORT_SYMBOL(of_dev_put);
  45. int of_device_add(struct platform_device *ofdev)
  46. {
  47. BUG_ON(ofdev->dev.of_node == NULL);
  48. /* name and id have to be set so that the platform bus doesn't get
  49. * confused on matching */
  50. ofdev->name = dev_name(&ofdev->dev);
  51. ofdev->id = -1;
  52. /* device_add will assume that this device is on the same node as
  53. * the parent. If there is no parent defined, set the node
  54. * explicitly */
  55. if (!ofdev->dev.parent)
  56. set_dev_node(&ofdev->dev, of_node_to_nid(ofdev->dev.of_node));
  57. return device_add(&ofdev->dev);
  58. }
  59. int of_device_register(struct platform_device *pdev)
  60. {
  61. device_initialize(&pdev->dev);
  62. return of_device_add(pdev);
  63. }
  64. EXPORT_SYMBOL(of_device_register);
  65. void of_device_unregister(struct platform_device *ofdev)
  66. {
  67. device_unregister(&ofdev->dev);
  68. }
  69. EXPORT_SYMBOL(of_device_unregister);
  70. ssize_t of_device_get_modalias(struct device *dev, char *str, ssize_t len)
  71. {
  72. const char *compat;
  73. int cplen, i;
  74. ssize_t tsize, csize, repend;
  75. if ((!dev) || (!dev->of_node))
  76. return -ENODEV;
  77. /* Name & Type */
  78. csize = snprintf(str, len, "of:N%sT%s", dev->of_node->name,
  79. dev->of_node->type);
  80. /* Get compatible property if any */
  81. compat = of_get_property(dev->of_node, "compatible", &cplen);
  82. if (!compat)
  83. return csize;
  84. /* Find true end (we tolerate multiple \0 at the end */
  85. for (i = (cplen - 1); i >= 0 && !compat[i]; i--)
  86. cplen--;
  87. if (!cplen)
  88. return csize;
  89. cplen++;
  90. /* Check space (need cplen+1 chars including final \0) */
  91. tsize = csize + cplen;
  92. repend = tsize;
  93. if (csize >= len) /* @ the limit, all is already filled */
  94. return tsize;
  95. if (tsize >= len) { /* limit compat list */
  96. cplen = len - csize - 1;
  97. repend = len;
  98. }
  99. /* Copy and do char replacement */
  100. memcpy(&str[csize + 1], compat, cplen);
  101. for (i = csize; i < repend; i++) {
  102. char c = str[i];
  103. if (c == '\0')
  104. str[i] = 'C';
  105. else if (c == ' ')
  106. str[i] = '_';
  107. }
  108. return tsize;
  109. }
  110. /**
  111. * of_device_uevent - Display OF related uevent information
  112. */
  113. void of_device_uevent(struct device *dev, struct kobj_uevent_env *env)
  114. {
  115. const char *compat;
  116. struct alias_prop *app;
  117. int seen = 0, cplen, sl;
  118. if ((!dev) || (!dev->of_node))
  119. return;
  120. add_uevent_var(env, "OF_NAME=%s", dev->of_node->name);
  121. add_uevent_var(env, "OF_FULLNAME=%s", dev->of_node->full_name);
  122. if (dev->of_node->type && strcmp("<NULL>", dev->of_node->type) != 0)
  123. add_uevent_var(env, "OF_TYPE=%s", dev->of_node->type);
  124. /* Since the compatible field can contain pretty much anything
  125. * it's not really legal to split it out with commas. We split it
  126. * up using a number of environment variables instead. */
  127. compat = of_get_property(dev->of_node, "compatible", &cplen);
  128. while (compat && *compat && cplen > 0) {
  129. add_uevent_var(env, "OF_COMPATIBLE_%d=%s", seen, compat);
  130. sl = strlen(compat) + 1;
  131. compat += sl;
  132. cplen -= sl;
  133. seen++;
  134. }
  135. add_uevent_var(env, "OF_COMPATIBLE_N=%d", seen);
  136. seen = 0;
  137. mutex_lock(&of_mutex);
  138. list_for_each_entry(app, &aliases_lookup, link) {
  139. if (dev->of_node == app->np) {
  140. add_uevent_var(env, "OF_ALIAS_%d=%s", seen,
  141. app->alias);
  142. seen++;
  143. }
  144. }
  145. mutex_unlock(&of_mutex);
  146. }
  147. int of_device_uevent_modalias(struct device *dev, struct kobj_uevent_env *env)
  148. {
  149. int sl;
  150. if ((!dev) || (!dev->of_node))
  151. return -ENODEV;
  152. /* Devicetree modalias is tricky, we add it in 2 steps */
  153. if (add_uevent_var(env, "MODALIAS="))
  154. return -ENOMEM;
  155. sl = of_device_get_modalias(dev, &env->buf[env->buflen-1],
  156. sizeof(env->buf) - env->buflen);
  157. if (sl >= (sizeof(env->buf) - env->buflen))
  158. return -ENOMEM;
  159. env->buflen += sl;
  160. return 0;
  161. }