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; 169e454cea2SKay Sievers mode_t mode = 0; 1706fcf53acSKay Sievers 1717eff2e7aSKay Sievers add_uevent_var(env, "MAJOR=%u", MAJOR(dev->devt)); 1727eff2e7aSKay Sievers add_uevent_var(env, "MINOR=%u", MINOR(dev->devt)); 173e454cea2SKay Sievers name = device_get_devnode(dev, &mode, &tmp); 1746fcf53acSKay Sievers if (name) { 1756fcf53acSKay Sievers add_uevent_var(env, "DEVNAME=%s", name); 1766fcf53acSKay Sievers kfree(tmp); 177e454cea2SKay Sievers if (mode) 178e454cea2SKay Sievers add_uevent_var(env, "DEVMODE=%#o", mode & 0777); 1796fcf53acSKay Sievers } 18023681e47SGreg Kroah-Hartman } 18123681e47SGreg Kroah-Hartman 182414264f9SKay Sievers if (dev->type && dev->type->name) 1837eff2e7aSKay Sievers add_uevent_var(env, "DEVTYPE=%s", dev->type->name); 184414264f9SKay Sievers 185239378f1SKay Sievers if (dev->driver) 1867eff2e7aSKay Sievers add_uevent_var(env, "DRIVER=%s", dev->driver->name); 187239378f1SKay Sievers 188a87cb2acSKay Sievers #ifdef CONFIG_SYSFS_DEPRECATED 189239378f1SKay Sievers if (dev->class) { 190239378f1SKay Sievers struct device *parent = dev->parent; 191239378f1SKay Sievers 192239378f1SKay Sievers /* find first bus device in parent chain */ 193239378f1SKay Sievers while (parent && !parent->bus) 194239378f1SKay Sievers parent = parent->parent; 195239378f1SKay Sievers if (parent && parent->bus) { 196239378f1SKay Sievers const char *path; 197239378f1SKay Sievers 198239378f1SKay Sievers path = kobject_get_path(&parent->kobj, GFP_KERNEL); 1992c7afd12SKay Sievers if (path) { 2007eff2e7aSKay Sievers add_uevent_var(env, "PHYSDEVPATH=%s", path); 201239378f1SKay Sievers kfree(path); 2022c7afd12SKay Sievers } 203239378f1SKay Sievers 2047eff2e7aSKay Sievers add_uevent_var(env, "PHYSDEVBUS=%s", parent->bus->name); 205239378f1SKay Sievers 206239378f1SKay Sievers if (parent->driver) 2077eff2e7aSKay Sievers add_uevent_var(env, "PHYSDEVDRIVER=%s", 2087eff2e7aSKay Sievers parent->driver->name); 209239378f1SKay Sievers } 210239378f1SKay Sievers } else if (dev->bus) { 2117eff2e7aSKay Sievers add_uevent_var(env, "PHYSDEVBUS=%s", dev->bus->name); 212239378f1SKay Sievers 213239378f1SKay Sievers if (dev->driver) 2144a3ad20cSGreg Kroah-Hartman add_uevent_var(env, "PHYSDEVDRIVER=%s", 2154a3ad20cSGreg Kroah-Hartman dev->driver->name); 216d81d9d6bSKay Sievers } 217239378f1SKay Sievers #endif 2181da177e4SLinus Torvalds 2191da177e4SLinus Torvalds /* have the bus specific function add its stuff */ 2207eff2e7aSKay Sievers if (dev->bus && dev->bus->uevent) { 2217eff2e7aSKay Sievers retval = dev->bus->uevent(dev, env); 222f9f852dfSKay Sievers if (retval) 2237dc72b28SGreg Kroah-Hartman pr_debug("device: '%s': %s: bus uevent() returned %d\n", 2241e0b2cf9SKay Sievers dev_name(dev), __func__, retval); 2251da177e4SLinus Torvalds } 2261da177e4SLinus Torvalds 2272620efefSGreg Kroah-Hartman /* have the class specific function add its stuff */ 2287eff2e7aSKay Sievers if (dev->class && dev->class->dev_uevent) { 2297eff2e7aSKay Sievers retval = dev->class->dev_uevent(dev, env); 230f9f852dfSKay Sievers if (retval) 2317dc72b28SGreg Kroah-Hartman pr_debug("device: '%s': %s: class uevent() " 2321e0b2cf9SKay Sievers "returned %d\n", dev_name(dev), 2332b3a302aSHarvey Harrison __func__, retval); 2342620efefSGreg Kroah-Hartman } 235f9f852dfSKay Sievers 236f9f852dfSKay Sievers /* have the device type specific fuction add its stuff */ 2377eff2e7aSKay Sievers if (dev->type && dev->type->uevent) { 2387eff2e7aSKay Sievers retval = dev->type->uevent(dev, env); 239f9f852dfSKay Sievers if (retval) 2407dc72b28SGreg Kroah-Hartman pr_debug("device: '%s': %s: dev_type uevent() " 2411e0b2cf9SKay Sievers "returned %d\n", dev_name(dev), 2422b3a302aSHarvey Harrison __func__, retval); 2432620efefSGreg Kroah-Hartman } 2442620efefSGreg Kroah-Hartman 2451da177e4SLinus Torvalds return retval; 2461da177e4SLinus Torvalds } 2471da177e4SLinus Torvalds 248312c004dSKay Sievers static struct kset_uevent_ops device_uevent_ops = { 249312c004dSKay Sievers .filter = dev_uevent_filter, 250312c004dSKay Sievers .name = dev_uevent_name, 251312c004dSKay Sievers .uevent = dev_uevent, 2521da177e4SLinus Torvalds }; 2531da177e4SLinus Torvalds 25416574dccSKay Sievers static ssize_t show_uevent(struct device *dev, struct device_attribute *attr, 25516574dccSKay Sievers char *buf) 25616574dccSKay Sievers { 25716574dccSKay Sievers struct kobject *top_kobj; 25816574dccSKay Sievers struct kset *kset; 2597eff2e7aSKay Sievers struct kobj_uevent_env *env = NULL; 26016574dccSKay Sievers int i; 26116574dccSKay Sievers size_t count = 0; 26216574dccSKay Sievers int retval; 26316574dccSKay Sievers 26416574dccSKay Sievers /* search the kset, the device belongs to */ 26516574dccSKay Sievers top_kobj = &dev->kobj; 2665c5daf65SKay Sievers while (!top_kobj->kset && top_kobj->parent) 26716574dccSKay Sievers top_kobj = top_kobj->parent; 26816574dccSKay Sievers if (!top_kobj->kset) 26916574dccSKay Sievers goto out; 2705c5daf65SKay Sievers 27116574dccSKay Sievers kset = top_kobj->kset; 27216574dccSKay Sievers if (!kset->uevent_ops || !kset->uevent_ops->uevent) 27316574dccSKay Sievers goto out; 27416574dccSKay Sievers 27516574dccSKay Sievers /* respect filter */ 27616574dccSKay Sievers if (kset->uevent_ops && kset->uevent_ops->filter) 27716574dccSKay Sievers if (!kset->uevent_ops->filter(kset, &dev->kobj)) 27816574dccSKay Sievers goto out; 27916574dccSKay Sievers 2807eff2e7aSKay Sievers env = kzalloc(sizeof(struct kobj_uevent_env), GFP_KERNEL); 2817eff2e7aSKay Sievers if (!env) 282c7308c81SGreg Kroah-Hartman return -ENOMEM; 283c7308c81SGreg Kroah-Hartman 28416574dccSKay Sievers /* let the kset specific function add its keys */ 2857eff2e7aSKay Sievers retval = kset->uevent_ops->uevent(kset, &dev->kobj, env); 28616574dccSKay Sievers if (retval) 28716574dccSKay Sievers goto out; 28816574dccSKay Sievers 28916574dccSKay Sievers /* copy keys to file */ 2907eff2e7aSKay Sievers for (i = 0; i < env->envp_idx; i++) 2917eff2e7aSKay Sievers count += sprintf(&buf[count], "%s\n", env->envp[i]); 29216574dccSKay Sievers out: 2937eff2e7aSKay Sievers kfree(env); 29416574dccSKay Sievers return count; 29516574dccSKay Sievers } 29616574dccSKay Sievers 297a7fd6706SKay Sievers static ssize_t store_uevent(struct device *dev, struct device_attribute *attr, 298a7fd6706SKay Sievers const char *buf, size_t count) 299a7fd6706SKay Sievers { 30060a96a59SKay Sievers enum kobject_action action; 30160a96a59SKay Sievers 3025c5daf65SKay Sievers if (kobject_action_type(buf, count, &action) == 0) { 30360a96a59SKay Sievers kobject_uevent(&dev->kobj, action); 30460a96a59SKay Sievers goto out; 30560a96a59SKay Sievers } 30660a96a59SKay Sievers 30722af74f3SKay Sievers dev_err(dev, "uevent: unsupported action-string; this will " 30860a96a59SKay Sievers "be ignored in a future kernel version\n"); 309312c004dSKay Sievers kobject_uevent(&dev->kobj, KOBJ_ADD); 31060a96a59SKay Sievers out: 311a7fd6706SKay Sievers return count; 312a7fd6706SKay Sievers } 313a7fd6706SKay Sievers 314ad6a1e1cSTejun Heo static struct device_attribute uevent_attr = 315ad6a1e1cSTejun Heo __ATTR(uevent, S_IRUGO | S_IWUSR, show_uevent, store_uevent); 316ad6a1e1cSTejun Heo 317621a1672SDmitry Torokhov static int device_add_attributes(struct device *dev, 318621a1672SDmitry Torokhov struct device_attribute *attrs) 319de0ff00dSGreg Kroah-Hartman { 320de0ff00dSGreg Kroah-Hartman int error = 0; 321621a1672SDmitry Torokhov int i; 322de0ff00dSGreg Kroah-Hartman 323621a1672SDmitry Torokhov if (attrs) { 324621a1672SDmitry Torokhov for (i = 0; attr_name(attrs[i]); i++) { 325621a1672SDmitry Torokhov error = device_create_file(dev, &attrs[i]); 326621a1672SDmitry Torokhov if (error) 327621a1672SDmitry Torokhov break; 328621a1672SDmitry Torokhov } 329621a1672SDmitry Torokhov if (error) 330de0ff00dSGreg Kroah-Hartman while (--i >= 0) 331621a1672SDmitry Torokhov device_remove_file(dev, &attrs[i]); 332de0ff00dSGreg Kroah-Hartman } 333de0ff00dSGreg Kroah-Hartman return error; 334de0ff00dSGreg Kroah-Hartman } 335de0ff00dSGreg Kroah-Hartman 336621a1672SDmitry Torokhov static void device_remove_attributes(struct device *dev, 337621a1672SDmitry Torokhov struct device_attribute *attrs) 338de0ff00dSGreg Kroah-Hartman { 339de0ff00dSGreg Kroah-Hartman int i; 340621a1672SDmitry Torokhov 341621a1672SDmitry Torokhov if (attrs) 342621a1672SDmitry Torokhov for (i = 0; attr_name(attrs[i]); i++) 343621a1672SDmitry Torokhov device_remove_file(dev, &attrs[i]); 344621a1672SDmitry Torokhov } 345621a1672SDmitry Torokhov 346621a1672SDmitry Torokhov static int device_add_groups(struct device *dev, 347a4dbd674SDavid Brownell const struct attribute_group **groups) 348621a1672SDmitry Torokhov { 349621a1672SDmitry Torokhov int error = 0; 350621a1672SDmitry Torokhov int i; 351621a1672SDmitry Torokhov 352621a1672SDmitry Torokhov if (groups) { 353621a1672SDmitry Torokhov for (i = 0; groups[i]; i++) { 354621a1672SDmitry Torokhov error = sysfs_create_group(&dev->kobj, groups[i]); 355621a1672SDmitry Torokhov if (error) { 356621a1672SDmitry Torokhov while (--i >= 0) 3574a3ad20cSGreg Kroah-Hartman sysfs_remove_group(&dev->kobj, 3584a3ad20cSGreg Kroah-Hartman groups[i]); 359621a1672SDmitry Torokhov break; 360de0ff00dSGreg Kroah-Hartman } 361de0ff00dSGreg Kroah-Hartman } 362de0ff00dSGreg Kroah-Hartman } 363621a1672SDmitry Torokhov return error; 364621a1672SDmitry Torokhov } 365621a1672SDmitry Torokhov 366621a1672SDmitry Torokhov static void device_remove_groups(struct device *dev, 367a4dbd674SDavid Brownell const struct attribute_group **groups) 368621a1672SDmitry Torokhov { 369621a1672SDmitry Torokhov int i; 370621a1672SDmitry Torokhov 371621a1672SDmitry Torokhov if (groups) 372621a1672SDmitry Torokhov for (i = 0; groups[i]; i++) 373621a1672SDmitry Torokhov sysfs_remove_group(&dev->kobj, groups[i]); 374621a1672SDmitry Torokhov } 375de0ff00dSGreg Kroah-Hartman 3762620efefSGreg Kroah-Hartman static int device_add_attrs(struct device *dev) 3772620efefSGreg Kroah-Hartman { 3782620efefSGreg Kroah-Hartman struct class *class = dev->class; 379f9f852dfSKay Sievers struct device_type *type = dev->type; 380621a1672SDmitry Torokhov int error; 3812620efefSGreg Kroah-Hartman 382621a1672SDmitry Torokhov if (class) { 383621a1672SDmitry Torokhov error = device_add_attributes(dev, class->dev_attrs); 3842620efefSGreg Kroah-Hartman if (error) 385621a1672SDmitry Torokhov return error; 386f9f852dfSKay Sievers } 387f9f852dfSKay Sievers 388621a1672SDmitry Torokhov if (type) { 389621a1672SDmitry Torokhov error = device_add_groups(dev, type->groups); 390f9f852dfSKay Sievers if (error) 391621a1672SDmitry Torokhov goto err_remove_class_attrs; 392f9f852dfSKay Sievers } 393621a1672SDmitry Torokhov 394621a1672SDmitry Torokhov error = device_add_groups(dev, dev->groups); 395f9f852dfSKay Sievers if (error) 396621a1672SDmitry Torokhov goto err_remove_type_groups; 397621a1672SDmitry Torokhov 398621a1672SDmitry Torokhov return 0; 399621a1672SDmitry Torokhov 400621a1672SDmitry Torokhov err_remove_type_groups: 401621a1672SDmitry Torokhov if (type) 402621a1672SDmitry Torokhov device_remove_groups(dev, type->groups); 403621a1672SDmitry Torokhov err_remove_class_attrs: 404621a1672SDmitry Torokhov if (class) 405621a1672SDmitry Torokhov device_remove_attributes(dev, class->dev_attrs); 406f9f852dfSKay Sievers 4072620efefSGreg Kroah-Hartman return error; 4082620efefSGreg Kroah-Hartman } 4092620efefSGreg Kroah-Hartman 4102620efefSGreg Kroah-Hartman static void device_remove_attrs(struct device *dev) 4112620efefSGreg Kroah-Hartman { 4122620efefSGreg Kroah-Hartman struct class *class = dev->class; 413f9f852dfSKay Sievers struct device_type *type = dev->type; 4142620efefSGreg Kroah-Hartman 415621a1672SDmitry Torokhov device_remove_groups(dev, dev->groups); 416f9f852dfSKay Sievers 417621a1672SDmitry Torokhov if (type) 418621a1672SDmitry Torokhov device_remove_groups(dev, type->groups); 419621a1672SDmitry Torokhov 420621a1672SDmitry Torokhov if (class) 421621a1672SDmitry Torokhov device_remove_attributes(dev, class->dev_attrs); 4222620efefSGreg Kroah-Hartman } 4232620efefSGreg Kroah-Hartman 4242620efefSGreg Kroah-Hartman 42523681e47SGreg Kroah-Hartman static ssize_t show_dev(struct device *dev, struct device_attribute *attr, 42623681e47SGreg Kroah-Hartman char *buf) 42723681e47SGreg Kroah-Hartman { 42823681e47SGreg Kroah-Hartman return print_dev_t(buf, dev->devt); 42923681e47SGreg Kroah-Hartman } 43023681e47SGreg Kroah-Hartman 431ad6a1e1cSTejun Heo static struct device_attribute devt_attr = 432ad6a1e1cSTejun Heo __ATTR(dev, S_IRUGO, show_dev, NULL); 433ad6a1e1cSTejun Heo 434881c6cfdSGreg Kroah-Hartman /* kset to create /sys/devices/ */ 435881c6cfdSGreg Kroah-Hartman struct kset *devices_kset; 4361da177e4SLinus Torvalds 4371da177e4SLinus Torvalds /** 4381da177e4SLinus Torvalds * device_create_file - create sysfs attribute file for device. 4391da177e4SLinus Torvalds * @dev: device. 4401da177e4SLinus Torvalds * @attr: device attribute descriptor. 4411da177e4SLinus Torvalds */ 4421da177e4SLinus Torvalds int device_create_file(struct device *dev, struct device_attribute *attr) 4431da177e4SLinus Torvalds { 4441da177e4SLinus Torvalds int error = 0; 4450c98b19fSCornelia Huck if (dev) 4461da177e4SLinus Torvalds error = sysfs_create_file(&dev->kobj, &attr->attr); 4471da177e4SLinus Torvalds return error; 4481da177e4SLinus Torvalds } 4491da177e4SLinus Torvalds 4501da177e4SLinus Torvalds /** 4511da177e4SLinus Torvalds * device_remove_file - remove sysfs attribute file. 4521da177e4SLinus Torvalds * @dev: device. 4531da177e4SLinus Torvalds * @attr: device attribute descriptor. 4541da177e4SLinus Torvalds */ 4551da177e4SLinus Torvalds void device_remove_file(struct device *dev, struct device_attribute *attr) 4561da177e4SLinus Torvalds { 4570c98b19fSCornelia Huck if (dev) 4581da177e4SLinus Torvalds sysfs_remove_file(&dev->kobj, &attr->attr); 4591da177e4SLinus Torvalds } 4601da177e4SLinus Torvalds 4612589f188SGreg Kroah-Hartman /** 4622589f188SGreg Kroah-Hartman * device_create_bin_file - create sysfs binary attribute file for device. 4632589f188SGreg Kroah-Hartman * @dev: device. 4642589f188SGreg Kroah-Hartman * @attr: device binary attribute descriptor. 4652589f188SGreg Kroah-Hartman */ 4662589f188SGreg Kroah-Hartman int device_create_bin_file(struct device *dev, struct bin_attribute *attr) 4672589f188SGreg Kroah-Hartman { 4682589f188SGreg Kroah-Hartman int error = -EINVAL; 4692589f188SGreg Kroah-Hartman if (dev) 4702589f188SGreg Kroah-Hartman error = sysfs_create_bin_file(&dev->kobj, attr); 4712589f188SGreg Kroah-Hartman return error; 4722589f188SGreg Kroah-Hartman } 4732589f188SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(device_create_bin_file); 4742589f188SGreg Kroah-Hartman 4752589f188SGreg Kroah-Hartman /** 4762589f188SGreg Kroah-Hartman * device_remove_bin_file - remove sysfs binary attribute file 4772589f188SGreg Kroah-Hartman * @dev: device. 4782589f188SGreg Kroah-Hartman * @attr: device binary attribute descriptor. 4792589f188SGreg Kroah-Hartman */ 4802589f188SGreg Kroah-Hartman void device_remove_bin_file(struct device *dev, struct bin_attribute *attr) 4812589f188SGreg Kroah-Hartman { 4822589f188SGreg Kroah-Hartman if (dev) 4832589f188SGreg Kroah-Hartman sysfs_remove_bin_file(&dev->kobj, attr); 4842589f188SGreg Kroah-Hartman } 4852589f188SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(device_remove_bin_file); 4862589f188SGreg Kroah-Hartman 487d9a9cdfbSAlan Stern /** 488523ded71SAlan Stern * device_schedule_callback_owner - helper to schedule a callback for a device 489d9a9cdfbSAlan Stern * @dev: device. 490d9a9cdfbSAlan Stern * @func: callback function to invoke later. 491523ded71SAlan Stern * @owner: module owning the callback routine 492d9a9cdfbSAlan Stern * 493d9a9cdfbSAlan Stern * Attribute methods must not unregister themselves or their parent device 494d9a9cdfbSAlan Stern * (which would amount to the same thing). Attempts to do so will deadlock, 495d9a9cdfbSAlan Stern * since unregistration is mutually exclusive with driver callbacks. 496d9a9cdfbSAlan Stern * 497d9a9cdfbSAlan Stern * Instead methods can call this routine, which will attempt to allocate 498d9a9cdfbSAlan Stern * and schedule a workqueue request to call back @func with @dev as its 499d9a9cdfbSAlan Stern * argument in the workqueue's process context. @dev will be pinned until 500d9a9cdfbSAlan Stern * @func returns. 501d9a9cdfbSAlan Stern * 502523ded71SAlan Stern * This routine is usually called via the inline device_schedule_callback(), 503523ded71SAlan Stern * which automatically sets @owner to THIS_MODULE. 504523ded71SAlan Stern * 505d9a9cdfbSAlan Stern * Returns 0 if the request was submitted, -ENOMEM if storage could not 506523ded71SAlan Stern * be allocated, -ENODEV if a reference to @owner isn't available. 507d9a9cdfbSAlan Stern * 508d9a9cdfbSAlan Stern * NOTE: This routine won't work if CONFIG_SYSFS isn't set! It uses an 509d9a9cdfbSAlan Stern * underlying sysfs routine (since it is intended for use by attribute 510d9a9cdfbSAlan Stern * methods), and if sysfs isn't available you'll get nothing but -ENOSYS. 511d9a9cdfbSAlan Stern */ 512523ded71SAlan Stern int device_schedule_callback_owner(struct device *dev, 513523ded71SAlan Stern void (*func)(struct device *), struct module *owner) 514d9a9cdfbSAlan Stern { 515d9a9cdfbSAlan Stern return sysfs_schedule_callback(&dev->kobj, 516523ded71SAlan Stern (void (*)(void *)) func, dev, owner); 517d9a9cdfbSAlan Stern } 518523ded71SAlan Stern EXPORT_SYMBOL_GPL(device_schedule_callback_owner); 519d9a9cdfbSAlan Stern 52034bb61f9SJames Bottomley static void klist_children_get(struct klist_node *n) 52134bb61f9SJames Bottomley { 522f791b8c8SGreg Kroah-Hartman struct device_private *p = to_device_private_parent(n); 523f791b8c8SGreg Kroah-Hartman struct device *dev = p->device; 52434bb61f9SJames Bottomley 52534bb61f9SJames Bottomley get_device(dev); 52634bb61f9SJames Bottomley } 52734bb61f9SJames Bottomley 52834bb61f9SJames Bottomley static void klist_children_put(struct klist_node *n) 52934bb61f9SJames Bottomley { 530f791b8c8SGreg Kroah-Hartman struct device_private *p = to_device_private_parent(n); 531f791b8c8SGreg Kroah-Hartman struct device *dev = p->device; 53234bb61f9SJames Bottomley 53334bb61f9SJames Bottomley put_device(dev); 53434bb61f9SJames Bottomley } 53534bb61f9SJames Bottomley 5361da177e4SLinus Torvalds /** 5371da177e4SLinus Torvalds * device_initialize - init device structure. 5381da177e4SLinus Torvalds * @dev: device. 5391da177e4SLinus Torvalds * 5405739411aSCornelia Huck * This prepares the device for use by other layers by initializing 5415739411aSCornelia Huck * its fields. 5421da177e4SLinus Torvalds * It is the first half of device_register(), if called by 5435739411aSCornelia Huck * that function, though it can also be called separately, so one 5445739411aSCornelia Huck * may use @dev's fields. In particular, get_device()/put_device() 5455739411aSCornelia Huck * may be used for reference counting of @dev after calling this 5465739411aSCornelia Huck * function. 5475739411aSCornelia Huck * 5485739411aSCornelia Huck * NOTE: Use put_device() to give up your reference instead of freeing 5495739411aSCornelia Huck * @dev directly once you have called this function. 5501da177e4SLinus Torvalds */ 5511da177e4SLinus Torvalds void device_initialize(struct device *dev) 5521da177e4SLinus Torvalds { 553881c6cfdSGreg Kroah-Hartman dev->kobj.kset = devices_kset; 554f9cb074bSGreg Kroah-Hartman kobject_init(&dev->kobj, &device_ktype); 5551da177e4SLinus Torvalds INIT_LIST_HEAD(&dev->dma_pools); 556af70316aSmochel@digitalimplant.org init_MUTEX(&dev->sem); 5579ac7849eSTejun Heo spin_lock_init(&dev->devres_lock); 5589ac7849eSTejun Heo INIT_LIST_HEAD(&dev->devres_head); 5590ac85241SDavid Brownell device_init_wakeup(dev, 0); 5603b98aeafSAlan Stern device_pm_init(dev); 56187348136SChristoph Hellwig set_dev_node(dev, -1); 5621da177e4SLinus Torvalds } 5631da177e4SLinus Torvalds 56440fa5422SGreg Kroah-Hartman #ifdef CONFIG_SYSFS_DEPRECATED 565c744aeaeSCornelia Huck static struct kobject *get_device_parent(struct device *dev, 566c744aeaeSCornelia Huck struct device *parent) 56740fa5422SGreg Kroah-Hartman { 568da231fd5SKay Sievers /* class devices without a parent live in /sys/class/<classname>/ */ 5693eb215deSDmitry Torokhov if (dev->class && (!parent || parent->class != dev->class)) 5701fbfee6cSGreg Kroah-Hartman return &dev->class->p->class_subsys.kobj; 571da231fd5SKay Sievers /* all other devices keep their parent */ 57240fa5422SGreg Kroah-Hartman else if (parent) 573c744aeaeSCornelia Huck return &parent->kobj; 57440fa5422SGreg Kroah-Hartman 575c744aeaeSCornelia Huck return NULL; 57640fa5422SGreg Kroah-Hartman } 577da231fd5SKay Sievers 578da231fd5SKay Sievers static inline void cleanup_device_parent(struct device *dev) {} 57963b6971aSCornelia Huck static inline void cleanup_glue_dir(struct device *dev, 58063b6971aSCornelia Huck struct kobject *glue_dir) {} 58140fa5422SGreg Kroah-Hartman #else 582c744aeaeSCornelia Huck static struct kobject *virtual_device_parent(struct device *dev) 583f0ee61a6SGreg Kroah-Hartman { 584f0ee61a6SGreg Kroah-Hartman static struct kobject *virtual_dir = NULL; 585f0ee61a6SGreg Kroah-Hartman 586f0ee61a6SGreg Kroah-Hartman if (!virtual_dir) 5874ff6abffSGreg Kroah-Hartman virtual_dir = kobject_create_and_add("virtual", 588881c6cfdSGreg Kroah-Hartman &devices_kset->kobj); 589f0ee61a6SGreg Kroah-Hartman 59086406245SKay Sievers return virtual_dir; 591f0ee61a6SGreg Kroah-Hartman } 592f0ee61a6SGreg Kroah-Hartman 593c744aeaeSCornelia Huck static struct kobject *get_device_parent(struct device *dev, 594c744aeaeSCornelia Huck struct device *parent) 59540fa5422SGreg Kroah-Hartman { 59643968d2fSGreg Kroah-Hartman int retval; 59743968d2fSGreg Kroah-Hartman 59886406245SKay Sievers if (dev->class) { 59986406245SKay Sievers struct kobject *kobj = NULL; 60086406245SKay Sievers struct kobject *parent_kobj; 60186406245SKay Sievers struct kobject *k; 60286406245SKay Sievers 60386406245SKay Sievers /* 60486406245SKay Sievers * If we have no parent, we live in "virtual". 6050f4dafc0SKay Sievers * Class-devices with a non class-device as parent, live 6060f4dafc0SKay Sievers * in a "glue" directory to prevent namespace collisions. 60786406245SKay Sievers */ 60886406245SKay Sievers if (parent == NULL) 60986406245SKay Sievers parent_kobj = virtual_device_parent(dev); 61086406245SKay Sievers else if (parent->class) 61186406245SKay Sievers return &parent->kobj; 61286406245SKay Sievers else 61386406245SKay Sievers parent_kobj = &parent->kobj; 61486406245SKay Sievers 61586406245SKay Sievers /* find our class-directory at the parent and reference it */ 6167c71448bSGreg Kroah-Hartman spin_lock(&dev->class->p->class_dirs.list_lock); 6177c71448bSGreg Kroah-Hartman list_for_each_entry(k, &dev->class->p->class_dirs.list, entry) 61886406245SKay Sievers if (k->parent == parent_kobj) { 61986406245SKay Sievers kobj = kobject_get(k); 62086406245SKay Sievers break; 62186406245SKay Sievers } 6227c71448bSGreg Kroah-Hartman spin_unlock(&dev->class->p->class_dirs.list_lock); 62386406245SKay Sievers if (kobj) 62486406245SKay Sievers return kobj; 62586406245SKay Sievers 62686406245SKay Sievers /* or create a new class-directory at the parent device */ 62743968d2fSGreg Kroah-Hartman k = kobject_create(); 62843968d2fSGreg Kroah-Hartman if (!k) 62943968d2fSGreg Kroah-Hartman return NULL; 6307c71448bSGreg Kroah-Hartman k->kset = &dev->class->p->class_dirs; 631b2d6db58SGreg Kroah-Hartman retval = kobject_add(k, parent_kobj, "%s", dev->class->name); 63243968d2fSGreg Kroah-Hartman if (retval < 0) { 63343968d2fSGreg Kroah-Hartman kobject_put(k); 63443968d2fSGreg Kroah-Hartman return NULL; 63543968d2fSGreg Kroah-Hartman } 6360f4dafc0SKay Sievers /* do not emit an uevent for this simple "glue" directory */ 63743968d2fSGreg Kroah-Hartman return k; 63886406245SKay Sievers } 63986406245SKay Sievers 64086406245SKay Sievers if (parent) 641c744aeaeSCornelia Huck return &parent->kobj; 642c744aeaeSCornelia Huck return NULL; 643c744aeaeSCornelia Huck } 644da231fd5SKay Sievers 64563b6971aSCornelia Huck static void cleanup_glue_dir(struct device *dev, struct kobject *glue_dir) 646da231fd5SKay Sievers { 6470f4dafc0SKay Sievers /* see if we live in a "glue" directory */ 648c1fe539aSCornelia Huck if (!glue_dir || !dev->class || 6497c71448bSGreg Kroah-Hartman glue_dir->kset != &dev->class->p->class_dirs) 650da231fd5SKay Sievers return; 651da231fd5SKay Sievers 6520f4dafc0SKay Sievers kobject_put(glue_dir); 653da231fd5SKay Sievers } 65463b6971aSCornelia Huck 65563b6971aSCornelia Huck static void cleanup_device_parent(struct device *dev) 65663b6971aSCornelia Huck { 65763b6971aSCornelia Huck cleanup_glue_dir(dev, dev->kobj.parent); 65863b6971aSCornelia Huck } 659c744aeaeSCornelia Huck #endif 66086406245SKay Sievers 66163b6971aSCornelia Huck static void setup_parent(struct device *dev, struct device *parent) 662c744aeaeSCornelia Huck { 663c744aeaeSCornelia Huck struct kobject *kobj; 664c744aeaeSCornelia Huck kobj = get_device_parent(dev, parent); 665c744aeaeSCornelia Huck if (kobj) 666c744aeaeSCornelia Huck dev->kobj.parent = kobj; 66740fa5422SGreg Kroah-Hartman } 66840fa5422SGreg Kroah-Hartman 6692ee97cafSCornelia Huck static int device_add_class_symlinks(struct device *dev) 6702ee97cafSCornelia Huck { 6712ee97cafSCornelia Huck int error; 6722ee97cafSCornelia Huck 6732ee97cafSCornelia Huck if (!dev->class) 6742ee97cafSCornelia Huck return 0; 675da231fd5SKay Sievers 6761fbfee6cSGreg Kroah-Hartman error = sysfs_create_link(&dev->kobj, 6771fbfee6cSGreg Kroah-Hartman &dev->class->p->class_subsys.kobj, 6782ee97cafSCornelia Huck "subsystem"); 6792ee97cafSCornelia Huck if (error) 6802ee97cafSCornelia Huck goto out; 681da231fd5SKay Sievers 682da231fd5SKay Sievers #ifdef CONFIG_SYSFS_DEPRECATED 683da231fd5SKay Sievers /* stacked class devices need a symlink in the class directory */ 6841fbfee6cSGreg Kroah-Hartman if (dev->kobj.parent != &dev->class->p->class_subsys.kobj && 6854e886c29SGreg Kroah-Hartman device_is_not_partition(dev)) { 6861fbfee6cSGreg Kroah-Hartman error = sysfs_create_link(&dev->class->p->class_subsys.kobj, 6871e0b2cf9SKay Sievers &dev->kobj, dev_name(dev)); 6882ee97cafSCornelia Huck if (error) 6892ee97cafSCornelia Huck goto out_subsys; 6902ee97cafSCornelia Huck } 691da231fd5SKay Sievers 6924e886c29SGreg Kroah-Hartman if (dev->parent && device_is_not_partition(dev)) { 6934f01a757SDmitry Torokhov struct device *parent = dev->parent; 6944f01a757SDmitry Torokhov char *class_name; 6954f01a757SDmitry Torokhov 6964f01a757SDmitry Torokhov /* 697da231fd5SKay Sievers * stacked class devices have the 'device' link 698da231fd5SKay Sievers * pointing to the bus device instead of the parent 6994f01a757SDmitry Torokhov */ 7004f01a757SDmitry Torokhov while (parent->class && !parent->bus && parent->parent) 7014f01a757SDmitry Torokhov parent = parent->parent; 7024f01a757SDmitry Torokhov 7034f01a757SDmitry Torokhov error = sysfs_create_link(&dev->kobj, 7044f01a757SDmitry Torokhov &parent->kobj, 7052ee97cafSCornelia Huck "device"); 7062ee97cafSCornelia Huck if (error) 7072ee97cafSCornelia Huck goto out_busid; 7084f01a757SDmitry Torokhov 7094f01a757SDmitry Torokhov class_name = make_class_name(dev->class->name, 7102ee97cafSCornelia Huck &dev->kobj); 7112ee97cafSCornelia Huck if (class_name) 7122ee97cafSCornelia Huck error = sysfs_create_link(&dev->parent->kobj, 7132ee97cafSCornelia Huck &dev->kobj, class_name); 7142ee97cafSCornelia Huck kfree(class_name); 7152ee97cafSCornelia Huck if (error) 7162ee97cafSCornelia Huck goto out_device; 7172ee97cafSCornelia Huck } 718da231fd5SKay Sievers return 0; 719da231fd5SKay Sievers 720da231fd5SKay Sievers out_device: 7214e886c29SGreg Kroah-Hartman if (dev->parent && device_is_not_partition(dev)) 722da231fd5SKay Sievers sysfs_remove_link(&dev->kobj, "device"); 723da231fd5SKay Sievers out_busid: 7241fbfee6cSGreg Kroah-Hartman if (dev->kobj.parent != &dev->class->p->class_subsys.kobj && 7254e886c29SGreg Kroah-Hartman device_is_not_partition(dev)) 7261fbfee6cSGreg Kroah-Hartman sysfs_remove_link(&dev->class->p->class_subsys.kobj, 7271e0b2cf9SKay Sievers dev_name(dev)); 7284f01a757SDmitry Torokhov #else 729da231fd5SKay Sievers /* link in the class directory pointing to the device */ 7301fbfee6cSGreg Kroah-Hartman error = sysfs_create_link(&dev->class->p->class_subsys.kobj, 7311e0b2cf9SKay Sievers &dev->kobj, dev_name(dev)); 732da231fd5SKay Sievers if (error) 733da231fd5SKay Sievers goto out_subsys; 734da231fd5SKay Sievers 7354e886c29SGreg Kroah-Hartman if (dev->parent && device_is_not_partition(dev)) { 7364f01a757SDmitry Torokhov error = sysfs_create_link(&dev->kobj, &dev->parent->kobj, 7374f01a757SDmitry Torokhov "device"); 7384f01a757SDmitry Torokhov if (error) 7394f01a757SDmitry Torokhov goto out_busid; 7402ee97cafSCornelia Huck } 7412ee97cafSCornelia Huck return 0; 7422ee97cafSCornelia Huck 7432ee97cafSCornelia Huck out_busid: 7441e0b2cf9SKay Sievers sysfs_remove_link(&dev->class->p->class_subsys.kobj, dev_name(dev)); 745da231fd5SKay Sievers #endif 746da231fd5SKay Sievers 7472ee97cafSCornelia Huck out_subsys: 7482ee97cafSCornelia Huck sysfs_remove_link(&dev->kobj, "subsystem"); 7492ee97cafSCornelia Huck out: 7502ee97cafSCornelia Huck return error; 7512ee97cafSCornelia Huck } 7522ee97cafSCornelia Huck 7532ee97cafSCornelia Huck static void device_remove_class_symlinks(struct device *dev) 7542ee97cafSCornelia Huck { 7552ee97cafSCornelia Huck if (!dev->class) 7562ee97cafSCornelia Huck return; 757da231fd5SKay Sievers 7582ee97cafSCornelia Huck #ifdef CONFIG_SYSFS_DEPRECATED 7594e886c29SGreg Kroah-Hartman if (dev->parent && device_is_not_partition(dev)) { 7602ee97cafSCornelia Huck char *class_name; 7612ee97cafSCornelia Huck 7622ee97cafSCornelia Huck class_name = make_class_name(dev->class->name, &dev->kobj); 7632ee97cafSCornelia Huck if (class_name) { 7642ee97cafSCornelia Huck sysfs_remove_link(&dev->parent->kobj, class_name); 7652ee97cafSCornelia Huck kfree(class_name); 7662ee97cafSCornelia Huck } 7672ee97cafSCornelia Huck sysfs_remove_link(&dev->kobj, "device"); 7682ee97cafSCornelia Huck } 769da231fd5SKay Sievers 7701fbfee6cSGreg Kroah-Hartman if (dev->kobj.parent != &dev->class->p->class_subsys.kobj && 7714e886c29SGreg Kroah-Hartman device_is_not_partition(dev)) 7721fbfee6cSGreg Kroah-Hartman sysfs_remove_link(&dev->class->p->class_subsys.kobj, 7731e0b2cf9SKay Sievers dev_name(dev)); 774da231fd5SKay Sievers #else 7754e886c29SGreg Kroah-Hartman if (dev->parent && device_is_not_partition(dev)) 776da231fd5SKay Sievers sysfs_remove_link(&dev->kobj, "device"); 777da231fd5SKay Sievers 7781e0b2cf9SKay Sievers sysfs_remove_link(&dev->class->p->class_subsys.kobj, dev_name(dev)); 779da231fd5SKay Sievers #endif 780da231fd5SKay Sievers 7812ee97cafSCornelia Huck sysfs_remove_link(&dev->kobj, "subsystem"); 7822ee97cafSCornelia Huck } 7832ee97cafSCornelia Huck 7841da177e4SLinus Torvalds /** 785413c239fSStephen Rothwell * dev_set_name - set a device name 786413c239fSStephen Rothwell * @dev: device 78746232366SRandy Dunlap * @fmt: format string for the device's name 788413c239fSStephen Rothwell */ 789413c239fSStephen Rothwell int dev_set_name(struct device *dev, const char *fmt, ...) 790413c239fSStephen Rothwell { 791413c239fSStephen Rothwell va_list vargs; 7921fa5ae85SKay Sievers int err; 793413c239fSStephen Rothwell 794413c239fSStephen Rothwell va_start(vargs, fmt); 7951fa5ae85SKay Sievers err = kobject_set_name_vargs(&dev->kobj, fmt, vargs); 796413c239fSStephen Rothwell va_end(vargs); 7971fa5ae85SKay Sievers return err; 798413c239fSStephen Rothwell } 799413c239fSStephen Rothwell EXPORT_SYMBOL_GPL(dev_set_name); 800413c239fSStephen Rothwell 801413c239fSStephen Rothwell /** 802e105b8bfSDan Williams * device_to_dev_kobj - select a /sys/dev/ directory for the device 803e105b8bfSDan Williams * @dev: device 804e105b8bfSDan Williams * 805e105b8bfSDan Williams * By default we select char/ for new entries. Setting class->dev_obj 806e105b8bfSDan Williams * to NULL prevents an entry from being created. class->dev_kobj must 807e105b8bfSDan Williams * be set (or cleared) before any devices are registered to the class 808e105b8bfSDan Williams * otherwise device_create_sys_dev_entry() and 809e105b8bfSDan Williams * device_remove_sys_dev_entry() will disagree about the the presence 810e105b8bfSDan Williams * of the link. 811e105b8bfSDan Williams */ 812e105b8bfSDan Williams static struct kobject *device_to_dev_kobj(struct device *dev) 813e105b8bfSDan Williams { 814e105b8bfSDan Williams struct kobject *kobj; 815e105b8bfSDan Williams 816e105b8bfSDan Williams if (dev->class) 817e105b8bfSDan Williams kobj = dev->class->dev_kobj; 818e105b8bfSDan Williams else 819e105b8bfSDan Williams kobj = sysfs_dev_char_kobj; 820e105b8bfSDan Williams 821e105b8bfSDan Williams return kobj; 822e105b8bfSDan Williams } 823e105b8bfSDan Williams 824e105b8bfSDan Williams static int device_create_sys_dev_entry(struct device *dev) 825e105b8bfSDan Williams { 826e105b8bfSDan Williams struct kobject *kobj = device_to_dev_kobj(dev); 827e105b8bfSDan Williams int error = 0; 828e105b8bfSDan Williams char devt_str[15]; 829e105b8bfSDan Williams 830e105b8bfSDan Williams if (kobj) { 831e105b8bfSDan Williams format_dev_t(devt_str, dev->devt); 832e105b8bfSDan Williams error = sysfs_create_link(kobj, &dev->kobj, devt_str); 833e105b8bfSDan Williams } 834e105b8bfSDan Williams 835e105b8bfSDan Williams return error; 836e105b8bfSDan Williams } 837e105b8bfSDan Williams 838e105b8bfSDan Williams static void device_remove_sys_dev_entry(struct device *dev) 839e105b8bfSDan Williams { 840e105b8bfSDan Williams struct kobject *kobj = device_to_dev_kobj(dev); 841e105b8bfSDan Williams char devt_str[15]; 842e105b8bfSDan Williams 843e105b8bfSDan Williams if (kobj) { 844e105b8bfSDan Williams format_dev_t(devt_str, dev->devt); 845e105b8bfSDan Williams sysfs_remove_link(kobj, devt_str); 846e105b8bfSDan Williams } 847e105b8bfSDan Williams } 848e105b8bfSDan Williams 849b4028437SGreg Kroah-Hartman int device_private_init(struct device *dev) 850b4028437SGreg Kroah-Hartman { 851b4028437SGreg Kroah-Hartman dev->p = kzalloc(sizeof(*dev->p), GFP_KERNEL); 852b4028437SGreg Kroah-Hartman if (!dev->p) 853b4028437SGreg Kroah-Hartman return -ENOMEM; 854b4028437SGreg Kroah-Hartman dev->p->device = dev; 855b4028437SGreg Kroah-Hartman klist_init(&dev->p->klist_children, klist_children_get, 856b4028437SGreg Kroah-Hartman klist_children_put); 857b4028437SGreg Kroah-Hartman return 0; 858b4028437SGreg Kroah-Hartman } 859b4028437SGreg Kroah-Hartman 860e105b8bfSDan Williams /** 8611da177e4SLinus Torvalds * device_add - add device to device hierarchy. 8621da177e4SLinus Torvalds * @dev: device. 8631da177e4SLinus Torvalds * 8641da177e4SLinus Torvalds * This is part 2 of device_register(), though may be called 8651da177e4SLinus Torvalds * separately _iff_ device_initialize() has been called separately. 8661da177e4SLinus Torvalds * 8675739411aSCornelia Huck * This adds @dev to the kobject hierarchy via kobject_add(), adds it 8681da177e4SLinus Torvalds * to the global and sibling lists for the device, then 8691da177e4SLinus Torvalds * adds it to the other relevant subsystems of the driver model. 8705739411aSCornelia Huck * 8715739411aSCornelia Huck * NOTE: _Never_ directly free @dev after calling this function, even 8725739411aSCornelia Huck * if it returned an error! Always use put_device() to give up your 8735739411aSCornelia Huck * reference instead. 8741da177e4SLinus Torvalds */ 8751da177e4SLinus Torvalds int device_add(struct device *dev) 8761da177e4SLinus Torvalds { 8771da177e4SLinus Torvalds struct device *parent = NULL; 878c47ed219SGreg Kroah-Hartman struct class_interface *class_intf; 879c906a48aSGreg Kroah-Hartman int error = -EINVAL; 880775b64d2SRafael J. Wysocki 8811da177e4SLinus Torvalds dev = get_device(dev); 882c906a48aSGreg Kroah-Hartman if (!dev) 883c906a48aSGreg Kroah-Hartman goto done; 884c906a48aSGreg Kroah-Hartman 885fb069a5dSGreg Kroah-Hartman if (!dev->p) { 886b4028437SGreg Kroah-Hartman error = device_private_init(dev); 887b4028437SGreg Kroah-Hartman if (error) 888fb069a5dSGreg Kroah-Hartman goto done; 889fb069a5dSGreg Kroah-Hartman } 890fb069a5dSGreg Kroah-Hartman 8911fa5ae85SKay Sievers /* 8921fa5ae85SKay Sievers * for statically allocated devices, which should all be converted 8931fa5ae85SKay Sievers * some day, we need to initialize the name. We prevent reading back 8941fa5ae85SKay Sievers * the name, and force the use of dev_name() 8951fa5ae85SKay Sievers */ 8961fa5ae85SKay Sievers if (dev->init_name) { 897acc0e90fSGreg Kroah-Hartman dev_set_name(dev, "%s", dev->init_name); 8981fa5ae85SKay Sievers dev->init_name = NULL; 8991fa5ae85SKay Sievers } 900c906a48aSGreg Kroah-Hartman 9011fa5ae85SKay Sievers if (!dev_name(dev)) 9025c8563d7SKay Sievers goto name_error; 9031da177e4SLinus Torvalds 9041e0b2cf9SKay Sievers pr_debug("device: '%s': %s\n", dev_name(dev), __func__); 905c205ef48SGreg Kroah-Hartman 9061da177e4SLinus Torvalds parent = get_device(dev->parent); 90763b6971aSCornelia Huck setup_parent(dev, parent); 9081da177e4SLinus Torvalds 9090d358f22SYinghai Lu /* use parent numa_node */ 9100d358f22SYinghai Lu if (parent) 9110d358f22SYinghai Lu set_dev_node(dev, dev_to_node(parent)); 9120d358f22SYinghai Lu 9131da177e4SLinus Torvalds /* first, register with generic layer. */ 9148a577ffcSKay Sievers /* we require the name to be set before, and pass NULL */ 9158a577ffcSKay Sievers error = kobject_add(&dev->kobj, dev->kobj.parent, NULL); 91640fa5422SGreg Kroah-Hartman if (error) 9171da177e4SLinus Torvalds goto Error; 918a7fd6706SKay Sievers 91937022644SBrian Walsh /* notify platform of device entry */ 92037022644SBrian Walsh if (platform_notify) 92137022644SBrian Walsh platform_notify(dev); 92237022644SBrian Walsh 923ad6a1e1cSTejun Heo error = device_create_file(dev, &uevent_attr); 924a306eea4SCornelia Huck if (error) 925a306eea4SCornelia Huck goto attrError; 926a7fd6706SKay Sievers 92723681e47SGreg Kroah-Hartman if (MAJOR(dev->devt)) { 928ad6a1e1cSTejun Heo error = device_create_file(dev, &devt_attr); 929ad6a1e1cSTejun Heo if (error) 930a306eea4SCornelia Huck goto ueventattrError; 931e105b8bfSDan Williams 932e105b8bfSDan Williams error = device_create_sys_dev_entry(dev); 933e105b8bfSDan Williams if (error) 934e105b8bfSDan Williams goto devtattrError; 9352b2af54aSKay Sievers 9362b2af54aSKay Sievers devtmpfs_create_node(dev); 93723681e47SGreg Kroah-Hartman } 93823681e47SGreg Kroah-Hartman 9392ee97cafSCornelia Huck error = device_add_class_symlinks(dev); 9402ee97cafSCornelia Huck if (error) 9412ee97cafSCornelia Huck goto SymlinkError; 942dc0afa83SCornelia Huck error = device_add_attrs(dev); 943dc0afa83SCornelia Huck if (error) 9442620efefSGreg Kroah-Hartman goto AttrsError; 945dc0afa83SCornelia Huck error = bus_add_device(dev); 946dc0afa83SCornelia Huck if (error) 9471da177e4SLinus Torvalds goto BusError; 9483b98aeafSAlan Stern error = dpm_sysfs_add(dev); 94957eee3d2SRafael J. Wysocki if (error) 9503b98aeafSAlan Stern goto DPMError; 9513b98aeafSAlan Stern device_pm_add(dev); 952ec0676eeSAlan Stern 953ec0676eeSAlan Stern /* Notify clients of device addition. This call must come 954ec0676eeSAlan Stern * after dpm_sysf_add() and before kobject_uevent(). 955ec0676eeSAlan Stern */ 956ec0676eeSAlan Stern if (dev->bus) 957ec0676eeSAlan Stern blocking_notifier_call_chain(&dev->bus->p->bus_notifier, 958ec0676eeSAlan Stern BUS_NOTIFY_ADD_DEVICE, dev); 959ec0676eeSAlan Stern 96053877d06SKay Sievers kobject_uevent(&dev->kobj, KOBJ_ADD); 9612023c610SAlan Stern bus_probe_device(dev); 9621da177e4SLinus Torvalds if (parent) 963f791b8c8SGreg Kroah-Hartman klist_add_tail(&dev->p->knode_parent, 964f791b8c8SGreg Kroah-Hartman &parent->p->klist_children); 9651da177e4SLinus Torvalds 9665d9fd169SGreg Kroah-Hartman if (dev->class) { 967f75b1c60SDave Young mutex_lock(&dev->class->p->class_mutex); 968c47ed219SGreg Kroah-Hartman /* tie the class to the device */ 9695a3ceb86STejun Heo klist_add_tail(&dev->knode_class, 9705a3ceb86STejun Heo &dev->class->p->class_devices); 971c47ed219SGreg Kroah-Hartman 972c47ed219SGreg Kroah-Hartman /* notify any interfaces that the device is here */ 973184f1f77SGreg Kroah-Hartman list_for_each_entry(class_intf, 974184f1f77SGreg Kroah-Hartman &dev->class->p->class_interfaces, node) 975c47ed219SGreg Kroah-Hartman if (class_intf->add_dev) 976c47ed219SGreg Kroah-Hartman class_intf->add_dev(dev, class_intf); 977f75b1c60SDave Young mutex_unlock(&dev->class->p->class_mutex); 9785d9fd169SGreg Kroah-Hartman } 979c906a48aSGreg Kroah-Hartman done: 9801da177e4SLinus Torvalds put_device(dev); 9811da177e4SLinus Torvalds return error; 9823b98aeafSAlan Stern DPMError: 98357eee3d2SRafael J. Wysocki bus_remove_device(dev); 98457eee3d2SRafael J. Wysocki BusError: 9852620efefSGreg Kroah-Hartman device_remove_attrs(dev); 9862620efefSGreg Kroah-Hartman AttrsError: 9872ee97cafSCornelia Huck device_remove_class_symlinks(dev); 9882ee97cafSCornelia Huck SymlinkError: 989ad6a1e1cSTejun Heo if (MAJOR(dev->devt)) 990*ad72956dSKay Sievers devtmpfs_delete_node(dev); 991*ad72956dSKay Sievers if (MAJOR(dev->devt)) 992e105b8bfSDan Williams device_remove_sys_dev_entry(dev); 993e105b8bfSDan Williams devtattrError: 994e105b8bfSDan Williams if (MAJOR(dev->devt)) 995ad6a1e1cSTejun Heo device_remove_file(dev, &devt_attr); 996a306eea4SCornelia Huck ueventattrError: 997ad6a1e1cSTejun Heo device_remove_file(dev, &uevent_attr); 99823681e47SGreg Kroah-Hartman attrError: 999312c004dSKay Sievers kobject_uevent(&dev->kobj, KOBJ_REMOVE); 10001da177e4SLinus Torvalds kobject_del(&dev->kobj); 10011da177e4SLinus Torvalds Error: 100263b6971aSCornelia Huck cleanup_device_parent(dev); 10031da177e4SLinus Torvalds if (parent) 10041da177e4SLinus Torvalds put_device(parent); 10055c8563d7SKay Sievers name_error: 10065c8563d7SKay Sievers kfree(dev->p); 10075c8563d7SKay Sievers dev->p = NULL; 1008c906a48aSGreg Kroah-Hartman goto done; 10091da177e4SLinus Torvalds } 10101da177e4SLinus Torvalds 10111da177e4SLinus Torvalds /** 10121da177e4SLinus Torvalds * device_register - register a device with the system. 10131da177e4SLinus Torvalds * @dev: pointer to the device structure 10141da177e4SLinus Torvalds * 10151da177e4SLinus Torvalds * This happens in two clean steps - initialize the device 10161da177e4SLinus Torvalds * and add it to the system. The two steps can be called 10171da177e4SLinus Torvalds * separately, but this is the easiest and most common. 10181da177e4SLinus Torvalds * I.e. you should only call the two helpers separately if 10191da177e4SLinus Torvalds * have a clearly defined need to use and refcount the device 10201da177e4SLinus Torvalds * before it is added to the hierarchy. 10215739411aSCornelia Huck * 10225739411aSCornelia Huck * NOTE: _Never_ directly free @dev after calling this function, even 10235739411aSCornelia Huck * if it returned an error! Always use put_device() to give up the 10245739411aSCornelia Huck * reference initialized in this function instead. 10251da177e4SLinus Torvalds */ 10261da177e4SLinus Torvalds int device_register(struct device *dev) 10271da177e4SLinus Torvalds { 10281da177e4SLinus Torvalds device_initialize(dev); 10291da177e4SLinus Torvalds return device_add(dev); 10301da177e4SLinus Torvalds } 10311da177e4SLinus Torvalds 10321da177e4SLinus Torvalds /** 10331da177e4SLinus Torvalds * get_device - increment reference count for device. 10341da177e4SLinus Torvalds * @dev: device. 10351da177e4SLinus Torvalds * 10361da177e4SLinus Torvalds * This simply forwards the call to kobject_get(), though 10371da177e4SLinus Torvalds * we do take care to provide for the case that we get a NULL 10381da177e4SLinus Torvalds * pointer passed in. 10391da177e4SLinus Torvalds */ 10401da177e4SLinus Torvalds struct device *get_device(struct device *dev) 10411da177e4SLinus Torvalds { 10421da177e4SLinus Torvalds return dev ? to_dev(kobject_get(&dev->kobj)) : NULL; 10431da177e4SLinus Torvalds } 10441da177e4SLinus Torvalds 10451da177e4SLinus Torvalds /** 10461da177e4SLinus Torvalds * put_device - decrement reference count. 10471da177e4SLinus Torvalds * @dev: device in question. 10481da177e4SLinus Torvalds */ 10491da177e4SLinus Torvalds void put_device(struct device *dev) 10501da177e4SLinus Torvalds { 1051edfaa7c3SKay Sievers /* might_sleep(); */ 10521da177e4SLinus Torvalds if (dev) 10531da177e4SLinus Torvalds kobject_put(&dev->kobj); 10541da177e4SLinus Torvalds } 10551da177e4SLinus Torvalds 10561da177e4SLinus Torvalds /** 10571da177e4SLinus Torvalds * device_del - delete device from system. 10581da177e4SLinus Torvalds * @dev: device. 10591da177e4SLinus Torvalds * 10601da177e4SLinus Torvalds * This is the first part of the device unregistration 10611da177e4SLinus Torvalds * sequence. This removes the device from the lists we control 10621da177e4SLinus Torvalds * from here, has it removed from the other driver model 10631da177e4SLinus Torvalds * subsystems it was added to in device_add(), and removes it 10641da177e4SLinus Torvalds * from the kobject hierarchy. 10651da177e4SLinus Torvalds * 10661da177e4SLinus Torvalds * NOTE: this should be called manually _iff_ device_add() was 10671da177e4SLinus Torvalds * also called manually. 10681da177e4SLinus Torvalds */ 10691da177e4SLinus Torvalds void device_del(struct device *dev) 10701da177e4SLinus Torvalds { 10711da177e4SLinus Torvalds struct device *parent = dev->parent; 1072c47ed219SGreg Kroah-Hartman struct class_interface *class_intf; 10731da177e4SLinus Torvalds 1074ec0676eeSAlan Stern /* Notify clients of device removal. This call must come 1075ec0676eeSAlan Stern * before dpm_sysfs_remove(). 1076ec0676eeSAlan Stern */ 1077ec0676eeSAlan Stern if (dev->bus) 1078ec0676eeSAlan Stern blocking_notifier_call_chain(&dev->bus->p->bus_notifier, 1079ec0676eeSAlan Stern BUS_NOTIFY_DEL_DEVICE, dev); 1080775b64d2SRafael J. Wysocki device_pm_remove(dev); 10813b98aeafSAlan Stern dpm_sysfs_remove(dev); 10821da177e4SLinus Torvalds if (parent) 1083f791b8c8SGreg Kroah-Hartman klist_del(&dev->p->knode_parent); 1084e105b8bfSDan Williams if (MAJOR(dev->devt)) { 10852b2af54aSKay Sievers devtmpfs_delete_node(dev); 1086e105b8bfSDan Williams device_remove_sys_dev_entry(dev); 1087ad6a1e1cSTejun Heo device_remove_file(dev, &devt_attr); 1088e105b8bfSDan Williams } 1089b9d9c82bSKay Sievers if (dev->class) { 1090da231fd5SKay Sievers device_remove_class_symlinks(dev); 109199ef3ef8SKay Sievers 1092f75b1c60SDave Young mutex_lock(&dev->class->p->class_mutex); 1093c47ed219SGreg Kroah-Hartman /* notify any interfaces that the device is now gone */ 1094184f1f77SGreg Kroah-Hartman list_for_each_entry(class_intf, 1095184f1f77SGreg Kroah-Hartman &dev->class->p->class_interfaces, node) 1096c47ed219SGreg Kroah-Hartman if (class_intf->remove_dev) 1097c47ed219SGreg Kroah-Hartman class_intf->remove_dev(dev, class_intf); 1098c47ed219SGreg Kroah-Hartman /* remove the device from the class list */ 10995a3ceb86STejun Heo klist_del(&dev->knode_class); 1100f75b1c60SDave Young mutex_unlock(&dev->class->p->class_mutex); 1101b9d9c82bSKay Sievers } 1102ad6a1e1cSTejun Heo device_remove_file(dev, &uevent_attr); 11032620efefSGreg Kroah-Hartman device_remove_attrs(dev); 110428953533SBenjamin Herrenschmidt bus_remove_device(dev); 11051da177e4SLinus Torvalds 11062f8d16a9STejun Heo /* 11072f8d16a9STejun Heo * Some platform devices are driven without driver attached 11082f8d16a9STejun Heo * and managed resources may have been acquired. Make sure 11092f8d16a9STejun Heo * all resources are released. 11102f8d16a9STejun Heo */ 11112f8d16a9STejun Heo devres_release_all(dev); 11122f8d16a9STejun Heo 11131da177e4SLinus Torvalds /* Notify the platform of the removal, in case they 11141da177e4SLinus Torvalds * need to do anything... 11151da177e4SLinus Torvalds */ 11161da177e4SLinus Torvalds if (platform_notify_remove) 11171da177e4SLinus Torvalds platform_notify_remove(dev); 1118312c004dSKay Sievers kobject_uevent(&dev->kobj, KOBJ_REMOVE); 1119da231fd5SKay Sievers cleanup_device_parent(dev); 11201da177e4SLinus Torvalds kobject_del(&dev->kobj); 11211da177e4SLinus Torvalds put_device(parent); 11221da177e4SLinus Torvalds } 11231da177e4SLinus Torvalds 11241da177e4SLinus Torvalds /** 11251da177e4SLinus Torvalds * device_unregister - unregister device from system. 11261da177e4SLinus Torvalds * @dev: device going away. 11271da177e4SLinus Torvalds * 11281da177e4SLinus Torvalds * We do this in two parts, like we do device_register(). First, 11291da177e4SLinus Torvalds * we remove it from all the subsystems with device_del(), then 11301da177e4SLinus Torvalds * we decrement the reference count via put_device(). If that 11311da177e4SLinus Torvalds * is the final reference count, the device will be cleaned up 11321da177e4SLinus Torvalds * via device_release() above. Otherwise, the structure will 11331da177e4SLinus Torvalds * stick around until the final reference to the device is dropped. 11341da177e4SLinus Torvalds */ 11351da177e4SLinus Torvalds void device_unregister(struct device *dev) 11361da177e4SLinus Torvalds { 11371e0b2cf9SKay Sievers pr_debug("device: '%s': %s\n", dev_name(dev), __func__); 11381da177e4SLinus Torvalds device_del(dev); 11391da177e4SLinus Torvalds put_device(dev); 11401da177e4SLinus Torvalds } 11411da177e4SLinus Torvalds 114236239577Smochel@digitalimplant.org static struct device *next_device(struct klist_iter *i) 114336239577Smochel@digitalimplant.org { 114436239577Smochel@digitalimplant.org struct klist_node *n = klist_next(i); 1145f791b8c8SGreg Kroah-Hartman struct device *dev = NULL; 1146f791b8c8SGreg Kroah-Hartman struct device_private *p; 1147f791b8c8SGreg Kroah-Hartman 1148f791b8c8SGreg Kroah-Hartman if (n) { 1149f791b8c8SGreg Kroah-Hartman p = to_device_private_parent(n); 1150f791b8c8SGreg Kroah-Hartman dev = p->device; 1151f791b8c8SGreg Kroah-Hartman } 1152f791b8c8SGreg Kroah-Hartman return dev; 115336239577Smochel@digitalimplant.org } 115436239577Smochel@digitalimplant.org 11551da177e4SLinus Torvalds /** 1156e454cea2SKay Sievers * device_get_devnode - path of device node file 11576fcf53acSKay Sievers * @dev: device 1158e454cea2SKay Sievers * @mode: returned file access mode 11596fcf53acSKay Sievers * @tmp: possibly allocated string 11606fcf53acSKay Sievers * 11616fcf53acSKay Sievers * Return the relative path of a possible device node. 11626fcf53acSKay Sievers * Non-default names may need to allocate a memory to compose 11636fcf53acSKay Sievers * a name. This memory is returned in tmp and needs to be 11646fcf53acSKay Sievers * freed by the caller. 11656fcf53acSKay Sievers */ 1166e454cea2SKay Sievers const char *device_get_devnode(struct device *dev, 1167e454cea2SKay Sievers mode_t *mode, const char **tmp) 11686fcf53acSKay Sievers { 11696fcf53acSKay Sievers char *s; 11706fcf53acSKay Sievers 11716fcf53acSKay Sievers *tmp = NULL; 11726fcf53acSKay Sievers 11736fcf53acSKay Sievers /* the device type may provide a specific name */ 1174e454cea2SKay Sievers if (dev->type && dev->type->devnode) 1175e454cea2SKay Sievers *tmp = dev->type->devnode(dev, mode); 11766fcf53acSKay Sievers if (*tmp) 11776fcf53acSKay Sievers return *tmp; 11786fcf53acSKay Sievers 11796fcf53acSKay Sievers /* the class may provide a specific name */ 1180e454cea2SKay Sievers if (dev->class && dev->class->devnode) 1181e454cea2SKay Sievers *tmp = dev->class->devnode(dev, mode); 11826fcf53acSKay Sievers if (*tmp) 11836fcf53acSKay Sievers return *tmp; 11846fcf53acSKay Sievers 11856fcf53acSKay Sievers /* return name without allocation, tmp == NULL */ 11866fcf53acSKay Sievers if (strchr(dev_name(dev), '!') == NULL) 11876fcf53acSKay Sievers return dev_name(dev); 11886fcf53acSKay Sievers 11896fcf53acSKay Sievers /* replace '!' in the name with '/' */ 11906fcf53acSKay Sievers *tmp = kstrdup(dev_name(dev), GFP_KERNEL); 11916fcf53acSKay Sievers if (!*tmp) 11926fcf53acSKay Sievers return NULL; 11936fcf53acSKay Sievers while ((s = strchr(*tmp, '!'))) 11946fcf53acSKay Sievers s[0] = '/'; 11956fcf53acSKay Sievers return *tmp; 11966fcf53acSKay Sievers } 11976fcf53acSKay Sievers 11986fcf53acSKay Sievers /** 11991da177e4SLinus Torvalds * device_for_each_child - device child iterator. 1200c41455fbSRandy Dunlap * @parent: parent struct device. 12011da177e4SLinus Torvalds * @data: data for the callback. 12021da177e4SLinus Torvalds * @fn: function to be called for each device. 12031da177e4SLinus Torvalds * 1204c41455fbSRandy Dunlap * Iterate over @parent's child devices, and call @fn for each, 12051da177e4SLinus Torvalds * passing it @data. 12061da177e4SLinus Torvalds * 12071da177e4SLinus Torvalds * We check the return of @fn each time. If it returns anything 12081da177e4SLinus Torvalds * other than 0, we break out and return that value. 12091da177e4SLinus Torvalds */ 121036239577Smochel@digitalimplant.org int device_for_each_child(struct device *parent, void *data, 12114a3ad20cSGreg Kroah-Hartman int (*fn)(struct device *dev, void *data)) 12121da177e4SLinus Torvalds { 121336239577Smochel@digitalimplant.org struct klist_iter i; 12141da177e4SLinus Torvalds struct device *child; 12151da177e4SLinus Torvalds int error = 0; 12161da177e4SLinus Torvalds 1217014c90dbSGreg Kroah-Hartman if (!parent->p) 1218014c90dbSGreg Kroah-Hartman return 0; 1219014c90dbSGreg Kroah-Hartman 1220f791b8c8SGreg Kroah-Hartman klist_iter_init(&parent->p->klist_children, &i); 122136239577Smochel@digitalimplant.org while ((child = next_device(&i)) && !error) 122236239577Smochel@digitalimplant.org error = fn(child, data); 122336239577Smochel@digitalimplant.org klist_iter_exit(&i); 12241da177e4SLinus Torvalds return error; 12251da177e4SLinus Torvalds } 12261da177e4SLinus Torvalds 12275ab69981SCornelia Huck /** 12285ab69981SCornelia Huck * device_find_child - device iterator for locating a particular device. 12295ab69981SCornelia Huck * @parent: parent struct device 12305ab69981SCornelia Huck * @data: Data to pass to match function 12315ab69981SCornelia Huck * @match: Callback function to check device 12325ab69981SCornelia Huck * 12335ab69981SCornelia Huck * This is similar to the device_for_each_child() function above, but it 12345ab69981SCornelia Huck * returns a reference to a device that is 'found' for later use, as 12355ab69981SCornelia Huck * determined by the @match callback. 12365ab69981SCornelia Huck * 12375ab69981SCornelia Huck * The callback should return 0 if the device doesn't match and non-zero 12385ab69981SCornelia Huck * if it does. If the callback returns non-zero and a reference to the 12395ab69981SCornelia Huck * current device can be obtained, this function will return to the caller 12405ab69981SCornelia Huck * and not iterate over any more devices. 12415ab69981SCornelia Huck */ 12425ab69981SCornelia Huck struct device *device_find_child(struct device *parent, void *data, 12434a3ad20cSGreg Kroah-Hartman int (*match)(struct device *dev, void *data)) 12445ab69981SCornelia Huck { 12455ab69981SCornelia Huck struct klist_iter i; 12465ab69981SCornelia Huck struct device *child; 12475ab69981SCornelia Huck 12485ab69981SCornelia Huck if (!parent) 12495ab69981SCornelia Huck return NULL; 12505ab69981SCornelia Huck 1251f791b8c8SGreg Kroah-Hartman klist_iter_init(&parent->p->klist_children, &i); 12525ab69981SCornelia Huck while ((child = next_device(&i))) 12535ab69981SCornelia Huck if (match(child, data) && get_device(child)) 12545ab69981SCornelia Huck break; 12555ab69981SCornelia Huck klist_iter_exit(&i); 12565ab69981SCornelia Huck return child; 12575ab69981SCornelia Huck } 12585ab69981SCornelia Huck 12591da177e4SLinus Torvalds int __init devices_init(void) 12601da177e4SLinus Torvalds { 1261881c6cfdSGreg Kroah-Hartman devices_kset = kset_create_and_add("devices", &device_uevent_ops, NULL); 1262881c6cfdSGreg Kroah-Hartman if (!devices_kset) 1263881c6cfdSGreg Kroah-Hartman return -ENOMEM; 1264e105b8bfSDan Williams dev_kobj = kobject_create_and_add("dev", NULL); 1265e105b8bfSDan Williams if (!dev_kobj) 1266e105b8bfSDan Williams goto dev_kobj_err; 1267e105b8bfSDan Williams sysfs_dev_block_kobj = kobject_create_and_add("block", dev_kobj); 1268e105b8bfSDan Williams if (!sysfs_dev_block_kobj) 1269e105b8bfSDan Williams goto block_kobj_err; 1270e105b8bfSDan Williams sysfs_dev_char_kobj = kobject_create_and_add("char", dev_kobj); 1271e105b8bfSDan Williams if (!sysfs_dev_char_kobj) 1272e105b8bfSDan Williams goto char_kobj_err; 1273e105b8bfSDan Williams 1274881c6cfdSGreg Kroah-Hartman return 0; 1275e105b8bfSDan Williams 1276e105b8bfSDan Williams char_kobj_err: 1277e105b8bfSDan Williams kobject_put(sysfs_dev_block_kobj); 1278e105b8bfSDan Williams block_kobj_err: 1279e105b8bfSDan Williams kobject_put(dev_kobj); 1280e105b8bfSDan Williams dev_kobj_err: 1281e105b8bfSDan Williams kset_unregister(devices_kset); 1282e105b8bfSDan Williams return -ENOMEM; 12831da177e4SLinus Torvalds } 12841da177e4SLinus Torvalds 12851da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(device_for_each_child); 12865ab69981SCornelia Huck EXPORT_SYMBOL_GPL(device_find_child); 12871da177e4SLinus Torvalds 12881da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(device_initialize); 12891da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(device_add); 12901da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(device_register); 12911da177e4SLinus Torvalds 12921da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(device_del); 12931da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(device_unregister); 12941da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(get_device); 12951da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(put_device); 12961da177e4SLinus Torvalds 12971da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(device_create_file); 12981da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(device_remove_file); 129923681e47SGreg Kroah-Hartman 13000aa0dc41SMark McLoughlin struct root_device 13010aa0dc41SMark McLoughlin { 13020aa0dc41SMark McLoughlin struct device dev; 13030aa0dc41SMark McLoughlin struct module *owner; 13040aa0dc41SMark McLoughlin }; 13050aa0dc41SMark McLoughlin 13060aa0dc41SMark McLoughlin #define to_root_device(dev) container_of(dev, struct root_device, dev) 13070aa0dc41SMark McLoughlin 13080aa0dc41SMark McLoughlin static void root_device_release(struct device *dev) 13090aa0dc41SMark McLoughlin { 13100aa0dc41SMark McLoughlin kfree(to_root_device(dev)); 13110aa0dc41SMark McLoughlin } 13120aa0dc41SMark McLoughlin 13130aa0dc41SMark McLoughlin /** 13140aa0dc41SMark McLoughlin * __root_device_register - allocate and register a root device 13150aa0dc41SMark McLoughlin * @name: root device name 13160aa0dc41SMark McLoughlin * @owner: owner module of the root device, usually THIS_MODULE 13170aa0dc41SMark McLoughlin * 13180aa0dc41SMark McLoughlin * This function allocates a root device and registers it 13190aa0dc41SMark McLoughlin * using device_register(). In order to free the returned 13200aa0dc41SMark McLoughlin * device, use root_device_unregister(). 13210aa0dc41SMark McLoughlin * 13220aa0dc41SMark McLoughlin * Root devices are dummy devices which allow other devices 13230aa0dc41SMark McLoughlin * to be grouped under /sys/devices. Use this function to 13240aa0dc41SMark McLoughlin * allocate a root device and then use it as the parent of 13250aa0dc41SMark McLoughlin * any device which should appear under /sys/devices/{name} 13260aa0dc41SMark McLoughlin * 13270aa0dc41SMark McLoughlin * The /sys/devices/{name} directory will also contain a 13280aa0dc41SMark McLoughlin * 'module' symlink which points to the @owner directory 13290aa0dc41SMark McLoughlin * in sysfs. 13300aa0dc41SMark McLoughlin * 13310aa0dc41SMark McLoughlin * Note: You probably want to use root_device_register(). 13320aa0dc41SMark McLoughlin */ 13330aa0dc41SMark McLoughlin struct device *__root_device_register(const char *name, struct module *owner) 13340aa0dc41SMark McLoughlin { 13350aa0dc41SMark McLoughlin struct root_device *root; 13360aa0dc41SMark McLoughlin int err = -ENOMEM; 13370aa0dc41SMark McLoughlin 13380aa0dc41SMark McLoughlin root = kzalloc(sizeof(struct root_device), GFP_KERNEL); 13390aa0dc41SMark McLoughlin if (!root) 13400aa0dc41SMark McLoughlin return ERR_PTR(err); 13410aa0dc41SMark McLoughlin 1342acc0e90fSGreg Kroah-Hartman err = dev_set_name(&root->dev, "%s", name); 13430aa0dc41SMark McLoughlin if (err) { 13440aa0dc41SMark McLoughlin kfree(root); 13450aa0dc41SMark McLoughlin return ERR_PTR(err); 13460aa0dc41SMark McLoughlin } 13470aa0dc41SMark McLoughlin 13480aa0dc41SMark McLoughlin root->dev.release = root_device_release; 13490aa0dc41SMark McLoughlin 13500aa0dc41SMark McLoughlin err = device_register(&root->dev); 13510aa0dc41SMark McLoughlin if (err) { 13520aa0dc41SMark McLoughlin put_device(&root->dev); 13530aa0dc41SMark McLoughlin return ERR_PTR(err); 13540aa0dc41SMark McLoughlin } 13550aa0dc41SMark McLoughlin 13560aa0dc41SMark McLoughlin #ifdef CONFIG_MODULE /* gotta find a "cleaner" way to do this */ 13570aa0dc41SMark McLoughlin if (owner) { 13580aa0dc41SMark McLoughlin struct module_kobject *mk = &owner->mkobj; 13590aa0dc41SMark McLoughlin 13600aa0dc41SMark McLoughlin err = sysfs_create_link(&root->dev.kobj, &mk->kobj, "module"); 13610aa0dc41SMark McLoughlin if (err) { 13620aa0dc41SMark McLoughlin device_unregister(&root->dev); 13630aa0dc41SMark McLoughlin return ERR_PTR(err); 13640aa0dc41SMark McLoughlin } 13650aa0dc41SMark McLoughlin root->owner = owner; 13660aa0dc41SMark McLoughlin } 13670aa0dc41SMark McLoughlin #endif 13680aa0dc41SMark McLoughlin 13690aa0dc41SMark McLoughlin return &root->dev; 13700aa0dc41SMark McLoughlin } 13710aa0dc41SMark McLoughlin EXPORT_SYMBOL_GPL(__root_device_register); 13720aa0dc41SMark McLoughlin 13730aa0dc41SMark McLoughlin /** 13740aa0dc41SMark McLoughlin * root_device_unregister - unregister and free a root device 13757cbcf225SRandy Dunlap * @dev: device going away 13760aa0dc41SMark McLoughlin * 13770aa0dc41SMark McLoughlin * This function unregisters and cleans up a device that was created by 13780aa0dc41SMark McLoughlin * root_device_register(). 13790aa0dc41SMark McLoughlin */ 13800aa0dc41SMark McLoughlin void root_device_unregister(struct device *dev) 13810aa0dc41SMark McLoughlin { 13820aa0dc41SMark McLoughlin struct root_device *root = to_root_device(dev); 13830aa0dc41SMark McLoughlin 13840aa0dc41SMark McLoughlin if (root->owner) 13850aa0dc41SMark McLoughlin sysfs_remove_link(&root->dev.kobj, "module"); 13860aa0dc41SMark McLoughlin 13870aa0dc41SMark McLoughlin device_unregister(dev); 13880aa0dc41SMark McLoughlin } 13890aa0dc41SMark McLoughlin EXPORT_SYMBOL_GPL(root_device_unregister); 13900aa0dc41SMark McLoughlin 139123681e47SGreg Kroah-Hartman 139223681e47SGreg Kroah-Hartman static void device_create_release(struct device *dev) 139323681e47SGreg Kroah-Hartman { 13941e0b2cf9SKay Sievers pr_debug("device: '%s': %s\n", dev_name(dev), __func__); 139523681e47SGreg Kroah-Hartman kfree(dev); 139623681e47SGreg Kroah-Hartman } 139723681e47SGreg Kroah-Hartman 139823681e47SGreg Kroah-Hartman /** 13998882b394SGreg Kroah-Hartman * device_create_vargs - creates a device and registers it with sysfs 14008882b394SGreg Kroah-Hartman * @class: pointer to the struct class that this device should be registered to 14018882b394SGreg Kroah-Hartman * @parent: pointer to the parent struct device of this new device, if any 14028882b394SGreg Kroah-Hartman * @devt: the dev_t for the char device to be added 14038882b394SGreg Kroah-Hartman * @drvdata: the data to be added to the device for callbacks 14048882b394SGreg Kroah-Hartman * @fmt: string for the device's name 14058882b394SGreg Kroah-Hartman * @args: va_list for the device's name 14068882b394SGreg Kroah-Hartman * 14078882b394SGreg Kroah-Hartman * This function can be used by char device classes. A struct device 14088882b394SGreg Kroah-Hartman * will be created in sysfs, registered to the specified class. 14098882b394SGreg Kroah-Hartman * 14108882b394SGreg Kroah-Hartman * A "dev" file will be created, showing the dev_t for the device, if 14118882b394SGreg Kroah-Hartman * the dev_t is not 0,0. 14128882b394SGreg Kroah-Hartman * If a pointer to a parent struct device is passed in, the newly created 14138882b394SGreg Kroah-Hartman * struct device will be a child of that device in sysfs. 14148882b394SGreg Kroah-Hartman * The pointer to the struct device will be returned from the call. 14158882b394SGreg Kroah-Hartman * Any further sysfs files that might be required can be created using this 14168882b394SGreg Kroah-Hartman * pointer. 14178882b394SGreg Kroah-Hartman * 14188882b394SGreg Kroah-Hartman * Note: the struct class passed to this function must have previously 14198882b394SGreg Kroah-Hartman * been created with a call to class_create(). 14208882b394SGreg Kroah-Hartman */ 14218882b394SGreg Kroah-Hartman struct device *device_create_vargs(struct class *class, struct device *parent, 14228882b394SGreg Kroah-Hartman dev_t devt, void *drvdata, const char *fmt, 14238882b394SGreg Kroah-Hartman va_list args) 14248882b394SGreg Kroah-Hartman { 14258882b394SGreg Kroah-Hartman struct device *dev = NULL; 14268882b394SGreg Kroah-Hartman int retval = -ENODEV; 14278882b394SGreg Kroah-Hartman 14288882b394SGreg Kroah-Hartman if (class == NULL || IS_ERR(class)) 14298882b394SGreg Kroah-Hartman goto error; 14308882b394SGreg Kroah-Hartman 14318882b394SGreg Kroah-Hartman dev = kzalloc(sizeof(*dev), GFP_KERNEL); 14328882b394SGreg Kroah-Hartman if (!dev) { 14338882b394SGreg Kroah-Hartman retval = -ENOMEM; 14348882b394SGreg Kroah-Hartman goto error; 14358882b394SGreg Kroah-Hartman } 14368882b394SGreg Kroah-Hartman 14378882b394SGreg Kroah-Hartman dev->devt = devt; 14388882b394SGreg Kroah-Hartman dev->class = class; 14398882b394SGreg Kroah-Hartman dev->parent = parent; 14408882b394SGreg Kroah-Hartman dev->release = device_create_release; 14418882b394SGreg Kroah-Hartman dev_set_drvdata(dev, drvdata); 14428882b394SGreg Kroah-Hartman 14431fa5ae85SKay Sievers retval = kobject_set_name_vargs(&dev->kobj, fmt, args); 14441fa5ae85SKay Sievers if (retval) 14451fa5ae85SKay Sievers goto error; 14461fa5ae85SKay Sievers 14478882b394SGreg Kroah-Hartman retval = device_register(dev); 14488882b394SGreg Kroah-Hartman if (retval) 14498882b394SGreg Kroah-Hartman goto error; 14508882b394SGreg Kroah-Hartman 14518882b394SGreg Kroah-Hartman return dev; 14528882b394SGreg Kroah-Hartman 14538882b394SGreg Kroah-Hartman error: 1454286661b3SCornelia Huck put_device(dev); 14558882b394SGreg Kroah-Hartman return ERR_PTR(retval); 14568882b394SGreg Kroah-Hartman } 14578882b394SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(device_create_vargs); 14588882b394SGreg Kroah-Hartman 14598882b394SGreg Kroah-Hartman /** 14604e106739SGreg Kroah-Hartman * device_create - creates a device and registers it with sysfs 14618882b394SGreg Kroah-Hartman * @class: pointer to the struct class that this device should be registered to 14628882b394SGreg Kroah-Hartman * @parent: pointer to the parent struct device of this new device, if any 14638882b394SGreg Kroah-Hartman * @devt: the dev_t for the char device to be added 14648882b394SGreg Kroah-Hartman * @drvdata: the data to be added to the device for callbacks 14658882b394SGreg Kroah-Hartman * @fmt: string for the device's name 14668882b394SGreg Kroah-Hartman * 14678882b394SGreg Kroah-Hartman * This function can be used by char device classes. A struct device 14688882b394SGreg Kroah-Hartman * will be created in sysfs, registered to the specified class. 14698882b394SGreg Kroah-Hartman * 14708882b394SGreg Kroah-Hartman * A "dev" file will be created, showing the dev_t for the device, if 14718882b394SGreg Kroah-Hartman * the dev_t is not 0,0. 14728882b394SGreg Kroah-Hartman * If a pointer to a parent struct device is passed in, the newly created 14738882b394SGreg Kroah-Hartman * struct device will be a child of that device in sysfs. 14748882b394SGreg Kroah-Hartman * The pointer to the struct device will be returned from the call. 14758882b394SGreg Kroah-Hartman * Any further sysfs files that might be required can be created using this 14768882b394SGreg Kroah-Hartman * pointer. 14778882b394SGreg Kroah-Hartman * 14788882b394SGreg Kroah-Hartman * Note: the struct class passed to this function must have previously 14798882b394SGreg Kroah-Hartman * been created with a call to class_create(). 14808882b394SGreg Kroah-Hartman */ 14814e106739SGreg Kroah-Hartman struct device *device_create(struct class *class, struct device *parent, 14824e106739SGreg Kroah-Hartman dev_t devt, void *drvdata, const char *fmt, ...) 14838882b394SGreg Kroah-Hartman { 14848882b394SGreg Kroah-Hartman va_list vargs; 14858882b394SGreg Kroah-Hartman struct device *dev; 14868882b394SGreg Kroah-Hartman 14878882b394SGreg Kroah-Hartman va_start(vargs, fmt); 14888882b394SGreg Kroah-Hartman dev = device_create_vargs(class, parent, devt, drvdata, fmt, vargs); 14898882b394SGreg Kroah-Hartman va_end(vargs); 14908882b394SGreg Kroah-Hartman return dev; 14918882b394SGreg Kroah-Hartman } 14924e106739SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(device_create); 14938882b394SGreg Kroah-Hartman 1494cd35449bSDave Young static int __match_devt(struct device *dev, void *data) 149523681e47SGreg Kroah-Hartman { 1496cd35449bSDave Young dev_t *devt = data; 149723681e47SGreg Kroah-Hartman 1498cd35449bSDave Young return dev->devt == *devt; 1499775b64d2SRafael J. Wysocki } 150023681e47SGreg Kroah-Hartman 1501775b64d2SRafael J. Wysocki /** 1502775b64d2SRafael J. Wysocki * device_destroy - removes a device that was created with device_create() 1503775b64d2SRafael J. Wysocki * @class: pointer to the struct class that this device was registered with 1504775b64d2SRafael J. Wysocki * @devt: the dev_t of the device that was previously registered 1505775b64d2SRafael J. Wysocki * 1506775b64d2SRafael J. Wysocki * This call unregisters and cleans up a device that was created with a 1507775b64d2SRafael J. Wysocki * call to device_create(). 1508775b64d2SRafael J. Wysocki */ 1509775b64d2SRafael J. Wysocki void device_destroy(struct class *class, dev_t devt) 1510775b64d2SRafael J. Wysocki { 1511775b64d2SRafael J. Wysocki struct device *dev; 1512775b64d2SRafael J. Wysocki 1513695794aeSGreg Kroah-Hartman dev = class_find_device(class, NULL, &devt, __match_devt); 1514cd35449bSDave Young if (dev) { 1515cd35449bSDave Young put_device(dev); 151623681e47SGreg Kroah-Hartman device_unregister(dev); 151723681e47SGreg Kroah-Hartman } 1518cd35449bSDave Young } 151923681e47SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(device_destroy); 1520a2de48caSGreg Kroah-Hartman 1521a2de48caSGreg Kroah-Hartman /** 1522a2de48caSGreg Kroah-Hartman * device_rename - renames a device 1523a2de48caSGreg Kroah-Hartman * @dev: the pointer to the struct device to be renamed 1524a2de48caSGreg Kroah-Hartman * @new_name: the new name of the device 1525030c1d2bSEric W. Biederman * 1526030c1d2bSEric W. Biederman * It is the responsibility of the caller to provide mutual 1527030c1d2bSEric W. Biederman * exclusion between two different calls of device_rename 1528030c1d2bSEric W. Biederman * on the same device to ensure that new_name is valid and 1529030c1d2bSEric W. Biederman * won't conflict with other devices. 1530a2de48caSGreg Kroah-Hartman */ 1531a2de48caSGreg Kroah-Hartman int device_rename(struct device *dev, char *new_name) 1532a2de48caSGreg Kroah-Hartman { 1533a2de48caSGreg Kroah-Hartman char *old_class_name = NULL; 1534a2de48caSGreg Kroah-Hartman char *new_class_name = NULL; 15352ee97cafSCornelia Huck char *old_device_name = NULL; 1536a2de48caSGreg Kroah-Hartman int error; 1537a2de48caSGreg Kroah-Hartman 1538a2de48caSGreg Kroah-Hartman dev = get_device(dev); 1539a2de48caSGreg Kroah-Hartman if (!dev) 1540a2de48caSGreg Kroah-Hartman return -EINVAL; 1541a2de48caSGreg Kroah-Hartman 15421e0b2cf9SKay Sievers pr_debug("device: '%s': %s: renaming to '%s'\n", dev_name(dev), 15432b3a302aSHarvey Harrison __func__, new_name); 1544a2de48caSGreg Kroah-Hartman 154599ef3ef8SKay Sievers #ifdef CONFIG_SYSFS_DEPRECATED 1546a2de48caSGreg Kroah-Hartman if ((dev->class) && (dev->parent)) 1547a2de48caSGreg Kroah-Hartman old_class_name = make_class_name(dev->class->name, &dev->kobj); 154899ef3ef8SKay Sievers #endif 1549a2de48caSGreg Kroah-Hartman 15501fa5ae85SKay Sievers old_device_name = kstrdup(dev_name(dev), GFP_KERNEL); 15512ee97cafSCornelia Huck if (!old_device_name) { 1552952ab431SJesper Juhl error = -ENOMEM; 15532ee97cafSCornelia Huck goto out; 1554952ab431SJesper Juhl } 1555a2de48caSGreg Kroah-Hartman 1556a2de48caSGreg Kroah-Hartman error = kobject_rename(&dev->kobj, new_name); 15571fa5ae85SKay Sievers if (error) 15582ee97cafSCornelia Huck goto out; 1559a2de48caSGreg Kroah-Hartman 156099ef3ef8SKay Sievers #ifdef CONFIG_SYSFS_DEPRECATED 1561a2de48caSGreg Kroah-Hartman if (old_class_name) { 1562a2de48caSGreg Kroah-Hartman new_class_name = make_class_name(dev->class->name, &dev->kobj); 1563a2de48caSGreg Kroah-Hartman if (new_class_name) { 156436ce6dadSCornelia Huck error = sysfs_create_link_nowarn(&dev->parent->kobj, 156536ce6dadSCornelia Huck &dev->kobj, 156636ce6dadSCornelia Huck new_class_name); 15672ee97cafSCornelia Huck if (error) 15682ee97cafSCornelia Huck goto out; 1569a2de48caSGreg Kroah-Hartman sysfs_remove_link(&dev->parent->kobj, old_class_name); 1570a2de48caSGreg Kroah-Hartman } 1571a2de48caSGreg Kroah-Hartman } 157260b8cabdSKay Sievers #else 1573a2de48caSGreg Kroah-Hartman if (dev->class) { 157436ce6dadSCornelia Huck error = sysfs_create_link_nowarn(&dev->class->p->class_subsys.kobj, 15751e0b2cf9SKay Sievers &dev->kobj, dev_name(dev)); 15760599ad53SStephen Hemminger if (error) 15770599ad53SStephen Hemminger goto out; 15781fbfee6cSGreg Kroah-Hartman sysfs_remove_link(&dev->class->p->class_subsys.kobj, 15797c71448bSGreg Kroah-Hartman old_device_name); 15802ee97cafSCornelia Huck } 158160b8cabdSKay Sievers #endif 158260b8cabdSKay Sievers 15832ee97cafSCornelia Huck out: 1584a2de48caSGreg Kroah-Hartman put_device(dev); 1585a2de48caSGreg Kroah-Hartman 1586a2de48caSGreg Kroah-Hartman kfree(new_class_name); 1587952ab431SJesper Juhl kfree(old_class_name); 15882ee97cafSCornelia Huck kfree(old_device_name); 1589a2de48caSGreg Kroah-Hartman 1590a2de48caSGreg Kroah-Hartman return error; 1591a2de48caSGreg Kroah-Hartman } 1592a2807dbcSJohannes Berg EXPORT_SYMBOL_GPL(device_rename); 15938a82472fSCornelia Huck 15948a82472fSCornelia Huck static int device_move_class_links(struct device *dev, 15958a82472fSCornelia Huck struct device *old_parent, 15968a82472fSCornelia Huck struct device *new_parent) 15978a82472fSCornelia Huck { 1598f7f3461dSGreg Kroah-Hartman int error = 0; 15998a82472fSCornelia Huck #ifdef CONFIG_SYSFS_DEPRECATED 16008a82472fSCornelia Huck char *class_name; 16018a82472fSCornelia Huck 16028a82472fSCornelia Huck class_name = make_class_name(dev->class->name, &dev->kobj); 16038a82472fSCornelia Huck if (!class_name) { 1604cb360bbfSCornelia Huck error = -ENOMEM; 16058a82472fSCornelia Huck goto out; 16068a82472fSCornelia Huck } 16078a82472fSCornelia Huck if (old_parent) { 16088a82472fSCornelia Huck sysfs_remove_link(&dev->kobj, "device"); 16098a82472fSCornelia Huck sysfs_remove_link(&old_parent->kobj, class_name); 16108a82472fSCornelia Huck } 1611c744aeaeSCornelia Huck if (new_parent) { 1612c744aeaeSCornelia Huck error = sysfs_create_link(&dev->kobj, &new_parent->kobj, 1613c744aeaeSCornelia Huck "device"); 16148a82472fSCornelia Huck if (error) 16158a82472fSCornelia Huck goto out; 1616c744aeaeSCornelia Huck error = sysfs_create_link(&new_parent->kobj, &dev->kobj, 1617c744aeaeSCornelia Huck class_name); 16188a82472fSCornelia Huck if (error) 16198a82472fSCornelia Huck sysfs_remove_link(&dev->kobj, "device"); 16204a3ad20cSGreg Kroah-Hartman } else 1621c744aeaeSCornelia Huck error = 0; 16228a82472fSCornelia Huck out: 16238a82472fSCornelia Huck kfree(class_name); 16248a82472fSCornelia Huck return error; 16258a82472fSCornelia Huck #else 1626f7f3461dSGreg Kroah-Hartman if (old_parent) 1627f7f3461dSGreg Kroah-Hartman sysfs_remove_link(&dev->kobj, "device"); 1628f7f3461dSGreg Kroah-Hartman if (new_parent) 1629f7f3461dSGreg Kroah-Hartman error = sysfs_create_link(&dev->kobj, &new_parent->kobj, 1630f7f3461dSGreg Kroah-Hartman "device"); 1631f7f3461dSGreg Kroah-Hartman return error; 16328a82472fSCornelia Huck #endif 16338a82472fSCornelia Huck } 16348a82472fSCornelia Huck 16358a82472fSCornelia Huck /** 16368a82472fSCornelia Huck * device_move - moves a device to a new parent 16378a82472fSCornelia Huck * @dev: the pointer to the struct device to be moved 1638c744aeaeSCornelia Huck * @new_parent: the new parent of the device (can by NULL) 1639ffa6a705SCornelia Huck * @dpm_order: how to reorder the dpm_list 16408a82472fSCornelia Huck */ 1641ffa6a705SCornelia Huck int device_move(struct device *dev, struct device *new_parent, 1642ffa6a705SCornelia Huck enum dpm_order dpm_order) 16438a82472fSCornelia Huck { 16448a82472fSCornelia Huck int error; 16458a82472fSCornelia Huck struct device *old_parent; 1646c744aeaeSCornelia Huck struct kobject *new_parent_kobj; 16478a82472fSCornelia Huck 16488a82472fSCornelia Huck dev = get_device(dev); 16498a82472fSCornelia Huck if (!dev) 16508a82472fSCornelia Huck return -EINVAL; 16518a82472fSCornelia Huck 1652ffa6a705SCornelia Huck device_pm_lock(); 16538a82472fSCornelia Huck new_parent = get_device(new_parent); 1654c744aeaeSCornelia Huck new_parent_kobj = get_device_parent(dev, new_parent); 165563b6971aSCornelia Huck 16561e0b2cf9SKay Sievers pr_debug("device: '%s': %s: moving to '%s'\n", dev_name(dev), 16571e0b2cf9SKay Sievers __func__, new_parent ? dev_name(new_parent) : "<NULL>"); 1658c744aeaeSCornelia Huck error = kobject_move(&dev->kobj, new_parent_kobj); 16598a82472fSCornelia Huck if (error) { 166063b6971aSCornelia Huck cleanup_glue_dir(dev, new_parent_kobj); 16618a82472fSCornelia Huck put_device(new_parent); 16628a82472fSCornelia Huck goto out; 16638a82472fSCornelia Huck } 16648a82472fSCornelia Huck old_parent = dev->parent; 16658a82472fSCornelia Huck dev->parent = new_parent; 16668a82472fSCornelia Huck if (old_parent) 1667f791b8c8SGreg Kroah-Hartman klist_remove(&dev->p->knode_parent); 16680d358f22SYinghai Lu if (new_parent) { 1669f791b8c8SGreg Kroah-Hartman klist_add_tail(&dev->p->knode_parent, 1670f791b8c8SGreg Kroah-Hartman &new_parent->p->klist_children); 16710d358f22SYinghai Lu set_dev_node(dev, dev_to_node(new_parent)); 16720d358f22SYinghai Lu } 16730d358f22SYinghai Lu 16748a82472fSCornelia Huck if (!dev->class) 16758a82472fSCornelia Huck goto out_put; 16768a82472fSCornelia Huck error = device_move_class_links(dev, old_parent, new_parent); 16778a82472fSCornelia Huck if (error) { 16788a82472fSCornelia Huck /* We ignore errors on cleanup since we're hosed anyway... */ 16798a82472fSCornelia Huck device_move_class_links(dev, new_parent, old_parent); 16808a82472fSCornelia Huck if (!kobject_move(&dev->kobj, &old_parent->kobj)) { 1681c744aeaeSCornelia Huck if (new_parent) 1682f791b8c8SGreg Kroah-Hartman klist_remove(&dev->p->knode_parent); 16830d358f22SYinghai Lu dev->parent = old_parent; 16840d358f22SYinghai Lu if (old_parent) { 1685f791b8c8SGreg Kroah-Hartman klist_add_tail(&dev->p->knode_parent, 1686f791b8c8SGreg Kroah-Hartman &old_parent->p->klist_children); 16870d358f22SYinghai Lu set_dev_node(dev, dev_to_node(old_parent)); 16880d358f22SYinghai Lu } 16898a82472fSCornelia Huck } 169063b6971aSCornelia Huck cleanup_glue_dir(dev, new_parent_kobj); 16918a82472fSCornelia Huck put_device(new_parent); 16928a82472fSCornelia Huck goto out; 16938a82472fSCornelia Huck } 1694ffa6a705SCornelia Huck switch (dpm_order) { 1695ffa6a705SCornelia Huck case DPM_ORDER_NONE: 1696ffa6a705SCornelia Huck break; 1697ffa6a705SCornelia Huck case DPM_ORDER_DEV_AFTER_PARENT: 1698ffa6a705SCornelia Huck device_pm_move_after(dev, new_parent); 1699ffa6a705SCornelia Huck break; 1700ffa6a705SCornelia Huck case DPM_ORDER_PARENT_BEFORE_DEV: 1701ffa6a705SCornelia Huck device_pm_move_before(new_parent, dev); 1702ffa6a705SCornelia Huck break; 1703ffa6a705SCornelia Huck case DPM_ORDER_DEV_LAST: 1704ffa6a705SCornelia Huck device_pm_move_last(dev); 1705ffa6a705SCornelia Huck break; 1706ffa6a705SCornelia Huck } 17078a82472fSCornelia Huck out_put: 17088a82472fSCornelia Huck put_device(old_parent); 17098a82472fSCornelia Huck out: 1710ffa6a705SCornelia Huck device_pm_unlock(); 17118a82472fSCornelia Huck put_device(dev); 17128a82472fSCornelia Huck return error; 17138a82472fSCornelia Huck } 17148a82472fSCornelia Huck EXPORT_SYMBOL_GPL(device_move); 171537b0c020SGreg Kroah-Hartman 171637b0c020SGreg Kroah-Hartman /** 171737b0c020SGreg Kroah-Hartman * device_shutdown - call ->shutdown() on each device to shutdown. 171837b0c020SGreg Kroah-Hartman */ 171937b0c020SGreg Kroah-Hartman void device_shutdown(void) 172037b0c020SGreg Kroah-Hartman { 172137b0c020SGreg Kroah-Hartman struct device *dev, *devn; 172237b0c020SGreg Kroah-Hartman 172337b0c020SGreg Kroah-Hartman list_for_each_entry_safe_reverse(dev, devn, &devices_kset->list, 172437b0c020SGreg Kroah-Hartman kobj.entry) { 172537b0c020SGreg Kroah-Hartman if (dev->bus && dev->bus->shutdown) { 172637b0c020SGreg Kroah-Hartman dev_dbg(dev, "shutdown\n"); 172737b0c020SGreg Kroah-Hartman dev->bus->shutdown(dev); 172837b0c020SGreg Kroah-Hartman } else if (dev->driver && dev->driver->shutdown) { 172937b0c020SGreg Kroah-Hartman dev_dbg(dev, "shutdown\n"); 173037b0c020SGreg Kroah-Hartman dev->driver->shutdown(dev); 173137b0c020SGreg Kroah-Hartman } 173237b0c020SGreg Kroah-Hartman } 1733e105b8bfSDan Williams kobject_put(sysfs_dev_char_kobj); 1734e105b8bfSDan Williams kobject_put(sysfs_dev_block_kobj); 1735e105b8bfSDan Williams kobject_put(dev_kobj); 1736401097eaSShaohua Li async_synchronize_full(); 173737b0c020SGreg Kroah-Hartman } 1738