of_device.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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 <linux/of_device.h>
  9. #include <asm/errno.h>
  10. #include <asm/dcr.h>
  11. static void of_device_make_bus_id(struct of_device *dev)
  12. {
  13. static atomic_t bus_no_reg_magic;
  14. struct device_node *node = dev->node;
  15. char *name = dev->dev.bus_id;
  16. const u32 *reg;
  17. u64 addr;
  18. int magic;
  19. /*
  20. * If it's a DCR based device, use 'd' for native DCRs
  21. * and 'D' for MMIO DCRs.
  22. */
  23. #ifdef CONFIG_PPC_DCR
  24. reg = of_get_property(node, "dcr-reg", NULL);
  25. if (reg) {
  26. #ifdef CONFIG_PPC_DCR_NATIVE
  27. snprintf(name, BUS_ID_SIZE, "d%x.%s",
  28. *reg, node->name);
  29. #else /* CONFIG_PPC_DCR_NATIVE */
  30. addr = of_translate_dcr_address(node, *reg, NULL);
  31. if (addr != OF_BAD_ADDR) {
  32. snprintf(name, BUS_ID_SIZE,
  33. "D%llx.%s", (unsigned long long)addr,
  34. node->name);
  35. return;
  36. }
  37. #endif /* !CONFIG_PPC_DCR_NATIVE */
  38. }
  39. #endif /* CONFIG_PPC_DCR */
  40. /*
  41. * For MMIO, get the physical address
  42. */
  43. reg = of_get_property(node, "reg", NULL);
  44. if (reg) {
  45. addr = of_translate_address(node, reg);
  46. if (addr != OF_BAD_ADDR) {
  47. snprintf(name, BUS_ID_SIZE,
  48. "%llx.%s", (unsigned long long)addr,
  49. node->name);
  50. return;
  51. }
  52. }
  53. /*
  54. * No BusID, use the node name and add a globally incremented
  55. * counter (and pray...)
  56. */
  57. magic = atomic_add_return(1, &bus_no_reg_magic);
  58. snprintf(name, BUS_ID_SIZE, "%s.%d", node->name, magic - 1);
  59. }
  60. struct of_device *of_device_alloc(struct device_node *np,
  61. const char *bus_id,
  62. struct device *parent)
  63. {
  64. struct of_device *dev;
  65. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  66. if (!dev)
  67. return NULL;
  68. dev->node = of_node_get(np);
  69. dev->dev.dma_mask = &dev->dma_mask;
  70. dev->dev.parent = parent;
  71. dev->dev.release = of_release_dev;
  72. dev->dev.archdata.of_node = np;
  73. dev->dev.archdata.numa_node = of_node_to_nid(np);
  74. if (bus_id)
  75. strlcpy(dev->dev.bus_id, bus_id, BUS_ID_SIZE);
  76. else
  77. of_device_make_bus_id(dev);
  78. return dev;
  79. }
  80. EXPORT_SYMBOL(of_device_alloc);
  81. ssize_t of_device_get_modalias(struct of_device *ofdev,
  82. char *str, ssize_t len)
  83. {
  84. const char *compat;
  85. int cplen, i;
  86. ssize_t tsize, csize, repend;
  87. /* Name & Type */
  88. csize = snprintf(str, len, "of:N%sT%s",
  89. ofdev->node->name, ofdev->node->type);
  90. /* Get compatible property if any */
  91. compat = of_get_property(ofdev->node, "compatible", &cplen);
  92. if (!compat)
  93. return csize;
  94. /* Find true end (we tolerate multiple \0 at the end */
  95. for (i=(cplen-1); i>=0 && !compat[i]; i--)
  96. cplen--;
  97. if (!cplen)
  98. return csize;
  99. cplen++;
  100. /* Check space (need cplen+1 chars including final \0) */
  101. tsize = csize + cplen;
  102. repend = tsize;
  103. if (csize>=len) /* @ the limit, all is already filled */
  104. return tsize;
  105. if (tsize>=len) { /* limit compat list */
  106. cplen = len-csize-1;
  107. repend = len;
  108. }
  109. /* Copy and do char replacement */
  110. memcpy(&str[csize+1], compat, cplen);
  111. for (i=csize; i<repend; i++) {
  112. char c = str[i];
  113. if (c=='\0')
  114. str[i] = 'C';
  115. else if (c==' ')
  116. str[i] = '_';
  117. }
  118. return tsize;
  119. }
  120. int of_device_uevent(struct device *dev, struct kobj_uevent_env *env)
  121. {
  122. struct of_device *ofdev;
  123. const char *compat;
  124. int seen = 0, cplen, sl;
  125. if (!dev)
  126. return -ENODEV;
  127. ofdev = to_of_device(dev);
  128. if (add_uevent_var(env, "OF_NAME=%s", ofdev->node->name))
  129. return -ENOMEM;
  130. if (add_uevent_var(env, "OF_TYPE=%s", ofdev->node->type))
  131. return -ENOMEM;
  132. /* Since the compatible field can contain pretty much anything
  133. * it's not really legal to split it out with commas. We split it
  134. * up using a number of environment variables instead. */
  135. compat = of_get_property(ofdev->node, "compatible", &cplen);
  136. while (compat && *compat && cplen > 0) {
  137. if (add_uevent_var(env, "OF_COMPATIBLE_%d=%s", seen, compat))
  138. return -ENOMEM;
  139. sl = strlen (compat) + 1;
  140. compat += sl;
  141. cplen -= sl;
  142. seen++;
  143. }
  144. if (add_uevent_var(env, "OF_COMPATIBLE_N=%d", seen))
  145. return -ENOMEM;
  146. /* modalias is trickier, we add it in 2 steps */
  147. if (add_uevent_var(env, "MODALIAS="))
  148. return -ENOMEM;
  149. sl = of_device_get_modalias(ofdev, &env->buf[env->buflen-1],
  150. sizeof(env->buf) - env->buflen);
  151. if (sl >= (sizeof(env->buf) - env->buflen))
  152. return -ENOMEM;
  153. env->buflen += sl;
  154. return 0;
  155. }
  156. EXPORT_SYMBOL(of_device_uevent);
  157. EXPORT_SYMBOL(of_device_get_modalias);