blob: a7391a30cb294ab7fbc405b6f5b2496654626dc2 [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>
Grant Likely07d57a32012-02-01 11:22:22 -070021#include <linux/of.h>
22#include <linux/of_device.h>
Kay Sieversda231fd2007-11-21 17:29:15 +010023#include <linux/genhd.h>
Andrew Morton815d2d52008-03-04 15:09:07 -080024#include <linux/kallsyms.h>
Dave Youngf75b1c62008-05-28 09:28:39 -070025#include <linux/mutex.h>
Shaohua Li401097e2009-05-12 13:37:57 -070026#include <linux/async.h>
Peter Chenaf8db152011-11-15 21:52:29 +010027#include <linux/pm_runtime.h>
Kay Sieversc4e00da2012-05-03 02:29:59 +020028#include <linux/netdevice.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
30#include "base.h"
31#include "power/power.h"
32
Andi Kleene52eec12010-09-08 16:54:17 +020033#ifdef CONFIG_SYSFS_DEPRECATED
34#ifdef CONFIG_SYSFS_DEPRECATED_V2
35long sysfs_deprecated = 1;
36#else
37long sysfs_deprecated = 0;
38#endif
39static __init int sysfs_deprecated_setup(char *arg)
40{
41 return strict_strtol(arg, 10, &sysfs_deprecated);
42}
43early_param("sysfs.deprecated", sysfs_deprecated_setup);
44#endif
45
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -080046int (*platform_notify)(struct device *dev) = NULL;
47int (*platform_notify_remove)(struct device *dev) = NULL;
Dan Williamse105b8b2008-04-21 10:51:07 -070048static struct kobject *dev_kobj;
49struct kobject *sysfs_dev_char_kobj;
50struct kobject *sysfs_dev_block_kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Greg Kroah-Hartman4e886c22008-01-27 12:12:43 -080052#ifdef CONFIG_BLOCK
53static inline int device_is_not_partition(struct device *dev)
54{
55 return !(dev->type == &part_type);
56}
57#else
58static inline int device_is_not_partition(struct device *dev)
59{
60 return 1;
61}
62#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Alan Stern3e956372006-06-16 17:10:48 -040064/**
65 * dev_driver_string - Return a device's driver name, if at all possible
66 * @dev: struct device to get the name of
67 *
68 * Will return the device's driver's name if it is bound to a device. If
yan9169c012012-04-20 21:08:45 +080069 * the device is not bound to a driver, it will return the name of the bus
Alan Stern3e956372006-06-16 17:10:48 -040070 * it is attached to. If it is not attached to a bus either, an empty
71 * string will be returned.
72 */
Jean Delvarebf9ca692008-07-30 12:29:21 -070073const char *dev_driver_string(const struct device *dev)
Alan Stern3e956372006-06-16 17:10:48 -040074{
Alan Stern35899722009-12-04 11:06:57 -050075 struct device_driver *drv;
76
77 /* dev->driver can change to NULL underneath us because of unbinding,
78 * so be careful about accessing it. dev->bus and dev->class should
79 * never change once they are set, so they don't need special care.
80 */
81 drv = ACCESS_ONCE(dev->driver);
82 return drv ? drv->name :
Jean Delvarea456b702007-03-09 16:33:10 +010083 (dev->bus ? dev->bus->name :
84 (dev->class ? dev->class->name : ""));
Alan Stern3e956372006-06-16 17:10:48 -040085}
Matthew Wilcox310a9222006-09-23 23:35:04 -060086EXPORT_SYMBOL(dev_driver_string);
Alan Stern3e956372006-06-16 17:10:48 -040087
Linus Torvalds1da177e2005-04-16 15:20:36 -070088#define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr)
89
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -080090static ssize_t dev_attr_show(struct kobject *kobj, struct attribute *attr,
91 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070092{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -080093 struct device_attribute *dev_attr = to_dev_attr(attr);
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +020094 struct device *dev = kobj_to_dev(kobj);
Dmitry Torokhov4a0c20b2005-04-29 01:23:47 -050095 ssize_t ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
97 if (dev_attr->show)
Yani Ioannou54b6f352005-05-17 06:39:34 -040098 ret = dev_attr->show(dev, dev_attr, buf);
Andrew Morton815d2d52008-03-04 15:09:07 -080099 if (ret >= (ssize_t)PAGE_SIZE) {
Greg Kroah-Hartman53a9c872013-01-17 13:10:23 -0800100 print_symbol("dev_attr_show: %s returned bad count\n",
101 (unsigned long)dev_attr->show);
Andrew Morton815d2d52008-03-04 15:09:07 -0800102 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 return ret;
104}
105
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800106static ssize_t dev_attr_store(struct kobject *kobj, struct attribute *attr,
107 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800109 struct device_attribute *dev_attr = to_dev_attr(attr);
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200110 struct device *dev = kobj_to_dev(kobj);
Dmitry Torokhov4a0c20b2005-04-29 01:23:47 -0500111 ssize_t ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
113 if (dev_attr->store)
Yani Ioannou54b6f352005-05-17 06:39:34 -0400114 ret = dev_attr->store(dev, dev_attr, buf, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 return ret;
116}
117
Emese Revfy52cf25d2010-01-19 02:58:23 +0100118static const struct sysfs_ops dev_sysfs_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 .show = dev_attr_show,
120 .store = dev_attr_store,
121};
122
Kay Sieversca22e562011-12-14 14:29:38 -0800123#define to_ext_attr(x) container_of(x, struct dev_ext_attribute, attr)
124
125ssize_t device_store_ulong(struct device *dev,
126 struct device_attribute *attr,
127 const char *buf, size_t size)
128{
129 struct dev_ext_attribute *ea = to_ext_attr(attr);
130 char *end;
131 unsigned long new = simple_strtoul(buf, &end, 0);
132 if (end == buf)
133 return -EINVAL;
134 *(unsigned long *)(ea->var) = new;
135 /* Always return full write size even if we didn't consume all */
136 return size;
137}
138EXPORT_SYMBOL_GPL(device_store_ulong);
139
140ssize_t device_show_ulong(struct device *dev,
141 struct device_attribute *attr,
142 char *buf)
143{
144 struct dev_ext_attribute *ea = to_ext_attr(attr);
145 return snprintf(buf, PAGE_SIZE, "%lx\n", *(unsigned long *)(ea->var));
146}
147EXPORT_SYMBOL_GPL(device_show_ulong);
148
149ssize_t device_store_int(struct device *dev,
150 struct device_attribute *attr,
151 const char *buf, size_t size)
152{
153 struct dev_ext_attribute *ea = to_ext_attr(attr);
154 char *end;
155 long new = simple_strtol(buf, &end, 0);
156 if (end == buf || new > INT_MAX || new < INT_MIN)
157 return -EINVAL;
158 *(int *)(ea->var) = new;
159 /* Always return full write size even if we didn't consume all */
160 return size;
161}
162EXPORT_SYMBOL_GPL(device_store_int);
163
164ssize_t device_show_int(struct device *dev,
165 struct device_attribute *attr,
166 char *buf)
167{
168 struct dev_ext_attribute *ea = to_ext_attr(attr);
169
170 return snprintf(buf, PAGE_SIZE, "%d\n", *(int *)(ea->var));
171}
172EXPORT_SYMBOL_GPL(device_show_int);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
Borislav Petkov91872392012-10-09 19:52:05 +0200174ssize_t device_store_bool(struct device *dev, struct device_attribute *attr,
175 const char *buf, size_t size)
176{
177 struct dev_ext_attribute *ea = to_ext_attr(attr);
178
179 if (strtobool(buf, ea->var) < 0)
180 return -EINVAL;
181
182 return size;
183}
184EXPORT_SYMBOL_GPL(device_store_bool);
185
186ssize_t device_show_bool(struct device *dev, struct device_attribute *attr,
187 char *buf)
188{
189 struct dev_ext_attribute *ea = to_ext_attr(attr);
190
191 return snprintf(buf, PAGE_SIZE, "%d\n", *(bool *)(ea->var));
192}
193EXPORT_SYMBOL_GPL(device_show_bool);
194
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195/**
196 * device_release - free device structure.
197 * @kobj: device's kobject.
198 *
199 * This is called once the reference count for the object
200 * reaches 0. We forward the call to the device's release
201 * method, which should handle actually freeing the structure.
202 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800203static void device_release(struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200205 struct device *dev = kobj_to_dev(kobj);
Greg Kroah-Hartmanfb069a52008-12-16 12:23:36 -0800206 struct device_private *p = dev->p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
Ming Leia525a3d2012-07-25 01:42:29 +0800208 /*
209 * Some platform devices are driven without driver attached
210 * and managed resources may have been acquired. Make sure
211 * all resources are released.
212 *
213 * Drivers still can add resources into device after device
214 * is deleted but alive, so release devres here to avoid
215 * possible memory leak.
216 */
217 devres_release_all(dev);
218
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 if (dev->release)
220 dev->release(dev);
Kay Sieversf9f852d2006-10-07 21:54:55 +0200221 else if (dev->type && dev->type->release)
222 dev->type->release(dev);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700223 else if (dev->class && dev->class->dev_release)
224 dev->class->dev_release(dev);
Arjan van de Venf810a5c2008-07-25 19:45:39 -0700225 else
226 WARN(1, KERN_ERR "Device '%s' does not have a release() "
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800227 "function, it is broken and must be fixed.\n",
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100228 dev_name(dev));
Greg Kroah-Hartmanfb069a52008-12-16 12:23:36 -0800229 kfree(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230}
231
Eric W. Biedermanbc451f22010-03-30 11:31:25 -0700232static const void *device_namespace(struct kobject *kobj)
233{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200234 struct device *dev = kobj_to_dev(kobj);
Eric W. Biedermanbc451f22010-03-30 11:31:25 -0700235 const void *ns = NULL;
236
237 if (dev->class && dev->class->ns_type)
238 ns = dev->class->namespace(dev);
239
240 return ns;
241}
242
Greg Kroah-Hartman8f4afc42007-10-11 10:47:49 -0600243static struct kobj_type device_ktype = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 .release = device_release,
245 .sysfs_ops = &dev_sysfs_ops,
Eric W. Biedermanbc451f22010-03-30 11:31:25 -0700246 .namespace = device_namespace,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247};
248
249
Kay Sievers312c0042005-11-16 09:00:00 +0100250static int dev_uevent_filter(struct kset *kset, struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251{
252 struct kobj_type *ktype = get_ktype(kobj);
253
Greg Kroah-Hartman8f4afc42007-10-11 10:47:49 -0600254 if (ktype == &device_ktype) {
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200255 struct device *dev = kobj_to_dev(kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 if (dev->bus)
257 return 1;
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700258 if (dev->class)
259 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 }
261 return 0;
262}
263
Kay Sievers312c0042005-11-16 09:00:00 +0100264static const char *dev_uevent_name(struct kset *kset, struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200266 struct device *dev = kobj_to_dev(kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700268 if (dev->bus)
269 return dev->bus->name;
270 if (dev->class)
271 return dev->class->name;
272 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273}
274
Kay Sievers7eff2e72007-08-14 15:15:12 +0200275static int dev_uevent(struct kset *kset, struct kobject *kobj,
276 struct kobj_uevent_env *env)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200278 struct device *dev = kobj_to_dev(kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 int retval = 0;
280
Kay Sievers6fcf53a2009-04-30 15:23:42 +0200281 /* add device node properties if present */
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700282 if (MAJOR(dev->devt)) {
Kay Sievers6fcf53a2009-04-30 15:23:42 +0200283 const char *tmp;
284 const char *name;
Al Viro2c9ede52011-07-23 20:24:48 -0400285 umode_t mode = 0;
Kay Sievers6fcf53a2009-04-30 15:23:42 +0200286
Kay Sievers7eff2e72007-08-14 15:15:12 +0200287 add_uevent_var(env, "MAJOR=%u", MAJOR(dev->devt));
288 add_uevent_var(env, "MINOR=%u", MINOR(dev->devt));
Kay Sieverse454cea2009-09-18 23:01:12 +0200289 name = device_get_devnode(dev, &mode, &tmp);
Kay Sievers6fcf53a2009-04-30 15:23:42 +0200290 if (name) {
291 add_uevent_var(env, "DEVNAME=%s", name);
292 kfree(tmp);
Kay Sieverse454cea2009-09-18 23:01:12 +0200293 if (mode)
294 add_uevent_var(env, "DEVMODE=%#o", mode & 0777);
Kay Sievers6fcf53a2009-04-30 15:23:42 +0200295 }
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700296 }
297
Kay Sievers414264f2007-03-12 21:08:57 +0100298 if (dev->type && dev->type->name)
Kay Sievers7eff2e72007-08-14 15:15:12 +0200299 add_uevent_var(env, "DEVTYPE=%s", dev->type->name);
Kay Sievers414264f2007-03-12 21:08:57 +0100300
Kay Sievers239378f2006-10-07 21:54:55 +0200301 if (dev->driver)
Kay Sievers7eff2e72007-08-14 15:15:12 +0200302 add_uevent_var(env, "DRIVER=%s", dev->driver->name);
Kay Sievers239378f2006-10-07 21:54:55 +0200303
Grant Likely07d57a32012-02-01 11:22:22 -0700304 /* Add common DT information about the device */
305 of_device_uevent(dev, env);
306
Kay Sievers7eff2e72007-08-14 15:15:12 +0200307 /* have the bus specific function add its stuff */
Kay Sievers312c0042005-11-16 09:00:00 +0100308 if (dev->bus && dev->bus->uevent) {
Kay Sievers7eff2e72007-08-14 15:15:12 +0200309 retval = dev->bus->uevent(dev, env);
Kay Sieversf9f852d2006-10-07 21:54:55 +0200310 if (retval)
Greg Kroah-Hartman7dc72b22007-11-28 23:49:41 -0800311 pr_debug("device: '%s': %s: bus uevent() returned %d\n",
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100312 dev_name(dev), __func__, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 }
314
Kay Sievers7eff2e72007-08-14 15:15:12 +0200315 /* have the class specific function add its stuff */
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700316 if (dev->class && dev->class->dev_uevent) {
Kay Sievers7eff2e72007-08-14 15:15:12 +0200317 retval = dev->class->dev_uevent(dev, env);
Kay Sieversf9f852d2006-10-07 21:54:55 +0200318 if (retval)
Greg Kroah-Hartman7dc72b22007-11-28 23:49:41 -0800319 pr_debug("device: '%s': %s: class uevent() "
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100320 "returned %d\n", dev_name(dev),
Harvey Harrison2b3a3022008-03-04 16:41:05 -0800321 __func__, retval);
Kay Sieversf9f852d2006-10-07 21:54:55 +0200322 }
323
Stefan Weileef35c22010-08-06 21:11:15 +0200324 /* have the device type specific function add its stuff */
Kay Sieversf9f852d2006-10-07 21:54:55 +0200325 if (dev->type && dev->type->uevent) {
Kay Sievers7eff2e72007-08-14 15:15:12 +0200326 retval = dev->type->uevent(dev, env);
Kay Sieversf9f852d2006-10-07 21:54:55 +0200327 if (retval)
Greg Kroah-Hartman7dc72b22007-11-28 23:49:41 -0800328 pr_debug("device: '%s': %s: dev_type uevent() "
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100329 "returned %d\n", dev_name(dev),
Harvey Harrison2b3a3022008-03-04 16:41:05 -0800330 __func__, retval);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700331 }
332
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 return retval;
334}
335
Emese Revfy9cd43612009-12-31 14:52:51 +0100336static const struct kset_uevent_ops device_uevent_ops = {
Kay Sievers312c0042005-11-16 09:00:00 +0100337 .filter = dev_uevent_filter,
338 .name = dev_uevent_name,
339 .uevent = dev_uevent,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340};
341
Kay Sievers16574dc2007-04-06 01:40:38 +0200342static ssize_t show_uevent(struct device *dev, struct device_attribute *attr,
343 char *buf)
344{
345 struct kobject *top_kobj;
346 struct kset *kset;
Kay Sievers7eff2e72007-08-14 15:15:12 +0200347 struct kobj_uevent_env *env = NULL;
Kay Sievers16574dc2007-04-06 01:40:38 +0200348 int i;
349 size_t count = 0;
350 int retval;
351
352 /* search the kset, the device belongs to */
353 top_kobj = &dev->kobj;
Kay Sievers5c5daf62007-08-12 20:43:55 +0200354 while (!top_kobj->kset && top_kobj->parent)
355 top_kobj = top_kobj->parent;
Kay Sievers16574dc2007-04-06 01:40:38 +0200356 if (!top_kobj->kset)
357 goto out;
Kay Sievers5c5daf62007-08-12 20:43:55 +0200358
Kay Sievers16574dc2007-04-06 01:40:38 +0200359 kset = top_kobj->kset;
360 if (!kset->uevent_ops || !kset->uevent_ops->uevent)
361 goto out;
362
363 /* respect filter */
364 if (kset->uevent_ops && kset->uevent_ops->filter)
365 if (!kset->uevent_ops->filter(kset, &dev->kobj))
366 goto out;
367
Kay Sievers7eff2e72007-08-14 15:15:12 +0200368 env = kzalloc(sizeof(struct kobj_uevent_env), GFP_KERNEL);
369 if (!env)
Greg Kroah-Hartmanc7308c82007-05-02 14:14:11 +0200370 return -ENOMEM;
371
Kay Sievers16574dc2007-04-06 01:40:38 +0200372 /* let the kset specific function add its keys */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200373 retval = kset->uevent_ops->uevent(kset, &dev->kobj, env);
Kay Sievers16574dc2007-04-06 01:40:38 +0200374 if (retval)
375 goto out;
376
377 /* copy keys to file */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200378 for (i = 0; i < env->envp_idx; i++)
379 count += sprintf(&buf[count], "%s\n", env->envp[i]);
Kay Sievers16574dc2007-04-06 01:40:38 +0200380out:
Kay Sievers7eff2e72007-08-14 15:15:12 +0200381 kfree(env);
Kay Sievers16574dc2007-04-06 01:40:38 +0200382 return count;
383}
384
Kay Sieversa7fd6702005-10-01 14:49:43 +0200385static ssize_t store_uevent(struct device *dev, struct device_attribute *attr,
386 const char *buf, size_t count)
387{
Kay Sievers60a96a52007-07-08 22:29:26 +0200388 enum kobject_action action;
389
Kay Sievers3f5468c2010-01-14 22:54:37 +0100390 if (kobject_action_type(buf, count, &action) == 0)
Kay Sievers60a96a52007-07-08 22:29:26 +0200391 kobject_uevent(&dev->kobj, action);
Kay Sievers3f5468c2010-01-14 22:54:37 +0100392 else
393 dev_err(dev, "uevent: unknown action-string\n");
Kay Sieversa7fd6702005-10-01 14:49:43 +0200394 return count;
395}
396
Tejun Heoad6a1e12007-06-14 03:45:17 +0900397static struct device_attribute uevent_attr =
398 __ATTR(uevent, S_IRUGO | S_IWUSR, show_uevent, store_uevent);
399
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500400static int device_add_attributes(struct device *dev,
401 struct device_attribute *attrs)
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -0700402{
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -0700403 int error = 0;
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500404 int i;
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -0700405
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500406 if (attrs) {
407 for (i = 0; attr_name(attrs[i]); i++) {
408 error = device_create_file(dev, &attrs[i]);
409 if (error)
410 break;
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -0700411 }
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500412 if (error)
413 while (--i >= 0)
414 device_remove_file(dev, &attrs[i]);
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -0700415 }
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -0700416 return error;
417}
418
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500419static void device_remove_attributes(struct device *dev,
420 struct device_attribute *attrs)
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -0700421{
422 int i;
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500423
424 if (attrs)
425 for (i = 0; attr_name(attrs[i]); i++)
426 device_remove_file(dev, &attrs[i]);
427}
428
Stefan Achatzc97415a2010-11-26 19:57:29 +0000429static int device_add_bin_attributes(struct device *dev,
430 struct bin_attribute *attrs)
431{
432 int error = 0;
433 int i;
434
435 if (attrs) {
436 for (i = 0; attr_name(attrs[i]); i++) {
437 error = device_create_bin_file(dev, &attrs[i]);
438 if (error)
439 break;
440 }
441 if (error)
442 while (--i >= 0)
443 device_remove_bin_file(dev, &attrs[i]);
444 }
445 return error;
446}
447
448static void device_remove_bin_attributes(struct device *dev,
449 struct bin_attribute *attrs)
450{
451 int i;
452
453 if (attrs)
454 for (i = 0; attr_name(attrs[i]); i++)
455 device_remove_bin_file(dev, &attrs[i]);
456}
457
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500458static int device_add_groups(struct device *dev,
David Brownella4dbd672009-06-24 10:06:31 -0700459 const struct attribute_group **groups)
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500460{
461 int error = 0;
462 int i;
463
464 if (groups) {
465 for (i = 0; groups[i]; i++) {
466 error = sysfs_create_group(&dev->kobj, groups[i]);
467 if (error) {
468 while (--i >= 0)
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800469 sysfs_remove_group(&dev->kobj,
470 groups[i]);
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500471 break;
472 }
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -0700473 }
474 }
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500475 return error;
476}
477
478static void device_remove_groups(struct device *dev,
David Brownella4dbd672009-06-24 10:06:31 -0700479 const struct attribute_group **groups)
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500480{
481 int i;
482
483 if (groups)
484 for (i = 0; groups[i]; i++)
485 sysfs_remove_group(&dev->kobj, groups[i]);
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -0700486}
487
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700488static int device_add_attrs(struct device *dev)
489{
490 struct class *class = dev->class;
Stephen Hemmingeraed65af2011-03-28 09:12:52 -0700491 const struct device_type *type = dev->type;
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500492 int error;
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700493
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500494 if (class) {
495 error = device_add_attributes(dev, class->dev_attrs);
Kay Sieversf9f852d2006-10-07 21:54:55 +0200496 if (error)
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500497 return error;
Stefan Achatzc97415a2010-11-26 19:57:29 +0000498 error = device_add_bin_attributes(dev, class->dev_bin_attrs);
499 if (error)
500 goto err_remove_class_attrs;
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700501 }
Kay Sieversf9f852d2006-10-07 21:54:55 +0200502
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500503 if (type) {
504 error = device_add_groups(dev, type->groups);
Kay Sieversf9f852d2006-10-07 21:54:55 +0200505 if (error)
Stefan Achatzc97415a2010-11-26 19:57:29 +0000506 goto err_remove_class_bin_attrs;
Kay Sieversf9f852d2006-10-07 21:54:55 +0200507 }
508
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500509 error = device_add_groups(dev, dev->groups);
510 if (error)
511 goto err_remove_type_groups;
512
513 return 0;
514
515 err_remove_type_groups:
516 if (type)
517 device_remove_groups(dev, type->groups);
Stefan Achatzc97415a2010-11-26 19:57:29 +0000518 err_remove_class_bin_attrs:
519 if (class)
520 device_remove_bin_attributes(dev, class->dev_bin_attrs);
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500521 err_remove_class_attrs:
522 if (class)
523 device_remove_attributes(dev, class->dev_attrs);
524
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700525 return error;
526}
527
528static void device_remove_attrs(struct device *dev)
529{
530 struct class *class = dev->class;
Stephen Hemmingeraed65af2011-03-28 09:12:52 -0700531 const struct device_type *type = dev->type;
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700532
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500533 device_remove_groups(dev, dev->groups);
Kay Sieversf9f852d2006-10-07 21:54:55 +0200534
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500535 if (type)
536 device_remove_groups(dev, type->groups);
537
Stefan Achatzc97415a2010-11-26 19:57:29 +0000538 if (class) {
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500539 device_remove_attributes(dev, class->dev_attrs);
Stefan Achatzc97415a2010-11-26 19:57:29 +0000540 device_remove_bin_attributes(dev, class->dev_bin_attrs);
541 }
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700542}
543
544
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700545static ssize_t show_dev(struct device *dev, struct device_attribute *attr,
546 char *buf)
547{
548 return print_dev_t(buf, dev->devt);
549}
550
Tejun Heoad6a1e12007-06-14 03:45:17 +0900551static struct device_attribute devt_attr =
552 __ATTR(dev, S_IRUGO, show_dev, NULL);
553
Kay Sieversca22e562011-12-14 14:29:38 -0800554/* /sys/devices/ */
Greg Kroah-Hartman881c6cfd2007-11-01 09:29:06 -0600555struct kset *devices_kset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800558 * device_create_file - create sysfs attribute file for device.
559 * @dev: device.
560 * @attr: device attribute descriptor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 */
Phil Carmody26579ab2009-12-18 15:34:19 +0200562int device_create_file(struct device *dev,
563 const struct device_attribute *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564{
565 int error = 0;
Felipe Balbi8f46baa2013-02-20 10:31:42 +0200566
567 if (dev) {
568 WARN(((attr->attr.mode & S_IWUGO) && !attr->store),
569 "Write permission without 'store'\n");
570 WARN(((attr->attr.mode & S_IRUGO) && !attr->show),
571 "Read permission without 'show'\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 error = sysfs_create_file(&dev->kobj, &attr->attr);
Felipe Balbi8f46baa2013-02-20 10:31:42 +0200573 }
574
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 return error;
576}
577
578/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800579 * device_remove_file - remove sysfs attribute file.
580 * @dev: device.
581 * @attr: device attribute descriptor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 */
Phil Carmody26579ab2009-12-18 15:34:19 +0200583void device_remove_file(struct device *dev,
584 const struct device_attribute *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585{
Cornelia Huck0c98b192008-01-31 10:39:38 +0100586 if (dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 sysfs_remove_file(&dev->kobj, &attr->attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588}
589
Greg Kroah-Hartman2589f1882006-09-19 09:39:19 -0700590/**
591 * device_create_bin_file - create sysfs binary attribute file for device.
592 * @dev: device.
593 * @attr: device binary attribute descriptor.
594 */
Phil Carmody66ecb922009-12-18 15:34:20 +0200595int device_create_bin_file(struct device *dev,
596 const struct bin_attribute *attr)
Greg Kroah-Hartman2589f1882006-09-19 09:39:19 -0700597{
598 int error = -EINVAL;
599 if (dev)
600 error = sysfs_create_bin_file(&dev->kobj, attr);
601 return error;
602}
603EXPORT_SYMBOL_GPL(device_create_bin_file);
604
605/**
606 * device_remove_bin_file - remove sysfs binary attribute file
607 * @dev: device.
608 * @attr: device binary attribute descriptor.
609 */
Phil Carmody66ecb922009-12-18 15:34:20 +0200610void device_remove_bin_file(struct device *dev,
611 const struct bin_attribute *attr)
Greg Kroah-Hartman2589f1882006-09-19 09:39:19 -0700612{
613 if (dev)
614 sysfs_remove_bin_file(&dev->kobj, attr);
615}
616EXPORT_SYMBOL_GPL(device_remove_bin_file);
617
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400618/**
Alan Stern523ded72007-04-26 00:12:04 -0700619 * device_schedule_callback_owner - helper to schedule a callback for a device
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400620 * @dev: device.
621 * @func: callback function to invoke later.
Alan Stern523ded72007-04-26 00:12:04 -0700622 * @owner: module owning the callback routine
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400623 *
624 * Attribute methods must not unregister themselves or their parent device
625 * (which would amount to the same thing). Attempts to do so will deadlock,
626 * since unregistration is mutually exclusive with driver callbacks.
627 *
628 * Instead methods can call this routine, which will attempt to allocate
629 * and schedule a workqueue request to call back @func with @dev as its
630 * argument in the workqueue's process context. @dev will be pinned until
631 * @func returns.
632 *
Alan Stern523ded72007-04-26 00:12:04 -0700633 * This routine is usually called via the inline device_schedule_callback(),
634 * which automatically sets @owner to THIS_MODULE.
635 *
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400636 * Returns 0 if the request was submitted, -ENOMEM if storage could not
Alan Stern523ded72007-04-26 00:12:04 -0700637 * be allocated, -ENODEV if a reference to @owner isn't available.
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400638 *
639 * NOTE: This routine won't work if CONFIG_SYSFS isn't set! It uses an
640 * underlying sysfs routine (since it is intended for use by attribute
641 * methods), and if sysfs isn't available you'll get nothing but -ENOSYS.
642 */
Alan Stern523ded72007-04-26 00:12:04 -0700643int device_schedule_callback_owner(struct device *dev,
644 void (*func)(struct device *), struct module *owner)
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400645{
646 return sysfs_schedule_callback(&dev->kobj,
Alan Stern523ded72007-04-26 00:12:04 -0700647 (void (*)(void *)) func, dev, owner);
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400648}
Alan Stern523ded72007-04-26 00:12:04 -0700649EXPORT_SYMBOL_GPL(device_schedule_callback_owner);
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400650
James Bottomley34bb61f2005-09-06 16:56:51 -0700651static void klist_children_get(struct klist_node *n)
652{
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -0800653 struct device_private *p = to_device_private_parent(n);
654 struct device *dev = p->device;
James Bottomley34bb61f2005-09-06 16:56:51 -0700655
656 get_device(dev);
657}
658
659static void klist_children_put(struct klist_node *n)
660{
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -0800661 struct device_private *p = to_device_private_parent(n);
662 struct device *dev = p->device;
James Bottomley34bb61f2005-09-06 16:56:51 -0700663
664 put_device(dev);
665}
666
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800668 * device_initialize - init device structure.
669 * @dev: device.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 *
Cornelia Huck57394112008-09-03 18:26:40 +0200671 * This prepares the device for use by other layers by initializing
672 * its fields.
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800673 * It is the first half of device_register(), if called by
Cornelia Huck57394112008-09-03 18:26:40 +0200674 * that function, though it can also be called separately, so one
675 * may use @dev's fields. In particular, get_device()/put_device()
676 * may be used for reference counting of @dev after calling this
677 * function.
678 *
Alan Sternb10d5ef2012-01-17 11:39:00 -0500679 * All fields in @dev must be initialized by the caller to 0, except
680 * for those explicitly set to some other value. The simplest
681 * approach is to use kzalloc() to allocate the structure containing
682 * @dev.
683 *
Cornelia Huck57394112008-09-03 18:26:40 +0200684 * NOTE: Use put_device() to give up your reference instead of freeing
685 * @dev directly once you have called this function.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687void device_initialize(struct device *dev)
688{
Greg Kroah-Hartman881c6cfd2007-11-01 09:29:06 -0600689 dev->kobj.kset = devices_kset;
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -0700690 kobject_init(&dev->kobj, &device_ktype);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 INIT_LIST_HEAD(&dev->dma_pools);
Thomas Gleixner31427882010-01-29 20:39:02 +0000692 mutex_init(&dev->mutex);
Peter Zijlstra1704f472010-03-19 01:37:42 +0100693 lockdep_set_novalidate_class(&dev->mutex);
Tejun Heo9ac78492007-01-20 16:00:26 +0900694 spin_lock_init(&dev->devres_lock);
695 INIT_LIST_HEAD(&dev->devres_head);
Alan Stern3b98aea2008-08-07 13:06:12 -0400696 device_pm_init(dev);
Christoph Hellwig87348132006-12-06 20:32:33 -0800697 set_dev_node(dev, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698}
699
Kay Sievers86406242007-03-14 03:25:56 +0100700static struct kobject *virtual_device_parent(struct device *dev)
Greg Kroah-Hartmanf0ee61a2006-10-23 10:40:54 -0700701{
Kay Sievers86406242007-03-14 03:25:56 +0100702 static struct kobject *virtual_dir = NULL;
Greg Kroah-Hartmanf0ee61a2006-10-23 10:40:54 -0700703
Kay Sievers86406242007-03-14 03:25:56 +0100704 if (!virtual_dir)
Greg Kroah-Hartman4ff6abf2007-11-05 22:24:43 -0800705 virtual_dir = kobject_create_and_add("virtual",
Greg Kroah-Hartman881c6cfd2007-11-01 09:29:06 -0600706 &devices_kset->kobj);
Greg Kroah-Hartmanf0ee61a2006-10-23 10:40:54 -0700707
Kay Sievers86406242007-03-14 03:25:56 +0100708 return virtual_dir;
Greg Kroah-Hartmanf0ee61a2006-10-23 10:40:54 -0700709}
710
Eric W. Biedermanbc451f22010-03-30 11:31:25 -0700711struct class_dir {
712 struct kobject kobj;
713 struct class *class;
714};
715
716#define to_class_dir(obj) container_of(obj, struct class_dir, kobj)
717
718static void class_dir_release(struct kobject *kobj)
719{
720 struct class_dir *dir = to_class_dir(kobj);
721 kfree(dir);
722}
723
724static const
725struct kobj_ns_type_operations *class_dir_child_ns_type(struct kobject *kobj)
726{
727 struct class_dir *dir = to_class_dir(kobj);
728 return dir->class->ns_type;
729}
730
731static struct kobj_type class_dir_ktype = {
732 .release = class_dir_release,
733 .sysfs_ops = &kobj_sysfs_ops,
734 .child_ns_type = class_dir_child_ns_type
735};
736
737static struct kobject *
738class_dir_create_and_add(struct class *class, struct kobject *parent_kobj)
739{
740 struct class_dir *dir;
741 int retval;
742
743 dir = kzalloc(sizeof(*dir), GFP_KERNEL);
744 if (!dir)
745 return NULL;
746
747 dir->class = class;
748 kobject_init(&dir->kobj, &class_dir_ktype);
749
Kay Sievers6b6e39a2010-11-15 23:13:18 +0100750 dir->kobj.kset = &class->p->glue_dirs;
Eric W. Biedermanbc451f22010-03-30 11:31:25 -0700751
752 retval = kobject_add(&dir->kobj, parent_kobj, "%s", class->name);
753 if (retval < 0) {
754 kobject_put(&dir->kobj);
755 return NULL;
756 }
757 return &dir->kobj;
758}
759
760
Kay Sieversda231fd2007-11-21 17:29:15 +0100761static struct kobject *get_device_parent(struct device *dev,
762 struct device *parent)
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +0100763{
Kay Sievers86406242007-03-14 03:25:56 +0100764 if (dev->class) {
Tejun Heo77d3d7c2010-02-05 17:57:02 +0900765 static DEFINE_MUTEX(gdp_mutex);
Kay Sievers86406242007-03-14 03:25:56 +0100766 struct kobject *kobj = NULL;
767 struct kobject *parent_kobj;
768 struct kobject *k;
769
Randy Dunlapead454f2010-09-24 14:36:49 -0700770#ifdef CONFIG_BLOCK
Kay Sievers39aba962010-09-04 22:33:14 -0700771 /* block disks show up in /sys/block */
Andi Kleene52eec12010-09-08 16:54:17 +0200772 if (sysfs_deprecated && dev->class == &block_class) {
Kay Sievers39aba962010-09-04 22:33:14 -0700773 if (parent && parent->class == &block_class)
774 return &parent->kobj;
Kay Sievers6b6e39a2010-11-15 23:13:18 +0100775 return &block_class.p->subsys.kobj;
Kay Sievers39aba962010-09-04 22:33:14 -0700776 }
Randy Dunlapead454f2010-09-24 14:36:49 -0700777#endif
Andi Kleene52eec12010-09-08 16:54:17 +0200778
Kay Sievers86406242007-03-14 03:25:56 +0100779 /*
780 * If we have no parent, we live in "virtual".
Kay Sievers0f4dafc2007-12-19 01:40:42 +0100781 * Class-devices with a non class-device as parent, live
782 * in a "glue" directory to prevent namespace collisions.
Kay Sievers86406242007-03-14 03:25:56 +0100783 */
784 if (parent == NULL)
785 parent_kobj = virtual_device_parent(dev);
Eric W. Biederman24b14422010-07-24 22:43:35 -0700786 else if (parent->class && !dev->class->ns_type)
Kay Sievers86406242007-03-14 03:25:56 +0100787 return &parent->kobj;
788 else
789 parent_kobj = &parent->kobj;
790
Tejun Heo77d3d7c2010-02-05 17:57:02 +0900791 mutex_lock(&gdp_mutex);
792
Kay Sievers86406242007-03-14 03:25:56 +0100793 /* find our class-directory at the parent and reference it */
Kay Sievers6b6e39a2010-11-15 23:13:18 +0100794 spin_lock(&dev->class->p->glue_dirs.list_lock);
795 list_for_each_entry(k, &dev->class->p->glue_dirs.list, entry)
Kay Sievers86406242007-03-14 03:25:56 +0100796 if (k->parent == parent_kobj) {
797 kobj = kobject_get(k);
798 break;
799 }
Kay Sievers6b6e39a2010-11-15 23:13:18 +0100800 spin_unlock(&dev->class->p->glue_dirs.list_lock);
Tejun Heo77d3d7c2010-02-05 17:57:02 +0900801 if (kobj) {
802 mutex_unlock(&gdp_mutex);
Kay Sievers86406242007-03-14 03:25:56 +0100803 return kobj;
Tejun Heo77d3d7c2010-02-05 17:57:02 +0900804 }
Kay Sievers86406242007-03-14 03:25:56 +0100805
806 /* or create a new class-directory at the parent device */
Eric W. Biedermanbc451f22010-03-30 11:31:25 -0700807 k = class_dir_create_and_add(dev->class, parent_kobj);
Kay Sievers0f4dafc2007-12-19 01:40:42 +0100808 /* do not emit an uevent for this simple "glue" directory */
Tejun Heo77d3d7c2010-02-05 17:57:02 +0900809 mutex_unlock(&gdp_mutex);
Greg Kroah-Hartman43968d22007-11-05 22:24:43 -0800810 return k;
Kay Sievers86406242007-03-14 03:25:56 +0100811 }
812
Kay Sieversca22e562011-12-14 14:29:38 -0800813 /* subsystems can specify a default root directory for their devices */
814 if (!parent && dev->bus && dev->bus->dev_root)
815 return &dev->bus->dev_root->kobj;
816
Kay Sievers86406242007-03-14 03:25:56 +0100817 if (parent)
Cornelia Huckc744aeae2007-01-08 20:16:44 +0100818 return &parent->kobj;
819 return NULL;
820}
Kay Sieversda231fd2007-11-21 17:29:15 +0100821
Cornelia Huck63b69712008-01-21 16:09:44 +0100822static void cleanup_glue_dir(struct device *dev, struct kobject *glue_dir)
Kay Sieversda231fd2007-11-21 17:29:15 +0100823{
Kay Sievers0f4dafc2007-12-19 01:40:42 +0100824 /* see if we live in a "glue" directory */
Cornelia Huckc1fe5392008-02-27 15:38:23 +0100825 if (!glue_dir || !dev->class ||
Kay Sievers6b6e39a2010-11-15 23:13:18 +0100826 glue_dir->kset != &dev->class->p->glue_dirs)
Kay Sieversda231fd2007-11-21 17:29:15 +0100827 return;
828
Kay Sievers0f4dafc2007-12-19 01:40:42 +0100829 kobject_put(glue_dir);
Kay Sieversda231fd2007-11-21 17:29:15 +0100830}
Cornelia Huck63b69712008-01-21 16:09:44 +0100831
832static void cleanup_device_parent(struct device *dev)
833{
834 cleanup_glue_dir(dev, dev->kobj.parent);
835}
Kay Sievers86406242007-03-14 03:25:56 +0100836
Cornelia Huck2ee97ca2007-07-18 01:43:47 -0700837static int device_add_class_symlinks(struct device *dev)
838{
839 int error;
840
841 if (!dev->class)
842 return 0;
Kay Sieversda231fd2007-11-21 17:29:15 +0100843
Greg Kroah-Hartman1fbfee62008-05-28 09:28:39 -0700844 error = sysfs_create_link(&dev->kobj,
Kay Sievers6b6e39a2010-11-15 23:13:18 +0100845 &dev->class->p->subsys.kobj,
Cornelia Huck2ee97ca2007-07-18 01:43:47 -0700846 "subsystem");
847 if (error)
848 goto out;
Kay Sieversda231fd2007-11-21 17:29:15 +0100849
Greg Kroah-Hartman4e886c22008-01-27 12:12:43 -0800850 if (dev->parent && device_is_not_partition(dev)) {
Dmitry Torokhov4f01a752007-09-18 22:46:50 -0700851 error = sysfs_create_link(&dev->kobj, &dev->parent->kobj,
852 "device");
853 if (error)
Kay Sievers39aba962010-09-04 22:33:14 -0700854 goto out_subsys;
Cornelia Huck2ee97ca2007-07-18 01:43:47 -0700855 }
Kay Sievers39aba962010-09-04 22:33:14 -0700856
Randy Dunlapead454f2010-09-24 14:36:49 -0700857#ifdef CONFIG_BLOCK
Kay Sievers39aba962010-09-04 22:33:14 -0700858 /* /sys/block has directories and does not need symlinks */
Andi Kleene52eec12010-09-08 16:54:17 +0200859 if (sysfs_deprecated && dev->class == &block_class)
Kay Sievers39aba962010-09-04 22:33:14 -0700860 return 0;
Randy Dunlapead454f2010-09-24 14:36:49 -0700861#endif
Kay Sievers39aba962010-09-04 22:33:14 -0700862
863 /* link in the class directory pointing to the device */
Kay Sievers6b6e39a2010-11-15 23:13:18 +0100864 error = sysfs_create_link(&dev->class->p->subsys.kobj,
Kay Sievers39aba962010-09-04 22:33:14 -0700865 &dev->kobj, dev_name(dev));
866 if (error)
867 goto out_device;
868
Cornelia Huck2ee97ca2007-07-18 01:43:47 -0700869 return 0;
870
Kay Sievers39aba962010-09-04 22:33:14 -0700871out_device:
872 sysfs_remove_link(&dev->kobj, "device");
Kay Sieversda231fd2007-11-21 17:29:15 +0100873
Cornelia Huck2ee97ca2007-07-18 01:43:47 -0700874out_subsys:
875 sysfs_remove_link(&dev->kobj, "subsystem");
876out:
877 return error;
878}
879
880static void device_remove_class_symlinks(struct device *dev)
881{
882 if (!dev->class)
883 return;
Kay Sieversda231fd2007-11-21 17:29:15 +0100884
Greg Kroah-Hartman4e886c22008-01-27 12:12:43 -0800885 if (dev->parent && device_is_not_partition(dev))
Kay Sieversda231fd2007-11-21 17:29:15 +0100886 sysfs_remove_link(&dev->kobj, "device");
Cornelia Huck2ee97ca2007-07-18 01:43:47 -0700887 sysfs_remove_link(&dev->kobj, "subsystem");
Randy Dunlapead454f2010-09-24 14:36:49 -0700888#ifdef CONFIG_BLOCK
Andi Kleene52eec12010-09-08 16:54:17 +0200889 if (sysfs_deprecated && dev->class == &block_class)
Kay Sievers39aba962010-09-04 22:33:14 -0700890 return;
Randy Dunlapead454f2010-09-24 14:36:49 -0700891#endif
Kay Sievers6b6e39a2010-11-15 23:13:18 +0100892 sysfs_delete_link(&dev->class->p->subsys.kobj, &dev->kobj, dev_name(dev));
Cornelia Huck2ee97ca2007-07-18 01:43:47 -0700893}
894
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895/**
Stephen Rothwell413c2392008-05-30 10:16:40 +1000896 * dev_set_name - set a device name
897 * @dev: device
Randy Dunlap46232362008-06-04 21:40:43 -0700898 * @fmt: format string for the device's name
Stephen Rothwell413c2392008-05-30 10:16:40 +1000899 */
900int dev_set_name(struct device *dev, const char *fmt, ...)
901{
902 va_list vargs;
Kay Sievers1fa5ae82009-01-25 15:17:37 +0100903 int err;
Stephen Rothwell413c2392008-05-30 10:16:40 +1000904
905 va_start(vargs, fmt);
Kay Sievers1fa5ae82009-01-25 15:17:37 +0100906 err = kobject_set_name_vargs(&dev->kobj, fmt, vargs);
Stephen Rothwell413c2392008-05-30 10:16:40 +1000907 va_end(vargs);
Kay Sievers1fa5ae82009-01-25 15:17:37 +0100908 return err;
Stephen Rothwell413c2392008-05-30 10:16:40 +1000909}
910EXPORT_SYMBOL_GPL(dev_set_name);
911
912/**
Dan Williamse105b8b2008-04-21 10:51:07 -0700913 * device_to_dev_kobj - select a /sys/dev/ directory for the device
914 * @dev: device
915 *
916 * By default we select char/ for new entries. Setting class->dev_obj
917 * to NULL prevents an entry from being created. class->dev_kobj must
918 * be set (or cleared) before any devices are registered to the class
919 * otherwise device_create_sys_dev_entry() and
Peter Korsgaard0d4e293c2012-04-17 12:12:57 +0200920 * device_remove_sys_dev_entry() will disagree about the presence of
921 * the link.
Dan Williamse105b8b2008-04-21 10:51:07 -0700922 */
923static struct kobject *device_to_dev_kobj(struct device *dev)
924{
925 struct kobject *kobj;
926
927 if (dev->class)
928 kobj = dev->class->dev_kobj;
929 else
930 kobj = sysfs_dev_char_kobj;
931
932 return kobj;
933}
934
935static int device_create_sys_dev_entry(struct device *dev)
936{
937 struct kobject *kobj = device_to_dev_kobj(dev);
938 int error = 0;
939 char devt_str[15];
940
941 if (kobj) {
942 format_dev_t(devt_str, dev->devt);
943 error = sysfs_create_link(kobj, &dev->kobj, devt_str);
944 }
945
946 return error;
947}
948
949static void device_remove_sys_dev_entry(struct device *dev)
950{
951 struct kobject *kobj = device_to_dev_kobj(dev);
952 char devt_str[15];
953
954 if (kobj) {
955 format_dev_t(devt_str, dev->devt);
956 sysfs_remove_link(kobj, devt_str);
957 }
958}
959
Greg Kroah-Hartmanb4028432009-05-11 14:16:57 -0700960int device_private_init(struct device *dev)
961{
962 dev->p = kzalloc(sizeof(*dev->p), GFP_KERNEL);
963 if (!dev->p)
964 return -ENOMEM;
965 dev->p->device = dev;
966 klist_init(&dev->p->klist_children, klist_children_get,
967 klist_children_put);
Greg Kroah-Hartmanef8a3fd2012-03-08 12:17:22 -0800968 INIT_LIST_HEAD(&dev->p->deferred_probe);
Greg Kroah-Hartmanb4028432009-05-11 14:16:57 -0700969 return 0;
970}
971
Dan Williamse105b8b2008-04-21 10:51:07 -0700972/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800973 * device_add - add device to device hierarchy.
974 * @dev: device.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800976 * This is part 2 of device_register(), though may be called
977 * separately _iff_ device_initialize() has been called separately.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 *
Cornelia Huck57394112008-09-03 18:26:40 +0200979 * This adds @dev to the kobject hierarchy via kobject_add(), adds it
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800980 * to the global and sibling lists for the device, then
981 * adds it to the other relevant subsystems of the driver model.
Cornelia Huck57394112008-09-03 18:26:40 +0200982 *
Alan Sternb10d5ef2012-01-17 11:39:00 -0500983 * Do not call this routine or device_register() more than once for
984 * any device structure. The driver model core is not designed to work
985 * with devices that get unregistered and then spring back to life.
986 * (Among other things, it's very hard to guarantee that all references
987 * to the previous incarnation of @dev have been dropped.) Allocate
988 * and register a fresh new struct device instead.
989 *
Cornelia Huck57394112008-09-03 18:26:40 +0200990 * NOTE: _Never_ directly free @dev after calling this function, even
991 * if it returned an error! Always use put_device() to give up your
992 * reference instead.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 */
994int device_add(struct device *dev)
995{
996 struct device *parent = NULL;
Kay Sieversca22e562011-12-14 14:29:38 -0800997 struct kobject *kobj;
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +0200998 struct class_interface *class_intf;
Greg Kroah-Hartmanc906a482008-05-30 10:45:12 -0700999 int error = -EINVAL;
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001000
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 dev = get_device(dev);
Greg Kroah-Hartmanc906a482008-05-30 10:45:12 -07001002 if (!dev)
1003 goto done;
1004
Greg Kroah-Hartmanfb069a52008-12-16 12:23:36 -08001005 if (!dev->p) {
Greg Kroah-Hartmanb4028432009-05-11 14:16:57 -07001006 error = device_private_init(dev);
1007 if (error)
1008 goto done;
Greg Kroah-Hartmanfb069a52008-12-16 12:23:36 -08001009 }
Greg Kroah-Hartmanfb069a52008-12-16 12:23:36 -08001010
Kay Sievers1fa5ae82009-01-25 15:17:37 +01001011 /*
1012 * for statically allocated devices, which should all be converted
1013 * some day, we need to initialize the name. We prevent reading back
1014 * the name, and force the use of dev_name()
1015 */
1016 if (dev->init_name) {
Greg Kroah-Hartmanacc0e902009-06-02 15:39:55 -07001017 dev_set_name(dev, "%s", dev->init_name);
Kay Sievers1fa5ae82009-01-25 15:17:37 +01001018 dev->init_name = NULL;
1019 }
Greg Kroah-Hartmanc906a482008-05-30 10:45:12 -07001020
Kay Sieversca22e562011-12-14 14:29:38 -08001021 /* subsystems can specify simple device enumeration */
1022 if (!dev_name(dev) && dev->bus && dev->bus->dev_name)
1023 dev_set_name(dev, "%s%u", dev->bus->dev_name, dev->id);
1024
Thomas Gleixnere6309e72009-12-10 19:32:49 +00001025 if (!dev_name(dev)) {
1026 error = -EINVAL;
Kay Sievers5c8563d2009-05-28 14:24:07 -07001027 goto name_error;
Thomas Gleixnere6309e72009-12-10 19:32:49 +00001028 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01001030 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
Greg Kroah-Hartmanc205ef42006-08-07 22:19:37 -07001031
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 parent = get_device(dev->parent);
Kay Sieversca22e562011-12-14 14:29:38 -08001033 kobj = get_device_parent(dev, parent);
1034 if (kobj)
1035 dev->kobj.parent = kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036
Yinghai Lu0d358f22008-02-19 03:20:41 -08001037 /* use parent numa_node */
1038 if (parent)
1039 set_dev_node(dev, dev_to_node(parent));
1040
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 /* first, register with generic layer. */
Kay Sievers8a577ff2009-04-18 15:05:45 -07001042 /* we require the name to be set before, and pass NULL */
1043 error = kobject_add(&dev->kobj, dev->kobj.parent, NULL);
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +01001044 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 goto Error;
Kay Sieversa7fd6702005-10-01 14:49:43 +02001046
Brian Walsh37022642006-08-14 22:43:19 -07001047 /* notify platform of device entry */
1048 if (platform_notify)
1049 platform_notify(dev);
1050
Tejun Heoad6a1e12007-06-14 03:45:17 +09001051 error = device_create_file(dev, &uevent_attr);
Cornelia Hucka306eea2006-09-22 11:37:13 +02001052 if (error)
1053 goto attrError;
Kay Sieversa7fd6702005-10-01 14:49:43 +02001054
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001055 if (MAJOR(dev->devt)) {
Tejun Heoad6a1e12007-06-14 03:45:17 +09001056 error = device_create_file(dev, &devt_attr);
1057 if (error)
Cornelia Hucka306eea2006-09-22 11:37:13 +02001058 goto ueventattrError;
Dan Williamse105b8b2008-04-21 10:51:07 -07001059
1060 error = device_create_sys_dev_entry(dev);
1061 if (error)
1062 goto devtattrError;
Kay Sievers2b2af542009-04-30 15:23:42 +02001063
1064 devtmpfs_create_node(dev);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001065 }
1066
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001067 error = device_add_class_symlinks(dev);
1068 if (error)
1069 goto SymlinkError;
Cornelia Huckdc0afa82007-07-09 11:39:18 -07001070 error = device_add_attrs(dev);
1071 if (error)
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07001072 goto AttrsError;
Cornelia Huckdc0afa82007-07-09 11:39:18 -07001073 error = bus_add_device(dev);
1074 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 goto BusError;
Alan Stern3b98aea2008-08-07 13:06:12 -04001076 error = dpm_sysfs_add(dev);
Rafael J. Wysocki57eee3d2008-03-12 00:59:38 +01001077 if (error)
Alan Stern3b98aea2008-08-07 13:06:12 -04001078 goto DPMError;
1079 device_pm_add(dev);
Alan Sternec0676ee2008-12-05 14:10:31 -05001080
1081 /* Notify clients of device addition. This call must come
majianpeng268863f2012-01-11 15:12:06 +00001082 * after dpm_sysfs_add() and before kobject_uevent().
Alan Sternec0676ee2008-12-05 14:10:31 -05001083 */
1084 if (dev->bus)
1085 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
1086 BUS_NOTIFY_ADD_DEVICE, dev);
1087
Cornelia Huck83b5fb4c2007-03-29 11:12:11 +02001088 kobject_uevent(&dev->kobj, KOBJ_ADD);
Alan Stern2023c612009-07-30 15:27:18 -04001089 bus_probe_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 if (parent)
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08001091 klist_add_tail(&dev->p->knode_parent,
1092 &parent->p->klist_children);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093
Greg Kroah-Hartman5d9fd162006-06-22 17:17:32 -07001094 if (dev->class) {
Kay Sieversca22e562011-12-14 14:29:38 -08001095 mutex_lock(&dev->class->p->mutex);
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02001096 /* tie the class to the device */
Tejun Heo5a3ceb82008-08-25 19:50:19 +02001097 klist_add_tail(&dev->knode_class,
Kay Sievers6b6e39a2010-11-15 23:13:18 +01001098 &dev->class->p->klist_devices);
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02001099
1100 /* notify any interfaces that the device is here */
Greg Kroah-Hartman184f1f72008-05-28 09:28:39 -07001101 list_for_each_entry(class_intf,
Kay Sieversca22e562011-12-14 14:29:38 -08001102 &dev->class->p->interfaces, node)
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02001103 if (class_intf->add_dev)
1104 class_intf->add_dev(dev, class_intf);
Kay Sieversca22e562011-12-14 14:29:38 -08001105 mutex_unlock(&dev->class->p->mutex);
Greg Kroah-Hartman5d9fd162006-06-22 17:17:32 -07001106 }
Greg Kroah-Hartmanc906a482008-05-30 10:45:12 -07001107done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 put_device(dev);
1109 return error;
Alan Stern3b98aea2008-08-07 13:06:12 -04001110 DPMError:
Rafael J. Wysocki57eee3d2008-03-12 00:59:38 +01001111 bus_remove_device(dev);
1112 BusError:
James Simmons82f0cf92007-02-21 17:44:51 +00001113 device_remove_attrs(dev);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07001114 AttrsError:
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001115 device_remove_class_symlinks(dev);
1116 SymlinkError:
Tejun Heoad6a1e12007-06-14 03:45:17 +09001117 if (MAJOR(dev->devt))
Kay Sieversad729562009-10-28 19:51:37 +01001118 devtmpfs_delete_node(dev);
1119 if (MAJOR(dev->devt))
Dan Williamse105b8b2008-04-21 10:51:07 -07001120 device_remove_sys_dev_entry(dev);
1121 devtattrError:
1122 if (MAJOR(dev->devt))
Tejun Heoad6a1e12007-06-14 03:45:17 +09001123 device_remove_file(dev, &devt_attr);
Cornelia Hucka306eea2006-09-22 11:37:13 +02001124 ueventattrError:
Tejun Heoad6a1e12007-06-14 03:45:17 +09001125 device_remove_file(dev, &uevent_attr);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001126 attrError:
Kay Sievers312c0042005-11-16 09:00:00 +01001127 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 kobject_del(&dev->kobj);
1129 Error:
Cornelia Huck63b69712008-01-21 16:09:44 +01001130 cleanup_device_parent(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 if (parent)
1132 put_device(parent);
Kay Sievers5c8563d2009-05-28 14:24:07 -07001133name_error:
1134 kfree(dev->p);
1135 dev->p = NULL;
Greg Kroah-Hartmanc906a482008-05-30 10:45:12 -07001136 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137}
1138
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001140 * device_register - register a device with the system.
1141 * @dev: pointer to the device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001143 * This happens in two clean steps - initialize the device
1144 * and add it to the system. The two steps can be called
1145 * separately, but this is the easiest and most common.
1146 * I.e. you should only call the two helpers separately if
1147 * have a clearly defined need to use and refcount the device
1148 * before it is added to the hierarchy.
Cornelia Huck57394112008-09-03 18:26:40 +02001149 *
Alan Sternb10d5ef2012-01-17 11:39:00 -05001150 * For more information, see the kerneldoc for device_initialize()
1151 * and device_add().
1152 *
Cornelia Huck57394112008-09-03 18:26:40 +02001153 * NOTE: _Never_ directly free @dev after calling this function, even
1154 * if it returned an error! Always use put_device() to give up the
1155 * reference initialized in this function instead.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157int device_register(struct device *dev)
1158{
1159 device_initialize(dev);
1160 return device_add(dev);
1161}
1162
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001164 * get_device - increment reference count for device.
1165 * @dev: device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001167 * This simply forwards the call to kobject_get(), though
1168 * we do take care to provide for the case that we get a NULL
1169 * pointer passed in.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001171struct device *get_device(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +02001173 return dev ? kobj_to_dev(kobject_get(&dev->kobj)) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174}
1175
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001177 * put_device - decrement reference count.
1178 * @dev: device in question.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001180void put_device(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181{
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001182 /* might_sleep(); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 if (dev)
1184 kobject_put(&dev->kobj);
1185}
1186
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001188 * device_del - delete device from system.
1189 * @dev: device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001191 * This is the first part of the device unregistration
1192 * sequence. This removes the device from the lists we control
1193 * from here, has it removed from the other driver model
1194 * subsystems it was added to in device_add(), and removes it
1195 * from the kobject hierarchy.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001197 * NOTE: this should be called manually _iff_ device_add() was
1198 * also called manually.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001200void device_del(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001202 struct device *parent = dev->parent;
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02001203 struct class_interface *class_intf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204
Alan Sternec0676ee2008-12-05 14:10:31 -05001205 /* Notify clients of device removal. This call must come
1206 * before dpm_sysfs_remove().
1207 */
1208 if (dev->bus)
1209 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
1210 BUS_NOTIFY_DEL_DEVICE, dev);
Alan Stern3b98aea2008-08-07 13:06:12 -04001211 dpm_sysfs_remove(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 if (parent)
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08001213 klist_del(&dev->p->knode_parent);
Dan Williamse105b8b2008-04-21 10:51:07 -07001214 if (MAJOR(dev->devt)) {
Kay Sievers2b2af542009-04-30 15:23:42 +02001215 devtmpfs_delete_node(dev);
Dan Williamse105b8b2008-04-21 10:51:07 -07001216 device_remove_sys_dev_entry(dev);
Tejun Heoad6a1e12007-06-14 03:45:17 +09001217 device_remove_file(dev, &devt_attr);
Dan Williamse105b8b2008-04-21 10:51:07 -07001218 }
Kay Sieversb9d9c822006-06-15 15:31:56 +02001219 if (dev->class) {
Kay Sieversda231fd2007-11-21 17:29:15 +01001220 device_remove_class_symlinks(dev);
Kay Sievers99ef3ef2006-09-14 11:23:28 +02001221
Kay Sieversca22e562011-12-14 14:29:38 -08001222 mutex_lock(&dev->class->p->mutex);
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02001223 /* notify any interfaces that the device is now gone */
Greg Kroah-Hartman184f1f72008-05-28 09:28:39 -07001224 list_for_each_entry(class_intf,
Kay Sieversca22e562011-12-14 14:29:38 -08001225 &dev->class->p->interfaces, node)
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02001226 if (class_intf->remove_dev)
1227 class_intf->remove_dev(dev, class_intf);
1228 /* remove the device from the class list */
Tejun Heo5a3ceb82008-08-25 19:50:19 +02001229 klist_del(&dev->knode_class);
Kay Sieversca22e562011-12-14 14:29:38 -08001230 mutex_unlock(&dev->class->p->mutex);
Kay Sieversb9d9c822006-06-15 15:31:56 +02001231 }
Tejun Heoad6a1e12007-06-14 03:45:17 +09001232 device_remove_file(dev, &uevent_attr);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07001233 device_remove_attrs(dev);
Benjamin Herrenschmidt28953532006-11-08 19:46:14 -08001234 bus_remove_device(dev);
LongX Zhang4b6d1f122012-10-25 00:21:28 +02001235 device_pm_remove(dev);
Grant Likelyd1c34142012-03-05 08:47:41 -07001236 driver_deferred_probe_del(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237
1238 /* Notify the platform of the removal, in case they
1239 * need to do anything...
1240 */
1241 if (platform_notify_remove)
1242 platform_notify_remove(dev);
Kay Sievers312c0042005-11-16 09:00:00 +01001243 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
Kay Sieversda231fd2007-11-21 17:29:15 +01001244 cleanup_device_parent(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 kobject_del(&dev->kobj);
Kay Sieversda231fd2007-11-21 17:29:15 +01001246 put_device(parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247}
1248
1249/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001250 * device_unregister - unregister device from system.
1251 * @dev: device going away.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001253 * We do this in two parts, like we do device_register(). First,
1254 * we remove it from all the subsystems with device_del(), then
1255 * we decrement the reference count via put_device(). If that
1256 * is the final reference count, the device will be cleaned up
1257 * via device_release() above. Otherwise, the structure will
1258 * stick around until the final reference to the device is dropped.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001260void device_unregister(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261{
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01001262 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 device_del(dev);
1264 put_device(dev);
1265}
1266
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001267static struct device *next_device(struct klist_iter *i)
mochel@digitalimplant.org36239572005-03-24 19:08:30 -08001268{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001269 struct klist_node *n = klist_next(i);
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08001270 struct device *dev = NULL;
1271 struct device_private *p;
1272
1273 if (n) {
1274 p = to_device_private_parent(n);
1275 dev = p->device;
1276 }
1277 return dev;
mochel@digitalimplant.org36239572005-03-24 19:08:30 -08001278}
1279
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280/**
Kay Sieverse454cea2009-09-18 23:01:12 +02001281 * device_get_devnode - path of device node file
Kay Sievers6fcf53a2009-04-30 15:23:42 +02001282 * @dev: device
Kay Sieverse454cea2009-09-18 23:01:12 +02001283 * @mode: returned file access mode
Kay Sievers6fcf53a2009-04-30 15:23:42 +02001284 * @tmp: possibly allocated string
1285 *
1286 * Return the relative path of a possible device node.
1287 * Non-default names may need to allocate a memory to compose
1288 * a name. This memory is returned in tmp and needs to be
1289 * freed by the caller.
1290 */
Kay Sieverse454cea2009-09-18 23:01:12 +02001291const char *device_get_devnode(struct device *dev,
Al Viro2c9ede52011-07-23 20:24:48 -04001292 umode_t *mode, const char **tmp)
Kay Sievers6fcf53a2009-04-30 15:23:42 +02001293{
1294 char *s;
1295
1296 *tmp = NULL;
1297
1298 /* the device type may provide a specific name */
Kay Sieverse454cea2009-09-18 23:01:12 +02001299 if (dev->type && dev->type->devnode)
1300 *tmp = dev->type->devnode(dev, mode);
Kay Sievers6fcf53a2009-04-30 15:23:42 +02001301 if (*tmp)
1302 return *tmp;
1303
1304 /* the class may provide a specific name */
Kay Sieverse454cea2009-09-18 23:01:12 +02001305 if (dev->class && dev->class->devnode)
1306 *tmp = dev->class->devnode(dev, mode);
Kay Sievers6fcf53a2009-04-30 15:23:42 +02001307 if (*tmp)
1308 return *tmp;
1309
1310 /* return name without allocation, tmp == NULL */
1311 if (strchr(dev_name(dev), '!') == NULL)
1312 return dev_name(dev);
1313
1314 /* replace '!' in the name with '/' */
1315 *tmp = kstrdup(dev_name(dev), GFP_KERNEL);
1316 if (!*tmp)
1317 return NULL;
1318 while ((s = strchr(*tmp, '!')))
1319 s[0] = '/';
1320 return *tmp;
1321}
1322
1323/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001324 * device_for_each_child - device child iterator.
1325 * @parent: parent struct device.
1326 * @data: data for the callback.
1327 * @fn: function to be called for each device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001329 * Iterate over @parent's child devices, and call @fn for each,
1330 * passing it @data.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001332 * We check the return of @fn each time. If it returns anything
1333 * other than 0, we break out and return that value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001335int device_for_each_child(struct device *parent, void *data,
1336 int (*fn)(struct device *dev, void *data))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337{
mochel@digitalimplant.org36239572005-03-24 19:08:30 -08001338 struct klist_iter i;
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001339 struct device *child;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 int error = 0;
1341
Greg Kroah-Hartman014c90db2009-04-15 16:00:12 -07001342 if (!parent->p)
1343 return 0;
1344
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08001345 klist_iter_init(&parent->p->klist_children, &i);
mochel@digitalimplant.org36239572005-03-24 19:08:30 -08001346 while ((child = next_device(&i)) && !error)
1347 error = fn(child, data);
1348 klist_iter_exit(&i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 return error;
1350}
1351
Cornelia Huck5ab69982006-11-16 15:42:07 +01001352/**
1353 * device_find_child - device iterator for locating a particular device.
1354 * @parent: parent struct device
1355 * @data: Data to pass to match function
1356 * @match: Callback function to check device
1357 *
1358 * This is similar to the device_for_each_child() function above, but it
1359 * returns a reference to a device that is 'found' for later use, as
1360 * determined by the @match callback.
1361 *
1362 * The callback should return 0 if the device doesn't match and non-zero
1363 * if it does. If the callback returns non-zero and a reference to the
1364 * current device can be obtained, this function will return to the caller
1365 * and not iterate over any more devices.
1366 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001367struct device *device_find_child(struct device *parent, void *data,
1368 int (*match)(struct device *dev, void *data))
Cornelia Huck5ab69982006-11-16 15:42:07 +01001369{
1370 struct klist_iter i;
1371 struct device *child;
1372
1373 if (!parent)
1374 return NULL;
1375
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08001376 klist_iter_init(&parent->p->klist_children, &i);
Cornelia Huck5ab69982006-11-16 15:42:07 +01001377 while ((child = next_device(&i)))
1378 if (match(child, data) && get_device(child))
1379 break;
1380 klist_iter_exit(&i);
1381 return child;
1382}
1383
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384int __init devices_init(void)
1385{
Greg Kroah-Hartman881c6cfd2007-11-01 09:29:06 -06001386 devices_kset = kset_create_and_add("devices", &device_uevent_ops, NULL);
1387 if (!devices_kset)
1388 return -ENOMEM;
Dan Williamse105b8b2008-04-21 10:51:07 -07001389 dev_kobj = kobject_create_and_add("dev", NULL);
1390 if (!dev_kobj)
1391 goto dev_kobj_err;
1392 sysfs_dev_block_kobj = kobject_create_and_add("block", dev_kobj);
1393 if (!sysfs_dev_block_kobj)
1394 goto block_kobj_err;
1395 sysfs_dev_char_kobj = kobject_create_and_add("char", dev_kobj);
1396 if (!sysfs_dev_char_kobj)
1397 goto char_kobj_err;
1398
Greg Kroah-Hartman881c6cfd2007-11-01 09:29:06 -06001399 return 0;
Dan Williamse105b8b2008-04-21 10:51:07 -07001400
1401 char_kobj_err:
1402 kobject_put(sysfs_dev_block_kobj);
1403 block_kobj_err:
1404 kobject_put(dev_kobj);
1405 dev_kobj_err:
1406 kset_unregister(devices_kset);
1407 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408}
1409
1410EXPORT_SYMBOL_GPL(device_for_each_child);
Cornelia Huck5ab69982006-11-16 15:42:07 +01001411EXPORT_SYMBOL_GPL(device_find_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412
1413EXPORT_SYMBOL_GPL(device_initialize);
1414EXPORT_SYMBOL_GPL(device_add);
1415EXPORT_SYMBOL_GPL(device_register);
1416
1417EXPORT_SYMBOL_GPL(device_del);
1418EXPORT_SYMBOL_GPL(device_unregister);
1419EXPORT_SYMBOL_GPL(get_device);
1420EXPORT_SYMBOL_GPL(put_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421
1422EXPORT_SYMBOL_GPL(device_create_file);
1423EXPORT_SYMBOL_GPL(device_remove_file);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001424
Karthigan Srinivasan7f100d12011-04-18 16:16:52 -05001425struct root_device {
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00001426 struct device dev;
1427 struct module *owner;
1428};
1429
Josh Triplett93058422012-11-18 21:27:55 -08001430static inline struct root_device *to_root_device(struct device *d)
Ferenc Wagner481e2072011-01-07 15:17:47 +01001431{
1432 return container_of(d, struct root_device, dev);
1433}
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00001434
1435static void root_device_release(struct device *dev)
1436{
1437 kfree(to_root_device(dev));
1438}
1439
1440/**
1441 * __root_device_register - allocate and register a root device
1442 * @name: root device name
1443 * @owner: owner module of the root device, usually THIS_MODULE
1444 *
1445 * This function allocates a root device and registers it
1446 * using device_register(). In order to free the returned
1447 * device, use root_device_unregister().
1448 *
1449 * Root devices are dummy devices which allow other devices
1450 * to be grouped under /sys/devices. Use this function to
1451 * allocate a root device and then use it as the parent of
1452 * any device which should appear under /sys/devices/{name}
1453 *
1454 * The /sys/devices/{name} directory will also contain a
1455 * 'module' symlink which points to the @owner directory
1456 * in sysfs.
1457 *
Jani Nikulaf0eae0e2010-03-11 18:11:45 +02001458 * Returns &struct device pointer on success, or ERR_PTR() on error.
1459 *
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00001460 * Note: You probably want to use root_device_register().
1461 */
1462struct device *__root_device_register(const char *name, struct module *owner)
1463{
1464 struct root_device *root;
1465 int err = -ENOMEM;
1466
1467 root = kzalloc(sizeof(struct root_device), GFP_KERNEL);
1468 if (!root)
1469 return ERR_PTR(err);
1470
Greg Kroah-Hartmanacc0e902009-06-02 15:39:55 -07001471 err = dev_set_name(&root->dev, "%s", name);
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00001472 if (err) {
1473 kfree(root);
1474 return ERR_PTR(err);
1475 }
1476
1477 root->dev.release = root_device_release;
1478
1479 err = device_register(&root->dev);
1480 if (err) {
1481 put_device(&root->dev);
1482 return ERR_PTR(err);
1483 }
1484
Christoph Egger1d9e8822010-05-17 16:57:58 +02001485#ifdef CONFIG_MODULES /* gotta find a "cleaner" way to do this */
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00001486 if (owner) {
1487 struct module_kobject *mk = &owner->mkobj;
1488
1489 err = sysfs_create_link(&root->dev.kobj, &mk->kobj, "module");
1490 if (err) {
1491 device_unregister(&root->dev);
1492 return ERR_PTR(err);
1493 }
1494 root->owner = owner;
1495 }
1496#endif
1497
1498 return &root->dev;
1499}
1500EXPORT_SYMBOL_GPL(__root_device_register);
1501
1502/**
1503 * root_device_unregister - unregister and free a root device
Randy Dunlap7cbcf222009-01-20 16:29:13 -08001504 * @dev: device going away
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00001505 *
1506 * This function unregisters and cleans up a device that was created by
1507 * root_device_register().
1508 */
1509void root_device_unregister(struct device *dev)
1510{
1511 struct root_device *root = to_root_device(dev);
1512
1513 if (root->owner)
1514 sysfs_remove_link(&root->dev.kobj, "module");
1515
1516 device_unregister(dev);
1517}
1518EXPORT_SYMBOL_GPL(root_device_unregister);
1519
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001520
1521static void device_create_release(struct device *dev)
1522{
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01001523 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001524 kfree(dev);
1525}
1526
1527/**
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07001528 * device_create_vargs - creates a device and registers it with sysfs
1529 * @class: pointer to the struct class that this device should be registered to
1530 * @parent: pointer to the parent struct device of this new device, if any
1531 * @devt: the dev_t for the char device to be added
1532 * @drvdata: the data to be added to the device for callbacks
1533 * @fmt: string for the device's name
1534 * @args: va_list for the device's name
1535 *
1536 * This function can be used by char device classes. A struct device
1537 * will be created in sysfs, registered to the specified class.
1538 *
1539 * A "dev" file will be created, showing the dev_t for the device, if
1540 * the dev_t is not 0,0.
1541 * If a pointer to a parent struct device is passed in, the newly created
1542 * struct device will be a child of that device in sysfs.
1543 * The pointer to the struct device will be returned from the call.
1544 * Any further sysfs files that might be required can be created using this
1545 * pointer.
1546 *
Jani Nikulaf0eae0e2010-03-11 18:11:45 +02001547 * Returns &struct device pointer on success, or ERR_PTR() on error.
1548 *
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07001549 * Note: the struct class passed to this function must have previously
1550 * been created with a call to class_create().
1551 */
1552struct device *device_create_vargs(struct class *class, struct device *parent,
1553 dev_t devt, void *drvdata, const char *fmt,
1554 va_list args)
1555{
1556 struct device *dev = NULL;
1557 int retval = -ENODEV;
1558
1559 if (class == NULL || IS_ERR(class))
1560 goto error;
1561
1562 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1563 if (!dev) {
1564 retval = -ENOMEM;
1565 goto error;
1566 }
1567
1568 dev->devt = devt;
1569 dev->class = class;
1570 dev->parent = parent;
1571 dev->release = device_create_release;
1572 dev_set_drvdata(dev, drvdata);
1573
Kay Sievers1fa5ae82009-01-25 15:17:37 +01001574 retval = kobject_set_name_vargs(&dev->kobj, fmt, args);
1575 if (retval)
1576 goto error;
1577
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07001578 retval = device_register(dev);
1579 if (retval)
1580 goto error;
1581
1582 return dev;
1583
1584error:
Cornelia Huck286661b2008-09-03 18:26:41 +02001585 put_device(dev);
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07001586 return ERR_PTR(retval);
1587}
1588EXPORT_SYMBOL_GPL(device_create_vargs);
1589
1590/**
Greg Kroah-Hartman4e106732008-07-21 20:03:34 -07001591 * device_create - creates a device and registers it with sysfs
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07001592 * @class: pointer to the struct class that this device should be registered to
1593 * @parent: pointer to the parent struct device of this new device, if any
1594 * @devt: the dev_t for the char device to be added
1595 * @drvdata: the data to be added to the device for callbacks
1596 * @fmt: string for the device's name
1597 *
1598 * This function can be used by char device classes. A struct device
1599 * will be created in sysfs, registered to the specified class.
1600 *
1601 * A "dev" file will be created, showing the dev_t for the device, if
1602 * the dev_t is not 0,0.
1603 * If a pointer to a parent struct device is passed in, the newly created
1604 * struct device will be a child of that device in sysfs.
1605 * The pointer to the struct device will be returned from the call.
1606 * Any further sysfs files that might be required can be created using this
1607 * pointer.
1608 *
Jani Nikulaf0eae0e2010-03-11 18:11:45 +02001609 * Returns &struct device pointer on success, or ERR_PTR() on error.
1610 *
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07001611 * Note: the struct class passed to this function must have previously
1612 * been created with a call to class_create().
1613 */
Greg Kroah-Hartman4e106732008-07-21 20:03:34 -07001614struct device *device_create(struct class *class, struct device *parent,
1615 dev_t devt, void *drvdata, const char *fmt, ...)
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07001616{
1617 va_list vargs;
1618 struct device *dev;
1619
1620 va_start(vargs, fmt);
1621 dev = device_create_vargs(class, parent, devt, drvdata, fmt, vargs);
1622 va_end(vargs);
1623 return dev;
1624}
Greg Kroah-Hartman4e106732008-07-21 20:03:34 -07001625EXPORT_SYMBOL_GPL(device_create);
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07001626
Michał Mirosław9f3b7952013-02-01 20:40:17 +01001627static int __match_devt(struct device *dev, const void *data)
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001628{
Michał Mirosław9f3b7952013-02-01 20:40:17 +01001629 const dev_t *devt = data;
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001630
Dave Youngcd354492008-01-28 16:56:11 +08001631 return dev->devt == *devt;
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001632}
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001633
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001634/**
1635 * device_destroy - removes a device that was created with device_create()
1636 * @class: pointer to the struct class that this device was registered with
1637 * @devt: the dev_t of the device that was previously registered
1638 *
1639 * This call unregisters and cleans up a device that was created with a
1640 * call to device_create().
1641 */
1642void device_destroy(struct class *class, dev_t devt)
1643{
1644 struct device *dev;
1645
Greg Kroah-Hartman695794a2008-05-22 17:21:08 -04001646 dev = class_find_device(class, NULL, &devt, __match_devt);
Dave Youngcd354492008-01-28 16:56:11 +08001647 if (dev) {
1648 put_device(dev);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001649 device_unregister(dev);
Dave Youngcd354492008-01-28 16:56:11 +08001650 }
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001651}
1652EXPORT_SYMBOL_GPL(device_destroy);
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001653
1654/**
1655 * device_rename - renames a device
1656 * @dev: the pointer to the struct device to be renamed
1657 * @new_name: the new name of the device
Eric W. Biederman030c1d22008-05-08 14:41:00 -07001658 *
1659 * It is the responsibility of the caller to provide mutual
1660 * exclusion between two different calls of device_rename
1661 * on the same device to ensure that new_name is valid and
1662 * won't conflict with other devices.
Michael Ellermanc6c0ac62010-11-25 09:44:07 +11001663 *
Timur Tabia5462512010-12-13 14:08:52 -06001664 * Note: Don't call this function. Currently, the networking layer calls this
1665 * function, but that will change. The following text from Kay Sievers offers
1666 * some insight:
1667 *
1668 * Renaming devices is racy at many levels, symlinks and other stuff are not
1669 * replaced atomically, and you get a "move" uevent, but it's not easy to
1670 * connect the event to the old and new device. Device nodes are not renamed at
1671 * all, there isn't even support for that in the kernel now.
1672 *
1673 * In the meantime, during renaming, your target name might be taken by another
1674 * driver, creating conflicts. Or the old name is taken directly after you
1675 * renamed it -- then you get events for the same DEVPATH, before you even see
1676 * the "move" event. It's just a mess, and nothing new should ever rely on
1677 * kernel device renaming. Besides that, it's not even implemented now for
1678 * other things than (driver-core wise very simple) network devices.
1679 *
1680 * We are currently about to change network renaming in udev to completely
1681 * disallow renaming of devices in the same namespace as the kernel uses,
1682 * because we can't solve the problems properly, that arise with swapping names
1683 * of multiple interfaces without races. Means, renaming of eth[0-9]* will only
1684 * be allowed to some other name than eth[0-9]*, for the aforementioned
1685 * reasons.
1686 *
1687 * Make up a "real" name in the driver before you register anything, or add
1688 * some other attributes for userspace to find the device, or use udev to add
1689 * symlinks -- but never rename kernel devices later, it's a complete mess. We
1690 * don't even want to get into that and try to implement the missing pieces in
1691 * the core. We really have other pieces to fix in the driver core mess. :)
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001692 */
Johannes Berg6937e8f2010-08-05 17:38:18 +02001693int device_rename(struct device *dev, const char *new_name)
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001694{
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001695 char *old_device_name = NULL;
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001696 int error;
1697
1698 dev = get_device(dev);
1699 if (!dev)
1700 return -EINVAL;
1701
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01001702 pr_debug("device: '%s': %s: renaming to '%s'\n", dev_name(dev),
Harvey Harrison2b3a3022008-03-04 16:41:05 -08001703 __func__, new_name);
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001704
Kay Sievers1fa5ae82009-01-25 15:17:37 +01001705 old_device_name = kstrdup(dev_name(dev), GFP_KERNEL);
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001706 if (!old_device_name) {
1707 error = -ENOMEM;
1708 goto out;
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001709 }
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001710
Eric W. Biedermanf349cf32010-03-30 11:31:29 -07001711 if (dev->class) {
Kay Sievers6b6e39a2010-11-15 23:13:18 +01001712 error = sysfs_rename_link(&dev->class->p->subsys.kobj,
Eric W. Biedermanf349cf32010-03-30 11:31:29 -07001713 &dev->kobj, old_device_name, new_name);
1714 if (error)
1715 goto out;
1716 }
Kay Sievers39aba962010-09-04 22:33:14 -07001717
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001718 error = kobject_rename(&dev->kobj, new_name);
Kay Sievers1fa5ae82009-01-25 15:17:37 +01001719 if (error)
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001720 goto out;
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001721
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001722out:
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001723 put_device(dev);
1724
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001725 kfree(old_device_name);
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001726
1727 return error;
1728}
Johannes Berga2807db2007-02-28 12:38:31 +01001729EXPORT_SYMBOL_GPL(device_rename);
Cornelia Huck8a824722006-11-20 17:07:51 +01001730
1731static int device_move_class_links(struct device *dev,
1732 struct device *old_parent,
1733 struct device *new_parent)
1734{
Greg Kroah-Hartmanf7f34612007-03-06 12:55:53 -08001735 int error = 0;
Cornelia Huck8a824722006-11-20 17:07:51 +01001736
Greg Kroah-Hartmanf7f34612007-03-06 12:55:53 -08001737 if (old_parent)
1738 sysfs_remove_link(&dev->kobj, "device");
1739 if (new_parent)
1740 error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
1741 "device");
1742 return error;
Cornelia Huck8a824722006-11-20 17:07:51 +01001743}
1744
1745/**
1746 * device_move - moves a device to a new parent
1747 * @dev: the pointer to the struct device to be moved
Cornelia Huckc744aeae2007-01-08 20:16:44 +01001748 * @new_parent: the new parent of the device (can by NULL)
Cornelia Huckffa6a702009-03-04 12:44:00 +01001749 * @dpm_order: how to reorder the dpm_list
Cornelia Huck8a824722006-11-20 17:07:51 +01001750 */
Cornelia Huckffa6a702009-03-04 12:44:00 +01001751int device_move(struct device *dev, struct device *new_parent,
1752 enum dpm_order dpm_order)
Cornelia Huck8a824722006-11-20 17:07:51 +01001753{
1754 int error;
1755 struct device *old_parent;
Cornelia Huckc744aeae2007-01-08 20:16:44 +01001756 struct kobject *new_parent_kobj;
Cornelia Huck8a824722006-11-20 17:07:51 +01001757
1758 dev = get_device(dev);
1759 if (!dev)
1760 return -EINVAL;
1761
Cornelia Huckffa6a702009-03-04 12:44:00 +01001762 device_pm_lock();
Cornelia Huck8a824722006-11-20 17:07:51 +01001763 new_parent = get_device(new_parent);
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001764 new_parent_kobj = get_device_parent(dev, new_parent);
Cornelia Huck63b69712008-01-21 16:09:44 +01001765
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01001766 pr_debug("device: '%s': %s: moving to '%s'\n", dev_name(dev),
1767 __func__, new_parent ? dev_name(new_parent) : "<NULL>");
Cornelia Huckc744aeae2007-01-08 20:16:44 +01001768 error = kobject_move(&dev->kobj, new_parent_kobj);
Cornelia Huck8a824722006-11-20 17:07:51 +01001769 if (error) {
Cornelia Huck63b69712008-01-21 16:09:44 +01001770 cleanup_glue_dir(dev, new_parent_kobj);
Cornelia Huck8a824722006-11-20 17:07:51 +01001771 put_device(new_parent);
1772 goto out;
1773 }
1774 old_parent = dev->parent;
1775 dev->parent = new_parent;
1776 if (old_parent)
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08001777 klist_remove(&dev->p->knode_parent);
Yinghai Lu0d358f22008-02-19 03:20:41 -08001778 if (new_parent) {
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08001779 klist_add_tail(&dev->p->knode_parent,
1780 &new_parent->p->klist_children);
Yinghai Lu0d358f22008-02-19 03:20:41 -08001781 set_dev_node(dev, dev_to_node(new_parent));
1782 }
1783
Rabin Vincentbdd40342012-04-23 09:16:36 +02001784 if (dev->class) {
1785 error = device_move_class_links(dev, old_parent, new_parent);
1786 if (error) {
1787 /* We ignore errors on cleanup since we're hosed anyway... */
1788 device_move_class_links(dev, new_parent, old_parent);
1789 if (!kobject_move(&dev->kobj, &old_parent->kobj)) {
1790 if (new_parent)
1791 klist_remove(&dev->p->knode_parent);
1792 dev->parent = old_parent;
1793 if (old_parent) {
1794 klist_add_tail(&dev->p->knode_parent,
1795 &old_parent->p->klist_children);
1796 set_dev_node(dev, dev_to_node(old_parent));
1797 }
Yinghai Lu0d358f22008-02-19 03:20:41 -08001798 }
Rabin Vincentbdd40342012-04-23 09:16:36 +02001799 cleanup_glue_dir(dev, new_parent_kobj);
1800 put_device(new_parent);
1801 goto out;
Cornelia Huck8a824722006-11-20 17:07:51 +01001802 }
Cornelia Huck8a824722006-11-20 17:07:51 +01001803 }
Cornelia Huckffa6a702009-03-04 12:44:00 +01001804 switch (dpm_order) {
1805 case DPM_ORDER_NONE:
1806 break;
1807 case DPM_ORDER_DEV_AFTER_PARENT:
1808 device_pm_move_after(dev, new_parent);
1809 break;
1810 case DPM_ORDER_PARENT_BEFORE_DEV:
1811 device_pm_move_before(new_parent, dev);
1812 break;
1813 case DPM_ORDER_DEV_LAST:
1814 device_pm_move_last(dev);
1815 break;
1816 }
Rabin Vincentbdd40342012-04-23 09:16:36 +02001817
Cornelia Huck8a824722006-11-20 17:07:51 +01001818 put_device(old_parent);
1819out:
Cornelia Huckffa6a702009-03-04 12:44:00 +01001820 device_pm_unlock();
Cornelia Huck8a824722006-11-20 17:07:51 +01001821 put_device(dev);
1822 return error;
1823}
Cornelia Huck8a824722006-11-20 17:07:51 +01001824EXPORT_SYMBOL_GPL(device_move);
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08001825
1826/**
1827 * device_shutdown - call ->shutdown() on each device to shutdown.
1828 */
1829void device_shutdown(void)
1830{
Hugh Daschbach62458382010-03-22 10:36:37 -07001831 struct device *dev;
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08001832
Hugh Daschbach62458382010-03-22 10:36:37 -07001833 spin_lock(&devices_kset->list_lock);
1834 /*
1835 * Walk the devices list backward, shutting down each in turn.
1836 * Beware that device unplug events may also start pulling
1837 * devices offline, even as the system is shutting down.
1838 */
1839 while (!list_empty(&devices_kset->list)) {
1840 dev = list_entry(devices_kset->list.prev, struct device,
1841 kobj.entry);
Ming Leid1c6c032012-06-22 18:01:40 +08001842
1843 /*
1844 * hold reference count of device's parent to
1845 * prevent it from being freed because parent's
1846 * lock is to be held
1847 */
1848 get_device(dev->parent);
Hugh Daschbach62458382010-03-22 10:36:37 -07001849 get_device(dev);
1850 /*
1851 * Make sure the device is off the kset list, in the
1852 * event that dev->*->shutdown() doesn't remove it.
1853 */
1854 list_del_init(&dev->kobj.entry);
1855 spin_unlock(&devices_kset->list_lock);
Alan Sternfe6b91f2011-12-06 23:24:52 +01001856
Ming Leid1c6c032012-06-22 18:01:40 +08001857 /* hold lock to avoid race with probe/release */
1858 if (dev->parent)
1859 device_lock(dev->parent);
1860 device_lock(dev);
1861
Alan Sternfe6b91f2011-12-06 23:24:52 +01001862 /* Don't allow any more runtime suspends */
1863 pm_runtime_get_noresume(dev);
1864 pm_runtime_barrier(dev);
Hugh Daschbach62458382010-03-22 10:36:37 -07001865
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08001866 if (dev->bus && dev->bus->shutdown) {
ShuoX Liu0246c4f2012-11-23 15:14:12 +08001867 if (initcall_debug)
1868 dev_info(dev, "shutdown\n");
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08001869 dev->bus->shutdown(dev);
1870 } else if (dev->driver && dev->driver->shutdown) {
ShuoX Liu0246c4f2012-11-23 15:14:12 +08001871 if (initcall_debug)
1872 dev_info(dev, "shutdown\n");
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08001873 dev->driver->shutdown(dev);
1874 }
Ming Leid1c6c032012-06-22 18:01:40 +08001875
1876 device_unlock(dev);
1877 if (dev->parent)
1878 device_unlock(dev->parent);
1879
Hugh Daschbach62458382010-03-22 10:36:37 -07001880 put_device(dev);
Ming Leid1c6c032012-06-22 18:01:40 +08001881 put_device(dev->parent);
Hugh Daschbach62458382010-03-22 10:36:37 -07001882
1883 spin_lock(&devices_kset->list_lock);
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08001884 }
Hugh Daschbach62458382010-03-22 10:36:37 -07001885 spin_unlock(&devices_kset->list_lock);
Shaohua Li401097e2009-05-12 13:37:57 -07001886 async_synchronize_full();
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08001887}
Joe Perches99bcf212010-06-27 01:02:34 +00001888
1889/*
1890 * Device logging functions
1891 */
1892
1893#ifdef CONFIG_PRINTK
Joe Perches666f3552012-09-12 20:14:11 -07001894static int
1895create_syslog_header(const struct device *dev, char *hdr, size_t hdrlen)
Joe Perches99bcf212010-06-27 01:02:34 +00001896{
Kay Sieversc4e00da2012-05-03 02:29:59 +02001897 const char *subsys;
Joe Perches798efc62012-09-12 20:11:29 -07001898 size_t pos = 0;
Joe Perches99bcf212010-06-27 01:02:34 +00001899
Kay Sieversc4e00da2012-05-03 02:29:59 +02001900 if (dev->class)
1901 subsys = dev->class->name;
1902 else if (dev->bus)
1903 subsys = dev->bus->name;
1904 else
Joe Perches798efc62012-09-12 20:11:29 -07001905 return 0;
Kay Sieversc4e00da2012-05-03 02:29:59 +02001906
Joe Perches798efc62012-09-12 20:11:29 -07001907 pos += snprintf(hdr + pos, hdrlen - pos, "SUBSYSTEM=%s", subsys);
Kay Sieversc4e00da2012-05-03 02:29:59 +02001908
1909 /*
1910 * Add device identifier DEVICE=:
1911 * b12:8 block dev_t
1912 * c127:3 char dev_t
1913 * n8 netdev ifindex
1914 * +sound:card0 subsystem:devname
1915 */
1916 if (MAJOR(dev->devt)) {
1917 char c;
1918
1919 if (strcmp(subsys, "block") == 0)
1920 c = 'b';
1921 else
1922 c = 'c';
Joe Perches798efc62012-09-12 20:11:29 -07001923 pos++;
1924 pos += snprintf(hdr + pos, hdrlen - pos,
1925 "DEVICE=%c%u:%u",
1926 c, MAJOR(dev->devt), MINOR(dev->devt));
Kay Sieversc4e00da2012-05-03 02:29:59 +02001927 } else if (strcmp(subsys, "net") == 0) {
1928 struct net_device *net = to_net_dev(dev);
1929
Joe Perches798efc62012-09-12 20:11:29 -07001930 pos++;
1931 pos += snprintf(hdr + pos, hdrlen - pos,
1932 "DEVICE=n%u", net->ifindex);
Kay Sieversc4e00da2012-05-03 02:29:59 +02001933 } else {
Joe Perches798efc62012-09-12 20:11:29 -07001934 pos++;
1935 pos += snprintf(hdr + pos, hdrlen - pos,
1936 "DEVICE=+%s:%s", subsys, dev_name(dev));
Kay Sieversc4e00da2012-05-03 02:29:59 +02001937 }
Jim Cromieaf7f2152012-07-19 13:46:21 -06001938
Joe Perches798efc62012-09-12 20:11:29 -07001939 return pos;
Joe Perches99bcf212010-06-27 01:02:34 +00001940}
Joe Perches798efc62012-09-12 20:11:29 -07001941EXPORT_SYMBOL(create_syslog_header);
1942
Joe Perches05e4e5b2012-09-12 20:13:37 -07001943int dev_vprintk_emit(int level, const struct device *dev,
1944 const char *fmt, va_list args)
1945{
1946 char hdr[128];
1947 size_t hdrlen;
1948
1949 hdrlen = create_syslog_header(dev, hdr, sizeof(hdr));
1950
1951 return vprintk_emit(0, level, hdrlen ? hdr : NULL, hdrlen, fmt, args);
1952}
1953EXPORT_SYMBOL(dev_vprintk_emit);
1954
1955int dev_printk_emit(int level, const struct device *dev, const char *fmt, ...)
1956{
1957 va_list args;
1958 int r;
1959
1960 va_start(args, fmt);
1961
1962 r = dev_vprintk_emit(level, dev, fmt, args);
1963
1964 va_end(args);
1965
1966 return r;
1967}
1968EXPORT_SYMBOL(dev_printk_emit);
1969
Joe Perches798efc62012-09-12 20:11:29 -07001970static int __dev_printk(const char *level, const struct device *dev,
1971 struct va_format *vaf)
1972{
Joe Perches798efc62012-09-12 20:11:29 -07001973 if (!dev)
1974 return printk("%s(NULL device *): %pV", level, vaf);
1975
Joe Perches666f3552012-09-12 20:14:11 -07001976 return dev_printk_emit(level[1] - '0', dev,
1977 "%s %s: %pV",
1978 dev_driver_string(dev), dev_name(dev), vaf);
Joe Perches798efc62012-09-12 20:11:29 -07001979}
Joe Perches99bcf212010-06-27 01:02:34 +00001980
1981int dev_printk(const char *level, const struct device *dev,
1982 const char *fmt, ...)
1983{
1984 struct va_format vaf;
1985 va_list args;
1986 int r;
1987
1988 va_start(args, fmt);
1989
1990 vaf.fmt = fmt;
1991 vaf.va = &args;
1992
1993 r = __dev_printk(level, dev, &vaf);
Joe Perches798efc62012-09-12 20:11:29 -07001994
Joe Perches99bcf212010-06-27 01:02:34 +00001995 va_end(args);
1996
1997 return r;
1998}
1999EXPORT_SYMBOL(dev_printk);
2000
2001#define define_dev_printk_level(func, kern_level) \
2002int func(const struct device *dev, const char *fmt, ...) \
2003{ \
2004 struct va_format vaf; \
2005 va_list args; \
2006 int r; \
2007 \
2008 va_start(args, fmt); \
2009 \
2010 vaf.fmt = fmt; \
2011 vaf.va = &args; \
2012 \
2013 r = __dev_printk(kern_level, dev, &vaf); \
Joe Perches798efc62012-09-12 20:11:29 -07002014 \
Joe Perches99bcf212010-06-27 01:02:34 +00002015 va_end(args); \
2016 \
2017 return r; \
2018} \
2019EXPORT_SYMBOL(func);
2020
2021define_dev_printk_level(dev_emerg, KERN_EMERG);
2022define_dev_printk_level(dev_alert, KERN_ALERT);
2023define_dev_printk_level(dev_crit, KERN_CRIT);
2024define_dev_printk_level(dev_err, KERN_ERR);
2025define_dev_printk_level(dev_warn, KERN_WARNING);
2026define_dev_printk_level(dev_notice, KERN_NOTICE);
2027define_dev_printk_level(_dev_info, KERN_INFO);
2028
2029#endif