xref: /openbmc/linux/drivers/base/core.c (revision f70fa629)
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>
201da177e4SLinus Torvalds 
211da177e4SLinus Torvalds #include <asm/semaphore.h>
221da177e4SLinus Torvalds 
231da177e4SLinus Torvalds #include "base.h"
241da177e4SLinus Torvalds #include "power/power.h"
251da177e4SLinus Torvalds 
261da177e4SLinus Torvalds int (*platform_notify)(struct device * dev) = NULL;
271da177e4SLinus Torvalds int (*platform_notify_remove)(struct device * dev) = NULL;
281da177e4SLinus Torvalds 
291da177e4SLinus Torvalds /*
301da177e4SLinus Torvalds  * sysfs bindings for devices.
311da177e4SLinus Torvalds  */
321da177e4SLinus Torvalds 
333e95637aSAlan Stern /**
343e95637aSAlan Stern  * dev_driver_string - Return a device's driver name, if at all possible
353e95637aSAlan Stern  * @dev: struct device to get the name of
363e95637aSAlan Stern  *
373e95637aSAlan Stern  * Will return the device's driver's name if it is bound to a device.  If
383e95637aSAlan Stern  * the device is not bound to a device, it will return the name of the bus
393e95637aSAlan Stern  * it is attached to.  If it is not attached to a bus either, an empty
403e95637aSAlan Stern  * string will be returned.
413e95637aSAlan Stern  */
423e95637aSAlan Stern const char *dev_driver_string(struct device *dev)
433e95637aSAlan Stern {
443e95637aSAlan Stern 	return dev->driver ? dev->driver->name :
453e95637aSAlan Stern 			(dev->bus ? dev->bus->name : "");
463e95637aSAlan Stern }
47310a922dSMatthew Wilcox EXPORT_SYMBOL(dev_driver_string);
483e95637aSAlan Stern 
491da177e4SLinus Torvalds #define to_dev(obj) container_of(obj, struct device, kobj)
501da177e4SLinus Torvalds #define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr)
511da177e4SLinus Torvalds 
521da177e4SLinus Torvalds static ssize_t
531da177e4SLinus Torvalds dev_attr_show(struct kobject * kobj, struct attribute * attr, char * buf)
541da177e4SLinus Torvalds {
551da177e4SLinus Torvalds 	struct device_attribute * dev_attr = to_dev_attr(attr);
561da177e4SLinus Torvalds 	struct device * dev = to_dev(kobj);
574a0c20bfSDmitry Torokhov 	ssize_t ret = -EIO;
581da177e4SLinus Torvalds 
591da177e4SLinus Torvalds 	if (dev_attr->show)
6054b6f35cSYani Ioannou 		ret = dev_attr->show(dev, dev_attr, buf);
611da177e4SLinus Torvalds 	return ret;
621da177e4SLinus Torvalds }
631da177e4SLinus Torvalds 
641da177e4SLinus Torvalds static ssize_t
651da177e4SLinus Torvalds dev_attr_store(struct kobject * kobj, struct attribute * attr,
661da177e4SLinus Torvalds 	       const char * buf, size_t count)
671da177e4SLinus Torvalds {
681da177e4SLinus Torvalds 	struct device_attribute * dev_attr = to_dev_attr(attr);
691da177e4SLinus Torvalds 	struct device * dev = to_dev(kobj);
704a0c20bfSDmitry Torokhov 	ssize_t ret = -EIO;
711da177e4SLinus Torvalds 
721da177e4SLinus Torvalds 	if (dev_attr->store)
7354b6f35cSYani Ioannou 		ret = dev_attr->store(dev, dev_attr, buf, count);
741da177e4SLinus Torvalds 	return ret;
751da177e4SLinus Torvalds }
761da177e4SLinus Torvalds 
771da177e4SLinus Torvalds static struct sysfs_ops dev_sysfs_ops = {
781da177e4SLinus Torvalds 	.show	= dev_attr_show,
791da177e4SLinus Torvalds 	.store	= dev_attr_store,
801da177e4SLinus Torvalds };
811da177e4SLinus Torvalds 
821da177e4SLinus Torvalds 
831da177e4SLinus Torvalds /**
841da177e4SLinus Torvalds  *	device_release - free device structure.
851da177e4SLinus Torvalds  *	@kobj:	device's kobject.
861da177e4SLinus Torvalds  *
871da177e4SLinus Torvalds  *	This is called once the reference count for the object
881da177e4SLinus Torvalds  *	reaches 0. We forward the call to the device's release
891da177e4SLinus Torvalds  *	method, which should handle actually freeing the structure.
901da177e4SLinus Torvalds  */
911da177e4SLinus Torvalds static void device_release(struct kobject * kobj)
921da177e4SLinus Torvalds {
931da177e4SLinus Torvalds 	struct device * dev = to_dev(kobj);
941da177e4SLinus Torvalds 
951da177e4SLinus Torvalds 	if (dev->release)
961da177e4SLinus Torvalds 		dev->release(dev);
972620efefSGreg Kroah-Hartman 	else if (dev->class && dev->class->dev_release)
982620efefSGreg Kroah-Hartman 		dev->class->dev_release(dev);
991da177e4SLinus Torvalds 	else {
1001da177e4SLinus Torvalds 		printk(KERN_ERR "Device '%s' does not have a release() function, "
1011da177e4SLinus Torvalds 			"it is broken and must be fixed.\n",
1021da177e4SLinus Torvalds 			dev->bus_id);
1031da177e4SLinus Torvalds 		WARN_ON(1);
1041da177e4SLinus Torvalds 	}
1051da177e4SLinus Torvalds }
1061da177e4SLinus Torvalds 
1071da177e4SLinus Torvalds static struct kobj_type ktype_device = {
1081da177e4SLinus Torvalds 	.release	= device_release,
1091da177e4SLinus Torvalds 	.sysfs_ops	= &dev_sysfs_ops,
1101da177e4SLinus Torvalds };
1111da177e4SLinus Torvalds 
1121da177e4SLinus Torvalds 
113312c004dSKay Sievers static int dev_uevent_filter(struct kset *kset, struct kobject *kobj)
1141da177e4SLinus Torvalds {
1151da177e4SLinus Torvalds 	struct kobj_type *ktype = get_ktype(kobj);
1161da177e4SLinus Torvalds 
1171da177e4SLinus Torvalds 	if (ktype == &ktype_device) {
1181da177e4SLinus Torvalds 		struct device *dev = to_dev(kobj);
1191da177e4SLinus Torvalds 		if (dev->bus)
1201da177e4SLinus Torvalds 			return 1;
12123681e47SGreg Kroah-Hartman 		if (dev->class)
12223681e47SGreg Kroah-Hartman 			return 1;
1231da177e4SLinus Torvalds 	}
1241da177e4SLinus Torvalds 	return 0;
1251da177e4SLinus Torvalds }
1261da177e4SLinus Torvalds 
127312c004dSKay Sievers static const char *dev_uevent_name(struct kset *kset, struct kobject *kobj)
1281da177e4SLinus Torvalds {
1291da177e4SLinus Torvalds 	struct device *dev = to_dev(kobj);
1301da177e4SLinus Torvalds 
13123681e47SGreg Kroah-Hartman 	if (dev->bus)
1321da177e4SLinus Torvalds 		return dev->bus->name;
13323681e47SGreg Kroah-Hartman 	if (dev->class)
13423681e47SGreg Kroah-Hartman 		return dev->class->name;
13523681e47SGreg Kroah-Hartman 	return NULL;
1361da177e4SLinus Torvalds }
1371da177e4SLinus Torvalds 
138312c004dSKay Sievers static int dev_uevent(struct kset *kset, struct kobject *kobj, char **envp,
1391da177e4SLinus Torvalds 			int num_envp, char *buffer, int buffer_size)
1401da177e4SLinus Torvalds {
1411da177e4SLinus Torvalds 	struct device *dev = to_dev(kobj);
1421da177e4SLinus Torvalds 	int i = 0;
1431da177e4SLinus Torvalds 	int length = 0;
1441da177e4SLinus Torvalds 	int retval = 0;
1451da177e4SLinus Torvalds 
14623681e47SGreg Kroah-Hartman 	/* add the major/minor if present */
14723681e47SGreg Kroah-Hartman 	if (MAJOR(dev->devt)) {
14823681e47SGreg Kroah-Hartman 		add_uevent_var(envp, num_envp, &i,
14923681e47SGreg Kroah-Hartman 			       buffer, buffer_size, &length,
15023681e47SGreg Kroah-Hartman 			       "MAJOR=%u", MAJOR(dev->devt));
15123681e47SGreg Kroah-Hartman 		add_uevent_var(envp, num_envp, &i,
15223681e47SGreg Kroah-Hartman 			       buffer, buffer_size, &length,
15323681e47SGreg Kroah-Hartman 			       "MINOR=%u", MINOR(dev->devt));
15423681e47SGreg Kroah-Hartman 	}
15523681e47SGreg Kroah-Hartman 
156d81d9d6bSKay Sievers 	/* add bus name (same as SUBSYSTEM, deprecated) */
1571da177e4SLinus Torvalds 	if (dev->bus)
158312c004dSKay Sievers 		add_uevent_var(envp, num_envp, &i,
1591da177e4SLinus Torvalds 			       buffer, buffer_size, &length,
1601da177e4SLinus Torvalds 			       "PHYSDEVBUS=%s", dev->bus->name);
1611da177e4SLinus Torvalds 
162d81d9d6bSKay Sievers 	/* add driver name (PHYSDEV* values are deprecated)*/
163d81d9d6bSKay Sievers 	if (dev->driver) {
164d81d9d6bSKay Sievers 		add_uevent_var(envp, num_envp, &i,
165d81d9d6bSKay Sievers 			       buffer, buffer_size, &length,
166d81d9d6bSKay Sievers 			       "DRIVER=%s", dev->driver->name);
167312c004dSKay Sievers 		add_uevent_var(envp, num_envp, &i,
1681da177e4SLinus Torvalds 			       buffer, buffer_size, &length,
1691da177e4SLinus Torvalds 			       "PHYSDEVDRIVER=%s", dev->driver->name);
170d81d9d6bSKay Sievers 	}
1711da177e4SLinus Torvalds 
1721da177e4SLinus Torvalds 	/* terminate, set to next free slot, shrink available space */
1731da177e4SLinus Torvalds 	envp[i] = NULL;
1741da177e4SLinus Torvalds 	envp = &envp[i];
1751da177e4SLinus Torvalds 	num_envp -= i;
1761da177e4SLinus Torvalds 	buffer = &buffer[length];
1771da177e4SLinus Torvalds 	buffer_size -= length;
1781da177e4SLinus Torvalds 
179312c004dSKay Sievers 	if (dev->bus && dev->bus->uevent) {
1801da177e4SLinus Torvalds 		/* have the bus specific function add its stuff */
181312c004dSKay Sievers 		retval = dev->bus->uevent(dev, envp, num_envp, buffer, buffer_size);
1821da177e4SLinus Torvalds 			if (retval) {
183312c004dSKay Sievers 			pr_debug ("%s - uevent() returned %d\n",
1841da177e4SLinus Torvalds 				  __FUNCTION__, retval);
1851da177e4SLinus Torvalds 		}
1861da177e4SLinus Torvalds 	}
1871da177e4SLinus Torvalds 
1882620efefSGreg Kroah-Hartman 	if (dev->class && dev->class->dev_uevent) {
1892620efefSGreg Kroah-Hartman 		/* have the class specific function add its stuff */
1902620efefSGreg Kroah-Hartman 		retval = dev->class->dev_uevent(dev, envp, num_envp, buffer, buffer_size);
1912620efefSGreg Kroah-Hartman 			if (retval) {
1922620efefSGreg Kroah-Hartman 				pr_debug("%s - dev_uevent() returned %d\n",
1932620efefSGreg Kroah-Hartman 					 __FUNCTION__, retval);
1942620efefSGreg Kroah-Hartman 		}
1952620efefSGreg Kroah-Hartman 	}
1962620efefSGreg Kroah-Hartman 
1971da177e4SLinus Torvalds 	return retval;
1981da177e4SLinus Torvalds }
1991da177e4SLinus Torvalds 
200312c004dSKay Sievers static struct kset_uevent_ops device_uevent_ops = {
201312c004dSKay Sievers 	.filter =	dev_uevent_filter,
202312c004dSKay Sievers 	.name =		dev_uevent_name,
203312c004dSKay Sievers 	.uevent =	dev_uevent,
2041da177e4SLinus Torvalds };
2051da177e4SLinus Torvalds 
206a7fd6706SKay Sievers static ssize_t store_uevent(struct device *dev, struct device_attribute *attr,
207a7fd6706SKay Sievers 			    const char *buf, size_t count)
208a7fd6706SKay Sievers {
209312c004dSKay Sievers 	kobject_uevent(&dev->kobj, KOBJ_ADD);
210a7fd6706SKay Sievers 	return count;
211a7fd6706SKay Sievers }
212a7fd6706SKay Sievers 
213de0ff00dSGreg Kroah-Hartman static int device_add_groups(struct device *dev)
214de0ff00dSGreg Kroah-Hartman {
215de0ff00dSGreg Kroah-Hartman 	int i;
216de0ff00dSGreg Kroah-Hartman 	int error = 0;
217de0ff00dSGreg Kroah-Hartman 
218de0ff00dSGreg Kroah-Hartman 	if (dev->groups) {
219de0ff00dSGreg Kroah-Hartman 		for (i = 0; dev->groups[i]; i++) {
220de0ff00dSGreg Kroah-Hartman 			error = sysfs_create_group(&dev->kobj, dev->groups[i]);
221de0ff00dSGreg Kroah-Hartman 			if (error) {
222de0ff00dSGreg Kroah-Hartman 				while (--i >= 0)
223de0ff00dSGreg Kroah-Hartman 					sysfs_remove_group(&dev->kobj, dev->groups[i]);
224de0ff00dSGreg Kroah-Hartman 				goto out;
225de0ff00dSGreg Kroah-Hartman 			}
226de0ff00dSGreg Kroah-Hartman 		}
227de0ff00dSGreg Kroah-Hartman 	}
228de0ff00dSGreg Kroah-Hartman out:
229de0ff00dSGreg Kroah-Hartman 	return error;
230de0ff00dSGreg Kroah-Hartman }
231de0ff00dSGreg Kroah-Hartman 
232de0ff00dSGreg Kroah-Hartman static void device_remove_groups(struct device *dev)
233de0ff00dSGreg Kroah-Hartman {
234de0ff00dSGreg Kroah-Hartman 	int i;
235de0ff00dSGreg Kroah-Hartman 	if (dev->groups) {
236de0ff00dSGreg Kroah-Hartman 		for (i = 0; dev->groups[i]; i++) {
237de0ff00dSGreg Kroah-Hartman 			sysfs_remove_group(&dev->kobj, dev->groups[i]);
238de0ff00dSGreg Kroah-Hartman 		}
239de0ff00dSGreg Kroah-Hartman 	}
240de0ff00dSGreg Kroah-Hartman }
241de0ff00dSGreg Kroah-Hartman 
2422620efefSGreg Kroah-Hartman static int device_add_attrs(struct device *dev)
2432620efefSGreg Kroah-Hartman {
2442620efefSGreg Kroah-Hartman 	struct class *class = dev->class;
2452620efefSGreg Kroah-Hartman 	int error = 0;
2462620efefSGreg Kroah-Hartman 	int i;
2472620efefSGreg Kroah-Hartman 
2482620efefSGreg Kroah-Hartman 	if (!class)
2492620efefSGreg Kroah-Hartman 		return 0;
2502620efefSGreg Kroah-Hartman 
2512620efefSGreg Kroah-Hartman 	if (class->dev_attrs) {
2522620efefSGreg Kroah-Hartman 		for (i = 0; attr_name(class->dev_attrs[i]); i++) {
2532620efefSGreg Kroah-Hartman 			error = device_create_file(dev, &class->dev_attrs[i]);
2542620efefSGreg Kroah-Hartman 			if (error)
2552620efefSGreg Kroah-Hartman 				break;
2562620efefSGreg Kroah-Hartman 		}
2572620efefSGreg Kroah-Hartman 	}
2582620efefSGreg Kroah-Hartman 	if (error)
2592620efefSGreg Kroah-Hartman 		while (--i >= 0)
2602620efefSGreg Kroah-Hartman 			device_remove_file(dev, &class->dev_attrs[i]);
2612620efefSGreg Kroah-Hartman 	return error;
2622620efefSGreg Kroah-Hartman }
2632620efefSGreg Kroah-Hartman 
2642620efefSGreg Kroah-Hartman static void device_remove_attrs(struct device *dev)
2652620efefSGreg Kroah-Hartman {
2662620efefSGreg Kroah-Hartman 	struct class *class = dev->class;
2672620efefSGreg Kroah-Hartman 	int i;
2682620efefSGreg Kroah-Hartman 
2692620efefSGreg Kroah-Hartman 	if (!class)
2702620efefSGreg Kroah-Hartman 		return;
2712620efefSGreg Kroah-Hartman 
2722620efefSGreg Kroah-Hartman 	if (class->dev_attrs) {
2732620efefSGreg Kroah-Hartman 		for (i = 0; attr_name(class->dev_attrs[i]); i++)
2742620efefSGreg Kroah-Hartman 			device_remove_file(dev, &class->dev_attrs[i]);
2752620efefSGreg Kroah-Hartman 	}
2762620efefSGreg Kroah-Hartman }
2772620efefSGreg Kroah-Hartman 
2782620efefSGreg Kroah-Hartman 
27923681e47SGreg Kroah-Hartman static ssize_t show_dev(struct device *dev, struct device_attribute *attr,
28023681e47SGreg Kroah-Hartman 			char *buf)
28123681e47SGreg Kroah-Hartman {
28223681e47SGreg Kroah-Hartman 	return print_dev_t(buf, dev->devt);
28323681e47SGreg Kroah-Hartman }
28423681e47SGreg Kroah-Hartman 
2850863afb3SMartin Waitz /*
2860863afb3SMartin Waitz  *	devices_subsys - structure to be registered with kobject core.
2871da177e4SLinus Torvalds  */
2881da177e4SLinus Torvalds 
289312c004dSKay Sievers decl_subsys(devices, &ktype_device, &device_uevent_ops);
2901da177e4SLinus Torvalds 
2911da177e4SLinus Torvalds 
2921da177e4SLinus Torvalds /**
2931da177e4SLinus Torvalds  *	device_create_file - create sysfs attribute file for device.
2941da177e4SLinus Torvalds  *	@dev:	device.
2951da177e4SLinus Torvalds  *	@attr:	device attribute descriptor.
2961da177e4SLinus Torvalds  */
2971da177e4SLinus Torvalds 
2981da177e4SLinus Torvalds int device_create_file(struct device * dev, struct device_attribute * attr)
2991da177e4SLinus Torvalds {
3001da177e4SLinus Torvalds 	int error = 0;
3011da177e4SLinus Torvalds 	if (get_device(dev)) {
3021da177e4SLinus Torvalds 		error = sysfs_create_file(&dev->kobj, &attr->attr);
3031da177e4SLinus Torvalds 		put_device(dev);
3041da177e4SLinus Torvalds 	}
3051da177e4SLinus Torvalds 	return error;
3061da177e4SLinus Torvalds }
3071da177e4SLinus Torvalds 
3081da177e4SLinus Torvalds /**
3091da177e4SLinus Torvalds  *	device_remove_file - remove sysfs attribute file.
3101da177e4SLinus Torvalds  *	@dev:	device.
3111da177e4SLinus Torvalds  *	@attr:	device attribute descriptor.
3121da177e4SLinus Torvalds  */
3131da177e4SLinus Torvalds 
3141da177e4SLinus Torvalds void device_remove_file(struct device * dev, struct device_attribute * attr)
3151da177e4SLinus Torvalds {
3161da177e4SLinus Torvalds 	if (get_device(dev)) {
3171da177e4SLinus Torvalds 		sysfs_remove_file(&dev->kobj, &attr->attr);
3181da177e4SLinus Torvalds 		put_device(dev);
3191da177e4SLinus Torvalds 	}
3201da177e4SLinus Torvalds }
3211da177e4SLinus Torvalds 
3222589f188SGreg Kroah-Hartman /**
3232589f188SGreg Kroah-Hartman  * device_create_bin_file - create sysfs binary attribute file for device.
3242589f188SGreg Kroah-Hartman  * @dev: device.
3252589f188SGreg Kroah-Hartman  * @attr: device binary attribute descriptor.
3262589f188SGreg Kroah-Hartman  */
3272589f188SGreg Kroah-Hartman int device_create_bin_file(struct device *dev, struct bin_attribute *attr)
3282589f188SGreg Kroah-Hartman {
3292589f188SGreg Kroah-Hartman 	int error = -EINVAL;
3302589f188SGreg Kroah-Hartman 	if (dev)
3312589f188SGreg Kroah-Hartman 		error = sysfs_create_bin_file(&dev->kobj, attr);
3322589f188SGreg Kroah-Hartman 	return error;
3332589f188SGreg Kroah-Hartman }
3342589f188SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(device_create_bin_file);
3352589f188SGreg Kroah-Hartman 
3362589f188SGreg Kroah-Hartman /**
3372589f188SGreg Kroah-Hartman  * device_remove_bin_file - remove sysfs binary attribute file
3382589f188SGreg Kroah-Hartman  * @dev: device.
3392589f188SGreg Kroah-Hartman  * @attr: device binary attribute descriptor.
3402589f188SGreg Kroah-Hartman  */
3412589f188SGreg Kroah-Hartman void device_remove_bin_file(struct device *dev, struct bin_attribute *attr)
3422589f188SGreg Kroah-Hartman {
3432589f188SGreg Kroah-Hartman 	if (dev)
3442589f188SGreg Kroah-Hartman 		sysfs_remove_bin_file(&dev->kobj, attr);
3452589f188SGreg Kroah-Hartman }
3462589f188SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(device_remove_bin_file);
3472589f188SGreg Kroah-Hartman 
34834bb61f9SJames Bottomley static void klist_children_get(struct klist_node *n)
34934bb61f9SJames Bottomley {
35034bb61f9SJames Bottomley 	struct device *dev = container_of(n, struct device, knode_parent);
35134bb61f9SJames Bottomley 
35234bb61f9SJames Bottomley 	get_device(dev);
35334bb61f9SJames Bottomley }
35434bb61f9SJames Bottomley 
35534bb61f9SJames Bottomley static void klist_children_put(struct klist_node *n)
35634bb61f9SJames Bottomley {
35734bb61f9SJames Bottomley 	struct device *dev = container_of(n, struct device, knode_parent);
35834bb61f9SJames Bottomley 
35934bb61f9SJames Bottomley 	put_device(dev);
36034bb61f9SJames Bottomley }
36134bb61f9SJames Bottomley 
3621da177e4SLinus Torvalds 
3631da177e4SLinus Torvalds /**
3641da177e4SLinus Torvalds  *	device_initialize - init device structure.
3651da177e4SLinus Torvalds  *	@dev:	device.
3661da177e4SLinus Torvalds  *
3671da177e4SLinus Torvalds  *	This prepares the device for use by other layers,
3681da177e4SLinus Torvalds  *	including adding it to the device hierarchy.
3691da177e4SLinus Torvalds  *	It is the first half of device_register(), if called by
3701da177e4SLinus Torvalds  *	that, though it can also be called separately, so one
3711da177e4SLinus Torvalds  *	may use @dev's fields (e.g. the refcount).
3721da177e4SLinus Torvalds  */
3731da177e4SLinus Torvalds 
3741da177e4SLinus Torvalds void device_initialize(struct device *dev)
3751da177e4SLinus Torvalds {
3761da177e4SLinus Torvalds 	kobj_set_kset_s(dev, devices_subsys);
3771da177e4SLinus Torvalds 	kobject_init(&dev->kobj);
37834bb61f9SJames Bottomley 	klist_init(&dev->klist_children, klist_children_get,
37934bb61f9SJames Bottomley 		   klist_children_put);
3801da177e4SLinus Torvalds 	INIT_LIST_HEAD(&dev->dma_pools);
38123681e47SGreg Kroah-Hartman 	INIT_LIST_HEAD(&dev->node);
382af70316aSmochel@digitalimplant.org 	init_MUTEX(&dev->sem);
3830ac85241SDavid Brownell 	device_init_wakeup(dev, 0);
3841da177e4SLinus Torvalds }
3851da177e4SLinus Torvalds 
3861da177e4SLinus Torvalds /**
3871da177e4SLinus Torvalds  *	device_add - add device to device hierarchy.
3881da177e4SLinus Torvalds  *	@dev:	device.
3891da177e4SLinus Torvalds  *
3901da177e4SLinus Torvalds  *	This is part 2 of device_register(), though may be called
3911da177e4SLinus Torvalds  *	separately _iff_ device_initialize() has been called separately.
3921da177e4SLinus Torvalds  *
3931da177e4SLinus Torvalds  *	This adds it to the kobject hierarchy via kobject_add(), adds it
3941da177e4SLinus Torvalds  *	to the global and sibling lists for the device, then
3951da177e4SLinus Torvalds  *	adds it to the other relevant subsystems of the driver model.
3961da177e4SLinus Torvalds  */
3971da177e4SLinus Torvalds int device_add(struct device *dev)
3981da177e4SLinus Torvalds {
3991da177e4SLinus Torvalds 	struct device *parent = NULL;
400e9a7d305SGreg Kroah-Hartman 	char *class_name = NULL;
401c47ed219SGreg Kroah-Hartman 	struct class_interface *class_intf;
4021da177e4SLinus Torvalds 	int error = -EINVAL;
4031da177e4SLinus Torvalds 
4041da177e4SLinus Torvalds 	dev = get_device(dev);
4051da177e4SLinus Torvalds 	if (!dev || !strlen(dev->bus_id))
4061da177e4SLinus Torvalds 		goto Error;
4071da177e4SLinus Torvalds 
408c205ef48SGreg Kroah-Hartman 	/* if this is a class device, and has no parent, create one */
409c205ef48SGreg Kroah-Hartman 	if ((dev->class) && (dev->parent == NULL)) {
410c205ef48SGreg Kroah-Hartman 		error = virtual_device_parent(dev);
411c205ef48SGreg Kroah-Hartman 		if (error)
412c205ef48SGreg Kroah-Hartman 			goto Error;
413c205ef48SGreg Kroah-Hartman 	}
414c205ef48SGreg Kroah-Hartman 
4151da177e4SLinus Torvalds 	parent = get_device(dev->parent);
4161da177e4SLinus Torvalds 
4171da177e4SLinus Torvalds 	pr_debug("DEV: registering device: ID = '%s'\n", dev->bus_id);
4181da177e4SLinus Torvalds 
4191da177e4SLinus Torvalds 	/* first, register with generic layer. */
4201da177e4SLinus Torvalds 	kobject_set_name(&dev->kobj, "%s", dev->bus_id);
4211da177e4SLinus Torvalds 	if (parent)
4221da177e4SLinus Torvalds 		dev->kobj.parent = &parent->kobj;
4231da177e4SLinus Torvalds 
4241da177e4SLinus Torvalds 	if ((error = kobject_add(&dev->kobj)))
4251da177e4SLinus Torvalds 		goto Error;
426a7fd6706SKay Sievers 
42737022644SBrian Walsh 	/* notify platform of device entry */
42837022644SBrian Walsh 	if (platform_notify)
42937022644SBrian Walsh 		platform_notify(dev);
43037022644SBrian Walsh 
431a7fd6706SKay Sievers 	dev->uevent_attr.attr.name = "uevent";
432a7fd6706SKay Sievers 	dev->uevent_attr.attr.mode = S_IWUSR;
433a7fd6706SKay Sievers 	if (dev->driver)
434a7fd6706SKay Sievers 		dev->uevent_attr.attr.owner = dev->driver->owner;
435a7fd6706SKay Sievers 	dev->uevent_attr.store = store_uevent;
436a306eea4SCornelia Huck 	error = device_create_file(dev, &dev->uevent_attr);
437a306eea4SCornelia Huck 	if (error)
438a306eea4SCornelia Huck 		goto attrError;
439a7fd6706SKay Sievers 
44023681e47SGreg Kroah-Hartman 	if (MAJOR(dev->devt)) {
44123681e47SGreg Kroah-Hartman 		struct device_attribute *attr;
44223681e47SGreg Kroah-Hartman 		attr = kzalloc(sizeof(*attr), GFP_KERNEL);
44323681e47SGreg Kroah-Hartman 		if (!attr) {
44423681e47SGreg Kroah-Hartman 			error = -ENOMEM;
445a306eea4SCornelia Huck 			goto ueventattrError;
44623681e47SGreg Kroah-Hartman 		}
44723681e47SGreg Kroah-Hartman 		attr->attr.name = "dev";
44823681e47SGreg Kroah-Hartman 		attr->attr.mode = S_IRUGO;
44923681e47SGreg Kroah-Hartman 		if (dev->driver)
45023681e47SGreg Kroah-Hartman 			attr->attr.owner = dev->driver->owner;
45123681e47SGreg Kroah-Hartman 		attr->show = show_dev;
45223681e47SGreg Kroah-Hartman 		error = device_create_file(dev, attr);
45323681e47SGreg Kroah-Hartman 		if (error) {
45423681e47SGreg Kroah-Hartman 			kfree(attr);
455a306eea4SCornelia Huck 			goto ueventattrError;
45623681e47SGreg Kroah-Hartman 		}
45723681e47SGreg Kroah-Hartman 
45823681e47SGreg Kroah-Hartman 		dev->devt_attr = attr;
45923681e47SGreg Kroah-Hartman 	}
46023681e47SGreg Kroah-Hartman 
461b9d9c82bSKay Sievers 	if (dev->class) {
462b9d9c82bSKay Sievers 		sysfs_create_link(&dev->kobj, &dev->class->subsys.kset.kobj,
463b9d9c82bSKay Sievers 				  "subsystem");
46423681e47SGreg Kroah-Hartman 		sysfs_create_link(&dev->class->subsys.kset.kobj, &dev->kobj,
46523681e47SGreg Kroah-Hartman 				  dev->bus_id);
46664bb5d2cSGreg Kroah-Hartman 		if (parent) {
467e9a7d305SGreg Kroah-Hartman 			sysfs_create_link(&dev->kobj, &dev->parent->kobj, "device");
468e9a7d305SGreg Kroah-Hartman 			class_name = make_class_name(dev->class->name, &dev->kobj);
469e9a7d305SGreg Kroah-Hartman 			sysfs_create_link(&dev->parent->kobj, &dev->kobj, class_name);
470b9d9c82bSKay Sievers 		}
47164bb5d2cSGreg Kroah-Hartman 	}
47223681e47SGreg Kroah-Hartman 
4732620efefSGreg Kroah-Hartman 	if ((error = device_add_attrs(dev)))
4742620efefSGreg Kroah-Hartman 		goto AttrsError;
475de0ff00dSGreg Kroah-Hartman 	if ((error = device_add_groups(dev)))
476de0ff00dSGreg Kroah-Hartman 		goto GroupError;
4771da177e4SLinus Torvalds 	if ((error = device_pm_add(dev)))
4781da177e4SLinus Torvalds 		goto PMError;
4791da177e4SLinus Torvalds 	if ((error = bus_add_device(dev)))
4801da177e4SLinus Torvalds 		goto BusError;
48153877d06SKay Sievers 	kobject_uevent(&dev->kobj, KOBJ_ADD);
482f70fa629SAlan Stern 	if ((error = bus_attach_device(dev)))
483f70fa629SAlan Stern 		goto AttachError;
4841da177e4SLinus Torvalds 	if (parent)
485d856f1e3SJames Bottomley 		klist_add_tail(&dev->knode_parent, &parent->klist_children);
4861da177e4SLinus Torvalds 
4875d9fd169SGreg Kroah-Hartman 	if (dev->class) {
4885d9fd169SGreg Kroah-Hartman 		down(&dev->class->sem);
489c47ed219SGreg Kroah-Hartman 		/* tie the class to the device */
4905d9fd169SGreg Kroah-Hartman 		list_add_tail(&dev->node, &dev->class->devices);
491c47ed219SGreg Kroah-Hartman 
492c47ed219SGreg Kroah-Hartman 		/* notify any interfaces that the device is here */
493c47ed219SGreg Kroah-Hartman 		list_for_each_entry(class_intf, &dev->class->interfaces, node)
494c47ed219SGreg Kroah-Hartman 			if (class_intf->add_dev)
495c47ed219SGreg Kroah-Hartman 				class_intf->add_dev(dev, class_intf);
4965d9fd169SGreg Kroah-Hartman 		up(&dev->class->sem);
4975d9fd169SGreg Kroah-Hartman 	}
4981da177e4SLinus Torvalds  Done:
499e9a7d305SGreg Kroah-Hartman  	kfree(class_name);
5001da177e4SLinus Torvalds 	put_device(dev);
5011da177e4SLinus Torvalds 	return error;
502f70fa629SAlan Stern  AttachError:
503f70fa629SAlan Stern 	bus_remove_device(dev);
5041da177e4SLinus Torvalds  BusError:
5051da177e4SLinus Torvalds 	device_pm_remove(dev);
5061da177e4SLinus Torvalds  PMError:
507de0ff00dSGreg Kroah-Hartman 	device_remove_groups(dev);
508de0ff00dSGreg Kroah-Hartman  GroupError:
5092620efefSGreg Kroah-Hartman  	device_remove_attrs(dev);
5102620efefSGreg Kroah-Hartman  AttrsError:
51123681e47SGreg Kroah-Hartman 	if (dev->devt_attr) {
51223681e47SGreg Kroah-Hartman 		device_remove_file(dev, dev->devt_attr);
51323681e47SGreg Kroah-Hartman 		kfree(dev->devt_attr);
51423681e47SGreg Kroah-Hartman 	}
515a306eea4SCornelia Huck  ueventattrError:
516a306eea4SCornelia Huck 	device_remove_file(dev, &dev->uevent_attr);
51723681e47SGreg Kroah-Hartman  attrError:
518312c004dSKay Sievers 	kobject_uevent(&dev->kobj, KOBJ_REMOVE);
5191da177e4SLinus Torvalds 	kobject_del(&dev->kobj);
5201da177e4SLinus Torvalds  Error:
5211da177e4SLinus Torvalds 	if (parent)
5221da177e4SLinus Torvalds 		put_device(parent);
5231da177e4SLinus Torvalds 	goto Done;
5241da177e4SLinus Torvalds }
5251da177e4SLinus Torvalds 
5261da177e4SLinus Torvalds 
5271da177e4SLinus Torvalds /**
5281da177e4SLinus Torvalds  *	device_register - register a device with the system.
5291da177e4SLinus Torvalds  *	@dev:	pointer to the device structure
5301da177e4SLinus Torvalds  *
5311da177e4SLinus Torvalds  *	This happens in two clean steps - initialize the device
5321da177e4SLinus Torvalds  *	and add it to the system. The two steps can be called
5331da177e4SLinus Torvalds  *	separately, but this is the easiest and most common.
5341da177e4SLinus Torvalds  *	I.e. you should only call the two helpers separately if
5351da177e4SLinus Torvalds  *	have a clearly defined need to use and refcount the device
5361da177e4SLinus Torvalds  *	before it is added to the hierarchy.
5371da177e4SLinus Torvalds  */
5381da177e4SLinus Torvalds 
5391da177e4SLinus Torvalds int device_register(struct device *dev)
5401da177e4SLinus Torvalds {
5411da177e4SLinus Torvalds 	device_initialize(dev);
5421da177e4SLinus Torvalds 	return device_add(dev);
5431da177e4SLinus Torvalds }
5441da177e4SLinus Torvalds 
5451da177e4SLinus Torvalds 
5461da177e4SLinus Torvalds /**
5471da177e4SLinus Torvalds  *	get_device - increment reference count for device.
5481da177e4SLinus Torvalds  *	@dev:	device.
5491da177e4SLinus Torvalds  *
5501da177e4SLinus Torvalds  *	This simply forwards the call to kobject_get(), though
5511da177e4SLinus Torvalds  *	we do take care to provide for the case that we get a NULL
5521da177e4SLinus Torvalds  *	pointer passed in.
5531da177e4SLinus Torvalds  */
5541da177e4SLinus Torvalds 
5551da177e4SLinus Torvalds struct device * get_device(struct device * dev)
5561da177e4SLinus Torvalds {
5571da177e4SLinus Torvalds 	return dev ? to_dev(kobject_get(&dev->kobj)) : NULL;
5581da177e4SLinus Torvalds }
5591da177e4SLinus Torvalds 
5601da177e4SLinus Torvalds 
5611da177e4SLinus Torvalds /**
5621da177e4SLinus Torvalds  *	put_device - decrement reference count.
5631da177e4SLinus Torvalds  *	@dev:	device in question.
5641da177e4SLinus Torvalds  */
5651da177e4SLinus Torvalds void put_device(struct device * dev)
5661da177e4SLinus Torvalds {
5671da177e4SLinus Torvalds 	if (dev)
5681da177e4SLinus Torvalds 		kobject_put(&dev->kobj);
5691da177e4SLinus Torvalds }
5701da177e4SLinus Torvalds 
5711da177e4SLinus Torvalds 
5721da177e4SLinus Torvalds /**
5731da177e4SLinus Torvalds  *	device_del - delete device from system.
5741da177e4SLinus Torvalds  *	@dev:	device.
5751da177e4SLinus Torvalds  *
5761da177e4SLinus Torvalds  *	This is the first part of the device unregistration
5771da177e4SLinus Torvalds  *	sequence. This removes the device from the lists we control
5781da177e4SLinus Torvalds  *	from here, has it removed from the other driver model
5791da177e4SLinus Torvalds  *	subsystems it was added to in device_add(), and removes it
5801da177e4SLinus Torvalds  *	from the kobject hierarchy.
5811da177e4SLinus Torvalds  *
5821da177e4SLinus Torvalds  *	NOTE: this should be called manually _iff_ device_add() was
5831da177e4SLinus Torvalds  *	also called manually.
5841da177e4SLinus Torvalds  */
5851da177e4SLinus Torvalds 
5861da177e4SLinus Torvalds void device_del(struct device * dev)
5871da177e4SLinus Torvalds {
5881da177e4SLinus Torvalds 	struct device * parent = dev->parent;
589e9a7d305SGreg Kroah-Hartman 	char *class_name = NULL;
590c47ed219SGreg Kroah-Hartman 	struct class_interface *class_intf;
5911da177e4SLinus Torvalds 
5921da177e4SLinus Torvalds 	if (parent)
593d62c0f9fSPatrick Mochel 		klist_del(&dev->knode_parent);
59423681e47SGreg Kroah-Hartman 	if (dev->devt_attr)
59523681e47SGreg Kroah-Hartman 		device_remove_file(dev, dev->devt_attr);
596b9d9c82bSKay Sievers 	if (dev->class) {
597b9d9c82bSKay Sievers 		sysfs_remove_link(&dev->kobj, "subsystem");
59823681e47SGreg Kroah-Hartman 		sysfs_remove_link(&dev->class->subsys.kset.kobj, dev->bus_id);
599e9a7d305SGreg Kroah-Hartman 		class_name = make_class_name(dev->class->name, &dev->kobj);
60064bb5d2cSGreg Kroah-Hartman 		if (parent) {
601e9a7d305SGreg Kroah-Hartman 			sysfs_remove_link(&dev->kobj, "device");
602e9a7d305SGreg Kroah-Hartman 			sysfs_remove_link(&dev->parent->kobj, class_name);
60364bb5d2cSGreg Kroah-Hartman 		}
604e9a7d305SGreg Kroah-Hartman 		kfree(class_name);
6055d9fd169SGreg Kroah-Hartman 		down(&dev->class->sem);
606c47ed219SGreg Kroah-Hartman 		/* notify any interfaces that the device is now gone */
607c47ed219SGreg Kroah-Hartman 		list_for_each_entry(class_intf, &dev->class->interfaces, node)
608c47ed219SGreg Kroah-Hartman 			if (class_intf->remove_dev)
609c47ed219SGreg Kroah-Hartman 				class_intf->remove_dev(dev, class_intf);
610c47ed219SGreg Kroah-Hartman 		/* remove the device from the class list */
6115d9fd169SGreg Kroah-Hartman 		list_del_init(&dev->node);
6125d9fd169SGreg Kroah-Hartman 		up(&dev->class->sem);
613b9d9c82bSKay Sievers 	}
614a7fd6706SKay Sievers 	device_remove_file(dev, &dev->uevent_attr);
615de0ff00dSGreg Kroah-Hartman 	device_remove_groups(dev);
6162620efefSGreg Kroah-Hartman 	device_remove_attrs(dev);
6171da177e4SLinus Torvalds 
6181da177e4SLinus Torvalds 	/* Notify the platform of the removal, in case they
6191da177e4SLinus Torvalds 	 * need to do anything...
6201da177e4SLinus Torvalds 	 */
6211da177e4SLinus Torvalds 	if (platform_notify_remove)
6221da177e4SLinus Torvalds 		platform_notify_remove(dev);
6231da177e4SLinus Torvalds 	bus_remove_device(dev);
6241da177e4SLinus Torvalds 	device_pm_remove(dev);
625312c004dSKay Sievers 	kobject_uevent(&dev->kobj, KOBJ_REMOVE);
6261da177e4SLinus Torvalds 	kobject_del(&dev->kobj);
6271da177e4SLinus Torvalds 	if (parent)
6281da177e4SLinus Torvalds 		put_device(parent);
6291da177e4SLinus Torvalds }
6301da177e4SLinus Torvalds 
6311da177e4SLinus Torvalds /**
6321da177e4SLinus Torvalds  *	device_unregister - unregister device from system.
6331da177e4SLinus Torvalds  *	@dev:	device going away.
6341da177e4SLinus Torvalds  *
6351da177e4SLinus Torvalds  *	We do this in two parts, like we do device_register(). First,
6361da177e4SLinus Torvalds  *	we remove it from all the subsystems with device_del(), then
6371da177e4SLinus Torvalds  *	we decrement the reference count via put_device(). If that
6381da177e4SLinus Torvalds  *	is the final reference count, the device will be cleaned up
6391da177e4SLinus Torvalds  *	via device_release() above. Otherwise, the structure will
6401da177e4SLinus Torvalds  *	stick around until the final reference to the device is dropped.
6411da177e4SLinus Torvalds  */
6421da177e4SLinus Torvalds void device_unregister(struct device * dev)
6431da177e4SLinus Torvalds {
6441da177e4SLinus Torvalds 	pr_debug("DEV: Unregistering device. ID = '%s'\n", dev->bus_id);
6451da177e4SLinus Torvalds 	device_del(dev);
6461da177e4SLinus Torvalds 	put_device(dev);
6471da177e4SLinus Torvalds }
6481da177e4SLinus Torvalds 
6491da177e4SLinus Torvalds 
65036239577Smochel@digitalimplant.org static struct device * next_device(struct klist_iter * i)
65136239577Smochel@digitalimplant.org {
65236239577Smochel@digitalimplant.org 	struct klist_node * n = klist_next(i);
65336239577Smochel@digitalimplant.org 	return n ? container_of(n, struct device, knode_parent) : NULL;
65436239577Smochel@digitalimplant.org }
65536239577Smochel@digitalimplant.org 
6561da177e4SLinus Torvalds /**
6571da177e4SLinus Torvalds  *	device_for_each_child - device child iterator.
658c41455fbSRandy Dunlap  *	@parent: parent struct device.
6591da177e4SLinus Torvalds  *	@data:	data for the callback.
6601da177e4SLinus Torvalds  *	@fn:	function to be called for each device.
6611da177e4SLinus Torvalds  *
662c41455fbSRandy Dunlap  *	Iterate over @parent's child devices, and call @fn for each,
6631da177e4SLinus Torvalds  *	passing it @data.
6641da177e4SLinus Torvalds  *
6651da177e4SLinus Torvalds  *	We check the return of @fn each time. If it returns anything
6661da177e4SLinus Torvalds  *	other than 0, we break out and return that value.
6671da177e4SLinus Torvalds  */
66836239577Smochel@digitalimplant.org int device_for_each_child(struct device * parent, void * data,
6691da177e4SLinus Torvalds 		     int (*fn)(struct device *, void *))
6701da177e4SLinus Torvalds {
67136239577Smochel@digitalimplant.org 	struct klist_iter i;
6721da177e4SLinus Torvalds 	struct device * child;
6731da177e4SLinus Torvalds 	int error = 0;
6741da177e4SLinus Torvalds 
67536239577Smochel@digitalimplant.org 	klist_iter_init(&parent->klist_children, &i);
67636239577Smochel@digitalimplant.org 	while ((child = next_device(&i)) && !error)
67736239577Smochel@digitalimplant.org 		error = fn(child, data);
67836239577Smochel@digitalimplant.org 	klist_iter_exit(&i);
6791da177e4SLinus Torvalds 	return error;
6801da177e4SLinus Torvalds }
6811da177e4SLinus Torvalds 
6821da177e4SLinus Torvalds int __init devices_init(void)
6831da177e4SLinus Torvalds {
6841da177e4SLinus Torvalds 	return subsystem_register(&devices_subsys);
6851da177e4SLinus Torvalds }
6861da177e4SLinus Torvalds 
6871da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(device_for_each_child);
6881da177e4SLinus Torvalds 
6891da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(device_initialize);
6901da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(device_add);
6911da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(device_register);
6921da177e4SLinus Torvalds 
6931da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(device_del);
6941da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(device_unregister);
6951da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(get_device);
6961da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(put_device);
6971da177e4SLinus Torvalds 
6981da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(device_create_file);
6991da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(device_remove_file);
70023681e47SGreg Kroah-Hartman 
70123681e47SGreg Kroah-Hartman 
70223681e47SGreg Kroah-Hartman static void device_create_release(struct device *dev)
70323681e47SGreg Kroah-Hartman {
70423681e47SGreg Kroah-Hartman 	pr_debug("%s called for %s\n", __FUNCTION__, dev->bus_id);
70523681e47SGreg Kroah-Hartman 	kfree(dev);
70623681e47SGreg Kroah-Hartman }
70723681e47SGreg Kroah-Hartman 
70823681e47SGreg Kroah-Hartman /**
70923681e47SGreg Kroah-Hartman  * device_create - creates a device and registers it with sysfs
71042734dafSHenrik Kretzschmar  * @class: pointer to the struct class that this device should be registered to
71142734dafSHenrik Kretzschmar  * @parent: pointer to the parent struct device of this new device, if any
71242734dafSHenrik Kretzschmar  * @devt: the dev_t for the char device to be added
71342734dafSHenrik Kretzschmar  * @fmt: string for the device's name
71423681e47SGreg Kroah-Hartman  *
71542734dafSHenrik Kretzschmar  * This function can be used by char device classes.  A struct device
71642734dafSHenrik Kretzschmar  * will be created in sysfs, registered to the specified class.
71742734dafSHenrik Kretzschmar  *
71823681e47SGreg Kroah-Hartman  * A "dev" file will be created, showing the dev_t for the device, if
71923681e47SGreg Kroah-Hartman  * the dev_t is not 0,0.
72042734dafSHenrik Kretzschmar  * If a pointer to a parent struct device is passed in, the newly created
72142734dafSHenrik Kretzschmar  * struct device will be a child of that device in sysfs.
72242734dafSHenrik Kretzschmar  * The pointer to the struct device will be returned from the call.
72342734dafSHenrik Kretzschmar  * Any further sysfs files that might be required can be created using this
72423681e47SGreg Kroah-Hartman  * pointer.
72523681e47SGreg Kroah-Hartman  *
72623681e47SGreg Kroah-Hartman  * Note: the struct class passed to this function must have previously
72723681e47SGreg Kroah-Hartman  * been created with a call to class_create().
72823681e47SGreg Kroah-Hartman  */
72923681e47SGreg Kroah-Hartman struct device *device_create(struct class *class, struct device *parent,
7305cbe5f8aSGreg Kroah-Hartman 			     dev_t devt, const char *fmt, ...)
73123681e47SGreg Kroah-Hartman {
73223681e47SGreg Kroah-Hartman 	va_list args;
73323681e47SGreg Kroah-Hartman 	struct device *dev = NULL;
73423681e47SGreg Kroah-Hartman 	int retval = -ENODEV;
73523681e47SGreg Kroah-Hartman 
73623681e47SGreg Kroah-Hartman 	if (class == NULL || IS_ERR(class))
73723681e47SGreg Kroah-Hartman 		goto error;
73823681e47SGreg Kroah-Hartman 
73923681e47SGreg Kroah-Hartman 	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
74023681e47SGreg Kroah-Hartman 	if (!dev) {
74123681e47SGreg Kroah-Hartman 		retval = -ENOMEM;
74223681e47SGreg Kroah-Hartman 		goto error;
74323681e47SGreg Kroah-Hartman 	}
74423681e47SGreg Kroah-Hartman 
74523681e47SGreg Kroah-Hartman 	dev->devt = devt;
74623681e47SGreg Kroah-Hartman 	dev->class = class;
74723681e47SGreg Kroah-Hartman 	dev->parent = parent;
74823681e47SGreg Kroah-Hartman 	dev->release = device_create_release;
74923681e47SGreg Kroah-Hartman 
75023681e47SGreg Kroah-Hartman 	va_start(args, fmt);
75123681e47SGreg Kroah-Hartman 	vsnprintf(dev->bus_id, BUS_ID_SIZE, fmt, args);
75223681e47SGreg Kroah-Hartman 	va_end(args);
75323681e47SGreg Kroah-Hartman 	retval = device_register(dev);
75423681e47SGreg Kroah-Hartman 	if (retval)
75523681e47SGreg Kroah-Hartman 		goto error;
75623681e47SGreg Kroah-Hartman 
75723681e47SGreg Kroah-Hartman 	return dev;
75823681e47SGreg Kroah-Hartman 
75923681e47SGreg Kroah-Hartman error:
76023681e47SGreg Kroah-Hartman 	kfree(dev);
76123681e47SGreg Kroah-Hartman 	return ERR_PTR(retval);
76223681e47SGreg Kroah-Hartman }
76323681e47SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(device_create);
76423681e47SGreg Kroah-Hartman 
76523681e47SGreg Kroah-Hartman /**
76623681e47SGreg Kroah-Hartman  * device_destroy - removes a device that was created with device_create()
76742734dafSHenrik Kretzschmar  * @class: pointer to the struct class that this device was registered with
76842734dafSHenrik Kretzschmar  * @devt: the dev_t of the device that was previously registered
76923681e47SGreg Kroah-Hartman  *
77042734dafSHenrik Kretzschmar  * This call unregisters and cleans up a device that was created with a
77142734dafSHenrik Kretzschmar  * call to device_create().
77223681e47SGreg Kroah-Hartman  */
77323681e47SGreg Kroah-Hartman void device_destroy(struct class *class, dev_t devt)
77423681e47SGreg Kroah-Hartman {
77523681e47SGreg Kroah-Hartman 	struct device *dev = NULL;
77623681e47SGreg Kroah-Hartman 	struct device *dev_tmp;
77723681e47SGreg Kroah-Hartman 
77823681e47SGreg Kroah-Hartman 	down(&class->sem);
77923681e47SGreg Kroah-Hartman 	list_for_each_entry(dev_tmp, &class->devices, node) {
78023681e47SGreg Kroah-Hartman 		if (dev_tmp->devt == devt) {
78123681e47SGreg Kroah-Hartman 			dev = dev_tmp;
78223681e47SGreg Kroah-Hartman 			break;
78323681e47SGreg Kroah-Hartman 		}
78423681e47SGreg Kroah-Hartman 	}
78523681e47SGreg Kroah-Hartman 	up(&class->sem);
78623681e47SGreg Kroah-Hartman 
7875d9fd169SGreg Kroah-Hartman 	if (dev)
78823681e47SGreg Kroah-Hartman 		device_unregister(dev);
78923681e47SGreg Kroah-Hartman }
79023681e47SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(device_destroy);
791a2de48caSGreg Kroah-Hartman 
792a2de48caSGreg Kroah-Hartman /**
793a2de48caSGreg Kroah-Hartman  * device_rename - renames a device
794a2de48caSGreg Kroah-Hartman  * @dev: the pointer to the struct device to be renamed
795a2de48caSGreg Kroah-Hartman  * @new_name: the new name of the device
796a2de48caSGreg Kroah-Hartman  */
797a2de48caSGreg Kroah-Hartman int device_rename(struct device *dev, char *new_name)
798a2de48caSGreg Kroah-Hartman {
799a2de48caSGreg Kroah-Hartman 	char *old_class_name = NULL;
800a2de48caSGreg Kroah-Hartman 	char *new_class_name = NULL;
801a2de48caSGreg Kroah-Hartman 	char *old_symlink_name = NULL;
802a2de48caSGreg Kroah-Hartman 	int error;
803a2de48caSGreg Kroah-Hartman 
804a2de48caSGreg Kroah-Hartman 	dev = get_device(dev);
805a2de48caSGreg Kroah-Hartman 	if (!dev)
806a2de48caSGreg Kroah-Hartman 		return -EINVAL;
807a2de48caSGreg Kroah-Hartman 
808a2de48caSGreg Kroah-Hartman 	pr_debug("DEVICE: renaming '%s' to '%s'\n", dev->bus_id, new_name);
809a2de48caSGreg Kroah-Hartman 
810a2de48caSGreg Kroah-Hartman 	if ((dev->class) && (dev->parent))
811a2de48caSGreg Kroah-Hartman 		old_class_name = make_class_name(dev->class->name, &dev->kobj);
812a2de48caSGreg Kroah-Hartman 
813a2de48caSGreg Kroah-Hartman 	if (dev->class) {
814a2de48caSGreg Kroah-Hartman 		old_symlink_name = kmalloc(BUS_ID_SIZE, GFP_KERNEL);
815952ab431SJesper Juhl 		if (!old_symlink_name) {
816952ab431SJesper Juhl 			error = -ENOMEM;
817952ab431SJesper Juhl 			goto out_free_old_class;
818952ab431SJesper Juhl 		}
819a2de48caSGreg Kroah-Hartman 		strlcpy(old_symlink_name, dev->bus_id, BUS_ID_SIZE);
820a2de48caSGreg Kroah-Hartman 	}
821a2de48caSGreg Kroah-Hartman 
822a2de48caSGreg Kroah-Hartman 	strlcpy(dev->bus_id, new_name, BUS_ID_SIZE);
823a2de48caSGreg Kroah-Hartman 
824a2de48caSGreg Kroah-Hartman 	error = kobject_rename(&dev->kobj, new_name);
825a2de48caSGreg Kroah-Hartman 
826a2de48caSGreg Kroah-Hartman 	if (old_class_name) {
827a2de48caSGreg Kroah-Hartman 		new_class_name = make_class_name(dev->class->name, &dev->kobj);
828a2de48caSGreg Kroah-Hartman 		if (new_class_name) {
829a2de48caSGreg Kroah-Hartman 			sysfs_create_link(&dev->parent->kobj, &dev->kobj,
830a2de48caSGreg Kroah-Hartman 					  new_class_name);
831a2de48caSGreg Kroah-Hartman 			sysfs_remove_link(&dev->parent->kobj, old_class_name);
832a2de48caSGreg Kroah-Hartman 		}
833a2de48caSGreg Kroah-Hartman 	}
834a2de48caSGreg Kroah-Hartman 	if (dev->class) {
835a2de48caSGreg Kroah-Hartman 		sysfs_remove_link(&dev->class->subsys.kset.kobj,
836a2de48caSGreg Kroah-Hartman 				  old_symlink_name);
837a2de48caSGreg Kroah-Hartman 		sysfs_create_link(&dev->class->subsys.kset.kobj, &dev->kobj,
838a2de48caSGreg Kroah-Hartman 				  dev->bus_id);
839a2de48caSGreg Kroah-Hartman 	}
840a2de48caSGreg Kroah-Hartman 	put_device(dev);
841a2de48caSGreg Kroah-Hartman 
842a2de48caSGreg Kroah-Hartman 	kfree(new_class_name);
843a2de48caSGreg Kroah-Hartman 	kfree(old_symlink_name);
844952ab431SJesper Juhl  out_free_old_class:
845952ab431SJesper Juhl 	kfree(old_class_name);
846a2de48caSGreg Kroah-Hartman 
847a2de48caSGreg Kroah-Hartman 	return error;
848a2de48caSGreg Kroah-Hartman }
849