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 664bb5d2cSGreg Kroah-Hartman * Copyright (c) 2006 Greg Kroah-Hartman <gregkh@suse.de> 764bb5d2cSGreg Kroah-Hartman * Copyright (c) 2006 Novell, Inc. 81da177e4SLinus Torvalds * 91da177e4SLinus Torvalds * This file is released under the GPLv2 101da177e4SLinus Torvalds * 111da177e4SLinus Torvalds */ 121da177e4SLinus Torvalds 131da177e4SLinus Torvalds #include <linux/device.h> 141da177e4SLinus Torvalds #include <linux/err.h> 151da177e4SLinus Torvalds #include <linux/init.h> 161da177e4SLinus Torvalds #include <linux/module.h> 171da177e4SLinus Torvalds #include <linux/slab.h> 181da177e4SLinus Torvalds #include <linux/string.h> 1923681e47SGreg Kroah-Hartman #include <linux/kdev_t.h> 201da177e4SLinus Torvalds 211da177e4SLinus Torvalds #include <asm/semaphore.h> 221da177e4SLinus Torvalds 231da177e4SLinus Torvalds #include "base.h" 241da177e4SLinus Torvalds #include "power/power.h" 251da177e4SLinus Torvalds 261da177e4SLinus Torvalds int (*platform_notify)(struct device * dev) = NULL; 271da177e4SLinus Torvalds int (*platform_notify_remove)(struct device * dev) = NULL; 281da177e4SLinus Torvalds 291da177e4SLinus Torvalds /* 301da177e4SLinus Torvalds * sysfs bindings for devices. 311da177e4SLinus Torvalds */ 321da177e4SLinus Torvalds 333e95637aSAlan Stern /** 343e95637aSAlan Stern * dev_driver_string - Return a device's driver name, if at all possible 353e95637aSAlan Stern * @dev: struct device to get the name of 363e95637aSAlan Stern * 373e95637aSAlan Stern * Will return the device's driver's name if it is bound to a device. If 383e95637aSAlan Stern * the device is not bound to a device, it will return the name of the bus 393e95637aSAlan Stern * it is attached to. If it is not attached to a bus either, an empty 403e95637aSAlan Stern * string will be returned. 413e95637aSAlan Stern */ 423e95637aSAlan Stern const char *dev_driver_string(struct device *dev) 433e95637aSAlan Stern { 443e95637aSAlan Stern return dev->driver ? dev->driver->name : 453e95637aSAlan Stern (dev->bus ? dev->bus->name : ""); 463e95637aSAlan Stern } 473e95637aSAlan Stern EXPORT_SYMBOL_GPL(dev_driver_string); 483e95637aSAlan Stern 491da177e4SLinus Torvalds #define to_dev(obj) container_of(obj, struct device, kobj) 501da177e4SLinus Torvalds #define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr) 511da177e4SLinus Torvalds 521da177e4SLinus Torvalds static ssize_t 531da177e4SLinus Torvalds dev_attr_show(struct kobject * kobj, struct attribute * attr, char * buf) 541da177e4SLinus Torvalds { 551da177e4SLinus Torvalds struct device_attribute * dev_attr = to_dev_attr(attr); 561da177e4SLinus Torvalds struct device * dev = to_dev(kobj); 574a0c20bfSDmitry Torokhov ssize_t ret = -EIO; 581da177e4SLinus Torvalds 591da177e4SLinus Torvalds if (dev_attr->show) 6054b6f35cSYani Ioannou ret = dev_attr->show(dev, dev_attr, buf); 611da177e4SLinus Torvalds return ret; 621da177e4SLinus Torvalds } 631da177e4SLinus Torvalds 641da177e4SLinus Torvalds static ssize_t 651da177e4SLinus Torvalds dev_attr_store(struct kobject * kobj, struct attribute * attr, 661da177e4SLinus Torvalds const char * buf, size_t count) 671da177e4SLinus Torvalds { 681da177e4SLinus Torvalds struct device_attribute * dev_attr = to_dev_attr(attr); 691da177e4SLinus Torvalds struct device * dev = to_dev(kobj); 704a0c20bfSDmitry Torokhov ssize_t ret = -EIO; 711da177e4SLinus Torvalds 721da177e4SLinus Torvalds if (dev_attr->store) 7354b6f35cSYani Ioannou ret = dev_attr->store(dev, dev_attr, buf, count); 741da177e4SLinus Torvalds return ret; 751da177e4SLinus Torvalds } 761da177e4SLinus Torvalds 771da177e4SLinus Torvalds static struct sysfs_ops dev_sysfs_ops = { 781da177e4SLinus Torvalds .show = dev_attr_show, 791da177e4SLinus Torvalds .store = dev_attr_store, 801da177e4SLinus Torvalds }; 811da177e4SLinus Torvalds 821da177e4SLinus Torvalds 831da177e4SLinus Torvalds /** 841da177e4SLinus Torvalds * device_release - free device structure. 851da177e4SLinus Torvalds * @kobj: device's kobject. 861da177e4SLinus Torvalds * 871da177e4SLinus Torvalds * This is called once the reference count for the object 881da177e4SLinus Torvalds * reaches 0. We forward the call to the device's release 891da177e4SLinus Torvalds * method, which should handle actually freeing the structure. 901da177e4SLinus Torvalds */ 911da177e4SLinus Torvalds static void device_release(struct kobject * kobj) 921da177e4SLinus Torvalds { 931da177e4SLinus Torvalds struct device * dev = to_dev(kobj); 941da177e4SLinus Torvalds 951da177e4SLinus Torvalds if (dev->release) 961da177e4SLinus Torvalds dev->release(dev); 972620efefSGreg Kroah-Hartman else if (dev->class && dev->class->dev_release) 982620efefSGreg Kroah-Hartman dev->class->dev_release(dev); 991da177e4SLinus Torvalds else { 1001da177e4SLinus Torvalds printk(KERN_ERR "Device '%s' does not have a release() function, " 1011da177e4SLinus Torvalds "it is broken and must be fixed.\n", 1021da177e4SLinus Torvalds dev->bus_id); 1031da177e4SLinus Torvalds WARN_ON(1); 1041da177e4SLinus Torvalds } 1051da177e4SLinus Torvalds } 1061da177e4SLinus Torvalds 1071da177e4SLinus Torvalds static struct kobj_type ktype_device = { 1081da177e4SLinus Torvalds .release = device_release, 1091da177e4SLinus Torvalds .sysfs_ops = &dev_sysfs_ops, 1101da177e4SLinus Torvalds }; 1111da177e4SLinus Torvalds 1121da177e4SLinus Torvalds 113312c004dSKay Sievers static int dev_uevent_filter(struct kset *kset, struct kobject *kobj) 1141da177e4SLinus Torvalds { 1151da177e4SLinus Torvalds struct kobj_type *ktype = get_ktype(kobj); 1161da177e4SLinus Torvalds 1171da177e4SLinus Torvalds if (ktype == &ktype_device) { 1181da177e4SLinus Torvalds struct device *dev = to_dev(kobj); 1191da177e4SLinus Torvalds if (dev->bus) 1201da177e4SLinus Torvalds return 1; 12123681e47SGreg Kroah-Hartman if (dev->class) 12223681e47SGreg Kroah-Hartman return 1; 1231da177e4SLinus Torvalds } 1241da177e4SLinus Torvalds return 0; 1251da177e4SLinus Torvalds } 1261da177e4SLinus Torvalds 127312c004dSKay Sievers static const char *dev_uevent_name(struct kset *kset, struct kobject *kobj) 1281da177e4SLinus Torvalds { 1291da177e4SLinus Torvalds struct device *dev = to_dev(kobj); 1301da177e4SLinus Torvalds 13123681e47SGreg Kroah-Hartman if (dev->bus) 1321da177e4SLinus Torvalds return dev->bus->name; 13323681e47SGreg Kroah-Hartman if (dev->class) 13423681e47SGreg Kroah-Hartman return dev->class->name; 13523681e47SGreg Kroah-Hartman return NULL; 1361da177e4SLinus Torvalds } 1371da177e4SLinus Torvalds 138312c004dSKay Sievers static int dev_uevent(struct kset *kset, struct kobject *kobj, char **envp, 1391da177e4SLinus Torvalds int num_envp, char *buffer, int buffer_size) 1401da177e4SLinus Torvalds { 1411da177e4SLinus Torvalds struct device *dev = to_dev(kobj); 1421da177e4SLinus Torvalds int i = 0; 1431da177e4SLinus Torvalds int length = 0; 1441da177e4SLinus Torvalds int retval = 0; 1451da177e4SLinus Torvalds 14623681e47SGreg Kroah-Hartman /* add the major/minor if present */ 14723681e47SGreg Kroah-Hartman if (MAJOR(dev->devt)) { 14823681e47SGreg Kroah-Hartman add_uevent_var(envp, num_envp, &i, 14923681e47SGreg Kroah-Hartman buffer, buffer_size, &length, 15023681e47SGreg Kroah-Hartman "MAJOR=%u", MAJOR(dev->devt)); 15123681e47SGreg Kroah-Hartman add_uevent_var(envp, num_envp, &i, 15223681e47SGreg Kroah-Hartman buffer, buffer_size, &length, 15323681e47SGreg Kroah-Hartman "MINOR=%u", MINOR(dev->devt)); 15423681e47SGreg Kroah-Hartman } 15523681e47SGreg Kroah-Hartman 156d81d9d6bSKay Sievers /* add bus name (same as SUBSYSTEM, deprecated) */ 1571da177e4SLinus Torvalds if (dev->bus) 158312c004dSKay Sievers add_uevent_var(envp, num_envp, &i, 1591da177e4SLinus Torvalds buffer, buffer_size, &length, 1601da177e4SLinus Torvalds "PHYSDEVBUS=%s", dev->bus->name); 1611da177e4SLinus Torvalds 162d81d9d6bSKay Sievers /* add driver name (PHYSDEV* values are deprecated)*/ 163d81d9d6bSKay Sievers if (dev->driver) { 164d81d9d6bSKay Sievers add_uevent_var(envp, num_envp, &i, 165d81d9d6bSKay Sievers buffer, buffer_size, &length, 166d81d9d6bSKay Sievers "DRIVER=%s", dev->driver->name); 167312c004dSKay Sievers add_uevent_var(envp, num_envp, &i, 1681da177e4SLinus Torvalds buffer, buffer_size, &length, 1691da177e4SLinus Torvalds "PHYSDEVDRIVER=%s", dev->driver->name); 170d81d9d6bSKay Sievers } 1711da177e4SLinus Torvalds 1721da177e4SLinus Torvalds /* terminate, set to next free slot, shrink available space */ 1731da177e4SLinus Torvalds envp[i] = NULL; 1741da177e4SLinus Torvalds envp = &envp[i]; 1751da177e4SLinus Torvalds num_envp -= i; 1761da177e4SLinus Torvalds buffer = &buffer[length]; 1771da177e4SLinus Torvalds buffer_size -= length; 1781da177e4SLinus Torvalds 179312c004dSKay Sievers if (dev->bus && dev->bus->uevent) { 1801da177e4SLinus Torvalds /* have the bus specific function add its stuff */ 181312c004dSKay Sievers retval = dev->bus->uevent(dev, envp, num_envp, buffer, buffer_size); 1821da177e4SLinus Torvalds if (retval) { 183312c004dSKay Sievers pr_debug ("%s - uevent() returned %d\n", 1841da177e4SLinus Torvalds __FUNCTION__, retval); 1851da177e4SLinus Torvalds } 1861da177e4SLinus Torvalds } 1871da177e4SLinus Torvalds 1882620efefSGreg Kroah-Hartman if (dev->class && dev->class->dev_uevent) { 1892620efefSGreg Kroah-Hartman /* have the class specific function add its stuff */ 1902620efefSGreg Kroah-Hartman retval = dev->class->dev_uevent(dev, envp, num_envp, buffer, buffer_size); 1912620efefSGreg Kroah-Hartman if (retval) { 1922620efefSGreg Kroah-Hartman pr_debug("%s - dev_uevent() returned %d\n", 1932620efefSGreg Kroah-Hartman __FUNCTION__, retval); 1942620efefSGreg Kroah-Hartman } 1952620efefSGreg Kroah-Hartman } 1962620efefSGreg Kroah-Hartman 1971da177e4SLinus Torvalds return retval; 1981da177e4SLinus Torvalds } 1991da177e4SLinus Torvalds 200312c004dSKay Sievers static struct kset_uevent_ops device_uevent_ops = { 201312c004dSKay Sievers .filter = dev_uevent_filter, 202312c004dSKay Sievers .name = dev_uevent_name, 203312c004dSKay Sievers .uevent = dev_uevent, 2041da177e4SLinus Torvalds }; 2051da177e4SLinus Torvalds 206a7fd6706SKay Sievers static ssize_t store_uevent(struct device *dev, struct device_attribute *attr, 207a7fd6706SKay Sievers const char *buf, size_t count) 208a7fd6706SKay Sievers { 209312c004dSKay Sievers kobject_uevent(&dev->kobj, KOBJ_ADD); 210a7fd6706SKay Sievers return count; 211a7fd6706SKay Sievers } 212a7fd6706SKay Sievers 213de0ff00dSGreg Kroah-Hartman static int device_add_groups(struct device *dev) 214de0ff00dSGreg Kroah-Hartman { 215de0ff00dSGreg Kroah-Hartman int i; 216de0ff00dSGreg Kroah-Hartman int error = 0; 217de0ff00dSGreg Kroah-Hartman 218de0ff00dSGreg Kroah-Hartman if (dev->groups) { 219de0ff00dSGreg Kroah-Hartman for (i = 0; dev->groups[i]; i++) { 220de0ff00dSGreg Kroah-Hartman error = sysfs_create_group(&dev->kobj, dev->groups[i]); 221de0ff00dSGreg Kroah-Hartman if (error) { 222de0ff00dSGreg Kroah-Hartman while (--i >= 0) 223de0ff00dSGreg Kroah-Hartman sysfs_remove_group(&dev->kobj, dev->groups[i]); 224de0ff00dSGreg Kroah-Hartman goto out; 225de0ff00dSGreg Kroah-Hartman } 226de0ff00dSGreg Kroah-Hartman } 227de0ff00dSGreg Kroah-Hartman } 228de0ff00dSGreg Kroah-Hartman out: 229de0ff00dSGreg Kroah-Hartman return error; 230de0ff00dSGreg Kroah-Hartman } 231de0ff00dSGreg Kroah-Hartman 232de0ff00dSGreg Kroah-Hartman static void device_remove_groups(struct device *dev) 233de0ff00dSGreg Kroah-Hartman { 234de0ff00dSGreg Kroah-Hartman int i; 235de0ff00dSGreg Kroah-Hartman if (dev->groups) { 236de0ff00dSGreg Kroah-Hartman for (i = 0; dev->groups[i]; i++) { 237de0ff00dSGreg Kroah-Hartman sysfs_remove_group(&dev->kobj, dev->groups[i]); 238de0ff00dSGreg Kroah-Hartman } 239de0ff00dSGreg Kroah-Hartman } 240de0ff00dSGreg Kroah-Hartman } 241de0ff00dSGreg Kroah-Hartman 2422620efefSGreg Kroah-Hartman static int device_add_attrs(struct device *dev) 2432620efefSGreg Kroah-Hartman { 2442620efefSGreg Kroah-Hartman struct class *class = dev->class; 2452620efefSGreg Kroah-Hartman int error = 0; 2462620efefSGreg Kroah-Hartman int i; 2472620efefSGreg Kroah-Hartman 2482620efefSGreg Kroah-Hartman if (!class) 2492620efefSGreg Kroah-Hartman return 0; 2502620efefSGreg Kroah-Hartman 2512620efefSGreg Kroah-Hartman if (class->dev_attrs) { 2522620efefSGreg Kroah-Hartman for (i = 0; attr_name(class->dev_attrs[i]); i++) { 2532620efefSGreg Kroah-Hartman error = device_create_file(dev, &class->dev_attrs[i]); 2542620efefSGreg Kroah-Hartman if (error) 2552620efefSGreg Kroah-Hartman break; 2562620efefSGreg Kroah-Hartman } 2572620efefSGreg Kroah-Hartman } 2582620efefSGreg Kroah-Hartman if (error) 2592620efefSGreg Kroah-Hartman while (--i >= 0) 2602620efefSGreg Kroah-Hartman device_remove_file(dev, &class->dev_attrs[i]); 2612620efefSGreg Kroah-Hartman return error; 2622620efefSGreg Kroah-Hartman } 2632620efefSGreg Kroah-Hartman 2642620efefSGreg Kroah-Hartman static void device_remove_attrs(struct device *dev) 2652620efefSGreg Kroah-Hartman { 2662620efefSGreg Kroah-Hartman struct class *class = dev->class; 2672620efefSGreg Kroah-Hartman int i; 2682620efefSGreg Kroah-Hartman 2692620efefSGreg Kroah-Hartman if (!class) 2702620efefSGreg Kroah-Hartman return; 2712620efefSGreg Kroah-Hartman 2722620efefSGreg Kroah-Hartman if (class->dev_attrs) { 2732620efefSGreg Kroah-Hartman for (i = 0; attr_name(class->dev_attrs[i]); i++) 2742620efefSGreg Kroah-Hartman device_remove_file(dev, &class->dev_attrs[i]); 2752620efefSGreg Kroah-Hartman } 2762620efefSGreg Kroah-Hartman } 2772620efefSGreg Kroah-Hartman 2782620efefSGreg Kroah-Hartman 27923681e47SGreg Kroah-Hartman static ssize_t show_dev(struct device *dev, struct device_attribute *attr, 28023681e47SGreg Kroah-Hartman char *buf) 28123681e47SGreg Kroah-Hartman { 28223681e47SGreg Kroah-Hartman return print_dev_t(buf, dev->devt); 28323681e47SGreg Kroah-Hartman } 28423681e47SGreg Kroah-Hartman 2850863afb3SMartin Waitz /* 2860863afb3SMartin Waitz * devices_subsys - structure to be registered with kobject core. 2871da177e4SLinus Torvalds */ 2881da177e4SLinus Torvalds 289312c004dSKay Sievers decl_subsys(devices, &ktype_device, &device_uevent_ops); 2901da177e4SLinus Torvalds 2911da177e4SLinus Torvalds 2921da177e4SLinus Torvalds /** 2931da177e4SLinus Torvalds * device_create_file - create sysfs attribute file for device. 2941da177e4SLinus Torvalds * @dev: device. 2951da177e4SLinus Torvalds * @attr: device attribute descriptor. 2961da177e4SLinus Torvalds */ 2971da177e4SLinus Torvalds 2981da177e4SLinus Torvalds int device_create_file(struct device * dev, struct device_attribute * attr) 2991da177e4SLinus Torvalds { 3001da177e4SLinus Torvalds int error = 0; 3011da177e4SLinus Torvalds if (get_device(dev)) { 3021da177e4SLinus Torvalds error = sysfs_create_file(&dev->kobj, &attr->attr); 3031da177e4SLinus Torvalds put_device(dev); 3041da177e4SLinus Torvalds } 3051da177e4SLinus Torvalds return error; 3061da177e4SLinus Torvalds } 3071da177e4SLinus Torvalds 3081da177e4SLinus Torvalds /** 3091da177e4SLinus Torvalds * device_remove_file - remove sysfs attribute file. 3101da177e4SLinus Torvalds * @dev: device. 3111da177e4SLinus Torvalds * @attr: device attribute descriptor. 3121da177e4SLinus Torvalds */ 3131da177e4SLinus Torvalds 3141da177e4SLinus Torvalds void device_remove_file(struct device * dev, struct device_attribute * attr) 3151da177e4SLinus Torvalds { 3161da177e4SLinus Torvalds if (get_device(dev)) { 3171da177e4SLinus Torvalds sysfs_remove_file(&dev->kobj, &attr->attr); 3181da177e4SLinus Torvalds put_device(dev); 3191da177e4SLinus Torvalds } 3201da177e4SLinus Torvalds } 3211da177e4SLinus Torvalds 32234bb61f9SJames Bottomley static void klist_children_get(struct klist_node *n) 32334bb61f9SJames Bottomley { 32434bb61f9SJames Bottomley struct device *dev = container_of(n, struct device, knode_parent); 32534bb61f9SJames Bottomley 32634bb61f9SJames Bottomley get_device(dev); 32734bb61f9SJames Bottomley } 32834bb61f9SJames Bottomley 32934bb61f9SJames Bottomley static void klist_children_put(struct klist_node *n) 33034bb61f9SJames Bottomley { 33134bb61f9SJames Bottomley struct device *dev = container_of(n, struct device, knode_parent); 33234bb61f9SJames Bottomley 33334bb61f9SJames Bottomley put_device(dev); 33434bb61f9SJames Bottomley } 33534bb61f9SJames Bottomley 3361da177e4SLinus Torvalds 3371da177e4SLinus Torvalds /** 3381da177e4SLinus Torvalds * device_initialize - init device structure. 3391da177e4SLinus Torvalds * @dev: device. 3401da177e4SLinus Torvalds * 3411da177e4SLinus Torvalds * This prepares the device for use by other layers, 3421da177e4SLinus Torvalds * including adding it to the device hierarchy. 3431da177e4SLinus Torvalds * It is the first half of device_register(), if called by 3441da177e4SLinus Torvalds * that, though it can also be called separately, so one 3451da177e4SLinus Torvalds * may use @dev's fields (e.g. the refcount). 3461da177e4SLinus Torvalds */ 3471da177e4SLinus Torvalds 3481da177e4SLinus Torvalds void device_initialize(struct device *dev) 3491da177e4SLinus Torvalds { 3501da177e4SLinus Torvalds kobj_set_kset_s(dev, devices_subsys); 3511da177e4SLinus Torvalds kobject_init(&dev->kobj); 35234bb61f9SJames Bottomley klist_init(&dev->klist_children, klist_children_get, 35334bb61f9SJames Bottomley klist_children_put); 3541da177e4SLinus Torvalds INIT_LIST_HEAD(&dev->dma_pools); 35523681e47SGreg Kroah-Hartman INIT_LIST_HEAD(&dev->node); 356af70316aSmochel@digitalimplant.org init_MUTEX(&dev->sem); 3570ac85241SDavid Brownell device_init_wakeup(dev, 0); 3581da177e4SLinus Torvalds } 3591da177e4SLinus Torvalds 3601da177e4SLinus Torvalds /** 3611da177e4SLinus Torvalds * device_add - add device to device hierarchy. 3621da177e4SLinus Torvalds * @dev: device. 3631da177e4SLinus Torvalds * 3641da177e4SLinus Torvalds * This is part 2 of device_register(), though may be called 3651da177e4SLinus Torvalds * separately _iff_ device_initialize() has been called separately. 3661da177e4SLinus Torvalds * 3671da177e4SLinus Torvalds * This adds it to the kobject hierarchy via kobject_add(), adds it 3681da177e4SLinus Torvalds * to the global and sibling lists for the device, then 3691da177e4SLinus Torvalds * adds it to the other relevant subsystems of the driver model. 3701da177e4SLinus Torvalds */ 3711da177e4SLinus Torvalds int device_add(struct device *dev) 3721da177e4SLinus Torvalds { 3731da177e4SLinus Torvalds struct device *parent = NULL; 374e9a7d305SGreg Kroah-Hartman char *class_name = NULL; 3751da177e4SLinus Torvalds int error = -EINVAL; 3761da177e4SLinus Torvalds 3771da177e4SLinus Torvalds dev = get_device(dev); 3781da177e4SLinus Torvalds if (!dev || !strlen(dev->bus_id)) 3791da177e4SLinus Torvalds goto Error; 3801da177e4SLinus Torvalds 381c205ef48SGreg Kroah-Hartman /* if this is a class device, and has no parent, create one */ 382c205ef48SGreg Kroah-Hartman if ((dev->class) && (dev->parent == NULL)) { 383c205ef48SGreg Kroah-Hartman error = virtual_device_parent(dev); 384c205ef48SGreg Kroah-Hartman if (error) 385c205ef48SGreg Kroah-Hartman goto Error; 386c205ef48SGreg Kroah-Hartman } 387c205ef48SGreg Kroah-Hartman 3881da177e4SLinus Torvalds parent = get_device(dev->parent); 3891da177e4SLinus Torvalds 3901da177e4SLinus Torvalds pr_debug("DEV: registering device: ID = '%s'\n", dev->bus_id); 3911da177e4SLinus Torvalds 3921da177e4SLinus Torvalds /* first, register with generic layer. */ 3931da177e4SLinus Torvalds kobject_set_name(&dev->kobj, "%s", dev->bus_id); 3941da177e4SLinus Torvalds if (parent) 3951da177e4SLinus Torvalds dev->kobj.parent = &parent->kobj; 3961da177e4SLinus Torvalds 3971da177e4SLinus Torvalds if ((error = kobject_add(&dev->kobj))) 3981da177e4SLinus Torvalds goto Error; 399a7fd6706SKay Sievers 400a7fd6706SKay Sievers dev->uevent_attr.attr.name = "uevent"; 401a7fd6706SKay Sievers dev->uevent_attr.attr.mode = S_IWUSR; 402a7fd6706SKay Sievers if (dev->driver) 403a7fd6706SKay Sievers dev->uevent_attr.attr.owner = dev->driver->owner; 404a7fd6706SKay Sievers dev->uevent_attr.store = store_uevent; 405a7fd6706SKay Sievers device_create_file(dev, &dev->uevent_attr); 406a7fd6706SKay Sievers 40723681e47SGreg Kroah-Hartman if (MAJOR(dev->devt)) { 40823681e47SGreg Kroah-Hartman struct device_attribute *attr; 40923681e47SGreg Kroah-Hartman attr = kzalloc(sizeof(*attr), GFP_KERNEL); 41023681e47SGreg Kroah-Hartman if (!attr) { 41123681e47SGreg Kroah-Hartman error = -ENOMEM; 41223681e47SGreg Kroah-Hartman goto PMError; 41323681e47SGreg Kroah-Hartman } 41423681e47SGreg Kroah-Hartman attr->attr.name = "dev"; 41523681e47SGreg Kroah-Hartman attr->attr.mode = S_IRUGO; 41623681e47SGreg Kroah-Hartman if (dev->driver) 41723681e47SGreg Kroah-Hartman attr->attr.owner = dev->driver->owner; 41823681e47SGreg Kroah-Hartman attr->show = show_dev; 41923681e47SGreg Kroah-Hartman error = device_create_file(dev, attr); 42023681e47SGreg Kroah-Hartman if (error) { 42123681e47SGreg Kroah-Hartman kfree(attr); 42223681e47SGreg Kroah-Hartman goto attrError; 42323681e47SGreg Kroah-Hartman } 42423681e47SGreg Kroah-Hartman 42523681e47SGreg Kroah-Hartman dev->devt_attr = attr; 42623681e47SGreg Kroah-Hartman } 42723681e47SGreg Kroah-Hartman 428b9d9c82bSKay Sievers if (dev->class) { 429b9d9c82bSKay Sievers sysfs_create_link(&dev->kobj, &dev->class->subsys.kset.kobj, 430b9d9c82bSKay Sievers "subsystem"); 43123681e47SGreg Kroah-Hartman sysfs_create_link(&dev->class->subsys.kset.kobj, &dev->kobj, 43223681e47SGreg Kroah-Hartman dev->bus_id); 43364bb5d2cSGreg Kroah-Hartman if (parent) { 434e9a7d305SGreg Kroah-Hartman sysfs_create_link(&dev->kobj, &dev->parent->kobj, "device"); 435e9a7d305SGreg Kroah-Hartman class_name = make_class_name(dev->class->name, &dev->kobj); 436e9a7d305SGreg Kroah-Hartman sysfs_create_link(&dev->parent->kobj, &dev->kobj, class_name); 437b9d9c82bSKay Sievers } 43864bb5d2cSGreg Kroah-Hartman } 43923681e47SGreg Kroah-Hartman 4402620efefSGreg Kroah-Hartman if ((error = device_add_attrs(dev))) 4412620efefSGreg Kroah-Hartman goto AttrsError; 442de0ff00dSGreg Kroah-Hartman if ((error = device_add_groups(dev))) 443de0ff00dSGreg Kroah-Hartman goto GroupError; 4441da177e4SLinus Torvalds if ((error = device_pm_add(dev))) 4451da177e4SLinus Torvalds goto PMError; 4461da177e4SLinus Torvalds if ((error = bus_add_device(dev))) 4471da177e4SLinus Torvalds goto BusError; 44853877d06SKay Sievers kobject_uevent(&dev->kobj, KOBJ_ADD); 44953877d06SKay Sievers bus_attach_device(dev); 4501da177e4SLinus Torvalds if (parent) 451d856f1e3SJames Bottomley klist_add_tail(&dev->knode_parent, &parent->klist_children); 4521da177e4SLinus Torvalds 4535d9fd169SGreg Kroah-Hartman if (dev->class) { 4545d9fd169SGreg Kroah-Hartman /* tie the class to the device */ 4555d9fd169SGreg Kroah-Hartman down(&dev->class->sem); 4565d9fd169SGreg Kroah-Hartman list_add_tail(&dev->node, &dev->class->devices); 4575d9fd169SGreg Kroah-Hartman up(&dev->class->sem); 4585d9fd169SGreg Kroah-Hartman } 4595d9fd169SGreg Kroah-Hartman 4601da177e4SLinus Torvalds /* notify platform of device entry */ 4611da177e4SLinus Torvalds if (platform_notify) 4621da177e4SLinus Torvalds platform_notify(dev); 4631da177e4SLinus Torvalds Done: 464e9a7d305SGreg Kroah-Hartman kfree(class_name); 4651da177e4SLinus Torvalds put_device(dev); 4661da177e4SLinus Torvalds return error; 4671da177e4SLinus Torvalds BusError: 4681da177e4SLinus Torvalds device_pm_remove(dev); 4691da177e4SLinus Torvalds PMError: 470de0ff00dSGreg Kroah-Hartman device_remove_groups(dev); 471de0ff00dSGreg Kroah-Hartman GroupError: 4722620efefSGreg Kroah-Hartman device_remove_attrs(dev); 4732620efefSGreg Kroah-Hartman AttrsError: 47423681e47SGreg Kroah-Hartman if (dev->devt_attr) { 47523681e47SGreg Kroah-Hartman device_remove_file(dev, dev->devt_attr); 47623681e47SGreg Kroah-Hartman kfree(dev->devt_attr); 47723681e47SGreg Kroah-Hartman } 47823681e47SGreg Kroah-Hartman attrError: 479312c004dSKay Sievers kobject_uevent(&dev->kobj, KOBJ_REMOVE); 4801da177e4SLinus Torvalds kobject_del(&dev->kobj); 4811da177e4SLinus Torvalds Error: 4821da177e4SLinus Torvalds if (parent) 4831da177e4SLinus Torvalds put_device(parent); 4841da177e4SLinus Torvalds goto Done; 4851da177e4SLinus Torvalds } 4861da177e4SLinus Torvalds 4871da177e4SLinus Torvalds 4881da177e4SLinus Torvalds /** 4891da177e4SLinus Torvalds * device_register - register a device with the system. 4901da177e4SLinus Torvalds * @dev: pointer to the device structure 4911da177e4SLinus Torvalds * 4921da177e4SLinus Torvalds * This happens in two clean steps - initialize the device 4931da177e4SLinus Torvalds * and add it to the system. The two steps can be called 4941da177e4SLinus Torvalds * separately, but this is the easiest and most common. 4951da177e4SLinus Torvalds * I.e. you should only call the two helpers separately if 4961da177e4SLinus Torvalds * have a clearly defined need to use and refcount the device 4971da177e4SLinus Torvalds * before it is added to the hierarchy. 4981da177e4SLinus Torvalds */ 4991da177e4SLinus Torvalds 5001da177e4SLinus Torvalds int device_register(struct device *dev) 5011da177e4SLinus Torvalds { 5021da177e4SLinus Torvalds device_initialize(dev); 5031da177e4SLinus Torvalds return device_add(dev); 5041da177e4SLinus Torvalds } 5051da177e4SLinus Torvalds 5061da177e4SLinus Torvalds 5071da177e4SLinus Torvalds /** 5081da177e4SLinus Torvalds * get_device - increment reference count for device. 5091da177e4SLinus Torvalds * @dev: device. 5101da177e4SLinus Torvalds * 5111da177e4SLinus Torvalds * This simply forwards the call to kobject_get(), though 5121da177e4SLinus Torvalds * we do take care to provide for the case that we get a NULL 5131da177e4SLinus Torvalds * pointer passed in. 5141da177e4SLinus Torvalds */ 5151da177e4SLinus Torvalds 5161da177e4SLinus Torvalds struct device * get_device(struct device * dev) 5171da177e4SLinus Torvalds { 5181da177e4SLinus Torvalds return dev ? to_dev(kobject_get(&dev->kobj)) : NULL; 5191da177e4SLinus Torvalds } 5201da177e4SLinus Torvalds 5211da177e4SLinus Torvalds 5221da177e4SLinus Torvalds /** 5231da177e4SLinus Torvalds * put_device - decrement reference count. 5241da177e4SLinus Torvalds * @dev: device in question. 5251da177e4SLinus Torvalds */ 5261da177e4SLinus Torvalds void put_device(struct device * dev) 5271da177e4SLinus Torvalds { 5281da177e4SLinus Torvalds if (dev) 5291da177e4SLinus Torvalds kobject_put(&dev->kobj); 5301da177e4SLinus Torvalds } 5311da177e4SLinus Torvalds 5321da177e4SLinus Torvalds 5331da177e4SLinus Torvalds /** 5341da177e4SLinus Torvalds * device_del - delete device from system. 5351da177e4SLinus Torvalds * @dev: device. 5361da177e4SLinus Torvalds * 5371da177e4SLinus Torvalds * This is the first part of the device unregistration 5381da177e4SLinus Torvalds * sequence. This removes the device from the lists we control 5391da177e4SLinus Torvalds * from here, has it removed from the other driver model 5401da177e4SLinus Torvalds * subsystems it was added to in device_add(), and removes it 5411da177e4SLinus Torvalds * from the kobject hierarchy. 5421da177e4SLinus Torvalds * 5431da177e4SLinus Torvalds * NOTE: this should be called manually _iff_ device_add() was 5441da177e4SLinus Torvalds * also called manually. 5451da177e4SLinus Torvalds */ 5461da177e4SLinus Torvalds 5471da177e4SLinus Torvalds void device_del(struct device * dev) 5481da177e4SLinus Torvalds { 5491da177e4SLinus Torvalds struct device * parent = dev->parent; 550e9a7d305SGreg Kroah-Hartman char *class_name = NULL; 5511da177e4SLinus Torvalds 5521da177e4SLinus Torvalds if (parent) 553d62c0f9fSPatrick Mochel klist_del(&dev->knode_parent); 55423681e47SGreg Kroah-Hartman if (dev->devt_attr) 55523681e47SGreg Kroah-Hartman device_remove_file(dev, dev->devt_attr); 556b9d9c82bSKay Sievers if (dev->class) { 557b9d9c82bSKay Sievers sysfs_remove_link(&dev->kobj, "subsystem"); 55823681e47SGreg Kroah-Hartman sysfs_remove_link(&dev->class->subsys.kset.kobj, dev->bus_id); 559e9a7d305SGreg Kroah-Hartman class_name = make_class_name(dev->class->name, &dev->kobj); 56064bb5d2cSGreg Kroah-Hartman if (parent) { 561e9a7d305SGreg Kroah-Hartman sysfs_remove_link(&dev->kobj, "device"); 562e9a7d305SGreg Kroah-Hartman sysfs_remove_link(&dev->parent->kobj, class_name); 56364bb5d2cSGreg Kroah-Hartman } 564e9a7d305SGreg Kroah-Hartman kfree(class_name); 5655d9fd169SGreg Kroah-Hartman down(&dev->class->sem); 5665d9fd169SGreg Kroah-Hartman list_del_init(&dev->node); 5675d9fd169SGreg Kroah-Hartman up(&dev->class->sem); 568b9d9c82bSKay Sievers } 569a7fd6706SKay Sievers device_remove_file(dev, &dev->uevent_attr); 570de0ff00dSGreg Kroah-Hartman device_remove_groups(dev); 5712620efefSGreg Kroah-Hartman device_remove_attrs(dev); 5721da177e4SLinus Torvalds 5731da177e4SLinus Torvalds /* Notify the platform of the removal, in case they 5741da177e4SLinus Torvalds * need to do anything... 5751da177e4SLinus Torvalds */ 5761da177e4SLinus Torvalds if (platform_notify_remove) 5771da177e4SLinus Torvalds platform_notify_remove(dev); 5781da177e4SLinus Torvalds bus_remove_device(dev); 5791da177e4SLinus Torvalds device_pm_remove(dev); 580312c004dSKay Sievers kobject_uevent(&dev->kobj, KOBJ_REMOVE); 5811da177e4SLinus Torvalds kobject_del(&dev->kobj); 5821da177e4SLinus Torvalds if (parent) 5831da177e4SLinus Torvalds put_device(parent); 5841da177e4SLinus Torvalds } 5851da177e4SLinus Torvalds 5861da177e4SLinus Torvalds /** 5871da177e4SLinus Torvalds * device_unregister - unregister device from system. 5881da177e4SLinus Torvalds * @dev: device going away. 5891da177e4SLinus Torvalds * 5901da177e4SLinus Torvalds * We do this in two parts, like we do device_register(). First, 5911da177e4SLinus Torvalds * we remove it from all the subsystems with device_del(), then 5921da177e4SLinus Torvalds * we decrement the reference count via put_device(). If that 5931da177e4SLinus Torvalds * is the final reference count, the device will be cleaned up 5941da177e4SLinus Torvalds * via device_release() above. Otherwise, the structure will 5951da177e4SLinus Torvalds * stick around until the final reference to the device is dropped. 5961da177e4SLinus Torvalds */ 5971da177e4SLinus Torvalds void device_unregister(struct device * dev) 5981da177e4SLinus Torvalds { 5991da177e4SLinus Torvalds pr_debug("DEV: Unregistering device. ID = '%s'\n", dev->bus_id); 6001da177e4SLinus Torvalds device_del(dev); 6011da177e4SLinus Torvalds put_device(dev); 6021da177e4SLinus Torvalds } 6031da177e4SLinus Torvalds 6041da177e4SLinus Torvalds 60536239577Smochel@digitalimplant.org static struct device * next_device(struct klist_iter * i) 60636239577Smochel@digitalimplant.org { 60736239577Smochel@digitalimplant.org struct klist_node * n = klist_next(i); 60836239577Smochel@digitalimplant.org return n ? container_of(n, struct device, knode_parent) : NULL; 60936239577Smochel@digitalimplant.org } 61036239577Smochel@digitalimplant.org 6111da177e4SLinus Torvalds /** 6121da177e4SLinus Torvalds * device_for_each_child - device child iterator. 613c41455fbSRandy Dunlap * @parent: parent struct device. 6141da177e4SLinus Torvalds * @data: data for the callback. 6151da177e4SLinus Torvalds * @fn: function to be called for each device. 6161da177e4SLinus Torvalds * 617c41455fbSRandy Dunlap * Iterate over @parent's child devices, and call @fn for each, 6181da177e4SLinus Torvalds * passing it @data. 6191da177e4SLinus Torvalds * 6201da177e4SLinus Torvalds * We check the return of @fn each time. If it returns anything 6211da177e4SLinus Torvalds * other than 0, we break out and return that value. 6221da177e4SLinus Torvalds */ 62336239577Smochel@digitalimplant.org int device_for_each_child(struct device * parent, void * data, 6241da177e4SLinus Torvalds int (*fn)(struct device *, void *)) 6251da177e4SLinus Torvalds { 62636239577Smochel@digitalimplant.org struct klist_iter i; 6271da177e4SLinus Torvalds struct device * child; 6281da177e4SLinus Torvalds int error = 0; 6291da177e4SLinus Torvalds 63036239577Smochel@digitalimplant.org klist_iter_init(&parent->klist_children, &i); 63136239577Smochel@digitalimplant.org while ((child = next_device(&i)) && !error) 63236239577Smochel@digitalimplant.org error = fn(child, data); 63336239577Smochel@digitalimplant.org klist_iter_exit(&i); 6341da177e4SLinus Torvalds return error; 6351da177e4SLinus Torvalds } 6361da177e4SLinus Torvalds 6371da177e4SLinus Torvalds int __init devices_init(void) 6381da177e4SLinus Torvalds { 6391da177e4SLinus Torvalds return subsystem_register(&devices_subsys); 6401da177e4SLinus Torvalds } 6411da177e4SLinus Torvalds 6421da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(device_for_each_child); 6431da177e4SLinus Torvalds 6441da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(device_initialize); 6451da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(device_add); 6461da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(device_register); 6471da177e4SLinus Torvalds 6481da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(device_del); 6491da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(device_unregister); 6501da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(get_device); 6511da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(put_device); 6521da177e4SLinus Torvalds 6531da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(device_create_file); 6541da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(device_remove_file); 65523681e47SGreg Kroah-Hartman 65623681e47SGreg Kroah-Hartman 65723681e47SGreg Kroah-Hartman static void device_create_release(struct device *dev) 65823681e47SGreg Kroah-Hartman { 65923681e47SGreg Kroah-Hartman pr_debug("%s called for %s\n", __FUNCTION__, dev->bus_id); 66023681e47SGreg Kroah-Hartman kfree(dev); 66123681e47SGreg Kroah-Hartman } 66223681e47SGreg Kroah-Hartman 66323681e47SGreg Kroah-Hartman /** 66423681e47SGreg Kroah-Hartman * device_create - creates a device and registers it with sysfs 66542734dafSHenrik Kretzschmar * @class: pointer to the struct class that this device should be registered to 66642734dafSHenrik Kretzschmar * @parent: pointer to the parent struct device of this new device, if any 66742734dafSHenrik Kretzschmar * @devt: the dev_t for the char device to be added 66842734dafSHenrik Kretzschmar * @fmt: string for the device's name 66923681e47SGreg Kroah-Hartman * 67042734dafSHenrik Kretzschmar * This function can be used by char device classes. A struct device 67142734dafSHenrik Kretzschmar * will be created in sysfs, registered to the specified class. 67242734dafSHenrik Kretzschmar * 67323681e47SGreg Kroah-Hartman * A "dev" file will be created, showing the dev_t for the device, if 67423681e47SGreg Kroah-Hartman * the dev_t is not 0,0. 67542734dafSHenrik Kretzschmar * If a pointer to a parent struct device is passed in, the newly created 67642734dafSHenrik Kretzschmar * struct device will be a child of that device in sysfs. 67742734dafSHenrik Kretzschmar * The pointer to the struct device will be returned from the call. 67842734dafSHenrik Kretzschmar * Any further sysfs files that might be required can be created using this 67923681e47SGreg Kroah-Hartman * pointer. 68023681e47SGreg Kroah-Hartman * 68123681e47SGreg Kroah-Hartman * Note: the struct class passed to this function must have previously 68223681e47SGreg Kroah-Hartman * been created with a call to class_create(). 68323681e47SGreg Kroah-Hartman */ 68423681e47SGreg Kroah-Hartman struct device *device_create(struct class *class, struct device *parent, 6855cbe5f8aSGreg Kroah-Hartman dev_t devt, const char *fmt, ...) 68623681e47SGreg Kroah-Hartman { 68723681e47SGreg Kroah-Hartman va_list args; 68823681e47SGreg Kroah-Hartman struct device *dev = NULL; 68923681e47SGreg Kroah-Hartman int retval = -ENODEV; 69023681e47SGreg Kroah-Hartman 69123681e47SGreg Kroah-Hartman if (class == NULL || IS_ERR(class)) 69223681e47SGreg Kroah-Hartman goto error; 69323681e47SGreg Kroah-Hartman 69423681e47SGreg Kroah-Hartman dev = kzalloc(sizeof(*dev), GFP_KERNEL); 69523681e47SGreg Kroah-Hartman if (!dev) { 69623681e47SGreg Kroah-Hartman retval = -ENOMEM; 69723681e47SGreg Kroah-Hartman goto error; 69823681e47SGreg Kroah-Hartman } 69923681e47SGreg Kroah-Hartman 70023681e47SGreg Kroah-Hartman dev->devt = devt; 70123681e47SGreg Kroah-Hartman dev->class = class; 70223681e47SGreg Kroah-Hartman dev->parent = parent; 70323681e47SGreg Kroah-Hartman dev->release = device_create_release; 70423681e47SGreg Kroah-Hartman 70523681e47SGreg Kroah-Hartman va_start(args, fmt); 70623681e47SGreg Kroah-Hartman vsnprintf(dev->bus_id, BUS_ID_SIZE, fmt, args); 70723681e47SGreg Kroah-Hartman va_end(args); 70823681e47SGreg Kroah-Hartman retval = device_register(dev); 70923681e47SGreg Kroah-Hartman if (retval) 71023681e47SGreg Kroah-Hartman goto error; 71123681e47SGreg Kroah-Hartman 71223681e47SGreg Kroah-Hartman return dev; 71323681e47SGreg Kroah-Hartman 71423681e47SGreg Kroah-Hartman error: 71523681e47SGreg Kroah-Hartman kfree(dev); 71623681e47SGreg Kroah-Hartman return ERR_PTR(retval); 71723681e47SGreg Kroah-Hartman } 71823681e47SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(device_create); 71923681e47SGreg Kroah-Hartman 72023681e47SGreg Kroah-Hartman /** 72123681e47SGreg Kroah-Hartman * device_destroy - removes a device that was created with device_create() 72242734dafSHenrik Kretzschmar * @class: pointer to the struct class that this device was registered with 72342734dafSHenrik Kretzschmar * @devt: the dev_t of the device that was previously registered 72423681e47SGreg Kroah-Hartman * 72542734dafSHenrik Kretzschmar * This call unregisters and cleans up a device that was created with a 72642734dafSHenrik Kretzschmar * call to device_create(). 72723681e47SGreg Kroah-Hartman */ 72823681e47SGreg Kroah-Hartman void device_destroy(struct class *class, dev_t devt) 72923681e47SGreg Kroah-Hartman { 73023681e47SGreg Kroah-Hartman struct device *dev = NULL; 73123681e47SGreg Kroah-Hartman struct device *dev_tmp; 73223681e47SGreg Kroah-Hartman 73323681e47SGreg Kroah-Hartman down(&class->sem); 73423681e47SGreg Kroah-Hartman list_for_each_entry(dev_tmp, &class->devices, node) { 73523681e47SGreg Kroah-Hartman if (dev_tmp->devt == devt) { 73623681e47SGreg Kroah-Hartman dev = dev_tmp; 73723681e47SGreg Kroah-Hartman break; 73823681e47SGreg Kroah-Hartman } 73923681e47SGreg Kroah-Hartman } 74023681e47SGreg Kroah-Hartman up(&class->sem); 74123681e47SGreg Kroah-Hartman 7425d9fd169SGreg Kroah-Hartman if (dev) 74323681e47SGreg Kroah-Hartman device_unregister(dev); 74423681e47SGreg Kroah-Hartman } 74523681e47SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(device_destroy); 746a2de48caSGreg Kroah-Hartman 747a2de48caSGreg Kroah-Hartman /** 748a2de48caSGreg Kroah-Hartman * device_rename - renames a device 749a2de48caSGreg Kroah-Hartman * @dev: the pointer to the struct device to be renamed 750a2de48caSGreg Kroah-Hartman * @new_name: the new name of the device 751a2de48caSGreg Kroah-Hartman */ 752a2de48caSGreg Kroah-Hartman int device_rename(struct device *dev, char *new_name) 753a2de48caSGreg Kroah-Hartman { 754a2de48caSGreg Kroah-Hartman char *old_class_name = NULL; 755a2de48caSGreg Kroah-Hartman char *new_class_name = NULL; 756a2de48caSGreg Kroah-Hartman char *old_symlink_name = NULL; 757a2de48caSGreg Kroah-Hartman int error; 758a2de48caSGreg Kroah-Hartman 759a2de48caSGreg Kroah-Hartman dev = get_device(dev); 760a2de48caSGreg Kroah-Hartman if (!dev) 761a2de48caSGreg Kroah-Hartman return -EINVAL; 762a2de48caSGreg Kroah-Hartman 763a2de48caSGreg Kroah-Hartman pr_debug("DEVICE: renaming '%s' to '%s'\n", dev->bus_id, new_name); 764a2de48caSGreg Kroah-Hartman 765a2de48caSGreg Kroah-Hartman if ((dev->class) && (dev->parent)) 766a2de48caSGreg Kroah-Hartman old_class_name = make_class_name(dev->class->name, &dev->kobj); 767a2de48caSGreg Kroah-Hartman 768a2de48caSGreg Kroah-Hartman if (dev->class) { 769a2de48caSGreg Kroah-Hartman old_symlink_name = kmalloc(BUS_ID_SIZE, GFP_KERNEL); 770a2de48caSGreg Kroah-Hartman if (!old_symlink_name) 771a2de48caSGreg Kroah-Hartman return -ENOMEM; 772a2de48caSGreg Kroah-Hartman strlcpy(old_symlink_name, dev->bus_id, BUS_ID_SIZE); 773a2de48caSGreg Kroah-Hartman } 774a2de48caSGreg Kroah-Hartman 775a2de48caSGreg Kroah-Hartman strlcpy(dev->bus_id, new_name, BUS_ID_SIZE); 776a2de48caSGreg Kroah-Hartman 777a2de48caSGreg Kroah-Hartman error = kobject_rename(&dev->kobj, new_name); 778a2de48caSGreg Kroah-Hartman 779a2de48caSGreg Kroah-Hartman if (old_class_name) { 780a2de48caSGreg Kroah-Hartman new_class_name = make_class_name(dev->class->name, &dev->kobj); 781a2de48caSGreg Kroah-Hartman if (new_class_name) { 782a2de48caSGreg Kroah-Hartman sysfs_create_link(&dev->parent->kobj, &dev->kobj, 783a2de48caSGreg Kroah-Hartman new_class_name); 784a2de48caSGreg Kroah-Hartman sysfs_remove_link(&dev->parent->kobj, old_class_name); 785a2de48caSGreg Kroah-Hartman } 786a2de48caSGreg Kroah-Hartman } 787a2de48caSGreg Kroah-Hartman if (dev->class) { 788a2de48caSGreg Kroah-Hartman sysfs_remove_link(&dev->class->subsys.kset.kobj, 789a2de48caSGreg Kroah-Hartman old_symlink_name); 790a2de48caSGreg Kroah-Hartman sysfs_create_link(&dev->class->subsys.kset.kobj, &dev->kobj, 791a2de48caSGreg Kroah-Hartman dev->bus_id); 792a2de48caSGreg Kroah-Hartman } 793a2de48caSGreg Kroah-Hartman put_device(dev); 794a2de48caSGreg Kroah-Hartman 795a2de48caSGreg Kroah-Hartman kfree(old_class_name); 796a2de48caSGreg Kroah-Hartman kfree(new_class_name); 797a2de48caSGreg Kroah-Hartman kfree(old_symlink_name); 798a2de48caSGreg Kroah-Hartman 799a2de48caSGreg Kroah-Hartman return error; 800a2de48caSGreg Kroah-Hartman } 801