Paul Mackerras | 2e686bc | 2005-10-06 13:22:17 +1000 | [diff] [blame] | 1 | #include <linux/string.h> |
| 2 | #include <linux/kernel.h> |
Stephen Rothwell | f85ff30 | 2007-05-01 16:40:36 +1000 | [diff] [blame] | 3 | #include <linux/of.h> |
Paul Mackerras | 2e686bc | 2005-10-06 13:22:17 +1000 | [diff] [blame] | 4 | #include <linux/init.h> |
| 5 | #include <linux/module.h> |
| 6 | #include <linux/mod_devicetable.h> |
Paul Mackerras | 734d652 | 2005-10-31 13:57:01 +1100 | [diff] [blame] | 7 | #include <linux/slab.h> |
Jon Loeliger | 0173d42 | 2008-01-08 05:07:15 +1100 | [diff] [blame] | 8 | #include <linux/of_device.h> |
Paul Mackerras | 734d652 | 2005-10-31 13:57:01 +1100 | [diff] [blame] | 9 | |
Paul Mackerras | 2e686bc | 2005-10-06 13:22:17 +1000 | [diff] [blame] | 10 | #include <asm/errno.h> |
Joachim Fenkes | fec738d | 2007-09-26 19:44:12 +1000 | [diff] [blame] | 11 | #include <asm/dcr.h> |
Paul Mackerras | 2e686bc | 2005-10-06 13:22:17 +1000 | [diff] [blame] | 12 | |
Joachim Fenkes | fec738d | 2007-09-26 19:44:12 +1000 | [diff] [blame] | 13 | static void of_device_make_bus_id(struct of_device *dev) |
| 14 | { |
| 15 | static atomic_t bus_no_reg_magic; |
| 16 | struct device_node *node = dev->node; |
| 17 | char *name = dev->dev.bus_id; |
| 18 | const u32 *reg; |
| 19 | u64 addr; |
| 20 | int magic; |
| 21 | |
| 22 | /* |
| 23 | * If it's a DCR based device, use 'd' for native DCRs |
| 24 | * and 'D' for MMIO DCRs. |
| 25 | */ |
| 26 | #ifdef CONFIG_PPC_DCR |
| 27 | reg = of_get_property(node, "dcr-reg", NULL); |
| 28 | if (reg) { |
| 29 | #ifdef CONFIG_PPC_DCR_NATIVE |
| 30 | snprintf(name, BUS_ID_SIZE, "d%x.%s", |
| 31 | *reg, node->name); |
| 32 | #else /* CONFIG_PPC_DCR_NATIVE */ |
| 33 | addr = of_translate_dcr_address(node, *reg, NULL); |
| 34 | if (addr != OF_BAD_ADDR) { |
| 35 | snprintf(name, BUS_ID_SIZE, |
| 36 | "D%llx.%s", (unsigned long long)addr, |
| 37 | node->name); |
| 38 | return; |
| 39 | } |
| 40 | #endif /* !CONFIG_PPC_DCR_NATIVE */ |
| 41 | } |
| 42 | #endif /* CONFIG_PPC_DCR */ |
| 43 | |
| 44 | /* |
| 45 | * For MMIO, get the physical address |
| 46 | */ |
| 47 | reg = of_get_property(node, "reg", NULL); |
| 48 | if (reg) { |
| 49 | addr = of_translate_address(node, reg); |
| 50 | if (addr != OF_BAD_ADDR) { |
| 51 | snprintf(name, BUS_ID_SIZE, |
| 52 | "%llx.%s", (unsigned long long)addr, |
| 53 | node->name); |
| 54 | return; |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | /* |
| 59 | * No BusID, use the node name and add a globally incremented |
| 60 | * counter (and pray...) |
| 61 | */ |
| 62 | magic = atomic_add_return(1, &bus_no_reg_magic); |
| 63 | snprintf(name, BUS_ID_SIZE, "%s.%d", node->name, magic - 1); |
| 64 | } |
| 65 | |
| 66 | struct of_device *of_device_alloc(struct device_node *np, |
| 67 | const char *bus_id, |
| 68 | struct device *parent) |
| 69 | { |
| 70 | struct of_device *dev; |
| 71 | |
| 72 | dev = kzalloc(sizeof(*dev), GFP_KERNEL); |
| 73 | if (!dev) |
| 74 | return NULL; |
| 75 | |
| 76 | dev->node = of_node_get(np); |
| 77 | dev->dev.dma_mask = &dev->dma_mask; |
| 78 | dev->dev.parent = parent; |
| 79 | dev->dev.release = of_release_dev; |
| 80 | dev->dev.archdata.of_node = np; |
| 81 | dev->dev.archdata.numa_node = of_node_to_nid(np); |
| 82 | |
| 83 | if (bus_id) |
| 84 | strlcpy(dev->dev.bus_id, bus_id, BUS_ID_SIZE); |
| 85 | else |
| 86 | of_device_make_bus_id(dev); |
| 87 | |
| 88 | return dev; |
| 89 | } |
| 90 | EXPORT_SYMBOL(of_device_alloc); |
| 91 | |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 92 | int of_device_uevent(struct device *dev, struct kobj_uevent_env *env) |
Sylvain Munaut | eb0cb8a | 2007-02-12 23:13:25 +0100 | [diff] [blame] | 93 | { |
| 94 | struct of_device *ofdev; |
| 95 | const char *compat; |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 96 | int seen = 0, cplen, sl; |
Sylvain Munaut | eb0cb8a | 2007-02-12 23:13:25 +0100 | [diff] [blame] | 97 | |
| 98 | if (!dev) |
| 99 | return -ENODEV; |
| 100 | |
| 101 | ofdev = to_of_device(dev); |
| 102 | |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 103 | if (add_uevent_var(env, "OF_NAME=%s", ofdev->node->name)) |
Sylvain Munaut | eb0cb8a | 2007-02-12 23:13:25 +0100 | [diff] [blame] | 104 | return -ENOMEM; |
| 105 | |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 106 | if (add_uevent_var(env, "OF_TYPE=%s", ofdev->node->type)) |
Sylvain Munaut | eb0cb8a | 2007-02-12 23:13:25 +0100 | [diff] [blame] | 107 | return -ENOMEM; |
| 108 | |
| 109 | /* Since the compatible field can contain pretty much anything |
| 110 | * it's not really legal to split it out with commas. We split it |
| 111 | * up using a number of environment variables instead. */ |
| 112 | |
Stephen Rothwell | b3a6d2a | 2007-04-13 17:14:22 +1000 | [diff] [blame] | 113 | compat = of_get_property(ofdev->node, "compatible", &cplen); |
Sylvain Munaut | eb0cb8a | 2007-02-12 23:13:25 +0100 | [diff] [blame] | 114 | while (compat && *compat && cplen > 0) { |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 115 | if (add_uevent_var(env, "OF_COMPATIBLE_%d=%s", seen, compat)) |
Sylvain Munaut | eb0cb8a | 2007-02-12 23:13:25 +0100 | [diff] [blame] | 116 | return -ENOMEM; |
| 117 | |
| 118 | sl = strlen (compat) + 1; |
| 119 | compat += sl; |
| 120 | cplen -= sl; |
| 121 | seen++; |
| 122 | } |
| 123 | |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 124 | if (add_uevent_var(env, "OF_COMPATIBLE_N=%d", seen)) |
Sylvain Munaut | eb0cb8a | 2007-02-12 23:13:25 +0100 | [diff] [blame] | 125 | return -ENOMEM; |
| 126 | |
| 127 | /* modalias is trickier, we add it in 2 steps */ |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 128 | if (add_uevent_var(env, "MODALIAS=")) |
Sylvain Munaut | eb0cb8a | 2007-02-12 23:13:25 +0100 | [diff] [blame] | 129 | return -ENOMEM; |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 130 | sl = of_device_get_modalias(ofdev, &env->buf[env->buflen-1], |
| 131 | sizeof(env->buf) - env->buflen); |
| 132 | if (sl >= (sizeof(env->buf) - env->buflen)) |
Sylvain Munaut | eb0cb8a | 2007-02-12 23:13:25 +0100 | [diff] [blame] | 133 | return -ENOMEM; |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 134 | env->buflen += sl; |
Sylvain Munaut | eb0cb8a | 2007-02-12 23:13:25 +0100 | [diff] [blame] | 135 | |
| 136 | return 0; |
| 137 | } |
Sylvain Munaut | eb0cb8a | 2007-02-12 23:13:25 +0100 | [diff] [blame] | 138 | EXPORT_SYMBOL(of_device_uevent); |
Sylvain Munaut | de41189 | 2007-05-07 01:38:46 +1000 | [diff] [blame] | 139 | EXPORT_SYMBOL(of_device_get_modalias); |