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