blob: c7b0925f627a367968f4edbe291ea01a0059cedf [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>
Greg Kroah-Hartman63967682013-08-27 10:24:15 -070029#include <linux/sysfs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
31#include "base.h"
32#include "power/power.h"
33
Andi Kleene52eec12010-09-08 16:54:17 +020034#ifdef CONFIG_SYSFS_DEPRECATED
35#ifdef CONFIG_SYSFS_DEPRECATED_V2
36long sysfs_deprecated = 1;
37#else
38long sysfs_deprecated = 0;
39#endif
Hanjun Guo3454bf92013-08-17 20:42:24 +080040static int __init sysfs_deprecated_setup(char *arg)
Andi Kleene52eec12010-09-08 16:54:17 +020041{
Jingoo Han34da5e62013-07-26 13:10:22 +090042 return kstrtol(arg, 10, &sysfs_deprecated);
Andi Kleene52eec12010-09-08 16:54:17 +020043}
44early_param("sysfs.deprecated", sysfs_deprecated_setup);
45#endif
46
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -080047int (*platform_notify)(struct device *dev) = NULL;
48int (*platform_notify_remove)(struct device *dev) = NULL;
Dan Williamse105b8b2008-04-21 10:51:07 -070049static struct kobject *dev_kobj;
50struct kobject *sysfs_dev_char_kobj;
51struct kobject *sysfs_dev_block_kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Greg Kroah-Hartman4e886c22008-01-27 12:12:43 -080053#ifdef CONFIG_BLOCK
54static inline int device_is_not_partition(struct device *dev)
55{
56 return !(dev->type == &part_type);
57}
58#else
59static inline int device_is_not_partition(struct device *dev)
60{
61 return 1;
62}
63#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Alan Stern3e956372006-06-16 17:10:48 -040065/**
66 * dev_driver_string - Return a device's driver name, if at all possible
67 * @dev: struct device to get the name of
68 *
69 * Will return the device's driver's name if it is bound to a device. If
yan9169c012012-04-20 21:08:45 +080070 * the device is not bound to a driver, it will return the name of the bus
Alan Stern3e956372006-06-16 17:10:48 -040071 * it is attached to. If it is not attached to a bus either, an empty
72 * string will be returned.
73 */
Jean Delvarebf9ca692008-07-30 12:29:21 -070074const char *dev_driver_string(const struct device *dev)
Alan Stern3e956372006-06-16 17:10:48 -040075{
Alan Stern35899722009-12-04 11:06:57 -050076 struct device_driver *drv;
77
78 /* dev->driver can change to NULL underneath us because of unbinding,
79 * so be careful about accessing it. dev->bus and dev->class should
80 * never change once they are set, so they don't need special care.
81 */
82 drv = ACCESS_ONCE(dev->driver);
83 return drv ? drv->name :
Jean Delvarea456b702007-03-09 16:33:10 +010084 (dev->bus ? dev->bus->name :
85 (dev->class ? dev->class->name : ""));
Alan Stern3e956372006-06-16 17:10:48 -040086}
Matthew Wilcox310a9222006-09-23 23:35:04 -060087EXPORT_SYMBOL(dev_driver_string);
Alan Stern3e956372006-06-16 17:10:48 -040088
Linus Torvalds1da177e2005-04-16 15:20:36 -070089#define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr)
90
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -080091static ssize_t dev_attr_show(struct kobject *kobj, struct attribute *attr,
92 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070093{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -080094 struct device_attribute *dev_attr = to_dev_attr(attr);
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +020095 struct device *dev = kobj_to_dev(kobj);
Dmitry Torokhov4a0c20b2005-04-29 01:23:47 -050096 ssize_t ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
98 if (dev_attr->show)
Yani Ioannou54b6f352005-05-17 06:39:34 -040099 ret = dev_attr->show(dev, dev_attr, buf);
Andrew Morton815d2d52008-03-04 15:09:07 -0800100 if (ret >= (ssize_t)PAGE_SIZE) {
Greg Kroah-Hartman53a9c872013-01-17 13:10:23 -0800101 print_symbol("dev_attr_show: %s returned bad count\n",
102 (unsigned long)dev_attr->show);
Andrew Morton815d2d52008-03-04 15:09:07 -0800103 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 return ret;
105}
106
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800107static ssize_t dev_attr_store(struct kobject *kobj, struct attribute *attr,
108 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800110 struct device_attribute *dev_attr = to_dev_attr(attr);
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200111 struct device *dev = kobj_to_dev(kobj);
Dmitry Torokhov4a0c20b2005-04-29 01:23:47 -0500112 ssize_t ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
114 if (dev_attr->store)
Yani Ioannou54b6f352005-05-17 06:39:34 -0400115 ret = dev_attr->store(dev, dev_attr, buf, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 return ret;
117}
118
Emese Revfy52cf25d2010-01-19 02:58:23 +0100119static const struct sysfs_ops dev_sysfs_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 .show = dev_attr_show,
121 .store = dev_attr_store,
122};
123
Kay Sieversca22e562011-12-14 14:29:38 -0800124#define to_ext_attr(x) container_of(x, struct dev_ext_attribute, attr)
125
126ssize_t device_store_ulong(struct device *dev,
127 struct device_attribute *attr,
128 const char *buf, size_t size)
129{
130 struct dev_ext_attribute *ea = to_ext_attr(attr);
131 char *end;
132 unsigned long new = simple_strtoul(buf, &end, 0);
133 if (end == buf)
134 return -EINVAL;
135 *(unsigned long *)(ea->var) = new;
136 /* Always return full write size even if we didn't consume all */
137 return size;
138}
139EXPORT_SYMBOL_GPL(device_store_ulong);
140
141ssize_t device_show_ulong(struct device *dev,
142 struct device_attribute *attr,
143 char *buf)
144{
145 struct dev_ext_attribute *ea = to_ext_attr(attr);
146 return snprintf(buf, PAGE_SIZE, "%lx\n", *(unsigned long *)(ea->var));
147}
148EXPORT_SYMBOL_GPL(device_show_ulong);
149
150ssize_t device_store_int(struct device *dev,
151 struct device_attribute *attr,
152 const char *buf, size_t size)
153{
154 struct dev_ext_attribute *ea = to_ext_attr(attr);
155 char *end;
156 long new = simple_strtol(buf, &end, 0);
157 if (end == buf || new > INT_MAX || new < INT_MIN)
158 return -EINVAL;
159 *(int *)(ea->var) = new;
160 /* Always return full write size even if we didn't consume all */
161 return size;
162}
163EXPORT_SYMBOL_GPL(device_store_int);
164
165ssize_t device_show_int(struct device *dev,
166 struct device_attribute *attr,
167 char *buf)
168{
169 struct dev_ext_attribute *ea = to_ext_attr(attr);
170
171 return snprintf(buf, PAGE_SIZE, "%d\n", *(int *)(ea->var));
172}
173EXPORT_SYMBOL_GPL(device_show_int);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
Borislav Petkov91872392012-10-09 19:52:05 +0200175ssize_t device_store_bool(struct device *dev, struct device_attribute *attr,
176 const char *buf, size_t size)
177{
178 struct dev_ext_attribute *ea = to_ext_attr(attr);
179
180 if (strtobool(buf, ea->var) < 0)
181 return -EINVAL;
182
183 return size;
184}
185EXPORT_SYMBOL_GPL(device_store_bool);
186
187ssize_t device_show_bool(struct device *dev, struct device_attribute *attr,
188 char *buf)
189{
190 struct dev_ext_attribute *ea = to_ext_attr(attr);
191
192 return snprintf(buf, PAGE_SIZE, "%d\n", *(bool *)(ea->var));
193}
194EXPORT_SYMBOL_GPL(device_show_bool);
195
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196/**
Robert P. J. Dayf8878dc2013-06-01 20:17:34 -0400197 * device_release - free device structure.
198 * @kobj: device's kobject.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 *
Robert P. J. Dayf8878dc2013-06-01 20:17:34 -0400200 * This is called once the reference count for the object
201 * reaches 0. We forward the call to the device's release
202 * method, which should handle actually freeing the structure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800204static void device_release(struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200206 struct device *dev = kobj_to_dev(kobj);
Greg Kroah-Hartmanfb069a52008-12-16 12:23:36 -0800207 struct device_private *p = dev->p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
Ming Leia525a3d2012-07-25 01:42:29 +0800209 /*
210 * Some platform devices are driven without driver attached
211 * and managed resources may have been acquired. Make sure
212 * all resources are released.
213 *
214 * Drivers still can add resources into device after device
215 * is deleted but alive, so release devres here to avoid
216 * possible memory leak.
217 */
218 devres_release_all(dev);
219
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 if (dev->release)
221 dev->release(dev);
Kay Sieversf9f852d2006-10-07 21:54:55 +0200222 else if (dev->type && dev->type->release)
223 dev->type->release(dev);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700224 else if (dev->class && dev->class->dev_release)
225 dev->class->dev_release(dev);
Arjan van de Venf810a5c2008-07-25 19:45:39 -0700226 else
227 WARN(1, KERN_ERR "Device '%s' does not have a release() "
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800228 "function, it is broken and must be fixed.\n",
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100229 dev_name(dev));
Greg Kroah-Hartmanfb069a52008-12-16 12:23:36 -0800230 kfree(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231}
232
Eric W. Biedermanbc451f22010-03-30 11:31:25 -0700233static const void *device_namespace(struct kobject *kobj)
234{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200235 struct device *dev = kobj_to_dev(kobj);
Eric W. Biedermanbc451f22010-03-30 11:31:25 -0700236 const void *ns = NULL;
237
238 if (dev->class && dev->class->ns_type)
239 ns = dev->class->namespace(dev);
240
241 return ns;
242}
243
Greg Kroah-Hartman8f4afc42007-10-11 10:47:49 -0600244static struct kobj_type device_ktype = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 .release = device_release,
246 .sysfs_ops = &dev_sysfs_ops,
Eric W. Biedermanbc451f22010-03-30 11:31:25 -0700247 .namespace = device_namespace,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248};
249
250
Kay Sievers312c0042005-11-16 09:00:00 +0100251static int dev_uevent_filter(struct kset *kset, struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252{
253 struct kobj_type *ktype = get_ktype(kobj);
254
Greg Kroah-Hartman8f4afc42007-10-11 10:47:49 -0600255 if (ktype == &device_ktype) {
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200256 struct device *dev = kobj_to_dev(kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 if (dev->bus)
258 return 1;
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700259 if (dev->class)
260 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 }
262 return 0;
263}
264
Kay Sievers312c0042005-11-16 09:00:00 +0100265static const char *dev_uevent_name(struct kset *kset, struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200267 struct device *dev = kobj_to_dev(kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700269 if (dev->bus)
270 return dev->bus->name;
271 if (dev->class)
272 return dev->class->name;
273 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274}
275
Kay Sievers7eff2e72007-08-14 15:15:12 +0200276static int dev_uevent(struct kset *kset, struct kobject *kobj,
277 struct kobj_uevent_env *env)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200279 struct device *dev = kobj_to_dev(kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 int retval = 0;
281
Kay Sievers6fcf53a2009-04-30 15:23:42 +0200282 /* add device node properties if present */
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700283 if (MAJOR(dev->devt)) {
Kay Sievers6fcf53a2009-04-30 15:23:42 +0200284 const char *tmp;
285 const char *name;
Al Viro2c9ede52011-07-23 20:24:48 -0400286 umode_t mode = 0;
Greg Kroah-Hartman4e4098a2013-04-11 11:43:29 -0700287 kuid_t uid = GLOBAL_ROOT_UID;
288 kgid_t gid = GLOBAL_ROOT_GID;
Kay Sievers6fcf53a2009-04-30 15:23:42 +0200289
Kay Sievers7eff2e72007-08-14 15:15:12 +0200290 add_uevent_var(env, "MAJOR=%u", MAJOR(dev->devt));
291 add_uevent_var(env, "MINOR=%u", MINOR(dev->devt));
Kay Sievers3c2670e2013-04-06 09:56:00 -0700292 name = device_get_devnode(dev, &mode, &uid, &gid, &tmp);
Kay Sievers6fcf53a2009-04-30 15:23:42 +0200293 if (name) {
294 add_uevent_var(env, "DEVNAME=%s", name);
Kay Sieverse454cea2009-09-18 23:01:12 +0200295 if (mode)
296 add_uevent_var(env, "DEVMODE=%#o", mode & 0777);
Greg Kroah-Hartman4e4098a2013-04-11 11:43:29 -0700297 if (!uid_eq(uid, GLOBAL_ROOT_UID))
298 add_uevent_var(env, "DEVUID=%u", from_kuid(&init_user_ns, uid));
299 if (!gid_eq(gid, GLOBAL_ROOT_GID))
300 add_uevent_var(env, "DEVGID=%u", from_kgid(&init_user_ns, gid));
Kay Sievers3c2670e2013-04-06 09:56:00 -0700301 kfree(tmp);
Kay Sievers6fcf53a2009-04-30 15:23:42 +0200302 }
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700303 }
304
Kay Sievers414264f2007-03-12 21:08:57 +0100305 if (dev->type && dev->type->name)
Kay Sievers7eff2e72007-08-14 15:15:12 +0200306 add_uevent_var(env, "DEVTYPE=%s", dev->type->name);
Kay Sievers414264f2007-03-12 21:08:57 +0100307
Kay Sievers239378f2006-10-07 21:54:55 +0200308 if (dev->driver)
Kay Sievers7eff2e72007-08-14 15:15:12 +0200309 add_uevent_var(env, "DRIVER=%s", dev->driver->name);
Kay Sievers239378f2006-10-07 21:54:55 +0200310
Grant Likely07d57a32012-02-01 11:22:22 -0700311 /* Add common DT information about the device */
312 of_device_uevent(dev, env);
313
Kay Sievers7eff2e72007-08-14 15:15:12 +0200314 /* have the bus specific function add its stuff */
Kay Sievers312c0042005-11-16 09:00:00 +0100315 if (dev->bus && dev->bus->uevent) {
Kay Sievers7eff2e72007-08-14 15:15:12 +0200316 retval = dev->bus->uevent(dev, env);
Kay Sieversf9f852d2006-10-07 21:54:55 +0200317 if (retval)
Greg Kroah-Hartman7dc72b22007-11-28 23:49:41 -0800318 pr_debug("device: '%s': %s: bus uevent() returned %d\n",
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100319 dev_name(dev), __func__, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 }
321
Kay Sievers7eff2e72007-08-14 15:15:12 +0200322 /* have the class specific function add its stuff */
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700323 if (dev->class && dev->class->dev_uevent) {
Kay Sievers7eff2e72007-08-14 15:15:12 +0200324 retval = dev->class->dev_uevent(dev, env);
Kay Sieversf9f852d2006-10-07 21:54:55 +0200325 if (retval)
Greg Kroah-Hartman7dc72b22007-11-28 23:49:41 -0800326 pr_debug("device: '%s': %s: class uevent() "
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100327 "returned %d\n", dev_name(dev),
Harvey Harrison2b3a3022008-03-04 16:41:05 -0800328 __func__, retval);
Kay Sieversf9f852d2006-10-07 21:54:55 +0200329 }
330
Stefan Weileef35c22010-08-06 21:11:15 +0200331 /* have the device type specific function add its stuff */
Kay Sieversf9f852d2006-10-07 21:54:55 +0200332 if (dev->type && dev->type->uevent) {
Kay Sievers7eff2e72007-08-14 15:15:12 +0200333 retval = dev->type->uevent(dev, env);
Kay Sieversf9f852d2006-10-07 21:54:55 +0200334 if (retval)
Greg Kroah-Hartman7dc72b22007-11-28 23:49:41 -0800335 pr_debug("device: '%s': %s: dev_type uevent() "
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100336 "returned %d\n", dev_name(dev),
Harvey Harrison2b3a3022008-03-04 16:41:05 -0800337 __func__, retval);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700338 }
339
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 return retval;
341}
342
Emese Revfy9cd43612009-12-31 14:52:51 +0100343static const struct kset_uevent_ops device_uevent_ops = {
Kay Sievers312c0042005-11-16 09:00:00 +0100344 .filter = dev_uevent_filter,
345 .name = dev_uevent_name,
346 .uevent = dev_uevent,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347};
348
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -0700349static ssize_t uevent_show(struct device *dev, struct device_attribute *attr,
Kay Sievers16574dc2007-04-06 01:40:38 +0200350 char *buf)
351{
352 struct kobject *top_kobj;
353 struct kset *kset;
Kay Sievers7eff2e72007-08-14 15:15:12 +0200354 struct kobj_uevent_env *env = NULL;
Kay Sievers16574dc2007-04-06 01:40:38 +0200355 int i;
356 size_t count = 0;
357 int retval;
358
359 /* search the kset, the device belongs to */
360 top_kobj = &dev->kobj;
Kay Sievers5c5daf62007-08-12 20:43:55 +0200361 while (!top_kobj->kset && top_kobj->parent)
362 top_kobj = top_kobj->parent;
Kay Sievers16574dc2007-04-06 01:40:38 +0200363 if (!top_kobj->kset)
364 goto out;
Kay Sievers5c5daf62007-08-12 20:43:55 +0200365
Kay Sievers16574dc2007-04-06 01:40:38 +0200366 kset = top_kobj->kset;
367 if (!kset->uevent_ops || !kset->uevent_ops->uevent)
368 goto out;
369
370 /* respect filter */
371 if (kset->uevent_ops && kset->uevent_ops->filter)
372 if (!kset->uevent_ops->filter(kset, &dev->kobj))
373 goto out;
374
Kay Sievers7eff2e72007-08-14 15:15:12 +0200375 env = kzalloc(sizeof(struct kobj_uevent_env), GFP_KERNEL);
376 if (!env)
Greg Kroah-Hartmanc7308c82007-05-02 14:14:11 +0200377 return -ENOMEM;
378
Kay Sievers16574dc2007-04-06 01:40:38 +0200379 /* let the kset specific function add its keys */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200380 retval = kset->uevent_ops->uevent(kset, &dev->kobj, env);
Kay Sievers16574dc2007-04-06 01:40:38 +0200381 if (retval)
382 goto out;
383
384 /* copy keys to file */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200385 for (i = 0; i < env->envp_idx; i++)
386 count += sprintf(&buf[count], "%s\n", env->envp[i]);
Kay Sievers16574dc2007-04-06 01:40:38 +0200387out:
Kay Sievers7eff2e72007-08-14 15:15:12 +0200388 kfree(env);
Kay Sievers16574dc2007-04-06 01:40:38 +0200389 return count;
390}
391
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -0700392static ssize_t uevent_store(struct device *dev, struct device_attribute *attr,
Kay Sieversa7fd6702005-10-01 14:49:43 +0200393 const char *buf, size_t count)
394{
Kay Sievers60a96a52007-07-08 22:29:26 +0200395 enum kobject_action action;
396
Kay Sievers3f5468c2010-01-14 22:54:37 +0100397 if (kobject_action_type(buf, count, &action) == 0)
Kay Sievers60a96a52007-07-08 22:29:26 +0200398 kobject_uevent(&dev->kobj, action);
Kay Sievers3f5468c2010-01-14 22:54:37 +0100399 else
400 dev_err(dev, "uevent: unknown action-string\n");
Kay Sieversa7fd6702005-10-01 14:49:43 +0200401 return count;
402}
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -0700403static DEVICE_ATTR_RW(uevent);
Kay Sieversa7fd6702005-10-01 14:49:43 +0200404
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -0700405static ssize_t online_show(struct device *dev, struct device_attribute *attr,
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +0200406 char *buf)
407{
408 bool val;
409
410 lock_device_hotplug();
411 val = !dev->offline;
412 unlock_device_hotplug();
413 return sprintf(buf, "%u\n", val);
414}
415
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -0700416static ssize_t online_store(struct device *dev, struct device_attribute *attr,
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +0200417 const char *buf, size_t count)
418{
419 bool val;
420 int ret;
421
422 ret = strtobool(buf, &val);
423 if (ret < 0)
424 return ret;
425
426 lock_device_hotplug();
427 ret = val ? device_online(dev) : device_offline(dev);
428 unlock_device_hotplug();
429 return ret < 0 ? ret : count;
430}
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -0700431static DEVICE_ATTR_RW(online);
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +0200432
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500433static int device_add_attributes(struct device *dev,
434 struct device_attribute *attrs)
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -0700435{
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -0700436 int error = 0;
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500437 int i;
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -0700438
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500439 if (attrs) {
Greg Kroah-Hartman3e1026b2013-08-22 10:25:34 -0700440 for (i = 0; attrs[i].attr.name; i++) {
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500441 error = device_create_file(dev, &attrs[i]);
442 if (error)
443 break;
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -0700444 }
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500445 if (error)
446 while (--i >= 0)
447 device_remove_file(dev, &attrs[i]);
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -0700448 }
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -0700449 return error;
450}
451
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500452static void device_remove_attributes(struct device *dev,
453 struct device_attribute *attrs)
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -0700454{
455 int i;
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500456
457 if (attrs)
Greg Kroah-Hartman3e1026b2013-08-22 10:25:34 -0700458 for (i = 0; attrs[i].attr.name; i++)
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500459 device_remove_file(dev, &attrs[i]);
460}
461
Stefan Achatzc97415a2010-11-26 19:57:29 +0000462static int device_add_bin_attributes(struct device *dev,
463 struct bin_attribute *attrs)
464{
465 int error = 0;
466 int i;
467
468 if (attrs) {
Greg Kroah-Hartman3e1026b2013-08-22 10:25:34 -0700469 for (i = 0; attrs[i].attr.name; i++) {
Stefan Achatzc97415a2010-11-26 19:57:29 +0000470 error = device_create_bin_file(dev, &attrs[i]);
471 if (error)
472 break;
473 }
474 if (error)
475 while (--i >= 0)
476 device_remove_bin_file(dev, &attrs[i]);
477 }
478 return error;
479}
480
481static void device_remove_bin_attributes(struct device *dev,
482 struct bin_attribute *attrs)
483{
484 int i;
485
486 if (attrs)
Greg Kroah-Hartman3e1026b2013-08-22 10:25:34 -0700487 for (i = 0; attrs[i].attr.name; i++)
Stefan Achatzc97415a2010-11-26 19:57:29 +0000488 device_remove_bin_file(dev, &attrs[i]);
489}
490
Greg Kroah-Hartmanfa6fdb32013-08-08 15:22:55 -0700491int device_add_groups(struct device *dev, const struct attribute_group **groups)
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500492{
Greg Kroah-Hartman3e9b2ba2013-08-21 13:47:50 -0700493 return sysfs_create_groups(&dev->kobj, groups);
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500494}
495
Greg Kroah-Hartmanfa6fdb32013-08-08 15:22:55 -0700496void device_remove_groups(struct device *dev,
497 const struct attribute_group **groups)
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500498{
Greg Kroah-Hartman3e9b2ba2013-08-21 13:47:50 -0700499 sysfs_remove_groups(&dev->kobj, groups);
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -0700500}
501
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700502static int device_add_attrs(struct device *dev)
503{
504 struct class *class = dev->class;
Stephen Hemmingeraed65af2011-03-28 09:12:52 -0700505 const struct device_type *type = dev->type;
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500506 int error;
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700507
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500508 if (class) {
Greg Kroah-Hartmand05a6f92013-07-14 16:05:58 -0700509 error = device_add_groups(dev, class->dev_groups);
Kay Sieversf9f852d2006-10-07 21:54:55 +0200510 if (error)
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500511 return error;
Greg Kroah-Hartmand05a6f92013-07-14 16:05:58 -0700512 error = device_add_attributes(dev, class->dev_attrs);
513 if (error)
514 goto err_remove_class_groups;
Stefan Achatzc97415a2010-11-26 19:57:29 +0000515 error = device_add_bin_attributes(dev, class->dev_bin_attrs);
516 if (error)
517 goto err_remove_class_attrs;
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700518 }
Kay Sieversf9f852d2006-10-07 21:54:55 +0200519
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500520 if (type) {
521 error = device_add_groups(dev, type->groups);
Kay Sieversf9f852d2006-10-07 21:54:55 +0200522 if (error)
Stefan Achatzc97415a2010-11-26 19:57:29 +0000523 goto err_remove_class_bin_attrs;
Kay Sieversf9f852d2006-10-07 21:54:55 +0200524 }
525
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500526 error = device_add_groups(dev, dev->groups);
527 if (error)
528 goto err_remove_type_groups;
529
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +0200530 if (device_supports_offline(dev) && !dev->offline_disabled) {
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -0700531 error = device_create_file(dev, &dev_attr_online);
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +0200532 if (error)
533 goto err_remove_type_groups;
534 }
535
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500536 return 0;
537
538 err_remove_type_groups:
539 if (type)
540 device_remove_groups(dev, type->groups);
Stefan Achatzc97415a2010-11-26 19:57:29 +0000541 err_remove_class_bin_attrs:
542 if (class)
543 device_remove_bin_attributes(dev, class->dev_bin_attrs);
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500544 err_remove_class_attrs:
545 if (class)
546 device_remove_attributes(dev, class->dev_attrs);
Greg Kroah-Hartmand05a6f92013-07-14 16:05:58 -0700547 err_remove_class_groups:
548 if (class)
549 device_remove_groups(dev, class->dev_groups);
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500550
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700551 return error;
552}
553
554static void device_remove_attrs(struct device *dev)
555{
556 struct class *class = dev->class;
Stephen Hemmingeraed65af2011-03-28 09:12:52 -0700557 const struct device_type *type = dev->type;
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700558
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -0700559 device_remove_file(dev, &dev_attr_online);
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500560 device_remove_groups(dev, dev->groups);
Kay Sieversf9f852d2006-10-07 21:54:55 +0200561
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500562 if (type)
563 device_remove_groups(dev, type->groups);
564
Stefan Achatzc97415a2010-11-26 19:57:29 +0000565 if (class) {
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500566 device_remove_attributes(dev, class->dev_attrs);
Stefan Achatzc97415a2010-11-26 19:57:29 +0000567 device_remove_bin_attributes(dev, class->dev_bin_attrs);
Greg Kroah-Hartmand05a6f92013-07-14 16:05:58 -0700568 device_remove_groups(dev, class->dev_groups);
Stefan Achatzc97415a2010-11-26 19:57:29 +0000569 }
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700570}
571
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -0700572static ssize_t dev_show(struct device *dev, struct device_attribute *attr,
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700573 char *buf)
574{
575 return print_dev_t(buf, dev->devt);
576}
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -0700577static DEVICE_ATTR_RO(dev);
Tejun Heoad6a1e12007-06-14 03:45:17 +0900578
Kay Sieversca22e562011-12-14 14:29:38 -0800579/* /sys/devices/ */
Greg Kroah-Hartman881c6cfd2007-11-01 09:29:06 -0600580struct kset *devices_kset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800583 * device_create_file - create sysfs attribute file for device.
584 * @dev: device.
585 * @attr: device attribute descriptor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 */
Phil Carmody26579ab2009-12-18 15:34:19 +0200587int device_create_file(struct device *dev,
588 const struct device_attribute *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589{
590 int error = 0;
Felipe Balbi8f46baa2013-02-20 10:31:42 +0200591
592 if (dev) {
593 WARN(((attr->attr.mode & S_IWUGO) && !attr->store),
dyoung@redhat.com97521972013-05-16 14:31:30 +0800594 "Attribute %s: write permission without 'store'\n",
595 attr->attr.name);
Felipe Balbi8f46baa2013-02-20 10:31:42 +0200596 WARN(((attr->attr.mode & S_IRUGO) && !attr->show),
dyoung@redhat.com97521972013-05-16 14:31:30 +0800597 "Attribute %s: read permission without 'show'\n",
598 attr->attr.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 error = sysfs_create_file(&dev->kobj, &attr->attr);
Felipe Balbi8f46baa2013-02-20 10:31:42 +0200600 }
601
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 return error;
603}
David Graham White86df2682013-07-21 20:41:14 -0400604EXPORT_SYMBOL_GPL(device_create_file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
606/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800607 * device_remove_file - remove sysfs attribute file.
608 * @dev: device.
609 * @attr: device attribute descriptor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 */
Phil Carmody26579ab2009-12-18 15:34:19 +0200611void device_remove_file(struct device *dev,
612 const struct device_attribute *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613{
Cornelia Huck0c98b192008-01-31 10:39:38 +0100614 if (dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 sysfs_remove_file(&dev->kobj, &attr->attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616}
David Graham White86df2682013-07-21 20:41:14 -0400617EXPORT_SYMBOL_GPL(device_remove_file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
Greg Kroah-Hartman2589f1882006-09-19 09:39:19 -0700619/**
620 * device_create_bin_file - create sysfs binary attribute file for device.
621 * @dev: device.
622 * @attr: device binary attribute descriptor.
623 */
Phil Carmody66ecb922009-12-18 15:34:20 +0200624int device_create_bin_file(struct device *dev,
625 const struct bin_attribute *attr)
Greg Kroah-Hartman2589f1882006-09-19 09:39:19 -0700626{
627 int error = -EINVAL;
628 if (dev)
629 error = sysfs_create_bin_file(&dev->kobj, attr);
630 return error;
631}
632EXPORT_SYMBOL_GPL(device_create_bin_file);
633
634/**
635 * device_remove_bin_file - remove sysfs binary attribute file
636 * @dev: device.
637 * @attr: device binary attribute descriptor.
638 */
Phil Carmody66ecb922009-12-18 15:34:20 +0200639void device_remove_bin_file(struct device *dev,
640 const struct bin_attribute *attr)
Greg Kroah-Hartman2589f1882006-09-19 09:39:19 -0700641{
642 if (dev)
643 sysfs_remove_bin_file(&dev->kobj, attr);
644}
645EXPORT_SYMBOL_GPL(device_remove_bin_file);
646
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400647/**
Alan Stern523ded72007-04-26 00:12:04 -0700648 * device_schedule_callback_owner - helper to schedule a callback for a device
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400649 * @dev: device.
650 * @func: callback function to invoke later.
Alan Stern523ded72007-04-26 00:12:04 -0700651 * @owner: module owning the callback routine
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400652 *
653 * Attribute methods must not unregister themselves or their parent device
654 * (which would amount to the same thing). Attempts to do so will deadlock,
655 * since unregistration is mutually exclusive with driver callbacks.
656 *
657 * Instead methods can call this routine, which will attempt to allocate
658 * and schedule a workqueue request to call back @func with @dev as its
659 * argument in the workqueue's process context. @dev will be pinned until
660 * @func returns.
661 *
Alan Stern523ded72007-04-26 00:12:04 -0700662 * This routine is usually called via the inline device_schedule_callback(),
663 * which automatically sets @owner to THIS_MODULE.
664 *
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400665 * Returns 0 if the request was submitted, -ENOMEM if storage could not
Alan Stern523ded72007-04-26 00:12:04 -0700666 * be allocated, -ENODEV if a reference to @owner isn't available.
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400667 *
668 * NOTE: This routine won't work if CONFIG_SYSFS isn't set! It uses an
669 * underlying sysfs routine (since it is intended for use by attribute
670 * methods), and if sysfs isn't available you'll get nothing but -ENOSYS.
671 */
Alan Stern523ded72007-04-26 00:12:04 -0700672int device_schedule_callback_owner(struct device *dev,
673 void (*func)(struct device *), struct module *owner)
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400674{
675 return sysfs_schedule_callback(&dev->kobj,
Alan Stern523ded72007-04-26 00:12:04 -0700676 (void (*)(void *)) func, dev, owner);
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400677}
Alan Stern523ded72007-04-26 00:12:04 -0700678EXPORT_SYMBOL_GPL(device_schedule_callback_owner);
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400679
James Bottomley34bb61f2005-09-06 16:56:51 -0700680static void klist_children_get(struct klist_node *n)
681{
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -0800682 struct device_private *p = to_device_private_parent(n);
683 struct device *dev = p->device;
James Bottomley34bb61f2005-09-06 16:56:51 -0700684
685 get_device(dev);
686}
687
688static void klist_children_put(struct klist_node *n)
689{
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -0800690 struct device_private *p = to_device_private_parent(n);
691 struct device *dev = p->device;
James Bottomley34bb61f2005-09-06 16:56:51 -0700692
693 put_device(dev);
694}
695
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800697 * device_initialize - init device structure.
698 * @dev: device.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 *
Cornelia Huck57394112008-09-03 18:26:40 +0200700 * This prepares the device for use by other layers by initializing
701 * its fields.
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800702 * It is the first half of device_register(), if called by
Cornelia Huck57394112008-09-03 18:26:40 +0200703 * that function, though it can also be called separately, so one
704 * may use @dev's fields. In particular, get_device()/put_device()
705 * may be used for reference counting of @dev after calling this
706 * function.
707 *
Alan Sternb10d5ef2012-01-17 11:39:00 -0500708 * All fields in @dev must be initialized by the caller to 0, except
709 * for those explicitly set to some other value. The simplest
710 * approach is to use kzalloc() to allocate the structure containing
711 * @dev.
712 *
Cornelia Huck57394112008-09-03 18:26:40 +0200713 * NOTE: Use put_device() to give up your reference instead of freeing
714 * @dev directly once you have called this function.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716void device_initialize(struct device *dev)
717{
Greg Kroah-Hartman881c6cfd2007-11-01 09:29:06 -0600718 dev->kobj.kset = devices_kset;
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -0700719 kobject_init(&dev->kobj, &device_ktype);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 INIT_LIST_HEAD(&dev->dma_pools);
Thomas Gleixner31427882010-01-29 20:39:02 +0000721 mutex_init(&dev->mutex);
Peter Zijlstra1704f472010-03-19 01:37:42 +0100722 lockdep_set_novalidate_class(&dev->mutex);
Tejun Heo9ac78492007-01-20 16:00:26 +0900723 spin_lock_init(&dev->devres_lock);
724 INIT_LIST_HEAD(&dev->devres_head);
Alan Stern3b98aea2008-08-07 13:06:12 -0400725 device_pm_init(dev);
Christoph Hellwig87348132006-12-06 20:32:33 -0800726 set_dev_node(dev, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727}
David Graham White86df2682013-07-21 20:41:14 -0400728EXPORT_SYMBOL_GPL(device_initialize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729
Tejun Heod73ce002013-03-12 11:30:05 -0700730struct kobject *virtual_device_parent(struct device *dev)
Greg Kroah-Hartmanf0ee61a2006-10-23 10:40:54 -0700731{
Kay Sievers86406242007-03-14 03:25:56 +0100732 static struct kobject *virtual_dir = NULL;
Greg Kroah-Hartmanf0ee61a2006-10-23 10:40:54 -0700733
Kay Sievers86406242007-03-14 03:25:56 +0100734 if (!virtual_dir)
Greg Kroah-Hartman4ff6abf2007-11-05 22:24:43 -0800735 virtual_dir = kobject_create_and_add("virtual",
Greg Kroah-Hartman881c6cfd2007-11-01 09:29:06 -0600736 &devices_kset->kobj);
Greg Kroah-Hartmanf0ee61a2006-10-23 10:40:54 -0700737
Kay Sievers86406242007-03-14 03:25:56 +0100738 return virtual_dir;
Greg Kroah-Hartmanf0ee61a2006-10-23 10:40:54 -0700739}
740
Eric W. Biedermanbc451f22010-03-30 11:31:25 -0700741struct class_dir {
742 struct kobject kobj;
743 struct class *class;
744};
745
746#define to_class_dir(obj) container_of(obj, struct class_dir, kobj)
747
748static void class_dir_release(struct kobject *kobj)
749{
750 struct class_dir *dir = to_class_dir(kobj);
751 kfree(dir);
752}
753
754static const
755struct kobj_ns_type_operations *class_dir_child_ns_type(struct kobject *kobj)
756{
757 struct class_dir *dir = to_class_dir(kobj);
758 return dir->class->ns_type;
759}
760
761static struct kobj_type class_dir_ktype = {
762 .release = class_dir_release,
763 .sysfs_ops = &kobj_sysfs_ops,
764 .child_ns_type = class_dir_child_ns_type
765};
766
767static struct kobject *
768class_dir_create_and_add(struct class *class, struct kobject *parent_kobj)
769{
770 struct class_dir *dir;
771 int retval;
772
773 dir = kzalloc(sizeof(*dir), GFP_KERNEL);
774 if (!dir)
775 return NULL;
776
777 dir->class = class;
778 kobject_init(&dir->kobj, &class_dir_ktype);
779
Kay Sievers6b6e39a2010-11-15 23:13:18 +0100780 dir->kobj.kset = &class->p->glue_dirs;
Eric W. Biedermanbc451f22010-03-30 11:31:25 -0700781
782 retval = kobject_add(&dir->kobj, parent_kobj, "%s", class->name);
783 if (retval < 0) {
784 kobject_put(&dir->kobj);
785 return NULL;
786 }
787 return &dir->kobj;
788}
789
790
Kay Sieversda231fd2007-11-21 17:29:15 +0100791static struct kobject *get_device_parent(struct device *dev,
792 struct device *parent)
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +0100793{
Kay Sievers86406242007-03-14 03:25:56 +0100794 if (dev->class) {
Tejun Heo77d3d7c2010-02-05 17:57:02 +0900795 static DEFINE_MUTEX(gdp_mutex);
Kay Sievers86406242007-03-14 03:25:56 +0100796 struct kobject *kobj = NULL;
797 struct kobject *parent_kobj;
798 struct kobject *k;
799
Randy Dunlapead454f2010-09-24 14:36:49 -0700800#ifdef CONFIG_BLOCK
Kay Sievers39aba962010-09-04 22:33:14 -0700801 /* block disks show up in /sys/block */
Andi Kleene52eec12010-09-08 16:54:17 +0200802 if (sysfs_deprecated && dev->class == &block_class) {
Kay Sievers39aba962010-09-04 22:33:14 -0700803 if (parent && parent->class == &block_class)
804 return &parent->kobj;
Kay Sievers6b6e39a2010-11-15 23:13:18 +0100805 return &block_class.p->subsys.kobj;
Kay Sievers39aba962010-09-04 22:33:14 -0700806 }
Randy Dunlapead454f2010-09-24 14:36:49 -0700807#endif
Andi Kleene52eec12010-09-08 16:54:17 +0200808
Kay Sievers86406242007-03-14 03:25:56 +0100809 /*
810 * If we have no parent, we live in "virtual".
Kay Sievers0f4dafc2007-12-19 01:40:42 +0100811 * Class-devices with a non class-device as parent, live
812 * in a "glue" directory to prevent namespace collisions.
Kay Sievers86406242007-03-14 03:25:56 +0100813 */
814 if (parent == NULL)
815 parent_kobj = virtual_device_parent(dev);
Eric W. Biederman24b14422010-07-24 22:43:35 -0700816 else if (parent->class && !dev->class->ns_type)
Kay Sievers86406242007-03-14 03:25:56 +0100817 return &parent->kobj;
818 else
819 parent_kobj = &parent->kobj;
820
Tejun Heo77d3d7c2010-02-05 17:57:02 +0900821 mutex_lock(&gdp_mutex);
822
Kay Sievers86406242007-03-14 03:25:56 +0100823 /* find our class-directory at the parent and reference it */
Kay Sievers6b6e39a2010-11-15 23:13:18 +0100824 spin_lock(&dev->class->p->glue_dirs.list_lock);
825 list_for_each_entry(k, &dev->class->p->glue_dirs.list, entry)
Kay Sievers86406242007-03-14 03:25:56 +0100826 if (k->parent == parent_kobj) {
827 kobj = kobject_get(k);
828 break;
829 }
Kay Sievers6b6e39a2010-11-15 23:13:18 +0100830 spin_unlock(&dev->class->p->glue_dirs.list_lock);
Tejun Heo77d3d7c2010-02-05 17:57:02 +0900831 if (kobj) {
832 mutex_unlock(&gdp_mutex);
Kay Sievers86406242007-03-14 03:25:56 +0100833 return kobj;
Tejun Heo77d3d7c2010-02-05 17:57:02 +0900834 }
Kay Sievers86406242007-03-14 03:25:56 +0100835
836 /* or create a new class-directory at the parent device */
Eric W. Biedermanbc451f22010-03-30 11:31:25 -0700837 k = class_dir_create_and_add(dev->class, parent_kobj);
Kay Sievers0f4dafc2007-12-19 01:40:42 +0100838 /* do not emit an uevent for this simple "glue" directory */
Tejun Heo77d3d7c2010-02-05 17:57:02 +0900839 mutex_unlock(&gdp_mutex);
Greg Kroah-Hartman43968d22007-11-05 22:24:43 -0800840 return k;
Kay Sievers86406242007-03-14 03:25:56 +0100841 }
842
Kay Sieversca22e562011-12-14 14:29:38 -0800843 /* subsystems can specify a default root directory for their devices */
844 if (!parent && dev->bus && dev->bus->dev_root)
845 return &dev->bus->dev_root->kobj;
846
Kay Sievers86406242007-03-14 03:25:56 +0100847 if (parent)
Cornelia Huckc744aeae2007-01-08 20:16:44 +0100848 return &parent->kobj;
849 return NULL;
850}
Kay Sieversda231fd2007-11-21 17:29:15 +0100851
Cornelia Huck63b69712008-01-21 16:09:44 +0100852static void cleanup_glue_dir(struct device *dev, struct kobject *glue_dir)
Kay Sieversda231fd2007-11-21 17:29:15 +0100853{
Kay Sievers0f4dafc2007-12-19 01:40:42 +0100854 /* see if we live in a "glue" directory */
Cornelia Huckc1fe5392008-02-27 15:38:23 +0100855 if (!glue_dir || !dev->class ||
Kay Sievers6b6e39a2010-11-15 23:13:18 +0100856 glue_dir->kset != &dev->class->p->glue_dirs)
Kay Sieversda231fd2007-11-21 17:29:15 +0100857 return;
858
Kay Sievers0f4dafc2007-12-19 01:40:42 +0100859 kobject_put(glue_dir);
Kay Sieversda231fd2007-11-21 17:29:15 +0100860}
Cornelia Huck63b69712008-01-21 16:09:44 +0100861
862static void cleanup_device_parent(struct device *dev)
863{
864 cleanup_glue_dir(dev, dev->kobj.parent);
865}
Kay Sievers86406242007-03-14 03:25:56 +0100866
Cornelia Huck2ee97ca2007-07-18 01:43:47 -0700867static int device_add_class_symlinks(struct device *dev)
868{
869 int error;
870
871 if (!dev->class)
872 return 0;
Kay Sieversda231fd2007-11-21 17:29:15 +0100873
Greg Kroah-Hartman1fbfee62008-05-28 09:28:39 -0700874 error = sysfs_create_link(&dev->kobj,
Kay Sievers6b6e39a2010-11-15 23:13:18 +0100875 &dev->class->p->subsys.kobj,
Cornelia Huck2ee97ca2007-07-18 01:43:47 -0700876 "subsystem");
877 if (error)
878 goto out;
Kay Sieversda231fd2007-11-21 17:29:15 +0100879
Greg Kroah-Hartman4e886c22008-01-27 12:12:43 -0800880 if (dev->parent && device_is_not_partition(dev)) {
Dmitry Torokhov4f01a752007-09-18 22:46:50 -0700881 error = sysfs_create_link(&dev->kobj, &dev->parent->kobj,
882 "device");
883 if (error)
Kay Sievers39aba962010-09-04 22:33:14 -0700884 goto out_subsys;
Cornelia Huck2ee97ca2007-07-18 01:43:47 -0700885 }
Kay Sievers39aba962010-09-04 22:33:14 -0700886
Randy Dunlapead454f2010-09-24 14:36:49 -0700887#ifdef CONFIG_BLOCK
Kay Sievers39aba962010-09-04 22:33:14 -0700888 /* /sys/block has directories and does not need symlinks */
Andi Kleene52eec12010-09-08 16:54:17 +0200889 if (sysfs_deprecated && dev->class == &block_class)
Kay Sievers39aba962010-09-04 22:33:14 -0700890 return 0;
Randy Dunlapead454f2010-09-24 14:36:49 -0700891#endif
Kay Sievers39aba962010-09-04 22:33:14 -0700892
893 /* link in the class directory pointing to the device */
Kay Sievers6b6e39a2010-11-15 23:13:18 +0100894 error = sysfs_create_link(&dev->class->p->subsys.kobj,
Kay Sievers39aba962010-09-04 22:33:14 -0700895 &dev->kobj, dev_name(dev));
896 if (error)
897 goto out_device;
898
Cornelia Huck2ee97ca2007-07-18 01:43:47 -0700899 return 0;
900
Kay Sievers39aba962010-09-04 22:33:14 -0700901out_device:
902 sysfs_remove_link(&dev->kobj, "device");
Kay Sieversda231fd2007-11-21 17:29:15 +0100903
Cornelia Huck2ee97ca2007-07-18 01:43:47 -0700904out_subsys:
905 sysfs_remove_link(&dev->kobj, "subsystem");
906out:
907 return error;
908}
909
910static void device_remove_class_symlinks(struct device *dev)
911{
912 if (!dev->class)
913 return;
Kay Sieversda231fd2007-11-21 17:29:15 +0100914
Greg Kroah-Hartman4e886c22008-01-27 12:12:43 -0800915 if (dev->parent && device_is_not_partition(dev))
Kay Sieversda231fd2007-11-21 17:29:15 +0100916 sysfs_remove_link(&dev->kobj, "device");
Cornelia Huck2ee97ca2007-07-18 01:43:47 -0700917 sysfs_remove_link(&dev->kobj, "subsystem");
Randy Dunlapead454f2010-09-24 14:36:49 -0700918#ifdef CONFIG_BLOCK
Andi Kleene52eec12010-09-08 16:54:17 +0200919 if (sysfs_deprecated && dev->class == &block_class)
Kay Sievers39aba962010-09-04 22:33:14 -0700920 return;
Randy Dunlapead454f2010-09-24 14:36:49 -0700921#endif
Kay Sievers6b6e39a2010-11-15 23:13:18 +0100922 sysfs_delete_link(&dev->class->p->subsys.kobj, &dev->kobj, dev_name(dev));
Cornelia Huck2ee97ca2007-07-18 01:43:47 -0700923}
924
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925/**
Stephen Rothwell413c2392008-05-30 10:16:40 +1000926 * dev_set_name - set a device name
927 * @dev: device
Randy Dunlap46232362008-06-04 21:40:43 -0700928 * @fmt: format string for the device's name
Stephen Rothwell413c2392008-05-30 10:16:40 +1000929 */
930int dev_set_name(struct device *dev, const char *fmt, ...)
931{
932 va_list vargs;
Kay Sievers1fa5ae82009-01-25 15:17:37 +0100933 int err;
Stephen Rothwell413c2392008-05-30 10:16:40 +1000934
935 va_start(vargs, fmt);
Kay Sievers1fa5ae82009-01-25 15:17:37 +0100936 err = kobject_set_name_vargs(&dev->kobj, fmt, vargs);
Stephen Rothwell413c2392008-05-30 10:16:40 +1000937 va_end(vargs);
Kay Sievers1fa5ae82009-01-25 15:17:37 +0100938 return err;
Stephen Rothwell413c2392008-05-30 10:16:40 +1000939}
940EXPORT_SYMBOL_GPL(dev_set_name);
941
942/**
Dan Williamse105b8b2008-04-21 10:51:07 -0700943 * device_to_dev_kobj - select a /sys/dev/ directory for the device
944 * @dev: device
945 *
946 * By default we select char/ for new entries. Setting class->dev_obj
947 * to NULL prevents an entry from being created. class->dev_kobj must
948 * be set (or cleared) before any devices are registered to the class
949 * otherwise device_create_sys_dev_entry() and
Peter Korsgaard0d4e293c2012-04-17 12:12:57 +0200950 * device_remove_sys_dev_entry() will disagree about the presence of
951 * the link.
Dan Williamse105b8b2008-04-21 10:51:07 -0700952 */
953static struct kobject *device_to_dev_kobj(struct device *dev)
954{
955 struct kobject *kobj;
956
957 if (dev->class)
958 kobj = dev->class->dev_kobj;
959 else
960 kobj = sysfs_dev_char_kobj;
961
962 return kobj;
963}
964
965static int device_create_sys_dev_entry(struct device *dev)
966{
967 struct kobject *kobj = device_to_dev_kobj(dev);
968 int error = 0;
969 char devt_str[15];
970
971 if (kobj) {
972 format_dev_t(devt_str, dev->devt);
973 error = sysfs_create_link(kobj, &dev->kobj, devt_str);
974 }
975
976 return error;
977}
978
979static void device_remove_sys_dev_entry(struct device *dev)
980{
981 struct kobject *kobj = device_to_dev_kobj(dev);
982 char devt_str[15];
983
984 if (kobj) {
985 format_dev_t(devt_str, dev->devt);
986 sysfs_remove_link(kobj, devt_str);
987 }
988}
989
Greg Kroah-Hartmanb4028432009-05-11 14:16:57 -0700990int device_private_init(struct device *dev)
991{
992 dev->p = kzalloc(sizeof(*dev->p), GFP_KERNEL);
993 if (!dev->p)
994 return -ENOMEM;
995 dev->p->device = dev;
996 klist_init(&dev->p->klist_children, klist_children_get,
997 klist_children_put);
Greg Kroah-Hartmanef8a3fd2012-03-08 12:17:22 -0800998 INIT_LIST_HEAD(&dev->p->deferred_probe);
Greg Kroah-Hartmanb4028432009-05-11 14:16:57 -0700999 return 0;
1000}
1001
Dan Williamse105b8b2008-04-21 10:51:07 -07001002/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001003 * device_add - add device to device hierarchy.
1004 * @dev: device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001006 * This is part 2 of device_register(), though may be called
1007 * separately _iff_ device_initialize() has been called separately.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 *
Cornelia Huck57394112008-09-03 18:26:40 +02001009 * This adds @dev to the kobject hierarchy via kobject_add(), adds it
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001010 * to the global and sibling lists for the device, then
1011 * adds it to the other relevant subsystems of the driver model.
Cornelia Huck57394112008-09-03 18:26:40 +02001012 *
Alan Sternb10d5ef2012-01-17 11:39:00 -05001013 * Do not call this routine or device_register() more than once for
1014 * any device structure. The driver model core is not designed to work
1015 * with devices that get unregistered and then spring back to life.
1016 * (Among other things, it's very hard to guarantee that all references
1017 * to the previous incarnation of @dev have been dropped.) Allocate
1018 * and register a fresh new struct device instead.
1019 *
Cornelia Huck57394112008-09-03 18:26:40 +02001020 * NOTE: _Never_ directly free @dev after calling this function, even
1021 * if it returned an error! Always use put_device() to give up your
1022 * reference instead.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 */
1024int device_add(struct device *dev)
1025{
1026 struct device *parent = NULL;
Kay Sieversca22e562011-12-14 14:29:38 -08001027 struct kobject *kobj;
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02001028 struct class_interface *class_intf;
Greg Kroah-Hartmanc906a482008-05-30 10:45:12 -07001029 int error = -EINVAL;
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001030
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 dev = get_device(dev);
Greg Kroah-Hartmanc906a482008-05-30 10:45:12 -07001032 if (!dev)
1033 goto done;
1034
Greg Kroah-Hartmanfb069a52008-12-16 12:23:36 -08001035 if (!dev->p) {
Greg Kroah-Hartmanb4028432009-05-11 14:16:57 -07001036 error = device_private_init(dev);
1037 if (error)
1038 goto done;
Greg Kroah-Hartmanfb069a52008-12-16 12:23:36 -08001039 }
Greg Kroah-Hartmanfb069a52008-12-16 12:23:36 -08001040
Kay Sievers1fa5ae82009-01-25 15:17:37 +01001041 /*
1042 * for statically allocated devices, which should all be converted
1043 * some day, we need to initialize the name. We prevent reading back
1044 * the name, and force the use of dev_name()
1045 */
1046 if (dev->init_name) {
Greg Kroah-Hartmanacc0e902009-06-02 15:39:55 -07001047 dev_set_name(dev, "%s", dev->init_name);
Kay Sievers1fa5ae82009-01-25 15:17:37 +01001048 dev->init_name = NULL;
1049 }
Greg Kroah-Hartmanc906a482008-05-30 10:45:12 -07001050
Kay Sieversca22e562011-12-14 14:29:38 -08001051 /* subsystems can specify simple device enumeration */
1052 if (!dev_name(dev) && dev->bus && dev->bus->dev_name)
1053 dev_set_name(dev, "%s%u", dev->bus->dev_name, dev->id);
1054
Thomas Gleixnere6309e72009-12-10 19:32:49 +00001055 if (!dev_name(dev)) {
1056 error = -EINVAL;
Kay Sievers5c8563d2009-05-28 14:24:07 -07001057 goto name_error;
Thomas Gleixnere6309e72009-12-10 19:32:49 +00001058 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01001060 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
Greg Kroah-Hartmanc205ef42006-08-07 22:19:37 -07001061
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 parent = get_device(dev->parent);
Kay Sieversca22e562011-12-14 14:29:38 -08001063 kobj = get_device_parent(dev, parent);
1064 if (kobj)
1065 dev->kobj.parent = kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066
Yinghai Lu0d358f22008-02-19 03:20:41 -08001067 /* use parent numa_node */
1068 if (parent)
1069 set_dev_node(dev, dev_to_node(parent));
1070
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 /* first, register with generic layer. */
Kay Sievers8a577ff2009-04-18 15:05:45 -07001072 /* we require the name to be set before, and pass NULL */
1073 error = kobject_add(&dev->kobj, dev->kobj.parent, NULL);
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +01001074 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 goto Error;
Kay Sieversa7fd6702005-10-01 14:49:43 +02001076
Brian Walsh37022642006-08-14 22:43:19 -07001077 /* notify platform of device entry */
1078 if (platform_notify)
1079 platform_notify(dev);
1080
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07001081 error = device_create_file(dev, &dev_attr_uevent);
Cornelia Hucka306eea2006-09-22 11:37:13 +02001082 if (error)
1083 goto attrError;
Kay Sieversa7fd6702005-10-01 14:49:43 +02001084
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001085 if (MAJOR(dev->devt)) {
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07001086 error = device_create_file(dev, &dev_attr_dev);
Tejun Heoad6a1e12007-06-14 03:45:17 +09001087 if (error)
Cornelia Hucka306eea2006-09-22 11:37:13 +02001088 goto ueventattrError;
Dan Williamse105b8b2008-04-21 10:51:07 -07001089
1090 error = device_create_sys_dev_entry(dev);
1091 if (error)
1092 goto devtattrError;
Kay Sievers2b2af542009-04-30 15:23:42 +02001093
1094 devtmpfs_create_node(dev);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001095 }
1096
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001097 error = device_add_class_symlinks(dev);
1098 if (error)
1099 goto SymlinkError;
Cornelia Huckdc0afa82007-07-09 11:39:18 -07001100 error = device_add_attrs(dev);
1101 if (error)
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07001102 goto AttrsError;
Cornelia Huckdc0afa82007-07-09 11:39:18 -07001103 error = bus_add_device(dev);
1104 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 goto BusError;
Alan Stern3b98aea2008-08-07 13:06:12 -04001106 error = dpm_sysfs_add(dev);
Rafael J. Wysocki57eee3d2008-03-12 00:59:38 +01001107 if (error)
Alan Stern3b98aea2008-08-07 13:06:12 -04001108 goto DPMError;
1109 device_pm_add(dev);
Alan Sternec0676ee2008-12-05 14:10:31 -05001110
1111 /* Notify clients of device addition. This call must come
majianpeng268863f2012-01-11 15:12:06 +00001112 * after dpm_sysfs_add() and before kobject_uevent().
Alan Sternec0676ee2008-12-05 14:10:31 -05001113 */
1114 if (dev->bus)
1115 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
1116 BUS_NOTIFY_ADD_DEVICE, dev);
1117
Cornelia Huck83b5fb4c2007-03-29 11:12:11 +02001118 kobject_uevent(&dev->kobj, KOBJ_ADD);
Alan Stern2023c612009-07-30 15:27:18 -04001119 bus_probe_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 if (parent)
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08001121 klist_add_tail(&dev->p->knode_parent,
1122 &parent->p->klist_children);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123
Greg Kroah-Hartman5d9fd162006-06-22 17:17:32 -07001124 if (dev->class) {
Kay Sieversca22e562011-12-14 14:29:38 -08001125 mutex_lock(&dev->class->p->mutex);
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02001126 /* tie the class to the device */
Tejun Heo5a3ceb82008-08-25 19:50:19 +02001127 klist_add_tail(&dev->knode_class,
Kay Sievers6b6e39a2010-11-15 23:13:18 +01001128 &dev->class->p->klist_devices);
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02001129
1130 /* notify any interfaces that the device is here */
Greg Kroah-Hartman184f1f72008-05-28 09:28:39 -07001131 list_for_each_entry(class_intf,
Kay Sieversca22e562011-12-14 14:29:38 -08001132 &dev->class->p->interfaces, node)
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02001133 if (class_intf->add_dev)
1134 class_intf->add_dev(dev, class_intf);
Kay Sieversca22e562011-12-14 14:29:38 -08001135 mutex_unlock(&dev->class->p->mutex);
Greg Kroah-Hartman5d9fd162006-06-22 17:17:32 -07001136 }
Greg Kroah-Hartmanc906a482008-05-30 10:45:12 -07001137done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 put_device(dev);
1139 return error;
Alan Stern3b98aea2008-08-07 13:06:12 -04001140 DPMError:
Rafael J. Wysocki57eee3d2008-03-12 00:59:38 +01001141 bus_remove_device(dev);
1142 BusError:
James Simmons82f0cf92007-02-21 17:44:51 +00001143 device_remove_attrs(dev);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07001144 AttrsError:
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001145 device_remove_class_symlinks(dev);
1146 SymlinkError:
Tejun Heoad6a1e12007-06-14 03:45:17 +09001147 if (MAJOR(dev->devt))
Kay Sieversad729562009-10-28 19:51:37 +01001148 devtmpfs_delete_node(dev);
1149 if (MAJOR(dev->devt))
Dan Williamse105b8b2008-04-21 10:51:07 -07001150 device_remove_sys_dev_entry(dev);
1151 devtattrError:
1152 if (MAJOR(dev->devt))
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07001153 device_remove_file(dev, &dev_attr_dev);
Cornelia Hucka306eea2006-09-22 11:37:13 +02001154 ueventattrError:
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07001155 device_remove_file(dev, &dev_attr_uevent);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001156 attrError:
Kay Sievers312c0042005-11-16 09:00:00 +01001157 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 kobject_del(&dev->kobj);
1159 Error:
Cornelia Huck63b69712008-01-21 16:09:44 +01001160 cleanup_device_parent(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 if (parent)
1162 put_device(parent);
Kay Sievers5c8563d2009-05-28 14:24:07 -07001163name_error:
1164 kfree(dev->p);
1165 dev->p = NULL;
Greg Kroah-Hartmanc906a482008-05-30 10:45:12 -07001166 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167}
David Graham White86df2682013-07-21 20:41:14 -04001168EXPORT_SYMBOL_GPL(device_add);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001171 * device_register - register a device with the system.
1172 * @dev: pointer to the device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001174 * This happens in two clean steps - initialize the device
1175 * and add it to the system. The two steps can be called
1176 * separately, but this is the easiest and most common.
1177 * I.e. you should only call the two helpers separately if
1178 * have a clearly defined need to use and refcount the device
1179 * before it is added to the hierarchy.
Cornelia Huck57394112008-09-03 18:26:40 +02001180 *
Alan Sternb10d5ef2012-01-17 11:39:00 -05001181 * For more information, see the kerneldoc for device_initialize()
1182 * and device_add().
1183 *
Cornelia Huck57394112008-09-03 18:26:40 +02001184 * NOTE: _Never_ directly free @dev after calling this function, even
1185 * if it returned an error! Always use put_device() to give up the
1186 * reference initialized in this function instead.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188int device_register(struct device *dev)
1189{
1190 device_initialize(dev);
1191 return device_add(dev);
1192}
David Graham White86df2682013-07-21 20:41:14 -04001193EXPORT_SYMBOL_GPL(device_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001196 * get_device - increment reference count for device.
1197 * @dev: device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001199 * This simply forwards the call to kobject_get(), though
1200 * we do take care to provide for the case that we get a NULL
1201 * pointer passed in.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001203struct device *get_device(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +02001205 return dev ? kobj_to_dev(kobject_get(&dev->kobj)) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206}
David Graham White86df2682013-07-21 20:41:14 -04001207EXPORT_SYMBOL_GPL(get_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001210 * put_device - decrement reference count.
1211 * @dev: device in question.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001213void put_device(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214{
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001215 /* might_sleep(); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 if (dev)
1217 kobject_put(&dev->kobj);
1218}
David Graham White86df2682013-07-21 20:41:14 -04001219EXPORT_SYMBOL_GPL(put_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001222 * device_del - delete device from system.
1223 * @dev: device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001225 * This is the first part of the device unregistration
1226 * sequence. This removes the device from the lists we control
1227 * from here, has it removed from the other driver model
1228 * subsystems it was added to in device_add(), and removes it
1229 * from the kobject hierarchy.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001231 * NOTE: this should be called manually _iff_ device_add() was
1232 * also called manually.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001234void device_del(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001236 struct device *parent = dev->parent;
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02001237 struct class_interface *class_intf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238
Alan Sternec0676ee2008-12-05 14:10:31 -05001239 /* Notify clients of device removal. This call must come
1240 * before dpm_sysfs_remove().
1241 */
1242 if (dev->bus)
1243 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
1244 BUS_NOTIFY_DEL_DEVICE, dev);
Alan Stern3b98aea2008-08-07 13:06:12 -04001245 dpm_sysfs_remove(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 if (parent)
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08001247 klist_del(&dev->p->knode_parent);
Dan Williamse105b8b2008-04-21 10:51:07 -07001248 if (MAJOR(dev->devt)) {
Kay Sievers2b2af542009-04-30 15:23:42 +02001249 devtmpfs_delete_node(dev);
Dan Williamse105b8b2008-04-21 10:51:07 -07001250 device_remove_sys_dev_entry(dev);
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07001251 device_remove_file(dev, &dev_attr_dev);
Dan Williamse105b8b2008-04-21 10:51:07 -07001252 }
Kay Sieversb9d9c822006-06-15 15:31:56 +02001253 if (dev->class) {
Kay Sieversda231fd2007-11-21 17:29:15 +01001254 device_remove_class_symlinks(dev);
Kay Sievers99ef3ef2006-09-14 11:23:28 +02001255
Kay Sieversca22e562011-12-14 14:29:38 -08001256 mutex_lock(&dev->class->p->mutex);
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02001257 /* notify any interfaces that the device is now gone */
Greg Kroah-Hartman184f1f72008-05-28 09:28:39 -07001258 list_for_each_entry(class_intf,
Kay Sieversca22e562011-12-14 14:29:38 -08001259 &dev->class->p->interfaces, node)
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02001260 if (class_intf->remove_dev)
1261 class_intf->remove_dev(dev, class_intf);
1262 /* remove the device from the class list */
Tejun Heo5a3ceb82008-08-25 19:50:19 +02001263 klist_del(&dev->knode_class);
Kay Sieversca22e562011-12-14 14:29:38 -08001264 mutex_unlock(&dev->class->p->mutex);
Kay Sieversb9d9c822006-06-15 15:31:56 +02001265 }
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07001266 device_remove_file(dev, &dev_attr_uevent);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07001267 device_remove_attrs(dev);
Benjamin Herrenschmidt28953532006-11-08 19:46:14 -08001268 bus_remove_device(dev);
LongX Zhang4b6d1f122012-10-25 00:21:28 +02001269 device_pm_remove(dev);
Grant Likelyd1c34142012-03-05 08:47:41 -07001270 driver_deferred_probe_del(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271
1272 /* Notify the platform of the removal, in case they
1273 * need to do anything...
1274 */
1275 if (platform_notify_remove)
1276 platform_notify_remove(dev);
Kay Sievers312c0042005-11-16 09:00:00 +01001277 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
Kay Sieversda231fd2007-11-21 17:29:15 +01001278 cleanup_device_parent(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 kobject_del(&dev->kobj);
Kay Sieversda231fd2007-11-21 17:29:15 +01001280 put_device(parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281}
David Graham White86df2682013-07-21 20:41:14 -04001282EXPORT_SYMBOL_GPL(device_del);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283
1284/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001285 * device_unregister - unregister device from system.
1286 * @dev: device going away.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001288 * We do this in two parts, like we do device_register(). First,
1289 * we remove it from all the subsystems with device_del(), then
1290 * we decrement the reference count via put_device(). If that
1291 * is the final reference count, the device will be cleaned up
1292 * via device_release() above. Otherwise, the structure will
1293 * stick around until the final reference to the device is dropped.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001295void device_unregister(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296{
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01001297 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 device_del(dev);
1299 put_device(dev);
1300}
David Graham White86df2682013-07-21 20:41:14 -04001301EXPORT_SYMBOL_GPL(device_unregister);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001303static struct device *next_device(struct klist_iter *i)
mochel@digitalimplant.org36239572005-03-24 19:08:30 -08001304{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001305 struct klist_node *n = klist_next(i);
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08001306 struct device *dev = NULL;
1307 struct device_private *p;
1308
1309 if (n) {
1310 p = to_device_private_parent(n);
1311 dev = p->device;
1312 }
1313 return dev;
mochel@digitalimplant.org36239572005-03-24 19:08:30 -08001314}
1315
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316/**
Kay Sieverse454cea2009-09-18 23:01:12 +02001317 * device_get_devnode - path of device node file
Kay Sievers6fcf53a2009-04-30 15:23:42 +02001318 * @dev: device
Kay Sieverse454cea2009-09-18 23:01:12 +02001319 * @mode: returned file access mode
Kay Sievers3c2670e2013-04-06 09:56:00 -07001320 * @uid: returned file owner
1321 * @gid: returned file group
Kay Sievers6fcf53a2009-04-30 15:23:42 +02001322 * @tmp: possibly allocated string
1323 *
1324 * Return the relative path of a possible device node.
1325 * Non-default names may need to allocate a memory to compose
1326 * a name. This memory is returned in tmp and needs to be
1327 * freed by the caller.
1328 */
Kay Sieverse454cea2009-09-18 23:01:12 +02001329const char *device_get_devnode(struct device *dev,
Greg Kroah-Hartman4e4098a2013-04-11 11:43:29 -07001330 umode_t *mode, kuid_t *uid, kgid_t *gid,
Kay Sievers3c2670e2013-04-06 09:56:00 -07001331 const char **tmp)
Kay Sievers6fcf53a2009-04-30 15:23:42 +02001332{
1333 char *s;
1334
1335 *tmp = NULL;
1336
1337 /* the device type may provide a specific name */
Kay Sieverse454cea2009-09-18 23:01:12 +02001338 if (dev->type && dev->type->devnode)
Kay Sievers3c2670e2013-04-06 09:56:00 -07001339 *tmp = dev->type->devnode(dev, mode, uid, gid);
Kay Sievers6fcf53a2009-04-30 15:23:42 +02001340 if (*tmp)
1341 return *tmp;
1342
1343 /* the class may provide a specific name */
Kay Sieverse454cea2009-09-18 23:01:12 +02001344 if (dev->class && dev->class->devnode)
1345 *tmp = dev->class->devnode(dev, mode);
Kay Sievers6fcf53a2009-04-30 15:23:42 +02001346 if (*tmp)
1347 return *tmp;
1348
1349 /* return name without allocation, tmp == NULL */
1350 if (strchr(dev_name(dev), '!') == NULL)
1351 return dev_name(dev);
1352
1353 /* replace '!' in the name with '/' */
1354 *tmp = kstrdup(dev_name(dev), GFP_KERNEL);
1355 if (!*tmp)
1356 return NULL;
1357 while ((s = strchr(*tmp, '!')))
1358 s[0] = '/';
1359 return *tmp;
1360}
1361
1362/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001363 * device_for_each_child - device child iterator.
1364 * @parent: parent struct device.
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001365 * @fn: function to be called for each device.
Robert P. J. Dayf8878dc2013-06-01 20:17:34 -04001366 * @data: data for the callback.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001368 * Iterate over @parent's child devices, and call @fn for each,
1369 * passing it @data.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001371 * We check the return of @fn each time. If it returns anything
1372 * other than 0, we break out and return that value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001374int device_for_each_child(struct device *parent, void *data,
1375 int (*fn)(struct device *dev, void *data))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376{
mochel@digitalimplant.org36239572005-03-24 19:08:30 -08001377 struct klist_iter i;
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001378 struct device *child;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 int error = 0;
1380
Greg Kroah-Hartman014c90db2009-04-15 16:00:12 -07001381 if (!parent->p)
1382 return 0;
1383
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08001384 klist_iter_init(&parent->p->klist_children, &i);
mochel@digitalimplant.org36239572005-03-24 19:08:30 -08001385 while ((child = next_device(&i)) && !error)
1386 error = fn(child, data);
1387 klist_iter_exit(&i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 return error;
1389}
David Graham White86df2682013-07-21 20:41:14 -04001390EXPORT_SYMBOL_GPL(device_for_each_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391
Cornelia Huck5ab69982006-11-16 15:42:07 +01001392/**
1393 * device_find_child - device iterator for locating a particular device.
1394 * @parent: parent struct device
Cornelia Huck5ab69982006-11-16 15:42:07 +01001395 * @match: Callback function to check device
Robert P. J. Dayf8878dc2013-06-01 20:17:34 -04001396 * @data: Data to pass to match function
Cornelia Huck5ab69982006-11-16 15:42:07 +01001397 *
1398 * This is similar to the device_for_each_child() function above, but it
1399 * returns a reference to a device that is 'found' for later use, as
1400 * determined by the @match callback.
1401 *
1402 * The callback should return 0 if the device doesn't match and non-zero
1403 * if it does. If the callback returns non-zero and a reference to the
1404 * current device can be obtained, this function will return to the caller
1405 * and not iterate over any more devices.
Federico Vagaa4e24002013-04-15 11:18:11 +02001406 *
1407 * NOTE: you will need to drop the reference with put_device() after use.
Cornelia Huck5ab69982006-11-16 15:42:07 +01001408 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001409struct device *device_find_child(struct device *parent, void *data,
1410 int (*match)(struct device *dev, void *data))
Cornelia Huck5ab69982006-11-16 15:42:07 +01001411{
1412 struct klist_iter i;
1413 struct device *child;
1414
1415 if (!parent)
1416 return NULL;
1417
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08001418 klist_iter_init(&parent->p->klist_children, &i);
Cornelia Huck5ab69982006-11-16 15:42:07 +01001419 while ((child = next_device(&i)))
1420 if (match(child, data) && get_device(child))
1421 break;
1422 klist_iter_exit(&i);
1423 return child;
1424}
David Graham White86df2682013-07-21 20:41:14 -04001425EXPORT_SYMBOL_GPL(device_find_child);
Cornelia Huck5ab69982006-11-16 15:42:07 +01001426
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427int __init devices_init(void)
1428{
Greg Kroah-Hartman881c6cfd2007-11-01 09:29:06 -06001429 devices_kset = kset_create_and_add("devices", &device_uevent_ops, NULL);
1430 if (!devices_kset)
1431 return -ENOMEM;
Dan Williamse105b8b2008-04-21 10:51:07 -07001432 dev_kobj = kobject_create_and_add("dev", NULL);
1433 if (!dev_kobj)
1434 goto dev_kobj_err;
1435 sysfs_dev_block_kobj = kobject_create_and_add("block", dev_kobj);
1436 if (!sysfs_dev_block_kobj)
1437 goto block_kobj_err;
1438 sysfs_dev_char_kobj = kobject_create_and_add("char", dev_kobj);
1439 if (!sysfs_dev_char_kobj)
1440 goto char_kobj_err;
1441
Greg Kroah-Hartman881c6cfd2007-11-01 09:29:06 -06001442 return 0;
Dan Williamse105b8b2008-04-21 10:51:07 -07001443
1444 char_kobj_err:
1445 kobject_put(sysfs_dev_block_kobj);
1446 block_kobj_err:
1447 kobject_put(dev_kobj);
1448 dev_kobj_err:
1449 kset_unregister(devices_kset);
1450 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451}
1452
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02001453static DEFINE_MUTEX(device_hotplug_lock);
1454
1455void lock_device_hotplug(void)
1456{
1457 mutex_lock(&device_hotplug_lock);
1458}
1459
1460void unlock_device_hotplug(void)
1461{
1462 mutex_unlock(&device_hotplug_lock);
1463}
1464
1465static int device_check_offline(struct device *dev, void *not_used)
1466{
1467 int ret;
1468
1469 ret = device_for_each_child(dev, NULL, device_check_offline);
1470 if (ret)
1471 return ret;
1472
1473 return device_supports_offline(dev) && !dev->offline ? -EBUSY : 0;
1474}
1475
1476/**
1477 * device_offline - Prepare the device for hot-removal.
1478 * @dev: Device to be put offline.
1479 *
1480 * Execute the device bus type's .offline() callback, if present, to prepare
1481 * the device for a subsequent hot-removal. If that succeeds, the device must
1482 * not be used until either it is removed or its bus type's .online() callback
1483 * is executed.
1484 *
1485 * Call under device_hotplug_lock.
1486 */
1487int device_offline(struct device *dev)
1488{
1489 int ret;
1490
1491 if (dev->offline_disabled)
1492 return -EPERM;
1493
1494 ret = device_for_each_child(dev, NULL, device_check_offline);
1495 if (ret)
1496 return ret;
1497
1498 device_lock(dev);
1499 if (device_supports_offline(dev)) {
1500 if (dev->offline) {
1501 ret = 1;
1502 } else {
1503 ret = dev->bus->offline(dev);
1504 if (!ret) {
1505 kobject_uevent(&dev->kobj, KOBJ_OFFLINE);
1506 dev->offline = true;
1507 }
1508 }
1509 }
1510 device_unlock(dev);
1511
1512 return ret;
1513}
1514
1515/**
1516 * device_online - Put the device back online after successful device_offline().
1517 * @dev: Device to be put back online.
1518 *
1519 * If device_offline() has been successfully executed for @dev, but the device
1520 * has not been removed subsequently, execute its bus type's .online() callback
1521 * to indicate that the device can be used again.
1522 *
1523 * Call under device_hotplug_lock.
1524 */
1525int device_online(struct device *dev)
1526{
1527 int ret = 0;
1528
1529 device_lock(dev);
1530 if (device_supports_offline(dev)) {
1531 if (dev->offline) {
1532 ret = dev->bus->online(dev);
1533 if (!ret) {
1534 kobject_uevent(&dev->kobj, KOBJ_ONLINE);
1535 dev->offline = false;
1536 }
1537 } else {
1538 ret = 1;
1539 }
1540 }
1541 device_unlock(dev);
1542
1543 return ret;
1544}
1545
Karthigan Srinivasan7f100d12011-04-18 16:16:52 -05001546struct root_device {
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00001547 struct device dev;
1548 struct module *owner;
1549};
1550
Josh Triplett93058422012-11-18 21:27:55 -08001551static inline struct root_device *to_root_device(struct device *d)
Ferenc Wagner481e2072011-01-07 15:17:47 +01001552{
1553 return container_of(d, struct root_device, dev);
1554}
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00001555
1556static void root_device_release(struct device *dev)
1557{
1558 kfree(to_root_device(dev));
1559}
1560
1561/**
1562 * __root_device_register - allocate and register a root device
1563 * @name: root device name
1564 * @owner: owner module of the root device, usually THIS_MODULE
1565 *
1566 * This function allocates a root device and registers it
1567 * using device_register(). In order to free the returned
1568 * device, use root_device_unregister().
1569 *
1570 * Root devices are dummy devices which allow other devices
1571 * to be grouped under /sys/devices. Use this function to
1572 * allocate a root device and then use it as the parent of
1573 * any device which should appear under /sys/devices/{name}
1574 *
1575 * The /sys/devices/{name} directory will also contain a
1576 * 'module' symlink which points to the @owner directory
1577 * in sysfs.
1578 *
Jani Nikulaf0eae0e2010-03-11 18:11:45 +02001579 * Returns &struct device pointer on success, or ERR_PTR() on error.
1580 *
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00001581 * Note: You probably want to use root_device_register().
1582 */
1583struct device *__root_device_register(const char *name, struct module *owner)
1584{
1585 struct root_device *root;
1586 int err = -ENOMEM;
1587
1588 root = kzalloc(sizeof(struct root_device), GFP_KERNEL);
1589 if (!root)
1590 return ERR_PTR(err);
1591
Greg Kroah-Hartmanacc0e902009-06-02 15:39:55 -07001592 err = dev_set_name(&root->dev, "%s", name);
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00001593 if (err) {
1594 kfree(root);
1595 return ERR_PTR(err);
1596 }
1597
1598 root->dev.release = root_device_release;
1599
1600 err = device_register(&root->dev);
1601 if (err) {
1602 put_device(&root->dev);
1603 return ERR_PTR(err);
1604 }
1605
Christoph Egger1d9e8822010-05-17 16:57:58 +02001606#ifdef CONFIG_MODULES /* gotta find a "cleaner" way to do this */
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00001607 if (owner) {
1608 struct module_kobject *mk = &owner->mkobj;
1609
1610 err = sysfs_create_link(&root->dev.kobj, &mk->kobj, "module");
1611 if (err) {
1612 device_unregister(&root->dev);
1613 return ERR_PTR(err);
1614 }
1615 root->owner = owner;
1616 }
1617#endif
1618
1619 return &root->dev;
1620}
1621EXPORT_SYMBOL_GPL(__root_device_register);
1622
1623/**
1624 * root_device_unregister - unregister and free a root device
Randy Dunlap7cbcf222009-01-20 16:29:13 -08001625 * @dev: device going away
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00001626 *
1627 * This function unregisters and cleans up a device that was created by
1628 * root_device_register().
1629 */
1630void root_device_unregister(struct device *dev)
1631{
1632 struct root_device *root = to_root_device(dev);
1633
1634 if (root->owner)
1635 sysfs_remove_link(&root->dev.kobj, "module");
1636
1637 device_unregister(dev);
1638}
1639EXPORT_SYMBOL_GPL(root_device_unregister);
1640
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001641
1642static void device_create_release(struct device *dev)
1643{
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01001644 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001645 kfree(dev);
1646}
1647
Guenter Roeck39ef3112013-07-14 16:05:57 -07001648static struct device *
1649device_create_groups_vargs(struct class *class, struct device *parent,
1650 dev_t devt, void *drvdata,
1651 const struct attribute_group **groups,
1652 const char *fmt, va_list args)
1653{
1654 struct device *dev = NULL;
1655 int retval = -ENODEV;
1656
1657 if (class == NULL || IS_ERR(class))
1658 goto error;
1659
1660 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1661 if (!dev) {
1662 retval = -ENOMEM;
1663 goto error;
1664 }
1665
1666 dev->devt = devt;
1667 dev->class = class;
1668 dev->parent = parent;
1669 dev->groups = groups;
1670 dev->release = device_create_release;
1671 dev_set_drvdata(dev, drvdata);
1672
1673 retval = kobject_set_name_vargs(&dev->kobj, fmt, args);
1674 if (retval)
1675 goto error;
1676
1677 retval = device_register(dev);
1678 if (retval)
1679 goto error;
1680
1681 return dev;
1682
1683error:
1684 put_device(dev);
1685 return ERR_PTR(retval);
1686}
1687
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001688/**
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07001689 * device_create_vargs - creates a device and registers it with sysfs
1690 * @class: pointer to the struct class that this device should be registered to
1691 * @parent: pointer to the parent struct device of this new device, if any
1692 * @devt: the dev_t for the char device to be added
1693 * @drvdata: the data to be added to the device for callbacks
1694 * @fmt: string for the device's name
1695 * @args: va_list for the device's name
1696 *
1697 * This function can be used by char device classes. A struct device
1698 * will be created in sysfs, registered to the specified class.
1699 *
1700 * A "dev" file will be created, showing the dev_t for the device, if
1701 * the dev_t is not 0,0.
1702 * If a pointer to a parent struct device is passed in, the newly created
1703 * struct device will be a child of that device in sysfs.
1704 * The pointer to the struct device will be returned from the call.
1705 * Any further sysfs files that might be required can be created using this
1706 * pointer.
1707 *
Jani Nikulaf0eae0e2010-03-11 18:11:45 +02001708 * Returns &struct device pointer on success, or ERR_PTR() on error.
1709 *
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07001710 * Note: the struct class passed to this function must have previously
1711 * been created with a call to class_create().
1712 */
1713struct device *device_create_vargs(struct class *class, struct device *parent,
1714 dev_t devt, void *drvdata, const char *fmt,
1715 va_list args)
1716{
Guenter Roeck39ef3112013-07-14 16:05:57 -07001717 return device_create_groups_vargs(class, parent, devt, drvdata, NULL,
1718 fmt, args);
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07001719}
1720EXPORT_SYMBOL_GPL(device_create_vargs);
1721
1722/**
Greg Kroah-Hartman4e106732008-07-21 20:03:34 -07001723 * device_create - creates a device and registers it with sysfs
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07001724 * @class: pointer to the struct class that this device should be registered to
1725 * @parent: pointer to the parent struct device of this new device, if any
1726 * @devt: the dev_t for the char device to be added
1727 * @drvdata: the data to be added to the device for callbacks
1728 * @fmt: string for the device's name
1729 *
1730 * This function can be used by char device classes. A struct device
1731 * will be created in sysfs, registered to the specified class.
1732 *
1733 * A "dev" file will be created, showing the dev_t for the device, if
1734 * the dev_t is not 0,0.
1735 * If a pointer to a parent struct device is passed in, the newly created
1736 * struct device will be a child of that device in sysfs.
1737 * The pointer to the struct device will be returned from the call.
1738 * Any further sysfs files that might be required can be created using this
1739 * pointer.
1740 *
Jani Nikulaf0eae0e2010-03-11 18:11:45 +02001741 * Returns &struct device pointer on success, or ERR_PTR() on error.
1742 *
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07001743 * Note: the struct class passed to this function must have previously
1744 * been created with a call to class_create().
1745 */
Greg Kroah-Hartman4e106732008-07-21 20:03:34 -07001746struct device *device_create(struct class *class, struct device *parent,
1747 dev_t devt, void *drvdata, const char *fmt, ...)
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07001748{
1749 va_list vargs;
1750 struct device *dev;
1751
1752 va_start(vargs, fmt);
1753 dev = device_create_vargs(class, parent, devt, drvdata, fmt, vargs);
1754 va_end(vargs);
1755 return dev;
1756}
Greg Kroah-Hartman4e106732008-07-21 20:03:34 -07001757EXPORT_SYMBOL_GPL(device_create);
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07001758
Guenter Roeck39ef3112013-07-14 16:05:57 -07001759/**
1760 * device_create_with_groups - creates a device and registers it with sysfs
1761 * @class: pointer to the struct class that this device should be registered to
1762 * @parent: pointer to the parent struct device of this new device, if any
1763 * @devt: the dev_t for the char device to be added
1764 * @drvdata: the data to be added to the device for callbacks
1765 * @groups: NULL-terminated list of attribute groups to be created
1766 * @fmt: string for the device's name
1767 *
1768 * This function can be used by char device classes. A struct device
1769 * will be created in sysfs, registered to the specified class.
1770 * Additional attributes specified in the groups parameter will also
1771 * be created automatically.
1772 *
1773 * A "dev" file will be created, showing the dev_t for the device, if
1774 * the dev_t is not 0,0.
1775 * If a pointer to a parent struct device is passed in, the newly created
1776 * struct device will be a child of that device in sysfs.
1777 * The pointer to the struct device will be returned from the call.
1778 * Any further sysfs files that might be required can be created using this
1779 * pointer.
1780 *
1781 * Returns &struct device pointer on success, or ERR_PTR() on error.
1782 *
1783 * Note: the struct class passed to this function must have previously
1784 * been created with a call to class_create().
1785 */
1786struct device *device_create_with_groups(struct class *class,
1787 struct device *parent, dev_t devt,
1788 void *drvdata,
1789 const struct attribute_group **groups,
1790 const char *fmt, ...)
1791{
1792 va_list vargs;
1793 struct device *dev;
1794
1795 va_start(vargs, fmt);
1796 dev = device_create_groups_vargs(class, parent, devt, drvdata, groups,
1797 fmt, vargs);
1798 va_end(vargs);
1799 return dev;
1800}
1801EXPORT_SYMBOL_GPL(device_create_with_groups);
1802
Michał Mirosław9f3b7952013-02-01 20:40:17 +01001803static int __match_devt(struct device *dev, const void *data)
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001804{
Michał Mirosław9f3b7952013-02-01 20:40:17 +01001805 const dev_t *devt = data;
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001806
Dave Youngcd354492008-01-28 16:56:11 +08001807 return dev->devt == *devt;
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001808}
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001809
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001810/**
1811 * device_destroy - removes a device that was created with device_create()
1812 * @class: pointer to the struct class that this device was registered with
1813 * @devt: the dev_t of the device that was previously registered
1814 *
1815 * This call unregisters and cleans up a device that was created with a
1816 * call to device_create().
1817 */
1818void device_destroy(struct class *class, dev_t devt)
1819{
1820 struct device *dev;
1821
Greg Kroah-Hartman695794a2008-05-22 17:21:08 -04001822 dev = class_find_device(class, NULL, &devt, __match_devt);
Dave Youngcd354492008-01-28 16:56:11 +08001823 if (dev) {
1824 put_device(dev);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001825 device_unregister(dev);
Dave Youngcd354492008-01-28 16:56:11 +08001826 }
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001827}
1828EXPORT_SYMBOL_GPL(device_destroy);
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001829
1830/**
1831 * device_rename - renames a device
1832 * @dev: the pointer to the struct device to be renamed
1833 * @new_name: the new name of the device
Eric W. Biederman030c1d22008-05-08 14:41:00 -07001834 *
1835 * It is the responsibility of the caller to provide mutual
1836 * exclusion between two different calls of device_rename
1837 * on the same device to ensure that new_name is valid and
1838 * won't conflict with other devices.
Michael Ellermanc6c0ac62010-11-25 09:44:07 +11001839 *
Timur Tabia5462512010-12-13 14:08:52 -06001840 * Note: Don't call this function. Currently, the networking layer calls this
1841 * function, but that will change. The following text from Kay Sievers offers
1842 * some insight:
1843 *
1844 * Renaming devices is racy at many levels, symlinks and other stuff are not
1845 * replaced atomically, and you get a "move" uevent, but it's not easy to
1846 * connect the event to the old and new device. Device nodes are not renamed at
1847 * all, there isn't even support for that in the kernel now.
1848 *
1849 * In the meantime, during renaming, your target name might be taken by another
1850 * driver, creating conflicts. Or the old name is taken directly after you
1851 * renamed it -- then you get events for the same DEVPATH, before you even see
1852 * the "move" event. It's just a mess, and nothing new should ever rely on
1853 * kernel device renaming. Besides that, it's not even implemented now for
1854 * other things than (driver-core wise very simple) network devices.
1855 *
1856 * We are currently about to change network renaming in udev to completely
1857 * disallow renaming of devices in the same namespace as the kernel uses,
1858 * because we can't solve the problems properly, that arise with swapping names
1859 * of multiple interfaces without races. Means, renaming of eth[0-9]* will only
1860 * be allowed to some other name than eth[0-9]*, for the aforementioned
1861 * reasons.
1862 *
1863 * Make up a "real" name in the driver before you register anything, or add
1864 * some other attributes for userspace to find the device, or use udev to add
1865 * symlinks -- but never rename kernel devices later, it's a complete mess. We
1866 * don't even want to get into that and try to implement the missing pieces in
1867 * the core. We really have other pieces to fix in the driver core mess. :)
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001868 */
Johannes Berg6937e8f2010-08-05 17:38:18 +02001869int device_rename(struct device *dev, const char *new_name)
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001870{
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001871 char *old_device_name = NULL;
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001872 int error;
1873
1874 dev = get_device(dev);
1875 if (!dev)
1876 return -EINVAL;
1877
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01001878 pr_debug("device: '%s': %s: renaming to '%s'\n", dev_name(dev),
Harvey Harrison2b3a3022008-03-04 16:41:05 -08001879 __func__, new_name);
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001880
Kay Sievers1fa5ae82009-01-25 15:17:37 +01001881 old_device_name = kstrdup(dev_name(dev), GFP_KERNEL);
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001882 if (!old_device_name) {
1883 error = -ENOMEM;
1884 goto out;
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001885 }
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001886
Eric W. Biedermanf349cf32010-03-30 11:31:29 -07001887 if (dev->class) {
Kay Sievers6b6e39a2010-11-15 23:13:18 +01001888 error = sysfs_rename_link(&dev->class->p->subsys.kobj,
Eric W. Biedermanf349cf32010-03-30 11:31:29 -07001889 &dev->kobj, old_device_name, new_name);
1890 if (error)
1891 goto out;
1892 }
Kay Sievers39aba962010-09-04 22:33:14 -07001893
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001894 error = kobject_rename(&dev->kobj, new_name);
Kay Sievers1fa5ae82009-01-25 15:17:37 +01001895 if (error)
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001896 goto out;
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001897
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001898out:
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001899 put_device(dev);
1900
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001901 kfree(old_device_name);
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001902
1903 return error;
1904}
Johannes Berga2807db2007-02-28 12:38:31 +01001905EXPORT_SYMBOL_GPL(device_rename);
Cornelia Huck8a824722006-11-20 17:07:51 +01001906
1907static int device_move_class_links(struct device *dev,
1908 struct device *old_parent,
1909 struct device *new_parent)
1910{
Greg Kroah-Hartmanf7f34612007-03-06 12:55:53 -08001911 int error = 0;
Cornelia Huck8a824722006-11-20 17:07:51 +01001912
Greg Kroah-Hartmanf7f34612007-03-06 12:55:53 -08001913 if (old_parent)
1914 sysfs_remove_link(&dev->kobj, "device");
1915 if (new_parent)
1916 error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
1917 "device");
1918 return error;
Cornelia Huck8a824722006-11-20 17:07:51 +01001919}
1920
1921/**
1922 * device_move - moves a device to a new parent
1923 * @dev: the pointer to the struct device to be moved
Cornelia Huckc744aeae2007-01-08 20:16:44 +01001924 * @new_parent: the new parent of the device (can by NULL)
Cornelia Huckffa6a702009-03-04 12:44:00 +01001925 * @dpm_order: how to reorder the dpm_list
Cornelia Huck8a824722006-11-20 17:07:51 +01001926 */
Cornelia Huckffa6a702009-03-04 12:44:00 +01001927int device_move(struct device *dev, struct device *new_parent,
1928 enum dpm_order dpm_order)
Cornelia Huck8a824722006-11-20 17:07:51 +01001929{
1930 int error;
1931 struct device *old_parent;
Cornelia Huckc744aeae2007-01-08 20:16:44 +01001932 struct kobject *new_parent_kobj;
Cornelia Huck8a824722006-11-20 17:07:51 +01001933
1934 dev = get_device(dev);
1935 if (!dev)
1936 return -EINVAL;
1937
Cornelia Huckffa6a702009-03-04 12:44:00 +01001938 device_pm_lock();
Cornelia Huck8a824722006-11-20 17:07:51 +01001939 new_parent = get_device(new_parent);
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001940 new_parent_kobj = get_device_parent(dev, new_parent);
Cornelia Huck63b69712008-01-21 16:09:44 +01001941
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01001942 pr_debug("device: '%s': %s: moving to '%s'\n", dev_name(dev),
1943 __func__, new_parent ? dev_name(new_parent) : "<NULL>");
Cornelia Huckc744aeae2007-01-08 20:16:44 +01001944 error = kobject_move(&dev->kobj, new_parent_kobj);
Cornelia Huck8a824722006-11-20 17:07:51 +01001945 if (error) {
Cornelia Huck63b69712008-01-21 16:09:44 +01001946 cleanup_glue_dir(dev, new_parent_kobj);
Cornelia Huck8a824722006-11-20 17:07:51 +01001947 put_device(new_parent);
1948 goto out;
1949 }
1950 old_parent = dev->parent;
1951 dev->parent = new_parent;
1952 if (old_parent)
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08001953 klist_remove(&dev->p->knode_parent);
Yinghai Lu0d358f22008-02-19 03:20:41 -08001954 if (new_parent) {
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08001955 klist_add_tail(&dev->p->knode_parent,
1956 &new_parent->p->klist_children);
Yinghai Lu0d358f22008-02-19 03:20:41 -08001957 set_dev_node(dev, dev_to_node(new_parent));
1958 }
1959
Rabin Vincentbdd40342012-04-23 09:16:36 +02001960 if (dev->class) {
1961 error = device_move_class_links(dev, old_parent, new_parent);
1962 if (error) {
1963 /* We ignore errors on cleanup since we're hosed anyway... */
1964 device_move_class_links(dev, new_parent, old_parent);
1965 if (!kobject_move(&dev->kobj, &old_parent->kobj)) {
1966 if (new_parent)
1967 klist_remove(&dev->p->knode_parent);
1968 dev->parent = old_parent;
1969 if (old_parent) {
1970 klist_add_tail(&dev->p->knode_parent,
1971 &old_parent->p->klist_children);
1972 set_dev_node(dev, dev_to_node(old_parent));
1973 }
Yinghai Lu0d358f22008-02-19 03:20:41 -08001974 }
Rabin Vincentbdd40342012-04-23 09:16:36 +02001975 cleanup_glue_dir(dev, new_parent_kobj);
1976 put_device(new_parent);
1977 goto out;
Cornelia Huck8a824722006-11-20 17:07:51 +01001978 }
Cornelia Huck8a824722006-11-20 17:07:51 +01001979 }
Cornelia Huckffa6a702009-03-04 12:44:00 +01001980 switch (dpm_order) {
1981 case DPM_ORDER_NONE:
1982 break;
1983 case DPM_ORDER_DEV_AFTER_PARENT:
1984 device_pm_move_after(dev, new_parent);
1985 break;
1986 case DPM_ORDER_PARENT_BEFORE_DEV:
1987 device_pm_move_before(new_parent, dev);
1988 break;
1989 case DPM_ORDER_DEV_LAST:
1990 device_pm_move_last(dev);
1991 break;
1992 }
Rabin Vincentbdd40342012-04-23 09:16:36 +02001993
Cornelia Huck8a824722006-11-20 17:07:51 +01001994 put_device(old_parent);
1995out:
Cornelia Huckffa6a702009-03-04 12:44:00 +01001996 device_pm_unlock();
Cornelia Huck8a824722006-11-20 17:07:51 +01001997 put_device(dev);
1998 return error;
1999}
Cornelia Huck8a824722006-11-20 17:07:51 +01002000EXPORT_SYMBOL_GPL(device_move);
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08002001
2002/**
2003 * device_shutdown - call ->shutdown() on each device to shutdown.
2004 */
2005void device_shutdown(void)
2006{
Hugh Daschbach62458382010-03-22 10:36:37 -07002007 struct device *dev;
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08002008
Hugh Daschbach62458382010-03-22 10:36:37 -07002009 spin_lock(&devices_kset->list_lock);
2010 /*
2011 * Walk the devices list backward, shutting down each in turn.
2012 * Beware that device unplug events may also start pulling
2013 * devices offline, even as the system is shutting down.
2014 */
2015 while (!list_empty(&devices_kset->list)) {
2016 dev = list_entry(devices_kset->list.prev, struct device,
2017 kobj.entry);
Ming Leid1c6c032012-06-22 18:01:40 +08002018
2019 /*
2020 * hold reference count of device's parent to
2021 * prevent it from being freed because parent's
2022 * lock is to be held
2023 */
2024 get_device(dev->parent);
Hugh Daschbach62458382010-03-22 10:36:37 -07002025 get_device(dev);
2026 /*
2027 * Make sure the device is off the kset list, in the
2028 * event that dev->*->shutdown() doesn't remove it.
2029 */
2030 list_del_init(&dev->kobj.entry);
2031 spin_unlock(&devices_kset->list_lock);
Alan Sternfe6b91f2011-12-06 23:24:52 +01002032
Ming Leid1c6c032012-06-22 18:01:40 +08002033 /* hold lock to avoid race with probe/release */
2034 if (dev->parent)
2035 device_lock(dev->parent);
2036 device_lock(dev);
2037
Alan Sternfe6b91f2011-12-06 23:24:52 +01002038 /* Don't allow any more runtime suspends */
2039 pm_runtime_get_noresume(dev);
2040 pm_runtime_barrier(dev);
Hugh Daschbach62458382010-03-22 10:36:37 -07002041
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08002042 if (dev->bus && dev->bus->shutdown) {
ShuoX Liu0246c4f2012-11-23 15:14:12 +08002043 if (initcall_debug)
2044 dev_info(dev, "shutdown\n");
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08002045 dev->bus->shutdown(dev);
2046 } else if (dev->driver && dev->driver->shutdown) {
ShuoX Liu0246c4f2012-11-23 15:14:12 +08002047 if (initcall_debug)
2048 dev_info(dev, "shutdown\n");
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08002049 dev->driver->shutdown(dev);
2050 }
Ming Leid1c6c032012-06-22 18:01:40 +08002051
2052 device_unlock(dev);
2053 if (dev->parent)
2054 device_unlock(dev->parent);
2055
Hugh Daschbach62458382010-03-22 10:36:37 -07002056 put_device(dev);
Ming Leid1c6c032012-06-22 18:01:40 +08002057 put_device(dev->parent);
Hugh Daschbach62458382010-03-22 10:36:37 -07002058
2059 spin_lock(&devices_kset->list_lock);
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08002060 }
Hugh Daschbach62458382010-03-22 10:36:37 -07002061 spin_unlock(&devices_kset->list_lock);
Shaohua Li401097e2009-05-12 13:37:57 -07002062 async_synchronize_full();
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08002063}
Joe Perches99bcf212010-06-27 01:02:34 +00002064
2065/*
2066 * Device logging functions
2067 */
2068
2069#ifdef CONFIG_PRINTK
Joe Perches666f3552012-09-12 20:14:11 -07002070static int
2071create_syslog_header(const struct device *dev, char *hdr, size_t hdrlen)
Joe Perches99bcf212010-06-27 01:02:34 +00002072{
Kay Sieversc4e00da2012-05-03 02:29:59 +02002073 const char *subsys;
Joe Perches798efc62012-09-12 20:11:29 -07002074 size_t pos = 0;
Joe Perches99bcf212010-06-27 01:02:34 +00002075
Kay Sieversc4e00da2012-05-03 02:29:59 +02002076 if (dev->class)
2077 subsys = dev->class->name;
2078 else if (dev->bus)
2079 subsys = dev->bus->name;
2080 else
Joe Perches798efc62012-09-12 20:11:29 -07002081 return 0;
Kay Sieversc4e00da2012-05-03 02:29:59 +02002082
Joe Perches798efc62012-09-12 20:11:29 -07002083 pos += snprintf(hdr + pos, hdrlen - pos, "SUBSYSTEM=%s", subsys);
Kay Sieversc4e00da2012-05-03 02:29:59 +02002084
2085 /*
2086 * Add device identifier DEVICE=:
2087 * b12:8 block dev_t
2088 * c127:3 char dev_t
2089 * n8 netdev ifindex
2090 * +sound:card0 subsystem:devname
2091 */
2092 if (MAJOR(dev->devt)) {
2093 char c;
2094
2095 if (strcmp(subsys, "block") == 0)
2096 c = 'b';
2097 else
2098 c = 'c';
Joe Perches798efc62012-09-12 20:11:29 -07002099 pos++;
2100 pos += snprintf(hdr + pos, hdrlen - pos,
2101 "DEVICE=%c%u:%u",
2102 c, MAJOR(dev->devt), MINOR(dev->devt));
Kay Sieversc4e00da2012-05-03 02:29:59 +02002103 } else if (strcmp(subsys, "net") == 0) {
2104 struct net_device *net = to_net_dev(dev);
2105
Joe Perches798efc62012-09-12 20:11:29 -07002106 pos++;
2107 pos += snprintf(hdr + pos, hdrlen - pos,
2108 "DEVICE=n%u", net->ifindex);
Kay Sieversc4e00da2012-05-03 02:29:59 +02002109 } else {
Joe Perches798efc62012-09-12 20:11:29 -07002110 pos++;
2111 pos += snprintf(hdr + pos, hdrlen - pos,
2112 "DEVICE=+%s:%s", subsys, dev_name(dev));
Kay Sieversc4e00da2012-05-03 02:29:59 +02002113 }
Jim Cromieaf7f2152012-07-19 13:46:21 -06002114
Joe Perches798efc62012-09-12 20:11:29 -07002115 return pos;
Joe Perches99bcf212010-06-27 01:02:34 +00002116}
Joe Perches798efc62012-09-12 20:11:29 -07002117EXPORT_SYMBOL(create_syslog_header);
2118
Joe Perches05e4e5b2012-09-12 20:13:37 -07002119int dev_vprintk_emit(int level, const struct device *dev,
2120 const char *fmt, va_list args)
2121{
2122 char hdr[128];
2123 size_t hdrlen;
2124
2125 hdrlen = create_syslog_header(dev, hdr, sizeof(hdr));
2126
2127 return vprintk_emit(0, level, hdrlen ? hdr : NULL, hdrlen, fmt, args);
2128}
2129EXPORT_SYMBOL(dev_vprintk_emit);
2130
2131int dev_printk_emit(int level, const struct device *dev, const char *fmt, ...)
2132{
2133 va_list args;
2134 int r;
2135
2136 va_start(args, fmt);
2137
2138 r = dev_vprintk_emit(level, dev, fmt, args);
2139
2140 va_end(args);
2141
2142 return r;
2143}
2144EXPORT_SYMBOL(dev_printk_emit);
2145
Joe Perches798efc62012-09-12 20:11:29 -07002146static int __dev_printk(const char *level, const struct device *dev,
2147 struct va_format *vaf)
2148{
Joe Perches798efc62012-09-12 20:11:29 -07002149 if (!dev)
2150 return printk("%s(NULL device *): %pV", level, vaf);
2151
Joe Perches666f3552012-09-12 20:14:11 -07002152 return dev_printk_emit(level[1] - '0', dev,
2153 "%s %s: %pV",
2154 dev_driver_string(dev), dev_name(dev), vaf);
Joe Perches798efc62012-09-12 20:11:29 -07002155}
Joe Perches99bcf212010-06-27 01:02:34 +00002156
2157int dev_printk(const char *level, const struct device *dev,
2158 const char *fmt, ...)
2159{
2160 struct va_format vaf;
2161 va_list args;
2162 int r;
2163
2164 va_start(args, fmt);
2165
2166 vaf.fmt = fmt;
2167 vaf.va = &args;
2168
2169 r = __dev_printk(level, dev, &vaf);
Joe Perches798efc62012-09-12 20:11:29 -07002170
Joe Perches99bcf212010-06-27 01:02:34 +00002171 va_end(args);
2172
2173 return r;
2174}
2175EXPORT_SYMBOL(dev_printk);
2176
2177#define define_dev_printk_level(func, kern_level) \
2178int func(const struct device *dev, const char *fmt, ...) \
2179{ \
2180 struct va_format vaf; \
2181 va_list args; \
2182 int r; \
2183 \
2184 va_start(args, fmt); \
2185 \
2186 vaf.fmt = fmt; \
2187 vaf.va = &args; \
2188 \
2189 r = __dev_printk(kern_level, dev, &vaf); \
Joe Perches798efc62012-09-12 20:11:29 -07002190 \
Joe Perches99bcf212010-06-27 01:02:34 +00002191 va_end(args); \
2192 \
2193 return r; \
2194} \
2195EXPORT_SYMBOL(func);
2196
2197define_dev_printk_level(dev_emerg, KERN_EMERG);
2198define_dev_printk_level(dev_alert, KERN_ALERT);
2199define_dev_printk_level(dev_crit, KERN_CRIT);
2200define_dev_printk_level(dev_err, KERN_ERR);
2201define_dev_printk_level(dev_warn, KERN_WARNING);
2202define_dev_printk_level(dev_notice, KERN_NOTICE);
2203define_dev_printk_level(_dev_info, KERN_INFO);
2204
2205#endif