xref: /openbmc/linux/drivers/base/core.c (revision 0aa0dc41)
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);
1122831fe6fSGreg 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));
1242831fe6fSGreg 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);
13983b5fb4cSCornelia Huck 		if (dev->uevent_suppress)
14083b5fb4cSCornelia Huck 			return 0;
1411da177e4SLinus Torvalds 		if (dev->bus)
1421da177e4SLinus Torvalds 			return 1;
14323681e47SGreg Kroah-Hartman 		if (dev->class)
14423681e47SGreg Kroah-Hartman 			return 1;
1451da177e4SLinus Torvalds 	}
1461da177e4SLinus Torvalds 	return 0;
1471da177e4SLinus Torvalds }
1481da177e4SLinus Torvalds 
149312c004dSKay Sievers static const char *dev_uevent_name(struct kset *kset, struct kobject *kobj)
1501da177e4SLinus Torvalds {
1511da177e4SLinus Torvalds 	struct device *dev = to_dev(kobj);
1521da177e4SLinus Torvalds 
15323681e47SGreg Kroah-Hartman 	if (dev->bus)
1541da177e4SLinus Torvalds 		return dev->bus->name;
15523681e47SGreg Kroah-Hartman 	if (dev->class)
15623681e47SGreg Kroah-Hartman 		return dev->class->name;
15723681e47SGreg Kroah-Hartman 	return NULL;
1581da177e4SLinus Torvalds }
1591da177e4SLinus Torvalds 
1607eff2e7aSKay Sievers static int dev_uevent(struct kset *kset, struct kobject *kobj,
1617eff2e7aSKay Sievers 		      struct kobj_uevent_env *env)
1621da177e4SLinus Torvalds {
1631da177e4SLinus Torvalds 	struct device *dev = to_dev(kobj);
1641da177e4SLinus Torvalds 	int retval = 0;
1651da177e4SLinus Torvalds 
16623681e47SGreg Kroah-Hartman 	/* add the major/minor if present */
16723681e47SGreg Kroah-Hartman 	if (MAJOR(dev->devt)) {
1687eff2e7aSKay Sievers 		add_uevent_var(env, "MAJOR=%u", MAJOR(dev->devt));
1697eff2e7aSKay Sievers 		add_uevent_var(env, "MINOR=%u", MINOR(dev->devt));
17023681e47SGreg Kroah-Hartman 	}
17123681e47SGreg Kroah-Hartman 
172414264f9SKay Sievers 	if (dev->type && dev->type->name)
1737eff2e7aSKay Sievers 		add_uevent_var(env, "DEVTYPE=%s", dev->type->name);
174414264f9SKay Sievers 
175239378f1SKay Sievers 	if (dev->driver)
1767eff2e7aSKay Sievers 		add_uevent_var(env, "DRIVER=%s", dev->driver->name);
177239378f1SKay Sievers 
178a87cb2acSKay Sievers #ifdef CONFIG_SYSFS_DEPRECATED
179239378f1SKay Sievers 	if (dev->class) {
180239378f1SKay Sievers 		struct device *parent = dev->parent;
181239378f1SKay Sievers 
182239378f1SKay Sievers 		/* find first bus device in parent chain */
183239378f1SKay Sievers 		while (parent && !parent->bus)
184239378f1SKay Sievers 			parent = parent->parent;
185239378f1SKay Sievers 		if (parent && parent->bus) {
186239378f1SKay Sievers 			const char *path;
187239378f1SKay Sievers 
188239378f1SKay Sievers 			path = kobject_get_path(&parent->kobj, GFP_KERNEL);
1892c7afd12SKay Sievers 			if (path) {
1907eff2e7aSKay Sievers 				add_uevent_var(env, "PHYSDEVPATH=%s", path);
191239378f1SKay Sievers 				kfree(path);
1922c7afd12SKay Sievers 			}
193239378f1SKay Sievers 
1947eff2e7aSKay Sievers 			add_uevent_var(env, "PHYSDEVBUS=%s", parent->bus->name);
195239378f1SKay Sievers 
196239378f1SKay Sievers 			if (parent->driver)
1977eff2e7aSKay Sievers 				add_uevent_var(env, "PHYSDEVDRIVER=%s",
1987eff2e7aSKay Sievers 					       parent->driver->name);
199239378f1SKay Sievers 		}
200239378f1SKay Sievers 	} else if (dev->bus) {
2017eff2e7aSKay Sievers 		add_uevent_var(env, "PHYSDEVBUS=%s", dev->bus->name);
202239378f1SKay Sievers 
203239378f1SKay Sievers 		if (dev->driver)
2044a3ad20cSGreg Kroah-Hartman 			add_uevent_var(env, "PHYSDEVDRIVER=%s",
2054a3ad20cSGreg Kroah-Hartman 				       dev->driver->name);
206d81d9d6bSKay Sievers 	}
207239378f1SKay Sievers #endif
2081da177e4SLinus Torvalds 
2091da177e4SLinus Torvalds 	/* have the bus specific function add its stuff */
2107eff2e7aSKay Sievers 	if (dev->bus && dev->bus->uevent) {
2117eff2e7aSKay Sievers 		retval = dev->bus->uevent(dev, env);
212f9f852dfSKay Sievers 		if (retval)
2137dc72b28SGreg Kroah-Hartman 			pr_debug("device: '%s': %s: bus uevent() returned %d\n",
2141e0b2cf9SKay Sievers 				 dev_name(dev), __func__, retval);
2151da177e4SLinus Torvalds 	}
2161da177e4SLinus Torvalds 
2172620efefSGreg Kroah-Hartman 	/* have the class specific function add its stuff */
2187eff2e7aSKay Sievers 	if (dev->class && dev->class->dev_uevent) {
2197eff2e7aSKay Sievers 		retval = dev->class->dev_uevent(dev, env);
220f9f852dfSKay Sievers 		if (retval)
2217dc72b28SGreg Kroah-Hartman 			pr_debug("device: '%s': %s: class uevent() "
2221e0b2cf9SKay Sievers 				 "returned %d\n", dev_name(dev),
2232b3a302aSHarvey Harrison 				 __func__, retval);
2242620efefSGreg Kroah-Hartman 	}
225f9f852dfSKay Sievers 
226f9f852dfSKay Sievers 	/* have the device type specific fuction add its stuff */
2277eff2e7aSKay Sievers 	if (dev->type && dev->type->uevent) {
2287eff2e7aSKay Sievers 		retval = dev->type->uevent(dev, env);
229f9f852dfSKay Sievers 		if (retval)
2307dc72b28SGreg Kroah-Hartman 			pr_debug("device: '%s': %s: dev_type uevent() "
2311e0b2cf9SKay Sievers 				 "returned %d\n", dev_name(dev),
2322b3a302aSHarvey Harrison 				 __func__, retval);
2332620efefSGreg Kroah-Hartman 	}
2342620efefSGreg Kroah-Hartman 
2351da177e4SLinus Torvalds 	return retval;
2361da177e4SLinus Torvalds }
2371da177e4SLinus Torvalds 
238312c004dSKay Sievers static struct kset_uevent_ops device_uevent_ops = {
239312c004dSKay Sievers 	.filter =	dev_uevent_filter,
240312c004dSKay Sievers 	.name =		dev_uevent_name,
241312c004dSKay Sievers 	.uevent =	dev_uevent,
2421da177e4SLinus Torvalds };
2431da177e4SLinus Torvalds 
24416574dccSKay Sievers static ssize_t show_uevent(struct device *dev, struct device_attribute *attr,
24516574dccSKay Sievers 			   char *buf)
24616574dccSKay Sievers {
24716574dccSKay Sievers 	struct kobject *top_kobj;
24816574dccSKay Sievers 	struct kset *kset;
2497eff2e7aSKay Sievers 	struct kobj_uevent_env *env = NULL;
25016574dccSKay Sievers 	int i;
25116574dccSKay Sievers 	size_t count = 0;
25216574dccSKay Sievers 	int retval;
25316574dccSKay Sievers 
25416574dccSKay Sievers 	/* search the kset, the device belongs to */
25516574dccSKay Sievers 	top_kobj = &dev->kobj;
2565c5daf65SKay Sievers 	while (!top_kobj->kset && top_kobj->parent)
25716574dccSKay Sievers 		top_kobj = top_kobj->parent;
25816574dccSKay Sievers 	if (!top_kobj->kset)
25916574dccSKay Sievers 		goto out;
2605c5daf65SKay Sievers 
26116574dccSKay Sievers 	kset = top_kobj->kset;
26216574dccSKay Sievers 	if (!kset->uevent_ops || !kset->uevent_ops->uevent)
26316574dccSKay Sievers 		goto out;
26416574dccSKay Sievers 
26516574dccSKay Sievers 	/* respect filter */
26616574dccSKay Sievers 	if (kset->uevent_ops && kset->uevent_ops->filter)
26716574dccSKay Sievers 		if (!kset->uevent_ops->filter(kset, &dev->kobj))
26816574dccSKay Sievers 			goto out;
26916574dccSKay Sievers 
2707eff2e7aSKay Sievers 	env = kzalloc(sizeof(struct kobj_uevent_env), GFP_KERNEL);
2717eff2e7aSKay Sievers 	if (!env)
272c7308c81SGreg Kroah-Hartman 		return -ENOMEM;
273c7308c81SGreg Kroah-Hartman 
27416574dccSKay Sievers 	/* let the kset specific function add its keys */
2757eff2e7aSKay Sievers 	retval = kset->uevent_ops->uevent(kset, &dev->kobj, env);
27616574dccSKay Sievers 	if (retval)
27716574dccSKay Sievers 		goto out;
27816574dccSKay Sievers 
27916574dccSKay Sievers 	/* copy keys to file */
2807eff2e7aSKay Sievers 	for (i = 0; i < env->envp_idx; i++)
2817eff2e7aSKay Sievers 		count += sprintf(&buf[count], "%s\n", env->envp[i]);
28216574dccSKay Sievers out:
2837eff2e7aSKay Sievers 	kfree(env);
28416574dccSKay Sievers 	return count;
28516574dccSKay Sievers }
28616574dccSKay Sievers 
287a7fd6706SKay Sievers static ssize_t store_uevent(struct device *dev, struct device_attribute *attr,
288a7fd6706SKay Sievers 			    const char *buf, size_t count)
289a7fd6706SKay Sievers {
29060a96a59SKay Sievers 	enum kobject_action action;
29160a96a59SKay Sievers 
2925c5daf65SKay Sievers 	if (kobject_action_type(buf, count, &action) == 0) {
29360a96a59SKay Sievers 		kobject_uevent(&dev->kobj, action);
29460a96a59SKay Sievers 		goto out;
29560a96a59SKay Sievers 	}
29660a96a59SKay Sievers 
29722af74f3SKay Sievers 	dev_err(dev, "uevent: unsupported action-string; this will "
29860a96a59SKay Sievers 		     "be ignored in a future kernel version\n");
299312c004dSKay Sievers 	kobject_uevent(&dev->kobj, KOBJ_ADD);
30060a96a59SKay Sievers out:
301a7fd6706SKay Sievers 	return count;
302a7fd6706SKay Sievers }
303a7fd6706SKay Sievers 
304ad6a1e1cSTejun Heo static struct device_attribute uevent_attr =
305ad6a1e1cSTejun Heo 	__ATTR(uevent, S_IRUGO | S_IWUSR, show_uevent, store_uevent);
306ad6a1e1cSTejun Heo 
307621a1672SDmitry Torokhov static int device_add_attributes(struct device *dev,
308621a1672SDmitry Torokhov 				 struct device_attribute *attrs)
309de0ff00dSGreg Kroah-Hartman {
310de0ff00dSGreg Kroah-Hartman 	int error = 0;
311621a1672SDmitry Torokhov 	int i;
312de0ff00dSGreg Kroah-Hartman 
313621a1672SDmitry Torokhov 	if (attrs) {
314621a1672SDmitry Torokhov 		for (i = 0; attr_name(attrs[i]); i++) {
315621a1672SDmitry Torokhov 			error = device_create_file(dev, &attrs[i]);
316621a1672SDmitry Torokhov 			if (error)
317621a1672SDmitry Torokhov 				break;
318621a1672SDmitry Torokhov 		}
319621a1672SDmitry Torokhov 		if (error)
320de0ff00dSGreg Kroah-Hartman 			while (--i >= 0)
321621a1672SDmitry Torokhov 				device_remove_file(dev, &attrs[i]);
322de0ff00dSGreg Kroah-Hartman 	}
323de0ff00dSGreg Kroah-Hartman 	return error;
324de0ff00dSGreg Kroah-Hartman }
325de0ff00dSGreg Kroah-Hartman 
326621a1672SDmitry Torokhov static void device_remove_attributes(struct device *dev,
327621a1672SDmitry Torokhov 				     struct device_attribute *attrs)
328de0ff00dSGreg Kroah-Hartman {
329de0ff00dSGreg Kroah-Hartman 	int i;
330621a1672SDmitry Torokhov 
331621a1672SDmitry Torokhov 	if (attrs)
332621a1672SDmitry Torokhov 		for (i = 0; attr_name(attrs[i]); i++)
333621a1672SDmitry Torokhov 			device_remove_file(dev, &attrs[i]);
334621a1672SDmitry Torokhov }
335621a1672SDmitry Torokhov 
336621a1672SDmitry Torokhov static int device_add_groups(struct device *dev,
337621a1672SDmitry Torokhov 			     struct attribute_group **groups)
338621a1672SDmitry Torokhov {
339621a1672SDmitry Torokhov 	int error = 0;
340621a1672SDmitry Torokhov 	int i;
341621a1672SDmitry Torokhov 
342621a1672SDmitry Torokhov 	if (groups) {
343621a1672SDmitry Torokhov 		for (i = 0; groups[i]; i++) {
344621a1672SDmitry Torokhov 			error = sysfs_create_group(&dev->kobj, groups[i]);
345621a1672SDmitry Torokhov 			if (error) {
346621a1672SDmitry Torokhov 				while (--i >= 0)
3474a3ad20cSGreg Kroah-Hartman 					sysfs_remove_group(&dev->kobj,
3484a3ad20cSGreg Kroah-Hartman 							   groups[i]);
349621a1672SDmitry Torokhov 				break;
350de0ff00dSGreg Kroah-Hartman 			}
351de0ff00dSGreg Kroah-Hartman 		}
352de0ff00dSGreg Kroah-Hartman 	}
353621a1672SDmitry Torokhov 	return error;
354621a1672SDmitry Torokhov }
355621a1672SDmitry Torokhov 
356621a1672SDmitry Torokhov static void device_remove_groups(struct device *dev,
357621a1672SDmitry Torokhov 				 struct attribute_group **groups)
358621a1672SDmitry Torokhov {
359621a1672SDmitry Torokhov 	int i;
360621a1672SDmitry Torokhov 
361621a1672SDmitry Torokhov 	if (groups)
362621a1672SDmitry Torokhov 		for (i = 0; groups[i]; i++)
363621a1672SDmitry Torokhov 			sysfs_remove_group(&dev->kobj, groups[i]);
364621a1672SDmitry Torokhov }
365de0ff00dSGreg Kroah-Hartman 
3662620efefSGreg Kroah-Hartman static int device_add_attrs(struct device *dev)
3672620efefSGreg Kroah-Hartman {
3682620efefSGreg Kroah-Hartman 	struct class *class = dev->class;
369f9f852dfSKay Sievers 	struct device_type *type = dev->type;
370621a1672SDmitry Torokhov 	int error;
3712620efefSGreg Kroah-Hartman 
372621a1672SDmitry Torokhov 	if (class) {
373621a1672SDmitry Torokhov 		error = device_add_attributes(dev, class->dev_attrs);
3742620efefSGreg Kroah-Hartman 		if (error)
375621a1672SDmitry Torokhov 			return error;
376f9f852dfSKay Sievers 	}
377f9f852dfSKay Sievers 
378621a1672SDmitry Torokhov 	if (type) {
379621a1672SDmitry Torokhov 		error = device_add_groups(dev, type->groups);
380f9f852dfSKay Sievers 		if (error)
381621a1672SDmitry Torokhov 			goto err_remove_class_attrs;
382f9f852dfSKay Sievers 	}
383621a1672SDmitry Torokhov 
384621a1672SDmitry Torokhov 	error = device_add_groups(dev, dev->groups);
385f9f852dfSKay Sievers 	if (error)
386621a1672SDmitry Torokhov 		goto err_remove_type_groups;
387621a1672SDmitry Torokhov 
388621a1672SDmitry Torokhov 	return 0;
389621a1672SDmitry Torokhov 
390621a1672SDmitry Torokhov  err_remove_type_groups:
391621a1672SDmitry Torokhov 	if (type)
392621a1672SDmitry Torokhov 		device_remove_groups(dev, type->groups);
393621a1672SDmitry Torokhov  err_remove_class_attrs:
394621a1672SDmitry Torokhov 	if (class)
395621a1672SDmitry Torokhov 		device_remove_attributes(dev, class->dev_attrs);
396f9f852dfSKay Sievers 
3972620efefSGreg Kroah-Hartman 	return error;
3982620efefSGreg Kroah-Hartman }
3992620efefSGreg Kroah-Hartman 
4002620efefSGreg Kroah-Hartman static void device_remove_attrs(struct device *dev)
4012620efefSGreg Kroah-Hartman {
4022620efefSGreg Kroah-Hartman 	struct class *class = dev->class;
403f9f852dfSKay Sievers 	struct device_type *type = dev->type;
4042620efefSGreg Kroah-Hartman 
405621a1672SDmitry Torokhov 	device_remove_groups(dev, dev->groups);
406f9f852dfSKay Sievers 
407621a1672SDmitry Torokhov 	if (type)
408621a1672SDmitry Torokhov 		device_remove_groups(dev, type->groups);
409621a1672SDmitry Torokhov 
410621a1672SDmitry Torokhov 	if (class)
411621a1672SDmitry Torokhov 		device_remove_attributes(dev, class->dev_attrs);
4122620efefSGreg Kroah-Hartman }
4132620efefSGreg Kroah-Hartman 
4142620efefSGreg Kroah-Hartman 
41523681e47SGreg Kroah-Hartman static ssize_t show_dev(struct device *dev, struct device_attribute *attr,
41623681e47SGreg Kroah-Hartman 			char *buf)
41723681e47SGreg Kroah-Hartman {
41823681e47SGreg Kroah-Hartman 	return print_dev_t(buf, dev->devt);
41923681e47SGreg Kroah-Hartman }
42023681e47SGreg Kroah-Hartman 
421ad6a1e1cSTejun Heo static struct device_attribute devt_attr =
422ad6a1e1cSTejun Heo 	__ATTR(dev, S_IRUGO, show_dev, NULL);
423ad6a1e1cSTejun Heo 
424881c6cfdSGreg Kroah-Hartman /* kset to create /sys/devices/  */
425881c6cfdSGreg Kroah-Hartman struct kset *devices_kset;
4261da177e4SLinus Torvalds 
4271da177e4SLinus Torvalds /**
4281da177e4SLinus Torvalds  * device_create_file - create sysfs attribute file for device.
4291da177e4SLinus Torvalds  * @dev: device.
4301da177e4SLinus Torvalds  * @attr: device attribute descriptor.
4311da177e4SLinus Torvalds  */
4321da177e4SLinus Torvalds int device_create_file(struct device *dev, struct device_attribute *attr)
4331da177e4SLinus Torvalds {
4341da177e4SLinus Torvalds 	int error = 0;
4350c98b19fSCornelia Huck 	if (dev)
4361da177e4SLinus Torvalds 		error = sysfs_create_file(&dev->kobj, &attr->attr);
4371da177e4SLinus Torvalds 	return error;
4381da177e4SLinus Torvalds }
4391da177e4SLinus Torvalds 
4401da177e4SLinus Torvalds /**
4411da177e4SLinus Torvalds  * device_remove_file - remove sysfs attribute file.
4421da177e4SLinus Torvalds  * @dev: device.
4431da177e4SLinus Torvalds  * @attr: device attribute descriptor.
4441da177e4SLinus Torvalds  */
4451da177e4SLinus Torvalds void device_remove_file(struct device *dev, struct device_attribute *attr)
4461da177e4SLinus Torvalds {
4470c98b19fSCornelia Huck 	if (dev)
4481da177e4SLinus Torvalds 		sysfs_remove_file(&dev->kobj, &attr->attr);
4491da177e4SLinus Torvalds }
4501da177e4SLinus Torvalds 
4512589f188SGreg Kroah-Hartman /**
4522589f188SGreg Kroah-Hartman  * device_create_bin_file - create sysfs binary attribute file for device.
4532589f188SGreg Kroah-Hartman  * @dev: device.
4542589f188SGreg Kroah-Hartman  * @attr: device binary attribute descriptor.
4552589f188SGreg Kroah-Hartman  */
4562589f188SGreg Kroah-Hartman int device_create_bin_file(struct device *dev, struct bin_attribute *attr)
4572589f188SGreg Kroah-Hartman {
4582589f188SGreg Kroah-Hartman 	int error = -EINVAL;
4592589f188SGreg Kroah-Hartman 	if (dev)
4602589f188SGreg Kroah-Hartman 		error = sysfs_create_bin_file(&dev->kobj, attr);
4612589f188SGreg Kroah-Hartman 	return error;
4622589f188SGreg Kroah-Hartman }
4632589f188SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(device_create_bin_file);
4642589f188SGreg Kroah-Hartman 
4652589f188SGreg Kroah-Hartman /**
4662589f188SGreg Kroah-Hartman  * device_remove_bin_file - remove sysfs binary attribute file
4672589f188SGreg Kroah-Hartman  * @dev: device.
4682589f188SGreg Kroah-Hartman  * @attr: device binary attribute descriptor.
4692589f188SGreg Kroah-Hartman  */
4702589f188SGreg Kroah-Hartman void device_remove_bin_file(struct device *dev, struct bin_attribute *attr)
4712589f188SGreg Kroah-Hartman {
4722589f188SGreg Kroah-Hartman 	if (dev)
4732589f188SGreg Kroah-Hartman 		sysfs_remove_bin_file(&dev->kobj, attr);
4742589f188SGreg Kroah-Hartman }
4752589f188SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(device_remove_bin_file);
4762589f188SGreg Kroah-Hartman 
477d9a9cdfbSAlan Stern /**
478523ded71SAlan Stern  * device_schedule_callback_owner - helper to schedule a callback for a device
479d9a9cdfbSAlan Stern  * @dev: device.
480d9a9cdfbSAlan Stern  * @func: callback function to invoke later.
481523ded71SAlan Stern  * @owner: module owning the callback routine
482d9a9cdfbSAlan Stern  *
483d9a9cdfbSAlan Stern  * Attribute methods must not unregister themselves or their parent device
484d9a9cdfbSAlan Stern  * (which would amount to the same thing).  Attempts to do so will deadlock,
485d9a9cdfbSAlan Stern  * since unregistration is mutually exclusive with driver callbacks.
486d9a9cdfbSAlan Stern  *
487d9a9cdfbSAlan Stern  * Instead methods can call this routine, which will attempt to allocate
488d9a9cdfbSAlan Stern  * and schedule a workqueue request to call back @func with @dev as its
489d9a9cdfbSAlan Stern  * argument in the workqueue's process context.  @dev will be pinned until
490d9a9cdfbSAlan Stern  * @func returns.
491d9a9cdfbSAlan Stern  *
492523ded71SAlan Stern  * This routine is usually called via the inline device_schedule_callback(),
493523ded71SAlan Stern  * which automatically sets @owner to THIS_MODULE.
494523ded71SAlan Stern  *
495d9a9cdfbSAlan Stern  * Returns 0 if the request was submitted, -ENOMEM if storage could not
496523ded71SAlan Stern  * be allocated, -ENODEV if a reference to @owner isn't available.
497d9a9cdfbSAlan Stern  *
498d9a9cdfbSAlan Stern  * NOTE: This routine won't work if CONFIG_SYSFS isn't set!  It uses an
499d9a9cdfbSAlan Stern  * underlying sysfs routine (since it is intended for use by attribute
500d9a9cdfbSAlan Stern  * methods), and if sysfs isn't available you'll get nothing but -ENOSYS.
501d9a9cdfbSAlan Stern  */
502523ded71SAlan Stern int device_schedule_callback_owner(struct device *dev,
503523ded71SAlan Stern 		void (*func)(struct device *), struct module *owner)
504d9a9cdfbSAlan Stern {
505d9a9cdfbSAlan Stern 	return sysfs_schedule_callback(&dev->kobj,
506523ded71SAlan Stern 			(void (*)(void *)) func, dev, owner);
507d9a9cdfbSAlan Stern }
508523ded71SAlan Stern EXPORT_SYMBOL_GPL(device_schedule_callback_owner);
509d9a9cdfbSAlan Stern 
51034bb61f9SJames Bottomley static void klist_children_get(struct klist_node *n)
51134bb61f9SJames Bottomley {
51211c3b5c3SGreg Kroah-Hartman 	struct device_private *p = to_device_private_parent(n);
51311c3b5c3SGreg Kroah-Hartman 	struct device *dev = p->device;
51434bb61f9SJames Bottomley 
51534bb61f9SJames Bottomley 	get_device(dev);
51634bb61f9SJames Bottomley }
51734bb61f9SJames Bottomley 
51834bb61f9SJames Bottomley static void klist_children_put(struct klist_node *n)
51934bb61f9SJames Bottomley {
52011c3b5c3SGreg Kroah-Hartman 	struct device_private *p = to_device_private_parent(n);
52111c3b5c3SGreg Kroah-Hartman 	struct device *dev = p->device;
52234bb61f9SJames Bottomley 
52334bb61f9SJames Bottomley 	put_device(dev);
52434bb61f9SJames Bottomley }
52534bb61f9SJames Bottomley 
5261da177e4SLinus Torvalds /**
5271da177e4SLinus Torvalds  * device_initialize - init device structure.
5281da177e4SLinus Torvalds  * @dev: device.
5291da177e4SLinus Torvalds  *
5305739411aSCornelia Huck  * This prepares the device for use by other layers by initializing
5315739411aSCornelia Huck  * its fields.
5321da177e4SLinus Torvalds  * It is the first half of device_register(), if called by
5335739411aSCornelia Huck  * that function, though it can also be called separately, so one
5345739411aSCornelia Huck  * may use @dev's fields. In particular, get_device()/put_device()
5355739411aSCornelia Huck  * may be used for reference counting of @dev after calling this
5365739411aSCornelia Huck  * function.
5375739411aSCornelia Huck  *
5385739411aSCornelia Huck  * NOTE: Use put_device() to give up your reference instead of freeing
5395739411aSCornelia Huck  * @dev directly once you have called this function.
5401da177e4SLinus Torvalds  */
5411da177e4SLinus Torvalds void device_initialize(struct device *dev)
5421da177e4SLinus Torvalds {
5432831fe6fSGreg Kroah-Hartman 	dev->p = kzalloc(sizeof(*dev->p), GFP_KERNEL);
5442831fe6fSGreg Kroah-Hartman 	if (!dev->p) {
5452831fe6fSGreg Kroah-Hartman 		WARN_ON(1);
5462831fe6fSGreg Kroah-Hartman 		return;
5472831fe6fSGreg Kroah-Hartman 	}
5482831fe6fSGreg Kroah-Hartman 	dev->p->device = dev;
549881c6cfdSGreg Kroah-Hartman 	dev->kobj.kset = devices_kset;
550f9cb074bSGreg Kroah-Hartman 	kobject_init(&dev->kobj, &device_ktype);
55111c3b5c3SGreg Kroah-Hartman 	klist_init(&dev->p->klist_children, klist_children_get,
55234bb61f9SJames Bottomley 		   klist_children_put);
5531da177e4SLinus Torvalds 	INIT_LIST_HEAD(&dev->dma_pools);
554af70316aSmochel@digitalimplant.org 	init_MUTEX(&dev->sem);
5559ac7849eSTejun Heo 	spin_lock_init(&dev->devres_lock);
5569ac7849eSTejun Heo 	INIT_LIST_HEAD(&dev->devres_head);
5570ac85241SDavid Brownell 	device_init_wakeup(dev, 0);
5583b98aeafSAlan Stern 	device_pm_init(dev);
55987348136SChristoph Hellwig 	set_dev_node(dev, -1);
5601da177e4SLinus Torvalds }
5611da177e4SLinus Torvalds 
56240fa5422SGreg Kroah-Hartman #ifdef CONFIG_SYSFS_DEPRECATED
563c744aeaeSCornelia Huck static struct kobject *get_device_parent(struct device *dev,
564c744aeaeSCornelia Huck 					 struct device *parent)
56540fa5422SGreg Kroah-Hartman {
566da231fd5SKay Sievers 	/* class devices without a parent live in /sys/class/<classname>/ */
5673eb215deSDmitry Torokhov 	if (dev->class && (!parent || parent->class != dev->class))
5681fbfee6cSGreg Kroah-Hartman 		return &dev->class->p->class_subsys.kobj;
569da231fd5SKay Sievers 	/* all other devices keep their parent */
57040fa5422SGreg Kroah-Hartman 	else if (parent)
571c744aeaeSCornelia Huck 		return &parent->kobj;
57240fa5422SGreg Kroah-Hartman 
573c744aeaeSCornelia Huck 	return NULL;
57440fa5422SGreg Kroah-Hartman }
575da231fd5SKay Sievers 
576da231fd5SKay Sievers static inline void cleanup_device_parent(struct device *dev) {}
57763b6971aSCornelia Huck static inline void cleanup_glue_dir(struct device *dev,
57863b6971aSCornelia Huck 				    struct kobject *glue_dir) {}
57940fa5422SGreg Kroah-Hartman #else
580c744aeaeSCornelia Huck static struct kobject *virtual_device_parent(struct device *dev)
581f0ee61a6SGreg Kroah-Hartman {
582f0ee61a6SGreg Kroah-Hartman 	static struct kobject *virtual_dir = NULL;
583f0ee61a6SGreg Kroah-Hartman 
584f0ee61a6SGreg Kroah-Hartman 	if (!virtual_dir)
5854ff6abffSGreg Kroah-Hartman 		virtual_dir = kobject_create_and_add("virtual",
586881c6cfdSGreg Kroah-Hartman 						     &devices_kset->kobj);
587f0ee61a6SGreg Kroah-Hartman 
58886406245SKay Sievers 	return virtual_dir;
589f0ee61a6SGreg Kroah-Hartman }
590f0ee61a6SGreg Kroah-Hartman 
591c744aeaeSCornelia Huck static struct kobject *get_device_parent(struct device *dev,
592c744aeaeSCornelia Huck 					 struct device *parent)
59340fa5422SGreg Kroah-Hartman {
59443968d2fSGreg Kroah-Hartman 	int retval;
59543968d2fSGreg Kroah-Hartman 
59686406245SKay Sievers 	if (dev->class) {
59786406245SKay Sievers 		struct kobject *kobj = NULL;
59886406245SKay Sievers 		struct kobject *parent_kobj;
59986406245SKay Sievers 		struct kobject *k;
60086406245SKay Sievers 
60186406245SKay Sievers 		/*
60286406245SKay Sievers 		 * If we have no parent, we live in "virtual".
6030f4dafc0SKay Sievers 		 * Class-devices with a non class-device as parent, live
6040f4dafc0SKay Sievers 		 * in a "glue" directory to prevent namespace collisions.
60586406245SKay Sievers 		 */
60686406245SKay Sievers 		if (parent == NULL)
60786406245SKay Sievers 			parent_kobj = virtual_device_parent(dev);
60886406245SKay Sievers 		else if (parent->class)
60986406245SKay Sievers 			return &parent->kobj;
61086406245SKay Sievers 		else
61186406245SKay Sievers 			parent_kobj = &parent->kobj;
61286406245SKay Sievers 
61386406245SKay Sievers 		/* find our class-directory at the parent and reference it */
6147c71448bSGreg Kroah-Hartman 		spin_lock(&dev->class->p->class_dirs.list_lock);
6157c71448bSGreg Kroah-Hartman 		list_for_each_entry(k, &dev->class->p->class_dirs.list, entry)
61686406245SKay Sievers 			if (k->parent == parent_kobj) {
61786406245SKay Sievers 				kobj = kobject_get(k);
61886406245SKay Sievers 				break;
61986406245SKay Sievers 			}
6207c71448bSGreg Kroah-Hartman 		spin_unlock(&dev->class->p->class_dirs.list_lock);
62186406245SKay Sievers 		if (kobj)
62286406245SKay Sievers 			return kobj;
62386406245SKay Sievers 
62486406245SKay Sievers 		/* or create a new class-directory at the parent device */
62543968d2fSGreg Kroah-Hartman 		k = kobject_create();
62643968d2fSGreg Kroah-Hartman 		if (!k)
62743968d2fSGreg Kroah-Hartman 			return NULL;
6287c71448bSGreg Kroah-Hartman 		k->kset = &dev->class->p->class_dirs;
629b2d6db58SGreg Kroah-Hartman 		retval = kobject_add(k, parent_kobj, "%s", dev->class->name);
63043968d2fSGreg Kroah-Hartman 		if (retval < 0) {
63143968d2fSGreg Kroah-Hartman 			kobject_put(k);
63243968d2fSGreg Kroah-Hartman 			return NULL;
63343968d2fSGreg Kroah-Hartman 		}
6340f4dafc0SKay Sievers 		/* do not emit an uevent for this simple "glue" directory */
63543968d2fSGreg Kroah-Hartman 		return k;
63686406245SKay Sievers 	}
63786406245SKay Sievers 
63886406245SKay Sievers 	if (parent)
639c744aeaeSCornelia Huck 		return &parent->kobj;
640c744aeaeSCornelia Huck 	return NULL;
641c744aeaeSCornelia Huck }
642da231fd5SKay Sievers 
64363b6971aSCornelia Huck static void cleanup_glue_dir(struct device *dev, struct kobject *glue_dir)
644da231fd5SKay Sievers {
6450f4dafc0SKay Sievers 	/* see if we live in a "glue" directory */
646c1fe539aSCornelia Huck 	if (!glue_dir || !dev->class ||
6477c71448bSGreg Kroah-Hartman 	    glue_dir->kset != &dev->class->p->class_dirs)
648da231fd5SKay Sievers 		return;
649da231fd5SKay Sievers 
6500f4dafc0SKay Sievers 	kobject_put(glue_dir);
651da231fd5SKay Sievers }
65263b6971aSCornelia Huck 
65363b6971aSCornelia Huck static void cleanup_device_parent(struct device *dev)
65463b6971aSCornelia Huck {
65563b6971aSCornelia Huck 	cleanup_glue_dir(dev, dev->kobj.parent);
65663b6971aSCornelia Huck }
657c744aeaeSCornelia Huck #endif
65886406245SKay Sievers 
65963b6971aSCornelia Huck static void setup_parent(struct device *dev, struct device *parent)
660c744aeaeSCornelia Huck {
661c744aeaeSCornelia Huck 	struct kobject *kobj;
662c744aeaeSCornelia Huck 	kobj = get_device_parent(dev, parent);
663c744aeaeSCornelia Huck 	if (kobj)
664c744aeaeSCornelia Huck 		dev->kobj.parent = kobj;
66540fa5422SGreg Kroah-Hartman }
66640fa5422SGreg Kroah-Hartman 
6672ee97cafSCornelia Huck static int device_add_class_symlinks(struct device *dev)
6682ee97cafSCornelia Huck {
6692ee97cafSCornelia Huck 	int error;
6702ee97cafSCornelia Huck 
6712ee97cafSCornelia Huck 	if (!dev->class)
6722ee97cafSCornelia Huck 		return 0;
673da231fd5SKay Sievers 
6741fbfee6cSGreg Kroah-Hartman 	error = sysfs_create_link(&dev->kobj,
6751fbfee6cSGreg Kroah-Hartman 				  &dev->class->p->class_subsys.kobj,
6762ee97cafSCornelia Huck 				  "subsystem");
6772ee97cafSCornelia Huck 	if (error)
6782ee97cafSCornelia Huck 		goto out;
679da231fd5SKay Sievers 
680da231fd5SKay Sievers #ifdef CONFIG_SYSFS_DEPRECATED
681da231fd5SKay Sievers 	/* stacked class devices need a symlink in the class directory */
6821fbfee6cSGreg Kroah-Hartman 	if (dev->kobj.parent != &dev->class->p->class_subsys.kobj &&
6834e886c29SGreg Kroah-Hartman 	    device_is_not_partition(dev)) {
6841fbfee6cSGreg Kroah-Hartman 		error = sysfs_create_link(&dev->class->p->class_subsys.kobj,
6851e0b2cf9SKay Sievers 					  &dev->kobj, dev_name(dev));
6862ee97cafSCornelia Huck 		if (error)
6872ee97cafSCornelia Huck 			goto out_subsys;
6882ee97cafSCornelia Huck 	}
689da231fd5SKay Sievers 
6904e886c29SGreg Kroah-Hartman 	if (dev->parent && device_is_not_partition(dev)) {
6914f01a757SDmitry Torokhov 		struct device *parent = dev->parent;
6924f01a757SDmitry Torokhov 		char *class_name;
6934f01a757SDmitry Torokhov 
6944f01a757SDmitry Torokhov 		/*
695da231fd5SKay Sievers 		 * stacked class devices have the 'device' link
696da231fd5SKay Sievers 		 * pointing to the bus device instead of the parent
6974f01a757SDmitry Torokhov 		 */
6984f01a757SDmitry Torokhov 		while (parent->class && !parent->bus && parent->parent)
6994f01a757SDmitry Torokhov 			parent = parent->parent;
7004f01a757SDmitry Torokhov 
7014f01a757SDmitry Torokhov 		error = sysfs_create_link(&dev->kobj,
7024f01a757SDmitry Torokhov 					  &parent->kobj,
7032ee97cafSCornelia Huck 					  "device");
7042ee97cafSCornelia Huck 		if (error)
7052ee97cafSCornelia Huck 			goto out_busid;
7064f01a757SDmitry Torokhov 
7074f01a757SDmitry Torokhov 		class_name = make_class_name(dev->class->name,
7082ee97cafSCornelia Huck 						&dev->kobj);
7092ee97cafSCornelia Huck 		if (class_name)
7102ee97cafSCornelia Huck 			error = sysfs_create_link(&dev->parent->kobj,
7112ee97cafSCornelia Huck 						&dev->kobj, class_name);
7122ee97cafSCornelia Huck 		kfree(class_name);
7132ee97cafSCornelia Huck 		if (error)
7142ee97cafSCornelia Huck 			goto out_device;
7152ee97cafSCornelia Huck 	}
716da231fd5SKay Sievers 	return 0;
717da231fd5SKay Sievers 
718da231fd5SKay Sievers out_device:
7194e886c29SGreg Kroah-Hartman 	if (dev->parent && device_is_not_partition(dev))
720da231fd5SKay Sievers 		sysfs_remove_link(&dev->kobj, "device");
721da231fd5SKay Sievers out_busid:
7221fbfee6cSGreg Kroah-Hartman 	if (dev->kobj.parent != &dev->class->p->class_subsys.kobj &&
7234e886c29SGreg Kroah-Hartman 	    device_is_not_partition(dev))
7241fbfee6cSGreg Kroah-Hartman 		sysfs_remove_link(&dev->class->p->class_subsys.kobj,
7251e0b2cf9SKay Sievers 				  dev_name(dev));
7264f01a757SDmitry Torokhov #else
727da231fd5SKay Sievers 	/* link in the class directory pointing to the device */
7281fbfee6cSGreg Kroah-Hartman 	error = sysfs_create_link(&dev->class->p->class_subsys.kobj,
7291e0b2cf9SKay Sievers 				  &dev->kobj, dev_name(dev));
730da231fd5SKay Sievers 	if (error)
731da231fd5SKay Sievers 		goto out_subsys;
732da231fd5SKay Sievers 
7334e886c29SGreg Kroah-Hartman 	if (dev->parent && device_is_not_partition(dev)) {
7344f01a757SDmitry Torokhov 		error = sysfs_create_link(&dev->kobj, &dev->parent->kobj,
7354f01a757SDmitry Torokhov 					  "device");
7364f01a757SDmitry Torokhov 		if (error)
7374f01a757SDmitry Torokhov 			goto out_busid;
7382ee97cafSCornelia Huck 	}
7392ee97cafSCornelia Huck 	return 0;
7402ee97cafSCornelia Huck 
7412ee97cafSCornelia Huck out_busid:
7421e0b2cf9SKay Sievers 	sysfs_remove_link(&dev->class->p->class_subsys.kobj, dev_name(dev));
743da231fd5SKay Sievers #endif
744da231fd5SKay Sievers 
7452ee97cafSCornelia Huck out_subsys:
7462ee97cafSCornelia Huck 	sysfs_remove_link(&dev->kobj, "subsystem");
7472ee97cafSCornelia Huck out:
7482ee97cafSCornelia Huck 	return error;
7492ee97cafSCornelia Huck }
7502ee97cafSCornelia Huck 
7512ee97cafSCornelia Huck static void device_remove_class_symlinks(struct device *dev)
7522ee97cafSCornelia Huck {
7532ee97cafSCornelia Huck 	if (!dev->class)
7542ee97cafSCornelia Huck 		return;
755da231fd5SKay Sievers 
7562ee97cafSCornelia Huck #ifdef CONFIG_SYSFS_DEPRECATED
7574e886c29SGreg Kroah-Hartman 	if (dev->parent && device_is_not_partition(dev)) {
7582ee97cafSCornelia Huck 		char *class_name;
7592ee97cafSCornelia Huck 
7602ee97cafSCornelia Huck 		class_name = make_class_name(dev->class->name, &dev->kobj);
7612ee97cafSCornelia Huck 		if (class_name) {
7622ee97cafSCornelia Huck 			sysfs_remove_link(&dev->parent->kobj, class_name);
7632ee97cafSCornelia Huck 			kfree(class_name);
7642ee97cafSCornelia Huck 		}
7652ee97cafSCornelia Huck 		sysfs_remove_link(&dev->kobj, "device");
7662ee97cafSCornelia Huck 	}
767da231fd5SKay Sievers 
7681fbfee6cSGreg Kroah-Hartman 	if (dev->kobj.parent != &dev->class->p->class_subsys.kobj &&
7694e886c29SGreg Kroah-Hartman 	    device_is_not_partition(dev))
7701fbfee6cSGreg Kroah-Hartman 		sysfs_remove_link(&dev->class->p->class_subsys.kobj,
7711e0b2cf9SKay Sievers 				  dev_name(dev));
772da231fd5SKay Sievers #else
7734e886c29SGreg Kroah-Hartman 	if (dev->parent && device_is_not_partition(dev))
774da231fd5SKay Sievers 		sysfs_remove_link(&dev->kobj, "device");
775da231fd5SKay Sievers 
7761e0b2cf9SKay Sievers 	sysfs_remove_link(&dev->class->p->class_subsys.kobj, dev_name(dev));
777da231fd5SKay Sievers #endif
778da231fd5SKay Sievers 
7792ee97cafSCornelia Huck 	sysfs_remove_link(&dev->kobj, "subsystem");
7802ee97cafSCornelia Huck }
7812ee97cafSCornelia Huck 
7821da177e4SLinus Torvalds /**
783413c239fSStephen Rothwell  * dev_set_name - set a device name
784413c239fSStephen Rothwell  * @dev: device
78546232366SRandy Dunlap  * @fmt: format string for the device's name
786413c239fSStephen Rothwell  */
787413c239fSStephen Rothwell int dev_set_name(struct device *dev, const char *fmt, ...)
788413c239fSStephen Rothwell {
789413c239fSStephen Rothwell 	va_list vargs;
790413c239fSStephen Rothwell 
791413c239fSStephen Rothwell 	va_start(vargs, fmt);
792413c239fSStephen Rothwell 	vsnprintf(dev->bus_id, sizeof(dev->bus_id), fmt, vargs);
793413c239fSStephen Rothwell 	va_end(vargs);
794413c239fSStephen Rothwell 	return 0;
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 
846e105b8bfSDan Williams /**
8471da177e4SLinus Torvalds  * device_add - add device to device hierarchy.
8481da177e4SLinus Torvalds  * @dev: device.
8491da177e4SLinus Torvalds  *
8501da177e4SLinus Torvalds  * This is part 2 of device_register(), though may be called
8511da177e4SLinus Torvalds  * separately _iff_ device_initialize() has been called separately.
8521da177e4SLinus Torvalds  *
8535739411aSCornelia Huck  * This adds @dev to the kobject hierarchy via kobject_add(), adds it
8541da177e4SLinus Torvalds  * to the global and sibling lists for the device, then
8551da177e4SLinus Torvalds  * adds it to the other relevant subsystems of the driver model.
8565739411aSCornelia Huck  *
8575739411aSCornelia Huck  * NOTE: _Never_ directly free @dev after calling this function, even
8585739411aSCornelia Huck  * if it returned an error! Always use put_device() to give up your
8595739411aSCornelia Huck  * reference instead.
8601da177e4SLinus Torvalds  */
8611da177e4SLinus Torvalds int device_add(struct device *dev)
8621da177e4SLinus Torvalds {
8631da177e4SLinus Torvalds 	struct device *parent = NULL;
864c47ed219SGreg Kroah-Hartman 	struct class_interface *class_intf;
865c906a48aSGreg Kroah-Hartman 	int error = -EINVAL;
866775b64d2SRafael J. Wysocki 
8671da177e4SLinus Torvalds 	dev = get_device(dev);
868c906a48aSGreg Kroah-Hartman 	if (!dev)
869c906a48aSGreg Kroah-Hartman 		goto done;
870c906a48aSGreg Kroah-Hartman 
871c906a48aSGreg Kroah-Hartman 	/* Temporarily support init_name if it is set.
872c906a48aSGreg Kroah-Hartman 	 * It will override bus_id for now */
873c906a48aSGreg Kroah-Hartman 	if (dev->init_name)
874c906a48aSGreg Kroah-Hartman 		dev_set_name(dev, "%s", dev->init_name);
875c906a48aSGreg Kroah-Hartman 
876c906a48aSGreg Kroah-Hartman 	if (!strlen(dev->bus_id))
877c906a48aSGreg Kroah-Hartman 		goto done;
8781da177e4SLinus Torvalds 
8791e0b2cf9SKay Sievers 	pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
880c205ef48SGreg Kroah-Hartman 
8811da177e4SLinus Torvalds 	parent = get_device(dev->parent);
88263b6971aSCornelia Huck 	setup_parent(dev, parent);
8831da177e4SLinus Torvalds 
8840d358f22SYinghai Lu 	/* use parent numa_node */
8850d358f22SYinghai Lu 	if (parent)
8860d358f22SYinghai Lu 		set_dev_node(dev, dev_to_node(parent));
8870d358f22SYinghai Lu 
8881da177e4SLinus Torvalds 	/* first, register with generic layer. */
8891e0b2cf9SKay Sievers 	error = kobject_add(&dev->kobj, dev->kobj.parent, "%s", dev_name(dev));
89040fa5422SGreg Kroah-Hartman 	if (error)
8911da177e4SLinus Torvalds 		goto Error;
892a7fd6706SKay Sievers 
89337022644SBrian Walsh 	/* notify platform of device entry */
89437022644SBrian Walsh 	if (platform_notify)
89537022644SBrian Walsh 		platform_notify(dev);
89637022644SBrian Walsh 
897ad6a1e1cSTejun Heo 	error = device_create_file(dev, &uevent_attr);
898a306eea4SCornelia Huck 	if (error)
899a306eea4SCornelia Huck 		goto attrError;
900a7fd6706SKay Sievers 
90123681e47SGreg Kroah-Hartman 	if (MAJOR(dev->devt)) {
902ad6a1e1cSTejun Heo 		error = device_create_file(dev, &devt_attr);
903ad6a1e1cSTejun Heo 		if (error)
904a306eea4SCornelia Huck 			goto ueventattrError;
905e105b8bfSDan Williams 
906e105b8bfSDan Williams 		error = device_create_sys_dev_entry(dev);
907e105b8bfSDan Williams 		if (error)
908e105b8bfSDan Williams 			goto devtattrError;
90923681e47SGreg Kroah-Hartman 	}
91023681e47SGreg Kroah-Hartman 
9112ee97cafSCornelia Huck 	error = device_add_class_symlinks(dev);
9122ee97cafSCornelia Huck 	if (error)
9132ee97cafSCornelia Huck 		goto SymlinkError;
914dc0afa83SCornelia Huck 	error = device_add_attrs(dev);
915dc0afa83SCornelia Huck 	if (error)
9162620efefSGreg Kroah-Hartman 		goto AttrsError;
917dc0afa83SCornelia Huck 	error = bus_add_device(dev);
918dc0afa83SCornelia Huck 	if (error)
9191da177e4SLinus Torvalds 		goto BusError;
9203b98aeafSAlan Stern 	error = dpm_sysfs_add(dev);
92157eee3d2SRafael J. Wysocki 	if (error)
9223b98aeafSAlan Stern 		goto DPMError;
9233b98aeafSAlan Stern 	device_pm_add(dev);
924ec0676eeSAlan Stern 
925ec0676eeSAlan Stern 	/* Notify clients of device addition.  This call must come
926ec0676eeSAlan Stern 	 * after dpm_sysf_add() and before kobject_uevent().
927ec0676eeSAlan Stern 	 */
928ec0676eeSAlan Stern 	if (dev->bus)
929ec0676eeSAlan Stern 		blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
930ec0676eeSAlan Stern 					     BUS_NOTIFY_ADD_DEVICE, dev);
931ec0676eeSAlan Stern 
93253877d06SKay Sievers 	kobject_uevent(&dev->kobj, KOBJ_ADD);
933c6a46696SCornelia Huck 	bus_attach_device(dev);
9341da177e4SLinus Torvalds 	if (parent)
93511c3b5c3SGreg Kroah-Hartman 		klist_add_tail(&dev->p->knode_parent,
93611c3b5c3SGreg Kroah-Hartman 			       &parent->p->klist_children);
9371da177e4SLinus Torvalds 
9385d9fd169SGreg Kroah-Hartman 	if (dev->class) {
939f75b1c60SDave Young 		mutex_lock(&dev->class->p->class_mutex);
940c47ed219SGreg Kroah-Hartman 		/* tie the class to the device */
9415a3ceb86STejun Heo 		klist_add_tail(&dev->knode_class,
9425a3ceb86STejun Heo 			       &dev->class->p->class_devices);
943c47ed219SGreg Kroah-Hartman 
944c47ed219SGreg Kroah-Hartman 		/* notify any interfaces that the device is here */
945184f1f77SGreg Kroah-Hartman 		list_for_each_entry(class_intf,
946184f1f77SGreg Kroah-Hartman 				    &dev->class->p->class_interfaces, node)
947c47ed219SGreg Kroah-Hartman 			if (class_intf->add_dev)
948c47ed219SGreg Kroah-Hartman 				class_intf->add_dev(dev, class_intf);
949f75b1c60SDave Young 		mutex_unlock(&dev->class->p->class_mutex);
9505d9fd169SGreg Kroah-Hartman 	}
951c906a48aSGreg Kroah-Hartman done:
9521da177e4SLinus Torvalds 	put_device(dev);
9531da177e4SLinus Torvalds 	return error;
9543b98aeafSAlan Stern  DPMError:
95557eee3d2SRafael J. Wysocki 	bus_remove_device(dev);
95657eee3d2SRafael J. Wysocki  BusError:
9572620efefSGreg Kroah-Hartman 	device_remove_attrs(dev);
9582620efefSGreg Kroah-Hartman  AttrsError:
9592ee97cafSCornelia Huck 	device_remove_class_symlinks(dev);
9602ee97cafSCornelia Huck  SymlinkError:
961ad6a1e1cSTejun Heo 	if (MAJOR(dev->devt))
962e105b8bfSDan Williams 		device_remove_sys_dev_entry(dev);
963e105b8bfSDan Williams  devtattrError:
964e105b8bfSDan Williams 	if (MAJOR(dev->devt))
965ad6a1e1cSTejun Heo 		device_remove_file(dev, &devt_attr);
966a306eea4SCornelia Huck  ueventattrError:
967ad6a1e1cSTejun Heo 	device_remove_file(dev, &uevent_attr);
96823681e47SGreg Kroah-Hartman  attrError:
969312c004dSKay Sievers 	kobject_uevent(&dev->kobj, KOBJ_REMOVE);
9701da177e4SLinus Torvalds 	kobject_del(&dev->kobj);
9711da177e4SLinus Torvalds  Error:
97263b6971aSCornelia Huck 	cleanup_device_parent(dev);
9731da177e4SLinus Torvalds 	if (parent)
9741da177e4SLinus Torvalds 		put_device(parent);
975c906a48aSGreg Kroah-Hartman 	goto done;
9761da177e4SLinus Torvalds }
9771da177e4SLinus Torvalds 
9781da177e4SLinus Torvalds /**
9791da177e4SLinus Torvalds  * device_register - register a device with the system.
9801da177e4SLinus Torvalds  * @dev: pointer to the device structure
9811da177e4SLinus Torvalds  *
9821da177e4SLinus Torvalds  * This happens in two clean steps - initialize the device
9831da177e4SLinus Torvalds  * and add it to the system. The two steps can be called
9841da177e4SLinus Torvalds  * separately, but this is the easiest and most common.
9851da177e4SLinus Torvalds  * I.e. you should only call the two helpers separately if
9861da177e4SLinus Torvalds  * have a clearly defined need to use and refcount the device
9871da177e4SLinus Torvalds  * before it is added to the hierarchy.
9885739411aSCornelia Huck  *
9895739411aSCornelia Huck  * NOTE: _Never_ directly free @dev after calling this function, even
9905739411aSCornelia Huck  * if it returned an error! Always use put_device() to give up the
9915739411aSCornelia Huck  * reference initialized in this function instead.
9921da177e4SLinus Torvalds  */
9931da177e4SLinus Torvalds int device_register(struct device *dev)
9941da177e4SLinus Torvalds {
9951da177e4SLinus Torvalds 	device_initialize(dev);
9961da177e4SLinus Torvalds 	return device_add(dev);
9971da177e4SLinus Torvalds }
9981da177e4SLinus Torvalds 
9991da177e4SLinus Torvalds /**
10001da177e4SLinus Torvalds  * get_device - increment reference count for device.
10011da177e4SLinus Torvalds  * @dev: device.
10021da177e4SLinus Torvalds  *
10031da177e4SLinus Torvalds  * This simply forwards the call to kobject_get(), though
10041da177e4SLinus Torvalds  * we do take care to provide for the case that we get a NULL
10051da177e4SLinus Torvalds  * pointer passed in.
10061da177e4SLinus Torvalds  */
10071da177e4SLinus Torvalds struct device *get_device(struct device *dev)
10081da177e4SLinus Torvalds {
10091da177e4SLinus Torvalds 	return dev ? to_dev(kobject_get(&dev->kobj)) : NULL;
10101da177e4SLinus Torvalds }
10111da177e4SLinus Torvalds 
10121da177e4SLinus Torvalds /**
10131da177e4SLinus Torvalds  * put_device - decrement reference count.
10141da177e4SLinus Torvalds  * @dev: device in question.
10151da177e4SLinus Torvalds  */
10161da177e4SLinus Torvalds void put_device(struct device *dev)
10171da177e4SLinus Torvalds {
1018edfaa7c3SKay Sievers 	/* might_sleep(); */
10191da177e4SLinus Torvalds 	if (dev)
10201da177e4SLinus Torvalds 		kobject_put(&dev->kobj);
10211da177e4SLinus Torvalds }
10221da177e4SLinus Torvalds 
10231da177e4SLinus Torvalds /**
10241da177e4SLinus Torvalds  * device_del - delete device from system.
10251da177e4SLinus Torvalds  * @dev: device.
10261da177e4SLinus Torvalds  *
10271da177e4SLinus Torvalds  * This is the first part of the device unregistration
10281da177e4SLinus Torvalds  * sequence. This removes the device from the lists we control
10291da177e4SLinus Torvalds  * from here, has it removed from the other driver model
10301da177e4SLinus Torvalds  * subsystems it was added to in device_add(), and removes it
10311da177e4SLinus Torvalds  * from the kobject hierarchy.
10321da177e4SLinus Torvalds  *
10331da177e4SLinus Torvalds  * NOTE: this should be called manually _iff_ device_add() was
10341da177e4SLinus Torvalds  * also called manually.
10351da177e4SLinus Torvalds  */
10361da177e4SLinus Torvalds void device_del(struct device *dev)
10371da177e4SLinus Torvalds {
10381da177e4SLinus Torvalds 	struct device *parent = dev->parent;
1039c47ed219SGreg Kroah-Hartman 	struct class_interface *class_intf;
10401da177e4SLinus Torvalds 
1041ec0676eeSAlan Stern 	/* Notify clients of device removal.  This call must come
1042ec0676eeSAlan Stern 	 * before dpm_sysfs_remove().
1043ec0676eeSAlan Stern 	 */
1044ec0676eeSAlan Stern 	if (dev->bus)
1045ec0676eeSAlan Stern 		blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
1046ec0676eeSAlan Stern 					     BUS_NOTIFY_DEL_DEVICE, dev);
1047775b64d2SRafael J. Wysocki 	device_pm_remove(dev);
10483b98aeafSAlan Stern 	dpm_sysfs_remove(dev);
10491da177e4SLinus Torvalds 	if (parent)
105011c3b5c3SGreg Kroah-Hartman 		klist_del(&dev->p->knode_parent);
1051e105b8bfSDan Williams 	if (MAJOR(dev->devt)) {
1052e105b8bfSDan Williams 		device_remove_sys_dev_entry(dev);
1053ad6a1e1cSTejun Heo 		device_remove_file(dev, &devt_attr);
1054e105b8bfSDan Williams 	}
1055b9d9c82bSKay Sievers 	if (dev->class) {
1056da231fd5SKay Sievers 		device_remove_class_symlinks(dev);
105799ef3ef8SKay Sievers 
1058f75b1c60SDave Young 		mutex_lock(&dev->class->p->class_mutex);
1059c47ed219SGreg Kroah-Hartman 		/* notify any interfaces that the device is now gone */
1060184f1f77SGreg Kroah-Hartman 		list_for_each_entry(class_intf,
1061184f1f77SGreg Kroah-Hartman 				    &dev->class->p->class_interfaces, node)
1062c47ed219SGreg Kroah-Hartman 			if (class_intf->remove_dev)
1063c47ed219SGreg Kroah-Hartman 				class_intf->remove_dev(dev, class_intf);
1064c47ed219SGreg Kroah-Hartman 		/* remove the device from the class list */
10655a3ceb86STejun Heo 		klist_del(&dev->knode_class);
1066f75b1c60SDave Young 		mutex_unlock(&dev->class->p->class_mutex);
1067b9d9c82bSKay Sievers 	}
1068ad6a1e1cSTejun Heo 	device_remove_file(dev, &uevent_attr);
10692620efefSGreg Kroah-Hartman 	device_remove_attrs(dev);
107028953533SBenjamin Herrenschmidt 	bus_remove_device(dev);
10711da177e4SLinus Torvalds 
10722f8d16a9STejun Heo 	/*
10732f8d16a9STejun Heo 	 * Some platform devices are driven without driver attached
10742f8d16a9STejun Heo 	 * and managed resources may have been acquired.  Make sure
10752f8d16a9STejun Heo 	 * all resources are released.
10762f8d16a9STejun Heo 	 */
10772f8d16a9STejun Heo 	devres_release_all(dev);
10782f8d16a9STejun Heo 
10791da177e4SLinus Torvalds 	/* Notify the platform of the removal, in case they
10801da177e4SLinus Torvalds 	 * need to do anything...
10811da177e4SLinus Torvalds 	 */
10821da177e4SLinus Torvalds 	if (platform_notify_remove)
10831da177e4SLinus Torvalds 		platform_notify_remove(dev);
1084312c004dSKay Sievers 	kobject_uevent(&dev->kobj, KOBJ_REMOVE);
1085da231fd5SKay Sievers 	cleanup_device_parent(dev);
10861da177e4SLinus Torvalds 	kobject_del(&dev->kobj);
10871da177e4SLinus Torvalds 	put_device(parent);
10881da177e4SLinus Torvalds }
10891da177e4SLinus Torvalds 
10901da177e4SLinus Torvalds /**
10911da177e4SLinus Torvalds  * device_unregister - unregister device from system.
10921da177e4SLinus Torvalds  * @dev: device going away.
10931da177e4SLinus Torvalds  *
10941da177e4SLinus Torvalds  * We do this in two parts, like we do device_register(). First,
10951da177e4SLinus Torvalds  * we remove it from all the subsystems with device_del(), then
10961da177e4SLinus Torvalds  * we decrement the reference count via put_device(). If that
10971da177e4SLinus Torvalds  * is the final reference count, the device will be cleaned up
10981da177e4SLinus Torvalds  * via device_release() above. Otherwise, the structure will
10991da177e4SLinus Torvalds  * stick around until the final reference to the device is dropped.
11001da177e4SLinus Torvalds  */
11011da177e4SLinus Torvalds void device_unregister(struct device *dev)
11021da177e4SLinus Torvalds {
11031e0b2cf9SKay Sievers 	pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
11041da177e4SLinus Torvalds 	device_del(dev);
11051da177e4SLinus Torvalds 	put_device(dev);
11061da177e4SLinus Torvalds }
11071da177e4SLinus Torvalds 
110836239577Smochel@digitalimplant.org static struct device *next_device(struct klist_iter *i)
110936239577Smochel@digitalimplant.org {
111036239577Smochel@digitalimplant.org 	struct klist_node *n = klist_next(i);
111111c3b5c3SGreg Kroah-Hartman 	struct device *dev = NULL;
111211c3b5c3SGreg Kroah-Hartman 	struct device_private *p;
111311c3b5c3SGreg Kroah-Hartman 
111411c3b5c3SGreg Kroah-Hartman 	if (n) {
111511c3b5c3SGreg Kroah-Hartman 		p = to_device_private_parent(n);
111611c3b5c3SGreg Kroah-Hartman 		dev = p->device;
111711c3b5c3SGreg Kroah-Hartman 	}
111811c3b5c3SGreg Kroah-Hartman 	return dev;
111936239577Smochel@digitalimplant.org }
112036239577Smochel@digitalimplant.org 
11211da177e4SLinus Torvalds /**
11221da177e4SLinus Torvalds  * device_for_each_child - device child iterator.
1123c41455fbSRandy Dunlap  * @parent: parent struct device.
11241da177e4SLinus Torvalds  * @data: data for the callback.
11251da177e4SLinus Torvalds  * @fn: function to be called for each device.
11261da177e4SLinus Torvalds  *
1127c41455fbSRandy Dunlap  * Iterate over @parent's child devices, and call @fn for each,
11281da177e4SLinus Torvalds  * passing it @data.
11291da177e4SLinus Torvalds  *
11301da177e4SLinus Torvalds  * We check the return of @fn each time. If it returns anything
11311da177e4SLinus Torvalds  * other than 0, we break out and return that value.
11321da177e4SLinus Torvalds  */
113336239577Smochel@digitalimplant.org int device_for_each_child(struct device *parent, void *data,
11344a3ad20cSGreg Kroah-Hartman 			  int (*fn)(struct device *dev, void *data))
11351da177e4SLinus Torvalds {
113636239577Smochel@digitalimplant.org 	struct klist_iter i;
11371da177e4SLinus Torvalds 	struct device *child;
11381da177e4SLinus Torvalds 	int error = 0;
11391da177e4SLinus Torvalds 
114011c3b5c3SGreg Kroah-Hartman 	klist_iter_init(&parent->p->klist_children, &i);
114136239577Smochel@digitalimplant.org 	while ((child = next_device(&i)) && !error)
114236239577Smochel@digitalimplant.org 		error = fn(child, data);
114336239577Smochel@digitalimplant.org 	klist_iter_exit(&i);
11441da177e4SLinus Torvalds 	return error;
11451da177e4SLinus Torvalds }
11461da177e4SLinus Torvalds 
11475ab69981SCornelia Huck /**
11485ab69981SCornelia Huck  * device_find_child - device iterator for locating a particular device.
11495ab69981SCornelia Huck  * @parent: parent struct device
11505ab69981SCornelia Huck  * @data: Data to pass to match function
11515ab69981SCornelia Huck  * @match: Callback function to check device
11525ab69981SCornelia Huck  *
11535ab69981SCornelia Huck  * This is similar to the device_for_each_child() function above, but it
11545ab69981SCornelia Huck  * returns a reference to a device that is 'found' for later use, as
11555ab69981SCornelia Huck  * determined by the @match callback.
11565ab69981SCornelia Huck  *
11575ab69981SCornelia Huck  * The callback should return 0 if the device doesn't match and non-zero
11585ab69981SCornelia Huck  * if it does.  If the callback returns non-zero and a reference to the
11595ab69981SCornelia Huck  * current device can be obtained, this function will return to the caller
11605ab69981SCornelia Huck  * and not iterate over any more devices.
11615ab69981SCornelia Huck  */
11625ab69981SCornelia Huck struct device *device_find_child(struct device *parent, void *data,
11634a3ad20cSGreg Kroah-Hartman 				 int (*match)(struct device *dev, void *data))
11645ab69981SCornelia Huck {
11655ab69981SCornelia Huck 	struct klist_iter i;
11665ab69981SCornelia Huck 	struct device *child;
11675ab69981SCornelia Huck 
11685ab69981SCornelia Huck 	if (!parent)
11695ab69981SCornelia Huck 		return NULL;
11705ab69981SCornelia Huck 
117111c3b5c3SGreg Kroah-Hartman 	klist_iter_init(&parent->p->klist_children, &i);
11725ab69981SCornelia Huck 	while ((child = next_device(&i)))
11735ab69981SCornelia Huck 		if (match(child, data) && get_device(child))
11745ab69981SCornelia Huck 			break;
11755ab69981SCornelia Huck 	klist_iter_exit(&i);
11765ab69981SCornelia Huck 	return child;
11775ab69981SCornelia Huck }
11785ab69981SCornelia Huck 
11791da177e4SLinus Torvalds int __init devices_init(void)
11801da177e4SLinus Torvalds {
1181881c6cfdSGreg Kroah-Hartman 	devices_kset = kset_create_and_add("devices", &device_uevent_ops, NULL);
1182881c6cfdSGreg Kroah-Hartman 	if (!devices_kset)
1183881c6cfdSGreg Kroah-Hartman 		return -ENOMEM;
1184e105b8bfSDan Williams 	dev_kobj = kobject_create_and_add("dev", NULL);
1185e105b8bfSDan Williams 	if (!dev_kobj)
1186e105b8bfSDan Williams 		goto dev_kobj_err;
1187e105b8bfSDan Williams 	sysfs_dev_block_kobj = kobject_create_and_add("block", dev_kobj);
1188e105b8bfSDan Williams 	if (!sysfs_dev_block_kobj)
1189e105b8bfSDan Williams 		goto block_kobj_err;
1190e105b8bfSDan Williams 	sysfs_dev_char_kobj = kobject_create_and_add("char", dev_kobj);
1191e105b8bfSDan Williams 	if (!sysfs_dev_char_kobj)
1192e105b8bfSDan Williams 		goto char_kobj_err;
1193e105b8bfSDan Williams 
1194881c6cfdSGreg Kroah-Hartman 	return 0;
1195e105b8bfSDan Williams 
1196e105b8bfSDan Williams  char_kobj_err:
1197e105b8bfSDan Williams 	kobject_put(sysfs_dev_block_kobj);
1198e105b8bfSDan Williams  block_kobj_err:
1199e105b8bfSDan Williams 	kobject_put(dev_kobj);
1200e105b8bfSDan Williams  dev_kobj_err:
1201e105b8bfSDan Williams 	kset_unregister(devices_kset);
1202e105b8bfSDan Williams 	return -ENOMEM;
12031da177e4SLinus Torvalds }
12041da177e4SLinus Torvalds 
12051da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(device_for_each_child);
12065ab69981SCornelia Huck EXPORT_SYMBOL_GPL(device_find_child);
12071da177e4SLinus Torvalds 
12081da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(device_initialize);
12091da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(device_add);
12101da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(device_register);
12111da177e4SLinus Torvalds 
12121da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(device_del);
12131da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(device_unregister);
12141da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(get_device);
12151da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(put_device);
12161da177e4SLinus Torvalds 
12171da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(device_create_file);
12181da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(device_remove_file);
121923681e47SGreg Kroah-Hartman 
12200aa0dc41SMark McLoughlin struct root_device
12210aa0dc41SMark McLoughlin {
12220aa0dc41SMark McLoughlin 	struct device dev;
12230aa0dc41SMark McLoughlin 	struct module *owner;
12240aa0dc41SMark McLoughlin };
12250aa0dc41SMark McLoughlin 
12260aa0dc41SMark McLoughlin #define to_root_device(dev) container_of(dev, struct root_device, dev)
12270aa0dc41SMark McLoughlin 
12280aa0dc41SMark McLoughlin static void root_device_release(struct device *dev)
12290aa0dc41SMark McLoughlin {
12300aa0dc41SMark McLoughlin 	kfree(to_root_device(dev));
12310aa0dc41SMark McLoughlin }
12320aa0dc41SMark McLoughlin 
12330aa0dc41SMark McLoughlin /**
12340aa0dc41SMark McLoughlin  * __root_device_register - allocate and register a root device
12350aa0dc41SMark McLoughlin  * @name: root device name
12360aa0dc41SMark McLoughlin  * @owner: owner module of the root device, usually THIS_MODULE
12370aa0dc41SMark McLoughlin  *
12380aa0dc41SMark McLoughlin  * This function allocates a root device and registers it
12390aa0dc41SMark McLoughlin  * using device_register(). In order to free the returned
12400aa0dc41SMark McLoughlin  * device, use root_device_unregister().
12410aa0dc41SMark McLoughlin  *
12420aa0dc41SMark McLoughlin  * Root devices are dummy devices which allow other devices
12430aa0dc41SMark McLoughlin  * to be grouped under /sys/devices. Use this function to
12440aa0dc41SMark McLoughlin  * allocate a root device and then use it as the parent of
12450aa0dc41SMark McLoughlin  * any device which should appear under /sys/devices/{name}
12460aa0dc41SMark McLoughlin  *
12470aa0dc41SMark McLoughlin  * The /sys/devices/{name} directory will also contain a
12480aa0dc41SMark McLoughlin  * 'module' symlink which points to the @owner directory
12490aa0dc41SMark McLoughlin  * in sysfs.
12500aa0dc41SMark McLoughlin  *
12510aa0dc41SMark McLoughlin  * Note: You probably want to use root_device_register().
12520aa0dc41SMark McLoughlin  */
12530aa0dc41SMark McLoughlin struct device *__root_device_register(const char *name, struct module *owner)
12540aa0dc41SMark McLoughlin {
12550aa0dc41SMark McLoughlin 	struct root_device *root;
12560aa0dc41SMark McLoughlin 	int err = -ENOMEM;
12570aa0dc41SMark McLoughlin 
12580aa0dc41SMark McLoughlin 	root = kzalloc(sizeof(struct root_device), GFP_KERNEL);
12590aa0dc41SMark McLoughlin 	if (!root)
12600aa0dc41SMark McLoughlin 		return ERR_PTR(err);
12610aa0dc41SMark McLoughlin 
12620aa0dc41SMark McLoughlin 	err = dev_set_name(&root->dev, name);
12630aa0dc41SMark McLoughlin 	if (err) {
12640aa0dc41SMark McLoughlin 		kfree(root);
12650aa0dc41SMark McLoughlin 		return ERR_PTR(err);
12660aa0dc41SMark McLoughlin 	}
12670aa0dc41SMark McLoughlin 
12680aa0dc41SMark McLoughlin 	root->dev.release = root_device_release;
12690aa0dc41SMark McLoughlin 
12700aa0dc41SMark McLoughlin 	err = device_register(&root->dev);
12710aa0dc41SMark McLoughlin 	if (err) {
12720aa0dc41SMark McLoughlin 		put_device(&root->dev);
12730aa0dc41SMark McLoughlin 		return ERR_PTR(err);
12740aa0dc41SMark McLoughlin 	}
12750aa0dc41SMark McLoughlin 
12760aa0dc41SMark McLoughlin #ifdef CONFIG_MODULE	/* gotta find a "cleaner" way to do this */
12770aa0dc41SMark McLoughlin 	if (owner) {
12780aa0dc41SMark McLoughlin 		struct module_kobject *mk = &owner->mkobj;
12790aa0dc41SMark McLoughlin 
12800aa0dc41SMark McLoughlin 		err = sysfs_create_link(&root->dev.kobj, &mk->kobj, "module");
12810aa0dc41SMark McLoughlin 		if (err) {
12820aa0dc41SMark McLoughlin 			device_unregister(&root->dev);
12830aa0dc41SMark McLoughlin 			return ERR_PTR(err);
12840aa0dc41SMark McLoughlin 		}
12850aa0dc41SMark McLoughlin 		root->owner = owner;
12860aa0dc41SMark McLoughlin 	}
12870aa0dc41SMark McLoughlin #endif
12880aa0dc41SMark McLoughlin 
12890aa0dc41SMark McLoughlin 	return &root->dev;
12900aa0dc41SMark McLoughlin }
12910aa0dc41SMark McLoughlin EXPORT_SYMBOL_GPL(__root_device_register);
12920aa0dc41SMark McLoughlin 
12930aa0dc41SMark McLoughlin /**
12940aa0dc41SMark McLoughlin  * root_device_unregister - unregister and free a root device
12950aa0dc41SMark McLoughlin  * @root: device going away.
12960aa0dc41SMark McLoughlin  *
12970aa0dc41SMark McLoughlin  * This function unregisters and cleans up a device that was created by
12980aa0dc41SMark McLoughlin  * root_device_register().
12990aa0dc41SMark McLoughlin  */
13000aa0dc41SMark McLoughlin void root_device_unregister(struct device *dev)
13010aa0dc41SMark McLoughlin {
13020aa0dc41SMark McLoughlin 	struct root_device *root = to_root_device(dev);
13030aa0dc41SMark McLoughlin 
13040aa0dc41SMark McLoughlin 	if (root->owner)
13050aa0dc41SMark McLoughlin 		sysfs_remove_link(&root->dev.kobj, "module");
13060aa0dc41SMark McLoughlin 
13070aa0dc41SMark McLoughlin 	device_unregister(dev);
13080aa0dc41SMark McLoughlin }
13090aa0dc41SMark McLoughlin EXPORT_SYMBOL_GPL(root_device_unregister);
13100aa0dc41SMark McLoughlin 
131123681e47SGreg Kroah-Hartman 
131223681e47SGreg Kroah-Hartman static void device_create_release(struct device *dev)
131323681e47SGreg Kroah-Hartman {
13141e0b2cf9SKay Sievers 	pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
131523681e47SGreg Kroah-Hartman 	kfree(dev);
131623681e47SGreg Kroah-Hartman }
131723681e47SGreg Kroah-Hartman 
131823681e47SGreg Kroah-Hartman /**
13198882b394SGreg Kroah-Hartman  * device_create_vargs - creates a device and registers it with sysfs
13208882b394SGreg Kroah-Hartman  * @class: pointer to the struct class that this device should be registered to
13218882b394SGreg Kroah-Hartman  * @parent: pointer to the parent struct device of this new device, if any
13228882b394SGreg Kroah-Hartman  * @devt: the dev_t for the char device to be added
13238882b394SGreg Kroah-Hartman  * @drvdata: the data to be added to the device for callbacks
13248882b394SGreg Kroah-Hartman  * @fmt: string for the device's name
13258882b394SGreg Kroah-Hartman  * @args: va_list for the device's name
13268882b394SGreg Kroah-Hartman  *
13278882b394SGreg Kroah-Hartman  * This function can be used by char device classes.  A struct device
13288882b394SGreg Kroah-Hartman  * will be created in sysfs, registered to the specified class.
13298882b394SGreg Kroah-Hartman  *
13308882b394SGreg Kroah-Hartman  * A "dev" file will be created, showing the dev_t for the device, if
13318882b394SGreg Kroah-Hartman  * the dev_t is not 0,0.
13328882b394SGreg Kroah-Hartman  * If a pointer to a parent struct device is passed in, the newly created
13338882b394SGreg Kroah-Hartman  * struct device will be a child of that device in sysfs.
13348882b394SGreg Kroah-Hartman  * The pointer to the struct device will be returned from the call.
13358882b394SGreg Kroah-Hartman  * Any further sysfs files that might be required can be created using this
13368882b394SGreg Kroah-Hartman  * pointer.
13378882b394SGreg Kroah-Hartman  *
13388882b394SGreg Kroah-Hartman  * Note: the struct class passed to this function must have previously
13398882b394SGreg Kroah-Hartman  * been created with a call to class_create().
13408882b394SGreg Kroah-Hartman  */
13418882b394SGreg Kroah-Hartman struct device *device_create_vargs(struct class *class, struct device *parent,
13428882b394SGreg Kroah-Hartman 				   dev_t devt, void *drvdata, const char *fmt,
13438882b394SGreg Kroah-Hartman 				   va_list args)
13448882b394SGreg Kroah-Hartman {
13458882b394SGreg Kroah-Hartman 	struct device *dev = NULL;
13468882b394SGreg Kroah-Hartman 	int retval = -ENODEV;
13478882b394SGreg Kroah-Hartman 
13488882b394SGreg Kroah-Hartman 	if (class == NULL || IS_ERR(class))
13498882b394SGreg Kroah-Hartman 		goto error;
13508882b394SGreg Kroah-Hartman 
13518882b394SGreg Kroah-Hartman 	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
13528882b394SGreg Kroah-Hartman 	if (!dev) {
13538882b394SGreg Kroah-Hartman 		retval = -ENOMEM;
13548882b394SGreg Kroah-Hartman 		goto error;
13558882b394SGreg Kroah-Hartman 	}
13568882b394SGreg Kroah-Hartman 
13578882b394SGreg Kroah-Hartman 	dev->devt = devt;
13588882b394SGreg Kroah-Hartman 	dev->class = class;
13598882b394SGreg Kroah-Hartman 	dev->parent = parent;
13608882b394SGreg Kroah-Hartman 	dev->release = device_create_release;
13618882b394SGreg Kroah-Hartman 	dev_set_drvdata(dev, drvdata);
13628882b394SGreg Kroah-Hartman 
13638882b394SGreg Kroah-Hartman 	vsnprintf(dev->bus_id, BUS_ID_SIZE, fmt, args);
13648882b394SGreg Kroah-Hartman 	retval = device_register(dev);
13658882b394SGreg Kroah-Hartman 	if (retval)
13668882b394SGreg Kroah-Hartman 		goto error;
13678882b394SGreg Kroah-Hartman 
13688882b394SGreg Kroah-Hartman 	return dev;
13698882b394SGreg Kroah-Hartman 
13708882b394SGreg Kroah-Hartman error:
1371286661b3SCornelia Huck 	put_device(dev);
13728882b394SGreg Kroah-Hartman 	return ERR_PTR(retval);
13738882b394SGreg Kroah-Hartman }
13748882b394SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(device_create_vargs);
13758882b394SGreg Kroah-Hartman 
13768882b394SGreg Kroah-Hartman /**
13774e106739SGreg Kroah-Hartman  * device_create - creates a device and registers it with sysfs
13788882b394SGreg Kroah-Hartman  * @class: pointer to the struct class that this device should be registered to
13798882b394SGreg Kroah-Hartman  * @parent: pointer to the parent struct device of this new device, if any
13808882b394SGreg Kroah-Hartman  * @devt: the dev_t for the char device to be added
13818882b394SGreg Kroah-Hartman  * @drvdata: the data to be added to the device for callbacks
13828882b394SGreg Kroah-Hartman  * @fmt: string for the device's name
13838882b394SGreg Kroah-Hartman  *
13848882b394SGreg Kroah-Hartman  * This function can be used by char device classes.  A struct device
13858882b394SGreg Kroah-Hartman  * will be created in sysfs, registered to the specified class.
13868882b394SGreg Kroah-Hartman  *
13878882b394SGreg Kroah-Hartman  * A "dev" file will be created, showing the dev_t for the device, if
13888882b394SGreg Kroah-Hartman  * the dev_t is not 0,0.
13898882b394SGreg Kroah-Hartman  * If a pointer to a parent struct device is passed in, the newly created
13908882b394SGreg Kroah-Hartman  * struct device will be a child of that device in sysfs.
13918882b394SGreg Kroah-Hartman  * The pointer to the struct device will be returned from the call.
13928882b394SGreg Kroah-Hartman  * Any further sysfs files that might be required can be created using this
13938882b394SGreg Kroah-Hartman  * pointer.
13948882b394SGreg Kroah-Hartman  *
13958882b394SGreg Kroah-Hartman  * Note: the struct class passed to this function must have previously
13968882b394SGreg Kroah-Hartman  * been created with a call to class_create().
13978882b394SGreg Kroah-Hartman  */
13984e106739SGreg Kroah-Hartman struct device *device_create(struct class *class, struct device *parent,
13994e106739SGreg Kroah-Hartman 			     dev_t devt, void *drvdata, const char *fmt, ...)
14008882b394SGreg Kroah-Hartman {
14018882b394SGreg Kroah-Hartman 	va_list vargs;
14028882b394SGreg Kroah-Hartman 	struct device *dev;
14038882b394SGreg Kroah-Hartman 
14048882b394SGreg Kroah-Hartman 	va_start(vargs, fmt);
14058882b394SGreg Kroah-Hartman 	dev = device_create_vargs(class, parent, devt, drvdata, fmt, vargs);
14068882b394SGreg Kroah-Hartman 	va_end(vargs);
14078882b394SGreg Kroah-Hartman 	return dev;
14088882b394SGreg Kroah-Hartman }
14094e106739SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(device_create);
14108882b394SGreg Kroah-Hartman 
1411cd35449bSDave Young static int __match_devt(struct device *dev, void *data)
141223681e47SGreg Kroah-Hartman {
1413cd35449bSDave Young 	dev_t *devt = data;
141423681e47SGreg Kroah-Hartman 
1415cd35449bSDave Young 	return dev->devt == *devt;
1416775b64d2SRafael J. Wysocki }
141723681e47SGreg Kroah-Hartman 
1418775b64d2SRafael J. Wysocki /**
1419775b64d2SRafael J. Wysocki  * device_destroy - removes a device that was created with device_create()
1420775b64d2SRafael J. Wysocki  * @class: pointer to the struct class that this device was registered with
1421775b64d2SRafael J. Wysocki  * @devt: the dev_t of the device that was previously registered
1422775b64d2SRafael J. Wysocki  *
1423775b64d2SRafael J. Wysocki  * This call unregisters and cleans up a device that was created with a
1424775b64d2SRafael J. Wysocki  * call to device_create().
1425775b64d2SRafael J. Wysocki  */
1426775b64d2SRafael J. Wysocki void device_destroy(struct class *class, dev_t devt)
1427775b64d2SRafael J. Wysocki {
1428775b64d2SRafael J. Wysocki 	struct device *dev;
1429775b64d2SRafael J. Wysocki 
1430695794aeSGreg Kroah-Hartman 	dev = class_find_device(class, NULL, &devt, __match_devt);
1431cd35449bSDave Young 	if (dev) {
1432cd35449bSDave Young 		put_device(dev);
143323681e47SGreg Kroah-Hartman 		device_unregister(dev);
143423681e47SGreg Kroah-Hartman 	}
1435cd35449bSDave Young }
143623681e47SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(device_destroy);
1437a2de48caSGreg Kroah-Hartman 
1438a2de48caSGreg Kroah-Hartman /**
1439a2de48caSGreg Kroah-Hartman  * device_rename - renames a device
1440a2de48caSGreg Kroah-Hartman  * @dev: the pointer to the struct device to be renamed
1441a2de48caSGreg Kroah-Hartman  * @new_name: the new name of the device
1442030c1d2bSEric W. Biederman  *
1443030c1d2bSEric W. Biederman  * It is the responsibility of the caller to provide mutual
1444030c1d2bSEric W. Biederman  * exclusion between two different calls of device_rename
1445030c1d2bSEric W. Biederman  * on the same device to ensure that new_name is valid and
1446030c1d2bSEric W. Biederman  * won't conflict with other devices.
1447a2de48caSGreg Kroah-Hartman  */
1448a2de48caSGreg Kroah-Hartman int device_rename(struct device *dev, char *new_name)
1449a2de48caSGreg Kroah-Hartman {
1450a2de48caSGreg Kroah-Hartman 	char *old_class_name = NULL;
1451a2de48caSGreg Kroah-Hartman 	char *new_class_name = NULL;
14522ee97cafSCornelia Huck 	char *old_device_name = NULL;
1453a2de48caSGreg Kroah-Hartman 	int error;
1454a2de48caSGreg Kroah-Hartman 
1455a2de48caSGreg Kroah-Hartman 	dev = get_device(dev);
1456a2de48caSGreg Kroah-Hartman 	if (!dev)
1457a2de48caSGreg Kroah-Hartman 		return -EINVAL;
1458a2de48caSGreg Kroah-Hartman 
14591e0b2cf9SKay Sievers 	pr_debug("device: '%s': %s: renaming to '%s'\n", dev_name(dev),
14602b3a302aSHarvey Harrison 		 __func__, new_name);
1461a2de48caSGreg Kroah-Hartman 
146299ef3ef8SKay Sievers #ifdef CONFIG_SYSFS_DEPRECATED
1463a2de48caSGreg Kroah-Hartman 	if ((dev->class) && (dev->parent))
1464a2de48caSGreg Kroah-Hartman 		old_class_name = make_class_name(dev->class->name, &dev->kobj);
146599ef3ef8SKay Sievers #endif
1466a2de48caSGreg Kroah-Hartman 
14672ee97cafSCornelia Huck 	old_device_name = kmalloc(BUS_ID_SIZE, GFP_KERNEL);
14682ee97cafSCornelia Huck 	if (!old_device_name) {
1469952ab431SJesper Juhl 		error = -ENOMEM;
14702ee97cafSCornelia Huck 		goto out;
1471952ab431SJesper Juhl 	}
14722ee97cafSCornelia Huck 	strlcpy(old_device_name, dev->bus_id, BUS_ID_SIZE);
1473a2de48caSGreg Kroah-Hartman 	strlcpy(dev->bus_id, new_name, BUS_ID_SIZE);
1474a2de48caSGreg Kroah-Hartman 
1475a2de48caSGreg Kroah-Hartman 	error = kobject_rename(&dev->kobj, new_name);
14762ee97cafSCornelia Huck 	if (error) {
14772ee97cafSCornelia Huck 		strlcpy(dev->bus_id, old_device_name, BUS_ID_SIZE);
14782ee97cafSCornelia Huck 		goto out;
14792ee97cafSCornelia Huck 	}
1480a2de48caSGreg Kroah-Hartman 
148199ef3ef8SKay Sievers #ifdef CONFIG_SYSFS_DEPRECATED
1482a2de48caSGreg Kroah-Hartman 	if (old_class_name) {
1483a2de48caSGreg Kroah-Hartman 		new_class_name = make_class_name(dev->class->name, &dev->kobj);
1484a2de48caSGreg Kroah-Hartman 		if (new_class_name) {
148536ce6dadSCornelia Huck 			error = sysfs_create_link_nowarn(&dev->parent->kobj,
148636ce6dadSCornelia Huck 							 &dev->kobj,
148736ce6dadSCornelia Huck 							 new_class_name);
14882ee97cafSCornelia Huck 			if (error)
14892ee97cafSCornelia Huck 				goto out;
1490a2de48caSGreg Kroah-Hartman 			sysfs_remove_link(&dev->parent->kobj, old_class_name);
1491a2de48caSGreg Kroah-Hartman 		}
1492a2de48caSGreg Kroah-Hartman 	}
149360b8cabdSKay Sievers #else
1494a2de48caSGreg Kroah-Hartman 	if (dev->class) {
149536ce6dadSCornelia Huck 		error = sysfs_create_link_nowarn(&dev->class->p->class_subsys.kobj,
14961e0b2cf9SKay Sievers 						 &dev->kobj, dev_name(dev));
14970599ad53SStephen Hemminger 		if (error)
14980599ad53SStephen Hemminger 			goto out;
14991fbfee6cSGreg Kroah-Hartman 		sysfs_remove_link(&dev->class->p->class_subsys.kobj,
15007c71448bSGreg Kroah-Hartman 				  old_device_name);
15012ee97cafSCornelia Huck 	}
150260b8cabdSKay Sievers #endif
150360b8cabdSKay Sievers 
15042ee97cafSCornelia Huck out:
1505a2de48caSGreg Kroah-Hartman 	put_device(dev);
1506a2de48caSGreg Kroah-Hartman 
1507a2de48caSGreg Kroah-Hartman 	kfree(new_class_name);
1508952ab431SJesper Juhl 	kfree(old_class_name);
15092ee97cafSCornelia Huck 	kfree(old_device_name);
1510a2de48caSGreg Kroah-Hartman 
1511a2de48caSGreg Kroah-Hartman 	return error;
1512a2de48caSGreg Kroah-Hartman }
1513a2807dbcSJohannes Berg EXPORT_SYMBOL_GPL(device_rename);
15148a82472fSCornelia Huck 
15158a82472fSCornelia Huck static int device_move_class_links(struct device *dev,
15168a82472fSCornelia Huck 				   struct device *old_parent,
15178a82472fSCornelia Huck 				   struct device *new_parent)
15188a82472fSCornelia Huck {
1519f7f3461dSGreg Kroah-Hartman 	int error = 0;
15208a82472fSCornelia Huck #ifdef CONFIG_SYSFS_DEPRECATED
15218a82472fSCornelia Huck 	char *class_name;
15228a82472fSCornelia Huck 
15238a82472fSCornelia Huck 	class_name = make_class_name(dev->class->name, &dev->kobj);
15248a82472fSCornelia Huck 	if (!class_name) {
1525cb360bbfSCornelia Huck 		error = -ENOMEM;
15268a82472fSCornelia Huck 		goto out;
15278a82472fSCornelia Huck 	}
15288a82472fSCornelia Huck 	if (old_parent) {
15298a82472fSCornelia Huck 		sysfs_remove_link(&dev->kobj, "device");
15308a82472fSCornelia Huck 		sysfs_remove_link(&old_parent->kobj, class_name);
15318a82472fSCornelia Huck 	}
1532c744aeaeSCornelia Huck 	if (new_parent) {
1533c744aeaeSCornelia Huck 		error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
1534c744aeaeSCornelia Huck 					  "device");
15358a82472fSCornelia Huck 		if (error)
15368a82472fSCornelia Huck 			goto out;
1537c744aeaeSCornelia Huck 		error = sysfs_create_link(&new_parent->kobj, &dev->kobj,
1538c744aeaeSCornelia Huck 					  class_name);
15398a82472fSCornelia Huck 		if (error)
15408a82472fSCornelia Huck 			sysfs_remove_link(&dev->kobj, "device");
15414a3ad20cSGreg Kroah-Hartman 	} else
1542c744aeaeSCornelia Huck 		error = 0;
15438a82472fSCornelia Huck out:
15448a82472fSCornelia Huck 	kfree(class_name);
15458a82472fSCornelia Huck 	return error;
15468a82472fSCornelia Huck #else
1547f7f3461dSGreg Kroah-Hartman 	if (old_parent)
1548f7f3461dSGreg Kroah-Hartman 		sysfs_remove_link(&dev->kobj, "device");
1549f7f3461dSGreg Kroah-Hartman 	if (new_parent)
1550f7f3461dSGreg Kroah-Hartman 		error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
1551f7f3461dSGreg Kroah-Hartman 					  "device");
1552f7f3461dSGreg Kroah-Hartman 	return error;
15538a82472fSCornelia Huck #endif
15548a82472fSCornelia Huck }
15558a82472fSCornelia Huck 
15568a82472fSCornelia Huck /**
15578a82472fSCornelia Huck  * device_move - moves a device to a new parent
15588a82472fSCornelia Huck  * @dev: the pointer to the struct device to be moved
1559c744aeaeSCornelia Huck  * @new_parent: the new parent of the device (can by NULL)
15608a82472fSCornelia Huck  */
15618a82472fSCornelia Huck int device_move(struct device *dev, struct device *new_parent)
15628a82472fSCornelia Huck {
15638a82472fSCornelia Huck 	int error;
15648a82472fSCornelia Huck 	struct device *old_parent;
1565c744aeaeSCornelia Huck 	struct kobject *new_parent_kobj;
15668a82472fSCornelia Huck 
15678a82472fSCornelia Huck 	dev = get_device(dev);
15688a82472fSCornelia Huck 	if (!dev)
15698a82472fSCornelia Huck 		return -EINVAL;
15708a82472fSCornelia Huck 
15718a82472fSCornelia Huck 	new_parent = get_device(new_parent);
1572c744aeaeSCornelia Huck 	new_parent_kobj = get_device_parent(dev, new_parent);
157363b6971aSCornelia Huck 
15741e0b2cf9SKay Sievers 	pr_debug("device: '%s': %s: moving to '%s'\n", dev_name(dev),
15751e0b2cf9SKay Sievers 		 __func__, new_parent ? dev_name(new_parent) : "<NULL>");
1576c744aeaeSCornelia Huck 	error = kobject_move(&dev->kobj, new_parent_kobj);
15778a82472fSCornelia Huck 	if (error) {
157863b6971aSCornelia Huck 		cleanup_glue_dir(dev, new_parent_kobj);
15798a82472fSCornelia Huck 		put_device(new_parent);
15808a82472fSCornelia Huck 		goto out;
15818a82472fSCornelia Huck 	}
15828a82472fSCornelia Huck 	old_parent = dev->parent;
15838a82472fSCornelia Huck 	dev->parent = new_parent;
15848a82472fSCornelia Huck 	if (old_parent)
158511c3b5c3SGreg Kroah-Hartman 		klist_remove(&dev->p->knode_parent);
15860d358f22SYinghai Lu 	if (new_parent) {
158711c3b5c3SGreg Kroah-Hartman 		klist_add_tail(&dev->p->knode_parent,
158811c3b5c3SGreg Kroah-Hartman 			       &new_parent->p->klist_children);
15890d358f22SYinghai Lu 		set_dev_node(dev, dev_to_node(new_parent));
15900d358f22SYinghai Lu 	}
15910d358f22SYinghai Lu 
15928a82472fSCornelia Huck 	if (!dev->class)
15938a82472fSCornelia Huck 		goto out_put;
15948a82472fSCornelia Huck 	error = device_move_class_links(dev, old_parent, new_parent);
15958a82472fSCornelia Huck 	if (error) {
15968a82472fSCornelia Huck 		/* We ignore errors on cleanup since we're hosed anyway... */
15978a82472fSCornelia Huck 		device_move_class_links(dev, new_parent, old_parent);
15988a82472fSCornelia Huck 		if (!kobject_move(&dev->kobj, &old_parent->kobj)) {
1599c744aeaeSCornelia Huck 			if (new_parent)
160011c3b5c3SGreg Kroah-Hartman 				klist_remove(&dev->p->knode_parent);
16010d358f22SYinghai Lu 			dev->parent = old_parent;
16020d358f22SYinghai Lu 			if (old_parent) {
160311c3b5c3SGreg Kroah-Hartman 				klist_add_tail(&dev->p->knode_parent,
160411c3b5c3SGreg Kroah-Hartman 					       &old_parent->p->klist_children);
16050d358f22SYinghai Lu 				set_dev_node(dev, dev_to_node(old_parent));
16060d358f22SYinghai Lu 			}
16078a82472fSCornelia Huck 		}
160863b6971aSCornelia Huck 		cleanup_glue_dir(dev, new_parent_kobj);
16098a82472fSCornelia Huck 		put_device(new_parent);
16108a82472fSCornelia Huck 		goto out;
16118a82472fSCornelia Huck 	}
16128a82472fSCornelia Huck out_put:
16138a82472fSCornelia Huck 	put_device(old_parent);
16148a82472fSCornelia Huck out:
16158a82472fSCornelia Huck 	put_device(dev);
16168a82472fSCornelia Huck 	return error;
16178a82472fSCornelia Huck }
16188a82472fSCornelia Huck EXPORT_SYMBOL_GPL(device_move);
161937b0c020SGreg Kroah-Hartman 
162037b0c020SGreg Kroah-Hartman /**
162137b0c020SGreg Kroah-Hartman  * device_shutdown - call ->shutdown() on each device to shutdown.
162237b0c020SGreg Kroah-Hartman  */
162337b0c020SGreg Kroah-Hartman void device_shutdown(void)
162437b0c020SGreg Kroah-Hartman {
162537b0c020SGreg Kroah-Hartman 	struct device *dev, *devn;
162637b0c020SGreg Kroah-Hartman 
162737b0c020SGreg Kroah-Hartman 	list_for_each_entry_safe_reverse(dev, devn, &devices_kset->list,
162837b0c020SGreg Kroah-Hartman 				kobj.entry) {
162937b0c020SGreg Kroah-Hartman 		if (dev->bus && dev->bus->shutdown) {
163037b0c020SGreg Kroah-Hartman 			dev_dbg(dev, "shutdown\n");
163137b0c020SGreg Kroah-Hartman 			dev->bus->shutdown(dev);
163237b0c020SGreg Kroah-Hartman 		} else if (dev->driver && dev->driver->shutdown) {
163337b0c020SGreg Kroah-Hartman 			dev_dbg(dev, "shutdown\n");
163437b0c020SGreg Kroah-Hartman 			dev->driver->shutdown(dev);
163537b0c020SGreg Kroah-Hartman 		}
163637b0c020SGreg Kroah-Hartman 	}
1637e105b8bfSDan Williams 	kobject_put(sysfs_dev_char_kobj);
1638e105b8bfSDan Williams 	kobject_put(sysfs_dev_block_kobj);
1639e105b8bfSDan Williams 	kobject_put(dev_kobj);
164037b0c020SGreg Kroah-Hartman }
1641