blob: 2fd9e611f8a61e19b2b06ed90418d591e64a303c [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{
Alan Stern35899722009-12-04 11:06:57 -050059 struct device_driver *drv;
60
61 /* dev->driver can change to NULL underneath us because of unbinding,
62 * so be careful about accessing it. dev->bus and dev->class should
63 * never change once they are set, so they don't need special care.
64 */
65 drv = ACCESS_ONCE(dev->driver);
66 return drv ? drv->name :
Jean Delvarea456b702007-03-09 16:33:10 +010067 (dev->bus ? dev->bus->name :
68 (dev->class ? dev->class->name : ""));
Alan Stern3e956372006-06-16 17:10:48 -040069}
Matthew Wilcox310a9222006-09-23 23:35:04 -060070EXPORT_SYMBOL(dev_driver_string);
Alan Stern3e956372006-06-16 17:10:48 -040071
Linus Torvalds1da177e2005-04-16 15:20:36 -070072#define to_dev(obj) container_of(obj, struct device, kobj)
73#define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr)
74
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -080075static ssize_t dev_attr_show(struct kobject *kobj, struct attribute *attr,
76 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070077{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -080078 struct device_attribute *dev_attr = to_dev_attr(attr);
79 struct device *dev = to_dev(kobj);
Dmitry Torokhov4a0c20b2005-04-29 01:23:47 -050080 ssize_t ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
82 if (dev_attr->show)
Yani Ioannou54b6f352005-05-17 06:39:34 -040083 ret = dev_attr->show(dev, dev_attr, buf);
Andrew Morton815d2d52008-03-04 15:09:07 -080084 if (ret >= (ssize_t)PAGE_SIZE) {
85 print_symbol("dev_attr_show: %s returned bad count\n",
86 (unsigned long)dev_attr->show);
87 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 return ret;
89}
90
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -080091static ssize_t dev_attr_store(struct kobject *kobj, struct attribute *attr,
92 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -070093{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -080094 struct device_attribute *dev_attr = to_dev_attr(attr);
95 struct device *dev = to_dev(kobj);
Dmitry Torokhov4a0c20b2005-04-29 01:23:47 -050096 ssize_t ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
98 if (dev_attr->store)
Yani Ioannou54b6f352005-05-17 06:39:34 -040099 ret = dev_attr->store(dev, dev_attr, buf, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 return ret;
101}
102
103static struct sysfs_ops dev_sysfs_ops = {
104 .show = dev_attr_show,
105 .store = dev_attr_store,
106};
107
108
109/**
110 * device_release - free device structure.
111 * @kobj: device's kobject.
112 *
113 * This is called once the reference count for the object
114 * reaches 0. We forward the call to the device's release
115 * method, which should handle actually freeing the structure.
116 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800117static void device_release(struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800119 struct device *dev = to_dev(kobj);
Greg Kroah-Hartmanfb069a52008-12-16 12:23:36 -0800120 struct device_private *p = dev->p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
122 if (dev->release)
123 dev->release(dev);
Kay Sieversf9f852d2006-10-07 21:54:55 +0200124 else if (dev->type && dev->type->release)
125 dev->type->release(dev);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700126 else if (dev->class && dev->class->dev_release)
127 dev->class->dev_release(dev);
Arjan van de Venf810a5c2008-07-25 19:45:39 -0700128 else
129 WARN(1, KERN_ERR "Device '%s' does not have a release() "
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800130 "function, it is broken and must be fixed.\n",
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100131 dev_name(dev));
Greg Kroah-Hartmanfb069a52008-12-16 12:23:36 -0800132 kfree(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133}
134
Greg Kroah-Hartman8f4afc42007-10-11 10:47:49 -0600135static struct kobj_type device_ktype = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 .release = device_release,
137 .sysfs_ops = &dev_sysfs_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138};
139
140
Kay Sievers312c0042005-11-16 09:00:00 +0100141static int dev_uevent_filter(struct kset *kset, struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142{
143 struct kobj_type *ktype = get_ktype(kobj);
144
Greg Kroah-Hartman8f4afc42007-10-11 10:47:49 -0600145 if (ktype == &device_ktype) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 struct device *dev = to_dev(kobj);
147 if (dev->bus)
148 return 1;
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700149 if (dev->class)
150 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 }
152 return 0;
153}
154
Kay Sievers312c0042005-11-16 09:00:00 +0100155static const char *dev_uevent_name(struct kset *kset, struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156{
157 struct device *dev = to_dev(kobj);
158
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700159 if (dev->bus)
160 return dev->bus->name;
161 if (dev->class)
162 return dev->class->name;
163 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164}
165
Kay Sievers7eff2e72007-08-14 15:15:12 +0200166static int dev_uevent(struct kset *kset, struct kobject *kobj,
167 struct kobj_uevent_env *env)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168{
169 struct device *dev = to_dev(kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 int retval = 0;
171
Kay Sievers6fcf53a2009-04-30 15:23:42 +0200172 /* add device node properties if present */
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700173 if (MAJOR(dev->devt)) {
Kay Sievers6fcf53a2009-04-30 15:23:42 +0200174 const char *tmp;
175 const char *name;
Kay Sieverse454cea2009-09-18 23:01:12 +0200176 mode_t mode = 0;
Kay Sievers6fcf53a2009-04-30 15:23:42 +0200177
Kay Sievers7eff2e72007-08-14 15:15:12 +0200178 add_uevent_var(env, "MAJOR=%u", MAJOR(dev->devt));
179 add_uevent_var(env, "MINOR=%u", MINOR(dev->devt));
Kay Sieverse454cea2009-09-18 23:01:12 +0200180 name = device_get_devnode(dev, &mode, &tmp);
Kay Sievers6fcf53a2009-04-30 15:23:42 +0200181 if (name) {
182 add_uevent_var(env, "DEVNAME=%s", name);
183 kfree(tmp);
Kay Sieverse454cea2009-09-18 23:01:12 +0200184 if (mode)
185 add_uevent_var(env, "DEVMODE=%#o", mode & 0777);
Kay Sievers6fcf53a2009-04-30 15:23:42 +0200186 }
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700187 }
188
Kay Sievers414264f2007-03-12 21:08:57 +0100189 if (dev->type && dev->type->name)
Kay Sievers7eff2e72007-08-14 15:15:12 +0200190 add_uevent_var(env, "DEVTYPE=%s", dev->type->name);
Kay Sievers414264f2007-03-12 21:08:57 +0100191
Kay Sievers239378f2006-10-07 21:54:55 +0200192 if (dev->driver)
Kay Sievers7eff2e72007-08-14 15:15:12 +0200193 add_uevent_var(env, "DRIVER=%s", dev->driver->name);
Kay Sievers239378f2006-10-07 21:54:55 +0200194
Kay Sieversa87cb2a2006-09-14 11:23:28 +0200195#ifdef CONFIG_SYSFS_DEPRECATED
Kay Sievers239378f2006-10-07 21:54:55 +0200196 if (dev->class) {
197 struct device *parent = dev->parent;
198
199 /* find first bus device in parent chain */
200 while (parent && !parent->bus)
201 parent = parent->parent;
202 if (parent && parent->bus) {
203 const char *path;
204
205 path = kobject_get_path(&parent->kobj, GFP_KERNEL);
Kay Sievers2c7afd12007-05-01 13:46:26 +0200206 if (path) {
Kay Sievers7eff2e72007-08-14 15:15:12 +0200207 add_uevent_var(env, "PHYSDEVPATH=%s", path);
Kay Sievers2c7afd12007-05-01 13:46:26 +0200208 kfree(path);
209 }
Kay Sievers239378f2006-10-07 21:54:55 +0200210
Kay Sievers7eff2e72007-08-14 15:15:12 +0200211 add_uevent_var(env, "PHYSDEVBUS=%s", parent->bus->name);
Kay Sievers239378f2006-10-07 21:54:55 +0200212
213 if (parent->driver)
Kay Sievers7eff2e72007-08-14 15:15:12 +0200214 add_uevent_var(env, "PHYSDEVDRIVER=%s",
215 parent->driver->name);
Kay Sievers239378f2006-10-07 21:54:55 +0200216 }
217 } else if (dev->bus) {
Kay Sievers7eff2e72007-08-14 15:15:12 +0200218 add_uevent_var(env, "PHYSDEVBUS=%s", dev->bus->name);
Kay Sievers239378f2006-10-07 21:54:55 +0200219
220 if (dev->driver)
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800221 add_uevent_var(env, "PHYSDEVDRIVER=%s",
222 dev->driver->name);
Kay Sieversd81d9d62006-08-13 06:17:09 +0200223 }
Kay Sievers239378f2006-10-07 21:54:55 +0200224#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
Kay Sievers7eff2e72007-08-14 15:15:12 +0200226 /* have the bus specific function add its stuff */
Kay Sievers312c0042005-11-16 09:00:00 +0100227 if (dev->bus && dev->bus->uevent) {
Kay Sievers7eff2e72007-08-14 15:15:12 +0200228 retval = dev->bus->uevent(dev, env);
Kay Sieversf9f852d2006-10-07 21:54:55 +0200229 if (retval)
Greg Kroah-Hartman7dc72b22007-11-28 23:49:41 -0800230 pr_debug("device: '%s': %s: bus uevent() returned %d\n",
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100231 dev_name(dev), __func__, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 }
233
Kay Sievers7eff2e72007-08-14 15:15:12 +0200234 /* have the class specific function add its stuff */
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700235 if (dev->class && dev->class->dev_uevent) {
Kay Sievers7eff2e72007-08-14 15:15:12 +0200236 retval = dev->class->dev_uevent(dev, env);
Kay Sieversf9f852d2006-10-07 21:54:55 +0200237 if (retval)
Greg Kroah-Hartman7dc72b22007-11-28 23:49:41 -0800238 pr_debug("device: '%s': %s: class uevent() "
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100239 "returned %d\n", dev_name(dev),
Harvey Harrison2b3a3022008-03-04 16:41:05 -0800240 __func__, retval);
Kay Sieversf9f852d2006-10-07 21:54:55 +0200241 }
242
Kay Sievers7eff2e72007-08-14 15:15:12 +0200243 /* have the device type specific fuction add its stuff */
Kay Sieversf9f852d2006-10-07 21:54:55 +0200244 if (dev->type && dev->type->uevent) {
Kay Sievers7eff2e72007-08-14 15:15:12 +0200245 retval = dev->type->uevent(dev, env);
Kay Sieversf9f852d2006-10-07 21:54:55 +0200246 if (retval)
Greg Kroah-Hartman7dc72b22007-11-28 23:49:41 -0800247 pr_debug("device: '%s': %s: dev_type uevent() "
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100248 "returned %d\n", dev_name(dev),
Harvey Harrison2b3a3022008-03-04 16:41:05 -0800249 __func__, retval);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700250 }
251
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 return retval;
253}
254
Kay Sievers312c0042005-11-16 09:00:00 +0100255static struct kset_uevent_ops device_uevent_ops = {
256 .filter = dev_uevent_filter,
257 .name = dev_uevent_name,
258 .uevent = dev_uevent,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259};
260
Kay Sievers16574dc2007-04-06 01:40:38 +0200261static ssize_t show_uevent(struct device *dev, struct device_attribute *attr,
262 char *buf)
263{
264 struct kobject *top_kobj;
265 struct kset *kset;
Kay Sievers7eff2e72007-08-14 15:15:12 +0200266 struct kobj_uevent_env *env = NULL;
Kay Sievers16574dc2007-04-06 01:40:38 +0200267 int i;
268 size_t count = 0;
269 int retval;
270
271 /* search the kset, the device belongs to */
272 top_kobj = &dev->kobj;
Kay Sievers5c5daf62007-08-12 20:43:55 +0200273 while (!top_kobj->kset && top_kobj->parent)
274 top_kobj = top_kobj->parent;
Kay Sievers16574dc2007-04-06 01:40:38 +0200275 if (!top_kobj->kset)
276 goto out;
Kay Sievers5c5daf62007-08-12 20:43:55 +0200277
Kay Sievers16574dc2007-04-06 01:40:38 +0200278 kset = top_kobj->kset;
279 if (!kset->uevent_ops || !kset->uevent_ops->uevent)
280 goto out;
281
282 /* respect filter */
283 if (kset->uevent_ops && kset->uevent_ops->filter)
284 if (!kset->uevent_ops->filter(kset, &dev->kobj))
285 goto out;
286
Kay Sievers7eff2e72007-08-14 15:15:12 +0200287 env = kzalloc(sizeof(struct kobj_uevent_env), GFP_KERNEL);
288 if (!env)
Greg Kroah-Hartmanc7308c82007-05-02 14:14:11 +0200289 return -ENOMEM;
290
Kay Sievers16574dc2007-04-06 01:40:38 +0200291 /* let the kset specific function add its keys */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200292 retval = kset->uevent_ops->uevent(kset, &dev->kobj, env);
Kay Sievers16574dc2007-04-06 01:40:38 +0200293 if (retval)
294 goto out;
295
296 /* copy keys to file */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200297 for (i = 0; i < env->envp_idx; i++)
298 count += sprintf(&buf[count], "%s\n", env->envp[i]);
Kay Sievers16574dc2007-04-06 01:40:38 +0200299out:
Kay Sievers7eff2e72007-08-14 15:15:12 +0200300 kfree(env);
Kay Sievers16574dc2007-04-06 01:40:38 +0200301 return count;
302}
303
Kay Sieversa7fd6702005-10-01 14:49:43 +0200304static ssize_t store_uevent(struct device *dev, struct device_attribute *attr,
305 const char *buf, size_t count)
306{
Kay Sievers60a96a52007-07-08 22:29:26 +0200307 enum kobject_action action;
308
Kay Sievers5c5daf62007-08-12 20:43:55 +0200309 if (kobject_action_type(buf, count, &action) == 0) {
Kay Sievers60a96a52007-07-08 22:29:26 +0200310 kobject_uevent(&dev->kobj, action);
311 goto out;
312 }
313
314 dev_err(dev, "uevent: unsupported action-string; this will "
315 "be ignored in a future kernel version\n");
Kay Sievers312c0042005-11-16 09:00:00 +0100316 kobject_uevent(&dev->kobj, KOBJ_ADD);
Kay Sievers60a96a52007-07-08 22:29:26 +0200317out:
Kay Sieversa7fd6702005-10-01 14:49:43 +0200318 return count;
319}
320
Tejun Heoad6a1e12007-06-14 03:45:17 +0900321static struct device_attribute uevent_attr =
322 __ATTR(uevent, S_IRUGO | S_IWUSR, show_uevent, store_uevent);
323
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500324static int device_add_attributes(struct device *dev,
325 struct device_attribute *attrs)
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -0700326{
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -0700327 int error = 0;
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500328 int i;
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -0700329
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500330 if (attrs) {
331 for (i = 0; attr_name(attrs[i]); i++) {
332 error = device_create_file(dev, &attrs[i]);
333 if (error)
334 break;
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -0700335 }
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500336 if (error)
337 while (--i >= 0)
338 device_remove_file(dev, &attrs[i]);
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -0700339 }
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -0700340 return error;
341}
342
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500343static void device_remove_attributes(struct device *dev,
344 struct device_attribute *attrs)
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -0700345{
346 int i;
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500347
348 if (attrs)
349 for (i = 0; attr_name(attrs[i]); i++)
350 device_remove_file(dev, &attrs[i]);
351}
352
353static int device_add_groups(struct device *dev,
David Brownella4dbd672009-06-24 10:06:31 -0700354 const struct attribute_group **groups)
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500355{
356 int error = 0;
357 int i;
358
359 if (groups) {
360 for (i = 0; groups[i]; i++) {
361 error = sysfs_create_group(&dev->kobj, groups[i]);
362 if (error) {
363 while (--i >= 0)
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800364 sysfs_remove_group(&dev->kobj,
365 groups[i]);
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500366 break;
367 }
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -0700368 }
369 }
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500370 return error;
371}
372
373static void device_remove_groups(struct device *dev,
David Brownella4dbd672009-06-24 10:06:31 -0700374 const struct attribute_group **groups)
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500375{
376 int i;
377
378 if (groups)
379 for (i = 0; groups[i]; i++)
380 sysfs_remove_group(&dev->kobj, groups[i]);
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -0700381}
382
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700383static int device_add_attrs(struct device *dev)
384{
385 struct class *class = dev->class;
Kay Sieversf9f852d2006-10-07 21:54:55 +0200386 struct device_type *type = dev->type;
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500387 int error;
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700388
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500389 if (class) {
390 error = device_add_attributes(dev, class->dev_attrs);
Kay Sieversf9f852d2006-10-07 21:54:55 +0200391 if (error)
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500392 return error;
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700393 }
Kay Sieversf9f852d2006-10-07 21:54:55 +0200394
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500395 if (type) {
396 error = device_add_groups(dev, type->groups);
Kay Sieversf9f852d2006-10-07 21:54:55 +0200397 if (error)
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500398 goto err_remove_class_attrs;
Kay Sieversf9f852d2006-10-07 21:54:55 +0200399 }
400
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500401 error = device_add_groups(dev, dev->groups);
402 if (error)
403 goto err_remove_type_groups;
404
405 return 0;
406
407 err_remove_type_groups:
408 if (type)
409 device_remove_groups(dev, type->groups);
410 err_remove_class_attrs:
411 if (class)
412 device_remove_attributes(dev, class->dev_attrs);
413
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700414 return error;
415}
416
417static void device_remove_attrs(struct device *dev)
418{
419 struct class *class = dev->class;
Kay Sieversf9f852d2006-10-07 21:54:55 +0200420 struct device_type *type = dev->type;
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700421
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500422 device_remove_groups(dev, dev->groups);
Kay Sieversf9f852d2006-10-07 21:54:55 +0200423
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500424 if (type)
425 device_remove_groups(dev, type->groups);
426
427 if (class)
428 device_remove_attributes(dev, class->dev_attrs);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700429}
430
431
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700432static ssize_t show_dev(struct device *dev, struct device_attribute *attr,
433 char *buf)
434{
435 return print_dev_t(buf, dev->devt);
436}
437
Tejun Heoad6a1e12007-06-14 03:45:17 +0900438static struct device_attribute devt_attr =
439 __ATTR(dev, S_IRUGO, show_dev, NULL);
440
Greg Kroah-Hartman881c6cf2007-11-01 09:29:06 -0600441/* kset to create /sys/devices/ */
442struct kset *devices_kset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800445 * device_create_file - create sysfs attribute file for device.
446 * @dev: device.
447 * @attr: device attribute descriptor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 */
Phil Carmody26579ab2009-12-18 15:34:19 +0200449int device_create_file(struct device *dev,
450 const struct device_attribute *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451{
452 int error = 0;
Cornelia Huck0c98b192008-01-31 10:39:38 +0100453 if (dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 error = sysfs_create_file(&dev->kobj, &attr->attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 return error;
456}
457
458/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800459 * device_remove_file - remove sysfs attribute file.
460 * @dev: device.
461 * @attr: device attribute descriptor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 */
Phil Carmody26579ab2009-12-18 15:34:19 +0200463void device_remove_file(struct device *dev,
464 const struct device_attribute *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465{
Cornelia Huck0c98b192008-01-31 10:39:38 +0100466 if (dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 sysfs_remove_file(&dev->kobj, &attr->attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468}
469
Greg Kroah-Hartman2589f1882006-09-19 09:39:19 -0700470/**
471 * device_create_bin_file - create sysfs binary attribute file for device.
472 * @dev: device.
473 * @attr: device binary attribute descriptor.
474 */
475int device_create_bin_file(struct device *dev, struct bin_attribute *attr)
476{
477 int error = -EINVAL;
478 if (dev)
479 error = sysfs_create_bin_file(&dev->kobj, attr);
480 return error;
481}
482EXPORT_SYMBOL_GPL(device_create_bin_file);
483
484/**
485 * device_remove_bin_file - remove sysfs binary attribute file
486 * @dev: device.
487 * @attr: device binary attribute descriptor.
488 */
489void device_remove_bin_file(struct device *dev, struct bin_attribute *attr)
490{
491 if (dev)
492 sysfs_remove_bin_file(&dev->kobj, attr);
493}
494EXPORT_SYMBOL_GPL(device_remove_bin_file);
495
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400496/**
Alan Stern523ded72007-04-26 00:12:04 -0700497 * device_schedule_callback_owner - helper to schedule a callback for a device
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400498 * @dev: device.
499 * @func: callback function to invoke later.
Alan Stern523ded72007-04-26 00:12:04 -0700500 * @owner: module owning the callback routine
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400501 *
502 * Attribute methods must not unregister themselves or their parent device
503 * (which would amount to the same thing). Attempts to do so will deadlock,
504 * since unregistration is mutually exclusive with driver callbacks.
505 *
506 * Instead methods can call this routine, which will attempt to allocate
507 * and schedule a workqueue request to call back @func with @dev as its
508 * argument in the workqueue's process context. @dev will be pinned until
509 * @func returns.
510 *
Alan Stern523ded72007-04-26 00:12:04 -0700511 * This routine is usually called via the inline device_schedule_callback(),
512 * which automatically sets @owner to THIS_MODULE.
513 *
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400514 * Returns 0 if the request was submitted, -ENOMEM if storage could not
Alan Stern523ded72007-04-26 00:12:04 -0700515 * be allocated, -ENODEV if a reference to @owner isn't available.
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400516 *
517 * NOTE: This routine won't work if CONFIG_SYSFS isn't set! It uses an
518 * underlying sysfs routine (since it is intended for use by attribute
519 * methods), and if sysfs isn't available you'll get nothing but -ENOSYS.
520 */
Alan Stern523ded72007-04-26 00:12:04 -0700521int device_schedule_callback_owner(struct device *dev,
522 void (*func)(struct device *), struct module *owner)
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400523{
524 return sysfs_schedule_callback(&dev->kobj,
Alan Stern523ded72007-04-26 00:12:04 -0700525 (void (*)(void *)) func, dev, owner);
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400526}
Alan Stern523ded72007-04-26 00:12:04 -0700527EXPORT_SYMBOL_GPL(device_schedule_callback_owner);
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400528
James Bottomley34bb61f2005-09-06 16:56:51 -0700529static void klist_children_get(struct klist_node *n)
530{
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -0800531 struct device_private *p = to_device_private_parent(n);
532 struct device *dev = p->device;
James Bottomley34bb61f2005-09-06 16:56:51 -0700533
534 get_device(dev);
535}
536
537static void klist_children_put(struct klist_node *n)
538{
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -0800539 struct device_private *p = to_device_private_parent(n);
540 struct device *dev = p->device;
James Bottomley34bb61f2005-09-06 16:56:51 -0700541
542 put_device(dev);
543}
544
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800546 * device_initialize - init device structure.
547 * @dev: device.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 *
Cornelia Huck57394112008-09-03 18:26:40 +0200549 * This prepares the device for use by other layers by initializing
550 * its fields.
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800551 * It is the first half of device_register(), if called by
Cornelia Huck57394112008-09-03 18:26:40 +0200552 * that function, though it can also be called separately, so one
553 * may use @dev's fields. In particular, get_device()/put_device()
554 * may be used for reference counting of @dev after calling this
555 * function.
556 *
557 * NOTE: Use put_device() to give up your reference instead of freeing
558 * @dev directly once you have called this function.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560void device_initialize(struct device *dev)
561{
Greg Kroah-Hartman881c6cf2007-11-01 09:29:06 -0600562 dev->kobj.kset = devices_kset;
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -0700563 kobject_init(&dev->kobj, &device_ktype);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 INIT_LIST_HEAD(&dev->dma_pools);
mochel@digitalimplant.orgaf703162005-03-21 10:41:04 -0800565 init_MUTEX(&dev->sem);
Tejun Heo9ac78492007-01-20 16:00:26 +0900566 spin_lock_init(&dev->devres_lock);
567 INIT_LIST_HEAD(&dev->devres_head);
David Brownell0ac85242005-09-12 19:39:34 -0700568 device_init_wakeup(dev, 0);
Alan Stern3b98aea2008-08-07 13:06:12 -0400569 device_pm_init(dev);
Christoph Hellwig87348132006-12-06 20:32:33 -0800570 set_dev_node(dev, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571}
572
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +0100573#ifdef CONFIG_SYSFS_DEPRECATED
Kay Sieversda231fd2007-11-21 17:29:15 +0100574static struct kobject *get_device_parent(struct device *dev,
575 struct device *parent)
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +0100576{
Kay Sieversda231fd2007-11-21 17:29:15 +0100577 /* class devices without a parent live in /sys/class/<classname>/ */
Dmitry Torokhov3eb215d2007-10-07 12:22:21 -0400578 if (dev->class && (!parent || parent->class != dev->class))
Greg Kroah-Hartman1fbfee62008-05-28 09:28:39 -0700579 return &dev->class->p->class_subsys.kobj;
Kay Sieversda231fd2007-11-21 17:29:15 +0100580 /* all other devices keep their parent */
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +0100581 else if (parent)
Cornelia Huckc744aea2007-01-08 20:16:44 +0100582 return &parent->kobj;
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +0100583
Cornelia Huckc744aea2007-01-08 20:16:44 +0100584 return NULL;
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +0100585}
Kay Sieversda231fd2007-11-21 17:29:15 +0100586
587static inline void cleanup_device_parent(struct device *dev) {}
Cornelia Huck63b69712008-01-21 16:09:44 +0100588static inline void cleanup_glue_dir(struct device *dev,
589 struct kobject *glue_dir) {}
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +0100590#else
Kay Sievers86406242007-03-14 03:25:56 +0100591static struct kobject *virtual_device_parent(struct device *dev)
Greg Kroah-Hartmanf0ee61a2006-10-23 10:40:54 -0700592{
Kay Sievers86406242007-03-14 03:25:56 +0100593 static struct kobject *virtual_dir = NULL;
Greg Kroah-Hartmanf0ee61a2006-10-23 10:40:54 -0700594
Kay Sievers86406242007-03-14 03:25:56 +0100595 if (!virtual_dir)
Greg Kroah-Hartman4ff6abf2007-11-05 22:24:43 -0800596 virtual_dir = kobject_create_and_add("virtual",
Greg Kroah-Hartman881c6cf2007-11-01 09:29:06 -0600597 &devices_kset->kobj);
Greg Kroah-Hartmanf0ee61a2006-10-23 10:40:54 -0700598
Kay Sievers86406242007-03-14 03:25:56 +0100599 return virtual_dir;
Greg Kroah-Hartmanf0ee61a2006-10-23 10:40:54 -0700600}
601
Kay Sieversda231fd2007-11-21 17:29:15 +0100602static struct kobject *get_device_parent(struct device *dev,
603 struct device *parent)
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +0100604{
Greg Kroah-Hartman43968d22007-11-05 22:24:43 -0800605 int retval;
606
Kay Sievers86406242007-03-14 03:25:56 +0100607 if (dev->class) {
608 struct kobject *kobj = NULL;
609 struct kobject *parent_kobj;
610 struct kobject *k;
611
612 /*
613 * If we have no parent, we live in "virtual".
Kay Sievers0f4dafc2007-12-19 01:40:42 +0100614 * Class-devices with a non class-device as parent, live
615 * in a "glue" directory to prevent namespace collisions.
Kay Sievers86406242007-03-14 03:25:56 +0100616 */
617 if (parent == NULL)
618 parent_kobj = virtual_device_parent(dev);
619 else if (parent->class)
620 return &parent->kobj;
621 else
622 parent_kobj = &parent->kobj;
623
624 /* find our class-directory at the parent and reference it */
Greg Kroah-Hartman7c714482008-01-22 18:17:41 -0500625 spin_lock(&dev->class->p->class_dirs.list_lock);
626 list_for_each_entry(k, &dev->class->p->class_dirs.list, entry)
Kay Sievers86406242007-03-14 03:25:56 +0100627 if (k->parent == parent_kobj) {
628 kobj = kobject_get(k);
629 break;
630 }
Greg Kroah-Hartman7c714482008-01-22 18:17:41 -0500631 spin_unlock(&dev->class->p->class_dirs.list_lock);
Kay Sievers86406242007-03-14 03:25:56 +0100632 if (kobj)
633 return kobj;
634
635 /* or create a new class-directory at the parent device */
Greg Kroah-Hartman43968d22007-11-05 22:24:43 -0800636 k = kobject_create();
637 if (!k)
638 return NULL;
Greg Kroah-Hartman7c714482008-01-22 18:17:41 -0500639 k->kset = &dev->class->p->class_dirs;
Greg Kroah-Hartmanb2d6db52007-12-17 23:05:35 -0700640 retval = kobject_add(k, parent_kobj, "%s", dev->class->name);
Greg Kroah-Hartman43968d22007-11-05 22:24:43 -0800641 if (retval < 0) {
642 kobject_put(k);
643 return NULL;
644 }
Kay Sievers0f4dafc2007-12-19 01:40:42 +0100645 /* do not emit an uevent for this simple "glue" directory */
Greg Kroah-Hartman43968d22007-11-05 22:24:43 -0800646 return k;
Kay Sievers86406242007-03-14 03:25:56 +0100647 }
648
649 if (parent)
Cornelia Huckc744aea2007-01-08 20:16:44 +0100650 return &parent->kobj;
651 return NULL;
652}
Kay Sieversda231fd2007-11-21 17:29:15 +0100653
Cornelia Huck63b69712008-01-21 16:09:44 +0100654static void cleanup_glue_dir(struct device *dev, struct kobject *glue_dir)
Kay Sieversda231fd2007-11-21 17:29:15 +0100655{
Kay Sievers0f4dafc2007-12-19 01:40:42 +0100656 /* see if we live in a "glue" directory */
Cornelia Huckc1fe5392008-02-27 15:38:23 +0100657 if (!glue_dir || !dev->class ||
Greg Kroah-Hartman7c714482008-01-22 18:17:41 -0500658 glue_dir->kset != &dev->class->p->class_dirs)
Kay Sieversda231fd2007-11-21 17:29:15 +0100659 return;
660
Kay Sievers0f4dafc2007-12-19 01:40:42 +0100661 kobject_put(glue_dir);
Kay Sieversda231fd2007-11-21 17:29:15 +0100662}
Cornelia Huck63b69712008-01-21 16:09:44 +0100663
664static void cleanup_device_parent(struct device *dev)
665{
666 cleanup_glue_dir(dev, dev->kobj.parent);
667}
Cornelia Huckc744aea2007-01-08 20:16:44 +0100668#endif
Kay Sievers86406242007-03-14 03:25:56 +0100669
Cornelia Huck63b69712008-01-21 16:09:44 +0100670static void setup_parent(struct device *dev, struct device *parent)
Cornelia Huckc744aea2007-01-08 20:16:44 +0100671{
672 struct kobject *kobj;
673 kobj = get_device_parent(dev, parent);
Cornelia Huckc744aea2007-01-08 20:16:44 +0100674 if (kobj)
675 dev->kobj.parent = kobj;
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +0100676}
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +0100677
Cornelia Huck2ee97caf2007-07-18 01:43:47 -0700678static int device_add_class_symlinks(struct device *dev)
679{
680 int error;
681
682 if (!dev->class)
683 return 0;
Kay Sieversda231fd2007-11-21 17:29:15 +0100684
Greg Kroah-Hartman1fbfee62008-05-28 09:28:39 -0700685 error = sysfs_create_link(&dev->kobj,
686 &dev->class->p->class_subsys.kobj,
Cornelia Huck2ee97caf2007-07-18 01:43:47 -0700687 "subsystem");
688 if (error)
689 goto out;
Kay Sieversda231fd2007-11-21 17:29:15 +0100690
691#ifdef CONFIG_SYSFS_DEPRECATED
692 /* stacked class devices need a symlink in the class directory */
Greg Kroah-Hartman1fbfee62008-05-28 09:28:39 -0700693 if (dev->kobj.parent != &dev->class->p->class_subsys.kobj &&
Greg Kroah-Hartman4e886c22008-01-27 12:12:43 -0800694 device_is_not_partition(dev)) {
Greg Kroah-Hartman1fbfee62008-05-28 09:28:39 -0700695 error = sysfs_create_link(&dev->class->p->class_subsys.kobj,
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100696 &dev->kobj, dev_name(dev));
Cornelia Huck2ee97caf2007-07-18 01:43:47 -0700697 if (error)
698 goto out_subsys;
699 }
Kay Sieversda231fd2007-11-21 17:29:15 +0100700
Greg Kroah-Hartman4e886c22008-01-27 12:12:43 -0800701 if (dev->parent && device_is_not_partition(dev)) {
Kay Sieversda231fd2007-11-21 17:29:15 +0100702 struct device *parent = dev->parent;
703 char *class_name;
Dmitry Torokhov4f01a752007-09-18 22:46:50 -0700704
Kay Sieversda231fd2007-11-21 17:29:15 +0100705 /*
706 * stacked class devices have the 'device' link
707 * pointing to the bus device instead of the parent
708 */
709 while (parent->class && !parent->bus && parent->parent)
710 parent = parent->parent;
Dmitry Torokhov4f01a752007-09-18 22:46:50 -0700711
Kay Sieversda231fd2007-11-21 17:29:15 +0100712 error = sysfs_create_link(&dev->kobj,
713 &parent->kobj,
714 "device");
715 if (error)
716 goto out_busid;
Dmitry Torokhov4f01a752007-09-18 22:46:50 -0700717
Kay Sieversda231fd2007-11-21 17:29:15 +0100718 class_name = make_class_name(dev->class->name,
719 &dev->kobj);
720 if (class_name)
721 error = sysfs_create_link(&dev->parent->kobj,
722 &dev->kobj, class_name);
723 kfree(class_name);
724 if (error)
725 goto out_device;
726 }
727 return 0;
728
729out_device:
Greg Kroah-Hartman4e886c22008-01-27 12:12:43 -0800730 if (dev->parent && device_is_not_partition(dev))
Kay Sieversda231fd2007-11-21 17:29:15 +0100731 sysfs_remove_link(&dev->kobj, "device");
732out_busid:
Greg Kroah-Hartman1fbfee62008-05-28 09:28:39 -0700733 if (dev->kobj.parent != &dev->class->p->class_subsys.kobj &&
Greg Kroah-Hartman4e886c22008-01-27 12:12:43 -0800734 device_is_not_partition(dev))
Greg Kroah-Hartman1fbfee62008-05-28 09:28:39 -0700735 sysfs_remove_link(&dev->class->p->class_subsys.kobj,
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100736 dev_name(dev));
Dmitry Torokhov4f01a752007-09-18 22:46:50 -0700737#else
Kay Sieversda231fd2007-11-21 17:29:15 +0100738 /* link in the class directory pointing to the device */
Greg Kroah-Hartman1fbfee62008-05-28 09:28:39 -0700739 error = sysfs_create_link(&dev->class->p->class_subsys.kobj,
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100740 &dev->kobj, dev_name(dev));
Kay Sieversda231fd2007-11-21 17:29:15 +0100741 if (error)
742 goto out_subsys;
743
Greg Kroah-Hartman4e886c22008-01-27 12:12:43 -0800744 if (dev->parent && device_is_not_partition(dev)) {
Dmitry Torokhov4f01a752007-09-18 22:46:50 -0700745 error = sysfs_create_link(&dev->kobj, &dev->parent->kobj,
746 "device");
747 if (error)
748 goto out_busid;
Cornelia Huck2ee97caf2007-07-18 01:43:47 -0700749 }
750 return 0;
751
Cornelia Huck2ee97caf2007-07-18 01:43:47 -0700752out_busid:
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100753 sysfs_remove_link(&dev->class->p->class_subsys.kobj, dev_name(dev));
Kay Sieversda231fd2007-11-21 17:29:15 +0100754#endif
755
Cornelia Huck2ee97caf2007-07-18 01:43:47 -0700756out_subsys:
757 sysfs_remove_link(&dev->kobj, "subsystem");
758out:
759 return error;
760}
761
762static void device_remove_class_symlinks(struct device *dev)
763{
764 if (!dev->class)
765 return;
Kay Sieversda231fd2007-11-21 17:29:15 +0100766
Cornelia Huck2ee97caf2007-07-18 01:43:47 -0700767#ifdef CONFIG_SYSFS_DEPRECATED
Greg Kroah-Hartman4e886c22008-01-27 12:12:43 -0800768 if (dev->parent && device_is_not_partition(dev)) {
Cornelia Huck2ee97caf2007-07-18 01:43:47 -0700769 char *class_name;
770
771 class_name = make_class_name(dev->class->name, &dev->kobj);
772 if (class_name) {
773 sysfs_remove_link(&dev->parent->kobj, class_name);
774 kfree(class_name);
775 }
Cornelia Huck2ee97caf2007-07-18 01:43:47 -0700776 sysfs_remove_link(&dev->kobj, "device");
777 }
Kay Sieversda231fd2007-11-21 17:29:15 +0100778
Greg Kroah-Hartman1fbfee62008-05-28 09:28:39 -0700779 if (dev->kobj.parent != &dev->class->p->class_subsys.kobj &&
Greg Kroah-Hartman4e886c22008-01-27 12:12:43 -0800780 device_is_not_partition(dev))
Greg Kroah-Hartman1fbfee62008-05-28 09:28:39 -0700781 sysfs_remove_link(&dev->class->p->class_subsys.kobj,
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100782 dev_name(dev));
Kay Sieversda231fd2007-11-21 17:29:15 +0100783#else
Greg Kroah-Hartman4e886c22008-01-27 12:12:43 -0800784 if (dev->parent && device_is_not_partition(dev))
Kay Sieversda231fd2007-11-21 17:29:15 +0100785 sysfs_remove_link(&dev->kobj, "device");
786
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100787 sysfs_remove_link(&dev->class->p->class_subsys.kobj, dev_name(dev));
Kay Sieversda231fd2007-11-21 17:29:15 +0100788#endif
789
Cornelia Huck2ee97caf2007-07-18 01:43:47 -0700790 sysfs_remove_link(&dev->kobj, "subsystem");
791}
792
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793/**
Stephen Rothwell413c2392008-05-30 10:16:40 +1000794 * dev_set_name - set a device name
795 * @dev: device
Randy Dunlap46232362008-06-04 21:40:43 -0700796 * @fmt: format string for the device's name
Stephen Rothwell413c2392008-05-30 10:16:40 +1000797 */
798int dev_set_name(struct device *dev, const char *fmt, ...)
799{
800 va_list vargs;
Kay Sievers1fa5ae82009-01-25 15:17:37 +0100801 int err;
Stephen Rothwell413c2392008-05-30 10:16:40 +1000802
803 va_start(vargs, fmt);
Kay Sievers1fa5ae82009-01-25 15:17:37 +0100804 err = kobject_set_name_vargs(&dev->kobj, fmt, vargs);
Stephen Rothwell413c2392008-05-30 10:16:40 +1000805 va_end(vargs);
Kay Sievers1fa5ae82009-01-25 15:17:37 +0100806 return err;
Stephen Rothwell413c2392008-05-30 10:16:40 +1000807}
808EXPORT_SYMBOL_GPL(dev_set_name);
809
810/**
Dan Williamse105b8b2008-04-21 10:51:07 -0700811 * device_to_dev_kobj - select a /sys/dev/ directory for the device
812 * @dev: device
813 *
814 * By default we select char/ for new entries. Setting class->dev_obj
815 * to NULL prevents an entry from being created. class->dev_kobj must
816 * be set (or cleared) before any devices are registered to the class
817 * otherwise device_create_sys_dev_entry() and
818 * device_remove_sys_dev_entry() will disagree about the the presence
819 * of the link.
820 */
821static struct kobject *device_to_dev_kobj(struct device *dev)
822{
823 struct kobject *kobj;
824
825 if (dev->class)
826 kobj = dev->class->dev_kobj;
827 else
828 kobj = sysfs_dev_char_kobj;
829
830 return kobj;
831}
832
833static int device_create_sys_dev_entry(struct device *dev)
834{
835 struct kobject *kobj = device_to_dev_kobj(dev);
836 int error = 0;
837 char devt_str[15];
838
839 if (kobj) {
840 format_dev_t(devt_str, dev->devt);
841 error = sysfs_create_link(kobj, &dev->kobj, devt_str);
842 }
843
844 return error;
845}
846
847static void device_remove_sys_dev_entry(struct device *dev)
848{
849 struct kobject *kobj = device_to_dev_kobj(dev);
850 char devt_str[15];
851
852 if (kobj) {
853 format_dev_t(devt_str, dev->devt);
854 sysfs_remove_link(kobj, devt_str);
855 }
856}
857
Greg Kroah-Hartmanb4028432009-05-11 14:16:57 -0700858int device_private_init(struct device *dev)
859{
860 dev->p = kzalloc(sizeof(*dev->p), GFP_KERNEL);
861 if (!dev->p)
862 return -ENOMEM;
863 dev->p->device = dev;
864 klist_init(&dev->p->klist_children, klist_children_get,
865 klist_children_put);
866 return 0;
867}
868
Dan Williamse105b8b2008-04-21 10:51:07 -0700869/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800870 * device_add - add device to device hierarchy.
871 * @dev: device.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800873 * This is part 2 of device_register(), though may be called
874 * separately _iff_ device_initialize() has been called separately.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 *
Cornelia Huck57394112008-09-03 18:26:40 +0200876 * This adds @dev to the kobject hierarchy via kobject_add(), adds it
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800877 * to the global and sibling lists for the device, then
878 * adds it to the other relevant subsystems of the driver model.
Cornelia Huck57394112008-09-03 18:26:40 +0200879 *
880 * NOTE: _Never_ directly free @dev after calling this function, even
881 * if it returned an error! Always use put_device() to give up your
882 * reference instead.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 */
884int device_add(struct device *dev)
885{
886 struct device *parent = NULL;
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +0200887 struct class_interface *class_intf;
Greg Kroah-Hartmanc906a482008-05-30 10:45:12 -0700888 int error = -EINVAL;
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100889
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 dev = get_device(dev);
Greg Kroah-Hartmanc906a482008-05-30 10:45:12 -0700891 if (!dev)
892 goto done;
893
Greg Kroah-Hartmanfb069a52008-12-16 12:23:36 -0800894 if (!dev->p) {
Greg Kroah-Hartmanb4028432009-05-11 14:16:57 -0700895 error = device_private_init(dev);
896 if (error)
897 goto done;
Greg Kroah-Hartmanfb069a52008-12-16 12:23:36 -0800898 }
Greg Kroah-Hartmanfb069a52008-12-16 12:23:36 -0800899
Kay Sievers1fa5ae82009-01-25 15:17:37 +0100900 /*
901 * for statically allocated devices, which should all be converted
902 * some day, we need to initialize the name. We prevent reading back
903 * the name, and force the use of dev_name()
904 */
905 if (dev->init_name) {
Greg Kroah-Hartmanacc0e902009-06-02 15:39:55 -0700906 dev_set_name(dev, "%s", dev->init_name);
Kay Sievers1fa5ae82009-01-25 15:17:37 +0100907 dev->init_name = NULL;
908 }
Greg Kroah-Hartmanc906a482008-05-30 10:45:12 -0700909
Kay Sievers1fa5ae82009-01-25 15:17:37 +0100910 if (!dev_name(dev))
Kay Sievers5c8563d2009-05-28 14:24:07 -0700911 goto name_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100913 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
Greg Kroah-Hartmanc205ef42006-08-07 22:19:37 -0700914
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 parent = get_device(dev->parent);
Cornelia Huck63b69712008-01-21 16:09:44 +0100916 setup_parent(dev, parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917
Yinghai Lu0d358f22008-02-19 03:20:41 -0800918 /* use parent numa_node */
919 if (parent)
920 set_dev_node(dev, dev_to_node(parent));
921
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 /* first, register with generic layer. */
Kay Sievers8a577ff2009-04-18 15:05:45 -0700923 /* we require the name to be set before, and pass NULL */
924 error = kobject_add(&dev->kobj, dev->kobj.parent, NULL);
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +0100925 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 goto Error;
Kay Sieversa7fd6702005-10-01 14:49:43 +0200927
Brian Walsh37022642006-08-14 22:43:19 -0700928 /* notify platform of device entry */
929 if (platform_notify)
930 platform_notify(dev);
931
Tejun Heoad6a1e12007-06-14 03:45:17 +0900932 error = device_create_file(dev, &uevent_attr);
Cornelia Hucka306eea2006-09-22 11:37:13 +0200933 if (error)
934 goto attrError;
Kay Sieversa7fd6702005-10-01 14:49:43 +0200935
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700936 if (MAJOR(dev->devt)) {
Tejun Heoad6a1e12007-06-14 03:45:17 +0900937 error = device_create_file(dev, &devt_attr);
938 if (error)
Cornelia Hucka306eea2006-09-22 11:37:13 +0200939 goto ueventattrError;
Dan Williamse105b8b2008-04-21 10:51:07 -0700940
941 error = device_create_sys_dev_entry(dev);
942 if (error)
943 goto devtattrError;
Kay Sievers2b2af542009-04-30 15:23:42 +0200944
945 devtmpfs_create_node(dev);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700946 }
947
Cornelia Huck2ee97caf2007-07-18 01:43:47 -0700948 error = device_add_class_symlinks(dev);
949 if (error)
950 goto SymlinkError;
Cornelia Huckdc0afa82007-07-09 11:39:18 -0700951 error = device_add_attrs(dev);
952 if (error)
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700953 goto AttrsError;
Cornelia Huckdc0afa82007-07-09 11:39:18 -0700954 error = bus_add_device(dev);
955 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 goto BusError;
Alan Stern3b98aea2008-08-07 13:06:12 -0400957 error = dpm_sysfs_add(dev);
Rafael J. Wysocki57eee3d2008-03-12 00:59:38 +0100958 if (error)
Alan Stern3b98aea2008-08-07 13:06:12 -0400959 goto DPMError;
960 device_pm_add(dev);
Alan Sternec0676ee2008-12-05 14:10:31 -0500961
962 /* Notify clients of device addition. This call must come
963 * after dpm_sysf_add() and before kobject_uevent().
964 */
965 if (dev->bus)
966 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
967 BUS_NOTIFY_ADD_DEVICE, dev);
968
Cornelia Huck83b5fb4c2007-03-29 11:12:11 +0200969 kobject_uevent(&dev->kobj, KOBJ_ADD);
Alan Stern2023c612009-07-30 15:27:18 -0400970 bus_probe_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 if (parent)
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -0800972 klist_add_tail(&dev->p->knode_parent,
973 &parent->p->klist_children);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974
Greg Kroah-Hartman5d9fd162006-06-22 17:17:32 -0700975 if (dev->class) {
Dave Youngf75b1c62008-05-28 09:28:39 -0700976 mutex_lock(&dev->class->p->class_mutex);
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +0200977 /* tie the class to the device */
Tejun Heo5a3ceb82008-08-25 19:50:19 +0200978 klist_add_tail(&dev->knode_class,
979 &dev->class->p->class_devices);
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +0200980
981 /* notify any interfaces that the device is here */
Greg Kroah-Hartman184f1f72008-05-28 09:28:39 -0700982 list_for_each_entry(class_intf,
983 &dev->class->p->class_interfaces, node)
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +0200984 if (class_intf->add_dev)
985 class_intf->add_dev(dev, class_intf);
Dave Youngf75b1c62008-05-28 09:28:39 -0700986 mutex_unlock(&dev->class->p->class_mutex);
Greg Kroah-Hartman5d9fd162006-06-22 17:17:32 -0700987 }
Greg Kroah-Hartmanc906a482008-05-30 10:45:12 -0700988done:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 put_device(dev);
990 return error;
Alan Stern3b98aea2008-08-07 13:06:12 -0400991 DPMError:
Rafael J. Wysocki57eee3d2008-03-12 00:59:38 +0100992 bus_remove_device(dev);
993 BusError:
James Simmons82f0cf92007-02-21 17:44:51 +0000994 device_remove_attrs(dev);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700995 AttrsError:
Cornelia Huck2ee97caf2007-07-18 01:43:47 -0700996 device_remove_class_symlinks(dev);
997 SymlinkError:
Tejun Heoad6a1e12007-06-14 03:45:17 +0900998 if (MAJOR(dev->devt))
Kay Sieversad729562009-10-28 19:51:37 +0100999 devtmpfs_delete_node(dev);
1000 if (MAJOR(dev->devt))
Dan Williamse105b8b2008-04-21 10:51:07 -07001001 device_remove_sys_dev_entry(dev);
1002 devtattrError:
1003 if (MAJOR(dev->devt))
Tejun Heoad6a1e12007-06-14 03:45:17 +09001004 device_remove_file(dev, &devt_attr);
Cornelia Hucka306eea2006-09-22 11:37:13 +02001005 ueventattrError:
Tejun Heoad6a1e12007-06-14 03:45:17 +09001006 device_remove_file(dev, &uevent_attr);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001007 attrError:
Kay Sievers312c0042005-11-16 09:00:00 +01001008 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 kobject_del(&dev->kobj);
1010 Error:
Cornelia Huck63b69712008-01-21 16:09:44 +01001011 cleanup_device_parent(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 if (parent)
1013 put_device(parent);
Kay Sievers5c8563d2009-05-28 14:24:07 -07001014name_error:
1015 kfree(dev->p);
1016 dev->p = NULL;
Greg Kroah-Hartmanc906a482008-05-30 10:45:12 -07001017 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018}
1019
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001021 * device_register - register a device with the system.
1022 * @dev: pointer to the device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001024 * This happens in two clean steps - initialize the device
1025 * and add it to the system. The two steps can be called
1026 * separately, but this is the easiest and most common.
1027 * I.e. you should only call the two helpers separately if
1028 * have a clearly defined need to use and refcount the device
1029 * before it is added to the hierarchy.
Cornelia Huck57394112008-09-03 18:26:40 +02001030 *
1031 * NOTE: _Never_ directly free @dev after calling this function, even
1032 * if it returned an error! Always use put_device() to give up the
1033 * reference initialized in this function instead.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035int device_register(struct device *dev)
1036{
1037 device_initialize(dev);
1038 return device_add(dev);
1039}
1040
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001042 * get_device - increment reference count for device.
1043 * @dev: device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001045 * This simply forwards the call to kobject_get(), though
1046 * we do take care to provide for the case that we get a NULL
1047 * pointer passed in.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001049struct device *get_device(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050{
1051 return dev ? to_dev(kobject_get(&dev->kobj)) : NULL;
1052}
1053
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001055 * put_device - decrement reference count.
1056 * @dev: device in question.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001058void put_device(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059{
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001060 /* might_sleep(); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 if (dev)
1062 kobject_put(&dev->kobj);
1063}
1064
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001066 * device_del - delete device from system.
1067 * @dev: device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001069 * This is the first part of the device unregistration
1070 * sequence. This removes the device from the lists we control
1071 * from here, has it removed from the other driver model
1072 * subsystems it was added to in device_add(), and removes it
1073 * from the kobject hierarchy.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001075 * NOTE: this should be called manually _iff_ device_add() was
1076 * also called manually.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001078void device_del(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001080 struct device *parent = dev->parent;
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02001081 struct class_interface *class_intf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082
Alan Sternec0676ee2008-12-05 14:10:31 -05001083 /* Notify clients of device removal. This call must come
1084 * before dpm_sysfs_remove().
1085 */
1086 if (dev->bus)
1087 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
1088 BUS_NOTIFY_DEL_DEVICE, dev);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001089 device_pm_remove(dev);
Alan Stern3b98aea2008-08-07 13:06:12 -04001090 dpm_sysfs_remove(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 if (parent)
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08001092 klist_del(&dev->p->knode_parent);
Dan Williamse105b8b2008-04-21 10:51:07 -07001093 if (MAJOR(dev->devt)) {
Kay Sievers2b2af542009-04-30 15:23:42 +02001094 devtmpfs_delete_node(dev);
Dan Williamse105b8b2008-04-21 10:51:07 -07001095 device_remove_sys_dev_entry(dev);
Tejun Heoad6a1e12007-06-14 03:45:17 +09001096 device_remove_file(dev, &devt_attr);
Dan Williamse105b8b2008-04-21 10:51:07 -07001097 }
Kay Sieversb9d9c822006-06-15 15:31:56 +02001098 if (dev->class) {
Kay Sieversda231fd2007-11-21 17:29:15 +01001099 device_remove_class_symlinks(dev);
Kay Sievers99ef3ef2006-09-14 11:23:28 +02001100
Dave Youngf75b1c62008-05-28 09:28:39 -07001101 mutex_lock(&dev->class->p->class_mutex);
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02001102 /* notify any interfaces that the device is now gone */
Greg Kroah-Hartman184f1f72008-05-28 09:28:39 -07001103 list_for_each_entry(class_intf,
1104 &dev->class->p->class_interfaces, node)
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02001105 if (class_intf->remove_dev)
1106 class_intf->remove_dev(dev, class_intf);
1107 /* remove the device from the class list */
Tejun Heo5a3ceb82008-08-25 19:50:19 +02001108 klist_del(&dev->knode_class);
Dave Youngf75b1c62008-05-28 09:28:39 -07001109 mutex_unlock(&dev->class->p->class_mutex);
Kay Sieversb9d9c822006-06-15 15:31:56 +02001110 }
Tejun Heoad6a1e12007-06-14 03:45:17 +09001111 device_remove_file(dev, &uevent_attr);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07001112 device_remove_attrs(dev);
Benjamin Herrenschmidt28953532006-11-08 19:46:14 -08001113 bus_remove_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114
Tejun Heo2f8d16a2007-03-09 19:34:19 +09001115 /*
1116 * Some platform devices are driven without driver attached
1117 * and managed resources may have been acquired. Make sure
1118 * all resources are released.
1119 */
1120 devres_release_all(dev);
1121
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 /* Notify the platform of the removal, in case they
1123 * need to do anything...
1124 */
1125 if (platform_notify_remove)
1126 platform_notify_remove(dev);
Kay Sievers312c0042005-11-16 09:00:00 +01001127 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
Kay Sieversda231fd2007-11-21 17:29:15 +01001128 cleanup_device_parent(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 kobject_del(&dev->kobj);
Kay Sieversda231fd2007-11-21 17:29:15 +01001130 put_device(parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131}
1132
1133/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001134 * device_unregister - unregister device from system.
1135 * @dev: device going away.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001137 * We do this in two parts, like we do device_register(). First,
1138 * we remove it from all the subsystems with device_del(), then
1139 * we decrement the reference count via put_device(). If that
1140 * is the final reference count, the device will be cleaned up
1141 * via device_release() above. Otherwise, the structure will
1142 * stick around until the final reference to the device is dropped.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001144void device_unregister(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145{
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01001146 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 device_del(dev);
1148 put_device(dev);
1149}
1150
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001151static struct device *next_device(struct klist_iter *i)
mochel@digitalimplant.org36239572005-03-24 19:08:30 -08001152{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001153 struct klist_node *n = klist_next(i);
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08001154 struct device *dev = NULL;
1155 struct device_private *p;
1156
1157 if (n) {
1158 p = to_device_private_parent(n);
1159 dev = p->device;
1160 }
1161 return dev;
mochel@digitalimplant.org36239572005-03-24 19:08:30 -08001162}
1163
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164/**
Kay Sieverse454cea2009-09-18 23:01:12 +02001165 * device_get_devnode - path of device node file
Kay Sievers6fcf53a2009-04-30 15:23:42 +02001166 * @dev: device
Kay Sieverse454cea2009-09-18 23:01:12 +02001167 * @mode: returned file access mode
Kay Sievers6fcf53a2009-04-30 15:23:42 +02001168 * @tmp: possibly allocated string
1169 *
1170 * Return the relative path of a possible device node.
1171 * Non-default names may need to allocate a memory to compose
1172 * a name. This memory is returned in tmp and needs to be
1173 * freed by the caller.
1174 */
Kay Sieverse454cea2009-09-18 23:01:12 +02001175const char *device_get_devnode(struct device *dev,
1176 mode_t *mode, const char **tmp)
Kay Sievers6fcf53a2009-04-30 15:23:42 +02001177{
1178 char *s;
1179
1180 *tmp = NULL;
1181
1182 /* the device type may provide a specific name */
Kay Sieverse454cea2009-09-18 23:01:12 +02001183 if (dev->type && dev->type->devnode)
1184 *tmp = dev->type->devnode(dev, mode);
Kay Sievers6fcf53a2009-04-30 15:23:42 +02001185 if (*tmp)
1186 return *tmp;
1187
1188 /* the class may provide a specific name */
Kay Sieverse454cea2009-09-18 23:01:12 +02001189 if (dev->class && dev->class->devnode)
1190 *tmp = dev->class->devnode(dev, mode);
Kay Sievers6fcf53a2009-04-30 15:23:42 +02001191 if (*tmp)
1192 return *tmp;
1193
1194 /* return name without allocation, tmp == NULL */
1195 if (strchr(dev_name(dev), '!') == NULL)
1196 return dev_name(dev);
1197
1198 /* replace '!' in the name with '/' */
1199 *tmp = kstrdup(dev_name(dev), GFP_KERNEL);
1200 if (!*tmp)
1201 return NULL;
1202 while ((s = strchr(*tmp, '!')))
1203 s[0] = '/';
1204 return *tmp;
1205}
1206
1207/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001208 * device_for_each_child - device child iterator.
1209 * @parent: parent struct device.
1210 * @data: data for the callback.
1211 * @fn: function to be called for each device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001213 * Iterate over @parent's child devices, and call @fn for each,
1214 * passing it @data.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001216 * We check the return of @fn each time. If it returns anything
1217 * other than 0, we break out and return that value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001219int device_for_each_child(struct device *parent, void *data,
1220 int (*fn)(struct device *dev, void *data))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221{
mochel@digitalimplant.org36239572005-03-24 19:08:30 -08001222 struct klist_iter i;
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001223 struct device *child;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 int error = 0;
1225
Greg Kroah-Hartman014c90d2009-04-15 16:00:12 -07001226 if (!parent->p)
1227 return 0;
1228
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08001229 klist_iter_init(&parent->p->klist_children, &i);
mochel@digitalimplant.org36239572005-03-24 19:08:30 -08001230 while ((child = next_device(&i)) && !error)
1231 error = fn(child, data);
1232 klist_iter_exit(&i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 return error;
1234}
1235
Cornelia Huck5ab69982006-11-16 15:42:07 +01001236/**
1237 * device_find_child - device iterator for locating a particular device.
1238 * @parent: parent struct device
1239 * @data: Data to pass to match function
1240 * @match: Callback function to check device
1241 *
1242 * This is similar to the device_for_each_child() function above, but it
1243 * returns a reference to a device that is 'found' for later use, as
1244 * determined by the @match callback.
1245 *
1246 * The callback should return 0 if the device doesn't match and non-zero
1247 * if it does. If the callback returns non-zero and a reference to the
1248 * current device can be obtained, this function will return to the caller
1249 * and not iterate over any more devices.
1250 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001251struct device *device_find_child(struct device *parent, void *data,
1252 int (*match)(struct device *dev, void *data))
Cornelia Huck5ab69982006-11-16 15:42:07 +01001253{
1254 struct klist_iter i;
1255 struct device *child;
1256
1257 if (!parent)
1258 return NULL;
1259
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08001260 klist_iter_init(&parent->p->klist_children, &i);
Cornelia Huck5ab69982006-11-16 15:42:07 +01001261 while ((child = next_device(&i)))
1262 if (match(child, data) && get_device(child))
1263 break;
1264 klist_iter_exit(&i);
1265 return child;
1266}
1267
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268int __init devices_init(void)
1269{
Greg Kroah-Hartman881c6cf2007-11-01 09:29:06 -06001270 devices_kset = kset_create_and_add("devices", &device_uevent_ops, NULL);
1271 if (!devices_kset)
1272 return -ENOMEM;
Dan Williamse105b8b2008-04-21 10:51:07 -07001273 dev_kobj = kobject_create_and_add("dev", NULL);
1274 if (!dev_kobj)
1275 goto dev_kobj_err;
1276 sysfs_dev_block_kobj = kobject_create_and_add("block", dev_kobj);
1277 if (!sysfs_dev_block_kobj)
1278 goto block_kobj_err;
1279 sysfs_dev_char_kobj = kobject_create_and_add("char", dev_kobj);
1280 if (!sysfs_dev_char_kobj)
1281 goto char_kobj_err;
1282
Greg Kroah-Hartman881c6cf2007-11-01 09:29:06 -06001283 return 0;
Dan Williamse105b8b2008-04-21 10:51:07 -07001284
1285 char_kobj_err:
1286 kobject_put(sysfs_dev_block_kobj);
1287 block_kobj_err:
1288 kobject_put(dev_kobj);
1289 dev_kobj_err:
1290 kset_unregister(devices_kset);
1291 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292}
1293
1294EXPORT_SYMBOL_GPL(device_for_each_child);
Cornelia Huck5ab69982006-11-16 15:42:07 +01001295EXPORT_SYMBOL_GPL(device_find_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296
1297EXPORT_SYMBOL_GPL(device_initialize);
1298EXPORT_SYMBOL_GPL(device_add);
1299EXPORT_SYMBOL_GPL(device_register);
1300
1301EXPORT_SYMBOL_GPL(device_del);
1302EXPORT_SYMBOL_GPL(device_unregister);
1303EXPORT_SYMBOL_GPL(get_device);
1304EXPORT_SYMBOL_GPL(put_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305
1306EXPORT_SYMBOL_GPL(device_create_file);
1307EXPORT_SYMBOL_GPL(device_remove_file);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001308
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00001309struct root_device
1310{
1311 struct device dev;
1312 struct module *owner;
1313};
1314
1315#define to_root_device(dev) container_of(dev, struct root_device, dev)
1316
1317static void root_device_release(struct device *dev)
1318{
1319 kfree(to_root_device(dev));
1320}
1321
1322/**
1323 * __root_device_register - allocate and register a root device
1324 * @name: root device name
1325 * @owner: owner module of the root device, usually THIS_MODULE
1326 *
1327 * This function allocates a root device and registers it
1328 * using device_register(). In order to free the returned
1329 * device, use root_device_unregister().
1330 *
1331 * Root devices are dummy devices which allow other devices
1332 * to be grouped under /sys/devices. Use this function to
1333 * allocate a root device and then use it as the parent of
1334 * any device which should appear under /sys/devices/{name}
1335 *
1336 * The /sys/devices/{name} directory will also contain a
1337 * 'module' symlink which points to the @owner directory
1338 * in sysfs.
1339 *
1340 * Note: You probably want to use root_device_register().
1341 */
1342struct device *__root_device_register(const char *name, struct module *owner)
1343{
1344 struct root_device *root;
1345 int err = -ENOMEM;
1346
1347 root = kzalloc(sizeof(struct root_device), GFP_KERNEL);
1348 if (!root)
1349 return ERR_PTR(err);
1350
Greg Kroah-Hartmanacc0e902009-06-02 15:39:55 -07001351 err = dev_set_name(&root->dev, "%s", name);
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00001352 if (err) {
1353 kfree(root);
1354 return ERR_PTR(err);
1355 }
1356
1357 root->dev.release = root_device_release;
1358
1359 err = device_register(&root->dev);
1360 if (err) {
1361 put_device(&root->dev);
1362 return ERR_PTR(err);
1363 }
1364
1365#ifdef CONFIG_MODULE /* gotta find a "cleaner" way to do this */
1366 if (owner) {
1367 struct module_kobject *mk = &owner->mkobj;
1368
1369 err = sysfs_create_link(&root->dev.kobj, &mk->kobj, "module");
1370 if (err) {
1371 device_unregister(&root->dev);
1372 return ERR_PTR(err);
1373 }
1374 root->owner = owner;
1375 }
1376#endif
1377
1378 return &root->dev;
1379}
1380EXPORT_SYMBOL_GPL(__root_device_register);
1381
1382/**
1383 * root_device_unregister - unregister and free a root device
Randy Dunlap7cbcf222009-01-20 16:29:13 -08001384 * @dev: device going away
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00001385 *
1386 * This function unregisters and cleans up a device that was created by
1387 * root_device_register().
1388 */
1389void root_device_unregister(struct device *dev)
1390{
1391 struct root_device *root = to_root_device(dev);
1392
1393 if (root->owner)
1394 sysfs_remove_link(&root->dev.kobj, "module");
1395
1396 device_unregister(dev);
1397}
1398EXPORT_SYMBOL_GPL(root_device_unregister);
1399
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001400
1401static void device_create_release(struct device *dev)
1402{
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01001403 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001404 kfree(dev);
1405}
1406
1407/**
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07001408 * device_create_vargs - creates a device and registers it with sysfs
1409 * @class: pointer to the struct class that this device should be registered to
1410 * @parent: pointer to the parent struct device of this new device, if any
1411 * @devt: the dev_t for the char device to be added
1412 * @drvdata: the data to be added to the device for callbacks
1413 * @fmt: string for the device's name
1414 * @args: va_list for the device's name
1415 *
1416 * This function can be used by char device classes. A struct device
1417 * will be created in sysfs, registered to the specified class.
1418 *
1419 * A "dev" file will be created, showing the dev_t for the device, if
1420 * the dev_t is not 0,0.
1421 * If a pointer to a parent struct device is passed in, the newly created
1422 * struct device will be a child of that device in sysfs.
1423 * The pointer to the struct device will be returned from the call.
1424 * Any further sysfs files that might be required can be created using this
1425 * pointer.
1426 *
1427 * Note: the struct class passed to this function must have previously
1428 * been created with a call to class_create().
1429 */
1430struct device *device_create_vargs(struct class *class, struct device *parent,
1431 dev_t devt, void *drvdata, const char *fmt,
1432 va_list args)
1433{
1434 struct device *dev = NULL;
1435 int retval = -ENODEV;
1436
1437 if (class == NULL || IS_ERR(class))
1438 goto error;
1439
1440 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1441 if (!dev) {
1442 retval = -ENOMEM;
1443 goto error;
1444 }
1445
1446 dev->devt = devt;
1447 dev->class = class;
1448 dev->parent = parent;
1449 dev->release = device_create_release;
1450 dev_set_drvdata(dev, drvdata);
1451
Kay Sievers1fa5ae82009-01-25 15:17:37 +01001452 retval = kobject_set_name_vargs(&dev->kobj, fmt, args);
1453 if (retval)
1454 goto error;
1455
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07001456 retval = device_register(dev);
1457 if (retval)
1458 goto error;
1459
1460 return dev;
1461
1462error:
Cornelia Huck286661b2008-09-03 18:26:41 +02001463 put_device(dev);
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07001464 return ERR_PTR(retval);
1465}
1466EXPORT_SYMBOL_GPL(device_create_vargs);
1467
1468/**
Greg Kroah-Hartman4e106732008-07-21 20:03:34 -07001469 * device_create - creates a device and registers it with sysfs
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07001470 * @class: pointer to the struct class that this device should be registered to
1471 * @parent: pointer to the parent struct device of this new device, if any
1472 * @devt: the dev_t for the char device to be added
1473 * @drvdata: the data to be added to the device for callbacks
1474 * @fmt: string for the device's name
1475 *
1476 * This function can be used by char device classes. A struct device
1477 * will be created in sysfs, registered to the specified class.
1478 *
1479 * A "dev" file will be created, showing the dev_t for the device, if
1480 * the dev_t is not 0,0.
1481 * If a pointer to a parent struct device is passed in, the newly created
1482 * struct device will be a child of that device in sysfs.
1483 * The pointer to the struct device will be returned from the call.
1484 * Any further sysfs files that might be required can be created using this
1485 * pointer.
1486 *
1487 * Note: the struct class passed to this function must have previously
1488 * been created with a call to class_create().
1489 */
Greg Kroah-Hartman4e106732008-07-21 20:03:34 -07001490struct device *device_create(struct class *class, struct device *parent,
1491 dev_t devt, void *drvdata, const char *fmt, ...)
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07001492{
1493 va_list vargs;
1494 struct device *dev;
1495
1496 va_start(vargs, fmt);
1497 dev = device_create_vargs(class, parent, devt, drvdata, fmt, vargs);
1498 va_end(vargs);
1499 return dev;
1500}
Greg Kroah-Hartman4e106732008-07-21 20:03:34 -07001501EXPORT_SYMBOL_GPL(device_create);
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07001502
Dave Youngcd354492008-01-28 16:56:11 +08001503static int __match_devt(struct device *dev, void *data)
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001504{
Dave Youngcd354492008-01-28 16:56:11 +08001505 dev_t *devt = data;
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001506
Dave Youngcd354492008-01-28 16:56:11 +08001507 return dev->devt == *devt;
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001508}
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001509
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001510/**
1511 * device_destroy - removes a device that was created with device_create()
1512 * @class: pointer to the struct class that this device was registered with
1513 * @devt: the dev_t of the device that was previously registered
1514 *
1515 * This call unregisters and cleans up a device that was created with a
1516 * call to device_create().
1517 */
1518void device_destroy(struct class *class, dev_t devt)
1519{
1520 struct device *dev;
1521
Greg Kroah-Hartman695794a2008-05-22 17:21:08 -04001522 dev = class_find_device(class, NULL, &devt, __match_devt);
Dave Youngcd354492008-01-28 16:56:11 +08001523 if (dev) {
1524 put_device(dev);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001525 device_unregister(dev);
Dave Youngcd354492008-01-28 16:56:11 +08001526 }
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001527}
1528EXPORT_SYMBOL_GPL(device_destroy);
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001529
1530/**
1531 * device_rename - renames a device
1532 * @dev: the pointer to the struct device to be renamed
1533 * @new_name: the new name of the device
Eric W. Biederman030c1d22008-05-08 14:41:00 -07001534 *
1535 * It is the responsibility of the caller to provide mutual
1536 * exclusion between two different calls of device_rename
1537 * on the same device to ensure that new_name is valid and
1538 * won't conflict with other devices.
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001539 */
1540int device_rename(struct device *dev, char *new_name)
1541{
1542 char *old_class_name = NULL;
1543 char *new_class_name = NULL;
Cornelia Huck2ee97caf2007-07-18 01:43:47 -07001544 char *old_device_name = NULL;
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001545 int error;
1546
1547 dev = get_device(dev);
1548 if (!dev)
1549 return -EINVAL;
1550
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01001551 pr_debug("device: '%s': %s: renaming to '%s'\n", dev_name(dev),
Harvey Harrison2b3a3022008-03-04 16:41:05 -08001552 __func__, new_name);
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001553
Kay Sievers99ef3ef2006-09-14 11:23:28 +02001554#ifdef CONFIG_SYSFS_DEPRECATED
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001555 if ((dev->class) && (dev->parent))
1556 old_class_name = make_class_name(dev->class->name, &dev->kobj);
Kay Sievers99ef3ef2006-09-14 11:23:28 +02001557#endif
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001558
Kay Sievers1fa5ae82009-01-25 15:17:37 +01001559 old_device_name = kstrdup(dev_name(dev), GFP_KERNEL);
Cornelia Huck2ee97caf2007-07-18 01:43:47 -07001560 if (!old_device_name) {
1561 error = -ENOMEM;
1562 goto out;
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001563 }
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001564
1565 error = kobject_rename(&dev->kobj, new_name);
Kay Sievers1fa5ae82009-01-25 15:17:37 +01001566 if (error)
Cornelia Huck2ee97caf2007-07-18 01:43:47 -07001567 goto out;
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001568
Kay Sievers99ef3ef2006-09-14 11:23:28 +02001569#ifdef CONFIG_SYSFS_DEPRECATED
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001570 if (old_class_name) {
1571 new_class_name = make_class_name(dev->class->name, &dev->kobj);
1572 if (new_class_name) {
Cornelia Huck36ce6da2008-06-10 11:09:08 +02001573 error = sysfs_create_link_nowarn(&dev->parent->kobj,
1574 &dev->kobj,
1575 new_class_name);
Cornelia Huck2ee97caf2007-07-18 01:43:47 -07001576 if (error)
1577 goto out;
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001578 sysfs_remove_link(&dev->parent->kobj, old_class_name);
1579 }
1580 }
Kay Sievers60b8cab2007-10-26 20:07:44 +02001581#else
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001582 if (dev->class) {
Cornelia Huck36ce6da2008-06-10 11:09:08 +02001583 error = sysfs_create_link_nowarn(&dev->class->p->class_subsys.kobj,
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01001584 &dev->kobj, dev_name(dev));
Stephen Hemminger0599ad52008-05-14 22:34:16 -07001585 if (error)
1586 goto out;
Greg Kroah-Hartman1fbfee62008-05-28 09:28:39 -07001587 sysfs_remove_link(&dev->class->p->class_subsys.kobj,
Greg Kroah-Hartman7c714482008-01-22 18:17:41 -05001588 old_device_name);
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001589 }
Kay Sievers60b8cab2007-10-26 20:07:44 +02001590#endif
1591
Cornelia Huck2ee97caf2007-07-18 01:43:47 -07001592out:
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001593 put_device(dev);
1594
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001595 kfree(new_class_name);
Jesper Juhl952ab432006-09-28 23:56:01 +02001596 kfree(old_class_name);
Cornelia Huck2ee97caf2007-07-18 01:43:47 -07001597 kfree(old_device_name);
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001598
1599 return error;
1600}
Johannes Berga2807db2007-02-28 12:38:31 +01001601EXPORT_SYMBOL_GPL(device_rename);
Cornelia Huck8a824722006-11-20 17:07:51 +01001602
1603static int device_move_class_links(struct device *dev,
1604 struct device *old_parent,
1605 struct device *new_parent)
1606{
Greg Kroah-Hartmanf7f34612007-03-06 12:55:53 -08001607 int error = 0;
Cornelia Huck8a824722006-11-20 17:07:51 +01001608#ifdef CONFIG_SYSFS_DEPRECATED
Cornelia Huck8a824722006-11-20 17:07:51 +01001609 char *class_name;
1610
1611 class_name = make_class_name(dev->class->name, &dev->kobj);
1612 if (!class_name) {
Cornelia Huckcb360bb2006-11-27 10:35:05 +01001613 error = -ENOMEM;
Cornelia Huck8a824722006-11-20 17:07:51 +01001614 goto out;
1615 }
1616 if (old_parent) {
1617 sysfs_remove_link(&dev->kobj, "device");
1618 sysfs_remove_link(&old_parent->kobj, class_name);
1619 }
Cornelia Huckc744aea2007-01-08 20:16:44 +01001620 if (new_parent) {
1621 error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
1622 "device");
1623 if (error)
1624 goto out;
1625 error = sysfs_create_link(&new_parent->kobj, &dev->kobj,
1626 class_name);
1627 if (error)
1628 sysfs_remove_link(&dev->kobj, "device");
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001629 } else
Cornelia Huckc744aea2007-01-08 20:16:44 +01001630 error = 0;
Cornelia Huck8a824722006-11-20 17:07:51 +01001631out:
1632 kfree(class_name);
1633 return error;
1634#else
Greg Kroah-Hartmanf7f34612007-03-06 12:55:53 -08001635 if (old_parent)
1636 sysfs_remove_link(&dev->kobj, "device");
1637 if (new_parent)
1638 error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
1639 "device");
1640 return error;
Cornelia Huck8a824722006-11-20 17:07:51 +01001641#endif
1642}
1643
1644/**
1645 * device_move - moves a device to a new parent
1646 * @dev: the pointer to the struct device to be moved
Cornelia Huckc744aea2007-01-08 20:16:44 +01001647 * @new_parent: the new parent of the device (can by NULL)
Cornelia Huckffa6a702009-03-04 12:44:00 +01001648 * @dpm_order: how to reorder the dpm_list
Cornelia Huck8a824722006-11-20 17:07:51 +01001649 */
Cornelia Huckffa6a702009-03-04 12:44:00 +01001650int device_move(struct device *dev, struct device *new_parent,
1651 enum dpm_order dpm_order)
Cornelia Huck8a824722006-11-20 17:07:51 +01001652{
1653 int error;
1654 struct device *old_parent;
Cornelia Huckc744aea2007-01-08 20:16:44 +01001655 struct kobject *new_parent_kobj;
Cornelia Huck8a824722006-11-20 17:07:51 +01001656
1657 dev = get_device(dev);
1658 if (!dev)
1659 return -EINVAL;
1660
Cornelia Huckffa6a702009-03-04 12:44:00 +01001661 device_pm_lock();
Cornelia Huck8a824722006-11-20 17:07:51 +01001662 new_parent = get_device(new_parent);
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001663 new_parent_kobj = get_device_parent(dev, new_parent);
Cornelia Huck63b69712008-01-21 16:09:44 +01001664
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01001665 pr_debug("device: '%s': %s: moving to '%s'\n", dev_name(dev),
1666 __func__, new_parent ? dev_name(new_parent) : "<NULL>");
Cornelia Huckc744aea2007-01-08 20:16:44 +01001667 error = kobject_move(&dev->kobj, new_parent_kobj);
Cornelia Huck8a824722006-11-20 17:07:51 +01001668 if (error) {
Cornelia Huck63b69712008-01-21 16:09:44 +01001669 cleanup_glue_dir(dev, new_parent_kobj);
Cornelia Huck8a824722006-11-20 17:07:51 +01001670 put_device(new_parent);
1671 goto out;
1672 }
1673 old_parent = dev->parent;
1674 dev->parent = new_parent;
1675 if (old_parent)
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08001676 klist_remove(&dev->p->knode_parent);
Yinghai Lu0d358f22008-02-19 03:20:41 -08001677 if (new_parent) {
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08001678 klist_add_tail(&dev->p->knode_parent,
1679 &new_parent->p->klist_children);
Yinghai Lu0d358f22008-02-19 03:20:41 -08001680 set_dev_node(dev, dev_to_node(new_parent));
1681 }
1682
Cornelia Huck8a824722006-11-20 17:07:51 +01001683 if (!dev->class)
1684 goto out_put;
1685 error = device_move_class_links(dev, old_parent, new_parent);
1686 if (error) {
1687 /* We ignore errors on cleanup since we're hosed anyway... */
1688 device_move_class_links(dev, new_parent, old_parent);
1689 if (!kobject_move(&dev->kobj, &old_parent->kobj)) {
Cornelia Huckc744aea2007-01-08 20:16:44 +01001690 if (new_parent)
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08001691 klist_remove(&dev->p->knode_parent);
Yinghai Lu0d358f22008-02-19 03:20:41 -08001692 dev->parent = old_parent;
1693 if (old_parent) {
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08001694 klist_add_tail(&dev->p->knode_parent,
1695 &old_parent->p->klist_children);
Yinghai Lu0d358f22008-02-19 03:20:41 -08001696 set_dev_node(dev, dev_to_node(old_parent));
1697 }
Cornelia Huck8a824722006-11-20 17:07:51 +01001698 }
Cornelia Huck63b69712008-01-21 16:09:44 +01001699 cleanup_glue_dir(dev, new_parent_kobj);
Cornelia Huck8a824722006-11-20 17:07:51 +01001700 put_device(new_parent);
1701 goto out;
1702 }
Cornelia Huckffa6a702009-03-04 12:44:00 +01001703 switch (dpm_order) {
1704 case DPM_ORDER_NONE:
1705 break;
1706 case DPM_ORDER_DEV_AFTER_PARENT:
1707 device_pm_move_after(dev, new_parent);
1708 break;
1709 case DPM_ORDER_PARENT_BEFORE_DEV:
1710 device_pm_move_before(new_parent, dev);
1711 break;
1712 case DPM_ORDER_DEV_LAST:
1713 device_pm_move_last(dev);
1714 break;
1715 }
Cornelia Huck8a824722006-11-20 17:07:51 +01001716out_put:
1717 put_device(old_parent);
1718out:
Cornelia Huckffa6a702009-03-04 12:44:00 +01001719 device_pm_unlock();
Cornelia Huck8a824722006-11-20 17:07:51 +01001720 put_device(dev);
1721 return error;
1722}
Cornelia Huck8a824722006-11-20 17:07:51 +01001723EXPORT_SYMBOL_GPL(device_move);
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08001724
1725/**
1726 * device_shutdown - call ->shutdown() on each device to shutdown.
1727 */
1728void device_shutdown(void)
1729{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001730 struct device *dev, *devn;
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08001731
1732 list_for_each_entry_safe_reverse(dev, devn, &devices_kset->list,
1733 kobj.entry) {
1734 if (dev->bus && dev->bus->shutdown) {
1735 dev_dbg(dev, "shutdown\n");
1736 dev->bus->shutdown(dev);
1737 } else if (dev->driver && dev->driver->shutdown) {
1738 dev_dbg(dev, "shutdown\n");
1739 dev->driver->shutdown(dev);
1740 }
1741 }
Shaohua Li401097e2009-05-12 13:37:57 -07001742 async_synchronize_full();
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08001743}