[TG3]: Set minimal hw interrupt mitigation.
[linux-2.6/verdex.git] / arch / ppc64 / kernel / of_device.c
blobf4c825a69fa0ce700065f51ed48a71cfa5d5985b
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, pm_message_t 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 kfree(ofdev);
189 int of_device_register(struct of_device *ofdev)
191 int rc;
192 struct of_device **odprop;
194 BUG_ON(ofdev->node == NULL);
196 odprop = (struct of_device **)get_property(ofdev->node, "linux,device", NULL);
197 if (!odprop) {
198 struct property *new_prop;
200 new_prop = kmalloc(sizeof(struct property) + sizeof(struct of_device *),
201 GFP_KERNEL);
202 if (new_prop == NULL)
203 return -ENOMEM;
204 new_prop->name = "linux,device";
205 new_prop->length = sizeof(sizeof(struct of_device *));
206 new_prop->value = (unsigned char *)&new_prop[1];
207 odprop = (struct of_device **)new_prop->value;
208 *odprop = NULL;
209 prom_add_property(ofdev->node, new_prop);
211 *odprop = ofdev;
213 rc = device_register(&ofdev->dev);
214 if (rc)
215 return rc;
217 device_create_file(&ofdev->dev, &dev_attr_devspec);
219 return 0;
222 void of_device_unregister(struct of_device *ofdev)
224 struct of_device **odprop;
226 device_remove_file(&ofdev->dev, &dev_attr_devspec);
228 odprop = (struct of_device **)get_property(ofdev->node, "linux,device", NULL);
229 if (odprop)
230 *odprop = NULL;
232 device_unregister(&ofdev->dev);
235 struct of_device* of_platform_device_create(struct device_node *np, const char *bus_id)
237 struct of_device *dev;
238 u32 *reg;
240 dev = kmalloc(sizeof(*dev), GFP_KERNEL);
241 if (!dev)
242 return NULL;
243 memset(dev, 0, sizeof(*dev));
245 dev->node = np;
246 dev->dma_mask = 0xffffffffUL;
247 dev->dev.dma_mask = &dev->dma_mask;
248 dev->dev.parent = NULL;
249 dev->dev.bus = &of_platform_bus_type;
250 dev->dev.release = of_release_dev;
252 reg = (u32 *)get_property(np, "reg", NULL);
253 strlcpy(dev->dev.bus_id, bus_id, BUS_ID_SIZE);
255 if (of_device_register(dev) != 0) {
256 kfree(dev);
257 return NULL;
260 return dev;
263 EXPORT_SYMBOL(of_match_device);
264 EXPORT_SYMBOL(of_platform_bus_type);
265 EXPORT_SYMBOL(of_register_driver);
266 EXPORT_SYMBOL(of_unregister_driver);
267 EXPORT_SYMBOL(of_device_register);
268 EXPORT_SYMBOL(of_device_unregister);
269 EXPORT_SYMBOL(of_dev_get);
270 EXPORT_SYMBOL(of_dev_put);
271 EXPORT_SYMBOL(of_platform_device_create);
272 EXPORT_SYMBOL(of_release_dev);