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
6 * Copyright (c) 2006 Greg Kroah-Hartman <gregkh@suse.de>
7 * Copyright (c) 2006 Novell, Inc.
9 * This file is released under the GPLv2
13 #include <linux/device.h>
14 #include <linux/err.h>
15 #include <linux/init.h>
16 #include <linux/module.h>
17 #include <linux/slab.h>
18 #include <linux/string.h>
19 #include <linux/kdev_t.h>
21 #include <asm/semaphore.h>
24 #include "power/power.h"
26 int (*platform_notify)(struct device * dev) = NULL;
27 int (*platform_notify_remove)(struct device * dev) = NULL;
30 * sysfs bindings for devices.
34 * dev_driver_string - Return a device's driver name, if at all possible
35 * @dev: struct device to get the name of
37 * Will return the device's driver's name if it is bound to a device. If
38 * the device is not bound to a device, it will return the name of the bus
39 * it is attached to. If it is not attached to a bus either, an empty
40 * string will be returned.
42 const char *dev_driver_string(struct device *dev)
44 return dev->driver ? dev->driver->name :
45 (dev->bus ? dev->bus->name : "");
47 EXPORT_SYMBOL(dev_driver_string);
49 #define to_dev(obj) container_of(obj, struct device, kobj)
50 #define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr)
53 dev_attr_show(struct kobject * kobj, struct attribute * attr, char * buf)
55 struct device_attribute * dev_attr = to_dev_attr(attr);
56 struct device * dev = to_dev(kobj);
60 ret = dev_attr->show(dev, dev_attr, buf);
65 dev_attr_store(struct kobject * kobj, struct attribute * attr,
66 const char * buf, size_t count)
68 struct device_attribute * dev_attr = to_dev_attr(attr);
69 struct device * dev = to_dev(kobj);
73 ret = dev_attr->store(dev, dev_attr, buf, count);
77 static struct sysfs_ops dev_sysfs_ops = {
78 .show = dev_attr_show,
79 .store = dev_attr_store,
84 * device_release - free device structure.
85 * @kobj: device's kobject.
87 * This is called once the reference count for the object
88 * reaches 0. We forward the call to the device's release
89 * method, which should handle actually freeing the structure.
91 static void device_release(struct kobject * kobj)
93 struct device * dev = to_dev(kobj);
97 else if (dev->class && dev->class->dev_release)
98 dev->class->dev_release(dev);
100 printk(KERN_ERR "Device '%s' does not have a release() function, "
101 "it is broken and must be fixed.\n",
107 static struct kobj_type ktype_device = {
108 .release = device_release,
109 .sysfs_ops = &dev_sysfs_ops,
113 static int dev_uevent_filter(struct kset *kset, struct kobject *kobj)
115 struct kobj_type *ktype = get_ktype(kobj);
117 if (ktype == &ktype_device) {
118 struct device *dev = to_dev(kobj);
127 static const char *dev_uevent_name(struct kset *kset, struct kobject *kobj)
129 struct device *dev = to_dev(kobj);
132 return dev->bus->name;
134 return dev->class->name;
138 static int dev_uevent(struct kset *kset, struct kobject *kobj, char **envp,
139 int num_envp, char *buffer, int buffer_size)
141 struct device *dev = to_dev(kobj);
146 /* add the major/minor if present */
147 if (MAJOR(dev->devt)) {
148 add_uevent_var(envp, num_envp, &i,
149 buffer, buffer_size, &length,
150 "MAJOR=%u", MAJOR(dev->devt));
151 add_uevent_var(envp, num_envp, &i,
152 buffer, buffer_size, &length,
153 "MINOR=%u", MINOR(dev->devt));
156 /* add bus name (same as SUBSYSTEM, deprecated) */
158 add_uevent_var(envp, num_envp, &i,
159 buffer, buffer_size, &length,
160 "PHYSDEVBUS=%s", dev->bus->name);
162 /* add driver name (PHYSDEV* values are deprecated)*/
164 add_uevent_var(envp, num_envp, &i,
165 buffer, buffer_size, &length,
166 "DRIVER=%s", dev->driver->name);
167 add_uevent_var(envp, num_envp, &i,
168 buffer, buffer_size, &length,
169 "PHYSDEVDRIVER=%s", dev->driver->name);
172 /* terminate, set to next free slot, shrink available space */
176 buffer = &buffer[length];
177 buffer_size -= length;
179 if (dev->bus && dev->bus->uevent) {
180 /* have the bus specific function add its stuff */
181 retval = dev->bus->uevent(dev, envp, num_envp, buffer, buffer_size);
183 pr_debug ("%s - uevent() returned %d\n",
184 __FUNCTION__, retval);
188 if (dev->class && dev->class->dev_uevent) {
189 /* have the class specific function add its stuff */
190 retval = dev->class->dev_uevent(dev, envp, num_envp, buffer, buffer_size);
192 pr_debug("%s - dev_uevent() returned %d\n",
193 __FUNCTION__, retval);
200 static struct kset_uevent_ops device_uevent_ops = {
201 .filter = dev_uevent_filter,
202 .name = dev_uevent_name,
203 .uevent = dev_uevent,
206 static ssize_t store_uevent(struct device *dev, struct device_attribute *attr,
207 const char *buf, size_t count)
209 kobject_uevent(&dev->kobj, KOBJ_ADD);
213 static int device_add_groups(struct device *dev)
219 for (i = 0; dev->groups[i]; i++) {
220 error = sysfs_create_group(&dev->kobj, dev->groups[i]);
223 sysfs_remove_group(&dev->kobj, dev->groups[i]);
232 static void device_remove_groups(struct device *dev)
236 for (i = 0; dev->groups[i]; i++) {
237 sysfs_remove_group(&dev->kobj, dev->groups[i]);
242 static int device_add_attrs(struct device *dev)
244 struct class *class = dev->class;
251 if (class->dev_attrs) {
252 for (i = 0; attr_name(class->dev_attrs[i]); i++) {
253 error = device_create_file(dev, &class->dev_attrs[i]);
260 device_remove_file(dev, &class->dev_attrs[i]);
264 static void device_remove_attrs(struct device *dev)
266 struct class *class = dev->class;
272 if (class->dev_attrs) {
273 for (i = 0; attr_name(class->dev_attrs[i]); i++)
274 device_remove_file(dev, &class->dev_attrs[i]);
279 static ssize_t show_dev(struct device *dev, struct device_attribute *attr,
282 return print_dev_t(buf, dev->devt);
286 * devices_subsys - structure to be registered with kobject core.
289 decl_subsys(devices, &ktype_device, &device_uevent_ops);
293 * device_create_file - create sysfs attribute file for device.
295 * @attr: device attribute descriptor.
298 int device_create_file(struct device * dev, struct device_attribute * attr)
301 if (get_device(dev)) {
302 error = sysfs_create_file(&dev->kobj, &attr->attr);
309 * device_remove_file - remove sysfs attribute file.
311 * @attr: device attribute descriptor.
314 void device_remove_file(struct device * dev, struct device_attribute * attr)
316 if (get_device(dev)) {
317 sysfs_remove_file(&dev->kobj, &attr->attr);
323 * device_create_bin_file - create sysfs binary attribute file for device.
325 * @attr: device binary attribute descriptor.
327 int device_create_bin_file(struct device *dev, struct bin_attribute *attr)
331 error = sysfs_create_bin_file(&dev->kobj, attr);
334 EXPORT_SYMBOL_GPL(device_create_bin_file);
337 * device_remove_bin_file - remove sysfs binary attribute file
339 * @attr: device binary attribute descriptor.
341 void device_remove_bin_file(struct device *dev, struct bin_attribute *attr)
344 sysfs_remove_bin_file(&dev->kobj, attr);
346 EXPORT_SYMBOL_GPL(device_remove_bin_file);
348 static void klist_children_get(struct klist_node *n)
350 struct device *dev = container_of(n, struct device, knode_parent);
355 static void klist_children_put(struct klist_node *n)
357 struct device *dev = container_of(n, struct device, knode_parent);
364 * device_initialize - init device structure.
367 * This prepares the device for use by other layers,
368 * including adding it to the device hierarchy.
369 * It is the first half of device_register(), if called by
370 * that, though it can also be called separately, so one
371 * may use @dev's fields (e.g. the refcount).
374 void device_initialize(struct device *dev)
376 kobj_set_kset_s(dev, devices_subsys);
377 kobject_init(&dev->kobj);
378 klist_init(&dev->klist_children, klist_children_get,
380 INIT_LIST_HEAD(&dev->dma_pools);
381 INIT_LIST_HEAD(&dev->node);
382 init_MUTEX(&dev->sem);
383 device_init_wakeup(dev, 0);
387 * device_add - add device to device hierarchy.
390 * This is part 2 of device_register(), though may be called
391 * separately _iff_ device_initialize() has been called separately.
393 * This adds it to the kobject hierarchy via kobject_add(), adds it
394 * to the global and sibling lists for the device, then
395 * adds it to the other relevant subsystems of the driver model.
397 int device_add(struct device *dev)
399 struct device *parent = NULL;
400 char *class_name = NULL;
401 struct class_interface *class_intf;
404 dev = get_device(dev);
405 if (!dev || !strlen(dev->bus_id))
408 /* if this is a class device, and has no parent, create one */
409 if ((dev->class) && (dev->parent == NULL)) {
410 error = virtual_device_parent(dev);
415 parent = get_device(dev->parent);
417 pr_debug("DEV: registering device: ID = '%s'\n", dev->bus_id);
419 /* first, register with generic layer. */
420 kobject_set_name(&dev->kobj, "%s", dev->bus_id);
422 dev->kobj.parent = &parent->kobj;
424 if ((error = kobject_add(&dev->kobj)))
427 /* notify platform of device entry */
429 platform_notify(dev);
431 dev->uevent_attr.attr.name = "uevent";
432 dev->uevent_attr.attr.mode = S_IWUSR;
434 dev->uevent_attr.attr.owner = dev->driver->owner;
435 dev->uevent_attr.store = store_uevent;
436 error = device_create_file(dev, &dev->uevent_attr);
440 if (MAJOR(dev->devt)) {
441 struct device_attribute *attr;
442 attr = kzalloc(sizeof(*attr), GFP_KERNEL);
445 goto ueventattrError;
447 attr->attr.name = "dev";
448 attr->attr.mode = S_IRUGO;
450 attr->attr.owner = dev->driver->owner;
451 attr->show = show_dev;
452 error = device_create_file(dev, attr);
455 goto ueventattrError;
458 dev->devt_attr = attr;
462 sysfs_create_link(&dev->kobj, &dev->class->subsys.kset.kobj,
464 sysfs_create_link(&dev->class->subsys.kset.kobj, &dev->kobj,
467 sysfs_create_link(&dev->kobj, &dev->parent->kobj, "device");
468 class_name = make_class_name(dev->class->name, &dev->kobj);
469 sysfs_create_link(&dev->parent->kobj, &dev->kobj, class_name);
473 if ((error = device_add_attrs(dev)))
475 if ((error = device_add_groups(dev)))
477 if ((error = device_pm_add(dev)))
479 if ((error = bus_add_device(dev)))
481 kobject_uevent(&dev->kobj, KOBJ_ADD);
482 if ((error = bus_attach_device(dev)))
485 klist_add_tail(&dev->knode_parent, &parent->klist_children);
488 down(&dev->class->sem);
489 /* tie the class to the device */
490 list_add_tail(&dev->node, &dev->class->devices);
492 /* notify any interfaces that the device is here */
493 list_for_each_entry(class_intf, &dev->class->interfaces, node)
494 if (class_intf->add_dev)
495 class_intf->add_dev(dev, class_intf);
496 up(&dev->class->sem);
503 bus_remove_device(dev);
505 device_pm_remove(dev);
507 device_remove_groups(dev);
509 device_remove_attrs(dev);
511 if (dev->devt_attr) {
512 device_remove_file(dev, dev->devt_attr);
513 kfree(dev->devt_attr);
516 device_remove_file(dev, &dev->uevent_attr);
518 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
519 kobject_del(&dev->kobj);
528 * device_register - register a device with the system.
529 * @dev: pointer to the device structure
531 * This happens in two clean steps - initialize the device
532 * and add it to the system. The two steps can be called
533 * separately, but this is the easiest and most common.
534 * I.e. you should only call the two helpers separately if
535 * have a clearly defined need to use and refcount the device
536 * before it is added to the hierarchy.
539 int device_register(struct device *dev)
541 device_initialize(dev);
542 return device_add(dev);
547 * get_device - increment reference count for device.
550 * This simply forwards the call to kobject_get(), though
551 * we do take care to provide for the case that we get a NULL
555 struct device * get_device(struct device * dev)
557 return dev ? to_dev(kobject_get(&dev->kobj)) : NULL;
562 * put_device - decrement reference count.
563 * @dev: device in question.
565 void put_device(struct device * dev)
568 kobject_put(&dev->kobj);
573 * device_del - delete device from system.
576 * This is the first part of the device unregistration
577 * sequence. This removes the device from the lists we control
578 * from here, has it removed from the other driver model
579 * subsystems it was added to in device_add(), and removes it
580 * from the kobject hierarchy.
582 * NOTE: this should be called manually _iff_ device_add() was
583 * also called manually.
586 void device_del(struct device * dev)
588 struct device * parent = dev->parent;
589 char *class_name = NULL;
590 struct class_interface *class_intf;
593 klist_del(&dev->knode_parent);
595 device_remove_file(dev, dev->devt_attr);
597 sysfs_remove_link(&dev->kobj, "subsystem");
598 sysfs_remove_link(&dev->class->subsys.kset.kobj, dev->bus_id);
599 class_name = make_class_name(dev->class->name, &dev->kobj);
601 sysfs_remove_link(&dev->kobj, "device");
602 sysfs_remove_link(&dev->parent->kobj, class_name);
605 down(&dev->class->sem);
606 /* notify any interfaces that the device is now gone */
607 list_for_each_entry(class_intf, &dev->class->interfaces, node)
608 if (class_intf->remove_dev)
609 class_intf->remove_dev(dev, class_intf);
610 /* remove the device from the class list */
611 list_del_init(&dev->node);
612 up(&dev->class->sem);
614 device_remove_file(dev, &dev->uevent_attr);
615 device_remove_groups(dev);
616 device_remove_attrs(dev);
618 /* Notify the platform of the removal, in case they
619 * need to do anything...
621 if (platform_notify_remove)
622 platform_notify_remove(dev);
623 bus_remove_device(dev);
624 device_pm_remove(dev);
625 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
626 kobject_del(&dev->kobj);
632 * device_unregister - unregister device from system.
633 * @dev: device going away.
635 * We do this in two parts, like we do device_register(). First,
636 * we remove it from all the subsystems with device_del(), then
637 * we decrement the reference count via put_device(). If that
638 * is the final reference count, the device will be cleaned up
639 * via device_release() above. Otherwise, the structure will
640 * stick around until the final reference to the device is dropped.
642 void device_unregister(struct device * dev)
644 pr_debug("DEV: Unregistering device. ID = '%s'\n", dev->bus_id);
650 static struct device * next_device(struct klist_iter * i)
652 struct klist_node * n = klist_next(i);
653 return n ? container_of(n, struct device, knode_parent) : NULL;
657 * device_for_each_child - device child iterator.
658 * @parent: parent struct device.
659 * @data: data for the callback.
660 * @fn: function to be called for each device.
662 * Iterate over @parent's child devices, and call @fn for each,
665 * We check the return of @fn each time. If it returns anything
666 * other than 0, we break out and return that value.
668 int device_for_each_child(struct device * parent, void * data,
669 int (*fn)(struct device *, void *))
672 struct device * child;
675 klist_iter_init(&parent->klist_children, &i);
676 while ((child = next_device(&i)) && !error)
677 error = fn(child, data);
682 int __init devices_init(void)
684 return subsystem_register(&devices_subsys);
687 EXPORT_SYMBOL_GPL(device_for_each_child);
689 EXPORT_SYMBOL_GPL(device_initialize);
690 EXPORT_SYMBOL_GPL(device_add);
691 EXPORT_SYMBOL_GPL(device_register);
693 EXPORT_SYMBOL_GPL(device_del);
694 EXPORT_SYMBOL_GPL(device_unregister);
695 EXPORT_SYMBOL_GPL(get_device);
696 EXPORT_SYMBOL_GPL(put_device);
698 EXPORT_SYMBOL_GPL(device_create_file);
699 EXPORT_SYMBOL_GPL(device_remove_file);
702 static void device_create_release(struct device *dev)
704 pr_debug("%s called for %s\n", __FUNCTION__, dev->bus_id);
709 * device_create - creates a device and registers it with sysfs
710 * @class: pointer to the struct class that this device should be registered to
711 * @parent: pointer to the parent struct device of this new device, if any
712 * @devt: the dev_t for the char device to be added
713 * @fmt: string for the device's name
715 * This function can be used by char device classes. A struct device
716 * will be created in sysfs, registered to the specified class.
718 * A "dev" file will be created, showing the dev_t for the device, if
719 * the dev_t is not 0,0.
720 * If a pointer to a parent struct device is passed in, the newly created
721 * struct device will be a child of that device in sysfs.
722 * The pointer to the struct device will be returned from the call.
723 * Any further sysfs files that might be required can be created using this
726 * Note: the struct class passed to this function must have previously
727 * been created with a call to class_create().
729 struct device *device_create(struct class *class, struct device *parent,
730 dev_t devt, const char *fmt, ...)
733 struct device *dev = NULL;
734 int retval = -ENODEV;
736 if (class == NULL || IS_ERR(class))
739 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
747 dev->parent = parent;
748 dev->release = device_create_release;
751 vsnprintf(dev->bus_id, BUS_ID_SIZE, fmt, args);
753 retval = device_register(dev);
761 return ERR_PTR(retval);
763 EXPORT_SYMBOL_GPL(device_create);
766 * device_destroy - removes a device that was created with device_create()
767 * @class: pointer to the struct class that this device was registered with
768 * @devt: the dev_t of the device that was previously registered
770 * This call unregisters and cleans up a device that was created with a
771 * call to device_create().
773 void device_destroy(struct class *class, dev_t devt)
775 struct device *dev = NULL;
776 struct device *dev_tmp;
779 list_for_each_entry(dev_tmp, &class->devices, node) {
780 if (dev_tmp->devt == devt) {
788 device_unregister(dev);
790 EXPORT_SYMBOL_GPL(device_destroy);
793 * device_rename - renames a device
794 * @dev: the pointer to the struct device to be renamed
795 * @new_name: the new name of the device
797 int device_rename(struct device *dev, char *new_name)
799 char *old_class_name = NULL;
800 char *new_class_name = NULL;
801 char *old_symlink_name = NULL;
804 dev = get_device(dev);
808 pr_debug("DEVICE: renaming '%s' to '%s'\n", dev->bus_id, new_name);
810 if ((dev->class) && (dev->parent))
811 old_class_name = make_class_name(dev->class->name, &dev->kobj);
814 old_symlink_name = kmalloc(BUS_ID_SIZE, GFP_KERNEL);
815 if (!old_symlink_name) {
817 goto out_free_old_class;
819 strlcpy(old_symlink_name, dev->bus_id, BUS_ID_SIZE);
822 strlcpy(dev->bus_id, new_name, BUS_ID_SIZE);
824 error = kobject_rename(&dev->kobj, new_name);
826 if (old_class_name) {
827 new_class_name = make_class_name(dev->class->name, &dev->kobj);
828 if (new_class_name) {
829 sysfs_create_link(&dev->parent->kobj, &dev->kobj,
831 sysfs_remove_link(&dev->parent->kobj, old_class_name);
835 sysfs_remove_link(&dev->class->subsys.kset.kobj,
837 sysfs_create_link(&dev->class->subsys.kset.kobj, &dev->kobj,
842 kfree(new_class_name);
843 kfree(old_symlink_name);
845 kfree(old_class_name);