device.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. /* Name & Type */
  76. csize = snprintf(str, len, "of:N%sT%s", dev->of_node->name,
  77. dev->of_node->type);
  78. /* Get compatible property if any */
  79. compat = of_get_property(dev->of_node, "compatible", &cplen);
  80. if (!compat)
  81. return csize;
  82. /* Find true end (we tolerate multiple \0 at the end */
  83. for (i = (cplen - 1); i >= 0 && !compat[i]; i--)
  84. cplen--;
  85. if (!cplen)
  86. return csize;
  87. cplen++;
  88. /* Check space (need cplen+1 chars including final \0) */
  89. tsize = csize + cplen;
  90. repend = tsize;
  91. if (csize >= len) /* @ the limit, all is already filled */
  92. return tsize;
  93. if (tsize >= len) { /* limit compat list */
  94. cplen = len - csize - 1;
  95. repend = len;
  96. }
  97. /* Copy and do char replacement */
  98. memcpy(&str[csize + 1], compat, cplen);
  99. for (i = csize; i < repend; i++) {
  100. char c = str[i];
  101. if (c == '\0')
  102. str[i] = 'C';
  103. else if (c == ' ')
  104. str[i] = '_';
  105. }
  106. return tsize;
  107. }
  108. /**
  109. * of_device_uevent - Display OF related uevent information
  110. */
  111. void of_device_uevent(struct device *dev, struct kobj_uevent_env *env)
  112. {
  113. const char *compat;
  114. struct alias_prop *app;
  115. int seen = 0, cplen, sl;
  116. if ((!dev) || (!dev->of_node))
  117. return;
  118. add_uevent_var(env, "OF_NAME=%s", dev->of_node->name);
  119. add_uevent_var(env, "OF_FULLNAME=%s", dev->of_node->full_name);
  120. if (dev->of_node->type && strcmp("<NULL>", dev->of_node->type) != 0)
  121. add_uevent_var(env, "OF_TYPE=%s", dev->of_node->type);
  122. /* Since the compatible field can contain pretty much anything
  123. * it's not really legal to split it out with commas. We split it
  124. * up using a number of environment variables instead. */
  125. compat = of_get_property(dev->of_node, "compatible", &cplen);
  126. while (compat && *compat && cplen > 0) {
  127. add_uevent_var(env, "OF_COMPATIBLE_%d=%s", seen, compat);
  128. sl = strlen(compat) + 1;
  129. compat += sl;
  130. cplen -= sl;
  131. seen++;
  132. }
  133. add_uevent_var(env, "OF_COMPATIBLE_N=%d", seen);
  134. seen = 0;
  135. mutex_lock(&of_aliases_mutex);
  136. list_for_each_entry(app, &aliases_lookup, link) {
  137. if (dev->of_node == app->np) {
  138. add_uevent_var(env, "OF_ALIAS_%d=%s", seen,
  139. app->alias);
  140. seen++;
  141. }
  142. }
  143. if (seen)
  144. add_uevent_var(env, "OF_ALIAS_N=%d", seen);
  145. mutex_unlock(&of_aliases_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. }