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