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> 23f75b1c60SDave Young #include <linux/mutex.h> 24401097eaSShaohua Li #include <linux/async.h> 251da177e4SLinus Torvalds 261da177e4SLinus Torvalds #include "base.h" 271da177e4SLinus Torvalds #include "power/power.h" 281da177e4SLinus Torvalds 291da177e4SLinus Torvalds int (*platform_notify)(struct device *dev) = NULL; 301da177e4SLinus Torvalds int (*platform_notify_remove)(struct device *dev) = NULL; 31e105b8bfSDan Williams static struct kobject *dev_kobj; 32e105b8bfSDan Williams struct kobject *sysfs_dev_char_kobj; 33e105b8bfSDan Williams struct kobject *sysfs_dev_block_kobj; 341da177e4SLinus Torvalds 354e886c29SGreg Kroah-Hartman #ifdef CONFIG_BLOCK 364e886c29SGreg Kroah-Hartman static inline int device_is_not_partition(struct device *dev) 374e886c29SGreg Kroah-Hartman { 384e886c29SGreg Kroah-Hartman return !(dev->type == &part_type); 394e886c29SGreg Kroah-Hartman } 404e886c29SGreg Kroah-Hartman #else 414e886c29SGreg Kroah-Hartman static inline int device_is_not_partition(struct device *dev) 424e886c29SGreg Kroah-Hartman { 434e886c29SGreg Kroah-Hartman return 1; 444e886c29SGreg Kroah-Hartman } 454e886c29SGreg Kroah-Hartman #endif 461da177e4SLinus Torvalds 473e95637aSAlan Stern /** 483e95637aSAlan Stern * dev_driver_string - Return a device's driver name, if at all possible 493e95637aSAlan Stern * @dev: struct device to get the name of 503e95637aSAlan Stern * 513e95637aSAlan Stern * Will return the device's driver's name if it is bound to a device. If 523e95637aSAlan Stern * the device is not bound to a device, it will return the name of the bus 533e95637aSAlan Stern * it is attached to. If it is not attached to a bus either, an empty 543e95637aSAlan Stern * string will be returned. 553e95637aSAlan Stern */ 56bf9ca69fSJean Delvare const char *dev_driver_string(const struct device *dev) 573e95637aSAlan Stern { 583589972eSAlan Stern struct device_driver *drv; 593589972eSAlan Stern 603589972eSAlan Stern /* dev->driver can change to NULL underneath us because of unbinding, 613589972eSAlan Stern * so be careful about accessing it. dev->bus and dev->class should 623589972eSAlan Stern * never change once they are set, so they don't need special care. 633589972eSAlan Stern */ 643589972eSAlan Stern drv = ACCESS_ONCE(dev->driver); 653589972eSAlan Stern return drv ? drv->name : 66a456b702SJean Delvare (dev->bus ? dev->bus->name : 67a456b702SJean Delvare (dev->class ? dev->class->name : "")); 683e95637aSAlan Stern } 69310a922dSMatthew Wilcox EXPORT_SYMBOL(dev_driver_string); 703e95637aSAlan Stern 711da177e4SLinus Torvalds #define to_dev(obj) container_of(obj, struct device, kobj) 721da177e4SLinus Torvalds #define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr) 731da177e4SLinus Torvalds 744a3ad20cSGreg Kroah-Hartman static ssize_t dev_attr_show(struct kobject *kobj, struct attribute *attr, 754a3ad20cSGreg Kroah-Hartman char *buf) 761da177e4SLinus Torvalds { 771da177e4SLinus Torvalds struct device_attribute *dev_attr = to_dev_attr(attr); 781da177e4SLinus Torvalds struct device *dev = to_dev(kobj); 794a0c20bfSDmitry Torokhov ssize_t ret = -EIO; 801da177e4SLinus Torvalds 811da177e4SLinus Torvalds if (dev_attr->show) 8254b6f35cSYani Ioannou ret = dev_attr->show(dev, dev_attr, buf); 83815d2d50SAndrew Morton if (ret >= (ssize_t)PAGE_SIZE) { 84815d2d50SAndrew Morton print_symbol("dev_attr_show: %s returned bad count\n", 85815d2d50SAndrew Morton (unsigned long)dev_attr->show); 86815d2d50SAndrew Morton } 871da177e4SLinus Torvalds return ret; 881da177e4SLinus Torvalds } 891da177e4SLinus Torvalds 904a3ad20cSGreg Kroah-Hartman static ssize_t dev_attr_store(struct kobject *kobj, struct attribute *attr, 911da177e4SLinus Torvalds const char *buf, size_t count) 921da177e4SLinus Torvalds { 931da177e4SLinus Torvalds struct device_attribute *dev_attr = to_dev_attr(attr); 941da177e4SLinus Torvalds struct device *dev = to_dev(kobj); 954a0c20bfSDmitry Torokhov ssize_t ret = -EIO; 961da177e4SLinus Torvalds 971da177e4SLinus Torvalds if (dev_attr->store) 9854b6f35cSYani Ioannou ret = dev_attr->store(dev, dev_attr, buf, count); 991da177e4SLinus Torvalds return ret; 1001da177e4SLinus Torvalds } 1011da177e4SLinus Torvalds 10252cf25d0SEmese Revfy static const struct sysfs_ops dev_sysfs_ops = { 1031da177e4SLinus Torvalds .show = dev_attr_show, 1041da177e4SLinus Torvalds .store = dev_attr_store, 1051da177e4SLinus Torvalds }; 1061da177e4SLinus Torvalds 1071da177e4SLinus Torvalds 1081da177e4SLinus Torvalds /** 1091da177e4SLinus Torvalds * device_release - free device structure. 1101da177e4SLinus Torvalds * @kobj: device's kobject. 1111da177e4SLinus Torvalds * 1121da177e4SLinus Torvalds * This is called once the reference count for the object 1131da177e4SLinus Torvalds * reaches 0. We forward the call to the device's release 1141da177e4SLinus Torvalds * method, which should handle actually freeing the structure. 1151da177e4SLinus Torvalds */ 1161da177e4SLinus Torvalds static void device_release(struct kobject *kobj) 1171da177e4SLinus Torvalds { 1181da177e4SLinus Torvalds struct device *dev = to_dev(kobj); 119fb069a5dSGreg Kroah-Hartman struct device_private *p = dev->p; 1201da177e4SLinus Torvalds 1211da177e4SLinus Torvalds if (dev->release) 1221da177e4SLinus Torvalds dev->release(dev); 123f9f852dfSKay Sievers else if (dev->type && dev->type->release) 124f9f852dfSKay Sievers dev->type->release(dev); 1252620efefSGreg Kroah-Hartman else if (dev->class && dev->class->dev_release) 1262620efefSGreg Kroah-Hartman dev->class->dev_release(dev); 127f810a5cfSArjan van de Ven else 128f810a5cfSArjan van de Ven WARN(1, KERN_ERR "Device '%s' does not have a release() " 1294a3ad20cSGreg Kroah-Hartman "function, it is broken and must be fixed.\n", 1301e0b2cf9SKay Sievers dev_name(dev)); 131fb069a5dSGreg Kroah-Hartman kfree(p); 1321da177e4SLinus Torvalds } 1331da177e4SLinus Torvalds 134bc451f20SEric W. Biederman static const void *device_namespace(struct kobject *kobj) 135bc451f20SEric W. Biederman { 136bc451f20SEric W. Biederman struct device *dev = to_dev(kobj); 137bc451f20SEric W. Biederman const void *ns = NULL; 138bc451f20SEric W. Biederman 139bc451f20SEric W. Biederman if (dev->class && dev->class->ns_type) 140bc451f20SEric W. Biederman ns = dev->class->namespace(dev); 141bc451f20SEric W. Biederman 142bc451f20SEric W. Biederman return ns; 143bc451f20SEric W. Biederman } 144bc451f20SEric W. Biederman 1458f4afc41SGreg Kroah-Hartman static struct kobj_type device_ktype = { 1461da177e4SLinus Torvalds .release = device_release, 1471da177e4SLinus Torvalds .sysfs_ops = &dev_sysfs_ops, 148bc451f20SEric W. Biederman .namespace = device_namespace, 1491da177e4SLinus Torvalds }; 1501da177e4SLinus Torvalds 1511da177e4SLinus Torvalds 152312c004dSKay Sievers static int dev_uevent_filter(struct kset *kset, struct kobject *kobj) 1531da177e4SLinus Torvalds { 1541da177e4SLinus Torvalds struct kobj_type *ktype = get_ktype(kobj); 1551da177e4SLinus Torvalds 1568f4afc41SGreg Kroah-Hartman if (ktype == &device_ktype) { 1571da177e4SLinus Torvalds struct device *dev = to_dev(kobj); 1581da177e4SLinus Torvalds if (dev->bus) 1591da177e4SLinus Torvalds return 1; 16023681e47SGreg Kroah-Hartman if (dev->class) 16123681e47SGreg Kroah-Hartman return 1; 1621da177e4SLinus Torvalds } 1631da177e4SLinus Torvalds return 0; 1641da177e4SLinus Torvalds } 1651da177e4SLinus Torvalds 166312c004dSKay Sievers static const char *dev_uevent_name(struct kset *kset, struct kobject *kobj) 1671da177e4SLinus Torvalds { 1681da177e4SLinus Torvalds struct device *dev = to_dev(kobj); 1691da177e4SLinus Torvalds 17023681e47SGreg Kroah-Hartman if (dev->bus) 1711da177e4SLinus Torvalds return dev->bus->name; 17223681e47SGreg Kroah-Hartman if (dev->class) 17323681e47SGreg Kroah-Hartman return dev->class->name; 17423681e47SGreg Kroah-Hartman return NULL; 1751da177e4SLinus Torvalds } 1761da177e4SLinus Torvalds 1777eff2e7aSKay Sievers static int dev_uevent(struct kset *kset, struct kobject *kobj, 1787eff2e7aSKay Sievers struct kobj_uevent_env *env) 1791da177e4SLinus Torvalds { 1801da177e4SLinus Torvalds struct device *dev = to_dev(kobj); 1811da177e4SLinus Torvalds int retval = 0; 1821da177e4SLinus Torvalds 1836fcf53acSKay Sievers /* add device node properties if present */ 18423681e47SGreg Kroah-Hartman if (MAJOR(dev->devt)) { 1856fcf53acSKay Sievers const char *tmp; 1866fcf53acSKay Sievers const char *name; 187e454cea2SKay Sievers mode_t mode = 0; 1886fcf53acSKay Sievers 1897eff2e7aSKay Sievers add_uevent_var(env, "MAJOR=%u", MAJOR(dev->devt)); 1907eff2e7aSKay Sievers add_uevent_var(env, "MINOR=%u", MINOR(dev->devt)); 191e454cea2SKay Sievers name = device_get_devnode(dev, &mode, &tmp); 1926fcf53acSKay Sievers if (name) { 1936fcf53acSKay Sievers add_uevent_var(env, "DEVNAME=%s", name); 1946fcf53acSKay Sievers kfree(tmp); 195e454cea2SKay Sievers if (mode) 196e454cea2SKay Sievers add_uevent_var(env, "DEVMODE=%#o", mode & 0777); 1976fcf53acSKay Sievers } 19823681e47SGreg Kroah-Hartman } 19923681e47SGreg Kroah-Hartman 200414264f9SKay Sievers if (dev->type && dev->type->name) 2017eff2e7aSKay Sievers add_uevent_var(env, "DEVTYPE=%s", dev->type->name); 202414264f9SKay Sievers 203239378f1SKay Sievers if (dev->driver) 2047eff2e7aSKay Sievers add_uevent_var(env, "DRIVER=%s", dev->driver->name); 205239378f1SKay Sievers 206a87cb2acSKay Sievers #ifdef CONFIG_SYSFS_DEPRECATED 207239378f1SKay Sievers if (dev->class) { 208239378f1SKay Sievers struct device *parent = dev->parent; 209239378f1SKay Sievers 210239378f1SKay Sievers /* find first bus device in parent chain */ 211239378f1SKay Sievers while (parent && !parent->bus) 212239378f1SKay Sievers parent = parent->parent; 213239378f1SKay Sievers if (parent && parent->bus) { 214239378f1SKay Sievers const char *path; 215239378f1SKay Sievers 216239378f1SKay Sievers path = kobject_get_path(&parent->kobj, GFP_KERNEL); 2172c7afd12SKay Sievers if (path) { 2187eff2e7aSKay Sievers add_uevent_var(env, "PHYSDEVPATH=%s", path); 219239378f1SKay Sievers kfree(path); 2202c7afd12SKay Sievers } 221239378f1SKay Sievers 2227eff2e7aSKay Sievers add_uevent_var(env, "PHYSDEVBUS=%s", parent->bus->name); 223239378f1SKay Sievers 224239378f1SKay Sievers if (parent->driver) 2257eff2e7aSKay Sievers add_uevent_var(env, "PHYSDEVDRIVER=%s", 2267eff2e7aSKay Sievers parent->driver->name); 227239378f1SKay Sievers } 228239378f1SKay Sievers } else if (dev->bus) { 2297eff2e7aSKay Sievers add_uevent_var(env, "PHYSDEVBUS=%s", dev->bus->name); 230239378f1SKay Sievers 231239378f1SKay Sievers if (dev->driver) 2324a3ad20cSGreg Kroah-Hartman add_uevent_var(env, "PHYSDEVDRIVER=%s", 2334a3ad20cSGreg Kroah-Hartman dev->driver->name); 234d81d9d6bSKay Sievers } 235239378f1SKay Sievers #endif 2361da177e4SLinus Torvalds 2371da177e4SLinus Torvalds /* have the bus specific function add its stuff */ 2387eff2e7aSKay Sievers if (dev->bus && dev->bus->uevent) { 2397eff2e7aSKay Sievers retval = dev->bus->uevent(dev, env); 240f9f852dfSKay Sievers if (retval) 2417dc72b28SGreg Kroah-Hartman pr_debug("device: '%s': %s: bus uevent() returned %d\n", 2421e0b2cf9SKay Sievers dev_name(dev), __func__, retval); 2431da177e4SLinus Torvalds } 2441da177e4SLinus Torvalds 2452620efefSGreg Kroah-Hartman /* have the class specific function add its stuff */ 2467eff2e7aSKay Sievers if (dev->class && dev->class->dev_uevent) { 2477eff2e7aSKay Sievers retval = dev->class->dev_uevent(dev, env); 248f9f852dfSKay Sievers if (retval) 2497dc72b28SGreg Kroah-Hartman pr_debug("device: '%s': %s: class uevent() " 2501e0b2cf9SKay Sievers "returned %d\n", dev_name(dev), 2512b3a302aSHarvey Harrison __func__, retval); 2522620efefSGreg Kroah-Hartman } 253f9f852dfSKay Sievers 254f9f852dfSKay Sievers /* have the device type specific fuction add its stuff */ 2557eff2e7aSKay Sievers if (dev->type && dev->type->uevent) { 2567eff2e7aSKay Sievers retval = dev->type->uevent(dev, env); 257f9f852dfSKay Sievers if (retval) 2587dc72b28SGreg Kroah-Hartman pr_debug("device: '%s': %s: dev_type uevent() " 2591e0b2cf9SKay Sievers "returned %d\n", dev_name(dev), 2602b3a302aSHarvey Harrison __func__, retval); 2612620efefSGreg Kroah-Hartman } 2622620efefSGreg Kroah-Hartman 2631da177e4SLinus Torvalds return retval; 2641da177e4SLinus Torvalds } 2651da177e4SLinus Torvalds 2669cd43611SEmese Revfy static const struct kset_uevent_ops device_uevent_ops = { 267312c004dSKay Sievers .filter = dev_uevent_filter, 268312c004dSKay Sievers .name = dev_uevent_name, 269312c004dSKay Sievers .uevent = dev_uevent, 2701da177e4SLinus Torvalds }; 2711da177e4SLinus Torvalds 27216574dccSKay Sievers static ssize_t show_uevent(struct device *dev, struct device_attribute *attr, 27316574dccSKay Sievers char *buf) 27416574dccSKay Sievers { 27516574dccSKay Sievers struct kobject *top_kobj; 27616574dccSKay Sievers struct kset *kset; 2777eff2e7aSKay Sievers struct kobj_uevent_env *env = NULL; 27816574dccSKay Sievers int i; 27916574dccSKay Sievers size_t count = 0; 28016574dccSKay Sievers int retval; 28116574dccSKay Sievers 28216574dccSKay Sievers /* search the kset, the device belongs to */ 28316574dccSKay Sievers top_kobj = &dev->kobj; 2845c5daf65SKay Sievers while (!top_kobj->kset && top_kobj->parent) 28516574dccSKay Sievers top_kobj = top_kobj->parent; 28616574dccSKay Sievers if (!top_kobj->kset) 28716574dccSKay Sievers goto out; 2885c5daf65SKay Sievers 28916574dccSKay Sievers kset = top_kobj->kset; 29016574dccSKay Sievers if (!kset->uevent_ops || !kset->uevent_ops->uevent) 29116574dccSKay Sievers goto out; 29216574dccSKay Sievers 29316574dccSKay Sievers /* respect filter */ 29416574dccSKay Sievers if (kset->uevent_ops && kset->uevent_ops->filter) 29516574dccSKay Sievers if (!kset->uevent_ops->filter(kset, &dev->kobj)) 29616574dccSKay Sievers goto out; 29716574dccSKay Sievers 2987eff2e7aSKay Sievers env = kzalloc(sizeof(struct kobj_uevent_env), GFP_KERNEL); 2997eff2e7aSKay Sievers if (!env) 300c7308c81SGreg Kroah-Hartman return -ENOMEM; 301c7308c81SGreg Kroah-Hartman 30216574dccSKay Sievers /* let the kset specific function add its keys */ 3037eff2e7aSKay Sievers retval = kset->uevent_ops->uevent(kset, &dev->kobj, env); 30416574dccSKay Sievers if (retval) 30516574dccSKay Sievers goto out; 30616574dccSKay Sievers 30716574dccSKay Sievers /* copy keys to file */ 3087eff2e7aSKay Sievers for (i = 0; i < env->envp_idx; i++) 3097eff2e7aSKay Sievers count += sprintf(&buf[count], "%s\n", env->envp[i]); 31016574dccSKay Sievers out: 3117eff2e7aSKay Sievers kfree(env); 31216574dccSKay Sievers return count; 31316574dccSKay Sievers } 31416574dccSKay Sievers 315a7fd6706SKay Sievers static ssize_t store_uevent(struct device *dev, struct device_attribute *attr, 316a7fd6706SKay Sievers const char *buf, size_t count) 317a7fd6706SKay Sievers { 31860a96a59SKay Sievers enum kobject_action action; 31960a96a59SKay Sievers 3203f5468c9SKay Sievers if (kobject_action_type(buf, count, &action) == 0) 32160a96a59SKay Sievers kobject_uevent(&dev->kobj, action); 3223f5468c9SKay Sievers else 3233f5468c9SKay Sievers dev_err(dev, "uevent: unknown action-string\n"); 324a7fd6706SKay Sievers return count; 325a7fd6706SKay Sievers } 326a7fd6706SKay Sievers 327ad6a1e1cSTejun Heo static struct device_attribute uevent_attr = 328ad6a1e1cSTejun Heo __ATTR(uevent, S_IRUGO | S_IWUSR, show_uevent, store_uevent); 329ad6a1e1cSTejun Heo 330621a1672SDmitry Torokhov static int device_add_attributes(struct device *dev, 331621a1672SDmitry Torokhov struct device_attribute *attrs) 332de0ff00dSGreg Kroah-Hartman { 333de0ff00dSGreg Kroah-Hartman int error = 0; 334621a1672SDmitry Torokhov int i; 335de0ff00dSGreg Kroah-Hartman 336621a1672SDmitry Torokhov if (attrs) { 337621a1672SDmitry Torokhov for (i = 0; attr_name(attrs[i]); i++) { 338621a1672SDmitry Torokhov error = device_create_file(dev, &attrs[i]); 339621a1672SDmitry Torokhov if (error) 340621a1672SDmitry Torokhov break; 341621a1672SDmitry Torokhov } 342621a1672SDmitry Torokhov if (error) 343de0ff00dSGreg Kroah-Hartman while (--i >= 0) 344621a1672SDmitry Torokhov device_remove_file(dev, &attrs[i]); 345de0ff00dSGreg Kroah-Hartman } 346de0ff00dSGreg Kroah-Hartman return error; 347de0ff00dSGreg Kroah-Hartman } 348de0ff00dSGreg Kroah-Hartman 349621a1672SDmitry Torokhov static void device_remove_attributes(struct device *dev, 350621a1672SDmitry Torokhov struct device_attribute *attrs) 351de0ff00dSGreg Kroah-Hartman { 352de0ff00dSGreg Kroah-Hartman int i; 353621a1672SDmitry Torokhov 354621a1672SDmitry Torokhov if (attrs) 355621a1672SDmitry Torokhov for (i = 0; attr_name(attrs[i]); i++) 356621a1672SDmitry Torokhov device_remove_file(dev, &attrs[i]); 357621a1672SDmitry Torokhov } 358621a1672SDmitry Torokhov 359621a1672SDmitry Torokhov static int device_add_groups(struct device *dev, 360a4dbd674SDavid Brownell const struct attribute_group **groups) 361621a1672SDmitry Torokhov { 362621a1672SDmitry Torokhov int error = 0; 363621a1672SDmitry Torokhov int i; 364621a1672SDmitry Torokhov 365621a1672SDmitry Torokhov if (groups) { 366621a1672SDmitry Torokhov for (i = 0; groups[i]; i++) { 367621a1672SDmitry Torokhov error = sysfs_create_group(&dev->kobj, groups[i]); 368621a1672SDmitry Torokhov if (error) { 369621a1672SDmitry Torokhov while (--i >= 0) 3704a3ad20cSGreg Kroah-Hartman sysfs_remove_group(&dev->kobj, 3714a3ad20cSGreg Kroah-Hartman groups[i]); 372621a1672SDmitry Torokhov break; 373de0ff00dSGreg Kroah-Hartman } 374de0ff00dSGreg Kroah-Hartman } 375de0ff00dSGreg Kroah-Hartman } 376621a1672SDmitry Torokhov return error; 377621a1672SDmitry Torokhov } 378621a1672SDmitry Torokhov 379621a1672SDmitry Torokhov static void device_remove_groups(struct device *dev, 380a4dbd674SDavid Brownell const struct attribute_group **groups) 381621a1672SDmitry Torokhov { 382621a1672SDmitry Torokhov int i; 383621a1672SDmitry Torokhov 384621a1672SDmitry Torokhov if (groups) 385621a1672SDmitry Torokhov for (i = 0; groups[i]; i++) 386621a1672SDmitry Torokhov sysfs_remove_group(&dev->kobj, groups[i]); 387621a1672SDmitry Torokhov } 388de0ff00dSGreg Kroah-Hartman 3892620efefSGreg Kroah-Hartman static int device_add_attrs(struct device *dev) 3902620efefSGreg Kroah-Hartman { 3912620efefSGreg Kroah-Hartman struct class *class = dev->class; 392f9f852dfSKay Sievers struct device_type *type = dev->type; 393621a1672SDmitry Torokhov int error; 3942620efefSGreg Kroah-Hartman 395621a1672SDmitry Torokhov if (class) { 396621a1672SDmitry Torokhov error = device_add_attributes(dev, class->dev_attrs); 3972620efefSGreg Kroah-Hartman if (error) 398621a1672SDmitry Torokhov return error; 399f9f852dfSKay Sievers } 400f9f852dfSKay Sievers 401621a1672SDmitry Torokhov if (type) { 402621a1672SDmitry Torokhov error = device_add_groups(dev, type->groups); 403f9f852dfSKay Sievers if (error) 404621a1672SDmitry Torokhov goto err_remove_class_attrs; 405f9f852dfSKay Sievers } 406621a1672SDmitry Torokhov 407621a1672SDmitry Torokhov error = device_add_groups(dev, dev->groups); 408f9f852dfSKay Sievers if (error) 409621a1672SDmitry Torokhov goto err_remove_type_groups; 410621a1672SDmitry Torokhov 411621a1672SDmitry Torokhov return 0; 412621a1672SDmitry Torokhov 413621a1672SDmitry Torokhov err_remove_type_groups: 414621a1672SDmitry Torokhov if (type) 415621a1672SDmitry Torokhov device_remove_groups(dev, type->groups); 416621a1672SDmitry Torokhov err_remove_class_attrs: 417621a1672SDmitry Torokhov if (class) 418621a1672SDmitry Torokhov device_remove_attributes(dev, class->dev_attrs); 419f9f852dfSKay Sievers 4202620efefSGreg Kroah-Hartman return error; 4212620efefSGreg Kroah-Hartman } 4222620efefSGreg Kroah-Hartman 4232620efefSGreg Kroah-Hartman static void device_remove_attrs(struct device *dev) 4242620efefSGreg Kroah-Hartman { 4252620efefSGreg Kroah-Hartman struct class *class = dev->class; 426f9f852dfSKay Sievers struct device_type *type = dev->type; 4272620efefSGreg Kroah-Hartman 428621a1672SDmitry Torokhov device_remove_groups(dev, dev->groups); 429f9f852dfSKay Sievers 430621a1672SDmitry Torokhov if (type) 431621a1672SDmitry Torokhov device_remove_groups(dev, type->groups); 432621a1672SDmitry Torokhov 433621a1672SDmitry Torokhov if (class) 434621a1672SDmitry Torokhov device_remove_attributes(dev, class->dev_attrs); 4352620efefSGreg Kroah-Hartman } 4362620efefSGreg Kroah-Hartman 4372620efefSGreg Kroah-Hartman 43823681e47SGreg Kroah-Hartman static ssize_t show_dev(struct device *dev, struct device_attribute *attr, 43923681e47SGreg Kroah-Hartman char *buf) 44023681e47SGreg Kroah-Hartman { 44123681e47SGreg Kroah-Hartman return print_dev_t(buf, dev->devt); 44223681e47SGreg Kroah-Hartman } 44323681e47SGreg Kroah-Hartman 444ad6a1e1cSTejun Heo static struct device_attribute devt_attr = 445ad6a1e1cSTejun Heo __ATTR(dev, S_IRUGO, show_dev, NULL); 446ad6a1e1cSTejun Heo 447881c6cfdSGreg Kroah-Hartman /* kset to create /sys/devices/ */ 448881c6cfdSGreg Kroah-Hartman struct kset *devices_kset; 4491da177e4SLinus Torvalds 4501da177e4SLinus Torvalds /** 4511da177e4SLinus Torvalds * device_create_file - create sysfs attribute file for device. 4521da177e4SLinus Torvalds * @dev: device. 4531da177e4SLinus Torvalds * @attr: device attribute descriptor. 4541da177e4SLinus Torvalds */ 45526579ab7SPhil Carmody int device_create_file(struct device *dev, 45626579ab7SPhil Carmody const struct device_attribute *attr) 4571da177e4SLinus Torvalds { 4581da177e4SLinus Torvalds int error = 0; 4590c98b19fSCornelia Huck if (dev) 4601da177e4SLinus Torvalds error = sysfs_create_file(&dev->kobj, &attr->attr); 4611da177e4SLinus Torvalds return error; 4621da177e4SLinus Torvalds } 4631da177e4SLinus Torvalds 4641da177e4SLinus Torvalds /** 4651da177e4SLinus Torvalds * device_remove_file - remove sysfs attribute file. 4661da177e4SLinus Torvalds * @dev: device. 4671da177e4SLinus Torvalds * @attr: device attribute descriptor. 4681da177e4SLinus Torvalds */ 46926579ab7SPhil Carmody void device_remove_file(struct device *dev, 47026579ab7SPhil Carmody const struct device_attribute *attr) 4711da177e4SLinus Torvalds { 4720c98b19fSCornelia Huck if (dev) 4731da177e4SLinus Torvalds sysfs_remove_file(&dev->kobj, &attr->attr); 4741da177e4SLinus Torvalds } 4751da177e4SLinus Torvalds 4762589f188SGreg Kroah-Hartman /** 4772589f188SGreg Kroah-Hartman * device_create_bin_file - create sysfs binary attribute file for device. 4782589f188SGreg Kroah-Hartman * @dev: device. 4792589f188SGreg Kroah-Hartman * @attr: device binary attribute descriptor. 4802589f188SGreg Kroah-Hartman */ 48166ecb92bSPhil Carmody int device_create_bin_file(struct device *dev, 48266ecb92bSPhil Carmody const struct bin_attribute *attr) 4832589f188SGreg Kroah-Hartman { 4842589f188SGreg Kroah-Hartman int error = -EINVAL; 4852589f188SGreg Kroah-Hartman if (dev) 4862589f188SGreg Kroah-Hartman error = sysfs_create_bin_file(&dev->kobj, attr); 4872589f188SGreg Kroah-Hartman return error; 4882589f188SGreg Kroah-Hartman } 4892589f188SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(device_create_bin_file); 4902589f188SGreg Kroah-Hartman 4912589f188SGreg Kroah-Hartman /** 4922589f188SGreg Kroah-Hartman * device_remove_bin_file - remove sysfs binary attribute file 4932589f188SGreg Kroah-Hartman * @dev: device. 4942589f188SGreg Kroah-Hartman * @attr: device binary attribute descriptor. 4952589f188SGreg Kroah-Hartman */ 49666ecb92bSPhil Carmody void device_remove_bin_file(struct device *dev, 49766ecb92bSPhil Carmody const struct bin_attribute *attr) 4982589f188SGreg Kroah-Hartman { 4992589f188SGreg Kroah-Hartman if (dev) 5002589f188SGreg Kroah-Hartman sysfs_remove_bin_file(&dev->kobj, attr); 5012589f188SGreg Kroah-Hartman } 5022589f188SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(device_remove_bin_file); 5032589f188SGreg Kroah-Hartman 504d9a9cdfbSAlan Stern /** 505523ded71SAlan Stern * device_schedule_callback_owner - helper to schedule a callback for a device 506d9a9cdfbSAlan Stern * @dev: device. 507d9a9cdfbSAlan Stern * @func: callback function to invoke later. 508523ded71SAlan Stern * @owner: module owning the callback routine 509d9a9cdfbSAlan Stern * 510d9a9cdfbSAlan Stern * Attribute methods must not unregister themselves or their parent device 511d9a9cdfbSAlan Stern * (which would amount to the same thing). Attempts to do so will deadlock, 512d9a9cdfbSAlan Stern * since unregistration is mutually exclusive with driver callbacks. 513d9a9cdfbSAlan Stern * 514d9a9cdfbSAlan Stern * Instead methods can call this routine, which will attempt to allocate 515d9a9cdfbSAlan Stern * and schedule a workqueue request to call back @func with @dev as its 516d9a9cdfbSAlan Stern * argument in the workqueue's process context. @dev will be pinned until 517d9a9cdfbSAlan Stern * @func returns. 518d9a9cdfbSAlan Stern * 519523ded71SAlan Stern * This routine is usually called via the inline device_schedule_callback(), 520523ded71SAlan Stern * which automatically sets @owner to THIS_MODULE. 521523ded71SAlan Stern * 522d9a9cdfbSAlan Stern * Returns 0 if the request was submitted, -ENOMEM if storage could not 523523ded71SAlan Stern * be allocated, -ENODEV if a reference to @owner isn't available. 524d9a9cdfbSAlan Stern * 525d9a9cdfbSAlan Stern * NOTE: This routine won't work if CONFIG_SYSFS isn't set! It uses an 526d9a9cdfbSAlan Stern * underlying sysfs routine (since it is intended for use by attribute 527d9a9cdfbSAlan Stern * methods), and if sysfs isn't available you'll get nothing but -ENOSYS. 528d9a9cdfbSAlan Stern */ 529523ded71SAlan Stern int device_schedule_callback_owner(struct device *dev, 530523ded71SAlan Stern void (*func)(struct device *), struct module *owner) 531d9a9cdfbSAlan Stern { 532d9a9cdfbSAlan Stern return sysfs_schedule_callback(&dev->kobj, 533523ded71SAlan Stern (void (*)(void *)) func, dev, owner); 534d9a9cdfbSAlan Stern } 535523ded71SAlan Stern EXPORT_SYMBOL_GPL(device_schedule_callback_owner); 536d9a9cdfbSAlan Stern 53734bb61f9SJames Bottomley static void klist_children_get(struct klist_node *n) 53834bb61f9SJames Bottomley { 539f791b8c8SGreg Kroah-Hartman struct device_private *p = to_device_private_parent(n); 540f791b8c8SGreg Kroah-Hartman struct device *dev = p->device; 54134bb61f9SJames Bottomley 54234bb61f9SJames Bottomley get_device(dev); 54334bb61f9SJames Bottomley } 54434bb61f9SJames Bottomley 54534bb61f9SJames Bottomley static void klist_children_put(struct klist_node *n) 54634bb61f9SJames Bottomley { 547f791b8c8SGreg Kroah-Hartman struct device_private *p = to_device_private_parent(n); 548f791b8c8SGreg Kroah-Hartman struct device *dev = p->device; 54934bb61f9SJames Bottomley 55034bb61f9SJames Bottomley put_device(dev); 55134bb61f9SJames Bottomley } 55234bb61f9SJames Bottomley 5531da177e4SLinus Torvalds /** 5541da177e4SLinus Torvalds * device_initialize - init device structure. 5551da177e4SLinus Torvalds * @dev: device. 5561da177e4SLinus Torvalds * 5575739411aSCornelia Huck * This prepares the device for use by other layers by initializing 5585739411aSCornelia Huck * its fields. 5591da177e4SLinus Torvalds * It is the first half of device_register(), if called by 5605739411aSCornelia Huck * that function, though it can also be called separately, so one 5615739411aSCornelia Huck * may use @dev's fields. In particular, get_device()/put_device() 5625739411aSCornelia Huck * may be used for reference counting of @dev after calling this 5635739411aSCornelia Huck * function. 5645739411aSCornelia Huck * 5655739411aSCornelia Huck * NOTE: Use put_device() to give up your reference instead of freeing 5665739411aSCornelia Huck * @dev directly once you have called this function. 5671da177e4SLinus Torvalds */ 5681da177e4SLinus Torvalds void device_initialize(struct device *dev) 5691da177e4SLinus Torvalds { 570881c6cfdSGreg Kroah-Hartman dev->kobj.kset = devices_kset; 571f9cb074bSGreg Kroah-Hartman kobject_init(&dev->kobj, &device_ktype); 5721da177e4SLinus Torvalds INIT_LIST_HEAD(&dev->dma_pools); 5733142788bSThomas Gleixner mutex_init(&dev->mutex); 5741704f47bSPeter Zijlstra lockdep_set_novalidate_class(&dev->mutex); 5759ac7849eSTejun Heo spin_lock_init(&dev->devres_lock); 5769ac7849eSTejun Heo INIT_LIST_HEAD(&dev->devres_head); 5773b98aeafSAlan Stern device_pm_init(dev); 57887348136SChristoph Hellwig set_dev_node(dev, -1); 5791da177e4SLinus Torvalds } 5801da177e4SLinus Torvalds 58140fa5422SGreg Kroah-Hartman #ifdef CONFIG_SYSFS_DEPRECATED 582c744aeaeSCornelia Huck static struct kobject *get_device_parent(struct device *dev, 583c744aeaeSCornelia Huck struct device *parent) 58440fa5422SGreg Kroah-Hartman { 585da231fd5SKay Sievers /* class devices without a parent live in /sys/class/<classname>/ */ 5863eb215deSDmitry Torokhov if (dev->class && (!parent || parent->class != dev->class)) 5871fbfee6cSGreg Kroah-Hartman return &dev->class->p->class_subsys.kobj; 588da231fd5SKay Sievers /* all other devices keep their parent */ 58940fa5422SGreg Kroah-Hartman else if (parent) 590c744aeaeSCornelia Huck return &parent->kobj; 59140fa5422SGreg Kroah-Hartman 592c744aeaeSCornelia Huck return NULL; 59340fa5422SGreg Kroah-Hartman } 594da231fd5SKay Sievers 595da231fd5SKay Sievers static inline void cleanup_device_parent(struct device *dev) {} 59663b6971aSCornelia Huck static inline void cleanup_glue_dir(struct device *dev, 59763b6971aSCornelia Huck struct kobject *glue_dir) {} 59840fa5422SGreg Kroah-Hartman #else 599c744aeaeSCornelia Huck static struct kobject *virtual_device_parent(struct device *dev) 600f0ee61a6SGreg Kroah-Hartman { 601f0ee61a6SGreg Kroah-Hartman static struct kobject *virtual_dir = NULL; 602f0ee61a6SGreg Kroah-Hartman 603f0ee61a6SGreg Kroah-Hartman if (!virtual_dir) 6044ff6abffSGreg Kroah-Hartman virtual_dir = kobject_create_and_add("virtual", 605881c6cfdSGreg Kroah-Hartman &devices_kset->kobj); 606f0ee61a6SGreg Kroah-Hartman 60786406245SKay Sievers return virtual_dir; 608f0ee61a6SGreg Kroah-Hartman } 609f0ee61a6SGreg Kroah-Hartman 610bc451f20SEric W. Biederman struct class_dir { 611bc451f20SEric W. Biederman struct kobject kobj; 612bc451f20SEric W. Biederman struct class *class; 613bc451f20SEric W. Biederman }; 614bc451f20SEric W. Biederman 615bc451f20SEric W. Biederman #define to_class_dir(obj) container_of(obj, struct class_dir, kobj) 616bc451f20SEric W. Biederman 617bc451f20SEric W. Biederman static void class_dir_release(struct kobject *kobj) 618bc451f20SEric W. Biederman { 619bc451f20SEric W. Biederman struct class_dir *dir = to_class_dir(kobj); 620bc451f20SEric W. Biederman kfree(dir); 621bc451f20SEric W. Biederman } 622bc451f20SEric W. Biederman 623bc451f20SEric W. Biederman static const 624bc451f20SEric W. Biederman struct kobj_ns_type_operations *class_dir_child_ns_type(struct kobject *kobj) 625bc451f20SEric W. Biederman { 626bc451f20SEric W. Biederman struct class_dir *dir = to_class_dir(kobj); 627bc451f20SEric W. Biederman return dir->class->ns_type; 628bc451f20SEric W. Biederman } 629bc451f20SEric W. Biederman 630bc451f20SEric W. Biederman static struct kobj_type class_dir_ktype = { 631bc451f20SEric W. Biederman .release = class_dir_release, 632bc451f20SEric W. Biederman .sysfs_ops = &kobj_sysfs_ops, 633bc451f20SEric W. Biederman .child_ns_type = class_dir_child_ns_type 634bc451f20SEric W. Biederman }; 635bc451f20SEric W. Biederman 636bc451f20SEric W. Biederman static struct kobject * 637bc451f20SEric W. Biederman class_dir_create_and_add(struct class *class, struct kobject *parent_kobj) 638bc451f20SEric W. Biederman { 639bc451f20SEric W. Biederman struct class_dir *dir; 640bc451f20SEric W. Biederman int retval; 641bc451f20SEric W. Biederman 642bc451f20SEric W. Biederman dir = kzalloc(sizeof(*dir), GFP_KERNEL); 643bc451f20SEric W. Biederman if (!dir) 644bc451f20SEric W. Biederman return NULL; 645bc451f20SEric W. Biederman 646bc451f20SEric W. Biederman dir->class = class; 647bc451f20SEric W. Biederman kobject_init(&dir->kobj, &class_dir_ktype); 648bc451f20SEric W. Biederman 649bc451f20SEric W. Biederman dir->kobj.kset = &class->p->class_dirs; 650bc451f20SEric W. Biederman 651bc451f20SEric W. Biederman retval = kobject_add(&dir->kobj, parent_kobj, "%s", class->name); 652bc451f20SEric W. Biederman if (retval < 0) { 653bc451f20SEric W. Biederman kobject_put(&dir->kobj); 654bc451f20SEric W. Biederman return NULL; 655bc451f20SEric W. Biederman } 656bc451f20SEric W. Biederman return &dir->kobj; 657bc451f20SEric W. Biederman } 658bc451f20SEric W. Biederman 659bc451f20SEric W. Biederman 660c744aeaeSCornelia Huck static struct kobject *get_device_parent(struct device *dev, 661c744aeaeSCornelia Huck struct device *parent) 66240fa5422SGreg Kroah-Hartman { 66386406245SKay Sievers if (dev->class) { 66477d3d7c1STejun Heo static DEFINE_MUTEX(gdp_mutex); 66586406245SKay Sievers struct kobject *kobj = NULL; 66686406245SKay Sievers struct kobject *parent_kobj; 66786406245SKay Sievers struct kobject *k; 66886406245SKay Sievers 66986406245SKay Sievers /* 67086406245SKay Sievers * If we have no parent, we live in "virtual". 6710f4dafc0SKay Sievers * Class-devices with a non class-device as parent, live 6720f4dafc0SKay Sievers * in a "glue" directory to prevent namespace collisions. 67386406245SKay Sievers */ 67486406245SKay Sievers if (parent == NULL) 67586406245SKay Sievers parent_kobj = virtual_device_parent(dev); 67624b1442dSEric W. Biederman else if (parent->class && !dev->class->ns_type) 67786406245SKay Sievers return &parent->kobj; 67886406245SKay Sievers else 67986406245SKay Sievers parent_kobj = &parent->kobj; 68086406245SKay Sievers 68177d3d7c1STejun Heo mutex_lock(&gdp_mutex); 68277d3d7c1STejun Heo 68386406245SKay Sievers /* find our class-directory at the parent and reference it */ 6847c71448bSGreg Kroah-Hartman spin_lock(&dev->class->p->class_dirs.list_lock); 6857c71448bSGreg Kroah-Hartman list_for_each_entry(k, &dev->class->p->class_dirs.list, entry) 68686406245SKay Sievers if (k->parent == parent_kobj) { 68786406245SKay Sievers kobj = kobject_get(k); 68886406245SKay Sievers break; 68986406245SKay Sievers } 6907c71448bSGreg Kroah-Hartman spin_unlock(&dev->class->p->class_dirs.list_lock); 69177d3d7c1STejun Heo if (kobj) { 69277d3d7c1STejun Heo mutex_unlock(&gdp_mutex); 69386406245SKay Sievers return kobj; 69477d3d7c1STejun Heo } 69586406245SKay Sievers 69686406245SKay Sievers /* or create a new class-directory at the parent device */ 697bc451f20SEric W. Biederman k = class_dir_create_and_add(dev->class, parent_kobj); 6980f4dafc0SKay Sievers /* do not emit an uevent for this simple "glue" directory */ 69977d3d7c1STejun Heo mutex_unlock(&gdp_mutex); 70043968d2fSGreg Kroah-Hartman return k; 70186406245SKay Sievers } 70286406245SKay Sievers 70386406245SKay Sievers if (parent) 704c744aeaeSCornelia Huck return &parent->kobj; 705c744aeaeSCornelia Huck return NULL; 706c744aeaeSCornelia Huck } 707da231fd5SKay Sievers 70863b6971aSCornelia Huck static void cleanup_glue_dir(struct device *dev, struct kobject *glue_dir) 709da231fd5SKay Sievers { 7100f4dafc0SKay Sievers /* see if we live in a "glue" directory */ 711c1fe539aSCornelia Huck if (!glue_dir || !dev->class || 7127c71448bSGreg Kroah-Hartman glue_dir->kset != &dev->class->p->class_dirs) 713da231fd5SKay Sievers return; 714da231fd5SKay Sievers 7150f4dafc0SKay Sievers kobject_put(glue_dir); 716da231fd5SKay Sievers } 71763b6971aSCornelia Huck 71863b6971aSCornelia Huck static void cleanup_device_parent(struct device *dev) 71963b6971aSCornelia Huck { 72063b6971aSCornelia Huck cleanup_glue_dir(dev, dev->kobj.parent); 72163b6971aSCornelia Huck } 722c744aeaeSCornelia Huck #endif 72386406245SKay Sievers 72463b6971aSCornelia Huck static void setup_parent(struct device *dev, struct device *parent) 725c744aeaeSCornelia Huck { 726c744aeaeSCornelia Huck struct kobject *kobj; 727c744aeaeSCornelia Huck kobj = get_device_parent(dev, parent); 728c744aeaeSCornelia Huck if (kobj) 729c744aeaeSCornelia Huck dev->kobj.parent = kobj; 73040fa5422SGreg Kroah-Hartman } 73140fa5422SGreg Kroah-Hartman 7322ee97cafSCornelia Huck static int device_add_class_symlinks(struct device *dev) 7332ee97cafSCornelia Huck { 7342ee97cafSCornelia Huck int error; 7352ee97cafSCornelia Huck 7362ee97cafSCornelia Huck if (!dev->class) 7372ee97cafSCornelia Huck return 0; 738da231fd5SKay Sievers 7391fbfee6cSGreg Kroah-Hartman error = sysfs_create_link(&dev->kobj, 7401fbfee6cSGreg Kroah-Hartman &dev->class->p->class_subsys.kobj, 7412ee97cafSCornelia Huck "subsystem"); 7422ee97cafSCornelia Huck if (error) 7432ee97cafSCornelia Huck goto out; 744da231fd5SKay Sievers 745da231fd5SKay Sievers #ifdef CONFIG_SYSFS_DEPRECATED 746da231fd5SKay Sievers /* stacked class devices need a symlink in the class directory */ 7471fbfee6cSGreg Kroah-Hartman if (dev->kobj.parent != &dev->class->p->class_subsys.kobj && 7484e886c29SGreg Kroah-Hartman device_is_not_partition(dev)) { 7491fbfee6cSGreg Kroah-Hartman error = sysfs_create_link(&dev->class->p->class_subsys.kobj, 7501e0b2cf9SKay Sievers &dev->kobj, dev_name(dev)); 7512ee97cafSCornelia Huck if (error) 7522ee97cafSCornelia Huck goto out_subsys; 7532ee97cafSCornelia Huck } 754da231fd5SKay Sievers 7554e886c29SGreg Kroah-Hartman if (dev->parent && device_is_not_partition(dev)) { 7564f01a757SDmitry Torokhov struct device *parent = dev->parent; 7574f01a757SDmitry Torokhov char *class_name; 7584f01a757SDmitry Torokhov 7594f01a757SDmitry Torokhov /* 760da231fd5SKay Sievers * stacked class devices have the 'device' link 761da231fd5SKay Sievers * pointing to the bus device instead of the parent 7624f01a757SDmitry Torokhov */ 7634f01a757SDmitry Torokhov while (parent->class && !parent->bus && parent->parent) 7644f01a757SDmitry Torokhov parent = parent->parent; 7654f01a757SDmitry Torokhov 7664f01a757SDmitry Torokhov error = sysfs_create_link(&dev->kobj, 7674f01a757SDmitry Torokhov &parent->kobj, 7682ee97cafSCornelia Huck "device"); 7692ee97cafSCornelia Huck if (error) 7702ee97cafSCornelia Huck goto out_busid; 7714f01a757SDmitry Torokhov 7724f01a757SDmitry Torokhov class_name = make_class_name(dev->class->name, 7732ee97cafSCornelia Huck &dev->kobj); 7742ee97cafSCornelia Huck if (class_name) 7752ee97cafSCornelia Huck error = sysfs_create_link(&dev->parent->kobj, 7762ee97cafSCornelia Huck &dev->kobj, class_name); 7772ee97cafSCornelia Huck kfree(class_name); 7782ee97cafSCornelia Huck if (error) 7792ee97cafSCornelia Huck goto out_device; 7802ee97cafSCornelia Huck } 781da231fd5SKay Sievers return 0; 782da231fd5SKay Sievers 783da231fd5SKay Sievers out_device: 7844e886c29SGreg Kroah-Hartman if (dev->parent && device_is_not_partition(dev)) 785da231fd5SKay Sievers sysfs_remove_link(&dev->kobj, "device"); 786da231fd5SKay Sievers out_busid: 7871fbfee6cSGreg Kroah-Hartman if (dev->kobj.parent != &dev->class->p->class_subsys.kobj && 7884e886c29SGreg Kroah-Hartman device_is_not_partition(dev)) 789f349cf34SEric W. Biederman sysfs_delete_link(&dev->class->p->class_subsys.kobj, &dev->kobj, 7901e0b2cf9SKay Sievers dev_name(dev)); 7914f01a757SDmitry Torokhov #else 792da231fd5SKay Sievers /* link in the class directory pointing to the device */ 7931fbfee6cSGreg Kroah-Hartman error = sysfs_create_link(&dev->class->p->class_subsys.kobj, 7941e0b2cf9SKay Sievers &dev->kobj, dev_name(dev)); 795da231fd5SKay Sievers if (error) 796da231fd5SKay Sievers goto out_subsys; 797da231fd5SKay Sievers 7984e886c29SGreg Kroah-Hartman if (dev->parent && device_is_not_partition(dev)) { 7994f01a757SDmitry Torokhov error = sysfs_create_link(&dev->kobj, &dev->parent->kobj, 8004f01a757SDmitry Torokhov "device"); 8014f01a757SDmitry Torokhov if (error) 8024f01a757SDmitry Torokhov goto out_busid; 8032ee97cafSCornelia Huck } 8042ee97cafSCornelia Huck return 0; 8052ee97cafSCornelia Huck 8062ee97cafSCornelia Huck out_busid: 807f349cf34SEric W. Biederman sysfs_delete_link(&dev->class->p->class_subsys.kobj, &dev->kobj, dev_name(dev)); 808da231fd5SKay Sievers #endif 809da231fd5SKay Sievers 8102ee97cafSCornelia Huck out_subsys: 8112ee97cafSCornelia Huck sysfs_remove_link(&dev->kobj, "subsystem"); 8122ee97cafSCornelia Huck out: 8132ee97cafSCornelia Huck return error; 8142ee97cafSCornelia Huck } 8152ee97cafSCornelia Huck 8162ee97cafSCornelia Huck static void device_remove_class_symlinks(struct device *dev) 8172ee97cafSCornelia Huck { 8182ee97cafSCornelia Huck if (!dev->class) 8192ee97cafSCornelia Huck return; 820da231fd5SKay Sievers 8212ee97cafSCornelia Huck #ifdef CONFIG_SYSFS_DEPRECATED 8224e886c29SGreg Kroah-Hartman if (dev->parent && device_is_not_partition(dev)) { 8232ee97cafSCornelia Huck char *class_name; 8242ee97cafSCornelia Huck 8252ee97cafSCornelia Huck class_name = make_class_name(dev->class->name, &dev->kobj); 8262ee97cafSCornelia Huck if (class_name) { 8272ee97cafSCornelia Huck sysfs_remove_link(&dev->parent->kobj, class_name); 8282ee97cafSCornelia Huck kfree(class_name); 8292ee97cafSCornelia Huck } 8302ee97cafSCornelia Huck sysfs_remove_link(&dev->kobj, "device"); 8312ee97cafSCornelia Huck } 832da231fd5SKay Sievers 8331fbfee6cSGreg Kroah-Hartman if (dev->kobj.parent != &dev->class->p->class_subsys.kobj && 8344e886c29SGreg Kroah-Hartman device_is_not_partition(dev)) 835f349cf34SEric W. Biederman sysfs_delete_link(&dev->class->p->class_subsys.kobj, &dev->kobj, 8361e0b2cf9SKay Sievers dev_name(dev)); 837da231fd5SKay Sievers #else 8384e886c29SGreg Kroah-Hartman if (dev->parent && device_is_not_partition(dev)) 839da231fd5SKay Sievers sysfs_remove_link(&dev->kobj, "device"); 840da231fd5SKay Sievers 841f349cf34SEric W. Biederman sysfs_delete_link(&dev->class->p->class_subsys.kobj, &dev->kobj, dev_name(dev)); 842da231fd5SKay Sievers #endif 843da231fd5SKay Sievers 8442ee97cafSCornelia Huck sysfs_remove_link(&dev->kobj, "subsystem"); 8452ee97cafSCornelia Huck } 8462ee97cafSCornelia Huck 8471da177e4SLinus Torvalds /** 848413c239fSStephen Rothwell * dev_set_name - set a device name 849413c239fSStephen Rothwell * @dev: device 85046232366SRandy Dunlap * @fmt: format string for the device's name 851413c239fSStephen Rothwell */ 852413c239fSStephen Rothwell int dev_set_name(struct device *dev, const char *fmt, ...) 853413c239fSStephen Rothwell { 854413c239fSStephen Rothwell va_list vargs; 8551fa5ae85SKay Sievers int err; 856413c239fSStephen Rothwell 857413c239fSStephen Rothwell va_start(vargs, fmt); 8581fa5ae85SKay Sievers err = kobject_set_name_vargs(&dev->kobj, fmt, vargs); 859413c239fSStephen Rothwell va_end(vargs); 8601fa5ae85SKay Sievers return err; 861413c239fSStephen Rothwell } 862413c239fSStephen Rothwell EXPORT_SYMBOL_GPL(dev_set_name); 863413c239fSStephen Rothwell 864413c239fSStephen Rothwell /** 865e105b8bfSDan Williams * device_to_dev_kobj - select a /sys/dev/ directory for the device 866e105b8bfSDan Williams * @dev: device 867e105b8bfSDan Williams * 868e105b8bfSDan Williams * By default we select char/ for new entries. Setting class->dev_obj 869e105b8bfSDan Williams * to NULL prevents an entry from being created. class->dev_kobj must 870e105b8bfSDan Williams * be set (or cleared) before any devices are registered to the class 871e105b8bfSDan Williams * otherwise device_create_sys_dev_entry() and 872e105b8bfSDan Williams * device_remove_sys_dev_entry() will disagree about the the presence 873e105b8bfSDan Williams * of the link. 874e105b8bfSDan Williams */ 875e105b8bfSDan Williams static struct kobject *device_to_dev_kobj(struct device *dev) 876e105b8bfSDan Williams { 877e105b8bfSDan Williams struct kobject *kobj; 878e105b8bfSDan Williams 879e105b8bfSDan Williams if (dev->class) 880e105b8bfSDan Williams kobj = dev->class->dev_kobj; 881e105b8bfSDan Williams else 882e105b8bfSDan Williams kobj = sysfs_dev_char_kobj; 883e105b8bfSDan Williams 884e105b8bfSDan Williams return kobj; 885e105b8bfSDan Williams } 886e105b8bfSDan Williams 887e105b8bfSDan Williams static int device_create_sys_dev_entry(struct device *dev) 888e105b8bfSDan Williams { 889e105b8bfSDan Williams struct kobject *kobj = device_to_dev_kobj(dev); 890e105b8bfSDan Williams int error = 0; 891e105b8bfSDan Williams char devt_str[15]; 892e105b8bfSDan Williams 893e105b8bfSDan Williams if (kobj) { 894e105b8bfSDan Williams format_dev_t(devt_str, dev->devt); 895e105b8bfSDan Williams error = sysfs_create_link(kobj, &dev->kobj, devt_str); 896e105b8bfSDan Williams } 897e105b8bfSDan Williams 898e105b8bfSDan Williams return error; 899e105b8bfSDan Williams } 900e105b8bfSDan Williams 901e105b8bfSDan Williams static void device_remove_sys_dev_entry(struct device *dev) 902e105b8bfSDan Williams { 903e105b8bfSDan Williams struct kobject *kobj = device_to_dev_kobj(dev); 904e105b8bfSDan Williams char devt_str[15]; 905e105b8bfSDan Williams 906e105b8bfSDan Williams if (kobj) { 907e105b8bfSDan Williams format_dev_t(devt_str, dev->devt); 908e105b8bfSDan Williams sysfs_remove_link(kobj, devt_str); 909e105b8bfSDan Williams } 910e105b8bfSDan Williams } 911e105b8bfSDan Williams 912b4028437SGreg Kroah-Hartman int device_private_init(struct device *dev) 913b4028437SGreg Kroah-Hartman { 914b4028437SGreg Kroah-Hartman dev->p = kzalloc(sizeof(*dev->p), GFP_KERNEL); 915b4028437SGreg Kroah-Hartman if (!dev->p) 916b4028437SGreg Kroah-Hartman return -ENOMEM; 917b4028437SGreg Kroah-Hartman dev->p->device = dev; 918b4028437SGreg Kroah-Hartman klist_init(&dev->p->klist_children, klist_children_get, 919b4028437SGreg Kroah-Hartman klist_children_put); 920b4028437SGreg Kroah-Hartman return 0; 921b4028437SGreg Kroah-Hartman } 922b4028437SGreg Kroah-Hartman 923e105b8bfSDan Williams /** 9241da177e4SLinus Torvalds * device_add - add device to device hierarchy. 9251da177e4SLinus Torvalds * @dev: device. 9261da177e4SLinus Torvalds * 9271da177e4SLinus Torvalds * This is part 2 of device_register(), though may be called 9281da177e4SLinus Torvalds * separately _iff_ device_initialize() has been called separately. 9291da177e4SLinus Torvalds * 9305739411aSCornelia Huck * This adds @dev to the kobject hierarchy via kobject_add(), adds it 9311da177e4SLinus Torvalds * to the global and sibling lists for the device, then 9321da177e4SLinus Torvalds * adds it to the other relevant subsystems of the driver model. 9335739411aSCornelia Huck * 9345739411aSCornelia Huck * NOTE: _Never_ directly free @dev after calling this function, even 9355739411aSCornelia Huck * if it returned an error! Always use put_device() to give up your 9365739411aSCornelia Huck * reference instead. 9371da177e4SLinus Torvalds */ 9381da177e4SLinus Torvalds int device_add(struct device *dev) 9391da177e4SLinus Torvalds { 9401da177e4SLinus Torvalds struct device *parent = NULL; 941c47ed219SGreg Kroah-Hartman struct class_interface *class_intf; 942c906a48aSGreg Kroah-Hartman int error = -EINVAL; 943775b64d2SRafael J. Wysocki 9441da177e4SLinus Torvalds dev = get_device(dev); 945c906a48aSGreg Kroah-Hartman if (!dev) 946c906a48aSGreg Kroah-Hartman goto done; 947c906a48aSGreg Kroah-Hartman 948fb069a5dSGreg Kroah-Hartman if (!dev->p) { 949b4028437SGreg Kroah-Hartman error = device_private_init(dev); 950b4028437SGreg Kroah-Hartman if (error) 951fb069a5dSGreg Kroah-Hartman goto done; 952fb069a5dSGreg Kroah-Hartman } 953fb069a5dSGreg Kroah-Hartman 9541fa5ae85SKay Sievers /* 9551fa5ae85SKay Sievers * for statically allocated devices, which should all be converted 9561fa5ae85SKay Sievers * some day, we need to initialize the name. We prevent reading back 9571fa5ae85SKay Sievers * the name, and force the use of dev_name() 9581fa5ae85SKay Sievers */ 9591fa5ae85SKay Sievers if (dev->init_name) { 960acc0e90fSGreg Kroah-Hartman dev_set_name(dev, "%s", dev->init_name); 9611fa5ae85SKay Sievers dev->init_name = NULL; 9621fa5ae85SKay Sievers } 963c906a48aSGreg Kroah-Hartman 964e6309e75SThomas Gleixner if (!dev_name(dev)) { 965e6309e75SThomas Gleixner error = -EINVAL; 9665c8563d7SKay Sievers goto name_error; 967e6309e75SThomas Gleixner } 9681da177e4SLinus Torvalds 9691e0b2cf9SKay Sievers pr_debug("device: '%s': %s\n", dev_name(dev), __func__); 970c205ef48SGreg Kroah-Hartman 9711da177e4SLinus Torvalds parent = get_device(dev->parent); 97263b6971aSCornelia Huck setup_parent(dev, parent); 9731da177e4SLinus Torvalds 9740d358f22SYinghai Lu /* use parent numa_node */ 9750d358f22SYinghai Lu if (parent) 9760d358f22SYinghai Lu set_dev_node(dev, dev_to_node(parent)); 9770d358f22SYinghai Lu 9781da177e4SLinus Torvalds /* first, register with generic layer. */ 9798a577ffcSKay Sievers /* we require the name to be set before, and pass NULL */ 9808a577ffcSKay Sievers error = kobject_add(&dev->kobj, dev->kobj.parent, NULL); 98140fa5422SGreg Kroah-Hartman if (error) 9821da177e4SLinus Torvalds goto Error; 983a7fd6706SKay Sievers 98437022644SBrian Walsh /* notify platform of device entry */ 98537022644SBrian Walsh if (platform_notify) 98637022644SBrian Walsh platform_notify(dev); 98737022644SBrian Walsh 988ad6a1e1cSTejun Heo error = device_create_file(dev, &uevent_attr); 989a306eea4SCornelia Huck if (error) 990a306eea4SCornelia Huck goto attrError; 991a7fd6706SKay Sievers 99223681e47SGreg Kroah-Hartman if (MAJOR(dev->devt)) { 993ad6a1e1cSTejun Heo error = device_create_file(dev, &devt_attr); 994ad6a1e1cSTejun Heo if (error) 995a306eea4SCornelia Huck goto ueventattrError; 996e105b8bfSDan Williams 997e105b8bfSDan Williams error = device_create_sys_dev_entry(dev); 998e105b8bfSDan Williams if (error) 999e105b8bfSDan Williams goto devtattrError; 10002b2af54aSKay Sievers 10012b2af54aSKay Sievers devtmpfs_create_node(dev); 100223681e47SGreg Kroah-Hartman } 100323681e47SGreg Kroah-Hartman 10042ee97cafSCornelia Huck error = device_add_class_symlinks(dev); 10052ee97cafSCornelia Huck if (error) 10062ee97cafSCornelia Huck goto SymlinkError; 1007dc0afa83SCornelia Huck error = device_add_attrs(dev); 1008dc0afa83SCornelia Huck if (error) 10092620efefSGreg Kroah-Hartman goto AttrsError; 1010dc0afa83SCornelia Huck error = bus_add_device(dev); 1011dc0afa83SCornelia Huck if (error) 10121da177e4SLinus Torvalds goto BusError; 10133b98aeafSAlan Stern error = dpm_sysfs_add(dev); 101457eee3d2SRafael J. Wysocki if (error) 10153b98aeafSAlan Stern goto DPMError; 10163b98aeafSAlan Stern device_pm_add(dev); 1017ec0676eeSAlan Stern 1018ec0676eeSAlan Stern /* Notify clients of device addition. This call must come 1019ec0676eeSAlan Stern * after dpm_sysf_add() and before kobject_uevent(). 1020ec0676eeSAlan Stern */ 1021ec0676eeSAlan Stern if (dev->bus) 1022ec0676eeSAlan Stern blocking_notifier_call_chain(&dev->bus->p->bus_notifier, 1023ec0676eeSAlan Stern BUS_NOTIFY_ADD_DEVICE, dev); 1024ec0676eeSAlan Stern 102553877d06SKay Sievers kobject_uevent(&dev->kobj, KOBJ_ADD); 10262023c610SAlan Stern bus_probe_device(dev); 10271da177e4SLinus Torvalds if (parent) 1028f791b8c8SGreg Kroah-Hartman klist_add_tail(&dev->p->knode_parent, 1029f791b8c8SGreg Kroah-Hartman &parent->p->klist_children); 10301da177e4SLinus Torvalds 10315d9fd169SGreg Kroah-Hartman if (dev->class) { 1032f75b1c60SDave Young mutex_lock(&dev->class->p->class_mutex); 1033c47ed219SGreg Kroah-Hartman /* tie the class to the device */ 10345a3ceb86STejun Heo klist_add_tail(&dev->knode_class, 10355a3ceb86STejun Heo &dev->class->p->class_devices); 1036c47ed219SGreg Kroah-Hartman 1037c47ed219SGreg Kroah-Hartman /* notify any interfaces that the device is here */ 1038184f1f77SGreg Kroah-Hartman list_for_each_entry(class_intf, 1039184f1f77SGreg Kroah-Hartman &dev->class->p->class_interfaces, node) 1040c47ed219SGreg Kroah-Hartman if (class_intf->add_dev) 1041c47ed219SGreg Kroah-Hartman class_intf->add_dev(dev, class_intf); 1042f75b1c60SDave Young mutex_unlock(&dev->class->p->class_mutex); 10435d9fd169SGreg Kroah-Hartman } 1044c906a48aSGreg Kroah-Hartman done: 10451da177e4SLinus Torvalds put_device(dev); 10461da177e4SLinus Torvalds return error; 10473b98aeafSAlan Stern DPMError: 104857eee3d2SRafael J. Wysocki bus_remove_device(dev); 104957eee3d2SRafael J. Wysocki BusError: 10502620efefSGreg Kroah-Hartman device_remove_attrs(dev); 10512620efefSGreg Kroah-Hartman AttrsError: 10522ee97cafSCornelia Huck device_remove_class_symlinks(dev); 10532ee97cafSCornelia Huck SymlinkError: 1054ad6a1e1cSTejun Heo if (MAJOR(dev->devt)) 1055ad72956dSKay Sievers devtmpfs_delete_node(dev); 1056ad72956dSKay Sievers if (MAJOR(dev->devt)) 1057e105b8bfSDan Williams device_remove_sys_dev_entry(dev); 1058e105b8bfSDan Williams devtattrError: 1059e105b8bfSDan Williams if (MAJOR(dev->devt)) 1060ad6a1e1cSTejun Heo device_remove_file(dev, &devt_attr); 1061a306eea4SCornelia Huck ueventattrError: 1062ad6a1e1cSTejun Heo device_remove_file(dev, &uevent_attr); 106323681e47SGreg Kroah-Hartman attrError: 1064312c004dSKay Sievers kobject_uevent(&dev->kobj, KOBJ_REMOVE); 10651da177e4SLinus Torvalds kobject_del(&dev->kobj); 10661da177e4SLinus Torvalds Error: 106763b6971aSCornelia Huck cleanup_device_parent(dev); 10681da177e4SLinus Torvalds if (parent) 10691da177e4SLinus Torvalds put_device(parent); 10705c8563d7SKay Sievers name_error: 10715c8563d7SKay Sievers kfree(dev->p); 10725c8563d7SKay Sievers dev->p = NULL; 1073c906a48aSGreg Kroah-Hartman goto done; 10741da177e4SLinus Torvalds } 10751da177e4SLinus Torvalds 10761da177e4SLinus Torvalds /** 10771da177e4SLinus Torvalds * device_register - register a device with the system. 10781da177e4SLinus Torvalds * @dev: pointer to the device structure 10791da177e4SLinus Torvalds * 10801da177e4SLinus Torvalds * This happens in two clean steps - initialize the device 10811da177e4SLinus Torvalds * and add it to the system. The two steps can be called 10821da177e4SLinus Torvalds * separately, but this is the easiest and most common. 10831da177e4SLinus Torvalds * I.e. you should only call the two helpers separately if 10841da177e4SLinus Torvalds * have a clearly defined need to use and refcount the device 10851da177e4SLinus Torvalds * before it is added to the hierarchy. 10865739411aSCornelia Huck * 10875739411aSCornelia Huck * NOTE: _Never_ directly free @dev after calling this function, even 10885739411aSCornelia Huck * if it returned an error! Always use put_device() to give up the 10895739411aSCornelia Huck * reference initialized in this function instead. 10901da177e4SLinus Torvalds */ 10911da177e4SLinus Torvalds int device_register(struct device *dev) 10921da177e4SLinus Torvalds { 10931da177e4SLinus Torvalds device_initialize(dev); 10941da177e4SLinus Torvalds return device_add(dev); 10951da177e4SLinus Torvalds } 10961da177e4SLinus Torvalds 10971da177e4SLinus Torvalds /** 10981da177e4SLinus Torvalds * get_device - increment reference count for device. 10991da177e4SLinus Torvalds * @dev: device. 11001da177e4SLinus Torvalds * 11011da177e4SLinus Torvalds * This simply forwards the call to kobject_get(), though 11021da177e4SLinus Torvalds * we do take care to provide for the case that we get a NULL 11031da177e4SLinus Torvalds * pointer passed in. 11041da177e4SLinus Torvalds */ 11051da177e4SLinus Torvalds struct device *get_device(struct device *dev) 11061da177e4SLinus Torvalds { 11071da177e4SLinus Torvalds return dev ? to_dev(kobject_get(&dev->kobj)) : NULL; 11081da177e4SLinus Torvalds } 11091da177e4SLinus Torvalds 11101da177e4SLinus Torvalds /** 11111da177e4SLinus Torvalds * put_device - decrement reference count. 11121da177e4SLinus Torvalds * @dev: device in question. 11131da177e4SLinus Torvalds */ 11141da177e4SLinus Torvalds void put_device(struct device *dev) 11151da177e4SLinus Torvalds { 1116edfaa7c3SKay Sievers /* might_sleep(); */ 11171da177e4SLinus Torvalds if (dev) 11181da177e4SLinus Torvalds kobject_put(&dev->kobj); 11191da177e4SLinus Torvalds } 11201da177e4SLinus Torvalds 11211da177e4SLinus Torvalds /** 11221da177e4SLinus Torvalds * device_del - delete device from system. 11231da177e4SLinus Torvalds * @dev: device. 11241da177e4SLinus Torvalds * 11251da177e4SLinus Torvalds * This is the first part of the device unregistration 11261da177e4SLinus Torvalds * sequence. This removes the device from the lists we control 11271da177e4SLinus Torvalds * from here, has it removed from the other driver model 11281da177e4SLinus Torvalds * subsystems it was added to in device_add(), and removes it 11291da177e4SLinus Torvalds * from the kobject hierarchy. 11301da177e4SLinus Torvalds * 11311da177e4SLinus Torvalds * NOTE: this should be called manually _iff_ device_add() was 11321da177e4SLinus Torvalds * also called manually. 11331da177e4SLinus Torvalds */ 11341da177e4SLinus Torvalds void device_del(struct device *dev) 11351da177e4SLinus Torvalds { 11361da177e4SLinus Torvalds struct device *parent = dev->parent; 1137c47ed219SGreg Kroah-Hartman struct class_interface *class_intf; 11381da177e4SLinus Torvalds 1139ec0676eeSAlan Stern /* Notify clients of device removal. This call must come 1140ec0676eeSAlan Stern * before dpm_sysfs_remove(). 1141ec0676eeSAlan Stern */ 1142ec0676eeSAlan Stern if (dev->bus) 1143ec0676eeSAlan Stern blocking_notifier_call_chain(&dev->bus->p->bus_notifier, 1144ec0676eeSAlan Stern BUS_NOTIFY_DEL_DEVICE, dev); 1145775b64d2SRafael J. Wysocki device_pm_remove(dev); 11463b98aeafSAlan Stern dpm_sysfs_remove(dev); 11471da177e4SLinus Torvalds if (parent) 1148f791b8c8SGreg Kroah-Hartman klist_del(&dev->p->knode_parent); 1149e105b8bfSDan Williams if (MAJOR(dev->devt)) { 11502b2af54aSKay Sievers devtmpfs_delete_node(dev); 1151e105b8bfSDan Williams device_remove_sys_dev_entry(dev); 1152ad6a1e1cSTejun Heo device_remove_file(dev, &devt_attr); 1153e105b8bfSDan Williams } 1154b9d9c82bSKay Sievers if (dev->class) { 1155da231fd5SKay Sievers device_remove_class_symlinks(dev); 115699ef3ef8SKay Sievers 1157f75b1c60SDave Young mutex_lock(&dev->class->p->class_mutex); 1158c47ed219SGreg Kroah-Hartman /* notify any interfaces that the device is now gone */ 1159184f1f77SGreg Kroah-Hartman list_for_each_entry(class_intf, 1160184f1f77SGreg Kroah-Hartman &dev->class->p->class_interfaces, node) 1161c47ed219SGreg Kroah-Hartman if (class_intf->remove_dev) 1162c47ed219SGreg Kroah-Hartman class_intf->remove_dev(dev, class_intf); 1163c47ed219SGreg Kroah-Hartman /* remove the device from the class list */ 11645a3ceb86STejun Heo klist_del(&dev->knode_class); 1165f75b1c60SDave Young mutex_unlock(&dev->class->p->class_mutex); 1166b9d9c82bSKay Sievers } 1167ad6a1e1cSTejun Heo device_remove_file(dev, &uevent_attr); 11682620efefSGreg Kroah-Hartman device_remove_attrs(dev); 116928953533SBenjamin Herrenschmidt bus_remove_device(dev); 11701da177e4SLinus Torvalds 11712f8d16a9STejun Heo /* 11722f8d16a9STejun Heo * Some platform devices are driven without driver attached 11732f8d16a9STejun Heo * and managed resources may have been acquired. Make sure 11742f8d16a9STejun Heo * all resources are released. 11752f8d16a9STejun Heo */ 11762f8d16a9STejun Heo devres_release_all(dev); 11772f8d16a9STejun Heo 11781da177e4SLinus Torvalds /* Notify the platform of the removal, in case they 11791da177e4SLinus Torvalds * need to do anything... 11801da177e4SLinus Torvalds */ 11811da177e4SLinus Torvalds if (platform_notify_remove) 11821da177e4SLinus Torvalds platform_notify_remove(dev); 1183312c004dSKay Sievers kobject_uevent(&dev->kobj, KOBJ_REMOVE); 1184da231fd5SKay Sievers cleanup_device_parent(dev); 11851da177e4SLinus Torvalds kobject_del(&dev->kobj); 11861da177e4SLinus Torvalds put_device(parent); 11871da177e4SLinus Torvalds } 11881da177e4SLinus Torvalds 11891da177e4SLinus Torvalds /** 11901da177e4SLinus Torvalds * device_unregister - unregister device from system. 11911da177e4SLinus Torvalds * @dev: device going away. 11921da177e4SLinus Torvalds * 11931da177e4SLinus Torvalds * We do this in two parts, like we do device_register(). First, 11941da177e4SLinus Torvalds * we remove it from all the subsystems with device_del(), then 11951da177e4SLinus Torvalds * we decrement the reference count via put_device(). If that 11961da177e4SLinus Torvalds * is the final reference count, the device will be cleaned up 11971da177e4SLinus Torvalds * via device_release() above. Otherwise, the structure will 11981da177e4SLinus Torvalds * stick around until the final reference to the device is dropped. 11991da177e4SLinus Torvalds */ 12001da177e4SLinus Torvalds void device_unregister(struct device *dev) 12011da177e4SLinus Torvalds { 12021e0b2cf9SKay Sievers pr_debug("device: '%s': %s\n", dev_name(dev), __func__); 12031da177e4SLinus Torvalds device_del(dev); 12041da177e4SLinus Torvalds put_device(dev); 12051da177e4SLinus Torvalds } 12061da177e4SLinus Torvalds 120736239577Smochel@digitalimplant.org static struct device *next_device(struct klist_iter *i) 120836239577Smochel@digitalimplant.org { 120936239577Smochel@digitalimplant.org struct klist_node *n = klist_next(i); 1210f791b8c8SGreg Kroah-Hartman struct device *dev = NULL; 1211f791b8c8SGreg Kroah-Hartman struct device_private *p; 1212f791b8c8SGreg Kroah-Hartman 1213f791b8c8SGreg Kroah-Hartman if (n) { 1214f791b8c8SGreg Kroah-Hartman p = to_device_private_parent(n); 1215f791b8c8SGreg Kroah-Hartman dev = p->device; 1216f791b8c8SGreg Kroah-Hartman } 1217f791b8c8SGreg Kroah-Hartman return dev; 121836239577Smochel@digitalimplant.org } 121936239577Smochel@digitalimplant.org 12201da177e4SLinus Torvalds /** 1221e454cea2SKay Sievers * device_get_devnode - path of device node file 12226fcf53acSKay Sievers * @dev: device 1223e454cea2SKay Sievers * @mode: returned file access mode 12246fcf53acSKay Sievers * @tmp: possibly allocated string 12256fcf53acSKay Sievers * 12266fcf53acSKay Sievers * Return the relative path of a possible device node. 12276fcf53acSKay Sievers * Non-default names may need to allocate a memory to compose 12286fcf53acSKay Sievers * a name. This memory is returned in tmp and needs to be 12296fcf53acSKay Sievers * freed by the caller. 12306fcf53acSKay Sievers */ 1231e454cea2SKay Sievers const char *device_get_devnode(struct device *dev, 1232e454cea2SKay Sievers mode_t *mode, const char **tmp) 12336fcf53acSKay Sievers { 12346fcf53acSKay Sievers char *s; 12356fcf53acSKay Sievers 12366fcf53acSKay Sievers *tmp = NULL; 12376fcf53acSKay Sievers 12386fcf53acSKay Sievers /* the device type may provide a specific name */ 1239e454cea2SKay Sievers if (dev->type && dev->type->devnode) 1240e454cea2SKay Sievers *tmp = dev->type->devnode(dev, mode); 12416fcf53acSKay Sievers if (*tmp) 12426fcf53acSKay Sievers return *tmp; 12436fcf53acSKay Sievers 12446fcf53acSKay Sievers /* the class may provide a specific name */ 1245e454cea2SKay Sievers if (dev->class && dev->class->devnode) 1246e454cea2SKay Sievers *tmp = dev->class->devnode(dev, mode); 12476fcf53acSKay Sievers if (*tmp) 12486fcf53acSKay Sievers return *tmp; 12496fcf53acSKay Sievers 12506fcf53acSKay Sievers /* return name without allocation, tmp == NULL */ 12516fcf53acSKay Sievers if (strchr(dev_name(dev), '!') == NULL) 12526fcf53acSKay Sievers return dev_name(dev); 12536fcf53acSKay Sievers 12546fcf53acSKay Sievers /* replace '!' in the name with '/' */ 12556fcf53acSKay Sievers *tmp = kstrdup(dev_name(dev), GFP_KERNEL); 12566fcf53acSKay Sievers if (!*tmp) 12576fcf53acSKay Sievers return NULL; 12586fcf53acSKay Sievers while ((s = strchr(*tmp, '!'))) 12596fcf53acSKay Sievers s[0] = '/'; 12606fcf53acSKay Sievers return *tmp; 12616fcf53acSKay Sievers } 12626fcf53acSKay Sievers 12636fcf53acSKay Sievers /** 12641da177e4SLinus Torvalds * device_for_each_child - device child iterator. 1265c41455fbSRandy Dunlap * @parent: parent struct device. 12661da177e4SLinus Torvalds * @data: data for the callback. 12671da177e4SLinus Torvalds * @fn: function to be called for each device. 12681da177e4SLinus Torvalds * 1269c41455fbSRandy Dunlap * Iterate over @parent's child devices, and call @fn for each, 12701da177e4SLinus Torvalds * passing it @data. 12711da177e4SLinus Torvalds * 12721da177e4SLinus Torvalds * We check the return of @fn each time. If it returns anything 12731da177e4SLinus Torvalds * other than 0, we break out and return that value. 12741da177e4SLinus Torvalds */ 127536239577Smochel@digitalimplant.org int device_for_each_child(struct device *parent, void *data, 12764a3ad20cSGreg Kroah-Hartman int (*fn)(struct device *dev, void *data)) 12771da177e4SLinus Torvalds { 127836239577Smochel@digitalimplant.org struct klist_iter i; 12791da177e4SLinus Torvalds struct device *child; 12801da177e4SLinus Torvalds int error = 0; 12811da177e4SLinus Torvalds 1282014c90dbSGreg Kroah-Hartman if (!parent->p) 1283014c90dbSGreg Kroah-Hartman return 0; 1284014c90dbSGreg Kroah-Hartman 1285f791b8c8SGreg Kroah-Hartman klist_iter_init(&parent->p->klist_children, &i); 128636239577Smochel@digitalimplant.org while ((child = next_device(&i)) && !error) 128736239577Smochel@digitalimplant.org error = fn(child, data); 128836239577Smochel@digitalimplant.org klist_iter_exit(&i); 12891da177e4SLinus Torvalds return error; 12901da177e4SLinus Torvalds } 12911da177e4SLinus Torvalds 12925ab69981SCornelia Huck /** 12935ab69981SCornelia Huck * device_find_child - device iterator for locating a particular device. 12945ab69981SCornelia Huck * @parent: parent struct device 12955ab69981SCornelia Huck * @data: Data to pass to match function 12965ab69981SCornelia Huck * @match: Callback function to check device 12975ab69981SCornelia Huck * 12985ab69981SCornelia Huck * This is similar to the device_for_each_child() function above, but it 12995ab69981SCornelia Huck * returns a reference to a device that is 'found' for later use, as 13005ab69981SCornelia Huck * determined by the @match callback. 13015ab69981SCornelia Huck * 13025ab69981SCornelia Huck * The callback should return 0 if the device doesn't match and non-zero 13035ab69981SCornelia Huck * if it does. If the callback returns non-zero and a reference to the 13045ab69981SCornelia Huck * current device can be obtained, this function will return to the caller 13055ab69981SCornelia Huck * and not iterate over any more devices. 13065ab69981SCornelia Huck */ 13075ab69981SCornelia Huck struct device *device_find_child(struct device *parent, void *data, 13084a3ad20cSGreg Kroah-Hartman int (*match)(struct device *dev, void *data)) 13095ab69981SCornelia Huck { 13105ab69981SCornelia Huck struct klist_iter i; 13115ab69981SCornelia Huck struct device *child; 13125ab69981SCornelia Huck 13135ab69981SCornelia Huck if (!parent) 13145ab69981SCornelia Huck return NULL; 13155ab69981SCornelia Huck 1316f791b8c8SGreg Kroah-Hartman klist_iter_init(&parent->p->klist_children, &i); 13175ab69981SCornelia Huck while ((child = next_device(&i))) 13185ab69981SCornelia Huck if (match(child, data) && get_device(child)) 13195ab69981SCornelia Huck break; 13205ab69981SCornelia Huck klist_iter_exit(&i); 13215ab69981SCornelia Huck return child; 13225ab69981SCornelia Huck } 13235ab69981SCornelia Huck 13241da177e4SLinus Torvalds int __init devices_init(void) 13251da177e4SLinus Torvalds { 1326881c6cfdSGreg Kroah-Hartman devices_kset = kset_create_and_add("devices", &device_uevent_ops, NULL); 1327881c6cfdSGreg Kroah-Hartman if (!devices_kset) 1328881c6cfdSGreg Kroah-Hartman return -ENOMEM; 1329e105b8bfSDan Williams dev_kobj = kobject_create_and_add("dev", NULL); 1330e105b8bfSDan Williams if (!dev_kobj) 1331e105b8bfSDan Williams goto dev_kobj_err; 1332e105b8bfSDan Williams sysfs_dev_block_kobj = kobject_create_and_add("block", dev_kobj); 1333e105b8bfSDan Williams if (!sysfs_dev_block_kobj) 1334e105b8bfSDan Williams goto block_kobj_err; 1335e105b8bfSDan Williams sysfs_dev_char_kobj = kobject_create_and_add("char", dev_kobj); 1336e105b8bfSDan Williams if (!sysfs_dev_char_kobj) 1337e105b8bfSDan Williams goto char_kobj_err; 1338e105b8bfSDan Williams 1339881c6cfdSGreg Kroah-Hartman return 0; 1340e105b8bfSDan Williams 1341e105b8bfSDan Williams char_kobj_err: 1342e105b8bfSDan Williams kobject_put(sysfs_dev_block_kobj); 1343e105b8bfSDan Williams block_kobj_err: 1344e105b8bfSDan Williams kobject_put(dev_kobj); 1345e105b8bfSDan Williams dev_kobj_err: 1346e105b8bfSDan Williams kset_unregister(devices_kset); 1347e105b8bfSDan Williams return -ENOMEM; 13481da177e4SLinus Torvalds } 13491da177e4SLinus Torvalds 13501da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(device_for_each_child); 13515ab69981SCornelia Huck EXPORT_SYMBOL_GPL(device_find_child); 13521da177e4SLinus Torvalds 13531da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(device_initialize); 13541da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(device_add); 13551da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(device_register); 13561da177e4SLinus Torvalds 13571da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(device_del); 13581da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(device_unregister); 13591da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(get_device); 13601da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(put_device); 13611da177e4SLinus Torvalds 13621da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(device_create_file); 13631da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(device_remove_file); 136423681e47SGreg Kroah-Hartman 13650aa0dc41SMark McLoughlin struct root_device 13660aa0dc41SMark McLoughlin { 13670aa0dc41SMark McLoughlin struct device dev; 13680aa0dc41SMark McLoughlin struct module *owner; 13690aa0dc41SMark McLoughlin }; 13700aa0dc41SMark McLoughlin 13710aa0dc41SMark McLoughlin #define to_root_device(dev) container_of(dev, struct root_device, dev) 13720aa0dc41SMark McLoughlin 13730aa0dc41SMark McLoughlin static void root_device_release(struct device *dev) 13740aa0dc41SMark McLoughlin { 13750aa0dc41SMark McLoughlin kfree(to_root_device(dev)); 13760aa0dc41SMark McLoughlin } 13770aa0dc41SMark McLoughlin 13780aa0dc41SMark McLoughlin /** 13790aa0dc41SMark McLoughlin * __root_device_register - allocate and register a root device 13800aa0dc41SMark McLoughlin * @name: root device name 13810aa0dc41SMark McLoughlin * @owner: owner module of the root device, usually THIS_MODULE 13820aa0dc41SMark McLoughlin * 13830aa0dc41SMark McLoughlin * This function allocates a root device and registers it 13840aa0dc41SMark McLoughlin * using device_register(). In order to free the returned 13850aa0dc41SMark McLoughlin * device, use root_device_unregister(). 13860aa0dc41SMark McLoughlin * 13870aa0dc41SMark McLoughlin * Root devices are dummy devices which allow other devices 13880aa0dc41SMark McLoughlin * to be grouped under /sys/devices. Use this function to 13890aa0dc41SMark McLoughlin * allocate a root device and then use it as the parent of 13900aa0dc41SMark McLoughlin * any device which should appear under /sys/devices/{name} 13910aa0dc41SMark McLoughlin * 13920aa0dc41SMark McLoughlin * The /sys/devices/{name} directory will also contain a 13930aa0dc41SMark McLoughlin * 'module' symlink which points to the @owner directory 13940aa0dc41SMark McLoughlin * in sysfs. 13950aa0dc41SMark McLoughlin * 1396f0eae0edSJani Nikula * Returns &struct device pointer on success, or ERR_PTR() on error. 1397f0eae0edSJani Nikula * 13980aa0dc41SMark McLoughlin * Note: You probably want to use root_device_register(). 13990aa0dc41SMark McLoughlin */ 14000aa0dc41SMark McLoughlin struct device *__root_device_register(const char *name, struct module *owner) 14010aa0dc41SMark McLoughlin { 14020aa0dc41SMark McLoughlin struct root_device *root; 14030aa0dc41SMark McLoughlin int err = -ENOMEM; 14040aa0dc41SMark McLoughlin 14050aa0dc41SMark McLoughlin root = kzalloc(sizeof(struct root_device), GFP_KERNEL); 14060aa0dc41SMark McLoughlin if (!root) 14070aa0dc41SMark McLoughlin return ERR_PTR(err); 14080aa0dc41SMark McLoughlin 1409acc0e90fSGreg Kroah-Hartman err = dev_set_name(&root->dev, "%s", name); 14100aa0dc41SMark McLoughlin if (err) { 14110aa0dc41SMark McLoughlin kfree(root); 14120aa0dc41SMark McLoughlin return ERR_PTR(err); 14130aa0dc41SMark McLoughlin } 14140aa0dc41SMark McLoughlin 14150aa0dc41SMark McLoughlin root->dev.release = root_device_release; 14160aa0dc41SMark McLoughlin 14170aa0dc41SMark McLoughlin err = device_register(&root->dev); 14180aa0dc41SMark McLoughlin if (err) { 14190aa0dc41SMark McLoughlin put_device(&root->dev); 14200aa0dc41SMark McLoughlin return ERR_PTR(err); 14210aa0dc41SMark McLoughlin } 14220aa0dc41SMark McLoughlin 14231d9e882bSChristoph Egger #ifdef CONFIG_MODULES /* gotta find a "cleaner" way to do this */ 14240aa0dc41SMark McLoughlin if (owner) { 14250aa0dc41SMark McLoughlin struct module_kobject *mk = &owner->mkobj; 14260aa0dc41SMark McLoughlin 14270aa0dc41SMark McLoughlin err = sysfs_create_link(&root->dev.kobj, &mk->kobj, "module"); 14280aa0dc41SMark McLoughlin if (err) { 14290aa0dc41SMark McLoughlin device_unregister(&root->dev); 14300aa0dc41SMark McLoughlin return ERR_PTR(err); 14310aa0dc41SMark McLoughlin } 14320aa0dc41SMark McLoughlin root->owner = owner; 14330aa0dc41SMark McLoughlin } 14340aa0dc41SMark McLoughlin #endif 14350aa0dc41SMark McLoughlin 14360aa0dc41SMark McLoughlin return &root->dev; 14370aa0dc41SMark McLoughlin } 14380aa0dc41SMark McLoughlin EXPORT_SYMBOL_GPL(__root_device_register); 14390aa0dc41SMark McLoughlin 14400aa0dc41SMark McLoughlin /** 14410aa0dc41SMark McLoughlin * root_device_unregister - unregister and free a root device 14427cbcf225SRandy Dunlap * @dev: device going away 14430aa0dc41SMark McLoughlin * 14440aa0dc41SMark McLoughlin * This function unregisters and cleans up a device that was created by 14450aa0dc41SMark McLoughlin * root_device_register(). 14460aa0dc41SMark McLoughlin */ 14470aa0dc41SMark McLoughlin void root_device_unregister(struct device *dev) 14480aa0dc41SMark McLoughlin { 14490aa0dc41SMark McLoughlin struct root_device *root = to_root_device(dev); 14500aa0dc41SMark McLoughlin 14510aa0dc41SMark McLoughlin if (root->owner) 14520aa0dc41SMark McLoughlin sysfs_remove_link(&root->dev.kobj, "module"); 14530aa0dc41SMark McLoughlin 14540aa0dc41SMark McLoughlin device_unregister(dev); 14550aa0dc41SMark McLoughlin } 14560aa0dc41SMark McLoughlin EXPORT_SYMBOL_GPL(root_device_unregister); 14570aa0dc41SMark McLoughlin 145823681e47SGreg Kroah-Hartman 145923681e47SGreg Kroah-Hartman static void device_create_release(struct device *dev) 146023681e47SGreg Kroah-Hartman { 14611e0b2cf9SKay Sievers pr_debug("device: '%s': %s\n", dev_name(dev), __func__); 146223681e47SGreg Kroah-Hartman kfree(dev); 146323681e47SGreg Kroah-Hartman } 146423681e47SGreg Kroah-Hartman 146523681e47SGreg Kroah-Hartman /** 14668882b394SGreg Kroah-Hartman * device_create_vargs - creates a device and registers it with sysfs 14678882b394SGreg Kroah-Hartman * @class: pointer to the struct class that this device should be registered to 14688882b394SGreg Kroah-Hartman * @parent: pointer to the parent struct device of this new device, if any 14698882b394SGreg Kroah-Hartman * @devt: the dev_t for the char device to be added 14708882b394SGreg Kroah-Hartman * @drvdata: the data to be added to the device for callbacks 14718882b394SGreg Kroah-Hartman * @fmt: string for the device's name 14728882b394SGreg Kroah-Hartman * @args: va_list for the device's name 14738882b394SGreg Kroah-Hartman * 14748882b394SGreg Kroah-Hartman * This function can be used by char device classes. A struct device 14758882b394SGreg Kroah-Hartman * will be created in sysfs, registered to the specified class. 14768882b394SGreg Kroah-Hartman * 14778882b394SGreg Kroah-Hartman * A "dev" file will be created, showing the dev_t for the device, if 14788882b394SGreg Kroah-Hartman * the dev_t is not 0,0. 14798882b394SGreg Kroah-Hartman * If a pointer to a parent struct device is passed in, the newly created 14808882b394SGreg Kroah-Hartman * struct device will be a child of that device in sysfs. 14818882b394SGreg Kroah-Hartman * The pointer to the struct device will be returned from the call. 14828882b394SGreg Kroah-Hartman * Any further sysfs files that might be required can be created using this 14838882b394SGreg Kroah-Hartman * pointer. 14848882b394SGreg Kroah-Hartman * 1485f0eae0edSJani Nikula * Returns &struct device pointer on success, or ERR_PTR() on error. 1486f0eae0edSJani Nikula * 14878882b394SGreg Kroah-Hartman * Note: the struct class passed to this function must have previously 14888882b394SGreg Kroah-Hartman * been created with a call to class_create(). 14898882b394SGreg Kroah-Hartman */ 14908882b394SGreg Kroah-Hartman struct device *device_create_vargs(struct class *class, struct device *parent, 14918882b394SGreg Kroah-Hartman dev_t devt, void *drvdata, const char *fmt, 14928882b394SGreg Kroah-Hartman va_list args) 14938882b394SGreg Kroah-Hartman { 14948882b394SGreg Kroah-Hartman struct device *dev = NULL; 14958882b394SGreg Kroah-Hartman int retval = -ENODEV; 14968882b394SGreg Kroah-Hartman 14978882b394SGreg Kroah-Hartman if (class == NULL || IS_ERR(class)) 14988882b394SGreg Kroah-Hartman goto error; 14998882b394SGreg Kroah-Hartman 15008882b394SGreg Kroah-Hartman dev = kzalloc(sizeof(*dev), GFP_KERNEL); 15018882b394SGreg Kroah-Hartman if (!dev) { 15028882b394SGreg Kroah-Hartman retval = -ENOMEM; 15038882b394SGreg Kroah-Hartman goto error; 15048882b394SGreg Kroah-Hartman } 15058882b394SGreg Kroah-Hartman 15068882b394SGreg Kroah-Hartman dev->devt = devt; 15078882b394SGreg Kroah-Hartman dev->class = class; 15088882b394SGreg Kroah-Hartman dev->parent = parent; 15098882b394SGreg Kroah-Hartman dev->release = device_create_release; 15108882b394SGreg Kroah-Hartman dev_set_drvdata(dev, drvdata); 15118882b394SGreg Kroah-Hartman 15121fa5ae85SKay Sievers retval = kobject_set_name_vargs(&dev->kobj, fmt, args); 15131fa5ae85SKay Sievers if (retval) 15141fa5ae85SKay Sievers goto error; 15151fa5ae85SKay Sievers 15168882b394SGreg Kroah-Hartman retval = device_register(dev); 15178882b394SGreg Kroah-Hartman if (retval) 15188882b394SGreg Kroah-Hartman goto error; 15198882b394SGreg Kroah-Hartman 15208882b394SGreg Kroah-Hartman return dev; 15218882b394SGreg Kroah-Hartman 15228882b394SGreg Kroah-Hartman error: 1523286661b3SCornelia Huck put_device(dev); 15248882b394SGreg Kroah-Hartman return ERR_PTR(retval); 15258882b394SGreg Kroah-Hartman } 15268882b394SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(device_create_vargs); 15278882b394SGreg Kroah-Hartman 15288882b394SGreg Kroah-Hartman /** 15294e106739SGreg Kroah-Hartman * device_create - creates a device and registers it with sysfs 15308882b394SGreg Kroah-Hartman * @class: pointer to the struct class that this device should be registered to 15318882b394SGreg Kroah-Hartman * @parent: pointer to the parent struct device of this new device, if any 15328882b394SGreg Kroah-Hartman * @devt: the dev_t for the char device to be added 15338882b394SGreg Kroah-Hartman * @drvdata: the data to be added to the device for callbacks 15348882b394SGreg Kroah-Hartman * @fmt: string for the device's name 15358882b394SGreg Kroah-Hartman * 15368882b394SGreg Kroah-Hartman * This function can be used by char device classes. A struct device 15378882b394SGreg Kroah-Hartman * will be created in sysfs, registered to the specified class. 15388882b394SGreg Kroah-Hartman * 15398882b394SGreg Kroah-Hartman * A "dev" file will be created, showing the dev_t for the device, if 15408882b394SGreg Kroah-Hartman * the dev_t is not 0,0. 15418882b394SGreg Kroah-Hartman * If a pointer to a parent struct device is passed in, the newly created 15428882b394SGreg Kroah-Hartman * struct device will be a child of that device in sysfs. 15438882b394SGreg Kroah-Hartman * The pointer to the struct device will be returned from the call. 15448882b394SGreg Kroah-Hartman * Any further sysfs files that might be required can be created using this 15458882b394SGreg Kroah-Hartman * pointer. 15468882b394SGreg Kroah-Hartman * 1547f0eae0edSJani Nikula * Returns &struct device pointer on success, or ERR_PTR() on error. 1548f0eae0edSJani Nikula * 15498882b394SGreg Kroah-Hartman * Note: the struct class passed to this function must have previously 15508882b394SGreg Kroah-Hartman * been created with a call to class_create(). 15518882b394SGreg Kroah-Hartman */ 15524e106739SGreg Kroah-Hartman struct device *device_create(struct class *class, struct device *parent, 15534e106739SGreg Kroah-Hartman dev_t devt, void *drvdata, const char *fmt, ...) 15548882b394SGreg Kroah-Hartman { 15558882b394SGreg Kroah-Hartman va_list vargs; 15568882b394SGreg Kroah-Hartman struct device *dev; 15578882b394SGreg Kroah-Hartman 15588882b394SGreg Kroah-Hartman va_start(vargs, fmt); 15598882b394SGreg Kroah-Hartman dev = device_create_vargs(class, parent, devt, drvdata, fmt, vargs); 15608882b394SGreg Kroah-Hartman va_end(vargs); 15618882b394SGreg Kroah-Hartman return dev; 15628882b394SGreg Kroah-Hartman } 15634e106739SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(device_create); 15648882b394SGreg Kroah-Hartman 1565cd35449bSDave Young static int __match_devt(struct device *dev, void *data) 156623681e47SGreg Kroah-Hartman { 1567cd35449bSDave Young dev_t *devt = data; 156823681e47SGreg Kroah-Hartman 1569cd35449bSDave Young return dev->devt == *devt; 1570775b64d2SRafael J. Wysocki } 157123681e47SGreg Kroah-Hartman 1572775b64d2SRafael J. Wysocki /** 1573775b64d2SRafael J. Wysocki * device_destroy - removes a device that was created with device_create() 1574775b64d2SRafael J. Wysocki * @class: pointer to the struct class that this device was registered with 1575775b64d2SRafael J. Wysocki * @devt: the dev_t of the device that was previously registered 1576775b64d2SRafael J. Wysocki * 1577775b64d2SRafael J. Wysocki * This call unregisters and cleans up a device that was created with a 1578775b64d2SRafael J. Wysocki * call to device_create(). 1579775b64d2SRafael J. Wysocki */ 1580775b64d2SRafael J. Wysocki void device_destroy(struct class *class, dev_t devt) 1581775b64d2SRafael J. Wysocki { 1582775b64d2SRafael J. Wysocki struct device *dev; 1583775b64d2SRafael J. Wysocki 1584695794aeSGreg Kroah-Hartman dev = class_find_device(class, NULL, &devt, __match_devt); 1585cd35449bSDave Young if (dev) { 1586cd35449bSDave Young put_device(dev); 158723681e47SGreg Kroah-Hartman device_unregister(dev); 158823681e47SGreg Kroah-Hartman } 1589cd35449bSDave Young } 159023681e47SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(device_destroy); 1591a2de48caSGreg Kroah-Hartman 1592a2de48caSGreg Kroah-Hartman /** 1593a2de48caSGreg Kroah-Hartman * device_rename - renames a device 1594a2de48caSGreg Kroah-Hartman * @dev: the pointer to the struct device to be renamed 1595a2de48caSGreg Kroah-Hartman * @new_name: the new name of the device 1596030c1d2bSEric W. Biederman * 1597030c1d2bSEric W. Biederman * It is the responsibility of the caller to provide mutual 1598030c1d2bSEric W. Biederman * exclusion between two different calls of device_rename 1599030c1d2bSEric W. Biederman * on the same device to ensure that new_name is valid and 1600030c1d2bSEric W. Biederman * won't conflict with other devices. 1601a2de48caSGreg Kroah-Hartman */ 1602a2de48caSGreg Kroah-Hartman int device_rename(struct device *dev, char *new_name) 1603a2de48caSGreg Kroah-Hartman { 1604a2de48caSGreg Kroah-Hartman char *old_class_name = NULL; 1605a2de48caSGreg Kroah-Hartman char *new_class_name = NULL; 16062ee97cafSCornelia Huck char *old_device_name = NULL; 1607a2de48caSGreg Kroah-Hartman int error; 1608a2de48caSGreg Kroah-Hartman 1609a2de48caSGreg Kroah-Hartman dev = get_device(dev); 1610a2de48caSGreg Kroah-Hartman if (!dev) 1611a2de48caSGreg Kroah-Hartman return -EINVAL; 1612a2de48caSGreg Kroah-Hartman 16131e0b2cf9SKay Sievers pr_debug("device: '%s': %s: renaming to '%s'\n", dev_name(dev), 16142b3a302aSHarvey Harrison __func__, new_name); 1615a2de48caSGreg Kroah-Hartman 161699ef3ef8SKay Sievers #ifdef CONFIG_SYSFS_DEPRECATED 1617a2de48caSGreg Kroah-Hartman if ((dev->class) && (dev->parent)) 1618a2de48caSGreg Kroah-Hartman old_class_name = make_class_name(dev->class->name, &dev->kobj); 161999ef3ef8SKay Sievers #endif 1620a2de48caSGreg Kroah-Hartman 16211fa5ae85SKay Sievers old_device_name = kstrdup(dev_name(dev), GFP_KERNEL); 16222ee97cafSCornelia Huck if (!old_device_name) { 1623952ab431SJesper Juhl error = -ENOMEM; 16242ee97cafSCornelia Huck goto out; 1625952ab431SJesper Juhl } 1626a2de48caSGreg Kroah-Hartman 1627f349cf34SEric W. Biederman #ifndef CONFIG_SYSFS_DEPRECATED 1628f349cf34SEric W. Biederman if (dev->class) { 1629f349cf34SEric W. Biederman error = sysfs_rename_link(&dev->class->p->class_subsys.kobj, 1630f349cf34SEric W. Biederman &dev->kobj, old_device_name, new_name); 1631f349cf34SEric W. Biederman if (error) 1632f349cf34SEric W. Biederman goto out; 1633f349cf34SEric W. Biederman } 1634f349cf34SEric W. Biederman #endif 1635a2de48caSGreg Kroah-Hartman error = kobject_rename(&dev->kobj, new_name); 16361fa5ae85SKay Sievers if (error) 16372ee97cafSCornelia Huck goto out; 1638a2de48caSGreg Kroah-Hartman 163999ef3ef8SKay Sievers #ifdef CONFIG_SYSFS_DEPRECATED 1640a2de48caSGreg Kroah-Hartman if (old_class_name) { 1641a2de48caSGreg Kroah-Hartman new_class_name = make_class_name(dev->class->name, &dev->kobj); 1642a2de48caSGreg Kroah-Hartman if (new_class_name) { 16432354dcc7SEric W. Biederman error = sysfs_rename_link(&dev->parent->kobj, 164436ce6dadSCornelia Huck &dev->kobj, 16452354dcc7SEric W. Biederman old_class_name, 164636ce6dadSCornelia Huck new_class_name); 1647a2de48caSGreg Kroah-Hartman } 1648a2de48caSGreg Kroah-Hartman } 164960b8cabdSKay Sievers #endif 165060b8cabdSKay Sievers 16512ee97cafSCornelia Huck out: 1652a2de48caSGreg Kroah-Hartman put_device(dev); 1653a2de48caSGreg Kroah-Hartman 1654a2de48caSGreg Kroah-Hartman kfree(new_class_name); 1655952ab431SJesper Juhl kfree(old_class_name); 16562ee97cafSCornelia Huck kfree(old_device_name); 1657a2de48caSGreg Kroah-Hartman 1658a2de48caSGreg Kroah-Hartman return error; 1659a2de48caSGreg Kroah-Hartman } 1660a2807dbcSJohannes Berg EXPORT_SYMBOL_GPL(device_rename); 16618a82472fSCornelia Huck 16628a82472fSCornelia Huck static int device_move_class_links(struct device *dev, 16638a82472fSCornelia Huck struct device *old_parent, 16648a82472fSCornelia Huck struct device *new_parent) 16658a82472fSCornelia Huck { 1666f7f3461dSGreg Kroah-Hartman int error = 0; 16678a82472fSCornelia Huck #ifdef CONFIG_SYSFS_DEPRECATED 16688a82472fSCornelia Huck char *class_name; 16698a82472fSCornelia Huck 16708a82472fSCornelia Huck class_name = make_class_name(dev->class->name, &dev->kobj); 16718a82472fSCornelia Huck if (!class_name) { 1672cb360bbfSCornelia Huck error = -ENOMEM; 16738a82472fSCornelia Huck goto out; 16748a82472fSCornelia Huck } 16758a82472fSCornelia Huck if (old_parent) { 16768a82472fSCornelia Huck sysfs_remove_link(&dev->kobj, "device"); 16778a82472fSCornelia Huck sysfs_remove_link(&old_parent->kobj, class_name); 16788a82472fSCornelia Huck } 1679c744aeaeSCornelia Huck if (new_parent) { 1680c744aeaeSCornelia Huck error = sysfs_create_link(&dev->kobj, &new_parent->kobj, 1681c744aeaeSCornelia Huck "device"); 16828a82472fSCornelia Huck if (error) 16838a82472fSCornelia Huck goto out; 1684c744aeaeSCornelia Huck error = sysfs_create_link(&new_parent->kobj, &dev->kobj, 1685c744aeaeSCornelia Huck class_name); 16868a82472fSCornelia Huck if (error) 16878a82472fSCornelia Huck sysfs_remove_link(&dev->kobj, "device"); 16884a3ad20cSGreg Kroah-Hartman } else 1689c744aeaeSCornelia Huck error = 0; 16908a82472fSCornelia Huck out: 16918a82472fSCornelia Huck kfree(class_name); 16928a82472fSCornelia Huck return error; 16938a82472fSCornelia Huck #else 1694f7f3461dSGreg Kroah-Hartman if (old_parent) 1695f7f3461dSGreg Kroah-Hartman sysfs_remove_link(&dev->kobj, "device"); 1696f7f3461dSGreg Kroah-Hartman if (new_parent) 1697f7f3461dSGreg Kroah-Hartman error = sysfs_create_link(&dev->kobj, &new_parent->kobj, 1698f7f3461dSGreg Kroah-Hartman "device"); 1699f7f3461dSGreg Kroah-Hartman return error; 17008a82472fSCornelia Huck #endif 17018a82472fSCornelia Huck } 17028a82472fSCornelia Huck 17038a82472fSCornelia Huck /** 17048a82472fSCornelia Huck * device_move - moves a device to a new parent 17058a82472fSCornelia Huck * @dev: the pointer to the struct device to be moved 1706c744aeaeSCornelia Huck * @new_parent: the new parent of the device (can by NULL) 1707ffa6a705SCornelia Huck * @dpm_order: how to reorder the dpm_list 17088a82472fSCornelia Huck */ 1709ffa6a705SCornelia Huck int device_move(struct device *dev, struct device *new_parent, 1710ffa6a705SCornelia Huck enum dpm_order dpm_order) 17118a82472fSCornelia Huck { 17128a82472fSCornelia Huck int error; 17138a82472fSCornelia Huck struct device *old_parent; 1714c744aeaeSCornelia Huck struct kobject *new_parent_kobj; 17158a82472fSCornelia Huck 17168a82472fSCornelia Huck dev = get_device(dev); 17178a82472fSCornelia Huck if (!dev) 17188a82472fSCornelia Huck return -EINVAL; 17198a82472fSCornelia Huck 1720ffa6a705SCornelia Huck device_pm_lock(); 17218a82472fSCornelia Huck new_parent = get_device(new_parent); 1722c744aeaeSCornelia Huck new_parent_kobj = get_device_parent(dev, new_parent); 172363b6971aSCornelia Huck 17241e0b2cf9SKay Sievers pr_debug("device: '%s': %s: moving to '%s'\n", dev_name(dev), 17251e0b2cf9SKay Sievers __func__, new_parent ? dev_name(new_parent) : "<NULL>"); 1726c744aeaeSCornelia Huck error = kobject_move(&dev->kobj, new_parent_kobj); 17278a82472fSCornelia Huck if (error) { 172863b6971aSCornelia Huck cleanup_glue_dir(dev, new_parent_kobj); 17298a82472fSCornelia Huck put_device(new_parent); 17308a82472fSCornelia Huck goto out; 17318a82472fSCornelia Huck } 17328a82472fSCornelia Huck old_parent = dev->parent; 17338a82472fSCornelia Huck dev->parent = new_parent; 17348a82472fSCornelia Huck if (old_parent) 1735f791b8c8SGreg Kroah-Hartman klist_remove(&dev->p->knode_parent); 17360d358f22SYinghai Lu if (new_parent) { 1737f791b8c8SGreg Kroah-Hartman klist_add_tail(&dev->p->knode_parent, 1738f791b8c8SGreg Kroah-Hartman &new_parent->p->klist_children); 17390d358f22SYinghai Lu set_dev_node(dev, dev_to_node(new_parent)); 17400d358f22SYinghai Lu } 17410d358f22SYinghai Lu 17428a82472fSCornelia Huck if (!dev->class) 17438a82472fSCornelia Huck goto out_put; 17448a82472fSCornelia Huck error = device_move_class_links(dev, old_parent, new_parent); 17458a82472fSCornelia Huck if (error) { 17468a82472fSCornelia Huck /* We ignore errors on cleanup since we're hosed anyway... */ 17478a82472fSCornelia Huck device_move_class_links(dev, new_parent, old_parent); 17488a82472fSCornelia Huck if (!kobject_move(&dev->kobj, &old_parent->kobj)) { 1749c744aeaeSCornelia Huck if (new_parent) 1750f791b8c8SGreg Kroah-Hartman klist_remove(&dev->p->knode_parent); 17510d358f22SYinghai Lu dev->parent = old_parent; 17520d358f22SYinghai Lu if (old_parent) { 1753f791b8c8SGreg Kroah-Hartman klist_add_tail(&dev->p->knode_parent, 1754f791b8c8SGreg Kroah-Hartman &old_parent->p->klist_children); 17550d358f22SYinghai Lu set_dev_node(dev, dev_to_node(old_parent)); 17560d358f22SYinghai Lu } 17578a82472fSCornelia Huck } 175863b6971aSCornelia Huck cleanup_glue_dir(dev, new_parent_kobj); 17598a82472fSCornelia Huck put_device(new_parent); 17608a82472fSCornelia Huck goto out; 17618a82472fSCornelia Huck } 1762ffa6a705SCornelia Huck switch (dpm_order) { 1763ffa6a705SCornelia Huck case DPM_ORDER_NONE: 1764ffa6a705SCornelia Huck break; 1765ffa6a705SCornelia Huck case DPM_ORDER_DEV_AFTER_PARENT: 1766ffa6a705SCornelia Huck device_pm_move_after(dev, new_parent); 1767ffa6a705SCornelia Huck break; 1768ffa6a705SCornelia Huck case DPM_ORDER_PARENT_BEFORE_DEV: 1769ffa6a705SCornelia Huck device_pm_move_before(new_parent, dev); 1770ffa6a705SCornelia Huck break; 1771ffa6a705SCornelia Huck case DPM_ORDER_DEV_LAST: 1772ffa6a705SCornelia Huck device_pm_move_last(dev); 1773ffa6a705SCornelia Huck break; 1774ffa6a705SCornelia Huck } 17758a82472fSCornelia Huck out_put: 17768a82472fSCornelia Huck put_device(old_parent); 17778a82472fSCornelia Huck out: 1778ffa6a705SCornelia Huck device_pm_unlock(); 17798a82472fSCornelia Huck put_device(dev); 17808a82472fSCornelia Huck return error; 17818a82472fSCornelia Huck } 17828a82472fSCornelia Huck EXPORT_SYMBOL_GPL(device_move); 178337b0c020SGreg Kroah-Hartman 178437b0c020SGreg Kroah-Hartman /** 178537b0c020SGreg Kroah-Hartman * device_shutdown - call ->shutdown() on each device to shutdown. 178637b0c020SGreg Kroah-Hartman */ 178737b0c020SGreg Kroah-Hartman void device_shutdown(void) 178837b0c020SGreg Kroah-Hartman { 17896245838fSHugh Daschbach struct device *dev; 179037b0c020SGreg Kroah-Hartman 17916245838fSHugh Daschbach spin_lock(&devices_kset->list_lock); 17926245838fSHugh Daschbach /* 17936245838fSHugh Daschbach * Walk the devices list backward, shutting down each in turn. 17946245838fSHugh Daschbach * Beware that device unplug events may also start pulling 17956245838fSHugh Daschbach * devices offline, even as the system is shutting down. 17966245838fSHugh Daschbach */ 17976245838fSHugh Daschbach while (!list_empty(&devices_kset->list)) { 17986245838fSHugh Daschbach dev = list_entry(devices_kset->list.prev, struct device, 17996245838fSHugh Daschbach kobj.entry); 18006245838fSHugh Daschbach get_device(dev); 18016245838fSHugh Daschbach /* 18026245838fSHugh Daschbach * Make sure the device is off the kset list, in the 18036245838fSHugh Daschbach * event that dev->*->shutdown() doesn't remove it. 18046245838fSHugh Daschbach */ 18056245838fSHugh Daschbach list_del_init(&dev->kobj.entry); 18066245838fSHugh Daschbach spin_unlock(&devices_kset->list_lock); 18076245838fSHugh Daschbach 180837b0c020SGreg Kroah-Hartman if (dev->bus && dev->bus->shutdown) { 180937b0c020SGreg Kroah-Hartman dev_dbg(dev, "shutdown\n"); 181037b0c020SGreg Kroah-Hartman dev->bus->shutdown(dev); 181137b0c020SGreg Kroah-Hartman } else if (dev->driver && dev->driver->shutdown) { 181237b0c020SGreg Kroah-Hartman dev_dbg(dev, "shutdown\n"); 181337b0c020SGreg Kroah-Hartman dev->driver->shutdown(dev); 181437b0c020SGreg Kroah-Hartman } 18156245838fSHugh Daschbach put_device(dev); 18166245838fSHugh Daschbach 18176245838fSHugh Daschbach spin_lock(&devices_kset->list_lock); 181837b0c020SGreg Kroah-Hartman } 18196245838fSHugh Daschbach spin_unlock(&devices_kset->list_lock); 1820401097eaSShaohua Li async_synchronize_full(); 182137b0c020SGreg Kroah-Hartman } 1822