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> 20116af378SBenjamin Herrenschmidt #include <linux/notifier.h> 21da231fd5SKay Sievers #include <linux/genhd.h> 22815d2d50SAndrew Morton #include <linux/kallsyms.h> 236188e10dSMatthew Wilcox #include <linux/semaphore.h> 24f75b1c60SDave Young #include <linux/mutex.h> 25401097eaSShaohua Li #include <linux/async.h> 261da177e4SLinus Torvalds 271da177e4SLinus Torvalds #include "base.h" 281da177e4SLinus Torvalds #include "power/power.h" 291da177e4SLinus Torvalds 301da177e4SLinus Torvalds int (*platform_notify)(struct device *dev) = NULL; 311da177e4SLinus Torvalds int (*platform_notify_remove)(struct device *dev) = NULL; 32e105b8bfSDan Williams static struct kobject *dev_kobj; 33e105b8bfSDan Williams struct kobject *sysfs_dev_char_kobj; 34e105b8bfSDan Williams struct kobject *sysfs_dev_block_kobj; 351da177e4SLinus Torvalds 364e886c29SGreg Kroah-Hartman #ifdef CONFIG_BLOCK 374e886c29SGreg Kroah-Hartman static inline int device_is_not_partition(struct device *dev) 384e886c29SGreg Kroah-Hartman { 394e886c29SGreg Kroah-Hartman return !(dev->type == &part_type); 404e886c29SGreg Kroah-Hartman } 414e886c29SGreg Kroah-Hartman #else 424e886c29SGreg Kroah-Hartman static inline int device_is_not_partition(struct device *dev) 434e886c29SGreg Kroah-Hartman { 444e886c29SGreg Kroah-Hartman return 1; 454e886c29SGreg Kroah-Hartman } 464e886c29SGreg Kroah-Hartman #endif 471da177e4SLinus Torvalds 483e95637aSAlan Stern /** 493e95637aSAlan Stern * dev_driver_string - Return a device's driver name, if at all possible 503e95637aSAlan Stern * @dev: struct device to get the name of 513e95637aSAlan Stern * 523e95637aSAlan Stern * Will return the device's driver's name if it is bound to a device. If 533e95637aSAlan Stern * the device is not bound to a device, it will return the name of the bus 543e95637aSAlan Stern * it is attached to. If it is not attached to a bus either, an empty 553e95637aSAlan Stern * string will be returned. 563e95637aSAlan Stern */ 57bf9ca69fSJean Delvare const char *dev_driver_string(const struct device *dev) 583e95637aSAlan Stern { 593e95637aSAlan Stern return dev->driver ? dev->driver->name : 60a456b702SJean Delvare (dev->bus ? dev->bus->name : 61a456b702SJean Delvare (dev->class ? dev->class->name : "")); 623e95637aSAlan Stern } 63310a922dSMatthew Wilcox EXPORT_SYMBOL(dev_driver_string); 643e95637aSAlan Stern 651da177e4SLinus Torvalds #define to_dev(obj) container_of(obj, struct device, kobj) 661da177e4SLinus Torvalds #define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr) 671da177e4SLinus Torvalds 684a3ad20cSGreg Kroah-Hartman static ssize_t dev_attr_show(struct kobject *kobj, struct attribute *attr, 694a3ad20cSGreg Kroah-Hartman char *buf) 701da177e4SLinus Torvalds { 711da177e4SLinus Torvalds struct device_attribute *dev_attr = to_dev_attr(attr); 721da177e4SLinus Torvalds struct device *dev = to_dev(kobj); 734a0c20bfSDmitry Torokhov ssize_t ret = -EIO; 741da177e4SLinus Torvalds 751da177e4SLinus Torvalds if (dev_attr->show) 7654b6f35cSYani Ioannou ret = dev_attr->show(dev, dev_attr, buf); 77815d2d50SAndrew Morton if (ret >= (ssize_t)PAGE_SIZE) { 78815d2d50SAndrew Morton print_symbol("dev_attr_show: %s returned bad count\n", 79815d2d50SAndrew Morton (unsigned long)dev_attr->show); 80815d2d50SAndrew Morton } 811da177e4SLinus Torvalds return ret; 821da177e4SLinus Torvalds } 831da177e4SLinus Torvalds 844a3ad20cSGreg Kroah-Hartman static ssize_t dev_attr_store(struct kobject *kobj, struct attribute *attr, 851da177e4SLinus Torvalds const char *buf, size_t count) 861da177e4SLinus Torvalds { 871da177e4SLinus Torvalds struct device_attribute *dev_attr = to_dev_attr(attr); 881da177e4SLinus Torvalds struct device *dev = to_dev(kobj); 894a0c20bfSDmitry Torokhov ssize_t ret = -EIO; 901da177e4SLinus Torvalds 911da177e4SLinus Torvalds if (dev_attr->store) 9254b6f35cSYani Ioannou ret = dev_attr->store(dev, dev_attr, buf, count); 931da177e4SLinus Torvalds return ret; 941da177e4SLinus Torvalds } 951da177e4SLinus Torvalds 961da177e4SLinus Torvalds static struct sysfs_ops dev_sysfs_ops = { 971da177e4SLinus Torvalds .show = dev_attr_show, 981da177e4SLinus Torvalds .store = dev_attr_store, 991da177e4SLinus Torvalds }; 1001da177e4SLinus Torvalds 1011da177e4SLinus Torvalds 1021da177e4SLinus Torvalds /** 1031da177e4SLinus Torvalds * device_release - free device structure. 1041da177e4SLinus Torvalds * @kobj: device's kobject. 1051da177e4SLinus Torvalds * 1061da177e4SLinus Torvalds * This is called once the reference count for the object 1071da177e4SLinus Torvalds * reaches 0. We forward the call to the device's release 1081da177e4SLinus Torvalds * method, which should handle actually freeing the structure. 1091da177e4SLinus Torvalds */ 1101da177e4SLinus Torvalds static void device_release(struct kobject *kobj) 1111da177e4SLinus Torvalds { 1121da177e4SLinus Torvalds struct device *dev = to_dev(kobj); 113fb069a5dSGreg Kroah-Hartman struct device_private *p = dev->p; 1141da177e4SLinus Torvalds 1151da177e4SLinus Torvalds if (dev->release) 1161da177e4SLinus Torvalds dev->release(dev); 117f9f852dfSKay Sievers else if (dev->type && dev->type->release) 118f9f852dfSKay Sievers dev->type->release(dev); 1192620efefSGreg Kroah-Hartman else if (dev->class && dev->class->dev_release) 1202620efefSGreg Kroah-Hartman dev->class->dev_release(dev); 121f810a5cfSArjan van de Ven else 122f810a5cfSArjan van de Ven WARN(1, KERN_ERR "Device '%s' does not have a release() " 1234a3ad20cSGreg Kroah-Hartman "function, it is broken and must be fixed.\n", 1241e0b2cf9SKay Sievers dev_name(dev)); 125fb069a5dSGreg Kroah-Hartman kfree(p); 1261da177e4SLinus Torvalds } 1271da177e4SLinus Torvalds 1288f4afc41SGreg Kroah-Hartman static struct kobj_type device_ktype = { 1291da177e4SLinus Torvalds .release = device_release, 1301da177e4SLinus Torvalds .sysfs_ops = &dev_sysfs_ops, 1311da177e4SLinus Torvalds }; 1321da177e4SLinus Torvalds 1331da177e4SLinus Torvalds 134312c004dSKay Sievers static int dev_uevent_filter(struct kset *kset, struct kobject *kobj) 1351da177e4SLinus Torvalds { 1361da177e4SLinus Torvalds struct kobj_type *ktype = get_ktype(kobj); 1371da177e4SLinus Torvalds 1388f4afc41SGreg Kroah-Hartman if (ktype == &device_ktype) { 1391da177e4SLinus Torvalds struct device *dev = to_dev(kobj); 1401da177e4SLinus Torvalds if (dev->bus) 1411da177e4SLinus Torvalds return 1; 14223681e47SGreg Kroah-Hartman if (dev->class) 14323681e47SGreg Kroah-Hartman return 1; 1441da177e4SLinus Torvalds } 1451da177e4SLinus Torvalds return 0; 1461da177e4SLinus Torvalds } 1471da177e4SLinus Torvalds 148312c004dSKay Sievers static const char *dev_uevent_name(struct kset *kset, struct kobject *kobj) 1491da177e4SLinus Torvalds { 1501da177e4SLinus Torvalds struct device *dev = to_dev(kobj); 1511da177e4SLinus Torvalds 15223681e47SGreg Kroah-Hartman if (dev->bus) 1531da177e4SLinus Torvalds return dev->bus->name; 15423681e47SGreg Kroah-Hartman if (dev->class) 15523681e47SGreg Kroah-Hartman return dev->class->name; 15623681e47SGreg Kroah-Hartman return NULL; 1571da177e4SLinus Torvalds } 1581da177e4SLinus Torvalds 1597eff2e7aSKay Sievers static int dev_uevent(struct kset *kset, struct kobject *kobj, 1607eff2e7aSKay Sievers struct kobj_uevent_env *env) 1611da177e4SLinus Torvalds { 1621da177e4SLinus Torvalds struct device *dev = to_dev(kobj); 1631da177e4SLinus Torvalds int retval = 0; 1641da177e4SLinus Torvalds 1656fcf53acSKay Sievers /* add device node properties if present */ 16623681e47SGreg Kroah-Hartman if (MAJOR(dev->devt)) { 1676fcf53acSKay Sievers const char *tmp; 1686fcf53acSKay Sievers const char *name; 1696fcf53acSKay Sievers 1707eff2e7aSKay Sievers add_uevent_var(env, "MAJOR=%u", MAJOR(dev->devt)); 1717eff2e7aSKay Sievers add_uevent_var(env, "MINOR=%u", MINOR(dev->devt)); 1726fcf53acSKay Sievers name = device_get_nodename(dev, &tmp); 1736fcf53acSKay Sievers if (name) { 1746fcf53acSKay Sievers add_uevent_var(env, "DEVNAME=%s", name); 1756fcf53acSKay Sievers kfree(tmp); 1766fcf53acSKay Sievers } 17723681e47SGreg Kroah-Hartman } 17823681e47SGreg Kroah-Hartman 179414264f9SKay Sievers if (dev->type && dev->type->name) 1807eff2e7aSKay Sievers add_uevent_var(env, "DEVTYPE=%s", dev->type->name); 181414264f9SKay Sievers 182239378f1SKay Sievers if (dev->driver) 1837eff2e7aSKay Sievers add_uevent_var(env, "DRIVER=%s", dev->driver->name); 184239378f1SKay Sievers 185a87cb2acSKay Sievers #ifdef CONFIG_SYSFS_DEPRECATED 186239378f1SKay Sievers if (dev->class) { 187239378f1SKay Sievers struct device *parent = dev->parent; 188239378f1SKay Sievers 189239378f1SKay Sievers /* find first bus device in parent chain */ 190239378f1SKay Sievers while (parent && !parent->bus) 191239378f1SKay Sievers parent = parent->parent; 192239378f1SKay Sievers if (parent && parent->bus) { 193239378f1SKay Sievers const char *path; 194239378f1SKay Sievers 195239378f1SKay Sievers path = kobject_get_path(&parent->kobj, GFP_KERNEL); 1962c7afd12SKay Sievers if (path) { 1977eff2e7aSKay Sievers add_uevent_var(env, "PHYSDEVPATH=%s", path); 198239378f1SKay Sievers kfree(path); 1992c7afd12SKay Sievers } 200239378f1SKay Sievers 2017eff2e7aSKay Sievers add_uevent_var(env, "PHYSDEVBUS=%s", parent->bus->name); 202239378f1SKay Sievers 203239378f1SKay Sievers if (parent->driver) 2047eff2e7aSKay Sievers add_uevent_var(env, "PHYSDEVDRIVER=%s", 2057eff2e7aSKay Sievers parent->driver->name); 206239378f1SKay Sievers } 207239378f1SKay Sievers } else if (dev->bus) { 2087eff2e7aSKay Sievers add_uevent_var(env, "PHYSDEVBUS=%s", dev->bus->name); 209239378f1SKay Sievers 210239378f1SKay Sievers if (dev->driver) 2114a3ad20cSGreg Kroah-Hartman add_uevent_var(env, "PHYSDEVDRIVER=%s", 2124a3ad20cSGreg Kroah-Hartman dev->driver->name); 213d81d9d6bSKay Sievers } 214239378f1SKay Sievers #endif 2151da177e4SLinus Torvalds 2161da177e4SLinus Torvalds /* have the bus specific function add its stuff */ 2177eff2e7aSKay Sievers if (dev->bus && dev->bus->uevent) { 2187eff2e7aSKay Sievers retval = dev->bus->uevent(dev, env); 219f9f852dfSKay Sievers if (retval) 2207dc72b28SGreg Kroah-Hartman pr_debug("device: '%s': %s: bus uevent() returned %d\n", 2211e0b2cf9SKay Sievers dev_name(dev), __func__, retval); 2221da177e4SLinus Torvalds } 2231da177e4SLinus Torvalds 2242620efefSGreg Kroah-Hartman /* have the class specific function add its stuff */ 2257eff2e7aSKay Sievers if (dev->class && dev->class->dev_uevent) { 2267eff2e7aSKay Sievers retval = dev->class->dev_uevent(dev, env); 227f9f852dfSKay Sievers if (retval) 2287dc72b28SGreg Kroah-Hartman pr_debug("device: '%s': %s: class uevent() " 2291e0b2cf9SKay Sievers "returned %d\n", dev_name(dev), 2302b3a302aSHarvey Harrison __func__, retval); 2312620efefSGreg Kroah-Hartman } 232f9f852dfSKay Sievers 233f9f852dfSKay Sievers /* have the device type specific fuction add its stuff */ 2347eff2e7aSKay Sievers if (dev->type && dev->type->uevent) { 2357eff2e7aSKay Sievers retval = dev->type->uevent(dev, env); 236f9f852dfSKay Sievers if (retval) 2377dc72b28SGreg Kroah-Hartman pr_debug("device: '%s': %s: dev_type uevent() " 2381e0b2cf9SKay Sievers "returned %d\n", dev_name(dev), 2392b3a302aSHarvey Harrison __func__, retval); 2402620efefSGreg Kroah-Hartman } 2412620efefSGreg Kroah-Hartman 2421da177e4SLinus Torvalds return retval; 2431da177e4SLinus Torvalds } 2441da177e4SLinus Torvalds 245312c004dSKay Sievers static struct kset_uevent_ops device_uevent_ops = { 246312c004dSKay Sievers .filter = dev_uevent_filter, 247312c004dSKay Sievers .name = dev_uevent_name, 248312c004dSKay Sievers .uevent = dev_uevent, 2491da177e4SLinus Torvalds }; 2501da177e4SLinus Torvalds 25116574dccSKay Sievers static ssize_t show_uevent(struct device *dev, struct device_attribute *attr, 25216574dccSKay Sievers char *buf) 25316574dccSKay Sievers { 25416574dccSKay Sievers struct kobject *top_kobj; 25516574dccSKay Sievers struct kset *kset; 2567eff2e7aSKay Sievers struct kobj_uevent_env *env = NULL; 25716574dccSKay Sievers int i; 25816574dccSKay Sievers size_t count = 0; 25916574dccSKay Sievers int retval; 26016574dccSKay Sievers 26116574dccSKay Sievers /* search the kset, the device belongs to */ 26216574dccSKay Sievers top_kobj = &dev->kobj; 2635c5daf65SKay Sievers while (!top_kobj->kset && top_kobj->parent) 26416574dccSKay Sievers top_kobj = top_kobj->parent; 26516574dccSKay Sievers if (!top_kobj->kset) 26616574dccSKay Sievers goto out; 2675c5daf65SKay Sievers 26816574dccSKay Sievers kset = top_kobj->kset; 26916574dccSKay Sievers if (!kset->uevent_ops || !kset->uevent_ops->uevent) 27016574dccSKay Sievers goto out; 27116574dccSKay Sievers 27216574dccSKay Sievers /* respect filter */ 27316574dccSKay Sievers if (kset->uevent_ops && kset->uevent_ops->filter) 27416574dccSKay Sievers if (!kset->uevent_ops->filter(kset, &dev->kobj)) 27516574dccSKay Sievers goto out; 27616574dccSKay Sievers 2777eff2e7aSKay Sievers env = kzalloc(sizeof(struct kobj_uevent_env), GFP_KERNEL); 2787eff2e7aSKay Sievers if (!env) 279c7308c81SGreg Kroah-Hartman return -ENOMEM; 280c7308c81SGreg Kroah-Hartman 28116574dccSKay Sievers /* let the kset specific function add its keys */ 2827eff2e7aSKay Sievers retval = kset->uevent_ops->uevent(kset, &dev->kobj, env); 28316574dccSKay Sievers if (retval) 28416574dccSKay Sievers goto out; 28516574dccSKay Sievers 28616574dccSKay Sievers /* copy keys to file */ 2877eff2e7aSKay Sievers for (i = 0; i < env->envp_idx; i++) 2887eff2e7aSKay Sievers count += sprintf(&buf[count], "%s\n", env->envp[i]); 28916574dccSKay Sievers out: 2907eff2e7aSKay Sievers kfree(env); 29116574dccSKay Sievers return count; 29216574dccSKay Sievers } 29316574dccSKay Sievers 294a7fd6706SKay Sievers static ssize_t store_uevent(struct device *dev, struct device_attribute *attr, 295a7fd6706SKay Sievers const char *buf, size_t count) 296a7fd6706SKay Sievers { 29760a96a59SKay Sievers enum kobject_action action; 29860a96a59SKay Sievers 2995c5daf65SKay Sievers if (kobject_action_type(buf, count, &action) == 0) { 30060a96a59SKay Sievers kobject_uevent(&dev->kobj, action); 30160a96a59SKay Sievers goto out; 30260a96a59SKay Sievers } 30360a96a59SKay Sievers 30422af74f3SKay Sievers dev_err(dev, "uevent: unsupported action-string; this will " 30560a96a59SKay Sievers "be ignored in a future kernel version\n"); 306312c004dSKay Sievers kobject_uevent(&dev->kobj, KOBJ_ADD); 30760a96a59SKay Sievers out: 308a7fd6706SKay Sievers return count; 309a7fd6706SKay Sievers } 310a7fd6706SKay Sievers 311ad6a1e1cSTejun Heo static struct device_attribute uevent_attr = 312ad6a1e1cSTejun Heo __ATTR(uevent, S_IRUGO | S_IWUSR, show_uevent, store_uevent); 313ad6a1e1cSTejun Heo 314621a1672SDmitry Torokhov static int device_add_attributes(struct device *dev, 315621a1672SDmitry Torokhov struct device_attribute *attrs) 316de0ff00dSGreg Kroah-Hartman { 317de0ff00dSGreg Kroah-Hartman int error = 0; 318621a1672SDmitry Torokhov int i; 319de0ff00dSGreg Kroah-Hartman 320621a1672SDmitry Torokhov if (attrs) { 321621a1672SDmitry Torokhov for (i = 0; attr_name(attrs[i]); i++) { 322621a1672SDmitry Torokhov error = device_create_file(dev, &attrs[i]); 323621a1672SDmitry Torokhov if (error) 324621a1672SDmitry Torokhov break; 325621a1672SDmitry Torokhov } 326621a1672SDmitry Torokhov if (error) 327de0ff00dSGreg Kroah-Hartman while (--i >= 0) 328621a1672SDmitry Torokhov device_remove_file(dev, &attrs[i]); 329de0ff00dSGreg Kroah-Hartman } 330de0ff00dSGreg Kroah-Hartman return error; 331de0ff00dSGreg Kroah-Hartman } 332de0ff00dSGreg Kroah-Hartman 333621a1672SDmitry Torokhov static void device_remove_attributes(struct device *dev, 334621a1672SDmitry Torokhov struct device_attribute *attrs) 335de0ff00dSGreg Kroah-Hartman { 336de0ff00dSGreg Kroah-Hartman int i; 337621a1672SDmitry Torokhov 338621a1672SDmitry Torokhov if (attrs) 339621a1672SDmitry Torokhov for (i = 0; attr_name(attrs[i]); i++) 340621a1672SDmitry Torokhov device_remove_file(dev, &attrs[i]); 341621a1672SDmitry Torokhov } 342621a1672SDmitry Torokhov 343621a1672SDmitry Torokhov static int device_add_groups(struct device *dev, 344621a1672SDmitry Torokhov struct attribute_group **groups) 345621a1672SDmitry Torokhov { 346621a1672SDmitry Torokhov int error = 0; 347621a1672SDmitry Torokhov int i; 348621a1672SDmitry Torokhov 349621a1672SDmitry Torokhov if (groups) { 350621a1672SDmitry Torokhov for (i = 0; groups[i]; i++) { 351621a1672SDmitry Torokhov error = sysfs_create_group(&dev->kobj, groups[i]); 352621a1672SDmitry Torokhov if (error) { 353621a1672SDmitry Torokhov while (--i >= 0) 3544a3ad20cSGreg Kroah-Hartman sysfs_remove_group(&dev->kobj, 3554a3ad20cSGreg Kroah-Hartman groups[i]); 356621a1672SDmitry Torokhov break; 357de0ff00dSGreg Kroah-Hartman } 358de0ff00dSGreg Kroah-Hartman } 359de0ff00dSGreg Kroah-Hartman } 360621a1672SDmitry Torokhov return error; 361621a1672SDmitry Torokhov } 362621a1672SDmitry Torokhov 363621a1672SDmitry Torokhov static void device_remove_groups(struct device *dev, 364621a1672SDmitry Torokhov struct attribute_group **groups) 365621a1672SDmitry Torokhov { 366621a1672SDmitry Torokhov int i; 367621a1672SDmitry Torokhov 368621a1672SDmitry Torokhov if (groups) 369621a1672SDmitry Torokhov for (i = 0; groups[i]; i++) 370621a1672SDmitry Torokhov sysfs_remove_group(&dev->kobj, groups[i]); 371621a1672SDmitry Torokhov } 372de0ff00dSGreg Kroah-Hartman 3732620efefSGreg Kroah-Hartman static int device_add_attrs(struct device *dev) 3742620efefSGreg Kroah-Hartman { 3752620efefSGreg Kroah-Hartman struct class *class = dev->class; 376f9f852dfSKay Sievers struct device_type *type = dev->type; 377621a1672SDmitry Torokhov int error; 3782620efefSGreg Kroah-Hartman 379621a1672SDmitry Torokhov if (class) { 380621a1672SDmitry Torokhov error = device_add_attributes(dev, class->dev_attrs); 3812620efefSGreg Kroah-Hartman if (error) 382621a1672SDmitry Torokhov return error; 383f9f852dfSKay Sievers } 384f9f852dfSKay Sievers 385621a1672SDmitry Torokhov if (type) { 386621a1672SDmitry Torokhov error = device_add_groups(dev, type->groups); 387f9f852dfSKay Sievers if (error) 388621a1672SDmitry Torokhov goto err_remove_class_attrs; 389f9f852dfSKay Sievers } 390621a1672SDmitry Torokhov 391621a1672SDmitry Torokhov error = device_add_groups(dev, dev->groups); 392f9f852dfSKay Sievers if (error) 393621a1672SDmitry Torokhov goto err_remove_type_groups; 394621a1672SDmitry Torokhov 395621a1672SDmitry Torokhov return 0; 396621a1672SDmitry Torokhov 397621a1672SDmitry Torokhov err_remove_type_groups: 398621a1672SDmitry Torokhov if (type) 399621a1672SDmitry Torokhov device_remove_groups(dev, type->groups); 400621a1672SDmitry Torokhov err_remove_class_attrs: 401621a1672SDmitry Torokhov if (class) 402621a1672SDmitry Torokhov device_remove_attributes(dev, class->dev_attrs); 403f9f852dfSKay Sievers 4042620efefSGreg Kroah-Hartman return error; 4052620efefSGreg Kroah-Hartman } 4062620efefSGreg Kroah-Hartman 4072620efefSGreg Kroah-Hartman static void device_remove_attrs(struct device *dev) 4082620efefSGreg Kroah-Hartman { 4092620efefSGreg Kroah-Hartman struct class *class = dev->class; 410f9f852dfSKay Sievers struct device_type *type = dev->type; 4112620efefSGreg Kroah-Hartman 412621a1672SDmitry Torokhov device_remove_groups(dev, dev->groups); 413f9f852dfSKay Sievers 414621a1672SDmitry Torokhov if (type) 415621a1672SDmitry Torokhov device_remove_groups(dev, type->groups); 416621a1672SDmitry Torokhov 417621a1672SDmitry Torokhov if (class) 418621a1672SDmitry Torokhov device_remove_attributes(dev, class->dev_attrs); 4192620efefSGreg Kroah-Hartman } 4202620efefSGreg Kroah-Hartman 4212620efefSGreg Kroah-Hartman 42223681e47SGreg Kroah-Hartman static ssize_t show_dev(struct device *dev, struct device_attribute *attr, 42323681e47SGreg Kroah-Hartman char *buf) 42423681e47SGreg Kroah-Hartman { 42523681e47SGreg Kroah-Hartman return print_dev_t(buf, dev->devt); 42623681e47SGreg Kroah-Hartman } 42723681e47SGreg Kroah-Hartman 428ad6a1e1cSTejun Heo static struct device_attribute devt_attr = 429ad6a1e1cSTejun Heo __ATTR(dev, S_IRUGO, show_dev, NULL); 430ad6a1e1cSTejun Heo 431881c6cfdSGreg Kroah-Hartman /* kset to create /sys/devices/ */ 432881c6cfdSGreg Kroah-Hartman struct kset *devices_kset; 4331da177e4SLinus Torvalds 4341da177e4SLinus Torvalds /** 4351da177e4SLinus Torvalds * device_create_file - create sysfs attribute file for device. 4361da177e4SLinus Torvalds * @dev: device. 4371da177e4SLinus Torvalds * @attr: device attribute descriptor. 4381da177e4SLinus Torvalds */ 4391da177e4SLinus Torvalds int device_create_file(struct device *dev, struct device_attribute *attr) 4401da177e4SLinus Torvalds { 4411da177e4SLinus Torvalds int error = 0; 4420c98b19fSCornelia Huck if (dev) 4431da177e4SLinus Torvalds error = sysfs_create_file(&dev->kobj, &attr->attr); 4441da177e4SLinus Torvalds return error; 4451da177e4SLinus Torvalds } 4461da177e4SLinus Torvalds 4471da177e4SLinus Torvalds /** 4481da177e4SLinus Torvalds * device_remove_file - remove sysfs attribute file. 4491da177e4SLinus Torvalds * @dev: device. 4501da177e4SLinus Torvalds * @attr: device attribute descriptor. 4511da177e4SLinus Torvalds */ 4521da177e4SLinus Torvalds void device_remove_file(struct device *dev, struct device_attribute *attr) 4531da177e4SLinus Torvalds { 4540c98b19fSCornelia Huck if (dev) 4551da177e4SLinus Torvalds sysfs_remove_file(&dev->kobj, &attr->attr); 4561da177e4SLinus Torvalds } 4571da177e4SLinus Torvalds 4582589f188SGreg Kroah-Hartman /** 4592589f188SGreg Kroah-Hartman * device_create_bin_file - create sysfs binary attribute file for device. 4602589f188SGreg Kroah-Hartman * @dev: device. 4612589f188SGreg Kroah-Hartman * @attr: device binary attribute descriptor. 4622589f188SGreg Kroah-Hartman */ 4632589f188SGreg Kroah-Hartman int device_create_bin_file(struct device *dev, struct bin_attribute *attr) 4642589f188SGreg Kroah-Hartman { 4652589f188SGreg Kroah-Hartman int error = -EINVAL; 4662589f188SGreg Kroah-Hartman if (dev) 4672589f188SGreg Kroah-Hartman error = sysfs_create_bin_file(&dev->kobj, attr); 4682589f188SGreg Kroah-Hartman return error; 4692589f188SGreg Kroah-Hartman } 4702589f188SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(device_create_bin_file); 4712589f188SGreg Kroah-Hartman 4722589f188SGreg Kroah-Hartman /** 4732589f188SGreg Kroah-Hartman * device_remove_bin_file - remove sysfs binary attribute file 4742589f188SGreg Kroah-Hartman * @dev: device. 4752589f188SGreg Kroah-Hartman * @attr: device binary attribute descriptor. 4762589f188SGreg Kroah-Hartman */ 4772589f188SGreg Kroah-Hartman void device_remove_bin_file(struct device *dev, struct bin_attribute *attr) 4782589f188SGreg Kroah-Hartman { 4792589f188SGreg Kroah-Hartman if (dev) 4802589f188SGreg Kroah-Hartman sysfs_remove_bin_file(&dev->kobj, attr); 4812589f188SGreg Kroah-Hartman } 4822589f188SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(device_remove_bin_file); 4832589f188SGreg Kroah-Hartman 484d9a9cdfbSAlan Stern /** 485523ded71SAlan Stern * device_schedule_callback_owner - helper to schedule a callback for a device 486d9a9cdfbSAlan Stern * @dev: device. 487d9a9cdfbSAlan Stern * @func: callback function to invoke later. 488523ded71SAlan Stern * @owner: module owning the callback routine 489d9a9cdfbSAlan Stern * 490d9a9cdfbSAlan Stern * Attribute methods must not unregister themselves or their parent device 491d9a9cdfbSAlan Stern * (which would amount to the same thing). Attempts to do so will deadlock, 492d9a9cdfbSAlan Stern * since unregistration is mutually exclusive with driver callbacks. 493d9a9cdfbSAlan Stern * 494d9a9cdfbSAlan Stern * Instead methods can call this routine, which will attempt to allocate 495d9a9cdfbSAlan Stern * and schedule a workqueue request to call back @func with @dev as its 496d9a9cdfbSAlan Stern * argument in the workqueue's process context. @dev will be pinned until 497d9a9cdfbSAlan Stern * @func returns. 498d9a9cdfbSAlan Stern * 499523ded71SAlan Stern * This routine is usually called via the inline device_schedule_callback(), 500523ded71SAlan Stern * which automatically sets @owner to THIS_MODULE. 501523ded71SAlan Stern * 502d9a9cdfbSAlan Stern * Returns 0 if the request was submitted, -ENOMEM if storage could not 503523ded71SAlan Stern * be allocated, -ENODEV if a reference to @owner isn't available. 504d9a9cdfbSAlan Stern * 505d9a9cdfbSAlan Stern * NOTE: This routine won't work if CONFIG_SYSFS isn't set! It uses an 506d9a9cdfbSAlan Stern * underlying sysfs routine (since it is intended for use by attribute 507d9a9cdfbSAlan Stern * methods), and if sysfs isn't available you'll get nothing but -ENOSYS. 508d9a9cdfbSAlan Stern */ 509523ded71SAlan Stern int device_schedule_callback_owner(struct device *dev, 510523ded71SAlan Stern void (*func)(struct device *), struct module *owner) 511d9a9cdfbSAlan Stern { 512d9a9cdfbSAlan Stern return sysfs_schedule_callback(&dev->kobj, 513523ded71SAlan Stern (void (*)(void *)) func, dev, owner); 514d9a9cdfbSAlan Stern } 515523ded71SAlan Stern EXPORT_SYMBOL_GPL(device_schedule_callback_owner); 516d9a9cdfbSAlan Stern 51734bb61f9SJames Bottomley static void klist_children_get(struct klist_node *n) 51834bb61f9SJames Bottomley { 519f791b8c8SGreg Kroah-Hartman struct device_private *p = to_device_private_parent(n); 520f791b8c8SGreg Kroah-Hartman struct device *dev = p->device; 52134bb61f9SJames Bottomley 52234bb61f9SJames Bottomley get_device(dev); 52334bb61f9SJames Bottomley } 52434bb61f9SJames Bottomley 52534bb61f9SJames Bottomley static void klist_children_put(struct klist_node *n) 52634bb61f9SJames Bottomley { 527f791b8c8SGreg Kroah-Hartman struct device_private *p = to_device_private_parent(n); 528f791b8c8SGreg Kroah-Hartman struct device *dev = p->device; 52934bb61f9SJames Bottomley 53034bb61f9SJames Bottomley put_device(dev); 53134bb61f9SJames Bottomley } 53234bb61f9SJames Bottomley 5331da177e4SLinus Torvalds /** 5341da177e4SLinus Torvalds * device_initialize - init device structure. 5351da177e4SLinus Torvalds * @dev: device. 5361da177e4SLinus Torvalds * 5375739411aSCornelia Huck * This prepares the device for use by other layers by initializing 5385739411aSCornelia Huck * its fields. 5391da177e4SLinus Torvalds * It is the first half of device_register(), if called by 5405739411aSCornelia Huck * that function, though it can also be called separately, so one 5415739411aSCornelia Huck * may use @dev's fields. In particular, get_device()/put_device() 5425739411aSCornelia Huck * may be used for reference counting of @dev after calling this 5435739411aSCornelia Huck * function. 5445739411aSCornelia Huck * 5455739411aSCornelia Huck * NOTE: Use put_device() to give up your reference instead of freeing 5465739411aSCornelia Huck * @dev directly once you have called this function. 5471da177e4SLinus Torvalds */ 5481da177e4SLinus Torvalds void device_initialize(struct device *dev) 5491da177e4SLinus Torvalds { 550881c6cfdSGreg Kroah-Hartman dev->kobj.kset = devices_kset; 551f9cb074bSGreg Kroah-Hartman kobject_init(&dev->kobj, &device_ktype); 5521da177e4SLinus Torvalds INIT_LIST_HEAD(&dev->dma_pools); 553af70316aSmochel@digitalimplant.org init_MUTEX(&dev->sem); 5549ac7849eSTejun Heo spin_lock_init(&dev->devres_lock); 5559ac7849eSTejun Heo INIT_LIST_HEAD(&dev->devres_head); 5560ac85241SDavid Brownell device_init_wakeup(dev, 0); 5573b98aeafSAlan Stern device_pm_init(dev); 55887348136SChristoph Hellwig set_dev_node(dev, -1); 5591da177e4SLinus Torvalds } 5601da177e4SLinus Torvalds 56140fa5422SGreg Kroah-Hartman #ifdef CONFIG_SYSFS_DEPRECATED 562c744aeaeSCornelia Huck static struct kobject *get_device_parent(struct device *dev, 563c744aeaeSCornelia Huck struct device *parent) 56440fa5422SGreg Kroah-Hartman { 565da231fd5SKay Sievers /* class devices without a parent live in /sys/class/<classname>/ */ 5663eb215deSDmitry Torokhov if (dev->class && (!parent || parent->class != dev->class)) 5671fbfee6cSGreg Kroah-Hartman return &dev->class->p->class_subsys.kobj; 568da231fd5SKay Sievers /* all other devices keep their parent */ 56940fa5422SGreg Kroah-Hartman else if (parent) 570c744aeaeSCornelia Huck return &parent->kobj; 57140fa5422SGreg Kroah-Hartman 572c744aeaeSCornelia Huck return NULL; 57340fa5422SGreg Kroah-Hartman } 574da231fd5SKay Sievers 575da231fd5SKay Sievers static inline void cleanup_device_parent(struct device *dev) {} 57663b6971aSCornelia Huck static inline void cleanup_glue_dir(struct device *dev, 57763b6971aSCornelia Huck struct kobject *glue_dir) {} 57840fa5422SGreg Kroah-Hartman #else 579c744aeaeSCornelia Huck static struct kobject *virtual_device_parent(struct device *dev) 580f0ee61a6SGreg Kroah-Hartman { 581f0ee61a6SGreg Kroah-Hartman static struct kobject *virtual_dir = NULL; 582f0ee61a6SGreg Kroah-Hartman 583f0ee61a6SGreg Kroah-Hartman if (!virtual_dir) 5844ff6abffSGreg Kroah-Hartman virtual_dir = kobject_create_and_add("virtual", 585881c6cfdSGreg Kroah-Hartman &devices_kset->kobj); 586f0ee61a6SGreg Kroah-Hartman 58786406245SKay Sievers return virtual_dir; 588f0ee61a6SGreg Kroah-Hartman } 589f0ee61a6SGreg Kroah-Hartman 590c744aeaeSCornelia Huck static struct kobject *get_device_parent(struct device *dev, 591c744aeaeSCornelia Huck struct device *parent) 59240fa5422SGreg Kroah-Hartman { 59343968d2fSGreg Kroah-Hartman int retval; 59443968d2fSGreg Kroah-Hartman 59586406245SKay Sievers if (dev->class) { 59686406245SKay Sievers struct kobject *kobj = NULL; 59786406245SKay Sievers struct kobject *parent_kobj; 59886406245SKay Sievers struct kobject *k; 59986406245SKay Sievers 60086406245SKay Sievers /* 60186406245SKay Sievers * If we have no parent, we live in "virtual". 6020f4dafc0SKay Sievers * Class-devices with a non class-device as parent, live 6030f4dafc0SKay Sievers * in a "glue" directory to prevent namespace collisions. 60486406245SKay Sievers */ 60586406245SKay Sievers if (parent == NULL) 60686406245SKay Sievers parent_kobj = virtual_device_parent(dev); 60786406245SKay Sievers else if (parent->class) 60886406245SKay Sievers return &parent->kobj; 60986406245SKay Sievers else 61086406245SKay Sievers parent_kobj = &parent->kobj; 61186406245SKay Sievers 61286406245SKay Sievers /* find our class-directory at the parent and reference it */ 6137c71448bSGreg Kroah-Hartman spin_lock(&dev->class->p->class_dirs.list_lock); 6147c71448bSGreg Kroah-Hartman list_for_each_entry(k, &dev->class->p->class_dirs.list, entry) 61586406245SKay Sievers if (k->parent == parent_kobj) { 61686406245SKay Sievers kobj = kobject_get(k); 61786406245SKay Sievers break; 61886406245SKay Sievers } 6197c71448bSGreg Kroah-Hartman spin_unlock(&dev->class->p->class_dirs.list_lock); 62086406245SKay Sievers if (kobj) 62186406245SKay Sievers return kobj; 62286406245SKay Sievers 62386406245SKay Sievers /* or create a new class-directory at the parent device */ 62443968d2fSGreg Kroah-Hartman k = kobject_create(); 62543968d2fSGreg Kroah-Hartman if (!k) 62643968d2fSGreg Kroah-Hartman return NULL; 6277c71448bSGreg Kroah-Hartman k->kset = &dev->class->p->class_dirs; 628b2d6db58SGreg Kroah-Hartman retval = kobject_add(k, parent_kobj, "%s", dev->class->name); 62943968d2fSGreg Kroah-Hartman if (retval < 0) { 63043968d2fSGreg Kroah-Hartman kobject_put(k); 63143968d2fSGreg Kroah-Hartman return NULL; 63243968d2fSGreg Kroah-Hartman } 6330f4dafc0SKay Sievers /* do not emit an uevent for this simple "glue" directory */ 63443968d2fSGreg Kroah-Hartman return k; 63586406245SKay Sievers } 63686406245SKay Sievers 63786406245SKay Sievers if (parent) 638c744aeaeSCornelia Huck return &parent->kobj; 639c744aeaeSCornelia Huck return NULL; 640c744aeaeSCornelia Huck } 641da231fd5SKay Sievers 64263b6971aSCornelia Huck static void cleanup_glue_dir(struct device *dev, struct kobject *glue_dir) 643da231fd5SKay Sievers { 6440f4dafc0SKay Sievers /* see if we live in a "glue" directory */ 645c1fe539aSCornelia Huck if (!glue_dir || !dev->class || 6467c71448bSGreg Kroah-Hartman glue_dir->kset != &dev->class->p->class_dirs) 647da231fd5SKay Sievers return; 648da231fd5SKay Sievers 6490f4dafc0SKay Sievers kobject_put(glue_dir); 650da231fd5SKay Sievers } 65163b6971aSCornelia Huck 65263b6971aSCornelia Huck static void cleanup_device_parent(struct device *dev) 65363b6971aSCornelia Huck { 65463b6971aSCornelia Huck cleanup_glue_dir(dev, dev->kobj.parent); 65563b6971aSCornelia Huck } 656c744aeaeSCornelia Huck #endif 65786406245SKay Sievers 65863b6971aSCornelia Huck static void setup_parent(struct device *dev, struct device *parent) 659c744aeaeSCornelia Huck { 660c744aeaeSCornelia Huck struct kobject *kobj; 661c744aeaeSCornelia Huck kobj = get_device_parent(dev, parent); 662c744aeaeSCornelia Huck if (kobj) 663c744aeaeSCornelia Huck dev->kobj.parent = kobj; 66440fa5422SGreg Kroah-Hartman } 66540fa5422SGreg Kroah-Hartman 6662ee97cafSCornelia Huck static int device_add_class_symlinks(struct device *dev) 6672ee97cafSCornelia Huck { 6682ee97cafSCornelia Huck int error; 6692ee97cafSCornelia Huck 6702ee97cafSCornelia Huck if (!dev->class) 6712ee97cafSCornelia Huck return 0; 672da231fd5SKay Sievers 6731fbfee6cSGreg Kroah-Hartman error = sysfs_create_link(&dev->kobj, 6741fbfee6cSGreg Kroah-Hartman &dev->class->p->class_subsys.kobj, 6752ee97cafSCornelia Huck "subsystem"); 6762ee97cafSCornelia Huck if (error) 6772ee97cafSCornelia Huck goto out; 678da231fd5SKay Sievers 679da231fd5SKay Sievers #ifdef CONFIG_SYSFS_DEPRECATED 680da231fd5SKay Sievers /* stacked class devices need a symlink in the class directory */ 6811fbfee6cSGreg Kroah-Hartman if (dev->kobj.parent != &dev->class->p->class_subsys.kobj && 6824e886c29SGreg Kroah-Hartman device_is_not_partition(dev)) { 6831fbfee6cSGreg Kroah-Hartman error = sysfs_create_link(&dev->class->p->class_subsys.kobj, 6841e0b2cf9SKay Sievers &dev->kobj, dev_name(dev)); 6852ee97cafSCornelia Huck if (error) 6862ee97cafSCornelia Huck goto out_subsys; 6872ee97cafSCornelia Huck } 688da231fd5SKay Sievers 6894e886c29SGreg Kroah-Hartman if (dev->parent && device_is_not_partition(dev)) { 6904f01a757SDmitry Torokhov struct device *parent = dev->parent; 6914f01a757SDmitry Torokhov char *class_name; 6924f01a757SDmitry Torokhov 6934f01a757SDmitry Torokhov /* 694da231fd5SKay Sievers * stacked class devices have the 'device' link 695da231fd5SKay Sievers * pointing to the bus device instead of the parent 6964f01a757SDmitry Torokhov */ 6974f01a757SDmitry Torokhov while (parent->class && !parent->bus && parent->parent) 6984f01a757SDmitry Torokhov parent = parent->parent; 6994f01a757SDmitry Torokhov 7004f01a757SDmitry Torokhov error = sysfs_create_link(&dev->kobj, 7014f01a757SDmitry Torokhov &parent->kobj, 7022ee97cafSCornelia Huck "device"); 7032ee97cafSCornelia Huck if (error) 7042ee97cafSCornelia Huck goto out_busid; 7054f01a757SDmitry Torokhov 7064f01a757SDmitry Torokhov class_name = make_class_name(dev->class->name, 7072ee97cafSCornelia Huck &dev->kobj); 7082ee97cafSCornelia Huck if (class_name) 7092ee97cafSCornelia Huck error = sysfs_create_link(&dev->parent->kobj, 7102ee97cafSCornelia Huck &dev->kobj, class_name); 7112ee97cafSCornelia Huck kfree(class_name); 7122ee97cafSCornelia Huck if (error) 7132ee97cafSCornelia Huck goto out_device; 7142ee97cafSCornelia Huck } 715da231fd5SKay Sievers return 0; 716da231fd5SKay Sievers 717da231fd5SKay Sievers out_device: 7184e886c29SGreg Kroah-Hartman if (dev->parent && device_is_not_partition(dev)) 719da231fd5SKay Sievers sysfs_remove_link(&dev->kobj, "device"); 720da231fd5SKay Sievers out_busid: 7211fbfee6cSGreg Kroah-Hartman if (dev->kobj.parent != &dev->class->p->class_subsys.kobj && 7224e886c29SGreg Kroah-Hartman device_is_not_partition(dev)) 7231fbfee6cSGreg Kroah-Hartman sysfs_remove_link(&dev->class->p->class_subsys.kobj, 7241e0b2cf9SKay Sievers dev_name(dev)); 7254f01a757SDmitry Torokhov #else 726da231fd5SKay Sievers /* link in the class directory pointing to the device */ 7271fbfee6cSGreg Kroah-Hartman error = sysfs_create_link(&dev->class->p->class_subsys.kobj, 7281e0b2cf9SKay Sievers &dev->kobj, dev_name(dev)); 729da231fd5SKay Sievers if (error) 730da231fd5SKay Sievers goto out_subsys; 731da231fd5SKay Sievers 7324e886c29SGreg Kroah-Hartman if (dev->parent && device_is_not_partition(dev)) { 7334f01a757SDmitry Torokhov error = sysfs_create_link(&dev->kobj, &dev->parent->kobj, 7344f01a757SDmitry Torokhov "device"); 7354f01a757SDmitry Torokhov if (error) 7364f01a757SDmitry Torokhov goto out_busid; 7372ee97cafSCornelia Huck } 7382ee97cafSCornelia Huck return 0; 7392ee97cafSCornelia Huck 7402ee97cafSCornelia Huck out_busid: 7411e0b2cf9SKay Sievers sysfs_remove_link(&dev->class->p->class_subsys.kobj, dev_name(dev)); 742da231fd5SKay Sievers #endif 743da231fd5SKay Sievers 7442ee97cafSCornelia Huck out_subsys: 7452ee97cafSCornelia Huck sysfs_remove_link(&dev->kobj, "subsystem"); 7462ee97cafSCornelia Huck out: 7472ee97cafSCornelia Huck return error; 7482ee97cafSCornelia Huck } 7492ee97cafSCornelia Huck 7502ee97cafSCornelia Huck static void device_remove_class_symlinks(struct device *dev) 7512ee97cafSCornelia Huck { 7522ee97cafSCornelia Huck if (!dev->class) 7532ee97cafSCornelia Huck return; 754da231fd5SKay Sievers 7552ee97cafSCornelia Huck #ifdef CONFIG_SYSFS_DEPRECATED 7564e886c29SGreg Kroah-Hartman if (dev->parent && device_is_not_partition(dev)) { 7572ee97cafSCornelia Huck char *class_name; 7582ee97cafSCornelia Huck 7592ee97cafSCornelia Huck class_name = make_class_name(dev->class->name, &dev->kobj); 7602ee97cafSCornelia Huck if (class_name) { 7612ee97cafSCornelia Huck sysfs_remove_link(&dev->parent->kobj, class_name); 7622ee97cafSCornelia Huck kfree(class_name); 7632ee97cafSCornelia Huck } 7642ee97cafSCornelia Huck sysfs_remove_link(&dev->kobj, "device"); 7652ee97cafSCornelia Huck } 766da231fd5SKay Sievers 7671fbfee6cSGreg Kroah-Hartman if (dev->kobj.parent != &dev->class->p->class_subsys.kobj && 7684e886c29SGreg Kroah-Hartman device_is_not_partition(dev)) 7691fbfee6cSGreg Kroah-Hartman sysfs_remove_link(&dev->class->p->class_subsys.kobj, 7701e0b2cf9SKay Sievers dev_name(dev)); 771da231fd5SKay Sievers #else 7724e886c29SGreg Kroah-Hartman if (dev->parent && device_is_not_partition(dev)) 773da231fd5SKay Sievers sysfs_remove_link(&dev->kobj, "device"); 774da231fd5SKay Sievers 7751e0b2cf9SKay Sievers sysfs_remove_link(&dev->class->p->class_subsys.kobj, dev_name(dev)); 776da231fd5SKay Sievers #endif 777da231fd5SKay Sievers 7782ee97cafSCornelia Huck sysfs_remove_link(&dev->kobj, "subsystem"); 7792ee97cafSCornelia Huck } 7802ee97cafSCornelia Huck 7811da177e4SLinus Torvalds /** 782413c239fSStephen Rothwell * dev_set_name - set a device name 783413c239fSStephen Rothwell * @dev: device 78446232366SRandy Dunlap * @fmt: format string for the device's name 785413c239fSStephen Rothwell */ 786413c239fSStephen Rothwell int dev_set_name(struct device *dev, const char *fmt, ...) 787413c239fSStephen Rothwell { 788413c239fSStephen Rothwell va_list vargs; 7891fa5ae85SKay Sievers int err; 790413c239fSStephen Rothwell 791413c239fSStephen Rothwell va_start(vargs, fmt); 7921fa5ae85SKay Sievers err = kobject_set_name_vargs(&dev->kobj, fmt, vargs); 793413c239fSStephen Rothwell va_end(vargs); 7941fa5ae85SKay Sievers return err; 795413c239fSStephen Rothwell } 796413c239fSStephen Rothwell EXPORT_SYMBOL_GPL(dev_set_name); 797413c239fSStephen Rothwell 798413c239fSStephen Rothwell /** 799e105b8bfSDan Williams * device_to_dev_kobj - select a /sys/dev/ directory for the device 800e105b8bfSDan Williams * @dev: device 801e105b8bfSDan Williams * 802e105b8bfSDan Williams * By default we select char/ for new entries. Setting class->dev_obj 803e105b8bfSDan Williams * to NULL prevents an entry from being created. class->dev_kobj must 804e105b8bfSDan Williams * be set (or cleared) before any devices are registered to the class 805e105b8bfSDan Williams * otherwise device_create_sys_dev_entry() and 806e105b8bfSDan Williams * device_remove_sys_dev_entry() will disagree about the the presence 807e105b8bfSDan Williams * of the link. 808e105b8bfSDan Williams */ 809e105b8bfSDan Williams static struct kobject *device_to_dev_kobj(struct device *dev) 810e105b8bfSDan Williams { 811e105b8bfSDan Williams struct kobject *kobj; 812e105b8bfSDan Williams 813e105b8bfSDan Williams if (dev->class) 814e105b8bfSDan Williams kobj = dev->class->dev_kobj; 815e105b8bfSDan Williams else 816e105b8bfSDan Williams kobj = sysfs_dev_char_kobj; 817e105b8bfSDan Williams 818e105b8bfSDan Williams return kobj; 819e105b8bfSDan Williams } 820e105b8bfSDan Williams 821e105b8bfSDan Williams static int device_create_sys_dev_entry(struct device *dev) 822e105b8bfSDan Williams { 823e105b8bfSDan Williams struct kobject *kobj = device_to_dev_kobj(dev); 824e105b8bfSDan Williams int error = 0; 825e105b8bfSDan Williams char devt_str[15]; 826e105b8bfSDan Williams 827e105b8bfSDan Williams if (kobj) { 828e105b8bfSDan Williams format_dev_t(devt_str, dev->devt); 829e105b8bfSDan Williams error = sysfs_create_link(kobj, &dev->kobj, devt_str); 830e105b8bfSDan Williams } 831e105b8bfSDan Williams 832e105b8bfSDan Williams return error; 833e105b8bfSDan Williams } 834e105b8bfSDan Williams 835e105b8bfSDan Williams static void device_remove_sys_dev_entry(struct device *dev) 836e105b8bfSDan Williams { 837e105b8bfSDan Williams struct kobject *kobj = device_to_dev_kobj(dev); 838e105b8bfSDan Williams char devt_str[15]; 839e105b8bfSDan Williams 840e105b8bfSDan Williams if (kobj) { 841e105b8bfSDan Williams format_dev_t(devt_str, dev->devt); 842e105b8bfSDan Williams sysfs_remove_link(kobj, devt_str); 843e105b8bfSDan Williams } 844e105b8bfSDan Williams } 845e105b8bfSDan Williams 846e105b8bfSDan Williams /** 8471da177e4SLinus Torvalds * device_add - add device to device hierarchy. 8481da177e4SLinus Torvalds * @dev: device. 8491da177e4SLinus Torvalds * 8501da177e4SLinus Torvalds * This is part 2 of device_register(), though may be called 8511da177e4SLinus Torvalds * separately _iff_ device_initialize() has been called separately. 8521da177e4SLinus Torvalds * 8535739411aSCornelia Huck * This adds @dev to the kobject hierarchy via kobject_add(), adds it 8541da177e4SLinus Torvalds * to the global and sibling lists for the device, then 8551da177e4SLinus Torvalds * adds it to the other relevant subsystems of the driver model. 8565739411aSCornelia Huck * 8575739411aSCornelia Huck * NOTE: _Never_ directly free @dev after calling this function, even 8585739411aSCornelia Huck * if it returned an error! Always use put_device() to give up your 8595739411aSCornelia Huck * reference instead. 8601da177e4SLinus Torvalds */ 8611da177e4SLinus Torvalds int device_add(struct device *dev) 8621da177e4SLinus Torvalds { 8631da177e4SLinus Torvalds struct device *parent = NULL; 864c47ed219SGreg Kroah-Hartman struct class_interface *class_intf; 865c906a48aSGreg Kroah-Hartman int error = -EINVAL; 866775b64d2SRafael J. Wysocki 8671da177e4SLinus Torvalds dev = get_device(dev); 868c906a48aSGreg Kroah-Hartman if (!dev) 869c906a48aSGreg Kroah-Hartman goto done; 870c906a48aSGreg Kroah-Hartman 871fb069a5dSGreg Kroah-Hartman dev->p = kzalloc(sizeof(*dev->p), GFP_KERNEL); 872fb069a5dSGreg Kroah-Hartman if (!dev->p) { 873fb069a5dSGreg Kroah-Hartman error = -ENOMEM; 874fb069a5dSGreg Kroah-Hartman goto done; 875fb069a5dSGreg Kroah-Hartman } 876fb069a5dSGreg Kroah-Hartman dev->p->device = dev; 877f791b8c8SGreg Kroah-Hartman klist_init(&dev->p->klist_children, klist_children_get, 878f791b8c8SGreg Kroah-Hartman klist_children_put); 879fb069a5dSGreg Kroah-Hartman 8801fa5ae85SKay Sievers /* 8811fa5ae85SKay Sievers * for statically allocated devices, which should all be converted 8821fa5ae85SKay Sievers * some day, we need to initialize the name. We prevent reading back 8831fa5ae85SKay Sievers * the name, and force the use of dev_name() 8841fa5ae85SKay Sievers */ 8851fa5ae85SKay Sievers if (dev->init_name) { 886acc0e90fSGreg Kroah-Hartman dev_set_name(dev, "%s", dev->init_name); 8871fa5ae85SKay Sievers dev->init_name = NULL; 8881fa5ae85SKay Sievers } 889c906a48aSGreg Kroah-Hartman 8901fa5ae85SKay Sievers if (!dev_name(dev)) 8915c8563d7SKay Sievers goto name_error; 8921da177e4SLinus Torvalds 8931e0b2cf9SKay Sievers pr_debug("device: '%s': %s\n", dev_name(dev), __func__); 894c205ef48SGreg Kroah-Hartman 8951da177e4SLinus Torvalds parent = get_device(dev->parent); 89663b6971aSCornelia Huck setup_parent(dev, parent); 8971da177e4SLinus Torvalds 8980d358f22SYinghai Lu /* use parent numa_node */ 8990d358f22SYinghai Lu if (parent) 9000d358f22SYinghai Lu set_dev_node(dev, dev_to_node(parent)); 9010d358f22SYinghai Lu 9021da177e4SLinus Torvalds /* first, register with generic layer. */ 9038a577ffcSKay Sievers /* we require the name to be set before, and pass NULL */ 9048a577ffcSKay Sievers error = kobject_add(&dev->kobj, dev->kobj.parent, NULL); 90540fa5422SGreg Kroah-Hartman if (error) 9061da177e4SLinus Torvalds goto Error; 907a7fd6706SKay Sievers 90837022644SBrian Walsh /* notify platform of device entry */ 90937022644SBrian Walsh if (platform_notify) 91037022644SBrian Walsh platform_notify(dev); 91137022644SBrian Walsh 912ad6a1e1cSTejun Heo error = device_create_file(dev, &uevent_attr); 913a306eea4SCornelia Huck if (error) 914a306eea4SCornelia Huck goto attrError; 915a7fd6706SKay Sievers 91623681e47SGreg Kroah-Hartman if (MAJOR(dev->devt)) { 917ad6a1e1cSTejun Heo error = device_create_file(dev, &devt_attr); 918ad6a1e1cSTejun Heo if (error) 919a306eea4SCornelia Huck goto ueventattrError; 920e105b8bfSDan Williams 921e105b8bfSDan Williams error = device_create_sys_dev_entry(dev); 922e105b8bfSDan Williams if (error) 923e105b8bfSDan Williams goto devtattrError; 92423681e47SGreg Kroah-Hartman } 92523681e47SGreg Kroah-Hartman 9262ee97cafSCornelia Huck error = device_add_class_symlinks(dev); 9272ee97cafSCornelia Huck if (error) 9282ee97cafSCornelia Huck goto SymlinkError; 929dc0afa83SCornelia Huck error = device_add_attrs(dev); 930dc0afa83SCornelia Huck if (error) 9312620efefSGreg Kroah-Hartman goto AttrsError; 932dc0afa83SCornelia Huck error = bus_add_device(dev); 933dc0afa83SCornelia Huck if (error) 9341da177e4SLinus Torvalds goto BusError; 9353b98aeafSAlan Stern error = dpm_sysfs_add(dev); 93657eee3d2SRafael J. Wysocki if (error) 9373b98aeafSAlan Stern goto DPMError; 9383b98aeafSAlan Stern device_pm_add(dev); 939ec0676eeSAlan Stern 940ec0676eeSAlan Stern /* Notify clients of device addition. This call must come 941ec0676eeSAlan Stern * after dpm_sysf_add() and before kobject_uevent(). 942ec0676eeSAlan Stern */ 943ec0676eeSAlan Stern if (dev->bus) 944ec0676eeSAlan Stern blocking_notifier_call_chain(&dev->bus->p->bus_notifier, 945ec0676eeSAlan Stern BUS_NOTIFY_ADD_DEVICE, dev); 946ec0676eeSAlan Stern 94753877d06SKay Sievers kobject_uevent(&dev->kobj, KOBJ_ADD); 9482023c610SAlan Stern bus_probe_device(dev); 9491da177e4SLinus Torvalds if (parent) 950f791b8c8SGreg Kroah-Hartman klist_add_tail(&dev->p->knode_parent, 951f791b8c8SGreg Kroah-Hartman &parent->p->klist_children); 9521da177e4SLinus Torvalds 9535d9fd169SGreg Kroah-Hartman if (dev->class) { 954f75b1c60SDave Young mutex_lock(&dev->class->p->class_mutex); 955c47ed219SGreg Kroah-Hartman /* tie the class to the device */ 9565a3ceb86STejun Heo klist_add_tail(&dev->knode_class, 9575a3ceb86STejun Heo &dev->class->p->class_devices); 958c47ed219SGreg Kroah-Hartman 959c47ed219SGreg Kroah-Hartman /* notify any interfaces that the device is here */ 960184f1f77SGreg Kroah-Hartman list_for_each_entry(class_intf, 961184f1f77SGreg Kroah-Hartman &dev->class->p->class_interfaces, node) 962c47ed219SGreg Kroah-Hartman if (class_intf->add_dev) 963c47ed219SGreg Kroah-Hartman class_intf->add_dev(dev, class_intf); 964f75b1c60SDave Young mutex_unlock(&dev->class->p->class_mutex); 9655d9fd169SGreg Kroah-Hartman } 966c906a48aSGreg Kroah-Hartman done: 9671da177e4SLinus Torvalds put_device(dev); 9681da177e4SLinus Torvalds return error; 9693b98aeafSAlan Stern DPMError: 97057eee3d2SRafael J. Wysocki bus_remove_device(dev); 97157eee3d2SRafael J. Wysocki BusError: 9722620efefSGreg Kroah-Hartman device_remove_attrs(dev); 9732620efefSGreg Kroah-Hartman AttrsError: 9742ee97cafSCornelia Huck device_remove_class_symlinks(dev); 9752ee97cafSCornelia Huck SymlinkError: 976ad6a1e1cSTejun Heo if (MAJOR(dev->devt)) 977e105b8bfSDan Williams device_remove_sys_dev_entry(dev); 978e105b8bfSDan Williams devtattrError: 979e105b8bfSDan Williams if (MAJOR(dev->devt)) 980ad6a1e1cSTejun Heo device_remove_file(dev, &devt_attr); 981a306eea4SCornelia Huck ueventattrError: 982ad6a1e1cSTejun Heo device_remove_file(dev, &uevent_attr); 98323681e47SGreg Kroah-Hartman attrError: 984312c004dSKay Sievers kobject_uevent(&dev->kobj, KOBJ_REMOVE); 9851da177e4SLinus Torvalds kobject_del(&dev->kobj); 9861da177e4SLinus Torvalds Error: 98763b6971aSCornelia Huck cleanup_device_parent(dev); 9881da177e4SLinus Torvalds if (parent) 9891da177e4SLinus Torvalds put_device(parent); 9905c8563d7SKay Sievers name_error: 9915c8563d7SKay Sievers kfree(dev->p); 9925c8563d7SKay Sievers dev->p = NULL; 993c906a48aSGreg Kroah-Hartman goto done; 9941da177e4SLinus Torvalds } 9951da177e4SLinus Torvalds 9961da177e4SLinus Torvalds /** 9971da177e4SLinus Torvalds * device_register - register a device with the system. 9981da177e4SLinus Torvalds * @dev: pointer to the device structure 9991da177e4SLinus Torvalds * 10001da177e4SLinus Torvalds * This happens in two clean steps - initialize the device 10011da177e4SLinus Torvalds * and add it to the system. The two steps can be called 10021da177e4SLinus Torvalds * separately, but this is the easiest and most common. 10031da177e4SLinus Torvalds * I.e. you should only call the two helpers separately if 10041da177e4SLinus Torvalds * have a clearly defined need to use and refcount the device 10051da177e4SLinus Torvalds * before it is added to the hierarchy. 10065739411aSCornelia Huck * 10075739411aSCornelia Huck * NOTE: _Never_ directly free @dev after calling this function, even 10085739411aSCornelia Huck * if it returned an error! Always use put_device() to give up the 10095739411aSCornelia Huck * reference initialized in this function instead. 10101da177e4SLinus Torvalds */ 10111da177e4SLinus Torvalds int device_register(struct device *dev) 10121da177e4SLinus Torvalds { 10131da177e4SLinus Torvalds device_initialize(dev); 10141da177e4SLinus Torvalds return device_add(dev); 10151da177e4SLinus Torvalds } 10161da177e4SLinus Torvalds 10171da177e4SLinus Torvalds /** 10181da177e4SLinus Torvalds * get_device - increment reference count for device. 10191da177e4SLinus Torvalds * @dev: device. 10201da177e4SLinus Torvalds * 10211da177e4SLinus Torvalds * This simply forwards the call to kobject_get(), though 10221da177e4SLinus Torvalds * we do take care to provide for the case that we get a NULL 10231da177e4SLinus Torvalds * pointer passed in. 10241da177e4SLinus Torvalds */ 10251da177e4SLinus Torvalds struct device *get_device(struct device *dev) 10261da177e4SLinus Torvalds { 10271da177e4SLinus Torvalds return dev ? to_dev(kobject_get(&dev->kobj)) : NULL; 10281da177e4SLinus Torvalds } 10291da177e4SLinus Torvalds 10301da177e4SLinus Torvalds /** 10311da177e4SLinus Torvalds * put_device - decrement reference count. 10321da177e4SLinus Torvalds * @dev: device in question. 10331da177e4SLinus Torvalds */ 10341da177e4SLinus Torvalds void put_device(struct device *dev) 10351da177e4SLinus Torvalds { 1036edfaa7c3SKay Sievers /* might_sleep(); */ 10371da177e4SLinus Torvalds if (dev) 10381da177e4SLinus Torvalds kobject_put(&dev->kobj); 10391da177e4SLinus Torvalds } 10401da177e4SLinus Torvalds 10411da177e4SLinus Torvalds /** 10421da177e4SLinus Torvalds * device_del - delete device from system. 10431da177e4SLinus Torvalds * @dev: device. 10441da177e4SLinus Torvalds * 10451da177e4SLinus Torvalds * This is the first part of the device unregistration 10461da177e4SLinus Torvalds * sequence. This removes the device from the lists we control 10471da177e4SLinus Torvalds * from here, has it removed from the other driver model 10481da177e4SLinus Torvalds * subsystems it was added to in device_add(), and removes it 10491da177e4SLinus Torvalds * from the kobject hierarchy. 10501da177e4SLinus Torvalds * 10511da177e4SLinus Torvalds * NOTE: this should be called manually _iff_ device_add() was 10521da177e4SLinus Torvalds * also called manually. 10531da177e4SLinus Torvalds */ 10541da177e4SLinus Torvalds void device_del(struct device *dev) 10551da177e4SLinus Torvalds { 10561da177e4SLinus Torvalds struct device *parent = dev->parent; 1057c47ed219SGreg Kroah-Hartman struct class_interface *class_intf; 10581da177e4SLinus Torvalds 1059ec0676eeSAlan Stern /* Notify clients of device removal. This call must come 1060ec0676eeSAlan Stern * before dpm_sysfs_remove(). 1061ec0676eeSAlan Stern */ 1062ec0676eeSAlan Stern if (dev->bus) 1063ec0676eeSAlan Stern blocking_notifier_call_chain(&dev->bus->p->bus_notifier, 1064ec0676eeSAlan Stern BUS_NOTIFY_DEL_DEVICE, dev); 1065775b64d2SRafael J. Wysocki device_pm_remove(dev); 10663b98aeafSAlan Stern dpm_sysfs_remove(dev); 10671da177e4SLinus Torvalds if (parent) 1068f791b8c8SGreg Kroah-Hartman klist_del(&dev->p->knode_parent); 1069e105b8bfSDan Williams if (MAJOR(dev->devt)) { 1070e105b8bfSDan Williams device_remove_sys_dev_entry(dev); 1071ad6a1e1cSTejun Heo device_remove_file(dev, &devt_attr); 1072e105b8bfSDan Williams } 1073b9d9c82bSKay Sievers if (dev->class) { 1074da231fd5SKay Sievers device_remove_class_symlinks(dev); 107599ef3ef8SKay Sievers 1076f75b1c60SDave Young mutex_lock(&dev->class->p->class_mutex); 1077c47ed219SGreg Kroah-Hartman /* notify any interfaces that the device is now gone */ 1078184f1f77SGreg Kroah-Hartman list_for_each_entry(class_intf, 1079184f1f77SGreg Kroah-Hartman &dev->class->p->class_interfaces, node) 1080c47ed219SGreg Kroah-Hartman if (class_intf->remove_dev) 1081c47ed219SGreg Kroah-Hartman class_intf->remove_dev(dev, class_intf); 1082c47ed219SGreg Kroah-Hartman /* remove the device from the class list */ 10835a3ceb86STejun Heo klist_del(&dev->knode_class); 1084f75b1c60SDave Young mutex_unlock(&dev->class->p->class_mutex); 1085b9d9c82bSKay Sievers } 1086ad6a1e1cSTejun Heo device_remove_file(dev, &uevent_attr); 10872620efefSGreg Kroah-Hartman device_remove_attrs(dev); 108828953533SBenjamin Herrenschmidt bus_remove_device(dev); 10891da177e4SLinus Torvalds 10902f8d16a9STejun Heo /* 10912f8d16a9STejun Heo * Some platform devices are driven without driver attached 10922f8d16a9STejun Heo * and managed resources may have been acquired. Make sure 10932f8d16a9STejun Heo * all resources are released. 10942f8d16a9STejun Heo */ 10952f8d16a9STejun Heo devres_release_all(dev); 10962f8d16a9STejun Heo 10971da177e4SLinus Torvalds /* Notify the platform of the removal, in case they 10981da177e4SLinus Torvalds * need to do anything... 10991da177e4SLinus Torvalds */ 11001da177e4SLinus Torvalds if (platform_notify_remove) 11011da177e4SLinus Torvalds platform_notify_remove(dev); 1102312c004dSKay Sievers kobject_uevent(&dev->kobj, KOBJ_REMOVE); 1103da231fd5SKay Sievers cleanup_device_parent(dev); 11041da177e4SLinus Torvalds kobject_del(&dev->kobj); 11051da177e4SLinus Torvalds put_device(parent); 11061da177e4SLinus Torvalds } 11071da177e4SLinus Torvalds 11081da177e4SLinus Torvalds /** 11091da177e4SLinus Torvalds * device_unregister - unregister device from system. 11101da177e4SLinus Torvalds * @dev: device going away. 11111da177e4SLinus Torvalds * 11121da177e4SLinus Torvalds * We do this in two parts, like we do device_register(). First, 11131da177e4SLinus Torvalds * we remove it from all the subsystems with device_del(), then 11141da177e4SLinus Torvalds * we decrement the reference count via put_device(). If that 11151da177e4SLinus Torvalds * is the final reference count, the device will be cleaned up 11161da177e4SLinus Torvalds * via device_release() above. Otherwise, the structure will 11171da177e4SLinus Torvalds * stick around until the final reference to the device is dropped. 11181da177e4SLinus Torvalds */ 11191da177e4SLinus Torvalds void device_unregister(struct device *dev) 11201da177e4SLinus Torvalds { 11211e0b2cf9SKay Sievers pr_debug("device: '%s': %s\n", dev_name(dev), __func__); 11221da177e4SLinus Torvalds device_del(dev); 11231da177e4SLinus Torvalds put_device(dev); 11241da177e4SLinus Torvalds } 11251da177e4SLinus Torvalds 112636239577Smochel@digitalimplant.org static struct device *next_device(struct klist_iter *i) 112736239577Smochel@digitalimplant.org { 112836239577Smochel@digitalimplant.org struct klist_node *n = klist_next(i); 1129f791b8c8SGreg Kroah-Hartman struct device *dev = NULL; 1130f791b8c8SGreg Kroah-Hartman struct device_private *p; 1131f791b8c8SGreg Kroah-Hartman 1132f791b8c8SGreg Kroah-Hartman if (n) { 1133f791b8c8SGreg Kroah-Hartman p = to_device_private_parent(n); 1134f791b8c8SGreg Kroah-Hartman dev = p->device; 1135f791b8c8SGreg Kroah-Hartman } 1136f791b8c8SGreg Kroah-Hartman return dev; 113736239577Smochel@digitalimplant.org } 113836239577Smochel@digitalimplant.org 11391da177e4SLinus Torvalds /** 11406fcf53acSKay Sievers * device_get_nodename - path of device node file 11416fcf53acSKay Sievers * @dev: device 11426fcf53acSKay Sievers * @tmp: possibly allocated string 11436fcf53acSKay Sievers * 11446fcf53acSKay Sievers * Return the relative path of a possible device node. 11456fcf53acSKay Sievers * Non-default names may need to allocate a memory to compose 11466fcf53acSKay Sievers * a name. This memory is returned in tmp and needs to be 11476fcf53acSKay Sievers * freed by the caller. 11486fcf53acSKay Sievers */ 11496fcf53acSKay Sievers const char *device_get_nodename(struct device *dev, const char **tmp) 11506fcf53acSKay Sievers { 11516fcf53acSKay Sievers char *s; 11526fcf53acSKay Sievers 11536fcf53acSKay Sievers *tmp = NULL; 11546fcf53acSKay Sievers 11556fcf53acSKay Sievers /* the device type may provide a specific name */ 11566fcf53acSKay Sievers if (dev->type && dev->type->nodename) 11576fcf53acSKay Sievers *tmp = dev->type->nodename(dev); 11586fcf53acSKay Sievers if (*tmp) 11596fcf53acSKay Sievers return *tmp; 11606fcf53acSKay Sievers 11616fcf53acSKay Sievers /* the class may provide a specific name */ 11626fcf53acSKay Sievers if (dev->class && dev->class->nodename) 11636fcf53acSKay Sievers *tmp = dev->class->nodename(dev); 11646fcf53acSKay Sievers if (*tmp) 11656fcf53acSKay Sievers return *tmp; 11666fcf53acSKay Sievers 11676fcf53acSKay Sievers /* return name without allocation, tmp == NULL */ 11686fcf53acSKay Sievers if (strchr(dev_name(dev), '!') == NULL) 11696fcf53acSKay Sievers return dev_name(dev); 11706fcf53acSKay Sievers 11716fcf53acSKay Sievers /* replace '!' in the name with '/' */ 11726fcf53acSKay Sievers *tmp = kstrdup(dev_name(dev), GFP_KERNEL); 11736fcf53acSKay Sievers if (!*tmp) 11746fcf53acSKay Sievers return NULL; 11756fcf53acSKay Sievers while ((s = strchr(*tmp, '!'))) 11766fcf53acSKay Sievers s[0] = '/'; 11776fcf53acSKay Sievers return *tmp; 11786fcf53acSKay Sievers } 11796fcf53acSKay Sievers 11806fcf53acSKay Sievers /** 11811da177e4SLinus Torvalds * device_for_each_child - device child iterator. 1182c41455fbSRandy Dunlap * @parent: parent struct device. 11831da177e4SLinus Torvalds * @data: data for the callback. 11841da177e4SLinus Torvalds * @fn: function to be called for each device. 11851da177e4SLinus Torvalds * 1186c41455fbSRandy Dunlap * Iterate over @parent's child devices, and call @fn for each, 11871da177e4SLinus Torvalds * passing it @data. 11881da177e4SLinus Torvalds * 11891da177e4SLinus Torvalds * We check the return of @fn each time. If it returns anything 11901da177e4SLinus Torvalds * other than 0, we break out and return that value. 11911da177e4SLinus Torvalds */ 119236239577Smochel@digitalimplant.org int device_for_each_child(struct device *parent, void *data, 11934a3ad20cSGreg Kroah-Hartman int (*fn)(struct device *dev, void *data)) 11941da177e4SLinus Torvalds { 119536239577Smochel@digitalimplant.org struct klist_iter i; 11961da177e4SLinus Torvalds struct device *child; 11971da177e4SLinus Torvalds int error = 0; 11981da177e4SLinus Torvalds 1199014c90dbSGreg Kroah-Hartman if (!parent->p) 1200014c90dbSGreg Kroah-Hartman return 0; 1201014c90dbSGreg Kroah-Hartman 1202f791b8c8SGreg Kroah-Hartman klist_iter_init(&parent->p->klist_children, &i); 120336239577Smochel@digitalimplant.org while ((child = next_device(&i)) && !error) 120436239577Smochel@digitalimplant.org error = fn(child, data); 120536239577Smochel@digitalimplant.org klist_iter_exit(&i); 12061da177e4SLinus Torvalds return error; 12071da177e4SLinus Torvalds } 12081da177e4SLinus Torvalds 12095ab69981SCornelia Huck /** 12105ab69981SCornelia Huck * device_find_child - device iterator for locating a particular device. 12115ab69981SCornelia Huck * @parent: parent struct device 12125ab69981SCornelia Huck * @data: Data to pass to match function 12135ab69981SCornelia Huck * @match: Callback function to check device 12145ab69981SCornelia Huck * 12155ab69981SCornelia Huck * This is similar to the device_for_each_child() function above, but it 12165ab69981SCornelia Huck * returns a reference to a device that is 'found' for later use, as 12175ab69981SCornelia Huck * determined by the @match callback. 12185ab69981SCornelia Huck * 12195ab69981SCornelia Huck * The callback should return 0 if the device doesn't match and non-zero 12205ab69981SCornelia Huck * if it does. If the callback returns non-zero and a reference to the 12215ab69981SCornelia Huck * current device can be obtained, this function will return to the caller 12225ab69981SCornelia Huck * and not iterate over any more devices. 12235ab69981SCornelia Huck */ 12245ab69981SCornelia Huck struct device *device_find_child(struct device *parent, void *data, 12254a3ad20cSGreg Kroah-Hartman int (*match)(struct device *dev, void *data)) 12265ab69981SCornelia Huck { 12275ab69981SCornelia Huck struct klist_iter i; 12285ab69981SCornelia Huck struct device *child; 12295ab69981SCornelia Huck 12305ab69981SCornelia Huck if (!parent) 12315ab69981SCornelia Huck return NULL; 12325ab69981SCornelia Huck 1233f791b8c8SGreg Kroah-Hartman klist_iter_init(&parent->p->klist_children, &i); 12345ab69981SCornelia Huck while ((child = next_device(&i))) 12355ab69981SCornelia Huck if (match(child, data) && get_device(child)) 12365ab69981SCornelia Huck break; 12375ab69981SCornelia Huck klist_iter_exit(&i); 12385ab69981SCornelia Huck return child; 12395ab69981SCornelia Huck } 12405ab69981SCornelia Huck 12411da177e4SLinus Torvalds int __init devices_init(void) 12421da177e4SLinus Torvalds { 1243881c6cfdSGreg Kroah-Hartman devices_kset = kset_create_and_add("devices", &device_uevent_ops, NULL); 1244881c6cfdSGreg Kroah-Hartman if (!devices_kset) 1245881c6cfdSGreg Kroah-Hartman return -ENOMEM; 1246e105b8bfSDan Williams dev_kobj = kobject_create_and_add("dev", NULL); 1247e105b8bfSDan Williams if (!dev_kobj) 1248e105b8bfSDan Williams goto dev_kobj_err; 1249e105b8bfSDan Williams sysfs_dev_block_kobj = kobject_create_and_add("block", dev_kobj); 1250e105b8bfSDan Williams if (!sysfs_dev_block_kobj) 1251e105b8bfSDan Williams goto block_kobj_err; 1252e105b8bfSDan Williams sysfs_dev_char_kobj = kobject_create_and_add("char", dev_kobj); 1253e105b8bfSDan Williams if (!sysfs_dev_char_kobj) 1254e105b8bfSDan Williams goto char_kobj_err; 1255e105b8bfSDan Williams 1256881c6cfdSGreg Kroah-Hartman return 0; 1257e105b8bfSDan Williams 1258e105b8bfSDan Williams char_kobj_err: 1259e105b8bfSDan Williams kobject_put(sysfs_dev_block_kobj); 1260e105b8bfSDan Williams block_kobj_err: 1261e105b8bfSDan Williams kobject_put(dev_kobj); 1262e105b8bfSDan Williams dev_kobj_err: 1263e105b8bfSDan Williams kset_unregister(devices_kset); 1264e105b8bfSDan Williams return -ENOMEM; 12651da177e4SLinus Torvalds } 12661da177e4SLinus Torvalds 12671da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(device_for_each_child); 12685ab69981SCornelia Huck EXPORT_SYMBOL_GPL(device_find_child); 12691da177e4SLinus Torvalds 12701da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(device_initialize); 12711da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(device_add); 12721da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(device_register); 12731da177e4SLinus Torvalds 12741da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(device_del); 12751da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(device_unregister); 12761da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(get_device); 12771da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(put_device); 12781da177e4SLinus Torvalds 12791da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(device_create_file); 12801da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(device_remove_file); 128123681e47SGreg Kroah-Hartman 12820aa0dc41SMark McLoughlin struct root_device 12830aa0dc41SMark McLoughlin { 12840aa0dc41SMark McLoughlin struct device dev; 12850aa0dc41SMark McLoughlin struct module *owner; 12860aa0dc41SMark McLoughlin }; 12870aa0dc41SMark McLoughlin 12880aa0dc41SMark McLoughlin #define to_root_device(dev) container_of(dev, struct root_device, dev) 12890aa0dc41SMark McLoughlin 12900aa0dc41SMark McLoughlin static void root_device_release(struct device *dev) 12910aa0dc41SMark McLoughlin { 12920aa0dc41SMark McLoughlin kfree(to_root_device(dev)); 12930aa0dc41SMark McLoughlin } 12940aa0dc41SMark McLoughlin 12950aa0dc41SMark McLoughlin /** 12960aa0dc41SMark McLoughlin * __root_device_register - allocate and register a root device 12970aa0dc41SMark McLoughlin * @name: root device name 12980aa0dc41SMark McLoughlin * @owner: owner module of the root device, usually THIS_MODULE 12990aa0dc41SMark McLoughlin * 13000aa0dc41SMark McLoughlin * This function allocates a root device and registers it 13010aa0dc41SMark McLoughlin * using device_register(). In order to free the returned 13020aa0dc41SMark McLoughlin * device, use root_device_unregister(). 13030aa0dc41SMark McLoughlin * 13040aa0dc41SMark McLoughlin * Root devices are dummy devices which allow other devices 13050aa0dc41SMark McLoughlin * to be grouped under /sys/devices. Use this function to 13060aa0dc41SMark McLoughlin * allocate a root device and then use it as the parent of 13070aa0dc41SMark McLoughlin * any device which should appear under /sys/devices/{name} 13080aa0dc41SMark McLoughlin * 13090aa0dc41SMark McLoughlin * The /sys/devices/{name} directory will also contain a 13100aa0dc41SMark McLoughlin * 'module' symlink which points to the @owner directory 13110aa0dc41SMark McLoughlin * in sysfs. 13120aa0dc41SMark McLoughlin * 13130aa0dc41SMark McLoughlin * Note: You probably want to use root_device_register(). 13140aa0dc41SMark McLoughlin */ 13150aa0dc41SMark McLoughlin struct device *__root_device_register(const char *name, struct module *owner) 13160aa0dc41SMark McLoughlin { 13170aa0dc41SMark McLoughlin struct root_device *root; 13180aa0dc41SMark McLoughlin int err = -ENOMEM; 13190aa0dc41SMark McLoughlin 13200aa0dc41SMark McLoughlin root = kzalloc(sizeof(struct root_device), GFP_KERNEL); 13210aa0dc41SMark McLoughlin if (!root) 13220aa0dc41SMark McLoughlin return ERR_PTR(err); 13230aa0dc41SMark McLoughlin 1324acc0e90fSGreg Kroah-Hartman err = dev_set_name(&root->dev, "%s", name); 13250aa0dc41SMark McLoughlin if (err) { 13260aa0dc41SMark McLoughlin kfree(root); 13270aa0dc41SMark McLoughlin return ERR_PTR(err); 13280aa0dc41SMark McLoughlin } 13290aa0dc41SMark McLoughlin 13300aa0dc41SMark McLoughlin root->dev.release = root_device_release; 13310aa0dc41SMark McLoughlin 13320aa0dc41SMark McLoughlin err = device_register(&root->dev); 13330aa0dc41SMark McLoughlin if (err) { 13340aa0dc41SMark McLoughlin put_device(&root->dev); 13350aa0dc41SMark McLoughlin return ERR_PTR(err); 13360aa0dc41SMark McLoughlin } 13370aa0dc41SMark McLoughlin 13380aa0dc41SMark McLoughlin #ifdef CONFIG_MODULE /* gotta find a "cleaner" way to do this */ 13390aa0dc41SMark McLoughlin if (owner) { 13400aa0dc41SMark McLoughlin struct module_kobject *mk = &owner->mkobj; 13410aa0dc41SMark McLoughlin 13420aa0dc41SMark McLoughlin err = sysfs_create_link(&root->dev.kobj, &mk->kobj, "module"); 13430aa0dc41SMark McLoughlin if (err) { 13440aa0dc41SMark McLoughlin device_unregister(&root->dev); 13450aa0dc41SMark McLoughlin return ERR_PTR(err); 13460aa0dc41SMark McLoughlin } 13470aa0dc41SMark McLoughlin root->owner = owner; 13480aa0dc41SMark McLoughlin } 13490aa0dc41SMark McLoughlin #endif 13500aa0dc41SMark McLoughlin 13510aa0dc41SMark McLoughlin return &root->dev; 13520aa0dc41SMark McLoughlin } 13530aa0dc41SMark McLoughlin EXPORT_SYMBOL_GPL(__root_device_register); 13540aa0dc41SMark McLoughlin 13550aa0dc41SMark McLoughlin /** 13560aa0dc41SMark McLoughlin * root_device_unregister - unregister and free a root device 13577cbcf225SRandy Dunlap * @dev: device going away 13580aa0dc41SMark McLoughlin * 13590aa0dc41SMark McLoughlin * This function unregisters and cleans up a device that was created by 13600aa0dc41SMark McLoughlin * root_device_register(). 13610aa0dc41SMark McLoughlin */ 13620aa0dc41SMark McLoughlin void root_device_unregister(struct device *dev) 13630aa0dc41SMark McLoughlin { 13640aa0dc41SMark McLoughlin struct root_device *root = to_root_device(dev); 13650aa0dc41SMark McLoughlin 13660aa0dc41SMark McLoughlin if (root->owner) 13670aa0dc41SMark McLoughlin sysfs_remove_link(&root->dev.kobj, "module"); 13680aa0dc41SMark McLoughlin 13690aa0dc41SMark McLoughlin device_unregister(dev); 13700aa0dc41SMark McLoughlin } 13710aa0dc41SMark McLoughlin EXPORT_SYMBOL_GPL(root_device_unregister); 13720aa0dc41SMark McLoughlin 137323681e47SGreg Kroah-Hartman 137423681e47SGreg Kroah-Hartman static void device_create_release(struct device *dev) 137523681e47SGreg Kroah-Hartman { 13761e0b2cf9SKay Sievers pr_debug("device: '%s': %s\n", dev_name(dev), __func__); 137723681e47SGreg Kroah-Hartman kfree(dev); 137823681e47SGreg Kroah-Hartman } 137923681e47SGreg Kroah-Hartman 138023681e47SGreg Kroah-Hartman /** 13818882b394SGreg Kroah-Hartman * device_create_vargs - creates a device and registers it with sysfs 13828882b394SGreg Kroah-Hartman * @class: pointer to the struct class that this device should be registered to 13838882b394SGreg Kroah-Hartman * @parent: pointer to the parent struct device of this new device, if any 13848882b394SGreg Kroah-Hartman * @devt: the dev_t for the char device to be added 13858882b394SGreg Kroah-Hartman * @drvdata: the data to be added to the device for callbacks 13868882b394SGreg Kroah-Hartman * @fmt: string for the device's name 13878882b394SGreg Kroah-Hartman * @args: va_list for the device's name 13888882b394SGreg Kroah-Hartman * 13898882b394SGreg Kroah-Hartman * This function can be used by char device classes. A struct device 13908882b394SGreg Kroah-Hartman * will be created in sysfs, registered to the specified class. 13918882b394SGreg Kroah-Hartman * 13928882b394SGreg Kroah-Hartman * A "dev" file will be created, showing the dev_t for the device, if 13938882b394SGreg Kroah-Hartman * the dev_t is not 0,0. 13948882b394SGreg Kroah-Hartman * If a pointer to a parent struct device is passed in, the newly created 13958882b394SGreg Kroah-Hartman * struct device will be a child of that device in sysfs. 13968882b394SGreg Kroah-Hartman * The pointer to the struct device will be returned from the call. 13978882b394SGreg Kroah-Hartman * Any further sysfs files that might be required can be created using this 13988882b394SGreg Kroah-Hartman * pointer. 13998882b394SGreg Kroah-Hartman * 14008882b394SGreg Kroah-Hartman * Note: the struct class passed to this function must have previously 14018882b394SGreg Kroah-Hartman * been created with a call to class_create(). 14028882b394SGreg Kroah-Hartman */ 14038882b394SGreg Kroah-Hartman struct device *device_create_vargs(struct class *class, struct device *parent, 14048882b394SGreg Kroah-Hartman dev_t devt, void *drvdata, const char *fmt, 14058882b394SGreg Kroah-Hartman va_list args) 14068882b394SGreg Kroah-Hartman { 14078882b394SGreg Kroah-Hartman struct device *dev = NULL; 14088882b394SGreg Kroah-Hartman int retval = -ENODEV; 14098882b394SGreg Kroah-Hartman 14108882b394SGreg Kroah-Hartman if (class == NULL || IS_ERR(class)) 14118882b394SGreg Kroah-Hartman goto error; 14128882b394SGreg Kroah-Hartman 14138882b394SGreg Kroah-Hartman dev = kzalloc(sizeof(*dev), GFP_KERNEL); 14148882b394SGreg Kroah-Hartman if (!dev) { 14158882b394SGreg Kroah-Hartman retval = -ENOMEM; 14168882b394SGreg Kroah-Hartman goto error; 14178882b394SGreg Kroah-Hartman } 14188882b394SGreg Kroah-Hartman 14198882b394SGreg Kroah-Hartman dev->devt = devt; 14208882b394SGreg Kroah-Hartman dev->class = class; 14218882b394SGreg Kroah-Hartman dev->parent = parent; 14228882b394SGreg Kroah-Hartman dev->release = device_create_release; 14238882b394SGreg Kroah-Hartman dev_set_drvdata(dev, drvdata); 14248882b394SGreg Kroah-Hartman 14251fa5ae85SKay Sievers retval = kobject_set_name_vargs(&dev->kobj, fmt, args); 14261fa5ae85SKay Sievers if (retval) 14271fa5ae85SKay Sievers goto error; 14281fa5ae85SKay Sievers 14298882b394SGreg Kroah-Hartman retval = device_register(dev); 14308882b394SGreg Kroah-Hartman if (retval) 14318882b394SGreg Kroah-Hartman goto error; 14328882b394SGreg Kroah-Hartman 14338882b394SGreg Kroah-Hartman return dev; 14348882b394SGreg Kroah-Hartman 14358882b394SGreg Kroah-Hartman error: 1436286661b3SCornelia Huck put_device(dev); 14378882b394SGreg Kroah-Hartman return ERR_PTR(retval); 14388882b394SGreg Kroah-Hartman } 14398882b394SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(device_create_vargs); 14408882b394SGreg Kroah-Hartman 14418882b394SGreg Kroah-Hartman /** 14424e106739SGreg Kroah-Hartman * device_create - creates a device and registers it with sysfs 14438882b394SGreg Kroah-Hartman * @class: pointer to the struct class that this device should be registered to 14448882b394SGreg Kroah-Hartman * @parent: pointer to the parent struct device of this new device, if any 14458882b394SGreg Kroah-Hartman * @devt: the dev_t for the char device to be added 14468882b394SGreg Kroah-Hartman * @drvdata: the data to be added to the device for callbacks 14478882b394SGreg Kroah-Hartman * @fmt: string for the device's name 14488882b394SGreg Kroah-Hartman * 14498882b394SGreg Kroah-Hartman * This function can be used by char device classes. A struct device 14508882b394SGreg Kroah-Hartman * will be created in sysfs, registered to the specified class. 14518882b394SGreg Kroah-Hartman * 14528882b394SGreg Kroah-Hartman * A "dev" file will be created, showing the dev_t for the device, if 14538882b394SGreg Kroah-Hartman * the dev_t is not 0,0. 14548882b394SGreg Kroah-Hartman * If a pointer to a parent struct device is passed in, the newly created 14558882b394SGreg Kroah-Hartman * struct device will be a child of that device in sysfs. 14568882b394SGreg Kroah-Hartman * The pointer to the struct device will be returned from the call. 14578882b394SGreg Kroah-Hartman * Any further sysfs files that might be required can be created using this 14588882b394SGreg Kroah-Hartman * pointer. 14598882b394SGreg Kroah-Hartman * 14608882b394SGreg Kroah-Hartman * Note: the struct class passed to this function must have previously 14618882b394SGreg Kroah-Hartman * been created with a call to class_create(). 14628882b394SGreg Kroah-Hartman */ 14634e106739SGreg Kroah-Hartman struct device *device_create(struct class *class, struct device *parent, 14644e106739SGreg Kroah-Hartman dev_t devt, void *drvdata, const char *fmt, ...) 14658882b394SGreg Kroah-Hartman { 14668882b394SGreg Kroah-Hartman va_list vargs; 14678882b394SGreg Kroah-Hartman struct device *dev; 14688882b394SGreg Kroah-Hartman 14698882b394SGreg Kroah-Hartman va_start(vargs, fmt); 14708882b394SGreg Kroah-Hartman dev = device_create_vargs(class, parent, devt, drvdata, fmt, vargs); 14718882b394SGreg Kroah-Hartman va_end(vargs); 14728882b394SGreg Kroah-Hartman return dev; 14738882b394SGreg Kroah-Hartman } 14744e106739SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(device_create); 14758882b394SGreg Kroah-Hartman 1476cd35449bSDave Young static int __match_devt(struct device *dev, void *data) 147723681e47SGreg Kroah-Hartman { 1478cd35449bSDave Young dev_t *devt = data; 147923681e47SGreg Kroah-Hartman 1480cd35449bSDave Young return dev->devt == *devt; 1481775b64d2SRafael J. Wysocki } 148223681e47SGreg Kroah-Hartman 1483775b64d2SRafael J. Wysocki /** 1484775b64d2SRafael J. Wysocki * device_destroy - removes a device that was created with device_create() 1485775b64d2SRafael J. Wysocki * @class: pointer to the struct class that this device was registered with 1486775b64d2SRafael J. Wysocki * @devt: the dev_t of the device that was previously registered 1487775b64d2SRafael J. Wysocki * 1488775b64d2SRafael J. Wysocki * This call unregisters and cleans up a device that was created with a 1489775b64d2SRafael J. Wysocki * call to device_create(). 1490775b64d2SRafael J. Wysocki */ 1491775b64d2SRafael J. Wysocki void device_destroy(struct class *class, dev_t devt) 1492775b64d2SRafael J. Wysocki { 1493775b64d2SRafael J. Wysocki struct device *dev; 1494775b64d2SRafael J. Wysocki 1495695794aeSGreg Kroah-Hartman dev = class_find_device(class, NULL, &devt, __match_devt); 1496cd35449bSDave Young if (dev) { 1497cd35449bSDave Young put_device(dev); 149823681e47SGreg Kroah-Hartman device_unregister(dev); 149923681e47SGreg Kroah-Hartman } 1500cd35449bSDave Young } 150123681e47SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(device_destroy); 1502a2de48caSGreg Kroah-Hartman 1503a2de48caSGreg Kroah-Hartman /** 1504a2de48caSGreg Kroah-Hartman * device_rename - renames a device 1505a2de48caSGreg Kroah-Hartman * @dev: the pointer to the struct device to be renamed 1506a2de48caSGreg Kroah-Hartman * @new_name: the new name of the device 1507030c1d2bSEric W. Biederman * 1508030c1d2bSEric W. Biederman * It is the responsibility of the caller to provide mutual 1509030c1d2bSEric W. Biederman * exclusion between two different calls of device_rename 1510030c1d2bSEric W. Biederman * on the same device to ensure that new_name is valid and 1511030c1d2bSEric W. Biederman * won't conflict with other devices. 1512a2de48caSGreg Kroah-Hartman */ 1513a2de48caSGreg Kroah-Hartman int device_rename(struct device *dev, char *new_name) 1514a2de48caSGreg Kroah-Hartman { 1515a2de48caSGreg Kroah-Hartman char *old_class_name = NULL; 1516a2de48caSGreg Kroah-Hartman char *new_class_name = NULL; 15172ee97cafSCornelia Huck char *old_device_name = NULL; 1518a2de48caSGreg Kroah-Hartman int error; 1519a2de48caSGreg Kroah-Hartman 1520a2de48caSGreg Kroah-Hartman dev = get_device(dev); 1521a2de48caSGreg Kroah-Hartman if (!dev) 1522a2de48caSGreg Kroah-Hartman return -EINVAL; 1523a2de48caSGreg Kroah-Hartman 15241e0b2cf9SKay Sievers pr_debug("device: '%s': %s: renaming to '%s'\n", dev_name(dev), 15252b3a302aSHarvey Harrison __func__, new_name); 1526a2de48caSGreg Kroah-Hartman 152799ef3ef8SKay Sievers #ifdef CONFIG_SYSFS_DEPRECATED 1528a2de48caSGreg Kroah-Hartman if ((dev->class) && (dev->parent)) 1529a2de48caSGreg Kroah-Hartman old_class_name = make_class_name(dev->class->name, &dev->kobj); 153099ef3ef8SKay Sievers #endif 1531a2de48caSGreg Kroah-Hartman 15321fa5ae85SKay Sievers old_device_name = kstrdup(dev_name(dev), GFP_KERNEL); 15332ee97cafSCornelia Huck if (!old_device_name) { 1534952ab431SJesper Juhl error = -ENOMEM; 15352ee97cafSCornelia Huck goto out; 1536952ab431SJesper Juhl } 1537a2de48caSGreg Kroah-Hartman 1538a2de48caSGreg Kroah-Hartman error = kobject_rename(&dev->kobj, new_name); 15391fa5ae85SKay Sievers if (error) 15402ee97cafSCornelia Huck goto out; 1541a2de48caSGreg Kroah-Hartman 154299ef3ef8SKay Sievers #ifdef CONFIG_SYSFS_DEPRECATED 1543a2de48caSGreg Kroah-Hartman if (old_class_name) { 1544a2de48caSGreg Kroah-Hartman new_class_name = make_class_name(dev->class->name, &dev->kobj); 1545a2de48caSGreg Kroah-Hartman if (new_class_name) { 154636ce6dadSCornelia Huck error = sysfs_create_link_nowarn(&dev->parent->kobj, 154736ce6dadSCornelia Huck &dev->kobj, 154836ce6dadSCornelia Huck new_class_name); 15492ee97cafSCornelia Huck if (error) 15502ee97cafSCornelia Huck goto out; 1551a2de48caSGreg Kroah-Hartman sysfs_remove_link(&dev->parent->kobj, old_class_name); 1552a2de48caSGreg Kroah-Hartman } 1553a2de48caSGreg Kroah-Hartman } 155460b8cabdSKay Sievers #else 1555a2de48caSGreg Kroah-Hartman if (dev->class) { 155636ce6dadSCornelia Huck error = sysfs_create_link_nowarn(&dev->class->p->class_subsys.kobj, 15571e0b2cf9SKay Sievers &dev->kobj, dev_name(dev)); 15580599ad53SStephen Hemminger if (error) 15590599ad53SStephen Hemminger goto out; 15601fbfee6cSGreg Kroah-Hartman sysfs_remove_link(&dev->class->p->class_subsys.kobj, 15617c71448bSGreg Kroah-Hartman old_device_name); 15622ee97cafSCornelia Huck } 156360b8cabdSKay Sievers #endif 156460b8cabdSKay Sievers 15652ee97cafSCornelia Huck out: 1566a2de48caSGreg Kroah-Hartman put_device(dev); 1567a2de48caSGreg Kroah-Hartman 1568a2de48caSGreg Kroah-Hartman kfree(new_class_name); 1569952ab431SJesper Juhl kfree(old_class_name); 15702ee97cafSCornelia Huck kfree(old_device_name); 1571a2de48caSGreg Kroah-Hartman 1572a2de48caSGreg Kroah-Hartman return error; 1573a2de48caSGreg Kroah-Hartman } 1574a2807dbcSJohannes Berg EXPORT_SYMBOL_GPL(device_rename); 15758a82472fSCornelia Huck 15768a82472fSCornelia Huck static int device_move_class_links(struct device *dev, 15778a82472fSCornelia Huck struct device *old_parent, 15788a82472fSCornelia Huck struct device *new_parent) 15798a82472fSCornelia Huck { 1580f7f3461dSGreg Kroah-Hartman int error = 0; 15818a82472fSCornelia Huck #ifdef CONFIG_SYSFS_DEPRECATED 15828a82472fSCornelia Huck char *class_name; 15838a82472fSCornelia Huck 15848a82472fSCornelia Huck class_name = make_class_name(dev->class->name, &dev->kobj); 15858a82472fSCornelia Huck if (!class_name) { 1586cb360bbfSCornelia Huck error = -ENOMEM; 15878a82472fSCornelia Huck goto out; 15888a82472fSCornelia Huck } 15898a82472fSCornelia Huck if (old_parent) { 15908a82472fSCornelia Huck sysfs_remove_link(&dev->kobj, "device"); 15918a82472fSCornelia Huck sysfs_remove_link(&old_parent->kobj, class_name); 15928a82472fSCornelia Huck } 1593c744aeaeSCornelia Huck if (new_parent) { 1594c744aeaeSCornelia Huck error = sysfs_create_link(&dev->kobj, &new_parent->kobj, 1595c744aeaeSCornelia Huck "device"); 15968a82472fSCornelia Huck if (error) 15978a82472fSCornelia Huck goto out; 1598c744aeaeSCornelia Huck error = sysfs_create_link(&new_parent->kobj, &dev->kobj, 1599c744aeaeSCornelia Huck class_name); 16008a82472fSCornelia Huck if (error) 16018a82472fSCornelia Huck sysfs_remove_link(&dev->kobj, "device"); 16024a3ad20cSGreg Kroah-Hartman } else 1603c744aeaeSCornelia Huck error = 0; 16048a82472fSCornelia Huck out: 16058a82472fSCornelia Huck kfree(class_name); 16068a82472fSCornelia Huck return error; 16078a82472fSCornelia Huck #else 1608f7f3461dSGreg Kroah-Hartman if (old_parent) 1609f7f3461dSGreg Kroah-Hartman sysfs_remove_link(&dev->kobj, "device"); 1610f7f3461dSGreg Kroah-Hartman if (new_parent) 1611f7f3461dSGreg Kroah-Hartman error = sysfs_create_link(&dev->kobj, &new_parent->kobj, 1612f7f3461dSGreg Kroah-Hartman "device"); 1613f7f3461dSGreg Kroah-Hartman return error; 16148a82472fSCornelia Huck #endif 16158a82472fSCornelia Huck } 16168a82472fSCornelia Huck 16178a82472fSCornelia Huck /** 16188a82472fSCornelia Huck * device_move - moves a device to a new parent 16198a82472fSCornelia Huck * @dev: the pointer to the struct device to be moved 1620c744aeaeSCornelia Huck * @new_parent: the new parent of the device (can by NULL) 1621ffa6a705SCornelia Huck * @dpm_order: how to reorder the dpm_list 16228a82472fSCornelia Huck */ 1623ffa6a705SCornelia Huck int device_move(struct device *dev, struct device *new_parent, 1624ffa6a705SCornelia Huck enum dpm_order dpm_order) 16258a82472fSCornelia Huck { 16268a82472fSCornelia Huck int error; 16278a82472fSCornelia Huck struct device *old_parent; 1628c744aeaeSCornelia Huck struct kobject *new_parent_kobj; 16298a82472fSCornelia Huck 16308a82472fSCornelia Huck dev = get_device(dev); 16318a82472fSCornelia Huck if (!dev) 16328a82472fSCornelia Huck return -EINVAL; 16338a82472fSCornelia Huck 1634ffa6a705SCornelia Huck device_pm_lock(); 16358a82472fSCornelia Huck new_parent = get_device(new_parent); 1636c744aeaeSCornelia Huck new_parent_kobj = get_device_parent(dev, new_parent); 163763b6971aSCornelia Huck 16381e0b2cf9SKay Sievers pr_debug("device: '%s': %s: moving to '%s'\n", dev_name(dev), 16391e0b2cf9SKay Sievers __func__, new_parent ? dev_name(new_parent) : "<NULL>"); 1640c744aeaeSCornelia Huck error = kobject_move(&dev->kobj, new_parent_kobj); 16418a82472fSCornelia Huck if (error) { 164263b6971aSCornelia Huck cleanup_glue_dir(dev, new_parent_kobj); 16438a82472fSCornelia Huck put_device(new_parent); 16448a82472fSCornelia Huck goto out; 16458a82472fSCornelia Huck } 16468a82472fSCornelia Huck old_parent = dev->parent; 16478a82472fSCornelia Huck dev->parent = new_parent; 16488a82472fSCornelia Huck if (old_parent) 1649f791b8c8SGreg Kroah-Hartman klist_remove(&dev->p->knode_parent); 16500d358f22SYinghai Lu if (new_parent) { 1651f791b8c8SGreg Kroah-Hartman klist_add_tail(&dev->p->knode_parent, 1652f791b8c8SGreg Kroah-Hartman &new_parent->p->klist_children); 16530d358f22SYinghai Lu set_dev_node(dev, dev_to_node(new_parent)); 16540d358f22SYinghai Lu } 16550d358f22SYinghai Lu 16568a82472fSCornelia Huck if (!dev->class) 16578a82472fSCornelia Huck goto out_put; 16588a82472fSCornelia Huck error = device_move_class_links(dev, old_parent, new_parent); 16598a82472fSCornelia Huck if (error) { 16608a82472fSCornelia Huck /* We ignore errors on cleanup since we're hosed anyway... */ 16618a82472fSCornelia Huck device_move_class_links(dev, new_parent, old_parent); 16628a82472fSCornelia Huck if (!kobject_move(&dev->kobj, &old_parent->kobj)) { 1663c744aeaeSCornelia Huck if (new_parent) 1664f791b8c8SGreg Kroah-Hartman klist_remove(&dev->p->knode_parent); 16650d358f22SYinghai Lu dev->parent = old_parent; 16660d358f22SYinghai Lu if (old_parent) { 1667f791b8c8SGreg Kroah-Hartman klist_add_tail(&dev->p->knode_parent, 1668f791b8c8SGreg Kroah-Hartman &old_parent->p->klist_children); 16690d358f22SYinghai Lu set_dev_node(dev, dev_to_node(old_parent)); 16700d358f22SYinghai Lu } 16718a82472fSCornelia Huck } 167263b6971aSCornelia Huck cleanup_glue_dir(dev, new_parent_kobj); 16738a82472fSCornelia Huck put_device(new_parent); 16748a82472fSCornelia Huck goto out; 16758a82472fSCornelia Huck } 1676ffa6a705SCornelia Huck switch (dpm_order) { 1677ffa6a705SCornelia Huck case DPM_ORDER_NONE: 1678ffa6a705SCornelia Huck break; 1679ffa6a705SCornelia Huck case DPM_ORDER_DEV_AFTER_PARENT: 1680ffa6a705SCornelia Huck device_pm_move_after(dev, new_parent); 1681ffa6a705SCornelia Huck break; 1682ffa6a705SCornelia Huck case DPM_ORDER_PARENT_BEFORE_DEV: 1683ffa6a705SCornelia Huck device_pm_move_before(new_parent, dev); 1684ffa6a705SCornelia Huck break; 1685ffa6a705SCornelia Huck case DPM_ORDER_DEV_LAST: 1686ffa6a705SCornelia Huck device_pm_move_last(dev); 1687ffa6a705SCornelia Huck break; 1688ffa6a705SCornelia Huck } 16898a82472fSCornelia Huck out_put: 16908a82472fSCornelia Huck put_device(old_parent); 16918a82472fSCornelia Huck out: 1692ffa6a705SCornelia Huck device_pm_unlock(); 16938a82472fSCornelia Huck put_device(dev); 16948a82472fSCornelia Huck return error; 16958a82472fSCornelia Huck } 16968a82472fSCornelia Huck EXPORT_SYMBOL_GPL(device_move); 169737b0c020SGreg Kroah-Hartman 169837b0c020SGreg Kroah-Hartman /** 169937b0c020SGreg Kroah-Hartman * device_shutdown - call ->shutdown() on each device to shutdown. 170037b0c020SGreg Kroah-Hartman */ 170137b0c020SGreg Kroah-Hartman void device_shutdown(void) 170237b0c020SGreg Kroah-Hartman { 170337b0c020SGreg Kroah-Hartman struct device *dev, *devn; 170437b0c020SGreg Kroah-Hartman 170537b0c020SGreg Kroah-Hartman list_for_each_entry_safe_reverse(dev, devn, &devices_kset->list, 170637b0c020SGreg Kroah-Hartman kobj.entry) { 170737b0c020SGreg Kroah-Hartman if (dev->bus && dev->bus->shutdown) { 170837b0c020SGreg Kroah-Hartman dev_dbg(dev, "shutdown\n"); 170937b0c020SGreg Kroah-Hartman dev->bus->shutdown(dev); 171037b0c020SGreg Kroah-Hartman } else if (dev->driver && dev->driver->shutdown) { 171137b0c020SGreg Kroah-Hartman dev_dbg(dev, "shutdown\n"); 171237b0c020SGreg Kroah-Hartman dev->driver->shutdown(dev); 171337b0c020SGreg Kroah-Hartman } 171437b0c020SGreg Kroah-Hartman } 1715e105b8bfSDan Williams kobject_put(sysfs_dev_char_kobj); 1716e105b8bfSDan Williams kobject_put(sysfs_dev_block_kobj); 1717e105b8bfSDan Williams kobject_put(dev_kobj); 1718401097eaSShaohua Li async_synchronize_full(); 171937b0c020SGreg Kroah-Hartman } 1720