KVM: Prevent kvm_init from corrupting debugfs structures
[linux-2.6/verdex.git] / arch / microblaze / kernel / of_platform.c
blobacf4574d0f188fd7fd99fec2ab911b8a4d0e446e
1 /*
2 * Copyright (C) 2006 Benjamin Herrenschmidt, IBM Corp.
3 * <benh@kernel.crashing.org>
4 * and Arnd Bergmann, IBM Corp.
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
13 #undef DEBUG
15 #include <linux/string.h>
16 #include <linux/kernel.h>
17 #include <linux/init.h>
18 #include <linux/module.h>
19 #include <linux/mod_devicetable.h>
20 #include <linux/slab.h>
21 #include <linux/pci.h>
22 #include <linux/of.h>
23 #include <linux/of_device.h>
24 #include <linux/of_platform.h>
26 #include <linux/errno.h>
27 #include <linux/topology.h>
28 #include <asm/atomic.h>
30 struct bus_type of_platform_bus_type = {
31 .uevent = of_device_uevent,
33 EXPORT_SYMBOL(of_platform_bus_type);
35 static int __init of_bus_driver_init(void)
37 return of_bus_type_init(&of_platform_bus_type, "of_platform");
39 postcore_initcall(of_bus_driver_init);
41 struct of_device *of_platform_device_create(struct device_node *np,
42 const char *bus_id,
43 struct device *parent)
45 struct of_device *dev;
47 dev = of_device_alloc(np, bus_id, parent);
48 if (!dev)
49 return NULL;
51 dev->dma_mask = 0xffffffffUL;
52 dev->dev.bus = &of_platform_bus_type;
54 /* We do not fill the DMA ops for platform devices by default.
55 * This is currently the responsibility of the platform code
56 * to do such, possibly using a device notifier
59 if (of_device_register(dev) != 0) {
60 of_device_free(dev);
61 return NULL;
64 return dev;
66 EXPORT_SYMBOL(of_platform_device_create);
68 /**
69 * of_platform_bus_create - Create an OF device for a bus node and all its
70 * children. Optionally recursively instanciate matching busses.
71 * @bus: device node of the bus to instanciate
72 * @matches: match table, NULL to use the default, OF_NO_DEEP_PROBE to
73 * disallow recursive creation of child busses
75 static int of_platform_bus_create(const struct device_node *bus,
76 const struct of_device_id *matches,
77 struct device *parent)
79 struct device_node *child;
80 struct of_device *dev;
81 int rc = 0;
83 for_each_child_of_node(bus, child) {
84 pr_debug(" create child: %s\n", child->full_name);
85 dev = of_platform_device_create(child, NULL, parent);
86 if (dev == NULL)
87 rc = -ENOMEM;
88 else if (!of_match_node(matches, child))
89 continue;
90 if (rc == 0) {
91 pr_debug(" and sub busses\n");
92 rc = of_platform_bus_create(child, matches, &dev->dev);
94 if (rc) {
95 of_node_put(child);
96 break;
99 return rc;
104 * of_platform_bus_probe - Probe the device-tree for platform busses
105 * @root: parent of the first level to probe or NULL for the root of the tree
106 * @matches: match table, NULL to use the default
107 * @parent: parent to hook devices from, NULL for toplevel
109 * Note that children of the provided root are not instanciated as devices
110 * unless the specified root itself matches the bus list and is not NULL.
113 int of_platform_bus_probe(struct device_node *root,
114 const struct of_device_id *matches,
115 struct device *parent)
117 struct device_node *child;
118 struct of_device *dev;
119 int rc = 0;
121 if (matches == NULL)
122 matches = of_default_bus_ids;
123 if (matches == OF_NO_DEEP_PROBE)
124 return -EINVAL;
125 if (root == NULL)
126 root = of_find_node_by_path("/");
127 else
128 of_node_get(root);
130 pr_debug("of_platform_bus_probe()\n");
131 pr_debug(" starting at: %s\n", root->full_name);
133 /* Do a self check of bus type, if there's a match, create
134 * children
136 if (of_match_node(matches, root)) {
137 pr_debug(" root match, create all sub devices\n");
138 dev = of_platform_device_create(root, NULL, parent);
139 if (dev == NULL) {
140 rc = -ENOMEM;
141 goto bail;
143 pr_debug(" create all sub busses\n");
144 rc = of_platform_bus_create(root, matches, &dev->dev);
145 goto bail;
147 for_each_child_of_node(root, child) {
148 if (!of_match_node(matches, child))
149 continue;
151 pr_debug(" match: %s\n", child->full_name);
152 dev = of_platform_device_create(child, NULL, parent);
153 if (dev == NULL)
154 rc = -ENOMEM;
155 else
156 rc = of_platform_bus_create(child, matches, &dev->dev);
157 if (rc) {
158 of_node_put(child);
159 break;
162 bail:
163 of_node_put(root);
164 return rc;
166 EXPORT_SYMBOL(of_platform_bus_probe);
168 static int of_dev_node_match(struct device *dev, void *data)
170 return to_of_device(dev)->node == data;
173 struct of_device *of_find_device_by_node(struct device_node *np)
175 struct device *dev;
177 dev = bus_find_device(&of_platform_bus_type,
178 NULL, np, of_dev_node_match);
179 if (dev)
180 return to_of_device(dev);
181 return NULL;
183 EXPORT_SYMBOL(of_find_device_by_node);
185 static int of_dev_phandle_match(struct device *dev, void *data)
187 phandle *ph = data;
188 return to_of_device(dev)->node->linux_phandle == *ph;
191 struct of_device *of_find_device_by_phandle(phandle ph)
193 struct device *dev;
195 dev = bus_find_device(&of_platform_bus_type,
196 NULL, &ph, of_dev_phandle_match);
197 if (dev)
198 return to_of_device(dev);
199 return NULL;
201 EXPORT_SYMBOL(of_find_device_by_phandle);