of_device.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. int of_device_uevent(struct device *dev, struct kobj_uevent_env *env)
  82. {
  83. struct of_device *ofdev;
  84. const char *compat;
  85. int seen = 0, cplen, sl;
  86. if (!dev)
  87. return -ENODEV;
  88. ofdev = to_of_device(dev);
  89. if (add_uevent_var(env, "OF_NAME=%s", ofdev->node->name))
  90. return -ENOMEM;
  91. if (add_uevent_var(env, "OF_TYPE=%s", ofdev->node->type))
  92. return -ENOMEM;
  93. /* Since the compatible field can contain pretty much anything
  94. * it's not really legal to split it out with commas. We split it
  95. * up using a number of environment variables instead. */
  96. compat = of_get_property(ofdev->node, "compatible", &cplen);
  97. while (compat && *compat && cplen > 0) {
  98. if (add_uevent_var(env, "OF_COMPATIBLE_%d=%s", seen, compat))
  99. return -ENOMEM;
  100. sl = strlen (compat) + 1;
  101. compat += sl;
  102. cplen -= sl;
  103. seen++;
  104. }
  105. if (add_uevent_var(env, "OF_COMPATIBLE_N=%d", seen))
  106. return -ENOMEM;
  107. /* modalias is trickier, we add it in 2 steps */
  108. if (add_uevent_var(env, "MODALIAS="))
  109. return -ENOMEM;
  110. sl = of_device_get_modalias(ofdev, &env->buf[env->buflen-1],
  111. sizeof(env->buf) - env->buflen);
  112. if (sl >= (sizeof(env->buf) - env->buflen))
  113. return -ENOMEM;
  114. env->buflen += sl;
  115. return 0;
  116. }
  117. EXPORT_SYMBOL(of_device_uevent);
  118. EXPORT_SYMBOL(of_device_get_modalias);