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> 2107d57a32SGrant Likely #include <linux/of.h> 2207d57a32SGrant Likely #include <linux/of_device.h> 23da231fd5SKay Sievers #include <linux/genhd.h> 24815d2d50SAndrew Morton #include <linux/kallsyms.h> 25f75b1c60SDave Young #include <linux/mutex.h> 26401097eaSShaohua Li #include <linux/async.h> 27af8db150SPeter Chen #include <linux/pm_runtime.h> 28c4e00daaSKay Sievers #include <linux/netdevice.h> 2963967685SGreg Kroah-Hartman #include <linux/sysfs.h> 301da177e4SLinus Torvalds 311da177e4SLinus Torvalds #include "base.h" 321da177e4SLinus Torvalds #include "power/power.h" 331da177e4SLinus Torvalds 34e52eec13SAndi Kleen #ifdef CONFIG_SYSFS_DEPRECATED 35e52eec13SAndi Kleen #ifdef CONFIG_SYSFS_DEPRECATED_V2 36e52eec13SAndi Kleen long sysfs_deprecated = 1; 37e52eec13SAndi Kleen #else 38e52eec13SAndi Kleen long sysfs_deprecated = 0; 39e52eec13SAndi Kleen #endif 403454bf96SHanjun Guo static int __init sysfs_deprecated_setup(char *arg) 41e52eec13SAndi Kleen { 4234da5e67SJingoo Han return kstrtol(arg, 10, &sysfs_deprecated); 43e52eec13SAndi Kleen } 44e52eec13SAndi Kleen early_param("sysfs.deprecated", sysfs_deprecated_setup); 45e52eec13SAndi Kleen #endif 46e52eec13SAndi Kleen 471da177e4SLinus Torvalds int (*platform_notify)(struct device *dev) = NULL; 481da177e4SLinus Torvalds int (*platform_notify_remove)(struct device *dev) = NULL; 49e105b8bfSDan Williams static struct kobject *dev_kobj; 50e105b8bfSDan Williams struct kobject *sysfs_dev_char_kobj; 51e105b8bfSDan Williams struct kobject *sysfs_dev_block_kobj; 521da177e4SLinus Torvalds 535e33bc41SRafael J. Wysocki static DEFINE_MUTEX(device_hotplug_lock); 545e33bc41SRafael J. Wysocki 555e33bc41SRafael J. Wysocki void lock_device_hotplug(void) 565e33bc41SRafael J. Wysocki { 575e33bc41SRafael J. Wysocki mutex_lock(&device_hotplug_lock); 585e33bc41SRafael J. Wysocki } 595e33bc41SRafael J. Wysocki 605e33bc41SRafael J. Wysocki void unlock_device_hotplug(void) 615e33bc41SRafael J. Wysocki { 625e33bc41SRafael J. Wysocki mutex_unlock(&device_hotplug_lock); 635e33bc41SRafael J. Wysocki } 645e33bc41SRafael J. Wysocki 655e33bc41SRafael J. Wysocki int lock_device_hotplug_sysfs(void) 665e33bc41SRafael J. Wysocki { 675e33bc41SRafael J. Wysocki if (mutex_trylock(&device_hotplug_lock)) 685e33bc41SRafael J. Wysocki return 0; 695e33bc41SRafael J. Wysocki 705e33bc41SRafael J. Wysocki /* Avoid busy looping (5 ms of sleep should do). */ 715e33bc41SRafael J. Wysocki msleep(5); 725e33bc41SRafael J. Wysocki return restart_syscall(); 735e33bc41SRafael J. Wysocki } 745e33bc41SRafael J. Wysocki 754e886c29SGreg Kroah-Hartman #ifdef CONFIG_BLOCK 764e886c29SGreg Kroah-Hartman static inline int device_is_not_partition(struct device *dev) 774e886c29SGreg Kroah-Hartman { 784e886c29SGreg Kroah-Hartman return !(dev->type == &part_type); 794e886c29SGreg Kroah-Hartman } 804e886c29SGreg Kroah-Hartman #else 814e886c29SGreg Kroah-Hartman static inline int device_is_not_partition(struct device *dev) 824e886c29SGreg Kroah-Hartman { 834e886c29SGreg Kroah-Hartman return 1; 844e886c29SGreg Kroah-Hartman } 854e886c29SGreg Kroah-Hartman #endif 861da177e4SLinus Torvalds 873e95637aSAlan Stern /** 883e95637aSAlan Stern * dev_driver_string - Return a device's driver name, if at all possible 893e95637aSAlan Stern * @dev: struct device to get the name of 903e95637aSAlan Stern * 913e95637aSAlan Stern * Will return the device's driver's name if it is bound to a device. If 929169c012Syan * the device is not bound to a driver, it will return the name of the bus 933e95637aSAlan Stern * it is attached to. If it is not attached to a bus either, an empty 943e95637aSAlan Stern * string will be returned. 953e95637aSAlan Stern */ 96bf9ca69fSJean Delvare const char *dev_driver_string(const struct device *dev) 973e95637aSAlan Stern { 983589972eSAlan Stern struct device_driver *drv; 993589972eSAlan Stern 1003589972eSAlan Stern /* dev->driver can change to NULL underneath us because of unbinding, 1013589972eSAlan Stern * so be careful about accessing it. dev->bus and dev->class should 1023589972eSAlan Stern * never change once they are set, so they don't need special care. 1033589972eSAlan Stern */ 1043589972eSAlan Stern drv = ACCESS_ONCE(dev->driver); 1053589972eSAlan Stern return drv ? drv->name : 106a456b702SJean Delvare (dev->bus ? dev->bus->name : 107a456b702SJean Delvare (dev->class ? dev->class->name : "")); 1083e95637aSAlan Stern } 109310a922dSMatthew Wilcox EXPORT_SYMBOL(dev_driver_string); 1103e95637aSAlan Stern 1111da177e4SLinus Torvalds #define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr) 1121da177e4SLinus Torvalds 1134a3ad20cSGreg Kroah-Hartman static ssize_t dev_attr_show(struct kobject *kobj, struct attribute *attr, 1144a3ad20cSGreg Kroah-Hartman char *buf) 1151da177e4SLinus Torvalds { 1161da177e4SLinus Torvalds struct device_attribute *dev_attr = to_dev_attr(attr); 117b0d1f807SLars-Peter Clausen struct device *dev = kobj_to_dev(kobj); 1184a0c20bfSDmitry Torokhov ssize_t ret = -EIO; 1191da177e4SLinus Torvalds 1201da177e4SLinus Torvalds if (dev_attr->show) 12154b6f35cSYani Ioannou ret = dev_attr->show(dev, dev_attr, buf); 122815d2d50SAndrew Morton if (ret >= (ssize_t)PAGE_SIZE) { 12353a9c87eSGreg Kroah-Hartman print_symbol("dev_attr_show: %s returned bad count\n", 12453a9c87eSGreg Kroah-Hartman (unsigned long)dev_attr->show); 125815d2d50SAndrew Morton } 1261da177e4SLinus Torvalds return ret; 1271da177e4SLinus Torvalds } 1281da177e4SLinus Torvalds 1294a3ad20cSGreg Kroah-Hartman static ssize_t dev_attr_store(struct kobject *kobj, struct attribute *attr, 1301da177e4SLinus Torvalds const char *buf, size_t count) 1311da177e4SLinus Torvalds { 1321da177e4SLinus Torvalds struct device_attribute *dev_attr = to_dev_attr(attr); 133b0d1f807SLars-Peter Clausen struct device *dev = kobj_to_dev(kobj); 1344a0c20bfSDmitry Torokhov ssize_t ret = -EIO; 1351da177e4SLinus Torvalds 1361da177e4SLinus Torvalds if (dev_attr->store) 13754b6f35cSYani Ioannou ret = dev_attr->store(dev, dev_attr, buf, count); 1381da177e4SLinus Torvalds return ret; 1391da177e4SLinus Torvalds } 1401da177e4SLinus Torvalds 14152cf25d0SEmese Revfy static const struct sysfs_ops dev_sysfs_ops = { 1421da177e4SLinus Torvalds .show = dev_attr_show, 1431da177e4SLinus Torvalds .store = dev_attr_store, 1441da177e4SLinus Torvalds }; 1451da177e4SLinus Torvalds 146ca22e56dSKay Sievers #define to_ext_attr(x) container_of(x, struct dev_ext_attribute, attr) 147ca22e56dSKay Sievers 148ca22e56dSKay Sievers ssize_t device_store_ulong(struct device *dev, 149ca22e56dSKay Sievers struct device_attribute *attr, 150ca22e56dSKay Sievers const char *buf, size_t size) 151ca22e56dSKay Sievers { 152ca22e56dSKay Sievers struct dev_ext_attribute *ea = to_ext_attr(attr); 153ca22e56dSKay Sievers char *end; 154ca22e56dSKay Sievers unsigned long new = simple_strtoul(buf, &end, 0); 155ca22e56dSKay Sievers if (end == buf) 156ca22e56dSKay Sievers return -EINVAL; 157ca22e56dSKay Sievers *(unsigned long *)(ea->var) = new; 158ca22e56dSKay Sievers /* Always return full write size even if we didn't consume all */ 159ca22e56dSKay Sievers return size; 160ca22e56dSKay Sievers } 161ca22e56dSKay Sievers EXPORT_SYMBOL_GPL(device_store_ulong); 162ca22e56dSKay Sievers 163ca22e56dSKay Sievers ssize_t device_show_ulong(struct device *dev, 164ca22e56dSKay Sievers struct device_attribute *attr, 165ca22e56dSKay Sievers char *buf) 166ca22e56dSKay Sievers { 167ca22e56dSKay Sievers struct dev_ext_attribute *ea = to_ext_attr(attr); 168ca22e56dSKay Sievers return snprintf(buf, PAGE_SIZE, "%lx\n", *(unsigned long *)(ea->var)); 169ca22e56dSKay Sievers } 170ca22e56dSKay Sievers EXPORT_SYMBOL_GPL(device_show_ulong); 171ca22e56dSKay Sievers 172ca22e56dSKay Sievers ssize_t device_store_int(struct device *dev, 173ca22e56dSKay Sievers struct device_attribute *attr, 174ca22e56dSKay Sievers const char *buf, size_t size) 175ca22e56dSKay Sievers { 176ca22e56dSKay Sievers struct dev_ext_attribute *ea = to_ext_attr(attr); 177ca22e56dSKay Sievers char *end; 178ca22e56dSKay Sievers long new = simple_strtol(buf, &end, 0); 179ca22e56dSKay Sievers if (end == buf || new > INT_MAX || new < INT_MIN) 180ca22e56dSKay Sievers return -EINVAL; 181ca22e56dSKay Sievers *(int *)(ea->var) = new; 182ca22e56dSKay Sievers /* Always return full write size even if we didn't consume all */ 183ca22e56dSKay Sievers return size; 184ca22e56dSKay Sievers } 185ca22e56dSKay Sievers EXPORT_SYMBOL_GPL(device_store_int); 186ca22e56dSKay Sievers 187ca22e56dSKay Sievers ssize_t device_show_int(struct device *dev, 188ca22e56dSKay Sievers struct device_attribute *attr, 189ca22e56dSKay Sievers char *buf) 190ca22e56dSKay Sievers { 191ca22e56dSKay Sievers struct dev_ext_attribute *ea = to_ext_attr(attr); 192ca22e56dSKay Sievers 193ca22e56dSKay Sievers return snprintf(buf, PAGE_SIZE, "%d\n", *(int *)(ea->var)); 194ca22e56dSKay Sievers } 195ca22e56dSKay Sievers EXPORT_SYMBOL_GPL(device_show_int); 1961da177e4SLinus Torvalds 19791872392SBorislav Petkov ssize_t device_store_bool(struct device *dev, struct device_attribute *attr, 19891872392SBorislav Petkov const char *buf, size_t size) 19991872392SBorislav Petkov { 20091872392SBorislav Petkov struct dev_ext_attribute *ea = to_ext_attr(attr); 20191872392SBorislav Petkov 20291872392SBorislav Petkov if (strtobool(buf, ea->var) < 0) 20391872392SBorislav Petkov return -EINVAL; 20491872392SBorislav Petkov 20591872392SBorislav Petkov return size; 20691872392SBorislav Petkov } 20791872392SBorislav Petkov EXPORT_SYMBOL_GPL(device_store_bool); 20891872392SBorislav Petkov 20991872392SBorislav Petkov ssize_t device_show_bool(struct device *dev, struct device_attribute *attr, 21091872392SBorislav Petkov char *buf) 21191872392SBorislav Petkov { 21291872392SBorislav Petkov struct dev_ext_attribute *ea = to_ext_attr(attr); 21391872392SBorislav Petkov 21491872392SBorislav Petkov return snprintf(buf, PAGE_SIZE, "%d\n", *(bool *)(ea->var)); 21591872392SBorislav Petkov } 21691872392SBorislav Petkov EXPORT_SYMBOL_GPL(device_show_bool); 21791872392SBorislav Petkov 2181da177e4SLinus Torvalds /** 2191da177e4SLinus Torvalds * device_release - free device structure. 2201da177e4SLinus Torvalds * @kobj: device's kobject. 2211da177e4SLinus Torvalds * 2221da177e4SLinus Torvalds * This is called once the reference count for the object 2231da177e4SLinus Torvalds * reaches 0. We forward the call to the device's release 2241da177e4SLinus Torvalds * method, which should handle actually freeing the structure. 2251da177e4SLinus Torvalds */ 2261da177e4SLinus Torvalds static void device_release(struct kobject *kobj) 2271da177e4SLinus Torvalds { 228b0d1f807SLars-Peter Clausen struct device *dev = kobj_to_dev(kobj); 229fb069a5dSGreg Kroah-Hartman struct device_private *p = dev->p; 2301da177e4SLinus Torvalds 231a525a3ddSMing Lei /* 232a525a3ddSMing Lei * Some platform devices are driven without driver attached 233a525a3ddSMing Lei * and managed resources may have been acquired. Make sure 234a525a3ddSMing Lei * all resources are released. 235a525a3ddSMing Lei * 236a525a3ddSMing Lei * Drivers still can add resources into device after device 237a525a3ddSMing Lei * is deleted but alive, so release devres here to avoid 238a525a3ddSMing Lei * possible memory leak. 239a525a3ddSMing Lei */ 240a525a3ddSMing Lei devres_release_all(dev); 241a525a3ddSMing Lei 2421da177e4SLinus Torvalds if (dev->release) 2431da177e4SLinus Torvalds dev->release(dev); 244f9f852dfSKay Sievers else if (dev->type && dev->type->release) 245f9f852dfSKay Sievers dev->type->release(dev); 2462620efefSGreg Kroah-Hartman else if (dev->class && dev->class->dev_release) 2472620efefSGreg Kroah-Hartman dev->class->dev_release(dev); 248f810a5cfSArjan van de Ven else 249f810a5cfSArjan van de Ven WARN(1, KERN_ERR "Device '%s' does not have a release() " 2504a3ad20cSGreg Kroah-Hartman "function, it is broken and must be fixed.\n", 2511e0b2cf9SKay Sievers dev_name(dev)); 252fb069a5dSGreg Kroah-Hartman kfree(p); 2531da177e4SLinus Torvalds } 2541da177e4SLinus Torvalds 255bc451f20SEric W. Biederman static const void *device_namespace(struct kobject *kobj) 256bc451f20SEric W. Biederman { 257b0d1f807SLars-Peter Clausen struct device *dev = kobj_to_dev(kobj); 258bc451f20SEric W. Biederman const void *ns = NULL; 259bc451f20SEric W. Biederman 260bc451f20SEric W. Biederman if (dev->class && dev->class->ns_type) 261bc451f20SEric W. Biederman ns = dev->class->namespace(dev); 262bc451f20SEric W. Biederman 263bc451f20SEric W. Biederman return ns; 264bc451f20SEric W. Biederman } 265bc451f20SEric W. Biederman 2668f4afc41SGreg Kroah-Hartman static struct kobj_type device_ktype = { 2671da177e4SLinus Torvalds .release = device_release, 2681da177e4SLinus Torvalds .sysfs_ops = &dev_sysfs_ops, 269bc451f20SEric W. Biederman .namespace = device_namespace, 2701da177e4SLinus Torvalds }; 2711da177e4SLinus Torvalds 2721da177e4SLinus Torvalds 273312c004dSKay Sievers static int dev_uevent_filter(struct kset *kset, struct kobject *kobj) 2741da177e4SLinus Torvalds { 2751da177e4SLinus Torvalds struct kobj_type *ktype = get_ktype(kobj); 2761da177e4SLinus Torvalds 2778f4afc41SGreg Kroah-Hartman if (ktype == &device_ktype) { 278b0d1f807SLars-Peter Clausen struct device *dev = kobj_to_dev(kobj); 2791da177e4SLinus Torvalds if (dev->bus) 2801da177e4SLinus Torvalds return 1; 28123681e47SGreg Kroah-Hartman if (dev->class) 28223681e47SGreg Kroah-Hartman return 1; 2831da177e4SLinus Torvalds } 2841da177e4SLinus Torvalds return 0; 2851da177e4SLinus Torvalds } 2861da177e4SLinus Torvalds 287312c004dSKay Sievers static const char *dev_uevent_name(struct kset *kset, struct kobject *kobj) 2881da177e4SLinus Torvalds { 289b0d1f807SLars-Peter Clausen struct device *dev = kobj_to_dev(kobj); 2901da177e4SLinus Torvalds 29123681e47SGreg Kroah-Hartman if (dev->bus) 2921da177e4SLinus Torvalds return dev->bus->name; 29323681e47SGreg Kroah-Hartman if (dev->class) 29423681e47SGreg Kroah-Hartman return dev->class->name; 29523681e47SGreg Kroah-Hartman return NULL; 2961da177e4SLinus Torvalds } 2971da177e4SLinus Torvalds 2987eff2e7aSKay Sievers static int dev_uevent(struct kset *kset, struct kobject *kobj, 2997eff2e7aSKay Sievers struct kobj_uevent_env *env) 3001da177e4SLinus Torvalds { 301b0d1f807SLars-Peter Clausen struct device *dev = kobj_to_dev(kobj); 3021da177e4SLinus Torvalds int retval = 0; 3031da177e4SLinus Torvalds 3046fcf53acSKay Sievers /* add device node properties if present */ 30523681e47SGreg Kroah-Hartman if (MAJOR(dev->devt)) { 3066fcf53acSKay Sievers const char *tmp; 3076fcf53acSKay Sievers const char *name; 3082c9ede55SAl Viro umode_t mode = 0; 3094e4098a3SGreg Kroah-Hartman kuid_t uid = GLOBAL_ROOT_UID; 3104e4098a3SGreg Kroah-Hartman kgid_t gid = GLOBAL_ROOT_GID; 3116fcf53acSKay Sievers 3127eff2e7aSKay Sievers add_uevent_var(env, "MAJOR=%u", MAJOR(dev->devt)); 3137eff2e7aSKay Sievers add_uevent_var(env, "MINOR=%u", MINOR(dev->devt)); 3143c2670e6SKay Sievers name = device_get_devnode(dev, &mode, &uid, &gid, &tmp); 3156fcf53acSKay Sievers if (name) { 3166fcf53acSKay Sievers add_uevent_var(env, "DEVNAME=%s", name); 317e454cea2SKay Sievers if (mode) 318e454cea2SKay Sievers add_uevent_var(env, "DEVMODE=%#o", mode & 0777); 3194e4098a3SGreg Kroah-Hartman if (!uid_eq(uid, GLOBAL_ROOT_UID)) 3204e4098a3SGreg Kroah-Hartman add_uevent_var(env, "DEVUID=%u", from_kuid(&init_user_ns, uid)); 3214e4098a3SGreg Kroah-Hartman if (!gid_eq(gid, GLOBAL_ROOT_GID)) 3224e4098a3SGreg Kroah-Hartman add_uevent_var(env, "DEVGID=%u", from_kgid(&init_user_ns, gid)); 3233c2670e6SKay Sievers kfree(tmp); 3246fcf53acSKay Sievers } 32523681e47SGreg Kroah-Hartman } 32623681e47SGreg Kroah-Hartman 327414264f9SKay Sievers if (dev->type && dev->type->name) 3287eff2e7aSKay Sievers add_uevent_var(env, "DEVTYPE=%s", dev->type->name); 329414264f9SKay Sievers 330239378f1SKay Sievers if (dev->driver) 3317eff2e7aSKay Sievers add_uevent_var(env, "DRIVER=%s", dev->driver->name); 332239378f1SKay Sievers 33307d57a32SGrant Likely /* Add common DT information about the device */ 33407d57a32SGrant Likely of_device_uevent(dev, env); 33507d57a32SGrant Likely 3361da177e4SLinus Torvalds /* have the bus specific function add its stuff */ 3377eff2e7aSKay Sievers if (dev->bus && dev->bus->uevent) { 3387eff2e7aSKay Sievers retval = dev->bus->uevent(dev, env); 339f9f852dfSKay Sievers if (retval) 3407dc72b28SGreg Kroah-Hartman pr_debug("device: '%s': %s: bus uevent() returned %d\n", 3411e0b2cf9SKay Sievers dev_name(dev), __func__, retval); 3421da177e4SLinus Torvalds } 3431da177e4SLinus Torvalds 3442620efefSGreg Kroah-Hartman /* have the class specific function add its stuff */ 3457eff2e7aSKay Sievers if (dev->class && dev->class->dev_uevent) { 3467eff2e7aSKay Sievers retval = dev->class->dev_uevent(dev, env); 347f9f852dfSKay Sievers if (retval) 3487dc72b28SGreg Kroah-Hartman pr_debug("device: '%s': %s: class uevent() " 3491e0b2cf9SKay Sievers "returned %d\n", dev_name(dev), 3502b3a302aSHarvey Harrison __func__, retval); 3512620efefSGreg Kroah-Hartman } 352f9f852dfSKay Sievers 353eef35c2dSStefan Weil /* have the device type specific function add its stuff */ 3547eff2e7aSKay Sievers if (dev->type && dev->type->uevent) { 3557eff2e7aSKay Sievers retval = dev->type->uevent(dev, env); 356f9f852dfSKay Sievers if (retval) 3577dc72b28SGreg Kroah-Hartman pr_debug("device: '%s': %s: dev_type uevent() " 3581e0b2cf9SKay Sievers "returned %d\n", dev_name(dev), 3592b3a302aSHarvey Harrison __func__, retval); 3602620efefSGreg Kroah-Hartman } 3612620efefSGreg Kroah-Hartman 3621da177e4SLinus Torvalds return retval; 3631da177e4SLinus Torvalds } 3641da177e4SLinus Torvalds 3659cd43611SEmese Revfy static const struct kset_uevent_ops device_uevent_ops = { 366312c004dSKay Sievers .filter = dev_uevent_filter, 367312c004dSKay Sievers .name = dev_uevent_name, 368312c004dSKay Sievers .uevent = dev_uevent, 3691da177e4SLinus Torvalds }; 3701da177e4SLinus Torvalds 371c5e064a6SGreg Kroah-Hartman static ssize_t uevent_show(struct device *dev, struct device_attribute *attr, 37216574dccSKay Sievers char *buf) 37316574dccSKay Sievers { 37416574dccSKay Sievers struct kobject *top_kobj; 37516574dccSKay Sievers struct kset *kset; 3767eff2e7aSKay Sievers struct kobj_uevent_env *env = NULL; 37716574dccSKay Sievers int i; 37816574dccSKay Sievers size_t count = 0; 37916574dccSKay Sievers int retval; 38016574dccSKay Sievers 38116574dccSKay Sievers /* search the kset, the device belongs to */ 38216574dccSKay Sievers top_kobj = &dev->kobj; 3835c5daf65SKay Sievers while (!top_kobj->kset && top_kobj->parent) 38416574dccSKay Sievers top_kobj = top_kobj->parent; 38516574dccSKay Sievers if (!top_kobj->kset) 38616574dccSKay Sievers goto out; 3875c5daf65SKay Sievers 38816574dccSKay Sievers kset = top_kobj->kset; 38916574dccSKay Sievers if (!kset->uevent_ops || !kset->uevent_ops->uevent) 39016574dccSKay Sievers goto out; 39116574dccSKay Sievers 39216574dccSKay Sievers /* respect filter */ 39316574dccSKay Sievers if (kset->uevent_ops && kset->uevent_ops->filter) 39416574dccSKay Sievers if (!kset->uevent_ops->filter(kset, &dev->kobj)) 39516574dccSKay Sievers goto out; 39616574dccSKay Sievers 3977eff2e7aSKay Sievers env = kzalloc(sizeof(struct kobj_uevent_env), GFP_KERNEL); 3987eff2e7aSKay Sievers if (!env) 399c7308c81SGreg Kroah-Hartman return -ENOMEM; 400c7308c81SGreg Kroah-Hartman 40116574dccSKay Sievers /* let the kset specific function add its keys */ 4027eff2e7aSKay Sievers retval = kset->uevent_ops->uevent(kset, &dev->kobj, env); 40316574dccSKay Sievers if (retval) 40416574dccSKay Sievers goto out; 40516574dccSKay Sievers 40616574dccSKay Sievers /* copy keys to file */ 4077eff2e7aSKay Sievers for (i = 0; i < env->envp_idx; i++) 4087eff2e7aSKay Sievers count += sprintf(&buf[count], "%s\n", env->envp[i]); 40916574dccSKay Sievers out: 4107eff2e7aSKay Sievers kfree(env); 41116574dccSKay Sievers return count; 41216574dccSKay Sievers } 41316574dccSKay Sievers 414c5e064a6SGreg Kroah-Hartman static ssize_t uevent_store(struct device *dev, struct device_attribute *attr, 415a7fd6706SKay Sievers const char *buf, size_t count) 416a7fd6706SKay Sievers { 41760a96a59SKay Sievers enum kobject_action action; 41860a96a59SKay Sievers 4193f5468c9SKay Sievers if (kobject_action_type(buf, count, &action) == 0) 42060a96a59SKay Sievers kobject_uevent(&dev->kobj, action); 4213f5468c9SKay Sievers else 4223f5468c9SKay Sievers dev_err(dev, "uevent: unknown action-string\n"); 423a7fd6706SKay Sievers return count; 424a7fd6706SKay Sievers } 425c5e064a6SGreg Kroah-Hartman static DEVICE_ATTR_RW(uevent); 426a7fd6706SKay Sievers 427c5e064a6SGreg Kroah-Hartman static ssize_t online_show(struct device *dev, struct device_attribute *attr, 4284f3549d7SRafael J. Wysocki char *buf) 4294f3549d7SRafael J. Wysocki { 4304f3549d7SRafael J. Wysocki bool val; 4314f3549d7SRafael J. Wysocki 4325e33bc41SRafael J. Wysocki device_lock(dev); 4334f3549d7SRafael J. Wysocki val = !dev->offline; 4345e33bc41SRafael J. Wysocki device_unlock(dev); 4354f3549d7SRafael J. Wysocki return sprintf(buf, "%u\n", val); 4364f3549d7SRafael J. Wysocki } 4374f3549d7SRafael J. Wysocki 438c5e064a6SGreg Kroah-Hartman static ssize_t online_store(struct device *dev, struct device_attribute *attr, 4394f3549d7SRafael J. Wysocki const char *buf, size_t count) 4404f3549d7SRafael J. Wysocki { 4414f3549d7SRafael J. Wysocki bool val; 4424f3549d7SRafael J. Wysocki int ret; 4434f3549d7SRafael J. Wysocki 4444f3549d7SRafael J. Wysocki ret = strtobool(buf, &val); 4454f3549d7SRafael J. Wysocki if (ret < 0) 4464f3549d7SRafael J. Wysocki return ret; 4474f3549d7SRafael J. Wysocki 4485e33bc41SRafael J. Wysocki ret = lock_device_hotplug_sysfs(); 4495e33bc41SRafael J. Wysocki if (ret) 4505e33bc41SRafael J. Wysocki return ret; 4515e33bc41SRafael J. Wysocki 4524f3549d7SRafael J. Wysocki ret = val ? device_online(dev) : device_offline(dev); 4534f3549d7SRafael J. Wysocki unlock_device_hotplug(); 4544f3549d7SRafael J. Wysocki return ret < 0 ? ret : count; 4554f3549d7SRafael J. Wysocki } 456c5e064a6SGreg Kroah-Hartman static DEVICE_ATTR_RW(online); 4574f3549d7SRafael J. Wysocki 458fa6fdb33SGreg Kroah-Hartman int device_add_groups(struct device *dev, const struct attribute_group **groups) 459621a1672SDmitry Torokhov { 4603e9b2baeSGreg Kroah-Hartman return sysfs_create_groups(&dev->kobj, groups); 461621a1672SDmitry Torokhov } 462621a1672SDmitry Torokhov 463fa6fdb33SGreg Kroah-Hartman void device_remove_groups(struct device *dev, 464a4dbd674SDavid Brownell const struct attribute_group **groups) 465621a1672SDmitry Torokhov { 4663e9b2baeSGreg Kroah-Hartman sysfs_remove_groups(&dev->kobj, groups); 467621a1672SDmitry Torokhov } 468de0ff00dSGreg Kroah-Hartman 4692620efefSGreg Kroah-Hartman static int device_add_attrs(struct device *dev) 4702620efefSGreg Kroah-Hartman { 4712620efefSGreg Kroah-Hartman struct class *class = dev->class; 472aed65af1SStephen Hemminger const struct device_type *type = dev->type; 473621a1672SDmitry Torokhov int error; 4742620efefSGreg Kroah-Hartman 475621a1672SDmitry Torokhov if (class) { 476d05a6f96SGreg Kroah-Hartman error = device_add_groups(dev, class->dev_groups); 4772620efefSGreg Kroah-Hartman if (error) 478621a1672SDmitry Torokhov return error; 479f9f852dfSKay Sievers } 480f9f852dfSKay Sievers 481621a1672SDmitry Torokhov if (type) { 482621a1672SDmitry Torokhov error = device_add_groups(dev, type->groups); 483f9f852dfSKay Sievers if (error) 484a6b01dedSGreg Kroah-Hartman goto err_remove_class_groups; 485f9f852dfSKay Sievers } 486621a1672SDmitry Torokhov 487621a1672SDmitry Torokhov error = device_add_groups(dev, dev->groups); 488f9f852dfSKay Sievers if (error) 489621a1672SDmitry Torokhov goto err_remove_type_groups; 490621a1672SDmitry Torokhov 4914f3549d7SRafael J. Wysocki if (device_supports_offline(dev) && !dev->offline_disabled) { 492c5e064a6SGreg Kroah-Hartman error = device_create_file(dev, &dev_attr_online); 4934f3549d7SRafael J. Wysocki if (error) 4944f3549d7SRafael J. Wysocki goto err_remove_type_groups; 4954f3549d7SRafael J. Wysocki } 4964f3549d7SRafael J. Wysocki 497621a1672SDmitry Torokhov return 0; 498621a1672SDmitry Torokhov 499621a1672SDmitry Torokhov err_remove_type_groups: 500621a1672SDmitry Torokhov if (type) 501621a1672SDmitry Torokhov device_remove_groups(dev, type->groups); 502d05a6f96SGreg Kroah-Hartman err_remove_class_groups: 503d05a6f96SGreg Kroah-Hartman if (class) 504d05a6f96SGreg Kroah-Hartman device_remove_groups(dev, class->dev_groups); 505f9f852dfSKay Sievers 5062620efefSGreg Kroah-Hartman return error; 5072620efefSGreg Kroah-Hartman } 5082620efefSGreg Kroah-Hartman 5092620efefSGreg Kroah-Hartman static void device_remove_attrs(struct device *dev) 5102620efefSGreg Kroah-Hartman { 5112620efefSGreg Kroah-Hartman struct class *class = dev->class; 512aed65af1SStephen Hemminger const struct device_type *type = dev->type; 5132620efefSGreg Kroah-Hartman 514c5e064a6SGreg Kroah-Hartman device_remove_file(dev, &dev_attr_online); 515621a1672SDmitry Torokhov device_remove_groups(dev, dev->groups); 516f9f852dfSKay Sievers 517621a1672SDmitry Torokhov if (type) 518621a1672SDmitry Torokhov device_remove_groups(dev, type->groups); 519621a1672SDmitry Torokhov 520a6b01dedSGreg Kroah-Hartman if (class) 521d05a6f96SGreg Kroah-Hartman device_remove_groups(dev, class->dev_groups); 522c97415a7SStefan Achatz } 5232620efefSGreg Kroah-Hartman 524c5e064a6SGreg Kroah-Hartman static ssize_t dev_show(struct device *dev, struct device_attribute *attr, 52523681e47SGreg Kroah-Hartman char *buf) 52623681e47SGreg Kroah-Hartman { 52723681e47SGreg Kroah-Hartman return print_dev_t(buf, dev->devt); 52823681e47SGreg Kroah-Hartman } 529c5e064a6SGreg Kroah-Hartman static DEVICE_ATTR_RO(dev); 530ad6a1e1cSTejun Heo 531ca22e56dSKay Sievers /* /sys/devices/ */ 532881c6cfdSGreg Kroah-Hartman struct kset *devices_kset; 5331da177e4SLinus Torvalds 5341da177e4SLinus Torvalds /** 5351da177e4SLinus Torvalds * device_create_file - create sysfs attribute file for device. 5361da177e4SLinus Torvalds * @dev: device. 5371da177e4SLinus Torvalds * @attr: device attribute descriptor. 5381da177e4SLinus Torvalds */ 53926579ab7SPhil Carmody int device_create_file(struct device *dev, 54026579ab7SPhil Carmody const struct device_attribute *attr) 5411da177e4SLinus Torvalds { 5421da177e4SLinus Torvalds int error = 0; 5438f46baaaSFelipe Balbi 5448f46baaaSFelipe Balbi if (dev) { 5458f46baaaSFelipe Balbi WARN(((attr->attr.mode & S_IWUGO) && !attr->store), 54697521978Sdyoung@redhat.com "Attribute %s: write permission without 'store'\n", 54797521978Sdyoung@redhat.com attr->attr.name); 5488f46baaaSFelipe Balbi WARN(((attr->attr.mode & S_IRUGO) && !attr->show), 54997521978Sdyoung@redhat.com "Attribute %s: read permission without 'show'\n", 55097521978Sdyoung@redhat.com attr->attr.name); 5511da177e4SLinus Torvalds error = sysfs_create_file(&dev->kobj, &attr->attr); 5528f46baaaSFelipe Balbi } 5538f46baaaSFelipe Balbi 5541da177e4SLinus Torvalds return error; 5551da177e4SLinus Torvalds } 55686df2687SDavid Graham White EXPORT_SYMBOL_GPL(device_create_file); 5571da177e4SLinus Torvalds 5581da177e4SLinus Torvalds /** 5591da177e4SLinus Torvalds * device_remove_file - remove sysfs attribute file. 5601da177e4SLinus Torvalds * @dev: device. 5611da177e4SLinus Torvalds * @attr: device attribute descriptor. 5621da177e4SLinus Torvalds */ 56326579ab7SPhil Carmody void device_remove_file(struct device *dev, 56426579ab7SPhil Carmody const struct device_attribute *attr) 5651da177e4SLinus Torvalds { 5660c98b19fSCornelia Huck if (dev) 5671da177e4SLinus Torvalds sysfs_remove_file(&dev->kobj, &attr->attr); 5681da177e4SLinus Torvalds } 56986df2687SDavid Graham White EXPORT_SYMBOL_GPL(device_remove_file); 5701da177e4SLinus Torvalds 5712589f188SGreg Kroah-Hartman /** 5722589f188SGreg Kroah-Hartman * device_create_bin_file - create sysfs binary attribute file for device. 5732589f188SGreg Kroah-Hartman * @dev: device. 5742589f188SGreg Kroah-Hartman * @attr: device binary attribute descriptor. 5752589f188SGreg Kroah-Hartman */ 57666ecb92bSPhil Carmody int device_create_bin_file(struct device *dev, 57766ecb92bSPhil Carmody const struct bin_attribute *attr) 5782589f188SGreg Kroah-Hartman { 5792589f188SGreg Kroah-Hartman int error = -EINVAL; 5802589f188SGreg Kroah-Hartman if (dev) 5812589f188SGreg Kroah-Hartman error = sysfs_create_bin_file(&dev->kobj, attr); 5822589f188SGreg Kroah-Hartman return error; 5832589f188SGreg Kroah-Hartman } 5842589f188SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(device_create_bin_file); 5852589f188SGreg Kroah-Hartman 5862589f188SGreg Kroah-Hartman /** 5872589f188SGreg Kroah-Hartman * device_remove_bin_file - remove sysfs binary attribute file 5882589f188SGreg Kroah-Hartman * @dev: device. 5892589f188SGreg Kroah-Hartman * @attr: device binary attribute descriptor. 5902589f188SGreg Kroah-Hartman */ 59166ecb92bSPhil Carmody void device_remove_bin_file(struct device *dev, 59266ecb92bSPhil Carmody const struct bin_attribute *attr) 5932589f188SGreg Kroah-Hartman { 5942589f188SGreg Kroah-Hartman if (dev) 5952589f188SGreg Kroah-Hartman sysfs_remove_bin_file(&dev->kobj, attr); 5962589f188SGreg Kroah-Hartman } 5972589f188SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(device_remove_bin_file); 5982589f188SGreg Kroah-Hartman 599d9a9cdfbSAlan Stern /** 600523ded71SAlan Stern * device_schedule_callback_owner - helper to schedule a callback for a device 601d9a9cdfbSAlan Stern * @dev: device. 602d9a9cdfbSAlan Stern * @func: callback function to invoke later. 603523ded71SAlan Stern * @owner: module owning the callback routine 604d9a9cdfbSAlan Stern * 605d9a9cdfbSAlan Stern * Attribute methods must not unregister themselves or their parent device 606d9a9cdfbSAlan Stern * (which would amount to the same thing). Attempts to do so will deadlock, 607d9a9cdfbSAlan Stern * since unregistration is mutually exclusive with driver callbacks. 608d9a9cdfbSAlan Stern * 609d9a9cdfbSAlan Stern * Instead methods can call this routine, which will attempt to allocate 610d9a9cdfbSAlan Stern * and schedule a workqueue request to call back @func with @dev as its 611d9a9cdfbSAlan Stern * argument in the workqueue's process context. @dev will be pinned until 612d9a9cdfbSAlan Stern * @func returns. 613d9a9cdfbSAlan Stern * 614523ded71SAlan Stern * This routine is usually called via the inline device_schedule_callback(), 615523ded71SAlan Stern * which automatically sets @owner to THIS_MODULE. 616523ded71SAlan Stern * 617d9a9cdfbSAlan Stern * Returns 0 if the request was submitted, -ENOMEM if storage could not 618523ded71SAlan Stern * be allocated, -ENODEV if a reference to @owner isn't available. 619d9a9cdfbSAlan Stern * 620d9a9cdfbSAlan Stern * NOTE: This routine won't work if CONFIG_SYSFS isn't set! It uses an 621d9a9cdfbSAlan Stern * underlying sysfs routine (since it is intended for use by attribute 622d9a9cdfbSAlan Stern * methods), and if sysfs isn't available you'll get nothing but -ENOSYS. 623d9a9cdfbSAlan Stern */ 624523ded71SAlan Stern int device_schedule_callback_owner(struct device *dev, 625523ded71SAlan Stern void (*func)(struct device *), struct module *owner) 626d9a9cdfbSAlan Stern { 627d9a9cdfbSAlan Stern return sysfs_schedule_callback(&dev->kobj, 628523ded71SAlan Stern (void (*)(void *)) func, dev, owner); 629d9a9cdfbSAlan Stern } 630523ded71SAlan Stern EXPORT_SYMBOL_GPL(device_schedule_callback_owner); 631d9a9cdfbSAlan Stern 63234bb61f9SJames Bottomley static void klist_children_get(struct klist_node *n) 63334bb61f9SJames Bottomley { 634f791b8c8SGreg Kroah-Hartman struct device_private *p = to_device_private_parent(n); 635f791b8c8SGreg Kroah-Hartman struct device *dev = p->device; 63634bb61f9SJames Bottomley 63734bb61f9SJames Bottomley get_device(dev); 63834bb61f9SJames Bottomley } 63934bb61f9SJames Bottomley 64034bb61f9SJames Bottomley static void klist_children_put(struct klist_node *n) 64134bb61f9SJames Bottomley { 642f791b8c8SGreg Kroah-Hartman struct device_private *p = to_device_private_parent(n); 643f791b8c8SGreg Kroah-Hartman struct device *dev = p->device; 64434bb61f9SJames Bottomley 64534bb61f9SJames Bottomley put_device(dev); 64634bb61f9SJames Bottomley } 64734bb61f9SJames Bottomley 6481da177e4SLinus Torvalds /** 6491da177e4SLinus Torvalds * device_initialize - init device structure. 6501da177e4SLinus Torvalds * @dev: device. 6511da177e4SLinus Torvalds * 6525739411aSCornelia Huck * This prepares the device for use by other layers by initializing 6535739411aSCornelia Huck * its fields. 6541da177e4SLinus Torvalds * It is the first half of device_register(), if called by 6555739411aSCornelia Huck * that function, though it can also be called separately, so one 6565739411aSCornelia Huck * may use @dev's fields. In particular, get_device()/put_device() 6575739411aSCornelia Huck * may be used for reference counting of @dev after calling this 6585739411aSCornelia Huck * function. 6595739411aSCornelia Huck * 660b10d5efdSAlan Stern * All fields in @dev must be initialized by the caller to 0, except 661b10d5efdSAlan Stern * for those explicitly set to some other value. The simplest 662b10d5efdSAlan Stern * approach is to use kzalloc() to allocate the structure containing 663b10d5efdSAlan Stern * @dev. 664b10d5efdSAlan Stern * 6655739411aSCornelia Huck * NOTE: Use put_device() to give up your reference instead of freeing 6665739411aSCornelia Huck * @dev directly once you have called this function. 6671da177e4SLinus Torvalds */ 6681da177e4SLinus Torvalds void device_initialize(struct device *dev) 6691da177e4SLinus Torvalds { 670881c6cfdSGreg Kroah-Hartman dev->kobj.kset = devices_kset; 671f9cb074bSGreg Kroah-Hartman kobject_init(&dev->kobj, &device_ktype); 6721da177e4SLinus Torvalds INIT_LIST_HEAD(&dev->dma_pools); 6733142788bSThomas Gleixner mutex_init(&dev->mutex); 6741704f47bSPeter Zijlstra lockdep_set_novalidate_class(&dev->mutex); 6759ac7849eSTejun Heo spin_lock_init(&dev->devres_lock); 6769ac7849eSTejun Heo INIT_LIST_HEAD(&dev->devres_head); 6773b98aeafSAlan Stern device_pm_init(dev); 67887348136SChristoph Hellwig set_dev_node(dev, -1); 6791da177e4SLinus Torvalds } 68086df2687SDavid Graham White EXPORT_SYMBOL_GPL(device_initialize); 6811da177e4SLinus Torvalds 682d73ce004STejun Heo struct kobject *virtual_device_parent(struct device *dev) 683f0ee61a6SGreg Kroah-Hartman { 684f0ee61a6SGreg Kroah-Hartman static struct kobject *virtual_dir = NULL; 685f0ee61a6SGreg Kroah-Hartman 686f0ee61a6SGreg Kroah-Hartman if (!virtual_dir) 6874ff6abffSGreg Kroah-Hartman virtual_dir = kobject_create_and_add("virtual", 688881c6cfdSGreg Kroah-Hartman &devices_kset->kobj); 689f0ee61a6SGreg Kroah-Hartman 69086406245SKay Sievers return virtual_dir; 691f0ee61a6SGreg Kroah-Hartman } 692f0ee61a6SGreg Kroah-Hartman 693bc451f20SEric W. Biederman struct class_dir { 694bc451f20SEric W. Biederman struct kobject kobj; 695bc451f20SEric W. Biederman struct class *class; 696bc451f20SEric W. Biederman }; 697bc451f20SEric W. Biederman 698bc451f20SEric W. Biederman #define to_class_dir(obj) container_of(obj, struct class_dir, kobj) 699bc451f20SEric W. Biederman 700bc451f20SEric W. Biederman static void class_dir_release(struct kobject *kobj) 701bc451f20SEric W. Biederman { 702bc451f20SEric W. Biederman struct class_dir *dir = to_class_dir(kobj); 703bc451f20SEric W. Biederman kfree(dir); 704bc451f20SEric W. Biederman } 705bc451f20SEric W. Biederman 706bc451f20SEric W. Biederman static const 707bc451f20SEric W. Biederman struct kobj_ns_type_operations *class_dir_child_ns_type(struct kobject *kobj) 708bc451f20SEric W. Biederman { 709bc451f20SEric W. Biederman struct class_dir *dir = to_class_dir(kobj); 710bc451f20SEric W. Biederman return dir->class->ns_type; 711bc451f20SEric W. Biederman } 712bc451f20SEric W. Biederman 713bc451f20SEric W. Biederman static struct kobj_type class_dir_ktype = { 714bc451f20SEric W. Biederman .release = class_dir_release, 715bc451f20SEric W. Biederman .sysfs_ops = &kobj_sysfs_ops, 716bc451f20SEric W. Biederman .child_ns_type = class_dir_child_ns_type 717bc451f20SEric W. Biederman }; 718bc451f20SEric W. Biederman 719bc451f20SEric W. Biederman static struct kobject * 720bc451f20SEric W. Biederman class_dir_create_and_add(struct class *class, struct kobject *parent_kobj) 721bc451f20SEric W. Biederman { 722bc451f20SEric W. Biederman struct class_dir *dir; 723bc451f20SEric W. Biederman int retval; 724bc451f20SEric W. Biederman 725bc451f20SEric W. Biederman dir = kzalloc(sizeof(*dir), GFP_KERNEL); 726bc451f20SEric W. Biederman if (!dir) 727bc451f20SEric W. Biederman return NULL; 728bc451f20SEric W. Biederman 729bc451f20SEric W. Biederman dir->class = class; 730bc451f20SEric W. Biederman kobject_init(&dir->kobj, &class_dir_ktype); 731bc451f20SEric W. Biederman 7326b6e39a6SKay Sievers dir->kobj.kset = &class->p->glue_dirs; 733bc451f20SEric W. Biederman 734bc451f20SEric W. Biederman retval = kobject_add(&dir->kobj, parent_kobj, "%s", class->name); 735bc451f20SEric W. Biederman if (retval < 0) { 736bc451f20SEric W. Biederman kobject_put(&dir->kobj); 737bc451f20SEric W. Biederman return NULL; 738bc451f20SEric W. Biederman } 739bc451f20SEric W. Biederman return &dir->kobj; 740bc451f20SEric W. Biederman } 741bc451f20SEric W. Biederman 742bc451f20SEric W. Biederman 743c744aeaeSCornelia Huck static struct kobject *get_device_parent(struct device *dev, 744c744aeaeSCornelia Huck struct device *parent) 74540fa5422SGreg Kroah-Hartman { 74686406245SKay Sievers if (dev->class) { 74777d3d7c1STejun Heo static DEFINE_MUTEX(gdp_mutex); 74886406245SKay Sievers struct kobject *kobj = NULL; 74986406245SKay Sievers struct kobject *parent_kobj; 75086406245SKay Sievers struct kobject *k; 75186406245SKay Sievers 752ead454feSRandy Dunlap #ifdef CONFIG_BLOCK 75339aba963SKay Sievers /* block disks show up in /sys/block */ 754e52eec13SAndi Kleen if (sysfs_deprecated && dev->class == &block_class) { 75539aba963SKay Sievers if (parent && parent->class == &block_class) 75639aba963SKay Sievers return &parent->kobj; 7576b6e39a6SKay Sievers return &block_class.p->subsys.kobj; 75839aba963SKay Sievers } 759ead454feSRandy Dunlap #endif 760e52eec13SAndi Kleen 76186406245SKay Sievers /* 76286406245SKay Sievers * If we have no parent, we live in "virtual". 7630f4dafc0SKay Sievers * Class-devices with a non class-device as parent, live 7640f4dafc0SKay Sievers * in a "glue" directory to prevent namespace collisions. 76586406245SKay Sievers */ 76686406245SKay Sievers if (parent == NULL) 76786406245SKay Sievers parent_kobj = virtual_device_parent(dev); 76824b1442dSEric W. Biederman else if (parent->class && !dev->class->ns_type) 76986406245SKay Sievers return &parent->kobj; 77086406245SKay Sievers else 77186406245SKay Sievers parent_kobj = &parent->kobj; 77286406245SKay Sievers 77377d3d7c1STejun Heo mutex_lock(&gdp_mutex); 77477d3d7c1STejun Heo 77586406245SKay Sievers /* find our class-directory at the parent and reference it */ 7766b6e39a6SKay Sievers spin_lock(&dev->class->p->glue_dirs.list_lock); 7776b6e39a6SKay Sievers list_for_each_entry(k, &dev->class->p->glue_dirs.list, entry) 77886406245SKay Sievers if (k->parent == parent_kobj) { 77986406245SKay Sievers kobj = kobject_get(k); 78086406245SKay Sievers break; 78186406245SKay Sievers } 7826b6e39a6SKay Sievers spin_unlock(&dev->class->p->glue_dirs.list_lock); 78377d3d7c1STejun Heo if (kobj) { 78477d3d7c1STejun Heo mutex_unlock(&gdp_mutex); 78586406245SKay Sievers return kobj; 78677d3d7c1STejun Heo } 78786406245SKay Sievers 78886406245SKay Sievers /* or create a new class-directory at the parent device */ 789bc451f20SEric W. Biederman k = class_dir_create_and_add(dev->class, parent_kobj); 7900f4dafc0SKay Sievers /* do not emit an uevent for this simple "glue" directory */ 79177d3d7c1STejun Heo mutex_unlock(&gdp_mutex); 79243968d2fSGreg Kroah-Hartman return k; 79386406245SKay Sievers } 79486406245SKay Sievers 795ca22e56dSKay Sievers /* subsystems can specify a default root directory for their devices */ 796ca22e56dSKay Sievers if (!parent && dev->bus && dev->bus->dev_root) 797ca22e56dSKay Sievers return &dev->bus->dev_root->kobj; 798ca22e56dSKay Sievers 79986406245SKay Sievers if (parent) 800c744aeaeSCornelia Huck return &parent->kobj; 801c744aeaeSCornelia Huck return NULL; 802c744aeaeSCornelia Huck } 803da231fd5SKay Sievers 80463b6971aSCornelia Huck static void cleanup_glue_dir(struct device *dev, struct kobject *glue_dir) 805da231fd5SKay Sievers { 8060f4dafc0SKay Sievers /* see if we live in a "glue" directory */ 807c1fe539aSCornelia Huck if (!glue_dir || !dev->class || 8086b6e39a6SKay Sievers glue_dir->kset != &dev->class->p->glue_dirs) 809da231fd5SKay Sievers return; 810da231fd5SKay Sievers 8110f4dafc0SKay Sievers kobject_put(glue_dir); 812da231fd5SKay Sievers } 81363b6971aSCornelia Huck 81463b6971aSCornelia Huck static void cleanup_device_parent(struct device *dev) 81563b6971aSCornelia Huck { 81663b6971aSCornelia Huck cleanup_glue_dir(dev, dev->kobj.parent); 81763b6971aSCornelia Huck } 81886406245SKay Sievers 8192ee97cafSCornelia Huck static int device_add_class_symlinks(struct device *dev) 8202ee97cafSCornelia Huck { 8212ee97cafSCornelia Huck int error; 8222ee97cafSCornelia Huck 8232ee97cafSCornelia Huck if (!dev->class) 8242ee97cafSCornelia Huck return 0; 825da231fd5SKay Sievers 8261fbfee6cSGreg Kroah-Hartman error = sysfs_create_link(&dev->kobj, 8276b6e39a6SKay Sievers &dev->class->p->subsys.kobj, 8282ee97cafSCornelia Huck "subsystem"); 8292ee97cafSCornelia Huck if (error) 8302ee97cafSCornelia Huck goto out; 831da231fd5SKay Sievers 8324e886c29SGreg Kroah-Hartman if (dev->parent && device_is_not_partition(dev)) { 8334f01a757SDmitry Torokhov error = sysfs_create_link(&dev->kobj, &dev->parent->kobj, 8344f01a757SDmitry Torokhov "device"); 8354f01a757SDmitry Torokhov if (error) 83639aba963SKay Sievers goto out_subsys; 8372ee97cafSCornelia Huck } 83839aba963SKay Sievers 839ead454feSRandy Dunlap #ifdef CONFIG_BLOCK 84039aba963SKay Sievers /* /sys/block has directories and does not need symlinks */ 841e52eec13SAndi Kleen if (sysfs_deprecated && dev->class == &block_class) 84239aba963SKay Sievers return 0; 843ead454feSRandy Dunlap #endif 84439aba963SKay Sievers 84539aba963SKay Sievers /* link in the class directory pointing to the device */ 8466b6e39a6SKay Sievers error = sysfs_create_link(&dev->class->p->subsys.kobj, 84739aba963SKay Sievers &dev->kobj, dev_name(dev)); 84839aba963SKay Sievers if (error) 84939aba963SKay Sievers goto out_device; 85039aba963SKay Sievers 8512ee97cafSCornelia Huck return 0; 8522ee97cafSCornelia Huck 85339aba963SKay Sievers out_device: 85439aba963SKay Sievers sysfs_remove_link(&dev->kobj, "device"); 855da231fd5SKay Sievers 8562ee97cafSCornelia Huck out_subsys: 8572ee97cafSCornelia Huck sysfs_remove_link(&dev->kobj, "subsystem"); 8582ee97cafSCornelia Huck out: 8592ee97cafSCornelia Huck return error; 8602ee97cafSCornelia Huck } 8612ee97cafSCornelia Huck 8622ee97cafSCornelia Huck static void device_remove_class_symlinks(struct device *dev) 8632ee97cafSCornelia Huck { 8642ee97cafSCornelia Huck if (!dev->class) 8652ee97cafSCornelia Huck return; 866da231fd5SKay Sievers 8674e886c29SGreg Kroah-Hartman if (dev->parent && device_is_not_partition(dev)) 868da231fd5SKay Sievers sysfs_remove_link(&dev->kobj, "device"); 8692ee97cafSCornelia Huck sysfs_remove_link(&dev->kobj, "subsystem"); 870ead454feSRandy Dunlap #ifdef CONFIG_BLOCK 871e52eec13SAndi Kleen if (sysfs_deprecated && dev->class == &block_class) 87239aba963SKay Sievers return; 873ead454feSRandy Dunlap #endif 8746b6e39a6SKay Sievers sysfs_delete_link(&dev->class->p->subsys.kobj, &dev->kobj, dev_name(dev)); 8752ee97cafSCornelia Huck } 8762ee97cafSCornelia Huck 8771da177e4SLinus Torvalds /** 878413c239fSStephen Rothwell * dev_set_name - set a device name 879413c239fSStephen Rothwell * @dev: device 88046232366SRandy Dunlap * @fmt: format string for the device's name 881413c239fSStephen Rothwell */ 882413c239fSStephen Rothwell int dev_set_name(struct device *dev, const char *fmt, ...) 883413c239fSStephen Rothwell { 884413c239fSStephen Rothwell va_list vargs; 8851fa5ae85SKay Sievers int err; 886413c239fSStephen Rothwell 887413c239fSStephen Rothwell va_start(vargs, fmt); 8881fa5ae85SKay Sievers err = kobject_set_name_vargs(&dev->kobj, fmt, vargs); 889413c239fSStephen Rothwell va_end(vargs); 8901fa5ae85SKay Sievers return err; 891413c239fSStephen Rothwell } 892413c239fSStephen Rothwell EXPORT_SYMBOL_GPL(dev_set_name); 893413c239fSStephen Rothwell 894413c239fSStephen Rothwell /** 895e105b8bfSDan Williams * device_to_dev_kobj - select a /sys/dev/ directory for the device 896e105b8bfSDan Williams * @dev: device 897e105b8bfSDan Williams * 898e105b8bfSDan Williams * By default we select char/ for new entries. Setting class->dev_obj 899e105b8bfSDan Williams * to NULL prevents an entry from being created. class->dev_kobj must 900e105b8bfSDan Williams * be set (or cleared) before any devices are registered to the class 901e105b8bfSDan Williams * otherwise device_create_sys_dev_entry() and 9020d4e293cSPeter Korsgaard * device_remove_sys_dev_entry() will disagree about the presence of 9030d4e293cSPeter Korsgaard * the link. 904e105b8bfSDan Williams */ 905e105b8bfSDan Williams static struct kobject *device_to_dev_kobj(struct device *dev) 906e105b8bfSDan Williams { 907e105b8bfSDan Williams struct kobject *kobj; 908e105b8bfSDan Williams 909e105b8bfSDan Williams if (dev->class) 910e105b8bfSDan Williams kobj = dev->class->dev_kobj; 911e105b8bfSDan Williams else 912e105b8bfSDan Williams kobj = sysfs_dev_char_kobj; 913e105b8bfSDan Williams 914e105b8bfSDan Williams return kobj; 915e105b8bfSDan Williams } 916e105b8bfSDan Williams 917e105b8bfSDan Williams static int device_create_sys_dev_entry(struct device *dev) 918e105b8bfSDan Williams { 919e105b8bfSDan Williams struct kobject *kobj = device_to_dev_kobj(dev); 920e105b8bfSDan Williams int error = 0; 921e105b8bfSDan Williams char devt_str[15]; 922e105b8bfSDan Williams 923e105b8bfSDan Williams if (kobj) { 924e105b8bfSDan Williams format_dev_t(devt_str, dev->devt); 925e105b8bfSDan Williams error = sysfs_create_link(kobj, &dev->kobj, devt_str); 926e105b8bfSDan Williams } 927e105b8bfSDan Williams 928e105b8bfSDan Williams return error; 929e105b8bfSDan Williams } 930e105b8bfSDan Williams 931e105b8bfSDan Williams static void device_remove_sys_dev_entry(struct device *dev) 932e105b8bfSDan Williams { 933e105b8bfSDan Williams struct kobject *kobj = device_to_dev_kobj(dev); 934e105b8bfSDan Williams char devt_str[15]; 935e105b8bfSDan Williams 936e105b8bfSDan Williams if (kobj) { 937e105b8bfSDan Williams format_dev_t(devt_str, dev->devt); 938e105b8bfSDan Williams sysfs_remove_link(kobj, devt_str); 939e105b8bfSDan Williams } 940e105b8bfSDan Williams } 941e105b8bfSDan Williams 942b4028437SGreg Kroah-Hartman int device_private_init(struct device *dev) 943b4028437SGreg Kroah-Hartman { 944b4028437SGreg Kroah-Hartman dev->p = kzalloc(sizeof(*dev->p), GFP_KERNEL); 945b4028437SGreg Kroah-Hartman if (!dev->p) 946b4028437SGreg Kroah-Hartman return -ENOMEM; 947b4028437SGreg Kroah-Hartman dev->p->device = dev; 948b4028437SGreg Kroah-Hartman klist_init(&dev->p->klist_children, klist_children_get, 949b4028437SGreg Kroah-Hartman klist_children_put); 950ef8a3fd6SGreg Kroah-Hartman INIT_LIST_HEAD(&dev->p->deferred_probe); 951b4028437SGreg Kroah-Hartman return 0; 952b4028437SGreg Kroah-Hartman } 953b4028437SGreg Kroah-Hartman 954e105b8bfSDan Williams /** 9551da177e4SLinus Torvalds * device_add - add device to device hierarchy. 9561da177e4SLinus Torvalds * @dev: device. 9571da177e4SLinus Torvalds * 9581da177e4SLinus Torvalds * This is part 2 of device_register(), though may be called 9591da177e4SLinus Torvalds * separately _iff_ device_initialize() has been called separately. 9601da177e4SLinus Torvalds * 9615739411aSCornelia Huck * This adds @dev to the kobject hierarchy via kobject_add(), adds it 9621da177e4SLinus Torvalds * to the global and sibling lists for the device, then 9631da177e4SLinus Torvalds * adds it to the other relevant subsystems of the driver model. 9645739411aSCornelia Huck * 965b10d5efdSAlan Stern * Do not call this routine or device_register() more than once for 966b10d5efdSAlan Stern * any device structure. The driver model core is not designed to work 967b10d5efdSAlan Stern * with devices that get unregistered and then spring back to life. 968b10d5efdSAlan Stern * (Among other things, it's very hard to guarantee that all references 969b10d5efdSAlan Stern * to the previous incarnation of @dev have been dropped.) Allocate 970b10d5efdSAlan Stern * and register a fresh new struct device instead. 971b10d5efdSAlan Stern * 9725739411aSCornelia Huck * NOTE: _Never_ directly free @dev after calling this function, even 9735739411aSCornelia Huck * if it returned an error! Always use put_device() to give up your 9745739411aSCornelia Huck * reference instead. 9751da177e4SLinus Torvalds */ 9761da177e4SLinus Torvalds int device_add(struct device *dev) 9771da177e4SLinus Torvalds { 9781da177e4SLinus Torvalds struct device *parent = NULL; 979ca22e56dSKay Sievers struct kobject *kobj; 980c47ed219SGreg Kroah-Hartman struct class_interface *class_intf; 981c906a48aSGreg Kroah-Hartman int error = -EINVAL; 982775b64d2SRafael J. Wysocki 9831da177e4SLinus Torvalds dev = get_device(dev); 984c906a48aSGreg Kroah-Hartman if (!dev) 985c906a48aSGreg Kroah-Hartman goto done; 986c906a48aSGreg Kroah-Hartman 987fb069a5dSGreg Kroah-Hartman if (!dev->p) { 988b4028437SGreg Kroah-Hartman error = device_private_init(dev); 989b4028437SGreg Kroah-Hartman if (error) 990fb069a5dSGreg Kroah-Hartman goto done; 991fb069a5dSGreg Kroah-Hartman } 992fb069a5dSGreg Kroah-Hartman 9931fa5ae85SKay Sievers /* 9941fa5ae85SKay Sievers * for statically allocated devices, which should all be converted 9951fa5ae85SKay Sievers * some day, we need to initialize the name. We prevent reading back 9961fa5ae85SKay Sievers * the name, and force the use of dev_name() 9971fa5ae85SKay Sievers */ 9981fa5ae85SKay Sievers if (dev->init_name) { 999acc0e90fSGreg Kroah-Hartman dev_set_name(dev, "%s", dev->init_name); 10001fa5ae85SKay Sievers dev->init_name = NULL; 10011fa5ae85SKay Sievers } 1002c906a48aSGreg Kroah-Hartman 1003ca22e56dSKay Sievers /* subsystems can specify simple device enumeration */ 1004ca22e56dSKay Sievers if (!dev_name(dev) && dev->bus && dev->bus->dev_name) 1005ca22e56dSKay Sievers dev_set_name(dev, "%s%u", dev->bus->dev_name, dev->id); 1006ca22e56dSKay Sievers 1007e6309e75SThomas Gleixner if (!dev_name(dev)) { 1008e6309e75SThomas Gleixner error = -EINVAL; 10095c8563d7SKay Sievers goto name_error; 1010e6309e75SThomas Gleixner } 10111da177e4SLinus Torvalds 10121e0b2cf9SKay Sievers pr_debug("device: '%s': %s\n", dev_name(dev), __func__); 1013c205ef48SGreg Kroah-Hartman 10141da177e4SLinus Torvalds parent = get_device(dev->parent); 1015ca22e56dSKay Sievers kobj = get_device_parent(dev, parent); 1016ca22e56dSKay Sievers if (kobj) 1017ca22e56dSKay Sievers dev->kobj.parent = kobj; 10181da177e4SLinus Torvalds 10190d358f22SYinghai Lu /* use parent numa_node */ 10200d358f22SYinghai Lu if (parent) 10210d358f22SYinghai Lu set_dev_node(dev, dev_to_node(parent)); 10220d358f22SYinghai Lu 10231da177e4SLinus Torvalds /* first, register with generic layer. */ 10248a577ffcSKay Sievers /* we require the name to be set before, and pass NULL */ 10258a577ffcSKay Sievers error = kobject_add(&dev->kobj, dev->kobj.parent, NULL); 102640fa5422SGreg Kroah-Hartman if (error) 10271da177e4SLinus Torvalds goto Error; 1028a7fd6706SKay Sievers 102937022644SBrian Walsh /* notify platform of device entry */ 103037022644SBrian Walsh if (platform_notify) 103137022644SBrian Walsh platform_notify(dev); 103237022644SBrian Walsh 1033c5e064a6SGreg Kroah-Hartman error = device_create_file(dev, &dev_attr_uevent); 1034a306eea4SCornelia Huck if (error) 1035a306eea4SCornelia Huck goto attrError; 1036a7fd6706SKay Sievers 103723681e47SGreg Kroah-Hartman if (MAJOR(dev->devt)) { 1038c5e064a6SGreg Kroah-Hartman error = device_create_file(dev, &dev_attr_dev); 1039ad6a1e1cSTejun Heo if (error) 1040a306eea4SCornelia Huck goto ueventattrError; 1041e105b8bfSDan Williams 1042e105b8bfSDan Williams error = device_create_sys_dev_entry(dev); 1043e105b8bfSDan Williams if (error) 1044e105b8bfSDan Williams goto devtattrError; 10452b2af54aSKay Sievers 10462b2af54aSKay Sievers devtmpfs_create_node(dev); 104723681e47SGreg Kroah-Hartman } 104823681e47SGreg Kroah-Hartman 10492ee97cafSCornelia Huck error = device_add_class_symlinks(dev); 10502ee97cafSCornelia Huck if (error) 10512ee97cafSCornelia Huck goto SymlinkError; 1052dc0afa83SCornelia Huck error = device_add_attrs(dev); 1053dc0afa83SCornelia Huck if (error) 10542620efefSGreg Kroah-Hartman goto AttrsError; 1055dc0afa83SCornelia Huck error = bus_add_device(dev); 1056dc0afa83SCornelia Huck if (error) 10571da177e4SLinus Torvalds goto BusError; 10583b98aeafSAlan Stern error = dpm_sysfs_add(dev); 105957eee3d2SRafael J. Wysocki if (error) 10603b98aeafSAlan Stern goto DPMError; 10613b98aeafSAlan Stern device_pm_add(dev); 1062ec0676eeSAlan Stern 1063ec0676eeSAlan Stern /* Notify clients of device addition. This call must come 1064268863f4Smajianpeng * after dpm_sysfs_add() and before kobject_uevent(). 1065ec0676eeSAlan Stern */ 1066ec0676eeSAlan Stern if (dev->bus) 1067ec0676eeSAlan Stern blocking_notifier_call_chain(&dev->bus->p->bus_notifier, 1068ec0676eeSAlan Stern BUS_NOTIFY_ADD_DEVICE, dev); 1069ec0676eeSAlan Stern 107053877d06SKay Sievers kobject_uevent(&dev->kobj, KOBJ_ADD); 10712023c610SAlan Stern bus_probe_device(dev); 10721da177e4SLinus Torvalds if (parent) 1073f791b8c8SGreg Kroah-Hartman klist_add_tail(&dev->p->knode_parent, 1074f791b8c8SGreg Kroah-Hartman &parent->p->klist_children); 10751da177e4SLinus Torvalds 10765d9fd169SGreg Kroah-Hartman if (dev->class) { 1077ca22e56dSKay Sievers mutex_lock(&dev->class->p->mutex); 1078c47ed219SGreg Kroah-Hartman /* tie the class to the device */ 10795a3ceb86STejun Heo klist_add_tail(&dev->knode_class, 10806b6e39a6SKay Sievers &dev->class->p->klist_devices); 1081c47ed219SGreg Kroah-Hartman 1082c47ed219SGreg Kroah-Hartman /* notify any interfaces that the device is here */ 1083184f1f77SGreg Kroah-Hartman list_for_each_entry(class_intf, 1084ca22e56dSKay Sievers &dev->class->p->interfaces, node) 1085c47ed219SGreg Kroah-Hartman if (class_intf->add_dev) 1086c47ed219SGreg Kroah-Hartman class_intf->add_dev(dev, class_intf); 1087ca22e56dSKay Sievers mutex_unlock(&dev->class->p->mutex); 10885d9fd169SGreg Kroah-Hartman } 1089c906a48aSGreg Kroah-Hartman done: 10901da177e4SLinus Torvalds put_device(dev); 10911da177e4SLinus Torvalds return error; 10923b98aeafSAlan Stern DPMError: 109357eee3d2SRafael J. Wysocki bus_remove_device(dev); 109457eee3d2SRafael J. Wysocki BusError: 10952620efefSGreg Kroah-Hartman device_remove_attrs(dev); 10962620efefSGreg Kroah-Hartman AttrsError: 10972ee97cafSCornelia Huck device_remove_class_symlinks(dev); 10982ee97cafSCornelia Huck SymlinkError: 1099ad6a1e1cSTejun Heo if (MAJOR(dev->devt)) 1100ad72956dSKay Sievers devtmpfs_delete_node(dev); 1101ad72956dSKay Sievers if (MAJOR(dev->devt)) 1102e105b8bfSDan Williams device_remove_sys_dev_entry(dev); 1103e105b8bfSDan Williams devtattrError: 1104e105b8bfSDan Williams if (MAJOR(dev->devt)) 1105c5e064a6SGreg Kroah-Hartman device_remove_file(dev, &dev_attr_dev); 1106a306eea4SCornelia Huck ueventattrError: 1107c5e064a6SGreg Kroah-Hartman device_remove_file(dev, &dev_attr_uevent); 110823681e47SGreg Kroah-Hartman attrError: 1109312c004dSKay Sievers kobject_uevent(&dev->kobj, KOBJ_REMOVE); 11101da177e4SLinus Torvalds kobject_del(&dev->kobj); 11111da177e4SLinus Torvalds Error: 111263b6971aSCornelia Huck cleanup_device_parent(dev); 11131da177e4SLinus Torvalds if (parent) 11141da177e4SLinus Torvalds put_device(parent); 11155c8563d7SKay Sievers name_error: 11165c8563d7SKay Sievers kfree(dev->p); 11175c8563d7SKay Sievers dev->p = NULL; 1118c906a48aSGreg Kroah-Hartman goto done; 11191da177e4SLinus Torvalds } 112086df2687SDavid Graham White EXPORT_SYMBOL_GPL(device_add); 11211da177e4SLinus Torvalds 11221da177e4SLinus Torvalds /** 11231da177e4SLinus Torvalds * device_register - register a device with the system. 11241da177e4SLinus Torvalds * @dev: pointer to the device structure 11251da177e4SLinus Torvalds * 11261da177e4SLinus Torvalds * This happens in two clean steps - initialize the device 11271da177e4SLinus Torvalds * and add it to the system. The two steps can be called 11281da177e4SLinus Torvalds * separately, but this is the easiest and most common. 11291da177e4SLinus Torvalds * I.e. you should only call the two helpers separately if 11301da177e4SLinus Torvalds * have a clearly defined need to use and refcount the device 11311da177e4SLinus Torvalds * before it is added to the hierarchy. 11325739411aSCornelia Huck * 1133b10d5efdSAlan Stern * For more information, see the kerneldoc for device_initialize() 1134b10d5efdSAlan Stern * and device_add(). 1135b10d5efdSAlan Stern * 11365739411aSCornelia Huck * NOTE: _Never_ directly free @dev after calling this function, even 11375739411aSCornelia Huck * if it returned an error! Always use put_device() to give up the 11385739411aSCornelia Huck * reference initialized in this function instead. 11391da177e4SLinus Torvalds */ 11401da177e4SLinus Torvalds int device_register(struct device *dev) 11411da177e4SLinus Torvalds { 11421da177e4SLinus Torvalds device_initialize(dev); 11431da177e4SLinus Torvalds return device_add(dev); 11441da177e4SLinus Torvalds } 114586df2687SDavid Graham White EXPORT_SYMBOL_GPL(device_register); 11461da177e4SLinus Torvalds 11471da177e4SLinus Torvalds /** 11481da177e4SLinus Torvalds * get_device - increment reference count for device. 11491da177e4SLinus Torvalds * @dev: device. 11501da177e4SLinus Torvalds * 11511da177e4SLinus Torvalds * This simply forwards the call to kobject_get(), though 11521da177e4SLinus Torvalds * we do take care to provide for the case that we get a NULL 11531da177e4SLinus Torvalds * pointer passed in. 11541da177e4SLinus Torvalds */ 11551da177e4SLinus Torvalds struct device *get_device(struct device *dev) 11561da177e4SLinus Torvalds { 1157b0d1f807SLars-Peter Clausen return dev ? kobj_to_dev(kobject_get(&dev->kobj)) : NULL; 11581da177e4SLinus Torvalds } 115986df2687SDavid Graham White EXPORT_SYMBOL_GPL(get_device); 11601da177e4SLinus Torvalds 11611da177e4SLinus Torvalds /** 11621da177e4SLinus Torvalds * put_device - decrement reference count. 11631da177e4SLinus Torvalds * @dev: device in question. 11641da177e4SLinus Torvalds */ 11651da177e4SLinus Torvalds void put_device(struct device *dev) 11661da177e4SLinus Torvalds { 1167edfaa7c3SKay Sievers /* might_sleep(); */ 11681da177e4SLinus Torvalds if (dev) 11691da177e4SLinus Torvalds kobject_put(&dev->kobj); 11701da177e4SLinus Torvalds } 117186df2687SDavid Graham White EXPORT_SYMBOL_GPL(put_device); 11721da177e4SLinus Torvalds 11731da177e4SLinus Torvalds /** 11741da177e4SLinus Torvalds * device_del - delete device from system. 11751da177e4SLinus Torvalds * @dev: device. 11761da177e4SLinus Torvalds * 11771da177e4SLinus Torvalds * This is the first part of the device unregistration 11781da177e4SLinus Torvalds * sequence. This removes the device from the lists we control 11791da177e4SLinus Torvalds * from here, has it removed from the other driver model 11801da177e4SLinus Torvalds * subsystems it was added to in device_add(), and removes it 11811da177e4SLinus Torvalds * from the kobject hierarchy. 11821da177e4SLinus Torvalds * 11831da177e4SLinus Torvalds * NOTE: this should be called manually _iff_ device_add() was 11841da177e4SLinus Torvalds * also called manually. 11851da177e4SLinus Torvalds */ 11861da177e4SLinus Torvalds void device_del(struct device *dev) 11871da177e4SLinus Torvalds { 11881da177e4SLinus Torvalds struct device *parent = dev->parent; 1189c47ed219SGreg Kroah-Hartman struct class_interface *class_intf; 11901da177e4SLinus Torvalds 1191ec0676eeSAlan Stern /* Notify clients of device removal. This call must come 1192ec0676eeSAlan Stern * before dpm_sysfs_remove(). 1193ec0676eeSAlan Stern */ 1194ec0676eeSAlan Stern if (dev->bus) 1195ec0676eeSAlan Stern blocking_notifier_call_chain(&dev->bus->p->bus_notifier, 1196ec0676eeSAlan Stern BUS_NOTIFY_DEL_DEVICE, dev); 11973b98aeafSAlan Stern dpm_sysfs_remove(dev); 11981da177e4SLinus Torvalds if (parent) 1199f791b8c8SGreg Kroah-Hartman klist_del(&dev->p->knode_parent); 1200e105b8bfSDan Williams if (MAJOR(dev->devt)) { 12012b2af54aSKay Sievers devtmpfs_delete_node(dev); 1202e105b8bfSDan Williams device_remove_sys_dev_entry(dev); 1203c5e064a6SGreg Kroah-Hartman device_remove_file(dev, &dev_attr_dev); 1204e105b8bfSDan Williams } 1205b9d9c82bSKay Sievers if (dev->class) { 1206da231fd5SKay Sievers device_remove_class_symlinks(dev); 120799ef3ef8SKay Sievers 1208ca22e56dSKay Sievers mutex_lock(&dev->class->p->mutex); 1209c47ed219SGreg Kroah-Hartman /* notify any interfaces that the device is now gone */ 1210184f1f77SGreg Kroah-Hartman list_for_each_entry(class_intf, 1211ca22e56dSKay Sievers &dev->class->p->interfaces, node) 1212c47ed219SGreg Kroah-Hartman if (class_intf->remove_dev) 1213c47ed219SGreg Kroah-Hartman class_intf->remove_dev(dev, class_intf); 1214c47ed219SGreg Kroah-Hartman /* remove the device from the class list */ 12155a3ceb86STejun Heo klist_del(&dev->knode_class); 1216ca22e56dSKay Sievers mutex_unlock(&dev->class->p->mutex); 1217b9d9c82bSKay Sievers } 1218c5e064a6SGreg Kroah-Hartman device_remove_file(dev, &dev_attr_uevent); 12192620efefSGreg Kroah-Hartman device_remove_attrs(dev); 122028953533SBenjamin Herrenschmidt bus_remove_device(dev); 12214b6d1f12SLongX Zhang device_pm_remove(dev); 1222d1c3414cSGrant Likely driver_deferred_probe_del(dev); 12231da177e4SLinus Torvalds 12241da177e4SLinus Torvalds /* Notify the platform of the removal, in case they 12251da177e4SLinus Torvalds * need to do anything... 12261da177e4SLinus Torvalds */ 12271da177e4SLinus Torvalds if (platform_notify_remove) 12281da177e4SLinus Torvalds platform_notify_remove(dev); 1229312c004dSKay Sievers kobject_uevent(&dev->kobj, KOBJ_REMOVE); 1230da231fd5SKay Sievers cleanup_device_parent(dev); 12311da177e4SLinus Torvalds kobject_del(&dev->kobj); 12321da177e4SLinus Torvalds put_device(parent); 12331da177e4SLinus Torvalds } 123486df2687SDavid Graham White EXPORT_SYMBOL_GPL(device_del); 12351da177e4SLinus Torvalds 12361da177e4SLinus Torvalds /** 12371da177e4SLinus Torvalds * device_unregister - unregister device from system. 12381da177e4SLinus Torvalds * @dev: device going away. 12391da177e4SLinus Torvalds * 12401da177e4SLinus Torvalds * We do this in two parts, like we do device_register(). First, 12411da177e4SLinus Torvalds * we remove it from all the subsystems with device_del(), then 12421da177e4SLinus Torvalds * we decrement the reference count via put_device(). If that 12431da177e4SLinus Torvalds * is the final reference count, the device will be cleaned up 12441da177e4SLinus Torvalds * via device_release() above. Otherwise, the structure will 12451da177e4SLinus Torvalds * stick around until the final reference to the device is dropped. 12461da177e4SLinus Torvalds */ 12471da177e4SLinus Torvalds void device_unregister(struct device *dev) 12481da177e4SLinus Torvalds { 12491e0b2cf9SKay Sievers pr_debug("device: '%s': %s\n", dev_name(dev), __func__); 12501da177e4SLinus Torvalds device_del(dev); 12511da177e4SLinus Torvalds put_device(dev); 12521da177e4SLinus Torvalds } 125386df2687SDavid Graham White EXPORT_SYMBOL_GPL(device_unregister); 12541da177e4SLinus Torvalds 125536239577Smochel@digitalimplant.org static struct device *next_device(struct klist_iter *i) 125636239577Smochel@digitalimplant.org { 125736239577Smochel@digitalimplant.org struct klist_node *n = klist_next(i); 1258f791b8c8SGreg Kroah-Hartman struct device *dev = NULL; 1259f791b8c8SGreg Kroah-Hartman struct device_private *p; 1260f791b8c8SGreg Kroah-Hartman 1261f791b8c8SGreg Kroah-Hartman if (n) { 1262f791b8c8SGreg Kroah-Hartman p = to_device_private_parent(n); 1263f791b8c8SGreg Kroah-Hartman dev = p->device; 1264f791b8c8SGreg Kroah-Hartman } 1265f791b8c8SGreg Kroah-Hartman return dev; 126636239577Smochel@digitalimplant.org } 126736239577Smochel@digitalimplant.org 12681da177e4SLinus Torvalds /** 1269e454cea2SKay Sievers * device_get_devnode - path of device node file 12706fcf53acSKay Sievers * @dev: device 1271e454cea2SKay Sievers * @mode: returned file access mode 12723c2670e6SKay Sievers * @uid: returned file owner 12733c2670e6SKay Sievers * @gid: returned file group 12746fcf53acSKay Sievers * @tmp: possibly allocated string 12756fcf53acSKay Sievers * 12766fcf53acSKay Sievers * Return the relative path of a possible device node. 12776fcf53acSKay Sievers * Non-default names may need to allocate a memory to compose 12786fcf53acSKay Sievers * a name. This memory is returned in tmp and needs to be 12796fcf53acSKay Sievers * freed by the caller. 12806fcf53acSKay Sievers */ 1281e454cea2SKay Sievers const char *device_get_devnode(struct device *dev, 12824e4098a3SGreg Kroah-Hartman umode_t *mode, kuid_t *uid, kgid_t *gid, 12833c2670e6SKay Sievers const char **tmp) 12846fcf53acSKay Sievers { 12856fcf53acSKay Sievers char *s; 12866fcf53acSKay Sievers 12876fcf53acSKay Sievers *tmp = NULL; 12886fcf53acSKay Sievers 12896fcf53acSKay Sievers /* the device type may provide a specific name */ 1290e454cea2SKay Sievers if (dev->type && dev->type->devnode) 12913c2670e6SKay Sievers *tmp = dev->type->devnode(dev, mode, uid, gid); 12926fcf53acSKay Sievers if (*tmp) 12936fcf53acSKay Sievers return *tmp; 12946fcf53acSKay Sievers 12956fcf53acSKay Sievers /* the class may provide a specific name */ 1296e454cea2SKay Sievers if (dev->class && dev->class->devnode) 1297e454cea2SKay Sievers *tmp = dev->class->devnode(dev, mode); 12986fcf53acSKay Sievers if (*tmp) 12996fcf53acSKay Sievers return *tmp; 13006fcf53acSKay Sievers 13016fcf53acSKay Sievers /* return name without allocation, tmp == NULL */ 13026fcf53acSKay Sievers if (strchr(dev_name(dev), '!') == NULL) 13036fcf53acSKay Sievers return dev_name(dev); 13046fcf53acSKay Sievers 13056fcf53acSKay Sievers /* replace '!' in the name with '/' */ 13066fcf53acSKay Sievers *tmp = kstrdup(dev_name(dev), GFP_KERNEL); 13076fcf53acSKay Sievers if (!*tmp) 13086fcf53acSKay Sievers return NULL; 13096fcf53acSKay Sievers while ((s = strchr(*tmp, '!'))) 13106fcf53acSKay Sievers s[0] = '/'; 13116fcf53acSKay Sievers return *tmp; 13126fcf53acSKay Sievers } 13136fcf53acSKay Sievers 13146fcf53acSKay Sievers /** 13151da177e4SLinus Torvalds * device_for_each_child - device child iterator. 1316c41455fbSRandy Dunlap * @parent: parent struct device. 13171da177e4SLinus Torvalds * @fn: function to be called for each device. 1318f8878dcbSRobert P. J. Day * @data: data for the callback. 13191da177e4SLinus Torvalds * 1320c41455fbSRandy Dunlap * Iterate over @parent's child devices, and call @fn for each, 13211da177e4SLinus Torvalds * passing it @data. 13221da177e4SLinus Torvalds * 13231da177e4SLinus Torvalds * We check the return of @fn each time. If it returns anything 13241da177e4SLinus Torvalds * other than 0, we break out and return that value. 13251da177e4SLinus Torvalds */ 132636239577Smochel@digitalimplant.org int device_for_each_child(struct device *parent, void *data, 13274a3ad20cSGreg Kroah-Hartman int (*fn)(struct device *dev, void *data)) 13281da177e4SLinus Torvalds { 132936239577Smochel@digitalimplant.org struct klist_iter i; 13301da177e4SLinus Torvalds struct device *child; 13311da177e4SLinus Torvalds int error = 0; 13321da177e4SLinus Torvalds 1333014c90dbSGreg Kroah-Hartman if (!parent->p) 1334014c90dbSGreg Kroah-Hartman return 0; 1335014c90dbSGreg Kroah-Hartman 1336f791b8c8SGreg Kroah-Hartman klist_iter_init(&parent->p->klist_children, &i); 133736239577Smochel@digitalimplant.org while ((child = next_device(&i)) && !error) 133836239577Smochel@digitalimplant.org error = fn(child, data); 133936239577Smochel@digitalimplant.org klist_iter_exit(&i); 13401da177e4SLinus Torvalds return error; 13411da177e4SLinus Torvalds } 134286df2687SDavid Graham White EXPORT_SYMBOL_GPL(device_for_each_child); 13431da177e4SLinus Torvalds 13445ab69981SCornelia Huck /** 13455ab69981SCornelia Huck * device_find_child - device iterator for locating a particular device. 13465ab69981SCornelia Huck * @parent: parent struct device 13475ab69981SCornelia Huck * @match: Callback function to check device 1348f8878dcbSRobert P. J. Day * @data: Data to pass to match function 13495ab69981SCornelia Huck * 13505ab69981SCornelia Huck * This is similar to the device_for_each_child() function above, but it 13515ab69981SCornelia Huck * returns a reference to a device that is 'found' for later use, as 13525ab69981SCornelia Huck * determined by the @match callback. 13535ab69981SCornelia Huck * 13545ab69981SCornelia Huck * The callback should return 0 if the device doesn't match and non-zero 13555ab69981SCornelia Huck * if it does. If the callback returns non-zero and a reference to the 13565ab69981SCornelia Huck * current device can be obtained, this function will return to the caller 13575ab69981SCornelia Huck * and not iterate over any more devices. 1358a4e2400aSFederico Vaga * 1359a4e2400aSFederico Vaga * NOTE: you will need to drop the reference with put_device() after use. 13605ab69981SCornelia Huck */ 13615ab69981SCornelia Huck struct device *device_find_child(struct device *parent, void *data, 13624a3ad20cSGreg Kroah-Hartman int (*match)(struct device *dev, void *data)) 13635ab69981SCornelia Huck { 13645ab69981SCornelia Huck struct klist_iter i; 13655ab69981SCornelia Huck struct device *child; 13665ab69981SCornelia Huck 13675ab69981SCornelia Huck if (!parent) 13685ab69981SCornelia Huck return NULL; 13695ab69981SCornelia Huck 1370f791b8c8SGreg Kroah-Hartman klist_iter_init(&parent->p->klist_children, &i); 13715ab69981SCornelia Huck while ((child = next_device(&i))) 13725ab69981SCornelia Huck if (match(child, data) && get_device(child)) 13735ab69981SCornelia Huck break; 13745ab69981SCornelia Huck klist_iter_exit(&i); 13755ab69981SCornelia Huck return child; 13765ab69981SCornelia Huck } 137786df2687SDavid Graham White EXPORT_SYMBOL_GPL(device_find_child); 13785ab69981SCornelia Huck 13791da177e4SLinus Torvalds int __init devices_init(void) 13801da177e4SLinus Torvalds { 1381881c6cfdSGreg Kroah-Hartman devices_kset = kset_create_and_add("devices", &device_uevent_ops, NULL); 1382881c6cfdSGreg Kroah-Hartman if (!devices_kset) 1383881c6cfdSGreg Kroah-Hartman return -ENOMEM; 1384e105b8bfSDan Williams dev_kobj = kobject_create_and_add("dev", NULL); 1385e105b8bfSDan Williams if (!dev_kobj) 1386e105b8bfSDan Williams goto dev_kobj_err; 1387e105b8bfSDan Williams sysfs_dev_block_kobj = kobject_create_and_add("block", dev_kobj); 1388e105b8bfSDan Williams if (!sysfs_dev_block_kobj) 1389e105b8bfSDan Williams goto block_kobj_err; 1390e105b8bfSDan Williams sysfs_dev_char_kobj = kobject_create_and_add("char", dev_kobj); 1391e105b8bfSDan Williams if (!sysfs_dev_char_kobj) 1392e105b8bfSDan Williams goto char_kobj_err; 1393e105b8bfSDan Williams 1394881c6cfdSGreg Kroah-Hartman return 0; 1395e105b8bfSDan Williams 1396e105b8bfSDan Williams char_kobj_err: 1397e105b8bfSDan Williams kobject_put(sysfs_dev_block_kobj); 1398e105b8bfSDan Williams block_kobj_err: 1399e105b8bfSDan Williams kobject_put(dev_kobj); 1400e105b8bfSDan Williams dev_kobj_err: 1401e105b8bfSDan Williams kset_unregister(devices_kset); 1402e105b8bfSDan Williams return -ENOMEM; 14031da177e4SLinus Torvalds } 14041da177e4SLinus Torvalds 14054f3549d7SRafael J. Wysocki static int device_check_offline(struct device *dev, void *not_used) 14064f3549d7SRafael J. Wysocki { 14074f3549d7SRafael J. Wysocki int ret; 14084f3549d7SRafael J. Wysocki 14094f3549d7SRafael J. Wysocki ret = device_for_each_child(dev, NULL, device_check_offline); 14104f3549d7SRafael J. Wysocki if (ret) 14114f3549d7SRafael J. Wysocki return ret; 14124f3549d7SRafael J. Wysocki 14134f3549d7SRafael J. Wysocki return device_supports_offline(dev) && !dev->offline ? -EBUSY : 0; 14144f3549d7SRafael J. Wysocki } 14154f3549d7SRafael J. Wysocki 14164f3549d7SRafael J. Wysocki /** 14174f3549d7SRafael J. Wysocki * device_offline - Prepare the device for hot-removal. 14184f3549d7SRafael J. Wysocki * @dev: Device to be put offline. 14194f3549d7SRafael J. Wysocki * 14204f3549d7SRafael J. Wysocki * Execute the device bus type's .offline() callback, if present, to prepare 14214f3549d7SRafael J. Wysocki * the device for a subsequent hot-removal. If that succeeds, the device must 14224f3549d7SRafael J. Wysocki * not be used until either it is removed or its bus type's .online() callback 14234f3549d7SRafael J. Wysocki * is executed. 14244f3549d7SRafael J. Wysocki * 14254f3549d7SRafael J. Wysocki * Call under device_hotplug_lock. 14264f3549d7SRafael J. Wysocki */ 14274f3549d7SRafael J. Wysocki int device_offline(struct device *dev) 14284f3549d7SRafael J. Wysocki { 14294f3549d7SRafael J. Wysocki int ret; 14304f3549d7SRafael J. Wysocki 14314f3549d7SRafael J. Wysocki if (dev->offline_disabled) 14324f3549d7SRafael J. Wysocki return -EPERM; 14334f3549d7SRafael J. Wysocki 14344f3549d7SRafael J. Wysocki ret = device_for_each_child(dev, NULL, device_check_offline); 14354f3549d7SRafael J. Wysocki if (ret) 14364f3549d7SRafael J. Wysocki return ret; 14374f3549d7SRafael J. Wysocki 14384f3549d7SRafael J. Wysocki device_lock(dev); 14394f3549d7SRafael J. Wysocki if (device_supports_offline(dev)) { 14404f3549d7SRafael J. Wysocki if (dev->offline) { 14414f3549d7SRafael J. Wysocki ret = 1; 14424f3549d7SRafael J. Wysocki } else { 14434f3549d7SRafael J. Wysocki ret = dev->bus->offline(dev); 14444f3549d7SRafael J. Wysocki if (!ret) { 14454f3549d7SRafael J. Wysocki kobject_uevent(&dev->kobj, KOBJ_OFFLINE); 14464f3549d7SRafael J. Wysocki dev->offline = true; 14474f3549d7SRafael J. Wysocki } 14484f3549d7SRafael J. Wysocki } 14494f3549d7SRafael J. Wysocki } 14504f3549d7SRafael J. Wysocki device_unlock(dev); 14514f3549d7SRafael J. Wysocki 14524f3549d7SRafael J. Wysocki return ret; 14534f3549d7SRafael J. Wysocki } 14544f3549d7SRafael J. Wysocki 14554f3549d7SRafael J. Wysocki /** 14564f3549d7SRafael J. Wysocki * device_online - Put the device back online after successful device_offline(). 14574f3549d7SRafael J. Wysocki * @dev: Device to be put back online. 14584f3549d7SRafael J. Wysocki * 14594f3549d7SRafael J. Wysocki * If device_offline() has been successfully executed for @dev, but the device 14604f3549d7SRafael J. Wysocki * has not been removed subsequently, execute its bus type's .online() callback 14614f3549d7SRafael J. Wysocki * to indicate that the device can be used again. 14624f3549d7SRafael J. Wysocki * 14634f3549d7SRafael J. Wysocki * Call under device_hotplug_lock. 14644f3549d7SRafael J. Wysocki */ 14654f3549d7SRafael J. Wysocki int device_online(struct device *dev) 14664f3549d7SRafael J. Wysocki { 14674f3549d7SRafael J. Wysocki int ret = 0; 14684f3549d7SRafael J. Wysocki 14694f3549d7SRafael J. Wysocki device_lock(dev); 14704f3549d7SRafael J. Wysocki if (device_supports_offline(dev)) { 14714f3549d7SRafael J. Wysocki if (dev->offline) { 14724f3549d7SRafael J. Wysocki ret = dev->bus->online(dev); 14734f3549d7SRafael J. Wysocki if (!ret) { 14744f3549d7SRafael J. Wysocki kobject_uevent(&dev->kobj, KOBJ_ONLINE); 14754f3549d7SRafael J. Wysocki dev->offline = false; 14764f3549d7SRafael J. Wysocki } 14774f3549d7SRafael J. Wysocki } else { 14784f3549d7SRafael J. Wysocki ret = 1; 14794f3549d7SRafael J. Wysocki } 14804f3549d7SRafael J. Wysocki } 14814f3549d7SRafael J. Wysocki device_unlock(dev); 14824f3549d7SRafael J. Wysocki 14834f3549d7SRafael J. Wysocki return ret; 14844f3549d7SRafael J. Wysocki } 14854f3549d7SRafael J. Wysocki 14867f100d15SKarthigan Srinivasan struct root_device { 14870aa0dc41SMark McLoughlin struct device dev; 14880aa0dc41SMark McLoughlin struct module *owner; 14890aa0dc41SMark McLoughlin }; 14900aa0dc41SMark McLoughlin 149193058424SJosh Triplett static inline struct root_device *to_root_device(struct device *d) 1492481e2079SFerenc Wagner { 1493481e2079SFerenc Wagner return container_of(d, struct root_device, dev); 1494481e2079SFerenc Wagner } 14950aa0dc41SMark McLoughlin 14960aa0dc41SMark McLoughlin static void root_device_release(struct device *dev) 14970aa0dc41SMark McLoughlin { 14980aa0dc41SMark McLoughlin kfree(to_root_device(dev)); 14990aa0dc41SMark McLoughlin } 15000aa0dc41SMark McLoughlin 15010aa0dc41SMark McLoughlin /** 15020aa0dc41SMark McLoughlin * __root_device_register - allocate and register a root device 15030aa0dc41SMark McLoughlin * @name: root device name 15040aa0dc41SMark McLoughlin * @owner: owner module of the root device, usually THIS_MODULE 15050aa0dc41SMark McLoughlin * 15060aa0dc41SMark McLoughlin * This function allocates a root device and registers it 15070aa0dc41SMark McLoughlin * using device_register(). In order to free the returned 15080aa0dc41SMark McLoughlin * device, use root_device_unregister(). 15090aa0dc41SMark McLoughlin * 15100aa0dc41SMark McLoughlin * Root devices are dummy devices which allow other devices 15110aa0dc41SMark McLoughlin * to be grouped under /sys/devices. Use this function to 15120aa0dc41SMark McLoughlin * allocate a root device and then use it as the parent of 15130aa0dc41SMark McLoughlin * any device which should appear under /sys/devices/{name} 15140aa0dc41SMark McLoughlin * 15150aa0dc41SMark McLoughlin * The /sys/devices/{name} directory will also contain a 15160aa0dc41SMark McLoughlin * 'module' symlink which points to the @owner directory 15170aa0dc41SMark McLoughlin * in sysfs. 15180aa0dc41SMark McLoughlin * 1519f0eae0edSJani Nikula * Returns &struct device pointer on success, or ERR_PTR() on error. 1520f0eae0edSJani Nikula * 15210aa0dc41SMark McLoughlin * Note: You probably want to use root_device_register(). 15220aa0dc41SMark McLoughlin */ 15230aa0dc41SMark McLoughlin struct device *__root_device_register(const char *name, struct module *owner) 15240aa0dc41SMark McLoughlin { 15250aa0dc41SMark McLoughlin struct root_device *root; 15260aa0dc41SMark McLoughlin int err = -ENOMEM; 15270aa0dc41SMark McLoughlin 15280aa0dc41SMark McLoughlin root = kzalloc(sizeof(struct root_device), GFP_KERNEL); 15290aa0dc41SMark McLoughlin if (!root) 15300aa0dc41SMark McLoughlin return ERR_PTR(err); 15310aa0dc41SMark McLoughlin 1532acc0e90fSGreg Kroah-Hartman err = dev_set_name(&root->dev, "%s", name); 15330aa0dc41SMark McLoughlin if (err) { 15340aa0dc41SMark McLoughlin kfree(root); 15350aa0dc41SMark McLoughlin return ERR_PTR(err); 15360aa0dc41SMark McLoughlin } 15370aa0dc41SMark McLoughlin 15380aa0dc41SMark McLoughlin root->dev.release = root_device_release; 15390aa0dc41SMark McLoughlin 15400aa0dc41SMark McLoughlin err = device_register(&root->dev); 15410aa0dc41SMark McLoughlin if (err) { 15420aa0dc41SMark McLoughlin put_device(&root->dev); 15430aa0dc41SMark McLoughlin return ERR_PTR(err); 15440aa0dc41SMark McLoughlin } 15450aa0dc41SMark McLoughlin 15461d9e882bSChristoph Egger #ifdef CONFIG_MODULES /* gotta find a "cleaner" way to do this */ 15470aa0dc41SMark McLoughlin if (owner) { 15480aa0dc41SMark McLoughlin struct module_kobject *mk = &owner->mkobj; 15490aa0dc41SMark McLoughlin 15500aa0dc41SMark McLoughlin err = sysfs_create_link(&root->dev.kobj, &mk->kobj, "module"); 15510aa0dc41SMark McLoughlin if (err) { 15520aa0dc41SMark McLoughlin device_unregister(&root->dev); 15530aa0dc41SMark McLoughlin return ERR_PTR(err); 15540aa0dc41SMark McLoughlin } 15550aa0dc41SMark McLoughlin root->owner = owner; 15560aa0dc41SMark McLoughlin } 15570aa0dc41SMark McLoughlin #endif 15580aa0dc41SMark McLoughlin 15590aa0dc41SMark McLoughlin return &root->dev; 15600aa0dc41SMark McLoughlin } 15610aa0dc41SMark McLoughlin EXPORT_SYMBOL_GPL(__root_device_register); 15620aa0dc41SMark McLoughlin 15630aa0dc41SMark McLoughlin /** 15640aa0dc41SMark McLoughlin * root_device_unregister - unregister and free a root device 15657cbcf225SRandy Dunlap * @dev: device going away 15660aa0dc41SMark McLoughlin * 15670aa0dc41SMark McLoughlin * This function unregisters and cleans up a device that was created by 15680aa0dc41SMark McLoughlin * root_device_register(). 15690aa0dc41SMark McLoughlin */ 15700aa0dc41SMark McLoughlin void root_device_unregister(struct device *dev) 15710aa0dc41SMark McLoughlin { 15720aa0dc41SMark McLoughlin struct root_device *root = to_root_device(dev); 15730aa0dc41SMark McLoughlin 15740aa0dc41SMark McLoughlin if (root->owner) 15750aa0dc41SMark McLoughlin sysfs_remove_link(&root->dev.kobj, "module"); 15760aa0dc41SMark McLoughlin 15770aa0dc41SMark McLoughlin device_unregister(dev); 15780aa0dc41SMark McLoughlin } 15790aa0dc41SMark McLoughlin EXPORT_SYMBOL_GPL(root_device_unregister); 15800aa0dc41SMark McLoughlin 158123681e47SGreg Kroah-Hartman 158223681e47SGreg Kroah-Hartman static void device_create_release(struct device *dev) 158323681e47SGreg Kroah-Hartman { 15841e0b2cf9SKay Sievers pr_debug("device: '%s': %s\n", dev_name(dev), __func__); 158523681e47SGreg Kroah-Hartman kfree(dev); 158623681e47SGreg Kroah-Hartman } 158723681e47SGreg Kroah-Hartman 158839ef3112SGuenter Roeck static struct device * 158939ef3112SGuenter Roeck device_create_groups_vargs(struct class *class, struct device *parent, 159039ef3112SGuenter Roeck dev_t devt, void *drvdata, 159139ef3112SGuenter Roeck const struct attribute_group **groups, 159239ef3112SGuenter Roeck const char *fmt, va_list args) 159339ef3112SGuenter Roeck { 159439ef3112SGuenter Roeck struct device *dev = NULL; 159539ef3112SGuenter Roeck int retval = -ENODEV; 159639ef3112SGuenter Roeck 159739ef3112SGuenter Roeck if (class == NULL || IS_ERR(class)) 159839ef3112SGuenter Roeck goto error; 159939ef3112SGuenter Roeck 160039ef3112SGuenter Roeck dev = kzalloc(sizeof(*dev), GFP_KERNEL); 160139ef3112SGuenter Roeck if (!dev) { 160239ef3112SGuenter Roeck retval = -ENOMEM; 160339ef3112SGuenter Roeck goto error; 160439ef3112SGuenter Roeck } 160539ef3112SGuenter Roeck 160639ef3112SGuenter Roeck dev->devt = devt; 160739ef3112SGuenter Roeck dev->class = class; 160839ef3112SGuenter Roeck dev->parent = parent; 160939ef3112SGuenter Roeck dev->groups = groups; 161039ef3112SGuenter Roeck dev->release = device_create_release; 161139ef3112SGuenter Roeck dev_set_drvdata(dev, drvdata); 161239ef3112SGuenter Roeck 161339ef3112SGuenter Roeck retval = kobject_set_name_vargs(&dev->kobj, fmt, args); 161439ef3112SGuenter Roeck if (retval) 161539ef3112SGuenter Roeck goto error; 161639ef3112SGuenter Roeck 161739ef3112SGuenter Roeck retval = device_register(dev); 161839ef3112SGuenter Roeck if (retval) 161939ef3112SGuenter Roeck goto error; 162039ef3112SGuenter Roeck 162139ef3112SGuenter Roeck return dev; 162239ef3112SGuenter Roeck 162339ef3112SGuenter Roeck error: 162439ef3112SGuenter Roeck put_device(dev); 162539ef3112SGuenter Roeck return ERR_PTR(retval); 162639ef3112SGuenter Roeck } 162739ef3112SGuenter Roeck 162823681e47SGreg Kroah-Hartman /** 16298882b394SGreg Kroah-Hartman * device_create_vargs - creates a device and registers it with sysfs 16308882b394SGreg Kroah-Hartman * @class: pointer to the struct class that this device should be registered to 16318882b394SGreg Kroah-Hartman * @parent: pointer to the parent struct device of this new device, if any 16328882b394SGreg Kroah-Hartman * @devt: the dev_t for the char device to be added 16338882b394SGreg Kroah-Hartman * @drvdata: the data to be added to the device for callbacks 16348882b394SGreg Kroah-Hartman * @fmt: string for the device's name 16358882b394SGreg Kroah-Hartman * @args: va_list for the device's name 16368882b394SGreg Kroah-Hartman * 16378882b394SGreg Kroah-Hartman * This function can be used by char device classes. A struct device 16388882b394SGreg Kroah-Hartman * will be created in sysfs, registered to the specified class. 16398882b394SGreg Kroah-Hartman * 16408882b394SGreg Kroah-Hartman * A "dev" file will be created, showing the dev_t for the device, if 16418882b394SGreg Kroah-Hartman * the dev_t is not 0,0. 16428882b394SGreg Kroah-Hartman * If a pointer to a parent struct device is passed in, the newly created 16438882b394SGreg Kroah-Hartman * struct device will be a child of that device in sysfs. 16448882b394SGreg Kroah-Hartman * The pointer to the struct device will be returned from the call. 16458882b394SGreg Kroah-Hartman * Any further sysfs files that might be required can be created using this 16468882b394SGreg Kroah-Hartman * pointer. 16478882b394SGreg Kroah-Hartman * 1648f0eae0edSJani Nikula * Returns &struct device pointer on success, or ERR_PTR() on error. 1649f0eae0edSJani Nikula * 16508882b394SGreg Kroah-Hartman * Note: the struct class passed to this function must have previously 16518882b394SGreg Kroah-Hartman * been created with a call to class_create(). 16528882b394SGreg Kroah-Hartman */ 16538882b394SGreg Kroah-Hartman struct device *device_create_vargs(struct class *class, struct device *parent, 16548882b394SGreg Kroah-Hartman dev_t devt, void *drvdata, const char *fmt, 16558882b394SGreg Kroah-Hartman va_list args) 16568882b394SGreg Kroah-Hartman { 165739ef3112SGuenter Roeck return device_create_groups_vargs(class, parent, devt, drvdata, NULL, 165839ef3112SGuenter Roeck fmt, args); 16598882b394SGreg Kroah-Hartman } 16608882b394SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(device_create_vargs); 16618882b394SGreg Kroah-Hartman 16628882b394SGreg Kroah-Hartman /** 16634e106739SGreg Kroah-Hartman * device_create - creates a device and registers it with sysfs 16648882b394SGreg Kroah-Hartman * @class: pointer to the struct class that this device should be registered to 16658882b394SGreg Kroah-Hartman * @parent: pointer to the parent struct device of this new device, if any 16668882b394SGreg Kroah-Hartman * @devt: the dev_t for the char device to be added 16678882b394SGreg Kroah-Hartman * @drvdata: the data to be added to the device for callbacks 16688882b394SGreg Kroah-Hartman * @fmt: string for the device's name 16698882b394SGreg Kroah-Hartman * 16708882b394SGreg Kroah-Hartman * This function can be used by char device classes. A struct device 16718882b394SGreg Kroah-Hartman * will be created in sysfs, registered to the specified class. 16728882b394SGreg Kroah-Hartman * 16738882b394SGreg Kroah-Hartman * A "dev" file will be created, showing the dev_t for the device, if 16748882b394SGreg Kroah-Hartman * the dev_t is not 0,0. 16758882b394SGreg Kroah-Hartman * If a pointer to a parent struct device is passed in, the newly created 16768882b394SGreg Kroah-Hartman * struct device will be a child of that device in sysfs. 16778882b394SGreg Kroah-Hartman * The pointer to the struct device will be returned from the call. 16788882b394SGreg Kroah-Hartman * Any further sysfs files that might be required can be created using this 16798882b394SGreg Kroah-Hartman * pointer. 16808882b394SGreg Kroah-Hartman * 1681f0eae0edSJani Nikula * Returns &struct device pointer on success, or ERR_PTR() on error. 1682f0eae0edSJani Nikula * 16838882b394SGreg Kroah-Hartman * Note: the struct class passed to this function must have previously 16848882b394SGreg Kroah-Hartman * been created with a call to class_create(). 16858882b394SGreg Kroah-Hartman */ 16864e106739SGreg Kroah-Hartman struct device *device_create(struct class *class, struct device *parent, 16874e106739SGreg Kroah-Hartman dev_t devt, void *drvdata, const char *fmt, ...) 16888882b394SGreg Kroah-Hartman { 16898882b394SGreg Kroah-Hartman va_list vargs; 16908882b394SGreg Kroah-Hartman struct device *dev; 16918882b394SGreg Kroah-Hartman 16928882b394SGreg Kroah-Hartman va_start(vargs, fmt); 16938882b394SGreg Kroah-Hartman dev = device_create_vargs(class, parent, devt, drvdata, fmt, vargs); 16948882b394SGreg Kroah-Hartman va_end(vargs); 16958882b394SGreg Kroah-Hartman return dev; 16968882b394SGreg Kroah-Hartman } 16974e106739SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(device_create); 16988882b394SGreg Kroah-Hartman 169939ef3112SGuenter Roeck /** 170039ef3112SGuenter Roeck * device_create_with_groups - creates a device and registers it with sysfs 170139ef3112SGuenter Roeck * @class: pointer to the struct class that this device should be registered to 170239ef3112SGuenter Roeck * @parent: pointer to the parent struct device of this new device, if any 170339ef3112SGuenter Roeck * @devt: the dev_t for the char device to be added 170439ef3112SGuenter Roeck * @drvdata: the data to be added to the device for callbacks 170539ef3112SGuenter Roeck * @groups: NULL-terminated list of attribute groups to be created 170639ef3112SGuenter Roeck * @fmt: string for the device's name 170739ef3112SGuenter Roeck * 170839ef3112SGuenter Roeck * This function can be used by char device classes. A struct device 170939ef3112SGuenter Roeck * will be created in sysfs, registered to the specified class. 171039ef3112SGuenter Roeck * Additional attributes specified in the groups parameter will also 171139ef3112SGuenter Roeck * be created automatically. 171239ef3112SGuenter Roeck * 171339ef3112SGuenter Roeck * A "dev" file will be created, showing the dev_t for the device, if 171439ef3112SGuenter Roeck * the dev_t is not 0,0. 171539ef3112SGuenter Roeck * If a pointer to a parent struct device is passed in, the newly created 171639ef3112SGuenter Roeck * struct device will be a child of that device in sysfs. 171739ef3112SGuenter Roeck * The pointer to the struct device will be returned from the call. 171839ef3112SGuenter Roeck * Any further sysfs files that might be required can be created using this 171939ef3112SGuenter Roeck * pointer. 172039ef3112SGuenter Roeck * 172139ef3112SGuenter Roeck * Returns &struct device pointer on success, or ERR_PTR() on error. 172239ef3112SGuenter Roeck * 172339ef3112SGuenter Roeck * Note: the struct class passed to this function must have previously 172439ef3112SGuenter Roeck * been created with a call to class_create(). 172539ef3112SGuenter Roeck */ 172639ef3112SGuenter Roeck struct device *device_create_with_groups(struct class *class, 172739ef3112SGuenter Roeck struct device *parent, dev_t devt, 172839ef3112SGuenter Roeck void *drvdata, 172939ef3112SGuenter Roeck const struct attribute_group **groups, 173039ef3112SGuenter Roeck const char *fmt, ...) 173139ef3112SGuenter Roeck { 173239ef3112SGuenter Roeck va_list vargs; 173339ef3112SGuenter Roeck struct device *dev; 173439ef3112SGuenter Roeck 173539ef3112SGuenter Roeck va_start(vargs, fmt); 173639ef3112SGuenter Roeck dev = device_create_groups_vargs(class, parent, devt, drvdata, groups, 173739ef3112SGuenter Roeck fmt, vargs); 173839ef3112SGuenter Roeck va_end(vargs); 173939ef3112SGuenter Roeck return dev; 174039ef3112SGuenter Roeck } 174139ef3112SGuenter Roeck EXPORT_SYMBOL_GPL(device_create_with_groups); 174239ef3112SGuenter Roeck 17439f3b795aSMichał Mirosław static int __match_devt(struct device *dev, const void *data) 174423681e47SGreg Kroah-Hartman { 17459f3b795aSMichał Mirosław const dev_t *devt = data; 174623681e47SGreg Kroah-Hartman 1747cd35449bSDave Young return dev->devt == *devt; 1748775b64d2SRafael J. Wysocki } 174923681e47SGreg Kroah-Hartman 1750775b64d2SRafael J. Wysocki /** 1751775b64d2SRafael J. Wysocki * device_destroy - removes a device that was created with device_create() 1752775b64d2SRafael J. Wysocki * @class: pointer to the struct class that this device was registered with 1753775b64d2SRafael J. Wysocki * @devt: the dev_t of the device that was previously registered 1754775b64d2SRafael J. Wysocki * 1755775b64d2SRafael J. Wysocki * This call unregisters and cleans up a device that was created with a 1756775b64d2SRafael J. Wysocki * call to device_create(). 1757775b64d2SRafael J. Wysocki */ 1758775b64d2SRafael J. Wysocki void device_destroy(struct class *class, dev_t devt) 1759775b64d2SRafael J. Wysocki { 1760775b64d2SRafael J. Wysocki struct device *dev; 1761775b64d2SRafael J. Wysocki 1762695794aeSGreg Kroah-Hartman dev = class_find_device(class, NULL, &devt, __match_devt); 1763cd35449bSDave Young if (dev) { 1764cd35449bSDave Young put_device(dev); 176523681e47SGreg Kroah-Hartman device_unregister(dev); 176623681e47SGreg Kroah-Hartman } 1767cd35449bSDave Young } 176823681e47SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(device_destroy); 1769a2de48caSGreg Kroah-Hartman 1770a2de48caSGreg Kroah-Hartman /** 1771a2de48caSGreg Kroah-Hartman * device_rename - renames a device 1772a2de48caSGreg Kroah-Hartman * @dev: the pointer to the struct device to be renamed 1773a2de48caSGreg Kroah-Hartman * @new_name: the new name of the device 1774030c1d2bSEric W. Biederman * 1775030c1d2bSEric W. Biederman * It is the responsibility of the caller to provide mutual 1776030c1d2bSEric W. Biederman * exclusion between two different calls of device_rename 1777030c1d2bSEric W. Biederman * on the same device to ensure that new_name is valid and 1778030c1d2bSEric W. Biederman * won't conflict with other devices. 1779c6c0ac66SMichael Ellerman * 1780a5462516STimur Tabi * Note: Don't call this function. Currently, the networking layer calls this 1781a5462516STimur Tabi * function, but that will change. The following text from Kay Sievers offers 1782a5462516STimur Tabi * some insight: 1783a5462516STimur Tabi * 1784a5462516STimur Tabi * Renaming devices is racy at many levels, symlinks and other stuff are not 1785a5462516STimur Tabi * replaced atomically, and you get a "move" uevent, but it's not easy to 1786a5462516STimur Tabi * connect the event to the old and new device. Device nodes are not renamed at 1787a5462516STimur Tabi * all, there isn't even support for that in the kernel now. 1788a5462516STimur Tabi * 1789a5462516STimur Tabi * In the meantime, during renaming, your target name might be taken by another 1790a5462516STimur Tabi * driver, creating conflicts. Or the old name is taken directly after you 1791a5462516STimur Tabi * renamed it -- then you get events for the same DEVPATH, before you even see 1792a5462516STimur Tabi * the "move" event. It's just a mess, and nothing new should ever rely on 1793a5462516STimur Tabi * kernel device renaming. Besides that, it's not even implemented now for 1794a5462516STimur Tabi * other things than (driver-core wise very simple) network devices. 1795a5462516STimur Tabi * 1796a5462516STimur Tabi * We are currently about to change network renaming in udev to completely 1797a5462516STimur Tabi * disallow renaming of devices in the same namespace as the kernel uses, 1798a5462516STimur Tabi * because we can't solve the problems properly, that arise with swapping names 1799a5462516STimur Tabi * of multiple interfaces without races. Means, renaming of eth[0-9]* will only 1800a5462516STimur Tabi * be allowed to some other name than eth[0-9]*, for the aforementioned 1801a5462516STimur Tabi * reasons. 1802a5462516STimur Tabi * 1803a5462516STimur Tabi * Make up a "real" name in the driver before you register anything, or add 1804a5462516STimur Tabi * some other attributes for userspace to find the device, or use udev to add 1805a5462516STimur Tabi * symlinks -- but never rename kernel devices later, it's a complete mess. We 1806a5462516STimur Tabi * don't even want to get into that and try to implement the missing pieces in 1807a5462516STimur Tabi * the core. We really have other pieces to fix in the driver core mess. :) 1808a2de48caSGreg Kroah-Hartman */ 18096937e8f8SJohannes Berg int device_rename(struct device *dev, const char *new_name) 1810a2de48caSGreg Kroah-Hartman { 18114b30ee58STejun Heo struct kobject *kobj = &dev->kobj; 18122ee97cafSCornelia Huck char *old_device_name = NULL; 1813a2de48caSGreg Kroah-Hartman int error; 1814a2de48caSGreg Kroah-Hartman 1815a2de48caSGreg Kroah-Hartman dev = get_device(dev); 1816a2de48caSGreg Kroah-Hartman if (!dev) 1817a2de48caSGreg Kroah-Hartman return -EINVAL; 1818a2de48caSGreg Kroah-Hartman 1819*69df7533Sethan.zhao dev_dbg(dev, "renaming to %s\n", new_name); 1820a2de48caSGreg Kroah-Hartman 18211fa5ae85SKay Sievers old_device_name = kstrdup(dev_name(dev), GFP_KERNEL); 18222ee97cafSCornelia Huck if (!old_device_name) { 1823952ab431SJesper Juhl error = -ENOMEM; 18242ee97cafSCornelia Huck goto out; 1825952ab431SJesper Juhl } 1826a2de48caSGreg Kroah-Hartman 1827f349cf34SEric W. Biederman if (dev->class) { 18284b30ee58STejun Heo error = sysfs_rename_link_ns(&dev->class->p->subsys.kobj, 18294b30ee58STejun Heo kobj, old_device_name, 18304b30ee58STejun Heo new_name, kobject_namespace(kobj)); 1831f349cf34SEric W. Biederman if (error) 1832f349cf34SEric W. Biederman goto out; 1833f349cf34SEric W. Biederman } 183439aba963SKay Sievers 18354b30ee58STejun Heo error = kobject_rename(kobj, new_name); 18361fa5ae85SKay Sievers if (error) 18372ee97cafSCornelia Huck goto out; 1838a2de48caSGreg Kroah-Hartman 18392ee97cafSCornelia Huck out: 1840a2de48caSGreg Kroah-Hartman put_device(dev); 1841a2de48caSGreg Kroah-Hartman 18422ee97cafSCornelia Huck kfree(old_device_name); 1843a2de48caSGreg Kroah-Hartman 1844a2de48caSGreg Kroah-Hartman return error; 1845a2de48caSGreg Kroah-Hartman } 1846a2807dbcSJohannes Berg EXPORT_SYMBOL_GPL(device_rename); 18478a82472fSCornelia Huck 18488a82472fSCornelia Huck static int device_move_class_links(struct device *dev, 18498a82472fSCornelia Huck struct device *old_parent, 18508a82472fSCornelia Huck struct device *new_parent) 18518a82472fSCornelia Huck { 1852f7f3461dSGreg Kroah-Hartman int error = 0; 18538a82472fSCornelia Huck 1854f7f3461dSGreg Kroah-Hartman if (old_parent) 1855f7f3461dSGreg Kroah-Hartman sysfs_remove_link(&dev->kobj, "device"); 1856f7f3461dSGreg Kroah-Hartman if (new_parent) 1857f7f3461dSGreg Kroah-Hartman error = sysfs_create_link(&dev->kobj, &new_parent->kobj, 1858f7f3461dSGreg Kroah-Hartman "device"); 1859f7f3461dSGreg Kroah-Hartman return error; 18608a82472fSCornelia Huck } 18618a82472fSCornelia Huck 18628a82472fSCornelia Huck /** 18638a82472fSCornelia Huck * device_move - moves a device to a new parent 18648a82472fSCornelia Huck * @dev: the pointer to the struct device to be moved 1865c744aeaeSCornelia Huck * @new_parent: the new parent of the device (can by NULL) 1866ffa6a705SCornelia Huck * @dpm_order: how to reorder the dpm_list 18678a82472fSCornelia Huck */ 1868ffa6a705SCornelia Huck int device_move(struct device *dev, struct device *new_parent, 1869ffa6a705SCornelia Huck enum dpm_order dpm_order) 18708a82472fSCornelia Huck { 18718a82472fSCornelia Huck int error; 18728a82472fSCornelia Huck struct device *old_parent; 1873c744aeaeSCornelia Huck struct kobject *new_parent_kobj; 18748a82472fSCornelia Huck 18758a82472fSCornelia Huck dev = get_device(dev); 18768a82472fSCornelia Huck if (!dev) 18778a82472fSCornelia Huck return -EINVAL; 18788a82472fSCornelia Huck 1879ffa6a705SCornelia Huck device_pm_lock(); 18808a82472fSCornelia Huck new_parent = get_device(new_parent); 1881c744aeaeSCornelia Huck new_parent_kobj = get_device_parent(dev, new_parent); 188263b6971aSCornelia Huck 18831e0b2cf9SKay Sievers pr_debug("device: '%s': %s: moving to '%s'\n", dev_name(dev), 18841e0b2cf9SKay Sievers __func__, new_parent ? dev_name(new_parent) : "<NULL>"); 1885c744aeaeSCornelia Huck error = kobject_move(&dev->kobj, new_parent_kobj); 18868a82472fSCornelia Huck if (error) { 188763b6971aSCornelia Huck cleanup_glue_dir(dev, new_parent_kobj); 18888a82472fSCornelia Huck put_device(new_parent); 18898a82472fSCornelia Huck goto out; 18908a82472fSCornelia Huck } 18918a82472fSCornelia Huck old_parent = dev->parent; 18928a82472fSCornelia Huck dev->parent = new_parent; 18938a82472fSCornelia Huck if (old_parent) 1894f791b8c8SGreg Kroah-Hartman klist_remove(&dev->p->knode_parent); 18950d358f22SYinghai Lu if (new_parent) { 1896f791b8c8SGreg Kroah-Hartman klist_add_tail(&dev->p->knode_parent, 1897f791b8c8SGreg Kroah-Hartman &new_parent->p->klist_children); 18980d358f22SYinghai Lu set_dev_node(dev, dev_to_node(new_parent)); 18990d358f22SYinghai Lu } 19000d358f22SYinghai Lu 1901bdd4034dSRabin Vincent if (dev->class) { 19028a82472fSCornelia Huck error = device_move_class_links(dev, old_parent, new_parent); 19038a82472fSCornelia Huck if (error) { 19048a82472fSCornelia Huck /* We ignore errors on cleanup since we're hosed anyway... */ 19058a82472fSCornelia Huck device_move_class_links(dev, new_parent, old_parent); 19068a82472fSCornelia Huck if (!kobject_move(&dev->kobj, &old_parent->kobj)) { 1907c744aeaeSCornelia Huck if (new_parent) 1908f791b8c8SGreg Kroah-Hartman klist_remove(&dev->p->knode_parent); 19090d358f22SYinghai Lu dev->parent = old_parent; 19100d358f22SYinghai Lu if (old_parent) { 1911f791b8c8SGreg Kroah-Hartman klist_add_tail(&dev->p->knode_parent, 1912f791b8c8SGreg Kroah-Hartman &old_parent->p->klist_children); 19130d358f22SYinghai Lu set_dev_node(dev, dev_to_node(old_parent)); 19140d358f22SYinghai Lu } 19158a82472fSCornelia Huck } 191663b6971aSCornelia Huck cleanup_glue_dir(dev, new_parent_kobj); 19178a82472fSCornelia Huck put_device(new_parent); 19188a82472fSCornelia Huck goto out; 19198a82472fSCornelia Huck } 1920bdd4034dSRabin Vincent } 1921ffa6a705SCornelia Huck switch (dpm_order) { 1922ffa6a705SCornelia Huck case DPM_ORDER_NONE: 1923ffa6a705SCornelia Huck break; 1924ffa6a705SCornelia Huck case DPM_ORDER_DEV_AFTER_PARENT: 1925ffa6a705SCornelia Huck device_pm_move_after(dev, new_parent); 1926ffa6a705SCornelia Huck break; 1927ffa6a705SCornelia Huck case DPM_ORDER_PARENT_BEFORE_DEV: 1928ffa6a705SCornelia Huck device_pm_move_before(new_parent, dev); 1929ffa6a705SCornelia Huck break; 1930ffa6a705SCornelia Huck case DPM_ORDER_DEV_LAST: 1931ffa6a705SCornelia Huck device_pm_move_last(dev); 1932ffa6a705SCornelia Huck break; 1933ffa6a705SCornelia Huck } 1934bdd4034dSRabin Vincent 19358a82472fSCornelia Huck put_device(old_parent); 19368a82472fSCornelia Huck out: 1937ffa6a705SCornelia Huck device_pm_unlock(); 19388a82472fSCornelia Huck put_device(dev); 19398a82472fSCornelia Huck return error; 19408a82472fSCornelia Huck } 19418a82472fSCornelia Huck EXPORT_SYMBOL_GPL(device_move); 194237b0c020SGreg Kroah-Hartman 194337b0c020SGreg Kroah-Hartman /** 194437b0c020SGreg Kroah-Hartman * device_shutdown - call ->shutdown() on each device to shutdown. 194537b0c020SGreg Kroah-Hartman */ 194637b0c020SGreg Kroah-Hartman void device_shutdown(void) 194737b0c020SGreg Kroah-Hartman { 1948f123db8eSBenson Leung struct device *dev, *parent; 194937b0c020SGreg Kroah-Hartman 19506245838fSHugh Daschbach spin_lock(&devices_kset->list_lock); 19516245838fSHugh Daschbach /* 19526245838fSHugh Daschbach * Walk the devices list backward, shutting down each in turn. 19536245838fSHugh Daschbach * Beware that device unplug events may also start pulling 19546245838fSHugh Daschbach * devices offline, even as the system is shutting down. 19556245838fSHugh Daschbach */ 19566245838fSHugh Daschbach while (!list_empty(&devices_kset->list)) { 19576245838fSHugh Daschbach dev = list_entry(devices_kset->list.prev, struct device, 19586245838fSHugh Daschbach kobj.entry); 1959d1c6c030SMing Lei 1960d1c6c030SMing Lei /* 1961d1c6c030SMing Lei * hold reference count of device's parent to 1962d1c6c030SMing Lei * prevent it from being freed because parent's 1963d1c6c030SMing Lei * lock is to be held 1964d1c6c030SMing Lei */ 1965f123db8eSBenson Leung parent = get_device(dev->parent); 19666245838fSHugh Daschbach get_device(dev); 19676245838fSHugh Daschbach /* 19686245838fSHugh Daschbach * Make sure the device is off the kset list, in the 19696245838fSHugh Daschbach * event that dev->*->shutdown() doesn't remove it. 19706245838fSHugh Daschbach */ 19716245838fSHugh Daschbach list_del_init(&dev->kobj.entry); 19726245838fSHugh Daschbach spin_unlock(&devices_kset->list_lock); 1973fe6b91f4SAlan Stern 1974d1c6c030SMing Lei /* hold lock to avoid race with probe/release */ 1975f123db8eSBenson Leung if (parent) 1976f123db8eSBenson Leung device_lock(parent); 1977d1c6c030SMing Lei device_lock(dev); 1978d1c6c030SMing Lei 1979fe6b91f4SAlan Stern /* Don't allow any more runtime suspends */ 1980fe6b91f4SAlan Stern pm_runtime_get_noresume(dev); 1981fe6b91f4SAlan Stern pm_runtime_barrier(dev); 19826245838fSHugh Daschbach 198337b0c020SGreg Kroah-Hartman if (dev->bus && dev->bus->shutdown) { 19840246c4faSShuoX Liu if (initcall_debug) 19850246c4faSShuoX Liu dev_info(dev, "shutdown\n"); 198637b0c020SGreg Kroah-Hartman dev->bus->shutdown(dev); 198737b0c020SGreg Kroah-Hartman } else if (dev->driver && dev->driver->shutdown) { 19880246c4faSShuoX Liu if (initcall_debug) 19890246c4faSShuoX Liu dev_info(dev, "shutdown\n"); 199037b0c020SGreg Kroah-Hartman dev->driver->shutdown(dev); 199137b0c020SGreg Kroah-Hartman } 1992d1c6c030SMing Lei 1993d1c6c030SMing Lei device_unlock(dev); 1994f123db8eSBenson Leung if (parent) 1995f123db8eSBenson Leung device_unlock(parent); 1996d1c6c030SMing Lei 19976245838fSHugh Daschbach put_device(dev); 1998f123db8eSBenson Leung put_device(parent); 19996245838fSHugh Daschbach 20006245838fSHugh Daschbach spin_lock(&devices_kset->list_lock); 200137b0c020SGreg Kroah-Hartman } 20026245838fSHugh Daschbach spin_unlock(&devices_kset->list_lock); 2003401097eaSShaohua Li async_synchronize_full(); 200437b0c020SGreg Kroah-Hartman } 200599bcf217SJoe Perches 200699bcf217SJoe Perches /* 200799bcf217SJoe Perches * Device logging functions 200899bcf217SJoe Perches */ 200999bcf217SJoe Perches 201099bcf217SJoe Perches #ifdef CONFIG_PRINTK 2011666f355fSJoe Perches static int 2012666f355fSJoe Perches create_syslog_header(const struct device *dev, char *hdr, size_t hdrlen) 201399bcf217SJoe Perches { 2014c4e00daaSKay Sievers const char *subsys; 2015798efc60SJoe Perches size_t pos = 0; 201699bcf217SJoe Perches 2017c4e00daaSKay Sievers if (dev->class) 2018c4e00daaSKay Sievers subsys = dev->class->name; 2019c4e00daaSKay Sievers else if (dev->bus) 2020c4e00daaSKay Sievers subsys = dev->bus->name; 2021c4e00daaSKay Sievers else 2022798efc60SJoe Perches return 0; 2023c4e00daaSKay Sievers 2024798efc60SJoe Perches pos += snprintf(hdr + pos, hdrlen - pos, "SUBSYSTEM=%s", subsys); 2025c4e00daaSKay Sievers 2026c4e00daaSKay Sievers /* 2027c4e00daaSKay Sievers * Add device identifier DEVICE=: 2028c4e00daaSKay Sievers * b12:8 block dev_t 2029c4e00daaSKay Sievers * c127:3 char dev_t 2030c4e00daaSKay Sievers * n8 netdev ifindex 2031c4e00daaSKay Sievers * +sound:card0 subsystem:devname 2032c4e00daaSKay Sievers */ 2033c4e00daaSKay Sievers if (MAJOR(dev->devt)) { 2034c4e00daaSKay Sievers char c; 2035c4e00daaSKay Sievers 2036c4e00daaSKay Sievers if (strcmp(subsys, "block") == 0) 2037c4e00daaSKay Sievers c = 'b'; 2038c4e00daaSKay Sievers else 2039c4e00daaSKay Sievers c = 'c'; 2040798efc60SJoe Perches pos++; 2041798efc60SJoe Perches pos += snprintf(hdr + pos, hdrlen - pos, 2042c4e00daaSKay Sievers "DEVICE=%c%u:%u", 2043c4e00daaSKay Sievers c, MAJOR(dev->devt), MINOR(dev->devt)); 2044c4e00daaSKay Sievers } else if (strcmp(subsys, "net") == 0) { 2045c4e00daaSKay Sievers struct net_device *net = to_net_dev(dev); 2046c4e00daaSKay Sievers 2047798efc60SJoe Perches pos++; 2048798efc60SJoe Perches pos += snprintf(hdr + pos, hdrlen - pos, 2049c4e00daaSKay Sievers "DEVICE=n%u", net->ifindex); 2050c4e00daaSKay Sievers } else { 2051798efc60SJoe Perches pos++; 2052798efc60SJoe Perches pos += snprintf(hdr + pos, hdrlen - pos, 2053c4e00daaSKay Sievers "DEVICE=+%s:%s", subsys, dev_name(dev)); 2054c4e00daaSKay Sievers } 2055af7f2158SJim Cromie 2056798efc60SJoe Perches return pos; 205799bcf217SJoe Perches } 2058798efc60SJoe Perches EXPORT_SYMBOL(create_syslog_header); 2059798efc60SJoe Perches 206005e4e5b8SJoe Perches int dev_vprintk_emit(int level, const struct device *dev, 206105e4e5b8SJoe Perches const char *fmt, va_list args) 206205e4e5b8SJoe Perches { 206305e4e5b8SJoe Perches char hdr[128]; 206405e4e5b8SJoe Perches size_t hdrlen; 206505e4e5b8SJoe Perches 206605e4e5b8SJoe Perches hdrlen = create_syslog_header(dev, hdr, sizeof(hdr)); 206705e4e5b8SJoe Perches 206805e4e5b8SJoe Perches return vprintk_emit(0, level, hdrlen ? hdr : NULL, hdrlen, fmt, args); 206905e4e5b8SJoe Perches } 207005e4e5b8SJoe Perches EXPORT_SYMBOL(dev_vprintk_emit); 207105e4e5b8SJoe Perches 207205e4e5b8SJoe Perches int dev_printk_emit(int level, const struct device *dev, const char *fmt, ...) 207305e4e5b8SJoe Perches { 207405e4e5b8SJoe Perches va_list args; 207505e4e5b8SJoe Perches int r; 207605e4e5b8SJoe Perches 207705e4e5b8SJoe Perches va_start(args, fmt); 207805e4e5b8SJoe Perches 207905e4e5b8SJoe Perches r = dev_vprintk_emit(level, dev, fmt, args); 208005e4e5b8SJoe Perches 208105e4e5b8SJoe Perches va_end(args); 208205e4e5b8SJoe Perches 208305e4e5b8SJoe Perches return r; 208405e4e5b8SJoe Perches } 208505e4e5b8SJoe Perches EXPORT_SYMBOL(dev_printk_emit); 208605e4e5b8SJoe Perches 2087798efc60SJoe Perches static int __dev_printk(const char *level, const struct device *dev, 2088798efc60SJoe Perches struct va_format *vaf) 2089798efc60SJoe Perches { 2090798efc60SJoe Perches if (!dev) 2091798efc60SJoe Perches return printk("%s(NULL device *): %pV", level, vaf); 2092798efc60SJoe Perches 2093666f355fSJoe Perches return dev_printk_emit(level[1] - '0', dev, 2094798efc60SJoe Perches "%s %s: %pV", 2095798efc60SJoe Perches dev_driver_string(dev), dev_name(dev), vaf); 2096798efc60SJoe Perches } 209799bcf217SJoe Perches 209899bcf217SJoe Perches int dev_printk(const char *level, const struct device *dev, 209999bcf217SJoe Perches const char *fmt, ...) 210099bcf217SJoe Perches { 210199bcf217SJoe Perches struct va_format vaf; 210299bcf217SJoe Perches va_list args; 210399bcf217SJoe Perches int r; 210499bcf217SJoe Perches 210599bcf217SJoe Perches va_start(args, fmt); 210699bcf217SJoe Perches 210799bcf217SJoe Perches vaf.fmt = fmt; 210899bcf217SJoe Perches vaf.va = &args; 210999bcf217SJoe Perches 211099bcf217SJoe Perches r = __dev_printk(level, dev, &vaf); 2111798efc60SJoe Perches 211299bcf217SJoe Perches va_end(args); 211399bcf217SJoe Perches 211499bcf217SJoe Perches return r; 211599bcf217SJoe Perches } 211699bcf217SJoe Perches EXPORT_SYMBOL(dev_printk); 211799bcf217SJoe Perches 211899bcf217SJoe Perches #define define_dev_printk_level(func, kern_level) \ 211999bcf217SJoe Perches int func(const struct device *dev, const char *fmt, ...) \ 212099bcf217SJoe Perches { \ 212199bcf217SJoe Perches struct va_format vaf; \ 212299bcf217SJoe Perches va_list args; \ 212399bcf217SJoe Perches int r; \ 212499bcf217SJoe Perches \ 212599bcf217SJoe Perches va_start(args, fmt); \ 212699bcf217SJoe Perches \ 212799bcf217SJoe Perches vaf.fmt = fmt; \ 212899bcf217SJoe Perches vaf.va = &args; \ 212999bcf217SJoe Perches \ 213099bcf217SJoe Perches r = __dev_printk(kern_level, dev, &vaf); \ 2131798efc60SJoe Perches \ 213299bcf217SJoe Perches va_end(args); \ 213399bcf217SJoe Perches \ 213499bcf217SJoe Perches return r; \ 213599bcf217SJoe Perches } \ 213699bcf217SJoe Perches EXPORT_SYMBOL(func); 213799bcf217SJoe Perches 213899bcf217SJoe Perches define_dev_printk_level(dev_emerg, KERN_EMERG); 213999bcf217SJoe Perches define_dev_printk_level(dev_alert, KERN_ALERT); 214099bcf217SJoe Perches define_dev_printk_level(dev_crit, KERN_CRIT); 214199bcf217SJoe Perches define_dev_printk_level(dev_err, KERN_ERR); 214299bcf217SJoe Perches define_dev_printk_level(dev_warn, KERN_WARNING); 214399bcf217SJoe Perches define_dev_printk_level(dev_notice, KERN_NOTICE); 214499bcf217SJoe Perches define_dev_printk_level(_dev_info, KERN_INFO); 214599bcf217SJoe Perches 214699bcf217SJoe Perches #endif 2147