2 * drivers/base/core.c - core driver model code (device registration, etc)
4 * Copyright (c) 2002-3 Patrick Mochel
5 * Copyright (c) 2002-3 Open Source Development Labs
7 * This file is released under the GPLv2
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>
19 #include <asm/semaphore.h>
22 #include "power/power.h"
24 int (*platform_notify)(struct device * dev) = NULL;
25 int (*platform_notify_remove)(struct device * dev) = NULL;
28 * sysfs bindings for devices.
31 #define to_dev(obj) container_of(obj,struct device,kobj)
32 #define to_dev_attr(_attr) container_of(_attr,struct device_attribute,attr)
34 extern struct attribute * dev_default_attrs[];
37 dev_attr_show(struct kobject * kobj, struct attribute * attr, char * buf)
39 struct device_attribute * dev_attr = to_dev_attr(attr);
40 struct device * dev = to_dev(kobj);
44 ret = dev_attr->show(dev,buf);
49 dev_attr_store(struct kobject * kobj, struct attribute * attr,
50 const char * buf, size_t count)
52 struct device_attribute * dev_attr = to_dev_attr(attr);
53 struct device * dev = to_dev(kobj);
57 ret = dev_attr->store(dev,buf,count);
61 static struct sysfs_ops dev_sysfs_ops = {
62 .show = dev_attr_show,
63 .store = dev_attr_store,
68 * device_release - free device structure.
69 * @kobj: device's kobject.
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.
75 static void device_release(struct kobject * kobj)
77 struct device * dev = to_dev(kobj);
82 printk(KERN_ERR "Device '%s' does not have a release() function, "
83 "it is broken and must be fixed.\n",
89 static struct kobj_type ktype_device = {
90 .release = device_release,
91 .sysfs_ops = &dev_sysfs_ops,
92 .default_attrs = dev_default_attrs,
96 static int dev_hotplug_filter(struct kset *kset, struct kobject *kobj)
98 struct kobj_type *ktype = get_ktype(kobj);
100 if (ktype == &ktype_device) {
101 struct device *dev = to_dev(kobj);
108 static char *dev_hotplug_name(struct kset *kset, struct kobject *kobj)
110 struct device *dev = to_dev(kobj);
112 return dev->bus->name;
115 static int dev_hotplug(struct kset *kset, struct kobject *kobj, char **envp,
116 int num_envp, char *buffer, int buffer_size)
118 struct device *dev = to_dev(kobj);
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);
125 pr_debug ("%s - hotplug() returned %d\n",
126 __FUNCTION__, retval);
133 static struct kset_hotplug_ops device_hotplug_ops = {
134 .filter = dev_hotplug_filter,
135 .name = dev_hotplug_name,
136 .hotplug = dev_hotplug,
140 * device_subsys - structure to be registered with kobject core.
143 decl_subsys(devices, &ktype_device, &device_hotplug_ops);
147 * device_create_file - create sysfs attribute file for device.
149 * @attr: device attribute descriptor.
152 int device_create_file(struct device * dev, struct device_attribute * attr)
155 if (get_device(dev)) {
156 error = sysfs_create_file(&dev->kobj,&attr->attr);
163 * device_remove_file - remove sysfs attribute file.
165 * @attr: device attribute descriptor.
168 void device_remove_file(struct device * dev, struct device_attribute * attr)
170 if (get_device(dev)) {
171 sysfs_remove_file(&dev->kobj,&attr->attr);
178 * device_initialize - init device structure.
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).
188 void device_initialize(struct device *dev)
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);
200 * device_add - add device to device hierarchy.
203 * This is part 2 of device_register(), though may be called
204 * separately _iff_ device_initialize() has been called separately.
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.
210 int device_add(struct device *dev)
212 struct device * parent;
215 dev = get_device(dev);
216 if (!dev || !strlen(dev->bus_id))
219 parent = get_device(dev->parent);
221 pr_debug("DEV: registering device: ID = '%s'\n", dev->bus_id);
223 /* first, register with generic layer. */
224 kobject_set_name(&dev->kobj,dev->bus_id);
226 dev->kobj.parent = &parent->kobj;
228 if ((error = kobject_add(&dev->kobj)))
230 if ((error = device_pm_add(dev)))
232 if ((error = bus_add_device(dev)))
234 down_write(&devices_subsys.rwsem);
236 list_add_tail(&dev->node,&parent->children);
237 up_write(&devices_subsys.rwsem);
239 /* notify platform of device entry */
241 platform_notify(dev);
246 device_pm_remove(dev);
248 kobject_unregister(&dev->kobj);
257 * device_register - register a device with the system.
258 * @dev: pointer to the device structure
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.
268 int device_register(struct device *dev)
270 device_initialize(dev);
271 return device_add(dev);
276 * get_device - increment reference count for device.
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
284 struct device * get_device(struct device * dev)
286 return dev ? to_dev(kobject_get(&dev->kobj)) : NULL;
291 * put_device - decrement reference count.
292 * @dev: device in question.
294 void put_device(struct device * dev)
296 kobject_put(&dev->kobj);
301 * device_del - delete device from system.
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.
310 * NOTE: this should be called manually _iff_ device_add() was
311 * also called manually.
314 void device_del(struct device * dev)
316 struct device * parent = dev->parent;
318 down_write(&devices_subsys.rwsem);
320 list_del_init(&dev->node);
321 up_write(&devices_subsys.rwsem);
323 /* Notify the platform of the removal, in case they
324 * need to do anything...
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);
336 * device_unregister - unregister device from system.
337 * @dev: device going away.
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.
346 void device_unregister(struct device * dev)
348 pr_debug("DEV: Unregistering device. ID = '%s'\n", dev->bus_id);
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.
360 * Iterate over @dev's child devices, and call @fn for each,
363 * We check the return of @fn each time. If it returns anything
364 * other than 0, we break out and return that value.
366 int device_for_each_child(struct device * dev, void * data,
367 int (*fn)(struct device *, void *))
369 struct device * child;
372 down_read(&devices_subsys.rwsem);
373 list_for_each_entry(child,&dev->children,node) {
374 if((error = fn(child,data)))
377 up_read(&devices_subsys.rwsem);
381 struct device *device_find(const char *name, struct bus_type *bus)
383 struct kobject *k = kset_find_obj(&bus->devices, name);
389 int __init devices_init(void)
391 return subsystem_register(&devices_subsys);
394 EXPORT_SYMBOL(device_for_each_child);
396 EXPORT_SYMBOL(device_initialize);
397 EXPORT_SYMBOL(device_add);
398 EXPORT_SYMBOL(device_register);
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);
406 EXPORT_SYMBOL(device_create_file);
407 EXPORT_SYMBOL(device_remove_file);