blob: d743dcee1e77ddcdc1cab0808f48ff262359e54c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * drivers/base/core.c - core driver model code (device registration, etc)
3 *
4 * Copyright (c) 2002-3 Patrick Mochel
5 * Copyright (c) 2002-3 Open Source Development Labs
Greg Kroah-Hartman64bb5d22006-06-28 16:19:58 -07006 * Copyright (c) 2006 Greg Kroah-Hartman <gregkh@suse.de>
7 * Copyright (c) 2006 Novell, Inc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 *
9 * This file is released under the GPLv2
10 *
11 */
12
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/device.h>
14#include <linux/err.h>
15#include <linux/init.h>
16#include <linux/module.h>
17#include <linux/slab.h>
18#include <linux/string.h>
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -070019#include <linux/kdev_t.h>
Benjamin Herrenschmidt116af372006-10-25 13:44:59 +100020#include <linux/notifier.h>
Grant Likely07d57a32012-02-01 11:22:22 -070021#include <linux/of.h>
22#include <linux/of_device.h>
Kay Sieversda231fd2007-11-21 17:29:15 +010023#include <linux/genhd.h>
Andrew Morton815d2d52008-03-04 15:09:07 -080024#include <linux/kallsyms.h>
Dave Youngf75b1c62008-05-28 09:28:39 -070025#include <linux/mutex.h>
Shaohua Li401097e2009-05-12 13:37:57 -070026#include <linux/async.h>
Peter Chenaf8db152011-11-15 21:52:29 +010027#include <linux/pm_runtime.h>
Kay Sieversc4e00da2012-05-03 02:29:59 +020028#include <linux/netdevice.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
30#include "base.h"
31#include "power/power.h"
32
Andi Kleene52eec12010-09-08 16:54:17 +020033#ifdef CONFIG_SYSFS_DEPRECATED
34#ifdef CONFIG_SYSFS_DEPRECATED_V2
35long sysfs_deprecated = 1;
36#else
37long sysfs_deprecated = 0;
38#endif
Hanjun Guo3454bf92013-08-17 20:42:24 +080039static int __init sysfs_deprecated_setup(char *arg)
Andi Kleene52eec12010-09-08 16:54:17 +020040{
Jingoo Han34da5e62013-07-26 13:10:22 +090041 return kstrtol(arg, 10, &sysfs_deprecated);
Andi Kleene52eec12010-09-08 16:54:17 +020042}
43early_param("sysfs.deprecated", sysfs_deprecated_setup);
44#endif
45
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -080046int (*platform_notify)(struct device *dev) = NULL;
47int (*platform_notify_remove)(struct device *dev) = NULL;
Dan Williamse105b8b2008-04-21 10:51:07 -070048static struct kobject *dev_kobj;
49struct kobject *sysfs_dev_char_kobj;
50struct kobject *sysfs_dev_block_kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Greg Kroah-Hartman4e886c22008-01-27 12:12:43 -080052#ifdef CONFIG_BLOCK
53static inline int device_is_not_partition(struct device *dev)
54{
55 return !(dev->type == &part_type);
56}
57#else
58static inline int device_is_not_partition(struct device *dev)
59{
60 return 1;
61}
62#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Alan Stern3e956372006-06-16 17:10:48 -040064/**
65 * dev_driver_string - Return a device's driver name, if at all possible
66 * @dev: struct device to get the name of
67 *
68 * Will return the device's driver's name if it is bound to a device. If
yan9169c012012-04-20 21:08:45 +080069 * the device is not bound to a driver, it will return the name of the bus
Alan Stern3e956372006-06-16 17:10:48 -040070 * it is attached to. If it is not attached to a bus either, an empty
71 * string will be returned.
72 */
Jean Delvarebf9ca692008-07-30 12:29:21 -070073const char *dev_driver_string(const struct device *dev)
Alan Stern3e956372006-06-16 17:10:48 -040074{
Alan Stern35899722009-12-04 11:06:57 -050075 struct device_driver *drv;
76
77 /* dev->driver can change to NULL underneath us because of unbinding,
78 * so be careful about accessing it. dev->bus and dev->class should
79 * never change once they are set, so they don't need special care.
80 */
81 drv = ACCESS_ONCE(dev->driver);
82 return drv ? drv->name :
Jean Delvarea456b702007-03-09 16:33:10 +010083 (dev->bus ? dev->bus->name :
84 (dev->class ? dev->class->name : ""));
Alan Stern3e956372006-06-16 17:10:48 -040085}
Matthew Wilcox310a9222006-09-23 23:35:04 -060086EXPORT_SYMBOL(dev_driver_string);
Alan Stern3e956372006-06-16 17:10:48 -040087
Linus Torvalds1da177e2005-04-16 15:20:36 -070088#define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr)
89
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -080090static ssize_t dev_attr_show(struct kobject *kobj, struct attribute *attr,
91 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070092{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -080093 struct device_attribute *dev_attr = to_dev_attr(attr);
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +020094 struct device *dev = kobj_to_dev(kobj);
Dmitry Torokhov4a0c20b2005-04-29 01:23:47 -050095 ssize_t ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
97 if (dev_attr->show)
Yani Ioannou54b6f352005-05-17 06:39:34 -040098 ret = dev_attr->show(dev, dev_attr, buf);
Andrew Morton815d2d52008-03-04 15:09:07 -080099 if (ret >= (ssize_t)PAGE_SIZE) {
Greg Kroah-Hartman53a9c872013-01-17 13:10:23 -0800100 print_symbol("dev_attr_show: %s returned bad count\n",
101 (unsigned long)dev_attr->show);
Andrew Morton815d2d52008-03-04 15:09:07 -0800102 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 return ret;
104}
105
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800106static ssize_t dev_attr_store(struct kobject *kobj, struct attribute *attr,
107 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800109 struct device_attribute *dev_attr = to_dev_attr(attr);
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200110 struct device *dev = kobj_to_dev(kobj);
Dmitry Torokhov4a0c20b2005-04-29 01:23:47 -0500111 ssize_t ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
113 if (dev_attr->store)
Yani Ioannou54b6f352005-05-17 06:39:34 -0400114 ret = dev_attr->store(dev, dev_attr, buf, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 return ret;
116}
117
Emese Revfy52cf25d2010-01-19 02:58:23 +0100118static const struct sysfs_ops dev_sysfs_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 .show = dev_attr_show,
120 .store = dev_attr_store,
121};
122
Kay Sieversca22e562011-12-14 14:29:38 -0800123#define to_ext_attr(x) container_of(x, struct dev_ext_attribute, attr)
124
125ssize_t device_store_ulong(struct device *dev,
126 struct device_attribute *attr,
127 const char *buf, size_t size)
128{
129 struct dev_ext_attribute *ea = to_ext_attr(attr);
130 char *end;
131 unsigned long new = simple_strtoul(buf, &end, 0);
132 if (end == buf)
133 return -EINVAL;
134 *(unsigned long *)(ea->var) = new;
135 /* Always return full write size even if we didn't consume all */
136 return size;
137}
138EXPORT_SYMBOL_GPL(device_store_ulong);
139
140ssize_t device_show_ulong(struct device *dev,
141 struct device_attribute *attr,
142 char *buf)
143{
144 struct dev_ext_attribute *ea = to_ext_attr(attr);
145 return snprintf(buf, PAGE_SIZE, "%lx\n", *(unsigned long *)(ea->var));
146}
147EXPORT_SYMBOL_GPL(device_show_ulong);
148
149ssize_t device_store_int(struct device *dev,
150 struct device_attribute *attr,
151 const char *buf, size_t size)
152{
153 struct dev_ext_attribute *ea = to_ext_attr(attr);
154 char *end;
155 long new = simple_strtol(buf, &end, 0);
156 if (end == buf || new > INT_MAX || new < INT_MIN)
157 return -EINVAL;
158 *(int *)(ea->var) = new;
159 /* Always return full write size even if we didn't consume all */
160 return size;
161}
162EXPORT_SYMBOL_GPL(device_store_int);
163
164ssize_t device_show_int(struct device *dev,
165 struct device_attribute *attr,
166 char *buf)
167{
168 struct dev_ext_attribute *ea = to_ext_attr(attr);
169
170 return snprintf(buf, PAGE_SIZE, "%d\n", *(int *)(ea->var));
171}
172EXPORT_SYMBOL_GPL(device_show_int);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
Borislav Petkov91872392012-10-09 19:52:05 +0200174ssize_t device_store_bool(struct device *dev, struct device_attribute *attr,
175 const char *buf, size_t size)
176{
177 struct dev_ext_attribute *ea = to_ext_attr(attr);
178
179 if (strtobool(buf, ea->var) < 0)
180 return -EINVAL;
181
182 return size;
183}
184EXPORT_SYMBOL_GPL(device_store_bool);
185
186ssize_t device_show_bool(struct device *dev, struct device_attribute *attr,
187 char *buf)
188{
189 struct dev_ext_attribute *ea = to_ext_attr(attr);
190
191 return snprintf(buf, PAGE_SIZE, "%d\n", *(bool *)(ea->var));
192}
193EXPORT_SYMBOL_GPL(device_show_bool);
194
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195/**
Robert P. J. Dayf8878dc2013-06-01 20:17:34 -0400196 * device_release - free device structure.
197 * @kobj: device's kobject.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 *
Robert P. J. Dayf8878dc2013-06-01 20:17:34 -0400199 * This is called once the reference count for the object
200 * reaches 0. We forward the call to the device's release
201 * method, which should handle actually freeing the structure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800203static void device_release(struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200205 struct device *dev = kobj_to_dev(kobj);
Greg Kroah-Hartmanfb069a52008-12-16 12:23:36 -0800206 struct device_private *p = dev->p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
Ming Leia525a3d2012-07-25 01:42:29 +0800208 /*
209 * Some platform devices are driven without driver attached
210 * and managed resources may have been acquired. Make sure
211 * all resources are released.
212 *
213 * Drivers still can add resources into device after device
214 * is deleted but alive, so release devres here to avoid
215 * possible memory leak.
216 */
217 devres_release_all(dev);
218
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 if (dev->release)
220 dev->release(dev);
Kay Sieversf9f852d2006-10-07 21:54:55 +0200221 else if (dev->type && dev->type->release)
222 dev->type->release(dev);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700223 else if (dev->class && dev->class->dev_release)
224 dev->class->dev_release(dev);
Arjan van de Venf810a5c2008-07-25 19:45:39 -0700225 else
226 WARN(1, KERN_ERR "Device '%s' does not have a release() "
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800227 "function, it is broken and must be fixed.\n",
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100228 dev_name(dev));
Greg Kroah-Hartmanfb069a52008-12-16 12:23:36 -0800229 kfree(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230}
231
Eric W. Biedermanbc451f22010-03-30 11:31:25 -0700232static const void *device_namespace(struct kobject *kobj)
233{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200234 struct device *dev = kobj_to_dev(kobj);
Eric W. Biedermanbc451f22010-03-30 11:31:25 -0700235 const void *ns = NULL;
236
237 if (dev->class && dev->class->ns_type)
238 ns = dev->class->namespace(dev);
239
240 return ns;
241}
242
Greg Kroah-Hartman8f4afc42007-10-11 10:47:49 -0600243static struct kobj_type device_ktype = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 .release = device_release,
245 .sysfs_ops = &dev_sysfs_ops,
Eric W. Biedermanbc451f22010-03-30 11:31:25 -0700246 .namespace = device_namespace,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247};
248
249
Kay Sievers312c0042005-11-16 09:00:00 +0100250static int dev_uevent_filter(struct kset *kset, struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251{
252 struct kobj_type *ktype = get_ktype(kobj);
253
Greg Kroah-Hartman8f4afc42007-10-11 10:47:49 -0600254 if (ktype == &device_ktype) {
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200255 struct device *dev = kobj_to_dev(kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 if (dev->bus)
257 return 1;
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700258 if (dev->class)
259 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 }
261 return 0;
262}
263
Kay Sievers312c0042005-11-16 09:00:00 +0100264static const char *dev_uevent_name(struct kset *kset, struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200266 struct device *dev = kobj_to_dev(kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700268 if (dev->bus)
269 return dev->bus->name;
270 if (dev->class)
271 return dev->class->name;
272 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273}
274
Kay Sievers7eff2e72007-08-14 15:15:12 +0200275static int dev_uevent(struct kset *kset, struct kobject *kobj,
276 struct kobj_uevent_env *env)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200278 struct device *dev = kobj_to_dev(kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 int retval = 0;
280
Kay Sievers6fcf53a2009-04-30 15:23:42 +0200281 /* add device node properties if present */
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700282 if (MAJOR(dev->devt)) {
Kay Sievers6fcf53a2009-04-30 15:23:42 +0200283 const char *tmp;
284 const char *name;
Al Viro2c9ede52011-07-23 20:24:48 -0400285 umode_t mode = 0;
Greg Kroah-Hartman4e4098a2013-04-11 11:43:29 -0700286 kuid_t uid = GLOBAL_ROOT_UID;
287 kgid_t gid = GLOBAL_ROOT_GID;
Kay Sievers6fcf53a2009-04-30 15:23:42 +0200288
Kay Sievers7eff2e72007-08-14 15:15:12 +0200289 add_uevent_var(env, "MAJOR=%u", MAJOR(dev->devt));
290 add_uevent_var(env, "MINOR=%u", MINOR(dev->devt));
Kay Sievers3c2670e2013-04-06 09:56:00 -0700291 name = device_get_devnode(dev, &mode, &uid, &gid, &tmp);
Kay Sievers6fcf53a2009-04-30 15:23:42 +0200292 if (name) {
293 add_uevent_var(env, "DEVNAME=%s", name);
Kay Sieverse454cea2009-09-18 23:01:12 +0200294 if (mode)
295 add_uevent_var(env, "DEVMODE=%#o", mode & 0777);
Greg Kroah-Hartman4e4098a2013-04-11 11:43:29 -0700296 if (!uid_eq(uid, GLOBAL_ROOT_UID))
297 add_uevent_var(env, "DEVUID=%u", from_kuid(&init_user_ns, uid));
298 if (!gid_eq(gid, GLOBAL_ROOT_GID))
299 add_uevent_var(env, "DEVGID=%u", from_kgid(&init_user_ns, gid));
Kay Sievers3c2670e2013-04-06 09:56:00 -0700300 kfree(tmp);
Kay Sievers6fcf53a2009-04-30 15:23:42 +0200301 }
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700302 }
303
Kay Sievers414264f2007-03-12 21:08:57 +0100304 if (dev->type && dev->type->name)
Kay Sievers7eff2e72007-08-14 15:15:12 +0200305 add_uevent_var(env, "DEVTYPE=%s", dev->type->name);
Kay Sievers414264f2007-03-12 21:08:57 +0100306
Kay Sievers239378f2006-10-07 21:54:55 +0200307 if (dev->driver)
Kay Sievers7eff2e72007-08-14 15:15:12 +0200308 add_uevent_var(env, "DRIVER=%s", dev->driver->name);
Kay Sievers239378f2006-10-07 21:54:55 +0200309
Grant Likely07d57a32012-02-01 11:22:22 -0700310 /* Add common DT information about the device */
311 of_device_uevent(dev, env);
312
Kay Sievers7eff2e72007-08-14 15:15:12 +0200313 /* have the bus specific function add its stuff */
Kay Sievers312c0042005-11-16 09:00:00 +0100314 if (dev->bus && dev->bus->uevent) {
Kay Sievers7eff2e72007-08-14 15:15:12 +0200315 retval = dev->bus->uevent(dev, env);
Kay Sieversf9f852d2006-10-07 21:54:55 +0200316 if (retval)
Greg Kroah-Hartman7dc72b22007-11-28 23:49:41 -0800317 pr_debug("device: '%s': %s: bus uevent() returned %d\n",
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100318 dev_name(dev), __func__, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 }
320
Kay Sievers7eff2e72007-08-14 15:15:12 +0200321 /* have the class specific function add its stuff */
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700322 if (dev->class && dev->class->dev_uevent) {
Kay Sievers7eff2e72007-08-14 15:15:12 +0200323 retval = dev->class->dev_uevent(dev, env);
Kay Sieversf9f852d2006-10-07 21:54:55 +0200324 if (retval)
Greg Kroah-Hartman7dc72b22007-11-28 23:49:41 -0800325 pr_debug("device: '%s': %s: class uevent() "
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100326 "returned %d\n", dev_name(dev),
Harvey Harrison2b3a3022008-03-04 16:41:05 -0800327 __func__, retval);
Kay Sieversf9f852d2006-10-07 21:54:55 +0200328 }
329
Stefan Weileef35c22010-08-06 21:11:15 +0200330 /* have the device type specific function add its stuff */
Kay Sieversf9f852d2006-10-07 21:54:55 +0200331 if (dev->type && dev->type->uevent) {
Kay Sievers7eff2e72007-08-14 15:15:12 +0200332 retval = dev->type->uevent(dev, env);
Kay Sieversf9f852d2006-10-07 21:54:55 +0200333 if (retval)
Greg Kroah-Hartman7dc72b22007-11-28 23:49:41 -0800334 pr_debug("device: '%s': %s: dev_type uevent() "
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100335 "returned %d\n", dev_name(dev),
Harvey Harrison2b3a3022008-03-04 16:41:05 -0800336 __func__, retval);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700337 }
338
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 return retval;
340}
341
Emese Revfy9cd43612009-12-31 14:52:51 +0100342static const struct kset_uevent_ops device_uevent_ops = {
Kay Sievers312c0042005-11-16 09:00:00 +0100343 .filter = dev_uevent_filter,
344 .name = dev_uevent_name,
345 .uevent = dev_uevent,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346};
347
Kay Sievers16574dc2007-04-06 01:40:38 +0200348static ssize_t show_uevent(struct device *dev, struct device_attribute *attr,
349 char *buf)
350{
351 struct kobject *top_kobj;
352 struct kset *kset;
Kay Sievers7eff2e72007-08-14 15:15:12 +0200353 struct kobj_uevent_env *env = NULL;
Kay Sievers16574dc2007-04-06 01:40:38 +0200354 int i;
355 size_t count = 0;
356 int retval;
357
358 /* search the kset, the device belongs to */
359 top_kobj = &dev->kobj;
Kay Sievers5c5daf62007-08-12 20:43:55 +0200360 while (!top_kobj->kset && top_kobj->parent)
361 top_kobj = top_kobj->parent;
Kay Sievers16574dc2007-04-06 01:40:38 +0200362 if (!top_kobj->kset)
363 goto out;
Kay Sievers5c5daf62007-08-12 20:43:55 +0200364
Kay Sievers16574dc2007-04-06 01:40:38 +0200365 kset = top_kobj->kset;
366 if (!kset->uevent_ops || !kset->uevent_ops->uevent)
367 goto out;
368
369 /* respect filter */
370 if (kset->uevent_ops && kset->uevent_ops->filter)
371 if (!kset->uevent_ops->filter(kset, &dev->kobj))
372 goto out;
373
Kay Sievers7eff2e72007-08-14 15:15:12 +0200374 env = kzalloc(sizeof(struct kobj_uevent_env), GFP_KERNEL);
375 if (!env)
Greg Kroah-Hartmanc7308c82007-05-02 14:14:11 +0200376 return -ENOMEM;
377
Kay Sievers16574dc2007-04-06 01:40:38 +0200378 /* let the kset specific function add its keys */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200379 retval = kset->uevent_ops->uevent(kset, &dev->kobj, env);
Kay Sievers16574dc2007-04-06 01:40:38 +0200380 if (retval)
381 goto out;
382
383 /* copy keys to file */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200384 for (i = 0; i < env->envp_idx; i++)
385 count += sprintf(&buf[count], "%s\n", env->envp[i]);
Kay Sievers16574dc2007-04-06 01:40:38 +0200386out:
Kay Sievers7eff2e72007-08-14 15:15:12 +0200387 kfree(env);
Kay Sievers16574dc2007-04-06 01:40:38 +0200388 return count;
389}
390
Kay Sieversa7fd6702005-10-01 14:49:43 +0200391static ssize_t store_uevent(struct device *dev, struct device_attribute *attr,
392 const char *buf, size_t count)
393{
Kay Sievers60a96a52007-07-08 22:29:26 +0200394 enum kobject_action action;
395
Kay Sievers3f5468c2010-01-14 22:54:37 +0100396 if (kobject_action_type(buf, count, &action) == 0)
Kay Sievers60a96a52007-07-08 22:29:26 +0200397 kobject_uevent(&dev->kobj, action);
Kay Sievers3f5468c2010-01-14 22:54:37 +0100398 else
399 dev_err(dev, "uevent: unknown action-string\n");
Kay Sieversa7fd6702005-10-01 14:49:43 +0200400 return count;
401}
402
Tejun Heoad6a1e12007-06-14 03:45:17 +0900403static struct device_attribute uevent_attr =
404 __ATTR(uevent, S_IRUGO | S_IWUSR, show_uevent, store_uevent);
405
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +0200406static ssize_t show_online(struct device *dev, struct device_attribute *attr,
407 char *buf)
408{
409 bool val;
410
411 lock_device_hotplug();
412 val = !dev->offline;
413 unlock_device_hotplug();
414 return sprintf(buf, "%u\n", val);
415}
416
417static ssize_t store_online(struct device *dev, struct device_attribute *attr,
418 const char *buf, size_t count)
419{
420 bool val;
421 int ret;
422
423 ret = strtobool(buf, &val);
424 if (ret < 0)
425 return ret;
426
427 lock_device_hotplug();
428 ret = val ? device_online(dev) : device_offline(dev);
429 unlock_device_hotplug();
430 return ret < 0 ? ret : count;
431}
432
433static struct device_attribute online_attr =
434 __ATTR(online, S_IRUGO | S_IWUSR, show_online, store_online);
435
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500436static int device_add_attributes(struct device *dev,
437 struct device_attribute *attrs)
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -0700438{
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -0700439 int error = 0;
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500440 int i;
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -0700441
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500442 if (attrs) {
443 for (i = 0; attr_name(attrs[i]); i++) {
444 error = device_create_file(dev, &attrs[i]);
445 if (error)
446 break;
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -0700447 }
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500448 if (error)
449 while (--i >= 0)
450 device_remove_file(dev, &attrs[i]);
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -0700451 }
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -0700452 return error;
453}
454
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500455static void device_remove_attributes(struct device *dev,
456 struct device_attribute *attrs)
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -0700457{
458 int i;
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500459
460 if (attrs)
461 for (i = 0; attr_name(attrs[i]); i++)
462 device_remove_file(dev, &attrs[i]);
463}
464
Stefan Achatzc97415a2010-11-26 19:57:29 +0000465static int device_add_bin_attributes(struct device *dev,
466 struct bin_attribute *attrs)
467{
468 int error = 0;
469 int i;
470
471 if (attrs) {
472 for (i = 0; attr_name(attrs[i]); i++) {
473 error = device_create_bin_file(dev, &attrs[i]);
474 if (error)
475 break;
476 }
477 if (error)
478 while (--i >= 0)
479 device_remove_bin_file(dev, &attrs[i]);
480 }
481 return error;
482}
483
484static void device_remove_bin_attributes(struct device *dev,
485 struct bin_attribute *attrs)
486{
487 int i;
488
489 if (attrs)
490 for (i = 0; attr_name(attrs[i]); i++)
491 device_remove_bin_file(dev, &attrs[i]);
492}
493
Greg Kroah-Hartmanfa6fdb32013-08-08 15:22:55 -0700494int device_add_groups(struct device *dev, const struct attribute_group **groups)
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500495{
Greg Kroah-Hartman3e9b2ba2013-08-21 13:47:50 -0700496 return sysfs_create_groups(&dev->kobj, groups);
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500497}
498
Greg Kroah-Hartmanfa6fdb32013-08-08 15:22:55 -0700499void device_remove_groups(struct device *dev,
500 const struct attribute_group **groups)
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500501{
Greg Kroah-Hartman3e9b2ba2013-08-21 13:47:50 -0700502 sysfs_remove_groups(&dev->kobj, groups);
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -0700503}
504
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700505static int device_add_attrs(struct device *dev)
506{
507 struct class *class = dev->class;
Stephen Hemmingeraed65af2011-03-28 09:12:52 -0700508 const struct device_type *type = dev->type;
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500509 int error;
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700510
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500511 if (class) {
Greg Kroah-Hartmand05a6f92013-07-14 16:05:58 -0700512 error = device_add_groups(dev, class->dev_groups);
Kay Sieversf9f852d2006-10-07 21:54:55 +0200513 if (error)
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500514 return error;
Greg Kroah-Hartmand05a6f92013-07-14 16:05:58 -0700515 error = device_add_attributes(dev, class->dev_attrs);
516 if (error)
517 goto err_remove_class_groups;
Stefan Achatzc97415a2010-11-26 19:57:29 +0000518 error = device_add_bin_attributes(dev, class->dev_bin_attrs);
519 if (error)
520 goto err_remove_class_attrs;
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700521 }
Kay Sieversf9f852d2006-10-07 21:54:55 +0200522
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500523 if (type) {
524 error = device_add_groups(dev, type->groups);
Kay Sieversf9f852d2006-10-07 21:54:55 +0200525 if (error)
Stefan Achatzc97415a2010-11-26 19:57:29 +0000526 goto err_remove_class_bin_attrs;
Kay Sieversf9f852d2006-10-07 21:54:55 +0200527 }
528
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500529 error = device_add_groups(dev, dev->groups);
530 if (error)
531 goto err_remove_type_groups;
532
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +0200533 if (device_supports_offline(dev) && !dev->offline_disabled) {
534 error = device_create_file(dev, &online_attr);
535 if (error)
536 goto err_remove_type_groups;
537 }
538
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500539 return 0;
540
541 err_remove_type_groups:
542 if (type)
543 device_remove_groups(dev, type->groups);
Stefan Achatzc97415a2010-11-26 19:57:29 +0000544 err_remove_class_bin_attrs:
545 if (class)
546 device_remove_bin_attributes(dev, class->dev_bin_attrs);
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500547 err_remove_class_attrs:
548 if (class)
549 device_remove_attributes(dev, class->dev_attrs);
Greg Kroah-Hartmand05a6f92013-07-14 16:05:58 -0700550 err_remove_class_groups:
551 if (class)
552 device_remove_groups(dev, class->dev_groups);
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500553
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700554 return error;
555}
556
557static void device_remove_attrs(struct device *dev)
558{
559 struct class *class = dev->class;
Stephen Hemmingeraed65af2011-03-28 09:12:52 -0700560 const struct device_type *type = dev->type;
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700561
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +0200562 device_remove_file(dev, &online_attr);
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500563 device_remove_groups(dev, dev->groups);
Kay Sieversf9f852d2006-10-07 21:54:55 +0200564
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500565 if (type)
566 device_remove_groups(dev, type->groups);
567
Stefan Achatzc97415a2010-11-26 19:57:29 +0000568 if (class) {
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500569 device_remove_attributes(dev, class->dev_attrs);
Stefan Achatzc97415a2010-11-26 19:57:29 +0000570 device_remove_bin_attributes(dev, class->dev_bin_attrs);
Greg Kroah-Hartmand05a6f92013-07-14 16:05:58 -0700571 device_remove_groups(dev, class->dev_groups);
Stefan Achatzc97415a2010-11-26 19:57:29 +0000572 }
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700573}
574
575
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700576static ssize_t show_dev(struct device *dev, struct device_attribute *attr,
577 char *buf)
578{
579 return print_dev_t(buf, dev->devt);
580}
581
Tejun Heoad6a1e12007-06-14 03:45:17 +0900582static struct device_attribute devt_attr =
583 __ATTR(dev, S_IRUGO, show_dev, NULL);
584
Kay Sieversca22e562011-12-14 14:29:38 -0800585/* /sys/devices/ */
Greg Kroah-Hartman881c6cfd2007-11-01 09:29:06 -0600586struct kset *devices_kset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800589 * device_create_file - create sysfs attribute file for device.
590 * @dev: device.
591 * @attr: device attribute descriptor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 */
Phil Carmody26579ab2009-12-18 15:34:19 +0200593int device_create_file(struct device *dev,
594 const struct device_attribute *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595{
596 int error = 0;
Felipe Balbi8f46baa2013-02-20 10:31:42 +0200597
598 if (dev) {
599 WARN(((attr->attr.mode & S_IWUGO) && !attr->store),
dyoung@redhat.com97521972013-05-16 14:31:30 +0800600 "Attribute %s: write permission without 'store'\n",
601 attr->attr.name);
Felipe Balbi8f46baa2013-02-20 10:31:42 +0200602 WARN(((attr->attr.mode & S_IRUGO) && !attr->show),
dyoung@redhat.com97521972013-05-16 14:31:30 +0800603 "Attribute %s: read permission without 'show'\n",
604 attr->attr.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 error = sysfs_create_file(&dev->kobj, &attr->attr);
Felipe Balbi8f46baa2013-02-20 10:31:42 +0200606 }
607
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 return error;
609}
David Graham White86df2682013-07-21 20:41:14 -0400610EXPORT_SYMBOL_GPL(device_create_file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611
612/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800613 * device_remove_file - remove sysfs attribute file.
614 * @dev: device.
615 * @attr: device attribute descriptor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 */
Phil Carmody26579ab2009-12-18 15:34:19 +0200617void device_remove_file(struct device *dev,
618 const struct device_attribute *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619{
Cornelia Huck0c98b192008-01-31 10:39:38 +0100620 if (dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 sysfs_remove_file(&dev->kobj, &attr->attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622}
David Graham White86df2682013-07-21 20:41:14 -0400623EXPORT_SYMBOL_GPL(device_remove_file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624
Greg Kroah-Hartman2589f1882006-09-19 09:39:19 -0700625/**
626 * device_create_bin_file - create sysfs binary attribute file for device.
627 * @dev: device.
628 * @attr: device binary attribute descriptor.
629 */
Phil Carmody66ecb922009-12-18 15:34:20 +0200630int device_create_bin_file(struct device *dev,
631 const struct bin_attribute *attr)
Greg Kroah-Hartman2589f1882006-09-19 09:39:19 -0700632{
633 int error = -EINVAL;
634 if (dev)
635 error = sysfs_create_bin_file(&dev->kobj, attr);
636 return error;
637}
638EXPORT_SYMBOL_GPL(device_create_bin_file);
639
640/**
641 * device_remove_bin_file - remove sysfs binary attribute file
642 * @dev: device.
643 * @attr: device binary attribute descriptor.
644 */
Phil Carmody66ecb922009-12-18 15:34:20 +0200645void device_remove_bin_file(struct device *dev,
646 const struct bin_attribute *attr)
Greg Kroah-Hartman2589f1882006-09-19 09:39:19 -0700647{
648 if (dev)
649 sysfs_remove_bin_file(&dev->kobj, attr);
650}
651EXPORT_SYMBOL_GPL(device_remove_bin_file);
652
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400653/**
Alan Stern523ded72007-04-26 00:12:04 -0700654 * device_schedule_callback_owner - helper to schedule a callback for a device
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400655 * @dev: device.
656 * @func: callback function to invoke later.
Alan Stern523ded72007-04-26 00:12:04 -0700657 * @owner: module owning the callback routine
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400658 *
659 * Attribute methods must not unregister themselves or their parent device
660 * (which would amount to the same thing). Attempts to do so will deadlock,
661 * since unregistration is mutually exclusive with driver callbacks.
662 *
663 * Instead methods can call this routine, which will attempt to allocate
664 * and schedule a workqueue request to call back @func with @dev as its
665 * argument in the workqueue's process context. @dev will be pinned until
666 * @func returns.
667 *
Alan Stern523ded72007-04-26 00:12:04 -0700668 * This routine is usually called via the inline device_schedule_callback(),
669 * which automatically sets @owner to THIS_MODULE.
670 *
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400671 * Returns 0 if the request was submitted, -ENOMEM if storage could not
Alan Stern523ded72007-04-26 00:12:04 -0700672 * be allocated, -ENODEV if a reference to @owner isn't available.
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400673 *
674 * NOTE: This routine won't work if CONFIG_SYSFS isn't set! It uses an
675 * underlying sysfs routine (since it is intended for use by attribute
676 * methods), and if sysfs isn't available you'll get nothing but -ENOSYS.
677 */
Alan Stern523ded72007-04-26 00:12:04 -0700678int device_schedule_callback_owner(struct device *dev,
679 void (*func)(struct device *), struct module *owner)
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400680{
681 return sysfs_schedule_callback(&dev->kobj,
Alan Stern523ded72007-04-26 00:12:04 -0700682 (void (*)(void *)) func, dev, owner);
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400683}
Alan Stern523ded72007-04-26 00:12:04 -0700684EXPORT_SYMBOL_GPL(device_schedule_callback_owner);
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400685
James Bottomley34bb61f2005-09-06 16:56:51 -0700686static void klist_children_get(struct klist_node *n)
687{
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -0800688 struct device_private *p = to_device_private_parent(n);
689 struct device *dev = p->device;
James Bottomley34bb61f2005-09-06 16:56:51 -0700690
691 get_device(dev);
692}
693
694static void klist_children_put(struct klist_node *n)
695{
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -0800696 struct device_private *p = to_device_private_parent(n);
697 struct device *dev = p->device;
James Bottomley34bb61f2005-09-06 16:56:51 -0700698
699 put_device(dev);
700}
701
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800703 * device_initialize - init device structure.
704 * @dev: device.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 *
Cornelia Huck57394112008-09-03 18:26:40 +0200706 * This prepares the device for use by other layers by initializing
707 * its fields.
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800708 * It is the first half of device_register(), if called by
Cornelia Huck57394112008-09-03 18:26:40 +0200709 * that function, though it can also be called separately, so one
710 * may use @dev's fields. In particular, get_device()/put_device()
711 * may be used for reference counting of @dev after calling this
712 * function.
713 *
Alan Sternb10d5ef2012-01-17 11:39:00 -0500714 * All fields in @dev must be initialized by the caller to 0, except
715 * for those explicitly set to some other value. The simplest
716 * approach is to use kzalloc() to allocate the structure containing
717 * @dev.
718 *
Cornelia Huck57394112008-09-03 18:26:40 +0200719 * NOTE: Use put_device() to give up your reference instead of freeing
720 * @dev directly once you have called this function.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722void device_initialize(struct device *dev)
723{
Greg Kroah-Hartman881c6cfd2007-11-01 09:29:06 -0600724 dev->kobj.kset = devices_kset;
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -0700725 kobject_init(&dev->kobj, &device_ktype);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 INIT_LIST_HEAD(&dev->dma_pools);
Thomas Gleixner31427882010-01-29 20:39:02 +0000727 mutex_init(&dev->mutex);
Peter Zijlstra1704f472010-03-19 01:37:42 +0100728 lockdep_set_novalidate_class(&dev->mutex);
Tejun Heo9ac78492007-01-20 16:00:26 +0900729 spin_lock_init(&dev->devres_lock);
730 INIT_LIST_HEAD(&dev->devres_head);
Alan Stern3b98aea2008-08-07 13:06:12 -0400731 device_pm_init(dev);
Christoph Hellwig87348132006-12-06 20:32:33 -0800732 set_dev_node(dev, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733}
David Graham White86df2682013-07-21 20:41:14 -0400734EXPORT_SYMBOL_GPL(device_initialize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735
Tejun Heod73ce002013-03-12 11:30:05 -0700736struct kobject *virtual_device_parent(struct device *dev)
Greg Kroah-Hartmanf0ee61a2006-10-23 10:40:54 -0700737{
Kay Sievers86406242007-03-14 03:25:56 +0100738 static struct kobject *virtual_dir = NULL;
Greg Kroah-Hartmanf0ee61a2006-10-23 10:40:54 -0700739
Kay Sievers86406242007-03-14 03:25:56 +0100740 if (!virtual_dir)
Greg Kroah-Hartman4ff6abf2007-11-05 22:24:43 -0800741 virtual_dir = kobject_create_and_add("virtual",
Greg Kroah-Hartman881c6cfd2007-11-01 09:29:06 -0600742 &devices_kset->kobj);
Greg Kroah-Hartmanf0ee61a2006-10-23 10:40:54 -0700743
Kay Sievers86406242007-03-14 03:25:56 +0100744 return virtual_dir;
Greg Kroah-Hartmanf0ee61a2006-10-23 10:40:54 -0700745}
746
Eric W. Biedermanbc451f22010-03-30 11:31:25 -0700747struct class_dir {
748 struct kobject kobj;
749 struct class *class;
750};
751
752#define to_class_dir(obj) container_of(obj, struct class_dir, kobj)
753
754static void class_dir_release(struct kobject *kobj)
755{
756 struct class_dir *dir = to_class_dir(kobj);
757 kfree(dir);
758}
759
760static const
761struct kobj_ns_type_operations *class_dir_child_ns_type(struct kobject *kobj)
762{
763 struct class_dir *dir = to_class_dir(kobj);
764 return dir->class->ns_type;
765}
766
767static struct kobj_type class_dir_ktype = {
768 .release = class_dir_release,
769 .sysfs_ops = &kobj_sysfs_ops,
770 .child_ns_type = class_dir_child_ns_type
771};
772
773static struct kobject *
774class_dir_create_and_add(struct class *class, struct kobject *parent_kobj)
775{
776 struct class_dir *dir;
777 int retval;
778
779 dir = kzalloc(sizeof(*dir), GFP_KERNEL);
780 if (!dir)
781 return NULL;
782
783 dir->class = class;
784 kobject_init(&dir->kobj, &class_dir_ktype);
785
Kay Sievers6b6e39a2010-11-15 23:13:18 +0100786 dir->kobj.kset = &class->p->glue_dirs;
Eric W. Biedermanbc451f22010-03-30 11:31:25 -0700787
788 retval = kobject_add(&dir->kobj, parent_kobj, "%s", class->name);
789 if (retval < 0) {
790 kobject_put(&dir->kobj);
791 return NULL;
792 }
793 return &dir->kobj;
794}
795
796
Kay Sieversda231fd2007-11-21 17:29:15 +0100797static struct kobject *get_device_parent(struct device *dev,
798 struct device *parent)
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +0100799{
Kay Sievers86406242007-03-14 03:25:56 +0100800 if (dev->class) {
Tejun Heo77d3d7c2010-02-05 17:57:02 +0900801 static DEFINE_MUTEX(gdp_mutex);
Kay Sievers86406242007-03-14 03:25:56 +0100802 struct kobject *kobj = NULL;
803 struct kobject *parent_kobj;
804 struct kobject *k;
805
Randy Dunlapead454f2010-09-24 14:36:49 -0700806#ifdef CONFIG_BLOCK
Kay Sievers39aba962010-09-04 22:33:14 -0700807 /* block disks show up in /sys/block */
Andi Kleene52eec12010-09-08 16:54:17 +0200808 if (sysfs_deprecated && dev->class == &block_class) {
Kay Sievers39aba962010-09-04 22:33:14 -0700809 if (parent && parent->class == &block_class)
810 return &parent->kobj;
Kay Sievers6b6e39a2010-11-15 23:13:18 +0100811 return &block_class.p->subsys.kobj;
Kay Sievers39aba962010-09-04 22:33:14 -0700812 }
Randy Dunlapead454f2010-09-24 14:36:49 -0700813#endif
Andi Kleene52eec12010-09-08 16:54:17 +0200814
Kay Sievers86406242007-03-14 03:25:56 +0100815 /*
816 * If we have no parent, we live in "virtual".
Kay Sievers0f4dafc2007-12-19 01:40:42 +0100817 * Class-devices with a non class-device as parent, live
818 * in a "glue" directory to prevent namespace collisions.
Kay Sievers86406242007-03-14 03:25:56 +0100819 */
820 if (parent == NULL)
821 parent_kobj = virtual_device_parent(dev);
Eric W. Biederman24b14422010-07-24 22:43:35 -0700822 else if (parent->class && !dev->class->ns_type)
Kay Sievers86406242007-03-14 03:25:56 +0100823 return &parent->kobj;
824 else
825 parent_kobj = &parent->kobj;
826
Tejun Heo77d3d7c2010-02-05 17:57:02 +0900827 mutex_lock(&gdp_mutex);
828
Kay Sievers86406242007-03-14 03:25:56 +0100829 /* find our class-directory at the parent and reference it */
Kay Sievers6b6e39a2010-11-15 23:13:18 +0100830 spin_lock(&dev->class->p->glue_dirs.list_lock);
831 list_for_each_entry(k, &dev->class->p->glue_dirs.list, entry)
Kay Sievers86406242007-03-14 03:25:56 +0100832 if (k->parent == parent_kobj) {
833 kobj = kobject_get(k);
834 break;
835 }
Kay Sievers6b6e39a2010-11-15 23:13:18 +0100836 spin_unlock(&dev->class->p->glue_dirs.list_lock);
Tejun Heo77d3d7c2010-02-05 17:57:02 +0900837 if (kobj) {
838 mutex_unlock(&gdp_mutex);
Kay Sievers86406242007-03-14 03:25:56 +0100839 return kobj;
Tejun Heo77d3d7c2010-02-05 17:57:02 +0900840 }
Kay Sievers86406242007-03-14 03:25:56 +0100841
842 /* or create a new class-directory at the parent device */
Eric W. Biedermanbc451f22010-03-30 11:31:25 -0700843 k = class_dir_create_and_add(dev->class, parent_kobj);
Kay Sievers0f4dafc2007-12-19 01:40:42 +0100844 /* do not emit an uevent for this simple "glue" directory */
Tejun Heo77d3d7c2010-02-05 17:57:02 +0900845 mutex_unlock(&gdp_mutex);
Greg Kroah-Hartman43968d22007-11-05 22:24:43 -0800846 return k;
Kay Sievers86406242007-03-14 03:25:56 +0100847 }
848
Kay Sieversca22e562011-12-14 14:29:38 -0800849 /* subsystems can specify a default root directory for their devices */
850 if (!parent && dev->bus && dev->bus->dev_root)
851 return &dev->bus->dev_root->kobj;
852
Kay Sievers86406242007-03-14 03:25:56 +0100853 if (parent)
Cornelia Huckc744aeae2007-01-08 20:16:44 +0100854 return &parent->kobj;
855 return NULL;
856}
Kay Sieversda231fd2007-11-21 17:29:15 +0100857
Cornelia Huck63b69712008-01-21 16:09:44 +0100858static void cleanup_glue_dir(struct device *dev, struct kobject *glue_dir)
Kay Sieversda231fd2007-11-21 17:29:15 +0100859{
Kay Sievers0f4dafc2007-12-19 01:40:42 +0100860 /* see if we live in a "glue" directory */
Cornelia Huckc1fe5392008-02-27 15:38:23 +0100861 if (!glue_dir || !dev->class ||
Kay Sievers6b6e39a2010-11-15 23:13:18 +0100862 glue_dir->kset != &dev->class->p->glue_dirs)
Kay Sieversda231fd2007-11-21 17:29:15 +0100863 return;
864
Kay Sievers0f4dafc2007-12-19 01:40:42 +0100865 kobject_put(glue_dir);
Kay Sieversda231fd2007-11-21 17:29:15 +0100866}
Cornelia Huck63b69712008-01-21 16:09:44 +0100867
868static void cleanup_device_parent(struct device *dev)
869{
870 cleanup_glue_dir(dev, dev->kobj.parent);
871}
Kay Sievers86406242007-03-14 03:25:56 +0100872
Cornelia Huck2ee97ca2007-07-18 01:43:47 -0700873static int device_add_class_symlinks(struct device *dev)
874{
875 int error;
876
877 if (!dev->class)
878 return 0;
Kay Sieversda231fd2007-11-21 17:29:15 +0100879
Greg Kroah-Hartman1fbfee62008-05-28 09:28:39 -0700880 error = sysfs_create_link(&dev->kobj,
Kay Sievers6b6e39a2010-11-15 23:13:18 +0100881 &dev->class->p->subsys.kobj,
Cornelia Huck2ee97ca2007-07-18 01:43:47 -0700882 "subsystem");
883 if (error)
884 goto out;
Kay Sieversda231fd2007-11-21 17:29:15 +0100885
Greg Kroah-Hartman4e886c22008-01-27 12:12:43 -0800886 if (dev->parent && device_is_not_partition(dev)) {
Dmitry Torokhov4f01a752007-09-18 22:46:50 -0700887 error = sysfs_create_link(&dev->kobj, &dev->parent->kobj,
888 "device");
889 if (error)
Kay Sievers39aba962010-09-04 22:33:14 -0700890 goto out_subsys;
Cornelia Huck2ee97ca2007-07-18 01:43:47 -0700891 }
Kay Sievers39aba962010-09-04 22:33:14 -0700892
Randy Dunlapead454f2010-09-24 14:36:49 -0700893#ifdef CONFIG_BLOCK
Kay Sievers39aba962010-09-04 22:33:14 -0700894 /* /sys/block has directories and does not need symlinks */
Andi Kleene52eec12010-09-08 16:54:17 +0200895 if (sysfs_deprecated && dev->class == &block_class)
Kay Sievers39aba962010-09-04 22:33:14 -0700896 return 0;
Randy Dunlapead454f2010-09-24 14:36:49 -0700897#endif
Kay Sievers39aba962010-09-04 22:33:14 -0700898
899 /* link in the class directory pointing to the device */
Kay Sievers6b6e39a2010-11-15 23:13:18 +0100900 error = sysfs_create_link(&dev->class->p->subsys.kobj,
Kay Sievers39aba962010-09-04 22:33:14 -0700901 &dev->kobj, dev_name(dev));
902 if (error)
903 goto out_device;
904
Cornelia Huck2ee97ca2007-07-18 01:43:47 -0700905 return 0;
906
Kay Sievers39aba962010-09-04 22:33:14 -0700907out_device:
908 sysfs_remove_link(&dev->kobj, "device");
Kay Sieversda231fd2007-11-21 17:29:15 +0100909
Cornelia Huck2ee97ca2007-07-18 01:43:47 -0700910out_subsys:
911 sysfs_remove_link(&dev->kobj, "subsystem");
912out:
913 return error;
914}
915
916static void device_remove_class_symlinks(struct device *dev)
917{
918 if (!dev->class)
919 return;
Kay Sieversda231fd2007-11-21 17:29:15 +0100920
Greg Kroah-Hartman4e886c22008-01-27 12:12:43 -0800921 if (dev->parent && device_is_not_partition(dev))
Kay Sieversda231fd2007-11-21 17:29:15 +0100922 sysfs_remove_link(&dev->kobj, "device");
Cornelia Huck2ee97ca2007-07-18 01:43:47 -0700923 sysfs_remove_link(&dev->kobj, "subsystem");
Randy Dunlapead454f2010-09-24 14:36:49 -0700924#ifdef CONFIG_BLOCK
Andi Kleene52eec12010-09-08 16:54:17 +0200925 if (sysfs_deprecated && dev->class == &block_class)
Kay Sievers39aba962010-09-04 22:33:14 -0700926 return;
Randy Dunlapead454f2010-09-24 14:36:49 -0700927#endif
Kay Sievers6b6e39a2010-11-15 23:13:18 +0100928 sysfs_delete_link(&dev->class->p->subsys.kobj, &dev->kobj, dev_name(dev));
Cornelia Huck2ee97ca2007-07-18 01:43:47 -0700929}
930
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931/**
Stephen Rothwell413c2392008-05-30 10:16:40 +1000932 * dev_set_name - set a device name
933 * @dev: device
Randy Dunlap46232362008-06-04 21:40:43 -0700934 * @fmt: format string for the device's name
Stephen Rothwell413c2392008-05-30 10:16:40 +1000935 */
936int dev_set_name(struct device *dev, const char *fmt, ...)
937{
938 va_list vargs;
Kay Sievers1fa5ae82009-01-25 15:17:37 +0100939 int err;
Stephen Rothwell413c2392008-05-30 10:16:40 +1000940
941 va_start(vargs, fmt);
Kay Sievers1fa5ae82009-01-25 15:17:37 +0100942 err = kobject_set_name_vargs(&dev->kobj, fmt, vargs);
Stephen Rothwell413c2392008-05-30 10:16:40 +1000943 va_end(vargs);
Kay Sievers1fa5ae82009-01-25 15:17:37 +0100944 return err;
Stephen Rothwell413c2392008-05-30 10:16:40 +1000945}
946EXPORT_SYMBOL_GPL(dev_set_name);
947
948/**
Dan Williamse105b8b2008-04-21 10:51:07 -0700949 * device_to_dev_kobj - select a /sys/dev/ directory for the device
950 * @dev: device
951 *
952 * By default we select char/ for new entries. Setting class->dev_obj
953 * to NULL prevents an entry from being created. class->dev_kobj must
954 * be set (or cleared) before any devices are registered to the class
955 * otherwise device_create_sys_dev_entry() and
Peter Korsgaard0d4e293c2012-04-17 12:12:57 +0200956 * device_remove_sys_dev_entry() will disagree about the presence of
957 * the link.
Dan Williamse105b8b2008-04-21 10:51:07 -0700958 */
959static struct kobject *device_to_dev_kobj(struct device *dev)
960{
961 struct kobject *kobj;
962
963 if (dev->class)
964 kobj = dev->class->dev_kobj;
965 else
966 kobj = sysfs_dev_char_kobj;
967
968 return kobj;
969}
970
971static int device_create_sys_dev_entry(struct device *dev)
972{
973 struct kobject *kobj = device_to_dev_kobj(dev);
974 int error = 0;
975 char devt_str[15];
976
977 if (kobj) {
978 format_dev_t(devt_str, dev->devt);
979 error = sysfs_create_link(kobj, &dev->kobj, devt_str);
980 }
981
982 return error;
983}
984
985static void device_remove_sys_dev_entry(struct device *dev)
986{
987 struct kobject *kobj = device_to_dev_kobj(dev);
988 char devt_str[15];
989
990 if (kobj) {
991 format_dev_t(devt_str, dev->devt);
992 sysfs_remove_link(kobj, devt_str);
993 }
994}
995
Greg Kroah-Hartmanb4028432009-05-11 14:16:57 -0700996int device_private_init(struct device *dev)
997{
998 dev->p = kzalloc(sizeof(*dev->p), GFP_KERNEL);
999 if (!dev->p)
1000 return -ENOMEM;
1001 dev->p->device = dev;
1002 klist_init(&dev->p->klist_children, klist_children_get,
1003 klist_children_put);
Greg Kroah-Hartmanef8a3fd2012-03-08 12:17:22 -08001004 INIT_LIST_HEAD(&dev->p->deferred_probe);
Greg Kroah-Hartmanb4028432009-05-11 14:16:57 -07001005 return 0;
1006}
1007
Dan Williamse105b8b2008-04-21 10:51:07 -07001008/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001009 * device_add - add device to device hierarchy.
1010 * @dev: device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001012 * This is part 2 of device_register(), though may be called
1013 * separately _iff_ device_initialize() has been called separately.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 *
Cornelia Huck57394112008-09-03 18:26:40 +02001015 * This adds @dev to the kobject hierarchy via kobject_add(), adds it
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001016 * to the global and sibling lists for the device, then
1017 * adds it to the other relevant subsystems of the driver model.
Cornelia Huck57394112008-09-03 18:26:40 +02001018 *
Alan Sternb10d5ef2012-01-17 11:39:00 -05001019 * Do not call this routine or device_register() more than once for
1020 * any device structure. The driver model core is not designed to work
1021 * with devices that get unregistered and then spring back to life.
1022 * (Among other things, it's very hard to guarantee that all references
1023 * to the previous incarnation of @dev have been dropped.) Allocate
1024 * and register a fresh new struct device instead.
1025 *
Cornelia Huck57394112008-09-03 18:26:40 +02001026 * NOTE: _Never_ directly free @dev after calling this function, even
1027 * if it returned an error! Always use put_device() to give up your
1028 * reference instead.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 */
1030int device_add(struct device *dev)
1031{
1032 struct device *parent = NULL;
Kay Sieversca22e562011-12-14 14:29:38 -08001033 struct kobject *kobj;
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02001034 struct class_interface *class_intf;
Greg Kroah-Hartmanc906a482008-05-30 10:45:12 -07001035 int error = -EINVAL;
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001036
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 dev = get_device(dev);
Greg Kroah-Hartmanc906a482008-05-30 10:45:12 -07001038 if (!dev)
1039 goto done;
1040
Greg Kroah-Hartmanfb069a52008-12-16 12:23:36 -08001041 if (!dev->p) {
Greg Kroah-Hartmanb4028432009-05-11 14:16:57 -07001042 error = device_private_init(dev);
1043 if (error)
1044 goto done;
Greg Kroah-Hartmanfb069a52008-12-16 12:23:36 -08001045 }
Greg Kroah-Hartmanfb069a52008-12-16 12:23:36 -08001046
Kay Sievers1fa5ae82009-01-25 15:17:37 +01001047 /*
1048 * for statically allocated devices, which should all be converted
1049 * some day, we need to initialize the name. We prevent reading back
1050 * the name, and force the use of dev_name()
1051 */
1052 if (dev->init_name) {
Greg Kroah-Hartmanacc0e902009-06-02 15:39:55 -07001053 dev_set_name(dev, "%s", dev->init_name);
Kay Sievers1fa5ae82009-01-25 15:17:37 +01001054 dev->init_name = NULL;
1055 }
Greg Kroah-Hartmanc906a482008-05-30 10:45:12 -07001056
Kay Sieversca22e562011-12-14 14:29:38 -08001057 /* subsystems can specify simple device enumeration */
1058 if (!dev_name(dev) && dev->bus && dev->bus->dev_name)
1059 dev_set_name(dev, "%s%u", dev->bus->dev_name, dev->id);
1060
Thomas Gleixnere6309e72009-12-10 19:32:49 +00001061 if (!dev_name(dev)) {
1062 error = -EINVAL;
Kay Sievers5c8563d2009-05-28 14:24:07 -07001063 goto name_error;
Thomas Gleixnere6309e72009-12-10 19:32:49 +00001064 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01001066 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
Greg Kroah-Hartmanc205ef42006-08-07 22:19:37 -07001067
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 parent = get_device(dev->parent);
Kay Sieversca22e562011-12-14 14:29:38 -08001069 kobj = get_device_parent(dev, parent);
1070 if (kobj)
1071 dev->kobj.parent = kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072
Yinghai Lu0d358f22008-02-19 03:20:41 -08001073 /* use parent numa_node */
1074 if (parent)
1075 set_dev_node(dev, dev_to_node(parent));
1076
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 /* first, register with generic layer. */
Kay Sievers8a577ff2009-04-18 15:05:45 -07001078 /* we require the name to be set before, and pass NULL */
1079 error = kobject_add(&dev->kobj, dev->kobj.parent, NULL);
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +01001080 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 goto Error;
Kay Sieversa7fd6702005-10-01 14:49:43 +02001082
Brian Walsh37022642006-08-14 22:43:19 -07001083 /* notify platform of device entry */
1084 if (platform_notify)
1085 platform_notify(dev);
1086
Tejun Heoad6a1e12007-06-14 03:45:17 +09001087 error = device_create_file(dev, &uevent_attr);
Cornelia Hucka306eea2006-09-22 11:37:13 +02001088 if (error)
1089 goto attrError;
Kay Sieversa7fd6702005-10-01 14:49:43 +02001090
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001091 if (MAJOR(dev->devt)) {
Tejun Heoad6a1e12007-06-14 03:45:17 +09001092 error = device_create_file(dev, &devt_attr);
1093 if (error)
Cornelia Hucka306eea2006-09-22 11:37:13 +02001094 goto ueventattrError;
Dan Williamse105b8b2008-04-21 10:51:07 -07001095
1096 error = device_create_sys_dev_entry(dev);
1097 if (error)
1098 goto devtattrError;
Kay Sievers2b2af542009-04-30 15:23:42 +02001099
1100 devtmpfs_create_node(dev);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001101 }
1102
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001103 error = device_add_class_symlinks(dev);
1104 if (error)
1105 goto SymlinkError;
Cornelia Huckdc0afa82007-07-09 11:39:18 -07001106 error = device_add_attrs(dev);
1107 if (error)
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07001108 goto AttrsError;
Cornelia Huckdc0afa82007-07-09 11:39:18 -07001109 error = bus_add_device(dev);
1110 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 goto BusError;
Alan Stern3b98aea2008-08-07 13:06:12 -04001112 error = dpm_sysfs_add(dev);
Rafael J. Wysocki57eee3d2008-03-12 00:59:38 +01001113 if (error)
Alan Stern3b98aea2008-08-07 13:06:12 -04001114 goto DPMError;
1115 device_pm_add(dev);
Alan Sternec0676ee2008-12-05 14:10:31 -05001116
1117 /* Notify clients of device addition. This call must come
majianpeng268863f2012-01-11 15:12:06 +00001118 * after dpm_sysfs_add() and before kobject_uevent().
Alan Sternec0676ee2008-12-05 14:10:31 -05001119 */
1120 if (dev->bus)
1121 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
1122 BUS_NOTIFY_ADD_DEVICE, dev);
1123
Cornelia Huck83b5fb4c2007-03-29 11:12:11 +02001124 kobject_uevent(&dev->kobj, KOBJ_ADD);
Alan Stern2023c612009-07-30 15:27:18 -04001125 bus_probe_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 if (parent)
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08001127 klist_add_tail(&dev->p->knode_parent,
1128 &parent->p->klist_children);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129
Greg Kroah-Hartman5d9fd162006-06-22 17:17:32 -07001130 if (dev->class) {
Kay Sieversca22e562011-12-14 14:29:38 -08001131 mutex_lock(&dev->class->p->mutex);
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02001132 /* tie the class to the device */
Tejun Heo5a3ceb82008-08-25 19:50:19 +02001133 klist_add_tail(&dev->knode_class,
Kay Sievers6b6e39a2010-11-15 23:13:18 +01001134 &dev->class->p->klist_devices);
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02001135
1136 /* notify any interfaces that the device is here */
Greg Kroah-Hartman184f1f72008-05-28 09:28:39 -07001137 list_for_each_entry(class_intf,
Kay Sieversca22e562011-12-14 14:29:38 -08001138 &dev->class->p->interfaces, node)
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02001139 if (class_intf->add_dev)
1140 class_intf->add_dev(dev, class_intf);
Kay Sieversca22e562011-12-14 14:29:38 -08001141 mutex_unlock(&dev->class->p->mutex);
Greg Kroah-Hartman5d9fd162006-06-22 17:17:32 -07001142 }
Greg Kroah-Hartmanc906a482008-05-30 10:45:12 -07001143done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 put_device(dev);
1145 return error;
Alan Stern3b98aea2008-08-07 13:06:12 -04001146 DPMError:
Rafael J. Wysocki57eee3d2008-03-12 00:59:38 +01001147 bus_remove_device(dev);
1148 BusError:
James Simmons82f0cf92007-02-21 17:44:51 +00001149 device_remove_attrs(dev);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07001150 AttrsError:
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001151 device_remove_class_symlinks(dev);
1152 SymlinkError:
Tejun Heoad6a1e12007-06-14 03:45:17 +09001153 if (MAJOR(dev->devt))
Kay Sieversad729562009-10-28 19:51:37 +01001154 devtmpfs_delete_node(dev);
1155 if (MAJOR(dev->devt))
Dan Williamse105b8b2008-04-21 10:51:07 -07001156 device_remove_sys_dev_entry(dev);
1157 devtattrError:
1158 if (MAJOR(dev->devt))
Tejun Heoad6a1e12007-06-14 03:45:17 +09001159 device_remove_file(dev, &devt_attr);
Cornelia Hucka306eea2006-09-22 11:37:13 +02001160 ueventattrError:
Tejun Heoad6a1e12007-06-14 03:45:17 +09001161 device_remove_file(dev, &uevent_attr);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001162 attrError:
Kay Sievers312c0042005-11-16 09:00:00 +01001163 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 kobject_del(&dev->kobj);
1165 Error:
Cornelia Huck63b69712008-01-21 16:09:44 +01001166 cleanup_device_parent(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 if (parent)
1168 put_device(parent);
Kay Sievers5c8563d2009-05-28 14:24:07 -07001169name_error:
1170 kfree(dev->p);
1171 dev->p = NULL;
Greg Kroah-Hartmanc906a482008-05-30 10:45:12 -07001172 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173}
David Graham White86df2682013-07-21 20:41:14 -04001174EXPORT_SYMBOL_GPL(device_add);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001177 * device_register - register a device with the system.
1178 * @dev: pointer to the device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001180 * This happens in two clean steps - initialize the device
1181 * and add it to the system. The two steps can be called
1182 * separately, but this is the easiest and most common.
1183 * I.e. you should only call the two helpers separately if
1184 * have a clearly defined need to use and refcount the device
1185 * before it is added to the hierarchy.
Cornelia Huck57394112008-09-03 18:26:40 +02001186 *
Alan Sternb10d5ef2012-01-17 11:39:00 -05001187 * For more information, see the kerneldoc for device_initialize()
1188 * and device_add().
1189 *
Cornelia Huck57394112008-09-03 18:26:40 +02001190 * NOTE: _Never_ directly free @dev after calling this function, even
1191 * if it returned an error! Always use put_device() to give up the
1192 * reference initialized in this function instead.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194int device_register(struct device *dev)
1195{
1196 device_initialize(dev);
1197 return device_add(dev);
1198}
David Graham White86df2682013-07-21 20:41:14 -04001199EXPORT_SYMBOL_GPL(device_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001202 * get_device - increment reference count for device.
1203 * @dev: device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001205 * This simply forwards the call to kobject_get(), though
1206 * we do take care to provide for the case that we get a NULL
1207 * pointer passed in.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001209struct device *get_device(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +02001211 return dev ? kobj_to_dev(kobject_get(&dev->kobj)) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212}
David Graham White86df2682013-07-21 20:41:14 -04001213EXPORT_SYMBOL_GPL(get_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001216 * put_device - decrement reference count.
1217 * @dev: device in question.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001219void put_device(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220{
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001221 /* might_sleep(); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 if (dev)
1223 kobject_put(&dev->kobj);
1224}
David Graham White86df2682013-07-21 20:41:14 -04001225EXPORT_SYMBOL_GPL(put_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001228 * device_del - delete device from system.
1229 * @dev: device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001231 * This is the first part of the device unregistration
1232 * sequence. This removes the device from the lists we control
1233 * from here, has it removed from the other driver model
1234 * subsystems it was added to in device_add(), and removes it
1235 * from the kobject hierarchy.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001237 * NOTE: this should be called manually _iff_ device_add() was
1238 * also called manually.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001240void device_del(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001242 struct device *parent = dev->parent;
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02001243 struct class_interface *class_intf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244
Alan Sternec0676ee2008-12-05 14:10:31 -05001245 /* Notify clients of device removal. This call must come
1246 * before dpm_sysfs_remove().
1247 */
1248 if (dev->bus)
1249 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
1250 BUS_NOTIFY_DEL_DEVICE, dev);
Alan Stern3b98aea2008-08-07 13:06:12 -04001251 dpm_sysfs_remove(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 if (parent)
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08001253 klist_del(&dev->p->knode_parent);
Dan Williamse105b8b2008-04-21 10:51:07 -07001254 if (MAJOR(dev->devt)) {
Kay Sievers2b2af542009-04-30 15:23:42 +02001255 devtmpfs_delete_node(dev);
Dan Williamse105b8b2008-04-21 10:51:07 -07001256 device_remove_sys_dev_entry(dev);
Tejun Heoad6a1e12007-06-14 03:45:17 +09001257 device_remove_file(dev, &devt_attr);
Dan Williamse105b8b2008-04-21 10:51:07 -07001258 }
Kay Sieversb9d9c822006-06-15 15:31:56 +02001259 if (dev->class) {
Kay Sieversda231fd2007-11-21 17:29:15 +01001260 device_remove_class_symlinks(dev);
Kay Sievers99ef3ef2006-09-14 11:23:28 +02001261
Kay Sieversca22e562011-12-14 14:29:38 -08001262 mutex_lock(&dev->class->p->mutex);
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02001263 /* notify any interfaces that the device is now gone */
Greg Kroah-Hartman184f1f72008-05-28 09:28:39 -07001264 list_for_each_entry(class_intf,
Kay Sieversca22e562011-12-14 14:29:38 -08001265 &dev->class->p->interfaces, node)
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02001266 if (class_intf->remove_dev)
1267 class_intf->remove_dev(dev, class_intf);
1268 /* remove the device from the class list */
Tejun Heo5a3ceb82008-08-25 19:50:19 +02001269 klist_del(&dev->knode_class);
Kay Sieversca22e562011-12-14 14:29:38 -08001270 mutex_unlock(&dev->class->p->mutex);
Kay Sieversb9d9c822006-06-15 15:31:56 +02001271 }
Tejun Heoad6a1e12007-06-14 03:45:17 +09001272 device_remove_file(dev, &uevent_attr);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07001273 device_remove_attrs(dev);
Benjamin Herrenschmidt28953532006-11-08 19:46:14 -08001274 bus_remove_device(dev);
LongX Zhang4b6d1f122012-10-25 00:21:28 +02001275 device_pm_remove(dev);
Grant Likelyd1c34142012-03-05 08:47:41 -07001276 driver_deferred_probe_del(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277
1278 /* Notify the platform of the removal, in case they
1279 * need to do anything...
1280 */
1281 if (platform_notify_remove)
1282 platform_notify_remove(dev);
Kay Sievers312c0042005-11-16 09:00:00 +01001283 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
Kay Sieversda231fd2007-11-21 17:29:15 +01001284 cleanup_device_parent(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 kobject_del(&dev->kobj);
Kay Sieversda231fd2007-11-21 17:29:15 +01001286 put_device(parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287}
David Graham White86df2682013-07-21 20:41:14 -04001288EXPORT_SYMBOL_GPL(device_del);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289
1290/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001291 * device_unregister - unregister device from system.
1292 * @dev: device going away.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001294 * We do this in two parts, like we do device_register(). First,
1295 * we remove it from all the subsystems with device_del(), then
1296 * we decrement the reference count via put_device(). If that
1297 * is the final reference count, the device will be cleaned up
1298 * via device_release() above. Otherwise, the structure will
1299 * stick around until the final reference to the device is dropped.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001301void device_unregister(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302{
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01001303 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 device_del(dev);
1305 put_device(dev);
1306}
David Graham White86df2682013-07-21 20:41:14 -04001307EXPORT_SYMBOL_GPL(device_unregister);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001309static struct device *next_device(struct klist_iter *i)
mochel@digitalimplant.org36239572005-03-24 19:08:30 -08001310{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001311 struct klist_node *n = klist_next(i);
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08001312 struct device *dev = NULL;
1313 struct device_private *p;
1314
1315 if (n) {
1316 p = to_device_private_parent(n);
1317 dev = p->device;
1318 }
1319 return dev;
mochel@digitalimplant.org36239572005-03-24 19:08:30 -08001320}
1321
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322/**
Kay Sieverse454cea2009-09-18 23:01:12 +02001323 * device_get_devnode - path of device node file
Kay Sievers6fcf53a2009-04-30 15:23:42 +02001324 * @dev: device
Kay Sieverse454cea2009-09-18 23:01:12 +02001325 * @mode: returned file access mode
Kay Sievers3c2670e2013-04-06 09:56:00 -07001326 * @uid: returned file owner
1327 * @gid: returned file group
Kay Sievers6fcf53a2009-04-30 15:23:42 +02001328 * @tmp: possibly allocated string
1329 *
1330 * Return the relative path of a possible device node.
1331 * Non-default names may need to allocate a memory to compose
1332 * a name. This memory is returned in tmp and needs to be
1333 * freed by the caller.
1334 */
Kay Sieverse454cea2009-09-18 23:01:12 +02001335const char *device_get_devnode(struct device *dev,
Greg Kroah-Hartman4e4098a2013-04-11 11:43:29 -07001336 umode_t *mode, kuid_t *uid, kgid_t *gid,
Kay Sievers3c2670e2013-04-06 09:56:00 -07001337 const char **tmp)
Kay Sievers6fcf53a2009-04-30 15:23:42 +02001338{
1339 char *s;
1340
1341 *tmp = NULL;
1342
1343 /* the device type may provide a specific name */
Kay Sieverse454cea2009-09-18 23:01:12 +02001344 if (dev->type && dev->type->devnode)
Kay Sievers3c2670e2013-04-06 09:56:00 -07001345 *tmp = dev->type->devnode(dev, mode, uid, gid);
Kay Sievers6fcf53a2009-04-30 15:23:42 +02001346 if (*tmp)
1347 return *tmp;
1348
1349 /* the class may provide a specific name */
Kay Sieverse454cea2009-09-18 23:01:12 +02001350 if (dev->class && dev->class->devnode)
1351 *tmp = dev->class->devnode(dev, mode);
Kay Sievers6fcf53a2009-04-30 15:23:42 +02001352 if (*tmp)
1353 return *tmp;
1354
1355 /* return name without allocation, tmp == NULL */
1356 if (strchr(dev_name(dev), '!') == NULL)
1357 return dev_name(dev);
1358
1359 /* replace '!' in the name with '/' */
1360 *tmp = kstrdup(dev_name(dev), GFP_KERNEL);
1361 if (!*tmp)
1362 return NULL;
1363 while ((s = strchr(*tmp, '!')))
1364 s[0] = '/';
1365 return *tmp;
1366}
1367
1368/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001369 * device_for_each_child - device child iterator.
1370 * @parent: parent struct device.
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001371 * @fn: function to be called for each device.
Robert P. J. Dayf8878dc2013-06-01 20:17:34 -04001372 * @data: data for the callback.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001374 * Iterate over @parent's child devices, and call @fn for each,
1375 * passing it @data.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001377 * We check the return of @fn each time. If it returns anything
1378 * other than 0, we break out and return that value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001380int device_for_each_child(struct device *parent, void *data,
1381 int (*fn)(struct device *dev, void *data))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382{
mochel@digitalimplant.org36239572005-03-24 19:08:30 -08001383 struct klist_iter i;
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001384 struct device *child;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 int error = 0;
1386
Greg Kroah-Hartman014c90db2009-04-15 16:00:12 -07001387 if (!parent->p)
1388 return 0;
1389
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08001390 klist_iter_init(&parent->p->klist_children, &i);
mochel@digitalimplant.org36239572005-03-24 19:08:30 -08001391 while ((child = next_device(&i)) && !error)
1392 error = fn(child, data);
1393 klist_iter_exit(&i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 return error;
1395}
David Graham White86df2682013-07-21 20:41:14 -04001396EXPORT_SYMBOL_GPL(device_for_each_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397
Cornelia Huck5ab69982006-11-16 15:42:07 +01001398/**
1399 * device_find_child - device iterator for locating a particular device.
1400 * @parent: parent struct device
Cornelia Huck5ab69982006-11-16 15:42:07 +01001401 * @match: Callback function to check device
Robert P. J. Dayf8878dc2013-06-01 20:17:34 -04001402 * @data: Data to pass to match function
Cornelia Huck5ab69982006-11-16 15:42:07 +01001403 *
1404 * This is similar to the device_for_each_child() function above, but it
1405 * returns a reference to a device that is 'found' for later use, as
1406 * determined by the @match callback.
1407 *
1408 * The callback should return 0 if the device doesn't match and non-zero
1409 * if it does. If the callback returns non-zero and a reference to the
1410 * current device can be obtained, this function will return to the caller
1411 * and not iterate over any more devices.
Federico Vagaa4e24002013-04-15 11:18:11 +02001412 *
1413 * NOTE: you will need to drop the reference with put_device() after use.
Cornelia Huck5ab69982006-11-16 15:42:07 +01001414 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001415struct device *device_find_child(struct device *parent, void *data,
1416 int (*match)(struct device *dev, void *data))
Cornelia Huck5ab69982006-11-16 15:42:07 +01001417{
1418 struct klist_iter i;
1419 struct device *child;
1420
1421 if (!parent)
1422 return NULL;
1423
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08001424 klist_iter_init(&parent->p->klist_children, &i);
Cornelia Huck5ab69982006-11-16 15:42:07 +01001425 while ((child = next_device(&i)))
1426 if (match(child, data) && get_device(child))
1427 break;
1428 klist_iter_exit(&i);
1429 return child;
1430}
David Graham White86df2682013-07-21 20:41:14 -04001431EXPORT_SYMBOL_GPL(device_find_child);
Cornelia Huck5ab69982006-11-16 15:42:07 +01001432
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433int __init devices_init(void)
1434{
Greg Kroah-Hartman881c6cfd2007-11-01 09:29:06 -06001435 devices_kset = kset_create_and_add("devices", &device_uevent_ops, NULL);
1436 if (!devices_kset)
1437 return -ENOMEM;
Dan Williamse105b8b2008-04-21 10:51:07 -07001438 dev_kobj = kobject_create_and_add("dev", NULL);
1439 if (!dev_kobj)
1440 goto dev_kobj_err;
1441 sysfs_dev_block_kobj = kobject_create_and_add("block", dev_kobj);
1442 if (!sysfs_dev_block_kobj)
1443 goto block_kobj_err;
1444 sysfs_dev_char_kobj = kobject_create_and_add("char", dev_kobj);
1445 if (!sysfs_dev_char_kobj)
1446 goto char_kobj_err;
1447
Greg Kroah-Hartman881c6cfd2007-11-01 09:29:06 -06001448 return 0;
Dan Williamse105b8b2008-04-21 10:51:07 -07001449
1450 char_kobj_err:
1451 kobject_put(sysfs_dev_block_kobj);
1452 block_kobj_err:
1453 kobject_put(dev_kobj);
1454 dev_kobj_err:
1455 kset_unregister(devices_kset);
1456 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457}
1458
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02001459static DEFINE_MUTEX(device_hotplug_lock);
1460
1461void lock_device_hotplug(void)
1462{
1463 mutex_lock(&device_hotplug_lock);
1464}
1465
1466void unlock_device_hotplug(void)
1467{
1468 mutex_unlock(&device_hotplug_lock);
1469}
1470
1471static int device_check_offline(struct device *dev, void *not_used)
1472{
1473 int ret;
1474
1475 ret = device_for_each_child(dev, NULL, device_check_offline);
1476 if (ret)
1477 return ret;
1478
1479 return device_supports_offline(dev) && !dev->offline ? -EBUSY : 0;
1480}
1481
1482/**
1483 * device_offline - Prepare the device for hot-removal.
1484 * @dev: Device to be put offline.
1485 *
1486 * Execute the device bus type's .offline() callback, if present, to prepare
1487 * the device for a subsequent hot-removal. If that succeeds, the device must
1488 * not be used until either it is removed or its bus type's .online() callback
1489 * is executed.
1490 *
1491 * Call under device_hotplug_lock.
1492 */
1493int device_offline(struct device *dev)
1494{
1495 int ret;
1496
1497 if (dev->offline_disabled)
1498 return -EPERM;
1499
1500 ret = device_for_each_child(dev, NULL, device_check_offline);
1501 if (ret)
1502 return ret;
1503
1504 device_lock(dev);
1505 if (device_supports_offline(dev)) {
1506 if (dev->offline) {
1507 ret = 1;
1508 } else {
1509 ret = dev->bus->offline(dev);
1510 if (!ret) {
1511 kobject_uevent(&dev->kobj, KOBJ_OFFLINE);
1512 dev->offline = true;
1513 }
1514 }
1515 }
1516 device_unlock(dev);
1517
1518 return ret;
1519}
1520
1521/**
1522 * device_online - Put the device back online after successful device_offline().
1523 * @dev: Device to be put back online.
1524 *
1525 * If device_offline() has been successfully executed for @dev, but the device
1526 * has not been removed subsequently, execute its bus type's .online() callback
1527 * to indicate that the device can be used again.
1528 *
1529 * Call under device_hotplug_lock.
1530 */
1531int device_online(struct device *dev)
1532{
1533 int ret = 0;
1534
1535 device_lock(dev);
1536 if (device_supports_offline(dev)) {
1537 if (dev->offline) {
1538 ret = dev->bus->online(dev);
1539 if (!ret) {
1540 kobject_uevent(&dev->kobj, KOBJ_ONLINE);
1541 dev->offline = false;
1542 }
1543 } else {
1544 ret = 1;
1545 }
1546 }
1547 device_unlock(dev);
1548
1549 return ret;
1550}
1551
Karthigan Srinivasan7f100d12011-04-18 16:16:52 -05001552struct root_device {
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00001553 struct device dev;
1554 struct module *owner;
1555};
1556
Josh Triplett93058422012-11-18 21:27:55 -08001557static inline struct root_device *to_root_device(struct device *d)
Ferenc Wagner481e2072011-01-07 15:17:47 +01001558{
1559 return container_of(d, struct root_device, dev);
1560}
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00001561
1562static void root_device_release(struct device *dev)
1563{
1564 kfree(to_root_device(dev));
1565}
1566
1567/**
1568 * __root_device_register - allocate and register a root device
1569 * @name: root device name
1570 * @owner: owner module of the root device, usually THIS_MODULE
1571 *
1572 * This function allocates a root device and registers it
1573 * using device_register(). In order to free the returned
1574 * device, use root_device_unregister().
1575 *
1576 * Root devices are dummy devices which allow other devices
1577 * to be grouped under /sys/devices. Use this function to
1578 * allocate a root device and then use it as the parent of
1579 * any device which should appear under /sys/devices/{name}
1580 *
1581 * The /sys/devices/{name} directory will also contain a
1582 * 'module' symlink which points to the @owner directory
1583 * in sysfs.
1584 *
Jani Nikulaf0eae0e2010-03-11 18:11:45 +02001585 * Returns &struct device pointer on success, or ERR_PTR() on error.
1586 *
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00001587 * Note: You probably want to use root_device_register().
1588 */
1589struct device *__root_device_register(const char *name, struct module *owner)
1590{
1591 struct root_device *root;
1592 int err = -ENOMEM;
1593
1594 root = kzalloc(sizeof(struct root_device), GFP_KERNEL);
1595 if (!root)
1596 return ERR_PTR(err);
1597
Greg Kroah-Hartmanacc0e902009-06-02 15:39:55 -07001598 err = dev_set_name(&root->dev, "%s", name);
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00001599 if (err) {
1600 kfree(root);
1601 return ERR_PTR(err);
1602 }
1603
1604 root->dev.release = root_device_release;
1605
1606 err = device_register(&root->dev);
1607 if (err) {
1608 put_device(&root->dev);
1609 return ERR_PTR(err);
1610 }
1611
Christoph Egger1d9e8822010-05-17 16:57:58 +02001612#ifdef CONFIG_MODULES /* gotta find a "cleaner" way to do this */
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00001613 if (owner) {
1614 struct module_kobject *mk = &owner->mkobj;
1615
1616 err = sysfs_create_link(&root->dev.kobj, &mk->kobj, "module");
1617 if (err) {
1618 device_unregister(&root->dev);
1619 return ERR_PTR(err);
1620 }
1621 root->owner = owner;
1622 }
1623#endif
1624
1625 return &root->dev;
1626}
1627EXPORT_SYMBOL_GPL(__root_device_register);
1628
1629/**
1630 * root_device_unregister - unregister and free a root device
Randy Dunlap7cbcf222009-01-20 16:29:13 -08001631 * @dev: device going away
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00001632 *
1633 * This function unregisters and cleans up a device that was created by
1634 * root_device_register().
1635 */
1636void root_device_unregister(struct device *dev)
1637{
1638 struct root_device *root = to_root_device(dev);
1639
1640 if (root->owner)
1641 sysfs_remove_link(&root->dev.kobj, "module");
1642
1643 device_unregister(dev);
1644}
1645EXPORT_SYMBOL_GPL(root_device_unregister);
1646
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001647
1648static void device_create_release(struct device *dev)
1649{
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01001650 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001651 kfree(dev);
1652}
1653
Guenter Roeck39ef3112013-07-14 16:05:57 -07001654static struct device *
1655device_create_groups_vargs(struct class *class, struct device *parent,
1656 dev_t devt, void *drvdata,
1657 const struct attribute_group **groups,
1658 const char *fmt, va_list args)
1659{
1660 struct device *dev = NULL;
1661 int retval = -ENODEV;
1662
1663 if (class == NULL || IS_ERR(class))
1664 goto error;
1665
1666 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1667 if (!dev) {
1668 retval = -ENOMEM;
1669 goto error;
1670 }
1671
1672 dev->devt = devt;
1673 dev->class = class;
1674 dev->parent = parent;
1675 dev->groups = groups;
1676 dev->release = device_create_release;
1677 dev_set_drvdata(dev, drvdata);
1678
1679 retval = kobject_set_name_vargs(&dev->kobj, fmt, args);
1680 if (retval)
1681 goto error;
1682
1683 retval = device_register(dev);
1684 if (retval)
1685 goto error;
1686
1687 return dev;
1688
1689error:
1690 put_device(dev);
1691 return ERR_PTR(retval);
1692}
1693
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001694/**
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07001695 * device_create_vargs - creates a device and registers it with sysfs
1696 * @class: pointer to the struct class that this device should be registered to
1697 * @parent: pointer to the parent struct device of this new device, if any
1698 * @devt: the dev_t for the char device to be added
1699 * @drvdata: the data to be added to the device for callbacks
1700 * @fmt: string for the device's name
1701 * @args: va_list for the device's name
1702 *
1703 * This function can be used by char device classes. A struct device
1704 * will be created in sysfs, registered to the specified class.
1705 *
1706 * A "dev" file will be created, showing the dev_t for the device, if
1707 * the dev_t is not 0,0.
1708 * If a pointer to a parent struct device is passed in, the newly created
1709 * struct device will be a child of that device in sysfs.
1710 * The pointer to the struct device will be returned from the call.
1711 * Any further sysfs files that might be required can be created using this
1712 * pointer.
1713 *
Jani Nikulaf0eae0e2010-03-11 18:11:45 +02001714 * Returns &struct device pointer on success, or ERR_PTR() on error.
1715 *
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07001716 * Note: the struct class passed to this function must have previously
1717 * been created with a call to class_create().
1718 */
1719struct device *device_create_vargs(struct class *class, struct device *parent,
1720 dev_t devt, void *drvdata, const char *fmt,
1721 va_list args)
1722{
Guenter Roeck39ef3112013-07-14 16:05:57 -07001723 return device_create_groups_vargs(class, parent, devt, drvdata, NULL,
1724 fmt, args);
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07001725}
1726EXPORT_SYMBOL_GPL(device_create_vargs);
1727
1728/**
Greg Kroah-Hartman4e106732008-07-21 20:03:34 -07001729 * device_create - creates a device and registers it with sysfs
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07001730 * @class: pointer to the struct class that this device should be registered to
1731 * @parent: pointer to the parent struct device of this new device, if any
1732 * @devt: the dev_t for the char device to be added
1733 * @drvdata: the data to be added to the device for callbacks
1734 * @fmt: string for the device's name
1735 *
1736 * This function can be used by char device classes. A struct device
1737 * will be created in sysfs, registered to the specified class.
1738 *
1739 * A "dev" file will be created, showing the dev_t for the device, if
1740 * the dev_t is not 0,0.
1741 * If a pointer to a parent struct device is passed in, the newly created
1742 * struct device will be a child of that device in sysfs.
1743 * The pointer to the struct device will be returned from the call.
1744 * Any further sysfs files that might be required can be created using this
1745 * pointer.
1746 *
Jani Nikulaf0eae0e2010-03-11 18:11:45 +02001747 * Returns &struct device pointer on success, or ERR_PTR() on error.
1748 *
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07001749 * Note: the struct class passed to this function must have previously
1750 * been created with a call to class_create().
1751 */
Greg Kroah-Hartman4e106732008-07-21 20:03:34 -07001752struct device *device_create(struct class *class, struct device *parent,
1753 dev_t devt, void *drvdata, const char *fmt, ...)
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07001754{
1755 va_list vargs;
1756 struct device *dev;
1757
1758 va_start(vargs, fmt);
1759 dev = device_create_vargs(class, parent, devt, drvdata, fmt, vargs);
1760 va_end(vargs);
1761 return dev;
1762}
Greg Kroah-Hartman4e106732008-07-21 20:03:34 -07001763EXPORT_SYMBOL_GPL(device_create);
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07001764
Guenter Roeck39ef3112013-07-14 16:05:57 -07001765/**
1766 * device_create_with_groups - creates a device and registers it with sysfs
1767 * @class: pointer to the struct class that this device should be registered to
1768 * @parent: pointer to the parent struct device of this new device, if any
1769 * @devt: the dev_t for the char device to be added
1770 * @drvdata: the data to be added to the device for callbacks
1771 * @groups: NULL-terminated list of attribute groups to be created
1772 * @fmt: string for the device's name
1773 *
1774 * This function can be used by char device classes. A struct device
1775 * will be created in sysfs, registered to the specified class.
1776 * Additional attributes specified in the groups parameter will also
1777 * be created automatically.
1778 *
1779 * A "dev" file will be created, showing the dev_t for the device, if
1780 * the dev_t is not 0,0.
1781 * If a pointer to a parent struct device is passed in, the newly created
1782 * struct device will be a child of that device in sysfs.
1783 * The pointer to the struct device will be returned from the call.
1784 * Any further sysfs files that might be required can be created using this
1785 * pointer.
1786 *
1787 * Returns &struct device pointer on success, or ERR_PTR() on error.
1788 *
1789 * Note: the struct class passed to this function must have previously
1790 * been created with a call to class_create().
1791 */
1792struct device *device_create_with_groups(struct class *class,
1793 struct device *parent, dev_t devt,
1794 void *drvdata,
1795 const struct attribute_group **groups,
1796 const char *fmt, ...)
1797{
1798 va_list vargs;
1799 struct device *dev;
1800
1801 va_start(vargs, fmt);
1802 dev = device_create_groups_vargs(class, parent, devt, drvdata, groups,
1803 fmt, vargs);
1804 va_end(vargs);
1805 return dev;
1806}
1807EXPORT_SYMBOL_GPL(device_create_with_groups);
1808
Michał Mirosław9f3b7952013-02-01 20:40:17 +01001809static int __match_devt(struct device *dev, const void *data)
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001810{
Michał Mirosław9f3b7952013-02-01 20:40:17 +01001811 const dev_t *devt = data;
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001812
Dave Youngcd354492008-01-28 16:56:11 +08001813 return dev->devt == *devt;
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001814}
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001815
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001816/**
1817 * device_destroy - removes a device that was created with device_create()
1818 * @class: pointer to the struct class that this device was registered with
1819 * @devt: the dev_t of the device that was previously registered
1820 *
1821 * This call unregisters and cleans up a device that was created with a
1822 * call to device_create().
1823 */
1824void device_destroy(struct class *class, dev_t devt)
1825{
1826 struct device *dev;
1827
Greg Kroah-Hartman695794a2008-05-22 17:21:08 -04001828 dev = class_find_device(class, NULL, &devt, __match_devt);
Dave Youngcd354492008-01-28 16:56:11 +08001829 if (dev) {
1830 put_device(dev);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001831 device_unregister(dev);
Dave Youngcd354492008-01-28 16:56:11 +08001832 }
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001833}
1834EXPORT_SYMBOL_GPL(device_destroy);
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001835
1836/**
1837 * device_rename - renames a device
1838 * @dev: the pointer to the struct device to be renamed
1839 * @new_name: the new name of the device
Eric W. Biederman030c1d22008-05-08 14:41:00 -07001840 *
1841 * It is the responsibility of the caller to provide mutual
1842 * exclusion between two different calls of device_rename
1843 * on the same device to ensure that new_name is valid and
1844 * won't conflict with other devices.
Michael Ellermanc6c0ac62010-11-25 09:44:07 +11001845 *
Timur Tabia5462512010-12-13 14:08:52 -06001846 * Note: Don't call this function. Currently, the networking layer calls this
1847 * function, but that will change. The following text from Kay Sievers offers
1848 * some insight:
1849 *
1850 * Renaming devices is racy at many levels, symlinks and other stuff are not
1851 * replaced atomically, and you get a "move" uevent, but it's not easy to
1852 * connect the event to the old and new device. Device nodes are not renamed at
1853 * all, there isn't even support for that in the kernel now.
1854 *
1855 * In the meantime, during renaming, your target name might be taken by another
1856 * driver, creating conflicts. Or the old name is taken directly after you
1857 * renamed it -- then you get events for the same DEVPATH, before you even see
1858 * the "move" event. It's just a mess, and nothing new should ever rely on
1859 * kernel device renaming. Besides that, it's not even implemented now for
1860 * other things than (driver-core wise very simple) network devices.
1861 *
1862 * We are currently about to change network renaming in udev to completely
1863 * disallow renaming of devices in the same namespace as the kernel uses,
1864 * because we can't solve the problems properly, that arise with swapping names
1865 * of multiple interfaces without races. Means, renaming of eth[0-9]* will only
1866 * be allowed to some other name than eth[0-9]*, for the aforementioned
1867 * reasons.
1868 *
1869 * Make up a "real" name in the driver before you register anything, or add
1870 * some other attributes for userspace to find the device, or use udev to add
1871 * symlinks -- but never rename kernel devices later, it's a complete mess. We
1872 * don't even want to get into that and try to implement the missing pieces in
1873 * the core. We really have other pieces to fix in the driver core mess. :)
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001874 */
Johannes Berg6937e8f2010-08-05 17:38:18 +02001875int device_rename(struct device *dev, const char *new_name)
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001876{
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001877 char *old_device_name = NULL;
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001878 int error;
1879
1880 dev = get_device(dev);
1881 if (!dev)
1882 return -EINVAL;
1883
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01001884 pr_debug("device: '%s': %s: renaming to '%s'\n", dev_name(dev),
Harvey Harrison2b3a3022008-03-04 16:41:05 -08001885 __func__, new_name);
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001886
Kay Sievers1fa5ae82009-01-25 15:17:37 +01001887 old_device_name = kstrdup(dev_name(dev), GFP_KERNEL);
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001888 if (!old_device_name) {
1889 error = -ENOMEM;
1890 goto out;
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001891 }
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001892
Eric W. Biedermanf349cf32010-03-30 11:31:29 -07001893 if (dev->class) {
Kay Sievers6b6e39a2010-11-15 23:13:18 +01001894 error = sysfs_rename_link(&dev->class->p->subsys.kobj,
Eric W. Biedermanf349cf32010-03-30 11:31:29 -07001895 &dev->kobj, old_device_name, new_name);
1896 if (error)
1897 goto out;
1898 }
Kay Sievers39aba962010-09-04 22:33:14 -07001899
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001900 error = kobject_rename(&dev->kobj, new_name);
Kay Sievers1fa5ae82009-01-25 15:17:37 +01001901 if (error)
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001902 goto out;
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001903
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001904out:
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001905 put_device(dev);
1906
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001907 kfree(old_device_name);
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001908
1909 return error;
1910}
Johannes Berga2807db2007-02-28 12:38:31 +01001911EXPORT_SYMBOL_GPL(device_rename);
Cornelia Huck8a824722006-11-20 17:07:51 +01001912
1913static int device_move_class_links(struct device *dev,
1914 struct device *old_parent,
1915 struct device *new_parent)
1916{
Greg Kroah-Hartmanf7f34612007-03-06 12:55:53 -08001917 int error = 0;
Cornelia Huck8a824722006-11-20 17:07:51 +01001918
Greg Kroah-Hartmanf7f34612007-03-06 12:55:53 -08001919 if (old_parent)
1920 sysfs_remove_link(&dev->kobj, "device");
1921 if (new_parent)
1922 error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
1923 "device");
1924 return error;
Cornelia Huck8a824722006-11-20 17:07:51 +01001925}
1926
1927/**
1928 * device_move - moves a device to a new parent
1929 * @dev: the pointer to the struct device to be moved
Cornelia Huckc744aeae2007-01-08 20:16:44 +01001930 * @new_parent: the new parent of the device (can by NULL)
Cornelia Huckffa6a702009-03-04 12:44:00 +01001931 * @dpm_order: how to reorder the dpm_list
Cornelia Huck8a824722006-11-20 17:07:51 +01001932 */
Cornelia Huckffa6a702009-03-04 12:44:00 +01001933int device_move(struct device *dev, struct device *new_parent,
1934 enum dpm_order dpm_order)
Cornelia Huck8a824722006-11-20 17:07:51 +01001935{
1936 int error;
1937 struct device *old_parent;
Cornelia Huckc744aeae2007-01-08 20:16:44 +01001938 struct kobject *new_parent_kobj;
Cornelia Huck8a824722006-11-20 17:07:51 +01001939
1940 dev = get_device(dev);
1941 if (!dev)
1942 return -EINVAL;
1943
Cornelia Huckffa6a702009-03-04 12:44:00 +01001944 device_pm_lock();
Cornelia Huck8a824722006-11-20 17:07:51 +01001945 new_parent = get_device(new_parent);
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001946 new_parent_kobj = get_device_parent(dev, new_parent);
Cornelia Huck63b69712008-01-21 16:09:44 +01001947
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01001948 pr_debug("device: '%s': %s: moving to '%s'\n", dev_name(dev),
1949 __func__, new_parent ? dev_name(new_parent) : "<NULL>");
Cornelia Huckc744aeae2007-01-08 20:16:44 +01001950 error = kobject_move(&dev->kobj, new_parent_kobj);
Cornelia Huck8a824722006-11-20 17:07:51 +01001951 if (error) {
Cornelia Huck63b69712008-01-21 16:09:44 +01001952 cleanup_glue_dir(dev, new_parent_kobj);
Cornelia Huck8a824722006-11-20 17:07:51 +01001953 put_device(new_parent);
1954 goto out;
1955 }
1956 old_parent = dev->parent;
1957 dev->parent = new_parent;
1958 if (old_parent)
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08001959 klist_remove(&dev->p->knode_parent);
Yinghai Lu0d358f22008-02-19 03:20:41 -08001960 if (new_parent) {
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08001961 klist_add_tail(&dev->p->knode_parent,
1962 &new_parent->p->klist_children);
Yinghai Lu0d358f22008-02-19 03:20:41 -08001963 set_dev_node(dev, dev_to_node(new_parent));
1964 }
1965
Rabin Vincentbdd40342012-04-23 09:16:36 +02001966 if (dev->class) {
1967 error = device_move_class_links(dev, old_parent, new_parent);
1968 if (error) {
1969 /* We ignore errors on cleanup since we're hosed anyway... */
1970 device_move_class_links(dev, new_parent, old_parent);
1971 if (!kobject_move(&dev->kobj, &old_parent->kobj)) {
1972 if (new_parent)
1973 klist_remove(&dev->p->knode_parent);
1974 dev->parent = old_parent;
1975 if (old_parent) {
1976 klist_add_tail(&dev->p->knode_parent,
1977 &old_parent->p->klist_children);
1978 set_dev_node(dev, dev_to_node(old_parent));
1979 }
Yinghai Lu0d358f22008-02-19 03:20:41 -08001980 }
Rabin Vincentbdd40342012-04-23 09:16:36 +02001981 cleanup_glue_dir(dev, new_parent_kobj);
1982 put_device(new_parent);
1983 goto out;
Cornelia Huck8a824722006-11-20 17:07:51 +01001984 }
Cornelia Huck8a824722006-11-20 17:07:51 +01001985 }
Cornelia Huckffa6a702009-03-04 12:44:00 +01001986 switch (dpm_order) {
1987 case DPM_ORDER_NONE:
1988 break;
1989 case DPM_ORDER_DEV_AFTER_PARENT:
1990 device_pm_move_after(dev, new_parent);
1991 break;
1992 case DPM_ORDER_PARENT_BEFORE_DEV:
1993 device_pm_move_before(new_parent, dev);
1994 break;
1995 case DPM_ORDER_DEV_LAST:
1996 device_pm_move_last(dev);
1997 break;
1998 }
Rabin Vincentbdd40342012-04-23 09:16:36 +02001999
Cornelia Huck8a824722006-11-20 17:07:51 +01002000 put_device(old_parent);
2001out:
Cornelia Huckffa6a702009-03-04 12:44:00 +01002002 device_pm_unlock();
Cornelia Huck8a824722006-11-20 17:07:51 +01002003 put_device(dev);
2004 return error;
2005}
Cornelia Huck8a824722006-11-20 17:07:51 +01002006EXPORT_SYMBOL_GPL(device_move);
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08002007
2008/**
2009 * device_shutdown - call ->shutdown() on each device to shutdown.
2010 */
2011void device_shutdown(void)
2012{
Hugh Daschbach62458382010-03-22 10:36:37 -07002013 struct device *dev;
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08002014
Hugh Daschbach62458382010-03-22 10:36:37 -07002015 spin_lock(&devices_kset->list_lock);
2016 /*
2017 * Walk the devices list backward, shutting down each in turn.
2018 * Beware that device unplug events may also start pulling
2019 * devices offline, even as the system is shutting down.
2020 */
2021 while (!list_empty(&devices_kset->list)) {
2022 dev = list_entry(devices_kset->list.prev, struct device,
2023 kobj.entry);
Ming Leid1c6c032012-06-22 18:01:40 +08002024
2025 /*
2026 * hold reference count of device's parent to
2027 * prevent it from being freed because parent's
2028 * lock is to be held
2029 */
2030 get_device(dev->parent);
Hugh Daschbach62458382010-03-22 10:36:37 -07002031 get_device(dev);
2032 /*
2033 * Make sure the device is off the kset list, in the
2034 * event that dev->*->shutdown() doesn't remove it.
2035 */
2036 list_del_init(&dev->kobj.entry);
2037 spin_unlock(&devices_kset->list_lock);
Alan Sternfe6b91f2011-12-06 23:24:52 +01002038
Ming Leid1c6c032012-06-22 18:01:40 +08002039 /* hold lock to avoid race with probe/release */
2040 if (dev->parent)
2041 device_lock(dev->parent);
2042 device_lock(dev);
2043
Alan Sternfe6b91f2011-12-06 23:24:52 +01002044 /* Don't allow any more runtime suspends */
2045 pm_runtime_get_noresume(dev);
2046 pm_runtime_barrier(dev);
Hugh Daschbach62458382010-03-22 10:36:37 -07002047
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08002048 if (dev->bus && dev->bus->shutdown) {
ShuoX Liu0246c4f2012-11-23 15:14:12 +08002049 if (initcall_debug)
2050 dev_info(dev, "shutdown\n");
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08002051 dev->bus->shutdown(dev);
2052 } else if (dev->driver && dev->driver->shutdown) {
ShuoX Liu0246c4f2012-11-23 15:14:12 +08002053 if (initcall_debug)
2054 dev_info(dev, "shutdown\n");
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08002055 dev->driver->shutdown(dev);
2056 }
Ming Leid1c6c032012-06-22 18:01:40 +08002057
2058 device_unlock(dev);
2059 if (dev->parent)
2060 device_unlock(dev->parent);
2061
Hugh Daschbach62458382010-03-22 10:36:37 -07002062 put_device(dev);
Ming Leid1c6c032012-06-22 18:01:40 +08002063 put_device(dev->parent);
Hugh Daschbach62458382010-03-22 10:36:37 -07002064
2065 spin_lock(&devices_kset->list_lock);
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08002066 }
Hugh Daschbach62458382010-03-22 10:36:37 -07002067 spin_unlock(&devices_kset->list_lock);
Shaohua Li401097e2009-05-12 13:37:57 -07002068 async_synchronize_full();
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08002069}
Joe Perches99bcf212010-06-27 01:02:34 +00002070
2071/*
2072 * Device logging functions
2073 */
2074
2075#ifdef CONFIG_PRINTK
Joe Perches666f3552012-09-12 20:14:11 -07002076static int
2077create_syslog_header(const struct device *dev, char *hdr, size_t hdrlen)
Joe Perches99bcf212010-06-27 01:02:34 +00002078{
Kay Sieversc4e00da2012-05-03 02:29:59 +02002079 const char *subsys;
Joe Perches798efc62012-09-12 20:11:29 -07002080 size_t pos = 0;
Joe Perches99bcf212010-06-27 01:02:34 +00002081
Kay Sieversc4e00da2012-05-03 02:29:59 +02002082 if (dev->class)
2083 subsys = dev->class->name;
2084 else if (dev->bus)
2085 subsys = dev->bus->name;
2086 else
Joe Perches798efc62012-09-12 20:11:29 -07002087 return 0;
Kay Sieversc4e00da2012-05-03 02:29:59 +02002088
Joe Perches798efc62012-09-12 20:11:29 -07002089 pos += snprintf(hdr + pos, hdrlen - pos, "SUBSYSTEM=%s", subsys);
Kay Sieversc4e00da2012-05-03 02:29:59 +02002090
2091 /*
2092 * Add device identifier DEVICE=:
2093 * b12:8 block dev_t
2094 * c127:3 char dev_t
2095 * n8 netdev ifindex
2096 * +sound:card0 subsystem:devname
2097 */
2098 if (MAJOR(dev->devt)) {
2099 char c;
2100
2101 if (strcmp(subsys, "block") == 0)
2102 c = 'b';
2103 else
2104 c = 'c';
Joe Perches798efc62012-09-12 20:11:29 -07002105 pos++;
2106 pos += snprintf(hdr + pos, hdrlen - pos,
2107 "DEVICE=%c%u:%u",
2108 c, MAJOR(dev->devt), MINOR(dev->devt));
Kay Sieversc4e00da2012-05-03 02:29:59 +02002109 } else if (strcmp(subsys, "net") == 0) {
2110 struct net_device *net = to_net_dev(dev);
2111
Joe Perches798efc62012-09-12 20:11:29 -07002112 pos++;
2113 pos += snprintf(hdr + pos, hdrlen - pos,
2114 "DEVICE=n%u", net->ifindex);
Kay Sieversc4e00da2012-05-03 02:29:59 +02002115 } else {
Joe Perches798efc62012-09-12 20:11:29 -07002116 pos++;
2117 pos += snprintf(hdr + pos, hdrlen - pos,
2118 "DEVICE=+%s:%s", subsys, dev_name(dev));
Kay Sieversc4e00da2012-05-03 02:29:59 +02002119 }
Jim Cromieaf7f2152012-07-19 13:46:21 -06002120
Joe Perches798efc62012-09-12 20:11:29 -07002121 return pos;
Joe Perches99bcf212010-06-27 01:02:34 +00002122}
Joe Perches798efc62012-09-12 20:11:29 -07002123EXPORT_SYMBOL(create_syslog_header);
2124
Joe Perches05e4e5b2012-09-12 20:13:37 -07002125int dev_vprintk_emit(int level, const struct device *dev,
2126 const char *fmt, va_list args)
2127{
2128 char hdr[128];
2129 size_t hdrlen;
2130
2131 hdrlen = create_syslog_header(dev, hdr, sizeof(hdr));
2132
2133 return vprintk_emit(0, level, hdrlen ? hdr : NULL, hdrlen, fmt, args);
2134}
2135EXPORT_SYMBOL(dev_vprintk_emit);
2136
2137int dev_printk_emit(int level, const struct device *dev, const char *fmt, ...)
2138{
2139 va_list args;
2140 int r;
2141
2142 va_start(args, fmt);
2143
2144 r = dev_vprintk_emit(level, dev, fmt, args);
2145
2146 va_end(args);
2147
2148 return r;
2149}
2150EXPORT_SYMBOL(dev_printk_emit);
2151
Joe Perches798efc62012-09-12 20:11:29 -07002152static int __dev_printk(const char *level, const struct device *dev,
2153 struct va_format *vaf)
2154{
Joe Perches798efc62012-09-12 20:11:29 -07002155 if (!dev)
2156 return printk("%s(NULL device *): %pV", level, vaf);
2157
Joe Perches666f3552012-09-12 20:14:11 -07002158 return dev_printk_emit(level[1] - '0', dev,
2159 "%s %s: %pV",
2160 dev_driver_string(dev), dev_name(dev), vaf);
Joe Perches798efc62012-09-12 20:11:29 -07002161}
Joe Perches99bcf212010-06-27 01:02:34 +00002162
2163int dev_printk(const char *level, const struct device *dev,
2164 const char *fmt, ...)
2165{
2166 struct va_format vaf;
2167 va_list args;
2168 int r;
2169
2170 va_start(args, fmt);
2171
2172 vaf.fmt = fmt;
2173 vaf.va = &args;
2174
2175 r = __dev_printk(level, dev, &vaf);
Joe Perches798efc62012-09-12 20:11:29 -07002176
Joe Perches99bcf212010-06-27 01:02:34 +00002177 va_end(args);
2178
2179 return r;
2180}
2181EXPORT_SYMBOL(dev_printk);
2182
2183#define define_dev_printk_level(func, kern_level) \
2184int func(const struct device *dev, const char *fmt, ...) \
2185{ \
2186 struct va_format vaf; \
2187 va_list args; \
2188 int r; \
2189 \
2190 va_start(args, fmt); \
2191 \
2192 vaf.fmt = fmt; \
2193 vaf.va = &args; \
2194 \
2195 r = __dev_printk(kern_level, dev, &vaf); \
Joe Perches798efc62012-09-12 20:11:29 -07002196 \
Joe Perches99bcf212010-06-27 01:02:34 +00002197 va_end(args); \
2198 \
2199 return r; \
2200} \
2201EXPORT_SYMBOL(func);
2202
2203define_dev_printk_level(dev_emerg, KERN_EMERG);
2204define_dev_printk_level(dev_alert, KERN_ALERT);
2205define_dev_printk_level(dev_crit, KERN_CRIT);
2206define_dev_printk_level(dev_err, KERN_ERR);
2207define_dev_printk_level(dev_warn, KERN_WARNING);
2208define_dev_printk_level(dev_notice, KERN_NOTICE);
2209define_dev_printk_level(_dev_info, KERN_INFO);
2210
2211#endif