device.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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 a struct device matches an of_device_id list
  12. * @ids: array of of device match structures to search in
  13. * @dev: the of device structure to match against
  14. *
  15. * Used by a driver to check whether an platform_device present in the
  16. * system is in its list of supported devices.
  17. */
  18. const struct of_device_id *of_match_device(const struct of_device_id *matches,
  19. const struct device *dev)
  20. {
  21. if ((!matches) || (!dev->of_node))
  22. return NULL;
  23. return of_match_node(matches, dev->of_node);
  24. }
  25. EXPORT_SYMBOL(of_match_device);
  26. struct platform_device *of_dev_get(struct platform_device *dev)
  27. {
  28. struct device *tmp;
  29. if (!dev)
  30. return NULL;
  31. tmp = get_device(&dev->dev);
  32. if (tmp)
  33. return to_platform_device(tmp);
  34. else
  35. return NULL;
  36. }
  37. EXPORT_SYMBOL(of_dev_get);
  38. void of_dev_put(struct platform_device *dev)
  39. {
  40. if (dev)
  41. put_device(&dev->dev);
  42. }
  43. EXPORT_SYMBOL(of_dev_put);
  44. static ssize_t devspec_show(struct device *dev,
  45. struct device_attribute *attr, char *buf)
  46. {
  47. struct platform_device *ofdev;
  48. ofdev = to_platform_device(dev);
  49. return sprintf(buf, "%s\n", ofdev->dev.of_node->full_name);
  50. }
  51. static ssize_t name_show(struct device *dev,
  52. struct device_attribute *attr, char *buf)
  53. {
  54. struct platform_device *ofdev;
  55. ofdev = to_platform_device(dev);
  56. return sprintf(buf, "%s\n", ofdev->dev.of_node->name);
  57. }
  58. static ssize_t modalias_show(struct device *dev,
  59. struct device_attribute *attr, char *buf)
  60. {
  61. ssize_t len = of_device_get_modalias(dev, buf, PAGE_SIZE - 2);
  62. buf[len] = '\n';
  63. buf[len+1] = 0;
  64. return len+1;
  65. }
  66. struct device_attribute of_platform_device_attrs[] = {
  67. __ATTR_RO(devspec),
  68. __ATTR_RO(name),
  69. __ATTR_RO(modalias),
  70. __ATTR_NULL
  71. };
  72. int of_device_add(struct platform_device *ofdev)
  73. {
  74. BUG_ON(ofdev->dev.of_node == NULL);
  75. /* name and id have to be set so that the platform bus doesn't get
  76. * confused on matching */
  77. ofdev->name = dev_name(&ofdev->dev);
  78. ofdev->id = -1;
  79. /* device_add will assume that this device is on the same node as
  80. * the parent. If there is no parent defined, set the node
  81. * explicitly */
  82. if (!ofdev->dev.parent)
  83. set_dev_node(&ofdev->dev, of_node_to_nid(ofdev->dev.of_node));
  84. return device_add(&ofdev->dev);
  85. }
  86. int of_device_register(struct platform_device *pdev)
  87. {
  88. device_initialize(&pdev->dev);
  89. return of_device_add(pdev);
  90. }
  91. EXPORT_SYMBOL(of_device_register);
  92. void of_device_unregister(struct platform_device *ofdev)
  93. {
  94. device_unregister(&ofdev->dev);
  95. }
  96. EXPORT_SYMBOL(of_device_unregister);
  97. ssize_t of_device_get_modalias(struct device *dev, char *str, ssize_t len)
  98. {
  99. const char *compat;
  100. int cplen, i;
  101. ssize_t tsize, csize, repend;
  102. /* Name & Type */
  103. csize = snprintf(str, len, "of:N%sT%s", dev->of_node->name,
  104. dev->of_node->type);
  105. /* Get compatible property if any */
  106. compat = of_get_property(dev->of_node, "compatible", &cplen);
  107. if (!compat)
  108. return csize;
  109. /* Find true end (we tolerate multiple \0 at the end */
  110. for (i = (cplen - 1); i >= 0 && !compat[i]; i--)
  111. cplen--;
  112. if (!cplen)
  113. return csize;
  114. cplen++;
  115. /* Check space (need cplen+1 chars including final \0) */
  116. tsize = csize + cplen;
  117. repend = tsize;
  118. if (csize >= len) /* @ the limit, all is already filled */
  119. return tsize;
  120. if (tsize >= len) { /* limit compat list */
  121. cplen = len - csize - 1;
  122. repend = len;
  123. }
  124. /* Copy and do char replacement */
  125. memcpy(&str[csize + 1], compat, cplen);
  126. for (i = csize; i < repend; i++) {
  127. char c = str[i];
  128. if (c == '\0')
  129. str[i] = 'C';
  130. else if (c == ' ')
  131. str[i] = '_';
  132. }
  133. return tsize;
  134. }
  135. /**
  136. * of_device_uevent - Display OF related uevent information
  137. */
  138. int of_device_uevent(struct device *dev, struct kobj_uevent_env *env)
  139. {
  140. const char *compat;
  141. int seen = 0, cplen, sl;
  142. if ((!dev) || (!dev->of_node))
  143. return -ENODEV;
  144. if (add_uevent_var(env, "OF_NAME=%s", dev->of_node->name))
  145. return -ENOMEM;
  146. if (add_uevent_var(env, "OF_TYPE=%s", dev->of_node->type))
  147. return -ENOMEM;
  148. /* Since the compatible field can contain pretty much anything
  149. * it's not really legal to split it out with commas. We split it
  150. * up using a number of environment variables instead. */
  151. compat = of_get_property(dev->of_node, "compatible", &cplen);
  152. while (compat && *compat && cplen > 0) {
  153. if (add_uevent_var(env, "OF_COMPATIBLE_%d=%s", seen, compat))
  154. return -ENOMEM;
  155. sl = strlen(compat) + 1;
  156. compat += sl;
  157. cplen -= sl;
  158. seen++;
  159. }
  160. if (add_uevent_var(env, "OF_COMPATIBLE_N=%d", seen))
  161. return -ENOMEM;
  162. /* modalias is trickier, we add it in 2 steps */
  163. if (add_uevent_var(env, "MODALIAS="))
  164. return -ENOMEM;
  165. sl = of_device_get_modalias(dev, &env->buf[env->buflen-1],
  166. sizeof(env->buf) - env->buflen);
  167. if (sl >= (sizeof(env->buf) - env->buflen))
  168. return -ENOMEM;
  169. env->buflen += sl;
  170. return 0;
  171. }