blob: fe9f2aea84bd4499d699c04905295cf8a2a169db [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
Rafael J. Wysocki7ccf3b82019-10-09 01:29:10 +020013#include <linux/cpufreq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/device.h>
15#include <linux/err.h>
Rafael J. Wysocki97badf82015-04-03 23:23:37 +020016#include <linux/fwnode.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/init.h>
18#include <linux/module.h>
19#include <linux/slab.h>
20#include <linux/string.h>
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -070021#include <linux/kdev_t.h>
Benjamin Herrenschmidt116af372006-10-25 13:44:59 +100022#include <linux/notifier.h>
Grant Likely07d57a32012-02-01 11:22:22 -070023#include <linux/of.h>
24#include <linux/of_device.h>
Kay Sieversda231fd2007-11-21 17:29:15 +010025#include <linux/genhd.h>
Andrew Morton815d2d52008-03-04 15:09:07 -080026#include <linux/kallsyms.h>
Dave Youngf75b1c62008-05-28 09:28:39 -070027#include <linux/mutex.h>
Peter Chenaf8db152011-11-15 21:52:29 +010028#include <linux/pm_runtime.h>
Kay Sieversc4e00da2012-05-03 02:29:59 +020029#include <linux/netdevice.h>
Greg Kroah-Hartman63967682013-08-27 10:24:15 -070030#include <linux/sysfs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
32#include "base.h"
33#include "power/power.h"
34
Andi Kleene52eec12010-09-08 16:54:17 +020035#ifdef CONFIG_SYSFS_DEPRECATED
36#ifdef CONFIG_SYSFS_DEPRECATED_V2
37long sysfs_deprecated = 1;
38#else
39long sysfs_deprecated = 0;
40#endif
Hanjun Guo3454bf92013-08-17 20:42:24 +080041static int __init sysfs_deprecated_setup(char *arg)
Andi Kleene52eec12010-09-08 16:54:17 +020042{
Jingoo Han34da5e62013-07-26 13:10:22 +090043 return kstrtol(arg, 10, &sysfs_deprecated);
Andi Kleene52eec12010-09-08 16:54:17 +020044}
45early_param("sysfs.deprecated", sysfs_deprecated_setup);
46#endif
47
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -080048int (*platform_notify)(struct device *dev) = NULL;
49int (*platform_notify_remove)(struct device *dev) = NULL;
Dan Williamse105b8b2008-04-21 10:51:07 -070050static struct kobject *dev_kobj;
51struct kobject *sysfs_dev_char_kobj;
52struct kobject *sysfs_dev_block_kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
Rafael J. Wysocki5e33bc42013-08-28 21:41:01 +020054static DEFINE_MUTEX(device_hotplug_lock);
55
56void lock_device_hotplug(void)
57{
58 mutex_lock(&device_hotplug_lock);
59}
60
61void unlock_device_hotplug(void)
62{
63 mutex_unlock(&device_hotplug_lock);
64}
65
66int lock_device_hotplug_sysfs(void)
67{
68 if (mutex_trylock(&device_hotplug_lock))
69 return 0;
70
71 /* Avoid busy looping (5 ms of sleep should do). */
72 msleep(5);
73 return restart_syscall();
74}
75
Greg Kroah-Hartman4e886c22008-01-27 12:12:43 -080076#ifdef CONFIG_BLOCK
77static inline int device_is_not_partition(struct device *dev)
78{
79 return !(dev->type == &part_type);
80}
81#else
82static inline int device_is_not_partition(struct device *dev)
83{
84 return 1;
85}
86#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
Alan Stern3e956372006-06-16 17:10:48 -040088/**
89 * dev_driver_string - Return a device's driver name, if at all possible
90 * @dev: struct device to get the name of
91 *
92 * Will return the device's driver's name if it is bound to a device. If
yan9169c012012-04-20 21:08:45 +080093 * the device is not bound to a driver, it will return the name of the bus
Alan Stern3e956372006-06-16 17:10:48 -040094 * it is attached to. If it is not attached to a bus either, an empty
95 * string will be returned.
96 */
Jean Delvarebf9ca692008-07-30 12:29:21 -070097const char *dev_driver_string(const struct device *dev)
Alan Stern3e956372006-06-16 17:10:48 -040098{
Alan Stern35899722009-12-04 11:06:57 -050099 struct device_driver *drv;
100
101 /* dev->driver can change to NULL underneath us because of unbinding,
102 * so be careful about accessing it. dev->bus and dev->class should
103 * never change once they are set, so they don't need special care.
104 */
105 drv = ACCESS_ONCE(dev->driver);
106 return drv ? drv->name :
Jean Delvarea456b702007-03-09 16:33:10 +0100107 (dev->bus ? dev->bus->name :
108 (dev->class ? dev->class->name : ""));
Alan Stern3e956372006-06-16 17:10:48 -0400109}
Matthew Wilcox310a9222006-09-23 23:35:04 -0600110EXPORT_SYMBOL(dev_driver_string);
Alan Stern3e956372006-06-16 17:10:48 -0400111
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112#define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr)
113
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800114static ssize_t dev_attr_show(struct kobject *kobj, struct attribute *attr,
115 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800117 struct device_attribute *dev_attr = to_dev_attr(attr);
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200118 struct device *dev = kobj_to_dev(kobj);
Dmitry Torokhov4a0c20b2005-04-29 01:23:47 -0500119 ssize_t ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
121 if (dev_attr->show)
Yani Ioannou54b6f352005-05-17 06:39:34 -0400122 ret = dev_attr->show(dev, dev_attr, buf);
Andrew Morton815d2d52008-03-04 15:09:07 -0800123 if (ret >= (ssize_t)PAGE_SIZE) {
Greg Kroah-Hartman53a9c872013-01-17 13:10:23 -0800124 print_symbol("dev_attr_show: %s returned bad count\n",
125 (unsigned long)dev_attr->show);
Andrew Morton815d2d52008-03-04 15:09:07 -0800126 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 return ret;
128}
129
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800130static ssize_t dev_attr_store(struct kobject *kobj, struct attribute *attr,
131 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800133 struct device_attribute *dev_attr = to_dev_attr(attr);
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200134 struct device *dev = kobj_to_dev(kobj);
Dmitry Torokhov4a0c20b2005-04-29 01:23:47 -0500135 ssize_t ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
137 if (dev_attr->store)
Yani Ioannou54b6f352005-05-17 06:39:34 -0400138 ret = dev_attr->store(dev, dev_attr, buf, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 return ret;
140}
141
Emese Revfy52cf25d2010-01-19 02:58:23 +0100142static const struct sysfs_ops dev_sysfs_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 .show = dev_attr_show,
144 .store = dev_attr_store,
145};
146
Kay Sieversca22e562011-12-14 14:29:38 -0800147#define to_ext_attr(x) container_of(x, struct dev_ext_attribute, attr)
148
149ssize_t device_store_ulong(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 unsigned long new = simple_strtoul(buf, &end, 0);
156 if (end == buf)
157 return -EINVAL;
158 *(unsigned long *)(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_ulong);
163
164ssize_t device_show_ulong(struct device *dev,
165 struct device_attribute *attr,
166 char *buf)
167{
168 struct dev_ext_attribute *ea = to_ext_attr(attr);
169 return snprintf(buf, PAGE_SIZE, "%lx\n", *(unsigned long *)(ea->var));
170}
171EXPORT_SYMBOL_GPL(device_show_ulong);
172
173ssize_t device_store_int(struct device *dev,
174 struct device_attribute *attr,
175 const char *buf, size_t size)
176{
177 struct dev_ext_attribute *ea = to_ext_attr(attr);
178 char *end;
179 long new = simple_strtol(buf, &end, 0);
180 if (end == buf || new > INT_MAX || new < INT_MIN)
181 return -EINVAL;
182 *(int *)(ea->var) = new;
183 /* Always return full write size even if we didn't consume all */
184 return size;
185}
186EXPORT_SYMBOL_GPL(device_store_int);
187
188ssize_t device_show_int(struct device *dev,
189 struct device_attribute *attr,
190 char *buf)
191{
192 struct dev_ext_attribute *ea = to_ext_attr(attr);
193
194 return snprintf(buf, PAGE_SIZE, "%d\n", *(int *)(ea->var));
195}
196EXPORT_SYMBOL_GPL(device_show_int);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
Borislav Petkov91872392012-10-09 19:52:05 +0200198ssize_t device_store_bool(struct device *dev, struct device_attribute *attr,
199 const char *buf, size_t size)
200{
201 struct dev_ext_attribute *ea = to_ext_attr(attr);
202
203 if (strtobool(buf, ea->var) < 0)
204 return -EINVAL;
205
206 return size;
207}
208EXPORT_SYMBOL_GPL(device_store_bool);
209
210ssize_t device_show_bool(struct device *dev, struct device_attribute *attr,
211 char *buf)
212{
213 struct dev_ext_attribute *ea = to_ext_attr(attr);
214
215 return snprintf(buf, PAGE_SIZE, "%d\n", *(bool *)(ea->var));
216}
217EXPORT_SYMBOL_GPL(device_show_bool);
218
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219/**
Robert P. J. Dayf8878dc2013-06-01 20:17:34 -0400220 * device_release - free device structure.
221 * @kobj: device's kobject.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 *
Robert P. J. Dayf8878dc2013-06-01 20:17:34 -0400223 * This is called once the reference count for the object
224 * reaches 0. We forward the call to the device's release
225 * method, which should handle actually freeing the structure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800227static void device_release(struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200229 struct device *dev = kobj_to_dev(kobj);
Greg Kroah-Hartmanfb069a52008-12-16 12:23:36 -0800230 struct device_private *p = dev->p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
Ming Leia525a3d2012-07-25 01:42:29 +0800232 /*
233 * Some platform devices are driven without driver attached
234 * and managed resources may have been acquired. Make sure
235 * all resources are released.
236 *
237 * Drivers still can add resources into device after device
238 * is deleted but alive, so release devres here to avoid
239 * possible memory leak.
240 */
241 devres_release_all(dev);
242
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 if (dev->release)
244 dev->release(dev);
Kay Sieversf9f852d2006-10-07 21:54:55 +0200245 else if (dev->type && dev->type->release)
246 dev->type->release(dev);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700247 else if (dev->class && dev->class->dev_release)
248 dev->class->dev_release(dev);
Arjan van de Venf810a5c2008-07-25 19:45:39 -0700249 else
250 WARN(1, KERN_ERR "Device '%s' does not have a release() "
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800251 "function, it is broken and must be fixed.\n",
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100252 dev_name(dev));
Greg Kroah-Hartmanfb069a52008-12-16 12:23:36 -0800253 kfree(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254}
255
Eric W. Biedermanbc451f22010-03-30 11:31:25 -0700256static const void *device_namespace(struct kobject *kobj)
257{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200258 struct device *dev = kobj_to_dev(kobj);
Eric W. Biedermanbc451f22010-03-30 11:31:25 -0700259 const void *ns = NULL;
260
261 if (dev->class && dev->class->ns_type)
262 ns = dev->class->namespace(dev);
263
264 return ns;
265}
266
Greg Kroah-Hartman8f4afc42007-10-11 10:47:49 -0600267static struct kobj_type device_ktype = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 .release = device_release,
269 .sysfs_ops = &dev_sysfs_ops,
Eric W. Biedermanbc451f22010-03-30 11:31:25 -0700270 .namespace = device_namespace,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271};
272
273
Kay Sievers312c0042005-11-16 09:00:00 +0100274static int dev_uevent_filter(struct kset *kset, struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275{
276 struct kobj_type *ktype = get_ktype(kobj);
277
Greg Kroah-Hartman8f4afc42007-10-11 10:47:49 -0600278 if (ktype == &device_ktype) {
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200279 struct device *dev = kobj_to_dev(kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 if (dev->bus)
281 return 1;
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700282 if (dev->class)
283 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 }
285 return 0;
286}
287
Kay Sievers312c0042005-11-16 09:00:00 +0100288static const char *dev_uevent_name(struct kset *kset, struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200290 struct device *dev = kobj_to_dev(kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700292 if (dev->bus)
293 return dev->bus->name;
294 if (dev->class)
295 return dev->class->name;
296 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297}
298
Kay Sievers7eff2e72007-08-14 15:15:12 +0200299static int dev_uevent(struct kset *kset, struct kobject *kobj,
300 struct kobj_uevent_env *env)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200302 struct device *dev = kobj_to_dev(kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 int retval = 0;
304
Kay Sievers6fcf53a2009-04-30 15:23:42 +0200305 /* add device node properties if present */
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700306 if (MAJOR(dev->devt)) {
Kay Sievers6fcf53a2009-04-30 15:23:42 +0200307 const char *tmp;
308 const char *name;
Al Viro2c9ede52011-07-23 20:24:48 -0400309 umode_t mode = 0;
Greg Kroah-Hartman4e4098a2013-04-11 11:43:29 -0700310 kuid_t uid = GLOBAL_ROOT_UID;
311 kgid_t gid = GLOBAL_ROOT_GID;
Kay Sievers6fcf53a2009-04-30 15:23:42 +0200312
Kay Sievers7eff2e72007-08-14 15:15:12 +0200313 add_uevent_var(env, "MAJOR=%u", MAJOR(dev->devt));
314 add_uevent_var(env, "MINOR=%u", MINOR(dev->devt));
Kay Sievers3c2670e2013-04-06 09:56:00 -0700315 name = device_get_devnode(dev, &mode, &uid, &gid, &tmp);
Kay Sievers6fcf53a2009-04-30 15:23:42 +0200316 if (name) {
317 add_uevent_var(env, "DEVNAME=%s", name);
Kay Sieverse454cea2009-09-18 23:01:12 +0200318 if (mode)
319 add_uevent_var(env, "DEVMODE=%#o", mode & 0777);
Greg Kroah-Hartman4e4098a2013-04-11 11:43:29 -0700320 if (!uid_eq(uid, GLOBAL_ROOT_UID))
321 add_uevent_var(env, "DEVUID=%u", from_kuid(&init_user_ns, uid));
322 if (!gid_eq(gid, GLOBAL_ROOT_GID))
323 add_uevent_var(env, "DEVGID=%u", from_kgid(&init_user_ns, gid));
Kay Sievers3c2670e2013-04-06 09:56:00 -0700324 kfree(tmp);
Kay Sievers6fcf53a2009-04-30 15:23:42 +0200325 }
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700326 }
327
Kay Sievers414264f2007-03-12 21:08:57 +0100328 if (dev->type && dev->type->name)
Kay Sievers7eff2e72007-08-14 15:15:12 +0200329 add_uevent_var(env, "DEVTYPE=%s", dev->type->name);
Kay Sievers414264f2007-03-12 21:08:57 +0100330
Kay Sievers239378f2006-10-07 21:54:55 +0200331 if (dev->driver)
Kay Sievers7eff2e72007-08-14 15:15:12 +0200332 add_uevent_var(env, "DRIVER=%s", dev->driver->name);
Kay Sievers239378f2006-10-07 21:54:55 +0200333
Grant Likely07d57a32012-02-01 11:22:22 -0700334 /* Add common DT information about the device */
335 of_device_uevent(dev, env);
336
Kay Sievers7eff2e72007-08-14 15:15:12 +0200337 /* have the bus specific function add its stuff */
Kay Sievers312c0042005-11-16 09:00:00 +0100338 if (dev->bus && dev->bus->uevent) {
Kay Sievers7eff2e72007-08-14 15:15:12 +0200339 retval = dev->bus->uevent(dev, env);
Kay Sieversf9f852d2006-10-07 21:54:55 +0200340 if (retval)
Greg Kroah-Hartman7dc72b22007-11-28 23:49:41 -0800341 pr_debug("device: '%s': %s: bus uevent() returned %d\n",
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100342 dev_name(dev), __func__, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 }
344
Kay Sievers7eff2e72007-08-14 15:15:12 +0200345 /* have the class specific function add its stuff */
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700346 if (dev->class && dev->class->dev_uevent) {
Kay Sievers7eff2e72007-08-14 15:15:12 +0200347 retval = dev->class->dev_uevent(dev, env);
Kay Sieversf9f852d2006-10-07 21:54:55 +0200348 if (retval)
Greg Kroah-Hartman7dc72b22007-11-28 23:49:41 -0800349 pr_debug("device: '%s': %s: class uevent() "
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100350 "returned %d\n", dev_name(dev),
Harvey Harrison2b3a3022008-03-04 16:41:05 -0800351 __func__, retval);
Kay Sieversf9f852d2006-10-07 21:54:55 +0200352 }
353
Stefan Weileef35c22010-08-06 21:11:15 +0200354 /* have the device type specific function add its stuff */
Kay Sieversf9f852d2006-10-07 21:54:55 +0200355 if (dev->type && dev->type->uevent) {
Kay Sievers7eff2e72007-08-14 15:15:12 +0200356 retval = dev->type->uevent(dev, env);
Kay Sieversf9f852d2006-10-07 21:54:55 +0200357 if (retval)
Greg Kroah-Hartman7dc72b22007-11-28 23:49:41 -0800358 pr_debug("device: '%s': %s: dev_type uevent() "
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100359 "returned %d\n", dev_name(dev),
Harvey Harrison2b3a3022008-03-04 16:41:05 -0800360 __func__, retval);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700361 }
362
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 return retval;
364}
365
Emese Revfy9cd43612009-12-31 14:52:51 +0100366static const struct kset_uevent_ops device_uevent_ops = {
Kay Sievers312c0042005-11-16 09:00:00 +0100367 .filter = dev_uevent_filter,
368 .name = dev_uevent_name,
369 .uevent = dev_uevent,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370};
371
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -0700372static ssize_t uevent_show(struct device *dev, struct device_attribute *attr,
Kay Sievers16574dc2007-04-06 01:40:38 +0200373 char *buf)
374{
375 struct kobject *top_kobj;
376 struct kset *kset;
Kay Sievers7eff2e72007-08-14 15:15:12 +0200377 struct kobj_uevent_env *env = NULL;
Kay Sievers16574dc2007-04-06 01:40:38 +0200378 int i;
379 size_t count = 0;
380 int retval;
381
382 /* search the kset, the device belongs to */
383 top_kobj = &dev->kobj;
Kay Sievers5c5daf62007-08-12 20:43:55 +0200384 while (!top_kobj->kset && top_kobj->parent)
385 top_kobj = top_kobj->parent;
Kay Sievers16574dc2007-04-06 01:40:38 +0200386 if (!top_kobj->kset)
387 goto out;
Kay Sievers5c5daf62007-08-12 20:43:55 +0200388
Kay Sievers16574dc2007-04-06 01:40:38 +0200389 kset = top_kobj->kset;
390 if (!kset->uevent_ops || !kset->uevent_ops->uevent)
391 goto out;
392
393 /* respect filter */
394 if (kset->uevent_ops && kset->uevent_ops->filter)
395 if (!kset->uevent_ops->filter(kset, &dev->kobj))
396 goto out;
397
Kay Sievers7eff2e72007-08-14 15:15:12 +0200398 env = kzalloc(sizeof(struct kobj_uevent_env), GFP_KERNEL);
399 if (!env)
Greg Kroah-Hartmanc7308c82007-05-02 14:14:11 +0200400 return -ENOMEM;
401
Kay Sievers16574dc2007-04-06 01:40:38 +0200402 /* let the kset specific function add its keys */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200403 retval = kset->uevent_ops->uevent(kset, &dev->kobj, env);
Kay Sievers16574dc2007-04-06 01:40:38 +0200404 if (retval)
405 goto out;
406
407 /* copy keys to file */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200408 for (i = 0; i < env->envp_idx; i++)
409 count += sprintf(&buf[count], "%s\n", env->envp[i]);
Kay Sievers16574dc2007-04-06 01:40:38 +0200410out:
Kay Sievers7eff2e72007-08-14 15:15:12 +0200411 kfree(env);
Kay Sievers16574dc2007-04-06 01:40:38 +0200412 return count;
413}
414
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -0700415static ssize_t uevent_store(struct device *dev, struct device_attribute *attr,
Kay Sieversa7fd6702005-10-01 14:49:43 +0200416 const char *buf, size_t count)
417{
Kay Sievers60a96a52007-07-08 22:29:26 +0200418 enum kobject_action action;
419
Kay Sievers3f5468c2010-01-14 22:54:37 +0100420 if (kobject_action_type(buf, count, &action) == 0)
Kay Sievers60a96a52007-07-08 22:29:26 +0200421 kobject_uevent(&dev->kobj, action);
Kay Sievers3f5468c2010-01-14 22:54:37 +0100422 else
423 dev_err(dev, "uevent: unknown action-string\n");
Kay Sieversa7fd6702005-10-01 14:49:43 +0200424 return count;
425}
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -0700426static DEVICE_ATTR_RW(uevent);
Kay Sieversa7fd6702005-10-01 14:49:43 +0200427
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -0700428static ssize_t online_show(struct device *dev, struct device_attribute *attr,
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +0200429 char *buf)
430{
431 bool val;
432
Rafael J. Wysocki5e33bc42013-08-28 21:41:01 +0200433 device_lock(dev);
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +0200434 val = !dev->offline;
Rafael J. Wysocki5e33bc42013-08-28 21:41:01 +0200435 device_unlock(dev);
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +0200436 return sprintf(buf, "%u\n", val);
437}
438
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -0700439static ssize_t online_store(struct device *dev, struct device_attribute *attr,
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +0200440 const char *buf, size_t count)
441{
442 bool val;
443 int ret;
444
445 ret = strtobool(buf, &val);
446 if (ret < 0)
447 return ret;
448
Rafael J. Wysocki5e33bc42013-08-28 21:41:01 +0200449 ret = lock_device_hotplug_sysfs();
450 if (ret)
451 return ret;
452
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +0200453 ret = val ? device_online(dev) : device_offline(dev);
454 unlock_device_hotplug();
455 return ret < 0 ? ret : count;
456}
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -0700457static DEVICE_ATTR_RW(online);
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +0200458
Greg Kroah-Hartmanfa6fdb32013-08-08 15:22:55 -0700459int device_add_groups(struct device *dev, const struct attribute_group **groups)
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500460{
Greg Kroah-Hartman3e9b2ba2013-08-21 13:47:50 -0700461 return sysfs_create_groups(&dev->kobj, groups);
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500462}
463
Greg Kroah-Hartmanfa6fdb32013-08-08 15:22:55 -0700464void device_remove_groups(struct device *dev,
465 const struct attribute_group **groups)
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500466{
Greg Kroah-Hartman3e9b2ba2013-08-21 13:47:50 -0700467 sysfs_remove_groups(&dev->kobj, groups);
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -0700468}
469
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700470static int device_add_attrs(struct device *dev)
471{
472 struct class *class = dev->class;
Stephen Hemmingeraed65af2011-03-28 09:12:52 -0700473 const struct device_type *type = dev->type;
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500474 int error;
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700475
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500476 if (class) {
Greg Kroah-Hartmand05a6f92013-07-14 16:05:58 -0700477 error = device_add_groups(dev, class->dev_groups);
Kay Sieversf9f852d2006-10-07 21:54:55 +0200478 if (error)
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500479 return error;
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700480 }
Kay Sieversf9f852d2006-10-07 21:54:55 +0200481
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500482 if (type) {
483 error = device_add_groups(dev, type->groups);
Kay Sieversf9f852d2006-10-07 21:54:55 +0200484 if (error)
Greg Kroah-Hartmana6b01de2013-10-05 18:19:30 -0700485 goto err_remove_class_groups;
Kay Sieversf9f852d2006-10-07 21:54:55 +0200486 }
487
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500488 error = device_add_groups(dev, dev->groups);
489 if (error)
490 goto err_remove_type_groups;
491
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +0200492 if (device_supports_offline(dev) && !dev->offline_disabled) {
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -0700493 error = device_create_file(dev, &dev_attr_online);
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +0200494 if (error)
Rafael J. Wysockiecfbf6f2013-12-12 06:11:02 +0100495 goto err_remove_dev_groups;
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +0200496 }
497
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500498 return 0;
499
Rafael J. Wysockiecfbf6f2013-12-12 06:11:02 +0100500 err_remove_dev_groups:
501 device_remove_groups(dev, dev->groups);
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500502 err_remove_type_groups:
503 if (type)
504 device_remove_groups(dev, type->groups);
Greg Kroah-Hartmand05a6f92013-07-14 16:05:58 -0700505 err_remove_class_groups:
506 if (class)
507 device_remove_groups(dev, class->dev_groups);
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500508
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700509 return error;
510}
511
512static void device_remove_attrs(struct device *dev)
513{
514 struct class *class = dev->class;
Stephen Hemmingeraed65af2011-03-28 09:12:52 -0700515 const struct device_type *type = dev->type;
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700516
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -0700517 device_remove_file(dev, &dev_attr_online);
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500518 device_remove_groups(dev, dev->groups);
Kay Sieversf9f852d2006-10-07 21:54:55 +0200519
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500520 if (type)
521 device_remove_groups(dev, type->groups);
522
Greg Kroah-Hartmana6b01de2013-10-05 18:19:30 -0700523 if (class)
Greg Kroah-Hartmand05a6f92013-07-14 16:05:58 -0700524 device_remove_groups(dev, class->dev_groups);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700525}
526
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -0700527static ssize_t dev_show(struct device *dev, struct device_attribute *attr,
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700528 char *buf)
529{
530 return print_dev_t(buf, dev->devt);
531}
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -0700532static DEVICE_ATTR_RO(dev);
Tejun Heoad6a1e12007-06-14 03:45:17 +0900533
Kay Sieversca22e562011-12-14 14:29:38 -0800534/* /sys/devices/ */
Greg Kroah-Hartman881c6cfd2007-11-01 09:29:06 -0600535struct kset *devices_kset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537/**
Grygorii Strashko52cdbdd2015-07-27 20:43:01 +0300538 * devices_kset_move_before - Move device in the devices_kset's list.
539 * @deva: Device to move.
540 * @devb: Device @deva should come before.
541 */
542static void devices_kset_move_before(struct device *deva, struct device *devb)
543{
544 if (!devices_kset)
545 return;
546 pr_debug("devices_kset: Moving %s before %s\n",
547 dev_name(deva), dev_name(devb));
548 spin_lock(&devices_kset->list_lock);
549 list_move_tail(&deva->kobj.entry, &devb->kobj.entry);
550 spin_unlock(&devices_kset->list_lock);
551}
552
553/**
554 * devices_kset_move_after - Move device in the devices_kset's list.
555 * @deva: Device to move
556 * @devb: Device @deva should come after.
557 */
558static void devices_kset_move_after(struct device *deva, struct device *devb)
559{
560 if (!devices_kset)
561 return;
562 pr_debug("devices_kset: Moving %s after %s\n",
563 dev_name(deva), dev_name(devb));
564 spin_lock(&devices_kset->list_lock);
565 list_move(&deva->kobj.entry, &devb->kobj.entry);
566 spin_unlock(&devices_kset->list_lock);
567}
568
569/**
570 * devices_kset_move_last - move the device to the end of devices_kset's list.
571 * @dev: device to move
572 */
573void devices_kset_move_last(struct device *dev)
574{
575 if (!devices_kset)
576 return;
577 pr_debug("devices_kset: Moving %s to end of list\n", dev_name(dev));
578 spin_lock(&devices_kset->list_lock);
579 list_move_tail(&dev->kobj.entry, &devices_kset->list);
580 spin_unlock(&devices_kset->list_lock);
581}
582
583/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800584 * device_create_file - create sysfs attribute file for device.
585 * @dev: device.
586 * @attr: device attribute descriptor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 */
Phil Carmody26579ab2009-12-18 15:34:19 +0200588int device_create_file(struct device *dev,
589 const struct device_attribute *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590{
591 int error = 0;
Felipe Balbi8f46baa2013-02-20 10:31:42 +0200592
593 if (dev) {
594 WARN(((attr->attr.mode & S_IWUGO) && !attr->store),
dyoung@redhat.com97521972013-05-16 14:31:30 +0800595 "Attribute %s: write permission without 'store'\n",
596 attr->attr.name);
Felipe Balbi8f46baa2013-02-20 10:31:42 +0200597 WARN(((attr->attr.mode & S_IRUGO) && !attr->show),
dyoung@redhat.com97521972013-05-16 14:31:30 +0800598 "Attribute %s: read permission without 'show'\n",
599 attr->attr.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 error = sysfs_create_file(&dev->kobj, &attr->attr);
Felipe Balbi8f46baa2013-02-20 10:31:42 +0200601 }
602
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 return error;
604}
David Graham White86df2682013-07-21 20:41:14 -0400605EXPORT_SYMBOL_GPL(device_create_file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
607/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800608 * device_remove_file - remove sysfs attribute file.
609 * @dev: device.
610 * @attr: device attribute descriptor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 */
Phil Carmody26579ab2009-12-18 15:34:19 +0200612void device_remove_file(struct device *dev,
613 const struct device_attribute *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614{
Cornelia Huck0c98b192008-01-31 10:39:38 +0100615 if (dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 sysfs_remove_file(&dev->kobj, &attr->attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617}
David Graham White86df2682013-07-21 20:41:14 -0400618EXPORT_SYMBOL_GPL(device_remove_file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619
Greg Kroah-Hartman2589f1882006-09-19 09:39:19 -0700620/**
Tejun Heo6b0afc22014-02-03 14:03:01 -0500621 * device_remove_file_self - remove sysfs attribute file from its own method.
622 * @dev: device.
623 * @attr: device attribute descriptor.
624 *
625 * See kernfs_remove_self() for details.
626 */
627bool device_remove_file_self(struct device *dev,
628 const struct device_attribute *attr)
629{
630 if (dev)
631 return sysfs_remove_file_self(&dev->kobj, &attr->attr);
632 else
633 return false;
634}
635EXPORT_SYMBOL_GPL(device_remove_file_self);
636
637/**
Greg Kroah-Hartman2589f1882006-09-19 09:39:19 -0700638 * device_create_bin_file - create sysfs binary attribute file for device.
639 * @dev: device.
640 * @attr: device binary attribute descriptor.
641 */
Phil Carmody66ecb922009-12-18 15:34:20 +0200642int device_create_bin_file(struct device *dev,
643 const struct bin_attribute *attr)
Greg Kroah-Hartman2589f1882006-09-19 09:39:19 -0700644{
645 int error = -EINVAL;
646 if (dev)
647 error = sysfs_create_bin_file(&dev->kobj, attr);
648 return error;
649}
650EXPORT_SYMBOL_GPL(device_create_bin_file);
651
652/**
653 * device_remove_bin_file - remove sysfs binary attribute file
654 * @dev: device.
655 * @attr: device binary attribute descriptor.
656 */
Phil Carmody66ecb922009-12-18 15:34:20 +0200657void device_remove_bin_file(struct device *dev,
658 const struct bin_attribute *attr)
Greg Kroah-Hartman2589f1882006-09-19 09:39:19 -0700659{
660 if (dev)
661 sysfs_remove_bin_file(&dev->kobj, attr);
662}
663EXPORT_SYMBOL_GPL(device_remove_bin_file);
664
James Bottomley34bb61f2005-09-06 16:56:51 -0700665static void klist_children_get(struct klist_node *n)
666{
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -0800667 struct device_private *p = to_device_private_parent(n);
668 struct device *dev = p->device;
James Bottomley34bb61f2005-09-06 16:56:51 -0700669
670 get_device(dev);
671}
672
673static void klist_children_put(struct klist_node *n)
674{
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -0800675 struct device_private *p = to_device_private_parent(n);
676 struct device *dev = p->device;
James Bottomley34bb61f2005-09-06 16:56:51 -0700677
678 put_device(dev);
679}
680
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800682 * device_initialize - init device structure.
683 * @dev: device.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 *
Cornelia Huck57394112008-09-03 18:26:40 +0200685 * This prepares the device for use by other layers by initializing
686 * its fields.
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800687 * It is the first half of device_register(), if called by
Cornelia Huck57394112008-09-03 18:26:40 +0200688 * that function, though it can also be called separately, so one
689 * may use @dev's fields. In particular, get_device()/put_device()
690 * may be used for reference counting of @dev after calling this
691 * function.
692 *
Alan Sternb10d5ef2012-01-17 11:39:00 -0500693 * All fields in @dev must be initialized by the caller to 0, except
694 * for those explicitly set to some other value. The simplest
695 * approach is to use kzalloc() to allocate the structure containing
696 * @dev.
697 *
Cornelia Huck57394112008-09-03 18:26:40 +0200698 * NOTE: Use put_device() to give up your reference instead of freeing
699 * @dev directly once you have called this function.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701void device_initialize(struct device *dev)
702{
Greg Kroah-Hartman881c6cfd2007-11-01 09:29:06 -0600703 dev->kobj.kset = devices_kset;
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -0700704 kobject_init(&dev->kobj, &device_ktype);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 INIT_LIST_HEAD(&dev->dma_pools);
Thomas Gleixner31427882010-01-29 20:39:02 +0000706 mutex_init(&dev->mutex);
Peter Zijlstra1704f472010-03-19 01:37:42 +0100707 lockdep_set_novalidate_class(&dev->mutex);
Tejun Heo9ac78492007-01-20 16:00:26 +0900708 spin_lock_init(&dev->devres_lock);
709 INIT_LIST_HEAD(&dev->devres_head);
Alan Stern3b98aea2008-08-07 13:06:12 -0400710 device_pm_init(dev);
Christoph Hellwig87348132006-12-06 20:32:33 -0800711 set_dev_node(dev, -1);
Jiang Liu4a7cc832015-07-09 16:00:44 +0800712#ifdef CONFIG_GENERIC_MSI_IRQ
713 INIT_LIST_HEAD(&dev->msi_list);
714#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715}
David Graham White86df2682013-07-21 20:41:14 -0400716EXPORT_SYMBOL_GPL(device_initialize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
Tejun Heod73ce002013-03-12 11:30:05 -0700718struct kobject *virtual_device_parent(struct device *dev)
Greg Kroah-Hartmanf0ee61a2006-10-23 10:40:54 -0700719{
Kay Sievers86406242007-03-14 03:25:56 +0100720 static struct kobject *virtual_dir = NULL;
Greg Kroah-Hartmanf0ee61a2006-10-23 10:40:54 -0700721
Kay Sievers86406242007-03-14 03:25:56 +0100722 if (!virtual_dir)
Greg Kroah-Hartman4ff6abf2007-11-05 22:24:43 -0800723 virtual_dir = kobject_create_and_add("virtual",
Greg Kroah-Hartman881c6cfd2007-11-01 09:29:06 -0600724 &devices_kset->kobj);
Greg Kroah-Hartmanf0ee61a2006-10-23 10:40:54 -0700725
Kay Sievers86406242007-03-14 03:25:56 +0100726 return virtual_dir;
Greg Kroah-Hartmanf0ee61a2006-10-23 10:40:54 -0700727}
728
Eric W. Biedermanbc451f22010-03-30 11:31:25 -0700729struct class_dir {
730 struct kobject kobj;
731 struct class *class;
732};
733
734#define to_class_dir(obj) container_of(obj, struct class_dir, kobj)
735
736static void class_dir_release(struct kobject *kobj)
737{
738 struct class_dir *dir = to_class_dir(kobj);
739 kfree(dir);
740}
741
742static const
743struct kobj_ns_type_operations *class_dir_child_ns_type(struct kobject *kobj)
744{
745 struct class_dir *dir = to_class_dir(kobj);
746 return dir->class->ns_type;
747}
748
749static struct kobj_type class_dir_ktype = {
750 .release = class_dir_release,
751 .sysfs_ops = &kobj_sysfs_ops,
752 .child_ns_type = class_dir_child_ns_type
753};
754
755static struct kobject *
756class_dir_create_and_add(struct class *class, struct kobject *parent_kobj)
757{
758 struct class_dir *dir;
759 int retval;
760
761 dir = kzalloc(sizeof(*dir), GFP_KERNEL);
762 if (!dir)
Tetsuo Handa4f65ebc2018-05-07 19:10:31 +0900763 return ERR_PTR(-ENOMEM);
Eric W. Biedermanbc451f22010-03-30 11:31:25 -0700764
765 dir->class = class;
766 kobject_init(&dir->kobj, &class_dir_ktype);
767
Kay Sievers6b6e39a2010-11-15 23:13:18 +0100768 dir->kobj.kset = &class->p->glue_dirs;
Eric W. Biedermanbc451f22010-03-30 11:31:25 -0700769
770 retval = kobject_add(&dir->kobj, parent_kobj, "%s", class->name);
771 if (retval < 0) {
772 kobject_put(&dir->kobj);
Tetsuo Handa4f65ebc2018-05-07 19:10:31 +0900773 return ERR_PTR(retval);
Eric W. Biedermanbc451f22010-03-30 11:31:25 -0700774 }
775 return &dir->kobj;
776}
777
Yijing Wange4a60d12014-11-07 12:05:49 +0800778static DEFINE_MUTEX(gdp_mutex);
Eric W. Biedermanbc451f22010-03-30 11:31:25 -0700779
Kay Sieversda231fd2007-11-21 17:29:15 +0100780static struct kobject *get_device_parent(struct device *dev,
781 struct device *parent)
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +0100782{
Kay Sievers86406242007-03-14 03:25:56 +0100783 if (dev->class) {
784 struct kobject *kobj = NULL;
785 struct kobject *parent_kobj;
786 struct kobject *k;
787
Randy Dunlapead454f2010-09-24 14:36:49 -0700788#ifdef CONFIG_BLOCK
Kay Sievers39aba962010-09-04 22:33:14 -0700789 /* block disks show up in /sys/block */
Andi Kleene52eec12010-09-08 16:54:17 +0200790 if (sysfs_deprecated && dev->class == &block_class) {
Kay Sievers39aba962010-09-04 22:33:14 -0700791 if (parent && parent->class == &block_class)
792 return &parent->kobj;
Kay Sievers6b6e39a2010-11-15 23:13:18 +0100793 return &block_class.p->subsys.kobj;
Kay Sievers39aba962010-09-04 22:33:14 -0700794 }
Randy Dunlapead454f2010-09-24 14:36:49 -0700795#endif
Andi Kleene52eec12010-09-08 16:54:17 +0200796
Kay Sievers86406242007-03-14 03:25:56 +0100797 /*
798 * If we have no parent, we live in "virtual".
Kay Sievers0f4dafc2007-12-19 01:40:42 +0100799 * Class-devices with a non class-device as parent, live
800 * in a "glue" directory to prevent namespace collisions.
Kay Sievers86406242007-03-14 03:25:56 +0100801 */
802 if (parent == NULL)
803 parent_kobj = virtual_device_parent(dev);
Eric W. Biederman24b14422010-07-24 22:43:35 -0700804 else if (parent->class && !dev->class->ns_type)
Kay Sievers86406242007-03-14 03:25:56 +0100805 return &parent->kobj;
806 else
807 parent_kobj = &parent->kobj;
808
Tejun Heo77d3d7c2010-02-05 17:57:02 +0900809 mutex_lock(&gdp_mutex);
810
Kay Sievers86406242007-03-14 03:25:56 +0100811 /* find our class-directory at the parent and reference it */
Kay Sievers6b6e39a2010-11-15 23:13:18 +0100812 spin_lock(&dev->class->p->glue_dirs.list_lock);
813 list_for_each_entry(k, &dev->class->p->glue_dirs.list, entry)
Kay Sievers86406242007-03-14 03:25:56 +0100814 if (k->parent == parent_kobj) {
815 kobj = kobject_get(k);
816 break;
817 }
Kay Sievers6b6e39a2010-11-15 23:13:18 +0100818 spin_unlock(&dev->class->p->glue_dirs.list_lock);
Tejun Heo77d3d7c2010-02-05 17:57:02 +0900819 if (kobj) {
820 mutex_unlock(&gdp_mutex);
Kay Sievers86406242007-03-14 03:25:56 +0100821 return kobj;
Tejun Heo77d3d7c2010-02-05 17:57:02 +0900822 }
Kay Sievers86406242007-03-14 03:25:56 +0100823
824 /* or create a new class-directory at the parent device */
Eric W. Biedermanbc451f22010-03-30 11:31:25 -0700825 k = class_dir_create_and_add(dev->class, parent_kobj);
Kay Sievers0f4dafc2007-12-19 01:40:42 +0100826 /* do not emit an uevent for this simple "glue" directory */
Tejun Heo77d3d7c2010-02-05 17:57:02 +0900827 mutex_unlock(&gdp_mutex);
Greg Kroah-Hartman43968d22007-11-05 22:24:43 -0800828 return k;
Kay Sievers86406242007-03-14 03:25:56 +0100829 }
830
Kay Sieversca22e562011-12-14 14:29:38 -0800831 /* subsystems can specify a default root directory for their devices */
832 if (!parent && dev->bus && dev->bus->dev_root)
833 return &dev->bus->dev_root->kobj;
834
Kay Sievers86406242007-03-14 03:25:56 +0100835 if (parent)
Cornelia Huckc744aeae2007-01-08 20:16:44 +0100836 return &parent->kobj;
837 return NULL;
838}
Kay Sieversda231fd2007-11-21 17:29:15 +0100839
Ming Leicebf8fd2016-07-10 19:27:36 +0800840static inline bool live_in_glue_dir(struct kobject *kobj,
841 struct device *dev)
842{
843 if (!kobj || !dev->class ||
844 kobj->kset != &dev->class->p->glue_dirs)
845 return false;
846 return true;
847}
848
849static inline struct kobject *get_glue_dir(struct device *dev)
850{
851 return dev->kobj.parent;
852}
853
854/*
855 * make sure cleaning up dir as the last step, we need to make
856 * sure .release handler of kobject is run with holding the
857 * global lock
858 */
Cornelia Huck63b69712008-01-21 16:09:44 +0100859static void cleanup_glue_dir(struct device *dev, struct kobject *glue_dir)
Kay Sieversda231fd2007-11-21 17:29:15 +0100860{
Muchun Song052278b2019-07-27 11:21:22 +0800861 unsigned int ref;
862
Kay Sievers0f4dafc2007-12-19 01:40:42 +0100863 /* see if we live in a "glue" directory */
Ming Leicebf8fd2016-07-10 19:27:36 +0800864 if (!live_in_glue_dir(glue_dir, dev))
Kay Sieversda231fd2007-11-21 17:29:15 +0100865 return;
866
Yijing Wange4a60d12014-11-07 12:05:49 +0800867 mutex_lock(&gdp_mutex);
Muchun Song052278b2019-07-27 11:21:22 +0800868 /**
869 * There is a race condition between removing glue directory
870 * and adding a new device under the glue directory.
871 *
872 * CPU1: CPU2:
873 *
874 * device_add()
875 * get_device_parent()
876 * class_dir_create_and_add()
877 * kobject_add_internal()
878 * create_dir() // create glue_dir
879 *
880 * device_add()
881 * get_device_parent()
882 * kobject_get() // get glue_dir
883 *
884 * device_del()
885 * cleanup_glue_dir()
886 * kobject_del(glue_dir)
887 *
888 * kobject_add()
889 * kobject_add_internal()
890 * create_dir() // in glue_dir
891 * sysfs_create_dir_ns()
892 * kernfs_create_dir_ns(sd)
893 *
894 * sysfs_remove_dir() // glue_dir->sd=NULL
895 * sysfs_put() // free glue_dir->sd
896 *
897 * // sd is freed
898 * kernfs_new_node(sd)
899 * kernfs_get(glue_dir)
900 * kernfs_add_one()
901 * kernfs_put()
902 *
903 * Before CPU1 remove last child device under glue dir, if CPU2 add
904 * a new device under glue dir, the glue_dir kobject reference count
905 * will be increase to 2 in kobject_get(k). And CPU2 has been called
906 * kernfs_create_dir_ns(). Meanwhile, CPU1 call sysfs_remove_dir()
907 * and sysfs_put(). This result in glue_dir->sd is freed.
908 *
909 * Then the CPU2 will see a stale "empty" but still potentially used
910 * glue dir around in kernfs_new_node().
911 *
912 * In order to avoid this happening, we also should make sure that
913 * kernfs_node for glue_dir is released in CPU1 only when refcount
914 * for glue_dir kobj is 1.
915 */
916 ref = atomic_read(&glue_dir->kref.refcount);
917 if (!kobject_has_children(glue_dir) && !--ref)
Benjamin Herrenschmidt50091942018-07-10 10:29:10 +1000918 kobject_del(glue_dir);
Kay Sievers0f4dafc2007-12-19 01:40:42 +0100919 kobject_put(glue_dir);
Yijing Wange4a60d12014-11-07 12:05:49 +0800920 mutex_unlock(&gdp_mutex);
Kay Sieversda231fd2007-11-21 17:29:15 +0100921}
Cornelia Huck63b69712008-01-21 16:09:44 +0100922
Cornelia Huck2ee97ca2007-07-18 01:43:47 -0700923static int device_add_class_symlinks(struct device *dev)
924{
Benjamin Herrenschmidt5590f312015-02-18 11:25:18 +1100925 struct device_node *of_node = dev_of_node(dev);
Cornelia Huck2ee97ca2007-07-18 01:43:47 -0700926 int error;
927
Benjamin Herrenschmidt5590f312015-02-18 11:25:18 +1100928 if (of_node) {
929 error = sysfs_create_link(&dev->kobj, &of_node->kobj,"of_node");
930 if (error)
931 dev_warn(dev, "Error %d creating of_node link\n",error);
932 /* An error here doesn't warrant bringing down the device */
933 }
934
Cornelia Huck2ee97ca2007-07-18 01:43:47 -0700935 if (!dev->class)
936 return 0;
Kay Sieversda231fd2007-11-21 17:29:15 +0100937
Greg Kroah-Hartman1fbfee62008-05-28 09:28:39 -0700938 error = sysfs_create_link(&dev->kobj,
Kay Sievers6b6e39a2010-11-15 23:13:18 +0100939 &dev->class->p->subsys.kobj,
Cornelia Huck2ee97ca2007-07-18 01:43:47 -0700940 "subsystem");
941 if (error)
Benjamin Herrenschmidt5590f312015-02-18 11:25:18 +1100942 goto out_devnode;
Kay Sieversda231fd2007-11-21 17:29:15 +0100943
Greg Kroah-Hartman4e886c22008-01-27 12:12:43 -0800944 if (dev->parent && device_is_not_partition(dev)) {
Dmitry Torokhov4f01a752007-09-18 22:46:50 -0700945 error = sysfs_create_link(&dev->kobj, &dev->parent->kobj,
946 "device");
947 if (error)
Kay Sievers39aba962010-09-04 22:33:14 -0700948 goto out_subsys;
Cornelia Huck2ee97ca2007-07-18 01:43:47 -0700949 }
Kay Sievers39aba962010-09-04 22:33:14 -0700950
Randy Dunlapead454f2010-09-24 14:36:49 -0700951#ifdef CONFIG_BLOCK
Kay Sievers39aba962010-09-04 22:33:14 -0700952 /* /sys/block has directories and does not need symlinks */
Andi Kleene52eec12010-09-08 16:54:17 +0200953 if (sysfs_deprecated && dev->class == &block_class)
Kay Sievers39aba962010-09-04 22:33:14 -0700954 return 0;
Randy Dunlapead454f2010-09-24 14:36:49 -0700955#endif
Kay Sievers39aba962010-09-04 22:33:14 -0700956
957 /* link in the class directory pointing to the device */
Kay Sievers6b6e39a2010-11-15 23:13:18 +0100958 error = sysfs_create_link(&dev->class->p->subsys.kobj,
Kay Sievers39aba962010-09-04 22:33:14 -0700959 &dev->kobj, dev_name(dev));
960 if (error)
961 goto out_device;
962
Cornelia Huck2ee97ca2007-07-18 01:43:47 -0700963 return 0;
964
Kay Sievers39aba962010-09-04 22:33:14 -0700965out_device:
966 sysfs_remove_link(&dev->kobj, "device");
Kay Sieversda231fd2007-11-21 17:29:15 +0100967
Cornelia Huck2ee97ca2007-07-18 01:43:47 -0700968out_subsys:
969 sysfs_remove_link(&dev->kobj, "subsystem");
Benjamin Herrenschmidt5590f312015-02-18 11:25:18 +1100970out_devnode:
971 sysfs_remove_link(&dev->kobj, "of_node");
Cornelia Huck2ee97ca2007-07-18 01:43:47 -0700972 return error;
973}
974
975static void device_remove_class_symlinks(struct device *dev)
976{
Benjamin Herrenschmidt5590f312015-02-18 11:25:18 +1100977 if (dev_of_node(dev))
978 sysfs_remove_link(&dev->kobj, "of_node");
979
Cornelia Huck2ee97ca2007-07-18 01:43:47 -0700980 if (!dev->class)
981 return;
Kay Sieversda231fd2007-11-21 17:29:15 +0100982
Greg Kroah-Hartman4e886c22008-01-27 12:12:43 -0800983 if (dev->parent && device_is_not_partition(dev))
Kay Sieversda231fd2007-11-21 17:29:15 +0100984 sysfs_remove_link(&dev->kobj, "device");
Cornelia Huck2ee97ca2007-07-18 01:43:47 -0700985 sysfs_remove_link(&dev->kobj, "subsystem");
Randy Dunlapead454f2010-09-24 14:36:49 -0700986#ifdef CONFIG_BLOCK
Andi Kleene52eec12010-09-08 16:54:17 +0200987 if (sysfs_deprecated && dev->class == &block_class)
Kay Sievers39aba962010-09-04 22:33:14 -0700988 return;
Randy Dunlapead454f2010-09-24 14:36:49 -0700989#endif
Kay Sievers6b6e39a2010-11-15 23:13:18 +0100990 sysfs_delete_link(&dev->class->p->subsys.kobj, &dev->kobj, dev_name(dev));
Cornelia Huck2ee97ca2007-07-18 01:43:47 -0700991}
992
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993/**
Stephen Rothwell413c2392008-05-30 10:16:40 +1000994 * dev_set_name - set a device name
995 * @dev: device
Randy Dunlap46232362008-06-04 21:40:43 -0700996 * @fmt: format string for the device's name
Stephen Rothwell413c2392008-05-30 10:16:40 +1000997 */
998int dev_set_name(struct device *dev, const char *fmt, ...)
999{
1000 va_list vargs;
Kay Sievers1fa5ae82009-01-25 15:17:37 +01001001 int err;
Stephen Rothwell413c2392008-05-30 10:16:40 +10001002
1003 va_start(vargs, fmt);
Kay Sievers1fa5ae82009-01-25 15:17:37 +01001004 err = kobject_set_name_vargs(&dev->kobj, fmt, vargs);
Stephen Rothwell413c2392008-05-30 10:16:40 +10001005 va_end(vargs);
Kay Sievers1fa5ae82009-01-25 15:17:37 +01001006 return err;
Stephen Rothwell413c2392008-05-30 10:16:40 +10001007}
1008EXPORT_SYMBOL_GPL(dev_set_name);
1009
1010/**
Dan Williamse105b8b2008-04-21 10:51:07 -07001011 * device_to_dev_kobj - select a /sys/dev/ directory for the device
1012 * @dev: device
1013 *
1014 * By default we select char/ for new entries. Setting class->dev_obj
1015 * to NULL prevents an entry from being created. class->dev_kobj must
1016 * be set (or cleared) before any devices are registered to the class
1017 * otherwise device_create_sys_dev_entry() and
Peter Korsgaard0d4e293c2012-04-17 12:12:57 +02001018 * device_remove_sys_dev_entry() will disagree about the presence of
1019 * the link.
Dan Williamse105b8b2008-04-21 10:51:07 -07001020 */
1021static struct kobject *device_to_dev_kobj(struct device *dev)
1022{
1023 struct kobject *kobj;
1024
1025 if (dev->class)
1026 kobj = dev->class->dev_kobj;
1027 else
1028 kobj = sysfs_dev_char_kobj;
1029
1030 return kobj;
1031}
1032
1033static int device_create_sys_dev_entry(struct device *dev)
1034{
1035 struct kobject *kobj = device_to_dev_kobj(dev);
1036 int error = 0;
1037 char devt_str[15];
1038
1039 if (kobj) {
1040 format_dev_t(devt_str, dev->devt);
1041 error = sysfs_create_link(kobj, &dev->kobj, devt_str);
1042 }
1043
1044 return error;
1045}
1046
1047static void device_remove_sys_dev_entry(struct device *dev)
1048{
1049 struct kobject *kobj = device_to_dev_kobj(dev);
1050 char devt_str[15];
1051
1052 if (kobj) {
1053 format_dev_t(devt_str, dev->devt);
1054 sysfs_remove_link(kobj, devt_str);
1055 }
1056}
1057
Greg Kroah-Hartmanb4028432009-05-11 14:16:57 -07001058int device_private_init(struct device *dev)
1059{
1060 dev->p = kzalloc(sizeof(*dev->p), GFP_KERNEL);
1061 if (!dev->p)
1062 return -ENOMEM;
1063 dev->p->device = dev;
1064 klist_init(&dev->p->klist_children, klist_children_get,
1065 klist_children_put);
Greg Kroah-Hartmanef8a3fd2012-03-08 12:17:22 -08001066 INIT_LIST_HEAD(&dev->p->deferred_probe);
Greg Kroah-Hartmanb4028432009-05-11 14:16:57 -07001067 return 0;
1068}
1069
Dan Williamse105b8b2008-04-21 10:51:07 -07001070/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001071 * device_add - add device to device hierarchy.
1072 * @dev: device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001074 * This is part 2 of device_register(), though may be called
1075 * separately _iff_ device_initialize() has been called separately.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 *
Cornelia Huck57394112008-09-03 18:26:40 +02001077 * This adds @dev to the kobject hierarchy via kobject_add(), adds it
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001078 * to the global and sibling lists for the device, then
1079 * adds it to the other relevant subsystems of the driver model.
Cornelia Huck57394112008-09-03 18:26:40 +02001080 *
Alan Sternb10d5ef2012-01-17 11:39:00 -05001081 * Do not call this routine or device_register() more than once for
1082 * any device structure. The driver model core is not designed to work
1083 * with devices that get unregistered and then spring back to life.
1084 * (Among other things, it's very hard to guarantee that all references
1085 * to the previous incarnation of @dev have been dropped.) Allocate
1086 * and register a fresh new struct device instead.
1087 *
Cornelia Huck57394112008-09-03 18:26:40 +02001088 * NOTE: _Never_ directly free @dev after calling this function, even
1089 * if it returned an error! Always use put_device() to give up your
1090 * reference instead.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 */
1092int device_add(struct device *dev)
1093{
1094 struct device *parent = NULL;
Kay Sieversca22e562011-12-14 14:29:38 -08001095 struct kobject *kobj;
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02001096 struct class_interface *class_intf;
Greg Kroah-Hartmanc906a482008-05-30 10:45:12 -07001097 int error = -EINVAL;
Ming Leicebf8fd2016-07-10 19:27:36 +08001098 struct kobject *glue_dir = NULL;
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001099
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 dev = get_device(dev);
Greg Kroah-Hartmanc906a482008-05-30 10:45:12 -07001101 if (!dev)
1102 goto done;
1103
Greg Kroah-Hartmanfb069a52008-12-16 12:23:36 -08001104 if (!dev->p) {
Greg Kroah-Hartmanb4028432009-05-11 14:16:57 -07001105 error = device_private_init(dev);
1106 if (error)
1107 goto done;
Greg Kroah-Hartmanfb069a52008-12-16 12:23:36 -08001108 }
Greg Kroah-Hartmanfb069a52008-12-16 12:23:36 -08001109
Kay Sievers1fa5ae82009-01-25 15:17:37 +01001110 /*
1111 * for statically allocated devices, which should all be converted
1112 * some day, we need to initialize the name. We prevent reading back
1113 * the name, and force the use of dev_name()
1114 */
1115 if (dev->init_name) {
Greg Kroah-Hartmanacc0e902009-06-02 15:39:55 -07001116 dev_set_name(dev, "%s", dev->init_name);
Kay Sievers1fa5ae82009-01-25 15:17:37 +01001117 dev->init_name = NULL;
1118 }
Greg Kroah-Hartmanc906a482008-05-30 10:45:12 -07001119
Kay Sieversca22e562011-12-14 14:29:38 -08001120 /* subsystems can specify simple device enumeration */
1121 if (!dev_name(dev) && dev->bus && dev->bus->dev_name)
1122 dev_set_name(dev, "%s%u", dev->bus->dev_name, dev->id);
1123
Thomas Gleixnere6309e72009-12-10 19:32:49 +00001124 if (!dev_name(dev)) {
1125 error = -EINVAL;
Kay Sievers5c8563d2009-05-28 14:24:07 -07001126 goto name_error;
Thomas Gleixnere6309e72009-12-10 19:32:49 +00001127 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01001129 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
Greg Kroah-Hartmanc205ef42006-08-07 22:19:37 -07001130
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 parent = get_device(dev->parent);
Kay Sieversca22e562011-12-14 14:29:38 -08001132 kobj = get_device_parent(dev, parent);
Tetsuo Handa4f65ebc2018-05-07 19:10:31 +09001133 if (IS_ERR(kobj)) {
1134 error = PTR_ERR(kobj);
1135 goto parent_error;
1136 }
Kay Sieversca22e562011-12-14 14:29:38 -08001137 if (kobj)
1138 dev->kobj.parent = kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139
Yinghai Lu0d358f22008-02-19 03:20:41 -08001140 /* use parent numa_node */
Zhen Lei56f2de82015-08-25 12:08:22 +08001141 if (parent && (dev_to_node(dev) == NUMA_NO_NODE))
Yinghai Lu0d358f22008-02-19 03:20:41 -08001142 set_dev_node(dev, dev_to_node(parent));
1143
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 /* first, register with generic layer. */
Kay Sievers8a577ff2009-04-18 15:05:45 -07001145 /* we require the name to be set before, and pass NULL */
1146 error = kobject_add(&dev->kobj, dev->kobj.parent, NULL);
Ming Leicebf8fd2016-07-10 19:27:36 +08001147 if (error) {
1148 glue_dir = get_glue_dir(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 goto Error;
Ming Leicebf8fd2016-07-10 19:27:36 +08001150 }
Kay Sieversa7fd6702005-10-01 14:49:43 +02001151
Brian Walsh37022642006-08-14 22:43:19 -07001152 /* notify platform of device entry */
1153 if (platform_notify)
1154 platform_notify(dev);
1155
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07001156 error = device_create_file(dev, &dev_attr_uevent);
Cornelia Hucka306eea2006-09-22 11:37:13 +02001157 if (error)
1158 goto attrError;
Kay Sieversa7fd6702005-10-01 14:49:43 +02001159
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001160 error = device_add_class_symlinks(dev);
1161 if (error)
1162 goto SymlinkError;
Cornelia Huckdc0afa82007-07-09 11:39:18 -07001163 error = device_add_attrs(dev);
1164 if (error)
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07001165 goto AttrsError;
Cornelia Huckdc0afa82007-07-09 11:39:18 -07001166 error = bus_add_device(dev);
1167 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 goto BusError;
Alan Stern3b98aea2008-08-07 13:06:12 -04001169 error = dpm_sysfs_add(dev);
Rafael J. Wysocki57eee3d2008-03-12 00:59:38 +01001170 if (error)
Alan Stern3b98aea2008-08-07 13:06:12 -04001171 goto DPMError;
1172 device_pm_add(dev);
Alan Sternec0676ee2008-12-05 14:10:31 -05001173
Sergey Klyaus0cd75042014-10-08 11:31:54 +04001174 if (MAJOR(dev->devt)) {
1175 error = device_create_file(dev, &dev_attr_dev);
1176 if (error)
1177 goto DevAttrError;
1178
1179 error = device_create_sys_dev_entry(dev);
1180 if (error)
1181 goto SysEntryError;
1182
1183 devtmpfs_create_node(dev);
1184 }
1185
Alan Sternec0676ee2008-12-05 14:10:31 -05001186 /* Notify clients of device addition. This call must come
majianpeng268863f2012-01-11 15:12:06 +00001187 * after dpm_sysfs_add() and before kobject_uevent().
Alan Sternec0676ee2008-12-05 14:10:31 -05001188 */
1189 if (dev->bus)
1190 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
1191 BUS_NOTIFY_ADD_DEVICE, dev);
1192
Cornelia Huck83b5fb4c2007-03-29 11:12:11 +02001193 kobject_uevent(&dev->kobj, KOBJ_ADD);
Alan Stern2023c612009-07-30 15:27:18 -04001194 bus_probe_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 if (parent)
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08001196 klist_add_tail(&dev->p->knode_parent,
1197 &parent->p->klist_children);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198
Greg Kroah-Hartman5d9fd162006-06-22 17:17:32 -07001199 if (dev->class) {
Kay Sieversca22e562011-12-14 14:29:38 -08001200 mutex_lock(&dev->class->p->mutex);
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02001201 /* tie the class to the device */
Tejun Heo5a3ceb82008-08-25 19:50:19 +02001202 klist_add_tail(&dev->knode_class,
Kay Sievers6b6e39a2010-11-15 23:13:18 +01001203 &dev->class->p->klist_devices);
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02001204
1205 /* notify any interfaces that the device is here */
Greg Kroah-Hartman184f1f72008-05-28 09:28:39 -07001206 list_for_each_entry(class_intf,
Kay Sieversca22e562011-12-14 14:29:38 -08001207 &dev->class->p->interfaces, node)
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02001208 if (class_intf->add_dev)
1209 class_intf->add_dev(dev, class_intf);
Kay Sieversca22e562011-12-14 14:29:38 -08001210 mutex_unlock(&dev->class->p->mutex);
Greg Kroah-Hartman5d9fd162006-06-22 17:17:32 -07001211 }
Greg Kroah-Hartmanc906a482008-05-30 10:45:12 -07001212done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 put_device(dev);
1214 return error;
Sergey Klyaus0cd75042014-10-08 11:31:54 +04001215 SysEntryError:
1216 if (MAJOR(dev->devt))
1217 device_remove_file(dev, &dev_attr_dev);
1218 DevAttrError:
1219 device_pm_remove(dev);
1220 dpm_sysfs_remove(dev);
Alan Stern3b98aea2008-08-07 13:06:12 -04001221 DPMError:
Rafael J. Wysocki57eee3d2008-03-12 00:59:38 +01001222 bus_remove_device(dev);
1223 BusError:
James Simmons82f0cf92007-02-21 17:44:51 +00001224 device_remove_attrs(dev);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07001225 AttrsError:
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001226 device_remove_class_symlinks(dev);
1227 SymlinkError:
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07001228 device_remove_file(dev, &dev_attr_uevent);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001229 attrError:
Kay Sievers312c0042005-11-16 09:00:00 +01001230 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
Ming Leicebf8fd2016-07-10 19:27:36 +08001231 glue_dir = get_glue_dir(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 kobject_del(&dev->kobj);
1233 Error:
Ming Leicebf8fd2016-07-10 19:27:36 +08001234 cleanup_glue_dir(dev, glue_dir);
Tetsuo Handa4f65ebc2018-05-07 19:10:31 +09001235parent_error:
Markus Elfring5f0163a2015-02-05 11:48:26 +01001236 put_device(parent);
Kay Sievers5c8563d2009-05-28 14:24:07 -07001237name_error:
1238 kfree(dev->p);
1239 dev->p = NULL;
Greg Kroah-Hartmanc906a482008-05-30 10:45:12 -07001240 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241}
David Graham White86df2682013-07-21 20:41:14 -04001242EXPORT_SYMBOL_GPL(device_add);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001245 * device_register - register a device with the system.
1246 * @dev: pointer to the device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001248 * This happens in two clean steps - initialize the device
1249 * and add it to the system. The two steps can be called
1250 * separately, but this is the easiest and most common.
1251 * I.e. you should only call the two helpers separately if
1252 * have a clearly defined need to use and refcount the device
1253 * before it is added to the hierarchy.
Cornelia Huck57394112008-09-03 18:26:40 +02001254 *
Alan Sternb10d5ef2012-01-17 11:39:00 -05001255 * For more information, see the kerneldoc for device_initialize()
1256 * and device_add().
1257 *
Cornelia Huck57394112008-09-03 18:26:40 +02001258 * NOTE: _Never_ directly free @dev after calling this function, even
1259 * if it returned an error! Always use put_device() to give up the
1260 * reference initialized in this function instead.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262int device_register(struct device *dev)
1263{
1264 device_initialize(dev);
1265 return device_add(dev);
1266}
David Graham White86df2682013-07-21 20:41:14 -04001267EXPORT_SYMBOL_GPL(device_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001270 * get_device - increment reference count for device.
1271 * @dev: device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001273 * This simply forwards the call to kobject_get(), though
1274 * we do take care to provide for the case that we get a NULL
1275 * pointer passed in.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001277struct device *get_device(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +02001279 return dev ? kobj_to_dev(kobject_get(&dev->kobj)) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280}
David Graham White86df2682013-07-21 20:41:14 -04001281EXPORT_SYMBOL_GPL(get_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001284 * put_device - decrement reference count.
1285 * @dev: device in question.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001287void put_device(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288{
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001289 /* might_sleep(); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 if (dev)
1291 kobject_put(&dev->kobj);
1292}
David Graham White86df2682013-07-21 20:41:14 -04001293EXPORT_SYMBOL_GPL(put_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001296 * device_del - delete device from system.
1297 * @dev: device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001299 * This is the first part of the device unregistration
1300 * sequence. This removes the device from the lists we control
1301 * from here, has it removed from the other driver model
1302 * subsystems it was added to in device_add(), and removes it
1303 * from the kobject hierarchy.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001305 * NOTE: this should be called manually _iff_ device_add() was
1306 * also called manually.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001308void device_del(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001310 struct device *parent = dev->parent;
Ming Leicebf8fd2016-07-10 19:27:36 +08001311 struct kobject *glue_dir = NULL;
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02001312 struct class_interface *class_intf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313
Alan Sternec0676ee2008-12-05 14:10:31 -05001314 /* Notify clients of device removal. This call must come
1315 * before dpm_sysfs_remove().
1316 */
1317 if (dev->bus)
1318 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
1319 BUS_NOTIFY_DEL_DEVICE, dev);
Alan Stern3b98aea2008-08-07 13:06:12 -04001320 dpm_sysfs_remove(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 if (parent)
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08001322 klist_del(&dev->p->knode_parent);
Dan Williamse105b8b2008-04-21 10:51:07 -07001323 if (MAJOR(dev->devt)) {
Kay Sievers2b2af542009-04-30 15:23:42 +02001324 devtmpfs_delete_node(dev);
Dan Williamse105b8b2008-04-21 10:51:07 -07001325 device_remove_sys_dev_entry(dev);
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07001326 device_remove_file(dev, &dev_attr_dev);
Dan Williamse105b8b2008-04-21 10:51:07 -07001327 }
Kay Sieversb9d9c822006-06-15 15:31:56 +02001328 if (dev->class) {
Kay Sieversda231fd2007-11-21 17:29:15 +01001329 device_remove_class_symlinks(dev);
Kay Sievers99ef3ef2006-09-14 11:23:28 +02001330
Kay Sieversca22e562011-12-14 14:29:38 -08001331 mutex_lock(&dev->class->p->mutex);
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02001332 /* notify any interfaces that the device is now gone */
Greg Kroah-Hartman184f1f72008-05-28 09:28:39 -07001333 list_for_each_entry(class_intf,
Kay Sieversca22e562011-12-14 14:29:38 -08001334 &dev->class->p->interfaces, node)
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02001335 if (class_intf->remove_dev)
1336 class_intf->remove_dev(dev, class_intf);
1337 /* remove the device from the class list */
Tejun Heo5a3ceb82008-08-25 19:50:19 +02001338 klist_del(&dev->knode_class);
Kay Sieversca22e562011-12-14 14:29:38 -08001339 mutex_unlock(&dev->class->p->mutex);
Kay Sieversb9d9c822006-06-15 15:31:56 +02001340 }
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07001341 device_remove_file(dev, &dev_attr_uevent);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07001342 device_remove_attrs(dev);
Benjamin Herrenschmidt28953532006-11-08 19:46:14 -08001343 bus_remove_device(dev);
LongX Zhang4b6d1f122012-10-25 00:21:28 +02001344 device_pm_remove(dev);
Grant Likelyd1c34142012-03-05 08:47:41 -07001345 driver_deferred_probe_del(dev);
Lukas Wunner478573c2016-07-28 02:25:41 +02001346 device_remove_properties(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347
1348 /* Notify the platform of the removal, in case they
1349 * need to do anything...
1350 */
1351 if (platform_notify_remove)
1352 platform_notify_remove(dev);
Joerg Roedel599bad32014-09-30 13:02:02 +02001353 if (dev->bus)
1354 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
1355 BUS_NOTIFY_REMOVED_DEVICE, dev);
Kay Sievers312c0042005-11-16 09:00:00 +01001356 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
Ming Leicebf8fd2016-07-10 19:27:36 +08001357 glue_dir = get_glue_dir(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 kobject_del(&dev->kobj);
Ming Leicebf8fd2016-07-10 19:27:36 +08001359 cleanup_glue_dir(dev, glue_dir);
Kay Sieversda231fd2007-11-21 17:29:15 +01001360 put_device(parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361}
David Graham White86df2682013-07-21 20:41:14 -04001362EXPORT_SYMBOL_GPL(device_del);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363
1364/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001365 * device_unregister - unregister device from system.
1366 * @dev: device going away.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001368 * We do this in two parts, like we do device_register(). First,
1369 * we remove it from all the subsystems with device_del(), then
1370 * we decrement the reference count via put_device(). If that
1371 * is the final reference count, the device will be cleaned up
1372 * via device_release() above. Otherwise, the structure will
1373 * stick around until the final reference to the device is dropped.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001375void device_unregister(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376{
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01001377 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 device_del(dev);
1379 put_device(dev);
1380}
David Graham White86df2682013-07-21 20:41:14 -04001381EXPORT_SYMBOL_GPL(device_unregister);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382
Andy Shevchenko3d060ae2015-07-27 18:04:00 +03001383static struct device *prev_device(struct klist_iter *i)
1384{
1385 struct klist_node *n = klist_prev(i);
1386 struct device *dev = NULL;
1387 struct device_private *p;
1388
1389 if (n) {
1390 p = to_device_private_parent(n);
1391 dev = p->device;
1392 }
1393 return dev;
1394}
1395
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001396static struct device *next_device(struct klist_iter *i)
mochel@digitalimplant.org36239572005-03-24 19:08:30 -08001397{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001398 struct klist_node *n = klist_next(i);
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08001399 struct device *dev = NULL;
1400 struct device_private *p;
1401
1402 if (n) {
1403 p = to_device_private_parent(n);
1404 dev = p->device;
1405 }
1406 return dev;
mochel@digitalimplant.org36239572005-03-24 19:08:30 -08001407}
1408
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409/**
Kay Sieverse454cea2009-09-18 23:01:12 +02001410 * device_get_devnode - path of device node file
Kay Sievers6fcf53a2009-04-30 15:23:42 +02001411 * @dev: device
Kay Sieverse454cea2009-09-18 23:01:12 +02001412 * @mode: returned file access mode
Kay Sievers3c2670e2013-04-06 09:56:00 -07001413 * @uid: returned file owner
1414 * @gid: returned file group
Kay Sievers6fcf53a2009-04-30 15:23:42 +02001415 * @tmp: possibly allocated string
1416 *
1417 * Return the relative path of a possible device node.
1418 * Non-default names may need to allocate a memory to compose
1419 * a name. This memory is returned in tmp and needs to be
1420 * freed by the caller.
1421 */
Kay Sieverse454cea2009-09-18 23:01:12 +02001422const char *device_get_devnode(struct device *dev,
Greg Kroah-Hartman4e4098a2013-04-11 11:43:29 -07001423 umode_t *mode, kuid_t *uid, kgid_t *gid,
Kay Sievers3c2670e2013-04-06 09:56:00 -07001424 const char **tmp)
Kay Sievers6fcf53a2009-04-30 15:23:42 +02001425{
1426 char *s;
1427
1428 *tmp = NULL;
1429
1430 /* the device type may provide a specific name */
Kay Sieverse454cea2009-09-18 23:01:12 +02001431 if (dev->type && dev->type->devnode)
Kay Sievers3c2670e2013-04-06 09:56:00 -07001432 *tmp = dev->type->devnode(dev, mode, uid, gid);
Kay Sievers6fcf53a2009-04-30 15:23:42 +02001433 if (*tmp)
1434 return *tmp;
1435
1436 /* the class may provide a specific name */
Kay Sieverse454cea2009-09-18 23:01:12 +02001437 if (dev->class && dev->class->devnode)
1438 *tmp = dev->class->devnode(dev, mode);
Kay Sievers6fcf53a2009-04-30 15:23:42 +02001439 if (*tmp)
1440 return *tmp;
1441
1442 /* return name without allocation, tmp == NULL */
1443 if (strchr(dev_name(dev), '!') == NULL)
1444 return dev_name(dev);
1445
1446 /* replace '!' in the name with '/' */
Rasmus Villemoesa29fd612015-06-25 15:02:33 -07001447 s = kstrdup(dev_name(dev), GFP_KERNEL);
1448 if (!s)
Kay Sievers6fcf53a2009-04-30 15:23:42 +02001449 return NULL;
Rasmus Villemoesa29fd612015-06-25 15:02:33 -07001450 strreplace(s, '!', '/');
1451 return *tmp = s;
Kay Sievers6fcf53a2009-04-30 15:23:42 +02001452}
1453
1454/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001455 * device_for_each_child - device child iterator.
1456 * @parent: parent struct device.
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001457 * @fn: function to be called for each device.
Robert P. J. Dayf8878dc2013-06-01 20:17:34 -04001458 * @data: data for the callback.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001460 * Iterate over @parent's child devices, and call @fn for each,
1461 * passing it @data.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001463 * We check the return of @fn each time. If it returns anything
1464 * other than 0, we break out and return that value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001466int device_for_each_child(struct device *parent, void *data,
1467 int (*fn)(struct device *dev, void *data))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468{
mochel@digitalimplant.org36239572005-03-24 19:08:30 -08001469 struct klist_iter i;
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001470 struct device *child;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 int error = 0;
1472
Greg Kroah-Hartman014c90db2009-04-15 16:00:12 -07001473 if (!parent->p)
1474 return 0;
1475
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08001476 klist_iter_init(&parent->p->klist_children, &i);
mochel@digitalimplant.org36239572005-03-24 19:08:30 -08001477 while ((child = next_device(&i)) && !error)
1478 error = fn(child, data);
1479 klist_iter_exit(&i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 return error;
1481}
David Graham White86df2682013-07-21 20:41:14 -04001482EXPORT_SYMBOL_GPL(device_for_each_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483
Cornelia Huck5ab69982006-11-16 15:42:07 +01001484/**
Andy Shevchenko3d060ae2015-07-27 18:04:00 +03001485 * device_for_each_child_reverse - device child iterator in reversed order.
1486 * @parent: parent struct device.
1487 * @fn: function to be called for each device.
1488 * @data: data for the callback.
1489 *
1490 * Iterate over @parent's child devices, and call @fn for each,
1491 * passing it @data.
1492 *
1493 * We check the return of @fn each time. If it returns anything
1494 * other than 0, we break out and return that value.
1495 */
1496int device_for_each_child_reverse(struct device *parent, void *data,
1497 int (*fn)(struct device *dev, void *data))
1498{
1499 struct klist_iter i;
1500 struct device *child;
1501 int error = 0;
1502
1503 if (!parent->p)
1504 return 0;
1505
1506 klist_iter_init(&parent->p->klist_children, &i);
1507 while ((child = prev_device(&i)) && !error)
1508 error = fn(child, data);
1509 klist_iter_exit(&i);
1510 return error;
1511}
1512EXPORT_SYMBOL_GPL(device_for_each_child_reverse);
1513
1514/**
Cornelia Huck5ab69982006-11-16 15:42:07 +01001515 * device_find_child - device iterator for locating a particular device.
1516 * @parent: parent struct device
Cornelia Huck5ab69982006-11-16 15:42:07 +01001517 * @match: Callback function to check device
Robert P. J. Dayf8878dc2013-06-01 20:17:34 -04001518 * @data: Data to pass to match function
Cornelia Huck5ab69982006-11-16 15:42:07 +01001519 *
1520 * This is similar to the device_for_each_child() function above, but it
1521 * returns a reference to a device that is 'found' for later use, as
1522 * determined by the @match callback.
1523 *
1524 * The callback should return 0 if the device doesn't match and non-zero
1525 * if it does. If the callback returns non-zero and a reference to the
1526 * current device can be obtained, this function will return to the caller
1527 * and not iterate over any more devices.
Federico Vagaa4e24002013-04-15 11:18:11 +02001528 *
1529 * NOTE: you will need to drop the reference with put_device() after use.
Cornelia Huck5ab69982006-11-16 15:42:07 +01001530 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001531struct device *device_find_child(struct device *parent, void *data,
1532 int (*match)(struct device *dev, void *data))
Cornelia Huck5ab69982006-11-16 15:42:07 +01001533{
1534 struct klist_iter i;
1535 struct device *child;
1536
1537 if (!parent)
1538 return NULL;
1539
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08001540 klist_iter_init(&parent->p->klist_children, &i);
Cornelia Huck5ab69982006-11-16 15:42:07 +01001541 while ((child = next_device(&i)))
1542 if (match(child, data) && get_device(child))
1543 break;
1544 klist_iter_exit(&i);
1545 return child;
1546}
David Graham White86df2682013-07-21 20:41:14 -04001547EXPORT_SYMBOL_GPL(device_find_child);
Cornelia Huck5ab69982006-11-16 15:42:07 +01001548
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549int __init devices_init(void)
1550{
Greg Kroah-Hartman881c6cfd2007-11-01 09:29:06 -06001551 devices_kset = kset_create_and_add("devices", &device_uevent_ops, NULL);
1552 if (!devices_kset)
1553 return -ENOMEM;
Dan Williamse105b8b2008-04-21 10:51:07 -07001554 dev_kobj = kobject_create_and_add("dev", NULL);
1555 if (!dev_kobj)
1556 goto dev_kobj_err;
1557 sysfs_dev_block_kobj = kobject_create_and_add("block", dev_kobj);
1558 if (!sysfs_dev_block_kobj)
1559 goto block_kobj_err;
1560 sysfs_dev_char_kobj = kobject_create_and_add("char", dev_kobj);
1561 if (!sysfs_dev_char_kobj)
1562 goto char_kobj_err;
1563
Greg Kroah-Hartman881c6cfd2007-11-01 09:29:06 -06001564 return 0;
Dan Williamse105b8b2008-04-21 10:51:07 -07001565
1566 char_kobj_err:
1567 kobject_put(sysfs_dev_block_kobj);
1568 block_kobj_err:
1569 kobject_put(dev_kobj);
1570 dev_kobj_err:
1571 kset_unregister(devices_kset);
1572 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573}
1574
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02001575static int device_check_offline(struct device *dev, void *not_used)
1576{
1577 int ret;
1578
1579 ret = device_for_each_child(dev, NULL, device_check_offline);
1580 if (ret)
1581 return ret;
1582
1583 return device_supports_offline(dev) && !dev->offline ? -EBUSY : 0;
1584}
1585
1586/**
1587 * device_offline - Prepare the device for hot-removal.
1588 * @dev: Device to be put offline.
1589 *
1590 * Execute the device bus type's .offline() callback, if present, to prepare
1591 * the device for a subsequent hot-removal. If that succeeds, the device must
1592 * not be used until either it is removed or its bus type's .online() callback
1593 * is executed.
1594 *
1595 * Call under device_hotplug_lock.
1596 */
1597int device_offline(struct device *dev)
1598{
1599 int ret;
1600
1601 if (dev->offline_disabled)
1602 return -EPERM;
1603
1604 ret = device_for_each_child(dev, NULL, device_check_offline);
1605 if (ret)
1606 return ret;
1607
1608 device_lock(dev);
1609 if (device_supports_offline(dev)) {
1610 if (dev->offline) {
1611 ret = 1;
1612 } else {
1613 ret = dev->bus->offline(dev);
1614 if (!ret) {
1615 kobject_uevent(&dev->kobj, KOBJ_OFFLINE);
1616 dev->offline = true;
1617 }
1618 }
1619 }
1620 device_unlock(dev);
1621
1622 return ret;
1623}
1624
1625/**
1626 * device_online - Put the device back online after successful device_offline().
1627 * @dev: Device to be put back online.
1628 *
1629 * If device_offline() has been successfully executed for @dev, but the device
1630 * has not been removed subsequently, execute its bus type's .online() callback
1631 * to indicate that the device can be used again.
1632 *
1633 * Call under device_hotplug_lock.
1634 */
1635int device_online(struct device *dev)
1636{
1637 int ret = 0;
1638
1639 device_lock(dev);
1640 if (device_supports_offline(dev)) {
1641 if (dev->offline) {
1642 ret = dev->bus->online(dev);
1643 if (!ret) {
1644 kobject_uevent(&dev->kobj, KOBJ_ONLINE);
1645 dev->offline = false;
1646 }
1647 } else {
1648 ret = 1;
1649 }
1650 }
1651 device_unlock(dev);
1652
1653 return ret;
1654}
1655
Karthigan Srinivasan7f100d12011-04-18 16:16:52 -05001656struct root_device {
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00001657 struct device dev;
1658 struct module *owner;
1659};
1660
Josh Triplett93058422012-11-18 21:27:55 -08001661static inline struct root_device *to_root_device(struct device *d)
Ferenc Wagner481e2072011-01-07 15:17:47 +01001662{
1663 return container_of(d, struct root_device, dev);
1664}
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00001665
1666static void root_device_release(struct device *dev)
1667{
1668 kfree(to_root_device(dev));
1669}
1670
1671/**
1672 * __root_device_register - allocate and register a root device
1673 * @name: root device name
1674 * @owner: owner module of the root device, usually THIS_MODULE
1675 *
1676 * This function allocates a root device and registers it
1677 * using device_register(). In order to free the returned
1678 * device, use root_device_unregister().
1679 *
1680 * Root devices are dummy devices which allow other devices
1681 * to be grouped under /sys/devices. Use this function to
1682 * allocate a root device and then use it as the parent of
1683 * any device which should appear under /sys/devices/{name}
1684 *
1685 * The /sys/devices/{name} directory will also contain a
1686 * 'module' symlink which points to the @owner directory
1687 * in sysfs.
1688 *
Jani Nikulaf0eae0e2010-03-11 18:11:45 +02001689 * Returns &struct device pointer on success, or ERR_PTR() on error.
1690 *
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00001691 * Note: You probably want to use root_device_register().
1692 */
1693struct device *__root_device_register(const char *name, struct module *owner)
1694{
1695 struct root_device *root;
1696 int err = -ENOMEM;
1697
1698 root = kzalloc(sizeof(struct root_device), GFP_KERNEL);
1699 if (!root)
1700 return ERR_PTR(err);
1701
Greg Kroah-Hartmanacc0e902009-06-02 15:39:55 -07001702 err = dev_set_name(&root->dev, "%s", name);
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00001703 if (err) {
1704 kfree(root);
1705 return ERR_PTR(err);
1706 }
1707
1708 root->dev.release = root_device_release;
1709
1710 err = device_register(&root->dev);
1711 if (err) {
1712 put_device(&root->dev);
1713 return ERR_PTR(err);
1714 }
1715
Christoph Egger1d9e8822010-05-17 16:57:58 +02001716#ifdef CONFIG_MODULES /* gotta find a "cleaner" way to do this */
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00001717 if (owner) {
1718 struct module_kobject *mk = &owner->mkobj;
1719
1720 err = sysfs_create_link(&root->dev.kobj, &mk->kobj, "module");
1721 if (err) {
1722 device_unregister(&root->dev);
1723 return ERR_PTR(err);
1724 }
1725 root->owner = owner;
1726 }
1727#endif
1728
1729 return &root->dev;
1730}
1731EXPORT_SYMBOL_GPL(__root_device_register);
1732
1733/**
1734 * root_device_unregister - unregister and free a root device
Randy Dunlap7cbcf222009-01-20 16:29:13 -08001735 * @dev: device going away
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00001736 *
1737 * This function unregisters and cleans up a device that was created by
1738 * root_device_register().
1739 */
1740void root_device_unregister(struct device *dev)
1741{
1742 struct root_device *root = to_root_device(dev);
1743
1744 if (root->owner)
1745 sysfs_remove_link(&root->dev.kobj, "module");
1746
1747 device_unregister(dev);
1748}
1749EXPORT_SYMBOL_GPL(root_device_unregister);
1750
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001751
1752static void device_create_release(struct device *dev)
1753{
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01001754 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001755 kfree(dev);
1756}
1757
Guenter Roeck39ef3112013-07-14 16:05:57 -07001758static struct device *
1759device_create_groups_vargs(struct class *class, struct device *parent,
1760 dev_t devt, void *drvdata,
1761 const struct attribute_group **groups,
1762 const char *fmt, va_list args)
1763{
1764 struct device *dev = NULL;
1765 int retval = -ENODEV;
1766
1767 if (class == NULL || IS_ERR(class))
1768 goto error;
1769
1770 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1771 if (!dev) {
1772 retval = -ENOMEM;
1773 goto error;
1774 }
1775
David Herrmannbbc780f2013-11-21 20:15:48 +01001776 device_initialize(dev);
Guenter Roeck39ef3112013-07-14 16:05:57 -07001777 dev->devt = devt;
1778 dev->class = class;
1779 dev->parent = parent;
1780 dev->groups = groups;
1781 dev->release = device_create_release;
1782 dev_set_drvdata(dev, drvdata);
1783
1784 retval = kobject_set_name_vargs(&dev->kobj, fmt, args);
1785 if (retval)
1786 goto error;
1787
David Herrmannbbc780f2013-11-21 20:15:48 +01001788 retval = device_add(dev);
Guenter Roeck39ef3112013-07-14 16:05:57 -07001789 if (retval)
1790 goto error;
1791
1792 return dev;
1793
1794error:
1795 put_device(dev);
1796 return ERR_PTR(retval);
1797}
1798
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001799/**
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07001800 * device_create_vargs - creates a device and registers it with sysfs
1801 * @class: pointer to the struct class that this device should be registered to
1802 * @parent: pointer to the parent struct device of this new device, if any
1803 * @devt: the dev_t for the char device to be added
1804 * @drvdata: the data to be added to the device for callbacks
1805 * @fmt: string for the device's name
1806 * @args: va_list for the device's name
1807 *
1808 * This function can be used by char device classes. A struct device
1809 * will be created in sysfs, registered to the specified class.
1810 *
1811 * A "dev" file will be created, showing the dev_t for the device, if
1812 * the dev_t is not 0,0.
1813 * If a pointer to a parent struct device is passed in, the newly created
1814 * struct device will be a child of that device in sysfs.
1815 * The pointer to the struct device will be returned from the call.
1816 * Any further sysfs files that might be required can be created using this
1817 * pointer.
1818 *
Jani Nikulaf0eae0e2010-03-11 18:11:45 +02001819 * Returns &struct device pointer on success, or ERR_PTR() on error.
1820 *
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07001821 * Note: the struct class passed to this function must have previously
1822 * been created with a call to class_create().
1823 */
1824struct device *device_create_vargs(struct class *class, struct device *parent,
1825 dev_t devt, void *drvdata, const char *fmt,
1826 va_list args)
1827{
Guenter Roeck39ef3112013-07-14 16:05:57 -07001828 return device_create_groups_vargs(class, parent, devt, drvdata, NULL,
1829 fmt, args);
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07001830}
1831EXPORT_SYMBOL_GPL(device_create_vargs);
1832
1833/**
Greg Kroah-Hartman4e106732008-07-21 20:03:34 -07001834 * device_create - creates a device and registers it with sysfs
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07001835 * @class: pointer to the struct class that this device should be registered to
1836 * @parent: pointer to the parent struct device of this new device, if any
1837 * @devt: the dev_t for the char device to be added
1838 * @drvdata: the data to be added to the device for callbacks
1839 * @fmt: string for the device's name
1840 *
1841 * This function can be used by char device classes. A struct device
1842 * will be created in sysfs, registered to the specified class.
1843 *
1844 * A "dev" file will be created, showing the dev_t for the device, if
1845 * the dev_t is not 0,0.
1846 * If a pointer to a parent struct device is passed in, the newly created
1847 * struct device will be a child of that device in sysfs.
1848 * The pointer to the struct device will be returned from the call.
1849 * Any further sysfs files that might be required can be created using this
1850 * pointer.
1851 *
Jani Nikulaf0eae0e2010-03-11 18:11:45 +02001852 * Returns &struct device pointer on success, or ERR_PTR() on error.
1853 *
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07001854 * Note: the struct class passed to this function must have previously
1855 * been created with a call to class_create().
1856 */
Greg Kroah-Hartman4e106732008-07-21 20:03:34 -07001857struct device *device_create(struct class *class, struct device *parent,
1858 dev_t devt, void *drvdata, const char *fmt, ...)
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07001859{
1860 va_list vargs;
1861 struct device *dev;
1862
1863 va_start(vargs, fmt);
1864 dev = device_create_vargs(class, parent, devt, drvdata, fmt, vargs);
1865 va_end(vargs);
1866 return dev;
1867}
Greg Kroah-Hartman4e106732008-07-21 20:03:34 -07001868EXPORT_SYMBOL_GPL(device_create);
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07001869
Guenter Roeck39ef3112013-07-14 16:05:57 -07001870/**
1871 * device_create_with_groups - creates a device and registers it with sysfs
1872 * @class: pointer to the struct class that this device should be registered to
1873 * @parent: pointer to the parent struct device of this new device, if any
1874 * @devt: the dev_t for the char device to be added
1875 * @drvdata: the data to be added to the device for callbacks
1876 * @groups: NULL-terminated list of attribute groups to be created
1877 * @fmt: string for the device's name
1878 *
1879 * This function can be used by char device classes. A struct device
1880 * will be created in sysfs, registered to the specified class.
1881 * Additional attributes specified in the groups parameter will also
1882 * be created automatically.
1883 *
1884 * A "dev" file will be created, showing the dev_t for the device, if
1885 * the dev_t is not 0,0.
1886 * If a pointer to a parent struct device is passed in, the newly created
1887 * struct device will be a child of that device in sysfs.
1888 * The pointer to the struct device will be returned from the call.
1889 * Any further sysfs files that might be required can be created using this
1890 * pointer.
1891 *
1892 * Returns &struct device pointer on success, or ERR_PTR() on error.
1893 *
1894 * Note: the struct class passed to this function must have previously
1895 * been created with a call to class_create().
1896 */
1897struct device *device_create_with_groups(struct class *class,
1898 struct device *parent, dev_t devt,
1899 void *drvdata,
1900 const struct attribute_group **groups,
1901 const char *fmt, ...)
1902{
1903 va_list vargs;
1904 struct device *dev;
1905
1906 va_start(vargs, fmt);
1907 dev = device_create_groups_vargs(class, parent, devt, drvdata, groups,
1908 fmt, vargs);
1909 va_end(vargs);
1910 return dev;
1911}
1912EXPORT_SYMBOL_GPL(device_create_with_groups);
1913
Michał Mirosław9f3b7952013-02-01 20:40:17 +01001914static int __match_devt(struct device *dev, const void *data)
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001915{
Michał Mirosław9f3b7952013-02-01 20:40:17 +01001916 const dev_t *devt = data;
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001917
Dave Youngcd354492008-01-28 16:56:11 +08001918 return dev->devt == *devt;
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001919}
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001920
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001921/**
1922 * device_destroy - removes a device that was created with device_create()
1923 * @class: pointer to the struct class that this device was registered with
1924 * @devt: the dev_t of the device that was previously registered
1925 *
1926 * This call unregisters and cleans up a device that was created with a
1927 * call to device_create().
1928 */
1929void device_destroy(struct class *class, dev_t devt)
1930{
1931 struct device *dev;
1932
Greg Kroah-Hartman695794a2008-05-22 17:21:08 -04001933 dev = class_find_device(class, NULL, &devt, __match_devt);
Dave Youngcd354492008-01-28 16:56:11 +08001934 if (dev) {
1935 put_device(dev);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001936 device_unregister(dev);
Dave Youngcd354492008-01-28 16:56:11 +08001937 }
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001938}
1939EXPORT_SYMBOL_GPL(device_destroy);
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001940
1941/**
1942 * device_rename - renames a device
1943 * @dev: the pointer to the struct device to be renamed
1944 * @new_name: the new name of the device
Eric W. Biederman030c1d22008-05-08 14:41:00 -07001945 *
1946 * It is the responsibility of the caller to provide mutual
1947 * exclusion between two different calls of device_rename
1948 * on the same device to ensure that new_name is valid and
1949 * won't conflict with other devices.
Michael Ellermanc6c0ac62010-11-25 09:44:07 +11001950 *
Timur Tabia5462512010-12-13 14:08:52 -06001951 * Note: Don't call this function. Currently, the networking layer calls this
1952 * function, but that will change. The following text from Kay Sievers offers
1953 * some insight:
1954 *
1955 * Renaming devices is racy at many levels, symlinks and other stuff are not
1956 * replaced atomically, and you get a "move" uevent, but it's not easy to
1957 * connect the event to the old and new device. Device nodes are not renamed at
1958 * all, there isn't even support for that in the kernel now.
1959 *
1960 * In the meantime, during renaming, your target name might be taken by another
1961 * driver, creating conflicts. Or the old name is taken directly after you
1962 * renamed it -- then you get events for the same DEVPATH, before you even see
1963 * the "move" event. It's just a mess, and nothing new should ever rely on
1964 * kernel device renaming. Besides that, it's not even implemented now for
1965 * other things than (driver-core wise very simple) network devices.
1966 *
1967 * We are currently about to change network renaming in udev to completely
1968 * disallow renaming of devices in the same namespace as the kernel uses,
1969 * because we can't solve the problems properly, that arise with swapping names
1970 * of multiple interfaces without races. Means, renaming of eth[0-9]* will only
1971 * be allowed to some other name than eth[0-9]*, for the aforementioned
1972 * reasons.
1973 *
1974 * Make up a "real" name in the driver before you register anything, or add
1975 * some other attributes for userspace to find the device, or use udev to add
1976 * symlinks -- but never rename kernel devices later, it's a complete mess. We
1977 * don't even want to get into that and try to implement the missing pieces in
1978 * the core. We really have other pieces to fix in the driver core mess. :)
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001979 */
Johannes Berg6937e8f2010-08-05 17:38:18 +02001980int device_rename(struct device *dev, const char *new_name)
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001981{
Tejun Heo4b30ee52013-09-11 22:29:06 -04001982 struct kobject *kobj = &dev->kobj;
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001983 char *old_device_name = NULL;
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001984 int error;
1985
1986 dev = get_device(dev);
1987 if (!dev)
1988 return -EINVAL;
1989
ethan.zhao69df7532013-10-13 22:12:35 +08001990 dev_dbg(dev, "renaming to %s\n", new_name);
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001991
Kay Sievers1fa5ae82009-01-25 15:17:37 +01001992 old_device_name = kstrdup(dev_name(dev), GFP_KERNEL);
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001993 if (!old_device_name) {
1994 error = -ENOMEM;
1995 goto out;
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001996 }
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001997
Eric W. Biedermanf349cf32010-03-30 11:31:29 -07001998 if (dev->class) {
Tejun Heo4b30ee52013-09-11 22:29:06 -04001999 error = sysfs_rename_link_ns(&dev->class->p->subsys.kobj,
2000 kobj, old_device_name,
2001 new_name, kobject_namespace(kobj));
Eric W. Biedermanf349cf32010-03-30 11:31:29 -07002002 if (error)
2003 goto out;
2004 }
Kay Sievers39aba962010-09-04 22:33:14 -07002005
Tejun Heo4b30ee52013-09-11 22:29:06 -04002006 error = kobject_rename(kobj, new_name);
Kay Sievers1fa5ae82009-01-25 15:17:37 +01002007 if (error)
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07002008 goto out;
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07002009
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07002010out:
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07002011 put_device(dev);
2012
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07002013 kfree(old_device_name);
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07002014
2015 return error;
2016}
Johannes Berga2807db2007-02-28 12:38:31 +01002017EXPORT_SYMBOL_GPL(device_rename);
Cornelia Huck8a824722006-11-20 17:07:51 +01002018
2019static int device_move_class_links(struct device *dev,
2020 struct device *old_parent,
2021 struct device *new_parent)
2022{
Greg Kroah-Hartmanf7f34612007-03-06 12:55:53 -08002023 int error = 0;
Cornelia Huck8a824722006-11-20 17:07:51 +01002024
Greg Kroah-Hartmanf7f34612007-03-06 12:55:53 -08002025 if (old_parent)
2026 sysfs_remove_link(&dev->kobj, "device");
2027 if (new_parent)
2028 error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
2029 "device");
2030 return error;
Cornelia Huck8a824722006-11-20 17:07:51 +01002031}
2032
2033/**
2034 * device_move - moves a device to a new parent
2035 * @dev: the pointer to the struct device to be moved
Cornelia Huckc744aeae2007-01-08 20:16:44 +01002036 * @new_parent: the new parent of the device (can by NULL)
Cornelia Huckffa6a702009-03-04 12:44:00 +01002037 * @dpm_order: how to reorder the dpm_list
Cornelia Huck8a824722006-11-20 17:07:51 +01002038 */
Cornelia Huckffa6a702009-03-04 12:44:00 +01002039int device_move(struct device *dev, struct device *new_parent,
2040 enum dpm_order dpm_order)
Cornelia Huck8a824722006-11-20 17:07:51 +01002041{
2042 int error;
2043 struct device *old_parent;
Cornelia Huckc744aeae2007-01-08 20:16:44 +01002044 struct kobject *new_parent_kobj;
Cornelia Huck8a824722006-11-20 17:07:51 +01002045
2046 dev = get_device(dev);
2047 if (!dev)
2048 return -EINVAL;
2049
Cornelia Huckffa6a702009-03-04 12:44:00 +01002050 device_pm_lock();
Cornelia Huck8a824722006-11-20 17:07:51 +01002051 new_parent = get_device(new_parent);
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002052 new_parent_kobj = get_device_parent(dev, new_parent);
Tetsuo Handa4f65ebc2018-05-07 19:10:31 +09002053 if (IS_ERR(new_parent_kobj)) {
2054 error = PTR_ERR(new_parent_kobj);
2055 put_device(new_parent);
2056 goto out;
2057 }
Cornelia Huck63b69712008-01-21 16:09:44 +01002058
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01002059 pr_debug("device: '%s': %s: moving to '%s'\n", dev_name(dev),
2060 __func__, new_parent ? dev_name(new_parent) : "<NULL>");
Cornelia Huckc744aeae2007-01-08 20:16:44 +01002061 error = kobject_move(&dev->kobj, new_parent_kobj);
Cornelia Huck8a824722006-11-20 17:07:51 +01002062 if (error) {
Cornelia Huck63b69712008-01-21 16:09:44 +01002063 cleanup_glue_dir(dev, new_parent_kobj);
Cornelia Huck8a824722006-11-20 17:07:51 +01002064 put_device(new_parent);
2065 goto out;
2066 }
2067 old_parent = dev->parent;
2068 dev->parent = new_parent;
2069 if (old_parent)
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08002070 klist_remove(&dev->p->knode_parent);
Yinghai Lu0d358f22008-02-19 03:20:41 -08002071 if (new_parent) {
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08002072 klist_add_tail(&dev->p->knode_parent,
2073 &new_parent->p->klist_children);
Yinghai Lu0d358f22008-02-19 03:20:41 -08002074 set_dev_node(dev, dev_to_node(new_parent));
2075 }
2076
Rabin Vincentbdd40342012-04-23 09:16:36 +02002077 if (dev->class) {
2078 error = device_move_class_links(dev, old_parent, new_parent);
2079 if (error) {
2080 /* We ignore errors on cleanup since we're hosed anyway... */
2081 device_move_class_links(dev, new_parent, old_parent);
2082 if (!kobject_move(&dev->kobj, &old_parent->kobj)) {
2083 if (new_parent)
2084 klist_remove(&dev->p->knode_parent);
2085 dev->parent = old_parent;
2086 if (old_parent) {
2087 klist_add_tail(&dev->p->knode_parent,
2088 &old_parent->p->klist_children);
2089 set_dev_node(dev, dev_to_node(old_parent));
2090 }
Yinghai Lu0d358f22008-02-19 03:20:41 -08002091 }
Rabin Vincentbdd40342012-04-23 09:16:36 +02002092 cleanup_glue_dir(dev, new_parent_kobj);
2093 put_device(new_parent);
2094 goto out;
Cornelia Huck8a824722006-11-20 17:07:51 +01002095 }
Cornelia Huck8a824722006-11-20 17:07:51 +01002096 }
Cornelia Huckffa6a702009-03-04 12:44:00 +01002097 switch (dpm_order) {
2098 case DPM_ORDER_NONE:
2099 break;
2100 case DPM_ORDER_DEV_AFTER_PARENT:
2101 device_pm_move_after(dev, new_parent);
Grygorii Strashko52cdbdd2015-07-27 20:43:01 +03002102 devices_kset_move_after(dev, new_parent);
Cornelia Huckffa6a702009-03-04 12:44:00 +01002103 break;
2104 case DPM_ORDER_PARENT_BEFORE_DEV:
2105 device_pm_move_before(new_parent, dev);
Grygorii Strashko52cdbdd2015-07-27 20:43:01 +03002106 devices_kset_move_before(new_parent, dev);
Cornelia Huckffa6a702009-03-04 12:44:00 +01002107 break;
2108 case DPM_ORDER_DEV_LAST:
2109 device_pm_move_last(dev);
Grygorii Strashko52cdbdd2015-07-27 20:43:01 +03002110 devices_kset_move_last(dev);
Cornelia Huckffa6a702009-03-04 12:44:00 +01002111 break;
2112 }
Rabin Vincentbdd40342012-04-23 09:16:36 +02002113
Cornelia Huck8a824722006-11-20 17:07:51 +01002114 put_device(old_parent);
2115out:
Cornelia Huckffa6a702009-03-04 12:44:00 +01002116 device_pm_unlock();
Cornelia Huck8a824722006-11-20 17:07:51 +01002117 put_device(dev);
2118 return error;
2119}
Cornelia Huck8a824722006-11-20 17:07:51 +01002120EXPORT_SYMBOL_GPL(device_move);
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08002121
2122/**
2123 * device_shutdown - call ->shutdown() on each device to shutdown.
2124 */
2125void device_shutdown(void)
2126{
Benson Leungf123db82013-09-24 20:05:11 -07002127 struct device *dev, *parent;
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08002128
Pingfan Liue6f55802018-07-19 13:14:58 +08002129 wait_for_device_probe();
2130 device_block_probing();
2131
Rafael J. Wysocki7ccf3b82019-10-09 01:29:10 +02002132 cpufreq_suspend();
2133
Hugh Daschbach62458382010-03-22 10:36:37 -07002134 spin_lock(&devices_kset->list_lock);
2135 /*
2136 * Walk the devices list backward, shutting down each in turn.
2137 * Beware that device unplug events may also start pulling
2138 * devices offline, even as the system is shutting down.
2139 */
2140 while (!list_empty(&devices_kset->list)) {
2141 dev = list_entry(devices_kset->list.prev, struct device,
2142 kobj.entry);
Ming Leid1c6c032012-06-22 18:01:40 +08002143
2144 /*
2145 * hold reference count of device's parent to
2146 * prevent it from being freed because parent's
2147 * lock is to be held
2148 */
Benson Leungf123db82013-09-24 20:05:11 -07002149 parent = get_device(dev->parent);
Hugh Daschbach62458382010-03-22 10:36:37 -07002150 get_device(dev);
2151 /*
2152 * Make sure the device is off the kset list, in the
2153 * event that dev->*->shutdown() doesn't remove it.
2154 */
2155 list_del_init(&dev->kobj.entry);
2156 spin_unlock(&devices_kset->list_lock);
Alan Sternfe6b91f2011-12-06 23:24:52 +01002157
Ming Leid1c6c032012-06-22 18:01:40 +08002158 /* hold lock to avoid race with probe/release */
Benson Leungf123db82013-09-24 20:05:11 -07002159 if (parent)
2160 device_lock(parent);
Ming Leid1c6c032012-06-22 18:01:40 +08002161 device_lock(dev);
2162
Alan Sternfe6b91f2011-12-06 23:24:52 +01002163 /* Don't allow any more runtime suspends */
2164 pm_runtime_get_noresume(dev);
2165 pm_runtime_barrier(dev);
Hugh Daschbach62458382010-03-22 10:36:37 -07002166
Josh Zimmerman5a1e1c62017-06-25 14:53:23 -07002167 if (dev->class && dev->class->shutdown) {
2168 if (initcall_debug)
2169 dev_info(dev, "shutdown\n");
2170 dev->class->shutdown(dev);
2171 } else if (dev->bus && dev->bus->shutdown) {
ShuoX Liu0246c4f2012-11-23 15:14:12 +08002172 if (initcall_debug)
2173 dev_info(dev, "shutdown\n");
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08002174 dev->bus->shutdown(dev);
2175 } else if (dev->driver && dev->driver->shutdown) {
ShuoX Liu0246c4f2012-11-23 15:14:12 +08002176 if (initcall_debug)
2177 dev_info(dev, "shutdown\n");
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08002178 dev->driver->shutdown(dev);
2179 }
Ming Leid1c6c032012-06-22 18:01:40 +08002180
2181 device_unlock(dev);
Benson Leungf123db82013-09-24 20:05:11 -07002182 if (parent)
2183 device_unlock(parent);
Ming Leid1c6c032012-06-22 18:01:40 +08002184
Hugh Daschbach62458382010-03-22 10:36:37 -07002185 put_device(dev);
Benson Leungf123db82013-09-24 20:05:11 -07002186 put_device(parent);
Hugh Daschbach62458382010-03-22 10:36:37 -07002187
2188 spin_lock(&devices_kset->list_lock);
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08002189 }
Hugh Daschbach62458382010-03-22 10:36:37 -07002190 spin_unlock(&devices_kset->list_lock);
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08002191}
Joe Perches99bcf212010-06-27 01:02:34 +00002192
2193/*
2194 * Device logging functions
2195 */
2196
2197#ifdef CONFIG_PRINTK
Joe Perches666f3552012-09-12 20:14:11 -07002198static int
2199create_syslog_header(const struct device *dev, char *hdr, size_t hdrlen)
Joe Perches99bcf212010-06-27 01:02:34 +00002200{
Kay Sieversc4e00da2012-05-03 02:29:59 +02002201 const char *subsys;
Joe Perches798efc62012-09-12 20:11:29 -07002202 size_t pos = 0;
Joe Perches99bcf212010-06-27 01:02:34 +00002203
Kay Sieversc4e00da2012-05-03 02:29:59 +02002204 if (dev->class)
2205 subsys = dev->class->name;
2206 else if (dev->bus)
2207 subsys = dev->bus->name;
2208 else
Joe Perches798efc62012-09-12 20:11:29 -07002209 return 0;
Kay Sieversc4e00da2012-05-03 02:29:59 +02002210
Joe Perches798efc62012-09-12 20:11:29 -07002211 pos += snprintf(hdr + pos, hdrlen - pos, "SUBSYSTEM=%s", subsys);
Ben Hutchings655e5b72014-08-26 00:34:44 -07002212 if (pos >= hdrlen)
2213 goto overflow;
Kay Sieversc4e00da2012-05-03 02:29:59 +02002214
2215 /*
2216 * Add device identifier DEVICE=:
2217 * b12:8 block dev_t
2218 * c127:3 char dev_t
2219 * n8 netdev ifindex
2220 * +sound:card0 subsystem:devname
2221 */
2222 if (MAJOR(dev->devt)) {
2223 char c;
2224
2225 if (strcmp(subsys, "block") == 0)
2226 c = 'b';
2227 else
2228 c = 'c';
Joe Perches798efc62012-09-12 20:11:29 -07002229 pos++;
2230 pos += snprintf(hdr + pos, hdrlen - pos,
2231 "DEVICE=%c%u:%u",
2232 c, MAJOR(dev->devt), MINOR(dev->devt));
Kay Sieversc4e00da2012-05-03 02:29:59 +02002233 } else if (strcmp(subsys, "net") == 0) {
2234 struct net_device *net = to_net_dev(dev);
2235
Joe Perches798efc62012-09-12 20:11:29 -07002236 pos++;
2237 pos += snprintf(hdr + pos, hdrlen - pos,
2238 "DEVICE=n%u", net->ifindex);
Kay Sieversc4e00da2012-05-03 02:29:59 +02002239 } else {
Joe Perches798efc62012-09-12 20:11:29 -07002240 pos++;
2241 pos += snprintf(hdr + pos, hdrlen - pos,
2242 "DEVICE=+%s:%s", subsys, dev_name(dev));
Kay Sieversc4e00da2012-05-03 02:29:59 +02002243 }
Jim Cromieaf7f2152012-07-19 13:46:21 -06002244
Ben Hutchings655e5b72014-08-26 00:34:44 -07002245 if (pos >= hdrlen)
2246 goto overflow;
2247
Joe Perches798efc62012-09-12 20:11:29 -07002248 return pos;
Ben Hutchings655e5b72014-08-26 00:34:44 -07002249
2250overflow:
2251 dev_WARN(dev, "device/subsystem name too long");
2252 return 0;
Joe Perches99bcf212010-06-27 01:02:34 +00002253}
Joe Perches798efc62012-09-12 20:11:29 -07002254
Joe Perches05e4e5b2012-09-12 20:13:37 -07002255int dev_vprintk_emit(int level, const struct device *dev,
2256 const char *fmt, va_list args)
2257{
2258 char hdr[128];
2259 size_t hdrlen;
2260
2261 hdrlen = create_syslog_header(dev, hdr, sizeof(hdr));
2262
2263 return vprintk_emit(0, level, hdrlen ? hdr : NULL, hdrlen, fmt, args);
2264}
2265EXPORT_SYMBOL(dev_vprintk_emit);
2266
2267int dev_printk_emit(int level, const struct device *dev, const char *fmt, ...)
2268{
2269 va_list args;
2270 int r;
2271
2272 va_start(args, fmt);
2273
2274 r = dev_vprintk_emit(level, dev, fmt, args);
2275
2276 va_end(args);
2277
2278 return r;
2279}
2280EXPORT_SYMBOL(dev_printk_emit);
2281
Joe Perchesd1f10522014-12-25 15:07:04 -08002282static void __dev_printk(const char *level, const struct device *dev,
Joe Perches798efc62012-09-12 20:11:29 -07002283 struct va_format *vaf)
2284{
Joe Perchesd1f10522014-12-25 15:07:04 -08002285 if (dev)
2286 dev_printk_emit(level[1] - '0', dev, "%s %s: %pV",
2287 dev_driver_string(dev), dev_name(dev), vaf);
2288 else
2289 printk("%s(NULL device *): %pV", level, vaf);
Joe Perches798efc62012-09-12 20:11:29 -07002290}
Joe Perches99bcf212010-06-27 01:02:34 +00002291
Joe Perchesd1f10522014-12-25 15:07:04 -08002292void dev_printk(const char *level, const struct device *dev,
2293 const char *fmt, ...)
Joe Perches99bcf212010-06-27 01:02:34 +00002294{
2295 struct va_format vaf;
2296 va_list args;
Joe Perches99bcf212010-06-27 01:02:34 +00002297
2298 va_start(args, fmt);
2299
2300 vaf.fmt = fmt;
2301 vaf.va = &args;
2302
Joe Perchesd1f10522014-12-25 15:07:04 -08002303 __dev_printk(level, dev, &vaf);
Joe Perches798efc62012-09-12 20:11:29 -07002304
Joe Perches99bcf212010-06-27 01:02:34 +00002305 va_end(args);
Joe Perches99bcf212010-06-27 01:02:34 +00002306}
2307EXPORT_SYMBOL(dev_printk);
2308
2309#define define_dev_printk_level(func, kern_level) \
Joe Perchesd1f10522014-12-25 15:07:04 -08002310void func(const struct device *dev, const char *fmt, ...) \
Joe Perches99bcf212010-06-27 01:02:34 +00002311{ \
2312 struct va_format vaf; \
2313 va_list args; \
Joe Perches99bcf212010-06-27 01:02:34 +00002314 \
2315 va_start(args, fmt); \
2316 \
2317 vaf.fmt = fmt; \
2318 vaf.va = &args; \
2319 \
Joe Perchesd1f10522014-12-25 15:07:04 -08002320 __dev_printk(kern_level, dev, &vaf); \
Joe Perches798efc62012-09-12 20:11:29 -07002321 \
Joe Perches99bcf212010-06-27 01:02:34 +00002322 va_end(args); \
Joe Perches99bcf212010-06-27 01:02:34 +00002323} \
2324EXPORT_SYMBOL(func);
2325
2326define_dev_printk_level(dev_emerg, KERN_EMERG);
2327define_dev_printk_level(dev_alert, KERN_ALERT);
2328define_dev_printk_level(dev_crit, KERN_CRIT);
2329define_dev_printk_level(dev_err, KERN_ERR);
2330define_dev_printk_level(dev_warn, KERN_WARNING);
2331define_dev_printk_level(dev_notice, KERN_NOTICE);
2332define_dev_printk_level(_dev_info, KERN_INFO);
2333
2334#endif
Rafael J. Wysocki97badf82015-04-03 23:23:37 +02002335
2336static inline bool fwnode_is_primary(struct fwnode_handle *fwnode)
2337{
2338 return fwnode && !IS_ERR(fwnode->secondary);
2339}
2340
2341/**
2342 * set_primary_fwnode - Change the primary firmware node of a given device.
2343 * @dev: Device to handle.
2344 * @fwnode: New primary firmware node of the device.
2345 *
2346 * Set the device's firmware node pointer to @fwnode, but if a secondary
2347 * firmware node of the device is present, preserve it.
2348 */
2349void set_primary_fwnode(struct device *dev, struct fwnode_handle *fwnode)
2350{
Heikki Krogerusaf753232020-08-21 13:53:42 +03002351 struct fwnode_handle *fn = dev->fwnode;
Rafael J. Wysocki97badf82015-04-03 23:23:37 +02002352
Heikki Krogerusaf753232020-08-21 13:53:42 +03002353 if (fwnode) {
Rafael J. Wysocki97badf82015-04-03 23:23:37 +02002354 if (fwnode_is_primary(fn))
2355 fn = fn->secondary;
2356
Mika Westerberg55f89a82015-11-30 17:11:39 +02002357 if (fn) {
2358 WARN_ON(fwnode->secondary);
2359 fwnode->secondary = fn;
2360 }
Rafael J. Wysocki97badf82015-04-03 23:23:37 +02002361 dev->fwnode = fwnode;
2362 } else {
Heikki Krogerusaf753232020-08-21 13:53:42 +03002363 if (fwnode_is_primary(fn)) {
2364 dev->fwnode = fn->secondary;
2365 fn->secondary = NULL;
2366 } else {
2367 dev->fwnode = NULL;
2368 }
Rafael J. Wysocki97badf82015-04-03 23:23:37 +02002369 }
2370}
2371EXPORT_SYMBOL_GPL(set_primary_fwnode);
2372
2373/**
2374 * set_secondary_fwnode - Change the secondary firmware node of a given device.
2375 * @dev: Device to handle.
2376 * @fwnode: New secondary firmware node of the device.
2377 *
2378 * If a primary firmware node of the device is present, set its secondary
2379 * pointer to @fwnode. Otherwise, set the device's firmware node pointer to
2380 * @fwnode.
2381 */
2382void set_secondary_fwnode(struct device *dev, struct fwnode_handle *fwnode)
2383{
2384 if (fwnode)
2385 fwnode->secondary = ERR_PTR(-ENODEV);
2386
2387 if (fwnode_is_primary(dev->fwnode))
2388 dev->fwnode->secondary = fwnode;
2389 else
2390 dev->fwnode = fwnode;
2391}