of_device.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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, struct kobj_uevent_env *env)
  50. {
  51. struct of_device *ofdev;
  52. const char *compat;
  53. int seen = 0, cplen, sl;
  54. if (!dev)
  55. return -ENODEV;
  56. ofdev = to_of_device(dev);
  57. if (add_uevent_var(env, "OF_NAME=%s", ofdev->node->name))
  58. return -ENOMEM;
  59. if (add_uevent_var(env, "OF_TYPE=%s", ofdev->node->type))
  60. return -ENOMEM;
  61. /* Since the compatible field can contain pretty much anything
  62. * it's not really legal to split it out with commas. We split it
  63. * up using a number of environment variables instead. */
  64. compat = of_get_property(ofdev->node, "compatible", &cplen);
  65. while (compat && *compat && cplen > 0) {
  66. if (add_uevent_var(env, "OF_COMPATIBLE_%d=%s", seen, compat))
  67. return -ENOMEM;
  68. sl = strlen (compat) + 1;
  69. compat += sl;
  70. cplen -= sl;
  71. seen++;
  72. }
  73. if (add_uevent_var(env, "OF_COMPATIBLE_N=%d", seen))
  74. return -ENOMEM;
  75. /* modalias is trickier, we add it in 2 steps */
  76. if (add_uevent_var(env, "MODALIAS="))
  77. return -ENOMEM;
  78. sl = of_device_get_modalias(ofdev, &env->buf[env->buflen-1],
  79. sizeof(env->buf) - env->buflen);
  80. if (sl >= (sizeof(env->buf) - env->buflen))
  81. return -ENOMEM;
  82. env->buflen += sl;
  83. return 0;
  84. }
  85. EXPORT_SYMBOL(of_device_uevent);
  86. EXPORT_SYMBOL(of_device_get_modalias);