[PATCH] acpi bridge hotadd: Export the interface to get PCI id for an ACPI handle
[pandora-kernel.git] / drivers / base / core.c
1 /*
2  * drivers/base/core.c - core driver model code (device registration, etc)
3  *
4  * Copyright (c) 2002-3 Patrick Mochel
5  * Copyright (c) 2002-3 Open Source Development Labs
6  *
7  * This file is released under the GPLv2
8  *
9  */
10
11 #include <linux/config.h>
12 #include <linux/device.h>
13 #include <linux/err.h>
14 #include <linux/init.h>
15 #include <linux/module.h>
16 #include <linux/slab.h>
17 #include <linux/string.h>
18
19 #include <asm/semaphore.h>
20
21 #include "base.h"
22 #include "power/power.h"
23
24 int (*platform_notify)(struct device * dev) = NULL;
25 int (*platform_notify_remove)(struct device * dev) = NULL;
26
27 /*
28  * sysfs bindings for devices.
29  */
30
31 #define to_dev(obj) container_of(obj, struct device, kobj)
32 #define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr)
33
34 static ssize_t
35 dev_attr_show(struct kobject * kobj, struct attribute * attr, char * buf)
36 {
37         struct device_attribute * dev_attr = to_dev_attr(attr);
38         struct device * dev = to_dev(kobj);
39         ssize_t ret = -EIO;
40
41         if (dev_attr->show)
42                 ret = dev_attr->show(dev, dev_attr, buf);
43         return ret;
44 }
45
46 static ssize_t
47 dev_attr_store(struct kobject * kobj, struct attribute * attr,
48                const char * buf, size_t count)
49 {
50         struct device_attribute * dev_attr = to_dev_attr(attr);
51         struct device * dev = to_dev(kobj);
52         ssize_t ret = -EIO;
53
54         if (dev_attr->store)
55                 ret = dev_attr->store(dev, dev_attr, buf, count);
56         return ret;
57 }
58
59 static struct sysfs_ops dev_sysfs_ops = {
60         .show   = dev_attr_show,
61         .store  = dev_attr_store,
62 };
63
64
65 /**
66  *      device_release - free device structure.
67  *      @kobj:  device's kobject.
68  *
69  *      This is called once the reference count for the object
70  *      reaches 0. We forward the call to the device's release
71  *      method, which should handle actually freeing the structure.
72  */
73 static void device_release(struct kobject * kobj)
74 {
75         struct device * dev = to_dev(kobj);
76
77         if (dev->release)
78                 dev->release(dev);
79         else {
80                 printk(KERN_ERR "Device '%s' does not have a release() function, "
81                         "it is broken and must be fixed.\n",
82                         dev->bus_id);
83                 WARN_ON(1);
84         }
85 }
86
87 static struct kobj_type ktype_device = {
88         .release        = device_release,
89         .sysfs_ops      = &dev_sysfs_ops,
90 };
91
92
93 static int dev_hotplug_filter(struct kset *kset, struct kobject *kobj)
94 {
95         struct kobj_type *ktype = get_ktype(kobj);
96
97         if (ktype == &ktype_device) {
98                 struct device *dev = to_dev(kobj);
99                 if (dev->bus)
100                         return 1;
101         }
102         return 0;
103 }
104
105 static const char *dev_hotplug_name(struct kset *kset, struct kobject *kobj)
106 {
107         struct device *dev = to_dev(kobj);
108
109         return dev->bus->name;
110 }
111
112 static int dev_hotplug(struct kset *kset, struct kobject *kobj, char **envp,
113                         int num_envp, char *buffer, int buffer_size)
114 {
115         struct device *dev = to_dev(kobj);
116         int i = 0;
117         int length = 0;
118         int retval = 0;
119
120         /* add bus name of physical device */
121         if (dev->bus)
122                 add_hotplug_env_var(envp, num_envp, &i,
123                                     buffer, buffer_size, &length,
124                                     "PHYSDEVBUS=%s", dev->bus->name);
125
126         /* add driver name of physical device */
127         if (dev->driver)
128                 add_hotplug_env_var(envp, num_envp, &i,
129                                     buffer, buffer_size, &length,
130                                     "PHYSDEVDRIVER=%s", dev->driver->name);
131
132         /* terminate, set to next free slot, shrink available space */
133         envp[i] = NULL;
134         envp = &envp[i];
135         num_envp -= i;
136         buffer = &buffer[length];
137         buffer_size -= length;
138
139         if (dev->bus && dev->bus->hotplug) {
140                 /* have the bus specific function add its stuff */
141                 retval = dev->bus->hotplug (dev, envp, num_envp, buffer, buffer_size);
142                         if (retval) {
143                         pr_debug ("%s - hotplug() returned %d\n",
144                                   __FUNCTION__, retval);
145                 }
146         }
147
148         return retval;
149 }
150
151 static struct kset_hotplug_ops device_hotplug_ops = {
152         .filter =       dev_hotplug_filter,
153         .name =         dev_hotplug_name,
154         .hotplug =      dev_hotplug,
155 };
156
157 /**
158  *      device_subsys - structure to be registered with kobject core.
159  */
160
161 decl_subsys(devices, &ktype_device, &device_hotplug_ops);
162
163
164 /**
165  *      device_create_file - create sysfs attribute file for device.
166  *      @dev:   device.
167  *      @attr:  device attribute descriptor.
168  */
169
170 int device_create_file(struct device * dev, struct device_attribute * attr)
171 {
172         int error = 0;
173         if (get_device(dev)) {
174                 error = sysfs_create_file(&dev->kobj, &attr->attr);
175                 put_device(dev);
176         }
177         return error;
178 }
179
180 /**
181  *      device_remove_file - remove sysfs attribute file.
182  *      @dev:   device.
183  *      @attr:  device attribute descriptor.
184  */
185
186 void device_remove_file(struct device * dev, struct device_attribute * attr)
187 {
188         if (get_device(dev)) {
189                 sysfs_remove_file(&dev->kobj, &attr->attr);
190                 put_device(dev);
191         }
192 }
193
194
195 /**
196  *      device_initialize - init device structure.
197  *      @dev:   device.
198  *
199  *      This prepares the device for use by other layers,
200  *      including adding it to the device hierarchy.
201  *      It is the first half of device_register(), if called by
202  *      that, though it can also be called separately, so one
203  *      may use @dev's fields (e.g. the refcount).
204  */
205
206 void device_initialize(struct device *dev)
207 {
208         kobj_set_kset_s(dev, devices_subsys);
209         kobject_init(&dev->kobj);
210         klist_init(&dev->klist_children);
211         INIT_LIST_HEAD(&dev->dma_pools);
212         init_MUTEX(&dev->sem);
213 }
214
215 /**
216  *      device_add - add device to device hierarchy.
217  *      @dev:   device.
218  *
219  *      This is part 2 of device_register(), though may be called
220  *      separately _iff_ device_initialize() has been called separately.
221  *
222  *      This adds it to the kobject hierarchy via kobject_add(), adds it
223  *      to the global and sibling lists for the device, then
224  *      adds it to the other relevant subsystems of the driver model.
225  */
226 int device_add(struct device *dev)
227 {
228         struct device *parent = NULL;
229         int error = -EINVAL;
230
231         dev = get_device(dev);
232         if (!dev || !strlen(dev->bus_id))
233                 goto Error;
234
235         parent = get_device(dev->parent);
236
237         pr_debug("DEV: registering device: ID = '%s'\n", dev->bus_id);
238
239         /* first, register with generic layer. */
240         kobject_set_name(&dev->kobj, "%s", dev->bus_id);
241         if (parent)
242                 dev->kobj.parent = &parent->kobj;
243
244         if ((error = kobject_add(&dev->kobj)))
245                 goto Error;
246         kobject_hotplug(&dev->kobj, KOBJ_ADD);
247         if ((error = device_pm_add(dev)))
248                 goto PMError;
249         if ((error = bus_add_device(dev)))
250                 goto BusError;
251         if (parent)
252                 klist_add_tail(&parent->klist_children, &dev->knode_parent);
253
254         /* notify platform of device entry */
255         if (platform_notify)
256                 platform_notify(dev);
257  Done:
258         put_device(dev);
259         return error;
260  BusError:
261         device_pm_remove(dev);
262  PMError:
263         kobject_hotplug(&dev->kobj, KOBJ_REMOVE);
264         kobject_del(&dev->kobj);
265  Error:
266         if (parent)
267                 put_device(parent);
268         goto Done;
269 }
270
271
272 /**
273  *      device_register - register a device with the system.
274  *      @dev:   pointer to the device structure
275  *
276  *      This happens in two clean steps - initialize the device
277  *      and add it to the system. The two steps can be called
278  *      separately, but this is the easiest and most common.
279  *      I.e. you should only call the two helpers separately if
280  *      have a clearly defined need to use and refcount the device
281  *      before it is added to the hierarchy.
282  */
283
284 int device_register(struct device *dev)
285 {
286         device_initialize(dev);
287         return device_add(dev);
288 }
289
290
291 /**
292  *      get_device - increment reference count for device.
293  *      @dev:   device.
294  *
295  *      This simply forwards the call to kobject_get(), though
296  *      we do take care to provide for the case that we get a NULL
297  *      pointer passed in.
298  */
299
300 struct device * get_device(struct device * dev)
301 {
302         return dev ? to_dev(kobject_get(&dev->kobj)) : NULL;
303 }
304
305
306 /**
307  *      put_device - decrement reference count.
308  *      @dev:   device in question.
309  */
310 void put_device(struct device * dev)
311 {
312         if (dev)
313                 kobject_put(&dev->kobj);
314 }
315
316
317 /**
318  *      device_del - delete device from system.
319  *      @dev:   device.
320  *
321  *      This is the first part of the device unregistration
322  *      sequence. This removes the device from the lists we control
323  *      from here, has it removed from the other driver model
324  *      subsystems it was added to in device_add(), and removes it
325  *      from the kobject hierarchy.
326  *
327  *      NOTE: this should be called manually _iff_ device_add() was
328  *      also called manually.
329  */
330
331 void device_del(struct device * dev)
332 {
333         struct device * parent = dev->parent;
334
335         if (parent)
336                 klist_remove(&dev->knode_parent);
337
338         /* Notify the platform of the removal, in case they
339          * need to do anything...
340          */
341         if (platform_notify_remove)
342                 platform_notify_remove(dev);
343         bus_remove_device(dev);
344         device_pm_remove(dev);
345         kobject_hotplug(&dev->kobj, KOBJ_REMOVE);
346         kobject_del(&dev->kobj);
347         if (parent)
348                 put_device(parent);
349 }
350
351 /**
352  *      device_unregister - unregister device from system.
353  *      @dev:   device going away.
354  *
355  *      We do this in two parts, like we do device_register(). First,
356  *      we remove it from all the subsystems with device_del(), then
357  *      we decrement the reference count via put_device(). If that
358  *      is the final reference count, the device will be cleaned up
359  *      via device_release() above. Otherwise, the structure will
360  *      stick around until the final reference to the device is dropped.
361  */
362 void device_unregister(struct device * dev)
363 {
364         pr_debug("DEV: Unregistering device. ID = '%s'\n", dev->bus_id);
365         device_del(dev);
366         put_device(dev);
367 }
368
369
370 static struct device * next_device(struct klist_iter * i)
371 {
372         struct klist_node * n = klist_next(i);
373         return n ? container_of(n, struct device, knode_parent) : NULL;
374 }
375
376 /**
377  *      device_for_each_child - device child iterator.
378  *      @dev:   parent struct device.
379  *      @data:  data for the callback.
380  *      @fn:    function to be called for each device.
381  *
382  *      Iterate over @dev's child devices, and call @fn for each,
383  *      passing it @data.
384  *
385  *      We check the return of @fn each time. If it returns anything
386  *      other than 0, we break out and return that value.
387  */
388 int device_for_each_child(struct device * parent, void * data,
389                      int (*fn)(struct device *, void *))
390 {
391         struct klist_iter i;
392         struct device * child;
393         int error = 0;
394
395         klist_iter_init(&parent->klist_children, &i);
396         while ((child = next_device(&i)) && !error)
397                 error = fn(child, data);
398         klist_iter_exit(&i);
399         return error;
400 }
401
402 int __init devices_init(void)
403 {
404         return subsystem_register(&devices_subsys);
405 }
406
407 EXPORT_SYMBOL_GPL(device_for_each_child);
408
409 EXPORT_SYMBOL_GPL(device_initialize);
410 EXPORT_SYMBOL_GPL(device_add);
411 EXPORT_SYMBOL_GPL(device_register);
412
413 EXPORT_SYMBOL_GPL(device_del);
414 EXPORT_SYMBOL_GPL(device_unregister);
415 EXPORT_SYMBOL_GPL(get_device);
416 EXPORT_SYMBOL_GPL(put_device);
417
418 EXPORT_SYMBOL_GPL(device_create_file);
419 EXPORT_SYMBOL_GPL(device_remove_file);