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 : 463e95637aSAlan Stern (dev->bus ? dev->bus->name : ""); 473e95637aSAlan Stern } 48310a922dSMatthew Wilcox EXPORT_SYMBOL(dev_driver_string); 493e95637aSAlan Stern 501da177e4SLinus Torvalds #define to_dev(obj) container_of(obj, struct device, kobj) 511da177e4SLinus Torvalds #define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr) 521da177e4SLinus Torvalds 531da177e4SLinus Torvalds static ssize_t 541da177e4SLinus Torvalds dev_attr_show(struct kobject * kobj, struct attribute * attr, char * buf) 551da177e4SLinus Torvalds { 561da177e4SLinus Torvalds struct device_attribute * dev_attr = to_dev_attr(attr); 571da177e4SLinus Torvalds struct device * dev = to_dev(kobj); 584a0c20bfSDmitry Torokhov ssize_t ret = -EIO; 591da177e4SLinus Torvalds 601da177e4SLinus Torvalds if (dev_attr->show) 6154b6f35cSYani Ioannou ret = dev_attr->show(dev, dev_attr, buf); 621da177e4SLinus Torvalds return ret; 631da177e4SLinus Torvalds } 641da177e4SLinus Torvalds 651da177e4SLinus Torvalds static ssize_t 661da177e4SLinus Torvalds dev_attr_store(struct kobject * kobj, struct attribute * attr, 671da177e4SLinus Torvalds const char * buf, size_t count) 681da177e4SLinus Torvalds { 691da177e4SLinus Torvalds struct device_attribute * dev_attr = to_dev_attr(attr); 701da177e4SLinus Torvalds struct device * dev = to_dev(kobj); 714a0c20bfSDmitry Torokhov ssize_t ret = -EIO; 721da177e4SLinus Torvalds 731da177e4SLinus Torvalds if (dev_attr->store) 7454b6f35cSYani Ioannou ret = dev_attr->store(dev, dev_attr, buf, count); 751da177e4SLinus Torvalds return ret; 761da177e4SLinus Torvalds } 771da177e4SLinus Torvalds 781da177e4SLinus Torvalds static struct sysfs_ops dev_sysfs_ops = { 791da177e4SLinus Torvalds .show = dev_attr_show, 801da177e4SLinus Torvalds .store = dev_attr_store, 811da177e4SLinus Torvalds }; 821da177e4SLinus Torvalds 831da177e4SLinus Torvalds 841da177e4SLinus Torvalds /** 851da177e4SLinus Torvalds * device_release - free device structure. 861da177e4SLinus Torvalds * @kobj: device's kobject. 871da177e4SLinus Torvalds * 881da177e4SLinus Torvalds * This is called once the reference count for the object 891da177e4SLinus Torvalds * reaches 0. We forward the call to the device's release 901da177e4SLinus Torvalds * method, which should handle actually freeing the structure. 911da177e4SLinus Torvalds */ 921da177e4SLinus Torvalds static void device_release(struct kobject * kobj) 931da177e4SLinus Torvalds { 941da177e4SLinus Torvalds struct device * dev = to_dev(kobj); 951da177e4SLinus Torvalds 961da177e4SLinus Torvalds if (dev->release) 971da177e4SLinus Torvalds dev->release(dev); 98f9f852dfSKay Sievers else if (dev->type && dev->type->release) 99f9f852dfSKay Sievers dev->type->release(dev); 1002620efefSGreg Kroah-Hartman else if (dev->class && dev->class->dev_release) 1012620efefSGreg Kroah-Hartman dev->class->dev_release(dev); 1021da177e4SLinus Torvalds else { 1031da177e4SLinus Torvalds printk(KERN_ERR "Device '%s' does not have a release() function, " 1041da177e4SLinus Torvalds "it is broken and must be fixed.\n", 1051da177e4SLinus Torvalds dev->bus_id); 1061da177e4SLinus Torvalds WARN_ON(1); 1071da177e4SLinus Torvalds } 1081da177e4SLinus Torvalds } 1091da177e4SLinus Torvalds 1101da177e4SLinus Torvalds static struct kobj_type ktype_device = { 1111da177e4SLinus Torvalds .release = device_release, 1121da177e4SLinus Torvalds .sysfs_ops = &dev_sysfs_ops, 1131da177e4SLinus Torvalds }; 1141da177e4SLinus Torvalds 1151da177e4SLinus Torvalds 116312c004dSKay Sievers static int dev_uevent_filter(struct kset *kset, struct kobject *kobj) 1171da177e4SLinus Torvalds { 1181da177e4SLinus Torvalds struct kobj_type *ktype = get_ktype(kobj); 1191da177e4SLinus Torvalds 1201da177e4SLinus Torvalds if (ktype == &ktype_device) { 1211da177e4SLinus Torvalds struct device *dev = to_dev(kobj); 1221da177e4SLinus Torvalds if (dev->bus) 1231da177e4SLinus Torvalds return 1; 12423681e47SGreg Kroah-Hartman if (dev->class) 12523681e47SGreg Kroah-Hartman return 1; 1261da177e4SLinus Torvalds } 1271da177e4SLinus Torvalds return 0; 1281da177e4SLinus Torvalds } 1291da177e4SLinus Torvalds 130312c004dSKay Sievers static const char *dev_uevent_name(struct kset *kset, struct kobject *kobj) 1311da177e4SLinus Torvalds { 1321da177e4SLinus Torvalds struct device *dev = to_dev(kobj); 1331da177e4SLinus Torvalds 13423681e47SGreg Kroah-Hartman if (dev->bus) 1351da177e4SLinus Torvalds return dev->bus->name; 13623681e47SGreg Kroah-Hartman if (dev->class) 13723681e47SGreg Kroah-Hartman return dev->class->name; 13823681e47SGreg Kroah-Hartman return NULL; 1391da177e4SLinus Torvalds } 1401da177e4SLinus Torvalds 141312c004dSKay Sievers static int dev_uevent(struct kset *kset, struct kobject *kobj, char **envp, 1421da177e4SLinus Torvalds int num_envp, char *buffer, int buffer_size) 1431da177e4SLinus Torvalds { 1441da177e4SLinus Torvalds struct device *dev = to_dev(kobj); 1451da177e4SLinus Torvalds int i = 0; 1461da177e4SLinus Torvalds int length = 0; 1471da177e4SLinus Torvalds int retval = 0; 1481da177e4SLinus Torvalds 14923681e47SGreg Kroah-Hartman /* add the major/minor if present */ 15023681e47SGreg Kroah-Hartman if (MAJOR(dev->devt)) { 15123681e47SGreg Kroah-Hartman add_uevent_var(envp, num_envp, &i, 15223681e47SGreg Kroah-Hartman buffer, buffer_size, &length, 15323681e47SGreg Kroah-Hartman "MAJOR=%u", MAJOR(dev->devt)); 15423681e47SGreg Kroah-Hartman add_uevent_var(envp, num_envp, &i, 15523681e47SGreg Kroah-Hartman buffer, buffer_size, &length, 15623681e47SGreg Kroah-Hartman "MINOR=%u", MINOR(dev->devt)); 15723681e47SGreg Kroah-Hartman } 15823681e47SGreg Kroah-Hartman 159239378f1SKay Sievers if (dev->driver) 160d81d9d6bSKay Sievers add_uevent_var(envp, num_envp, &i, 161d81d9d6bSKay Sievers buffer, buffer_size, &length, 162d81d9d6bSKay Sievers "DRIVER=%s", dev->driver->name); 163239378f1SKay Sievers 164a87cb2acSKay Sievers #ifdef CONFIG_SYSFS_DEPRECATED 165239378f1SKay Sievers if (dev->class) { 166239378f1SKay Sievers struct device *parent = dev->parent; 167239378f1SKay Sievers 168239378f1SKay Sievers /* find first bus device in parent chain */ 169239378f1SKay Sievers while (parent && !parent->bus) 170239378f1SKay Sievers parent = parent->parent; 171239378f1SKay Sievers if (parent && parent->bus) { 172239378f1SKay Sievers const char *path; 173239378f1SKay Sievers 174239378f1SKay Sievers path = kobject_get_path(&parent->kobj, GFP_KERNEL); 175239378f1SKay Sievers add_uevent_var(envp, num_envp, &i, 176239378f1SKay Sievers buffer, buffer_size, &length, 177239378f1SKay Sievers "PHYSDEVPATH=%s", path); 178239378f1SKay Sievers kfree(path); 179239378f1SKay Sievers 180239378f1SKay Sievers add_uevent_var(envp, num_envp, &i, 181239378f1SKay Sievers buffer, buffer_size, &length, 182239378f1SKay Sievers "PHYSDEVBUS=%s", parent->bus->name); 183239378f1SKay Sievers 184239378f1SKay Sievers if (parent->driver) 185239378f1SKay Sievers add_uevent_var(envp, num_envp, &i, 186239378f1SKay Sievers buffer, buffer_size, &length, 187239378f1SKay Sievers "PHYSDEVDRIVER=%s", parent->driver->name); 188239378f1SKay Sievers } 189239378f1SKay Sievers } else if (dev->bus) { 190239378f1SKay Sievers add_uevent_var(envp, num_envp, &i, 191239378f1SKay Sievers buffer, buffer_size, &length, 192239378f1SKay Sievers "PHYSDEVBUS=%s", dev->bus->name); 193239378f1SKay Sievers 194239378f1SKay Sievers if (dev->driver) 195312c004dSKay Sievers add_uevent_var(envp, num_envp, &i, 1961da177e4SLinus Torvalds buffer, buffer_size, &length, 1971da177e4SLinus Torvalds "PHYSDEVDRIVER=%s", dev->driver->name); 198d81d9d6bSKay Sievers } 199239378f1SKay Sievers #endif 2001da177e4SLinus Torvalds 2011da177e4SLinus Torvalds /* terminate, set to next free slot, shrink available space */ 2021da177e4SLinus Torvalds envp[i] = NULL; 2031da177e4SLinus Torvalds envp = &envp[i]; 2041da177e4SLinus Torvalds num_envp -= i; 2051da177e4SLinus Torvalds buffer = &buffer[length]; 2061da177e4SLinus Torvalds buffer_size -= length; 2071da177e4SLinus Torvalds 208312c004dSKay Sievers if (dev->bus && dev->bus->uevent) { 2091da177e4SLinus Torvalds /* have the bus specific function add its stuff */ 210312c004dSKay Sievers retval = dev->bus->uevent(dev, envp, num_envp, buffer, buffer_size); 211f9f852dfSKay Sievers if (retval) 212f9f852dfSKay Sievers pr_debug ("%s: bus uevent() returned %d\n", 2131da177e4SLinus Torvalds __FUNCTION__, retval); 2141da177e4SLinus Torvalds } 2151da177e4SLinus Torvalds 2162620efefSGreg Kroah-Hartman if (dev->class && dev->class->dev_uevent) { 2172620efefSGreg Kroah-Hartman /* have the class specific function add its stuff */ 2182620efefSGreg Kroah-Hartman retval = dev->class->dev_uevent(dev, envp, num_envp, buffer, buffer_size); 219f9f852dfSKay Sievers if (retval) 220f9f852dfSKay Sievers pr_debug("%s: class uevent() returned %d\n", 2212620efefSGreg Kroah-Hartman __FUNCTION__, retval); 2222620efefSGreg Kroah-Hartman } 223f9f852dfSKay Sievers 224f9f852dfSKay Sievers if (dev->type && dev->type->uevent) { 225f9f852dfSKay Sievers /* have the device type specific fuction add its stuff */ 226f9f852dfSKay Sievers retval = dev->type->uevent(dev, envp, num_envp, buffer, buffer_size); 227f9f852dfSKay Sievers if (retval) 228f9f852dfSKay Sievers pr_debug("%s: dev_type uevent() returned %d\n", 229f9f852dfSKay Sievers __FUNCTION__, retval); 2302620efefSGreg Kroah-Hartman } 2312620efefSGreg Kroah-Hartman 2321da177e4SLinus Torvalds return retval; 2331da177e4SLinus Torvalds } 2341da177e4SLinus Torvalds 235312c004dSKay Sievers static struct kset_uevent_ops device_uevent_ops = { 236312c004dSKay Sievers .filter = dev_uevent_filter, 237312c004dSKay Sievers .name = dev_uevent_name, 238312c004dSKay Sievers .uevent = dev_uevent, 2391da177e4SLinus Torvalds }; 2401da177e4SLinus Torvalds 241a7fd6706SKay Sievers static ssize_t store_uevent(struct device *dev, struct device_attribute *attr, 242a7fd6706SKay Sievers const char *buf, size_t count) 243a7fd6706SKay Sievers { 244312c004dSKay Sievers kobject_uevent(&dev->kobj, KOBJ_ADD); 245a7fd6706SKay Sievers return count; 246a7fd6706SKay Sievers } 247a7fd6706SKay Sievers 248de0ff00dSGreg Kroah-Hartman static int device_add_groups(struct device *dev) 249de0ff00dSGreg Kroah-Hartman { 250de0ff00dSGreg Kroah-Hartman int i; 251de0ff00dSGreg Kroah-Hartman int error = 0; 252de0ff00dSGreg Kroah-Hartman 253de0ff00dSGreg Kroah-Hartman if (dev->groups) { 254de0ff00dSGreg Kroah-Hartman for (i = 0; dev->groups[i]; i++) { 255de0ff00dSGreg Kroah-Hartman error = sysfs_create_group(&dev->kobj, dev->groups[i]); 256de0ff00dSGreg Kroah-Hartman if (error) { 257de0ff00dSGreg Kroah-Hartman while (--i >= 0) 258de0ff00dSGreg Kroah-Hartman sysfs_remove_group(&dev->kobj, dev->groups[i]); 259de0ff00dSGreg Kroah-Hartman goto out; 260de0ff00dSGreg Kroah-Hartman } 261de0ff00dSGreg Kroah-Hartman } 262de0ff00dSGreg Kroah-Hartman } 263de0ff00dSGreg Kroah-Hartman out: 264de0ff00dSGreg Kroah-Hartman return error; 265de0ff00dSGreg Kroah-Hartman } 266de0ff00dSGreg Kroah-Hartman 267de0ff00dSGreg Kroah-Hartman static void device_remove_groups(struct device *dev) 268de0ff00dSGreg Kroah-Hartman { 269de0ff00dSGreg Kroah-Hartman int i; 270de0ff00dSGreg Kroah-Hartman if (dev->groups) { 271de0ff00dSGreg Kroah-Hartman for (i = 0; dev->groups[i]; i++) { 272de0ff00dSGreg Kroah-Hartman sysfs_remove_group(&dev->kobj, dev->groups[i]); 273de0ff00dSGreg Kroah-Hartman } 274de0ff00dSGreg Kroah-Hartman } 275de0ff00dSGreg Kroah-Hartman } 276de0ff00dSGreg Kroah-Hartman 2772620efefSGreg Kroah-Hartman static int device_add_attrs(struct device *dev) 2782620efefSGreg Kroah-Hartman { 2792620efefSGreg Kroah-Hartman struct class *class = dev->class; 280f9f852dfSKay Sievers struct device_type *type = dev->type; 2812620efefSGreg Kroah-Hartman int error = 0; 2822620efefSGreg Kroah-Hartman int i; 2832620efefSGreg Kroah-Hartman 284f9f852dfSKay Sievers if (class && class->dev_attrs) { 2852620efefSGreg Kroah-Hartman for (i = 0; attr_name(class->dev_attrs[i]); i++) { 2862620efefSGreg Kroah-Hartman error = device_create_file(dev, &class->dev_attrs[i]); 2872620efefSGreg Kroah-Hartman if (error) 2882620efefSGreg Kroah-Hartman break; 2892620efefSGreg Kroah-Hartman } 2902620efefSGreg Kroah-Hartman if (error) 2912620efefSGreg Kroah-Hartman while (--i >= 0) 2922620efefSGreg Kroah-Hartman device_remove_file(dev, &class->dev_attrs[i]); 293f9f852dfSKay Sievers } 294f9f852dfSKay Sievers 295f9f852dfSKay Sievers if (type && type->attrs) { 296f9f852dfSKay Sievers for (i = 0; attr_name(type->attrs[i]); i++) { 297f9f852dfSKay Sievers error = device_create_file(dev, &type->attrs[i]); 298f9f852dfSKay Sievers if (error) 299f9f852dfSKay Sievers break; 300f9f852dfSKay Sievers } 301f9f852dfSKay Sievers if (error) 302f9f852dfSKay Sievers while (--i >= 0) 303f9f852dfSKay Sievers device_remove_file(dev, &type->attrs[i]); 304f9f852dfSKay Sievers } 305f9f852dfSKay Sievers 3062620efefSGreg Kroah-Hartman return error; 3072620efefSGreg Kroah-Hartman } 3082620efefSGreg Kroah-Hartman 3092620efefSGreg Kroah-Hartman static void device_remove_attrs(struct device *dev) 3102620efefSGreg Kroah-Hartman { 3112620efefSGreg Kroah-Hartman struct class *class = dev->class; 312f9f852dfSKay Sievers struct device_type *type = dev->type; 3132620efefSGreg Kroah-Hartman int i; 3142620efefSGreg Kroah-Hartman 315f9f852dfSKay Sievers if (class && class->dev_attrs) { 3162620efefSGreg Kroah-Hartman for (i = 0; attr_name(class->dev_attrs[i]); i++) 3172620efefSGreg Kroah-Hartman device_remove_file(dev, &class->dev_attrs[i]); 3182620efefSGreg Kroah-Hartman } 319f9f852dfSKay Sievers 320f9f852dfSKay Sievers if (type && type->attrs) { 321f9f852dfSKay Sievers for (i = 0; attr_name(type->attrs[i]); i++) 322f9f852dfSKay Sievers device_remove_file(dev, &type->attrs[i]); 323f9f852dfSKay Sievers } 3242620efefSGreg Kroah-Hartman } 3252620efefSGreg Kroah-Hartman 3262620efefSGreg Kroah-Hartman 32723681e47SGreg Kroah-Hartman static ssize_t show_dev(struct device *dev, struct device_attribute *attr, 32823681e47SGreg Kroah-Hartman char *buf) 32923681e47SGreg Kroah-Hartman { 33023681e47SGreg Kroah-Hartman return print_dev_t(buf, dev->devt); 33123681e47SGreg Kroah-Hartman } 33223681e47SGreg Kroah-Hartman 3330863afb3SMartin Waitz /* 3340863afb3SMartin Waitz * devices_subsys - structure to be registered with kobject core. 3351da177e4SLinus Torvalds */ 3361da177e4SLinus Torvalds 337312c004dSKay Sievers decl_subsys(devices, &ktype_device, &device_uevent_ops); 3381da177e4SLinus Torvalds 3391da177e4SLinus Torvalds 3401da177e4SLinus Torvalds /** 3411da177e4SLinus Torvalds * device_create_file - create sysfs attribute file for device. 3421da177e4SLinus Torvalds * @dev: device. 3431da177e4SLinus Torvalds * @attr: device attribute descriptor. 3441da177e4SLinus Torvalds */ 3451da177e4SLinus Torvalds 3461da177e4SLinus Torvalds int device_create_file(struct device * dev, struct device_attribute * attr) 3471da177e4SLinus Torvalds { 3481da177e4SLinus Torvalds int error = 0; 3491da177e4SLinus Torvalds if (get_device(dev)) { 3501da177e4SLinus Torvalds error = sysfs_create_file(&dev->kobj, &attr->attr); 3511da177e4SLinus Torvalds put_device(dev); 3521da177e4SLinus Torvalds } 3531da177e4SLinus Torvalds return error; 3541da177e4SLinus Torvalds } 3551da177e4SLinus Torvalds 3561da177e4SLinus Torvalds /** 3571da177e4SLinus Torvalds * device_remove_file - remove sysfs attribute file. 3581da177e4SLinus Torvalds * @dev: device. 3591da177e4SLinus Torvalds * @attr: device attribute descriptor. 3601da177e4SLinus Torvalds */ 3611da177e4SLinus Torvalds 3621da177e4SLinus Torvalds void device_remove_file(struct device * dev, struct device_attribute * attr) 3631da177e4SLinus Torvalds { 3641da177e4SLinus Torvalds if (get_device(dev)) { 3651da177e4SLinus Torvalds sysfs_remove_file(&dev->kobj, &attr->attr); 3661da177e4SLinus Torvalds put_device(dev); 3671da177e4SLinus Torvalds } 3681da177e4SLinus Torvalds } 3691da177e4SLinus Torvalds 3702589f188SGreg Kroah-Hartman /** 3712589f188SGreg Kroah-Hartman * device_create_bin_file - create sysfs binary attribute file for device. 3722589f188SGreg Kroah-Hartman * @dev: device. 3732589f188SGreg Kroah-Hartman * @attr: device binary attribute descriptor. 3742589f188SGreg Kroah-Hartman */ 3752589f188SGreg Kroah-Hartman int device_create_bin_file(struct device *dev, struct bin_attribute *attr) 3762589f188SGreg Kroah-Hartman { 3772589f188SGreg Kroah-Hartman int error = -EINVAL; 3782589f188SGreg Kroah-Hartman if (dev) 3792589f188SGreg Kroah-Hartman error = sysfs_create_bin_file(&dev->kobj, attr); 3802589f188SGreg Kroah-Hartman return error; 3812589f188SGreg Kroah-Hartman } 3822589f188SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(device_create_bin_file); 3832589f188SGreg Kroah-Hartman 3842589f188SGreg Kroah-Hartman /** 3852589f188SGreg Kroah-Hartman * device_remove_bin_file - remove sysfs binary attribute file 3862589f188SGreg Kroah-Hartman * @dev: device. 3872589f188SGreg Kroah-Hartman * @attr: device binary attribute descriptor. 3882589f188SGreg Kroah-Hartman */ 3892589f188SGreg Kroah-Hartman void device_remove_bin_file(struct device *dev, struct bin_attribute *attr) 3902589f188SGreg Kroah-Hartman { 3912589f188SGreg Kroah-Hartman if (dev) 3922589f188SGreg Kroah-Hartman sysfs_remove_bin_file(&dev->kobj, attr); 3932589f188SGreg Kroah-Hartman } 3942589f188SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(device_remove_bin_file); 3952589f188SGreg Kroah-Hartman 39634bb61f9SJames Bottomley static void klist_children_get(struct klist_node *n) 39734bb61f9SJames Bottomley { 39834bb61f9SJames Bottomley struct device *dev = container_of(n, struct device, knode_parent); 39934bb61f9SJames Bottomley 40034bb61f9SJames Bottomley get_device(dev); 40134bb61f9SJames Bottomley } 40234bb61f9SJames Bottomley 40334bb61f9SJames Bottomley static void klist_children_put(struct klist_node *n) 40434bb61f9SJames Bottomley { 40534bb61f9SJames Bottomley struct device *dev = container_of(n, struct device, knode_parent); 40634bb61f9SJames Bottomley 40734bb61f9SJames Bottomley put_device(dev); 40834bb61f9SJames Bottomley } 40934bb61f9SJames Bottomley 4101da177e4SLinus Torvalds 4111da177e4SLinus Torvalds /** 4121da177e4SLinus Torvalds * device_initialize - init device structure. 4131da177e4SLinus Torvalds * @dev: device. 4141da177e4SLinus Torvalds * 4151da177e4SLinus Torvalds * This prepares the device for use by other layers, 4161da177e4SLinus Torvalds * including adding it to the device hierarchy. 4171da177e4SLinus Torvalds * It is the first half of device_register(), if called by 4181da177e4SLinus Torvalds * that, though it can also be called separately, so one 4191da177e4SLinus Torvalds * may use @dev's fields (e.g. the refcount). 4201da177e4SLinus Torvalds */ 4211da177e4SLinus Torvalds 4221da177e4SLinus Torvalds void device_initialize(struct device *dev) 4231da177e4SLinus Torvalds { 4241da177e4SLinus Torvalds kobj_set_kset_s(dev, devices_subsys); 4251da177e4SLinus Torvalds kobject_init(&dev->kobj); 42634bb61f9SJames Bottomley klist_init(&dev->klist_children, klist_children_get, 42734bb61f9SJames Bottomley klist_children_put); 4281da177e4SLinus Torvalds INIT_LIST_HEAD(&dev->dma_pools); 42923681e47SGreg Kroah-Hartman INIT_LIST_HEAD(&dev->node); 430af70316aSmochel@digitalimplant.org init_MUTEX(&dev->sem); 4310ac85241SDavid Brownell device_init_wakeup(dev, 0); 43287348136SChristoph Hellwig set_dev_node(dev, -1); 4331da177e4SLinus Torvalds } 4341da177e4SLinus Torvalds 43540fa5422SGreg Kroah-Hartman #ifdef CONFIG_SYSFS_DEPRECATED 436c744aeaeSCornelia Huck static struct kobject * get_device_parent(struct device *dev, 437c744aeaeSCornelia Huck struct device *parent) 43840fa5422SGreg Kroah-Hartman { 43940fa5422SGreg Kroah-Hartman /* Set the parent to the class, not the parent device */ 44040fa5422SGreg Kroah-Hartman /* this keeps sysfs from having a symlink to make old udevs happy */ 44140fa5422SGreg Kroah-Hartman if (dev->class) 442c744aeaeSCornelia Huck return &dev->class->subsys.kset.kobj; 44340fa5422SGreg Kroah-Hartman else if (parent) 444c744aeaeSCornelia Huck return &parent->kobj; 44540fa5422SGreg Kroah-Hartman 446c744aeaeSCornelia Huck return NULL; 44740fa5422SGreg Kroah-Hartman } 44840fa5422SGreg Kroah-Hartman #else 449c744aeaeSCornelia Huck static struct kobject * virtual_device_parent(struct device *dev) 450f0ee61a6SGreg Kroah-Hartman { 451f0ee61a6SGreg Kroah-Hartman if (!dev->class) 452c744aeaeSCornelia Huck return ERR_PTR(-ENODEV); 453f0ee61a6SGreg Kroah-Hartman 454f0ee61a6SGreg Kroah-Hartman if (!dev->class->virtual_dir) { 455f0ee61a6SGreg Kroah-Hartman static struct kobject *virtual_dir = NULL; 456f0ee61a6SGreg Kroah-Hartman 457f0ee61a6SGreg Kroah-Hartman if (!virtual_dir) 458f0ee61a6SGreg Kroah-Hartman virtual_dir = kobject_add_dir(&devices_subsys.kset.kobj, "virtual"); 459f0ee61a6SGreg Kroah-Hartman dev->class->virtual_dir = kobject_add_dir(virtual_dir, dev->class->name); 460f0ee61a6SGreg Kroah-Hartman } 461f0ee61a6SGreg Kroah-Hartman 462c744aeaeSCornelia Huck return dev->class->virtual_dir; 463f0ee61a6SGreg Kroah-Hartman } 464f0ee61a6SGreg Kroah-Hartman 465c744aeaeSCornelia Huck static struct kobject * get_device_parent(struct device *dev, 466c744aeaeSCornelia Huck struct device *parent) 46740fa5422SGreg Kroah-Hartman { 46840fa5422SGreg Kroah-Hartman /* if this is a class device, and has no parent, create one */ 46940fa5422SGreg Kroah-Hartman if ((dev->class) && (parent == NULL)) { 470c744aeaeSCornelia Huck return virtual_device_parent(dev); 47140fa5422SGreg Kroah-Hartman } else if (parent) 472c744aeaeSCornelia Huck return &parent->kobj; 473c744aeaeSCornelia Huck return NULL; 474c744aeaeSCornelia Huck } 47540fa5422SGreg Kroah-Hartman 476c744aeaeSCornelia Huck #endif 477c744aeaeSCornelia Huck static int setup_parent(struct device *dev, struct device *parent) 478c744aeaeSCornelia Huck { 479c744aeaeSCornelia Huck struct kobject *kobj; 480c744aeaeSCornelia Huck kobj = get_device_parent(dev, parent); 481c744aeaeSCornelia Huck if (IS_ERR(kobj)) 482c744aeaeSCornelia Huck return PTR_ERR(kobj); 483c744aeaeSCornelia Huck if (kobj) 484c744aeaeSCornelia Huck dev->kobj.parent = kobj; 48540fa5422SGreg Kroah-Hartman return 0; 48640fa5422SGreg Kroah-Hartman } 48740fa5422SGreg Kroah-Hartman 4881da177e4SLinus Torvalds /** 4891da177e4SLinus Torvalds * device_add - add device to device hierarchy. 4901da177e4SLinus Torvalds * @dev: device. 4911da177e4SLinus Torvalds * 4921da177e4SLinus Torvalds * This is part 2 of device_register(), though may be called 4931da177e4SLinus Torvalds * separately _iff_ device_initialize() has been called separately. 4941da177e4SLinus Torvalds * 4951da177e4SLinus Torvalds * This adds it to the kobject hierarchy via kobject_add(), adds it 4961da177e4SLinus Torvalds * to the global and sibling lists for the device, then 4971da177e4SLinus Torvalds * adds it to the other relevant subsystems of the driver model. 4981da177e4SLinus Torvalds */ 4991da177e4SLinus Torvalds int device_add(struct device *dev) 5001da177e4SLinus Torvalds { 5011da177e4SLinus Torvalds struct device *parent = NULL; 502e9a7d305SGreg Kroah-Hartman char *class_name = NULL; 503c47ed219SGreg Kroah-Hartman struct class_interface *class_intf; 5041da177e4SLinus Torvalds int error = -EINVAL; 5051da177e4SLinus Torvalds 5061da177e4SLinus Torvalds dev = get_device(dev); 5071da177e4SLinus Torvalds if (!dev || !strlen(dev->bus_id)) 5081da177e4SLinus Torvalds goto Error; 5091da177e4SLinus Torvalds 51040fa5422SGreg Kroah-Hartman pr_debug("DEV: registering device: ID = '%s'\n", dev->bus_id); 511c205ef48SGreg Kroah-Hartman 5121da177e4SLinus Torvalds parent = get_device(dev->parent); 5131da177e4SLinus Torvalds 51440fa5422SGreg Kroah-Hartman error = setup_parent(dev, parent); 51540fa5422SGreg Kroah-Hartman if (error) 51640fa5422SGreg Kroah-Hartman goto Error; 5171da177e4SLinus Torvalds 5181da177e4SLinus Torvalds /* first, register with generic layer. */ 5191da177e4SLinus Torvalds kobject_set_name(&dev->kobj, "%s", dev->bus_id); 52040fa5422SGreg Kroah-Hartman error = kobject_add(&dev->kobj); 52140fa5422SGreg Kroah-Hartman if (error) 5221da177e4SLinus Torvalds goto Error; 523a7fd6706SKay Sievers 52437022644SBrian Walsh /* notify platform of device entry */ 52537022644SBrian Walsh if (platform_notify) 52637022644SBrian Walsh platform_notify(dev); 52737022644SBrian Walsh 528116af378SBenjamin Herrenschmidt /* notify clients of device entry (new way) */ 529116af378SBenjamin Herrenschmidt if (dev->bus) 530116af378SBenjamin Herrenschmidt blocking_notifier_call_chain(&dev->bus->bus_notifier, 531116af378SBenjamin Herrenschmidt BUS_NOTIFY_ADD_DEVICE, dev); 532116af378SBenjamin Herrenschmidt 533a7fd6706SKay Sievers dev->uevent_attr.attr.name = "uevent"; 534a7fd6706SKay Sievers dev->uevent_attr.attr.mode = S_IWUSR; 535a7fd6706SKay Sievers if (dev->driver) 536a7fd6706SKay Sievers dev->uevent_attr.attr.owner = dev->driver->owner; 537a7fd6706SKay Sievers dev->uevent_attr.store = store_uevent; 538a306eea4SCornelia Huck error = device_create_file(dev, &dev->uevent_attr); 539a306eea4SCornelia Huck if (error) 540a306eea4SCornelia Huck goto attrError; 541a7fd6706SKay Sievers 54223681e47SGreg Kroah-Hartman if (MAJOR(dev->devt)) { 54323681e47SGreg Kroah-Hartman struct device_attribute *attr; 54423681e47SGreg Kroah-Hartman attr = kzalloc(sizeof(*attr), GFP_KERNEL); 54523681e47SGreg Kroah-Hartman if (!attr) { 54623681e47SGreg Kroah-Hartman error = -ENOMEM; 547a306eea4SCornelia Huck goto ueventattrError; 54823681e47SGreg Kroah-Hartman } 54923681e47SGreg Kroah-Hartman attr->attr.name = "dev"; 55023681e47SGreg Kroah-Hartman attr->attr.mode = S_IRUGO; 55123681e47SGreg Kroah-Hartman if (dev->driver) 55223681e47SGreg Kroah-Hartman attr->attr.owner = dev->driver->owner; 55323681e47SGreg Kroah-Hartman attr->show = show_dev; 55423681e47SGreg Kroah-Hartman error = device_create_file(dev, attr); 55523681e47SGreg Kroah-Hartman if (error) { 55623681e47SGreg Kroah-Hartman kfree(attr); 557a306eea4SCornelia Huck goto ueventattrError; 55823681e47SGreg Kroah-Hartman } 55923681e47SGreg Kroah-Hartman 56023681e47SGreg Kroah-Hartman dev->devt_attr = attr; 56123681e47SGreg Kroah-Hartman } 56223681e47SGreg Kroah-Hartman 563b9d9c82bSKay Sievers if (dev->class) { 564b9d9c82bSKay Sievers sysfs_create_link(&dev->kobj, &dev->class->subsys.kset.kobj, 565b9d9c82bSKay Sievers "subsystem"); 56640fa5422SGreg Kroah-Hartman /* If this is not a "fake" compatible device, then create the 56740fa5422SGreg Kroah-Hartman * symlink from the class to the device. */ 56840fa5422SGreg Kroah-Hartman if (dev->kobj.parent != &dev->class->subsys.kset.kobj) 56940fa5422SGreg Kroah-Hartman sysfs_create_link(&dev->class->subsys.kset.kobj, 57040fa5422SGreg Kroah-Hartman &dev->kobj, dev->bus_id); 57199ef3ef8SKay Sievers #ifdef CONFIG_SYSFS_DEPRECATED 57264bb5d2cSGreg Kroah-Hartman if (parent) { 573cb360bbfSCornelia Huck sysfs_create_link(&dev->kobj, &dev->parent->kobj, 574cb360bbfSCornelia Huck "device"); 575cb360bbfSCornelia Huck class_name = make_class_name(dev->class->name, 576cb360bbfSCornelia Huck &dev->kobj); 577cb360bbfSCornelia Huck if (class_name) 578cb360bbfSCornelia Huck sysfs_create_link(&dev->parent->kobj, 579cb360bbfSCornelia Huck &dev->kobj, class_name); 580b9d9c82bSKay Sievers } 58199ef3ef8SKay Sievers #endif 58264bb5d2cSGreg Kroah-Hartman } 58323681e47SGreg Kroah-Hartman 5842620efefSGreg Kroah-Hartman if ((error = device_add_attrs(dev))) 5852620efefSGreg Kroah-Hartman goto AttrsError; 586de0ff00dSGreg Kroah-Hartman if ((error = device_add_groups(dev))) 587de0ff00dSGreg Kroah-Hartman goto GroupError; 5881da177e4SLinus Torvalds if ((error = device_pm_add(dev))) 5891da177e4SLinus Torvalds goto PMError; 5901da177e4SLinus Torvalds if ((error = bus_add_device(dev))) 5911da177e4SLinus Torvalds goto BusError; 592b7a3e813SKay Sievers if (!dev->uevent_suppress) 59353877d06SKay Sievers kobject_uevent(&dev->kobj, KOBJ_ADD); 594f70fa629SAlan Stern if ((error = bus_attach_device(dev))) 595f70fa629SAlan Stern goto AttachError; 5961da177e4SLinus Torvalds if (parent) 597d856f1e3SJames Bottomley klist_add_tail(&dev->knode_parent, &parent->klist_children); 5981da177e4SLinus Torvalds 5995d9fd169SGreg Kroah-Hartman if (dev->class) { 6005d9fd169SGreg Kroah-Hartman down(&dev->class->sem); 601c47ed219SGreg Kroah-Hartman /* tie the class to the device */ 6025d9fd169SGreg Kroah-Hartman list_add_tail(&dev->node, &dev->class->devices); 603c47ed219SGreg Kroah-Hartman 604c47ed219SGreg Kroah-Hartman /* notify any interfaces that the device is here */ 605c47ed219SGreg Kroah-Hartman list_for_each_entry(class_intf, &dev->class->interfaces, node) 606c47ed219SGreg Kroah-Hartman if (class_intf->add_dev) 607c47ed219SGreg Kroah-Hartman class_intf->add_dev(dev, class_intf); 6085d9fd169SGreg Kroah-Hartman up(&dev->class->sem); 6095d9fd169SGreg Kroah-Hartman } 6101da177e4SLinus Torvalds Done: 611e9a7d305SGreg Kroah-Hartman kfree(class_name); 6121da177e4SLinus Torvalds put_device(dev); 6131da177e4SLinus Torvalds return error; 614f70fa629SAlan Stern AttachError: 615f70fa629SAlan Stern bus_remove_device(dev); 6161da177e4SLinus Torvalds BusError: 6171da177e4SLinus Torvalds device_pm_remove(dev); 6181da177e4SLinus Torvalds PMError: 619116af378SBenjamin Herrenschmidt if (dev->bus) 620116af378SBenjamin Herrenschmidt blocking_notifier_call_chain(&dev->bus->bus_notifier, 621116af378SBenjamin Herrenschmidt BUS_NOTIFY_DEL_DEVICE, dev); 622de0ff00dSGreg Kroah-Hartman device_remove_groups(dev); 623de0ff00dSGreg Kroah-Hartman GroupError: 6242620efefSGreg Kroah-Hartman device_remove_attrs(dev); 6252620efefSGreg Kroah-Hartman AttrsError: 62623681e47SGreg Kroah-Hartman if (dev->devt_attr) { 62723681e47SGreg Kroah-Hartman device_remove_file(dev, dev->devt_attr); 62823681e47SGreg Kroah-Hartman kfree(dev->devt_attr); 62923681e47SGreg Kroah-Hartman } 630a306eea4SCornelia Huck ueventattrError: 631a306eea4SCornelia Huck device_remove_file(dev, &dev->uevent_attr); 63223681e47SGreg Kroah-Hartman attrError: 633312c004dSKay Sievers kobject_uevent(&dev->kobj, KOBJ_REMOVE); 6341da177e4SLinus Torvalds kobject_del(&dev->kobj); 6351da177e4SLinus Torvalds Error: 6361da177e4SLinus Torvalds if (parent) 6371da177e4SLinus Torvalds put_device(parent); 6381da177e4SLinus Torvalds goto Done; 6391da177e4SLinus Torvalds } 6401da177e4SLinus Torvalds 6411da177e4SLinus Torvalds 6421da177e4SLinus Torvalds /** 6431da177e4SLinus Torvalds * device_register - register a device with the system. 6441da177e4SLinus Torvalds * @dev: pointer to the device structure 6451da177e4SLinus Torvalds * 6461da177e4SLinus Torvalds * This happens in two clean steps - initialize the device 6471da177e4SLinus Torvalds * and add it to the system. The two steps can be called 6481da177e4SLinus Torvalds * separately, but this is the easiest and most common. 6491da177e4SLinus Torvalds * I.e. you should only call the two helpers separately if 6501da177e4SLinus Torvalds * have a clearly defined need to use and refcount the device 6511da177e4SLinus Torvalds * before it is added to the hierarchy. 6521da177e4SLinus Torvalds */ 6531da177e4SLinus Torvalds 6541da177e4SLinus Torvalds int device_register(struct device *dev) 6551da177e4SLinus Torvalds { 6561da177e4SLinus Torvalds device_initialize(dev); 6571da177e4SLinus Torvalds return device_add(dev); 6581da177e4SLinus Torvalds } 6591da177e4SLinus Torvalds 6601da177e4SLinus Torvalds 6611da177e4SLinus Torvalds /** 6621da177e4SLinus Torvalds * get_device - increment reference count for device. 6631da177e4SLinus Torvalds * @dev: device. 6641da177e4SLinus Torvalds * 6651da177e4SLinus Torvalds * This simply forwards the call to kobject_get(), though 6661da177e4SLinus Torvalds * we do take care to provide for the case that we get a NULL 6671da177e4SLinus Torvalds * pointer passed in. 6681da177e4SLinus Torvalds */ 6691da177e4SLinus Torvalds 6701da177e4SLinus Torvalds struct device * get_device(struct device * dev) 6711da177e4SLinus Torvalds { 6721da177e4SLinus Torvalds return dev ? to_dev(kobject_get(&dev->kobj)) : NULL; 6731da177e4SLinus Torvalds } 6741da177e4SLinus Torvalds 6751da177e4SLinus Torvalds 6761da177e4SLinus Torvalds /** 6771da177e4SLinus Torvalds * put_device - decrement reference count. 6781da177e4SLinus Torvalds * @dev: device in question. 6791da177e4SLinus Torvalds */ 6801da177e4SLinus Torvalds void put_device(struct device * dev) 6811da177e4SLinus Torvalds { 6821da177e4SLinus Torvalds if (dev) 6831da177e4SLinus Torvalds kobject_put(&dev->kobj); 6841da177e4SLinus Torvalds } 6851da177e4SLinus Torvalds 6861da177e4SLinus Torvalds 6871da177e4SLinus Torvalds /** 6881da177e4SLinus Torvalds * device_del - delete device from system. 6891da177e4SLinus Torvalds * @dev: device. 6901da177e4SLinus Torvalds * 6911da177e4SLinus Torvalds * This is the first part of the device unregistration 6921da177e4SLinus Torvalds * sequence. This removes the device from the lists we control 6931da177e4SLinus Torvalds * from here, has it removed from the other driver model 6941da177e4SLinus Torvalds * subsystems it was added to in device_add(), and removes it 6951da177e4SLinus Torvalds * from the kobject hierarchy. 6961da177e4SLinus Torvalds * 6971da177e4SLinus Torvalds * NOTE: this should be called manually _iff_ device_add() was 6981da177e4SLinus Torvalds * also called manually. 6991da177e4SLinus Torvalds */ 7001da177e4SLinus Torvalds 7011da177e4SLinus Torvalds void device_del(struct device * dev) 7021da177e4SLinus Torvalds { 7031da177e4SLinus Torvalds struct device * parent = dev->parent; 704c47ed219SGreg Kroah-Hartman struct class_interface *class_intf; 7051da177e4SLinus Torvalds 7061da177e4SLinus Torvalds if (parent) 707d62c0f9fSPatrick Mochel klist_del(&dev->knode_parent); 70882189b98SCatalin Marinas if (dev->devt_attr) { 70923681e47SGreg Kroah-Hartman device_remove_file(dev, dev->devt_attr); 71082189b98SCatalin Marinas kfree(dev->devt_attr); 71182189b98SCatalin Marinas } 712b9d9c82bSKay Sievers if (dev->class) { 713b9d9c82bSKay Sievers sysfs_remove_link(&dev->kobj, "subsystem"); 71440fa5422SGreg Kroah-Hartman /* If this is not a "fake" compatible device, remove the 71540fa5422SGreg Kroah-Hartman * symlink from the class to the device. */ 71640fa5422SGreg Kroah-Hartman if (dev->kobj.parent != &dev->class->subsys.kset.kobj) 71740fa5422SGreg Kroah-Hartman sysfs_remove_link(&dev->class->subsys.kset.kobj, 71840fa5422SGreg Kroah-Hartman dev->bus_id); 71999ef3ef8SKay Sievers #ifdef CONFIG_SYSFS_DEPRECATED 72064bb5d2cSGreg Kroah-Hartman if (parent) { 72199ef3ef8SKay Sievers char *class_name = make_class_name(dev->class->name, 72299ef3ef8SKay Sievers &dev->kobj); 723cb360bbfSCornelia Huck if (class_name) 724cb360bbfSCornelia Huck sysfs_remove_link(&dev->parent->kobj, 725cb360bbfSCornelia Huck class_name); 726e9a7d305SGreg Kroah-Hartman kfree(class_name); 72799ef3ef8SKay Sievers sysfs_remove_link(&dev->kobj, "device"); 72899ef3ef8SKay Sievers } 72999ef3ef8SKay Sievers #endif 73099ef3ef8SKay Sievers 7315d9fd169SGreg Kroah-Hartman down(&dev->class->sem); 732c47ed219SGreg Kroah-Hartman /* notify any interfaces that the device is now gone */ 733c47ed219SGreg Kroah-Hartman list_for_each_entry(class_intf, &dev->class->interfaces, node) 734c47ed219SGreg Kroah-Hartman if (class_intf->remove_dev) 735c47ed219SGreg Kroah-Hartman class_intf->remove_dev(dev, class_intf); 736c47ed219SGreg Kroah-Hartman /* remove the device from the class list */ 7375d9fd169SGreg Kroah-Hartman list_del_init(&dev->node); 7385d9fd169SGreg Kroah-Hartman up(&dev->class->sem); 739b9d9c82bSKay Sievers } 740a7fd6706SKay Sievers device_remove_file(dev, &dev->uevent_attr); 741de0ff00dSGreg Kroah-Hartman device_remove_groups(dev); 7422620efefSGreg Kroah-Hartman device_remove_attrs(dev); 74328953533SBenjamin Herrenschmidt bus_remove_device(dev); 7441da177e4SLinus Torvalds 7451da177e4SLinus Torvalds /* Notify the platform of the removal, in case they 7461da177e4SLinus Torvalds * need to do anything... 7471da177e4SLinus Torvalds */ 7481da177e4SLinus Torvalds if (platform_notify_remove) 7491da177e4SLinus Torvalds platform_notify_remove(dev); 750116af378SBenjamin Herrenschmidt if (dev->bus) 751116af378SBenjamin Herrenschmidt blocking_notifier_call_chain(&dev->bus->bus_notifier, 752116af378SBenjamin Herrenschmidt BUS_NOTIFY_DEL_DEVICE, dev); 7531da177e4SLinus Torvalds device_pm_remove(dev); 754312c004dSKay Sievers kobject_uevent(&dev->kobj, KOBJ_REMOVE); 7551da177e4SLinus Torvalds kobject_del(&dev->kobj); 7561da177e4SLinus Torvalds if (parent) 7571da177e4SLinus Torvalds put_device(parent); 7581da177e4SLinus Torvalds } 7591da177e4SLinus Torvalds 7601da177e4SLinus Torvalds /** 7611da177e4SLinus Torvalds * device_unregister - unregister device from system. 7621da177e4SLinus Torvalds * @dev: device going away. 7631da177e4SLinus Torvalds * 7641da177e4SLinus Torvalds * We do this in two parts, like we do device_register(). First, 7651da177e4SLinus Torvalds * we remove it from all the subsystems with device_del(), then 7661da177e4SLinus Torvalds * we decrement the reference count via put_device(). If that 7671da177e4SLinus Torvalds * is the final reference count, the device will be cleaned up 7681da177e4SLinus Torvalds * via device_release() above. Otherwise, the structure will 7691da177e4SLinus Torvalds * stick around until the final reference to the device is dropped. 7701da177e4SLinus Torvalds */ 7711da177e4SLinus Torvalds void device_unregister(struct device * dev) 7721da177e4SLinus Torvalds { 7731da177e4SLinus Torvalds pr_debug("DEV: Unregistering device. ID = '%s'\n", dev->bus_id); 7741da177e4SLinus Torvalds device_del(dev); 7751da177e4SLinus Torvalds put_device(dev); 7761da177e4SLinus Torvalds } 7771da177e4SLinus Torvalds 7781da177e4SLinus Torvalds 77936239577Smochel@digitalimplant.org static struct device * next_device(struct klist_iter * i) 78036239577Smochel@digitalimplant.org { 78136239577Smochel@digitalimplant.org struct klist_node * n = klist_next(i); 78236239577Smochel@digitalimplant.org return n ? container_of(n, struct device, knode_parent) : NULL; 78336239577Smochel@digitalimplant.org } 78436239577Smochel@digitalimplant.org 7851da177e4SLinus Torvalds /** 7861da177e4SLinus Torvalds * device_for_each_child - device child iterator. 787c41455fbSRandy Dunlap * @parent: parent struct device. 7881da177e4SLinus Torvalds * @data: data for the callback. 7891da177e4SLinus Torvalds * @fn: function to be called for each device. 7901da177e4SLinus Torvalds * 791c41455fbSRandy Dunlap * Iterate over @parent's child devices, and call @fn for each, 7921da177e4SLinus Torvalds * passing it @data. 7931da177e4SLinus Torvalds * 7941da177e4SLinus Torvalds * We check the return of @fn each time. If it returns anything 7951da177e4SLinus Torvalds * other than 0, we break out and return that value. 7961da177e4SLinus Torvalds */ 79736239577Smochel@digitalimplant.org int device_for_each_child(struct device * parent, void * data, 7981da177e4SLinus Torvalds int (*fn)(struct device *, void *)) 7991da177e4SLinus Torvalds { 80036239577Smochel@digitalimplant.org struct klist_iter i; 8011da177e4SLinus Torvalds struct device * child; 8021da177e4SLinus Torvalds int error = 0; 8031da177e4SLinus Torvalds 80436239577Smochel@digitalimplant.org klist_iter_init(&parent->klist_children, &i); 80536239577Smochel@digitalimplant.org while ((child = next_device(&i)) && !error) 80636239577Smochel@digitalimplant.org error = fn(child, data); 80736239577Smochel@digitalimplant.org klist_iter_exit(&i); 8081da177e4SLinus Torvalds return error; 8091da177e4SLinus Torvalds } 8101da177e4SLinus Torvalds 8115ab69981SCornelia Huck /** 8125ab69981SCornelia Huck * device_find_child - device iterator for locating a particular device. 8135ab69981SCornelia Huck * @parent: parent struct device 8145ab69981SCornelia Huck * @data: Data to pass to match function 8155ab69981SCornelia Huck * @match: Callback function to check device 8165ab69981SCornelia Huck * 8175ab69981SCornelia Huck * This is similar to the device_for_each_child() function above, but it 8185ab69981SCornelia Huck * returns a reference to a device that is 'found' for later use, as 8195ab69981SCornelia Huck * determined by the @match callback. 8205ab69981SCornelia Huck * 8215ab69981SCornelia Huck * The callback should return 0 if the device doesn't match and non-zero 8225ab69981SCornelia Huck * if it does. If the callback returns non-zero and a reference to the 8235ab69981SCornelia Huck * current device can be obtained, this function will return to the caller 8245ab69981SCornelia Huck * and not iterate over any more devices. 8255ab69981SCornelia Huck */ 8265ab69981SCornelia Huck struct device * device_find_child(struct device *parent, void *data, 8275ab69981SCornelia Huck int (*match)(struct device *, void *)) 8285ab69981SCornelia Huck { 8295ab69981SCornelia Huck struct klist_iter i; 8305ab69981SCornelia Huck struct device *child; 8315ab69981SCornelia Huck 8325ab69981SCornelia Huck if (!parent) 8335ab69981SCornelia Huck return NULL; 8345ab69981SCornelia Huck 8355ab69981SCornelia Huck klist_iter_init(&parent->klist_children, &i); 8365ab69981SCornelia Huck while ((child = next_device(&i))) 8375ab69981SCornelia Huck if (match(child, data) && get_device(child)) 8385ab69981SCornelia Huck break; 8395ab69981SCornelia Huck klist_iter_exit(&i); 8405ab69981SCornelia Huck return child; 8415ab69981SCornelia Huck } 8425ab69981SCornelia Huck 8431da177e4SLinus Torvalds int __init devices_init(void) 8441da177e4SLinus Torvalds { 8451da177e4SLinus Torvalds return subsystem_register(&devices_subsys); 8461da177e4SLinus Torvalds } 8471da177e4SLinus Torvalds 8481da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(device_for_each_child); 8495ab69981SCornelia Huck EXPORT_SYMBOL_GPL(device_find_child); 8501da177e4SLinus Torvalds 8511da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(device_initialize); 8521da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(device_add); 8531da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(device_register); 8541da177e4SLinus Torvalds 8551da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(device_del); 8561da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(device_unregister); 8571da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(get_device); 8581da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(put_device); 8591da177e4SLinus Torvalds 8601da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(device_create_file); 8611da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(device_remove_file); 86223681e47SGreg Kroah-Hartman 86323681e47SGreg Kroah-Hartman 86423681e47SGreg Kroah-Hartman static void device_create_release(struct device *dev) 86523681e47SGreg Kroah-Hartman { 86623681e47SGreg Kroah-Hartman pr_debug("%s called for %s\n", __FUNCTION__, dev->bus_id); 86723681e47SGreg Kroah-Hartman kfree(dev); 86823681e47SGreg Kroah-Hartman } 86923681e47SGreg Kroah-Hartman 87023681e47SGreg Kroah-Hartman /** 87123681e47SGreg Kroah-Hartman * device_create - creates a device and registers it with sysfs 87242734dafSHenrik Kretzschmar * @class: pointer to the struct class that this device should be registered to 87342734dafSHenrik Kretzschmar * @parent: pointer to the parent struct device of this new device, if any 87442734dafSHenrik Kretzschmar * @devt: the dev_t for the char device to be added 87542734dafSHenrik Kretzschmar * @fmt: string for the device's name 87623681e47SGreg Kroah-Hartman * 87742734dafSHenrik Kretzschmar * This function can be used by char device classes. A struct device 87842734dafSHenrik Kretzschmar * will be created in sysfs, registered to the specified class. 87942734dafSHenrik Kretzschmar * 88023681e47SGreg Kroah-Hartman * A "dev" file will be created, showing the dev_t for the device, if 88123681e47SGreg Kroah-Hartman * the dev_t is not 0,0. 88242734dafSHenrik Kretzschmar * If a pointer to a parent struct device is passed in, the newly created 88342734dafSHenrik Kretzschmar * struct device will be a child of that device in sysfs. 88442734dafSHenrik Kretzschmar * The pointer to the struct device will be returned from the call. 88542734dafSHenrik Kretzschmar * Any further sysfs files that might be required can be created using this 88623681e47SGreg Kroah-Hartman * pointer. 88723681e47SGreg Kroah-Hartman * 88823681e47SGreg Kroah-Hartman * Note: the struct class passed to this function must have previously 88923681e47SGreg Kroah-Hartman * been created with a call to class_create(). 89023681e47SGreg Kroah-Hartman */ 89123681e47SGreg Kroah-Hartman struct device *device_create(struct class *class, struct device *parent, 8925cbe5f8aSGreg Kroah-Hartman dev_t devt, const char *fmt, ...) 89323681e47SGreg Kroah-Hartman { 89423681e47SGreg Kroah-Hartman va_list args; 89523681e47SGreg Kroah-Hartman struct device *dev = NULL; 89623681e47SGreg Kroah-Hartman int retval = -ENODEV; 89723681e47SGreg Kroah-Hartman 89823681e47SGreg Kroah-Hartman if (class == NULL || IS_ERR(class)) 89923681e47SGreg Kroah-Hartman goto error; 90023681e47SGreg Kroah-Hartman 90123681e47SGreg Kroah-Hartman dev = kzalloc(sizeof(*dev), GFP_KERNEL); 90223681e47SGreg Kroah-Hartman if (!dev) { 90323681e47SGreg Kroah-Hartman retval = -ENOMEM; 90423681e47SGreg Kroah-Hartman goto error; 90523681e47SGreg Kroah-Hartman } 90623681e47SGreg Kroah-Hartman 90723681e47SGreg Kroah-Hartman dev->devt = devt; 90823681e47SGreg Kroah-Hartman dev->class = class; 90923681e47SGreg Kroah-Hartman dev->parent = parent; 91023681e47SGreg Kroah-Hartman dev->release = device_create_release; 91123681e47SGreg Kroah-Hartman 91223681e47SGreg Kroah-Hartman va_start(args, fmt); 91323681e47SGreg Kroah-Hartman vsnprintf(dev->bus_id, BUS_ID_SIZE, fmt, args); 91423681e47SGreg Kroah-Hartman va_end(args); 91523681e47SGreg Kroah-Hartman retval = device_register(dev); 91623681e47SGreg Kroah-Hartman if (retval) 91723681e47SGreg Kroah-Hartman goto error; 91823681e47SGreg Kroah-Hartman 91923681e47SGreg Kroah-Hartman return dev; 92023681e47SGreg Kroah-Hartman 92123681e47SGreg Kroah-Hartman error: 92223681e47SGreg Kroah-Hartman kfree(dev); 92323681e47SGreg Kroah-Hartman return ERR_PTR(retval); 92423681e47SGreg Kroah-Hartman } 92523681e47SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(device_create); 92623681e47SGreg Kroah-Hartman 92723681e47SGreg Kroah-Hartman /** 92823681e47SGreg Kroah-Hartman * device_destroy - removes a device that was created with device_create() 92942734dafSHenrik Kretzschmar * @class: pointer to the struct class that this device was registered with 93042734dafSHenrik Kretzschmar * @devt: the dev_t of the device that was previously registered 93123681e47SGreg Kroah-Hartman * 93242734dafSHenrik Kretzschmar * This call unregisters and cleans up a device that was created with a 93342734dafSHenrik Kretzschmar * call to device_create(). 93423681e47SGreg Kroah-Hartman */ 93523681e47SGreg Kroah-Hartman void device_destroy(struct class *class, dev_t devt) 93623681e47SGreg Kroah-Hartman { 93723681e47SGreg Kroah-Hartman struct device *dev = NULL; 93823681e47SGreg Kroah-Hartman struct device *dev_tmp; 93923681e47SGreg Kroah-Hartman 94023681e47SGreg Kroah-Hartman down(&class->sem); 94123681e47SGreg Kroah-Hartman list_for_each_entry(dev_tmp, &class->devices, node) { 94223681e47SGreg Kroah-Hartman if (dev_tmp->devt == devt) { 94323681e47SGreg Kroah-Hartman dev = dev_tmp; 94423681e47SGreg Kroah-Hartman break; 94523681e47SGreg Kroah-Hartman } 94623681e47SGreg Kroah-Hartman } 94723681e47SGreg Kroah-Hartman up(&class->sem); 94823681e47SGreg Kroah-Hartman 9495d9fd169SGreg Kroah-Hartman if (dev) 95023681e47SGreg Kroah-Hartman device_unregister(dev); 95123681e47SGreg Kroah-Hartman } 95223681e47SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(device_destroy); 953a2de48caSGreg Kroah-Hartman 954a2de48caSGreg Kroah-Hartman /** 955a2de48caSGreg Kroah-Hartman * device_rename - renames a device 956a2de48caSGreg Kroah-Hartman * @dev: the pointer to the struct device to be renamed 957a2de48caSGreg Kroah-Hartman * @new_name: the new name of the device 958a2de48caSGreg Kroah-Hartman */ 959a2de48caSGreg Kroah-Hartman int device_rename(struct device *dev, char *new_name) 960a2de48caSGreg Kroah-Hartman { 961a2de48caSGreg Kroah-Hartman char *old_class_name = NULL; 962a2de48caSGreg Kroah-Hartman char *new_class_name = NULL; 963a2de48caSGreg Kroah-Hartman char *old_symlink_name = NULL; 964a2de48caSGreg Kroah-Hartman int error; 965a2de48caSGreg Kroah-Hartman 966a2de48caSGreg Kroah-Hartman dev = get_device(dev); 967a2de48caSGreg Kroah-Hartman if (!dev) 968a2de48caSGreg Kroah-Hartman return -EINVAL; 969a2de48caSGreg Kroah-Hartman 970a2de48caSGreg Kroah-Hartman pr_debug("DEVICE: renaming '%s' to '%s'\n", dev->bus_id, new_name); 971a2de48caSGreg Kroah-Hartman 97299ef3ef8SKay Sievers #ifdef CONFIG_SYSFS_DEPRECATED 973a2de48caSGreg Kroah-Hartman if ((dev->class) && (dev->parent)) 974a2de48caSGreg Kroah-Hartman old_class_name = make_class_name(dev->class->name, &dev->kobj); 97599ef3ef8SKay Sievers #endif 976a2de48caSGreg Kroah-Hartman 977a2de48caSGreg Kroah-Hartman if (dev->class) { 978a2de48caSGreg Kroah-Hartman old_symlink_name = kmalloc(BUS_ID_SIZE, GFP_KERNEL); 979952ab431SJesper Juhl if (!old_symlink_name) { 980952ab431SJesper Juhl error = -ENOMEM; 981952ab431SJesper Juhl goto out_free_old_class; 982952ab431SJesper Juhl } 983a2de48caSGreg Kroah-Hartman strlcpy(old_symlink_name, dev->bus_id, BUS_ID_SIZE); 984a2de48caSGreg Kroah-Hartman } 985a2de48caSGreg Kroah-Hartman 986a2de48caSGreg Kroah-Hartman strlcpy(dev->bus_id, new_name, BUS_ID_SIZE); 987a2de48caSGreg Kroah-Hartman 988a2de48caSGreg Kroah-Hartman error = kobject_rename(&dev->kobj, new_name); 989a2de48caSGreg Kroah-Hartman 99099ef3ef8SKay Sievers #ifdef CONFIG_SYSFS_DEPRECATED 991a2de48caSGreg Kroah-Hartman if (old_class_name) { 992a2de48caSGreg Kroah-Hartman new_class_name = make_class_name(dev->class->name, &dev->kobj); 993a2de48caSGreg Kroah-Hartman if (new_class_name) { 994a2de48caSGreg Kroah-Hartman sysfs_create_link(&dev->parent->kobj, &dev->kobj, 995a2de48caSGreg Kroah-Hartman new_class_name); 996a2de48caSGreg Kroah-Hartman sysfs_remove_link(&dev->parent->kobj, old_class_name); 997a2de48caSGreg Kroah-Hartman } 998a2de48caSGreg Kroah-Hartman } 99999ef3ef8SKay Sievers #endif 100099ef3ef8SKay Sievers 1001a2de48caSGreg Kroah-Hartman if (dev->class) { 1002a2de48caSGreg Kroah-Hartman sysfs_remove_link(&dev->class->subsys.kset.kobj, 1003a2de48caSGreg Kroah-Hartman old_symlink_name); 1004a2de48caSGreg Kroah-Hartman sysfs_create_link(&dev->class->subsys.kset.kobj, &dev->kobj, 1005a2de48caSGreg Kroah-Hartman dev->bus_id); 1006a2de48caSGreg Kroah-Hartman } 1007a2de48caSGreg Kroah-Hartman put_device(dev); 1008a2de48caSGreg Kroah-Hartman 1009a2de48caSGreg Kroah-Hartman kfree(new_class_name); 1010a2de48caSGreg Kroah-Hartman kfree(old_symlink_name); 1011952ab431SJesper Juhl out_free_old_class: 1012952ab431SJesper Juhl kfree(old_class_name); 1013a2de48caSGreg Kroah-Hartman 1014a2de48caSGreg Kroah-Hartman return error; 1015a2de48caSGreg Kroah-Hartman } 10168a82472fSCornelia Huck 10178a82472fSCornelia Huck 10188a82472fSCornelia Huck static int device_move_class_links(struct device *dev, 10198a82472fSCornelia Huck struct device *old_parent, 10208a82472fSCornelia Huck struct device *new_parent) 10218a82472fSCornelia Huck { 10228a82472fSCornelia Huck #ifdef CONFIG_SYSFS_DEPRECATED 10238a82472fSCornelia Huck int error; 10248a82472fSCornelia Huck char *class_name; 10258a82472fSCornelia Huck 10268a82472fSCornelia Huck class_name = make_class_name(dev->class->name, &dev->kobj); 10278a82472fSCornelia Huck if (!class_name) { 1028cb360bbfSCornelia Huck error = -ENOMEM; 10298a82472fSCornelia Huck goto out; 10308a82472fSCornelia Huck } 10318a82472fSCornelia Huck if (old_parent) { 10328a82472fSCornelia Huck sysfs_remove_link(&dev->kobj, "device"); 10338a82472fSCornelia Huck sysfs_remove_link(&old_parent->kobj, class_name); 10348a82472fSCornelia Huck } 1035c744aeaeSCornelia Huck if (new_parent) { 1036c744aeaeSCornelia Huck error = sysfs_create_link(&dev->kobj, &new_parent->kobj, 1037c744aeaeSCornelia Huck "device"); 10388a82472fSCornelia Huck if (error) 10398a82472fSCornelia Huck goto out; 1040c744aeaeSCornelia Huck error = sysfs_create_link(&new_parent->kobj, &dev->kobj, 1041c744aeaeSCornelia Huck class_name); 10428a82472fSCornelia Huck if (error) 10438a82472fSCornelia Huck sysfs_remove_link(&dev->kobj, "device"); 1044c744aeaeSCornelia Huck } 1045c744aeaeSCornelia Huck else 1046c744aeaeSCornelia Huck error = 0; 10478a82472fSCornelia Huck out: 10488a82472fSCornelia Huck kfree(class_name); 10498a82472fSCornelia Huck return error; 10508a82472fSCornelia Huck #else 10518a82472fSCornelia Huck return 0; 10528a82472fSCornelia Huck #endif 10538a82472fSCornelia Huck } 10548a82472fSCornelia Huck 10558a82472fSCornelia Huck /** 10568a82472fSCornelia Huck * device_move - moves a device to a new parent 10578a82472fSCornelia Huck * @dev: the pointer to the struct device to be moved 1058c744aeaeSCornelia Huck * @new_parent: the new parent of the device (can by NULL) 10598a82472fSCornelia Huck */ 10608a82472fSCornelia Huck int device_move(struct device *dev, struct device *new_parent) 10618a82472fSCornelia Huck { 10628a82472fSCornelia Huck int error; 10638a82472fSCornelia Huck struct device *old_parent; 1064c744aeaeSCornelia Huck struct kobject *new_parent_kobj; 10658a82472fSCornelia Huck 10668a82472fSCornelia Huck dev = get_device(dev); 10678a82472fSCornelia Huck if (!dev) 10688a82472fSCornelia Huck return -EINVAL; 10698a82472fSCornelia Huck 10708a82472fSCornelia Huck new_parent = get_device(new_parent); 1071c744aeaeSCornelia Huck new_parent_kobj = get_device_parent (dev, new_parent); 1072c744aeaeSCornelia Huck if (IS_ERR(new_parent_kobj)) { 1073c744aeaeSCornelia Huck error = PTR_ERR(new_parent_kobj); 1074c744aeaeSCornelia Huck put_device(new_parent); 10758a82472fSCornelia Huck goto out; 10768a82472fSCornelia Huck } 10778a82472fSCornelia Huck pr_debug("DEVICE: moving '%s' to '%s'\n", dev->bus_id, 1078c744aeaeSCornelia Huck new_parent ? new_parent->bus_id : "<NULL>"); 1079c744aeaeSCornelia Huck error = kobject_move(&dev->kobj, new_parent_kobj); 10808a82472fSCornelia Huck if (error) { 10818a82472fSCornelia Huck put_device(new_parent); 10828a82472fSCornelia Huck goto out; 10838a82472fSCornelia Huck } 10848a82472fSCornelia Huck old_parent = dev->parent; 10858a82472fSCornelia Huck dev->parent = new_parent; 10868a82472fSCornelia Huck if (old_parent) 1087acf02d23SCornelia Huck klist_remove(&dev->knode_parent); 1088c744aeaeSCornelia Huck if (new_parent) 10898a82472fSCornelia Huck klist_add_tail(&dev->knode_parent, &new_parent->klist_children); 10908a82472fSCornelia Huck if (!dev->class) 10918a82472fSCornelia Huck goto out_put; 10928a82472fSCornelia Huck error = device_move_class_links(dev, old_parent, new_parent); 10938a82472fSCornelia Huck if (error) { 10948a82472fSCornelia Huck /* We ignore errors on cleanup since we're hosed anyway... */ 10958a82472fSCornelia Huck device_move_class_links(dev, new_parent, old_parent); 10968a82472fSCornelia Huck if (!kobject_move(&dev->kobj, &old_parent->kobj)) { 1097c744aeaeSCornelia Huck if (new_parent) 1098acf02d23SCornelia Huck klist_remove(&dev->knode_parent); 10998a82472fSCornelia Huck if (old_parent) 11008a82472fSCornelia Huck klist_add_tail(&dev->knode_parent, 11018a82472fSCornelia Huck &old_parent->klist_children); 11028a82472fSCornelia Huck } 11038a82472fSCornelia Huck put_device(new_parent); 11048a82472fSCornelia Huck goto out; 11058a82472fSCornelia Huck } 11068a82472fSCornelia Huck out_put: 11078a82472fSCornelia Huck put_device(old_parent); 11088a82472fSCornelia Huck out: 11098a82472fSCornelia Huck put_device(dev); 11108a82472fSCornelia Huck return error; 11118a82472fSCornelia Huck } 11128a82472fSCornelia Huck 11138a82472fSCornelia Huck EXPORT_SYMBOL_GPL(device_move); 1114