blob: ac419a15fcd459b3ac94bfb7bb4c5da68916a1ae [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * drivers/base/core.c - core driver model code (device registration, etc)
3 *
4 * Copyright (c) 2002-3 Patrick Mochel
5 * Copyright (c) 2002-3 Open Source Development Labs
Greg Kroah-Hartman64bb5d22006-06-28 16:19:58 -07006 * Copyright (c) 2006 Greg Kroah-Hartman <gregkh@suse.de>
7 * Copyright (c) 2006 Novell, Inc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 *
9 * This file is released under the GPLv2
10 *
11 */
12
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/device.h>
14#include <linux/err.h>
15#include <linux/init.h>
16#include <linux/module.h>
17#include <linux/slab.h>
18#include <linux/string.h>
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -070019#include <linux/kdev_t.h>
Benjamin Herrenschmidt116af372006-10-25 13:44:59 +100020#include <linux/notifier.h>
Grant Likely07d57a32012-02-01 11:22:22 -070021#include <linux/of.h>
22#include <linux/of_device.h>
Kay Sieversda231fd2007-11-21 17:29:15 +010023#include <linux/genhd.h>
Andrew Morton815d2d52008-03-04 15:09:07 -080024#include <linux/kallsyms.h>
Dave Youngf75b1c62008-05-28 09:28:39 -070025#include <linux/mutex.h>
Shaohua Li401097e2009-05-12 13:37:57 -070026#include <linux/async.h>
Peter Chenaf8db152011-11-15 21:52:29 +010027#include <linux/pm_runtime.h>
Kay Sieversc4e00da2012-05-03 02:29:59 +020028#include <linux/netdevice.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
30#include "base.h"
31#include "power/power.h"
32
Andi Kleene52eec12010-09-08 16:54:17 +020033#ifdef CONFIG_SYSFS_DEPRECATED
34#ifdef CONFIG_SYSFS_DEPRECATED_V2
35long sysfs_deprecated = 1;
36#else
37long sysfs_deprecated = 0;
38#endif
39static __init int sysfs_deprecated_setup(char *arg)
40{
41 return strict_strtol(arg, 10, &sysfs_deprecated);
42}
43early_param("sysfs.deprecated", sysfs_deprecated_setup);
44#endif
45
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -080046int (*platform_notify)(struct device *dev) = NULL;
47int (*platform_notify_remove)(struct device *dev) = NULL;
Dan Williamse105b8b2008-04-21 10:51:07 -070048static struct kobject *dev_kobj;
49struct kobject *sysfs_dev_char_kobj;
50struct kobject *sysfs_dev_block_kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Rafael J. Wysocki5e33bc42013-08-28 21:41:01 +020052static DEFINE_MUTEX(device_hotplug_lock);
53
54void lock_device_hotplug(void)
55{
56 mutex_lock(&device_hotplug_lock);
57}
58
59void unlock_device_hotplug(void)
60{
61 mutex_unlock(&device_hotplug_lock);
62}
63
64int lock_device_hotplug_sysfs(void)
65{
66 if (mutex_trylock(&device_hotplug_lock))
67 return 0;
68
69 /* Avoid busy looping (5 ms of sleep should do). */
70 msleep(5);
71 return restart_syscall();
72}
73
Greg Kroah-Hartman4e886c22008-01-27 12:12:43 -080074#ifdef CONFIG_BLOCK
75static inline int device_is_not_partition(struct device *dev)
76{
77 return !(dev->type == &part_type);
78}
79#else
80static inline int device_is_not_partition(struct device *dev)
81{
82 return 1;
83}
84#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
Alan Stern3e956372006-06-16 17:10:48 -040086/**
87 * dev_driver_string - Return a device's driver name, if at all possible
88 * @dev: struct device to get the name of
89 *
90 * Will return the device's driver's name if it is bound to a device. If
yan9169c012012-04-20 21:08:45 +080091 * the device is not bound to a driver, it will return the name of the bus
Alan Stern3e956372006-06-16 17:10:48 -040092 * it is attached to. If it is not attached to a bus either, an empty
93 * string will be returned.
94 */
Jean Delvarebf9ca692008-07-30 12:29:21 -070095const char *dev_driver_string(const struct device *dev)
Alan Stern3e956372006-06-16 17:10:48 -040096{
Alan Stern35899722009-12-04 11:06:57 -050097 struct device_driver *drv;
98
99 /* dev->driver can change to NULL underneath us because of unbinding,
100 * so be careful about accessing it. dev->bus and dev->class should
101 * never change once they are set, so they don't need special care.
102 */
103 drv = ACCESS_ONCE(dev->driver);
104 return drv ? drv->name :
Jean Delvarea456b702007-03-09 16:33:10 +0100105 (dev->bus ? dev->bus->name :
106 (dev->class ? dev->class->name : ""));
Alan Stern3e956372006-06-16 17:10:48 -0400107}
Matthew Wilcox310a9222006-09-23 23:35:04 -0600108EXPORT_SYMBOL(dev_driver_string);
Alan Stern3e956372006-06-16 17:10:48 -0400109
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110#define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr)
111
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800112static ssize_t dev_attr_show(struct kobject *kobj, struct attribute *attr,
113 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800115 struct device_attribute *dev_attr = to_dev_attr(attr);
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200116 struct device *dev = kobj_to_dev(kobj);
Dmitry Torokhov4a0c20b2005-04-29 01:23:47 -0500117 ssize_t ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
119 if (dev_attr->show)
Yani Ioannou54b6f352005-05-17 06:39:34 -0400120 ret = dev_attr->show(dev, dev_attr, buf);
Andrew Morton815d2d52008-03-04 15:09:07 -0800121 if (ret >= (ssize_t)PAGE_SIZE) {
Greg Kroah-Hartman53a9c872013-01-17 13:10:23 -0800122 print_symbol("dev_attr_show: %s returned bad count\n",
123 (unsigned long)dev_attr->show);
Andrew Morton815d2d52008-03-04 15:09:07 -0800124 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 return ret;
126}
127
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800128static ssize_t dev_attr_store(struct kobject *kobj, struct attribute *attr,
129 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800131 struct device_attribute *dev_attr = to_dev_attr(attr);
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200132 struct device *dev = kobj_to_dev(kobj);
Dmitry Torokhov4a0c20b2005-04-29 01:23:47 -0500133 ssize_t ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
135 if (dev_attr->store)
Yani Ioannou54b6f352005-05-17 06:39:34 -0400136 ret = dev_attr->store(dev, dev_attr, buf, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 return ret;
138}
139
Emese Revfy52cf25d2010-01-19 02:58:23 +0100140static const struct sysfs_ops dev_sysfs_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 .show = dev_attr_show,
142 .store = dev_attr_store,
143};
144
Kay Sieversca22e562011-12-14 14:29:38 -0800145#define to_ext_attr(x) container_of(x, struct dev_ext_attribute, attr)
146
147ssize_t device_store_ulong(struct device *dev,
148 struct device_attribute *attr,
149 const char *buf, size_t size)
150{
151 struct dev_ext_attribute *ea = to_ext_attr(attr);
152 char *end;
153 unsigned long new = simple_strtoul(buf, &end, 0);
154 if (end == buf)
155 return -EINVAL;
156 *(unsigned long *)(ea->var) = new;
157 /* Always return full write size even if we didn't consume all */
158 return size;
159}
160EXPORT_SYMBOL_GPL(device_store_ulong);
161
162ssize_t device_show_ulong(struct device *dev,
163 struct device_attribute *attr,
164 char *buf)
165{
166 struct dev_ext_attribute *ea = to_ext_attr(attr);
167 return snprintf(buf, PAGE_SIZE, "%lx\n", *(unsigned long *)(ea->var));
168}
169EXPORT_SYMBOL_GPL(device_show_ulong);
170
171ssize_t device_store_int(struct device *dev,
172 struct device_attribute *attr,
173 const char *buf, size_t size)
174{
175 struct dev_ext_attribute *ea = to_ext_attr(attr);
176 char *end;
177 long new = simple_strtol(buf, &end, 0);
178 if (end == buf || new > INT_MAX || new < INT_MIN)
179 return -EINVAL;
180 *(int *)(ea->var) = new;
181 /* Always return full write size even if we didn't consume all */
182 return size;
183}
184EXPORT_SYMBOL_GPL(device_store_int);
185
186ssize_t device_show_int(struct device *dev,
187 struct device_attribute *attr,
188 char *buf)
189{
190 struct dev_ext_attribute *ea = to_ext_attr(attr);
191
192 return snprintf(buf, PAGE_SIZE, "%d\n", *(int *)(ea->var));
193}
194EXPORT_SYMBOL_GPL(device_show_int);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
Borislav Petkov91872392012-10-09 19:52:05 +0200196ssize_t device_store_bool(struct device *dev, struct device_attribute *attr,
197 const char *buf, size_t size)
198{
199 struct dev_ext_attribute *ea = to_ext_attr(attr);
200
201 if (strtobool(buf, ea->var) < 0)
202 return -EINVAL;
203
204 return size;
205}
206EXPORT_SYMBOL_GPL(device_store_bool);
207
208ssize_t device_show_bool(struct device *dev, struct device_attribute *attr,
209 char *buf)
210{
211 struct dev_ext_attribute *ea = to_ext_attr(attr);
212
213 return snprintf(buf, PAGE_SIZE, "%d\n", *(bool *)(ea->var));
214}
215EXPORT_SYMBOL_GPL(device_show_bool);
216
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217/**
Robert P. J. Dayf8878dc2013-06-01 20:17:34 -0400218 * device_release - free device structure.
219 * @kobj: device's kobject.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 *
Robert P. J. Dayf8878dc2013-06-01 20:17:34 -0400221 * This is called once the reference count for the object
222 * reaches 0. We forward the call to the device's release
223 * method, which should handle actually freeing the structure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800225static void device_release(struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200227 struct device *dev = kobj_to_dev(kobj);
Greg Kroah-Hartmanfb069a52008-12-16 12:23:36 -0800228 struct device_private *p = dev->p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
Ming Leia525a3d2012-07-25 01:42:29 +0800230 /*
231 * Some platform devices are driven without driver attached
232 * and managed resources may have been acquired. Make sure
233 * all resources are released.
234 *
235 * Drivers still can add resources into device after device
236 * is deleted but alive, so release devres here to avoid
237 * possible memory leak.
238 */
239 devres_release_all(dev);
240
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 if (dev->release)
242 dev->release(dev);
Kay Sieversf9f852d2006-10-07 21:54:55 +0200243 else if (dev->type && dev->type->release)
244 dev->type->release(dev);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700245 else if (dev->class && dev->class->dev_release)
246 dev->class->dev_release(dev);
Arjan van de Venf810a5c2008-07-25 19:45:39 -0700247 else
248 WARN(1, KERN_ERR "Device '%s' does not have a release() "
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800249 "function, it is broken and must be fixed.\n",
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100250 dev_name(dev));
Greg Kroah-Hartmanfb069a52008-12-16 12:23:36 -0800251 kfree(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252}
253
Eric W. Biedermanbc451f22010-03-30 11:31:25 -0700254static const void *device_namespace(struct kobject *kobj)
255{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200256 struct device *dev = kobj_to_dev(kobj);
Eric W. Biedermanbc451f22010-03-30 11:31:25 -0700257 const void *ns = NULL;
258
259 if (dev->class && dev->class->ns_type)
260 ns = dev->class->namespace(dev);
261
262 return ns;
263}
264
Greg Kroah-Hartman8f4afc42007-10-11 10:47:49 -0600265static struct kobj_type device_ktype = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 .release = device_release,
267 .sysfs_ops = &dev_sysfs_ops,
Eric W. Biedermanbc451f22010-03-30 11:31:25 -0700268 .namespace = device_namespace,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269};
270
271
Kay Sievers312c0042005-11-16 09:00:00 +0100272static int dev_uevent_filter(struct kset *kset, struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273{
274 struct kobj_type *ktype = get_ktype(kobj);
275
Greg Kroah-Hartman8f4afc42007-10-11 10:47:49 -0600276 if (ktype == &device_ktype) {
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200277 struct device *dev = kobj_to_dev(kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 if (dev->bus)
279 return 1;
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700280 if (dev->class)
281 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 }
283 return 0;
284}
285
Kay Sievers312c0042005-11-16 09:00:00 +0100286static const char *dev_uevent_name(struct kset *kset, struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200288 struct device *dev = kobj_to_dev(kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700290 if (dev->bus)
291 return dev->bus->name;
292 if (dev->class)
293 return dev->class->name;
294 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295}
296
Kay Sievers7eff2e72007-08-14 15:15:12 +0200297static int dev_uevent(struct kset *kset, struct kobject *kobj,
298 struct kobj_uevent_env *env)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200300 struct device *dev = kobj_to_dev(kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 int retval = 0;
302
Kay Sievers6fcf53a2009-04-30 15:23:42 +0200303 /* add device node properties if present */
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700304 if (MAJOR(dev->devt)) {
Kay Sievers6fcf53a2009-04-30 15:23:42 +0200305 const char *tmp;
306 const char *name;
Al Viro2c9ede52011-07-23 20:24:48 -0400307 umode_t mode = 0;
Greg Kroah-Hartman4e4098a2013-04-11 11:43:29 -0700308 kuid_t uid = GLOBAL_ROOT_UID;
309 kgid_t gid = GLOBAL_ROOT_GID;
Kay Sievers6fcf53a2009-04-30 15:23:42 +0200310
Kay Sievers7eff2e72007-08-14 15:15:12 +0200311 add_uevent_var(env, "MAJOR=%u", MAJOR(dev->devt));
312 add_uevent_var(env, "MINOR=%u", MINOR(dev->devt));
Kay Sievers3c2670e2013-04-06 09:56:00 -0700313 name = device_get_devnode(dev, &mode, &uid, &gid, &tmp);
Kay Sievers6fcf53a2009-04-30 15:23:42 +0200314 if (name) {
315 add_uevent_var(env, "DEVNAME=%s", name);
Kay Sieverse454cea2009-09-18 23:01:12 +0200316 if (mode)
317 add_uevent_var(env, "DEVMODE=%#o", mode & 0777);
Greg Kroah-Hartman4e4098a2013-04-11 11:43:29 -0700318 if (!uid_eq(uid, GLOBAL_ROOT_UID))
319 add_uevent_var(env, "DEVUID=%u", from_kuid(&init_user_ns, uid));
320 if (!gid_eq(gid, GLOBAL_ROOT_GID))
321 add_uevent_var(env, "DEVGID=%u", from_kgid(&init_user_ns, gid));
Kay Sievers3c2670e2013-04-06 09:56:00 -0700322 kfree(tmp);
Kay Sievers6fcf53a2009-04-30 15:23:42 +0200323 }
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700324 }
325
Kay Sievers414264f2007-03-12 21:08:57 +0100326 if (dev->type && dev->type->name)
Kay Sievers7eff2e72007-08-14 15:15:12 +0200327 add_uevent_var(env, "DEVTYPE=%s", dev->type->name);
Kay Sievers414264f2007-03-12 21:08:57 +0100328
Kay Sievers239378f2006-10-07 21:54:55 +0200329 if (dev->driver)
Kay Sievers7eff2e72007-08-14 15:15:12 +0200330 add_uevent_var(env, "DRIVER=%s", dev->driver->name);
Kay Sievers239378f2006-10-07 21:54:55 +0200331
Grant Likely07d57a32012-02-01 11:22:22 -0700332 /* Add common DT information about the device */
333 of_device_uevent(dev, env);
334
Kay Sievers7eff2e72007-08-14 15:15:12 +0200335 /* have the bus specific function add its stuff */
Kay Sievers312c0042005-11-16 09:00:00 +0100336 if (dev->bus && dev->bus->uevent) {
Kay Sievers7eff2e72007-08-14 15:15:12 +0200337 retval = dev->bus->uevent(dev, env);
Kay Sieversf9f852d2006-10-07 21:54:55 +0200338 if (retval)
Greg Kroah-Hartman7dc72b22007-11-28 23:49:41 -0800339 pr_debug("device: '%s': %s: bus uevent() returned %d\n",
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100340 dev_name(dev), __func__, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 }
342
Kay Sievers7eff2e72007-08-14 15:15:12 +0200343 /* have the class specific function add its stuff */
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700344 if (dev->class && dev->class->dev_uevent) {
Kay Sievers7eff2e72007-08-14 15:15:12 +0200345 retval = dev->class->dev_uevent(dev, env);
Kay Sieversf9f852d2006-10-07 21:54:55 +0200346 if (retval)
Greg Kroah-Hartman7dc72b22007-11-28 23:49:41 -0800347 pr_debug("device: '%s': %s: class uevent() "
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100348 "returned %d\n", dev_name(dev),
Harvey Harrison2b3a3022008-03-04 16:41:05 -0800349 __func__, retval);
Kay Sieversf9f852d2006-10-07 21:54:55 +0200350 }
351
Stefan Weileef35c22010-08-06 21:11:15 +0200352 /* have the device type specific function add its stuff */
Kay Sieversf9f852d2006-10-07 21:54:55 +0200353 if (dev->type && dev->type->uevent) {
Kay Sievers7eff2e72007-08-14 15:15:12 +0200354 retval = dev->type->uevent(dev, env);
Kay Sieversf9f852d2006-10-07 21:54:55 +0200355 if (retval)
Greg Kroah-Hartman7dc72b22007-11-28 23:49:41 -0800356 pr_debug("device: '%s': %s: dev_type uevent() "
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100357 "returned %d\n", dev_name(dev),
Harvey Harrison2b3a3022008-03-04 16:41:05 -0800358 __func__, retval);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700359 }
360
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 return retval;
362}
363
Emese Revfy9cd43612009-12-31 14:52:51 +0100364static const struct kset_uevent_ops device_uevent_ops = {
Kay Sievers312c0042005-11-16 09:00:00 +0100365 .filter = dev_uevent_filter,
366 .name = dev_uevent_name,
367 .uevent = dev_uevent,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368};
369
Kay Sievers16574dc2007-04-06 01:40:38 +0200370static ssize_t show_uevent(struct device *dev, struct device_attribute *attr,
371 char *buf)
372{
373 struct kobject *top_kobj;
374 struct kset *kset;
Kay Sievers7eff2e72007-08-14 15:15:12 +0200375 struct kobj_uevent_env *env = NULL;
Kay Sievers16574dc2007-04-06 01:40:38 +0200376 int i;
377 size_t count = 0;
378 int retval;
379
380 /* search the kset, the device belongs to */
381 top_kobj = &dev->kobj;
Kay Sievers5c5daf62007-08-12 20:43:55 +0200382 while (!top_kobj->kset && top_kobj->parent)
383 top_kobj = top_kobj->parent;
Kay Sievers16574dc2007-04-06 01:40:38 +0200384 if (!top_kobj->kset)
385 goto out;
Kay Sievers5c5daf62007-08-12 20:43:55 +0200386
Kay Sievers16574dc2007-04-06 01:40:38 +0200387 kset = top_kobj->kset;
388 if (!kset->uevent_ops || !kset->uevent_ops->uevent)
389 goto out;
390
391 /* respect filter */
392 if (kset->uevent_ops && kset->uevent_ops->filter)
393 if (!kset->uevent_ops->filter(kset, &dev->kobj))
394 goto out;
395
Kay Sievers7eff2e72007-08-14 15:15:12 +0200396 env = kzalloc(sizeof(struct kobj_uevent_env), GFP_KERNEL);
397 if (!env)
Greg Kroah-Hartmanc7308c82007-05-02 14:14:11 +0200398 return -ENOMEM;
399
Kay Sievers16574dc2007-04-06 01:40:38 +0200400 /* let the kset specific function add its keys */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200401 retval = kset->uevent_ops->uevent(kset, &dev->kobj, env);
Kay Sievers16574dc2007-04-06 01:40:38 +0200402 if (retval)
403 goto out;
404
405 /* copy keys to file */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200406 for (i = 0; i < env->envp_idx; i++)
407 count += sprintf(&buf[count], "%s\n", env->envp[i]);
Kay Sievers16574dc2007-04-06 01:40:38 +0200408out:
Kay Sievers7eff2e72007-08-14 15:15:12 +0200409 kfree(env);
Kay Sievers16574dc2007-04-06 01:40:38 +0200410 return count;
411}
412
Kay Sieversa7fd6702005-10-01 14:49:43 +0200413static ssize_t store_uevent(struct device *dev, struct device_attribute *attr,
414 const char *buf, size_t count)
415{
Kay Sievers60a96a52007-07-08 22:29:26 +0200416 enum kobject_action action;
417
Kay Sievers3f5468c2010-01-14 22:54:37 +0100418 if (kobject_action_type(buf, count, &action) == 0)
Kay Sievers60a96a52007-07-08 22:29:26 +0200419 kobject_uevent(&dev->kobj, action);
Kay Sievers3f5468c2010-01-14 22:54:37 +0100420 else
421 dev_err(dev, "uevent: unknown action-string\n");
Kay Sieversa7fd6702005-10-01 14:49:43 +0200422 return count;
423}
424
Tejun Heoad6a1e12007-06-14 03:45:17 +0900425static struct device_attribute uevent_attr =
426 __ATTR(uevent, S_IRUGO | S_IWUSR, show_uevent, store_uevent);
427
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +0200428static ssize_t show_online(struct device *dev, struct device_attribute *attr,
429 char *buf)
430{
431 bool val;
432
Rafael J. Wysocki5e33bc42013-08-28 21:41:01 +0200433 device_lock(dev);
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +0200434 val = !dev->offline;
Rafael J. Wysocki5e33bc42013-08-28 21:41:01 +0200435 device_unlock(dev);
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +0200436 return sprintf(buf, "%u\n", val);
437}
438
439static ssize_t store_online(struct device *dev, struct device_attribute *attr,
440 const char *buf, size_t count)
441{
442 bool val;
443 int ret;
444
445 ret = strtobool(buf, &val);
446 if (ret < 0)
447 return ret;
448
Rafael J. Wysocki5e33bc42013-08-28 21:41:01 +0200449 ret = lock_device_hotplug_sysfs();
450 if (ret)
451 return ret;
452
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +0200453 ret = val ? device_online(dev) : device_offline(dev);
454 unlock_device_hotplug();
455 return ret < 0 ? ret : count;
456}
457
458static struct device_attribute online_attr =
459 __ATTR(online, S_IRUGO | S_IWUSR, show_online, store_online);
460
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500461static int device_add_attributes(struct device *dev,
462 struct device_attribute *attrs)
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -0700463{
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -0700464 int error = 0;
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500465 int i;
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -0700466
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500467 if (attrs) {
468 for (i = 0; attr_name(attrs[i]); i++) {
469 error = device_create_file(dev, &attrs[i]);
470 if (error)
471 break;
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -0700472 }
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500473 if (error)
474 while (--i >= 0)
475 device_remove_file(dev, &attrs[i]);
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -0700476 }
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -0700477 return error;
478}
479
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500480static void device_remove_attributes(struct device *dev,
481 struct device_attribute *attrs)
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -0700482{
483 int i;
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500484
485 if (attrs)
486 for (i = 0; attr_name(attrs[i]); i++)
487 device_remove_file(dev, &attrs[i]);
488}
489
Stefan Achatzc97415a2010-11-26 19:57:29 +0000490static int device_add_bin_attributes(struct device *dev,
491 struct bin_attribute *attrs)
492{
493 int error = 0;
494 int i;
495
496 if (attrs) {
497 for (i = 0; attr_name(attrs[i]); i++) {
498 error = device_create_bin_file(dev, &attrs[i]);
499 if (error)
500 break;
501 }
502 if (error)
503 while (--i >= 0)
504 device_remove_bin_file(dev, &attrs[i]);
505 }
506 return error;
507}
508
509static void device_remove_bin_attributes(struct device *dev,
510 struct bin_attribute *attrs)
511{
512 int i;
513
514 if (attrs)
515 for (i = 0; attr_name(attrs[i]); i++)
516 device_remove_bin_file(dev, &attrs[i]);
517}
518
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500519static int device_add_groups(struct device *dev,
David Brownella4dbd672009-06-24 10:06:31 -0700520 const struct attribute_group **groups)
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500521{
522 int error = 0;
523 int i;
524
525 if (groups) {
526 for (i = 0; groups[i]; i++) {
527 error = sysfs_create_group(&dev->kobj, groups[i]);
528 if (error) {
529 while (--i >= 0)
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800530 sysfs_remove_group(&dev->kobj,
531 groups[i]);
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500532 break;
533 }
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -0700534 }
535 }
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500536 return error;
537}
538
539static void device_remove_groups(struct device *dev,
David Brownella4dbd672009-06-24 10:06:31 -0700540 const struct attribute_group **groups)
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500541{
542 int i;
543
544 if (groups)
545 for (i = 0; groups[i]; i++)
546 sysfs_remove_group(&dev->kobj, groups[i]);
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -0700547}
548
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700549static int device_add_attrs(struct device *dev)
550{
551 struct class *class = dev->class;
Stephen Hemmingeraed65af2011-03-28 09:12:52 -0700552 const struct device_type *type = dev->type;
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500553 int error;
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700554
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500555 if (class) {
Greg Kroah-Hartmand05a6f92013-07-14 16:05:58 -0700556 error = device_add_groups(dev, class->dev_groups);
Kay Sieversf9f852d2006-10-07 21:54:55 +0200557 if (error)
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500558 return error;
Greg Kroah-Hartmand05a6f92013-07-14 16:05:58 -0700559 error = device_add_attributes(dev, class->dev_attrs);
560 if (error)
561 goto err_remove_class_groups;
Stefan Achatzc97415a2010-11-26 19:57:29 +0000562 error = device_add_bin_attributes(dev, class->dev_bin_attrs);
563 if (error)
564 goto err_remove_class_attrs;
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700565 }
Kay Sieversf9f852d2006-10-07 21:54:55 +0200566
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500567 if (type) {
568 error = device_add_groups(dev, type->groups);
Kay Sieversf9f852d2006-10-07 21:54:55 +0200569 if (error)
Stefan Achatzc97415a2010-11-26 19:57:29 +0000570 goto err_remove_class_bin_attrs;
Kay Sieversf9f852d2006-10-07 21:54:55 +0200571 }
572
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500573 error = device_add_groups(dev, dev->groups);
574 if (error)
575 goto err_remove_type_groups;
576
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +0200577 if (device_supports_offline(dev) && !dev->offline_disabled) {
578 error = device_create_file(dev, &online_attr);
579 if (error)
580 goto err_remove_type_groups;
581 }
582
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500583 return 0;
584
585 err_remove_type_groups:
586 if (type)
587 device_remove_groups(dev, type->groups);
Stefan Achatzc97415a2010-11-26 19:57:29 +0000588 err_remove_class_bin_attrs:
589 if (class)
590 device_remove_bin_attributes(dev, class->dev_bin_attrs);
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500591 err_remove_class_attrs:
592 if (class)
593 device_remove_attributes(dev, class->dev_attrs);
Greg Kroah-Hartmand05a6f92013-07-14 16:05:58 -0700594 err_remove_class_groups:
595 if (class)
596 device_remove_groups(dev, class->dev_groups);
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500597
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700598 return error;
599}
600
601static void device_remove_attrs(struct device *dev)
602{
603 struct class *class = dev->class;
Stephen Hemmingeraed65af2011-03-28 09:12:52 -0700604 const struct device_type *type = dev->type;
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700605
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +0200606 device_remove_file(dev, &online_attr);
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500607 device_remove_groups(dev, dev->groups);
Kay Sieversf9f852d2006-10-07 21:54:55 +0200608
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500609 if (type)
610 device_remove_groups(dev, type->groups);
611
Stefan Achatzc97415a2010-11-26 19:57:29 +0000612 if (class) {
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500613 device_remove_attributes(dev, class->dev_attrs);
Stefan Achatzc97415a2010-11-26 19:57:29 +0000614 device_remove_bin_attributes(dev, class->dev_bin_attrs);
Greg Kroah-Hartmand05a6f92013-07-14 16:05:58 -0700615 device_remove_groups(dev, class->dev_groups);
Stefan Achatzc97415a2010-11-26 19:57:29 +0000616 }
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700617}
618
619
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700620static ssize_t show_dev(struct device *dev, struct device_attribute *attr,
621 char *buf)
622{
623 return print_dev_t(buf, dev->devt);
624}
625
Tejun Heoad6a1e12007-06-14 03:45:17 +0900626static struct device_attribute devt_attr =
627 __ATTR(dev, S_IRUGO, show_dev, NULL);
628
Kay Sieversca22e562011-12-14 14:29:38 -0800629/* /sys/devices/ */
Greg Kroah-Hartman881c6cfd2007-11-01 09:29:06 -0600630struct kset *devices_kset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800633 * device_create_file - create sysfs attribute file for device.
634 * @dev: device.
635 * @attr: device attribute descriptor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 */
Phil Carmody26579ab2009-12-18 15:34:19 +0200637int device_create_file(struct device *dev,
638 const struct device_attribute *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639{
640 int error = 0;
Felipe Balbi8f46baa2013-02-20 10:31:42 +0200641
642 if (dev) {
643 WARN(((attr->attr.mode & S_IWUGO) && !attr->store),
dyoung@redhat.com97521972013-05-16 14:31:30 +0800644 "Attribute %s: write permission without 'store'\n",
645 attr->attr.name);
Felipe Balbi8f46baa2013-02-20 10:31:42 +0200646 WARN(((attr->attr.mode & S_IRUGO) && !attr->show),
dyoung@redhat.com97521972013-05-16 14:31:30 +0800647 "Attribute %s: read permission without 'show'\n",
648 attr->attr.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 error = sysfs_create_file(&dev->kobj, &attr->attr);
Felipe Balbi8f46baa2013-02-20 10:31:42 +0200650 }
651
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 return error;
653}
654
655/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800656 * device_remove_file - remove sysfs attribute file.
657 * @dev: device.
658 * @attr: device attribute descriptor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 */
Phil Carmody26579ab2009-12-18 15:34:19 +0200660void device_remove_file(struct device *dev,
661 const struct device_attribute *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662{
Cornelia Huck0c98b192008-01-31 10:39:38 +0100663 if (dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 sysfs_remove_file(&dev->kobj, &attr->attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665}
666
Greg Kroah-Hartman2589f1882006-09-19 09:39:19 -0700667/**
668 * device_create_bin_file - create sysfs binary attribute file for device.
669 * @dev: device.
670 * @attr: device binary attribute descriptor.
671 */
Phil Carmody66ecb922009-12-18 15:34:20 +0200672int device_create_bin_file(struct device *dev,
673 const struct bin_attribute *attr)
Greg Kroah-Hartman2589f1882006-09-19 09:39:19 -0700674{
675 int error = -EINVAL;
676 if (dev)
677 error = sysfs_create_bin_file(&dev->kobj, attr);
678 return error;
679}
680EXPORT_SYMBOL_GPL(device_create_bin_file);
681
682/**
683 * device_remove_bin_file - remove sysfs binary attribute file
684 * @dev: device.
685 * @attr: device binary attribute descriptor.
686 */
Phil Carmody66ecb922009-12-18 15:34:20 +0200687void device_remove_bin_file(struct device *dev,
688 const struct bin_attribute *attr)
Greg Kroah-Hartman2589f1882006-09-19 09:39:19 -0700689{
690 if (dev)
691 sysfs_remove_bin_file(&dev->kobj, attr);
692}
693EXPORT_SYMBOL_GPL(device_remove_bin_file);
694
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400695/**
Alan Stern523ded72007-04-26 00:12:04 -0700696 * device_schedule_callback_owner - helper to schedule a callback for a device
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400697 * @dev: device.
698 * @func: callback function to invoke later.
Alan Stern523ded72007-04-26 00:12:04 -0700699 * @owner: module owning the callback routine
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400700 *
701 * Attribute methods must not unregister themselves or their parent device
702 * (which would amount to the same thing). Attempts to do so will deadlock,
703 * since unregistration is mutually exclusive with driver callbacks.
704 *
705 * Instead methods can call this routine, which will attempt to allocate
706 * and schedule a workqueue request to call back @func with @dev as its
707 * argument in the workqueue's process context. @dev will be pinned until
708 * @func returns.
709 *
Alan Stern523ded72007-04-26 00:12:04 -0700710 * This routine is usually called via the inline device_schedule_callback(),
711 * which automatically sets @owner to THIS_MODULE.
712 *
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400713 * Returns 0 if the request was submitted, -ENOMEM if storage could not
Alan Stern523ded72007-04-26 00:12:04 -0700714 * be allocated, -ENODEV if a reference to @owner isn't available.
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400715 *
716 * NOTE: This routine won't work if CONFIG_SYSFS isn't set! It uses an
717 * underlying sysfs routine (since it is intended for use by attribute
718 * methods), and if sysfs isn't available you'll get nothing but -ENOSYS.
719 */
Alan Stern523ded72007-04-26 00:12:04 -0700720int device_schedule_callback_owner(struct device *dev,
721 void (*func)(struct device *), struct module *owner)
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400722{
723 return sysfs_schedule_callback(&dev->kobj,
Alan Stern523ded72007-04-26 00:12:04 -0700724 (void (*)(void *)) func, dev, owner);
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400725}
Alan Stern523ded72007-04-26 00:12:04 -0700726EXPORT_SYMBOL_GPL(device_schedule_callback_owner);
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400727
James Bottomley34bb61f2005-09-06 16:56:51 -0700728static void klist_children_get(struct klist_node *n)
729{
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -0800730 struct device_private *p = to_device_private_parent(n);
731 struct device *dev = p->device;
James Bottomley34bb61f2005-09-06 16:56:51 -0700732
733 get_device(dev);
734}
735
736static void klist_children_put(struct klist_node *n)
737{
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -0800738 struct device_private *p = to_device_private_parent(n);
739 struct device *dev = p->device;
James Bottomley34bb61f2005-09-06 16:56:51 -0700740
741 put_device(dev);
742}
743
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800745 * device_initialize - init device structure.
746 * @dev: device.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 *
Cornelia Huck57394112008-09-03 18:26:40 +0200748 * This prepares the device for use by other layers by initializing
749 * its fields.
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800750 * It is the first half of device_register(), if called by
Cornelia Huck57394112008-09-03 18:26:40 +0200751 * that function, though it can also be called separately, so one
752 * may use @dev's fields. In particular, get_device()/put_device()
753 * may be used for reference counting of @dev after calling this
754 * function.
755 *
Alan Sternb10d5ef2012-01-17 11:39:00 -0500756 * All fields in @dev must be initialized by the caller to 0, except
757 * for those explicitly set to some other value. The simplest
758 * approach is to use kzalloc() to allocate the structure containing
759 * @dev.
760 *
Cornelia Huck57394112008-09-03 18:26:40 +0200761 * NOTE: Use put_device() to give up your reference instead of freeing
762 * @dev directly once you have called this function.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764void device_initialize(struct device *dev)
765{
Greg Kroah-Hartman881c6cfd2007-11-01 09:29:06 -0600766 dev->kobj.kset = devices_kset;
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -0700767 kobject_init(&dev->kobj, &device_ktype);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 INIT_LIST_HEAD(&dev->dma_pools);
Thomas Gleixner31427882010-01-29 20:39:02 +0000769 mutex_init(&dev->mutex);
Peter Zijlstra1704f472010-03-19 01:37:42 +0100770 lockdep_set_novalidate_class(&dev->mutex);
Tejun Heo9ac78492007-01-20 16:00:26 +0900771 spin_lock_init(&dev->devres_lock);
772 INIT_LIST_HEAD(&dev->devres_head);
Alan Stern3b98aea2008-08-07 13:06:12 -0400773 device_pm_init(dev);
Christoph Hellwig87348132006-12-06 20:32:33 -0800774 set_dev_node(dev, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775}
776
Tejun Heod73ce002013-03-12 11:30:05 -0700777struct kobject *virtual_device_parent(struct device *dev)
Greg Kroah-Hartmanf0ee61a2006-10-23 10:40:54 -0700778{
Kay Sievers86406242007-03-14 03:25:56 +0100779 static struct kobject *virtual_dir = NULL;
Greg Kroah-Hartmanf0ee61a2006-10-23 10:40:54 -0700780
Kay Sievers86406242007-03-14 03:25:56 +0100781 if (!virtual_dir)
Greg Kroah-Hartman4ff6abf2007-11-05 22:24:43 -0800782 virtual_dir = kobject_create_and_add("virtual",
Greg Kroah-Hartman881c6cfd2007-11-01 09:29:06 -0600783 &devices_kset->kobj);
Greg Kroah-Hartmanf0ee61a2006-10-23 10:40:54 -0700784
Kay Sievers86406242007-03-14 03:25:56 +0100785 return virtual_dir;
Greg Kroah-Hartmanf0ee61a2006-10-23 10:40:54 -0700786}
787
Eric W. Biedermanbc451f22010-03-30 11:31:25 -0700788struct class_dir {
789 struct kobject kobj;
790 struct class *class;
791};
792
793#define to_class_dir(obj) container_of(obj, struct class_dir, kobj)
794
795static void class_dir_release(struct kobject *kobj)
796{
797 struct class_dir *dir = to_class_dir(kobj);
798 kfree(dir);
799}
800
801static const
802struct kobj_ns_type_operations *class_dir_child_ns_type(struct kobject *kobj)
803{
804 struct class_dir *dir = to_class_dir(kobj);
805 return dir->class->ns_type;
806}
807
808static struct kobj_type class_dir_ktype = {
809 .release = class_dir_release,
810 .sysfs_ops = &kobj_sysfs_ops,
811 .child_ns_type = class_dir_child_ns_type
812};
813
814static struct kobject *
815class_dir_create_and_add(struct class *class, struct kobject *parent_kobj)
816{
817 struct class_dir *dir;
818 int retval;
819
820 dir = kzalloc(sizeof(*dir), GFP_KERNEL);
821 if (!dir)
822 return NULL;
823
824 dir->class = class;
825 kobject_init(&dir->kobj, &class_dir_ktype);
826
Kay Sievers6b6e39a2010-11-15 23:13:18 +0100827 dir->kobj.kset = &class->p->glue_dirs;
Eric W. Biedermanbc451f22010-03-30 11:31:25 -0700828
829 retval = kobject_add(&dir->kobj, parent_kobj, "%s", class->name);
830 if (retval < 0) {
831 kobject_put(&dir->kobj);
832 return NULL;
833 }
834 return &dir->kobj;
835}
836
837
Kay Sieversda231fd2007-11-21 17:29:15 +0100838static struct kobject *get_device_parent(struct device *dev,
839 struct device *parent)
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +0100840{
Kay Sievers86406242007-03-14 03:25:56 +0100841 if (dev->class) {
Tejun Heo77d3d7c2010-02-05 17:57:02 +0900842 static DEFINE_MUTEX(gdp_mutex);
Kay Sievers86406242007-03-14 03:25:56 +0100843 struct kobject *kobj = NULL;
844 struct kobject *parent_kobj;
845 struct kobject *k;
846
Randy Dunlapead454f2010-09-24 14:36:49 -0700847#ifdef CONFIG_BLOCK
Kay Sievers39aba962010-09-04 22:33:14 -0700848 /* block disks show up in /sys/block */
Andi Kleene52eec12010-09-08 16:54:17 +0200849 if (sysfs_deprecated && dev->class == &block_class) {
Kay Sievers39aba962010-09-04 22:33:14 -0700850 if (parent && parent->class == &block_class)
851 return &parent->kobj;
Kay Sievers6b6e39a2010-11-15 23:13:18 +0100852 return &block_class.p->subsys.kobj;
Kay Sievers39aba962010-09-04 22:33:14 -0700853 }
Randy Dunlapead454f2010-09-24 14:36:49 -0700854#endif
Andi Kleene52eec12010-09-08 16:54:17 +0200855
Kay Sievers86406242007-03-14 03:25:56 +0100856 /*
857 * If we have no parent, we live in "virtual".
Kay Sievers0f4dafc2007-12-19 01:40:42 +0100858 * Class-devices with a non class-device as parent, live
859 * in a "glue" directory to prevent namespace collisions.
Kay Sievers86406242007-03-14 03:25:56 +0100860 */
861 if (parent == NULL)
862 parent_kobj = virtual_device_parent(dev);
Eric W. Biederman24b14422010-07-24 22:43:35 -0700863 else if (parent->class && !dev->class->ns_type)
Kay Sievers86406242007-03-14 03:25:56 +0100864 return &parent->kobj;
865 else
866 parent_kobj = &parent->kobj;
867
Tejun Heo77d3d7c2010-02-05 17:57:02 +0900868 mutex_lock(&gdp_mutex);
869
Kay Sievers86406242007-03-14 03:25:56 +0100870 /* find our class-directory at the parent and reference it */
Kay Sievers6b6e39a2010-11-15 23:13:18 +0100871 spin_lock(&dev->class->p->glue_dirs.list_lock);
872 list_for_each_entry(k, &dev->class->p->glue_dirs.list, entry)
Kay Sievers86406242007-03-14 03:25:56 +0100873 if (k->parent == parent_kobj) {
874 kobj = kobject_get(k);
875 break;
876 }
Kay Sievers6b6e39a2010-11-15 23:13:18 +0100877 spin_unlock(&dev->class->p->glue_dirs.list_lock);
Tejun Heo77d3d7c2010-02-05 17:57:02 +0900878 if (kobj) {
879 mutex_unlock(&gdp_mutex);
Kay Sievers86406242007-03-14 03:25:56 +0100880 return kobj;
Tejun Heo77d3d7c2010-02-05 17:57:02 +0900881 }
Kay Sievers86406242007-03-14 03:25:56 +0100882
883 /* or create a new class-directory at the parent device */
Eric W. Biedermanbc451f22010-03-30 11:31:25 -0700884 k = class_dir_create_and_add(dev->class, parent_kobj);
Kay Sievers0f4dafc2007-12-19 01:40:42 +0100885 /* do not emit an uevent for this simple "glue" directory */
Tejun Heo77d3d7c2010-02-05 17:57:02 +0900886 mutex_unlock(&gdp_mutex);
Greg Kroah-Hartman43968d22007-11-05 22:24:43 -0800887 return k;
Kay Sievers86406242007-03-14 03:25:56 +0100888 }
889
Kay Sieversca22e562011-12-14 14:29:38 -0800890 /* subsystems can specify a default root directory for their devices */
891 if (!parent && dev->bus && dev->bus->dev_root)
892 return &dev->bus->dev_root->kobj;
893
Kay Sievers86406242007-03-14 03:25:56 +0100894 if (parent)
Cornelia Huckc744aeae2007-01-08 20:16:44 +0100895 return &parent->kobj;
896 return NULL;
897}
Kay Sieversda231fd2007-11-21 17:29:15 +0100898
Cornelia Huck63b69712008-01-21 16:09:44 +0100899static void cleanup_glue_dir(struct device *dev, struct kobject *glue_dir)
Kay Sieversda231fd2007-11-21 17:29:15 +0100900{
Kay Sievers0f4dafc2007-12-19 01:40:42 +0100901 /* see if we live in a "glue" directory */
Cornelia Huckc1fe5392008-02-27 15:38:23 +0100902 if (!glue_dir || !dev->class ||
Kay Sievers6b6e39a2010-11-15 23:13:18 +0100903 glue_dir->kset != &dev->class->p->glue_dirs)
Kay Sieversda231fd2007-11-21 17:29:15 +0100904 return;
905
Kay Sievers0f4dafc2007-12-19 01:40:42 +0100906 kobject_put(glue_dir);
Kay Sieversda231fd2007-11-21 17:29:15 +0100907}
Cornelia Huck63b69712008-01-21 16:09:44 +0100908
909static void cleanup_device_parent(struct device *dev)
910{
911 cleanup_glue_dir(dev, dev->kobj.parent);
912}
Kay Sievers86406242007-03-14 03:25:56 +0100913
Cornelia Huck2ee97ca2007-07-18 01:43:47 -0700914static int device_add_class_symlinks(struct device *dev)
915{
916 int error;
917
918 if (!dev->class)
919 return 0;
Kay Sieversda231fd2007-11-21 17:29:15 +0100920
Greg Kroah-Hartman1fbfee62008-05-28 09:28:39 -0700921 error = sysfs_create_link(&dev->kobj,
Kay Sievers6b6e39a2010-11-15 23:13:18 +0100922 &dev->class->p->subsys.kobj,
Cornelia Huck2ee97ca2007-07-18 01:43:47 -0700923 "subsystem");
924 if (error)
925 goto out;
Kay Sieversda231fd2007-11-21 17:29:15 +0100926
Greg Kroah-Hartman4e886c22008-01-27 12:12:43 -0800927 if (dev->parent && device_is_not_partition(dev)) {
Dmitry Torokhov4f01a752007-09-18 22:46:50 -0700928 error = sysfs_create_link(&dev->kobj, &dev->parent->kobj,
929 "device");
930 if (error)
Kay Sievers39aba962010-09-04 22:33:14 -0700931 goto out_subsys;
Cornelia Huck2ee97ca2007-07-18 01:43:47 -0700932 }
Kay Sievers39aba962010-09-04 22:33:14 -0700933
Randy Dunlapead454f2010-09-24 14:36:49 -0700934#ifdef CONFIG_BLOCK
Kay Sievers39aba962010-09-04 22:33:14 -0700935 /* /sys/block has directories and does not need symlinks */
Andi Kleene52eec12010-09-08 16:54:17 +0200936 if (sysfs_deprecated && dev->class == &block_class)
Kay Sievers39aba962010-09-04 22:33:14 -0700937 return 0;
Randy Dunlapead454f2010-09-24 14:36:49 -0700938#endif
Kay Sievers39aba962010-09-04 22:33:14 -0700939
940 /* link in the class directory pointing to the device */
Kay Sievers6b6e39a2010-11-15 23:13:18 +0100941 error = sysfs_create_link(&dev->class->p->subsys.kobj,
Kay Sievers39aba962010-09-04 22:33:14 -0700942 &dev->kobj, dev_name(dev));
943 if (error)
944 goto out_device;
945
Cornelia Huck2ee97ca2007-07-18 01:43:47 -0700946 return 0;
947
Kay Sievers39aba962010-09-04 22:33:14 -0700948out_device:
949 sysfs_remove_link(&dev->kobj, "device");
Kay Sieversda231fd2007-11-21 17:29:15 +0100950
Cornelia Huck2ee97ca2007-07-18 01:43:47 -0700951out_subsys:
952 sysfs_remove_link(&dev->kobj, "subsystem");
953out:
954 return error;
955}
956
957static void device_remove_class_symlinks(struct device *dev)
958{
959 if (!dev->class)
960 return;
Kay Sieversda231fd2007-11-21 17:29:15 +0100961
Greg Kroah-Hartman4e886c22008-01-27 12:12:43 -0800962 if (dev->parent && device_is_not_partition(dev))
Kay Sieversda231fd2007-11-21 17:29:15 +0100963 sysfs_remove_link(&dev->kobj, "device");
Cornelia Huck2ee97ca2007-07-18 01:43:47 -0700964 sysfs_remove_link(&dev->kobj, "subsystem");
Randy Dunlapead454f2010-09-24 14:36:49 -0700965#ifdef CONFIG_BLOCK
Andi Kleene52eec12010-09-08 16:54:17 +0200966 if (sysfs_deprecated && dev->class == &block_class)
Kay Sievers39aba962010-09-04 22:33:14 -0700967 return;
Randy Dunlapead454f2010-09-24 14:36:49 -0700968#endif
Kay Sievers6b6e39a2010-11-15 23:13:18 +0100969 sysfs_delete_link(&dev->class->p->subsys.kobj, &dev->kobj, dev_name(dev));
Cornelia Huck2ee97ca2007-07-18 01:43:47 -0700970}
971
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972/**
Stephen Rothwell413c2392008-05-30 10:16:40 +1000973 * dev_set_name - set a device name
974 * @dev: device
Randy Dunlap46232362008-06-04 21:40:43 -0700975 * @fmt: format string for the device's name
Stephen Rothwell413c2392008-05-30 10:16:40 +1000976 */
977int dev_set_name(struct device *dev, const char *fmt, ...)
978{
979 va_list vargs;
Kay Sievers1fa5ae82009-01-25 15:17:37 +0100980 int err;
Stephen Rothwell413c2392008-05-30 10:16:40 +1000981
982 va_start(vargs, fmt);
Kay Sievers1fa5ae82009-01-25 15:17:37 +0100983 err = kobject_set_name_vargs(&dev->kobj, fmt, vargs);
Stephen Rothwell413c2392008-05-30 10:16:40 +1000984 va_end(vargs);
Kay Sievers1fa5ae82009-01-25 15:17:37 +0100985 return err;
Stephen Rothwell413c2392008-05-30 10:16:40 +1000986}
987EXPORT_SYMBOL_GPL(dev_set_name);
988
989/**
Dan Williamse105b8b2008-04-21 10:51:07 -0700990 * device_to_dev_kobj - select a /sys/dev/ directory for the device
991 * @dev: device
992 *
993 * By default we select char/ for new entries. Setting class->dev_obj
994 * to NULL prevents an entry from being created. class->dev_kobj must
995 * be set (or cleared) before any devices are registered to the class
996 * otherwise device_create_sys_dev_entry() and
Peter Korsgaard0d4e293c2012-04-17 12:12:57 +0200997 * device_remove_sys_dev_entry() will disagree about the presence of
998 * the link.
Dan Williamse105b8b2008-04-21 10:51:07 -0700999 */
1000static struct kobject *device_to_dev_kobj(struct device *dev)
1001{
1002 struct kobject *kobj;
1003
1004 if (dev->class)
1005 kobj = dev->class->dev_kobj;
1006 else
1007 kobj = sysfs_dev_char_kobj;
1008
1009 return kobj;
1010}
1011
1012static int device_create_sys_dev_entry(struct device *dev)
1013{
1014 struct kobject *kobj = device_to_dev_kobj(dev);
1015 int error = 0;
1016 char devt_str[15];
1017
1018 if (kobj) {
1019 format_dev_t(devt_str, dev->devt);
1020 error = sysfs_create_link(kobj, &dev->kobj, devt_str);
1021 }
1022
1023 return error;
1024}
1025
1026static void device_remove_sys_dev_entry(struct device *dev)
1027{
1028 struct kobject *kobj = device_to_dev_kobj(dev);
1029 char devt_str[15];
1030
1031 if (kobj) {
1032 format_dev_t(devt_str, dev->devt);
1033 sysfs_remove_link(kobj, devt_str);
1034 }
1035}
1036
Greg Kroah-Hartmanb4028432009-05-11 14:16:57 -07001037int device_private_init(struct device *dev)
1038{
1039 dev->p = kzalloc(sizeof(*dev->p), GFP_KERNEL);
1040 if (!dev->p)
1041 return -ENOMEM;
1042 dev->p->device = dev;
1043 klist_init(&dev->p->klist_children, klist_children_get,
1044 klist_children_put);
Greg Kroah-Hartmanef8a3fd2012-03-08 12:17:22 -08001045 INIT_LIST_HEAD(&dev->p->deferred_probe);
Greg Kroah-Hartmanb4028432009-05-11 14:16:57 -07001046 return 0;
1047}
1048
Dan Williamse105b8b2008-04-21 10:51:07 -07001049/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001050 * device_add - add device to device hierarchy.
1051 * @dev: device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001053 * This is part 2 of device_register(), though may be called
1054 * separately _iff_ device_initialize() has been called separately.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 *
Cornelia Huck57394112008-09-03 18:26:40 +02001056 * This adds @dev to the kobject hierarchy via kobject_add(), adds it
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001057 * to the global and sibling lists for the device, then
1058 * adds it to the other relevant subsystems of the driver model.
Cornelia Huck57394112008-09-03 18:26:40 +02001059 *
Alan Sternb10d5ef2012-01-17 11:39:00 -05001060 * Do not call this routine or device_register() more than once for
1061 * any device structure. The driver model core is not designed to work
1062 * with devices that get unregistered and then spring back to life.
1063 * (Among other things, it's very hard to guarantee that all references
1064 * to the previous incarnation of @dev have been dropped.) Allocate
1065 * and register a fresh new struct device instead.
1066 *
Cornelia Huck57394112008-09-03 18:26:40 +02001067 * NOTE: _Never_ directly free @dev after calling this function, even
1068 * if it returned an error! Always use put_device() to give up your
1069 * reference instead.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 */
1071int device_add(struct device *dev)
1072{
1073 struct device *parent = NULL;
Kay Sieversca22e562011-12-14 14:29:38 -08001074 struct kobject *kobj;
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02001075 struct class_interface *class_intf;
Greg Kroah-Hartmanc906a482008-05-30 10:45:12 -07001076 int error = -EINVAL;
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001077
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 dev = get_device(dev);
Greg Kroah-Hartmanc906a482008-05-30 10:45:12 -07001079 if (!dev)
1080 goto done;
1081
Greg Kroah-Hartmanfb069a52008-12-16 12:23:36 -08001082 if (!dev->p) {
Greg Kroah-Hartmanb4028432009-05-11 14:16:57 -07001083 error = device_private_init(dev);
1084 if (error)
1085 goto done;
Greg Kroah-Hartmanfb069a52008-12-16 12:23:36 -08001086 }
Greg Kroah-Hartmanfb069a52008-12-16 12:23:36 -08001087
Kay Sievers1fa5ae82009-01-25 15:17:37 +01001088 /*
1089 * for statically allocated devices, which should all be converted
1090 * some day, we need to initialize the name. We prevent reading back
1091 * the name, and force the use of dev_name()
1092 */
1093 if (dev->init_name) {
Greg Kroah-Hartmanacc0e902009-06-02 15:39:55 -07001094 dev_set_name(dev, "%s", dev->init_name);
Kay Sievers1fa5ae82009-01-25 15:17:37 +01001095 dev->init_name = NULL;
1096 }
Greg Kroah-Hartmanc906a482008-05-30 10:45:12 -07001097
Kay Sieversca22e562011-12-14 14:29:38 -08001098 /* subsystems can specify simple device enumeration */
1099 if (!dev_name(dev) && dev->bus && dev->bus->dev_name)
1100 dev_set_name(dev, "%s%u", dev->bus->dev_name, dev->id);
1101
Thomas Gleixnere6309e72009-12-10 19:32:49 +00001102 if (!dev_name(dev)) {
1103 error = -EINVAL;
Kay Sievers5c8563d2009-05-28 14:24:07 -07001104 goto name_error;
Thomas Gleixnere6309e72009-12-10 19:32:49 +00001105 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01001107 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
Greg Kroah-Hartmanc205ef42006-08-07 22:19:37 -07001108
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 parent = get_device(dev->parent);
Kay Sieversca22e562011-12-14 14:29:38 -08001110 kobj = get_device_parent(dev, parent);
1111 if (kobj)
1112 dev->kobj.parent = kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113
Yinghai Lu0d358f22008-02-19 03:20:41 -08001114 /* use parent numa_node */
1115 if (parent)
1116 set_dev_node(dev, dev_to_node(parent));
1117
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 /* first, register with generic layer. */
Kay Sievers8a577ff2009-04-18 15:05:45 -07001119 /* we require the name to be set before, and pass NULL */
1120 error = kobject_add(&dev->kobj, dev->kobj.parent, NULL);
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +01001121 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 goto Error;
Kay Sieversa7fd6702005-10-01 14:49:43 +02001123
Brian Walsh37022642006-08-14 22:43:19 -07001124 /* notify platform of device entry */
1125 if (platform_notify)
1126 platform_notify(dev);
1127
Tejun Heoad6a1e12007-06-14 03:45:17 +09001128 error = device_create_file(dev, &uevent_attr);
Cornelia Hucka306eea2006-09-22 11:37:13 +02001129 if (error)
1130 goto attrError;
Kay Sieversa7fd6702005-10-01 14:49:43 +02001131
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001132 if (MAJOR(dev->devt)) {
Tejun Heoad6a1e12007-06-14 03:45:17 +09001133 error = device_create_file(dev, &devt_attr);
1134 if (error)
Cornelia Hucka306eea2006-09-22 11:37:13 +02001135 goto ueventattrError;
Dan Williamse105b8b2008-04-21 10:51:07 -07001136
1137 error = device_create_sys_dev_entry(dev);
1138 if (error)
1139 goto devtattrError;
Kay Sievers2b2af542009-04-30 15:23:42 +02001140
1141 devtmpfs_create_node(dev);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001142 }
1143
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001144 error = device_add_class_symlinks(dev);
1145 if (error)
1146 goto SymlinkError;
Cornelia Huckdc0afa82007-07-09 11:39:18 -07001147 error = device_add_attrs(dev);
1148 if (error)
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07001149 goto AttrsError;
Cornelia Huckdc0afa82007-07-09 11:39:18 -07001150 error = bus_add_device(dev);
1151 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 goto BusError;
Alan Stern3b98aea2008-08-07 13:06:12 -04001153 error = dpm_sysfs_add(dev);
Rafael J. Wysocki57eee3d2008-03-12 00:59:38 +01001154 if (error)
Alan Stern3b98aea2008-08-07 13:06:12 -04001155 goto DPMError;
1156 device_pm_add(dev);
Alan Sternec0676ee2008-12-05 14:10:31 -05001157
1158 /* Notify clients of device addition. This call must come
majianpeng268863f2012-01-11 15:12:06 +00001159 * after dpm_sysfs_add() and before kobject_uevent().
Alan Sternec0676ee2008-12-05 14:10:31 -05001160 */
1161 if (dev->bus)
1162 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
1163 BUS_NOTIFY_ADD_DEVICE, dev);
1164
Cornelia Huck83b5fb4c2007-03-29 11:12:11 +02001165 kobject_uevent(&dev->kobj, KOBJ_ADD);
Alan Stern2023c612009-07-30 15:27:18 -04001166 bus_probe_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 if (parent)
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08001168 klist_add_tail(&dev->p->knode_parent,
1169 &parent->p->klist_children);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170
Greg Kroah-Hartman5d9fd162006-06-22 17:17:32 -07001171 if (dev->class) {
Kay Sieversca22e562011-12-14 14:29:38 -08001172 mutex_lock(&dev->class->p->mutex);
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02001173 /* tie the class to the device */
Tejun Heo5a3ceb82008-08-25 19:50:19 +02001174 klist_add_tail(&dev->knode_class,
Kay Sievers6b6e39a2010-11-15 23:13:18 +01001175 &dev->class->p->klist_devices);
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02001176
1177 /* notify any interfaces that the device is here */
Greg Kroah-Hartman184f1f72008-05-28 09:28:39 -07001178 list_for_each_entry(class_intf,
Kay Sieversca22e562011-12-14 14:29:38 -08001179 &dev->class->p->interfaces, node)
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02001180 if (class_intf->add_dev)
1181 class_intf->add_dev(dev, class_intf);
Kay Sieversca22e562011-12-14 14:29:38 -08001182 mutex_unlock(&dev->class->p->mutex);
Greg Kroah-Hartman5d9fd162006-06-22 17:17:32 -07001183 }
Greg Kroah-Hartmanc906a482008-05-30 10:45:12 -07001184done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 put_device(dev);
1186 return error;
Alan Stern3b98aea2008-08-07 13:06:12 -04001187 DPMError:
Rafael J. Wysocki57eee3d2008-03-12 00:59:38 +01001188 bus_remove_device(dev);
1189 BusError:
James Simmons82f0cf92007-02-21 17:44:51 +00001190 device_remove_attrs(dev);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07001191 AttrsError:
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001192 device_remove_class_symlinks(dev);
1193 SymlinkError:
Tejun Heoad6a1e12007-06-14 03:45:17 +09001194 if (MAJOR(dev->devt))
Kay Sieversad729562009-10-28 19:51:37 +01001195 devtmpfs_delete_node(dev);
1196 if (MAJOR(dev->devt))
Dan Williamse105b8b2008-04-21 10:51:07 -07001197 device_remove_sys_dev_entry(dev);
1198 devtattrError:
1199 if (MAJOR(dev->devt))
Tejun Heoad6a1e12007-06-14 03:45:17 +09001200 device_remove_file(dev, &devt_attr);
Cornelia Hucka306eea2006-09-22 11:37:13 +02001201 ueventattrError:
Tejun Heoad6a1e12007-06-14 03:45:17 +09001202 device_remove_file(dev, &uevent_attr);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001203 attrError:
Kay Sievers312c0042005-11-16 09:00:00 +01001204 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 kobject_del(&dev->kobj);
1206 Error:
Cornelia Huck63b69712008-01-21 16:09:44 +01001207 cleanup_device_parent(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 if (parent)
1209 put_device(parent);
Kay Sievers5c8563d2009-05-28 14:24:07 -07001210name_error:
1211 kfree(dev->p);
1212 dev->p = NULL;
Greg Kroah-Hartmanc906a482008-05-30 10:45:12 -07001213 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214}
1215
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001217 * device_register - register a device with the system.
1218 * @dev: pointer to the device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001220 * This happens in two clean steps - initialize the device
1221 * and add it to the system. The two steps can be called
1222 * separately, but this is the easiest and most common.
1223 * I.e. you should only call the two helpers separately if
1224 * have a clearly defined need to use and refcount the device
1225 * before it is added to the hierarchy.
Cornelia Huck57394112008-09-03 18:26:40 +02001226 *
Alan Sternb10d5ef2012-01-17 11:39:00 -05001227 * For more information, see the kerneldoc for device_initialize()
1228 * and device_add().
1229 *
Cornelia Huck57394112008-09-03 18:26:40 +02001230 * NOTE: _Never_ directly free @dev after calling this function, even
1231 * if it returned an error! Always use put_device() to give up the
1232 * reference initialized in this function instead.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234int device_register(struct device *dev)
1235{
1236 device_initialize(dev);
1237 return device_add(dev);
1238}
1239
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001241 * get_device - increment reference count for device.
1242 * @dev: device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001244 * This simply forwards the call to kobject_get(), though
1245 * we do take care to provide for the case that we get a NULL
1246 * pointer passed in.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001248struct device *get_device(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +02001250 return dev ? kobj_to_dev(kobject_get(&dev->kobj)) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251}
1252
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001254 * put_device - decrement reference count.
1255 * @dev: device in question.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001257void put_device(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258{
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001259 /* might_sleep(); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 if (dev)
1261 kobject_put(&dev->kobj);
1262}
1263
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001265 * device_del - delete device from system.
1266 * @dev: device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001268 * This is the first part of the device unregistration
1269 * sequence. This removes the device from the lists we control
1270 * from here, has it removed from the other driver model
1271 * subsystems it was added to in device_add(), and removes it
1272 * from the kobject hierarchy.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001274 * NOTE: this should be called manually _iff_ device_add() was
1275 * also called manually.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001277void device_del(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001279 struct device *parent = dev->parent;
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02001280 struct class_interface *class_intf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281
Alan Sternec0676ee2008-12-05 14:10:31 -05001282 /* Notify clients of device removal. This call must come
1283 * before dpm_sysfs_remove().
1284 */
1285 if (dev->bus)
1286 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
1287 BUS_NOTIFY_DEL_DEVICE, dev);
Alan Stern3b98aea2008-08-07 13:06:12 -04001288 dpm_sysfs_remove(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 if (parent)
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08001290 klist_del(&dev->p->knode_parent);
Dan Williamse105b8b2008-04-21 10:51:07 -07001291 if (MAJOR(dev->devt)) {
Kay Sievers2b2af542009-04-30 15:23:42 +02001292 devtmpfs_delete_node(dev);
Dan Williamse105b8b2008-04-21 10:51:07 -07001293 device_remove_sys_dev_entry(dev);
Tejun Heoad6a1e12007-06-14 03:45:17 +09001294 device_remove_file(dev, &devt_attr);
Dan Williamse105b8b2008-04-21 10:51:07 -07001295 }
Kay Sieversb9d9c822006-06-15 15:31:56 +02001296 if (dev->class) {
Kay Sieversda231fd2007-11-21 17:29:15 +01001297 device_remove_class_symlinks(dev);
Kay Sievers99ef3ef2006-09-14 11:23:28 +02001298
Kay Sieversca22e562011-12-14 14:29:38 -08001299 mutex_lock(&dev->class->p->mutex);
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02001300 /* notify any interfaces that the device is now gone */
Greg Kroah-Hartman184f1f72008-05-28 09:28:39 -07001301 list_for_each_entry(class_intf,
Kay Sieversca22e562011-12-14 14:29:38 -08001302 &dev->class->p->interfaces, node)
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02001303 if (class_intf->remove_dev)
1304 class_intf->remove_dev(dev, class_intf);
1305 /* remove the device from the class list */
Tejun Heo5a3ceb82008-08-25 19:50:19 +02001306 klist_del(&dev->knode_class);
Kay Sieversca22e562011-12-14 14:29:38 -08001307 mutex_unlock(&dev->class->p->mutex);
Kay Sieversb9d9c822006-06-15 15:31:56 +02001308 }
Tejun Heoad6a1e12007-06-14 03:45:17 +09001309 device_remove_file(dev, &uevent_attr);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07001310 device_remove_attrs(dev);
Benjamin Herrenschmidt28953532006-11-08 19:46:14 -08001311 bus_remove_device(dev);
LongX Zhang4b6d1f122012-10-25 00:21:28 +02001312 device_pm_remove(dev);
Grant Likelyd1c34142012-03-05 08:47:41 -07001313 driver_deferred_probe_del(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314
1315 /* Notify the platform of the removal, in case they
1316 * need to do anything...
1317 */
1318 if (platform_notify_remove)
1319 platform_notify_remove(dev);
Kay Sievers312c0042005-11-16 09:00:00 +01001320 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
Kay Sieversda231fd2007-11-21 17:29:15 +01001321 cleanup_device_parent(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 kobject_del(&dev->kobj);
Kay Sieversda231fd2007-11-21 17:29:15 +01001323 put_device(parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324}
1325
1326/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001327 * device_unregister - unregister device from system.
1328 * @dev: device going away.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001330 * We do this in two parts, like we do device_register(). First,
1331 * we remove it from all the subsystems with device_del(), then
1332 * we decrement the reference count via put_device(). If that
1333 * is the final reference count, the device will be cleaned up
1334 * via device_release() above. Otherwise, the structure will
1335 * stick around until the final reference to the device is dropped.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001337void device_unregister(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338{
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01001339 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 device_del(dev);
1341 put_device(dev);
1342}
1343
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001344static struct device *next_device(struct klist_iter *i)
mochel@digitalimplant.org36239572005-03-24 19:08:30 -08001345{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001346 struct klist_node *n = klist_next(i);
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08001347 struct device *dev = NULL;
1348 struct device_private *p;
1349
1350 if (n) {
1351 p = to_device_private_parent(n);
1352 dev = p->device;
1353 }
1354 return dev;
mochel@digitalimplant.org36239572005-03-24 19:08:30 -08001355}
1356
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357/**
Kay Sieverse454cea2009-09-18 23:01:12 +02001358 * device_get_devnode - path of device node file
Kay Sievers6fcf53a2009-04-30 15:23:42 +02001359 * @dev: device
Kay Sieverse454cea2009-09-18 23:01:12 +02001360 * @mode: returned file access mode
Kay Sievers3c2670e2013-04-06 09:56:00 -07001361 * @uid: returned file owner
1362 * @gid: returned file group
Kay Sievers6fcf53a2009-04-30 15:23:42 +02001363 * @tmp: possibly allocated string
1364 *
1365 * Return the relative path of a possible device node.
1366 * Non-default names may need to allocate a memory to compose
1367 * a name. This memory is returned in tmp and needs to be
1368 * freed by the caller.
1369 */
Kay Sieverse454cea2009-09-18 23:01:12 +02001370const char *device_get_devnode(struct device *dev,
Greg Kroah-Hartman4e4098a2013-04-11 11:43:29 -07001371 umode_t *mode, kuid_t *uid, kgid_t *gid,
Kay Sievers3c2670e2013-04-06 09:56:00 -07001372 const char **tmp)
Kay Sievers6fcf53a2009-04-30 15:23:42 +02001373{
1374 char *s;
1375
1376 *tmp = NULL;
1377
1378 /* the device type may provide a specific name */
Kay Sieverse454cea2009-09-18 23:01:12 +02001379 if (dev->type && dev->type->devnode)
Kay Sievers3c2670e2013-04-06 09:56:00 -07001380 *tmp = dev->type->devnode(dev, mode, uid, gid);
Kay Sievers6fcf53a2009-04-30 15:23:42 +02001381 if (*tmp)
1382 return *tmp;
1383
1384 /* the class may provide a specific name */
Kay Sieverse454cea2009-09-18 23:01:12 +02001385 if (dev->class && dev->class->devnode)
1386 *tmp = dev->class->devnode(dev, mode);
Kay Sievers6fcf53a2009-04-30 15:23:42 +02001387 if (*tmp)
1388 return *tmp;
1389
1390 /* return name without allocation, tmp == NULL */
1391 if (strchr(dev_name(dev), '!') == NULL)
1392 return dev_name(dev);
1393
1394 /* replace '!' in the name with '/' */
1395 *tmp = kstrdup(dev_name(dev), GFP_KERNEL);
1396 if (!*tmp)
1397 return NULL;
1398 while ((s = strchr(*tmp, '!')))
1399 s[0] = '/';
1400 return *tmp;
1401}
1402
1403/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001404 * device_for_each_child - device child iterator.
1405 * @parent: parent struct device.
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001406 * @fn: function to be called for each device.
Robert P. J. Dayf8878dc2013-06-01 20:17:34 -04001407 * @data: data for the callback.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001409 * Iterate over @parent's child devices, and call @fn for each,
1410 * passing it @data.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001412 * We check the return of @fn each time. If it returns anything
1413 * other than 0, we break out and return that value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001415int device_for_each_child(struct device *parent, void *data,
1416 int (*fn)(struct device *dev, void *data))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417{
mochel@digitalimplant.org36239572005-03-24 19:08:30 -08001418 struct klist_iter i;
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001419 struct device *child;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 int error = 0;
1421
Greg Kroah-Hartman014c90db2009-04-15 16:00:12 -07001422 if (!parent->p)
1423 return 0;
1424
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08001425 klist_iter_init(&parent->p->klist_children, &i);
mochel@digitalimplant.org36239572005-03-24 19:08:30 -08001426 while ((child = next_device(&i)) && !error)
1427 error = fn(child, data);
1428 klist_iter_exit(&i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 return error;
1430}
1431
Cornelia Huck5ab69982006-11-16 15:42:07 +01001432/**
1433 * device_find_child - device iterator for locating a particular device.
1434 * @parent: parent struct device
Cornelia Huck5ab69982006-11-16 15:42:07 +01001435 * @match: Callback function to check device
Robert P. J. Dayf8878dc2013-06-01 20:17:34 -04001436 * @data: Data to pass to match function
Cornelia Huck5ab69982006-11-16 15:42:07 +01001437 *
1438 * This is similar to the device_for_each_child() function above, but it
1439 * returns a reference to a device that is 'found' for later use, as
1440 * determined by the @match callback.
1441 *
1442 * The callback should return 0 if the device doesn't match and non-zero
1443 * if it does. If the callback returns non-zero and a reference to the
1444 * current device can be obtained, this function will return to the caller
1445 * and not iterate over any more devices.
Federico Vagaa4e24002013-04-15 11:18:11 +02001446 *
1447 * NOTE: you will need to drop the reference with put_device() after use.
Cornelia Huck5ab69982006-11-16 15:42:07 +01001448 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001449struct device *device_find_child(struct device *parent, void *data,
1450 int (*match)(struct device *dev, void *data))
Cornelia Huck5ab69982006-11-16 15:42:07 +01001451{
1452 struct klist_iter i;
1453 struct device *child;
1454
1455 if (!parent)
1456 return NULL;
1457
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08001458 klist_iter_init(&parent->p->klist_children, &i);
Cornelia Huck5ab69982006-11-16 15:42:07 +01001459 while ((child = next_device(&i)))
1460 if (match(child, data) && get_device(child))
1461 break;
1462 klist_iter_exit(&i);
1463 return child;
1464}
1465
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466int __init devices_init(void)
1467{
Greg Kroah-Hartman881c6cfd2007-11-01 09:29:06 -06001468 devices_kset = kset_create_and_add("devices", &device_uevent_ops, NULL);
1469 if (!devices_kset)
1470 return -ENOMEM;
Dan Williamse105b8b2008-04-21 10:51:07 -07001471 dev_kobj = kobject_create_and_add("dev", NULL);
1472 if (!dev_kobj)
1473 goto dev_kobj_err;
1474 sysfs_dev_block_kobj = kobject_create_and_add("block", dev_kobj);
1475 if (!sysfs_dev_block_kobj)
1476 goto block_kobj_err;
1477 sysfs_dev_char_kobj = kobject_create_and_add("char", dev_kobj);
1478 if (!sysfs_dev_char_kobj)
1479 goto char_kobj_err;
1480
Greg Kroah-Hartman881c6cfd2007-11-01 09:29:06 -06001481 return 0;
Dan Williamse105b8b2008-04-21 10:51:07 -07001482
1483 char_kobj_err:
1484 kobject_put(sysfs_dev_block_kobj);
1485 block_kobj_err:
1486 kobject_put(dev_kobj);
1487 dev_kobj_err:
1488 kset_unregister(devices_kset);
1489 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490}
1491
1492EXPORT_SYMBOL_GPL(device_for_each_child);
Cornelia Huck5ab69982006-11-16 15:42:07 +01001493EXPORT_SYMBOL_GPL(device_find_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494
1495EXPORT_SYMBOL_GPL(device_initialize);
1496EXPORT_SYMBOL_GPL(device_add);
1497EXPORT_SYMBOL_GPL(device_register);
1498
1499EXPORT_SYMBOL_GPL(device_del);
1500EXPORT_SYMBOL_GPL(device_unregister);
1501EXPORT_SYMBOL_GPL(get_device);
1502EXPORT_SYMBOL_GPL(put_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503
1504EXPORT_SYMBOL_GPL(device_create_file);
1505EXPORT_SYMBOL_GPL(device_remove_file);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001506
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02001507static int device_check_offline(struct device *dev, void *not_used)
1508{
1509 int ret;
1510
1511 ret = device_for_each_child(dev, NULL, device_check_offline);
1512 if (ret)
1513 return ret;
1514
1515 return device_supports_offline(dev) && !dev->offline ? -EBUSY : 0;
1516}
1517
1518/**
1519 * device_offline - Prepare the device for hot-removal.
1520 * @dev: Device to be put offline.
1521 *
1522 * Execute the device bus type's .offline() callback, if present, to prepare
1523 * the device for a subsequent hot-removal. If that succeeds, the device must
1524 * not be used until either it is removed or its bus type's .online() callback
1525 * is executed.
1526 *
1527 * Call under device_hotplug_lock.
1528 */
1529int device_offline(struct device *dev)
1530{
1531 int ret;
1532
1533 if (dev->offline_disabled)
1534 return -EPERM;
1535
1536 ret = device_for_each_child(dev, NULL, device_check_offline);
1537 if (ret)
1538 return ret;
1539
1540 device_lock(dev);
1541 if (device_supports_offline(dev)) {
1542 if (dev->offline) {
1543 ret = 1;
1544 } else {
1545 ret = dev->bus->offline(dev);
1546 if (!ret) {
1547 kobject_uevent(&dev->kobj, KOBJ_OFFLINE);
1548 dev->offline = true;
1549 }
1550 }
1551 }
1552 device_unlock(dev);
1553
1554 return ret;
1555}
1556
1557/**
1558 * device_online - Put the device back online after successful device_offline().
1559 * @dev: Device to be put back online.
1560 *
1561 * If device_offline() has been successfully executed for @dev, but the device
1562 * has not been removed subsequently, execute its bus type's .online() callback
1563 * to indicate that the device can be used again.
1564 *
1565 * Call under device_hotplug_lock.
1566 */
1567int device_online(struct device *dev)
1568{
1569 int ret = 0;
1570
1571 device_lock(dev);
1572 if (device_supports_offline(dev)) {
1573 if (dev->offline) {
1574 ret = dev->bus->online(dev);
1575 if (!ret) {
1576 kobject_uevent(&dev->kobj, KOBJ_ONLINE);
1577 dev->offline = false;
1578 }
1579 } else {
1580 ret = 1;
1581 }
1582 }
1583 device_unlock(dev);
1584
1585 return ret;
1586}
1587
Karthigan Srinivasan7f100d12011-04-18 16:16:52 -05001588struct root_device {
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00001589 struct device dev;
1590 struct module *owner;
1591};
1592
Josh Triplett93058422012-11-18 21:27:55 -08001593static inline struct root_device *to_root_device(struct device *d)
Ferenc Wagner481e2072011-01-07 15:17:47 +01001594{
1595 return container_of(d, struct root_device, dev);
1596}
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00001597
1598static void root_device_release(struct device *dev)
1599{
1600 kfree(to_root_device(dev));
1601}
1602
1603/**
1604 * __root_device_register - allocate and register a root device
1605 * @name: root device name
1606 * @owner: owner module of the root device, usually THIS_MODULE
1607 *
1608 * This function allocates a root device and registers it
1609 * using device_register(). In order to free the returned
1610 * device, use root_device_unregister().
1611 *
1612 * Root devices are dummy devices which allow other devices
1613 * to be grouped under /sys/devices. Use this function to
1614 * allocate a root device and then use it as the parent of
1615 * any device which should appear under /sys/devices/{name}
1616 *
1617 * The /sys/devices/{name} directory will also contain a
1618 * 'module' symlink which points to the @owner directory
1619 * in sysfs.
1620 *
Jani Nikulaf0eae0e2010-03-11 18:11:45 +02001621 * Returns &struct device pointer on success, or ERR_PTR() on error.
1622 *
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00001623 * Note: You probably want to use root_device_register().
1624 */
1625struct device *__root_device_register(const char *name, struct module *owner)
1626{
1627 struct root_device *root;
1628 int err = -ENOMEM;
1629
1630 root = kzalloc(sizeof(struct root_device), GFP_KERNEL);
1631 if (!root)
1632 return ERR_PTR(err);
1633
Greg Kroah-Hartmanacc0e902009-06-02 15:39:55 -07001634 err = dev_set_name(&root->dev, "%s", name);
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00001635 if (err) {
1636 kfree(root);
1637 return ERR_PTR(err);
1638 }
1639
1640 root->dev.release = root_device_release;
1641
1642 err = device_register(&root->dev);
1643 if (err) {
1644 put_device(&root->dev);
1645 return ERR_PTR(err);
1646 }
1647
Christoph Egger1d9e8822010-05-17 16:57:58 +02001648#ifdef CONFIG_MODULES /* gotta find a "cleaner" way to do this */
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00001649 if (owner) {
1650 struct module_kobject *mk = &owner->mkobj;
1651
1652 err = sysfs_create_link(&root->dev.kobj, &mk->kobj, "module");
1653 if (err) {
1654 device_unregister(&root->dev);
1655 return ERR_PTR(err);
1656 }
1657 root->owner = owner;
1658 }
1659#endif
1660
1661 return &root->dev;
1662}
1663EXPORT_SYMBOL_GPL(__root_device_register);
1664
1665/**
1666 * root_device_unregister - unregister and free a root device
Randy Dunlap7cbcf222009-01-20 16:29:13 -08001667 * @dev: device going away
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00001668 *
1669 * This function unregisters and cleans up a device that was created by
1670 * root_device_register().
1671 */
1672void root_device_unregister(struct device *dev)
1673{
1674 struct root_device *root = to_root_device(dev);
1675
1676 if (root->owner)
1677 sysfs_remove_link(&root->dev.kobj, "module");
1678
1679 device_unregister(dev);
1680}
1681EXPORT_SYMBOL_GPL(root_device_unregister);
1682
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001683
1684static void device_create_release(struct device *dev)
1685{
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01001686 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001687 kfree(dev);
1688}
1689
Guenter Roeck39ef3112013-07-14 16:05:57 -07001690static struct device *
1691device_create_groups_vargs(struct class *class, struct device *parent,
1692 dev_t devt, void *drvdata,
1693 const struct attribute_group **groups,
1694 const char *fmt, va_list args)
1695{
1696 struct device *dev = NULL;
1697 int retval = -ENODEV;
1698
1699 if (class == NULL || IS_ERR(class))
1700 goto error;
1701
1702 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1703 if (!dev) {
1704 retval = -ENOMEM;
1705 goto error;
1706 }
1707
1708 dev->devt = devt;
1709 dev->class = class;
1710 dev->parent = parent;
1711 dev->groups = groups;
1712 dev->release = device_create_release;
1713 dev_set_drvdata(dev, drvdata);
1714
1715 retval = kobject_set_name_vargs(&dev->kobj, fmt, args);
1716 if (retval)
1717 goto error;
1718
1719 retval = device_register(dev);
1720 if (retval)
1721 goto error;
1722
1723 return dev;
1724
1725error:
1726 put_device(dev);
1727 return ERR_PTR(retval);
1728}
1729
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001730/**
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07001731 * device_create_vargs - creates a device and registers it with sysfs
1732 * @class: pointer to the struct class that this device should be registered to
1733 * @parent: pointer to the parent struct device of this new device, if any
1734 * @devt: the dev_t for the char device to be added
1735 * @drvdata: the data to be added to the device for callbacks
1736 * @fmt: string for the device's name
1737 * @args: va_list for the device's name
1738 *
1739 * This function can be used by char device classes. A struct device
1740 * will be created in sysfs, registered to the specified class.
1741 *
1742 * A "dev" file will be created, showing the dev_t for the device, if
1743 * the dev_t is not 0,0.
1744 * If a pointer to a parent struct device is passed in, the newly created
1745 * struct device will be a child of that device in sysfs.
1746 * The pointer to the struct device will be returned from the call.
1747 * Any further sysfs files that might be required can be created using this
1748 * pointer.
1749 *
Jani Nikulaf0eae0e2010-03-11 18:11:45 +02001750 * Returns &struct device pointer on success, or ERR_PTR() on error.
1751 *
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07001752 * Note: the struct class passed to this function must have previously
1753 * been created with a call to class_create().
1754 */
1755struct device *device_create_vargs(struct class *class, struct device *parent,
1756 dev_t devt, void *drvdata, const char *fmt,
1757 va_list args)
1758{
Guenter Roeck39ef3112013-07-14 16:05:57 -07001759 return device_create_groups_vargs(class, parent, devt, drvdata, NULL,
1760 fmt, args);
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07001761}
1762EXPORT_SYMBOL_GPL(device_create_vargs);
1763
1764/**
Greg Kroah-Hartman4e106732008-07-21 20:03:34 -07001765 * device_create - creates a device and registers it with sysfs
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07001766 * @class: pointer to the struct class that this device should be registered to
1767 * @parent: pointer to the parent struct device of this new device, if any
1768 * @devt: the dev_t for the char device to be added
1769 * @drvdata: the data to be added to the device for callbacks
1770 * @fmt: string for the device's name
1771 *
1772 * This function can be used by char device classes. A struct device
1773 * will be created in sysfs, registered to the specified class.
1774 *
1775 * A "dev" file will be created, showing the dev_t for the device, if
1776 * the dev_t is not 0,0.
1777 * If a pointer to a parent struct device is passed in, the newly created
1778 * struct device will be a child of that device in sysfs.
1779 * The pointer to the struct device will be returned from the call.
1780 * Any further sysfs files that might be required can be created using this
1781 * pointer.
1782 *
Jani Nikulaf0eae0e2010-03-11 18:11:45 +02001783 * Returns &struct device pointer on success, or ERR_PTR() on error.
1784 *
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07001785 * Note: the struct class passed to this function must have previously
1786 * been created with a call to class_create().
1787 */
Greg Kroah-Hartman4e106732008-07-21 20:03:34 -07001788struct device *device_create(struct class *class, struct device *parent,
1789 dev_t devt, void *drvdata, const char *fmt, ...)
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07001790{
1791 va_list vargs;
1792 struct device *dev;
1793
1794 va_start(vargs, fmt);
1795 dev = device_create_vargs(class, parent, devt, drvdata, fmt, vargs);
1796 va_end(vargs);
1797 return dev;
1798}
Greg Kroah-Hartman4e106732008-07-21 20:03:34 -07001799EXPORT_SYMBOL_GPL(device_create);
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07001800
Guenter Roeck39ef3112013-07-14 16:05:57 -07001801/**
1802 * device_create_with_groups - creates a device and registers it with sysfs
1803 * @class: pointer to the struct class that this device should be registered to
1804 * @parent: pointer to the parent struct device of this new device, if any
1805 * @devt: the dev_t for the char device to be added
1806 * @drvdata: the data to be added to the device for callbacks
1807 * @groups: NULL-terminated list of attribute groups to be created
1808 * @fmt: string for the device's name
1809 *
1810 * This function can be used by char device classes. A struct device
1811 * will be created in sysfs, registered to the specified class.
1812 * Additional attributes specified in the groups parameter will also
1813 * be created automatically.
1814 *
1815 * A "dev" file will be created, showing the dev_t for the device, if
1816 * the dev_t is not 0,0.
1817 * If a pointer to a parent struct device is passed in, the newly created
1818 * struct device will be a child of that device in sysfs.
1819 * The pointer to the struct device will be returned from the call.
1820 * Any further sysfs files that might be required can be created using this
1821 * pointer.
1822 *
1823 * Returns &struct device pointer on success, or ERR_PTR() on error.
1824 *
1825 * Note: the struct class passed to this function must have previously
1826 * been created with a call to class_create().
1827 */
1828struct device *device_create_with_groups(struct class *class,
1829 struct device *parent, dev_t devt,
1830 void *drvdata,
1831 const struct attribute_group **groups,
1832 const char *fmt, ...)
1833{
1834 va_list vargs;
1835 struct device *dev;
1836
1837 va_start(vargs, fmt);
1838 dev = device_create_groups_vargs(class, parent, devt, drvdata, groups,
1839 fmt, vargs);
1840 va_end(vargs);
1841 return dev;
1842}
1843EXPORT_SYMBOL_GPL(device_create_with_groups);
1844
Michał Mirosław9f3b7952013-02-01 20:40:17 +01001845static int __match_devt(struct device *dev, const void *data)
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001846{
Michał Mirosław9f3b7952013-02-01 20:40:17 +01001847 const dev_t *devt = data;
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001848
Dave Youngcd354492008-01-28 16:56:11 +08001849 return dev->devt == *devt;
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001850}
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001851
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001852/**
1853 * device_destroy - removes a device that was created with device_create()
1854 * @class: pointer to the struct class that this device was registered with
1855 * @devt: the dev_t of the device that was previously registered
1856 *
1857 * This call unregisters and cleans up a device that was created with a
1858 * call to device_create().
1859 */
1860void device_destroy(struct class *class, dev_t devt)
1861{
1862 struct device *dev;
1863
Greg Kroah-Hartman695794a2008-05-22 17:21:08 -04001864 dev = class_find_device(class, NULL, &devt, __match_devt);
Dave Youngcd354492008-01-28 16:56:11 +08001865 if (dev) {
1866 put_device(dev);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001867 device_unregister(dev);
Dave Youngcd354492008-01-28 16:56:11 +08001868 }
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001869}
1870EXPORT_SYMBOL_GPL(device_destroy);
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001871
1872/**
1873 * device_rename - renames a device
1874 * @dev: the pointer to the struct device to be renamed
1875 * @new_name: the new name of the device
Eric W. Biederman030c1d22008-05-08 14:41:00 -07001876 *
1877 * It is the responsibility of the caller to provide mutual
1878 * exclusion between two different calls of device_rename
1879 * on the same device to ensure that new_name is valid and
1880 * won't conflict with other devices.
Michael Ellermanc6c0ac62010-11-25 09:44:07 +11001881 *
Timur Tabia5462512010-12-13 14:08:52 -06001882 * Note: Don't call this function. Currently, the networking layer calls this
1883 * function, but that will change. The following text from Kay Sievers offers
1884 * some insight:
1885 *
1886 * Renaming devices is racy at many levels, symlinks and other stuff are not
1887 * replaced atomically, and you get a "move" uevent, but it's not easy to
1888 * connect the event to the old and new device. Device nodes are not renamed at
1889 * all, there isn't even support for that in the kernel now.
1890 *
1891 * In the meantime, during renaming, your target name might be taken by another
1892 * driver, creating conflicts. Or the old name is taken directly after you
1893 * renamed it -- then you get events for the same DEVPATH, before you even see
1894 * the "move" event. It's just a mess, and nothing new should ever rely on
1895 * kernel device renaming. Besides that, it's not even implemented now for
1896 * other things than (driver-core wise very simple) network devices.
1897 *
1898 * We are currently about to change network renaming in udev to completely
1899 * disallow renaming of devices in the same namespace as the kernel uses,
1900 * because we can't solve the problems properly, that arise with swapping names
1901 * of multiple interfaces without races. Means, renaming of eth[0-9]* will only
1902 * be allowed to some other name than eth[0-9]*, for the aforementioned
1903 * reasons.
1904 *
1905 * Make up a "real" name in the driver before you register anything, or add
1906 * some other attributes for userspace to find the device, or use udev to add
1907 * symlinks -- but never rename kernel devices later, it's a complete mess. We
1908 * don't even want to get into that and try to implement the missing pieces in
1909 * the core. We really have other pieces to fix in the driver core mess. :)
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001910 */
Johannes Berg6937e8f2010-08-05 17:38:18 +02001911int device_rename(struct device *dev, const char *new_name)
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001912{
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001913 char *old_device_name = NULL;
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001914 int error;
1915
1916 dev = get_device(dev);
1917 if (!dev)
1918 return -EINVAL;
1919
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01001920 pr_debug("device: '%s': %s: renaming to '%s'\n", dev_name(dev),
Harvey Harrison2b3a3022008-03-04 16:41:05 -08001921 __func__, new_name);
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001922
Kay Sievers1fa5ae82009-01-25 15:17:37 +01001923 old_device_name = kstrdup(dev_name(dev), GFP_KERNEL);
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001924 if (!old_device_name) {
1925 error = -ENOMEM;
1926 goto out;
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001927 }
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001928
Eric W. Biedermanf349cf32010-03-30 11:31:29 -07001929 if (dev->class) {
Kay Sievers6b6e39a2010-11-15 23:13:18 +01001930 error = sysfs_rename_link(&dev->class->p->subsys.kobj,
Eric W. Biedermanf349cf32010-03-30 11:31:29 -07001931 &dev->kobj, old_device_name, new_name);
1932 if (error)
1933 goto out;
1934 }
Kay Sievers39aba962010-09-04 22:33:14 -07001935
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001936 error = kobject_rename(&dev->kobj, new_name);
Kay Sievers1fa5ae82009-01-25 15:17:37 +01001937 if (error)
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001938 goto out;
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001939
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001940out:
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001941 put_device(dev);
1942
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001943 kfree(old_device_name);
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001944
1945 return error;
1946}
Johannes Berga2807db2007-02-28 12:38:31 +01001947EXPORT_SYMBOL_GPL(device_rename);
Cornelia Huck8a824722006-11-20 17:07:51 +01001948
1949static int device_move_class_links(struct device *dev,
1950 struct device *old_parent,
1951 struct device *new_parent)
1952{
Greg Kroah-Hartmanf7f34612007-03-06 12:55:53 -08001953 int error = 0;
Cornelia Huck8a824722006-11-20 17:07:51 +01001954
Greg Kroah-Hartmanf7f34612007-03-06 12:55:53 -08001955 if (old_parent)
1956 sysfs_remove_link(&dev->kobj, "device");
1957 if (new_parent)
1958 error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
1959 "device");
1960 return error;
Cornelia Huck8a824722006-11-20 17:07:51 +01001961}
1962
1963/**
1964 * device_move - moves a device to a new parent
1965 * @dev: the pointer to the struct device to be moved
Cornelia Huckc744aeae2007-01-08 20:16:44 +01001966 * @new_parent: the new parent of the device (can by NULL)
Cornelia Huckffa6a702009-03-04 12:44:00 +01001967 * @dpm_order: how to reorder the dpm_list
Cornelia Huck8a824722006-11-20 17:07:51 +01001968 */
Cornelia Huckffa6a702009-03-04 12:44:00 +01001969int device_move(struct device *dev, struct device *new_parent,
1970 enum dpm_order dpm_order)
Cornelia Huck8a824722006-11-20 17:07:51 +01001971{
1972 int error;
1973 struct device *old_parent;
Cornelia Huckc744aeae2007-01-08 20:16:44 +01001974 struct kobject *new_parent_kobj;
Cornelia Huck8a824722006-11-20 17:07:51 +01001975
1976 dev = get_device(dev);
1977 if (!dev)
1978 return -EINVAL;
1979
Cornelia Huckffa6a702009-03-04 12:44:00 +01001980 device_pm_lock();
Cornelia Huck8a824722006-11-20 17:07:51 +01001981 new_parent = get_device(new_parent);
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001982 new_parent_kobj = get_device_parent(dev, new_parent);
Cornelia Huck63b69712008-01-21 16:09:44 +01001983
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01001984 pr_debug("device: '%s': %s: moving to '%s'\n", dev_name(dev),
1985 __func__, new_parent ? dev_name(new_parent) : "<NULL>");
Cornelia Huckc744aeae2007-01-08 20:16:44 +01001986 error = kobject_move(&dev->kobj, new_parent_kobj);
Cornelia Huck8a824722006-11-20 17:07:51 +01001987 if (error) {
Cornelia Huck63b69712008-01-21 16:09:44 +01001988 cleanup_glue_dir(dev, new_parent_kobj);
Cornelia Huck8a824722006-11-20 17:07:51 +01001989 put_device(new_parent);
1990 goto out;
1991 }
1992 old_parent = dev->parent;
1993 dev->parent = new_parent;
1994 if (old_parent)
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08001995 klist_remove(&dev->p->knode_parent);
Yinghai Lu0d358f22008-02-19 03:20:41 -08001996 if (new_parent) {
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08001997 klist_add_tail(&dev->p->knode_parent,
1998 &new_parent->p->klist_children);
Yinghai Lu0d358f22008-02-19 03:20:41 -08001999 set_dev_node(dev, dev_to_node(new_parent));
2000 }
2001
Rabin Vincentbdd40342012-04-23 09:16:36 +02002002 if (dev->class) {
2003 error = device_move_class_links(dev, old_parent, new_parent);
2004 if (error) {
2005 /* We ignore errors on cleanup since we're hosed anyway... */
2006 device_move_class_links(dev, new_parent, old_parent);
2007 if (!kobject_move(&dev->kobj, &old_parent->kobj)) {
2008 if (new_parent)
2009 klist_remove(&dev->p->knode_parent);
2010 dev->parent = old_parent;
2011 if (old_parent) {
2012 klist_add_tail(&dev->p->knode_parent,
2013 &old_parent->p->klist_children);
2014 set_dev_node(dev, dev_to_node(old_parent));
2015 }
Yinghai Lu0d358f22008-02-19 03:20:41 -08002016 }
Rabin Vincentbdd40342012-04-23 09:16:36 +02002017 cleanup_glue_dir(dev, new_parent_kobj);
2018 put_device(new_parent);
2019 goto out;
Cornelia Huck8a824722006-11-20 17:07:51 +01002020 }
Cornelia Huck8a824722006-11-20 17:07:51 +01002021 }
Cornelia Huckffa6a702009-03-04 12:44:00 +01002022 switch (dpm_order) {
2023 case DPM_ORDER_NONE:
2024 break;
2025 case DPM_ORDER_DEV_AFTER_PARENT:
2026 device_pm_move_after(dev, new_parent);
2027 break;
2028 case DPM_ORDER_PARENT_BEFORE_DEV:
2029 device_pm_move_before(new_parent, dev);
2030 break;
2031 case DPM_ORDER_DEV_LAST:
2032 device_pm_move_last(dev);
2033 break;
2034 }
Rabin Vincentbdd40342012-04-23 09:16:36 +02002035
Cornelia Huck8a824722006-11-20 17:07:51 +01002036 put_device(old_parent);
2037out:
Cornelia Huckffa6a702009-03-04 12:44:00 +01002038 device_pm_unlock();
Cornelia Huck8a824722006-11-20 17:07:51 +01002039 put_device(dev);
2040 return error;
2041}
Cornelia Huck8a824722006-11-20 17:07:51 +01002042EXPORT_SYMBOL_GPL(device_move);
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08002043
2044/**
2045 * device_shutdown - call ->shutdown() on each device to shutdown.
2046 */
2047void device_shutdown(void)
2048{
Hugh Daschbach62458382010-03-22 10:36:37 -07002049 struct device *dev;
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08002050
Hugh Daschbach62458382010-03-22 10:36:37 -07002051 spin_lock(&devices_kset->list_lock);
2052 /*
2053 * Walk the devices list backward, shutting down each in turn.
2054 * Beware that device unplug events may also start pulling
2055 * devices offline, even as the system is shutting down.
2056 */
2057 while (!list_empty(&devices_kset->list)) {
2058 dev = list_entry(devices_kset->list.prev, struct device,
2059 kobj.entry);
Ming Leid1c6c032012-06-22 18:01:40 +08002060
2061 /*
2062 * hold reference count of device's parent to
2063 * prevent it from being freed because parent's
2064 * lock is to be held
2065 */
2066 get_device(dev->parent);
Hugh Daschbach62458382010-03-22 10:36:37 -07002067 get_device(dev);
2068 /*
2069 * Make sure the device is off the kset list, in the
2070 * event that dev->*->shutdown() doesn't remove it.
2071 */
2072 list_del_init(&dev->kobj.entry);
2073 spin_unlock(&devices_kset->list_lock);
Alan Sternfe6b91f2011-12-06 23:24:52 +01002074
Ming Leid1c6c032012-06-22 18:01:40 +08002075 /* hold lock to avoid race with probe/release */
2076 if (dev->parent)
2077 device_lock(dev->parent);
2078 device_lock(dev);
2079
Alan Sternfe6b91f2011-12-06 23:24:52 +01002080 /* Don't allow any more runtime suspends */
2081 pm_runtime_get_noresume(dev);
2082 pm_runtime_barrier(dev);
Hugh Daschbach62458382010-03-22 10:36:37 -07002083
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08002084 if (dev->bus && dev->bus->shutdown) {
ShuoX Liu0246c4f2012-11-23 15:14:12 +08002085 if (initcall_debug)
2086 dev_info(dev, "shutdown\n");
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08002087 dev->bus->shutdown(dev);
2088 } else if (dev->driver && dev->driver->shutdown) {
ShuoX Liu0246c4f2012-11-23 15:14:12 +08002089 if (initcall_debug)
2090 dev_info(dev, "shutdown\n");
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08002091 dev->driver->shutdown(dev);
2092 }
Ming Leid1c6c032012-06-22 18:01:40 +08002093
2094 device_unlock(dev);
2095 if (dev->parent)
2096 device_unlock(dev->parent);
2097
Hugh Daschbach62458382010-03-22 10:36:37 -07002098 put_device(dev);
Ming Leid1c6c032012-06-22 18:01:40 +08002099 put_device(dev->parent);
Hugh Daschbach62458382010-03-22 10:36:37 -07002100
2101 spin_lock(&devices_kset->list_lock);
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08002102 }
Hugh Daschbach62458382010-03-22 10:36:37 -07002103 spin_unlock(&devices_kset->list_lock);
Shaohua Li401097e2009-05-12 13:37:57 -07002104 async_synchronize_full();
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08002105}
Joe Perches99bcf212010-06-27 01:02:34 +00002106
2107/*
2108 * Device logging functions
2109 */
2110
2111#ifdef CONFIG_PRINTK
Joe Perches666f3552012-09-12 20:14:11 -07002112static int
2113create_syslog_header(const struct device *dev, char *hdr, size_t hdrlen)
Joe Perches99bcf212010-06-27 01:02:34 +00002114{
Kay Sieversc4e00da2012-05-03 02:29:59 +02002115 const char *subsys;
Joe Perches798efc62012-09-12 20:11:29 -07002116 size_t pos = 0;
Joe Perches99bcf212010-06-27 01:02:34 +00002117
Kay Sieversc4e00da2012-05-03 02:29:59 +02002118 if (dev->class)
2119 subsys = dev->class->name;
2120 else if (dev->bus)
2121 subsys = dev->bus->name;
2122 else
Joe Perches798efc62012-09-12 20:11:29 -07002123 return 0;
Kay Sieversc4e00da2012-05-03 02:29:59 +02002124
Joe Perches798efc62012-09-12 20:11:29 -07002125 pos += snprintf(hdr + pos, hdrlen - pos, "SUBSYSTEM=%s", subsys);
Kay Sieversc4e00da2012-05-03 02:29:59 +02002126
2127 /*
2128 * Add device identifier DEVICE=:
2129 * b12:8 block dev_t
2130 * c127:3 char dev_t
2131 * n8 netdev ifindex
2132 * +sound:card0 subsystem:devname
2133 */
2134 if (MAJOR(dev->devt)) {
2135 char c;
2136
2137 if (strcmp(subsys, "block") == 0)
2138 c = 'b';
2139 else
2140 c = 'c';
Joe Perches798efc62012-09-12 20:11:29 -07002141 pos++;
2142 pos += snprintf(hdr + pos, hdrlen - pos,
2143 "DEVICE=%c%u:%u",
2144 c, MAJOR(dev->devt), MINOR(dev->devt));
Kay Sieversc4e00da2012-05-03 02:29:59 +02002145 } else if (strcmp(subsys, "net") == 0) {
2146 struct net_device *net = to_net_dev(dev);
2147
Joe Perches798efc62012-09-12 20:11:29 -07002148 pos++;
2149 pos += snprintf(hdr + pos, hdrlen - pos,
2150 "DEVICE=n%u", net->ifindex);
Kay Sieversc4e00da2012-05-03 02:29:59 +02002151 } else {
Joe Perches798efc62012-09-12 20:11:29 -07002152 pos++;
2153 pos += snprintf(hdr + pos, hdrlen - pos,
2154 "DEVICE=+%s:%s", subsys, dev_name(dev));
Kay Sieversc4e00da2012-05-03 02:29:59 +02002155 }
Jim Cromieaf7f2152012-07-19 13:46:21 -06002156
Joe Perches798efc62012-09-12 20:11:29 -07002157 return pos;
Joe Perches99bcf212010-06-27 01:02:34 +00002158}
Joe Perches798efc62012-09-12 20:11:29 -07002159EXPORT_SYMBOL(create_syslog_header);
2160
Joe Perches05e4e5b2012-09-12 20:13:37 -07002161int dev_vprintk_emit(int level, const struct device *dev,
2162 const char *fmt, va_list args)
2163{
2164 char hdr[128];
2165 size_t hdrlen;
2166
2167 hdrlen = create_syslog_header(dev, hdr, sizeof(hdr));
2168
2169 return vprintk_emit(0, level, hdrlen ? hdr : NULL, hdrlen, fmt, args);
2170}
2171EXPORT_SYMBOL(dev_vprintk_emit);
2172
2173int dev_printk_emit(int level, const struct device *dev, const char *fmt, ...)
2174{
2175 va_list args;
2176 int r;
2177
2178 va_start(args, fmt);
2179
2180 r = dev_vprintk_emit(level, dev, fmt, args);
2181
2182 va_end(args);
2183
2184 return r;
2185}
2186EXPORT_SYMBOL(dev_printk_emit);
2187
Joe Perches798efc62012-09-12 20:11:29 -07002188static int __dev_printk(const char *level, const struct device *dev,
2189 struct va_format *vaf)
2190{
Joe Perches798efc62012-09-12 20:11:29 -07002191 if (!dev)
2192 return printk("%s(NULL device *): %pV", level, vaf);
2193
Joe Perches666f3552012-09-12 20:14:11 -07002194 return dev_printk_emit(level[1] - '0', dev,
2195 "%s %s: %pV",
2196 dev_driver_string(dev), dev_name(dev), vaf);
Joe Perches798efc62012-09-12 20:11:29 -07002197}
Joe Perches99bcf212010-06-27 01:02:34 +00002198
2199int dev_printk(const char *level, const struct device *dev,
2200 const char *fmt, ...)
2201{
2202 struct va_format vaf;
2203 va_list args;
2204 int r;
2205
2206 va_start(args, fmt);
2207
2208 vaf.fmt = fmt;
2209 vaf.va = &args;
2210
2211 r = __dev_printk(level, dev, &vaf);
Joe Perches798efc62012-09-12 20:11:29 -07002212
Joe Perches99bcf212010-06-27 01:02:34 +00002213 va_end(args);
2214
2215 return r;
2216}
2217EXPORT_SYMBOL(dev_printk);
2218
2219#define define_dev_printk_level(func, kern_level) \
2220int func(const struct device *dev, const char *fmt, ...) \
2221{ \
2222 struct va_format vaf; \
2223 va_list args; \
2224 int r; \
2225 \
2226 va_start(args, fmt); \
2227 \
2228 vaf.fmt = fmt; \
2229 vaf.va = &args; \
2230 \
2231 r = __dev_printk(kern_level, dev, &vaf); \
Joe Perches798efc62012-09-12 20:11:29 -07002232 \
Joe Perches99bcf212010-06-27 01:02:34 +00002233 va_end(args); \
2234 \
2235 return r; \
2236} \
2237EXPORT_SYMBOL(func);
2238
2239define_dev_printk_level(dev_emerg, KERN_EMERG);
2240define_dev_printk_level(dev_alert, KERN_ALERT);
2241define_dev_printk_level(dev_crit, KERN_CRIT);
2242define_dev_printk_level(dev_err, KERN_ERR);
2243define_dev_printk_level(dev_warn, KERN_WARNING);
2244define_dev_printk_level(dev_notice, KERN_NOTICE);
2245define_dev_printk_level(_dev_info, KERN_INFO);
2246
2247#endif