device.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. /**
  73. * of_release_dev - free an of device structure when all users of it are finished.
  74. * @dev: device that's been disconnected
  75. *
  76. * Will be called only by the device core when all users of this of device are
  77. * done.
  78. */
  79. void of_release_dev(struct device *dev)
  80. {
  81. struct platform_device *ofdev;
  82. ofdev = to_platform_device(dev);
  83. of_node_put(ofdev->dev.of_node);
  84. kfree(ofdev);
  85. }
  86. EXPORT_SYMBOL(of_release_dev);
  87. int of_device_register(struct platform_device *ofdev)
  88. {
  89. BUG_ON(ofdev->dev.of_node == NULL);
  90. device_initialize(&ofdev->dev);
  91. /* name and id have to be set so that the platform bus doesn't get
  92. * confused on matching */
  93. ofdev->name = dev_name(&ofdev->dev);
  94. ofdev->id = -1;
  95. /* device_add will assume that this device is on the same node as
  96. * the parent. If there is no parent defined, set the node
  97. * explicitly */
  98. if (!ofdev->dev.parent)
  99. set_dev_node(&ofdev->dev, of_node_to_nid(ofdev->dev.of_node));
  100. return device_add(&ofdev->dev);
  101. }
  102. EXPORT_SYMBOL(of_device_register);
  103. void of_device_unregister(struct platform_device *ofdev)
  104. {
  105. device_unregister(&ofdev->dev);
  106. }
  107. EXPORT_SYMBOL(of_device_unregister);
  108. ssize_t of_device_get_modalias(struct device *dev, char *str, ssize_t len)
  109. {
  110. const char *compat;
  111. int cplen, i;
  112. ssize_t tsize, csize, repend;
  113. /* Name & Type */
  114. csize = snprintf(str, len, "of:N%sT%s", dev->of_node->name,
  115. dev->of_node->type);
  116. /* Get compatible property if any */
  117. compat = of_get_property(dev->of_node, "compatible", &cplen);
  118. if (!compat)
  119. return csize;
  120. /* Find true end (we tolerate multiple \0 at the end */
  121. for (i = (cplen - 1); i >= 0 && !compat[i]; i--)
  122. cplen--;
  123. if (!cplen)
  124. return csize;
  125. cplen++;
  126. /* Check space (need cplen+1 chars including final \0) */
  127. tsize = csize + cplen;
  128. repend = tsize;
  129. if (csize >= len) /* @ the limit, all is already filled */
  130. return tsize;
  131. if (tsize >= len) { /* limit compat list */
  132. cplen = len - csize - 1;
  133. repend = len;
  134. }
  135. /* Copy and do char replacement */
  136. memcpy(&str[csize + 1], compat, cplen);
  137. for (i = csize; i < repend; i++) {
  138. char c = str[i];
  139. if (c == '\0')
  140. str[i] = 'C';
  141. else if (c == ' ')
  142. str[i] = '_';
  143. }
  144. return tsize;
  145. }
  146. /**
  147. * of_device_uevent - Display OF related uevent information
  148. */
  149. int of_device_uevent(struct device *dev, struct kobj_uevent_env *env)
  150. {
  151. const char *compat;
  152. int seen = 0, cplen, sl;
  153. if ((!dev) || (!dev->of_node))
  154. return -ENODEV;
  155. if (add_uevent_var(env, "OF_NAME=%s", dev->of_node->name))
  156. return -ENOMEM;
  157. if (add_uevent_var(env, "OF_TYPE=%s", dev->of_node->type))
  158. return -ENOMEM;
  159. /* Since the compatible field can contain pretty much anything
  160. * it's not really legal to split it out with commas. We split it
  161. * up using a number of environment variables instead. */
  162. compat = of_get_property(dev->of_node, "compatible", &cplen);
  163. while (compat && *compat && cplen > 0) {
  164. if (add_uevent_var(env, "OF_COMPATIBLE_%d=%s", seen, compat))
  165. return -ENOMEM;
  166. sl = strlen(compat) + 1;
  167. compat += sl;
  168. cplen -= sl;
  169. seen++;
  170. }
  171. if (add_uevent_var(env, "OF_COMPATIBLE_N=%d", seen))
  172. return -ENOMEM;
  173. /* modalias is trickier, we add it in 2 steps */
  174. if (add_uevent_var(env, "MODALIAS="))
  175. return -ENOMEM;
  176. sl = of_device_get_modalias(dev, &env->buf[env->buflen-1],
  177. sizeof(env->buf) - env->buflen);
  178. if (sl >= (sizeof(env->buf) - env->buflen))
  179. return -ENOMEM;
  180. env->buflen += sl;
  181. return 0;
  182. }