of_device.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. #include <linux/string.h>
  2. #include <linux/kernel.h>
  3. #include <linux/of.h>
  4. #include <linux/init.h>
  5. #include <linux/module.h>
  6. #include <linux/mod_devicetable.h>
  7. #include <linux/slab.h>
  8. #include <asm/errno.h>
  9. #include <asm/of_device.h>
  10. ssize_t of_device_get_modalias(struct of_device *ofdev,
  11. char *str, ssize_t len)
  12. {
  13. const char *compat;
  14. int cplen, i;
  15. ssize_t tsize, csize, repend;
  16. /* Name & Type */
  17. csize = snprintf(str, len, "of:N%sT%s",
  18. ofdev->node->name, ofdev->node->type);
  19. /* Get compatible property if any */
  20. compat = of_get_property(ofdev->node, "compatible", &cplen);
  21. if (!compat)
  22. return csize;
  23. /* Find true end (we tolerate multiple \0 at the end */
  24. for (i=(cplen-1); i>=0 && !compat[i]; i--)
  25. cplen--;
  26. if (!cplen)
  27. return csize;
  28. cplen++;
  29. /* Check space (need cplen+1 chars including final \0) */
  30. tsize = csize + cplen;
  31. repend = tsize;
  32. if (csize>=len) /* @ the limit, all is already filled */
  33. return tsize;
  34. if (tsize>=len) { /* limit compat list */
  35. cplen = len-csize-1;
  36. repend = len;
  37. }
  38. /* Copy and do char replacement */
  39. memcpy(&str[csize+1], compat, cplen);
  40. for (i=csize; i<repend; i++) {
  41. char c = str[i];
  42. if (c=='\0')
  43. str[i] = 'C';
  44. else if (c==' ')
  45. str[i] = '_';
  46. }
  47. return tsize;
  48. }
  49. int of_device_uevent(struct device *dev,
  50. char **envp, int num_envp, char *buffer, int buffer_size)
  51. {
  52. struct of_device *ofdev;
  53. const char *compat;
  54. int i = 0, length = 0, seen = 0, cplen, sl;
  55. if (!dev)
  56. return -ENODEV;
  57. ofdev = to_of_device(dev);
  58. if (add_uevent_var(envp, num_envp, &i,
  59. buffer, buffer_size, &length,
  60. "OF_NAME=%s", ofdev->node->name))
  61. return -ENOMEM;
  62. if (add_uevent_var(envp, num_envp, &i,
  63. buffer, buffer_size, &length,
  64. "OF_TYPE=%s", ofdev->node->type))
  65. return -ENOMEM;
  66. /* Since the compatible field can contain pretty much anything
  67. * it's not really legal to split it out with commas. We split it
  68. * up using a number of environment variables instead. */
  69. compat = of_get_property(ofdev->node, "compatible", &cplen);
  70. while (compat && *compat && cplen > 0) {
  71. if (add_uevent_var(envp, num_envp, &i,
  72. buffer, buffer_size, &length,
  73. "OF_COMPATIBLE_%d=%s", seen, compat))
  74. return -ENOMEM;
  75. sl = strlen (compat) + 1;
  76. compat += sl;
  77. cplen -= sl;
  78. seen++;
  79. }
  80. if (add_uevent_var(envp, num_envp, &i,
  81. buffer, buffer_size, &length,
  82. "OF_COMPATIBLE_N=%d", seen))
  83. return -ENOMEM;
  84. /* modalias is trickier, we add it in 2 steps */
  85. if (add_uevent_var(envp, num_envp, &i,
  86. buffer, buffer_size, &length,
  87. "MODALIAS="))
  88. return -ENOMEM;
  89. sl = of_device_get_modalias(ofdev, &buffer[length-1],
  90. buffer_size-length);
  91. if (sl >= (buffer_size-length))
  92. return -ENOMEM;
  93. length += sl;
  94. envp[i] = NULL;
  95. return 0;
  96. }
  97. EXPORT_SYMBOL(of_device_uevent);
  98. EXPORT_SYMBOL(of_device_get_modalias);