xref: /openbmc/linux/drivers/base/core.c (revision af8db150)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * drivers/base/core.c - core driver model code (device registration, etc)
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  * Copyright (c) 2002-3 Patrick Mochel
51da177e4SLinus Torvalds  * Copyright (c) 2002-3 Open Source Development Labs
664bb5d2cSGreg Kroah-Hartman  * Copyright (c) 2006 Greg Kroah-Hartman <gregkh@suse.de>
764bb5d2cSGreg Kroah-Hartman  * Copyright (c) 2006 Novell, Inc.
81da177e4SLinus Torvalds  *
91da177e4SLinus Torvalds  * This file is released under the GPLv2
101da177e4SLinus Torvalds  *
111da177e4SLinus Torvalds  */
121da177e4SLinus Torvalds 
131da177e4SLinus Torvalds #include <linux/device.h>
141da177e4SLinus Torvalds #include <linux/err.h>
151da177e4SLinus Torvalds #include <linux/init.h>
161da177e4SLinus Torvalds #include <linux/module.h>
171da177e4SLinus Torvalds #include <linux/slab.h>
181da177e4SLinus Torvalds #include <linux/string.h>
1923681e47SGreg Kroah-Hartman #include <linux/kdev_t.h>
20116af378SBenjamin Herrenschmidt #include <linux/notifier.h>
21da231fd5SKay Sievers #include <linux/genhd.h>
22815d2d50SAndrew Morton #include <linux/kallsyms.h>
23f75b1c60SDave Young #include <linux/mutex.h>
24401097eaSShaohua Li #include <linux/async.h>
25af8db150SPeter Chen #include <linux/pm_runtime.h>
261da177e4SLinus Torvalds 
271da177e4SLinus Torvalds #include "base.h"
281da177e4SLinus Torvalds #include "power/power.h"
291da177e4SLinus Torvalds 
30e52eec13SAndi Kleen #ifdef CONFIG_SYSFS_DEPRECATED
31e52eec13SAndi Kleen #ifdef CONFIG_SYSFS_DEPRECATED_V2
32e52eec13SAndi Kleen long sysfs_deprecated = 1;
33e52eec13SAndi Kleen #else
34e52eec13SAndi Kleen long sysfs_deprecated = 0;
35e52eec13SAndi Kleen #endif
36e52eec13SAndi Kleen static __init int sysfs_deprecated_setup(char *arg)
37e52eec13SAndi Kleen {
38e52eec13SAndi Kleen 	return strict_strtol(arg, 10, &sysfs_deprecated);
39e52eec13SAndi Kleen }
40e52eec13SAndi Kleen early_param("sysfs.deprecated", sysfs_deprecated_setup);
41e52eec13SAndi Kleen #endif
42e52eec13SAndi Kleen 
431da177e4SLinus Torvalds int (*platform_notify)(struct device *dev) = NULL;
441da177e4SLinus Torvalds int (*platform_notify_remove)(struct device *dev) = NULL;
45e105b8bfSDan Williams static struct kobject *dev_kobj;
46e105b8bfSDan Williams struct kobject *sysfs_dev_char_kobj;
47e105b8bfSDan Williams struct kobject *sysfs_dev_block_kobj;
481da177e4SLinus Torvalds 
494e886c29SGreg Kroah-Hartman #ifdef CONFIG_BLOCK
504e886c29SGreg Kroah-Hartman static inline int device_is_not_partition(struct device *dev)
514e886c29SGreg Kroah-Hartman {
524e886c29SGreg Kroah-Hartman 	return !(dev->type == &part_type);
534e886c29SGreg Kroah-Hartman }
544e886c29SGreg Kroah-Hartman #else
554e886c29SGreg Kroah-Hartman static inline int device_is_not_partition(struct device *dev)
564e886c29SGreg Kroah-Hartman {
574e886c29SGreg Kroah-Hartman 	return 1;
584e886c29SGreg Kroah-Hartman }
594e886c29SGreg Kroah-Hartman #endif
601da177e4SLinus Torvalds 
613e95637aSAlan Stern /**
623e95637aSAlan Stern  * dev_driver_string - Return a device's driver name, if at all possible
633e95637aSAlan Stern  * @dev: struct device to get the name of
643e95637aSAlan Stern  *
653e95637aSAlan Stern  * Will return the device's driver's name if it is bound to a device.  If
663e95637aSAlan Stern  * the device is not bound to a device, it will return the name of the bus
673e95637aSAlan Stern  * it is attached to.  If it is not attached to a bus either, an empty
683e95637aSAlan Stern  * string will be returned.
693e95637aSAlan Stern  */
70bf9ca69fSJean Delvare const char *dev_driver_string(const struct device *dev)
713e95637aSAlan Stern {
723589972eSAlan Stern 	struct device_driver *drv;
733589972eSAlan Stern 
743589972eSAlan Stern 	/* dev->driver can change to NULL underneath us because of unbinding,
753589972eSAlan Stern 	 * so be careful about accessing it.  dev->bus and dev->class should
763589972eSAlan Stern 	 * never change once they are set, so they don't need special care.
773589972eSAlan Stern 	 */
783589972eSAlan Stern 	drv = ACCESS_ONCE(dev->driver);
793589972eSAlan Stern 	return drv ? drv->name :
80a456b702SJean Delvare 			(dev->bus ? dev->bus->name :
81a456b702SJean Delvare 			(dev->class ? dev->class->name : ""));
823e95637aSAlan Stern }
83310a922dSMatthew Wilcox EXPORT_SYMBOL(dev_driver_string);
843e95637aSAlan Stern 
851da177e4SLinus Torvalds #define to_dev(obj) container_of(obj, struct device, kobj)
861da177e4SLinus Torvalds #define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr)
871da177e4SLinus Torvalds 
884a3ad20cSGreg Kroah-Hartman static ssize_t dev_attr_show(struct kobject *kobj, struct attribute *attr,
894a3ad20cSGreg Kroah-Hartman 			     char *buf)
901da177e4SLinus Torvalds {
911da177e4SLinus Torvalds 	struct device_attribute *dev_attr = to_dev_attr(attr);
921da177e4SLinus Torvalds 	struct device *dev = to_dev(kobj);
934a0c20bfSDmitry Torokhov 	ssize_t ret = -EIO;
941da177e4SLinus Torvalds 
951da177e4SLinus Torvalds 	if (dev_attr->show)
9654b6f35cSYani Ioannou 		ret = dev_attr->show(dev, dev_attr, buf);
97815d2d50SAndrew Morton 	if (ret >= (ssize_t)PAGE_SIZE) {
98815d2d50SAndrew Morton 		print_symbol("dev_attr_show: %s returned bad count\n",
99815d2d50SAndrew Morton 				(unsigned long)dev_attr->show);
100815d2d50SAndrew Morton 	}
1011da177e4SLinus Torvalds 	return ret;
1021da177e4SLinus Torvalds }
1031da177e4SLinus Torvalds 
1044a3ad20cSGreg Kroah-Hartman static ssize_t dev_attr_store(struct kobject *kobj, struct attribute *attr,
1051da177e4SLinus Torvalds 			      const char *buf, size_t count)
1061da177e4SLinus Torvalds {
1071da177e4SLinus Torvalds 	struct device_attribute *dev_attr = to_dev_attr(attr);
1081da177e4SLinus Torvalds 	struct device *dev = to_dev(kobj);
1094a0c20bfSDmitry Torokhov 	ssize_t ret = -EIO;
1101da177e4SLinus Torvalds 
1111da177e4SLinus Torvalds 	if (dev_attr->store)
11254b6f35cSYani Ioannou 		ret = dev_attr->store(dev, dev_attr, buf, count);
1131da177e4SLinus Torvalds 	return ret;
1141da177e4SLinus Torvalds }
1151da177e4SLinus Torvalds 
11652cf25d0SEmese Revfy static const struct sysfs_ops dev_sysfs_ops = {
1171da177e4SLinus Torvalds 	.show	= dev_attr_show,
1181da177e4SLinus Torvalds 	.store	= dev_attr_store,
1191da177e4SLinus Torvalds };
1201da177e4SLinus Torvalds 
1211da177e4SLinus Torvalds 
1221da177e4SLinus Torvalds /**
1231da177e4SLinus Torvalds  *	device_release - free device structure.
1241da177e4SLinus Torvalds  *	@kobj:	device's kobject.
1251da177e4SLinus Torvalds  *
1261da177e4SLinus Torvalds  *	This is called once the reference count for the object
1271da177e4SLinus Torvalds  *	reaches 0. We forward the call to the device's release
1281da177e4SLinus Torvalds  *	method, which should handle actually freeing the structure.
1291da177e4SLinus Torvalds  */
1301da177e4SLinus Torvalds static void device_release(struct kobject *kobj)
1311da177e4SLinus Torvalds {
1321da177e4SLinus Torvalds 	struct device *dev = to_dev(kobj);
133fb069a5dSGreg Kroah-Hartman 	struct device_private *p = dev->p;
1341da177e4SLinus Torvalds 
1351da177e4SLinus Torvalds 	if (dev->release)
1361da177e4SLinus Torvalds 		dev->release(dev);
137f9f852dfSKay Sievers 	else if (dev->type && dev->type->release)
138f9f852dfSKay Sievers 		dev->type->release(dev);
1392620efefSGreg Kroah-Hartman 	else if (dev->class && dev->class->dev_release)
1402620efefSGreg Kroah-Hartman 		dev->class->dev_release(dev);
141f810a5cfSArjan van de Ven 	else
142f810a5cfSArjan van de Ven 		WARN(1, KERN_ERR "Device '%s' does not have a release() "
1434a3ad20cSGreg Kroah-Hartman 			"function, it is broken and must be fixed.\n",
1441e0b2cf9SKay Sievers 			dev_name(dev));
145fb069a5dSGreg Kroah-Hartman 	kfree(p);
1461da177e4SLinus Torvalds }
1471da177e4SLinus Torvalds 
148bc451f20SEric W. Biederman static const void *device_namespace(struct kobject *kobj)
149bc451f20SEric W. Biederman {
150bc451f20SEric W. Biederman 	struct device *dev = to_dev(kobj);
151bc451f20SEric W. Biederman 	const void *ns = NULL;
152bc451f20SEric W. Biederman 
153bc451f20SEric W. Biederman 	if (dev->class && dev->class->ns_type)
154bc451f20SEric W. Biederman 		ns = dev->class->namespace(dev);
155bc451f20SEric W. Biederman 
156bc451f20SEric W. Biederman 	return ns;
157bc451f20SEric W. Biederman }
158bc451f20SEric W. Biederman 
1598f4afc41SGreg Kroah-Hartman static struct kobj_type device_ktype = {
1601da177e4SLinus Torvalds 	.release	= device_release,
1611da177e4SLinus Torvalds 	.sysfs_ops	= &dev_sysfs_ops,
162bc451f20SEric W. Biederman 	.namespace	= device_namespace,
1631da177e4SLinus Torvalds };
1641da177e4SLinus Torvalds 
1651da177e4SLinus Torvalds 
166312c004dSKay Sievers static int dev_uevent_filter(struct kset *kset, struct kobject *kobj)
1671da177e4SLinus Torvalds {
1681da177e4SLinus Torvalds 	struct kobj_type *ktype = get_ktype(kobj);
1691da177e4SLinus Torvalds 
1708f4afc41SGreg Kroah-Hartman 	if (ktype == &device_ktype) {
1711da177e4SLinus Torvalds 		struct device *dev = to_dev(kobj);
1721da177e4SLinus Torvalds 		if (dev->bus)
1731da177e4SLinus Torvalds 			return 1;
17423681e47SGreg Kroah-Hartman 		if (dev->class)
17523681e47SGreg Kroah-Hartman 			return 1;
1761da177e4SLinus Torvalds 	}
1771da177e4SLinus Torvalds 	return 0;
1781da177e4SLinus Torvalds }
1791da177e4SLinus Torvalds 
180312c004dSKay Sievers static const char *dev_uevent_name(struct kset *kset, struct kobject *kobj)
1811da177e4SLinus Torvalds {
1821da177e4SLinus Torvalds 	struct device *dev = to_dev(kobj);
1831da177e4SLinus Torvalds 
18423681e47SGreg Kroah-Hartman 	if (dev->bus)
1851da177e4SLinus Torvalds 		return dev->bus->name;
18623681e47SGreg Kroah-Hartman 	if (dev->class)
18723681e47SGreg Kroah-Hartman 		return dev->class->name;
18823681e47SGreg Kroah-Hartman 	return NULL;
1891da177e4SLinus Torvalds }
1901da177e4SLinus Torvalds 
1917eff2e7aSKay Sievers static int dev_uevent(struct kset *kset, struct kobject *kobj,
1927eff2e7aSKay Sievers 		      struct kobj_uevent_env *env)
1931da177e4SLinus Torvalds {
1941da177e4SLinus Torvalds 	struct device *dev = to_dev(kobj);
1951da177e4SLinus Torvalds 	int retval = 0;
1961da177e4SLinus Torvalds 
1976fcf53acSKay Sievers 	/* add device node properties if present */
19823681e47SGreg Kroah-Hartman 	if (MAJOR(dev->devt)) {
1996fcf53acSKay Sievers 		const char *tmp;
2006fcf53acSKay Sievers 		const char *name;
201e454cea2SKay Sievers 		mode_t mode = 0;
2026fcf53acSKay Sievers 
2037eff2e7aSKay Sievers 		add_uevent_var(env, "MAJOR=%u", MAJOR(dev->devt));
2047eff2e7aSKay Sievers 		add_uevent_var(env, "MINOR=%u", MINOR(dev->devt));
205e454cea2SKay Sievers 		name = device_get_devnode(dev, &mode, &tmp);
2066fcf53acSKay Sievers 		if (name) {
2076fcf53acSKay Sievers 			add_uevent_var(env, "DEVNAME=%s", name);
2086fcf53acSKay Sievers 			kfree(tmp);
209e454cea2SKay Sievers 			if (mode)
210e454cea2SKay Sievers 				add_uevent_var(env, "DEVMODE=%#o", mode & 0777);
2116fcf53acSKay Sievers 		}
21223681e47SGreg Kroah-Hartman 	}
21323681e47SGreg Kroah-Hartman 
214414264f9SKay Sievers 	if (dev->type && dev->type->name)
2157eff2e7aSKay Sievers 		add_uevent_var(env, "DEVTYPE=%s", dev->type->name);
216414264f9SKay Sievers 
217239378f1SKay Sievers 	if (dev->driver)
2187eff2e7aSKay Sievers 		add_uevent_var(env, "DRIVER=%s", dev->driver->name);
219239378f1SKay Sievers 
2201da177e4SLinus Torvalds 	/* have the bus specific function add its stuff */
2217eff2e7aSKay Sievers 	if (dev->bus && dev->bus->uevent) {
2227eff2e7aSKay Sievers 		retval = dev->bus->uevent(dev, env);
223f9f852dfSKay Sievers 		if (retval)
2247dc72b28SGreg Kroah-Hartman 			pr_debug("device: '%s': %s: bus uevent() returned %d\n",
2251e0b2cf9SKay Sievers 				 dev_name(dev), __func__, retval);
2261da177e4SLinus Torvalds 	}
2271da177e4SLinus Torvalds 
2282620efefSGreg Kroah-Hartman 	/* have the class specific function add its stuff */
2297eff2e7aSKay Sievers 	if (dev->class && dev->class->dev_uevent) {
2307eff2e7aSKay Sievers 		retval = dev->class->dev_uevent(dev, env);
231f9f852dfSKay Sievers 		if (retval)
2327dc72b28SGreg Kroah-Hartman 			pr_debug("device: '%s': %s: class uevent() "
2331e0b2cf9SKay Sievers 				 "returned %d\n", dev_name(dev),
2342b3a302aSHarvey Harrison 				 __func__, retval);
2352620efefSGreg Kroah-Hartman 	}
236f9f852dfSKay Sievers 
237eef35c2dSStefan Weil 	/* have the device type specific function add its stuff */
2387eff2e7aSKay Sievers 	if (dev->type && dev->type->uevent) {
2397eff2e7aSKay Sievers 		retval = dev->type->uevent(dev, env);
240f9f852dfSKay Sievers 		if (retval)
2417dc72b28SGreg Kroah-Hartman 			pr_debug("device: '%s': %s: dev_type uevent() "
2421e0b2cf9SKay Sievers 				 "returned %d\n", dev_name(dev),
2432b3a302aSHarvey Harrison 				 __func__, retval);
2442620efefSGreg Kroah-Hartman 	}
2452620efefSGreg Kroah-Hartman 
2461da177e4SLinus Torvalds 	return retval;
2471da177e4SLinus Torvalds }
2481da177e4SLinus Torvalds 
2499cd43611SEmese Revfy static const struct kset_uevent_ops device_uevent_ops = {
250312c004dSKay Sievers 	.filter =	dev_uevent_filter,
251312c004dSKay Sievers 	.name =		dev_uevent_name,
252312c004dSKay Sievers 	.uevent =	dev_uevent,
2531da177e4SLinus Torvalds };
2541da177e4SLinus Torvalds 
25516574dccSKay Sievers static ssize_t show_uevent(struct device *dev, struct device_attribute *attr,
25616574dccSKay Sievers 			   char *buf)
25716574dccSKay Sievers {
25816574dccSKay Sievers 	struct kobject *top_kobj;
25916574dccSKay Sievers 	struct kset *kset;
2607eff2e7aSKay Sievers 	struct kobj_uevent_env *env = NULL;
26116574dccSKay Sievers 	int i;
26216574dccSKay Sievers 	size_t count = 0;
26316574dccSKay Sievers 	int retval;
26416574dccSKay Sievers 
26516574dccSKay Sievers 	/* search the kset, the device belongs to */
26616574dccSKay Sievers 	top_kobj = &dev->kobj;
2675c5daf65SKay Sievers 	while (!top_kobj->kset && top_kobj->parent)
26816574dccSKay Sievers 		top_kobj = top_kobj->parent;
26916574dccSKay Sievers 	if (!top_kobj->kset)
27016574dccSKay Sievers 		goto out;
2715c5daf65SKay Sievers 
27216574dccSKay Sievers 	kset = top_kobj->kset;
27316574dccSKay Sievers 	if (!kset->uevent_ops || !kset->uevent_ops->uevent)
27416574dccSKay Sievers 		goto out;
27516574dccSKay Sievers 
27616574dccSKay Sievers 	/* respect filter */
27716574dccSKay Sievers 	if (kset->uevent_ops && kset->uevent_ops->filter)
27816574dccSKay Sievers 		if (!kset->uevent_ops->filter(kset, &dev->kobj))
27916574dccSKay Sievers 			goto out;
28016574dccSKay Sievers 
2817eff2e7aSKay Sievers 	env = kzalloc(sizeof(struct kobj_uevent_env), GFP_KERNEL);
2827eff2e7aSKay Sievers 	if (!env)
283c7308c81SGreg Kroah-Hartman 		return -ENOMEM;
284c7308c81SGreg Kroah-Hartman 
28516574dccSKay Sievers 	/* let the kset specific function add its keys */
2867eff2e7aSKay Sievers 	retval = kset->uevent_ops->uevent(kset, &dev->kobj, env);
28716574dccSKay Sievers 	if (retval)
28816574dccSKay Sievers 		goto out;
28916574dccSKay Sievers 
29016574dccSKay Sievers 	/* copy keys to file */
2917eff2e7aSKay Sievers 	for (i = 0; i < env->envp_idx; i++)
2927eff2e7aSKay Sievers 		count += sprintf(&buf[count], "%s\n", env->envp[i]);
29316574dccSKay Sievers out:
2947eff2e7aSKay Sievers 	kfree(env);
29516574dccSKay Sievers 	return count;
29616574dccSKay Sievers }
29716574dccSKay Sievers 
298a7fd6706SKay Sievers static ssize_t store_uevent(struct device *dev, struct device_attribute *attr,
299a7fd6706SKay Sievers 			    const char *buf, size_t count)
300a7fd6706SKay Sievers {
30160a96a59SKay Sievers 	enum kobject_action action;
30260a96a59SKay Sievers 
3033f5468c9SKay Sievers 	if (kobject_action_type(buf, count, &action) == 0)
30460a96a59SKay Sievers 		kobject_uevent(&dev->kobj, action);
3053f5468c9SKay Sievers 	else
3063f5468c9SKay Sievers 		dev_err(dev, "uevent: unknown action-string\n");
307a7fd6706SKay Sievers 	return count;
308a7fd6706SKay Sievers }
309a7fd6706SKay Sievers 
310ad6a1e1cSTejun Heo static struct device_attribute uevent_attr =
311ad6a1e1cSTejun Heo 	__ATTR(uevent, S_IRUGO | S_IWUSR, show_uevent, store_uevent);
312ad6a1e1cSTejun Heo 
313621a1672SDmitry Torokhov static int device_add_attributes(struct device *dev,
314621a1672SDmitry Torokhov 				 struct device_attribute *attrs)
315de0ff00dSGreg Kroah-Hartman {
316de0ff00dSGreg Kroah-Hartman 	int error = 0;
317621a1672SDmitry Torokhov 	int i;
318de0ff00dSGreg Kroah-Hartman 
319621a1672SDmitry Torokhov 	if (attrs) {
320621a1672SDmitry Torokhov 		for (i = 0; attr_name(attrs[i]); i++) {
321621a1672SDmitry Torokhov 			error = device_create_file(dev, &attrs[i]);
322621a1672SDmitry Torokhov 			if (error)
323621a1672SDmitry Torokhov 				break;
324621a1672SDmitry Torokhov 		}
325621a1672SDmitry Torokhov 		if (error)
326de0ff00dSGreg Kroah-Hartman 			while (--i >= 0)
327621a1672SDmitry Torokhov 				device_remove_file(dev, &attrs[i]);
328de0ff00dSGreg Kroah-Hartman 	}
329de0ff00dSGreg Kroah-Hartman 	return error;
330de0ff00dSGreg Kroah-Hartman }
331de0ff00dSGreg Kroah-Hartman 
332621a1672SDmitry Torokhov static void device_remove_attributes(struct device *dev,
333621a1672SDmitry Torokhov 				     struct device_attribute *attrs)
334de0ff00dSGreg Kroah-Hartman {
335de0ff00dSGreg Kroah-Hartman 	int i;
336621a1672SDmitry Torokhov 
337621a1672SDmitry Torokhov 	if (attrs)
338621a1672SDmitry Torokhov 		for (i = 0; attr_name(attrs[i]); i++)
339621a1672SDmitry Torokhov 			device_remove_file(dev, &attrs[i]);
340621a1672SDmitry Torokhov }
341621a1672SDmitry Torokhov 
342c97415a7SStefan Achatz static int device_add_bin_attributes(struct device *dev,
343c97415a7SStefan Achatz 				     struct bin_attribute *attrs)
344c97415a7SStefan Achatz {
345c97415a7SStefan Achatz 	int error = 0;
346c97415a7SStefan Achatz 	int i;
347c97415a7SStefan Achatz 
348c97415a7SStefan Achatz 	if (attrs) {
349c97415a7SStefan Achatz 		for (i = 0; attr_name(attrs[i]); i++) {
350c97415a7SStefan Achatz 			error = device_create_bin_file(dev, &attrs[i]);
351c97415a7SStefan Achatz 			if (error)
352c97415a7SStefan Achatz 				break;
353c97415a7SStefan Achatz 		}
354c97415a7SStefan Achatz 		if (error)
355c97415a7SStefan Achatz 			while (--i >= 0)
356c97415a7SStefan Achatz 				device_remove_bin_file(dev, &attrs[i]);
357c97415a7SStefan Achatz 	}
358c97415a7SStefan Achatz 	return error;
359c97415a7SStefan Achatz }
360c97415a7SStefan Achatz 
361c97415a7SStefan Achatz static void device_remove_bin_attributes(struct device *dev,
362c97415a7SStefan Achatz 					 struct bin_attribute *attrs)
363c97415a7SStefan Achatz {
364c97415a7SStefan Achatz 	int i;
365c97415a7SStefan Achatz 
366c97415a7SStefan Achatz 	if (attrs)
367c97415a7SStefan Achatz 		for (i = 0; attr_name(attrs[i]); i++)
368c97415a7SStefan Achatz 			device_remove_bin_file(dev, &attrs[i]);
369c97415a7SStefan Achatz }
370c97415a7SStefan Achatz 
371621a1672SDmitry Torokhov static int device_add_groups(struct device *dev,
372a4dbd674SDavid Brownell 			     const struct attribute_group **groups)
373621a1672SDmitry Torokhov {
374621a1672SDmitry Torokhov 	int error = 0;
375621a1672SDmitry Torokhov 	int i;
376621a1672SDmitry Torokhov 
377621a1672SDmitry Torokhov 	if (groups) {
378621a1672SDmitry Torokhov 		for (i = 0; groups[i]; i++) {
379621a1672SDmitry Torokhov 			error = sysfs_create_group(&dev->kobj, groups[i]);
380621a1672SDmitry Torokhov 			if (error) {
381621a1672SDmitry Torokhov 				while (--i >= 0)
3824a3ad20cSGreg Kroah-Hartman 					sysfs_remove_group(&dev->kobj,
3834a3ad20cSGreg Kroah-Hartman 							   groups[i]);
384621a1672SDmitry Torokhov 				break;
385de0ff00dSGreg Kroah-Hartman 			}
386de0ff00dSGreg Kroah-Hartman 		}
387de0ff00dSGreg Kroah-Hartman 	}
388621a1672SDmitry Torokhov 	return error;
389621a1672SDmitry Torokhov }
390621a1672SDmitry Torokhov 
391621a1672SDmitry Torokhov static void device_remove_groups(struct device *dev,
392a4dbd674SDavid Brownell 				 const struct attribute_group **groups)
393621a1672SDmitry Torokhov {
394621a1672SDmitry Torokhov 	int i;
395621a1672SDmitry Torokhov 
396621a1672SDmitry Torokhov 	if (groups)
397621a1672SDmitry Torokhov 		for (i = 0; groups[i]; i++)
398621a1672SDmitry Torokhov 			sysfs_remove_group(&dev->kobj, groups[i]);
399621a1672SDmitry Torokhov }
400de0ff00dSGreg Kroah-Hartman 
4012620efefSGreg Kroah-Hartman static int device_add_attrs(struct device *dev)
4022620efefSGreg Kroah-Hartman {
4032620efefSGreg Kroah-Hartman 	struct class *class = dev->class;
404aed65af1SStephen Hemminger 	const struct device_type *type = dev->type;
405621a1672SDmitry Torokhov 	int error;
4062620efefSGreg Kroah-Hartman 
407621a1672SDmitry Torokhov 	if (class) {
408621a1672SDmitry Torokhov 		error = device_add_attributes(dev, class->dev_attrs);
4092620efefSGreg Kroah-Hartman 		if (error)
410621a1672SDmitry Torokhov 			return error;
411c97415a7SStefan Achatz 		error = device_add_bin_attributes(dev, class->dev_bin_attrs);
412c97415a7SStefan Achatz 		if (error)
413c97415a7SStefan Achatz 			goto err_remove_class_attrs;
414f9f852dfSKay Sievers 	}
415f9f852dfSKay Sievers 
416621a1672SDmitry Torokhov 	if (type) {
417621a1672SDmitry Torokhov 		error = device_add_groups(dev, type->groups);
418f9f852dfSKay Sievers 		if (error)
419c97415a7SStefan Achatz 			goto err_remove_class_bin_attrs;
420f9f852dfSKay Sievers 	}
421621a1672SDmitry Torokhov 
422621a1672SDmitry Torokhov 	error = device_add_groups(dev, dev->groups);
423f9f852dfSKay Sievers 	if (error)
424621a1672SDmitry Torokhov 		goto err_remove_type_groups;
425621a1672SDmitry Torokhov 
426621a1672SDmitry Torokhov 	return 0;
427621a1672SDmitry Torokhov 
428621a1672SDmitry Torokhov  err_remove_type_groups:
429621a1672SDmitry Torokhov 	if (type)
430621a1672SDmitry Torokhov 		device_remove_groups(dev, type->groups);
431c97415a7SStefan Achatz  err_remove_class_bin_attrs:
432c97415a7SStefan Achatz 	if (class)
433c97415a7SStefan Achatz 		device_remove_bin_attributes(dev, class->dev_bin_attrs);
434621a1672SDmitry Torokhov  err_remove_class_attrs:
435621a1672SDmitry Torokhov 	if (class)
436621a1672SDmitry Torokhov 		device_remove_attributes(dev, class->dev_attrs);
437f9f852dfSKay Sievers 
4382620efefSGreg Kroah-Hartman 	return error;
4392620efefSGreg Kroah-Hartman }
4402620efefSGreg Kroah-Hartman 
4412620efefSGreg Kroah-Hartman static void device_remove_attrs(struct device *dev)
4422620efefSGreg Kroah-Hartman {
4432620efefSGreg Kroah-Hartman 	struct class *class = dev->class;
444aed65af1SStephen Hemminger 	const struct device_type *type = dev->type;
4452620efefSGreg Kroah-Hartman 
446621a1672SDmitry Torokhov 	device_remove_groups(dev, dev->groups);
447f9f852dfSKay Sievers 
448621a1672SDmitry Torokhov 	if (type)
449621a1672SDmitry Torokhov 		device_remove_groups(dev, type->groups);
450621a1672SDmitry Torokhov 
451c97415a7SStefan Achatz 	if (class) {
452621a1672SDmitry Torokhov 		device_remove_attributes(dev, class->dev_attrs);
453c97415a7SStefan Achatz 		device_remove_bin_attributes(dev, class->dev_bin_attrs);
454c97415a7SStefan Achatz 	}
4552620efefSGreg Kroah-Hartman }
4562620efefSGreg Kroah-Hartman 
4572620efefSGreg Kroah-Hartman 
45823681e47SGreg Kroah-Hartman static ssize_t show_dev(struct device *dev, struct device_attribute *attr,
45923681e47SGreg Kroah-Hartman 			char *buf)
46023681e47SGreg Kroah-Hartman {
46123681e47SGreg Kroah-Hartman 	return print_dev_t(buf, dev->devt);
46223681e47SGreg Kroah-Hartman }
46323681e47SGreg Kroah-Hartman 
464ad6a1e1cSTejun Heo static struct device_attribute devt_attr =
465ad6a1e1cSTejun Heo 	__ATTR(dev, S_IRUGO, show_dev, NULL);
466ad6a1e1cSTejun Heo 
467881c6cfdSGreg Kroah-Hartman /* kset to create /sys/devices/  */
468881c6cfdSGreg Kroah-Hartman struct kset *devices_kset;
4691da177e4SLinus Torvalds 
4701da177e4SLinus Torvalds /**
4711da177e4SLinus Torvalds  * device_create_file - create sysfs attribute file for device.
4721da177e4SLinus Torvalds  * @dev: device.
4731da177e4SLinus Torvalds  * @attr: device attribute descriptor.
4741da177e4SLinus Torvalds  */
47526579ab7SPhil Carmody int device_create_file(struct device *dev,
47626579ab7SPhil Carmody 		       const struct device_attribute *attr)
4771da177e4SLinus Torvalds {
4781da177e4SLinus Torvalds 	int error = 0;
4790c98b19fSCornelia Huck 	if (dev)
4801da177e4SLinus Torvalds 		error = sysfs_create_file(&dev->kobj, &attr->attr);
4811da177e4SLinus Torvalds 	return error;
4821da177e4SLinus Torvalds }
4831da177e4SLinus Torvalds 
4841da177e4SLinus Torvalds /**
4851da177e4SLinus Torvalds  * device_remove_file - remove sysfs attribute file.
4861da177e4SLinus Torvalds  * @dev: device.
4871da177e4SLinus Torvalds  * @attr: device attribute descriptor.
4881da177e4SLinus Torvalds  */
48926579ab7SPhil Carmody void device_remove_file(struct device *dev,
49026579ab7SPhil Carmody 			const struct device_attribute *attr)
4911da177e4SLinus Torvalds {
4920c98b19fSCornelia Huck 	if (dev)
4931da177e4SLinus Torvalds 		sysfs_remove_file(&dev->kobj, &attr->attr);
4941da177e4SLinus Torvalds }
4951da177e4SLinus Torvalds 
4962589f188SGreg Kroah-Hartman /**
4972589f188SGreg Kroah-Hartman  * device_create_bin_file - create sysfs binary attribute file for device.
4982589f188SGreg Kroah-Hartman  * @dev: device.
4992589f188SGreg Kroah-Hartman  * @attr: device binary attribute descriptor.
5002589f188SGreg Kroah-Hartman  */
50166ecb92bSPhil Carmody int device_create_bin_file(struct device *dev,
50266ecb92bSPhil Carmody 			   const struct bin_attribute *attr)
5032589f188SGreg Kroah-Hartman {
5042589f188SGreg Kroah-Hartman 	int error = -EINVAL;
5052589f188SGreg Kroah-Hartman 	if (dev)
5062589f188SGreg Kroah-Hartman 		error = sysfs_create_bin_file(&dev->kobj, attr);
5072589f188SGreg Kroah-Hartman 	return error;
5082589f188SGreg Kroah-Hartman }
5092589f188SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(device_create_bin_file);
5102589f188SGreg Kroah-Hartman 
5112589f188SGreg Kroah-Hartman /**
5122589f188SGreg Kroah-Hartman  * device_remove_bin_file - remove sysfs binary attribute file
5132589f188SGreg Kroah-Hartman  * @dev: device.
5142589f188SGreg Kroah-Hartman  * @attr: device binary attribute descriptor.
5152589f188SGreg Kroah-Hartman  */
51666ecb92bSPhil Carmody void device_remove_bin_file(struct device *dev,
51766ecb92bSPhil Carmody 			    const struct bin_attribute *attr)
5182589f188SGreg Kroah-Hartman {
5192589f188SGreg Kroah-Hartman 	if (dev)
5202589f188SGreg Kroah-Hartman 		sysfs_remove_bin_file(&dev->kobj, attr);
5212589f188SGreg Kroah-Hartman }
5222589f188SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(device_remove_bin_file);
5232589f188SGreg Kroah-Hartman 
524d9a9cdfbSAlan Stern /**
525523ded71SAlan Stern  * device_schedule_callback_owner - helper to schedule a callback for a device
526d9a9cdfbSAlan Stern  * @dev: device.
527d9a9cdfbSAlan Stern  * @func: callback function to invoke later.
528523ded71SAlan Stern  * @owner: module owning the callback routine
529d9a9cdfbSAlan Stern  *
530d9a9cdfbSAlan Stern  * Attribute methods must not unregister themselves or their parent device
531d9a9cdfbSAlan Stern  * (which would amount to the same thing).  Attempts to do so will deadlock,
532d9a9cdfbSAlan Stern  * since unregistration is mutually exclusive with driver callbacks.
533d9a9cdfbSAlan Stern  *
534d9a9cdfbSAlan Stern  * Instead methods can call this routine, which will attempt to allocate
535d9a9cdfbSAlan Stern  * and schedule a workqueue request to call back @func with @dev as its
536d9a9cdfbSAlan Stern  * argument in the workqueue's process context.  @dev will be pinned until
537d9a9cdfbSAlan Stern  * @func returns.
538d9a9cdfbSAlan Stern  *
539523ded71SAlan Stern  * This routine is usually called via the inline device_schedule_callback(),
540523ded71SAlan Stern  * which automatically sets @owner to THIS_MODULE.
541523ded71SAlan Stern  *
542d9a9cdfbSAlan Stern  * Returns 0 if the request was submitted, -ENOMEM if storage could not
543523ded71SAlan Stern  * be allocated, -ENODEV if a reference to @owner isn't available.
544d9a9cdfbSAlan Stern  *
545d9a9cdfbSAlan Stern  * NOTE: This routine won't work if CONFIG_SYSFS isn't set!  It uses an
546d9a9cdfbSAlan Stern  * underlying sysfs routine (since it is intended for use by attribute
547d9a9cdfbSAlan Stern  * methods), and if sysfs isn't available you'll get nothing but -ENOSYS.
548d9a9cdfbSAlan Stern  */
549523ded71SAlan Stern int device_schedule_callback_owner(struct device *dev,
550523ded71SAlan Stern 		void (*func)(struct device *), struct module *owner)
551d9a9cdfbSAlan Stern {
552d9a9cdfbSAlan Stern 	return sysfs_schedule_callback(&dev->kobj,
553523ded71SAlan Stern 			(void (*)(void *)) func, dev, owner);
554d9a9cdfbSAlan Stern }
555523ded71SAlan Stern EXPORT_SYMBOL_GPL(device_schedule_callback_owner);
556d9a9cdfbSAlan Stern 
55734bb61f9SJames Bottomley static void klist_children_get(struct klist_node *n)
55834bb61f9SJames Bottomley {
559f791b8c8SGreg Kroah-Hartman 	struct device_private *p = to_device_private_parent(n);
560f791b8c8SGreg Kroah-Hartman 	struct device *dev = p->device;
56134bb61f9SJames Bottomley 
56234bb61f9SJames Bottomley 	get_device(dev);
56334bb61f9SJames Bottomley }
56434bb61f9SJames Bottomley 
56534bb61f9SJames Bottomley static void klist_children_put(struct klist_node *n)
56634bb61f9SJames Bottomley {
567f791b8c8SGreg Kroah-Hartman 	struct device_private *p = to_device_private_parent(n);
568f791b8c8SGreg Kroah-Hartman 	struct device *dev = p->device;
56934bb61f9SJames Bottomley 
57034bb61f9SJames Bottomley 	put_device(dev);
57134bb61f9SJames Bottomley }
57234bb61f9SJames Bottomley 
5731da177e4SLinus Torvalds /**
5741da177e4SLinus Torvalds  * device_initialize - init device structure.
5751da177e4SLinus Torvalds  * @dev: device.
5761da177e4SLinus Torvalds  *
5775739411aSCornelia Huck  * This prepares the device for use by other layers by initializing
5785739411aSCornelia Huck  * its fields.
5791da177e4SLinus Torvalds  * It is the first half of device_register(), if called by
5805739411aSCornelia Huck  * that function, though it can also be called separately, so one
5815739411aSCornelia Huck  * may use @dev's fields. In particular, get_device()/put_device()
5825739411aSCornelia Huck  * may be used for reference counting of @dev after calling this
5835739411aSCornelia Huck  * function.
5845739411aSCornelia Huck  *
5855739411aSCornelia Huck  * NOTE: Use put_device() to give up your reference instead of freeing
5865739411aSCornelia Huck  * @dev directly once you have called this function.
5871da177e4SLinus Torvalds  */
5881da177e4SLinus Torvalds void device_initialize(struct device *dev)
5891da177e4SLinus Torvalds {
590881c6cfdSGreg Kroah-Hartman 	dev->kobj.kset = devices_kset;
591f9cb074bSGreg Kroah-Hartman 	kobject_init(&dev->kobj, &device_ktype);
5921da177e4SLinus Torvalds 	INIT_LIST_HEAD(&dev->dma_pools);
5933142788bSThomas Gleixner 	mutex_init(&dev->mutex);
5941704f47bSPeter Zijlstra 	lockdep_set_novalidate_class(&dev->mutex);
5959ac7849eSTejun Heo 	spin_lock_init(&dev->devres_lock);
5969ac7849eSTejun Heo 	INIT_LIST_HEAD(&dev->devres_head);
5973b98aeafSAlan Stern 	device_pm_init(dev);
59887348136SChristoph Hellwig 	set_dev_node(dev, -1);
5991da177e4SLinus Torvalds }
6001da177e4SLinus Torvalds 
601c744aeaeSCornelia Huck static struct kobject *virtual_device_parent(struct device *dev)
602f0ee61a6SGreg Kroah-Hartman {
603f0ee61a6SGreg Kroah-Hartman 	static struct kobject *virtual_dir = NULL;
604f0ee61a6SGreg Kroah-Hartman 
605f0ee61a6SGreg Kroah-Hartman 	if (!virtual_dir)
6064ff6abffSGreg Kroah-Hartman 		virtual_dir = kobject_create_and_add("virtual",
607881c6cfdSGreg Kroah-Hartman 						     &devices_kset->kobj);
608f0ee61a6SGreg Kroah-Hartman 
60986406245SKay Sievers 	return virtual_dir;
610f0ee61a6SGreg Kroah-Hartman }
611f0ee61a6SGreg Kroah-Hartman 
612bc451f20SEric W. Biederman struct class_dir {
613bc451f20SEric W. Biederman 	struct kobject kobj;
614bc451f20SEric W. Biederman 	struct class *class;
615bc451f20SEric W. Biederman };
616bc451f20SEric W. Biederman 
617bc451f20SEric W. Biederman #define to_class_dir(obj) container_of(obj, struct class_dir, kobj)
618bc451f20SEric W. Biederman 
619bc451f20SEric W. Biederman static void class_dir_release(struct kobject *kobj)
620bc451f20SEric W. Biederman {
621bc451f20SEric W. Biederman 	struct class_dir *dir = to_class_dir(kobj);
622bc451f20SEric W. Biederman 	kfree(dir);
623bc451f20SEric W. Biederman }
624bc451f20SEric W. Biederman 
625bc451f20SEric W. Biederman static const
626bc451f20SEric W. Biederman struct kobj_ns_type_operations *class_dir_child_ns_type(struct kobject *kobj)
627bc451f20SEric W. Biederman {
628bc451f20SEric W. Biederman 	struct class_dir *dir = to_class_dir(kobj);
629bc451f20SEric W. Biederman 	return dir->class->ns_type;
630bc451f20SEric W. Biederman }
631bc451f20SEric W. Biederman 
632bc451f20SEric W. Biederman static struct kobj_type class_dir_ktype = {
633bc451f20SEric W. Biederman 	.release	= class_dir_release,
634bc451f20SEric W. Biederman 	.sysfs_ops	= &kobj_sysfs_ops,
635bc451f20SEric W. Biederman 	.child_ns_type	= class_dir_child_ns_type
636bc451f20SEric W. Biederman };
637bc451f20SEric W. Biederman 
638bc451f20SEric W. Biederman static struct kobject *
639bc451f20SEric W. Biederman class_dir_create_and_add(struct class *class, struct kobject *parent_kobj)
640bc451f20SEric W. Biederman {
641bc451f20SEric W. Biederman 	struct class_dir *dir;
642bc451f20SEric W. Biederman 	int retval;
643bc451f20SEric W. Biederman 
644bc451f20SEric W. Biederman 	dir = kzalloc(sizeof(*dir), GFP_KERNEL);
645bc451f20SEric W. Biederman 	if (!dir)
646bc451f20SEric W. Biederman 		return NULL;
647bc451f20SEric W. Biederman 
648bc451f20SEric W. Biederman 	dir->class = class;
649bc451f20SEric W. Biederman 	kobject_init(&dir->kobj, &class_dir_ktype);
650bc451f20SEric W. Biederman 
6516b6e39a6SKay Sievers 	dir->kobj.kset = &class->p->glue_dirs;
652bc451f20SEric W. Biederman 
653bc451f20SEric W. Biederman 	retval = kobject_add(&dir->kobj, parent_kobj, "%s", class->name);
654bc451f20SEric W. Biederman 	if (retval < 0) {
655bc451f20SEric W. Biederman 		kobject_put(&dir->kobj);
656bc451f20SEric W. Biederman 		return NULL;
657bc451f20SEric W. Biederman 	}
658bc451f20SEric W. Biederman 	return &dir->kobj;
659bc451f20SEric W. Biederman }
660bc451f20SEric W. Biederman 
661bc451f20SEric W. Biederman 
662c744aeaeSCornelia Huck static struct kobject *get_device_parent(struct device *dev,
663c744aeaeSCornelia Huck 					 struct device *parent)
66440fa5422SGreg Kroah-Hartman {
66586406245SKay Sievers 	if (dev->class) {
66677d3d7c1STejun Heo 		static DEFINE_MUTEX(gdp_mutex);
66786406245SKay Sievers 		struct kobject *kobj = NULL;
66886406245SKay Sievers 		struct kobject *parent_kobj;
66986406245SKay Sievers 		struct kobject *k;
67086406245SKay Sievers 
671ead454feSRandy Dunlap #ifdef CONFIG_BLOCK
67239aba963SKay Sievers 		/* block disks show up in /sys/block */
673e52eec13SAndi Kleen 		if (sysfs_deprecated && dev->class == &block_class) {
67439aba963SKay Sievers 			if (parent && parent->class == &block_class)
67539aba963SKay Sievers 				return &parent->kobj;
6766b6e39a6SKay Sievers 			return &block_class.p->subsys.kobj;
67739aba963SKay Sievers 		}
678ead454feSRandy Dunlap #endif
679e52eec13SAndi Kleen 
68086406245SKay Sievers 		/*
68186406245SKay Sievers 		 * If we have no parent, we live in "virtual".
6820f4dafc0SKay Sievers 		 * Class-devices with a non class-device as parent, live
6830f4dafc0SKay Sievers 		 * in a "glue" directory to prevent namespace collisions.
68486406245SKay Sievers 		 */
68586406245SKay Sievers 		if (parent == NULL)
68686406245SKay Sievers 			parent_kobj = virtual_device_parent(dev);
68724b1442dSEric W. Biederman 		else if (parent->class && !dev->class->ns_type)
68886406245SKay Sievers 			return &parent->kobj;
68986406245SKay Sievers 		else
69086406245SKay Sievers 			parent_kobj = &parent->kobj;
69186406245SKay Sievers 
69277d3d7c1STejun Heo 		mutex_lock(&gdp_mutex);
69377d3d7c1STejun Heo 
69486406245SKay Sievers 		/* find our class-directory at the parent and reference it */
6956b6e39a6SKay Sievers 		spin_lock(&dev->class->p->glue_dirs.list_lock);
6966b6e39a6SKay Sievers 		list_for_each_entry(k, &dev->class->p->glue_dirs.list, entry)
69786406245SKay Sievers 			if (k->parent == parent_kobj) {
69886406245SKay Sievers 				kobj = kobject_get(k);
69986406245SKay Sievers 				break;
70086406245SKay Sievers 			}
7016b6e39a6SKay Sievers 		spin_unlock(&dev->class->p->glue_dirs.list_lock);
70277d3d7c1STejun Heo 		if (kobj) {
70377d3d7c1STejun Heo 			mutex_unlock(&gdp_mutex);
70486406245SKay Sievers 			return kobj;
70577d3d7c1STejun Heo 		}
70686406245SKay Sievers 
70786406245SKay Sievers 		/* or create a new class-directory at the parent device */
708bc451f20SEric W. Biederman 		k = class_dir_create_and_add(dev->class, parent_kobj);
7090f4dafc0SKay Sievers 		/* do not emit an uevent for this simple "glue" directory */
71077d3d7c1STejun Heo 		mutex_unlock(&gdp_mutex);
71143968d2fSGreg Kroah-Hartman 		return k;
71286406245SKay Sievers 	}
71386406245SKay Sievers 
71486406245SKay Sievers 	if (parent)
715c744aeaeSCornelia Huck 		return &parent->kobj;
716c744aeaeSCornelia Huck 	return NULL;
717c744aeaeSCornelia Huck }
718da231fd5SKay Sievers 
71963b6971aSCornelia Huck static void cleanup_glue_dir(struct device *dev, struct kobject *glue_dir)
720da231fd5SKay Sievers {
7210f4dafc0SKay Sievers 	/* see if we live in a "glue" directory */
722c1fe539aSCornelia Huck 	if (!glue_dir || !dev->class ||
7236b6e39a6SKay Sievers 	    glue_dir->kset != &dev->class->p->glue_dirs)
724da231fd5SKay Sievers 		return;
725da231fd5SKay Sievers 
7260f4dafc0SKay Sievers 	kobject_put(glue_dir);
727da231fd5SKay Sievers }
72863b6971aSCornelia Huck 
72963b6971aSCornelia Huck static void cleanup_device_parent(struct device *dev)
73063b6971aSCornelia Huck {
73163b6971aSCornelia Huck 	cleanup_glue_dir(dev, dev->kobj.parent);
73263b6971aSCornelia Huck }
73386406245SKay Sievers 
73463b6971aSCornelia Huck static void setup_parent(struct device *dev, struct device *parent)
735c744aeaeSCornelia Huck {
736c744aeaeSCornelia Huck 	struct kobject *kobj;
737c744aeaeSCornelia Huck 	kobj = get_device_parent(dev, parent);
738c744aeaeSCornelia Huck 	if (kobj)
739c744aeaeSCornelia Huck 		dev->kobj.parent = kobj;
74040fa5422SGreg Kroah-Hartman }
74140fa5422SGreg Kroah-Hartman 
7422ee97cafSCornelia Huck static int device_add_class_symlinks(struct device *dev)
7432ee97cafSCornelia Huck {
7442ee97cafSCornelia Huck 	int error;
7452ee97cafSCornelia Huck 
7462ee97cafSCornelia Huck 	if (!dev->class)
7472ee97cafSCornelia Huck 		return 0;
748da231fd5SKay Sievers 
7491fbfee6cSGreg Kroah-Hartman 	error = sysfs_create_link(&dev->kobj,
7506b6e39a6SKay Sievers 				  &dev->class->p->subsys.kobj,
7512ee97cafSCornelia Huck 				  "subsystem");
7522ee97cafSCornelia Huck 	if (error)
7532ee97cafSCornelia Huck 		goto out;
754da231fd5SKay Sievers 
7554e886c29SGreg Kroah-Hartman 	if (dev->parent && device_is_not_partition(dev)) {
7564f01a757SDmitry Torokhov 		error = sysfs_create_link(&dev->kobj, &dev->parent->kobj,
7574f01a757SDmitry Torokhov 					  "device");
7584f01a757SDmitry Torokhov 		if (error)
75939aba963SKay Sievers 			goto out_subsys;
7602ee97cafSCornelia Huck 	}
76139aba963SKay Sievers 
762ead454feSRandy Dunlap #ifdef CONFIG_BLOCK
76339aba963SKay Sievers 	/* /sys/block has directories and does not need symlinks */
764e52eec13SAndi Kleen 	if (sysfs_deprecated && dev->class == &block_class)
76539aba963SKay Sievers 		return 0;
766ead454feSRandy Dunlap #endif
76739aba963SKay Sievers 
76839aba963SKay Sievers 	/* link in the class directory pointing to the device */
7696b6e39a6SKay Sievers 	error = sysfs_create_link(&dev->class->p->subsys.kobj,
77039aba963SKay Sievers 				  &dev->kobj, dev_name(dev));
77139aba963SKay Sievers 	if (error)
77239aba963SKay Sievers 		goto out_device;
77339aba963SKay Sievers 
7742ee97cafSCornelia Huck 	return 0;
7752ee97cafSCornelia Huck 
77639aba963SKay Sievers out_device:
77739aba963SKay Sievers 	sysfs_remove_link(&dev->kobj, "device");
778da231fd5SKay Sievers 
7792ee97cafSCornelia Huck out_subsys:
7802ee97cafSCornelia Huck 	sysfs_remove_link(&dev->kobj, "subsystem");
7812ee97cafSCornelia Huck out:
7822ee97cafSCornelia Huck 	return error;
7832ee97cafSCornelia Huck }
7842ee97cafSCornelia Huck 
7852ee97cafSCornelia Huck static void device_remove_class_symlinks(struct device *dev)
7862ee97cafSCornelia Huck {
7872ee97cafSCornelia Huck 	if (!dev->class)
7882ee97cafSCornelia Huck 		return;
789da231fd5SKay Sievers 
7904e886c29SGreg Kroah-Hartman 	if (dev->parent && device_is_not_partition(dev))
791da231fd5SKay Sievers 		sysfs_remove_link(&dev->kobj, "device");
7922ee97cafSCornelia Huck 	sysfs_remove_link(&dev->kobj, "subsystem");
793ead454feSRandy Dunlap #ifdef CONFIG_BLOCK
794e52eec13SAndi Kleen 	if (sysfs_deprecated && dev->class == &block_class)
79539aba963SKay Sievers 		return;
796ead454feSRandy Dunlap #endif
7976b6e39a6SKay Sievers 	sysfs_delete_link(&dev->class->p->subsys.kobj, &dev->kobj, dev_name(dev));
7982ee97cafSCornelia Huck }
7992ee97cafSCornelia Huck 
8001da177e4SLinus Torvalds /**
801413c239fSStephen Rothwell  * dev_set_name - set a device name
802413c239fSStephen Rothwell  * @dev: device
80346232366SRandy Dunlap  * @fmt: format string for the device's name
804413c239fSStephen Rothwell  */
805413c239fSStephen Rothwell int dev_set_name(struct device *dev, const char *fmt, ...)
806413c239fSStephen Rothwell {
807413c239fSStephen Rothwell 	va_list vargs;
8081fa5ae85SKay Sievers 	int err;
809413c239fSStephen Rothwell 
810413c239fSStephen Rothwell 	va_start(vargs, fmt);
8111fa5ae85SKay Sievers 	err = kobject_set_name_vargs(&dev->kobj, fmt, vargs);
812413c239fSStephen Rothwell 	va_end(vargs);
8131fa5ae85SKay Sievers 	return err;
814413c239fSStephen Rothwell }
815413c239fSStephen Rothwell EXPORT_SYMBOL_GPL(dev_set_name);
816413c239fSStephen Rothwell 
817413c239fSStephen Rothwell /**
818e105b8bfSDan Williams  * device_to_dev_kobj - select a /sys/dev/ directory for the device
819e105b8bfSDan Williams  * @dev: device
820e105b8bfSDan Williams  *
821e105b8bfSDan Williams  * By default we select char/ for new entries.  Setting class->dev_obj
822e105b8bfSDan Williams  * to NULL prevents an entry from being created.  class->dev_kobj must
823e105b8bfSDan Williams  * be set (or cleared) before any devices are registered to the class
824e105b8bfSDan Williams  * otherwise device_create_sys_dev_entry() and
825e105b8bfSDan Williams  * device_remove_sys_dev_entry() will disagree about the the presence
826e105b8bfSDan Williams  * of the link.
827e105b8bfSDan Williams  */
828e105b8bfSDan Williams static struct kobject *device_to_dev_kobj(struct device *dev)
829e105b8bfSDan Williams {
830e105b8bfSDan Williams 	struct kobject *kobj;
831e105b8bfSDan Williams 
832e105b8bfSDan Williams 	if (dev->class)
833e105b8bfSDan Williams 		kobj = dev->class->dev_kobj;
834e105b8bfSDan Williams 	else
835e105b8bfSDan Williams 		kobj = sysfs_dev_char_kobj;
836e105b8bfSDan Williams 
837e105b8bfSDan Williams 	return kobj;
838e105b8bfSDan Williams }
839e105b8bfSDan Williams 
840e105b8bfSDan Williams static int device_create_sys_dev_entry(struct device *dev)
841e105b8bfSDan Williams {
842e105b8bfSDan Williams 	struct kobject *kobj = device_to_dev_kobj(dev);
843e105b8bfSDan Williams 	int error = 0;
844e105b8bfSDan Williams 	char devt_str[15];
845e105b8bfSDan Williams 
846e105b8bfSDan Williams 	if (kobj) {
847e105b8bfSDan Williams 		format_dev_t(devt_str, dev->devt);
848e105b8bfSDan Williams 		error = sysfs_create_link(kobj, &dev->kobj, devt_str);
849e105b8bfSDan Williams 	}
850e105b8bfSDan Williams 
851e105b8bfSDan Williams 	return error;
852e105b8bfSDan Williams }
853e105b8bfSDan Williams 
854e105b8bfSDan Williams static void device_remove_sys_dev_entry(struct device *dev)
855e105b8bfSDan Williams {
856e105b8bfSDan Williams 	struct kobject *kobj = device_to_dev_kobj(dev);
857e105b8bfSDan Williams 	char devt_str[15];
858e105b8bfSDan Williams 
859e105b8bfSDan Williams 	if (kobj) {
860e105b8bfSDan Williams 		format_dev_t(devt_str, dev->devt);
861e105b8bfSDan Williams 		sysfs_remove_link(kobj, devt_str);
862e105b8bfSDan Williams 	}
863e105b8bfSDan Williams }
864e105b8bfSDan Williams 
865b4028437SGreg Kroah-Hartman int device_private_init(struct device *dev)
866b4028437SGreg Kroah-Hartman {
867b4028437SGreg Kroah-Hartman 	dev->p = kzalloc(sizeof(*dev->p), GFP_KERNEL);
868b4028437SGreg Kroah-Hartman 	if (!dev->p)
869b4028437SGreg Kroah-Hartman 		return -ENOMEM;
870b4028437SGreg Kroah-Hartman 	dev->p->device = dev;
871b4028437SGreg Kroah-Hartman 	klist_init(&dev->p->klist_children, klist_children_get,
872b4028437SGreg Kroah-Hartman 		   klist_children_put);
873b4028437SGreg Kroah-Hartman 	return 0;
874b4028437SGreg Kroah-Hartman }
875b4028437SGreg Kroah-Hartman 
876e105b8bfSDan Williams /**
8771da177e4SLinus Torvalds  * device_add - add device to device hierarchy.
8781da177e4SLinus Torvalds  * @dev: device.
8791da177e4SLinus Torvalds  *
8801da177e4SLinus Torvalds  * This is part 2 of device_register(), though may be called
8811da177e4SLinus Torvalds  * separately _iff_ device_initialize() has been called separately.
8821da177e4SLinus Torvalds  *
8835739411aSCornelia Huck  * This adds @dev to the kobject hierarchy via kobject_add(), adds it
8841da177e4SLinus Torvalds  * to the global and sibling lists for the device, then
8851da177e4SLinus Torvalds  * adds it to the other relevant subsystems of the driver model.
8865739411aSCornelia Huck  *
8875739411aSCornelia Huck  * NOTE: _Never_ directly free @dev after calling this function, even
8885739411aSCornelia Huck  * if it returned an error! Always use put_device() to give up your
8895739411aSCornelia Huck  * reference instead.
8901da177e4SLinus Torvalds  */
8911da177e4SLinus Torvalds int device_add(struct device *dev)
8921da177e4SLinus Torvalds {
8931da177e4SLinus Torvalds 	struct device *parent = NULL;
894c47ed219SGreg Kroah-Hartman 	struct class_interface *class_intf;
895c906a48aSGreg Kroah-Hartman 	int error = -EINVAL;
896775b64d2SRafael J. Wysocki 
8971da177e4SLinus Torvalds 	dev = get_device(dev);
898c906a48aSGreg Kroah-Hartman 	if (!dev)
899c906a48aSGreg Kroah-Hartman 		goto done;
900c906a48aSGreg Kroah-Hartman 
901fb069a5dSGreg Kroah-Hartman 	if (!dev->p) {
902b4028437SGreg Kroah-Hartman 		error = device_private_init(dev);
903b4028437SGreg Kroah-Hartman 		if (error)
904fb069a5dSGreg Kroah-Hartman 			goto done;
905fb069a5dSGreg Kroah-Hartman 	}
906fb069a5dSGreg Kroah-Hartman 
9071fa5ae85SKay Sievers 	/*
9081fa5ae85SKay Sievers 	 * for statically allocated devices, which should all be converted
9091fa5ae85SKay Sievers 	 * some day, we need to initialize the name. We prevent reading back
9101fa5ae85SKay Sievers 	 * the name, and force the use of dev_name()
9111fa5ae85SKay Sievers 	 */
9121fa5ae85SKay Sievers 	if (dev->init_name) {
913acc0e90fSGreg Kroah-Hartman 		dev_set_name(dev, "%s", dev->init_name);
9141fa5ae85SKay Sievers 		dev->init_name = NULL;
9151fa5ae85SKay Sievers 	}
916c906a48aSGreg Kroah-Hartman 
917e6309e75SThomas Gleixner 	if (!dev_name(dev)) {
918e6309e75SThomas Gleixner 		error = -EINVAL;
9195c8563d7SKay Sievers 		goto name_error;
920e6309e75SThomas Gleixner 	}
9211da177e4SLinus Torvalds 
9221e0b2cf9SKay Sievers 	pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
923c205ef48SGreg Kroah-Hartman 
9241da177e4SLinus Torvalds 	parent = get_device(dev->parent);
92563b6971aSCornelia Huck 	setup_parent(dev, parent);
9261da177e4SLinus Torvalds 
9270d358f22SYinghai Lu 	/* use parent numa_node */
9280d358f22SYinghai Lu 	if (parent)
9290d358f22SYinghai Lu 		set_dev_node(dev, dev_to_node(parent));
9300d358f22SYinghai Lu 
9311da177e4SLinus Torvalds 	/* first, register with generic layer. */
9328a577ffcSKay Sievers 	/* we require the name to be set before, and pass NULL */
9338a577ffcSKay Sievers 	error = kobject_add(&dev->kobj, dev->kobj.parent, NULL);
93440fa5422SGreg Kroah-Hartman 	if (error)
9351da177e4SLinus Torvalds 		goto Error;
936a7fd6706SKay Sievers 
93737022644SBrian Walsh 	/* notify platform of device entry */
93837022644SBrian Walsh 	if (platform_notify)
93937022644SBrian Walsh 		platform_notify(dev);
94037022644SBrian Walsh 
941ad6a1e1cSTejun Heo 	error = device_create_file(dev, &uevent_attr);
942a306eea4SCornelia Huck 	if (error)
943a306eea4SCornelia Huck 		goto attrError;
944a7fd6706SKay Sievers 
94523681e47SGreg Kroah-Hartman 	if (MAJOR(dev->devt)) {
946ad6a1e1cSTejun Heo 		error = device_create_file(dev, &devt_attr);
947ad6a1e1cSTejun Heo 		if (error)
948a306eea4SCornelia Huck 			goto ueventattrError;
949e105b8bfSDan Williams 
950e105b8bfSDan Williams 		error = device_create_sys_dev_entry(dev);
951e105b8bfSDan Williams 		if (error)
952e105b8bfSDan Williams 			goto devtattrError;
9532b2af54aSKay Sievers 
9542b2af54aSKay Sievers 		devtmpfs_create_node(dev);
95523681e47SGreg Kroah-Hartman 	}
95623681e47SGreg Kroah-Hartman 
9572ee97cafSCornelia Huck 	error = device_add_class_symlinks(dev);
9582ee97cafSCornelia Huck 	if (error)
9592ee97cafSCornelia Huck 		goto SymlinkError;
960dc0afa83SCornelia Huck 	error = device_add_attrs(dev);
961dc0afa83SCornelia Huck 	if (error)
9622620efefSGreg Kroah-Hartman 		goto AttrsError;
963dc0afa83SCornelia Huck 	error = bus_add_device(dev);
964dc0afa83SCornelia Huck 	if (error)
9651da177e4SLinus Torvalds 		goto BusError;
9663b98aeafSAlan Stern 	error = dpm_sysfs_add(dev);
96757eee3d2SRafael J. Wysocki 	if (error)
9683b98aeafSAlan Stern 		goto DPMError;
9693b98aeafSAlan Stern 	device_pm_add(dev);
970ec0676eeSAlan Stern 
971ec0676eeSAlan Stern 	/* Notify clients of device addition.  This call must come
972ec0676eeSAlan Stern 	 * after dpm_sysf_add() and before kobject_uevent().
973ec0676eeSAlan Stern 	 */
974ec0676eeSAlan Stern 	if (dev->bus)
975ec0676eeSAlan Stern 		blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
976ec0676eeSAlan Stern 					     BUS_NOTIFY_ADD_DEVICE, dev);
977ec0676eeSAlan Stern 
97853877d06SKay Sievers 	kobject_uevent(&dev->kobj, KOBJ_ADD);
9792023c610SAlan Stern 	bus_probe_device(dev);
9801da177e4SLinus Torvalds 	if (parent)
981f791b8c8SGreg Kroah-Hartman 		klist_add_tail(&dev->p->knode_parent,
982f791b8c8SGreg Kroah-Hartman 			       &parent->p->klist_children);
9831da177e4SLinus Torvalds 
9845d9fd169SGreg Kroah-Hartman 	if (dev->class) {
985f75b1c60SDave Young 		mutex_lock(&dev->class->p->class_mutex);
986c47ed219SGreg Kroah-Hartman 		/* tie the class to the device */
9875a3ceb86STejun Heo 		klist_add_tail(&dev->knode_class,
9886b6e39a6SKay Sievers 			       &dev->class->p->klist_devices);
989c47ed219SGreg Kroah-Hartman 
990c47ed219SGreg Kroah-Hartman 		/* notify any interfaces that the device is here */
991184f1f77SGreg Kroah-Hartman 		list_for_each_entry(class_intf,
992184f1f77SGreg Kroah-Hartman 				    &dev->class->p->class_interfaces, node)
993c47ed219SGreg Kroah-Hartman 			if (class_intf->add_dev)
994c47ed219SGreg Kroah-Hartman 				class_intf->add_dev(dev, class_intf);
995f75b1c60SDave Young 		mutex_unlock(&dev->class->p->class_mutex);
9965d9fd169SGreg Kroah-Hartman 	}
997c906a48aSGreg Kroah-Hartman done:
9981da177e4SLinus Torvalds 	put_device(dev);
9991da177e4SLinus Torvalds 	return error;
10003b98aeafSAlan Stern  DPMError:
100157eee3d2SRafael J. Wysocki 	bus_remove_device(dev);
100257eee3d2SRafael J. Wysocki  BusError:
10032620efefSGreg Kroah-Hartman 	device_remove_attrs(dev);
10042620efefSGreg Kroah-Hartman  AttrsError:
10052ee97cafSCornelia Huck 	device_remove_class_symlinks(dev);
10062ee97cafSCornelia Huck  SymlinkError:
1007ad6a1e1cSTejun Heo 	if (MAJOR(dev->devt))
1008ad72956dSKay Sievers 		devtmpfs_delete_node(dev);
1009ad72956dSKay Sievers 	if (MAJOR(dev->devt))
1010e105b8bfSDan Williams 		device_remove_sys_dev_entry(dev);
1011e105b8bfSDan Williams  devtattrError:
1012e105b8bfSDan Williams 	if (MAJOR(dev->devt))
1013ad6a1e1cSTejun Heo 		device_remove_file(dev, &devt_attr);
1014a306eea4SCornelia Huck  ueventattrError:
1015ad6a1e1cSTejun Heo 	device_remove_file(dev, &uevent_attr);
101623681e47SGreg Kroah-Hartman  attrError:
1017312c004dSKay Sievers 	kobject_uevent(&dev->kobj, KOBJ_REMOVE);
10181da177e4SLinus Torvalds 	kobject_del(&dev->kobj);
10191da177e4SLinus Torvalds  Error:
102063b6971aSCornelia Huck 	cleanup_device_parent(dev);
10211da177e4SLinus Torvalds 	if (parent)
10221da177e4SLinus Torvalds 		put_device(parent);
10235c8563d7SKay Sievers name_error:
10245c8563d7SKay Sievers 	kfree(dev->p);
10255c8563d7SKay Sievers 	dev->p = NULL;
1026c906a48aSGreg Kroah-Hartman 	goto done;
10271da177e4SLinus Torvalds }
10281da177e4SLinus Torvalds 
10291da177e4SLinus Torvalds /**
10301da177e4SLinus Torvalds  * device_register - register a device with the system.
10311da177e4SLinus Torvalds  * @dev: pointer to the device structure
10321da177e4SLinus Torvalds  *
10331da177e4SLinus Torvalds  * This happens in two clean steps - initialize the device
10341da177e4SLinus Torvalds  * and add it to the system. The two steps can be called
10351da177e4SLinus Torvalds  * separately, but this is the easiest and most common.
10361da177e4SLinus Torvalds  * I.e. you should only call the two helpers separately if
10371da177e4SLinus Torvalds  * have a clearly defined need to use and refcount the device
10381da177e4SLinus Torvalds  * before it is added to the hierarchy.
10395739411aSCornelia Huck  *
10405739411aSCornelia Huck  * NOTE: _Never_ directly free @dev after calling this function, even
10415739411aSCornelia Huck  * if it returned an error! Always use put_device() to give up the
10425739411aSCornelia Huck  * reference initialized in this function instead.
10431da177e4SLinus Torvalds  */
10441da177e4SLinus Torvalds int device_register(struct device *dev)
10451da177e4SLinus Torvalds {
10461da177e4SLinus Torvalds 	device_initialize(dev);
10471da177e4SLinus Torvalds 	return device_add(dev);
10481da177e4SLinus Torvalds }
10491da177e4SLinus Torvalds 
10501da177e4SLinus Torvalds /**
10511da177e4SLinus Torvalds  * get_device - increment reference count for device.
10521da177e4SLinus Torvalds  * @dev: device.
10531da177e4SLinus Torvalds  *
10541da177e4SLinus Torvalds  * This simply forwards the call to kobject_get(), though
10551da177e4SLinus Torvalds  * we do take care to provide for the case that we get a NULL
10561da177e4SLinus Torvalds  * pointer passed in.
10571da177e4SLinus Torvalds  */
10581da177e4SLinus Torvalds struct device *get_device(struct device *dev)
10591da177e4SLinus Torvalds {
10601da177e4SLinus Torvalds 	return dev ? to_dev(kobject_get(&dev->kobj)) : NULL;
10611da177e4SLinus Torvalds }
10621da177e4SLinus Torvalds 
10631da177e4SLinus Torvalds /**
10641da177e4SLinus Torvalds  * put_device - decrement reference count.
10651da177e4SLinus Torvalds  * @dev: device in question.
10661da177e4SLinus Torvalds  */
10671da177e4SLinus Torvalds void put_device(struct device *dev)
10681da177e4SLinus Torvalds {
1069edfaa7c3SKay Sievers 	/* might_sleep(); */
10701da177e4SLinus Torvalds 	if (dev)
10711da177e4SLinus Torvalds 		kobject_put(&dev->kobj);
10721da177e4SLinus Torvalds }
10731da177e4SLinus Torvalds 
10741da177e4SLinus Torvalds /**
10751da177e4SLinus Torvalds  * device_del - delete device from system.
10761da177e4SLinus Torvalds  * @dev: device.
10771da177e4SLinus Torvalds  *
10781da177e4SLinus Torvalds  * This is the first part of the device unregistration
10791da177e4SLinus Torvalds  * sequence. This removes the device from the lists we control
10801da177e4SLinus Torvalds  * from here, has it removed from the other driver model
10811da177e4SLinus Torvalds  * subsystems it was added to in device_add(), and removes it
10821da177e4SLinus Torvalds  * from the kobject hierarchy.
10831da177e4SLinus Torvalds  *
10841da177e4SLinus Torvalds  * NOTE: this should be called manually _iff_ device_add() was
10851da177e4SLinus Torvalds  * also called manually.
10861da177e4SLinus Torvalds  */
10871da177e4SLinus Torvalds void device_del(struct device *dev)
10881da177e4SLinus Torvalds {
10891da177e4SLinus Torvalds 	struct device *parent = dev->parent;
1090c47ed219SGreg Kroah-Hartman 	struct class_interface *class_intf;
10911da177e4SLinus Torvalds 
1092ec0676eeSAlan Stern 	/* Notify clients of device removal.  This call must come
1093ec0676eeSAlan Stern 	 * before dpm_sysfs_remove().
1094ec0676eeSAlan Stern 	 */
1095ec0676eeSAlan Stern 	if (dev->bus)
1096ec0676eeSAlan Stern 		blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
1097ec0676eeSAlan Stern 					     BUS_NOTIFY_DEL_DEVICE, dev);
1098775b64d2SRafael J. Wysocki 	device_pm_remove(dev);
10993b98aeafSAlan Stern 	dpm_sysfs_remove(dev);
11001da177e4SLinus Torvalds 	if (parent)
1101f791b8c8SGreg Kroah-Hartman 		klist_del(&dev->p->knode_parent);
1102e105b8bfSDan Williams 	if (MAJOR(dev->devt)) {
11032b2af54aSKay Sievers 		devtmpfs_delete_node(dev);
1104e105b8bfSDan Williams 		device_remove_sys_dev_entry(dev);
1105ad6a1e1cSTejun Heo 		device_remove_file(dev, &devt_attr);
1106e105b8bfSDan Williams 	}
1107b9d9c82bSKay Sievers 	if (dev->class) {
1108da231fd5SKay Sievers 		device_remove_class_symlinks(dev);
110999ef3ef8SKay Sievers 
1110f75b1c60SDave Young 		mutex_lock(&dev->class->p->class_mutex);
1111c47ed219SGreg Kroah-Hartman 		/* notify any interfaces that the device is now gone */
1112184f1f77SGreg Kroah-Hartman 		list_for_each_entry(class_intf,
1113184f1f77SGreg Kroah-Hartman 				    &dev->class->p->class_interfaces, node)
1114c47ed219SGreg Kroah-Hartman 			if (class_intf->remove_dev)
1115c47ed219SGreg Kroah-Hartman 				class_intf->remove_dev(dev, class_intf);
1116c47ed219SGreg Kroah-Hartman 		/* remove the device from the class list */
11175a3ceb86STejun Heo 		klist_del(&dev->knode_class);
1118f75b1c60SDave Young 		mutex_unlock(&dev->class->p->class_mutex);
1119b9d9c82bSKay Sievers 	}
1120ad6a1e1cSTejun Heo 	device_remove_file(dev, &uevent_attr);
11212620efefSGreg Kroah-Hartman 	device_remove_attrs(dev);
112228953533SBenjamin Herrenschmidt 	bus_remove_device(dev);
11231da177e4SLinus Torvalds 
11242f8d16a9STejun Heo 	/*
11252f8d16a9STejun Heo 	 * Some platform devices are driven without driver attached
11262f8d16a9STejun Heo 	 * and managed resources may have been acquired.  Make sure
11272f8d16a9STejun Heo 	 * all resources are released.
11282f8d16a9STejun Heo 	 */
11292f8d16a9STejun Heo 	devres_release_all(dev);
11302f8d16a9STejun Heo 
11311da177e4SLinus Torvalds 	/* Notify the platform of the removal, in case they
11321da177e4SLinus Torvalds 	 * need to do anything...
11331da177e4SLinus Torvalds 	 */
11341da177e4SLinus Torvalds 	if (platform_notify_remove)
11351da177e4SLinus Torvalds 		platform_notify_remove(dev);
1136312c004dSKay Sievers 	kobject_uevent(&dev->kobj, KOBJ_REMOVE);
1137da231fd5SKay Sievers 	cleanup_device_parent(dev);
11381da177e4SLinus Torvalds 	kobject_del(&dev->kobj);
11391da177e4SLinus Torvalds 	put_device(parent);
11401da177e4SLinus Torvalds }
11411da177e4SLinus Torvalds 
11421da177e4SLinus Torvalds /**
11431da177e4SLinus Torvalds  * device_unregister - unregister device from system.
11441da177e4SLinus Torvalds  * @dev: device going away.
11451da177e4SLinus Torvalds  *
11461da177e4SLinus Torvalds  * We do this in two parts, like we do device_register(). First,
11471da177e4SLinus Torvalds  * we remove it from all the subsystems with device_del(), then
11481da177e4SLinus Torvalds  * we decrement the reference count via put_device(). If that
11491da177e4SLinus Torvalds  * is the final reference count, the device will be cleaned up
11501da177e4SLinus Torvalds  * via device_release() above. Otherwise, the structure will
11511da177e4SLinus Torvalds  * stick around until the final reference to the device is dropped.
11521da177e4SLinus Torvalds  */
11531da177e4SLinus Torvalds void device_unregister(struct device *dev)
11541da177e4SLinus Torvalds {
11551e0b2cf9SKay Sievers 	pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
11561da177e4SLinus Torvalds 	device_del(dev);
11571da177e4SLinus Torvalds 	put_device(dev);
11581da177e4SLinus Torvalds }
11591da177e4SLinus Torvalds 
116036239577Smochel@digitalimplant.org static struct device *next_device(struct klist_iter *i)
116136239577Smochel@digitalimplant.org {
116236239577Smochel@digitalimplant.org 	struct klist_node *n = klist_next(i);
1163f791b8c8SGreg Kroah-Hartman 	struct device *dev = NULL;
1164f791b8c8SGreg Kroah-Hartman 	struct device_private *p;
1165f791b8c8SGreg Kroah-Hartman 
1166f791b8c8SGreg Kroah-Hartman 	if (n) {
1167f791b8c8SGreg Kroah-Hartman 		p = to_device_private_parent(n);
1168f791b8c8SGreg Kroah-Hartman 		dev = p->device;
1169f791b8c8SGreg Kroah-Hartman 	}
1170f791b8c8SGreg Kroah-Hartman 	return dev;
117136239577Smochel@digitalimplant.org }
117236239577Smochel@digitalimplant.org 
11731da177e4SLinus Torvalds /**
1174e454cea2SKay Sievers  * device_get_devnode - path of device node file
11756fcf53acSKay Sievers  * @dev: device
1176e454cea2SKay Sievers  * @mode: returned file access mode
11776fcf53acSKay Sievers  * @tmp: possibly allocated string
11786fcf53acSKay Sievers  *
11796fcf53acSKay Sievers  * Return the relative path of a possible device node.
11806fcf53acSKay Sievers  * Non-default names may need to allocate a memory to compose
11816fcf53acSKay Sievers  * a name. This memory is returned in tmp and needs to be
11826fcf53acSKay Sievers  * freed by the caller.
11836fcf53acSKay Sievers  */
1184e454cea2SKay Sievers const char *device_get_devnode(struct device *dev,
1185e454cea2SKay Sievers 			       mode_t *mode, const char **tmp)
11866fcf53acSKay Sievers {
11876fcf53acSKay Sievers 	char *s;
11886fcf53acSKay Sievers 
11896fcf53acSKay Sievers 	*tmp = NULL;
11906fcf53acSKay Sievers 
11916fcf53acSKay Sievers 	/* the device type may provide a specific name */
1192e454cea2SKay Sievers 	if (dev->type && dev->type->devnode)
1193e454cea2SKay Sievers 		*tmp = dev->type->devnode(dev, mode);
11946fcf53acSKay Sievers 	if (*tmp)
11956fcf53acSKay Sievers 		return *tmp;
11966fcf53acSKay Sievers 
11976fcf53acSKay Sievers 	/* the class may provide a specific name */
1198e454cea2SKay Sievers 	if (dev->class && dev->class->devnode)
1199e454cea2SKay Sievers 		*tmp = dev->class->devnode(dev, mode);
12006fcf53acSKay Sievers 	if (*tmp)
12016fcf53acSKay Sievers 		return *tmp;
12026fcf53acSKay Sievers 
12036fcf53acSKay Sievers 	/* return name without allocation, tmp == NULL */
12046fcf53acSKay Sievers 	if (strchr(dev_name(dev), '!') == NULL)
12056fcf53acSKay Sievers 		return dev_name(dev);
12066fcf53acSKay Sievers 
12076fcf53acSKay Sievers 	/* replace '!' in the name with '/' */
12086fcf53acSKay Sievers 	*tmp = kstrdup(dev_name(dev), GFP_KERNEL);
12096fcf53acSKay Sievers 	if (!*tmp)
12106fcf53acSKay Sievers 		return NULL;
12116fcf53acSKay Sievers 	while ((s = strchr(*tmp, '!')))
12126fcf53acSKay Sievers 		s[0] = '/';
12136fcf53acSKay Sievers 	return *tmp;
12146fcf53acSKay Sievers }
12156fcf53acSKay Sievers 
12166fcf53acSKay Sievers /**
12171da177e4SLinus Torvalds  * device_for_each_child - device child iterator.
1218c41455fbSRandy Dunlap  * @parent: parent struct device.
12191da177e4SLinus Torvalds  * @data: data for the callback.
12201da177e4SLinus Torvalds  * @fn: function to be called for each device.
12211da177e4SLinus Torvalds  *
1222c41455fbSRandy Dunlap  * Iterate over @parent's child devices, and call @fn for each,
12231da177e4SLinus Torvalds  * passing it @data.
12241da177e4SLinus Torvalds  *
12251da177e4SLinus Torvalds  * We check the return of @fn each time. If it returns anything
12261da177e4SLinus Torvalds  * other than 0, we break out and return that value.
12271da177e4SLinus Torvalds  */
122836239577Smochel@digitalimplant.org int device_for_each_child(struct device *parent, void *data,
12294a3ad20cSGreg Kroah-Hartman 			  int (*fn)(struct device *dev, void *data))
12301da177e4SLinus Torvalds {
123136239577Smochel@digitalimplant.org 	struct klist_iter i;
12321da177e4SLinus Torvalds 	struct device *child;
12331da177e4SLinus Torvalds 	int error = 0;
12341da177e4SLinus Torvalds 
1235014c90dbSGreg Kroah-Hartman 	if (!parent->p)
1236014c90dbSGreg Kroah-Hartman 		return 0;
1237014c90dbSGreg Kroah-Hartman 
1238f791b8c8SGreg Kroah-Hartman 	klist_iter_init(&parent->p->klist_children, &i);
123936239577Smochel@digitalimplant.org 	while ((child = next_device(&i)) && !error)
124036239577Smochel@digitalimplant.org 		error = fn(child, data);
124136239577Smochel@digitalimplant.org 	klist_iter_exit(&i);
12421da177e4SLinus Torvalds 	return error;
12431da177e4SLinus Torvalds }
12441da177e4SLinus Torvalds 
12455ab69981SCornelia Huck /**
12465ab69981SCornelia Huck  * device_find_child - device iterator for locating a particular device.
12475ab69981SCornelia Huck  * @parent: parent struct device
12485ab69981SCornelia Huck  * @data: Data to pass to match function
12495ab69981SCornelia Huck  * @match: Callback function to check device
12505ab69981SCornelia Huck  *
12515ab69981SCornelia Huck  * This is similar to the device_for_each_child() function above, but it
12525ab69981SCornelia Huck  * returns a reference to a device that is 'found' for later use, as
12535ab69981SCornelia Huck  * determined by the @match callback.
12545ab69981SCornelia Huck  *
12555ab69981SCornelia Huck  * The callback should return 0 if the device doesn't match and non-zero
12565ab69981SCornelia Huck  * if it does.  If the callback returns non-zero and a reference to the
12575ab69981SCornelia Huck  * current device can be obtained, this function will return to the caller
12585ab69981SCornelia Huck  * and not iterate over any more devices.
12595ab69981SCornelia Huck  */
12605ab69981SCornelia Huck struct device *device_find_child(struct device *parent, void *data,
12614a3ad20cSGreg Kroah-Hartman 				 int (*match)(struct device *dev, void *data))
12625ab69981SCornelia Huck {
12635ab69981SCornelia Huck 	struct klist_iter i;
12645ab69981SCornelia Huck 	struct device *child;
12655ab69981SCornelia Huck 
12665ab69981SCornelia Huck 	if (!parent)
12675ab69981SCornelia Huck 		return NULL;
12685ab69981SCornelia Huck 
1269f791b8c8SGreg Kroah-Hartman 	klist_iter_init(&parent->p->klist_children, &i);
12705ab69981SCornelia Huck 	while ((child = next_device(&i)))
12715ab69981SCornelia Huck 		if (match(child, data) && get_device(child))
12725ab69981SCornelia Huck 			break;
12735ab69981SCornelia Huck 	klist_iter_exit(&i);
12745ab69981SCornelia Huck 	return child;
12755ab69981SCornelia Huck }
12765ab69981SCornelia Huck 
12771da177e4SLinus Torvalds int __init devices_init(void)
12781da177e4SLinus Torvalds {
1279881c6cfdSGreg Kroah-Hartman 	devices_kset = kset_create_and_add("devices", &device_uevent_ops, NULL);
1280881c6cfdSGreg Kroah-Hartman 	if (!devices_kset)
1281881c6cfdSGreg Kroah-Hartman 		return -ENOMEM;
1282e105b8bfSDan Williams 	dev_kobj = kobject_create_and_add("dev", NULL);
1283e105b8bfSDan Williams 	if (!dev_kobj)
1284e105b8bfSDan Williams 		goto dev_kobj_err;
1285e105b8bfSDan Williams 	sysfs_dev_block_kobj = kobject_create_and_add("block", dev_kobj);
1286e105b8bfSDan Williams 	if (!sysfs_dev_block_kobj)
1287e105b8bfSDan Williams 		goto block_kobj_err;
1288e105b8bfSDan Williams 	sysfs_dev_char_kobj = kobject_create_and_add("char", dev_kobj);
1289e105b8bfSDan Williams 	if (!sysfs_dev_char_kobj)
1290e105b8bfSDan Williams 		goto char_kobj_err;
1291e105b8bfSDan Williams 
1292881c6cfdSGreg Kroah-Hartman 	return 0;
1293e105b8bfSDan Williams 
1294e105b8bfSDan Williams  char_kobj_err:
1295e105b8bfSDan Williams 	kobject_put(sysfs_dev_block_kobj);
1296e105b8bfSDan Williams  block_kobj_err:
1297e105b8bfSDan Williams 	kobject_put(dev_kobj);
1298e105b8bfSDan Williams  dev_kobj_err:
1299e105b8bfSDan Williams 	kset_unregister(devices_kset);
1300e105b8bfSDan Williams 	return -ENOMEM;
13011da177e4SLinus Torvalds }
13021da177e4SLinus Torvalds 
13031da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(device_for_each_child);
13045ab69981SCornelia Huck EXPORT_SYMBOL_GPL(device_find_child);
13051da177e4SLinus Torvalds 
13061da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(device_initialize);
13071da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(device_add);
13081da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(device_register);
13091da177e4SLinus Torvalds 
13101da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(device_del);
13111da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(device_unregister);
13121da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(get_device);
13131da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(put_device);
13141da177e4SLinus Torvalds 
13151da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(device_create_file);
13161da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(device_remove_file);
131723681e47SGreg Kroah-Hartman 
13187f100d15SKarthigan Srinivasan struct root_device {
13190aa0dc41SMark McLoughlin 	struct device dev;
13200aa0dc41SMark McLoughlin 	struct module *owner;
13210aa0dc41SMark McLoughlin };
13220aa0dc41SMark McLoughlin 
1323481e2079SFerenc Wagner inline struct root_device *to_root_device(struct device *d)
1324481e2079SFerenc Wagner {
1325481e2079SFerenc Wagner 	return container_of(d, struct root_device, dev);
1326481e2079SFerenc Wagner }
13270aa0dc41SMark McLoughlin 
13280aa0dc41SMark McLoughlin static void root_device_release(struct device *dev)
13290aa0dc41SMark McLoughlin {
13300aa0dc41SMark McLoughlin 	kfree(to_root_device(dev));
13310aa0dc41SMark McLoughlin }
13320aa0dc41SMark McLoughlin 
13330aa0dc41SMark McLoughlin /**
13340aa0dc41SMark McLoughlin  * __root_device_register - allocate and register a root device
13350aa0dc41SMark McLoughlin  * @name: root device name
13360aa0dc41SMark McLoughlin  * @owner: owner module of the root device, usually THIS_MODULE
13370aa0dc41SMark McLoughlin  *
13380aa0dc41SMark McLoughlin  * This function allocates a root device and registers it
13390aa0dc41SMark McLoughlin  * using device_register(). In order to free the returned
13400aa0dc41SMark McLoughlin  * device, use root_device_unregister().
13410aa0dc41SMark McLoughlin  *
13420aa0dc41SMark McLoughlin  * Root devices are dummy devices which allow other devices
13430aa0dc41SMark McLoughlin  * to be grouped under /sys/devices. Use this function to
13440aa0dc41SMark McLoughlin  * allocate a root device and then use it as the parent of
13450aa0dc41SMark McLoughlin  * any device which should appear under /sys/devices/{name}
13460aa0dc41SMark McLoughlin  *
13470aa0dc41SMark McLoughlin  * The /sys/devices/{name} directory will also contain a
13480aa0dc41SMark McLoughlin  * 'module' symlink which points to the @owner directory
13490aa0dc41SMark McLoughlin  * in sysfs.
13500aa0dc41SMark McLoughlin  *
1351f0eae0edSJani Nikula  * Returns &struct device pointer on success, or ERR_PTR() on error.
1352f0eae0edSJani Nikula  *
13530aa0dc41SMark McLoughlin  * Note: You probably want to use root_device_register().
13540aa0dc41SMark McLoughlin  */
13550aa0dc41SMark McLoughlin struct device *__root_device_register(const char *name, struct module *owner)
13560aa0dc41SMark McLoughlin {
13570aa0dc41SMark McLoughlin 	struct root_device *root;
13580aa0dc41SMark McLoughlin 	int err = -ENOMEM;
13590aa0dc41SMark McLoughlin 
13600aa0dc41SMark McLoughlin 	root = kzalloc(sizeof(struct root_device), GFP_KERNEL);
13610aa0dc41SMark McLoughlin 	if (!root)
13620aa0dc41SMark McLoughlin 		return ERR_PTR(err);
13630aa0dc41SMark McLoughlin 
1364acc0e90fSGreg Kroah-Hartman 	err = dev_set_name(&root->dev, "%s", name);
13650aa0dc41SMark McLoughlin 	if (err) {
13660aa0dc41SMark McLoughlin 		kfree(root);
13670aa0dc41SMark McLoughlin 		return ERR_PTR(err);
13680aa0dc41SMark McLoughlin 	}
13690aa0dc41SMark McLoughlin 
13700aa0dc41SMark McLoughlin 	root->dev.release = root_device_release;
13710aa0dc41SMark McLoughlin 
13720aa0dc41SMark McLoughlin 	err = device_register(&root->dev);
13730aa0dc41SMark McLoughlin 	if (err) {
13740aa0dc41SMark McLoughlin 		put_device(&root->dev);
13750aa0dc41SMark McLoughlin 		return ERR_PTR(err);
13760aa0dc41SMark McLoughlin 	}
13770aa0dc41SMark McLoughlin 
13781d9e882bSChristoph Egger #ifdef CONFIG_MODULES	/* gotta find a "cleaner" way to do this */
13790aa0dc41SMark McLoughlin 	if (owner) {
13800aa0dc41SMark McLoughlin 		struct module_kobject *mk = &owner->mkobj;
13810aa0dc41SMark McLoughlin 
13820aa0dc41SMark McLoughlin 		err = sysfs_create_link(&root->dev.kobj, &mk->kobj, "module");
13830aa0dc41SMark McLoughlin 		if (err) {
13840aa0dc41SMark McLoughlin 			device_unregister(&root->dev);
13850aa0dc41SMark McLoughlin 			return ERR_PTR(err);
13860aa0dc41SMark McLoughlin 		}
13870aa0dc41SMark McLoughlin 		root->owner = owner;
13880aa0dc41SMark McLoughlin 	}
13890aa0dc41SMark McLoughlin #endif
13900aa0dc41SMark McLoughlin 
13910aa0dc41SMark McLoughlin 	return &root->dev;
13920aa0dc41SMark McLoughlin }
13930aa0dc41SMark McLoughlin EXPORT_SYMBOL_GPL(__root_device_register);
13940aa0dc41SMark McLoughlin 
13950aa0dc41SMark McLoughlin /**
13960aa0dc41SMark McLoughlin  * root_device_unregister - unregister and free a root device
13977cbcf225SRandy Dunlap  * @dev: device going away
13980aa0dc41SMark McLoughlin  *
13990aa0dc41SMark McLoughlin  * This function unregisters and cleans up a device that was created by
14000aa0dc41SMark McLoughlin  * root_device_register().
14010aa0dc41SMark McLoughlin  */
14020aa0dc41SMark McLoughlin void root_device_unregister(struct device *dev)
14030aa0dc41SMark McLoughlin {
14040aa0dc41SMark McLoughlin 	struct root_device *root = to_root_device(dev);
14050aa0dc41SMark McLoughlin 
14060aa0dc41SMark McLoughlin 	if (root->owner)
14070aa0dc41SMark McLoughlin 		sysfs_remove_link(&root->dev.kobj, "module");
14080aa0dc41SMark McLoughlin 
14090aa0dc41SMark McLoughlin 	device_unregister(dev);
14100aa0dc41SMark McLoughlin }
14110aa0dc41SMark McLoughlin EXPORT_SYMBOL_GPL(root_device_unregister);
14120aa0dc41SMark McLoughlin 
141323681e47SGreg Kroah-Hartman 
141423681e47SGreg Kroah-Hartman static void device_create_release(struct device *dev)
141523681e47SGreg Kroah-Hartman {
14161e0b2cf9SKay Sievers 	pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
141723681e47SGreg Kroah-Hartman 	kfree(dev);
141823681e47SGreg Kroah-Hartman }
141923681e47SGreg Kroah-Hartman 
142023681e47SGreg Kroah-Hartman /**
14218882b394SGreg Kroah-Hartman  * device_create_vargs - creates a device and registers it with sysfs
14228882b394SGreg Kroah-Hartman  * @class: pointer to the struct class that this device should be registered to
14238882b394SGreg Kroah-Hartman  * @parent: pointer to the parent struct device of this new device, if any
14248882b394SGreg Kroah-Hartman  * @devt: the dev_t for the char device to be added
14258882b394SGreg Kroah-Hartman  * @drvdata: the data to be added to the device for callbacks
14268882b394SGreg Kroah-Hartman  * @fmt: string for the device's name
14278882b394SGreg Kroah-Hartman  * @args: va_list for the device's name
14288882b394SGreg Kroah-Hartman  *
14298882b394SGreg Kroah-Hartman  * This function can be used by char device classes.  A struct device
14308882b394SGreg Kroah-Hartman  * will be created in sysfs, registered to the specified class.
14318882b394SGreg Kroah-Hartman  *
14328882b394SGreg Kroah-Hartman  * A "dev" file will be created, showing the dev_t for the device, if
14338882b394SGreg Kroah-Hartman  * the dev_t is not 0,0.
14348882b394SGreg Kroah-Hartman  * If a pointer to a parent struct device is passed in, the newly created
14358882b394SGreg Kroah-Hartman  * struct device will be a child of that device in sysfs.
14368882b394SGreg Kroah-Hartman  * The pointer to the struct device will be returned from the call.
14378882b394SGreg Kroah-Hartman  * Any further sysfs files that might be required can be created using this
14388882b394SGreg Kroah-Hartman  * pointer.
14398882b394SGreg Kroah-Hartman  *
1440f0eae0edSJani Nikula  * Returns &struct device pointer on success, or ERR_PTR() on error.
1441f0eae0edSJani Nikula  *
14428882b394SGreg Kroah-Hartman  * Note: the struct class passed to this function must have previously
14438882b394SGreg Kroah-Hartman  * been created with a call to class_create().
14448882b394SGreg Kroah-Hartman  */
14458882b394SGreg Kroah-Hartman struct device *device_create_vargs(struct class *class, struct device *parent,
14468882b394SGreg Kroah-Hartman 				   dev_t devt, void *drvdata, const char *fmt,
14478882b394SGreg Kroah-Hartman 				   va_list args)
14488882b394SGreg Kroah-Hartman {
14498882b394SGreg Kroah-Hartman 	struct device *dev = NULL;
14508882b394SGreg Kroah-Hartman 	int retval = -ENODEV;
14518882b394SGreg Kroah-Hartman 
14528882b394SGreg Kroah-Hartman 	if (class == NULL || IS_ERR(class))
14538882b394SGreg Kroah-Hartman 		goto error;
14548882b394SGreg Kroah-Hartman 
14558882b394SGreg Kroah-Hartman 	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
14568882b394SGreg Kroah-Hartman 	if (!dev) {
14578882b394SGreg Kroah-Hartman 		retval = -ENOMEM;
14588882b394SGreg Kroah-Hartman 		goto error;
14598882b394SGreg Kroah-Hartman 	}
14608882b394SGreg Kroah-Hartman 
14618882b394SGreg Kroah-Hartman 	dev->devt = devt;
14628882b394SGreg Kroah-Hartman 	dev->class = class;
14638882b394SGreg Kroah-Hartman 	dev->parent = parent;
14648882b394SGreg Kroah-Hartman 	dev->release = device_create_release;
14658882b394SGreg Kroah-Hartman 	dev_set_drvdata(dev, drvdata);
14668882b394SGreg Kroah-Hartman 
14671fa5ae85SKay Sievers 	retval = kobject_set_name_vargs(&dev->kobj, fmt, args);
14681fa5ae85SKay Sievers 	if (retval)
14691fa5ae85SKay Sievers 		goto error;
14701fa5ae85SKay Sievers 
14718882b394SGreg Kroah-Hartman 	retval = device_register(dev);
14728882b394SGreg Kroah-Hartman 	if (retval)
14738882b394SGreg Kroah-Hartman 		goto error;
14748882b394SGreg Kroah-Hartman 
14758882b394SGreg Kroah-Hartman 	return dev;
14768882b394SGreg Kroah-Hartman 
14778882b394SGreg Kroah-Hartman error:
1478286661b3SCornelia Huck 	put_device(dev);
14798882b394SGreg Kroah-Hartman 	return ERR_PTR(retval);
14808882b394SGreg Kroah-Hartman }
14818882b394SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(device_create_vargs);
14828882b394SGreg Kroah-Hartman 
14838882b394SGreg Kroah-Hartman /**
14844e106739SGreg Kroah-Hartman  * device_create - creates a device and registers it with sysfs
14858882b394SGreg Kroah-Hartman  * @class: pointer to the struct class that this device should be registered to
14868882b394SGreg Kroah-Hartman  * @parent: pointer to the parent struct device of this new device, if any
14878882b394SGreg Kroah-Hartman  * @devt: the dev_t for the char device to be added
14888882b394SGreg Kroah-Hartman  * @drvdata: the data to be added to the device for callbacks
14898882b394SGreg Kroah-Hartman  * @fmt: string for the device's name
14908882b394SGreg Kroah-Hartman  *
14918882b394SGreg Kroah-Hartman  * This function can be used by char device classes.  A struct device
14928882b394SGreg Kroah-Hartman  * will be created in sysfs, registered to the specified class.
14938882b394SGreg Kroah-Hartman  *
14948882b394SGreg Kroah-Hartman  * A "dev" file will be created, showing the dev_t for the device, if
14958882b394SGreg Kroah-Hartman  * the dev_t is not 0,0.
14968882b394SGreg Kroah-Hartman  * If a pointer to a parent struct device is passed in, the newly created
14978882b394SGreg Kroah-Hartman  * struct device will be a child of that device in sysfs.
14988882b394SGreg Kroah-Hartman  * The pointer to the struct device will be returned from the call.
14998882b394SGreg Kroah-Hartman  * Any further sysfs files that might be required can be created using this
15008882b394SGreg Kroah-Hartman  * pointer.
15018882b394SGreg Kroah-Hartman  *
1502f0eae0edSJani Nikula  * Returns &struct device pointer on success, or ERR_PTR() on error.
1503f0eae0edSJani Nikula  *
15048882b394SGreg Kroah-Hartman  * Note: the struct class passed to this function must have previously
15058882b394SGreg Kroah-Hartman  * been created with a call to class_create().
15068882b394SGreg Kroah-Hartman  */
15074e106739SGreg Kroah-Hartman struct device *device_create(struct class *class, struct device *parent,
15084e106739SGreg Kroah-Hartman 			     dev_t devt, void *drvdata, const char *fmt, ...)
15098882b394SGreg Kroah-Hartman {
15108882b394SGreg Kroah-Hartman 	va_list vargs;
15118882b394SGreg Kroah-Hartman 	struct device *dev;
15128882b394SGreg Kroah-Hartman 
15138882b394SGreg Kroah-Hartman 	va_start(vargs, fmt);
15148882b394SGreg Kroah-Hartman 	dev = device_create_vargs(class, parent, devt, drvdata, fmt, vargs);
15158882b394SGreg Kroah-Hartman 	va_end(vargs);
15168882b394SGreg Kroah-Hartman 	return dev;
15178882b394SGreg Kroah-Hartman }
15184e106739SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(device_create);
15198882b394SGreg Kroah-Hartman 
1520cd35449bSDave Young static int __match_devt(struct device *dev, void *data)
152123681e47SGreg Kroah-Hartman {
1522cd35449bSDave Young 	dev_t *devt = data;
152323681e47SGreg Kroah-Hartman 
1524cd35449bSDave Young 	return dev->devt == *devt;
1525775b64d2SRafael J. Wysocki }
152623681e47SGreg Kroah-Hartman 
1527775b64d2SRafael J. Wysocki /**
1528775b64d2SRafael J. Wysocki  * device_destroy - removes a device that was created with device_create()
1529775b64d2SRafael J. Wysocki  * @class: pointer to the struct class that this device was registered with
1530775b64d2SRafael J. Wysocki  * @devt: the dev_t of the device that was previously registered
1531775b64d2SRafael J. Wysocki  *
1532775b64d2SRafael J. Wysocki  * This call unregisters and cleans up a device that was created with a
1533775b64d2SRafael J. Wysocki  * call to device_create().
1534775b64d2SRafael J. Wysocki  */
1535775b64d2SRafael J. Wysocki void device_destroy(struct class *class, dev_t devt)
1536775b64d2SRafael J. Wysocki {
1537775b64d2SRafael J. Wysocki 	struct device *dev;
1538775b64d2SRafael J. Wysocki 
1539695794aeSGreg Kroah-Hartman 	dev = class_find_device(class, NULL, &devt, __match_devt);
1540cd35449bSDave Young 	if (dev) {
1541cd35449bSDave Young 		put_device(dev);
154223681e47SGreg Kroah-Hartman 		device_unregister(dev);
154323681e47SGreg Kroah-Hartman 	}
1544cd35449bSDave Young }
154523681e47SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(device_destroy);
1546a2de48caSGreg Kroah-Hartman 
1547a2de48caSGreg Kroah-Hartman /**
1548a2de48caSGreg Kroah-Hartman  * device_rename - renames a device
1549a2de48caSGreg Kroah-Hartman  * @dev: the pointer to the struct device to be renamed
1550a2de48caSGreg Kroah-Hartman  * @new_name: the new name of the device
1551030c1d2bSEric W. Biederman  *
1552030c1d2bSEric W. Biederman  * It is the responsibility of the caller to provide mutual
1553030c1d2bSEric W. Biederman  * exclusion between two different calls of device_rename
1554030c1d2bSEric W. Biederman  * on the same device to ensure that new_name is valid and
1555030c1d2bSEric W. Biederman  * won't conflict with other devices.
1556c6c0ac66SMichael Ellerman  *
1557a5462516STimur Tabi  * Note: Don't call this function.  Currently, the networking layer calls this
1558a5462516STimur Tabi  * function, but that will change.  The following text from Kay Sievers offers
1559a5462516STimur Tabi  * some insight:
1560a5462516STimur Tabi  *
1561a5462516STimur Tabi  * Renaming devices is racy at many levels, symlinks and other stuff are not
1562a5462516STimur Tabi  * replaced atomically, and you get a "move" uevent, but it's not easy to
1563a5462516STimur Tabi  * connect the event to the old and new device. Device nodes are not renamed at
1564a5462516STimur Tabi  * all, there isn't even support for that in the kernel now.
1565a5462516STimur Tabi  *
1566a5462516STimur Tabi  * In the meantime, during renaming, your target name might be taken by another
1567a5462516STimur Tabi  * driver, creating conflicts. Or the old name is taken directly after you
1568a5462516STimur Tabi  * renamed it -- then you get events for the same DEVPATH, before you even see
1569a5462516STimur Tabi  * the "move" event. It's just a mess, and nothing new should ever rely on
1570a5462516STimur Tabi  * kernel device renaming. Besides that, it's not even implemented now for
1571a5462516STimur Tabi  * other things than (driver-core wise very simple) network devices.
1572a5462516STimur Tabi  *
1573a5462516STimur Tabi  * We are currently about to change network renaming in udev to completely
1574a5462516STimur Tabi  * disallow renaming of devices in the same namespace as the kernel uses,
1575a5462516STimur Tabi  * because we can't solve the problems properly, that arise with swapping names
1576a5462516STimur Tabi  * of multiple interfaces without races. Means, renaming of eth[0-9]* will only
1577a5462516STimur Tabi  * be allowed to some other name than eth[0-9]*, for the aforementioned
1578a5462516STimur Tabi  * reasons.
1579a5462516STimur Tabi  *
1580a5462516STimur Tabi  * Make up a "real" name in the driver before you register anything, or add
1581a5462516STimur Tabi  * some other attributes for userspace to find the device, or use udev to add
1582a5462516STimur Tabi  * symlinks -- but never rename kernel devices later, it's a complete mess. We
1583a5462516STimur Tabi  * don't even want to get into that and try to implement the missing pieces in
1584a5462516STimur Tabi  * the core. We really have other pieces to fix in the driver core mess. :)
1585a2de48caSGreg Kroah-Hartman  */
15866937e8f8SJohannes Berg int device_rename(struct device *dev, const char *new_name)
1587a2de48caSGreg Kroah-Hartman {
1588a2de48caSGreg Kroah-Hartman 	char *old_class_name = NULL;
1589a2de48caSGreg Kroah-Hartman 	char *new_class_name = NULL;
15902ee97cafSCornelia Huck 	char *old_device_name = NULL;
1591a2de48caSGreg Kroah-Hartman 	int error;
1592a2de48caSGreg Kroah-Hartman 
1593a2de48caSGreg Kroah-Hartman 	dev = get_device(dev);
1594a2de48caSGreg Kroah-Hartman 	if (!dev)
1595a2de48caSGreg Kroah-Hartman 		return -EINVAL;
1596a2de48caSGreg Kroah-Hartman 
15971e0b2cf9SKay Sievers 	pr_debug("device: '%s': %s: renaming to '%s'\n", dev_name(dev),
15982b3a302aSHarvey Harrison 		 __func__, new_name);
1599a2de48caSGreg Kroah-Hartman 
16001fa5ae85SKay Sievers 	old_device_name = kstrdup(dev_name(dev), GFP_KERNEL);
16012ee97cafSCornelia Huck 	if (!old_device_name) {
1602952ab431SJesper Juhl 		error = -ENOMEM;
16032ee97cafSCornelia Huck 		goto out;
1604952ab431SJesper Juhl 	}
1605a2de48caSGreg Kroah-Hartman 
1606f349cf34SEric W. Biederman 	if (dev->class) {
16076b6e39a6SKay Sievers 		error = sysfs_rename_link(&dev->class->p->subsys.kobj,
1608f349cf34SEric W. Biederman 			&dev->kobj, old_device_name, new_name);
1609f349cf34SEric W. Biederman 		if (error)
1610f349cf34SEric W. Biederman 			goto out;
1611f349cf34SEric W. Biederman 	}
161239aba963SKay Sievers 
1613a2de48caSGreg Kroah-Hartman 	error = kobject_rename(&dev->kobj, new_name);
16141fa5ae85SKay Sievers 	if (error)
16152ee97cafSCornelia Huck 		goto out;
1616a2de48caSGreg Kroah-Hartman 
16172ee97cafSCornelia Huck out:
1618a2de48caSGreg Kroah-Hartman 	put_device(dev);
1619a2de48caSGreg Kroah-Hartman 
1620a2de48caSGreg Kroah-Hartman 	kfree(new_class_name);
1621952ab431SJesper Juhl 	kfree(old_class_name);
16222ee97cafSCornelia Huck 	kfree(old_device_name);
1623a2de48caSGreg Kroah-Hartman 
1624a2de48caSGreg Kroah-Hartman 	return error;
1625a2de48caSGreg Kroah-Hartman }
1626a2807dbcSJohannes Berg EXPORT_SYMBOL_GPL(device_rename);
16278a82472fSCornelia Huck 
16288a82472fSCornelia Huck static int device_move_class_links(struct device *dev,
16298a82472fSCornelia Huck 				   struct device *old_parent,
16308a82472fSCornelia Huck 				   struct device *new_parent)
16318a82472fSCornelia Huck {
1632f7f3461dSGreg Kroah-Hartman 	int error = 0;
16338a82472fSCornelia Huck 
1634f7f3461dSGreg Kroah-Hartman 	if (old_parent)
1635f7f3461dSGreg Kroah-Hartman 		sysfs_remove_link(&dev->kobj, "device");
1636f7f3461dSGreg Kroah-Hartman 	if (new_parent)
1637f7f3461dSGreg Kroah-Hartman 		error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
1638f7f3461dSGreg Kroah-Hartman 					  "device");
1639f7f3461dSGreg Kroah-Hartman 	return error;
16408a82472fSCornelia Huck }
16418a82472fSCornelia Huck 
16428a82472fSCornelia Huck /**
16438a82472fSCornelia Huck  * device_move - moves a device to a new parent
16448a82472fSCornelia Huck  * @dev: the pointer to the struct device to be moved
1645c744aeaeSCornelia Huck  * @new_parent: the new parent of the device (can by NULL)
1646ffa6a705SCornelia Huck  * @dpm_order: how to reorder the dpm_list
16478a82472fSCornelia Huck  */
1648ffa6a705SCornelia Huck int device_move(struct device *dev, struct device *new_parent,
1649ffa6a705SCornelia Huck 		enum dpm_order dpm_order)
16508a82472fSCornelia Huck {
16518a82472fSCornelia Huck 	int error;
16528a82472fSCornelia Huck 	struct device *old_parent;
1653c744aeaeSCornelia Huck 	struct kobject *new_parent_kobj;
16548a82472fSCornelia Huck 
16558a82472fSCornelia Huck 	dev = get_device(dev);
16568a82472fSCornelia Huck 	if (!dev)
16578a82472fSCornelia Huck 		return -EINVAL;
16588a82472fSCornelia Huck 
1659ffa6a705SCornelia Huck 	device_pm_lock();
16608a82472fSCornelia Huck 	new_parent = get_device(new_parent);
1661c744aeaeSCornelia Huck 	new_parent_kobj = get_device_parent(dev, new_parent);
166263b6971aSCornelia Huck 
16631e0b2cf9SKay Sievers 	pr_debug("device: '%s': %s: moving to '%s'\n", dev_name(dev),
16641e0b2cf9SKay Sievers 		 __func__, new_parent ? dev_name(new_parent) : "<NULL>");
1665c744aeaeSCornelia Huck 	error = kobject_move(&dev->kobj, new_parent_kobj);
16668a82472fSCornelia Huck 	if (error) {
166763b6971aSCornelia Huck 		cleanup_glue_dir(dev, new_parent_kobj);
16688a82472fSCornelia Huck 		put_device(new_parent);
16698a82472fSCornelia Huck 		goto out;
16708a82472fSCornelia Huck 	}
16718a82472fSCornelia Huck 	old_parent = dev->parent;
16728a82472fSCornelia Huck 	dev->parent = new_parent;
16738a82472fSCornelia Huck 	if (old_parent)
1674f791b8c8SGreg Kroah-Hartman 		klist_remove(&dev->p->knode_parent);
16750d358f22SYinghai Lu 	if (new_parent) {
1676f791b8c8SGreg Kroah-Hartman 		klist_add_tail(&dev->p->knode_parent,
1677f791b8c8SGreg Kroah-Hartman 			       &new_parent->p->klist_children);
16780d358f22SYinghai Lu 		set_dev_node(dev, dev_to_node(new_parent));
16790d358f22SYinghai Lu 	}
16800d358f22SYinghai Lu 
16818a82472fSCornelia Huck 	if (!dev->class)
16828a82472fSCornelia Huck 		goto out_put;
16838a82472fSCornelia Huck 	error = device_move_class_links(dev, old_parent, new_parent);
16848a82472fSCornelia Huck 	if (error) {
16858a82472fSCornelia Huck 		/* We ignore errors on cleanup since we're hosed anyway... */
16868a82472fSCornelia Huck 		device_move_class_links(dev, new_parent, old_parent);
16878a82472fSCornelia Huck 		if (!kobject_move(&dev->kobj, &old_parent->kobj)) {
1688c744aeaeSCornelia Huck 			if (new_parent)
1689f791b8c8SGreg Kroah-Hartman 				klist_remove(&dev->p->knode_parent);
16900d358f22SYinghai Lu 			dev->parent = old_parent;
16910d358f22SYinghai Lu 			if (old_parent) {
1692f791b8c8SGreg Kroah-Hartman 				klist_add_tail(&dev->p->knode_parent,
1693f791b8c8SGreg Kroah-Hartman 					       &old_parent->p->klist_children);
16940d358f22SYinghai Lu 				set_dev_node(dev, dev_to_node(old_parent));
16950d358f22SYinghai Lu 			}
16968a82472fSCornelia Huck 		}
169763b6971aSCornelia Huck 		cleanup_glue_dir(dev, new_parent_kobj);
16988a82472fSCornelia Huck 		put_device(new_parent);
16998a82472fSCornelia Huck 		goto out;
17008a82472fSCornelia Huck 	}
1701ffa6a705SCornelia Huck 	switch (dpm_order) {
1702ffa6a705SCornelia Huck 	case DPM_ORDER_NONE:
1703ffa6a705SCornelia Huck 		break;
1704ffa6a705SCornelia Huck 	case DPM_ORDER_DEV_AFTER_PARENT:
1705ffa6a705SCornelia Huck 		device_pm_move_after(dev, new_parent);
1706ffa6a705SCornelia Huck 		break;
1707ffa6a705SCornelia Huck 	case DPM_ORDER_PARENT_BEFORE_DEV:
1708ffa6a705SCornelia Huck 		device_pm_move_before(new_parent, dev);
1709ffa6a705SCornelia Huck 		break;
1710ffa6a705SCornelia Huck 	case DPM_ORDER_DEV_LAST:
1711ffa6a705SCornelia Huck 		device_pm_move_last(dev);
1712ffa6a705SCornelia Huck 		break;
1713ffa6a705SCornelia Huck 	}
17148a82472fSCornelia Huck out_put:
17158a82472fSCornelia Huck 	put_device(old_parent);
17168a82472fSCornelia Huck out:
1717ffa6a705SCornelia Huck 	device_pm_unlock();
17188a82472fSCornelia Huck 	put_device(dev);
17198a82472fSCornelia Huck 	return error;
17208a82472fSCornelia Huck }
17218a82472fSCornelia Huck EXPORT_SYMBOL_GPL(device_move);
172237b0c020SGreg Kroah-Hartman 
172337b0c020SGreg Kroah-Hartman /**
172437b0c020SGreg Kroah-Hartman  * device_shutdown - call ->shutdown() on each device to shutdown.
172537b0c020SGreg Kroah-Hartman  */
172637b0c020SGreg Kroah-Hartman void device_shutdown(void)
172737b0c020SGreg Kroah-Hartman {
17286245838fSHugh Daschbach 	struct device *dev;
172937b0c020SGreg Kroah-Hartman 
17306245838fSHugh Daschbach 	spin_lock(&devices_kset->list_lock);
17316245838fSHugh Daschbach 	/*
17326245838fSHugh Daschbach 	 * Walk the devices list backward, shutting down each in turn.
17336245838fSHugh Daschbach 	 * Beware that device unplug events may also start pulling
17346245838fSHugh Daschbach 	 * devices offline, even as the system is shutting down.
17356245838fSHugh Daschbach 	 */
17366245838fSHugh Daschbach 	while (!list_empty(&devices_kset->list)) {
17376245838fSHugh Daschbach 		dev = list_entry(devices_kset->list.prev, struct device,
17386245838fSHugh Daschbach 				kobj.entry);
17396245838fSHugh Daschbach 		get_device(dev);
17406245838fSHugh Daschbach 		/*
17416245838fSHugh Daschbach 		 * Make sure the device is off the kset list, in the
17426245838fSHugh Daschbach 		 * event that dev->*->shutdown() doesn't remove it.
17436245838fSHugh Daschbach 		 */
17446245838fSHugh Daschbach 		list_del_init(&dev->kobj.entry);
17456245838fSHugh Daschbach 		spin_unlock(&devices_kset->list_lock);
1746af8db150SPeter Chen 		/* Disable all device's runtime power management */
1747af8db150SPeter Chen 		pm_runtime_disable(dev);
17486245838fSHugh Daschbach 
174937b0c020SGreg Kroah-Hartman 		if (dev->bus && dev->bus->shutdown) {
175037b0c020SGreg Kroah-Hartman 			dev_dbg(dev, "shutdown\n");
175137b0c020SGreg Kroah-Hartman 			dev->bus->shutdown(dev);
175237b0c020SGreg Kroah-Hartman 		} else if (dev->driver && dev->driver->shutdown) {
175337b0c020SGreg Kroah-Hartman 			dev_dbg(dev, "shutdown\n");
175437b0c020SGreg Kroah-Hartman 			dev->driver->shutdown(dev);
175537b0c020SGreg Kroah-Hartman 		}
17566245838fSHugh Daschbach 		put_device(dev);
17576245838fSHugh Daschbach 
17586245838fSHugh Daschbach 		spin_lock(&devices_kset->list_lock);
175937b0c020SGreg Kroah-Hartman 	}
17606245838fSHugh Daschbach 	spin_unlock(&devices_kset->list_lock);
1761401097eaSShaohua Li 	async_synchronize_full();
176237b0c020SGreg Kroah-Hartman }
176399bcf217SJoe Perches 
176499bcf217SJoe Perches /*
176599bcf217SJoe Perches  * Device logging functions
176699bcf217SJoe Perches  */
176799bcf217SJoe Perches 
176899bcf217SJoe Perches #ifdef CONFIG_PRINTK
176999bcf217SJoe Perches 
1770cbc46635SJoe Perches int __dev_printk(const char *level, const struct device *dev,
177199bcf217SJoe Perches 		 struct va_format *vaf)
177299bcf217SJoe Perches {
177399bcf217SJoe Perches 	if (!dev)
177499bcf217SJoe Perches 		return printk("%s(NULL device *): %pV", level, vaf);
177599bcf217SJoe Perches 
177699bcf217SJoe Perches 	return printk("%s%s %s: %pV",
177799bcf217SJoe Perches 		      level, dev_driver_string(dev), dev_name(dev), vaf);
177899bcf217SJoe Perches }
1779cbc46635SJoe Perches EXPORT_SYMBOL(__dev_printk);
178099bcf217SJoe Perches 
178199bcf217SJoe Perches int dev_printk(const char *level, const struct device *dev,
178299bcf217SJoe Perches 	       const char *fmt, ...)
178399bcf217SJoe Perches {
178499bcf217SJoe Perches 	struct va_format vaf;
178599bcf217SJoe Perches 	va_list args;
178699bcf217SJoe Perches 	int r;
178799bcf217SJoe Perches 
178899bcf217SJoe Perches 	va_start(args, fmt);
178999bcf217SJoe Perches 
179099bcf217SJoe Perches 	vaf.fmt = fmt;
179199bcf217SJoe Perches 	vaf.va = &args;
179299bcf217SJoe Perches 
179399bcf217SJoe Perches 	r = __dev_printk(level, dev, &vaf);
179499bcf217SJoe Perches 	va_end(args);
179599bcf217SJoe Perches 
179699bcf217SJoe Perches 	return r;
179799bcf217SJoe Perches }
179899bcf217SJoe Perches EXPORT_SYMBOL(dev_printk);
179999bcf217SJoe Perches 
180099bcf217SJoe Perches #define define_dev_printk_level(func, kern_level)		\
180199bcf217SJoe Perches int func(const struct device *dev, const char *fmt, ...)	\
180299bcf217SJoe Perches {								\
180399bcf217SJoe Perches 	struct va_format vaf;					\
180499bcf217SJoe Perches 	va_list args;						\
180599bcf217SJoe Perches 	int r;							\
180699bcf217SJoe Perches 								\
180799bcf217SJoe Perches 	va_start(args, fmt);					\
180899bcf217SJoe Perches 								\
180999bcf217SJoe Perches 	vaf.fmt = fmt;						\
181099bcf217SJoe Perches 	vaf.va = &args;						\
181199bcf217SJoe Perches 								\
181299bcf217SJoe Perches 	r = __dev_printk(kern_level, dev, &vaf);		\
181399bcf217SJoe Perches 	va_end(args);						\
181499bcf217SJoe Perches 								\
181599bcf217SJoe Perches 	return r;						\
181699bcf217SJoe Perches }								\
181799bcf217SJoe Perches EXPORT_SYMBOL(func);
181899bcf217SJoe Perches 
181999bcf217SJoe Perches define_dev_printk_level(dev_emerg, KERN_EMERG);
182099bcf217SJoe Perches define_dev_printk_level(dev_alert, KERN_ALERT);
182199bcf217SJoe Perches define_dev_printk_level(dev_crit, KERN_CRIT);
182299bcf217SJoe Perches define_dev_printk_level(dev_err, KERN_ERR);
182399bcf217SJoe Perches define_dev_printk_level(dev_warn, KERN_WARNING);
182499bcf217SJoe Perches define_dev_printk_level(dev_notice, KERN_NOTICE);
182599bcf217SJoe Perches define_dev_printk_level(_dev_info, KERN_INFO);
182699bcf217SJoe Perches 
182799bcf217SJoe Perches #endif
1828