of_device.c 2.6 KB

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