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 { 30660a96a59SKay Sievers size_t len = count; 30760a96a59SKay Sievers enum kobject_action action; 30860a96a59SKay Sievers 30960a96a59SKay Sievers if (len && buf[len-1] == '\n') 31060a96a59SKay Sievers len--; 31160a96a59SKay Sievers 31260a96a59SKay Sievers for (action = 0; action < KOBJ_MAX; action++) { 31360a96a59SKay Sievers if (strncmp(kobject_actions[action], buf, len) != 0) 31460a96a59SKay Sievers continue; 31560a96a59SKay Sievers if (kobject_actions[action][len] != '\0') 31660a96a59SKay Sievers continue; 31760a96a59SKay Sievers kobject_uevent(&dev->kobj, action); 31860a96a59SKay Sievers goto out; 31960a96a59SKay Sievers } 32060a96a59SKay Sievers 32122af74f3SKay Sievers dev_err(dev, "uevent: unsupported action-string; this will " 32260a96a59SKay Sievers "be ignored in a future kernel version\n"); 323312c004dSKay Sievers kobject_uevent(&dev->kobj, KOBJ_ADD); 32460a96a59SKay Sievers out: 325a7fd6706SKay Sievers return count; 326a7fd6706SKay Sievers } 327a7fd6706SKay Sievers 328ad6a1e1cSTejun Heo static struct device_attribute uevent_attr = 329ad6a1e1cSTejun Heo __ATTR(uevent, S_IRUGO | S_IWUSR, show_uevent, store_uevent); 330ad6a1e1cSTejun Heo 331621a1672SDmitry Torokhov static int device_add_attributes(struct device *dev, 332621a1672SDmitry Torokhov struct device_attribute *attrs) 333de0ff00dSGreg Kroah-Hartman { 334de0ff00dSGreg Kroah-Hartman int error = 0; 335621a1672SDmitry Torokhov int i; 336de0ff00dSGreg Kroah-Hartman 337621a1672SDmitry Torokhov if (attrs) { 338621a1672SDmitry Torokhov for (i = 0; attr_name(attrs[i]); i++) { 339621a1672SDmitry Torokhov error = device_create_file(dev, &attrs[i]); 340621a1672SDmitry Torokhov if (error) 341621a1672SDmitry Torokhov break; 342621a1672SDmitry Torokhov } 343621a1672SDmitry Torokhov if (error) 344de0ff00dSGreg Kroah-Hartman while (--i >= 0) 345621a1672SDmitry Torokhov device_remove_file(dev, &attrs[i]); 346de0ff00dSGreg Kroah-Hartman } 347de0ff00dSGreg Kroah-Hartman return error; 348de0ff00dSGreg Kroah-Hartman } 349de0ff00dSGreg Kroah-Hartman 350621a1672SDmitry Torokhov static void device_remove_attributes(struct device *dev, 351621a1672SDmitry Torokhov struct device_attribute *attrs) 352de0ff00dSGreg Kroah-Hartman { 353de0ff00dSGreg Kroah-Hartman int i; 354621a1672SDmitry Torokhov 355621a1672SDmitry Torokhov if (attrs) 356621a1672SDmitry Torokhov for (i = 0; attr_name(attrs[i]); i++) 357621a1672SDmitry Torokhov device_remove_file(dev, &attrs[i]); 358621a1672SDmitry Torokhov } 359621a1672SDmitry Torokhov 360621a1672SDmitry Torokhov static int device_add_groups(struct device *dev, 361621a1672SDmitry Torokhov struct attribute_group **groups) 362621a1672SDmitry Torokhov { 363621a1672SDmitry Torokhov int error = 0; 364621a1672SDmitry Torokhov int i; 365621a1672SDmitry Torokhov 366621a1672SDmitry Torokhov if (groups) { 367621a1672SDmitry Torokhov for (i = 0; groups[i]; i++) { 368621a1672SDmitry Torokhov error = sysfs_create_group(&dev->kobj, groups[i]); 369621a1672SDmitry Torokhov if (error) { 370621a1672SDmitry Torokhov while (--i >= 0) 371621a1672SDmitry Torokhov sysfs_remove_group(&dev->kobj, groups[i]); 372621a1672SDmitry Torokhov break; 373de0ff00dSGreg Kroah-Hartman } 374de0ff00dSGreg Kroah-Hartman } 375de0ff00dSGreg Kroah-Hartman } 376621a1672SDmitry Torokhov return error; 377621a1672SDmitry Torokhov } 378621a1672SDmitry Torokhov 379621a1672SDmitry Torokhov static void device_remove_groups(struct device *dev, 380621a1672SDmitry Torokhov struct attribute_group **groups) 381621a1672SDmitry Torokhov { 382621a1672SDmitry Torokhov int i; 383621a1672SDmitry Torokhov 384621a1672SDmitry Torokhov if (groups) 385621a1672SDmitry Torokhov for (i = 0; groups[i]; i++) 386621a1672SDmitry Torokhov sysfs_remove_group(&dev->kobj, groups[i]); 387621a1672SDmitry Torokhov } 388de0ff00dSGreg Kroah-Hartman 3892620efefSGreg Kroah-Hartman static int device_add_attrs(struct device *dev) 3902620efefSGreg Kroah-Hartman { 3912620efefSGreg Kroah-Hartman struct class *class = dev->class; 392f9f852dfSKay Sievers struct device_type *type = dev->type; 393621a1672SDmitry Torokhov int error; 3942620efefSGreg Kroah-Hartman 395621a1672SDmitry Torokhov if (class) { 396621a1672SDmitry Torokhov error = device_add_attributes(dev, class->dev_attrs); 3972620efefSGreg Kroah-Hartman if (error) 398621a1672SDmitry Torokhov return error; 399f9f852dfSKay Sievers } 400f9f852dfSKay Sievers 401621a1672SDmitry Torokhov if (type) { 402621a1672SDmitry Torokhov error = device_add_groups(dev, type->groups); 403f9f852dfSKay Sievers if (error) 404621a1672SDmitry Torokhov goto err_remove_class_attrs; 405f9f852dfSKay Sievers } 406621a1672SDmitry Torokhov 407621a1672SDmitry Torokhov error = device_add_groups(dev, dev->groups); 408f9f852dfSKay Sievers if (error) 409621a1672SDmitry Torokhov goto err_remove_type_groups; 410621a1672SDmitry Torokhov 411621a1672SDmitry Torokhov return 0; 412621a1672SDmitry Torokhov 413621a1672SDmitry Torokhov err_remove_type_groups: 414621a1672SDmitry Torokhov if (type) 415621a1672SDmitry Torokhov device_remove_groups(dev, type->groups); 416621a1672SDmitry Torokhov err_remove_class_attrs: 417621a1672SDmitry Torokhov if (class) 418621a1672SDmitry Torokhov device_remove_attributes(dev, class->dev_attrs); 419f9f852dfSKay Sievers 4202620efefSGreg Kroah-Hartman return error; 4212620efefSGreg Kroah-Hartman } 4222620efefSGreg Kroah-Hartman 4232620efefSGreg Kroah-Hartman static void device_remove_attrs(struct device *dev) 4242620efefSGreg Kroah-Hartman { 4252620efefSGreg Kroah-Hartman struct class *class = dev->class; 426f9f852dfSKay Sievers struct device_type *type = dev->type; 4272620efefSGreg Kroah-Hartman 428621a1672SDmitry Torokhov device_remove_groups(dev, dev->groups); 429f9f852dfSKay Sievers 430621a1672SDmitry Torokhov if (type) 431621a1672SDmitry Torokhov device_remove_groups(dev, type->groups); 432621a1672SDmitry Torokhov 433621a1672SDmitry Torokhov if (class) 434621a1672SDmitry Torokhov device_remove_attributes(dev, class->dev_attrs); 4352620efefSGreg Kroah-Hartman } 4362620efefSGreg Kroah-Hartman 4372620efefSGreg Kroah-Hartman 43823681e47SGreg Kroah-Hartman static ssize_t show_dev(struct device *dev, struct device_attribute *attr, 43923681e47SGreg Kroah-Hartman char *buf) 44023681e47SGreg Kroah-Hartman { 44123681e47SGreg Kroah-Hartman return print_dev_t(buf, dev->devt); 44223681e47SGreg Kroah-Hartman } 44323681e47SGreg Kroah-Hartman 444ad6a1e1cSTejun Heo static struct device_attribute devt_attr = 445ad6a1e1cSTejun Heo __ATTR(dev, S_IRUGO, show_dev, NULL); 446ad6a1e1cSTejun Heo 4470863afb3SMartin Waitz /* 4480863afb3SMartin Waitz * devices_subsys - structure to be registered with kobject core. 4491da177e4SLinus Torvalds */ 4501da177e4SLinus Torvalds 451312c004dSKay Sievers decl_subsys(devices, &ktype_device, &device_uevent_ops); 4521da177e4SLinus Torvalds 4531da177e4SLinus Torvalds 4541da177e4SLinus Torvalds /** 4551da177e4SLinus Torvalds * device_create_file - create sysfs attribute file for device. 4561da177e4SLinus Torvalds * @dev: device. 4571da177e4SLinus Torvalds * @attr: device attribute descriptor. 4581da177e4SLinus Torvalds */ 4591da177e4SLinus Torvalds 4601da177e4SLinus Torvalds int device_create_file(struct device * dev, struct device_attribute * attr) 4611da177e4SLinus Torvalds { 4621da177e4SLinus Torvalds int error = 0; 4631da177e4SLinus Torvalds if (get_device(dev)) { 4641da177e4SLinus Torvalds error = sysfs_create_file(&dev->kobj, &attr->attr); 4651da177e4SLinus Torvalds put_device(dev); 4661da177e4SLinus Torvalds } 4671da177e4SLinus Torvalds return error; 4681da177e4SLinus Torvalds } 4691da177e4SLinus Torvalds 4701da177e4SLinus Torvalds /** 4711da177e4SLinus Torvalds * device_remove_file - remove sysfs attribute file. 4721da177e4SLinus Torvalds * @dev: device. 4731da177e4SLinus Torvalds * @attr: device attribute descriptor. 4741da177e4SLinus Torvalds */ 4751da177e4SLinus Torvalds 4761da177e4SLinus Torvalds void device_remove_file(struct device * dev, struct device_attribute * attr) 4771da177e4SLinus Torvalds { 4781da177e4SLinus Torvalds if (get_device(dev)) { 4791da177e4SLinus Torvalds sysfs_remove_file(&dev->kobj, &attr->attr); 4801da177e4SLinus Torvalds put_device(dev); 4811da177e4SLinus Torvalds } 4821da177e4SLinus Torvalds } 4831da177e4SLinus Torvalds 4842589f188SGreg Kroah-Hartman /** 4852589f188SGreg Kroah-Hartman * device_create_bin_file - create sysfs binary attribute file for device. 4862589f188SGreg Kroah-Hartman * @dev: device. 4872589f188SGreg Kroah-Hartman * @attr: device binary attribute descriptor. 4882589f188SGreg Kroah-Hartman */ 4892589f188SGreg Kroah-Hartman int device_create_bin_file(struct device *dev, struct bin_attribute *attr) 4902589f188SGreg Kroah-Hartman { 4912589f188SGreg Kroah-Hartman int error = -EINVAL; 4922589f188SGreg Kroah-Hartman if (dev) 4932589f188SGreg Kroah-Hartman error = sysfs_create_bin_file(&dev->kobj, attr); 4942589f188SGreg Kroah-Hartman return error; 4952589f188SGreg Kroah-Hartman } 4962589f188SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(device_create_bin_file); 4972589f188SGreg Kroah-Hartman 4982589f188SGreg Kroah-Hartman /** 4992589f188SGreg Kroah-Hartman * device_remove_bin_file - remove sysfs binary attribute file 5002589f188SGreg Kroah-Hartman * @dev: device. 5012589f188SGreg Kroah-Hartman * @attr: device binary attribute descriptor. 5022589f188SGreg Kroah-Hartman */ 5032589f188SGreg Kroah-Hartman void device_remove_bin_file(struct device *dev, struct bin_attribute *attr) 5042589f188SGreg Kroah-Hartman { 5052589f188SGreg Kroah-Hartman if (dev) 5062589f188SGreg Kroah-Hartman sysfs_remove_bin_file(&dev->kobj, attr); 5072589f188SGreg Kroah-Hartman } 5082589f188SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(device_remove_bin_file); 5092589f188SGreg Kroah-Hartman 510d9a9cdfbSAlan Stern /** 511523ded71SAlan Stern * device_schedule_callback_owner - helper to schedule a callback for a device 512d9a9cdfbSAlan Stern * @dev: device. 513d9a9cdfbSAlan Stern * @func: callback function to invoke later. 514523ded71SAlan Stern * @owner: module owning the callback routine 515d9a9cdfbSAlan Stern * 516d9a9cdfbSAlan Stern * Attribute methods must not unregister themselves or their parent device 517d9a9cdfbSAlan Stern * (which would amount to the same thing). Attempts to do so will deadlock, 518d9a9cdfbSAlan Stern * since unregistration is mutually exclusive with driver callbacks. 519d9a9cdfbSAlan Stern * 520d9a9cdfbSAlan Stern * Instead methods can call this routine, which will attempt to allocate 521d9a9cdfbSAlan Stern * and schedule a workqueue request to call back @func with @dev as its 522d9a9cdfbSAlan Stern * argument in the workqueue's process context. @dev will be pinned until 523d9a9cdfbSAlan Stern * @func returns. 524d9a9cdfbSAlan Stern * 525523ded71SAlan Stern * This routine is usually called via the inline device_schedule_callback(), 526523ded71SAlan Stern * which automatically sets @owner to THIS_MODULE. 527523ded71SAlan Stern * 528d9a9cdfbSAlan Stern * Returns 0 if the request was submitted, -ENOMEM if storage could not 529523ded71SAlan Stern * be allocated, -ENODEV if a reference to @owner isn't available. 530d9a9cdfbSAlan Stern * 531d9a9cdfbSAlan Stern * NOTE: This routine won't work if CONFIG_SYSFS isn't set! It uses an 532d9a9cdfbSAlan Stern * underlying sysfs routine (since it is intended for use by attribute 533d9a9cdfbSAlan Stern * methods), and if sysfs isn't available you'll get nothing but -ENOSYS. 534d9a9cdfbSAlan Stern */ 535523ded71SAlan Stern int device_schedule_callback_owner(struct device *dev, 536523ded71SAlan Stern void (*func)(struct device *), struct module *owner) 537d9a9cdfbSAlan Stern { 538d9a9cdfbSAlan Stern return sysfs_schedule_callback(&dev->kobj, 539523ded71SAlan Stern (void (*)(void *)) func, dev, owner); 540d9a9cdfbSAlan Stern } 541523ded71SAlan Stern EXPORT_SYMBOL_GPL(device_schedule_callback_owner); 542d9a9cdfbSAlan Stern 54334bb61f9SJames Bottomley static void klist_children_get(struct klist_node *n) 54434bb61f9SJames Bottomley { 54534bb61f9SJames Bottomley struct device *dev = container_of(n, struct device, knode_parent); 54634bb61f9SJames Bottomley 54734bb61f9SJames Bottomley get_device(dev); 54834bb61f9SJames Bottomley } 54934bb61f9SJames Bottomley 55034bb61f9SJames Bottomley static void klist_children_put(struct klist_node *n) 55134bb61f9SJames Bottomley { 55234bb61f9SJames Bottomley struct device *dev = container_of(n, struct device, knode_parent); 55334bb61f9SJames Bottomley 55434bb61f9SJames Bottomley put_device(dev); 55534bb61f9SJames Bottomley } 55634bb61f9SJames Bottomley 5571da177e4SLinus Torvalds 5581da177e4SLinus Torvalds /** 5591da177e4SLinus Torvalds * device_initialize - init device structure. 5601da177e4SLinus Torvalds * @dev: device. 5611da177e4SLinus Torvalds * 5621da177e4SLinus Torvalds * This prepares the device for use by other layers, 5631da177e4SLinus Torvalds * including adding it to the device hierarchy. 5641da177e4SLinus Torvalds * It is the first half of device_register(), if called by 5651da177e4SLinus Torvalds * that, though it can also be called separately, so one 5661da177e4SLinus Torvalds * may use @dev's fields (e.g. the refcount). 5671da177e4SLinus Torvalds */ 5681da177e4SLinus Torvalds 5691da177e4SLinus Torvalds void device_initialize(struct device *dev) 5701da177e4SLinus Torvalds { 5711da177e4SLinus Torvalds kobj_set_kset_s(dev, devices_subsys); 5721da177e4SLinus Torvalds kobject_init(&dev->kobj); 57334bb61f9SJames Bottomley klist_init(&dev->klist_children, klist_children_get, 57434bb61f9SJames Bottomley klist_children_put); 5751da177e4SLinus Torvalds INIT_LIST_HEAD(&dev->dma_pools); 57623681e47SGreg Kroah-Hartman INIT_LIST_HEAD(&dev->node); 577af70316aSmochel@digitalimplant.org init_MUTEX(&dev->sem); 5789ac7849eSTejun Heo spin_lock_init(&dev->devres_lock); 5799ac7849eSTejun Heo INIT_LIST_HEAD(&dev->devres_head); 5800ac85241SDavid Brownell device_init_wakeup(dev, 0); 58187348136SChristoph Hellwig set_dev_node(dev, -1); 5821da177e4SLinus Torvalds } 5831da177e4SLinus Torvalds 58440fa5422SGreg Kroah-Hartman #ifdef CONFIG_SYSFS_DEPRECATED 585c744aeaeSCornelia Huck static struct kobject * get_device_parent(struct device *dev, 586c744aeaeSCornelia Huck struct device *parent) 58740fa5422SGreg Kroah-Hartman { 58840fa5422SGreg Kroah-Hartman /* Set the parent to the class, not the parent device */ 58940fa5422SGreg Kroah-Hartman /* this keeps sysfs from having a symlink to make old udevs happy */ 59040fa5422SGreg Kroah-Hartman if (dev->class) 591823bccfcSGreg Kroah-Hartman return &dev->class->subsys.kobj; 59240fa5422SGreg Kroah-Hartman else if (parent) 593c744aeaeSCornelia Huck return &parent->kobj; 59440fa5422SGreg Kroah-Hartman 595c744aeaeSCornelia Huck return NULL; 59640fa5422SGreg Kroah-Hartman } 59740fa5422SGreg Kroah-Hartman #else 598c744aeaeSCornelia Huck static struct kobject *virtual_device_parent(struct device *dev) 599f0ee61a6SGreg Kroah-Hartman { 600f0ee61a6SGreg Kroah-Hartman static struct kobject *virtual_dir = NULL; 601f0ee61a6SGreg Kroah-Hartman 602f0ee61a6SGreg Kroah-Hartman if (!virtual_dir) 603823bccfcSGreg Kroah-Hartman virtual_dir = kobject_add_dir(&devices_subsys.kobj, "virtual"); 604f0ee61a6SGreg Kroah-Hartman 60586406245SKay Sievers return virtual_dir; 606f0ee61a6SGreg Kroah-Hartman } 607f0ee61a6SGreg Kroah-Hartman 608c744aeaeSCornelia Huck static struct kobject * get_device_parent(struct device *dev, 609c744aeaeSCornelia Huck struct device *parent) 61040fa5422SGreg Kroah-Hartman { 61186406245SKay Sievers if (dev->class) { 61286406245SKay Sievers struct kobject *kobj = NULL; 61386406245SKay Sievers struct kobject *parent_kobj; 61486406245SKay Sievers struct kobject *k; 61586406245SKay Sievers 61686406245SKay Sievers /* 61786406245SKay Sievers * If we have no parent, we live in "virtual". 61886406245SKay Sievers * Class-devices with a bus-device as parent, live 61986406245SKay Sievers * in a class-directory to prevent namespace collisions. 62086406245SKay Sievers */ 62186406245SKay Sievers if (parent == NULL) 62286406245SKay Sievers parent_kobj = virtual_device_parent(dev); 62386406245SKay Sievers else if (parent->class) 62486406245SKay Sievers return &parent->kobj; 62586406245SKay Sievers else 62686406245SKay Sievers parent_kobj = &parent->kobj; 62786406245SKay Sievers 62886406245SKay Sievers /* find our class-directory at the parent and reference it */ 62986406245SKay Sievers spin_lock(&dev->class->class_dirs.list_lock); 63086406245SKay Sievers list_for_each_entry(k, &dev->class->class_dirs.list, entry) 63186406245SKay Sievers if (k->parent == parent_kobj) { 63286406245SKay Sievers kobj = kobject_get(k); 63386406245SKay Sievers break; 63486406245SKay Sievers } 63586406245SKay Sievers spin_unlock(&dev->class->class_dirs.list_lock); 63686406245SKay Sievers if (kobj) 63786406245SKay Sievers return kobj; 63886406245SKay Sievers 63986406245SKay Sievers /* or create a new class-directory at the parent device */ 64086406245SKay Sievers return kobject_kset_add_dir(&dev->class->class_dirs, 64186406245SKay Sievers parent_kobj, dev->class->name); 64286406245SKay Sievers } 64386406245SKay Sievers 64486406245SKay Sievers if (parent) 645c744aeaeSCornelia Huck return &parent->kobj; 646c744aeaeSCornelia Huck return NULL; 647c744aeaeSCornelia Huck } 648c744aeaeSCornelia Huck #endif 64986406245SKay Sievers 650c744aeaeSCornelia Huck static int setup_parent(struct device *dev, struct device *parent) 651c744aeaeSCornelia Huck { 652c744aeaeSCornelia Huck struct kobject *kobj; 653c744aeaeSCornelia Huck kobj = get_device_parent(dev, parent); 654c744aeaeSCornelia Huck if (IS_ERR(kobj)) 655c744aeaeSCornelia Huck return PTR_ERR(kobj); 656c744aeaeSCornelia Huck if (kobj) 657c744aeaeSCornelia Huck dev->kobj.parent = kobj; 65840fa5422SGreg Kroah-Hartman return 0; 65940fa5422SGreg Kroah-Hartman } 66040fa5422SGreg Kroah-Hartman 6612ee97cafSCornelia Huck static int device_add_class_symlinks(struct device *dev) 6622ee97cafSCornelia Huck { 6632ee97cafSCornelia Huck int error; 6642ee97cafSCornelia Huck 6652ee97cafSCornelia Huck if (!dev->class) 6662ee97cafSCornelia Huck return 0; 6672ee97cafSCornelia Huck error = sysfs_create_link(&dev->kobj, &dev->class->subsys.kobj, 6682ee97cafSCornelia Huck "subsystem"); 6692ee97cafSCornelia Huck if (error) 6702ee97cafSCornelia Huck goto out; 6712ee97cafSCornelia Huck /* 6722ee97cafSCornelia Huck * If this is not a "fake" compatible device, then create the 6732ee97cafSCornelia Huck * symlink from the class to the device. 6742ee97cafSCornelia Huck */ 6752ee97cafSCornelia Huck if (dev->kobj.parent != &dev->class->subsys.kobj) { 6762ee97cafSCornelia Huck error = sysfs_create_link(&dev->class->subsys.kobj, &dev->kobj, 6772ee97cafSCornelia Huck dev->bus_id); 6782ee97cafSCornelia Huck if (error) 6792ee97cafSCornelia Huck goto out_subsys; 6802ee97cafSCornelia Huck } 68127907689SCornelia Huck if (dev->parent) { 6822ee97cafSCornelia Huck error = sysfs_create_link(&dev->kobj, &dev->parent->kobj, 6832ee97cafSCornelia Huck "device"); 6842ee97cafSCornelia Huck if (error) 6852ee97cafSCornelia Huck goto out_busid; 6862ee97cafSCornelia Huck #ifdef CONFIG_SYSFS_DEPRECATED 6872ee97cafSCornelia Huck { 6882ee97cafSCornelia Huck char * class_name = make_class_name(dev->class->name, 6892ee97cafSCornelia Huck &dev->kobj); 6902ee97cafSCornelia Huck if (class_name) 6912ee97cafSCornelia Huck error = sysfs_create_link(&dev->parent->kobj, 6922ee97cafSCornelia Huck &dev->kobj, class_name); 6932ee97cafSCornelia Huck kfree(class_name); 6942ee97cafSCornelia Huck if (error) 6952ee97cafSCornelia Huck goto out_device; 6962ee97cafSCornelia Huck } 6972ee97cafSCornelia Huck #endif 6982ee97cafSCornelia Huck } 6992ee97cafSCornelia Huck return 0; 7002ee97cafSCornelia Huck 7012ee97cafSCornelia Huck #ifdef CONFIG_SYSFS_DEPRECATED 7022ee97cafSCornelia Huck out_device: 7032ee97cafSCornelia Huck if (dev->parent) 7042ee97cafSCornelia Huck sysfs_remove_link(&dev->kobj, "device"); 7052ee97cafSCornelia Huck #endif 7062ee97cafSCornelia Huck out_busid: 7072ee97cafSCornelia Huck if (dev->kobj.parent != &dev->class->subsys.kobj) 7082ee97cafSCornelia Huck sysfs_remove_link(&dev->class->subsys.kobj, dev->bus_id); 7092ee97cafSCornelia Huck out_subsys: 7102ee97cafSCornelia Huck sysfs_remove_link(&dev->kobj, "subsystem"); 7112ee97cafSCornelia Huck out: 7122ee97cafSCornelia Huck return error; 7132ee97cafSCornelia Huck } 7142ee97cafSCornelia Huck 7152ee97cafSCornelia Huck static void device_remove_class_symlinks(struct device *dev) 7162ee97cafSCornelia Huck { 7172ee97cafSCornelia Huck if (!dev->class) 7182ee97cafSCornelia Huck return; 7192ee97cafSCornelia Huck if (dev->parent) { 7202ee97cafSCornelia Huck #ifdef CONFIG_SYSFS_DEPRECATED 7212ee97cafSCornelia Huck char *class_name; 7222ee97cafSCornelia Huck 7232ee97cafSCornelia Huck class_name = make_class_name(dev->class->name, &dev->kobj); 7242ee97cafSCornelia Huck if (class_name) { 7252ee97cafSCornelia Huck sysfs_remove_link(&dev->parent->kobj, class_name); 7262ee97cafSCornelia Huck kfree(class_name); 7272ee97cafSCornelia Huck } 7282ee97cafSCornelia Huck #endif 7292ee97cafSCornelia Huck sysfs_remove_link(&dev->kobj, "device"); 7302ee97cafSCornelia Huck } 7312ee97cafSCornelia Huck if (dev->kobj.parent != &dev->class->subsys.kobj) 7322ee97cafSCornelia Huck sysfs_remove_link(&dev->class->subsys.kobj, dev->bus_id); 7332ee97cafSCornelia Huck sysfs_remove_link(&dev->kobj, "subsystem"); 7342ee97cafSCornelia Huck } 7352ee97cafSCornelia Huck 7361da177e4SLinus Torvalds /** 7371da177e4SLinus Torvalds * device_add - add device to device hierarchy. 7381da177e4SLinus Torvalds * @dev: device. 7391da177e4SLinus Torvalds * 7401da177e4SLinus Torvalds * This is part 2 of device_register(), though may be called 7411da177e4SLinus Torvalds * separately _iff_ device_initialize() has been called separately. 7421da177e4SLinus Torvalds * 7431da177e4SLinus Torvalds * This adds it to the kobject hierarchy via kobject_add(), adds it 7441da177e4SLinus Torvalds * to the global and sibling lists for the device, then 7451da177e4SLinus Torvalds * adds it to the other relevant subsystems of the driver model. 7461da177e4SLinus Torvalds */ 7471da177e4SLinus Torvalds int device_add(struct device *dev) 7481da177e4SLinus Torvalds { 7491da177e4SLinus Torvalds struct device *parent = NULL; 750c47ed219SGreg Kroah-Hartman struct class_interface *class_intf; 7511da177e4SLinus Torvalds int error = -EINVAL; 7521da177e4SLinus Torvalds 7531da177e4SLinus Torvalds dev = get_device(dev); 7541da177e4SLinus Torvalds if (!dev || !strlen(dev->bus_id)) 7551da177e4SLinus Torvalds goto Error; 7561da177e4SLinus Torvalds 75740fa5422SGreg Kroah-Hartman pr_debug("DEV: registering device: ID = '%s'\n", dev->bus_id); 758c205ef48SGreg Kroah-Hartman 7591da177e4SLinus Torvalds parent = get_device(dev->parent); 76040fa5422SGreg Kroah-Hartman error = setup_parent(dev, parent); 76140fa5422SGreg Kroah-Hartman if (error) 76240fa5422SGreg Kroah-Hartman goto Error; 7631da177e4SLinus Torvalds 7641da177e4SLinus Torvalds /* first, register with generic layer. */ 7651da177e4SLinus Torvalds kobject_set_name(&dev->kobj, "%s", dev->bus_id); 76640fa5422SGreg Kroah-Hartman error = kobject_add(&dev->kobj); 76740fa5422SGreg Kroah-Hartman if (error) 7681da177e4SLinus Torvalds goto Error; 769a7fd6706SKay Sievers 77037022644SBrian Walsh /* notify platform of device entry */ 77137022644SBrian Walsh if (platform_notify) 77237022644SBrian Walsh platform_notify(dev); 77337022644SBrian Walsh 774116af378SBenjamin Herrenschmidt /* notify clients of device entry (new way) */ 775116af378SBenjamin Herrenschmidt if (dev->bus) 776116af378SBenjamin Herrenschmidt blocking_notifier_call_chain(&dev->bus->bus_notifier, 777116af378SBenjamin Herrenschmidt BUS_NOTIFY_ADD_DEVICE, dev); 778116af378SBenjamin Herrenschmidt 779ad6a1e1cSTejun Heo error = device_create_file(dev, &uevent_attr); 780a306eea4SCornelia Huck if (error) 781a306eea4SCornelia Huck goto attrError; 782a7fd6706SKay Sievers 78323681e47SGreg Kroah-Hartman if (MAJOR(dev->devt)) { 784ad6a1e1cSTejun Heo error = device_create_file(dev, &devt_attr); 785ad6a1e1cSTejun Heo if (error) 786a306eea4SCornelia Huck goto ueventattrError; 78723681e47SGreg Kroah-Hartman } 78823681e47SGreg Kroah-Hartman 7892ee97cafSCornelia Huck error = device_add_class_symlinks(dev); 7902ee97cafSCornelia Huck if (error) 7912ee97cafSCornelia Huck goto SymlinkError; 792dc0afa83SCornelia Huck error = device_add_attrs(dev); 793dc0afa83SCornelia Huck if (error) 7942620efefSGreg Kroah-Hartman goto AttrsError; 795dc0afa83SCornelia Huck error = device_pm_add(dev); 796dc0afa83SCornelia Huck if (error) 7971da177e4SLinus Torvalds goto PMError; 798dc0afa83SCornelia Huck error = bus_add_device(dev); 799dc0afa83SCornelia Huck if (error) 8001da177e4SLinus Torvalds goto BusError; 80153877d06SKay Sievers kobject_uevent(&dev->kobj, KOBJ_ADD); 802c6a46696SCornelia Huck bus_attach_device(dev); 8031da177e4SLinus Torvalds if (parent) 804d856f1e3SJames Bottomley klist_add_tail(&dev->knode_parent, &parent->klist_children); 8051da177e4SLinus Torvalds 8065d9fd169SGreg Kroah-Hartman if (dev->class) { 8075d9fd169SGreg Kroah-Hartman down(&dev->class->sem); 808c47ed219SGreg Kroah-Hartman /* tie the class to the device */ 8095d9fd169SGreg Kroah-Hartman list_add_tail(&dev->node, &dev->class->devices); 810c47ed219SGreg Kroah-Hartman 811c47ed219SGreg Kroah-Hartman /* notify any interfaces that the device is here */ 812c47ed219SGreg Kroah-Hartman list_for_each_entry(class_intf, &dev->class->interfaces, node) 813c47ed219SGreg Kroah-Hartman if (class_intf->add_dev) 814c47ed219SGreg Kroah-Hartman class_intf->add_dev(dev, class_intf); 8155d9fd169SGreg Kroah-Hartman up(&dev->class->sem); 8165d9fd169SGreg Kroah-Hartman } 8171da177e4SLinus Torvalds Done: 8181da177e4SLinus Torvalds put_device(dev); 8191da177e4SLinus Torvalds return error; 8201da177e4SLinus Torvalds BusError: 8211da177e4SLinus Torvalds device_pm_remove(dev); 8221da177e4SLinus Torvalds PMError: 823116af378SBenjamin Herrenschmidt if (dev->bus) 824116af378SBenjamin Herrenschmidt blocking_notifier_call_chain(&dev->bus->bus_notifier, 825116af378SBenjamin Herrenschmidt BUS_NOTIFY_DEL_DEVICE, dev); 8262620efefSGreg Kroah-Hartman device_remove_attrs(dev); 8272620efefSGreg Kroah-Hartman AttrsError: 8282ee97cafSCornelia Huck device_remove_class_symlinks(dev); 8292ee97cafSCornelia Huck SymlinkError: 830ad6a1e1cSTejun Heo if (MAJOR(dev->devt)) 831ad6a1e1cSTejun Heo device_remove_file(dev, &devt_attr); 83282f0cf9bSJames Simmons 83382f0cf9bSJames Simmons if (dev->class) { 83482f0cf9bSJames Simmons sysfs_remove_link(&dev->kobj, "subsystem"); 83582f0cf9bSJames Simmons /* If this is not a "fake" compatible device, remove the 83682f0cf9bSJames Simmons * symlink from the class to the device. */ 837823bccfcSGreg Kroah-Hartman if (dev->kobj.parent != &dev->class->subsys.kobj) 838823bccfcSGreg Kroah-Hartman sysfs_remove_link(&dev->class->subsys.kobj, 83982f0cf9bSJames Simmons dev->bus_id); 84082f0cf9bSJames Simmons if (parent) { 841f7f3461dSGreg Kroah-Hartman #ifdef CONFIG_SYSFS_DEPRECATED 84282f0cf9bSJames Simmons char *class_name = make_class_name(dev->class->name, 84382f0cf9bSJames Simmons &dev->kobj); 84482f0cf9bSJames Simmons if (class_name) 84582f0cf9bSJames Simmons sysfs_remove_link(&dev->parent->kobj, 84682f0cf9bSJames Simmons class_name); 84782f0cf9bSJames Simmons kfree(class_name); 848f7f3461dSGreg Kroah-Hartman #endif 84982f0cf9bSJames Simmons sysfs_remove_link(&dev->kobj, "device"); 85082f0cf9bSJames Simmons } 85123681e47SGreg Kroah-Hartman } 852a306eea4SCornelia Huck ueventattrError: 853ad6a1e1cSTejun Heo device_remove_file(dev, &uevent_attr); 85423681e47SGreg Kroah-Hartman attrError: 855312c004dSKay Sievers kobject_uevent(&dev->kobj, KOBJ_REMOVE); 8561da177e4SLinus Torvalds kobject_del(&dev->kobj); 8571da177e4SLinus Torvalds Error: 8581da177e4SLinus Torvalds if (parent) 8591da177e4SLinus Torvalds put_device(parent); 8601da177e4SLinus Torvalds goto Done; 8611da177e4SLinus Torvalds } 8621da177e4SLinus Torvalds 8631da177e4SLinus Torvalds 8641da177e4SLinus Torvalds /** 8651da177e4SLinus Torvalds * device_register - register a device with the system. 8661da177e4SLinus Torvalds * @dev: pointer to the device structure 8671da177e4SLinus Torvalds * 8681da177e4SLinus Torvalds * This happens in two clean steps - initialize the device 8691da177e4SLinus Torvalds * and add it to the system. The two steps can be called 8701da177e4SLinus Torvalds * separately, but this is the easiest and most common. 8711da177e4SLinus Torvalds * I.e. you should only call the two helpers separately if 8721da177e4SLinus Torvalds * have a clearly defined need to use and refcount the device 8731da177e4SLinus Torvalds * before it is added to the hierarchy. 8741da177e4SLinus Torvalds */ 8751da177e4SLinus Torvalds 8761da177e4SLinus Torvalds int device_register(struct device *dev) 8771da177e4SLinus Torvalds { 8781da177e4SLinus Torvalds device_initialize(dev); 8791da177e4SLinus Torvalds return device_add(dev); 8801da177e4SLinus Torvalds } 8811da177e4SLinus Torvalds 8821da177e4SLinus Torvalds 8831da177e4SLinus Torvalds /** 8841da177e4SLinus Torvalds * get_device - increment reference count for device. 8851da177e4SLinus Torvalds * @dev: device. 8861da177e4SLinus Torvalds * 8871da177e4SLinus Torvalds * This simply forwards the call to kobject_get(), though 8881da177e4SLinus Torvalds * we do take care to provide for the case that we get a NULL 8891da177e4SLinus Torvalds * pointer passed in. 8901da177e4SLinus Torvalds */ 8911da177e4SLinus Torvalds 8921da177e4SLinus Torvalds struct device * get_device(struct device * dev) 8931da177e4SLinus Torvalds { 8941da177e4SLinus Torvalds return dev ? to_dev(kobject_get(&dev->kobj)) : NULL; 8951da177e4SLinus Torvalds } 8961da177e4SLinus Torvalds 8971da177e4SLinus Torvalds 8981da177e4SLinus Torvalds /** 8991da177e4SLinus Torvalds * put_device - decrement reference count. 9001da177e4SLinus Torvalds * @dev: device in question. 9011da177e4SLinus Torvalds */ 9021da177e4SLinus Torvalds void put_device(struct device * dev) 9031da177e4SLinus Torvalds { 9041da177e4SLinus Torvalds if (dev) 9051da177e4SLinus Torvalds kobject_put(&dev->kobj); 9061da177e4SLinus Torvalds } 9071da177e4SLinus Torvalds 9081da177e4SLinus Torvalds 9091da177e4SLinus Torvalds /** 9101da177e4SLinus Torvalds * device_del - delete device from system. 9111da177e4SLinus Torvalds * @dev: device. 9121da177e4SLinus Torvalds * 9131da177e4SLinus Torvalds * This is the first part of the device unregistration 9141da177e4SLinus Torvalds * sequence. This removes the device from the lists we control 9151da177e4SLinus Torvalds * from here, has it removed from the other driver model 9161da177e4SLinus Torvalds * subsystems it was added to in device_add(), and removes it 9171da177e4SLinus Torvalds * from the kobject hierarchy. 9181da177e4SLinus Torvalds * 9191da177e4SLinus Torvalds * NOTE: this should be called manually _iff_ device_add() was 9201da177e4SLinus Torvalds * also called manually. 9211da177e4SLinus Torvalds */ 9221da177e4SLinus Torvalds 9231da177e4SLinus Torvalds void device_del(struct device * dev) 9241da177e4SLinus Torvalds { 9251da177e4SLinus Torvalds struct device * parent = dev->parent; 926c47ed219SGreg Kroah-Hartman struct class_interface *class_intf; 9271da177e4SLinus Torvalds 9281da177e4SLinus Torvalds if (parent) 929d62c0f9fSPatrick Mochel klist_del(&dev->knode_parent); 930ad6a1e1cSTejun Heo if (MAJOR(dev->devt)) 931ad6a1e1cSTejun Heo device_remove_file(dev, &devt_attr); 932b9d9c82bSKay Sievers if (dev->class) { 933b9d9c82bSKay Sievers sysfs_remove_link(&dev->kobj, "subsystem"); 93440fa5422SGreg Kroah-Hartman /* If this is not a "fake" compatible device, remove the 93540fa5422SGreg Kroah-Hartman * symlink from the class to the device. */ 936823bccfcSGreg Kroah-Hartman if (dev->kobj.parent != &dev->class->subsys.kobj) 937823bccfcSGreg Kroah-Hartman sysfs_remove_link(&dev->class->subsys.kobj, 93840fa5422SGreg Kroah-Hartman dev->bus_id); 93964bb5d2cSGreg Kroah-Hartman if (parent) { 940f7f3461dSGreg Kroah-Hartman #ifdef CONFIG_SYSFS_DEPRECATED 94199ef3ef8SKay Sievers char *class_name = make_class_name(dev->class->name, 94299ef3ef8SKay Sievers &dev->kobj); 943cb360bbfSCornelia Huck if (class_name) 944cb360bbfSCornelia Huck sysfs_remove_link(&dev->parent->kobj, 945cb360bbfSCornelia Huck class_name); 946e9a7d305SGreg Kroah-Hartman kfree(class_name); 947f7f3461dSGreg Kroah-Hartman #endif 94899ef3ef8SKay Sievers sysfs_remove_link(&dev->kobj, "device"); 94999ef3ef8SKay Sievers } 95099ef3ef8SKay Sievers 9515d9fd169SGreg Kroah-Hartman down(&dev->class->sem); 952c47ed219SGreg Kroah-Hartman /* notify any interfaces that the device is now gone */ 953c47ed219SGreg Kroah-Hartman list_for_each_entry(class_intf, &dev->class->interfaces, node) 954c47ed219SGreg Kroah-Hartman if (class_intf->remove_dev) 955c47ed219SGreg Kroah-Hartman class_intf->remove_dev(dev, class_intf); 956c47ed219SGreg Kroah-Hartman /* remove the device from the class list */ 9575d9fd169SGreg Kroah-Hartman list_del_init(&dev->node); 9585d9fd169SGreg Kroah-Hartman up(&dev->class->sem); 95986406245SKay Sievers 96086406245SKay Sievers /* If we live in a parent class-directory, unreference it */ 96186406245SKay Sievers if (dev->kobj.parent->kset == &dev->class->class_dirs) { 96286406245SKay Sievers struct device *d; 96386406245SKay Sievers int other = 0; 96486406245SKay Sievers 96586406245SKay Sievers /* 96686406245SKay Sievers * if we are the last child of our class, delete 96786406245SKay Sievers * our class-directory at this parent 96886406245SKay Sievers */ 96986406245SKay Sievers down(&dev->class->sem); 97086406245SKay Sievers list_for_each_entry(d, &dev->class->devices, node) { 97186406245SKay Sievers if (d == dev) 97286406245SKay Sievers continue; 97386406245SKay Sievers if (d->kobj.parent == dev->kobj.parent) { 97486406245SKay Sievers other = 1; 97586406245SKay Sievers break; 97686406245SKay Sievers } 97786406245SKay Sievers } 97886406245SKay Sievers if (!other) 97986406245SKay Sievers kobject_del(dev->kobj.parent); 98086406245SKay Sievers 98186406245SKay Sievers kobject_put(dev->kobj.parent); 98286406245SKay Sievers up(&dev->class->sem); 98386406245SKay Sievers } 984b9d9c82bSKay Sievers } 985ad6a1e1cSTejun Heo device_remove_file(dev, &uevent_attr); 9862620efefSGreg Kroah-Hartman device_remove_attrs(dev); 98728953533SBenjamin Herrenschmidt bus_remove_device(dev); 9881da177e4SLinus Torvalds 9892f8d16a9STejun Heo /* 9902f8d16a9STejun Heo * Some platform devices are driven without driver attached 9912f8d16a9STejun Heo * and managed resources may have been acquired. Make sure 9922f8d16a9STejun Heo * all resources are released. 9932f8d16a9STejun Heo */ 9942f8d16a9STejun Heo devres_release_all(dev); 9952f8d16a9STejun Heo 9961da177e4SLinus Torvalds /* Notify the platform of the removal, in case they 9971da177e4SLinus Torvalds * need to do anything... 9981da177e4SLinus Torvalds */ 9991da177e4SLinus Torvalds if (platform_notify_remove) 10001da177e4SLinus Torvalds platform_notify_remove(dev); 1001116af378SBenjamin Herrenschmidt if (dev->bus) 1002116af378SBenjamin Herrenschmidt blocking_notifier_call_chain(&dev->bus->bus_notifier, 1003116af378SBenjamin Herrenschmidt BUS_NOTIFY_DEL_DEVICE, dev); 10041da177e4SLinus Torvalds device_pm_remove(dev); 1005312c004dSKay Sievers kobject_uevent(&dev->kobj, KOBJ_REMOVE); 10061da177e4SLinus Torvalds kobject_del(&dev->kobj); 10071da177e4SLinus Torvalds if (parent) 10081da177e4SLinus Torvalds put_device(parent); 10091da177e4SLinus Torvalds } 10101da177e4SLinus Torvalds 10111da177e4SLinus Torvalds /** 10121da177e4SLinus Torvalds * device_unregister - unregister device from system. 10131da177e4SLinus Torvalds * @dev: device going away. 10141da177e4SLinus Torvalds * 10151da177e4SLinus Torvalds * We do this in two parts, like we do device_register(). First, 10161da177e4SLinus Torvalds * we remove it from all the subsystems with device_del(), then 10171da177e4SLinus Torvalds * we decrement the reference count via put_device(). If that 10181da177e4SLinus Torvalds * is the final reference count, the device will be cleaned up 10191da177e4SLinus Torvalds * via device_release() above. Otherwise, the structure will 10201da177e4SLinus Torvalds * stick around until the final reference to the device is dropped. 10211da177e4SLinus Torvalds */ 10221da177e4SLinus Torvalds void device_unregister(struct device * dev) 10231da177e4SLinus Torvalds { 10241da177e4SLinus Torvalds pr_debug("DEV: Unregistering device. ID = '%s'\n", dev->bus_id); 10251da177e4SLinus Torvalds device_del(dev); 10261da177e4SLinus Torvalds put_device(dev); 10271da177e4SLinus Torvalds } 10281da177e4SLinus Torvalds 10291da177e4SLinus Torvalds 103036239577Smochel@digitalimplant.org static struct device * next_device(struct klist_iter * i) 103136239577Smochel@digitalimplant.org { 103236239577Smochel@digitalimplant.org struct klist_node * n = klist_next(i); 103336239577Smochel@digitalimplant.org return n ? container_of(n, struct device, knode_parent) : NULL; 103436239577Smochel@digitalimplant.org } 103536239577Smochel@digitalimplant.org 10361da177e4SLinus Torvalds /** 10371da177e4SLinus Torvalds * device_for_each_child - device child iterator. 1038c41455fbSRandy Dunlap * @parent: parent struct device. 10391da177e4SLinus Torvalds * @data: data for the callback. 10401da177e4SLinus Torvalds * @fn: function to be called for each device. 10411da177e4SLinus Torvalds * 1042c41455fbSRandy Dunlap * Iterate over @parent's child devices, and call @fn for each, 10431da177e4SLinus Torvalds * passing it @data. 10441da177e4SLinus Torvalds * 10451da177e4SLinus Torvalds * We check the return of @fn each time. If it returns anything 10461da177e4SLinus Torvalds * other than 0, we break out and return that value. 10471da177e4SLinus Torvalds */ 104836239577Smochel@digitalimplant.org int device_for_each_child(struct device * parent, void * data, 10491da177e4SLinus Torvalds int (*fn)(struct device *, void *)) 10501da177e4SLinus Torvalds { 105136239577Smochel@digitalimplant.org struct klist_iter i; 10521da177e4SLinus Torvalds struct device * child; 10531da177e4SLinus Torvalds int error = 0; 10541da177e4SLinus Torvalds 105536239577Smochel@digitalimplant.org klist_iter_init(&parent->klist_children, &i); 105636239577Smochel@digitalimplant.org while ((child = next_device(&i)) && !error) 105736239577Smochel@digitalimplant.org error = fn(child, data); 105836239577Smochel@digitalimplant.org klist_iter_exit(&i); 10591da177e4SLinus Torvalds return error; 10601da177e4SLinus Torvalds } 10611da177e4SLinus Torvalds 10625ab69981SCornelia Huck /** 10635ab69981SCornelia Huck * device_find_child - device iterator for locating a particular device. 10645ab69981SCornelia Huck * @parent: parent struct device 10655ab69981SCornelia Huck * @data: Data to pass to match function 10665ab69981SCornelia Huck * @match: Callback function to check device 10675ab69981SCornelia Huck * 10685ab69981SCornelia Huck * This is similar to the device_for_each_child() function above, but it 10695ab69981SCornelia Huck * returns a reference to a device that is 'found' for later use, as 10705ab69981SCornelia Huck * determined by the @match callback. 10715ab69981SCornelia Huck * 10725ab69981SCornelia Huck * The callback should return 0 if the device doesn't match and non-zero 10735ab69981SCornelia Huck * if it does. If the callback returns non-zero and a reference to the 10745ab69981SCornelia Huck * current device can be obtained, this function will return to the caller 10755ab69981SCornelia Huck * and not iterate over any more devices. 10765ab69981SCornelia Huck */ 10775ab69981SCornelia Huck struct device * device_find_child(struct device *parent, void *data, 10785ab69981SCornelia Huck int (*match)(struct device *, void *)) 10795ab69981SCornelia Huck { 10805ab69981SCornelia Huck struct klist_iter i; 10815ab69981SCornelia Huck struct device *child; 10825ab69981SCornelia Huck 10835ab69981SCornelia Huck if (!parent) 10845ab69981SCornelia Huck return NULL; 10855ab69981SCornelia Huck 10865ab69981SCornelia Huck klist_iter_init(&parent->klist_children, &i); 10875ab69981SCornelia Huck while ((child = next_device(&i))) 10885ab69981SCornelia Huck if (match(child, data) && get_device(child)) 10895ab69981SCornelia Huck break; 10905ab69981SCornelia Huck klist_iter_exit(&i); 10915ab69981SCornelia Huck return child; 10925ab69981SCornelia Huck } 10935ab69981SCornelia Huck 10941da177e4SLinus Torvalds int __init devices_init(void) 10951da177e4SLinus Torvalds { 10961da177e4SLinus Torvalds return subsystem_register(&devices_subsys); 10971da177e4SLinus Torvalds } 10981da177e4SLinus Torvalds 10991da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(device_for_each_child); 11005ab69981SCornelia Huck EXPORT_SYMBOL_GPL(device_find_child); 11011da177e4SLinus Torvalds 11021da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(device_initialize); 11031da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(device_add); 11041da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(device_register); 11051da177e4SLinus Torvalds 11061da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(device_del); 11071da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(device_unregister); 11081da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(get_device); 11091da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(put_device); 11101da177e4SLinus Torvalds 11111da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(device_create_file); 11121da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(device_remove_file); 111323681e47SGreg Kroah-Hartman 111423681e47SGreg Kroah-Hartman 111523681e47SGreg Kroah-Hartman static void device_create_release(struct device *dev) 111623681e47SGreg Kroah-Hartman { 111723681e47SGreg Kroah-Hartman pr_debug("%s called for %s\n", __FUNCTION__, dev->bus_id); 111823681e47SGreg Kroah-Hartman kfree(dev); 111923681e47SGreg Kroah-Hartman } 112023681e47SGreg Kroah-Hartman 112123681e47SGreg Kroah-Hartman /** 112223681e47SGreg Kroah-Hartman * device_create - creates a device and registers it with sysfs 112342734dafSHenrik Kretzschmar * @class: pointer to the struct class that this device should be registered to 112442734dafSHenrik Kretzschmar * @parent: pointer to the parent struct device of this new device, if any 112542734dafSHenrik Kretzschmar * @devt: the dev_t for the char device to be added 112642734dafSHenrik Kretzschmar * @fmt: string for the device's name 112723681e47SGreg Kroah-Hartman * 112842734dafSHenrik Kretzschmar * This function can be used by char device classes. A struct device 112942734dafSHenrik Kretzschmar * will be created in sysfs, registered to the specified class. 113042734dafSHenrik Kretzschmar * 113123681e47SGreg Kroah-Hartman * A "dev" file will be created, showing the dev_t for the device, if 113223681e47SGreg Kroah-Hartman * the dev_t is not 0,0. 113342734dafSHenrik Kretzschmar * If a pointer to a parent struct device is passed in, the newly created 113442734dafSHenrik Kretzschmar * struct device will be a child of that device in sysfs. 113542734dafSHenrik Kretzschmar * The pointer to the struct device will be returned from the call. 113642734dafSHenrik Kretzschmar * Any further sysfs files that might be required can be created using this 113723681e47SGreg Kroah-Hartman * pointer. 113823681e47SGreg Kroah-Hartman * 113923681e47SGreg Kroah-Hartman * Note: the struct class passed to this function must have previously 114023681e47SGreg Kroah-Hartman * been created with a call to class_create(). 114123681e47SGreg Kroah-Hartman */ 114223681e47SGreg Kroah-Hartman struct device *device_create(struct class *class, struct device *parent, 11435cbe5f8aSGreg Kroah-Hartman dev_t devt, const char *fmt, ...) 114423681e47SGreg Kroah-Hartman { 114523681e47SGreg Kroah-Hartman va_list args; 114623681e47SGreg Kroah-Hartman struct device *dev = NULL; 114723681e47SGreg Kroah-Hartman int retval = -ENODEV; 114823681e47SGreg Kroah-Hartman 114923681e47SGreg Kroah-Hartman if (class == NULL || IS_ERR(class)) 115023681e47SGreg Kroah-Hartman goto error; 115123681e47SGreg Kroah-Hartman 115223681e47SGreg Kroah-Hartman dev = kzalloc(sizeof(*dev), GFP_KERNEL); 115323681e47SGreg Kroah-Hartman if (!dev) { 115423681e47SGreg Kroah-Hartman retval = -ENOMEM; 115523681e47SGreg Kroah-Hartman goto error; 115623681e47SGreg Kroah-Hartman } 115723681e47SGreg Kroah-Hartman 115823681e47SGreg Kroah-Hartman dev->devt = devt; 115923681e47SGreg Kroah-Hartman dev->class = class; 116023681e47SGreg Kroah-Hartman dev->parent = parent; 116123681e47SGreg Kroah-Hartman dev->release = device_create_release; 116223681e47SGreg Kroah-Hartman 116323681e47SGreg Kroah-Hartman va_start(args, fmt); 116423681e47SGreg Kroah-Hartman vsnprintf(dev->bus_id, BUS_ID_SIZE, fmt, args); 116523681e47SGreg Kroah-Hartman va_end(args); 116623681e47SGreg Kroah-Hartman retval = device_register(dev); 116723681e47SGreg Kroah-Hartman if (retval) 116823681e47SGreg Kroah-Hartman goto error; 116923681e47SGreg Kroah-Hartman 117023681e47SGreg Kroah-Hartman return dev; 117123681e47SGreg Kroah-Hartman 117223681e47SGreg Kroah-Hartman error: 117323681e47SGreg Kroah-Hartman kfree(dev); 117423681e47SGreg Kroah-Hartman return ERR_PTR(retval); 117523681e47SGreg Kroah-Hartman } 117623681e47SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(device_create); 117723681e47SGreg Kroah-Hartman 117823681e47SGreg Kroah-Hartman /** 117923681e47SGreg Kroah-Hartman * device_destroy - removes a device that was created with device_create() 118042734dafSHenrik Kretzschmar * @class: pointer to the struct class that this device was registered with 118142734dafSHenrik Kretzschmar * @devt: the dev_t of the device that was previously registered 118223681e47SGreg Kroah-Hartman * 118342734dafSHenrik Kretzschmar * This call unregisters and cleans up a device that was created with a 118442734dafSHenrik Kretzschmar * call to device_create(). 118523681e47SGreg Kroah-Hartman */ 118623681e47SGreg Kroah-Hartman void device_destroy(struct class *class, dev_t devt) 118723681e47SGreg Kroah-Hartman { 118823681e47SGreg Kroah-Hartman struct device *dev = NULL; 118923681e47SGreg Kroah-Hartman struct device *dev_tmp; 119023681e47SGreg Kroah-Hartman 119123681e47SGreg Kroah-Hartman down(&class->sem); 119223681e47SGreg Kroah-Hartman list_for_each_entry(dev_tmp, &class->devices, node) { 119323681e47SGreg Kroah-Hartman if (dev_tmp->devt == devt) { 119423681e47SGreg Kroah-Hartman dev = dev_tmp; 119523681e47SGreg Kroah-Hartman break; 119623681e47SGreg Kroah-Hartman } 119723681e47SGreg Kroah-Hartman } 119823681e47SGreg Kroah-Hartman up(&class->sem); 119923681e47SGreg Kroah-Hartman 12005d9fd169SGreg Kroah-Hartman if (dev) 120123681e47SGreg Kroah-Hartman device_unregister(dev); 120223681e47SGreg Kroah-Hartman } 120323681e47SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(device_destroy); 1204a2de48caSGreg Kroah-Hartman 1205a2de48caSGreg Kroah-Hartman /** 1206a2de48caSGreg Kroah-Hartman * device_rename - renames a device 1207a2de48caSGreg Kroah-Hartman * @dev: the pointer to the struct device to be renamed 1208a2de48caSGreg Kroah-Hartman * @new_name: the new name of the device 1209a2de48caSGreg Kroah-Hartman */ 1210a2de48caSGreg Kroah-Hartman int device_rename(struct device *dev, char *new_name) 1211a2de48caSGreg Kroah-Hartman { 1212a2de48caSGreg Kroah-Hartman char *old_class_name = NULL; 1213a2de48caSGreg Kroah-Hartman char *new_class_name = NULL; 12142ee97cafSCornelia Huck char *old_device_name = NULL; 1215a2de48caSGreg Kroah-Hartman int error; 1216a2de48caSGreg Kroah-Hartman 1217a2de48caSGreg Kroah-Hartman dev = get_device(dev); 1218a2de48caSGreg Kroah-Hartman if (!dev) 1219a2de48caSGreg Kroah-Hartman return -EINVAL; 1220a2de48caSGreg Kroah-Hartman 1221a2de48caSGreg Kroah-Hartman pr_debug("DEVICE: renaming '%s' to '%s'\n", dev->bus_id, new_name); 1222a2de48caSGreg Kroah-Hartman 122399ef3ef8SKay Sievers #ifdef CONFIG_SYSFS_DEPRECATED 1224a2de48caSGreg Kroah-Hartman if ((dev->class) && (dev->parent)) 1225a2de48caSGreg Kroah-Hartman old_class_name = make_class_name(dev->class->name, &dev->kobj); 122699ef3ef8SKay Sievers #endif 1227a2de48caSGreg Kroah-Hartman 12282ee97cafSCornelia Huck old_device_name = kmalloc(BUS_ID_SIZE, GFP_KERNEL); 12292ee97cafSCornelia Huck if (!old_device_name) { 1230952ab431SJesper Juhl error = -ENOMEM; 12312ee97cafSCornelia Huck goto out; 1232952ab431SJesper Juhl } 12332ee97cafSCornelia Huck strlcpy(old_device_name, dev->bus_id, BUS_ID_SIZE); 1234a2de48caSGreg Kroah-Hartman strlcpy(dev->bus_id, new_name, BUS_ID_SIZE); 1235a2de48caSGreg Kroah-Hartman 1236a2de48caSGreg Kroah-Hartman error = kobject_rename(&dev->kobj, new_name); 12372ee97cafSCornelia Huck if (error) { 12382ee97cafSCornelia Huck strlcpy(dev->bus_id, old_device_name, BUS_ID_SIZE); 12392ee97cafSCornelia Huck goto out; 12402ee97cafSCornelia Huck } 1241a2de48caSGreg Kroah-Hartman 124299ef3ef8SKay Sievers #ifdef CONFIG_SYSFS_DEPRECATED 1243a2de48caSGreg Kroah-Hartman if (old_class_name) { 1244a2de48caSGreg Kroah-Hartman new_class_name = make_class_name(dev->class->name, &dev->kobj); 1245a2de48caSGreg Kroah-Hartman if (new_class_name) { 12462ee97cafSCornelia Huck error = sysfs_create_link(&dev->parent->kobj, 12472ee97cafSCornelia Huck &dev->kobj, new_class_name); 12482ee97cafSCornelia Huck if (error) 12492ee97cafSCornelia Huck goto out; 1250a2de48caSGreg Kroah-Hartman sysfs_remove_link(&dev->parent->kobj, old_class_name); 1251a2de48caSGreg Kroah-Hartman } 1252a2de48caSGreg Kroah-Hartman } 125399ef3ef8SKay Sievers #endif 125499ef3ef8SKay Sievers 1255a2de48caSGreg Kroah-Hartman if (dev->class) { 12562ee97cafSCornelia Huck sysfs_remove_link(&dev->class->subsys.kobj, old_device_name); 12572ee97cafSCornelia Huck error = sysfs_create_link(&dev->class->subsys.kobj, &dev->kobj, 1258a2de48caSGreg Kroah-Hartman dev->bus_id); 12592ee97cafSCornelia Huck if (error) { 12602ee97cafSCornelia Huck /* Uh... how to unravel this if restoring can fail? */ 12612ee97cafSCornelia Huck dev_err(dev, "%s: sysfs_create_symlink failed (%d)\n", 12622ee97cafSCornelia Huck __FUNCTION__, error); 1263a2de48caSGreg Kroah-Hartman } 12642ee97cafSCornelia Huck } 12652ee97cafSCornelia Huck out: 1266a2de48caSGreg Kroah-Hartman put_device(dev); 1267a2de48caSGreg Kroah-Hartman 1268a2de48caSGreg Kroah-Hartman kfree(new_class_name); 1269952ab431SJesper Juhl kfree(old_class_name); 12702ee97cafSCornelia Huck kfree(old_device_name); 1271a2de48caSGreg Kroah-Hartman 1272a2de48caSGreg Kroah-Hartman return error; 1273a2de48caSGreg Kroah-Hartman } 1274a2807dbcSJohannes Berg EXPORT_SYMBOL_GPL(device_rename); 12758a82472fSCornelia Huck 12768a82472fSCornelia Huck static int device_move_class_links(struct device *dev, 12778a82472fSCornelia Huck struct device *old_parent, 12788a82472fSCornelia Huck struct device *new_parent) 12798a82472fSCornelia Huck { 1280f7f3461dSGreg Kroah-Hartman int error = 0; 12818a82472fSCornelia Huck #ifdef CONFIG_SYSFS_DEPRECATED 12828a82472fSCornelia Huck char *class_name; 12838a82472fSCornelia Huck 12848a82472fSCornelia Huck class_name = make_class_name(dev->class->name, &dev->kobj); 12858a82472fSCornelia Huck if (!class_name) { 1286cb360bbfSCornelia Huck error = -ENOMEM; 12878a82472fSCornelia Huck goto out; 12888a82472fSCornelia Huck } 12898a82472fSCornelia Huck if (old_parent) { 12908a82472fSCornelia Huck sysfs_remove_link(&dev->kobj, "device"); 12918a82472fSCornelia Huck sysfs_remove_link(&old_parent->kobj, class_name); 12928a82472fSCornelia Huck } 1293c744aeaeSCornelia Huck if (new_parent) { 1294c744aeaeSCornelia Huck error = sysfs_create_link(&dev->kobj, &new_parent->kobj, 1295c744aeaeSCornelia Huck "device"); 12968a82472fSCornelia Huck if (error) 12978a82472fSCornelia Huck goto out; 1298c744aeaeSCornelia Huck error = sysfs_create_link(&new_parent->kobj, &dev->kobj, 1299c744aeaeSCornelia Huck class_name); 13008a82472fSCornelia Huck if (error) 13018a82472fSCornelia Huck sysfs_remove_link(&dev->kobj, "device"); 1302c744aeaeSCornelia Huck } 1303c744aeaeSCornelia Huck else 1304c744aeaeSCornelia Huck error = 0; 13058a82472fSCornelia Huck out: 13068a82472fSCornelia Huck kfree(class_name); 13078a82472fSCornelia Huck return error; 13088a82472fSCornelia Huck #else 1309f7f3461dSGreg Kroah-Hartman if (old_parent) 1310f7f3461dSGreg Kroah-Hartman sysfs_remove_link(&dev->kobj, "device"); 1311f7f3461dSGreg Kroah-Hartman if (new_parent) 1312f7f3461dSGreg Kroah-Hartman error = sysfs_create_link(&dev->kobj, &new_parent->kobj, 1313f7f3461dSGreg Kroah-Hartman "device"); 1314f7f3461dSGreg Kroah-Hartman return error; 13158a82472fSCornelia Huck #endif 13168a82472fSCornelia Huck } 13178a82472fSCornelia Huck 13188a82472fSCornelia Huck /** 13198a82472fSCornelia Huck * device_move - moves a device to a new parent 13208a82472fSCornelia Huck * @dev: the pointer to the struct device to be moved 1321c744aeaeSCornelia Huck * @new_parent: the new parent of the device (can by NULL) 13228a82472fSCornelia Huck */ 13238a82472fSCornelia Huck int device_move(struct device *dev, struct device *new_parent) 13248a82472fSCornelia Huck { 13258a82472fSCornelia Huck int error; 13268a82472fSCornelia Huck struct device *old_parent; 1327c744aeaeSCornelia Huck struct kobject *new_parent_kobj; 13288a82472fSCornelia Huck 13298a82472fSCornelia Huck dev = get_device(dev); 13308a82472fSCornelia Huck if (!dev) 13318a82472fSCornelia Huck return -EINVAL; 13328a82472fSCornelia Huck 13338a82472fSCornelia Huck new_parent = get_device(new_parent); 1334c744aeaeSCornelia Huck new_parent_kobj = get_device_parent (dev, new_parent); 1335c744aeaeSCornelia Huck if (IS_ERR(new_parent_kobj)) { 1336c744aeaeSCornelia Huck error = PTR_ERR(new_parent_kobj); 1337c744aeaeSCornelia Huck put_device(new_parent); 13388a82472fSCornelia Huck goto out; 13398a82472fSCornelia Huck } 13408a82472fSCornelia Huck pr_debug("DEVICE: moving '%s' to '%s'\n", dev->bus_id, 1341c744aeaeSCornelia Huck new_parent ? new_parent->bus_id : "<NULL>"); 1342c744aeaeSCornelia Huck error = kobject_move(&dev->kobj, new_parent_kobj); 13438a82472fSCornelia Huck if (error) { 13448a82472fSCornelia Huck put_device(new_parent); 13458a82472fSCornelia Huck goto out; 13468a82472fSCornelia Huck } 13478a82472fSCornelia Huck old_parent = dev->parent; 13488a82472fSCornelia Huck dev->parent = new_parent; 13498a82472fSCornelia Huck if (old_parent) 1350acf02d23SCornelia Huck klist_remove(&dev->knode_parent); 1351c744aeaeSCornelia Huck if (new_parent) 13528a82472fSCornelia Huck klist_add_tail(&dev->knode_parent, &new_parent->klist_children); 13538a82472fSCornelia Huck if (!dev->class) 13548a82472fSCornelia Huck goto out_put; 13558a82472fSCornelia Huck error = device_move_class_links(dev, old_parent, new_parent); 13568a82472fSCornelia Huck if (error) { 13578a82472fSCornelia Huck /* We ignore errors on cleanup since we're hosed anyway... */ 13588a82472fSCornelia Huck device_move_class_links(dev, new_parent, old_parent); 13598a82472fSCornelia Huck if (!kobject_move(&dev->kobj, &old_parent->kobj)) { 1360c744aeaeSCornelia Huck if (new_parent) 1361acf02d23SCornelia Huck klist_remove(&dev->knode_parent); 13628a82472fSCornelia Huck if (old_parent) 13638a82472fSCornelia Huck klist_add_tail(&dev->knode_parent, 13648a82472fSCornelia Huck &old_parent->klist_children); 13658a82472fSCornelia Huck } 13668a82472fSCornelia Huck put_device(new_parent); 13678a82472fSCornelia Huck goto out; 13688a82472fSCornelia Huck } 13698a82472fSCornelia Huck out_put: 13708a82472fSCornelia Huck put_device(old_parent); 13718a82472fSCornelia Huck out: 13728a82472fSCornelia Huck put_device(dev); 13738a82472fSCornelia Huck return error; 13748a82472fSCornelia Huck } 13758a82472fSCornelia Huck 13768a82472fSCornelia Huck EXPORT_SYMBOL_GPL(device_move); 1377