xref: /openbmc/linux/drivers/base/core.c (revision 23681e47)
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
61da177e4SLinus Torvalds  *
71da177e4SLinus Torvalds  * This file is released under the GPLv2
81da177e4SLinus Torvalds  *
91da177e4SLinus Torvalds  */
101da177e4SLinus Torvalds 
111da177e4SLinus Torvalds #include <linux/config.h>
121da177e4SLinus Torvalds #include <linux/device.h>
131da177e4SLinus Torvalds #include <linux/err.h>
141da177e4SLinus Torvalds #include <linux/init.h>
151da177e4SLinus Torvalds #include <linux/module.h>
161da177e4SLinus Torvalds #include <linux/slab.h>
171da177e4SLinus Torvalds #include <linux/string.h>
1823681e47SGreg Kroah-Hartman #include <linux/kdev_t.h>
191da177e4SLinus Torvalds 
201da177e4SLinus Torvalds #include <asm/semaphore.h>
211da177e4SLinus Torvalds 
221da177e4SLinus Torvalds #include "base.h"
231da177e4SLinus Torvalds #include "power/power.h"
241da177e4SLinus Torvalds 
251da177e4SLinus Torvalds int (*platform_notify)(struct device * dev) = NULL;
261da177e4SLinus Torvalds int (*platform_notify_remove)(struct device * dev) = NULL;
271da177e4SLinus Torvalds 
281da177e4SLinus Torvalds /*
291da177e4SLinus Torvalds  * sysfs bindings for devices.
301da177e4SLinus Torvalds  */
311da177e4SLinus Torvalds 
321da177e4SLinus Torvalds #define to_dev(obj) container_of(obj, struct device, kobj)
331da177e4SLinus Torvalds #define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr)
341da177e4SLinus Torvalds 
351da177e4SLinus Torvalds static ssize_t
361da177e4SLinus Torvalds dev_attr_show(struct kobject * kobj, struct attribute * attr, char * buf)
371da177e4SLinus Torvalds {
381da177e4SLinus Torvalds 	struct device_attribute * dev_attr = to_dev_attr(attr);
391da177e4SLinus Torvalds 	struct device * dev = to_dev(kobj);
404a0c20bfSDmitry Torokhov 	ssize_t ret = -EIO;
411da177e4SLinus Torvalds 
421da177e4SLinus Torvalds 	if (dev_attr->show)
4354b6f35cSYani Ioannou 		ret = dev_attr->show(dev, dev_attr, buf);
441da177e4SLinus Torvalds 	return ret;
451da177e4SLinus Torvalds }
461da177e4SLinus Torvalds 
471da177e4SLinus Torvalds static ssize_t
481da177e4SLinus Torvalds dev_attr_store(struct kobject * kobj, struct attribute * attr,
491da177e4SLinus Torvalds 	       const char * buf, size_t count)
501da177e4SLinus Torvalds {
511da177e4SLinus Torvalds 	struct device_attribute * dev_attr = to_dev_attr(attr);
521da177e4SLinus Torvalds 	struct device * dev = to_dev(kobj);
534a0c20bfSDmitry Torokhov 	ssize_t ret = -EIO;
541da177e4SLinus Torvalds 
551da177e4SLinus Torvalds 	if (dev_attr->store)
5654b6f35cSYani Ioannou 		ret = dev_attr->store(dev, dev_attr, buf, count);
571da177e4SLinus Torvalds 	return ret;
581da177e4SLinus Torvalds }
591da177e4SLinus Torvalds 
601da177e4SLinus Torvalds static struct sysfs_ops dev_sysfs_ops = {
611da177e4SLinus Torvalds 	.show	= dev_attr_show,
621da177e4SLinus Torvalds 	.store	= dev_attr_store,
631da177e4SLinus Torvalds };
641da177e4SLinus Torvalds 
651da177e4SLinus Torvalds 
661da177e4SLinus Torvalds /**
671da177e4SLinus Torvalds  *	device_release - free device structure.
681da177e4SLinus Torvalds  *	@kobj:	device's kobject.
691da177e4SLinus Torvalds  *
701da177e4SLinus Torvalds  *	This is called once the reference count for the object
711da177e4SLinus Torvalds  *	reaches 0. We forward the call to the device's release
721da177e4SLinus Torvalds  *	method, which should handle actually freeing the structure.
731da177e4SLinus Torvalds  */
741da177e4SLinus Torvalds static void device_release(struct kobject * kobj)
751da177e4SLinus Torvalds {
761da177e4SLinus Torvalds 	struct device * dev = to_dev(kobj);
771da177e4SLinus Torvalds 
781da177e4SLinus Torvalds 	if (dev->release)
791da177e4SLinus Torvalds 		dev->release(dev);
801da177e4SLinus Torvalds 	else {
811da177e4SLinus Torvalds 		printk(KERN_ERR "Device '%s' does not have a release() function, "
821da177e4SLinus Torvalds 			"it is broken and must be fixed.\n",
831da177e4SLinus Torvalds 			dev->bus_id);
841da177e4SLinus Torvalds 		WARN_ON(1);
851da177e4SLinus Torvalds 	}
861da177e4SLinus Torvalds }
871da177e4SLinus Torvalds 
881da177e4SLinus Torvalds static struct kobj_type ktype_device = {
891da177e4SLinus Torvalds 	.release	= device_release,
901da177e4SLinus Torvalds 	.sysfs_ops	= &dev_sysfs_ops,
911da177e4SLinus Torvalds };
921da177e4SLinus Torvalds 
931da177e4SLinus Torvalds 
94312c004dSKay Sievers static int dev_uevent_filter(struct kset *kset, struct kobject *kobj)
951da177e4SLinus Torvalds {
961da177e4SLinus Torvalds 	struct kobj_type *ktype = get_ktype(kobj);
971da177e4SLinus Torvalds 
981da177e4SLinus Torvalds 	if (ktype == &ktype_device) {
991da177e4SLinus Torvalds 		struct device *dev = to_dev(kobj);
1001da177e4SLinus Torvalds 		if (dev->bus)
1011da177e4SLinus Torvalds 			return 1;
10223681e47SGreg Kroah-Hartman 		if (dev->class)
10323681e47SGreg Kroah-Hartman 			return 1;
1041da177e4SLinus Torvalds 	}
1051da177e4SLinus Torvalds 	return 0;
1061da177e4SLinus Torvalds }
1071da177e4SLinus Torvalds 
108312c004dSKay Sievers static const char *dev_uevent_name(struct kset *kset, struct kobject *kobj)
1091da177e4SLinus Torvalds {
1101da177e4SLinus Torvalds 	struct device *dev = to_dev(kobj);
1111da177e4SLinus Torvalds 
11223681e47SGreg Kroah-Hartman 	if (dev->bus)
1131da177e4SLinus Torvalds 		return dev->bus->name;
11423681e47SGreg Kroah-Hartman 	if (dev->class)
11523681e47SGreg Kroah-Hartman 		return dev->class->name;
11623681e47SGreg Kroah-Hartman 	return NULL;
1171da177e4SLinus Torvalds }
1181da177e4SLinus Torvalds 
119312c004dSKay Sievers static int dev_uevent(struct kset *kset, struct kobject *kobj, char **envp,
1201da177e4SLinus Torvalds 			int num_envp, char *buffer, int buffer_size)
1211da177e4SLinus Torvalds {
1221da177e4SLinus Torvalds 	struct device *dev = to_dev(kobj);
1231da177e4SLinus Torvalds 	int i = 0;
1241da177e4SLinus Torvalds 	int length = 0;
1251da177e4SLinus Torvalds 	int retval = 0;
1261da177e4SLinus Torvalds 
12723681e47SGreg Kroah-Hartman 	/* add the major/minor if present */
12823681e47SGreg Kroah-Hartman 	if (MAJOR(dev->devt)) {
12923681e47SGreg Kroah-Hartman 		add_uevent_var(envp, num_envp, &i,
13023681e47SGreg Kroah-Hartman 			       buffer, buffer_size, &length,
13123681e47SGreg Kroah-Hartman 			       "MAJOR=%u", MAJOR(dev->devt));
13223681e47SGreg Kroah-Hartman 		add_uevent_var(envp, num_envp, &i,
13323681e47SGreg Kroah-Hartman 			       buffer, buffer_size, &length,
13423681e47SGreg Kroah-Hartman 			       "MINOR=%u", MINOR(dev->devt));
13523681e47SGreg Kroah-Hartman 	}
13623681e47SGreg Kroah-Hartman 
1371da177e4SLinus Torvalds 	/* add bus name of physical device */
1381da177e4SLinus Torvalds 	if (dev->bus)
139312c004dSKay Sievers 		add_uevent_var(envp, num_envp, &i,
1401da177e4SLinus Torvalds 			       buffer, buffer_size, &length,
1411da177e4SLinus Torvalds 			       "PHYSDEVBUS=%s", dev->bus->name);
1421da177e4SLinus Torvalds 
1431da177e4SLinus Torvalds 	/* add driver name of physical device */
1441da177e4SLinus Torvalds 	if (dev->driver)
145312c004dSKay Sievers 		add_uevent_var(envp, num_envp, &i,
1461da177e4SLinus Torvalds 			       buffer, buffer_size, &length,
1471da177e4SLinus Torvalds 			       "PHYSDEVDRIVER=%s", dev->driver->name);
1481da177e4SLinus Torvalds 
1491da177e4SLinus Torvalds 	/* terminate, set to next free slot, shrink available space */
1501da177e4SLinus Torvalds 	envp[i] = NULL;
1511da177e4SLinus Torvalds 	envp = &envp[i];
1521da177e4SLinus Torvalds 	num_envp -= i;
1531da177e4SLinus Torvalds 	buffer = &buffer[length];
1541da177e4SLinus Torvalds 	buffer_size -= length;
1551da177e4SLinus Torvalds 
156312c004dSKay Sievers 	if (dev->bus && dev->bus->uevent) {
1571da177e4SLinus Torvalds 		/* have the bus specific function add its stuff */
158312c004dSKay Sievers 		retval = dev->bus->uevent(dev, envp, num_envp, buffer, buffer_size);
1591da177e4SLinus Torvalds 			if (retval) {
160312c004dSKay Sievers 			pr_debug ("%s - uevent() returned %d\n",
1611da177e4SLinus Torvalds 				  __FUNCTION__, retval);
1621da177e4SLinus Torvalds 		}
1631da177e4SLinus Torvalds 	}
1641da177e4SLinus Torvalds 
1651da177e4SLinus Torvalds 	return retval;
1661da177e4SLinus Torvalds }
1671da177e4SLinus Torvalds 
168312c004dSKay Sievers static struct kset_uevent_ops device_uevent_ops = {
169312c004dSKay Sievers 	.filter =	dev_uevent_filter,
170312c004dSKay Sievers 	.name =		dev_uevent_name,
171312c004dSKay Sievers 	.uevent =	dev_uevent,
1721da177e4SLinus Torvalds };
1731da177e4SLinus Torvalds 
174a7fd6706SKay Sievers static ssize_t store_uevent(struct device *dev, struct device_attribute *attr,
175a7fd6706SKay Sievers 			    const char *buf, size_t count)
176a7fd6706SKay Sievers {
177312c004dSKay Sievers 	kobject_uevent(&dev->kobj, KOBJ_ADD);
178a7fd6706SKay Sievers 	return count;
179a7fd6706SKay Sievers }
180a7fd6706SKay Sievers 
18123681e47SGreg Kroah-Hartman static ssize_t show_dev(struct device *dev, struct device_attribute *attr,
18223681e47SGreg Kroah-Hartman 			char *buf)
18323681e47SGreg Kroah-Hartman {
18423681e47SGreg Kroah-Hartman 	return print_dev_t(buf, dev->devt);
18523681e47SGreg Kroah-Hartman }
18623681e47SGreg Kroah-Hartman 
1870863afb3SMartin Waitz /*
1880863afb3SMartin Waitz  *	devices_subsys - structure to be registered with kobject core.
1891da177e4SLinus Torvalds  */
1901da177e4SLinus Torvalds 
191312c004dSKay Sievers decl_subsys(devices, &ktype_device, &device_uevent_ops);
1921da177e4SLinus Torvalds 
1931da177e4SLinus Torvalds 
1941da177e4SLinus Torvalds /**
1951da177e4SLinus Torvalds  *	device_create_file - create sysfs attribute file for device.
1961da177e4SLinus Torvalds  *	@dev:	device.
1971da177e4SLinus Torvalds  *	@attr:	device attribute descriptor.
1981da177e4SLinus Torvalds  */
1991da177e4SLinus Torvalds 
2001da177e4SLinus Torvalds int device_create_file(struct device * dev, struct device_attribute * attr)
2011da177e4SLinus Torvalds {
2021da177e4SLinus Torvalds 	int error = 0;
2031da177e4SLinus Torvalds 	if (get_device(dev)) {
2041da177e4SLinus Torvalds 		error = sysfs_create_file(&dev->kobj, &attr->attr);
2051da177e4SLinus Torvalds 		put_device(dev);
2061da177e4SLinus Torvalds 	}
2071da177e4SLinus Torvalds 	return error;
2081da177e4SLinus Torvalds }
2091da177e4SLinus Torvalds 
2101da177e4SLinus Torvalds /**
2111da177e4SLinus Torvalds  *	device_remove_file - remove sysfs attribute file.
2121da177e4SLinus Torvalds  *	@dev:	device.
2131da177e4SLinus Torvalds  *	@attr:	device attribute descriptor.
2141da177e4SLinus Torvalds  */
2151da177e4SLinus Torvalds 
2161da177e4SLinus Torvalds void device_remove_file(struct device * dev, struct device_attribute * attr)
2171da177e4SLinus Torvalds {
2181da177e4SLinus Torvalds 	if (get_device(dev)) {
2191da177e4SLinus Torvalds 		sysfs_remove_file(&dev->kobj, &attr->attr);
2201da177e4SLinus Torvalds 		put_device(dev);
2211da177e4SLinus Torvalds 	}
2221da177e4SLinus Torvalds }
2231da177e4SLinus Torvalds 
22434bb61f9SJames Bottomley static void klist_children_get(struct klist_node *n)
22534bb61f9SJames Bottomley {
22634bb61f9SJames Bottomley 	struct device *dev = container_of(n, struct device, knode_parent);
22734bb61f9SJames Bottomley 
22834bb61f9SJames Bottomley 	get_device(dev);
22934bb61f9SJames Bottomley }
23034bb61f9SJames Bottomley 
23134bb61f9SJames Bottomley static void klist_children_put(struct klist_node *n)
23234bb61f9SJames Bottomley {
23334bb61f9SJames Bottomley 	struct device *dev = container_of(n, struct device, knode_parent);
23434bb61f9SJames Bottomley 
23534bb61f9SJames Bottomley 	put_device(dev);
23634bb61f9SJames Bottomley }
23734bb61f9SJames Bottomley 
2381da177e4SLinus Torvalds 
2391da177e4SLinus Torvalds /**
2401da177e4SLinus Torvalds  *	device_initialize - init device structure.
2411da177e4SLinus Torvalds  *	@dev:	device.
2421da177e4SLinus Torvalds  *
2431da177e4SLinus Torvalds  *	This prepares the device for use by other layers,
2441da177e4SLinus Torvalds  *	including adding it to the device hierarchy.
2451da177e4SLinus Torvalds  *	It is the first half of device_register(), if called by
2461da177e4SLinus Torvalds  *	that, though it can also be called separately, so one
2471da177e4SLinus Torvalds  *	may use @dev's fields (e.g. the refcount).
2481da177e4SLinus Torvalds  */
2491da177e4SLinus Torvalds 
2501da177e4SLinus Torvalds void device_initialize(struct device *dev)
2511da177e4SLinus Torvalds {
2521da177e4SLinus Torvalds 	kobj_set_kset_s(dev, devices_subsys);
2531da177e4SLinus Torvalds 	kobject_init(&dev->kobj);
25434bb61f9SJames Bottomley 	klist_init(&dev->klist_children, klist_children_get,
25534bb61f9SJames Bottomley 		   klist_children_put);
2561da177e4SLinus Torvalds 	INIT_LIST_HEAD(&dev->dma_pools);
25723681e47SGreg Kroah-Hartman 	INIT_LIST_HEAD(&dev->node);
258af70316aSmochel@digitalimplant.org 	init_MUTEX(&dev->sem);
2590ac85241SDavid Brownell 	device_init_wakeup(dev, 0);
2601da177e4SLinus Torvalds }
2611da177e4SLinus Torvalds 
2621da177e4SLinus Torvalds /**
2631da177e4SLinus Torvalds  *	device_add - add device to device hierarchy.
2641da177e4SLinus Torvalds  *	@dev:	device.
2651da177e4SLinus Torvalds  *
2661da177e4SLinus Torvalds  *	This is part 2 of device_register(), though may be called
2671da177e4SLinus Torvalds  *	separately _iff_ device_initialize() has been called separately.
2681da177e4SLinus Torvalds  *
2691da177e4SLinus Torvalds  *	This adds it to the kobject hierarchy via kobject_add(), adds it
2701da177e4SLinus Torvalds  *	to the global and sibling lists for the device, then
2711da177e4SLinus Torvalds  *	adds it to the other relevant subsystems of the driver model.
2721da177e4SLinus Torvalds  */
2731da177e4SLinus Torvalds int device_add(struct device *dev)
2741da177e4SLinus Torvalds {
2751da177e4SLinus Torvalds 	struct device *parent = NULL;
2761da177e4SLinus Torvalds 	int error = -EINVAL;
2771da177e4SLinus Torvalds 
2781da177e4SLinus Torvalds 	dev = get_device(dev);
2791da177e4SLinus Torvalds 	if (!dev || !strlen(dev->bus_id))
2801da177e4SLinus Torvalds 		goto Error;
2811da177e4SLinus Torvalds 
2821da177e4SLinus Torvalds 	parent = get_device(dev->parent);
2831da177e4SLinus Torvalds 
2841da177e4SLinus Torvalds 	pr_debug("DEV: registering device: ID = '%s'\n", dev->bus_id);
2851da177e4SLinus Torvalds 
2861da177e4SLinus Torvalds 	/* first, register with generic layer. */
2871da177e4SLinus Torvalds 	kobject_set_name(&dev->kobj, "%s", dev->bus_id);
2881da177e4SLinus Torvalds 	if (parent)
2891da177e4SLinus Torvalds 		dev->kobj.parent = &parent->kobj;
2901da177e4SLinus Torvalds 
2911da177e4SLinus Torvalds 	if ((error = kobject_add(&dev->kobj)))
2921da177e4SLinus Torvalds 		goto Error;
293a7fd6706SKay Sievers 
294a7fd6706SKay Sievers 	dev->uevent_attr.attr.name = "uevent";
295a7fd6706SKay Sievers 	dev->uevent_attr.attr.mode = S_IWUSR;
296a7fd6706SKay Sievers 	if (dev->driver)
297a7fd6706SKay Sievers 		dev->uevent_attr.attr.owner = dev->driver->owner;
298a7fd6706SKay Sievers 	dev->uevent_attr.store = store_uevent;
299a7fd6706SKay Sievers 	device_create_file(dev, &dev->uevent_attr);
300a7fd6706SKay Sievers 
30123681e47SGreg Kroah-Hartman 	if (MAJOR(dev->devt)) {
30223681e47SGreg Kroah-Hartman 		struct device_attribute *attr;
30323681e47SGreg Kroah-Hartman 		attr = kzalloc(sizeof(*attr), GFP_KERNEL);
30423681e47SGreg Kroah-Hartman 		if (!attr) {
30523681e47SGreg Kroah-Hartman 			error = -ENOMEM;
30623681e47SGreg Kroah-Hartman 			goto PMError;
30723681e47SGreg Kroah-Hartman 		}
30823681e47SGreg Kroah-Hartman 		attr->attr.name = "dev";
30923681e47SGreg Kroah-Hartman 		attr->attr.mode = S_IRUGO;
31023681e47SGreg Kroah-Hartman 		if (dev->driver)
31123681e47SGreg Kroah-Hartman 			attr->attr.owner = dev->driver->owner;
31223681e47SGreg Kroah-Hartman 		attr->show = show_dev;
31323681e47SGreg Kroah-Hartman 		error = device_create_file(dev, attr);
31423681e47SGreg Kroah-Hartman 		if (error) {
31523681e47SGreg Kroah-Hartman 			kfree(attr);
31623681e47SGreg Kroah-Hartman 			goto attrError;
31723681e47SGreg Kroah-Hartman 		}
31823681e47SGreg Kroah-Hartman 
31923681e47SGreg Kroah-Hartman 		dev->devt_attr = attr;
32023681e47SGreg Kroah-Hartman 	}
32123681e47SGreg Kroah-Hartman 
32223681e47SGreg Kroah-Hartman 	if (dev->class)
32323681e47SGreg Kroah-Hartman 		sysfs_create_link(&dev->class->subsys.kset.kobj, &dev->kobj,
32423681e47SGreg Kroah-Hartman 				  dev->bus_id);
32523681e47SGreg Kroah-Hartman 
3261da177e4SLinus Torvalds 	if ((error = device_pm_add(dev)))
3271da177e4SLinus Torvalds 		goto PMError;
3281da177e4SLinus Torvalds 	if ((error = bus_add_device(dev)))
3291da177e4SLinus Torvalds 		goto BusError;
33053877d06SKay Sievers 	kobject_uevent(&dev->kobj, KOBJ_ADD);
33153877d06SKay Sievers 	bus_attach_device(dev);
3321da177e4SLinus Torvalds 	if (parent)
333d856f1e3SJames Bottomley 		klist_add_tail(&dev->knode_parent, &parent->klist_children);
3341da177e4SLinus Torvalds 
3351da177e4SLinus Torvalds 	/* notify platform of device entry */
3361da177e4SLinus Torvalds 	if (platform_notify)
3371da177e4SLinus Torvalds 		platform_notify(dev);
3381da177e4SLinus Torvalds  Done:
3391da177e4SLinus Torvalds 	put_device(dev);
3401da177e4SLinus Torvalds 	return error;
3411da177e4SLinus Torvalds  BusError:
3421da177e4SLinus Torvalds 	device_pm_remove(dev);
3431da177e4SLinus Torvalds  PMError:
34423681e47SGreg Kroah-Hartman 	if (dev->devt_attr) {
34523681e47SGreg Kroah-Hartman 		device_remove_file(dev, dev->devt_attr);
34623681e47SGreg Kroah-Hartman 		kfree(dev->devt_attr);
34723681e47SGreg Kroah-Hartman 	}
34823681e47SGreg Kroah-Hartman  attrError:
349312c004dSKay Sievers 	kobject_uevent(&dev->kobj, KOBJ_REMOVE);
3501da177e4SLinus Torvalds 	kobject_del(&dev->kobj);
3511da177e4SLinus Torvalds  Error:
3521da177e4SLinus Torvalds 	if (parent)
3531da177e4SLinus Torvalds 		put_device(parent);
3541da177e4SLinus Torvalds 	goto Done;
3551da177e4SLinus Torvalds }
3561da177e4SLinus Torvalds 
3571da177e4SLinus Torvalds 
3581da177e4SLinus Torvalds /**
3591da177e4SLinus Torvalds  *	device_register - register a device with the system.
3601da177e4SLinus Torvalds  *	@dev:	pointer to the device structure
3611da177e4SLinus Torvalds  *
3621da177e4SLinus Torvalds  *	This happens in two clean steps - initialize the device
3631da177e4SLinus Torvalds  *	and add it to the system. The two steps can be called
3641da177e4SLinus Torvalds  *	separately, but this is the easiest and most common.
3651da177e4SLinus Torvalds  *	I.e. you should only call the two helpers separately if
3661da177e4SLinus Torvalds  *	have a clearly defined need to use and refcount the device
3671da177e4SLinus Torvalds  *	before it is added to the hierarchy.
3681da177e4SLinus Torvalds  */
3691da177e4SLinus Torvalds 
3701da177e4SLinus Torvalds int device_register(struct device *dev)
3711da177e4SLinus Torvalds {
3721da177e4SLinus Torvalds 	device_initialize(dev);
3731da177e4SLinus Torvalds 	return device_add(dev);
3741da177e4SLinus Torvalds }
3751da177e4SLinus Torvalds 
3761da177e4SLinus Torvalds 
3771da177e4SLinus Torvalds /**
3781da177e4SLinus Torvalds  *	get_device - increment reference count for device.
3791da177e4SLinus Torvalds  *	@dev:	device.
3801da177e4SLinus Torvalds  *
3811da177e4SLinus Torvalds  *	This simply forwards the call to kobject_get(), though
3821da177e4SLinus Torvalds  *	we do take care to provide for the case that we get a NULL
3831da177e4SLinus Torvalds  *	pointer passed in.
3841da177e4SLinus Torvalds  */
3851da177e4SLinus Torvalds 
3861da177e4SLinus Torvalds struct device * get_device(struct device * dev)
3871da177e4SLinus Torvalds {
3881da177e4SLinus Torvalds 	return dev ? to_dev(kobject_get(&dev->kobj)) : NULL;
3891da177e4SLinus Torvalds }
3901da177e4SLinus Torvalds 
3911da177e4SLinus Torvalds 
3921da177e4SLinus Torvalds /**
3931da177e4SLinus Torvalds  *	put_device - decrement reference count.
3941da177e4SLinus Torvalds  *	@dev:	device in question.
3951da177e4SLinus Torvalds  */
3961da177e4SLinus Torvalds void put_device(struct device * dev)
3971da177e4SLinus Torvalds {
3981da177e4SLinus Torvalds 	if (dev)
3991da177e4SLinus Torvalds 		kobject_put(&dev->kobj);
4001da177e4SLinus Torvalds }
4011da177e4SLinus Torvalds 
4021da177e4SLinus Torvalds 
4031da177e4SLinus Torvalds /**
4041da177e4SLinus Torvalds  *	device_del - delete device from system.
4051da177e4SLinus Torvalds  *	@dev:	device.
4061da177e4SLinus Torvalds  *
4071da177e4SLinus Torvalds  *	This is the first part of the device unregistration
4081da177e4SLinus Torvalds  *	sequence. This removes the device from the lists we control
4091da177e4SLinus Torvalds  *	from here, has it removed from the other driver model
4101da177e4SLinus Torvalds  *	subsystems it was added to in device_add(), and removes it
4111da177e4SLinus Torvalds  *	from the kobject hierarchy.
4121da177e4SLinus Torvalds  *
4131da177e4SLinus Torvalds  *	NOTE: this should be called manually _iff_ device_add() was
4141da177e4SLinus Torvalds  *	also called manually.
4151da177e4SLinus Torvalds  */
4161da177e4SLinus Torvalds 
4171da177e4SLinus Torvalds void device_del(struct device * dev)
4181da177e4SLinus Torvalds {
4191da177e4SLinus Torvalds 	struct device * parent = dev->parent;
4201da177e4SLinus Torvalds 
4211da177e4SLinus Torvalds 	if (parent)
422d62c0f9fSPatrick Mochel 		klist_del(&dev->knode_parent);
42323681e47SGreg Kroah-Hartman 	if (dev->devt_attr)
42423681e47SGreg Kroah-Hartman 		device_remove_file(dev, dev->devt_attr);
42523681e47SGreg Kroah-Hartman 	if (dev->class)
42623681e47SGreg Kroah-Hartman 		sysfs_remove_link(&dev->class->subsys.kset.kobj, dev->bus_id);
427a7fd6706SKay Sievers 	device_remove_file(dev, &dev->uevent_attr);
4281da177e4SLinus Torvalds 
4291da177e4SLinus Torvalds 	/* Notify the platform of the removal, in case they
4301da177e4SLinus Torvalds 	 * need to do anything...
4311da177e4SLinus Torvalds 	 */
4321da177e4SLinus Torvalds 	if (platform_notify_remove)
4331da177e4SLinus Torvalds 		platform_notify_remove(dev);
4341da177e4SLinus Torvalds 	bus_remove_device(dev);
4351da177e4SLinus Torvalds 	device_pm_remove(dev);
436312c004dSKay Sievers 	kobject_uevent(&dev->kobj, KOBJ_REMOVE);
4371da177e4SLinus Torvalds 	kobject_del(&dev->kobj);
4381da177e4SLinus Torvalds 	if (parent)
4391da177e4SLinus Torvalds 		put_device(parent);
4401da177e4SLinus Torvalds }
4411da177e4SLinus Torvalds 
4421da177e4SLinus Torvalds /**
4431da177e4SLinus Torvalds  *	device_unregister - unregister device from system.
4441da177e4SLinus Torvalds  *	@dev:	device going away.
4451da177e4SLinus Torvalds  *
4461da177e4SLinus Torvalds  *	We do this in two parts, like we do device_register(). First,
4471da177e4SLinus Torvalds  *	we remove it from all the subsystems with device_del(), then
4481da177e4SLinus Torvalds  *	we decrement the reference count via put_device(). If that
4491da177e4SLinus Torvalds  *	is the final reference count, the device will be cleaned up
4501da177e4SLinus Torvalds  *	via device_release() above. Otherwise, the structure will
4511da177e4SLinus Torvalds  *	stick around until the final reference to the device is dropped.
4521da177e4SLinus Torvalds  */
4531da177e4SLinus Torvalds void device_unregister(struct device * dev)
4541da177e4SLinus Torvalds {
4551da177e4SLinus Torvalds 	pr_debug("DEV: Unregistering device. ID = '%s'\n", dev->bus_id);
4561da177e4SLinus Torvalds 	device_del(dev);
4571da177e4SLinus Torvalds 	put_device(dev);
4581da177e4SLinus Torvalds }
4591da177e4SLinus Torvalds 
4601da177e4SLinus Torvalds 
46136239577Smochel@digitalimplant.org static struct device * next_device(struct klist_iter * i)
46236239577Smochel@digitalimplant.org {
46336239577Smochel@digitalimplant.org 	struct klist_node * n = klist_next(i);
46436239577Smochel@digitalimplant.org 	return n ? container_of(n, struct device, knode_parent) : NULL;
46536239577Smochel@digitalimplant.org }
46636239577Smochel@digitalimplant.org 
4671da177e4SLinus Torvalds /**
4681da177e4SLinus Torvalds  *	device_for_each_child - device child iterator.
469c41455fbSRandy Dunlap  *	@parent: parent struct device.
4701da177e4SLinus Torvalds  *	@data:	data for the callback.
4711da177e4SLinus Torvalds  *	@fn:	function to be called for each device.
4721da177e4SLinus Torvalds  *
473c41455fbSRandy Dunlap  *	Iterate over @parent's child devices, and call @fn for each,
4741da177e4SLinus Torvalds  *	passing it @data.
4751da177e4SLinus Torvalds  *
4761da177e4SLinus Torvalds  *	We check the return of @fn each time. If it returns anything
4771da177e4SLinus Torvalds  *	other than 0, we break out and return that value.
4781da177e4SLinus Torvalds  */
47936239577Smochel@digitalimplant.org int device_for_each_child(struct device * parent, void * data,
4801da177e4SLinus Torvalds 		     int (*fn)(struct device *, void *))
4811da177e4SLinus Torvalds {
48236239577Smochel@digitalimplant.org 	struct klist_iter i;
4831da177e4SLinus Torvalds 	struct device * child;
4841da177e4SLinus Torvalds 	int error = 0;
4851da177e4SLinus Torvalds 
48636239577Smochel@digitalimplant.org 	klist_iter_init(&parent->klist_children, &i);
48736239577Smochel@digitalimplant.org 	while ((child = next_device(&i)) && !error)
48836239577Smochel@digitalimplant.org 		error = fn(child, data);
48936239577Smochel@digitalimplant.org 	klist_iter_exit(&i);
4901da177e4SLinus Torvalds 	return error;
4911da177e4SLinus Torvalds }
4921da177e4SLinus Torvalds 
4931da177e4SLinus Torvalds int __init devices_init(void)
4941da177e4SLinus Torvalds {
4951da177e4SLinus Torvalds 	return subsystem_register(&devices_subsys);
4961da177e4SLinus Torvalds }
4971da177e4SLinus Torvalds 
4981da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(device_for_each_child);
4991da177e4SLinus Torvalds 
5001da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(device_initialize);
5011da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(device_add);
5021da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(device_register);
5031da177e4SLinus Torvalds 
5041da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(device_del);
5051da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(device_unregister);
5061da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(get_device);
5071da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(put_device);
5081da177e4SLinus Torvalds 
5091da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(device_create_file);
5101da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(device_remove_file);
51123681e47SGreg Kroah-Hartman 
51223681e47SGreg Kroah-Hartman 
51323681e47SGreg Kroah-Hartman static void device_create_release(struct device *dev)
51423681e47SGreg Kroah-Hartman {
51523681e47SGreg Kroah-Hartman 	pr_debug("%s called for %s\n", __FUNCTION__, dev->bus_id);
51623681e47SGreg Kroah-Hartman 	kfree(dev);
51723681e47SGreg Kroah-Hartman }
51823681e47SGreg Kroah-Hartman 
51923681e47SGreg Kroah-Hartman /**
52023681e47SGreg Kroah-Hartman  * device_create - creates a device and registers it with sysfs
52123681e47SGreg Kroah-Hartman  * @cs: pointer to the struct class that this device should be registered to.
52223681e47SGreg Kroah-Hartman  * @parent: pointer to the parent struct device of this new device, if any.
52323681e47SGreg Kroah-Hartman  * @dev: the dev_t for the char device to be added.
52423681e47SGreg Kroah-Hartman  * @fmt: string for the class device's name
52523681e47SGreg Kroah-Hartman  *
52623681e47SGreg Kroah-Hartman  * This function can be used by char device classes.  A struct
52723681e47SGreg Kroah-Hartman  * device will be created in sysfs, registered to the specified
52823681e47SGreg Kroah-Hartman  * class.
52923681e47SGreg Kroah-Hartman  * A "dev" file will be created, showing the dev_t for the device, if
53023681e47SGreg Kroah-Hartman  * the dev_t is not 0,0.
53123681e47SGreg Kroah-Hartman  * If a pointer to a parent struct device is passed in, the newly
53223681e47SGreg Kroah-Hartman  * created struct device will be a child of that device in sysfs.  The
53323681e47SGreg Kroah-Hartman  * pointer to the struct device will be returned from the call.  Any
53423681e47SGreg Kroah-Hartman  * further sysfs files that might be required can be created using this
53523681e47SGreg Kroah-Hartman  * pointer.
53623681e47SGreg Kroah-Hartman  *
53723681e47SGreg Kroah-Hartman  * Note: the struct class passed to this function must have previously
53823681e47SGreg Kroah-Hartman  * been created with a call to class_create().
53923681e47SGreg Kroah-Hartman  */
54023681e47SGreg Kroah-Hartman struct device *device_create(struct class *class, struct device *parent,
54123681e47SGreg Kroah-Hartman 			     dev_t devt, char *fmt, ...)
54223681e47SGreg Kroah-Hartman {
54323681e47SGreg Kroah-Hartman 	va_list args;
54423681e47SGreg Kroah-Hartman 	struct device *dev = NULL;
54523681e47SGreg Kroah-Hartman 	int retval = -ENODEV;
54623681e47SGreg Kroah-Hartman 
54723681e47SGreg Kroah-Hartman 	if (class == NULL || IS_ERR(class))
54823681e47SGreg Kroah-Hartman 		goto error;
54923681e47SGreg Kroah-Hartman 	if (parent == NULL) {
55023681e47SGreg Kroah-Hartman 		printk(KERN_WARNING "%s does not work yet for NULL parents\n", __FUNCTION__);
55123681e47SGreg Kroah-Hartman 		goto error;
55223681e47SGreg Kroah-Hartman 	}
55323681e47SGreg Kroah-Hartman 
55423681e47SGreg Kroah-Hartman 	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
55523681e47SGreg Kroah-Hartman 	if (!dev) {
55623681e47SGreg Kroah-Hartman 		retval = -ENOMEM;
55723681e47SGreg Kroah-Hartman 		goto error;
55823681e47SGreg Kroah-Hartman 	}
55923681e47SGreg Kroah-Hartman 
56023681e47SGreg Kroah-Hartman 	dev->devt = devt;
56123681e47SGreg Kroah-Hartman 	dev->class = class;
56223681e47SGreg Kroah-Hartman 	dev->parent = parent;
56323681e47SGreg Kroah-Hartman 	dev->release = device_create_release;
56423681e47SGreg Kroah-Hartman 
56523681e47SGreg Kroah-Hartman 	va_start(args, fmt);
56623681e47SGreg Kroah-Hartman 	vsnprintf(dev->bus_id, BUS_ID_SIZE, fmt, args);
56723681e47SGreg Kroah-Hartman 	va_end(args);
56823681e47SGreg Kroah-Hartman 	retval = device_register(dev);
56923681e47SGreg Kroah-Hartman 	if (retval)
57023681e47SGreg Kroah-Hartman 		goto error;
57123681e47SGreg Kroah-Hartman 
57223681e47SGreg Kroah-Hartman 	/* tie the class to the device */
57323681e47SGreg Kroah-Hartman 	down(&class->sem);
57423681e47SGreg Kroah-Hartman 	list_add_tail(&dev->node, &class->devices);
57523681e47SGreg Kroah-Hartman 	up(&class->sem);
57623681e47SGreg Kroah-Hartman 
57723681e47SGreg Kroah-Hartman 	return dev;
57823681e47SGreg Kroah-Hartman 
57923681e47SGreg Kroah-Hartman error:
58023681e47SGreg Kroah-Hartman 	kfree(dev);
58123681e47SGreg Kroah-Hartman 	return ERR_PTR(retval);
58223681e47SGreg Kroah-Hartman }
58323681e47SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(device_create);
58423681e47SGreg Kroah-Hartman 
58523681e47SGreg Kroah-Hartman /**
58623681e47SGreg Kroah-Hartman  * device_destroy - removes a device that was created with device_create()
58723681e47SGreg Kroah-Hartman  * @class: the pointer to the struct class that this device was registered * with.
58823681e47SGreg Kroah-Hartman  * @dev: the dev_t of the device that was previously registered.
58923681e47SGreg Kroah-Hartman  *
59023681e47SGreg Kroah-Hartman  * This call unregisters and cleans up a class device that was created with a
59123681e47SGreg Kroah-Hartman  * call to class_device_create()
59223681e47SGreg Kroah-Hartman  */
59323681e47SGreg Kroah-Hartman void device_destroy(struct class *class, dev_t devt)
59423681e47SGreg Kroah-Hartman {
59523681e47SGreg Kroah-Hartman 	struct device *dev = NULL;
59623681e47SGreg Kroah-Hartman 	struct device *dev_tmp;
59723681e47SGreg Kroah-Hartman 
59823681e47SGreg Kroah-Hartman 	down(&class->sem);
59923681e47SGreg Kroah-Hartman 	list_for_each_entry(dev_tmp, &class->devices, node) {
60023681e47SGreg Kroah-Hartman 		if (dev_tmp->devt == devt) {
60123681e47SGreg Kroah-Hartman 			dev = dev_tmp;
60223681e47SGreg Kroah-Hartman 			break;
60323681e47SGreg Kroah-Hartman 		}
60423681e47SGreg Kroah-Hartman 	}
60523681e47SGreg Kroah-Hartman 	up(&class->sem);
60623681e47SGreg Kroah-Hartman 
60723681e47SGreg Kroah-Hartman 	if (dev) {
60823681e47SGreg Kroah-Hartman 		list_del_init(&dev->node);
60923681e47SGreg Kroah-Hartman 		device_unregister(dev);
61023681e47SGreg Kroah-Hartman 	}
61123681e47SGreg Kroah-Hartman }
61223681e47SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(device_destroy);
613