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 { 593589972eSAlan Stern struct device_driver *drv; 603589972eSAlan Stern 613589972eSAlan Stern /* dev->driver can change to NULL underneath us because of unbinding, 623589972eSAlan Stern * so be careful about accessing it. dev->bus and dev->class should 633589972eSAlan Stern * never change once they are set, so they don't need special care. 643589972eSAlan Stern */ 653589972eSAlan Stern drv = ACCESS_ONCE(dev->driver); 663589972eSAlan Stern return drv ? drv->name : 67a456b702SJean Delvare (dev->bus ? dev->bus->name : 68a456b702SJean Delvare (dev->class ? dev->class->name : "")); 693e95637aSAlan Stern } 70310a922dSMatthew Wilcox EXPORT_SYMBOL(dev_driver_string); 713e95637aSAlan Stern 721da177e4SLinus Torvalds #define to_dev(obj) container_of(obj, struct device, kobj) 731da177e4SLinus Torvalds #define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr) 741da177e4SLinus Torvalds 754a3ad20cSGreg Kroah-Hartman static ssize_t dev_attr_show(struct kobject *kobj, struct attribute *attr, 764a3ad20cSGreg Kroah-Hartman char *buf) 771da177e4SLinus Torvalds { 781da177e4SLinus Torvalds struct device_attribute *dev_attr = to_dev_attr(attr); 791da177e4SLinus Torvalds struct device *dev = to_dev(kobj); 804a0c20bfSDmitry Torokhov ssize_t ret = -EIO; 811da177e4SLinus Torvalds 821da177e4SLinus Torvalds if (dev_attr->show) 8354b6f35cSYani Ioannou ret = dev_attr->show(dev, dev_attr, buf); 84815d2d50SAndrew Morton if (ret >= (ssize_t)PAGE_SIZE) { 85815d2d50SAndrew Morton print_symbol("dev_attr_show: %s returned bad count\n", 86815d2d50SAndrew Morton (unsigned long)dev_attr->show); 87815d2d50SAndrew Morton } 881da177e4SLinus Torvalds return ret; 891da177e4SLinus Torvalds } 901da177e4SLinus Torvalds 914a3ad20cSGreg Kroah-Hartman static ssize_t dev_attr_store(struct kobject *kobj, struct attribute *attr, 921da177e4SLinus Torvalds const char *buf, size_t count) 931da177e4SLinus Torvalds { 941da177e4SLinus Torvalds struct device_attribute *dev_attr = to_dev_attr(attr); 951da177e4SLinus Torvalds struct device *dev = to_dev(kobj); 964a0c20bfSDmitry Torokhov ssize_t ret = -EIO; 971da177e4SLinus Torvalds 981da177e4SLinus Torvalds if (dev_attr->store) 9954b6f35cSYani Ioannou ret = dev_attr->store(dev, dev_attr, buf, count); 1001da177e4SLinus Torvalds return ret; 1011da177e4SLinus Torvalds } 1021da177e4SLinus Torvalds 10352cf25d0SEmese Revfy static const struct sysfs_ops dev_sysfs_ops = { 1041da177e4SLinus Torvalds .show = dev_attr_show, 1051da177e4SLinus Torvalds .store = dev_attr_store, 1061da177e4SLinus Torvalds }; 1071da177e4SLinus Torvalds 1081da177e4SLinus Torvalds 1091da177e4SLinus Torvalds /** 1101da177e4SLinus Torvalds * device_release - free device structure. 1111da177e4SLinus Torvalds * @kobj: device's kobject. 1121da177e4SLinus Torvalds * 1131da177e4SLinus Torvalds * This is called once the reference count for the object 1141da177e4SLinus Torvalds * reaches 0. We forward the call to the device's release 1151da177e4SLinus Torvalds * method, which should handle actually freeing the structure. 1161da177e4SLinus Torvalds */ 1171da177e4SLinus Torvalds static void device_release(struct kobject *kobj) 1181da177e4SLinus Torvalds { 1191da177e4SLinus Torvalds struct device *dev = to_dev(kobj); 120fb069a5dSGreg Kroah-Hartman struct device_private *p = dev->p; 1211da177e4SLinus Torvalds 1221da177e4SLinus Torvalds if (dev->release) 1231da177e4SLinus Torvalds dev->release(dev); 124f9f852dfSKay Sievers else if (dev->type && dev->type->release) 125f9f852dfSKay Sievers dev->type->release(dev); 1262620efefSGreg Kroah-Hartman else if (dev->class && dev->class->dev_release) 1272620efefSGreg Kroah-Hartman dev->class->dev_release(dev); 128f810a5cfSArjan van de Ven else 129f810a5cfSArjan van de Ven WARN(1, KERN_ERR "Device '%s' does not have a release() " 1304a3ad20cSGreg Kroah-Hartman "function, it is broken and must be fixed.\n", 1311e0b2cf9SKay Sievers dev_name(dev)); 132fb069a5dSGreg Kroah-Hartman kfree(p); 1331da177e4SLinus Torvalds } 1341da177e4SLinus Torvalds 1358f4afc41SGreg Kroah-Hartman static struct kobj_type device_ktype = { 1361da177e4SLinus Torvalds .release = device_release, 1371da177e4SLinus Torvalds .sysfs_ops = &dev_sysfs_ops, 1381da177e4SLinus Torvalds }; 1391da177e4SLinus Torvalds 1401da177e4SLinus Torvalds 141312c004dSKay Sievers static int dev_uevent_filter(struct kset *kset, struct kobject *kobj) 1421da177e4SLinus Torvalds { 1431da177e4SLinus Torvalds struct kobj_type *ktype = get_ktype(kobj); 1441da177e4SLinus Torvalds 1458f4afc41SGreg Kroah-Hartman if (ktype == &device_ktype) { 1461da177e4SLinus Torvalds struct device *dev = to_dev(kobj); 1471da177e4SLinus Torvalds if (dev->bus) 1481da177e4SLinus Torvalds return 1; 14923681e47SGreg Kroah-Hartman if (dev->class) 15023681e47SGreg Kroah-Hartman return 1; 1511da177e4SLinus Torvalds } 1521da177e4SLinus Torvalds return 0; 1531da177e4SLinus Torvalds } 1541da177e4SLinus Torvalds 155312c004dSKay Sievers static const char *dev_uevent_name(struct kset *kset, struct kobject *kobj) 1561da177e4SLinus Torvalds { 1571da177e4SLinus Torvalds struct device *dev = to_dev(kobj); 1581da177e4SLinus Torvalds 15923681e47SGreg Kroah-Hartman if (dev->bus) 1601da177e4SLinus Torvalds return dev->bus->name; 16123681e47SGreg Kroah-Hartman if (dev->class) 16223681e47SGreg Kroah-Hartman return dev->class->name; 16323681e47SGreg Kroah-Hartman return NULL; 1641da177e4SLinus Torvalds } 1651da177e4SLinus Torvalds 1667eff2e7aSKay Sievers static int dev_uevent(struct kset *kset, struct kobject *kobj, 1677eff2e7aSKay Sievers struct kobj_uevent_env *env) 1681da177e4SLinus Torvalds { 1691da177e4SLinus Torvalds struct device *dev = to_dev(kobj); 1701da177e4SLinus Torvalds int retval = 0; 1711da177e4SLinus Torvalds 1726fcf53acSKay Sievers /* add device node properties if present */ 17323681e47SGreg Kroah-Hartman if (MAJOR(dev->devt)) { 1746fcf53acSKay Sievers const char *tmp; 1756fcf53acSKay Sievers const char *name; 176e454cea2SKay Sievers mode_t mode = 0; 1776fcf53acSKay Sievers 1787eff2e7aSKay Sievers add_uevent_var(env, "MAJOR=%u", MAJOR(dev->devt)); 1797eff2e7aSKay Sievers add_uevent_var(env, "MINOR=%u", MINOR(dev->devt)); 180e454cea2SKay Sievers name = device_get_devnode(dev, &mode, &tmp); 1816fcf53acSKay Sievers if (name) { 1826fcf53acSKay Sievers add_uevent_var(env, "DEVNAME=%s", name); 1836fcf53acSKay Sievers kfree(tmp); 184e454cea2SKay Sievers if (mode) 185e454cea2SKay Sievers add_uevent_var(env, "DEVMODE=%#o", mode & 0777); 1866fcf53acSKay Sievers } 18723681e47SGreg Kroah-Hartman } 18823681e47SGreg Kroah-Hartman 189414264f9SKay Sievers if (dev->type && dev->type->name) 1907eff2e7aSKay Sievers add_uevent_var(env, "DEVTYPE=%s", dev->type->name); 191414264f9SKay Sievers 192239378f1SKay Sievers if (dev->driver) 1937eff2e7aSKay Sievers add_uevent_var(env, "DRIVER=%s", dev->driver->name); 194239378f1SKay Sievers 195a87cb2acSKay Sievers #ifdef CONFIG_SYSFS_DEPRECATED 196239378f1SKay Sievers if (dev->class) { 197239378f1SKay Sievers struct device *parent = dev->parent; 198239378f1SKay Sievers 199239378f1SKay Sievers /* find first bus device in parent chain */ 200239378f1SKay Sievers while (parent && !parent->bus) 201239378f1SKay Sievers parent = parent->parent; 202239378f1SKay Sievers if (parent && parent->bus) { 203239378f1SKay Sievers const char *path; 204239378f1SKay Sievers 205239378f1SKay Sievers path = kobject_get_path(&parent->kobj, GFP_KERNEL); 2062c7afd12SKay Sievers if (path) { 2077eff2e7aSKay Sievers add_uevent_var(env, "PHYSDEVPATH=%s", path); 208239378f1SKay Sievers kfree(path); 2092c7afd12SKay Sievers } 210239378f1SKay Sievers 2117eff2e7aSKay Sievers add_uevent_var(env, "PHYSDEVBUS=%s", parent->bus->name); 212239378f1SKay Sievers 213239378f1SKay Sievers if (parent->driver) 2147eff2e7aSKay Sievers add_uevent_var(env, "PHYSDEVDRIVER=%s", 2157eff2e7aSKay Sievers parent->driver->name); 216239378f1SKay Sievers } 217239378f1SKay Sievers } else if (dev->bus) { 2187eff2e7aSKay Sievers add_uevent_var(env, "PHYSDEVBUS=%s", dev->bus->name); 219239378f1SKay Sievers 220239378f1SKay Sievers if (dev->driver) 2214a3ad20cSGreg Kroah-Hartman add_uevent_var(env, "PHYSDEVDRIVER=%s", 2224a3ad20cSGreg Kroah-Hartman dev->driver->name); 223d81d9d6bSKay Sievers } 224239378f1SKay Sievers #endif 2251da177e4SLinus Torvalds 2261da177e4SLinus Torvalds /* have the bus specific function add its stuff */ 2277eff2e7aSKay Sievers if (dev->bus && dev->bus->uevent) { 2287eff2e7aSKay Sievers retval = dev->bus->uevent(dev, env); 229f9f852dfSKay Sievers if (retval) 2307dc72b28SGreg Kroah-Hartman pr_debug("device: '%s': %s: bus uevent() returned %d\n", 2311e0b2cf9SKay Sievers dev_name(dev), __func__, retval); 2321da177e4SLinus Torvalds } 2331da177e4SLinus Torvalds 2342620efefSGreg Kroah-Hartman /* have the class specific function add its stuff */ 2357eff2e7aSKay Sievers if (dev->class && dev->class->dev_uevent) { 2367eff2e7aSKay Sievers retval = dev->class->dev_uevent(dev, env); 237f9f852dfSKay Sievers if (retval) 2387dc72b28SGreg Kroah-Hartman pr_debug("device: '%s': %s: class uevent() " 2391e0b2cf9SKay Sievers "returned %d\n", dev_name(dev), 2402b3a302aSHarvey Harrison __func__, retval); 2412620efefSGreg Kroah-Hartman } 242f9f852dfSKay Sievers 243f9f852dfSKay Sievers /* have the device type specific fuction add its stuff */ 2447eff2e7aSKay Sievers if (dev->type && dev->type->uevent) { 2457eff2e7aSKay Sievers retval = dev->type->uevent(dev, env); 246f9f852dfSKay Sievers if (retval) 2477dc72b28SGreg Kroah-Hartman pr_debug("device: '%s': %s: dev_type uevent() " 2481e0b2cf9SKay Sievers "returned %d\n", dev_name(dev), 2492b3a302aSHarvey Harrison __func__, retval); 2502620efefSGreg Kroah-Hartman } 2512620efefSGreg Kroah-Hartman 2521da177e4SLinus Torvalds return retval; 2531da177e4SLinus Torvalds } 2541da177e4SLinus Torvalds 2559cd43611SEmese Revfy static const struct kset_uevent_ops device_uevent_ops = { 256312c004dSKay Sievers .filter = dev_uevent_filter, 257312c004dSKay Sievers .name = dev_uevent_name, 258312c004dSKay Sievers .uevent = dev_uevent, 2591da177e4SLinus Torvalds }; 2601da177e4SLinus Torvalds 26116574dccSKay Sievers static ssize_t show_uevent(struct device *dev, struct device_attribute *attr, 26216574dccSKay Sievers char *buf) 26316574dccSKay Sievers { 26416574dccSKay Sievers struct kobject *top_kobj; 26516574dccSKay Sievers struct kset *kset; 2667eff2e7aSKay Sievers struct kobj_uevent_env *env = NULL; 26716574dccSKay Sievers int i; 26816574dccSKay Sievers size_t count = 0; 26916574dccSKay Sievers int retval; 27016574dccSKay Sievers 27116574dccSKay Sievers /* search the kset, the device belongs to */ 27216574dccSKay Sievers top_kobj = &dev->kobj; 2735c5daf65SKay Sievers while (!top_kobj->kset && top_kobj->parent) 27416574dccSKay Sievers top_kobj = top_kobj->parent; 27516574dccSKay Sievers if (!top_kobj->kset) 27616574dccSKay Sievers goto out; 2775c5daf65SKay Sievers 27816574dccSKay Sievers kset = top_kobj->kset; 27916574dccSKay Sievers if (!kset->uevent_ops || !kset->uevent_ops->uevent) 28016574dccSKay Sievers goto out; 28116574dccSKay Sievers 28216574dccSKay Sievers /* respect filter */ 28316574dccSKay Sievers if (kset->uevent_ops && kset->uevent_ops->filter) 28416574dccSKay Sievers if (!kset->uevent_ops->filter(kset, &dev->kobj)) 28516574dccSKay Sievers goto out; 28616574dccSKay Sievers 2877eff2e7aSKay Sievers env = kzalloc(sizeof(struct kobj_uevent_env), GFP_KERNEL); 2887eff2e7aSKay Sievers if (!env) 289c7308c81SGreg Kroah-Hartman return -ENOMEM; 290c7308c81SGreg Kroah-Hartman 29116574dccSKay Sievers /* let the kset specific function add its keys */ 2927eff2e7aSKay Sievers retval = kset->uevent_ops->uevent(kset, &dev->kobj, env); 29316574dccSKay Sievers if (retval) 29416574dccSKay Sievers goto out; 29516574dccSKay Sievers 29616574dccSKay Sievers /* copy keys to file */ 2977eff2e7aSKay Sievers for (i = 0; i < env->envp_idx; i++) 2987eff2e7aSKay Sievers count += sprintf(&buf[count], "%s\n", env->envp[i]); 29916574dccSKay Sievers out: 3007eff2e7aSKay Sievers kfree(env); 30116574dccSKay Sievers return count; 30216574dccSKay Sievers } 30316574dccSKay Sievers 304a7fd6706SKay Sievers static ssize_t store_uevent(struct device *dev, struct device_attribute *attr, 305a7fd6706SKay Sievers const char *buf, size_t count) 306a7fd6706SKay Sievers { 30760a96a59SKay Sievers enum kobject_action action; 30860a96a59SKay Sievers 3093f5468c9SKay Sievers if (kobject_action_type(buf, count, &action) == 0) 31060a96a59SKay Sievers kobject_uevent(&dev->kobj, action); 3113f5468c9SKay Sievers else 3123f5468c9SKay Sievers dev_err(dev, "uevent: unknown action-string\n"); 313a7fd6706SKay Sievers return count; 314a7fd6706SKay Sievers } 315a7fd6706SKay Sievers 316ad6a1e1cSTejun Heo static struct device_attribute uevent_attr = 317ad6a1e1cSTejun Heo __ATTR(uevent, S_IRUGO | S_IWUSR, show_uevent, store_uevent); 318ad6a1e1cSTejun Heo 319621a1672SDmitry Torokhov static int device_add_attributes(struct device *dev, 320621a1672SDmitry Torokhov struct device_attribute *attrs) 321de0ff00dSGreg Kroah-Hartman { 322de0ff00dSGreg Kroah-Hartman int error = 0; 323621a1672SDmitry Torokhov int i; 324de0ff00dSGreg Kroah-Hartman 325621a1672SDmitry Torokhov if (attrs) { 326621a1672SDmitry Torokhov for (i = 0; attr_name(attrs[i]); i++) { 327621a1672SDmitry Torokhov error = device_create_file(dev, &attrs[i]); 328621a1672SDmitry Torokhov if (error) 329621a1672SDmitry Torokhov break; 330621a1672SDmitry Torokhov } 331621a1672SDmitry Torokhov if (error) 332de0ff00dSGreg Kroah-Hartman while (--i >= 0) 333621a1672SDmitry Torokhov device_remove_file(dev, &attrs[i]); 334de0ff00dSGreg Kroah-Hartman } 335de0ff00dSGreg Kroah-Hartman return error; 336de0ff00dSGreg Kroah-Hartman } 337de0ff00dSGreg Kroah-Hartman 338621a1672SDmitry Torokhov static void device_remove_attributes(struct device *dev, 339621a1672SDmitry Torokhov struct device_attribute *attrs) 340de0ff00dSGreg Kroah-Hartman { 341de0ff00dSGreg Kroah-Hartman int i; 342621a1672SDmitry Torokhov 343621a1672SDmitry Torokhov if (attrs) 344621a1672SDmitry Torokhov for (i = 0; attr_name(attrs[i]); i++) 345621a1672SDmitry Torokhov device_remove_file(dev, &attrs[i]); 346621a1672SDmitry Torokhov } 347621a1672SDmitry Torokhov 348621a1672SDmitry Torokhov static int device_add_groups(struct device *dev, 349a4dbd674SDavid Brownell const struct attribute_group **groups) 350621a1672SDmitry Torokhov { 351621a1672SDmitry Torokhov int error = 0; 352621a1672SDmitry Torokhov int i; 353621a1672SDmitry Torokhov 354621a1672SDmitry Torokhov if (groups) { 355621a1672SDmitry Torokhov for (i = 0; groups[i]; i++) { 356621a1672SDmitry Torokhov error = sysfs_create_group(&dev->kobj, groups[i]); 357621a1672SDmitry Torokhov if (error) { 358621a1672SDmitry Torokhov while (--i >= 0) 3594a3ad20cSGreg Kroah-Hartman sysfs_remove_group(&dev->kobj, 3604a3ad20cSGreg Kroah-Hartman groups[i]); 361621a1672SDmitry Torokhov break; 362de0ff00dSGreg Kroah-Hartman } 363de0ff00dSGreg Kroah-Hartman } 364de0ff00dSGreg Kroah-Hartman } 365621a1672SDmitry Torokhov return error; 366621a1672SDmitry Torokhov } 367621a1672SDmitry Torokhov 368621a1672SDmitry Torokhov static void device_remove_groups(struct device *dev, 369a4dbd674SDavid Brownell const struct attribute_group **groups) 370621a1672SDmitry Torokhov { 371621a1672SDmitry Torokhov int i; 372621a1672SDmitry Torokhov 373621a1672SDmitry Torokhov if (groups) 374621a1672SDmitry Torokhov for (i = 0; groups[i]; i++) 375621a1672SDmitry Torokhov sysfs_remove_group(&dev->kobj, groups[i]); 376621a1672SDmitry Torokhov } 377de0ff00dSGreg Kroah-Hartman 3782620efefSGreg Kroah-Hartman static int device_add_attrs(struct device *dev) 3792620efefSGreg Kroah-Hartman { 3802620efefSGreg Kroah-Hartman struct class *class = dev->class; 381f9f852dfSKay Sievers struct device_type *type = dev->type; 382621a1672SDmitry Torokhov int error; 3832620efefSGreg Kroah-Hartman 384621a1672SDmitry Torokhov if (class) { 385621a1672SDmitry Torokhov error = device_add_attributes(dev, class->dev_attrs); 3862620efefSGreg Kroah-Hartman if (error) 387621a1672SDmitry Torokhov return error; 388f9f852dfSKay Sievers } 389f9f852dfSKay Sievers 390621a1672SDmitry Torokhov if (type) { 391621a1672SDmitry Torokhov error = device_add_groups(dev, type->groups); 392f9f852dfSKay Sievers if (error) 393621a1672SDmitry Torokhov goto err_remove_class_attrs; 394f9f852dfSKay Sievers } 395621a1672SDmitry Torokhov 396621a1672SDmitry Torokhov error = device_add_groups(dev, dev->groups); 397f9f852dfSKay Sievers if (error) 398621a1672SDmitry Torokhov goto err_remove_type_groups; 399621a1672SDmitry Torokhov 400621a1672SDmitry Torokhov return 0; 401621a1672SDmitry Torokhov 402621a1672SDmitry Torokhov err_remove_type_groups: 403621a1672SDmitry Torokhov if (type) 404621a1672SDmitry Torokhov device_remove_groups(dev, type->groups); 405621a1672SDmitry Torokhov err_remove_class_attrs: 406621a1672SDmitry Torokhov if (class) 407621a1672SDmitry Torokhov device_remove_attributes(dev, class->dev_attrs); 408f9f852dfSKay Sievers 4092620efefSGreg Kroah-Hartman return error; 4102620efefSGreg Kroah-Hartman } 4112620efefSGreg Kroah-Hartman 4122620efefSGreg Kroah-Hartman static void device_remove_attrs(struct device *dev) 4132620efefSGreg Kroah-Hartman { 4142620efefSGreg Kroah-Hartman struct class *class = dev->class; 415f9f852dfSKay Sievers struct device_type *type = dev->type; 4162620efefSGreg Kroah-Hartman 417621a1672SDmitry Torokhov device_remove_groups(dev, dev->groups); 418f9f852dfSKay Sievers 419621a1672SDmitry Torokhov if (type) 420621a1672SDmitry Torokhov device_remove_groups(dev, type->groups); 421621a1672SDmitry Torokhov 422621a1672SDmitry Torokhov if (class) 423621a1672SDmitry Torokhov device_remove_attributes(dev, class->dev_attrs); 4242620efefSGreg Kroah-Hartman } 4252620efefSGreg Kroah-Hartman 4262620efefSGreg Kroah-Hartman 42723681e47SGreg Kroah-Hartman static ssize_t show_dev(struct device *dev, struct device_attribute *attr, 42823681e47SGreg Kroah-Hartman char *buf) 42923681e47SGreg Kroah-Hartman { 43023681e47SGreg Kroah-Hartman return print_dev_t(buf, dev->devt); 43123681e47SGreg Kroah-Hartman } 43223681e47SGreg Kroah-Hartman 433ad6a1e1cSTejun Heo static struct device_attribute devt_attr = 434ad6a1e1cSTejun Heo __ATTR(dev, S_IRUGO, show_dev, NULL); 435ad6a1e1cSTejun Heo 436881c6cfdSGreg Kroah-Hartman /* kset to create /sys/devices/ */ 437881c6cfdSGreg Kroah-Hartman struct kset *devices_kset; 4381da177e4SLinus Torvalds 4391da177e4SLinus Torvalds /** 4401da177e4SLinus Torvalds * device_create_file - create sysfs attribute file for device. 4411da177e4SLinus Torvalds * @dev: device. 4421da177e4SLinus Torvalds * @attr: device attribute descriptor. 4431da177e4SLinus Torvalds */ 44426579ab7SPhil Carmody int device_create_file(struct device *dev, 44526579ab7SPhil Carmody const struct device_attribute *attr) 4461da177e4SLinus Torvalds { 4471da177e4SLinus Torvalds int error = 0; 4480c98b19fSCornelia Huck if (dev) 4491da177e4SLinus Torvalds error = sysfs_create_file(&dev->kobj, &attr->attr); 4501da177e4SLinus Torvalds return error; 4511da177e4SLinus Torvalds } 4521da177e4SLinus Torvalds 4531da177e4SLinus Torvalds /** 4541da177e4SLinus Torvalds * device_remove_file - remove sysfs attribute file. 4551da177e4SLinus Torvalds * @dev: device. 4561da177e4SLinus Torvalds * @attr: device attribute descriptor. 4571da177e4SLinus Torvalds */ 45826579ab7SPhil Carmody void device_remove_file(struct device *dev, 45926579ab7SPhil Carmody const struct device_attribute *attr) 4601da177e4SLinus Torvalds { 4610c98b19fSCornelia Huck if (dev) 4621da177e4SLinus Torvalds sysfs_remove_file(&dev->kobj, &attr->attr); 4631da177e4SLinus Torvalds } 4641da177e4SLinus Torvalds 4652589f188SGreg Kroah-Hartman /** 4662589f188SGreg Kroah-Hartman * device_create_bin_file - create sysfs binary attribute file for device. 4672589f188SGreg Kroah-Hartman * @dev: device. 4682589f188SGreg Kroah-Hartman * @attr: device binary attribute descriptor. 4692589f188SGreg Kroah-Hartman */ 47066ecb92bSPhil Carmody int device_create_bin_file(struct device *dev, 47166ecb92bSPhil Carmody const struct bin_attribute *attr) 4722589f188SGreg Kroah-Hartman { 4732589f188SGreg Kroah-Hartman int error = -EINVAL; 4742589f188SGreg Kroah-Hartman if (dev) 4752589f188SGreg Kroah-Hartman error = sysfs_create_bin_file(&dev->kobj, attr); 4762589f188SGreg Kroah-Hartman return error; 4772589f188SGreg Kroah-Hartman } 4782589f188SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(device_create_bin_file); 4792589f188SGreg Kroah-Hartman 4802589f188SGreg Kroah-Hartman /** 4812589f188SGreg Kroah-Hartman * device_remove_bin_file - remove sysfs binary attribute file 4822589f188SGreg Kroah-Hartman * @dev: device. 4832589f188SGreg Kroah-Hartman * @attr: device binary attribute descriptor. 4842589f188SGreg Kroah-Hartman */ 48566ecb92bSPhil Carmody void device_remove_bin_file(struct device *dev, 48666ecb92bSPhil Carmody const struct bin_attribute *attr) 4872589f188SGreg Kroah-Hartman { 4882589f188SGreg Kroah-Hartman if (dev) 4892589f188SGreg Kroah-Hartman sysfs_remove_bin_file(&dev->kobj, attr); 4902589f188SGreg Kroah-Hartman } 4912589f188SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(device_remove_bin_file); 4922589f188SGreg Kroah-Hartman 493d9a9cdfbSAlan Stern /** 494523ded71SAlan Stern * device_schedule_callback_owner - helper to schedule a callback for a device 495d9a9cdfbSAlan Stern * @dev: device. 496d9a9cdfbSAlan Stern * @func: callback function to invoke later. 497523ded71SAlan Stern * @owner: module owning the callback routine 498d9a9cdfbSAlan Stern * 499d9a9cdfbSAlan Stern * Attribute methods must not unregister themselves or their parent device 500d9a9cdfbSAlan Stern * (which would amount to the same thing). Attempts to do so will deadlock, 501d9a9cdfbSAlan Stern * since unregistration is mutually exclusive with driver callbacks. 502d9a9cdfbSAlan Stern * 503d9a9cdfbSAlan Stern * Instead methods can call this routine, which will attempt to allocate 504d9a9cdfbSAlan Stern * and schedule a workqueue request to call back @func with @dev as its 505d9a9cdfbSAlan Stern * argument in the workqueue's process context. @dev will be pinned until 506d9a9cdfbSAlan Stern * @func returns. 507d9a9cdfbSAlan Stern * 508523ded71SAlan Stern * This routine is usually called via the inline device_schedule_callback(), 509523ded71SAlan Stern * which automatically sets @owner to THIS_MODULE. 510523ded71SAlan Stern * 511d9a9cdfbSAlan Stern * Returns 0 if the request was submitted, -ENOMEM if storage could not 512523ded71SAlan Stern * be allocated, -ENODEV if a reference to @owner isn't available. 513d9a9cdfbSAlan Stern * 514d9a9cdfbSAlan Stern * NOTE: This routine won't work if CONFIG_SYSFS isn't set! It uses an 515d9a9cdfbSAlan Stern * underlying sysfs routine (since it is intended for use by attribute 516d9a9cdfbSAlan Stern * methods), and if sysfs isn't available you'll get nothing but -ENOSYS. 517d9a9cdfbSAlan Stern */ 518523ded71SAlan Stern int device_schedule_callback_owner(struct device *dev, 519523ded71SAlan Stern void (*func)(struct device *), struct module *owner) 520d9a9cdfbSAlan Stern { 521d9a9cdfbSAlan Stern return sysfs_schedule_callback(&dev->kobj, 522523ded71SAlan Stern (void (*)(void *)) func, dev, owner); 523d9a9cdfbSAlan Stern } 524523ded71SAlan Stern EXPORT_SYMBOL_GPL(device_schedule_callback_owner); 525d9a9cdfbSAlan Stern 52634bb61f9SJames Bottomley static void klist_children_get(struct klist_node *n) 52734bb61f9SJames Bottomley { 528f791b8c8SGreg Kroah-Hartman struct device_private *p = to_device_private_parent(n); 529f791b8c8SGreg Kroah-Hartman struct device *dev = p->device; 53034bb61f9SJames Bottomley 53134bb61f9SJames Bottomley get_device(dev); 53234bb61f9SJames Bottomley } 53334bb61f9SJames Bottomley 53434bb61f9SJames Bottomley static void klist_children_put(struct klist_node *n) 53534bb61f9SJames Bottomley { 536f791b8c8SGreg Kroah-Hartman struct device_private *p = to_device_private_parent(n); 537f791b8c8SGreg Kroah-Hartman struct device *dev = p->device; 53834bb61f9SJames Bottomley 53934bb61f9SJames Bottomley put_device(dev); 54034bb61f9SJames Bottomley } 54134bb61f9SJames Bottomley 5421da177e4SLinus Torvalds /** 5431da177e4SLinus Torvalds * device_initialize - init device structure. 5441da177e4SLinus Torvalds * @dev: device. 5451da177e4SLinus Torvalds * 5465739411aSCornelia Huck * This prepares the device for use by other layers by initializing 5475739411aSCornelia Huck * its fields. 5481da177e4SLinus Torvalds * It is the first half of device_register(), if called by 5495739411aSCornelia Huck * that function, though it can also be called separately, so one 5505739411aSCornelia Huck * may use @dev's fields. In particular, get_device()/put_device() 5515739411aSCornelia Huck * may be used for reference counting of @dev after calling this 5525739411aSCornelia Huck * function. 5535739411aSCornelia Huck * 5545739411aSCornelia Huck * NOTE: Use put_device() to give up your reference instead of freeing 5555739411aSCornelia Huck * @dev directly once you have called this function. 5561da177e4SLinus Torvalds */ 5571da177e4SLinus Torvalds void device_initialize(struct device *dev) 5581da177e4SLinus Torvalds { 559881c6cfdSGreg Kroah-Hartman dev->kobj.kset = devices_kset; 560f9cb074bSGreg Kroah-Hartman kobject_init(&dev->kobj, &device_ktype); 5611da177e4SLinus Torvalds INIT_LIST_HEAD(&dev->dma_pools); 562af70316aSmochel@digitalimplant.org init_MUTEX(&dev->sem); 5639ac7849eSTejun Heo spin_lock_init(&dev->devres_lock); 5649ac7849eSTejun Heo INIT_LIST_HEAD(&dev->devres_head); 5650ac85241SDavid Brownell device_init_wakeup(dev, 0); 5663b98aeafSAlan Stern device_pm_init(dev); 56787348136SChristoph Hellwig set_dev_node(dev, -1); 5681da177e4SLinus Torvalds } 5691da177e4SLinus Torvalds 57040fa5422SGreg Kroah-Hartman #ifdef CONFIG_SYSFS_DEPRECATED 571c744aeaeSCornelia Huck static struct kobject *get_device_parent(struct device *dev, 572c744aeaeSCornelia Huck struct device *parent) 57340fa5422SGreg Kroah-Hartman { 574da231fd5SKay Sievers /* class devices without a parent live in /sys/class/<classname>/ */ 5753eb215deSDmitry Torokhov if (dev->class && (!parent || parent->class != dev->class)) 5761fbfee6cSGreg Kroah-Hartman return &dev->class->p->class_subsys.kobj; 577da231fd5SKay Sievers /* all other devices keep their parent */ 57840fa5422SGreg Kroah-Hartman else if (parent) 579c744aeaeSCornelia Huck return &parent->kobj; 58040fa5422SGreg Kroah-Hartman 581c744aeaeSCornelia Huck return NULL; 58240fa5422SGreg Kroah-Hartman } 583da231fd5SKay Sievers 584da231fd5SKay Sievers static inline void cleanup_device_parent(struct device *dev) {} 58563b6971aSCornelia Huck static inline void cleanup_glue_dir(struct device *dev, 58663b6971aSCornelia Huck struct kobject *glue_dir) {} 58740fa5422SGreg Kroah-Hartman #else 588c744aeaeSCornelia Huck static struct kobject *virtual_device_parent(struct device *dev) 589f0ee61a6SGreg Kroah-Hartman { 590f0ee61a6SGreg Kroah-Hartman static struct kobject *virtual_dir = NULL; 591f0ee61a6SGreg Kroah-Hartman 592f0ee61a6SGreg Kroah-Hartman if (!virtual_dir) 5934ff6abffSGreg Kroah-Hartman virtual_dir = kobject_create_and_add("virtual", 594881c6cfdSGreg Kroah-Hartman &devices_kset->kobj); 595f0ee61a6SGreg Kroah-Hartman 59686406245SKay Sievers return virtual_dir; 597f0ee61a6SGreg Kroah-Hartman } 598f0ee61a6SGreg Kroah-Hartman 599c744aeaeSCornelia Huck static struct kobject *get_device_parent(struct device *dev, 600c744aeaeSCornelia Huck struct device *parent) 60140fa5422SGreg Kroah-Hartman { 60243968d2fSGreg Kroah-Hartman int retval; 60343968d2fSGreg Kroah-Hartman 60486406245SKay Sievers if (dev->class) { 60577d3d7c1STejun Heo static DEFINE_MUTEX(gdp_mutex); 60686406245SKay Sievers struct kobject *kobj = NULL; 60786406245SKay Sievers struct kobject *parent_kobj; 60886406245SKay Sievers struct kobject *k; 60986406245SKay Sievers 61086406245SKay Sievers /* 61186406245SKay Sievers * If we have no parent, we live in "virtual". 6120f4dafc0SKay Sievers * Class-devices with a non class-device as parent, live 6130f4dafc0SKay Sievers * in a "glue" directory to prevent namespace collisions. 61486406245SKay Sievers */ 61586406245SKay Sievers if (parent == NULL) 61686406245SKay Sievers parent_kobj = virtual_device_parent(dev); 61786406245SKay Sievers else if (parent->class) 61886406245SKay Sievers return &parent->kobj; 61986406245SKay Sievers else 62086406245SKay Sievers parent_kobj = &parent->kobj; 62186406245SKay Sievers 62277d3d7c1STejun Heo mutex_lock(&gdp_mutex); 62377d3d7c1STejun Heo 62486406245SKay Sievers /* find our class-directory at the parent and reference it */ 6257c71448bSGreg Kroah-Hartman spin_lock(&dev->class->p->class_dirs.list_lock); 6267c71448bSGreg Kroah-Hartman list_for_each_entry(k, &dev->class->p->class_dirs.list, entry) 62786406245SKay Sievers if (k->parent == parent_kobj) { 62886406245SKay Sievers kobj = kobject_get(k); 62986406245SKay Sievers break; 63086406245SKay Sievers } 6317c71448bSGreg Kroah-Hartman spin_unlock(&dev->class->p->class_dirs.list_lock); 63277d3d7c1STejun Heo if (kobj) { 63377d3d7c1STejun Heo mutex_unlock(&gdp_mutex); 63486406245SKay Sievers return kobj; 63577d3d7c1STejun Heo } 63686406245SKay Sievers 63786406245SKay Sievers /* or create a new class-directory at the parent device */ 63843968d2fSGreg Kroah-Hartman k = kobject_create(); 63977d3d7c1STejun Heo if (!k) { 64077d3d7c1STejun Heo mutex_unlock(&gdp_mutex); 64143968d2fSGreg Kroah-Hartman return NULL; 64277d3d7c1STejun Heo } 6437c71448bSGreg Kroah-Hartman k->kset = &dev->class->p->class_dirs; 644b2d6db58SGreg Kroah-Hartman retval = kobject_add(k, parent_kobj, "%s", dev->class->name); 64543968d2fSGreg Kroah-Hartman if (retval < 0) { 64677d3d7c1STejun Heo mutex_unlock(&gdp_mutex); 64743968d2fSGreg Kroah-Hartman kobject_put(k); 64843968d2fSGreg Kroah-Hartman return NULL; 64943968d2fSGreg Kroah-Hartman } 6500f4dafc0SKay Sievers /* do not emit an uevent for this simple "glue" directory */ 65177d3d7c1STejun Heo mutex_unlock(&gdp_mutex); 65243968d2fSGreg Kroah-Hartman return k; 65386406245SKay Sievers } 65486406245SKay Sievers 65586406245SKay Sievers if (parent) 656c744aeaeSCornelia Huck return &parent->kobj; 657c744aeaeSCornelia Huck return NULL; 658c744aeaeSCornelia Huck } 659da231fd5SKay Sievers 66063b6971aSCornelia Huck static void cleanup_glue_dir(struct device *dev, struct kobject *glue_dir) 661da231fd5SKay Sievers { 6620f4dafc0SKay Sievers /* see if we live in a "glue" directory */ 663c1fe539aSCornelia Huck if (!glue_dir || !dev->class || 6647c71448bSGreg Kroah-Hartman glue_dir->kset != &dev->class->p->class_dirs) 665da231fd5SKay Sievers return; 666da231fd5SKay Sievers 6670f4dafc0SKay Sievers kobject_put(glue_dir); 668da231fd5SKay Sievers } 66963b6971aSCornelia Huck 67063b6971aSCornelia Huck static void cleanup_device_parent(struct device *dev) 67163b6971aSCornelia Huck { 67263b6971aSCornelia Huck cleanup_glue_dir(dev, dev->kobj.parent); 67363b6971aSCornelia Huck } 674c744aeaeSCornelia Huck #endif 67586406245SKay Sievers 67663b6971aSCornelia Huck static void setup_parent(struct device *dev, struct device *parent) 677c744aeaeSCornelia Huck { 678c744aeaeSCornelia Huck struct kobject *kobj; 679c744aeaeSCornelia Huck kobj = get_device_parent(dev, parent); 680c744aeaeSCornelia Huck if (kobj) 681c744aeaeSCornelia Huck dev->kobj.parent = kobj; 68240fa5422SGreg Kroah-Hartman } 68340fa5422SGreg Kroah-Hartman 6842ee97cafSCornelia Huck static int device_add_class_symlinks(struct device *dev) 6852ee97cafSCornelia Huck { 6862ee97cafSCornelia Huck int error; 6872ee97cafSCornelia Huck 6882ee97cafSCornelia Huck if (!dev->class) 6892ee97cafSCornelia Huck return 0; 690da231fd5SKay Sievers 6911fbfee6cSGreg Kroah-Hartman error = sysfs_create_link(&dev->kobj, 6921fbfee6cSGreg Kroah-Hartman &dev->class->p->class_subsys.kobj, 6932ee97cafSCornelia Huck "subsystem"); 6942ee97cafSCornelia Huck if (error) 6952ee97cafSCornelia Huck goto out; 696da231fd5SKay Sievers 697da231fd5SKay Sievers #ifdef CONFIG_SYSFS_DEPRECATED 698da231fd5SKay Sievers /* stacked class devices need a symlink in the class directory */ 6991fbfee6cSGreg Kroah-Hartman if (dev->kobj.parent != &dev->class->p->class_subsys.kobj && 7004e886c29SGreg Kroah-Hartman device_is_not_partition(dev)) { 7011fbfee6cSGreg Kroah-Hartman error = sysfs_create_link(&dev->class->p->class_subsys.kobj, 7021e0b2cf9SKay Sievers &dev->kobj, dev_name(dev)); 7032ee97cafSCornelia Huck if (error) 7042ee97cafSCornelia Huck goto out_subsys; 7052ee97cafSCornelia Huck } 706da231fd5SKay Sievers 7074e886c29SGreg Kroah-Hartman if (dev->parent && device_is_not_partition(dev)) { 7084f01a757SDmitry Torokhov struct device *parent = dev->parent; 7094f01a757SDmitry Torokhov char *class_name; 7104f01a757SDmitry Torokhov 7114f01a757SDmitry Torokhov /* 712da231fd5SKay Sievers * stacked class devices have the 'device' link 713da231fd5SKay Sievers * pointing to the bus device instead of the parent 7144f01a757SDmitry Torokhov */ 7154f01a757SDmitry Torokhov while (parent->class && !parent->bus && parent->parent) 7164f01a757SDmitry Torokhov parent = parent->parent; 7174f01a757SDmitry Torokhov 7184f01a757SDmitry Torokhov error = sysfs_create_link(&dev->kobj, 7194f01a757SDmitry Torokhov &parent->kobj, 7202ee97cafSCornelia Huck "device"); 7212ee97cafSCornelia Huck if (error) 7222ee97cafSCornelia Huck goto out_busid; 7234f01a757SDmitry Torokhov 7244f01a757SDmitry Torokhov class_name = make_class_name(dev->class->name, 7252ee97cafSCornelia Huck &dev->kobj); 7262ee97cafSCornelia Huck if (class_name) 7272ee97cafSCornelia Huck error = sysfs_create_link(&dev->parent->kobj, 7282ee97cafSCornelia Huck &dev->kobj, class_name); 7292ee97cafSCornelia Huck kfree(class_name); 7302ee97cafSCornelia Huck if (error) 7312ee97cafSCornelia Huck goto out_device; 7322ee97cafSCornelia Huck } 733da231fd5SKay Sievers return 0; 734da231fd5SKay Sievers 735da231fd5SKay Sievers out_device: 7364e886c29SGreg Kroah-Hartman if (dev->parent && device_is_not_partition(dev)) 737da231fd5SKay Sievers sysfs_remove_link(&dev->kobj, "device"); 738da231fd5SKay Sievers out_busid: 7391fbfee6cSGreg Kroah-Hartman if (dev->kobj.parent != &dev->class->p->class_subsys.kobj && 7404e886c29SGreg Kroah-Hartman device_is_not_partition(dev)) 7411fbfee6cSGreg Kroah-Hartman sysfs_remove_link(&dev->class->p->class_subsys.kobj, 7421e0b2cf9SKay Sievers dev_name(dev)); 7434f01a757SDmitry Torokhov #else 744da231fd5SKay Sievers /* link in the class directory pointing to the device */ 7451fbfee6cSGreg Kroah-Hartman error = sysfs_create_link(&dev->class->p->class_subsys.kobj, 7461e0b2cf9SKay Sievers &dev->kobj, dev_name(dev)); 747da231fd5SKay Sievers if (error) 748da231fd5SKay Sievers goto out_subsys; 749da231fd5SKay Sievers 7504e886c29SGreg Kroah-Hartman if (dev->parent && device_is_not_partition(dev)) { 7514f01a757SDmitry Torokhov error = sysfs_create_link(&dev->kobj, &dev->parent->kobj, 7524f01a757SDmitry Torokhov "device"); 7534f01a757SDmitry Torokhov if (error) 7544f01a757SDmitry Torokhov goto out_busid; 7552ee97cafSCornelia Huck } 7562ee97cafSCornelia Huck return 0; 7572ee97cafSCornelia Huck 7582ee97cafSCornelia Huck out_busid: 7591e0b2cf9SKay Sievers sysfs_remove_link(&dev->class->p->class_subsys.kobj, dev_name(dev)); 760da231fd5SKay Sievers #endif 761da231fd5SKay Sievers 7622ee97cafSCornelia Huck out_subsys: 7632ee97cafSCornelia Huck sysfs_remove_link(&dev->kobj, "subsystem"); 7642ee97cafSCornelia Huck out: 7652ee97cafSCornelia Huck return error; 7662ee97cafSCornelia Huck } 7672ee97cafSCornelia Huck 7682ee97cafSCornelia Huck static void device_remove_class_symlinks(struct device *dev) 7692ee97cafSCornelia Huck { 7702ee97cafSCornelia Huck if (!dev->class) 7712ee97cafSCornelia Huck return; 772da231fd5SKay Sievers 7732ee97cafSCornelia Huck #ifdef CONFIG_SYSFS_DEPRECATED 7744e886c29SGreg Kroah-Hartman if (dev->parent && device_is_not_partition(dev)) { 7752ee97cafSCornelia Huck char *class_name; 7762ee97cafSCornelia Huck 7772ee97cafSCornelia Huck class_name = make_class_name(dev->class->name, &dev->kobj); 7782ee97cafSCornelia Huck if (class_name) { 7792ee97cafSCornelia Huck sysfs_remove_link(&dev->parent->kobj, class_name); 7802ee97cafSCornelia Huck kfree(class_name); 7812ee97cafSCornelia Huck } 7822ee97cafSCornelia Huck sysfs_remove_link(&dev->kobj, "device"); 7832ee97cafSCornelia Huck } 784da231fd5SKay Sievers 7851fbfee6cSGreg Kroah-Hartman if (dev->kobj.parent != &dev->class->p->class_subsys.kobj && 7864e886c29SGreg Kroah-Hartman device_is_not_partition(dev)) 7871fbfee6cSGreg Kroah-Hartman sysfs_remove_link(&dev->class->p->class_subsys.kobj, 7881e0b2cf9SKay Sievers dev_name(dev)); 789da231fd5SKay Sievers #else 7904e886c29SGreg Kroah-Hartman if (dev->parent && device_is_not_partition(dev)) 791da231fd5SKay Sievers sysfs_remove_link(&dev->kobj, "device"); 792da231fd5SKay Sievers 7931e0b2cf9SKay Sievers sysfs_remove_link(&dev->class->p->class_subsys.kobj, dev_name(dev)); 794da231fd5SKay Sievers #endif 795da231fd5SKay Sievers 7962ee97cafSCornelia Huck sysfs_remove_link(&dev->kobj, "subsystem"); 7972ee97cafSCornelia Huck } 7982ee97cafSCornelia Huck 7991da177e4SLinus Torvalds /** 800413c239fSStephen Rothwell * dev_set_name - set a device name 801413c239fSStephen Rothwell * @dev: device 80246232366SRandy Dunlap * @fmt: format string for the device's name 803413c239fSStephen Rothwell */ 804413c239fSStephen Rothwell int dev_set_name(struct device *dev, const char *fmt, ...) 805413c239fSStephen Rothwell { 806413c239fSStephen Rothwell va_list vargs; 8071fa5ae85SKay Sievers int err; 808413c239fSStephen Rothwell 809413c239fSStephen Rothwell va_start(vargs, fmt); 8101fa5ae85SKay Sievers err = kobject_set_name_vargs(&dev->kobj, fmt, vargs); 811413c239fSStephen Rothwell va_end(vargs); 8121fa5ae85SKay Sievers return err; 813413c239fSStephen Rothwell } 814413c239fSStephen Rothwell EXPORT_SYMBOL_GPL(dev_set_name); 815413c239fSStephen Rothwell 816413c239fSStephen Rothwell /** 817e105b8bfSDan Williams * device_to_dev_kobj - select a /sys/dev/ directory for the device 818e105b8bfSDan Williams * @dev: device 819e105b8bfSDan Williams * 820e105b8bfSDan Williams * By default we select char/ for new entries. Setting class->dev_obj 821e105b8bfSDan Williams * to NULL prevents an entry from being created. class->dev_kobj must 822e105b8bfSDan Williams * be set (or cleared) before any devices are registered to the class 823e105b8bfSDan Williams * otherwise device_create_sys_dev_entry() and 824e105b8bfSDan Williams * device_remove_sys_dev_entry() will disagree about the the presence 825e105b8bfSDan Williams * of the link. 826e105b8bfSDan Williams */ 827e105b8bfSDan Williams static struct kobject *device_to_dev_kobj(struct device *dev) 828e105b8bfSDan Williams { 829e105b8bfSDan Williams struct kobject *kobj; 830e105b8bfSDan Williams 831e105b8bfSDan Williams if (dev->class) 832e105b8bfSDan Williams kobj = dev->class->dev_kobj; 833e105b8bfSDan Williams else 834e105b8bfSDan Williams kobj = sysfs_dev_char_kobj; 835e105b8bfSDan Williams 836e105b8bfSDan Williams return kobj; 837e105b8bfSDan Williams } 838e105b8bfSDan Williams 839e105b8bfSDan Williams static int device_create_sys_dev_entry(struct device *dev) 840e105b8bfSDan Williams { 841e105b8bfSDan Williams struct kobject *kobj = device_to_dev_kobj(dev); 842e105b8bfSDan Williams int error = 0; 843e105b8bfSDan Williams char devt_str[15]; 844e105b8bfSDan Williams 845e105b8bfSDan Williams if (kobj) { 846e105b8bfSDan Williams format_dev_t(devt_str, dev->devt); 847e105b8bfSDan Williams error = sysfs_create_link(kobj, &dev->kobj, devt_str); 848e105b8bfSDan Williams } 849e105b8bfSDan Williams 850e105b8bfSDan Williams return error; 851e105b8bfSDan Williams } 852e105b8bfSDan Williams 853e105b8bfSDan Williams static void device_remove_sys_dev_entry(struct device *dev) 854e105b8bfSDan Williams { 855e105b8bfSDan Williams struct kobject *kobj = device_to_dev_kobj(dev); 856e105b8bfSDan Williams char devt_str[15]; 857e105b8bfSDan Williams 858e105b8bfSDan Williams if (kobj) { 859e105b8bfSDan Williams format_dev_t(devt_str, dev->devt); 860e105b8bfSDan Williams sysfs_remove_link(kobj, devt_str); 861e105b8bfSDan Williams } 862e105b8bfSDan Williams } 863e105b8bfSDan Williams 864b4028437SGreg Kroah-Hartman int device_private_init(struct device *dev) 865b4028437SGreg Kroah-Hartman { 866b4028437SGreg Kroah-Hartman dev->p = kzalloc(sizeof(*dev->p), GFP_KERNEL); 867b4028437SGreg Kroah-Hartman if (!dev->p) 868b4028437SGreg Kroah-Hartman return -ENOMEM; 869b4028437SGreg Kroah-Hartman dev->p->device = dev; 870b4028437SGreg Kroah-Hartman klist_init(&dev->p->klist_children, klist_children_get, 871b4028437SGreg Kroah-Hartman klist_children_put); 872b4028437SGreg Kroah-Hartman return 0; 873b4028437SGreg Kroah-Hartman } 874b4028437SGreg Kroah-Hartman 875e105b8bfSDan Williams /** 8761da177e4SLinus Torvalds * device_add - add device to device hierarchy. 8771da177e4SLinus Torvalds * @dev: device. 8781da177e4SLinus Torvalds * 8791da177e4SLinus Torvalds * This is part 2 of device_register(), though may be called 8801da177e4SLinus Torvalds * separately _iff_ device_initialize() has been called separately. 8811da177e4SLinus Torvalds * 8825739411aSCornelia Huck * This adds @dev to the kobject hierarchy via kobject_add(), adds it 8831da177e4SLinus Torvalds * to the global and sibling lists for the device, then 8841da177e4SLinus Torvalds * adds it to the other relevant subsystems of the driver model. 8855739411aSCornelia Huck * 8865739411aSCornelia Huck * NOTE: _Never_ directly free @dev after calling this function, even 8875739411aSCornelia Huck * if it returned an error! Always use put_device() to give up your 8885739411aSCornelia Huck * reference instead. 8891da177e4SLinus Torvalds */ 8901da177e4SLinus Torvalds int device_add(struct device *dev) 8911da177e4SLinus Torvalds { 8921da177e4SLinus Torvalds struct device *parent = NULL; 893c47ed219SGreg Kroah-Hartman struct class_interface *class_intf; 894c906a48aSGreg Kroah-Hartman int error = -EINVAL; 895775b64d2SRafael J. Wysocki 8961da177e4SLinus Torvalds dev = get_device(dev); 897c906a48aSGreg Kroah-Hartman if (!dev) 898c906a48aSGreg Kroah-Hartman goto done; 899c906a48aSGreg Kroah-Hartman 900fb069a5dSGreg Kroah-Hartman if (!dev->p) { 901b4028437SGreg Kroah-Hartman error = device_private_init(dev); 902b4028437SGreg Kroah-Hartman if (error) 903fb069a5dSGreg Kroah-Hartman goto done; 904fb069a5dSGreg Kroah-Hartman } 905fb069a5dSGreg Kroah-Hartman 9061fa5ae85SKay Sievers /* 9071fa5ae85SKay Sievers * for statically allocated devices, which should all be converted 9081fa5ae85SKay Sievers * some day, we need to initialize the name. We prevent reading back 9091fa5ae85SKay Sievers * the name, and force the use of dev_name() 9101fa5ae85SKay Sievers */ 9111fa5ae85SKay Sievers if (dev->init_name) { 912acc0e90fSGreg Kroah-Hartman dev_set_name(dev, "%s", dev->init_name); 9131fa5ae85SKay Sievers dev->init_name = NULL; 9141fa5ae85SKay Sievers } 915c906a48aSGreg Kroah-Hartman 916e6309e75SThomas Gleixner if (!dev_name(dev)) { 917e6309e75SThomas Gleixner error = -EINVAL; 9185c8563d7SKay Sievers goto name_error; 919e6309e75SThomas Gleixner } 9201da177e4SLinus Torvalds 9211e0b2cf9SKay Sievers pr_debug("device: '%s': %s\n", dev_name(dev), __func__); 922c205ef48SGreg Kroah-Hartman 9231da177e4SLinus Torvalds parent = get_device(dev->parent); 92463b6971aSCornelia Huck setup_parent(dev, parent); 9251da177e4SLinus Torvalds 9260d358f22SYinghai Lu /* use parent numa_node */ 9270d358f22SYinghai Lu if (parent) 9280d358f22SYinghai Lu set_dev_node(dev, dev_to_node(parent)); 9290d358f22SYinghai Lu 9301da177e4SLinus Torvalds /* first, register with generic layer. */ 9318a577ffcSKay Sievers /* we require the name to be set before, and pass NULL */ 9328a577ffcSKay Sievers error = kobject_add(&dev->kobj, dev->kobj.parent, NULL); 93340fa5422SGreg Kroah-Hartman if (error) 9341da177e4SLinus Torvalds goto Error; 935a7fd6706SKay Sievers 93637022644SBrian Walsh /* notify platform of device entry */ 93737022644SBrian Walsh if (platform_notify) 93837022644SBrian Walsh platform_notify(dev); 93937022644SBrian Walsh 940ad6a1e1cSTejun Heo error = device_create_file(dev, &uevent_attr); 941a306eea4SCornelia Huck if (error) 942a306eea4SCornelia Huck goto attrError; 943a7fd6706SKay Sievers 94423681e47SGreg Kroah-Hartman if (MAJOR(dev->devt)) { 945ad6a1e1cSTejun Heo error = device_create_file(dev, &devt_attr); 946ad6a1e1cSTejun Heo if (error) 947a306eea4SCornelia Huck goto ueventattrError; 948e105b8bfSDan Williams 949e105b8bfSDan Williams error = device_create_sys_dev_entry(dev); 950e105b8bfSDan Williams if (error) 951e105b8bfSDan Williams goto devtattrError; 9522b2af54aSKay Sievers 9532b2af54aSKay Sievers devtmpfs_create_node(dev); 95423681e47SGreg Kroah-Hartman } 95523681e47SGreg Kroah-Hartman 9562ee97cafSCornelia Huck error = device_add_class_symlinks(dev); 9572ee97cafSCornelia Huck if (error) 9582ee97cafSCornelia Huck goto SymlinkError; 959dc0afa83SCornelia Huck error = device_add_attrs(dev); 960dc0afa83SCornelia Huck if (error) 9612620efefSGreg Kroah-Hartman goto AttrsError; 962dc0afa83SCornelia Huck error = bus_add_device(dev); 963dc0afa83SCornelia Huck if (error) 9641da177e4SLinus Torvalds goto BusError; 9653b98aeafSAlan Stern error = dpm_sysfs_add(dev); 96657eee3d2SRafael J. Wysocki if (error) 9673b98aeafSAlan Stern goto DPMError; 9683b98aeafSAlan Stern device_pm_add(dev); 969ec0676eeSAlan Stern 970ec0676eeSAlan Stern /* Notify clients of device addition. This call must come 971ec0676eeSAlan Stern * after dpm_sysf_add() and before kobject_uevent(). 972ec0676eeSAlan Stern */ 973ec0676eeSAlan Stern if (dev->bus) 974ec0676eeSAlan Stern blocking_notifier_call_chain(&dev->bus->p->bus_notifier, 975ec0676eeSAlan Stern BUS_NOTIFY_ADD_DEVICE, dev); 976ec0676eeSAlan Stern 97753877d06SKay Sievers kobject_uevent(&dev->kobj, KOBJ_ADD); 9782023c610SAlan Stern bus_probe_device(dev); 9791da177e4SLinus Torvalds if (parent) 980f791b8c8SGreg Kroah-Hartman klist_add_tail(&dev->p->knode_parent, 981f791b8c8SGreg Kroah-Hartman &parent->p->klist_children); 9821da177e4SLinus Torvalds 9835d9fd169SGreg Kroah-Hartman if (dev->class) { 984f75b1c60SDave Young mutex_lock(&dev->class->p->class_mutex); 985c47ed219SGreg Kroah-Hartman /* tie the class to the device */ 9865a3ceb86STejun Heo klist_add_tail(&dev->knode_class, 9875a3ceb86STejun Heo &dev->class->p->class_devices); 988c47ed219SGreg Kroah-Hartman 989c47ed219SGreg Kroah-Hartman /* notify any interfaces that the device is here */ 990184f1f77SGreg Kroah-Hartman list_for_each_entry(class_intf, 991184f1f77SGreg Kroah-Hartman &dev->class->p->class_interfaces, node) 992c47ed219SGreg Kroah-Hartman if (class_intf->add_dev) 993c47ed219SGreg Kroah-Hartman class_intf->add_dev(dev, class_intf); 994f75b1c60SDave Young mutex_unlock(&dev->class->p->class_mutex); 9955d9fd169SGreg Kroah-Hartman } 996c906a48aSGreg Kroah-Hartman done: 9971da177e4SLinus Torvalds put_device(dev); 9981da177e4SLinus Torvalds return error; 9993b98aeafSAlan Stern DPMError: 100057eee3d2SRafael J. Wysocki bus_remove_device(dev); 100157eee3d2SRafael J. Wysocki BusError: 10022620efefSGreg Kroah-Hartman device_remove_attrs(dev); 10032620efefSGreg Kroah-Hartman AttrsError: 10042ee97cafSCornelia Huck device_remove_class_symlinks(dev); 10052ee97cafSCornelia Huck SymlinkError: 1006ad6a1e1cSTejun Heo if (MAJOR(dev->devt)) 1007ad72956dSKay Sievers devtmpfs_delete_node(dev); 1008ad72956dSKay Sievers if (MAJOR(dev->devt)) 1009e105b8bfSDan Williams device_remove_sys_dev_entry(dev); 1010e105b8bfSDan Williams devtattrError: 1011e105b8bfSDan Williams if (MAJOR(dev->devt)) 1012ad6a1e1cSTejun Heo device_remove_file(dev, &devt_attr); 1013a306eea4SCornelia Huck ueventattrError: 1014ad6a1e1cSTejun Heo device_remove_file(dev, &uevent_attr); 101523681e47SGreg Kroah-Hartman attrError: 1016312c004dSKay Sievers kobject_uevent(&dev->kobj, KOBJ_REMOVE); 10171da177e4SLinus Torvalds kobject_del(&dev->kobj); 10181da177e4SLinus Torvalds Error: 101963b6971aSCornelia Huck cleanup_device_parent(dev); 10201da177e4SLinus Torvalds if (parent) 10211da177e4SLinus Torvalds put_device(parent); 10225c8563d7SKay Sievers name_error: 10235c8563d7SKay Sievers kfree(dev->p); 10245c8563d7SKay Sievers dev->p = NULL; 1025c906a48aSGreg Kroah-Hartman goto done; 10261da177e4SLinus Torvalds } 10271da177e4SLinus Torvalds 10281da177e4SLinus Torvalds /** 10291da177e4SLinus Torvalds * device_register - register a device with the system. 10301da177e4SLinus Torvalds * @dev: pointer to the device structure 10311da177e4SLinus Torvalds * 10321da177e4SLinus Torvalds * This happens in two clean steps - initialize the device 10331da177e4SLinus Torvalds * and add it to the system. The two steps can be called 10341da177e4SLinus Torvalds * separately, but this is the easiest and most common. 10351da177e4SLinus Torvalds * I.e. you should only call the two helpers separately if 10361da177e4SLinus Torvalds * have a clearly defined need to use and refcount the device 10371da177e4SLinus Torvalds * before it is added to the hierarchy. 10385739411aSCornelia Huck * 10395739411aSCornelia Huck * NOTE: _Never_ directly free @dev after calling this function, even 10405739411aSCornelia Huck * if it returned an error! Always use put_device() to give up the 10415739411aSCornelia Huck * reference initialized in this function instead. 10421da177e4SLinus Torvalds */ 10431da177e4SLinus Torvalds int device_register(struct device *dev) 10441da177e4SLinus Torvalds { 10451da177e4SLinus Torvalds device_initialize(dev); 10461da177e4SLinus Torvalds return device_add(dev); 10471da177e4SLinus Torvalds } 10481da177e4SLinus Torvalds 10491da177e4SLinus Torvalds /** 10501da177e4SLinus Torvalds * get_device - increment reference count for device. 10511da177e4SLinus Torvalds * @dev: device. 10521da177e4SLinus Torvalds * 10531da177e4SLinus Torvalds * This simply forwards the call to kobject_get(), though 10541da177e4SLinus Torvalds * we do take care to provide for the case that we get a NULL 10551da177e4SLinus Torvalds * pointer passed in. 10561da177e4SLinus Torvalds */ 10571da177e4SLinus Torvalds struct device *get_device(struct device *dev) 10581da177e4SLinus Torvalds { 10591da177e4SLinus Torvalds return dev ? to_dev(kobject_get(&dev->kobj)) : NULL; 10601da177e4SLinus Torvalds } 10611da177e4SLinus Torvalds 10621da177e4SLinus Torvalds /** 10631da177e4SLinus Torvalds * put_device - decrement reference count. 10641da177e4SLinus Torvalds * @dev: device in question. 10651da177e4SLinus Torvalds */ 10661da177e4SLinus Torvalds void put_device(struct device *dev) 10671da177e4SLinus Torvalds { 1068edfaa7c3SKay Sievers /* might_sleep(); */ 10691da177e4SLinus Torvalds if (dev) 10701da177e4SLinus Torvalds kobject_put(&dev->kobj); 10711da177e4SLinus Torvalds } 10721da177e4SLinus Torvalds 10731da177e4SLinus Torvalds /** 10741da177e4SLinus Torvalds * device_del - delete device from system. 10751da177e4SLinus Torvalds * @dev: device. 10761da177e4SLinus Torvalds * 10771da177e4SLinus Torvalds * This is the first part of the device unregistration 10781da177e4SLinus Torvalds * sequence. This removes the device from the lists we control 10791da177e4SLinus Torvalds * from here, has it removed from the other driver model 10801da177e4SLinus Torvalds * subsystems it was added to in device_add(), and removes it 10811da177e4SLinus Torvalds * from the kobject hierarchy. 10821da177e4SLinus Torvalds * 10831da177e4SLinus Torvalds * NOTE: this should be called manually _iff_ device_add() was 10841da177e4SLinus Torvalds * also called manually. 10851da177e4SLinus Torvalds */ 10861da177e4SLinus Torvalds void device_del(struct device *dev) 10871da177e4SLinus Torvalds { 10881da177e4SLinus Torvalds struct device *parent = dev->parent; 1089c47ed219SGreg Kroah-Hartman struct class_interface *class_intf; 10901da177e4SLinus Torvalds 1091ec0676eeSAlan Stern /* Notify clients of device removal. This call must come 1092ec0676eeSAlan Stern * before dpm_sysfs_remove(). 1093ec0676eeSAlan Stern */ 1094ec0676eeSAlan Stern if (dev->bus) 1095ec0676eeSAlan Stern blocking_notifier_call_chain(&dev->bus->p->bus_notifier, 1096ec0676eeSAlan Stern BUS_NOTIFY_DEL_DEVICE, dev); 1097775b64d2SRafael J. Wysocki device_pm_remove(dev); 10983b98aeafSAlan Stern dpm_sysfs_remove(dev); 10991da177e4SLinus Torvalds if (parent) 1100f791b8c8SGreg Kroah-Hartman klist_del(&dev->p->knode_parent); 1101e105b8bfSDan Williams if (MAJOR(dev->devt)) { 11022b2af54aSKay Sievers devtmpfs_delete_node(dev); 1103e105b8bfSDan Williams device_remove_sys_dev_entry(dev); 1104ad6a1e1cSTejun Heo device_remove_file(dev, &devt_attr); 1105e105b8bfSDan Williams } 1106b9d9c82bSKay Sievers if (dev->class) { 1107da231fd5SKay Sievers device_remove_class_symlinks(dev); 110899ef3ef8SKay Sievers 1109f75b1c60SDave Young mutex_lock(&dev->class->p->class_mutex); 1110c47ed219SGreg Kroah-Hartman /* notify any interfaces that the device is now gone */ 1111184f1f77SGreg Kroah-Hartman list_for_each_entry(class_intf, 1112184f1f77SGreg Kroah-Hartman &dev->class->p->class_interfaces, node) 1113c47ed219SGreg Kroah-Hartman if (class_intf->remove_dev) 1114c47ed219SGreg Kroah-Hartman class_intf->remove_dev(dev, class_intf); 1115c47ed219SGreg Kroah-Hartman /* remove the device from the class list */ 11165a3ceb86STejun Heo klist_del(&dev->knode_class); 1117f75b1c60SDave Young mutex_unlock(&dev->class->p->class_mutex); 1118b9d9c82bSKay Sievers } 1119ad6a1e1cSTejun Heo device_remove_file(dev, &uevent_attr); 11202620efefSGreg Kroah-Hartman device_remove_attrs(dev); 112128953533SBenjamin Herrenschmidt bus_remove_device(dev); 11221da177e4SLinus Torvalds 11232f8d16a9STejun Heo /* 11242f8d16a9STejun Heo * Some platform devices are driven without driver attached 11252f8d16a9STejun Heo * and managed resources may have been acquired. Make sure 11262f8d16a9STejun Heo * all resources are released. 11272f8d16a9STejun Heo */ 11282f8d16a9STejun Heo devres_release_all(dev); 11292f8d16a9STejun Heo 11301da177e4SLinus Torvalds /* Notify the platform of the removal, in case they 11311da177e4SLinus Torvalds * need to do anything... 11321da177e4SLinus Torvalds */ 11331da177e4SLinus Torvalds if (platform_notify_remove) 11341da177e4SLinus Torvalds platform_notify_remove(dev); 1135312c004dSKay Sievers kobject_uevent(&dev->kobj, KOBJ_REMOVE); 1136da231fd5SKay Sievers cleanup_device_parent(dev); 11371da177e4SLinus Torvalds kobject_del(&dev->kobj); 11381da177e4SLinus Torvalds put_device(parent); 11391da177e4SLinus Torvalds } 11401da177e4SLinus Torvalds 11411da177e4SLinus Torvalds /** 11421da177e4SLinus Torvalds * device_unregister - unregister device from system. 11431da177e4SLinus Torvalds * @dev: device going away. 11441da177e4SLinus Torvalds * 11451da177e4SLinus Torvalds * We do this in two parts, like we do device_register(). First, 11461da177e4SLinus Torvalds * we remove it from all the subsystems with device_del(), then 11471da177e4SLinus Torvalds * we decrement the reference count via put_device(). If that 11481da177e4SLinus Torvalds * is the final reference count, the device will be cleaned up 11491da177e4SLinus Torvalds * via device_release() above. Otherwise, the structure will 11501da177e4SLinus Torvalds * stick around until the final reference to the device is dropped. 11511da177e4SLinus Torvalds */ 11521da177e4SLinus Torvalds void device_unregister(struct device *dev) 11531da177e4SLinus Torvalds { 11541e0b2cf9SKay Sievers pr_debug("device: '%s': %s\n", dev_name(dev), __func__); 11551da177e4SLinus Torvalds device_del(dev); 11561da177e4SLinus Torvalds put_device(dev); 11571da177e4SLinus Torvalds } 11581da177e4SLinus Torvalds 115936239577Smochel@digitalimplant.org static struct device *next_device(struct klist_iter *i) 116036239577Smochel@digitalimplant.org { 116136239577Smochel@digitalimplant.org struct klist_node *n = klist_next(i); 1162f791b8c8SGreg Kroah-Hartman struct device *dev = NULL; 1163f791b8c8SGreg Kroah-Hartman struct device_private *p; 1164f791b8c8SGreg Kroah-Hartman 1165f791b8c8SGreg Kroah-Hartman if (n) { 1166f791b8c8SGreg Kroah-Hartman p = to_device_private_parent(n); 1167f791b8c8SGreg Kroah-Hartman dev = p->device; 1168f791b8c8SGreg Kroah-Hartman } 1169f791b8c8SGreg Kroah-Hartman return dev; 117036239577Smochel@digitalimplant.org } 117136239577Smochel@digitalimplant.org 11721da177e4SLinus Torvalds /** 1173e454cea2SKay Sievers * device_get_devnode - path of device node file 11746fcf53acSKay Sievers * @dev: device 1175e454cea2SKay Sievers * @mode: returned file access mode 11766fcf53acSKay Sievers * @tmp: possibly allocated string 11776fcf53acSKay Sievers * 11786fcf53acSKay Sievers * Return the relative path of a possible device node. 11796fcf53acSKay Sievers * Non-default names may need to allocate a memory to compose 11806fcf53acSKay Sievers * a name. This memory is returned in tmp and needs to be 11816fcf53acSKay Sievers * freed by the caller. 11826fcf53acSKay Sievers */ 1183e454cea2SKay Sievers const char *device_get_devnode(struct device *dev, 1184e454cea2SKay Sievers mode_t *mode, const char **tmp) 11856fcf53acSKay Sievers { 11866fcf53acSKay Sievers char *s; 11876fcf53acSKay Sievers 11886fcf53acSKay Sievers *tmp = NULL; 11896fcf53acSKay Sievers 11906fcf53acSKay Sievers /* the device type may provide a specific name */ 1191e454cea2SKay Sievers if (dev->type && dev->type->devnode) 1192e454cea2SKay Sievers *tmp = dev->type->devnode(dev, mode); 11936fcf53acSKay Sievers if (*tmp) 11946fcf53acSKay Sievers return *tmp; 11956fcf53acSKay Sievers 11966fcf53acSKay Sievers /* the class may provide a specific name */ 1197e454cea2SKay Sievers if (dev->class && dev->class->devnode) 1198e454cea2SKay Sievers *tmp = dev->class->devnode(dev, mode); 11996fcf53acSKay Sievers if (*tmp) 12006fcf53acSKay Sievers return *tmp; 12016fcf53acSKay Sievers 12026fcf53acSKay Sievers /* return name without allocation, tmp == NULL */ 12036fcf53acSKay Sievers if (strchr(dev_name(dev), '!') == NULL) 12046fcf53acSKay Sievers return dev_name(dev); 12056fcf53acSKay Sievers 12066fcf53acSKay Sievers /* replace '!' in the name with '/' */ 12076fcf53acSKay Sievers *tmp = kstrdup(dev_name(dev), GFP_KERNEL); 12086fcf53acSKay Sievers if (!*tmp) 12096fcf53acSKay Sievers return NULL; 12106fcf53acSKay Sievers while ((s = strchr(*tmp, '!'))) 12116fcf53acSKay Sievers s[0] = '/'; 12126fcf53acSKay Sievers return *tmp; 12136fcf53acSKay Sievers } 12146fcf53acSKay Sievers 12156fcf53acSKay Sievers /** 12161da177e4SLinus Torvalds * device_for_each_child - device child iterator. 1217c41455fbSRandy Dunlap * @parent: parent struct device. 12181da177e4SLinus Torvalds * @data: data for the callback. 12191da177e4SLinus Torvalds * @fn: function to be called for each device. 12201da177e4SLinus Torvalds * 1221c41455fbSRandy Dunlap * Iterate over @parent's child devices, and call @fn for each, 12221da177e4SLinus Torvalds * passing it @data. 12231da177e4SLinus Torvalds * 12241da177e4SLinus Torvalds * We check the return of @fn each time. If it returns anything 12251da177e4SLinus Torvalds * other than 0, we break out and return that value. 12261da177e4SLinus Torvalds */ 122736239577Smochel@digitalimplant.org int device_for_each_child(struct device *parent, void *data, 12284a3ad20cSGreg Kroah-Hartman int (*fn)(struct device *dev, void *data)) 12291da177e4SLinus Torvalds { 123036239577Smochel@digitalimplant.org struct klist_iter i; 12311da177e4SLinus Torvalds struct device *child; 12321da177e4SLinus Torvalds int error = 0; 12331da177e4SLinus Torvalds 1234014c90dbSGreg Kroah-Hartman if (!parent->p) 1235014c90dbSGreg Kroah-Hartman return 0; 1236014c90dbSGreg Kroah-Hartman 1237f791b8c8SGreg Kroah-Hartman klist_iter_init(&parent->p->klist_children, &i); 123836239577Smochel@digitalimplant.org while ((child = next_device(&i)) && !error) 123936239577Smochel@digitalimplant.org error = fn(child, data); 124036239577Smochel@digitalimplant.org klist_iter_exit(&i); 12411da177e4SLinus Torvalds return error; 12421da177e4SLinus Torvalds } 12431da177e4SLinus Torvalds 12445ab69981SCornelia Huck /** 12455ab69981SCornelia Huck * device_find_child - device iterator for locating a particular device. 12465ab69981SCornelia Huck * @parent: parent struct device 12475ab69981SCornelia Huck * @data: Data to pass to match function 12485ab69981SCornelia Huck * @match: Callback function to check device 12495ab69981SCornelia Huck * 12505ab69981SCornelia Huck * This is similar to the device_for_each_child() function above, but it 12515ab69981SCornelia Huck * returns a reference to a device that is 'found' for later use, as 12525ab69981SCornelia Huck * determined by the @match callback. 12535ab69981SCornelia Huck * 12545ab69981SCornelia Huck * The callback should return 0 if the device doesn't match and non-zero 12555ab69981SCornelia Huck * if it does. If the callback returns non-zero and a reference to the 12565ab69981SCornelia Huck * current device can be obtained, this function will return to the caller 12575ab69981SCornelia Huck * and not iterate over any more devices. 12585ab69981SCornelia Huck */ 12595ab69981SCornelia Huck struct device *device_find_child(struct device *parent, void *data, 12604a3ad20cSGreg Kroah-Hartman int (*match)(struct device *dev, void *data)) 12615ab69981SCornelia Huck { 12625ab69981SCornelia Huck struct klist_iter i; 12635ab69981SCornelia Huck struct device *child; 12645ab69981SCornelia Huck 12655ab69981SCornelia Huck if (!parent) 12665ab69981SCornelia Huck return NULL; 12675ab69981SCornelia Huck 1268f791b8c8SGreg Kroah-Hartman klist_iter_init(&parent->p->klist_children, &i); 12695ab69981SCornelia Huck while ((child = next_device(&i))) 12705ab69981SCornelia Huck if (match(child, data) && get_device(child)) 12715ab69981SCornelia Huck break; 12725ab69981SCornelia Huck klist_iter_exit(&i); 12735ab69981SCornelia Huck return child; 12745ab69981SCornelia Huck } 12755ab69981SCornelia Huck 12761da177e4SLinus Torvalds int __init devices_init(void) 12771da177e4SLinus Torvalds { 1278881c6cfdSGreg Kroah-Hartman devices_kset = kset_create_and_add("devices", &device_uevent_ops, NULL); 1279881c6cfdSGreg Kroah-Hartman if (!devices_kset) 1280881c6cfdSGreg Kroah-Hartman return -ENOMEM; 1281e105b8bfSDan Williams dev_kobj = kobject_create_and_add("dev", NULL); 1282e105b8bfSDan Williams if (!dev_kobj) 1283e105b8bfSDan Williams goto dev_kobj_err; 1284e105b8bfSDan Williams sysfs_dev_block_kobj = kobject_create_and_add("block", dev_kobj); 1285e105b8bfSDan Williams if (!sysfs_dev_block_kobj) 1286e105b8bfSDan Williams goto block_kobj_err; 1287e105b8bfSDan Williams sysfs_dev_char_kobj = kobject_create_and_add("char", dev_kobj); 1288e105b8bfSDan Williams if (!sysfs_dev_char_kobj) 1289e105b8bfSDan Williams goto char_kobj_err; 1290e105b8bfSDan Williams 1291881c6cfdSGreg Kroah-Hartman return 0; 1292e105b8bfSDan Williams 1293e105b8bfSDan Williams char_kobj_err: 1294e105b8bfSDan Williams kobject_put(sysfs_dev_block_kobj); 1295e105b8bfSDan Williams block_kobj_err: 1296e105b8bfSDan Williams kobject_put(dev_kobj); 1297e105b8bfSDan Williams dev_kobj_err: 1298e105b8bfSDan Williams kset_unregister(devices_kset); 1299e105b8bfSDan Williams return -ENOMEM; 13001da177e4SLinus Torvalds } 13011da177e4SLinus Torvalds 13021da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(device_for_each_child); 13035ab69981SCornelia Huck EXPORT_SYMBOL_GPL(device_find_child); 13041da177e4SLinus Torvalds 13051da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(device_initialize); 13061da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(device_add); 13071da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(device_register); 13081da177e4SLinus Torvalds 13091da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(device_del); 13101da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(device_unregister); 13111da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(get_device); 13121da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(put_device); 13131da177e4SLinus Torvalds 13141da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(device_create_file); 13151da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(device_remove_file); 131623681e47SGreg Kroah-Hartman 13170aa0dc41SMark McLoughlin struct root_device 13180aa0dc41SMark McLoughlin { 13190aa0dc41SMark McLoughlin struct device dev; 13200aa0dc41SMark McLoughlin struct module *owner; 13210aa0dc41SMark McLoughlin }; 13220aa0dc41SMark McLoughlin 13230aa0dc41SMark McLoughlin #define to_root_device(dev) container_of(dev, struct root_device, dev) 13240aa0dc41SMark McLoughlin 13250aa0dc41SMark McLoughlin static void root_device_release(struct device *dev) 13260aa0dc41SMark McLoughlin { 13270aa0dc41SMark McLoughlin kfree(to_root_device(dev)); 13280aa0dc41SMark McLoughlin } 13290aa0dc41SMark McLoughlin 13300aa0dc41SMark McLoughlin /** 13310aa0dc41SMark McLoughlin * __root_device_register - allocate and register a root device 13320aa0dc41SMark McLoughlin * @name: root device name 13330aa0dc41SMark McLoughlin * @owner: owner module of the root device, usually THIS_MODULE 13340aa0dc41SMark McLoughlin * 13350aa0dc41SMark McLoughlin * This function allocates a root device and registers it 13360aa0dc41SMark McLoughlin * using device_register(). In order to free the returned 13370aa0dc41SMark McLoughlin * device, use root_device_unregister(). 13380aa0dc41SMark McLoughlin * 13390aa0dc41SMark McLoughlin * Root devices are dummy devices which allow other devices 13400aa0dc41SMark McLoughlin * to be grouped under /sys/devices. Use this function to 13410aa0dc41SMark McLoughlin * allocate a root device and then use it as the parent of 13420aa0dc41SMark McLoughlin * any device which should appear under /sys/devices/{name} 13430aa0dc41SMark McLoughlin * 13440aa0dc41SMark McLoughlin * The /sys/devices/{name} directory will also contain a 13450aa0dc41SMark McLoughlin * 'module' symlink which points to the @owner directory 13460aa0dc41SMark McLoughlin * in sysfs. 13470aa0dc41SMark McLoughlin * 13480aa0dc41SMark McLoughlin * Note: You probably want to use root_device_register(). 13490aa0dc41SMark McLoughlin */ 13500aa0dc41SMark McLoughlin struct device *__root_device_register(const char *name, struct module *owner) 13510aa0dc41SMark McLoughlin { 13520aa0dc41SMark McLoughlin struct root_device *root; 13530aa0dc41SMark McLoughlin int err = -ENOMEM; 13540aa0dc41SMark McLoughlin 13550aa0dc41SMark McLoughlin root = kzalloc(sizeof(struct root_device), GFP_KERNEL); 13560aa0dc41SMark McLoughlin if (!root) 13570aa0dc41SMark McLoughlin return ERR_PTR(err); 13580aa0dc41SMark McLoughlin 1359acc0e90fSGreg Kroah-Hartman err = dev_set_name(&root->dev, "%s", name); 13600aa0dc41SMark McLoughlin if (err) { 13610aa0dc41SMark McLoughlin kfree(root); 13620aa0dc41SMark McLoughlin return ERR_PTR(err); 13630aa0dc41SMark McLoughlin } 13640aa0dc41SMark McLoughlin 13650aa0dc41SMark McLoughlin root->dev.release = root_device_release; 13660aa0dc41SMark McLoughlin 13670aa0dc41SMark McLoughlin err = device_register(&root->dev); 13680aa0dc41SMark McLoughlin if (err) { 13690aa0dc41SMark McLoughlin put_device(&root->dev); 13700aa0dc41SMark McLoughlin return ERR_PTR(err); 13710aa0dc41SMark McLoughlin } 13720aa0dc41SMark McLoughlin 13730aa0dc41SMark McLoughlin #ifdef CONFIG_MODULE /* gotta find a "cleaner" way to do this */ 13740aa0dc41SMark McLoughlin if (owner) { 13750aa0dc41SMark McLoughlin struct module_kobject *mk = &owner->mkobj; 13760aa0dc41SMark McLoughlin 13770aa0dc41SMark McLoughlin err = sysfs_create_link(&root->dev.kobj, &mk->kobj, "module"); 13780aa0dc41SMark McLoughlin if (err) { 13790aa0dc41SMark McLoughlin device_unregister(&root->dev); 13800aa0dc41SMark McLoughlin return ERR_PTR(err); 13810aa0dc41SMark McLoughlin } 13820aa0dc41SMark McLoughlin root->owner = owner; 13830aa0dc41SMark McLoughlin } 13840aa0dc41SMark McLoughlin #endif 13850aa0dc41SMark McLoughlin 13860aa0dc41SMark McLoughlin return &root->dev; 13870aa0dc41SMark McLoughlin } 13880aa0dc41SMark McLoughlin EXPORT_SYMBOL_GPL(__root_device_register); 13890aa0dc41SMark McLoughlin 13900aa0dc41SMark McLoughlin /** 13910aa0dc41SMark McLoughlin * root_device_unregister - unregister and free a root device 13927cbcf225SRandy Dunlap * @dev: device going away 13930aa0dc41SMark McLoughlin * 13940aa0dc41SMark McLoughlin * This function unregisters and cleans up a device that was created by 13950aa0dc41SMark McLoughlin * root_device_register(). 13960aa0dc41SMark McLoughlin */ 13970aa0dc41SMark McLoughlin void root_device_unregister(struct device *dev) 13980aa0dc41SMark McLoughlin { 13990aa0dc41SMark McLoughlin struct root_device *root = to_root_device(dev); 14000aa0dc41SMark McLoughlin 14010aa0dc41SMark McLoughlin if (root->owner) 14020aa0dc41SMark McLoughlin sysfs_remove_link(&root->dev.kobj, "module"); 14030aa0dc41SMark McLoughlin 14040aa0dc41SMark McLoughlin device_unregister(dev); 14050aa0dc41SMark McLoughlin } 14060aa0dc41SMark McLoughlin EXPORT_SYMBOL_GPL(root_device_unregister); 14070aa0dc41SMark McLoughlin 140823681e47SGreg Kroah-Hartman 140923681e47SGreg Kroah-Hartman static void device_create_release(struct device *dev) 141023681e47SGreg Kroah-Hartman { 14111e0b2cf9SKay Sievers pr_debug("device: '%s': %s\n", dev_name(dev), __func__); 141223681e47SGreg Kroah-Hartman kfree(dev); 141323681e47SGreg Kroah-Hartman } 141423681e47SGreg Kroah-Hartman 141523681e47SGreg Kroah-Hartman /** 14168882b394SGreg Kroah-Hartman * device_create_vargs - creates a device and registers it with sysfs 14178882b394SGreg Kroah-Hartman * @class: pointer to the struct class that this device should be registered to 14188882b394SGreg Kroah-Hartman * @parent: pointer to the parent struct device of this new device, if any 14198882b394SGreg Kroah-Hartman * @devt: the dev_t for the char device to be added 14208882b394SGreg Kroah-Hartman * @drvdata: the data to be added to the device for callbacks 14218882b394SGreg Kroah-Hartman * @fmt: string for the device's name 14228882b394SGreg Kroah-Hartman * @args: va_list for the device's name 14238882b394SGreg Kroah-Hartman * 14248882b394SGreg Kroah-Hartman * This function can be used by char device classes. A struct device 14258882b394SGreg Kroah-Hartman * will be created in sysfs, registered to the specified class. 14268882b394SGreg Kroah-Hartman * 14278882b394SGreg Kroah-Hartman * A "dev" file will be created, showing the dev_t for the device, if 14288882b394SGreg Kroah-Hartman * the dev_t is not 0,0. 14298882b394SGreg Kroah-Hartman * If a pointer to a parent struct device is passed in, the newly created 14308882b394SGreg Kroah-Hartman * struct device will be a child of that device in sysfs. 14318882b394SGreg Kroah-Hartman * The pointer to the struct device will be returned from the call. 14328882b394SGreg Kroah-Hartman * Any further sysfs files that might be required can be created using this 14338882b394SGreg Kroah-Hartman * pointer. 14348882b394SGreg Kroah-Hartman * 14358882b394SGreg Kroah-Hartman * Note: the struct class passed to this function must have previously 14368882b394SGreg Kroah-Hartman * been created with a call to class_create(). 14378882b394SGreg Kroah-Hartman */ 14388882b394SGreg Kroah-Hartman struct device *device_create_vargs(struct class *class, struct device *parent, 14398882b394SGreg Kroah-Hartman dev_t devt, void *drvdata, const char *fmt, 14408882b394SGreg Kroah-Hartman va_list args) 14418882b394SGreg Kroah-Hartman { 14428882b394SGreg Kroah-Hartman struct device *dev = NULL; 14438882b394SGreg Kroah-Hartman int retval = -ENODEV; 14448882b394SGreg Kroah-Hartman 14458882b394SGreg Kroah-Hartman if (class == NULL || IS_ERR(class)) 14468882b394SGreg Kroah-Hartman goto error; 14478882b394SGreg Kroah-Hartman 14488882b394SGreg Kroah-Hartman dev = kzalloc(sizeof(*dev), GFP_KERNEL); 14498882b394SGreg Kroah-Hartman if (!dev) { 14508882b394SGreg Kroah-Hartman retval = -ENOMEM; 14518882b394SGreg Kroah-Hartman goto error; 14528882b394SGreg Kroah-Hartman } 14538882b394SGreg Kroah-Hartman 14548882b394SGreg Kroah-Hartman dev->devt = devt; 14558882b394SGreg Kroah-Hartman dev->class = class; 14568882b394SGreg Kroah-Hartman dev->parent = parent; 14578882b394SGreg Kroah-Hartman dev->release = device_create_release; 14588882b394SGreg Kroah-Hartman dev_set_drvdata(dev, drvdata); 14598882b394SGreg Kroah-Hartman 14601fa5ae85SKay Sievers retval = kobject_set_name_vargs(&dev->kobj, fmt, args); 14611fa5ae85SKay Sievers if (retval) 14621fa5ae85SKay Sievers goto error; 14631fa5ae85SKay Sievers 14648882b394SGreg Kroah-Hartman retval = device_register(dev); 14658882b394SGreg Kroah-Hartman if (retval) 14668882b394SGreg Kroah-Hartman goto error; 14678882b394SGreg Kroah-Hartman 14688882b394SGreg Kroah-Hartman return dev; 14698882b394SGreg Kroah-Hartman 14708882b394SGreg Kroah-Hartman error: 1471286661b3SCornelia Huck put_device(dev); 14728882b394SGreg Kroah-Hartman return ERR_PTR(retval); 14738882b394SGreg Kroah-Hartman } 14748882b394SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(device_create_vargs); 14758882b394SGreg Kroah-Hartman 14768882b394SGreg Kroah-Hartman /** 14774e106739SGreg Kroah-Hartman * device_create - creates a device and registers it with sysfs 14788882b394SGreg Kroah-Hartman * @class: pointer to the struct class that this device should be registered to 14798882b394SGreg Kroah-Hartman * @parent: pointer to the parent struct device of this new device, if any 14808882b394SGreg Kroah-Hartman * @devt: the dev_t for the char device to be added 14818882b394SGreg Kroah-Hartman * @drvdata: the data to be added to the device for callbacks 14828882b394SGreg Kroah-Hartman * @fmt: string for the device's name 14838882b394SGreg Kroah-Hartman * 14848882b394SGreg Kroah-Hartman * This function can be used by char device classes. A struct device 14858882b394SGreg Kroah-Hartman * will be created in sysfs, registered to the specified class. 14868882b394SGreg Kroah-Hartman * 14878882b394SGreg Kroah-Hartman * A "dev" file will be created, showing the dev_t for the device, if 14888882b394SGreg Kroah-Hartman * the dev_t is not 0,0. 14898882b394SGreg Kroah-Hartman * If a pointer to a parent struct device is passed in, the newly created 14908882b394SGreg Kroah-Hartman * struct device will be a child of that device in sysfs. 14918882b394SGreg Kroah-Hartman * The pointer to the struct device will be returned from the call. 14928882b394SGreg Kroah-Hartman * Any further sysfs files that might be required can be created using this 14938882b394SGreg Kroah-Hartman * pointer. 14948882b394SGreg Kroah-Hartman * 14958882b394SGreg Kroah-Hartman * Note: the struct class passed to this function must have previously 14968882b394SGreg Kroah-Hartman * been created with a call to class_create(). 14978882b394SGreg Kroah-Hartman */ 14984e106739SGreg Kroah-Hartman struct device *device_create(struct class *class, struct device *parent, 14994e106739SGreg Kroah-Hartman dev_t devt, void *drvdata, const char *fmt, ...) 15008882b394SGreg Kroah-Hartman { 15018882b394SGreg Kroah-Hartman va_list vargs; 15028882b394SGreg Kroah-Hartman struct device *dev; 15038882b394SGreg Kroah-Hartman 15048882b394SGreg Kroah-Hartman va_start(vargs, fmt); 15058882b394SGreg Kroah-Hartman dev = device_create_vargs(class, parent, devt, drvdata, fmt, vargs); 15068882b394SGreg Kroah-Hartman va_end(vargs); 15078882b394SGreg Kroah-Hartman return dev; 15088882b394SGreg Kroah-Hartman } 15094e106739SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(device_create); 15108882b394SGreg Kroah-Hartman 1511cd35449bSDave Young static int __match_devt(struct device *dev, void *data) 151223681e47SGreg Kroah-Hartman { 1513cd35449bSDave Young dev_t *devt = data; 151423681e47SGreg Kroah-Hartman 1515cd35449bSDave Young return dev->devt == *devt; 1516775b64d2SRafael J. Wysocki } 151723681e47SGreg Kroah-Hartman 1518775b64d2SRafael J. Wysocki /** 1519775b64d2SRafael J. Wysocki * device_destroy - removes a device that was created with device_create() 1520775b64d2SRafael J. Wysocki * @class: pointer to the struct class that this device was registered with 1521775b64d2SRafael J. Wysocki * @devt: the dev_t of the device that was previously registered 1522775b64d2SRafael J. Wysocki * 1523775b64d2SRafael J. Wysocki * This call unregisters and cleans up a device that was created with a 1524775b64d2SRafael J. Wysocki * call to device_create(). 1525775b64d2SRafael J. Wysocki */ 1526775b64d2SRafael J. Wysocki void device_destroy(struct class *class, dev_t devt) 1527775b64d2SRafael J. Wysocki { 1528775b64d2SRafael J. Wysocki struct device *dev; 1529775b64d2SRafael J. Wysocki 1530695794aeSGreg Kroah-Hartman dev = class_find_device(class, NULL, &devt, __match_devt); 1531cd35449bSDave Young if (dev) { 1532cd35449bSDave Young put_device(dev); 153323681e47SGreg Kroah-Hartman device_unregister(dev); 153423681e47SGreg Kroah-Hartman } 1535cd35449bSDave Young } 153623681e47SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(device_destroy); 1537a2de48caSGreg Kroah-Hartman 1538a2de48caSGreg Kroah-Hartman /** 1539a2de48caSGreg Kroah-Hartman * device_rename - renames a device 1540a2de48caSGreg Kroah-Hartman * @dev: the pointer to the struct device to be renamed 1541a2de48caSGreg Kroah-Hartman * @new_name: the new name of the device 1542030c1d2bSEric W. Biederman * 1543030c1d2bSEric W. Biederman * It is the responsibility of the caller to provide mutual 1544030c1d2bSEric W. Biederman * exclusion between two different calls of device_rename 1545030c1d2bSEric W. Biederman * on the same device to ensure that new_name is valid and 1546030c1d2bSEric W. Biederman * won't conflict with other devices. 1547a2de48caSGreg Kroah-Hartman */ 1548a2de48caSGreg Kroah-Hartman int device_rename(struct device *dev, char *new_name) 1549a2de48caSGreg Kroah-Hartman { 1550a2de48caSGreg Kroah-Hartman char *old_class_name = NULL; 1551a2de48caSGreg Kroah-Hartman char *new_class_name = NULL; 15522ee97cafSCornelia Huck char *old_device_name = NULL; 1553a2de48caSGreg Kroah-Hartman int error; 1554a2de48caSGreg Kroah-Hartman 1555a2de48caSGreg Kroah-Hartman dev = get_device(dev); 1556a2de48caSGreg Kroah-Hartman if (!dev) 1557a2de48caSGreg Kroah-Hartman return -EINVAL; 1558a2de48caSGreg Kroah-Hartman 15591e0b2cf9SKay Sievers pr_debug("device: '%s': %s: renaming to '%s'\n", dev_name(dev), 15602b3a302aSHarvey Harrison __func__, new_name); 1561a2de48caSGreg Kroah-Hartman 156299ef3ef8SKay Sievers #ifdef CONFIG_SYSFS_DEPRECATED 1563a2de48caSGreg Kroah-Hartman if ((dev->class) && (dev->parent)) 1564a2de48caSGreg Kroah-Hartman old_class_name = make_class_name(dev->class->name, &dev->kobj); 156599ef3ef8SKay Sievers #endif 1566a2de48caSGreg Kroah-Hartman 15671fa5ae85SKay Sievers old_device_name = kstrdup(dev_name(dev), GFP_KERNEL); 15682ee97cafSCornelia Huck if (!old_device_name) { 1569952ab431SJesper Juhl error = -ENOMEM; 15702ee97cafSCornelia Huck goto out; 1571952ab431SJesper Juhl } 1572a2de48caSGreg Kroah-Hartman 1573a2de48caSGreg Kroah-Hartman error = kobject_rename(&dev->kobj, new_name); 15741fa5ae85SKay Sievers if (error) 15752ee97cafSCornelia Huck goto out; 1576a2de48caSGreg Kroah-Hartman 157799ef3ef8SKay Sievers #ifdef CONFIG_SYSFS_DEPRECATED 1578a2de48caSGreg Kroah-Hartman if (old_class_name) { 1579a2de48caSGreg Kroah-Hartman new_class_name = make_class_name(dev->class->name, &dev->kobj); 1580a2de48caSGreg Kroah-Hartman if (new_class_name) { 15812354dcc7SEric W. Biederman error = sysfs_rename_link(&dev->parent->kobj, 158236ce6dadSCornelia Huck &dev->kobj, 15832354dcc7SEric W. Biederman old_class_name, 158436ce6dadSCornelia Huck new_class_name); 1585a2de48caSGreg Kroah-Hartman } 1586a2de48caSGreg Kroah-Hartman } 158760b8cabdSKay Sievers #else 1588a2de48caSGreg Kroah-Hartman if (dev->class) { 15892354dcc7SEric W. Biederman error = sysfs_rename_link(&dev->class->p->class_subsys.kobj, 15902354dcc7SEric W. Biederman &dev->kobj, old_device_name, new_name); 15912ee97cafSCornelia Huck } 159260b8cabdSKay Sievers #endif 159360b8cabdSKay Sievers 15942ee97cafSCornelia Huck out: 1595a2de48caSGreg Kroah-Hartman put_device(dev); 1596a2de48caSGreg Kroah-Hartman 1597a2de48caSGreg Kroah-Hartman kfree(new_class_name); 1598952ab431SJesper Juhl kfree(old_class_name); 15992ee97cafSCornelia Huck kfree(old_device_name); 1600a2de48caSGreg Kroah-Hartman 1601a2de48caSGreg Kroah-Hartman return error; 1602a2de48caSGreg Kroah-Hartman } 1603a2807dbcSJohannes Berg EXPORT_SYMBOL_GPL(device_rename); 16048a82472fSCornelia Huck 16058a82472fSCornelia Huck static int device_move_class_links(struct device *dev, 16068a82472fSCornelia Huck struct device *old_parent, 16078a82472fSCornelia Huck struct device *new_parent) 16088a82472fSCornelia Huck { 1609f7f3461dSGreg Kroah-Hartman int error = 0; 16108a82472fSCornelia Huck #ifdef CONFIG_SYSFS_DEPRECATED 16118a82472fSCornelia Huck char *class_name; 16128a82472fSCornelia Huck 16138a82472fSCornelia Huck class_name = make_class_name(dev->class->name, &dev->kobj); 16148a82472fSCornelia Huck if (!class_name) { 1615cb360bbfSCornelia Huck error = -ENOMEM; 16168a82472fSCornelia Huck goto out; 16178a82472fSCornelia Huck } 16188a82472fSCornelia Huck if (old_parent) { 16198a82472fSCornelia Huck sysfs_remove_link(&dev->kobj, "device"); 16208a82472fSCornelia Huck sysfs_remove_link(&old_parent->kobj, class_name); 16218a82472fSCornelia Huck } 1622c744aeaeSCornelia Huck if (new_parent) { 1623c744aeaeSCornelia Huck error = sysfs_create_link(&dev->kobj, &new_parent->kobj, 1624c744aeaeSCornelia Huck "device"); 16258a82472fSCornelia Huck if (error) 16268a82472fSCornelia Huck goto out; 1627c744aeaeSCornelia Huck error = sysfs_create_link(&new_parent->kobj, &dev->kobj, 1628c744aeaeSCornelia Huck class_name); 16298a82472fSCornelia Huck if (error) 16308a82472fSCornelia Huck sysfs_remove_link(&dev->kobj, "device"); 16314a3ad20cSGreg Kroah-Hartman } else 1632c744aeaeSCornelia Huck error = 0; 16338a82472fSCornelia Huck out: 16348a82472fSCornelia Huck kfree(class_name); 16358a82472fSCornelia Huck return error; 16368a82472fSCornelia Huck #else 1637f7f3461dSGreg Kroah-Hartman if (old_parent) 1638f7f3461dSGreg Kroah-Hartman sysfs_remove_link(&dev->kobj, "device"); 1639f7f3461dSGreg Kroah-Hartman if (new_parent) 1640f7f3461dSGreg Kroah-Hartman error = sysfs_create_link(&dev->kobj, &new_parent->kobj, 1641f7f3461dSGreg Kroah-Hartman "device"); 1642f7f3461dSGreg Kroah-Hartman return error; 16438a82472fSCornelia Huck #endif 16448a82472fSCornelia Huck } 16458a82472fSCornelia Huck 16468a82472fSCornelia Huck /** 16478a82472fSCornelia Huck * device_move - moves a device to a new parent 16488a82472fSCornelia Huck * @dev: the pointer to the struct device to be moved 1649c744aeaeSCornelia Huck * @new_parent: the new parent of the device (can by NULL) 1650ffa6a705SCornelia Huck * @dpm_order: how to reorder the dpm_list 16518a82472fSCornelia Huck */ 1652ffa6a705SCornelia Huck int device_move(struct device *dev, struct device *new_parent, 1653ffa6a705SCornelia Huck enum dpm_order dpm_order) 16548a82472fSCornelia Huck { 16558a82472fSCornelia Huck int error; 16568a82472fSCornelia Huck struct device *old_parent; 1657c744aeaeSCornelia Huck struct kobject *new_parent_kobj; 16588a82472fSCornelia Huck 16598a82472fSCornelia Huck dev = get_device(dev); 16608a82472fSCornelia Huck if (!dev) 16618a82472fSCornelia Huck return -EINVAL; 16628a82472fSCornelia Huck 1663ffa6a705SCornelia Huck device_pm_lock(); 16648a82472fSCornelia Huck new_parent = get_device(new_parent); 1665c744aeaeSCornelia Huck new_parent_kobj = get_device_parent(dev, new_parent); 166663b6971aSCornelia Huck 16671e0b2cf9SKay Sievers pr_debug("device: '%s': %s: moving to '%s'\n", dev_name(dev), 16681e0b2cf9SKay Sievers __func__, new_parent ? dev_name(new_parent) : "<NULL>"); 1669c744aeaeSCornelia Huck error = kobject_move(&dev->kobj, new_parent_kobj); 16708a82472fSCornelia Huck if (error) { 167163b6971aSCornelia Huck cleanup_glue_dir(dev, new_parent_kobj); 16728a82472fSCornelia Huck put_device(new_parent); 16738a82472fSCornelia Huck goto out; 16748a82472fSCornelia Huck } 16758a82472fSCornelia Huck old_parent = dev->parent; 16768a82472fSCornelia Huck dev->parent = new_parent; 16778a82472fSCornelia Huck if (old_parent) 1678f791b8c8SGreg Kroah-Hartman klist_remove(&dev->p->knode_parent); 16790d358f22SYinghai Lu if (new_parent) { 1680f791b8c8SGreg Kroah-Hartman klist_add_tail(&dev->p->knode_parent, 1681f791b8c8SGreg Kroah-Hartman &new_parent->p->klist_children); 16820d358f22SYinghai Lu set_dev_node(dev, dev_to_node(new_parent)); 16830d358f22SYinghai Lu } 16840d358f22SYinghai Lu 16858a82472fSCornelia Huck if (!dev->class) 16868a82472fSCornelia Huck goto out_put; 16878a82472fSCornelia Huck error = device_move_class_links(dev, old_parent, new_parent); 16888a82472fSCornelia Huck if (error) { 16898a82472fSCornelia Huck /* We ignore errors on cleanup since we're hosed anyway... */ 16908a82472fSCornelia Huck device_move_class_links(dev, new_parent, old_parent); 16918a82472fSCornelia Huck if (!kobject_move(&dev->kobj, &old_parent->kobj)) { 1692c744aeaeSCornelia Huck if (new_parent) 1693f791b8c8SGreg Kroah-Hartman klist_remove(&dev->p->knode_parent); 16940d358f22SYinghai Lu dev->parent = old_parent; 16950d358f22SYinghai Lu if (old_parent) { 1696f791b8c8SGreg Kroah-Hartman klist_add_tail(&dev->p->knode_parent, 1697f791b8c8SGreg Kroah-Hartman &old_parent->p->klist_children); 16980d358f22SYinghai Lu set_dev_node(dev, dev_to_node(old_parent)); 16990d358f22SYinghai Lu } 17008a82472fSCornelia Huck } 170163b6971aSCornelia Huck cleanup_glue_dir(dev, new_parent_kobj); 17028a82472fSCornelia Huck put_device(new_parent); 17038a82472fSCornelia Huck goto out; 17048a82472fSCornelia Huck } 1705ffa6a705SCornelia Huck switch (dpm_order) { 1706ffa6a705SCornelia Huck case DPM_ORDER_NONE: 1707ffa6a705SCornelia Huck break; 1708ffa6a705SCornelia Huck case DPM_ORDER_DEV_AFTER_PARENT: 1709ffa6a705SCornelia Huck device_pm_move_after(dev, new_parent); 1710ffa6a705SCornelia Huck break; 1711ffa6a705SCornelia Huck case DPM_ORDER_PARENT_BEFORE_DEV: 1712ffa6a705SCornelia Huck device_pm_move_before(new_parent, dev); 1713ffa6a705SCornelia Huck break; 1714ffa6a705SCornelia Huck case DPM_ORDER_DEV_LAST: 1715ffa6a705SCornelia Huck device_pm_move_last(dev); 1716ffa6a705SCornelia Huck break; 1717ffa6a705SCornelia Huck } 17188a82472fSCornelia Huck out_put: 17198a82472fSCornelia Huck put_device(old_parent); 17208a82472fSCornelia Huck out: 1721ffa6a705SCornelia Huck device_pm_unlock(); 17228a82472fSCornelia Huck put_device(dev); 17238a82472fSCornelia Huck return error; 17248a82472fSCornelia Huck } 17258a82472fSCornelia Huck EXPORT_SYMBOL_GPL(device_move); 172637b0c020SGreg Kroah-Hartman 172737b0c020SGreg Kroah-Hartman /** 172837b0c020SGreg Kroah-Hartman * device_shutdown - call ->shutdown() on each device to shutdown. 172937b0c020SGreg Kroah-Hartman */ 173037b0c020SGreg Kroah-Hartman void device_shutdown(void) 173137b0c020SGreg Kroah-Hartman { 173237b0c020SGreg Kroah-Hartman struct device *dev, *devn; 173337b0c020SGreg Kroah-Hartman 173437b0c020SGreg Kroah-Hartman list_for_each_entry_safe_reverse(dev, devn, &devices_kset->list, 173537b0c020SGreg Kroah-Hartman kobj.entry) { 173637b0c020SGreg Kroah-Hartman if (dev->bus && dev->bus->shutdown) { 173737b0c020SGreg Kroah-Hartman dev_dbg(dev, "shutdown\n"); 173837b0c020SGreg Kroah-Hartman dev->bus->shutdown(dev); 173937b0c020SGreg Kroah-Hartman } else if (dev->driver && dev->driver->shutdown) { 174037b0c020SGreg Kroah-Hartman dev_dbg(dev, "shutdown\n"); 174137b0c020SGreg Kroah-Hartman dev->driver->shutdown(dev); 174237b0c020SGreg Kroah-Hartman } 174337b0c020SGreg Kroah-Hartman } 1744401097eaSShaohua Li async_synchronize_full(); 174537b0c020SGreg Kroah-Hartman } 1746