blob: 4d59975c24a850c4d134a178211ae9ab520730f7 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * drivers/base/core.c - core driver model code (device registration, etc)
3 *
4 * Copyright (c) 2002-3 Patrick Mochel
5 * Copyright (c) 2002-3 Open Source Development Labs
Greg Kroah-Hartman64bb5d22006-06-28 16:19:58 -07006 * Copyright (c) 2006 Greg Kroah-Hartman <gregkh@suse.de>
7 * Copyright (c) 2006 Novell, Inc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 *
9 * This file is released under the GPLv2
10 *
11 */
12
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/device.h>
14#include <linux/err.h>
15#include <linux/init.h>
16#include <linux/module.h>
17#include <linux/slab.h>
18#include <linux/string.h>
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -070019#include <linux/kdev_t.h>
Benjamin Herrenschmidt116af372006-10-25 13:44:59 +100020#include <linux/notifier.h>
Kay Sieversda231fd2007-11-21 17:29:15 +010021#include <linux/genhd.h>
Andrew Morton815d2d52008-03-04 15:09:07 -080022#include <linux/kallsyms.h>
Matthew Wilcox6188e102008-04-18 22:21:05 -040023#include <linux/semaphore.h>
Dave Youngf75b1c62008-05-28 09:28:39 -070024#include <linux/mutex.h>
Shaohua Li401097e2009-05-12 13:37:57 -070025#include <linux/async.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
27#include "base.h"
28#include "power/power.h"
29
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -080030int (*platform_notify)(struct device *dev) = NULL;
31int (*platform_notify_remove)(struct device *dev) = NULL;
Dan Williamse105b8b2008-04-21 10:51:07 -070032static struct kobject *dev_kobj;
33struct kobject *sysfs_dev_char_kobj;
34struct kobject *sysfs_dev_block_kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
Greg Kroah-Hartman4e886c22008-01-27 12:12:43 -080036#ifdef CONFIG_BLOCK
37static inline int device_is_not_partition(struct device *dev)
38{
39 return !(dev->type == &part_type);
40}
41#else
42static inline int device_is_not_partition(struct device *dev)
43{
44 return 1;
45}
46#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Alan Stern3e956372006-06-16 17:10:48 -040048/**
49 * dev_driver_string - Return a device's driver name, if at all possible
50 * @dev: struct device to get the name of
51 *
52 * Will return the device's driver's name if it is bound to a device. If
53 * the device is not bound to a device, it will return the name of the bus
54 * it is attached to. If it is not attached to a bus either, an empty
55 * string will be returned.
56 */
Jean Delvarebf9ca692008-07-30 12:29:21 -070057const char *dev_driver_string(const struct device *dev)
Alan Stern3e956372006-06-16 17:10:48 -040058{
59 return dev->driver ? dev->driver->name :
Jean Delvarea456b702007-03-09 16:33:10 +010060 (dev->bus ? dev->bus->name :
61 (dev->class ? dev->class->name : ""));
Alan Stern3e956372006-06-16 17:10:48 -040062}
Matthew Wilcox310a9222006-09-23 23:35:04 -060063EXPORT_SYMBOL(dev_driver_string);
Alan Stern3e956372006-06-16 17:10:48 -040064
Linus Torvalds1da177e2005-04-16 15:20:36 -070065#define to_dev(obj) container_of(obj, struct device, kobj)
66#define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr)
67
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -080068static ssize_t dev_attr_show(struct kobject *kobj, struct attribute *attr,
69 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070070{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -080071 struct device_attribute *dev_attr = to_dev_attr(attr);
72 struct device *dev = to_dev(kobj);
Dmitry Torokhov4a0c20b2005-04-29 01:23:47 -050073 ssize_t ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
75 if (dev_attr->show)
Yani Ioannou54b6f352005-05-17 06:39:34 -040076 ret = dev_attr->show(dev, dev_attr, buf);
Andrew Morton815d2d52008-03-04 15:09:07 -080077 if (ret >= (ssize_t)PAGE_SIZE) {
78 print_symbol("dev_attr_show: %s returned bad count\n",
79 (unsigned long)dev_attr->show);
80 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 return ret;
82}
83
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -080084static ssize_t dev_attr_store(struct kobject *kobj, struct attribute *attr,
85 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -070086{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -080087 struct device_attribute *dev_attr = to_dev_attr(attr);
88 struct device *dev = to_dev(kobj);
Dmitry Torokhov4a0c20b2005-04-29 01:23:47 -050089 ssize_t ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
91 if (dev_attr->store)
Yani Ioannou54b6f352005-05-17 06:39:34 -040092 ret = dev_attr->store(dev, dev_attr, buf, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 return ret;
94}
95
96static struct sysfs_ops dev_sysfs_ops = {
97 .show = dev_attr_show,
98 .store = dev_attr_store,
99};
100
101
102/**
103 * device_release - free device structure.
104 * @kobj: device's kobject.
105 *
106 * This is called once the reference count for the object
107 * reaches 0. We forward the call to the device's release
108 * method, which should handle actually freeing the structure.
109 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800110static void device_release(struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800112 struct device *dev = to_dev(kobj);
Greg Kroah-Hartmanfb069a52008-12-16 12:23:36 -0800113 struct device_private *p = dev->p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
115 if (dev->release)
116 dev->release(dev);
Kay Sieversf9f852d2006-10-07 21:54:55 +0200117 else if (dev->type && dev->type->release)
118 dev->type->release(dev);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700119 else if (dev->class && dev->class->dev_release)
120 dev->class->dev_release(dev);
Arjan van de Venf810a5c2008-07-25 19:45:39 -0700121 else
122 WARN(1, KERN_ERR "Device '%s' does not have a release() "
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800123 "function, it is broken and must be fixed.\n",
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100124 dev_name(dev));
Greg Kroah-Hartmanfb069a52008-12-16 12:23:36 -0800125 kfree(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126}
127
Greg Kroah-Hartman8f4afc42007-10-11 10:47:49 -0600128static struct kobj_type device_ktype = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 .release = device_release,
130 .sysfs_ops = &dev_sysfs_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131};
132
133
Kay Sievers312c0042005-11-16 09:00:00 +0100134static int dev_uevent_filter(struct kset *kset, struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135{
136 struct kobj_type *ktype = get_ktype(kobj);
137
Greg Kroah-Hartman8f4afc42007-10-11 10:47:49 -0600138 if (ktype == &device_ktype) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 struct device *dev = to_dev(kobj);
140 if (dev->bus)
141 return 1;
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700142 if (dev->class)
143 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 }
145 return 0;
146}
147
Kay Sievers312c0042005-11-16 09:00:00 +0100148static const char *dev_uevent_name(struct kset *kset, struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149{
150 struct device *dev = to_dev(kobj);
151
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700152 if (dev->bus)
153 return dev->bus->name;
154 if (dev->class)
155 return dev->class->name;
156 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157}
158
Kay Sievers7eff2e72007-08-14 15:15:12 +0200159static int dev_uevent(struct kset *kset, struct kobject *kobj,
160 struct kobj_uevent_env *env)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161{
162 struct device *dev = to_dev(kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 int retval = 0;
164
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700165 /* add the major/minor if present */
166 if (MAJOR(dev->devt)) {
Kay Sievers7eff2e72007-08-14 15:15:12 +0200167 add_uevent_var(env, "MAJOR=%u", MAJOR(dev->devt));
168 add_uevent_var(env, "MINOR=%u", MINOR(dev->devt));
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700169 }
170
Kay Sievers414264f2007-03-12 21:08:57 +0100171 if (dev->type && dev->type->name)
Kay Sievers7eff2e72007-08-14 15:15:12 +0200172 add_uevent_var(env, "DEVTYPE=%s", dev->type->name);
Kay Sievers414264f2007-03-12 21:08:57 +0100173
Kay Sievers239378f2006-10-07 21:54:55 +0200174 if (dev->driver)
Kay Sievers7eff2e72007-08-14 15:15:12 +0200175 add_uevent_var(env, "DRIVER=%s", dev->driver->name);
Kay Sievers239378f2006-10-07 21:54:55 +0200176
Kay Sieversa87cb2a2006-09-14 11:23:28 +0200177#ifdef CONFIG_SYSFS_DEPRECATED
Kay Sievers239378f2006-10-07 21:54:55 +0200178 if (dev->class) {
179 struct device *parent = dev->parent;
180
181 /* find first bus device in parent chain */
182 while (parent && !parent->bus)
183 parent = parent->parent;
184 if (parent && parent->bus) {
185 const char *path;
186
187 path = kobject_get_path(&parent->kobj, GFP_KERNEL);
Kay Sievers2c7afd12007-05-01 13:46:26 +0200188 if (path) {
Kay Sievers7eff2e72007-08-14 15:15:12 +0200189 add_uevent_var(env, "PHYSDEVPATH=%s", path);
Kay Sievers2c7afd12007-05-01 13:46:26 +0200190 kfree(path);
191 }
Kay Sievers239378f2006-10-07 21:54:55 +0200192
Kay Sievers7eff2e72007-08-14 15:15:12 +0200193 add_uevent_var(env, "PHYSDEVBUS=%s", parent->bus->name);
Kay Sievers239378f2006-10-07 21:54:55 +0200194
195 if (parent->driver)
Kay Sievers7eff2e72007-08-14 15:15:12 +0200196 add_uevent_var(env, "PHYSDEVDRIVER=%s",
197 parent->driver->name);
Kay Sievers239378f2006-10-07 21:54:55 +0200198 }
199 } else if (dev->bus) {
Kay Sievers7eff2e72007-08-14 15:15:12 +0200200 add_uevent_var(env, "PHYSDEVBUS=%s", dev->bus->name);
Kay Sievers239378f2006-10-07 21:54:55 +0200201
202 if (dev->driver)
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800203 add_uevent_var(env, "PHYSDEVDRIVER=%s",
204 dev->driver->name);
Kay Sieversd81d9d62006-08-13 06:17:09 +0200205 }
Kay Sievers239378f2006-10-07 21:54:55 +0200206#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
Kay Sievers7eff2e72007-08-14 15:15:12 +0200208 /* have the bus specific function add its stuff */
Kay Sievers312c0042005-11-16 09:00:00 +0100209 if (dev->bus && dev->bus->uevent) {
Kay Sievers7eff2e72007-08-14 15:15:12 +0200210 retval = dev->bus->uevent(dev, env);
Kay Sieversf9f852d2006-10-07 21:54:55 +0200211 if (retval)
Greg Kroah-Hartman7dc72b22007-11-28 23:49:41 -0800212 pr_debug("device: '%s': %s: bus uevent() returned %d\n",
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100213 dev_name(dev), __func__, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 }
215
Kay Sievers7eff2e72007-08-14 15:15:12 +0200216 /* have the class specific function add its stuff */
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700217 if (dev->class && dev->class->dev_uevent) {
Kay Sievers7eff2e72007-08-14 15:15:12 +0200218 retval = dev->class->dev_uevent(dev, env);
Kay Sieversf9f852d2006-10-07 21:54:55 +0200219 if (retval)
Greg Kroah-Hartman7dc72b22007-11-28 23:49:41 -0800220 pr_debug("device: '%s': %s: class uevent() "
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100221 "returned %d\n", dev_name(dev),
Harvey Harrison2b3a3022008-03-04 16:41:05 -0800222 __func__, retval);
Kay Sieversf9f852d2006-10-07 21:54:55 +0200223 }
224
Kay Sievers7eff2e72007-08-14 15:15:12 +0200225 /* have the device type specific fuction add its stuff */
Kay Sieversf9f852d2006-10-07 21:54:55 +0200226 if (dev->type && dev->type->uevent) {
Kay Sievers7eff2e72007-08-14 15:15:12 +0200227 retval = dev->type->uevent(dev, env);
Kay Sieversf9f852d2006-10-07 21:54:55 +0200228 if (retval)
Greg Kroah-Hartman7dc72b22007-11-28 23:49:41 -0800229 pr_debug("device: '%s': %s: dev_type uevent() "
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100230 "returned %d\n", dev_name(dev),
Harvey Harrison2b3a3022008-03-04 16:41:05 -0800231 __func__, retval);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700232 }
233
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 return retval;
235}
236
Kay Sievers312c0042005-11-16 09:00:00 +0100237static struct kset_uevent_ops device_uevent_ops = {
238 .filter = dev_uevent_filter,
239 .name = dev_uevent_name,
240 .uevent = dev_uevent,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241};
242
Kay Sievers16574dc2007-04-06 01:40:38 +0200243static ssize_t show_uevent(struct device *dev, struct device_attribute *attr,
244 char *buf)
245{
246 struct kobject *top_kobj;
247 struct kset *kset;
Kay Sievers7eff2e72007-08-14 15:15:12 +0200248 struct kobj_uevent_env *env = NULL;
Kay Sievers16574dc2007-04-06 01:40:38 +0200249 int i;
250 size_t count = 0;
251 int retval;
252
253 /* search the kset, the device belongs to */
254 top_kobj = &dev->kobj;
Kay Sievers5c5daf62007-08-12 20:43:55 +0200255 while (!top_kobj->kset && top_kobj->parent)
256 top_kobj = top_kobj->parent;
Kay Sievers16574dc2007-04-06 01:40:38 +0200257 if (!top_kobj->kset)
258 goto out;
Kay Sievers5c5daf62007-08-12 20:43:55 +0200259
Kay Sievers16574dc2007-04-06 01:40:38 +0200260 kset = top_kobj->kset;
261 if (!kset->uevent_ops || !kset->uevent_ops->uevent)
262 goto out;
263
264 /* respect filter */
265 if (kset->uevent_ops && kset->uevent_ops->filter)
266 if (!kset->uevent_ops->filter(kset, &dev->kobj))
267 goto out;
268
Kay Sievers7eff2e72007-08-14 15:15:12 +0200269 env = kzalloc(sizeof(struct kobj_uevent_env), GFP_KERNEL);
270 if (!env)
Greg Kroah-Hartmanc7308c82007-05-02 14:14:11 +0200271 return -ENOMEM;
272
Kay Sievers16574dc2007-04-06 01:40:38 +0200273 /* let the kset specific function add its keys */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200274 retval = kset->uevent_ops->uevent(kset, &dev->kobj, env);
Kay Sievers16574dc2007-04-06 01:40:38 +0200275 if (retval)
276 goto out;
277
278 /* copy keys to file */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200279 for (i = 0; i < env->envp_idx; i++)
280 count += sprintf(&buf[count], "%s\n", env->envp[i]);
Kay Sievers16574dc2007-04-06 01:40:38 +0200281out:
Kay Sievers7eff2e72007-08-14 15:15:12 +0200282 kfree(env);
Kay Sievers16574dc2007-04-06 01:40:38 +0200283 return count;
284}
285
Kay Sieversa7fd6702005-10-01 14:49:43 +0200286static ssize_t store_uevent(struct device *dev, struct device_attribute *attr,
287 const char *buf, size_t count)
288{
Kay Sievers60a96a52007-07-08 22:29:26 +0200289 enum kobject_action action;
290
Kay Sievers5c5daf62007-08-12 20:43:55 +0200291 if (kobject_action_type(buf, count, &action) == 0) {
Kay Sievers60a96a52007-07-08 22:29:26 +0200292 kobject_uevent(&dev->kobj, action);
293 goto out;
294 }
295
296 dev_err(dev, "uevent: unsupported action-string; this will "
297 "be ignored in a future kernel version\n");
Kay Sievers312c0042005-11-16 09:00:00 +0100298 kobject_uevent(&dev->kobj, KOBJ_ADD);
Kay Sievers60a96a52007-07-08 22:29:26 +0200299out:
Kay Sieversa7fd6702005-10-01 14:49:43 +0200300 return count;
301}
302
Tejun Heoad6a1e12007-06-14 03:45:17 +0900303static struct device_attribute uevent_attr =
304 __ATTR(uevent, S_IRUGO | S_IWUSR, show_uevent, store_uevent);
305
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500306static int device_add_attributes(struct device *dev,
307 struct device_attribute *attrs)
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -0700308{
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -0700309 int error = 0;
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500310 int i;
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -0700311
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500312 if (attrs) {
313 for (i = 0; attr_name(attrs[i]); i++) {
314 error = device_create_file(dev, &attrs[i]);
315 if (error)
316 break;
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -0700317 }
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500318 if (error)
319 while (--i >= 0)
320 device_remove_file(dev, &attrs[i]);
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -0700321 }
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -0700322 return error;
323}
324
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500325static void device_remove_attributes(struct device *dev,
326 struct device_attribute *attrs)
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -0700327{
328 int i;
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500329
330 if (attrs)
331 for (i = 0; attr_name(attrs[i]); i++)
332 device_remove_file(dev, &attrs[i]);
333}
334
335static int device_add_groups(struct device *dev,
336 struct attribute_group **groups)
337{
338 int error = 0;
339 int i;
340
341 if (groups) {
342 for (i = 0; groups[i]; i++) {
343 error = sysfs_create_group(&dev->kobj, groups[i]);
344 if (error) {
345 while (--i >= 0)
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800346 sysfs_remove_group(&dev->kobj,
347 groups[i]);
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500348 break;
349 }
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -0700350 }
351 }
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500352 return error;
353}
354
355static void device_remove_groups(struct device *dev,
356 struct attribute_group **groups)
357{
358 int i;
359
360 if (groups)
361 for (i = 0; groups[i]; i++)
362 sysfs_remove_group(&dev->kobj, groups[i]);
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -0700363}
364
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700365static int device_add_attrs(struct device *dev)
366{
367 struct class *class = dev->class;
Kay Sieversf9f852d2006-10-07 21:54:55 +0200368 struct device_type *type = dev->type;
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500369 int error;
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700370
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500371 if (class) {
372 error = device_add_attributes(dev, class->dev_attrs);
Kay Sieversf9f852d2006-10-07 21:54:55 +0200373 if (error)
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500374 return error;
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700375 }
Kay Sieversf9f852d2006-10-07 21:54:55 +0200376
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500377 if (type) {
378 error = device_add_groups(dev, type->groups);
Kay Sieversf9f852d2006-10-07 21:54:55 +0200379 if (error)
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500380 goto err_remove_class_attrs;
Kay Sieversf9f852d2006-10-07 21:54:55 +0200381 }
382
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500383 error = device_add_groups(dev, dev->groups);
384 if (error)
385 goto err_remove_type_groups;
386
387 return 0;
388
389 err_remove_type_groups:
390 if (type)
391 device_remove_groups(dev, type->groups);
392 err_remove_class_attrs:
393 if (class)
394 device_remove_attributes(dev, class->dev_attrs);
395
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700396 return error;
397}
398
399static void device_remove_attrs(struct device *dev)
400{
401 struct class *class = dev->class;
Kay Sieversf9f852d2006-10-07 21:54:55 +0200402 struct device_type *type = dev->type;
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700403
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500404 device_remove_groups(dev, dev->groups);
Kay Sieversf9f852d2006-10-07 21:54:55 +0200405
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500406 if (type)
407 device_remove_groups(dev, type->groups);
408
409 if (class)
410 device_remove_attributes(dev, class->dev_attrs);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700411}
412
413
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700414static ssize_t show_dev(struct device *dev, struct device_attribute *attr,
415 char *buf)
416{
417 return print_dev_t(buf, dev->devt);
418}
419
Tejun Heoad6a1e12007-06-14 03:45:17 +0900420static struct device_attribute devt_attr =
421 __ATTR(dev, S_IRUGO, show_dev, NULL);
422
Greg Kroah-Hartman881c6cfd2007-11-01 09:29:06 -0600423/* kset to create /sys/devices/ */
424struct kset *devices_kset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800427 * device_create_file - create sysfs attribute file for device.
428 * @dev: device.
429 * @attr: device attribute descriptor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800431int device_create_file(struct device *dev, struct device_attribute *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432{
433 int error = 0;
Cornelia Huck0c98b192008-01-31 10:39:38 +0100434 if (dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 error = sysfs_create_file(&dev->kobj, &attr->attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 return error;
437}
438
439/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800440 * device_remove_file - remove sysfs attribute file.
441 * @dev: device.
442 * @attr: device attribute descriptor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800444void device_remove_file(struct device *dev, struct device_attribute *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445{
Cornelia Huck0c98b192008-01-31 10:39:38 +0100446 if (dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 sysfs_remove_file(&dev->kobj, &attr->attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448}
449
Greg Kroah-Hartman2589f1882006-09-19 09:39:19 -0700450/**
451 * device_create_bin_file - create sysfs binary attribute file for device.
452 * @dev: device.
453 * @attr: device binary attribute descriptor.
454 */
455int device_create_bin_file(struct device *dev, struct bin_attribute *attr)
456{
457 int error = -EINVAL;
458 if (dev)
459 error = sysfs_create_bin_file(&dev->kobj, attr);
460 return error;
461}
462EXPORT_SYMBOL_GPL(device_create_bin_file);
463
464/**
465 * device_remove_bin_file - remove sysfs binary attribute file
466 * @dev: device.
467 * @attr: device binary attribute descriptor.
468 */
469void device_remove_bin_file(struct device *dev, struct bin_attribute *attr)
470{
471 if (dev)
472 sysfs_remove_bin_file(&dev->kobj, attr);
473}
474EXPORT_SYMBOL_GPL(device_remove_bin_file);
475
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400476/**
Alan Stern523ded72007-04-26 00:12:04 -0700477 * device_schedule_callback_owner - helper to schedule a callback for a device
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400478 * @dev: device.
479 * @func: callback function to invoke later.
Alan Stern523ded72007-04-26 00:12:04 -0700480 * @owner: module owning the callback routine
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400481 *
482 * Attribute methods must not unregister themselves or their parent device
483 * (which would amount to the same thing). Attempts to do so will deadlock,
484 * since unregistration is mutually exclusive with driver callbacks.
485 *
486 * Instead methods can call this routine, which will attempt to allocate
487 * and schedule a workqueue request to call back @func with @dev as its
488 * argument in the workqueue's process context. @dev will be pinned until
489 * @func returns.
490 *
Alan Stern523ded72007-04-26 00:12:04 -0700491 * This routine is usually called via the inline device_schedule_callback(),
492 * which automatically sets @owner to THIS_MODULE.
493 *
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400494 * Returns 0 if the request was submitted, -ENOMEM if storage could not
Alan Stern523ded72007-04-26 00:12:04 -0700495 * be allocated, -ENODEV if a reference to @owner isn't available.
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400496 *
497 * NOTE: This routine won't work if CONFIG_SYSFS isn't set! It uses an
498 * underlying sysfs routine (since it is intended for use by attribute
499 * methods), and if sysfs isn't available you'll get nothing but -ENOSYS.
500 */
Alan Stern523ded72007-04-26 00:12:04 -0700501int device_schedule_callback_owner(struct device *dev,
502 void (*func)(struct device *), struct module *owner)
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400503{
504 return sysfs_schedule_callback(&dev->kobj,
Alan Stern523ded72007-04-26 00:12:04 -0700505 (void (*)(void *)) func, dev, owner);
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400506}
Alan Stern523ded72007-04-26 00:12:04 -0700507EXPORT_SYMBOL_GPL(device_schedule_callback_owner);
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400508
James Bottomley34bb61f2005-09-06 16:56:51 -0700509static void klist_children_get(struct klist_node *n)
510{
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -0800511 struct device_private *p = to_device_private_parent(n);
512 struct device *dev = p->device;
James Bottomley34bb61f2005-09-06 16:56:51 -0700513
514 get_device(dev);
515}
516
517static void klist_children_put(struct klist_node *n)
518{
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -0800519 struct device_private *p = to_device_private_parent(n);
520 struct device *dev = p->device;
James Bottomley34bb61f2005-09-06 16:56:51 -0700521
522 put_device(dev);
523}
524
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800526 * device_initialize - init device structure.
527 * @dev: device.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 *
Cornelia Huck57394112008-09-03 18:26:40 +0200529 * This prepares the device for use by other layers by initializing
530 * its fields.
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800531 * It is the first half of device_register(), if called by
Cornelia Huck57394112008-09-03 18:26:40 +0200532 * that function, though it can also be called separately, so one
533 * may use @dev's fields. In particular, get_device()/put_device()
534 * may be used for reference counting of @dev after calling this
535 * function.
536 *
537 * NOTE: Use put_device() to give up your reference instead of freeing
538 * @dev directly once you have called this function.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540void device_initialize(struct device *dev)
541{
Greg Kroah-Hartman881c6cfd2007-11-01 09:29:06 -0600542 dev->kobj.kset = devices_kset;
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -0700543 kobject_init(&dev->kobj, &device_ktype);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 INIT_LIST_HEAD(&dev->dma_pools);
mochel@digitalimplant.orgaf703162005-03-21 10:41:04 -0800545 init_MUTEX(&dev->sem);
Tejun Heo9ac78492007-01-20 16:00:26 +0900546 spin_lock_init(&dev->devres_lock);
547 INIT_LIST_HEAD(&dev->devres_head);
David Brownell0ac85242005-09-12 19:39:34 -0700548 device_init_wakeup(dev, 0);
Alan Stern3b98aea2008-08-07 13:06:12 -0400549 device_pm_init(dev);
Christoph Hellwig87348132006-12-06 20:32:33 -0800550 set_dev_node(dev, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551}
552
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +0100553#ifdef CONFIG_SYSFS_DEPRECATED
Kay Sieversda231fd2007-11-21 17:29:15 +0100554static struct kobject *get_device_parent(struct device *dev,
555 struct device *parent)
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +0100556{
Kay Sieversda231fd2007-11-21 17:29:15 +0100557 /* class devices without a parent live in /sys/class/<classname>/ */
Dmitry Torokhov3eb215d2007-10-07 12:22:21 -0400558 if (dev->class && (!parent || parent->class != dev->class))
Greg Kroah-Hartman1fbfee62008-05-28 09:28:39 -0700559 return &dev->class->p->class_subsys.kobj;
Kay Sieversda231fd2007-11-21 17:29:15 +0100560 /* all other devices keep their parent */
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +0100561 else if (parent)
Cornelia Huckc744aeae2007-01-08 20:16:44 +0100562 return &parent->kobj;
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +0100563
Cornelia Huckc744aeae2007-01-08 20:16:44 +0100564 return NULL;
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +0100565}
Kay Sieversda231fd2007-11-21 17:29:15 +0100566
567static inline void cleanup_device_parent(struct device *dev) {}
Cornelia Huck63b69712008-01-21 16:09:44 +0100568static inline void cleanup_glue_dir(struct device *dev,
569 struct kobject *glue_dir) {}
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +0100570#else
Kay Sievers86406242007-03-14 03:25:56 +0100571static struct kobject *virtual_device_parent(struct device *dev)
Greg Kroah-Hartmanf0ee61a2006-10-23 10:40:54 -0700572{
Kay Sievers86406242007-03-14 03:25:56 +0100573 static struct kobject *virtual_dir = NULL;
Greg Kroah-Hartmanf0ee61a2006-10-23 10:40:54 -0700574
Kay Sievers86406242007-03-14 03:25:56 +0100575 if (!virtual_dir)
Greg Kroah-Hartman4ff6abf2007-11-05 22:24:43 -0800576 virtual_dir = kobject_create_and_add("virtual",
Greg Kroah-Hartman881c6cfd2007-11-01 09:29:06 -0600577 &devices_kset->kobj);
Greg Kroah-Hartmanf0ee61a2006-10-23 10:40:54 -0700578
Kay Sievers86406242007-03-14 03:25:56 +0100579 return virtual_dir;
Greg Kroah-Hartmanf0ee61a2006-10-23 10:40:54 -0700580}
581
Kay Sieversda231fd2007-11-21 17:29:15 +0100582static struct kobject *get_device_parent(struct device *dev,
583 struct device *parent)
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +0100584{
Greg Kroah-Hartman43968d22007-11-05 22:24:43 -0800585 int retval;
586
Kay Sievers86406242007-03-14 03:25:56 +0100587 if (dev->class) {
588 struct kobject *kobj = NULL;
589 struct kobject *parent_kobj;
590 struct kobject *k;
591
592 /*
593 * If we have no parent, we live in "virtual".
Kay Sievers0f4dafc2007-12-19 01:40:42 +0100594 * Class-devices with a non class-device as parent, live
595 * in a "glue" directory to prevent namespace collisions.
Kay Sievers86406242007-03-14 03:25:56 +0100596 */
597 if (parent == NULL)
598 parent_kobj = virtual_device_parent(dev);
599 else if (parent->class)
600 return &parent->kobj;
601 else
602 parent_kobj = &parent->kobj;
603
604 /* find our class-directory at the parent and reference it */
Greg Kroah-Hartman7c714482008-01-22 18:17:41 -0500605 spin_lock(&dev->class->p->class_dirs.list_lock);
606 list_for_each_entry(k, &dev->class->p->class_dirs.list, entry)
Kay Sievers86406242007-03-14 03:25:56 +0100607 if (k->parent == parent_kobj) {
608 kobj = kobject_get(k);
609 break;
610 }
Greg Kroah-Hartman7c714482008-01-22 18:17:41 -0500611 spin_unlock(&dev->class->p->class_dirs.list_lock);
Kay Sievers86406242007-03-14 03:25:56 +0100612 if (kobj)
613 return kobj;
614
615 /* or create a new class-directory at the parent device */
Greg Kroah-Hartman43968d22007-11-05 22:24:43 -0800616 k = kobject_create();
617 if (!k)
618 return NULL;
Greg Kroah-Hartman7c714482008-01-22 18:17:41 -0500619 k->kset = &dev->class->p->class_dirs;
Greg Kroah-Hartmanb2d6db52007-12-17 23:05:35 -0700620 retval = kobject_add(k, parent_kobj, "%s", dev->class->name);
Greg Kroah-Hartman43968d22007-11-05 22:24:43 -0800621 if (retval < 0) {
622 kobject_put(k);
623 return NULL;
624 }
Kay Sievers0f4dafc2007-12-19 01:40:42 +0100625 /* do not emit an uevent for this simple "glue" directory */
Greg Kroah-Hartman43968d22007-11-05 22:24:43 -0800626 return k;
Kay Sievers86406242007-03-14 03:25:56 +0100627 }
628
629 if (parent)
Cornelia Huckc744aeae2007-01-08 20:16:44 +0100630 return &parent->kobj;
631 return NULL;
632}
Kay Sieversda231fd2007-11-21 17:29:15 +0100633
Cornelia Huck63b69712008-01-21 16:09:44 +0100634static void cleanup_glue_dir(struct device *dev, struct kobject *glue_dir)
Kay Sieversda231fd2007-11-21 17:29:15 +0100635{
Kay Sievers0f4dafc2007-12-19 01:40:42 +0100636 /* see if we live in a "glue" directory */
Cornelia Huckc1fe5392008-02-27 15:38:23 +0100637 if (!glue_dir || !dev->class ||
Greg Kroah-Hartman7c714482008-01-22 18:17:41 -0500638 glue_dir->kset != &dev->class->p->class_dirs)
Kay Sieversda231fd2007-11-21 17:29:15 +0100639 return;
640
Kay Sievers0f4dafc2007-12-19 01:40:42 +0100641 kobject_put(glue_dir);
Kay Sieversda231fd2007-11-21 17:29:15 +0100642}
Cornelia Huck63b69712008-01-21 16:09:44 +0100643
644static void cleanup_device_parent(struct device *dev)
645{
646 cleanup_glue_dir(dev, dev->kobj.parent);
647}
Cornelia Huckc744aeae2007-01-08 20:16:44 +0100648#endif
Kay Sievers86406242007-03-14 03:25:56 +0100649
Cornelia Huck63b69712008-01-21 16:09:44 +0100650static void setup_parent(struct device *dev, struct device *parent)
Cornelia Huckc744aeae2007-01-08 20:16:44 +0100651{
652 struct kobject *kobj;
653 kobj = get_device_parent(dev, parent);
Cornelia Huckc744aeae2007-01-08 20:16:44 +0100654 if (kobj)
655 dev->kobj.parent = kobj;
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +0100656}
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +0100657
Cornelia Huck2ee97ca2007-07-18 01:43:47 -0700658static int device_add_class_symlinks(struct device *dev)
659{
660 int error;
661
662 if (!dev->class)
663 return 0;
Kay Sieversda231fd2007-11-21 17:29:15 +0100664
Greg Kroah-Hartman1fbfee62008-05-28 09:28:39 -0700665 error = sysfs_create_link(&dev->kobj,
666 &dev->class->p->class_subsys.kobj,
Cornelia Huck2ee97ca2007-07-18 01:43:47 -0700667 "subsystem");
668 if (error)
669 goto out;
Kay Sieversda231fd2007-11-21 17:29:15 +0100670
671#ifdef CONFIG_SYSFS_DEPRECATED
672 /* stacked class devices need a symlink in the class directory */
Greg Kroah-Hartman1fbfee62008-05-28 09:28:39 -0700673 if (dev->kobj.parent != &dev->class->p->class_subsys.kobj &&
Greg Kroah-Hartman4e886c22008-01-27 12:12:43 -0800674 device_is_not_partition(dev)) {
Greg Kroah-Hartman1fbfee62008-05-28 09:28:39 -0700675 error = sysfs_create_link(&dev->class->p->class_subsys.kobj,
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100676 &dev->kobj, dev_name(dev));
Cornelia Huck2ee97ca2007-07-18 01:43:47 -0700677 if (error)
678 goto out_subsys;
679 }
Kay Sieversda231fd2007-11-21 17:29:15 +0100680
Greg Kroah-Hartman4e886c22008-01-27 12:12:43 -0800681 if (dev->parent && device_is_not_partition(dev)) {
Kay Sieversda231fd2007-11-21 17:29:15 +0100682 struct device *parent = dev->parent;
683 char *class_name;
Dmitry Torokhov4f01a752007-09-18 22:46:50 -0700684
Kay Sieversda231fd2007-11-21 17:29:15 +0100685 /*
686 * stacked class devices have the 'device' link
687 * pointing to the bus device instead of the parent
688 */
689 while (parent->class && !parent->bus && parent->parent)
690 parent = parent->parent;
Dmitry Torokhov4f01a752007-09-18 22:46:50 -0700691
Kay Sieversda231fd2007-11-21 17:29:15 +0100692 error = sysfs_create_link(&dev->kobj,
693 &parent->kobj,
694 "device");
695 if (error)
696 goto out_busid;
Dmitry Torokhov4f01a752007-09-18 22:46:50 -0700697
Kay Sieversda231fd2007-11-21 17:29:15 +0100698 class_name = make_class_name(dev->class->name,
699 &dev->kobj);
700 if (class_name)
701 error = sysfs_create_link(&dev->parent->kobj,
702 &dev->kobj, class_name);
703 kfree(class_name);
704 if (error)
705 goto out_device;
706 }
707 return 0;
708
709out_device:
Greg Kroah-Hartman4e886c22008-01-27 12:12:43 -0800710 if (dev->parent && device_is_not_partition(dev))
Kay Sieversda231fd2007-11-21 17:29:15 +0100711 sysfs_remove_link(&dev->kobj, "device");
712out_busid:
Greg Kroah-Hartman1fbfee62008-05-28 09:28:39 -0700713 if (dev->kobj.parent != &dev->class->p->class_subsys.kobj &&
Greg Kroah-Hartman4e886c22008-01-27 12:12:43 -0800714 device_is_not_partition(dev))
Greg Kroah-Hartman1fbfee62008-05-28 09:28:39 -0700715 sysfs_remove_link(&dev->class->p->class_subsys.kobj,
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100716 dev_name(dev));
Dmitry Torokhov4f01a752007-09-18 22:46:50 -0700717#else
Kay Sieversda231fd2007-11-21 17:29:15 +0100718 /* link in the class directory pointing to the device */
Greg Kroah-Hartman1fbfee62008-05-28 09:28:39 -0700719 error = sysfs_create_link(&dev->class->p->class_subsys.kobj,
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100720 &dev->kobj, dev_name(dev));
Kay Sieversda231fd2007-11-21 17:29:15 +0100721 if (error)
722 goto out_subsys;
723
Greg Kroah-Hartman4e886c22008-01-27 12:12:43 -0800724 if (dev->parent && device_is_not_partition(dev)) {
Dmitry Torokhov4f01a752007-09-18 22:46:50 -0700725 error = sysfs_create_link(&dev->kobj, &dev->parent->kobj,
726 "device");
727 if (error)
728 goto out_busid;
Cornelia Huck2ee97ca2007-07-18 01:43:47 -0700729 }
730 return 0;
731
Cornelia Huck2ee97ca2007-07-18 01:43:47 -0700732out_busid:
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100733 sysfs_remove_link(&dev->class->p->class_subsys.kobj, dev_name(dev));
Kay Sieversda231fd2007-11-21 17:29:15 +0100734#endif
735
Cornelia Huck2ee97ca2007-07-18 01:43:47 -0700736out_subsys:
737 sysfs_remove_link(&dev->kobj, "subsystem");
738out:
739 return error;
740}
741
742static void device_remove_class_symlinks(struct device *dev)
743{
744 if (!dev->class)
745 return;
Kay Sieversda231fd2007-11-21 17:29:15 +0100746
Cornelia Huck2ee97ca2007-07-18 01:43:47 -0700747#ifdef CONFIG_SYSFS_DEPRECATED
Greg Kroah-Hartman4e886c22008-01-27 12:12:43 -0800748 if (dev->parent && device_is_not_partition(dev)) {
Cornelia Huck2ee97ca2007-07-18 01:43:47 -0700749 char *class_name;
750
751 class_name = make_class_name(dev->class->name, &dev->kobj);
752 if (class_name) {
753 sysfs_remove_link(&dev->parent->kobj, class_name);
754 kfree(class_name);
755 }
Cornelia Huck2ee97ca2007-07-18 01:43:47 -0700756 sysfs_remove_link(&dev->kobj, "device");
757 }
Kay Sieversda231fd2007-11-21 17:29:15 +0100758
Greg Kroah-Hartman1fbfee62008-05-28 09:28:39 -0700759 if (dev->kobj.parent != &dev->class->p->class_subsys.kobj &&
Greg Kroah-Hartman4e886c22008-01-27 12:12:43 -0800760 device_is_not_partition(dev))
Greg Kroah-Hartman1fbfee62008-05-28 09:28:39 -0700761 sysfs_remove_link(&dev->class->p->class_subsys.kobj,
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100762 dev_name(dev));
Kay Sieversda231fd2007-11-21 17:29:15 +0100763#else
Greg Kroah-Hartman4e886c22008-01-27 12:12:43 -0800764 if (dev->parent && device_is_not_partition(dev))
Kay Sieversda231fd2007-11-21 17:29:15 +0100765 sysfs_remove_link(&dev->kobj, "device");
766
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100767 sysfs_remove_link(&dev->class->p->class_subsys.kobj, dev_name(dev));
Kay Sieversda231fd2007-11-21 17:29:15 +0100768#endif
769
Cornelia Huck2ee97ca2007-07-18 01:43:47 -0700770 sysfs_remove_link(&dev->kobj, "subsystem");
771}
772
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773/**
Stephen Rothwell413c2392008-05-30 10:16:40 +1000774 * dev_set_name - set a device name
775 * @dev: device
Randy Dunlap46232362008-06-04 21:40:43 -0700776 * @fmt: format string for the device's name
Stephen Rothwell413c2392008-05-30 10:16:40 +1000777 */
778int dev_set_name(struct device *dev, const char *fmt, ...)
779{
780 va_list vargs;
Kay Sievers1fa5ae82009-01-25 15:17:37 +0100781 int err;
Stephen Rothwell413c2392008-05-30 10:16:40 +1000782
783 va_start(vargs, fmt);
Kay Sievers1fa5ae82009-01-25 15:17:37 +0100784 err = kobject_set_name_vargs(&dev->kobj, fmt, vargs);
Stephen Rothwell413c2392008-05-30 10:16:40 +1000785 va_end(vargs);
Kay Sievers1fa5ae82009-01-25 15:17:37 +0100786 return err;
Stephen Rothwell413c2392008-05-30 10:16:40 +1000787}
788EXPORT_SYMBOL_GPL(dev_set_name);
789
790/**
Dan Williamse105b8b2008-04-21 10:51:07 -0700791 * device_to_dev_kobj - select a /sys/dev/ directory for the device
792 * @dev: device
793 *
794 * By default we select char/ for new entries. Setting class->dev_obj
795 * to NULL prevents an entry from being created. class->dev_kobj must
796 * be set (or cleared) before any devices are registered to the class
797 * otherwise device_create_sys_dev_entry() and
798 * device_remove_sys_dev_entry() will disagree about the the presence
799 * of the link.
800 */
801static struct kobject *device_to_dev_kobj(struct device *dev)
802{
803 struct kobject *kobj;
804
805 if (dev->class)
806 kobj = dev->class->dev_kobj;
807 else
808 kobj = sysfs_dev_char_kobj;
809
810 return kobj;
811}
812
813static int device_create_sys_dev_entry(struct device *dev)
814{
815 struct kobject *kobj = device_to_dev_kobj(dev);
816 int error = 0;
817 char devt_str[15];
818
819 if (kobj) {
820 format_dev_t(devt_str, dev->devt);
821 error = sysfs_create_link(kobj, &dev->kobj, devt_str);
822 }
823
824 return error;
825}
826
827static void device_remove_sys_dev_entry(struct device *dev)
828{
829 struct kobject *kobj = device_to_dev_kobj(dev);
830 char devt_str[15];
831
832 if (kobj) {
833 format_dev_t(devt_str, dev->devt);
834 sysfs_remove_link(kobj, devt_str);
835 }
836}
837
838/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800839 * device_add - add device to device hierarchy.
840 * @dev: device.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800842 * This is part 2 of device_register(), though may be called
843 * separately _iff_ device_initialize() has been called separately.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 *
Cornelia Huck57394112008-09-03 18:26:40 +0200845 * This adds @dev to the kobject hierarchy via kobject_add(), adds it
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800846 * to the global and sibling lists for the device, then
847 * adds it to the other relevant subsystems of the driver model.
Cornelia Huck57394112008-09-03 18:26:40 +0200848 *
849 * NOTE: _Never_ directly free @dev after calling this function, even
850 * if it returned an error! Always use put_device() to give up your
851 * reference instead.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 */
853int device_add(struct device *dev)
854{
855 struct device *parent = NULL;
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +0200856 struct class_interface *class_intf;
Greg Kroah-Hartmanc906a482008-05-30 10:45:12 -0700857 int error = -EINVAL;
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100858
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 dev = get_device(dev);
Greg Kroah-Hartmanc906a482008-05-30 10:45:12 -0700860 if (!dev)
861 goto done;
862
Greg Kroah-Hartmanfb069a52008-12-16 12:23:36 -0800863 dev->p = kzalloc(sizeof(*dev->p), GFP_KERNEL);
864 if (!dev->p) {
865 error = -ENOMEM;
866 goto done;
867 }
868 dev->p->device = dev;
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -0800869 klist_init(&dev->p->klist_children, klist_children_get,
870 klist_children_put);
Greg Kroah-Hartmanfb069a52008-12-16 12:23:36 -0800871
Kay Sievers1fa5ae82009-01-25 15:17:37 +0100872 /*
873 * for statically allocated devices, which should all be converted
874 * some day, we need to initialize the name. We prevent reading back
875 * the name, and force the use of dev_name()
876 */
877 if (dev->init_name) {
Greg Kroah-Hartmanacc0e902009-06-02 15:39:55 -0700878 dev_set_name(dev, "%s", dev->init_name);
Kay Sievers1fa5ae82009-01-25 15:17:37 +0100879 dev->init_name = NULL;
880 }
Greg Kroah-Hartmanc906a482008-05-30 10:45:12 -0700881
Kay Sievers1fa5ae82009-01-25 15:17:37 +0100882 if (!dev_name(dev))
Kay Sievers5c8563d2009-05-28 14:24:07 -0700883 goto name_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100885 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
Greg Kroah-Hartmanc205ef42006-08-07 22:19:37 -0700886
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 parent = get_device(dev->parent);
Cornelia Huck63b69712008-01-21 16:09:44 +0100888 setup_parent(dev, parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889
Yinghai Lu0d358f22008-02-19 03:20:41 -0800890 /* use parent numa_node */
891 if (parent)
892 set_dev_node(dev, dev_to_node(parent));
893
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 /* first, register with generic layer. */
Kay Sievers8a577ff2009-04-18 15:05:45 -0700895 /* we require the name to be set before, and pass NULL */
896 error = kobject_add(&dev->kobj, dev->kobj.parent, NULL);
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +0100897 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 goto Error;
Kay Sieversa7fd6702005-10-01 14:49:43 +0200899
Brian Walsh37022642006-08-14 22:43:19 -0700900 /* notify platform of device entry */
901 if (platform_notify)
902 platform_notify(dev);
903
Tejun Heoad6a1e12007-06-14 03:45:17 +0900904 error = device_create_file(dev, &uevent_attr);
Cornelia Hucka306eea2006-09-22 11:37:13 +0200905 if (error)
906 goto attrError;
Kay Sieversa7fd6702005-10-01 14:49:43 +0200907
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700908 if (MAJOR(dev->devt)) {
Tejun Heoad6a1e12007-06-14 03:45:17 +0900909 error = device_create_file(dev, &devt_attr);
910 if (error)
Cornelia Hucka306eea2006-09-22 11:37:13 +0200911 goto ueventattrError;
Dan Williamse105b8b2008-04-21 10:51:07 -0700912
913 error = device_create_sys_dev_entry(dev);
914 if (error)
915 goto devtattrError;
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700916 }
917
Cornelia Huck2ee97ca2007-07-18 01:43:47 -0700918 error = device_add_class_symlinks(dev);
919 if (error)
920 goto SymlinkError;
Cornelia Huckdc0afa82007-07-09 11:39:18 -0700921 error = device_add_attrs(dev);
922 if (error)
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700923 goto AttrsError;
Cornelia Huckdc0afa82007-07-09 11:39:18 -0700924 error = bus_add_device(dev);
925 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 goto BusError;
Alan Stern3b98aea2008-08-07 13:06:12 -0400927 error = dpm_sysfs_add(dev);
Rafael J. Wysocki57eee3d2008-03-12 00:59:38 +0100928 if (error)
Alan Stern3b98aea2008-08-07 13:06:12 -0400929 goto DPMError;
930 device_pm_add(dev);
Alan Sternec0676ee2008-12-05 14:10:31 -0500931
932 /* Notify clients of device addition. This call must come
933 * after dpm_sysf_add() and before kobject_uevent().
934 */
935 if (dev->bus)
936 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
937 BUS_NOTIFY_ADD_DEVICE, dev);
938
Cornelia Huck83b5fb4c2007-03-29 11:12:11 +0200939 kobject_uevent(&dev->kobj, KOBJ_ADD);
Cornelia Huckc6a46692007-02-05 16:15:26 -0800940 bus_attach_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 if (parent)
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -0800942 klist_add_tail(&dev->p->knode_parent,
943 &parent->p->klist_children);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944
Greg Kroah-Hartman5d9fd162006-06-22 17:17:32 -0700945 if (dev->class) {
Dave Youngf75b1c62008-05-28 09:28:39 -0700946 mutex_lock(&dev->class->p->class_mutex);
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +0200947 /* tie the class to the device */
Tejun Heo5a3ceb82008-08-25 19:50:19 +0200948 klist_add_tail(&dev->knode_class,
949 &dev->class->p->class_devices);
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +0200950
951 /* notify any interfaces that the device is here */
Greg Kroah-Hartman184f1f72008-05-28 09:28:39 -0700952 list_for_each_entry(class_intf,
953 &dev->class->p->class_interfaces, node)
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +0200954 if (class_intf->add_dev)
955 class_intf->add_dev(dev, class_intf);
Dave Youngf75b1c62008-05-28 09:28:39 -0700956 mutex_unlock(&dev->class->p->class_mutex);
Greg Kroah-Hartman5d9fd162006-06-22 17:17:32 -0700957 }
Greg Kroah-Hartmanc906a482008-05-30 10:45:12 -0700958done:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 put_device(dev);
960 return error;
Alan Stern3b98aea2008-08-07 13:06:12 -0400961 DPMError:
Rafael J. Wysocki57eee3d2008-03-12 00:59:38 +0100962 bus_remove_device(dev);
963 BusError:
James Simmons82f0cf92007-02-21 17:44:51 +0000964 device_remove_attrs(dev);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700965 AttrsError:
Cornelia Huck2ee97ca2007-07-18 01:43:47 -0700966 device_remove_class_symlinks(dev);
967 SymlinkError:
Tejun Heoad6a1e12007-06-14 03:45:17 +0900968 if (MAJOR(dev->devt))
Dan Williamse105b8b2008-04-21 10:51:07 -0700969 device_remove_sys_dev_entry(dev);
970 devtattrError:
971 if (MAJOR(dev->devt))
Tejun Heoad6a1e12007-06-14 03:45:17 +0900972 device_remove_file(dev, &devt_attr);
Cornelia Hucka306eea2006-09-22 11:37:13 +0200973 ueventattrError:
Tejun Heoad6a1e12007-06-14 03:45:17 +0900974 device_remove_file(dev, &uevent_attr);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700975 attrError:
Kay Sievers312c0042005-11-16 09:00:00 +0100976 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 kobject_del(&dev->kobj);
978 Error:
Cornelia Huck63b69712008-01-21 16:09:44 +0100979 cleanup_device_parent(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 if (parent)
981 put_device(parent);
Kay Sievers5c8563d2009-05-28 14:24:07 -0700982name_error:
983 kfree(dev->p);
984 dev->p = NULL;
Greg Kroah-Hartmanc906a482008-05-30 10:45:12 -0700985 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986}
987
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800989 * device_register - register a device with the system.
990 * @dev: pointer to the device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800992 * This happens in two clean steps - initialize the device
993 * and add it to the system. The two steps can be called
994 * separately, but this is the easiest and most common.
995 * I.e. you should only call the two helpers separately if
996 * have a clearly defined need to use and refcount the device
997 * before it is added to the hierarchy.
Cornelia Huck57394112008-09-03 18:26:40 +0200998 *
999 * NOTE: _Never_ directly free @dev after calling this function, even
1000 * if it returned an error! Always use put_device() to give up the
1001 * reference initialized in this function instead.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003int device_register(struct device *dev)
1004{
1005 device_initialize(dev);
1006 return device_add(dev);
1007}
1008
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001010 * get_device - increment reference count for device.
1011 * @dev: device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001013 * This simply forwards the call to kobject_get(), though
1014 * we do take care to provide for the case that we get a NULL
1015 * pointer passed in.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001017struct device *get_device(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018{
1019 return dev ? to_dev(kobject_get(&dev->kobj)) : NULL;
1020}
1021
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001023 * put_device - decrement reference count.
1024 * @dev: device in question.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001026void put_device(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027{
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001028 /* might_sleep(); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 if (dev)
1030 kobject_put(&dev->kobj);
1031}
1032
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001034 * device_del - delete device from system.
1035 * @dev: device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001037 * This is the first part of the device unregistration
1038 * sequence. This removes the device from the lists we control
1039 * from here, has it removed from the other driver model
1040 * subsystems it was added to in device_add(), and removes it
1041 * from the kobject hierarchy.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001043 * NOTE: this should be called manually _iff_ device_add() was
1044 * also called manually.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001046void device_del(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001048 struct device *parent = dev->parent;
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02001049 struct class_interface *class_intf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050
Alan Sternec0676ee2008-12-05 14:10:31 -05001051 /* Notify clients of device removal. This call must come
1052 * before dpm_sysfs_remove().
1053 */
1054 if (dev->bus)
1055 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
1056 BUS_NOTIFY_DEL_DEVICE, dev);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001057 device_pm_remove(dev);
Alan Stern3b98aea2008-08-07 13:06:12 -04001058 dpm_sysfs_remove(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 if (parent)
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08001060 klist_del(&dev->p->knode_parent);
Dan Williamse105b8b2008-04-21 10:51:07 -07001061 if (MAJOR(dev->devt)) {
1062 device_remove_sys_dev_entry(dev);
Tejun Heoad6a1e12007-06-14 03:45:17 +09001063 device_remove_file(dev, &devt_attr);
Dan Williamse105b8b2008-04-21 10:51:07 -07001064 }
Kay Sieversb9d9c822006-06-15 15:31:56 +02001065 if (dev->class) {
Kay Sieversda231fd2007-11-21 17:29:15 +01001066 device_remove_class_symlinks(dev);
Kay Sievers99ef3ef2006-09-14 11:23:28 +02001067
Dave Youngf75b1c62008-05-28 09:28:39 -07001068 mutex_lock(&dev->class->p->class_mutex);
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02001069 /* notify any interfaces that the device is now gone */
Greg Kroah-Hartman184f1f72008-05-28 09:28:39 -07001070 list_for_each_entry(class_intf,
1071 &dev->class->p->class_interfaces, node)
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02001072 if (class_intf->remove_dev)
1073 class_intf->remove_dev(dev, class_intf);
1074 /* remove the device from the class list */
Tejun Heo5a3ceb82008-08-25 19:50:19 +02001075 klist_del(&dev->knode_class);
Dave Youngf75b1c62008-05-28 09:28:39 -07001076 mutex_unlock(&dev->class->p->class_mutex);
Kay Sieversb9d9c822006-06-15 15:31:56 +02001077 }
Tejun Heoad6a1e12007-06-14 03:45:17 +09001078 device_remove_file(dev, &uevent_attr);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07001079 device_remove_attrs(dev);
Benjamin Herrenschmidt28953532006-11-08 19:46:14 -08001080 bus_remove_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081
Tejun Heo2f8d16a2007-03-09 19:34:19 +09001082 /*
1083 * Some platform devices are driven without driver attached
1084 * and managed resources may have been acquired. Make sure
1085 * all resources are released.
1086 */
1087 devres_release_all(dev);
1088
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 /* Notify the platform of the removal, in case they
1090 * need to do anything...
1091 */
1092 if (platform_notify_remove)
1093 platform_notify_remove(dev);
Kay Sievers312c0042005-11-16 09:00:00 +01001094 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
Kay Sieversda231fd2007-11-21 17:29:15 +01001095 cleanup_device_parent(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 kobject_del(&dev->kobj);
Kay Sieversda231fd2007-11-21 17:29:15 +01001097 put_device(parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098}
1099
1100/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001101 * device_unregister - unregister device from system.
1102 * @dev: device going away.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001104 * We do this in two parts, like we do device_register(). First,
1105 * we remove it from all the subsystems with device_del(), then
1106 * we decrement the reference count via put_device(). If that
1107 * is the final reference count, the device will be cleaned up
1108 * via device_release() above. Otherwise, the structure will
1109 * stick around until the final reference to the device is dropped.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001111void device_unregister(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112{
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01001113 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 device_del(dev);
1115 put_device(dev);
1116}
1117
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001118static struct device *next_device(struct klist_iter *i)
mochel@digitalimplant.org36239572005-03-24 19:08:30 -08001119{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001120 struct klist_node *n = klist_next(i);
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08001121 struct device *dev = NULL;
1122 struct device_private *p;
1123
1124 if (n) {
1125 p = to_device_private_parent(n);
1126 dev = p->device;
1127 }
1128 return dev;
mochel@digitalimplant.org36239572005-03-24 19:08:30 -08001129}
1130
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001132 * device_for_each_child - device child iterator.
1133 * @parent: parent struct device.
1134 * @data: data for the callback.
1135 * @fn: function to be called for each device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001137 * Iterate over @parent's child devices, and call @fn for each,
1138 * passing it @data.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001140 * We check the return of @fn each time. If it returns anything
1141 * other than 0, we break out and return that value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001143int device_for_each_child(struct device *parent, void *data,
1144 int (*fn)(struct device *dev, void *data))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145{
mochel@digitalimplant.org36239572005-03-24 19:08:30 -08001146 struct klist_iter i;
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001147 struct device *child;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 int error = 0;
1149
Greg Kroah-Hartman014c90db2009-04-15 16:00:12 -07001150 if (!parent->p)
1151 return 0;
1152
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08001153 klist_iter_init(&parent->p->klist_children, &i);
mochel@digitalimplant.org36239572005-03-24 19:08:30 -08001154 while ((child = next_device(&i)) && !error)
1155 error = fn(child, data);
1156 klist_iter_exit(&i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 return error;
1158}
1159
Cornelia Huck5ab69982006-11-16 15:42:07 +01001160/**
1161 * device_find_child - device iterator for locating a particular device.
1162 * @parent: parent struct device
1163 * @data: Data to pass to match function
1164 * @match: Callback function to check device
1165 *
1166 * This is similar to the device_for_each_child() function above, but it
1167 * returns a reference to a device that is 'found' for later use, as
1168 * determined by the @match callback.
1169 *
1170 * The callback should return 0 if the device doesn't match and non-zero
1171 * if it does. If the callback returns non-zero and a reference to the
1172 * current device can be obtained, this function will return to the caller
1173 * and not iterate over any more devices.
1174 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001175struct device *device_find_child(struct device *parent, void *data,
1176 int (*match)(struct device *dev, void *data))
Cornelia Huck5ab69982006-11-16 15:42:07 +01001177{
1178 struct klist_iter i;
1179 struct device *child;
1180
1181 if (!parent)
1182 return NULL;
1183
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08001184 klist_iter_init(&parent->p->klist_children, &i);
Cornelia Huck5ab69982006-11-16 15:42:07 +01001185 while ((child = next_device(&i)))
1186 if (match(child, data) && get_device(child))
1187 break;
1188 klist_iter_exit(&i);
1189 return child;
1190}
1191
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192int __init devices_init(void)
1193{
Greg Kroah-Hartman881c6cfd2007-11-01 09:29:06 -06001194 devices_kset = kset_create_and_add("devices", &device_uevent_ops, NULL);
1195 if (!devices_kset)
1196 return -ENOMEM;
Dan Williamse105b8b2008-04-21 10:51:07 -07001197 dev_kobj = kobject_create_and_add("dev", NULL);
1198 if (!dev_kobj)
1199 goto dev_kobj_err;
1200 sysfs_dev_block_kobj = kobject_create_and_add("block", dev_kobj);
1201 if (!sysfs_dev_block_kobj)
1202 goto block_kobj_err;
1203 sysfs_dev_char_kobj = kobject_create_and_add("char", dev_kobj);
1204 if (!sysfs_dev_char_kobj)
1205 goto char_kobj_err;
1206
Greg Kroah-Hartman881c6cfd2007-11-01 09:29:06 -06001207 return 0;
Dan Williamse105b8b2008-04-21 10:51:07 -07001208
1209 char_kobj_err:
1210 kobject_put(sysfs_dev_block_kobj);
1211 block_kobj_err:
1212 kobject_put(dev_kobj);
1213 dev_kobj_err:
1214 kset_unregister(devices_kset);
1215 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216}
1217
1218EXPORT_SYMBOL_GPL(device_for_each_child);
Cornelia Huck5ab69982006-11-16 15:42:07 +01001219EXPORT_SYMBOL_GPL(device_find_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220
1221EXPORT_SYMBOL_GPL(device_initialize);
1222EXPORT_SYMBOL_GPL(device_add);
1223EXPORT_SYMBOL_GPL(device_register);
1224
1225EXPORT_SYMBOL_GPL(device_del);
1226EXPORT_SYMBOL_GPL(device_unregister);
1227EXPORT_SYMBOL_GPL(get_device);
1228EXPORT_SYMBOL_GPL(put_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229
1230EXPORT_SYMBOL_GPL(device_create_file);
1231EXPORT_SYMBOL_GPL(device_remove_file);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001232
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00001233struct root_device
1234{
1235 struct device dev;
1236 struct module *owner;
1237};
1238
1239#define to_root_device(dev) container_of(dev, struct root_device, dev)
1240
1241static void root_device_release(struct device *dev)
1242{
1243 kfree(to_root_device(dev));
1244}
1245
1246/**
1247 * __root_device_register - allocate and register a root device
1248 * @name: root device name
1249 * @owner: owner module of the root device, usually THIS_MODULE
1250 *
1251 * This function allocates a root device and registers it
1252 * using device_register(). In order to free the returned
1253 * device, use root_device_unregister().
1254 *
1255 * Root devices are dummy devices which allow other devices
1256 * to be grouped under /sys/devices. Use this function to
1257 * allocate a root device and then use it as the parent of
1258 * any device which should appear under /sys/devices/{name}
1259 *
1260 * The /sys/devices/{name} directory will also contain a
1261 * 'module' symlink which points to the @owner directory
1262 * in sysfs.
1263 *
1264 * Note: You probably want to use root_device_register().
1265 */
1266struct device *__root_device_register(const char *name, struct module *owner)
1267{
1268 struct root_device *root;
1269 int err = -ENOMEM;
1270
1271 root = kzalloc(sizeof(struct root_device), GFP_KERNEL);
1272 if (!root)
1273 return ERR_PTR(err);
1274
Greg Kroah-Hartmanacc0e902009-06-02 15:39:55 -07001275 err = dev_set_name(&root->dev, "%s", name);
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00001276 if (err) {
1277 kfree(root);
1278 return ERR_PTR(err);
1279 }
1280
1281 root->dev.release = root_device_release;
1282
1283 err = device_register(&root->dev);
1284 if (err) {
1285 put_device(&root->dev);
1286 return ERR_PTR(err);
1287 }
1288
1289#ifdef CONFIG_MODULE /* gotta find a "cleaner" way to do this */
1290 if (owner) {
1291 struct module_kobject *mk = &owner->mkobj;
1292
1293 err = sysfs_create_link(&root->dev.kobj, &mk->kobj, "module");
1294 if (err) {
1295 device_unregister(&root->dev);
1296 return ERR_PTR(err);
1297 }
1298 root->owner = owner;
1299 }
1300#endif
1301
1302 return &root->dev;
1303}
1304EXPORT_SYMBOL_GPL(__root_device_register);
1305
1306/**
1307 * root_device_unregister - unregister and free a root device
Randy Dunlap7cbcf222009-01-20 16:29:13 -08001308 * @dev: device going away
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00001309 *
1310 * This function unregisters and cleans up a device that was created by
1311 * root_device_register().
1312 */
1313void root_device_unregister(struct device *dev)
1314{
1315 struct root_device *root = to_root_device(dev);
1316
1317 if (root->owner)
1318 sysfs_remove_link(&root->dev.kobj, "module");
1319
1320 device_unregister(dev);
1321}
1322EXPORT_SYMBOL_GPL(root_device_unregister);
1323
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001324
1325static void device_create_release(struct device *dev)
1326{
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01001327 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001328 kfree(dev);
1329}
1330
1331/**
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07001332 * device_create_vargs - creates a device and registers it with sysfs
1333 * @class: pointer to the struct class that this device should be registered to
1334 * @parent: pointer to the parent struct device of this new device, if any
1335 * @devt: the dev_t for the char device to be added
1336 * @drvdata: the data to be added to the device for callbacks
1337 * @fmt: string for the device's name
1338 * @args: va_list for the device's name
1339 *
1340 * This function can be used by char device classes. A struct device
1341 * will be created in sysfs, registered to the specified class.
1342 *
1343 * A "dev" file will be created, showing the dev_t for the device, if
1344 * the dev_t is not 0,0.
1345 * If a pointer to a parent struct device is passed in, the newly created
1346 * struct device will be a child of that device in sysfs.
1347 * The pointer to the struct device will be returned from the call.
1348 * Any further sysfs files that might be required can be created using this
1349 * pointer.
1350 *
1351 * Note: the struct class passed to this function must have previously
1352 * been created with a call to class_create().
1353 */
1354struct device *device_create_vargs(struct class *class, struct device *parent,
1355 dev_t devt, void *drvdata, const char *fmt,
1356 va_list args)
1357{
1358 struct device *dev = NULL;
1359 int retval = -ENODEV;
1360
1361 if (class == NULL || IS_ERR(class))
1362 goto error;
1363
1364 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1365 if (!dev) {
1366 retval = -ENOMEM;
1367 goto error;
1368 }
1369
1370 dev->devt = devt;
1371 dev->class = class;
1372 dev->parent = parent;
1373 dev->release = device_create_release;
1374 dev_set_drvdata(dev, drvdata);
1375
Kay Sievers1fa5ae82009-01-25 15:17:37 +01001376 retval = kobject_set_name_vargs(&dev->kobj, fmt, args);
1377 if (retval)
1378 goto error;
1379
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07001380 retval = device_register(dev);
1381 if (retval)
1382 goto error;
1383
1384 return dev;
1385
1386error:
Cornelia Huck286661b2008-09-03 18:26:41 +02001387 put_device(dev);
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07001388 return ERR_PTR(retval);
1389}
1390EXPORT_SYMBOL_GPL(device_create_vargs);
1391
1392/**
Greg Kroah-Hartman4e106732008-07-21 20:03:34 -07001393 * device_create - creates a device and registers it with sysfs
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07001394 * @class: pointer to the struct class that this device should be registered to
1395 * @parent: pointer to the parent struct device of this new device, if any
1396 * @devt: the dev_t for the char device to be added
1397 * @drvdata: the data to be added to the device for callbacks
1398 * @fmt: string for the device's name
1399 *
1400 * This function can be used by char device classes. A struct device
1401 * will be created in sysfs, registered to the specified class.
1402 *
1403 * A "dev" file will be created, showing the dev_t for the device, if
1404 * the dev_t is not 0,0.
1405 * If a pointer to a parent struct device is passed in, the newly created
1406 * struct device will be a child of that device in sysfs.
1407 * The pointer to the struct device will be returned from the call.
1408 * Any further sysfs files that might be required can be created using this
1409 * pointer.
1410 *
1411 * Note: the struct class passed to this function must have previously
1412 * been created with a call to class_create().
1413 */
Greg Kroah-Hartman4e106732008-07-21 20:03:34 -07001414struct device *device_create(struct class *class, struct device *parent,
1415 dev_t devt, void *drvdata, const char *fmt, ...)
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07001416{
1417 va_list vargs;
1418 struct device *dev;
1419
1420 va_start(vargs, fmt);
1421 dev = device_create_vargs(class, parent, devt, drvdata, fmt, vargs);
1422 va_end(vargs);
1423 return dev;
1424}
Greg Kroah-Hartman4e106732008-07-21 20:03:34 -07001425EXPORT_SYMBOL_GPL(device_create);
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07001426
Dave Youngcd354492008-01-28 16:56:11 +08001427static int __match_devt(struct device *dev, void *data)
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001428{
Dave Youngcd354492008-01-28 16:56:11 +08001429 dev_t *devt = data;
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001430
Dave Youngcd354492008-01-28 16:56:11 +08001431 return dev->devt == *devt;
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001432}
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001433
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001434/**
1435 * device_destroy - removes a device that was created with device_create()
1436 * @class: pointer to the struct class that this device was registered with
1437 * @devt: the dev_t of the device that was previously registered
1438 *
1439 * This call unregisters and cleans up a device that was created with a
1440 * call to device_create().
1441 */
1442void device_destroy(struct class *class, dev_t devt)
1443{
1444 struct device *dev;
1445
Greg Kroah-Hartman695794a2008-05-22 17:21:08 -04001446 dev = class_find_device(class, NULL, &devt, __match_devt);
Dave Youngcd354492008-01-28 16:56:11 +08001447 if (dev) {
1448 put_device(dev);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001449 device_unregister(dev);
Dave Youngcd354492008-01-28 16:56:11 +08001450 }
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001451}
1452EXPORT_SYMBOL_GPL(device_destroy);
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001453
1454/**
1455 * device_rename - renames a device
1456 * @dev: the pointer to the struct device to be renamed
1457 * @new_name: the new name of the device
Eric W. Biederman030c1d22008-05-08 14:41:00 -07001458 *
1459 * It is the responsibility of the caller to provide mutual
1460 * exclusion between two different calls of device_rename
1461 * on the same device to ensure that new_name is valid and
1462 * won't conflict with other devices.
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001463 */
1464int device_rename(struct device *dev, char *new_name)
1465{
1466 char *old_class_name = NULL;
1467 char *new_class_name = NULL;
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001468 char *old_device_name = NULL;
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001469 int error;
1470
1471 dev = get_device(dev);
1472 if (!dev)
1473 return -EINVAL;
1474
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01001475 pr_debug("device: '%s': %s: renaming to '%s'\n", dev_name(dev),
Harvey Harrison2b3a3022008-03-04 16:41:05 -08001476 __func__, new_name);
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001477
Kay Sievers99ef3ef2006-09-14 11:23:28 +02001478#ifdef CONFIG_SYSFS_DEPRECATED
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001479 if ((dev->class) && (dev->parent))
1480 old_class_name = make_class_name(dev->class->name, &dev->kobj);
Kay Sievers99ef3ef2006-09-14 11:23:28 +02001481#endif
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001482
Kay Sievers1fa5ae82009-01-25 15:17:37 +01001483 old_device_name = kstrdup(dev_name(dev), GFP_KERNEL);
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001484 if (!old_device_name) {
1485 error = -ENOMEM;
1486 goto out;
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001487 }
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001488
1489 error = kobject_rename(&dev->kobj, new_name);
Kay Sievers1fa5ae82009-01-25 15:17:37 +01001490 if (error)
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001491 goto out;
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001492
Kay Sievers99ef3ef2006-09-14 11:23:28 +02001493#ifdef CONFIG_SYSFS_DEPRECATED
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001494 if (old_class_name) {
1495 new_class_name = make_class_name(dev->class->name, &dev->kobj);
1496 if (new_class_name) {
Cornelia Huck36ce6da2008-06-10 11:09:08 +02001497 error = sysfs_create_link_nowarn(&dev->parent->kobj,
1498 &dev->kobj,
1499 new_class_name);
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001500 if (error)
1501 goto out;
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001502 sysfs_remove_link(&dev->parent->kobj, old_class_name);
1503 }
1504 }
Kay Sievers60b8cab2007-10-26 20:07:44 +02001505#else
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001506 if (dev->class) {
Cornelia Huck36ce6da2008-06-10 11:09:08 +02001507 error = sysfs_create_link_nowarn(&dev->class->p->class_subsys.kobj,
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01001508 &dev->kobj, dev_name(dev));
Stephen Hemminger0599ad52008-05-14 22:34:16 -07001509 if (error)
1510 goto out;
Greg Kroah-Hartman1fbfee62008-05-28 09:28:39 -07001511 sysfs_remove_link(&dev->class->p->class_subsys.kobj,
Greg Kroah-Hartman7c714482008-01-22 18:17:41 -05001512 old_device_name);
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001513 }
Kay Sievers60b8cab2007-10-26 20:07:44 +02001514#endif
1515
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001516out:
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001517 put_device(dev);
1518
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001519 kfree(new_class_name);
Jesper Juhl952ab432006-09-28 23:56:01 +02001520 kfree(old_class_name);
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001521 kfree(old_device_name);
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001522
1523 return error;
1524}
Johannes Berga2807db2007-02-28 12:38:31 +01001525EXPORT_SYMBOL_GPL(device_rename);
Cornelia Huck8a824722006-11-20 17:07:51 +01001526
1527static int device_move_class_links(struct device *dev,
1528 struct device *old_parent,
1529 struct device *new_parent)
1530{
Greg Kroah-Hartmanf7f34612007-03-06 12:55:53 -08001531 int error = 0;
Cornelia Huck8a824722006-11-20 17:07:51 +01001532#ifdef CONFIG_SYSFS_DEPRECATED
Cornelia Huck8a824722006-11-20 17:07:51 +01001533 char *class_name;
1534
1535 class_name = make_class_name(dev->class->name, &dev->kobj);
1536 if (!class_name) {
Cornelia Huckcb360bb2006-11-27 10:35:05 +01001537 error = -ENOMEM;
Cornelia Huck8a824722006-11-20 17:07:51 +01001538 goto out;
1539 }
1540 if (old_parent) {
1541 sysfs_remove_link(&dev->kobj, "device");
1542 sysfs_remove_link(&old_parent->kobj, class_name);
1543 }
Cornelia Huckc744aeae2007-01-08 20:16:44 +01001544 if (new_parent) {
1545 error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
1546 "device");
1547 if (error)
1548 goto out;
1549 error = sysfs_create_link(&new_parent->kobj, &dev->kobj,
1550 class_name);
1551 if (error)
1552 sysfs_remove_link(&dev->kobj, "device");
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001553 } else
Cornelia Huckc744aeae2007-01-08 20:16:44 +01001554 error = 0;
Cornelia Huck8a824722006-11-20 17:07:51 +01001555out:
1556 kfree(class_name);
1557 return error;
1558#else
Greg Kroah-Hartmanf7f34612007-03-06 12:55:53 -08001559 if (old_parent)
1560 sysfs_remove_link(&dev->kobj, "device");
1561 if (new_parent)
1562 error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
1563 "device");
1564 return error;
Cornelia Huck8a824722006-11-20 17:07:51 +01001565#endif
1566}
1567
1568/**
1569 * device_move - moves a device to a new parent
1570 * @dev: the pointer to the struct device to be moved
Cornelia Huckc744aeae2007-01-08 20:16:44 +01001571 * @new_parent: the new parent of the device (can by NULL)
Cornelia Huckffa6a702009-03-04 12:44:00 +01001572 * @dpm_order: how to reorder the dpm_list
Cornelia Huck8a824722006-11-20 17:07:51 +01001573 */
Cornelia Huckffa6a702009-03-04 12:44:00 +01001574int device_move(struct device *dev, struct device *new_parent,
1575 enum dpm_order dpm_order)
Cornelia Huck8a824722006-11-20 17:07:51 +01001576{
1577 int error;
1578 struct device *old_parent;
Cornelia Huckc744aeae2007-01-08 20:16:44 +01001579 struct kobject *new_parent_kobj;
Cornelia Huck8a824722006-11-20 17:07:51 +01001580
1581 dev = get_device(dev);
1582 if (!dev)
1583 return -EINVAL;
1584
Cornelia Huckffa6a702009-03-04 12:44:00 +01001585 device_pm_lock();
Cornelia Huck8a824722006-11-20 17:07:51 +01001586 new_parent = get_device(new_parent);
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001587 new_parent_kobj = get_device_parent(dev, new_parent);
Cornelia Huck63b69712008-01-21 16:09:44 +01001588
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01001589 pr_debug("device: '%s': %s: moving to '%s'\n", dev_name(dev),
1590 __func__, new_parent ? dev_name(new_parent) : "<NULL>");
Cornelia Huckc744aeae2007-01-08 20:16:44 +01001591 error = kobject_move(&dev->kobj, new_parent_kobj);
Cornelia Huck8a824722006-11-20 17:07:51 +01001592 if (error) {
Cornelia Huck63b69712008-01-21 16:09:44 +01001593 cleanup_glue_dir(dev, new_parent_kobj);
Cornelia Huck8a824722006-11-20 17:07:51 +01001594 put_device(new_parent);
1595 goto out;
1596 }
1597 old_parent = dev->parent;
1598 dev->parent = new_parent;
1599 if (old_parent)
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08001600 klist_remove(&dev->p->knode_parent);
Yinghai Lu0d358f22008-02-19 03:20:41 -08001601 if (new_parent) {
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08001602 klist_add_tail(&dev->p->knode_parent,
1603 &new_parent->p->klist_children);
Yinghai Lu0d358f22008-02-19 03:20:41 -08001604 set_dev_node(dev, dev_to_node(new_parent));
1605 }
1606
Cornelia Huck8a824722006-11-20 17:07:51 +01001607 if (!dev->class)
1608 goto out_put;
1609 error = device_move_class_links(dev, old_parent, new_parent);
1610 if (error) {
1611 /* We ignore errors on cleanup since we're hosed anyway... */
1612 device_move_class_links(dev, new_parent, old_parent);
1613 if (!kobject_move(&dev->kobj, &old_parent->kobj)) {
Cornelia Huckc744aeae2007-01-08 20:16:44 +01001614 if (new_parent)
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08001615 klist_remove(&dev->p->knode_parent);
Yinghai Lu0d358f22008-02-19 03:20:41 -08001616 dev->parent = old_parent;
1617 if (old_parent) {
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08001618 klist_add_tail(&dev->p->knode_parent,
1619 &old_parent->p->klist_children);
Yinghai Lu0d358f22008-02-19 03:20:41 -08001620 set_dev_node(dev, dev_to_node(old_parent));
1621 }
Cornelia Huck8a824722006-11-20 17:07:51 +01001622 }
Cornelia Huck63b69712008-01-21 16:09:44 +01001623 cleanup_glue_dir(dev, new_parent_kobj);
Cornelia Huck8a824722006-11-20 17:07:51 +01001624 put_device(new_parent);
1625 goto out;
1626 }
Cornelia Huckffa6a702009-03-04 12:44:00 +01001627 switch (dpm_order) {
1628 case DPM_ORDER_NONE:
1629 break;
1630 case DPM_ORDER_DEV_AFTER_PARENT:
1631 device_pm_move_after(dev, new_parent);
1632 break;
1633 case DPM_ORDER_PARENT_BEFORE_DEV:
1634 device_pm_move_before(new_parent, dev);
1635 break;
1636 case DPM_ORDER_DEV_LAST:
1637 device_pm_move_last(dev);
1638 break;
1639 }
Cornelia Huck8a824722006-11-20 17:07:51 +01001640out_put:
1641 put_device(old_parent);
1642out:
Cornelia Huckffa6a702009-03-04 12:44:00 +01001643 device_pm_unlock();
Cornelia Huck8a824722006-11-20 17:07:51 +01001644 put_device(dev);
1645 return error;
1646}
Cornelia Huck8a824722006-11-20 17:07:51 +01001647EXPORT_SYMBOL_GPL(device_move);
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08001648
1649/**
1650 * device_shutdown - call ->shutdown() on each device to shutdown.
1651 */
1652void device_shutdown(void)
1653{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001654 struct device *dev, *devn;
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08001655
1656 list_for_each_entry_safe_reverse(dev, devn, &devices_kset->list,
1657 kobj.entry) {
1658 if (dev->bus && dev->bus->shutdown) {
1659 dev_dbg(dev, "shutdown\n");
1660 dev->bus->shutdown(dev);
1661 } else if (dev->driver && dev->driver->shutdown) {
1662 dev_dbg(dev, "shutdown\n");
1663 dev->driver->shutdown(dev);
1664 }
1665 }
Dan Williamse105b8b2008-04-21 10:51:07 -07001666 kobject_put(sysfs_dev_char_kobj);
1667 kobject_put(sysfs_dev_block_kobj);
1668 kobject_put(dev_kobj);
Shaohua Li401097e2009-05-12 13:37:57 -07001669 async_synchronize_full();
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08001670}