11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * drivers/base/core.c - core driver model code (device registration, etc) 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * Copyright (c) 2002-3 Patrick Mochel 51da177e4SLinus Torvalds * Copyright (c) 2002-3 Open Source Development Labs 61da177e4SLinus Torvalds * 71da177e4SLinus Torvalds * This file is released under the GPLv2 81da177e4SLinus Torvalds * 91da177e4SLinus Torvalds */ 101da177e4SLinus Torvalds 111da177e4SLinus Torvalds #include <linux/device.h> 121da177e4SLinus Torvalds #include <linux/err.h> 131da177e4SLinus Torvalds #include <linux/init.h> 141da177e4SLinus Torvalds #include <linux/module.h> 151da177e4SLinus Torvalds #include <linux/slab.h> 161da177e4SLinus Torvalds #include <linux/string.h> 1723681e47SGreg Kroah-Hartman #include <linux/kdev_t.h> 181da177e4SLinus Torvalds 191da177e4SLinus Torvalds #include <asm/semaphore.h> 201da177e4SLinus Torvalds 211da177e4SLinus Torvalds #include "base.h" 221da177e4SLinus Torvalds #include "power/power.h" 231da177e4SLinus Torvalds 241da177e4SLinus Torvalds int (*platform_notify)(struct device * dev) = NULL; 251da177e4SLinus Torvalds int (*platform_notify_remove)(struct device * dev) = NULL; 261da177e4SLinus Torvalds 271da177e4SLinus Torvalds /* 281da177e4SLinus Torvalds * sysfs bindings for devices. 291da177e4SLinus Torvalds */ 301da177e4SLinus Torvalds 313e95637aSAlan Stern /** 323e95637aSAlan Stern * dev_driver_string - Return a device's driver name, if at all possible 333e95637aSAlan Stern * @dev: struct device to get the name of 343e95637aSAlan Stern * 353e95637aSAlan Stern * Will return the device's driver's name if it is bound to a device. If 363e95637aSAlan Stern * the device is not bound to a device, it will return the name of the bus 373e95637aSAlan Stern * it is attached to. If it is not attached to a bus either, an empty 383e95637aSAlan Stern * string will be returned. 393e95637aSAlan Stern */ 403e95637aSAlan Stern const char *dev_driver_string(struct device *dev) 413e95637aSAlan Stern { 423e95637aSAlan Stern return dev->driver ? dev->driver->name : 433e95637aSAlan Stern (dev->bus ? dev->bus->name : ""); 443e95637aSAlan Stern } 453e95637aSAlan Stern EXPORT_SYMBOL_GPL(dev_driver_string); 463e95637aSAlan Stern 471da177e4SLinus Torvalds #define to_dev(obj) container_of(obj, struct device, kobj) 481da177e4SLinus Torvalds #define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr) 491da177e4SLinus Torvalds 501da177e4SLinus Torvalds static ssize_t 511da177e4SLinus Torvalds dev_attr_show(struct kobject * kobj, struct attribute * attr, char * buf) 521da177e4SLinus Torvalds { 531da177e4SLinus Torvalds struct device_attribute * dev_attr = to_dev_attr(attr); 541da177e4SLinus Torvalds struct device * dev = to_dev(kobj); 554a0c20bfSDmitry Torokhov ssize_t ret = -EIO; 561da177e4SLinus Torvalds 571da177e4SLinus Torvalds if (dev_attr->show) 5854b6f35cSYani Ioannou ret = dev_attr->show(dev, dev_attr, buf); 591da177e4SLinus Torvalds return ret; 601da177e4SLinus Torvalds } 611da177e4SLinus Torvalds 621da177e4SLinus Torvalds static ssize_t 631da177e4SLinus Torvalds dev_attr_store(struct kobject * kobj, struct attribute * attr, 641da177e4SLinus Torvalds const char * buf, size_t count) 651da177e4SLinus Torvalds { 661da177e4SLinus Torvalds struct device_attribute * dev_attr = to_dev_attr(attr); 671da177e4SLinus Torvalds struct device * dev = to_dev(kobj); 684a0c20bfSDmitry Torokhov ssize_t ret = -EIO; 691da177e4SLinus Torvalds 701da177e4SLinus Torvalds if (dev_attr->store) 7154b6f35cSYani Ioannou ret = dev_attr->store(dev, dev_attr, buf, count); 721da177e4SLinus Torvalds return ret; 731da177e4SLinus Torvalds } 741da177e4SLinus Torvalds 751da177e4SLinus Torvalds static struct sysfs_ops dev_sysfs_ops = { 761da177e4SLinus Torvalds .show = dev_attr_show, 771da177e4SLinus Torvalds .store = dev_attr_store, 781da177e4SLinus Torvalds }; 791da177e4SLinus Torvalds 801da177e4SLinus Torvalds 811da177e4SLinus Torvalds /** 821da177e4SLinus Torvalds * device_release - free device structure. 831da177e4SLinus Torvalds * @kobj: device's kobject. 841da177e4SLinus Torvalds * 851da177e4SLinus Torvalds * This is called once the reference count for the object 861da177e4SLinus Torvalds * reaches 0. We forward the call to the device's release 871da177e4SLinus Torvalds * method, which should handle actually freeing the structure. 881da177e4SLinus Torvalds */ 891da177e4SLinus Torvalds static void device_release(struct kobject * kobj) 901da177e4SLinus Torvalds { 911da177e4SLinus Torvalds struct device * dev = to_dev(kobj); 921da177e4SLinus Torvalds 931da177e4SLinus Torvalds if (dev->release) 941da177e4SLinus Torvalds dev->release(dev); 951da177e4SLinus Torvalds else { 961da177e4SLinus Torvalds printk(KERN_ERR "Device '%s' does not have a release() function, " 971da177e4SLinus Torvalds "it is broken and must be fixed.\n", 981da177e4SLinus Torvalds dev->bus_id); 991da177e4SLinus Torvalds WARN_ON(1); 1001da177e4SLinus Torvalds } 1011da177e4SLinus Torvalds } 1021da177e4SLinus Torvalds 1031da177e4SLinus Torvalds static struct kobj_type ktype_device = { 1041da177e4SLinus Torvalds .release = device_release, 1051da177e4SLinus Torvalds .sysfs_ops = &dev_sysfs_ops, 1061da177e4SLinus Torvalds }; 1071da177e4SLinus Torvalds 1081da177e4SLinus Torvalds 109312c004dSKay Sievers static int dev_uevent_filter(struct kset *kset, struct kobject *kobj) 1101da177e4SLinus Torvalds { 1111da177e4SLinus Torvalds struct kobj_type *ktype = get_ktype(kobj); 1121da177e4SLinus Torvalds 1131da177e4SLinus Torvalds if (ktype == &ktype_device) { 1141da177e4SLinus Torvalds struct device *dev = to_dev(kobj); 1151da177e4SLinus Torvalds if (dev->bus) 1161da177e4SLinus Torvalds return 1; 11723681e47SGreg Kroah-Hartman if (dev->class) 11823681e47SGreg Kroah-Hartman return 1; 1191da177e4SLinus Torvalds } 1201da177e4SLinus Torvalds return 0; 1211da177e4SLinus Torvalds } 1221da177e4SLinus Torvalds 123312c004dSKay Sievers static const char *dev_uevent_name(struct kset *kset, struct kobject *kobj) 1241da177e4SLinus Torvalds { 1251da177e4SLinus Torvalds struct device *dev = to_dev(kobj); 1261da177e4SLinus Torvalds 12723681e47SGreg Kroah-Hartman if (dev->bus) 1281da177e4SLinus Torvalds return dev->bus->name; 12923681e47SGreg Kroah-Hartman if (dev->class) 13023681e47SGreg Kroah-Hartman return dev->class->name; 13123681e47SGreg Kroah-Hartman return NULL; 1321da177e4SLinus Torvalds } 1331da177e4SLinus Torvalds 134312c004dSKay Sievers static int dev_uevent(struct kset *kset, struct kobject *kobj, char **envp, 1351da177e4SLinus Torvalds int num_envp, char *buffer, int buffer_size) 1361da177e4SLinus Torvalds { 1371da177e4SLinus Torvalds struct device *dev = to_dev(kobj); 1381da177e4SLinus Torvalds int i = 0; 1391da177e4SLinus Torvalds int length = 0; 1401da177e4SLinus Torvalds int retval = 0; 1411da177e4SLinus Torvalds 14223681e47SGreg Kroah-Hartman /* add the major/minor if present */ 14323681e47SGreg Kroah-Hartman if (MAJOR(dev->devt)) { 14423681e47SGreg Kroah-Hartman add_uevent_var(envp, num_envp, &i, 14523681e47SGreg Kroah-Hartman buffer, buffer_size, &length, 14623681e47SGreg Kroah-Hartman "MAJOR=%u", MAJOR(dev->devt)); 14723681e47SGreg Kroah-Hartman add_uevent_var(envp, num_envp, &i, 14823681e47SGreg Kroah-Hartman buffer, buffer_size, &length, 14923681e47SGreg Kroah-Hartman "MINOR=%u", MINOR(dev->devt)); 15023681e47SGreg Kroah-Hartman } 15123681e47SGreg Kroah-Hartman 152d81d9d6bSKay Sievers /* add bus name (same as SUBSYSTEM, deprecated) */ 1531da177e4SLinus Torvalds if (dev->bus) 154312c004dSKay Sievers add_uevent_var(envp, num_envp, &i, 1551da177e4SLinus Torvalds buffer, buffer_size, &length, 1561da177e4SLinus Torvalds "PHYSDEVBUS=%s", dev->bus->name); 1571da177e4SLinus Torvalds 158d81d9d6bSKay Sievers /* add driver name (PHYSDEV* values are deprecated)*/ 159d81d9d6bSKay Sievers if (dev->driver) { 160d81d9d6bSKay Sievers add_uevent_var(envp, num_envp, &i, 161d81d9d6bSKay Sievers buffer, buffer_size, &length, 162d81d9d6bSKay Sievers "DRIVER=%s", dev->driver->name); 163312c004dSKay Sievers add_uevent_var(envp, num_envp, &i, 1641da177e4SLinus Torvalds buffer, buffer_size, &length, 1651da177e4SLinus Torvalds "PHYSDEVDRIVER=%s", dev->driver->name); 166d81d9d6bSKay Sievers } 1671da177e4SLinus Torvalds 1681da177e4SLinus Torvalds /* terminate, set to next free slot, shrink available space */ 1691da177e4SLinus Torvalds envp[i] = NULL; 1701da177e4SLinus Torvalds envp = &envp[i]; 1711da177e4SLinus Torvalds num_envp -= i; 1721da177e4SLinus Torvalds buffer = &buffer[length]; 1731da177e4SLinus Torvalds buffer_size -= length; 1741da177e4SLinus Torvalds 175312c004dSKay Sievers if (dev->bus && dev->bus->uevent) { 1761da177e4SLinus Torvalds /* have the bus specific function add its stuff */ 177312c004dSKay Sievers retval = dev->bus->uevent(dev, envp, num_envp, buffer, buffer_size); 1781da177e4SLinus Torvalds if (retval) { 179312c004dSKay Sievers pr_debug ("%s - uevent() returned %d\n", 1801da177e4SLinus Torvalds __FUNCTION__, retval); 1811da177e4SLinus Torvalds } 1821da177e4SLinus Torvalds } 1831da177e4SLinus Torvalds 1841da177e4SLinus Torvalds return retval; 1851da177e4SLinus Torvalds } 1861da177e4SLinus Torvalds 187312c004dSKay Sievers static struct kset_uevent_ops device_uevent_ops = { 188312c004dSKay Sievers .filter = dev_uevent_filter, 189312c004dSKay Sievers .name = dev_uevent_name, 190312c004dSKay Sievers .uevent = dev_uevent, 1911da177e4SLinus Torvalds }; 1921da177e4SLinus Torvalds 193a7fd6706SKay Sievers static ssize_t store_uevent(struct device *dev, struct device_attribute *attr, 194a7fd6706SKay Sievers const char *buf, size_t count) 195a7fd6706SKay Sievers { 196312c004dSKay Sievers kobject_uevent(&dev->kobj, KOBJ_ADD); 197a7fd6706SKay Sievers return count; 198a7fd6706SKay Sievers } 199a7fd6706SKay Sievers 20023681e47SGreg Kroah-Hartman static ssize_t show_dev(struct device *dev, struct device_attribute *attr, 20123681e47SGreg Kroah-Hartman char *buf) 20223681e47SGreg Kroah-Hartman { 20323681e47SGreg Kroah-Hartman return print_dev_t(buf, dev->devt); 20423681e47SGreg Kroah-Hartman } 20523681e47SGreg Kroah-Hartman 2060863afb3SMartin Waitz /* 2070863afb3SMartin Waitz * devices_subsys - structure to be registered with kobject core. 2081da177e4SLinus Torvalds */ 2091da177e4SLinus Torvalds 210312c004dSKay Sievers decl_subsys(devices, &ktype_device, &device_uevent_ops); 2111da177e4SLinus Torvalds 2121da177e4SLinus Torvalds 2131da177e4SLinus Torvalds /** 2141da177e4SLinus Torvalds * device_create_file - create sysfs attribute file for device. 2151da177e4SLinus Torvalds * @dev: device. 2161da177e4SLinus Torvalds * @attr: device attribute descriptor. 2171da177e4SLinus Torvalds */ 2181da177e4SLinus Torvalds 2191da177e4SLinus Torvalds int device_create_file(struct device * dev, struct device_attribute * attr) 2201da177e4SLinus Torvalds { 2211da177e4SLinus Torvalds int error = 0; 2221da177e4SLinus Torvalds if (get_device(dev)) { 2231da177e4SLinus Torvalds error = sysfs_create_file(&dev->kobj, &attr->attr); 2241da177e4SLinus Torvalds put_device(dev); 2251da177e4SLinus Torvalds } 2261da177e4SLinus Torvalds return error; 2271da177e4SLinus Torvalds } 2281da177e4SLinus Torvalds 2291da177e4SLinus Torvalds /** 2301da177e4SLinus Torvalds * device_remove_file - remove sysfs attribute file. 2311da177e4SLinus Torvalds * @dev: device. 2321da177e4SLinus Torvalds * @attr: device attribute descriptor. 2331da177e4SLinus Torvalds */ 2341da177e4SLinus Torvalds 2351da177e4SLinus Torvalds void device_remove_file(struct device * dev, struct device_attribute * attr) 2361da177e4SLinus Torvalds { 2371da177e4SLinus Torvalds if (get_device(dev)) { 2381da177e4SLinus Torvalds sysfs_remove_file(&dev->kobj, &attr->attr); 2391da177e4SLinus Torvalds put_device(dev); 2401da177e4SLinus Torvalds } 2411da177e4SLinus Torvalds } 2421da177e4SLinus Torvalds 24334bb61f9SJames Bottomley static void klist_children_get(struct klist_node *n) 24434bb61f9SJames Bottomley { 24534bb61f9SJames Bottomley struct device *dev = container_of(n, struct device, knode_parent); 24634bb61f9SJames Bottomley 24734bb61f9SJames Bottomley get_device(dev); 24834bb61f9SJames Bottomley } 24934bb61f9SJames Bottomley 25034bb61f9SJames Bottomley static void klist_children_put(struct klist_node *n) 25134bb61f9SJames Bottomley { 25234bb61f9SJames Bottomley struct device *dev = container_of(n, struct device, knode_parent); 25334bb61f9SJames Bottomley 25434bb61f9SJames Bottomley put_device(dev); 25534bb61f9SJames Bottomley } 25634bb61f9SJames Bottomley 2571da177e4SLinus Torvalds 2581da177e4SLinus Torvalds /** 2591da177e4SLinus Torvalds * device_initialize - init device structure. 2601da177e4SLinus Torvalds * @dev: device. 2611da177e4SLinus Torvalds * 2621da177e4SLinus Torvalds * This prepares the device for use by other layers, 2631da177e4SLinus Torvalds * including adding it to the device hierarchy. 2641da177e4SLinus Torvalds * It is the first half of device_register(), if called by 2651da177e4SLinus Torvalds * that, though it can also be called separately, so one 2661da177e4SLinus Torvalds * may use @dev's fields (e.g. the refcount). 2671da177e4SLinus Torvalds */ 2681da177e4SLinus Torvalds 2691da177e4SLinus Torvalds void device_initialize(struct device *dev) 2701da177e4SLinus Torvalds { 2711da177e4SLinus Torvalds kobj_set_kset_s(dev, devices_subsys); 2721da177e4SLinus Torvalds kobject_init(&dev->kobj); 27334bb61f9SJames Bottomley klist_init(&dev->klist_children, klist_children_get, 27434bb61f9SJames Bottomley klist_children_put); 2751da177e4SLinus Torvalds INIT_LIST_HEAD(&dev->dma_pools); 27623681e47SGreg Kroah-Hartman INIT_LIST_HEAD(&dev->node); 277af70316aSmochel@digitalimplant.org init_MUTEX(&dev->sem); 2780ac85241SDavid Brownell device_init_wakeup(dev, 0); 2791da177e4SLinus Torvalds } 2801da177e4SLinus Torvalds 2811da177e4SLinus Torvalds /** 2821da177e4SLinus Torvalds * device_add - add device to device hierarchy. 2831da177e4SLinus Torvalds * @dev: device. 2841da177e4SLinus Torvalds * 2851da177e4SLinus Torvalds * This is part 2 of device_register(), though may be called 2861da177e4SLinus Torvalds * separately _iff_ device_initialize() has been called separately. 2871da177e4SLinus Torvalds * 2881da177e4SLinus Torvalds * This adds it to the kobject hierarchy via kobject_add(), adds it 2891da177e4SLinus Torvalds * to the global and sibling lists for the device, then 2901da177e4SLinus Torvalds * adds it to the other relevant subsystems of the driver model. 2911da177e4SLinus Torvalds */ 2921da177e4SLinus Torvalds int device_add(struct device *dev) 2931da177e4SLinus Torvalds { 2941da177e4SLinus Torvalds struct device *parent = NULL; 295e9a7d305SGreg Kroah-Hartman char *class_name = NULL; 2961da177e4SLinus Torvalds int error = -EINVAL; 2971da177e4SLinus Torvalds 2981da177e4SLinus Torvalds dev = get_device(dev); 2991da177e4SLinus Torvalds if (!dev || !strlen(dev->bus_id)) 3001da177e4SLinus Torvalds goto Error; 3011da177e4SLinus Torvalds 3021da177e4SLinus Torvalds parent = get_device(dev->parent); 3031da177e4SLinus Torvalds 3041da177e4SLinus Torvalds pr_debug("DEV: registering device: ID = '%s'\n", dev->bus_id); 3051da177e4SLinus Torvalds 3061da177e4SLinus Torvalds /* first, register with generic layer. */ 3071da177e4SLinus Torvalds kobject_set_name(&dev->kobj, "%s", dev->bus_id); 3081da177e4SLinus Torvalds if (parent) 3091da177e4SLinus Torvalds dev->kobj.parent = &parent->kobj; 3101da177e4SLinus Torvalds 3111da177e4SLinus Torvalds if ((error = kobject_add(&dev->kobj))) 3121da177e4SLinus Torvalds goto Error; 313a7fd6706SKay Sievers 314a7fd6706SKay Sievers dev->uevent_attr.attr.name = "uevent"; 315a7fd6706SKay Sievers dev->uevent_attr.attr.mode = S_IWUSR; 316a7fd6706SKay Sievers if (dev->driver) 317a7fd6706SKay Sievers dev->uevent_attr.attr.owner = dev->driver->owner; 318a7fd6706SKay Sievers dev->uevent_attr.store = store_uevent; 319a7fd6706SKay Sievers device_create_file(dev, &dev->uevent_attr); 320a7fd6706SKay Sievers 32123681e47SGreg Kroah-Hartman if (MAJOR(dev->devt)) { 32223681e47SGreg Kroah-Hartman struct device_attribute *attr; 32323681e47SGreg Kroah-Hartman attr = kzalloc(sizeof(*attr), GFP_KERNEL); 32423681e47SGreg Kroah-Hartman if (!attr) { 32523681e47SGreg Kroah-Hartman error = -ENOMEM; 32623681e47SGreg Kroah-Hartman goto PMError; 32723681e47SGreg Kroah-Hartman } 32823681e47SGreg Kroah-Hartman attr->attr.name = "dev"; 32923681e47SGreg Kroah-Hartman attr->attr.mode = S_IRUGO; 33023681e47SGreg Kroah-Hartman if (dev->driver) 33123681e47SGreg Kroah-Hartman attr->attr.owner = dev->driver->owner; 33223681e47SGreg Kroah-Hartman attr->show = show_dev; 33323681e47SGreg Kroah-Hartman error = device_create_file(dev, attr); 33423681e47SGreg Kroah-Hartman if (error) { 33523681e47SGreg Kroah-Hartman kfree(attr); 33623681e47SGreg Kroah-Hartman goto attrError; 33723681e47SGreg Kroah-Hartman } 33823681e47SGreg Kroah-Hartman 33923681e47SGreg Kroah-Hartman dev->devt_attr = attr; 34023681e47SGreg Kroah-Hartman } 34123681e47SGreg Kroah-Hartman 342b9d9c82bSKay Sievers if (dev->class) { 343b9d9c82bSKay Sievers sysfs_create_link(&dev->kobj, &dev->class->subsys.kset.kobj, 344b9d9c82bSKay Sievers "subsystem"); 34523681e47SGreg Kroah-Hartman sysfs_create_link(&dev->class->subsys.kset.kobj, &dev->kobj, 34623681e47SGreg Kroah-Hartman dev->bus_id); 347e9a7d305SGreg Kroah-Hartman 348e9a7d305SGreg Kroah-Hartman sysfs_create_link(&dev->kobj, &dev->parent->kobj, "device"); 349e9a7d305SGreg Kroah-Hartman class_name = make_class_name(dev->class->name, &dev->kobj); 350e9a7d305SGreg Kroah-Hartman sysfs_create_link(&dev->parent->kobj, &dev->kobj, class_name); 351b9d9c82bSKay Sievers } 35223681e47SGreg Kroah-Hartman 3531da177e4SLinus Torvalds if ((error = device_pm_add(dev))) 3541da177e4SLinus Torvalds goto PMError; 3551da177e4SLinus Torvalds if ((error = bus_add_device(dev))) 3561da177e4SLinus Torvalds goto BusError; 35753877d06SKay Sievers kobject_uevent(&dev->kobj, KOBJ_ADD); 35853877d06SKay Sievers bus_attach_device(dev); 3591da177e4SLinus Torvalds if (parent) 360d856f1e3SJames Bottomley klist_add_tail(&dev->knode_parent, &parent->klist_children); 3611da177e4SLinus Torvalds 3625d9fd169SGreg Kroah-Hartman if (dev->class) { 3635d9fd169SGreg Kroah-Hartman /* tie the class to the device */ 3645d9fd169SGreg Kroah-Hartman down(&dev->class->sem); 3655d9fd169SGreg Kroah-Hartman list_add_tail(&dev->node, &dev->class->devices); 3665d9fd169SGreg Kroah-Hartman up(&dev->class->sem); 3675d9fd169SGreg Kroah-Hartman } 3685d9fd169SGreg Kroah-Hartman 3691da177e4SLinus Torvalds /* notify platform of device entry */ 3701da177e4SLinus Torvalds if (platform_notify) 3711da177e4SLinus Torvalds platform_notify(dev); 3721da177e4SLinus Torvalds Done: 373e9a7d305SGreg Kroah-Hartman kfree(class_name); 3741da177e4SLinus Torvalds put_device(dev); 3751da177e4SLinus Torvalds return error; 3761da177e4SLinus Torvalds BusError: 3771da177e4SLinus Torvalds device_pm_remove(dev); 3781da177e4SLinus Torvalds PMError: 37923681e47SGreg Kroah-Hartman if (dev->devt_attr) { 38023681e47SGreg Kroah-Hartman device_remove_file(dev, dev->devt_attr); 38123681e47SGreg Kroah-Hartman kfree(dev->devt_attr); 38223681e47SGreg Kroah-Hartman } 38323681e47SGreg Kroah-Hartman attrError: 384312c004dSKay Sievers kobject_uevent(&dev->kobj, KOBJ_REMOVE); 3851da177e4SLinus Torvalds kobject_del(&dev->kobj); 3861da177e4SLinus Torvalds Error: 3871da177e4SLinus Torvalds if (parent) 3881da177e4SLinus Torvalds put_device(parent); 3891da177e4SLinus Torvalds goto Done; 3901da177e4SLinus Torvalds } 3911da177e4SLinus Torvalds 3921da177e4SLinus Torvalds 3931da177e4SLinus Torvalds /** 3941da177e4SLinus Torvalds * device_register - register a device with the system. 3951da177e4SLinus Torvalds * @dev: pointer to the device structure 3961da177e4SLinus Torvalds * 3971da177e4SLinus Torvalds * This happens in two clean steps - initialize the device 3981da177e4SLinus Torvalds * and add it to the system. The two steps can be called 3991da177e4SLinus Torvalds * separately, but this is the easiest and most common. 4001da177e4SLinus Torvalds * I.e. you should only call the two helpers separately if 4011da177e4SLinus Torvalds * have a clearly defined need to use and refcount the device 4021da177e4SLinus Torvalds * before it is added to the hierarchy. 4031da177e4SLinus Torvalds */ 4041da177e4SLinus Torvalds 4051da177e4SLinus Torvalds int device_register(struct device *dev) 4061da177e4SLinus Torvalds { 4071da177e4SLinus Torvalds device_initialize(dev); 4081da177e4SLinus Torvalds return device_add(dev); 4091da177e4SLinus Torvalds } 4101da177e4SLinus Torvalds 4111da177e4SLinus Torvalds 4121da177e4SLinus Torvalds /** 4131da177e4SLinus Torvalds * get_device - increment reference count for device. 4141da177e4SLinus Torvalds * @dev: device. 4151da177e4SLinus Torvalds * 4161da177e4SLinus Torvalds * This simply forwards the call to kobject_get(), though 4171da177e4SLinus Torvalds * we do take care to provide for the case that we get a NULL 4181da177e4SLinus Torvalds * pointer passed in. 4191da177e4SLinus Torvalds */ 4201da177e4SLinus Torvalds 4211da177e4SLinus Torvalds struct device * get_device(struct device * dev) 4221da177e4SLinus Torvalds { 4231da177e4SLinus Torvalds return dev ? to_dev(kobject_get(&dev->kobj)) : NULL; 4241da177e4SLinus Torvalds } 4251da177e4SLinus Torvalds 4261da177e4SLinus Torvalds 4271da177e4SLinus Torvalds /** 4281da177e4SLinus Torvalds * put_device - decrement reference count. 4291da177e4SLinus Torvalds * @dev: device in question. 4301da177e4SLinus Torvalds */ 4311da177e4SLinus Torvalds void put_device(struct device * dev) 4321da177e4SLinus Torvalds { 4331da177e4SLinus Torvalds if (dev) 4341da177e4SLinus Torvalds kobject_put(&dev->kobj); 4351da177e4SLinus Torvalds } 4361da177e4SLinus Torvalds 4371da177e4SLinus Torvalds 4381da177e4SLinus Torvalds /** 4391da177e4SLinus Torvalds * device_del - delete device from system. 4401da177e4SLinus Torvalds * @dev: device. 4411da177e4SLinus Torvalds * 4421da177e4SLinus Torvalds * This is the first part of the device unregistration 4431da177e4SLinus Torvalds * sequence. This removes the device from the lists we control 4441da177e4SLinus Torvalds * from here, has it removed from the other driver model 4451da177e4SLinus Torvalds * subsystems it was added to in device_add(), and removes it 4461da177e4SLinus Torvalds * from the kobject hierarchy. 4471da177e4SLinus Torvalds * 4481da177e4SLinus Torvalds * NOTE: this should be called manually _iff_ device_add() was 4491da177e4SLinus Torvalds * also called manually. 4501da177e4SLinus Torvalds */ 4511da177e4SLinus Torvalds 4521da177e4SLinus Torvalds void device_del(struct device * dev) 4531da177e4SLinus Torvalds { 4541da177e4SLinus Torvalds struct device * parent = dev->parent; 455e9a7d305SGreg Kroah-Hartman char *class_name = NULL; 4561da177e4SLinus Torvalds 4571da177e4SLinus Torvalds if (parent) 458d62c0f9fSPatrick Mochel klist_del(&dev->knode_parent); 45923681e47SGreg Kroah-Hartman if (dev->devt_attr) 46023681e47SGreg Kroah-Hartman device_remove_file(dev, dev->devt_attr); 461b9d9c82bSKay Sievers if (dev->class) { 462b9d9c82bSKay Sievers sysfs_remove_link(&dev->kobj, "subsystem"); 46323681e47SGreg Kroah-Hartman sysfs_remove_link(&dev->class->subsys.kset.kobj, dev->bus_id); 464e9a7d305SGreg Kroah-Hartman class_name = make_class_name(dev->class->name, &dev->kobj); 465e9a7d305SGreg Kroah-Hartman sysfs_remove_link(&dev->kobj, "device"); 466e9a7d305SGreg Kroah-Hartman sysfs_remove_link(&dev->parent->kobj, class_name); 467e9a7d305SGreg Kroah-Hartman kfree(class_name); 4685d9fd169SGreg Kroah-Hartman down(&dev->class->sem); 4695d9fd169SGreg Kroah-Hartman list_del_init(&dev->node); 4705d9fd169SGreg Kroah-Hartman up(&dev->class->sem); 471b9d9c82bSKay Sievers } 472a7fd6706SKay Sievers device_remove_file(dev, &dev->uevent_attr); 4731da177e4SLinus Torvalds 4741da177e4SLinus Torvalds /* Notify the platform of the removal, in case they 4751da177e4SLinus Torvalds * need to do anything... 4761da177e4SLinus Torvalds */ 4771da177e4SLinus Torvalds if (platform_notify_remove) 4781da177e4SLinus Torvalds platform_notify_remove(dev); 4791da177e4SLinus Torvalds bus_remove_device(dev); 4801da177e4SLinus Torvalds device_pm_remove(dev); 481312c004dSKay Sievers kobject_uevent(&dev->kobj, KOBJ_REMOVE); 4821da177e4SLinus Torvalds kobject_del(&dev->kobj); 4831da177e4SLinus Torvalds if (parent) 4841da177e4SLinus Torvalds put_device(parent); 4851da177e4SLinus Torvalds } 4861da177e4SLinus Torvalds 4871da177e4SLinus Torvalds /** 4881da177e4SLinus Torvalds * device_unregister - unregister device from system. 4891da177e4SLinus Torvalds * @dev: device going away. 4901da177e4SLinus Torvalds * 4911da177e4SLinus Torvalds * We do this in two parts, like we do device_register(). First, 4921da177e4SLinus Torvalds * we remove it from all the subsystems with device_del(), then 4931da177e4SLinus Torvalds * we decrement the reference count via put_device(). If that 4941da177e4SLinus Torvalds * is the final reference count, the device will be cleaned up 4951da177e4SLinus Torvalds * via device_release() above. Otherwise, the structure will 4961da177e4SLinus Torvalds * stick around until the final reference to the device is dropped. 4971da177e4SLinus Torvalds */ 4981da177e4SLinus Torvalds void device_unregister(struct device * dev) 4991da177e4SLinus Torvalds { 5001da177e4SLinus Torvalds pr_debug("DEV: Unregistering device. ID = '%s'\n", dev->bus_id); 5011da177e4SLinus Torvalds device_del(dev); 5021da177e4SLinus Torvalds put_device(dev); 5031da177e4SLinus Torvalds } 5041da177e4SLinus Torvalds 5051da177e4SLinus Torvalds 50636239577Smochel@digitalimplant.org static struct device * next_device(struct klist_iter * i) 50736239577Smochel@digitalimplant.org { 50836239577Smochel@digitalimplant.org struct klist_node * n = klist_next(i); 50936239577Smochel@digitalimplant.org return n ? container_of(n, struct device, knode_parent) : NULL; 51036239577Smochel@digitalimplant.org } 51136239577Smochel@digitalimplant.org 5121da177e4SLinus Torvalds /** 5131da177e4SLinus Torvalds * device_for_each_child - device child iterator. 514c41455fbSRandy Dunlap * @parent: parent struct device. 5151da177e4SLinus Torvalds * @data: data for the callback. 5161da177e4SLinus Torvalds * @fn: function to be called for each device. 5171da177e4SLinus Torvalds * 518c41455fbSRandy Dunlap * Iterate over @parent's child devices, and call @fn for each, 5191da177e4SLinus Torvalds * passing it @data. 5201da177e4SLinus Torvalds * 5211da177e4SLinus Torvalds * We check the return of @fn each time. If it returns anything 5221da177e4SLinus Torvalds * other than 0, we break out and return that value. 5231da177e4SLinus Torvalds */ 52436239577Smochel@digitalimplant.org int device_for_each_child(struct device * parent, void * data, 5251da177e4SLinus Torvalds int (*fn)(struct device *, void *)) 5261da177e4SLinus Torvalds { 52736239577Smochel@digitalimplant.org struct klist_iter i; 5281da177e4SLinus Torvalds struct device * child; 5291da177e4SLinus Torvalds int error = 0; 5301da177e4SLinus Torvalds 53136239577Smochel@digitalimplant.org klist_iter_init(&parent->klist_children, &i); 53236239577Smochel@digitalimplant.org while ((child = next_device(&i)) && !error) 53336239577Smochel@digitalimplant.org error = fn(child, data); 53436239577Smochel@digitalimplant.org klist_iter_exit(&i); 5351da177e4SLinus Torvalds return error; 5361da177e4SLinus Torvalds } 5371da177e4SLinus Torvalds 5381da177e4SLinus Torvalds int __init devices_init(void) 5391da177e4SLinus Torvalds { 5401da177e4SLinus Torvalds return subsystem_register(&devices_subsys); 5411da177e4SLinus Torvalds } 5421da177e4SLinus Torvalds 5431da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(device_for_each_child); 5441da177e4SLinus Torvalds 5451da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(device_initialize); 5461da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(device_add); 5471da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(device_register); 5481da177e4SLinus Torvalds 5491da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(device_del); 5501da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(device_unregister); 5511da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(get_device); 5521da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(put_device); 5531da177e4SLinus Torvalds 5541da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(device_create_file); 5551da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(device_remove_file); 55623681e47SGreg Kroah-Hartman 55723681e47SGreg Kroah-Hartman 55823681e47SGreg Kroah-Hartman static void device_create_release(struct device *dev) 55923681e47SGreg Kroah-Hartman { 56023681e47SGreg Kroah-Hartman pr_debug("%s called for %s\n", __FUNCTION__, dev->bus_id); 56123681e47SGreg Kroah-Hartman kfree(dev); 56223681e47SGreg Kroah-Hartman } 56323681e47SGreg Kroah-Hartman 56423681e47SGreg Kroah-Hartman /** 56523681e47SGreg Kroah-Hartman * device_create - creates a device and registers it with sysfs 56642734dafSHenrik Kretzschmar * @class: pointer to the struct class that this device should be registered to 56742734dafSHenrik Kretzschmar * @parent: pointer to the parent struct device of this new device, if any 56842734dafSHenrik Kretzschmar * @devt: the dev_t for the char device to be added 56942734dafSHenrik Kretzschmar * @fmt: string for the device's name 57023681e47SGreg Kroah-Hartman * 57142734dafSHenrik Kretzschmar * This function can be used by char device classes. A struct device 57242734dafSHenrik Kretzschmar * will be created in sysfs, registered to the specified class. 57342734dafSHenrik Kretzschmar * 57423681e47SGreg Kroah-Hartman * A "dev" file will be created, showing the dev_t for the device, if 57523681e47SGreg Kroah-Hartman * the dev_t is not 0,0. 57642734dafSHenrik Kretzschmar * If a pointer to a parent struct device is passed in, the newly created 57742734dafSHenrik Kretzschmar * struct device will be a child of that device in sysfs. 57842734dafSHenrik Kretzschmar * The pointer to the struct device will be returned from the call. 57942734dafSHenrik Kretzschmar * Any further sysfs files that might be required can be created using this 58023681e47SGreg Kroah-Hartman * pointer. 58123681e47SGreg Kroah-Hartman * 58223681e47SGreg Kroah-Hartman * Note: the struct class passed to this function must have previously 58323681e47SGreg Kroah-Hartman * been created with a call to class_create(). 58423681e47SGreg Kroah-Hartman */ 58523681e47SGreg Kroah-Hartman struct device *device_create(struct class *class, struct device *parent, 5865cbe5f8aSGreg Kroah-Hartman dev_t devt, const char *fmt, ...) 58723681e47SGreg Kroah-Hartman { 58823681e47SGreg Kroah-Hartman va_list args; 58923681e47SGreg Kroah-Hartman struct device *dev = NULL; 59023681e47SGreg Kroah-Hartman int retval = -ENODEV; 59123681e47SGreg Kroah-Hartman 59223681e47SGreg Kroah-Hartman if (class == NULL || IS_ERR(class)) 59323681e47SGreg Kroah-Hartman goto error; 59423681e47SGreg Kroah-Hartman if (parent == NULL) { 59523681e47SGreg Kroah-Hartman printk(KERN_WARNING "%s does not work yet for NULL parents\n", __FUNCTION__); 59623681e47SGreg Kroah-Hartman goto error; 59723681e47SGreg Kroah-Hartman } 59823681e47SGreg Kroah-Hartman 59923681e47SGreg Kroah-Hartman dev = kzalloc(sizeof(*dev), GFP_KERNEL); 60023681e47SGreg Kroah-Hartman if (!dev) { 60123681e47SGreg Kroah-Hartman retval = -ENOMEM; 60223681e47SGreg Kroah-Hartman goto error; 60323681e47SGreg Kroah-Hartman } 60423681e47SGreg Kroah-Hartman 60523681e47SGreg Kroah-Hartman dev->devt = devt; 60623681e47SGreg Kroah-Hartman dev->class = class; 60723681e47SGreg Kroah-Hartman dev->parent = parent; 60823681e47SGreg Kroah-Hartman dev->release = device_create_release; 60923681e47SGreg Kroah-Hartman 61023681e47SGreg Kroah-Hartman va_start(args, fmt); 61123681e47SGreg Kroah-Hartman vsnprintf(dev->bus_id, BUS_ID_SIZE, fmt, args); 61223681e47SGreg Kroah-Hartman va_end(args); 61323681e47SGreg Kroah-Hartman retval = device_register(dev); 61423681e47SGreg Kroah-Hartman if (retval) 61523681e47SGreg Kroah-Hartman goto error; 61623681e47SGreg Kroah-Hartman 61723681e47SGreg Kroah-Hartman return dev; 61823681e47SGreg Kroah-Hartman 61923681e47SGreg Kroah-Hartman error: 62023681e47SGreg Kroah-Hartman kfree(dev); 62123681e47SGreg Kroah-Hartman return ERR_PTR(retval); 62223681e47SGreg Kroah-Hartman } 62323681e47SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(device_create); 62423681e47SGreg Kroah-Hartman 62523681e47SGreg Kroah-Hartman /** 62623681e47SGreg Kroah-Hartman * device_destroy - removes a device that was created with device_create() 62742734dafSHenrik Kretzschmar * @class: pointer to the struct class that this device was registered with 62842734dafSHenrik Kretzschmar * @devt: the dev_t of the device that was previously registered 62923681e47SGreg Kroah-Hartman * 63042734dafSHenrik Kretzschmar * This call unregisters and cleans up a device that was created with a 63142734dafSHenrik Kretzschmar * call to device_create(). 63223681e47SGreg Kroah-Hartman */ 63323681e47SGreg Kroah-Hartman void device_destroy(struct class *class, dev_t devt) 63423681e47SGreg Kroah-Hartman { 63523681e47SGreg Kroah-Hartman struct device *dev = NULL; 63623681e47SGreg Kroah-Hartman struct device *dev_tmp; 63723681e47SGreg Kroah-Hartman 63823681e47SGreg Kroah-Hartman down(&class->sem); 63923681e47SGreg Kroah-Hartman list_for_each_entry(dev_tmp, &class->devices, node) { 64023681e47SGreg Kroah-Hartman if (dev_tmp->devt == devt) { 64123681e47SGreg Kroah-Hartman dev = dev_tmp; 64223681e47SGreg Kroah-Hartman break; 64323681e47SGreg Kroah-Hartman } 64423681e47SGreg Kroah-Hartman } 64523681e47SGreg Kroah-Hartman up(&class->sem); 64623681e47SGreg Kroah-Hartman 6475d9fd169SGreg Kroah-Hartman if (dev) 64823681e47SGreg Kroah-Hartman device_unregister(dev); 64923681e47SGreg Kroah-Hartman } 65023681e47SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(device_destroy); 651