xref: /openbmc/linux/drivers/base/core.c (revision b446b60e)
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);
58799ef3ef8SKay Sievers #ifdef CONFIG_SYSFS_DEPRECATED
58864bb5d2cSGreg Kroah-Hartman 		if (parent) {
589cb360bbfSCornelia Huck 			sysfs_create_link(&dev->kobj, &dev->parent->kobj,
590cb360bbfSCornelia Huck 							"device");
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);
596b9d9c82bSKay Sievers 		}
59799ef3ef8SKay Sievers #endif
59864bb5d2cSGreg 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);
64523681e47SGreg Kroah-Hartman 	}
646a306eea4SCornelia Huck  ueventattrError:
647a306eea4SCornelia Huck 	device_remove_file(dev, &dev->uevent_attr);
64823681e47SGreg Kroah-Hartman  attrError:
649312c004dSKay Sievers 	kobject_uevent(&dev->kobj, KOBJ_REMOVE);
6501da177e4SLinus Torvalds 	kobject_del(&dev->kobj);
6511da177e4SLinus Torvalds  Error:
6521da177e4SLinus Torvalds 	if (parent)
6531da177e4SLinus Torvalds 		put_device(parent);
6541da177e4SLinus Torvalds 	goto Done;
6551da177e4SLinus Torvalds }
6561da177e4SLinus Torvalds 
6571da177e4SLinus Torvalds 
6581da177e4SLinus Torvalds /**
6591da177e4SLinus Torvalds  *	device_register - register a device with the system.
6601da177e4SLinus Torvalds  *	@dev:	pointer to the device structure
6611da177e4SLinus Torvalds  *
6621da177e4SLinus Torvalds  *	This happens in two clean steps - initialize the device
6631da177e4SLinus Torvalds  *	and add it to the system. The two steps can be called
6641da177e4SLinus Torvalds  *	separately, but this is the easiest and most common.
6651da177e4SLinus Torvalds  *	I.e. you should only call the two helpers separately if
6661da177e4SLinus Torvalds  *	have a clearly defined need to use and refcount the device
6671da177e4SLinus Torvalds  *	before it is added to the hierarchy.
6681da177e4SLinus Torvalds  */
6691da177e4SLinus Torvalds 
6701da177e4SLinus Torvalds int device_register(struct device *dev)
6711da177e4SLinus Torvalds {
6721da177e4SLinus Torvalds 	device_initialize(dev);
6731da177e4SLinus Torvalds 	return device_add(dev);
6741da177e4SLinus Torvalds }
6751da177e4SLinus Torvalds 
6761da177e4SLinus Torvalds 
6771da177e4SLinus Torvalds /**
6781da177e4SLinus Torvalds  *	get_device - increment reference count for device.
6791da177e4SLinus Torvalds  *	@dev:	device.
6801da177e4SLinus Torvalds  *
6811da177e4SLinus Torvalds  *	This simply forwards the call to kobject_get(), though
6821da177e4SLinus Torvalds  *	we do take care to provide for the case that we get a NULL
6831da177e4SLinus Torvalds  *	pointer passed in.
6841da177e4SLinus Torvalds  */
6851da177e4SLinus Torvalds 
6861da177e4SLinus Torvalds struct device * get_device(struct device * dev)
6871da177e4SLinus Torvalds {
6881da177e4SLinus Torvalds 	return dev ? to_dev(kobject_get(&dev->kobj)) : NULL;
6891da177e4SLinus Torvalds }
6901da177e4SLinus Torvalds 
6911da177e4SLinus Torvalds 
6921da177e4SLinus Torvalds /**
6931da177e4SLinus Torvalds  *	put_device - decrement reference count.
6941da177e4SLinus Torvalds  *	@dev:	device in question.
6951da177e4SLinus Torvalds  */
6961da177e4SLinus Torvalds void put_device(struct device * dev)
6971da177e4SLinus Torvalds {
6981da177e4SLinus Torvalds 	if (dev)
6991da177e4SLinus Torvalds 		kobject_put(&dev->kobj);
7001da177e4SLinus Torvalds }
7011da177e4SLinus Torvalds 
7021da177e4SLinus Torvalds 
7031da177e4SLinus Torvalds /**
7041da177e4SLinus Torvalds  *	device_del - delete device from system.
7051da177e4SLinus Torvalds  *	@dev:	device.
7061da177e4SLinus Torvalds  *
7071da177e4SLinus Torvalds  *	This is the first part of the device unregistration
7081da177e4SLinus Torvalds  *	sequence. This removes the device from the lists we control
7091da177e4SLinus Torvalds  *	from here, has it removed from the other driver model
7101da177e4SLinus Torvalds  *	subsystems it was added to in device_add(), and removes it
7111da177e4SLinus Torvalds  *	from the kobject hierarchy.
7121da177e4SLinus Torvalds  *
7131da177e4SLinus Torvalds  *	NOTE: this should be called manually _iff_ device_add() was
7141da177e4SLinus Torvalds  *	also called manually.
7151da177e4SLinus Torvalds  */
7161da177e4SLinus Torvalds 
7171da177e4SLinus Torvalds void device_del(struct device * dev)
7181da177e4SLinus Torvalds {
7191da177e4SLinus Torvalds 	struct device * parent = dev->parent;
720c47ed219SGreg Kroah-Hartman 	struct class_interface *class_intf;
7211da177e4SLinus Torvalds 
7221da177e4SLinus Torvalds 	if (parent)
723d62c0f9fSPatrick Mochel 		klist_del(&dev->knode_parent);
72482189b98SCatalin Marinas 	if (dev->devt_attr) {
72523681e47SGreg Kroah-Hartman 		device_remove_file(dev, dev->devt_attr);
72682189b98SCatalin Marinas 		kfree(dev->devt_attr);
72782189b98SCatalin Marinas 	}
728b9d9c82bSKay Sievers 	if (dev->class) {
729b9d9c82bSKay Sievers 		sysfs_remove_link(&dev->kobj, "subsystem");
73040fa5422SGreg Kroah-Hartman 		/* If this is not a "fake" compatible device, remove the
73140fa5422SGreg Kroah-Hartman 		 * symlink from the class to the device. */
73240fa5422SGreg Kroah-Hartman 		if (dev->kobj.parent != &dev->class->subsys.kset.kobj)
73340fa5422SGreg Kroah-Hartman 			sysfs_remove_link(&dev->class->subsys.kset.kobj,
73440fa5422SGreg Kroah-Hartman 					  dev->bus_id);
73599ef3ef8SKay Sievers #ifdef CONFIG_SYSFS_DEPRECATED
73664bb5d2cSGreg Kroah-Hartman 		if (parent) {
73799ef3ef8SKay Sievers 			char *class_name = make_class_name(dev->class->name,
73899ef3ef8SKay Sievers 							   &dev->kobj);
739cb360bbfSCornelia Huck 			if (class_name)
740cb360bbfSCornelia Huck 				sysfs_remove_link(&dev->parent->kobj,
741cb360bbfSCornelia Huck 						  class_name);
742e9a7d305SGreg Kroah-Hartman 			kfree(class_name);
74399ef3ef8SKay Sievers 			sysfs_remove_link(&dev->kobj, "device");
74499ef3ef8SKay Sievers 		}
74599ef3ef8SKay Sievers #endif
74699ef3ef8SKay Sievers 
7475d9fd169SGreg Kroah-Hartman 		down(&dev->class->sem);
748c47ed219SGreg Kroah-Hartman 		/* notify any interfaces that the device is now gone */
749c47ed219SGreg Kroah-Hartman 		list_for_each_entry(class_intf, &dev->class->interfaces, node)
750c47ed219SGreg Kroah-Hartman 			if (class_intf->remove_dev)
751c47ed219SGreg Kroah-Hartman 				class_intf->remove_dev(dev, class_intf);
752c47ed219SGreg Kroah-Hartman 		/* remove the device from the class list */
7535d9fd169SGreg Kroah-Hartman 		list_del_init(&dev->node);
7545d9fd169SGreg Kroah-Hartman 		up(&dev->class->sem);
755b9d9c82bSKay Sievers 	}
756a7fd6706SKay Sievers 	device_remove_file(dev, &dev->uevent_attr);
757de0ff00dSGreg Kroah-Hartman 	device_remove_groups(dev);
7582620efefSGreg Kroah-Hartman 	device_remove_attrs(dev);
75928953533SBenjamin Herrenschmidt 	bus_remove_device(dev);
7601da177e4SLinus Torvalds 
7611da177e4SLinus Torvalds 	/* Notify the platform of the removal, in case they
7621da177e4SLinus Torvalds 	 * need to do anything...
7631da177e4SLinus Torvalds 	 */
7641da177e4SLinus Torvalds 	if (platform_notify_remove)
7651da177e4SLinus Torvalds 		platform_notify_remove(dev);
766116af378SBenjamin Herrenschmidt 	if (dev->bus)
767116af378SBenjamin Herrenschmidt 		blocking_notifier_call_chain(&dev->bus->bus_notifier,
768116af378SBenjamin Herrenschmidt 					     BUS_NOTIFY_DEL_DEVICE, dev);
7691da177e4SLinus Torvalds 	device_pm_remove(dev);
770312c004dSKay Sievers 	kobject_uevent(&dev->kobj, KOBJ_REMOVE);
7711da177e4SLinus Torvalds 	kobject_del(&dev->kobj);
7721da177e4SLinus Torvalds 	if (parent)
7731da177e4SLinus Torvalds 		put_device(parent);
7741da177e4SLinus Torvalds }
7751da177e4SLinus Torvalds 
7761da177e4SLinus Torvalds /**
7771da177e4SLinus Torvalds  *	device_unregister - unregister device from system.
7781da177e4SLinus Torvalds  *	@dev:	device going away.
7791da177e4SLinus Torvalds  *
7801da177e4SLinus Torvalds  *	We do this in two parts, like we do device_register(). First,
7811da177e4SLinus Torvalds  *	we remove it from all the subsystems with device_del(), then
7821da177e4SLinus Torvalds  *	we decrement the reference count via put_device(). If that
7831da177e4SLinus Torvalds  *	is the final reference count, the device will be cleaned up
7841da177e4SLinus Torvalds  *	via device_release() above. Otherwise, the structure will
7851da177e4SLinus Torvalds  *	stick around until the final reference to the device is dropped.
7861da177e4SLinus Torvalds  */
7871da177e4SLinus Torvalds void device_unregister(struct device * dev)
7881da177e4SLinus Torvalds {
7891da177e4SLinus Torvalds 	pr_debug("DEV: Unregistering device. ID = '%s'\n", dev->bus_id);
7901da177e4SLinus Torvalds 	device_del(dev);
7911da177e4SLinus Torvalds 	put_device(dev);
7921da177e4SLinus Torvalds }
7931da177e4SLinus Torvalds 
7941da177e4SLinus Torvalds 
79536239577Smochel@digitalimplant.org static struct device * next_device(struct klist_iter * i)
79636239577Smochel@digitalimplant.org {
79736239577Smochel@digitalimplant.org 	struct klist_node * n = klist_next(i);
79836239577Smochel@digitalimplant.org 	return n ? container_of(n, struct device, knode_parent) : NULL;
79936239577Smochel@digitalimplant.org }
80036239577Smochel@digitalimplant.org 
8011da177e4SLinus Torvalds /**
8021da177e4SLinus Torvalds  *	device_for_each_child - device child iterator.
803c41455fbSRandy Dunlap  *	@parent: parent struct device.
8041da177e4SLinus Torvalds  *	@data:	data for the callback.
8051da177e4SLinus Torvalds  *	@fn:	function to be called for each device.
8061da177e4SLinus Torvalds  *
807c41455fbSRandy Dunlap  *	Iterate over @parent's child devices, and call @fn for each,
8081da177e4SLinus Torvalds  *	passing it @data.
8091da177e4SLinus Torvalds  *
8101da177e4SLinus Torvalds  *	We check the return of @fn each time. If it returns anything
8111da177e4SLinus Torvalds  *	other than 0, we break out and return that value.
8121da177e4SLinus Torvalds  */
81336239577Smochel@digitalimplant.org int device_for_each_child(struct device * parent, void * data,
8141da177e4SLinus Torvalds 		     int (*fn)(struct device *, void *))
8151da177e4SLinus Torvalds {
81636239577Smochel@digitalimplant.org 	struct klist_iter i;
8171da177e4SLinus Torvalds 	struct device * child;
8181da177e4SLinus Torvalds 	int error = 0;
8191da177e4SLinus Torvalds 
82036239577Smochel@digitalimplant.org 	klist_iter_init(&parent->klist_children, &i);
82136239577Smochel@digitalimplant.org 	while ((child = next_device(&i)) && !error)
82236239577Smochel@digitalimplant.org 		error = fn(child, data);
82336239577Smochel@digitalimplant.org 	klist_iter_exit(&i);
8241da177e4SLinus Torvalds 	return error;
8251da177e4SLinus Torvalds }
8261da177e4SLinus Torvalds 
8275ab69981SCornelia Huck /**
8285ab69981SCornelia Huck  * device_find_child - device iterator for locating a particular device.
8295ab69981SCornelia Huck  * @parent: parent struct device
8305ab69981SCornelia Huck  * @data: Data to pass to match function
8315ab69981SCornelia Huck  * @match: Callback function to check device
8325ab69981SCornelia Huck  *
8335ab69981SCornelia Huck  * This is similar to the device_for_each_child() function above, but it
8345ab69981SCornelia Huck  * returns a reference to a device that is 'found' for later use, as
8355ab69981SCornelia Huck  * determined by the @match callback.
8365ab69981SCornelia Huck  *
8375ab69981SCornelia Huck  * The callback should return 0 if the device doesn't match and non-zero
8385ab69981SCornelia Huck  * if it does.  If the callback returns non-zero and a reference to the
8395ab69981SCornelia Huck  * current device can be obtained, this function will return to the caller
8405ab69981SCornelia Huck  * and not iterate over any more devices.
8415ab69981SCornelia Huck  */
8425ab69981SCornelia Huck struct device * device_find_child(struct device *parent, void *data,
8435ab69981SCornelia Huck 				  int (*match)(struct device *, void *))
8445ab69981SCornelia Huck {
8455ab69981SCornelia Huck 	struct klist_iter i;
8465ab69981SCornelia Huck 	struct device *child;
8475ab69981SCornelia Huck 
8485ab69981SCornelia Huck 	if (!parent)
8495ab69981SCornelia Huck 		return NULL;
8505ab69981SCornelia Huck 
8515ab69981SCornelia Huck 	klist_iter_init(&parent->klist_children, &i);
8525ab69981SCornelia Huck 	while ((child = next_device(&i)))
8535ab69981SCornelia Huck 		if (match(child, data) && get_device(child))
8545ab69981SCornelia Huck 			break;
8555ab69981SCornelia Huck 	klist_iter_exit(&i);
8565ab69981SCornelia Huck 	return child;
8575ab69981SCornelia Huck }
8585ab69981SCornelia Huck 
8591da177e4SLinus Torvalds int __init devices_init(void)
8601da177e4SLinus Torvalds {
8611da177e4SLinus Torvalds 	return subsystem_register(&devices_subsys);
8621da177e4SLinus Torvalds }
8631da177e4SLinus Torvalds 
8641da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(device_for_each_child);
8655ab69981SCornelia Huck EXPORT_SYMBOL_GPL(device_find_child);
8661da177e4SLinus Torvalds 
8671da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(device_initialize);
8681da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(device_add);
8691da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(device_register);
8701da177e4SLinus Torvalds 
8711da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(device_del);
8721da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(device_unregister);
8731da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(get_device);
8741da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(put_device);
8751da177e4SLinus Torvalds 
8761da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(device_create_file);
8771da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(device_remove_file);
87823681e47SGreg Kroah-Hartman 
87923681e47SGreg Kroah-Hartman 
88023681e47SGreg Kroah-Hartman static void device_create_release(struct device *dev)
88123681e47SGreg Kroah-Hartman {
88223681e47SGreg Kroah-Hartman 	pr_debug("%s called for %s\n", __FUNCTION__, dev->bus_id);
88323681e47SGreg Kroah-Hartman 	kfree(dev);
88423681e47SGreg Kroah-Hartman }
88523681e47SGreg Kroah-Hartman 
88623681e47SGreg Kroah-Hartman /**
88723681e47SGreg Kroah-Hartman  * device_create - creates a device and registers it with sysfs
88842734dafSHenrik Kretzschmar  * @class: pointer to the struct class that this device should be registered to
88942734dafSHenrik Kretzschmar  * @parent: pointer to the parent struct device of this new device, if any
89042734dafSHenrik Kretzschmar  * @devt: the dev_t for the char device to be added
89142734dafSHenrik Kretzschmar  * @fmt: string for the device's name
89223681e47SGreg Kroah-Hartman  *
89342734dafSHenrik Kretzschmar  * This function can be used by char device classes.  A struct device
89442734dafSHenrik Kretzschmar  * will be created in sysfs, registered to the specified class.
89542734dafSHenrik Kretzschmar  *
89623681e47SGreg Kroah-Hartman  * A "dev" file will be created, showing the dev_t for the device, if
89723681e47SGreg Kroah-Hartman  * the dev_t is not 0,0.
89842734dafSHenrik Kretzschmar  * If a pointer to a parent struct device is passed in, the newly created
89942734dafSHenrik Kretzschmar  * struct device will be a child of that device in sysfs.
90042734dafSHenrik Kretzschmar  * The pointer to the struct device will be returned from the call.
90142734dafSHenrik Kretzschmar  * Any further sysfs files that might be required can be created using this
90223681e47SGreg Kroah-Hartman  * pointer.
90323681e47SGreg Kroah-Hartman  *
90423681e47SGreg Kroah-Hartman  * Note: the struct class passed to this function must have previously
90523681e47SGreg Kroah-Hartman  * been created with a call to class_create().
90623681e47SGreg Kroah-Hartman  */
90723681e47SGreg Kroah-Hartman struct device *device_create(struct class *class, struct device *parent,
9085cbe5f8aSGreg Kroah-Hartman 			     dev_t devt, const char *fmt, ...)
90923681e47SGreg Kroah-Hartman {
91023681e47SGreg Kroah-Hartman 	va_list args;
91123681e47SGreg Kroah-Hartman 	struct device *dev = NULL;
91223681e47SGreg Kroah-Hartman 	int retval = -ENODEV;
91323681e47SGreg Kroah-Hartman 
91423681e47SGreg Kroah-Hartman 	if (class == NULL || IS_ERR(class))
91523681e47SGreg Kroah-Hartman 		goto error;
91623681e47SGreg Kroah-Hartman 
91723681e47SGreg Kroah-Hartman 	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
91823681e47SGreg Kroah-Hartman 	if (!dev) {
91923681e47SGreg Kroah-Hartman 		retval = -ENOMEM;
92023681e47SGreg Kroah-Hartman 		goto error;
92123681e47SGreg Kroah-Hartman 	}
92223681e47SGreg Kroah-Hartman 
92323681e47SGreg Kroah-Hartman 	dev->devt = devt;
92423681e47SGreg Kroah-Hartman 	dev->class = class;
92523681e47SGreg Kroah-Hartman 	dev->parent = parent;
92623681e47SGreg Kroah-Hartman 	dev->release = device_create_release;
92723681e47SGreg Kroah-Hartman 
92823681e47SGreg Kroah-Hartman 	va_start(args, fmt);
92923681e47SGreg Kroah-Hartman 	vsnprintf(dev->bus_id, BUS_ID_SIZE, fmt, args);
93023681e47SGreg Kroah-Hartman 	va_end(args);
93123681e47SGreg Kroah-Hartman 	retval = device_register(dev);
93223681e47SGreg Kroah-Hartman 	if (retval)
93323681e47SGreg Kroah-Hartman 		goto error;
93423681e47SGreg Kroah-Hartman 
93523681e47SGreg Kroah-Hartman 	return dev;
93623681e47SGreg Kroah-Hartman 
93723681e47SGreg Kroah-Hartman error:
93823681e47SGreg Kroah-Hartman 	kfree(dev);
93923681e47SGreg Kroah-Hartman 	return ERR_PTR(retval);
94023681e47SGreg Kroah-Hartman }
94123681e47SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(device_create);
94223681e47SGreg Kroah-Hartman 
94323681e47SGreg Kroah-Hartman /**
94423681e47SGreg Kroah-Hartman  * device_destroy - removes a device that was created with device_create()
94542734dafSHenrik Kretzschmar  * @class: pointer to the struct class that this device was registered with
94642734dafSHenrik Kretzschmar  * @devt: the dev_t of the device that was previously registered
94723681e47SGreg Kroah-Hartman  *
94842734dafSHenrik Kretzschmar  * This call unregisters and cleans up a device that was created with a
94942734dafSHenrik Kretzschmar  * call to device_create().
95023681e47SGreg Kroah-Hartman  */
95123681e47SGreg Kroah-Hartman void device_destroy(struct class *class, dev_t devt)
95223681e47SGreg Kroah-Hartman {
95323681e47SGreg Kroah-Hartman 	struct device *dev = NULL;
95423681e47SGreg Kroah-Hartman 	struct device *dev_tmp;
95523681e47SGreg Kroah-Hartman 
95623681e47SGreg Kroah-Hartman 	down(&class->sem);
95723681e47SGreg Kroah-Hartman 	list_for_each_entry(dev_tmp, &class->devices, node) {
95823681e47SGreg Kroah-Hartman 		if (dev_tmp->devt == devt) {
95923681e47SGreg Kroah-Hartman 			dev = dev_tmp;
96023681e47SGreg Kroah-Hartman 			break;
96123681e47SGreg Kroah-Hartman 		}
96223681e47SGreg Kroah-Hartman 	}
96323681e47SGreg Kroah-Hartman 	up(&class->sem);
96423681e47SGreg Kroah-Hartman 
9655d9fd169SGreg Kroah-Hartman 	if (dev)
96623681e47SGreg Kroah-Hartman 		device_unregister(dev);
96723681e47SGreg Kroah-Hartman }
96823681e47SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(device_destroy);
969a2de48caSGreg Kroah-Hartman 
970a2de48caSGreg Kroah-Hartman /**
971a2de48caSGreg Kroah-Hartman  * device_rename - renames a device
972a2de48caSGreg Kroah-Hartman  * @dev: the pointer to the struct device to be renamed
973a2de48caSGreg Kroah-Hartman  * @new_name: the new name of the device
974a2de48caSGreg Kroah-Hartman  */
975a2de48caSGreg Kroah-Hartman int device_rename(struct device *dev, char *new_name)
976a2de48caSGreg Kroah-Hartman {
977a2de48caSGreg Kroah-Hartman 	char *old_class_name = NULL;
978a2de48caSGreg Kroah-Hartman 	char *new_class_name = NULL;
979a2de48caSGreg Kroah-Hartman 	char *old_symlink_name = NULL;
980a2de48caSGreg Kroah-Hartman 	int error;
981a2de48caSGreg Kroah-Hartman 
982a2de48caSGreg Kroah-Hartman 	dev = get_device(dev);
983a2de48caSGreg Kroah-Hartman 	if (!dev)
984a2de48caSGreg Kroah-Hartman 		return -EINVAL;
985a2de48caSGreg Kroah-Hartman 
986a2de48caSGreg Kroah-Hartman 	pr_debug("DEVICE: renaming '%s' to '%s'\n", dev->bus_id, new_name);
987a2de48caSGreg Kroah-Hartman 
98899ef3ef8SKay Sievers #ifdef CONFIG_SYSFS_DEPRECATED
989a2de48caSGreg Kroah-Hartman 	if ((dev->class) && (dev->parent))
990a2de48caSGreg Kroah-Hartman 		old_class_name = make_class_name(dev->class->name, &dev->kobj);
99199ef3ef8SKay Sievers #endif
992a2de48caSGreg Kroah-Hartman 
993a2de48caSGreg Kroah-Hartman 	if (dev->class) {
994a2de48caSGreg Kroah-Hartman 		old_symlink_name = kmalloc(BUS_ID_SIZE, GFP_KERNEL);
995952ab431SJesper Juhl 		if (!old_symlink_name) {
996952ab431SJesper Juhl 			error = -ENOMEM;
997952ab431SJesper Juhl 			goto out_free_old_class;
998952ab431SJesper Juhl 		}
999a2de48caSGreg Kroah-Hartman 		strlcpy(old_symlink_name, dev->bus_id, BUS_ID_SIZE);
1000a2de48caSGreg Kroah-Hartman 	}
1001a2de48caSGreg Kroah-Hartman 
1002a2de48caSGreg Kroah-Hartman 	strlcpy(dev->bus_id, new_name, BUS_ID_SIZE);
1003a2de48caSGreg Kroah-Hartman 
1004a2de48caSGreg Kroah-Hartman 	error = kobject_rename(&dev->kobj, new_name);
1005a2de48caSGreg Kroah-Hartman 
100699ef3ef8SKay Sievers #ifdef CONFIG_SYSFS_DEPRECATED
1007a2de48caSGreg Kroah-Hartman 	if (old_class_name) {
1008a2de48caSGreg Kroah-Hartman 		new_class_name = make_class_name(dev->class->name, &dev->kobj);
1009a2de48caSGreg Kroah-Hartman 		if (new_class_name) {
1010a2de48caSGreg Kroah-Hartman 			sysfs_create_link(&dev->parent->kobj, &dev->kobj,
1011a2de48caSGreg Kroah-Hartman 					  new_class_name);
1012a2de48caSGreg Kroah-Hartman 			sysfs_remove_link(&dev->parent->kobj, old_class_name);
1013a2de48caSGreg Kroah-Hartman 		}
1014a2de48caSGreg Kroah-Hartman 	}
101599ef3ef8SKay Sievers #endif
101699ef3ef8SKay Sievers 
1017a2de48caSGreg Kroah-Hartman 	if (dev->class) {
1018a2de48caSGreg Kroah-Hartman 		sysfs_remove_link(&dev->class->subsys.kset.kobj,
1019a2de48caSGreg Kroah-Hartman 				  old_symlink_name);
1020a2de48caSGreg Kroah-Hartman 		sysfs_create_link(&dev->class->subsys.kset.kobj, &dev->kobj,
1021a2de48caSGreg Kroah-Hartman 				  dev->bus_id);
1022a2de48caSGreg Kroah-Hartman 	}
1023a2de48caSGreg Kroah-Hartman 	put_device(dev);
1024a2de48caSGreg Kroah-Hartman 
1025a2de48caSGreg Kroah-Hartman 	kfree(new_class_name);
1026a2de48caSGreg Kroah-Hartman 	kfree(old_symlink_name);
1027952ab431SJesper Juhl  out_free_old_class:
1028952ab431SJesper Juhl 	kfree(old_class_name);
1029a2de48caSGreg Kroah-Hartman 
1030a2de48caSGreg Kroah-Hartman 	return error;
1031a2de48caSGreg Kroah-Hartman }
10328a82472fSCornelia Huck 
10338a82472fSCornelia Huck 
10348a82472fSCornelia Huck static int device_move_class_links(struct device *dev,
10358a82472fSCornelia Huck 				   struct device *old_parent,
10368a82472fSCornelia Huck 				   struct device *new_parent)
10378a82472fSCornelia Huck {
10388a82472fSCornelia Huck #ifdef CONFIG_SYSFS_DEPRECATED
10398a82472fSCornelia Huck 	int error;
10408a82472fSCornelia Huck 	char *class_name;
10418a82472fSCornelia Huck 
10428a82472fSCornelia Huck 	class_name = make_class_name(dev->class->name, &dev->kobj);
10438a82472fSCornelia Huck 	if (!class_name) {
1044cb360bbfSCornelia Huck 		error = -ENOMEM;
10458a82472fSCornelia Huck 		goto out;
10468a82472fSCornelia Huck 	}
10478a82472fSCornelia Huck 	if (old_parent) {
10488a82472fSCornelia Huck 		sysfs_remove_link(&dev->kobj, "device");
10498a82472fSCornelia Huck 		sysfs_remove_link(&old_parent->kobj, class_name);
10508a82472fSCornelia Huck 	}
1051c744aeaeSCornelia Huck 	if (new_parent) {
1052c744aeaeSCornelia Huck 		error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
1053c744aeaeSCornelia Huck 					  "device");
10548a82472fSCornelia Huck 		if (error)
10558a82472fSCornelia Huck 			goto out;
1056c744aeaeSCornelia Huck 		error = sysfs_create_link(&new_parent->kobj, &dev->kobj,
1057c744aeaeSCornelia Huck 					  class_name);
10588a82472fSCornelia Huck 		if (error)
10598a82472fSCornelia Huck 			sysfs_remove_link(&dev->kobj, "device");
1060c744aeaeSCornelia Huck 	}
1061c744aeaeSCornelia Huck 	else
1062c744aeaeSCornelia Huck 		error = 0;
10638a82472fSCornelia Huck out:
10648a82472fSCornelia Huck 	kfree(class_name);
10658a82472fSCornelia Huck 	return error;
10668a82472fSCornelia Huck #else
10678a82472fSCornelia Huck 	return 0;
10688a82472fSCornelia Huck #endif
10698a82472fSCornelia Huck }
10708a82472fSCornelia Huck 
10718a82472fSCornelia Huck /**
10728a82472fSCornelia Huck  * device_move - moves a device to a new parent
10738a82472fSCornelia Huck  * @dev: the pointer to the struct device to be moved
1074c744aeaeSCornelia Huck  * @new_parent: the new parent of the device (can by NULL)
10758a82472fSCornelia Huck  */
10768a82472fSCornelia Huck int device_move(struct device *dev, struct device *new_parent)
10778a82472fSCornelia Huck {
10788a82472fSCornelia Huck 	int error;
10798a82472fSCornelia Huck 	struct device *old_parent;
1080c744aeaeSCornelia Huck 	struct kobject *new_parent_kobj;
10818a82472fSCornelia Huck 
10828a82472fSCornelia Huck 	dev = get_device(dev);
10838a82472fSCornelia Huck 	if (!dev)
10848a82472fSCornelia Huck 		return -EINVAL;
10858a82472fSCornelia Huck 
10868a82472fSCornelia Huck 	new_parent = get_device(new_parent);
1087c744aeaeSCornelia Huck 	new_parent_kobj = get_device_parent (dev, new_parent);
1088c744aeaeSCornelia Huck 	if (IS_ERR(new_parent_kobj)) {
1089c744aeaeSCornelia Huck 		error = PTR_ERR(new_parent_kobj);
1090c744aeaeSCornelia Huck 		put_device(new_parent);
10918a82472fSCornelia Huck 		goto out;
10928a82472fSCornelia Huck 	}
10938a82472fSCornelia Huck 	pr_debug("DEVICE: moving '%s' to '%s'\n", dev->bus_id,
1094c744aeaeSCornelia Huck 		 new_parent ? new_parent->bus_id : "<NULL>");
1095c744aeaeSCornelia Huck 	error = kobject_move(&dev->kobj, new_parent_kobj);
10968a82472fSCornelia Huck 	if (error) {
10978a82472fSCornelia Huck 		put_device(new_parent);
10988a82472fSCornelia Huck 		goto out;
10998a82472fSCornelia Huck 	}
11008a82472fSCornelia Huck 	old_parent = dev->parent;
11018a82472fSCornelia Huck 	dev->parent = new_parent;
11028a82472fSCornelia Huck 	if (old_parent)
1103acf02d23SCornelia Huck 		klist_remove(&dev->knode_parent);
1104c744aeaeSCornelia Huck 	if (new_parent)
11058a82472fSCornelia Huck 		klist_add_tail(&dev->knode_parent, &new_parent->klist_children);
11068a82472fSCornelia Huck 	if (!dev->class)
11078a82472fSCornelia Huck 		goto out_put;
11088a82472fSCornelia Huck 	error = device_move_class_links(dev, old_parent, new_parent);
11098a82472fSCornelia Huck 	if (error) {
11108a82472fSCornelia Huck 		/* We ignore errors on cleanup since we're hosed anyway... */
11118a82472fSCornelia Huck 		device_move_class_links(dev, new_parent, old_parent);
11128a82472fSCornelia Huck 		if (!kobject_move(&dev->kobj, &old_parent->kobj)) {
1113c744aeaeSCornelia Huck 			if (new_parent)
1114acf02d23SCornelia Huck 				klist_remove(&dev->knode_parent);
11158a82472fSCornelia Huck 			if (old_parent)
11168a82472fSCornelia Huck 				klist_add_tail(&dev->knode_parent,
11178a82472fSCornelia Huck 					       &old_parent->klist_children);
11188a82472fSCornelia Huck 		}
11198a82472fSCornelia Huck 		put_device(new_parent);
11208a82472fSCornelia Huck 		goto out;
11218a82472fSCornelia Huck 	}
11228a82472fSCornelia Huck out_put:
11238a82472fSCornelia Huck 	put_device(old_parent);
11248a82472fSCornelia Huck out:
11258a82472fSCornelia Huck 	put_device(dev);
11268a82472fSCornelia Huck 	return error;
11278a82472fSCornelia Huck }
11288a82472fSCornelia Huck 
11298a82472fSCornelia Huck EXPORT_SYMBOL_GPL(device_move);
1130