]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - arch/ppc/syslib/of_device.c
[PATCH] fix missing includes
[mirror_ubuntu-artful-kernel.git] / arch / ppc / syslib / of_device.c
CommitLineData
1da177e4
LT
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>
5e655772 6#include <linux/mod_devicetable.h>
4e57b681
TS
7#include <linux/slab.h>
8
1da177e4
LT
9#include <asm/errno.h>
10#include <asm/of_device.h>
11
12/**
13 * of_match_device - Tell if an of_device structure has a matching
14 * of_match structure
15 * @ids: array of of device match structures to search in
16 * @dev: the of device structure to match against
17 *
18 * Used by a driver to check whether an of_device present in the
19 * system is in its list of supported devices.
20 */
5e655772 21const struct of_device_id * of_match_device(const struct of_device_id *matches,
1da177e4
LT
22 const struct of_device *dev)
23{
24 if (!dev->node)
25 return NULL;
5e655772 26 while (matches->name[0] || matches->type[0] || matches->compatible[0]) {
1da177e4 27 int match = 1;
5e655772 28 if (matches->name[0])
1da177e4
LT
29 match &= dev->node->name
30 && !strcmp(matches->name, dev->node->name);
5e655772 31 if (matches->type[0])
1da177e4
LT
32 match &= dev->node->type
33 && !strcmp(matches->type, dev->node->type);
5e655772 34 if (matches->compatible[0])
1da177e4
LT
35 match &= device_is_compatible(dev->node,
36 matches->compatible);
37 if (match)
38 return matches;
39 matches++;
40 }
41 return NULL;
42}
43
44static int of_platform_bus_match(struct device *dev, struct device_driver *drv)
45{
46 struct of_device * of_dev = to_of_device(dev);
47 struct of_platform_driver * of_drv = to_of_platform_driver(drv);
5e655772 48 const struct of_device_id * matches = of_drv->match_table;
1da177e4
LT
49
50 if (!matches)
51 return 0;
52
53 return of_match_device(matches, of_dev) != NULL;
54}
55
56struct of_device *of_dev_get(struct of_device *dev)
57{
58 struct device *tmp;
59
60 if (!dev)
61 return NULL;
62 tmp = get_device(&dev->dev);
63 if (tmp)
64 return to_of_device(tmp);
65 else
66 return NULL;
67}
68
69void of_dev_put(struct of_device *dev)
70{
71 if (dev)
72 put_device(&dev->dev);
73}
74
75
76static int of_device_probe(struct device *dev)
77{
78 int error = -ENODEV;
79 struct of_platform_driver *drv;
80 struct of_device *of_dev;
5e655772 81 const struct of_device_id *match;
1da177e4
LT
82
83 drv = to_of_platform_driver(dev->driver);
84 of_dev = to_of_device(dev);
85
86 if (!drv->probe)
87 return error;
88
89 of_dev_get(of_dev);
90
91 match = of_match_device(drv->match_table, of_dev);
92 if (match)
93 error = drv->probe(of_dev, match);
94 if (error)
95 of_dev_put(of_dev);
96
97 return error;
98}
99
100static int of_device_remove(struct device *dev)
101{
102 struct of_device * of_dev = to_of_device(dev);
103 struct of_platform_driver * drv = to_of_platform_driver(dev->driver);
104
105 if (dev->driver && drv->remove)
106 drv->remove(of_dev);
107 return 0;
108}
109
ca078bae 110static int of_device_suspend(struct device *dev, pm_message_t state)
1da177e4
LT
111{
112 struct of_device * of_dev = to_of_device(dev);
113 struct of_platform_driver * drv = to_of_platform_driver(dev->driver);
114 int error = 0;
115
116 if (dev->driver && drv->suspend)
117 error = drv->suspend(of_dev, state);
118 return error;
119}
120
121static int of_device_resume(struct device * dev)
122{
123 struct of_device * of_dev = to_of_device(dev);
124 struct of_platform_driver * drv = to_of_platform_driver(dev->driver);
125 int error = 0;
126
127 if (dev->driver && drv->resume)
128 error = drv->resume(of_dev);
129 return error;
130}
131
132struct bus_type of_platform_bus_type = {
133 .name = "of_platform",
134 .match = of_platform_bus_match,
135 .suspend = of_device_suspend,
136 .resume = of_device_resume,
137};
138
139static int __init of_bus_driver_init(void)
140{
141 return bus_register(&of_platform_bus_type);
142}
143
144postcore_initcall(of_bus_driver_init);
145
146int of_register_driver(struct of_platform_driver *drv)
147{
148 int count = 0;
149
150 /* initialize common driver fields */
151 drv->driver.name = drv->name;
152 drv->driver.bus = &of_platform_bus_type;
153 drv->driver.probe = of_device_probe;
154 drv->driver.remove = of_device_remove;
155
156 /* register with core */
157 count = driver_register(&drv->driver);
158 return count ? count : 1;
159}
160
161void of_unregister_driver(struct of_platform_driver *drv)
162{
163 driver_unregister(&drv->driver);
164}
165
166
ff381d22 167static ssize_t dev_show_devspec(struct device *dev, struct device_attribute *attr, char *buf)
1da177e4
LT
168{
169 struct of_device *ofdev;
170
171 ofdev = to_of_device(dev);
172 return sprintf(buf, "%s", ofdev->node->full_name);
173}
174
175static DEVICE_ATTR(devspec, S_IRUGO, dev_show_devspec, NULL);
176
177/**
178 * of_release_dev - free an of device structure when all users of it are finished.
179 * @dev: device that's been disconnected
180 *
181 * Will be called only by the device core when all users of this of device are
182 * done.
183 */
184void of_release_dev(struct device *dev)
185{
186 struct of_device *ofdev;
187
188 ofdev = to_of_device(dev);
189 of_node_put(ofdev->node);
190 kfree(ofdev);
191}
192
193int of_device_register(struct of_device *ofdev)
194{
195 int rc;
196 struct of_device **odprop;
197
198 BUG_ON(ofdev->node == NULL);
199
200 odprop = (struct of_device **)get_property(ofdev->node, "linux,device", NULL);
201 if (!odprop) {
202 struct property *new_prop;
203
204 new_prop = kmalloc(sizeof(struct property) + sizeof(struct of_device *),
205 GFP_KERNEL);
206 if (new_prop == NULL)
207 return -ENOMEM;
208 new_prop->name = "linux,device";
209 new_prop->length = sizeof(sizeof(struct of_device *));
210 new_prop->value = (unsigned char *)&new_prop[1];
211 odprop = (struct of_device **)new_prop->value;
212 *odprop = NULL;
213 prom_add_property(ofdev->node, new_prop);
214 }
215 *odprop = ofdev;
216
217 rc = device_register(&ofdev->dev);
218 if (rc)
219 return rc;
220
221 device_create_file(&ofdev->dev, &dev_attr_devspec);
222
223 return 0;
224}
225
226void of_device_unregister(struct of_device *ofdev)
227{
228 struct of_device **odprop;
229
230 device_remove_file(&ofdev->dev, &dev_attr_devspec);
231
232 odprop = (struct of_device **)get_property(ofdev->node, "linux,device", NULL);
233 if (odprop)
234 *odprop = NULL;
235
236 device_unregister(&ofdev->dev);
237}
238
0365ba7f
BH
239struct of_device* of_platform_device_create(struct device_node *np,
240 const char *bus_id,
241 struct device *parent)
1da177e4
LT
242{
243 struct of_device *dev;
244 u32 *reg;
245
246 dev = kmalloc(sizeof(*dev), GFP_KERNEL);
247 if (!dev)
248 return NULL;
249 memset(dev, 0, sizeof(*dev));
250
251 dev->node = of_node_get(np);
252 dev->dma_mask = 0xffffffffUL;
253 dev->dev.dma_mask = &dev->dma_mask;
0365ba7f 254 dev->dev.parent = parent;
1da177e4
LT
255 dev->dev.bus = &of_platform_bus_type;
256 dev->dev.release = of_release_dev;
257
258 reg = (u32 *)get_property(np, "reg", NULL);
259 strlcpy(dev->dev.bus_id, bus_id, BUS_ID_SIZE);
260
261 if (of_device_register(dev) != 0) {
262 kfree(dev);
263 return NULL;
264 }
265
266 return dev;
267}
268
269EXPORT_SYMBOL(of_match_device);
270EXPORT_SYMBOL(of_platform_bus_type);
271EXPORT_SYMBOL(of_register_driver);
272EXPORT_SYMBOL(of_unregister_driver);
273EXPORT_SYMBOL(of_device_register);
274EXPORT_SYMBOL(of_device_unregister);
275EXPORT_SYMBOL(of_dev_get);
276EXPORT_SYMBOL(of_dev_put);
277EXPORT_SYMBOL(of_platform_device_create);
278EXPORT_SYMBOL(of_release_dev);