xref: /openbmc/linux/drivers/base/core.c (revision ffa6a705)
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>
251da177e4SLinus Torvalds 
261da177e4SLinus Torvalds #include "base.h"
271da177e4SLinus Torvalds #include "power/power.h"
281da177e4SLinus Torvalds 
291da177e4SLinus Torvalds int (*platform_notify)(struct device *dev) = NULL;
301da177e4SLinus Torvalds int (*platform_notify_remove)(struct device *dev) = NULL;
31e105b8bfSDan Williams static struct kobject *dev_kobj;
32e105b8bfSDan Williams struct kobject *sysfs_dev_char_kobj;
33e105b8bfSDan Williams struct kobject *sysfs_dev_block_kobj;
341da177e4SLinus Torvalds 
354e886c29SGreg Kroah-Hartman #ifdef CONFIG_BLOCK
364e886c29SGreg Kroah-Hartman static inline int device_is_not_partition(struct device *dev)
374e886c29SGreg Kroah-Hartman {
384e886c29SGreg Kroah-Hartman 	return !(dev->type == &part_type);
394e886c29SGreg Kroah-Hartman }
404e886c29SGreg Kroah-Hartman #else
414e886c29SGreg Kroah-Hartman static inline int device_is_not_partition(struct device *dev)
424e886c29SGreg Kroah-Hartman {
434e886c29SGreg Kroah-Hartman 	return 1;
444e886c29SGreg Kroah-Hartman }
454e886c29SGreg Kroah-Hartman #endif
461da177e4SLinus Torvalds 
473e95637aSAlan Stern /**
483e95637aSAlan Stern  * dev_driver_string - Return a device's driver name, if at all possible
493e95637aSAlan Stern  * @dev: struct device to get the name of
503e95637aSAlan Stern  *
513e95637aSAlan Stern  * Will return the device's driver's name if it is bound to a device.  If
523e95637aSAlan Stern  * the device is not bound to a device, it will return the name of the bus
533e95637aSAlan Stern  * it is attached to.  If it is not attached to a bus either, an empty
543e95637aSAlan Stern  * string will be returned.
553e95637aSAlan Stern  */
56bf9ca69fSJean Delvare const char *dev_driver_string(const struct device *dev)
573e95637aSAlan Stern {
583e95637aSAlan Stern 	return dev->driver ? dev->driver->name :
59a456b702SJean Delvare 			(dev->bus ? dev->bus->name :
60a456b702SJean Delvare 			(dev->class ? dev->class->name : ""));
613e95637aSAlan Stern }
62310a922dSMatthew Wilcox EXPORT_SYMBOL(dev_driver_string);
633e95637aSAlan Stern 
641da177e4SLinus Torvalds #define to_dev(obj) container_of(obj, struct device, kobj)
651da177e4SLinus Torvalds #define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr)
661da177e4SLinus Torvalds 
674a3ad20cSGreg Kroah-Hartman static ssize_t dev_attr_show(struct kobject *kobj, struct attribute *attr,
684a3ad20cSGreg Kroah-Hartman 			     char *buf)
691da177e4SLinus Torvalds {
701da177e4SLinus Torvalds 	struct device_attribute *dev_attr = to_dev_attr(attr);
711da177e4SLinus Torvalds 	struct device *dev = to_dev(kobj);
724a0c20bfSDmitry Torokhov 	ssize_t ret = -EIO;
731da177e4SLinus Torvalds 
741da177e4SLinus Torvalds 	if (dev_attr->show)
7554b6f35cSYani Ioannou 		ret = dev_attr->show(dev, dev_attr, buf);
76815d2d50SAndrew Morton 	if (ret >= (ssize_t)PAGE_SIZE) {
77815d2d50SAndrew Morton 		print_symbol("dev_attr_show: %s returned bad count\n",
78815d2d50SAndrew Morton 				(unsigned long)dev_attr->show);
79815d2d50SAndrew Morton 	}
801da177e4SLinus Torvalds 	return ret;
811da177e4SLinus Torvalds }
821da177e4SLinus Torvalds 
834a3ad20cSGreg Kroah-Hartman static ssize_t dev_attr_store(struct kobject *kobj, struct attribute *attr,
841da177e4SLinus Torvalds 			      const char *buf, size_t count)
851da177e4SLinus Torvalds {
861da177e4SLinus Torvalds 	struct device_attribute *dev_attr = to_dev_attr(attr);
871da177e4SLinus Torvalds 	struct device *dev = to_dev(kobj);
884a0c20bfSDmitry Torokhov 	ssize_t ret = -EIO;
891da177e4SLinus Torvalds 
901da177e4SLinus Torvalds 	if (dev_attr->store)
9154b6f35cSYani Ioannou 		ret = dev_attr->store(dev, dev_attr, buf, count);
921da177e4SLinus Torvalds 	return ret;
931da177e4SLinus Torvalds }
941da177e4SLinus Torvalds 
951da177e4SLinus Torvalds static struct sysfs_ops dev_sysfs_ops = {
961da177e4SLinus Torvalds 	.show	= dev_attr_show,
971da177e4SLinus Torvalds 	.store	= dev_attr_store,
981da177e4SLinus Torvalds };
991da177e4SLinus Torvalds 
1001da177e4SLinus Torvalds 
1011da177e4SLinus Torvalds /**
1021da177e4SLinus Torvalds  *	device_release - free device structure.
1031da177e4SLinus Torvalds  *	@kobj:	device's kobject.
1041da177e4SLinus Torvalds  *
1051da177e4SLinus Torvalds  *	This is called once the reference count for the object
1061da177e4SLinus Torvalds  *	reaches 0. We forward the call to the device's release
1071da177e4SLinus Torvalds  *	method, which should handle actually freeing the structure.
1081da177e4SLinus Torvalds  */
1091da177e4SLinus Torvalds static void device_release(struct kobject *kobj)
1101da177e4SLinus Torvalds {
1111da177e4SLinus Torvalds 	struct device *dev = to_dev(kobj);
112fb069a5dSGreg Kroah-Hartman 	struct device_private *p = dev->p;
1131da177e4SLinus Torvalds 
1141da177e4SLinus Torvalds 	if (dev->release)
1151da177e4SLinus Torvalds 		dev->release(dev);
116f9f852dfSKay Sievers 	else if (dev->type && dev->type->release)
117f9f852dfSKay Sievers 		dev->type->release(dev);
1182620efefSGreg Kroah-Hartman 	else if (dev->class && dev->class->dev_release)
1192620efefSGreg Kroah-Hartman 		dev->class->dev_release(dev);
120f810a5cfSArjan van de Ven 	else
121f810a5cfSArjan van de Ven 		WARN(1, KERN_ERR "Device '%s' does not have a release() "
1224a3ad20cSGreg Kroah-Hartman 			"function, it is broken and must be fixed.\n",
1231e0b2cf9SKay Sievers 			dev_name(dev));
124fb069a5dSGreg Kroah-Hartman 	kfree(p);
1251da177e4SLinus Torvalds }
1261da177e4SLinus Torvalds 
1278f4afc41SGreg Kroah-Hartman static struct kobj_type device_ktype = {
1281da177e4SLinus Torvalds 	.release	= device_release,
1291da177e4SLinus Torvalds 	.sysfs_ops	= &dev_sysfs_ops,
1301da177e4SLinus Torvalds };
1311da177e4SLinus Torvalds 
1321da177e4SLinus Torvalds 
133312c004dSKay Sievers static int dev_uevent_filter(struct kset *kset, struct kobject *kobj)
1341da177e4SLinus Torvalds {
1351da177e4SLinus Torvalds 	struct kobj_type *ktype = get_ktype(kobj);
1361da177e4SLinus Torvalds 
1378f4afc41SGreg Kroah-Hartman 	if (ktype == &device_ktype) {
1381da177e4SLinus Torvalds 		struct device *dev = to_dev(kobj);
1391da177e4SLinus Torvalds 		if (dev->bus)
1401da177e4SLinus Torvalds 			return 1;
14123681e47SGreg Kroah-Hartman 		if (dev->class)
14223681e47SGreg Kroah-Hartman 			return 1;
1431da177e4SLinus Torvalds 	}
1441da177e4SLinus Torvalds 	return 0;
1451da177e4SLinus Torvalds }
1461da177e4SLinus Torvalds 
147312c004dSKay Sievers static const char *dev_uevent_name(struct kset *kset, struct kobject *kobj)
1481da177e4SLinus Torvalds {
1491da177e4SLinus Torvalds 	struct device *dev = to_dev(kobj);
1501da177e4SLinus Torvalds 
15123681e47SGreg Kroah-Hartman 	if (dev->bus)
1521da177e4SLinus Torvalds 		return dev->bus->name;
15323681e47SGreg Kroah-Hartman 	if (dev->class)
15423681e47SGreg Kroah-Hartman 		return dev->class->name;
15523681e47SGreg Kroah-Hartman 	return NULL;
1561da177e4SLinus Torvalds }
1571da177e4SLinus Torvalds 
1587eff2e7aSKay Sievers static int dev_uevent(struct kset *kset, struct kobject *kobj,
1597eff2e7aSKay Sievers 		      struct kobj_uevent_env *env)
1601da177e4SLinus Torvalds {
1611da177e4SLinus Torvalds 	struct device *dev = to_dev(kobj);
1621da177e4SLinus Torvalds 	int retval = 0;
1631da177e4SLinus Torvalds 
16423681e47SGreg Kroah-Hartman 	/* add the major/minor if present */
16523681e47SGreg Kroah-Hartman 	if (MAJOR(dev->devt)) {
1667eff2e7aSKay Sievers 		add_uevent_var(env, "MAJOR=%u", MAJOR(dev->devt));
1677eff2e7aSKay Sievers 		add_uevent_var(env, "MINOR=%u", MINOR(dev->devt));
16823681e47SGreg Kroah-Hartman 	}
16923681e47SGreg Kroah-Hartman 
170414264f9SKay Sievers 	if (dev->type && dev->type->name)
1717eff2e7aSKay Sievers 		add_uevent_var(env, "DEVTYPE=%s", dev->type->name);
172414264f9SKay Sievers 
173239378f1SKay Sievers 	if (dev->driver)
1747eff2e7aSKay Sievers 		add_uevent_var(env, "DRIVER=%s", dev->driver->name);
175239378f1SKay Sievers 
176a87cb2acSKay Sievers #ifdef CONFIG_SYSFS_DEPRECATED
177239378f1SKay Sievers 	if (dev->class) {
178239378f1SKay Sievers 		struct device *parent = dev->parent;
179239378f1SKay Sievers 
180239378f1SKay Sievers 		/* find first bus device in parent chain */
181239378f1SKay Sievers 		while (parent && !parent->bus)
182239378f1SKay Sievers 			parent = parent->parent;
183239378f1SKay Sievers 		if (parent && parent->bus) {
184239378f1SKay Sievers 			const char *path;
185239378f1SKay Sievers 
186239378f1SKay Sievers 			path = kobject_get_path(&parent->kobj, GFP_KERNEL);
1872c7afd12SKay Sievers 			if (path) {
1887eff2e7aSKay Sievers 				add_uevent_var(env, "PHYSDEVPATH=%s", path);
189239378f1SKay Sievers 				kfree(path);
1902c7afd12SKay Sievers 			}
191239378f1SKay Sievers 
1927eff2e7aSKay Sievers 			add_uevent_var(env, "PHYSDEVBUS=%s", parent->bus->name);
193239378f1SKay Sievers 
194239378f1SKay Sievers 			if (parent->driver)
1957eff2e7aSKay Sievers 				add_uevent_var(env, "PHYSDEVDRIVER=%s",
1967eff2e7aSKay Sievers 					       parent->driver->name);
197239378f1SKay Sievers 		}
198239378f1SKay Sievers 	} else if (dev->bus) {
1997eff2e7aSKay Sievers 		add_uevent_var(env, "PHYSDEVBUS=%s", dev->bus->name);
200239378f1SKay Sievers 
201239378f1SKay Sievers 		if (dev->driver)
2024a3ad20cSGreg Kroah-Hartman 			add_uevent_var(env, "PHYSDEVDRIVER=%s",
2034a3ad20cSGreg Kroah-Hartman 				       dev->driver->name);
204d81d9d6bSKay Sievers 	}
205239378f1SKay Sievers #endif
2061da177e4SLinus Torvalds 
2071da177e4SLinus Torvalds 	/* have the bus specific function add its stuff */
2087eff2e7aSKay Sievers 	if (dev->bus && dev->bus->uevent) {
2097eff2e7aSKay Sievers 		retval = dev->bus->uevent(dev, env);
210f9f852dfSKay Sievers 		if (retval)
2117dc72b28SGreg Kroah-Hartman 			pr_debug("device: '%s': %s: bus uevent() returned %d\n",
2121e0b2cf9SKay Sievers 				 dev_name(dev), __func__, retval);
2131da177e4SLinus Torvalds 	}
2141da177e4SLinus Torvalds 
2152620efefSGreg Kroah-Hartman 	/* have the class specific function add its stuff */
2167eff2e7aSKay Sievers 	if (dev->class && dev->class->dev_uevent) {
2177eff2e7aSKay Sievers 		retval = dev->class->dev_uevent(dev, env);
218f9f852dfSKay Sievers 		if (retval)
2197dc72b28SGreg Kroah-Hartman 			pr_debug("device: '%s': %s: class uevent() "
2201e0b2cf9SKay Sievers 				 "returned %d\n", dev_name(dev),
2212b3a302aSHarvey Harrison 				 __func__, retval);
2222620efefSGreg Kroah-Hartman 	}
223f9f852dfSKay Sievers 
224f9f852dfSKay Sievers 	/* have the device type specific fuction add its stuff */
2257eff2e7aSKay Sievers 	if (dev->type && dev->type->uevent) {
2267eff2e7aSKay Sievers 		retval = dev->type->uevent(dev, env);
227f9f852dfSKay Sievers 		if (retval)
2287dc72b28SGreg Kroah-Hartman 			pr_debug("device: '%s': %s: dev_type uevent() "
2291e0b2cf9SKay Sievers 				 "returned %d\n", dev_name(dev),
2302b3a302aSHarvey Harrison 				 __func__, retval);
2312620efefSGreg Kroah-Hartman 	}
2322620efefSGreg Kroah-Hartman 
2331da177e4SLinus Torvalds 	return retval;
2341da177e4SLinus Torvalds }
2351da177e4SLinus Torvalds 
236312c004dSKay Sievers static struct kset_uevent_ops device_uevent_ops = {
237312c004dSKay Sievers 	.filter =	dev_uevent_filter,
238312c004dSKay Sievers 	.name =		dev_uevent_name,
239312c004dSKay Sievers 	.uevent =	dev_uevent,
2401da177e4SLinus Torvalds };
2411da177e4SLinus Torvalds 
24216574dccSKay Sievers static ssize_t show_uevent(struct device *dev, struct device_attribute *attr,
24316574dccSKay Sievers 			   char *buf)
24416574dccSKay Sievers {
24516574dccSKay Sievers 	struct kobject *top_kobj;
24616574dccSKay Sievers 	struct kset *kset;
2477eff2e7aSKay Sievers 	struct kobj_uevent_env *env = NULL;
24816574dccSKay Sievers 	int i;
24916574dccSKay Sievers 	size_t count = 0;
25016574dccSKay Sievers 	int retval;
25116574dccSKay Sievers 
25216574dccSKay Sievers 	/* search the kset, the device belongs to */
25316574dccSKay Sievers 	top_kobj = &dev->kobj;
2545c5daf65SKay Sievers 	while (!top_kobj->kset && top_kobj->parent)
25516574dccSKay Sievers 		top_kobj = top_kobj->parent;
25616574dccSKay Sievers 	if (!top_kobj->kset)
25716574dccSKay Sievers 		goto out;
2585c5daf65SKay Sievers 
25916574dccSKay Sievers 	kset = top_kobj->kset;
26016574dccSKay Sievers 	if (!kset->uevent_ops || !kset->uevent_ops->uevent)
26116574dccSKay Sievers 		goto out;
26216574dccSKay Sievers 
26316574dccSKay Sievers 	/* respect filter */
26416574dccSKay Sievers 	if (kset->uevent_ops && kset->uevent_ops->filter)
26516574dccSKay Sievers 		if (!kset->uevent_ops->filter(kset, &dev->kobj))
26616574dccSKay Sievers 			goto out;
26716574dccSKay Sievers 
2687eff2e7aSKay Sievers 	env = kzalloc(sizeof(struct kobj_uevent_env), GFP_KERNEL);
2697eff2e7aSKay Sievers 	if (!env)
270c7308c81SGreg Kroah-Hartman 		return -ENOMEM;
271c7308c81SGreg Kroah-Hartman 
27216574dccSKay Sievers 	/* let the kset specific function add its keys */
2737eff2e7aSKay Sievers 	retval = kset->uevent_ops->uevent(kset, &dev->kobj, env);
27416574dccSKay Sievers 	if (retval)
27516574dccSKay Sievers 		goto out;
27616574dccSKay Sievers 
27716574dccSKay Sievers 	/* copy keys to file */
2787eff2e7aSKay Sievers 	for (i = 0; i < env->envp_idx; i++)
2797eff2e7aSKay Sievers 		count += sprintf(&buf[count], "%s\n", env->envp[i]);
28016574dccSKay Sievers out:
2817eff2e7aSKay Sievers 	kfree(env);
28216574dccSKay Sievers 	return count;
28316574dccSKay Sievers }
28416574dccSKay Sievers 
285a7fd6706SKay Sievers static ssize_t store_uevent(struct device *dev, struct device_attribute *attr,
286a7fd6706SKay Sievers 			    const char *buf, size_t count)
287a7fd6706SKay Sievers {
28860a96a59SKay Sievers 	enum kobject_action action;
28960a96a59SKay Sievers 
2905c5daf65SKay Sievers 	if (kobject_action_type(buf, count, &action) == 0) {
29160a96a59SKay Sievers 		kobject_uevent(&dev->kobj, action);
29260a96a59SKay Sievers 		goto out;
29360a96a59SKay Sievers 	}
29460a96a59SKay Sievers 
29522af74f3SKay Sievers 	dev_err(dev, "uevent: unsupported action-string; this will "
29660a96a59SKay Sievers 		     "be ignored in a future kernel version\n");
297312c004dSKay Sievers 	kobject_uevent(&dev->kobj, KOBJ_ADD);
29860a96a59SKay Sievers out:
299a7fd6706SKay Sievers 	return count;
300a7fd6706SKay Sievers }
301a7fd6706SKay Sievers 
302ad6a1e1cSTejun Heo static struct device_attribute uevent_attr =
303ad6a1e1cSTejun Heo 	__ATTR(uevent, S_IRUGO | S_IWUSR, show_uevent, store_uevent);
304ad6a1e1cSTejun Heo 
305621a1672SDmitry Torokhov static int device_add_attributes(struct device *dev,
306621a1672SDmitry Torokhov 				 struct device_attribute *attrs)
307de0ff00dSGreg Kroah-Hartman {
308de0ff00dSGreg Kroah-Hartman 	int error = 0;
309621a1672SDmitry Torokhov 	int i;
310de0ff00dSGreg Kroah-Hartman 
311621a1672SDmitry Torokhov 	if (attrs) {
312621a1672SDmitry Torokhov 		for (i = 0; attr_name(attrs[i]); i++) {
313621a1672SDmitry Torokhov 			error = device_create_file(dev, &attrs[i]);
314621a1672SDmitry Torokhov 			if (error)
315621a1672SDmitry Torokhov 				break;
316621a1672SDmitry Torokhov 		}
317621a1672SDmitry Torokhov 		if (error)
318de0ff00dSGreg Kroah-Hartman 			while (--i >= 0)
319621a1672SDmitry Torokhov 				device_remove_file(dev, &attrs[i]);
320de0ff00dSGreg Kroah-Hartman 	}
321de0ff00dSGreg Kroah-Hartman 	return error;
322de0ff00dSGreg Kroah-Hartman }
323de0ff00dSGreg Kroah-Hartman 
324621a1672SDmitry Torokhov static void device_remove_attributes(struct device *dev,
325621a1672SDmitry Torokhov 				     struct device_attribute *attrs)
326de0ff00dSGreg Kroah-Hartman {
327de0ff00dSGreg Kroah-Hartman 	int i;
328621a1672SDmitry Torokhov 
329621a1672SDmitry Torokhov 	if (attrs)
330621a1672SDmitry Torokhov 		for (i = 0; attr_name(attrs[i]); i++)
331621a1672SDmitry Torokhov 			device_remove_file(dev, &attrs[i]);
332621a1672SDmitry Torokhov }
333621a1672SDmitry Torokhov 
334621a1672SDmitry Torokhov static int device_add_groups(struct device *dev,
335621a1672SDmitry Torokhov 			     struct attribute_group **groups)
336621a1672SDmitry Torokhov {
337621a1672SDmitry Torokhov 	int error = 0;
338621a1672SDmitry Torokhov 	int i;
339621a1672SDmitry Torokhov 
340621a1672SDmitry Torokhov 	if (groups) {
341621a1672SDmitry Torokhov 		for (i = 0; groups[i]; i++) {
342621a1672SDmitry Torokhov 			error = sysfs_create_group(&dev->kobj, groups[i]);
343621a1672SDmitry Torokhov 			if (error) {
344621a1672SDmitry Torokhov 				while (--i >= 0)
3454a3ad20cSGreg Kroah-Hartman 					sysfs_remove_group(&dev->kobj,
3464a3ad20cSGreg Kroah-Hartman 							   groups[i]);
347621a1672SDmitry Torokhov 				break;
348de0ff00dSGreg Kroah-Hartman 			}
349de0ff00dSGreg Kroah-Hartman 		}
350de0ff00dSGreg Kroah-Hartman 	}
351621a1672SDmitry Torokhov 	return error;
352621a1672SDmitry Torokhov }
353621a1672SDmitry Torokhov 
354621a1672SDmitry Torokhov static void device_remove_groups(struct device *dev,
355621a1672SDmitry Torokhov 				 struct attribute_group **groups)
356621a1672SDmitry Torokhov {
357621a1672SDmitry Torokhov 	int i;
358621a1672SDmitry Torokhov 
359621a1672SDmitry Torokhov 	if (groups)
360621a1672SDmitry Torokhov 		for (i = 0; groups[i]; i++)
361621a1672SDmitry Torokhov 			sysfs_remove_group(&dev->kobj, groups[i]);
362621a1672SDmitry Torokhov }
363de0ff00dSGreg Kroah-Hartman 
3642620efefSGreg Kroah-Hartman static int device_add_attrs(struct device *dev)
3652620efefSGreg Kroah-Hartman {
3662620efefSGreg Kroah-Hartman 	struct class *class = dev->class;
367f9f852dfSKay Sievers 	struct device_type *type = dev->type;
368621a1672SDmitry Torokhov 	int error;
3692620efefSGreg Kroah-Hartman 
370621a1672SDmitry Torokhov 	if (class) {
371621a1672SDmitry Torokhov 		error = device_add_attributes(dev, class->dev_attrs);
3722620efefSGreg Kroah-Hartman 		if (error)
373621a1672SDmitry Torokhov 			return error;
374f9f852dfSKay Sievers 	}
375f9f852dfSKay Sievers 
376621a1672SDmitry Torokhov 	if (type) {
377621a1672SDmitry Torokhov 		error = device_add_groups(dev, type->groups);
378f9f852dfSKay Sievers 		if (error)
379621a1672SDmitry Torokhov 			goto err_remove_class_attrs;
380f9f852dfSKay Sievers 	}
381621a1672SDmitry Torokhov 
382621a1672SDmitry Torokhov 	error = device_add_groups(dev, dev->groups);
383f9f852dfSKay Sievers 	if (error)
384621a1672SDmitry Torokhov 		goto err_remove_type_groups;
385621a1672SDmitry Torokhov 
386621a1672SDmitry Torokhov 	return 0;
387621a1672SDmitry Torokhov 
388621a1672SDmitry Torokhov  err_remove_type_groups:
389621a1672SDmitry Torokhov 	if (type)
390621a1672SDmitry Torokhov 		device_remove_groups(dev, type->groups);
391621a1672SDmitry Torokhov  err_remove_class_attrs:
392621a1672SDmitry Torokhov 	if (class)
393621a1672SDmitry Torokhov 		device_remove_attributes(dev, class->dev_attrs);
394f9f852dfSKay Sievers 
3952620efefSGreg Kroah-Hartman 	return error;
3962620efefSGreg Kroah-Hartman }
3972620efefSGreg Kroah-Hartman 
3982620efefSGreg Kroah-Hartman static void device_remove_attrs(struct device *dev)
3992620efefSGreg Kroah-Hartman {
4002620efefSGreg Kroah-Hartman 	struct class *class = dev->class;
401f9f852dfSKay Sievers 	struct device_type *type = dev->type;
4022620efefSGreg Kroah-Hartman 
403621a1672SDmitry Torokhov 	device_remove_groups(dev, dev->groups);
404f9f852dfSKay Sievers 
405621a1672SDmitry Torokhov 	if (type)
406621a1672SDmitry Torokhov 		device_remove_groups(dev, type->groups);
407621a1672SDmitry Torokhov 
408621a1672SDmitry Torokhov 	if (class)
409621a1672SDmitry Torokhov 		device_remove_attributes(dev, class->dev_attrs);
4102620efefSGreg Kroah-Hartman }
4112620efefSGreg Kroah-Hartman 
4122620efefSGreg Kroah-Hartman 
41323681e47SGreg Kroah-Hartman static ssize_t show_dev(struct device *dev, struct device_attribute *attr,
41423681e47SGreg Kroah-Hartman 			char *buf)
41523681e47SGreg Kroah-Hartman {
41623681e47SGreg Kroah-Hartman 	return print_dev_t(buf, dev->devt);
41723681e47SGreg Kroah-Hartman }
41823681e47SGreg Kroah-Hartman 
419ad6a1e1cSTejun Heo static struct device_attribute devt_attr =
420ad6a1e1cSTejun Heo 	__ATTR(dev, S_IRUGO, show_dev, NULL);
421ad6a1e1cSTejun Heo 
422881c6cfdSGreg Kroah-Hartman /* kset to create /sys/devices/  */
423881c6cfdSGreg Kroah-Hartman struct kset *devices_kset;
4241da177e4SLinus Torvalds 
4251da177e4SLinus Torvalds /**
4261da177e4SLinus Torvalds  * device_create_file - create sysfs attribute file for device.
4271da177e4SLinus Torvalds  * @dev: device.
4281da177e4SLinus Torvalds  * @attr: device attribute descriptor.
4291da177e4SLinus Torvalds  */
4301da177e4SLinus Torvalds int device_create_file(struct device *dev, struct device_attribute *attr)
4311da177e4SLinus Torvalds {
4321da177e4SLinus Torvalds 	int error = 0;
4330c98b19fSCornelia Huck 	if (dev)
4341da177e4SLinus Torvalds 		error = sysfs_create_file(&dev->kobj, &attr->attr);
4351da177e4SLinus Torvalds 	return error;
4361da177e4SLinus Torvalds }
4371da177e4SLinus Torvalds 
4381da177e4SLinus Torvalds /**
4391da177e4SLinus Torvalds  * device_remove_file - remove sysfs attribute file.
4401da177e4SLinus Torvalds  * @dev: device.
4411da177e4SLinus Torvalds  * @attr: device attribute descriptor.
4421da177e4SLinus Torvalds  */
4431da177e4SLinus Torvalds void device_remove_file(struct device *dev, struct device_attribute *attr)
4441da177e4SLinus Torvalds {
4450c98b19fSCornelia Huck 	if (dev)
4461da177e4SLinus Torvalds 		sysfs_remove_file(&dev->kobj, &attr->attr);
4471da177e4SLinus Torvalds }
4481da177e4SLinus Torvalds 
4492589f188SGreg Kroah-Hartman /**
4502589f188SGreg Kroah-Hartman  * device_create_bin_file - create sysfs binary attribute file for device.
4512589f188SGreg Kroah-Hartman  * @dev: device.
4522589f188SGreg Kroah-Hartman  * @attr: device binary attribute descriptor.
4532589f188SGreg Kroah-Hartman  */
4542589f188SGreg Kroah-Hartman int device_create_bin_file(struct device *dev, struct bin_attribute *attr)
4552589f188SGreg Kroah-Hartman {
4562589f188SGreg Kroah-Hartman 	int error = -EINVAL;
4572589f188SGreg Kroah-Hartman 	if (dev)
4582589f188SGreg Kroah-Hartman 		error = sysfs_create_bin_file(&dev->kobj, attr);
4592589f188SGreg Kroah-Hartman 	return error;
4602589f188SGreg Kroah-Hartman }
4612589f188SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(device_create_bin_file);
4622589f188SGreg Kroah-Hartman 
4632589f188SGreg Kroah-Hartman /**
4642589f188SGreg Kroah-Hartman  * device_remove_bin_file - remove sysfs binary attribute file
4652589f188SGreg Kroah-Hartman  * @dev: device.
4662589f188SGreg Kroah-Hartman  * @attr: device binary attribute descriptor.
4672589f188SGreg Kroah-Hartman  */
4682589f188SGreg Kroah-Hartman void device_remove_bin_file(struct device *dev, struct bin_attribute *attr)
4692589f188SGreg Kroah-Hartman {
4702589f188SGreg Kroah-Hartman 	if (dev)
4712589f188SGreg Kroah-Hartman 		sysfs_remove_bin_file(&dev->kobj, attr);
4722589f188SGreg Kroah-Hartman }
4732589f188SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(device_remove_bin_file);
4742589f188SGreg Kroah-Hartman 
475d9a9cdfbSAlan Stern /**
476523ded71SAlan Stern  * device_schedule_callback_owner - helper to schedule a callback for a device
477d9a9cdfbSAlan Stern  * @dev: device.
478d9a9cdfbSAlan Stern  * @func: callback function to invoke later.
479523ded71SAlan Stern  * @owner: module owning the callback routine
480d9a9cdfbSAlan Stern  *
481d9a9cdfbSAlan Stern  * Attribute methods must not unregister themselves or their parent device
482d9a9cdfbSAlan Stern  * (which would amount to the same thing).  Attempts to do so will deadlock,
483d9a9cdfbSAlan Stern  * since unregistration is mutually exclusive with driver callbacks.
484d9a9cdfbSAlan Stern  *
485d9a9cdfbSAlan Stern  * Instead methods can call this routine, which will attempt to allocate
486d9a9cdfbSAlan Stern  * and schedule a workqueue request to call back @func with @dev as its
487d9a9cdfbSAlan Stern  * argument in the workqueue's process context.  @dev will be pinned until
488d9a9cdfbSAlan Stern  * @func returns.
489d9a9cdfbSAlan Stern  *
490523ded71SAlan Stern  * This routine is usually called via the inline device_schedule_callback(),
491523ded71SAlan Stern  * which automatically sets @owner to THIS_MODULE.
492523ded71SAlan Stern  *
493d9a9cdfbSAlan Stern  * Returns 0 if the request was submitted, -ENOMEM if storage could not
494523ded71SAlan Stern  * be allocated, -ENODEV if a reference to @owner isn't available.
495d9a9cdfbSAlan Stern  *
496d9a9cdfbSAlan Stern  * NOTE: This routine won't work if CONFIG_SYSFS isn't set!  It uses an
497d9a9cdfbSAlan Stern  * underlying sysfs routine (since it is intended for use by attribute
498d9a9cdfbSAlan Stern  * methods), and if sysfs isn't available you'll get nothing but -ENOSYS.
499d9a9cdfbSAlan Stern  */
500523ded71SAlan Stern int device_schedule_callback_owner(struct device *dev,
501523ded71SAlan Stern 		void (*func)(struct device *), struct module *owner)
502d9a9cdfbSAlan Stern {
503d9a9cdfbSAlan Stern 	return sysfs_schedule_callback(&dev->kobj,
504523ded71SAlan Stern 			(void (*)(void *)) func, dev, owner);
505d9a9cdfbSAlan Stern }
506523ded71SAlan Stern EXPORT_SYMBOL_GPL(device_schedule_callback_owner);
507d9a9cdfbSAlan Stern 
50834bb61f9SJames Bottomley static void klist_children_get(struct klist_node *n)
50934bb61f9SJames Bottomley {
510f791b8c8SGreg Kroah-Hartman 	struct device_private *p = to_device_private_parent(n);
511f791b8c8SGreg Kroah-Hartman 	struct device *dev = p->device;
51234bb61f9SJames Bottomley 
51334bb61f9SJames Bottomley 	get_device(dev);
51434bb61f9SJames Bottomley }
51534bb61f9SJames Bottomley 
51634bb61f9SJames Bottomley static void klist_children_put(struct klist_node *n)
51734bb61f9SJames Bottomley {
518f791b8c8SGreg Kroah-Hartman 	struct device_private *p = to_device_private_parent(n);
519f791b8c8SGreg Kroah-Hartman 	struct device *dev = p->device;
52034bb61f9SJames Bottomley 
52134bb61f9SJames Bottomley 	put_device(dev);
52234bb61f9SJames Bottomley }
52334bb61f9SJames Bottomley 
5241da177e4SLinus Torvalds /**
5251da177e4SLinus Torvalds  * device_initialize - init device structure.
5261da177e4SLinus Torvalds  * @dev: device.
5271da177e4SLinus Torvalds  *
5285739411aSCornelia Huck  * This prepares the device for use by other layers by initializing
5295739411aSCornelia Huck  * its fields.
5301da177e4SLinus Torvalds  * It is the first half of device_register(), if called by
5315739411aSCornelia Huck  * that function, though it can also be called separately, so one
5325739411aSCornelia Huck  * may use @dev's fields. In particular, get_device()/put_device()
5335739411aSCornelia Huck  * may be used for reference counting of @dev after calling this
5345739411aSCornelia Huck  * function.
5355739411aSCornelia Huck  *
5365739411aSCornelia Huck  * NOTE: Use put_device() to give up your reference instead of freeing
5375739411aSCornelia Huck  * @dev directly once you have called this function.
5381da177e4SLinus Torvalds  */
5391da177e4SLinus Torvalds void device_initialize(struct device *dev)
5401da177e4SLinus Torvalds {
541881c6cfdSGreg Kroah-Hartman 	dev->kobj.kset = devices_kset;
542f9cb074bSGreg Kroah-Hartman 	kobject_init(&dev->kobj, &device_ktype);
5431da177e4SLinus Torvalds 	INIT_LIST_HEAD(&dev->dma_pools);
544af70316aSmochel@digitalimplant.org 	init_MUTEX(&dev->sem);
5459ac7849eSTejun Heo 	spin_lock_init(&dev->devres_lock);
5469ac7849eSTejun Heo 	INIT_LIST_HEAD(&dev->devres_head);
5470ac85241SDavid Brownell 	device_init_wakeup(dev, 0);
5483b98aeafSAlan Stern 	device_pm_init(dev);
54987348136SChristoph Hellwig 	set_dev_node(dev, -1);
5501da177e4SLinus Torvalds }
5511da177e4SLinus Torvalds 
55240fa5422SGreg Kroah-Hartman #ifdef CONFIG_SYSFS_DEPRECATED
553c744aeaeSCornelia Huck static struct kobject *get_device_parent(struct device *dev,
554c744aeaeSCornelia Huck 					 struct device *parent)
55540fa5422SGreg Kroah-Hartman {
556da231fd5SKay Sievers 	/* class devices without a parent live in /sys/class/<classname>/ */
5573eb215deSDmitry Torokhov 	if (dev->class && (!parent || parent->class != dev->class))
5581fbfee6cSGreg Kroah-Hartman 		return &dev->class->p->class_subsys.kobj;
559da231fd5SKay Sievers 	/* all other devices keep their parent */
56040fa5422SGreg Kroah-Hartman 	else if (parent)
561c744aeaeSCornelia Huck 		return &parent->kobj;
56240fa5422SGreg Kroah-Hartman 
563c744aeaeSCornelia Huck 	return NULL;
56440fa5422SGreg Kroah-Hartman }
565da231fd5SKay Sievers 
566da231fd5SKay Sievers static inline void cleanup_device_parent(struct device *dev) {}
56763b6971aSCornelia Huck static inline void cleanup_glue_dir(struct device *dev,
56863b6971aSCornelia Huck 				    struct kobject *glue_dir) {}
56940fa5422SGreg Kroah-Hartman #else
570c744aeaeSCornelia Huck static struct kobject *virtual_device_parent(struct device *dev)
571f0ee61a6SGreg Kroah-Hartman {
572f0ee61a6SGreg Kroah-Hartman 	static struct kobject *virtual_dir = NULL;
573f0ee61a6SGreg Kroah-Hartman 
574f0ee61a6SGreg Kroah-Hartman 	if (!virtual_dir)
5754ff6abffSGreg Kroah-Hartman 		virtual_dir = kobject_create_and_add("virtual",
576881c6cfdSGreg Kroah-Hartman 						     &devices_kset->kobj);
577f0ee61a6SGreg Kroah-Hartman 
57886406245SKay Sievers 	return virtual_dir;
579f0ee61a6SGreg Kroah-Hartman }
580f0ee61a6SGreg Kroah-Hartman 
581c744aeaeSCornelia Huck static struct kobject *get_device_parent(struct device *dev,
582c744aeaeSCornelia Huck 					 struct device *parent)
58340fa5422SGreg Kroah-Hartman {
58443968d2fSGreg Kroah-Hartman 	int retval;
58543968d2fSGreg Kroah-Hartman 
58686406245SKay Sievers 	if (dev->class) {
58786406245SKay Sievers 		struct kobject *kobj = NULL;
58886406245SKay Sievers 		struct kobject *parent_kobj;
58986406245SKay Sievers 		struct kobject *k;
59086406245SKay Sievers 
59186406245SKay Sievers 		/*
59286406245SKay Sievers 		 * If we have no parent, we live in "virtual".
5930f4dafc0SKay Sievers 		 * Class-devices with a non class-device as parent, live
5940f4dafc0SKay Sievers 		 * in a "glue" directory to prevent namespace collisions.
59586406245SKay Sievers 		 */
59686406245SKay Sievers 		if (parent == NULL)
59786406245SKay Sievers 			parent_kobj = virtual_device_parent(dev);
59886406245SKay Sievers 		else if (parent->class)
59986406245SKay Sievers 			return &parent->kobj;
60086406245SKay Sievers 		else
60186406245SKay Sievers 			parent_kobj = &parent->kobj;
60286406245SKay Sievers 
60386406245SKay Sievers 		/* find our class-directory at the parent and reference it */
6047c71448bSGreg Kroah-Hartman 		spin_lock(&dev->class->p->class_dirs.list_lock);
6057c71448bSGreg Kroah-Hartman 		list_for_each_entry(k, &dev->class->p->class_dirs.list, entry)
60686406245SKay Sievers 			if (k->parent == parent_kobj) {
60786406245SKay Sievers 				kobj = kobject_get(k);
60886406245SKay Sievers 				break;
60986406245SKay Sievers 			}
6107c71448bSGreg Kroah-Hartman 		spin_unlock(&dev->class->p->class_dirs.list_lock);
61186406245SKay Sievers 		if (kobj)
61286406245SKay Sievers 			return kobj;
61386406245SKay Sievers 
61486406245SKay Sievers 		/* or create a new class-directory at the parent device */
61543968d2fSGreg Kroah-Hartman 		k = kobject_create();
61643968d2fSGreg Kroah-Hartman 		if (!k)
61743968d2fSGreg Kroah-Hartman 			return NULL;
6187c71448bSGreg Kroah-Hartman 		k->kset = &dev->class->p->class_dirs;
619b2d6db58SGreg Kroah-Hartman 		retval = kobject_add(k, parent_kobj, "%s", dev->class->name);
62043968d2fSGreg Kroah-Hartman 		if (retval < 0) {
62143968d2fSGreg Kroah-Hartman 			kobject_put(k);
62243968d2fSGreg Kroah-Hartman 			return NULL;
62343968d2fSGreg Kroah-Hartman 		}
6240f4dafc0SKay Sievers 		/* do not emit an uevent for this simple "glue" directory */
62543968d2fSGreg Kroah-Hartman 		return k;
62686406245SKay Sievers 	}
62786406245SKay Sievers 
62886406245SKay Sievers 	if (parent)
629c744aeaeSCornelia Huck 		return &parent->kobj;
630c744aeaeSCornelia Huck 	return NULL;
631c744aeaeSCornelia Huck }
632da231fd5SKay Sievers 
63363b6971aSCornelia Huck static void cleanup_glue_dir(struct device *dev, struct kobject *glue_dir)
634da231fd5SKay Sievers {
6350f4dafc0SKay Sievers 	/* see if we live in a "glue" directory */
636c1fe539aSCornelia Huck 	if (!glue_dir || !dev->class ||
6377c71448bSGreg Kroah-Hartman 	    glue_dir->kset != &dev->class->p->class_dirs)
638da231fd5SKay Sievers 		return;
639da231fd5SKay Sievers 
6400f4dafc0SKay Sievers 	kobject_put(glue_dir);
641da231fd5SKay Sievers }
64263b6971aSCornelia Huck 
64363b6971aSCornelia Huck static void cleanup_device_parent(struct device *dev)
64463b6971aSCornelia Huck {
64563b6971aSCornelia Huck 	cleanup_glue_dir(dev, dev->kobj.parent);
64663b6971aSCornelia Huck }
647c744aeaeSCornelia Huck #endif
64886406245SKay Sievers 
64963b6971aSCornelia Huck static void setup_parent(struct device *dev, struct device *parent)
650c744aeaeSCornelia Huck {
651c744aeaeSCornelia Huck 	struct kobject *kobj;
652c744aeaeSCornelia Huck 	kobj = get_device_parent(dev, parent);
653c744aeaeSCornelia Huck 	if (kobj)
654c744aeaeSCornelia Huck 		dev->kobj.parent = kobj;
65540fa5422SGreg Kroah-Hartman }
65640fa5422SGreg Kroah-Hartman 
6572ee97cafSCornelia Huck static int device_add_class_symlinks(struct device *dev)
6582ee97cafSCornelia Huck {
6592ee97cafSCornelia Huck 	int error;
6602ee97cafSCornelia Huck 
6612ee97cafSCornelia Huck 	if (!dev->class)
6622ee97cafSCornelia Huck 		return 0;
663da231fd5SKay Sievers 
6641fbfee6cSGreg Kroah-Hartman 	error = sysfs_create_link(&dev->kobj,
6651fbfee6cSGreg Kroah-Hartman 				  &dev->class->p->class_subsys.kobj,
6662ee97cafSCornelia Huck 				  "subsystem");
6672ee97cafSCornelia Huck 	if (error)
6682ee97cafSCornelia Huck 		goto out;
669da231fd5SKay Sievers 
670da231fd5SKay Sievers #ifdef CONFIG_SYSFS_DEPRECATED
671da231fd5SKay Sievers 	/* stacked class devices need a symlink in the class directory */
6721fbfee6cSGreg Kroah-Hartman 	if (dev->kobj.parent != &dev->class->p->class_subsys.kobj &&
6734e886c29SGreg Kroah-Hartman 	    device_is_not_partition(dev)) {
6741fbfee6cSGreg Kroah-Hartman 		error = sysfs_create_link(&dev->class->p->class_subsys.kobj,
6751e0b2cf9SKay Sievers 					  &dev->kobj, dev_name(dev));
6762ee97cafSCornelia Huck 		if (error)
6772ee97cafSCornelia Huck 			goto out_subsys;
6782ee97cafSCornelia Huck 	}
679da231fd5SKay Sievers 
6804e886c29SGreg Kroah-Hartman 	if (dev->parent && device_is_not_partition(dev)) {
6814f01a757SDmitry Torokhov 		struct device *parent = dev->parent;
6824f01a757SDmitry Torokhov 		char *class_name;
6834f01a757SDmitry Torokhov 
6844f01a757SDmitry Torokhov 		/*
685da231fd5SKay Sievers 		 * stacked class devices have the 'device' link
686da231fd5SKay Sievers 		 * pointing to the bus device instead of the parent
6874f01a757SDmitry Torokhov 		 */
6884f01a757SDmitry Torokhov 		while (parent->class && !parent->bus && parent->parent)
6894f01a757SDmitry Torokhov 			parent = parent->parent;
6904f01a757SDmitry Torokhov 
6914f01a757SDmitry Torokhov 		error = sysfs_create_link(&dev->kobj,
6924f01a757SDmitry Torokhov 					  &parent->kobj,
6932ee97cafSCornelia Huck 					  "device");
6942ee97cafSCornelia Huck 		if (error)
6952ee97cafSCornelia Huck 			goto out_busid;
6964f01a757SDmitry Torokhov 
6974f01a757SDmitry Torokhov 		class_name = make_class_name(dev->class->name,
6982ee97cafSCornelia Huck 						&dev->kobj);
6992ee97cafSCornelia Huck 		if (class_name)
7002ee97cafSCornelia Huck 			error = sysfs_create_link(&dev->parent->kobj,
7012ee97cafSCornelia Huck 						&dev->kobj, class_name);
7022ee97cafSCornelia Huck 		kfree(class_name);
7032ee97cafSCornelia Huck 		if (error)
7042ee97cafSCornelia Huck 			goto out_device;
7052ee97cafSCornelia Huck 	}
706da231fd5SKay Sievers 	return 0;
707da231fd5SKay Sievers 
708da231fd5SKay Sievers out_device:
7094e886c29SGreg Kroah-Hartman 	if (dev->parent && device_is_not_partition(dev))
710da231fd5SKay Sievers 		sysfs_remove_link(&dev->kobj, "device");
711da231fd5SKay Sievers out_busid:
7121fbfee6cSGreg Kroah-Hartman 	if (dev->kobj.parent != &dev->class->p->class_subsys.kobj &&
7134e886c29SGreg Kroah-Hartman 	    device_is_not_partition(dev))
7141fbfee6cSGreg Kroah-Hartman 		sysfs_remove_link(&dev->class->p->class_subsys.kobj,
7151e0b2cf9SKay Sievers 				  dev_name(dev));
7164f01a757SDmitry Torokhov #else
717da231fd5SKay Sievers 	/* link in the class directory pointing to the device */
7181fbfee6cSGreg Kroah-Hartman 	error = sysfs_create_link(&dev->class->p->class_subsys.kobj,
7191e0b2cf9SKay Sievers 				  &dev->kobj, dev_name(dev));
720da231fd5SKay Sievers 	if (error)
721da231fd5SKay Sievers 		goto out_subsys;
722da231fd5SKay Sievers 
7234e886c29SGreg Kroah-Hartman 	if (dev->parent && device_is_not_partition(dev)) {
7244f01a757SDmitry Torokhov 		error = sysfs_create_link(&dev->kobj, &dev->parent->kobj,
7254f01a757SDmitry Torokhov 					  "device");
7264f01a757SDmitry Torokhov 		if (error)
7274f01a757SDmitry Torokhov 			goto out_busid;
7282ee97cafSCornelia Huck 	}
7292ee97cafSCornelia Huck 	return 0;
7302ee97cafSCornelia Huck 
7312ee97cafSCornelia Huck out_busid:
7321e0b2cf9SKay Sievers 	sysfs_remove_link(&dev->class->p->class_subsys.kobj, dev_name(dev));
733da231fd5SKay Sievers #endif
734da231fd5SKay Sievers 
7352ee97cafSCornelia Huck out_subsys:
7362ee97cafSCornelia Huck 	sysfs_remove_link(&dev->kobj, "subsystem");
7372ee97cafSCornelia Huck out:
7382ee97cafSCornelia Huck 	return error;
7392ee97cafSCornelia Huck }
7402ee97cafSCornelia Huck 
7412ee97cafSCornelia Huck static void device_remove_class_symlinks(struct device *dev)
7422ee97cafSCornelia Huck {
7432ee97cafSCornelia Huck 	if (!dev->class)
7442ee97cafSCornelia Huck 		return;
745da231fd5SKay Sievers 
7462ee97cafSCornelia Huck #ifdef CONFIG_SYSFS_DEPRECATED
7474e886c29SGreg Kroah-Hartman 	if (dev->parent && device_is_not_partition(dev)) {
7482ee97cafSCornelia Huck 		char *class_name;
7492ee97cafSCornelia Huck 
7502ee97cafSCornelia Huck 		class_name = make_class_name(dev->class->name, &dev->kobj);
7512ee97cafSCornelia Huck 		if (class_name) {
7522ee97cafSCornelia Huck 			sysfs_remove_link(&dev->parent->kobj, class_name);
7532ee97cafSCornelia Huck 			kfree(class_name);
7542ee97cafSCornelia Huck 		}
7552ee97cafSCornelia Huck 		sysfs_remove_link(&dev->kobj, "device");
7562ee97cafSCornelia Huck 	}
757da231fd5SKay Sievers 
7581fbfee6cSGreg Kroah-Hartman 	if (dev->kobj.parent != &dev->class->p->class_subsys.kobj &&
7594e886c29SGreg Kroah-Hartman 	    device_is_not_partition(dev))
7601fbfee6cSGreg Kroah-Hartman 		sysfs_remove_link(&dev->class->p->class_subsys.kobj,
7611e0b2cf9SKay Sievers 				  dev_name(dev));
762da231fd5SKay Sievers #else
7634e886c29SGreg Kroah-Hartman 	if (dev->parent && device_is_not_partition(dev))
764da231fd5SKay Sievers 		sysfs_remove_link(&dev->kobj, "device");
765da231fd5SKay Sievers 
7661e0b2cf9SKay Sievers 	sysfs_remove_link(&dev->class->p->class_subsys.kobj, dev_name(dev));
767da231fd5SKay Sievers #endif
768da231fd5SKay Sievers 
7692ee97cafSCornelia Huck 	sysfs_remove_link(&dev->kobj, "subsystem");
7702ee97cafSCornelia Huck }
7712ee97cafSCornelia Huck 
7721da177e4SLinus Torvalds /**
773413c239fSStephen Rothwell  * dev_set_name - set a device name
774413c239fSStephen Rothwell  * @dev: device
77546232366SRandy Dunlap  * @fmt: format string for the device's name
776413c239fSStephen Rothwell  */
777413c239fSStephen Rothwell int dev_set_name(struct device *dev, const char *fmt, ...)
778413c239fSStephen Rothwell {
779413c239fSStephen Rothwell 	va_list vargs;
7801fa5ae85SKay Sievers 	int err;
781413c239fSStephen Rothwell 
782413c239fSStephen Rothwell 	va_start(vargs, fmt);
7831fa5ae85SKay Sievers 	err = kobject_set_name_vargs(&dev->kobj, fmt, vargs);
784413c239fSStephen Rothwell 	va_end(vargs);
7851fa5ae85SKay Sievers 	return err;
786413c239fSStephen Rothwell }
787413c239fSStephen Rothwell EXPORT_SYMBOL_GPL(dev_set_name);
788413c239fSStephen Rothwell 
789413c239fSStephen Rothwell /**
790e105b8bfSDan Williams  * device_to_dev_kobj - select a /sys/dev/ directory for the device
791e105b8bfSDan Williams  * @dev: device
792e105b8bfSDan Williams  *
793e105b8bfSDan Williams  * By default we select char/ for new entries.  Setting class->dev_obj
794e105b8bfSDan Williams  * to NULL prevents an entry from being created.  class->dev_kobj must
795e105b8bfSDan Williams  * be set (or cleared) before any devices are registered to the class
796e105b8bfSDan Williams  * otherwise device_create_sys_dev_entry() and
797e105b8bfSDan Williams  * device_remove_sys_dev_entry() will disagree about the the presence
798e105b8bfSDan Williams  * of the link.
799e105b8bfSDan Williams  */
800e105b8bfSDan Williams static struct kobject *device_to_dev_kobj(struct device *dev)
801e105b8bfSDan Williams {
802e105b8bfSDan Williams 	struct kobject *kobj;
803e105b8bfSDan Williams 
804e105b8bfSDan Williams 	if (dev->class)
805e105b8bfSDan Williams 		kobj = dev->class->dev_kobj;
806e105b8bfSDan Williams 	else
807e105b8bfSDan Williams 		kobj = sysfs_dev_char_kobj;
808e105b8bfSDan Williams 
809e105b8bfSDan Williams 	return kobj;
810e105b8bfSDan Williams }
811e105b8bfSDan Williams 
812e105b8bfSDan Williams static int device_create_sys_dev_entry(struct device *dev)
813e105b8bfSDan Williams {
814e105b8bfSDan Williams 	struct kobject *kobj = device_to_dev_kobj(dev);
815e105b8bfSDan Williams 	int error = 0;
816e105b8bfSDan Williams 	char devt_str[15];
817e105b8bfSDan Williams 
818e105b8bfSDan Williams 	if (kobj) {
819e105b8bfSDan Williams 		format_dev_t(devt_str, dev->devt);
820e105b8bfSDan Williams 		error = sysfs_create_link(kobj, &dev->kobj, devt_str);
821e105b8bfSDan Williams 	}
822e105b8bfSDan Williams 
823e105b8bfSDan Williams 	return error;
824e105b8bfSDan Williams }
825e105b8bfSDan Williams 
826e105b8bfSDan Williams static void device_remove_sys_dev_entry(struct device *dev)
827e105b8bfSDan Williams {
828e105b8bfSDan Williams 	struct kobject *kobj = device_to_dev_kobj(dev);
829e105b8bfSDan Williams 	char devt_str[15];
830e105b8bfSDan Williams 
831e105b8bfSDan Williams 	if (kobj) {
832e105b8bfSDan Williams 		format_dev_t(devt_str, dev->devt);
833e105b8bfSDan Williams 		sysfs_remove_link(kobj, devt_str);
834e105b8bfSDan Williams 	}
835e105b8bfSDan Williams }
836e105b8bfSDan Williams 
837e105b8bfSDan Williams /**
8381da177e4SLinus Torvalds  * device_add - add device to device hierarchy.
8391da177e4SLinus Torvalds  * @dev: device.
8401da177e4SLinus Torvalds  *
8411da177e4SLinus Torvalds  * This is part 2 of device_register(), though may be called
8421da177e4SLinus Torvalds  * separately _iff_ device_initialize() has been called separately.
8431da177e4SLinus Torvalds  *
8445739411aSCornelia Huck  * This adds @dev to the kobject hierarchy via kobject_add(), adds it
8451da177e4SLinus Torvalds  * to the global and sibling lists for the device, then
8461da177e4SLinus Torvalds  * adds it to the other relevant subsystems of the driver model.
8475739411aSCornelia Huck  *
8485739411aSCornelia Huck  * NOTE: _Never_ directly free @dev after calling this function, even
8495739411aSCornelia Huck  * if it returned an error! Always use put_device() to give up your
8505739411aSCornelia Huck  * reference instead.
8511da177e4SLinus Torvalds  */
8521da177e4SLinus Torvalds int device_add(struct device *dev)
8531da177e4SLinus Torvalds {
8541da177e4SLinus Torvalds 	struct device *parent = NULL;
855c47ed219SGreg Kroah-Hartman 	struct class_interface *class_intf;
856c906a48aSGreg Kroah-Hartman 	int error = -EINVAL;
857775b64d2SRafael J. Wysocki 
8581da177e4SLinus Torvalds 	dev = get_device(dev);
859c906a48aSGreg Kroah-Hartman 	if (!dev)
860c906a48aSGreg Kroah-Hartman 		goto done;
861c906a48aSGreg Kroah-Hartman 
862fb069a5dSGreg Kroah-Hartman 	dev->p = kzalloc(sizeof(*dev->p), GFP_KERNEL);
863fb069a5dSGreg Kroah-Hartman 	if (!dev->p) {
864fb069a5dSGreg Kroah-Hartman 		error = -ENOMEM;
865fb069a5dSGreg Kroah-Hartman 		goto done;
866fb069a5dSGreg Kroah-Hartman 	}
867fb069a5dSGreg Kroah-Hartman 	dev->p->device = dev;
868f791b8c8SGreg Kroah-Hartman 	klist_init(&dev->p->klist_children, klist_children_get,
869f791b8c8SGreg Kroah-Hartman 		   klist_children_put);
870fb069a5dSGreg Kroah-Hartman 
8711fa5ae85SKay Sievers 	/*
8721fa5ae85SKay Sievers 	 * for statically allocated devices, which should all be converted
8731fa5ae85SKay Sievers 	 * some day, we need to initialize the name. We prevent reading back
8741fa5ae85SKay Sievers 	 * the name, and force the use of dev_name()
8751fa5ae85SKay Sievers 	 */
8761fa5ae85SKay Sievers 	if (dev->init_name) {
8771fa5ae85SKay Sievers 		dev_set_name(dev, dev->init_name);
8781fa5ae85SKay Sievers 		dev->init_name = NULL;
8791fa5ae85SKay Sievers 	}
880c906a48aSGreg Kroah-Hartman 
8811fa5ae85SKay Sievers 	if (!dev_name(dev))
882c906a48aSGreg Kroah-Hartman 		goto done;
8831da177e4SLinus Torvalds 
8841e0b2cf9SKay Sievers 	pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
885c205ef48SGreg Kroah-Hartman 
8861da177e4SLinus Torvalds 	parent = get_device(dev->parent);
88763b6971aSCornelia Huck 	setup_parent(dev, parent);
8881da177e4SLinus Torvalds 
8890d358f22SYinghai Lu 	/* use parent numa_node */
8900d358f22SYinghai Lu 	if (parent)
8910d358f22SYinghai Lu 		set_dev_node(dev, dev_to_node(parent));
8920d358f22SYinghai Lu 
8931da177e4SLinus Torvalds 	/* first, register with generic layer. */
8941e0b2cf9SKay Sievers 	error = kobject_add(&dev->kobj, dev->kobj.parent, "%s", dev_name(dev));
89540fa5422SGreg Kroah-Hartman 	if (error)
8961da177e4SLinus Torvalds 		goto Error;
897a7fd6706SKay Sievers 
89837022644SBrian Walsh 	/* notify platform of device entry */
89937022644SBrian Walsh 	if (platform_notify)
90037022644SBrian Walsh 		platform_notify(dev);
90137022644SBrian Walsh 
902ad6a1e1cSTejun Heo 	error = device_create_file(dev, &uevent_attr);
903a306eea4SCornelia Huck 	if (error)
904a306eea4SCornelia Huck 		goto attrError;
905a7fd6706SKay Sievers 
90623681e47SGreg Kroah-Hartman 	if (MAJOR(dev->devt)) {
907ad6a1e1cSTejun Heo 		error = device_create_file(dev, &devt_attr);
908ad6a1e1cSTejun Heo 		if (error)
909a306eea4SCornelia Huck 			goto ueventattrError;
910e105b8bfSDan Williams 
911e105b8bfSDan Williams 		error = device_create_sys_dev_entry(dev);
912e105b8bfSDan Williams 		if (error)
913e105b8bfSDan Williams 			goto devtattrError;
91423681e47SGreg Kroah-Hartman 	}
91523681e47SGreg Kroah-Hartman 
9162ee97cafSCornelia Huck 	error = device_add_class_symlinks(dev);
9172ee97cafSCornelia Huck 	if (error)
9182ee97cafSCornelia Huck 		goto SymlinkError;
919dc0afa83SCornelia Huck 	error = device_add_attrs(dev);
920dc0afa83SCornelia Huck 	if (error)
9212620efefSGreg Kroah-Hartman 		goto AttrsError;
922dc0afa83SCornelia Huck 	error = bus_add_device(dev);
923dc0afa83SCornelia Huck 	if (error)
9241da177e4SLinus Torvalds 		goto BusError;
9253b98aeafSAlan Stern 	error = dpm_sysfs_add(dev);
92657eee3d2SRafael J. Wysocki 	if (error)
9273b98aeafSAlan Stern 		goto DPMError;
9283b98aeafSAlan Stern 	device_pm_add(dev);
929ec0676eeSAlan Stern 
930ec0676eeSAlan Stern 	/* Notify clients of device addition.  This call must come
931ec0676eeSAlan Stern 	 * after dpm_sysf_add() and before kobject_uevent().
932ec0676eeSAlan Stern 	 */
933ec0676eeSAlan Stern 	if (dev->bus)
934ec0676eeSAlan Stern 		blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
935ec0676eeSAlan Stern 					     BUS_NOTIFY_ADD_DEVICE, dev);
936ec0676eeSAlan Stern 
93753877d06SKay Sievers 	kobject_uevent(&dev->kobj, KOBJ_ADD);
938c6a46696SCornelia Huck 	bus_attach_device(dev);
9391da177e4SLinus Torvalds 	if (parent)
940f791b8c8SGreg Kroah-Hartman 		klist_add_tail(&dev->p->knode_parent,
941f791b8c8SGreg Kroah-Hartman 			       &parent->p->klist_children);
9421da177e4SLinus Torvalds 
9435d9fd169SGreg Kroah-Hartman 	if (dev->class) {
944f75b1c60SDave Young 		mutex_lock(&dev->class->p->class_mutex);
945c47ed219SGreg Kroah-Hartman 		/* tie the class to the device */
9465a3ceb86STejun Heo 		klist_add_tail(&dev->knode_class,
9475a3ceb86STejun Heo 			       &dev->class->p->class_devices);
948c47ed219SGreg Kroah-Hartman 
949c47ed219SGreg Kroah-Hartman 		/* notify any interfaces that the device is here */
950184f1f77SGreg Kroah-Hartman 		list_for_each_entry(class_intf,
951184f1f77SGreg Kroah-Hartman 				    &dev->class->p->class_interfaces, node)
952c47ed219SGreg Kroah-Hartman 			if (class_intf->add_dev)
953c47ed219SGreg Kroah-Hartman 				class_intf->add_dev(dev, class_intf);
954f75b1c60SDave Young 		mutex_unlock(&dev->class->p->class_mutex);
9555d9fd169SGreg Kroah-Hartman 	}
956c906a48aSGreg Kroah-Hartman done:
9571da177e4SLinus Torvalds 	put_device(dev);
9581da177e4SLinus Torvalds 	return error;
9593b98aeafSAlan Stern  DPMError:
96057eee3d2SRafael J. Wysocki 	bus_remove_device(dev);
96157eee3d2SRafael J. Wysocki  BusError:
9622620efefSGreg Kroah-Hartman 	device_remove_attrs(dev);
9632620efefSGreg Kroah-Hartman  AttrsError:
9642ee97cafSCornelia Huck 	device_remove_class_symlinks(dev);
9652ee97cafSCornelia Huck  SymlinkError:
966ad6a1e1cSTejun Heo 	if (MAJOR(dev->devt))
967e105b8bfSDan Williams 		device_remove_sys_dev_entry(dev);
968e105b8bfSDan Williams  devtattrError:
969e105b8bfSDan Williams 	if (MAJOR(dev->devt))
970ad6a1e1cSTejun Heo 		device_remove_file(dev, &devt_attr);
971a306eea4SCornelia Huck  ueventattrError:
972ad6a1e1cSTejun Heo 	device_remove_file(dev, &uevent_attr);
97323681e47SGreg Kroah-Hartman  attrError:
974312c004dSKay Sievers 	kobject_uevent(&dev->kobj, KOBJ_REMOVE);
9751da177e4SLinus Torvalds 	kobject_del(&dev->kobj);
9761da177e4SLinus Torvalds  Error:
97763b6971aSCornelia Huck 	cleanup_device_parent(dev);
9781da177e4SLinus Torvalds 	if (parent)
9791da177e4SLinus Torvalds 		put_device(parent);
980c906a48aSGreg Kroah-Hartman 	goto done;
9811da177e4SLinus Torvalds }
9821da177e4SLinus Torvalds 
9831da177e4SLinus Torvalds /**
9841da177e4SLinus Torvalds  * device_register - register a device with the system.
9851da177e4SLinus Torvalds  * @dev: pointer to the device structure
9861da177e4SLinus Torvalds  *
9871da177e4SLinus Torvalds  * This happens in two clean steps - initialize the device
9881da177e4SLinus Torvalds  * and add it to the system. The two steps can be called
9891da177e4SLinus Torvalds  * separately, but this is the easiest and most common.
9901da177e4SLinus Torvalds  * I.e. you should only call the two helpers separately if
9911da177e4SLinus Torvalds  * have a clearly defined need to use and refcount the device
9921da177e4SLinus Torvalds  * before it is added to the hierarchy.
9935739411aSCornelia Huck  *
9945739411aSCornelia Huck  * NOTE: _Never_ directly free @dev after calling this function, even
9955739411aSCornelia Huck  * if it returned an error! Always use put_device() to give up the
9965739411aSCornelia Huck  * reference initialized in this function instead.
9971da177e4SLinus Torvalds  */
9981da177e4SLinus Torvalds int device_register(struct device *dev)
9991da177e4SLinus Torvalds {
10001da177e4SLinus Torvalds 	device_initialize(dev);
10011da177e4SLinus Torvalds 	return device_add(dev);
10021da177e4SLinus Torvalds }
10031da177e4SLinus Torvalds 
10041da177e4SLinus Torvalds /**
10051da177e4SLinus Torvalds  * get_device - increment reference count for device.
10061da177e4SLinus Torvalds  * @dev: device.
10071da177e4SLinus Torvalds  *
10081da177e4SLinus Torvalds  * This simply forwards the call to kobject_get(), though
10091da177e4SLinus Torvalds  * we do take care to provide for the case that we get a NULL
10101da177e4SLinus Torvalds  * pointer passed in.
10111da177e4SLinus Torvalds  */
10121da177e4SLinus Torvalds struct device *get_device(struct device *dev)
10131da177e4SLinus Torvalds {
10141da177e4SLinus Torvalds 	return dev ? to_dev(kobject_get(&dev->kobj)) : NULL;
10151da177e4SLinus Torvalds }
10161da177e4SLinus Torvalds 
10171da177e4SLinus Torvalds /**
10181da177e4SLinus Torvalds  * put_device - decrement reference count.
10191da177e4SLinus Torvalds  * @dev: device in question.
10201da177e4SLinus Torvalds  */
10211da177e4SLinus Torvalds void put_device(struct device *dev)
10221da177e4SLinus Torvalds {
1023edfaa7c3SKay Sievers 	/* might_sleep(); */
10241da177e4SLinus Torvalds 	if (dev)
10251da177e4SLinus Torvalds 		kobject_put(&dev->kobj);
10261da177e4SLinus Torvalds }
10271da177e4SLinus Torvalds 
10281da177e4SLinus Torvalds /**
10291da177e4SLinus Torvalds  * device_del - delete device from system.
10301da177e4SLinus Torvalds  * @dev: device.
10311da177e4SLinus Torvalds  *
10321da177e4SLinus Torvalds  * This is the first part of the device unregistration
10331da177e4SLinus Torvalds  * sequence. This removes the device from the lists we control
10341da177e4SLinus Torvalds  * from here, has it removed from the other driver model
10351da177e4SLinus Torvalds  * subsystems it was added to in device_add(), and removes it
10361da177e4SLinus Torvalds  * from the kobject hierarchy.
10371da177e4SLinus Torvalds  *
10381da177e4SLinus Torvalds  * NOTE: this should be called manually _iff_ device_add() was
10391da177e4SLinus Torvalds  * also called manually.
10401da177e4SLinus Torvalds  */
10411da177e4SLinus Torvalds void device_del(struct device *dev)
10421da177e4SLinus Torvalds {
10431da177e4SLinus Torvalds 	struct device *parent = dev->parent;
1044c47ed219SGreg Kroah-Hartman 	struct class_interface *class_intf;
10451da177e4SLinus Torvalds 
1046ec0676eeSAlan Stern 	/* Notify clients of device removal.  This call must come
1047ec0676eeSAlan Stern 	 * before dpm_sysfs_remove().
1048ec0676eeSAlan Stern 	 */
1049ec0676eeSAlan Stern 	if (dev->bus)
1050ec0676eeSAlan Stern 		blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
1051ec0676eeSAlan Stern 					     BUS_NOTIFY_DEL_DEVICE, dev);
1052775b64d2SRafael J. Wysocki 	device_pm_remove(dev);
10533b98aeafSAlan Stern 	dpm_sysfs_remove(dev);
10541da177e4SLinus Torvalds 	if (parent)
1055f791b8c8SGreg Kroah-Hartman 		klist_del(&dev->p->knode_parent);
1056e105b8bfSDan Williams 	if (MAJOR(dev->devt)) {
1057e105b8bfSDan Williams 		device_remove_sys_dev_entry(dev);
1058ad6a1e1cSTejun Heo 		device_remove_file(dev, &devt_attr);
1059e105b8bfSDan Williams 	}
1060b9d9c82bSKay Sievers 	if (dev->class) {
1061da231fd5SKay Sievers 		device_remove_class_symlinks(dev);
106299ef3ef8SKay Sievers 
1063f75b1c60SDave Young 		mutex_lock(&dev->class->p->class_mutex);
1064c47ed219SGreg Kroah-Hartman 		/* notify any interfaces that the device is now gone */
1065184f1f77SGreg Kroah-Hartman 		list_for_each_entry(class_intf,
1066184f1f77SGreg Kroah-Hartman 				    &dev->class->p->class_interfaces, node)
1067c47ed219SGreg Kroah-Hartman 			if (class_intf->remove_dev)
1068c47ed219SGreg Kroah-Hartman 				class_intf->remove_dev(dev, class_intf);
1069c47ed219SGreg Kroah-Hartman 		/* remove the device from the class list */
10705a3ceb86STejun Heo 		klist_del(&dev->knode_class);
1071f75b1c60SDave Young 		mutex_unlock(&dev->class->p->class_mutex);
1072b9d9c82bSKay Sievers 	}
1073ad6a1e1cSTejun Heo 	device_remove_file(dev, &uevent_attr);
10742620efefSGreg Kroah-Hartman 	device_remove_attrs(dev);
107528953533SBenjamin Herrenschmidt 	bus_remove_device(dev);
10761da177e4SLinus Torvalds 
10772f8d16a9STejun Heo 	/*
10782f8d16a9STejun Heo 	 * Some platform devices are driven without driver attached
10792f8d16a9STejun Heo 	 * and managed resources may have been acquired.  Make sure
10802f8d16a9STejun Heo 	 * all resources are released.
10812f8d16a9STejun Heo 	 */
10822f8d16a9STejun Heo 	devres_release_all(dev);
10832f8d16a9STejun Heo 
10841da177e4SLinus Torvalds 	/* Notify the platform of the removal, in case they
10851da177e4SLinus Torvalds 	 * need to do anything...
10861da177e4SLinus Torvalds 	 */
10871da177e4SLinus Torvalds 	if (platform_notify_remove)
10881da177e4SLinus Torvalds 		platform_notify_remove(dev);
1089312c004dSKay Sievers 	kobject_uevent(&dev->kobj, KOBJ_REMOVE);
1090da231fd5SKay Sievers 	cleanup_device_parent(dev);
10911da177e4SLinus Torvalds 	kobject_del(&dev->kobj);
10921da177e4SLinus Torvalds 	put_device(parent);
10931da177e4SLinus Torvalds }
10941da177e4SLinus Torvalds 
10951da177e4SLinus Torvalds /**
10961da177e4SLinus Torvalds  * device_unregister - unregister device from system.
10971da177e4SLinus Torvalds  * @dev: device going away.
10981da177e4SLinus Torvalds  *
10991da177e4SLinus Torvalds  * We do this in two parts, like we do device_register(). First,
11001da177e4SLinus Torvalds  * we remove it from all the subsystems with device_del(), then
11011da177e4SLinus Torvalds  * we decrement the reference count via put_device(). If that
11021da177e4SLinus Torvalds  * is the final reference count, the device will be cleaned up
11031da177e4SLinus Torvalds  * via device_release() above. Otherwise, the structure will
11041da177e4SLinus Torvalds  * stick around until the final reference to the device is dropped.
11051da177e4SLinus Torvalds  */
11061da177e4SLinus Torvalds void device_unregister(struct device *dev)
11071da177e4SLinus Torvalds {
11081e0b2cf9SKay Sievers 	pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
11091da177e4SLinus Torvalds 	device_del(dev);
11101da177e4SLinus Torvalds 	put_device(dev);
11111da177e4SLinus Torvalds }
11121da177e4SLinus Torvalds 
111336239577Smochel@digitalimplant.org static struct device *next_device(struct klist_iter *i)
111436239577Smochel@digitalimplant.org {
111536239577Smochel@digitalimplant.org 	struct klist_node *n = klist_next(i);
1116f791b8c8SGreg Kroah-Hartman 	struct device *dev = NULL;
1117f791b8c8SGreg Kroah-Hartman 	struct device_private *p;
1118f791b8c8SGreg Kroah-Hartman 
1119f791b8c8SGreg Kroah-Hartman 	if (n) {
1120f791b8c8SGreg Kroah-Hartman 		p = to_device_private_parent(n);
1121f791b8c8SGreg Kroah-Hartman 		dev = p->device;
1122f791b8c8SGreg Kroah-Hartman 	}
1123f791b8c8SGreg Kroah-Hartman 	return dev;
112436239577Smochel@digitalimplant.org }
112536239577Smochel@digitalimplant.org 
11261da177e4SLinus Torvalds /**
11271da177e4SLinus Torvalds  * device_for_each_child - device child iterator.
1128c41455fbSRandy Dunlap  * @parent: parent struct device.
11291da177e4SLinus Torvalds  * @data: data for the callback.
11301da177e4SLinus Torvalds  * @fn: function to be called for each device.
11311da177e4SLinus Torvalds  *
1132c41455fbSRandy Dunlap  * Iterate over @parent's child devices, and call @fn for each,
11331da177e4SLinus Torvalds  * passing it @data.
11341da177e4SLinus Torvalds  *
11351da177e4SLinus Torvalds  * We check the return of @fn each time. If it returns anything
11361da177e4SLinus Torvalds  * other than 0, we break out and return that value.
11371da177e4SLinus Torvalds  */
113836239577Smochel@digitalimplant.org int device_for_each_child(struct device *parent, void *data,
11394a3ad20cSGreg Kroah-Hartman 			  int (*fn)(struct device *dev, void *data))
11401da177e4SLinus Torvalds {
114136239577Smochel@digitalimplant.org 	struct klist_iter i;
11421da177e4SLinus Torvalds 	struct device *child;
11431da177e4SLinus Torvalds 	int error = 0;
11441da177e4SLinus Torvalds 
1145f791b8c8SGreg Kroah-Hartman 	klist_iter_init(&parent->p->klist_children, &i);
114636239577Smochel@digitalimplant.org 	while ((child = next_device(&i)) && !error)
114736239577Smochel@digitalimplant.org 		error = fn(child, data);
114836239577Smochel@digitalimplant.org 	klist_iter_exit(&i);
11491da177e4SLinus Torvalds 	return error;
11501da177e4SLinus Torvalds }
11511da177e4SLinus Torvalds 
11525ab69981SCornelia Huck /**
11535ab69981SCornelia Huck  * device_find_child - device iterator for locating a particular device.
11545ab69981SCornelia Huck  * @parent: parent struct device
11555ab69981SCornelia Huck  * @data: Data to pass to match function
11565ab69981SCornelia Huck  * @match: Callback function to check device
11575ab69981SCornelia Huck  *
11585ab69981SCornelia Huck  * This is similar to the device_for_each_child() function above, but it
11595ab69981SCornelia Huck  * returns a reference to a device that is 'found' for later use, as
11605ab69981SCornelia Huck  * determined by the @match callback.
11615ab69981SCornelia Huck  *
11625ab69981SCornelia Huck  * The callback should return 0 if the device doesn't match and non-zero
11635ab69981SCornelia Huck  * if it does.  If the callback returns non-zero and a reference to the
11645ab69981SCornelia Huck  * current device can be obtained, this function will return to the caller
11655ab69981SCornelia Huck  * and not iterate over any more devices.
11665ab69981SCornelia Huck  */
11675ab69981SCornelia Huck struct device *device_find_child(struct device *parent, void *data,
11684a3ad20cSGreg Kroah-Hartman 				 int (*match)(struct device *dev, void *data))
11695ab69981SCornelia Huck {
11705ab69981SCornelia Huck 	struct klist_iter i;
11715ab69981SCornelia Huck 	struct device *child;
11725ab69981SCornelia Huck 
11735ab69981SCornelia Huck 	if (!parent)
11745ab69981SCornelia Huck 		return NULL;
11755ab69981SCornelia Huck 
1176f791b8c8SGreg Kroah-Hartman 	klist_iter_init(&parent->p->klist_children, &i);
11775ab69981SCornelia Huck 	while ((child = next_device(&i)))
11785ab69981SCornelia Huck 		if (match(child, data) && get_device(child))
11795ab69981SCornelia Huck 			break;
11805ab69981SCornelia Huck 	klist_iter_exit(&i);
11815ab69981SCornelia Huck 	return child;
11825ab69981SCornelia Huck }
11835ab69981SCornelia Huck 
11841da177e4SLinus Torvalds int __init devices_init(void)
11851da177e4SLinus Torvalds {
1186881c6cfdSGreg Kroah-Hartman 	devices_kset = kset_create_and_add("devices", &device_uevent_ops, NULL);
1187881c6cfdSGreg Kroah-Hartman 	if (!devices_kset)
1188881c6cfdSGreg Kroah-Hartman 		return -ENOMEM;
1189e105b8bfSDan Williams 	dev_kobj = kobject_create_and_add("dev", NULL);
1190e105b8bfSDan Williams 	if (!dev_kobj)
1191e105b8bfSDan Williams 		goto dev_kobj_err;
1192e105b8bfSDan Williams 	sysfs_dev_block_kobj = kobject_create_and_add("block", dev_kobj);
1193e105b8bfSDan Williams 	if (!sysfs_dev_block_kobj)
1194e105b8bfSDan Williams 		goto block_kobj_err;
1195e105b8bfSDan Williams 	sysfs_dev_char_kobj = kobject_create_and_add("char", dev_kobj);
1196e105b8bfSDan Williams 	if (!sysfs_dev_char_kobj)
1197e105b8bfSDan Williams 		goto char_kobj_err;
1198e105b8bfSDan Williams 
1199881c6cfdSGreg Kroah-Hartman 	return 0;
1200e105b8bfSDan Williams 
1201e105b8bfSDan Williams  char_kobj_err:
1202e105b8bfSDan Williams 	kobject_put(sysfs_dev_block_kobj);
1203e105b8bfSDan Williams  block_kobj_err:
1204e105b8bfSDan Williams 	kobject_put(dev_kobj);
1205e105b8bfSDan Williams  dev_kobj_err:
1206e105b8bfSDan Williams 	kset_unregister(devices_kset);
1207e105b8bfSDan Williams 	return -ENOMEM;
12081da177e4SLinus Torvalds }
12091da177e4SLinus Torvalds 
12101da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(device_for_each_child);
12115ab69981SCornelia Huck EXPORT_SYMBOL_GPL(device_find_child);
12121da177e4SLinus Torvalds 
12131da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(device_initialize);
12141da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(device_add);
12151da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(device_register);
12161da177e4SLinus Torvalds 
12171da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(device_del);
12181da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(device_unregister);
12191da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(get_device);
12201da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(put_device);
12211da177e4SLinus Torvalds 
12221da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(device_create_file);
12231da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(device_remove_file);
122423681e47SGreg Kroah-Hartman 
12250aa0dc41SMark McLoughlin struct root_device
12260aa0dc41SMark McLoughlin {
12270aa0dc41SMark McLoughlin 	struct device dev;
12280aa0dc41SMark McLoughlin 	struct module *owner;
12290aa0dc41SMark McLoughlin };
12300aa0dc41SMark McLoughlin 
12310aa0dc41SMark McLoughlin #define to_root_device(dev) container_of(dev, struct root_device, dev)
12320aa0dc41SMark McLoughlin 
12330aa0dc41SMark McLoughlin static void root_device_release(struct device *dev)
12340aa0dc41SMark McLoughlin {
12350aa0dc41SMark McLoughlin 	kfree(to_root_device(dev));
12360aa0dc41SMark McLoughlin }
12370aa0dc41SMark McLoughlin 
12380aa0dc41SMark McLoughlin /**
12390aa0dc41SMark McLoughlin  * __root_device_register - allocate and register a root device
12400aa0dc41SMark McLoughlin  * @name: root device name
12410aa0dc41SMark McLoughlin  * @owner: owner module of the root device, usually THIS_MODULE
12420aa0dc41SMark McLoughlin  *
12430aa0dc41SMark McLoughlin  * This function allocates a root device and registers it
12440aa0dc41SMark McLoughlin  * using device_register(). In order to free the returned
12450aa0dc41SMark McLoughlin  * device, use root_device_unregister().
12460aa0dc41SMark McLoughlin  *
12470aa0dc41SMark McLoughlin  * Root devices are dummy devices which allow other devices
12480aa0dc41SMark McLoughlin  * to be grouped under /sys/devices. Use this function to
12490aa0dc41SMark McLoughlin  * allocate a root device and then use it as the parent of
12500aa0dc41SMark McLoughlin  * any device which should appear under /sys/devices/{name}
12510aa0dc41SMark McLoughlin  *
12520aa0dc41SMark McLoughlin  * The /sys/devices/{name} directory will also contain a
12530aa0dc41SMark McLoughlin  * 'module' symlink which points to the @owner directory
12540aa0dc41SMark McLoughlin  * in sysfs.
12550aa0dc41SMark McLoughlin  *
12560aa0dc41SMark McLoughlin  * Note: You probably want to use root_device_register().
12570aa0dc41SMark McLoughlin  */
12580aa0dc41SMark McLoughlin struct device *__root_device_register(const char *name, struct module *owner)
12590aa0dc41SMark McLoughlin {
12600aa0dc41SMark McLoughlin 	struct root_device *root;
12610aa0dc41SMark McLoughlin 	int err = -ENOMEM;
12620aa0dc41SMark McLoughlin 
12630aa0dc41SMark McLoughlin 	root = kzalloc(sizeof(struct root_device), GFP_KERNEL);
12640aa0dc41SMark McLoughlin 	if (!root)
12650aa0dc41SMark McLoughlin 		return ERR_PTR(err);
12660aa0dc41SMark McLoughlin 
12670aa0dc41SMark McLoughlin 	err = dev_set_name(&root->dev, name);
12680aa0dc41SMark McLoughlin 	if (err) {
12690aa0dc41SMark McLoughlin 		kfree(root);
12700aa0dc41SMark McLoughlin 		return ERR_PTR(err);
12710aa0dc41SMark McLoughlin 	}
12720aa0dc41SMark McLoughlin 
12730aa0dc41SMark McLoughlin 	root->dev.release = root_device_release;
12740aa0dc41SMark McLoughlin 
12750aa0dc41SMark McLoughlin 	err = device_register(&root->dev);
12760aa0dc41SMark McLoughlin 	if (err) {
12770aa0dc41SMark McLoughlin 		put_device(&root->dev);
12780aa0dc41SMark McLoughlin 		return ERR_PTR(err);
12790aa0dc41SMark McLoughlin 	}
12800aa0dc41SMark McLoughlin 
12810aa0dc41SMark McLoughlin #ifdef CONFIG_MODULE	/* gotta find a "cleaner" way to do this */
12820aa0dc41SMark McLoughlin 	if (owner) {
12830aa0dc41SMark McLoughlin 		struct module_kobject *mk = &owner->mkobj;
12840aa0dc41SMark McLoughlin 
12850aa0dc41SMark McLoughlin 		err = sysfs_create_link(&root->dev.kobj, &mk->kobj, "module");
12860aa0dc41SMark McLoughlin 		if (err) {
12870aa0dc41SMark McLoughlin 			device_unregister(&root->dev);
12880aa0dc41SMark McLoughlin 			return ERR_PTR(err);
12890aa0dc41SMark McLoughlin 		}
12900aa0dc41SMark McLoughlin 		root->owner = owner;
12910aa0dc41SMark McLoughlin 	}
12920aa0dc41SMark McLoughlin #endif
12930aa0dc41SMark McLoughlin 
12940aa0dc41SMark McLoughlin 	return &root->dev;
12950aa0dc41SMark McLoughlin }
12960aa0dc41SMark McLoughlin EXPORT_SYMBOL_GPL(__root_device_register);
12970aa0dc41SMark McLoughlin 
12980aa0dc41SMark McLoughlin /**
12990aa0dc41SMark McLoughlin  * root_device_unregister - unregister and free a root device
13007cbcf225SRandy Dunlap  * @dev: device going away
13010aa0dc41SMark McLoughlin  *
13020aa0dc41SMark McLoughlin  * This function unregisters and cleans up a device that was created by
13030aa0dc41SMark McLoughlin  * root_device_register().
13040aa0dc41SMark McLoughlin  */
13050aa0dc41SMark McLoughlin void root_device_unregister(struct device *dev)
13060aa0dc41SMark McLoughlin {
13070aa0dc41SMark McLoughlin 	struct root_device *root = to_root_device(dev);
13080aa0dc41SMark McLoughlin 
13090aa0dc41SMark McLoughlin 	if (root->owner)
13100aa0dc41SMark McLoughlin 		sysfs_remove_link(&root->dev.kobj, "module");
13110aa0dc41SMark McLoughlin 
13120aa0dc41SMark McLoughlin 	device_unregister(dev);
13130aa0dc41SMark McLoughlin }
13140aa0dc41SMark McLoughlin EXPORT_SYMBOL_GPL(root_device_unregister);
13150aa0dc41SMark McLoughlin 
131623681e47SGreg Kroah-Hartman 
131723681e47SGreg Kroah-Hartman static void device_create_release(struct device *dev)
131823681e47SGreg Kroah-Hartman {
13191e0b2cf9SKay Sievers 	pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
132023681e47SGreg Kroah-Hartman 	kfree(dev);
132123681e47SGreg Kroah-Hartman }
132223681e47SGreg Kroah-Hartman 
132323681e47SGreg Kroah-Hartman /**
13248882b394SGreg Kroah-Hartman  * device_create_vargs - creates a device and registers it with sysfs
13258882b394SGreg Kroah-Hartman  * @class: pointer to the struct class that this device should be registered to
13268882b394SGreg Kroah-Hartman  * @parent: pointer to the parent struct device of this new device, if any
13278882b394SGreg Kroah-Hartman  * @devt: the dev_t for the char device to be added
13288882b394SGreg Kroah-Hartman  * @drvdata: the data to be added to the device for callbacks
13298882b394SGreg Kroah-Hartman  * @fmt: string for the device's name
13308882b394SGreg Kroah-Hartman  * @args: va_list for the device's name
13318882b394SGreg Kroah-Hartman  *
13328882b394SGreg Kroah-Hartman  * This function can be used by char device classes.  A struct device
13338882b394SGreg Kroah-Hartman  * will be created in sysfs, registered to the specified class.
13348882b394SGreg Kroah-Hartman  *
13358882b394SGreg Kroah-Hartman  * A "dev" file will be created, showing the dev_t for the device, if
13368882b394SGreg Kroah-Hartman  * the dev_t is not 0,0.
13378882b394SGreg Kroah-Hartman  * If a pointer to a parent struct device is passed in, the newly created
13388882b394SGreg Kroah-Hartman  * struct device will be a child of that device in sysfs.
13398882b394SGreg Kroah-Hartman  * The pointer to the struct device will be returned from the call.
13408882b394SGreg Kroah-Hartman  * Any further sysfs files that might be required can be created using this
13418882b394SGreg Kroah-Hartman  * pointer.
13428882b394SGreg Kroah-Hartman  *
13438882b394SGreg Kroah-Hartman  * Note: the struct class passed to this function must have previously
13448882b394SGreg Kroah-Hartman  * been created with a call to class_create().
13458882b394SGreg Kroah-Hartman  */
13468882b394SGreg Kroah-Hartman struct device *device_create_vargs(struct class *class, struct device *parent,
13478882b394SGreg Kroah-Hartman 				   dev_t devt, void *drvdata, const char *fmt,
13488882b394SGreg Kroah-Hartman 				   va_list args)
13498882b394SGreg Kroah-Hartman {
13508882b394SGreg Kroah-Hartman 	struct device *dev = NULL;
13518882b394SGreg Kroah-Hartman 	int retval = -ENODEV;
13528882b394SGreg Kroah-Hartman 
13538882b394SGreg Kroah-Hartman 	if (class == NULL || IS_ERR(class))
13548882b394SGreg Kroah-Hartman 		goto error;
13558882b394SGreg Kroah-Hartman 
13568882b394SGreg Kroah-Hartman 	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
13578882b394SGreg Kroah-Hartman 	if (!dev) {
13588882b394SGreg Kroah-Hartman 		retval = -ENOMEM;
13598882b394SGreg Kroah-Hartman 		goto error;
13608882b394SGreg Kroah-Hartman 	}
13618882b394SGreg Kroah-Hartman 
13628882b394SGreg Kroah-Hartman 	dev->devt = devt;
13638882b394SGreg Kroah-Hartman 	dev->class = class;
13648882b394SGreg Kroah-Hartman 	dev->parent = parent;
13658882b394SGreg Kroah-Hartman 	dev->release = device_create_release;
13668882b394SGreg Kroah-Hartman 	dev_set_drvdata(dev, drvdata);
13678882b394SGreg Kroah-Hartman 
13681fa5ae85SKay Sievers 	retval = kobject_set_name_vargs(&dev->kobj, fmt, args);
13691fa5ae85SKay Sievers 	if (retval)
13701fa5ae85SKay Sievers 		goto error;
13711fa5ae85SKay Sievers 
13728882b394SGreg Kroah-Hartman 	retval = device_register(dev);
13738882b394SGreg Kroah-Hartman 	if (retval)
13748882b394SGreg Kroah-Hartman 		goto error;
13758882b394SGreg Kroah-Hartman 
13768882b394SGreg Kroah-Hartman 	return dev;
13778882b394SGreg Kroah-Hartman 
13788882b394SGreg Kroah-Hartman error:
1379286661b3SCornelia Huck 	put_device(dev);
13808882b394SGreg Kroah-Hartman 	return ERR_PTR(retval);
13818882b394SGreg Kroah-Hartman }
13828882b394SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(device_create_vargs);
13838882b394SGreg Kroah-Hartman 
13848882b394SGreg Kroah-Hartman /**
13854e106739SGreg Kroah-Hartman  * device_create - creates a device and registers it with sysfs
13868882b394SGreg Kroah-Hartman  * @class: pointer to the struct class that this device should be registered to
13878882b394SGreg Kroah-Hartman  * @parent: pointer to the parent struct device of this new device, if any
13888882b394SGreg Kroah-Hartman  * @devt: the dev_t for the char device to be added
13898882b394SGreg Kroah-Hartman  * @drvdata: the data to be added to the device for callbacks
13908882b394SGreg Kroah-Hartman  * @fmt: string for the device's name
13918882b394SGreg Kroah-Hartman  *
13928882b394SGreg Kroah-Hartman  * This function can be used by char device classes.  A struct device
13938882b394SGreg Kroah-Hartman  * will be created in sysfs, registered to the specified class.
13948882b394SGreg Kroah-Hartman  *
13958882b394SGreg Kroah-Hartman  * A "dev" file will be created, showing the dev_t for the device, if
13968882b394SGreg Kroah-Hartman  * the dev_t is not 0,0.
13978882b394SGreg Kroah-Hartman  * If a pointer to a parent struct device is passed in, the newly created
13988882b394SGreg Kroah-Hartman  * struct device will be a child of that device in sysfs.
13998882b394SGreg Kroah-Hartman  * The pointer to the struct device will be returned from the call.
14008882b394SGreg Kroah-Hartman  * Any further sysfs files that might be required can be created using this
14018882b394SGreg Kroah-Hartman  * pointer.
14028882b394SGreg Kroah-Hartman  *
14038882b394SGreg Kroah-Hartman  * Note: the struct class passed to this function must have previously
14048882b394SGreg Kroah-Hartman  * been created with a call to class_create().
14058882b394SGreg Kroah-Hartman  */
14064e106739SGreg Kroah-Hartman struct device *device_create(struct class *class, struct device *parent,
14074e106739SGreg Kroah-Hartman 			     dev_t devt, void *drvdata, const char *fmt, ...)
14088882b394SGreg Kroah-Hartman {
14098882b394SGreg Kroah-Hartman 	va_list vargs;
14108882b394SGreg Kroah-Hartman 	struct device *dev;
14118882b394SGreg Kroah-Hartman 
14128882b394SGreg Kroah-Hartman 	va_start(vargs, fmt);
14138882b394SGreg Kroah-Hartman 	dev = device_create_vargs(class, parent, devt, drvdata, fmt, vargs);
14148882b394SGreg Kroah-Hartman 	va_end(vargs);
14158882b394SGreg Kroah-Hartman 	return dev;
14168882b394SGreg Kroah-Hartman }
14174e106739SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(device_create);
14188882b394SGreg Kroah-Hartman 
1419cd35449bSDave Young static int __match_devt(struct device *dev, void *data)
142023681e47SGreg Kroah-Hartman {
1421cd35449bSDave Young 	dev_t *devt = data;
142223681e47SGreg Kroah-Hartman 
1423cd35449bSDave Young 	return dev->devt == *devt;
1424775b64d2SRafael J. Wysocki }
142523681e47SGreg Kroah-Hartman 
1426775b64d2SRafael J. Wysocki /**
1427775b64d2SRafael J. Wysocki  * device_destroy - removes a device that was created with device_create()
1428775b64d2SRafael J. Wysocki  * @class: pointer to the struct class that this device was registered with
1429775b64d2SRafael J. Wysocki  * @devt: the dev_t of the device that was previously registered
1430775b64d2SRafael J. Wysocki  *
1431775b64d2SRafael J. Wysocki  * This call unregisters and cleans up a device that was created with a
1432775b64d2SRafael J. Wysocki  * call to device_create().
1433775b64d2SRafael J. Wysocki  */
1434775b64d2SRafael J. Wysocki void device_destroy(struct class *class, dev_t devt)
1435775b64d2SRafael J. Wysocki {
1436775b64d2SRafael J. Wysocki 	struct device *dev;
1437775b64d2SRafael J. Wysocki 
1438695794aeSGreg Kroah-Hartman 	dev = class_find_device(class, NULL, &devt, __match_devt);
1439cd35449bSDave Young 	if (dev) {
1440cd35449bSDave Young 		put_device(dev);
144123681e47SGreg Kroah-Hartman 		device_unregister(dev);
144223681e47SGreg Kroah-Hartman 	}
1443cd35449bSDave Young }
144423681e47SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(device_destroy);
1445a2de48caSGreg Kroah-Hartman 
1446a2de48caSGreg Kroah-Hartman /**
1447a2de48caSGreg Kroah-Hartman  * device_rename - renames a device
1448a2de48caSGreg Kroah-Hartman  * @dev: the pointer to the struct device to be renamed
1449a2de48caSGreg Kroah-Hartman  * @new_name: the new name of the device
1450030c1d2bSEric W. Biederman  *
1451030c1d2bSEric W. Biederman  * It is the responsibility of the caller to provide mutual
1452030c1d2bSEric W. Biederman  * exclusion between two different calls of device_rename
1453030c1d2bSEric W. Biederman  * on the same device to ensure that new_name is valid and
1454030c1d2bSEric W. Biederman  * won't conflict with other devices.
1455a2de48caSGreg Kroah-Hartman  */
1456a2de48caSGreg Kroah-Hartman int device_rename(struct device *dev, char *new_name)
1457a2de48caSGreg Kroah-Hartman {
1458a2de48caSGreg Kroah-Hartman 	char *old_class_name = NULL;
1459a2de48caSGreg Kroah-Hartman 	char *new_class_name = NULL;
14602ee97cafSCornelia Huck 	char *old_device_name = NULL;
1461a2de48caSGreg Kroah-Hartman 	int error;
1462a2de48caSGreg Kroah-Hartman 
1463a2de48caSGreg Kroah-Hartman 	dev = get_device(dev);
1464a2de48caSGreg Kroah-Hartman 	if (!dev)
1465a2de48caSGreg Kroah-Hartman 		return -EINVAL;
1466a2de48caSGreg Kroah-Hartman 
14671e0b2cf9SKay Sievers 	pr_debug("device: '%s': %s: renaming to '%s'\n", dev_name(dev),
14682b3a302aSHarvey Harrison 		 __func__, new_name);
1469a2de48caSGreg Kroah-Hartman 
147099ef3ef8SKay Sievers #ifdef CONFIG_SYSFS_DEPRECATED
1471a2de48caSGreg Kroah-Hartman 	if ((dev->class) && (dev->parent))
1472a2de48caSGreg Kroah-Hartman 		old_class_name = make_class_name(dev->class->name, &dev->kobj);
147399ef3ef8SKay Sievers #endif
1474a2de48caSGreg Kroah-Hartman 
14751fa5ae85SKay Sievers 	old_device_name = kstrdup(dev_name(dev), GFP_KERNEL);
14762ee97cafSCornelia Huck 	if (!old_device_name) {
1477952ab431SJesper Juhl 		error = -ENOMEM;
14782ee97cafSCornelia Huck 		goto out;
1479952ab431SJesper Juhl 	}
1480a2de48caSGreg Kroah-Hartman 
1481a2de48caSGreg Kroah-Hartman 	error = kobject_rename(&dev->kobj, new_name);
14821fa5ae85SKay Sievers 	if (error)
14832ee97cafSCornelia Huck 		goto out;
1484a2de48caSGreg Kroah-Hartman 
148599ef3ef8SKay Sievers #ifdef CONFIG_SYSFS_DEPRECATED
1486a2de48caSGreg Kroah-Hartman 	if (old_class_name) {
1487a2de48caSGreg Kroah-Hartman 		new_class_name = make_class_name(dev->class->name, &dev->kobj);
1488a2de48caSGreg Kroah-Hartman 		if (new_class_name) {
148936ce6dadSCornelia Huck 			error = sysfs_create_link_nowarn(&dev->parent->kobj,
149036ce6dadSCornelia Huck 							 &dev->kobj,
149136ce6dadSCornelia Huck 							 new_class_name);
14922ee97cafSCornelia Huck 			if (error)
14932ee97cafSCornelia Huck 				goto out;
1494a2de48caSGreg Kroah-Hartman 			sysfs_remove_link(&dev->parent->kobj, old_class_name);
1495a2de48caSGreg Kroah-Hartman 		}
1496a2de48caSGreg Kroah-Hartman 	}
149760b8cabdSKay Sievers #else
1498a2de48caSGreg Kroah-Hartman 	if (dev->class) {
149936ce6dadSCornelia Huck 		error = sysfs_create_link_nowarn(&dev->class->p->class_subsys.kobj,
15001e0b2cf9SKay Sievers 						 &dev->kobj, dev_name(dev));
15010599ad53SStephen Hemminger 		if (error)
15020599ad53SStephen Hemminger 			goto out;
15031fbfee6cSGreg Kroah-Hartman 		sysfs_remove_link(&dev->class->p->class_subsys.kobj,
15047c71448bSGreg Kroah-Hartman 				  old_device_name);
15052ee97cafSCornelia Huck 	}
150660b8cabdSKay Sievers #endif
150760b8cabdSKay Sievers 
15082ee97cafSCornelia Huck out:
1509a2de48caSGreg Kroah-Hartman 	put_device(dev);
1510a2de48caSGreg Kroah-Hartman 
1511a2de48caSGreg Kroah-Hartman 	kfree(new_class_name);
1512952ab431SJesper Juhl 	kfree(old_class_name);
15132ee97cafSCornelia Huck 	kfree(old_device_name);
1514a2de48caSGreg Kroah-Hartman 
1515a2de48caSGreg Kroah-Hartman 	return error;
1516a2de48caSGreg Kroah-Hartman }
1517a2807dbcSJohannes Berg EXPORT_SYMBOL_GPL(device_rename);
15188a82472fSCornelia Huck 
15198a82472fSCornelia Huck static int device_move_class_links(struct device *dev,
15208a82472fSCornelia Huck 				   struct device *old_parent,
15218a82472fSCornelia Huck 				   struct device *new_parent)
15228a82472fSCornelia Huck {
1523f7f3461dSGreg Kroah-Hartman 	int error = 0;
15248a82472fSCornelia Huck #ifdef CONFIG_SYSFS_DEPRECATED
15258a82472fSCornelia Huck 	char *class_name;
15268a82472fSCornelia Huck 
15278a82472fSCornelia Huck 	class_name = make_class_name(dev->class->name, &dev->kobj);
15288a82472fSCornelia Huck 	if (!class_name) {
1529cb360bbfSCornelia Huck 		error = -ENOMEM;
15308a82472fSCornelia Huck 		goto out;
15318a82472fSCornelia Huck 	}
15328a82472fSCornelia Huck 	if (old_parent) {
15338a82472fSCornelia Huck 		sysfs_remove_link(&dev->kobj, "device");
15348a82472fSCornelia Huck 		sysfs_remove_link(&old_parent->kobj, class_name);
15358a82472fSCornelia Huck 	}
1536c744aeaeSCornelia Huck 	if (new_parent) {
1537c744aeaeSCornelia Huck 		error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
1538c744aeaeSCornelia Huck 					  "device");
15398a82472fSCornelia Huck 		if (error)
15408a82472fSCornelia Huck 			goto out;
1541c744aeaeSCornelia Huck 		error = sysfs_create_link(&new_parent->kobj, &dev->kobj,
1542c744aeaeSCornelia Huck 					  class_name);
15438a82472fSCornelia Huck 		if (error)
15448a82472fSCornelia Huck 			sysfs_remove_link(&dev->kobj, "device");
15454a3ad20cSGreg Kroah-Hartman 	} else
1546c744aeaeSCornelia Huck 		error = 0;
15478a82472fSCornelia Huck out:
15488a82472fSCornelia Huck 	kfree(class_name);
15498a82472fSCornelia Huck 	return error;
15508a82472fSCornelia Huck #else
1551f7f3461dSGreg Kroah-Hartman 	if (old_parent)
1552f7f3461dSGreg Kroah-Hartman 		sysfs_remove_link(&dev->kobj, "device");
1553f7f3461dSGreg Kroah-Hartman 	if (new_parent)
1554f7f3461dSGreg Kroah-Hartman 		error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
1555f7f3461dSGreg Kroah-Hartman 					  "device");
1556f7f3461dSGreg Kroah-Hartman 	return error;
15578a82472fSCornelia Huck #endif
15588a82472fSCornelia Huck }
15598a82472fSCornelia Huck 
15608a82472fSCornelia Huck /**
15618a82472fSCornelia Huck  * device_move - moves a device to a new parent
15628a82472fSCornelia Huck  * @dev: the pointer to the struct device to be moved
1563c744aeaeSCornelia Huck  * @new_parent: the new parent of the device (can by NULL)
1564ffa6a705SCornelia Huck  * @dpm_order: how to reorder the dpm_list
15658a82472fSCornelia Huck  */
1566ffa6a705SCornelia Huck int device_move(struct device *dev, struct device *new_parent,
1567ffa6a705SCornelia Huck 		enum dpm_order dpm_order)
15688a82472fSCornelia Huck {
15698a82472fSCornelia Huck 	int error;
15708a82472fSCornelia Huck 	struct device *old_parent;
1571c744aeaeSCornelia Huck 	struct kobject *new_parent_kobj;
15728a82472fSCornelia Huck 
15738a82472fSCornelia Huck 	dev = get_device(dev);
15748a82472fSCornelia Huck 	if (!dev)
15758a82472fSCornelia Huck 		return -EINVAL;
15768a82472fSCornelia Huck 
1577ffa6a705SCornelia Huck 	device_pm_lock();
15788a82472fSCornelia Huck 	new_parent = get_device(new_parent);
1579c744aeaeSCornelia Huck 	new_parent_kobj = get_device_parent(dev, new_parent);
158063b6971aSCornelia Huck 
15811e0b2cf9SKay Sievers 	pr_debug("device: '%s': %s: moving to '%s'\n", dev_name(dev),
15821e0b2cf9SKay Sievers 		 __func__, new_parent ? dev_name(new_parent) : "<NULL>");
1583c744aeaeSCornelia Huck 	error = kobject_move(&dev->kobj, new_parent_kobj);
15848a82472fSCornelia Huck 	if (error) {
158563b6971aSCornelia Huck 		cleanup_glue_dir(dev, new_parent_kobj);
15868a82472fSCornelia Huck 		put_device(new_parent);
15878a82472fSCornelia Huck 		goto out;
15888a82472fSCornelia Huck 	}
15898a82472fSCornelia Huck 	old_parent = dev->parent;
15908a82472fSCornelia Huck 	dev->parent = new_parent;
15918a82472fSCornelia Huck 	if (old_parent)
1592f791b8c8SGreg Kroah-Hartman 		klist_remove(&dev->p->knode_parent);
15930d358f22SYinghai Lu 	if (new_parent) {
1594f791b8c8SGreg Kroah-Hartman 		klist_add_tail(&dev->p->knode_parent,
1595f791b8c8SGreg Kroah-Hartman 			       &new_parent->p->klist_children);
15960d358f22SYinghai Lu 		set_dev_node(dev, dev_to_node(new_parent));
15970d358f22SYinghai Lu 	}
15980d358f22SYinghai Lu 
15998a82472fSCornelia Huck 	if (!dev->class)
16008a82472fSCornelia Huck 		goto out_put;
16018a82472fSCornelia Huck 	error = device_move_class_links(dev, old_parent, new_parent);
16028a82472fSCornelia Huck 	if (error) {
16038a82472fSCornelia Huck 		/* We ignore errors on cleanup since we're hosed anyway... */
16048a82472fSCornelia Huck 		device_move_class_links(dev, new_parent, old_parent);
16058a82472fSCornelia Huck 		if (!kobject_move(&dev->kobj, &old_parent->kobj)) {
1606c744aeaeSCornelia Huck 			if (new_parent)
1607f791b8c8SGreg Kroah-Hartman 				klist_remove(&dev->p->knode_parent);
16080d358f22SYinghai Lu 			dev->parent = old_parent;
16090d358f22SYinghai Lu 			if (old_parent) {
1610f791b8c8SGreg Kroah-Hartman 				klist_add_tail(&dev->p->knode_parent,
1611f791b8c8SGreg Kroah-Hartman 					       &old_parent->p->klist_children);
16120d358f22SYinghai Lu 				set_dev_node(dev, dev_to_node(old_parent));
16130d358f22SYinghai Lu 			}
16148a82472fSCornelia Huck 		}
161563b6971aSCornelia Huck 		cleanup_glue_dir(dev, new_parent_kobj);
16168a82472fSCornelia Huck 		put_device(new_parent);
16178a82472fSCornelia Huck 		goto out;
16188a82472fSCornelia Huck 	}
1619ffa6a705SCornelia Huck 	switch (dpm_order) {
1620ffa6a705SCornelia Huck 	case DPM_ORDER_NONE:
1621ffa6a705SCornelia Huck 		break;
1622ffa6a705SCornelia Huck 	case DPM_ORDER_DEV_AFTER_PARENT:
1623ffa6a705SCornelia Huck 		device_pm_move_after(dev, new_parent);
1624ffa6a705SCornelia Huck 		break;
1625ffa6a705SCornelia Huck 	case DPM_ORDER_PARENT_BEFORE_DEV:
1626ffa6a705SCornelia Huck 		device_pm_move_before(new_parent, dev);
1627ffa6a705SCornelia Huck 		break;
1628ffa6a705SCornelia Huck 	case DPM_ORDER_DEV_LAST:
1629ffa6a705SCornelia Huck 		device_pm_move_last(dev);
1630ffa6a705SCornelia Huck 		break;
1631ffa6a705SCornelia Huck 	}
16328a82472fSCornelia Huck out_put:
16338a82472fSCornelia Huck 	put_device(old_parent);
16348a82472fSCornelia Huck out:
1635ffa6a705SCornelia Huck 	device_pm_unlock();
16368a82472fSCornelia Huck 	put_device(dev);
16378a82472fSCornelia Huck 	return error;
16388a82472fSCornelia Huck }
16398a82472fSCornelia Huck EXPORT_SYMBOL_GPL(device_move);
164037b0c020SGreg Kroah-Hartman 
164137b0c020SGreg Kroah-Hartman /**
164237b0c020SGreg Kroah-Hartman  * device_shutdown - call ->shutdown() on each device to shutdown.
164337b0c020SGreg Kroah-Hartman  */
164437b0c020SGreg Kroah-Hartman void device_shutdown(void)
164537b0c020SGreg Kroah-Hartman {
164637b0c020SGreg Kroah-Hartman 	struct device *dev, *devn;
164737b0c020SGreg Kroah-Hartman 
164837b0c020SGreg Kroah-Hartman 	list_for_each_entry_safe_reverse(dev, devn, &devices_kset->list,
164937b0c020SGreg Kroah-Hartman 				kobj.entry) {
165037b0c020SGreg Kroah-Hartman 		if (dev->bus && dev->bus->shutdown) {
165137b0c020SGreg Kroah-Hartman 			dev_dbg(dev, "shutdown\n");
165237b0c020SGreg Kroah-Hartman 			dev->bus->shutdown(dev);
165337b0c020SGreg Kroah-Hartman 		} else if (dev->driver && dev->driver->shutdown) {
165437b0c020SGreg Kroah-Hartman 			dev_dbg(dev, "shutdown\n");
165537b0c020SGreg Kroah-Hartman 			dev->driver->shutdown(dev);
165637b0c020SGreg Kroah-Hartman 		}
165737b0c020SGreg Kroah-Hartman 	}
1658e105b8bfSDan Williams 	kobject_put(sysfs_dev_char_kobj);
1659e105b8bfSDan Williams 	kobject_put(sysfs_dev_block_kobj);
1660e105b8bfSDan Williams 	kobject_put(dev_kobj);
166137b0c020SGreg Kroah-Hartman }
1662