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> 211da177e4SLinus Torvalds 221da177e4SLinus Torvalds #include <asm/semaphore.h> 231da177e4SLinus Torvalds 241da177e4SLinus Torvalds #include "base.h" 251da177e4SLinus Torvalds #include "power/power.h" 261da177e4SLinus Torvalds 271da177e4SLinus Torvalds int (*platform_notify)(struct device * dev) = NULL; 281da177e4SLinus Torvalds int (*platform_notify_remove)(struct device * dev) = NULL; 291da177e4SLinus Torvalds 301da177e4SLinus Torvalds /* 311da177e4SLinus Torvalds * sysfs bindings for devices. 321da177e4SLinus Torvalds */ 331da177e4SLinus Torvalds 343e95637aSAlan Stern /** 353e95637aSAlan Stern * dev_driver_string - Return a device's driver name, if at all possible 363e95637aSAlan Stern * @dev: struct device to get the name of 373e95637aSAlan Stern * 383e95637aSAlan Stern * Will return the device's driver's name if it is bound to a device. If 393e95637aSAlan Stern * the device is not bound to a device, it will return the name of the bus 403e95637aSAlan Stern * it is attached to. If it is not attached to a bus either, an empty 413e95637aSAlan Stern * string will be returned. 423e95637aSAlan Stern */ 433e95637aSAlan Stern const char *dev_driver_string(struct device *dev) 443e95637aSAlan Stern { 453e95637aSAlan Stern return dev->driver ? dev->driver->name : 46a456b702SJean Delvare (dev->bus ? dev->bus->name : 47a456b702SJean Delvare (dev->class ? dev->class->name : "")); 483e95637aSAlan Stern } 49310a922dSMatthew Wilcox EXPORT_SYMBOL(dev_driver_string); 503e95637aSAlan Stern 511da177e4SLinus Torvalds #define to_dev(obj) container_of(obj, struct device, kobj) 521da177e4SLinus Torvalds #define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr) 531da177e4SLinus Torvalds 541da177e4SLinus Torvalds static ssize_t 551da177e4SLinus Torvalds dev_attr_show(struct kobject * kobj, struct attribute * attr, char * buf) 561da177e4SLinus Torvalds { 571da177e4SLinus Torvalds struct device_attribute * dev_attr = to_dev_attr(attr); 581da177e4SLinus Torvalds struct device * dev = to_dev(kobj); 594a0c20bfSDmitry Torokhov ssize_t ret = -EIO; 601da177e4SLinus Torvalds 611da177e4SLinus Torvalds if (dev_attr->show) 6254b6f35cSYani Ioannou ret = dev_attr->show(dev, dev_attr, buf); 631da177e4SLinus Torvalds return ret; 641da177e4SLinus Torvalds } 651da177e4SLinus Torvalds 661da177e4SLinus Torvalds static ssize_t 671da177e4SLinus Torvalds dev_attr_store(struct kobject * kobj, struct attribute * attr, 681da177e4SLinus Torvalds const char * buf, size_t count) 691da177e4SLinus Torvalds { 701da177e4SLinus Torvalds struct device_attribute * dev_attr = to_dev_attr(attr); 711da177e4SLinus Torvalds struct device * dev = to_dev(kobj); 724a0c20bfSDmitry Torokhov ssize_t ret = -EIO; 731da177e4SLinus Torvalds 741da177e4SLinus Torvalds if (dev_attr->store) 7554b6f35cSYani Ioannou ret = dev_attr->store(dev, dev_attr, buf, count); 761da177e4SLinus Torvalds return ret; 771da177e4SLinus Torvalds } 781da177e4SLinus Torvalds 791da177e4SLinus Torvalds static struct sysfs_ops dev_sysfs_ops = { 801da177e4SLinus Torvalds .show = dev_attr_show, 811da177e4SLinus Torvalds .store = dev_attr_store, 821da177e4SLinus Torvalds }; 831da177e4SLinus Torvalds 841da177e4SLinus Torvalds 851da177e4SLinus Torvalds /** 861da177e4SLinus Torvalds * device_release - free device structure. 871da177e4SLinus Torvalds * @kobj: device's kobject. 881da177e4SLinus Torvalds * 891da177e4SLinus Torvalds * This is called once the reference count for the object 901da177e4SLinus Torvalds * reaches 0. We forward the call to the device's release 911da177e4SLinus Torvalds * method, which should handle actually freeing the structure. 921da177e4SLinus Torvalds */ 931da177e4SLinus Torvalds static void device_release(struct kobject * kobj) 941da177e4SLinus Torvalds { 951da177e4SLinus Torvalds struct device * dev = to_dev(kobj); 961da177e4SLinus Torvalds 971da177e4SLinus Torvalds if (dev->release) 981da177e4SLinus Torvalds dev->release(dev); 99f9f852dfSKay Sievers else if (dev->type && dev->type->release) 100f9f852dfSKay Sievers dev->type->release(dev); 1012620efefSGreg Kroah-Hartman else if (dev->class && dev->class->dev_release) 1022620efefSGreg Kroah-Hartman dev->class->dev_release(dev); 1031da177e4SLinus Torvalds else { 1041da177e4SLinus Torvalds printk(KERN_ERR "Device '%s' does not have a release() function, " 1051da177e4SLinus Torvalds "it is broken and must be fixed.\n", 1061da177e4SLinus Torvalds dev->bus_id); 1071da177e4SLinus Torvalds WARN_ON(1); 1081da177e4SLinus Torvalds } 1091da177e4SLinus Torvalds } 1101da177e4SLinus Torvalds 1111da177e4SLinus Torvalds static struct kobj_type ktype_device = { 1121da177e4SLinus Torvalds .release = device_release, 1131da177e4SLinus Torvalds .sysfs_ops = &dev_sysfs_ops, 1141da177e4SLinus Torvalds }; 1151da177e4SLinus Torvalds 1161da177e4SLinus Torvalds 117312c004dSKay Sievers static int dev_uevent_filter(struct kset *kset, struct kobject *kobj) 1181da177e4SLinus Torvalds { 1191da177e4SLinus Torvalds struct kobj_type *ktype = get_ktype(kobj); 1201da177e4SLinus Torvalds 1211da177e4SLinus Torvalds if (ktype == &ktype_device) { 1221da177e4SLinus Torvalds struct device *dev = to_dev(kobj); 12383b5fb4cSCornelia Huck if (dev->uevent_suppress) 12483b5fb4cSCornelia Huck return 0; 1251da177e4SLinus Torvalds if (dev->bus) 1261da177e4SLinus Torvalds return 1; 12723681e47SGreg Kroah-Hartman if (dev->class) 12823681e47SGreg Kroah-Hartman return 1; 1291da177e4SLinus Torvalds } 1301da177e4SLinus Torvalds return 0; 1311da177e4SLinus Torvalds } 1321da177e4SLinus Torvalds 133312c004dSKay Sievers static const char *dev_uevent_name(struct kset *kset, struct kobject *kobj) 1341da177e4SLinus Torvalds { 1351da177e4SLinus Torvalds struct device *dev = to_dev(kobj); 1361da177e4SLinus Torvalds 13723681e47SGreg Kroah-Hartman if (dev->bus) 1381da177e4SLinus Torvalds return dev->bus->name; 13923681e47SGreg Kroah-Hartman if (dev->class) 14023681e47SGreg Kroah-Hartman return dev->class->name; 14123681e47SGreg Kroah-Hartman return NULL; 1421da177e4SLinus Torvalds } 1431da177e4SLinus Torvalds 144312c004dSKay Sievers static int dev_uevent(struct kset *kset, struct kobject *kobj, char **envp, 1451da177e4SLinus Torvalds int num_envp, char *buffer, int buffer_size) 1461da177e4SLinus Torvalds { 1471da177e4SLinus Torvalds struct device *dev = to_dev(kobj); 1481da177e4SLinus Torvalds int i = 0; 1491da177e4SLinus Torvalds int length = 0; 1501da177e4SLinus Torvalds int retval = 0; 1511da177e4SLinus Torvalds 15223681e47SGreg Kroah-Hartman /* add the major/minor if present */ 15323681e47SGreg Kroah-Hartman if (MAJOR(dev->devt)) { 15423681e47SGreg Kroah-Hartman add_uevent_var(envp, num_envp, &i, 15523681e47SGreg Kroah-Hartman buffer, buffer_size, &length, 15623681e47SGreg Kroah-Hartman "MAJOR=%u", MAJOR(dev->devt)); 15723681e47SGreg Kroah-Hartman add_uevent_var(envp, num_envp, &i, 15823681e47SGreg Kroah-Hartman buffer, buffer_size, &length, 15923681e47SGreg Kroah-Hartman "MINOR=%u", MINOR(dev->devt)); 16023681e47SGreg Kroah-Hartman } 16123681e47SGreg Kroah-Hartman 162414264f9SKay Sievers if (dev->type && dev->type->name) 163414264f9SKay Sievers add_uevent_var(envp, num_envp, &i, 164414264f9SKay Sievers buffer, buffer_size, &length, 165414264f9SKay Sievers "DEVTYPE=%s", dev->type->name); 166414264f9SKay Sievers 167239378f1SKay Sievers if (dev->driver) 168d81d9d6bSKay Sievers add_uevent_var(envp, num_envp, &i, 169d81d9d6bSKay Sievers buffer, buffer_size, &length, 170d81d9d6bSKay Sievers "DRIVER=%s", dev->driver->name); 171239378f1SKay Sievers 172a87cb2acSKay Sievers #ifdef CONFIG_SYSFS_DEPRECATED 173239378f1SKay Sievers if (dev->class) { 174239378f1SKay Sievers struct device *parent = dev->parent; 175239378f1SKay Sievers 176239378f1SKay Sievers /* find first bus device in parent chain */ 177239378f1SKay Sievers while (parent && !parent->bus) 178239378f1SKay Sievers parent = parent->parent; 179239378f1SKay Sievers if (parent && parent->bus) { 180239378f1SKay Sievers const char *path; 181239378f1SKay Sievers 182239378f1SKay Sievers path = kobject_get_path(&parent->kobj, GFP_KERNEL); 1832c7afd12SKay Sievers if (path) { 184239378f1SKay Sievers add_uevent_var(envp, num_envp, &i, 185239378f1SKay Sievers buffer, buffer_size, &length, 186239378f1SKay Sievers "PHYSDEVPATH=%s", path); 187239378f1SKay Sievers kfree(path); 1882c7afd12SKay Sievers } 189239378f1SKay Sievers 190239378f1SKay Sievers add_uevent_var(envp, num_envp, &i, 191239378f1SKay Sievers buffer, buffer_size, &length, 192239378f1SKay Sievers "PHYSDEVBUS=%s", parent->bus->name); 193239378f1SKay Sievers 194239378f1SKay Sievers if (parent->driver) 195239378f1SKay Sievers add_uevent_var(envp, num_envp, &i, 196239378f1SKay Sievers buffer, buffer_size, &length, 197239378f1SKay Sievers "PHYSDEVDRIVER=%s", parent->driver->name); 198239378f1SKay Sievers } 199239378f1SKay Sievers } else if (dev->bus) { 200239378f1SKay Sievers add_uevent_var(envp, num_envp, &i, 201239378f1SKay Sievers buffer, buffer_size, &length, 202239378f1SKay Sievers "PHYSDEVBUS=%s", dev->bus->name); 203239378f1SKay Sievers 204239378f1SKay Sievers if (dev->driver) 205312c004dSKay Sievers add_uevent_var(envp, num_envp, &i, 2061da177e4SLinus Torvalds buffer, buffer_size, &length, 2071da177e4SLinus Torvalds "PHYSDEVDRIVER=%s", dev->driver->name); 208d81d9d6bSKay Sievers } 209239378f1SKay Sievers #endif 2101da177e4SLinus Torvalds 2111da177e4SLinus Torvalds /* terminate, set to next free slot, shrink available space */ 2121da177e4SLinus Torvalds envp[i] = NULL; 2131da177e4SLinus Torvalds envp = &envp[i]; 2141da177e4SLinus Torvalds num_envp -= i; 2151da177e4SLinus Torvalds buffer = &buffer[length]; 2161da177e4SLinus Torvalds buffer_size -= length; 2171da177e4SLinus Torvalds 218312c004dSKay Sievers if (dev->bus && dev->bus->uevent) { 2191da177e4SLinus Torvalds /* have the bus specific function add its stuff */ 220312c004dSKay Sievers retval = dev->bus->uevent(dev, envp, num_envp, buffer, buffer_size); 221f9f852dfSKay Sievers if (retval) 222f9f852dfSKay Sievers pr_debug ("%s: bus uevent() returned %d\n", 2231da177e4SLinus Torvalds __FUNCTION__, retval); 2241da177e4SLinus Torvalds } 2251da177e4SLinus Torvalds 2262620efefSGreg Kroah-Hartman if (dev->class && dev->class->dev_uevent) { 2272620efefSGreg Kroah-Hartman /* have the class specific function add its stuff */ 2282620efefSGreg Kroah-Hartman retval = dev->class->dev_uevent(dev, envp, num_envp, buffer, buffer_size); 229f9f852dfSKay Sievers if (retval) 230f9f852dfSKay Sievers pr_debug("%s: class uevent() returned %d\n", 2312620efefSGreg Kroah-Hartman __FUNCTION__, retval); 2322620efefSGreg Kroah-Hartman } 233f9f852dfSKay Sievers 234f9f852dfSKay Sievers if (dev->type && dev->type->uevent) { 235f9f852dfSKay Sievers /* have the device type specific fuction add its stuff */ 236f9f852dfSKay Sievers retval = dev->type->uevent(dev, envp, num_envp, buffer, buffer_size); 237f9f852dfSKay Sievers if (retval) 238f9f852dfSKay Sievers pr_debug("%s: dev_type uevent() returned %d\n", 239f9f852dfSKay Sievers __FUNCTION__, retval); 2402620efefSGreg Kroah-Hartman } 2412620efefSGreg Kroah-Hartman 2421da177e4SLinus Torvalds return retval; 2431da177e4SLinus Torvalds } 2441da177e4SLinus Torvalds 245312c004dSKay Sievers static struct kset_uevent_ops device_uevent_ops = { 246312c004dSKay Sievers .filter = dev_uevent_filter, 247312c004dSKay Sievers .name = dev_uevent_name, 248312c004dSKay Sievers .uevent = dev_uevent, 2491da177e4SLinus Torvalds }; 2501da177e4SLinus Torvalds 25116574dccSKay Sievers static ssize_t show_uevent(struct device *dev, struct device_attribute *attr, 25216574dccSKay Sievers char *buf) 25316574dccSKay Sievers { 25416574dccSKay Sievers struct kobject *top_kobj; 25516574dccSKay Sievers struct kset *kset; 25616574dccSKay Sievers char *envp[32]; 257c7308c81SGreg Kroah-Hartman char *data = NULL; 25816574dccSKay Sievers char *pos; 25916574dccSKay Sievers int i; 26016574dccSKay Sievers size_t count = 0; 26116574dccSKay Sievers int retval; 26216574dccSKay Sievers 26316574dccSKay Sievers /* search the kset, the device belongs to */ 26416574dccSKay Sievers top_kobj = &dev->kobj; 26516574dccSKay Sievers if (!top_kobj->kset && top_kobj->parent) { 26616574dccSKay Sievers do { 26716574dccSKay Sievers top_kobj = top_kobj->parent; 26816574dccSKay Sievers } while (!top_kobj->kset && top_kobj->parent); 26916574dccSKay Sievers } 27016574dccSKay Sievers if (!top_kobj->kset) 27116574dccSKay Sievers goto out; 27216574dccSKay Sievers kset = top_kobj->kset; 27316574dccSKay Sievers if (!kset->uevent_ops || !kset->uevent_ops->uevent) 27416574dccSKay Sievers goto out; 27516574dccSKay Sievers 27616574dccSKay Sievers /* respect filter */ 27716574dccSKay Sievers if (kset->uevent_ops && kset->uevent_ops->filter) 27816574dccSKay Sievers if (!kset->uevent_ops->filter(kset, &dev->kobj)) 27916574dccSKay Sievers goto out; 28016574dccSKay Sievers 281c7308c81SGreg Kroah-Hartman data = (char *)get_zeroed_page(GFP_KERNEL); 282c7308c81SGreg Kroah-Hartman if (!data) 283c7308c81SGreg Kroah-Hartman return -ENOMEM; 284c7308c81SGreg Kroah-Hartman 28516574dccSKay Sievers /* let the kset specific function add its keys */ 28616574dccSKay Sievers pos = data; 28716574dccSKay Sievers retval = kset->uevent_ops->uevent(kset, &dev->kobj, 28816574dccSKay Sievers envp, ARRAY_SIZE(envp), 28916574dccSKay Sievers pos, PAGE_SIZE); 29016574dccSKay Sievers if (retval) 29116574dccSKay Sievers goto out; 29216574dccSKay Sievers 29316574dccSKay Sievers /* copy keys to file */ 29416574dccSKay Sievers for (i = 0; envp[i]; i++) { 29516574dccSKay Sievers pos = &buf[count]; 29616574dccSKay Sievers count += sprintf(pos, "%s\n", envp[i]); 29716574dccSKay Sievers } 29816574dccSKay Sievers out: 299c7308c81SGreg Kroah-Hartman free_page((unsigned long)data); 30016574dccSKay Sievers return count; 30116574dccSKay Sievers } 30216574dccSKay Sievers 303a7fd6706SKay Sievers static ssize_t store_uevent(struct device *dev, struct device_attribute *attr, 304a7fd6706SKay Sievers const char *buf, size_t count) 305a7fd6706SKay Sievers { 30622af74f3SKay Sievers if (memcmp(buf, "add", 3) != 0) 30722af74f3SKay Sievers dev_err(dev, "uevent: unsupported action-string; this will " 30822af74f3SKay Sievers "be ignored in a future kernel version"); 309312c004dSKay Sievers kobject_uevent(&dev->kobj, KOBJ_ADD); 310a7fd6706SKay Sievers return count; 311a7fd6706SKay Sievers } 312a7fd6706SKay Sievers 313ad6a1e1cSTejun Heo static struct device_attribute uevent_attr = 314ad6a1e1cSTejun Heo __ATTR(uevent, S_IRUGO | S_IWUSR, show_uevent, store_uevent); 315ad6a1e1cSTejun Heo 316621a1672SDmitry Torokhov static int device_add_attributes(struct device *dev, 317621a1672SDmitry Torokhov struct device_attribute *attrs) 318de0ff00dSGreg Kroah-Hartman { 319de0ff00dSGreg Kroah-Hartman int error = 0; 320621a1672SDmitry Torokhov int i; 321de0ff00dSGreg Kroah-Hartman 322621a1672SDmitry Torokhov if (attrs) { 323621a1672SDmitry Torokhov for (i = 0; attr_name(attrs[i]); i++) { 324621a1672SDmitry Torokhov error = device_create_file(dev, &attrs[i]); 325621a1672SDmitry Torokhov if (error) 326621a1672SDmitry Torokhov break; 327621a1672SDmitry Torokhov } 328621a1672SDmitry Torokhov if (error) 329de0ff00dSGreg Kroah-Hartman while (--i >= 0) 330621a1672SDmitry Torokhov device_remove_file(dev, &attrs[i]); 331de0ff00dSGreg Kroah-Hartman } 332de0ff00dSGreg Kroah-Hartman return error; 333de0ff00dSGreg Kroah-Hartman } 334de0ff00dSGreg Kroah-Hartman 335621a1672SDmitry Torokhov static void device_remove_attributes(struct device *dev, 336621a1672SDmitry Torokhov struct device_attribute *attrs) 337de0ff00dSGreg Kroah-Hartman { 338de0ff00dSGreg Kroah-Hartman int i; 339621a1672SDmitry Torokhov 340621a1672SDmitry Torokhov if (attrs) 341621a1672SDmitry Torokhov for (i = 0; attr_name(attrs[i]); i++) 342621a1672SDmitry Torokhov device_remove_file(dev, &attrs[i]); 343621a1672SDmitry Torokhov } 344621a1672SDmitry Torokhov 345621a1672SDmitry Torokhov static int device_add_groups(struct device *dev, 346621a1672SDmitry Torokhov struct attribute_group **groups) 347621a1672SDmitry Torokhov { 348621a1672SDmitry Torokhov int error = 0; 349621a1672SDmitry Torokhov int i; 350621a1672SDmitry Torokhov 351621a1672SDmitry Torokhov if (groups) { 352621a1672SDmitry Torokhov for (i = 0; groups[i]; i++) { 353621a1672SDmitry Torokhov error = sysfs_create_group(&dev->kobj, groups[i]); 354621a1672SDmitry Torokhov if (error) { 355621a1672SDmitry Torokhov while (--i >= 0) 356621a1672SDmitry Torokhov sysfs_remove_group(&dev->kobj, groups[i]); 357621a1672SDmitry Torokhov break; 358de0ff00dSGreg Kroah-Hartman } 359de0ff00dSGreg Kroah-Hartman } 360de0ff00dSGreg Kroah-Hartman } 361621a1672SDmitry Torokhov return error; 362621a1672SDmitry Torokhov } 363621a1672SDmitry Torokhov 364621a1672SDmitry Torokhov static void device_remove_groups(struct device *dev, 365621a1672SDmitry Torokhov struct attribute_group **groups) 366621a1672SDmitry Torokhov { 367621a1672SDmitry Torokhov int i; 368621a1672SDmitry Torokhov 369621a1672SDmitry Torokhov if (groups) 370621a1672SDmitry Torokhov for (i = 0; groups[i]; i++) 371621a1672SDmitry Torokhov sysfs_remove_group(&dev->kobj, groups[i]); 372621a1672SDmitry Torokhov } 373de0ff00dSGreg Kroah-Hartman 3742620efefSGreg Kroah-Hartman static int device_add_attrs(struct device *dev) 3752620efefSGreg Kroah-Hartman { 3762620efefSGreg Kroah-Hartman struct class *class = dev->class; 377f9f852dfSKay Sievers struct device_type *type = dev->type; 378621a1672SDmitry Torokhov int error; 3792620efefSGreg Kroah-Hartman 380621a1672SDmitry Torokhov if (class) { 381621a1672SDmitry Torokhov error = device_add_attributes(dev, class->dev_attrs); 3822620efefSGreg Kroah-Hartman if (error) 383621a1672SDmitry Torokhov return error; 384f9f852dfSKay Sievers } 385f9f852dfSKay Sievers 386621a1672SDmitry Torokhov if (type) { 387621a1672SDmitry Torokhov error = device_add_groups(dev, type->groups); 388f9f852dfSKay Sievers if (error) 389621a1672SDmitry Torokhov goto err_remove_class_attrs; 390f9f852dfSKay Sievers } 391621a1672SDmitry Torokhov 392621a1672SDmitry Torokhov error = device_add_groups(dev, dev->groups); 393f9f852dfSKay Sievers if (error) 394621a1672SDmitry Torokhov goto err_remove_type_groups; 395621a1672SDmitry Torokhov 396621a1672SDmitry Torokhov return 0; 397621a1672SDmitry Torokhov 398621a1672SDmitry Torokhov err_remove_type_groups: 399621a1672SDmitry Torokhov if (type) 400621a1672SDmitry Torokhov device_remove_groups(dev, type->groups); 401621a1672SDmitry Torokhov err_remove_class_attrs: 402621a1672SDmitry Torokhov if (class) 403621a1672SDmitry Torokhov device_remove_attributes(dev, class->dev_attrs); 404f9f852dfSKay Sievers 4052620efefSGreg Kroah-Hartman return error; 4062620efefSGreg Kroah-Hartman } 4072620efefSGreg Kroah-Hartman 4082620efefSGreg Kroah-Hartman static void device_remove_attrs(struct device *dev) 4092620efefSGreg Kroah-Hartman { 4102620efefSGreg Kroah-Hartman struct class *class = dev->class; 411f9f852dfSKay Sievers struct device_type *type = dev->type; 4122620efefSGreg Kroah-Hartman 413621a1672SDmitry Torokhov device_remove_groups(dev, dev->groups); 414f9f852dfSKay Sievers 415621a1672SDmitry Torokhov if (type) 416621a1672SDmitry Torokhov device_remove_groups(dev, type->groups); 417621a1672SDmitry Torokhov 418621a1672SDmitry Torokhov if (class) 419621a1672SDmitry Torokhov device_remove_attributes(dev, class->dev_attrs); 4202620efefSGreg Kroah-Hartman } 4212620efefSGreg Kroah-Hartman 4222620efefSGreg Kroah-Hartman 42323681e47SGreg Kroah-Hartman static ssize_t show_dev(struct device *dev, struct device_attribute *attr, 42423681e47SGreg Kroah-Hartman char *buf) 42523681e47SGreg Kroah-Hartman { 42623681e47SGreg Kroah-Hartman return print_dev_t(buf, dev->devt); 42723681e47SGreg Kroah-Hartman } 42823681e47SGreg Kroah-Hartman 429ad6a1e1cSTejun Heo static struct device_attribute devt_attr = 430ad6a1e1cSTejun Heo __ATTR(dev, S_IRUGO, show_dev, NULL); 431ad6a1e1cSTejun Heo 4320863afb3SMartin Waitz /* 4330863afb3SMartin Waitz * devices_subsys - structure to be registered with kobject core. 4341da177e4SLinus Torvalds */ 4351da177e4SLinus Torvalds 436312c004dSKay Sievers decl_subsys(devices, &ktype_device, &device_uevent_ops); 4371da177e4SLinus Torvalds 4381da177e4SLinus Torvalds 4391da177e4SLinus Torvalds /** 4401da177e4SLinus Torvalds * device_create_file - create sysfs attribute file for device. 4411da177e4SLinus Torvalds * @dev: device. 4421da177e4SLinus Torvalds * @attr: device attribute descriptor. 4431da177e4SLinus Torvalds */ 4441da177e4SLinus Torvalds 4451da177e4SLinus Torvalds int device_create_file(struct device * dev, struct device_attribute * attr) 4461da177e4SLinus Torvalds { 4471da177e4SLinus Torvalds int error = 0; 4481da177e4SLinus Torvalds if (get_device(dev)) { 4491da177e4SLinus Torvalds error = sysfs_create_file(&dev->kobj, &attr->attr); 4501da177e4SLinus Torvalds put_device(dev); 4511da177e4SLinus Torvalds } 4521da177e4SLinus Torvalds return error; 4531da177e4SLinus Torvalds } 4541da177e4SLinus Torvalds 4551da177e4SLinus Torvalds /** 4561da177e4SLinus Torvalds * device_remove_file - remove sysfs attribute file. 4571da177e4SLinus Torvalds * @dev: device. 4581da177e4SLinus Torvalds * @attr: device attribute descriptor. 4591da177e4SLinus Torvalds */ 4601da177e4SLinus Torvalds 4611da177e4SLinus Torvalds void device_remove_file(struct device * dev, struct device_attribute * attr) 4621da177e4SLinus Torvalds { 4631da177e4SLinus Torvalds if (get_device(dev)) { 4641da177e4SLinus Torvalds sysfs_remove_file(&dev->kobj, &attr->attr); 4651da177e4SLinus Torvalds put_device(dev); 4661da177e4SLinus Torvalds } 4671da177e4SLinus Torvalds } 4681da177e4SLinus Torvalds 4692589f188SGreg Kroah-Hartman /** 4702589f188SGreg Kroah-Hartman * device_create_bin_file - create sysfs binary attribute file for device. 4712589f188SGreg Kroah-Hartman * @dev: device. 4722589f188SGreg Kroah-Hartman * @attr: device binary attribute descriptor. 4732589f188SGreg Kroah-Hartman */ 4742589f188SGreg Kroah-Hartman int device_create_bin_file(struct device *dev, struct bin_attribute *attr) 4752589f188SGreg Kroah-Hartman { 4762589f188SGreg Kroah-Hartman int error = -EINVAL; 4772589f188SGreg Kroah-Hartman if (dev) 4782589f188SGreg Kroah-Hartman error = sysfs_create_bin_file(&dev->kobj, attr); 4792589f188SGreg Kroah-Hartman return error; 4802589f188SGreg Kroah-Hartman } 4812589f188SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(device_create_bin_file); 4822589f188SGreg Kroah-Hartman 4832589f188SGreg Kroah-Hartman /** 4842589f188SGreg Kroah-Hartman * device_remove_bin_file - remove sysfs binary attribute file 4852589f188SGreg Kroah-Hartman * @dev: device. 4862589f188SGreg Kroah-Hartman * @attr: device binary attribute descriptor. 4872589f188SGreg Kroah-Hartman */ 4882589f188SGreg Kroah-Hartman void device_remove_bin_file(struct device *dev, struct bin_attribute *attr) 4892589f188SGreg Kroah-Hartman { 4902589f188SGreg Kroah-Hartman if (dev) 4912589f188SGreg Kroah-Hartman sysfs_remove_bin_file(&dev->kobj, attr); 4922589f188SGreg Kroah-Hartman } 4932589f188SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(device_remove_bin_file); 4942589f188SGreg Kroah-Hartman 495d9a9cdfbSAlan Stern /** 496523ded71SAlan Stern * device_schedule_callback_owner - helper to schedule a callback for a device 497d9a9cdfbSAlan Stern * @dev: device. 498d9a9cdfbSAlan Stern * @func: callback function to invoke later. 499523ded71SAlan Stern * @owner: module owning the callback routine 500d9a9cdfbSAlan Stern * 501d9a9cdfbSAlan Stern * Attribute methods must not unregister themselves or their parent device 502d9a9cdfbSAlan Stern * (which would amount to the same thing). Attempts to do so will deadlock, 503d9a9cdfbSAlan Stern * since unregistration is mutually exclusive with driver callbacks. 504d9a9cdfbSAlan Stern * 505d9a9cdfbSAlan Stern * Instead methods can call this routine, which will attempt to allocate 506d9a9cdfbSAlan Stern * and schedule a workqueue request to call back @func with @dev as its 507d9a9cdfbSAlan Stern * argument in the workqueue's process context. @dev will be pinned until 508d9a9cdfbSAlan Stern * @func returns. 509d9a9cdfbSAlan Stern * 510523ded71SAlan Stern * This routine is usually called via the inline device_schedule_callback(), 511523ded71SAlan Stern * which automatically sets @owner to THIS_MODULE. 512523ded71SAlan Stern * 513d9a9cdfbSAlan Stern * Returns 0 if the request was submitted, -ENOMEM if storage could not 514523ded71SAlan Stern * be allocated, -ENODEV if a reference to @owner isn't available. 515d9a9cdfbSAlan Stern * 516d9a9cdfbSAlan Stern * NOTE: This routine won't work if CONFIG_SYSFS isn't set! It uses an 517d9a9cdfbSAlan Stern * underlying sysfs routine (since it is intended for use by attribute 518d9a9cdfbSAlan Stern * methods), and if sysfs isn't available you'll get nothing but -ENOSYS. 519d9a9cdfbSAlan Stern */ 520523ded71SAlan Stern int device_schedule_callback_owner(struct device *dev, 521523ded71SAlan Stern void (*func)(struct device *), struct module *owner) 522d9a9cdfbSAlan Stern { 523d9a9cdfbSAlan Stern return sysfs_schedule_callback(&dev->kobj, 524523ded71SAlan Stern (void (*)(void *)) func, dev, owner); 525d9a9cdfbSAlan Stern } 526523ded71SAlan Stern EXPORT_SYMBOL_GPL(device_schedule_callback_owner); 527d9a9cdfbSAlan Stern 52834bb61f9SJames Bottomley static void klist_children_get(struct klist_node *n) 52934bb61f9SJames Bottomley { 53034bb61f9SJames Bottomley struct device *dev = container_of(n, struct device, knode_parent); 53134bb61f9SJames Bottomley 53234bb61f9SJames Bottomley get_device(dev); 53334bb61f9SJames Bottomley } 53434bb61f9SJames Bottomley 53534bb61f9SJames Bottomley static void klist_children_put(struct klist_node *n) 53634bb61f9SJames Bottomley { 53734bb61f9SJames Bottomley struct device *dev = container_of(n, struct device, knode_parent); 53834bb61f9SJames Bottomley 53934bb61f9SJames Bottomley put_device(dev); 54034bb61f9SJames Bottomley } 54134bb61f9SJames Bottomley 5421da177e4SLinus Torvalds 5431da177e4SLinus Torvalds /** 5441da177e4SLinus Torvalds * device_initialize - init device structure. 5451da177e4SLinus Torvalds * @dev: device. 5461da177e4SLinus Torvalds * 5471da177e4SLinus Torvalds * This prepares the device for use by other layers, 5481da177e4SLinus Torvalds * including adding it to the device hierarchy. 5491da177e4SLinus Torvalds * It is the first half of device_register(), if called by 5501da177e4SLinus Torvalds * that, though it can also be called separately, so one 5511da177e4SLinus Torvalds * may use @dev's fields (e.g. the refcount). 5521da177e4SLinus Torvalds */ 5531da177e4SLinus Torvalds 5541da177e4SLinus Torvalds void device_initialize(struct device *dev) 5551da177e4SLinus Torvalds { 5561da177e4SLinus Torvalds kobj_set_kset_s(dev, devices_subsys); 5571da177e4SLinus Torvalds kobject_init(&dev->kobj); 55834bb61f9SJames Bottomley klist_init(&dev->klist_children, klist_children_get, 55934bb61f9SJames Bottomley klist_children_put); 5601da177e4SLinus Torvalds INIT_LIST_HEAD(&dev->dma_pools); 56123681e47SGreg Kroah-Hartman INIT_LIST_HEAD(&dev->node); 562af70316aSmochel@digitalimplant.org init_MUTEX(&dev->sem); 5639ac7849eSTejun Heo spin_lock_init(&dev->devres_lock); 5649ac7849eSTejun Heo INIT_LIST_HEAD(&dev->devres_head); 5650ac85241SDavid Brownell device_init_wakeup(dev, 0); 56687348136SChristoph Hellwig set_dev_node(dev, -1); 5671da177e4SLinus Torvalds } 5681da177e4SLinus Torvalds 56940fa5422SGreg Kroah-Hartman #ifdef CONFIG_SYSFS_DEPRECATED 570c744aeaeSCornelia Huck static struct kobject * get_device_parent(struct device *dev, 571c744aeaeSCornelia Huck struct device *parent) 57240fa5422SGreg Kroah-Hartman { 57340fa5422SGreg Kroah-Hartman /* Set the parent to the class, not the parent device */ 57440fa5422SGreg Kroah-Hartman /* this keeps sysfs from having a symlink to make old udevs happy */ 57540fa5422SGreg Kroah-Hartman if (dev->class) 576823bccfcSGreg Kroah-Hartman return &dev->class->subsys.kobj; 57740fa5422SGreg Kroah-Hartman else if (parent) 578c744aeaeSCornelia Huck return &parent->kobj; 57940fa5422SGreg Kroah-Hartman 580c744aeaeSCornelia Huck return NULL; 58140fa5422SGreg Kroah-Hartman } 58240fa5422SGreg Kroah-Hartman #else 583c744aeaeSCornelia Huck static struct kobject *virtual_device_parent(struct device *dev) 584f0ee61a6SGreg Kroah-Hartman { 585f0ee61a6SGreg Kroah-Hartman static struct kobject *virtual_dir = NULL; 586f0ee61a6SGreg Kroah-Hartman 587f0ee61a6SGreg Kroah-Hartman if (!virtual_dir) 588823bccfcSGreg Kroah-Hartman virtual_dir = kobject_add_dir(&devices_subsys.kobj, "virtual"); 589f0ee61a6SGreg Kroah-Hartman 59086406245SKay Sievers return virtual_dir; 591f0ee61a6SGreg Kroah-Hartman } 592f0ee61a6SGreg Kroah-Hartman 593c744aeaeSCornelia Huck static struct kobject * get_device_parent(struct device *dev, 594c744aeaeSCornelia Huck struct device *parent) 59540fa5422SGreg Kroah-Hartman { 59686406245SKay Sievers if (dev->class) { 59786406245SKay Sievers struct kobject *kobj = NULL; 59886406245SKay Sievers struct kobject *parent_kobj; 59986406245SKay Sievers struct kobject *k; 60086406245SKay Sievers 60186406245SKay Sievers /* 60286406245SKay Sievers * If we have no parent, we live in "virtual". 60386406245SKay Sievers * Class-devices with a bus-device as parent, live 60486406245SKay Sievers * in a class-directory to prevent namespace collisions. 60586406245SKay Sievers */ 60686406245SKay Sievers if (parent == NULL) 60786406245SKay Sievers parent_kobj = virtual_device_parent(dev); 60886406245SKay Sievers else if (parent->class) 60986406245SKay Sievers return &parent->kobj; 61086406245SKay Sievers else 61186406245SKay Sievers parent_kobj = &parent->kobj; 61286406245SKay Sievers 61386406245SKay Sievers /* find our class-directory at the parent and reference it */ 61486406245SKay Sievers spin_lock(&dev->class->class_dirs.list_lock); 61586406245SKay Sievers list_for_each_entry(k, &dev->class->class_dirs.list, entry) 61686406245SKay Sievers if (k->parent == parent_kobj) { 61786406245SKay Sievers kobj = kobject_get(k); 61886406245SKay Sievers break; 61986406245SKay Sievers } 62086406245SKay Sievers spin_unlock(&dev->class->class_dirs.list_lock); 62186406245SKay Sievers if (kobj) 62286406245SKay Sievers return kobj; 62386406245SKay Sievers 62486406245SKay Sievers /* or create a new class-directory at the parent device */ 62586406245SKay Sievers return kobject_kset_add_dir(&dev->class->class_dirs, 62686406245SKay Sievers parent_kobj, dev->class->name); 62786406245SKay Sievers } 62886406245SKay Sievers 62986406245SKay Sievers if (parent) 630c744aeaeSCornelia Huck return &parent->kobj; 631c744aeaeSCornelia Huck return NULL; 632c744aeaeSCornelia Huck } 633c744aeaeSCornelia Huck #endif 63486406245SKay Sievers 635c744aeaeSCornelia Huck static int setup_parent(struct device *dev, struct device *parent) 636c744aeaeSCornelia Huck { 637c744aeaeSCornelia Huck struct kobject *kobj; 638c744aeaeSCornelia Huck kobj = get_device_parent(dev, parent); 639c744aeaeSCornelia Huck if (IS_ERR(kobj)) 640c744aeaeSCornelia Huck return PTR_ERR(kobj); 641c744aeaeSCornelia Huck if (kobj) 642c744aeaeSCornelia Huck dev->kobj.parent = kobj; 64340fa5422SGreg Kroah-Hartman return 0; 64440fa5422SGreg Kroah-Hartman } 64540fa5422SGreg Kroah-Hartman 6461da177e4SLinus Torvalds /** 6471da177e4SLinus Torvalds * device_add - add device to device hierarchy. 6481da177e4SLinus Torvalds * @dev: device. 6491da177e4SLinus Torvalds * 6501da177e4SLinus Torvalds * This is part 2 of device_register(), though may be called 6511da177e4SLinus Torvalds * separately _iff_ device_initialize() has been called separately. 6521da177e4SLinus Torvalds * 6531da177e4SLinus Torvalds * This adds it to the kobject hierarchy via kobject_add(), adds it 6541da177e4SLinus Torvalds * to the global and sibling lists for the device, then 6551da177e4SLinus Torvalds * adds it to the other relevant subsystems of the driver model. 6561da177e4SLinus Torvalds */ 6571da177e4SLinus Torvalds int device_add(struct device *dev) 6581da177e4SLinus Torvalds { 6591da177e4SLinus Torvalds struct device *parent = NULL; 660e9a7d305SGreg Kroah-Hartman char *class_name = NULL; 661c47ed219SGreg Kroah-Hartman struct class_interface *class_intf; 6621da177e4SLinus Torvalds int error = -EINVAL; 6631da177e4SLinus Torvalds 6641da177e4SLinus Torvalds dev = get_device(dev); 6651da177e4SLinus Torvalds if (!dev || !strlen(dev->bus_id)) 6661da177e4SLinus Torvalds goto Error; 6671da177e4SLinus Torvalds 66840fa5422SGreg Kroah-Hartman pr_debug("DEV: registering device: ID = '%s'\n", dev->bus_id); 669c205ef48SGreg Kroah-Hartman 6701da177e4SLinus Torvalds parent = get_device(dev->parent); 67140fa5422SGreg Kroah-Hartman error = setup_parent(dev, parent); 67240fa5422SGreg Kroah-Hartman if (error) 67340fa5422SGreg Kroah-Hartman goto Error; 6741da177e4SLinus Torvalds 6751da177e4SLinus Torvalds /* first, register with generic layer. */ 6761da177e4SLinus Torvalds kobject_set_name(&dev->kobj, "%s", dev->bus_id); 67740fa5422SGreg Kroah-Hartman error = kobject_add(&dev->kobj); 67840fa5422SGreg Kroah-Hartman if (error) 6791da177e4SLinus Torvalds goto Error; 680a7fd6706SKay Sievers 68137022644SBrian Walsh /* notify platform of device entry */ 68237022644SBrian Walsh if (platform_notify) 68337022644SBrian Walsh platform_notify(dev); 68437022644SBrian Walsh 685116af378SBenjamin Herrenschmidt /* notify clients of device entry (new way) */ 686116af378SBenjamin Herrenschmidt if (dev->bus) 687116af378SBenjamin Herrenschmidt blocking_notifier_call_chain(&dev->bus->bus_notifier, 688116af378SBenjamin Herrenschmidt BUS_NOTIFY_ADD_DEVICE, dev); 689116af378SBenjamin Herrenschmidt 690ad6a1e1cSTejun Heo error = device_create_file(dev, &uevent_attr); 691a306eea4SCornelia Huck if (error) 692a306eea4SCornelia Huck goto attrError; 693a7fd6706SKay Sievers 69423681e47SGreg Kroah-Hartman if (MAJOR(dev->devt)) { 695ad6a1e1cSTejun Heo error = device_create_file(dev, &devt_attr); 696ad6a1e1cSTejun Heo if (error) 697a306eea4SCornelia Huck goto ueventattrError; 69823681e47SGreg Kroah-Hartman } 69923681e47SGreg Kroah-Hartman 700b9d9c82bSKay Sievers if (dev->class) { 701823bccfcSGreg Kroah-Hartman sysfs_create_link(&dev->kobj, &dev->class->subsys.kobj, 702b9d9c82bSKay Sievers "subsystem"); 70340fa5422SGreg Kroah-Hartman /* If this is not a "fake" compatible device, then create the 70440fa5422SGreg Kroah-Hartman * symlink from the class to the device. */ 705823bccfcSGreg Kroah-Hartman if (dev->kobj.parent != &dev->class->subsys.kobj) 706823bccfcSGreg Kroah-Hartman sysfs_create_link(&dev->class->subsys.kobj, 70740fa5422SGreg Kroah-Hartman &dev->kobj, dev->bus_id); 70864bb5d2cSGreg Kroah-Hartman if (parent) { 709cb360bbfSCornelia Huck sysfs_create_link(&dev->kobj, &dev->parent->kobj, 710cb360bbfSCornelia Huck "device"); 711f7f3461dSGreg Kroah-Hartman #ifdef CONFIG_SYSFS_DEPRECATED 712cb360bbfSCornelia Huck class_name = make_class_name(dev->class->name, 713cb360bbfSCornelia Huck &dev->kobj); 714cb360bbfSCornelia Huck if (class_name) 715cb360bbfSCornelia Huck sysfs_create_link(&dev->parent->kobj, 716cb360bbfSCornelia Huck &dev->kobj, class_name); 71799ef3ef8SKay Sievers #endif 71864bb5d2cSGreg Kroah-Hartman } 719f7f3461dSGreg Kroah-Hartman } 72023681e47SGreg Kroah-Hartman 721dc0afa83SCornelia Huck error = device_add_attrs(dev); 722dc0afa83SCornelia Huck if (error) 7232620efefSGreg Kroah-Hartman goto AttrsError; 724dc0afa83SCornelia Huck error = device_pm_add(dev); 725dc0afa83SCornelia Huck if (error) 7261da177e4SLinus Torvalds goto PMError; 727dc0afa83SCornelia Huck error = bus_add_device(dev); 728dc0afa83SCornelia Huck if (error) 7291da177e4SLinus Torvalds goto BusError; 73053877d06SKay Sievers kobject_uevent(&dev->kobj, KOBJ_ADD); 731c6a46696SCornelia Huck bus_attach_device(dev); 7321da177e4SLinus Torvalds if (parent) 733d856f1e3SJames Bottomley klist_add_tail(&dev->knode_parent, &parent->klist_children); 7341da177e4SLinus Torvalds 7355d9fd169SGreg Kroah-Hartman if (dev->class) { 7365d9fd169SGreg Kroah-Hartman down(&dev->class->sem); 737c47ed219SGreg Kroah-Hartman /* tie the class to the device */ 7385d9fd169SGreg Kroah-Hartman list_add_tail(&dev->node, &dev->class->devices); 739c47ed219SGreg Kroah-Hartman 740c47ed219SGreg Kroah-Hartman /* notify any interfaces that the device is here */ 741c47ed219SGreg Kroah-Hartman list_for_each_entry(class_intf, &dev->class->interfaces, node) 742c47ed219SGreg Kroah-Hartman if (class_intf->add_dev) 743c47ed219SGreg Kroah-Hartman class_intf->add_dev(dev, class_intf); 7445d9fd169SGreg Kroah-Hartman up(&dev->class->sem); 7455d9fd169SGreg Kroah-Hartman } 7461da177e4SLinus Torvalds Done: 747e9a7d305SGreg Kroah-Hartman kfree(class_name); 7481da177e4SLinus Torvalds put_device(dev); 7491da177e4SLinus Torvalds return error; 7501da177e4SLinus Torvalds BusError: 7511da177e4SLinus Torvalds device_pm_remove(dev); 7521da177e4SLinus Torvalds PMError: 753116af378SBenjamin Herrenschmidt if (dev->bus) 754116af378SBenjamin Herrenschmidt blocking_notifier_call_chain(&dev->bus->bus_notifier, 755116af378SBenjamin Herrenschmidt BUS_NOTIFY_DEL_DEVICE, dev); 7562620efefSGreg Kroah-Hartman device_remove_attrs(dev); 7572620efefSGreg Kroah-Hartman AttrsError: 758ad6a1e1cSTejun Heo if (MAJOR(dev->devt)) 759ad6a1e1cSTejun Heo device_remove_file(dev, &devt_attr); 76082f0cf9bSJames Simmons 76182f0cf9bSJames Simmons if (dev->class) { 76282f0cf9bSJames Simmons sysfs_remove_link(&dev->kobj, "subsystem"); 76382f0cf9bSJames Simmons /* If this is not a "fake" compatible device, remove the 76482f0cf9bSJames Simmons * symlink from the class to the device. */ 765823bccfcSGreg Kroah-Hartman if (dev->kobj.parent != &dev->class->subsys.kobj) 766823bccfcSGreg Kroah-Hartman sysfs_remove_link(&dev->class->subsys.kobj, 76782f0cf9bSJames Simmons dev->bus_id); 76882f0cf9bSJames Simmons if (parent) { 769f7f3461dSGreg Kroah-Hartman #ifdef CONFIG_SYSFS_DEPRECATED 77082f0cf9bSJames Simmons char *class_name = make_class_name(dev->class->name, 77182f0cf9bSJames Simmons &dev->kobj); 77282f0cf9bSJames Simmons if (class_name) 77382f0cf9bSJames Simmons sysfs_remove_link(&dev->parent->kobj, 77482f0cf9bSJames Simmons class_name); 77582f0cf9bSJames Simmons kfree(class_name); 776f7f3461dSGreg Kroah-Hartman #endif 77782f0cf9bSJames Simmons sysfs_remove_link(&dev->kobj, "device"); 77882f0cf9bSJames Simmons } 77923681e47SGreg Kroah-Hartman } 780a306eea4SCornelia Huck ueventattrError: 781ad6a1e1cSTejun Heo device_remove_file(dev, &uevent_attr); 78223681e47SGreg Kroah-Hartman attrError: 783312c004dSKay Sievers kobject_uevent(&dev->kobj, KOBJ_REMOVE); 7841da177e4SLinus Torvalds kobject_del(&dev->kobj); 7851da177e4SLinus Torvalds Error: 7861da177e4SLinus Torvalds if (parent) 7871da177e4SLinus Torvalds put_device(parent); 7881da177e4SLinus Torvalds goto Done; 7891da177e4SLinus Torvalds } 7901da177e4SLinus Torvalds 7911da177e4SLinus Torvalds 7921da177e4SLinus Torvalds /** 7931da177e4SLinus Torvalds * device_register - register a device with the system. 7941da177e4SLinus Torvalds * @dev: pointer to the device structure 7951da177e4SLinus Torvalds * 7961da177e4SLinus Torvalds * This happens in two clean steps - initialize the device 7971da177e4SLinus Torvalds * and add it to the system. The two steps can be called 7981da177e4SLinus Torvalds * separately, but this is the easiest and most common. 7991da177e4SLinus Torvalds * I.e. you should only call the two helpers separately if 8001da177e4SLinus Torvalds * have a clearly defined need to use and refcount the device 8011da177e4SLinus Torvalds * before it is added to the hierarchy. 8021da177e4SLinus Torvalds */ 8031da177e4SLinus Torvalds 8041da177e4SLinus Torvalds int device_register(struct device *dev) 8051da177e4SLinus Torvalds { 8061da177e4SLinus Torvalds device_initialize(dev); 8071da177e4SLinus Torvalds return device_add(dev); 8081da177e4SLinus Torvalds } 8091da177e4SLinus Torvalds 8101da177e4SLinus Torvalds 8111da177e4SLinus Torvalds /** 8121da177e4SLinus Torvalds * get_device - increment reference count for device. 8131da177e4SLinus Torvalds * @dev: device. 8141da177e4SLinus Torvalds * 8151da177e4SLinus Torvalds * This simply forwards the call to kobject_get(), though 8161da177e4SLinus Torvalds * we do take care to provide for the case that we get a NULL 8171da177e4SLinus Torvalds * pointer passed in. 8181da177e4SLinus Torvalds */ 8191da177e4SLinus Torvalds 8201da177e4SLinus Torvalds struct device * get_device(struct device * dev) 8211da177e4SLinus Torvalds { 8221da177e4SLinus Torvalds return dev ? to_dev(kobject_get(&dev->kobj)) : NULL; 8231da177e4SLinus Torvalds } 8241da177e4SLinus Torvalds 8251da177e4SLinus Torvalds 8261da177e4SLinus Torvalds /** 8271da177e4SLinus Torvalds * put_device - decrement reference count. 8281da177e4SLinus Torvalds * @dev: device in question. 8291da177e4SLinus Torvalds */ 8301da177e4SLinus Torvalds void put_device(struct device * dev) 8311da177e4SLinus Torvalds { 8321da177e4SLinus Torvalds if (dev) 8331da177e4SLinus Torvalds kobject_put(&dev->kobj); 8341da177e4SLinus Torvalds } 8351da177e4SLinus Torvalds 8361da177e4SLinus Torvalds 8371da177e4SLinus Torvalds /** 8381da177e4SLinus Torvalds * device_del - delete device from system. 8391da177e4SLinus Torvalds * @dev: device. 8401da177e4SLinus Torvalds * 8411da177e4SLinus Torvalds * This is the first part of the device unregistration 8421da177e4SLinus Torvalds * sequence. This removes the device from the lists we control 8431da177e4SLinus Torvalds * from here, has it removed from the other driver model 8441da177e4SLinus Torvalds * subsystems it was added to in device_add(), and removes it 8451da177e4SLinus Torvalds * from the kobject hierarchy. 8461da177e4SLinus Torvalds * 8471da177e4SLinus Torvalds * NOTE: this should be called manually _iff_ device_add() was 8481da177e4SLinus Torvalds * also called manually. 8491da177e4SLinus Torvalds */ 8501da177e4SLinus Torvalds 8511da177e4SLinus Torvalds void device_del(struct device * dev) 8521da177e4SLinus Torvalds { 8531da177e4SLinus Torvalds struct device * parent = dev->parent; 854c47ed219SGreg Kroah-Hartman struct class_interface *class_intf; 8551da177e4SLinus Torvalds 8561da177e4SLinus Torvalds if (parent) 857d62c0f9fSPatrick Mochel klist_del(&dev->knode_parent); 858ad6a1e1cSTejun Heo if (MAJOR(dev->devt)) 859ad6a1e1cSTejun Heo device_remove_file(dev, &devt_attr); 860b9d9c82bSKay Sievers if (dev->class) { 861b9d9c82bSKay Sievers sysfs_remove_link(&dev->kobj, "subsystem"); 86240fa5422SGreg Kroah-Hartman /* If this is not a "fake" compatible device, remove the 86340fa5422SGreg Kroah-Hartman * symlink from the class to the device. */ 864823bccfcSGreg Kroah-Hartman if (dev->kobj.parent != &dev->class->subsys.kobj) 865823bccfcSGreg Kroah-Hartman sysfs_remove_link(&dev->class->subsys.kobj, 86640fa5422SGreg Kroah-Hartman dev->bus_id); 86764bb5d2cSGreg Kroah-Hartman if (parent) { 868f7f3461dSGreg Kroah-Hartman #ifdef CONFIG_SYSFS_DEPRECATED 86999ef3ef8SKay Sievers char *class_name = make_class_name(dev->class->name, 87099ef3ef8SKay Sievers &dev->kobj); 871cb360bbfSCornelia Huck if (class_name) 872cb360bbfSCornelia Huck sysfs_remove_link(&dev->parent->kobj, 873cb360bbfSCornelia Huck class_name); 874e9a7d305SGreg Kroah-Hartman kfree(class_name); 875f7f3461dSGreg Kroah-Hartman #endif 87699ef3ef8SKay Sievers sysfs_remove_link(&dev->kobj, "device"); 87799ef3ef8SKay Sievers } 87899ef3ef8SKay Sievers 8795d9fd169SGreg Kroah-Hartman down(&dev->class->sem); 880c47ed219SGreg Kroah-Hartman /* notify any interfaces that the device is now gone */ 881c47ed219SGreg Kroah-Hartman list_for_each_entry(class_intf, &dev->class->interfaces, node) 882c47ed219SGreg Kroah-Hartman if (class_intf->remove_dev) 883c47ed219SGreg Kroah-Hartman class_intf->remove_dev(dev, class_intf); 884c47ed219SGreg Kroah-Hartman /* remove the device from the class list */ 8855d9fd169SGreg Kroah-Hartman list_del_init(&dev->node); 8865d9fd169SGreg Kroah-Hartman up(&dev->class->sem); 88786406245SKay Sievers 88886406245SKay Sievers /* If we live in a parent class-directory, unreference it */ 88986406245SKay Sievers if (dev->kobj.parent->kset == &dev->class->class_dirs) { 89086406245SKay Sievers struct device *d; 89186406245SKay Sievers int other = 0; 89286406245SKay Sievers 89386406245SKay Sievers /* 89486406245SKay Sievers * if we are the last child of our class, delete 89586406245SKay Sievers * our class-directory at this parent 89686406245SKay Sievers */ 89786406245SKay Sievers down(&dev->class->sem); 89886406245SKay Sievers list_for_each_entry(d, &dev->class->devices, node) { 89986406245SKay Sievers if (d == dev) 90086406245SKay Sievers continue; 90186406245SKay Sievers if (d->kobj.parent == dev->kobj.parent) { 90286406245SKay Sievers other = 1; 90386406245SKay Sievers break; 90486406245SKay Sievers } 90586406245SKay Sievers } 90686406245SKay Sievers if (!other) 90786406245SKay Sievers kobject_del(dev->kobj.parent); 90886406245SKay Sievers 90986406245SKay Sievers kobject_put(dev->kobj.parent); 91086406245SKay Sievers up(&dev->class->sem); 91186406245SKay Sievers } 912b9d9c82bSKay Sievers } 913ad6a1e1cSTejun Heo device_remove_file(dev, &uevent_attr); 9142620efefSGreg Kroah-Hartman device_remove_attrs(dev); 91528953533SBenjamin Herrenschmidt bus_remove_device(dev); 9161da177e4SLinus Torvalds 9172f8d16a9STejun Heo /* 9182f8d16a9STejun Heo * Some platform devices are driven without driver attached 9192f8d16a9STejun Heo * and managed resources may have been acquired. Make sure 9202f8d16a9STejun Heo * all resources are released. 9212f8d16a9STejun Heo */ 9222f8d16a9STejun Heo devres_release_all(dev); 9232f8d16a9STejun Heo 9241da177e4SLinus Torvalds /* Notify the platform of the removal, in case they 9251da177e4SLinus Torvalds * need to do anything... 9261da177e4SLinus Torvalds */ 9271da177e4SLinus Torvalds if (platform_notify_remove) 9281da177e4SLinus Torvalds platform_notify_remove(dev); 929116af378SBenjamin Herrenschmidt if (dev->bus) 930116af378SBenjamin Herrenschmidt blocking_notifier_call_chain(&dev->bus->bus_notifier, 931116af378SBenjamin Herrenschmidt BUS_NOTIFY_DEL_DEVICE, dev); 9321da177e4SLinus Torvalds device_pm_remove(dev); 933312c004dSKay Sievers kobject_uevent(&dev->kobj, KOBJ_REMOVE); 9341da177e4SLinus Torvalds kobject_del(&dev->kobj); 9351da177e4SLinus Torvalds if (parent) 9361da177e4SLinus Torvalds put_device(parent); 9371da177e4SLinus Torvalds } 9381da177e4SLinus Torvalds 9391da177e4SLinus Torvalds /** 9401da177e4SLinus Torvalds * device_unregister - unregister device from system. 9411da177e4SLinus Torvalds * @dev: device going away. 9421da177e4SLinus Torvalds * 9431da177e4SLinus Torvalds * We do this in two parts, like we do device_register(). First, 9441da177e4SLinus Torvalds * we remove it from all the subsystems with device_del(), then 9451da177e4SLinus Torvalds * we decrement the reference count via put_device(). If that 9461da177e4SLinus Torvalds * is the final reference count, the device will be cleaned up 9471da177e4SLinus Torvalds * via device_release() above. Otherwise, the structure will 9481da177e4SLinus Torvalds * stick around until the final reference to the device is dropped. 9491da177e4SLinus Torvalds */ 9501da177e4SLinus Torvalds void device_unregister(struct device * dev) 9511da177e4SLinus Torvalds { 9521da177e4SLinus Torvalds pr_debug("DEV: Unregistering device. ID = '%s'\n", dev->bus_id); 9531da177e4SLinus Torvalds device_del(dev); 9541da177e4SLinus Torvalds put_device(dev); 9551da177e4SLinus Torvalds } 9561da177e4SLinus Torvalds 9571da177e4SLinus Torvalds 95836239577Smochel@digitalimplant.org static struct device * next_device(struct klist_iter * i) 95936239577Smochel@digitalimplant.org { 96036239577Smochel@digitalimplant.org struct klist_node * n = klist_next(i); 96136239577Smochel@digitalimplant.org return n ? container_of(n, struct device, knode_parent) : NULL; 96236239577Smochel@digitalimplant.org } 96336239577Smochel@digitalimplant.org 9641da177e4SLinus Torvalds /** 9651da177e4SLinus Torvalds * device_for_each_child - device child iterator. 966c41455fbSRandy Dunlap * @parent: parent struct device. 9671da177e4SLinus Torvalds * @data: data for the callback. 9681da177e4SLinus Torvalds * @fn: function to be called for each device. 9691da177e4SLinus Torvalds * 970c41455fbSRandy Dunlap * Iterate over @parent's child devices, and call @fn for each, 9711da177e4SLinus Torvalds * passing it @data. 9721da177e4SLinus Torvalds * 9731da177e4SLinus Torvalds * We check the return of @fn each time. If it returns anything 9741da177e4SLinus Torvalds * other than 0, we break out and return that value. 9751da177e4SLinus Torvalds */ 97636239577Smochel@digitalimplant.org int device_for_each_child(struct device * parent, void * data, 9771da177e4SLinus Torvalds int (*fn)(struct device *, void *)) 9781da177e4SLinus Torvalds { 97936239577Smochel@digitalimplant.org struct klist_iter i; 9801da177e4SLinus Torvalds struct device * child; 9811da177e4SLinus Torvalds int error = 0; 9821da177e4SLinus Torvalds 98336239577Smochel@digitalimplant.org klist_iter_init(&parent->klist_children, &i); 98436239577Smochel@digitalimplant.org while ((child = next_device(&i)) && !error) 98536239577Smochel@digitalimplant.org error = fn(child, data); 98636239577Smochel@digitalimplant.org klist_iter_exit(&i); 9871da177e4SLinus Torvalds return error; 9881da177e4SLinus Torvalds } 9891da177e4SLinus Torvalds 9905ab69981SCornelia Huck /** 9915ab69981SCornelia Huck * device_find_child - device iterator for locating a particular device. 9925ab69981SCornelia Huck * @parent: parent struct device 9935ab69981SCornelia Huck * @data: Data to pass to match function 9945ab69981SCornelia Huck * @match: Callback function to check device 9955ab69981SCornelia Huck * 9965ab69981SCornelia Huck * This is similar to the device_for_each_child() function above, but it 9975ab69981SCornelia Huck * returns a reference to a device that is 'found' for later use, as 9985ab69981SCornelia Huck * determined by the @match callback. 9995ab69981SCornelia Huck * 10005ab69981SCornelia Huck * The callback should return 0 if the device doesn't match and non-zero 10015ab69981SCornelia Huck * if it does. If the callback returns non-zero and a reference to the 10025ab69981SCornelia Huck * current device can be obtained, this function will return to the caller 10035ab69981SCornelia Huck * and not iterate over any more devices. 10045ab69981SCornelia Huck */ 10055ab69981SCornelia Huck struct device * device_find_child(struct device *parent, void *data, 10065ab69981SCornelia Huck int (*match)(struct device *, void *)) 10075ab69981SCornelia Huck { 10085ab69981SCornelia Huck struct klist_iter i; 10095ab69981SCornelia Huck struct device *child; 10105ab69981SCornelia Huck 10115ab69981SCornelia Huck if (!parent) 10125ab69981SCornelia Huck return NULL; 10135ab69981SCornelia Huck 10145ab69981SCornelia Huck klist_iter_init(&parent->klist_children, &i); 10155ab69981SCornelia Huck while ((child = next_device(&i))) 10165ab69981SCornelia Huck if (match(child, data) && get_device(child)) 10175ab69981SCornelia Huck break; 10185ab69981SCornelia Huck klist_iter_exit(&i); 10195ab69981SCornelia Huck return child; 10205ab69981SCornelia Huck } 10215ab69981SCornelia Huck 10221da177e4SLinus Torvalds int __init devices_init(void) 10231da177e4SLinus Torvalds { 10241da177e4SLinus Torvalds return subsystem_register(&devices_subsys); 10251da177e4SLinus Torvalds } 10261da177e4SLinus Torvalds 10271da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(device_for_each_child); 10285ab69981SCornelia Huck EXPORT_SYMBOL_GPL(device_find_child); 10291da177e4SLinus Torvalds 10301da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(device_initialize); 10311da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(device_add); 10321da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(device_register); 10331da177e4SLinus Torvalds 10341da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(device_del); 10351da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(device_unregister); 10361da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(get_device); 10371da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(put_device); 10381da177e4SLinus Torvalds 10391da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(device_create_file); 10401da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(device_remove_file); 104123681e47SGreg Kroah-Hartman 104223681e47SGreg Kroah-Hartman 104323681e47SGreg Kroah-Hartman static void device_create_release(struct device *dev) 104423681e47SGreg Kroah-Hartman { 104523681e47SGreg Kroah-Hartman pr_debug("%s called for %s\n", __FUNCTION__, dev->bus_id); 104623681e47SGreg Kroah-Hartman kfree(dev); 104723681e47SGreg Kroah-Hartman } 104823681e47SGreg Kroah-Hartman 104923681e47SGreg Kroah-Hartman /** 105023681e47SGreg Kroah-Hartman * device_create - creates a device and registers it with sysfs 105142734dafSHenrik Kretzschmar * @class: pointer to the struct class that this device should be registered to 105242734dafSHenrik Kretzschmar * @parent: pointer to the parent struct device of this new device, if any 105342734dafSHenrik Kretzschmar * @devt: the dev_t for the char device to be added 105442734dafSHenrik Kretzschmar * @fmt: string for the device's name 105523681e47SGreg Kroah-Hartman * 105642734dafSHenrik Kretzschmar * This function can be used by char device classes. A struct device 105742734dafSHenrik Kretzschmar * will be created in sysfs, registered to the specified class. 105842734dafSHenrik Kretzschmar * 105923681e47SGreg Kroah-Hartman * A "dev" file will be created, showing the dev_t for the device, if 106023681e47SGreg Kroah-Hartman * the dev_t is not 0,0. 106142734dafSHenrik Kretzschmar * If a pointer to a parent struct device is passed in, the newly created 106242734dafSHenrik Kretzschmar * struct device will be a child of that device in sysfs. 106342734dafSHenrik Kretzschmar * The pointer to the struct device will be returned from the call. 106442734dafSHenrik Kretzschmar * Any further sysfs files that might be required can be created using this 106523681e47SGreg Kroah-Hartman * pointer. 106623681e47SGreg Kroah-Hartman * 106723681e47SGreg Kroah-Hartman * Note: the struct class passed to this function must have previously 106823681e47SGreg Kroah-Hartman * been created with a call to class_create(). 106923681e47SGreg Kroah-Hartman */ 107023681e47SGreg Kroah-Hartman struct device *device_create(struct class *class, struct device *parent, 10715cbe5f8aSGreg Kroah-Hartman dev_t devt, const char *fmt, ...) 107223681e47SGreg Kroah-Hartman { 107323681e47SGreg Kroah-Hartman va_list args; 107423681e47SGreg Kroah-Hartman struct device *dev = NULL; 107523681e47SGreg Kroah-Hartman int retval = -ENODEV; 107623681e47SGreg Kroah-Hartman 107723681e47SGreg Kroah-Hartman if (class == NULL || IS_ERR(class)) 107823681e47SGreg Kroah-Hartman goto error; 107923681e47SGreg Kroah-Hartman 108023681e47SGreg Kroah-Hartman dev = kzalloc(sizeof(*dev), GFP_KERNEL); 108123681e47SGreg Kroah-Hartman if (!dev) { 108223681e47SGreg Kroah-Hartman retval = -ENOMEM; 108323681e47SGreg Kroah-Hartman goto error; 108423681e47SGreg Kroah-Hartman } 108523681e47SGreg Kroah-Hartman 108623681e47SGreg Kroah-Hartman dev->devt = devt; 108723681e47SGreg Kroah-Hartman dev->class = class; 108823681e47SGreg Kroah-Hartman dev->parent = parent; 108923681e47SGreg Kroah-Hartman dev->release = device_create_release; 109023681e47SGreg Kroah-Hartman 109123681e47SGreg Kroah-Hartman va_start(args, fmt); 109223681e47SGreg Kroah-Hartman vsnprintf(dev->bus_id, BUS_ID_SIZE, fmt, args); 109323681e47SGreg Kroah-Hartman va_end(args); 109423681e47SGreg Kroah-Hartman retval = device_register(dev); 109523681e47SGreg Kroah-Hartman if (retval) 109623681e47SGreg Kroah-Hartman goto error; 109723681e47SGreg Kroah-Hartman 109823681e47SGreg Kroah-Hartman return dev; 109923681e47SGreg Kroah-Hartman 110023681e47SGreg Kroah-Hartman error: 110123681e47SGreg Kroah-Hartman kfree(dev); 110223681e47SGreg Kroah-Hartman return ERR_PTR(retval); 110323681e47SGreg Kroah-Hartman } 110423681e47SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(device_create); 110523681e47SGreg Kroah-Hartman 110623681e47SGreg Kroah-Hartman /** 110723681e47SGreg Kroah-Hartman * device_destroy - removes a device that was created with device_create() 110842734dafSHenrik Kretzschmar * @class: pointer to the struct class that this device was registered with 110942734dafSHenrik Kretzschmar * @devt: the dev_t of the device that was previously registered 111023681e47SGreg Kroah-Hartman * 111142734dafSHenrik Kretzschmar * This call unregisters and cleans up a device that was created with a 111242734dafSHenrik Kretzschmar * call to device_create(). 111323681e47SGreg Kroah-Hartman */ 111423681e47SGreg Kroah-Hartman void device_destroy(struct class *class, dev_t devt) 111523681e47SGreg Kroah-Hartman { 111623681e47SGreg Kroah-Hartman struct device *dev = NULL; 111723681e47SGreg Kroah-Hartman struct device *dev_tmp; 111823681e47SGreg Kroah-Hartman 111923681e47SGreg Kroah-Hartman down(&class->sem); 112023681e47SGreg Kroah-Hartman list_for_each_entry(dev_tmp, &class->devices, node) { 112123681e47SGreg Kroah-Hartman if (dev_tmp->devt == devt) { 112223681e47SGreg Kroah-Hartman dev = dev_tmp; 112323681e47SGreg Kroah-Hartman break; 112423681e47SGreg Kroah-Hartman } 112523681e47SGreg Kroah-Hartman } 112623681e47SGreg Kroah-Hartman up(&class->sem); 112723681e47SGreg Kroah-Hartman 11285d9fd169SGreg Kroah-Hartman if (dev) 112923681e47SGreg Kroah-Hartman device_unregister(dev); 113023681e47SGreg Kroah-Hartman } 113123681e47SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(device_destroy); 1132a2de48caSGreg Kroah-Hartman 1133a2de48caSGreg Kroah-Hartman /** 1134a2de48caSGreg Kroah-Hartman * device_rename - renames a device 1135a2de48caSGreg Kroah-Hartman * @dev: the pointer to the struct device to be renamed 1136a2de48caSGreg Kroah-Hartman * @new_name: the new name of the device 1137a2de48caSGreg Kroah-Hartman */ 1138a2de48caSGreg Kroah-Hartman int device_rename(struct device *dev, char *new_name) 1139a2de48caSGreg Kroah-Hartman { 1140a2de48caSGreg Kroah-Hartman char *old_class_name = NULL; 1141a2de48caSGreg Kroah-Hartman char *new_class_name = NULL; 1142a2de48caSGreg Kroah-Hartman char *old_symlink_name = NULL; 1143a2de48caSGreg Kroah-Hartman int error; 1144a2de48caSGreg Kroah-Hartman 1145a2de48caSGreg Kroah-Hartman dev = get_device(dev); 1146a2de48caSGreg Kroah-Hartman if (!dev) 1147a2de48caSGreg Kroah-Hartman return -EINVAL; 1148a2de48caSGreg Kroah-Hartman 1149a2de48caSGreg Kroah-Hartman pr_debug("DEVICE: renaming '%s' to '%s'\n", dev->bus_id, new_name); 1150a2de48caSGreg Kroah-Hartman 115199ef3ef8SKay Sievers #ifdef CONFIG_SYSFS_DEPRECATED 1152a2de48caSGreg Kroah-Hartman if ((dev->class) && (dev->parent)) 1153a2de48caSGreg Kroah-Hartman old_class_name = make_class_name(dev->class->name, &dev->kobj); 115499ef3ef8SKay Sievers #endif 1155a2de48caSGreg Kroah-Hartman 1156a2de48caSGreg Kroah-Hartman if (dev->class) { 1157a2de48caSGreg Kroah-Hartman old_symlink_name = kmalloc(BUS_ID_SIZE, GFP_KERNEL); 1158952ab431SJesper Juhl if (!old_symlink_name) { 1159952ab431SJesper Juhl error = -ENOMEM; 1160952ab431SJesper Juhl goto out_free_old_class; 1161952ab431SJesper Juhl } 1162a2de48caSGreg Kroah-Hartman strlcpy(old_symlink_name, dev->bus_id, BUS_ID_SIZE); 1163a2de48caSGreg Kroah-Hartman } 1164a2de48caSGreg Kroah-Hartman 1165a2de48caSGreg Kroah-Hartman strlcpy(dev->bus_id, new_name, BUS_ID_SIZE); 1166a2de48caSGreg Kroah-Hartman 1167a2de48caSGreg Kroah-Hartman error = kobject_rename(&dev->kobj, new_name); 1168a2de48caSGreg Kroah-Hartman 116999ef3ef8SKay Sievers #ifdef CONFIG_SYSFS_DEPRECATED 1170a2de48caSGreg Kroah-Hartman if (old_class_name) { 1171a2de48caSGreg Kroah-Hartman new_class_name = make_class_name(dev->class->name, &dev->kobj); 1172a2de48caSGreg Kroah-Hartman if (new_class_name) { 1173a2de48caSGreg Kroah-Hartman sysfs_create_link(&dev->parent->kobj, &dev->kobj, 1174a2de48caSGreg Kroah-Hartman new_class_name); 1175a2de48caSGreg Kroah-Hartman sysfs_remove_link(&dev->parent->kobj, old_class_name); 1176a2de48caSGreg Kroah-Hartman } 1177a2de48caSGreg Kroah-Hartman } 117899ef3ef8SKay Sievers #endif 117999ef3ef8SKay Sievers 1180a2de48caSGreg Kroah-Hartman if (dev->class) { 1181823bccfcSGreg Kroah-Hartman sysfs_remove_link(&dev->class->subsys.kobj, 1182a2de48caSGreg Kroah-Hartman old_symlink_name); 1183823bccfcSGreg Kroah-Hartman sysfs_create_link(&dev->class->subsys.kobj, &dev->kobj, 1184a2de48caSGreg Kroah-Hartman dev->bus_id); 1185a2de48caSGreg Kroah-Hartman } 1186a2de48caSGreg Kroah-Hartman put_device(dev); 1187a2de48caSGreg Kroah-Hartman 1188a2de48caSGreg Kroah-Hartman kfree(new_class_name); 1189a2de48caSGreg Kroah-Hartman kfree(old_symlink_name); 1190952ab431SJesper Juhl out_free_old_class: 1191952ab431SJesper Juhl kfree(old_class_name); 1192a2de48caSGreg Kroah-Hartman 1193a2de48caSGreg Kroah-Hartman return error; 1194a2de48caSGreg Kroah-Hartman } 1195a2807dbcSJohannes Berg EXPORT_SYMBOL_GPL(device_rename); 11968a82472fSCornelia Huck 11978a82472fSCornelia Huck static int device_move_class_links(struct device *dev, 11988a82472fSCornelia Huck struct device *old_parent, 11998a82472fSCornelia Huck struct device *new_parent) 12008a82472fSCornelia Huck { 1201f7f3461dSGreg Kroah-Hartman int error = 0; 12028a82472fSCornelia Huck #ifdef CONFIG_SYSFS_DEPRECATED 12038a82472fSCornelia Huck char *class_name; 12048a82472fSCornelia Huck 12058a82472fSCornelia Huck class_name = make_class_name(dev->class->name, &dev->kobj); 12068a82472fSCornelia Huck if (!class_name) { 1207cb360bbfSCornelia Huck error = -ENOMEM; 12088a82472fSCornelia Huck goto out; 12098a82472fSCornelia Huck } 12108a82472fSCornelia Huck if (old_parent) { 12118a82472fSCornelia Huck sysfs_remove_link(&dev->kobj, "device"); 12128a82472fSCornelia Huck sysfs_remove_link(&old_parent->kobj, class_name); 12138a82472fSCornelia Huck } 1214c744aeaeSCornelia Huck if (new_parent) { 1215c744aeaeSCornelia Huck error = sysfs_create_link(&dev->kobj, &new_parent->kobj, 1216c744aeaeSCornelia Huck "device"); 12178a82472fSCornelia Huck if (error) 12188a82472fSCornelia Huck goto out; 1219c744aeaeSCornelia Huck error = sysfs_create_link(&new_parent->kobj, &dev->kobj, 1220c744aeaeSCornelia Huck class_name); 12218a82472fSCornelia Huck if (error) 12228a82472fSCornelia Huck sysfs_remove_link(&dev->kobj, "device"); 1223c744aeaeSCornelia Huck } 1224c744aeaeSCornelia Huck else 1225c744aeaeSCornelia Huck error = 0; 12268a82472fSCornelia Huck out: 12278a82472fSCornelia Huck kfree(class_name); 12288a82472fSCornelia Huck return error; 12298a82472fSCornelia Huck #else 1230f7f3461dSGreg Kroah-Hartman if (old_parent) 1231f7f3461dSGreg Kroah-Hartman sysfs_remove_link(&dev->kobj, "device"); 1232f7f3461dSGreg Kroah-Hartman if (new_parent) 1233f7f3461dSGreg Kroah-Hartman error = sysfs_create_link(&dev->kobj, &new_parent->kobj, 1234f7f3461dSGreg Kroah-Hartman "device"); 1235f7f3461dSGreg Kroah-Hartman return error; 12368a82472fSCornelia Huck #endif 12378a82472fSCornelia Huck } 12388a82472fSCornelia Huck 12398a82472fSCornelia Huck /** 12408a82472fSCornelia Huck * device_move - moves a device to a new parent 12418a82472fSCornelia Huck * @dev: the pointer to the struct device to be moved 1242c744aeaeSCornelia Huck * @new_parent: the new parent of the device (can by NULL) 12438a82472fSCornelia Huck */ 12448a82472fSCornelia Huck int device_move(struct device *dev, struct device *new_parent) 12458a82472fSCornelia Huck { 12468a82472fSCornelia Huck int error; 12478a82472fSCornelia Huck struct device *old_parent; 1248c744aeaeSCornelia Huck struct kobject *new_parent_kobj; 12498a82472fSCornelia Huck 12508a82472fSCornelia Huck dev = get_device(dev); 12518a82472fSCornelia Huck if (!dev) 12528a82472fSCornelia Huck return -EINVAL; 12538a82472fSCornelia Huck 12548a82472fSCornelia Huck new_parent = get_device(new_parent); 1255c744aeaeSCornelia Huck new_parent_kobj = get_device_parent (dev, new_parent); 1256c744aeaeSCornelia Huck if (IS_ERR(new_parent_kobj)) { 1257c744aeaeSCornelia Huck error = PTR_ERR(new_parent_kobj); 1258c744aeaeSCornelia Huck put_device(new_parent); 12598a82472fSCornelia Huck goto out; 12608a82472fSCornelia Huck } 12618a82472fSCornelia Huck pr_debug("DEVICE: moving '%s' to '%s'\n", dev->bus_id, 1262c744aeaeSCornelia Huck new_parent ? new_parent->bus_id : "<NULL>"); 1263c744aeaeSCornelia Huck error = kobject_move(&dev->kobj, new_parent_kobj); 12648a82472fSCornelia Huck if (error) { 12658a82472fSCornelia Huck put_device(new_parent); 12668a82472fSCornelia Huck goto out; 12678a82472fSCornelia Huck } 12688a82472fSCornelia Huck old_parent = dev->parent; 12698a82472fSCornelia Huck dev->parent = new_parent; 12708a82472fSCornelia Huck if (old_parent) 1271acf02d23SCornelia Huck klist_remove(&dev->knode_parent); 1272c744aeaeSCornelia Huck if (new_parent) 12738a82472fSCornelia Huck klist_add_tail(&dev->knode_parent, &new_parent->klist_children); 12748a82472fSCornelia Huck if (!dev->class) 12758a82472fSCornelia Huck goto out_put; 12768a82472fSCornelia Huck error = device_move_class_links(dev, old_parent, new_parent); 12778a82472fSCornelia Huck if (error) { 12788a82472fSCornelia Huck /* We ignore errors on cleanup since we're hosed anyway... */ 12798a82472fSCornelia Huck device_move_class_links(dev, new_parent, old_parent); 12808a82472fSCornelia Huck if (!kobject_move(&dev->kobj, &old_parent->kobj)) { 1281c744aeaeSCornelia Huck if (new_parent) 1282acf02d23SCornelia Huck klist_remove(&dev->knode_parent); 12838a82472fSCornelia Huck if (old_parent) 12848a82472fSCornelia Huck klist_add_tail(&dev->knode_parent, 12858a82472fSCornelia Huck &old_parent->klist_children); 12868a82472fSCornelia Huck } 12878a82472fSCornelia Huck put_device(new_parent); 12888a82472fSCornelia Huck goto out; 12898a82472fSCornelia Huck } 12908a82472fSCornelia Huck out_put: 12918a82472fSCornelia Huck put_device(old_parent); 12928a82472fSCornelia Huck out: 12938a82472fSCornelia Huck put_device(dev); 12948a82472fSCornelia Huck return error; 12958a82472fSCornelia Huck } 12968a82472fSCornelia Huck 12978a82472fSCornelia Huck EXPORT_SYMBOL_GPL(device_move); 1298