of_device.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. const u32 *reg;
  16. u64 addr;
  17. int magic;
  18. /*
  19. * If it's a DCR based device, use 'd' for native DCRs
  20. * and 'D' for MMIO DCRs.
  21. */
  22. #ifdef CONFIG_PPC_DCR
  23. reg = of_get_property(node, "dcr-reg", NULL);
  24. if (reg) {
  25. #ifdef CONFIG_PPC_DCR_NATIVE
  26. dev_set_name(&dev->dev, "d%x.%s", *reg, node->name);
  27. #else /* CONFIG_PPC_DCR_NATIVE */
  28. addr = of_translate_dcr_address(node, *reg, NULL);
  29. if (addr != OF_BAD_ADDR) {
  30. dev_set_name(&dev->dev, "D%llx.%s",
  31. (unsigned long long)addr, node->name);
  32. return;
  33. }
  34. #endif /* !CONFIG_PPC_DCR_NATIVE */
  35. }
  36. #endif /* CONFIG_PPC_DCR */
  37. /*
  38. * For MMIO, get the physical address
  39. */
  40. reg = of_get_property(node, "reg", NULL);
  41. if (reg) {
  42. addr = of_translate_address(node, reg);
  43. if (addr != OF_BAD_ADDR) {
  44. dev_set_name(&dev->dev, "%llx.%s",
  45. (unsigned long long)addr, node->name);
  46. return;
  47. }
  48. }
  49. /*
  50. * No BusID, use the node name and add a globally incremented
  51. * counter (and pray...)
  52. */
  53. magic = atomic_add_return(1, &bus_no_reg_magic);
  54. dev_set_name(&dev->dev, "%s.%d", node->name, magic - 1);
  55. }
  56. struct of_device *of_device_alloc(struct device_node *np,
  57. const char *bus_id,
  58. struct device *parent)
  59. {
  60. struct of_device *dev;
  61. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  62. if (!dev)
  63. return NULL;
  64. dev->node = of_node_get(np);
  65. dev->dev.dma_mask = &dev->dma_mask;
  66. dev->dev.parent = parent;
  67. dev->dev.release = of_release_dev;
  68. dev->dev.archdata.of_node = np;
  69. if (bus_id)
  70. dev_set_name(&dev->dev, "%s", bus_id);
  71. else
  72. of_device_make_bus_id(dev);
  73. return dev;
  74. }
  75. EXPORT_SYMBOL(of_device_alloc);
  76. int of_device_uevent(struct device *dev, struct kobj_uevent_env *env)
  77. {
  78. struct of_device *ofdev;
  79. const char *compat;
  80. int seen = 0, cplen, sl;
  81. if (!dev)
  82. return -ENODEV;
  83. ofdev = to_of_device(dev);
  84. if (add_uevent_var(env, "OF_NAME=%s", ofdev->node->name))
  85. return -ENOMEM;
  86. if (add_uevent_var(env, "OF_TYPE=%s", ofdev->node->type))
  87. return -ENOMEM;
  88. /* Since the compatible field can contain pretty much anything
  89. * it's not really legal to split it out with commas. We split it
  90. * up using a number of environment variables instead. */
  91. compat = of_get_property(ofdev->node, "compatible", &cplen);
  92. while (compat && *compat && cplen > 0) {
  93. if (add_uevent_var(env, "OF_COMPATIBLE_%d=%s", seen, compat))
  94. return -ENOMEM;
  95. sl = strlen (compat) + 1;
  96. compat += sl;
  97. cplen -= sl;
  98. seen++;
  99. }
  100. if (add_uevent_var(env, "OF_COMPATIBLE_N=%d", seen))
  101. return -ENOMEM;
  102. /* modalias is trickier, we add it in 2 steps */
  103. if (add_uevent_var(env, "MODALIAS="))
  104. return -ENOMEM;
  105. sl = of_device_get_modalias(ofdev, &env->buf[env->buflen-1],
  106. sizeof(env->buf) - env->buflen);
  107. if (sl >= (sizeof(env->buf) - env->buflen))
  108. return -ENOMEM;
  109. env->buflen += sl;
  110. return 0;
  111. }
  112. EXPORT_SYMBOL(of_device_uevent);
  113. EXPORT_SYMBOL(of_device_get_modalias);