blob: 921b94184dcc0f2b0e1101fb87996b5fe1f7fc04 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * drivers/base/core.c - core driver model code (device registration, etc)
3 *
4 * Copyright (c) 2002-3 Patrick Mochel
5 * Copyright (c) 2002-3 Open Source Development Labs
Greg Kroah-Hartman64bb5d22006-06-28 16:19:58 -07006 * Copyright (c) 2006 Greg Kroah-Hartman <gregkh@suse.de>
7 * Copyright (c) 2006 Novell, Inc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 *
9 * This file is released under the GPLv2
10 *
11 */
12
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/device.h>
14#include <linux/err.h>
15#include <linux/init.h>
16#include <linux/module.h>
17#include <linux/slab.h>
18#include <linux/string.h>
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -070019#include <linux/kdev_t.h>
Benjamin Herrenschmidt116af372006-10-25 13:44:59 +100020#include <linux/notifier.h>
Grant Likely07d57a32012-02-01 11:22:22 -070021#include <linux/of.h>
22#include <linux/of_device.h>
Kay Sieversda231fd2007-11-21 17:29:15 +010023#include <linux/genhd.h>
Andrew Morton815d2d52008-03-04 15:09:07 -080024#include <linux/kallsyms.h>
Dave Youngf75b1c62008-05-28 09:28:39 -070025#include <linux/mutex.h>
Shaohua Li401097e2009-05-12 13:37:57 -070026#include <linux/async.h>
Peter Chenaf8db152011-11-15 21:52:29 +010027#include <linux/pm_runtime.h>
Kay Sieversc4e00da2012-05-03 02:29:59 +020028#include <linux/netdevice.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
30#include "base.h"
31#include "power/power.h"
32
Andi Kleene52eec12010-09-08 16:54:17 +020033#ifdef CONFIG_SYSFS_DEPRECATED
34#ifdef CONFIG_SYSFS_DEPRECATED_V2
35long sysfs_deprecated = 1;
36#else
37long sysfs_deprecated = 0;
38#endif
Hanjun Guo3454bf92013-08-17 20:42:24 +080039static int __init sysfs_deprecated_setup(char *arg)
Andi Kleene52eec12010-09-08 16:54:17 +020040{
Jingoo Han34da5e62013-07-26 13:10:22 +090041 return kstrtol(arg, 10, &sysfs_deprecated);
Andi Kleene52eec12010-09-08 16:54:17 +020042}
43early_param("sysfs.deprecated", sysfs_deprecated_setup);
44#endif
45
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -080046int (*platform_notify)(struct device *dev) = NULL;
47int (*platform_notify_remove)(struct device *dev) = NULL;
Dan Williamse105b8b2008-04-21 10:51:07 -070048static struct kobject *dev_kobj;
49struct kobject *sysfs_dev_char_kobj;
50struct kobject *sysfs_dev_block_kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Greg Kroah-Hartman4e886c22008-01-27 12:12:43 -080052#ifdef CONFIG_BLOCK
53static inline int device_is_not_partition(struct device *dev)
54{
55 return !(dev->type == &part_type);
56}
57#else
58static inline int device_is_not_partition(struct device *dev)
59{
60 return 1;
61}
62#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Alan Stern3e956372006-06-16 17:10:48 -040064/**
65 * dev_driver_string - Return a device's driver name, if at all possible
66 * @dev: struct device to get the name of
67 *
68 * Will return the device's driver's name if it is bound to a device. If
yan9169c012012-04-20 21:08:45 +080069 * the device is not bound to a driver, it will return the name of the bus
Alan Stern3e956372006-06-16 17:10:48 -040070 * it is attached to. If it is not attached to a bus either, an empty
71 * string will be returned.
72 */
Jean Delvarebf9ca692008-07-30 12:29:21 -070073const char *dev_driver_string(const struct device *dev)
Alan Stern3e956372006-06-16 17:10:48 -040074{
Alan Stern35899722009-12-04 11:06:57 -050075 struct device_driver *drv;
76
77 /* dev->driver can change to NULL underneath us because of unbinding,
78 * so be careful about accessing it. dev->bus and dev->class should
79 * never change once they are set, so they don't need special care.
80 */
81 drv = ACCESS_ONCE(dev->driver);
82 return drv ? drv->name :
Jean Delvarea456b702007-03-09 16:33:10 +010083 (dev->bus ? dev->bus->name :
84 (dev->class ? dev->class->name : ""));
Alan Stern3e956372006-06-16 17:10:48 -040085}
Matthew Wilcox310a9222006-09-23 23:35:04 -060086EXPORT_SYMBOL(dev_driver_string);
Alan Stern3e956372006-06-16 17:10:48 -040087
Linus Torvalds1da177e2005-04-16 15:20:36 -070088#define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr)
89
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -080090static ssize_t dev_attr_show(struct kobject *kobj, struct attribute *attr,
91 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070092{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -080093 struct device_attribute *dev_attr = to_dev_attr(attr);
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +020094 struct device *dev = kobj_to_dev(kobj);
Dmitry Torokhov4a0c20b2005-04-29 01:23:47 -050095 ssize_t ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
97 if (dev_attr->show)
Yani Ioannou54b6f352005-05-17 06:39:34 -040098 ret = dev_attr->show(dev, dev_attr, buf);
Andrew Morton815d2d52008-03-04 15:09:07 -080099 if (ret >= (ssize_t)PAGE_SIZE) {
Greg Kroah-Hartman53a9c872013-01-17 13:10:23 -0800100 print_symbol("dev_attr_show: %s returned bad count\n",
101 (unsigned long)dev_attr->show);
Andrew Morton815d2d52008-03-04 15:09:07 -0800102 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 return ret;
104}
105
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800106static ssize_t dev_attr_store(struct kobject *kobj, struct attribute *attr,
107 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800109 struct device_attribute *dev_attr = to_dev_attr(attr);
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200110 struct device *dev = kobj_to_dev(kobj);
Dmitry Torokhov4a0c20b2005-04-29 01:23:47 -0500111 ssize_t ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
113 if (dev_attr->store)
Yani Ioannou54b6f352005-05-17 06:39:34 -0400114 ret = dev_attr->store(dev, dev_attr, buf, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 return ret;
116}
117
Emese Revfy52cf25d2010-01-19 02:58:23 +0100118static const struct sysfs_ops dev_sysfs_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 .show = dev_attr_show,
120 .store = dev_attr_store,
121};
122
Kay Sieversca22e562011-12-14 14:29:38 -0800123#define to_ext_attr(x) container_of(x, struct dev_ext_attribute, attr)
124
125ssize_t device_store_ulong(struct device *dev,
126 struct device_attribute *attr,
127 const char *buf, size_t size)
128{
129 struct dev_ext_attribute *ea = to_ext_attr(attr);
130 char *end;
131 unsigned long new = simple_strtoul(buf, &end, 0);
132 if (end == buf)
133 return -EINVAL;
134 *(unsigned long *)(ea->var) = new;
135 /* Always return full write size even if we didn't consume all */
136 return size;
137}
138EXPORT_SYMBOL_GPL(device_store_ulong);
139
140ssize_t device_show_ulong(struct device *dev,
141 struct device_attribute *attr,
142 char *buf)
143{
144 struct dev_ext_attribute *ea = to_ext_attr(attr);
145 return snprintf(buf, PAGE_SIZE, "%lx\n", *(unsigned long *)(ea->var));
146}
147EXPORT_SYMBOL_GPL(device_show_ulong);
148
149ssize_t device_store_int(struct device *dev,
150 struct device_attribute *attr,
151 const char *buf, size_t size)
152{
153 struct dev_ext_attribute *ea = to_ext_attr(attr);
154 char *end;
155 long new = simple_strtol(buf, &end, 0);
156 if (end == buf || new > INT_MAX || new < INT_MIN)
157 return -EINVAL;
158 *(int *)(ea->var) = new;
159 /* Always return full write size even if we didn't consume all */
160 return size;
161}
162EXPORT_SYMBOL_GPL(device_store_int);
163
164ssize_t device_show_int(struct device *dev,
165 struct device_attribute *attr,
166 char *buf)
167{
168 struct dev_ext_attribute *ea = to_ext_attr(attr);
169
170 return snprintf(buf, PAGE_SIZE, "%d\n", *(int *)(ea->var));
171}
172EXPORT_SYMBOL_GPL(device_show_int);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
Borislav Petkov91872392012-10-09 19:52:05 +0200174ssize_t device_store_bool(struct device *dev, struct device_attribute *attr,
175 const char *buf, size_t size)
176{
177 struct dev_ext_attribute *ea = to_ext_attr(attr);
178
179 if (strtobool(buf, ea->var) < 0)
180 return -EINVAL;
181
182 return size;
183}
184EXPORT_SYMBOL_GPL(device_store_bool);
185
186ssize_t device_show_bool(struct device *dev, struct device_attribute *attr,
187 char *buf)
188{
189 struct dev_ext_attribute *ea = to_ext_attr(attr);
190
191 return snprintf(buf, PAGE_SIZE, "%d\n", *(bool *)(ea->var));
192}
193EXPORT_SYMBOL_GPL(device_show_bool);
194
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195/**
Robert P. J. Dayf8878dc2013-06-01 20:17:34 -0400196 * device_release - free device structure.
197 * @kobj: device's kobject.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 *
Robert P. J. Dayf8878dc2013-06-01 20:17:34 -0400199 * This is called once the reference count for the object
200 * reaches 0. We forward the call to the device's release
201 * method, which should handle actually freeing the structure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800203static void device_release(struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200205 struct device *dev = kobj_to_dev(kobj);
Greg Kroah-Hartmanfb069a52008-12-16 12:23:36 -0800206 struct device_private *p = dev->p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
Ming Leia525a3d2012-07-25 01:42:29 +0800208 /*
209 * Some platform devices are driven without driver attached
210 * and managed resources may have been acquired. Make sure
211 * all resources are released.
212 *
213 * Drivers still can add resources into device after device
214 * is deleted but alive, so release devres here to avoid
215 * possible memory leak.
216 */
217 devres_release_all(dev);
218
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 if (dev->release)
220 dev->release(dev);
Kay Sieversf9f852d2006-10-07 21:54:55 +0200221 else if (dev->type && dev->type->release)
222 dev->type->release(dev);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700223 else if (dev->class && dev->class->dev_release)
224 dev->class->dev_release(dev);
Arjan van de Venf810a5c2008-07-25 19:45:39 -0700225 else
226 WARN(1, KERN_ERR "Device '%s' does not have a release() "
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800227 "function, it is broken and must be fixed.\n",
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100228 dev_name(dev));
Greg Kroah-Hartmanfb069a52008-12-16 12:23:36 -0800229 kfree(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230}
231
Eric W. Biedermanbc451f22010-03-30 11:31:25 -0700232static const void *device_namespace(struct kobject *kobj)
233{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200234 struct device *dev = kobj_to_dev(kobj);
Eric W. Biedermanbc451f22010-03-30 11:31:25 -0700235 const void *ns = NULL;
236
237 if (dev->class && dev->class->ns_type)
238 ns = dev->class->namespace(dev);
239
240 return ns;
241}
242
Greg Kroah-Hartman8f4afc42007-10-11 10:47:49 -0600243static struct kobj_type device_ktype = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 .release = device_release,
245 .sysfs_ops = &dev_sysfs_ops,
Eric W. Biedermanbc451f22010-03-30 11:31:25 -0700246 .namespace = device_namespace,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247};
248
249
Kay Sievers312c0042005-11-16 09:00:00 +0100250static int dev_uevent_filter(struct kset *kset, struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251{
252 struct kobj_type *ktype = get_ktype(kobj);
253
Greg Kroah-Hartman8f4afc42007-10-11 10:47:49 -0600254 if (ktype == &device_ktype) {
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200255 struct device *dev = kobj_to_dev(kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 if (dev->bus)
257 return 1;
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700258 if (dev->class)
259 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 }
261 return 0;
262}
263
Kay Sievers312c0042005-11-16 09:00:00 +0100264static const char *dev_uevent_name(struct kset *kset, struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200266 struct device *dev = kobj_to_dev(kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700268 if (dev->bus)
269 return dev->bus->name;
270 if (dev->class)
271 return dev->class->name;
272 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273}
274
Kay Sievers7eff2e72007-08-14 15:15:12 +0200275static int dev_uevent(struct kset *kset, struct kobject *kobj,
276 struct kobj_uevent_env *env)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200278 struct device *dev = kobj_to_dev(kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 int retval = 0;
280
Kay Sievers6fcf53a2009-04-30 15:23:42 +0200281 /* add device node properties if present */
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700282 if (MAJOR(dev->devt)) {
Kay Sievers6fcf53a2009-04-30 15:23:42 +0200283 const char *tmp;
284 const char *name;
Al Viro2c9ede52011-07-23 20:24:48 -0400285 umode_t mode = 0;
Greg Kroah-Hartman4e4098a2013-04-11 11:43:29 -0700286 kuid_t uid = GLOBAL_ROOT_UID;
287 kgid_t gid = GLOBAL_ROOT_GID;
Kay Sievers6fcf53a2009-04-30 15:23:42 +0200288
Kay Sievers7eff2e72007-08-14 15:15:12 +0200289 add_uevent_var(env, "MAJOR=%u", MAJOR(dev->devt));
290 add_uevent_var(env, "MINOR=%u", MINOR(dev->devt));
Kay Sievers3c2670e2013-04-06 09:56:00 -0700291 name = device_get_devnode(dev, &mode, &uid, &gid, &tmp);
Kay Sievers6fcf53a2009-04-30 15:23:42 +0200292 if (name) {
293 add_uevent_var(env, "DEVNAME=%s", name);
Kay Sieverse454cea2009-09-18 23:01:12 +0200294 if (mode)
295 add_uevent_var(env, "DEVMODE=%#o", mode & 0777);
Greg Kroah-Hartman4e4098a2013-04-11 11:43:29 -0700296 if (!uid_eq(uid, GLOBAL_ROOT_UID))
297 add_uevent_var(env, "DEVUID=%u", from_kuid(&init_user_ns, uid));
298 if (!gid_eq(gid, GLOBAL_ROOT_GID))
299 add_uevent_var(env, "DEVGID=%u", from_kgid(&init_user_ns, gid));
Kay Sievers3c2670e2013-04-06 09:56:00 -0700300 kfree(tmp);
Kay Sievers6fcf53a2009-04-30 15:23:42 +0200301 }
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700302 }
303
Kay Sievers414264f2007-03-12 21:08:57 +0100304 if (dev->type && dev->type->name)
Kay Sievers7eff2e72007-08-14 15:15:12 +0200305 add_uevent_var(env, "DEVTYPE=%s", dev->type->name);
Kay Sievers414264f2007-03-12 21:08:57 +0100306
Kay Sievers239378f2006-10-07 21:54:55 +0200307 if (dev->driver)
Kay Sievers7eff2e72007-08-14 15:15:12 +0200308 add_uevent_var(env, "DRIVER=%s", dev->driver->name);
Kay Sievers239378f2006-10-07 21:54:55 +0200309
Grant Likely07d57a32012-02-01 11:22:22 -0700310 /* Add common DT information about the device */
311 of_device_uevent(dev, env);
312
Kay Sievers7eff2e72007-08-14 15:15:12 +0200313 /* have the bus specific function add its stuff */
Kay Sievers312c0042005-11-16 09:00:00 +0100314 if (dev->bus && dev->bus->uevent) {
Kay Sievers7eff2e72007-08-14 15:15:12 +0200315 retval = dev->bus->uevent(dev, env);
Kay Sieversf9f852d2006-10-07 21:54:55 +0200316 if (retval)
Greg Kroah-Hartman7dc72b22007-11-28 23:49:41 -0800317 pr_debug("device: '%s': %s: bus uevent() returned %d\n",
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100318 dev_name(dev), __func__, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 }
320
Kay Sievers7eff2e72007-08-14 15:15:12 +0200321 /* have the class specific function add its stuff */
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700322 if (dev->class && dev->class->dev_uevent) {
Kay Sievers7eff2e72007-08-14 15:15:12 +0200323 retval = dev->class->dev_uevent(dev, env);
Kay Sieversf9f852d2006-10-07 21:54:55 +0200324 if (retval)
Greg Kroah-Hartman7dc72b22007-11-28 23:49:41 -0800325 pr_debug("device: '%s': %s: class uevent() "
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100326 "returned %d\n", dev_name(dev),
Harvey Harrison2b3a3022008-03-04 16:41:05 -0800327 __func__, retval);
Kay Sieversf9f852d2006-10-07 21:54:55 +0200328 }
329
Stefan Weileef35c22010-08-06 21:11:15 +0200330 /* have the device type specific function add its stuff */
Kay Sieversf9f852d2006-10-07 21:54:55 +0200331 if (dev->type && dev->type->uevent) {
Kay Sievers7eff2e72007-08-14 15:15:12 +0200332 retval = dev->type->uevent(dev, env);
Kay Sieversf9f852d2006-10-07 21:54:55 +0200333 if (retval)
Greg Kroah-Hartman7dc72b22007-11-28 23:49:41 -0800334 pr_debug("device: '%s': %s: dev_type uevent() "
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100335 "returned %d\n", dev_name(dev),
Harvey Harrison2b3a3022008-03-04 16:41:05 -0800336 __func__, retval);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700337 }
338
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 return retval;
340}
341
Emese Revfy9cd43612009-12-31 14:52:51 +0100342static const struct kset_uevent_ops device_uevent_ops = {
Kay Sievers312c0042005-11-16 09:00:00 +0100343 .filter = dev_uevent_filter,
344 .name = dev_uevent_name,
345 .uevent = dev_uevent,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346};
347
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -0700348static ssize_t uevent_show(struct device *dev, struct device_attribute *attr,
Kay Sievers16574dc2007-04-06 01:40:38 +0200349 char *buf)
350{
351 struct kobject *top_kobj;
352 struct kset *kset;
Kay Sievers7eff2e72007-08-14 15:15:12 +0200353 struct kobj_uevent_env *env = NULL;
Kay Sievers16574dc2007-04-06 01:40:38 +0200354 int i;
355 size_t count = 0;
356 int retval;
357
358 /* search the kset, the device belongs to */
359 top_kobj = &dev->kobj;
Kay Sievers5c5daf62007-08-12 20:43:55 +0200360 while (!top_kobj->kset && top_kobj->parent)
361 top_kobj = top_kobj->parent;
Kay Sievers16574dc2007-04-06 01:40:38 +0200362 if (!top_kobj->kset)
363 goto out;
Kay Sievers5c5daf62007-08-12 20:43:55 +0200364
Kay Sievers16574dc2007-04-06 01:40:38 +0200365 kset = top_kobj->kset;
366 if (!kset->uevent_ops || !kset->uevent_ops->uevent)
367 goto out;
368
369 /* respect filter */
370 if (kset->uevent_ops && kset->uevent_ops->filter)
371 if (!kset->uevent_ops->filter(kset, &dev->kobj))
372 goto out;
373
Kay Sievers7eff2e72007-08-14 15:15:12 +0200374 env = kzalloc(sizeof(struct kobj_uevent_env), GFP_KERNEL);
375 if (!env)
Greg Kroah-Hartmanc7308c82007-05-02 14:14:11 +0200376 return -ENOMEM;
377
Kay Sievers16574dc2007-04-06 01:40:38 +0200378 /* let the kset specific function add its keys */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200379 retval = kset->uevent_ops->uevent(kset, &dev->kobj, env);
Kay Sievers16574dc2007-04-06 01:40:38 +0200380 if (retval)
381 goto out;
382
383 /* copy keys to file */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200384 for (i = 0; i < env->envp_idx; i++)
385 count += sprintf(&buf[count], "%s\n", env->envp[i]);
Kay Sievers16574dc2007-04-06 01:40:38 +0200386out:
Kay Sievers7eff2e72007-08-14 15:15:12 +0200387 kfree(env);
Kay Sievers16574dc2007-04-06 01:40:38 +0200388 return count;
389}
390
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -0700391static ssize_t uevent_store(struct device *dev, struct device_attribute *attr,
Kay Sieversa7fd6702005-10-01 14:49:43 +0200392 const char *buf, size_t count)
393{
Kay Sievers60a96a52007-07-08 22:29:26 +0200394 enum kobject_action action;
395
Kay Sievers3f5468c2010-01-14 22:54:37 +0100396 if (kobject_action_type(buf, count, &action) == 0)
Kay Sievers60a96a52007-07-08 22:29:26 +0200397 kobject_uevent(&dev->kobj, action);
Kay Sievers3f5468c2010-01-14 22:54:37 +0100398 else
399 dev_err(dev, "uevent: unknown action-string\n");
Kay Sieversa7fd6702005-10-01 14:49:43 +0200400 return count;
401}
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -0700402static DEVICE_ATTR_RW(uevent);
Kay Sieversa7fd6702005-10-01 14:49:43 +0200403
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -0700404static ssize_t online_show(struct device *dev, struct device_attribute *attr,
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +0200405 char *buf)
406{
407 bool val;
408
409 lock_device_hotplug();
410 val = !dev->offline;
411 unlock_device_hotplug();
412 return sprintf(buf, "%u\n", val);
413}
414
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -0700415static ssize_t online_store(struct device *dev, struct device_attribute *attr,
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +0200416 const char *buf, size_t count)
417{
418 bool val;
419 int ret;
420
421 ret = strtobool(buf, &val);
422 if (ret < 0)
423 return ret;
424
425 lock_device_hotplug();
426 ret = val ? device_online(dev) : device_offline(dev);
427 unlock_device_hotplug();
428 return ret < 0 ? ret : count;
429}
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -0700430static DEVICE_ATTR_RW(online);
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +0200431
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500432static int device_add_attributes(struct device *dev,
433 struct device_attribute *attrs)
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -0700434{
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -0700435 int error = 0;
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500436 int i;
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -0700437
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500438 if (attrs) {
Greg Kroah-Hartman3e1026b2013-08-22 10:25:34 -0700439 for (i = 0; attrs[i].attr.name; i++) {
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500440 error = device_create_file(dev, &attrs[i]);
441 if (error)
442 break;
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -0700443 }
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500444 if (error)
445 while (--i >= 0)
446 device_remove_file(dev, &attrs[i]);
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -0700447 }
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -0700448 return error;
449}
450
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500451static void device_remove_attributes(struct device *dev,
452 struct device_attribute *attrs)
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -0700453{
454 int i;
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500455
456 if (attrs)
Greg Kroah-Hartman3e1026b2013-08-22 10:25:34 -0700457 for (i = 0; attrs[i].attr.name; i++)
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500458 device_remove_file(dev, &attrs[i]);
459}
460
Stefan Achatzc97415a2010-11-26 19:57:29 +0000461static int device_add_bin_attributes(struct device *dev,
462 struct bin_attribute *attrs)
463{
464 int error = 0;
465 int i;
466
467 if (attrs) {
Greg Kroah-Hartman3e1026b2013-08-22 10:25:34 -0700468 for (i = 0; attrs[i].attr.name; i++) {
Stefan Achatzc97415a2010-11-26 19:57:29 +0000469 error = device_create_bin_file(dev, &attrs[i]);
470 if (error)
471 break;
472 }
473 if (error)
474 while (--i >= 0)
475 device_remove_bin_file(dev, &attrs[i]);
476 }
477 return error;
478}
479
480static void device_remove_bin_attributes(struct device *dev,
481 struct bin_attribute *attrs)
482{
483 int i;
484
485 if (attrs)
Greg Kroah-Hartman3e1026b2013-08-22 10:25:34 -0700486 for (i = 0; attrs[i].attr.name; i++)
Stefan Achatzc97415a2010-11-26 19:57:29 +0000487 device_remove_bin_file(dev, &attrs[i]);
488}
489
Greg Kroah-Hartmanfa6fdb32013-08-08 15:22:55 -0700490int device_add_groups(struct device *dev, const struct attribute_group **groups)
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500491{
Greg Kroah-Hartman3e9b2ba2013-08-21 13:47:50 -0700492 return sysfs_create_groups(&dev->kobj, groups);
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500493}
494
Greg Kroah-Hartmanfa6fdb32013-08-08 15:22:55 -0700495void device_remove_groups(struct device *dev,
496 const struct attribute_group **groups)
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500497{
Greg Kroah-Hartman3e9b2ba2013-08-21 13:47:50 -0700498 sysfs_remove_groups(&dev->kobj, groups);
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -0700499}
500
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700501static int device_add_attrs(struct device *dev)
502{
503 struct class *class = dev->class;
Stephen Hemmingeraed65af2011-03-28 09:12:52 -0700504 const struct device_type *type = dev->type;
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500505 int error;
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700506
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500507 if (class) {
Greg Kroah-Hartmand05a6f92013-07-14 16:05:58 -0700508 error = device_add_groups(dev, class->dev_groups);
Kay Sieversf9f852d2006-10-07 21:54:55 +0200509 if (error)
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500510 return error;
Greg Kroah-Hartmand05a6f92013-07-14 16:05:58 -0700511 error = device_add_attributes(dev, class->dev_attrs);
512 if (error)
513 goto err_remove_class_groups;
Stefan Achatzc97415a2010-11-26 19:57:29 +0000514 error = device_add_bin_attributes(dev, class->dev_bin_attrs);
515 if (error)
516 goto err_remove_class_attrs;
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700517 }
Kay Sieversf9f852d2006-10-07 21:54:55 +0200518
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500519 if (type) {
520 error = device_add_groups(dev, type->groups);
Kay Sieversf9f852d2006-10-07 21:54:55 +0200521 if (error)
Stefan Achatzc97415a2010-11-26 19:57:29 +0000522 goto err_remove_class_bin_attrs;
Kay Sieversf9f852d2006-10-07 21:54:55 +0200523 }
524
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500525 error = device_add_groups(dev, dev->groups);
526 if (error)
527 goto err_remove_type_groups;
528
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +0200529 if (device_supports_offline(dev) && !dev->offline_disabled) {
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -0700530 error = device_create_file(dev, &dev_attr_online);
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +0200531 if (error)
532 goto err_remove_type_groups;
533 }
534
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500535 return 0;
536
537 err_remove_type_groups:
538 if (type)
539 device_remove_groups(dev, type->groups);
Stefan Achatzc97415a2010-11-26 19:57:29 +0000540 err_remove_class_bin_attrs:
541 if (class)
542 device_remove_bin_attributes(dev, class->dev_bin_attrs);
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500543 err_remove_class_attrs:
544 if (class)
545 device_remove_attributes(dev, class->dev_attrs);
Greg Kroah-Hartmand05a6f92013-07-14 16:05:58 -0700546 err_remove_class_groups:
547 if (class)
548 device_remove_groups(dev, class->dev_groups);
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500549
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700550 return error;
551}
552
553static void device_remove_attrs(struct device *dev)
554{
555 struct class *class = dev->class;
Stephen Hemmingeraed65af2011-03-28 09:12:52 -0700556 const struct device_type *type = dev->type;
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700557
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -0700558 device_remove_file(dev, &dev_attr_online);
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500559 device_remove_groups(dev, dev->groups);
Kay Sieversf9f852d2006-10-07 21:54:55 +0200560
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500561 if (type)
562 device_remove_groups(dev, type->groups);
563
Stefan Achatzc97415a2010-11-26 19:57:29 +0000564 if (class) {
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500565 device_remove_attributes(dev, class->dev_attrs);
Stefan Achatzc97415a2010-11-26 19:57:29 +0000566 device_remove_bin_attributes(dev, class->dev_bin_attrs);
Greg Kroah-Hartmand05a6f92013-07-14 16:05:58 -0700567 device_remove_groups(dev, class->dev_groups);
Stefan Achatzc97415a2010-11-26 19:57:29 +0000568 }
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700569}
570
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -0700571static ssize_t dev_show(struct device *dev, struct device_attribute *attr,
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700572 char *buf)
573{
574 return print_dev_t(buf, dev->devt);
575}
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -0700576static DEVICE_ATTR_RO(dev);
Tejun Heoad6a1e12007-06-14 03:45:17 +0900577
Kay Sieversca22e562011-12-14 14:29:38 -0800578/* /sys/devices/ */
Greg Kroah-Hartman881c6cfd2007-11-01 09:29:06 -0600579struct kset *devices_kset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800582 * device_create_file - create sysfs attribute file for device.
583 * @dev: device.
584 * @attr: device attribute descriptor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 */
Phil Carmody26579ab2009-12-18 15:34:19 +0200586int device_create_file(struct device *dev,
587 const struct device_attribute *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588{
589 int error = 0;
Felipe Balbi8f46baa2013-02-20 10:31:42 +0200590
591 if (dev) {
592 WARN(((attr->attr.mode & S_IWUGO) && !attr->store),
dyoung@redhat.com97521972013-05-16 14:31:30 +0800593 "Attribute %s: write permission without 'store'\n",
594 attr->attr.name);
Felipe Balbi8f46baa2013-02-20 10:31:42 +0200595 WARN(((attr->attr.mode & S_IRUGO) && !attr->show),
dyoung@redhat.com97521972013-05-16 14:31:30 +0800596 "Attribute %s: read permission without 'show'\n",
597 attr->attr.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 error = sysfs_create_file(&dev->kobj, &attr->attr);
Felipe Balbi8f46baa2013-02-20 10:31:42 +0200599 }
600
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 return error;
602}
David Graham White86df2682013-07-21 20:41:14 -0400603EXPORT_SYMBOL_GPL(device_create_file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
605/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800606 * device_remove_file - remove sysfs attribute file.
607 * @dev: device.
608 * @attr: device attribute descriptor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 */
Phil Carmody26579ab2009-12-18 15:34:19 +0200610void device_remove_file(struct device *dev,
611 const struct device_attribute *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612{
Cornelia Huck0c98b192008-01-31 10:39:38 +0100613 if (dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 sysfs_remove_file(&dev->kobj, &attr->attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615}
David Graham White86df2682013-07-21 20:41:14 -0400616EXPORT_SYMBOL_GPL(device_remove_file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617
Greg Kroah-Hartman2589f1882006-09-19 09:39:19 -0700618/**
619 * device_create_bin_file - create sysfs binary attribute file for device.
620 * @dev: device.
621 * @attr: device binary attribute descriptor.
622 */
Phil Carmody66ecb922009-12-18 15:34:20 +0200623int device_create_bin_file(struct device *dev,
624 const struct bin_attribute *attr)
Greg Kroah-Hartman2589f1882006-09-19 09:39:19 -0700625{
626 int error = -EINVAL;
627 if (dev)
628 error = sysfs_create_bin_file(&dev->kobj, attr);
629 return error;
630}
631EXPORT_SYMBOL_GPL(device_create_bin_file);
632
633/**
634 * device_remove_bin_file - remove sysfs binary attribute file
635 * @dev: device.
636 * @attr: device binary attribute descriptor.
637 */
Phil Carmody66ecb922009-12-18 15:34:20 +0200638void device_remove_bin_file(struct device *dev,
639 const struct bin_attribute *attr)
Greg Kroah-Hartman2589f1882006-09-19 09:39:19 -0700640{
641 if (dev)
642 sysfs_remove_bin_file(&dev->kobj, attr);
643}
644EXPORT_SYMBOL_GPL(device_remove_bin_file);
645
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400646/**
Alan Stern523ded72007-04-26 00:12:04 -0700647 * device_schedule_callback_owner - helper to schedule a callback for a device
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400648 * @dev: device.
649 * @func: callback function to invoke later.
Alan Stern523ded72007-04-26 00:12:04 -0700650 * @owner: module owning the callback routine
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400651 *
652 * Attribute methods must not unregister themselves or their parent device
653 * (which would amount to the same thing). Attempts to do so will deadlock,
654 * since unregistration is mutually exclusive with driver callbacks.
655 *
656 * Instead methods can call this routine, which will attempt to allocate
657 * and schedule a workqueue request to call back @func with @dev as its
658 * argument in the workqueue's process context. @dev will be pinned until
659 * @func returns.
660 *
Alan Stern523ded72007-04-26 00:12:04 -0700661 * This routine is usually called via the inline device_schedule_callback(),
662 * which automatically sets @owner to THIS_MODULE.
663 *
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400664 * Returns 0 if the request was submitted, -ENOMEM if storage could not
Alan Stern523ded72007-04-26 00:12:04 -0700665 * be allocated, -ENODEV if a reference to @owner isn't available.
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400666 *
667 * NOTE: This routine won't work if CONFIG_SYSFS isn't set! It uses an
668 * underlying sysfs routine (since it is intended for use by attribute
669 * methods), and if sysfs isn't available you'll get nothing but -ENOSYS.
670 */
Alan Stern523ded72007-04-26 00:12:04 -0700671int device_schedule_callback_owner(struct device *dev,
672 void (*func)(struct device *), struct module *owner)
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400673{
674 return sysfs_schedule_callback(&dev->kobj,
Alan Stern523ded72007-04-26 00:12:04 -0700675 (void (*)(void *)) func, dev, owner);
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400676}
Alan Stern523ded72007-04-26 00:12:04 -0700677EXPORT_SYMBOL_GPL(device_schedule_callback_owner);
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400678
James Bottomley34bb61f2005-09-06 16:56:51 -0700679static void klist_children_get(struct klist_node *n)
680{
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -0800681 struct device_private *p = to_device_private_parent(n);
682 struct device *dev = p->device;
James Bottomley34bb61f2005-09-06 16:56:51 -0700683
684 get_device(dev);
685}
686
687static void klist_children_put(struct klist_node *n)
688{
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -0800689 struct device_private *p = to_device_private_parent(n);
690 struct device *dev = p->device;
James Bottomley34bb61f2005-09-06 16:56:51 -0700691
692 put_device(dev);
693}
694
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800696 * device_initialize - init device structure.
697 * @dev: device.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 *
Cornelia Huck57394112008-09-03 18:26:40 +0200699 * This prepares the device for use by other layers by initializing
700 * its fields.
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800701 * It is the first half of device_register(), if called by
Cornelia Huck57394112008-09-03 18:26:40 +0200702 * that function, though it can also be called separately, so one
703 * may use @dev's fields. In particular, get_device()/put_device()
704 * may be used for reference counting of @dev after calling this
705 * function.
706 *
Alan Sternb10d5ef2012-01-17 11:39:00 -0500707 * All fields in @dev must be initialized by the caller to 0, except
708 * for those explicitly set to some other value. The simplest
709 * approach is to use kzalloc() to allocate the structure containing
710 * @dev.
711 *
Cornelia Huck57394112008-09-03 18:26:40 +0200712 * NOTE: Use put_device() to give up your reference instead of freeing
713 * @dev directly once you have called this function.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715void device_initialize(struct device *dev)
716{
Greg Kroah-Hartman881c6cfd2007-11-01 09:29:06 -0600717 dev->kobj.kset = devices_kset;
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -0700718 kobject_init(&dev->kobj, &device_ktype);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 INIT_LIST_HEAD(&dev->dma_pools);
Thomas Gleixner31427882010-01-29 20:39:02 +0000720 mutex_init(&dev->mutex);
Peter Zijlstra1704f472010-03-19 01:37:42 +0100721 lockdep_set_novalidate_class(&dev->mutex);
Tejun Heo9ac78492007-01-20 16:00:26 +0900722 spin_lock_init(&dev->devres_lock);
723 INIT_LIST_HEAD(&dev->devres_head);
Alan Stern3b98aea2008-08-07 13:06:12 -0400724 device_pm_init(dev);
Christoph Hellwig87348132006-12-06 20:32:33 -0800725 set_dev_node(dev, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726}
David Graham White86df2682013-07-21 20:41:14 -0400727EXPORT_SYMBOL_GPL(device_initialize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728
Tejun Heod73ce002013-03-12 11:30:05 -0700729struct kobject *virtual_device_parent(struct device *dev)
Greg Kroah-Hartmanf0ee61a2006-10-23 10:40:54 -0700730{
Kay Sievers86406242007-03-14 03:25:56 +0100731 static struct kobject *virtual_dir = NULL;
Greg Kroah-Hartmanf0ee61a2006-10-23 10:40:54 -0700732
Kay Sievers86406242007-03-14 03:25:56 +0100733 if (!virtual_dir)
Greg Kroah-Hartman4ff6abf2007-11-05 22:24:43 -0800734 virtual_dir = kobject_create_and_add("virtual",
Greg Kroah-Hartman881c6cfd2007-11-01 09:29:06 -0600735 &devices_kset->kobj);
Greg Kroah-Hartmanf0ee61a2006-10-23 10:40:54 -0700736
Kay Sievers86406242007-03-14 03:25:56 +0100737 return virtual_dir;
Greg Kroah-Hartmanf0ee61a2006-10-23 10:40:54 -0700738}
739
Eric W. Biedermanbc451f22010-03-30 11:31:25 -0700740struct class_dir {
741 struct kobject kobj;
742 struct class *class;
743};
744
745#define to_class_dir(obj) container_of(obj, struct class_dir, kobj)
746
747static void class_dir_release(struct kobject *kobj)
748{
749 struct class_dir *dir = to_class_dir(kobj);
750 kfree(dir);
751}
752
753static const
754struct kobj_ns_type_operations *class_dir_child_ns_type(struct kobject *kobj)
755{
756 struct class_dir *dir = to_class_dir(kobj);
757 return dir->class->ns_type;
758}
759
760static struct kobj_type class_dir_ktype = {
761 .release = class_dir_release,
762 .sysfs_ops = &kobj_sysfs_ops,
763 .child_ns_type = class_dir_child_ns_type
764};
765
766static struct kobject *
767class_dir_create_and_add(struct class *class, struct kobject *parent_kobj)
768{
769 struct class_dir *dir;
770 int retval;
771
772 dir = kzalloc(sizeof(*dir), GFP_KERNEL);
773 if (!dir)
774 return NULL;
775
776 dir->class = class;
777 kobject_init(&dir->kobj, &class_dir_ktype);
778
Kay Sievers6b6e39a2010-11-15 23:13:18 +0100779 dir->kobj.kset = &class->p->glue_dirs;
Eric W. Biedermanbc451f22010-03-30 11:31:25 -0700780
781 retval = kobject_add(&dir->kobj, parent_kobj, "%s", class->name);
782 if (retval < 0) {
783 kobject_put(&dir->kobj);
784 return NULL;
785 }
786 return &dir->kobj;
787}
788
789
Kay Sieversda231fd2007-11-21 17:29:15 +0100790static struct kobject *get_device_parent(struct device *dev,
791 struct device *parent)
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +0100792{
Kay Sievers86406242007-03-14 03:25:56 +0100793 if (dev->class) {
Tejun Heo77d3d7c2010-02-05 17:57:02 +0900794 static DEFINE_MUTEX(gdp_mutex);
Kay Sievers86406242007-03-14 03:25:56 +0100795 struct kobject *kobj = NULL;
796 struct kobject *parent_kobj;
797 struct kobject *k;
798
Randy Dunlapead454f2010-09-24 14:36:49 -0700799#ifdef CONFIG_BLOCK
Kay Sievers39aba962010-09-04 22:33:14 -0700800 /* block disks show up in /sys/block */
Andi Kleene52eec12010-09-08 16:54:17 +0200801 if (sysfs_deprecated && dev->class == &block_class) {
Kay Sievers39aba962010-09-04 22:33:14 -0700802 if (parent && parent->class == &block_class)
803 return &parent->kobj;
Kay Sievers6b6e39a2010-11-15 23:13:18 +0100804 return &block_class.p->subsys.kobj;
Kay Sievers39aba962010-09-04 22:33:14 -0700805 }
Randy Dunlapead454f2010-09-24 14:36:49 -0700806#endif
Andi Kleene52eec12010-09-08 16:54:17 +0200807
Kay Sievers86406242007-03-14 03:25:56 +0100808 /*
809 * If we have no parent, we live in "virtual".
Kay Sievers0f4dafc2007-12-19 01:40:42 +0100810 * Class-devices with a non class-device as parent, live
811 * in a "glue" directory to prevent namespace collisions.
Kay Sievers86406242007-03-14 03:25:56 +0100812 */
813 if (parent == NULL)
814 parent_kobj = virtual_device_parent(dev);
Eric W. Biederman24b14422010-07-24 22:43:35 -0700815 else if (parent->class && !dev->class->ns_type)
Kay Sievers86406242007-03-14 03:25:56 +0100816 return &parent->kobj;
817 else
818 parent_kobj = &parent->kobj;
819
Tejun Heo77d3d7c2010-02-05 17:57:02 +0900820 mutex_lock(&gdp_mutex);
821
Kay Sievers86406242007-03-14 03:25:56 +0100822 /* find our class-directory at the parent and reference it */
Kay Sievers6b6e39a2010-11-15 23:13:18 +0100823 spin_lock(&dev->class->p->glue_dirs.list_lock);
824 list_for_each_entry(k, &dev->class->p->glue_dirs.list, entry)
Kay Sievers86406242007-03-14 03:25:56 +0100825 if (k->parent == parent_kobj) {
826 kobj = kobject_get(k);
827 break;
828 }
Kay Sievers6b6e39a2010-11-15 23:13:18 +0100829 spin_unlock(&dev->class->p->glue_dirs.list_lock);
Tejun Heo77d3d7c2010-02-05 17:57:02 +0900830 if (kobj) {
831 mutex_unlock(&gdp_mutex);
Kay Sievers86406242007-03-14 03:25:56 +0100832 return kobj;
Tejun Heo77d3d7c2010-02-05 17:57:02 +0900833 }
Kay Sievers86406242007-03-14 03:25:56 +0100834
835 /* or create a new class-directory at the parent device */
Eric W. Biedermanbc451f22010-03-30 11:31:25 -0700836 k = class_dir_create_and_add(dev->class, parent_kobj);
Kay Sievers0f4dafc2007-12-19 01:40:42 +0100837 /* do not emit an uevent for this simple "glue" directory */
Tejun Heo77d3d7c2010-02-05 17:57:02 +0900838 mutex_unlock(&gdp_mutex);
Greg Kroah-Hartman43968d22007-11-05 22:24:43 -0800839 return k;
Kay Sievers86406242007-03-14 03:25:56 +0100840 }
841
Kay Sieversca22e562011-12-14 14:29:38 -0800842 /* subsystems can specify a default root directory for their devices */
843 if (!parent && dev->bus && dev->bus->dev_root)
844 return &dev->bus->dev_root->kobj;
845
Kay Sievers86406242007-03-14 03:25:56 +0100846 if (parent)
Cornelia Huckc744aeae2007-01-08 20:16:44 +0100847 return &parent->kobj;
848 return NULL;
849}
Kay Sieversda231fd2007-11-21 17:29:15 +0100850
Cornelia Huck63b69712008-01-21 16:09:44 +0100851static void cleanup_glue_dir(struct device *dev, struct kobject *glue_dir)
Kay Sieversda231fd2007-11-21 17:29:15 +0100852{
Kay Sievers0f4dafc2007-12-19 01:40:42 +0100853 /* see if we live in a "glue" directory */
Cornelia Huckc1fe5392008-02-27 15:38:23 +0100854 if (!glue_dir || !dev->class ||
Kay Sievers6b6e39a2010-11-15 23:13:18 +0100855 glue_dir->kset != &dev->class->p->glue_dirs)
Kay Sieversda231fd2007-11-21 17:29:15 +0100856 return;
857
Kay Sievers0f4dafc2007-12-19 01:40:42 +0100858 kobject_put(glue_dir);
Kay Sieversda231fd2007-11-21 17:29:15 +0100859}
Cornelia Huck63b69712008-01-21 16:09:44 +0100860
861static void cleanup_device_parent(struct device *dev)
862{
863 cleanup_glue_dir(dev, dev->kobj.parent);
864}
Kay Sievers86406242007-03-14 03:25:56 +0100865
Cornelia Huck2ee97ca2007-07-18 01:43:47 -0700866static int device_add_class_symlinks(struct device *dev)
867{
868 int error;
869
870 if (!dev->class)
871 return 0;
Kay Sieversda231fd2007-11-21 17:29:15 +0100872
Greg Kroah-Hartman1fbfee62008-05-28 09:28:39 -0700873 error = sysfs_create_link(&dev->kobj,
Kay Sievers6b6e39a2010-11-15 23:13:18 +0100874 &dev->class->p->subsys.kobj,
Cornelia Huck2ee97ca2007-07-18 01:43:47 -0700875 "subsystem");
876 if (error)
877 goto out;
Kay Sieversda231fd2007-11-21 17:29:15 +0100878
Greg Kroah-Hartman4e886c22008-01-27 12:12:43 -0800879 if (dev->parent && device_is_not_partition(dev)) {
Dmitry Torokhov4f01a752007-09-18 22:46:50 -0700880 error = sysfs_create_link(&dev->kobj, &dev->parent->kobj,
881 "device");
882 if (error)
Kay Sievers39aba962010-09-04 22:33:14 -0700883 goto out_subsys;
Cornelia Huck2ee97ca2007-07-18 01:43:47 -0700884 }
Kay Sievers39aba962010-09-04 22:33:14 -0700885
Randy Dunlapead454f2010-09-24 14:36:49 -0700886#ifdef CONFIG_BLOCK
Kay Sievers39aba962010-09-04 22:33:14 -0700887 /* /sys/block has directories and does not need symlinks */
Andi Kleene52eec12010-09-08 16:54:17 +0200888 if (sysfs_deprecated && dev->class == &block_class)
Kay Sievers39aba962010-09-04 22:33:14 -0700889 return 0;
Randy Dunlapead454f2010-09-24 14:36:49 -0700890#endif
Kay Sievers39aba962010-09-04 22:33:14 -0700891
892 /* link in the class directory pointing to the device */
Kay Sievers6b6e39a2010-11-15 23:13:18 +0100893 error = sysfs_create_link(&dev->class->p->subsys.kobj,
Kay Sievers39aba962010-09-04 22:33:14 -0700894 &dev->kobj, dev_name(dev));
895 if (error)
896 goto out_device;
897
Cornelia Huck2ee97ca2007-07-18 01:43:47 -0700898 return 0;
899
Kay Sievers39aba962010-09-04 22:33:14 -0700900out_device:
901 sysfs_remove_link(&dev->kobj, "device");
Kay Sieversda231fd2007-11-21 17:29:15 +0100902
Cornelia Huck2ee97ca2007-07-18 01:43:47 -0700903out_subsys:
904 sysfs_remove_link(&dev->kobj, "subsystem");
905out:
906 return error;
907}
908
909static void device_remove_class_symlinks(struct device *dev)
910{
911 if (!dev->class)
912 return;
Kay Sieversda231fd2007-11-21 17:29:15 +0100913
Greg Kroah-Hartman4e886c22008-01-27 12:12:43 -0800914 if (dev->parent && device_is_not_partition(dev))
Kay Sieversda231fd2007-11-21 17:29:15 +0100915 sysfs_remove_link(&dev->kobj, "device");
Cornelia Huck2ee97ca2007-07-18 01:43:47 -0700916 sysfs_remove_link(&dev->kobj, "subsystem");
Randy Dunlapead454f2010-09-24 14:36:49 -0700917#ifdef CONFIG_BLOCK
Andi Kleene52eec12010-09-08 16:54:17 +0200918 if (sysfs_deprecated && dev->class == &block_class)
Kay Sievers39aba962010-09-04 22:33:14 -0700919 return;
Randy Dunlapead454f2010-09-24 14:36:49 -0700920#endif
Kay Sievers6b6e39a2010-11-15 23:13:18 +0100921 sysfs_delete_link(&dev->class->p->subsys.kobj, &dev->kobj, dev_name(dev));
Cornelia Huck2ee97ca2007-07-18 01:43:47 -0700922}
923
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924/**
Stephen Rothwell413c2392008-05-30 10:16:40 +1000925 * dev_set_name - set a device name
926 * @dev: device
Randy Dunlap46232362008-06-04 21:40:43 -0700927 * @fmt: format string for the device's name
Stephen Rothwell413c2392008-05-30 10:16:40 +1000928 */
929int dev_set_name(struct device *dev, const char *fmt, ...)
930{
931 va_list vargs;
Kay Sievers1fa5ae82009-01-25 15:17:37 +0100932 int err;
Stephen Rothwell413c2392008-05-30 10:16:40 +1000933
934 va_start(vargs, fmt);
Kay Sievers1fa5ae82009-01-25 15:17:37 +0100935 err = kobject_set_name_vargs(&dev->kobj, fmt, vargs);
Stephen Rothwell413c2392008-05-30 10:16:40 +1000936 va_end(vargs);
Kay Sievers1fa5ae82009-01-25 15:17:37 +0100937 return err;
Stephen Rothwell413c2392008-05-30 10:16:40 +1000938}
939EXPORT_SYMBOL_GPL(dev_set_name);
940
941/**
Dan Williamse105b8b2008-04-21 10:51:07 -0700942 * device_to_dev_kobj - select a /sys/dev/ directory for the device
943 * @dev: device
944 *
945 * By default we select char/ for new entries. Setting class->dev_obj
946 * to NULL prevents an entry from being created. class->dev_kobj must
947 * be set (or cleared) before any devices are registered to the class
948 * otherwise device_create_sys_dev_entry() and
Peter Korsgaard0d4e293c2012-04-17 12:12:57 +0200949 * device_remove_sys_dev_entry() will disagree about the presence of
950 * the link.
Dan Williamse105b8b2008-04-21 10:51:07 -0700951 */
952static struct kobject *device_to_dev_kobj(struct device *dev)
953{
954 struct kobject *kobj;
955
956 if (dev->class)
957 kobj = dev->class->dev_kobj;
958 else
959 kobj = sysfs_dev_char_kobj;
960
961 return kobj;
962}
963
964static int device_create_sys_dev_entry(struct device *dev)
965{
966 struct kobject *kobj = device_to_dev_kobj(dev);
967 int error = 0;
968 char devt_str[15];
969
970 if (kobj) {
971 format_dev_t(devt_str, dev->devt);
972 error = sysfs_create_link(kobj, &dev->kobj, devt_str);
973 }
974
975 return error;
976}
977
978static void device_remove_sys_dev_entry(struct device *dev)
979{
980 struct kobject *kobj = device_to_dev_kobj(dev);
981 char devt_str[15];
982
983 if (kobj) {
984 format_dev_t(devt_str, dev->devt);
985 sysfs_remove_link(kobj, devt_str);
986 }
987}
988
Greg Kroah-Hartmanb4028432009-05-11 14:16:57 -0700989int device_private_init(struct device *dev)
990{
991 dev->p = kzalloc(sizeof(*dev->p), GFP_KERNEL);
992 if (!dev->p)
993 return -ENOMEM;
994 dev->p->device = dev;
995 klist_init(&dev->p->klist_children, klist_children_get,
996 klist_children_put);
Greg Kroah-Hartmanef8a3fd2012-03-08 12:17:22 -0800997 INIT_LIST_HEAD(&dev->p->deferred_probe);
Greg Kroah-Hartmanb4028432009-05-11 14:16:57 -0700998 return 0;
999}
1000
Dan Williamse105b8b2008-04-21 10:51:07 -07001001/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001002 * device_add - add device to device hierarchy.
1003 * @dev: device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001005 * This is part 2 of device_register(), though may be called
1006 * separately _iff_ device_initialize() has been called separately.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 *
Cornelia Huck57394112008-09-03 18:26:40 +02001008 * This adds @dev to the kobject hierarchy via kobject_add(), adds it
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001009 * to the global and sibling lists for the device, then
1010 * adds it to the other relevant subsystems of the driver model.
Cornelia Huck57394112008-09-03 18:26:40 +02001011 *
Alan Sternb10d5ef2012-01-17 11:39:00 -05001012 * Do not call this routine or device_register() more than once for
1013 * any device structure. The driver model core is not designed to work
1014 * with devices that get unregistered and then spring back to life.
1015 * (Among other things, it's very hard to guarantee that all references
1016 * to the previous incarnation of @dev have been dropped.) Allocate
1017 * and register a fresh new struct device instead.
1018 *
Cornelia Huck57394112008-09-03 18:26:40 +02001019 * NOTE: _Never_ directly free @dev after calling this function, even
1020 * if it returned an error! Always use put_device() to give up your
1021 * reference instead.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 */
1023int device_add(struct device *dev)
1024{
1025 struct device *parent = NULL;
Kay Sieversca22e562011-12-14 14:29:38 -08001026 struct kobject *kobj;
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02001027 struct class_interface *class_intf;
Greg Kroah-Hartmanc906a482008-05-30 10:45:12 -07001028 int error = -EINVAL;
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001029
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 dev = get_device(dev);
Greg Kroah-Hartmanc906a482008-05-30 10:45:12 -07001031 if (!dev)
1032 goto done;
1033
Greg Kroah-Hartmanfb069a52008-12-16 12:23:36 -08001034 if (!dev->p) {
Greg Kroah-Hartmanb4028432009-05-11 14:16:57 -07001035 error = device_private_init(dev);
1036 if (error)
1037 goto done;
Greg Kroah-Hartmanfb069a52008-12-16 12:23:36 -08001038 }
Greg Kroah-Hartmanfb069a52008-12-16 12:23:36 -08001039
Kay Sievers1fa5ae82009-01-25 15:17:37 +01001040 /*
1041 * for statically allocated devices, which should all be converted
1042 * some day, we need to initialize the name. We prevent reading back
1043 * the name, and force the use of dev_name()
1044 */
1045 if (dev->init_name) {
Greg Kroah-Hartmanacc0e902009-06-02 15:39:55 -07001046 dev_set_name(dev, "%s", dev->init_name);
Kay Sievers1fa5ae82009-01-25 15:17:37 +01001047 dev->init_name = NULL;
1048 }
Greg Kroah-Hartmanc906a482008-05-30 10:45:12 -07001049
Kay Sieversca22e562011-12-14 14:29:38 -08001050 /* subsystems can specify simple device enumeration */
1051 if (!dev_name(dev) && dev->bus && dev->bus->dev_name)
1052 dev_set_name(dev, "%s%u", dev->bus->dev_name, dev->id);
1053
Thomas Gleixnere6309e72009-12-10 19:32:49 +00001054 if (!dev_name(dev)) {
1055 error = -EINVAL;
Kay Sievers5c8563d2009-05-28 14:24:07 -07001056 goto name_error;
Thomas Gleixnere6309e72009-12-10 19:32:49 +00001057 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01001059 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
Greg Kroah-Hartmanc205ef42006-08-07 22:19:37 -07001060
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 parent = get_device(dev->parent);
Kay Sieversca22e562011-12-14 14:29:38 -08001062 kobj = get_device_parent(dev, parent);
1063 if (kobj)
1064 dev->kobj.parent = kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065
Yinghai Lu0d358f22008-02-19 03:20:41 -08001066 /* use parent numa_node */
1067 if (parent)
1068 set_dev_node(dev, dev_to_node(parent));
1069
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 /* first, register with generic layer. */
Kay Sievers8a577ff2009-04-18 15:05:45 -07001071 /* we require the name to be set before, and pass NULL */
1072 error = kobject_add(&dev->kobj, dev->kobj.parent, NULL);
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +01001073 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 goto Error;
Kay Sieversa7fd6702005-10-01 14:49:43 +02001075
Brian Walsh37022642006-08-14 22:43:19 -07001076 /* notify platform of device entry */
1077 if (platform_notify)
1078 platform_notify(dev);
1079
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07001080 error = device_create_file(dev, &dev_attr_uevent);
Cornelia Hucka306eea2006-09-22 11:37:13 +02001081 if (error)
1082 goto attrError;
Kay Sieversa7fd6702005-10-01 14:49:43 +02001083
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001084 if (MAJOR(dev->devt)) {
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07001085 error = device_create_file(dev, &dev_attr_dev);
Tejun Heoad6a1e12007-06-14 03:45:17 +09001086 if (error)
Cornelia Hucka306eea2006-09-22 11:37:13 +02001087 goto ueventattrError;
Dan Williamse105b8b2008-04-21 10:51:07 -07001088
1089 error = device_create_sys_dev_entry(dev);
1090 if (error)
1091 goto devtattrError;
Kay Sievers2b2af542009-04-30 15:23:42 +02001092
1093 devtmpfs_create_node(dev);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001094 }
1095
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001096 error = device_add_class_symlinks(dev);
1097 if (error)
1098 goto SymlinkError;
Cornelia Huckdc0afa82007-07-09 11:39:18 -07001099 error = device_add_attrs(dev);
1100 if (error)
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07001101 goto AttrsError;
Cornelia Huckdc0afa82007-07-09 11:39:18 -07001102 error = bus_add_device(dev);
1103 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 goto BusError;
Alan Stern3b98aea2008-08-07 13:06:12 -04001105 error = dpm_sysfs_add(dev);
Rafael J. Wysocki57eee3d2008-03-12 00:59:38 +01001106 if (error)
Alan Stern3b98aea2008-08-07 13:06:12 -04001107 goto DPMError;
1108 device_pm_add(dev);
Alan Sternec0676ee2008-12-05 14:10:31 -05001109
1110 /* Notify clients of device addition. This call must come
majianpeng268863f2012-01-11 15:12:06 +00001111 * after dpm_sysfs_add() and before kobject_uevent().
Alan Sternec0676ee2008-12-05 14:10:31 -05001112 */
1113 if (dev->bus)
1114 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
1115 BUS_NOTIFY_ADD_DEVICE, dev);
1116
Cornelia Huck83b5fb4c2007-03-29 11:12:11 +02001117 kobject_uevent(&dev->kobj, KOBJ_ADD);
Alan Stern2023c612009-07-30 15:27:18 -04001118 bus_probe_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 if (parent)
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08001120 klist_add_tail(&dev->p->knode_parent,
1121 &parent->p->klist_children);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122
Greg Kroah-Hartman5d9fd162006-06-22 17:17:32 -07001123 if (dev->class) {
Kay Sieversca22e562011-12-14 14:29:38 -08001124 mutex_lock(&dev->class->p->mutex);
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02001125 /* tie the class to the device */
Tejun Heo5a3ceb82008-08-25 19:50:19 +02001126 klist_add_tail(&dev->knode_class,
Kay Sievers6b6e39a2010-11-15 23:13:18 +01001127 &dev->class->p->klist_devices);
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02001128
1129 /* notify any interfaces that the device is here */
Greg Kroah-Hartman184f1f72008-05-28 09:28:39 -07001130 list_for_each_entry(class_intf,
Kay Sieversca22e562011-12-14 14:29:38 -08001131 &dev->class->p->interfaces, node)
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02001132 if (class_intf->add_dev)
1133 class_intf->add_dev(dev, class_intf);
Kay Sieversca22e562011-12-14 14:29:38 -08001134 mutex_unlock(&dev->class->p->mutex);
Greg Kroah-Hartman5d9fd162006-06-22 17:17:32 -07001135 }
Greg Kroah-Hartmanc906a482008-05-30 10:45:12 -07001136done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 put_device(dev);
1138 return error;
Alan Stern3b98aea2008-08-07 13:06:12 -04001139 DPMError:
Rafael J. Wysocki57eee3d2008-03-12 00:59:38 +01001140 bus_remove_device(dev);
1141 BusError:
James Simmons82f0cf92007-02-21 17:44:51 +00001142 device_remove_attrs(dev);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07001143 AttrsError:
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001144 device_remove_class_symlinks(dev);
1145 SymlinkError:
Tejun Heoad6a1e12007-06-14 03:45:17 +09001146 if (MAJOR(dev->devt))
Kay Sieversad729562009-10-28 19:51:37 +01001147 devtmpfs_delete_node(dev);
1148 if (MAJOR(dev->devt))
Dan Williamse105b8b2008-04-21 10:51:07 -07001149 device_remove_sys_dev_entry(dev);
1150 devtattrError:
1151 if (MAJOR(dev->devt))
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07001152 device_remove_file(dev, &dev_attr_dev);
Cornelia Hucka306eea2006-09-22 11:37:13 +02001153 ueventattrError:
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07001154 device_remove_file(dev, &dev_attr_uevent);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001155 attrError:
Kay Sievers312c0042005-11-16 09:00:00 +01001156 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 kobject_del(&dev->kobj);
1158 Error:
Cornelia Huck63b69712008-01-21 16:09:44 +01001159 cleanup_device_parent(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 if (parent)
1161 put_device(parent);
Kay Sievers5c8563d2009-05-28 14:24:07 -07001162name_error:
1163 kfree(dev->p);
1164 dev->p = NULL;
Greg Kroah-Hartmanc906a482008-05-30 10:45:12 -07001165 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166}
David Graham White86df2682013-07-21 20:41:14 -04001167EXPORT_SYMBOL_GPL(device_add);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001170 * device_register - register a device with the system.
1171 * @dev: pointer to the device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001173 * This happens in two clean steps - initialize the device
1174 * and add it to the system. The two steps can be called
1175 * separately, but this is the easiest and most common.
1176 * I.e. you should only call the two helpers separately if
1177 * have a clearly defined need to use and refcount the device
1178 * before it is added to the hierarchy.
Cornelia Huck57394112008-09-03 18:26:40 +02001179 *
Alan Sternb10d5ef2012-01-17 11:39:00 -05001180 * For more information, see the kerneldoc for device_initialize()
1181 * and device_add().
1182 *
Cornelia Huck57394112008-09-03 18:26:40 +02001183 * NOTE: _Never_ directly free @dev after calling this function, even
1184 * if it returned an error! Always use put_device() to give up the
1185 * reference initialized in this function instead.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187int device_register(struct device *dev)
1188{
1189 device_initialize(dev);
1190 return device_add(dev);
1191}
David Graham White86df2682013-07-21 20:41:14 -04001192EXPORT_SYMBOL_GPL(device_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001195 * get_device - increment reference count for device.
1196 * @dev: device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001198 * This simply forwards the call to kobject_get(), though
1199 * we do take care to provide for the case that we get a NULL
1200 * pointer passed in.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001202struct device *get_device(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +02001204 return dev ? kobj_to_dev(kobject_get(&dev->kobj)) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205}
David Graham White86df2682013-07-21 20:41:14 -04001206EXPORT_SYMBOL_GPL(get_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001209 * put_device - decrement reference count.
1210 * @dev: device in question.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001212void put_device(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213{
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001214 /* might_sleep(); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 if (dev)
1216 kobject_put(&dev->kobj);
1217}
David Graham White86df2682013-07-21 20:41:14 -04001218EXPORT_SYMBOL_GPL(put_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001221 * device_del - delete device from system.
1222 * @dev: device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001224 * This is the first part of the device unregistration
1225 * sequence. This removes the device from the lists we control
1226 * from here, has it removed from the other driver model
1227 * subsystems it was added to in device_add(), and removes it
1228 * from the kobject hierarchy.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001230 * NOTE: this should be called manually _iff_ device_add() was
1231 * also called manually.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001233void device_del(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001235 struct device *parent = dev->parent;
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02001236 struct class_interface *class_intf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237
Alan Sternec0676ee2008-12-05 14:10:31 -05001238 /* Notify clients of device removal. This call must come
1239 * before dpm_sysfs_remove().
1240 */
1241 if (dev->bus)
1242 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
1243 BUS_NOTIFY_DEL_DEVICE, dev);
Alan Stern3b98aea2008-08-07 13:06:12 -04001244 dpm_sysfs_remove(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 if (parent)
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08001246 klist_del(&dev->p->knode_parent);
Dan Williamse105b8b2008-04-21 10:51:07 -07001247 if (MAJOR(dev->devt)) {
Kay Sievers2b2af542009-04-30 15:23:42 +02001248 devtmpfs_delete_node(dev);
Dan Williamse105b8b2008-04-21 10:51:07 -07001249 device_remove_sys_dev_entry(dev);
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07001250 device_remove_file(dev, &dev_attr_dev);
Dan Williamse105b8b2008-04-21 10:51:07 -07001251 }
Kay Sieversb9d9c822006-06-15 15:31:56 +02001252 if (dev->class) {
Kay Sieversda231fd2007-11-21 17:29:15 +01001253 device_remove_class_symlinks(dev);
Kay Sievers99ef3ef2006-09-14 11:23:28 +02001254
Kay Sieversca22e562011-12-14 14:29:38 -08001255 mutex_lock(&dev->class->p->mutex);
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02001256 /* notify any interfaces that the device is now gone */
Greg Kroah-Hartman184f1f72008-05-28 09:28:39 -07001257 list_for_each_entry(class_intf,
Kay Sieversca22e562011-12-14 14:29:38 -08001258 &dev->class->p->interfaces, node)
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02001259 if (class_intf->remove_dev)
1260 class_intf->remove_dev(dev, class_intf);
1261 /* remove the device from the class list */
Tejun Heo5a3ceb82008-08-25 19:50:19 +02001262 klist_del(&dev->knode_class);
Kay Sieversca22e562011-12-14 14:29:38 -08001263 mutex_unlock(&dev->class->p->mutex);
Kay Sieversb9d9c822006-06-15 15:31:56 +02001264 }
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07001265 device_remove_file(dev, &dev_attr_uevent);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07001266 device_remove_attrs(dev);
Benjamin Herrenschmidt28953532006-11-08 19:46:14 -08001267 bus_remove_device(dev);
LongX Zhang4b6d1f122012-10-25 00:21:28 +02001268 device_pm_remove(dev);
Grant Likelyd1c34142012-03-05 08:47:41 -07001269 driver_deferred_probe_del(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270
1271 /* Notify the platform of the removal, in case they
1272 * need to do anything...
1273 */
1274 if (platform_notify_remove)
1275 platform_notify_remove(dev);
Kay Sievers312c0042005-11-16 09:00:00 +01001276 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
Kay Sieversda231fd2007-11-21 17:29:15 +01001277 cleanup_device_parent(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 kobject_del(&dev->kobj);
Kay Sieversda231fd2007-11-21 17:29:15 +01001279 put_device(parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280}
David Graham White86df2682013-07-21 20:41:14 -04001281EXPORT_SYMBOL_GPL(device_del);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282
1283/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001284 * device_unregister - unregister device from system.
1285 * @dev: device going away.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001287 * We do this in two parts, like we do device_register(). First,
1288 * we remove it from all the subsystems with device_del(), then
1289 * we decrement the reference count via put_device(). If that
1290 * is the final reference count, the device will be cleaned up
1291 * via device_release() above. Otherwise, the structure will
1292 * stick around until the final reference to the device is dropped.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001294void device_unregister(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295{
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01001296 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 device_del(dev);
1298 put_device(dev);
1299}
David Graham White86df2682013-07-21 20:41:14 -04001300EXPORT_SYMBOL_GPL(device_unregister);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001302static struct device *next_device(struct klist_iter *i)
mochel@digitalimplant.org36239572005-03-24 19:08:30 -08001303{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001304 struct klist_node *n = klist_next(i);
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08001305 struct device *dev = NULL;
1306 struct device_private *p;
1307
1308 if (n) {
1309 p = to_device_private_parent(n);
1310 dev = p->device;
1311 }
1312 return dev;
mochel@digitalimplant.org36239572005-03-24 19:08:30 -08001313}
1314
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315/**
Kay Sieverse454cea2009-09-18 23:01:12 +02001316 * device_get_devnode - path of device node file
Kay Sievers6fcf53a2009-04-30 15:23:42 +02001317 * @dev: device
Kay Sieverse454cea2009-09-18 23:01:12 +02001318 * @mode: returned file access mode
Kay Sievers3c2670e2013-04-06 09:56:00 -07001319 * @uid: returned file owner
1320 * @gid: returned file group
Kay Sievers6fcf53a2009-04-30 15:23:42 +02001321 * @tmp: possibly allocated string
1322 *
1323 * Return the relative path of a possible device node.
1324 * Non-default names may need to allocate a memory to compose
1325 * a name. This memory is returned in tmp and needs to be
1326 * freed by the caller.
1327 */
Kay Sieverse454cea2009-09-18 23:01:12 +02001328const char *device_get_devnode(struct device *dev,
Greg Kroah-Hartman4e4098a2013-04-11 11:43:29 -07001329 umode_t *mode, kuid_t *uid, kgid_t *gid,
Kay Sievers3c2670e2013-04-06 09:56:00 -07001330 const char **tmp)
Kay Sievers6fcf53a2009-04-30 15:23:42 +02001331{
1332 char *s;
1333
1334 *tmp = NULL;
1335
1336 /* the device type may provide a specific name */
Kay Sieverse454cea2009-09-18 23:01:12 +02001337 if (dev->type && dev->type->devnode)
Kay Sievers3c2670e2013-04-06 09:56:00 -07001338 *tmp = dev->type->devnode(dev, mode, uid, gid);
Kay Sievers6fcf53a2009-04-30 15:23:42 +02001339 if (*tmp)
1340 return *tmp;
1341
1342 /* the class may provide a specific name */
Kay Sieverse454cea2009-09-18 23:01:12 +02001343 if (dev->class && dev->class->devnode)
1344 *tmp = dev->class->devnode(dev, mode);
Kay Sievers6fcf53a2009-04-30 15:23:42 +02001345 if (*tmp)
1346 return *tmp;
1347
1348 /* return name without allocation, tmp == NULL */
1349 if (strchr(dev_name(dev), '!') == NULL)
1350 return dev_name(dev);
1351
1352 /* replace '!' in the name with '/' */
1353 *tmp = kstrdup(dev_name(dev), GFP_KERNEL);
1354 if (!*tmp)
1355 return NULL;
1356 while ((s = strchr(*tmp, '!')))
1357 s[0] = '/';
1358 return *tmp;
1359}
1360
1361/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001362 * device_for_each_child - device child iterator.
1363 * @parent: parent struct device.
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001364 * @fn: function to be called for each device.
Robert P. J. Dayf8878dc2013-06-01 20:17:34 -04001365 * @data: data for the callback.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001367 * Iterate over @parent's child devices, and call @fn for each,
1368 * passing it @data.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001370 * We check the return of @fn each time. If it returns anything
1371 * other than 0, we break out and return that value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001373int device_for_each_child(struct device *parent, void *data,
1374 int (*fn)(struct device *dev, void *data))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375{
mochel@digitalimplant.org36239572005-03-24 19:08:30 -08001376 struct klist_iter i;
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001377 struct device *child;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 int error = 0;
1379
Greg Kroah-Hartman014c90db2009-04-15 16:00:12 -07001380 if (!parent->p)
1381 return 0;
1382
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08001383 klist_iter_init(&parent->p->klist_children, &i);
mochel@digitalimplant.org36239572005-03-24 19:08:30 -08001384 while ((child = next_device(&i)) && !error)
1385 error = fn(child, data);
1386 klist_iter_exit(&i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 return error;
1388}
David Graham White86df2682013-07-21 20:41:14 -04001389EXPORT_SYMBOL_GPL(device_for_each_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390
Cornelia Huck5ab69982006-11-16 15:42:07 +01001391/**
1392 * device_find_child - device iterator for locating a particular device.
1393 * @parent: parent struct device
Cornelia Huck5ab69982006-11-16 15:42:07 +01001394 * @match: Callback function to check device
Robert P. J. Dayf8878dc2013-06-01 20:17:34 -04001395 * @data: Data to pass to match function
Cornelia Huck5ab69982006-11-16 15:42:07 +01001396 *
1397 * This is similar to the device_for_each_child() function above, but it
1398 * returns a reference to a device that is 'found' for later use, as
1399 * determined by the @match callback.
1400 *
1401 * The callback should return 0 if the device doesn't match and non-zero
1402 * if it does. If the callback returns non-zero and a reference to the
1403 * current device can be obtained, this function will return to the caller
1404 * and not iterate over any more devices.
Federico Vagaa4e24002013-04-15 11:18:11 +02001405 *
1406 * NOTE: you will need to drop the reference with put_device() after use.
Cornelia Huck5ab69982006-11-16 15:42:07 +01001407 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001408struct device *device_find_child(struct device *parent, void *data,
1409 int (*match)(struct device *dev, void *data))
Cornelia Huck5ab69982006-11-16 15:42:07 +01001410{
1411 struct klist_iter i;
1412 struct device *child;
1413
1414 if (!parent)
1415 return NULL;
1416
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08001417 klist_iter_init(&parent->p->klist_children, &i);
Cornelia Huck5ab69982006-11-16 15:42:07 +01001418 while ((child = next_device(&i)))
1419 if (match(child, data) && get_device(child))
1420 break;
1421 klist_iter_exit(&i);
1422 return child;
1423}
David Graham White86df2682013-07-21 20:41:14 -04001424EXPORT_SYMBOL_GPL(device_find_child);
Cornelia Huck5ab69982006-11-16 15:42:07 +01001425
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426int __init devices_init(void)
1427{
Greg Kroah-Hartman881c6cfd2007-11-01 09:29:06 -06001428 devices_kset = kset_create_and_add("devices", &device_uevent_ops, NULL);
1429 if (!devices_kset)
1430 return -ENOMEM;
Dan Williamse105b8b2008-04-21 10:51:07 -07001431 dev_kobj = kobject_create_and_add("dev", NULL);
1432 if (!dev_kobj)
1433 goto dev_kobj_err;
1434 sysfs_dev_block_kobj = kobject_create_and_add("block", dev_kobj);
1435 if (!sysfs_dev_block_kobj)
1436 goto block_kobj_err;
1437 sysfs_dev_char_kobj = kobject_create_and_add("char", dev_kobj);
1438 if (!sysfs_dev_char_kobj)
1439 goto char_kobj_err;
1440
Greg Kroah-Hartman881c6cfd2007-11-01 09:29:06 -06001441 return 0;
Dan Williamse105b8b2008-04-21 10:51:07 -07001442
1443 char_kobj_err:
1444 kobject_put(sysfs_dev_block_kobj);
1445 block_kobj_err:
1446 kobject_put(dev_kobj);
1447 dev_kobj_err:
1448 kset_unregister(devices_kset);
1449 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450}
1451
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02001452static DEFINE_MUTEX(device_hotplug_lock);
1453
1454void lock_device_hotplug(void)
1455{
1456 mutex_lock(&device_hotplug_lock);
1457}
1458
1459void unlock_device_hotplug(void)
1460{
1461 mutex_unlock(&device_hotplug_lock);
1462}
1463
1464static int device_check_offline(struct device *dev, void *not_used)
1465{
1466 int ret;
1467
1468 ret = device_for_each_child(dev, NULL, device_check_offline);
1469 if (ret)
1470 return ret;
1471
1472 return device_supports_offline(dev) && !dev->offline ? -EBUSY : 0;
1473}
1474
1475/**
1476 * device_offline - Prepare the device for hot-removal.
1477 * @dev: Device to be put offline.
1478 *
1479 * Execute the device bus type's .offline() callback, if present, to prepare
1480 * the device for a subsequent hot-removal. If that succeeds, the device must
1481 * not be used until either it is removed or its bus type's .online() callback
1482 * is executed.
1483 *
1484 * Call under device_hotplug_lock.
1485 */
1486int device_offline(struct device *dev)
1487{
1488 int ret;
1489
1490 if (dev->offline_disabled)
1491 return -EPERM;
1492
1493 ret = device_for_each_child(dev, NULL, device_check_offline);
1494 if (ret)
1495 return ret;
1496
1497 device_lock(dev);
1498 if (device_supports_offline(dev)) {
1499 if (dev->offline) {
1500 ret = 1;
1501 } else {
1502 ret = dev->bus->offline(dev);
1503 if (!ret) {
1504 kobject_uevent(&dev->kobj, KOBJ_OFFLINE);
1505 dev->offline = true;
1506 }
1507 }
1508 }
1509 device_unlock(dev);
1510
1511 return ret;
1512}
1513
1514/**
1515 * device_online - Put the device back online after successful device_offline().
1516 * @dev: Device to be put back online.
1517 *
1518 * If device_offline() has been successfully executed for @dev, but the device
1519 * has not been removed subsequently, execute its bus type's .online() callback
1520 * to indicate that the device can be used again.
1521 *
1522 * Call under device_hotplug_lock.
1523 */
1524int device_online(struct device *dev)
1525{
1526 int ret = 0;
1527
1528 device_lock(dev);
1529 if (device_supports_offline(dev)) {
1530 if (dev->offline) {
1531 ret = dev->bus->online(dev);
1532 if (!ret) {
1533 kobject_uevent(&dev->kobj, KOBJ_ONLINE);
1534 dev->offline = false;
1535 }
1536 } else {
1537 ret = 1;
1538 }
1539 }
1540 device_unlock(dev);
1541
1542 return ret;
1543}
1544
Karthigan Srinivasan7f100d12011-04-18 16:16:52 -05001545struct root_device {
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00001546 struct device dev;
1547 struct module *owner;
1548};
1549
Josh Triplett93058422012-11-18 21:27:55 -08001550static inline struct root_device *to_root_device(struct device *d)
Ferenc Wagner481e2072011-01-07 15:17:47 +01001551{
1552 return container_of(d, struct root_device, dev);
1553}
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00001554
1555static void root_device_release(struct device *dev)
1556{
1557 kfree(to_root_device(dev));
1558}
1559
1560/**
1561 * __root_device_register - allocate and register a root device
1562 * @name: root device name
1563 * @owner: owner module of the root device, usually THIS_MODULE
1564 *
1565 * This function allocates a root device and registers it
1566 * using device_register(). In order to free the returned
1567 * device, use root_device_unregister().
1568 *
1569 * Root devices are dummy devices which allow other devices
1570 * to be grouped under /sys/devices. Use this function to
1571 * allocate a root device and then use it as the parent of
1572 * any device which should appear under /sys/devices/{name}
1573 *
1574 * The /sys/devices/{name} directory will also contain a
1575 * 'module' symlink which points to the @owner directory
1576 * in sysfs.
1577 *
Jani Nikulaf0eae0e2010-03-11 18:11:45 +02001578 * Returns &struct device pointer on success, or ERR_PTR() on error.
1579 *
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00001580 * Note: You probably want to use root_device_register().
1581 */
1582struct device *__root_device_register(const char *name, struct module *owner)
1583{
1584 struct root_device *root;
1585 int err = -ENOMEM;
1586
1587 root = kzalloc(sizeof(struct root_device), GFP_KERNEL);
1588 if (!root)
1589 return ERR_PTR(err);
1590
Greg Kroah-Hartmanacc0e902009-06-02 15:39:55 -07001591 err = dev_set_name(&root->dev, "%s", name);
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00001592 if (err) {
1593 kfree(root);
1594 return ERR_PTR(err);
1595 }
1596
1597 root->dev.release = root_device_release;
1598
1599 err = device_register(&root->dev);
1600 if (err) {
1601 put_device(&root->dev);
1602 return ERR_PTR(err);
1603 }
1604
Christoph Egger1d9e8822010-05-17 16:57:58 +02001605#ifdef CONFIG_MODULES /* gotta find a "cleaner" way to do this */
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00001606 if (owner) {
1607 struct module_kobject *mk = &owner->mkobj;
1608
1609 err = sysfs_create_link(&root->dev.kobj, &mk->kobj, "module");
1610 if (err) {
1611 device_unregister(&root->dev);
1612 return ERR_PTR(err);
1613 }
1614 root->owner = owner;
1615 }
1616#endif
1617
1618 return &root->dev;
1619}
1620EXPORT_SYMBOL_GPL(__root_device_register);
1621
1622/**
1623 * root_device_unregister - unregister and free a root device
Randy Dunlap7cbcf222009-01-20 16:29:13 -08001624 * @dev: device going away
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00001625 *
1626 * This function unregisters and cleans up a device that was created by
1627 * root_device_register().
1628 */
1629void root_device_unregister(struct device *dev)
1630{
1631 struct root_device *root = to_root_device(dev);
1632
1633 if (root->owner)
1634 sysfs_remove_link(&root->dev.kobj, "module");
1635
1636 device_unregister(dev);
1637}
1638EXPORT_SYMBOL_GPL(root_device_unregister);
1639
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001640
1641static void device_create_release(struct device *dev)
1642{
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01001643 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001644 kfree(dev);
1645}
1646
Guenter Roeck39ef3112013-07-14 16:05:57 -07001647static struct device *
1648device_create_groups_vargs(struct class *class, struct device *parent,
1649 dev_t devt, void *drvdata,
1650 const struct attribute_group **groups,
1651 const char *fmt, va_list args)
1652{
1653 struct device *dev = NULL;
1654 int retval = -ENODEV;
1655
1656 if (class == NULL || IS_ERR(class))
1657 goto error;
1658
1659 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1660 if (!dev) {
1661 retval = -ENOMEM;
1662 goto error;
1663 }
1664
1665 dev->devt = devt;
1666 dev->class = class;
1667 dev->parent = parent;
1668 dev->groups = groups;
1669 dev->release = device_create_release;
1670 dev_set_drvdata(dev, drvdata);
1671
1672 retval = kobject_set_name_vargs(&dev->kobj, fmt, args);
1673 if (retval)
1674 goto error;
1675
1676 retval = device_register(dev);
1677 if (retval)
1678 goto error;
1679
1680 return dev;
1681
1682error:
1683 put_device(dev);
1684 return ERR_PTR(retval);
1685}
1686
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001687/**
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07001688 * device_create_vargs - creates a device and registers it with sysfs
1689 * @class: pointer to the struct class that this device should be registered to
1690 * @parent: pointer to the parent struct device of this new device, if any
1691 * @devt: the dev_t for the char device to be added
1692 * @drvdata: the data to be added to the device for callbacks
1693 * @fmt: string for the device's name
1694 * @args: va_list for the device's name
1695 *
1696 * This function can be used by char device classes. A struct device
1697 * will be created in sysfs, registered to the specified class.
1698 *
1699 * A "dev" file will be created, showing the dev_t for the device, if
1700 * the dev_t is not 0,0.
1701 * If a pointer to a parent struct device is passed in, the newly created
1702 * struct device will be a child of that device in sysfs.
1703 * The pointer to the struct device will be returned from the call.
1704 * Any further sysfs files that might be required can be created using this
1705 * pointer.
1706 *
Jani Nikulaf0eae0e2010-03-11 18:11:45 +02001707 * Returns &struct device pointer on success, or ERR_PTR() on error.
1708 *
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07001709 * Note: the struct class passed to this function must have previously
1710 * been created with a call to class_create().
1711 */
1712struct device *device_create_vargs(struct class *class, struct device *parent,
1713 dev_t devt, void *drvdata, const char *fmt,
1714 va_list args)
1715{
Guenter Roeck39ef3112013-07-14 16:05:57 -07001716 return device_create_groups_vargs(class, parent, devt, drvdata, NULL,
1717 fmt, args);
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07001718}
1719EXPORT_SYMBOL_GPL(device_create_vargs);
1720
1721/**
Greg Kroah-Hartman4e106732008-07-21 20:03:34 -07001722 * device_create - creates a device and registers it with sysfs
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07001723 * @class: pointer to the struct class that this device should be registered to
1724 * @parent: pointer to the parent struct device of this new device, if any
1725 * @devt: the dev_t for the char device to be added
1726 * @drvdata: the data to be added to the device for callbacks
1727 * @fmt: string for the device's name
1728 *
1729 * This function can be used by char device classes. A struct device
1730 * will be created in sysfs, registered to the specified class.
1731 *
1732 * A "dev" file will be created, showing the dev_t for the device, if
1733 * the dev_t is not 0,0.
1734 * If a pointer to a parent struct device is passed in, the newly created
1735 * struct device will be a child of that device in sysfs.
1736 * The pointer to the struct device will be returned from the call.
1737 * Any further sysfs files that might be required can be created using this
1738 * pointer.
1739 *
Jani Nikulaf0eae0e2010-03-11 18:11:45 +02001740 * Returns &struct device pointer on success, or ERR_PTR() on error.
1741 *
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07001742 * Note: the struct class passed to this function must have previously
1743 * been created with a call to class_create().
1744 */
Greg Kroah-Hartman4e106732008-07-21 20:03:34 -07001745struct device *device_create(struct class *class, struct device *parent,
1746 dev_t devt, void *drvdata, const char *fmt, ...)
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07001747{
1748 va_list vargs;
1749 struct device *dev;
1750
1751 va_start(vargs, fmt);
1752 dev = device_create_vargs(class, parent, devt, drvdata, fmt, vargs);
1753 va_end(vargs);
1754 return dev;
1755}
Greg Kroah-Hartman4e106732008-07-21 20:03:34 -07001756EXPORT_SYMBOL_GPL(device_create);
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07001757
Guenter Roeck39ef3112013-07-14 16:05:57 -07001758/**
1759 * device_create_with_groups - creates a device and registers it with sysfs
1760 * @class: pointer to the struct class that this device should be registered to
1761 * @parent: pointer to the parent struct device of this new device, if any
1762 * @devt: the dev_t for the char device to be added
1763 * @drvdata: the data to be added to the device for callbacks
1764 * @groups: NULL-terminated list of attribute groups to be created
1765 * @fmt: string for the device's name
1766 *
1767 * This function can be used by char device classes. A struct device
1768 * will be created in sysfs, registered to the specified class.
1769 * Additional attributes specified in the groups parameter will also
1770 * be created automatically.
1771 *
1772 * A "dev" file will be created, showing the dev_t for the device, if
1773 * the dev_t is not 0,0.
1774 * If a pointer to a parent struct device is passed in, the newly created
1775 * struct device will be a child of that device in sysfs.
1776 * The pointer to the struct device will be returned from the call.
1777 * Any further sysfs files that might be required can be created using this
1778 * pointer.
1779 *
1780 * Returns &struct device pointer on success, or ERR_PTR() on error.
1781 *
1782 * Note: the struct class passed to this function must have previously
1783 * been created with a call to class_create().
1784 */
1785struct device *device_create_with_groups(struct class *class,
1786 struct device *parent, dev_t devt,
1787 void *drvdata,
1788 const struct attribute_group **groups,
1789 const char *fmt, ...)
1790{
1791 va_list vargs;
1792 struct device *dev;
1793
1794 va_start(vargs, fmt);
1795 dev = device_create_groups_vargs(class, parent, devt, drvdata, groups,
1796 fmt, vargs);
1797 va_end(vargs);
1798 return dev;
1799}
1800EXPORT_SYMBOL_GPL(device_create_with_groups);
1801
Michał Mirosław9f3b7952013-02-01 20:40:17 +01001802static int __match_devt(struct device *dev, const void *data)
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001803{
Michał Mirosław9f3b7952013-02-01 20:40:17 +01001804 const dev_t *devt = data;
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001805
Dave Youngcd354492008-01-28 16:56:11 +08001806 return dev->devt == *devt;
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001807}
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001808
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001809/**
1810 * device_destroy - removes a device that was created with device_create()
1811 * @class: pointer to the struct class that this device was registered with
1812 * @devt: the dev_t of the device that was previously registered
1813 *
1814 * This call unregisters and cleans up a device that was created with a
1815 * call to device_create().
1816 */
1817void device_destroy(struct class *class, dev_t devt)
1818{
1819 struct device *dev;
1820
Greg Kroah-Hartman695794a2008-05-22 17:21:08 -04001821 dev = class_find_device(class, NULL, &devt, __match_devt);
Dave Youngcd354492008-01-28 16:56:11 +08001822 if (dev) {
1823 put_device(dev);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001824 device_unregister(dev);
Dave Youngcd354492008-01-28 16:56:11 +08001825 }
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001826}
1827EXPORT_SYMBOL_GPL(device_destroy);
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001828
1829/**
1830 * device_rename - renames a device
1831 * @dev: the pointer to the struct device to be renamed
1832 * @new_name: the new name of the device
Eric W. Biederman030c1d22008-05-08 14:41:00 -07001833 *
1834 * It is the responsibility of the caller to provide mutual
1835 * exclusion between two different calls of device_rename
1836 * on the same device to ensure that new_name is valid and
1837 * won't conflict with other devices.
Michael Ellermanc6c0ac62010-11-25 09:44:07 +11001838 *
Timur Tabia5462512010-12-13 14:08:52 -06001839 * Note: Don't call this function. Currently, the networking layer calls this
1840 * function, but that will change. The following text from Kay Sievers offers
1841 * some insight:
1842 *
1843 * Renaming devices is racy at many levels, symlinks and other stuff are not
1844 * replaced atomically, and you get a "move" uevent, but it's not easy to
1845 * connect the event to the old and new device. Device nodes are not renamed at
1846 * all, there isn't even support for that in the kernel now.
1847 *
1848 * In the meantime, during renaming, your target name might be taken by another
1849 * driver, creating conflicts. Or the old name is taken directly after you
1850 * renamed it -- then you get events for the same DEVPATH, before you even see
1851 * the "move" event. It's just a mess, and nothing new should ever rely on
1852 * kernel device renaming. Besides that, it's not even implemented now for
1853 * other things than (driver-core wise very simple) network devices.
1854 *
1855 * We are currently about to change network renaming in udev to completely
1856 * disallow renaming of devices in the same namespace as the kernel uses,
1857 * because we can't solve the problems properly, that arise with swapping names
1858 * of multiple interfaces without races. Means, renaming of eth[0-9]* will only
1859 * be allowed to some other name than eth[0-9]*, for the aforementioned
1860 * reasons.
1861 *
1862 * Make up a "real" name in the driver before you register anything, or add
1863 * some other attributes for userspace to find the device, or use udev to add
1864 * symlinks -- but never rename kernel devices later, it's a complete mess. We
1865 * don't even want to get into that and try to implement the missing pieces in
1866 * the core. We really have other pieces to fix in the driver core mess. :)
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001867 */
Johannes Berg6937e8f2010-08-05 17:38:18 +02001868int device_rename(struct device *dev, const char *new_name)
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001869{
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001870 char *old_device_name = NULL;
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001871 int error;
1872
1873 dev = get_device(dev);
1874 if (!dev)
1875 return -EINVAL;
1876
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01001877 pr_debug("device: '%s': %s: renaming to '%s'\n", dev_name(dev),
Harvey Harrison2b3a3022008-03-04 16:41:05 -08001878 __func__, new_name);
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001879
Kay Sievers1fa5ae82009-01-25 15:17:37 +01001880 old_device_name = kstrdup(dev_name(dev), GFP_KERNEL);
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001881 if (!old_device_name) {
1882 error = -ENOMEM;
1883 goto out;
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001884 }
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001885
Eric W. Biedermanf349cf32010-03-30 11:31:29 -07001886 if (dev->class) {
Kay Sievers6b6e39a2010-11-15 23:13:18 +01001887 error = sysfs_rename_link(&dev->class->p->subsys.kobj,
Eric W. Biedermanf349cf32010-03-30 11:31:29 -07001888 &dev->kobj, old_device_name, new_name);
1889 if (error)
1890 goto out;
1891 }
Kay Sievers39aba962010-09-04 22:33:14 -07001892
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001893 error = kobject_rename(&dev->kobj, new_name);
Kay Sievers1fa5ae82009-01-25 15:17:37 +01001894 if (error)
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001895 goto out;
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001896
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001897out:
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001898 put_device(dev);
1899
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001900 kfree(old_device_name);
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001901
1902 return error;
1903}
Johannes Berga2807db2007-02-28 12:38:31 +01001904EXPORT_SYMBOL_GPL(device_rename);
Cornelia Huck8a824722006-11-20 17:07:51 +01001905
1906static int device_move_class_links(struct device *dev,
1907 struct device *old_parent,
1908 struct device *new_parent)
1909{
Greg Kroah-Hartmanf7f34612007-03-06 12:55:53 -08001910 int error = 0;
Cornelia Huck8a824722006-11-20 17:07:51 +01001911
Greg Kroah-Hartmanf7f34612007-03-06 12:55:53 -08001912 if (old_parent)
1913 sysfs_remove_link(&dev->kobj, "device");
1914 if (new_parent)
1915 error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
1916 "device");
1917 return error;
Cornelia Huck8a824722006-11-20 17:07:51 +01001918}
1919
1920/**
1921 * device_move - moves a device to a new parent
1922 * @dev: the pointer to the struct device to be moved
Cornelia Huckc744aeae2007-01-08 20:16:44 +01001923 * @new_parent: the new parent of the device (can by NULL)
Cornelia Huckffa6a702009-03-04 12:44:00 +01001924 * @dpm_order: how to reorder the dpm_list
Cornelia Huck8a824722006-11-20 17:07:51 +01001925 */
Cornelia Huckffa6a702009-03-04 12:44:00 +01001926int device_move(struct device *dev, struct device *new_parent,
1927 enum dpm_order dpm_order)
Cornelia Huck8a824722006-11-20 17:07:51 +01001928{
1929 int error;
1930 struct device *old_parent;
Cornelia Huckc744aeae2007-01-08 20:16:44 +01001931 struct kobject *new_parent_kobj;
Cornelia Huck8a824722006-11-20 17:07:51 +01001932
1933 dev = get_device(dev);
1934 if (!dev)
1935 return -EINVAL;
1936
Cornelia Huckffa6a702009-03-04 12:44:00 +01001937 device_pm_lock();
Cornelia Huck8a824722006-11-20 17:07:51 +01001938 new_parent = get_device(new_parent);
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001939 new_parent_kobj = get_device_parent(dev, new_parent);
Cornelia Huck63b69712008-01-21 16:09:44 +01001940
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01001941 pr_debug("device: '%s': %s: moving to '%s'\n", dev_name(dev),
1942 __func__, new_parent ? dev_name(new_parent) : "<NULL>");
Cornelia Huckc744aeae2007-01-08 20:16:44 +01001943 error = kobject_move(&dev->kobj, new_parent_kobj);
Cornelia Huck8a824722006-11-20 17:07:51 +01001944 if (error) {
Cornelia Huck63b69712008-01-21 16:09:44 +01001945 cleanup_glue_dir(dev, new_parent_kobj);
Cornelia Huck8a824722006-11-20 17:07:51 +01001946 put_device(new_parent);
1947 goto out;
1948 }
1949 old_parent = dev->parent;
1950 dev->parent = new_parent;
1951 if (old_parent)
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08001952 klist_remove(&dev->p->knode_parent);
Yinghai Lu0d358f22008-02-19 03:20:41 -08001953 if (new_parent) {
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08001954 klist_add_tail(&dev->p->knode_parent,
1955 &new_parent->p->klist_children);
Yinghai Lu0d358f22008-02-19 03:20:41 -08001956 set_dev_node(dev, dev_to_node(new_parent));
1957 }
1958
Rabin Vincentbdd40342012-04-23 09:16:36 +02001959 if (dev->class) {
1960 error = device_move_class_links(dev, old_parent, new_parent);
1961 if (error) {
1962 /* We ignore errors on cleanup since we're hosed anyway... */
1963 device_move_class_links(dev, new_parent, old_parent);
1964 if (!kobject_move(&dev->kobj, &old_parent->kobj)) {
1965 if (new_parent)
1966 klist_remove(&dev->p->knode_parent);
1967 dev->parent = old_parent;
1968 if (old_parent) {
1969 klist_add_tail(&dev->p->knode_parent,
1970 &old_parent->p->klist_children);
1971 set_dev_node(dev, dev_to_node(old_parent));
1972 }
Yinghai Lu0d358f22008-02-19 03:20:41 -08001973 }
Rabin Vincentbdd40342012-04-23 09:16:36 +02001974 cleanup_glue_dir(dev, new_parent_kobj);
1975 put_device(new_parent);
1976 goto out;
Cornelia Huck8a824722006-11-20 17:07:51 +01001977 }
Cornelia Huck8a824722006-11-20 17:07:51 +01001978 }
Cornelia Huckffa6a702009-03-04 12:44:00 +01001979 switch (dpm_order) {
1980 case DPM_ORDER_NONE:
1981 break;
1982 case DPM_ORDER_DEV_AFTER_PARENT:
1983 device_pm_move_after(dev, new_parent);
1984 break;
1985 case DPM_ORDER_PARENT_BEFORE_DEV:
1986 device_pm_move_before(new_parent, dev);
1987 break;
1988 case DPM_ORDER_DEV_LAST:
1989 device_pm_move_last(dev);
1990 break;
1991 }
Rabin Vincentbdd40342012-04-23 09:16:36 +02001992
Cornelia Huck8a824722006-11-20 17:07:51 +01001993 put_device(old_parent);
1994out:
Cornelia Huckffa6a702009-03-04 12:44:00 +01001995 device_pm_unlock();
Cornelia Huck8a824722006-11-20 17:07:51 +01001996 put_device(dev);
1997 return error;
1998}
Cornelia Huck8a824722006-11-20 17:07:51 +01001999EXPORT_SYMBOL_GPL(device_move);
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08002000
2001/**
2002 * device_shutdown - call ->shutdown() on each device to shutdown.
2003 */
2004void device_shutdown(void)
2005{
Hugh Daschbach62458382010-03-22 10:36:37 -07002006 struct device *dev;
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08002007
Hugh Daschbach62458382010-03-22 10:36:37 -07002008 spin_lock(&devices_kset->list_lock);
2009 /*
2010 * Walk the devices list backward, shutting down each in turn.
2011 * Beware that device unplug events may also start pulling
2012 * devices offline, even as the system is shutting down.
2013 */
2014 while (!list_empty(&devices_kset->list)) {
2015 dev = list_entry(devices_kset->list.prev, struct device,
2016 kobj.entry);
Ming Leid1c6c032012-06-22 18:01:40 +08002017
2018 /*
2019 * hold reference count of device's parent to
2020 * prevent it from being freed because parent's
2021 * lock is to be held
2022 */
2023 get_device(dev->parent);
Hugh Daschbach62458382010-03-22 10:36:37 -07002024 get_device(dev);
2025 /*
2026 * Make sure the device is off the kset list, in the
2027 * event that dev->*->shutdown() doesn't remove it.
2028 */
2029 list_del_init(&dev->kobj.entry);
2030 spin_unlock(&devices_kset->list_lock);
Alan Sternfe6b91f2011-12-06 23:24:52 +01002031
Ming Leid1c6c032012-06-22 18:01:40 +08002032 /* hold lock to avoid race with probe/release */
2033 if (dev->parent)
2034 device_lock(dev->parent);
2035 device_lock(dev);
2036
Alan Sternfe6b91f2011-12-06 23:24:52 +01002037 /* Don't allow any more runtime suspends */
2038 pm_runtime_get_noresume(dev);
2039 pm_runtime_barrier(dev);
Hugh Daschbach62458382010-03-22 10:36:37 -07002040
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08002041 if (dev->bus && dev->bus->shutdown) {
ShuoX Liu0246c4f2012-11-23 15:14:12 +08002042 if (initcall_debug)
2043 dev_info(dev, "shutdown\n");
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08002044 dev->bus->shutdown(dev);
2045 } else if (dev->driver && dev->driver->shutdown) {
ShuoX Liu0246c4f2012-11-23 15:14:12 +08002046 if (initcall_debug)
2047 dev_info(dev, "shutdown\n");
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08002048 dev->driver->shutdown(dev);
2049 }
Ming Leid1c6c032012-06-22 18:01:40 +08002050
2051 device_unlock(dev);
2052 if (dev->parent)
2053 device_unlock(dev->parent);
2054
Hugh Daschbach62458382010-03-22 10:36:37 -07002055 put_device(dev);
Ming Leid1c6c032012-06-22 18:01:40 +08002056 put_device(dev->parent);
Hugh Daschbach62458382010-03-22 10:36:37 -07002057
2058 spin_lock(&devices_kset->list_lock);
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08002059 }
Hugh Daschbach62458382010-03-22 10:36:37 -07002060 spin_unlock(&devices_kset->list_lock);
Shaohua Li401097e2009-05-12 13:37:57 -07002061 async_synchronize_full();
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08002062}
Joe Perches99bcf212010-06-27 01:02:34 +00002063
2064/*
2065 * Device logging functions
2066 */
2067
2068#ifdef CONFIG_PRINTK
Joe Perches666f3552012-09-12 20:14:11 -07002069static int
2070create_syslog_header(const struct device *dev, char *hdr, size_t hdrlen)
Joe Perches99bcf212010-06-27 01:02:34 +00002071{
Kay Sieversc4e00da2012-05-03 02:29:59 +02002072 const char *subsys;
Joe Perches798efc62012-09-12 20:11:29 -07002073 size_t pos = 0;
Joe Perches99bcf212010-06-27 01:02:34 +00002074
Kay Sieversc4e00da2012-05-03 02:29:59 +02002075 if (dev->class)
2076 subsys = dev->class->name;
2077 else if (dev->bus)
2078 subsys = dev->bus->name;
2079 else
Joe Perches798efc62012-09-12 20:11:29 -07002080 return 0;
Kay Sieversc4e00da2012-05-03 02:29:59 +02002081
Joe Perches798efc62012-09-12 20:11:29 -07002082 pos += snprintf(hdr + pos, hdrlen - pos, "SUBSYSTEM=%s", subsys);
Kay Sieversc4e00da2012-05-03 02:29:59 +02002083
2084 /*
2085 * Add device identifier DEVICE=:
2086 * b12:8 block dev_t
2087 * c127:3 char dev_t
2088 * n8 netdev ifindex
2089 * +sound:card0 subsystem:devname
2090 */
2091 if (MAJOR(dev->devt)) {
2092 char c;
2093
2094 if (strcmp(subsys, "block") == 0)
2095 c = 'b';
2096 else
2097 c = 'c';
Joe Perches798efc62012-09-12 20:11:29 -07002098 pos++;
2099 pos += snprintf(hdr + pos, hdrlen - pos,
2100 "DEVICE=%c%u:%u",
2101 c, MAJOR(dev->devt), MINOR(dev->devt));
Kay Sieversc4e00da2012-05-03 02:29:59 +02002102 } else if (strcmp(subsys, "net") == 0) {
2103 struct net_device *net = to_net_dev(dev);
2104
Joe Perches798efc62012-09-12 20:11:29 -07002105 pos++;
2106 pos += snprintf(hdr + pos, hdrlen - pos,
2107 "DEVICE=n%u", net->ifindex);
Kay Sieversc4e00da2012-05-03 02:29:59 +02002108 } else {
Joe Perches798efc62012-09-12 20:11:29 -07002109 pos++;
2110 pos += snprintf(hdr + pos, hdrlen - pos,
2111 "DEVICE=+%s:%s", subsys, dev_name(dev));
Kay Sieversc4e00da2012-05-03 02:29:59 +02002112 }
Jim Cromieaf7f2152012-07-19 13:46:21 -06002113
Joe Perches798efc62012-09-12 20:11:29 -07002114 return pos;
Joe Perches99bcf212010-06-27 01:02:34 +00002115}
Joe Perches798efc62012-09-12 20:11:29 -07002116EXPORT_SYMBOL(create_syslog_header);
2117
Joe Perches05e4e5b2012-09-12 20:13:37 -07002118int dev_vprintk_emit(int level, const struct device *dev,
2119 const char *fmt, va_list args)
2120{
2121 char hdr[128];
2122 size_t hdrlen;
2123
2124 hdrlen = create_syslog_header(dev, hdr, sizeof(hdr));
2125
2126 return vprintk_emit(0, level, hdrlen ? hdr : NULL, hdrlen, fmt, args);
2127}
2128EXPORT_SYMBOL(dev_vprintk_emit);
2129
2130int dev_printk_emit(int level, const struct device *dev, const char *fmt, ...)
2131{
2132 va_list args;
2133 int r;
2134
2135 va_start(args, fmt);
2136
2137 r = dev_vprintk_emit(level, dev, fmt, args);
2138
2139 va_end(args);
2140
2141 return r;
2142}
2143EXPORT_SYMBOL(dev_printk_emit);
2144
Joe Perches798efc62012-09-12 20:11:29 -07002145static int __dev_printk(const char *level, const struct device *dev,
2146 struct va_format *vaf)
2147{
Joe Perches798efc62012-09-12 20:11:29 -07002148 if (!dev)
2149 return printk("%s(NULL device *): %pV", level, vaf);
2150
Joe Perches666f3552012-09-12 20:14:11 -07002151 return dev_printk_emit(level[1] - '0', dev,
2152 "%s %s: %pV",
2153 dev_driver_string(dev), dev_name(dev), vaf);
Joe Perches798efc62012-09-12 20:11:29 -07002154}
Joe Perches99bcf212010-06-27 01:02:34 +00002155
2156int dev_printk(const char *level, const struct device *dev,
2157 const char *fmt, ...)
2158{
2159 struct va_format vaf;
2160 va_list args;
2161 int r;
2162
2163 va_start(args, fmt);
2164
2165 vaf.fmt = fmt;
2166 vaf.va = &args;
2167
2168 r = __dev_printk(level, dev, &vaf);
Joe Perches798efc62012-09-12 20:11:29 -07002169
Joe Perches99bcf212010-06-27 01:02:34 +00002170 va_end(args);
2171
2172 return r;
2173}
2174EXPORT_SYMBOL(dev_printk);
2175
2176#define define_dev_printk_level(func, kern_level) \
2177int func(const struct device *dev, const char *fmt, ...) \
2178{ \
2179 struct va_format vaf; \
2180 va_list args; \
2181 int r; \
2182 \
2183 va_start(args, fmt); \
2184 \
2185 vaf.fmt = fmt; \
2186 vaf.va = &args; \
2187 \
2188 r = __dev_printk(kern_level, dev, &vaf); \
Joe Perches798efc62012-09-12 20:11:29 -07002189 \
Joe Perches99bcf212010-06-27 01:02:34 +00002190 va_end(args); \
2191 \
2192 return r; \
2193} \
2194EXPORT_SYMBOL(func);
2195
2196define_dev_printk_level(dev_emerg, KERN_EMERG);
2197define_dev_printk_level(dev_alert, KERN_ALERT);
2198define_dev_printk_level(dev_crit, KERN_CRIT);
2199define_dev_printk_level(dev_err, KERN_ERR);
2200define_dev_printk_level(dev_warn, KERN_WARNING);
2201define_dev_printk_level(dev_notice, KERN_NOTICE);
2202define_dev_printk_level(_dev_info, KERN_INFO);
2203
2204#endif