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