xref: /openbmc/linux/drivers/base/core.c (revision f9cb074b)
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>
221da177e4SLinus Torvalds #include <asm/semaphore.h>
231da177e4SLinus Torvalds 
241da177e4SLinus Torvalds #include "base.h"
251da177e4SLinus Torvalds #include "power/power.h"
261da177e4SLinus Torvalds 
271da177e4SLinus Torvalds int (*platform_notify)(struct device * dev) = NULL;
281da177e4SLinus Torvalds int (*platform_notify_remove)(struct device * dev) = NULL;
291da177e4SLinus Torvalds 
301da177e4SLinus Torvalds /*
311da177e4SLinus Torvalds  * sysfs bindings for devices.
321da177e4SLinus Torvalds  */
331da177e4SLinus Torvalds 
343e95637aSAlan Stern /**
353e95637aSAlan Stern  * dev_driver_string - Return a device's driver name, if at all possible
363e95637aSAlan Stern  * @dev: struct device to get the name of
373e95637aSAlan Stern  *
383e95637aSAlan Stern  * Will return the device's driver's name if it is bound to a device.  If
393e95637aSAlan Stern  * the device is not bound to a device, it will return the name of the bus
403e95637aSAlan Stern  * it is attached to.  If it is not attached to a bus either, an empty
413e95637aSAlan Stern  * string will be returned.
423e95637aSAlan Stern  */
433e95637aSAlan Stern const char *dev_driver_string(struct device *dev)
443e95637aSAlan Stern {
453e95637aSAlan Stern 	return dev->driver ? dev->driver->name :
46a456b702SJean Delvare 			(dev->bus ? dev->bus->name :
47a456b702SJean Delvare 			(dev->class ? dev->class->name : ""));
483e95637aSAlan Stern }
49310a922dSMatthew Wilcox EXPORT_SYMBOL(dev_driver_string);
503e95637aSAlan Stern 
511da177e4SLinus Torvalds #define to_dev(obj) container_of(obj, struct device, kobj)
521da177e4SLinus Torvalds #define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr)
531da177e4SLinus Torvalds 
541da177e4SLinus Torvalds static ssize_t
551da177e4SLinus Torvalds dev_attr_show(struct kobject * kobj, struct attribute * attr, char * buf)
561da177e4SLinus Torvalds {
571da177e4SLinus Torvalds 	struct device_attribute * dev_attr = to_dev_attr(attr);
581da177e4SLinus Torvalds 	struct device * dev = to_dev(kobj);
594a0c20bfSDmitry Torokhov 	ssize_t ret = -EIO;
601da177e4SLinus Torvalds 
611da177e4SLinus Torvalds 	if (dev_attr->show)
6254b6f35cSYani Ioannou 		ret = dev_attr->show(dev, dev_attr, buf);
631da177e4SLinus Torvalds 	return ret;
641da177e4SLinus Torvalds }
651da177e4SLinus Torvalds 
661da177e4SLinus Torvalds static ssize_t
671da177e4SLinus Torvalds dev_attr_store(struct kobject * kobj, struct attribute * attr,
681da177e4SLinus Torvalds 	       const char * buf, size_t count)
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->store)
7554b6f35cSYani Ioannou 		ret = dev_attr->store(dev, dev_attr, buf, count);
761da177e4SLinus Torvalds 	return ret;
771da177e4SLinus Torvalds }
781da177e4SLinus Torvalds 
791da177e4SLinus Torvalds static struct sysfs_ops dev_sysfs_ops = {
801da177e4SLinus Torvalds 	.show	= dev_attr_show,
811da177e4SLinus Torvalds 	.store	= dev_attr_store,
821da177e4SLinus Torvalds };
831da177e4SLinus Torvalds 
841da177e4SLinus Torvalds 
851da177e4SLinus Torvalds /**
861da177e4SLinus Torvalds  *	device_release - free device structure.
871da177e4SLinus Torvalds  *	@kobj:	device's kobject.
881da177e4SLinus Torvalds  *
891da177e4SLinus Torvalds  *	This is called once the reference count for the object
901da177e4SLinus Torvalds  *	reaches 0. We forward the call to the device's release
911da177e4SLinus Torvalds  *	method, which should handle actually freeing the structure.
921da177e4SLinus Torvalds  */
931da177e4SLinus Torvalds static void device_release(struct kobject * kobj)
941da177e4SLinus Torvalds {
951da177e4SLinus Torvalds 	struct device * dev = to_dev(kobj);
961da177e4SLinus Torvalds 
971da177e4SLinus Torvalds 	if (dev->release)
981da177e4SLinus Torvalds 		dev->release(dev);
99f9f852dfSKay Sievers 	else if (dev->type && dev->type->release)
100f9f852dfSKay Sievers 		dev->type->release(dev);
1012620efefSGreg Kroah-Hartman 	else if (dev->class && dev->class->dev_release)
1022620efefSGreg Kroah-Hartman 		dev->class->dev_release(dev);
1031da177e4SLinus Torvalds 	else {
1041da177e4SLinus Torvalds 		printk(KERN_ERR "Device '%s' does not have a release() function, "
1051da177e4SLinus Torvalds 			"it is broken and must be fixed.\n",
1061da177e4SLinus Torvalds 			dev->bus_id);
1071da177e4SLinus Torvalds 		WARN_ON(1);
1081da177e4SLinus Torvalds 	}
1091da177e4SLinus Torvalds }
1101da177e4SLinus Torvalds 
1118f4afc41SGreg Kroah-Hartman static struct kobj_type device_ktype = {
1121da177e4SLinus Torvalds 	.release	= device_release,
1131da177e4SLinus Torvalds 	.sysfs_ops	= &dev_sysfs_ops,
1141da177e4SLinus Torvalds };
1151da177e4SLinus Torvalds 
1161da177e4SLinus Torvalds 
117312c004dSKay Sievers static int dev_uevent_filter(struct kset *kset, struct kobject *kobj)
1181da177e4SLinus Torvalds {
1191da177e4SLinus Torvalds 	struct kobj_type *ktype = get_ktype(kobj);
1201da177e4SLinus Torvalds 
1218f4afc41SGreg Kroah-Hartman 	if (ktype == &device_ktype) {
1221da177e4SLinus Torvalds 		struct device *dev = to_dev(kobj);
12383b5fb4cSCornelia Huck 		if (dev->uevent_suppress)
12483b5fb4cSCornelia Huck 			return 0;
1251da177e4SLinus Torvalds 		if (dev->bus)
1261da177e4SLinus Torvalds 			return 1;
12723681e47SGreg Kroah-Hartman 		if (dev->class)
12823681e47SGreg Kroah-Hartman 			return 1;
1291da177e4SLinus Torvalds 	}
1301da177e4SLinus Torvalds 	return 0;
1311da177e4SLinus Torvalds }
1321da177e4SLinus Torvalds 
133312c004dSKay Sievers static const char *dev_uevent_name(struct kset *kset, struct kobject *kobj)
1341da177e4SLinus Torvalds {
1351da177e4SLinus Torvalds 	struct device *dev = to_dev(kobj);
1361da177e4SLinus Torvalds 
13723681e47SGreg Kroah-Hartman 	if (dev->bus)
1381da177e4SLinus Torvalds 		return dev->bus->name;
13923681e47SGreg Kroah-Hartman 	if (dev->class)
14023681e47SGreg Kroah-Hartman 		return dev->class->name;
14123681e47SGreg Kroah-Hartman 	return NULL;
1421da177e4SLinus Torvalds }
1431da177e4SLinus Torvalds 
1447eff2e7aSKay Sievers static int dev_uevent(struct kset *kset, struct kobject *kobj,
1457eff2e7aSKay Sievers 		      struct kobj_uevent_env *env)
1461da177e4SLinus Torvalds {
1471da177e4SLinus Torvalds 	struct device *dev = to_dev(kobj);
1481da177e4SLinus Torvalds 	int retval = 0;
1491da177e4SLinus Torvalds 
15023681e47SGreg Kroah-Hartman 	/* add the major/minor if present */
15123681e47SGreg Kroah-Hartman 	if (MAJOR(dev->devt)) {
1527eff2e7aSKay Sievers 		add_uevent_var(env, "MAJOR=%u", MAJOR(dev->devt));
1537eff2e7aSKay Sievers 		add_uevent_var(env, "MINOR=%u", MINOR(dev->devt));
15423681e47SGreg Kroah-Hartman 	}
15523681e47SGreg Kroah-Hartman 
156414264f9SKay Sievers 	if (dev->type && dev->type->name)
1577eff2e7aSKay Sievers 		add_uevent_var(env, "DEVTYPE=%s", dev->type->name);
158414264f9SKay Sievers 
159239378f1SKay Sievers 	if (dev->driver)
1607eff2e7aSKay Sievers 		add_uevent_var(env, "DRIVER=%s", dev->driver->name);
161239378f1SKay Sievers 
162a87cb2acSKay Sievers #ifdef CONFIG_SYSFS_DEPRECATED
163239378f1SKay Sievers 	if (dev->class) {
164239378f1SKay Sievers 		struct device *parent = dev->parent;
165239378f1SKay Sievers 
166239378f1SKay Sievers 		/* find first bus device in parent chain */
167239378f1SKay Sievers 		while (parent && !parent->bus)
168239378f1SKay Sievers 			parent = parent->parent;
169239378f1SKay Sievers 		if (parent && parent->bus) {
170239378f1SKay Sievers 			const char *path;
171239378f1SKay Sievers 
172239378f1SKay Sievers 			path = kobject_get_path(&parent->kobj, GFP_KERNEL);
1732c7afd12SKay Sievers 			if (path) {
1747eff2e7aSKay Sievers 				add_uevent_var(env, "PHYSDEVPATH=%s", path);
175239378f1SKay Sievers 				kfree(path);
1762c7afd12SKay Sievers 			}
177239378f1SKay Sievers 
1787eff2e7aSKay Sievers 			add_uevent_var(env, "PHYSDEVBUS=%s", parent->bus->name);
179239378f1SKay Sievers 
180239378f1SKay Sievers 			if (parent->driver)
1817eff2e7aSKay Sievers 				add_uevent_var(env, "PHYSDEVDRIVER=%s",
1827eff2e7aSKay Sievers 					       parent->driver->name);
183239378f1SKay Sievers 		}
184239378f1SKay Sievers 	} else if (dev->bus) {
1857eff2e7aSKay Sievers 		add_uevent_var(env, "PHYSDEVBUS=%s", dev->bus->name);
186239378f1SKay Sievers 
187239378f1SKay Sievers 		if (dev->driver)
1887eff2e7aSKay Sievers 			add_uevent_var(env, "PHYSDEVDRIVER=%s", dev->driver->name);
189d81d9d6bSKay Sievers 	}
190239378f1SKay Sievers #endif
1911da177e4SLinus Torvalds 
1921da177e4SLinus Torvalds 	/* have the bus specific function add its stuff */
1937eff2e7aSKay Sievers 	if (dev->bus && dev->bus->uevent) {
1947eff2e7aSKay Sievers 		retval = dev->bus->uevent(dev, env);
195f9f852dfSKay Sievers 		if (retval)
1967dc72b28SGreg Kroah-Hartman 			pr_debug("device: '%s': %s: bus uevent() returned %d\n",
1977dc72b28SGreg Kroah-Hartman 				 dev->bus_id, __FUNCTION__, retval);
1981da177e4SLinus Torvalds 	}
1991da177e4SLinus Torvalds 
2002620efefSGreg Kroah-Hartman 	/* have the class specific function add its stuff */
2017eff2e7aSKay Sievers 	if (dev->class && dev->class->dev_uevent) {
2027eff2e7aSKay Sievers 		retval = dev->class->dev_uevent(dev, env);
203f9f852dfSKay Sievers 		if (retval)
2047dc72b28SGreg Kroah-Hartman 			pr_debug("device: '%s': %s: class uevent() "
2057dc72b28SGreg Kroah-Hartman 				 "returned %d\n", dev->bus_id,
2062620efefSGreg Kroah-Hartman 				 __FUNCTION__, retval);
2072620efefSGreg Kroah-Hartman 	}
208f9f852dfSKay Sievers 
209f9f852dfSKay Sievers 	/* have the device type specific fuction add its stuff */
2107eff2e7aSKay Sievers 	if (dev->type && dev->type->uevent) {
2117eff2e7aSKay Sievers 		retval = dev->type->uevent(dev, env);
212f9f852dfSKay Sievers 		if (retval)
2137dc72b28SGreg Kroah-Hartman 			pr_debug("device: '%s': %s: dev_type uevent() "
2147dc72b28SGreg Kroah-Hartman 				 "returned %d\n", dev->bus_id,
215f9f852dfSKay Sievers 				 __FUNCTION__, retval);
2162620efefSGreg Kroah-Hartman 	}
2172620efefSGreg Kroah-Hartman 
2181da177e4SLinus Torvalds 	return retval;
2191da177e4SLinus Torvalds }
2201da177e4SLinus Torvalds 
221312c004dSKay Sievers static struct kset_uevent_ops device_uevent_ops = {
222312c004dSKay Sievers 	.filter =	dev_uevent_filter,
223312c004dSKay Sievers 	.name =		dev_uevent_name,
224312c004dSKay Sievers 	.uevent =	dev_uevent,
2251da177e4SLinus Torvalds };
2261da177e4SLinus Torvalds 
22716574dccSKay Sievers static ssize_t show_uevent(struct device *dev, struct device_attribute *attr,
22816574dccSKay Sievers 			   char *buf)
22916574dccSKay Sievers {
23016574dccSKay Sievers 	struct kobject *top_kobj;
23116574dccSKay Sievers 	struct kset *kset;
2327eff2e7aSKay Sievers 	struct kobj_uevent_env *env = NULL;
23316574dccSKay Sievers 	int i;
23416574dccSKay Sievers 	size_t count = 0;
23516574dccSKay Sievers 	int retval;
23616574dccSKay Sievers 
23716574dccSKay Sievers 	/* search the kset, the device belongs to */
23816574dccSKay Sievers 	top_kobj = &dev->kobj;
2395c5daf65SKay Sievers 	while (!top_kobj->kset && top_kobj->parent)
24016574dccSKay Sievers 		top_kobj = top_kobj->parent;
24116574dccSKay Sievers 	if (!top_kobj->kset)
24216574dccSKay Sievers 		goto out;
2435c5daf65SKay Sievers 
24416574dccSKay Sievers 	kset = top_kobj->kset;
24516574dccSKay Sievers 	if (!kset->uevent_ops || !kset->uevent_ops->uevent)
24616574dccSKay Sievers 		goto out;
24716574dccSKay Sievers 
24816574dccSKay Sievers 	/* respect filter */
24916574dccSKay Sievers 	if (kset->uevent_ops && kset->uevent_ops->filter)
25016574dccSKay Sievers 		if (!kset->uevent_ops->filter(kset, &dev->kobj))
25116574dccSKay Sievers 			goto out;
25216574dccSKay Sievers 
2537eff2e7aSKay Sievers 	env = kzalloc(sizeof(struct kobj_uevent_env), GFP_KERNEL);
2547eff2e7aSKay Sievers 	if (!env)
255c7308c81SGreg Kroah-Hartman 		return -ENOMEM;
256c7308c81SGreg Kroah-Hartman 
25716574dccSKay Sievers 	/* let the kset specific function add its keys */
2587eff2e7aSKay Sievers 	retval = kset->uevent_ops->uevent(kset, &dev->kobj, env);
25916574dccSKay Sievers 	if (retval)
26016574dccSKay Sievers 		goto out;
26116574dccSKay Sievers 
26216574dccSKay Sievers 	/* copy keys to file */
2637eff2e7aSKay Sievers 	for (i = 0; i < env->envp_idx; i++)
2647eff2e7aSKay Sievers 		count += sprintf(&buf[count], "%s\n", env->envp[i]);
26516574dccSKay Sievers out:
2667eff2e7aSKay Sievers 	kfree(env);
26716574dccSKay Sievers 	return count;
26816574dccSKay Sievers }
26916574dccSKay Sievers 
270a7fd6706SKay Sievers static ssize_t store_uevent(struct device *dev, struct device_attribute *attr,
271a7fd6706SKay Sievers 			    const char *buf, size_t count)
272a7fd6706SKay Sievers {
27360a96a59SKay Sievers 	enum kobject_action action;
27460a96a59SKay Sievers 
2755c5daf65SKay Sievers 	if (kobject_action_type(buf, count, &action) == 0) {
27660a96a59SKay Sievers 		kobject_uevent(&dev->kobj, action);
27760a96a59SKay Sievers 		goto out;
27860a96a59SKay Sievers 	}
27960a96a59SKay Sievers 
28022af74f3SKay Sievers 	dev_err(dev, "uevent: unsupported action-string; this will "
28160a96a59SKay Sievers 		     "be ignored in a future kernel version\n");
282312c004dSKay Sievers 	kobject_uevent(&dev->kobj, KOBJ_ADD);
28360a96a59SKay Sievers out:
284a7fd6706SKay Sievers 	return count;
285a7fd6706SKay Sievers }
286a7fd6706SKay Sievers 
287ad6a1e1cSTejun Heo static struct device_attribute uevent_attr =
288ad6a1e1cSTejun Heo 	__ATTR(uevent, S_IRUGO | S_IWUSR, show_uevent, store_uevent);
289ad6a1e1cSTejun Heo 
290621a1672SDmitry Torokhov static int device_add_attributes(struct device *dev,
291621a1672SDmitry Torokhov 				 struct device_attribute *attrs)
292de0ff00dSGreg Kroah-Hartman {
293de0ff00dSGreg Kroah-Hartman 	int error = 0;
294621a1672SDmitry Torokhov 	int i;
295de0ff00dSGreg Kroah-Hartman 
296621a1672SDmitry Torokhov 	if (attrs) {
297621a1672SDmitry Torokhov 		for (i = 0; attr_name(attrs[i]); i++) {
298621a1672SDmitry Torokhov 			error = device_create_file(dev, &attrs[i]);
299621a1672SDmitry Torokhov 			if (error)
300621a1672SDmitry Torokhov 				break;
301621a1672SDmitry Torokhov 		}
302621a1672SDmitry Torokhov 		if (error)
303de0ff00dSGreg Kroah-Hartman 			while (--i >= 0)
304621a1672SDmitry Torokhov 				device_remove_file(dev, &attrs[i]);
305de0ff00dSGreg Kroah-Hartman 	}
306de0ff00dSGreg Kroah-Hartman 	return error;
307de0ff00dSGreg Kroah-Hartman }
308de0ff00dSGreg Kroah-Hartman 
309621a1672SDmitry Torokhov static void device_remove_attributes(struct device *dev,
310621a1672SDmitry Torokhov 				     struct device_attribute *attrs)
311de0ff00dSGreg Kroah-Hartman {
312de0ff00dSGreg Kroah-Hartman 	int i;
313621a1672SDmitry Torokhov 
314621a1672SDmitry Torokhov 	if (attrs)
315621a1672SDmitry Torokhov 		for (i = 0; attr_name(attrs[i]); i++)
316621a1672SDmitry Torokhov 			device_remove_file(dev, &attrs[i]);
317621a1672SDmitry Torokhov }
318621a1672SDmitry Torokhov 
319621a1672SDmitry Torokhov static int device_add_groups(struct device *dev,
320621a1672SDmitry Torokhov 			     struct attribute_group **groups)
321621a1672SDmitry Torokhov {
322621a1672SDmitry Torokhov 	int error = 0;
323621a1672SDmitry Torokhov 	int i;
324621a1672SDmitry Torokhov 
325621a1672SDmitry Torokhov 	if (groups) {
326621a1672SDmitry Torokhov 		for (i = 0; groups[i]; i++) {
327621a1672SDmitry Torokhov 			error = sysfs_create_group(&dev->kobj, groups[i]);
328621a1672SDmitry Torokhov 			if (error) {
329621a1672SDmitry Torokhov 				while (--i >= 0)
330621a1672SDmitry Torokhov 					sysfs_remove_group(&dev->kobj, groups[i]);
331621a1672SDmitry Torokhov 				break;
332de0ff00dSGreg Kroah-Hartman 			}
333de0ff00dSGreg Kroah-Hartman 		}
334de0ff00dSGreg Kroah-Hartman 	}
335621a1672SDmitry Torokhov 	return error;
336621a1672SDmitry Torokhov }
337621a1672SDmitry Torokhov 
338621a1672SDmitry Torokhov static void device_remove_groups(struct device *dev,
339621a1672SDmitry Torokhov 				 struct attribute_group **groups)
340621a1672SDmitry Torokhov {
341621a1672SDmitry Torokhov 	int i;
342621a1672SDmitry Torokhov 
343621a1672SDmitry Torokhov 	if (groups)
344621a1672SDmitry Torokhov 		for (i = 0; groups[i]; i++)
345621a1672SDmitry Torokhov 			sysfs_remove_group(&dev->kobj, groups[i]);
346621a1672SDmitry Torokhov }
347de0ff00dSGreg Kroah-Hartman 
3482620efefSGreg Kroah-Hartman static int device_add_attrs(struct device *dev)
3492620efefSGreg Kroah-Hartman {
3502620efefSGreg Kroah-Hartman 	struct class *class = dev->class;
351f9f852dfSKay Sievers 	struct device_type *type = dev->type;
352621a1672SDmitry Torokhov 	int error;
3532620efefSGreg Kroah-Hartman 
354621a1672SDmitry Torokhov 	if (class) {
355621a1672SDmitry Torokhov 		error = device_add_attributes(dev, class->dev_attrs);
3562620efefSGreg Kroah-Hartman 		if (error)
357621a1672SDmitry Torokhov 			return error;
358f9f852dfSKay Sievers 	}
359f9f852dfSKay Sievers 
360621a1672SDmitry Torokhov 	if (type) {
361621a1672SDmitry Torokhov 		error = device_add_groups(dev, type->groups);
362f9f852dfSKay Sievers 		if (error)
363621a1672SDmitry Torokhov 			goto err_remove_class_attrs;
364f9f852dfSKay Sievers 	}
365621a1672SDmitry Torokhov 
366621a1672SDmitry Torokhov 	error = device_add_groups(dev, dev->groups);
367f9f852dfSKay Sievers 	if (error)
368621a1672SDmitry Torokhov 		goto err_remove_type_groups;
369621a1672SDmitry Torokhov 
370621a1672SDmitry Torokhov 	return 0;
371621a1672SDmitry Torokhov 
372621a1672SDmitry Torokhov  err_remove_type_groups:
373621a1672SDmitry Torokhov 	if (type)
374621a1672SDmitry Torokhov 		device_remove_groups(dev, type->groups);
375621a1672SDmitry Torokhov  err_remove_class_attrs:
376621a1672SDmitry Torokhov 	if (class)
377621a1672SDmitry Torokhov 		device_remove_attributes(dev, class->dev_attrs);
378f9f852dfSKay Sievers 
3792620efefSGreg Kroah-Hartman 	return error;
3802620efefSGreg Kroah-Hartman }
3812620efefSGreg Kroah-Hartman 
3822620efefSGreg Kroah-Hartman static void device_remove_attrs(struct device *dev)
3832620efefSGreg Kroah-Hartman {
3842620efefSGreg Kroah-Hartman 	struct class *class = dev->class;
385f9f852dfSKay Sievers 	struct device_type *type = dev->type;
3862620efefSGreg Kroah-Hartman 
387621a1672SDmitry Torokhov 	device_remove_groups(dev, dev->groups);
388f9f852dfSKay Sievers 
389621a1672SDmitry Torokhov 	if (type)
390621a1672SDmitry Torokhov 		device_remove_groups(dev, type->groups);
391621a1672SDmitry Torokhov 
392621a1672SDmitry Torokhov 	if (class)
393621a1672SDmitry Torokhov 		device_remove_attributes(dev, class->dev_attrs);
3942620efefSGreg Kroah-Hartman }
3952620efefSGreg Kroah-Hartman 
3962620efefSGreg Kroah-Hartman 
39723681e47SGreg Kroah-Hartman static ssize_t show_dev(struct device *dev, struct device_attribute *attr,
39823681e47SGreg Kroah-Hartman 			char *buf)
39923681e47SGreg Kroah-Hartman {
40023681e47SGreg Kroah-Hartman 	return print_dev_t(buf, dev->devt);
40123681e47SGreg Kroah-Hartman }
40223681e47SGreg Kroah-Hartman 
403ad6a1e1cSTejun Heo static struct device_attribute devt_attr =
404ad6a1e1cSTejun Heo 	__ATTR(dev, S_IRUGO, show_dev, NULL);
405ad6a1e1cSTejun Heo 
406881c6cfdSGreg Kroah-Hartman /* kset to create /sys/devices/  */
407881c6cfdSGreg Kroah-Hartman struct kset *devices_kset;
4081da177e4SLinus Torvalds 
4091da177e4SLinus Torvalds 
4101da177e4SLinus Torvalds /**
4111da177e4SLinus Torvalds  *	device_create_file - create sysfs attribute file for device.
4121da177e4SLinus Torvalds  *	@dev:	device.
4131da177e4SLinus Torvalds  *	@attr:	device attribute descriptor.
4141da177e4SLinus Torvalds  */
4151da177e4SLinus Torvalds 
4161da177e4SLinus Torvalds int device_create_file(struct device * dev, struct device_attribute * attr)
4171da177e4SLinus Torvalds {
4181da177e4SLinus Torvalds 	int error = 0;
4191da177e4SLinus Torvalds 	if (get_device(dev)) {
4201da177e4SLinus Torvalds 		error = sysfs_create_file(&dev->kobj, &attr->attr);
4211da177e4SLinus Torvalds 		put_device(dev);
4221da177e4SLinus Torvalds 	}
4231da177e4SLinus Torvalds 	return error;
4241da177e4SLinus Torvalds }
4251da177e4SLinus Torvalds 
4261da177e4SLinus Torvalds /**
4271da177e4SLinus Torvalds  *	device_remove_file - remove sysfs attribute file.
4281da177e4SLinus Torvalds  *	@dev:	device.
4291da177e4SLinus Torvalds  *	@attr:	device attribute descriptor.
4301da177e4SLinus Torvalds  */
4311da177e4SLinus Torvalds 
4321da177e4SLinus Torvalds void device_remove_file(struct device * dev, struct device_attribute * attr)
4331da177e4SLinus Torvalds {
4341da177e4SLinus Torvalds 	if (get_device(dev)) {
4351da177e4SLinus Torvalds 		sysfs_remove_file(&dev->kobj, &attr->attr);
4361da177e4SLinus Torvalds 		put_device(dev);
4371da177e4SLinus Torvalds 	}
4381da177e4SLinus Torvalds }
4391da177e4SLinus Torvalds 
4402589f188SGreg Kroah-Hartman /**
4412589f188SGreg Kroah-Hartman  * device_create_bin_file - create sysfs binary attribute file for device.
4422589f188SGreg Kroah-Hartman  * @dev: device.
4432589f188SGreg Kroah-Hartman  * @attr: device binary attribute descriptor.
4442589f188SGreg Kroah-Hartman  */
4452589f188SGreg Kroah-Hartman int device_create_bin_file(struct device *dev, struct bin_attribute *attr)
4462589f188SGreg Kroah-Hartman {
4472589f188SGreg Kroah-Hartman 	int error = -EINVAL;
4482589f188SGreg Kroah-Hartman 	if (dev)
4492589f188SGreg Kroah-Hartman 		error = sysfs_create_bin_file(&dev->kobj, attr);
4502589f188SGreg Kroah-Hartman 	return error;
4512589f188SGreg Kroah-Hartman }
4522589f188SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(device_create_bin_file);
4532589f188SGreg Kroah-Hartman 
4542589f188SGreg Kroah-Hartman /**
4552589f188SGreg Kroah-Hartman  * device_remove_bin_file - remove sysfs binary attribute file
4562589f188SGreg Kroah-Hartman  * @dev: device.
4572589f188SGreg Kroah-Hartman  * @attr: device binary attribute descriptor.
4582589f188SGreg Kroah-Hartman  */
4592589f188SGreg Kroah-Hartman void device_remove_bin_file(struct device *dev, struct bin_attribute *attr)
4602589f188SGreg Kroah-Hartman {
4612589f188SGreg Kroah-Hartman 	if (dev)
4622589f188SGreg Kroah-Hartman 		sysfs_remove_bin_file(&dev->kobj, attr);
4632589f188SGreg Kroah-Hartman }
4642589f188SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(device_remove_bin_file);
4652589f188SGreg Kroah-Hartman 
466d9a9cdfbSAlan Stern /**
467523ded71SAlan Stern  * device_schedule_callback_owner - helper to schedule a callback for a device
468d9a9cdfbSAlan Stern  * @dev: device.
469d9a9cdfbSAlan Stern  * @func: callback function to invoke later.
470523ded71SAlan Stern  * @owner: module owning the callback routine
471d9a9cdfbSAlan Stern  *
472d9a9cdfbSAlan Stern  * Attribute methods must not unregister themselves or their parent device
473d9a9cdfbSAlan Stern  * (which would amount to the same thing).  Attempts to do so will deadlock,
474d9a9cdfbSAlan Stern  * since unregistration is mutually exclusive with driver callbacks.
475d9a9cdfbSAlan Stern  *
476d9a9cdfbSAlan Stern  * Instead methods can call this routine, which will attempt to allocate
477d9a9cdfbSAlan Stern  * and schedule a workqueue request to call back @func with @dev as its
478d9a9cdfbSAlan Stern  * argument in the workqueue's process context.  @dev will be pinned until
479d9a9cdfbSAlan Stern  * @func returns.
480d9a9cdfbSAlan Stern  *
481523ded71SAlan Stern  * This routine is usually called via the inline device_schedule_callback(),
482523ded71SAlan Stern  * which automatically sets @owner to THIS_MODULE.
483523ded71SAlan Stern  *
484d9a9cdfbSAlan Stern  * Returns 0 if the request was submitted, -ENOMEM if storage could not
485523ded71SAlan Stern  * be allocated, -ENODEV if a reference to @owner isn't available.
486d9a9cdfbSAlan Stern  *
487d9a9cdfbSAlan Stern  * NOTE: This routine won't work if CONFIG_SYSFS isn't set!  It uses an
488d9a9cdfbSAlan Stern  * underlying sysfs routine (since it is intended for use by attribute
489d9a9cdfbSAlan Stern  * methods), and if sysfs isn't available you'll get nothing but -ENOSYS.
490d9a9cdfbSAlan Stern  */
491523ded71SAlan Stern int device_schedule_callback_owner(struct device *dev,
492523ded71SAlan Stern 		void (*func)(struct device *), struct module *owner)
493d9a9cdfbSAlan Stern {
494d9a9cdfbSAlan Stern 	return sysfs_schedule_callback(&dev->kobj,
495523ded71SAlan Stern 			(void (*)(void *)) func, dev, owner);
496d9a9cdfbSAlan Stern }
497523ded71SAlan Stern EXPORT_SYMBOL_GPL(device_schedule_callback_owner);
498d9a9cdfbSAlan Stern 
49934bb61f9SJames Bottomley static void klist_children_get(struct klist_node *n)
50034bb61f9SJames Bottomley {
50134bb61f9SJames Bottomley 	struct device *dev = container_of(n, struct device, knode_parent);
50234bb61f9SJames Bottomley 
50334bb61f9SJames Bottomley 	get_device(dev);
50434bb61f9SJames Bottomley }
50534bb61f9SJames Bottomley 
50634bb61f9SJames Bottomley static void klist_children_put(struct klist_node *n)
50734bb61f9SJames Bottomley {
50834bb61f9SJames Bottomley 	struct device *dev = container_of(n, struct device, knode_parent);
50934bb61f9SJames Bottomley 
51034bb61f9SJames Bottomley 	put_device(dev);
51134bb61f9SJames Bottomley }
51234bb61f9SJames Bottomley 
5131da177e4SLinus Torvalds 
5141da177e4SLinus Torvalds /**
5151da177e4SLinus Torvalds  *	device_initialize - init device structure.
5161da177e4SLinus Torvalds  *	@dev:	device.
5171da177e4SLinus Torvalds  *
5181da177e4SLinus Torvalds  *	This prepares the device for use by other layers,
5191da177e4SLinus Torvalds  *	including adding it to the device hierarchy.
5201da177e4SLinus Torvalds  *	It is the first half of device_register(), if called by
5211da177e4SLinus Torvalds  *	that, though it can also be called separately, so one
5221da177e4SLinus Torvalds  *	may use @dev's fields (e.g. the refcount).
5231da177e4SLinus Torvalds  */
5241da177e4SLinus Torvalds 
5251da177e4SLinus Torvalds void device_initialize(struct device *dev)
5261da177e4SLinus Torvalds {
527881c6cfdSGreg Kroah-Hartman 	dev->kobj.kset = devices_kset;
528f9cb074bSGreg Kroah-Hartman 	kobject_init(&dev->kobj, &device_ktype);
52934bb61f9SJames Bottomley 	klist_init(&dev->klist_children, klist_children_get,
53034bb61f9SJames Bottomley 		   klist_children_put);
5311da177e4SLinus Torvalds 	INIT_LIST_HEAD(&dev->dma_pools);
53223681e47SGreg Kroah-Hartman 	INIT_LIST_HEAD(&dev->node);
533af70316aSmochel@digitalimplant.org 	init_MUTEX(&dev->sem);
5349ac7849eSTejun Heo 	spin_lock_init(&dev->devres_lock);
5359ac7849eSTejun Heo 	INIT_LIST_HEAD(&dev->devres_head);
5360ac85241SDavid Brownell 	device_init_wakeup(dev, 0);
53787348136SChristoph Hellwig 	set_dev_node(dev, -1);
5381da177e4SLinus Torvalds }
5391da177e4SLinus Torvalds 
54040fa5422SGreg Kroah-Hartman #ifdef CONFIG_SYSFS_DEPRECATED
541c744aeaeSCornelia Huck static struct kobject *get_device_parent(struct device *dev,
542c744aeaeSCornelia Huck 					 struct device *parent)
54340fa5422SGreg Kroah-Hartman {
544da231fd5SKay Sievers 	/* class devices without a parent live in /sys/class/<classname>/ */
5453eb215deSDmitry Torokhov 	if (dev->class && (!parent || parent->class != dev->class))
546823bccfcSGreg Kroah-Hartman 		return &dev->class->subsys.kobj;
547da231fd5SKay Sievers 	/* all other devices keep their parent */
54840fa5422SGreg Kroah-Hartman 	else if (parent)
549c744aeaeSCornelia Huck 		return &parent->kobj;
55040fa5422SGreg Kroah-Hartman 
551c744aeaeSCornelia Huck 	return NULL;
55240fa5422SGreg Kroah-Hartman }
553da231fd5SKay Sievers 
554da231fd5SKay Sievers static inline void cleanup_device_parent(struct device *dev) {}
55540fa5422SGreg Kroah-Hartman #else
556c744aeaeSCornelia Huck static struct kobject *virtual_device_parent(struct device *dev)
557f0ee61a6SGreg Kroah-Hartman {
558f0ee61a6SGreg Kroah-Hartman 	static struct kobject *virtual_dir = NULL;
559f0ee61a6SGreg Kroah-Hartman 
560f0ee61a6SGreg Kroah-Hartman 	if (!virtual_dir)
5614ff6abffSGreg Kroah-Hartman 		virtual_dir = kobject_create_and_add("virtual",
562881c6cfdSGreg Kroah-Hartman 						     &devices_kset->kobj);
563f0ee61a6SGreg Kroah-Hartman 
56486406245SKay Sievers 	return virtual_dir;
565f0ee61a6SGreg Kroah-Hartman }
566f0ee61a6SGreg Kroah-Hartman 
567c744aeaeSCornelia Huck static struct kobject *get_device_parent(struct device *dev,
568c744aeaeSCornelia Huck 					 struct device *parent)
56940fa5422SGreg Kroah-Hartman {
57043968d2fSGreg Kroah-Hartman 	int retval;
57143968d2fSGreg Kroah-Hartman 
57286406245SKay Sievers 	if (dev->class) {
57386406245SKay Sievers 		struct kobject *kobj = NULL;
57486406245SKay Sievers 		struct kobject *parent_kobj;
57586406245SKay Sievers 		struct kobject *k;
57686406245SKay Sievers 
57786406245SKay Sievers 		/*
57886406245SKay Sievers 		 * If we have no parent, we live in "virtual".
57986406245SKay Sievers 		 * Class-devices with a bus-device as parent, live
58086406245SKay Sievers 		 * in a class-directory to prevent namespace collisions.
58186406245SKay Sievers 		 */
58286406245SKay Sievers 		if (parent == NULL)
58386406245SKay Sievers 			parent_kobj = virtual_device_parent(dev);
58486406245SKay Sievers 		else if (parent->class)
58586406245SKay Sievers 			return &parent->kobj;
58686406245SKay Sievers 		else
58786406245SKay Sievers 			parent_kobj = &parent->kobj;
58886406245SKay Sievers 
58986406245SKay Sievers 		/* find our class-directory at the parent and reference it */
59086406245SKay Sievers 		spin_lock(&dev->class->class_dirs.list_lock);
59186406245SKay Sievers 		list_for_each_entry(k, &dev->class->class_dirs.list, entry)
59286406245SKay Sievers 			if (k->parent == parent_kobj) {
59386406245SKay Sievers 				kobj = kobject_get(k);
59486406245SKay Sievers 				break;
59586406245SKay Sievers 			}
59686406245SKay Sievers 		spin_unlock(&dev->class->class_dirs.list_lock);
59786406245SKay Sievers 		if (kobj)
59886406245SKay Sievers 			return kobj;
59986406245SKay Sievers 
60086406245SKay Sievers 		/* or create a new class-directory at the parent device */
60143968d2fSGreg Kroah-Hartman 		k = kobject_create();
60243968d2fSGreg Kroah-Hartman 		if (!k)
60343968d2fSGreg Kroah-Hartman 			return NULL;
60443968d2fSGreg Kroah-Hartman 		k->kset = &dev->class->class_dirs;
605b2d6db58SGreg Kroah-Hartman 		retval = kobject_add(k, parent_kobj, "%s", dev->class->name);
60643968d2fSGreg Kroah-Hartman 		if (retval < 0) {
60743968d2fSGreg Kroah-Hartman 			kobject_put(k);
60843968d2fSGreg Kroah-Hartman 			return NULL;
60943968d2fSGreg Kroah-Hartman 		}
61043968d2fSGreg Kroah-Hartman 		/* Do not emit a uevent, as it's not needed for this
61143968d2fSGreg Kroah-Hartman 		 * "class glue" directory. */
61243968d2fSGreg Kroah-Hartman 		return k;
61386406245SKay Sievers 	}
61486406245SKay Sievers 
61586406245SKay Sievers 	if (parent)
616c744aeaeSCornelia Huck 		return &parent->kobj;
617c744aeaeSCornelia Huck 	return NULL;
618c744aeaeSCornelia Huck }
619da231fd5SKay Sievers 
620da231fd5SKay Sievers static void cleanup_device_parent(struct device *dev)
621da231fd5SKay Sievers {
622da231fd5SKay Sievers 	struct device *d;
623da231fd5SKay Sievers 	int other = 0;
624da231fd5SKay Sievers 
625da231fd5SKay Sievers 	if (!dev->class)
626da231fd5SKay Sievers 		return;
627da231fd5SKay Sievers 
628da231fd5SKay Sievers 	/* see if we live in a parent class directory */
629da231fd5SKay Sievers 	if (dev->kobj.parent->kset != &dev->class->class_dirs)
630da231fd5SKay Sievers 		return;
631da231fd5SKay Sievers 
632da231fd5SKay Sievers 	/* if we are the last child of our class, delete the directory */
633da231fd5SKay Sievers 	down(&dev->class->sem);
634da231fd5SKay Sievers 	list_for_each_entry(d, &dev->class->devices, node) {
635da231fd5SKay Sievers 		if (d == dev)
636da231fd5SKay Sievers 			continue;
637da231fd5SKay Sievers 		if (d->kobj.parent == dev->kobj.parent) {
638da231fd5SKay Sievers 			other = 1;
639da231fd5SKay Sievers 			break;
640da231fd5SKay Sievers 		}
641da231fd5SKay Sievers 	}
642da231fd5SKay Sievers 	if (!other)
643da231fd5SKay Sievers 		kobject_del(dev->kobj.parent);
644da231fd5SKay Sievers 	kobject_put(dev->kobj.parent);
645da231fd5SKay Sievers 	up(&dev->class->sem);
646da231fd5SKay Sievers }
647c744aeaeSCornelia Huck #endif
64886406245SKay Sievers 
649c744aeaeSCornelia Huck static int 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 (IS_ERR(kobj))
654c744aeaeSCornelia Huck 		return PTR_ERR(kobj);
655c744aeaeSCornelia Huck 	if (kobj)
656c744aeaeSCornelia Huck 		dev->kobj.parent = kobj;
65740fa5422SGreg Kroah-Hartman 	return 0;
65840fa5422SGreg Kroah-Hartman }
65940fa5422SGreg Kroah-Hartman 
6602ee97cafSCornelia Huck static int device_add_class_symlinks(struct device *dev)
6612ee97cafSCornelia Huck {
6622ee97cafSCornelia Huck 	int error;
6632ee97cafSCornelia Huck 
6642ee97cafSCornelia Huck 	if (!dev->class)
6652ee97cafSCornelia Huck 		return 0;
666da231fd5SKay Sievers 
6672ee97cafSCornelia Huck 	error = sysfs_create_link(&dev->kobj, &dev->class->subsys.kobj,
6682ee97cafSCornelia Huck 				  "subsystem");
6692ee97cafSCornelia Huck 	if (error)
6702ee97cafSCornelia Huck 		goto out;
671da231fd5SKay Sievers 
672da231fd5SKay Sievers #ifdef CONFIG_SYSFS_DEPRECATED
673da231fd5SKay Sievers 	/* stacked class devices need a symlink in the class directory */
674edfaa7c3SKay Sievers 	if (dev->kobj.parent != &dev->class->subsys.kobj &&
675edfaa7c3SKay Sievers 	    dev->type != &part_type) {
6762ee97cafSCornelia Huck 		error = sysfs_create_link(&dev->class->subsys.kobj, &dev->kobj,
6772ee97cafSCornelia Huck 					  dev->bus_id);
6782ee97cafSCornelia Huck 		if (error)
6792ee97cafSCornelia Huck 			goto out_subsys;
6802ee97cafSCornelia Huck 	}
681da231fd5SKay Sievers 
682edfaa7c3SKay Sievers 	if (dev->parent && dev->type != &part_type) {
6834f01a757SDmitry Torokhov 		struct device *parent = dev->parent;
6844f01a757SDmitry Torokhov 		char *class_name;
6854f01a757SDmitry Torokhov 
6864f01a757SDmitry Torokhov 		/*
687da231fd5SKay Sievers 		 * stacked class devices have the 'device' link
688da231fd5SKay Sievers 		 * pointing to the bus device instead of the parent
6894f01a757SDmitry Torokhov 		 */
6904f01a757SDmitry Torokhov 		while (parent->class && !parent->bus && parent->parent)
6914f01a757SDmitry Torokhov 			parent = parent->parent;
6924f01a757SDmitry Torokhov 
6934f01a757SDmitry Torokhov 		error = sysfs_create_link(&dev->kobj,
6944f01a757SDmitry Torokhov 					  &parent->kobj,
6952ee97cafSCornelia Huck 					  "device");
6962ee97cafSCornelia Huck 		if (error)
6972ee97cafSCornelia Huck 			goto out_busid;
6984f01a757SDmitry Torokhov 
6994f01a757SDmitry Torokhov 		class_name = make_class_name(dev->class->name,
7002ee97cafSCornelia Huck 						&dev->kobj);
7012ee97cafSCornelia Huck 		if (class_name)
7022ee97cafSCornelia Huck 			error = sysfs_create_link(&dev->parent->kobj,
7032ee97cafSCornelia Huck 						&dev->kobj, class_name);
7042ee97cafSCornelia Huck 		kfree(class_name);
7052ee97cafSCornelia Huck 		if (error)
7062ee97cafSCornelia Huck 			goto out_device;
7072ee97cafSCornelia Huck 	}
708da231fd5SKay Sievers 	return 0;
709da231fd5SKay Sievers 
710da231fd5SKay Sievers out_device:
711edfaa7c3SKay Sievers 	if (dev->parent && dev->type != &part_type)
712da231fd5SKay Sievers 		sysfs_remove_link(&dev->kobj, "device");
713da231fd5SKay Sievers out_busid:
714edfaa7c3SKay Sievers 	if (dev->kobj.parent != &dev->class->subsys.kobj &&
715edfaa7c3SKay Sievers 	    dev->type != &part_type)
716da231fd5SKay Sievers 		sysfs_remove_link(&dev->class->subsys.kobj, dev->bus_id);
7174f01a757SDmitry Torokhov #else
718da231fd5SKay Sievers 	/* link in the class directory pointing to the device */
719da231fd5SKay Sievers 	error = sysfs_create_link(&dev->class->subsys.kobj, &dev->kobj,
720da231fd5SKay Sievers 				  dev->bus_id);
721da231fd5SKay Sievers 	if (error)
722da231fd5SKay Sievers 		goto out_subsys;
723da231fd5SKay Sievers 
724edfaa7c3SKay Sievers 	if (dev->parent && dev->type != &part_type) {
7254f01a757SDmitry Torokhov 		error = sysfs_create_link(&dev->kobj, &dev->parent->kobj,
7264f01a757SDmitry Torokhov 					  "device");
7274f01a757SDmitry Torokhov 		if (error)
7284f01a757SDmitry Torokhov 			goto out_busid;
7292ee97cafSCornelia Huck 	}
7302ee97cafSCornelia Huck 	return 0;
7312ee97cafSCornelia Huck 
7322ee97cafSCornelia Huck out_busid:
7332ee97cafSCornelia Huck 	sysfs_remove_link(&dev->class->subsys.kobj, dev->bus_id);
734da231fd5SKay Sievers #endif
735da231fd5SKay Sievers 
7362ee97cafSCornelia Huck out_subsys:
7372ee97cafSCornelia Huck 	sysfs_remove_link(&dev->kobj, "subsystem");
7382ee97cafSCornelia Huck out:
7392ee97cafSCornelia Huck 	return error;
7402ee97cafSCornelia Huck }
7412ee97cafSCornelia Huck 
7422ee97cafSCornelia Huck static void device_remove_class_symlinks(struct device *dev)
7432ee97cafSCornelia Huck {
7442ee97cafSCornelia Huck 	if (!dev->class)
7452ee97cafSCornelia Huck 		return;
746da231fd5SKay Sievers 
7472ee97cafSCornelia Huck #ifdef CONFIG_SYSFS_DEPRECATED
748edfaa7c3SKay Sievers 	if (dev->parent && dev->type != &part_type) {
7492ee97cafSCornelia Huck 		char *class_name;
7502ee97cafSCornelia Huck 
7512ee97cafSCornelia Huck 		class_name = make_class_name(dev->class->name, &dev->kobj);
7522ee97cafSCornelia Huck 		if (class_name) {
7532ee97cafSCornelia Huck 			sysfs_remove_link(&dev->parent->kobj, class_name);
7542ee97cafSCornelia Huck 			kfree(class_name);
7552ee97cafSCornelia Huck 		}
7562ee97cafSCornelia Huck 		sysfs_remove_link(&dev->kobj, "device");
7572ee97cafSCornelia Huck 	}
758da231fd5SKay Sievers 
759edfaa7c3SKay Sievers 	if (dev->kobj.parent != &dev->class->subsys.kobj &&
760edfaa7c3SKay Sievers 	    dev->type != &part_type)
7612ee97cafSCornelia Huck 		sysfs_remove_link(&dev->class->subsys.kobj, dev->bus_id);
762da231fd5SKay Sievers #else
763edfaa7c3SKay Sievers 	if (dev->parent && dev->type != &part_type)
764da231fd5SKay Sievers 		sysfs_remove_link(&dev->kobj, "device");
765da231fd5SKay Sievers 
766da231fd5SKay Sievers 	sysfs_remove_link(&dev->class->subsys.kobj, dev->bus_id);
767da231fd5SKay Sievers #endif
768da231fd5SKay Sievers 
7692ee97cafSCornelia Huck 	sysfs_remove_link(&dev->kobj, "subsystem");
7702ee97cafSCornelia Huck }
7712ee97cafSCornelia Huck 
7721da177e4SLinus Torvalds /**
7731da177e4SLinus Torvalds  *	device_add - add device to device hierarchy.
7741da177e4SLinus Torvalds  *	@dev:	device.
7751da177e4SLinus Torvalds  *
7761da177e4SLinus Torvalds  *	This is part 2 of device_register(), though may be called
7771da177e4SLinus Torvalds  *	separately _iff_ device_initialize() has been called separately.
7781da177e4SLinus Torvalds  *
779b2d6db58SGreg Kroah-Hartman  *	This adds it to the kobject hierarchy via kobject_add(), adds it
7801da177e4SLinus Torvalds  *	to the global and sibling lists for the device, then
7811da177e4SLinus Torvalds  *	adds it to the other relevant subsystems of the driver model.
7821da177e4SLinus Torvalds  */
7831da177e4SLinus Torvalds int device_add(struct device *dev)
7841da177e4SLinus Torvalds {
7851da177e4SLinus Torvalds 	struct device *parent = NULL;
786c47ed219SGreg Kroah-Hartman 	struct class_interface *class_intf;
787775b64d2SRafael J. Wysocki 	int error;
788775b64d2SRafael J. Wysocki 
789775b64d2SRafael J. Wysocki 	error = pm_sleep_lock();
790775b64d2SRafael J. Wysocki 	if (error) {
791775b64d2SRafael J. Wysocki 		dev_warn(dev, "Suspicious %s during suspend\n", __FUNCTION__);
792775b64d2SRafael J. Wysocki 		dump_stack();
793775b64d2SRafael J. Wysocki 		return error;
794775b64d2SRafael J. Wysocki 	}
7951da177e4SLinus Torvalds 
7961da177e4SLinus Torvalds 	dev = get_device(dev);
797775b64d2SRafael J. Wysocki 	if (!dev || !strlen(dev->bus_id)) {
798775b64d2SRafael J. Wysocki 		error = -EINVAL;
7991da177e4SLinus Torvalds 		goto Error;
800775b64d2SRafael J. Wysocki 	}
8011da177e4SLinus Torvalds 
8027dc72b28SGreg Kroah-Hartman 	pr_debug("device: '%s': %s\n", dev->bus_id, __FUNCTION__);
803c205ef48SGreg Kroah-Hartman 
8041da177e4SLinus Torvalds 	parent = get_device(dev->parent);
80540fa5422SGreg Kroah-Hartman 	error = setup_parent(dev, parent);
80640fa5422SGreg Kroah-Hartman 	if (error)
80740fa5422SGreg Kroah-Hartman 		goto Error;
8081da177e4SLinus Torvalds 
8091da177e4SLinus Torvalds 	/* first, register with generic layer. */
810b2d6db58SGreg Kroah-Hartman 	error = kobject_add(&dev->kobj, dev->kobj.parent, "%s", dev->bus_id);
81140fa5422SGreg Kroah-Hartman 	if (error)
8121da177e4SLinus Torvalds 		goto Error;
813a7fd6706SKay Sievers 
81437022644SBrian Walsh 	/* notify platform of device entry */
81537022644SBrian Walsh 	if (platform_notify)
81637022644SBrian Walsh 		platform_notify(dev);
81737022644SBrian Walsh 
818116af378SBenjamin Herrenschmidt 	/* notify clients of device entry (new way) */
819116af378SBenjamin Herrenschmidt 	if (dev->bus)
820c6f7e72aSGreg Kroah-Hartman 		blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
821116af378SBenjamin Herrenschmidt 					     BUS_NOTIFY_ADD_DEVICE, dev);
822116af378SBenjamin Herrenschmidt 
823ad6a1e1cSTejun Heo 	error = device_create_file(dev, &uevent_attr);
824a306eea4SCornelia Huck 	if (error)
825a306eea4SCornelia Huck 		goto attrError;
826a7fd6706SKay Sievers 
82723681e47SGreg Kroah-Hartman 	if (MAJOR(dev->devt)) {
828ad6a1e1cSTejun Heo 		error = device_create_file(dev, &devt_attr);
829ad6a1e1cSTejun Heo 		if (error)
830a306eea4SCornelia Huck 			goto ueventattrError;
83123681e47SGreg Kroah-Hartman 	}
83223681e47SGreg Kroah-Hartman 
8332ee97cafSCornelia Huck 	error = device_add_class_symlinks(dev);
8342ee97cafSCornelia Huck 	if (error)
8352ee97cafSCornelia Huck 		goto SymlinkError;
836dc0afa83SCornelia Huck 	error = device_add_attrs(dev);
837dc0afa83SCornelia Huck 	if (error)
8382620efefSGreg Kroah-Hartman 		goto AttrsError;
839dec13c15SDaniel Drake 	error = dpm_sysfs_add(dev);
840dc0afa83SCornelia Huck 	if (error)
8411da177e4SLinus Torvalds 		goto PMError;
842dec13c15SDaniel Drake 	device_pm_add(dev);
843dc0afa83SCornelia Huck 	error = bus_add_device(dev);
844dc0afa83SCornelia Huck 	if (error)
8451da177e4SLinus Torvalds 		goto BusError;
84653877d06SKay Sievers 	kobject_uevent(&dev->kobj, KOBJ_ADD);
847c6a46696SCornelia Huck 	bus_attach_device(dev);
8481da177e4SLinus Torvalds 	if (parent)
849d856f1e3SJames Bottomley 		klist_add_tail(&dev->knode_parent, &parent->klist_children);
8501da177e4SLinus Torvalds 
8515d9fd169SGreg Kroah-Hartman 	if (dev->class) {
8525d9fd169SGreg Kroah-Hartman 		down(&dev->class->sem);
853c47ed219SGreg Kroah-Hartman 		/* tie the class to the device */
8545d9fd169SGreg Kroah-Hartman 		list_add_tail(&dev->node, &dev->class->devices);
855c47ed219SGreg Kroah-Hartman 
856c47ed219SGreg Kroah-Hartman 		/* notify any interfaces that the device is here */
857c47ed219SGreg Kroah-Hartman 		list_for_each_entry(class_intf, &dev->class->interfaces, node)
858c47ed219SGreg Kroah-Hartman 			if (class_intf->add_dev)
859c47ed219SGreg Kroah-Hartman 				class_intf->add_dev(dev, class_intf);
8605d9fd169SGreg Kroah-Hartman 		up(&dev->class->sem);
8615d9fd169SGreg Kroah-Hartman 	}
8621da177e4SLinus Torvalds  Done:
8631da177e4SLinus Torvalds 	put_device(dev);
864775b64d2SRafael J. Wysocki 	pm_sleep_unlock();
8651da177e4SLinus Torvalds 	return error;
8661da177e4SLinus Torvalds  BusError:
8671da177e4SLinus Torvalds 	device_pm_remove(dev);
868dec13c15SDaniel Drake 	dpm_sysfs_remove(dev);
8691da177e4SLinus Torvalds  PMError:
870116af378SBenjamin Herrenschmidt 	if (dev->bus)
871c6f7e72aSGreg Kroah-Hartman 		blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
872116af378SBenjamin Herrenschmidt 					     BUS_NOTIFY_DEL_DEVICE, dev);
8732620efefSGreg Kroah-Hartman 	device_remove_attrs(dev);
8742620efefSGreg Kroah-Hartman  AttrsError:
8752ee97cafSCornelia Huck 	device_remove_class_symlinks(dev);
8762ee97cafSCornelia Huck  SymlinkError:
877ad6a1e1cSTejun Heo 	if (MAJOR(dev->devt))
878ad6a1e1cSTejun Heo 		device_remove_file(dev, &devt_attr);
879a306eea4SCornelia Huck  ueventattrError:
880ad6a1e1cSTejun Heo 	device_remove_file(dev, &uevent_attr);
88123681e47SGreg Kroah-Hartman  attrError:
882312c004dSKay Sievers 	kobject_uevent(&dev->kobj, KOBJ_REMOVE);
8831da177e4SLinus Torvalds 	kobject_del(&dev->kobj);
8841da177e4SLinus Torvalds  Error:
8851da177e4SLinus Torvalds 	if (parent)
8861da177e4SLinus Torvalds 		put_device(parent);
8871da177e4SLinus Torvalds 	goto Done;
8881da177e4SLinus Torvalds }
8891da177e4SLinus Torvalds 
8901da177e4SLinus Torvalds 
8911da177e4SLinus Torvalds /**
8921da177e4SLinus Torvalds  *	device_register - register a device with the system.
8931da177e4SLinus Torvalds  *	@dev:	pointer to the device structure
8941da177e4SLinus Torvalds  *
8951da177e4SLinus Torvalds  *	This happens in two clean steps - initialize the device
8961da177e4SLinus Torvalds  *	and add it to the system. The two steps can be called
8971da177e4SLinus Torvalds  *	separately, but this is the easiest and most common.
8981da177e4SLinus Torvalds  *	I.e. you should only call the two helpers separately if
8991da177e4SLinus Torvalds  *	have a clearly defined need to use and refcount the device
9001da177e4SLinus Torvalds  *	before it is added to the hierarchy.
9011da177e4SLinus Torvalds  */
9021da177e4SLinus Torvalds 
9031da177e4SLinus Torvalds int device_register(struct device *dev)
9041da177e4SLinus Torvalds {
9051da177e4SLinus Torvalds 	device_initialize(dev);
9061da177e4SLinus Torvalds 	return device_add(dev);
9071da177e4SLinus Torvalds }
9081da177e4SLinus Torvalds 
9091da177e4SLinus Torvalds 
9101da177e4SLinus Torvalds /**
9111da177e4SLinus Torvalds  *	get_device - increment reference count for device.
9121da177e4SLinus Torvalds  *	@dev:	device.
9131da177e4SLinus Torvalds  *
9141da177e4SLinus Torvalds  *	This simply forwards the call to kobject_get(), though
9151da177e4SLinus Torvalds  *	we do take care to provide for the case that we get a NULL
9161da177e4SLinus Torvalds  *	pointer passed in.
9171da177e4SLinus Torvalds  */
9181da177e4SLinus Torvalds 
9191da177e4SLinus Torvalds struct device * get_device(struct device * dev)
9201da177e4SLinus Torvalds {
9211da177e4SLinus Torvalds 	return dev ? to_dev(kobject_get(&dev->kobj)) : NULL;
9221da177e4SLinus Torvalds }
9231da177e4SLinus Torvalds 
9241da177e4SLinus Torvalds 
9251da177e4SLinus Torvalds /**
9261da177e4SLinus Torvalds  *	put_device - decrement reference count.
9271da177e4SLinus Torvalds  *	@dev:	device in question.
9281da177e4SLinus Torvalds  */
9291da177e4SLinus Torvalds void put_device(struct device * dev)
9301da177e4SLinus Torvalds {
931edfaa7c3SKay Sievers 	/* might_sleep(); */
9321da177e4SLinus Torvalds 	if (dev)
9331da177e4SLinus Torvalds 		kobject_put(&dev->kobj);
9341da177e4SLinus Torvalds }
9351da177e4SLinus Torvalds 
9361da177e4SLinus Torvalds 
9371da177e4SLinus Torvalds /**
9381da177e4SLinus Torvalds  *	device_del - delete device from system.
9391da177e4SLinus Torvalds  *	@dev:	device.
9401da177e4SLinus Torvalds  *
9411da177e4SLinus Torvalds  *	This is the first part of the device unregistration
9421da177e4SLinus Torvalds  *	sequence. This removes the device from the lists we control
9431da177e4SLinus Torvalds  *	from here, has it removed from the other driver model
9441da177e4SLinus Torvalds  *	subsystems it was added to in device_add(), and removes it
9451da177e4SLinus Torvalds  *	from the kobject hierarchy.
9461da177e4SLinus Torvalds  *
9471da177e4SLinus Torvalds  *	NOTE: this should be called manually _iff_ device_add() was
9481da177e4SLinus Torvalds  *	also called manually.
9491da177e4SLinus Torvalds  */
9501da177e4SLinus Torvalds 
9511da177e4SLinus Torvalds void device_del(struct device * dev)
9521da177e4SLinus Torvalds {
9531da177e4SLinus Torvalds 	struct device * parent = dev->parent;
954c47ed219SGreg Kroah-Hartman 	struct class_interface *class_intf;
9551da177e4SLinus Torvalds 
956775b64d2SRafael J. Wysocki 	device_pm_remove(dev);
9571da177e4SLinus Torvalds 	if (parent)
958d62c0f9fSPatrick Mochel 		klist_del(&dev->knode_parent);
959ad6a1e1cSTejun Heo 	if (MAJOR(dev->devt))
960ad6a1e1cSTejun Heo 		device_remove_file(dev, &devt_attr);
961b9d9c82bSKay Sievers 	if (dev->class) {
962da231fd5SKay Sievers 		device_remove_class_symlinks(dev);
96399ef3ef8SKay Sievers 
9645d9fd169SGreg Kroah-Hartman 		down(&dev->class->sem);
965c47ed219SGreg Kroah-Hartman 		/* notify any interfaces that the device is now gone */
966c47ed219SGreg Kroah-Hartman 		list_for_each_entry(class_intf, &dev->class->interfaces, node)
967c47ed219SGreg Kroah-Hartman 			if (class_intf->remove_dev)
968c47ed219SGreg Kroah-Hartman 				class_intf->remove_dev(dev, class_intf);
969c47ed219SGreg Kroah-Hartman 		/* remove the device from the class list */
9705d9fd169SGreg Kroah-Hartman 		list_del_init(&dev->node);
9715d9fd169SGreg Kroah-Hartman 		up(&dev->class->sem);
972b9d9c82bSKay Sievers 	}
973ad6a1e1cSTejun Heo 	device_remove_file(dev, &uevent_attr);
9742620efefSGreg Kroah-Hartman 	device_remove_attrs(dev);
97528953533SBenjamin Herrenschmidt 	bus_remove_device(dev);
9761da177e4SLinus Torvalds 
9772f8d16a9STejun Heo 	/*
9782f8d16a9STejun Heo 	 * Some platform devices are driven without driver attached
9792f8d16a9STejun Heo 	 * and managed resources may have been acquired.  Make sure
9802f8d16a9STejun Heo 	 * all resources are released.
9812f8d16a9STejun Heo 	 */
9822f8d16a9STejun Heo 	devres_release_all(dev);
9832f8d16a9STejun Heo 
9841da177e4SLinus Torvalds 	/* Notify the platform of the removal, in case they
9851da177e4SLinus Torvalds 	 * need to do anything...
9861da177e4SLinus Torvalds 	 */
9871da177e4SLinus Torvalds 	if (platform_notify_remove)
9881da177e4SLinus Torvalds 		platform_notify_remove(dev);
989116af378SBenjamin Herrenschmidt 	if (dev->bus)
990c6f7e72aSGreg Kroah-Hartman 		blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
991116af378SBenjamin Herrenschmidt 					     BUS_NOTIFY_DEL_DEVICE, dev);
992312c004dSKay Sievers 	kobject_uevent(&dev->kobj, KOBJ_REMOVE);
993da231fd5SKay Sievers 	cleanup_device_parent(dev);
9941da177e4SLinus Torvalds 	kobject_del(&dev->kobj);
9951da177e4SLinus Torvalds 	put_device(parent);
9961da177e4SLinus Torvalds }
9971da177e4SLinus Torvalds 
9981da177e4SLinus Torvalds /**
9991da177e4SLinus Torvalds  *	device_unregister - unregister device from system.
10001da177e4SLinus Torvalds  *	@dev:	device going away.
10011da177e4SLinus Torvalds  *
10021da177e4SLinus Torvalds  *	We do this in two parts, like we do device_register(). First,
10031da177e4SLinus Torvalds  *	we remove it from all the subsystems with device_del(), then
10041da177e4SLinus Torvalds  *	we decrement the reference count via put_device(). If that
10051da177e4SLinus Torvalds  *	is the final reference count, the device will be cleaned up
10061da177e4SLinus Torvalds  *	via device_release() above. Otherwise, the structure will
10071da177e4SLinus Torvalds  *	stick around until the final reference to the device is dropped.
10081da177e4SLinus Torvalds  */
10091da177e4SLinus Torvalds void device_unregister(struct device * dev)
10101da177e4SLinus Torvalds {
10117dc72b28SGreg Kroah-Hartman 	pr_debug("device: '%s': %s\n", dev->bus_id, __FUNCTION__);
10121da177e4SLinus Torvalds 	device_del(dev);
10131da177e4SLinus Torvalds 	put_device(dev);
10141da177e4SLinus Torvalds }
10151da177e4SLinus Torvalds 
10161da177e4SLinus Torvalds 
101736239577Smochel@digitalimplant.org static struct device * next_device(struct klist_iter * i)
101836239577Smochel@digitalimplant.org {
101936239577Smochel@digitalimplant.org 	struct klist_node * n = klist_next(i);
102036239577Smochel@digitalimplant.org 	return n ? container_of(n, struct device, knode_parent) : NULL;
102136239577Smochel@digitalimplant.org }
102236239577Smochel@digitalimplant.org 
10231da177e4SLinus Torvalds /**
10241da177e4SLinus Torvalds  *	device_for_each_child - device child iterator.
1025c41455fbSRandy Dunlap  *	@parent: parent struct device.
10261da177e4SLinus Torvalds  *	@data:	data for the callback.
10271da177e4SLinus Torvalds  *	@fn:	function to be called for each device.
10281da177e4SLinus Torvalds  *
1029c41455fbSRandy Dunlap  *	Iterate over @parent's child devices, and call @fn for each,
10301da177e4SLinus Torvalds  *	passing it @data.
10311da177e4SLinus Torvalds  *
10321da177e4SLinus Torvalds  *	We check the return of @fn each time. If it returns anything
10331da177e4SLinus Torvalds  *	other than 0, we break out and return that value.
10341da177e4SLinus Torvalds  */
103536239577Smochel@digitalimplant.org int device_for_each_child(struct device * parent, void * data,
10361da177e4SLinus Torvalds 		     int (*fn)(struct device *, void *))
10371da177e4SLinus Torvalds {
103836239577Smochel@digitalimplant.org 	struct klist_iter i;
10391da177e4SLinus Torvalds 	struct device * child;
10401da177e4SLinus Torvalds 	int error = 0;
10411da177e4SLinus Torvalds 
104236239577Smochel@digitalimplant.org 	klist_iter_init(&parent->klist_children, &i);
104336239577Smochel@digitalimplant.org 	while ((child = next_device(&i)) && !error)
104436239577Smochel@digitalimplant.org 		error = fn(child, data);
104536239577Smochel@digitalimplant.org 	klist_iter_exit(&i);
10461da177e4SLinus Torvalds 	return error;
10471da177e4SLinus Torvalds }
10481da177e4SLinus Torvalds 
10495ab69981SCornelia Huck /**
10505ab69981SCornelia Huck  * device_find_child - device iterator for locating a particular device.
10515ab69981SCornelia Huck  * @parent: parent struct device
10525ab69981SCornelia Huck  * @data: Data to pass to match function
10535ab69981SCornelia Huck  * @match: Callback function to check device
10545ab69981SCornelia Huck  *
10555ab69981SCornelia Huck  * This is similar to the device_for_each_child() function above, but it
10565ab69981SCornelia Huck  * returns a reference to a device that is 'found' for later use, as
10575ab69981SCornelia Huck  * determined by the @match callback.
10585ab69981SCornelia Huck  *
10595ab69981SCornelia Huck  * The callback should return 0 if the device doesn't match and non-zero
10605ab69981SCornelia Huck  * if it does.  If the callback returns non-zero and a reference to the
10615ab69981SCornelia Huck  * current device can be obtained, this function will return to the caller
10625ab69981SCornelia Huck  * and not iterate over any more devices.
10635ab69981SCornelia Huck  */
10645ab69981SCornelia Huck struct device * device_find_child(struct device *parent, void *data,
10655ab69981SCornelia Huck 				  int (*match)(struct device *, void *))
10665ab69981SCornelia Huck {
10675ab69981SCornelia Huck 	struct klist_iter i;
10685ab69981SCornelia Huck 	struct device *child;
10695ab69981SCornelia Huck 
10705ab69981SCornelia Huck 	if (!parent)
10715ab69981SCornelia Huck 		return NULL;
10725ab69981SCornelia Huck 
10735ab69981SCornelia Huck 	klist_iter_init(&parent->klist_children, &i);
10745ab69981SCornelia Huck 	while ((child = next_device(&i)))
10755ab69981SCornelia Huck 		if (match(child, data) && get_device(child))
10765ab69981SCornelia Huck 			break;
10775ab69981SCornelia Huck 	klist_iter_exit(&i);
10785ab69981SCornelia Huck 	return child;
10795ab69981SCornelia Huck }
10805ab69981SCornelia Huck 
10811da177e4SLinus Torvalds int __init devices_init(void)
10821da177e4SLinus Torvalds {
1083881c6cfdSGreg Kroah-Hartman 	devices_kset = kset_create_and_add("devices", &device_uevent_ops, NULL);
1084881c6cfdSGreg Kroah-Hartman 	if (!devices_kset)
1085881c6cfdSGreg Kroah-Hartman 		return -ENOMEM;
1086881c6cfdSGreg Kroah-Hartman 	return 0;
10871da177e4SLinus Torvalds }
10881da177e4SLinus Torvalds 
10891da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(device_for_each_child);
10905ab69981SCornelia Huck EXPORT_SYMBOL_GPL(device_find_child);
10911da177e4SLinus Torvalds 
10921da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(device_initialize);
10931da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(device_add);
10941da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(device_register);
10951da177e4SLinus Torvalds 
10961da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(device_del);
10971da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(device_unregister);
10981da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(get_device);
10991da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(put_device);
11001da177e4SLinus Torvalds 
11011da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(device_create_file);
11021da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(device_remove_file);
110323681e47SGreg Kroah-Hartman 
110423681e47SGreg Kroah-Hartman 
110523681e47SGreg Kroah-Hartman static void device_create_release(struct device *dev)
110623681e47SGreg Kroah-Hartman {
11077dc72b28SGreg Kroah-Hartman 	pr_debug("device: '%s': %s\n", dev->bus_id, __FUNCTION__);
110823681e47SGreg Kroah-Hartman 	kfree(dev);
110923681e47SGreg Kroah-Hartman }
111023681e47SGreg Kroah-Hartman 
111123681e47SGreg Kroah-Hartman /**
111223681e47SGreg Kroah-Hartman  * device_create - creates a device and registers it with sysfs
111342734dafSHenrik Kretzschmar  * @class: pointer to the struct class that this device should be registered to
111442734dafSHenrik Kretzschmar  * @parent: pointer to the parent struct device of this new device, if any
111542734dafSHenrik Kretzschmar  * @devt: the dev_t for the char device to be added
111642734dafSHenrik Kretzschmar  * @fmt: string for the device's name
111723681e47SGreg Kroah-Hartman  *
111842734dafSHenrik Kretzschmar  * This function can be used by char device classes.  A struct device
111942734dafSHenrik Kretzschmar  * will be created in sysfs, registered to the specified class.
112042734dafSHenrik Kretzschmar  *
112123681e47SGreg Kroah-Hartman  * A "dev" file will be created, showing the dev_t for the device, if
112223681e47SGreg Kroah-Hartman  * the dev_t is not 0,0.
112342734dafSHenrik Kretzschmar  * If a pointer to a parent struct device is passed in, the newly created
112442734dafSHenrik Kretzschmar  * struct device will be a child of that device in sysfs.
112542734dafSHenrik Kretzschmar  * The pointer to the struct device will be returned from the call.
112642734dafSHenrik Kretzschmar  * Any further sysfs files that might be required can be created using this
112723681e47SGreg Kroah-Hartman  * pointer.
112823681e47SGreg Kroah-Hartman  *
112923681e47SGreg Kroah-Hartman  * Note: the struct class passed to this function must have previously
113023681e47SGreg Kroah-Hartman  * been created with a call to class_create().
113123681e47SGreg Kroah-Hartman  */
113223681e47SGreg Kroah-Hartman struct device *device_create(struct class *class, struct device *parent,
11335cbe5f8aSGreg Kroah-Hartman 			     dev_t devt, const char *fmt, ...)
113423681e47SGreg Kroah-Hartman {
113523681e47SGreg Kroah-Hartman 	va_list args;
113623681e47SGreg Kroah-Hartman 	struct device *dev = NULL;
113723681e47SGreg Kroah-Hartman 	int retval = -ENODEV;
113823681e47SGreg Kroah-Hartman 
113923681e47SGreg Kroah-Hartman 	if (class == NULL || IS_ERR(class))
114023681e47SGreg Kroah-Hartman 		goto error;
114123681e47SGreg Kroah-Hartman 
114223681e47SGreg Kroah-Hartman 	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
114323681e47SGreg Kroah-Hartman 	if (!dev) {
114423681e47SGreg Kroah-Hartman 		retval = -ENOMEM;
114523681e47SGreg Kroah-Hartman 		goto error;
114623681e47SGreg Kroah-Hartman 	}
114723681e47SGreg Kroah-Hartman 
114823681e47SGreg Kroah-Hartman 	dev->devt = devt;
114923681e47SGreg Kroah-Hartman 	dev->class = class;
115023681e47SGreg Kroah-Hartman 	dev->parent = parent;
115123681e47SGreg Kroah-Hartman 	dev->release = device_create_release;
115223681e47SGreg Kroah-Hartman 
115323681e47SGreg Kroah-Hartman 	va_start(args, fmt);
115423681e47SGreg Kroah-Hartman 	vsnprintf(dev->bus_id, BUS_ID_SIZE, fmt, args);
115523681e47SGreg Kroah-Hartman 	va_end(args);
115623681e47SGreg Kroah-Hartman 	retval = device_register(dev);
115723681e47SGreg Kroah-Hartman 	if (retval)
115823681e47SGreg Kroah-Hartman 		goto error;
115923681e47SGreg Kroah-Hartman 
116023681e47SGreg Kroah-Hartman 	return dev;
116123681e47SGreg Kroah-Hartman 
116223681e47SGreg Kroah-Hartman error:
116323681e47SGreg Kroah-Hartman 	kfree(dev);
116423681e47SGreg Kroah-Hartman 	return ERR_PTR(retval);
116523681e47SGreg Kroah-Hartman }
116623681e47SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(device_create);
116723681e47SGreg Kroah-Hartman 
116823681e47SGreg Kroah-Hartman /**
1169775b64d2SRafael J. Wysocki  * find_device - finds a device that was created with device_create()
117042734dafSHenrik Kretzschmar  * @class: pointer to the struct class that this device was registered with
117142734dafSHenrik Kretzschmar  * @devt: the dev_t of the device that was previously registered
117223681e47SGreg Kroah-Hartman  */
1173775b64d2SRafael J. Wysocki static struct device *find_device(struct class *class, dev_t devt)
117423681e47SGreg Kroah-Hartman {
117523681e47SGreg Kroah-Hartman 	struct device *dev = NULL;
117623681e47SGreg Kroah-Hartman 	struct device *dev_tmp;
117723681e47SGreg Kroah-Hartman 
117823681e47SGreg Kroah-Hartman 	down(&class->sem);
117923681e47SGreg Kroah-Hartman 	list_for_each_entry(dev_tmp, &class->devices, node) {
118023681e47SGreg Kroah-Hartman 		if (dev_tmp->devt == devt) {
118123681e47SGreg Kroah-Hartman 			dev = dev_tmp;
118223681e47SGreg Kroah-Hartman 			break;
118323681e47SGreg Kroah-Hartman 		}
118423681e47SGreg Kroah-Hartman 	}
118523681e47SGreg Kroah-Hartman 	up(&class->sem);
1186775b64d2SRafael J. Wysocki 	return dev;
1187775b64d2SRafael J. Wysocki }
118823681e47SGreg Kroah-Hartman 
1189775b64d2SRafael J. Wysocki /**
1190775b64d2SRafael J. Wysocki  * device_destroy - removes a device that was created with device_create()
1191775b64d2SRafael J. Wysocki  * @class: pointer to the struct class that this device was registered with
1192775b64d2SRafael J. Wysocki  * @devt: the dev_t of the device that was previously registered
1193775b64d2SRafael J. Wysocki  *
1194775b64d2SRafael J. Wysocki  * This call unregisters and cleans up a device that was created with a
1195775b64d2SRafael J. Wysocki  * call to device_create().
1196775b64d2SRafael J. Wysocki  */
1197775b64d2SRafael J. Wysocki void device_destroy(struct class *class, dev_t devt)
1198775b64d2SRafael J. Wysocki {
1199775b64d2SRafael J. Wysocki 	struct device *dev;
1200775b64d2SRafael J. Wysocki 
1201775b64d2SRafael J. Wysocki 	dev = find_device(class, devt);
12025d9fd169SGreg Kroah-Hartman 	if (dev)
120323681e47SGreg Kroah-Hartman 		device_unregister(dev);
120423681e47SGreg Kroah-Hartman }
120523681e47SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(device_destroy);
1206a2de48caSGreg Kroah-Hartman 
1207775b64d2SRafael J. Wysocki #ifdef CONFIG_PM_SLEEP
1208775b64d2SRafael J. Wysocki /**
1209775b64d2SRafael J. Wysocki  * destroy_suspended_device - asks the PM core to remove a suspended device
1210775b64d2SRafael J. Wysocki  * @class: pointer to the struct class that this device was registered with
1211775b64d2SRafael J. Wysocki  * @devt: the dev_t of the device that was previously registered
1212775b64d2SRafael J. Wysocki  *
1213775b64d2SRafael J. Wysocki  * This call notifies the PM core of the necessity to unregister a suspended
1214775b64d2SRafael J. Wysocki  * device created with a call to device_create() (devices cannot be
1215775b64d2SRafael J. Wysocki  * unregistered directly while suspended, since the PM core holds their
1216775b64d2SRafael J. Wysocki  * semaphores at that time).
1217775b64d2SRafael J. Wysocki  *
1218775b64d2SRafael J. Wysocki  * It can only be called within the scope of a system sleep transition.  In
1219775b64d2SRafael J. Wysocki  * practice this means it has to be directly or indirectly invoked either by
1220775b64d2SRafael J. Wysocki  * a suspend or resume method, or by the PM core (e.g. via
1221775b64d2SRafael J. Wysocki  * disable_nonboot_cpus() or enable_nonboot_cpus()).
1222775b64d2SRafael J. Wysocki  */
1223775b64d2SRafael J. Wysocki void destroy_suspended_device(struct class *class, dev_t devt)
1224775b64d2SRafael J. Wysocki {
1225775b64d2SRafael J. Wysocki 	struct device *dev;
1226775b64d2SRafael J. Wysocki 
1227775b64d2SRafael J. Wysocki 	dev = find_device(class, devt);
1228775b64d2SRafael J. Wysocki 	if (dev)
1229775b64d2SRafael J. Wysocki 		device_pm_schedule_removal(dev);
1230775b64d2SRafael J. Wysocki }
1231775b64d2SRafael J. Wysocki EXPORT_SYMBOL_GPL(destroy_suspended_device);
1232775b64d2SRafael J. Wysocki #endif /* CONFIG_PM_SLEEP */
1233775b64d2SRafael J. Wysocki 
1234a2de48caSGreg Kroah-Hartman /**
1235a2de48caSGreg Kroah-Hartman  * device_rename - renames a device
1236a2de48caSGreg Kroah-Hartman  * @dev: the pointer to the struct device to be renamed
1237a2de48caSGreg Kroah-Hartman  * @new_name: the new name of the device
1238a2de48caSGreg Kroah-Hartman  */
1239a2de48caSGreg Kroah-Hartman int device_rename(struct device *dev, char *new_name)
1240a2de48caSGreg Kroah-Hartman {
1241a2de48caSGreg Kroah-Hartman 	char *old_class_name = NULL;
1242a2de48caSGreg Kroah-Hartman 	char *new_class_name = NULL;
12432ee97cafSCornelia Huck 	char *old_device_name = NULL;
1244a2de48caSGreg Kroah-Hartman 	int error;
1245a2de48caSGreg Kroah-Hartman 
1246a2de48caSGreg Kroah-Hartman 	dev = get_device(dev);
1247a2de48caSGreg Kroah-Hartman 	if (!dev)
1248a2de48caSGreg Kroah-Hartman 		return -EINVAL;
1249a2de48caSGreg Kroah-Hartman 
12507dc72b28SGreg Kroah-Hartman 	pr_debug("device: '%s': %s: renaming to '%s'\n", dev->bus_id,
12517dc72b28SGreg Kroah-Hartman 		 __FUNCTION__, new_name);
1252a2de48caSGreg Kroah-Hartman 
125399ef3ef8SKay Sievers #ifdef CONFIG_SYSFS_DEPRECATED
1254a2de48caSGreg Kroah-Hartman 	if ((dev->class) && (dev->parent))
1255a2de48caSGreg Kroah-Hartman 		old_class_name = make_class_name(dev->class->name, &dev->kobj);
125699ef3ef8SKay Sievers #endif
1257a2de48caSGreg Kroah-Hartman 
12582ee97cafSCornelia Huck 	old_device_name = kmalloc(BUS_ID_SIZE, GFP_KERNEL);
12592ee97cafSCornelia Huck 	if (!old_device_name) {
1260952ab431SJesper Juhl 		error = -ENOMEM;
12612ee97cafSCornelia Huck 		goto out;
1262952ab431SJesper Juhl 	}
12632ee97cafSCornelia Huck 	strlcpy(old_device_name, dev->bus_id, BUS_ID_SIZE);
1264a2de48caSGreg Kroah-Hartman 	strlcpy(dev->bus_id, new_name, BUS_ID_SIZE);
1265a2de48caSGreg Kroah-Hartman 
1266a2de48caSGreg Kroah-Hartman 	error = kobject_rename(&dev->kobj, new_name);
12672ee97cafSCornelia Huck 	if (error) {
12682ee97cafSCornelia Huck 		strlcpy(dev->bus_id, old_device_name, BUS_ID_SIZE);
12692ee97cafSCornelia Huck 		goto out;
12702ee97cafSCornelia Huck 	}
1271a2de48caSGreg Kroah-Hartman 
127299ef3ef8SKay Sievers #ifdef CONFIG_SYSFS_DEPRECATED
1273a2de48caSGreg Kroah-Hartman 	if (old_class_name) {
1274a2de48caSGreg Kroah-Hartman 		new_class_name = make_class_name(dev->class->name, &dev->kobj);
1275a2de48caSGreg Kroah-Hartman 		if (new_class_name) {
12762ee97cafSCornelia Huck 			error = sysfs_create_link(&dev->parent->kobj,
12772ee97cafSCornelia Huck 						  &dev->kobj, new_class_name);
12782ee97cafSCornelia Huck 			if (error)
12792ee97cafSCornelia Huck 				goto out;
1280a2de48caSGreg Kroah-Hartman 			sysfs_remove_link(&dev->parent->kobj, old_class_name);
1281a2de48caSGreg Kroah-Hartman 		}
1282a2de48caSGreg Kroah-Hartman 	}
128360b8cabdSKay Sievers #else
1284a2de48caSGreg Kroah-Hartman 	if (dev->class) {
12852ee97cafSCornelia Huck 		sysfs_remove_link(&dev->class->subsys.kobj, old_device_name);
12862ee97cafSCornelia Huck 		error = sysfs_create_link(&dev->class->subsys.kobj, &dev->kobj,
1287a2de48caSGreg Kroah-Hartman 					  dev->bus_id);
12882ee97cafSCornelia Huck 		if (error) {
12892ee97cafSCornelia Huck 			dev_err(dev, "%s: sysfs_create_symlink failed (%d)\n",
12902ee97cafSCornelia Huck 				__FUNCTION__, error);
1291a2de48caSGreg Kroah-Hartman 		}
12922ee97cafSCornelia Huck 	}
129360b8cabdSKay Sievers #endif
129460b8cabdSKay Sievers 
12952ee97cafSCornelia Huck out:
1296a2de48caSGreg Kroah-Hartman 	put_device(dev);
1297a2de48caSGreg Kroah-Hartman 
1298a2de48caSGreg Kroah-Hartman 	kfree(new_class_name);
1299952ab431SJesper Juhl 	kfree(old_class_name);
13002ee97cafSCornelia Huck 	kfree(old_device_name);
1301a2de48caSGreg Kroah-Hartman 
1302a2de48caSGreg Kroah-Hartman 	return error;
1303a2de48caSGreg Kroah-Hartman }
1304a2807dbcSJohannes Berg EXPORT_SYMBOL_GPL(device_rename);
13058a82472fSCornelia Huck 
13068a82472fSCornelia Huck static int device_move_class_links(struct device *dev,
13078a82472fSCornelia Huck 				   struct device *old_parent,
13088a82472fSCornelia Huck 				   struct device *new_parent)
13098a82472fSCornelia Huck {
1310f7f3461dSGreg Kroah-Hartman 	int error = 0;
13118a82472fSCornelia Huck #ifdef CONFIG_SYSFS_DEPRECATED
13128a82472fSCornelia Huck 	char *class_name;
13138a82472fSCornelia Huck 
13148a82472fSCornelia Huck 	class_name = make_class_name(dev->class->name, &dev->kobj);
13158a82472fSCornelia Huck 	if (!class_name) {
1316cb360bbfSCornelia Huck 		error = -ENOMEM;
13178a82472fSCornelia Huck 		goto out;
13188a82472fSCornelia Huck 	}
13198a82472fSCornelia Huck 	if (old_parent) {
13208a82472fSCornelia Huck 		sysfs_remove_link(&dev->kobj, "device");
13218a82472fSCornelia Huck 		sysfs_remove_link(&old_parent->kobj, class_name);
13228a82472fSCornelia Huck 	}
1323c744aeaeSCornelia Huck 	if (new_parent) {
1324c744aeaeSCornelia Huck 		error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
1325c744aeaeSCornelia Huck 					  "device");
13268a82472fSCornelia Huck 		if (error)
13278a82472fSCornelia Huck 			goto out;
1328c744aeaeSCornelia Huck 		error = sysfs_create_link(&new_parent->kobj, &dev->kobj,
1329c744aeaeSCornelia Huck 					  class_name);
13308a82472fSCornelia Huck 		if (error)
13318a82472fSCornelia Huck 			sysfs_remove_link(&dev->kobj, "device");
1332c744aeaeSCornelia Huck 	}
1333c744aeaeSCornelia Huck 	else
1334c744aeaeSCornelia Huck 		error = 0;
13358a82472fSCornelia Huck out:
13368a82472fSCornelia Huck 	kfree(class_name);
13378a82472fSCornelia Huck 	return error;
13388a82472fSCornelia Huck #else
1339f7f3461dSGreg Kroah-Hartman 	if (old_parent)
1340f7f3461dSGreg Kroah-Hartman 		sysfs_remove_link(&dev->kobj, "device");
1341f7f3461dSGreg Kroah-Hartman 	if (new_parent)
1342f7f3461dSGreg Kroah-Hartman 		error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
1343f7f3461dSGreg Kroah-Hartman 					  "device");
1344f7f3461dSGreg Kroah-Hartman 	return error;
13458a82472fSCornelia Huck #endif
13468a82472fSCornelia Huck }
13478a82472fSCornelia Huck 
13488a82472fSCornelia Huck /**
13498a82472fSCornelia Huck  * device_move - moves a device to a new parent
13508a82472fSCornelia Huck  * @dev: the pointer to the struct device to be moved
1351c744aeaeSCornelia Huck  * @new_parent: the new parent of the device (can by NULL)
13528a82472fSCornelia Huck  */
13538a82472fSCornelia Huck int device_move(struct device *dev, struct device *new_parent)
13548a82472fSCornelia Huck {
13558a82472fSCornelia Huck 	int error;
13568a82472fSCornelia Huck 	struct device *old_parent;
1357c744aeaeSCornelia Huck 	struct kobject *new_parent_kobj;
13588a82472fSCornelia Huck 
13598a82472fSCornelia Huck 	dev = get_device(dev);
13608a82472fSCornelia Huck 	if (!dev)
13618a82472fSCornelia Huck 		return -EINVAL;
13628a82472fSCornelia Huck 
13638a82472fSCornelia Huck 	new_parent = get_device(new_parent);
1364c744aeaeSCornelia Huck 	new_parent_kobj = get_device_parent (dev, new_parent);
1365c744aeaeSCornelia Huck 	if (IS_ERR(new_parent_kobj)) {
1366c744aeaeSCornelia Huck 		error = PTR_ERR(new_parent_kobj);
1367c744aeaeSCornelia Huck 		put_device(new_parent);
13688a82472fSCornelia Huck 		goto out;
13698a82472fSCornelia Huck 	}
13707dc72b28SGreg Kroah-Hartman 	pr_debug("device: '%s': %s: moving to '%s'\n", dev->bus_id,
13717dc72b28SGreg Kroah-Hartman 		 __FUNCTION__, new_parent ? new_parent->bus_id : "<NULL>");
1372c744aeaeSCornelia Huck 	error = kobject_move(&dev->kobj, new_parent_kobj);
13738a82472fSCornelia Huck 	if (error) {
13748a82472fSCornelia Huck 		put_device(new_parent);
13758a82472fSCornelia Huck 		goto out;
13768a82472fSCornelia Huck 	}
13778a82472fSCornelia Huck 	old_parent = dev->parent;
13788a82472fSCornelia Huck 	dev->parent = new_parent;
13798a82472fSCornelia Huck 	if (old_parent)
1380acf02d23SCornelia Huck 		klist_remove(&dev->knode_parent);
1381c744aeaeSCornelia Huck 	if (new_parent)
13828a82472fSCornelia Huck 		klist_add_tail(&dev->knode_parent, &new_parent->klist_children);
13838a82472fSCornelia Huck 	if (!dev->class)
13848a82472fSCornelia Huck 		goto out_put;
13858a82472fSCornelia Huck 	error = device_move_class_links(dev, old_parent, new_parent);
13868a82472fSCornelia Huck 	if (error) {
13878a82472fSCornelia Huck 		/* We ignore errors on cleanup since we're hosed anyway... */
13888a82472fSCornelia Huck 		device_move_class_links(dev, new_parent, old_parent);
13898a82472fSCornelia Huck 		if (!kobject_move(&dev->kobj, &old_parent->kobj)) {
1390c744aeaeSCornelia Huck 			if (new_parent)
1391acf02d23SCornelia Huck 				klist_remove(&dev->knode_parent);
13928a82472fSCornelia Huck 			if (old_parent)
13938a82472fSCornelia Huck 				klist_add_tail(&dev->knode_parent,
13948a82472fSCornelia Huck 					       &old_parent->klist_children);
13958a82472fSCornelia Huck 		}
13968a82472fSCornelia Huck 		put_device(new_parent);
13978a82472fSCornelia Huck 		goto out;
13988a82472fSCornelia Huck 	}
13998a82472fSCornelia Huck out_put:
14008a82472fSCornelia Huck 	put_device(old_parent);
14018a82472fSCornelia Huck out:
14028a82472fSCornelia Huck 	put_device(dev);
14038a82472fSCornelia Huck 	return error;
14048a82472fSCornelia Huck }
14058a82472fSCornelia Huck EXPORT_SYMBOL_GPL(device_move);
140637b0c020SGreg Kroah-Hartman 
140737b0c020SGreg Kroah-Hartman /**
140837b0c020SGreg Kroah-Hartman  * device_shutdown - call ->shutdown() on each device to shutdown.
140937b0c020SGreg Kroah-Hartman  */
141037b0c020SGreg Kroah-Hartman void device_shutdown(void)
141137b0c020SGreg Kroah-Hartman {
141237b0c020SGreg Kroah-Hartman 	struct device * dev, *devn;
141337b0c020SGreg Kroah-Hartman 
141437b0c020SGreg Kroah-Hartman 	list_for_each_entry_safe_reverse(dev, devn, &devices_kset->list,
141537b0c020SGreg Kroah-Hartman 				kobj.entry) {
141637b0c020SGreg Kroah-Hartman 		if (dev->bus && dev->bus->shutdown) {
141737b0c020SGreg Kroah-Hartman 			dev_dbg(dev, "shutdown\n");
141837b0c020SGreg Kroah-Hartman 			dev->bus->shutdown(dev);
141937b0c020SGreg Kroah-Hartman 		} else if (dev->driver && dev->driver->shutdown) {
142037b0c020SGreg Kroah-Hartman 			dev_dbg(dev, "shutdown\n");
142137b0c020SGreg Kroah-Hartman 			dev->driver->shutdown(dev);
142237b0c020SGreg Kroah-Hartman 		}
142337b0c020SGreg Kroah-Hartman 	}
142437b0c020SGreg Kroah-Hartman }
1425