blob: a31ea193fba04b7f6fe4258ee71ff8874834eec5 [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>
Dave Youngf75b1c62008-05-28 09:28:39 -070023#include <linux/mutex.h>
Shaohua Li401097e2009-05-12 13:37:57 -070024#include <linux/async.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
26#include "base.h"
27#include "power/power.h"
28
Andi Kleene52eec12010-09-08 16:54:17 +020029#ifdef CONFIG_SYSFS_DEPRECATED
30#ifdef CONFIG_SYSFS_DEPRECATED_V2
31long sysfs_deprecated = 1;
32#else
33long sysfs_deprecated = 0;
34#endif
35static __init int sysfs_deprecated_setup(char *arg)
36{
37 return strict_strtol(arg, 10, &sysfs_deprecated);
38}
39early_param("sysfs.deprecated", sysfs_deprecated_setup);
40#endif
41
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -080042int (*platform_notify)(struct device *dev) = NULL;
43int (*platform_notify_remove)(struct device *dev) = NULL;
Dan Williamse105b8b2008-04-21 10:51:07 -070044static struct kobject *dev_kobj;
45struct kobject *sysfs_dev_char_kobj;
46struct kobject *sysfs_dev_block_kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Greg Kroah-Hartman4e886c22008-01-27 12:12:43 -080048#ifdef CONFIG_BLOCK
49static inline int device_is_not_partition(struct device *dev)
50{
51 return !(dev->type == &part_type);
52}
53#else
54static inline int device_is_not_partition(struct device *dev)
55{
56 return 1;
57}
58#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
Alan Stern3e956372006-06-16 17:10:48 -040060/**
61 * dev_driver_string - Return a device's driver name, if at all possible
62 * @dev: struct device to get the name of
63 *
64 * Will return the device's driver's name if it is bound to a device. If
65 * the device is not bound to a device, it will return the name of the bus
66 * it is attached to. If it is not attached to a bus either, an empty
67 * string will be returned.
68 */
Jean Delvarebf9ca692008-07-30 12:29:21 -070069const char *dev_driver_string(const struct device *dev)
Alan Stern3e956372006-06-16 17:10:48 -040070{
Alan Stern35899722009-12-04 11:06:57 -050071 struct device_driver *drv;
72
73 /* dev->driver can change to NULL underneath us because of unbinding,
74 * so be careful about accessing it. dev->bus and dev->class should
75 * never change once they are set, so they don't need special care.
76 */
77 drv = ACCESS_ONCE(dev->driver);
78 return drv ? drv->name :
Jean Delvarea456b702007-03-09 16:33:10 +010079 (dev->bus ? dev->bus->name :
80 (dev->class ? dev->class->name : ""));
Alan Stern3e956372006-06-16 17:10:48 -040081}
Matthew Wilcox310a9222006-09-23 23:35:04 -060082EXPORT_SYMBOL(dev_driver_string);
Alan Stern3e956372006-06-16 17:10:48 -040083
Linus Torvalds1da177e2005-04-16 15:20:36 -070084#define to_dev(obj) container_of(obj, struct device, kobj)
85#define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr)
86
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -080087static ssize_t dev_attr_show(struct kobject *kobj, struct attribute *attr,
88 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070089{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -080090 struct device_attribute *dev_attr = to_dev_attr(attr);
91 struct device *dev = to_dev(kobj);
Dmitry Torokhov4a0c20b2005-04-29 01:23:47 -050092 ssize_t ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
94 if (dev_attr->show)
Yani Ioannou54b6f352005-05-17 06:39:34 -040095 ret = dev_attr->show(dev, dev_attr, buf);
Andrew Morton815d2d52008-03-04 15:09:07 -080096 if (ret >= (ssize_t)PAGE_SIZE) {
97 print_symbol("dev_attr_show: %s returned bad count\n",
98 (unsigned long)dev_attr->show);
99 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 return ret;
101}
102
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800103static ssize_t dev_attr_store(struct kobject *kobj, struct attribute *attr,
104 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800106 struct device_attribute *dev_attr = to_dev_attr(attr);
107 struct device *dev = to_dev(kobj);
Dmitry Torokhov4a0c20b2005-04-29 01:23:47 -0500108 ssize_t ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
110 if (dev_attr->store)
Yani Ioannou54b6f352005-05-17 06:39:34 -0400111 ret = dev_attr->store(dev, dev_attr, buf, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 return ret;
113}
114
Emese Revfy52cf25d2010-01-19 02:58:23 +0100115static const struct sysfs_ops dev_sysfs_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 .show = dev_attr_show,
117 .store = dev_attr_store,
118};
119
Kay Sieversca22e562011-12-14 14:29:38 -0800120#define to_ext_attr(x) container_of(x, struct dev_ext_attribute, attr)
121
122ssize_t device_store_ulong(struct device *dev,
123 struct device_attribute *attr,
124 const char *buf, size_t size)
125{
126 struct dev_ext_attribute *ea = to_ext_attr(attr);
127 char *end;
128 unsigned long new = simple_strtoul(buf, &end, 0);
129 if (end == buf)
130 return -EINVAL;
131 *(unsigned long *)(ea->var) = new;
132 /* Always return full write size even if we didn't consume all */
133 return size;
134}
135EXPORT_SYMBOL_GPL(device_store_ulong);
136
137ssize_t device_show_ulong(struct device *dev,
138 struct device_attribute *attr,
139 char *buf)
140{
141 struct dev_ext_attribute *ea = to_ext_attr(attr);
142 return snprintf(buf, PAGE_SIZE, "%lx\n", *(unsigned long *)(ea->var));
143}
144EXPORT_SYMBOL_GPL(device_show_ulong);
145
146ssize_t device_store_int(struct device *dev,
147 struct device_attribute *attr,
148 const char *buf, size_t size)
149{
150 struct dev_ext_attribute *ea = to_ext_attr(attr);
151 char *end;
152 long new = simple_strtol(buf, &end, 0);
153 if (end == buf || new > INT_MAX || new < INT_MIN)
154 return -EINVAL;
155 *(int *)(ea->var) = new;
156 /* Always return full write size even if we didn't consume all */
157 return size;
158}
159EXPORT_SYMBOL_GPL(device_store_int);
160
161ssize_t device_show_int(struct device *dev,
162 struct device_attribute *attr,
163 char *buf)
164{
165 struct dev_ext_attribute *ea = to_ext_attr(attr);
166
167 return snprintf(buf, PAGE_SIZE, "%d\n", *(int *)(ea->var));
168}
169EXPORT_SYMBOL_GPL(device_show_int);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
171/**
172 * device_release - free device structure.
173 * @kobj: device's kobject.
174 *
175 * This is called once the reference count for the object
176 * reaches 0. We forward the call to the device's release
177 * method, which should handle actually freeing the structure.
178 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800179static void device_release(struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800181 struct device *dev = to_dev(kobj);
Greg Kroah-Hartmanfb069a52008-12-16 12:23:36 -0800182 struct device_private *p = dev->p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
184 if (dev->release)
185 dev->release(dev);
Kay Sieversf9f852d2006-10-07 21:54:55 +0200186 else if (dev->type && dev->type->release)
187 dev->type->release(dev);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700188 else if (dev->class && dev->class->dev_release)
189 dev->class->dev_release(dev);
Arjan van de Venf810a5c2008-07-25 19:45:39 -0700190 else
191 WARN(1, KERN_ERR "Device '%s' does not have a release() "
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800192 "function, it is broken and must be fixed.\n",
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100193 dev_name(dev));
Greg Kroah-Hartmanfb069a52008-12-16 12:23:36 -0800194 kfree(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195}
196
Eric W. Biedermanbc451f22010-03-30 11:31:25 -0700197static const void *device_namespace(struct kobject *kobj)
198{
199 struct device *dev = to_dev(kobj);
200 const void *ns = NULL;
201
202 if (dev->class && dev->class->ns_type)
203 ns = dev->class->namespace(dev);
204
205 return ns;
206}
207
Greg Kroah-Hartman8f4afc42007-10-11 10:47:49 -0600208static struct kobj_type device_ktype = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 .release = device_release,
210 .sysfs_ops = &dev_sysfs_ops,
Eric W. Biedermanbc451f22010-03-30 11:31:25 -0700211 .namespace = device_namespace,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212};
213
214
Kay Sievers312c0042005-11-16 09:00:00 +0100215static int dev_uevent_filter(struct kset *kset, struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216{
217 struct kobj_type *ktype = get_ktype(kobj);
218
Greg Kroah-Hartman8f4afc42007-10-11 10:47:49 -0600219 if (ktype == &device_ktype) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 struct device *dev = to_dev(kobj);
221 if (dev->bus)
222 return 1;
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700223 if (dev->class)
224 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 }
226 return 0;
227}
228
Kay Sievers312c0042005-11-16 09:00:00 +0100229static const char *dev_uevent_name(struct kset *kset, struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230{
231 struct device *dev = to_dev(kobj);
232
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700233 if (dev->bus)
234 return dev->bus->name;
235 if (dev->class)
236 return dev->class->name;
237 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238}
239
Kay Sievers7eff2e72007-08-14 15:15:12 +0200240static int dev_uevent(struct kset *kset, struct kobject *kobj,
241 struct kobj_uevent_env *env)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242{
243 struct device *dev = to_dev(kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 int retval = 0;
245
Kay Sievers6fcf53a2009-04-30 15:23:42 +0200246 /* add device node properties if present */
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700247 if (MAJOR(dev->devt)) {
Kay Sievers6fcf53a2009-04-30 15:23:42 +0200248 const char *tmp;
249 const char *name;
Kay Sieverse454cea2009-09-18 23:01:12 +0200250 mode_t mode = 0;
Kay Sievers6fcf53a2009-04-30 15:23:42 +0200251
Kay Sievers7eff2e72007-08-14 15:15:12 +0200252 add_uevent_var(env, "MAJOR=%u", MAJOR(dev->devt));
253 add_uevent_var(env, "MINOR=%u", MINOR(dev->devt));
Kay Sieverse454cea2009-09-18 23:01:12 +0200254 name = device_get_devnode(dev, &mode, &tmp);
Kay Sievers6fcf53a2009-04-30 15:23:42 +0200255 if (name) {
256 add_uevent_var(env, "DEVNAME=%s", name);
257 kfree(tmp);
Kay Sieverse454cea2009-09-18 23:01:12 +0200258 if (mode)
259 add_uevent_var(env, "DEVMODE=%#o", mode & 0777);
Kay Sievers6fcf53a2009-04-30 15:23:42 +0200260 }
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700261 }
262
Kay Sievers414264f2007-03-12 21:08:57 +0100263 if (dev->type && dev->type->name)
Kay Sievers7eff2e72007-08-14 15:15:12 +0200264 add_uevent_var(env, "DEVTYPE=%s", dev->type->name);
Kay Sievers414264f2007-03-12 21:08:57 +0100265
Kay Sievers239378f2006-10-07 21:54:55 +0200266 if (dev->driver)
Kay Sievers7eff2e72007-08-14 15:15:12 +0200267 add_uevent_var(env, "DRIVER=%s", dev->driver->name);
Kay Sievers239378f2006-10-07 21:54:55 +0200268
Kay Sievers7eff2e72007-08-14 15:15:12 +0200269 /* have the bus specific function add its stuff */
Kay Sievers312c0042005-11-16 09:00:00 +0100270 if (dev->bus && dev->bus->uevent) {
Kay Sievers7eff2e72007-08-14 15:15:12 +0200271 retval = dev->bus->uevent(dev, env);
Kay Sieversf9f852d2006-10-07 21:54:55 +0200272 if (retval)
Greg Kroah-Hartman7dc72b22007-11-28 23:49:41 -0800273 pr_debug("device: '%s': %s: bus uevent() returned %d\n",
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100274 dev_name(dev), __func__, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 }
276
Kay Sievers7eff2e72007-08-14 15:15:12 +0200277 /* have the class specific function add its stuff */
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700278 if (dev->class && dev->class->dev_uevent) {
Kay Sievers7eff2e72007-08-14 15:15:12 +0200279 retval = dev->class->dev_uevent(dev, env);
Kay Sieversf9f852d2006-10-07 21:54:55 +0200280 if (retval)
Greg Kroah-Hartman7dc72b22007-11-28 23:49:41 -0800281 pr_debug("device: '%s': %s: class uevent() "
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100282 "returned %d\n", dev_name(dev),
Harvey Harrison2b3a3022008-03-04 16:41:05 -0800283 __func__, retval);
Kay Sieversf9f852d2006-10-07 21:54:55 +0200284 }
285
Stefan Weileef35c22010-08-06 21:11:15 +0200286 /* have the device type specific function add its stuff */
Kay Sieversf9f852d2006-10-07 21:54:55 +0200287 if (dev->type && dev->type->uevent) {
Kay Sievers7eff2e72007-08-14 15:15:12 +0200288 retval = dev->type->uevent(dev, env);
Kay Sieversf9f852d2006-10-07 21:54:55 +0200289 if (retval)
Greg Kroah-Hartman7dc72b22007-11-28 23:49:41 -0800290 pr_debug("device: '%s': %s: dev_type uevent() "
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100291 "returned %d\n", dev_name(dev),
Harvey Harrison2b3a3022008-03-04 16:41:05 -0800292 __func__, retval);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700293 }
294
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 return retval;
296}
297
Emese Revfy9cd43612009-12-31 14:52:51 +0100298static const struct kset_uevent_ops device_uevent_ops = {
Kay Sievers312c0042005-11-16 09:00:00 +0100299 .filter = dev_uevent_filter,
300 .name = dev_uevent_name,
301 .uevent = dev_uevent,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302};
303
Kay Sievers16574dc2007-04-06 01:40:38 +0200304static ssize_t show_uevent(struct device *dev, struct device_attribute *attr,
305 char *buf)
306{
307 struct kobject *top_kobj;
308 struct kset *kset;
Kay Sievers7eff2e72007-08-14 15:15:12 +0200309 struct kobj_uevent_env *env = NULL;
Kay Sievers16574dc2007-04-06 01:40:38 +0200310 int i;
311 size_t count = 0;
312 int retval;
313
314 /* search the kset, the device belongs to */
315 top_kobj = &dev->kobj;
Kay Sievers5c5daf62007-08-12 20:43:55 +0200316 while (!top_kobj->kset && top_kobj->parent)
317 top_kobj = top_kobj->parent;
Kay Sievers16574dc2007-04-06 01:40:38 +0200318 if (!top_kobj->kset)
319 goto out;
Kay Sievers5c5daf62007-08-12 20:43:55 +0200320
Kay Sievers16574dc2007-04-06 01:40:38 +0200321 kset = top_kobj->kset;
322 if (!kset->uevent_ops || !kset->uevent_ops->uevent)
323 goto out;
324
325 /* respect filter */
326 if (kset->uevent_ops && kset->uevent_ops->filter)
327 if (!kset->uevent_ops->filter(kset, &dev->kobj))
328 goto out;
329
Kay Sievers7eff2e72007-08-14 15:15:12 +0200330 env = kzalloc(sizeof(struct kobj_uevent_env), GFP_KERNEL);
331 if (!env)
Greg Kroah-Hartmanc7308c82007-05-02 14:14:11 +0200332 return -ENOMEM;
333
Kay Sievers16574dc2007-04-06 01:40:38 +0200334 /* let the kset specific function add its keys */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200335 retval = kset->uevent_ops->uevent(kset, &dev->kobj, env);
Kay Sievers16574dc2007-04-06 01:40:38 +0200336 if (retval)
337 goto out;
338
339 /* copy keys to file */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200340 for (i = 0; i < env->envp_idx; i++)
341 count += sprintf(&buf[count], "%s\n", env->envp[i]);
Kay Sievers16574dc2007-04-06 01:40:38 +0200342out:
Kay Sievers7eff2e72007-08-14 15:15:12 +0200343 kfree(env);
Kay Sievers16574dc2007-04-06 01:40:38 +0200344 return count;
345}
346
Kay Sieversa7fd6702005-10-01 14:49:43 +0200347static ssize_t store_uevent(struct device *dev, struct device_attribute *attr,
348 const char *buf, size_t count)
349{
Kay Sievers60a96a52007-07-08 22:29:26 +0200350 enum kobject_action action;
351
Kay Sievers3f5468c2010-01-14 22:54:37 +0100352 if (kobject_action_type(buf, count, &action) == 0)
Kay Sievers60a96a52007-07-08 22:29:26 +0200353 kobject_uevent(&dev->kobj, action);
Kay Sievers3f5468c2010-01-14 22:54:37 +0100354 else
355 dev_err(dev, "uevent: unknown action-string\n");
Kay Sieversa7fd6702005-10-01 14:49:43 +0200356 return count;
357}
358
Tejun Heoad6a1e12007-06-14 03:45:17 +0900359static struct device_attribute uevent_attr =
360 __ATTR(uevent, S_IRUGO | S_IWUSR, show_uevent, store_uevent);
361
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500362static int device_add_attributes(struct device *dev,
363 struct device_attribute *attrs)
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -0700364{
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -0700365 int error = 0;
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500366 int i;
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -0700367
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500368 if (attrs) {
369 for (i = 0; attr_name(attrs[i]); i++) {
370 error = device_create_file(dev, &attrs[i]);
371 if (error)
372 break;
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -0700373 }
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500374 if (error)
375 while (--i >= 0)
376 device_remove_file(dev, &attrs[i]);
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -0700377 }
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -0700378 return error;
379}
380
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500381static void device_remove_attributes(struct device *dev,
382 struct device_attribute *attrs)
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -0700383{
384 int i;
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500385
386 if (attrs)
387 for (i = 0; attr_name(attrs[i]); i++)
388 device_remove_file(dev, &attrs[i]);
389}
390
Stefan Achatzc97415a2010-11-26 19:57:29 +0000391static int device_add_bin_attributes(struct device *dev,
392 struct bin_attribute *attrs)
393{
394 int error = 0;
395 int i;
396
397 if (attrs) {
398 for (i = 0; attr_name(attrs[i]); i++) {
399 error = device_create_bin_file(dev, &attrs[i]);
400 if (error)
401 break;
402 }
403 if (error)
404 while (--i >= 0)
405 device_remove_bin_file(dev, &attrs[i]);
406 }
407 return error;
408}
409
410static void device_remove_bin_attributes(struct device *dev,
411 struct bin_attribute *attrs)
412{
413 int i;
414
415 if (attrs)
416 for (i = 0; attr_name(attrs[i]); i++)
417 device_remove_bin_file(dev, &attrs[i]);
418}
419
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500420static int device_add_groups(struct device *dev,
David Brownella4dbd672009-06-24 10:06:31 -0700421 const struct attribute_group **groups)
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500422{
423 int error = 0;
424 int i;
425
426 if (groups) {
427 for (i = 0; groups[i]; i++) {
428 error = sysfs_create_group(&dev->kobj, groups[i]);
429 if (error) {
430 while (--i >= 0)
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800431 sysfs_remove_group(&dev->kobj,
432 groups[i]);
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500433 break;
434 }
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -0700435 }
436 }
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500437 return error;
438}
439
440static void device_remove_groups(struct device *dev,
David Brownella4dbd672009-06-24 10:06:31 -0700441 const struct attribute_group **groups)
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500442{
443 int i;
444
445 if (groups)
446 for (i = 0; groups[i]; i++)
447 sysfs_remove_group(&dev->kobj, groups[i]);
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -0700448}
449
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700450static int device_add_attrs(struct device *dev)
451{
452 struct class *class = dev->class;
Stephen Hemmingeraed65af2011-03-28 09:12:52 -0700453 const struct device_type *type = dev->type;
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500454 int error;
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700455
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500456 if (class) {
457 error = device_add_attributes(dev, class->dev_attrs);
Kay Sieversf9f852d2006-10-07 21:54:55 +0200458 if (error)
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500459 return error;
Stefan Achatzc97415a2010-11-26 19:57:29 +0000460 error = device_add_bin_attributes(dev, class->dev_bin_attrs);
461 if (error)
462 goto err_remove_class_attrs;
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700463 }
Kay Sieversf9f852d2006-10-07 21:54:55 +0200464
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500465 if (type) {
466 error = device_add_groups(dev, type->groups);
Kay Sieversf9f852d2006-10-07 21:54:55 +0200467 if (error)
Stefan Achatzc97415a2010-11-26 19:57:29 +0000468 goto err_remove_class_bin_attrs;
Kay Sieversf9f852d2006-10-07 21:54:55 +0200469 }
470
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500471 error = device_add_groups(dev, dev->groups);
472 if (error)
473 goto err_remove_type_groups;
474
475 return 0;
476
477 err_remove_type_groups:
478 if (type)
479 device_remove_groups(dev, type->groups);
Stefan Achatzc97415a2010-11-26 19:57:29 +0000480 err_remove_class_bin_attrs:
481 if (class)
482 device_remove_bin_attributes(dev, class->dev_bin_attrs);
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500483 err_remove_class_attrs:
484 if (class)
485 device_remove_attributes(dev, class->dev_attrs);
486
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700487 return error;
488}
489
490static void device_remove_attrs(struct device *dev)
491{
492 struct class *class = dev->class;
Stephen Hemmingeraed65af2011-03-28 09:12:52 -0700493 const struct device_type *type = dev->type;
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700494
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500495 device_remove_groups(dev, dev->groups);
Kay Sieversf9f852d2006-10-07 21:54:55 +0200496
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500497 if (type)
498 device_remove_groups(dev, type->groups);
499
Stefan Achatzc97415a2010-11-26 19:57:29 +0000500 if (class) {
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500501 device_remove_attributes(dev, class->dev_attrs);
Stefan Achatzc97415a2010-11-26 19:57:29 +0000502 device_remove_bin_attributes(dev, class->dev_bin_attrs);
503 }
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700504}
505
506
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700507static ssize_t show_dev(struct device *dev, struct device_attribute *attr,
508 char *buf)
509{
510 return print_dev_t(buf, dev->devt);
511}
512
Tejun Heoad6a1e12007-06-14 03:45:17 +0900513static struct device_attribute devt_attr =
514 __ATTR(dev, S_IRUGO, show_dev, NULL);
515
Kay Sieversca22e562011-12-14 14:29:38 -0800516/* /sys/devices/ */
Greg Kroah-Hartman881c6cf2007-11-01 09:29:06 -0600517struct kset *devices_kset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800520 * device_create_file - create sysfs attribute file for device.
521 * @dev: device.
522 * @attr: device attribute descriptor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 */
Phil Carmody26579ab2009-12-18 15:34:19 +0200524int device_create_file(struct device *dev,
525 const struct device_attribute *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526{
527 int error = 0;
Cornelia Huck0c98b192008-01-31 10:39:38 +0100528 if (dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 error = sysfs_create_file(&dev->kobj, &attr->attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 return error;
531}
532
533/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800534 * device_remove_file - remove sysfs attribute file.
535 * @dev: device.
536 * @attr: device attribute descriptor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 */
Phil Carmody26579ab2009-12-18 15:34:19 +0200538void device_remove_file(struct device *dev,
539 const struct device_attribute *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540{
Cornelia Huck0c98b192008-01-31 10:39:38 +0100541 if (dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 sysfs_remove_file(&dev->kobj, &attr->attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543}
544
Greg Kroah-Hartman2589f1882006-09-19 09:39:19 -0700545/**
546 * device_create_bin_file - create sysfs binary attribute file for device.
547 * @dev: device.
548 * @attr: device binary attribute descriptor.
549 */
Phil Carmody66ecb922009-12-18 15:34:20 +0200550int device_create_bin_file(struct device *dev,
551 const struct bin_attribute *attr)
Greg Kroah-Hartman2589f1882006-09-19 09:39:19 -0700552{
553 int error = -EINVAL;
554 if (dev)
555 error = sysfs_create_bin_file(&dev->kobj, attr);
556 return error;
557}
558EXPORT_SYMBOL_GPL(device_create_bin_file);
559
560/**
561 * device_remove_bin_file - remove sysfs binary attribute file
562 * @dev: device.
563 * @attr: device binary attribute descriptor.
564 */
Phil Carmody66ecb922009-12-18 15:34:20 +0200565void device_remove_bin_file(struct device *dev,
566 const struct bin_attribute *attr)
Greg Kroah-Hartman2589f1882006-09-19 09:39:19 -0700567{
568 if (dev)
569 sysfs_remove_bin_file(&dev->kobj, attr);
570}
571EXPORT_SYMBOL_GPL(device_remove_bin_file);
572
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400573/**
Alan Stern523ded72007-04-26 00:12:04 -0700574 * device_schedule_callback_owner - helper to schedule a callback for a device
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400575 * @dev: device.
576 * @func: callback function to invoke later.
Alan Stern523ded72007-04-26 00:12:04 -0700577 * @owner: module owning the callback routine
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400578 *
579 * Attribute methods must not unregister themselves or their parent device
580 * (which would amount to the same thing). Attempts to do so will deadlock,
581 * since unregistration is mutually exclusive with driver callbacks.
582 *
583 * Instead methods can call this routine, which will attempt to allocate
584 * and schedule a workqueue request to call back @func with @dev as its
585 * argument in the workqueue's process context. @dev will be pinned until
586 * @func returns.
587 *
Alan Stern523ded72007-04-26 00:12:04 -0700588 * This routine is usually called via the inline device_schedule_callback(),
589 * which automatically sets @owner to THIS_MODULE.
590 *
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400591 * Returns 0 if the request was submitted, -ENOMEM if storage could not
Alan Stern523ded72007-04-26 00:12:04 -0700592 * be allocated, -ENODEV if a reference to @owner isn't available.
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400593 *
594 * NOTE: This routine won't work if CONFIG_SYSFS isn't set! It uses an
595 * underlying sysfs routine (since it is intended for use by attribute
596 * methods), and if sysfs isn't available you'll get nothing but -ENOSYS.
597 */
Alan Stern523ded72007-04-26 00:12:04 -0700598int device_schedule_callback_owner(struct device *dev,
599 void (*func)(struct device *), struct module *owner)
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400600{
601 return sysfs_schedule_callback(&dev->kobj,
Alan Stern523ded72007-04-26 00:12:04 -0700602 (void (*)(void *)) func, dev, owner);
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400603}
Alan Stern523ded72007-04-26 00:12:04 -0700604EXPORT_SYMBOL_GPL(device_schedule_callback_owner);
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400605
James Bottomley34bb61f2005-09-06 16:56:51 -0700606static void klist_children_get(struct klist_node *n)
607{
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -0800608 struct device_private *p = to_device_private_parent(n);
609 struct device *dev = p->device;
James Bottomley34bb61f2005-09-06 16:56:51 -0700610
611 get_device(dev);
612}
613
614static void klist_children_put(struct klist_node *n)
615{
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -0800616 struct device_private *p = to_device_private_parent(n);
617 struct device *dev = p->device;
James Bottomley34bb61f2005-09-06 16:56:51 -0700618
619 put_device(dev);
620}
621
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800623 * device_initialize - init device structure.
624 * @dev: device.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 *
Cornelia Huck57394112008-09-03 18:26:40 +0200626 * This prepares the device for use by other layers by initializing
627 * its fields.
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800628 * It is the first half of device_register(), if called by
Cornelia Huck57394112008-09-03 18:26:40 +0200629 * that function, though it can also be called separately, so one
630 * may use @dev's fields. In particular, get_device()/put_device()
631 * may be used for reference counting of @dev after calling this
632 * function.
633 *
634 * NOTE: Use put_device() to give up your reference instead of freeing
635 * @dev directly once you have called this function.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637void device_initialize(struct device *dev)
638{
Greg Kroah-Hartman881c6cf2007-11-01 09:29:06 -0600639 dev->kobj.kset = devices_kset;
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -0700640 kobject_init(&dev->kobj, &device_ktype);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 INIT_LIST_HEAD(&dev->dma_pools);
Thomas Gleixner31427882010-01-29 20:39:02 +0000642 mutex_init(&dev->mutex);
Peter Zijlstra1704f472010-03-19 01:37:42 +0100643 lockdep_set_novalidate_class(&dev->mutex);
Tejun Heo9ac78492007-01-20 16:00:26 +0900644 spin_lock_init(&dev->devres_lock);
645 INIT_LIST_HEAD(&dev->devres_head);
Alan Stern3b98aea2008-08-07 13:06:12 -0400646 device_pm_init(dev);
Christoph Hellwig87348132006-12-06 20:32:33 -0800647 set_dev_node(dev, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648}
649
Kay Sievers86406242007-03-14 03:25:56 +0100650static struct kobject *virtual_device_parent(struct device *dev)
Greg Kroah-Hartmanf0ee61a2006-10-23 10:40:54 -0700651{
Kay Sievers86406242007-03-14 03:25:56 +0100652 static struct kobject *virtual_dir = NULL;
Greg Kroah-Hartmanf0ee61a2006-10-23 10:40:54 -0700653
Kay Sievers86406242007-03-14 03:25:56 +0100654 if (!virtual_dir)
Greg Kroah-Hartman4ff6abf2007-11-05 22:24:43 -0800655 virtual_dir = kobject_create_and_add("virtual",
Greg Kroah-Hartman881c6cf2007-11-01 09:29:06 -0600656 &devices_kset->kobj);
Greg Kroah-Hartmanf0ee61a2006-10-23 10:40:54 -0700657
Kay Sievers86406242007-03-14 03:25:56 +0100658 return virtual_dir;
Greg Kroah-Hartmanf0ee61a2006-10-23 10:40:54 -0700659}
660
Eric W. Biedermanbc451f22010-03-30 11:31:25 -0700661struct class_dir {
662 struct kobject kobj;
663 struct class *class;
664};
665
666#define to_class_dir(obj) container_of(obj, struct class_dir, kobj)
667
668static void class_dir_release(struct kobject *kobj)
669{
670 struct class_dir *dir = to_class_dir(kobj);
671 kfree(dir);
672}
673
674static const
675struct kobj_ns_type_operations *class_dir_child_ns_type(struct kobject *kobj)
676{
677 struct class_dir *dir = to_class_dir(kobj);
678 return dir->class->ns_type;
679}
680
681static struct kobj_type class_dir_ktype = {
682 .release = class_dir_release,
683 .sysfs_ops = &kobj_sysfs_ops,
684 .child_ns_type = class_dir_child_ns_type
685};
686
687static struct kobject *
688class_dir_create_and_add(struct class *class, struct kobject *parent_kobj)
689{
690 struct class_dir *dir;
691 int retval;
692
693 dir = kzalloc(sizeof(*dir), GFP_KERNEL);
694 if (!dir)
695 return NULL;
696
697 dir->class = class;
698 kobject_init(&dir->kobj, &class_dir_ktype);
699
Kay Sievers6b6e39a2010-11-15 23:13:18 +0100700 dir->kobj.kset = &class->p->glue_dirs;
Eric W. Biedermanbc451f22010-03-30 11:31:25 -0700701
702 retval = kobject_add(&dir->kobj, parent_kobj, "%s", class->name);
703 if (retval < 0) {
704 kobject_put(&dir->kobj);
705 return NULL;
706 }
707 return &dir->kobj;
708}
709
710
Kay Sieversda231fd2007-11-21 17:29:15 +0100711static struct kobject *get_device_parent(struct device *dev,
712 struct device *parent)
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +0100713{
Kay Sievers86406242007-03-14 03:25:56 +0100714 if (dev->class) {
Tejun Heo77d3d7c2010-02-05 17:57:02 +0900715 static DEFINE_MUTEX(gdp_mutex);
Kay Sievers86406242007-03-14 03:25:56 +0100716 struct kobject *kobj = NULL;
717 struct kobject *parent_kobj;
718 struct kobject *k;
719
Randy Dunlapead454f2010-09-24 14:36:49 -0700720#ifdef CONFIG_BLOCK
Kay Sievers39aba962010-09-04 22:33:14 -0700721 /* block disks show up in /sys/block */
Andi Kleene52eec12010-09-08 16:54:17 +0200722 if (sysfs_deprecated && dev->class == &block_class) {
Kay Sievers39aba962010-09-04 22:33:14 -0700723 if (parent && parent->class == &block_class)
724 return &parent->kobj;
Kay Sievers6b6e39a2010-11-15 23:13:18 +0100725 return &block_class.p->subsys.kobj;
Kay Sievers39aba962010-09-04 22:33:14 -0700726 }
Randy Dunlapead454f2010-09-24 14:36:49 -0700727#endif
Andi Kleene52eec12010-09-08 16:54:17 +0200728
Kay Sievers86406242007-03-14 03:25:56 +0100729 /*
730 * If we have no parent, we live in "virtual".
Kay Sievers0f4dafc2007-12-19 01:40:42 +0100731 * Class-devices with a non class-device as parent, live
732 * in a "glue" directory to prevent namespace collisions.
Kay Sievers86406242007-03-14 03:25:56 +0100733 */
734 if (parent == NULL)
735 parent_kobj = virtual_device_parent(dev);
Eric W. Biederman24b14422010-07-24 22:43:35 -0700736 else if (parent->class && !dev->class->ns_type)
Kay Sievers86406242007-03-14 03:25:56 +0100737 return &parent->kobj;
738 else
739 parent_kobj = &parent->kobj;
740
Tejun Heo77d3d7c2010-02-05 17:57:02 +0900741 mutex_lock(&gdp_mutex);
742
Kay Sievers86406242007-03-14 03:25:56 +0100743 /* find our class-directory at the parent and reference it */
Kay Sievers6b6e39a2010-11-15 23:13:18 +0100744 spin_lock(&dev->class->p->glue_dirs.list_lock);
745 list_for_each_entry(k, &dev->class->p->glue_dirs.list, entry)
Kay Sievers86406242007-03-14 03:25:56 +0100746 if (k->parent == parent_kobj) {
747 kobj = kobject_get(k);
748 break;
749 }
Kay Sievers6b6e39a2010-11-15 23:13:18 +0100750 spin_unlock(&dev->class->p->glue_dirs.list_lock);
Tejun Heo77d3d7c2010-02-05 17:57:02 +0900751 if (kobj) {
752 mutex_unlock(&gdp_mutex);
Kay Sievers86406242007-03-14 03:25:56 +0100753 return kobj;
Tejun Heo77d3d7c2010-02-05 17:57:02 +0900754 }
Kay Sievers86406242007-03-14 03:25:56 +0100755
756 /* or create a new class-directory at the parent device */
Eric W. Biedermanbc451f22010-03-30 11:31:25 -0700757 k = class_dir_create_and_add(dev->class, parent_kobj);
Kay Sievers0f4dafc2007-12-19 01:40:42 +0100758 /* do not emit an uevent for this simple "glue" directory */
Tejun Heo77d3d7c2010-02-05 17:57:02 +0900759 mutex_unlock(&gdp_mutex);
Greg Kroah-Hartman43968d22007-11-05 22:24:43 -0800760 return k;
Kay Sievers86406242007-03-14 03:25:56 +0100761 }
762
Kay Sieversca22e562011-12-14 14:29:38 -0800763 /* subsystems can specify a default root directory for their devices */
764 if (!parent && dev->bus && dev->bus->dev_root)
765 return &dev->bus->dev_root->kobj;
766
Kay Sievers86406242007-03-14 03:25:56 +0100767 if (parent)
Cornelia Huckc744aea2007-01-08 20:16:44 +0100768 return &parent->kobj;
769 return NULL;
770}
Kay Sieversda231fd2007-11-21 17:29:15 +0100771
Cornelia Huck63b69712008-01-21 16:09:44 +0100772static void cleanup_glue_dir(struct device *dev, struct kobject *glue_dir)
Kay Sieversda231fd2007-11-21 17:29:15 +0100773{
Kay Sievers0f4dafc2007-12-19 01:40:42 +0100774 /* see if we live in a "glue" directory */
Cornelia Huckc1fe5392008-02-27 15:38:23 +0100775 if (!glue_dir || !dev->class ||
Kay Sievers6b6e39a2010-11-15 23:13:18 +0100776 glue_dir->kset != &dev->class->p->glue_dirs)
Kay Sieversda231fd2007-11-21 17:29:15 +0100777 return;
778
Kay Sievers0f4dafc2007-12-19 01:40:42 +0100779 kobject_put(glue_dir);
Kay Sieversda231fd2007-11-21 17:29:15 +0100780}
Cornelia Huck63b69712008-01-21 16:09:44 +0100781
782static void cleanup_device_parent(struct device *dev)
783{
784 cleanup_glue_dir(dev, dev->kobj.parent);
785}
Kay Sievers86406242007-03-14 03:25:56 +0100786
Cornelia Huck2ee97caf2007-07-18 01:43:47 -0700787static int device_add_class_symlinks(struct device *dev)
788{
789 int error;
790
791 if (!dev->class)
792 return 0;
Kay Sieversda231fd2007-11-21 17:29:15 +0100793
Greg Kroah-Hartman1fbfee62008-05-28 09:28:39 -0700794 error = sysfs_create_link(&dev->kobj,
Kay Sievers6b6e39a2010-11-15 23:13:18 +0100795 &dev->class->p->subsys.kobj,
Cornelia Huck2ee97caf2007-07-18 01:43:47 -0700796 "subsystem");
797 if (error)
798 goto out;
Kay Sieversda231fd2007-11-21 17:29:15 +0100799
Greg Kroah-Hartman4e886c22008-01-27 12:12:43 -0800800 if (dev->parent && device_is_not_partition(dev)) {
Dmitry Torokhov4f01a752007-09-18 22:46:50 -0700801 error = sysfs_create_link(&dev->kobj, &dev->parent->kobj,
802 "device");
803 if (error)
Kay Sievers39aba962010-09-04 22:33:14 -0700804 goto out_subsys;
Cornelia Huck2ee97caf2007-07-18 01:43:47 -0700805 }
Kay Sievers39aba962010-09-04 22:33:14 -0700806
Randy Dunlapead454f2010-09-24 14:36:49 -0700807#ifdef CONFIG_BLOCK
Kay Sievers39aba962010-09-04 22:33:14 -0700808 /* /sys/block has directories and does not need symlinks */
Andi Kleene52eec12010-09-08 16:54:17 +0200809 if (sysfs_deprecated && dev->class == &block_class)
Kay Sievers39aba962010-09-04 22:33:14 -0700810 return 0;
Randy Dunlapead454f2010-09-24 14:36:49 -0700811#endif
Kay Sievers39aba962010-09-04 22:33:14 -0700812
813 /* link in the class directory pointing to the device */
Kay Sievers6b6e39a2010-11-15 23:13:18 +0100814 error = sysfs_create_link(&dev->class->p->subsys.kobj,
Kay Sievers39aba962010-09-04 22:33:14 -0700815 &dev->kobj, dev_name(dev));
816 if (error)
817 goto out_device;
818
Cornelia Huck2ee97caf2007-07-18 01:43:47 -0700819 return 0;
820
Kay Sievers39aba962010-09-04 22:33:14 -0700821out_device:
822 sysfs_remove_link(&dev->kobj, "device");
Kay Sieversda231fd2007-11-21 17:29:15 +0100823
Cornelia Huck2ee97caf2007-07-18 01:43:47 -0700824out_subsys:
825 sysfs_remove_link(&dev->kobj, "subsystem");
826out:
827 return error;
828}
829
830static void device_remove_class_symlinks(struct device *dev)
831{
832 if (!dev->class)
833 return;
Kay Sieversda231fd2007-11-21 17:29:15 +0100834
Greg Kroah-Hartman4e886c22008-01-27 12:12:43 -0800835 if (dev->parent && device_is_not_partition(dev))
Kay Sieversda231fd2007-11-21 17:29:15 +0100836 sysfs_remove_link(&dev->kobj, "device");
Cornelia Huck2ee97caf2007-07-18 01:43:47 -0700837 sysfs_remove_link(&dev->kobj, "subsystem");
Randy Dunlapead454f2010-09-24 14:36:49 -0700838#ifdef CONFIG_BLOCK
Andi Kleene52eec12010-09-08 16:54:17 +0200839 if (sysfs_deprecated && dev->class == &block_class)
Kay Sievers39aba962010-09-04 22:33:14 -0700840 return;
Randy Dunlapead454f2010-09-24 14:36:49 -0700841#endif
Kay Sievers6b6e39a2010-11-15 23:13:18 +0100842 sysfs_delete_link(&dev->class->p->subsys.kobj, &dev->kobj, dev_name(dev));
Cornelia Huck2ee97caf2007-07-18 01:43:47 -0700843}
844
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845/**
Stephen Rothwell413c2392008-05-30 10:16:40 +1000846 * dev_set_name - set a device name
847 * @dev: device
Randy Dunlap46232362008-06-04 21:40:43 -0700848 * @fmt: format string for the device's name
Stephen Rothwell413c2392008-05-30 10:16:40 +1000849 */
850int dev_set_name(struct device *dev, const char *fmt, ...)
851{
852 va_list vargs;
Kay Sievers1fa5ae82009-01-25 15:17:37 +0100853 int err;
Stephen Rothwell413c2392008-05-30 10:16:40 +1000854
855 va_start(vargs, fmt);
Kay Sievers1fa5ae82009-01-25 15:17:37 +0100856 err = kobject_set_name_vargs(&dev->kobj, fmt, vargs);
Stephen Rothwell413c2392008-05-30 10:16:40 +1000857 va_end(vargs);
Kay Sievers1fa5ae82009-01-25 15:17:37 +0100858 return err;
Stephen Rothwell413c2392008-05-30 10:16:40 +1000859}
860EXPORT_SYMBOL_GPL(dev_set_name);
861
862/**
Dan Williamse105b8b2008-04-21 10:51:07 -0700863 * device_to_dev_kobj - select a /sys/dev/ directory for the device
864 * @dev: device
865 *
866 * By default we select char/ for new entries. Setting class->dev_obj
867 * to NULL prevents an entry from being created. class->dev_kobj must
868 * be set (or cleared) before any devices are registered to the class
869 * otherwise device_create_sys_dev_entry() and
870 * device_remove_sys_dev_entry() will disagree about the the presence
871 * of the link.
872 */
873static struct kobject *device_to_dev_kobj(struct device *dev)
874{
875 struct kobject *kobj;
876
877 if (dev->class)
878 kobj = dev->class->dev_kobj;
879 else
880 kobj = sysfs_dev_char_kobj;
881
882 return kobj;
883}
884
885static int device_create_sys_dev_entry(struct device *dev)
886{
887 struct kobject *kobj = device_to_dev_kobj(dev);
888 int error = 0;
889 char devt_str[15];
890
891 if (kobj) {
892 format_dev_t(devt_str, dev->devt);
893 error = sysfs_create_link(kobj, &dev->kobj, devt_str);
894 }
895
896 return error;
897}
898
899static void device_remove_sys_dev_entry(struct device *dev)
900{
901 struct kobject *kobj = device_to_dev_kobj(dev);
902 char devt_str[15];
903
904 if (kobj) {
905 format_dev_t(devt_str, dev->devt);
906 sysfs_remove_link(kobj, devt_str);
907 }
908}
909
Greg Kroah-Hartmanb4028432009-05-11 14:16:57 -0700910int device_private_init(struct device *dev)
911{
912 dev->p = kzalloc(sizeof(*dev->p), GFP_KERNEL);
913 if (!dev->p)
914 return -ENOMEM;
915 dev->p->device = dev;
916 klist_init(&dev->p->klist_children, klist_children_get,
917 klist_children_put);
918 return 0;
919}
920
Dan Williamse105b8b2008-04-21 10:51:07 -0700921/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800922 * device_add - add device to device hierarchy.
923 * @dev: device.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800925 * This is part 2 of device_register(), though may be called
926 * separately _iff_ device_initialize() has been called separately.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 *
Cornelia Huck57394112008-09-03 18:26:40 +0200928 * This adds @dev to the kobject hierarchy via kobject_add(), adds it
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800929 * to the global and sibling lists for the device, then
930 * adds it to the other relevant subsystems of the driver model.
Cornelia Huck57394112008-09-03 18:26:40 +0200931 *
932 * NOTE: _Never_ directly free @dev after calling this function, even
933 * if it returned an error! Always use put_device() to give up your
934 * reference instead.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 */
936int device_add(struct device *dev)
937{
938 struct device *parent = NULL;
Kay Sieversca22e562011-12-14 14:29:38 -0800939 struct kobject *kobj;
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +0200940 struct class_interface *class_intf;
Greg Kroah-Hartmanc906a482008-05-30 10:45:12 -0700941 int error = -EINVAL;
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100942
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 dev = get_device(dev);
Greg Kroah-Hartmanc906a482008-05-30 10:45:12 -0700944 if (!dev)
945 goto done;
946
Greg Kroah-Hartmanfb069a52008-12-16 12:23:36 -0800947 if (!dev->p) {
Greg Kroah-Hartmanb4028432009-05-11 14:16:57 -0700948 error = device_private_init(dev);
949 if (error)
950 goto done;
Greg Kroah-Hartmanfb069a52008-12-16 12:23:36 -0800951 }
Greg Kroah-Hartmanfb069a52008-12-16 12:23:36 -0800952
Kay Sievers1fa5ae82009-01-25 15:17:37 +0100953 /*
954 * for statically allocated devices, which should all be converted
955 * some day, we need to initialize the name. We prevent reading back
956 * the name, and force the use of dev_name()
957 */
958 if (dev->init_name) {
Greg Kroah-Hartmanacc0e902009-06-02 15:39:55 -0700959 dev_set_name(dev, "%s", dev->init_name);
Kay Sievers1fa5ae82009-01-25 15:17:37 +0100960 dev->init_name = NULL;
961 }
Greg Kroah-Hartmanc906a482008-05-30 10:45:12 -0700962
Kay Sieversca22e562011-12-14 14:29:38 -0800963 /* subsystems can specify simple device enumeration */
964 if (!dev_name(dev) && dev->bus && dev->bus->dev_name)
965 dev_set_name(dev, "%s%u", dev->bus->dev_name, dev->id);
966
Thomas Gleixnere6309e72009-12-10 19:32:49 +0000967 if (!dev_name(dev)) {
968 error = -EINVAL;
Kay Sievers5c8563d2009-05-28 14:24:07 -0700969 goto name_error;
Thomas Gleixnere6309e72009-12-10 19:32:49 +0000970 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100972 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
Greg Kroah-Hartmanc205ef42006-08-07 22:19:37 -0700973
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 parent = get_device(dev->parent);
Kay Sieversca22e562011-12-14 14:29:38 -0800975 kobj = get_device_parent(dev, parent);
976 if (kobj)
977 dev->kobj.parent = kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978
Yinghai Lu0d358f22008-02-19 03:20:41 -0800979 /* use parent numa_node */
980 if (parent)
981 set_dev_node(dev, dev_to_node(parent));
982
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 /* first, register with generic layer. */
Kay Sievers8a577ff2009-04-18 15:05:45 -0700984 /* we require the name to be set before, and pass NULL */
985 error = kobject_add(&dev->kobj, dev->kobj.parent, NULL);
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +0100986 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 goto Error;
Kay Sieversa7fd6702005-10-01 14:49:43 +0200988
Brian Walsh37022642006-08-14 22:43:19 -0700989 /* notify platform of device entry */
990 if (platform_notify)
991 platform_notify(dev);
992
Tejun Heoad6a1e12007-06-14 03:45:17 +0900993 error = device_create_file(dev, &uevent_attr);
Cornelia Hucka306eea2006-09-22 11:37:13 +0200994 if (error)
995 goto attrError;
Kay Sieversa7fd6702005-10-01 14:49:43 +0200996
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700997 if (MAJOR(dev->devt)) {
Tejun Heoad6a1e12007-06-14 03:45:17 +0900998 error = device_create_file(dev, &devt_attr);
999 if (error)
Cornelia Hucka306eea2006-09-22 11:37:13 +02001000 goto ueventattrError;
Dan Williamse105b8b2008-04-21 10:51:07 -07001001
1002 error = device_create_sys_dev_entry(dev);
1003 if (error)
1004 goto devtattrError;
Kay Sievers2b2af542009-04-30 15:23:42 +02001005
1006 devtmpfs_create_node(dev);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001007 }
1008
Cornelia Huck2ee97caf2007-07-18 01:43:47 -07001009 error = device_add_class_symlinks(dev);
1010 if (error)
1011 goto SymlinkError;
Cornelia Huckdc0afa82007-07-09 11:39:18 -07001012 error = device_add_attrs(dev);
1013 if (error)
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07001014 goto AttrsError;
Cornelia Huckdc0afa82007-07-09 11:39:18 -07001015 error = bus_add_device(dev);
1016 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 goto BusError;
Alan Stern3b98aea2008-08-07 13:06:12 -04001018 error = dpm_sysfs_add(dev);
Rafael J. Wysocki57eee3d2008-03-12 00:59:38 +01001019 if (error)
Alan Stern3b98aea2008-08-07 13:06:12 -04001020 goto DPMError;
1021 device_pm_add(dev);
Alan Sternec0676ee2008-12-05 14:10:31 -05001022
1023 /* Notify clients of device addition. This call must come
1024 * after dpm_sysf_add() and before kobject_uevent().
1025 */
1026 if (dev->bus)
1027 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
1028 BUS_NOTIFY_ADD_DEVICE, dev);
1029
Cornelia Huck83b5fb4c2007-03-29 11:12:11 +02001030 kobject_uevent(&dev->kobj, KOBJ_ADD);
Alan Stern2023c612009-07-30 15:27:18 -04001031 bus_probe_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 if (parent)
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08001033 klist_add_tail(&dev->p->knode_parent,
1034 &parent->p->klist_children);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035
Greg Kroah-Hartman5d9fd162006-06-22 17:17:32 -07001036 if (dev->class) {
Kay Sieversca22e562011-12-14 14:29:38 -08001037 mutex_lock(&dev->class->p->mutex);
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02001038 /* tie the class to the device */
Tejun Heo5a3ceb82008-08-25 19:50:19 +02001039 klist_add_tail(&dev->knode_class,
Kay Sievers6b6e39a2010-11-15 23:13:18 +01001040 &dev->class->p->klist_devices);
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02001041
1042 /* notify any interfaces that the device is here */
Greg Kroah-Hartman184f1f72008-05-28 09:28:39 -07001043 list_for_each_entry(class_intf,
Kay Sieversca22e562011-12-14 14:29:38 -08001044 &dev->class->p->interfaces, node)
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02001045 if (class_intf->add_dev)
1046 class_intf->add_dev(dev, class_intf);
Kay Sieversca22e562011-12-14 14:29:38 -08001047 mutex_unlock(&dev->class->p->mutex);
Greg Kroah-Hartman5d9fd162006-06-22 17:17:32 -07001048 }
Greg Kroah-Hartmanc906a482008-05-30 10:45:12 -07001049done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 put_device(dev);
1051 return error;
Alan Stern3b98aea2008-08-07 13:06:12 -04001052 DPMError:
Rafael J. Wysocki57eee3d2008-03-12 00:59:38 +01001053 bus_remove_device(dev);
1054 BusError:
James Simmons82f0cf92007-02-21 17:44:51 +00001055 device_remove_attrs(dev);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07001056 AttrsError:
Cornelia Huck2ee97caf2007-07-18 01:43:47 -07001057 device_remove_class_symlinks(dev);
1058 SymlinkError:
Tejun Heoad6a1e12007-06-14 03:45:17 +09001059 if (MAJOR(dev->devt))
Kay Sieversad729562009-10-28 19:51:37 +01001060 devtmpfs_delete_node(dev);
1061 if (MAJOR(dev->devt))
Dan Williamse105b8b2008-04-21 10:51:07 -07001062 device_remove_sys_dev_entry(dev);
1063 devtattrError:
1064 if (MAJOR(dev->devt))
Tejun Heoad6a1e12007-06-14 03:45:17 +09001065 device_remove_file(dev, &devt_attr);
Cornelia Hucka306eea2006-09-22 11:37:13 +02001066 ueventattrError:
Tejun Heoad6a1e12007-06-14 03:45:17 +09001067 device_remove_file(dev, &uevent_attr);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001068 attrError:
Kay Sievers312c0042005-11-16 09:00:00 +01001069 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 kobject_del(&dev->kobj);
1071 Error:
Cornelia Huck63b69712008-01-21 16:09:44 +01001072 cleanup_device_parent(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 if (parent)
1074 put_device(parent);
Kay Sievers5c8563d2009-05-28 14:24:07 -07001075name_error:
1076 kfree(dev->p);
1077 dev->p = NULL;
Greg Kroah-Hartmanc906a482008-05-30 10:45:12 -07001078 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079}
1080
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001082 * device_register - register a device with the system.
1083 * @dev: pointer to the device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001085 * This happens in two clean steps - initialize the device
1086 * and add it to the system. The two steps can be called
1087 * separately, but this is the easiest and most common.
1088 * I.e. you should only call the two helpers separately if
1089 * have a clearly defined need to use and refcount the device
1090 * before it is added to the hierarchy.
Cornelia Huck57394112008-09-03 18:26:40 +02001091 *
1092 * NOTE: _Never_ directly free @dev after calling this function, even
1093 * if it returned an error! Always use put_device() to give up the
1094 * reference initialized in this function instead.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096int device_register(struct device *dev)
1097{
1098 device_initialize(dev);
1099 return device_add(dev);
1100}
1101
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001103 * get_device - increment reference count for device.
1104 * @dev: device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001106 * This simply forwards the call to kobject_get(), though
1107 * we do take care to provide for the case that we get a NULL
1108 * pointer passed in.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001110struct device *get_device(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111{
1112 return dev ? to_dev(kobject_get(&dev->kobj)) : NULL;
1113}
1114
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001116 * put_device - decrement reference count.
1117 * @dev: device in question.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001119void put_device(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120{
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001121 /* might_sleep(); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 if (dev)
1123 kobject_put(&dev->kobj);
1124}
1125
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001127 * device_del - delete device from system.
1128 * @dev: device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001130 * This is the first part of the device unregistration
1131 * sequence. This removes the device from the lists we control
1132 * from here, has it removed from the other driver model
1133 * subsystems it was added to in device_add(), and removes it
1134 * from the kobject hierarchy.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001136 * NOTE: this should be called manually _iff_ device_add() was
1137 * also called manually.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001139void device_del(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001141 struct device *parent = dev->parent;
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02001142 struct class_interface *class_intf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143
Alan Sternec0676ee2008-12-05 14:10:31 -05001144 /* Notify clients of device removal. This call must come
1145 * before dpm_sysfs_remove().
1146 */
1147 if (dev->bus)
1148 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
1149 BUS_NOTIFY_DEL_DEVICE, dev);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001150 device_pm_remove(dev);
Alan Stern3b98aea2008-08-07 13:06:12 -04001151 dpm_sysfs_remove(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 if (parent)
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08001153 klist_del(&dev->p->knode_parent);
Dan Williamse105b8b2008-04-21 10:51:07 -07001154 if (MAJOR(dev->devt)) {
Kay Sievers2b2af542009-04-30 15:23:42 +02001155 devtmpfs_delete_node(dev);
Dan Williamse105b8b2008-04-21 10:51:07 -07001156 device_remove_sys_dev_entry(dev);
Tejun Heoad6a1e12007-06-14 03:45:17 +09001157 device_remove_file(dev, &devt_attr);
Dan Williamse105b8b2008-04-21 10:51:07 -07001158 }
Kay Sieversb9d9c822006-06-15 15:31:56 +02001159 if (dev->class) {
Kay Sieversda231fd2007-11-21 17:29:15 +01001160 device_remove_class_symlinks(dev);
Kay Sievers99ef3ef2006-09-14 11:23:28 +02001161
Kay Sieversca22e562011-12-14 14:29:38 -08001162 mutex_lock(&dev->class->p->mutex);
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02001163 /* notify any interfaces that the device is now gone */
Greg Kroah-Hartman184f1f72008-05-28 09:28:39 -07001164 list_for_each_entry(class_intf,
Kay Sieversca22e562011-12-14 14:29:38 -08001165 &dev->class->p->interfaces, node)
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02001166 if (class_intf->remove_dev)
1167 class_intf->remove_dev(dev, class_intf);
1168 /* remove the device from the class list */
Tejun Heo5a3ceb82008-08-25 19:50:19 +02001169 klist_del(&dev->knode_class);
Kay Sieversca22e562011-12-14 14:29:38 -08001170 mutex_unlock(&dev->class->p->mutex);
Kay Sieversb9d9c822006-06-15 15:31:56 +02001171 }
Tejun Heoad6a1e12007-06-14 03:45:17 +09001172 device_remove_file(dev, &uevent_attr);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07001173 device_remove_attrs(dev);
Benjamin Herrenschmidt28953532006-11-08 19:46:14 -08001174 bus_remove_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175
Tejun Heo2f8d16a2007-03-09 19:34:19 +09001176 /*
1177 * Some platform devices are driven without driver attached
1178 * and managed resources may have been acquired. Make sure
1179 * all resources are released.
1180 */
1181 devres_release_all(dev);
1182
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 /* Notify the platform of the removal, in case they
1184 * need to do anything...
1185 */
1186 if (platform_notify_remove)
1187 platform_notify_remove(dev);
Kay Sievers312c0042005-11-16 09:00:00 +01001188 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
Kay Sieversda231fd2007-11-21 17:29:15 +01001189 cleanup_device_parent(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 kobject_del(&dev->kobj);
Kay Sieversda231fd2007-11-21 17:29:15 +01001191 put_device(parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192}
1193
1194/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001195 * device_unregister - unregister device from system.
1196 * @dev: device going away.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001198 * We do this in two parts, like we do device_register(). First,
1199 * we remove it from all the subsystems with device_del(), then
1200 * we decrement the reference count via put_device(). If that
1201 * is the final reference count, the device will be cleaned up
1202 * via device_release() above. Otherwise, the structure will
1203 * stick around until the final reference to the device is dropped.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001205void device_unregister(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206{
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01001207 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 device_del(dev);
1209 put_device(dev);
1210}
1211
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001212static struct device *next_device(struct klist_iter *i)
mochel@digitalimplant.org36239572005-03-24 19:08:30 -08001213{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001214 struct klist_node *n = klist_next(i);
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08001215 struct device *dev = NULL;
1216 struct device_private *p;
1217
1218 if (n) {
1219 p = to_device_private_parent(n);
1220 dev = p->device;
1221 }
1222 return dev;
mochel@digitalimplant.org36239572005-03-24 19:08:30 -08001223}
1224
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225/**
Kay Sieverse454cea2009-09-18 23:01:12 +02001226 * device_get_devnode - path of device node file
Kay Sievers6fcf53a2009-04-30 15:23:42 +02001227 * @dev: device
Kay Sieverse454cea2009-09-18 23:01:12 +02001228 * @mode: returned file access mode
Kay Sievers6fcf53a2009-04-30 15:23:42 +02001229 * @tmp: possibly allocated string
1230 *
1231 * Return the relative path of a possible device node.
1232 * Non-default names may need to allocate a memory to compose
1233 * a name. This memory is returned in tmp and needs to be
1234 * freed by the caller.
1235 */
Kay Sieverse454cea2009-09-18 23:01:12 +02001236const char *device_get_devnode(struct device *dev,
1237 mode_t *mode, const char **tmp)
Kay Sievers6fcf53a2009-04-30 15:23:42 +02001238{
1239 char *s;
1240
1241 *tmp = NULL;
1242
1243 /* the device type may provide a specific name */
Kay Sieverse454cea2009-09-18 23:01:12 +02001244 if (dev->type && dev->type->devnode)
1245 *tmp = dev->type->devnode(dev, mode);
Kay Sievers6fcf53a2009-04-30 15:23:42 +02001246 if (*tmp)
1247 return *tmp;
1248
1249 /* the class may provide a specific name */
Kay Sieverse454cea2009-09-18 23:01:12 +02001250 if (dev->class && dev->class->devnode)
1251 *tmp = dev->class->devnode(dev, mode);
Kay Sievers6fcf53a2009-04-30 15:23:42 +02001252 if (*tmp)
1253 return *tmp;
1254
1255 /* return name without allocation, tmp == NULL */
1256 if (strchr(dev_name(dev), '!') == NULL)
1257 return dev_name(dev);
1258
1259 /* replace '!' in the name with '/' */
1260 *tmp = kstrdup(dev_name(dev), GFP_KERNEL);
1261 if (!*tmp)
1262 return NULL;
1263 while ((s = strchr(*tmp, '!')))
1264 s[0] = '/';
1265 return *tmp;
1266}
1267
1268/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001269 * device_for_each_child - device child iterator.
1270 * @parent: parent struct device.
1271 * @data: data for the callback.
1272 * @fn: function to be called for each device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001274 * Iterate over @parent's child devices, and call @fn for each,
1275 * passing it @data.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001277 * We check the return of @fn each time. If it returns anything
1278 * other than 0, we break out and return that value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001280int device_for_each_child(struct device *parent, void *data,
1281 int (*fn)(struct device *dev, void *data))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282{
mochel@digitalimplant.org36239572005-03-24 19:08:30 -08001283 struct klist_iter i;
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001284 struct device *child;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 int error = 0;
1286
Greg Kroah-Hartman014c90d2009-04-15 16:00:12 -07001287 if (!parent->p)
1288 return 0;
1289
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08001290 klist_iter_init(&parent->p->klist_children, &i);
mochel@digitalimplant.org36239572005-03-24 19:08:30 -08001291 while ((child = next_device(&i)) && !error)
1292 error = fn(child, data);
1293 klist_iter_exit(&i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 return error;
1295}
1296
Cornelia Huck5ab69982006-11-16 15:42:07 +01001297/**
1298 * device_find_child - device iterator for locating a particular device.
1299 * @parent: parent struct device
1300 * @data: Data to pass to match function
1301 * @match: Callback function to check device
1302 *
1303 * This is similar to the device_for_each_child() function above, but it
1304 * returns a reference to a device that is 'found' for later use, as
1305 * determined by the @match callback.
1306 *
1307 * The callback should return 0 if the device doesn't match and non-zero
1308 * if it does. If the callback returns non-zero and a reference to the
1309 * current device can be obtained, this function will return to the caller
1310 * and not iterate over any more devices.
1311 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001312struct device *device_find_child(struct device *parent, void *data,
1313 int (*match)(struct device *dev, void *data))
Cornelia Huck5ab69982006-11-16 15:42:07 +01001314{
1315 struct klist_iter i;
1316 struct device *child;
1317
1318 if (!parent)
1319 return NULL;
1320
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08001321 klist_iter_init(&parent->p->klist_children, &i);
Cornelia Huck5ab69982006-11-16 15:42:07 +01001322 while ((child = next_device(&i)))
1323 if (match(child, data) && get_device(child))
1324 break;
1325 klist_iter_exit(&i);
1326 return child;
1327}
1328
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329int __init devices_init(void)
1330{
Greg Kroah-Hartman881c6cf2007-11-01 09:29:06 -06001331 devices_kset = kset_create_and_add("devices", &device_uevent_ops, NULL);
1332 if (!devices_kset)
1333 return -ENOMEM;
Dan Williamse105b8b2008-04-21 10:51:07 -07001334 dev_kobj = kobject_create_and_add("dev", NULL);
1335 if (!dev_kobj)
1336 goto dev_kobj_err;
1337 sysfs_dev_block_kobj = kobject_create_and_add("block", dev_kobj);
1338 if (!sysfs_dev_block_kobj)
1339 goto block_kobj_err;
1340 sysfs_dev_char_kobj = kobject_create_and_add("char", dev_kobj);
1341 if (!sysfs_dev_char_kobj)
1342 goto char_kobj_err;
1343
Greg Kroah-Hartman881c6cf2007-11-01 09:29:06 -06001344 return 0;
Dan Williamse105b8b2008-04-21 10:51:07 -07001345
1346 char_kobj_err:
1347 kobject_put(sysfs_dev_block_kobj);
1348 block_kobj_err:
1349 kobject_put(dev_kobj);
1350 dev_kobj_err:
1351 kset_unregister(devices_kset);
1352 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353}
1354
1355EXPORT_SYMBOL_GPL(device_for_each_child);
Cornelia Huck5ab69982006-11-16 15:42:07 +01001356EXPORT_SYMBOL_GPL(device_find_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357
1358EXPORT_SYMBOL_GPL(device_initialize);
1359EXPORT_SYMBOL_GPL(device_add);
1360EXPORT_SYMBOL_GPL(device_register);
1361
1362EXPORT_SYMBOL_GPL(device_del);
1363EXPORT_SYMBOL_GPL(device_unregister);
1364EXPORT_SYMBOL_GPL(get_device);
1365EXPORT_SYMBOL_GPL(put_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366
1367EXPORT_SYMBOL_GPL(device_create_file);
1368EXPORT_SYMBOL_GPL(device_remove_file);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001369
Karthigan Srinivasan7f100d12011-04-18 16:16:52 -05001370struct root_device {
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00001371 struct device dev;
1372 struct module *owner;
1373};
1374
Ferenc Wagner481e2072011-01-07 15:17:47 +01001375inline struct root_device *to_root_device(struct device *d)
1376{
1377 return container_of(d, struct root_device, dev);
1378}
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00001379
1380static void root_device_release(struct device *dev)
1381{
1382 kfree(to_root_device(dev));
1383}
1384
1385/**
1386 * __root_device_register - allocate and register a root device
1387 * @name: root device name
1388 * @owner: owner module of the root device, usually THIS_MODULE
1389 *
1390 * This function allocates a root device and registers it
1391 * using device_register(). In order to free the returned
1392 * device, use root_device_unregister().
1393 *
1394 * Root devices are dummy devices which allow other devices
1395 * to be grouped under /sys/devices. Use this function to
1396 * allocate a root device and then use it as the parent of
1397 * any device which should appear under /sys/devices/{name}
1398 *
1399 * The /sys/devices/{name} directory will also contain a
1400 * 'module' symlink which points to the @owner directory
1401 * in sysfs.
1402 *
Jani Nikulaf0eae0e2010-03-11 18:11:45 +02001403 * Returns &struct device pointer on success, or ERR_PTR() on error.
1404 *
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00001405 * Note: You probably want to use root_device_register().
1406 */
1407struct device *__root_device_register(const char *name, struct module *owner)
1408{
1409 struct root_device *root;
1410 int err = -ENOMEM;
1411
1412 root = kzalloc(sizeof(struct root_device), GFP_KERNEL);
1413 if (!root)
1414 return ERR_PTR(err);
1415
Greg Kroah-Hartmanacc0e902009-06-02 15:39:55 -07001416 err = dev_set_name(&root->dev, "%s", name);
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00001417 if (err) {
1418 kfree(root);
1419 return ERR_PTR(err);
1420 }
1421
1422 root->dev.release = root_device_release;
1423
1424 err = device_register(&root->dev);
1425 if (err) {
1426 put_device(&root->dev);
1427 return ERR_PTR(err);
1428 }
1429
Christoph Egger1d9e8822010-05-17 16:57:58 +02001430#ifdef CONFIG_MODULES /* gotta find a "cleaner" way to do this */
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00001431 if (owner) {
1432 struct module_kobject *mk = &owner->mkobj;
1433
1434 err = sysfs_create_link(&root->dev.kobj, &mk->kobj, "module");
1435 if (err) {
1436 device_unregister(&root->dev);
1437 return ERR_PTR(err);
1438 }
1439 root->owner = owner;
1440 }
1441#endif
1442
1443 return &root->dev;
1444}
1445EXPORT_SYMBOL_GPL(__root_device_register);
1446
1447/**
1448 * root_device_unregister - unregister and free a root device
Randy Dunlap7cbcf222009-01-20 16:29:13 -08001449 * @dev: device going away
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00001450 *
1451 * This function unregisters and cleans up a device that was created by
1452 * root_device_register().
1453 */
1454void root_device_unregister(struct device *dev)
1455{
1456 struct root_device *root = to_root_device(dev);
1457
1458 if (root->owner)
1459 sysfs_remove_link(&root->dev.kobj, "module");
1460
1461 device_unregister(dev);
1462}
1463EXPORT_SYMBOL_GPL(root_device_unregister);
1464
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001465
1466static void device_create_release(struct device *dev)
1467{
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01001468 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001469 kfree(dev);
1470}
1471
1472/**
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07001473 * device_create_vargs - creates a device and registers it with sysfs
1474 * @class: pointer to the struct class that this device should be registered to
1475 * @parent: pointer to the parent struct device of this new device, if any
1476 * @devt: the dev_t for the char device to be added
1477 * @drvdata: the data to be added to the device for callbacks
1478 * @fmt: string for the device's name
1479 * @args: va_list for the device's name
1480 *
1481 * This function can be used by char device classes. A struct device
1482 * will be created in sysfs, registered to the specified class.
1483 *
1484 * A "dev" file will be created, showing the dev_t for the device, if
1485 * the dev_t is not 0,0.
1486 * If a pointer to a parent struct device is passed in, the newly created
1487 * struct device will be a child of that device in sysfs.
1488 * The pointer to the struct device will be returned from the call.
1489 * Any further sysfs files that might be required can be created using this
1490 * pointer.
1491 *
Jani Nikulaf0eae0e2010-03-11 18:11:45 +02001492 * Returns &struct device pointer on success, or ERR_PTR() on error.
1493 *
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07001494 * Note: the struct class passed to this function must have previously
1495 * been created with a call to class_create().
1496 */
1497struct device *device_create_vargs(struct class *class, struct device *parent,
1498 dev_t devt, void *drvdata, const char *fmt,
1499 va_list args)
1500{
1501 struct device *dev = NULL;
1502 int retval = -ENODEV;
1503
1504 if (class == NULL || IS_ERR(class))
1505 goto error;
1506
1507 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1508 if (!dev) {
1509 retval = -ENOMEM;
1510 goto error;
1511 }
1512
1513 dev->devt = devt;
1514 dev->class = class;
1515 dev->parent = parent;
1516 dev->release = device_create_release;
1517 dev_set_drvdata(dev, drvdata);
1518
Kay Sievers1fa5ae82009-01-25 15:17:37 +01001519 retval = kobject_set_name_vargs(&dev->kobj, fmt, args);
1520 if (retval)
1521 goto error;
1522
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07001523 retval = device_register(dev);
1524 if (retval)
1525 goto error;
1526
1527 return dev;
1528
1529error:
Cornelia Huck286661b2008-09-03 18:26:41 +02001530 put_device(dev);
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07001531 return ERR_PTR(retval);
1532}
1533EXPORT_SYMBOL_GPL(device_create_vargs);
1534
1535/**
Greg Kroah-Hartman4e106732008-07-21 20:03:34 -07001536 * device_create - creates a device and registers it with sysfs
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07001537 * @class: pointer to the struct class that this device should be registered to
1538 * @parent: pointer to the parent struct device of this new device, if any
1539 * @devt: the dev_t for the char device to be added
1540 * @drvdata: the data to be added to the device for callbacks
1541 * @fmt: string for the device's name
1542 *
1543 * This function can be used by char device classes. A struct device
1544 * will be created in sysfs, registered to the specified class.
1545 *
1546 * A "dev" file will be created, showing the dev_t for the device, if
1547 * the dev_t is not 0,0.
1548 * If a pointer to a parent struct device is passed in, the newly created
1549 * struct device will be a child of that device in sysfs.
1550 * The pointer to the struct device will be returned from the call.
1551 * Any further sysfs files that might be required can be created using this
1552 * pointer.
1553 *
Jani Nikulaf0eae0e2010-03-11 18:11:45 +02001554 * Returns &struct device pointer on success, or ERR_PTR() on error.
1555 *
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07001556 * Note: the struct class passed to this function must have previously
1557 * been created with a call to class_create().
1558 */
Greg Kroah-Hartman4e106732008-07-21 20:03:34 -07001559struct device *device_create(struct class *class, struct device *parent,
1560 dev_t devt, void *drvdata, const char *fmt, ...)
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07001561{
1562 va_list vargs;
1563 struct device *dev;
1564
1565 va_start(vargs, fmt);
1566 dev = device_create_vargs(class, parent, devt, drvdata, fmt, vargs);
1567 va_end(vargs);
1568 return dev;
1569}
Greg Kroah-Hartman4e106732008-07-21 20:03:34 -07001570EXPORT_SYMBOL_GPL(device_create);
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07001571
Dave Youngcd354492008-01-28 16:56:11 +08001572static int __match_devt(struct device *dev, void *data)
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001573{
Dave Youngcd354492008-01-28 16:56:11 +08001574 dev_t *devt = data;
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001575
Dave Youngcd354492008-01-28 16:56:11 +08001576 return dev->devt == *devt;
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001577}
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001578
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001579/**
1580 * device_destroy - removes a device that was created with device_create()
1581 * @class: pointer to the struct class that this device was registered with
1582 * @devt: the dev_t of the device that was previously registered
1583 *
1584 * This call unregisters and cleans up a device that was created with a
1585 * call to device_create().
1586 */
1587void device_destroy(struct class *class, dev_t devt)
1588{
1589 struct device *dev;
1590
Greg Kroah-Hartman695794a2008-05-22 17:21:08 -04001591 dev = class_find_device(class, NULL, &devt, __match_devt);
Dave Youngcd354492008-01-28 16:56:11 +08001592 if (dev) {
1593 put_device(dev);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001594 device_unregister(dev);
Dave Youngcd354492008-01-28 16:56:11 +08001595 }
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001596}
1597EXPORT_SYMBOL_GPL(device_destroy);
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001598
1599/**
1600 * device_rename - renames a device
1601 * @dev: the pointer to the struct device to be renamed
1602 * @new_name: the new name of the device
Eric W. Biederman030c1d22008-05-08 14:41:00 -07001603 *
1604 * It is the responsibility of the caller to provide mutual
1605 * exclusion between two different calls of device_rename
1606 * on the same device to ensure that new_name is valid and
1607 * won't conflict with other devices.
Michael Ellermanc6c0ac62010-11-25 09:44:07 +11001608 *
Timur Tabia5462512010-12-13 14:08:52 -06001609 * Note: Don't call this function. Currently, the networking layer calls this
1610 * function, but that will change. The following text from Kay Sievers offers
1611 * some insight:
1612 *
1613 * Renaming devices is racy at many levels, symlinks and other stuff are not
1614 * replaced atomically, and you get a "move" uevent, but it's not easy to
1615 * connect the event to the old and new device. Device nodes are not renamed at
1616 * all, there isn't even support for that in the kernel now.
1617 *
1618 * In the meantime, during renaming, your target name might be taken by another
1619 * driver, creating conflicts. Or the old name is taken directly after you
1620 * renamed it -- then you get events for the same DEVPATH, before you even see
1621 * the "move" event. It's just a mess, and nothing new should ever rely on
1622 * kernel device renaming. Besides that, it's not even implemented now for
1623 * other things than (driver-core wise very simple) network devices.
1624 *
1625 * We are currently about to change network renaming in udev to completely
1626 * disallow renaming of devices in the same namespace as the kernel uses,
1627 * because we can't solve the problems properly, that arise with swapping names
1628 * of multiple interfaces without races. Means, renaming of eth[0-9]* will only
1629 * be allowed to some other name than eth[0-9]*, for the aforementioned
1630 * reasons.
1631 *
1632 * Make up a "real" name in the driver before you register anything, or add
1633 * some other attributes for userspace to find the device, or use udev to add
1634 * symlinks -- but never rename kernel devices later, it's a complete mess. We
1635 * don't even want to get into that and try to implement the missing pieces in
1636 * the core. We really have other pieces to fix in the driver core mess. :)
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001637 */
Johannes Berg6937e8f2010-08-05 17:38:18 +02001638int device_rename(struct device *dev, const char *new_name)
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001639{
1640 char *old_class_name = NULL;
1641 char *new_class_name = NULL;
Cornelia Huck2ee97caf2007-07-18 01:43:47 -07001642 char *old_device_name = NULL;
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001643 int error;
1644
1645 dev = get_device(dev);
1646 if (!dev)
1647 return -EINVAL;
1648
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01001649 pr_debug("device: '%s': %s: renaming to '%s'\n", dev_name(dev),
Harvey Harrison2b3a3022008-03-04 16:41:05 -08001650 __func__, new_name);
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001651
Kay Sievers1fa5ae82009-01-25 15:17:37 +01001652 old_device_name = kstrdup(dev_name(dev), GFP_KERNEL);
Cornelia Huck2ee97caf2007-07-18 01:43:47 -07001653 if (!old_device_name) {
1654 error = -ENOMEM;
1655 goto out;
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001656 }
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001657
Eric W. Biedermanf349cf32010-03-30 11:31:29 -07001658 if (dev->class) {
Kay Sievers6b6e39a2010-11-15 23:13:18 +01001659 error = sysfs_rename_link(&dev->class->p->subsys.kobj,
Eric W. Biedermanf349cf32010-03-30 11:31:29 -07001660 &dev->kobj, old_device_name, new_name);
1661 if (error)
1662 goto out;
1663 }
Kay Sievers39aba962010-09-04 22:33:14 -07001664
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001665 error = kobject_rename(&dev->kobj, new_name);
Kay Sievers1fa5ae82009-01-25 15:17:37 +01001666 if (error)
Cornelia Huck2ee97caf2007-07-18 01:43:47 -07001667 goto out;
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001668
Cornelia Huck2ee97caf2007-07-18 01:43:47 -07001669out:
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001670 put_device(dev);
1671
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001672 kfree(new_class_name);
Jesper Juhl952ab432006-09-28 23:56:01 +02001673 kfree(old_class_name);
Cornelia Huck2ee97caf2007-07-18 01:43:47 -07001674 kfree(old_device_name);
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001675
1676 return error;
1677}
Johannes Berga2807db2007-02-28 12:38:31 +01001678EXPORT_SYMBOL_GPL(device_rename);
Cornelia Huck8a824722006-11-20 17:07:51 +01001679
1680static int device_move_class_links(struct device *dev,
1681 struct device *old_parent,
1682 struct device *new_parent)
1683{
Greg Kroah-Hartmanf7f34612007-03-06 12:55:53 -08001684 int error = 0;
Cornelia Huck8a824722006-11-20 17:07:51 +01001685
Greg Kroah-Hartmanf7f34612007-03-06 12:55:53 -08001686 if (old_parent)
1687 sysfs_remove_link(&dev->kobj, "device");
1688 if (new_parent)
1689 error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
1690 "device");
1691 return error;
Cornelia Huck8a824722006-11-20 17:07:51 +01001692}
1693
1694/**
1695 * device_move - moves a device to a new parent
1696 * @dev: the pointer to the struct device to be moved
Cornelia Huckc744aea2007-01-08 20:16:44 +01001697 * @new_parent: the new parent of the device (can by NULL)
Cornelia Huckffa6a702009-03-04 12:44:00 +01001698 * @dpm_order: how to reorder the dpm_list
Cornelia Huck8a824722006-11-20 17:07:51 +01001699 */
Cornelia Huckffa6a702009-03-04 12:44:00 +01001700int device_move(struct device *dev, struct device *new_parent,
1701 enum dpm_order dpm_order)
Cornelia Huck8a824722006-11-20 17:07:51 +01001702{
1703 int error;
1704 struct device *old_parent;
Cornelia Huckc744aea2007-01-08 20:16:44 +01001705 struct kobject *new_parent_kobj;
Cornelia Huck8a824722006-11-20 17:07:51 +01001706
1707 dev = get_device(dev);
1708 if (!dev)
1709 return -EINVAL;
1710
Cornelia Huckffa6a702009-03-04 12:44:00 +01001711 device_pm_lock();
Cornelia Huck8a824722006-11-20 17:07:51 +01001712 new_parent = get_device(new_parent);
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001713 new_parent_kobj = get_device_parent(dev, new_parent);
Cornelia Huck63b69712008-01-21 16:09:44 +01001714
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01001715 pr_debug("device: '%s': %s: moving to '%s'\n", dev_name(dev),
1716 __func__, new_parent ? dev_name(new_parent) : "<NULL>");
Cornelia Huckc744aea2007-01-08 20:16:44 +01001717 error = kobject_move(&dev->kobj, new_parent_kobj);
Cornelia Huck8a824722006-11-20 17:07:51 +01001718 if (error) {
Cornelia Huck63b69712008-01-21 16:09:44 +01001719 cleanup_glue_dir(dev, new_parent_kobj);
Cornelia Huck8a824722006-11-20 17:07:51 +01001720 put_device(new_parent);
1721 goto out;
1722 }
1723 old_parent = dev->parent;
1724 dev->parent = new_parent;
1725 if (old_parent)
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08001726 klist_remove(&dev->p->knode_parent);
Yinghai Lu0d358f22008-02-19 03:20:41 -08001727 if (new_parent) {
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08001728 klist_add_tail(&dev->p->knode_parent,
1729 &new_parent->p->klist_children);
Yinghai Lu0d358f22008-02-19 03:20:41 -08001730 set_dev_node(dev, dev_to_node(new_parent));
1731 }
1732
Cornelia Huck8a824722006-11-20 17:07:51 +01001733 if (!dev->class)
1734 goto out_put;
1735 error = device_move_class_links(dev, old_parent, new_parent);
1736 if (error) {
1737 /* We ignore errors on cleanup since we're hosed anyway... */
1738 device_move_class_links(dev, new_parent, old_parent);
1739 if (!kobject_move(&dev->kobj, &old_parent->kobj)) {
Cornelia Huckc744aea2007-01-08 20:16:44 +01001740 if (new_parent)
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08001741 klist_remove(&dev->p->knode_parent);
Yinghai Lu0d358f22008-02-19 03:20:41 -08001742 dev->parent = old_parent;
1743 if (old_parent) {
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08001744 klist_add_tail(&dev->p->knode_parent,
1745 &old_parent->p->klist_children);
Yinghai Lu0d358f22008-02-19 03:20:41 -08001746 set_dev_node(dev, dev_to_node(old_parent));
1747 }
Cornelia Huck8a824722006-11-20 17:07:51 +01001748 }
Cornelia Huck63b69712008-01-21 16:09:44 +01001749 cleanup_glue_dir(dev, new_parent_kobj);
Cornelia Huck8a824722006-11-20 17:07:51 +01001750 put_device(new_parent);
1751 goto out;
1752 }
Cornelia Huckffa6a702009-03-04 12:44:00 +01001753 switch (dpm_order) {
1754 case DPM_ORDER_NONE:
1755 break;
1756 case DPM_ORDER_DEV_AFTER_PARENT:
1757 device_pm_move_after(dev, new_parent);
1758 break;
1759 case DPM_ORDER_PARENT_BEFORE_DEV:
1760 device_pm_move_before(new_parent, dev);
1761 break;
1762 case DPM_ORDER_DEV_LAST:
1763 device_pm_move_last(dev);
1764 break;
1765 }
Cornelia Huck8a824722006-11-20 17:07:51 +01001766out_put:
1767 put_device(old_parent);
1768out:
Cornelia Huckffa6a702009-03-04 12:44:00 +01001769 device_pm_unlock();
Cornelia Huck8a824722006-11-20 17:07:51 +01001770 put_device(dev);
1771 return error;
1772}
Cornelia Huck8a824722006-11-20 17:07:51 +01001773EXPORT_SYMBOL_GPL(device_move);
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08001774
1775/**
1776 * device_shutdown - call ->shutdown() on each device to shutdown.
1777 */
1778void device_shutdown(void)
1779{
Hugh Daschbach62458382010-03-22 10:36:37 -07001780 struct device *dev;
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08001781
Hugh Daschbach62458382010-03-22 10:36:37 -07001782 spin_lock(&devices_kset->list_lock);
1783 /*
1784 * Walk the devices list backward, shutting down each in turn.
1785 * Beware that device unplug events may also start pulling
1786 * devices offline, even as the system is shutting down.
1787 */
1788 while (!list_empty(&devices_kset->list)) {
1789 dev = list_entry(devices_kset->list.prev, struct device,
1790 kobj.entry);
1791 get_device(dev);
1792 /*
1793 * Make sure the device is off the kset list, in the
1794 * event that dev->*->shutdown() doesn't remove it.
1795 */
1796 list_del_init(&dev->kobj.entry);
1797 spin_unlock(&devices_kset->list_lock);
1798
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08001799 if (dev->bus && dev->bus->shutdown) {
1800 dev_dbg(dev, "shutdown\n");
1801 dev->bus->shutdown(dev);
1802 } else if (dev->driver && dev->driver->shutdown) {
1803 dev_dbg(dev, "shutdown\n");
1804 dev->driver->shutdown(dev);
1805 }
Hugh Daschbach62458382010-03-22 10:36:37 -07001806 put_device(dev);
1807
1808 spin_lock(&devices_kset->list_lock);
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08001809 }
Hugh Daschbach62458382010-03-22 10:36:37 -07001810 spin_unlock(&devices_kset->list_lock);
Shaohua Li401097e2009-05-12 13:37:57 -07001811 async_synchronize_full();
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08001812}
Joe Perches99bcf212010-06-27 01:02:34 +00001813
1814/*
1815 * Device logging functions
1816 */
1817
1818#ifdef CONFIG_PRINTK
1819
Joe Perchescbc46632011-08-11 14:36:21 -04001820int __dev_printk(const char *level, const struct device *dev,
1821 struct va_format *vaf)
Joe Perches99bcf212010-06-27 01:02:34 +00001822{
1823 if (!dev)
1824 return printk("%s(NULL device *): %pV", level, vaf);
1825
1826 return printk("%s%s %s: %pV",
1827 level, dev_driver_string(dev), dev_name(dev), vaf);
1828}
Joe Perchescbc46632011-08-11 14:36:21 -04001829EXPORT_SYMBOL(__dev_printk);
Joe Perches99bcf212010-06-27 01:02:34 +00001830
1831int dev_printk(const char *level, const struct device *dev,
1832 const char *fmt, ...)
1833{
1834 struct va_format vaf;
1835 va_list args;
1836 int r;
1837
1838 va_start(args, fmt);
1839
1840 vaf.fmt = fmt;
1841 vaf.va = &args;
1842
1843 r = __dev_printk(level, dev, &vaf);
1844 va_end(args);
1845
1846 return r;
1847}
1848EXPORT_SYMBOL(dev_printk);
1849
1850#define define_dev_printk_level(func, kern_level) \
1851int func(const struct device *dev, const char *fmt, ...) \
1852{ \
1853 struct va_format vaf; \
1854 va_list args; \
1855 int r; \
1856 \
1857 va_start(args, fmt); \
1858 \
1859 vaf.fmt = fmt; \
1860 vaf.va = &args; \
1861 \
1862 r = __dev_printk(kern_level, dev, &vaf); \
1863 va_end(args); \
1864 \
1865 return r; \
1866} \
1867EXPORT_SYMBOL(func);
1868
1869define_dev_printk_level(dev_emerg, KERN_EMERG);
1870define_dev_printk_level(dev_alert, KERN_ALERT);
1871define_dev_printk_level(dev_crit, KERN_CRIT);
1872define_dev_printk_level(dev_err, KERN_ERR);
1873define_dev_printk_level(dev_warn, KERN_WARNING);
1874define_dev_printk_level(dev_notice, KERN_NOTICE);
1875define_dev_printk_level(_dev_info, KERN_INFO);
1876
1877#endif