blob: 390e664ec1c7e93d74eba989315c0e7883b4578c [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
Kay Sievers6fcf53a2009-04-30 15:23:42 +0200165 /* add device node properties if present */
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700166 if (MAJOR(dev->devt)) {
Kay Sievers6fcf53a2009-04-30 15:23:42 +0200167 const char *tmp;
168 const char *name;
169
Kay Sievers7eff2e72007-08-14 15:15:12 +0200170 add_uevent_var(env, "MAJOR=%u", MAJOR(dev->devt));
171 add_uevent_var(env, "MINOR=%u", MINOR(dev->devt));
Kay Sievers6fcf53a2009-04-30 15:23:42 +0200172 name = device_get_nodename(dev, &tmp);
173 if (name) {
174 add_uevent_var(env, "DEVNAME=%s", name);
175 kfree(tmp);
176 }
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700177 }
178
Kay Sievers414264f2007-03-12 21:08:57 +0100179 if (dev->type && dev->type->name)
Kay Sievers7eff2e72007-08-14 15:15:12 +0200180 add_uevent_var(env, "DEVTYPE=%s", dev->type->name);
Kay Sievers414264f2007-03-12 21:08:57 +0100181
Kay Sievers239378f2006-10-07 21:54:55 +0200182 if (dev->driver)
Kay Sievers7eff2e72007-08-14 15:15:12 +0200183 add_uevent_var(env, "DRIVER=%s", dev->driver->name);
Kay Sievers239378f2006-10-07 21:54:55 +0200184
Kay Sieversa87cb2a2006-09-14 11:23:28 +0200185#ifdef CONFIG_SYSFS_DEPRECATED
Kay Sievers239378f2006-10-07 21:54:55 +0200186 if (dev->class) {
187 struct device *parent = dev->parent;
188
189 /* find first bus device in parent chain */
190 while (parent && !parent->bus)
191 parent = parent->parent;
192 if (parent && parent->bus) {
193 const char *path;
194
195 path = kobject_get_path(&parent->kobj, GFP_KERNEL);
Kay Sievers2c7afd12007-05-01 13:46:26 +0200196 if (path) {
Kay Sievers7eff2e72007-08-14 15:15:12 +0200197 add_uevent_var(env, "PHYSDEVPATH=%s", path);
Kay Sievers2c7afd12007-05-01 13:46:26 +0200198 kfree(path);
199 }
Kay Sievers239378f2006-10-07 21:54:55 +0200200
Kay Sievers7eff2e72007-08-14 15:15:12 +0200201 add_uevent_var(env, "PHYSDEVBUS=%s", parent->bus->name);
Kay Sievers239378f2006-10-07 21:54:55 +0200202
203 if (parent->driver)
Kay Sievers7eff2e72007-08-14 15:15:12 +0200204 add_uevent_var(env, "PHYSDEVDRIVER=%s",
205 parent->driver->name);
Kay Sievers239378f2006-10-07 21:54:55 +0200206 }
207 } else if (dev->bus) {
Kay Sievers7eff2e72007-08-14 15:15:12 +0200208 add_uevent_var(env, "PHYSDEVBUS=%s", dev->bus->name);
Kay Sievers239378f2006-10-07 21:54:55 +0200209
210 if (dev->driver)
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800211 add_uevent_var(env, "PHYSDEVDRIVER=%s",
212 dev->driver->name);
Kay Sieversd81d9d62006-08-13 06:17:09 +0200213 }
Kay Sievers239378f2006-10-07 21:54:55 +0200214#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
Kay Sievers7eff2e72007-08-14 15:15:12 +0200216 /* have the bus specific function add its stuff */
Kay Sievers312c0042005-11-16 09:00:00 +0100217 if (dev->bus && dev->bus->uevent) {
Kay Sievers7eff2e72007-08-14 15:15:12 +0200218 retval = dev->bus->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: bus uevent() returned %d\n",
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100221 dev_name(dev), __func__, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 }
223
Kay Sievers7eff2e72007-08-14 15:15:12 +0200224 /* have the class specific function add its stuff */
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700225 if (dev->class && dev->class->dev_uevent) {
Kay Sievers7eff2e72007-08-14 15:15:12 +0200226 retval = dev->class->dev_uevent(dev, env);
Kay Sieversf9f852d2006-10-07 21:54:55 +0200227 if (retval)
Greg Kroah-Hartman7dc72b22007-11-28 23:49:41 -0800228 pr_debug("device: '%s': %s: class uevent() "
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100229 "returned %d\n", dev_name(dev),
Harvey Harrison2b3a3022008-03-04 16:41:05 -0800230 __func__, retval);
Kay Sieversf9f852d2006-10-07 21:54:55 +0200231 }
232
Kay Sievers7eff2e72007-08-14 15:15:12 +0200233 /* have the device type specific fuction add its stuff */
Kay Sieversf9f852d2006-10-07 21:54:55 +0200234 if (dev->type && dev->type->uevent) {
Kay Sievers7eff2e72007-08-14 15:15:12 +0200235 retval = dev->type->uevent(dev, env);
Kay Sieversf9f852d2006-10-07 21:54:55 +0200236 if (retval)
Greg Kroah-Hartman7dc72b22007-11-28 23:49:41 -0800237 pr_debug("device: '%s': %s: dev_type uevent() "
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100238 "returned %d\n", dev_name(dev),
Harvey Harrison2b3a3022008-03-04 16:41:05 -0800239 __func__, retval);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700240 }
241
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 return retval;
243}
244
Kay Sievers312c0042005-11-16 09:00:00 +0100245static struct kset_uevent_ops device_uevent_ops = {
246 .filter = dev_uevent_filter,
247 .name = dev_uevent_name,
248 .uevent = dev_uevent,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249};
250
Kay Sievers16574dc2007-04-06 01:40:38 +0200251static ssize_t show_uevent(struct device *dev, struct device_attribute *attr,
252 char *buf)
253{
254 struct kobject *top_kobj;
255 struct kset *kset;
Kay Sievers7eff2e72007-08-14 15:15:12 +0200256 struct kobj_uevent_env *env = NULL;
Kay Sievers16574dc2007-04-06 01:40:38 +0200257 int i;
258 size_t count = 0;
259 int retval;
260
261 /* search the kset, the device belongs to */
262 top_kobj = &dev->kobj;
Kay Sievers5c5daf62007-08-12 20:43:55 +0200263 while (!top_kobj->kset && top_kobj->parent)
264 top_kobj = top_kobj->parent;
Kay Sievers16574dc2007-04-06 01:40:38 +0200265 if (!top_kobj->kset)
266 goto out;
Kay Sievers5c5daf62007-08-12 20:43:55 +0200267
Kay Sievers16574dc2007-04-06 01:40:38 +0200268 kset = top_kobj->kset;
269 if (!kset->uevent_ops || !kset->uevent_ops->uevent)
270 goto out;
271
272 /* respect filter */
273 if (kset->uevent_ops && kset->uevent_ops->filter)
274 if (!kset->uevent_ops->filter(kset, &dev->kobj))
275 goto out;
276
Kay Sievers7eff2e72007-08-14 15:15:12 +0200277 env = kzalloc(sizeof(struct kobj_uevent_env), GFP_KERNEL);
278 if (!env)
Greg Kroah-Hartmanc7308c82007-05-02 14:14:11 +0200279 return -ENOMEM;
280
Kay Sievers16574dc2007-04-06 01:40:38 +0200281 /* let the kset specific function add its keys */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200282 retval = kset->uevent_ops->uevent(kset, &dev->kobj, env);
Kay Sievers16574dc2007-04-06 01:40:38 +0200283 if (retval)
284 goto out;
285
286 /* copy keys to file */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200287 for (i = 0; i < env->envp_idx; i++)
288 count += sprintf(&buf[count], "%s\n", env->envp[i]);
Kay Sievers16574dc2007-04-06 01:40:38 +0200289out:
Kay Sievers7eff2e72007-08-14 15:15:12 +0200290 kfree(env);
Kay Sievers16574dc2007-04-06 01:40:38 +0200291 return count;
292}
293
Kay Sieversa7fd6702005-10-01 14:49:43 +0200294static ssize_t store_uevent(struct device *dev, struct device_attribute *attr,
295 const char *buf, size_t count)
296{
Kay Sievers60a96a52007-07-08 22:29:26 +0200297 enum kobject_action action;
298
Kay Sievers5c5daf62007-08-12 20:43:55 +0200299 if (kobject_action_type(buf, count, &action) == 0) {
Kay Sievers60a96a52007-07-08 22:29:26 +0200300 kobject_uevent(&dev->kobj, action);
301 goto out;
302 }
303
304 dev_err(dev, "uevent: unsupported action-string; this will "
305 "be ignored in a future kernel version\n");
Kay Sievers312c0042005-11-16 09:00:00 +0100306 kobject_uevent(&dev->kobj, KOBJ_ADD);
Kay Sievers60a96a52007-07-08 22:29:26 +0200307out:
Kay Sieversa7fd6702005-10-01 14:49:43 +0200308 return count;
309}
310
Tejun Heoad6a1e12007-06-14 03:45:17 +0900311static struct device_attribute uevent_attr =
312 __ATTR(uevent, S_IRUGO | S_IWUSR, show_uevent, store_uevent);
313
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500314static int device_add_attributes(struct device *dev,
315 struct device_attribute *attrs)
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -0700316{
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -0700317 int error = 0;
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500318 int i;
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -0700319
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500320 if (attrs) {
321 for (i = 0; attr_name(attrs[i]); i++) {
322 error = device_create_file(dev, &attrs[i]);
323 if (error)
324 break;
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -0700325 }
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500326 if (error)
327 while (--i >= 0)
328 device_remove_file(dev, &attrs[i]);
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -0700329 }
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -0700330 return error;
331}
332
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500333static void device_remove_attributes(struct device *dev,
334 struct device_attribute *attrs)
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -0700335{
336 int i;
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500337
338 if (attrs)
339 for (i = 0; attr_name(attrs[i]); i++)
340 device_remove_file(dev, &attrs[i]);
341}
342
343static int device_add_groups(struct device *dev,
David Brownella4dbd672009-06-24 10:06:31 -0700344 const struct attribute_group **groups)
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500345{
346 int error = 0;
347 int i;
348
349 if (groups) {
350 for (i = 0; groups[i]; i++) {
351 error = sysfs_create_group(&dev->kobj, groups[i]);
352 if (error) {
353 while (--i >= 0)
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800354 sysfs_remove_group(&dev->kobj,
355 groups[i]);
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500356 break;
357 }
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -0700358 }
359 }
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500360 return error;
361}
362
363static void device_remove_groups(struct device *dev,
David Brownella4dbd672009-06-24 10:06:31 -0700364 const struct attribute_group **groups)
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500365{
366 int i;
367
368 if (groups)
369 for (i = 0; groups[i]; i++)
370 sysfs_remove_group(&dev->kobj, groups[i]);
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -0700371}
372
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700373static int device_add_attrs(struct device *dev)
374{
375 struct class *class = dev->class;
Kay Sieversf9f852d2006-10-07 21:54:55 +0200376 struct device_type *type = dev->type;
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500377 int error;
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700378
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500379 if (class) {
380 error = device_add_attributes(dev, class->dev_attrs);
Kay Sieversf9f852d2006-10-07 21:54:55 +0200381 if (error)
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500382 return error;
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700383 }
Kay Sieversf9f852d2006-10-07 21:54:55 +0200384
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500385 if (type) {
386 error = device_add_groups(dev, type->groups);
Kay Sieversf9f852d2006-10-07 21:54:55 +0200387 if (error)
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500388 goto err_remove_class_attrs;
Kay Sieversf9f852d2006-10-07 21:54:55 +0200389 }
390
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500391 error = device_add_groups(dev, dev->groups);
392 if (error)
393 goto err_remove_type_groups;
394
395 return 0;
396
397 err_remove_type_groups:
398 if (type)
399 device_remove_groups(dev, type->groups);
400 err_remove_class_attrs:
401 if (class)
402 device_remove_attributes(dev, class->dev_attrs);
403
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700404 return error;
405}
406
407static void device_remove_attrs(struct device *dev)
408{
409 struct class *class = dev->class;
Kay Sieversf9f852d2006-10-07 21:54:55 +0200410 struct device_type *type = dev->type;
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700411
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500412 device_remove_groups(dev, dev->groups);
Kay Sieversf9f852d2006-10-07 21:54:55 +0200413
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500414 if (type)
415 device_remove_groups(dev, type->groups);
416
417 if (class)
418 device_remove_attributes(dev, class->dev_attrs);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700419}
420
421
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700422static ssize_t show_dev(struct device *dev, struct device_attribute *attr,
423 char *buf)
424{
425 return print_dev_t(buf, dev->devt);
426}
427
Tejun Heoad6a1e12007-06-14 03:45:17 +0900428static struct device_attribute devt_attr =
429 __ATTR(dev, S_IRUGO, show_dev, NULL);
430
Greg Kroah-Hartman881c6cfd2007-11-01 09:29:06 -0600431/* kset to create /sys/devices/ */
432struct kset *devices_kset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800435 * device_create_file - create sysfs attribute file for device.
436 * @dev: device.
437 * @attr: device attribute descriptor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800439int device_create_file(struct device *dev, struct device_attribute *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440{
441 int error = 0;
Cornelia Huck0c98b192008-01-31 10:39:38 +0100442 if (dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 error = sysfs_create_file(&dev->kobj, &attr->attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 return error;
445}
446
447/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800448 * device_remove_file - remove sysfs attribute file.
449 * @dev: device.
450 * @attr: device attribute descriptor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800452void device_remove_file(struct device *dev, struct device_attribute *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453{
Cornelia Huck0c98b192008-01-31 10:39:38 +0100454 if (dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 sysfs_remove_file(&dev->kobj, &attr->attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456}
457
Greg Kroah-Hartman2589f1882006-09-19 09:39:19 -0700458/**
459 * device_create_bin_file - create sysfs binary attribute file for device.
460 * @dev: device.
461 * @attr: device binary attribute descriptor.
462 */
463int device_create_bin_file(struct device *dev, struct bin_attribute *attr)
464{
465 int error = -EINVAL;
466 if (dev)
467 error = sysfs_create_bin_file(&dev->kobj, attr);
468 return error;
469}
470EXPORT_SYMBOL_GPL(device_create_bin_file);
471
472/**
473 * device_remove_bin_file - remove sysfs binary attribute file
474 * @dev: device.
475 * @attr: device binary attribute descriptor.
476 */
477void device_remove_bin_file(struct device *dev, struct bin_attribute *attr)
478{
479 if (dev)
480 sysfs_remove_bin_file(&dev->kobj, attr);
481}
482EXPORT_SYMBOL_GPL(device_remove_bin_file);
483
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400484/**
Alan Stern523ded72007-04-26 00:12:04 -0700485 * device_schedule_callback_owner - helper to schedule a callback for a device
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400486 * @dev: device.
487 * @func: callback function to invoke later.
Alan Stern523ded72007-04-26 00:12:04 -0700488 * @owner: module owning the callback routine
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400489 *
490 * Attribute methods must not unregister themselves or their parent device
491 * (which would amount to the same thing). Attempts to do so will deadlock,
492 * since unregistration is mutually exclusive with driver callbacks.
493 *
494 * Instead methods can call this routine, which will attempt to allocate
495 * and schedule a workqueue request to call back @func with @dev as its
496 * argument in the workqueue's process context. @dev will be pinned until
497 * @func returns.
498 *
Alan Stern523ded72007-04-26 00:12:04 -0700499 * This routine is usually called via the inline device_schedule_callback(),
500 * which automatically sets @owner to THIS_MODULE.
501 *
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400502 * Returns 0 if the request was submitted, -ENOMEM if storage could not
Alan Stern523ded72007-04-26 00:12:04 -0700503 * be allocated, -ENODEV if a reference to @owner isn't available.
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400504 *
505 * NOTE: This routine won't work if CONFIG_SYSFS isn't set! It uses an
506 * underlying sysfs routine (since it is intended for use by attribute
507 * methods), and if sysfs isn't available you'll get nothing but -ENOSYS.
508 */
Alan Stern523ded72007-04-26 00:12:04 -0700509int device_schedule_callback_owner(struct device *dev,
510 void (*func)(struct device *), struct module *owner)
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400511{
512 return sysfs_schedule_callback(&dev->kobj,
Alan Stern523ded72007-04-26 00:12:04 -0700513 (void (*)(void *)) func, dev, owner);
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400514}
Alan Stern523ded72007-04-26 00:12:04 -0700515EXPORT_SYMBOL_GPL(device_schedule_callback_owner);
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400516
James Bottomley34bb61f2005-09-06 16:56:51 -0700517static void klist_children_get(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 get_device(dev);
523}
524
525static void klist_children_put(struct klist_node *n)
526{
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -0800527 struct device_private *p = to_device_private_parent(n);
528 struct device *dev = p->device;
James Bottomley34bb61f2005-09-06 16:56:51 -0700529
530 put_device(dev);
531}
532
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800534 * device_initialize - init device structure.
535 * @dev: device.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 *
Cornelia Huck57394112008-09-03 18:26:40 +0200537 * This prepares the device for use by other layers by initializing
538 * its fields.
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800539 * It is the first half of device_register(), if called by
Cornelia Huck57394112008-09-03 18:26:40 +0200540 * that function, though it can also be called separately, so one
541 * may use @dev's fields. In particular, get_device()/put_device()
542 * may be used for reference counting of @dev after calling this
543 * function.
544 *
545 * NOTE: Use put_device() to give up your reference instead of freeing
546 * @dev directly once you have called this function.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548void device_initialize(struct device *dev)
549{
Greg Kroah-Hartman881c6cfd2007-11-01 09:29:06 -0600550 dev->kobj.kset = devices_kset;
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -0700551 kobject_init(&dev->kobj, &device_ktype);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 INIT_LIST_HEAD(&dev->dma_pools);
mochel@digitalimplant.orgaf703162005-03-21 10:41:04 -0800553 init_MUTEX(&dev->sem);
Tejun Heo9ac78492007-01-20 16:00:26 +0900554 spin_lock_init(&dev->devres_lock);
555 INIT_LIST_HEAD(&dev->devres_head);
David Brownell0ac85242005-09-12 19:39:34 -0700556 device_init_wakeup(dev, 0);
Alan Stern3b98aea2008-08-07 13:06:12 -0400557 device_pm_init(dev);
Christoph Hellwig87348132006-12-06 20:32:33 -0800558 set_dev_node(dev, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559}
560
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +0100561#ifdef CONFIG_SYSFS_DEPRECATED
Kay Sieversda231fd2007-11-21 17:29:15 +0100562static struct kobject *get_device_parent(struct device *dev,
563 struct device *parent)
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +0100564{
Kay Sieversda231fd2007-11-21 17:29:15 +0100565 /* class devices without a parent live in /sys/class/<classname>/ */
Dmitry Torokhov3eb215d2007-10-07 12:22:21 -0400566 if (dev->class && (!parent || parent->class != dev->class))
Greg Kroah-Hartman1fbfee62008-05-28 09:28:39 -0700567 return &dev->class->p->class_subsys.kobj;
Kay Sieversda231fd2007-11-21 17:29:15 +0100568 /* all other devices keep their parent */
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +0100569 else if (parent)
Cornelia Huckc744aeae2007-01-08 20:16:44 +0100570 return &parent->kobj;
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +0100571
Cornelia Huckc744aeae2007-01-08 20:16:44 +0100572 return NULL;
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +0100573}
Kay Sieversda231fd2007-11-21 17:29:15 +0100574
575static inline void cleanup_device_parent(struct device *dev) {}
Cornelia Huck63b69712008-01-21 16:09:44 +0100576static inline void cleanup_glue_dir(struct device *dev,
577 struct kobject *glue_dir) {}
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +0100578#else
Kay Sievers86406242007-03-14 03:25:56 +0100579static struct kobject *virtual_device_parent(struct device *dev)
Greg Kroah-Hartmanf0ee61a2006-10-23 10:40:54 -0700580{
Kay Sievers86406242007-03-14 03:25:56 +0100581 static struct kobject *virtual_dir = NULL;
Greg Kroah-Hartmanf0ee61a2006-10-23 10:40:54 -0700582
Kay Sievers86406242007-03-14 03:25:56 +0100583 if (!virtual_dir)
Greg Kroah-Hartman4ff6abf2007-11-05 22:24:43 -0800584 virtual_dir = kobject_create_and_add("virtual",
Greg Kroah-Hartman881c6cfd2007-11-01 09:29:06 -0600585 &devices_kset->kobj);
Greg Kroah-Hartmanf0ee61a2006-10-23 10:40:54 -0700586
Kay Sievers86406242007-03-14 03:25:56 +0100587 return virtual_dir;
Greg Kroah-Hartmanf0ee61a2006-10-23 10:40:54 -0700588}
589
Kay Sieversda231fd2007-11-21 17:29:15 +0100590static struct kobject *get_device_parent(struct device *dev,
591 struct device *parent)
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +0100592{
Greg Kroah-Hartman43968d22007-11-05 22:24:43 -0800593 int retval;
594
Kay Sievers86406242007-03-14 03:25:56 +0100595 if (dev->class) {
596 struct kobject *kobj = NULL;
597 struct kobject *parent_kobj;
598 struct kobject *k;
599
600 /*
601 * If we have no parent, we live in "virtual".
Kay Sievers0f4dafc2007-12-19 01:40:42 +0100602 * Class-devices with a non class-device as parent, live
603 * in a "glue" directory to prevent namespace collisions.
Kay Sievers86406242007-03-14 03:25:56 +0100604 */
605 if (parent == NULL)
606 parent_kobj = virtual_device_parent(dev);
607 else if (parent->class)
608 return &parent->kobj;
609 else
610 parent_kobj = &parent->kobj;
611
612 /* find our class-directory at the parent and reference it */
Greg Kroah-Hartman7c714482008-01-22 18:17:41 -0500613 spin_lock(&dev->class->p->class_dirs.list_lock);
614 list_for_each_entry(k, &dev->class->p->class_dirs.list, entry)
Kay Sievers86406242007-03-14 03:25:56 +0100615 if (k->parent == parent_kobj) {
616 kobj = kobject_get(k);
617 break;
618 }
Greg Kroah-Hartman7c714482008-01-22 18:17:41 -0500619 spin_unlock(&dev->class->p->class_dirs.list_lock);
Kay Sievers86406242007-03-14 03:25:56 +0100620 if (kobj)
621 return kobj;
622
623 /* or create a new class-directory at the parent device */
Greg Kroah-Hartman43968d22007-11-05 22:24:43 -0800624 k = kobject_create();
625 if (!k)
626 return NULL;
Greg Kroah-Hartman7c714482008-01-22 18:17:41 -0500627 k->kset = &dev->class->p->class_dirs;
Greg Kroah-Hartmanb2d6db52007-12-17 23:05:35 -0700628 retval = kobject_add(k, parent_kobj, "%s", dev->class->name);
Greg Kroah-Hartman43968d22007-11-05 22:24:43 -0800629 if (retval < 0) {
630 kobject_put(k);
631 return NULL;
632 }
Kay Sievers0f4dafc2007-12-19 01:40:42 +0100633 /* do not emit an uevent for this simple "glue" directory */
Greg Kroah-Hartman43968d22007-11-05 22:24:43 -0800634 return k;
Kay Sievers86406242007-03-14 03:25:56 +0100635 }
636
637 if (parent)
Cornelia Huckc744aeae2007-01-08 20:16:44 +0100638 return &parent->kobj;
639 return NULL;
640}
Kay Sieversda231fd2007-11-21 17:29:15 +0100641
Cornelia Huck63b69712008-01-21 16:09:44 +0100642static void cleanup_glue_dir(struct device *dev, struct kobject *glue_dir)
Kay Sieversda231fd2007-11-21 17:29:15 +0100643{
Kay Sievers0f4dafc2007-12-19 01:40:42 +0100644 /* see if we live in a "glue" directory */
Cornelia Huckc1fe5392008-02-27 15:38:23 +0100645 if (!glue_dir || !dev->class ||
Greg Kroah-Hartman7c714482008-01-22 18:17:41 -0500646 glue_dir->kset != &dev->class->p->class_dirs)
Kay Sieversda231fd2007-11-21 17:29:15 +0100647 return;
648
Kay Sievers0f4dafc2007-12-19 01:40:42 +0100649 kobject_put(glue_dir);
Kay Sieversda231fd2007-11-21 17:29:15 +0100650}
Cornelia Huck63b69712008-01-21 16:09:44 +0100651
652static void cleanup_device_parent(struct device *dev)
653{
654 cleanup_glue_dir(dev, dev->kobj.parent);
655}
Cornelia Huckc744aeae2007-01-08 20:16:44 +0100656#endif
Kay Sievers86406242007-03-14 03:25:56 +0100657
Cornelia Huck63b69712008-01-21 16:09:44 +0100658static void setup_parent(struct device *dev, struct device *parent)
Cornelia Huckc744aeae2007-01-08 20:16:44 +0100659{
660 struct kobject *kobj;
661 kobj = get_device_parent(dev, parent);
Cornelia Huckc744aeae2007-01-08 20:16:44 +0100662 if (kobj)
663 dev->kobj.parent = kobj;
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +0100664}
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +0100665
Cornelia Huck2ee97ca2007-07-18 01:43:47 -0700666static int device_add_class_symlinks(struct device *dev)
667{
668 int error;
669
670 if (!dev->class)
671 return 0;
Kay Sieversda231fd2007-11-21 17:29:15 +0100672
Greg Kroah-Hartman1fbfee62008-05-28 09:28:39 -0700673 error = sysfs_create_link(&dev->kobj,
674 &dev->class->p->class_subsys.kobj,
Cornelia Huck2ee97ca2007-07-18 01:43:47 -0700675 "subsystem");
676 if (error)
677 goto out;
Kay Sieversda231fd2007-11-21 17:29:15 +0100678
679#ifdef CONFIG_SYSFS_DEPRECATED
680 /* stacked class devices need a symlink in the class directory */
Greg Kroah-Hartman1fbfee62008-05-28 09:28:39 -0700681 if (dev->kobj.parent != &dev->class->p->class_subsys.kobj &&
Greg Kroah-Hartman4e886c22008-01-27 12:12:43 -0800682 device_is_not_partition(dev)) {
Greg Kroah-Hartman1fbfee62008-05-28 09:28:39 -0700683 error = sysfs_create_link(&dev->class->p->class_subsys.kobj,
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100684 &dev->kobj, dev_name(dev));
Cornelia Huck2ee97ca2007-07-18 01:43:47 -0700685 if (error)
686 goto out_subsys;
687 }
Kay Sieversda231fd2007-11-21 17:29:15 +0100688
Greg Kroah-Hartman4e886c22008-01-27 12:12:43 -0800689 if (dev->parent && device_is_not_partition(dev)) {
Kay Sieversda231fd2007-11-21 17:29:15 +0100690 struct device *parent = dev->parent;
691 char *class_name;
Dmitry Torokhov4f01a752007-09-18 22:46:50 -0700692
Kay Sieversda231fd2007-11-21 17:29:15 +0100693 /*
694 * stacked class devices have the 'device' link
695 * pointing to the bus device instead of the parent
696 */
697 while (parent->class && !parent->bus && parent->parent)
698 parent = parent->parent;
Dmitry Torokhov4f01a752007-09-18 22:46:50 -0700699
Kay Sieversda231fd2007-11-21 17:29:15 +0100700 error = sysfs_create_link(&dev->kobj,
701 &parent->kobj,
702 "device");
703 if (error)
704 goto out_busid;
Dmitry Torokhov4f01a752007-09-18 22:46:50 -0700705
Kay Sieversda231fd2007-11-21 17:29:15 +0100706 class_name = make_class_name(dev->class->name,
707 &dev->kobj);
708 if (class_name)
709 error = sysfs_create_link(&dev->parent->kobj,
710 &dev->kobj, class_name);
711 kfree(class_name);
712 if (error)
713 goto out_device;
714 }
715 return 0;
716
717out_device:
Greg Kroah-Hartman4e886c22008-01-27 12:12:43 -0800718 if (dev->parent && device_is_not_partition(dev))
Kay Sieversda231fd2007-11-21 17:29:15 +0100719 sysfs_remove_link(&dev->kobj, "device");
720out_busid:
Greg Kroah-Hartman1fbfee62008-05-28 09:28:39 -0700721 if (dev->kobj.parent != &dev->class->p->class_subsys.kobj &&
Greg Kroah-Hartman4e886c22008-01-27 12:12:43 -0800722 device_is_not_partition(dev))
Greg Kroah-Hartman1fbfee62008-05-28 09:28:39 -0700723 sysfs_remove_link(&dev->class->p->class_subsys.kobj,
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100724 dev_name(dev));
Dmitry Torokhov4f01a752007-09-18 22:46:50 -0700725#else
Kay Sieversda231fd2007-11-21 17:29:15 +0100726 /* link in the class directory pointing to the device */
Greg Kroah-Hartman1fbfee62008-05-28 09:28:39 -0700727 error = sysfs_create_link(&dev->class->p->class_subsys.kobj,
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100728 &dev->kobj, dev_name(dev));
Kay Sieversda231fd2007-11-21 17:29:15 +0100729 if (error)
730 goto out_subsys;
731
Greg Kroah-Hartman4e886c22008-01-27 12:12:43 -0800732 if (dev->parent && device_is_not_partition(dev)) {
Dmitry Torokhov4f01a752007-09-18 22:46:50 -0700733 error = sysfs_create_link(&dev->kobj, &dev->parent->kobj,
734 "device");
735 if (error)
736 goto out_busid;
Cornelia Huck2ee97ca2007-07-18 01:43:47 -0700737 }
738 return 0;
739
Cornelia Huck2ee97ca2007-07-18 01:43:47 -0700740out_busid:
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100741 sysfs_remove_link(&dev->class->p->class_subsys.kobj, dev_name(dev));
Kay Sieversda231fd2007-11-21 17:29:15 +0100742#endif
743
Cornelia Huck2ee97ca2007-07-18 01:43:47 -0700744out_subsys:
745 sysfs_remove_link(&dev->kobj, "subsystem");
746out:
747 return error;
748}
749
750static void device_remove_class_symlinks(struct device *dev)
751{
752 if (!dev->class)
753 return;
Kay Sieversda231fd2007-11-21 17:29:15 +0100754
Cornelia Huck2ee97ca2007-07-18 01:43:47 -0700755#ifdef CONFIG_SYSFS_DEPRECATED
Greg Kroah-Hartman4e886c22008-01-27 12:12:43 -0800756 if (dev->parent && device_is_not_partition(dev)) {
Cornelia Huck2ee97ca2007-07-18 01:43:47 -0700757 char *class_name;
758
759 class_name = make_class_name(dev->class->name, &dev->kobj);
760 if (class_name) {
761 sysfs_remove_link(&dev->parent->kobj, class_name);
762 kfree(class_name);
763 }
Cornelia Huck2ee97ca2007-07-18 01:43:47 -0700764 sysfs_remove_link(&dev->kobj, "device");
765 }
Kay Sieversda231fd2007-11-21 17:29:15 +0100766
Greg Kroah-Hartman1fbfee62008-05-28 09:28:39 -0700767 if (dev->kobj.parent != &dev->class->p->class_subsys.kobj &&
Greg Kroah-Hartman4e886c22008-01-27 12:12:43 -0800768 device_is_not_partition(dev))
Greg Kroah-Hartman1fbfee62008-05-28 09:28:39 -0700769 sysfs_remove_link(&dev->class->p->class_subsys.kobj,
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100770 dev_name(dev));
Kay Sieversda231fd2007-11-21 17:29:15 +0100771#else
Greg Kroah-Hartman4e886c22008-01-27 12:12:43 -0800772 if (dev->parent && device_is_not_partition(dev))
Kay Sieversda231fd2007-11-21 17:29:15 +0100773 sysfs_remove_link(&dev->kobj, "device");
774
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100775 sysfs_remove_link(&dev->class->p->class_subsys.kobj, dev_name(dev));
Kay Sieversda231fd2007-11-21 17:29:15 +0100776#endif
777
Cornelia Huck2ee97ca2007-07-18 01:43:47 -0700778 sysfs_remove_link(&dev->kobj, "subsystem");
779}
780
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781/**
Stephen Rothwell413c2392008-05-30 10:16:40 +1000782 * dev_set_name - set a device name
783 * @dev: device
Randy Dunlap46232362008-06-04 21:40:43 -0700784 * @fmt: format string for the device's name
Stephen Rothwell413c2392008-05-30 10:16:40 +1000785 */
786int dev_set_name(struct device *dev, const char *fmt, ...)
787{
788 va_list vargs;
Kay Sievers1fa5ae82009-01-25 15:17:37 +0100789 int err;
Stephen Rothwell413c2392008-05-30 10:16:40 +1000790
791 va_start(vargs, fmt);
Kay Sievers1fa5ae82009-01-25 15:17:37 +0100792 err = kobject_set_name_vargs(&dev->kobj, fmt, vargs);
Stephen Rothwell413c2392008-05-30 10:16:40 +1000793 va_end(vargs);
Kay Sievers1fa5ae82009-01-25 15:17:37 +0100794 return err;
Stephen Rothwell413c2392008-05-30 10:16:40 +1000795}
796EXPORT_SYMBOL_GPL(dev_set_name);
797
798/**
Dan Williamse105b8b2008-04-21 10:51:07 -0700799 * device_to_dev_kobj - select a /sys/dev/ directory for the device
800 * @dev: device
801 *
802 * By default we select char/ for new entries. Setting class->dev_obj
803 * to NULL prevents an entry from being created. class->dev_kobj must
804 * be set (or cleared) before any devices are registered to the class
805 * otherwise device_create_sys_dev_entry() and
806 * device_remove_sys_dev_entry() will disagree about the the presence
807 * of the link.
808 */
809static struct kobject *device_to_dev_kobj(struct device *dev)
810{
811 struct kobject *kobj;
812
813 if (dev->class)
814 kobj = dev->class->dev_kobj;
815 else
816 kobj = sysfs_dev_char_kobj;
817
818 return kobj;
819}
820
821static int device_create_sys_dev_entry(struct device *dev)
822{
823 struct kobject *kobj = device_to_dev_kobj(dev);
824 int error = 0;
825 char devt_str[15];
826
827 if (kobj) {
828 format_dev_t(devt_str, dev->devt);
829 error = sysfs_create_link(kobj, &dev->kobj, devt_str);
830 }
831
832 return error;
833}
834
835static void device_remove_sys_dev_entry(struct device *dev)
836{
837 struct kobject *kobj = device_to_dev_kobj(dev);
838 char devt_str[15];
839
840 if (kobj) {
841 format_dev_t(devt_str, dev->devt);
842 sysfs_remove_link(kobj, devt_str);
843 }
844}
845
Greg Kroah-Hartmanb4028432009-05-11 14:16:57 -0700846int device_private_init(struct device *dev)
847{
848 dev->p = kzalloc(sizeof(*dev->p), GFP_KERNEL);
849 if (!dev->p)
850 return -ENOMEM;
851 dev->p->device = dev;
852 klist_init(&dev->p->klist_children, klist_children_get,
853 klist_children_put);
854 return 0;
855}
856
Dan Williamse105b8b2008-04-21 10:51:07 -0700857/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800858 * device_add - add device to device hierarchy.
859 * @dev: device.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800861 * This is part 2 of device_register(), though may be called
862 * separately _iff_ device_initialize() has been called separately.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 *
Cornelia Huck57394112008-09-03 18:26:40 +0200864 * This adds @dev to the kobject hierarchy via kobject_add(), adds it
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800865 * to the global and sibling lists for the device, then
866 * adds it to the other relevant subsystems of the driver model.
Cornelia Huck57394112008-09-03 18:26:40 +0200867 *
868 * NOTE: _Never_ directly free @dev after calling this function, even
869 * if it returned an error! Always use put_device() to give up your
870 * reference instead.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 */
872int device_add(struct device *dev)
873{
874 struct device *parent = NULL;
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +0200875 struct class_interface *class_intf;
Greg Kroah-Hartmanc906a482008-05-30 10:45:12 -0700876 int error = -EINVAL;
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100877
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 dev = get_device(dev);
Greg Kroah-Hartmanc906a482008-05-30 10:45:12 -0700879 if (!dev)
880 goto done;
881
Greg Kroah-Hartmanfb069a52008-12-16 12:23:36 -0800882 if (!dev->p) {
Greg Kroah-Hartmanb4028432009-05-11 14:16:57 -0700883 error = device_private_init(dev);
884 if (error)
885 goto done;
Greg Kroah-Hartmanfb069a52008-12-16 12:23:36 -0800886 }
Greg Kroah-Hartmanfb069a52008-12-16 12:23:36 -0800887
Kay Sievers1fa5ae82009-01-25 15:17:37 +0100888 /*
889 * for statically allocated devices, which should all be converted
890 * some day, we need to initialize the name. We prevent reading back
891 * the name, and force the use of dev_name()
892 */
893 if (dev->init_name) {
Greg Kroah-Hartmanacc0e902009-06-02 15:39:55 -0700894 dev_set_name(dev, "%s", dev->init_name);
Kay Sievers1fa5ae82009-01-25 15:17:37 +0100895 dev->init_name = NULL;
896 }
Greg Kroah-Hartmanc906a482008-05-30 10:45:12 -0700897
Kay Sievers1fa5ae82009-01-25 15:17:37 +0100898 if (!dev_name(dev))
Kay Sievers5c8563d2009-05-28 14:24:07 -0700899 goto name_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100901 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
Greg Kroah-Hartmanc205ef42006-08-07 22:19:37 -0700902
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 parent = get_device(dev->parent);
Cornelia Huck63b69712008-01-21 16:09:44 +0100904 setup_parent(dev, parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905
Yinghai Lu0d358f22008-02-19 03:20:41 -0800906 /* use parent numa_node */
907 if (parent)
908 set_dev_node(dev, dev_to_node(parent));
909
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 /* first, register with generic layer. */
Kay Sievers8a577ff2009-04-18 15:05:45 -0700911 /* we require the name to be set before, and pass NULL */
912 error = kobject_add(&dev->kobj, dev->kobj.parent, NULL);
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +0100913 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 goto Error;
Kay Sieversa7fd6702005-10-01 14:49:43 +0200915
Brian Walsh37022642006-08-14 22:43:19 -0700916 /* notify platform of device entry */
917 if (platform_notify)
918 platform_notify(dev);
919
Tejun Heoad6a1e12007-06-14 03:45:17 +0900920 error = device_create_file(dev, &uevent_attr);
Cornelia Hucka306eea2006-09-22 11:37:13 +0200921 if (error)
922 goto attrError;
Kay Sieversa7fd6702005-10-01 14:49:43 +0200923
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700924 if (MAJOR(dev->devt)) {
Tejun Heoad6a1e12007-06-14 03:45:17 +0900925 error = device_create_file(dev, &devt_attr);
926 if (error)
Cornelia Hucka306eea2006-09-22 11:37:13 +0200927 goto ueventattrError;
Dan Williamse105b8b2008-04-21 10:51:07 -0700928
929 error = device_create_sys_dev_entry(dev);
930 if (error)
931 goto devtattrError;
Kay Sievers2b2af542009-04-30 15:23:42 +0200932
933 devtmpfs_create_node(dev);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700934 }
935
Cornelia Huck2ee97ca2007-07-18 01:43:47 -0700936 error = device_add_class_symlinks(dev);
937 if (error)
938 goto SymlinkError;
Cornelia Huckdc0afa82007-07-09 11:39:18 -0700939 error = device_add_attrs(dev);
940 if (error)
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700941 goto AttrsError;
Cornelia Huckdc0afa82007-07-09 11:39:18 -0700942 error = bus_add_device(dev);
943 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 goto BusError;
Alan Stern3b98aea2008-08-07 13:06:12 -0400945 error = dpm_sysfs_add(dev);
Rafael J. Wysocki57eee3d2008-03-12 00:59:38 +0100946 if (error)
Alan Stern3b98aea2008-08-07 13:06:12 -0400947 goto DPMError;
948 device_pm_add(dev);
Alan Sternec0676ee2008-12-05 14:10:31 -0500949
950 /* Notify clients of device addition. This call must come
951 * after dpm_sysf_add() and before kobject_uevent().
952 */
953 if (dev->bus)
954 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
955 BUS_NOTIFY_ADD_DEVICE, dev);
956
Cornelia Huck83b5fb4c2007-03-29 11:12:11 +0200957 kobject_uevent(&dev->kobj, KOBJ_ADD);
Alan Stern2023c612009-07-30 15:27:18 -0400958 bus_probe_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 if (parent)
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -0800960 klist_add_tail(&dev->p->knode_parent,
961 &parent->p->klist_children);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962
Greg Kroah-Hartman5d9fd162006-06-22 17:17:32 -0700963 if (dev->class) {
Dave Youngf75b1c62008-05-28 09:28:39 -0700964 mutex_lock(&dev->class->p->class_mutex);
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +0200965 /* tie the class to the device */
Tejun Heo5a3ceb82008-08-25 19:50:19 +0200966 klist_add_tail(&dev->knode_class,
967 &dev->class->p->class_devices);
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +0200968
969 /* notify any interfaces that the device is here */
Greg Kroah-Hartman184f1f72008-05-28 09:28:39 -0700970 list_for_each_entry(class_intf,
971 &dev->class->p->class_interfaces, node)
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +0200972 if (class_intf->add_dev)
973 class_intf->add_dev(dev, class_intf);
Dave Youngf75b1c62008-05-28 09:28:39 -0700974 mutex_unlock(&dev->class->p->class_mutex);
Greg Kroah-Hartman5d9fd162006-06-22 17:17:32 -0700975 }
Greg Kroah-Hartmanc906a482008-05-30 10:45:12 -0700976done:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 put_device(dev);
978 return error;
Alan Stern3b98aea2008-08-07 13:06:12 -0400979 DPMError:
Rafael J. Wysocki57eee3d2008-03-12 00:59:38 +0100980 bus_remove_device(dev);
981 BusError:
James Simmons82f0cf92007-02-21 17:44:51 +0000982 device_remove_attrs(dev);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700983 AttrsError:
Cornelia Huck2ee97ca2007-07-18 01:43:47 -0700984 device_remove_class_symlinks(dev);
985 SymlinkError:
Tejun Heoad6a1e12007-06-14 03:45:17 +0900986 if (MAJOR(dev->devt))
Dan Williamse105b8b2008-04-21 10:51:07 -0700987 device_remove_sys_dev_entry(dev);
988 devtattrError:
989 if (MAJOR(dev->devt))
Tejun Heoad6a1e12007-06-14 03:45:17 +0900990 device_remove_file(dev, &devt_attr);
Cornelia Hucka306eea2006-09-22 11:37:13 +0200991 ueventattrError:
Tejun Heoad6a1e12007-06-14 03:45:17 +0900992 device_remove_file(dev, &uevent_attr);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700993 attrError:
Kay Sievers312c0042005-11-16 09:00:00 +0100994 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 kobject_del(&dev->kobj);
996 Error:
Cornelia Huck63b69712008-01-21 16:09:44 +0100997 cleanup_device_parent(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 if (parent)
999 put_device(parent);
Kay Sievers5c8563d2009-05-28 14:24:07 -07001000name_error:
1001 kfree(dev->p);
1002 dev->p = NULL;
Greg Kroah-Hartmanc906a482008-05-30 10:45:12 -07001003 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004}
1005
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001007 * device_register - register a device with the system.
1008 * @dev: pointer to the device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001010 * This happens in two clean steps - initialize the device
1011 * and add it to the system. The two steps can be called
1012 * separately, but this is the easiest and most common.
1013 * I.e. you should only call the two helpers separately if
1014 * have a clearly defined need to use and refcount the device
1015 * before it is added to the hierarchy.
Cornelia Huck57394112008-09-03 18:26:40 +02001016 *
1017 * NOTE: _Never_ directly free @dev after calling this function, even
1018 * if it returned an error! Always use put_device() to give up the
1019 * reference initialized in this function instead.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021int device_register(struct device *dev)
1022{
1023 device_initialize(dev);
1024 return device_add(dev);
1025}
1026
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001028 * get_device - increment reference count for device.
1029 * @dev: device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001031 * This simply forwards the call to kobject_get(), though
1032 * we do take care to provide for the case that we get a NULL
1033 * pointer passed in.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001035struct device *get_device(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036{
1037 return dev ? to_dev(kobject_get(&dev->kobj)) : NULL;
1038}
1039
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001041 * put_device - decrement reference count.
1042 * @dev: device in question.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001044void put_device(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045{
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001046 /* might_sleep(); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 if (dev)
1048 kobject_put(&dev->kobj);
1049}
1050
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001052 * device_del - delete device from system.
1053 * @dev: device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001055 * This is the first part of the device unregistration
1056 * sequence. This removes the device from the lists we control
1057 * from here, has it removed from the other driver model
1058 * subsystems it was added to in device_add(), and removes it
1059 * from the kobject hierarchy.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001061 * NOTE: this should be called manually _iff_ device_add() was
1062 * also called manually.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001064void device_del(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001066 struct device *parent = dev->parent;
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02001067 struct class_interface *class_intf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068
Alan Sternec0676ee2008-12-05 14:10:31 -05001069 /* Notify clients of device removal. This call must come
1070 * before dpm_sysfs_remove().
1071 */
1072 if (dev->bus)
1073 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
1074 BUS_NOTIFY_DEL_DEVICE, dev);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001075 device_pm_remove(dev);
Alan Stern3b98aea2008-08-07 13:06:12 -04001076 dpm_sysfs_remove(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 if (parent)
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08001078 klist_del(&dev->p->knode_parent);
Dan Williamse105b8b2008-04-21 10:51:07 -07001079 if (MAJOR(dev->devt)) {
Kay Sievers2b2af542009-04-30 15:23:42 +02001080 devtmpfs_delete_node(dev);
Dan Williamse105b8b2008-04-21 10:51:07 -07001081 device_remove_sys_dev_entry(dev);
Tejun Heoad6a1e12007-06-14 03:45:17 +09001082 device_remove_file(dev, &devt_attr);
Dan Williamse105b8b2008-04-21 10:51:07 -07001083 }
Kay Sieversb9d9c822006-06-15 15:31:56 +02001084 if (dev->class) {
Kay Sieversda231fd2007-11-21 17:29:15 +01001085 device_remove_class_symlinks(dev);
Kay Sievers99ef3ef2006-09-14 11:23:28 +02001086
Dave Youngf75b1c62008-05-28 09:28:39 -07001087 mutex_lock(&dev->class->p->class_mutex);
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02001088 /* notify any interfaces that the device is now gone */
Greg Kroah-Hartman184f1f72008-05-28 09:28:39 -07001089 list_for_each_entry(class_intf,
1090 &dev->class->p->class_interfaces, node)
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02001091 if (class_intf->remove_dev)
1092 class_intf->remove_dev(dev, class_intf);
1093 /* remove the device from the class list */
Tejun Heo5a3ceb82008-08-25 19:50:19 +02001094 klist_del(&dev->knode_class);
Dave Youngf75b1c62008-05-28 09:28:39 -07001095 mutex_unlock(&dev->class->p->class_mutex);
Kay Sieversb9d9c822006-06-15 15:31:56 +02001096 }
Tejun Heoad6a1e12007-06-14 03:45:17 +09001097 device_remove_file(dev, &uevent_attr);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07001098 device_remove_attrs(dev);
Benjamin Herrenschmidt28953532006-11-08 19:46:14 -08001099 bus_remove_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100
Tejun Heo2f8d16a2007-03-09 19:34:19 +09001101 /*
1102 * Some platform devices are driven without driver attached
1103 * and managed resources may have been acquired. Make sure
1104 * all resources are released.
1105 */
1106 devres_release_all(dev);
1107
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 /* Notify the platform of the removal, in case they
1109 * need to do anything...
1110 */
1111 if (platform_notify_remove)
1112 platform_notify_remove(dev);
Kay Sievers312c0042005-11-16 09:00:00 +01001113 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
Kay Sieversda231fd2007-11-21 17:29:15 +01001114 cleanup_device_parent(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 kobject_del(&dev->kobj);
Kay Sieversda231fd2007-11-21 17:29:15 +01001116 put_device(parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117}
1118
1119/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001120 * device_unregister - unregister device from system.
1121 * @dev: device going away.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001123 * We do this in two parts, like we do device_register(). First,
1124 * we remove it from all the subsystems with device_del(), then
1125 * we decrement the reference count via put_device(). If that
1126 * is the final reference count, the device will be cleaned up
1127 * via device_release() above. Otherwise, the structure will
1128 * stick around until the final reference to the device is dropped.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001130void device_unregister(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131{
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01001132 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 device_del(dev);
1134 put_device(dev);
1135}
1136
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001137static struct device *next_device(struct klist_iter *i)
mochel@digitalimplant.org36239572005-03-24 19:08:30 -08001138{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001139 struct klist_node *n = klist_next(i);
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08001140 struct device *dev = NULL;
1141 struct device_private *p;
1142
1143 if (n) {
1144 p = to_device_private_parent(n);
1145 dev = p->device;
1146 }
1147 return dev;
mochel@digitalimplant.org36239572005-03-24 19:08:30 -08001148}
1149
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150/**
Kay Sievers6fcf53a2009-04-30 15:23:42 +02001151 * device_get_nodename - path of device node file
1152 * @dev: device
1153 * @tmp: possibly allocated string
1154 *
1155 * Return the relative path of a possible device node.
1156 * Non-default names may need to allocate a memory to compose
1157 * a name. This memory is returned in tmp and needs to be
1158 * freed by the caller.
1159 */
1160const char *device_get_nodename(struct device *dev, const char **tmp)
1161{
1162 char *s;
1163
1164 *tmp = NULL;
1165
1166 /* the device type may provide a specific name */
1167 if (dev->type && dev->type->nodename)
1168 *tmp = dev->type->nodename(dev);
1169 if (*tmp)
1170 return *tmp;
1171
1172 /* the class may provide a specific name */
1173 if (dev->class && dev->class->nodename)
1174 *tmp = dev->class->nodename(dev);
1175 if (*tmp)
1176 return *tmp;
1177
1178 /* return name without allocation, tmp == NULL */
1179 if (strchr(dev_name(dev), '!') == NULL)
1180 return dev_name(dev);
1181
1182 /* replace '!' in the name with '/' */
1183 *tmp = kstrdup(dev_name(dev), GFP_KERNEL);
1184 if (!*tmp)
1185 return NULL;
1186 while ((s = strchr(*tmp, '!')))
1187 s[0] = '/';
1188 return *tmp;
1189}
1190
1191/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001192 * device_for_each_child - device child iterator.
1193 * @parent: parent struct device.
1194 * @data: data for the callback.
1195 * @fn: function to be called for each device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001197 * Iterate over @parent's child devices, and call @fn for each,
1198 * passing it @data.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001200 * We check the return of @fn each time. If it returns anything
1201 * other than 0, we break out and return that value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001203int device_for_each_child(struct device *parent, void *data,
1204 int (*fn)(struct device *dev, void *data))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205{
mochel@digitalimplant.org36239572005-03-24 19:08:30 -08001206 struct klist_iter i;
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001207 struct device *child;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 int error = 0;
1209
Greg Kroah-Hartman014c90db2009-04-15 16:00:12 -07001210 if (!parent->p)
1211 return 0;
1212
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08001213 klist_iter_init(&parent->p->klist_children, &i);
mochel@digitalimplant.org36239572005-03-24 19:08:30 -08001214 while ((child = next_device(&i)) && !error)
1215 error = fn(child, data);
1216 klist_iter_exit(&i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 return error;
1218}
1219
Cornelia Huck5ab69982006-11-16 15:42:07 +01001220/**
1221 * device_find_child - device iterator for locating a particular device.
1222 * @parent: parent struct device
1223 * @data: Data to pass to match function
1224 * @match: Callback function to check device
1225 *
1226 * This is similar to the device_for_each_child() function above, but it
1227 * returns a reference to a device that is 'found' for later use, as
1228 * determined by the @match callback.
1229 *
1230 * The callback should return 0 if the device doesn't match and non-zero
1231 * if it does. If the callback returns non-zero and a reference to the
1232 * current device can be obtained, this function will return to the caller
1233 * and not iterate over any more devices.
1234 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001235struct device *device_find_child(struct device *parent, void *data,
1236 int (*match)(struct device *dev, void *data))
Cornelia Huck5ab69982006-11-16 15:42:07 +01001237{
1238 struct klist_iter i;
1239 struct device *child;
1240
1241 if (!parent)
1242 return NULL;
1243
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08001244 klist_iter_init(&parent->p->klist_children, &i);
Cornelia Huck5ab69982006-11-16 15:42:07 +01001245 while ((child = next_device(&i)))
1246 if (match(child, data) && get_device(child))
1247 break;
1248 klist_iter_exit(&i);
1249 return child;
1250}
1251
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252int __init devices_init(void)
1253{
Greg Kroah-Hartman881c6cfd2007-11-01 09:29:06 -06001254 devices_kset = kset_create_and_add("devices", &device_uevent_ops, NULL);
1255 if (!devices_kset)
1256 return -ENOMEM;
Dan Williamse105b8b2008-04-21 10:51:07 -07001257 dev_kobj = kobject_create_and_add("dev", NULL);
1258 if (!dev_kobj)
1259 goto dev_kobj_err;
1260 sysfs_dev_block_kobj = kobject_create_and_add("block", dev_kobj);
1261 if (!sysfs_dev_block_kobj)
1262 goto block_kobj_err;
1263 sysfs_dev_char_kobj = kobject_create_and_add("char", dev_kobj);
1264 if (!sysfs_dev_char_kobj)
1265 goto char_kobj_err;
1266
Greg Kroah-Hartman881c6cfd2007-11-01 09:29:06 -06001267 return 0;
Dan Williamse105b8b2008-04-21 10:51:07 -07001268
1269 char_kobj_err:
1270 kobject_put(sysfs_dev_block_kobj);
1271 block_kobj_err:
1272 kobject_put(dev_kobj);
1273 dev_kobj_err:
1274 kset_unregister(devices_kset);
1275 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276}
1277
1278EXPORT_SYMBOL_GPL(device_for_each_child);
Cornelia Huck5ab69982006-11-16 15:42:07 +01001279EXPORT_SYMBOL_GPL(device_find_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280
1281EXPORT_SYMBOL_GPL(device_initialize);
1282EXPORT_SYMBOL_GPL(device_add);
1283EXPORT_SYMBOL_GPL(device_register);
1284
1285EXPORT_SYMBOL_GPL(device_del);
1286EXPORT_SYMBOL_GPL(device_unregister);
1287EXPORT_SYMBOL_GPL(get_device);
1288EXPORT_SYMBOL_GPL(put_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289
1290EXPORT_SYMBOL_GPL(device_create_file);
1291EXPORT_SYMBOL_GPL(device_remove_file);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001292
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00001293struct root_device
1294{
1295 struct device dev;
1296 struct module *owner;
1297};
1298
1299#define to_root_device(dev) container_of(dev, struct root_device, dev)
1300
1301static void root_device_release(struct device *dev)
1302{
1303 kfree(to_root_device(dev));
1304}
1305
1306/**
1307 * __root_device_register - allocate and register a root device
1308 * @name: root device name
1309 * @owner: owner module of the root device, usually THIS_MODULE
1310 *
1311 * This function allocates a root device and registers it
1312 * using device_register(). In order to free the returned
1313 * device, use root_device_unregister().
1314 *
1315 * Root devices are dummy devices which allow other devices
1316 * to be grouped under /sys/devices. Use this function to
1317 * allocate a root device and then use it as the parent of
1318 * any device which should appear under /sys/devices/{name}
1319 *
1320 * The /sys/devices/{name} directory will also contain a
1321 * 'module' symlink which points to the @owner directory
1322 * in sysfs.
1323 *
1324 * Note: You probably want to use root_device_register().
1325 */
1326struct device *__root_device_register(const char *name, struct module *owner)
1327{
1328 struct root_device *root;
1329 int err = -ENOMEM;
1330
1331 root = kzalloc(sizeof(struct root_device), GFP_KERNEL);
1332 if (!root)
1333 return ERR_PTR(err);
1334
Greg Kroah-Hartmanacc0e902009-06-02 15:39:55 -07001335 err = dev_set_name(&root->dev, "%s", name);
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00001336 if (err) {
1337 kfree(root);
1338 return ERR_PTR(err);
1339 }
1340
1341 root->dev.release = root_device_release;
1342
1343 err = device_register(&root->dev);
1344 if (err) {
1345 put_device(&root->dev);
1346 return ERR_PTR(err);
1347 }
1348
1349#ifdef CONFIG_MODULE /* gotta find a "cleaner" way to do this */
1350 if (owner) {
1351 struct module_kobject *mk = &owner->mkobj;
1352
1353 err = sysfs_create_link(&root->dev.kobj, &mk->kobj, "module");
1354 if (err) {
1355 device_unregister(&root->dev);
1356 return ERR_PTR(err);
1357 }
1358 root->owner = owner;
1359 }
1360#endif
1361
1362 return &root->dev;
1363}
1364EXPORT_SYMBOL_GPL(__root_device_register);
1365
1366/**
1367 * root_device_unregister - unregister and free a root device
Randy Dunlap7cbcf222009-01-20 16:29:13 -08001368 * @dev: device going away
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00001369 *
1370 * This function unregisters and cleans up a device that was created by
1371 * root_device_register().
1372 */
1373void root_device_unregister(struct device *dev)
1374{
1375 struct root_device *root = to_root_device(dev);
1376
1377 if (root->owner)
1378 sysfs_remove_link(&root->dev.kobj, "module");
1379
1380 device_unregister(dev);
1381}
1382EXPORT_SYMBOL_GPL(root_device_unregister);
1383
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001384
1385static void device_create_release(struct device *dev)
1386{
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01001387 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001388 kfree(dev);
1389}
1390
1391/**
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07001392 * device_create_vargs - creates a device and registers it with sysfs
1393 * @class: pointer to the struct class that this device should be registered to
1394 * @parent: pointer to the parent struct device of this new device, if any
1395 * @devt: the dev_t for the char device to be added
1396 * @drvdata: the data to be added to the device for callbacks
1397 * @fmt: string for the device's name
1398 * @args: va_list 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 */
1414struct device *device_create_vargs(struct class *class, struct device *parent,
1415 dev_t devt, void *drvdata, const char *fmt,
1416 va_list args)
1417{
1418 struct device *dev = NULL;
1419 int retval = -ENODEV;
1420
1421 if (class == NULL || IS_ERR(class))
1422 goto error;
1423
1424 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1425 if (!dev) {
1426 retval = -ENOMEM;
1427 goto error;
1428 }
1429
1430 dev->devt = devt;
1431 dev->class = class;
1432 dev->parent = parent;
1433 dev->release = device_create_release;
1434 dev_set_drvdata(dev, drvdata);
1435
Kay Sievers1fa5ae82009-01-25 15:17:37 +01001436 retval = kobject_set_name_vargs(&dev->kobj, fmt, args);
1437 if (retval)
1438 goto error;
1439
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07001440 retval = device_register(dev);
1441 if (retval)
1442 goto error;
1443
1444 return dev;
1445
1446error:
Cornelia Huck286661b2008-09-03 18:26:41 +02001447 put_device(dev);
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07001448 return ERR_PTR(retval);
1449}
1450EXPORT_SYMBOL_GPL(device_create_vargs);
1451
1452/**
Greg Kroah-Hartman4e106732008-07-21 20:03:34 -07001453 * device_create - creates a device and registers it with sysfs
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07001454 * @class: pointer to the struct class that this device should be registered to
1455 * @parent: pointer to the parent struct device of this new device, if any
1456 * @devt: the dev_t for the char device to be added
1457 * @drvdata: the data to be added to the device for callbacks
1458 * @fmt: string for the device's name
1459 *
1460 * This function can be used by char device classes. A struct device
1461 * will be created in sysfs, registered to the specified class.
1462 *
1463 * A "dev" file will be created, showing the dev_t for the device, if
1464 * the dev_t is not 0,0.
1465 * If a pointer to a parent struct device is passed in, the newly created
1466 * struct device will be a child of that device in sysfs.
1467 * The pointer to the struct device will be returned from the call.
1468 * Any further sysfs files that might be required can be created using this
1469 * pointer.
1470 *
1471 * Note: the struct class passed to this function must have previously
1472 * been created with a call to class_create().
1473 */
Greg Kroah-Hartman4e106732008-07-21 20:03:34 -07001474struct device *device_create(struct class *class, struct device *parent,
1475 dev_t devt, void *drvdata, const char *fmt, ...)
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07001476{
1477 va_list vargs;
1478 struct device *dev;
1479
1480 va_start(vargs, fmt);
1481 dev = device_create_vargs(class, parent, devt, drvdata, fmt, vargs);
1482 va_end(vargs);
1483 return dev;
1484}
Greg Kroah-Hartman4e106732008-07-21 20:03:34 -07001485EXPORT_SYMBOL_GPL(device_create);
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07001486
Dave Youngcd354492008-01-28 16:56:11 +08001487static int __match_devt(struct device *dev, void *data)
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001488{
Dave Youngcd354492008-01-28 16:56:11 +08001489 dev_t *devt = data;
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001490
Dave Youngcd354492008-01-28 16:56:11 +08001491 return dev->devt == *devt;
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001492}
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001493
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001494/**
1495 * device_destroy - removes a device that was created with device_create()
1496 * @class: pointer to the struct class that this device was registered with
1497 * @devt: the dev_t of the device that was previously registered
1498 *
1499 * This call unregisters and cleans up a device that was created with a
1500 * call to device_create().
1501 */
1502void device_destroy(struct class *class, dev_t devt)
1503{
1504 struct device *dev;
1505
Greg Kroah-Hartman695794a2008-05-22 17:21:08 -04001506 dev = class_find_device(class, NULL, &devt, __match_devt);
Dave Youngcd354492008-01-28 16:56:11 +08001507 if (dev) {
1508 put_device(dev);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001509 device_unregister(dev);
Dave Youngcd354492008-01-28 16:56:11 +08001510 }
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001511}
1512EXPORT_SYMBOL_GPL(device_destroy);
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001513
1514/**
1515 * device_rename - renames a device
1516 * @dev: the pointer to the struct device to be renamed
1517 * @new_name: the new name of the device
Eric W. Biederman030c1d22008-05-08 14:41:00 -07001518 *
1519 * It is the responsibility of the caller to provide mutual
1520 * exclusion between two different calls of device_rename
1521 * on the same device to ensure that new_name is valid and
1522 * won't conflict with other devices.
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001523 */
1524int device_rename(struct device *dev, char *new_name)
1525{
1526 char *old_class_name = NULL;
1527 char *new_class_name = NULL;
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001528 char *old_device_name = NULL;
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001529 int error;
1530
1531 dev = get_device(dev);
1532 if (!dev)
1533 return -EINVAL;
1534
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01001535 pr_debug("device: '%s': %s: renaming to '%s'\n", dev_name(dev),
Harvey Harrison2b3a3022008-03-04 16:41:05 -08001536 __func__, new_name);
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001537
Kay Sievers99ef3ef2006-09-14 11:23:28 +02001538#ifdef CONFIG_SYSFS_DEPRECATED
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001539 if ((dev->class) && (dev->parent))
1540 old_class_name = make_class_name(dev->class->name, &dev->kobj);
Kay Sievers99ef3ef2006-09-14 11:23:28 +02001541#endif
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001542
Kay Sievers1fa5ae82009-01-25 15:17:37 +01001543 old_device_name = kstrdup(dev_name(dev), GFP_KERNEL);
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001544 if (!old_device_name) {
1545 error = -ENOMEM;
1546 goto out;
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001547 }
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001548
1549 error = kobject_rename(&dev->kobj, new_name);
Kay Sievers1fa5ae82009-01-25 15:17:37 +01001550 if (error)
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001551 goto out;
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001552
Kay Sievers99ef3ef2006-09-14 11:23:28 +02001553#ifdef CONFIG_SYSFS_DEPRECATED
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001554 if (old_class_name) {
1555 new_class_name = make_class_name(dev->class->name, &dev->kobj);
1556 if (new_class_name) {
Cornelia Huck36ce6da2008-06-10 11:09:08 +02001557 error = sysfs_create_link_nowarn(&dev->parent->kobj,
1558 &dev->kobj,
1559 new_class_name);
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001560 if (error)
1561 goto out;
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001562 sysfs_remove_link(&dev->parent->kobj, old_class_name);
1563 }
1564 }
Kay Sievers60b8cab2007-10-26 20:07:44 +02001565#else
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001566 if (dev->class) {
Cornelia Huck36ce6da2008-06-10 11:09:08 +02001567 error = sysfs_create_link_nowarn(&dev->class->p->class_subsys.kobj,
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01001568 &dev->kobj, dev_name(dev));
Stephen Hemminger0599ad52008-05-14 22:34:16 -07001569 if (error)
1570 goto out;
Greg Kroah-Hartman1fbfee62008-05-28 09:28:39 -07001571 sysfs_remove_link(&dev->class->p->class_subsys.kobj,
Greg Kroah-Hartman7c714482008-01-22 18:17:41 -05001572 old_device_name);
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001573 }
Kay Sievers60b8cab2007-10-26 20:07:44 +02001574#endif
1575
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001576out:
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001577 put_device(dev);
1578
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001579 kfree(new_class_name);
Jesper Juhl952ab432006-09-28 23:56:01 +02001580 kfree(old_class_name);
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001581 kfree(old_device_name);
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001582
1583 return error;
1584}
Johannes Berga2807db2007-02-28 12:38:31 +01001585EXPORT_SYMBOL_GPL(device_rename);
Cornelia Huck8a824722006-11-20 17:07:51 +01001586
1587static int device_move_class_links(struct device *dev,
1588 struct device *old_parent,
1589 struct device *new_parent)
1590{
Greg Kroah-Hartmanf7f34612007-03-06 12:55:53 -08001591 int error = 0;
Cornelia Huck8a824722006-11-20 17:07:51 +01001592#ifdef CONFIG_SYSFS_DEPRECATED
Cornelia Huck8a824722006-11-20 17:07:51 +01001593 char *class_name;
1594
1595 class_name = make_class_name(dev->class->name, &dev->kobj);
1596 if (!class_name) {
Cornelia Huckcb360bb2006-11-27 10:35:05 +01001597 error = -ENOMEM;
Cornelia Huck8a824722006-11-20 17:07:51 +01001598 goto out;
1599 }
1600 if (old_parent) {
1601 sysfs_remove_link(&dev->kobj, "device");
1602 sysfs_remove_link(&old_parent->kobj, class_name);
1603 }
Cornelia Huckc744aeae2007-01-08 20:16:44 +01001604 if (new_parent) {
1605 error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
1606 "device");
1607 if (error)
1608 goto out;
1609 error = sysfs_create_link(&new_parent->kobj, &dev->kobj,
1610 class_name);
1611 if (error)
1612 sysfs_remove_link(&dev->kobj, "device");
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001613 } else
Cornelia Huckc744aeae2007-01-08 20:16:44 +01001614 error = 0;
Cornelia Huck8a824722006-11-20 17:07:51 +01001615out:
1616 kfree(class_name);
1617 return error;
1618#else
Greg Kroah-Hartmanf7f34612007-03-06 12:55:53 -08001619 if (old_parent)
1620 sysfs_remove_link(&dev->kobj, "device");
1621 if (new_parent)
1622 error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
1623 "device");
1624 return error;
Cornelia Huck8a824722006-11-20 17:07:51 +01001625#endif
1626}
1627
1628/**
1629 * device_move - moves a device to a new parent
1630 * @dev: the pointer to the struct device to be moved
Cornelia Huckc744aeae2007-01-08 20:16:44 +01001631 * @new_parent: the new parent of the device (can by NULL)
Cornelia Huckffa6a702009-03-04 12:44:00 +01001632 * @dpm_order: how to reorder the dpm_list
Cornelia Huck8a824722006-11-20 17:07:51 +01001633 */
Cornelia Huckffa6a702009-03-04 12:44:00 +01001634int device_move(struct device *dev, struct device *new_parent,
1635 enum dpm_order dpm_order)
Cornelia Huck8a824722006-11-20 17:07:51 +01001636{
1637 int error;
1638 struct device *old_parent;
Cornelia Huckc744aeae2007-01-08 20:16:44 +01001639 struct kobject *new_parent_kobj;
Cornelia Huck8a824722006-11-20 17:07:51 +01001640
1641 dev = get_device(dev);
1642 if (!dev)
1643 return -EINVAL;
1644
Cornelia Huckffa6a702009-03-04 12:44:00 +01001645 device_pm_lock();
Cornelia Huck8a824722006-11-20 17:07:51 +01001646 new_parent = get_device(new_parent);
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001647 new_parent_kobj = get_device_parent(dev, new_parent);
Cornelia Huck63b69712008-01-21 16:09:44 +01001648
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01001649 pr_debug("device: '%s': %s: moving to '%s'\n", dev_name(dev),
1650 __func__, new_parent ? dev_name(new_parent) : "<NULL>");
Cornelia Huckc744aeae2007-01-08 20:16:44 +01001651 error = kobject_move(&dev->kobj, new_parent_kobj);
Cornelia Huck8a824722006-11-20 17:07:51 +01001652 if (error) {
Cornelia Huck63b69712008-01-21 16:09:44 +01001653 cleanup_glue_dir(dev, new_parent_kobj);
Cornelia Huck8a824722006-11-20 17:07:51 +01001654 put_device(new_parent);
1655 goto out;
1656 }
1657 old_parent = dev->parent;
1658 dev->parent = new_parent;
1659 if (old_parent)
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08001660 klist_remove(&dev->p->knode_parent);
Yinghai Lu0d358f22008-02-19 03:20:41 -08001661 if (new_parent) {
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08001662 klist_add_tail(&dev->p->knode_parent,
1663 &new_parent->p->klist_children);
Yinghai Lu0d358f22008-02-19 03:20:41 -08001664 set_dev_node(dev, dev_to_node(new_parent));
1665 }
1666
Cornelia Huck8a824722006-11-20 17:07:51 +01001667 if (!dev->class)
1668 goto out_put;
1669 error = device_move_class_links(dev, old_parent, new_parent);
1670 if (error) {
1671 /* We ignore errors on cleanup since we're hosed anyway... */
1672 device_move_class_links(dev, new_parent, old_parent);
1673 if (!kobject_move(&dev->kobj, &old_parent->kobj)) {
Cornelia Huckc744aeae2007-01-08 20:16:44 +01001674 if (new_parent)
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08001675 klist_remove(&dev->p->knode_parent);
Yinghai Lu0d358f22008-02-19 03:20:41 -08001676 dev->parent = old_parent;
1677 if (old_parent) {
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08001678 klist_add_tail(&dev->p->knode_parent,
1679 &old_parent->p->klist_children);
Yinghai Lu0d358f22008-02-19 03:20:41 -08001680 set_dev_node(dev, dev_to_node(old_parent));
1681 }
Cornelia Huck8a824722006-11-20 17:07:51 +01001682 }
Cornelia Huck63b69712008-01-21 16:09:44 +01001683 cleanup_glue_dir(dev, new_parent_kobj);
Cornelia Huck8a824722006-11-20 17:07:51 +01001684 put_device(new_parent);
1685 goto out;
1686 }
Cornelia Huckffa6a702009-03-04 12:44:00 +01001687 switch (dpm_order) {
1688 case DPM_ORDER_NONE:
1689 break;
1690 case DPM_ORDER_DEV_AFTER_PARENT:
1691 device_pm_move_after(dev, new_parent);
1692 break;
1693 case DPM_ORDER_PARENT_BEFORE_DEV:
1694 device_pm_move_before(new_parent, dev);
1695 break;
1696 case DPM_ORDER_DEV_LAST:
1697 device_pm_move_last(dev);
1698 break;
1699 }
Cornelia Huck8a824722006-11-20 17:07:51 +01001700out_put:
1701 put_device(old_parent);
1702out:
Cornelia Huckffa6a702009-03-04 12:44:00 +01001703 device_pm_unlock();
Cornelia Huck8a824722006-11-20 17:07:51 +01001704 put_device(dev);
1705 return error;
1706}
Cornelia Huck8a824722006-11-20 17:07:51 +01001707EXPORT_SYMBOL_GPL(device_move);
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08001708
1709/**
1710 * device_shutdown - call ->shutdown() on each device to shutdown.
1711 */
1712void device_shutdown(void)
1713{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001714 struct device *dev, *devn;
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08001715
1716 list_for_each_entry_safe_reverse(dev, devn, &devices_kset->list,
1717 kobj.entry) {
1718 if (dev->bus && dev->bus->shutdown) {
1719 dev_dbg(dev, "shutdown\n");
1720 dev->bus->shutdown(dev);
1721 } else if (dev->driver && dev->driver->shutdown) {
1722 dev_dbg(dev, "shutdown\n");
1723 dev->driver->shutdown(dev);
1724 }
1725 }
Dan Williamse105b8b2008-04-21 10:51:07 -07001726 kobject_put(sysfs_dev_char_kobj);
1727 kobject_put(sysfs_dev_block_kobj);
1728 kobject_put(dev_kobj);
Shaohua Li401097e2009-05-12 13:37:57 -07001729 async_synchronize_full();
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08001730}