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>
18 #include <linux/kdev_t.h>
20 #include <asm/semaphore.h>
23 #include "power/power.h"
25 int (*platform_notify)(struct device * dev) = NULL;
26 int (*platform_notify_remove)(struct device * dev) = NULL;
29 * sysfs bindings for devices.
33 * dev_driver_string - Return a device's driver name, if at all possible
34 * @dev: struct device to get the name of
36 * Will return the device's driver's name if it is bound to a device. If
37 * the device is not bound to a device, it will return the name of the bus
38 * it is attached to. If it is not attached to a bus either, an empty
39 * string will be returned.
41 const char *dev_driver_string(struct device *dev)
43 return dev->driver ? dev->driver->name :
44 (dev->bus ? dev->bus->name : "");
46 EXPORT_SYMBOL_GPL(dev_driver_string);
48 #define to_dev(obj) container_of(obj, struct device, kobj)
49 #define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr)
52 dev_attr_show(struct kobject * kobj, struct attribute * attr, char * buf)
54 struct device_attribute * dev_attr = to_dev_attr(attr);
55 struct device * dev = to_dev(kobj);
59 ret = dev_attr->show(dev, dev_attr, buf);
64 dev_attr_store(struct kobject * kobj, struct attribute * attr,
65 const char * buf, size_t count)
67 struct device_attribute * dev_attr = to_dev_attr(attr);
68 struct device * dev = to_dev(kobj);
72 ret = dev_attr->store(dev, dev_attr, buf, count);
76 static struct sysfs_ops dev_sysfs_ops = {
77 .show = dev_attr_show,
78 .store = dev_attr_store,
83 * device_release - free device structure.
84 * @kobj: device's kobject.
86 * This is called once the reference count for the object
87 * reaches 0. We forward the call to the device's release
88 * method, which should handle actually freeing the structure.
90 static void device_release(struct kobject * kobj)
92 struct device * dev = to_dev(kobj);
97 printk(KERN_ERR "Device '%s' does not have a release() function, "
98 "it is broken and must be fixed.\n",
104 static struct kobj_type ktype_device = {
105 .release = device_release,
106 .sysfs_ops = &dev_sysfs_ops,
110 static int dev_uevent_filter(struct kset *kset, struct kobject *kobj)
112 struct kobj_type *ktype = get_ktype(kobj);
114 if (ktype == &ktype_device) {
115 struct device *dev = to_dev(kobj);
124 static const char *dev_uevent_name(struct kset *kset, struct kobject *kobj)
126 struct device *dev = to_dev(kobj);
129 return dev->bus->name;
131 return dev->class->name;
135 static int dev_uevent(struct kset *kset, struct kobject *kobj, char **envp,
136 int num_envp, char *buffer, int buffer_size)
138 struct device *dev = to_dev(kobj);
143 /* add the major/minor if present */
144 if (MAJOR(dev->devt)) {
145 add_uevent_var(envp, num_envp, &i,
146 buffer, buffer_size, &length,
147 "MAJOR=%u", MAJOR(dev->devt));
148 add_uevent_var(envp, num_envp, &i,
149 buffer, buffer_size, &length,
150 "MINOR=%u", MINOR(dev->devt));
153 /* add bus name of physical device */
155 add_uevent_var(envp, num_envp, &i,
156 buffer, buffer_size, &length,
157 "PHYSDEVBUS=%s", dev->bus->name);
159 /* add driver name of physical device */
161 add_uevent_var(envp, num_envp, &i,
162 buffer, buffer_size, &length,
163 "PHYSDEVDRIVER=%s", dev->driver->name);
165 /* terminate, set to next free slot, shrink available space */
169 buffer = &buffer[length];
170 buffer_size -= length;
172 if (dev->bus && dev->bus->uevent) {
173 /* have the bus specific function add its stuff */
174 retval = dev->bus->uevent(dev, envp, num_envp, buffer, buffer_size);
176 pr_debug ("%s - uevent() returned %d\n",
177 __FUNCTION__, retval);
184 static struct kset_uevent_ops device_uevent_ops = {
185 .filter = dev_uevent_filter,
186 .name = dev_uevent_name,
187 .uevent = dev_uevent,
190 static ssize_t store_uevent(struct device *dev, struct device_attribute *attr,
191 const char *buf, size_t count)
193 kobject_uevent(&dev->kobj, KOBJ_ADD);
197 static ssize_t show_dev(struct device *dev, struct device_attribute *attr,
200 return print_dev_t(buf, dev->devt);
204 * devices_subsys - structure to be registered with kobject core.
207 decl_subsys(devices, &ktype_device, &device_uevent_ops);
211 * device_create_file - create sysfs attribute file for device.
213 * @attr: device attribute descriptor.
216 int device_create_file(struct device * dev, struct device_attribute * attr)
219 if (get_device(dev)) {
220 error = sysfs_create_file(&dev->kobj, &attr->attr);
227 * device_remove_file - remove sysfs attribute file.
229 * @attr: device attribute descriptor.
232 void device_remove_file(struct device * dev, struct device_attribute * attr)
234 if (get_device(dev)) {
235 sysfs_remove_file(&dev->kobj, &attr->attr);
240 static void klist_children_get(struct klist_node *n)
242 struct device *dev = container_of(n, struct device, knode_parent);
247 static void klist_children_put(struct klist_node *n)
249 struct device *dev = container_of(n, struct device, knode_parent);
256 * device_initialize - init device structure.
259 * This prepares the device for use by other layers,
260 * including adding it to the device hierarchy.
261 * It is the first half of device_register(), if called by
262 * that, though it can also be called separately, so one
263 * may use @dev's fields (e.g. the refcount).
266 void device_initialize(struct device *dev)
268 kobj_set_kset_s(dev, devices_subsys);
269 kobject_init(&dev->kobj);
270 klist_init(&dev->klist_children, klist_children_get,
272 INIT_LIST_HEAD(&dev->dma_pools);
273 INIT_LIST_HEAD(&dev->node);
274 init_MUTEX(&dev->sem);
275 device_init_wakeup(dev, 0);
279 * device_add - add device to device hierarchy.
282 * This is part 2 of device_register(), though may be called
283 * separately _iff_ device_initialize() has been called separately.
285 * This adds it to the kobject hierarchy via kobject_add(), adds it
286 * to the global and sibling lists for the device, then
287 * adds it to the other relevant subsystems of the driver model.
289 int device_add(struct device *dev)
291 struct device *parent = NULL;
292 char *class_name = NULL;
295 dev = get_device(dev);
296 if (!dev || !strlen(dev->bus_id))
299 parent = get_device(dev->parent);
301 pr_debug("DEV: registering device: ID = '%s'\n", dev->bus_id);
303 /* first, register with generic layer. */
304 kobject_set_name(&dev->kobj, "%s", dev->bus_id);
306 dev->kobj.parent = &parent->kobj;
308 if ((error = kobject_add(&dev->kobj)))
311 dev->uevent_attr.attr.name = "uevent";
312 dev->uevent_attr.attr.mode = S_IWUSR;
314 dev->uevent_attr.attr.owner = dev->driver->owner;
315 dev->uevent_attr.store = store_uevent;
316 device_create_file(dev, &dev->uevent_attr);
318 if (MAJOR(dev->devt)) {
319 struct device_attribute *attr;
320 attr = kzalloc(sizeof(*attr), GFP_KERNEL);
325 attr->attr.name = "dev";
326 attr->attr.mode = S_IRUGO;
328 attr->attr.owner = dev->driver->owner;
329 attr->show = show_dev;
330 error = device_create_file(dev, attr);
336 dev->devt_attr = attr;
340 sysfs_create_link(&dev->kobj, &dev->class->subsys.kset.kobj,
342 sysfs_create_link(&dev->class->subsys.kset.kobj, &dev->kobj,
345 sysfs_create_link(&dev->kobj, &dev->parent->kobj, "device");
346 class_name = make_class_name(dev->class->name, &dev->kobj);
347 sysfs_create_link(&dev->parent->kobj, &dev->kobj, class_name);
350 if ((error = device_pm_add(dev)))
352 if ((error = bus_add_device(dev)))
354 kobject_uevent(&dev->kobj, KOBJ_ADD);
355 bus_attach_device(dev);
357 klist_add_tail(&dev->knode_parent, &parent->klist_children);
360 /* tie the class to the device */
361 down(&dev->class->sem);
362 list_add_tail(&dev->node, &dev->class->devices);
363 up(&dev->class->sem);
366 /* notify platform of device entry */
368 platform_notify(dev);
374 device_pm_remove(dev);
376 if (dev->devt_attr) {
377 device_remove_file(dev, dev->devt_attr);
378 kfree(dev->devt_attr);
381 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
382 kobject_del(&dev->kobj);
391 * device_register - register a device with the system.
392 * @dev: pointer to the device structure
394 * This happens in two clean steps - initialize the device
395 * and add it to the system. The two steps can be called
396 * separately, but this is the easiest and most common.
397 * I.e. you should only call the two helpers separately if
398 * have a clearly defined need to use and refcount the device
399 * before it is added to the hierarchy.
402 int device_register(struct device *dev)
404 device_initialize(dev);
405 return device_add(dev);
410 * get_device - increment reference count for device.
413 * This simply forwards the call to kobject_get(), though
414 * we do take care to provide for the case that we get a NULL
418 struct device * get_device(struct device * dev)
420 return dev ? to_dev(kobject_get(&dev->kobj)) : NULL;
425 * put_device - decrement reference count.
426 * @dev: device in question.
428 void put_device(struct device * dev)
431 kobject_put(&dev->kobj);
436 * device_del - delete device from system.
439 * This is the first part of the device unregistration
440 * sequence. This removes the device from the lists we control
441 * from here, has it removed from the other driver model
442 * subsystems it was added to in device_add(), and removes it
443 * from the kobject hierarchy.
445 * NOTE: this should be called manually _iff_ device_add() was
446 * also called manually.
449 void device_del(struct device * dev)
451 struct device * parent = dev->parent;
452 char *class_name = NULL;
455 klist_del(&dev->knode_parent);
457 device_remove_file(dev, dev->devt_attr);
459 sysfs_remove_link(&dev->kobj, "subsystem");
460 sysfs_remove_link(&dev->class->subsys.kset.kobj, dev->bus_id);
461 class_name = make_class_name(dev->class->name, &dev->kobj);
462 sysfs_remove_link(&dev->kobj, "device");
463 sysfs_remove_link(&dev->parent->kobj, class_name);
465 down(&dev->class->sem);
466 list_del_init(&dev->node);
467 up(&dev->class->sem);
469 device_remove_file(dev, &dev->uevent_attr);
471 /* Notify the platform of the removal, in case they
472 * need to do anything...
474 if (platform_notify_remove)
475 platform_notify_remove(dev);
476 bus_remove_device(dev);
477 device_pm_remove(dev);
478 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
479 kobject_del(&dev->kobj);
485 * device_unregister - unregister device from system.
486 * @dev: device going away.
488 * We do this in two parts, like we do device_register(). First,
489 * we remove it from all the subsystems with device_del(), then
490 * we decrement the reference count via put_device(). If that
491 * is the final reference count, the device will be cleaned up
492 * via device_release() above. Otherwise, the structure will
493 * stick around until the final reference to the device is dropped.
495 void device_unregister(struct device * dev)
497 pr_debug("DEV: Unregistering device. ID = '%s'\n", dev->bus_id);
503 static struct device * next_device(struct klist_iter * i)
505 struct klist_node * n = klist_next(i);
506 return n ? container_of(n, struct device, knode_parent) : NULL;
510 * device_for_each_child - device child iterator.
511 * @parent: parent struct device.
512 * @data: data for the callback.
513 * @fn: function to be called for each device.
515 * Iterate over @parent's child devices, and call @fn for each,
518 * We check the return of @fn each time. If it returns anything
519 * other than 0, we break out and return that value.
521 int device_for_each_child(struct device * parent, void * data,
522 int (*fn)(struct device *, void *))
525 struct device * child;
528 klist_iter_init(&parent->klist_children, &i);
529 while ((child = next_device(&i)) && !error)
530 error = fn(child, data);
535 int __init devices_init(void)
537 return subsystem_register(&devices_subsys);
540 EXPORT_SYMBOL_GPL(device_for_each_child);
542 EXPORT_SYMBOL_GPL(device_initialize);
543 EXPORT_SYMBOL_GPL(device_add);
544 EXPORT_SYMBOL_GPL(device_register);
546 EXPORT_SYMBOL_GPL(device_del);
547 EXPORT_SYMBOL_GPL(device_unregister);
548 EXPORT_SYMBOL_GPL(get_device);
549 EXPORT_SYMBOL_GPL(put_device);
551 EXPORT_SYMBOL_GPL(device_create_file);
552 EXPORT_SYMBOL_GPL(device_remove_file);
555 static void device_create_release(struct device *dev)
557 pr_debug("%s called for %s\n", __FUNCTION__, dev->bus_id);
562 * device_create - creates a device and registers it with sysfs
563 * @cs: pointer to the struct class that this device should be registered to.
564 * @parent: pointer to the parent struct device of this new device, if any.
565 * @dev: the dev_t for the char device to be added.
566 * @fmt: string for the class device's name
568 * This function can be used by char device classes. A struct
569 * device will be created in sysfs, registered to the specified
571 * A "dev" file will be created, showing the dev_t for the device, if
572 * the dev_t is not 0,0.
573 * If a pointer to a parent struct device is passed in, the newly
574 * created struct device will be a child of that device in sysfs. The
575 * pointer to the struct device will be returned from the call. Any
576 * further sysfs files that might be required can be created using this
579 * Note: the struct class passed to this function must have previously
580 * been created with a call to class_create().
582 struct device *device_create(struct class *class, struct device *parent,
583 dev_t devt, char *fmt, ...)
586 struct device *dev = NULL;
587 int retval = -ENODEV;
589 if (class == NULL || IS_ERR(class))
591 if (parent == NULL) {
592 printk(KERN_WARNING "%s does not work yet for NULL parents\n", __FUNCTION__);
596 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
604 dev->parent = parent;
605 dev->release = device_create_release;
608 vsnprintf(dev->bus_id, BUS_ID_SIZE, fmt, args);
610 retval = device_register(dev);
618 return ERR_PTR(retval);
620 EXPORT_SYMBOL_GPL(device_create);
623 * device_destroy - removes a device that was created with device_create()
624 * @class: the pointer to the struct class that this device was registered * with.
625 * @dev: the dev_t of the device that was previously registered.
627 * This call unregisters and cleans up a class device that was created with a
628 * call to class_device_create()
630 void device_destroy(struct class *class, dev_t devt)
632 struct device *dev = NULL;
633 struct device *dev_tmp;
636 list_for_each_entry(dev_tmp, &class->devices, node) {
637 if (dev_tmp->devt == devt) {
645 device_unregister(dev);
647 EXPORT_SYMBOL_GPL(device_destroy);