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>
20 #include <linux/notifier.h>
22 #include <asm/semaphore.h>
25 #include "power/power.h"
27 int (*platform_notify)(struct device * dev) = NULL;
28 int (*platform_notify_remove)(struct device * dev) = NULL;
31 * sysfs bindings for devices.
35 * dev_driver_string - Return a device's driver name, if at all possible
36 * @dev: struct device to get the name of
38 * Will return the device's driver's name if it is bound to a device. If
39 * the device is not bound to a device, it will return the name of the bus
40 * it is attached to. If it is not attached to a bus either, an empty
41 * string will be returned.
43 const char *dev_driver_string(struct device *dev)
45 return dev->driver ? dev->driver->name :
46 (dev->bus ? dev->bus->name : "");
48 EXPORT_SYMBOL(dev_driver_string);
50 #define to_dev(obj) container_of(obj, struct device, kobj)
51 #define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr)
54 dev_attr_show(struct kobject * kobj, struct attribute * attr, char * buf)
56 struct device_attribute * dev_attr = to_dev_attr(attr);
57 struct device * dev = to_dev(kobj);
61 ret = dev_attr->show(dev, dev_attr, buf);
66 dev_attr_store(struct kobject * kobj, struct attribute * attr,
67 const char * buf, size_t count)
69 struct device_attribute * dev_attr = to_dev_attr(attr);
70 struct device * dev = to_dev(kobj);
74 ret = dev_attr->store(dev, dev_attr, buf, count);
78 static struct sysfs_ops dev_sysfs_ops = {
79 .show = dev_attr_show,
80 .store = dev_attr_store,
85 * device_release - free device structure.
86 * @kobj: device's kobject.
88 * This is called once the reference count for the object
89 * reaches 0. We forward the call to the device's release
90 * method, which should handle actually freeing the structure.
92 static void device_release(struct kobject * kobj)
94 struct device * dev = to_dev(kobj);
98 else if (dev->class && dev->class->dev_release)
99 dev->class->dev_release(dev);
101 printk(KERN_ERR "Device '%s' does not have a release() function, "
102 "it is broken and must be fixed.\n",
108 static struct kobj_type ktype_device = {
109 .release = device_release,
110 .sysfs_ops = &dev_sysfs_ops,
114 static int dev_uevent_filter(struct kset *kset, struct kobject *kobj)
116 struct kobj_type *ktype = get_ktype(kobj);
118 if (ktype == &ktype_device) {
119 struct device *dev = to_dev(kobj);
128 static const char *dev_uevent_name(struct kset *kset, struct kobject *kobj)
130 struct device *dev = to_dev(kobj);
133 return dev->bus->name;
135 return dev->class->name;
139 static int dev_uevent(struct kset *kset, struct kobject *kobj, char **envp,
140 int num_envp, char *buffer, int buffer_size)
142 struct device *dev = to_dev(kobj);
147 /* add the major/minor if present */
148 if (MAJOR(dev->devt)) {
149 add_uevent_var(envp, num_envp, &i,
150 buffer, buffer_size, &length,
151 "MAJOR=%u", MAJOR(dev->devt));
152 add_uevent_var(envp, num_envp, &i,
153 buffer, buffer_size, &length,
154 "MINOR=%u", MINOR(dev->devt));
157 /* add bus name (same as SUBSYSTEM, deprecated) */
159 add_uevent_var(envp, num_envp, &i,
160 buffer, buffer_size, &length,
161 "PHYSDEVBUS=%s", dev->bus->name);
163 /* add driver name (PHYSDEV* values are deprecated)*/
165 add_uevent_var(envp, num_envp, &i,
166 buffer, buffer_size, &length,
167 "DRIVER=%s", dev->driver->name);
168 add_uevent_var(envp, num_envp, &i,
169 buffer, buffer_size, &length,
170 "PHYSDEVDRIVER=%s", dev->driver->name);
173 /* terminate, set to next free slot, shrink available space */
177 buffer = &buffer[length];
178 buffer_size -= length;
180 if (dev->bus && dev->bus->uevent) {
181 /* have the bus specific function add its stuff */
182 retval = dev->bus->uevent(dev, envp, num_envp, buffer, buffer_size);
184 pr_debug ("%s - uevent() returned %d\n",
185 __FUNCTION__, retval);
189 if (dev->class && dev->class->dev_uevent) {
190 /* have the class specific function add its stuff */
191 retval = dev->class->dev_uevent(dev, envp, num_envp, buffer, buffer_size);
193 pr_debug("%s - dev_uevent() returned %d\n",
194 __FUNCTION__, retval);
201 static struct kset_uevent_ops device_uevent_ops = {
202 .filter = dev_uevent_filter,
203 .name = dev_uevent_name,
204 .uevent = dev_uevent,
207 static ssize_t store_uevent(struct device *dev, struct device_attribute *attr,
208 const char *buf, size_t count)
210 kobject_uevent(&dev->kobj, KOBJ_ADD);
214 static int device_add_groups(struct device *dev)
220 for (i = 0; dev->groups[i]; i++) {
221 error = sysfs_create_group(&dev->kobj, dev->groups[i]);
224 sysfs_remove_group(&dev->kobj, dev->groups[i]);
233 static void device_remove_groups(struct device *dev)
237 for (i = 0; dev->groups[i]; i++) {
238 sysfs_remove_group(&dev->kobj, dev->groups[i]);
243 static int device_add_attrs(struct device *dev)
245 struct class *class = dev->class;
252 if (class->dev_attrs) {
253 for (i = 0; attr_name(class->dev_attrs[i]); i++) {
254 error = device_create_file(dev, &class->dev_attrs[i]);
261 device_remove_file(dev, &class->dev_attrs[i]);
265 static void device_remove_attrs(struct device *dev)
267 struct class *class = dev->class;
273 if (class->dev_attrs) {
274 for (i = 0; attr_name(class->dev_attrs[i]); i++)
275 device_remove_file(dev, &class->dev_attrs[i]);
280 static ssize_t show_dev(struct device *dev, struct device_attribute *attr,
283 return print_dev_t(buf, dev->devt);
287 * devices_subsys - structure to be registered with kobject core.
290 decl_subsys(devices, &ktype_device, &device_uevent_ops);
294 * device_create_file - create sysfs attribute file for device.
296 * @attr: device attribute descriptor.
299 int device_create_file(struct device * dev, struct device_attribute * attr)
302 if (get_device(dev)) {
303 error = sysfs_create_file(&dev->kobj, &attr->attr);
310 * device_remove_file - remove sysfs attribute file.
312 * @attr: device attribute descriptor.
315 void device_remove_file(struct device * dev, struct device_attribute * attr)
317 if (get_device(dev)) {
318 sysfs_remove_file(&dev->kobj, &attr->attr);
324 * device_create_bin_file - create sysfs binary attribute file for device.
326 * @attr: device binary attribute descriptor.
328 int device_create_bin_file(struct device *dev, struct bin_attribute *attr)
332 error = sysfs_create_bin_file(&dev->kobj, attr);
335 EXPORT_SYMBOL_GPL(device_create_bin_file);
338 * device_remove_bin_file - remove sysfs binary attribute file
340 * @attr: device binary attribute descriptor.
342 void device_remove_bin_file(struct device *dev, struct bin_attribute *attr)
345 sysfs_remove_bin_file(&dev->kobj, attr);
347 EXPORT_SYMBOL_GPL(device_remove_bin_file);
349 static void klist_children_get(struct klist_node *n)
351 struct device *dev = container_of(n, struct device, knode_parent);
356 static void klist_children_put(struct klist_node *n)
358 struct device *dev = container_of(n, struct device, knode_parent);
365 * device_initialize - init device structure.
368 * This prepares the device for use by other layers,
369 * including adding it to the device hierarchy.
370 * It is the first half of device_register(), if called by
371 * that, though it can also be called separately, so one
372 * may use @dev's fields (e.g. the refcount).
375 void device_initialize(struct device *dev)
377 kobj_set_kset_s(dev, devices_subsys);
378 kobject_init(&dev->kobj);
379 klist_init(&dev->klist_children, klist_children_get,
381 INIT_LIST_HEAD(&dev->dma_pools);
382 INIT_LIST_HEAD(&dev->node);
383 init_MUTEX(&dev->sem);
384 device_init_wakeup(dev, 0);
387 #ifdef CONFIG_SYSFS_DEPRECATED
388 int setup_parent(struct device *dev, struct device *parent)
390 /* Set the parent to the class, not the parent device */
391 /* this keeps sysfs from having a symlink to make old udevs happy */
393 dev->kobj.parent = &dev->class->subsys.kset.kobj;
395 dev->kobj.parent = &parent->kobj;
400 static int virtual_device_parent(struct device *dev)
405 if (!dev->class->virtual_dir) {
406 static struct kobject *virtual_dir = NULL;
409 virtual_dir = kobject_add_dir(&devices_subsys.kset.kobj, "virtual");
410 dev->class->virtual_dir = kobject_add_dir(virtual_dir, dev->class->name);
413 dev->kobj.parent = dev->class->virtual_dir;
417 int setup_parent(struct device *dev, struct device *parent)
421 /* if this is a class device, and has no parent, create one */
422 if ((dev->class) && (parent == NULL)) {
423 error = virtual_device_parent(dev);
427 dev->kobj.parent = &parent->kobj;
434 * device_add - add device to device hierarchy.
437 * This is part 2 of device_register(), though may be called
438 * separately _iff_ device_initialize() has been called separately.
440 * This adds it to the kobject hierarchy via kobject_add(), adds it
441 * to the global and sibling lists for the device, then
442 * adds it to the other relevant subsystems of the driver model.
444 int device_add(struct device *dev)
446 struct device *parent = NULL;
447 char *class_name = NULL;
448 struct class_interface *class_intf;
451 dev = get_device(dev);
452 if (!dev || !strlen(dev->bus_id))
455 pr_debug("DEV: registering device: ID = '%s'\n", dev->bus_id);
457 parent = get_device(dev->parent);
459 error = setup_parent(dev, parent);
463 /* first, register with generic layer. */
464 kobject_set_name(&dev->kobj, "%s", dev->bus_id);
465 error = kobject_add(&dev->kobj);
469 /* notify platform of device entry */
471 platform_notify(dev);
473 /* notify clients of device entry (new way) */
475 blocking_notifier_call_chain(&dev->bus->bus_notifier,
476 BUS_NOTIFY_ADD_DEVICE, dev);
478 dev->uevent_attr.attr.name = "uevent";
479 dev->uevent_attr.attr.mode = S_IWUSR;
481 dev->uevent_attr.attr.owner = dev->driver->owner;
482 dev->uevent_attr.store = store_uevent;
483 error = device_create_file(dev, &dev->uevent_attr);
487 if (MAJOR(dev->devt)) {
488 struct device_attribute *attr;
489 attr = kzalloc(sizeof(*attr), GFP_KERNEL);
492 goto ueventattrError;
494 attr->attr.name = "dev";
495 attr->attr.mode = S_IRUGO;
497 attr->attr.owner = dev->driver->owner;
498 attr->show = show_dev;
499 error = device_create_file(dev, attr);
502 goto ueventattrError;
505 dev->devt_attr = attr;
509 sysfs_create_link(&dev->kobj, &dev->class->subsys.kset.kobj,
511 /* If this is not a "fake" compatible device, then create the
512 * symlink from the class to the device. */
513 if (dev->kobj.parent != &dev->class->subsys.kset.kobj)
514 sysfs_create_link(&dev->class->subsys.kset.kobj,
515 &dev->kobj, dev->bus_id);
516 #ifdef CONFIG_SYSFS_DEPRECATED
518 sysfs_create_link(&dev->kobj, &dev->parent->kobj, "device");
519 class_name = make_class_name(dev->class->name, &dev->kobj);
520 sysfs_create_link(&dev->parent->kobj, &dev->kobj, class_name);
525 if ((error = device_add_attrs(dev)))
527 if ((error = device_add_groups(dev)))
529 if ((error = device_pm_add(dev)))
531 if ((error = bus_add_device(dev)))
533 kobject_uevent(&dev->kobj, KOBJ_ADD);
534 if ((error = bus_attach_device(dev)))
537 klist_add_tail(&dev->knode_parent, &parent->klist_children);
540 down(&dev->class->sem);
541 /* tie the class to the device */
542 list_add_tail(&dev->node, &dev->class->devices);
544 /* notify any interfaces that the device is here */
545 list_for_each_entry(class_intf, &dev->class->interfaces, node)
546 if (class_intf->add_dev)
547 class_intf->add_dev(dev, class_intf);
548 up(&dev->class->sem);
555 bus_remove_device(dev);
557 device_pm_remove(dev);
560 blocking_notifier_call_chain(&dev->bus->bus_notifier,
561 BUS_NOTIFY_DEL_DEVICE, dev);
562 device_remove_groups(dev);
564 device_remove_attrs(dev);
566 if (dev->devt_attr) {
567 device_remove_file(dev, dev->devt_attr);
568 kfree(dev->devt_attr);
571 device_remove_file(dev, &dev->uevent_attr);
573 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
574 kobject_del(&dev->kobj);
583 * device_register - register a device with the system.
584 * @dev: pointer to the device structure
586 * This happens in two clean steps - initialize the device
587 * and add it to the system. The two steps can be called
588 * separately, but this is the easiest and most common.
589 * I.e. you should only call the two helpers separately if
590 * have a clearly defined need to use and refcount the device
591 * before it is added to the hierarchy.
594 int device_register(struct device *dev)
596 device_initialize(dev);
597 return device_add(dev);
602 * get_device - increment reference count for device.
605 * This simply forwards the call to kobject_get(), though
606 * we do take care to provide for the case that we get a NULL
610 struct device * get_device(struct device * dev)
612 return dev ? to_dev(kobject_get(&dev->kobj)) : NULL;
617 * put_device - decrement reference count.
618 * @dev: device in question.
620 void put_device(struct device * dev)
623 kobject_put(&dev->kobj);
628 * device_del - delete device from system.
631 * This is the first part of the device unregistration
632 * sequence. This removes the device from the lists we control
633 * from here, has it removed from the other driver model
634 * subsystems it was added to in device_add(), and removes it
635 * from the kobject hierarchy.
637 * NOTE: this should be called manually _iff_ device_add() was
638 * also called manually.
641 void device_del(struct device * dev)
643 struct device * parent = dev->parent;
644 struct class_interface *class_intf;
647 klist_del(&dev->knode_parent);
648 if (dev->devt_attr) {
649 device_remove_file(dev, dev->devt_attr);
650 kfree(dev->devt_attr);
653 sysfs_remove_link(&dev->kobj, "subsystem");
654 /* If this is not a "fake" compatible device, remove the
655 * symlink from the class to the device. */
656 if (dev->kobj.parent != &dev->class->subsys.kset.kobj)
657 sysfs_remove_link(&dev->class->subsys.kset.kobj,
659 #ifdef CONFIG_SYSFS_DEPRECATED
661 char *class_name = make_class_name(dev->class->name,
663 sysfs_remove_link(&dev->parent->kobj, class_name);
665 sysfs_remove_link(&dev->kobj, "device");
669 down(&dev->class->sem);
670 /* notify any interfaces that the device is now gone */
671 list_for_each_entry(class_intf, &dev->class->interfaces, node)
672 if (class_intf->remove_dev)
673 class_intf->remove_dev(dev, class_intf);
674 /* remove the device from the class list */
675 list_del_init(&dev->node);
676 up(&dev->class->sem);
678 device_remove_file(dev, &dev->uevent_attr);
679 device_remove_groups(dev);
680 device_remove_attrs(dev);
682 /* Notify the platform of the removal, in case they
683 * need to do anything...
685 if (platform_notify_remove)
686 platform_notify_remove(dev);
688 blocking_notifier_call_chain(&dev->bus->bus_notifier,
689 BUS_NOTIFY_DEL_DEVICE, dev);
690 bus_remove_device(dev);
691 device_pm_remove(dev);
692 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
693 kobject_del(&dev->kobj);
699 * device_unregister - unregister device from system.
700 * @dev: device going away.
702 * We do this in two parts, like we do device_register(). First,
703 * we remove it from all the subsystems with device_del(), then
704 * we decrement the reference count via put_device(). If that
705 * is the final reference count, the device will be cleaned up
706 * via device_release() above. Otherwise, the structure will
707 * stick around until the final reference to the device is dropped.
709 void device_unregister(struct device * dev)
711 pr_debug("DEV: Unregistering device. ID = '%s'\n", dev->bus_id);
717 static struct device * next_device(struct klist_iter * i)
719 struct klist_node * n = klist_next(i);
720 return n ? container_of(n, struct device, knode_parent) : NULL;
724 * device_for_each_child - device child iterator.
725 * @parent: parent struct device.
726 * @data: data for the callback.
727 * @fn: function to be called for each device.
729 * Iterate over @parent's child devices, and call @fn for each,
732 * We check the return of @fn each time. If it returns anything
733 * other than 0, we break out and return that value.
735 int device_for_each_child(struct device * parent, void * data,
736 int (*fn)(struct device *, void *))
739 struct device * child;
742 klist_iter_init(&parent->klist_children, &i);
743 while ((child = next_device(&i)) && !error)
744 error = fn(child, data);
749 int __init devices_init(void)
751 return subsystem_register(&devices_subsys);
754 EXPORT_SYMBOL_GPL(device_for_each_child);
756 EXPORT_SYMBOL_GPL(device_initialize);
757 EXPORT_SYMBOL_GPL(device_add);
758 EXPORT_SYMBOL_GPL(device_register);
760 EXPORT_SYMBOL_GPL(device_del);
761 EXPORT_SYMBOL_GPL(device_unregister);
762 EXPORT_SYMBOL_GPL(get_device);
763 EXPORT_SYMBOL_GPL(put_device);
765 EXPORT_SYMBOL_GPL(device_create_file);
766 EXPORT_SYMBOL_GPL(device_remove_file);
769 static void device_create_release(struct device *dev)
771 pr_debug("%s called for %s\n", __FUNCTION__, dev->bus_id);
776 * device_create - creates a device and registers it with sysfs
777 * @class: pointer to the struct class that this device should be registered to
778 * @parent: pointer to the parent struct device of this new device, if any
779 * @devt: the dev_t for the char device to be added
780 * @fmt: string for the device's name
782 * This function can be used by char device classes. A struct device
783 * will be created in sysfs, registered to the specified class.
785 * A "dev" file will be created, showing the dev_t for the device, if
786 * the dev_t is not 0,0.
787 * If a pointer to a parent struct device is passed in, the newly created
788 * struct device will be a child of that device in sysfs.
789 * The pointer to the struct device will be returned from the call.
790 * Any further sysfs files that might be required can be created using this
793 * Note: the struct class passed to this function must have previously
794 * been created with a call to class_create().
796 struct device *device_create(struct class *class, struct device *parent,
797 dev_t devt, const char *fmt, ...)
800 struct device *dev = NULL;
801 int retval = -ENODEV;
803 if (class == NULL || IS_ERR(class))
806 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
814 dev->parent = parent;
815 dev->release = device_create_release;
818 vsnprintf(dev->bus_id, BUS_ID_SIZE, fmt, args);
820 retval = device_register(dev);
828 return ERR_PTR(retval);
830 EXPORT_SYMBOL_GPL(device_create);
833 * device_destroy - removes a device that was created with device_create()
834 * @class: pointer to the struct class that this device was registered with
835 * @devt: the dev_t of the device that was previously registered
837 * This call unregisters and cleans up a device that was created with a
838 * call to device_create().
840 void device_destroy(struct class *class, dev_t devt)
842 struct device *dev = NULL;
843 struct device *dev_tmp;
846 list_for_each_entry(dev_tmp, &class->devices, node) {
847 if (dev_tmp->devt == devt) {
855 device_unregister(dev);
857 EXPORT_SYMBOL_GPL(device_destroy);
860 * device_rename - renames a device
861 * @dev: the pointer to the struct device to be renamed
862 * @new_name: the new name of the device
864 int device_rename(struct device *dev, char *new_name)
866 char *old_class_name = NULL;
867 char *new_class_name = NULL;
868 char *old_symlink_name = NULL;
871 dev = get_device(dev);
875 pr_debug("DEVICE: renaming '%s' to '%s'\n", dev->bus_id, new_name);
877 #ifdef CONFIG_SYSFS_DEPRECATED
878 if ((dev->class) && (dev->parent))
879 old_class_name = make_class_name(dev->class->name, &dev->kobj);
883 old_symlink_name = kmalloc(BUS_ID_SIZE, GFP_KERNEL);
884 if (!old_symlink_name) {
886 goto out_free_old_class;
888 strlcpy(old_symlink_name, dev->bus_id, BUS_ID_SIZE);
891 strlcpy(dev->bus_id, new_name, BUS_ID_SIZE);
893 error = kobject_rename(&dev->kobj, new_name);
895 #ifdef CONFIG_SYSFS_DEPRECATED
896 if (old_class_name) {
897 new_class_name = make_class_name(dev->class->name, &dev->kobj);
898 if (new_class_name) {
899 sysfs_create_link(&dev->parent->kobj, &dev->kobj,
901 sysfs_remove_link(&dev->parent->kobj, old_class_name);
907 sysfs_remove_link(&dev->class->subsys.kset.kobj,
909 sysfs_create_link(&dev->class->subsys.kset.kobj, &dev->kobj,
914 kfree(new_class_name);
915 kfree(old_symlink_name);
917 kfree(old_class_name);