xref: /openbmc/linux/drivers/base/core.c (revision 9169c012)
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>
281da177e4SLinus Torvalds 
291da177e4SLinus Torvalds #include "base.h"
301da177e4SLinus Torvalds #include "power/power.h"
311da177e4SLinus Torvalds 
32e52eec13SAndi Kleen #ifdef CONFIG_SYSFS_DEPRECATED
33e52eec13SAndi Kleen #ifdef CONFIG_SYSFS_DEPRECATED_V2
34e52eec13SAndi Kleen long sysfs_deprecated = 1;
35e52eec13SAndi Kleen #else
36e52eec13SAndi Kleen long sysfs_deprecated = 0;
37e52eec13SAndi Kleen #endif
38e52eec13SAndi Kleen static __init int sysfs_deprecated_setup(char *arg)
39e52eec13SAndi Kleen {
40e52eec13SAndi Kleen 	return strict_strtol(arg, 10, &sysfs_deprecated);
41e52eec13SAndi Kleen }
42e52eec13SAndi Kleen early_param("sysfs.deprecated", sysfs_deprecated_setup);
43e52eec13SAndi Kleen #endif
44e52eec13SAndi Kleen 
451da177e4SLinus Torvalds int (*platform_notify)(struct device *dev) = NULL;
461da177e4SLinus Torvalds int (*platform_notify_remove)(struct device *dev) = NULL;
47e105b8bfSDan Williams static struct kobject *dev_kobj;
48e105b8bfSDan Williams struct kobject *sysfs_dev_char_kobj;
49e105b8bfSDan Williams struct kobject *sysfs_dev_block_kobj;
501da177e4SLinus Torvalds 
514e886c29SGreg Kroah-Hartman #ifdef CONFIG_BLOCK
524e886c29SGreg Kroah-Hartman static inline int device_is_not_partition(struct device *dev)
534e886c29SGreg Kroah-Hartman {
544e886c29SGreg Kroah-Hartman 	return !(dev->type == &part_type);
554e886c29SGreg Kroah-Hartman }
564e886c29SGreg Kroah-Hartman #else
574e886c29SGreg Kroah-Hartman static inline int device_is_not_partition(struct device *dev)
584e886c29SGreg Kroah-Hartman {
594e886c29SGreg Kroah-Hartman 	return 1;
604e886c29SGreg Kroah-Hartman }
614e886c29SGreg Kroah-Hartman #endif
621da177e4SLinus Torvalds 
633e95637aSAlan Stern /**
643e95637aSAlan Stern  * dev_driver_string - Return a device's driver name, if at all possible
653e95637aSAlan Stern  * @dev: struct device to get the name of
663e95637aSAlan Stern  *
673e95637aSAlan Stern  * Will return the device's driver's name if it is bound to a device.  If
68*9169c012Syan  * the device is not bound to a driver, it will return the name of the bus
693e95637aSAlan Stern  * it is attached to.  If it is not attached to a bus either, an empty
703e95637aSAlan Stern  * string will be returned.
713e95637aSAlan Stern  */
72bf9ca69fSJean Delvare const char *dev_driver_string(const struct device *dev)
733e95637aSAlan Stern {
743589972eSAlan Stern 	struct device_driver *drv;
753589972eSAlan Stern 
763589972eSAlan Stern 	/* dev->driver can change to NULL underneath us because of unbinding,
773589972eSAlan Stern 	 * so be careful about accessing it.  dev->bus and dev->class should
783589972eSAlan Stern 	 * never change once they are set, so they don't need special care.
793589972eSAlan Stern 	 */
803589972eSAlan Stern 	drv = ACCESS_ONCE(dev->driver);
813589972eSAlan Stern 	return drv ? drv->name :
82a456b702SJean Delvare 			(dev->bus ? dev->bus->name :
83a456b702SJean Delvare 			(dev->class ? dev->class->name : ""));
843e95637aSAlan Stern }
85310a922dSMatthew Wilcox EXPORT_SYMBOL(dev_driver_string);
863e95637aSAlan Stern 
871da177e4SLinus Torvalds #define to_dev(obj) container_of(obj, struct device, kobj)
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);
941da177e4SLinus Torvalds 	struct device *dev = 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);
1101da177e4SLinus Torvalds 	struct device *dev = 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 {
1841da177e4SLinus Torvalds 	struct device *dev = to_dev(kobj);
185fb069a5dSGreg Kroah-Hartman 	struct device_private *p = dev->p;
1861da177e4SLinus Torvalds 
1871da177e4SLinus Torvalds 	if (dev->release)
1881da177e4SLinus Torvalds 		dev->release(dev);
189f9f852dfSKay Sievers 	else if (dev->type && dev->type->release)
190f9f852dfSKay Sievers 		dev->type->release(dev);
1912620efefSGreg Kroah-Hartman 	else if (dev->class && dev->class->dev_release)
1922620efefSGreg Kroah-Hartman 		dev->class->dev_release(dev);
193f810a5cfSArjan van de Ven 	else
194f810a5cfSArjan van de Ven 		WARN(1, KERN_ERR "Device '%s' does not have a release() "
1954a3ad20cSGreg Kroah-Hartman 			"function, it is broken and must be fixed.\n",
1961e0b2cf9SKay Sievers 			dev_name(dev));
197fb069a5dSGreg Kroah-Hartman 	kfree(p);
1981da177e4SLinus Torvalds }
1991da177e4SLinus Torvalds 
200bc451f20SEric W. Biederman static const void *device_namespace(struct kobject *kobj)
201bc451f20SEric W. Biederman {
202bc451f20SEric W. Biederman 	struct device *dev = to_dev(kobj);
203bc451f20SEric W. Biederman 	const void *ns = NULL;
204bc451f20SEric W. Biederman 
205bc451f20SEric W. Biederman 	if (dev->class && dev->class->ns_type)
206bc451f20SEric W. Biederman 		ns = dev->class->namespace(dev);
207bc451f20SEric W. Biederman 
208bc451f20SEric W. Biederman 	return ns;
209bc451f20SEric W. Biederman }
210bc451f20SEric W. Biederman 
2118f4afc41SGreg Kroah-Hartman static struct kobj_type device_ktype = {
2121da177e4SLinus Torvalds 	.release	= device_release,
2131da177e4SLinus Torvalds 	.sysfs_ops	= &dev_sysfs_ops,
214bc451f20SEric W. Biederman 	.namespace	= device_namespace,
2151da177e4SLinus Torvalds };
2161da177e4SLinus Torvalds 
2171da177e4SLinus Torvalds 
218312c004dSKay Sievers static int dev_uevent_filter(struct kset *kset, struct kobject *kobj)
2191da177e4SLinus Torvalds {
2201da177e4SLinus Torvalds 	struct kobj_type *ktype = get_ktype(kobj);
2211da177e4SLinus Torvalds 
2228f4afc41SGreg Kroah-Hartman 	if (ktype == &device_ktype) {
2231da177e4SLinus Torvalds 		struct device *dev = to_dev(kobj);
2241da177e4SLinus Torvalds 		if (dev->bus)
2251da177e4SLinus Torvalds 			return 1;
22623681e47SGreg Kroah-Hartman 		if (dev->class)
22723681e47SGreg Kroah-Hartman 			return 1;
2281da177e4SLinus Torvalds 	}
2291da177e4SLinus Torvalds 	return 0;
2301da177e4SLinus Torvalds }
2311da177e4SLinus Torvalds 
232312c004dSKay Sievers static const char *dev_uevent_name(struct kset *kset, struct kobject *kobj)
2331da177e4SLinus Torvalds {
2341da177e4SLinus Torvalds 	struct device *dev = to_dev(kobj);
2351da177e4SLinus Torvalds 
23623681e47SGreg Kroah-Hartman 	if (dev->bus)
2371da177e4SLinus Torvalds 		return dev->bus->name;
23823681e47SGreg Kroah-Hartman 	if (dev->class)
23923681e47SGreg Kroah-Hartman 		return dev->class->name;
24023681e47SGreg Kroah-Hartman 	return NULL;
2411da177e4SLinus Torvalds }
2421da177e4SLinus Torvalds 
2437eff2e7aSKay Sievers static int dev_uevent(struct kset *kset, struct kobject *kobj,
2447eff2e7aSKay Sievers 		      struct kobj_uevent_env *env)
2451da177e4SLinus Torvalds {
2461da177e4SLinus Torvalds 	struct device *dev = to_dev(kobj);
2471da177e4SLinus Torvalds 	int retval = 0;
2481da177e4SLinus Torvalds 
2496fcf53acSKay Sievers 	/* add device node properties if present */
25023681e47SGreg Kroah-Hartman 	if (MAJOR(dev->devt)) {
2516fcf53acSKay Sievers 		const char *tmp;
2526fcf53acSKay Sievers 		const char *name;
2532c9ede55SAl Viro 		umode_t mode = 0;
2546fcf53acSKay Sievers 
2557eff2e7aSKay Sievers 		add_uevent_var(env, "MAJOR=%u", MAJOR(dev->devt));
2567eff2e7aSKay Sievers 		add_uevent_var(env, "MINOR=%u", MINOR(dev->devt));
257e454cea2SKay Sievers 		name = device_get_devnode(dev, &mode, &tmp);
2586fcf53acSKay Sievers 		if (name) {
2596fcf53acSKay Sievers 			add_uevent_var(env, "DEVNAME=%s", name);
2606fcf53acSKay Sievers 			kfree(tmp);
261e454cea2SKay Sievers 			if (mode)
262e454cea2SKay Sievers 				add_uevent_var(env, "DEVMODE=%#o", mode & 0777);
2636fcf53acSKay Sievers 		}
26423681e47SGreg Kroah-Hartman 	}
26523681e47SGreg Kroah-Hartman 
266414264f9SKay Sievers 	if (dev->type && dev->type->name)
2677eff2e7aSKay Sievers 		add_uevent_var(env, "DEVTYPE=%s", dev->type->name);
268414264f9SKay Sievers 
269239378f1SKay Sievers 	if (dev->driver)
2707eff2e7aSKay Sievers 		add_uevent_var(env, "DRIVER=%s", dev->driver->name);
271239378f1SKay Sievers 
27207d57a32SGrant Likely 	/* Add common DT information about the device */
27307d57a32SGrant Likely 	of_device_uevent(dev, env);
27407d57a32SGrant Likely 
2751da177e4SLinus Torvalds 	/* have the bus specific function add its stuff */
2767eff2e7aSKay Sievers 	if (dev->bus && dev->bus->uevent) {
2777eff2e7aSKay Sievers 		retval = dev->bus->uevent(dev, env);
278f9f852dfSKay Sievers 		if (retval)
2797dc72b28SGreg Kroah-Hartman 			pr_debug("device: '%s': %s: bus uevent() returned %d\n",
2801e0b2cf9SKay Sievers 				 dev_name(dev), __func__, retval);
2811da177e4SLinus Torvalds 	}
2821da177e4SLinus Torvalds 
2832620efefSGreg Kroah-Hartman 	/* have the class specific function add its stuff */
2847eff2e7aSKay Sievers 	if (dev->class && dev->class->dev_uevent) {
2857eff2e7aSKay Sievers 		retval = dev->class->dev_uevent(dev, env);
286f9f852dfSKay Sievers 		if (retval)
2877dc72b28SGreg Kroah-Hartman 			pr_debug("device: '%s': %s: class uevent() "
2881e0b2cf9SKay Sievers 				 "returned %d\n", dev_name(dev),
2892b3a302aSHarvey Harrison 				 __func__, retval);
2902620efefSGreg Kroah-Hartman 	}
291f9f852dfSKay Sievers 
292eef35c2dSStefan Weil 	/* have the device type specific function add its stuff */
2937eff2e7aSKay Sievers 	if (dev->type && dev->type->uevent) {
2947eff2e7aSKay Sievers 		retval = dev->type->uevent(dev, env);
295f9f852dfSKay Sievers 		if (retval)
2967dc72b28SGreg Kroah-Hartman 			pr_debug("device: '%s': %s: dev_type uevent() "
2971e0b2cf9SKay Sievers 				 "returned %d\n", dev_name(dev),
2982b3a302aSHarvey Harrison 				 __func__, retval);
2992620efefSGreg Kroah-Hartman 	}
3002620efefSGreg Kroah-Hartman 
3011da177e4SLinus Torvalds 	return retval;
3021da177e4SLinus Torvalds }
3031da177e4SLinus Torvalds 
3049cd43611SEmese Revfy static const struct kset_uevent_ops device_uevent_ops = {
305312c004dSKay Sievers 	.filter =	dev_uevent_filter,
306312c004dSKay Sievers 	.name =		dev_uevent_name,
307312c004dSKay Sievers 	.uevent =	dev_uevent,
3081da177e4SLinus Torvalds };
3091da177e4SLinus Torvalds 
31016574dccSKay Sievers static ssize_t show_uevent(struct device *dev, struct device_attribute *attr,
31116574dccSKay Sievers 			   char *buf)
31216574dccSKay Sievers {
31316574dccSKay Sievers 	struct kobject *top_kobj;
31416574dccSKay Sievers 	struct kset *kset;
3157eff2e7aSKay Sievers 	struct kobj_uevent_env *env = NULL;
31616574dccSKay Sievers 	int i;
31716574dccSKay Sievers 	size_t count = 0;
31816574dccSKay Sievers 	int retval;
31916574dccSKay Sievers 
32016574dccSKay Sievers 	/* search the kset, the device belongs to */
32116574dccSKay Sievers 	top_kobj = &dev->kobj;
3225c5daf65SKay Sievers 	while (!top_kobj->kset && top_kobj->parent)
32316574dccSKay Sievers 		top_kobj = top_kobj->parent;
32416574dccSKay Sievers 	if (!top_kobj->kset)
32516574dccSKay Sievers 		goto out;
3265c5daf65SKay Sievers 
32716574dccSKay Sievers 	kset = top_kobj->kset;
32816574dccSKay Sievers 	if (!kset->uevent_ops || !kset->uevent_ops->uevent)
32916574dccSKay Sievers 		goto out;
33016574dccSKay Sievers 
33116574dccSKay Sievers 	/* respect filter */
33216574dccSKay Sievers 	if (kset->uevent_ops && kset->uevent_ops->filter)
33316574dccSKay Sievers 		if (!kset->uevent_ops->filter(kset, &dev->kobj))
33416574dccSKay Sievers 			goto out;
33516574dccSKay Sievers 
3367eff2e7aSKay Sievers 	env = kzalloc(sizeof(struct kobj_uevent_env), GFP_KERNEL);
3377eff2e7aSKay Sievers 	if (!env)
338c7308c81SGreg Kroah-Hartman 		return -ENOMEM;
339c7308c81SGreg Kroah-Hartman 
34016574dccSKay Sievers 	/* let the kset specific function add its keys */
3417eff2e7aSKay Sievers 	retval = kset->uevent_ops->uevent(kset, &dev->kobj, env);
34216574dccSKay Sievers 	if (retval)
34316574dccSKay Sievers 		goto out;
34416574dccSKay Sievers 
34516574dccSKay Sievers 	/* copy keys to file */
3467eff2e7aSKay Sievers 	for (i = 0; i < env->envp_idx; i++)
3477eff2e7aSKay Sievers 		count += sprintf(&buf[count], "%s\n", env->envp[i]);
34816574dccSKay Sievers out:
3497eff2e7aSKay Sievers 	kfree(env);
35016574dccSKay Sievers 	return count;
35116574dccSKay Sievers }
35216574dccSKay Sievers 
353a7fd6706SKay Sievers static ssize_t store_uevent(struct device *dev, struct device_attribute *attr,
354a7fd6706SKay Sievers 			    const char *buf, size_t count)
355a7fd6706SKay Sievers {
35660a96a59SKay Sievers 	enum kobject_action action;
35760a96a59SKay Sievers 
3583f5468c9SKay Sievers 	if (kobject_action_type(buf, count, &action) == 0)
35960a96a59SKay Sievers 		kobject_uevent(&dev->kobj, action);
3603f5468c9SKay Sievers 	else
3613f5468c9SKay Sievers 		dev_err(dev, "uevent: unknown action-string\n");
362a7fd6706SKay Sievers 	return count;
363a7fd6706SKay Sievers }
364a7fd6706SKay Sievers 
365ad6a1e1cSTejun Heo static struct device_attribute uevent_attr =
366ad6a1e1cSTejun Heo 	__ATTR(uevent, S_IRUGO | S_IWUSR, show_uevent, store_uevent);
367ad6a1e1cSTejun Heo 
368621a1672SDmitry Torokhov static int device_add_attributes(struct device *dev,
369621a1672SDmitry Torokhov 				 struct device_attribute *attrs)
370de0ff00dSGreg Kroah-Hartman {
371de0ff00dSGreg Kroah-Hartman 	int error = 0;
372621a1672SDmitry Torokhov 	int i;
373de0ff00dSGreg Kroah-Hartman 
374621a1672SDmitry Torokhov 	if (attrs) {
375621a1672SDmitry Torokhov 		for (i = 0; attr_name(attrs[i]); i++) {
376621a1672SDmitry Torokhov 			error = device_create_file(dev, &attrs[i]);
377621a1672SDmitry Torokhov 			if (error)
378621a1672SDmitry Torokhov 				break;
379621a1672SDmitry Torokhov 		}
380621a1672SDmitry Torokhov 		if (error)
381de0ff00dSGreg Kroah-Hartman 			while (--i >= 0)
382621a1672SDmitry Torokhov 				device_remove_file(dev, &attrs[i]);
383de0ff00dSGreg Kroah-Hartman 	}
384de0ff00dSGreg Kroah-Hartman 	return error;
385de0ff00dSGreg Kroah-Hartman }
386de0ff00dSGreg Kroah-Hartman 
387621a1672SDmitry Torokhov static void device_remove_attributes(struct device *dev,
388621a1672SDmitry Torokhov 				     struct device_attribute *attrs)
389de0ff00dSGreg Kroah-Hartman {
390de0ff00dSGreg Kroah-Hartman 	int i;
391621a1672SDmitry Torokhov 
392621a1672SDmitry Torokhov 	if (attrs)
393621a1672SDmitry Torokhov 		for (i = 0; attr_name(attrs[i]); i++)
394621a1672SDmitry Torokhov 			device_remove_file(dev, &attrs[i]);
395621a1672SDmitry Torokhov }
396621a1672SDmitry Torokhov 
397c97415a7SStefan Achatz static int device_add_bin_attributes(struct device *dev,
398c97415a7SStefan Achatz 				     struct bin_attribute *attrs)
399c97415a7SStefan Achatz {
400c97415a7SStefan Achatz 	int error = 0;
401c97415a7SStefan Achatz 	int i;
402c97415a7SStefan Achatz 
403c97415a7SStefan Achatz 	if (attrs) {
404c97415a7SStefan Achatz 		for (i = 0; attr_name(attrs[i]); i++) {
405c97415a7SStefan Achatz 			error = device_create_bin_file(dev, &attrs[i]);
406c97415a7SStefan Achatz 			if (error)
407c97415a7SStefan Achatz 				break;
408c97415a7SStefan Achatz 		}
409c97415a7SStefan Achatz 		if (error)
410c97415a7SStefan Achatz 			while (--i >= 0)
411c97415a7SStefan Achatz 				device_remove_bin_file(dev, &attrs[i]);
412c97415a7SStefan Achatz 	}
413c97415a7SStefan Achatz 	return error;
414c97415a7SStefan Achatz }
415c97415a7SStefan Achatz 
416c97415a7SStefan Achatz static void device_remove_bin_attributes(struct device *dev,
417c97415a7SStefan Achatz 					 struct bin_attribute *attrs)
418c97415a7SStefan Achatz {
419c97415a7SStefan Achatz 	int i;
420c97415a7SStefan Achatz 
421c97415a7SStefan Achatz 	if (attrs)
422c97415a7SStefan Achatz 		for (i = 0; attr_name(attrs[i]); i++)
423c97415a7SStefan Achatz 			device_remove_bin_file(dev, &attrs[i]);
424c97415a7SStefan Achatz }
425c97415a7SStefan Achatz 
426621a1672SDmitry Torokhov static int device_add_groups(struct device *dev,
427a4dbd674SDavid Brownell 			     const struct attribute_group **groups)
428621a1672SDmitry Torokhov {
429621a1672SDmitry Torokhov 	int error = 0;
430621a1672SDmitry Torokhov 	int i;
431621a1672SDmitry Torokhov 
432621a1672SDmitry Torokhov 	if (groups) {
433621a1672SDmitry Torokhov 		for (i = 0; groups[i]; i++) {
434621a1672SDmitry Torokhov 			error = sysfs_create_group(&dev->kobj, groups[i]);
435621a1672SDmitry Torokhov 			if (error) {
436621a1672SDmitry Torokhov 				while (--i >= 0)
4374a3ad20cSGreg Kroah-Hartman 					sysfs_remove_group(&dev->kobj,
4384a3ad20cSGreg Kroah-Hartman 							   groups[i]);
439621a1672SDmitry Torokhov 				break;
440de0ff00dSGreg Kroah-Hartman 			}
441de0ff00dSGreg Kroah-Hartman 		}
442de0ff00dSGreg Kroah-Hartman 	}
443621a1672SDmitry Torokhov 	return error;
444621a1672SDmitry Torokhov }
445621a1672SDmitry Torokhov 
446621a1672SDmitry Torokhov static void device_remove_groups(struct device *dev,
447a4dbd674SDavid Brownell 				 const struct attribute_group **groups)
448621a1672SDmitry Torokhov {
449621a1672SDmitry Torokhov 	int i;
450621a1672SDmitry Torokhov 
451621a1672SDmitry Torokhov 	if (groups)
452621a1672SDmitry Torokhov 		for (i = 0; groups[i]; i++)
453621a1672SDmitry Torokhov 			sysfs_remove_group(&dev->kobj, groups[i]);
454621a1672SDmitry Torokhov }
455de0ff00dSGreg Kroah-Hartman 
4562620efefSGreg Kroah-Hartman static int device_add_attrs(struct device *dev)
4572620efefSGreg Kroah-Hartman {
4582620efefSGreg Kroah-Hartman 	struct class *class = dev->class;
459aed65af1SStephen Hemminger 	const struct device_type *type = dev->type;
460621a1672SDmitry Torokhov 	int error;
4612620efefSGreg Kroah-Hartman 
462621a1672SDmitry Torokhov 	if (class) {
463621a1672SDmitry Torokhov 		error = device_add_attributes(dev, class->dev_attrs);
4642620efefSGreg Kroah-Hartman 		if (error)
465621a1672SDmitry Torokhov 			return error;
466c97415a7SStefan Achatz 		error = device_add_bin_attributes(dev, class->dev_bin_attrs);
467c97415a7SStefan Achatz 		if (error)
468c97415a7SStefan Achatz 			goto err_remove_class_attrs;
469f9f852dfSKay Sievers 	}
470f9f852dfSKay Sievers 
471621a1672SDmitry Torokhov 	if (type) {
472621a1672SDmitry Torokhov 		error = device_add_groups(dev, type->groups);
473f9f852dfSKay Sievers 		if (error)
474c97415a7SStefan Achatz 			goto err_remove_class_bin_attrs;
475f9f852dfSKay Sievers 	}
476621a1672SDmitry Torokhov 
477621a1672SDmitry Torokhov 	error = device_add_groups(dev, dev->groups);
478f9f852dfSKay Sievers 	if (error)
479621a1672SDmitry Torokhov 		goto err_remove_type_groups;
480621a1672SDmitry Torokhov 
481621a1672SDmitry Torokhov 	return 0;
482621a1672SDmitry Torokhov 
483621a1672SDmitry Torokhov  err_remove_type_groups:
484621a1672SDmitry Torokhov 	if (type)
485621a1672SDmitry Torokhov 		device_remove_groups(dev, type->groups);
486c97415a7SStefan Achatz  err_remove_class_bin_attrs:
487c97415a7SStefan Achatz 	if (class)
488c97415a7SStefan Achatz 		device_remove_bin_attributes(dev, class->dev_bin_attrs);
489621a1672SDmitry Torokhov  err_remove_class_attrs:
490621a1672SDmitry Torokhov 	if (class)
491621a1672SDmitry Torokhov 		device_remove_attributes(dev, class->dev_attrs);
492f9f852dfSKay Sievers 
4932620efefSGreg Kroah-Hartman 	return error;
4942620efefSGreg Kroah-Hartman }
4952620efefSGreg Kroah-Hartman 
4962620efefSGreg Kroah-Hartman static void device_remove_attrs(struct device *dev)
4972620efefSGreg Kroah-Hartman {
4982620efefSGreg Kroah-Hartman 	struct class *class = dev->class;
499aed65af1SStephen Hemminger 	const struct device_type *type = dev->type;
5002620efefSGreg Kroah-Hartman 
501621a1672SDmitry Torokhov 	device_remove_groups(dev, dev->groups);
502f9f852dfSKay Sievers 
503621a1672SDmitry Torokhov 	if (type)
504621a1672SDmitry Torokhov 		device_remove_groups(dev, type->groups);
505621a1672SDmitry Torokhov 
506c97415a7SStefan Achatz 	if (class) {
507621a1672SDmitry Torokhov 		device_remove_attributes(dev, class->dev_attrs);
508c97415a7SStefan Achatz 		device_remove_bin_attributes(dev, class->dev_bin_attrs);
509c97415a7SStefan Achatz 	}
5102620efefSGreg Kroah-Hartman }
5112620efefSGreg Kroah-Hartman 
5122620efefSGreg Kroah-Hartman 
51323681e47SGreg Kroah-Hartman static ssize_t show_dev(struct device *dev, struct device_attribute *attr,
51423681e47SGreg Kroah-Hartman 			char *buf)
51523681e47SGreg Kroah-Hartman {
51623681e47SGreg Kroah-Hartman 	return print_dev_t(buf, dev->devt);
51723681e47SGreg Kroah-Hartman }
51823681e47SGreg Kroah-Hartman 
519ad6a1e1cSTejun Heo static struct device_attribute devt_attr =
520ad6a1e1cSTejun Heo 	__ATTR(dev, S_IRUGO, show_dev, NULL);
521ad6a1e1cSTejun Heo 
522ca22e56dSKay Sievers /* /sys/devices/ */
523881c6cfdSGreg Kroah-Hartman struct kset *devices_kset;
5241da177e4SLinus Torvalds 
5251da177e4SLinus Torvalds /**
5261da177e4SLinus Torvalds  * device_create_file - create sysfs attribute file for device.
5271da177e4SLinus Torvalds  * @dev: device.
5281da177e4SLinus Torvalds  * @attr: device attribute descriptor.
5291da177e4SLinus Torvalds  */
53026579ab7SPhil Carmody int device_create_file(struct device *dev,
53126579ab7SPhil Carmody 		       const struct device_attribute *attr)
5321da177e4SLinus Torvalds {
5331da177e4SLinus Torvalds 	int error = 0;
5340c98b19fSCornelia Huck 	if (dev)
5351da177e4SLinus Torvalds 		error = sysfs_create_file(&dev->kobj, &attr->attr);
5361da177e4SLinus Torvalds 	return error;
5371da177e4SLinus Torvalds }
5381da177e4SLinus Torvalds 
5391da177e4SLinus Torvalds /**
5401da177e4SLinus Torvalds  * device_remove_file - remove sysfs attribute file.
5411da177e4SLinus Torvalds  * @dev: device.
5421da177e4SLinus Torvalds  * @attr: device attribute descriptor.
5431da177e4SLinus Torvalds  */
54426579ab7SPhil Carmody void device_remove_file(struct device *dev,
54526579ab7SPhil Carmody 			const struct device_attribute *attr)
5461da177e4SLinus Torvalds {
5470c98b19fSCornelia Huck 	if (dev)
5481da177e4SLinus Torvalds 		sysfs_remove_file(&dev->kobj, &attr->attr);
5491da177e4SLinus Torvalds }
5501da177e4SLinus Torvalds 
5512589f188SGreg Kroah-Hartman /**
5522589f188SGreg Kroah-Hartman  * device_create_bin_file - create sysfs binary attribute file for device.
5532589f188SGreg Kroah-Hartman  * @dev: device.
5542589f188SGreg Kroah-Hartman  * @attr: device binary attribute descriptor.
5552589f188SGreg Kroah-Hartman  */
55666ecb92bSPhil Carmody int device_create_bin_file(struct device *dev,
55766ecb92bSPhil Carmody 			   const struct bin_attribute *attr)
5582589f188SGreg Kroah-Hartman {
5592589f188SGreg Kroah-Hartman 	int error = -EINVAL;
5602589f188SGreg Kroah-Hartman 	if (dev)
5612589f188SGreg Kroah-Hartman 		error = sysfs_create_bin_file(&dev->kobj, attr);
5622589f188SGreg Kroah-Hartman 	return error;
5632589f188SGreg Kroah-Hartman }
5642589f188SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(device_create_bin_file);
5652589f188SGreg Kroah-Hartman 
5662589f188SGreg Kroah-Hartman /**
5672589f188SGreg Kroah-Hartman  * device_remove_bin_file - remove sysfs binary attribute file
5682589f188SGreg Kroah-Hartman  * @dev: device.
5692589f188SGreg Kroah-Hartman  * @attr: device binary attribute descriptor.
5702589f188SGreg Kroah-Hartman  */
57166ecb92bSPhil Carmody void device_remove_bin_file(struct device *dev,
57266ecb92bSPhil Carmody 			    const struct bin_attribute *attr)
5732589f188SGreg Kroah-Hartman {
5742589f188SGreg Kroah-Hartman 	if (dev)
5752589f188SGreg Kroah-Hartman 		sysfs_remove_bin_file(&dev->kobj, attr);
5762589f188SGreg Kroah-Hartman }
5772589f188SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(device_remove_bin_file);
5782589f188SGreg Kroah-Hartman 
579d9a9cdfbSAlan Stern /**
580523ded71SAlan Stern  * device_schedule_callback_owner - helper to schedule a callback for a device
581d9a9cdfbSAlan Stern  * @dev: device.
582d9a9cdfbSAlan Stern  * @func: callback function to invoke later.
583523ded71SAlan Stern  * @owner: module owning the callback routine
584d9a9cdfbSAlan Stern  *
585d9a9cdfbSAlan Stern  * Attribute methods must not unregister themselves or their parent device
586d9a9cdfbSAlan Stern  * (which would amount to the same thing).  Attempts to do so will deadlock,
587d9a9cdfbSAlan Stern  * since unregistration is mutually exclusive with driver callbacks.
588d9a9cdfbSAlan Stern  *
589d9a9cdfbSAlan Stern  * Instead methods can call this routine, which will attempt to allocate
590d9a9cdfbSAlan Stern  * and schedule a workqueue request to call back @func with @dev as its
591d9a9cdfbSAlan Stern  * argument in the workqueue's process context.  @dev will be pinned until
592d9a9cdfbSAlan Stern  * @func returns.
593d9a9cdfbSAlan Stern  *
594523ded71SAlan Stern  * This routine is usually called via the inline device_schedule_callback(),
595523ded71SAlan Stern  * which automatically sets @owner to THIS_MODULE.
596523ded71SAlan Stern  *
597d9a9cdfbSAlan Stern  * Returns 0 if the request was submitted, -ENOMEM if storage could not
598523ded71SAlan Stern  * be allocated, -ENODEV if a reference to @owner isn't available.
599d9a9cdfbSAlan Stern  *
600d9a9cdfbSAlan Stern  * NOTE: This routine won't work if CONFIG_SYSFS isn't set!  It uses an
601d9a9cdfbSAlan Stern  * underlying sysfs routine (since it is intended for use by attribute
602d9a9cdfbSAlan Stern  * methods), and if sysfs isn't available you'll get nothing but -ENOSYS.
603d9a9cdfbSAlan Stern  */
604523ded71SAlan Stern int device_schedule_callback_owner(struct device *dev,
605523ded71SAlan Stern 		void (*func)(struct device *), struct module *owner)
606d9a9cdfbSAlan Stern {
607d9a9cdfbSAlan Stern 	return sysfs_schedule_callback(&dev->kobj,
608523ded71SAlan Stern 			(void (*)(void *)) func, dev, owner);
609d9a9cdfbSAlan Stern }
610523ded71SAlan Stern EXPORT_SYMBOL_GPL(device_schedule_callback_owner);
611d9a9cdfbSAlan Stern 
61234bb61f9SJames Bottomley static void klist_children_get(struct klist_node *n)
61334bb61f9SJames Bottomley {
614f791b8c8SGreg Kroah-Hartman 	struct device_private *p = to_device_private_parent(n);
615f791b8c8SGreg Kroah-Hartman 	struct device *dev = p->device;
61634bb61f9SJames Bottomley 
61734bb61f9SJames Bottomley 	get_device(dev);
61834bb61f9SJames Bottomley }
61934bb61f9SJames Bottomley 
62034bb61f9SJames Bottomley static void klist_children_put(struct klist_node *n)
62134bb61f9SJames Bottomley {
622f791b8c8SGreg Kroah-Hartman 	struct device_private *p = to_device_private_parent(n);
623f791b8c8SGreg Kroah-Hartman 	struct device *dev = p->device;
62434bb61f9SJames Bottomley 
62534bb61f9SJames Bottomley 	put_device(dev);
62634bb61f9SJames Bottomley }
62734bb61f9SJames Bottomley 
6281da177e4SLinus Torvalds /**
6291da177e4SLinus Torvalds  * device_initialize - init device structure.
6301da177e4SLinus Torvalds  * @dev: device.
6311da177e4SLinus Torvalds  *
6325739411aSCornelia Huck  * This prepares the device for use by other layers by initializing
6335739411aSCornelia Huck  * its fields.
6341da177e4SLinus Torvalds  * It is the first half of device_register(), if called by
6355739411aSCornelia Huck  * that function, though it can also be called separately, so one
6365739411aSCornelia Huck  * may use @dev's fields. In particular, get_device()/put_device()
6375739411aSCornelia Huck  * may be used for reference counting of @dev after calling this
6385739411aSCornelia Huck  * function.
6395739411aSCornelia Huck  *
640b10d5efdSAlan Stern  * All fields in @dev must be initialized by the caller to 0, except
641b10d5efdSAlan Stern  * for those explicitly set to some other value.  The simplest
642b10d5efdSAlan Stern  * approach is to use kzalloc() to allocate the structure containing
643b10d5efdSAlan Stern  * @dev.
644b10d5efdSAlan Stern  *
6455739411aSCornelia Huck  * NOTE: Use put_device() to give up your reference instead of freeing
6465739411aSCornelia Huck  * @dev directly once you have called this function.
6471da177e4SLinus Torvalds  */
6481da177e4SLinus Torvalds void device_initialize(struct device *dev)
6491da177e4SLinus Torvalds {
650881c6cfdSGreg Kroah-Hartman 	dev->kobj.kset = devices_kset;
651f9cb074bSGreg Kroah-Hartman 	kobject_init(&dev->kobj, &device_ktype);
6521da177e4SLinus Torvalds 	INIT_LIST_HEAD(&dev->dma_pools);
6533142788bSThomas Gleixner 	mutex_init(&dev->mutex);
6541704f47bSPeter Zijlstra 	lockdep_set_novalidate_class(&dev->mutex);
6559ac7849eSTejun Heo 	spin_lock_init(&dev->devres_lock);
6569ac7849eSTejun Heo 	INIT_LIST_HEAD(&dev->devres_head);
6573b98aeafSAlan Stern 	device_pm_init(dev);
65887348136SChristoph Hellwig 	set_dev_node(dev, -1);
6591da177e4SLinus Torvalds }
6601da177e4SLinus Torvalds 
661c744aeaeSCornelia Huck static struct kobject *virtual_device_parent(struct device *dev)
662f0ee61a6SGreg Kroah-Hartman {
663f0ee61a6SGreg Kroah-Hartman 	static struct kobject *virtual_dir = NULL;
664f0ee61a6SGreg Kroah-Hartman 
665f0ee61a6SGreg Kroah-Hartman 	if (!virtual_dir)
6664ff6abffSGreg Kroah-Hartman 		virtual_dir = kobject_create_and_add("virtual",
667881c6cfdSGreg Kroah-Hartman 						     &devices_kset->kobj);
668f0ee61a6SGreg Kroah-Hartman 
66986406245SKay Sievers 	return virtual_dir;
670f0ee61a6SGreg Kroah-Hartman }
671f0ee61a6SGreg Kroah-Hartman 
672bc451f20SEric W. Biederman struct class_dir {
673bc451f20SEric W. Biederman 	struct kobject kobj;
674bc451f20SEric W. Biederman 	struct class *class;
675bc451f20SEric W. Biederman };
676bc451f20SEric W. Biederman 
677bc451f20SEric W. Biederman #define to_class_dir(obj) container_of(obj, struct class_dir, kobj)
678bc451f20SEric W. Biederman 
679bc451f20SEric W. Biederman static void class_dir_release(struct kobject *kobj)
680bc451f20SEric W. Biederman {
681bc451f20SEric W. Biederman 	struct class_dir *dir = to_class_dir(kobj);
682bc451f20SEric W. Biederman 	kfree(dir);
683bc451f20SEric W. Biederman }
684bc451f20SEric W. Biederman 
685bc451f20SEric W. Biederman static const
686bc451f20SEric W. Biederman struct kobj_ns_type_operations *class_dir_child_ns_type(struct kobject *kobj)
687bc451f20SEric W. Biederman {
688bc451f20SEric W. Biederman 	struct class_dir *dir = to_class_dir(kobj);
689bc451f20SEric W. Biederman 	return dir->class->ns_type;
690bc451f20SEric W. Biederman }
691bc451f20SEric W. Biederman 
692bc451f20SEric W. Biederman static struct kobj_type class_dir_ktype = {
693bc451f20SEric W. Biederman 	.release	= class_dir_release,
694bc451f20SEric W. Biederman 	.sysfs_ops	= &kobj_sysfs_ops,
695bc451f20SEric W. Biederman 	.child_ns_type	= class_dir_child_ns_type
696bc451f20SEric W. Biederman };
697bc451f20SEric W. Biederman 
698bc451f20SEric W. Biederman static struct kobject *
699bc451f20SEric W. Biederman class_dir_create_and_add(struct class *class, struct kobject *parent_kobj)
700bc451f20SEric W. Biederman {
701bc451f20SEric W. Biederman 	struct class_dir *dir;
702bc451f20SEric W. Biederman 	int retval;
703bc451f20SEric W. Biederman 
704bc451f20SEric W. Biederman 	dir = kzalloc(sizeof(*dir), GFP_KERNEL);
705bc451f20SEric W. Biederman 	if (!dir)
706bc451f20SEric W. Biederman 		return NULL;
707bc451f20SEric W. Biederman 
708bc451f20SEric W. Biederman 	dir->class = class;
709bc451f20SEric W. Biederman 	kobject_init(&dir->kobj, &class_dir_ktype);
710bc451f20SEric W. Biederman 
7116b6e39a6SKay Sievers 	dir->kobj.kset = &class->p->glue_dirs;
712bc451f20SEric W. Biederman 
713bc451f20SEric W. Biederman 	retval = kobject_add(&dir->kobj, parent_kobj, "%s", class->name);
714bc451f20SEric W. Biederman 	if (retval < 0) {
715bc451f20SEric W. Biederman 		kobject_put(&dir->kobj);
716bc451f20SEric W. Biederman 		return NULL;
717bc451f20SEric W. Biederman 	}
718bc451f20SEric W. Biederman 	return &dir->kobj;
719bc451f20SEric W. Biederman }
720bc451f20SEric W. Biederman 
721bc451f20SEric W. Biederman 
722c744aeaeSCornelia Huck static struct kobject *get_device_parent(struct device *dev,
723c744aeaeSCornelia Huck 					 struct device *parent)
72440fa5422SGreg Kroah-Hartman {
72586406245SKay Sievers 	if (dev->class) {
72677d3d7c1STejun Heo 		static DEFINE_MUTEX(gdp_mutex);
72786406245SKay Sievers 		struct kobject *kobj = NULL;
72886406245SKay Sievers 		struct kobject *parent_kobj;
72986406245SKay Sievers 		struct kobject *k;
73086406245SKay Sievers 
731ead454feSRandy Dunlap #ifdef CONFIG_BLOCK
73239aba963SKay Sievers 		/* block disks show up in /sys/block */
733e52eec13SAndi Kleen 		if (sysfs_deprecated && dev->class == &block_class) {
73439aba963SKay Sievers 			if (parent && parent->class == &block_class)
73539aba963SKay Sievers 				return &parent->kobj;
7366b6e39a6SKay Sievers 			return &block_class.p->subsys.kobj;
73739aba963SKay Sievers 		}
738ead454feSRandy Dunlap #endif
739e52eec13SAndi Kleen 
74086406245SKay Sievers 		/*
74186406245SKay Sievers 		 * If we have no parent, we live in "virtual".
7420f4dafc0SKay Sievers 		 * Class-devices with a non class-device as parent, live
7430f4dafc0SKay Sievers 		 * in a "glue" directory to prevent namespace collisions.
74486406245SKay Sievers 		 */
74586406245SKay Sievers 		if (parent == NULL)
74686406245SKay Sievers 			parent_kobj = virtual_device_parent(dev);
74724b1442dSEric W. Biederman 		else if (parent->class && !dev->class->ns_type)
74886406245SKay Sievers 			return &parent->kobj;
74986406245SKay Sievers 		else
75086406245SKay Sievers 			parent_kobj = &parent->kobj;
75186406245SKay Sievers 
75277d3d7c1STejun Heo 		mutex_lock(&gdp_mutex);
75377d3d7c1STejun Heo 
75486406245SKay Sievers 		/* find our class-directory at the parent and reference it */
7556b6e39a6SKay Sievers 		spin_lock(&dev->class->p->glue_dirs.list_lock);
7566b6e39a6SKay Sievers 		list_for_each_entry(k, &dev->class->p->glue_dirs.list, entry)
75786406245SKay Sievers 			if (k->parent == parent_kobj) {
75886406245SKay Sievers 				kobj = kobject_get(k);
75986406245SKay Sievers 				break;
76086406245SKay Sievers 			}
7616b6e39a6SKay Sievers 		spin_unlock(&dev->class->p->glue_dirs.list_lock);
76277d3d7c1STejun Heo 		if (kobj) {
76377d3d7c1STejun Heo 			mutex_unlock(&gdp_mutex);
76486406245SKay Sievers 			return kobj;
76577d3d7c1STejun Heo 		}
76686406245SKay Sievers 
76786406245SKay Sievers 		/* or create a new class-directory at the parent device */
768bc451f20SEric W. Biederman 		k = class_dir_create_and_add(dev->class, parent_kobj);
7690f4dafc0SKay Sievers 		/* do not emit an uevent for this simple "glue" directory */
77077d3d7c1STejun Heo 		mutex_unlock(&gdp_mutex);
77143968d2fSGreg Kroah-Hartman 		return k;
77286406245SKay Sievers 	}
77386406245SKay Sievers 
774ca22e56dSKay Sievers 	/* subsystems can specify a default root directory for their devices */
775ca22e56dSKay Sievers 	if (!parent && dev->bus && dev->bus->dev_root)
776ca22e56dSKay Sievers 		return &dev->bus->dev_root->kobj;
777ca22e56dSKay Sievers 
77886406245SKay Sievers 	if (parent)
779c744aeaeSCornelia Huck 		return &parent->kobj;
780c744aeaeSCornelia Huck 	return NULL;
781c744aeaeSCornelia Huck }
782da231fd5SKay Sievers 
78363b6971aSCornelia Huck static void cleanup_glue_dir(struct device *dev, struct kobject *glue_dir)
784da231fd5SKay Sievers {
7850f4dafc0SKay Sievers 	/* see if we live in a "glue" directory */
786c1fe539aSCornelia Huck 	if (!glue_dir || !dev->class ||
7876b6e39a6SKay Sievers 	    glue_dir->kset != &dev->class->p->glue_dirs)
788da231fd5SKay Sievers 		return;
789da231fd5SKay Sievers 
7900f4dafc0SKay Sievers 	kobject_put(glue_dir);
791da231fd5SKay Sievers }
79263b6971aSCornelia Huck 
79363b6971aSCornelia Huck static void cleanup_device_parent(struct device *dev)
79463b6971aSCornelia Huck {
79563b6971aSCornelia Huck 	cleanup_glue_dir(dev, dev->kobj.parent);
79663b6971aSCornelia Huck }
79786406245SKay Sievers 
7982ee97cafSCornelia Huck static int device_add_class_symlinks(struct device *dev)
7992ee97cafSCornelia Huck {
8002ee97cafSCornelia Huck 	int error;
8012ee97cafSCornelia Huck 
8022ee97cafSCornelia Huck 	if (!dev->class)
8032ee97cafSCornelia Huck 		return 0;
804da231fd5SKay Sievers 
8051fbfee6cSGreg Kroah-Hartman 	error = sysfs_create_link(&dev->kobj,
8066b6e39a6SKay Sievers 				  &dev->class->p->subsys.kobj,
8072ee97cafSCornelia Huck 				  "subsystem");
8082ee97cafSCornelia Huck 	if (error)
8092ee97cafSCornelia Huck 		goto out;
810da231fd5SKay Sievers 
8114e886c29SGreg Kroah-Hartman 	if (dev->parent && device_is_not_partition(dev)) {
8124f01a757SDmitry Torokhov 		error = sysfs_create_link(&dev->kobj, &dev->parent->kobj,
8134f01a757SDmitry Torokhov 					  "device");
8144f01a757SDmitry Torokhov 		if (error)
81539aba963SKay Sievers 			goto out_subsys;
8162ee97cafSCornelia Huck 	}
81739aba963SKay Sievers 
818ead454feSRandy Dunlap #ifdef CONFIG_BLOCK
81939aba963SKay Sievers 	/* /sys/block has directories and does not need symlinks */
820e52eec13SAndi Kleen 	if (sysfs_deprecated && dev->class == &block_class)
82139aba963SKay Sievers 		return 0;
822ead454feSRandy Dunlap #endif
82339aba963SKay Sievers 
82439aba963SKay Sievers 	/* link in the class directory pointing to the device */
8256b6e39a6SKay Sievers 	error = sysfs_create_link(&dev->class->p->subsys.kobj,
82639aba963SKay Sievers 				  &dev->kobj, dev_name(dev));
82739aba963SKay Sievers 	if (error)
82839aba963SKay Sievers 		goto out_device;
82939aba963SKay Sievers 
8302ee97cafSCornelia Huck 	return 0;
8312ee97cafSCornelia Huck 
83239aba963SKay Sievers out_device:
83339aba963SKay Sievers 	sysfs_remove_link(&dev->kobj, "device");
834da231fd5SKay Sievers 
8352ee97cafSCornelia Huck out_subsys:
8362ee97cafSCornelia Huck 	sysfs_remove_link(&dev->kobj, "subsystem");
8372ee97cafSCornelia Huck out:
8382ee97cafSCornelia Huck 	return error;
8392ee97cafSCornelia Huck }
8402ee97cafSCornelia Huck 
8412ee97cafSCornelia Huck static void device_remove_class_symlinks(struct device *dev)
8422ee97cafSCornelia Huck {
8432ee97cafSCornelia Huck 	if (!dev->class)
8442ee97cafSCornelia Huck 		return;
845da231fd5SKay Sievers 
8464e886c29SGreg Kroah-Hartman 	if (dev->parent && device_is_not_partition(dev))
847da231fd5SKay Sievers 		sysfs_remove_link(&dev->kobj, "device");
8482ee97cafSCornelia Huck 	sysfs_remove_link(&dev->kobj, "subsystem");
849ead454feSRandy Dunlap #ifdef CONFIG_BLOCK
850e52eec13SAndi Kleen 	if (sysfs_deprecated && dev->class == &block_class)
85139aba963SKay Sievers 		return;
852ead454feSRandy Dunlap #endif
8536b6e39a6SKay Sievers 	sysfs_delete_link(&dev->class->p->subsys.kobj, &dev->kobj, dev_name(dev));
8542ee97cafSCornelia Huck }
8552ee97cafSCornelia Huck 
8561da177e4SLinus Torvalds /**
857413c239fSStephen Rothwell  * dev_set_name - set a device name
858413c239fSStephen Rothwell  * @dev: device
85946232366SRandy Dunlap  * @fmt: format string for the device's name
860413c239fSStephen Rothwell  */
861413c239fSStephen Rothwell int dev_set_name(struct device *dev, const char *fmt, ...)
862413c239fSStephen Rothwell {
863413c239fSStephen Rothwell 	va_list vargs;
8641fa5ae85SKay Sievers 	int err;
865413c239fSStephen Rothwell 
866413c239fSStephen Rothwell 	va_start(vargs, fmt);
8671fa5ae85SKay Sievers 	err = kobject_set_name_vargs(&dev->kobj, fmt, vargs);
868413c239fSStephen Rothwell 	va_end(vargs);
8691fa5ae85SKay Sievers 	return err;
870413c239fSStephen Rothwell }
871413c239fSStephen Rothwell EXPORT_SYMBOL_GPL(dev_set_name);
872413c239fSStephen Rothwell 
873413c239fSStephen Rothwell /**
874e105b8bfSDan Williams  * device_to_dev_kobj - select a /sys/dev/ directory for the device
875e105b8bfSDan Williams  * @dev: device
876e105b8bfSDan Williams  *
877e105b8bfSDan Williams  * By default we select char/ for new entries.  Setting class->dev_obj
878e105b8bfSDan Williams  * to NULL prevents an entry from being created.  class->dev_kobj must
879e105b8bfSDan Williams  * be set (or cleared) before any devices are registered to the class
880e105b8bfSDan Williams  * otherwise device_create_sys_dev_entry() and
8810d4e293cSPeter Korsgaard  * device_remove_sys_dev_entry() will disagree about the presence of
8820d4e293cSPeter Korsgaard  * the link.
883e105b8bfSDan Williams  */
884e105b8bfSDan Williams static struct kobject *device_to_dev_kobj(struct device *dev)
885e105b8bfSDan Williams {
886e105b8bfSDan Williams 	struct kobject *kobj;
887e105b8bfSDan Williams 
888e105b8bfSDan Williams 	if (dev->class)
889e105b8bfSDan Williams 		kobj = dev->class->dev_kobj;
890e105b8bfSDan Williams 	else
891e105b8bfSDan Williams 		kobj = sysfs_dev_char_kobj;
892e105b8bfSDan Williams 
893e105b8bfSDan Williams 	return kobj;
894e105b8bfSDan Williams }
895e105b8bfSDan Williams 
896e105b8bfSDan Williams static int device_create_sys_dev_entry(struct device *dev)
897e105b8bfSDan Williams {
898e105b8bfSDan Williams 	struct kobject *kobj = device_to_dev_kobj(dev);
899e105b8bfSDan Williams 	int error = 0;
900e105b8bfSDan Williams 	char devt_str[15];
901e105b8bfSDan Williams 
902e105b8bfSDan Williams 	if (kobj) {
903e105b8bfSDan Williams 		format_dev_t(devt_str, dev->devt);
904e105b8bfSDan Williams 		error = sysfs_create_link(kobj, &dev->kobj, devt_str);
905e105b8bfSDan Williams 	}
906e105b8bfSDan Williams 
907e105b8bfSDan Williams 	return error;
908e105b8bfSDan Williams }
909e105b8bfSDan Williams 
910e105b8bfSDan Williams static void device_remove_sys_dev_entry(struct device *dev)
911e105b8bfSDan Williams {
912e105b8bfSDan Williams 	struct kobject *kobj = device_to_dev_kobj(dev);
913e105b8bfSDan Williams 	char devt_str[15];
914e105b8bfSDan Williams 
915e105b8bfSDan Williams 	if (kobj) {
916e105b8bfSDan Williams 		format_dev_t(devt_str, dev->devt);
917e105b8bfSDan Williams 		sysfs_remove_link(kobj, devt_str);
918e105b8bfSDan Williams 	}
919e105b8bfSDan Williams }
920e105b8bfSDan Williams 
921b4028437SGreg Kroah-Hartman int device_private_init(struct device *dev)
922b4028437SGreg Kroah-Hartman {
923b4028437SGreg Kroah-Hartman 	dev->p = kzalloc(sizeof(*dev->p), GFP_KERNEL);
924b4028437SGreg Kroah-Hartman 	if (!dev->p)
925b4028437SGreg Kroah-Hartman 		return -ENOMEM;
926b4028437SGreg Kroah-Hartman 	dev->p->device = dev;
927b4028437SGreg Kroah-Hartman 	klist_init(&dev->p->klist_children, klist_children_get,
928b4028437SGreg Kroah-Hartman 		   klist_children_put);
929ef8a3fd6SGreg Kroah-Hartman 	INIT_LIST_HEAD(&dev->p->deferred_probe);
930b4028437SGreg Kroah-Hartman 	return 0;
931b4028437SGreg Kroah-Hartman }
932b4028437SGreg Kroah-Hartman 
933e105b8bfSDan Williams /**
9341da177e4SLinus Torvalds  * device_add - add device to device hierarchy.
9351da177e4SLinus Torvalds  * @dev: device.
9361da177e4SLinus Torvalds  *
9371da177e4SLinus Torvalds  * This is part 2 of device_register(), though may be called
9381da177e4SLinus Torvalds  * separately _iff_ device_initialize() has been called separately.
9391da177e4SLinus Torvalds  *
9405739411aSCornelia Huck  * This adds @dev to the kobject hierarchy via kobject_add(), adds it
9411da177e4SLinus Torvalds  * to the global and sibling lists for the device, then
9421da177e4SLinus Torvalds  * adds it to the other relevant subsystems of the driver model.
9435739411aSCornelia Huck  *
944b10d5efdSAlan Stern  * Do not call this routine or device_register() more than once for
945b10d5efdSAlan Stern  * any device structure.  The driver model core is not designed to work
946b10d5efdSAlan Stern  * with devices that get unregistered and then spring back to life.
947b10d5efdSAlan Stern  * (Among other things, it's very hard to guarantee that all references
948b10d5efdSAlan Stern  * to the previous incarnation of @dev have been dropped.)  Allocate
949b10d5efdSAlan Stern  * and register a fresh new struct device instead.
950b10d5efdSAlan Stern  *
9515739411aSCornelia Huck  * NOTE: _Never_ directly free @dev after calling this function, even
9525739411aSCornelia Huck  * if it returned an error! Always use put_device() to give up your
9535739411aSCornelia Huck  * reference instead.
9541da177e4SLinus Torvalds  */
9551da177e4SLinus Torvalds int device_add(struct device *dev)
9561da177e4SLinus Torvalds {
9571da177e4SLinus Torvalds 	struct device *parent = NULL;
958ca22e56dSKay Sievers 	struct kobject *kobj;
959c47ed219SGreg Kroah-Hartman 	struct class_interface *class_intf;
960c906a48aSGreg Kroah-Hartman 	int error = -EINVAL;
961775b64d2SRafael J. Wysocki 
9621da177e4SLinus Torvalds 	dev = get_device(dev);
963c906a48aSGreg Kroah-Hartman 	if (!dev)
964c906a48aSGreg Kroah-Hartman 		goto done;
965c906a48aSGreg Kroah-Hartman 
966fb069a5dSGreg Kroah-Hartman 	if (!dev->p) {
967b4028437SGreg Kroah-Hartman 		error = device_private_init(dev);
968b4028437SGreg Kroah-Hartman 		if (error)
969fb069a5dSGreg Kroah-Hartman 			goto done;
970fb069a5dSGreg Kroah-Hartman 	}
971fb069a5dSGreg Kroah-Hartman 
9721fa5ae85SKay Sievers 	/*
9731fa5ae85SKay Sievers 	 * for statically allocated devices, which should all be converted
9741fa5ae85SKay Sievers 	 * some day, we need to initialize the name. We prevent reading back
9751fa5ae85SKay Sievers 	 * the name, and force the use of dev_name()
9761fa5ae85SKay Sievers 	 */
9771fa5ae85SKay Sievers 	if (dev->init_name) {
978acc0e90fSGreg Kroah-Hartman 		dev_set_name(dev, "%s", dev->init_name);
9791fa5ae85SKay Sievers 		dev->init_name = NULL;
9801fa5ae85SKay Sievers 	}
981c906a48aSGreg Kroah-Hartman 
982ca22e56dSKay Sievers 	/* subsystems can specify simple device enumeration */
983ca22e56dSKay Sievers 	if (!dev_name(dev) && dev->bus && dev->bus->dev_name)
984ca22e56dSKay Sievers 		dev_set_name(dev, "%s%u", dev->bus->dev_name, dev->id);
985ca22e56dSKay Sievers 
986e6309e75SThomas Gleixner 	if (!dev_name(dev)) {
987e6309e75SThomas Gleixner 		error = -EINVAL;
9885c8563d7SKay Sievers 		goto name_error;
989e6309e75SThomas Gleixner 	}
9901da177e4SLinus Torvalds 
9911e0b2cf9SKay Sievers 	pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
992c205ef48SGreg Kroah-Hartman 
9931da177e4SLinus Torvalds 	parent = get_device(dev->parent);
994ca22e56dSKay Sievers 	kobj = get_device_parent(dev, parent);
995ca22e56dSKay Sievers 	if (kobj)
996ca22e56dSKay Sievers 		dev->kobj.parent = kobj;
9971da177e4SLinus Torvalds 
9980d358f22SYinghai Lu 	/* use parent numa_node */
9990d358f22SYinghai Lu 	if (parent)
10000d358f22SYinghai Lu 		set_dev_node(dev, dev_to_node(parent));
10010d358f22SYinghai Lu 
10021da177e4SLinus Torvalds 	/* first, register with generic layer. */
10038a577ffcSKay Sievers 	/* we require the name to be set before, and pass NULL */
10048a577ffcSKay Sievers 	error = kobject_add(&dev->kobj, dev->kobj.parent, NULL);
100540fa5422SGreg Kroah-Hartman 	if (error)
10061da177e4SLinus Torvalds 		goto Error;
1007a7fd6706SKay Sievers 
100837022644SBrian Walsh 	/* notify platform of device entry */
100937022644SBrian Walsh 	if (platform_notify)
101037022644SBrian Walsh 		platform_notify(dev);
101137022644SBrian Walsh 
1012ad6a1e1cSTejun Heo 	error = device_create_file(dev, &uevent_attr);
1013a306eea4SCornelia Huck 	if (error)
1014a306eea4SCornelia Huck 		goto attrError;
1015a7fd6706SKay Sievers 
101623681e47SGreg Kroah-Hartman 	if (MAJOR(dev->devt)) {
1017ad6a1e1cSTejun Heo 		error = device_create_file(dev, &devt_attr);
1018ad6a1e1cSTejun Heo 		if (error)
1019a306eea4SCornelia Huck 			goto ueventattrError;
1020e105b8bfSDan Williams 
1021e105b8bfSDan Williams 		error = device_create_sys_dev_entry(dev);
1022e105b8bfSDan Williams 		if (error)
1023e105b8bfSDan Williams 			goto devtattrError;
10242b2af54aSKay Sievers 
10252b2af54aSKay Sievers 		devtmpfs_create_node(dev);
102623681e47SGreg Kroah-Hartman 	}
102723681e47SGreg Kroah-Hartman 
10282ee97cafSCornelia Huck 	error = device_add_class_symlinks(dev);
10292ee97cafSCornelia Huck 	if (error)
10302ee97cafSCornelia Huck 		goto SymlinkError;
1031dc0afa83SCornelia Huck 	error = device_add_attrs(dev);
1032dc0afa83SCornelia Huck 	if (error)
10332620efefSGreg Kroah-Hartman 		goto AttrsError;
1034dc0afa83SCornelia Huck 	error = bus_add_device(dev);
1035dc0afa83SCornelia Huck 	if (error)
10361da177e4SLinus Torvalds 		goto BusError;
10373b98aeafSAlan Stern 	error = dpm_sysfs_add(dev);
103857eee3d2SRafael J. Wysocki 	if (error)
10393b98aeafSAlan Stern 		goto DPMError;
10403b98aeafSAlan Stern 	device_pm_add(dev);
1041ec0676eeSAlan Stern 
1042ec0676eeSAlan Stern 	/* Notify clients of device addition.  This call must come
1043268863f4Smajianpeng 	 * after dpm_sysfs_add() and before kobject_uevent().
1044ec0676eeSAlan Stern 	 */
1045ec0676eeSAlan Stern 	if (dev->bus)
1046ec0676eeSAlan Stern 		blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
1047ec0676eeSAlan Stern 					     BUS_NOTIFY_ADD_DEVICE, dev);
1048ec0676eeSAlan Stern 
104953877d06SKay Sievers 	kobject_uevent(&dev->kobj, KOBJ_ADD);
10502023c610SAlan Stern 	bus_probe_device(dev);
10511da177e4SLinus Torvalds 	if (parent)
1052f791b8c8SGreg Kroah-Hartman 		klist_add_tail(&dev->p->knode_parent,
1053f791b8c8SGreg Kroah-Hartman 			       &parent->p->klist_children);
10541da177e4SLinus Torvalds 
10555d9fd169SGreg Kroah-Hartman 	if (dev->class) {
1056ca22e56dSKay Sievers 		mutex_lock(&dev->class->p->mutex);
1057c47ed219SGreg Kroah-Hartman 		/* tie the class to the device */
10585a3ceb86STejun Heo 		klist_add_tail(&dev->knode_class,
10596b6e39a6SKay Sievers 			       &dev->class->p->klist_devices);
1060c47ed219SGreg Kroah-Hartman 
1061c47ed219SGreg Kroah-Hartman 		/* notify any interfaces that the device is here */
1062184f1f77SGreg Kroah-Hartman 		list_for_each_entry(class_intf,
1063ca22e56dSKay Sievers 				    &dev->class->p->interfaces, node)
1064c47ed219SGreg Kroah-Hartman 			if (class_intf->add_dev)
1065c47ed219SGreg Kroah-Hartman 				class_intf->add_dev(dev, class_intf);
1066ca22e56dSKay Sievers 		mutex_unlock(&dev->class->p->mutex);
10675d9fd169SGreg Kroah-Hartman 	}
1068c906a48aSGreg Kroah-Hartman done:
10691da177e4SLinus Torvalds 	put_device(dev);
10701da177e4SLinus Torvalds 	return error;
10713b98aeafSAlan Stern  DPMError:
107257eee3d2SRafael J. Wysocki 	bus_remove_device(dev);
107357eee3d2SRafael J. Wysocki  BusError:
10742620efefSGreg Kroah-Hartman 	device_remove_attrs(dev);
10752620efefSGreg Kroah-Hartman  AttrsError:
10762ee97cafSCornelia Huck 	device_remove_class_symlinks(dev);
10772ee97cafSCornelia Huck  SymlinkError:
1078ad6a1e1cSTejun Heo 	if (MAJOR(dev->devt))
1079ad72956dSKay Sievers 		devtmpfs_delete_node(dev);
1080ad72956dSKay Sievers 	if (MAJOR(dev->devt))
1081e105b8bfSDan Williams 		device_remove_sys_dev_entry(dev);
1082e105b8bfSDan Williams  devtattrError:
1083e105b8bfSDan Williams 	if (MAJOR(dev->devt))
1084ad6a1e1cSTejun Heo 		device_remove_file(dev, &devt_attr);
1085a306eea4SCornelia Huck  ueventattrError:
1086ad6a1e1cSTejun Heo 	device_remove_file(dev, &uevent_attr);
108723681e47SGreg Kroah-Hartman  attrError:
1088312c004dSKay Sievers 	kobject_uevent(&dev->kobj, KOBJ_REMOVE);
10891da177e4SLinus Torvalds 	kobject_del(&dev->kobj);
10901da177e4SLinus Torvalds  Error:
109163b6971aSCornelia Huck 	cleanup_device_parent(dev);
10921da177e4SLinus Torvalds 	if (parent)
10931da177e4SLinus Torvalds 		put_device(parent);
10945c8563d7SKay Sievers name_error:
10955c8563d7SKay Sievers 	kfree(dev->p);
10965c8563d7SKay Sievers 	dev->p = NULL;
1097c906a48aSGreg Kroah-Hartman 	goto done;
10981da177e4SLinus Torvalds }
10991da177e4SLinus Torvalds 
11001da177e4SLinus Torvalds /**
11011da177e4SLinus Torvalds  * device_register - register a device with the system.
11021da177e4SLinus Torvalds  * @dev: pointer to the device structure
11031da177e4SLinus Torvalds  *
11041da177e4SLinus Torvalds  * This happens in two clean steps - initialize the device
11051da177e4SLinus Torvalds  * and add it to the system. The two steps can be called
11061da177e4SLinus Torvalds  * separately, but this is the easiest and most common.
11071da177e4SLinus Torvalds  * I.e. you should only call the two helpers separately if
11081da177e4SLinus Torvalds  * have a clearly defined need to use and refcount the device
11091da177e4SLinus Torvalds  * before it is added to the hierarchy.
11105739411aSCornelia Huck  *
1111b10d5efdSAlan Stern  * For more information, see the kerneldoc for device_initialize()
1112b10d5efdSAlan Stern  * and device_add().
1113b10d5efdSAlan Stern  *
11145739411aSCornelia Huck  * NOTE: _Never_ directly free @dev after calling this function, even
11155739411aSCornelia Huck  * if it returned an error! Always use put_device() to give up the
11165739411aSCornelia Huck  * reference initialized in this function instead.
11171da177e4SLinus Torvalds  */
11181da177e4SLinus Torvalds int device_register(struct device *dev)
11191da177e4SLinus Torvalds {
11201da177e4SLinus Torvalds 	device_initialize(dev);
11211da177e4SLinus Torvalds 	return device_add(dev);
11221da177e4SLinus Torvalds }
11231da177e4SLinus Torvalds 
11241da177e4SLinus Torvalds /**
11251da177e4SLinus Torvalds  * get_device - increment reference count for device.
11261da177e4SLinus Torvalds  * @dev: device.
11271da177e4SLinus Torvalds  *
11281da177e4SLinus Torvalds  * This simply forwards the call to kobject_get(), though
11291da177e4SLinus Torvalds  * we do take care to provide for the case that we get a NULL
11301da177e4SLinus Torvalds  * pointer passed in.
11311da177e4SLinus Torvalds  */
11321da177e4SLinus Torvalds struct device *get_device(struct device *dev)
11331da177e4SLinus Torvalds {
11341da177e4SLinus Torvalds 	return dev ? to_dev(kobject_get(&dev->kobj)) : NULL;
11351da177e4SLinus Torvalds }
11361da177e4SLinus Torvalds 
11371da177e4SLinus Torvalds /**
11381da177e4SLinus Torvalds  * put_device - decrement reference count.
11391da177e4SLinus Torvalds  * @dev: device in question.
11401da177e4SLinus Torvalds  */
11411da177e4SLinus Torvalds void put_device(struct device *dev)
11421da177e4SLinus Torvalds {
1143edfaa7c3SKay Sievers 	/* might_sleep(); */
11441da177e4SLinus Torvalds 	if (dev)
11451da177e4SLinus Torvalds 		kobject_put(&dev->kobj);
11461da177e4SLinus Torvalds }
11471da177e4SLinus Torvalds 
11481da177e4SLinus Torvalds /**
11491da177e4SLinus Torvalds  * device_del - delete device from system.
11501da177e4SLinus Torvalds  * @dev: device.
11511da177e4SLinus Torvalds  *
11521da177e4SLinus Torvalds  * This is the first part of the device unregistration
11531da177e4SLinus Torvalds  * sequence. This removes the device from the lists we control
11541da177e4SLinus Torvalds  * from here, has it removed from the other driver model
11551da177e4SLinus Torvalds  * subsystems it was added to in device_add(), and removes it
11561da177e4SLinus Torvalds  * from the kobject hierarchy.
11571da177e4SLinus Torvalds  *
11581da177e4SLinus Torvalds  * NOTE: this should be called manually _iff_ device_add() was
11591da177e4SLinus Torvalds  * also called manually.
11601da177e4SLinus Torvalds  */
11611da177e4SLinus Torvalds void device_del(struct device *dev)
11621da177e4SLinus Torvalds {
11631da177e4SLinus Torvalds 	struct device *parent = dev->parent;
1164c47ed219SGreg Kroah-Hartman 	struct class_interface *class_intf;
11651da177e4SLinus Torvalds 
1166ec0676eeSAlan Stern 	/* Notify clients of device removal.  This call must come
1167ec0676eeSAlan Stern 	 * before dpm_sysfs_remove().
1168ec0676eeSAlan Stern 	 */
1169ec0676eeSAlan Stern 	if (dev->bus)
1170ec0676eeSAlan Stern 		blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
1171ec0676eeSAlan Stern 					     BUS_NOTIFY_DEL_DEVICE, dev);
1172775b64d2SRafael J. Wysocki 	device_pm_remove(dev);
11733b98aeafSAlan Stern 	dpm_sysfs_remove(dev);
11741da177e4SLinus Torvalds 	if (parent)
1175f791b8c8SGreg Kroah-Hartman 		klist_del(&dev->p->knode_parent);
1176e105b8bfSDan Williams 	if (MAJOR(dev->devt)) {
11772b2af54aSKay Sievers 		devtmpfs_delete_node(dev);
1178e105b8bfSDan Williams 		device_remove_sys_dev_entry(dev);
1179ad6a1e1cSTejun Heo 		device_remove_file(dev, &devt_attr);
1180e105b8bfSDan Williams 	}
1181b9d9c82bSKay Sievers 	if (dev->class) {
1182da231fd5SKay Sievers 		device_remove_class_symlinks(dev);
118399ef3ef8SKay Sievers 
1184ca22e56dSKay Sievers 		mutex_lock(&dev->class->p->mutex);
1185c47ed219SGreg Kroah-Hartman 		/* notify any interfaces that the device is now gone */
1186184f1f77SGreg Kroah-Hartman 		list_for_each_entry(class_intf,
1187ca22e56dSKay Sievers 				    &dev->class->p->interfaces, node)
1188c47ed219SGreg Kroah-Hartman 			if (class_intf->remove_dev)
1189c47ed219SGreg Kroah-Hartman 				class_intf->remove_dev(dev, class_intf);
1190c47ed219SGreg Kroah-Hartman 		/* remove the device from the class list */
11915a3ceb86STejun Heo 		klist_del(&dev->knode_class);
1192ca22e56dSKay Sievers 		mutex_unlock(&dev->class->p->mutex);
1193b9d9c82bSKay Sievers 	}
1194ad6a1e1cSTejun Heo 	device_remove_file(dev, &uevent_attr);
11952620efefSGreg Kroah-Hartman 	device_remove_attrs(dev);
119628953533SBenjamin Herrenschmidt 	bus_remove_device(dev);
1197d1c3414cSGrant Likely 	driver_deferred_probe_del(dev);
11981da177e4SLinus Torvalds 
11992f8d16a9STejun Heo 	/*
12002f8d16a9STejun Heo 	 * Some platform devices are driven without driver attached
12012f8d16a9STejun Heo 	 * and managed resources may have been acquired.  Make sure
12022f8d16a9STejun Heo 	 * all resources are released.
12032f8d16a9STejun Heo 	 */
12042f8d16a9STejun Heo 	devres_release_all(dev);
12052f8d16a9STejun Heo 
12061da177e4SLinus Torvalds 	/* Notify the platform of the removal, in case they
12071da177e4SLinus Torvalds 	 * need to do anything...
12081da177e4SLinus Torvalds 	 */
12091da177e4SLinus Torvalds 	if (platform_notify_remove)
12101da177e4SLinus Torvalds 		platform_notify_remove(dev);
1211312c004dSKay Sievers 	kobject_uevent(&dev->kobj, KOBJ_REMOVE);
1212da231fd5SKay Sievers 	cleanup_device_parent(dev);
12131da177e4SLinus Torvalds 	kobject_del(&dev->kobj);
12141da177e4SLinus Torvalds 	put_device(parent);
12151da177e4SLinus Torvalds }
12161da177e4SLinus Torvalds 
12171da177e4SLinus Torvalds /**
12181da177e4SLinus Torvalds  * device_unregister - unregister device from system.
12191da177e4SLinus Torvalds  * @dev: device going away.
12201da177e4SLinus Torvalds  *
12211da177e4SLinus Torvalds  * We do this in two parts, like we do device_register(). First,
12221da177e4SLinus Torvalds  * we remove it from all the subsystems with device_del(), then
12231da177e4SLinus Torvalds  * we decrement the reference count via put_device(). If that
12241da177e4SLinus Torvalds  * is the final reference count, the device will be cleaned up
12251da177e4SLinus Torvalds  * via device_release() above. Otherwise, the structure will
12261da177e4SLinus Torvalds  * stick around until the final reference to the device is dropped.
12271da177e4SLinus Torvalds  */
12281da177e4SLinus Torvalds void device_unregister(struct device *dev)
12291da177e4SLinus Torvalds {
12301e0b2cf9SKay Sievers 	pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
12311da177e4SLinus Torvalds 	device_del(dev);
12321da177e4SLinus Torvalds 	put_device(dev);
12331da177e4SLinus Torvalds }
12341da177e4SLinus Torvalds 
123536239577Smochel@digitalimplant.org static struct device *next_device(struct klist_iter *i)
123636239577Smochel@digitalimplant.org {
123736239577Smochel@digitalimplant.org 	struct klist_node *n = klist_next(i);
1238f791b8c8SGreg Kroah-Hartman 	struct device *dev = NULL;
1239f791b8c8SGreg Kroah-Hartman 	struct device_private *p;
1240f791b8c8SGreg Kroah-Hartman 
1241f791b8c8SGreg Kroah-Hartman 	if (n) {
1242f791b8c8SGreg Kroah-Hartman 		p = to_device_private_parent(n);
1243f791b8c8SGreg Kroah-Hartman 		dev = p->device;
1244f791b8c8SGreg Kroah-Hartman 	}
1245f791b8c8SGreg Kroah-Hartman 	return dev;
124636239577Smochel@digitalimplant.org }
124736239577Smochel@digitalimplant.org 
12481da177e4SLinus Torvalds /**
1249e454cea2SKay Sievers  * device_get_devnode - path of device node file
12506fcf53acSKay Sievers  * @dev: device
1251e454cea2SKay Sievers  * @mode: returned file access mode
12526fcf53acSKay Sievers  * @tmp: possibly allocated string
12536fcf53acSKay Sievers  *
12546fcf53acSKay Sievers  * Return the relative path of a possible device node.
12556fcf53acSKay Sievers  * Non-default names may need to allocate a memory to compose
12566fcf53acSKay Sievers  * a name. This memory is returned in tmp and needs to be
12576fcf53acSKay Sievers  * freed by the caller.
12586fcf53acSKay Sievers  */
1259e454cea2SKay Sievers const char *device_get_devnode(struct device *dev,
12602c9ede55SAl Viro 			       umode_t *mode, const char **tmp)
12616fcf53acSKay Sievers {
12626fcf53acSKay Sievers 	char *s;
12636fcf53acSKay Sievers 
12646fcf53acSKay Sievers 	*tmp = NULL;
12656fcf53acSKay Sievers 
12666fcf53acSKay Sievers 	/* the device type may provide a specific name */
1267e454cea2SKay Sievers 	if (dev->type && dev->type->devnode)
1268e454cea2SKay Sievers 		*tmp = dev->type->devnode(dev, mode);
12696fcf53acSKay Sievers 	if (*tmp)
12706fcf53acSKay Sievers 		return *tmp;
12716fcf53acSKay Sievers 
12726fcf53acSKay Sievers 	/* the class may provide a specific name */
1273e454cea2SKay Sievers 	if (dev->class && dev->class->devnode)
1274e454cea2SKay Sievers 		*tmp = dev->class->devnode(dev, mode);
12756fcf53acSKay Sievers 	if (*tmp)
12766fcf53acSKay Sievers 		return *tmp;
12776fcf53acSKay Sievers 
12786fcf53acSKay Sievers 	/* return name without allocation, tmp == NULL */
12796fcf53acSKay Sievers 	if (strchr(dev_name(dev), '!') == NULL)
12806fcf53acSKay Sievers 		return dev_name(dev);
12816fcf53acSKay Sievers 
12826fcf53acSKay Sievers 	/* replace '!' in the name with '/' */
12836fcf53acSKay Sievers 	*tmp = kstrdup(dev_name(dev), GFP_KERNEL);
12846fcf53acSKay Sievers 	if (!*tmp)
12856fcf53acSKay Sievers 		return NULL;
12866fcf53acSKay Sievers 	while ((s = strchr(*tmp, '!')))
12876fcf53acSKay Sievers 		s[0] = '/';
12886fcf53acSKay Sievers 	return *tmp;
12896fcf53acSKay Sievers }
12906fcf53acSKay Sievers 
12916fcf53acSKay Sievers /**
12921da177e4SLinus Torvalds  * device_for_each_child - device child iterator.
1293c41455fbSRandy Dunlap  * @parent: parent struct device.
12941da177e4SLinus Torvalds  * @data: data for the callback.
12951da177e4SLinus Torvalds  * @fn: function to be called for each device.
12961da177e4SLinus Torvalds  *
1297c41455fbSRandy Dunlap  * Iterate over @parent's child devices, and call @fn for each,
12981da177e4SLinus Torvalds  * passing it @data.
12991da177e4SLinus Torvalds  *
13001da177e4SLinus Torvalds  * We check the return of @fn each time. If it returns anything
13011da177e4SLinus Torvalds  * other than 0, we break out and return that value.
13021da177e4SLinus Torvalds  */
130336239577Smochel@digitalimplant.org int device_for_each_child(struct device *parent, void *data,
13044a3ad20cSGreg Kroah-Hartman 			  int (*fn)(struct device *dev, void *data))
13051da177e4SLinus Torvalds {
130636239577Smochel@digitalimplant.org 	struct klist_iter i;
13071da177e4SLinus Torvalds 	struct device *child;
13081da177e4SLinus Torvalds 	int error = 0;
13091da177e4SLinus Torvalds 
1310014c90dbSGreg Kroah-Hartman 	if (!parent->p)
1311014c90dbSGreg Kroah-Hartman 		return 0;
1312014c90dbSGreg Kroah-Hartman 
1313f791b8c8SGreg Kroah-Hartman 	klist_iter_init(&parent->p->klist_children, &i);
131436239577Smochel@digitalimplant.org 	while ((child = next_device(&i)) && !error)
131536239577Smochel@digitalimplant.org 		error = fn(child, data);
131636239577Smochel@digitalimplant.org 	klist_iter_exit(&i);
13171da177e4SLinus Torvalds 	return error;
13181da177e4SLinus Torvalds }
13191da177e4SLinus Torvalds 
13205ab69981SCornelia Huck /**
13215ab69981SCornelia Huck  * device_find_child - device iterator for locating a particular device.
13225ab69981SCornelia Huck  * @parent: parent struct device
13235ab69981SCornelia Huck  * @data: Data to pass to match function
13245ab69981SCornelia Huck  * @match: Callback function to check device
13255ab69981SCornelia Huck  *
13265ab69981SCornelia Huck  * This is similar to the device_for_each_child() function above, but it
13275ab69981SCornelia Huck  * returns a reference to a device that is 'found' for later use, as
13285ab69981SCornelia Huck  * determined by the @match callback.
13295ab69981SCornelia Huck  *
13305ab69981SCornelia Huck  * The callback should return 0 if the device doesn't match and non-zero
13315ab69981SCornelia Huck  * if it does.  If the callback returns non-zero and a reference to the
13325ab69981SCornelia Huck  * current device can be obtained, this function will return to the caller
13335ab69981SCornelia Huck  * and not iterate over any more devices.
13345ab69981SCornelia Huck  */
13355ab69981SCornelia Huck struct device *device_find_child(struct device *parent, void *data,
13364a3ad20cSGreg Kroah-Hartman 				 int (*match)(struct device *dev, void *data))
13375ab69981SCornelia Huck {
13385ab69981SCornelia Huck 	struct klist_iter i;
13395ab69981SCornelia Huck 	struct device *child;
13405ab69981SCornelia Huck 
13415ab69981SCornelia Huck 	if (!parent)
13425ab69981SCornelia Huck 		return NULL;
13435ab69981SCornelia Huck 
1344f791b8c8SGreg Kroah-Hartman 	klist_iter_init(&parent->p->klist_children, &i);
13455ab69981SCornelia Huck 	while ((child = next_device(&i)))
13465ab69981SCornelia Huck 		if (match(child, data) && get_device(child))
13475ab69981SCornelia Huck 			break;
13485ab69981SCornelia Huck 	klist_iter_exit(&i);
13495ab69981SCornelia Huck 	return child;
13505ab69981SCornelia Huck }
13515ab69981SCornelia Huck 
13521da177e4SLinus Torvalds int __init devices_init(void)
13531da177e4SLinus Torvalds {
1354881c6cfdSGreg Kroah-Hartman 	devices_kset = kset_create_and_add("devices", &device_uevent_ops, NULL);
1355881c6cfdSGreg Kroah-Hartman 	if (!devices_kset)
1356881c6cfdSGreg Kroah-Hartman 		return -ENOMEM;
1357e105b8bfSDan Williams 	dev_kobj = kobject_create_and_add("dev", NULL);
1358e105b8bfSDan Williams 	if (!dev_kobj)
1359e105b8bfSDan Williams 		goto dev_kobj_err;
1360e105b8bfSDan Williams 	sysfs_dev_block_kobj = kobject_create_and_add("block", dev_kobj);
1361e105b8bfSDan Williams 	if (!sysfs_dev_block_kobj)
1362e105b8bfSDan Williams 		goto block_kobj_err;
1363e105b8bfSDan Williams 	sysfs_dev_char_kobj = kobject_create_and_add("char", dev_kobj);
1364e105b8bfSDan Williams 	if (!sysfs_dev_char_kobj)
1365e105b8bfSDan Williams 		goto char_kobj_err;
1366e105b8bfSDan Williams 
1367881c6cfdSGreg Kroah-Hartman 	return 0;
1368e105b8bfSDan Williams 
1369e105b8bfSDan Williams  char_kobj_err:
1370e105b8bfSDan Williams 	kobject_put(sysfs_dev_block_kobj);
1371e105b8bfSDan Williams  block_kobj_err:
1372e105b8bfSDan Williams 	kobject_put(dev_kobj);
1373e105b8bfSDan Williams  dev_kobj_err:
1374e105b8bfSDan Williams 	kset_unregister(devices_kset);
1375e105b8bfSDan Williams 	return -ENOMEM;
13761da177e4SLinus Torvalds }
13771da177e4SLinus Torvalds 
13781da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(device_for_each_child);
13795ab69981SCornelia Huck EXPORT_SYMBOL_GPL(device_find_child);
13801da177e4SLinus Torvalds 
13811da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(device_initialize);
13821da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(device_add);
13831da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(device_register);
13841da177e4SLinus Torvalds 
13851da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(device_del);
13861da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(device_unregister);
13871da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(get_device);
13881da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(put_device);
13891da177e4SLinus Torvalds 
13901da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(device_create_file);
13911da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(device_remove_file);
139223681e47SGreg Kroah-Hartman 
13937f100d15SKarthigan Srinivasan struct root_device {
13940aa0dc41SMark McLoughlin 	struct device dev;
13950aa0dc41SMark McLoughlin 	struct module *owner;
13960aa0dc41SMark McLoughlin };
13970aa0dc41SMark McLoughlin 
1398481e2079SFerenc Wagner inline struct root_device *to_root_device(struct device *d)
1399481e2079SFerenc Wagner {
1400481e2079SFerenc Wagner 	return container_of(d, struct root_device, dev);
1401481e2079SFerenc Wagner }
14020aa0dc41SMark McLoughlin 
14030aa0dc41SMark McLoughlin static void root_device_release(struct device *dev)
14040aa0dc41SMark McLoughlin {
14050aa0dc41SMark McLoughlin 	kfree(to_root_device(dev));
14060aa0dc41SMark McLoughlin }
14070aa0dc41SMark McLoughlin 
14080aa0dc41SMark McLoughlin /**
14090aa0dc41SMark McLoughlin  * __root_device_register - allocate and register a root device
14100aa0dc41SMark McLoughlin  * @name: root device name
14110aa0dc41SMark McLoughlin  * @owner: owner module of the root device, usually THIS_MODULE
14120aa0dc41SMark McLoughlin  *
14130aa0dc41SMark McLoughlin  * This function allocates a root device and registers it
14140aa0dc41SMark McLoughlin  * using device_register(). In order to free the returned
14150aa0dc41SMark McLoughlin  * device, use root_device_unregister().
14160aa0dc41SMark McLoughlin  *
14170aa0dc41SMark McLoughlin  * Root devices are dummy devices which allow other devices
14180aa0dc41SMark McLoughlin  * to be grouped under /sys/devices. Use this function to
14190aa0dc41SMark McLoughlin  * allocate a root device and then use it as the parent of
14200aa0dc41SMark McLoughlin  * any device which should appear under /sys/devices/{name}
14210aa0dc41SMark McLoughlin  *
14220aa0dc41SMark McLoughlin  * The /sys/devices/{name} directory will also contain a
14230aa0dc41SMark McLoughlin  * 'module' symlink which points to the @owner directory
14240aa0dc41SMark McLoughlin  * in sysfs.
14250aa0dc41SMark McLoughlin  *
1426f0eae0edSJani Nikula  * Returns &struct device pointer on success, or ERR_PTR() on error.
1427f0eae0edSJani Nikula  *
14280aa0dc41SMark McLoughlin  * Note: You probably want to use root_device_register().
14290aa0dc41SMark McLoughlin  */
14300aa0dc41SMark McLoughlin struct device *__root_device_register(const char *name, struct module *owner)
14310aa0dc41SMark McLoughlin {
14320aa0dc41SMark McLoughlin 	struct root_device *root;
14330aa0dc41SMark McLoughlin 	int err = -ENOMEM;
14340aa0dc41SMark McLoughlin 
14350aa0dc41SMark McLoughlin 	root = kzalloc(sizeof(struct root_device), GFP_KERNEL);
14360aa0dc41SMark McLoughlin 	if (!root)
14370aa0dc41SMark McLoughlin 		return ERR_PTR(err);
14380aa0dc41SMark McLoughlin 
1439acc0e90fSGreg Kroah-Hartman 	err = dev_set_name(&root->dev, "%s", name);
14400aa0dc41SMark McLoughlin 	if (err) {
14410aa0dc41SMark McLoughlin 		kfree(root);
14420aa0dc41SMark McLoughlin 		return ERR_PTR(err);
14430aa0dc41SMark McLoughlin 	}
14440aa0dc41SMark McLoughlin 
14450aa0dc41SMark McLoughlin 	root->dev.release = root_device_release;
14460aa0dc41SMark McLoughlin 
14470aa0dc41SMark McLoughlin 	err = device_register(&root->dev);
14480aa0dc41SMark McLoughlin 	if (err) {
14490aa0dc41SMark McLoughlin 		put_device(&root->dev);
14500aa0dc41SMark McLoughlin 		return ERR_PTR(err);
14510aa0dc41SMark McLoughlin 	}
14520aa0dc41SMark McLoughlin 
14531d9e882bSChristoph Egger #ifdef CONFIG_MODULES	/* gotta find a "cleaner" way to do this */
14540aa0dc41SMark McLoughlin 	if (owner) {
14550aa0dc41SMark McLoughlin 		struct module_kobject *mk = &owner->mkobj;
14560aa0dc41SMark McLoughlin 
14570aa0dc41SMark McLoughlin 		err = sysfs_create_link(&root->dev.kobj, &mk->kobj, "module");
14580aa0dc41SMark McLoughlin 		if (err) {
14590aa0dc41SMark McLoughlin 			device_unregister(&root->dev);
14600aa0dc41SMark McLoughlin 			return ERR_PTR(err);
14610aa0dc41SMark McLoughlin 		}
14620aa0dc41SMark McLoughlin 		root->owner = owner;
14630aa0dc41SMark McLoughlin 	}
14640aa0dc41SMark McLoughlin #endif
14650aa0dc41SMark McLoughlin 
14660aa0dc41SMark McLoughlin 	return &root->dev;
14670aa0dc41SMark McLoughlin }
14680aa0dc41SMark McLoughlin EXPORT_SYMBOL_GPL(__root_device_register);
14690aa0dc41SMark McLoughlin 
14700aa0dc41SMark McLoughlin /**
14710aa0dc41SMark McLoughlin  * root_device_unregister - unregister and free a root device
14727cbcf225SRandy Dunlap  * @dev: device going away
14730aa0dc41SMark McLoughlin  *
14740aa0dc41SMark McLoughlin  * This function unregisters and cleans up a device that was created by
14750aa0dc41SMark McLoughlin  * root_device_register().
14760aa0dc41SMark McLoughlin  */
14770aa0dc41SMark McLoughlin void root_device_unregister(struct device *dev)
14780aa0dc41SMark McLoughlin {
14790aa0dc41SMark McLoughlin 	struct root_device *root = to_root_device(dev);
14800aa0dc41SMark McLoughlin 
14810aa0dc41SMark McLoughlin 	if (root->owner)
14820aa0dc41SMark McLoughlin 		sysfs_remove_link(&root->dev.kobj, "module");
14830aa0dc41SMark McLoughlin 
14840aa0dc41SMark McLoughlin 	device_unregister(dev);
14850aa0dc41SMark McLoughlin }
14860aa0dc41SMark McLoughlin EXPORT_SYMBOL_GPL(root_device_unregister);
14870aa0dc41SMark McLoughlin 
148823681e47SGreg Kroah-Hartman 
148923681e47SGreg Kroah-Hartman static void device_create_release(struct device *dev)
149023681e47SGreg Kroah-Hartman {
14911e0b2cf9SKay Sievers 	pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
149223681e47SGreg Kroah-Hartman 	kfree(dev);
149323681e47SGreg Kroah-Hartman }
149423681e47SGreg Kroah-Hartman 
149523681e47SGreg Kroah-Hartman /**
14968882b394SGreg Kroah-Hartman  * device_create_vargs - creates a device and registers it with sysfs
14978882b394SGreg Kroah-Hartman  * @class: pointer to the struct class that this device should be registered to
14988882b394SGreg Kroah-Hartman  * @parent: pointer to the parent struct device of this new device, if any
14998882b394SGreg Kroah-Hartman  * @devt: the dev_t for the char device to be added
15008882b394SGreg Kroah-Hartman  * @drvdata: the data to be added to the device for callbacks
15018882b394SGreg Kroah-Hartman  * @fmt: string for the device's name
15028882b394SGreg Kroah-Hartman  * @args: va_list for the device's name
15038882b394SGreg Kroah-Hartman  *
15048882b394SGreg Kroah-Hartman  * This function can be used by char device classes.  A struct device
15058882b394SGreg Kroah-Hartman  * will be created in sysfs, registered to the specified class.
15068882b394SGreg Kroah-Hartman  *
15078882b394SGreg Kroah-Hartman  * A "dev" file will be created, showing the dev_t for the device, if
15088882b394SGreg Kroah-Hartman  * the dev_t is not 0,0.
15098882b394SGreg Kroah-Hartman  * If a pointer to a parent struct device is passed in, the newly created
15108882b394SGreg Kroah-Hartman  * struct device will be a child of that device in sysfs.
15118882b394SGreg Kroah-Hartman  * The pointer to the struct device will be returned from the call.
15128882b394SGreg Kroah-Hartman  * Any further sysfs files that might be required can be created using this
15138882b394SGreg Kroah-Hartman  * pointer.
15148882b394SGreg Kroah-Hartman  *
1515f0eae0edSJani Nikula  * Returns &struct device pointer on success, or ERR_PTR() on error.
1516f0eae0edSJani Nikula  *
15178882b394SGreg Kroah-Hartman  * Note: the struct class passed to this function must have previously
15188882b394SGreg Kroah-Hartman  * been created with a call to class_create().
15198882b394SGreg Kroah-Hartman  */
15208882b394SGreg Kroah-Hartman struct device *device_create_vargs(struct class *class, struct device *parent,
15218882b394SGreg Kroah-Hartman 				   dev_t devt, void *drvdata, const char *fmt,
15228882b394SGreg Kroah-Hartman 				   va_list args)
15238882b394SGreg Kroah-Hartman {
15248882b394SGreg Kroah-Hartman 	struct device *dev = NULL;
15258882b394SGreg Kroah-Hartman 	int retval = -ENODEV;
15268882b394SGreg Kroah-Hartman 
15278882b394SGreg Kroah-Hartman 	if (class == NULL || IS_ERR(class))
15288882b394SGreg Kroah-Hartman 		goto error;
15298882b394SGreg Kroah-Hartman 
15308882b394SGreg Kroah-Hartman 	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
15318882b394SGreg Kroah-Hartman 	if (!dev) {
15328882b394SGreg Kroah-Hartman 		retval = -ENOMEM;
15338882b394SGreg Kroah-Hartman 		goto error;
15348882b394SGreg Kroah-Hartman 	}
15358882b394SGreg Kroah-Hartman 
15368882b394SGreg Kroah-Hartman 	dev->devt = devt;
15378882b394SGreg Kroah-Hartman 	dev->class = class;
15388882b394SGreg Kroah-Hartman 	dev->parent = parent;
15398882b394SGreg Kroah-Hartman 	dev->release = device_create_release;
15408882b394SGreg Kroah-Hartman 	dev_set_drvdata(dev, drvdata);
15418882b394SGreg Kroah-Hartman 
15421fa5ae85SKay Sievers 	retval = kobject_set_name_vargs(&dev->kobj, fmt, args);
15431fa5ae85SKay Sievers 	if (retval)
15441fa5ae85SKay Sievers 		goto error;
15451fa5ae85SKay Sievers 
15468882b394SGreg Kroah-Hartman 	retval = device_register(dev);
15478882b394SGreg Kroah-Hartman 	if (retval)
15488882b394SGreg Kroah-Hartman 		goto error;
15498882b394SGreg Kroah-Hartman 
15508882b394SGreg Kroah-Hartman 	return dev;
15518882b394SGreg Kroah-Hartman 
15528882b394SGreg Kroah-Hartman error:
1553286661b3SCornelia Huck 	put_device(dev);
15548882b394SGreg Kroah-Hartman 	return ERR_PTR(retval);
15558882b394SGreg Kroah-Hartman }
15568882b394SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(device_create_vargs);
15578882b394SGreg Kroah-Hartman 
15588882b394SGreg Kroah-Hartman /**
15594e106739SGreg Kroah-Hartman  * device_create - creates a device and registers it with sysfs
15608882b394SGreg Kroah-Hartman  * @class: pointer to the struct class that this device should be registered to
15618882b394SGreg Kroah-Hartman  * @parent: pointer to the parent struct device of this new device, if any
15628882b394SGreg Kroah-Hartman  * @devt: the dev_t for the char device to be added
15638882b394SGreg Kroah-Hartman  * @drvdata: the data to be added to the device for callbacks
15648882b394SGreg Kroah-Hartman  * @fmt: string for the device's name
15658882b394SGreg Kroah-Hartman  *
15668882b394SGreg Kroah-Hartman  * This function can be used by char device classes.  A struct device
15678882b394SGreg Kroah-Hartman  * will be created in sysfs, registered to the specified class.
15688882b394SGreg Kroah-Hartman  *
15698882b394SGreg Kroah-Hartman  * A "dev" file will be created, showing the dev_t for the device, if
15708882b394SGreg Kroah-Hartman  * the dev_t is not 0,0.
15718882b394SGreg Kroah-Hartman  * If a pointer to a parent struct device is passed in, the newly created
15728882b394SGreg Kroah-Hartman  * struct device will be a child of that device in sysfs.
15738882b394SGreg Kroah-Hartman  * The pointer to the struct device will be returned from the call.
15748882b394SGreg Kroah-Hartman  * Any further sysfs files that might be required can be created using this
15758882b394SGreg Kroah-Hartman  * pointer.
15768882b394SGreg Kroah-Hartman  *
1577f0eae0edSJani Nikula  * Returns &struct device pointer on success, or ERR_PTR() on error.
1578f0eae0edSJani Nikula  *
15798882b394SGreg Kroah-Hartman  * Note: the struct class passed to this function must have previously
15808882b394SGreg Kroah-Hartman  * been created with a call to class_create().
15818882b394SGreg Kroah-Hartman  */
15824e106739SGreg Kroah-Hartman struct device *device_create(struct class *class, struct device *parent,
15834e106739SGreg Kroah-Hartman 			     dev_t devt, void *drvdata, const char *fmt, ...)
15848882b394SGreg Kroah-Hartman {
15858882b394SGreg Kroah-Hartman 	va_list vargs;
15868882b394SGreg Kroah-Hartman 	struct device *dev;
15878882b394SGreg Kroah-Hartman 
15888882b394SGreg Kroah-Hartman 	va_start(vargs, fmt);
15898882b394SGreg Kroah-Hartman 	dev = device_create_vargs(class, parent, devt, drvdata, fmt, vargs);
15908882b394SGreg Kroah-Hartman 	va_end(vargs);
15918882b394SGreg Kroah-Hartman 	return dev;
15928882b394SGreg Kroah-Hartman }
15934e106739SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(device_create);
15948882b394SGreg Kroah-Hartman 
1595cd35449bSDave Young static int __match_devt(struct device *dev, void *data)
159623681e47SGreg Kroah-Hartman {
1597cd35449bSDave Young 	dev_t *devt = data;
159823681e47SGreg Kroah-Hartman 
1599cd35449bSDave Young 	return dev->devt == *devt;
1600775b64d2SRafael J. Wysocki }
160123681e47SGreg Kroah-Hartman 
1602775b64d2SRafael J. Wysocki /**
1603775b64d2SRafael J. Wysocki  * device_destroy - removes a device that was created with device_create()
1604775b64d2SRafael J. Wysocki  * @class: pointer to the struct class that this device was registered with
1605775b64d2SRafael J. Wysocki  * @devt: the dev_t of the device that was previously registered
1606775b64d2SRafael J. Wysocki  *
1607775b64d2SRafael J. Wysocki  * This call unregisters and cleans up a device that was created with a
1608775b64d2SRafael J. Wysocki  * call to device_create().
1609775b64d2SRafael J. Wysocki  */
1610775b64d2SRafael J. Wysocki void device_destroy(struct class *class, dev_t devt)
1611775b64d2SRafael J. Wysocki {
1612775b64d2SRafael J. Wysocki 	struct device *dev;
1613775b64d2SRafael J. Wysocki 
1614695794aeSGreg Kroah-Hartman 	dev = class_find_device(class, NULL, &devt, __match_devt);
1615cd35449bSDave Young 	if (dev) {
1616cd35449bSDave Young 		put_device(dev);
161723681e47SGreg Kroah-Hartman 		device_unregister(dev);
161823681e47SGreg Kroah-Hartman 	}
1619cd35449bSDave Young }
162023681e47SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(device_destroy);
1621a2de48caSGreg Kroah-Hartman 
1622a2de48caSGreg Kroah-Hartman /**
1623a2de48caSGreg Kroah-Hartman  * device_rename - renames a device
1624a2de48caSGreg Kroah-Hartman  * @dev: the pointer to the struct device to be renamed
1625a2de48caSGreg Kroah-Hartman  * @new_name: the new name of the device
1626030c1d2bSEric W. Biederman  *
1627030c1d2bSEric W. Biederman  * It is the responsibility of the caller to provide mutual
1628030c1d2bSEric W. Biederman  * exclusion between two different calls of device_rename
1629030c1d2bSEric W. Biederman  * on the same device to ensure that new_name is valid and
1630030c1d2bSEric W. Biederman  * won't conflict with other devices.
1631c6c0ac66SMichael Ellerman  *
1632a5462516STimur Tabi  * Note: Don't call this function.  Currently, the networking layer calls this
1633a5462516STimur Tabi  * function, but that will change.  The following text from Kay Sievers offers
1634a5462516STimur Tabi  * some insight:
1635a5462516STimur Tabi  *
1636a5462516STimur Tabi  * Renaming devices is racy at many levels, symlinks and other stuff are not
1637a5462516STimur Tabi  * replaced atomically, and you get a "move" uevent, but it's not easy to
1638a5462516STimur Tabi  * connect the event to the old and new device. Device nodes are not renamed at
1639a5462516STimur Tabi  * all, there isn't even support for that in the kernel now.
1640a5462516STimur Tabi  *
1641a5462516STimur Tabi  * In the meantime, during renaming, your target name might be taken by another
1642a5462516STimur Tabi  * driver, creating conflicts. Or the old name is taken directly after you
1643a5462516STimur Tabi  * renamed it -- then you get events for the same DEVPATH, before you even see
1644a5462516STimur Tabi  * the "move" event. It's just a mess, and nothing new should ever rely on
1645a5462516STimur Tabi  * kernel device renaming. Besides that, it's not even implemented now for
1646a5462516STimur Tabi  * other things than (driver-core wise very simple) network devices.
1647a5462516STimur Tabi  *
1648a5462516STimur Tabi  * We are currently about to change network renaming in udev to completely
1649a5462516STimur Tabi  * disallow renaming of devices in the same namespace as the kernel uses,
1650a5462516STimur Tabi  * because we can't solve the problems properly, that arise with swapping names
1651a5462516STimur Tabi  * of multiple interfaces without races. Means, renaming of eth[0-9]* will only
1652a5462516STimur Tabi  * be allowed to some other name than eth[0-9]*, for the aforementioned
1653a5462516STimur Tabi  * reasons.
1654a5462516STimur Tabi  *
1655a5462516STimur Tabi  * Make up a "real" name in the driver before you register anything, or add
1656a5462516STimur Tabi  * some other attributes for userspace to find the device, or use udev to add
1657a5462516STimur Tabi  * symlinks -- but never rename kernel devices later, it's a complete mess. We
1658a5462516STimur Tabi  * don't even want to get into that and try to implement the missing pieces in
1659a5462516STimur Tabi  * the core. We really have other pieces to fix in the driver core mess. :)
1660a2de48caSGreg Kroah-Hartman  */
16616937e8f8SJohannes Berg int device_rename(struct device *dev, const char *new_name)
1662a2de48caSGreg Kroah-Hartman {
1663a2de48caSGreg Kroah-Hartman 	char *old_class_name = NULL;
1664a2de48caSGreg Kroah-Hartman 	char *new_class_name = NULL;
16652ee97cafSCornelia Huck 	char *old_device_name = NULL;
1666a2de48caSGreg Kroah-Hartman 	int error;
1667a2de48caSGreg Kroah-Hartman 
1668a2de48caSGreg Kroah-Hartman 	dev = get_device(dev);
1669a2de48caSGreg Kroah-Hartman 	if (!dev)
1670a2de48caSGreg Kroah-Hartman 		return -EINVAL;
1671a2de48caSGreg Kroah-Hartman 
16721e0b2cf9SKay Sievers 	pr_debug("device: '%s': %s: renaming to '%s'\n", dev_name(dev),
16732b3a302aSHarvey Harrison 		 __func__, new_name);
1674a2de48caSGreg Kroah-Hartman 
16751fa5ae85SKay Sievers 	old_device_name = kstrdup(dev_name(dev), GFP_KERNEL);
16762ee97cafSCornelia Huck 	if (!old_device_name) {
1677952ab431SJesper Juhl 		error = -ENOMEM;
16782ee97cafSCornelia Huck 		goto out;
1679952ab431SJesper Juhl 	}
1680a2de48caSGreg Kroah-Hartman 
1681f349cf34SEric W. Biederman 	if (dev->class) {
16826b6e39a6SKay Sievers 		error = sysfs_rename_link(&dev->class->p->subsys.kobj,
1683f349cf34SEric W. Biederman 			&dev->kobj, old_device_name, new_name);
1684f349cf34SEric W. Biederman 		if (error)
1685f349cf34SEric W. Biederman 			goto out;
1686f349cf34SEric W. Biederman 	}
168739aba963SKay Sievers 
1688a2de48caSGreg Kroah-Hartman 	error = kobject_rename(&dev->kobj, new_name);
16891fa5ae85SKay Sievers 	if (error)
16902ee97cafSCornelia Huck 		goto out;
1691a2de48caSGreg Kroah-Hartman 
16922ee97cafSCornelia Huck out:
1693a2de48caSGreg Kroah-Hartman 	put_device(dev);
1694a2de48caSGreg Kroah-Hartman 
1695a2de48caSGreg Kroah-Hartman 	kfree(new_class_name);
1696952ab431SJesper Juhl 	kfree(old_class_name);
16972ee97cafSCornelia Huck 	kfree(old_device_name);
1698a2de48caSGreg Kroah-Hartman 
1699a2de48caSGreg Kroah-Hartman 	return error;
1700a2de48caSGreg Kroah-Hartman }
1701a2807dbcSJohannes Berg EXPORT_SYMBOL_GPL(device_rename);
17028a82472fSCornelia Huck 
17038a82472fSCornelia Huck static int device_move_class_links(struct device *dev,
17048a82472fSCornelia Huck 				   struct device *old_parent,
17058a82472fSCornelia Huck 				   struct device *new_parent)
17068a82472fSCornelia Huck {
1707f7f3461dSGreg Kroah-Hartman 	int error = 0;
17088a82472fSCornelia Huck 
1709f7f3461dSGreg Kroah-Hartman 	if (old_parent)
1710f7f3461dSGreg Kroah-Hartman 		sysfs_remove_link(&dev->kobj, "device");
1711f7f3461dSGreg Kroah-Hartman 	if (new_parent)
1712f7f3461dSGreg Kroah-Hartman 		error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
1713f7f3461dSGreg Kroah-Hartman 					  "device");
1714f7f3461dSGreg Kroah-Hartman 	return error;
17158a82472fSCornelia Huck }
17168a82472fSCornelia Huck 
17178a82472fSCornelia Huck /**
17188a82472fSCornelia Huck  * device_move - moves a device to a new parent
17198a82472fSCornelia Huck  * @dev: the pointer to the struct device to be moved
1720c744aeaeSCornelia Huck  * @new_parent: the new parent of the device (can by NULL)
1721ffa6a705SCornelia Huck  * @dpm_order: how to reorder the dpm_list
17228a82472fSCornelia Huck  */
1723ffa6a705SCornelia Huck int device_move(struct device *dev, struct device *new_parent,
1724ffa6a705SCornelia Huck 		enum dpm_order dpm_order)
17258a82472fSCornelia Huck {
17268a82472fSCornelia Huck 	int error;
17278a82472fSCornelia Huck 	struct device *old_parent;
1728c744aeaeSCornelia Huck 	struct kobject *new_parent_kobj;
17298a82472fSCornelia Huck 
17308a82472fSCornelia Huck 	dev = get_device(dev);
17318a82472fSCornelia Huck 	if (!dev)
17328a82472fSCornelia Huck 		return -EINVAL;
17338a82472fSCornelia Huck 
1734ffa6a705SCornelia Huck 	device_pm_lock();
17358a82472fSCornelia Huck 	new_parent = get_device(new_parent);
1736c744aeaeSCornelia Huck 	new_parent_kobj = get_device_parent(dev, new_parent);
173763b6971aSCornelia Huck 
17381e0b2cf9SKay Sievers 	pr_debug("device: '%s': %s: moving to '%s'\n", dev_name(dev),
17391e0b2cf9SKay Sievers 		 __func__, new_parent ? dev_name(new_parent) : "<NULL>");
1740c744aeaeSCornelia Huck 	error = kobject_move(&dev->kobj, new_parent_kobj);
17418a82472fSCornelia Huck 	if (error) {
174263b6971aSCornelia Huck 		cleanup_glue_dir(dev, new_parent_kobj);
17438a82472fSCornelia Huck 		put_device(new_parent);
17448a82472fSCornelia Huck 		goto out;
17458a82472fSCornelia Huck 	}
17468a82472fSCornelia Huck 	old_parent = dev->parent;
17478a82472fSCornelia Huck 	dev->parent = new_parent;
17488a82472fSCornelia Huck 	if (old_parent)
1749f791b8c8SGreg Kroah-Hartman 		klist_remove(&dev->p->knode_parent);
17500d358f22SYinghai Lu 	if (new_parent) {
1751f791b8c8SGreg Kroah-Hartman 		klist_add_tail(&dev->p->knode_parent,
1752f791b8c8SGreg Kroah-Hartman 			       &new_parent->p->klist_children);
17530d358f22SYinghai Lu 		set_dev_node(dev, dev_to_node(new_parent));
17540d358f22SYinghai Lu 	}
17550d358f22SYinghai Lu 
17568a82472fSCornelia Huck 	if (!dev->class)
17578a82472fSCornelia Huck 		goto out_put;
17588a82472fSCornelia Huck 	error = device_move_class_links(dev, old_parent, new_parent);
17598a82472fSCornelia Huck 	if (error) {
17608a82472fSCornelia Huck 		/* We ignore errors on cleanup since we're hosed anyway... */
17618a82472fSCornelia Huck 		device_move_class_links(dev, new_parent, old_parent);
17628a82472fSCornelia Huck 		if (!kobject_move(&dev->kobj, &old_parent->kobj)) {
1763c744aeaeSCornelia Huck 			if (new_parent)
1764f791b8c8SGreg Kroah-Hartman 				klist_remove(&dev->p->knode_parent);
17650d358f22SYinghai Lu 			dev->parent = old_parent;
17660d358f22SYinghai Lu 			if (old_parent) {
1767f791b8c8SGreg Kroah-Hartman 				klist_add_tail(&dev->p->knode_parent,
1768f791b8c8SGreg Kroah-Hartman 					       &old_parent->p->klist_children);
17690d358f22SYinghai Lu 				set_dev_node(dev, dev_to_node(old_parent));
17700d358f22SYinghai Lu 			}
17718a82472fSCornelia Huck 		}
177263b6971aSCornelia Huck 		cleanup_glue_dir(dev, new_parent_kobj);
17738a82472fSCornelia Huck 		put_device(new_parent);
17748a82472fSCornelia Huck 		goto out;
17758a82472fSCornelia Huck 	}
1776ffa6a705SCornelia Huck 	switch (dpm_order) {
1777ffa6a705SCornelia Huck 	case DPM_ORDER_NONE:
1778ffa6a705SCornelia Huck 		break;
1779ffa6a705SCornelia Huck 	case DPM_ORDER_DEV_AFTER_PARENT:
1780ffa6a705SCornelia Huck 		device_pm_move_after(dev, new_parent);
1781ffa6a705SCornelia Huck 		break;
1782ffa6a705SCornelia Huck 	case DPM_ORDER_PARENT_BEFORE_DEV:
1783ffa6a705SCornelia Huck 		device_pm_move_before(new_parent, dev);
1784ffa6a705SCornelia Huck 		break;
1785ffa6a705SCornelia Huck 	case DPM_ORDER_DEV_LAST:
1786ffa6a705SCornelia Huck 		device_pm_move_last(dev);
1787ffa6a705SCornelia Huck 		break;
1788ffa6a705SCornelia Huck 	}
17898a82472fSCornelia Huck out_put:
17908a82472fSCornelia Huck 	put_device(old_parent);
17918a82472fSCornelia Huck out:
1792ffa6a705SCornelia Huck 	device_pm_unlock();
17938a82472fSCornelia Huck 	put_device(dev);
17948a82472fSCornelia Huck 	return error;
17958a82472fSCornelia Huck }
17968a82472fSCornelia Huck EXPORT_SYMBOL_GPL(device_move);
179737b0c020SGreg Kroah-Hartman 
179837b0c020SGreg Kroah-Hartman /**
179937b0c020SGreg Kroah-Hartman  * device_shutdown - call ->shutdown() on each device to shutdown.
180037b0c020SGreg Kroah-Hartman  */
180137b0c020SGreg Kroah-Hartman void device_shutdown(void)
180237b0c020SGreg Kroah-Hartman {
18036245838fSHugh Daschbach 	struct device *dev;
180437b0c020SGreg Kroah-Hartman 
18056245838fSHugh Daschbach 	spin_lock(&devices_kset->list_lock);
18066245838fSHugh Daschbach 	/*
18076245838fSHugh Daschbach 	 * Walk the devices list backward, shutting down each in turn.
18086245838fSHugh Daschbach 	 * Beware that device unplug events may also start pulling
18096245838fSHugh Daschbach 	 * devices offline, even as the system is shutting down.
18106245838fSHugh Daschbach 	 */
18116245838fSHugh Daschbach 	while (!list_empty(&devices_kset->list)) {
18126245838fSHugh Daschbach 		dev = list_entry(devices_kset->list.prev, struct device,
18136245838fSHugh Daschbach 				kobj.entry);
18146245838fSHugh Daschbach 		get_device(dev);
18156245838fSHugh Daschbach 		/*
18166245838fSHugh Daschbach 		 * Make sure the device is off the kset list, in the
18176245838fSHugh Daschbach 		 * event that dev->*->shutdown() doesn't remove it.
18186245838fSHugh Daschbach 		 */
18196245838fSHugh Daschbach 		list_del_init(&dev->kobj.entry);
18206245838fSHugh Daschbach 		spin_unlock(&devices_kset->list_lock);
1821fe6b91f4SAlan Stern 
1822fe6b91f4SAlan Stern 		/* Don't allow any more runtime suspends */
1823fe6b91f4SAlan Stern 		pm_runtime_get_noresume(dev);
1824fe6b91f4SAlan Stern 		pm_runtime_barrier(dev);
18256245838fSHugh Daschbach 
182637b0c020SGreg Kroah-Hartman 		if (dev->bus && dev->bus->shutdown) {
182737b0c020SGreg Kroah-Hartman 			dev_dbg(dev, "shutdown\n");
182837b0c020SGreg Kroah-Hartman 			dev->bus->shutdown(dev);
182937b0c020SGreg Kroah-Hartman 		} else if (dev->driver && dev->driver->shutdown) {
183037b0c020SGreg Kroah-Hartman 			dev_dbg(dev, "shutdown\n");
183137b0c020SGreg Kroah-Hartman 			dev->driver->shutdown(dev);
183237b0c020SGreg Kroah-Hartman 		}
18336245838fSHugh Daschbach 		put_device(dev);
18346245838fSHugh Daschbach 
18356245838fSHugh Daschbach 		spin_lock(&devices_kset->list_lock);
183637b0c020SGreg Kroah-Hartman 	}
18376245838fSHugh Daschbach 	spin_unlock(&devices_kset->list_lock);
1838401097eaSShaohua Li 	async_synchronize_full();
183937b0c020SGreg Kroah-Hartman }
184099bcf217SJoe Perches 
184199bcf217SJoe Perches /*
184299bcf217SJoe Perches  * Device logging functions
184399bcf217SJoe Perches  */
184499bcf217SJoe Perches 
184599bcf217SJoe Perches #ifdef CONFIG_PRINTK
184699bcf217SJoe Perches 
1847cbc46635SJoe Perches int __dev_printk(const char *level, const struct device *dev,
184899bcf217SJoe Perches 		 struct va_format *vaf)
184999bcf217SJoe Perches {
185099bcf217SJoe Perches 	if (!dev)
185199bcf217SJoe Perches 		return printk("%s(NULL device *): %pV", level, vaf);
185299bcf217SJoe Perches 
185399bcf217SJoe Perches 	return printk("%s%s %s: %pV",
185499bcf217SJoe Perches 		      level, dev_driver_string(dev), dev_name(dev), vaf);
185599bcf217SJoe Perches }
1856cbc46635SJoe Perches EXPORT_SYMBOL(__dev_printk);
185799bcf217SJoe Perches 
185899bcf217SJoe Perches int dev_printk(const char *level, const struct device *dev,
185999bcf217SJoe Perches 	       const char *fmt, ...)
186099bcf217SJoe Perches {
186199bcf217SJoe Perches 	struct va_format vaf;
186299bcf217SJoe Perches 	va_list args;
186399bcf217SJoe Perches 	int r;
186499bcf217SJoe Perches 
186599bcf217SJoe Perches 	va_start(args, fmt);
186699bcf217SJoe Perches 
186799bcf217SJoe Perches 	vaf.fmt = fmt;
186899bcf217SJoe Perches 	vaf.va = &args;
186999bcf217SJoe Perches 
187099bcf217SJoe Perches 	r = __dev_printk(level, dev, &vaf);
187199bcf217SJoe Perches 	va_end(args);
187299bcf217SJoe Perches 
187399bcf217SJoe Perches 	return r;
187499bcf217SJoe Perches }
187599bcf217SJoe Perches EXPORT_SYMBOL(dev_printk);
187699bcf217SJoe Perches 
187799bcf217SJoe Perches #define define_dev_printk_level(func, kern_level)		\
187899bcf217SJoe Perches int func(const struct device *dev, const char *fmt, ...)	\
187999bcf217SJoe Perches {								\
188099bcf217SJoe Perches 	struct va_format vaf;					\
188199bcf217SJoe Perches 	va_list args;						\
188299bcf217SJoe Perches 	int r;							\
188399bcf217SJoe Perches 								\
188499bcf217SJoe Perches 	va_start(args, fmt);					\
188599bcf217SJoe Perches 								\
188699bcf217SJoe Perches 	vaf.fmt = fmt;						\
188799bcf217SJoe Perches 	vaf.va = &args;						\
188899bcf217SJoe Perches 								\
188999bcf217SJoe Perches 	r = __dev_printk(kern_level, dev, &vaf);		\
189099bcf217SJoe Perches 	va_end(args);						\
189199bcf217SJoe Perches 								\
189299bcf217SJoe Perches 	return r;						\
189399bcf217SJoe Perches }								\
189499bcf217SJoe Perches EXPORT_SYMBOL(func);
189599bcf217SJoe Perches 
189699bcf217SJoe Perches define_dev_printk_level(dev_emerg, KERN_EMERG);
189799bcf217SJoe Perches define_dev_printk_level(dev_alert, KERN_ALERT);
189899bcf217SJoe Perches define_dev_printk_level(dev_crit, KERN_CRIT);
189999bcf217SJoe Perches define_dev_printk_level(dev_err, KERN_ERR);
190099bcf217SJoe Perches define_dev_printk_level(dev_warn, KERN_WARNING);
190199bcf217SJoe Perches define_dev_printk_level(dev_notice, KERN_NOTICE);
190299bcf217SJoe Perches define_dev_printk_level(_dev_info, KERN_INFO);
190399bcf217SJoe Perches 
190499bcf217SJoe Perches #endif
1905