xref: /openbmc/linux/drivers/base/core.c (revision 05e4e5b8)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * drivers/base/core.c - core driver model code (device registration, etc)
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  * Copyright (c) 2002-3 Patrick Mochel
51da177e4SLinus Torvalds  * Copyright (c) 2002-3 Open Source Development Labs
664bb5d2cSGreg Kroah-Hartman  * Copyright (c) 2006 Greg Kroah-Hartman <gregkh@suse.de>
764bb5d2cSGreg Kroah-Hartman  * Copyright (c) 2006 Novell, Inc.
81da177e4SLinus Torvalds  *
91da177e4SLinus Torvalds  * This file is released under the GPLv2
101da177e4SLinus Torvalds  *
111da177e4SLinus Torvalds  */
121da177e4SLinus Torvalds 
131da177e4SLinus Torvalds #include <linux/device.h>
141da177e4SLinus Torvalds #include <linux/err.h>
151da177e4SLinus Torvalds #include <linux/init.h>
161da177e4SLinus Torvalds #include <linux/module.h>
171da177e4SLinus Torvalds #include <linux/slab.h>
181da177e4SLinus Torvalds #include <linux/string.h>
1923681e47SGreg Kroah-Hartman #include <linux/kdev_t.h>
20116af378SBenjamin Herrenschmidt #include <linux/notifier.h>
2107d57a32SGrant Likely #include <linux/of.h>
2207d57a32SGrant Likely #include <linux/of_device.h>
23da231fd5SKay Sievers #include <linux/genhd.h>
24815d2d50SAndrew Morton #include <linux/kallsyms.h>
25f75b1c60SDave Young #include <linux/mutex.h>
26401097eaSShaohua Li #include <linux/async.h>
27af8db150SPeter Chen #include <linux/pm_runtime.h>
28c4e00daaSKay Sievers #include <linux/netdevice.h>
291da177e4SLinus Torvalds 
301da177e4SLinus Torvalds #include "base.h"
311da177e4SLinus Torvalds #include "power/power.h"
321da177e4SLinus Torvalds 
33e52eec13SAndi Kleen #ifdef CONFIG_SYSFS_DEPRECATED
34e52eec13SAndi Kleen #ifdef CONFIG_SYSFS_DEPRECATED_V2
35e52eec13SAndi Kleen long sysfs_deprecated = 1;
36e52eec13SAndi Kleen #else
37e52eec13SAndi Kleen long sysfs_deprecated = 0;
38e52eec13SAndi Kleen #endif
39e52eec13SAndi Kleen static __init int sysfs_deprecated_setup(char *arg)
40e52eec13SAndi Kleen {
41e52eec13SAndi Kleen 	return strict_strtol(arg, 10, &sysfs_deprecated);
42e52eec13SAndi Kleen }
43e52eec13SAndi Kleen early_param("sysfs.deprecated", sysfs_deprecated_setup);
44e52eec13SAndi Kleen #endif
45e52eec13SAndi Kleen 
461da177e4SLinus Torvalds int (*platform_notify)(struct device *dev) = NULL;
471da177e4SLinus Torvalds int (*platform_notify_remove)(struct device *dev) = NULL;
48e105b8bfSDan Williams static struct kobject *dev_kobj;
49e105b8bfSDan Williams struct kobject *sysfs_dev_char_kobj;
50e105b8bfSDan Williams struct kobject *sysfs_dev_block_kobj;
511da177e4SLinus Torvalds 
524e886c29SGreg Kroah-Hartman #ifdef CONFIG_BLOCK
534e886c29SGreg Kroah-Hartman static inline int device_is_not_partition(struct device *dev)
544e886c29SGreg Kroah-Hartman {
554e886c29SGreg Kroah-Hartman 	return !(dev->type == &part_type);
564e886c29SGreg Kroah-Hartman }
574e886c29SGreg Kroah-Hartman #else
584e886c29SGreg Kroah-Hartman static inline int device_is_not_partition(struct device *dev)
594e886c29SGreg Kroah-Hartman {
604e886c29SGreg Kroah-Hartman 	return 1;
614e886c29SGreg Kroah-Hartman }
624e886c29SGreg Kroah-Hartman #endif
631da177e4SLinus Torvalds 
643e95637aSAlan Stern /**
653e95637aSAlan Stern  * dev_driver_string - Return a device's driver name, if at all possible
663e95637aSAlan Stern  * @dev: struct device to get the name of
673e95637aSAlan Stern  *
683e95637aSAlan Stern  * Will return the device's driver's name if it is bound to a device.  If
699169c012Syan  * the device is not bound to a driver, it will return the name of the bus
703e95637aSAlan Stern  * it is attached to.  If it is not attached to a bus either, an empty
713e95637aSAlan Stern  * string will be returned.
723e95637aSAlan Stern  */
73bf9ca69fSJean Delvare const char *dev_driver_string(const struct device *dev)
743e95637aSAlan Stern {
753589972eSAlan Stern 	struct device_driver *drv;
763589972eSAlan Stern 
773589972eSAlan Stern 	/* dev->driver can change to NULL underneath us because of unbinding,
783589972eSAlan Stern 	 * so be careful about accessing it.  dev->bus and dev->class should
793589972eSAlan Stern 	 * never change once they are set, so they don't need special care.
803589972eSAlan Stern 	 */
813589972eSAlan Stern 	drv = ACCESS_ONCE(dev->driver);
823589972eSAlan Stern 	return drv ? drv->name :
83a456b702SJean Delvare 			(dev->bus ? dev->bus->name :
84a456b702SJean Delvare 			(dev->class ? dev->class->name : ""));
853e95637aSAlan Stern }
86310a922dSMatthew Wilcox EXPORT_SYMBOL(dev_driver_string);
873e95637aSAlan Stern 
881da177e4SLinus Torvalds #define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr)
891da177e4SLinus Torvalds 
904a3ad20cSGreg Kroah-Hartman static ssize_t dev_attr_show(struct kobject *kobj, struct attribute *attr,
914a3ad20cSGreg Kroah-Hartman 			     char *buf)
921da177e4SLinus Torvalds {
931da177e4SLinus Torvalds 	struct device_attribute *dev_attr = to_dev_attr(attr);
94b0d1f807SLars-Peter Clausen 	struct device *dev = kobj_to_dev(kobj);
954a0c20bfSDmitry Torokhov 	ssize_t ret = -EIO;
961da177e4SLinus Torvalds 
971da177e4SLinus Torvalds 	if (dev_attr->show)
9854b6f35cSYani Ioannou 		ret = dev_attr->show(dev, dev_attr, buf);
99815d2d50SAndrew Morton 	if (ret >= (ssize_t)PAGE_SIZE) {
100815d2d50SAndrew Morton 		print_symbol("dev_attr_show: %s returned bad count\n",
101815d2d50SAndrew Morton 				(unsigned long)dev_attr->show);
102815d2d50SAndrew Morton 	}
1031da177e4SLinus Torvalds 	return ret;
1041da177e4SLinus Torvalds }
1051da177e4SLinus Torvalds 
1064a3ad20cSGreg Kroah-Hartman static ssize_t dev_attr_store(struct kobject *kobj, struct attribute *attr,
1071da177e4SLinus Torvalds 			      const char *buf, size_t count)
1081da177e4SLinus Torvalds {
1091da177e4SLinus Torvalds 	struct device_attribute *dev_attr = to_dev_attr(attr);
110b0d1f807SLars-Peter Clausen 	struct device *dev = kobj_to_dev(kobj);
1114a0c20bfSDmitry Torokhov 	ssize_t ret = -EIO;
1121da177e4SLinus Torvalds 
1131da177e4SLinus Torvalds 	if (dev_attr->store)
11454b6f35cSYani Ioannou 		ret = dev_attr->store(dev, dev_attr, buf, count);
1151da177e4SLinus Torvalds 	return ret;
1161da177e4SLinus Torvalds }
1171da177e4SLinus Torvalds 
11852cf25d0SEmese Revfy static const struct sysfs_ops dev_sysfs_ops = {
1191da177e4SLinus Torvalds 	.show	= dev_attr_show,
1201da177e4SLinus Torvalds 	.store	= dev_attr_store,
1211da177e4SLinus Torvalds };
1221da177e4SLinus Torvalds 
123ca22e56dSKay Sievers #define to_ext_attr(x) container_of(x, struct dev_ext_attribute, attr)
124ca22e56dSKay Sievers 
125ca22e56dSKay Sievers ssize_t device_store_ulong(struct device *dev,
126ca22e56dSKay Sievers 			   struct device_attribute *attr,
127ca22e56dSKay Sievers 			   const char *buf, size_t size)
128ca22e56dSKay Sievers {
129ca22e56dSKay Sievers 	struct dev_ext_attribute *ea = to_ext_attr(attr);
130ca22e56dSKay Sievers 	char *end;
131ca22e56dSKay Sievers 	unsigned long new = simple_strtoul(buf, &end, 0);
132ca22e56dSKay Sievers 	if (end == buf)
133ca22e56dSKay Sievers 		return -EINVAL;
134ca22e56dSKay Sievers 	*(unsigned long *)(ea->var) = new;
135ca22e56dSKay Sievers 	/* Always return full write size even if we didn't consume all */
136ca22e56dSKay Sievers 	return size;
137ca22e56dSKay Sievers }
138ca22e56dSKay Sievers EXPORT_SYMBOL_GPL(device_store_ulong);
139ca22e56dSKay Sievers 
140ca22e56dSKay Sievers ssize_t device_show_ulong(struct device *dev,
141ca22e56dSKay Sievers 			  struct device_attribute *attr,
142ca22e56dSKay Sievers 			  char *buf)
143ca22e56dSKay Sievers {
144ca22e56dSKay Sievers 	struct dev_ext_attribute *ea = to_ext_attr(attr);
145ca22e56dSKay Sievers 	return snprintf(buf, PAGE_SIZE, "%lx\n", *(unsigned long *)(ea->var));
146ca22e56dSKay Sievers }
147ca22e56dSKay Sievers EXPORT_SYMBOL_GPL(device_show_ulong);
148ca22e56dSKay Sievers 
149ca22e56dSKay Sievers ssize_t device_store_int(struct device *dev,
150ca22e56dSKay Sievers 			 struct device_attribute *attr,
151ca22e56dSKay Sievers 			 const char *buf, size_t size)
152ca22e56dSKay Sievers {
153ca22e56dSKay Sievers 	struct dev_ext_attribute *ea = to_ext_attr(attr);
154ca22e56dSKay Sievers 	char *end;
155ca22e56dSKay Sievers 	long new = simple_strtol(buf, &end, 0);
156ca22e56dSKay Sievers 	if (end == buf || new > INT_MAX || new < INT_MIN)
157ca22e56dSKay Sievers 		return -EINVAL;
158ca22e56dSKay Sievers 	*(int *)(ea->var) = new;
159ca22e56dSKay Sievers 	/* Always return full write size even if we didn't consume all */
160ca22e56dSKay Sievers 	return size;
161ca22e56dSKay Sievers }
162ca22e56dSKay Sievers EXPORT_SYMBOL_GPL(device_store_int);
163ca22e56dSKay Sievers 
164ca22e56dSKay Sievers ssize_t device_show_int(struct device *dev,
165ca22e56dSKay Sievers 			struct device_attribute *attr,
166ca22e56dSKay Sievers 			char *buf)
167ca22e56dSKay Sievers {
168ca22e56dSKay Sievers 	struct dev_ext_attribute *ea = to_ext_attr(attr);
169ca22e56dSKay Sievers 
170ca22e56dSKay Sievers 	return snprintf(buf, PAGE_SIZE, "%d\n", *(int *)(ea->var));
171ca22e56dSKay Sievers }
172ca22e56dSKay Sievers EXPORT_SYMBOL_GPL(device_show_int);
1731da177e4SLinus Torvalds 
1741da177e4SLinus Torvalds /**
1751da177e4SLinus Torvalds  *	device_release - free device structure.
1761da177e4SLinus Torvalds  *	@kobj:	device's kobject.
1771da177e4SLinus Torvalds  *
1781da177e4SLinus Torvalds  *	This is called once the reference count for the object
1791da177e4SLinus Torvalds  *	reaches 0. We forward the call to the device's release
1801da177e4SLinus Torvalds  *	method, which should handle actually freeing the structure.
1811da177e4SLinus Torvalds  */
1821da177e4SLinus Torvalds static void device_release(struct kobject *kobj)
1831da177e4SLinus Torvalds {
184b0d1f807SLars-Peter Clausen 	struct device *dev = kobj_to_dev(kobj);
185fb069a5dSGreg Kroah-Hartman 	struct device_private *p = dev->p;
1861da177e4SLinus Torvalds 
187a525a3ddSMing Lei 	/*
188a525a3ddSMing Lei 	 * Some platform devices are driven without driver attached
189a525a3ddSMing Lei 	 * and managed resources may have been acquired.  Make sure
190a525a3ddSMing Lei 	 * all resources are released.
191a525a3ddSMing Lei 	 *
192a525a3ddSMing Lei 	 * Drivers still can add resources into device after device
193a525a3ddSMing Lei 	 * is deleted but alive, so release devres here to avoid
194a525a3ddSMing Lei 	 * possible memory leak.
195a525a3ddSMing Lei 	 */
196a525a3ddSMing Lei 	devres_release_all(dev);
197a525a3ddSMing Lei 
1981da177e4SLinus Torvalds 	if (dev->release)
1991da177e4SLinus Torvalds 		dev->release(dev);
200f9f852dfSKay Sievers 	else if (dev->type && dev->type->release)
201f9f852dfSKay Sievers 		dev->type->release(dev);
2022620efefSGreg Kroah-Hartman 	else if (dev->class && dev->class->dev_release)
2032620efefSGreg Kroah-Hartman 		dev->class->dev_release(dev);
204f810a5cfSArjan van de Ven 	else
205f810a5cfSArjan van de Ven 		WARN(1, KERN_ERR "Device '%s' does not have a release() "
2064a3ad20cSGreg Kroah-Hartman 			"function, it is broken and must be fixed.\n",
2071e0b2cf9SKay Sievers 			dev_name(dev));
208fb069a5dSGreg Kroah-Hartman 	kfree(p);
2091da177e4SLinus Torvalds }
2101da177e4SLinus Torvalds 
211bc451f20SEric W. Biederman static const void *device_namespace(struct kobject *kobj)
212bc451f20SEric W. Biederman {
213b0d1f807SLars-Peter Clausen 	struct device *dev = kobj_to_dev(kobj);
214bc451f20SEric W. Biederman 	const void *ns = NULL;
215bc451f20SEric W. Biederman 
216bc451f20SEric W. Biederman 	if (dev->class && dev->class->ns_type)
217bc451f20SEric W. Biederman 		ns = dev->class->namespace(dev);
218bc451f20SEric W. Biederman 
219bc451f20SEric W. Biederman 	return ns;
220bc451f20SEric W. Biederman }
221bc451f20SEric W. Biederman 
2228f4afc41SGreg Kroah-Hartman static struct kobj_type device_ktype = {
2231da177e4SLinus Torvalds 	.release	= device_release,
2241da177e4SLinus Torvalds 	.sysfs_ops	= &dev_sysfs_ops,
225bc451f20SEric W. Biederman 	.namespace	= device_namespace,
2261da177e4SLinus Torvalds };
2271da177e4SLinus Torvalds 
2281da177e4SLinus Torvalds 
229312c004dSKay Sievers static int dev_uevent_filter(struct kset *kset, struct kobject *kobj)
2301da177e4SLinus Torvalds {
2311da177e4SLinus Torvalds 	struct kobj_type *ktype = get_ktype(kobj);
2321da177e4SLinus Torvalds 
2338f4afc41SGreg Kroah-Hartman 	if (ktype == &device_ktype) {
234b0d1f807SLars-Peter Clausen 		struct device *dev = kobj_to_dev(kobj);
2351da177e4SLinus Torvalds 		if (dev->bus)
2361da177e4SLinus Torvalds 			return 1;
23723681e47SGreg Kroah-Hartman 		if (dev->class)
23823681e47SGreg Kroah-Hartman 			return 1;
2391da177e4SLinus Torvalds 	}
2401da177e4SLinus Torvalds 	return 0;
2411da177e4SLinus Torvalds }
2421da177e4SLinus Torvalds 
243312c004dSKay Sievers static const char *dev_uevent_name(struct kset *kset, struct kobject *kobj)
2441da177e4SLinus Torvalds {
245b0d1f807SLars-Peter Clausen 	struct device *dev = kobj_to_dev(kobj);
2461da177e4SLinus Torvalds 
24723681e47SGreg Kroah-Hartman 	if (dev->bus)
2481da177e4SLinus Torvalds 		return dev->bus->name;
24923681e47SGreg Kroah-Hartman 	if (dev->class)
25023681e47SGreg Kroah-Hartman 		return dev->class->name;
25123681e47SGreg Kroah-Hartman 	return NULL;
2521da177e4SLinus Torvalds }
2531da177e4SLinus Torvalds 
2547eff2e7aSKay Sievers static int dev_uevent(struct kset *kset, struct kobject *kobj,
2557eff2e7aSKay Sievers 		      struct kobj_uevent_env *env)
2561da177e4SLinus Torvalds {
257b0d1f807SLars-Peter Clausen 	struct device *dev = kobj_to_dev(kobj);
2581da177e4SLinus Torvalds 	int retval = 0;
2591da177e4SLinus Torvalds 
2606fcf53acSKay Sievers 	/* add device node properties if present */
26123681e47SGreg Kroah-Hartman 	if (MAJOR(dev->devt)) {
2626fcf53acSKay Sievers 		const char *tmp;
2636fcf53acSKay Sievers 		const char *name;
2642c9ede55SAl Viro 		umode_t mode = 0;
2656fcf53acSKay Sievers 
2667eff2e7aSKay Sievers 		add_uevent_var(env, "MAJOR=%u", MAJOR(dev->devt));
2677eff2e7aSKay Sievers 		add_uevent_var(env, "MINOR=%u", MINOR(dev->devt));
268e454cea2SKay Sievers 		name = device_get_devnode(dev, &mode, &tmp);
2696fcf53acSKay Sievers 		if (name) {
2706fcf53acSKay Sievers 			add_uevent_var(env, "DEVNAME=%s", name);
2716fcf53acSKay Sievers 			kfree(tmp);
272e454cea2SKay Sievers 			if (mode)
273e454cea2SKay Sievers 				add_uevent_var(env, "DEVMODE=%#o", mode & 0777);
2746fcf53acSKay Sievers 		}
27523681e47SGreg Kroah-Hartman 	}
27623681e47SGreg Kroah-Hartman 
277414264f9SKay Sievers 	if (dev->type && dev->type->name)
2787eff2e7aSKay Sievers 		add_uevent_var(env, "DEVTYPE=%s", dev->type->name);
279414264f9SKay Sievers 
280239378f1SKay Sievers 	if (dev->driver)
2817eff2e7aSKay Sievers 		add_uevent_var(env, "DRIVER=%s", dev->driver->name);
282239378f1SKay Sievers 
28307d57a32SGrant Likely 	/* Add common DT information about the device */
28407d57a32SGrant Likely 	of_device_uevent(dev, env);
28507d57a32SGrant Likely 
2861da177e4SLinus Torvalds 	/* have the bus specific function add its stuff */
2877eff2e7aSKay Sievers 	if (dev->bus && dev->bus->uevent) {
2887eff2e7aSKay Sievers 		retval = dev->bus->uevent(dev, env);
289f9f852dfSKay Sievers 		if (retval)
2907dc72b28SGreg Kroah-Hartman 			pr_debug("device: '%s': %s: bus uevent() returned %d\n",
2911e0b2cf9SKay Sievers 				 dev_name(dev), __func__, retval);
2921da177e4SLinus Torvalds 	}
2931da177e4SLinus Torvalds 
2942620efefSGreg Kroah-Hartman 	/* have the class specific function add its stuff */
2957eff2e7aSKay Sievers 	if (dev->class && dev->class->dev_uevent) {
2967eff2e7aSKay Sievers 		retval = dev->class->dev_uevent(dev, env);
297f9f852dfSKay Sievers 		if (retval)
2987dc72b28SGreg Kroah-Hartman 			pr_debug("device: '%s': %s: class uevent() "
2991e0b2cf9SKay Sievers 				 "returned %d\n", dev_name(dev),
3002b3a302aSHarvey Harrison 				 __func__, retval);
3012620efefSGreg Kroah-Hartman 	}
302f9f852dfSKay Sievers 
303eef35c2dSStefan Weil 	/* have the device type specific function add its stuff */
3047eff2e7aSKay Sievers 	if (dev->type && dev->type->uevent) {
3057eff2e7aSKay Sievers 		retval = dev->type->uevent(dev, env);
306f9f852dfSKay Sievers 		if (retval)
3077dc72b28SGreg Kroah-Hartman 			pr_debug("device: '%s': %s: dev_type uevent() "
3081e0b2cf9SKay Sievers 				 "returned %d\n", dev_name(dev),
3092b3a302aSHarvey Harrison 				 __func__, retval);
3102620efefSGreg Kroah-Hartman 	}
3112620efefSGreg Kroah-Hartman 
3121da177e4SLinus Torvalds 	return retval;
3131da177e4SLinus Torvalds }
3141da177e4SLinus Torvalds 
3159cd43611SEmese Revfy static const struct kset_uevent_ops device_uevent_ops = {
316312c004dSKay Sievers 	.filter =	dev_uevent_filter,
317312c004dSKay Sievers 	.name =		dev_uevent_name,
318312c004dSKay Sievers 	.uevent =	dev_uevent,
3191da177e4SLinus Torvalds };
3201da177e4SLinus Torvalds 
32116574dccSKay Sievers static ssize_t show_uevent(struct device *dev, struct device_attribute *attr,
32216574dccSKay Sievers 			   char *buf)
32316574dccSKay Sievers {
32416574dccSKay Sievers 	struct kobject *top_kobj;
32516574dccSKay Sievers 	struct kset *kset;
3267eff2e7aSKay Sievers 	struct kobj_uevent_env *env = NULL;
32716574dccSKay Sievers 	int i;
32816574dccSKay Sievers 	size_t count = 0;
32916574dccSKay Sievers 	int retval;
33016574dccSKay Sievers 
33116574dccSKay Sievers 	/* search the kset, the device belongs to */
33216574dccSKay Sievers 	top_kobj = &dev->kobj;
3335c5daf65SKay Sievers 	while (!top_kobj->kset && top_kobj->parent)
33416574dccSKay Sievers 		top_kobj = top_kobj->parent;
33516574dccSKay Sievers 	if (!top_kobj->kset)
33616574dccSKay Sievers 		goto out;
3375c5daf65SKay Sievers 
33816574dccSKay Sievers 	kset = top_kobj->kset;
33916574dccSKay Sievers 	if (!kset->uevent_ops || !kset->uevent_ops->uevent)
34016574dccSKay Sievers 		goto out;
34116574dccSKay Sievers 
34216574dccSKay Sievers 	/* respect filter */
34316574dccSKay Sievers 	if (kset->uevent_ops && kset->uevent_ops->filter)
34416574dccSKay Sievers 		if (!kset->uevent_ops->filter(kset, &dev->kobj))
34516574dccSKay Sievers 			goto out;
34616574dccSKay Sievers 
3477eff2e7aSKay Sievers 	env = kzalloc(sizeof(struct kobj_uevent_env), GFP_KERNEL);
3487eff2e7aSKay Sievers 	if (!env)
349c7308c81SGreg Kroah-Hartman 		return -ENOMEM;
350c7308c81SGreg Kroah-Hartman 
35116574dccSKay Sievers 	/* let the kset specific function add its keys */
3527eff2e7aSKay Sievers 	retval = kset->uevent_ops->uevent(kset, &dev->kobj, env);
35316574dccSKay Sievers 	if (retval)
35416574dccSKay Sievers 		goto out;
35516574dccSKay Sievers 
35616574dccSKay Sievers 	/* copy keys to file */
3577eff2e7aSKay Sievers 	for (i = 0; i < env->envp_idx; i++)
3587eff2e7aSKay Sievers 		count += sprintf(&buf[count], "%s\n", env->envp[i]);
35916574dccSKay Sievers out:
3607eff2e7aSKay Sievers 	kfree(env);
36116574dccSKay Sievers 	return count;
36216574dccSKay Sievers }
36316574dccSKay Sievers 
364a7fd6706SKay Sievers static ssize_t store_uevent(struct device *dev, struct device_attribute *attr,
365a7fd6706SKay Sievers 			    const char *buf, size_t count)
366a7fd6706SKay Sievers {
36760a96a59SKay Sievers 	enum kobject_action action;
36860a96a59SKay Sievers 
3693f5468c9SKay Sievers 	if (kobject_action_type(buf, count, &action) == 0)
37060a96a59SKay Sievers 		kobject_uevent(&dev->kobj, action);
3713f5468c9SKay Sievers 	else
3723f5468c9SKay Sievers 		dev_err(dev, "uevent: unknown action-string\n");
373a7fd6706SKay Sievers 	return count;
374a7fd6706SKay Sievers }
375a7fd6706SKay Sievers 
376ad6a1e1cSTejun Heo static struct device_attribute uevent_attr =
377ad6a1e1cSTejun Heo 	__ATTR(uevent, S_IRUGO | S_IWUSR, show_uevent, store_uevent);
378ad6a1e1cSTejun Heo 
379621a1672SDmitry Torokhov static int device_add_attributes(struct device *dev,
380621a1672SDmitry Torokhov 				 struct device_attribute *attrs)
381de0ff00dSGreg Kroah-Hartman {
382de0ff00dSGreg Kroah-Hartman 	int error = 0;
383621a1672SDmitry Torokhov 	int i;
384de0ff00dSGreg Kroah-Hartman 
385621a1672SDmitry Torokhov 	if (attrs) {
386621a1672SDmitry Torokhov 		for (i = 0; attr_name(attrs[i]); i++) {
387621a1672SDmitry Torokhov 			error = device_create_file(dev, &attrs[i]);
388621a1672SDmitry Torokhov 			if (error)
389621a1672SDmitry Torokhov 				break;
390621a1672SDmitry Torokhov 		}
391621a1672SDmitry Torokhov 		if (error)
392de0ff00dSGreg Kroah-Hartman 			while (--i >= 0)
393621a1672SDmitry Torokhov 				device_remove_file(dev, &attrs[i]);
394de0ff00dSGreg Kroah-Hartman 	}
395de0ff00dSGreg Kroah-Hartman 	return error;
396de0ff00dSGreg Kroah-Hartman }
397de0ff00dSGreg Kroah-Hartman 
398621a1672SDmitry Torokhov static void device_remove_attributes(struct device *dev,
399621a1672SDmitry Torokhov 				     struct device_attribute *attrs)
400de0ff00dSGreg Kroah-Hartman {
401de0ff00dSGreg Kroah-Hartman 	int i;
402621a1672SDmitry Torokhov 
403621a1672SDmitry Torokhov 	if (attrs)
404621a1672SDmitry Torokhov 		for (i = 0; attr_name(attrs[i]); i++)
405621a1672SDmitry Torokhov 			device_remove_file(dev, &attrs[i]);
406621a1672SDmitry Torokhov }
407621a1672SDmitry Torokhov 
408c97415a7SStefan Achatz static int device_add_bin_attributes(struct device *dev,
409c97415a7SStefan Achatz 				     struct bin_attribute *attrs)
410c97415a7SStefan Achatz {
411c97415a7SStefan Achatz 	int error = 0;
412c97415a7SStefan Achatz 	int i;
413c97415a7SStefan Achatz 
414c97415a7SStefan Achatz 	if (attrs) {
415c97415a7SStefan Achatz 		for (i = 0; attr_name(attrs[i]); i++) {
416c97415a7SStefan Achatz 			error = device_create_bin_file(dev, &attrs[i]);
417c97415a7SStefan Achatz 			if (error)
418c97415a7SStefan Achatz 				break;
419c97415a7SStefan Achatz 		}
420c97415a7SStefan Achatz 		if (error)
421c97415a7SStefan Achatz 			while (--i >= 0)
422c97415a7SStefan Achatz 				device_remove_bin_file(dev, &attrs[i]);
423c97415a7SStefan Achatz 	}
424c97415a7SStefan Achatz 	return error;
425c97415a7SStefan Achatz }
426c97415a7SStefan Achatz 
427c97415a7SStefan Achatz static void device_remove_bin_attributes(struct device *dev,
428c97415a7SStefan Achatz 					 struct bin_attribute *attrs)
429c97415a7SStefan Achatz {
430c97415a7SStefan Achatz 	int i;
431c97415a7SStefan Achatz 
432c97415a7SStefan Achatz 	if (attrs)
433c97415a7SStefan Achatz 		for (i = 0; attr_name(attrs[i]); i++)
434c97415a7SStefan Achatz 			device_remove_bin_file(dev, &attrs[i]);
435c97415a7SStefan Achatz }
436c97415a7SStefan Achatz 
437621a1672SDmitry Torokhov static int device_add_groups(struct device *dev,
438a4dbd674SDavid Brownell 			     const struct attribute_group **groups)
439621a1672SDmitry Torokhov {
440621a1672SDmitry Torokhov 	int error = 0;
441621a1672SDmitry Torokhov 	int i;
442621a1672SDmitry Torokhov 
443621a1672SDmitry Torokhov 	if (groups) {
444621a1672SDmitry Torokhov 		for (i = 0; groups[i]; i++) {
445621a1672SDmitry Torokhov 			error = sysfs_create_group(&dev->kobj, groups[i]);
446621a1672SDmitry Torokhov 			if (error) {
447621a1672SDmitry Torokhov 				while (--i >= 0)
4484a3ad20cSGreg Kroah-Hartman 					sysfs_remove_group(&dev->kobj,
4494a3ad20cSGreg Kroah-Hartman 							   groups[i]);
450621a1672SDmitry Torokhov 				break;
451de0ff00dSGreg Kroah-Hartman 			}
452de0ff00dSGreg Kroah-Hartman 		}
453de0ff00dSGreg Kroah-Hartman 	}
454621a1672SDmitry Torokhov 	return error;
455621a1672SDmitry Torokhov }
456621a1672SDmitry Torokhov 
457621a1672SDmitry Torokhov static void device_remove_groups(struct device *dev,
458a4dbd674SDavid Brownell 				 const struct attribute_group **groups)
459621a1672SDmitry Torokhov {
460621a1672SDmitry Torokhov 	int i;
461621a1672SDmitry Torokhov 
462621a1672SDmitry Torokhov 	if (groups)
463621a1672SDmitry Torokhov 		for (i = 0; groups[i]; i++)
464621a1672SDmitry Torokhov 			sysfs_remove_group(&dev->kobj, groups[i]);
465621a1672SDmitry Torokhov }
466de0ff00dSGreg Kroah-Hartman 
4672620efefSGreg Kroah-Hartman static int device_add_attrs(struct device *dev)
4682620efefSGreg Kroah-Hartman {
4692620efefSGreg Kroah-Hartman 	struct class *class = dev->class;
470aed65af1SStephen Hemminger 	const struct device_type *type = dev->type;
471621a1672SDmitry Torokhov 	int error;
4722620efefSGreg Kroah-Hartman 
473621a1672SDmitry Torokhov 	if (class) {
474621a1672SDmitry Torokhov 		error = device_add_attributes(dev, class->dev_attrs);
4752620efefSGreg Kroah-Hartman 		if (error)
476621a1672SDmitry Torokhov 			return error;
477c97415a7SStefan Achatz 		error = device_add_bin_attributes(dev, class->dev_bin_attrs);
478c97415a7SStefan Achatz 		if (error)
479c97415a7SStefan Achatz 			goto err_remove_class_attrs;
480f9f852dfSKay Sievers 	}
481f9f852dfSKay Sievers 
482621a1672SDmitry Torokhov 	if (type) {
483621a1672SDmitry Torokhov 		error = device_add_groups(dev, type->groups);
484f9f852dfSKay Sievers 		if (error)
485c97415a7SStefan Achatz 			goto err_remove_class_bin_attrs;
486f9f852dfSKay Sievers 	}
487621a1672SDmitry Torokhov 
488621a1672SDmitry Torokhov 	error = device_add_groups(dev, dev->groups);
489f9f852dfSKay Sievers 	if (error)
490621a1672SDmitry Torokhov 		goto err_remove_type_groups;
491621a1672SDmitry Torokhov 
492621a1672SDmitry Torokhov 	return 0;
493621a1672SDmitry Torokhov 
494621a1672SDmitry Torokhov  err_remove_type_groups:
495621a1672SDmitry Torokhov 	if (type)
496621a1672SDmitry Torokhov 		device_remove_groups(dev, type->groups);
497c97415a7SStefan Achatz  err_remove_class_bin_attrs:
498c97415a7SStefan Achatz 	if (class)
499c97415a7SStefan Achatz 		device_remove_bin_attributes(dev, class->dev_bin_attrs);
500621a1672SDmitry Torokhov  err_remove_class_attrs:
501621a1672SDmitry Torokhov 	if (class)
502621a1672SDmitry Torokhov 		device_remove_attributes(dev, class->dev_attrs);
503f9f852dfSKay Sievers 
5042620efefSGreg Kroah-Hartman 	return error;
5052620efefSGreg Kroah-Hartman }
5062620efefSGreg Kroah-Hartman 
5072620efefSGreg Kroah-Hartman static void device_remove_attrs(struct device *dev)
5082620efefSGreg Kroah-Hartman {
5092620efefSGreg Kroah-Hartman 	struct class *class = dev->class;
510aed65af1SStephen Hemminger 	const struct device_type *type = dev->type;
5112620efefSGreg Kroah-Hartman 
512621a1672SDmitry Torokhov 	device_remove_groups(dev, dev->groups);
513f9f852dfSKay Sievers 
514621a1672SDmitry Torokhov 	if (type)
515621a1672SDmitry Torokhov 		device_remove_groups(dev, type->groups);
516621a1672SDmitry Torokhov 
517c97415a7SStefan Achatz 	if (class) {
518621a1672SDmitry Torokhov 		device_remove_attributes(dev, class->dev_attrs);
519c97415a7SStefan Achatz 		device_remove_bin_attributes(dev, class->dev_bin_attrs);
520c97415a7SStefan Achatz 	}
5212620efefSGreg Kroah-Hartman }
5222620efefSGreg Kroah-Hartman 
5232620efefSGreg Kroah-Hartman 
52423681e47SGreg Kroah-Hartman static ssize_t show_dev(struct device *dev, struct device_attribute *attr,
52523681e47SGreg Kroah-Hartman 			char *buf)
52623681e47SGreg Kroah-Hartman {
52723681e47SGreg Kroah-Hartman 	return print_dev_t(buf, dev->devt);
52823681e47SGreg Kroah-Hartman }
52923681e47SGreg Kroah-Hartman 
530ad6a1e1cSTejun Heo static struct device_attribute devt_attr =
531ad6a1e1cSTejun Heo 	__ATTR(dev, S_IRUGO, show_dev, NULL);
532ad6a1e1cSTejun Heo 
533ca22e56dSKay Sievers /* /sys/devices/ */
534881c6cfdSGreg Kroah-Hartman struct kset *devices_kset;
5351da177e4SLinus Torvalds 
5361da177e4SLinus Torvalds /**
5371da177e4SLinus Torvalds  * device_create_file - create sysfs attribute file for device.
5381da177e4SLinus Torvalds  * @dev: device.
5391da177e4SLinus Torvalds  * @attr: device attribute descriptor.
5401da177e4SLinus Torvalds  */
54126579ab7SPhil Carmody int device_create_file(struct device *dev,
54226579ab7SPhil Carmody 		       const struct device_attribute *attr)
5431da177e4SLinus Torvalds {
5441da177e4SLinus Torvalds 	int error = 0;
5450c98b19fSCornelia Huck 	if (dev)
5461da177e4SLinus Torvalds 		error = sysfs_create_file(&dev->kobj, &attr->attr);
5471da177e4SLinus Torvalds 	return error;
5481da177e4SLinus Torvalds }
5491da177e4SLinus Torvalds 
5501da177e4SLinus Torvalds /**
5511da177e4SLinus Torvalds  * device_remove_file - remove sysfs attribute file.
5521da177e4SLinus Torvalds  * @dev: device.
5531da177e4SLinus Torvalds  * @attr: device attribute descriptor.
5541da177e4SLinus Torvalds  */
55526579ab7SPhil Carmody void device_remove_file(struct device *dev,
55626579ab7SPhil Carmody 			const struct device_attribute *attr)
5571da177e4SLinus Torvalds {
5580c98b19fSCornelia Huck 	if (dev)
5591da177e4SLinus Torvalds 		sysfs_remove_file(&dev->kobj, &attr->attr);
5601da177e4SLinus Torvalds }
5611da177e4SLinus Torvalds 
5622589f188SGreg Kroah-Hartman /**
5632589f188SGreg Kroah-Hartman  * device_create_bin_file - create sysfs binary attribute file for device.
5642589f188SGreg Kroah-Hartman  * @dev: device.
5652589f188SGreg Kroah-Hartman  * @attr: device binary attribute descriptor.
5662589f188SGreg Kroah-Hartman  */
56766ecb92bSPhil Carmody int device_create_bin_file(struct device *dev,
56866ecb92bSPhil Carmody 			   const struct bin_attribute *attr)
5692589f188SGreg Kroah-Hartman {
5702589f188SGreg Kroah-Hartman 	int error = -EINVAL;
5712589f188SGreg Kroah-Hartman 	if (dev)
5722589f188SGreg Kroah-Hartman 		error = sysfs_create_bin_file(&dev->kobj, attr);
5732589f188SGreg Kroah-Hartman 	return error;
5742589f188SGreg Kroah-Hartman }
5752589f188SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(device_create_bin_file);
5762589f188SGreg Kroah-Hartman 
5772589f188SGreg Kroah-Hartman /**
5782589f188SGreg Kroah-Hartman  * device_remove_bin_file - remove sysfs binary attribute file
5792589f188SGreg Kroah-Hartman  * @dev: device.
5802589f188SGreg Kroah-Hartman  * @attr: device binary attribute descriptor.
5812589f188SGreg Kroah-Hartman  */
58266ecb92bSPhil Carmody void device_remove_bin_file(struct device *dev,
58366ecb92bSPhil Carmody 			    const struct bin_attribute *attr)
5842589f188SGreg Kroah-Hartman {
5852589f188SGreg Kroah-Hartman 	if (dev)
5862589f188SGreg Kroah-Hartman 		sysfs_remove_bin_file(&dev->kobj, attr);
5872589f188SGreg Kroah-Hartman }
5882589f188SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(device_remove_bin_file);
5892589f188SGreg Kroah-Hartman 
590d9a9cdfbSAlan Stern /**
591523ded71SAlan Stern  * device_schedule_callback_owner - helper to schedule a callback for a device
592d9a9cdfbSAlan Stern  * @dev: device.
593d9a9cdfbSAlan Stern  * @func: callback function to invoke later.
594523ded71SAlan Stern  * @owner: module owning the callback routine
595d9a9cdfbSAlan Stern  *
596d9a9cdfbSAlan Stern  * Attribute methods must not unregister themselves or their parent device
597d9a9cdfbSAlan Stern  * (which would amount to the same thing).  Attempts to do so will deadlock,
598d9a9cdfbSAlan Stern  * since unregistration is mutually exclusive with driver callbacks.
599d9a9cdfbSAlan Stern  *
600d9a9cdfbSAlan Stern  * Instead methods can call this routine, which will attempt to allocate
601d9a9cdfbSAlan Stern  * and schedule a workqueue request to call back @func with @dev as its
602d9a9cdfbSAlan Stern  * argument in the workqueue's process context.  @dev will be pinned until
603d9a9cdfbSAlan Stern  * @func returns.
604d9a9cdfbSAlan Stern  *
605523ded71SAlan Stern  * This routine is usually called via the inline device_schedule_callback(),
606523ded71SAlan Stern  * which automatically sets @owner to THIS_MODULE.
607523ded71SAlan Stern  *
608d9a9cdfbSAlan Stern  * Returns 0 if the request was submitted, -ENOMEM if storage could not
609523ded71SAlan Stern  * be allocated, -ENODEV if a reference to @owner isn't available.
610d9a9cdfbSAlan Stern  *
611d9a9cdfbSAlan Stern  * NOTE: This routine won't work if CONFIG_SYSFS isn't set!  It uses an
612d9a9cdfbSAlan Stern  * underlying sysfs routine (since it is intended for use by attribute
613d9a9cdfbSAlan Stern  * methods), and if sysfs isn't available you'll get nothing but -ENOSYS.
614d9a9cdfbSAlan Stern  */
615523ded71SAlan Stern int device_schedule_callback_owner(struct device *dev,
616523ded71SAlan Stern 		void (*func)(struct device *), struct module *owner)
617d9a9cdfbSAlan Stern {
618d9a9cdfbSAlan Stern 	return sysfs_schedule_callback(&dev->kobj,
619523ded71SAlan Stern 			(void (*)(void *)) func, dev, owner);
620d9a9cdfbSAlan Stern }
621523ded71SAlan Stern EXPORT_SYMBOL_GPL(device_schedule_callback_owner);
622d9a9cdfbSAlan Stern 
62334bb61f9SJames Bottomley static void klist_children_get(struct klist_node *n)
62434bb61f9SJames Bottomley {
625f791b8c8SGreg Kroah-Hartman 	struct device_private *p = to_device_private_parent(n);
626f791b8c8SGreg Kroah-Hartman 	struct device *dev = p->device;
62734bb61f9SJames Bottomley 
62834bb61f9SJames Bottomley 	get_device(dev);
62934bb61f9SJames Bottomley }
63034bb61f9SJames Bottomley 
63134bb61f9SJames Bottomley static void klist_children_put(struct klist_node *n)
63234bb61f9SJames Bottomley {
633f791b8c8SGreg Kroah-Hartman 	struct device_private *p = to_device_private_parent(n);
634f791b8c8SGreg Kroah-Hartman 	struct device *dev = p->device;
63534bb61f9SJames Bottomley 
63634bb61f9SJames Bottomley 	put_device(dev);
63734bb61f9SJames Bottomley }
63834bb61f9SJames Bottomley 
6391da177e4SLinus Torvalds /**
6401da177e4SLinus Torvalds  * device_initialize - init device structure.
6411da177e4SLinus Torvalds  * @dev: device.
6421da177e4SLinus Torvalds  *
6435739411aSCornelia Huck  * This prepares the device for use by other layers by initializing
6445739411aSCornelia Huck  * its fields.
6451da177e4SLinus Torvalds  * It is the first half of device_register(), if called by
6465739411aSCornelia Huck  * that function, though it can also be called separately, so one
6475739411aSCornelia Huck  * may use @dev's fields. In particular, get_device()/put_device()
6485739411aSCornelia Huck  * may be used for reference counting of @dev after calling this
6495739411aSCornelia Huck  * function.
6505739411aSCornelia Huck  *
651b10d5efdSAlan Stern  * All fields in @dev must be initialized by the caller to 0, except
652b10d5efdSAlan Stern  * for those explicitly set to some other value.  The simplest
653b10d5efdSAlan Stern  * approach is to use kzalloc() to allocate the structure containing
654b10d5efdSAlan Stern  * @dev.
655b10d5efdSAlan Stern  *
6565739411aSCornelia Huck  * NOTE: Use put_device() to give up your reference instead of freeing
6575739411aSCornelia Huck  * @dev directly once you have called this function.
6581da177e4SLinus Torvalds  */
6591da177e4SLinus Torvalds void device_initialize(struct device *dev)
6601da177e4SLinus Torvalds {
661881c6cfdSGreg Kroah-Hartman 	dev->kobj.kset = devices_kset;
662f9cb074bSGreg Kroah-Hartman 	kobject_init(&dev->kobj, &device_ktype);
6631da177e4SLinus Torvalds 	INIT_LIST_HEAD(&dev->dma_pools);
6643142788bSThomas Gleixner 	mutex_init(&dev->mutex);
6651704f47bSPeter Zijlstra 	lockdep_set_novalidate_class(&dev->mutex);
6669ac7849eSTejun Heo 	spin_lock_init(&dev->devres_lock);
6679ac7849eSTejun Heo 	INIT_LIST_HEAD(&dev->devres_head);
6683b98aeafSAlan Stern 	device_pm_init(dev);
66987348136SChristoph Hellwig 	set_dev_node(dev, -1);
6701da177e4SLinus Torvalds }
6711da177e4SLinus Torvalds 
672c744aeaeSCornelia Huck static struct kobject *virtual_device_parent(struct device *dev)
673f0ee61a6SGreg Kroah-Hartman {
674f0ee61a6SGreg Kroah-Hartman 	static struct kobject *virtual_dir = NULL;
675f0ee61a6SGreg Kroah-Hartman 
676f0ee61a6SGreg Kroah-Hartman 	if (!virtual_dir)
6774ff6abffSGreg Kroah-Hartman 		virtual_dir = kobject_create_and_add("virtual",
678881c6cfdSGreg Kroah-Hartman 						     &devices_kset->kobj);
679f0ee61a6SGreg Kroah-Hartman 
68086406245SKay Sievers 	return virtual_dir;
681f0ee61a6SGreg Kroah-Hartman }
682f0ee61a6SGreg Kroah-Hartman 
683bc451f20SEric W. Biederman struct class_dir {
684bc451f20SEric W. Biederman 	struct kobject kobj;
685bc451f20SEric W. Biederman 	struct class *class;
686bc451f20SEric W. Biederman };
687bc451f20SEric W. Biederman 
688bc451f20SEric W. Biederman #define to_class_dir(obj) container_of(obj, struct class_dir, kobj)
689bc451f20SEric W. Biederman 
690bc451f20SEric W. Biederman static void class_dir_release(struct kobject *kobj)
691bc451f20SEric W. Biederman {
692bc451f20SEric W. Biederman 	struct class_dir *dir = to_class_dir(kobj);
693bc451f20SEric W. Biederman 	kfree(dir);
694bc451f20SEric W. Biederman }
695bc451f20SEric W. Biederman 
696bc451f20SEric W. Biederman static const
697bc451f20SEric W. Biederman struct kobj_ns_type_operations *class_dir_child_ns_type(struct kobject *kobj)
698bc451f20SEric W. Biederman {
699bc451f20SEric W. Biederman 	struct class_dir *dir = to_class_dir(kobj);
700bc451f20SEric W. Biederman 	return dir->class->ns_type;
701bc451f20SEric W. Biederman }
702bc451f20SEric W. Biederman 
703bc451f20SEric W. Biederman static struct kobj_type class_dir_ktype = {
704bc451f20SEric W. Biederman 	.release	= class_dir_release,
705bc451f20SEric W. Biederman 	.sysfs_ops	= &kobj_sysfs_ops,
706bc451f20SEric W. Biederman 	.child_ns_type	= class_dir_child_ns_type
707bc451f20SEric W. Biederman };
708bc451f20SEric W. Biederman 
709bc451f20SEric W. Biederman static struct kobject *
710bc451f20SEric W. Biederman class_dir_create_and_add(struct class *class, struct kobject *parent_kobj)
711bc451f20SEric W. Biederman {
712bc451f20SEric W. Biederman 	struct class_dir *dir;
713bc451f20SEric W. Biederman 	int retval;
714bc451f20SEric W. Biederman 
715bc451f20SEric W. Biederman 	dir = kzalloc(sizeof(*dir), GFP_KERNEL);
716bc451f20SEric W. Biederman 	if (!dir)
717bc451f20SEric W. Biederman 		return NULL;
718bc451f20SEric W. Biederman 
719bc451f20SEric W. Biederman 	dir->class = class;
720bc451f20SEric W. Biederman 	kobject_init(&dir->kobj, &class_dir_ktype);
721bc451f20SEric W. Biederman 
7226b6e39a6SKay Sievers 	dir->kobj.kset = &class->p->glue_dirs;
723bc451f20SEric W. Biederman 
724bc451f20SEric W. Biederman 	retval = kobject_add(&dir->kobj, parent_kobj, "%s", class->name);
725bc451f20SEric W. Biederman 	if (retval < 0) {
726bc451f20SEric W. Biederman 		kobject_put(&dir->kobj);
727bc451f20SEric W. Biederman 		return NULL;
728bc451f20SEric W. Biederman 	}
729bc451f20SEric W. Biederman 	return &dir->kobj;
730bc451f20SEric W. Biederman }
731bc451f20SEric W. Biederman 
732bc451f20SEric W. Biederman 
733c744aeaeSCornelia Huck static struct kobject *get_device_parent(struct device *dev,
734c744aeaeSCornelia Huck 					 struct device *parent)
73540fa5422SGreg Kroah-Hartman {
73686406245SKay Sievers 	if (dev->class) {
73777d3d7c1STejun Heo 		static DEFINE_MUTEX(gdp_mutex);
73886406245SKay Sievers 		struct kobject *kobj = NULL;
73986406245SKay Sievers 		struct kobject *parent_kobj;
74086406245SKay Sievers 		struct kobject *k;
74186406245SKay Sievers 
742ead454feSRandy Dunlap #ifdef CONFIG_BLOCK
74339aba963SKay Sievers 		/* block disks show up in /sys/block */
744e52eec13SAndi Kleen 		if (sysfs_deprecated && dev->class == &block_class) {
74539aba963SKay Sievers 			if (parent && parent->class == &block_class)
74639aba963SKay Sievers 				return &parent->kobj;
7476b6e39a6SKay Sievers 			return &block_class.p->subsys.kobj;
74839aba963SKay Sievers 		}
749ead454feSRandy Dunlap #endif
750e52eec13SAndi Kleen 
75186406245SKay Sievers 		/*
75286406245SKay Sievers 		 * If we have no parent, we live in "virtual".
7530f4dafc0SKay Sievers 		 * Class-devices with a non class-device as parent, live
7540f4dafc0SKay Sievers 		 * in a "glue" directory to prevent namespace collisions.
75586406245SKay Sievers 		 */
75686406245SKay Sievers 		if (parent == NULL)
75786406245SKay Sievers 			parent_kobj = virtual_device_parent(dev);
75824b1442dSEric W. Biederman 		else if (parent->class && !dev->class->ns_type)
75986406245SKay Sievers 			return &parent->kobj;
76086406245SKay Sievers 		else
76186406245SKay Sievers 			parent_kobj = &parent->kobj;
76286406245SKay Sievers 
76377d3d7c1STejun Heo 		mutex_lock(&gdp_mutex);
76477d3d7c1STejun Heo 
76586406245SKay Sievers 		/* find our class-directory at the parent and reference it */
7666b6e39a6SKay Sievers 		spin_lock(&dev->class->p->glue_dirs.list_lock);
7676b6e39a6SKay Sievers 		list_for_each_entry(k, &dev->class->p->glue_dirs.list, entry)
76886406245SKay Sievers 			if (k->parent == parent_kobj) {
76986406245SKay Sievers 				kobj = kobject_get(k);
77086406245SKay Sievers 				break;
77186406245SKay Sievers 			}
7726b6e39a6SKay Sievers 		spin_unlock(&dev->class->p->glue_dirs.list_lock);
77377d3d7c1STejun Heo 		if (kobj) {
77477d3d7c1STejun Heo 			mutex_unlock(&gdp_mutex);
77586406245SKay Sievers 			return kobj;
77677d3d7c1STejun Heo 		}
77786406245SKay Sievers 
77886406245SKay Sievers 		/* or create a new class-directory at the parent device */
779bc451f20SEric W. Biederman 		k = class_dir_create_and_add(dev->class, parent_kobj);
7800f4dafc0SKay Sievers 		/* do not emit an uevent for this simple "glue" directory */
78177d3d7c1STejun Heo 		mutex_unlock(&gdp_mutex);
78243968d2fSGreg Kroah-Hartman 		return k;
78386406245SKay Sievers 	}
78486406245SKay Sievers 
785ca22e56dSKay Sievers 	/* subsystems can specify a default root directory for their devices */
786ca22e56dSKay Sievers 	if (!parent && dev->bus && dev->bus->dev_root)
787ca22e56dSKay Sievers 		return &dev->bus->dev_root->kobj;
788ca22e56dSKay Sievers 
78986406245SKay Sievers 	if (parent)
790c744aeaeSCornelia Huck 		return &parent->kobj;
791c744aeaeSCornelia Huck 	return NULL;
792c744aeaeSCornelia Huck }
793da231fd5SKay Sievers 
79463b6971aSCornelia Huck static void cleanup_glue_dir(struct device *dev, struct kobject *glue_dir)
795da231fd5SKay Sievers {
7960f4dafc0SKay Sievers 	/* see if we live in a "glue" directory */
797c1fe539aSCornelia Huck 	if (!glue_dir || !dev->class ||
7986b6e39a6SKay Sievers 	    glue_dir->kset != &dev->class->p->glue_dirs)
799da231fd5SKay Sievers 		return;
800da231fd5SKay Sievers 
8010f4dafc0SKay Sievers 	kobject_put(glue_dir);
802da231fd5SKay Sievers }
80363b6971aSCornelia Huck 
80463b6971aSCornelia Huck static void cleanup_device_parent(struct device *dev)
80563b6971aSCornelia Huck {
80663b6971aSCornelia Huck 	cleanup_glue_dir(dev, dev->kobj.parent);
80763b6971aSCornelia Huck }
80886406245SKay Sievers 
8092ee97cafSCornelia Huck static int device_add_class_symlinks(struct device *dev)
8102ee97cafSCornelia Huck {
8112ee97cafSCornelia Huck 	int error;
8122ee97cafSCornelia Huck 
8132ee97cafSCornelia Huck 	if (!dev->class)
8142ee97cafSCornelia Huck 		return 0;
815da231fd5SKay Sievers 
8161fbfee6cSGreg Kroah-Hartman 	error = sysfs_create_link(&dev->kobj,
8176b6e39a6SKay Sievers 				  &dev->class->p->subsys.kobj,
8182ee97cafSCornelia Huck 				  "subsystem");
8192ee97cafSCornelia Huck 	if (error)
8202ee97cafSCornelia Huck 		goto out;
821da231fd5SKay Sievers 
8224e886c29SGreg Kroah-Hartman 	if (dev->parent && device_is_not_partition(dev)) {
8234f01a757SDmitry Torokhov 		error = sysfs_create_link(&dev->kobj, &dev->parent->kobj,
8244f01a757SDmitry Torokhov 					  "device");
8254f01a757SDmitry Torokhov 		if (error)
82639aba963SKay Sievers 			goto out_subsys;
8272ee97cafSCornelia Huck 	}
82839aba963SKay Sievers 
829ead454feSRandy Dunlap #ifdef CONFIG_BLOCK
83039aba963SKay Sievers 	/* /sys/block has directories and does not need symlinks */
831e52eec13SAndi Kleen 	if (sysfs_deprecated && dev->class == &block_class)
83239aba963SKay Sievers 		return 0;
833ead454feSRandy Dunlap #endif
83439aba963SKay Sievers 
83539aba963SKay Sievers 	/* link in the class directory pointing to the device */
8366b6e39a6SKay Sievers 	error = sysfs_create_link(&dev->class->p->subsys.kobj,
83739aba963SKay Sievers 				  &dev->kobj, dev_name(dev));
83839aba963SKay Sievers 	if (error)
83939aba963SKay Sievers 		goto out_device;
84039aba963SKay Sievers 
8412ee97cafSCornelia Huck 	return 0;
8422ee97cafSCornelia Huck 
84339aba963SKay Sievers out_device:
84439aba963SKay Sievers 	sysfs_remove_link(&dev->kobj, "device");
845da231fd5SKay Sievers 
8462ee97cafSCornelia Huck out_subsys:
8472ee97cafSCornelia Huck 	sysfs_remove_link(&dev->kobj, "subsystem");
8482ee97cafSCornelia Huck out:
8492ee97cafSCornelia Huck 	return error;
8502ee97cafSCornelia Huck }
8512ee97cafSCornelia Huck 
8522ee97cafSCornelia Huck static void device_remove_class_symlinks(struct device *dev)
8532ee97cafSCornelia Huck {
8542ee97cafSCornelia Huck 	if (!dev->class)
8552ee97cafSCornelia Huck 		return;
856da231fd5SKay Sievers 
8574e886c29SGreg Kroah-Hartman 	if (dev->parent && device_is_not_partition(dev))
858da231fd5SKay Sievers 		sysfs_remove_link(&dev->kobj, "device");
8592ee97cafSCornelia Huck 	sysfs_remove_link(&dev->kobj, "subsystem");
860ead454feSRandy Dunlap #ifdef CONFIG_BLOCK
861e52eec13SAndi Kleen 	if (sysfs_deprecated && dev->class == &block_class)
86239aba963SKay Sievers 		return;
863ead454feSRandy Dunlap #endif
8646b6e39a6SKay Sievers 	sysfs_delete_link(&dev->class->p->subsys.kobj, &dev->kobj, dev_name(dev));
8652ee97cafSCornelia Huck }
8662ee97cafSCornelia Huck 
8671da177e4SLinus Torvalds /**
868413c239fSStephen Rothwell  * dev_set_name - set a device name
869413c239fSStephen Rothwell  * @dev: device
87046232366SRandy Dunlap  * @fmt: format string for the device's name
871413c239fSStephen Rothwell  */
872413c239fSStephen Rothwell int dev_set_name(struct device *dev, const char *fmt, ...)
873413c239fSStephen Rothwell {
874413c239fSStephen Rothwell 	va_list vargs;
8751fa5ae85SKay Sievers 	int err;
876413c239fSStephen Rothwell 
877413c239fSStephen Rothwell 	va_start(vargs, fmt);
8781fa5ae85SKay Sievers 	err = kobject_set_name_vargs(&dev->kobj, fmt, vargs);
879413c239fSStephen Rothwell 	va_end(vargs);
8801fa5ae85SKay Sievers 	return err;
881413c239fSStephen Rothwell }
882413c239fSStephen Rothwell EXPORT_SYMBOL_GPL(dev_set_name);
883413c239fSStephen Rothwell 
884413c239fSStephen Rothwell /**
885e105b8bfSDan Williams  * device_to_dev_kobj - select a /sys/dev/ directory for the device
886e105b8bfSDan Williams  * @dev: device
887e105b8bfSDan Williams  *
888e105b8bfSDan Williams  * By default we select char/ for new entries.  Setting class->dev_obj
889e105b8bfSDan Williams  * to NULL prevents an entry from being created.  class->dev_kobj must
890e105b8bfSDan Williams  * be set (or cleared) before any devices are registered to the class
891e105b8bfSDan Williams  * otherwise device_create_sys_dev_entry() and
8920d4e293cSPeter Korsgaard  * device_remove_sys_dev_entry() will disagree about the presence of
8930d4e293cSPeter Korsgaard  * the link.
894e105b8bfSDan Williams  */
895e105b8bfSDan Williams static struct kobject *device_to_dev_kobj(struct device *dev)
896e105b8bfSDan Williams {
897e105b8bfSDan Williams 	struct kobject *kobj;
898e105b8bfSDan Williams 
899e105b8bfSDan Williams 	if (dev->class)
900e105b8bfSDan Williams 		kobj = dev->class->dev_kobj;
901e105b8bfSDan Williams 	else
902e105b8bfSDan Williams 		kobj = sysfs_dev_char_kobj;
903e105b8bfSDan Williams 
904e105b8bfSDan Williams 	return kobj;
905e105b8bfSDan Williams }
906e105b8bfSDan Williams 
907e105b8bfSDan Williams static int device_create_sys_dev_entry(struct device *dev)
908e105b8bfSDan Williams {
909e105b8bfSDan Williams 	struct kobject *kobj = device_to_dev_kobj(dev);
910e105b8bfSDan Williams 	int error = 0;
911e105b8bfSDan Williams 	char devt_str[15];
912e105b8bfSDan Williams 
913e105b8bfSDan Williams 	if (kobj) {
914e105b8bfSDan Williams 		format_dev_t(devt_str, dev->devt);
915e105b8bfSDan Williams 		error = sysfs_create_link(kobj, &dev->kobj, devt_str);
916e105b8bfSDan Williams 	}
917e105b8bfSDan Williams 
918e105b8bfSDan Williams 	return error;
919e105b8bfSDan Williams }
920e105b8bfSDan Williams 
921e105b8bfSDan Williams static void device_remove_sys_dev_entry(struct device *dev)
922e105b8bfSDan Williams {
923e105b8bfSDan Williams 	struct kobject *kobj = device_to_dev_kobj(dev);
924e105b8bfSDan Williams 	char devt_str[15];
925e105b8bfSDan Williams 
926e105b8bfSDan Williams 	if (kobj) {
927e105b8bfSDan Williams 		format_dev_t(devt_str, dev->devt);
928e105b8bfSDan Williams 		sysfs_remove_link(kobj, devt_str);
929e105b8bfSDan Williams 	}
930e105b8bfSDan Williams }
931e105b8bfSDan Williams 
932b4028437SGreg Kroah-Hartman int device_private_init(struct device *dev)
933b4028437SGreg Kroah-Hartman {
934b4028437SGreg Kroah-Hartman 	dev->p = kzalloc(sizeof(*dev->p), GFP_KERNEL);
935b4028437SGreg Kroah-Hartman 	if (!dev->p)
936b4028437SGreg Kroah-Hartman 		return -ENOMEM;
937b4028437SGreg Kroah-Hartman 	dev->p->device = dev;
938b4028437SGreg Kroah-Hartman 	klist_init(&dev->p->klist_children, klist_children_get,
939b4028437SGreg Kroah-Hartman 		   klist_children_put);
940ef8a3fd6SGreg Kroah-Hartman 	INIT_LIST_HEAD(&dev->p->deferred_probe);
941b4028437SGreg Kroah-Hartman 	return 0;
942b4028437SGreg Kroah-Hartman }
943b4028437SGreg Kroah-Hartman 
944e105b8bfSDan Williams /**
9451da177e4SLinus Torvalds  * device_add - add device to device hierarchy.
9461da177e4SLinus Torvalds  * @dev: device.
9471da177e4SLinus Torvalds  *
9481da177e4SLinus Torvalds  * This is part 2 of device_register(), though may be called
9491da177e4SLinus Torvalds  * separately _iff_ device_initialize() has been called separately.
9501da177e4SLinus Torvalds  *
9515739411aSCornelia Huck  * This adds @dev to the kobject hierarchy via kobject_add(), adds it
9521da177e4SLinus Torvalds  * to the global and sibling lists for the device, then
9531da177e4SLinus Torvalds  * adds it to the other relevant subsystems of the driver model.
9545739411aSCornelia Huck  *
955b10d5efdSAlan Stern  * Do not call this routine or device_register() more than once for
956b10d5efdSAlan Stern  * any device structure.  The driver model core is not designed to work
957b10d5efdSAlan Stern  * with devices that get unregistered and then spring back to life.
958b10d5efdSAlan Stern  * (Among other things, it's very hard to guarantee that all references
959b10d5efdSAlan Stern  * to the previous incarnation of @dev have been dropped.)  Allocate
960b10d5efdSAlan Stern  * and register a fresh new struct device instead.
961b10d5efdSAlan Stern  *
9625739411aSCornelia Huck  * NOTE: _Never_ directly free @dev after calling this function, even
9635739411aSCornelia Huck  * if it returned an error! Always use put_device() to give up your
9645739411aSCornelia Huck  * reference instead.
9651da177e4SLinus Torvalds  */
9661da177e4SLinus Torvalds int device_add(struct device *dev)
9671da177e4SLinus Torvalds {
9681da177e4SLinus Torvalds 	struct device *parent = NULL;
969ca22e56dSKay Sievers 	struct kobject *kobj;
970c47ed219SGreg Kroah-Hartman 	struct class_interface *class_intf;
971c906a48aSGreg Kroah-Hartman 	int error = -EINVAL;
972775b64d2SRafael J. Wysocki 
9731da177e4SLinus Torvalds 	dev = get_device(dev);
974c906a48aSGreg Kroah-Hartman 	if (!dev)
975c906a48aSGreg Kroah-Hartman 		goto done;
976c906a48aSGreg Kroah-Hartman 
977fb069a5dSGreg Kroah-Hartman 	if (!dev->p) {
978b4028437SGreg Kroah-Hartman 		error = device_private_init(dev);
979b4028437SGreg Kroah-Hartman 		if (error)
980fb069a5dSGreg Kroah-Hartman 			goto done;
981fb069a5dSGreg Kroah-Hartman 	}
982fb069a5dSGreg Kroah-Hartman 
9831fa5ae85SKay Sievers 	/*
9841fa5ae85SKay Sievers 	 * for statically allocated devices, which should all be converted
9851fa5ae85SKay Sievers 	 * some day, we need to initialize the name. We prevent reading back
9861fa5ae85SKay Sievers 	 * the name, and force the use of dev_name()
9871fa5ae85SKay Sievers 	 */
9881fa5ae85SKay Sievers 	if (dev->init_name) {
989acc0e90fSGreg Kroah-Hartman 		dev_set_name(dev, "%s", dev->init_name);
9901fa5ae85SKay Sievers 		dev->init_name = NULL;
9911fa5ae85SKay Sievers 	}
992c906a48aSGreg Kroah-Hartman 
993ca22e56dSKay Sievers 	/* subsystems can specify simple device enumeration */
994ca22e56dSKay Sievers 	if (!dev_name(dev) && dev->bus && dev->bus->dev_name)
995ca22e56dSKay Sievers 		dev_set_name(dev, "%s%u", dev->bus->dev_name, dev->id);
996ca22e56dSKay Sievers 
997e6309e75SThomas Gleixner 	if (!dev_name(dev)) {
998e6309e75SThomas Gleixner 		error = -EINVAL;
9995c8563d7SKay Sievers 		goto name_error;
1000e6309e75SThomas Gleixner 	}
10011da177e4SLinus Torvalds 
10021e0b2cf9SKay Sievers 	pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
1003c205ef48SGreg Kroah-Hartman 
10041da177e4SLinus Torvalds 	parent = get_device(dev->parent);
1005ca22e56dSKay Sievers 	kobj = get_device_parent(dev, parent);
1006ca22e56dSKay Sievers 	if (kobj)
1007ca22e56dSKay Sievers 		dev->kobj.parent = kobj;
10081da177e4SLinus Torvalds 
10090d358f22SYinghai Lu 	/* use parent numa_node */
10100d358f22SYinghai Lu 	if (parent)
10110d358f22SYinghai Lu 		set_dev_node(dev, dev_to_node(parent));
10120d358f22SYinghai Lu 
10131da177e4SLinus Torvalds 	/* first, register with generic layer. */
10148a577ffcSKay Sievers 	/* we require the name to be set before, and pass NULL */
10158a577ffcSKay Sievers 	error = kobject_add(&dev->kobj, dev->kobj.parent, NULL);
101640fa5422SGreg Kroah-Hartman 	if (error)
10171da177e4SLinus Torvalds 		goto Error;
1018a7fd6706SKay Sievers 
101937022644SBrian Walsh 	/* notify platform of device entry */
102037022644SBrian Walsh 	if (platform_notify)
102137022644SBrian Walsh 		platform_notify(dev);
102237022644SBrian Walsh 
1023ad6a1e1cSTejun Heo 	error = device_create_file(dev, &uevent_attr);
1024a306eea4SCornelia Huck 	if (error)
1025a306eea4SCornelia Huck 		goto attrError;
1026a7fd6706SKay Sievers 
102723681e47SGreg Kroah-Hartman 	if (MAJOR(dev->devt)) {
1028ad6a1e1cSTejun Heo 		error = device_create_file(dev, &devt_attr);
1029ad6a1e1cSTejun Heo 		if (error)
1030a306eea4SCornelia Huck 			goto ueventattrError;
1031e105b8bfSDan Williams 
1032e105b8bfSDan Williams 		error = device_create_sys_dev_entry(dev);
1033e105b8bfSDan Williams 		if (error)
1034e105b8bfSDan Williams 			goto devtattrError;
10352b2af54aSKay Sievers 
10362b2af54aSKay Sievers 		devtmpfs_create_node(dev);
103723681e47SGreg Kroah-Hartman 	}
103823681e47SGreg Kroah-Hartman 
10392ee97cafSCornelia Huck 	error = device_add_class_symlinks(dev);
10402ee97cafSCornelia Huck 	if (error)
10412ee97cafSCornelia Huck 		goto SymlinkError;
1042dc0afa83SCornelia Huck 	error = device_add_attrs(dev);
1043dc0afa83SCornelia Huck 	if (error)
10442620efefSGreg Kroah-Hartman 		goto AttrsError;
1045dc0afa83SCornelia Huck 	error = bus_add_device(dev);
1046dc0afa83SCornelia Huck 	if (error)
10471da177e4SLinus Torvalds 		goto BusError;
10483b98aeafSAlan Stern 	error = dpm_sysfs_add(dev);
104957eee3d2SRafael J. Wysocki 	if (error)
10503b98aeafSAlan Stern 		goto DPMError;
10513b98aeafSAlan Stern 	device_pm_add(dev);
1052ec0676eeSAlan Stern 
1053ec0676eeSAlan Stern 	/* Notify clients of device addition.  This call must come
1054268863f4Smajianpeng 	 * after dpm_sysfs_add() and before kobject_uevent().
1055ec0676eeSAlan Stern 	 */
1056ec0676eeSAlan Stern 	if (dev->bus)
1057ec0676eeSAlan Stern 		blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
1058ec0676eeSAlan Stern 					     BUS_NOTIFY_ADD_DEVICE, dev);
1059ec0676eeSAlan Stern 
106053877d06SKay Sievers 	kobject_uevent(&dev->kobj, KOBJ_ADD);
10612023c610SAlan Stern 	bus_probe_device(dev);
10621da177e4SLinus Torvalds 	if (parent)
1063f791b8c8SGreg Kroah-Hartman 		klist_add_tail(&dev->p->knode_parent,
1064f791b8c8SGreg Kroah-Hartman 			       &parent->p->klist_children);
10651da177e4SLinus Torvalds 
10665d9fd169SGreg Kroah-Hartman 	if (dev->class) {
1067ca22e56dSKay Sievers 		mutex_lock(&dev->class->p->mutex);
1068c47ed219SGreg Kroah-Hartman 		/* tie the class to the device */
10695a3ceb86STejun Heo 		klist_add_tail(&dev->knode_class,
10706b6e39a6SKay Sievers 			       &dev->class->p->klist_devices);
1071c47ed219SGreg Kroah-Hartman 
1072c47ed219SGreg Kroah-Hartman 		/* notify any interfaces that the device is here */
1073184f1f77SGreg Kroah-Hartman 		list_for_each_entry(class_intf,
1074ca22e56dSKay Sievers 				    &dev->class->p->interfaces, node)
1075c47ed219SGreg Kroah-Hartman 			if (class_intf->add_dev)
1076c47ed219SGreg Kroah-Hartman 				class_intf->add_dev(dev, class_intf);
1077ca22e56dSKay Sievers 		mutex_unlock(&dev->class->p->mutex);
10785d9fd169SGreg Kroah-Hartman 	}
1079c906a48aSGreg Kroah-Hartman done:
10801da177e4SLinus Torvalds 	put_device(dev);
10811da177e4SLinus Torvalds 	return error;
10823b98aeafSAlan Stern  DPMError:
108357eee3d2SRafael J. Wysocki 	bus_remove_device(dev);
108457eee3d2SRafael J. Wysocki  BusError:
10852620efefSGreg Kroah-Hartman 	device_remove_attrs(dev);
10862620efefSGreg Kroah-Hartman  AttrsError:
10872ee97cafSCornelia Huck 	device_remove_class_symlinks(dev);
10882ee97cafSCornelia Huck  SymlinkError:
1089ad6a1e1cSTejun Heo 	if (MAJOR(dev->devt))
1090ad72956dSKay Sievers 		devtmpfs_delete_node(dev);
1091ad72956dSKay Sievers 	if (MAJOR(dev->devt))
1092e105b8bfSDan Williams 		device_remove_sys_dev_entry(dev);
1093e105b8bfSDan Williams  devtattrError:
1094e105b8bfSDan Williams 	if (MAJOR(dev->devt))
1095ad6a1e1cSTejun Heo 		device_remove_file(dev, &devt_attr);
1096a306eea4SCornelia Huck  ueventattrError:
1097ad6a1e1cSTejun Heo 	device_remove_file(dev, &uevent_attr);
109823681e47SGreg Kroah-Hartman  attrError:
1099312c004dSKay Sievers 	kobject_uevent(&dev->kobj, KOBJ_REMOVE);
11001da177e4SLinus Torvalds 	kobject_del(&dev->kobj);
11011da177e4SLinus Torvalds  Error:
110263b6971aSCornelia Huck 	cleanup_device_parent(dev);
11031da177e4SLinus Torvalds 	if (parent)
11041da177e4SLinus Torvalds 		put_device(parent);
11055c8563d7SKay Sievers name_error:
11065c8563d7SKay Sievers 	kfree(dev->p);
11075c8563d7SKay Sievers 	dev->p = NULL;
1108c906a48aSGreg Kroah-Hartman 	goto done;
11091da177e4SLinus Torvalds }
11101da177e4SLinus Torvalds 
11111da177e4SLinus Torvalds /**
11121da177e4SLinus Torvalds  * device_register - register a device with the system.
11131da177e4SLinus Torvalds  * @dev: pointer to the device structure
11141da177e4SLinus Torvalds  *
11151da177e4SLinus Torvalds  * This happens in two clean steps - initialize the device
11161da177e4SLinus Torvalds  * and add it to the system. The two steps can be called
11171da177e4SLinus Torvalds  * separately, but this is the easiest and most common.
11181da177e4SLinus Torvalds  * I.e. you should only call the two helpers separately if
11191da177e4SLinus Torvalds  * have a clearly defined need to use and refcount the device
11201da177e4SLinus Torvalds  * before it is added to the hierarchy.
11215739411aSCornelia Huck  *
1122b10d5efdSAlan Stern  * For more information, see the kerneldoc for device_initialize()
1123b10d5efdSAlan Stern  * and device_add().
1124b10d5efdSAlan Stern  *
11255739411aSCornelia Huck  * NOTE: _Never_ directly free @dev after calling this function, even
11265739411aSCornelia Huck  * if it returned an error! Always use put_device() to give up the
11275739411aSCornelia Huck  * reference initialized in this function instead.
11281da177e4SLinus Torvalds  */
11291da177e4SLinus Torvalds int device_register(struct device *dev)
11301da177e4SLinus Torvalds {
11311da177e4SLinus Torvalds 	device_initialize(dev);
11321da177e4SLinus Torvalds 	return device_add(dev);
11331da177e4SLinus Torvalds }
11341da177e4SLinus Torvalds 
11351da177e4SLinus Torvalds /**
11361da177e4SLinus Torvalds  * get_device - increment reference count for device.
11371da177e4SLinus Torvalds  * @dev: device.
11381da177e4SLinus Torvalds  *
11391da177e4SLinus Torvalds  * This simply forwards the call to kobject_get(), though
11401da177e4SLinus Torvalds  * we do take care to provide for the case that we get a NULL
11411da177e4SLinus Torvalds  * pointer passed in.
11421da177e4SLinus Torvalds  */
11431da177e4SLinus Torvalds struct device *get_device(struct device *dev)
11441da177e4SLinus Torvalds {
1145b0d1f807SLars-Peter Clausen 	return dev ? kobj_to_dev(kobject_get(&dev->kobj)) : NULL;
11461da177e4SLinus Torvalds }
11471da177e4SLinus Torvalds 
11481da177e4SLinus Torvalds /**
11491da177e4SLinus Torvalds  * put_device - decrement reference count.
11501da177e4SLinus Torvalds  * @dev: device in question.
11511da177e4SLinus Torvalds  */
11521da177e4SLinus Torvalds void put_device(struct device *dev)
11531da177e4SLinus Torvalds {
1154edfaa7c3SKay Sievers 	/* might_sleep(); */
11551da177e4SLinus Torvalds 	if (dev)
11561da177e4SLinus Torvalds 		kobject_put(&dev->kobj);
11571da177e4SLinus Torvalds }
11581da177e4SLinus Torvalds 
11591da177e4SLinus Torvalds /**
11601da177e4SLinus Torvalds  * device_del - delete device from system.
11611da177e4SLinus Torvalds  * @dev: device.
11621da177e4SLinus Torvalds  *
11631da177e4SLinus Torvalds  * This is the first part of the device unregistration
11641da177e4SLinus Torvalds  * sequence. This removes the device from the lists we control
11651da177e4SLinus Torvalds  * from here, has it removed from the other driver model
11661da177e4SLinus Torvalds  * subsystems it was added to in device_add(), and removes it
11671da177e4SLinus Torvalds  * from the kobject hierarchy.
11681da177e4SLinus Torvalds  *
11691da177e4SLinus Torvalds  * NOTE: this should be called manually _iff_ device_add() was
11701da177e4SLinus Torvalds  * also called manually.
11711da177e4SLinus Torvalds  */
11721da177e4SLinus Torvalds void device_del(struct device *dev)
11731da177e4SLinus Torvalds {
11741da177e4SLinus Torvalds 	struct device *parent = dev->parent;
1175c47ed219SGreg Kroah-Hartman 	struct class_interface *class_intf;
11761da177e4SLinus Torvalds 
1177ec0676eeSAlan Stern 	/* Notify clients of device removal.  This call must come
1178ec0676eeSAlan Stern 	 * before dpm_sysfs_remove().
1179ec0676eeSAlan Stern 	 */
1180ec0676eeSAlan Stern 	if (dev->bus)
1181ec0676eeSAlan Stern 		blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
1182ec0676eeSAlan Stern 					     BUS_NOTIFY_DEL_DEVICE, dev);
1183775b64d2SRafael J. Wysocki 	device_pm_remove(dev);
11843b98aeafSAlan Stern 	dpm_sysfs_remove(dev);
11851da177e4SLinus Torvalds 	if (parent)
1186f791b8c8SGreg Kroah-Hartman 		klist_del(&dev->p->knode_parent);
1187e105b8bfSDan Williams 	if (MAJOR(dev->devt)) {
11882b2af54aSKay Sievers 		devtmpfs_delete_node(dev);
1189e105b8bfSDan Williams 		device_remove_sys_dev_entry(dev);
1190ad6a1e1cSTejun Heo 		device_remove_file(dev, &devt_attr);
1191e105b8bfSDan Williams 	}
1192b9d9c82bSKay Sievers 	if (dev->class) {
1193da231fd5SKay Sievers 		device_remove_class_symlinks(dev);
119499ef3ef8SKay Sievers 
1195ca22e56dSKay Sievers 		mutex_lock(&dev->class->p->mutex);
1196c47ed219SGreg Kroah-Hartman 		/* notify any interfaces that the device is now gone */
1197184f1f77SGreg Kroah-Hartman 		list_for_each_entry(class_intf,
1198ca22e56dSKay Sievers 				    &dev->class->p->interfaces, node)
1199c47ed219SGreg Kroah-Hartman 			if (class_intf->remove_dev)
1200c47ed219SGreg Kroah-Hartman 				class_intf->remove_dev(dev, class_intf);
1201c47ed219SGreg Kroah-Hartman 		/* remove the device from the class list */
12025a3ceb86STejun Heo 		klist_del(&dev->knode_class);
1203ca22e56dSKay Sievers 		mutex_unlock(&dev->class->p->mutex);
1204b9d9c82bSKay Sievers 	}
1205ad6a1e1cSTejun Heo 	device_remove_file(dev, &uevent_attr);
12062620efefSGreg Kroah-Hartman 	device_remove_attrs(dev);
120728953533SBenjamin Herrenschmidt 	bus_remove_device(dev);
1208d1c3414cSGrant Likely 	driver_deferred_probe_del(dev);
12091da177e4SLinus Torvalds 
12101da177e4SLinus Torvalds 	/* Notify the platform of the removal, in case they
12111da177e4SLinus Torvalds 	 * need to do anything...
12121da177e4SLinus Torvalds 	 */
12131da177e4SLinus Torvalds 	if (platform_notify_remove)
12141da177e4SLinus Torvalds 		platform_notify_remove(dev);
1215312c004dSKay Sievers 	kobject_uevent(&dev->kobj, KOBJ_REMOVE);
1216da231fd5SKay Sievers 	cleanup_device_parent(dev);
12171da177e4SLinus Torvalds 	kobject_del(&dev->kobj);
12181da177e4SLinus Torvalds 	put_device(parent);
12191da177e4SLinus Torvalds }
12201da177e4SLinus Torvalds 
12211da177e4SLinus Torvalds /**
12221da177e4SLinus Torvalds  * device_unregister - unregister device from system.
12231da177e4SLinus Torvalds  * @dev: device going away.
12241da177e4SLinus Torvalds  *
12251da177e4SLinus Torvalds  * We do this in two parts, like we do device_register(). First,
12261da177e4SLinus Torvalds  * we remove it from all the subsystems with device_del(), then
12271da177e4SLinus Torvalds  * we decrement the reference count via put_device(). If that
12281da177e4SLinus Torvalds  * is the final reference count, the device will be cleaned up
12291da177e4SLinus Torvalds  * via device_release() above. Otherwise, the structure will
12301da177e4SLinus Torvalds  * stick around until the final reference to the device is dropped.
12311da177e4SLinus Torvalds  */
12321da177e4SLinus Torvalds void device_unregister(struct device *dev)
12331da177e4SLinus Torvalds {
12341e0b2cf9SKay Sievers 	pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
12351da177e4SLinus Torvalds 	device_del(dev);
12361da177e4SLinus Torvalds 	put_device(dev);
12371da177e4SLinus Torvalds }
12381da177e4SLinus Torvalds 
123936239577Smochel@digitalimplant.org static struct device *next_device(struct klist_iter *i)
124036239577Smochel@digitalimplant.org {
124136239577Smochel@digitalimplant.org 	struct klist_node *n = klist_next(i);
1242f791b8c8SGreg Kroah-Hartman 	struct device *dev = NULL;
1243f791b8c8SGreg Kroah-Hartman 	struct device_private *p;
1244f791b8c8SGreg Kroah-Hartman 
1245f791b8c8SGreg Kroah-Hartman 	if (n) {
1246f791b8c8SGreg Kroah-Hartman 		p = to_device_private_parent(n);
1247f791b8c8SGreg Kroah-Hartman 		dev = p->device;
1248f791b8c8SGreg Kroah-Hartman 	}
1249f791b8c8SGreg Kroah-Hartman 	return dev;
125036239577Smochel@digitalimplant.org }
125136239577Smochel@digitalimplant.org 
12521da177e4SLinus Torvalds /**
1253e454cea2SKay Sievers  * device_get_devnode - path of device node file
12546fcf53acSKay Sievers  * @dev: device
1255e454cea2SKay Sievers  * @mode: returned file access mode
12566fcf53acSKay Sievers  * @tmp: possibly allocated string
12576fcf53acSKay Sievers  *
12586fcf53acSKay Sievers  * Return the relative path of a possible device node.
12596fcf53acSKay Sievers  * Non-default names may need to allocate a memory to compose
12606fcf53acSKay Sievers  * a name. This memory is returned in tmp and needs to be
12616fcf53acSKay Sievers  * freed by the caller.
12626fcf53acSKay Sievers  */
1263e454cea2SKay Sievers const char *device_get_devnode(struct device *dev,
12642c9ede55SAl Viro 			       umode_t *mode, const char **tmp)
12656fcf53acSKay Sievers {
12666fcf53acSKay Sievers 	char *s;
12676fcf53acSKay Sievers 
12686fcf53acSKay Sievers 	*tmp = NULL;
12696fcf53acSKay Sievers 
12706fcf53acSKay Sievers 	/* the device type may provide a specific name */
1271e454cea2SKay Sievers 	if (dev->type && dev->type->devnode)
1272e454cea2SKay Sievers 		*tmp = dev->type->devnode(dev, mode);
12736fcf53acSKay Sievers 	if (*tmp)
12746fcf53acSKay Sievers 		return *tmp;
12756fcf53acSKay Sievers 
12766fcf53acSKay Sievers 	/* the class may provide a specific name */
1277e454cea2SKay Sievers 	if (dev->class && dev->class->devnode)
1278e454cea2SKay Sievers 		*tmp = dev->class->devnode(dev, mode);
12796fcf53acSKay Sievers 	if (*tmp)
12806fcf53acSKay Sievers 		return *tmp;
12816fcf53acSKay Sievers 
12826fcf53acSKay Sievers 	/* return name without allocation, tmp == NULL */
12836fcf53acSKay Sievers 	if (strchr(dev_name(dev), '!') == NULL)
12846fcf53acSKay Sievers 		return dev_name(dev);
12856fcf53acSKay Sievers 
12866fcf53acSKay Sievers 	/* replace '!' in the name with '/' */
12876fcf53acSKay Sievers 	*tmp = kstrdup(dev_name(dev), GFP_KERNEL);
12886fcf53acSKay Sievers 	if (!*tmp)
12896fcf53acSKay Sievers 		return NULL;
12906fcf53acSKay Sievers 	while ((s = strchr(*tmp, '!')))
12916fcf53acSKay Sievers 		s[0] = '/';
12926fcf53acSKay Sievers 	return *tmp;
12936fcf53acSKay Sievers }
12946fcf53acSKay Sievers 
12956fcf53acSKay Sievers /**
12961da177e4SLinus Torvalds  * device_for_each_child - device child iterator.
1297c41455fbSRandy Dunlap  * @parent: parent struct device.
12981da177e4SLinus Torvalds  * @data: data for the callback.
12991da177e4SLinus Torvalds  * @fn: function to be called for each device.
13001da177e4SLinus Torvalds  *
1301c41455fbSRandy Dunlap  * Iterate over @parent's child devices, and call @fn for each,
13021da177e4SLinus Torvalds  * passing it @data.
13031da177e4SLinus Torvalds  *
13041da177e4SLinus Torvalds  * We check the return of @fn each time. If it returns anything
13051da177e4SLinus Torvalds  * other than 0, we break out and return that value.
13061da177e4SLinus Torvalds  */
130736239577Smochel@digitalimplant.org int device_for_each_child(struct device *parent, void *data,
13084a3ad20cSGreg Kroah-Hartman 			  int (*fn)(struct device *dev, void *data))
13091da177e4SLinus Torvalds {
131036239577Smochel@digitalimplant.org 	struct klist_iter i;
13111da177e4SLinus Torvalds 	struct device *child;
13121da177e4SLinus Torvalds 	int error = 0;
13131da177e4SLinus Torvalds 
1314014c90dbSGreg Kroah-Hartman 	if (!parent->p)
1315014c90dbSGreg Kroah-Hartman 		return 0;
1316014c90dbSGreg Kroah-Hartman 
1317f791b8c8SGreg Kroah-Hartman 	klist_iter_init(&parent->p->klist_children, &i);
131836239577Smochel@digitalimplant.org 	while ((child = next_device(&i)) && !error)
131936239577Smochel@digitalimplant.org 		error = fn(child, data);
132036239577Smochel@digitalimplant.org 	klist_iter_exit(&i);
13211da177e4SLinus Torvalds 	return error;
13221da177e4SLinus Torvalds }
13231da177e4SLinus Torvalds 
13245ab69981SCornelia Huck /**
13255ab69981SCornelia Huck  * device_find_child - device iterator for locating a particular device.
13265ab69981SCornelia Huck  * @parent: parent struct device
13275ab69981SCornelia Huck  * @data: Data to pass to match function
13285ab69981SCornelia Huck  * @match: Callback function to check device
13295ab69981SCornelia Huck  *
13305ab69981SCornelia Huck  * This is similar to the device_for_each_child() function above, but it
13315ab69981SCornelia Huck  * returns a reference to a device that is 'found' for later use, as
13325ab69981SCornelia Huck  * determined by the @match callback.
13335ab69981SCornelia Huck  *
13345ab69981SCornelia Huck  * The callback should return 0 if the device doesn't match and non-zero
13355ab69981SCornelia Huck  * if it does.  If the callback returns non-zero and a reference to the
13365ab69981SCornelia Huck  * current device can be obtained, this function will return to the caller
13375ab69981SCornelia Huck  * and not iterate over any more devices.
13385ab69981SCornelia Huck  */
13395ab69981SCornelia Huck struct device *device_find_child(struct device *parent, void *data,
13404a3ad20cSGreg Kroah-Hartman 				 int (*match)(struct device *dev, void *data))
13415ab69981SCornelia Huck {
13425ab69981SCornelia Huck 	struct klist_iter i;
13435ab69981SCornelia Huck 	struct device *child;
13445ab69981SCornelia Huck 
13455ab69981SCornelia Huck 	if (!parent)
13465ab69981SCornelia Huck 		return NULL;
13475ab69981SCornelia Huck 
1348f791b8c8SGreg Kroah-Hartman 	klist_iter_init(&parent->p->klist_children, &i);
13495ab69981SCornelia Huck 	while ((child = next_device(&i)))
13505ab69981SCornelia Huck 		if (match(child, data) && get_device(child))
13515ab69981SCornelia Huck 			break;
13525ab69981SCornelia Huck 	klist_iter_exit(&i);
13535ab69981SCornelia Huck 	return child;
13545ab69981SCornelia Huck }
13555ab69981SCornelia Huck 
13561da177e4SLinus Torvalds int __init devices_init(void)
13571da177e4SLinus Torvalds {
1358881c6cfdSGreg Kroah-Hartman 	devices_kset = kset_create_and_add("devices", &device_uevent_ops, NULL);
1359881c6cfdSGreg Kroah-Hartman 	if (!devices_kset)
1360881c6cfdSGreg Kroah-Hartman 		return -ENOMEM;
1361e105b8bfSDan Williams 	dev_kobj = kobject_create_and_add("dev", NULL);
1362e105b8bfSDan Williams 	if (!dev_kobj)
1363e105b8bfSDan Williams 		goto dev_kobj_err;
1364e105b8bfSDan Williams 	sysfs_dev_block_kobj = kobject_create_and_add("block", dev_kobj);
1365e105b8bfSDan Williams 	if (!sysfs_dev_block_kobj)
1366e105b8bfSDan Williams 		goto block_kobj_err;
1367e105b8bfSDan Williams 	sysfs_dev_char_kobj = kobject_create_and_add("char", dev_kobj);
1368e105b8bfSDan Williams 	if (!sysfs_dev_char_kobj)
1369e105b8bfSDan Williams 		goto char_kobj_err;
1370e105b8bfSDan Williams 
1371881c6cfdSGreg Kroah-Hartman 	return 0;
1372e105b8bfSDan Williams 
1373e105b8bfSDan Williams  char_kobj_err:
1374e105b8bfSDan Williams 	kobject_put(sysfs_dev_block_kobj);
1375e105b8bfSDan Williams  block_kobj_err:
1376e105b8bfSDan Williams 	kobject_put(dev_kobj);
1377e105b8bfSDan Williams  dev_kobj_err:
1378e105b8bfSDan Williams 	kset_unregister(devices_kset);
1379e105b8bfSDan Williams 	return -ENOMEM;
13801da177e4SLinus Torvalds }
13811da177e4SLinus Torvalds 
13821da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(device_for_each_child);
13835ab69981SCornelia Huck EXPORT_SYMBOL_GPL(device_find_child);
13841da177e4SLinus Torvalds 
13851da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(device_initialize);
13861da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(device_add);
13871da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(device_register);
13881da177e4SLinus Torvalds 
13891da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(device_del);
13901da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(device_unregister);
13911da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(get_device);
13921da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(put_device);
13931da177e4SLinus Torvalds 
13941da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(device_create_file);
13951da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(device_remove_file);
139623681e47SGreg Kroah-Hartman 
13977f100d15SKarthigan Srinivasan struct root_device {
13980aa0dc41SMark McLoughlin 	struct device dev;
13990aa0dc41SMark McLoughlin 	struct module *owner;
14000aa0dc41SMark McLoughlin };
14010aa0dc41SMark McLoughlin 
1402481e2079SFerenc Wagner inline struct root_device *to_root_device(struct device *d)
1403481e2079SFerenc Wagner {
1404481e2079SFerenc Wagner 	return container_of(d, struct root_device, dev);
1405481e2079SFerenc Wagner }
14060aa0dc41SMark McLoughlin 
14070aa0dc41SMark McLoughlin static void root_device_release(struct device *dev)
14080aa0dc41SMark McLoughlin {
14090aa0dc41SMark McLoughlin 	kfree(to_root_device(dev));
14100aa0dc41SMark McLoughlin }
14110aa0dc41SMark McLoughlin 
14120aa0dc41SMark McLoughlin /**
14130aa0dc41SMark McLoughlin  * __root_device_register - allocate and register a root device
14140aa0dc41SMark McLoughlin  * @name: root device name
14150aa0dc41SMark McLoughlin  * @owner: owner module of the root device, usually THIS_MODULE
14160aa0dc41SMark McLoughlin  *
14170aa0dc41SMark McLoughlin  * This function allocates a root device and registers it
14180aa0dc41SMark McLoughlin  * using device_register(). In order to free the returned
14190aa0dc41SMark McLoughlin  * device, use root_device_unregister().
14200aa0dc41SMark McLoughlin  *
14210aa0dc41SMark McLoughlin  * Root devices are dummy devices which allow other devices
14220aa0dc41SMark McLoughlin  * to be grouped under /sys/devices. Use this function to
14230aa0dc41SMark McLoughlin  * allocate a root device and then use it as the parent of
14240aa0dc41SMark McLoughlin  * any device which should appear under /sys/devices/{name}
14250aa0dc41SMark McLoughlin  *
14260aa0dc41SMark McLoughlin  * The /sys/devices/{name} directory will also contain a
14270aa0dc41SMark McLoughlin  * 'module' symlink which points to the @owner directory
14280aa0dc41SMark McLoughlin  * in sysfs.
14290aa0dc41SMark McLoughlin  *
1430f0eae0edSJani Nikula  * Returns &struct device pointer on success, or ERR_PTR() on error.
1431f0eae0edSJani Nikula  *
14320aa0dc41SMark McLoughlin  * Note: You probably want to use root_device_register().
14330aa0dc41SMark McLoughlin  */
14340aa0dc41SMark McLoughlin struct device *__root_device_register(const char *name, struct module *owner)
14350aa0dc41SMark McLoughlin {
14360aa0dc41SMark McLoughlin 	struct root_device *root;
14370aa0dc41SMark McLoughlin 	int err = -ENOMEM;
14380aa0dc41SMark McLoughlin 
14390aa0dc41SMark McLoughlin 	root = kzalloc(sizeof(struct root_device), GFP_KERNEL);
14400aa0dc41SMark McLoughlin 	if (!root)
14410aa0dc41SMark McLoughlin 		return ERR_PTR(err);
14420aa0dc41SMark McLoughlin 
1443acc0e90fSGreg Kroah-Hartman 	err = dev_set_name(&root->dev, "%s", name);
14440aa0dc41SMark McLoughlin 	if (err) {
14450aa0dc41SMark McLoughlin 		kfree(root);
14460aa0dc41SMark McLoughlin 		return ERR_PTR(err);
14470aa0dc41SMark McLoughlin 	}
14480aa0dc41SMark McLoughlin 
14490aa0dc41SMark McLoughlin 	root->dev.release = root_device_release;
14500aa0dc41SMark McLoughlin 
14510aa0dc41SMark McLoughlin 	err = device_register(&root->dev);
14520aa0dc41SMark McLoughlin 	if (err) {
14530aa0dc41SMark McLoughlin 		put_device(&root->dev);
14540aa0dc41SMark McLoughlin 		return ERR_PTR(err);
14550aa0dc41SMark McLoughlin 	}
14560aa0dc41SMark McLoughlin 
14571d9e882bSChristoph Egger #ifdef CONFIG_MODULES	/* gotta find a "cleaner" way to do this */
14580aa0dc41SMark McLoughlin 	if (owner) {
14590aa0dc41SMark McLoughlin 		struct module_kobject *mk = &owner->mkobj;
14600aa0dc41SMark McLoughlin 
14610aa0dc41SMark McLoughlin 		err = sysfs_create_link(&root->dev.kobj, &mk->kobj, "module");
14620aa0dc41SMark McLoughlin 		if (err) {
14630aa0dc41SMark McLoughlin 			device_unregister(&root->dev);
14640aa0dc41SMark McLoughlin 			return ERR_PTR(err);
14650aa0dc41SMark McLoughlin 		}
14660aa0dc41SMark McLoughlin 		root->owner = owner;
14670aa0dc41SMark McLoughlin 	}
14680aa0dc41SMark McLoughlin #endif
14690aa0dc41SMark McLoughlin 
14700aa0dc41SMark McLoughlin 	return &root->dev;
14710aa0dc41SMark McLoughlin }
14720aa0dc41SMark McLoughlin EXPORT_SYMBOL_GPL(__root_device_register);
14730aa0dc41SMark McLoughlin 
14740aa0dc41SMark McLoughlin /**
14750aa0dc41SMark McLoughlin  * root_device_unregister - unregister and free a root device
14767cbcf225SRandy Dunlap  * @dev: device going away
14770aa0dc41SMark McLoughlin  *
14780aa0dc41SMark McLoughlin  * This function unregisters and cleans up a device that was created by
14790aa0dc41SMark McLoughlin  * root_device_register().
14800aa0dc41SMark McLoughlin  */
14810aa0dc41SMark McLoughlin void root_device_unregister(struct device *dev)
14820aa0dc41SMark McLoughlin {
14830aa0dc41SMark McLoughlin 	struct root_device *root = to_root_device(dev);
14840aa0dc41SMark McLoughlin 
14850aa0dc41SMark McLoughlin 	if (root->owner)
14860aa0dc41SMark McLoughlin 		sysfs_remove_link(&root->dev.kobj, "module");
14870aa0dc41SMark McLoughlin 
14880aa0dc41SMark McLoughlin 	device_unregister(dev);
14890aa0dc41SMark McLoughlin }
14900aa0dc41SMark McLoughlin EXPORT_SYMBOL_GPL(root_device_unregister);
14910aa0dc41SMark McLoughlin 
149223681e47SGreg Kroah-Hartman 
149323681e47SGreg Kroah-Hartman static void device_create_release(struct device *dev)
149423681e47SGreg Kroah-Hartman {
14951e0b2cf9SKay Sievers 	pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
149623681e47SGreg Kroah-Hartman 	kfree(dev);
149723681e47SGreg Kroah-Hartman }
149823681e47SGreg Kroah-Hartman 
149923681e47SGreg Kroah-Hartman /**
15008882b394SGreg Kroah-Hartman  * device_create_vargs - creates a device and registers it with sysfs
15018882b394SGreg Kroah-Hartman  * @class: pointer to the struct class that this device should be registered to
15028882b394SGreg Kroah-Hartman  * @parent: pointer to the parent struct device of this new device, if any
15038882b394SGreg Kroah-Hartman  * @devt: the dev_t for the char device to be added
15048882b394SGreg Kroah-Hartman  * @drvdata: the data to be added to the device for callbacks
15058882b394SGreg Kroah-Hartman  * @fmt: string for the device's name
15068882b394SGreg Kroah-Hartman  * @args: va_list for the device's name
15078882b394SGreg Kroah-Hartman  *
15088882b394SGreg Kroah-Hartman  * This function can be used by char device classes.  A struct device
15098882b394SGreg Kroah-Hartman  * will be created in sysfs, registered to the specified class.
15108882b394SGreg Kroah-Hartman  *
15118882b394SGreg Kroah-Hartman  * A "dev" file will be created, showing the dev_t for the device, if
15128882b394SGreg Kroah-Hartman  * the dev_t is not 0,0.
15138882b394SGreg Kroah-Hartman  * If a pointer to a parent struct device is passed in, the newly created
15148882b394SGreg Kroah-Hartman  * struct device will be a child of that device in sysfs.
15158882b394SGreg Kroah-Hartman  * The pointer to the struct device will be returned from the call.
15168882b394SGreg Kroah-Hartman  * Any further sysfs files that might be required can be created using this
15178882b394SGreg Kroah-Hartman  * pointer.
15188882b394SGreg Kroah-Hartman  *
1519f0eae0edSJani Nikula  * Returns &struct device pointer on success, or ERR_PTR() on error.
1520f0eae0edSJani Nikula  *
15218882b394SGreg Kroah-Hartman  * Note: the struct class passed to this function must have previously
15228882b394SGreg Kroah-Hartman  * been created with a call to class_create().
15238882b394SGreg Kroah-Hartman  */
15248882b394SGreg Kroah-Hartman struct device *device_create_vargs(struct class *class, struct device *parent,
15258882b394SGreg Kroah-Hartman 				   dev_t devt, void *drvdata, const char *fmt,
15268882b394SGreg Kroah-Hartman 				   va_list args)
15278882b394SGreg Kroah-Hartman {
15288882b394SGreg Kroah-Hartman 	struct device *dev = NULL;
15298882b394SGreg Kroah-Hartman 	int retval = -ENODEV;
15308882b394SGreg Kroah-Hartman 
15318882b394SGreg Kroah-Hartman 	if (class == NULL || IS_ERR(class))
15328882b394SGreg Kroah-Hartman 		goto error;
15338882b394SGreg Kroah-Hartman 
15348882b394SGreg Kroah-Hartman 	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
15358882b394SGreg Kroah-Hartman 	if (!dev) {
15368882b394SGreg Kroah-Hartman 		retval = -ENOMEM;
15378882b394SGreg Kroah-Hartman 		goto error;
15388882b394SGreg Kroah-Hartman 	}
15398882b394SGreg Kroah-Hartman 
15408882b394SGreg Kroah-Hartman 	dev->devt = devt;
15418882b394SGreg Kroah-Hartman 	dev->class = class;
15428882b394SGreg Kroah-Hartman 	dev->parent = parent;
15438882b394SGreg Kroah-Hartman 	dev->release = device_create_release;
15448882b394SGreg Kroah-Hartman 	dev_set_drvdata(dev, drvdata);
15458882b394SGreg Kroah-Hartman 
15461fa5ae85SKay Sievers 	retval = kobject_set_name_vargs(&dev->kobj, fmt, args);
15471fa5ae85SKay Sievers 	if (retval)
15481fa5ae85SKay Sievers 		goto error;
15491fa5ae85SKay Sievers 
15508882b394SGreg Kroah-Hartman 	retval = device_register(dev);
15518882b394SGreg Kroah-Hartman 	if (retval)
15528882b394SGreg Kroah-Hartman 		goto error;
15538882b394SGreg Kroah-Hartman 
15548882b394SGreg Kroah-Hartman 	return dev;
15558882b394SGreg Kroah-Hartman 
15568882b394SGreg Kroah-Hartman error:
1557286661b3SCornelia Huck 	put_device(dev);
15588882b394SGreg Kroah-Hartman 	return ERR_PTR(retval);
15598882b394SGreg Kroah-Hartman }
15608882b394SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(device_create_vargs);
15618882b394SGreg Kroah-Hartman 
15628882b394SGreg Kroah-Hartman /**
15634e106739SGreg Kroah-Hartman  * device_create - creates a device and registers it with sysfs
15648882b394SGreg Kroah-Hartman  * @class: pointer to the struct class that this device should be registered to
15658882b394SGreg Kroah-Hartman  * @parent: pointer to the parent struct device of this new device, if any
15668882b394SGreg Kroah-Hartman  * @devt: the dev_t for the char device to be added
15678882b394SGreg Kroah-Hartman  * @drvdata: the data to be added to the device for callbacks
15688882b394SGreg Kroah-Hartman  * @fmt: string for the device's name
15698882b394SGreg Kroah-Hartman  *
15708882b394SGreg Kroah-Hartman  * This function can be used by char device classes.  A struct device
15718882b394SGreg Kroah-Hartman  * will be created in sysfs, registered to the specified class.
15728882b394SGreg Kroah-Hartman  *
15738882b394SGreg Kroah-Hartman  * A "dev" file will be created, showing the dev_t for the device, if
15748882b394SGreg Kroah-Hartman  * the dev_t is not 0,0.
15758882b394SGreg Kroah-Hartman  * If a pointer to a parent struct device is passed in, the newly created
15768882b394SGreg Kroah-Hartman  * struct device will be a child of that device in sysfs.
15778882b394SGreg Kroah-Hartman  * The pointer to the struct device will be returned from the call.
15788882b394SGreg Kroah-Hartman  * Any further sysfs files that might be required can be created using this
15798882b394SGreg Kroah-Hartman  * pointer.
15808882b394SGreg Kroah-Hartman  *
1581f0eae0edSJani Nikula  * Returns &struct device pointer on success, or ERR_PTR() on error.
1582f0eae0edSJani Nikula  *
15838882b394SGreg Kroah-Hartman  * Note: the struct class passed to this function must have previously
15848882b394SGreg Kroah-Hartman  * been created with a call to class_create().
15858882b394SGreg Kroah-Hartman  */
15864e106739SGreg Kroah-Hartman struct device *device_create(struct class *class, struct device *parent,
15874e106739SGreg Kroah-Hartman 			     dev_t devt, void *drvdata, const char *fmt, ...)
15888882b394SGreg Kroah-Hartman {
15898882b394SGreg Kroah-Hartman 	va_list vargs;
15908882b394SGreg Kroah-Hartman 	struct device *dev;
15918882b394SGreg Kroah-Hartman 
15928882b394SGreg Kroah-Hartman 	va_start(vargs, fmt);
15938882b394SGreg Kroah-Hartman 	dev = device_create_vargs(class, parent, devt, drvdata, fmt, vargs);
15948882b394SGreg Kroah-Hartman 	va_end(vargs);
15958882b394SGreg Kroah-Hartman 	return dev;
15968882b394SGreg Kroah-Hartman }
15974e106739SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(device_create);
15988882b394SGreg Kroah-Hartman 
1599cd35449bSDave Young static int __match_devt(struct device *dev, void *data)
160023681e47SGreg Kroah-Hartman {
1601cd35449bSDave Young 	dev_t *devt = data;
160223681e47SGreg Kroah-Hartman 
1603cd35449bSDave Young 	return dev->devt == *devt;
1604775b64d2SRafael J. Wysocki }
160523681e47SGreg Kroah-Hartman 
1606775b64d2SRafael J. Wysocki /**
1607775b64d2SRafael J. Wysocki  * device_destroy - removes a device that was created with device_create()
1608775b64d2SRafael J. Wysocki  * @class: pointer to the struct class that this device was registered with
1609775b64d2SRafael J. Wysocki  * @devt: the dev_t of the device that was previously registered
1610775b64d2SRafael J. Wysocki  *
1611775b64d2SRafael J. Wysocki  * This call unregisters and cleans up a device that was created with a
1612775b64d2SRafael J. Wysocki  * call to device_create().
1613775b64d2SRafael J. Wysocki  */
1614775b64d2SRafael J. Wysocki void device_destroy(struct class *class, dev_t devt)
1615775b64d2SRafael J. Wysocki {
1616775b64d2SRafael J. Wysocki 	struct device *dev;
1617775b64d2SRafael J. Wysocki 
1618695794aeSGreg Kroah-Hartman 	dev = class_find_device(class, NULL, &devt, __match_devt);
1619cd35449bSDave Young 	if (dev) {
1620cd35449bSDave Young 		put_device(dev);
162123681e47SGreg Kroah-Hartman 		device_unregister(dev);
162223681e47SGreg Kroah-Hartman 	}
1623cd35449bSDave Young }
162423681e47SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(device_destroy);
1625a2de48caSGreg Kroah-Hartman 
1626a2de48caSGreg Kroah-Hartman /**
1627a2de48caSGreg Kroah-Hartman  * device_rename - renames a device
1628a2de48caSGreg Kroah-Hartman  * @dev: the pointer to the struct device to be renamed
1629a2de48caSGreg Kroah-Hartman  * @new_name: the new name of the device
1630030c1d2bSEric W. Biederman  *
1631030c1d2bSEric W. Biederman  * It is the responsibility of the caller to provide mutual
1632030c1d2bSEric W. Biederman  * exclusion between two different calls of device_rename
1633030c1d2bSEric W. Biederman  * on the same device to ensure that new_name is valid and
1634030c1d2bSEric W. Biederman  * won't conflict with other devices.
1635c6c0ac66SMichael Ellerman  *
1636a5462516STimur Tabi  * Note: Don't call this function.  Currently, the networking layer calls this
1637a5462516STimur Tabi  * function, but that will change.  The following text from Kay Sievers offers
1638a5462516STimur Tabi  * some insight:
1639a5462516STimur Tabi  *
1640a5462516STimur Tabi  * Renaming devices is racy at many levels, symlinks and other stuff are not
1641a5462516STimur Tabi  * replaced atomically, and you get a "move" uevent, but it's not easy to
1642a5462516STimur Tabi  * connect the event to the old and new device. Device nodes are not renamed at
1643a5462516STimur Tabi  * all, there isn't even support for that in the kernel now.
1644a5462516STimur Tabi  *
1645a5462516STimur Tabi  * In the meantime, during renaming, your target name might be taken by another
1646a5462516STimur Tabi  * driver, creating conflicts. Or the old name is taken directly after you
1647a5462516STimur Tabi  * renamed it -- then you get events for the same DEVPATH, before you even see
1648a5462516STimur Tabi  * the "move" event. It's just a mess, and nothing new should ever rely on
1649a5462516STimur Tabi  * kernel device renaming. Besides that, it's not even implemented now for
1650a5462516STimur Tabi  * other things than (driver-core wise very simple) network devices.
1651a5462516STimur Tabi  *
1652a5462516STimur Tabi  * We are currently about to change network renaming in udev to completely
1653a5462516STimur Tabi  * disallow renaming of devices in the same namespace as the kernel uses,
1654a5462516STimur Tabi  * because we can't solve the problems properly, that arise with swapping names
1655a5462516STimur Tabi  * of multiple interfaces without races. Means, renaming of eth[0-9]* will only
1656a5462516STimur Tabi  * be allowed to some other name than eth[0-9]*, for the aforementioned
1657a5462516STimur Tabi  * reasons.
1658a5462516STimur Tabi  *
1659a5462516STimur Tabi  * Make up a "real" name in the driver before you register anything, or add
1660a5462516STimur Tabi  * some other attributes for userspace to find the device, or use udev to add
1661a5462516STimur Tabi  * symlinks -- but never rename kernel devices later, it's a complete mess. We
1662a5462516STimur Tabi  * don't even want to get into that and try to implement the missing pieces in
1663a5462516STimur Tabi  * the core. We really have other pieces to fix in the driver core mess. :)
1664a2de48caSGreg Kroah-Hartman  */
16656937e8f8SJohannes Berg int device_rename(struct device *dev, const char *new_name)
1666a2de48caSGreg Kroah-Hartman {
1667a2de48caSGreg Kroah-Hartman 	char *old_class_name = NULL;
1668a2de48caSGreg Kroah-Hartman 	char *new_class_name = NULL;
16692ee97cafSCornelia Huck 	char *old_device_name = NULL;
1670a2de48caSGreg Kroah-Hartman 	int error;
1671a2de48caSGreg Kroah-Hartman 
1672a2de48caSGreg Kroah-Hartman 	dev = get_device(dev);
1673a2de48caSGreg Kroah-Hartman 	if (!dev)
1674a2de48caSGreg Kroah-Hartman 		return -EINVAL;
1675a2de48caSGreg Kroah-Hartman 
16761e0b2cf9SKay Sievers 	pr_debug("device: '%s': %s: renaming to '%s'\n", dev_name(dev),
16772b3a302aSHarvey Harrison 		 __func__, new_name);
1678a2de48caSGreg Kroah-Hartman 
16791fa5ae85SKay Sievers 	old_device_name = kstrdup(dev_name(dev), GFP_KERNEL);
16802ee97cafSCornelia Huck 	if (!old_device_name) {
1681952ab431SJesper Juhl 		error = -ENOMEM;
16822ee97cafSCornelia Huck 		goto out;
1683952ab431SJesper Juhl 	}
1684a2de48caSGreg Kroah-Hartman 
1685f349cf34SEric W. Biederman 	if (dev->class) {
16866b6e39a6SKay Sievers 		error = sysfs_rename_link(&dev->class->p->subsys.kobj,
1687f349cf34SEric W. Biederman 			&dev->kobj, old_device_name, new_name);
1688f349cf34SEric W. Biederman 		if (error)
1689f349cf34SEric W. Biederman 			goto out;
1690f349cf34SEric W. Biederman 	}
169139aba963SKay Sievers 
1692a2de48caSGreg Kroah-Hartman 	error = kobject_rename(&dev->kobj, new_name);
16931fa5ae85SKay Sievers 	if (error)
16942ee97cafSCornelia Huck 		goto out;
1695a2de48caSGreg Kroah-Hartman 
16962ee97cafSCornelia Huck out:
1697a2de48caSGreg Kroah-Hartman 	put_device(dev);
1698a2de48caSGreg Kroah-Hartman 
1699a2de48caSGreg Kroah-Hartman 	kfree(new_class_name);
1700952ab431SJesper Juhl 	kfree(old_class_name);
17012ee97cafSCornelia Huck 	kfree(old_device_name);
1702a2de48caSGreg Kroah-Hartman 
1703a2de48caSGreg Kroah-Hartman 	return error;
1704a2de48caSGreg Kroah-Hartman }
1705a2807dbcSJohannes Berg EXPORT_SYMBOL_GPL(device_rename);
17068a82472fSCornelia Huck 
17078a82472fSCornelia Huck static int device_move_class_links(struct device *dev,
17088a82472fSCornelia Huck 				   struct device *old_parent,
17098a82472fSCornelia Huck 				   struct device *new_parent)
17108a82472fSCornelia Huck {
1711f7f3461dSGreg Kroah-Hartman 	int error = 0;
17128a82472fSCornelia Huck 
1713f7f3461dSGreg Kroah-Hartman 	if (old_parent)
1714f7f3461dSGreg Kroah-Hartman 		sysfs_remove_link(&dev->kobj, "device");
1715f7f3461dSGreg Kroah-Hartman 	if (new_parent)
1716f7f3461dSGreg Kroah-Hartman 		error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
1717f7f3461dSGreg Kroah-Hartman 					  "device");
1718f7f3461dSGreg Kroah-Hartman 	return error;
17198a82472fSCornelia Huck }
17208a82472fSCornelia Huck 
17218a82472fSCornelia Huck /**
17228a82472fSCornelia Huck  * device_move - moves a device to a new parent
17238a82472fSCornelia Huck  * @dev: the pointer to the struct device to be moved
1724c744aeaeSCornelia Huck  * @new_parent: the new parent of the device (can by NULL)
1725ffa6a705SCornelia Huck  * @dpm_order: how to reorder the dpm_list
17268a82472fSCornelia Huck  */
1727ffa6a705SCornelia Huck int device_move(struct device *dev, struct device *new_parent,
1728ffa6a705SCornelia Huck 		enum dpm_order dpm_order)
17298a82472fSCornelia Huck {
17308a82472fSCornelia Huck 	int error;
17318a82472fSCornelia Huck 	struct device *old_parent;
1732c744aeaeSCornelia Huck 	struct kobject *new_parent_kobj;
17338a82472fSCornelia Huck 
17348a82472fSCornelia Huck 	dev = get_device(dev);
17358a82472fSCornelia Huck 	if (!dev)
17368a82472fSCornelia Huck 		return -EINVAL;
17378a82472fSCornelia Huck 
1738ffa6a705SCornelia Huck 	device_pm_lock();
17398a82472fSCornelia Huck 	new_parent = get_device(new_parent);
1740c744aeaeSCornelia Huck 	new_parent_kobj = get_device_parent(dev, new_parent);
174163b6971aSCornelia Huck 
17421e0b2cf9SKay Sievers 	pr_debug("device: '%s': %s: moving to '%s'\n", dev_name(dev),
17431e0b2cf9SKay Sievers 		 __func__, new_parent ? dev_name(new_parent) : "<NULL>");
1744c744aeaeSCornelia Huck 	error = kobject_move(&dev->kobj, new_parent_kobj);
17458a82472fSCornelia Huck 	if (error) {
174663b6971aSCornelia Huck 		cleanup_glue_dir(dev, new_parent_kobj);
17478a82472fSCornelia Huck 		put_device(new_parent);
17488a82472fSCornelia Huck 		goto out;
17498a82472fSCornelia Huck 	}
17508a82472fSCornelia Huck 	old_parent = dev->parent;
17518a82472fSCornelia Huck 	dev->parent = new_parent;
17528a82472fSCornelia Huck 	if (old_parent)
1753f791b8c8SGreg Kroah-Hartman 		klist_remove(&dev->p->knode_parent);
17540d358f22SYinghai Lu 	if (new_parent) {
1755f791b8c8SGreg Kroah-Hartman 		klist_add_tail(&dev->p->knode_parent,
1756f791b8c8SGreg Kroah-Hartman 			       &new_parent->p->klist_children);
17570d358f22SYinghai Lu 		set_dev_node(dev, dev_to_node(new_parent));
17580d358f22SYinghai Lu 	}
17590d358f22SYinghai Lu 
1760bdd4034dSRabin Vincent 	if (dev->class) {
17618a82472fSCornelia Huck 		error = device_move_class_links(dev, old_parent, new_parent);
17628a82472fSCornelia Huck 		if (error) {
17638a82472fSCornelia Huck 			/* We ignore errors on cleanup since we're hosed anyway... */
17648a82472fSCornelia Huck 			device_move_class_links(dev, new_parent, old_parent);
17658a82472fSCornelia Huck 			if (!kobject_move(&dev->kobj, &old_parent->kobj)) {
1766c744aeaeSCornelia Huck 				if (new_parent)
1767f791b8c8SGreg Kroah-Hartman 					klist_remove(&dev->p->knode_parent);
17680d358f22SYinghai Lu 				dev->parent = old_parent;
17690d358f22SYinghai Lu 				if (old_parent) {
1770f791b8c8SGreg Kroah-Hartman 					klist_add_tail(&dev->p->knode_parent,
1771f791b8c8SGreg Kroah-Hartman 						       &old_parent->p->klist_children);
17720d358f22SYinghai Lu 					set_dev_node(dev, dev_to_node(old_parent));
17730d358f22SYinghai Lu 				}
17748a82472fSCornelia Huck 			}
177563b6971aSCornelia Huck 			cleanup_glue_dir(dev, new_parent_kobj);
17768a82472fSCornelia Huck 			put_device(new_parent);
17778a82472fSCornelia Huck 			goto out;
17788a82472fSCornelia Huck 		}
1779bdd4034dSRabin Vincent 	}
1780ffa6a705SCornelia Huck 	switch (dpm_order) {
1781ffa6a705SCornelia Huck 	case DPM_ORDER_NONE:
1782ffa6a705SCornelia Huck 		break;
1783ffa6a705SCornelia Huck 	case DPM_ORDER_DEV_AFTER_PARENT:
1784ffa6a705SCornelia Huck 		device_pm_move_after(dev, new_parent);
1785ffa6a705SCornelia Huck 		break;
1786ffa6a705SCornelia Huck 	case DPM_ORDER_PARENT_BEFORE_DEV:
1787ffa6a705SCornelia Huck 		device_pm_move_before(new_parent, dev);
1788ffa6a705SCornelia Huck 		break;
1789ffa6a705SCornelia Huck 	case DPM_ORDER_DEV_LAST:
1790ffa6a705SCornelia Huck 		device_pm_move_last(dev);
1791ffa6a705SCornelia Huck 		break;
1792ffa6a705SCornelia Huck 	}
1793bdd4034dSRabin Vincent 
17948a82472fSCornelia Huck 	put_device(old_parent);
17958a82472fSCornelia Huck out:
1796ffa6a705SCornelia Huck 	device_pm_unlock();
17978a82472fSCornelia Huck 	put_device(dev);
17988a82472fSCornelia Huck 	return error;
17998a82472fSCornelia Huck }
18008a82472fSCornelia Huck EXPORT_SYMBOL_GPL(device_move);
180137b0c020SGreg Kroah-Hartman 
180237b0c020SGreg Kroah-Hartman /**
180337b0c020SGreg Kroah-Hartman  * device_shutdown - call ->shutdown() on each device to shutdown.
180437b0c020SGreg Kroah-Hartman  */
180537b0c020SGreg Kroah-Hartman void device_shutdown(void)
180637b0c020SGreg Kroah-Hartman {
18076245838fSHugh Daschbach 	struct device *dev;
180837b0c020SGreg Kroah-Hartman 
18096245838fSHugh Daschbach 	spin_lock(&devices_kset->list_lock);
18106245838fSHugh Daschbach 	/*
18116245838fSHugh Daschbach 	 * Walk the devices list backward, shutting down each in turn.
18126245838fSHugh Daschbach 	 * Beware that device unplug events may also start pulling
18136245838fSHugh Daschbach 	 * devices offline, even as the system is shutting down.
18146245838fSHugh Daschbach 	 */
18156245838fSHugh Daschbach 	while (!list_empty(&devices_kset->list)) {
18166245838fSHugh Daschbach 		dev = list_entry(devices_kset->list.prev, struct device,
18176245838fSHugh Daschbach 				kobj.entry);
1818d1c6c030SMing Lei 
1819d1c6c030SMing Lei 		/*
1820d1c6c030SMing Lei 		 * hold reference count of device's parent to
1821d1c6c030SMing Lei 		 * prevent it from being freed because parent's
1822d1c6c030SMing Lei 		 * lock is to be held
1823d1c6c030SMing Lei 		 */
1824d1c6c030SMing Lei 		get_device(dev->parent);
18256245838fSHugh Daschbach 		get_device(dev);
18266245838fSHugh Daschbach 		/*
18276245838fSHugh Daschbach 		 * Make sure the device is off the kset list, in the
18286245838fSHugh Daschbach 		 * event that dev->*->shutdown() doesn't remove it.
18296245838fSHugh Daschbach 		 */
18306245838fSHugh Daschbach 		list_del_init(&dev->kobj.entry);
18316245838fSHugh Daschbach 		spin_unlock(&devices_kset->list_lock);
1832fe6b91f4SAlan Stern 
1833d1c6c030SMing Lei 		/* hold lock to avoid race with probe/release */
1834d1c6c030SMing Lei 		if (dev->parent)
1835d1c6c030SMing Lei 			device_lock(dev->parent);
1836d1c6c030SMing Lei 		device_lock(dev);
1837d1c6c030SMing Lei 
1838fe6b91f4SAlan Stern 		/* Don't allow any more runtime suspends */
1839fe6b91f4SAlan Stern 		pm_runtime_get_noresume(dev);
1840fe6b91f4SAlan Stern 		pm_runtime_barrier(dev);
18416245838fSHugh Daschbach 
184237b0c020SGreg Kroah-Hartman 		if (dev->bus && dev->bus->shutdown) {
184337b0c020SGreg Kroah-Hartman 			dev_dbg(dev, "shutdown\n");
184437b0c020SGreg Kroah-Hartman 			dev->bus->shutdown(dev);
184537b0c020SGreg Kroah-Hartman 		} else if (dev->driver && dev->driver->shutdown) {
184637b0c020SGreg Kroah-Hartman 			dev_dbg(dev, "shutdown\n");
184737b0c020SGreg Kroah-Hartman 			dev->driver->shutdown(dev);
184837b0c020SGreg Kroah-Hartman 		}
1849d1c6c030SMing Lei 
1850d1c6c030SMing Lei 		device_unlock(dev);
1851d1c6c030SMing Lei 		if (dev->parent)
1852d1c6c030SMing Lei 			device_unlock(dev->parent);
1853d1c6c030SMing Lei 
18546245838fSHugh Daschbach 		put_device(dev);
1855d1c6c030SMing Lei 		put_device(dev->parent);
18566245838fSHugh Daschbach 
18576245838fSHugh Daschbach 		spin_lock(&devices_kset->list_lock);
185837b0c020SGreg Kroah-Hartman 	}
18596245838fSHugh Daschbach 	spin_unlock(&devices_kset->list_lock);
1860401097eaSShaohua Li 	async_synchronize_full();
186137b0c020SGreg Kroah-Hartman }
186299bcf217SJoe Perches 
186399bcf217SJoe Perches /*
186499bcf217SJoe Perches  * Device logging functions
186599bcf217SJoe Perches  */
186699bcf217SJoe Perches 
186799bcf217SJoe Perches #ifdef CONFIG_PRINTK
1868798efc60SJoe Perches int create_syslog_header(const struct device *dev, char *hdr, size_t hdrlen)
186999bcf217SJoe Perches {
1870c4e00daaSKay Sievers 	const char *subsys;
1871798efc60SJoe Perches 	size_t pos = 0;
187299bcf217SJoe Perches 
1873c4e00daaSKay Sievers 	if (dev->class)
1874c4e00daaSKay Sievers 		subsys = dev->class->name;
1875c4e00daaSKay Sievers 	else if (dev->bus)
1876c4e00daaSKay Sievers 		subsys = dev->bus->name;
1877c4e00daaSKay Sievers 	else
1878798efc60SJoe Perches 		return 0;
1879c4e00daaSKay Sievers 
1880798efc60SJoe Perches 	pos += snprintf(hdr + pos, hdrlen - pos, "SUBSYSTEM=%s", subsys);
1881c4e00daaSKay Sievers 
1882c4e00daaSKay Sievers 	/*
1883c4e00daaSKay Sievers 	 * Add device identifier DEVICE=:
1884c4e00daaSKay Sievers 	 *   b12:8         block dev_t
1885c4e00daaSKay Sievers 	 *   c127:3        char dev_t
1886c4e00daaSKay Sievers 	 *   n8            netdev ifindex
1887c4e00daaSKay Sievers 	 *   +sound:card0  subsystem:devname
1888c4e00daaSKay Sievers 	 */
1889c4e00daaSKay Sievers 	if (MAJOR(dev->devt)) {
1890c4e00daaSKay Sievers 		char c;
1891c4e00daaSKay Sievers 
1892c4e00daaSKay Sievers 		if (strcmp(subsys, "block") == 0)
1893c4e00daaSKay Sievers 			c = 'b';
1894c4e00daaSKay Sievers 		else
1895c4e00daaSKay Sievers 			c = 'c';
1896798efc60SJoe Perches 		pos++;
1897798efc60SJoe Perches 		pos += snprintf(hdr + pos, hdrlen - pos,
1898c4e00daaSKay Sievers 				"DEVICE=%c%u:%u",
1899c4e00daaSKay Sievers 				c, MAJOR(dev->devt), MINOR(dev->devt));
1900c4e00daaSKay Sievers 	} else if (strcmp(subsys, "net") == 0) {
1901c4e00daaSKay Sievers 		struct net_device *net = to_net_dev(dev);
1902c4e00daaSKay Sievers 
1903798efc60SJoe Perches 		pos++;
1904798efc60SJoe Perches 		pos += snprintf(hdr + pos, hdrlen - pos,
1905c4e00daaSKay Sievers 				"DEVICE=n%u", net->ifindex);
1906c4e00daaSKay Sievers 	} else {
1907798efc60SJoe Perches 		pos++;
1908798efc60SJoe Perches 		pos += snprintf(hdr + pos, hdrlen - pos,
1909c4e00daaSKay Sievers 				"DEVICE=+%s:%s", subsys, dev_name(dev));
1910c4e00daaSKay Sievers 	}
1911af7f2158SJim Cromie 
1912798efc60SJoe Perches 	return pos;
191399bcf217SJoe Perches }
1914798efc60SJoe Perches EXPORT_SYMBOL(create_syslog_header);
1915798efc60SJoe Perches 
191605e4e5b8SJoe Perches int dev_vprintk_emit(int level, const struct device *dev,
191705e4e5b8SJoe Perches 		     const char *fmt, va_list args)
191805e4e5b8SJoe Perches {
191905e4e5b8SJoe Perches 	char hdr[128];
192005e4e5b8SJoe Perches 	size_t hdrlen;
192105e4e5b8SJoe Perches 
192205e4e5b8SJoe Perches 	hdrlen = create_syslog_header(dev, hdr, sizeof(hdr));
192305e4e5b8SJoe Perches 
192405e4e5b8SJoe Perches 	return vprintk_emit(0, level, hdrlen ? hdr : NULL, hdrlen, fmt, args);
192505e4e5b8SJoe Perches }
192605e4e5b8SJoe Perches EXPORT_SYMBOL(dev_vprintk_emit);
192705e4e5b8SJoe Perches 
192805e4e5b8SJoe Perches int dev_printk_emit(int level, const struct device *dev, const char *fmt, ...)
192905e4e5b8SJoe Perches {
193005e4e5b8SJoe Perches 	va_list args;
193105e4e5b8SJoe Perches 	int r;
193205e4e5b8SJoe Perches 
193305e4e5b8SJoe Perches 	va_start(args, fmt);
193405e4e5b8SJoe Perches 
193505e4e5b8SJoe Perches 	r = dev_vprintk_emit(level, dev, fmt, args);
193605e4e5b8SJoe Perches 
193705e4e5b8SJoe Perches 	va_end(args);
193805e4e5b8SJoe Perches 
193905e4e5b8SJoe Perches 	return r;
194005e4e5b8SJoe Perches }
194105e4e5b8SJoe Perches EXPORT_SYMBOL(dev_printk_emit);
194205e4e5b8SJoe Perches 
1943798efc60SJoe Perches static int __dev_printk(const char *level, const struct device *dev,
1944798efc60SJoe Perches 			struct va_format *vaf)
1945798efc60SJoe Perches {
1946798efc60SJoe Perches 	char hdr[128];
1947798efc60SJoe Perches 	size_t hdrlen;
1948798efc60SJoe Perches 
1949798efc60SJoe Perches 	if (!dev)
1950798efc60SJoe Perches 		return printk("%s(NULL device *): %pV", level, vaf);
1951798efc60SJoe Perches 
1952798efc60SJoe Perches 	hdrlen = create_syslog_header(dev, hdr, sizeof(hdr));
1953798efc60SJoe Perches 
1954798efc60SJoe Perches 	return printk_emit(0, level[1] - '0', hdrlen ? hdr : NULL, hdrlen,
1955798efc60SJoe Perches 			   "%s %s: %pV",
1956798efc60SJoe Perches 			   dev_driver_string(dev), dev_name(dev), vaf);
1957798efc60SJoe Perches }
195899bcf217SJoe Perches 
195999bcf217SJoe Perches int dev_printk(const char *level, const struct device *dev,
196099bcf217SJoe Perches 	       const char *fmt, ...)
196199bcf217SJoe Perches {
196299bcf217SJoe Perches 	struct va_format vaf;
196399bcf217SJoe Perches 	va_list args;
196499bcf217SJoe Perches 	int r;
196599bcf217SJoe Perches 
196699bcf217SJoe Perches 	va_start(args, fmt);
196799bcf217SJoe Perches 
196899bcf217SJoe Perches 	vaf.fmt = fmt;
196999bcf217SJoe Perches 	vaf.va = &args;
197099bcf217SJoe Perches 
197199bcf217SJoe Perches 	r = __dev_printk(level, dev, &vaf);
1972798efc60SJoe Perches 
197399bcf217SJoe Perches 	va_end(args);
197499bcf217SJoe Perches 
197599bcf217SJoe Perches 	return r;
197699bcf217SJoe Perches }
197799bcf217SJoe Perches EXPORT_SYMBOL(dev_printk);
197899bcf217SJoe Perches 
197999bcf217SJoe Perches #define define_dev_printk_level(func, kern_level)		\
198099bcf217SJoe Perches int func(const struct device *dev, const char *fmt, ...)	\
198199bcf217SJoe Perches {								\
198299bcf217SJoe Perches 	struct va_format vaf;					\
198399bcf217SJoe Perches 	va_list args;						\
198499bcf217SJoe Perches 	int r;							\
198599bcf217SJoe Perches 								\
198699bcf217SJoe Perches 	va_start(args, fmt);					\
198799bcf217SJoe Perches 								\
198899bcf217SJoe Perches 	vaf.fmt = fmt;						\
198999bcf217SJoe Perches 	vaf.va = &args;						\
199099bcf217SJoe Perches 								\
199199bcf217SJoe Perches 	r = __dev_printk(kern_level, dev, &vaf);		\
1992798efc60SJoe Perches 								\
199399bcf217SJoe Perches 	va_end(args);						\
199499bcf217SJoe Perches 								\
199599bcf217SJoe Perches 	return r;						\
199699bcf217SJoe Perches }								\
199799bcf217SJoe Perches EXPORT_SYMBOL(func);
199899bcf217SJoe Perches 
199999bcf217SJoe Perches define_dev_printk_level(dev_emerg, KERN_EMERG);
200099bcf217SJoe Perches define_dev_printk_level(dev_alert, KERN_ALERT);
200199bcf217SJoe Perches define_dev_printk_level(dev_crit, KERN_CRIT);
200299bcf217SJoe Perches define_dev_printk_level(dev_err, KERN_ERR);
200399bcf217SJoe Perches define_dev_printk_level(dev_warn, KERN_WARNING);
200499bcf217SJoe Perches define_dev_printk_level(dev_notice, KERN_NOTICE);
200599bcf217SJoe Perches define_dev_printk_level(_dev_info, KERN_INFO);
200699bcf217SJoe Perches 
200799bcf217SJoe Perches #endif
2008