xref: /linux-6.15/drivers/base/core.c (revision a4e2400a)
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 <[email protected]>
764bb5d2cSGreg Kroah-Hartman  * Copyright (c) 2006 Novell, Inc.
81da177e4SLinus Torvalds  *
91da177e4SLinus Torvalds  * This file is released under the GPLv2
101da177e4SLinus Torvalds  *
111da177e4SLinus Torvalds  */
121da177e4SLinus Torvalds 
131da177e4SLinus Torvalds #include <linux/device.h>
141da177e4SLinus Torvalds #include <linux/err.h>
151da177e4SLinus Torvalds #include <linux/init.h>
161da177e4SLinus Torvalds #include <linux/module.h>
171da177e4SLinus Torvalds #include <linux/slab.h>
181da177e4SLinus Torvalds #include <linux/string.h>
1923681e47SGreg Kroah-Hartman #include <linux/kdev_t.h>
20116af378SBenjamin Herrenschmidt #include <linux/notifier.h>
2107d57a32SGrant Likely #include <linux/of.h>
2207d57a32SGrant Likely #include <linux/of_device.h>
23da231fd5SKay Sievers #include <linux/genhd.h>
24815d2d50SAndrew Morton #include <linux/kallsyms.h>
25f75b1c60SDave Young #include <linux/mutex.h>
26401097eaSShaohua Li #include <linux/async.h>
27af8db150SPeter Chen #include <linux/pm_runtime.h>
28c4e00daaSKay Sievers #include <linux/netdevice.h>
291da177e4SLinus Torvalds 
301da177e4SLinus Torvalds #include "base.h"
311da177e4SLinus Torvalds #include "power/power.h"
321da177e4SLinus Torvalds 
33e52eec13SAndi Kleen #ifdef CONFIG_SYSFS_DEPRECATED
34e52eec13SAndi Kleen #ifdef CONFIG_SYSFS_DEPRECATED_V2
35e52eec13SAndi Kleen long sysfs_deprecated = 1;
36e52eec13SAndi Kleen #else
37e52eec13SAndi Kleen long sysfs_deprecated = 0;
38e52eec13SAndi Kleen #endif
39e52eec13SAndi Kleen static __init int sysfs_deprecated_setup(char *arg)
40e52eec13SAndi Kleen {
41e52eec13SAndi Kleen 	return strict_strtol(arg, 10, &sysfs_deprecated);
42e52eec13SAndi Kleen }
43e52eec13SAndi Kleen early_param("sysfs.deprecated", sysfs_deprecated_setup);
44e52eec13SAndi Kleen #endif
45e52eec13SAndi Kleen 
461da177e4SLinus Torvalds int (*platform_notify)(struct device *dev) = NULL;
471da177e4SLinus Torvalds int (*platform_notify_remove)(struct device *dev) = NULL;
48e105b8bfSDan Williams static struct kobject *dev_kobj;
49e105b8bfSDan Williams struct kobject *sysfs_dev_char_kobj;
50e105b8bfSDan Williams struct kobject *sysfs_dev_block_kobj;
511da177e4SLinus Torvalds 
524e886c29SGreg Kroah-Hartman #ifdef CONFIG_BLOCK
534e886c29SGreg Kroah-Hartman static inline int device_is_not_partition(struct device *dev)
544e886c29SGreg Kroah-Hartman {
554e886c29SGreg Kroah-Hartman 	return !(dev->type == &part_type);
564e886c29SGreg Kroah-Hartman }
574e886c29SGreg Kroah-Hartman #else
584e886c29SGreg Kroah-Hartman static inline int device_is_not_partition(struct device *dev)
594e886c29SGreg Kroah-Hartman {
604e886c29SGreg Kroah-Hartman 	return 1;
614e886c29SGreg Kroah-Hartman }
624e886c29SGreg Kroah-Hartman #endif
631da177e4SLinus Torvalds 
643e95637aSAlan Stern /**
653e95637aSAlan Stern  * dev_driver_string - Return a device's driver name, if at all possible
663e95637aSAlan Stern  * @dev: struct device to get the name of
673e95637aSAlan Stern  *
683e95637aSAlan Stern  * Will return the device's driver's name if it is bound to a device.  If
699169c012Syan  * the device is not bound to a driver, it will return the name of the bus
703e95637aSAlan Stern  * it is attached to.  If it is not attached to a bus either, an empty
713e95637aSAlan Stern  * string will be returned.
723e95637aSAlan Stern  */
73bf9ca69fSJean Delvare const char *dev_driver_string(const struct device *dev)
743e95637aSAlan Stern {
753589972eSAlan Stern 	struct device_driver *drv;
763589972eSAlan Stern 
773589972eSAlan Stern 	/* dev->driver can change to NULL underneath us because of unbinding,
783589972eSAlan Stern 	 * so be careful about accessing it.  dev->bus and dev->class should
793589972eSAlan Stern 	 * never change once they are set, so they don't need special care.
803589972eSAlan Stern 	 */
813589972eSAlan Stern 	drv = ACCESS_ONCE(dev->driver);
823589972eSAlan Stern 	return drv ? drv->name :
83a456b702SJean Delvare 			(dev->bus ? dev->bus->name :
84a456b702SJean Delvare 			(dev->class ? dev->class->name : ""));
853e95637aSAlan Stern }
86310a922dSMatthew Wilcox EXPORT_SYMBOL(dev_driver_string);
873e95637aSAlan Stern 
881da177e4SLinus Torvalds #define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr)
891da177e4SLinus Torvalds 
904a3ad20cSGreg Kroah-Hartman static ssize_t dev_attr_show(struct kobject *kobj, struct attribute *attr,
914a3ad20cSGreg Kroah-Hartman 			     char *buf)
921da177e4SLinus Torvalds {
931da177e4SLinus Torvalds 	struct device_attribute *dev_attr = to_dev_attr(attr);
94b0d1f807SLars-Peter Clausen 	struct device *dev = kobj_to_dev(kobj);
954a0c20bfSDmitry Torokhov 	ssize_t ret = -EIO;
961da177e4SLinus Torvalds 
971da177e4SLinus Torvalds 	if (dev_attr->show)
9854b6f35cSYani Ioannou 		ret = dev_attr->show(dev, dev_attr, buf);
99815d2d50SAndrew Morton 	if (ret >= (ssize_t)PAGE_SIZE) {
10053a9c87eSGreg Kroah-Hartman 		print_symbol("dev_attr_show: %s returned bad count\n",
10153a9c87eSGreg Kroah-Hartman 				(unsigned long)dev_attr->show);
102815d2d50SAndrew Morton 	}
1031da177e4SLinus Torvalds 	return ret;
1041da177e4SLinus Torvalds }
1051da177e4SLinus Torvalds 
1064a3ad20cSGreg Kroah-Hartman static ssize_t dev_attr_store(struct kobject *kobj, struct attribute *attr,
1071da177e4SLinus Torvalds 			      const char *buf, size_t count)
1081da177e4SLinus Torvalds {
1091da177e4SLinus Torvalds 	struct device_attribute *dev_attr = to_dev_attr(attr);
110b0d1f807SLars-Peter Clausen 	struct device *dev = kobj_to_dev(kobj);
1114a0c20bfSDmitry Torokhov 	ssize_t ret = -EIO;
1121da177e4SLinus Torvalds 
1131da177e4SLinus Torvalds 	if (dev_attr->store)
11454b6f35cSYani Ioannou 		ret = dev_attr->store(dev, dev_attr, buf, count);
1151da177e4SLinus Torvalds 	return ret;
1161da177e4SLinus Torvalds }
1171da177e4SLinus Torvalds 
11852cf25d0SEmese Revfy static const struct sysfs_ops dev_sysfs_ops = {
1191da177e4SLinus Torvalds 	.show	= dev_attr_show,
1201da177e4SLinus Torvalds 	.store	= dev_attr_store,
1211da177e4SLinus Torvalds };
1221da177e4SLinus Torvalds 
123ca22e56dSKay Sievers #define to_ext_attr(x) container_of(x, struct dev_ext_attribute, attr)
124ca22e56dSKay Sievers 
125ca22e56dSKay Sievers ssize_t device_store_ulong(struct device *dev,
126ca22e56dSKay Sievers 			   struct device_attribute *attr,
127ca22e56dSKay Sievers 			   const char *buf, size_t size)
128ca22e56dSKay Sievers {
129ca22e56dSKay Sievers 	struct dev_ext_attribute *ea = to_ext_attr(attr);
130ca22e56dSKay Sievers 	char *end;
131ca22e56dSKay Sievers 	unsigned long new = simple_strtoul(buf, &end, 0);
132ca22e56dSKay Sievers 	if (end == buf)
133ca22e56dSKay Sievers 		return -EINVAL;
134ca22e56dSKay Sievers 	*(unsigned long *)(ea->var) = new;
135ca22e56dSKay Sievers 	/* Always return full write size even if we didn't consume all */
136ca22e56dSKay Sievers 	return size;
137ca22e56dSKay Sievers }
138ca22e56dSKay Sievers EXPORT_SYMBOL_GPL(device_store_ulong);
139ca22e56dSKay Sievers 
140ca22e56dSKay Sievers ssize_t device_show_ulong(struct device *dev,
141ca22e56dSKay Sievers 			  struct device_attribute *attr,
142ca22e56dSKay Sievers 			  char *buf)
143ca22e56dSKay Sievers {
144ca22e56dSKay Sievers 	struct dev_ext_attribute *ea = to_ext_attr(attr);
145ca22e56dSKay Sievers 	return snprintf(buf, PAGE_SIZE, "%lx\n", *(unsigned long *)(ea->var));
146ca22e56dSKay Sievers }
147ca22e56dSKay Sievers EXPORT_SYMBOL_GPL(device_show_ulong);
148ca22e56dSKay Sievers 
149ca22e56dSKay Sievers ssize_t device_store_int(struct device *dev,
150ca22e56dSKay Sievers 			 struct device_attribute *attr,
151ca22e56dSKay Sievers 			 const char *buf, size_t size)
152ca22e56dSKay Sievers {
153ca22e56dSKay Sievers 	struct dev_ext_attribute *ea = to_ext_attr(attr);
154ca22e56dSKay Sievers 	char *end;
155ca22e56dSKay Sievers 	long new = simple_strtol(buf, &end, 0);
156ca22e56dSKay Sievers 	if (end == buf || new > INT_MAX || new < INT_MIN)
157ca22e56dSKay Sievers 		return -EINVAL;
158ca22e56dSKay Sievers 	*(int *)(ea->var) = new;
159ca22e56dSKay Sievers 	/* Always return full write size even if we didn't consume all */
160ca22e56dSKay Sievers 	return size;
161ca22e56dSKay Sievers }
162ca22e56dSKay Sievers EXPORT_SYMBOL_GPL(device_store_int);
163ca22e56dSKay Sievers 
164ca22e56dSKay Sievers ssize_t device_show_int(struct device *dev,
165ca22e56dSKay Sievers 			struct device_attribute *attr,
166ca22e56dSKay Sievers 			char *buf)
167ca22e56dSKay Sievers {
168ca22e56dSKay Sievers 	struct dev_ext_attribute *ea = to_ext_attr(attr);
169ca22e56dSKay Sievers 
170ca22e56dSKay Sievers 	return snprintf(buf, PAGE_SIZE, "%d\n", *(int *)(ea->var));
171ca22e56dSKay Sievers }
172ca22e56dSKay Sievers EXPORT_SYMBOL_GPL(device_show_int);
1731da177e4SLinus Torvalds 
17491872392SBorislav Petkov ssize_t device_store_bool(struct device *dev, struct device_attribute *attr,
17591872392SBorislav Petkov 			  const char *buf, size_t size)
17691872392SBorislav Petkov {
17791872392SBorislav Petkov 	struct dev_ext_attribute *ea = to_ext_attr(attr);
17891872392SBorislav Petkov 
17991872392SBorislav Petkov 	if (strtobool(buf, ea->var) < 0)
18091872392SBorislav Petkov 		return -EINVAL;
18191872392SBorislav Petkov 
18291872392SBorislav Petkov 	return size;
18391872392SBorislav Petkov }
18491872392SBorislav Petkov EXPORT_SYMBOL_GPL(device_store_bool);
18591872392SBorislav Petkov 
18691872392SBorislav Petkov ssize_t device_show_bool(struct device *dev, struct device_attribute *attr,
18791872392SBorislav Petkov 			 char *buf)
18891872392SBorislav Petkov {
18991872392SBorislav Petkov 	struct dev_ext_attribute *ea = to_ext_attr(attr);
19091872392SBorislav Petkov 
19191872392SBorislav Petkov 	return snprintf(buf, PAGE_SIZE, "%d\n", *(bool *)(ea->var));
19291872392SBorislav Petkov }
19391872392SBorislav Petkov EXPORT_SYMBOL_GPL(device_show_bool);
19491872392SBorislav Petkov 
1951da177e4SLinus Torvalds /**
1961da177e4SLinus Torvalds  *	device_release - free device structure.
1971da177e4SLinus Torvalds  *	@kobj:	device's kobject.
1981da177e4SLinus Torvalds  *
1991da177e4SLinus Torvalds  *	This is called once the reference count for the object
2001da177e4SLinus Torvalds  *	reaches 0. We forward the call to the device's release
2011da177e4SLinus Torvalds  *	method, which should handle actually freeing the structure.
2021da177e4SLinus Torvalds  */
2031da177e4SLinus Torvalds static void device_release(struct kobject *kobj)
2041da177e4SLinus Torvalds {
205b0d1f807SLars-Peter Clausen 	struct device *dev = kobj_to_dev(kobj);
206fb069a5dSGreg Kroah-Hartman 	struct device_private *p = dev->p;
2071da177e4SLinus Torvalds 
208a525a3ddSMing Lei 	/*
209a525a3ddSMing Lei 	 * Some platform devices are driven without driver attached
210a525a3ddSMing Lei 	 * and managed resources may have been acquired.  Make sure
211a525a3ddSMing Lei 	 * all resources are released.
212a525a3ddSMing Lei 	 *
213a525a3ddSMing Lei 	 * Drivers still can add resources into device after device
214a525a3ddSMing Lei 	 * is deleted but alive, so release devres here to avoid
215a525a3ddSMing Lei 	 * possible memory leak.
216a525a3ddSMing Lei 	 */
217a525a3ddSMing Lei 	devres_release_all(dev);
218a525a3ddSMing Lei 
2191da177e4SLinus Torvalds 	if (dev->release)
2201da177e4SLinus Torvalds 		dev->release(dev);
221f9f852dfSKay Sievers 	else if (dev->type && dev->type->release)
222f9f852dfSKay Sievers 		dev->type->release(dev);
2232620efefSGreg Kroah-Hartman 	else if (dev->class && dev->class->dev_release)
2242620efefSGreg Kroah-Hartman 		dev->class->dev_release(dev);
225f810a5cfSArjan van de Ven 	else
226f810a5cfSArjan van de Ven 		WARN(1, KERN_ERR "Device '%s' does not have a release() "
2274a3ad20cSGreg Kroah-Hartman 			"function, it is broken and must be fixed.\n",
2281e0b2cf9SKay Sievers 			dev_name(dev));
229fb069a5dSGreg Kroah-Hartman 	kfree(p);
2301da177e4SLinus Torvalds }
2311da177e4SLinus Torvalds 
232bc451f20SEric W. Biederman static const void *device_namespace(struct kobject *kobj)
233bc451f20SEric W. Biederman {
234b0d1f807SLars-Peter Clausen 	struct device *dev = kobj_to_dev(kobj);
235bc451f20SEric W. Biederman 	const void *ns = NULL;
236bc451f20SEric W. Biederman 
237bc451f20SEric W. Biederman 	if (dev->class && dev->class->ns_type)
238bc451f20SEric W. Biederman 		ns = dev->class->namespace(dev);
239bc451f20SEric W. Biederman 
240bc451f20SEric W. Biederman 	return ns;
241bc451f20SEric W. Biederman }
242bc451f20SEric W. Biederman 
2438f4afc41SGreg Kroah-Hartman static struct kobj_type device_ktype = {
2441da177e4SLinus Torvalds 	.release	= device_release,
2451da177e4SLinus Torvalds 	.sysfs_ops	= &dev_sysfs_ops,
246bc451f20SEric W. Biederman 	.namespace	= device_namespace,
2471da177e4SLinus Torvalds };
2481da177e4SLinus Torvalds 
2491da177e4SLinus Torvalds 
250312c004dSKay Sievers static int dev_uevent_filter(struct kset *kset, struct kobject *kobj)
2511da177e4SLinus Torvalds {
2521da177e4SLinus Torvalds 	struct kobj_type *ktype = get_ktype(kobj);
2531da177e4SLinus Torvalds 
2548f4afc41SGreg Kroah-Hartman 	if (ktype == &device_ktype) {
255b0d1f807SLars-Peter Clausen 		struct device *dev = kobj_to_dev(kobj);
2561da177e4SLinus Torvalds 		if (dev->bus)
2571da177e4SLinus Torvalds 			return 1;
25823681e47SGreg Kroah-Hartman 		if (dev->class)
25923681e47SGreg Kroah-Hartman 			return 1;
2601da177e4SLinus Torvalds 	}
2611da177e4SLinus Torvalds 	return 0;
2621da177e4SLinus Torvalds }
2631da177e4SLinus Torvalds 
264312c004dSKay Sievers static const char *dev_uevent_name(struct kset *kset, struct kobject *kobj)
2651da177e4SLinus Torvalds {
266b0d1f807SLars-Peter Clausen 	struct device *dev = kobj_to_dev(kobj);
2671da177e4SLinus Torvalds 
26823681e47SGreg Kroah-Hartman 	if (dev->bus)
2691da177e4SLinus Torvalds 		return dev->bus->name;
27023681e47SGreg Kroah-Hartman 	if (dev->class)
27123681e47SGreg Kroah-Hartman 		return dev->class->name;
27223681e47SGreg Kroah-Hartman 	return NULL;
2731da177e4SLinus Torvalds }
2741da177e4SLinus Torvalds 
2757eff2e7aSKay Sievers static int dev_uevent(struct kset *kset, struct kobject *kobj,
2767eff2e7aSKay Sievers 		      struct kobj_uevent_env *env)
2771da177e4SLinus Torvalds {
278b0d1f807SLars-Peter Clausen 	struct device *dev = kobj_to_dev(kobj);
2791da177e4SLinus Torvalds 	int retval = 0;
2801da177e4SLinus Torvalds 
2816fcf53acSKay Sievers 	/* add device node properties if present */
28223681e47SGreg Kroah-Hartman 	if (MAJOR(dev->devt)) {
2836fcf53acSKay Sievers 		const char *tmp;
2846fcf53acSKay Sievers 		const char *name;
2852c9ede55SAl Viro 		umode_t mode = 0;
2864e4098a3SGreg Kroah-Hartman 		kuid_t uid = GLOBAL_ROOT_UID;
2874e4098a3SGreg Kroah-Hartman 		kgid_t gid = GLOBAL_ROOT_GID;
2886fcf53acSKay Sievers 
2897eff2e7aSKay Sievers 		add_uevent_var(env, "MAJOR=%u", MAJOR(dev->devt));
2907eff2e7aSKay Sievers 		add_uevent_var(env, "MINOR=%u", MINOR(dev->devt));
2913c2670e6SKay Sievers 		name = device_get_devnode(dev, &mode, &uid, &gid, &tmp);
2926fcf53acSKay Sievers 		if (name) {
2936fcf53acSKay Sievers 			add_uevent_var(env, "DEVNAME=%s", name);
294e454cea2SKay Sievers 			if (mode)
295e454cea2SKay Sievers 				add_uevent_var(env, "DEVMODE=%#o", mode & 0777);
2964e4098a3SGreg Kroah-Hartman 			if (!uid_eq(uid, GLOBAL_ROOT_UID))
2974e4098a3SGreg Kroah-Hartman 				add_uevent_var(env, "DEVUID=%u", from_kuid(&init_user_ns, uid));
2984e4098a3SGreg Kroah-Hartman 			if (!gid_eq(gid, GLOBAL_ROOT_GID))
2994e4098a3SGreg Kroah-Hartman 				add_uevent_var(env, "DEVGID=%u", from_kgid(&init_user_ns, gid));
3003c2670e6SKay Sievers 			kfree(tmp);
3016fcf53acSKay Sievers 		}
30223681e47SGreg Kroah-Hartman 	}
30323681e47SGreg Kroah-Hartman 
304414264f9SKay Sievers 	if (dev->type && dev->type->name)
3057eff2e7aSKay Sievers 		add_uevent_var(env, "DEVTYPE=%s", dev->type->name);
306414264f9SKay Sievers 
307239378f1SKay Sievers 	if (dev->driver)
3087eff2e7aSKay Sievers 		add_uevent_var(env, "DRIVER=%s", dev->driver->name);
309239378f1SKay Sievers 
31007d57a32SGrant Likely 	/* Add common DT information about the device */
31107d57a32SGrant Likely 	of_device_uevent(dev, env);
31207d57a32SGrant Likely 
3131da177e4SLinus Torvalds 	/* have the bus specific function add its stuff */
3147eff2e7aSKay Sievers 	if (dev->bus && dev->bus->uevent) {
3157eff2e7aSKay Sievers 		retval = dev->bus->uevent(dev, env);
316f9f852dfSKay Sievers 		if (retval)
3177dc72b28SGreg Kroah-Hartman 			pr_debug("device: '%s': %s: bus uevent() returned %d\n",
3181e0b2cf9SKay Sievers 				 dev_name(dev), __func__, retval);
3191da177e4SLinus Torvalds 	}
3201da177e4SLinus Torvalds 
3212620efefSGreg Kroah-Hartman 	/* have the class specific function add its stuff */
3227eff2e7aSKay Sievers 	if (dev->class && dev->class->dev_uevent) {
3237eff2e7aSKay Sievers 		retval = dev->class->dev_uevent(dev, env);
324f9f852dfSKay Sievers 		if (retval)
3257dc72b28SGreg Kroah-Hartman 			pr_debug("device: '%s': %s: class uevent() "
3261e0b2cf9SKay Sievers 				 "returned %d\n", dev_name(dev),
3272b3a302aSHarvey Harrison 				 __func__, retval);
3282620efefSGreg Kroah-Hartman 	}
329f9f852dfSKay Sievers 
330eef35c2dSStefan Weil 	/* have the device type specific function add its stuff */
3317eff2e7aSKay Sievers 	if (dev->type && dev->type->uevent) {
3327eff2e7aSKay Sievers 		retval = dev->type->uevent(dev, env);
333f9f852dfSKay Sievers 		if (retval)
3347dc72b28SGreg Kroah-Hartman 			pr_debug("device: '%s': %s: dev_type uevent() "
3351e0b2cf9SKay Sievers 				 "returned %d\n", dev_name(dev),
3362b3a302aSHarvey Harrison 				 __func__, retval);
3372620efefSGreg Kroah-Hartman 	}
3382620efefSGreg Kroah-Hartman 
3391da177e4SLinus Torvalds 	return retval;
3401da177e4SLinus Torvalds }
3411da177e4SLinus Torvalds 
3429cd43611SEmese Revfy static const struct kset_uevent_ops device_uevent_ops = {
343312c004dSKay Sievers 	.filter =	dev_uevent_filter,
344312c004dSKay Sievers 	.name =		dev_uevent_name,
345312c004dSKay Sievers 	.uevent =	dev_uevent,
3461da177e4SLinus Torvalds };
3471da177e4SLinus Torvalds 
34816574dccSKay Sievers static ssize_t show_uevent(struct device *dev, struct device_attribute *attr,
34916574dccSKay Sievers 			   char *buf)
35016574dccSKay Sievers {
35116574dccSKay Sievers 	struct kobject *top_kobj;
35216574dccSKay Sievers 	struct kset *kset;
3537eff2e7aSKay Sievers 	struct kobj_uevent_env *env = NULL;
35416574dccSKay Sievers 	int i;
35516574dccSKay Sievers 	size_t count = 0;
35616574dccSKay Sievers 	int retval;
35716574dccSKay Sievers 
35816574dccSKay Sievers 	/* search the kset, the device belongs to */
35916574dccSKay Sievers 	top_kobj = &dev->kobj;
3605c5daf65SKay Sievers 	while (!top_kobj->kset && top_kobj->parent)
36116574dccSKay Sievers 		top_kobj = top_kobj->parent;
36216574dccSKay Sievers 	if (!top_kobj->kset)
36316574dccSKay Sievers 		goto out;
3645c5daf65SKay Sievers 
36516574dccSKay Sievers 	kset = top_kobj->kset;
36616574dccSKay Sievers 	if (!kset->uevent_ops || !kset->uevent_ops->uevent)
36716574dccSKay Sievers 		goto out;
36816574dccSKay Sievers 
36916574dccSKay Sievers 	/* respect filter */
37016574dccSKay Sievers 	if (kset->uevent_ops && kset->uevent_ops->filter)
37116574dccSKay Sievers 		if (!kset->uevent_ops->filter(kset, &dev->kobj))
37216574dccSKay Sievers 			goto out;
37316574dccSKay Sievers 
3747eff2e7aSKay Sievers 	env = kzalloc(sizeof(struct kobj_uevent_env), GFP_KERNEL);
3757eff2e7aSKay Sievers 	if (!env)
376c7308c81SGreg Kroah-Hartman 		return -ENOMEM;
377c7308c81SGreg Kroah-Hartman 
37816574dccSKay Sievers 	/* let the kset specific function add its keys */
3797eff2e7aSKay Sievers 	retval = kset->uevent_ops->uevent(kset, &dev->kobj, env);
38016574dccSKay Sievers 	if (retval)
38116574dccSKay Sievers 		goto out;
38216574dccSKay Sievers 
38316574dccSKay Sievers 	/* copy keys to file */
3847eff2e7aSKay Sievers 	for (i = 0; i < env->envp_idx; i++)
3857eff2e7aSKay Sievers 		count += sprintf(&buf[count], "%s\n", env->envp[i]);
38616574dccSKay Sievers out:
3877eff2e7aSKay Sievers 	kfree(env);
38816574dccSKay Sievers 	return count;
38916574dccSKay Sievers }
39016574dccSKay Sievers 
391a7fd6706SKay Sievers static ssize_t store_uevent(struct device *dev, struct device_attribute *attr,
392a7fd6706SKay Sievers 			    const char *buf, size_t count)
393a7fd6706SKay Sievers {
39460a96a59SKay Sievers 	enum kobject_action action;
39560a96a59SKay Sievers 
3963f5468c9SKay Sievers 	if (kobject_action_type(buf, count, &action) == 0)
39760a96a59SKay Sievers 		kobject_uevent(&dev->kobj, action);
3983f5468c9SKay Sievers 	else
3993f5468c9SKay Sievers 		dev_err(dev, "uevent: unknown action-string\n");
400a7fd6706SKay Sievers 	return count;
401a7fd6706SKay Sievers }
402a7fd6706SKay Sievers 
403ad6a1e1cSTejun Heo static struct device_attribute uevent_attr =
404ad6a1e1cSTejun Heo 	__ATTR(uevent, S_IRUGO | S_IWUSR, show_uevent, store_uevent);
405ad6a1e1cSTejun Heo 
406621a1672SDmitry Torokhov static int device_add_attributes(struct device *dev,
407621a1672SDmitry Torokhov 				 struct device_attribute *attrs)
408de0ff00dSGreg Kroah-Hartman {
409de0ff00dSGreg Kroah-Hartman 	int error = 0;
410621a1672SDmitry Torokhov 	int i;
411de0ff00dSGreg Kroah-Hartman 
412621a1672SDmitry Torokhov 	if (attrs) {
413621a1672SDmitry Torokhov 		for (i = 0; attr_name(attrs[i]); i++) {
414621a1672SDmitry Torokhov 			error = device_create_file(dev, &attrs[i]);
415621a1672SDmitry Torokhov 			if (error)
416621a1672SDmitry Torokhov 				break;
417621a1672SDmitry Torokhov 		}
418621a1672SDmitry Torokhov 		if (error)
419de0ff00dSGreg Kroah-Hartman 			while (--i >= 0)
420621a1672SDmitry Torokhov 				device_remove_file(dev, &attrs[i]);
421de0ff00dSGreg Kroah-Hartman 	}
422de0ff00dSGreg Kroah-Hartman 	return error;
423de0ff00dSGreg Kroah-Hartman }
424de0ff00dSGreg Kroah-Hartman 
425621a1672SDmitry Torokhov static void device_remove_attributes(struct device *dev,
426621a1672SDmitry Torokhov 				     struct device_attribute *attrs)
427de0ff00dSGreg Kroah-Hartman {
428de0ff00dSGreg Kroah-Hartman 	int i;
429621a1672SDmitry Torokhov 
430621a1672SDmitry Torokhov 	if (attrs)
431621a1672SDmitry Torokhov 		for (i = 0; attr_name(attrs[i]); i++)
432621a1672SDmitry Torokhov 			device_remove_file(dev, &attrs[i]);
433621a1672SDmitry Torokhov }
434621a1672SDmitry Torokhov 
435c97415a7SStefan Achatz static int device_add_bin_attributes(struct device *dev,
436c97415a7SStefan Achatz 				     struct bin_attribute *attrs)
437c97415a7SStefan Achatz {
438c97415a7SStefan Achatz 	int error = 0;
439c97415a7SStefan Achatz 	int i;
440c97415a7SStefan Achatz 
441c97415a7SStefan Achatz 	if (attrs) {
442c97415a7SStefan Achatz 		for (i = 0; attr_name(attrs[i]); i++) {
443c97415a7SStefan Achatz 			error = device_create_bin_file(dev, &attrs[i]);
444c97415a7SStefan Achatz 			if (error)
445c97415a7SStefan Achatz 				break;
446c97415a7SStefan Achatz 		}
447c97415a7SStefan Achatz 		if (error)
448c97415a7SStefan Achatz 			while (--i >= 0)
449c97415a7SStefan Achatz 				device_remove_bin_file(dev, &attrs[i]);
450c97415a7SStefan Achatz 	}
451c97415a7SStefan Achatz 	return error;
452c97415a7SStefan Achatz }
453c97415a7SStefan Achatz 
454c97415a7SStefan Achatz static void device_remove_bin_attributes(struct device *dev,
455c97415a7SStefan Achatz 					 struct bin_attribute *attrs)
456c97415a7SStefan Achatz {
457c97415a7SStefan Achatz 	int i;
458c97415a7SStefan Achatz 
459c97415a7SStefan Achatz 	if (attrs)
460c97415a7SStefan Achatz 		for (i = 0; attr_name(attrs[i]); i++)
461c97415a7SStefan Achatz 			device_remove_bin_file(dev, &attrs[i]);
462c97415a7SStefan Achatz }
463c97415a7SStefan Achatz 
464621a1672SDmitry Torokhov static int device_add_groups(struct device *dev,
465a4dbd674SDavid Brownell 			     const struct attribute_group **groups)
466621a1672SDmitry Torokhov {
467621a1672SDmitry Torokhov 	int error = 0;
468621a1672SDmitry Torokhov 	int i;
469621a1672SDmitry Torokhov 
470621a1672SDmitry Torokhov 	if (groups) {
471621a1672SDmitry Torokhov 		for (i = 0; groups[i]; i++) {
472621a1672SDmitry Torokhov 			error = sysfs_create_group(&dev->kobj, groups[i]);
473621a1672SDmitry Torokhov 			if (error) {
474621a1672SDmitry Torokhov 				while (--i >= 0)
4754a3ad20cSGreg Kroah-Hartman 					sysfs_remove_group(&dev->kobj,
4764a3ad20cSGreg Kroah-Hartman 							   groups[i]);
477621a1672SDmitry Torokhov 				break;
478de0ff00dSGreg Kroah-Hartman 			}
479de0ff00dSGreg Kroah-Hartman 		}
480de0ff00dSGreg Kroah-Hartman 	}
481621a1672SDmitry Torokhov 	return error;
482621a1672SDmitry Torokhov }
483621a1672SDmitry Torokhov 
484621a1672SDmitry Torokhov static void device_remove_groups(struct device *dev,
485a4dbd674SDavid Brownell 				 const struct attribute_group **groups)
486621a1672SDmitry Torokhov {
487621a1672SDmitry Torokhov 	int i;
488621a1672SDmitry Torokhov 
489621a1672SDmitry Torokhov 	if (groups)
490621a1672SDmitry Torokhov 		for (i = 0; groups[i]; i++)
491621a1672SDmitry Torokhov 			sysfs_remove_group(&dev->kobj, groups[i]);
492621a1672SDmitry Torokhov }
493de0ff00dSGreg Kroah-Hartman 
4942620efefSGreg Kroah-Hartman static int device_add_attrs(struct device *dev)
4952620efefSGreg Kroah-Hartman {
4962620efefSGreg Kroah-Hartman 	struct class *class = dev->class;
497aed65af1SStephen Hemminger 	const struct device_type *type = dev->type;
498621a1672SDmitry Torokhov 	int error;
4992620efefSGreg Kroah-Hartman 
500621a1672SDmitry Torokhov 	if (class) {
501621a1672SDmitry Torokhov 		error = device_add_attributes(dev, class->dev_attrs);
5022620efefSGreg Kroah-Hartman 		if (error)
503621a1672SDmitry Torokhov 			return error;
504c97415a7SStefan Achatz 		error = device_add_bin_attributes(dev, class->dev_bin_attrs);
505c97415a7SStefan Achatz 		if (error)
506c97415a7SStefan Achatz 			goto err_remove_class_attrs;
507f9f852dfSKay Sievers 	}
508f9f852dfSKay Sievers 
509621a1672SDmitry Torokhov 	if (type) {
510621a1672SDmitry Torokhov 		error = device_add_groups(dev, type->groups);
511f9f852dfSKay Sievers 		if (error)
512c97415a7SStefan Achatz 			goto err_remove_class_bin_attrs;
513f9f852dfSKay Sievers 	}
514621a1672SDmitry Torokhov 
515621a1672SDmitry Torokhov 	error = device_add_groups(dev, dev->groups);
516f9f852dfSKay Sievers 	if (error)
517621a1672SDmitry Torokhov 		goto err_remove_type_groups;
518621a1672SDmitry Torokhov 
519621a1672SDmitry Torokhov 	return 0;
520621a1672SDmitry Torokhov 
521621a1672SDmitry Torokhov  err_remove_type_groups:
522621a1672SDmitry Torokhov 	if (type)
523621a1672SDmitry Torokhov 		device_remove_groups(dev, type->groups);
524c97415a7SStefan Achatz  err_remove_class_bin_attrs:
525c97415a7SStefan Achatz 	if (class)
526c97415a7SStefan Achatz 		device_remove_bin_attributes(dev, class->dev_bin_attrs);
527621a1672SDmitry Torokhov  err_remove_class_attrs:
528621a1672SDmitry Torokhov 	if (class)
529621a1672SDmitry Torokhov 		device_remove_attributes(dev, class->dev_attrs);
530f9f852dfSKay Sievers 
5312620efefSGreg Kroah-Hartman 	return error;
5322620efefSGreg Kroah-Hartman }
5332620efefSGreg Kroah-Hartman 
5342620efefSGreg Kroah-Hartman static void device_remove_attrs(struct device *dev)
5352620efefSGreg Kroah-Hartman {
5362620efefSGreg Kroah-Hartman 	struct class *class = dev->class;
537aed65af1SStephen Hemminger 	const struct device_type *type = dev->type;
5382620efefSGreg Kroah-Hartman 
539621a1672SDmitry Torokhov 	device_remove_groups(dev, dev->groups);
540f9f852dfSKay Sievers 
541621a1672SDmitry Torokhov 	if (type)
542621a1672SDmitry Torokhov 		device_remove_groups(dev, type->groups);
543621a1672SDmitry Torokhov 
544c97415a7SStefan Achatz 	if (class) {
545621a1672SDmitry Torokhov 		device_remove_attributes(dev, class->dev_attrs);
546c97415a7SStefan Achatz 		device_remove_bin_attributes(dev, class->dev_bin_attrs);
547c97415a7SStefan Achatz 	}
5482620efefSGreg Kroah-Hartman }
5492620efefSGreg Kroah-Hartman 
5502620efefSGreg Kroah-Hartman 
55123681e47SGreg Kroah-Hartman static ssize_t show_dev(struct device *dev, struct device_attribute *attr,
55223681e47SGreg Kroah-Hartman 			char *buf)
55323681e47SGreg Kroah-Hartman {
55423681e47SGreg Kroah-Hartman 	return print_dev_t(buf, dev->devt);
55523681e47SGreg Kroah-Hartman }
55623681e47SGreg Kroah-Hartman 
557ad6a1e1cSTejun Heo static struct device_attribute devt_attr =
558ad6a1e1cSTejun Heo 	__ATTR(dev, S_IRUGO, show_dev, NULL);
559ad6a1e1cSTejun Heo 
560ca22e56dSKay Sievers /* /sys/devices/ */
561881c6cfdSGreg Kroah-Hartman struct kset *devices_kset;
5621da177e4SLinus Torvalds 
5631da177e4SLinus Torvalds /**
5641da177e4SLinus Torvalds  * device_create_file - create sysfs attribute file for device.
5651da177e4SLinus Torvalds  * @dev: device.
5661da177e4SLinus Torvalds  * @attr: device attribute descriptor.
5671da177e4SLinus Torvalds  */
56826579ab7SPhil Carmody int device_create_file(struct device *dev,
56926579ab7SPhil Carmody 		       const struct device_attribute *attr)
5701da177e4SLinus Torvalds {
5711da177e4SLinus Torvalds 	int error = 0;
5728f46baaaSFelipe Balbi 
5738f46baaaSFelipe Balbi 	if (dev) {
5748f46baaaSFelipe Balbi 		WARN(((attr->attr.mode & S_IWUGO) && !attr->store),
5758f46baaaSFelipe Balbi 				"Write permission without 'store'\n");
5768f46baaaSFelipe Balbi 		WARN(((attr->attr.mode & S_IRUGO) && !attr->show),
5778f46baaaSFelipe Balbi 				"Read permission without 'show'\n");
5781da177e4SLinus Torvalds 		error = sysfs_create_file(&dev->kobj, &attr->attr);
5798f46baaaSFelipe Balbi 	}
5808f46baaaSFelipe Balbi 
5811da177e4SLinus Torvalds 	return error;
5821da177e4SLinus Torvalds }
5831da177e4SLinus Torvalds 
5841da177e4SLinus Torvalds /**
5851da177e4SLinus Torvalds  * device_remove_file - remove sysfs attribute file.
5861da177e4SLinus Torvalds  * @dev: device.
5871da177e4SLinus Torvalds  * @attr: device attribute descriptor.
5881da177e4SLinus Torvalds  */
58926579ab7SPhil Carmody void device_remove_file(struct device *dev,
59026579ab7SPhil Carmody 			const struct device_attribute *attr)
5911da177e4SLinus Torvalds {
5920c98b19fSCornelia Huck 	if (dev)
5931da177e4SLinus Torvalds 		sysfs_remove_file(&dev->kobj, &attr->attr);
5941da177e4SLinus Torvalds }
5951da177e4SLinus Torvalds 
5962589f188SGreg Kroah-Hartman /**
5972589f188SGreg Kroah-Hartman  * device_create_bin_file - create sysfs binary attribute file for device.
5982589f188SGreg Kroah-Hartman  * @dev: device.
5992589f188SGreg Kroah-Hartman  * @attr: device binary attribute descriptor.
6002589f188SGreg Kroah-Hartman  */
60166ecb92bSPhil Carmody int device_create_bin_file(struct device *dev,
60266ecb92bSPhil Carmody 			   const struct bin_attribute *attr)
6032589f188SGreg Kroah-Hartman {
6042589f188SGreg Kroah-Hartman 	int error = -EINVAL;
6052589f188SGreg Kroah-Hartman 	if (dev)
6062589f188SGreg Kroah-Hartman 		error = sysfs_create_bin_file(&dev->kobj, attr);
6072589f188SGreg Kroah-Hartman 	return error;
6082589f188SGreg Kroah-Hartman }
6092589f188SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(device_create_bin_file);
6102589f188SGreg Kroah-Hartman 
6112589f188SGreg Kroah-Hartman /**
6122589f188SGreg Kroah-Hartman  * device_remove_bin_file - remove sysfs binary attribute file
6132589f188SGreg Kroah-Hartman  * @dev: device.
6142589f188SGreg Kroah-Hartman  * @attr: device binary attribute descriptor.
6152589f188SGreg Kroah-Hartman  */
61666ecb92bSPhil Carmody void device_remove_bin_file(struct device *dev,
61766ecb92bSPhil Carmody 			    const struct bin_attribute *attr)
6182589f188SGreg Kroah-Hartman {
6192589f188SGreg Kroah-Hartman 	if (dev)
6202589f188SGreg Kroah-Hartman 		sysfs_remove_bin_file(&dev->kobj, attr);
6212589f188SGreg Kroah-Hartman }
6222589f188SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(device_remove_bin_file);
6232589f188SGreg Kroah-Hartman 
624d9a9cdfbSAlan Stern /**
625523ded71SAlan Stern  * device_schedule_callback_owner - helper to schedule a callback for a device
626d9a9cdfbSAlan Stern  * @dev: device.
627d9a9cdfbSAlan Stern  * @func: callback function to invoke later.
628523ded71SAlan Stern  * @owner: module owning the callback routine
629d9a9cdfbSAlan Stern  *
630d9a9cdfbSAlan Stern  * Attribute methods must not unregister themselves or their parent device
631d9a9cdfbSAlan Stern  * (which would amount to the same thing).  Attempts to do so will deadlock,
632d9a9cdfbSAlan Stern  * since unregistration is mutually exclusive with driver callbacks.
633d9a9cdfbSAlan Stern  *
634d9a9cdfbSAlan Stern  * Instead methods can call this routine, which will attempt to allocate
635d9a9cdfbSAlan Stern  * and schedule a workqueue request to call back @func with @dev as its
636d9a9cdfbSAlan Stern  * argument in the workqueue's process context.  @dev will be pinned until
637d9a9cdfbSAlan Stern  * @func returns.
638d9a9cdfbSAlan Stern  *
639523ded71SAlan Stern  * This routine is usually called via the inline device_schedule_callback(),
640523ded71SAlan Stern  * which automatically sets @owner to THIS_MODULE.
641523ded71SAlan Stern  *
642d9a9cdfbSAlan Stern  * Returns 0 if the request was submitted, -ENOMEM if storage could not
643523ded71SAlan Stern  * be allocated, -ENODEV if a reference to @owner isn't available.
644d9a9cdfbSAlan Stern  *
645d9a9cdfbSAlan Stern  * NOTE: This routine won't work if CONFIG_SYSFS isn't set!  It uses an
646d9a9cdfbSAlan Stern  * underlying sysfs routine (since it is intended for use by attribute
647d9a9cdfbSAlan Stern  * methods), and if sysfs isn't available you'll get nothing but -ENOSYS.
648d9a9cdfbSAlan Stern  */
649523ded71SAlan Stern int device_schedule_callback_owner(struct device *dev,
650523ded71SAlan Stern 		void (*func)(struct device *), struct module *owner)
651d9a9cdfbSAlan Stern {
652d9a9cdfbSAlan Stern 	return sysfs_schedule_callback(&dev->kobj,
653523ded71SAlan Stern 			(void (*)(void *)) func, dev, owner);
654d9a9cdfbSAlan Stern }
655523ded71SAlan Stern EXPORT_SYMBOL_GPL(device_schedule_callback_owner);
656d9a9cdfbSAlan Stern 
65734bb61f9SJames Bottomley static void klist_children_get(struct klist_node *n)
65834bb61f9SJames Bottomley {
659f791b8c8SGreg Kroah-Hartman 	struct device_private *p = to_device_private_parent(n);
660f791b8c8SGreg Kroah-Hartman 	struct device *dev = p->device;
66134bb61f9SJames Bottomley 
66234bb61f9SJames Bottomley 	get_device(dev);
66334bb61f9SJames Bottomley }
66434bb61f9SJames Bottomley 
66534bb61f9SJames Bottomley static void klist_children_put(struct klist_node *n)
66634bb61f9SJames Bottomley {
667f791b8c8SGreg Kroah-Hartman 	struct device_private *p = to_device_private_parent(n);
668f791b8c8SGreg Kroah-Hartman 	struct device *dev = p->device;
66934bb61f9SJames Bottomley 
67034bb61f9SJames Bottomley 	put_device(dev);
67134bb61f9SJames Bottomley }
67234bb61f9SJames Bottomley 
6731da177e4SLinus Torvalds /**
6741da177e4SLinus Torvalds  * device_initialize - init device structure.
6751da177e4SLinus Torvalds  * @dev: device.
6761da177e4SLinus Torvalds  *
6775739411aSCornelia Huck  * This prepares the device for use by other layers by initializing
6785739411aSCornelia Huck  * its fields.
6791da177e4SLinus Torvalds  * It is the first half of device_register(), if called by
6805739411aSCornelia Huck  * that function, though it can also be called separately, so one
6815739411aSCornelia Huck  * may use @dev's fields. In particular, get_device()/put_device()
6825739411aSCornelia Huck  * may be used for reference counting of @dev after calling this
6835739411aSCornelia Huck  * function.
6845739411aSCornelia Huck  *
685b10d5efdSAlan Stern  * All fields in @dev must be initialized by the caller to 0, except
686b10d5efdSAlan Stern  * for those explicitly set to some other value.  The simplest
687b10d5efdSAlan Stern  * approach is to use kzalloc() to allocate the structure containing
688b10d5efdSAlan Stern  * @dev.
689b10d5efdSAlan Stern  *
6905739411aSCornelia Huck  * NOTE: Use put_device() to give up your reference instead of freeing
6915739411aSCornelia Huck  * @dev directly once you have called this function.
6921da177e4SLinus Torvalds  */
6931da177e4SLinus Torvalds void device_initialize(struct device *dev)
6941da177e4SLinus Torvalds {
695881c6cfdSGreg Kroah-Hartman 	dev->kobj.kset = devices_kset;
696f9cb074bSGreg Kroah-Hartman 	kobject_init(&dev->kobj, &device_ktype);
6971da177e4SLinus Torvalds 	INIT_LIST_HEAD(&dev->dma_pools);
6983142788bSThomas Gleixner 	mutex_init(&dev->mutex);
6991704f47bSPeter Zijlstra 	lockdep_set_novalidate_class(&dev->mutex);
7009ac7849eSTejun Heo 	spin_lock_init(&dev->devres_lock);
7019ac7849eSTejun Heo 	INIT_LIST_HEAD(&dev->devres_head);
7023b98aeafSAlan Stern 	device_pm_init(dev);
70387348136SChristoph Hellwig 	set_dev_node(dev, -1);
7041da177e4SLinus Torvalds }
7051da177e4SLinus Torvalds 
706d73ce004STejun Heo struct kobject *virtual_device_parent(struct device *dev)
707f0ee61a6SGreg Kroah-Hartman {
708f0ee61a6SGreg Kroah-Hartman 	static struct kobject *virtual_dir = NULL;
709f0ee61a6SGreg Kroah-Hartman 
710f0ee61a6SGreg Kroah-Hartman 	if (!virtual_dir)
7114ff6abffSGreg Kroah-Hartman 		virtual_dir = kobject_create_and_add("virtual",
712881c6cfdSGreg Kroah-Hartman 						     &devices_kset->kobj);
713f0ee61a6SGreg Kroah-Hartman 
71486406245SKay Sievers 	return virtual_dir;
715f0ee61a6SGreg Kroah-Hartman }
716f0ee61a6SGreg Kroah-Hartman 
717bc451f20SEric W. Biederman struct class_dir {
718bc451f20SEric W. Biederman 	struct kobject kobj;
719bc451f20SEric W. Biederman 	struct class *class;
720bc451f20SEric W. Biederman };
721bc451f20SEric W. Biederman 
722bc451f20SEric W. Biederman #define to_class_dir(obj) container_of(obj, struct class_dir, kobj)
723bc451f20SEric W. Biederman 
724bc451f20SEric W. Biederman static void class_dir_release(struct kobject *kobj)
725bc451f20SEric W. Biederman {
726bc451f20SEric W. Biederman 	struct class_dir *dir = to_class_dir(kobj);
727bc451f20SEric W. Biederman 	kfree(dir);
728bc451f20SEric W. Biederman }
729bc451f20SEric W. Biederman 
730bc451f20SEric W. Biederman static const
731bc451f20SEric W. Biederman struct kobj_ns_type_operations *class_dir_child_ns_type(struct kobject *kobj)
732bc451f20SEric W. Biederman {
733bc451f20SEric W. Biederman 	struct class_dir *dir = to_class_dir(kobj);
734bc451f20SEric W. Biederman 	return dir->class->ns_type;
735bc451f20SEric W. Biederman }
736bc451f20SEric W. Biederman 
737bc451f20SEric W. Biederman static struct kobj_type class_dir_ktype = {
738bc451f20SEric W. Biederman 	.release	= class_dir_release,
739bc451f20SEric W. Biederman 	.sysfs_ops	= &kobj_sysfs_ops,
740bc451f20SEric W. Biederman 	.child_ns_type	= class_dir_child_ns_type
741bc451f20SEric W. Biederman };
742bc451f20SEric W. Biederman 
743bc451f20SEric W. Biederman static struct kobject *
744bc451f20SEric W. Biederman class_dir_create_and_add(struct class *class, struct kobject *parent_kobj)
745bc451f20SEric W. Biederman {
746bc451f20SEric W. Biederman 	struct class_dir *dir;
747bc451f20SEric W. Biederman 	int retval;
748bc451f20SEric W. Biederman 
749bc451f20SEric W. Biederman 	dir = kzalloc(sizeof(*dir), GFP_KERNEL);
750bc451f20SEric W. Biederman 	if (!dir)
751bc451f20SEric W. Biederman 		return NULL;
752bc451f20SEric W. Biederman 
753bc451f20SEric W. Biederman 	dir->class = class;
754bc451f20SEric W. Biederman 	kobject_init(&dir->kobj, &class_dir_ktype);
755bc451f20SEric W. Biederman 
7566b6e39a6SKay Sievers 	dir->kobj.kset = &class->p->glue_dirs;
757bc451f20SEric W. Biederman 
758bc451f20SEric W. Biederman 	retval = kobject_add(&dir->kobj, parent_kobj, "%s", class->name);
759bc451f20SEric W. Biederman 	if (retval < 0) {
760bc451f20SEric W. Biederman 		kobject_put(&dir->kobj);
761bc451f20SEric W. Biederman 		return NULL;
762bc451f20SEric W. Biederman 	}
763bc451f20SEric W. Biederman 	return &dir->kobj;
764bc451f20SEric W. Biederman }
765bc451f20SEric W. Biederman 
766bc451f20SEric W. Biederman 
767c744aeaeSCornelia Huck static struct kobject *get_device_parent(struct device *dev,
768c744aeaeSCornelia Huck 					 struct device *parent)
76940fa5422SGreg Kroah-Hartman {
77086406245SKay Sievers 	if (dev->class) {
77177d3d7c1STejun Heo 		static DEFINE_MUTEX(gdp_mutex);
77286406245SKay Sievers 		struct kobject *kobj = NULL;
77386406245SKay Sievers 		struct kobject *parent_kobj;
77486406245SKay Sievers 		struct kobject *k;
77586406245SKay Sievers 
776ead454feSRandy Dunlap #ifdef CONFIG_BLOCK
77739aba963SKay Sievers 		/* block disks show up in /sys/block */
778e52eec13SAndi Kleen 		if (sysfs_deprecated && dev->class == &block_class) {
77939aba963SKay Sievers 			if (parent && parent->class == &block_class)
78039aba963SKay Sievers 				return &parent->kobj;
7816b6e39a6SKay Sievers 			return &block_class.p->subsys.kobj;
78239aba963SKay Sievers 		}
783ead454feSRandy Dunlap #endif
784e52eec13SAndi Kleen 
78586406245SKay Sievers 		/*
78686406245SKay Sievers 		 * If we have no parent, we live in "virtual".
7870f4dafc0SKay Sievers 		 * Class-devices with a non class-device as parent, live
7880f4dafc0SKay Sievers 		 * in a "glue" directory to prevent namespace collisions.
78986406245SKay Sievers 		 */
79086406245SKay Sievers 		if (parent == NULL)
79186406245SKay Sievers 			parent_kobj = virtual_device_parent(dev);
79224b1442dSEric W. Biederman 		else if (parent->class && !dev->class->ns_type)
79386406245SKay Sievers 			return &parent->kobj;
79486406245SKay Sievers 		else
79586406245SKay Sievers 			parent_kobj = &parent->kobj;
79686406245SKay Sievers 
79777d3d7c1STejun Heo 		mutex_lock(&gdp_mutex);
79877d3d7c1STejun Heo 
79986406245SKay Sievers 		/* find our class-directory at the parent and reference it */
8006b6e39a6SKay Sievers 		spin_lock(&dev->class->p->glue_dirs.list_lock);
8016b6e39a6SKay Sievers 		list_for_each_entry(k, &dev->class->p->glue_dirs.list, entry)
80286406245SKay Sievers 			if (k->parent == parent_kobj) {
80386406245SKay Sievers 				kobj = kobject_get(k);
80486406245SKay Sievers 				break;
80586406245SKay Sievers 			}
8066b6e39a6SKay Sievers 		spin_unlock(&dev->class->p->glue_dirs.list_lock);
80777d3d7c1STejun Heo 		if (kobj) {
80877d3d7c1STejun Heo 			mutex_unlock(&gdp_mutex);
80986406245SKay Sievers 			return kobj;
81077d3d7c1STejun Heo 		}
81186406245SKay Sievers 
81286406245SKay Sievers 		/* or create a new class-directory at the parent device */
813bc451f20SEric W. Biederman 		k = class_dir_create_and_add(dev->class, parent_kobj);
8140f4dafc0SKay Sievers 		/* do not emit an uevent for this simple "glue" directory */
81577d3d7c1STejun Heo 		mutex_unlock(&gdp_mutex);
81643968d2fSGreg Kroah-Hartman 		return k;
81786406245SKay Sievers 	}
81886406245SKay Sievers 
819ca22e56dSKay Sievers 	/* subsystems can specify a default root directory for their devices */
820ca22e56dSKay Sievers 	if (!parent && dev->bus && dev->bus->dev_root)
821ca22e56dSKay Sievers 		return &dev->bus->dev_root->kobj;
822ca22e56dSKay Sievers 
82386406245SKay Sievers 	if (parent)
824c744aeaeSCornelia Huck 		return &parent->kobj;
825c744aeaeSCornelia Huck 	return NULL;
826c744aeaeSCornelia Huck }
827da231fd5SKay Sievers 
82863b6971aSCornelia Huck static void cleanup_glue_dir(struct device *dev, struct kobject *glue_dir)
829da231fd5SKay Sievers {
8300f4dafc0SKay Sievers 	/* see if we live in a "glue" directory */
831c1fe539aSCornelia Huck 	if (!glue_dir || !dev->class ||
8326b6e39a6SKay Sievers 	    glue_dir->kset != &dev->class->p->glue_dirs)
833da231fd5SKay Sievers 		return;
834da231fd5SKay Sievers 
8350f4dafc0SKay Sievers 	kobject_put(glue_dir);
836da231fd5SKay Sievers }
83763b6971aSCornelia Huck 
83863b6971aSCornelia Huck static void cleanup_device_parent(struct device *dev)
83963b6971aSCornelia Huck {
84063b6971aSCornelia Huck 	cleanup_glue_dir(dev, dev->kobj.parent);
84163b6971aSCornelia Huck }
84286406245SKay Sievers 
8432ee97cafSCornelia Huck static int device_add_class_symlinks(struct device *dev)
8442ee97cafSCornelia Huck {
8452ee97cafSCornelia Huck 	int error;
8462ee97cafSCornelia Huck 
8472ee97cafSCornelia Huck 	if (!dev->class)
8482ee97cafSCornelia Huck 		return 0;
849da231fd5SKay Sievers 
8501fbfee6cSGreg Kroah-Hartman 	error = sysfs_create_link(&dev->kobj,
8516b6e39a6SKay Sievers 				  &dev->class->p->subsys.kobj,
8522ee97cafSCornelia Huck 				  "subsystem");
8532ee97cafSCornelia Huck 	if (error)
8542ee97cafSCornelia Huck 		goto out;
855da231fd5SKay Sievers 
8564e886c29SGreg Kroah-Hartman 	if (dev->parent && device_is_not_partition(dev)) {
8574f01a757SDmitry Torokhov 		error = sysfs_create_link(&dev->kobj, &dev->parent->kobj,
8584f01a757SDmitry Torokhov 					  "device");
8594f01a757SDmitry Torokhov 		if (error)
86039aba963SKay Sievers 			goto out_subsys;
8612ee97cafSCornelia Huck 	}
86239aba963SKay Sievers 
863ead454feSRandy Dunlap #ifdef CONFIG_BLOCK
86439aba963SKay Sievers 	/* /sys/block has directories and does not need symlinks */
865e52eec13SAndi Kleen 	if (sysfs_deprecated && dev->class == &block_class)
86639aba963SKay Sievers 		return 0;
867ead454feSRandy Dunlap #endif
86839aba963SKay Sievers 
86939aba963SKay Sievers 	/* link in the class directory pointing to the device */
8706b6e39a6SKay Sievers 	error = sysfs_create_link(&dev->class->p->subsys.kobj,
87139aba963SKay Sievers 				  &dev->kobj, dev_name(dev));
87239aba963SKay Sievers 	if (error)
87339aba963SKay Sievers 		goto out_device;
87439aba963SKay Sievers 
8752ee97cafSCornelia Huck 	return 0;
8762ee97cafSCornelia Huck 
87739aba963SKay Sievers out_device:
87839aba963SKay Sievers 	sysfs_remove_link(&dev->kobj, "device");
879da231fd5SKay Sievers 
8802ee97cafSCornelia Huck out_subsys:
8812ee97cafSCornelia Huck 	sysfs_remove_link(&dev->kobj, "subsystem");
8822ee97cafSCornelia Huck out:
8832ee97cafSCornelia Huck 	return error;
8842ee97cafSCornelia Huck }
8852ee97cafSCornelia Huck 
8862ee97cafSCornelia Huck static void device_remove_class_symlinks(struct device *dev)
8872ee97cafSCornelia Huck {
8882ee97cafSCornelia Huck 	if (!dev->class)
8892ee97cafSCornelia Huck 		return;
890da231fd5SKay Sievers 
8914e886c29SGreg Kroah-Hartman 	if (dev->parent && device_is_not_partition(dev))
892da231fd5SKay Sievers 		sysfs_remove_link(&dev->kobj, "device");
8932ee97cafSCornelia Huck 	sysfs_remove_link(&dev->kobj, "subsystem");
894ead454feSRandy Dunlap #ifdef CONFIG_BLOCK
895e52eec13SAndi Kleen 	if (sysfs_deprecated && dev->class == &block_class)
89639aba963SKay Sievers 		return;
897ead454feSRandy Dunlap #endif
8986b6e39a6SKay Sievers 	sysfs_delete_link(&dev->class->p->subsys.kobj, &dev->kobj, dev_name(dev));
8992ee97cafSCornelia Huck }
9002ee97cafSCornelia Huck 
9011da177e4SLinus Torvalds /**
902413c239fSStephen Rothwell  * dev_set_name - set a device name
903413c239fSStephen Rothwell  * @dev: device
90446232366SRandy Dunlap  * @fmt: format string for the device's name
905413c239fSStephen Rothwell  */
906413c239fSStephen Rothwell int dev_set_name(struct device *dev, const char *fmt, ...)
907413c239fSStephen Rothwell {
908413c239fSStephen Rothwell 	va_list vargs;
9091fa5ae85SKay Sievers 	int err;
910413c239fSStephen Rothwell 
911413c239fSStephen Rothwell 	va_start(vargs, fmt);
9121fa5ae85SKay Sievers 	err = kobject_set_name_vargs(&dev->kobj, fmt, vargs);
913413c239fSStephen Rothwell 	va_end(vargs);
9141fa5ae85SKay Sievers 	return err;
915413c239fSStephen Rothwell }
916413c239fSStephen Rothwell EXPORT_SYMBOL_GPL(dev_set_name);
917413c239fSStephen Rothwell 
918413c239fSStephen Rothwell /**
919e105b8bfSDan Williams  * device_to_dev_kobj - select a /sys/dev/ directory for the device
920e105b8bfSDan Williams  * @dev: device
921e105b8bfSDan Williams  *
922e105b8bfSDan Williams  * By default we select char/ for new entries.  Setting class->dev_obj
923e105b8bfSDan Williams  * to NULL prevents an entry from being created.  class->dev_kobj must
924e105b8bfSDan Williams  * be set (or cleared) before any devices are registered to the class
925e105b8bfSDan Williams  * otherwise device_create_sys_dev_entry() and
9260d4e293cSPeter Korsgaard  * device_remove_sys_dev_entry() will disagree about the presence of
9270d4e293cSPeter Korsgaard  * the link.
928e105b8bfSDan Williams  */
929e105b8bfSDan Williams static struct kobject *device_to_dev_kobj(struct device *dev)
930e105b8bfSDan Williams {
931e105b8bfSDan Williams 	struct kobject *kobj;
932e105b8bfSDan Williams 
933e105b8bfSDan Williams 	if (dev->class)
934e105b8bfSDan Williams 		kobj = dev->class->dev_kobj;
935e105b8bfSDan Williams 	else
936e105b8bfSDan Williams 		kobj = sysfs_dev_char_kobj;
937e105b8bfSDan Williams 
938e105b8bfSDan Williams 	return kobj;
939e105b8bfSDan Williams }
940e105b8bfSDan Williams 
941e105b8bfSDan Williams static int device_create_sys_dev_entry(struct device *dev)
942e105b8bfSDan Williams {
943e105b8bfSDan Williams 	struct kobject *kobj = device_to_dev_kobj(dev);
944e105b8bfSDan Williams 	int error = 0;
945e105b8bfSDan Williams 	char devt_str[15];
946e105b8bfSDan Williams 
947e105b8bfSDan Williams 	if (kobj) {
948e105b8bfSDan Williams 		format_dev_t(devt_str, dev->devt);
949e105b8bfSDan Williams 		error = sysfs_create_link(kobj, &dev->kobj, devt_str);
950e105b8bfSDan Williams 	}
951e105b8bfSDan Williams 
952e105b8bfSDan Williams 	return error;
953e105b8bfSDan Williams }
954e105b8bfSDan Williams 
955e105b8bfSDan Williams static void device_remove_sys_dev_entry(struct device *dev)
956e105b8bfSDan Williams {
957e105b8bfSDan Williams 	struct kobject *kobj = device_to_dev_kobj(dev);
958e105b8bfSDan Williams 	char devt_str[15];
959e105b8bfSDan Williams 
960e105b8bfSDan Williams 	if (kobj) {
961e105b8bfSDan Williams 		format_dev_t(devt_str, dev->devt);
962e105b8bfSDan Williams 		sysfs_remove_link(kobj, devt_str);
963e105b8bfSDan Williams 	}
964e105b8bfSDan Williams }
965e105b8bfSDan Williams 
966b4028437SGreg Kroah-Hartman int device_private_init(struct device *dev)
967b4028437SGreg Kroah-Hartman {
968b4028437SGreg Kroah-Hartman 	dev->p = kzalloc(sizeof(*dev->p), GFP_KERNEL);
969b4028437SGreg Kroah-Hartman 	if (!dev->p)
970b4028437SGreg Kroah-Hartman 		return -ENOMEM;
971b4028437SGreg Kroah-Hartman 	dev->p->device = dev;
972b4028437SGreg Kroah-Hartman 	klist_init(&dev->p->klist_children, klist_children_get,
973b4028437SGreg Kroah-Hartman 		   klist_children_put);
974ef8a3fd6SGreg Kroah-Hartman 	INIT_LIST_HEAD(&dev->p->deferred_probe);
975b4028437SGreg Kroah-Hartman 	return 0;
976b4028437SGreg Kroah-Hartman }
977b4028437SGreg Kroah-Hartman 
978e105b8bfSDan Williams /**
9791da177e4SLinus Torvalds  * device_add - add device to device hierarchy.
9801da177e4SLinus Torvalds  * @dev: device.
9811da177e4SLinus Torvalds  *
9821da177e4SLinus Torvalds  * This is part 2 of device_register(), though may be called
9831da177e4SLinus Torvalds  * separately _iff_ device_initialize() has been called separately.
9841da177e4SLinus Torvalds  *
9855739411aSCornelia Huck  * This adds @dev to the kobject hierarchy via kobject_add(), adds it
9861da177e4SLinus Torvalds  * to the global and sibling lists for the device, then
9871da177e4SLinus Torvalds  * adds it to the other relevant subsystems of the driver model.
9885739411aSCornelia Huck  *
989b10d5efdSAlan Stern  * Do not call this routine or device_register() more than once for
990b10d5efdSAlan Stern  * any device structure.  The driver model core is not designed to work
991b10d5efdSAlan Stern  * with devices that get unregistered and then spring back to life.
992b10d5efdSAlan Stern  * (Among other things, it's very hard to guarantee that all references
993b10d5efdSAlan Stern  * to the previous incarnation of @dev have been dropped.)  Allocate
994b10d5efdSAlan Stern  * and register a fresh new struct device instead.
995b10d5efdSAlan Stern  *
9965739411aSCornelia Huck  * NOTE: _Never_ directly free @dev after calling this function, even
9975739411aSCornelia Huck  * if it returned an error! Always use put_device() to give up your
9985739411aSCornelia Huck  * reference instead.
9991da177e4SLinus Torvalds  */
10001da177e4SLinus Torvalds int device_add(struct device *dev)
10011da177e4SLinus Torvalds {
10021da177e4SLinus Torvalds 	struct device *parent = NULL;
1003ca22e56dSKay Sievers 	struct kobject *kobj;
1004c47ed219SGreg Kroah-Hartman 	struct class_interface *class_intf;
1005c906a48aSGreg Kroah-Hartman 	int error = -EINVAL;
1006775b64d2SRafael J. Wysocki 
10071da177e4SLinus Torvalds 	dev = get_device(dev);
1008c906a48aSGreg Kroah-Hartman 	if (!dev)
1009c906a48aSGreg Kroah-Hartman 		goto done;
1010c906a48aSGreg Kroah-Hartman 
1011fb069a5dSGreg Kroah-Hartman 	if (!dev->p) {
1012b4028437SGreg Kroah-Hartman 		error = device_private_init(dev);
1013b4028437SGreg Kroah-Hartman 		if (error)
1014fb069a5dSGreg Kroah-Hartman 			goto done;
1015fb069a5dSGreg Kroah-Hartman 	}
1016fb069a5dSGreg Kroah-Hartman 
10171fa5ae85SKay Sievers 	/*
10181fa5ae85SKay Sievers 	 * for statically allocated devices, which should all be converted
10191fa5ae85SKay Sievers 	 * some day, we need to initialize the name. We prevent reading back
10201fa5ae85SKay Sievers 	 * the name, and force the use of dev_name()
10211fa5ae85SKay Sievers 	 */
10221fa5ae85SKay Sievers 	if (dev->init_name) {
1023acc0e90fSGreg Kroah-Hartman 		dev_set_name(dev, "%s", dev->init_name);
10241fa5ae85SKay Sievers 		dev->init_name = NULL;
10251fa5ae85SKay Sievers 	}
1026c906a48aSGreg Kroah-Hartman 
1027ca22e56dSKay Sievers 	/* subsystems can specify simple device enumeration */
1028ca22e56dSKay Sievers 	if (!dev_name(dev) && dev->bus && dev->bus->dev_name)
1029ca22e56dSKay Sievers 		dev_set_name(dev, "%s%u", dev->bus->dev_name, dev->id);
1030ca22e56dSKay Sievers 
1031e6309e75SThomas Gleixner 	if (!dev_name(dev)) {
1032e6309e75SThomas Gleixner 		error = -EINVAL;
10335c8563d7SKay Sievers 		goto name_error;
1034e6309e75SThomas Gleixner 	}
10351da177e4SLinus Torvalds 
10361e0b2cf9SKay Sievers 	pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
1037c205ef48SGreg Kroah-Hartman 
10381da177e4SLinus Torvalds 	parent = get_device(dev->parent);
1039ca22e56dSKay Sievers 	kobj = get_device_parent(dev, parent);
1040ca22e56dSKay Sievers 	if (kobj)
1041ca22e56dSKay Sievers 		dev->kobj.parent = kobj;
10421da177e4SLinus Torvalds 
10430d358f22SYinghai Lu 	/* use parent numa_node */
10440d358f22SYinghai Lu 	if (parent)
10450d358f22SYinghai Lu 		set_dev_node(dev, dev_to_node(parent));
10460d358f22SYinghai Lu 
10471da177e4SLinus Torvalds 	/* first, register with generic layer. */
10488a577ffcSKay Sievers 	/* we require the name to be set before, and pass NULL */
10498a577ffcSKay Sievers 	error = kobject_add(&dev->kobj, dev->kobj.parent, NULL);
105040fa5422SGreg Kroah-Hartman 	if (error)
10511da177e4SLinus Torvalds 		goto Error;
1052a7fd6706SKay Sievers 
105337022644SBrian Walsh 	/* notify platform of device entry */
105437022644SBrian Walsh 	if (platform_notify)
105537022644SBrian Walsh 		platform_notify(dev);
105637022644SBrian Walsh 
1057ad6a1e1cSTejun Heo 	error = device_create_file(dev, &uevent_attr);
1058a306eea4SCornelia Huck 	if (error)
1059a306eea4SCornelia Huck 		goto attrError;
1060a7fd6706SKay Sievers 
106123681e47SGreg Kroah-Hartman 	if (MAJOR(dev->devt)) {
1062ad6a1e1cSTejun Heo 		error = device_create_file(dev, &devt_attr);
1063ad6a1e1cSTejun Heo 		if (error)
1064a306eea4SCornelia Huck 			goto ueventattrError;
1065e105b8bfSDan Williams 
1066e105b8bfSDan Williams 		error = device_create_sys_dev_entry(dev);
1067e105b8bfSDan Williams 		if (error)
1068e105b8bfSDan Williams 			goto devtattrError;
10692b2af54aSKay Sievers 
10702b2af54aSKay Sievers 		devtmpfs_create_node(dev);
107123681e47SGreg Kroah-Hartman 	}
107223681e47SGreg Kroah-Hartman 
10732ee97cafSCornelia Huck 	error = device_add_class_symlinks(dev);
10742ee97cafSCornelia Huck 	if (error)
10752ee97cafSCornelia Huck 		goto SymlinkError;
1076dc0afa83SCornelia Huck 	error = device_add_attrs(dev);
1077dc0afa83SCornelia Huck 	if (error)
10782620efefSGreg Kroah-Hartman 		goto AttrsError;
1079dc0afa83SCornelia Huck 	error = bus_add_device(dev);
1080dc0afa83SCornelia Huck 	if (error)
10811da177e4SLinus Torvalds 		goto BusError;
10823b98aeafSAlan Stern 	error = dpm_sysfs_add(dev);
108357eee3d2SRafael J. Wysocki 	if (error)
10843b98aeafSAlan Stern 		goto DPMError;
10853b98aeafSAlan Stern 	device_pm_add(dev);
1086ec0676eeSAlan Stern 
1087ec0676eeSAlan Stern 	/* Notify clients of device addition.  This call must come
1088268863f4Smajianpeng 	 * after dpm_sysfs_add() and before kobject_uevent().
1089ec0676eeSAlan Stern 	 */
1090ec0676eeSAlan Stern 	if (dev->bus)
1091ec0676eeSAlan Stern 		blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
1092ec0676eeSAlan Stern 					     BUS_NOTIFY_ADD_DEVICE, dev);
1093ec0676eeSAlan Stern 
109453877d06SKay Sievers 	kobject_uevent(&dev->kobj, KOBJ_ADD);
10952023c610SAlan Stern 	bus_probe_device(dev);
10961da177e4SLinus Torvalds 	if (parent)
1097f791b8c8SGreg Kroah-Hartman 		klist_add_tail(&dev->p->knode_parent,
1098f791b8c8SGreg Kroah-Hartman 			       &parent->p->klist_children);
10991da177e4SLinus Torvalds 
11005d9fd169SGreg Kroah-Hartman 	if (dev->class) {
1101ca22e56dSKay Sievers 		mutex_lock(&dev->class->p->mutex);
1102c47ed219SGreg Kroah-Hartman 		/* tie the class to the device */
11035a3ceb86STejun Heo 		klist_add_tail(&dev->knode_class,
11046b6e39a6SKay Sievers 			       &dev->class->p->klist_devices);
1105c47ed219SGreg Kroah-Hartman 
1106c47ed219SGreg Kroah-Hartman 		/* notify any interfaces that the device is here */
1107184f1f77SGreg Kroah-Hartman 		list_for_each_entry(class_intf,
1108ca22e56dSKay Sievers 				    &dev->class->p->interfaces, node)
1109c47ed219SGreg Kroah-Hartman 			if (class_intf->add_dev)
1110c47ed219SGreg Kroah-Hartman 				class_intf->add_dev(dev, class_intf);
1111ca22e56dSKay Sievers 		mutex_unlock(&dev->class->p->mutex);
11125d9fd169SGreg Kroah-Hartman 	}
1113c906a48aSGreg Kroah-Hartman done:
11141da177e4SLinus Torvalds 	put_device(dev);
11151da177e4SLinus Torvalds 	return error;
11163b98aeafSAlan Stern  DPMError:
111757eee3d2SRafael J. Wysocki 	bus_remove_device(dev);
111857eee3d2SRafael J. Wysocki  BusError:
11192620efefSGreg Kroah-Hartman 	device_remove_attrs(dev);
11202620efefSGreg Kroah-Hartman  AttrsError:
11212ee97cafSCornelia Huck 	device_remove_class_symlinks(dev);
11222ee97cafSCornelia Huck  SymlinkError:
1123ad6a1e1cSTejun Heo 	if (MAJOR(dev->devt))
1124ad72956dSKay Sievers 		devtmpfs_delete_node(dev);
1125ad72956dSKay Sievers 	if (MAJOR(dev->devt))
1126e105b8bfSDan Williams 		device_remove_sys_dev_entry(dev);
1127e105b8bfSDan Williams  devtattrError:
1128e105b8bfSDan Williams 	if (MAJOR(dev->devt))
1129ad6a1e1cSTejun Heo 		device_remove_file(dev, &devt_attr);
1130a306eea4SCornelia Huck  ueventattrError:
1131ad6a1e1cSTejun Heo 	device_remove_file(dev, &uevent_attr);
113223681e47SGreg Kroah-Hartman  attrError:
1133312c004dSKay Sievers 	kobject_uevent(&dev->kobj, KOBJ_REMOVE);
11341da177e4SLinus Torvalds 	kobject_del(&dev->kobj);
11351da177e4SLinus Torvalds  Error:
113663b6971aSCornelia Huck 	cleanup_device_parent(dev);
11371da177e4SLinus Torvalds 	if (parent)
11381da177e4SLinus Torvalds 		put_device(parent);
11395c8563d7SKay Sievers name_error:
11405c8563d7SKay Sievers 	kfree(dev->p);
11415c8563d7SKay Sievers 	dev->p = NULL;
1142c906a48aSGreg Kroah-Hartman 	goto done;
11431da177e4SLinus Torvalds }
11441da177e4SLinus Torvalds 
11451da177e4SLinus Torvalds /**
11461da177e4SLinus Torvalds  * device_register - register a device with the system.
11471da177e4SLinus Torvalds  * @dev: pointer to the device structure
11481da177e4SLinus Torvalds  *
11491da177e4SLinus Torvalds  * This happens in two clean steps - initialize the device
11501da177e4SLinus Torvalds  * and add it to the system. The two steps can be called
11511da177e4SLinus Torvalds  * separately, but this is the easiest and most common.
11521da177e4SLinus Torvalds  * I.e. you should only call the two helpers separately if
11531da177e4SLinus Torvalds  * have a clearly defined need to use and refcount the device
11541da177e4SLinus Torvalds  * before it is added to the hierarchy.
11555739411aSCornelia Huck  *
1156b10d5efdSAlan Stern  * For more information, see the kerneldoc for device_initialize()
1157b10d5efdSAlan Stern  * and device_add().
1158b10d5efdSAlan Stern  *
11595739411aSCornelia Huck  * NOTE: _Never_ directly free @dev after calling this function, even
11605739411aSCornelia Huck  * if it returned an error! Always use put_device() to give up the
11615739411aSCornelia Huck  * reference initialized in this function instead.
11621da177e4SLinus Torvalds  */
11631da177e4SLinus Torvalds int device_register(struct device *dev)
11641da177e4SLinus Torvalds {
11651da177e4SLinus Torvalds 	device_initialize(dev);
11661da177e4SLinus Torvalds 	return device_add(dev);
11671da177e4SLinus Torvalds }
11681da177e4SLinus Torvalds 
11691da177e4SLinus Torvalds /**
11701da177e4SLinus Torvalds  * get_device - increment reference count for device.
11711da177e4SLinus Torvalds  * @dev: device.
11721da177e4SLinus Torvalds  *
11731da177e4SLinus Torvalds  * This simply forwards the call to kobject_get(), though
11741da177e4SLinus Torvalds  * we do take care to provide for the case that we get a NULL
11751da177e4SLinus Torvalds  * pointer passed in.
11761da177e4SLinus Torvalds  */
11771da177e4SLinus Torvalds struct device *get_device(struct device *dev)
11781da177e4SLinus Torvalds {
1179b0d1f807SLars-Peter Clausen 	return dev ? kobj_to_dev(kobject_get(&dev->kobj)) : NULL;
11801da177e4SLinus Torvalds }
11811da177e4SLinus Torvalds 
11821da177e4SLinus Torvalds /**
11831da177e4SLinus Torvalds  * put_device - decrement reference count.
11841da177e4SLinus Torvalds  * @dev: device in question.
11851da177e4SLinus Torvalds  */
11861da177e4SLinus Torvalds void put_device(struct device *dev)
11871da177e4SLinus Torvalds {
1188edfaa7c3SKay Sievers 	/* might_sleep(); */
11891da177e4SLinus Torvalds 	if (dev)
11901da177e4SLinus Torvalds 		kobject_put(&dev->kobj);
11911da177e4SLinus Torvalds }
11921da177e4SLinus Torvalds 
11931da177e4SLinus Torvalds /**
11941da177e4SLinus Torvalds  * device_del - delete device from system.
11951da177e4SLinus Torvalds  * @dev: device.
11961da177e4SLinus Torvalds  *
11971da177e4SLinus Torvalds  * This is the first part of the device unregistration
11981da177e4SLinus Torvalds  * sequence. This removes the device from the lists we control
11991da177e4SLinus Torvalds  * from here, has it removed from the other driver model
12001da177e4SLinus Torvalds  * subsystems it was added to in device_add(), and removes it
12011da177e4SLinus Torvalds  * from the kobject hierarchy.
12021da177e4SLinus Torvalds  *
12031da177e4SLinus Torvalds  * NOTE: this should be called manually _iff_ device_add() was
12041da177e4SLinus Torvalds  * also called manually.
12051da177e4SLinus Torvalds  */
12061da177e4SLinus Torvalds void device_del(struct device *dev)
12071da177e4SLinus Torvalds {
12081da177e4SLinus Torvalds 	struct device *parent = dev->parent;
1209c47ed219SGreg Kroah-Hartman 	struct class_interface *class_intf;
12101da177e4SLinus Torvalds 
1211ec0676eeSAlan Stern 	/* Notify clients of device removal.  This call must come
1212ec0676eeSAlan Stern 	 * before dpm_sysfs_remove().
1213ec0676eeSAlan Stern 	 */
1214ec0676eeSAlan Stern 	if (dev->bus)
1215ec0676eeSAlan Stern 		blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
1216ec0676eeSAlan Stern 					     BUS_NOTIFY_DEL_DEVICE, dev);
12173b98aeafSAlan Stern 	dpm_sysfs_remove(dev);
12181da177e4SLinus Torvalds 	if (parent)
1219f791b8c8SGreg Kroah-Hartman 		klist_del(&dev->p->knode_parent);
1220e105b8bfSDan Williams 	if (MAJOR(dev->devt)) {
12212b2af54aSKay Sievers 		devtmpfs_delete_node(dev);
1222e105b8bfSDan Williams 		device_remove_sys_dev_entry(dev);
1223ad6a1e1cSTejun Heo 		device_remove_file(dev, &devt_attr);
1224e105b8bfSDan Williams 	}
1225b9d9c82bSKay Sievers 	if (dev->class) {
1226da231fd5SKay Sievers 		device_remove_class_symlinks(dev);
122799ef3ef8SKay Sievers 
1228ca22e56dSKay Sievers 		mutex_lock(&dev->class->p->mutex);
1229c47ed219SGreg Kroah-Hartman 		/* notify any interfaces that the device is now gone */
1230184f1f77SGreg Kroah-Hartman 		list_for_each_entry(class_intf,
1231ca22e56dSKay Sievers 				    &dev->class->p->interfaces, node)
1232c47ed219SGreg Kroah-Hartman 			if (class_intf->remove_dev)
1233c47ed219SGreg Kroah-Hartman 				class_intf->remove_dev(dev, class_intf);
1234c47ed219SGreg Kroah-Hartman 		/* remove the device from the class list */
12355a3ceb86STejun Heo 		klist_del(&dev->knode_class);
1236ca22e56dSKay Sievers 		mutex_unlock(&dev->class->p->mutex);
1237b9d9c82bSKay Sievers 	}
1238ad6a1e1cSTejun Heo 	device_remove_file(dev, &uevent_attr);
12392620efefSGreg Kroah-Hartman 	device_remove_attrs(dev);
124028953533SBenjamin Herrenschmidt 	bus_remove_device(dev);
12414b6d1f12SLongX Zhang 	device_pm_remove(dev);
1242d1c3414cSGrant Likely 	driver_deferred_probe_del(dev);
12431da177e4SLinus Torvalds 
12441da177e4SLinus Torvalds 	/* Notify the platform of the removal, in case they
12451da177e4SLinus Torvalds 	 * need to do anything...
12461da177e4SLinus Torvalds 	 */
12471da177e4SLinus Torvalds 	if (platform_notify_remove)
12481da177e4SLinus Torvalds 		platform_notify_remove(dev);
1249312c004dSKay Sievers 	kobject_uevent(&dev->kobj, KOBJ_REMOVE);
1250da231fd5SKay Sievers 	cleanup_device_parent(dev);
12511da177e4SLinus Torvalds 	kobject_del(&dev->kobj);
12521da177e4SLinus Torvalds 	put_device(parent);
12531da177e4SLinus Torvalds }
12541da177e4SLinus Torvalds 
12551da177e4SLinus Torvalds /**
12561da177e4SLinus Torvalds  * device_unregister - unregister device from system.
12571da177e4SLinus Torvalds  * @dev: device going away.
12581da177e4SLinus Torvalds  *
12591da177e4SLinus Torvalds  * We do this in two parts, like we do device_register(). First,
12601da177e4SLinus Torvalds  * we remove it from all the subsystems with device_del(), then
12611da177e4SLinus Torvalds  * we decrement the reference count via put_device(). If that
12621da177e4SLinus Torvalds  * is the final reference count, the device will be cleaned up
12631da177e4SLinus Torvalds  * via device_release() above. Otherwise, the structure will
12641da177e4SLinus Torvalds  * stick around until the final reference to the device is dropped.
12651da177e4SLinus Torvalds  */
12661da177e4SLinus Torvalds void device_unregister(struct device *dev)
12671da177e4SLinus Torvalds {
12681e0b2cf9SKay Sievers 	pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
12691da177e4SLinus Torvalds 	device_del(dev);
12701da177e4SLinus Torvalds 	put_device(dev);
12711da177e4SLinus Torvalds }
12721da177e4SLinus Torvalds 
127336239577S[email protected] static struct device *next_device(struct klist_iter *i)
127436239577S[email protected] {
127536239577S[email protected] 	struct klist_node *n = klist_next(i);
1276f791b8c8SGreg Kroah-Hartman 	struct device *dev = NULL;
1277f791b8c8SGreg Kroah-Hartman 	struct device_private *p;
1278f791b8c8SGreg Kroah-Hartman 
1279f791b8c8SGreg Kroah-Hartman 	if (n) {
1280f791b8c8SGreg Kroah-Hartman 		p = to_device_private_parent(n);
1281f791b8c8SGreg Kroah-Hartman 		dev = p->device;
1282f791b8c8SGreg Kroah-Hartman 	}
1283f791b8c8SGreg Kroah-Hartman 	return dev;
128436239577S[email protected] }
128536239577S[email protected] 
12861da177e4SLinus Torvalds /**
1287e454cea2SKay Sievers  * device_get_devnode - path of device node file
12886fcf53acSKay Sievers  * @dev: device
1289e454cea2SKay Sievers  * @mode: returned file access mode
12903c2670e6SKay Sievers  * @uid: returned file owner
12913c2670e6SKay Sievers  * @gid: returned file group
12926fcf53acSKay Sievers  * @tmp: possibly allocated string
12936fcf53acSKay Sievers  *
12946fcf53acSKay Sievers  * Return the relative path of a possible device node.
12956fcf53acSKay Sievers  * Non-default names may need to allocate a memory to compose
12966fcf53acSKay Sievers  * a name. This memory is returned in tmp and needs to be
12976fcf53acSKay Sievers  * freed by the caller.
12986fcf53acSKay Sievers  */
1299e454cea2SKay Sievers const char *device_get_devnode(struct device *dev,
13004e4098a3SGreg Kroah-Hartman 			       umode_t *mode, kuid_t *uid, kgid_t *gid,
13013c2670e6SKay Sievers 			       const char **tmp)
13026fcf53acSKay Sievers {
13036fcf53acSKay Sievers 	char *s;
13046fcf53acSKay Sievers 
13056fcf53acSKay Sievers 	*tmp = NULL;
13066fcf53acSKay Sievers 
13076fcf53acSKay Sievers 	/* the device type may provide a specific name */
1308e454cea2SKay Sievers 	if (dev->type && dev->type->devnode)
13093c2670e6SKay Sievers 		*tmp = dev->type->devnode(dev, mode, uid, gid);
13106fcf53acSKay Sievers 	if (*tmp)
13116fcf53acSKay Sievers 		return *tmp;
13126fcf53acSKay Sievers 
13136fcf53acSKay Sievers 	/* the class may provide a specific name */
1314e454cea2SKay Sievers 	if (dev->class && dev->class->devnode)
1315e454cea2SKay Sievers 		*tmp = dev->class->devnode(dev, mode);
13166fcf53acSKay Sievers 	if (*tmp)
13176fcf53acSKay Sievers 		return *tmp;
13186fcf53acSKay Sievers 
13196fcf53acSKay Sievers 	/* return name without allocation, tmp == NULL */
13206fcf53acSKay Sievers 	if (strchr(dev_name(dev), '!') == NULL)
13216fcf53acSKay Sievers 		return dev_name(dev);
13226fcf53acSKay Sievers 
13236fcf53acSKay Sievers 	/* replace '!' in the name with '/' */
13246fcf53acSKay Sievers 	*tmp = kstrdup(dev_name(dev), GFP_KERNEL);
13256fcf53acSKay Sievers 	if (!*tmp)
13266fcf53acSKay Sievers 		return NULL;
13276fcf53acSKay Sievers 	while ((s = strchr(*tmp, '!')))
13286fcf53acSKay Sievers 		s[0] = '/';
13296fcf53acSKay Sievers 	return *tmp;
13306fcf53acSKay Sievers }
13316fcf53acSKay Sievers 
13326fcf53acSKay Sievers /**
13331da177e4SLinus Torvalds  * device_for_each_child - device child iterator.
1334c41455fbSRandy Dunlap  * @parent: parent struct device.
13351da177e4SLinus Torvalds  * @data: data for the callback.
13361da177e4SLinus Torvalds  * @fn: function to be called for each device.
13371da177e4SLinus Torvalds  *
1338c41455fbSRandy Dunlap  * Iterate over @parent's child devices, and call @fn for each,
13391da177e4SLinus Torvalds  * passing it @data.
13401da177e4SLinus Torvalds  *
13411da177e4SLinus Torvalds  * We check the return of @fn each time. If it returns anything
13421da177e4SLinus Torvalds  * other than 0, we break out and return that value.
13431da177e4SLinus Torvalds  */
134436239577S[email protected] int device_for_each_child(struct device *parent, void *data,
13454a3ad20cSGreg Kroah-Hartman 			  int (*fn)(struct device *dev, void *data))
13461da177e4SLinus Torvalds {
134736239577S[email protected] 	struct klist_iter i;
13481da177e4SLinus Torvalds 	struct device *child;
13491da177e4SLinus Torvalds 	int error = 0;
13501da177e4SLinus Torvalds 
1351014c90dbSGreg Kroah-Hartman 	if (!parent->p)
1352014c90dbSGreg Kroah-Hartman 		return 0;
1353014c90dbSGreg Kroah-Hartman 
1354f791b8c8SGreg Kroah-Hartman 	klist_iter_init(&parent->p->klist_children, &i);
135536239577S[email protected] 	while ((child = next_device(&i)) && !error)
135636239577S[email protected] 		error = fn(child, data);
135736239577S[email protected] 	klist_iter_exit(&i);
13581da177e4SLinus Torvalds 	return error;
13591da177e4SLinus Torvalds }
13601da177e4SLinus Torvalds 
13615ab69981SCornelia Huck /**
13625ab69981SCornelia Huck  * device_find_child - device iterator for locating a particular device.
13635ab69981SCornelia Huck  * @parent: parent struct device
13645ab69981SCornelia Huck  * @data: Data to pass to match function
13655ab69981SCornelia Huck  * @match: Callback function to check device
13665ab69981SCornelia Huck  *
13675ab69981SCornelia Huck  * This is similar to the device_for_each_child() function above, but it
13685ab69981SCornelia Huck  * returns a reference to a device that is 'found' for later use, as
13695ab69981SCornelia Huck  * determined by the @match callback.
13705ab69981SCornelia Huck  *
13715ab69981SCornelia Huck  * The callback should return 0 if the device doesn't match and non-zero
13725ab69981SCornelia Huck  * if it does.  If the callback returns non-zero and a reference to the
13735ab69981SCornelia Huck  * current device can be obtained, this function will return to the caller
13745ab69981SCornelia Huck  * and not iterate over any more devices.
1375*a4e2400aSFederico Vaga  *
1376*a4e2400aSFederico Vaga  * NOTE: you will need to drop the reference with put_device() after use.
13775ab69981SCornelia Huck  */
13785ab69981SCornelia Huck struct device *device_find_child(struct device *parent, void *data,
13794a3ad20cSGreg Kroah-Hartman 				 int (*match)(struct device *dev, void *data))
13805ab69981SCornelia Huck {
13815ab69981SCornelia Huck 	struct klist_iter i;
13825ab69981SCornelia Huck 	struct device *child;
13835ab69981SCornelia Huck 
13845ab69981SCornelia Huck 	if (!parent)
13855ab69981SCornelia Huck 		return NULL;
13865ab69981SCornelia Huck 
1387f791b8c8SGreg Kroah-Hartman 	klist_iter_init(&parent->p->klist_children, &i);
13885ab69981SCornelia Huck 	while ((child = next_device(&i)))
13895ab69981SCornelia Huck 		if (match(child, data) && get_device(child))
13905ab69981SCornelia Huck 			break;
13915ab69981SCornelia Huck 	klist_iter_exit(&i);
13925ab69981SCornelia Huck 	return child;
13935ab69981SCornelia Huck }
13945ab69981SCornelia Huck 
13951da177e4SLinus Torvalds int __init devices_init(void)
13961da177e4SLinus Torvalds {
1397881c6cfdSGreg Kroah-Hartman 	devices_kset = kset_create_and_add("devices", &device_uevent_ops, NULL);
1398881c6cfdSGreg Kroah-Hartman 	if (!devices_kset)
1399881c6cfdSGreg Kroah-Hartman 		return -ENOMEM;
1400e105b8bfSDan Williams 	dev_kobj = kobject_create_and_add("dev", NULL);
1401e105b8bfSDan Williams 	if (!dev_kobj)
1402e105b8bfSDan Williams 		goto dev_kobj_err;
1403e105b8bfSDan Williams 	sysfs_dev_block_kobj = kobject_create_and_add("block", dev_kobj);
1404e105b8bfSDan Williams 	if (!sysfs_dev_block_kobj)
1405e105b8bfSDan Williams 		goto block_kobj_err;
1406e105b8bfSDan Williams 	sysfs_dev_char_kobj = kobject_create_and_add("char", dev_kobj);
1407e105b8bfSDan Williams 	if (!sysfs_dev_char_kobj)
1408e105b8bfSDan Williams 		goto char_kobj_err;
1409e105b8bfSDan Williams 
1410881c6cfdSGreg Kroah-Hartman 	return 0;
1411e105b8bfSDan Williams 
1412e105b8bfSDan Williams  char_kobj_err:
1413e105b8bfSDan Williams 	kobject_put(sysfs_dev_block_kobj);
1414e105b8bfSDan Williams  block_kobj_err:
1415e105b8bfSDan Williams 	kobject_put(dev_kobj);
1416e105b8bfSDan Williams  dev_kobj_err:
1417e105b8bfSDan Williams 	kset_unregister(devices_kset);
1418e105b8bfSDan Williams 	return -ENOMEM;
14191da177e4SLinus Torvalds }
14201da177e4SLinus Torvalds 
14211da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(device_for_each_child);
14225ab69981SCornelia Huck EXPORT_SYMBOL_GPL(device_find_child);
14231da177e4SLinus Torvalds 
14241da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(device_initialize);
14251da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(device_add);
14261da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(device_register);
14271da177e4SLinus Torvalds 
14281da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(device_del);
14291da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(device_unregister);
14301da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(get_device);
14311da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(put_device);
14321da177e4SLinus Torvalds 
14331da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(device_create_file);
14341da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(device_remove_file);
143523681e47SGreg Kroah-Hartman 
14367f100d15SKarthigan Srinivasan struct root_device {
14370aa0dc41SMark McLoughlin 	struct device dev;
14380aa0dc41SMark McLoughlin 	struct module *owner;
14390aa0dc41SMark McLoughlin };
14400aa0dc41SMark McLoughlin 
144193058424SJosh Triplett static inline struct root_device *to_root_device(struct device *d)
1442481e2079SFerenc Wagner {
1443481e2079SFerenc Wagner 	return container_of(d, struct root_device, dev);
1444481e2079SFerenc Wagner }
14450aa0dc41SMark McLoughlin 
14460aa0dc41SMark McLoughlin static void root_device_release(struct device *dev)
14470aa0dc41SMark McLoughlin {
14480aa0dc41SMark McLoughlin 	kfree(to_root_device(dev));
14490aa0dc41SMark McLoughlin }
14500aa0dc41SMark McLoughlin 
14510aa0dc41SMark McLoughlin /**
14520aa0dc41SMark McLoughlin  * __root_device_register - allocate and register a root device
14530aa0dc41SMark McLoughlin  * @name: root device name
14540aa0dc41SMark McLoughlin  * @owner: owner module of the root device, usually THIS_MODULE
14550aa0dc41SMark McLoughlin  *
14560aa0dc41SMark McLoughlin  * This function allocates a root device and registers it
14570aa0dc41SMark McLoughlin  * using device_register(). In order to free the returned
14580aa0dc41SMark McLoughlin  * device, use root_device_unregister().
14590aa0dc41SMark McLoughlin  *
14600aa0dc41SMark McLoughlin  * Root devices are dummy devices which allow other devices
14610aa0dc41SMark McLoughlin  * to be grouped under /sys/devices. Use this function to
14620aa0dc41SMark McLoughlin  * allocate a root device and then use it as the parent of
14630aa0dc41SMark McLoughlin  * any device which should appear under /sys/devices/{name}
14640aa0dc41SMark McLoughlin  *
14650aa0dc41SMark McLoughlin  * The /sys/devices/{name} directory will also contain a
14660aa0dc41SMark McLoughlin  * 'module' symlink which points to the @owner directory
14670aa0dc41SMark McLoughlin  * in sysfs.
14680aa0dc41SMark McLoughlin  *
1469f0eae0edSJani Nikula  * Returns &struct device pointer on success, or ERR_PTR() on error.
1470f0eae0edSJani Nikula  *
14710aa0dc41SMark McLoughlin  * Note: You probably want to use root_device_register().
14720aa0dc41SMark McLoughlin  */
14730aa0dc41SMark McLoughlin struct device *__root_device_register(const char *name, struct module *owner)
14740aa0dc41SMark McLoughlin {
14750aa0dc41SMark McLoughlin 	struct root_device *root;
14760aa0dc41SMark McLoughlin 	int err = -ENOMEM;
14770aa0dc41SMark McLoughlin 
14780aa0dc41SMark McLoughlin 	root = kzalloc(sizeof(struct root_device), GFP_KERNEL);
14790aa0dc41SMark McLoughlin 	if (!root)
14800aa0dc41SMark McLoughlin 		return ERR_PTR(err);
14810aa0dc41SMark McLoughlin 
1482acc0e90fSGreg Kroah-Hartman 	err = dev_set_name(&root->dev, "%s", name);
14830aa0dc41SMark McLoughlin 	if (err) {
14840aa0dc41SMark McLoughlin 		kfree(root);
14850aa0dc41SMark McLoughlin 		return ERR_PTR(err);
14860aa0dc41SMark McLoughlin 	}
14870aa0dc41SMark McLoughlin 
14880aa0dc41SMark McLoughlin 	root->dev.release = root_device_release;
14890aa0dc41SMark McLoughlin 
14900aa0dc41SMark McLoughlin 	err = device_register(&root->dev);
14910aa0dc41SMark McLoughlin 	if (err) {
14920aa0dc41SMark McLoughlin 		put_device(&root->dev);
14930aa0dc41SMark McLoughlin 		return ERR_PTR(err);
14940aa0dc41SMark McLoughlin 	}
14950aa0dc41SMark McLoughlin 
14961d9e882bSChristoph Egger #ifdef CONFIG_MODULES	/* gotta find a "cleaner" way to do this */
14970aa0dc41SMark McLoughlin 	if (owner) {
14980aa0dc41SMark McLoughlin 		struct module_kobject *mk = &owner->mkobj;
14990aa0dc41SMark McLoughlin 
15000aa0dc41SMark McLoughlin 		err = sysfs_create_link(&root->dev.kobj, &mk->kobj, "module");
15010aa0dc41SMark McLoughlin 		if (err) {
15020aa0dc41SMark McLoughlin 			device_unregister(&root->dev);
15030aa0dc41SMark McLoughlin 			return ERR_PTR(err);
15040aa0dc41SMark McLoughlin 		}
15050aa0dc41SMark McLoughlin 		root->owner = owner;
15060aa0dc41SMark McLoughlin 	}
15070aa0dc41SMark McLoughlin #endif
15080aa0dc41SMark McLoughlin 
15090aa0dc41SMark McLoughlin 	return &root->dev;
15100aa0dc41SMark McLoughlin }
15110aa0dc41SMark McLoughlin EXPORT_SYMBOL_GPL(__root_device_register);
15120aa0dc41SMark McLoughlin 
15130aa0dc41SMark McLoughlin /**
15140aa0dc41SMark McLoughlin  * root_device_unregister - unregister and free a root device
15157cbcf225SRandy Dunlap  * @dev: device going away
15160aa0dc41SMark McLoughlin  *
15170aa0dc41SMark McLoughlin  * This function unregisters and cleans up a device that was created by
15180aa0dc41SMark McLoughlin  * root_device_register().
15190aa0dc41SMark McLoughlin  */
15200aa0dc41SMark McLoughlin void root_device_unregister(struct device *dev)
15210aa0dc41SMark McLoughlin {
15220aa0dc41SMark McLoughlin 	struct root_device *root = to_root_device(dev);
15230aa0dc41SMark McLoughlin 
15240aa0dc41SMark McLoughlin 	if (root->owner)
15250aa0dc41SMark McLoughlin 		sysfs_remove_link(&root->dev.kobj, "module");
15260aa0dc41SMark McLoughlin 
15270aa0dc41SMark McLoughlin 	device_unregister(dev);
15280aa0dc41SMark McLoughlin }
15290aa0dc41SMark McLoughlin EXPORT_SYMBOL_GPL(root_device_unregister);
15300aa0dc41SMark McLoughlin 
153123681e47SGreg Kroah-Hartman 
153223681e47SGreg Kroah-Hartman static void device_create_release(struct device *dev)
153323681e47SGreg Kroah-Hartman {
15341e0b2cf9SKay Sievers 	pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
153523681e47SGreg Kroah-Hartman 	kfree(dev);
153623681e47SGreg Kroah-Hartman }
153723681e47SGreg Kroah-Hartman 
153823681e47SGreg Kroah-Hartman /**
15398882b394SGreg Kroah-Hartman  * device_create_vargs - creates a device and registers it with sysfs
15408882b394SGreg Kroah-Hartman  * @class: pointer to the struct class that this device should be registered to
15418882b394SGreg Kroah-Hartman  * @parent: pointer to the parent struct device of this new device, if any
15428882b394SGreg Kroah-Hartman  * @devt: the dev_t for the char device to be added
15438882b394SGreg Kroah-Hartman  * @drvdata: the data to be added to the device for callbacks
15448882b394SGreg Kroah-Hartman  * @fmt: string for the device's name
15458882b394SGreg Kroah-Hartman  * @args: va_list for the device's name
15468882b394SGreg Kroah-Hartman  *
15478882b394SGreg Kroah-Hartman  * This function can be used by char device classes.  A struct device
15488882b394SGreg Kroah-Hartman  * will be created in sysfs, registered to the specified class.
15498882b394SGreg Kroah-Hartman  *
15508882b394SGreg Kroah-Hartman  * A "dev" file will be created, showing the dev_t for the device, if
15518882b394SGreg Kroah-Hartman  * the dev_t is not 0,0.
15528882b394SGreg Kroah-Hartman  * If a pointer to a parent struct device is passed in, the newly created
15538882b394SGreg Kroah-Hartman  * struct device will be a child of that device in sysfs.
15548882b394SGreg Kroah-Hartman  * The pointer to the struct device will be returned from the call.
15558882b394SGreg Kroah-Hartman  * Any further sysfs files that might be required can be created using this
15568882b394SGreg Kroah-Hartman  * pointer.
15578882b394SGreg Kroah-Hartman  *
1558f0eae0edSJani Nikula  * Returns &struct device pointer on success, or ERR_PTR() on error.
1559f0eae0edSJani Nikula  *
15608882b394SGreg Kroah-Hartman  * Note: the struct class passed to this function must have previously
15618882b394SGreg Kroah-Hartman  * been created with a call to class_create().
15628882b394SGreg Kroah-Hartman  */
15638882b394SGreg Kroah-Hartman struct device *device_create_vargs(struct class *class, struct device *parent,
15648882b394SGreg Kroah-Hartman 				   dev_t devt, void *drvdata, const char *fmt,
15658882b394SGreg Kroah-Hartman 				   va_list args)
15668882b394SGreg Kroah-Hartman {
15678882b394SGreg Kroah-Hartman 	struct device *dev = NULL;
15688882b394SGreg Kroah-Hartman 	int retval = -ENODEV;
15698882b394SGreg Kroah-Hartman 
15708882b394SGreg Kroah-Hartman 	if (class == NULL || IS_ERR(class))
15718882b394SGreg Kroah-Hartman 		goto error;
15728882b394SGreg Kroah-Hartman 
15738882b394SGreg Kroah-Hartman 	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
15748882b394SGreg Kroah-Hartman 	if (!dev) {
15758882b394SGreg Kroah-Hartman 		retval = -ENOMEM;
15768882b394SGreg Kroah-Hartman 		goto error;
15778882b394SGreg Kroah-Hartman 	}
15788882b394SGreg Kroah-Hartman 
15798882b394SGreg Kroah-Hartman 	dev->devt = devt;
15808882b394SGreg Kroah-Hartman 	dev->class = class;
15818882b394SGreg Kroah-Hartman 	dev->parent = parent;
15828882b394SGreg Kroah-Hartman 	dev->release = device_create_release;
15838882b394SGreg Kroah-Hartman 	dev_set_drvdata(dev, drvdata);
15848882b394SGreg Kroah-Hartman 
15851fa5ae85SKay Sievers 	retval = kobject_set_name_vargs(&dev->kobj, fmt, args);
15861fa5ae85SKay Sievers 	if (retval)
15871fa5ae85SKay Sievers 		goto error;
15881fa5ae85SKay Sievers 
15898882b394SGreg Kroah-Hartman 	retval = device_register(dev);
15908882b394SGreg Kroah-Hartman 	if (retval)
15918882b394SGreg Kroah-Hartman 		goto error;
15928882b394SGreg Kroah-Hartman 
15938882b394SGreg Kroah-Hartman 	return dev;
15948882b394SGreg Kroah-Hartman 
15958882b394SGreg Kroah-Hartman error:
1596286661b3SCornelia Huck 	put_device(dev);
15978882b394SGreg Kroah-Hartman 	return ERR_PTR(retval);
15988882b394SGreg Kroah-Hartman }
15998882b394SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(device_create_vargs);
16008882b394SGreg Kroah-Hartman 
16018882b394SGreg Kroah-Hartman /**
16024e106739SGreg Kroah-Hartman  * device_create - creates a device and registers it with sysfs
16038882b394SGreg Kroah-Hartman  * @class: pointer to the struct class that this device should be registered to
16048882b394SGreg Kroah-Hartman  * @parent: pointer to the parent struct device of this new device, if any
16058882b394SGreg Kroah-Hartman  * @devt: the dev_t for the char device to be added
16068882b394SGreg Kroah-Hartman  * @drvdata: the data to be added to the device for callbacks
16078882b394SGreg Kroah-Hartman  * @fmt: string for the device's name
16088882b394SGreg Kroah-Hartman  *
16098882b394SGreg Kroah-Hartman  * This function can be used by char device classes.  A struct device
16108882b394SGreg Kroah-Hartman  * will be created in sysfs, registered to the specified class.
16118882b394SGreg Kroah-Hartman  *
16128882b394SGreg Kroah-Hartman  * A "dev" file will be created, showing the dev_t for the device, if
16138882b394SGreg Kroah-Hartman  * the dev_t is not 0,0.
16148882b394SGreg Kroah-Hartman  * If a pointer to a parent struct device is passed in, the newly created
16158882b394SGreg Kroah-Hartman  * struct device will be a child of that device in sysfs.
16168882b394SGreg Kroah-Hartman  * The pointer to the struct device will be returned from the call.
16178882b394SGreg Kroah-Hartman  * Any further sysfs files that might be required can be created using this
16188882b394SGreg Kroah-Hartman  * pointer.
16198882b394SGreg Kroah-Hartman  *
1620f0eae0edSJani Nikula  * Returns &struct device pointer on success, or ERR_PTR() on error.
1621f0eae0edSJani Nikula  *
16228882b394SGreg Kroah-Hartman  * Note: the struct class passed to this function must have previously
16238882b394SGreg Kroah-Hartman  * been created with a call to class_create().
16248882b394SGreg Kroah-Hartman  */
16254e106739SGreg Kroah-Hartman struct device *device_create(struct class *class, struct device *parent,
16264e106739SGreg Kroah-Hartman 			     dev_t devt, void *drvdata, const char *fmt, ...)
16278882b394SGreg Kroah-Hartman {
16288882b394SGreg Kroah-Hartman 	va_list vargs;
16298882b394SGreg Kroah-Hartman 	struct device *dev;
16308882b394SGreg Kroah-Hartman 
16318882b394SGreg Kroah-Hartman 	va_start(vargs, fmt);
16328882b394SGreg Kroah-Hartman 	dev = device_create_vargs(class, parent, devt, drvdata, fmt, vargs);
16338882b394SGreg Kroah-Hartman 	va_end(vargs);
16348882b394SGreg Kroah-Hartman 	return dev;
16358882b394SGreg Kroah-Hartman }
16364e106739SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(device_create);
16378882b394SGreg Kroah-Hartman 
16389f3b795aSMichał Mirosław static int __match_devt(struct device *dev, const void *data)
163923681e47SGreg Kroah-Hartman {
16409f3b795aSMichał Mirosław 	const dev_t *devt = data;
164123681e47SGreg Kroah-Hartman 
1642cd35449bSDave Young 	return dev->devt == *devt;
1643775b64d2SRafael J. Wysocki }
164423681e47SGreg Kroah-Hartman 
1645775b64d2SRafael J. Wysocki /**
1646775b64d2SRafael J. Wysocki  * device_destroy - removes a device that was created with device_create()
1647775b64d2SRafael J. Wysocki  * @class: pointer to the struct class that this device was registered with
1648775b64d2SRafael J. Wysocki  * @devt: the dev_t of the device that was previously registered
1649775b64d2SRafael J. Wysocki  *
1650775b64d2SRafael J. Wysocki  * This call unregisters and cleans up a device that was created with a
1651775b64d2SRafael J. Wysocki  * call to device_create().
1652775b64d2SRafael J. Wysocki  */
1653775b64d2SRafael J. Wysocki void device_destroy(struct class *class, dev_t devt)
1654775b64d2SRafael J. Wysocki {
1655775b64d2SRafael J. Wysocki 	struct device *dev;
1656775b64d2SRafael J. Wysocki 
1657695794aeSGreg Kroah-Hartman 	dev = class_find_device(class, NULL, &devt, __match_devt);
1658cd35449bSDave Young 	if (dev) {
1659cd35449bSDave Young 		put_device(dev);
166023681e47SGreg Kroah-Hartman 		device_unregister(dev);
166123681e47SGreg Kroah-Hartman 	}
1662cd35449bSDave Young }
166323681e47SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(device_destroy);
1664a2de48caSGreg Kroah-Hartman 
1665a2de48caSGreg Kroah-Hartman /**
1666a2de48caSGreg Kroah-Hartman  * device_rename - renames a device
1667a2de48caSGreg Kroah-Hartman  * @dev: the pointer to the struct device to be renamed
1668a2de48caSGreg Kroah-Hartman  * @new_name: the new name of the device
1669030c1d2bSEric W. Biederman  *
1670030c1d2bSEric W. Biederman  * It is the responsibility of the caller to provide mutual
1671030c1d2bSEric W. Biederman  * exclusion between two different calls of device_rename
1672030c1d2bSEric W. Biederman  * on the same device to ensure that new_name is valid and
1673030c1d2bSEric W. Biederman  * won't conflict with other devices.
1674c6c0ac66SMichael Ellerman  *
1675a5462516STimur Tabi  * Note: Don't call this function.  Currently, the networking layer calls this
1676a5462516STimur Tabi  * function, but that will change.  The following text from Kay Sievers offers
1677a5462516STimur Tabi  * some insight:
1678a5462516STimur Tabi  *
1679a5462516STimur Tabi  * Renaming devices is racy at many levels, symlinks and other stuff are not
1680a5462516STimur Tabi  * replaced atomically, and you get a "move" uevent, but it's not easy to
1681a5462516STimur Tabi  * connect the event to the old and new device. Device nodes are not renamed at
1682a5462516STimur Tabi  * all, there isn't even support for that in the kernel now.
1683a5462516STimur Tabi  *
1684a5462516STimur Tabi  * In the meantime, during renaming, your target name might be taken by another
1685a5462516STimur Tabi  * driver, creating conflicts. Or the old name is taken directly after you
1686a5462516STimur Tabi  * renamed it -- then you get events for the same DEVPATH, before you even see
1687a5462516STimur Tabi  * the "move" event. It's just a mess, and nothing new should ever rely on
1688a5462516STimur Tabi  * kernel device renaming. Besides that, it's not even implemented now for
1689a5462516STimur Tabi  * other things than (driver-core wise very simple) network devices.
1690a5462516STimur Tabi  *
1691a5462516STimur Tabi  * We are currently about to change network renaming in udev to completely
1692a5462516STimur Tabi  * disallow renaming of devices in the same namespace as the kernel uses,
1693a5462516STimur Tabi  * because we can't solve the problems properly, that arise with swapping names
1694a5462516STimur Tabi  * of multiple interfaces without races. Means, renaming of eth[0-9]* will only
1695a5462516STimur Tabi  * be allowed to some other name than eth[0-9]*, for the aforementioned
1696a5462516STimur Tabi  * reasons.
1697a5462516STimur Tabi  *
1698a5462516STimur Tabi  * Make up a "real" name in the driver before you register anything, or add
1699a5462516STimur Tabi  * some other attributes for userspace to find the device, or use udev to add
1700a5462516STimur Tabi  * symlinks -- but never rename kernel devices later, it's a complete mess. We
1701a5462516STimur Tabi  * don't even want to get into that and try to implement the missing pieces in
1702a5462516STimur Tabi  * the core. We really have other pieces to fix in the driver core mess. :)
1703a2de48caSGreg Kroah-Hartman  */
17046937e8f8SJohannes Berg int device_rename(struct device *dev, const char *new_name)
1705a2de48caSGreg Kroah-Hartman {
17062ee97cafSCornelia Huck 	char *old_device_name = NULL;
1707a2de48caSGreg Kroah-Hartman 	int error;
1708a2de48caSGreg Kroah-Hartman 
1709a2de48caSGreg Kroah-Hartman 	dev = get_device(dev);
1710a2de48caSGreg Kroah-Hartman 	if (!dev)
1711a2de48caSGreg Kroah-Hartman 		return -EINVAL;
1712a2de48caSGreg Kroah-Hartman 
17131e0b2cf9SKay Sievers 	pr_debug("device: '%s': %s: renaming to '%s'\n", dev_name(dev),
17142b3a302aSHarvey Harrison 		 __func__, new_name);
1715a2de48caSGreg Kroah-Hartman 
17161fa5ae85SKay Sievers 	old_device_name = kstrdup(dev_name(dev), GFP_KERNEL);
17172ee97cafSCornelia Huck 	if (!old_device_name) {
1718952ab431SJesper Juhl 		error = -ENOMEM;
17192ee97cafSCornelia Huck 		goto out;
1720952ab431SJesper Juhl 	}
1721a2de48caSGreg Kroah-Hartman 
1722f349cf34SEric W. Biederman 	if (dev->class) {
17236b6e39a6SKay Sievers 		error = sysfs_rename_link(&dev->class->p->subsys.kobj,
1724f349cf34SEric W. Biederman 			&dev->kobj, old_device_name, new_name);
1725f349cf34SEric W. Biederman 		if (error)
1726f349cf34SEric W. Biederman 			goto out;
1727f349cf34SEric W. Biederman 	}
172839aba963SKay Sievers 
1729a2de48caSGreg Kroah-Hartman 	error = kobject_rename(&dev->kobj, new_name);
17301fa5ae85SKay Sievers 	if (error)
17312ee97cafSCornelia Huck 		goto out;
1732a2de48caSGreg Kroah-Hartman 
17332ee97cafSCornelia Huck out:
1734a2de48caSGreg Kroah-Hartman 	put_device(dev);
1735a2de48caSGreg Kroah-Hartman 
17362ee97cafSCornelia Huck 	kfree(old_device_name);
1737a2de48caSGreg Kroah-Hartman 
1738a2de48caSGreg Kroah-Hartman 	return error;
1739a2de48caSGreg Kroah-Hartman }
1740a2807dbcSJohannes Berg EXPORT_SYMBOL_GPL(device_rename);
17418a82472fSCornelia Huck 
17428a82472fSCornelia Huck static int device_move_class_links(struct device *dev,
17438a82472fSCornelia Huck 				   struct device *old_parent,
17448a82472fSCornelia Huck 				   struct device *new_parent)
17458a82472fSCornelia Huck {
1746f7f3461dSGreg Kroah-Hartman 	int error = 0;
17478a82472fSCornelia Huck 
1748f7f3461dSGreg Kroah-Hartman 	if (old_parent)
1749f7f3461dSGreg Kroah-Hartman 		sysfs_remove_link(&dev->kobj, "device");
1750f7f3461dSGreg Kroah-Hartman 	if (new_parent)
1751f7f3461dSGreg Kroah-Hartman 		error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
1752f7f3461dSGreg Kroah-Hartman 					  "device");
1753f7f3461dSGreg Kroah-Hartman 	return error;
17548a82472fSCornelia Huck }
17558a82472fSCornelia Huck 
17568a82472fSCornelia Huck /**
17578a82472fSCornelia Huck  * device_move - moves a device to a new parent
17588a82472fSCornelia Huck  * @dev: the pointer to the struct device to be moved
1759c744aeaeSCornelia Huck  * @new_parent: the new parent of the device (can by NULL)
1760ffa6a705SCornelia Huck  * @dpm_order: how to reorder the dpm_list
17618a82472fSCornelia Huck  */
1762ffa6a705SCornelia Huck int device_move(struct device *dev, struct device *new_parent,
1763ffa6a705SCornelia Huck 		enum dpm_order dpm_order)
17648a82472fSCornelia Huck {
17658a82472fSCornelia Huck 	int error;
17668a82472fSCornelia Huck 	struct device *old_parent;
1767c744aeaeSCornelia Huck 	struct kobject *new_parent_kobj;
17688a82472fSCornelia Huck 
17698a82472fSCornelia Huck 	dev = get_device(dev);
17708a82472fSCornelia Huck 	if (!dev)
17718a82472fSCornelia Huck 		return -EINVAL;
17728a82472fSCornelia Huck 
1773ffa6a705SCornelia Huck 	device_pm_lock();
17748a82472fSCornelia Huck 	new_parent = get_device(new_parent);
1775c744aeaeSCornelia Huck 	new_parent_kobj = get_device_parent(dev, new_parent);
177663b6971aSCornelia Huck 
17771e0b2cf9SKay Sievers 	pr_debug("device: '%s': %s: moving to '%s'\n", dev_name(dev),
17781e0b2cf9SKay Sievers 		 __func__, new_parent ? dev_name(new_parent) : "<NULL>");
1779c744aeaeSCornelia Huck 	error = kobject_move(&dev->kobj, new_parent_kobj);
17808a82472fSCornelia Huck 	if (error) {
178163b6971aSCornelia Huck 		cleanup_glue_dir(dev, new_parent_kobj);
17828a82472fSCornelia Huck 		put_device(new_parent);
17838a82472fSCornelia Huck 		goto out;
17848a82472fSCornelia Huck 	}
17858a82472fSCornelia Huck 	old_parent = dev->parent;
17868a82472fSCornelia Huck 	dev->parent = new_parent;
17878a82472fSCornelia Huck 	if (old_parent)
1788f791b8c8SGreg Kroah-Hartman 		klist_remove(&dev->p->knode_parent);
17890d358f22SYinghai Lu 	if (new_parent) {
1790f791b8c8SGreg Kroah-Hartman 		klist_add_tail(&dev->p->knode_parent,
1791f791b8c8SGreg Kroah-Hartman 			       &new_parent->p->klist_children);
17920d358f22SYinghai Lu 		set_dev_node(dev, dev_to_node(new_parent));
17930d358f22SYinghai Lu 	}
17940d358f22SYinghai Lu 
1795bdd4034dSRabin Vincent 	if (dev->class) {
17968a82472fSCornelia Huck 		error = device_move_class_links(dev, old_parent, new_parent);
17978a82472fSCornelia Huck 		if (error) {
17988a82472fSCornelia Huck 			/* We ignore errors on cleanup since we're hosed anyway... */
17998a82472fSCornelia Huck 			device_move_class_links(dev, new_parent, old_parent);
18008a82472fSCornelia Huck 			if (!kobject_move(&dev->kobj, &old_parent->kobj)) {
1801c744aeaeSCornelia Huck 				if (new_parent)
1802f791b8c8SGreg Kroah-Hartman 					klist_remove(&dev->p->knode_parent);
18030d358f22SYinghai Lu 				dev->parent = old_parent;
18040d358f22SYinghai Lu 				if (old_parent) {
1805f791b8c8SGreg Kroah-Hartman 					klist_add_tail(&dev->p->knode_parent,
1806f791b8c8SGreg Kroah-Hartman 						       &old_parent->p->klist_children);
18070d358f22SYinghai Lu 					set_dev_node(dev, dev_to_node(old_parent));
18080d358f22SYinghai Lu 				}
18098a82472fSCornelia Huck 			}
181063b6971aSCornelia Huck 			cleanup_glue_dir(dev, new_parent_kobj);
18118a82472fSCornelia Huck 			put_device(new_parent);
18128a82472fSCornelia Huck 			goto out;
18138a82472fSCornelia Huck 		}
1814bdd4034dSRabin Vincent 	}
1815ffa6a705SCornelia Huck 	switch (dpm_order) {
1816ffa6a705SCornelia Huck 	case DPM_ORDER_NONE:
1817ffa6a705SCornelia Huck 		break;
1818ffa6a705SCornelia Huck 	case DPM_ORDER_DEV_AFTER_PARENT:
1819ffa6a705SCornelia Huck 		device_pm_move_after(dev, new_parent);
1820ffa6a705SCornelia Huck 		break;
1821ffa6a705SCornelia Huck 	case DPM_ORDER_PARENT_BEFORE_DEV:
1822ffa6a705SCornelia Huck 		device_pm_move_before(new_parent, dev);
1823ffa6a705SCornelia Huck 		break;
1824ffa6a705SCornelia Huck 	case DPM_ORDER_DEV_LAST:
1825ffa6a705SCornelia Huck 		device_pm_move_last(dev);
1826ffa6a705SCornelia Huck 		break;
1827ffa6a705SCornelia Huck 	}
1828bdd4034dSRabin Vincent 
18298a82472fSCornelia Huck 	put_device(old_parent);
18308a82472fSCornelia Huck out:
1831ffa6a705SCornelia Huck 	device_pm_unlock();
18328a82472fSCornelia Huck 	put_device(dev);
18338a82472fSCornelia Huck 	return error;
18348a82472fSCornelia Huck }
18358a82472fSCornelia Huck EXPORT_SYMBOL_GPL(device_move);
183637b0c020SGreg Kroah-Hartman 
183737b0c020SGreg Kroah-Hartman /**
183837b0c020SGreg Kroah-Hartman  * device_shutdown - call ->shutdown() on each device to shutdown.
183937b0c020SGreg Kroah-Hartman  */
184037b0c020SGreg Kroah-Hartman void device_shutdown(void)
184137b0c020SGreg Kroah-Hartman {
18426245838fSHugh Daschbach 	struct device *dev;
184337b0c020SGreg Kroah-Hartman 
18446245838fSHugh Daschbach 	spin_lock(&devices_kset->list_lock);
18456245838fSHugh Daschbach 	/*
18466245838fSHugh Daschbach 	 * Walk the devices list backward, shutting down each in turn.
18476245838fSHugh Daschbach 	 * Beware that device unplug events may also start pulling
18486245838fSHugh Daschbach 	 * devices offline, even as the system is shutting down.
18496245838fSHugh Daschbach 	 */
18506245838fSHugh Daschbach 	while (!list_empty(&devices_kset->list)) {
18516245838fSHugh Daschbach 		dev = list_entry(devices_kset->list.prev, struct device,
18526245838fSHugh Daschbach 				kobj.entry);
1853d1c6c030SMing Lei 
1854d1c6c030SMing Lei 		/*
1855d1c6c030SMing Lei 		 * hold reference count of device's parent to
1856d1c6c030SMing Lei 		 * prevent it from being freed because parent's
1857d1c6c030SMing Lei 		 * lock is to be held
1858d1c6c030SMing Lei 		 */
1859d1c6c030SMing Lei 		get_device(dev->parent);
18606245838fSHugh Daschbach 		get_device(dev);
18616245838fSHugh Daschbach 		/*
18626245838fSHugh Daschbach 		 * Make sure the device is off the kset list, in the
18636245838fSHugh Daschbach 		 * event that dev->*->shutdown() doesn't remove it.
18646245838fSHugh Daschbach 		 */
18656245838fSHugh Daschbach 		list_del_init(&dev->kobj.entry);
18666245838fSHugh Daschbach 		spin_unlock(&devices_kset->list_lock);
1867fe6b91f4SAlan Stern 
1868d1c6c030SMing Lei 		/* hold lock to avoid race with probe/release */
1869d1c6c030SMing Lei 		if (dev->parent)
1870d1c6c030SMing Lei 			device_lock(dev->parent);
1871d1c6c030SMing Lei 		device_lock(dev);
1872d1c6c030SMing Lei 
1873fe6b91f4SAlan Stern 		/* Don't allow any more runtime suspends */
1874fe6b91f4SAlan Stern 		pm_runtime_get_noresume(dev);
1875fe6b91f4SAlan Stern 		pm_runtime_barrier(dev);
18766245838fSHugh Daschbach 
187737b0c020SGreg Kroah-Hartman 		if (dev->bus && dev->bus->shutdown) {
18780246c4faSShuoX Liu 			if (initcall_debug)
18790246c4faSShuoX Liu 				dev_info(dev, "shutdown\n");
188037b0c020SGreg Kroah-Hartman 			dev->bus->shutdown(dev);
188137b0c020SGreg Kroah-Hartman 		} else if (dev->driver && dev->driver->shutdown) {
18820246c4faSShuoX Liu 			if (initcall_debug)
18830246c4faSShuoX Liu 				dev_info(dev, "shutdown\n");
188437b0c020SGreg Kroah-Hartman 			dev->driver->shutdown(dev);
188537b0c020SGreg Kroah-Hartman 		}
1886d1c6c030SMing Lei 
1887d1c6c030SMing Lei 		device_unlock(dev);
1888d1c6c030SMing Lei 		if (dev->parent)
1889d1c6c030SMing Lei 			device_unlock(dev->parent);
1890d1c6c030SMing Lei 
18916245838fSHugh Daschbach 		put_device(dev);
1892d1c6c030SMing Lei 		put_device(dev->parent);
18936245838fSHugh Daschbach 
18946245838fSHugh Daschbach 		spin_lock(&devices_kset->list_lock);
189537b0c020SGreg Kroah-Hartman 	}
18966245838fSHugh Daschbach 	spin_unlock(&devices_kset->list_lock);
1897401097eaSShaohua Li 	async_synchronize_full();
189837b0c020SGreg Kroah-Hartman }
189999bcf217SJoe Perches 
190099bcf217SJoe Perches /*
190199bcf217SJoe Perches  * Device logging functions
190299bcf217SJoe Perches  */
190399bcf217SJoe Perches 
190499bcf217SJoe Perches #ifdef CONFIG_PRINTK
1905666f355fSJoe Perches static int
1906666f355fSJoe Perches create_syslog_header(const struct device *dev, char *hdr, size_t hdrlen)
190799bcf217SJoe Perches {
1908c4e00daaSKay Sievers 	const char *subsys;
1909798efc60SJoe Perches 	size_t pos = 0;
191099bcf217SJoe Perches 
1911c4e00daaSKay Sievers 	if (dev->class)
1912c4e00daaSKay Sievers 		subsys = dev->class->name;
1913c4e00daaSKay Sievers 	else if (dev->bus)
1914c4e00daaSKay Sievers 		subsys = dev->bus->name;
1915c4e00daaSKay Sievers 	else
1916798efc60SJoe Perches 		return 0;
1917c4e00daaSKay Sievers 
1918798efc60SJoe Perches 	pos += snprintf(hdr + pos, hdrlen - pos, "SUBSYSTEM=%s", subsys);
1919c4e00daaSKay Sievers 
1920c4e00daaSKay Sievers 	/*
1921c4e00daaSKay Sievers 	 * Add device identifier DEVICE=:
1922c4e00daaSKay Sievers 	 *   b12:8         block dev_t
1923c4e00daaSKay Sievers 	 *   c127:3        char dev_t
1924c4e00daaSKay Sievers 	 *   n8            netdev ifindex
1925c4e00daaSKay Sievers 	 *   +sound:card0  subsystem:devname
1926c4e00daaSKay Sievers 	 */
1927c4e00daaSKay Sievers 	if (MAJOR(dev->devt)) {
1928c4e00daaSKay Sievers 		char c;
1929c4e00daaSKay Sievers 
1930c4e00daaSKay Sievers 		if (strcmp(subsys, "block") == 0)
1931c4e00daaSKay Sievers 			c = 'b';
1932c4e00daaSKay Sievers 		else
1933c4e00daaSKay Sievers 			c = 'c';
1934798efc60SJoe Perches 		pos++;
1935798efc60SJoe Perches 		pos += snprintf(hdr + pos, hdrlen - pos,
1936c4e00daaSKay Sievers 				"DEVICE=%c%u:%u",
1937c4e00daaSKay Sievers 				c, MAJOR(dev->devt), MINOR(dev->devt));
1938c4e00daaSKay Sievers 	} else if (strcmp(subsys, "net") == 0) {
1939c4e00daaSKay Sievers 		struct net_device *net = to_net_dev(dev);
1940c4e00daaSKay Sievers 
1941798efc60SJoe Perches 		pos++;
1942798efc60SJoe Perches 		pos += snprintf(hdr + pos, hdrlen - pos,
1943c4e00daaSKay Sievers 				"DEVICE=n%u", net->ifindex);
1944c4e00daaSKay Sievers 	} else {
1945798efc60SJoe Perches 		pos++;
1946798efc60SJoe Perches 		pos += snprintf(hdr + pos, hdrlen - pos,
1947c4e00daaSKay Sievers 				"DEVICE=+%s:%s", subsys, dev_name(dev));
1948c4e00daaSKay Sievers 	}
1949af7f2158SJim Cromie 
1950798efc60SJoe Perches 	return pos;
195199bcf217SJoe Perches }
1952798efc60SJoe Perches EXPORT_SYMBOL(create_syslog_header);
1953798efc60SJoe Perches 
195405e4e5b8SJoe Perches int dev_vprintk_emit(int level, const struct device *dev,
195505e4e5b8SJoe Perches 		     const char *fmt, va_list args)
195605e4e5b8SJoe Perches {
195705e4e5b8SJoe Perches 	char hdr[128];
195805e4e5b8SJoe Perches 	size_t hdrlen;
195905e4e5b8SJoe Perches 
196005e4e5b8SJoe Perches 	hdrlen = create_syslog_header(dev, hdr, sizeof(hdr));
196105e4e5b8SJoe Perches 
196205e4e5b8SJoe Perches 	return vprintk_emit(0, level, hdrlen ? hdr : NULL, hdrlen, fmt, args);
196305e4e5b8SJoe Perches }
196405e4e5b8SJoe Perches EXPORT_SYMBOL(dev_vprintk_emit);
196505e4e5b8SJoe Perches 
196605e4e5b8SJoe Perches int dev_printk_emit(int level, const struct device *dev, const char *fmt, ...)
196705e4e5b8SJoe Perches {
196805e4e5b8SJoe Perches 	va_list args;
196905e4e5b8SJoe Perches 	int r;
197005e4e5b8SJoe Perches 
197105e4e5b8SJoe Perches 	va_start(args, fmt);
197205e4e5b8SJoe Perches 
197305e4e5b8SJoe Perches 	r = dev_vprintk_emit(level, dev, fmt, args);
197405e4e5b8SJoe Perches 
197505e4e5b8SJoe Perches 	va_end(args);
197605e4e5b8SJoe Perches 
197705e4e5b8SJoe Perches 	return r;
197805e4e5b8SJoe Perches }
197905e4e5b8SJoe Perches EXPORT_SYMBOL(dev_printk_emit);
198005e4e5b8SJoe Perches 
1981798efc60SJoe Perches static int __dev_printk(const char *level, const struct device *dev,
1982798efc60SJoe Perches 			struct va_format *vaf)
1983798efc60SJoe Perches {
1984798efc60SJoe Perches 	if (!dev)
1985798efc60SJoe Perches 		return printk("%s(NULL device *): %pV", level, vaf);
1986798efc60SJoe Perches 
1987666f355fSJoe Perches 	return dev_printk_emit(level[1] - '0', dev,
1988798efc60SJoe Perches 			       "%s %s: %pV",
1989798efc60SJoe Perches 			       dev_driver_string(dev), dev_name(dev), vaf);
1990798efc60SJoe Perches }
199199bcf217SJoe Perches 
199299bcf217SJoe Perches int dev_printk(const char *level, const struct device *dev,
199399bcf217SJoe Perches 	       const char *fmt, ...)
199499bcf217SJoe Perches {
199599bcf217SJoe Perches 	struct va_format vaf;
199699bcf217SJoe Perches 	va_list args;
199799bcf217SJoe Perches 	int r;
199899bcf217SJoe Perches 
199999bcf217SJoe Perches 	va_start(args, fmt);
200099bcf217SJoe Perches 
200199bcf217SJoe Perches 	vaf.fmt = fmt;
200299bcf217SJoe Perches 	vaf.va = &args;
200399bcf217SJoe Perches 
200499bcf217SJoe Perches 	r = __dev_printk(level, dev, &vaf);
2005798efc60SJoe Perches 
200699bcf217SJoe Perches 	va_end(args);
200799bcf217SJoe Perches 
200899bcf217SJoe Perches 	return r;
200999bcf217SJoe Perches }
201099bcf217SJoe Perches EXPORT_SYMBOL(dev_printk);
201199bcf217SJoe Perches 
201299bcf217SJoe Perches #define define_dev_printk_level(func, kern_level)		\
201399bcf217SJoe Perches int func(const struct device *dev, const char *fmt, ...)	\
201499bcf217SJoe Perches {								\
201599bcf217SJoe Perches 	struct va_format vaf;					\
201699bcf217SJoe Perches 	va_list args;						\
201799bcf217SJoe Perches 	int r;							\
201899bcf217SJoe Perches 								\
201999bcf217SJoe Perches 	va_start(args, fmt);					\
202099bcf217SJoe Perches 								\
202199bcf217SJoe Perches 	vaf.fmt = fmt;						\
202299bcf217SJoe Perches 	vaf.va = &args;						\
202399bcf217SJoe Perches 								\
202499bcf217SJoe Perches 	r = __dev_printk(kern_level, dev, &vaf);		\
2025798efc60SJoe Perches 								\
202699bcf217SJoe Perches 	va_end(args);						\
202799bcf217SJoe Perches 								\
202899bcf217SJoe Perches 	return r;						\
202999bcf217SJoe Perches }								\
203099bcf217SJoe Perches EXPORT_SYMBOL(func);
203199bcf217SJoe Perches 
203299bcf217SJoe Perches define_dev_printk_level(dev_emerg, KERN_EMERG);
203399bcf217SJoe Perches define_dev_printk_level(dev_alert, KERN_ALERT);
203499bcf217SJoe Perches define_dev_printk_level(dev_crit, KERN_CRIT);
203599bcf217SJoe Perches define_dev_printk_level(dev_err, KERN_ERR);
203699bcf217SJoe Perches define_dev_printk_level(dev_warn, KERN_WARNING);
203799bcf217SJoe Perches define_dev_printk_level(dev_notice, KERN_NOTICE);
203899bcf217SJoe Perches define_dev_printk_level(_dev_info, KERN_INFO);
203999bcf217SJoe Perches 
204099bcf217SJoe Perches #endif
2041