Linux-2.6.12-rc2
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / arch / ppc / syslib / of_device.c
blob46269ed21aee77eaab1d784ec4e57910c3b6adc1
1 #include <linux/config.h>
2 #include <linux/string.h>
3 #include <linux/kernel.h>
4 #include <linux/init.h>
5 #include <linux/module.h>
6 #include <asm/errno.h>
7 #include <asm/of_device.h>
9 /**
10 * of_match_device - Tell if an of_device structure has a matching
11 * of_match structure
12 * @ids: array of of device match structures to search in
13 * @dev: the of device structure to match against
15 * Used by a driver to check whether an of_device present in the
16 * system is in its list of supported devices.
18 const struct of_match * of_match_device(const struct of_match *matches,
19 const struct of_device *dev)
21 if (!dev->node)
22 return NULL;
23 while (matches->name || matches->type || matches->compatible) {
24 int match = 1;
25 if (matches->name && matches->name != OF_ANY_MATCH)
26 match &= dev->node->name
27 && !strcmp(matches->name, dev->node->name);
28 if (matches->type && matches->type != OF_ANY_MATCH)
29 match &= dev->node->type
30 && !strcmp(matches->type, dev->node->type);
31 if (matches->compatible && matches->compatible != OF_ANY_MATCH)
32 match &= device_is_compatible(dev->node,
33 matches->compatible);
34 if (match)
35 return matches;
36 matches++;
38 return NULL;
41 static int of_platform_bus_match(struct device *dev, struct device_driver *drv)
43 struct of_device * of_dev = to_of_device(dev);
44 struct of_platform_driver * of_drv = to_of_platform_driver(drv);
45 const struct of_match * matches = of_drv->match_table;
47 if (!matches)
48 return 0;
50 return of_match_device(matches, of_dev) != NULL;
53 struct of_device *of_dev_get(struct of_device *dev)
55 struct device *tmp;
57 if (!dev)
58 return NULL;
59 tmp = get_device(&dev->dev);
60 if (tmp)
61 return to_of_device(tmp);
62 else
63 return NULL;
66 void of_dev_put(struct of_device *dev)
68 if (dev)
69 put_device(&dev->dev);
73 static int of_device_probe(struct device *dev)
75 int error = -ENODEV;
76 struct of_platform_driver *drv;
77 struct of_device *of_dev;
78 const struct of_match *match;
80 drv = to_of_platform_driver(dev->driver);
81 of_dev = to_of_device(dev);
83 if (!drv->probe)
84 return error;
86 of_dev_get(of_dev);
88 match = of_match_device(drv->match_table, of_dev);
89 if (match)
90 error = drv->probe(of_dev, match);
91 if (error)
92 of_dev_put(of_dev);
94 return error;
97 static int of_device_remove(struct device *dev)
99 struct of_device * of_dev = to_of_device(dev);
100 struct of_platform_driver * drv = to_of_platform_driver(dev->driver);
102 if (dev->driver && drv->remove)
103 drv->remove(of_dev);
104 return 0;
107 static int of_device_suspend(struct device *dev, u32 state)
109 struct of_device * of_dev = to_of_device(dev);
110 struct of_platform_driver * drv = to_of_platform_driver(dev->driver);
111 int error = 0;
113 if (dev->driver && drv->suspend)
114 error = drv->suspend(of_dev, state);
115 return error;
118 static int of_device_resume(struct device * dev)
120 struct of_device * of_dev = to_of_device(dev);
121 struct of_platform_driver * drv = to_of_platform_driver(dev->driver);
122 int error = 0;
124 if (dev->driver && drv->resume)
125 error = drv->resume(of_dev);
126 return error;
129 struct bus_type of_platform_bus_type = {
130 .name = "of_platform",
131 .match = of_platform_bus_match,
132 .suspend = of_device_suspend,
133 .resume = of_device_resume,
136 static int __init of_bus_driver_init(void)
138 return bus_register(&of_platform_bus_type);
141 postcore_initcall(of_bus_driver_init);
143 int of_register_driver(struct of_platform_driver *drv)
145 int count = 0;
147 /* initialize common driver fields */
148 drv->driver.name = drv->name;
149 drv->driver.bus = &of_platform_bus_type;
150 drv->driver.probe = of_device_probe;
151 drv->driver.remove = of_device_remove;
153 /* register with core */
154 count = driver_register(&drv->driver);
155 return count ? count : 1;
158 void of_unregister_driver(struct of_platform_driver *drv)
160 driver_unregister(&drv->driver);
164 static ssize_t dev_show_devspec(struct device *dev, char *buf)
166 struct of_device *ofdev;
168 ofdev = to_of_device(dev);
169 return sprintf(buf, "%s", ofdev->node->full_name);
172 static DEVICE_ATTR(devspec, S_IRUGO, dev_show_devspec, NULL);
175 * of_release_dev - free an of device structure when all users of it are finished.
176 * @dev: device that's been disconnected
178 * Will be called only by the device core when all users of this of device are
179 * done.
181 void of_release_dev(struct device *dev)
183 struct of_device *ofdev;
185 ofdev = to_of_device(dev);
186 of_node_put(ofdev->node);
187 kfree(ofdev);
190 int of_device_register(struct of_device *ofdev)
192 int rc;
193 struct of_device **odprop;
195 BUG_ON(ofdev->node == NULL);
197 odprop = (struct of_device **)get_property(ofdev->node, "linux,device", NULL);
198 if (!odprop) {
199 struct property *new_prop;
201 new_prop = kmalloc(sizeof(struct property) + sizeof(struct of_device *),
202 GFP_KERNEL);
203 if (new_prop == NULL)
204 return -ENOMEM;
205 new_prop->name = "linux,device";
206 new_prop->length = sizeof(sizeof(struct of_device *));
207 new_prop->value = (unsigned char *)&new_prop[1];
208 odprop = (struct of_device **)new_prop->value;
209 *odprop = NULL;
210 prom_add_property(ofdev->node, new_prop);
212 *odprop = ofdev;
214 rc = device_register(&ofdev->dev);
215 if (rc)
216 return rc;
218 device_create_file(&ofdev->dev, &dev_attr_devspec);
220 return 0;
223 void of_device_unregister(struct of_device *ofdev)
225 struct of_device **odprop;
227 device_remove_file(&ofdev->dev, &dev_attr_devspec);
229 odprop = (struct of_device **)get_property(ofdev->node, "linux,device", NULL);
230 if (odprop)
231 *odprop = NULL;
233 device_unregister(&ofdev->dev);
236 struct of_device* of_platform_device_create(struct device_node *np, const char *bus_id)
238 struct of_device *dev;
239 u32 *reg;
241 dev = kmalloc(sizeof(*dev), GFP_KERNEL);
242 if (!dev)
243 return NULL;
244 memset(dev, 0, sizeof(*dev));
246 dev->node = of_node_get(np);
247 dev->dma_mask = 0xffffffffUL;
248 dev->dev.dma_mask = &dev->dma_mask;
249 dev->dev.parent = NULL;
250 dev->dev.bus = &of_platform_bus_type;
251 dev->dev.release = of_release_dev;
253 reg = (u32 *)get_property(np, "reg", NULL);
254 strlcpy(dev->dev.bus_id, bus_id, BUS_ID_SIZE);
256 if (of_device_register(dev) != 0) {
257 kfree(dev);
258 return NULL;
261 return dev;
264 EXPORT_SYMBOL(of_match_device);
265 EXPORT_SYMBOL(of_platform_bus_type);
266 EXPORT_SYMBOL(of_register_driver);
267 EXPORT_SYMBOL(of_unregister_driver);
268 EXPORT_SYMBOL(of_device_register);
269 EXPORT_SYMBOL(of_device_unregister);
270 EXPORT_SYMBOL(of_dev_get);
271 EXPORT_SYMBOL(of_dev_put);
272 EXPORT_SYMBOL(of_platform_device_create);
273 EXPORT_SYMBOL(of_release_dev);