xref: /openbmc/linux/drivers/base/core.c (revision a2807dbc)
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>
211da177e4SLinus Torvalds 
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 /*
31b446b60eSAndrew Morton  * Detect the LANANA-assigned LOCAL/EXPERIMENTAL majors
32b446b60eSAndrew Morton  */
33b446b60eSAndrew Morton bool is_lanana_major(unsigned int major)
34b446b60eSAndrew Morton {
35b446b60eSAndrew Morton 	if (major >= 60 && major <= 63)
36b446b60eSAndrew Morton 		return 1;
37b446b60eSAndrew Morton 	if (major >= 120 && major <= 127)
38b446b60eSAndrew Morton 		return 1;
39b446b60eSAndrew Morton 	if (major >= 240 && major <= 254)
40b446b60eSAndrew Morton 		return 1;
41b446b60eSAndrew Morton 	return 0;
42b446b60eSAndrew Morton }
43b446b60eSAndrew Morton 
44b446b60eSAndrew Morton /*
451da177e4SLinus Torvalds  * sysfs bindings for devices.
461da177e4SLinus Torvalds  */
471da177e4SLinus Torvalds 
483e95637aSAlan Stern /**
493e95637aSAlan Stern  * dev_driver_string - Return a device's driver name, if at all possible
503e95637aSAlan Stern  * @dev: struct device to get the name of
513e95637aSAlan Stern  *
523e95637aSAlan Stern  * Will return the device's driver's name if it is bound to a device.  If
533e95637aSAlan Stern  * the device is not bound to a device, it will return the name of the bus
543e95637aSAlan Stern  * it is attached to.  If it is not attached to a bus either, an empty
553e95637aSAlan Stern  * string will be returned.
563e95637aSAlan Stern  */
573e95637aSAlan Stern const char *dev_driver_string(struct device *dev)
583e95637aSAlan Stern {
593e95637aSAlan Stern 	return dev->driver ? dev->driver->name :
603e95637aSAlan Stern 			(dev->bus ? dev->bus->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 
671da177e4SLinus Torvalds static ssize_t
681da177e4SLinus Torvalds dev_attr_show(struct kobject * kobj, struct attribute * attr, 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);
761da177e4SLinus Torvalds 	return ret;
771da177e4SLinus Torvalds }
781da177e4SLinus Torvalds 
791da177e4SLinus Torvalds static ssize_t
801da177e4SLinus Torvalds dev_attr_store(struct kobject * kobj, struct attribute * attr,
811da177e4SLinus Torvalds 	       const char * buf, size_t count)
821da177e4SLinus Torvalds {
831da177e4SLinus Torvalds 	struct device_attribute * dev_attr = to_dev_attr(attr);
841da177e4SLinus Torvalds 	struct device * dev = to_dev(kobj);
854a0c20bfSDmitry Torokhov 	ssize_t ret = -EIO;
861da177e4SLinus Torvalds 
871da177e4SLinus Torvalds 	if (dev_attr->store)
8854b6f35cSYani Ioannou 		ret = dev_attr->store(dev, dev_attr, buf, count);
891da177e4SLinus Torvalds 	return ret;
901da177e4SLinus Torvalds }
911da177e4SLinus Torvalds 
921da177e4SLinus Torvalds static struct sysfs_ops dev_sysfs_ops = {
931da177e4SLinus Torvalds 	.show	= dev_attr_show,
941da177e4SLinus Torvalds 	.store	= dev_attr_store,
951da177e4SLinus Torvalds };
961da177e4SLinus Torvalds 
971da177e4SLinus Torvalds 
981da177e4SLinus Torvalds /**
991da177e4SLinus Torvalds  *	device_release - free device structure.
1001da177e4SLinus Torvalds  *	@kobj:	device's kobject.
1011da177e4SLinus Torvalds  *
1021da177e4SLinus Torvalds  *	This is called once the reference count for the object
1031da177e4SLinus Torvalds  *	reaches 0. We forward the call to the device's release
1041da177e4SLinus Torvalds  *	method, which should handle actually freeing the structure.
1051da177e4SLinus Torvalds  */
1061da177e4SLinus Torvalds static void device_release(struct kobject * kobj)
1071da177e4SLinus Torvalds {
1081da177e4SLinus Torvalds 	struct device * dev = to_dev(kobj);
1091da177e4SLinus Torvalds 
1101da177e4SLinus Torvalds 	if (dev->release)
1111da177e4SLinus Torvalds 		dev->release(dev);
112f9f852dfSKay Sievers 	else if (dev->type && dev->type->release)
113f9f852dfSKay Sievers 		dev->type->release(dev);
1142620efefSGreg Kroah-Hartman 	else if (dev->class && dev->class->dev_release)
1152620efefSGreg Kroah-Hartman 		dev->class->dev_release(dev);
1161da177e4SLinus Torvalds 	else {
1171da177e4SLinus Torvalds 		printk(KERN_ERR "Device '%s' does not have a release() function, "
1181da177e4SLinus Torvalds 			"it is broken and must be fixed.\n",
1191da177e4SLinus Torvalds 			dev->bus_id);
1201da177e4SLinus Torvalds 		WARN_ON(1);
1211da177e4SLinus Torvalds 	}
1221da177e4SLinus Torvalds }
1231da177e4SLinus Torvalds 
1241da177e4SLinus Torvalds static struct kobj_type ktype_device = {
1251da177e4SLinus Torvalds 	.release	= device_release,
1261da177e4SLinus Torvalds 	.sysfs_ops	= &dev_sysfs_ops,
1271da177e4SLinus Torvalds };
1281da177e4SLinus Torvalds 
1291da177e4SLinus Torvalds 
130312c004dSKay Sievers static int dev_uevent_filter(struct kset *kset, struct kobject *kobj)
1311da177e4SLinus Torvalds {
1321da177e4SLinus Torvalds 	struct kobj_type *ktype = get_ktype(kobj);
1331da177e4SLinus Torvalds 
1341da177e4SLinus Torvalds 	if (ktype == &ktype_device) {
1351da177e4SLinus Torvalds 		struct device *dev = to_dev(kobj);
1361da177e4SLinus Torvalds 		if (dev->bus)
1371da177e4SLinus Torvalds 			return 1;
13823681e47SGreg Kroah-Hartman 		if (dev->class)
13923681e47SGreg Kroah-Hartman 			return 1;
1401da177e4SLinus Torvalds 	}
1411da177e4SLinus Torvalds 	return 0;
1421da177e4SLinus Torvalds }
1431da177e4SLinus Torvalds 
144312c004dSKay Sievers static const char *dev_uevent_name(struct kset *kset, struct kobject *kobj)
1451da177e4SLinus Torvalds {
1461da177e4SLinus Torvalds 	struct device *dev = to_dev(kobj);
1471da177e4SLinus Torvalds 
14823681e47SGreg Kroah-Hartman 	if (dev->bus)
1491da177e4SLinus Torvalds 		return dev->bus->name;
15023681e47SGreg Kroah-Hartman 	if (dev->class)
15123681e47SGreg Kroah-Hartman 		return dev->class->name;
15223681e47SGreg Kroah-Hartman 	return NULL;
1531da177e4SLinus Torvalds }
1541da177e4SLinus Torvalds 
155312c004dSKay Sievers static int dev_uevent(struct kset *kset, struct kobject *kobj, char **envp,
1561da177e4SLinus Torvalds 			int num_envp, char *buffer, int buffer_size)
1571da177e4SLinus Torvalds {
1581da177e4SLinus Torvalds 	struct device *dev = to_dev(kobj);
1591da177e4SLinus Torvalds 	int i = 0;
1601da177e4SLinus Torvalds 	int length = 0;
1611da177e4SLinus Torvalds 	int retval = 0;
1621da177e4SLinus Torvalds 
16323681e47SGreg Kroah-Hartman 	/* add the major/minor if present */
16423681e47SGreg Kroah-Hartman 	if (MAJOR(dev->devt)) {
16523681e47SGreg Kroah-Hartman 		add_uevent_var(envp, num_envp, &i,
16623681e47SGreg Kroah-Hartman 			       buffer, buffer_size, &length,
16723681e47SGreg Kroah-Hartman 			       "MAJOR=%u", MAJOR(dev->devt));
16823681e47SGreg Kroah-Hartman 		add_uevent_var(envp, num_envp, &i,
16923681e47SGreg Kroah-Hartman 			       buffer, buffer_size, &length,
17023681e47SGreg Kroah-Hartman 			       "MINOR=%u", MINOR(dev->devt));
17123681e47SGreg Kroah-Hartman 	}
17223681e47SGreg Kroah-Hartman 
173239378f1SKay Sievers 	if (dev->driver)
174d81d9d6bSKay Sievers 		add_uevent_var(envp, num_envp, &i,
175d81d9d6bSKay Sievers 			       buffer, buffer_size, &length,
176d81d9d6bSKay Sievers 			       "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);
189239378f1SKay Sievers 			add_uevent_var(envp, num_envp, &i,
190239378f1SKay Sievers 				       buffer, buffer_size, &length,
191239378f1SKay Sievers 				       "PHYSDEVPATH=%s", path);
192239378f1SKay Sievers 			kfree(path);
193239378f1SKay Sievers 
194239378f1SKay Sievers 			add_uevent_var(envp, num_envp, &i,
195239378f1SKay Sievers 				       buffer, buffer_size, &length,
196239378f1SKay Sievers 				       "PHYSDEVBUS=%s", parent->bus->name);
197239378f1SKay Sievers 
198239378f1SKay Sievers 			if (parent->driver)
199239378f1SKay Sievers 				add_uevent_var(envp, num_envp, &i,
200239378f1SKay Sievers 					       buffer, buffer_size, &length,
201239378f1SKay Sievers 					       "PHYSDEVDRIVER=%s", parent->driver->name);
202239378f1SKay Sievers 		}
203239378f1SKay Sievers 	} else if (dev->bus) {
204239378f1SKay Sievers 		add_uevent_var(envp, num_envp, &i,
205239378f1SKay Sievers 			       buffer, buffer_size, &length,
206239378f1SKay Sievers 			       "PHYSDEVBUS=%s", dev->bus->name);
207239378f1SKay Sievers 
208239378f1SKay Sievers 		if (dev->driver)
209312c004dSKay Sievers 			add_uevent_var(envp, num_envp, &i,
2101da177e4SLinus Torvalds 				       buffer, buffer_size, &length,
2111da177e4SLinus Torvalds 				       "PHYSDEVDRIVER=%s", dev->driver->name);
212d81d9d6bSKay Sievers 	}
213239378f1SKay Sievers #endif
2141da177e4SLinus Torvalds 
2151da177e4SLinus Torvalds 	/* terminate, set to next free slot, shrink available space */
2161da177e4SLinus Torvalds 	envp[i] = NULL;
2171da177e4SLinus Torvalds 	envp = &envp[i];
2181da177e4SLinus Torvalds 	num_envp -= i;
2191da177e4SLinus Torvalds 	buffer = &buffer[length];
2201da177e4SLinus Torvalds 	buffer_size -= length;
2211da177e4SLinus Torvalds 
222312c004dSKay Sievers 	if (dev->bus && dev->bus->uevent) {
2231da177e4SLinus Torvalds 		/* have the bus specific function add its stuff */
224312c004dSKay Sievers 		retval = dev->bus->uevent(dev, envp, num_envp, buffer, buffer_size);
225f9f852dfSKay Sievers 		if (retval)
226f9f852dfSKay Sievers 			pr_debug ("%s: bus uevent() returned %d\n",
2271da177e4SLinus Torvalds 				  __FUNCTION__, retval);
2281da177e4SLinus Torvalds 	}
2291da177e4SLinus Torvalds 
2302620efefSGreg Kroah-Hartman 	if (dev->class && dev->class->dev_uevent) {
2312620efefSGreg Kroah-Hartman 		/* have the class specific function add its stuff */
2322620efefSGreg Kroah-Hartman 		retval = dev->class->dev_uevent(dev, envp, num_envp, buffer, buffer_size);
233f9f852dfSKay Sievers 		if (retval)
234f9f852dfSKay Sievers 			pr_debug("%s: class uevent() returned %d\n",
2352620efefSGreg Kroah-Hartman 				 __FUNCTION__, retval);
2362620efefSGreg Kroah-Hartman 	}
237f9f852dfSKay Sievers 
238f9f852dfSKay Sievers 	if (dev->type && dev->type->uevent) {
239f9f852dfSKay Sievers 		/* have the device type specific fuction add its stuff */
240f9f852dfSKay Sievers 		retval = dev->type->uevent(dev, envp, num_envp, buffer, buffer_size);
241f9f852dfSKay Sievers 		if (retval)
242f9f852dfSKay Sievers 			pr_debug("%s: dev_type uevent() returned %d\n",
243f9f852dfSKay Sievers 				 __FUNCTION__, retval);
2442620efefSGreg Kroah-Hartman 	}
2452620efefSGreg Kroah-Hartman 
2461da177e4SLinus Torvalds 	return retval;
2471da177e4SLinus Torvalds }
2481da177e4SLinus Torvalds 
249312c004dSKay Sievers static struct kset_uevent_ops device_uevent_ops = {
250312c004dSKay Sievers 	.filter =	dev_uevent_filter,
251312c004dSKay Sievers 	.name =		dev_uevent_name,
252312c004dSKay Sievers 	.uevent =	dev_uevent,
2531da177e4SLinus Torvalds };
2541da177e4SLinus Torvalds 
255a7fd6706SKay Sievers static ssize_t store_uevent(struct device *dev, struct device_attribute *attr,
256a7fd6706SKay Sievers 			    const char *buf, size_t count)
257a7fd6706SKay Sievers {
258312c004dSKay Sievers 	kobject_uevent(&dev->kobj, KOBJ_ADD);
259a7fd6706SKay Sievers 	return count;
260a7fd6706SKay Sievers }
261a7fd6706SKay Sievers 
262de0ff00dSGreg Kroah-Hartman static int device_add_groups(struct device *dev)
263de0ff00dSGreg Kroah-Hartman {
264de0ff00dSGreg Kroah-Hartman 	int i;
265de0ff00dSGreg Kroah-Hartman 	int error = 0;
266de0ff00dSGreg Kroah-Hartman 
267de0ff00dSGreg Kroah-Hartman 	if (dev->groups) {
268de0ff00dSGreg Kroah-Hartman 		for (i = 0; dev->groups[i]; i++) {
269de0ff00dSGreg Kroah-Hartman 			error = sysfs_create_group(&dev->kobj, dev->groups[i]);
270de0ff00dSGreg Kroah-Hartman 			if (error) {
271de0ff00dSGreg Kroah-Hartman 				while (--i >= 0)
272de0ff00dSGreg Kroah-Hartman 					sysfs_remove_group(&dev->kobj, dev->groups[i]);
273de0ff00dSGreg Kroah-Hartman 				goto out;
274de0ff00dSGreg Kroah-Hartman 			}
275de0ff00dSGreg Kroah-Hartman 		}
276de0ff00dSGreg Kroah-Hartman 	}
277de0ff00dSGreg Kroah-Hartman out:
278de0ff00dSGreg Kroah-Hartman 	return error;
279de0ff00dSGreg Kroah-Hartman }
280de0ff00dSGreg Kroah-Hartman 
281de0ff00dSGreg Kroah-Hartman static void device_remove_groups(struct device *dev)
282de0ff00dSGreg Kroah-Hartman {
283de0ff00dSGreg Kroah-Hartman 	int i;
284de0ff00dSGreg Kroah-Hartman 	if (dev->groups) {
285de0ff00dSGreg Kroah-Hartman 		for (i = 0; dev->groups[i]; i++) {
286de0ff00dSGreg Kroah-Hartman 			sysfs_remove_group(&dev->kobj, dev->groups[i]);
287de0ff00dSGreg Kroah-Hartman 		}
288de0ff00dSGreg Kroah-Hartman 	}
289de0ff00dSGreg Kroah-Hartman }
290de0ff00dSGreg Kroah-Hartman 
2912620efefSGreg Kroah-Hartman static int device_add_attrs(struct device *dev)
2922620efefSGreg Kroah-Hartman {
2932620efefSGreg Kroah-Hartman 	struct class *class = dev->class;
294f9f852dfSKay Sievers 	struct device_type *type = dev->type;
2952620efefSGreg Kroah-Hartman 	int error = 0;
2962620efefSGreg Kroah-Hartman 	int i;
2972620efefSGreg Kroah-Hartman 
298f9f852dfSKay Sievers 	if (class && class->dev_attrs) {
2992620efefSGreg Kroah-Hartman 		for (i = 0; attr_name(class->dev_attrs[i]); i++) {
3002620efefSGreg Kroah-Hartman 			error = device_create_file(dev, &class->dev_attrs[i]);
3012620efefSGreg Kroah-Hartman 			if (error)
3022620efefSGreg Kroah-Hartman 				break;
3032620efefSGreg Kroah-Hartman 		}
3042620efefSGreg Kroah-Hartman 		if (error)
3052620efefSGreg Kroah-Hartman 			while (--i >= 0)
3062620efefSGreg Kroah-Hartman 				device_remove_file(dev, &class->dev_attrs[i]);
307f9f852dfSKay Sievers 	}
308f9f852dfSKay Sievers 
309f9f852dfSKay Sievers 	if (type && type->attrs) {
310f9f852dfSKay Sievers 		for (i = 0; attr_name(type->attrs[i]); i++) {
311f9f852dfSKay Sievers 			error = device_create_file(dev, &type->attrs[i]);
312f9f852dfSKay Sievers 			if (error)
313f9f852dfSKay Sievers 				break;
314f9f852dfSKay Sievers 		}
315f9f852dfSKay Sievers 		if (error)
316f9f852dfSKay Sievers 			while (--i >= 0)
317f9f852dfSKay Sievers 				device_remove_file(dev, &type->attrs[i]);
318f9f852dfSKay Sievers 	}
319f9f852dfSKay Sievers 
3202620efefSGreg Kroah-Hartman 	return error;
3212620efefSGreg Kroah-Hartman }
3222620efefSGreg Kroah-Hartman 
3232620efefSGreg Kroah-Hartman static void device_remove_attrs(struct device *dev)
3242620efefSGreg Kroah-Hartman {
3252620efefSGreg Kroah-Hartman 	struct class *class = dev->class;
326f9f852dfSKay Sievers 	struct device_type *type = dev->type;
3272620efefSGreg Kroah-Hartman 	int i;
3282620efefSGreg Kroah-Hartman 
329f9f852dfSKay Sievers 	if (class && class->dev_attrs) {
3302620efefSGreg Kroah-Hartman 		for (i = 0; attr_name(class->dev_attrs[i]); i++)
3312620efefSGreg Kroah-Hartman 			device_remove_file(dev, &class->dev_attrs[i]);
3322620efefSGreg Kroah-Hartman 	}
333f9f852dfSKay Sievers 
334f9f852dfSKay Sievers 	if (type && type->attrs) {
335f9f852dfSKay Sievers 		for (i = 0; attr_name(type->attrs[i]); i++)
336f9f852dfSKay Sievers 			device_remove_file(dev, &type->attrs[i]);
337f9f852dfSKay Sievers 	}
3382620efefSGreg Kroah-Hartman }
3392620efefSGreg Kroah-Hartman 
3402620efefSGreg Kroah-Hartman 
34123681e47SGreg Kroah-Hartman static ssize_t show_dev(struct device *dev, struct device_attribute *attr,
34223681e47SGreg Kroah-Hartman 			char *buf)
34323681e47SGreg Kroah-Hartman {
34423681e47SGreg Kroah-Hartman 	return print_dev_t(buf, dev->devt);
34523681e47SGreg Kroah-Hartman }
34623681e47SGreg Kroah-Hartman 
3470863afb3SMartin Waitz /*
3480863afb3SMartin Waitz  *	devices_subsys - structure to be registered with kobject core.
3491da177e4SLinus Torvalds  */
3501da177e4SLinus Torvalds 
351312c004dSKay Sievers decl_subsys(devices, &ktype_device, &device_uevent_ops);
3521da177e4SLinus Torvalds 
3531da177e4SLinus Torvalds 
3541da177e4SLinus Torvalds /**
3551da177e4SLinus Torvalds  *	device_create_file - create sysfs attribute file for device.
3561da177e4SLinus Torvalds  *	@dev:	device.
3571da177e4SLinus Torvalds  *	@attr:	device attribute descriptor.
3581da177e4SLinus Torvalds  */
3591da177e4SLinus Torvalds 
3601da177e4SLinus Torvalds int device_create_file(struct device * dev, struct device_attribute * attr)
3611da177e4SLinus Torvalds {
3621da177e4SLinus Torvalds 	int error = 0;
3631da177e4SLinus Torvalds 	if (get_device(dev)) {
3641da177e4SLinus Torvalds 		error = sysfs_create_file(&dev->kobj, &attr->attr);
3651da177e4SLinus Torvalds 		put_device(dev);
3661da177e4SLinus Torvalds 	}
3671da177e4SLinus Torvalds 	return error;
3681da177e4SLinus Torvalds }
3691da177e4SLinus Torvalds 
3701da177e4SLinus Torvalds /**
3711da177e4SLinus Torvalds  *	device_remove_file - remove sysfs attribute file.
3721da177e4SLinus Torvalds  *	@dev:	device.
3731da177e4SLinus Torvalds  *	@attr:	device attribute descriptor.
3741da177e4SLinus Torvalds  */
3751da177e4SLinus Torvalds 
3761da177e4SLinus Torvalds void device_remove_file(struct device * dev, struct device_attribute * attr)
3771da177e4SLinus Torvalds {
3781da177e4SLinus Torvalds 	if (get_device(dev)) {
3791da177e4SLinus Torvalds 		sysfs_remove_file(&dev->kobj, &attr->attr);
3801da177e4SLinus Torvalds 		put_device(dev);
3811da177e4SLinus Torvalds 	}
3821da177e4SLinus Torvalds }
3831da177e4SLinus Torvalds 
3842589f188SGreg Kroah-Hartman /**
3852589f188SGreg Kroah-Hartman  * device_create_bin_file - create sysfs binary attribute file for device.
3862589f188SGreg Kroah-Hartman  * @dev: device.
3872589f188SGreg Kroah-Hartman  * @attr: device binary attribute descriptor.
3882589f188SGreg Kroah-Hartman  */
3892589f188SGreg Kroah-Hartman int device_create_bin_file(struct device *dev, struct bin_attribute *attr)
3902589f188SGreg Kroah-Hartman {
3912589f188SGreg Kroah-Hartman 	int error = -EINVAL;
3922589f188SGreg Kroah-Hartman 	if (dev)
3932589f188SGreg Kroah-Hartman 		error = sysfs_create_bin_file(&dev->kobj, attr);
3942589f188SGreg Kroah-Hartman 	return error;
3952589f188SGreg Kroah-Hartman }
3962589f188SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(device_create_bin_file);
3972589f188SGreg Kroah-Hartman 
3982589f188SGreg Kroah-Hartman /**
3992589f188SGreg Kroah-Hartman  * device_remove_bin_file - remove sysfs binary attribute file
4002589f188SGreg Kroah-Hartman  * @dev: device.
4012589f188SGreg Kroah-Hartman  * @attr: device binary attribute descriptor.
4022589f188SGreg Kroah-Hartman  */
4032589f188SGreg Kroah-Hartman void device_remove_bin_file(struct device *dev, struct bin_attribute *attr)
4042589f188SGreg Kroah-Hartman {
4052589f188SGreg Kroah-Hartman 	if (dev)
4062589f188SGreg Kroah-Hartman 		sysfs_remove_bin_file(&dev->kobj, attr);
4072589f188SGreg Kroah-Hartman }
4082589f188SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(device_remove_bin_file);
4092589f188SGreg Kroah-Hartman 
41034bb61f9SJames Bottomley static void klist_children_get(struct klist_node *n)
41134bb61f9SJames Bottomley {
41234bb61f9SJames Bottomley 	struct device *dev = container_of(n, struct device, knode_parent);
41334bb61f9SJames Bottomley 
41434bb61f9SJames Bottomley 	get_device(dev);
41534bb61f9SJames Bottomley }
41634bb61f9SJames Bottomley 
41734bb61f9SJames Bottomley static void klist_children_put(struct klist_node *n)
41834bb61f9SJames Bottomley {
41934bb61f9SJames Bottomley 	struct device *dev = container_of(n, struct device, knode_parent);
42034bb61f9SJames Bottomley 
42134bb61f9SJames Bottomley 	put_device(dev);
42234bb61f9SJames Bottomley }
42334bb61f9SJames Bottomley 
4241da177e4SLinus Torvalds 
4251da177e4SLinus Torvalds /**
4261da177e4SLinus Torvalds  *	device_initialize - init device structure.
4271da177e4SLinus Torvalds  *	@dev:	device.
4281da177e4SLinus Torvalds  *
4291da177e4SLinus Torvalds  *	This prepares the device for use by other layers,
4301da177e4SLinus Torvalds  *	including adding it to the device hierarchy.
4311da177e4SLinus Torvalds  *	It is the first half of device_register(), if called by
4321da177e4SLinus Torvalds  *	that, though it can also be called separately, so one
4331da177e4SLinus Torvalds  *	may use @dev's fields (e.g. the refcount).
4341da177e4SLinus Torvalds  */
4351da177e4SLinus Torvalds 
4361da177e4SLinus Torvalds void device_initialize(struct device *dev)
4371da177e4SLinus Torvalds {
4381da177e4SLinus Torvalds 	kobj_set_kset_s(dev, devices_subsys);
4391da177e4SLinus Torvalds 	kobject_init(&dev->kobj);
44034bb61f9SJames Bottomley 	klist_init(&dev->klist_children, klist_children_get,
44134bb61f9SJames Bottomley 		   klist_children_put);
4421da177e4SLinus Torvalds 	INIT_LIST_HEAD(&dev->dma_pools);
44323681e47SGreg Kroah-Hartman 	INIT_LIST_HEAD(&dev->node);
444af70316aSmochel@digitalimplant.org 	init_MUTEX(&dev->sem);
4459ac7849eSTejun Heo 	spin_lock_init(&dev->devres_lock);
4469ac7849eSTejun Heo 	INIT_LIST_HEAD(&dev->devres_head);
4470ac85241SDavid Brownell 	device_init_wakeup(dev, 0);
44887348136SChristoph Hellwig 	set_dev_node(dev, -1);
4491da177e4SLinus Torvalds }
4501da177e4SLinus Torvalds 
45140fa5422SGreg Kroah-Hartman #ifdef CONFIG_SYSFS_DEPRECATED
452c744aeaeSCornelia Huck static struct kobject * get_device_parent(struct device *dev,
453c744aeaeSCornelia Huck 					  struct device *parent)
45440fa5422SGreg Kroah-Hartman {
45540fa5422SGreg Kroah-Hartman 	/* Set the parent to the class, not the parent device */
45640fa5422SGreg Kroah-Hartman 	/* this keeps sysfs from having a symlink to make old udevs happy */
45740fa5422SGreg Kroah-Hartman 	if (dev->class)
458c744aeaeSCornelia Huck 		return &dev->class->subsys.kset.kobj;
45940fa5422SGreg Kroah-Hartman 	else if (parent)
460c744aeaeSCornelia Huck 		return &parent->kobj;
46140fa5422SGreg Kroah-Hartman 
462c744aeaeSCornelia Huck 	return NULL;
46340fa5422SGreg Kroah-Hartman }
46440fa5422SGreg Kroah-Hartman #else
465c744aeaeSCornelia Huck static struct kobject * virtual_device_parent(struct device *dev)
466f0ee61a6SGreg Kroah-Hartman {
467f0ee61a6SGreg Kroah-Hartman 	if (!dev->class)
468c744aeaeSCornelia Huck 		return ERR_PTR(-ENODEV);
469f0ee61a6SGreg Kroah-Hartman 
470f0ee61a6SGreg Kroah-Hartman 	if (!dev->class->virtual_dir) {
471f0ee61a6SGreg Kroah-Hartman 		static struct kobject *virtual_dir = NULL;
472f0ee61a6SGreg Kroah-Hartman 
473f0ee61a6SGreg Kroah-Hartman 		if (!virtual_dir)
474f0ee61a6SGreg Kroah-Hartman 			virtual_dir = kobject_add_dir(&devices_subsys.kset.kobj, "virtual");
475f0ee61a6SGreg Kroah-Hartman 		dev->class->virtual_dir = kobject_add_dir(virtual_dir, dev->class->name);
476f0ee61a6SGreg Kroah-Hartman 	}
477f0ee61a6SGreg Kroah-Hartman 
478c744aeaeSCornelia Huck 	return dev->class->virtual_dir;
479f0ee61a6SGreg Kroah-Hartman }
480f0ee61a6SGreg Kroah-Hartman 
481c744aeaeSCornelia Huck static struct kobject * get_device_parent(struct device *dev,
482c744aeaeSCornelia Huck 					  struct device *parent)
48340fa5422SGreg Kroah-Hartman {
48440fa5422SGreg Kroah-Hartman 	/* if this is a class device, and has no parent, create one */
48540fa5422SGreg Kroah-Hartman 	if ((dev->class) && (parent == NULL)) {
486c744aeaeSCornelia Huck 		return virtual_device_parent(dev);
48740fa5422SGreg Kroah-Hartman 	} else if (parent)
488c744aeaeSCornelia Huck 		return &parent->kobj;
489c744aeaeSCornelia Huck 	return NULL;
490c744aeaeSCornelia Huck }
49140fa5422SGreg Kroah-Hartman 
492c744aeaeSCornelia Huck #endif
493c744aeaeSCornelia Huck static int setup_parent(struct device *dev, struct device *parent)
494c744aeaeSCornelia Huck {
495c744aeaeSCornelia Huck 	struct kobject *kobj;
496c744aeaeSCornelia Huck 	kobj = get_device_parent(dev, parent);
497c744aeaeSCornelia Huck 	if (IS_ERR(kobj))
498c744aeaeSCornelia Huck 		return PTR_ERR(kobj);
499c744aeaeSCornelia Huck 	if (kobj)
500c744aeaeSCornelia Huck 		dev->kobj.parent = kobj;
50140fa5422SGreg Kroah-Hartman 	return 0;
50240fa5422SGreg Kroah-Hartman }
50340fa5422SGreg Kroah-Hartman 
5041da177e4SLinus Torvalds /**
5051da177e4SLinus Torvalds  *	device_add - add device to device hierarchy.
5061da177e4SLinus Torvalds  *	@dev:	device.
5071da177e4SLinus Torvalds  *
5081da177e4SLinus Torvalds  *	This is part 2 of device_register(), though may be called
5091da177e4SLinus Torvalds  *	separately _iff_ device_initialize() has been called separately.
5101da177e4SLinus Torvalds  *
5111da177e4SLinus Torvalds  *	This adds it to the kobject hierarchy via kobject_add(), adds it
5121da177e4SLinus Torvalds  *	to the global and sibling lists for the device, then
5131da177e4SLinus Torvalds  *	adds it to the other relevant subsystems of the driver model.
5141da177e4SLinus Torvalds  */
5151da177e4SLinus Torvalds int device_add(struct device *dev)
5161da177e4SLinus Torvalds {
5171da177e4SLinus Torvalds 	struct device *parent = NULL;
518e9a7d305SGreg Kroah-Hartman 	char *class_name = NULL;
519c47ed219SGreg Kroah-Hartman 	struct class_interface *class_intf;
5201da177e4SLinus Torvalds 	int error = -EINVAL;
5211da177e4SLinus Torvalds 
5221da177e4SLinus Torvalds 	dev = get_device(dev);
5231da177e4SLinus Torvalds 	if (!dev || !strlen(dev->bus_id))
5241da177e4SLinus Torvalds 		goto Error;
5251da177e4SLinus Torvalds 
52640fa5422SGreg Kroah-Hartman 	pr_debug("DEV: registering device: ID = '%s'\n", dev->bus_id);
527c205ef48SGreg Kroah-Hartman 
5281da177e4SLinus Torvalds 	parent = get_device(dev->parent);
5291da177e4SLinus Torvalds 
53040fa5422SGreg Kroah-Hartman 	error = setup_parent(dev, parent);
53140fa5422SGreg Kroah-Hartman 	if (error)
53240fa5422SGreg Kroah-Hartman 		goto Error;
5331da177e4SLinus Torvalds 
5341da177e4SLinus Torvalds 	/* first, register with generic layer. */
5351da177e4SLinus Torvalds 	kobject_set_name(&dev->kobj, "%s", dev->bus_id);
53640fa5422SGreg Kroah-Hartman 	error = kobject_add(&dev->kobj);
53740fa5422SGreg Kroah-Hartman 	if (error)
5381da177e4SLinus Torvalds 		goto Error;
539a7fd6706SKay Sievers 
54037022644SBrian Walsh 	/* notify platform of device entry */
54137022644SBrian Walsh 	if (platform_notify)
54237022644SBrian Walsh 		platform_notify(dev);
54337022644SBrian Walsh 
544116af378SBenjamin Herrenschmidt 	/* notify clients of device entry (new way) */
545116af378SBenjamin Herrenschmidt 	if (dev->bus)
546116af378SBenjamin Herrenschmidt 		blocking_notifier_call_chain(&dev->bus->bus_notifier,
547116af378SBenjamin Herrenschmidt 					     BUS_NOTIFY_ADD_DEVICE, dev);
548116af378SBenjamin Herrenschmidt 
549a7fd6706SKay Sievers 	dev->uevent_attr.attr.name = "uevent";
550a7fd6706SKay Sievers 	dev->uevent_attr.attr.mode = S_IWUSR;
551a7fd6706SKay Sievers 	if (dev->driver)
552a7fd6706SKay Sievers 		dev->uevent_attr.attr.owner = dev->driver->owner;
553a7fd6706SKay Sievers 	dev->uevent_attr.store = store_uevent;
554a306eea4SCornelia Huck 	error = device_create_file(dev, &dev->uevent_attr);
555a306eea4SCornelia Huck 	if (error)
556a306eea4SCornelia Huck 		goto attrError;
557a7fd6706SKay Sievers 
55823681e47SGreg Kroah-Hartman 	if (MAJOR(dev->devt)) {
55923681e47SGreg Kroah-Hartman 		struct device_attribute *attr;
56023681e47SGreg Kroah-Hartman 		attr = kzalloc(sizeof(*attr), GFP_KERNEL);
56123681e47SGreg Kroah-Hartman 		if (!attr) {
56223681e47SGreg Kroah-Hartman 			error = -ENOMEM;
563a306eea4SCornelia Huck 			goto ueventattrError;
56423681e47SGreg Kroah-Hartman 		}
56523681e47SGreg Kroah-Hartman 		attr->attr.name = "dev";
56623681e47SGreg Kroah-Hartman 		attr->attr.mode = S_IRUGO;
56723681e47SGreg Kroah-Hartman 		if (dev->driver)
56823681e47SGreg Kroah-Hartman 			attr->attr.owner = dev->driver->owner;
56923681e47SGreg Kroah-Hartman 		attr->show = show_dev;
57023681e47SGreg Kroah-Hartman 		error = device_create_file(dev, attr);
57123681e47SGreg Kroah-Hartman 		if (error) {
57223681e47SGreg Kroah-Hartman 			kfree(attr);
573a306eea4SCornelia Huck 			goto ueventattrError;
57423681e47SGreg Kroah-Hartman 		}
57523681e47SGreg Kroah-Hartman 
57623681e47SGreg Kroah-Hartman 		dev->devt_attr = attr;
57723681e47SGreg Kroah-Hartman 	}
57823681e47SGreg Kroah-Hartman 
579b9d9c82bSKay Sievers 	if (dev->class) {
580b9d9c82bSKay Sievers 		sysfs_create_link(&dev->kobj, &dev->class->subsys.kset.kobj,
581b9d9c82bSKay Sievers 				  "subsystem");
58240fa5422SGreg Kroah-Hartman 		/* If this is not a "fake" compatible device, then create the
58340fa5422SGreg Kroah-Hartman 		 * symlink from the class to the device. */
58440fa5422SGreg Kroah-Hartman 		if (dev->kobj.parent != &dev->class->subsys.kset.kobj)
58540fa5422SGreg Kroah-Hartman 			sysfs_create_link(&dev->class->subsys.kset.kobj,
58640fa5422SGreg Kroah-Hartman 					  &dev->kobj, dev->bus_id);
58764bb5d2cSGreg Kroah-Hartman 		if (parent) {
588cb360bbfSCornelia Huck 			sysfs_create_link(&dev->kobj, &dev->parent->kobj,
589cb360bbfSCornelia Huck 							"device");
590f7f3461dSGreg Kroah-Hartman #ifdef CONFIG_SYSFS_DEPRECATED
591cb360bbfSCornelia Huck 			class_name = make_class_name(dev->class->name,
592cb360bbfSCornelia Huck 							&dev->kobj);
593cb360bbfSCornelia Huck 			if (class_name)
594cb360bbfSCornelia Huck 				sysfs_create_link(&dev->parent->kobj,
595cb360bbfSCornelia Huck 						  &dev->kobj, class_name);
59699ef3ef8SKay Sievers #endif
59764bb5d2cSGreg Kroah-Hartman 		}
598f7f3461dSGreg Kroah-Hartman 	}
59923681e47SGreg Kroah-Hartman 
6002620efefSGreg Kroah-Hartman 	if ((error = device_add_attrs(dev)))
6012620efefSGreg Kroah-Hartman 		goto AttrsError;
602de0ff00dSGreg Kroah-Hartman 	if ((error = device_add_groups(dev)))
603de0ff00dSGreg Kroah-Hartman 		goto GroupError;
6041da177e4SLinus Torvalds 	if ((error = device_pm_add(dev)))
6051da177e4SLinus Torvalds 		goto PMError;
6061da177e4SLinus Torvalds 	if ((error = bus_add_device(dev)))
6071da177e4SLinus Torvalds 		goto BusError;
608b7a3e813SKay Sievers 	if (!dev->uevent_suppress)
60953877d06SKay Sievers 		kobject_uevent(&dev->kobj, KOBJ_ADD);
610f70fa629SAlan Stern 	if ((error = bus_attach_device(dev)))
611f70fa629SAlan Stern 		goto AttachError;
6121da177e4SLinus Torvalds 	if (parent)
613d856f1e3SJames Bottomley 		klist_add_tail(&dev->knode_parent, &parent->klist_children);
6141da177e4SLinus Torvalds 
6155d9fd169SGreg Kroah-Hartman 	if (dev->class) {
6165d9fd169SGreg Kroah-Hartman 		down(&dev->class->sem);
617c47ed219SGreg Kroah-Hartman 		/* tie the class to the device */
6185d9fd169SGreg Kroah-Hartman 		list_add_tail(&dev->node, &dev->class->devices);
619c47ed219SGreg Kroah-Hartman 
620c47ed219SGreg Kroah-Hartman 		/* notify any interfaces that the device is here */
621c47ed219SGreg Kroah-Hartman 		list_for_each_entry(class_intf, &dev->class->interfaces, node)
622c47ed219SGreg Kroah-Hartman 			if (class_intf->add_dev)
623c47ed219SGreg Kroah-Hartman 				class_intf->add_dev(dev, class_intf);
6245d9fd169SGreg Kroah-Hartman 		up(&dev->class->sem);
6255d9fd169SGreg Kroah-Hartman 	}
6261da177e4SLinus Torvalds  Done:
627e9a7d305SGreg Kroah-Hartman  	kfree(class_name);
6281da177e4SLinus Torvalds 	put_device(dev);
6291da177e4SLinus Torvalds 	return error;
630f70fa629SAlan Stern  AttachError:
631f70fa629SAlan Stern 	bus_remove_device(dev);
6321da177e4SLinus Torvalds  BusError:
6331da177e4SLinus Torvalds 	device_pm_remove(dev);
6341da177e4SLinus Torvalds  PMError:
635116af378SBenjamin Herrenschmidt 	if (dev->bus)
636116af378SBenjamin Herrenschmidt 		blocking_notifier_call_chain(&dev->bus->bus_notifier,
637116af378SBenjamin Herrenschmidt 					     BUS_NOTIFY_DEL_DEVICE, dev);
638de0ff00dSGreg Kroah-Hartman 	device_remove_groups(dev);
639de0ff00dSGreg Kroah-Hartman  GroupError:
6402620efefSGreg Kroah-Hartman 	device_remove_attrs(dev);
6412620efefSGreg Kroah-Hartman  AttrsError:
64223681e47SGreg Kroah-Hartman 	if (dev->devt_attr) {
64323681e47SGreg Kroah-Hartman 		device_remove_file(dev, dev->devt_attr);
64423681e47SGreg Kroah-Hartman 		kfree(dev->devt_attr);
64582f0cf9bSJames Simmons 	}
64682f0cf9bSJames Simmons 
64782f0cf9bSJames Simmons 	if (dev->class) {
64882f0cf9bSJames Simmons 		sysfs_remove_link(&dev->kobj, "subsystem");
64982f0cf9bSJames Simmons 		/* If this is not a "fake" compatible device, remove the
65082f0cf9bSJames Simmons 		 * symlink from the class to the device. */
65182f0cf9bSJames Simmons 		if (dev->kobj.parent != &dev->class->subsys.kset.kobj)
65282f0cf9bSJames Simmons 			sysfs_remove_link(&dev->class->subsys.kset.kobj,
65382f0cf9bSJames Simmons 					  dev->bus_id);
65482f0cf9bSJames Simmons 		if (parent) {
655f7f3461dSGreg Kroah-Hartman #ifdef CONFIG_SYSFS_DEPRECATED
65682f0cf9bSJames Simmons 			char *class_name = make_class_name(dev->class->name,
65782f0cf9bSJames Simmons 							   &dev->kobj);
65882f0cf9bSJames Simmons 			if (class_name)
65982f0cf9bSJames Simmons 				sysfs_remove_link(&dev->parent->kobj,
66082f0cf9bSJames Simmons 						  class_name);
66182f0cf9bSJames Simmons 			kfree(class_name);
662f7f3461dSGreg Kroah-Hartman #endif
66382f0cf9bSJames Simmons 			sysfs_remove_link(&dev->kobj, "device");
66482f0cf9bSJames Simmons 		}
66582f0cf9bSJames Simmons 
66682f0cf9bSJames Simmons 		down(&dev->class->sem);
66782f0cf9bSJames Simmons 		/* notify any interfaces that the device is now gone */
66882f0cf9bSJames Simmons 		list_for_each_entry(class_intf, &dev->class->interfaces, node)
66982f0cf9bSJames Simmons 			if (class_intf->remove_dev)
67082f0cf9bSJames Simmons 				class_intf->remove_dev(dev, class_intf);
67182f0cf9bSJames Simmons 		/* remove the device from the class list */
67282f0cf9bSJames Simmons 		list_del_init(&dev->node);
67382f0cf9bSJames Simmons 		up(&dev->class->sem);
67423681e47SGreg Kroah-Hartman 	}
675a306eea4SCornelia Huck  ueventattrError:
676a306eea4SCornelia Huck 	device_remove_file(dev, &dev->uevent_attr);
67723681e47SGreg Kroah-Hartman  attrError:
678312c004dSKay Sievers 	kobject_uevent(&dev->kobj, KOBJ_REMOVE);
6791da177e4SLinus Torvalds 	kobject_del(&dev->kobj);
6801da177e4SLinus Torvalds  Error:
6811da177e4SLinus Torvalds 	if (parent)
6821da177e4SLinus Torvalds 		put_device(parent);
6831da177e4SLinus Torvalds 	goto Done;
6841da177e4SLinus Torvalds }
6851da177e4SLinus Torvalds 
6861da177e4SLinus Torvalds 
6871da177e4SLinus Torvalds /**
6881da177e4SLinus Torvalds  *	device_register - register a device with the system.
6891da177e4SLinus Torvalds  *	@dev:	pointer to the device structure
6901da177e4SLinus Torvalds  *
6911da177e4SLinus Torvalds  *	This happens in two clean steps - initialize the device
6921da177e4SLinus Torvalds  *	and add it to the system. The two steps can be called
6931da177e4SLinus Torvalds  *	separately, but this is the easiest and most common.
6941da177e4SLinus Torvalds  *	I.e. you should only call the two helpers separately if
6951da177e4SLinus Torvalds  *	have a clearly defined need to use and refcount the device
6961da177e4SLinus Torvalds  *	before it is added to the hierarchy.
6971da177e4SLinus Torvalds  */
6981da177e4SLinus Torvalds 
6991da177e4SLinus Torvalds int device_register(struct device *dev)
7001da177e4SLinus Torvalds {
7011da177e4SLinus Torvalds 	device_initialize(dev);
7021da177e4SLinus Torvalds 	return device_add(dev);
7031da177e4SLinus Torvalds }
7041da177e4SLinus Torvalds 
7051da177e4SLinus Torvalds 
7061da177e4SLinus Torvalds /**
7071da177e4SLinus Torvalds  *	get_device - increment reference count for device.
7081da177e4SLinus Torvalds  *	@dev:	device.
7091da177e4SLinus Torvalds  *
7101da177e4SLinus Torvalds  *	This simply forwards the call to kobject_get(), though
7111da177e4SLinus Torvalds  *	we do take care to provide for the case that we get a NULL
7121da177e4SLinus Torvalds  *	pointer passed in.
7131da177e4SLinus Torvalds  */
7141da177e4SLinus Torvalds 
7151da177e4SLinus Torvalds struct device * get_device(struct device * dev)
7161da177e4SLinus Torvalds {
7171da177e4SLinus Torvalds 	return dev ? to_dev(kobject_get(&dev->kobj)) : NULL;
7181da177e4SLinus Torvalds }
7191da177e4SLinus Torvalds 
7201da177e4SLinus Torvalds 
7211da177e4SLinus Torvalds /**
7221da177e4SLinus Torvalds  *	put_device - decrement reference count.
7231da177e4SLinus Torvalds  *	@dev:	device in question.
7241da177e4SLinus Torvalds  */
7251da177e4SLinus Torvalds void put_device(struct device * dev)
7261da177e4SLinus Torvalds {
7271da177e4SLinus Torvalds 	if (dev)
7281da177e4SLinus Torvalds 		kobject_put(&dev->kobj);
7291da177e4SLinus Torvalds }
7301da177e4SLinus Torvalds 
7311da177e4SLinus Torvalds 
7321da177e4SLinus Torvalds /**
7331da177e4SLinus Torvalds  *	device_del - delete device from system.
7341da177e4SLinus Torvalds  *	@dev:	device.
7351da177e4SLinus Torvalds  *
7361da177e4SLinus Torvalds  *	This is the first part of the device unregistration
7371da177e4SLinus Torvalds  *	sequence. This removes the device from the lists we control
7381da177e4SLinus Torvalds  *	from here, has it removed from the other driver model
7391da177e4SLinus Torvalds  *	subsystems it was added to in device_add(), and removes it
7401da177e4SLinus Torvalds  *	from the kobject hierarchy.
7411da177e4SLinus Torvalds  *
7421da177e4SLinus Torvalds  *	NOTE: this should be called manually _iff_ device_add() was
7431da177e4SLinus Torvalds  *	also called manually.
7441da177e4SLinus Torvalds  */
7451da177e4SLinus Torvalds 
7461da177e4SLinus Torvalds void device_del(struct device * dev)
7471da177e4SLinus Torvalds {
7481da177e4SLinus Torvalds 	struct device * parent = dev->parent;
749c47ed219SGreg Kroah-Hartman 	struct class_interface *class_intf;
7501da177e4SLinus Torvalds 
7511da177e4SLinus Torvalds 	if (parent)
752d62c0f9fSPatrick Mochel 		klist_del(&dev->knode_parent);
75382189b98SCatalin Marinas 	if (dev->devt_attr) {
75423681e47SGreg Kroah-Hartman 		device_remove_file(dev, dev->devt_attr);
75582189b98SCatalin Marinas 		kfree(dev->devt_attr);
75682189b98SCatalin Marinas 	}
757b9d9c82bSKay Sievers 	if (dev->class) {
758b9d9c82bSKay Sievers 		sysfs_remove_link(&dev->kobj, "subsystem");
75940fa5422SGreg Kroah-Hartman 		/* If this is not a "fake" compatible device, remove the
76040fa5422SGreg Kroah-Hartman 		 * symlink from the class to the device. */
76140fa5422SGreg Kroah-Hartman 		if (dev->kobj.parent != &dev->class->subsys.kset.kobj)
76240fa5422SGreg Kroah-Hartman 			sysfs_remove_link(&dev->class->subsys.kset.kobj,
76340fa5422SGreg Kroah-Hartman 					  dev->bus_id);
76464bb5d2cSGreg Kroah-Hartman 		if (parent) {
765f7f3461dSGreg Kroah-Hartman #ifdef CONFIG_SYSFS_DEPRECATED
76699ef3ef8SKay Sievers 			char *class_name = make_class_name(dev->class->name,
76799ef3ef8SKay Sievers 							   &dev->kobj);
768cb360bbfSCornelia Huck 			if (class_name)
769cb360bbfSCornelia Huck 				sysfs_remove_link(&dev->parent->kobj,
770cb360bbfSCornelia Huck 						  class_name);
771e9a7d305SGreg Kroah-Hartman 			kfree(class_name);
772f7f3461dSGreg Kroah-Hartman #endif
77399ef3ef8SKay Sievers 			sysfs_remove_link(&dev->kobj, "device");
77499ef3ef8SKay Sievers 		}
77599ef3ef8SKay Sievers 
7765d9fd169SGreg Kroah-Hartman 		down(&dev->class->sem);
777c47ed219SGreg Kroah-Hartman 		/* notify any interfaces that the device is now gone */
778c47ed219SGreg Kroah-Hartman 		list_for_each_entry(class_intf, &dev->class->interfaces, node)
779c47ed219SGreg Kroah-Hartman 			if (class_intf->remove_dev)
780c47ed219SGreg Kroah-Hartman 				class_intf->remove_dev(dev, class_intf);
781c47ed219SGreg Kroah-Hartman 		/* remove the device from the class list */
7825d9fd169SGreg Kroah-Hartman 		list_del_init(&dev->node);
7835d9fd169SGreg Kroah-Hartman 		up(&dev->class->sem);
784b9d9c82bSKay Sievers 	}
785a7fd6706SKay Sievers 	device_remove_file(dev, &dev->uevent_attr);
786de0ff00dSGreg Kroah-Hartman 	device_remove_groups(dev);
7872620efefSGreg Kroah-Hartman 	device_remove_attrs(dev);
78828953533SBenjamin Herrenschmidt 	bus_remove_device(dev);
7891da177e4SLinus Torvalds 
7902f8d16a9STejun Heo 	/*
7912f8d16a9STejun Heo 	 * Some platform devices are driven without driver attached
7922f8d16a9STejun Heo 	 * and managed resources may have been acquired.  Make sure
7932f8d16a9STejun Heo 	 * all resources are released.
7942f8d16a9STejun Heo 	 */
7952f8d16a9STejun Heo 	devres_release_all(dev);
7962f8d16a9STejun Heo 
7971da177e4SLinus Torvalds 	/* Notify the platform of the removal, in case they
7981da177e4SLinus Torvalds 	 * need to do anything...
7991da177e4SLinus Torvalds 	 */
8001da177e4SLinus Torvalds 	if (platform_notify_remove)
8011da177e4SLinus Torvalds 		platform_notify_remove(dev);
802116af378SBenjamin Herrenschmidt 	if (dev->bus)
803116af378SBenjamin Herrenschmidt 		blocking_notifier_call_chain(&dev->bus->bus_notifier,
804116af378SBenjamin Herrenschmidt 					     BUS_NOTIFY_DEL_DEVICE, dev);
8051da177e4SLinus Torvalds 	device_pm_remove(dev);
806312c004dSKay Sievers 	kobject_uevent(&dev->kobj, KOBJ_REMOVE);
8071da177e4SLinus Torvalds 	kobject_del(&dev->kobj);
8081da177e4SLinus Torvalds 	if (parent)
8091da177e4SLinus Torvalds 		put_device(parent);
8101da177e4SLinus Torvalds }
8111da177e4SLinus Torvalds 
8121da177e4SLinus Torvalds /**
8131da177e4SLinus Torvalds  *	device_unregister - unregister device from system.
8141da177e4SLinus Torvalds  *	@dev:	device going away.
8151da177e4SLinus Torvalds  *
8161da177e4SLinus Torvalds  *	We do this in two parts, like we do device_register(). First,
8171da177e4SLinus Torvalds  *	we remove it from all the subsystems with device_del(), then
8181da177e4SLinus Torvalds  *	we decrement the reference count via put_device(). If that
8191da177e4SLinus Torvalds  *	is the final reference count, the device will be cleaned up
8201da177e4SLinus Torvalds  *	via device_release() above. Otherwise, the structure will
8211da177e4SLinus Torvalds  *	stick around until the final reference to the device is dropped.
8221da177e4SLinus Torvalds  */
8231da177e4SLinus Torvalds void device_unregister(struct device * dev)
8241da177e4SLinus Torvalds {
8251da177e4SLinus Torvalds 	pr_debug("DEV: Unregistering device. ID = '%s'\n", dev->bus_id);
8261da177e4SLinus Torvalds 	device_del(dev);
8271da177e4SLinus Torvalds 	put_device(dev);
8281da177e4SLinus Torvalds }
8291da177e4SLinus Torvalds 
8301da177e4SLinus Torvalds 
83136239577Smochel@digitalimplant.org static struct device * next_device(struct klist_iter * i)
83236239577Smochel@digitalimplant.org {
83336239577Smochel@digitalimplant.org 	struct klist_node * n = klist_next(i);
83436239577Smochel@digitalimplant.org 	return n ? container_of(n, struct device, knode_parent) : NULL;
83536239577Smochel@digitalimplant.org }
83636239577Smochel@digitalimplant.org 
8371da177e4SLinus Torvalds /**
8381da177e4SLinus Torvalds  *	device_for_each_child - device child iterator.
839c41455fbSRandy Dunlap  *	@parent: parent struct device.
8401da177e4SLinus Torvalds  *	@data:	data for the callback.
8411da177e4SLinus Torvalds  *	@fn:	function to be called for each device.
8421da177e4SLinus Torvalds  *
843c41455fbSRandy Dunlap  *	Iterate over @parent's child devices, and call @fn for each,
8441da177e4SLinus Torvalds  *	passing it @data.
8451da177e4SLinus Torvalds  *
8461da177e4SLinus Torvalds  *	We check the return of @fn each time. If it returns anything
8471da177e4SLinus Torvalds  *	other than 0, we break out and return that value.
8481da177e4SLinus Torvalds  */
84936239577Smochel@digitalimplant.org int device_for_each_child(struct device * parent, void * data,
8501da177e4SLinus Torvalds 		     int (*fn)(struct device *, void *))
8511da177e4SLinus Torvalds {
85236239577Smochel@digitalimplant.org 	struct klist_iter i;
8531da177e4SLinus Torvalds 	struct device * child;
8541da177e4SLinus Torvalds 	int error = 0;
8551da177e4SLinus Torvalds 
85636239577Smochel@digitalimplant.org 	klist_iter_init(&parent->klist_children, &i);
85736239577Smochel@digitalimplant.org 	while ((child = next_device(&i)) && !error)
85836239577Smochel@digitalimplant.org 		error = fn(child, data);
85936239577Smochel@digitalimplant.org 	klist_iter_exit(&i);
8601da177e4SLinus Torvalds 	return error;
8611da177e4SLinus Torvalds }
8621da177e4SLinus Torvalds 
8635ab69981SCornelia Huck /**
8645ab69981SCornelia Huck  * device_find_child - device iterator for locating a particular device.
8655ab69981SCornelia Huck  * @parent: parent struct device
8665ab69981SCornelia Huck  * @data: Data to pass to match function
8675ab69981SCornelia Huck  * @match: Callback function to check device
8685ab69981SCornelia Huck  *
8695ab69981SCornelia Huck  * This is similar to the device_for_each_child() function above, but it
8705ab69981SCornelia Huck  * returns a reference to a device that is 'found' for later use, as
8715ab69981SCornelia Huck  * determined by the @match callback.
8725ab69981SCornelia Huck  *
8735ab69981SCornelia Huck  * The callback should return 0 if the device doesn't match and non-zero
8745ab69981SCornelia Huck  * if it does.  If the callback returns non-zero and a reference to the
8755ab69981SCornelia Huck  * current device can be obtained, this function will return to the caller
8765ab69981SCornelia Huck  * and not iterate over any more devices.
8775ab69981SCornelia Huck  */
8785ab69981SCornelia Huck struct device * device_find_child(struct device *parent, void *data,
8795ab69981SCornelia Huck 				  int (*match)(struct device *, void *))
8805ab69981SCornelia Huck {
8815ab69981SCornelia Huck 	struct klist_iter i;
8825ab69981SCornelia Huck 	struct device *child;
8835ab69981SCornelia Huck 
8845ab69981SCornelia Huck 	if (!parent)
8855ab69981SCornelia Huck 		return NULL;
8865ab69981SCornelia Huck 
8875ab69981SCornelia Huck 	klist_iter_init(&parent->klist_children, &i);
8885ab69981SCornelia Huck 	while ((child = next_device(&i)))
8895ab69981SCornelia Huck 		if (match(child, data) && get_device(child))
8905ab69981SCornelia Huck 			break;
8915ab69981SCornelia Huck 	klist_iter_exit(&i);
8925ab69981SCornelia Huck 	return child;
8935ab69981SCornelia Huck }
8945ab69981SCornelia Huck 
8951da177e4SLinus Torvalds int __init devices_init(void)
8961da177e4SLinus Torvalds {
8971da177e4SLinus Torvalds 	return subsystem_register(&devices_subsys);
8981da177e4SLinus Torvalds }
8991da177e4SLinus Torvalds 
9001da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(device_for_each_child);
9015ab69981SCornelia Huck EXPORT_SYMBOL_GPL(device_find_child);
9021da177e4SLinus Torvalds 
9031da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(device_initialize);
9041da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(device_add);
9051da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(device_register);
9061da177e4SLinus Torvalds 
9071da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(device_del);
9081da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(device_unregister);
9091da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(get_device);
9101da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(put_device);
9111da177e4SLinus Torvalds 
9121da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(device_create_file);
9131da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(device_remove_file);
91423681e47SGreg Kroah-Hartman 
91523681e47SGreg Kroah-Hartman 
91623681e47SGreg Kroah-Hartman static void device_create_release(struct device *dev)
91723681e47SGreg Kroah-Hartman {
91823681e47SGreg Kroah-Hartman 	pr_debug("%s called for %s\n", __FUNCTION__, dev->bus_id);
91923681e47SGreg Kroah-Hartman 	kfree(dev);
92023681e47SGreg Kroah-Hartman }
92123681e47SGreg Kroah-Hartman 
92223681e47SGreg Kroah-Hartman /**
92323681e47SGreg Kroah-Hartman  * device_create - creates a device and registers it with sysfs
92442734dafSHenrik Kretzschmar  * @class: pointer to the struct class that this device should be registered to
92542734dafSHenrik Kretzschmar  * @parent: pointer to the parent struct device of this new device, if any
92642734dafSHenrik Kretzschmar  * @devt: the dev_t for the char device to be added
92742734dafSHenrik Kretzschmar  * @fmt: string for the device's name
92823681e47SGreg Kroah-Hartman  *
92942734dafSHenrik Kretzschmar  * This function can be used by char device classes.  A struct device
93042734dafSHenrik Kretzschmar  * will be created in sysfs, registered to the specified class.
93142734dafSHenrik Kretzschmar  *
93223681e47SGreg Kroah-Hartman  * A "dev" file will be created, showing the dev_t for the device, if
93323681e47SGreg Kroah-Hartman  * the dev_t is not 0,0.
93442734dafSHenrik Kretzschmar  * If a pointer to a parent struct device is passed in, the newly created
93542734dafSHenrik Kretzschmar  * struct device will be a child of that device in sysfs.
93642734dafSHenrik Kretzschmar  * The pointer to the struct device will be returned from the call.
93742734dafSHenrik Kretzschmar  * Any further sysfs files that might be required can be created using this
93823681e47SGreg Kroah-Hartman  * pointer.
93923681e47SGreg Kroah-Hartman  *
94023681e47SGreg Kroah-Hartman  * Note: the struct class passed to this function must have previously
94123681e47SGreg Kroah-Hartman  * been created with a call to class_create().
94223681e47SGreg Kroah-Hartman  */
94323681e47SGreg Kroah-Hartman struct device *device_create(struct class *class, struct device *parent,
9445cbe5f8aSGreg Kroah-Hartman 			     dev_t devt, const char *fmt, ...)
94523681e47SGreg Kroah-Hartman {
94623681e47SGreg Kroah-Hartman 	va_list args;
94723681e47SGreg Kroah-Hartman 	struct device *dev = NULL;
94823681e47SGreg Kroah-Hartman 	int retval = -ENODEV;
94923681e47SGreg Kroah-Hartman 
95023681e47SGreg Kroah-Hartman 	if (class == NULL || IS_ERR(class))
95123681e47SGreg Kroah-Hartman 		goto error;
95223681e47SGreg Kroah-Hartman 
95323681e47SGreg Kroah-Hartman 	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
95423681e47SGreg Kroah-Hartman 	if (!dev) {
95523681e47SGreg Kroah-Hartman 		retval = -ENOMEM;
95623681e47SGreg Kroah-Hartman 		goto error;
95723681e47SGreg Kroah-Hartman 	}
95823681e47SGreg Kroah-Hartman 
95923681e47SGreg Kroah-Hartman 	dev->devt = devt;
96023681e47SGreg Kroah-Hartman 	dev->class = class;
96123681e47SGreg Kroah-Hartman 	dev->parent = parent;
96223681e47SGreg Kroah-Hartman 	dev->release = device_create_release;
96323681e47SGreg Kroah-Hartman 
96423681e47SGreg Kroah-Hartman 	va_start(args, fmt);
96523681e47SGreg Kroah-Hartman 	vsnprintf(dev->bus_id, BUS_ID_SIZE, fmt, args);
96623681e47SGreg Kroah-Hartman 	va_end(args);
96723681e47SGreg Kroah-Hartman 	retval = device_register(dev);
96823681e47SGreg Kroah-Hartman 	if (retval)
96923681e47SGreg Kroah-Hartman 		goto error;
97023681e47SGreg Kroah-Hartman 
97123681e47SGreg Kroah-Hartman 	return dev;
97223681e47SGreg Kroah-Hartman 
97323681e47SGreg Kroah-Hartman error:
97423681e47SGreg Kroah-Hartman 	kfree(dev);
97523681e47SGreg Kroah-Hartman 	return ERR_PTR(retval);
97623681e47SGreg Kroah-Hartman }
97723681e47SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(device_create);
97823681e47SGreg Kroah-Hartman 
97923681e47SGreg Kroah-Hartman /**
98023681e47SGreg Kroah-Hartman  * device_destroy - removes a device that was created with device_create()
98142734dafSHenrik Kretzschmar  * @class: pointer to the struct class that this device was registered with
98242734dafSHenrik Kretzschmar  * @devt: the dev_t of the device that was previously registered
98323681e47SGreg Kroah-Hartman  *
98442734dafSHenrik Kretzschmar  * This call unregisters and cleans up a device that was created with a
98542734dafSHenrik Kretzschmar  * call to device_create().
98623681e47SGreg Kroah-Hartman  */
98723681e47SGreg Kroah-Hartman void device_destroy(struct class *class, dev_t devt)
98823681e47SGreg Kroah-Hartman {
98923681e47SGreg Kroah-Hartman 	struct device *dev = NULL;
99023681e47SGreg Kroah-Hartman 	struct device *dev_tmp;
99123681e47SGreg Kroah-Hartman 
99223681e47SGreg Kroah-Hartman 	down(&class->sem);
99323681e47SGreg Kroah-Hartman 	list_for_each_entry(dev_tmp, &class->devices, node) {
99423681e47SGreg Kroah-Hartman 		if (dev_tmp->devt == devt) {
99523681e47SGreg Kroah-Hartman 			dev = dev_tmp;
99623681e47SGreg Kroah-Hartman 			break;
99723681e47SGreg Kroah-Hartman 		}
99823681e47SGreg Kroah-Hartman 	}
99923681e47SGreg Kroah-Hartman 	up(&class->sem);
100023681e47SGreg Kroah-Hartman 
10015d9fd169SGreg Kroah-Hartman 	if (dev)
100223681e47SGreg Kroah-Hartman 		device_unregister(dev);
100323681e47SGreg Kroah-Hartman }
100423681e47SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(device_destroy);
1005a2de48caSGreg Kroah-Hartman 
1006a2de48caSGreg Kroah-Hartman /**
1007a2de48caSGreg Kroah-Hartman  * device_rename - renames a device
1008a2de48caSGreg Kroah-Hartman  * @dev: the pointer to the struct device to be renamed
1009a2de48caSGreg Kroah-Hartman  * @new_name: the new name of the device
1010a2de48caSGreg Kroah-Hartman  */
1011a2de48caSGreg Kroah-Hartman int device_rename(struct device *dev, char *new_name)
1012a2de48caSGreg Kroah-Hartman {
1013a2de48caSGreg Kroah-Hartman 	char *old_class_name = NULL;
1014a2de48caSGreg Kroah-Hartman 	char *new_class_name = NULL;
1015a2de48caSGreg Kroah-Hartman 	char *old_symlink_name = NULL;
1016a2de48caSGreg Kroah-Hartman 	int error;
1017a2de48caSGreg Kroah-Hartman 
1018a2de48caSGreg Kroah-Hartman 	dev = get_device(dev);
1019a2de48caSGreg Kroah-Hartman 	if (!dev)
1020a2de48caSGreg Kroah-Hartman 		return -EINVAL;
1021a2de48caSGreg Kroah-Hartman 
1022a2de48caSGreg Kroah-Hartman 	pr_debug("DEVICE: renaming '%s' to '%s'\n", dev->bus_id, new_name);
1023a2de48caSGreg Kroah-Hartman 
102499ef3ef8SKay Sievers #ifdef CONFIG_SYSFS_DEPRECATED
1025a2de48caSGreg Kroah-Hartman 	if ((dev->class) && (dev->parent))
1026a2de48caSGreg Kroah-Hartman 		old_class_name = make_class_name(dev->class->name, &dev->kobj);
102799ef3ef8SKay Sievers #endif
1028a2de48caSGreg Kroah-Hartman 
1029a2de48caSGreg Kroah-Hartman 	if (dev->class) {
1030a2de48caSGreg Kroah-Hartman 		old_symlink_name = kmalloc(BUS_ID_SIZE, GFP_KERNEL);
1031952ab431SJesper Juhl 		if (!old_symlink_name) {
1032952ab431SJesper Juhl 			error = -ENOMEM;
1033952ab431SJesper Juhl 			goto out_free_old_class;
1034952ab431SJesper Juhl 		}
1035a2de48caSGreg Kroah-Hartman 		strlcpy(old_symlink_name, dev->bus_id, BUS_ID_SIZE);
1036a2de48caSGreg Kroah-Hartman 	}
1037a2de48caSGreg Kroah-Hartman 
1038a2de48caSGreg Kroah-Hartman 	strlcpy(dev->bus_id, new_name, BUS_ID_SIZE);
1039a2de48caSGreg Kroah-Hartman 
1040a2de48caSGreg Kroah-Hartman 	error = kobject_rename(&dev->kobj, new_name);
1041a2de48caSGreg Kroah-Hartman 
104299ef3ef8SKay Sievers #ifdef CONFIG_SYSFS_DEPRECATED
1043a2de48caSGreg Kroah-Hartman 	if (old_class_name) {
1044a2de48caSGreg Kroah-Hartman 		new_class_name = make_class_name(dev->class->name, &dev->kobj);
1045a2de48caSGreg Kroah-Hartman 		if (new_class_name) {
1046a2de48caSGreg Kroah-Hartman 			sysfs_create_link(&dev->parent->kobj, &dev->kobj,
1047a2de48caSGreg Kroah-Hartman 					  new_class_name);
1048a2de48caSGreg Kroah-Hartman 			sysfs_remove_link(&dev->parent->kobj, old_class_name);
1049a2de48caSGreg Kroah-Hartman 		}
1050a2de48caSGreg Kroah-Hartman 	}
105199ef3ef8SKay Sievers #endif
105299ef3ef8SKay Sievers 
1053a2de48caSGreg Kroah-Hartman 	if (dev->class) {
1054a2de48caSGreg Kroah-Hartman 		sysfs_remove_link(&dev->class->subsys.kset.kobj,
1055a2de48caSGreg Kroah-Hartman 				  old_symlink_name);
1056a2de48caSGreg Kroah-Hartman 		sysfs_create_link(&dev->class->subsys.kset.kobj, &dev->kobj,
1057a2de48caSGreg Kroah-Hartman 				  dev->bus_id);
1058a2de48caSGreg Kroah-Hartman 	}
1059a2de48caSGreg Kroah-Hartman 	put_device(dev);
1060a2de48caSGreg Kroah-Hartman 
1061a2de48caSGreg Kroah-Hartman 	kfree(new_class_name);
1062a2de48caSGreg Kroah-Hartman 	kfree(old_symlink_name);
1063952ab431SJesper Juhl  out_free_old_class:
1064952ab431SJesper Juhl 	kfree(old_class_name);
1065a2de48caSGreg Kroah-Hartman 
1066a2de48caSGreg Kroah-Hartman 	return error;
1067a2de48caSGreg Kroah-Hartman }
1068a2807dbcSJohannes Berg EXPORT_SYMBOL_GPL(device_rename);
10698a82472fSCornelia Huck 
10708a82472fSCornelia Huck static int device_move_class_links(struct device *dev,
10718a82472fSCornelia Huck 				   struct device *old_parent,
10728a82472fSCornelia Huck 				   struct device *new_parent)
10738a82472fSCornelia Huck {
1074f7f3461dSGreg Kroah-Hartman 	int error = 0;
10758a82472fSCornelia Huck #ifdef CONFIG_SYSFS_DEPRECATED
10768a82472fSCornelia Huck 	char *class_name;
10778a82472fSCornelia Huck 
10788a82472fSCornelia Huck 	class_name = make_class_name(dev->class->name, &dev->kobj);
10798a82472fSCornelia Huck 	if (!class_name) {
1080cb360bbfSCornelia Huck 		error = -ENOMEM;
10818a82472fSCornelia Huck 		goto out;
10828a82472fSCornelia Huck 	}
10838a82472fSCornelia Huck 	if (old_parent) {
10848a82472fSCornelia Huck 		sysfs_remove_link(&dev->kobj, "device");
10858a82472fSCornelia Huck 		sysfs_remove_link(&old_parent->kobj, class_name);
10868a82472fSCornelia Huck 	}
1087c744aeaeSCornelia Huck 	if (new_parent) {
1088c744aeaeSCornelia Huck 		error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
1089c744aeaeSCornelia Huck 					  "device");
10908a82472fSCornelia Huck 		if (error)
10918a82472fSCornelia Huck 			goto out;
1092c744aeaeSCornelia Huck 		error = sysfs_create_link(&new_parent->kobj, &dev->kobj,
1093c744aeaeSCornelia Huck 					  class_name);
10948a82472fSCornelia Huck 		if (error)
10958a82472fSCornelia Huck 			sysfs_remove_link(&dev->kobj, "device");
1096c744aeaeSCornelia Huck 	}
1097c744aeaeSCornelia Huck 	else
1098c744aeaeSCornelia Huck 		error = 0;
10998a82472fSCornelia Huck out:
11008a82472fSCornelia Huck 	kfree(class_name);
11018a82472fSCornelia Huck 	return error;
11028a82472fSCornelia Huck #else
1103f7f3461dSGreg Kroah-Hartman 	if (old_parent)
1104f7f3461dSGreg Kroah-Hartman 		sysfs_remove_link(&dev->kobj, "device");
1105f7f3461dSGreg Kroah-Hartman 	if (new_parent)
1106f7f3461dSGreg Kroah-Hartman 		error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
1107f7f3461dSGreg Kroah-Hartman 					  "device");
1108f7f3461dSGreg Kroah-Hartman 	return error;
11098a82472fSCornelia Huck #endif
11108a82472fSCornelia Huck }
11118a82472fSCornelia Huck 
11128a82472fSCornelia Huck /**
11138a82472fSCornelia Huck  * device_move - moves a device to a new parent
11148a82472fSCornelia Huck  * @dev: the pointer to the struct device to be moved
1115c744aeaeSCornelia Huck  * @new_parent: the new parent of the device (can by NULL)
11168a82472fSCornelia Huck  */
11178a82472fSCornelia Huck int device_move(struct device *dev, struct device *new_parent)
11188a82472fSCornelia Huck {
11198a82472fSCornelia Huck 	int error;
11208a82472fSCornelia Huck 	struct device *old_parent;
1121c744aeaeSCornelia Huck 	struct kobject *new_parent_kobj;
11228a82472fSCornelia Huck 
11238a82472fSCornelia Huck 	dev = get_device(dev);
11248a82472fSCornelia Huck 	if (!dev)
11258a82472fSCornelia Huck 		return -EINVAL;
11268a82472fSCornelia Huck 
11278a82472fSCornelia Huck 	new_parent = get_device(new_parent);
1128c744aeaeSCornelia Huck 	new_parent_kobj = get_device_parent (dev, new_parent);
1129c744aeaeSCornelia Huck 	if (IS_ERR(new_parent_kobj)) {
1130c744aeaeSCornelia Huck 		error = PTR_ERR(new_parent_kobj);
1131c744aeaeSCornelia Huck 		put_device(new_parent);
11328a82472fSCornelia Huck 		goto out;
11338a82472fSCornelia Huck 	}
11348a82472fSCornelia Huck 	pr_debug("DEVICE: moving '%s' to '%s'\n", dev->bus_id,
1135c744aeaeSCornelia Huck 		 new_parent ? new_parent->bus_id : "<NULL>");
1136c744aeaeSCornelia Huck 	error = kobject_move(&dev->kobj, new_parent_kobj);
11378a82472fSCornelia Huck 	if (error) {
11388a82472fSCornelia Huck 		put_device(new_parent);
11398a82472fSCornelia Huck 		goto out;
11408a82472fSCornelia Huck 	}
11418a82472fSCornelia Huck 	old_parent = dev->parent;
11428a82472fSCornelia Huck 	dev->parent = new_parent;
11438a82472fSCornelia Huck 	if (old_parent)
1144acf02d23SCornelia Huck 		klist_remove(&dev->knode_parent);
1145c744aeaeSCornelia Huck 	if (new_parent)
11468a82472fSCornelia Huck 		klist_add_tail(&dev->knode_parent, &new_parent->klist_children);
11478a82472fSCornelia Huck 	if (!dev->class)
11488a82472fSCornelia Huck 		goto out_put;
11498a82472fSCornelia Huck 	error = device_move_class_links(dev, old_parent, new_parent);
11508a82472fSCornelia Huck 	if (error) {
11518a82472fSCornelia Huck 		/* We ignore errors on cleanup since we're hosed anyway... */
11528a82472fSCornelia Huck 		device_move_class_links(dev, new_parent, old_parent);
11538a82472fSCornelia Huck 		if (!kobject_move(&dev->kobj, &old_parent->kobj)) {
1154c744aeaeSCornelia Huck 			if (new_parent)
1155acf02d23SCornelia Huck 				klist_remove(&dev->knode_parent);
11568a82472fSCornelia Huck 			if (old_parent)
11578a82472fSCornelia Huck 				klist_add_tail(&dev->knode_parent,
11588a82472fSCornelia Huck 					       &old_parent->klist_children);
11598a82472fSCornelia Huck 		}
11608a82472fSCornelia Huck 		put_device(new_parent);
11618a82472fSCornelia Huck 		goto out;
11628a82472fSCornelia Huck 	}
11638a82472fSCornelia Huck out_put:
11648a82472fSCornelia Huck 	put_device(old_parent);
11658a82472fSCornelia Huck out:
11668a82472fSCornelia Huck 	put_device(dev);
11678a82472fSCornelia Huck 	return error;
11688a82472fSCornelia Huck }
11698a82472fSCornelia Huck 
11708a82472fSCornelia Huck EXPORT_SYMBOL_GPL(device_move);
1171