xref: /openbmc/linux/drivers/base/core.c (revision a4dbd674)
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>
236188e10dSMatthew Wilcox #include <linux/semaphore.h>
24f75b1c60SDave Young #include <linux/mutex.h>
25401097eaSShaohua Li #include <linux/async.h>
261da177e4SLinus Torvalds 
271da177e4SLinus Torvalds #include "base.h"
281da177e4SLinus Torvalds #include "power/power.h"
291da177e4SLinus Torvalds 
301da177e4SLinus Torvalds int (*platform_notify)(struct device *dev) = NULL;
311da177e4SLinus Torvalds int (*platform_notify_remove)(struct device *dev) = NULL;
32e105b8bfSDan Williams static struct kobject *dev_kobj;
33e105b8bfSDan Williams struct kobject *sysfs_dev_char_kobj;
34e105b8bfSDan Williams struct kobject *sysfs_dev_block_kobj;
351da177e4SLinus Torvalds 
364e886c29SGreg Kroah-Hartman #ifdef CONFIG_BLOCK
374e886c29SGreg Kroah-Hartman static inline int device_is_not_partition(struct device *dev)
384e886c29SGreg Kroah-Hartman {
394e886c29SGreg Kroah-Hartman 	return !(dev->type == &part_type);
404e886c29SGreg Kroah-Hartman }
414e886c29SGreg Kroah-Hartman #else
424e886c29SGreg Kroah-Hartman static inline int device_is_not_partition(struct device *dev)
434e886c29SGreg Kroah-Hartman {
444e886c29SGreg Kroah-Hartman 	return 1;
454e886c29SGreg Kroah-Hartman }
464e886c29SGreg Kroah-Hartman #endif
471da177e4SLinus Torvalds 
483e95637aSAlan Stern /**
493e95637aSAlan Stern  * dev_driver_string - Return a device's driver name, if at all possible
503e95637aSAlan Stern  * @dev: struct device to get the name of
513e95637aSAlan Stern  *
523e95637aSAlan Stern  * Will return the device's driver's name if it is bound to a device.  If
533e95637aSAlan Stern  * the device is not bound to a device, it will return the name of the bus
543e95637aSAlan Stern  * it is attached to.  If it is not attached to a bus either, an empty
553e95637aSAlan Stern  * string will be returned.
563e95637aSAlan Stern  */
57bf9ca69fSJean Delvare const char *dev_driver_string(const struct device *dev)
583e95637aSAlan Stern {
593e95637aSAlan Stern 	return dev->driver ? dev->driver->name :
60a456b702SJean Delvare 			(dev->bus ? dev->bus->name :
61a456b702SJean Delvare 			(dev->class ? dev->class->name : ""));
623e95637aSAlan Stern }
63310a922dSMatthew Wilcox EXPORT_SYMBOL(dev_driver_string);
643e95637aSAlan Stern 
651da177e4SLinus Torvalds #define to_dev(obj) container_of(obj, struct device, kobj)
661da177e4SLinus Torvalds #define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr)
671da177e4SLinus Torvalds 
684a3ad20cSGreg Kroah-Hartman static ssize_t dev_attr_show(struct kobject *kobj, struct attribute *attr,
694a3ad20cSGreg Kroah-Hartman 			     char *buf)
701da177e4SLinus Torvalds {
711da177e4SLinus Torvalds 	struct device_attribute *dev_attr = to_dev_attr(attr);
721da177e4SLinus Torvalds 	struct device *dev = to_dev(kobj);
734a0c20bfSDmitry Torokhov 	ssize_t ret = -EIO;
741da177e4SLinus Torvalds 
751da177e4SLinus Torvalds 	if (dev_attr->show)
7654b6f35cSYani Ioannou 		ret = dev_attr->show(dev, dev_attr, buf);
77815d2d50SAndrew Morton 	if (ret >= (ssize_t)PAGE_SIZE) {
78815d2d50SAndrew Morton 		print_symbol("dev_attr_show: %s returned bad count\n",
79815d2d50SAndrew Morton 				(unsigned long)dev_attr->show);
80815d2d50SAndrew Morton 	}
811da177e4SLinus Torvalds 	return ret;
821da177e4SLinus Torvalds }
831da177e4SLinus Torvalds 
844a3ad20cSGreg Kroah-Hartman static ssize_t dev_attr_store(struct kobject *kobj, struct attribute *attr,
851da177e4SLinus Torvalds 			      const char *buf, size_t count)
861da177e4SLinus Torvalds {
871da177e4SLinus Torvalds 	struct device_attribute *dev_attr = to_dev_attr(attr);
881da177e4SLinus Torvalds 	struct device *dev = to_dev(kobj);
894a0c20bfSDmitry Torokhov 	ssize_t ret = -EIO;
901da177e4SLinus Torvalds 
911da177e4SLinus Torvalds 	if (dev_attr->store)
9254b6f35cSYani Ioannou 		ret = dev_attr->store(dev, dev_attr, buf, count);
931da177e4SLinus Torvalds 	return ret;
941da177e4SLinus Torvalds }
951da177e4SLinus Torvalds 
961da177e4SLinus Torvalds static struct sysfs_ops dev_sysfs_ops = {
971da177e4SLinus Torvalds 	.show	= dev_attr_show,
981da177e4SLinus Torvalds 	.store	= dev_attr_store,
991da177e4SLinus Torvalds };
1001da177e4SLinus Torvalds 
1011da177e4SLinus Torvalds 
1021da177e4SLinus Torvalds /**
1031da177e4SLinus Torvalds  *	device_release - free device structure.
1041da177e4SLinus Torvalds  *	@kobj:	device's kobject.
1051da177e4SLinus Torvalds  *
1061da177e4SLinus Torvalds  *	This is called once the reference count for the object
1071da177e4SLinus Torvalds  *	reaches 0. We forward the call to the device's release
1081da177e4SLinus Torvalds  *	method, which should handle actually freeing the structure.
1091da177e4SLinus Torvalds  */
1101da177e4SLinus Torvalds static void device_release(struct kobject *kobj)
1111da177e4SLinus Torvalds {
1121da177e4SLinus Torvalds 	struct device *dev = to_dev(kobj);
113fb069a5dSGreg Kroah-Hartman 	struct device_private *p = dev->p;
1141da177e4SLinus Torvalds 
1151da177e4SLinus Torvalds 	if (dev->release)
1161da177e4SLinus Torvalds 		dev->release(dev);
117f9f852dfSKay Sievers 	else if (dev->type && dev->type->release)
118f9f852dfSKay Sievers 		dev->type->release(dev);
1192620efefSGreg Kroah-Hartman 	else if (dev->class && dev->class->dev_release)
1202620efefSGreg Kroah-Hartman 		dev->class->dev_release(dev);
121f810a5cfSArjan van de Ven 	else
122f810a5cfSArjan van de Ven 		WARN(1, KERN_ERR "Device '%s' does not have a release() "
1234a3ad20cSGreg Kroah-Hartman 			"function, it is broken and must be fixed.\n",
1241e0b2cf9SKay Sievers 			dev_name(dev));
125fb069a5dSGreg Kroah-Hartman 	kfree(p);
1261da177e4SLinus Torvalds }
1271da177e4SLinus Torvalds 
1288f4afc41SGreg Kroah-Hartman static struct kobj_type device_ktype = {
1291da177e4SLinus Torvalds 	.release	= device_release,
1301da177e4SLinus Torvalds 	.sysfs_ops	= &dev_sysfs_ops,
1311da177e4SLinus Torvalds };
1321da177e4SLinus Torvalds 
1331da177e4SLinus Torvalds 
134312c004dSKay Sievers static int dev_uevent_filter(struct kset *kset, struct kobject *kobj)
1351da177e4SLinus Torvalds {
1361da177e4SLinus Torvalds 	struct kobj_type *ktype = get_ktype(kobj);
1371da177e4SLinus Torvalds 
1388f4afc41SGreg Kroah-Hartman 	if (ktype == &device_ktype) {
1391da177e4SLinus Torvalds 		struct device *dev = to_dev(kobj);
1401da177e4SLinus Torvalds 		if (dev->bus)
1411da177e4SLinus Torvalds 			return 1;
14223681e47SGreg Kroah-Hartman 		if (dev->class)
14323681e47SGreg Kroah-Hartman 			return 1;
1441da177e4SLinus Torvalds 	}
1451da177e4SLinus Torvalds 	return 0;
1461da177e4SLinus Torvalds }
1471da177e4SLinus Torvalds 
148312c004dSKay Sievers static const char *dev_uevent_name(struct kset *kset, struct kobject *kobj)
1491da177e4SLinus Torvalds {
1501da177e4SLinus Torvalds 	struct device *dev = to_dev(kobj);
1511da177e4SLinus Torvalds 
15223681e47SGreg Kroah-Hartman 	if (dev->bus)
1531da177e4SLinus Torvalds 		return dev->bus->name;
15423681e47SGreg Kroah-Hartman 	if (dev->class)
15523681e47SGreg Kroah-Hartman 		return dev->class->name;
15623681e47SGreg Kroah-Hartman 	return NULL;
1571da177e4SLinus Torvalds }
1581da177e4SLinus Torvalds 
1597eff2e7aSKay Sievers static int dev_uevent(struct kset *kset, struct kobject *kobj,
1607eff2e7aSKay Sievers 		      struct kobj_uevent_env *env)
1611da177e4SLinus Torvalds {
1621da177e4SLinus Torvalds 	struct device *dev = to_dev(kobj);
1631da177e4SLinus Torvalds 	int retval = 0;
1641da177e4SLinus Torvalds 
1656fcf53acSKay Sievers 	/* add device node properties if present */
16623681e47SGreg Kroah-Hartman 	if (MAJOR(dev->devt)) {
1676fcf53acSKay Sievers 		const char *tmp;
1686fcf53acSKay Sievers 		const char *name;
1696fcf53acSKay Sievers 
1707eff2e7aSKay Sievers 		add_uevent_var(env, "MAJOR=%u", MAJOR(dev->devt));
1717eff2e7aSKay Sievers 		add_uevent_var(env, "MINOR=%u", MINOR(dev->devt));
1726fcf53acSKay Sievers 		name = device_get_nodename(dev, &tmp);
1736fcf53acSKay Sievers 		if (name) {
1746fcf53acSKay Sievers 			add_uevent_var(env, "DEVNAME=%s", name);
1756fcf53acSKay Sievers 			kfree(tmp);
1766fcf53acSKay Sievers 		}
17723681e47SGreg Kroah-Hartman 	}
17823681e47SGreg Kroah-Hartman 
179414264f9SKay Sievers 	if (dev->type && dev->type->name)
1807eff2e7aSKay Sievers 		add_uevent_var(env, "DEVTYPE=%s", dev->type->name);
181414264f9SKay Sievers 
182239378f1SKay Sievers 	if (dev->driver)
1837eff2e7aSKay Sievers 		add_uevent_var(env, "DRIVER=%s", dev->driver->name);
184239378f1SKay Sievers 
185a87cb2acSKay Sievers #ifdef CONFIG_SYSFS_DEPRECATED
186239378f1SKay Sievers 	if (dev->class) {
187239378f1SKay Sievers 		struct device *parent = dev->parent;
188239378f1SKay Sievers 
189239378f1SKay Sievers 		/* find first bus device in parent chain */
190239378f1SKay Sievers 		while (parent && !parent->bus)
191239378f1SKay Sievers 			parent = parent->parent;
192239378f1SKay Sievers 		if (parent && parent->bus) {
193239378f1SKay Sievers 			const char *path;
194239378f1SKay Sievers 
195239378f1SKay Sievers 			path = kobject_get_path(&parent->kobj, GFP_KERNEL);
1962c7afd12SKay Sievers 			if (path) {
1977eff2e7aSKay Sievers 				add_uevent_var(env, "PHYSDEVPATH=%s", path);
198239378f1SKay Sievers 				kfree(path);
1992c7afd12SKay Sievers 			}
200239378f1SKay Sievers 
2017eff2e7aSKay Sievers 			add_uevent_var(env, "PHYSDEVBUS=%s", parent->bus->name);
202239378f1SKay Sievers 
203239378f1SKay Sievers 			if (parent->driver)
2047eff2e7aSKay Sievers 				add_uevent_var(env, "PHYSDEVDRIVER=%s",
2057eff2e7aSKay Sievers 					       parent->driver->name);
206239378f1SKay Sievers 		}
207239378f1SKay Sievers 	} else if (dev->bus) {
2087eff2e7aSKay Sievers 		add_uevent_var(env, "PHYSDEVBUS=%s", dev->bus->name);
209239378f1SKay Sievers 
210239378f1SKay Sievers 		if (dev->driver)
2114a3ad20cSGreg Kroah-Hartman 			add_uevent_var(env, "PHYSDEVDRIVER=%s",
2124a3ad20cSGreg Kroah-Hartman 				       dev->driver->name);
213d81d9d6bSKay Sievers 	}
214239378f1SKay Sievers #endif
2151da177e4SLinus Torvalds 
2161da177e4SLinus Torvalds 	/* have the bus specific function add its stuff */
2177eff2e7aSKay Sievers 	if (dev->bus && dev->bus->uevent) {
2187eff2e7aSKay Sievers 		retval = dev->bus->uevent(dev, env);
219f9f852dfSKay Sievers 		if (retval)
2207dc72b28SGreg Kroah-Hartman 			pr_debug("device: '%s': %s: bus uevent() returned %d\n",
2211e0b2cf9SKay Sievers 				 dev_name(dev), __func__, retval);
2221da177e4SLinus Torvalds 	}
2231da177e4SLinus Torvalds 
2242620efefSGreg Kroah-Hartman 	/* have the class specific function add its stuff */
2257eff2e7aSKay Sievers 	if (dev->class && dev->class->dev_uevent) {
2267eff2e7aSKay Sievers 		retval = dev->class->dev_uevent(dev, env);
227f9f852dfSKay Sievers 		if (retval)
2287dc72b28SGreg Kroah-Hartman 			pr_debug("device: '%s': %s: class uevent() "
2291e0b2cf9SKay Sievers 				 "returned %d\n", dev_name(dev),
2302b3a302aSHarvey Harrison 				 __func__, retval);
2312620efefSGreg Kroah-Hartman 	}
232f9f852dfSKay Sievers 
233f9f852dfSKay Sievers 	/* have the device type specific fuction add its stuff */
2347eff2e7aSKay Sievers 	if (dev->type && dev->type->uevent) {
2357eff2e7aSKay Sievers 		retval = dev->type->uevent(dev, env);
236f9f852dfSKay Sievers 		if (retval)
2377dc72b28SGreg Kroah-Hartman 			pr_debug("device: '%s': %s: dev_type uevent() "
2381e0b2cf9SKay Sievers 				 "returned %d\n", dev_name(dev),
2392b3a302aSHarvey Harrison 				 __func__, retval);
2402620efefSGreg Kroah-Hartman 	}
2412620efefSGreg Kroah-Hartman 
2421da177e4SLinus Torvalds 	return retval;
2431da177e4SLinus Torvalds }
2441da177e4SLinus Torvalds 
245312c004dSKay Sievers static struct kset_uevent_ops device_uevent_ops = {
246312c004dSKay Sievers 	.filter =	dev_uevent_filter,
247312c004dSKay Sievers 	.name =		dev_uevent_name,
248312c004dSKay Sievers 	.uevent =	dev_uevent,
2491da177e4SLinus Torvalds };
2501da177e4SLinus Torvalds 
25116574dccSKay Sievers static ssize_t show_uevent(struct device *dev, struct device_attribute *attr,
25216574dccSKay Sievers 			   char *buf)
25316574dccSKay Sievers {
25416574dccSKay Sievers 	struct kobject *top_kobj;
25516574dccSKay Sievers 	struct kset *kset;
2567eff2e7aSKay Sievers 	struct kobj_uevent_env *env = NULL;
25716574dccSKay Sievers 	int i;
25816574dccSKay Sievers 	size_t count = 0;
25916574dccSKay Sievers 	int retval;
26016574dccSKay Sievers 
26116574dccSKay Sievers 	/* search the kset, the device belongs to */
26216574dccSKay Sievers 	top_kobj = &dev->kobj;
2635c5daf65SKay Sievers 	while (!top_kobj->kset && top_kobj->parent)
26416574dccSKay Sievers 		top_kobj = top_kobj->parent;
26516574dccSKay Sievers 	if (!top_kobj->kset)
26616574dccSKay Sievers 		goto out;
2675c5daf65SKay Sievers 
26816574dccSKay Sievers 	kset = top_kobj->kset;
26916574dccSKay Sievers 	if (!kset->uevent_ops || !kset->uevent_ops->uevent)
27016574dccSKay Sievers 		goto out;
27116574dccSKay Sievers 
27216574dccSKay Sievers 	/* respect filter */
27316574dccSKay Sievers 	if (kset->uevent_ops && kset->uevent_ops->filter)
27416574dccSKay Sievers 		if (!kset->uevent_ops->filter(kset, &dev->kobj))
27516574dccSKay Sievers 			goto out;
27616574dccSKay Sievers 
2777eff2e7aSKay Sievers 	env = kzalloc(sizeof(struct kobj_uevent_env), GFP_KERNEL);
2787eff2e7aSKay Sievers 	if (!env)
279c7308c81SGreg Kroah-Hartman 		return -ENOMEM;
280c7308c81SGreg Kroah-Hartman 
28116574dccSKay Sievers 	/* let the kset specific function add its keys */
2827eff2e7aSKay Sievers 	retval = kset->uevent_ops->uevent(kset, &dev->kobj, env);
28316574dccSKay Sievers 	if (retval)
28416574dccSKay Sievers 		goto out;
28516574dccSKay Sievers 
28616574dccSKay Sievers 	/* copy keys to file */
2877eff2e7aSKay Sievers 	for (i = 0; i < env->envp_idx; i++)
2887eff2e7aSKay Sievers 		count += sprintf(&buf[count], "%s\n", env->envp[i]);
28916574dccSKay Sievers out:
2907eff2e7aSKay Sievers 	kfree(env);
29116574dccSKay Sievers 	return count;
29216574dccSKay Sievers }
29316574dccSKay Sievers 
294a7fd6706SKay Sievers static ssize_t store_uevent(struct device *dev, struct device_attribute *attr,
295a7fd6706SKay Sievers 			    const char *buf, size_t count)
296a7fd6706SKay Sievers {
29760a96a59SKay Sievers 	enum kobject_action action;
29860a96a59SKay Sievers 
2995c5daf65SKay Sievers 	if (kobject_action_type(buf, count, &action) == 0) {
30060a96a59SKay Sievers 		kobject_uevent(&dev->kobj, action);
30160a96a59SKay Sievers 		goto out;
30260a96a59SKay Sievers 	}
30360a96a59SKay Sievers 
30422af74f3SKay Sievers 	dev_err(dev, "uevent: unsupported action-string; this will "
30560a96a59SKay Sievers 		     "be ignored in a future kernel version\n");
306312c004dSKay Sievers 	kobject_uevent(&dev->kobj, KOBJ_ADD);
30760a96a59SKay Sievers out:
308a7fd6706SKay Sievers 	return count;
309a7fd6706SKay Sievers }
310a7fd6706SKay Sievers 
311ad6a1e1cSTejun Heo static struct device_attribute uevent_attr =
312ad6a1e1cSTejun Heo 	__ATTR(uevent, S_IRUGO | S_IWUSR, show_uevent, store_uevent);
313ad6a1e1cSTejun Heo 
314621a1672SDmitry Torokhov static int device_add_attributes(struct device *dev,
315621a1672SDmitry Torokhov 				 struct device_attribute *attrs)
316de0ff00dSGreg Kroah-Hartman {
317de0ff00dSGreg Kroah-Hartman 	int error = 0;
318621a1672SDmitry Torokhov 	int i;
319de0ff00dSGreg Kroah-Hartman 
320621a1672SDmitry Torokhov 	if (attrs) {
321621a1672SDmitry Torokhov 		for (i = 0; attr_name(attrs[i]); i++) {
322621a1672SDmitry Torokhov 			error = device_create_file(dev, &attrs[i]);
323621a1672SDmitry Torokhov 			if (error)
324621a1672SDmitry Torokhov 				break;
325621a1672SDmitry Torokhov 		}
326621a1672SDmitry Torokhov 		if (error)
327de0ff00dSGreg Kroah-Hartman 			while (--i >= 0)
328621a1672SDmitry Torokhov 				device_remove_file(dev, &attrs[i]);
329de0ff00dSGreg Kroah-Hartman 	}
330de0ff00dSGreg Kroah-Hartman 	return error;
331de0ff00dSGreg Kroah-Hartman }
332de0ff00dSGreg Kroah-Hartman 
333621a1672SDmitry Torokhov static void device_remove_attributes(struct device *dev,
334621a1672SDmitry Torokhov 				     struct device_attribute *attrs)
335de0ff00dSGreg Kroah-Hartman {
336de0ff00dSGreg Kroah-Hartman 	int i;
337621a1672SDmitry Torokhov 
338621a1672SDmitry Torokhov 	if (attrs)
339621a1672SDmitry Torokhov 		for (i = 0; attr_name(attrs[i]); i++)
340621a1672SDmitry Torokhov 			device_remove_file(dev, &attrs[i]);
341621a1672SDmitry Torokhov }
342621a1672SDmitry Torokhov 
343621a1672SDmitry Torokhov static int device_add_groups(struct device *dev,
344a4dbd674SDavid Brownell 			     const struct attribute_group **groups)
345621a1672SDmitry Torokhov {
346621a1672SDmitry Torokhov 	int error = 0;
347621a1672SDmitry Torokhov 	int i;
348621a1672SDmitry Torokhov 
349621a1672SDmitry Torokhov 	if (groups) {
350621a1672SDmitry Torokhov 		for (i = 0; groups[i]; i++) {
351621a1672SDmitry Torokhov 			error = sysfs_create_group(&dev->kobj, groups[i]);
352621a1672SDmitry Torokhov 			if (error) {
353621a1672SDmitry Torokhov 				while (--i >= 0)
3544a3ad20cSGreg Kroah-Hartman 					sysfs_remove_group(&dev->kobj,
3554a3ad20cSGreg Kroah-Hartman 							   groups[i]);
356621a1672SDmitry Torokhov 				break;
357de0ff00dSGreg Kroah-Hartman 			}
358de0ff00dSGreg Kroah-Hartman 		}
359de0ff00dSGreg Kroah-Hartman 	}
360621a1672SDmitry Torokhov 	return error;
361621a1672SDmitry Torokhov }
362621a1672SDmitry Torokhov 
363621a1672SDmitry Torokhov static void device_remove_groups(struct device *dev,
364a4dbd674SDavid Brownell 				 const struct attribute_group **groups)
365621a1672SDmitry Torokhov {
366621a1672SDmitry Torokhov 	int i;
367621a1672SDmitry Torokhov 
368621a1672SDmitry Torokhov 	if (groups)
369621a1672SDmitry Torokhov 		for (i = 0; groups[i]; i++)
370621a1672SDmitry Torokhov 			sysfs_remove_group(&dev->kobj, groups[i]);
371621a1672SDmitry Torokhov }
372de0ff00dSGreg Kroah-Hartman 
3732620efefSGreg Kroah-Hartman static int device_add_attrs(struct device *dev)
3742620efefSGreg Kroah-Hartman {
3752620efefSGreg Kroah-Hartman 	struct class *class = dev->class;
376f9f852dfSKay Sievers 	struct device_type *type = dev->type;
377621a1672SDmitry Torokhov 	int error;
3782620efefSGreg Kroah-Hartman 
379621a1672SDmitry Torokhov 	if (class) {
380621a1672SDmitry Torokhov 		error = device_add_attributes(dev, class->dev_attrs);
3812620efefSGreg Kroah-Hartman 		if (error)
382621a1672SDmitry Torokhov 			return error;
383f9f852dfSKay Sievers 	}
384f9f852dfSKay Sievers 
385621a1672SDmitry Torokhov 	if (type) {
386621a1672SDmitry Torokhov 		error = device_add_groups(dev, type->groups);
387f9f852dfSKay Sievers 		if (error)
388621a1672SDmitry Torokhov 			goto err_remove_class_attrs;
389f9f852dfSKay Sievers 	}
390621a1672SDmitry Torokhov 
391621a1672SDmitry Torokhov 	error = device_add_groups(dev, dev->groups);
392f9f852dfSKay Sievers 	if (error)
393621a1672SDmitry Torokhov 		goto err_remove_type_groups;
394621a1672SDmitry Torokhov 
395621a1672SDmitry Torokhov 	return 0;
396621a1672SDmitry Torokhov 
397621a1672SDmitry Torokhov  err_remove_type_groups:
398621a1672SDmitry Torokhov 	if (type)
399621a1672SDmitry Torokhov 		device_remove_groups(dev, type->groups);
400621a1672SDmitry Torokhov  err_remove_class_attrs:
401621a1672SDmitry Torokhov 	if (class)
402621a1672SDmitry Torokhov 		device_remove_attributes(dev, class->dev_attrs);
403f9f852dfSKay Sievers 
4042620efefSGreg Kroah-Hartman 	return error;
4052620efefSGreg Kroah-Hartman }
4062620efefSGreg Kroah-Hartman 
4072620efefSGreg Kroah-Hartman static void device_remove_attrs(struct device *dev)
4082620efefSGreg Kroah-Hartman {
4092620efefSGreg Kroah-Hartman 	struct class *class = dev->class;
410f9f852dfSKay Sievers 	struct device_type *type = dev->type;
4112620efefSGreg Kroah-Hartman 
412621a1672SDmitry Torokhov 	device_remove_groups(dev, dev->groups);
413f9f852dfSKay Sievers 
414621a1672SDmitry Torokhov 	if (type)
415621a1672SDmitry Torokhov 		device_remove_groups(dev, type->groups);
416621a1672SDmitry Torokhov 
417621a1672SDmitry Torokhov 	if (class)
418621a1672SDmitry Torokhov 		device_remove_attributes(dev, class->dev_attrs);
4192620efefSGreg Kroah-Hartman }
4202620efefSGreg Kroah-Hartman 
4212620efefSGreg Kroah-Hartman 
42223681e47SGreg Kroah-Hartman static ssize_t show_dev(struct device *dev, struct device_attribute *attr,
42323681e47SGreg Kroah-Hartman 			char *buf)
42423681e47SGreg Kroah-Hartman {
42523681e47SGreg Kroah-Hartman 	return print_dev_t(buf, dev->devt);
42623681e47SGreg Kroah-Hartman }
42723681e47SGreg Kroah-Hartman 
428ad6a1e1cSTejun Heo static struct device_attribute devt_attr =
429ad6a1e1cSTejun Heo 	__ATTR(dev, S_IRUGO, show_dev, NULL);
430ad6a1e1cSTejun Heo 
431881c6cfdSGreg Kroah-Hartman /* kset to create /sys/devices/  */
432881c6cfdSGreg Kroah-Hartman struct kset *devices_kset;
4331da177e4SLinus Torvalds 
4341da177e4SLinus Torvalds /**
4351da177e4SLinus Torvalds  * device_create_file - create sysfs attribute file for device.
4361da177e4SLinus Torvalds  * @dev: device.
4371da177e4SLinus Torvalds  * @attr: device attribute descriptor.
4381da177e4SLinus Torvalds  */
4391da177e4SLinus Torvalds int device_create_file(struct device *dev, struct device_attribute *attr)
4401da177e4SLinus Torvalds {
4411da177e4SLinus Torvalds 	int error = 0;
4420c98b19fSCornelia Huck 	if (dev)
4431da177e4SLinus Torvalds 		error = sysfs_create_file(&dev->kobj, &attr->attr);
4441da177e4SLinus Torvalds 	return error;
4451da177e4SLinus Torvalds }
4461da177e4SLinus Torvalds 
4471da177e4SLinus Torvalds /**
4481da177e4SLinus Torvalds  * device_remove_file - remove sysfs attribute file.
4491da177e4SLinus Torvalds  * @dev: device.
4501da177e4SLinus Torvalds  * @attr: device attribute descriptor.
4511da177e4SLinus Torvalds  */
4521da177e4SLinus Torvalds void device_remove_file(struct device *dev, struct device_attribute *attr)
4531da177e4SLinus Torvalds {
4540c98b19fSCornelia Huck 	if (dev)
4551da177e4SLinus Torvalds 		sysfs_remove_file(&dev->kobj, &attr->attr);
4561da177e4SLinus Torvalds }
4571da177e4SLinus Torvalds 
4582589f188SGreg Kroah-Hartman /**
4592589f188SGreg Kroah-Hartman  * device_create_bin_file - create sysfs binary attribute file for device.
4602589f188SGreg Kroah-Hartman  * @dev: device.
4612589f188SGreg Kroah-Hartman  * @attr: device binary attribute descriptor.
4622589f188SGreg Kroah-Hartman  */
4632589f188SGreg Kroah-Hartman int device_create_bin_file(struct device *dev, struct bin_attribute *attr)
4642589f188SGreg Kroah-Hartman {
4652589f188SGreg Kroah-Hartman 	int error = -EINVAL;
4662589f188SGreg Kroah-Hartman 	if (dev)
4672589f188SGreg Kroah-Hartman 		error = sysfs_create_bin_file(&dev->kobj, attr);
4682589f188SGreg Kroah-Hartman 	return error;
4692589f188SGreg Kroah-Hartman }
4702589f188SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(device_create_bin_file);
4712589f188SGreg Kroah-Hartman 
4722589f188SGreg Kroah-Hartman /**
4732589f188SGreg Kroah-Hartman  * device_remove_bin_file - remove sysfs binary attribute file
4742589f188SGreg Kroah-Hartman  * @dev: device.
4752589f188SGreg Kroah-Hartman  * @attr: device binary attribute descriptor.
4762589f188SGreg Kroah-Hartman  */
4772589f188SGreg Kroah-Hartman void device_remove_bin_file(struct device *dev, struct bin_attribute *attr)
4782589f188SGreg Kroah-Hartman {
4792589f188SGreg Kroah-Hartman 	if (dev)
4802589f188SGreg Kroah-Hartman 		sysfs_remove_bin_file(&dev->kobj, attr);
4812589f188SGreg Kroah-Hartman }
4822589f188SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(device_remove_bin_file);
4832589f188SGreg Kroah-Hartman 
484d9a9cdfbSAlan Stern /**
485523ded71SAlan Stern  * device_schedule_callback_owner - helper to schedule a callback for a device
486d9a9cdfbSAlan Stern  * @dev: device.
487d9a9cdfbSAlan Stern  * @func: callback function to invoke later.
488523ded71SAlan Stern  * @owner: module owning the callback routine
489d9a9cdfbSAlan Stern  *
490d9a9cdfbSAlan Stern  * Attribute methods must not unregister themselves or their parent device
491d9a9cdfbSAlan Stern  * (which would amount to the same thing).  Attempts to do so will deadlock,
492d9a9cdfbSAlan Stern  * since unregistration is mutually exclusive with driver callbacks.
493d9a9cdfbSAlan Stern  *
494d9a9cdfbSAlan Stern  * Instead methods can call this routine, which will attempt to allocate
495d9a9cdfbSAlan Stern  * and schedule a workqueue request to call back @func with @dev as its
496d9a9cdfbSAlan Stern  * argument in the workqueue's process context.  @dev will be pinned until
497d9a9cdfbSAlan Stern  * @func returns.
498d9a9cdfbSAlan Stern  *
499523ded71SAlan Stern  * This routine is usually called via the inline device_schedule_callback(),
500523ded71SAlan Stern  * which automatically sets @owner to THIS_MODULE.
501523ded71SAlan Stern  *
502d9a9cdfbSAlan Stern  * Returns 0 if the request was submitted, -ENOMEM if storage could not
503523ded71SAlan Stern  * be allocated, -ENODEV if a reference to @owner isn't available.
504d9a9cdfbSAlan Stern  *
505d9a9cdfbSAlan Stern  * NOTE: This routine won't work if CONFIG_SYSFS isn't set!  It uses an
506d9a9cdfbSAlan Stern  * underlying sysfs routine (since it is intended for use by attribute
507d9a9cdfbSAlan Stern  * methods), and if sysfs isn't available you'll get nothing but -ENOSYS.
508d9a9cdfbSAlan Stern  */
509523ded71SAlan Stern int device_schedule_callback_owner(struct device *dev,
510523ded71SAlan Stern 		void (*func)(struct device *), struct module *owner)
511d9a9cdfbSAlan Stern {
512d9a9cdfbSAlan Stern 	return sysfs_schedule_callback(&dev->kobj,
513523ded71SAlan Stern 			(void (*)(void *)) func, dev, owner);
514d9a9cdfbSAlan Stern }
515523ded71SAlan Stern EXPORT_SYMBOL_GPL(device_schedule_callback_owner);
516d9a9cdfbSAlan Stern 
51734bb61f9SJames Bottomley static void klist_children_get(struct klist_node *n)
51834bb61f9SJames Bottomley {
519f791b8c8SGreg Kroah-Hartman 	struct device_private *p = to_device_private_parent(n);
520f791b8c8SGreg Kroah-Hartman 	struct device *dev = p->device;
52134bb61f9SJames Bottomley 
52234bb61f9SJames Bottomley 	get_device(dev);
52334bb61f9SJames Bottomley }
52434bb61f9SJames Bottomley 
52534bb61f9SJames Bottomley static void klist_children_put(struct klist_node *n)
52634bb61f9SJames Bottomley {
527f791b8c8SGreg Kroah-Hartman 	struct device_private *p = to_device_private_parent(n);
528f791b8c8SGreg Kroah-Hartman 	struct device *dev = p->device;
52934bb61f9SJames Bottomley 
53034bb61f9SJames Bottomley 	put_device(dev);
53134bb61f9SJames Bottomley }
53234bb61f9SJames Bottomley 
5331da177e4SLinus Torvalds /**
5341da177e4SLinus Torvalds  * device_initialize - init device structure.
5351da177e4SLinus Torvalds  * @dev: device.
5361da177e4SLinus Torvalds  *
5375739411aSCornelia Huck  * This prepares the device for use by other layers by initializing
5385739411aSCornelia Huck  * its fields.
5391da177e4SLinus Torvalds  * It is the first half of device_register(), if called by
5405739411aSCornelia Huck  * that function, though it can also be called separately, so one
5415739411aSCornelia Huck  * may use @dev's fields. In particular, get_device()/put_device()
5425739411aSCornelia Huck  * may be used for reference counting of @dev after calling this
5435739411aSCornelia Huck  * function.
5445739411aSCornelia Huck  *
5455739411aSCornelia Huck  * NOTE: Use put_device() to give up your reference instead of freeing
5465739411aSCornelia Huck  * @dev directly once you have called this function.
5471da177e4SLinus Torvalds  */
5481da177e4SLinus Torvalds void device_initialize(struct device *dev)
5491da177e4SLinus Torvalds {
550881c6cfdSGreg Kroah-Hartman 	dev->kobj.kset = devices_kset;
551f9cb074bSGreg Kroah-Hartman 	kobject_init(&dev->kobj, &device_ktype);
5521da177e4SLinus Torvalds 	INIT_LIST_HEAD(&dev->dma_pools);
553af70316aSmochel@digitalimplant.org 	init_MUTEX(&dev->sem);
5549ac7849eSTejun Heo 	spin_lock_init(&dev->devres_lock);
5559ac7849eSTejun Heo 	INIT_LIST_HEAD(&dev->devres_head);
5560ac85241SDavid Brownell 	device_init_wakeup(dev, 0);
5573b98aeafSAlan Stern 	device_pm_init(dev);
55887348136SChristoph Hellwig 	set_dev_node(dev, -1);
5591da177e4SLinus Torvalds }
5601da177e4SLinus Torvalds 
56140fa5422SGreg Kroah-Hartman #ifdef CONFIG_SYSFS_DEPRECATED
562c744aeaeSCornelia Huck static struct kobject *get_device_parent(struct device *dev,
563c744aeaeSCornelia Huck 					 struct device *parent)
56440fa5422SGreg Kroah-Hartman {
565da231fd5SKay Sievers 	/* class devices without a parent live in /sys/class/<classname>/ */
5663eb215deSDmitry Torokhov 	if (dev->class && (!parent || parent->class != dev->class))
5671fbfee6cSGreg Kroah-Hartman 		return &dev->class->p->class_subsys.kobj;
568da231fd5SKay Sievers 	/* all other devices keep their parent */
56940fa5422SGreg Kroah-Hartman 	else if (parent)
570c744aeaeSCornelia Huck 		return &parent->kobj;
57140fa5422SGreg Kroah-Hartman 
572c744aeaeSCornelia Huck 	return NULL;
57340fa5422SGreg Kroah-Hartman }
574da231fd5SKay Sievers 
575da231fd5SKay Sievers static inline void cleanup_device_parent(struct device *dev) {}
57663b6971aSCornelia Huck static inline void cleanup_glue_dir(struct device *dev,
57763b6971aSCornelia Huck 				    struct kobject *glue_dir) {}
57840fa5422SGreg Kroah-Hartman #else
579c744aeaeSCornelia Huck static struct kobject *virtual_device_parent(struct device *dev)
580f0ee61a6SGreg Kroah-Hartman {
581f0ee61a6SGreg Kroah-Hartman 	static struct kobject *virtual_dir = NULL;
582f0ee61a6SGreg Kroah-Hartman 
583f0ee61a6SGreg Kroah-Hartman 	if (!virtual_dir)
5844ff6abffSGreg Kroah-Hartman 		virtual_dir = kobject_create_and_add("virtual",
585881c6cfdSGreg Kroah-Hartman 						     &devices_kset->kobj);
586f0ee61a6SGreg Kroah-Hartman 
58786406245SKay Sievers 	return virtual_dir;
588f0ee61a6SGreg Kroah-Hartman }
589f0ee61a6SGreg Kroah-Hartman 
590c744aeaeSCornelia Huck static struct kobject *get_device_parent(struct device *dev,
591c744aeaeSCornelia Huck 					 struct device *parent)
59240fa5422SGreg Kroah-Hartman {
59343968d2fSGreg Kroah-Hartman 	int retval;
59443968d2fSGreg Kroah-Hartman 
59586406245SKay Sievers 	if (dev->class) {
59686406245SKay Sievers 		struct kobject *kobj = NULL;
59786406245SKay Sievers 		struct kobject *parent_kobj;
59886406245SKay Sievers 		struct kobject *k;
59986406245SKay Sievers 
60086406245SKay Sievers 		/*
60186406245SKay Sievers 		 * If we have no parent, we live in "virtual".
6020f4dafc0SKay Sievers 		 * Class-devices with a non class-device as parent, live
6030f4dafc0SKay Sievers 		 * in a "glue" directory to prevent namespace collisions.
60486406245SKay Sievers 		 */
60586406245SKay Sievers 		if (parent == NULL)
60686406245SKay Sievers 			parent_kobj = virtual_device_parent(dev);
60786406245SKay Sievers 		else if (parent->class)
60886406245SKay Sievers 			return &parent->kobj;
60986406245SKay Sievers 		else
61086406245SKay Sievers 			parent_kobj = &parent->kobj;
61186406245SKay Sievers 
61286406245SKay Sievers 		/* find our class-directory at the parent and reference it */
6137c71448bSGreg Kroah-Hartman 		spin_lock(&dev->class->p->class_dirs.list_lock);
6147c71448bSGreg Kroah-Hartman 		list_for_each_entry(k, &dev->class->p->class_dirs.list, entry)
61586406245SKay Sievers 			if (k->parent == parent_kobj) {
61686406245SKay Sievers 				kobj = kobject_get(k);
61786406245SKay Sievers 				break;
61886406245SKay Sievers 			}
6197c71448bSGreg Kroah-Hartman 		spin_unlock(&dev->class->p->class_dirs.list_lock);
62086406245SKay Sievers 		if (kobj)
62186406245SKay Sievers 			return kobj;
62286406245SKay Sievers 
62386406245SKay Sievers 		/* or create a new class-directory at the parent device */
62443968d2fSGreg Kroah-Hartman 		k = kobject_create();
62543968d2fSGreg Kroah-Hartman 		if (!k)
62643968d2fSGreg Kroah-Hartman 			return NULL;
6277c71448bSGreg Kroah-Hartman 		k->kset = &dev->class->p->class_dirs;
628b2d6db58SGreg Kroah-Hartman 		retval = kobject_add(k, parent_kobj, "%s", dev->class->name);
62943968d2fSGreg Kroah-Hartman 		if (retval < 0) {
63043968d2fSGreg Kroah-Hartman 			kobject_put(k);
63143968d2fSGreg Kroah-Hartman 			return NULL;
63243968d2fSGreg Kroah-Hartman 		}
6330f4dafc0SKay Sievers 		/* do not emit an uevent for this simple "glue" directory */
63443968d2fSGreg Kroah-Hartman 		return k;
63586406245SKay Sievers 	}
63686406245SKay Sievers 
63786406245SKay Sievers 	if (parent)
638c744aeaeSCornelia Huck 		return &parent->kobj;
639c744aeaeSCornelia Huck 	return NULL;
640c744aeaeSCornelia Huck }
641da231fd5SKay Sievers 
64263b6971aSCornelia Huck static void cleanup_glue_dir(struct device *dev, struct kobject *glue_dir)
643da231fd5SKay Sievers {
6440f4dafc0SKay Sievers 	/* see if we live in a "glue" directory */
645c1fe539aSCornelia Huck 	if (!glue_dir || !dev->class ||
6467c71448bSGreg Kroah-Hartman 	    glue_dir->kset != &dev->class->p->class_dirs)
647da231fd5SKay Sievers 		return;
648da231fd5SKay Sievers 
6490f4dafc0SKay Sievers 	kobject_put(glue_dir);
650da231fd5SKay Sievers }
65163b6971aSCornelia Huck 
65263b6971aSCornelia Huck static void cleanup_device_parent(struct device *dev)
65363b6971aSCornelia Huck {
65463b6971aSCornelia Huck 	cleanup_glue_dir(dev, dev->kobj.parent);
65563b6971aSCornelia Huck }
656c744aeaeSCornelia Huck #endif
65786406245SKay Sievers 
65863b6971aSCornelia Huck static void setup_parent(struct device *dev, struct device *parent)
659c744aeaeSCornelia Huck {
660c744aeaeSCornelia Huck 	struct kobject *kobj;
661c744aeaeSCornelia Huck 	kobj = get_device_parent(dev, parent);
662c744aeaeSCornelia Huck 	if (kobj)
663c744aeaeSCornelia Huck 		dev->kobj.parent = kobj;
66440fa5422SGreg Kroah-Hartman }
66540fa5422SGreg Kroah-Hartman 
6662ee97cafSCornelia Huck static int device_add_class_symlinks(struct device *dev)
6672ee97cafSCornelia Huck {
6682ee97cafSCornelia Huck 	int error;
6692ee97cafSCornelia Huck 
6702ee97cafSCornelia Huck 	if (!dev->class)
6712ee97cafSCornelia Huck 		return 0;
672da231fd5SKay Sievers 
6731fbfee6cSGreg Kroah-Hartman 	error = sysfs_create_link(&dev->kobj,
6741fbfee6cSGreg Kroah-Hartman 				  &dev->class->p->class_subsys.kobj,
6752ee97cafSCornelia Huck 				  "subsystem");
6762ee97cafSCornelia Huck 	if (error)
6772ee97cafSCornelia Huck 		goto out;
678da231fd5SKay Sievers 
679da231fd5SKay Sievers #ifdef CONFIG_SYSFS_DEPRECATED
680da231fd5SKay Sievers 	/* stacked class devices need a symlink in the class directory */
6811fbfee6cSGreg Kroah-Hartman 	if (dev->kobj.parent != &dev->class->p->class_subsys.kobj &&
6824e886c29SGreg Kroah-Hartman 	    device_is_not_partition(dev)) {
6831fbfee6cSGreg Kroah-Hartman 		error = sysfs_create_link(&dev->class->p->class_subsys.kobj,
6841e0b2cf9SKay Sievers 					  &dev->kobj, dev_name(dev));
6852ee97cafSCornelia Huck 		if (error)
6862ee97cafSCornelia Huck 			goto out_subsys;
6872ee97cafSCornelia Huck 	}
688da231fd5SKay Sievers 
6894e886c29SGreg Kroah-Hartman 	if (dev->parent && device_is_not_partition(dev)) {
6904f01a757SDmitry Torokhov 		struct device *parent = dev->parent;
6914f01a757SDmitry Torokhov 		char *class_name;
6924f01a757SDmitry Torokhov 
6934f01a757SDmitry Torokhov 		/*
694da231fd5SKay Sievers 		 * stacked class devices have the 'device' link
695da231fd5SKay Sievers 		 * pointing to the bus device instead of the parent
6964f01a757SDmitry Torokhov 		 */
6974f01a757SDmitry Torokhov 		while (parent->class && !parent->bus && parent->parent)
6984f01a757SDmitry Torokhov 			parent = parent->parent;
6994f01a757SDmitry Torokhov 
7004f01a757SDmitry Torokhov 		error = sysfs_create_link(&dev->kobj,
7014f01a757SDmitry Torokhov 					  &parent->kobj,
7022ee97cafSCornelia Huck 					  "device");
7032ee97cafSCornelia Huck 		if (error)
7042ee97cafSCornelia Huck 			goto out_busid;
7054f01a757SDmitry Torokhov 
7064f01a757SDmitry Torokhov 		class_name = make_class_name(dev->class->name,
7072ee97cafSCornelia Huck 						&dev->kobj);
7082ee97cafSCornelia Huck 		if (class_name)
7092ee97cafSCornelia Huck 			error = sysfs_create_link(&dev->parent->kobj,
7102ee97cafSCornelia Huck 						&dev->kobj, class_name);
7112ee97cafSCornelia Huck 		kfree(class_name);
7122ee97cafSCornelia Huck 		if (error)
7132ee97cafSCornelia Huck 			goto out_device;
7142ee97cafSCornelia Huck 	}
715da231fd5SKay Sievers 	return 0;
716da231fd5SKay Sievers 
717da231fd5SKay Sievers out_device:
7184e886c29SGreg Kroah-Hartman 	if (dev->parent && device_is_not_partition(dev))
719da231fd5SKay Sievers 		sysfs_remove_link(&dev->kobj, "device");
720da231fd5SKay Sievers out_busid:
7211fbfee6cSGreg Kroah-Hartman 	if (dev->kobj.parent != &dev->class->p->class_subsys.kobj &&
7224e886c29SGreg Kroah-Hartman 	    device_is_not_partition(dev))
7231fbfee6cSGreg Kroah-Hartman 		sysfs_remove_link(&dev->class->p->class_subsys.kobj,
7241e0b2cf9SKay Sievers 				  dev_name(dev));
7254f01a757SDmitry Torokhov #else
726da231fd5SKay Sievers 	/* link in the class directory pointing to the device */
7271fbfee6cSGreg Kroah-Hartman 	error = sysfs_create_link(&dev->class->p->class_subsys.kobj,
7281e0b2cf9SKay Sievers 				  &dev->kobj, dev_name(dev));
729da231fd5SKay Sievers 	if (error)
730da231fd5SKay Sievers 		goto out_subsys;
731da231fd5SKay Sievers 
7324e886c29SGreg Kroah-Hartman 	if (dev->parent && device_is_not_partition(dev)) {
7334f01a757SDmitry Torokhov 		error = sysfs_create_link(&dev->kobj, &dev->parent->kobj,
7344f01a757SDmitry Torokhov 					  "device");
7354f01a757SDmitry Torokhov 		if (error)
7364f01a757SDmitry Torokhov 			goto out_busid;
7372ee97cafSCornelia Huck 	}
7382ee97cafSCornelia Huck 	return 0;
7392ee97cafSCornelia Huck 
7402ee97cafSCornelia Huck out_busid:
7411e0b2cf9SKay Sievers 	sysfs_remove_link(&dev->class->p->class_subsys.kobj, dev_name(dev));
742da231fd5SKay Sievers #endif
743da231fd5SKay Sievers 
7442ee97cafSCornelia Huck out_subsys:
7452ee97cafSCornelia Huck 	sysfs_remove_link(&dev->kobj, "subsystem");
7462ee97cafSCornelia Huck out:
7472ee97cafSCornelia Huck 	return error;
7482ee97cafSCornelia Huck }
7492ee97cafSCornelia Huck 
7502ee97cafSCornelia Huck static void device_remove_class_symlinks(struct device *dev)
7512ee97cafSCornelia Huck {
7522ee97cafSCornelia Huck 	if (!dev->class)
7532ee97cafSCornelia Huck 		return;
754da231fd5SKay Sievers 
7552ee97cafSCornelia Huck #ifdef CONFIG_SYSFS_DEPRECATED
7564e886c29SGreg Kroah-Hartman 	if (dev->parent && device_is_not_partition(dev)) {
7572ee97cafSCornelia Huck 		char *class_name;
7582ee97cafSCornelia Huck 
7592ee97cafSCornelia Huck 		class_name = make_class_name(dev->class->name, &dev->kobj);
7602ee97cafSCornelia Huck 		if (class_name) {
7612ee97cafSCornelia Huck 			sysfs_remove_link(&dev->parent->kobj, class_name);
7622ee97cafSCornelia Huck 			kfree(class_name);
7632ee97cafSCornelia Huck 		}
7642ee97cafSCornelia Huck 		sysfs_remove_link(&dev->kobj, "device");
7652ee97cafSCornelia Huck 	}
766da231fd5SKay Sievers 
7671fbfee6cSGreg Kroah-Hartman 	if (dev->kobj.parent != &dev->class->p->class_subsys.kobj &&
7684e886c29SGreg Kroah-Hartman 	    device_is_not_partition(dev))
7691fbfee6cSGreg Kroah-Hartman 		sysfs_remove_link(&dev->class->p->class_subsys.kobj,
7701e0b2cf9SKay Sievers 				  dev_name(dev));
771da231fd5SKay Sievers #else
7724e886c29SGreg Kroah-Hartman 	if (dev->parent && device_is_not_partition(dev))
773da231fd5SKay Sievers 		sysfs_remove_link(&dev->kobj, "device");
774da231fd5SKay Sievers 
7751e0b2cf9SKay Sievers 	sysfs_remove_link(&dev->class->p->class_subsys.kobj, dev_name(dev));
776da231fd5SKay Sievers #endif
777da231fd5SKay Sievers 
7782ee97cafSCornelia Huck 	sysfs_remove_link(&dev->kobj, "subsystem");
7792ee97cafSCornelia Huck }
7802ee97cafSCornelia Huck 
7811da177e4SLinus Torvalds /**
782413c239fSStephen Rothwell  * dev_set_name - set a device name
783413c239fSStephen Rothwell  * @dev: device
78446232366SRandy Dunlap  * @fmt: format string for the device's name
785413c239fSStephen Rothwell  */
786413c239fSStephen Rothwell int dev_set_name(struct device *dev, const char *fmt, ...)
787413c239fSStephen Rothwell {
788413c239fSStephen Rothwell 	va_list vargs;
7891fa5ae85SKay Sievers 	int err;
790413c239fSStephen Rothwell 
791413c239fSStephen Rothwell 	va_start(vargs, fmt);
7921fa5ae85SKay Sievers 	err = kobject_set_name_vargs(&dev->kobj, fmt, vargs);
793413c239fSStephen Rothwell 	va_end(vargs);
7941fa5ae85SKay Sievers 	return err;
795413c239fSStephen Rothwell }
796413c239fSStephen Rothwell EXPORT_SYMBOL_GPL(dev_set_name);
797413c239fSStephen Rothwell 
798413c239fSStephen Rothwell /**
799e105b8bfSDan Williams  * device_to_dev_kobj - select a /sys/dev/ directory for the device
800e105b8bfSDan Williams  * @dev: device
801e105b8bfSDan Williams  *
802e105b8bfSDan Williams  * By default we select char/ for new entries.  Setting class->dev_obj
803e105b8bfSDan Williams  * to NULL prevents an entry from being created.  class->dev_kobj must
804e105b8bfSDan Williams  * be set (or cleared) before any devices are registered to the class
805e105b8bfSDan Williams  * otherwise device_create_sys_dev_entry() and
806e105b8bfSDan Williams  * device_remove_sys_dev_entry() will disagree about the the presence
807e105b8bfSDan Williams  * of the link.
808e105b8bfSDan Williams  */
809e105b8bfSDan Williams static struct kobject *device_to_dev_kobj(struct device *dev)
810e105b8bfSDan Williams {
811e105b8bfSDan Williams 	struct kobject *kobj;
812e105b8bfSDan Williams 
813e105b8bfSDan Williams 	if (dev->class)
814e105b8bfSDan Williams 		kobj = dev->class->dev_kobj;
815e105b8bfSDan Williams 	else
816e105b8bfSDan Williams 		kobj = sysfs_dev_char_kobj;
817e105b8bfSDan Williams 
818e105b8bfSDan Williams 	return kobj;
819e105b8bfSDan Williams }
820e105b8bfSDan Williams 
821e105b8bfSDan Williams static int device_create_sys_dev_entry(struct device *dev)
822e105b8bfSDan Williams {
823e105b8bfSDan Williams 	struct kobject *kobj = device_to_dev_kobj(dev);
824e105b8bfSDan Williams 	int error = 0;
825e105b8bfSDan Williams 	char devt_str[15];
826e105b8bfSDan Williams 
827e105b8bfSDan Williams 	if (kobj) {
828e105b8bfSDan Williams 		format_dev_t(devt_str, dev->devt);
829e105b8bfSDan Williams 		error = sysfs_create_link(kobj, &dev->kobj, devt_str);
830e105b8bfSDan Williams 	}
831e105b8bfSDan Williams 
832e105b8bfSDan Williams 	return error;
833e105b8bfSDan Williams }
834e105b8bfSDan Williams 
835e105b8bfSDan Williams static void device_remove_sys_dev_entry(struct device *dev)
836e105b8bfSDan Williams {
837e105b8bfSDan Williams 	struct kobject *kobj = device_to_dev_kobj(dev);
838e105b8bfSDan Williams 	char devt_str[15];
839e105b8bfSDan Williams 
840e105b8bfSDan Williams 	if (kobj) {
841e105b8bfSDan Williams 		format_dev_t(devt_str, dev->devt);
842e105b8bfSDan Williams 		sysfs_remove_link(kobj, devt_str);
843e105b8bfSDan Williams 	}
844e105b8bfSDan Williams }
845e105b8bfSDan Williams 
846b4028437SGreg Kroah-Hartman int device_private_init(struct device *dev)
847b4028437SGreg Kroah-Hartman {
848b4028437SGreg Kroah-Hartman 	dev->p = kzalloc(sizeof(*dev->p), GFP_KERNEL);
849b4028437SGreg Kroah-Hartman 	if (!dev->p)
850b4028437SGreg Kroah-Hartman 		return -ENOMEM;
851b4028437SGreg Kroah-Hartman 	dev->p->device = dev;
852b4028437SGreg Kroah-Hartman 	klist_init(&dev->p->klist_children, klist_children_get,
853b4028437SGreg Kroah-Hartman 		   klist_children_put);
854b4028437SGreg Kroah-Hartman 	return 0;
855b4028437SGreg Kroah-Hartman }
856b4028437SGreg Kroah-Hartman 
857e105b8bfSDan Williams /**
8581da177e4SLinus Torvalds  * device_add - add device to device hierarchy.
8591da177e4SLinus Torvalds  * @dev: device.
8601da177e4SLinus Torvalds  *
8611da177e4SLinus Torvalds  * This is part 2 of device_register(), though may be called
8621da177e4SLinus Torvalds  * separately _iff_ device_initialize() has been called separately.
8631da177e4SLinus Torvalds  *
8645739411aSCornelia Huck  * This adds @dev to the kobject hierarchy via kobject_add(), adds it
8651da177e4SLinus Torvalds  * to the global and sibling lists for the device, then
8661da177e4SLinus Torvalds  * adds it to the other relevant subsystems of the driver model.
8675739411aSCornelia Huck  *
8685739411aSCornelia Huck  * NOTE: _Never_ directly free @dev after calling this function, even
8695739411aSCornelia Huck  * if it returned an error! Always use put_device() to give up your
8705739411aSCornelia Huck  * reference instead.
8711da177e4SLinus Torvalds  */
8721da177e4SLinus Torvalds int device_add(struct device *dev)
8731da177e4SLinus Torvalds {
8741da177e4SLinus Torvalds 	struct device *parent = NULL;
875c47ed219SGreg Kroah-Hartman 	struct class_interface *class_intf;
876c906a48aSGreg Kroah-Hartman 	int error = -EINVAL;
877775b64d2SRafael J. Wysocki 
8781da177e4SLinus Torvalds 	dev = get_device(dev);
879c906a48aSGreg Kroah-Hartman 	if (!dev)
880c906a48aSGreg Kroah-Hartman 		goto done;
881c906a48aSGreg Kroah-Hartman 
882fb069a5dSGreg Kroah-Hartman 	if (!dev->p) {
883b4028437SGreg Kroah-Hartman 		error = device_private_init(dev);
884b4028437SGreg Kroah-Hartman 		if (error)
885fb069a5dSGreg Kroah-Hartman 			goto done;
886fb069a5dSGreg Kroah-Hartman 	}
887fb069a5dSGreg Kroah-Hartman 
8881fa5ae85SKay Sievers 	/*
8891fa5ae85SKay Sievers 	 * for statically allocated devices, which should all be converted
8901fa5ae85SKay Sievers 	 * some day, we need to initialize the name. We prevent reading back
8911fa5ae85SKay Sievers 	 * the name, and force the use of dev_name()
8921fa5ae85SKay Sievers 	 */
8931fa5ae85SKay Sievers 	if (dev->init_name) {
894acc0e90fSGreg Kroah-Hartman 		dev_set_name(dev, "%s", dev->init_name);
8951fa5ae85SKay Sievers 		dev->init_name = NULL;
8961fa5ae85SKay Sievers 	}
897c906a48aSGreg Kroah-Hartman 
8981fa5ae85SKay Sievers 	if (!dev_name(dev))
8995c8563d7SKay Sievers 		goto name_error;
9001da177e4SLinus Torvalds 
9011e0b2cf9SKay Sievers 	pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
902c205ef48SGreg Kroah-Hartman 
9031da177e4SLinus Torvalds 	parent = get_device(dev->parent);
90463b6971aSCornelia Huck 	setup_parent(dev, parent);
9051da177e4SLinus Torvalds 
9060d358f22SYinghai Lu 	/* use parent numa_node */
9070d358f22SYinghai Lu 	if (parent)
9080d358f22SYinghai Lu 		set_dev_node(dev, dev_to_node(parent));
9090d358f22SYinghai Lu 
9101da177e4SLinus Torvalds 	/* first, register with generic layer. */
9118a577ffcSKay Sievers 	/* we require the name to be set before, and pass NULL */
9128a577ffcSKay Sievers 	error = kobject_add(&dev->kobj, dev->kobj.parent, NULL);
91340fa5422SGreg Kroah-Hartman 	if (error)
9141da177e4SLinus Torvalds 		goto Error;
915a7fd6706SKay Sievers 
91637022644SBrian Walsh 	/* notify platform of device entry */
91737022644SBrian Walsh 	if (platform_notify)
91837022644SBrian Walsh 		platform_notify(dev);
91937022644SBrian Walsh 
920ad6a1e1cSTejun Heo 	error = device_create_file(dev, &uevent_attr);
921a306eea4SCornelia Huck 	if (error)
922a306eea4SCornelia Huck 		goto attrError;
923a7fd6706SKay Sievers 
92423681e47SGreg Kroah-Hartman 	if (MAJOR(dev->devt)) {
925ad6a1e1cSTejun Heo 		error = device_create_file(dev, &devt_attr);
926ad6a1e1cSTejun Heo 		if (error)
927a306eea4SCornelia Huck 			goto ueventattrError;
928e105b8bfSDan Williams 
929e105b8bfSDan Williams 		error = device_create_sys_dev_entry(dev);
930e105b8bfSDan Williams 		if (error)
931e105b8bfSDan Williams 			goto devtattrError;
93223681e47SGreg Kroah-Hartman 	}
93323681e47SGreg Kroah-Hartman 
9342ee97cafSCornelia Huck 	error = device_add_class_symlinks(dev);
9352ee97cafSCornelia Huck 	if (error)
9362ee97cafSCornelia Huck 		goto SymlinkError;
937dc0afa83SCornelia Huck 	error = device_add_attrs(dev);
938dc0afa83SCornelia Huck 	if (error)
9392620efefSGreg Kroah-Hartman 		goto AttrsError;
940dc0afa83SCornelia Huck 	error = bus_add_device(dev);
941dc0afa83SCornelia Huck 	if (error)
9421da177e4SLinus Torvalds 		goto BusError;
9433b98aeafSAlan Stern 	error = dpm_sysfs_add(dev);
94457eee3d2SRafael J. Wysocki 	if (error)
9453b98aeafSAlan Stern 		goto DPMError;
9463b98aeafSAlan Stern 	device_pm_add(dev);
947ec0676eeSAlan Stern 
948ec0676eeSAlan Stern 	/* Notify clients of device addition.  This call must come
949ec0676eeSAlan Stern 	 * after dpm_sysf_add() and before kobject_uevent().
950ec0676eeSAlan Stern 	 */
951ec0676eeSAlan Stern 	if (dev->bus)
952ec0676eeSAlan Stern 		blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
953ec0676eeSAlan Stern 					     BUS_NOTIFY_ADD_DEVICE, dev);
954ec0676eeSAlan Stern 
95553877d06SKay Sievers 	kobject_uevent(&dev->kobj, KOBJ_ADD);
9562023c610SAlan Stern 	bus_probe_device(dev);
9571da177e4SLinus Torvalds 	if (parent)
958f791b8c8SGreg Kroah-Hartman 		klist_add_tail(&dev->p->knode_parent,
959f791b8c8SGreg Kroah-Hartman 			       &parent->p->klist_children);
9601da177e4SLinus Torvalds 
9615d9fd169SGreg Kroah-Hartman 	if (dev->class) {
962f75b1c60SDave Young 		mutex_lock(&dev->class->p->class_mutex);
963c47ed219SGreg Kroah-Hartman 		/* tie the class to the device */
9645a3ceb86STejun Heo 		klist_add_tail(&dev->knode_class,
9655a3ceb86STejun Heo 			       &dev->class->p->class_devices);
966c47ed219SGreg Kroah-Hartman 
967c47ed219SGreg Kroah-Hartman 		/* notify any interfaces that the device is here */
968184f1f77SGreg Kroah-Hartman 		list_for_each_entry(class_intf,
969184f1f77SGreg Kroah-Hartman 				    &dev->class->p->class_interfaces, node)
970c47ed219SGreg Kroah-Hartman 			if (class_intf->add_dev)
971c47ed219SGreg Kroah-Hartman 				class_intf->add_dev(dev, class_intf);
972f75b1c60SDave Young 		mutex_unlock(&dev->class->p->class_mutex);
9735d9fd169SGreg Kroah-Hartman 	}
974c906a48aSGreg Kroah-Hartman done:
9751da177e4SLinus Torvalds 	put_device(dev);
9761da177e4SLinus Torvalds 	return error;
9773b98aeafSAlan Stern  DPMError:
97857eee3d2SRafael J. Wysocki 	bus_remove_device(dev);
97957eee3d2SRafael J. Wysocki  BusError:
9802620efefSGreg Kroah-Hartman 	device_remove_attrs(dev);
9812620efefSGreg Kroah-Hartman  AttrsError:
9822ee97cafSCornelia Huck 	device_remove_class_symlinks(dev);
9832ee97cafSCornelia Huck  SymlinkError:
984ad6a1e1cSTejun Heo 	if (MAJOR(dev->devt))
985e105b8bfSDan Williams 		device_remove_sys_dev_entry(dev);
986e105b8bfSDan Williams  devtattrError:
987e105b8bfSDan Williams 	if (MAJOR(dev->devt))
988ad6a1e1cSTejun Heo 		device_remove_file(dev, &devt_attr);
989a306eea4SCornelia Huck  ueventattrError:
990ad6a1e1cSTejun Heo 	device_remove_file(dev, &uevent_attr);
99123681e47SGreg Kroah-Hartman  attrError:
992312c004dSKay Sievers 	kobject_uevent(&dev->kobj, KOBJ_REMOVE);
9931da177e4SLinus Torvalds 	kobject_del(&dev->kobj);
9941da177e4SLinus Torvalds  Error:
99563b6971aSCornelia Huck 	cleanup_device_parent(dev);
9961da177e4SLinus Torvalds 	if (parent)
9971da177e4SLinus Torvalds 		put_device(parent);
9985c8563d7SKay Sievers name_error:
9995c8563d7SKay Sievers 	kfree(dev->p);
10005c8563d7SKay Sievers 	dev->p = NULL;
1001c906a48aSGreg Kroah-Hartman 	goto done;
10021da177e4SLinus Torvalds }
10031da177e4SLinus Torvalds 
10041da177e4SLinus Torvalds /**
10051da177e4SLinus Torvalds  * device_register - register a device with the system.
10061da177e4SLinus Torvalds  * @dev: pointer to the device structure
10071da177e4SLinus Torvalds  *
10081da177e4SLinus Torvalds  * This happens in two clean steps - initialize the device
10091da177e4SLinus Torvalds  * and add it to the system. The two steps can be called
10101da177e4SLinus Torvalds  * separately, but this is the easiest and most common.
10111da177e4SLinus Torvalds  * I.e. you should only call the two helpers separately if
10121da177e4SLinus Torvalds  * have a clearly defined need to use and refcount the device
10131da177e4SLinus Torvalds  * before it is added to the hierarchy.
10145739411aSCornelia Huck  *
10155739411aSCornelia Huck  * NOTE: _Never_ directly free @dev after calling this function, even
10165739411aSCornelia Huck  * if it returned an error! Always use put_device() to give up the
10175739411aSCornelia Huck  * reference initialized in this function instead.
10181da177e4SLinus Torvalds  */
10191da177e4SLinus Torvalds int device_register(struct device *dev)
10201da177e4SLinus Torvalds {
10211da177e4SLinus Torvalds 	device_initialize(dev);
10221da177e4SLinus Torvalds 	return device_add(dev);
10231da177e4SLinus Torvalds }
10241da177e4SLinus Torvalds 
10251da177e4SLinus Torvalds /**
10261da177e4SLinus Torvalds  * get_device - increment reference count for device.
10271da177e4SLinus Torvalds  * @dev: device.
10281da177e4SLinus Torvalds  *
10291da177e4SLinus Torvalds  * This simply forwards the call to kobject_get(), though
10301da177e4SLinus Torvalds  * we do take care to provide for the case that we get a NULL
10311da177e4SLinus Torvalds  * pointer passed in.
10321da177e4SLinus Torvalds  */
10331da177e4SLinus Torvalds struct device *get_device(struct device *dev)
10341da177e4SLinus Torvalds {
10351da177e4SLinus Torvalds 	return dev ? to_dev(kobject_get(&dev->kobj)) : NULL;
10361da177e4SLinus Torvalds }
10371da177e4SLinus Torvalds 
10381da177e4SLinus Torvalds /**
10391da177e4SLinus Torvalds  * put_device - decrement reference count.
10401da177e4SLinus Torvalds  * @dev: device in question.
10411da177e4SLinus Torvalds  */
10421da177e4SLinus Torvalds void put_device(struct device *dev)
10431da177e4SLinus Torvalds {
1044edfaa7c3SKay Sievers 	/* might_sleep(); */
10451da177e4SLinus Torvalds 	if (dev)
10461da177e4SLinus Torvalds 		kobject_put(&dev->kobj);
10471da177e4SLinus Torvalds }
10481da177e4SLinus Torvalds 
10491da177e4SLinus Torvalds /**
10501da177e4SLinus Torvalds  * device_del - delete device from system.
10511da177e4SLinus Torvalds  * @dev: device.
10521da177e4SLinus Torvalds  *
10531da177e4SLinus Torvalds  * This is the first part of the device unregistration
10541da177e4SLinus Torvalds  * sequence. This removes the device from the lists we control
10551da177e4SLinus Torvalds  * from here, has it removed from the other driver model
10561da177e4SLinus Torvalds  * subsystems it was added to in device_add(), and removes it
10571da177e4SLinus Torvalds  * from the kobject hierarchy.
10581da177e4SLinus Torvalds  *
10591da177e4SLinus Torvalds  * NOTE: this should be called manually _iff_ device_add() was
10601da177e4SLinus Torvalds  * also called manually.
10611da177e4SLinus Torvalds  */
10621da177e4SLinus Torvalds void device_del(struct device *dev)
10631da177e4SLinus Torvalds {
10641da177e4SLinus Torvalds 	struct device *parent = dev->parent;
1065c47ed219SGreg Kroah-Hartman 	struct class_interface *class_intf;
10661da177e4SLinus Torvalds 
1067ec0676eeSAlan Stern 	/* Notify clients of device removal.  This call must come
1068ec0676eeSAlan Stern 	 * before dpm_sysfs_remove().
1069ec0676eeSAlan Stern 	 */
1070ec0676eeSAlan Stern 	if (dev->bus)
1071ec0676eeSAlan Stern 		blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
1072ec0676eeSAlan Stern 					     BUS_NOTIFY_DEL_DEVICE, dev);
1073775b64d2SRafael J. Wysocki 	device_pm_remove(dev);
10743b98aeafSAlan Stern 	dpm_sysfs_remove(dev);
10751da177e4SLinus Torvalds 	if (parent)
1076f791b8c8SGreg Kroah-Hartman 		klist_del(&dev->p->knode_parent);
1077e105b8bfSDan Williams 	if (MAJOR(dev->devt)) {
1078e105b8bfSDan Williams 		device_remove_sys_dev_entry(dev);
1079ad6a1e1cSTejun Heo 		device_remove_file(dev, &devt_attr);
1080e105b8bfSDan Williams 	}
1081b9d9c82bSKay Sievers 	if (dev->class) {
1082da231fd5SKay Sievers 		device_remove_class_symlinks(dev);
108399ef3ef8SKay Sievers 
1084f75b1c60SDave Young 		mutex_lock(&dev->class->p->class_mutex);
1085c47ed219SGreg Kroah-Hartman 		/* notify any interfaces that the device is now gone */
1086184f1f77SGreg Kroah-Hartman 		list_for_each_entry(class_intf,
1087184f1f77SGreg Kroah-Hartman 				    &dev->class->p->class_interfaces, node)
1088c47ed219SGreg Kroah-Hartman 			if (class_intf->remove_dev)
1089c47ed219SGreg Kroah-Hartman 				class_intf->remove_dev(dev, class_intf);
1090c47ed219SGreg Kroah-Hartman 		/* remove the device from the class list */
10915a3ceb86STejun Heo 		klist_del(&dev->knode_class);
1092f75b1c60SDave Young 		mutex_unlock(&dev->class->p->class_mutex);
1093b9d9c82bSKay Sievers 	}
1094ad6a1e1cSTejun Heo 	device_remove_file(dev, &uevent_attr);
10952620efefSGreg Kroah-Hartman 	device_remove_attrs(dev);
109628953533SBenjamin Herrenschmidt 	bus_remove_device(dev);
10971da177e4SLinus Torvalds 
10982f8d16a9STejun Heo 	/*
10992f8d16a9STejun Heo 	 * Some platform devices are driven without driver attached
11002f8d16a9STejun Heo 	 * and managed resources may have been acquired.  Make sure
11012f8d16a9STejun Heo 	 * all resources are released.
11022f8d16a9STejun Heo 	 */
11032f8d16a9STejun Heo 	devres_release_all(dev);
11042f8d16a9STejun Heo 
11051da177e4SLinus Torvalds 	/* Notify the platform of the removal, in case they
11061da177e4SLinus Torvalds 	 * need to do anything...
11071da177e4SLinus Torvalds 	 */
11081da177e4SLinus Torvalds 	if (platform_notify_remove)
11091da177e4SLinus Torvalds 		platform_notify_remove(dev);
1110312c004dSKay Sievers 	kobject_uevent(&dev->kobj, KOBJ_REMOVE);
1111da231fd5SKay Sievers 	cleanup_device_parent(dev);
11121da177e4SLinus Torvalds 	kobject_del(&dev->kobj);
11131da177e4SLinus Torvalds 	put_device(parent);
11141da177e4SLinus Torvalds }
11151da177e4SLinus Torvalds 
11161da177e4SLinus Torvalds /**
11171da177e4SLinus Torvalds  * device_unregister - unregister device from system.
11181da177e4SLinus Torvalds  * @dev: device going away.
11191da177e4SLinus Torvalds  *
11201da177e4SLinus Torvalds  * We do this in two parts, like we do device_register(). First,
11211da177e4SLinus Torvalds  * we remove it from all the subsystems with device_del(), then
11221da177e4SLinus Torvalds  * we decrement the reference count via put_device(). If that
11231da177e4SLinus Torvalds  * is the final reference count, the device will be cleaned up
11241da177e4SLinus Torvalds  * via device_release() above. Otherwise, the structure will
11251da177e4SLinus Torvalds  * stick around until the final reference to the device is dropped.
11261da177e4SLinus Torvalds  */
11271da177e4SLinus Torvalds void device_unregister(struct device *dev)
11281da177e4SLinus Torvalds {
11291e0b2cf9SKay Sievers 	pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
11301da177e4SLinus Torvalds 	device_del(dev);
11311da177e4SLinus Torvalds 	put_device(dev);
11321da177e4SLinus Torvalds }
11331da177e4SLinus Torvalds 
113436239577Smochel@digitalimplant.org static struct device *next_device(struct klist_iter *i)
113536239577Smochel@digitalimplant.org {
113636239577Smochel@digitalimplant.org 	struct klist_node *n = klist_next(i);
1137f791b8c8SGreg Kroah-Hartman 	struct device *dev = NULL;
1138f791b8c8SGreg Kroah-Hartman 	struct device_private *p;
1139f791b8c8SGreg Kroah-Hartman 
1140f791b8c8SGreg Kroah-Hartman 	if (n) {
1141f791b8c8SGreg Kroah-Hartman 		p = to_device_private_parent(n);
1142f791b8c8SGreg Kroah-Hartman 		dev = p->device;
1143f791b8c8SGreg Kroah-Hartman 	}
1144f791b8c8SGreg Kroah-Hartman 	return dev;
114536239577Smochel@digitalimplant.org }
114636239577Smochel@digitalimplant.org 
11471da177e4SLinus Torvalds /**
11486fcf53acSKay Sievers  * device_get_nodename - path of device node file
11496fcf53acSKay Sievers  * @dev: device
11506fcf53acSKay Sievers  * @tmp: possibly allocated string
11516fcf53acSKay Sievers  *
11526fcf53acSKay Sievers  * Return the relative path of a possible device node.
11536fcf53acSKay Sievers  * Non-default names may need to allocate a memory to compose
11546fcf53acSKay Sievers  * a name. This memory is returned in tmp and needs to be
11556fcf53acSKay Sievers  * freed by the caller.
11566fcf53acSKay Sievers  */
11576fcf53acSKay Sievers const char *device_get_nodename(struct device *dev, const char **tmp)
11586fcf53acSKay Sievers {
11596fcf53acSKay Sievers 	char *s;
11606fcf53acSKay Sievers 
11616fcf53acSKay Sievers 	*tmp = NULL;
11626fcf53acSKay Sievers 
11636fcf53acSKay Sievers 	/* the device type may provide a specific name */
11646fcf53acSKay Sievers 	if (dev->type && dev->type->nodename)
11656fcf53acSKay Sievers 		*tmp = dev->type->nodename(dev);
11666fcf53acSKay Sievers 	if (*tmp)
11676fcf53acSKay Sievers 		return *tmp;
11686fcf53acSKay Sievers 
11696fcf53acSKay Sievers 	/* the class may provide a specific name */
11706fcf53acSKay Sievers 	if (dev->class && dev->class->nodename)
11716fcf53acSKay Sievers 		*tmp = dev->class->nodename(dev);
11726fcf53acSKay Sievers 	if (*tmp)
11736fcf53acSKay Sievers 		return *tmp;
11746fcf53acSKay Sievers 
11756fcf53acSKay Sievers 	/* return name without allocation, tmp == NULL */
11766fcf53acSKay Sievers 	if (strchr(dev_name(dev), '!') == NULL)
11776fcf53acSKay Sievers 		return dev_name(dev);
11786fcf53acSKay Sievers 
11796fcf53acSKay Sievers 	/* replace '!' in the name with '/' */
11806fcf53acSKay Sievers 	*tmp = kstrdup(dev_name(dev), GFP_KERNEL);
11816fcf53acSKay Sievers 	if (!*tmp)
11826fcf53acSKay Sievers 		return NULL;
11836fcf53acSKay Sievers 	while ((s = strchr(*tmp, '!')))
11846fcf53acSKay Sievers 		s[0] = '/';
11856fcf53acSKay Sievers 	return *tmp;
11866fcf53acSKay Sievers }
11876fcf53acSKay Sievers 
11886fcf53acSKay Sievers /**
11891da177e4SLinus Torvalds  * device_for_each_child - device child iterator.
1190c41455fbSRandy Dunlap  * @parent: parent struct device.
11911da177e4SLinus Torvalds  * @data: data for the callback.
11921da177e4SLinus Torvalds  * @fn: function to be called for each device.
11931da177e4SLinus Torvalds  *
1194c41455fbSRandy Dunlap  * Iterate over @parent's child devices, and call @fn for each,
11951da177e4SLinus Torvalds  * passing it @data.
11961da177e4SLinus Torvalds  *
11971da177e4SLinus Torvalds  * We check the return of @fn each time. If it returns anything
11981da177e4SLinus Torvalds  * other than 0, we break out and return that value.
11991da177e4SLinus Torvalds  */
120036239577Smochel@digitalimplant.org int device_for_each_child(struct device *parent, void *data,
12014a3ad20cSGreg Kroah-Hartman 			  int (*fn)(struct device *dev, void *data))
12021da177e4SLinus Torvalds {
120336239577Smochel@digitalimplant.org 	struct klist_iter i;
12041da177e4SLinus Torvalds 	struct device *child;
12051da177e4SLinus Torvalds 	int error = 0;
12061da177e4SLinus Torvalds 
1207014c90dbSGreg Kroah-Hartman 	if (!parent->p)
1208014c90dbSGreg Kroah-Hartman 		return 0;
1209014c90dbSGreg Kroah-Hartman 
1210f791b8c8SGreg Kroah-Hartman 	klist_iter_init(&parent->p->klist_children, &i);
121136239577Smochel@digitalimplant.org 	while ((child = next_device(&i)) && !error)
121236239577Smochel@digitalimplant.org 		error = fn(child, data);
121336239577Smochel@digitalimplant.org 	klist_iter_exit(&i);
12141da177e4SLinus Torvalds 	return error;
12151da177e4SLinus Torvalds }
12161da177e4SLinus Torvalds 
12175ab69981SCornelia Huck /**
12185ab69981SCornelia Huck  * device_find_child - device iterator for locating a particular device.
12195ab69981SCornelia Huck  * @parent: parent struct device
12205ab69981SCornelia Huck  * @data: Data to pass to match function
12215ab69981SCornelia Huck  * @match: Callback function to check device
12225ab69981SCornelia Huck  *
12235ab69981SCornelia Huck  * This is similar to the device_for_each_child() function above, but it
12245ab69981SCornelia Huck  * returns a reference to a device that is 'found' for later use, as
12255ab69981SCornelia Huck  * determined by the @match callback.
12265ab69981SCornelia Huck  *
12275ab69981SCornelia Huck  * The callback should return 0 if the device doesn't match and non-zero
12285ab69981SCornelia Huck  * if it does.  If the callback returns non-zero and a reference to the
12295ab69981SCornelia Huck  * current device can be obtained, this function will return to the caller
12305ab69981SCornelia Huck  * and not iterate over any more devices.
12315ab69981SCornelia Huck  */
12325ab69981SCornelia Huck struct device *device_find_child(struct device *parent, void *data,
12334a3ad20cSGreg Kroah-Hartman 				 int (*match)(struct device *dev, void *data))
12345ab69981SCornelia Huck {
12355ab69981SCornelia Huck 	struct klist_iter i;
12365ab69981SCornelia Huck 	struct device *child;
12375ab69981SCornelia Huck 
12385ab69981SCornelia Huck 	if (!parent)
12395ab69981SCornelia Huck 		return NULL;
12405ab69981SCornelia Huck 
1241f791b8c8SGreg Kroah-Hartman 	klist_iter_init(&parent->p->klist_children, &i);
12425ab69981SCornelia Huck 	while ((child = next_device(&i)))
12435ab69981SCornelia Huck 		if (match(child, data) && get_device(child))
12445ab69981SCornelia Huck 			break;
12455ab69981SCornelia Huck 	klist_iter_exit(&i);
12465ab69981SCornelia Huck 	return child;
12475ab69981SCornelia Huck }
12485ab69981SCornelia Huck 
12491da177e4SLinus Torvalds int __init devices_init(void)
12501da177e4SLinus Torvalds {
1251881c6cfdSGreg Kroah-Hartman 	devices_kset = kset_create_and_add("devices", &device_uevent_ops, NULL);
1252881c6cfdSGreg Kroah-Hartman 	if (!devices_kset)
1253881c6cfdSGreg Kroah-Hartman 		return -ENOMEM;
1254e105b8bfSDan Williams 	dev_kobj = kobject_create_and_add("dev", NULL);
1255e105b8bfSDan Williams 	if (!dev_kobj)
1256e105b8bfSDan Williams 		goto dev_kobj_err;
1257e105b8bfSDan Williams 	sysfs_dev_block_kobj = kobject_create_and_add("block", dev_kobj);
1258e105b8bfSDan Williams 	if (!sysfs_dev_block_kobj)
1259e105b8bfSDan Williams 		goto block_kobj_err;
1260e105b8bfSDan Williams 	sysfs_dev_char_kobj = kobject_create_and_add("char", dev_kobj);
1261e105b8bfSDan Williams 	if (!sysfs_dev_char_kobj)
1262e105b8bfSDan Williams 		goto char_kobj_err;
1263e105b8bfSDan Williams 
1264881c6cfdSGreg Kroah-Hartman 	return 0;
1265e105b8bfSDan Williams 
1266e105b8bfSDan Williams  char_kobj_err:
1267e105b8bfSDan Williams 	kobject_put(sysfs_dev_block_kobj);
1268e105b8bfSDan Williams  block_kobj_err:
1269e105b8bfSDan Williams 	kobject_put(dev_kobj);
1270e105b8bfSDan Williams  dev_kobj_err:
1271e105b8bfSDan Williams 	kset_unregister(devices_kset);
1272e105b8bfSDan Williams 	return -ENOMEM;
12731da177e4SLinus Torvalds }
12741da177e4SLinus Torvalds 
12751da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(device_for_each_child);
12765ab69981SCornelia Huck EXPORT_SYMBOL_GPL(device_find_child);
12771da177e4SLinus Torvalds 
12781da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(device_initialize);
12791da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(device_add);
12801da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(device_register);
12811da177e4SLinus Torvalds 
12821da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(device_del);
12831da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(device_unregister);
12841da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(get_device);
12851da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(put_device);
12861da177e4SLinus Torvalds 
12871da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(device_create_file);
12881da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(device_remove_file);
128923681e47SGreg Kroah-Hartman 
12900aa0dc41SMark McLoughlin struct root_device
12910aa0dc41SMark McLoughlin {
12920aa0dc41SMark McLoughlin 	struct device dev;
12930aa0dc41SMark McLoughlin 	struct module *owner;
12940aa0dc41SMark McLoughlin };
12950aa0dc41SMark McLoughlin 
12960aa0dc41SMark McLoughlin #define to_root_device(dev) container_of(dev, struct root_device, dev)
12970aa0dc41SMark McLoughlin 
12980aa0dc41SMark McLoughlin static void root_device_release(struct device *dev)
12990aa0dc41SMark McLoughlin {
13000aa0dc41SMark McLoughlin 	kfree(to_root_device(dev));
13010aa0dc41SMark McLoughlin }
13020aa0dc41SMark McLoughlin 
13030aa0dc41SMark McLoughlin /**
13040aa0dc41SMark McLoughlin  * __root_device_register - allocate and register a root device
13050aa0dc41SMark McLoughlin  * @name: root device name
13060aa0dc41SMark McLoughlin  * @owner: owner module of the root device, usually THIS_MODULE
13070aa0dc41SMark McLoughlin  *
13080aa0dc41SMark McLoughlin  * This function allocates a root device and registers it
13090aa0dc41SMark McLoughlin  * using device_register(). In order to free the returned
13100aa0dc41SMark McLoughlin  * device, use root_device_unregister().
13110aa0dc41SMark McLoughlin  *
13120aa0dc41SMark McLoughlin  * Root devices are dummy devices which allow other devices
13130aa0dc41SMark McLoughlin  * to be grouped under /sys/devices. Use this function to
13140aa0dc41SMark McLoughlin  * allocate a root device and then use it as the parent of
13150aa0dc41SMark McLoughlin  * any device which should appear under /sys/devices/{name}
13160aa0dc41SMark McLoughlin  *
13170aa0dc41SMark McLoughlin  * The /sys/devices/{name} directory will also contain a
13180aa0dc41SMark McLoughlin  * 'module' symlink which points to the @owner directory
13190aa0dc41SMark McLoughlin  * in sysfs.
13200aa0dc41SMark McLoughlin  *
13210aa0dc41SMark McLoughlin  * Note: You probably want to use root_device_register().
13220aa0dc41SMark McLoughlin  */
13230aa0dc41SMark McLoughlin struct device *__root_device_register(const char *name, struct module *owner)
13240aa0dc41SMark McLoughlin {
13250aa0dc41SMark McLoughlin 	struct root_device *root;
13260aa0dc41SMark McLoughlin 	int err = -ENOMEM;
13270aa0dc41SMark McLoughlin 
13280aa0dc41SMark McLoughlin 	root = kzalloc(sizeof(struct root_device), GFP_KERNEL);
13290aa0dc41SMark McLoughlin 	if (!root)
13300aa0dc41SMark McLoughlin 		return ERR_PTR(err);
13310aa0dc41SMark McLoughlin 
1332acc0e90fSGreg Kroah-Hartman 	err = dev_set_name(&root->dev, "%s", name);
13330aa0dc41SMark McLoughlin 	if (err) {
13340aa0dc41SMark McLoughlin 		kfree(root);
13350aa0dc41SMark McLoughlin 		return ERR_PTR(err);
13360aa0dc41SMark McLoughlin 	}
13370aa0dc41SMark McLoughlin 
13380aa0dc41SMark McLoughlin 	root->dev.release = root_device_release;
13390aa0dc41SMark McLoughlin 
13400aa0dc41SMark McLoughlin 	err = device_register(&root->dev);
13410aa0dc41SMark McLoughlin 	if (err) {
13420aa0dc41SMark McLoughlin 		put_device(&root->dev);
13430aa0dc41SMark McLoughlin 		return ERR_PTR(err);
13440aa0dc41SMark McLoughlin 	}
13450aa0dc41SMark McLoughlin 
13460aa0dc41SMark McLoughlin #ifdef CONFIG_MODULE	/* gotta find a "cleaner" way to do this */
13470aa0dc41SMark McLoughlin 	if (owner) {
13480aa0dc41SMark McLoughlin 		struct module_kobject *mk = &owner->mkobj;
13490aa0dc41SMark McLoughlin 
13500aa0dc41SMark McLoughlin 		err = sysfs_create_link(&root->dev.kobj, &mk->kobj, "module");
13510aa0dc41SMark McLoughlin 		if (err) {
13520aa0dc41SMark McLoughlin 			device_unregister(&root->dev);
13530aa0dc41SMark McLoughlin 			return ERR_PTR(err);
13540aa0dc41SMark McLoughlin 		}
13550aa0dc41SMark McLoughlin 		root->owner = owner;
13560aa0dc41SMark McLoughlin 	}
13570aa0dc41SMark McLoughlin #endif
13580aa0dc41SMark McLoughlin 
13590aa0dc41SMark McLoughlin 	return &root->dev;
13600aa0dc41SMark McLoughlin }
13610aa0dc41SMark McLoughlin EXPORT_SYMBOL_GPL(__root_device_register);
13620aa0dc41SMark McLoughlin 
13630aa0dc41SMark McLoughlin /**
13640aa0dc41SMark McLoughlin  * root_device_unregister - unregister and free a root device
13657cbcf225SRandy Dunlap  * @dev: device going away
13660aa0dc41SMark McLoughlin  *
13670aa0dc41SMark McLoughlin  * This function unregisters and cleans up a device that was created by
13680aa0dc41SMark McLoughlin  * root_device_register().
13690aa0dc41SMark McLoughlin  */
13700aa0dc41SMark McLoughlin void root_device_unregister(struct device *dev)
13710aa0dc41SMark McLoughlin {
13720aa0dc41SMark McLoughlin 	struct root_device *root = to_root_device(dev);
13730aa0dc41SMark McLoughlin 
13740aa0dc41SMark McLoughlin 	if (root->owner)
13750aa0dc41SMark McLoughlin 		sysfs_remove_link(&root->dev.kobj, "module");
13760aa0dc41SMark McLoughlin 
13770aa0dc41SMark McLoughlin 	device_unregister(dev);
13780aa0dc41SMark McLoughlin }
13790aa0dc41SMark McLoughlin EXPORT_SYMBOL_GPL(root_device_unregister);
13800aa0dc41SMark McLoughlin 
138123681e47SGreg Kroah-Hartman 
138223681e47SGreg Kroah-Hartman static void device_create_release(struct device *dev)
138323681e47SGreg Kroah-Hartman {
13841e0b2cf9SKay Sievers 	pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
138523681e47SGreg Kroah-Hartman 	kfree(dev);
138623681e47SGreg Kroah-Hartman }
138723681e47SGreg Kroah-Hartman 
138823681e47SGreg Kroah-Hartman /**
13898882b394SGreg Kroah-Hartman  * device_create_vargs - creates a device and registers it with sysfs
13908882b394SGreg Kroah-Hartman  * @class: pointer to the struct class that this device should be registered to
13918882b394SGreg Kroah-Hartman  * @parent: pointer to the parent struct device of this new device, if any
13928882b394SGreg Kroah-Hartman  * @devt: the dev_t for the char device to be added
13938882b394SGreg Kroah-Hartman  * @drvdata: the data to be added to the device for callbacks
13948882b394SGreg Kroah-Hartman  * @fmt: string for the device's name
13958882b394SGreg Kroah-Hartman  * @args: va_list for the device's name
13968882b394SGreg Kroah-Hartman  *
13978882b394SGreg Kroah-Hartman  * This function can be used by char device classes.  A struct device
13988882b394SGreg Kroah-Hartman  * will be created in sysfs, registered to the specified class.
13998882b394SGreg Kroah-Hartman  *
14008882b394SGreg Kroah-Hartman  * A "dev" file will be created, showing the dev_t for the device, if
14018882b394SGreg Kroah-Hartman  * the dev_t is not 0,0.
14028882b394SGreg Kroah-Hartman  * If a pointer to a parent struct device is passed in, the newly created
14038882b394SGreg Kroah-Hartman  * struct device will be a child of that device in sysfs.
14048882b394SGreg Kroah-Hartman  * The pointer to the struct device will be returned from the call.
14058882b394SGreg Kroah-Hartman  * Any further sysfs files that might be required can be created using this
14068882b394SGreg Kroah-Hartman  * pointer.
14078882b394SGreg Kroah-Hartman  *
14088882b394SGreg Kroah-Hartman  * Note: the struct class passed to this function must have previously
14098882b394SGreg Kroah-Hartman  * been created with a call to class_create().
14108882b394SGreg Kroah-Hartman  */
14118882b394SGreg Kroah-Hartman struct device *device_create_vargs(struct class *class, struct device *parent,
14128882b394SGreg Kroah-Hartman 				   dev_t devt, void *drvdata, const char *fmt,
14138882b394SGreg Kroah-Hartman 				   va_list args)
14148882b394SGreg Kroah-Hartman {
14158882b394SGreg Kroah-Hartman 	struct device *dev = NULL;
14168882b394SGreg Kroah-Hartman 	int retval = -ENODEV;
14178882b394SGreg Kroah-Hartman 
14188882b394SGreg Kroah-Hartman 	if (class == NULL || IS_ERR(class))
14198882b394SGreg Kroah-Hartman 		goto error;
14208882b394SGreg Kroah-Hartman 
14218882b394SGreg Kroah-Hartman 	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
14228882b394SGreg Kroah-Hartman 	if (!dev) {
14238882b394SGreg Kroah-Hartman 		retval = -ENOMEM;
14248882b394SGreg Kroah-Hartman 		goto error;
14258882b394SGreg Kroah-Hartman 	}
14268882b394SGreg Kroah-Hartman 
14278882b394SGreg Kroah-Hartman 	dev->devt = devt;
14288882b394SGreg Kroah-Hartman 	dev->class = class;
14298882b394SGreg Kroah-Hartman 	dev->parent = parent;
14308882b394SGreg Kroah-Hartman 	dev->release = device_create_release;
14318882b394SGreg Kroah-Hartman 	dev_set_drvdata(dev, drvdata);
14328882b394SGreg Kroah-Hartman 
14331fa5ae85SKay Sievers 	retval = kobject_set_name_vargs(&dev->kobj, fmt, args);
14341fa5ae85SKay Sievers 	if (retval)
14351fa5ae85SKay Sievers 		goto error;
14361fa5ae85SKay Sievers 
14378882b394SGreg Kroah-Hartman 	retval = device_register(dev);
14388882b394SGreg Kroah-Hartman 	if (retval)
14398882b394SGreg Kroah-Hartman 		goto error;
14408882b394SGreg Kroah-Hartman 
14418882b394SGreg Kroah-Hartman 	return dev;
14428882b394SGreg Kroah-Hartman 
14438882b394SGreg Kroah-Hartman error:
1444286661b3SCornelia Huck 	put_device(dev);
14458882b394SGreg Kroah-Hartman 	return ERR_PTR(retval);
14468882b394SGreg Kroah-Hartman }
14478882b394SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(device_create_vargs);
14488882b394SGreg Kroah-Hartman 
14498882b394SGreg Kroah-Hartman /**
14504e106739SGreg Kroah-Hartman  * device_create - creates a device and registers it with sysfs
14518882b394SGreg Kroah-Hartman  * @class: pointer to the struct class that this device should be registered to
14528882b394SGreg Kroah-Hartman  * @parent: pointer to the parent struct device of this new device, if any
14538882b394SGreg Kroah-Hartman  * @devt: the dev_t for the char device to be added
14548882b394SGreg Kroah-Hartman  * @drvdata: the data to be added to the device for callbacks
14558882b394SGreg Kroah-Hartman  * @fmt: string for the device's name
14568882b394SGreg Kroah-Hartman  *
14578882b394SGreg Kroah-Hartman  * This function can be used by char device classes.  A struct device
14588882b394SGreg Kroah-Hartman  * will be created in sysfs, registered to the specified class.
14598882b394SGreg Kroah-Hartman  *
14608882b394SGreg Kroah-Hartman  * A "dev" file will be created, showing the dev_t for the device, if
14618882b394SGreg Kroah-Hartman  * the dev_t is not 0,0.
14628882b394SGreg Kroah-Hartman  * If a pointer to a parent struct device is passed in, the newly created
14638882b394SGreg Kroah-Hartman  * struct device will be a child of that device in sysfs.
14648882b394SGreg Kroah-Hartman  * The pointer to the struct device will be returned from the call.
14658882b394SGreg Kroah-Hartman  * Any further sysfs files that might be required can be created using this
14668882b394SGreg Kroah-Hartman  * pointer.
14678882b394SGreg Kroah-Hartman  *
14688882b394SGreg Kroah-Hartman  * Note: the struct class passed to this function must have previously
14698882b394SGreg Kroah-Hartman  * been created with a call to class_create().
14708882b394SGreg Kroah-Hartman  */
14714e106739SGreg Kroah-Hartman struct device *device_create(struct class *class, struct device *parent,
14724e106739SGreg Kroah-Hartman 			     dev_t devt, void *drvdata, const char *fmt, ...)
14738882b394SGreg Kroah-Hartman {
14748882b394SGreg Kroah-Hartman 	va_list vargs;
14758882b394SGreg Kroah-Hartman 	struct device *dev;
14768882b394SGreg Kroah-Hartman 
14778882b394SGreg Kroah-Hartman 	va_start(vargs, fmt);
14788882b394SGreg Kroah-Hartman 	dev = device_create_vargs(class, parent, devt, drvdata, fmt, vargs);
14798882b394SGreg Kroah-Hartman 	va_end(vargs);
14808882b394SGreg Kroah-Hartman 	return dev;
14818882b394SGreg Kroah-Hartman }
14824e106739SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(device_create);
14838882b394SGreg Kroah-Hartman 
1484cd35449bSDave Young static int __match_devt(struct device *dev, void *data)
148523681e47SGreg Kroah-Hartman {
1486cd35449bSDave Young 	dev_t *devt = data;
148723681e47SGreg Kroah-Hartman 
1488cd35449bSDave Young 	return dev->devt == *devt;
1489775b64d2SRafael J. Wysocki }
149023681e47SGreg Kroah-Hartman 
1491775b64d2SRafael J. Wysocki /**
1492775b64d2SRafael J. Wysocki  * device_destroy - removes a device that was created with device_create()
1493775b64d2SRafael J. Wysocki  * @class: pointer to the struct class that this device was registered with
1494775b64d2SRafael J. Wysocki  * @devt: the dev_t of the device that was previously registered
1495775b64d2SRafael J. Wysocki  *
1496775b64d2SRafael J. Wysocki  * This call unregisters and cleans up a device that was created with a
1497775b64d2SRafael J. Wysocki  * call to device_create().
1498775b64d2SRafael J. Wysocki  */
1499775b64d2SRafael J. Wysocki void device_destroy(struct class *class, dev_t devt)
1500775b64d2SRafael J. Wysocki {
1501775b64d2SRafael J. Wysocki 	struct device *dev;
1502775b64d2SRafael J. Wysocki 
1503695794aeSGreg Kroah-Hartman 	dev = class_find_device(class, NULL, &devt, __match_devt);
1504cd35449bSDave Young 	if (dev) {
1505cd35449bSDave Young 		put_device(dev);
150623681e47SGreg Kroah-Hartman 		device_unregister(dev);
150723681e47SGreg Kroah-Hartman 	}
1508cd35449bSDave Young }
150923681e47SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(device_destroy);
1510a2de48caSGreg Kroah-Hartman 
1511a2de48caSGreg Kroah-Hartman /**
1512a2de48caSGreg Kroah-Hartman  * device_rename - renames a device
1513a2de48caSGreg Kroah-Hartman  * @dev: the pointer to the struct device to be renamed
1514a2de48caSGreg Kroah-Hartman  * @new_name: the new name of the device
1515030c1d2bSEric W. Biederman  *
1516030c1d2bSEric W. Biederman  * It is the responsibility of the caller to provide mutual
1517030c1d2bSEric W. Biederman  * exclusion between two different calls of device_rename
1518030c1d2bSEric W. Biederman  * on the same device to ensure that new_name is valid and
1519030c1d2bSEric W. Biederman  * won't conflict with other devices.
1520a2de48caSGreg Kroah-Hartman  */
1521a2de48caSGreg Kroah-Hartman int device_rename(struct device *dev, char *new_name)
1522a2de48caSGreg Kroah-Hartman {
1523a2de48caSGreg Kroah-Hartman 	char *old_class_name = NULL;
1524a2de48caSGreg Kroah-Hartman 	char *new_class_name = NULL;
15252ee97cafSCornelia Huck 	char *old_device_name = NULL;
1526a2de48caSGreg Kroah-Hartman 	int error;
1527a2de48caSGreg Kroah-Hartman 
1528a2de48caSGreg Kroah-Hartman 	dev = get_device(dev);
1529a2de48caSGreg Kroah-Hartman 	if (!dev)
1530a2de48caSGreg Kroah-Hartman 		return -EINVAL;
1531a2de48caSGreg Kroah-Hartman 
15321e0b2cf9SKay Sievers 	pr_debug("device: '%s': %s: renaming to '%s'\n", dev_name(dev),
15332b3a302aSHarvey Harrison 		 __func__, new_name);
1534a2de48caSGreg Kroah-Hartman 
153599ef3ef8SKay Sievers #ifdef CONFIG_SYSFS_DEPRECATED
1536a2de48caSGreg Kroah-Hartman 	if ((dev->class) && (dev->parent))
1537a2de48caSGreg Kroah-Hartman 		old_class_name = make_class_name(dev->class->name, &dev->kobj);
153899ef3ef8SKay Sievers #endif
1539a2de48caSGreg Kroah-Hartman 
15401fa5ae85SKay Sievers 	old_device_name = kstrdup(dev_name(dev), GFP_KERNEL);
15412ee97cafSCornelia Huck 	if (!old_device_name) {
1542952ab431SJesper Juhl 		error = -ENOMEM;
15432ee97cafSCornelia Huck 		goto out;
1544952ab431SJesper Juhl 	}
1545a2de48caSGreg Kroah-Hartman 
1546a2de48caSGreg Kroah-Hartman 	error = kobject_rename(&dev->kobj, new_name);
15471fa5ae85SKay Sievers 	if (error)
15482ee97cafSCornelia Huck 		goto out;
1549a2de48caSGreg Kroah-Hartman 
155099ef3ef8SKay Sievers #ifdef CONFIG_SYSFS_DEPRECATED
1551a2de48caSGreg Kroah-Hartman 	if (old_class_name) {
1552a2de48caSGreg Kroah-Hartman 		new_class_name = make_class_name(dev->class->name, &dev->kobj);
1553a2de48caSGreg Kroah-Hartman 		if (new_class_name) {
155436ce6dadSCornelia Huck 			error = sysfs_create_link_nowarn(&dev->parent->kobj,
155536ce6dadSCornelia Huck 							 &dev->kobj,
155636ce6dadSCornelia Huck 							 new_class_name);
15572ee97cafSCornelia Huck 			if (error)
15582ee97cafSCornelia Huck 				goto out;
1559a2de48caSGreg Kroah-Hartman 			sysfs_remove_link(&dev->parent->kobj, old_class_name);
1560a2de48caSGreg Kroah-Hartman 		}
1561a2de48caSGreg Kroah-Hartman 	}
156260b8cabdSKay Sievers #else
1563a2de48caSGreg Kroah-Hartman 	if (dev->class) {
156436ce6dadSCornelia Huck 		error = sysfs_create_link_nowarn(&dev->class->p->class_subsys.kobj,
15651e0b2cf9SKay Sievers 						 &dev->kobj, dev_name(dev));
15660599ad53SStephen Hemminger 		if (error)
15670599ad53SStephen Hemminger 			goto out;
15681fbfee6cSGreg Kroah-Hartman 		sysfs_remove_link(&dev->class->p->class_subsys.kobj,
15697c71448bSGreg Kroah-Hartman 				  old_device_name);
15702ee97cafSCornelia Huck 	}
157160b8cabdSKay Sievers #endif
157260b8cabdSKay Sievers 
15732ee97cafSCornelia Huck out:
1574a2de48caSGreg Kroah-Hartman 	put_device(dev);
1575a2de48caSGreg Kroah-Hartman 
1576a2de48caSGreg Kroah-Hartman 	kfree(new_class_name);
1577952ab431SJesper Juhl 	kfree(old_class_name);
15782ee97cafSCornelia Huck 	kfree(old_device_name);
1579a2de48caSGreg Kroah-Hartman 
1580a2de48caSGreg Kroah-Hartman 	return error;
1581a2de48caSGreg Kroah-Hartman }
1582a2807dbcSJohannes Berg EXPORT_SYMBOL_GPL(device_rename);
15838a82472fSCornelia Huck 
15848a82472fSCornelia Huck static int device_move_class_links(struct device *dev,
15858a82472fSCornelia Huck 				   struct device *old_parent,
15868a82472fSCornelia Huck 				   struct device *new_parent)
15878a82472fSCornelia Huck {
1588f7f3461dSGreg Kroah-Hartman 	int error = 0;
15898a82472fSCornelia Huck #ifdef CONFIG_SYSFS_DEPRECATED
15908a82472fSCornelia Huck 	char *class_name;
15918a82472fSCornelia Huck 
15928a82472fSCornelia Huck 	class_name = make_class_name(dev->class->name, &dev->kobj);
15938a82472fSCornelia Huck 	if (!class_name) {
1594cb360bbfSCornelia Huck 		error = -ENOMEM;
15958a82472fSCornelia Huck 		goto out;
15968a82472fSCornelia Huck 	}
15978a82472fSCornelia Huck 	if (old_parent) {
15988a82472fSCornelia Huck 		sysfs_remove_link(&dev->kobj, "device");
15998a82472fSCornelia Huck 		sysfs_remove_link(&old_parent->kobj, class_name);
16008a82472fSCornelia Huck 	}
1601c744aeaeSCornelia Huck 	if (new_parent) {
1602c744aeaeSCornelia Huck 		error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
1603c744aeaeSCornelia Huck 					  "device");
16048a82472fSCornelia Huck 		if (error)
16058a82472fSCornelia Huck 			goto out;
1606c744aeaeSCornelia Huck 		error = sysfs_create_link(&new_parent->kobj, &dev->kobj,
1607c744aeaeSCornelia Huck 					  class_name);
16088a82472fSCornelia Huck 		if (error)
16098a82472fSCornelia Huck 			sysfs_remove_link(&dev->kobj, "device");
16104a3ad20cSGreg Kroah-Hartman 	} else
1611c744aeaeSCornelia Huck 		error = 0;
16128a82472fSCornelia Huck out:
16138a82472fSCornelia Huck 	kfree(class_name);
16148a82472fSCornelia Huck 	return error;
16158a82472fSCornelia Huck #else
1616f7f3461dSGreg Kroah-Hartman 	if (old_parent)
1617f7f3461dSGreg Kroah-Hartman 		sysfs_remove_link(&dev->kobj, "device");
1618f7f3461dSGreg Kroah-Hartman 	if (new_parent)
1619f7f3461dSGreg Kroah-Hartman 		error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
1620f7f3461dSGreg Kroah-Hartman 					  "device");
1621f7f3461dSGreg Kroah-Hartman 	return error;
16228a82472fSCornelia Huck #endif
16238a82472fSCornelia Huck }
16248a82472fSCornelia Huck 
16258a82472fSCornelia Huck /**
16268a82472fSCornelia Huck  * device_move - moves a device to a new parent
16278a82472fSCornelia Huck  * @dev: the pointer to the struct device to be moved
1628c744aeaeSCornelia Huck  * @new_parent: the new parent of the device (can by NULL)
1629ffa6a705SCornelia Huck  * @dpm_order: how to reorder the dpm_list
16308a82472fSCornelia Huck  */
1631ffa6a705SCornelia Huck int device_move(struct device *dev, struct device *new_parent,
1632ffa6a705SCornelia Huck 		enum dpm_order dpm_order)
16338a82472fSCornelia Huck {
16348a82472fSCornelia Huck 	int error;
16358a82472fSCornelia Huck 	struct device *old_parent;
1636c744aeaeSCornelia Huck 	struct kobject *new_parent_kobj;
16378a82472fSCornelia Huck 
16388a82472fSCornelia Huck 	dev = get_device(dev);
16398a82472fSCornelia Huck 	if (!dev)
16408a82472fSCornelia Huck 		return -EINVAL;
16418a82472fSCornelia Huck 
1642ffa6a705SCornelia Huck 	device_pm_lock();
16438a82472fSCornelia Huck 	new_parent = get_device(new_parent);
1644c744aeaeSCornelia Huck 	new_parent_kobj = get_device_parent(dev, new_parent);
164563b6971aSCornelia Huck 
16461e0b2cf9SKay Sievers 	pr_debug("device: '%s': %s: moving to '%s'\n", dev_name(dev),
16471e0b2cf9SKay Sievers 		 __func__, new_parent ? dev_name(new_parent) : "<NULL>");
1648c744aeaeSCornelia Huck 	error = kobject_move(&dev->kobj, new_parent_kobj);
16498a82472fSCornelia Huck 	if (error) {
165063b6971aSCornelia Huck 		cleanup_glue_dir(dev, new_parent_kobj);
16518a82472fSCornelia Huck 		put_device(new_parent);
16528a82472fSCornelia Huck 		goto out;
16538a82472fSCornelia Huck 	}
16548a82472fSCornelia Huck 	old_parent = dev->parent;
16558a82472fSCornelia Huck 	dev->parent = new_parent;
16568a82472fSCornelia Huck 	if (old_parent)
1657f791b8c8SGreg Kroah-Hartman 		klist_remove(&dev->p->knode_parent);
16580d358f22SYinghai Lu 	if (new_parent) {
1659f791b8c8SGreg Kroah-Hartman 		klist_add_tail(&dev->p->knode_parent,
1660f791b8c8SGreg Kroah-Hartman 			       &new_parent->p->klist_children);
16610d358f22SYinghai Lu 		set_dev_node(dev, dev_to_node(new_parent));
16620d358f22SYinghai Lu 	}
16630d358f22SYinghai Lu 
16648a82472fSCornelia Huck 	if (!dev->class)
16658a82472fSCornelia Huck 		goto out_put;
16668a82472fSCornelia Huck 	error = device_move_class_links(dev, old_parent, new_parent);
16678a82472fSCornelia Huck 	if (error) {
16688a82472fSCornelia Huck 		/* We ignore errors on cleanup since we're hosed anyway... */
16698a82472fSCornelia Huck 		device_move_class_links(dev, new_parent, old_parent);
16708a82472fSCornelia Huck 		if (!kobject_move(&dev->kobj, &old_parent->kobj)) {
1671c744aeaeSCornelia Huck 			if (new_parent)
1672f791b8c8SGreg Kroah-Hartman 				klist_remove(&dev->p->knode_parent);
16730d358f22SYinghai Lu 			dev->parent = old_parent;
16740d358f22SYinghai Lu 			if (old_parent) {
1675f791b8c8SGreg Kroah-Hartman 				klist_add_tail(&dev->p->knode_parent,
1676f791b8c8SGreg Kroah-Hartman 					       &old_parent->p->klist_children);
16770d358f22SYinghai Lu 				set_dev_node(dev, dev_to_node(old_parent));
16780d358f22SYinghai Lu 			}
16798a82472fSCornelia Huck 		}
168063b6971aSCornelia Huck 		cleanup_glue_dir(dev, new_parent_kobj);
16818a82472fSCornelia Huck 		put_device(new_parent);
16828a82472fSCornelia Huck 		goto out;
16838a82472fSCornelia Huck 	}
1684ffa6a705SCornelia Huck 	switch (dpm_order) {
1685ffa6a705SCornelia Huck 	case DPM_ORDER_NONE:
1686ffa6a705SCornelia Huck 		break;
1687ffa6a705SCornelia Huck 	case DPM_ORDER_DEV_AFTER_PARENT:
1688ffa6a705SCornelia Huck 		device_pm_move_after(dev, new_parent);
1689ffa6a705SCornelia Huck 		break;
1690ffa6a705SCornelia Huck 	case DPM_ORDER_PARENT_BEFORE_DEV:
1691ffa6a705SCornelia Huck 		device_pm_move_before(new_parent, dev);
1692ffa6a705SCornelia Huck 		break;
1693ffa6a705SCornelia Huck 	case DPM_ORDER_DEV_LAST:
1694ffa6a705SCornelia Huck 		device_pm_move_last(dev);
1695ffa6a705SCornelia Huck 		break;
1696ffa6a705SCornelia Huck 	}
16978a82472fSCornelia Huck out_put:
16988a82472fSCornelia Huck 	put_device(old_parent);
16998a82472fSCornelia Huck out:
1700ffa6a705SCornelia Huck 	device_pm_unlock();
17018a82472fSCornelia Huck 	put_device(dev);
17028a82472fSCornelia Huck 	return error;
17038a82472fSCornelia Huck }
17048a82472fSCornelia Huck EXPORT_SYMBOL_GPL(device_move);
170537b0c020SGreg Kroah-Hartman 
170637b0c020SGreg Kroah-Hartman /**
170737b0c020SGreg Kroah-Hartman  * device_shutdown - call ->shutdown() on each device to shutdown.
170837b0c020SGreg Kroah-Hartman  */
170937b0c020SGreg Kroah-Hartman void device_shutdown(void)
171037b0c020SGreg Kroah-Hartman {
171137b0c020SGreg Kroah-Hartman 	struct device *dev, *devn;
171237b0c020SGreg Kroah-Hartman 
171337b0c020SGreg Kroah-Hartman 	list_for_each_entry_safe_reverse(dev, devn, &devices_kset->list,
171437b0c020SGreg Kroah-Hartman 				kobj.entry) {
171537b0c020SGreg Kroah-Hartman 		if (dev->bus && dev->bus->shutdown) {
171637b0c020SGreg Kroah-Hartman 			dev_dbg(dev, "shutdown\n");
171737b0c020SGreg Kroah-Hartman 			dev->bus->shutdown(dev);
171837b0c020SGreg Kroah-Hartman 		} else if (dev->driver && dev->driver->shutdown) {
171937b0c020SGreg Kroah-Hartman 			dev_dbg(dev, "shutdown\n");
172037b0c020SGreg Kroah-Hartman 			dev->driver->shutdown(dev);
172137b0c020SGreg Kroah-Hartman 		}
172237b0c020SGreg Kroah-Hartman 	}
1723e105b8bfSDan Williams 	kobject_put(sysfs_dev_char_kobj);
1724e105b8bfSDan Williams 	kobject_put(sysfs_dev_block_kobj);
1725e105b8bfSDan Williams 	kobject_put(dev_kobj);
1726401097eaSShaohua Li 	async_synchronize_full();
172737b0c020SGreg Kroah-Hartman }
1728