device.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. int of_device_add(struct platform_device *ofdev)
  45. {
  46. BUG_ON(ofdev->dev.of_node == NULL);
  47. /* name and id have to be set so that the platform bus doesn't get
  48. * confused on matching */
  49. ofdev->name = dev_name(&ofdev->dev);
  50. ofdev->id = -1;
  51. /* device_add will assume that this device is on the same node as
  52. * the parent. If there is no parent defined, set the node
  53. * explicitly */
  54. if (!ofdev->dev.parent)
  55. set_dev_node(&ofdev->dev, of_node_to_nid(ofdev->dev.of_node));
  56. return device_add(&ofdev->dev);
  57. }
  58. int of_device_register(struct platform_device *pdev)
  59. {
  60. device_initialize(&pdev->dev);
  61. return of_device_add(pdev);
  62. }
  63. EXPORT_SYMBOL(of_device_register);
  64. void of_device_unregister(struct platform_device *ofdev)
  65. {
  66. device_unregister(&ofdev->dev);
  67. }
  68. EXPORT_SYMBOL(of_device_unregister);
  69. ssize_t of_device_get_modalias(struct device *dev, char *str, ssize_t len)
  70. {
  71. const char *compat;
  72. int cplen, i;
  73. ssize_t tsize, csize, repend;
  74. /* Name & Type */
  75. csize = snprintf(str, len, "of:N%sT%s", dev->of_node->name,
  76. dev->of_node->type);
  77. /* Get compatible property if any */
  78. compat = of_get_property(dev->of_node, "compatible", &cplen);
  79. if (!compat)
  80. return csize;
  81. /* Find true end (we tolerate multiple \0 at the end */
  82. for (i = (cplen - 1); i >= 0 && !compat[i]; i--)
  83. cplen--;
  84. if (!cplen)
  85. return csize;
  86. cplen++;
  87. /* Check space (need cplen+1 chars including final \0) */
  88. tsize = csize + cplen;
  89. repend = tsize;
  90. if (csize >= len) /* @ the limit, all is already filled */
  91. return tsize;
  92. if (tsize >= len) { /* limit compat list */
  93. cplen = len - csize - 1;
  94. repend = len;
  95. }
  96. /* Copy and do char replacement */
  97. memcpy(&str[csize + 1], compat, cplen);
  98. for (i = csize; i < repend; i++) {
  99. char c = str[i];
  100. if (c == '\0')
  101. str[i] = 'C';
  102. else if (c == ' ')
  103. str[i] = '_';
  104. }
  105. return tsize;
  106. }
  107. /**
  108. * of_device_uevent - Display OF related uevent information
  109. */
  110. int of_device_uevent(struct device *dev, struct kobj_uevent_env *env)
  111. {
  112. const char *compat;
  113. int seen = 0, cplen, sl;
  114. if ((!dev) || (!dev->of_node))
  115. return -ENODEV;
  116. if (add_uevent_var(env, "OF_NAME=%s", dev->of_node->name))
  117. return -ENOMEM;
  118. if (add_uevent_var(env, "OF_TYPE=%s", dev->of_node->type))
  119. return -ENOMEM;
  120. /* Since the compatible field can contain pretty much anything
  121. * it's not really legal to split it out with commas. We split it
  122. * up using a number of environment variables instead. */
  123. compat = of_get_property(dev->of_node, "compatible", &cplen);
  124. while (compat && *compat && cplen > 0) {
  125. if (add_uevent_var(env, "OF_COMPATIBLE_%d=%s", seen, compat))
  126. return -ENOMEM;
  127. sl = strlen(compat) + 1;
  128. compat += sl;
  129. cplen -= sl;
  130. seen++;
  131. }
  132. if (add_uevent_var(env, "OF_COMPATIBLE_N=%d", seen))
  133. return -ENOMEM;
  134. /* modalias is trickier, we add it in 2 steps */
  135. if (add_uevent_var(env, "MODALIAS="))
  136. return -ENOMEM;
  137. sl = of_device_get_modalias(dev, &env->buf[env->buflen-1],
  138. sizeof(env->buf) - env->buflen);
  139. if (sl >= (sizeof(env->buf) - env->buflen))
  140. return -ENOMEM;
  141. env->buflen += sl;
  142. return 0;
  143. }