of_device.c 2.6 KB

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