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)
35 dev_attr_show(struct kobject * kobj, struct attribute * attr, char * buf)
37 struct device_attribute * dev_attr = to_dev_attr(attr);
38 struct device * dev = to_dev(kobj);
42 ret = dev_attr->show(dev, dev_attr, buf);
47 dev_attr_store(struct kobject * kobj, struct attribute * attr,
48 const char * buf, size_t count)
50 struct device_attribute * dev_attr = to_dev_attr(attr);
51 struct device * dev = to_dev(kobj);
55 ret = dev_attr->store(dev, dev_attr, buf, count);
59 static struct sysfs_ops dev_sysfs_ops = {
60 .show = dev_attr_show,
61 .store = dev_attr_store,
66 * device_release - free device structure.
67 * @kobj: device's kobject.
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.
73 static void device_release(struct kobject * kobj)
75 struct device * dev = to_dev(kobj);
80 printk(KERN_ERR "Device '%s' does not have a release() function, "
81 "it is broken and must be fixed.\n",
87 static struct kobj_type ktype_device = {
88 .release = device_release,
89 .sysfs_ops = &dev_sysfs_ops,
93 static int dev_hotplug_filter(struct kset *kset, struct kobject *kobj)
95 struct kobj_type *ktype = get_ktype(kobj);
97 if (ktype == &ktype_device) {
98 struct device *dev = to_dev(kobj);
105 static const char *dev_hotplug_name(struct kset *kset, struct kobject *kobj)
107 struct device *dev = to_dev(kobj);
109 return dev->bus->name;
112 static int dev_hotplug(struct kset *kset, struct kobject *kobj, char **envp,
113 int num_envp, char *buffer, int buffer_size)
115 struct device *dev = to_dev(kobj);
120 /* add bus name of physical device */
122 add_hotplug_env_var(envp, num_envp, &i,
123 buffer, buffer_size, &length,
124 "PHYSDEVBUS=%s", dev->bus->name);
126 /* add driver name of physical device */
128 add_hotplug_env_var(envp, num_envp, &i,
129 buffer, buffer_size, &length,
130 "PHYSDEVDRIVER=%s", dev->driver->name);
132 /* terminate, set to next free slot, shrink available space */
136 buffer = &buffer[length];
137 buffer_size -= length;
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);
143 pr_debug ("%s - hotplug() returned %d\n",
144 __FUNCTION__, retval);
151 static struct kset_hotplug_ops device_hotplug_ops = {
152 .filter = dev_hotplug_filter,
153 .name = dev_hotplug_name,
154 .hotplug = dev_hotplug,
158 * device_subsys - structure to be registered with kobject core.
161 decl_subsys(devices, &ktype_device, &device_hotplug_ops);
165 * device_create_file - create sysfs attribute file for device.
167 * @attr: device attribute descriptor.
170 int device_create_file(struct device * dev, struct device_attribute * attr)
173 if (get_device(dev)) {
174 error = sysfs_create_file(&dev->kobj, &attr->attr);
181 * device_remove_file - remove sysfs attribute file.
183 * @attr: device attribute descriptor.
186 void device_remove_file(struct device * dev, struct device_attribute * attr)
188 if (get_device(dev)) {
189 sysfs_remove_file(&dev->kobj, &attr->attr);
194 static void klist_children_get(struct klist_node *n)
196 struct device *dev = container_of(n, struct device, knode_parent);
201 static void klist_children_put(struct klist_node *n)
203 struct device *dev = container_of(n, struct device, knode_parent);
210 * device_initialize - init device structure.
213 * This prepares the device for use by other layers,
214 * including adding it to the device hierarchy.
215 * It is the first half of device_register(), if called by
216 * that, though it can also be called separately, so one
217 * may use @dev's fields (e.g. the refcount).
220 void device_initialize(struct device *dev)
222 kobj_set_kset_s(dev, devices_subsys);
223 kobject_init(&dev->kobj);
224 klist_init(&dev->klist_children, klist_children_get,
226 INIT_LIST_HEAD(&dev->dma_pools);
227 init_MUTEX(&dev->sem);
231 * device_add - add device to device hierarchy.
234 * This is part 2 of device_register(), though may be called
235 * separately _iff_ device_initialize() has been called separately.
237 * This adds it to the kobject hierarchy via kobject_add(), adds it
238 * to the global and sibling lists for the device, then
239 * adds it to the other relevant subsystems of the driver model.
241 int device_add(struct device *dev)
243 struct device *parent = NULL;
246 dev = get_device(dev);
247 if (!dev || !strlen(dev->bus_id))
250 parent = get_device(dev->parent);
252 pr_debug("DEV: registering device: ID = '%s'\n", dev->bus_id);
254 /* first, register with generic layer. */
255 kobject_set_name(&dev->kobj, "%s", dev->bus_id);
257 dev->kobj.parent = &parent->kobj;
259 if ((error = kobject_add(&dev->kobj)))
261 kobject_hotplug(&dev->kobj, KOBJ_ADD);
262 if ((error = device_pm_add(dev)))
264 if ((error = bus_add_device(dev)))
267 klist_add_tail(&dev->knode_parent, &parent->klist_children);
269 /* notify platform of device entry */
271 platform_notify(dev);
276 device_pm_remove(dev);
278 kobject_hotplug(&dev->kobj, KOBJ_REMOVE);
279 kobject_del(&dev->kobj);
288 * device_register - register a device with the system.
289 * @dev: pointer to the device structure
291 * This happens in two clean steps - initialize the device
292 * and add it to the system. The two steps can be called
293 * separately, but this is the easiest and most common.
294 * I.e. you should only call the two helpers separately if
295 * have a clearly defined need to use and refcount the device
296 * before it is added to the hierarchy.
299 int device_register(struct device *dev)
301 device_initialize(dev);
302 return device_add(dev);
307 * get_device - increment reference count for device.
310 * This simply forwards the call to kobject_get(), though
311 * we do take care to provide for the case that we get a NULL
315 struct device * get_device(struct device * dev)
317 return dev ? to_dev(kobject_get(&dev->kobj)) : NULL;
322 * put_device - decrement reference count.
323 * @dev: device in question.
325 void put_device(struct device * dev)
328 kobject_put(&dev->kobj);
333 * device_del - delete device from system.
336 * This is the first part of the device unregistration
337 * sequence. This removes the device from the lists we control
338 * from here, has it removed from the other driver model
339 * subsystems it was added to in device_add(), and removes it
340 * from the kobject hierarchy.
342 * NOTE: this should be called manually _iff_ device_add() was
343 * also called manually.
346 void device_del(struct device * dev)
348 struct device * parent = dev->parent;
351 klist_del(&dev->knode_parent);
353 /* Notify the platform of the removal, in case they
354 * need to do anything...
356 if (platform_notify_remove)
357 platform_notify_remove(dev);
358 bus_remove_device(dev);
359 device_pm_remove(dev);
360 kobject_hotplug(&dev->kobj, KOBJ_REMOVE);
361 kobject_del(&dev->kobj);
367 * device_unregister - unregister device from system.
368 * @dev: device going away.
370 * We do this in two parts, like we do device_register(). First,
371 * we remove it from all the subsystems with device_del(), then
372 * we decrement the reference count via put_device(). If that
373 * is the final reference count, the device will be cleaned up
374 * via device_release() above. Otherwise, the structure will
375 * stick around until the final reference to the device is dropped.
377 void device_unregister(struct device * dev)
379 pr_debug("DEV: Unregistering device. ID = '%s'\n", dev->bus_id);
385 static struct device * next_device(struct klist_iter * i)
387 struct klist_node * n = klist_next(i);
388 return n ? container_of(n, struct device, knode_parent) : NULL;
392 * device_for_each_child - device child iterator.
393 * @dev: parent struct device.
394 * @data: data for the callback.
395 * @fn: function to be called for each device.
397 * Iterate over @dev's child devices, and call @fn for each,
400 * We check the return of @fn each time. If it returns anything
401 * other than 0, we break out and return that value.
403 int device_for_each_child(struct device * parent, void * data,
404 int (*fn)(struct device *, void *))
407 struct device * child;
410 klist_iter_init(&parent->klist_children, &i);
411 while ((child = next_device(&i)) && !error)
412 error = fn(child, data);
417 int __init devices_init(void)
419 return subsystem_register(&devices_subsys);
422 EXPORT_SYMBOL_GPL(device_for_each_child);
424 EXPORT_SYMBOL_GPL(device_initialize);
425 EXPORT_SYMBOL_GPL(device_add);
426 EXPORT_SYMBOL_GPL(device_register);
428 EXPORT_SYMBOL_GPL(device_del);
429 EXPORT_SYMBOL_GPL(device_unregister);
430 EXPORT_SYMBOL_GPL(get_device);
431 EXPORT_SYMBOL_GPL(put_device);
433 EXPORT_SYMBOL_GPL(device_create_file);
434 EXPORT_SYMBOL_GPL(device_remove_file);