blob: ee783abc1c9650bf6cd0f17329fdc33edd91706f [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
Olav Haugand8354e62016-08-18 16:49:44 -070076void lock_device_hotplug_assert(void)
77{
78 lockdep_assert_held(&device_hotplug_lock);
79}
80
Greg Kroah-Hartman4e886c22008-01-27 12:12:43 -080081#ifdef CONFIG_BLOCK
82static inline int device_is_not_partition(struct device *dev)
83{
84 return !(dev->type == &part_type);
85}
86#else
87static inline int device_is_not_partition(struct device *dev)
88{
89 return 1;
90}
91#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
Alan Stern3e956372006-06-16 17:10:48 -040093/**
94 * dev_driver_string - Return a device's driver name, if at all possible
95 * @dev: struct device to get the name of
96 *
97 * Will return the device's driver's name if it is bound to a device. If
yan9169c012012-04-20 21:08:45 +080098 * the device is not bound to a driver, it will return the name of the bus
Alan Stern3e956372006-06-16 17:10:48 -040099 * it is attached to. If it is not attached to a bus either, an empty
100 * string will be returned.
101 */
Jean Delvarebf9ca692008-07-30 12:29:21 -0700102const char *dev_driver_string(const struct device *dev)
Alan Stern3e956372006-06-16 17:10:48 -0400103{
Alan Stern35899722009-12-04 11:06:57 -0500104 struct device_driver *drv;
105
106 /* dev->driver can change to NULL underneath us because of unbinding,
107 * so be careful about accessing it. dev->bus and dev->class should
108 * never change once they are set, so they don't need special care.
109 */
110 drv = ACCESS_ONCE(dev->driver);
111 return drv ? drv->name :
Jean Delvarea456b702007-03-09 16:33:10 +0100112 (dev->bus ? dev->bus->name :
113 (dev->class ? dev->class->name : ""));
Alan Stern3e956372006-06-16 17:10:48 -0400114}
Matthew Wilcox310a9222006-09-23 23:35:04 -0600115EXPORT_SYMBOL(dev_driver_string);
Alan Stern3e956372006-06-16 17:10:48 -0400116
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117#define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr)
118
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800119static ssize_t dev_attr_show(struct kobject *kobj, struct attribute *attr,
120 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800122 struct device_attribute *dev_attr = to_dev_attr(attr);
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200123 struct device *dev = kobj_to_dev(kobj);
Dmitry Torokhov4a0c20b2005-04-29 01:23:47 -0500124 ssize_t ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
126 if (dev_attr->show)
Yani Ioannou54b6f352005-05-17 06:39:34 -0400127 ret = dev_attr->show(dev, dev_attr, buf);
Andrew Morton815d2d52008-03-04 15:09:07 -0800128 if (ret >= (ssize_t)PAGE_SIZE) {
Greg Kroah-Hartman53a9c872013-01-17 13:10:23 -0800129 print_symbol("dev_attr_show: %s returned bad count\n",
130 (unsigned long)dev_attr->show);
Andrew Morton815d2d52008-03-04 15:09:07 -0800131 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 return ret;
133}
134
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800135static ssize_t dev_attr_store(struct kobject *kobj, struct attribute *attr,
136 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800138 struct device_attribute *dev_attr = to_dev_attr(attr);
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200139 struct device *dev = kobj_to_dev(kobj);
Dmitry Torokhov4a0c20b2005-04-29 01:23:47 -0500140 ssize_t ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
142 if (dev_attr->store)
Yani Ioannou54b6f352005-05-17 06:39:34 -0400143 ret = dev_attr->store(dev, dev_attr, buf, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 return ret;
145}
146
Emese Revfy52cf25d2010-01-19 02:58:23 +0100147static const struct sysfs_ops dev_sysfs_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 .show = dev_attr_show,
149 .store = dev_attr_store,
150};
151
Kay Sieversca22e562011-12-14 14:29:38 -0800152#define to_ext_attr(x) container_of(x, struct dev_ext_attribute, attr)
153
154ssize_t device_store_ulong(struct device *dev,
155 struct device_attribute *attr,
156 const char *buf, size_t size)
157{
158 struct dev_ext_attribute *ea = to_ext_attr(attr);
159 char *end;
160 unsigned long new = simple_strtoul(buf, &end, 0);
161 if (end == buf)
162 return -EINVAL;
163 *(unsigned long *)(ea->var) = new;
164 /* Always return full write size even if we didn't consume all */
165 return size;
166}
167EXPORT_SYMBOL_GPL(device_store_ulong);
168
169ssize_t device_show_ulong(struct device *dev,
170 struct device_attribute *attr,
171 char *buf)
172{
173 struct dev_ext_attribute *ea = to_ext_attr(attr);
174 return snprintf(buf, PAGE_SIZE, "%lx\n", *(unsigned long *)(ea->var));
175}
176EXPORT_SYMBOL_GPL(device_show_ulong);
177
178ssize_t device_store_int(struct device *dev,
179 struct device_attribute *attr,
180 const char *buf, size_t size)
181{
182 struct dev_ext_attribute *ea = to_ext_attr(attr);
183 char *end;
184 long new = simple_strtol(buf, &end, 0);
185 if (end == buf || new > INT_MAX || new < INT_MIN)
186 return -EINVAL;
187 *(int *)(ea->var) = new;
188 /* Always return full write size even if we didn't consume all */
189 return size;
190}
191EXPORT_SYMBOL_GPL(device_store_int);
192
193ssize_t device_show_int(struct device *dev,
194 struct device_attribute *attr,
195 char *buf)
196{
197 struct dev_ext_attribute *ea = to_ext_attr(attr);
198
199 return snprintf(buf, PAGE_SIZE, "%d\n", *(int *)(ea->var));
200}
201EXPORT_SYMBOL_GPL(device_show_int);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
Borislav Petkov91872392012-10-09 19:52:05 +0200203ssize_t device_store_bool(struct device *dev, struct device_attribute *attr,
204 const char *buf, size_t size)
205{
206 struct dev_ext_attribute *ea = to_ext_attr(attr);
207
208 if (strtobool(buf, ea->var) < 0)
209 return -EINVAL;
210
211 return size;
212}
213EXPORT_SYMBOL_GPL(device_store_bool);
214
215ssize_t device_show_bool(struct device *dev, struct device_attribute *attr,
216 char *buf)
217{
218 struct dev_ext_attribute *ea = to_ext_attr(attr);
219
220 return snprintf(buf, PAGE_SIZE, "%d\n", *(bool *)(ea->var));
221}
222EXPORT_SYMBOL_GPL(device_show_bool);
223
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224/**
Robert P. J. Dayf8878dc2013-06-01 20:17:34 -0400225 * device_release - free device structure.
226 * @kobj: device's kobject.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 *
Robert P. J. Dayf8878dc2013-06-01 20:17:34 -0400228 * This is called once the reference count for the object
229 * reaches 0. We forward the call to the device's release
230 * method, which should handle actually freeing the structure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800232static void device_release(struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200234 struct device *dev = kobj_to_dev(kobj);
Greg Kroah-Hartmanfb069a52008-12-16 12:23:36 -0800235 struct device_private *p = dev->p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
Ming Leia525a3d2012-07-25 01:42:29 +0800237 /*
238 * Some platform devices are driven without driver attached
239 * and managed resources may have been acquired. Make sure
240 * all resources are released.
241 *
242 * Drivers still can add resources into device after device
243 * is deleted but alive, so release devres here to avoid
244 * possible memory leak.
245 */
246 devres_release_all(dev);
247
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 if (dev->release)
249 dev->release(dev);
Kay Sieversf9f852d2006-10-07 21:54:55 +0200250 else if (dev->type && dev->type->release)
251 dev->type->release(dev);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700252 else if (dev->class && dev->class->dev_release)
253 dev->class->dev_release(dev);
Arjan van de Venf810a5c2008-07-25 19:45:39 -0700254 else
255 WARN(1, KERN_ERR "Device '%s' does not have a release() "
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800256 "function, it is broken and must be fixed.\n",
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100257 dev_name(dev));
Greg Kroah-Hartmanfb069a52008-12-16 12:23:36 -0800258 kfree(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259}
260
Eric W. Biedermanbc451f22010-03-30 11:31:25 -0700261static const void *device_namespace(struct kobject *kobj)
262{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200263 struct device *dev = kobj_to_dev(kobj);
Eric W. Biedermanbc451f22010-03-30 11:31:25 -0700264 const void *ns = NULL;
265
266 if (dev->class && dev->class->ns_type)
267 ns = dev->class->namespace(dev);
268
269 return ns;
270}
271
Greg Kroah-Hartman8f4afc42007-10-11 10:47:49 -0600272static struct kobj_type device_ktype = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 .release = device_release,
274 .sysfs_ops = &dev_sysfs_ops,
Eric W. Biedermanbc451f22010-03-30 11:31:25 -0700275 .namespace = device_namespace,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276};
277
278
Kay Sievers312c0042005-11-16 09:00:00 +0100279static int dev_uevent_filter(struct kset *kset, struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280{
281 struct kobj_type *ktype = get_ktype(kobj);
282
Greg Kroah-Hartman8f4afc42007-10-11 10:47:49 -0600283 if (ktype == &device_ktype) {
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200284 struct device *dev = kobj_to_dev(kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 if (dev->bus)
286 return 1;
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700287 if (dev->class)
288 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 }
290 return 0;
291}
292
Kay Sievers312c0042005-11-16 09:00:00 +0100293static const char *dev_uevent_name(struct kset *kset, struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200295 struct device *dev = kobj_to_dev(kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700297 if (dev->bus)
298 return dev->bus->name;
299 if (dev->class)
300 return dev->class->name;
301 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302}
303
Kay Sievers7eff2e72007-08-14 15:15:12 +0200304static int dev_uevent(struct kset *kset, struct kobject *kobj,
305 struct kobj_uevent_env *env)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200307 struct device *dev = kobj_to_dev(kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 int retval = 0;
309
Kay Sievers6fcf53a2009-04-30 15:23:42 +0200310 /* add device node properties if present */
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700311 if (MAJOR(dev->devt)) {
Kay Sievers6fcf53a2009-04-30 15:23:42 +0200312 const char *tmp;
313 const char *name;
Al Viro2c9ede52011-07-23 20:24:48 -0400314 umode_t mode = 0;
Greg Kroah-Hartman4e4098a2013-04-11 11:43:29 -0700315 kuid_t uid = GLOBAL_ROOT_UID;
316 kgid_t gid = GLOBAL_ROOT_GID;
Kay Sievers6fcf53a2009-04-30 15:23:42 +0200317
Kay Sievers7eff2e72007-08-14 15:15:12 +0200318 add_uevent_var(env, "MAJOR=%u", MAJOR(dev->devt));
319 add_uevent_var(env, "MINOR=%u", MINOR(dev->devt));
Kay Sievers3c2670e2013-04-06 09:56:00 -0700320 name = device_get_devnode(dev, &mode, &uid, &gid, &tmp);
Kay Sievers6fcf53a2009-04-30 15:23:42 +0200321 if (name) {
322 add_uevent_var(env, "DEVNAME=%s", name);
Kay Sieverse454cea2009-09-18 23:01:12 +0200323 if (mode)
324 add_uevent_var(env, "DEVMODE=%#o", mode & 0777);
Greg Kroah-Hartman4e4098a2013-04-11 11:43:29 -0700325 if (!uid_eq(uid, GLOBAL_ROOT_UID))
326 add_uevent_var(env, "DEVUID=%u", from_kuid(&init_user_ns, uid));
327 if (!gid_eq(gid, GLOBAL_ROOT_GID))
328 add_uevent_var(env, "DEVGID=%u", from_kgid(&init_user_ns, gid));
Kay Sievers3c2670e2013-04-06 09:56:00 -0700329 kfree(tmp);
Kay Sievers6fcf53a2009-04-30 15:23:42 +0200330 }
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700331 }
332
Kay Sievers414264f2007-03-12 21:08:57 +0100333 if (dev->type && dev->type->name)
Kay Sievers7eff2e72007-08-14 15:15:12 +0200334 add_uevent_var(env, "DEVTYPE=%s", dev->type->name);
Kay Sievers414264f2007-03-12 21:08:57 +0100335
Kay Sievers239378f2006-10-07 21:54:55 +0200336 if (dev->driver)
Kay Sievers7eff2e72007-08-14 15:15:12 +0200337 add_uevent_var(env, "DRIVER=%s", dev->driver->name);
Kay Sievers239378f2006-10-07 21:54:55 +0200338
Grant Likely07d57a32012-02-01 11:22:22 -0700339 /* Add common DT information about the device */
340 of_device_uevent(dev, env);
341
Kay Sievers7eff2e72007-08-14 15:15:12 +0200342 /* have the bus specific function add its stuff */
Kay Sievers312c0042005-11-16 09:00:00 +0100343 if (dev->bus && dev->bus->uevent) {
Kay Sievers7eff2e72007-08-14 15:15:12 +0200344 retval = dev->bus->uevent(dev, env);
Kay Sieversf9f852d2006-10-07 21:54:55 +0200345 if (retval)
Greg Kroah-Hartman7dc72b22007-11-28 23:49:41 -0800346 pr_debug("device: '%s': %s: bus uevent() returned %d\n",
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100347 dev_name(dev), __func__, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 }
349
Kay Sievers7eff2e72007-08-14 15:15:12 +0200350 /* have the class specific function add its stuff */
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700351 if (dev->class && dev->class->dev_uevent) {
Kay Sievers7eff2e72007-08-14 15:15:12 +0200352 retval = dev->class->dev_uevent(dev, env);
Kay Sieversf9f852d2006-10-07 21:54:55 +0200353 if (retval)
Greg Kroah-Hartman7dc72b22007-11-28 23:49:41 -0800354 pr_debug("device: '%s': %s: class uevent() "
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100355 "returned %d\n", dev_name(dev),
Harvey Harrison2b3a3022008-03-04 16:41:05 -0800356 __func__, retval);
Kay Sieversf9f852d2006-10-07 21:54:55 +0200357 }
358
Stefan Weileef35c22010-08-06 21:11:15 +0200359 /* have the device type specific function add its stuff */
Kay Sieversf9f852d2006-10-07 21:54:55 +0200360 if (dev->type && dev->type->uevent) {
Kay Sievers7eff2e72007-08-14 15:15:12 +0200361 retval = dev->type->uevent(dev, env);
Kay Sieversf9f852d2006-10-07 21:54:55 +0200362 if (retval)
Greg Kroah-Hartman7dc72b22007-11-28 23:49:41 -0800363 pr_debug("device: '%s': %s: dev_type uevent() "
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100364 "returned %d\n", dev_name(dev),
Harvey Harrison2b3a3022008-03-04 16:41:05 -0800365 __func__, retval);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700366 }
367
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 return retval;
369}
370
Emese Revfy9cd43612009-12-31 14:52:51 +0100371static const struct kset_uevent_ops device_uevent_ops = {
Kay Sievers312c0042005-11-16 09:00:00 +0100372 .filter = dev_uevent_filter,
373 .name = dev_uevent_name,
374 .uevent = dev_uevent,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375};
376
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -0700377static ssize_t uevent_show(struct device *dev, struct device_attribute *attr,
Kay Sievers16574dc2007-04-06 01:40:38 +0200378 char *buf)
379{
380 struct kobject *top_kobj;
381 struct kset *kset;
Kay Sievers7eff2e72007-08-14 15:15:12 +0200382 struct kobj_uevent_env *env = NULL;
Kay Sievers16574dc2007-04-06 01:40:38 +0200383 int i;
384 size_t count = 0;
385 int retval;
386
387 /* search the kset, the device belongs to */
388 top_kobj = &dev->kobj;
Kay Sievers5c5daf62007-08-12 20:43:55 +0200389 while (!top_kobj->kset && top_kobj->parent)
390 top_kobj = top_kobj->parent;
Kay Sievers16574dc2007-04-06 01:40:38 +0200391 if (!top_kobj->kset)
392 goto out;
Kay Sievers5c5daf62007-08-12 20:43:55 +0200393
Kay Sievers16574dc2007-04-06 01:40:38 +0200394 kset = top_kobj->kset;
395 if (!kset->uevent_ops || !kset->uevent_ops->uevent)
396 goto out;
397
398 /* respect filter */
399 if (kset->uevent_ops && kset->uevent_ops->filter)
400 if (!kset->uevent_ops->filter(kset, &dev->kobj))
401 goto out;
402
Kay Sievers7eff2e72007-08-14 15:15:12 +0200403 env = kzalloc(sizeof(struct kobj_uevent_env), GFP_KERNEL);
404 if (!env)
Greg Kroah-Hartmanc7308c82007-05-02 14:14:11 +0200405 return -ENOMEM;
406
Kay Sievers16574dc2007-04-06 01:40:38 +0200407 /* let the kset specific function add its keys */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200408 retval = kset->uevent_ops->uevent(kset, &dev->kobj, env);
Kay Sievers16574dc2007-04-06 01:40:38 +0200409 if (retval)
410 goto out;
411
412 /* copy keys to file */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200413 for (i = 0; i < env->envp_idx; i++)
414 count += sprintf(&buf[count], "%s\n", env->envp[i]);
Kay Sievers16574dc2007-04-06 01:40:38 +0200415out:
Kay Sievers7eff2e72007-08-14 15:15:12 +0200416 kfree(env);
Kay Sievers16574dc2007-04-06 01:40:38 +0200417 return count;
418}
419
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -0700420static ssize_t uevent_store(struct device *dev, struct device_attribute *attr,
Kay Sieversa7fd6702005-10-01 14:49:43 +0200421 const char *buf, size_t count)
422{
Kay Sievers60a96a52007-07-08 22:29:26 +0200423 enum kobject_action action;
424
Kay Sievers3f5468c2010-01-14 22:54:37 +0100425 if (kobject_action_type(buf, count, &action) == 0)
Kay Sievers60a96a52007-07-08 22:29:26 +0200426 kobject_uevent(&dev->kobj, action);
Kay Sievers3f5468c2010-01-14 22:54:37 +0100427 else
428 dev_err(dev, "uevent: unknown action-string\n");
Kay Sieversa7fd6702005-10-01 14:49:43 +0200429 return count;
430}
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -0700431static DEVICE_ATTR_RW(uevent);
Kay Sieversa7fd6702005-10-01 14:49:43 +0200432
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -0700433static ssize_t online_show(struct device *dev, struct device_attribute *attr,
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +0200434 char *buf)
435{
436 bool val;
437
Rafael J. Wysocki5e33bc42013-08-28 21:41:01 +0200438 device_lock(dev);
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +0200439 val = !dev->offline;
Rafael J. Wysocki5e33bc42013-08-28 21:41:01 +0200440 device_unlock(dev);
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +0200441 return sprintf(buf, "%u\n", val);
442}
443
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -0700444static ssize_t online_store(struct device *dev, struct device_attribute *attr,
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +0200445 const char *buf, size_t count)
446{
447 bool val;
448 int ret;
449
450 ret = strtobool(buf, &val);
451 if (ret < 0)
452 return ret;
453
Rafael J. Wysocki5e33bc42013-08-28 21:41:01 +0200454 ret = lock_device_hotplug_sysfs();
455 if (ret)
456 return ret;
457
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +0200458 ret = val ? device_online(dev) : device_offline(dev);
459 unlock_device_hotplug();
460 return ret < 0 ? ret : count;
461}
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -0700462static DEVICE_ATTR_RW(online);
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +0200463
Greg Kroah-Hartmanfa6fdb32013-08-08 15:22:55 -0700464int device_add_groups(struct device *dev, const struct attribute_group **groups)
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500465{
Greg Kroah-Hartman3e9b2ba2013-08-21 13:47:50 -0700466 return sysfs_create_groups(&dev->kobj, groups);
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500467}
468
Greg Kroah-Hartmanfa6fdb32013-08-08 15:22:55 -0700469void device_remove_groups(struct device *dev,
470 const struct attribute_group **groups)
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500471{
Greg Kroah-Hartman3e9b2ba2013-08-21 13:47:50 -0700472 sysfs_remove_groups(&dev->kobj, groups);
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -0700473}
474
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700475static int device_add_attrs(struct device *dev)
476{
477 struct class *class = dev->class;
Stephen Hemmingeraed65af2011-03-28 09:12:52 -0700478 const struct device_type *type = dev->type;
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500479 int error;
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700480
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500481 if (class) {
Greg Kroah-Hartmand05a6f92013-07-14 16:05:58 -0700482 error = device_add_groups(dev, class->dev_groups);
Kay Sieversf9f852d2006-10-07 21:54:55 +0200483 if (error)
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500484 return error;
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700485 }
Kay Sieversf9f852d2006-10-07 21:54:55 +0200486
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500487 if (type) {
488 error = device_add_groups(dev, type->groups);
Kay Sieversf9f852d2006-10-07 21:54:55 +0200489 if (error)
Greg Kroah-Hartmana6b01de2013-10-05 18:19:30 -0700490 goto err_remove_class_groups;
Kay Sieversf9f852d2006-10-07 21:54:55 +0200491 }
492
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500493 error = device_add_groups(dev, dev->groups);
494 if (error)
495 goto err_remove_type_groups;
496
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +0200497 if (device_supports_offline(dev) && !dev->offline_disabled) {
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -0700498 error = device_create_file(dev, &dev_attr_online);
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +0200499 if (error)
Rafael J. Wysockiecfbf6f2013-12-12 06:11:02 +0100500 goto err_remove_dev_groups;
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +0200501 }
502
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500503 return 0;
504
Rafael J. Wysockiecfbf6f2013-12-12 06:11:02 +0100505 err_remove_dev_groups:
506 device_remove_groups(dev, dev->groups);
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500507 err_remove_type_groups:
508 if (type)
509 device_remove_groups(dev, type->groups);
Greg Kroah-Hartmand05a6f92013-07-14 16:05:58 -0700510 err_remove_class_groups:
511 if (class)
512 device_remove_groups(dev, class->dev_groups);
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500513
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700514 return error;
515}
516
517static void device_remove_attrs(struct device *dev)
518{
519 struct class *class = dev->class;
Stephen Hemmingeraed65af2011-03-28 09:12:52 -0700520 const struct device_type *type = dev->type;
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700521
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -0700522 device_remove_file(dev, &dev_attr_online);
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500523 device_remove_groups(dev, dev->groups);
Kay Sieversf9f852d2006-10-07 21:54:55 +0200524
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500525 if (type)
526 device_remove_groups(dev, type->groups);
527
Greg Kroah-Hartmana6b01de2013-10-05 18:19:30 -0700528 if (class)
Greg Kroah-Hartmand05a6f92013-07-14 16:05:58 -0700529 device_remove_groups(dev, class->dev_groups);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700530}
531
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -0700532static ssize_t dev_show(struct device *dev, struct device_attribute *attr,
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700533 char *buf)
534{
535 return print_dev_t(buf, dev->devt);
536}
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -0700537static DEVICE_ATTR_RO(dev);
Tejun Heoad6a1e12007-06-14 03:45:17 +0900538
Kay Sieversca22e562011-12-14 14:29:38 -0800539/* /sys/devices/ */
Greg Kroah-Hartman881c6cfd2007-11-01 09:29:06 -0600540struct kset *devices_kset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542/**
Grygorii Strashko52cdbdd2015-07-27 20:43:01 +0300543 * devices_kset_move_before - Move device in the devices_kset's list.
544 * @deva: Device to move.
545 * @devb: Device @deva should come before.
546 */
547static void devices_kset_move_before(struct device *deva, struct device *devb)
548{
549 if (!devices_kset)
550 return;
551 pr_debug("devices_kset: Moving %s before %s\n",
552 dev_name(deva), dev_name(devb));
553 spin_lock(&devices_kset->list_lock);
554 list_move_tail(&deva->kobj.entry, &devb->kobj.entry);
555 spin_unlock(&devices_kset->list_lock);
556}
557
558/**
559 * devices_kset_move_after - Move device in the devices_kset's list.
560 * @deva: Device to move
561 * @devb: Device @deva should come after.
562 */
563static void devices_kset_move_after(struct device *deva, struct device *devb)
564{
565 if (!devices_kset)
566 return;
567 pr_debug("devices_kset: Moving %s after %s\n",
568 dev_name(deva), dev_name(devb));
569 spin_lock(&devices_kset->list_lock);
570 list_move(&deva->kobj.entry, &devb->kobj.entry);
571 spin_unlock(&devices_kset->list_lock);
572}
573
574/**
575 * devices_kset_move_last - move the device to the end of devices_kset's list.
576 * @dev: device to move
577 */
578void devices_kset_move_last(struct device *dev)
579{
580 if (!devices_kset)
581 return;
582 pr_debug("devices_kset: Moving %s to end of list\n", dev_name(dev));
583 spin_lock(&devices_kset->list_lock);
584 list_move_tail(&dev->kobj.entry, &devices_kset->list);
585 spin_unlock(&devices_kset->list_lock);
586}
587
588/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800589 * device_create_file - create sysfs attribute file for device.
590 * @dev: device.
591 * @attr: device attribute descriptor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 */
Phil Carmody26579ab2009-12-18 15:34:19 +0200593int device_create_file(struct device *dev,
594 const struct device_attribute *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595{
596 int error = 0;
Felipe Balbi8f46baa2013-02-20 10:31:42 +0200597
598 if (dev) {
599 WARN(((attr->attr.mode & S_IWUGO) && !attr->store),
dyoung@redhat.com97521972013-05-16 14:31:30 +0800600 "Attribute %s: write permission without 'store'\n",
601 attr->attr.name);
Felipe Balbi8f46baa2013-02-20 10:31:42 +0200602 WARN(((attr->attr.mode & S_IRUGO) && !attr->show),
dyoung@redhat.com97521972013-05-16 14:31:30 +0800603 "Attribute %s: read permission without 'show'\n",
604 attr->attr.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 error = sysfs_create_file(&dev->kobj, &attr->attr);
Felipe Balbi8f46baa2013-02-20 10:31:42 +0200606 }
607
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 return error;
609}
David Graham White86df2682013-07-21 20:41:14 -0400610EXPORT_SYMBOL_GPL(device_create_file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611
612/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800613 * device_remove_file - remove sysfs attribute file.
614 * @dev: device.
615 * @attr: device attribute descriptor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 */
Phil Carmody26579ab2009-12-18 15:34:19 +0200617void device_remove_file(struct device *dev,
618 const struct device_attribute *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619{
Cornelia Huck0c98b192008-01-31 10:39:38 +0100620 if (dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 sysfs_remove_file(&dev->kobj, &attr->attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622}
David Graham White86df2682013-07-21 20:41:14 -0400623EXPORT_SYMBOL_GPL(device_remove_file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624
Greg Kroah-Hartman2589f1882006-09-19 09:39:19 -0700625/**
Tejun Heo6b0afc22014-02-03 14:03:01 -0500626 * device_remove_file_self - remove sysfs attribute file from its own method.
627 * @dev: device.
628 * @attr: device attribute descriptor.
629 *
630 * See kernfs_remove_self() for details.
631 */
632bool device_remove_file_self(struct device *dev,
633 const struct device_attribute *attr)
634{
635 if (dev)
636 return sysfs_remove_file_self(&dev->kobj, &attr->attr);
637 else
638 return false;
639}
640EXPORT_SYMBOL_GPL(device_remove_file_self);
641
642/**
Greg Kroah-Hartman2589f1882006-09-19 09:39:19 -0700643 * device_create_bin_file - create sysfs binary attribute file for device.
644 * @dev: device.
645 * @attr: device binary attribute descriptor.
646 */
Phil Carmody66ecb922009-12-18 15:34:20 +0200647int device_create_bin_file(struct device *dev,
648 const struct bin_attribute *attr)
Greg Kroah-Hartman2589f1882006-09-19 09:39:19 -0700649{
650 int error = -EINVAL;
651 if (dev)
652 error = sysfs_create_bin_file(&dev->kobj, attr);
653 return error;
654}
655EXPORT_SYMBOL_GPL(device_create_bin_file);
656
657/**
658 * device_remove_bin_file - remove sysfs binary attribute file
659 * @dev: device.
660 * @attr: device binary attribute descriptor.
661 */
Phil Carmody66ecb922009-12-18 15:34:20 +0200662void device_remove_bin_file(struct device *dev,
663 const struct bin_attribute *attr)
Greg Kroah-Hartman2589f1882006-09-19 09:39:19 -0700664{
665 if (dev)
666 sysfs_remove_bin_file(&dev->kobj, attr);
667}
668EXPORT_SYMBOL_GPL(device_remove_bin_file);
669
James Bottomley34bb61f2005-09-06 16:56:51 -0700670static void klist_children_get(struct klist_node *n)
671{
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -0800672 struct device_private *p = to_device_private_parent(n);
673 struct device *dev = p->device;
James Bottomley34bb61f2005-09-06 16:56:51 -0700674
675 get_device(dev);
676}
677
678static void klist_children_put(struct klist_node *n)
679{
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -0800680 struct device_private *p = to_device_private_parent(n);
681 struct device *dev = p->device;
James Bottomley34bb61f2005-09-06 16:56:51 -0700682
683 put_device(dev);
684}
685
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800687 * device_initialize - init device structure.
688 * @dev: device.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 *
Cornelia Huck57394112008-09-03 18:26:40 +0200690 * This prepares the device for use by other layers by initializing
691 * its fields.
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800692 * It is the first half of device_register(), if called by
Cornelia Huck57394112008-09-03 18:26:40 +0200693 * that function, though it can also be called separately, so one
694 * may use @dev's fields. In particular, get_device()/put_device()
695 * may be used for reference counting of @dev after calling this
696 * function.
697 *
Alan Sternb10d5ef2012-01-17 11:39:00 -0500698 * All fields in @dev must be initialized by the caller to 0, except
699 * for those explicitly set to some other value. The simplest
700 * approach is to use kzalloc() to allocate the structure containing
701 * @dev.
702 *
Cornelia Huck57394112008-09-03 18:26:40 +0200703 * NOTE: Use put_device() to give up your reference instead of freeing
704 * @dev directly once you have called this function.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706void device_initialize(struct device *dev)
707{
Greg Kroah-Hartman881c6cfd2007-11-01 09:29:06 -0600708 dev->kobj.kset = devices_kset;
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -0700709 kobject_init(&dev->kobj, &device_ktype);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 INIT_LIST_HEAD(&dev->dma_pools);
Thomas Gleixner31427882010-01-29 20:39:02 +0000711 mutex_init(&dev->mutex);
Peter Zijlstra1704f472010-03-19 01:37:42 +0100712 lockdep_set_novalidate_class(&dev->mutex);
Tejun Heo9ac78492007-01-20 16:00:26 +0900713 spin_lock_init(&dev->devres_lock);
714 INIT_LIST_HEAD(&dev->devres_head);
Alan Stern3b98aea2008-08-07 13:06:12 -0400715 device_pm_init(dev);
Christoph Hellwig87348132006-12-06 20:32:33 -0800716 set_dev_node(dev, -1);
Jiang Liu4a7cc832015-07-09 16:00:44 +0800717#ifdef CONFIG_GENERIC_MSI_IRQ
718 INIT_LIST_HEAD(&dev->msi_list);
719#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720}
David Graham White86df2682013-07-21 20:41:14 -0400721EXPORT_SYMBOL_GPL(device_initialize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
Tejun Heod73ce002013-03-12 11:30:05 -0700723struct kobject *virtual_device_parent(struct device *dev)
Greg Kroah-Hartmanf0ee61a2006-10-23 10:40:54 -0700724{
Kay Sievers86406242007-03-14 03:25:56 +0100725 static struct kobject *virtual_dir = NULL;
Greg Kroah-Hartmanf0ee61a2006-10-23 10:40:54 -0700726
Kay Sievers86406242007-03-14 03:25:56 +0100727 if (!virtual_dir)
Greg Kroah-Hartman4ff6abf2007-11-05 22:24:43 -0800728 virtual_dir = kobject_create_and_add("virtual",
Greg Kroah-Hartman881c6cfd2007-11-01 09:29:06 -0600729 &devices_kset->kobj);
Greg Kroah-Hartmanf0ee61a2006-10-23 10:40:54 -0700730
Kay Sievers86406242007-03-14 03:25:56 +0100731 return virtual_dir;
Greg Kroah-Hartmanf0ee61a2006-10-23 10:40:54 -0700732}
733
Eric W. Biedermanbc451f22010-03-30 11:31:25 -0700734struct class_dir {
735 struct kobject kobj;
736 struct class *class;
737};
738
739#define to_class_dir(obj) container_of(obj, struct class_dir, kobj)
740
741static void class_dir_release(struct kobject *kobj)
742{
743 struct class_dir *dir = to_class_dir(kobj);
744 kfree(dir);
745}
746
747static const
748struct kobj_ns_type_operations *class_dir_child_ns_type(struct kobject *kobj)
749{
750 struct class_dir *dir = to_class_dir(kobj);
751 return dir->class->ns_type;
752}
753
754static struct kobj_type class_dir_ktype = {
755 .release = class_dir_release,
756 .sysfs_ops = &kobj_sysfs_ops,
757 .child_ns_type = class_dir_child_ns_type
758};
759
760static struct kobject *
761class_dir_create_and_add(struct class *class, struct kobject *parent_kobj)
762{
763 struct class_dir *dir;
764 int retval;
765
766 dir = kzalloc(sizeof(*dir), GFP_KERNEL);
767 if (!dir)
Tetsuo Handa4f65ebc2018-05-07 19:10:31 +0900768 return ERR_PTR(-ENOMEM);
Eric W. Biedermanbc451f22010-03-30 11:31:25 -0700769
770 dir->class = class;
771 kobject_init(&dir->kobj, &class_dir_ktype);
772
Kay Sievers6b6e39a2010-11-15 23:13:18 +0100773 dir->kobj.kset = &class->p->glue_dirs;
Eric W. Biedermanbc451f22010-03-30 11:31:25 -0700774
775 retval = kobject_add(&dir->kobj, parent_kobj, "%s", class->name);
776 if (retval < 0) {
777 kobject_put(&dir->kobj);
Tetsuo Handa4f65ebc2018-05-07 19:10:31 +0900778 return ERR_PTR(retval);
Eric W. Biedermanbc451f22010-03-30 11:31:25 -0700779 }
780 return &dir->kobj;
781}
782
Yijing Wange4a60d12014-11-07 12:05:49 +0800783static DEFINE_MUTEX(gdp_mutex);
Eric W. Biedermanbc451f22010-03-30 11:31:25 -0700784
Kay Sieversda231fd2007-11-21 17:29:15 +0100785static struct kobject *get_device_parent(struct device *dev,
786 struct device *parent)
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +0100787{
Kay Sievers86406242007-03-14 03:25:56 +0100788 if (dev->class) {
789 struct kobject *kobj = NULL;
790 struct kobject *parent_kobj;
791 struct kobject *k;
792
Randy Dunlapead454f2010-09-24 14:36:49 -0700793#ifdef CONFIG_BLOCK
Kay Sievers39aba962010-09-04 22:33:14 -0700794 /* block disks show up in /sys/block */
Andi Kleene52eec12010-09-08 16:54:17 +0200795 if (sysfs_deprecated && dev->class == &block_class) {
Kay Sievers39aba962010-09-04 22:33:14 -0700796 if (parent && parent->class == &block_class)
797 return &parent->kobj;
Kay Sievers6b6e39a2010-11-15 23:13:18 +0100798 return &block_class.p->subsys.kobj;
Kay Sievers39aba962010-09-04 22:33:14 -0700799 }
Randy Dunlapead454f2010-09-24 14:36:49 -0700800#endif
Andi Kleene52eec12010-09-08 16:54:17 +0200801
Kay Sievers86406242007-03-14 03:25:56 +0100802 /*
803 * If we have no parent, we live in "virtual".
Kay Sievers0f4dafc2007-12-19 01:40:42 +0100804 * Class-devices with a non class-device as parent, live
805 * in a "glue" directory to prevent namespace collisions.
Kay Sievers86406242007-03-14 03:25:56 +0100806 */
807 if (parent == NULL)
808 parent_kobj = virtual_device_parent(dev);
Eric W. Biederman24b14422010-07-24 22:43:35 -0700809 else if (parent->class && !dev->class->ns_type)
Kay Sievers86406242007-03-14 03:25:56 +0100810 return &parent->kobj;
811 else
812 parent_kobj = &parent->kobj;
813
Tejun Heo77d3d7c2010-02-05 17:57:02 +0900814 mutex_lock(&gdp_mutex);
815
Kay Sievers86406242007-03-14 03:25:56 +0100816 /* find our class-directory at the parent and reference it */
Kay Sievers6b6e39a2010-11-15 23:13:18 +0100817 spin_lock(&dev->class->p->glue_dirs.list_lock);
818 list_for_each_entry(k, &dev->class->p->glue_dirs.list, entry)
Kay Sievers86406242007-03-14 03:25:56 +0100819 if (k->parent == parent_kobj) {
820 kobj = kobject_get(k);
821 break;
822 }
Kay Sievers6b6e39a2010-11-15 23:13:18 +0100823 spin_unlock(&dev->class->p->glue_dirs.list_lock);
Tejun Heo77d3d7c2010-02-05 17:57:02 +0900824 if (kobj) {
825 mutex_unlock(&gdp_mutex);
Kay Sievers86406242007-03-14 03:25:56 +0100826 return kobj;
Tejun Heo77d3d7c2010-02-05 17:57:02 +0900827 }
Kay Sievers86406242007-03-14 03:25:56 +0100828
829 /* or create a new class-directory at the parent device */
Eric W. Biedermanbc451f22010-03-30 11:31:25 -0700830 k = class_dir_create_and_add(dev->class, parent_kobj);
Kay Sievers0f4dafc2007-12-19 01:40:42 +0100831 /* do not emit an uevent for this simple "glue" directory */
Tejun Heo77d3d7c2010-02-05 17:57:02 +0900832 mutex_unlock(&gdp_mutex);
Greg Kroah-Hartman43968d22007-11-05 22:24:43 -0800833 return k;
Kay Sievers86406242007-03-14 03:25:56 +0100834 }
835
Kay Sieversca22e562011-12-14 14:29:38 -0800836 /* subsystems can specify a default root directory for their devices */
837 if (!parent && dev->bus && dev->bus->dev_root)
838 return &dev->bus->dev_root->kobj;
839
Kay Sievers86406242007-03-14 03:25:56 +0100840 if (parent)
Cornelia Huckc744aeae2007-01-08 20:16:44 +0100841 return &parent->kobj;
842 return NULL;
843}
Kay Sieversda231fd2007-11-21 17:29:15 +0100844
Ming Leicebf8fd2016-07-10 19:27:36 +0800845static inline bool live_in_glue_dir(struct kobject *kobj,
846 struct device *dev)
847{
848 if (!kobj || !dev->class ||
849 kobj->kset != &dev->class->p->glue_dirs)
850 return false;
851 return true;
852}
853
854static inline struct kobject *get_glue_dir(struct device *dev)
855{
856 return dev->kobj.parent;
857}
858
859/*
860 * make sure cleaning up dir as the last step, we need to make
861 * sure .release handler of kobject is run with holding the
862 * global lock
863 */
Cornelia Huck63b69712008-01-21 16:09:44 +0100864static void cleanup_glue_dir(struct device *dev, struct kobject *glue_dir)
Kay Sieversda231fd2007-11-21 17:29:15 +0100865{
Kay Sievers0f4dafc2007-12-19 01:40:42 +0100866 /* see if we live in a "glue" directory */
Ming Leicebf8fd2016-07-10 19:27:36 +0800867 if (!live_in_glue_dir(glue_dir, dev))
Kay Sieversda231fd2007-11-21 17:29:15 +0100868 return;
869
Yijing Wange4a60d12014-11-07 12:05:49 +0800870 mutex_lock(&gdp_mutex);
Muchun Songac4bcdb2019-05-14 15:04:08 +0530871 if (!kobject_has_children(glue_dir) && atomic_read(&glue_dir->kref.refcount) == 1)
Benjamin Herrenschmidt50091942018-07-10 10:29:10 +1000872 kobject_del(glue_dir);
Kay Sievers0f4dafc2007-12-19 01:40:42 +0100873 kobject_put(glue_dir);
Yijing Wange4a60d12014-11-07 12:05:49 +0800874 mutex_unlock(&gdp_mutex);
Kay Sieversda231fd2007-11-21 17:29:15 +0100875}
Cornelia Huck63b69712008-01-21 16:09:44 +0100876
Cornelia Huck2ee97ca2007-07-18 01:43:47 -0700877static int device_add_class_symlinks(struct device *dev)
878{
Benjamin Herrenschmidt5590f312015-02-18 11:25:18 +1100879 struct device_node *of_node = dev_of_node(dev);
Cornelia Huck2ee97ca2007-07-18 01:43:47 -0700880 int error;
881
Patrick Daly4ab34002018-05-18 15:29:54 -0700882 if (of_node && of_node_kobj(of_node)) {
Rob Herring2d0c4812017-10-04 14:04:01 -0500883 error = sysfs_create_link(&dev->kobj, of_node_kobj(of_node), "of_node");
Benjamin Herrenschmidt5590f312015-02-18 11:25:18 +1100884 if (error)
885 dev_warn(dev, "Error %d creating of_node link\n",error);
886 /* An error here doesn't warrant bringing down the device */
887 }
888
Cornelia Huck2ee97ca2007-07-18 01:43:47 -0700889 if (!dev->class)
890 return 0;
Kay Sieversda231fd2007-11-21 17:29:15 +0100891
Greg Kroah-Hartman1fbfee62008-05-28 09:28:39 -0700892 error = sysfs_create_link(&dev->kobj,
Kay Sievers6b6e39a2010-11-15 23:13:18 +0100893 &dev->class->p->subsys.kobj,
Cornelia Huck2ee97ca2007-07-18 01:43:47 -0700894 "subsystem");
895 if (error)
Benjamin Herrenschmidt5590f312015-02-18 11:25:18 +1100896 goto out_devnode;
Kay Sieversda231fd2007-11-21 17:29:15 +0100897
Greg Kroah-Hartman4e886c22008-01-27 12:12:43 -0800898 if (dev->parent && device_is_not_partition(dev)) {
Dmitry Torokhov4f01a752007-09-18 22:46:50 -0700899 error = sysfs_create_link(&dev->kobj, &dev->parent->kobj,
900 "device");
901 if (error)
Kay Sievers39aba962010-09-04 22:33:14 -0700902 goto out_subsys;
Cornelia Huck2ee97ca2007-07-18 01:43:47 -0700903 }
Kay Sievers39aba962010-09-04 22:33:14 -0700904
Randy Dunlapead454f2010-09-24 14:36:49 -0700905#ifdef CONFIG_BLOCK
Kay Sievers39aba962010-09-04 22:33:14 -0700906 /* /sys/block has directories and does not need symlinks */
Andi Kleene52eec12010-09-08 16:54:17 +0200907 if (sysfs_deprecated && dev->class == &block_class)
Kay Sievers39aba962010-09-04 22:33:14 -0700908 return 0;
Randy Dunlapead454f2010-09-24 14:36:49 -0700909#endif
Kay Sievers39aba962010-09-04 22:33:14 -0700910
911 /* link in the class directory pointing to the device */
Kay Sievers6b6e39a2010-11-15 23:13:18 +0100912 error = sysfs_create_link(&dev->class->p->subsys.kobj,
Kay Sievers39aba962010-09-04 22:33:14 -0700913 &dev->kobj, dev_name(dev));
914 if (error)
915 goto out_device;
916
Cornelia Huck2ee97ca2007-07-18 01:43:47 -0700917 return 0;
918
Kay Sievers39aba962010-09-04 22:33:14 -0700919out_device:
920 sysfs_remove_link(&dev->kobj, "device");
Kay Sieversda231fd2007-11-21 17:29:15 +0100921
Cornelia Huck2ee97ca2007-07-18 01:43:47 -0700922out_subsys:
923 sysfs_remove_link(&dev->kobj, "subsystem");
Benjamin Herrenschmidt5590f312015-02-18 11:25:18 +1100924out_devnode:
925 sysfs_remove_link(&dev->kobj, "of_node");
Cornelia Huck2ee97ca2007-07-18 01:43:47 -0700926 return error;
927}
928
929static void device_remove_class_symlinks(struct device *dev)
930{
Benjamin Herrenschmidt5590f312015-02-18 11:25:18 +1100931 if (dev_of_node(dev))
932 sysfs_remove_link(&dev->kobj, "of_node");
933
Cornelia Huck2ee97ca2007-07-18 01:43:47 -0700934 if (!dev->class)
935 return;
Kay Sieversda231fd2007-11-21 17:29:15 +0100936
Greg Kroah-Hartman4e886c22008-01-27 12:12:43 -0800937 if (dev->parent && device_is_not_partition(dev))
Kay Sieversda231fd2007-11-21 17:29:15 +0100938 sysfs_remove_link(&dev->kobj, "device");
Cornelia Huck2ee97ca2007-07-18 01:43:47 -0700939 sysfs_remove_link(&dev->kobj, "subsystem");
Randy Dunlapead454f2010-09-24 14:36:49 -0700940#ifdef CONFIG_BLOCK
Andi Kleene52eec12010-09-08 16:54:17 +0200941 if (sysfs_deprecated && dev->class == &block_class)
Kay Sievers39aba962010-09-04 22:33:14 -0700942 return;
Randy Dunlapead454f2010-09-24 14:36:49 -0700943#endif
Kay Sievers6b6e39a2010-11-15 23:13:18 +0100944 sysfs_delete_link(&dev->class->p->subsys.kobj, &dev->kobj, dev_name(dev));
Cornelia Huck2ee97ca2007-07-18 01:43:47 -0700945}
946
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947/**
Stephen Rothwell413c2392008-05-30 10:16:40 +1000948 * dev_set_name - set a device name
949 * @dev: device
Randy Dunlap46232362008-06-04 21:40:43 -0700950 * @fmt: format string for the device's name
Stephen Rothwell413c2392008-05-30 10:16:40 +1000951 */
952int dev_set_name(struct device *dev, const char *fmt, ...)
953{
954 va_list vargs;
Kay Sievers1fa5ae82009-01-25 15:17:37 +0100955 int err;
Stephen Rothwell413c2392008-05-30 10:16:40 +1000956
957 va_start(vargs, fmt);
Kay Sievers1fa5ae82009-01-25 15:17:37 +0100958 err = kobject_set_name_vargs(&dev->kobj, fmt, vargs);
Stephen Rothwell413c2392008-05-30 10:16:40 +1000959 va_end(vargs);
Kay Sievers1fa5ae82009-01-25 15:17:37 +0100960 return err;
Stephen Rothwell413c2392008-05-30 10:16:40 +1000961}
962EXPORT_SYMBOL_GPL(dev_set_name);
963
964/**
Dan Williamse105b8b2008-04-21 10:51:07 -0700965 * device_to_dev_kobj - select a /sys/dev/ directory for the device
966 * @dev: device
967 *
968 * By default we select char/ for new entries. Setting class->dev_obj
969 * to NULL prevents an entry from being created. class->dev_kobj must
970 * be set (or cleared) before any devices are registered to the class
971 * otherwise device_create_sys_dev_entry() and
Peter Korsgaard0d4e293c2012-04-17 12:12:57 +0200972 * device_remove_sys_dev_entry() will disagree about the presence of
973 * the link.
Dan Williamse105b8b2008-04-21 10:51:07 -0700974 */
975static struct kobject *device_to_dev_kobj(struct device *dev)
976{
977 struct kobject *kobj;
978
979 if (dev->class)
980 kobj = dev->class->dev_kobj;
981 else
982 kobj = sysfs_dev_char_kobj;
983
984 return kobj;
985}
986
987static int device_create_sys_dev_entry(struct device *dev)
988{
989 struct kobject *kobj = device_to_dev_kobj(dev);
990 int error = 0;
991 char devt_str[15];
992
993 if (kobj) {
994 format_dev_t(devt_str, dev->devt);
995 error = sysfs_create_link(kobj, &dev->kobj, devt_str);
996 }
997
998 return error;
999}
1000
1001static void device_remove_sys_dev_entry(struct device *dev)
1002{
1003 struct kobject *kobj = device_to_dev_kobj(dev);
1004 char devt_str[15];
1005
1006 if (kobj) {
1007 format_dev_t(devt_str, dev->devt);
1008 sysfs_remove_link(kobj, devt_str);
1009 }
1010}
1011
Greg Kroah-Hartmanb4028432009-05-11 14:16:57 -07001012int device_private_init(struct device *dev)
1013{
1014 dev->p = kzalloc(sizeof(*dev->p), GFP_KERNEL);
1015 if (!dev->p)
1016 return -ENOMEM;
1017 dev->p->device = dev;
1018 klist_init(&dev->p->klist_children, klist_children_get,
1019 klist_children_put);
Greg Kroah-Hartmanef8a3fd2012-03-08 12:17:22 -08001020 INIT_LIST_HEAD(&dev->p->deferred_probe);
Greg Kroah-Hartmanb4028432009-05-11 14:16:57 -07001021 return 0;
1022}
1023
Dan Williamse105b8b2008-04-21 10:51:07 -07001024/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001025 * device_add - add device to device hierarchy.
1026 * @dev: device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001028 * This is part 2 of device_register(), though may be called
1029 * separately _iff_ device_initialize() has been called separately.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 *
Cornelia Huck57394112008-09-03 18:26:40 +02001031 * This adds @dev to the kobject hierarchy via kobject_add(), adds it
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001032 * to the global and sibling lists for the device, then
1033 * adds it to the other relevant subsystems of the driver model.
Cornelia Huck57394112008-09-03 18:26:40 +02001034 *
Alan Sternb10d5ef2012-01-17 11:39:00 -05001035 * Do not call this routine or device_register() more than once for
1036 * any device structure. The driver model core is not designed to work
1037 * with devices that get unregistered and then spring back to life.
1038 * (Among other things, it's very hard to guarantee that all references
1039 * to the previous incarnation of @dev have been dropped.) Allocate
1040 * and register a fresh new struct device instead.
1041 *
Cornelia Huck57394112008-09-03 18:26:40 +02001042 * NOTE: _Never_ directly free @dev after calling this function, even
1043 * if it returned an error! Always use put_device() to give up your
1044 * reference instead.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 */
1046int device_add(struct device *dev)
1047{
1048 struct device *parent = NULL;
Kay Sieversca22e562011-12-14 14:29:38 -08001049 struct kobject *kobj;
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02001050 struct class_interface *class_intf;
Greg Kroah-Hartmanc906a482008-05-30 10:45:12 -07001051 int error = -EINVAL;
Ming Leicebf8fd2016-07-10 19:27:36 +08001052 struct kobject *glue_dir = NULL;
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001053
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 dev = get_device(dev);
Greg Kroah-Hartmanc906a482008-05-30 10:45:12 -07001055 if (!dev)
1056 goto done;
1057
Greg Kroah-Hartmanfb069a52008-12-16 12:23:36 -08001058 if (!dev->p) {
Greg Kroah-Hartmanb4028432009-05-11 14:16:57 -07001059 error = device_private_init(dev);
1060 if (error)
1061 goto done;
Greg Kroah-Hartmanfb069a52008-12-16 12:23:36 -08001062 }
Greg Kroah-Hartmanfb069a52008-12-16 12:23:36 -08001063
Kay Sievers1fa5ae82009-01-25 15:17:37 +01001064 /*
1065 * for statically allocated devices, which should all be converted
1066 * some day, we need to initialize the name. We prevent reading back
1067 * the name, and force the use of dev_name()
1068 */
1069 if (dev->init_name) {
Greg Kroah-Hartmanacc0e902009-06-02 15:39:55 -07001070 dev_set_name(dev, "%s", dev->init_name);
Kay Sievers1fa5ae82009-01-25 15:17:37 +01001071 dev->init_name = NULL;
1072 }
Greg Kroah-Hartmanc906a482008-05-30 10:45:12 -07001073
Kay Sieversca22e562011-12-14 14:29:38 -08001074 /* subsystems can specify simple device enumeration */
1075 if (!dev_name(dev) && dev->bus && dev->bus->dev_name)
1076 dev_set_name(dev, "%s%u", dev->bus->dev_name, dev->id);
1077
Thomas Gleixnere6309e72009-12-10 19:32:49 +00001078 if (!dev_name(dev)) {
1079 error = -EINVAL;
Kay Sievers5c8563d2009-05-28 14:24:07 -07001080 goto name_error;
Thomas Gleixnere6309e72009-12-10 19:32:49 +00001081 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01001083 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
Greg Kroah-Hartmanc205ef42006-08-07 22:19:37 -07001084
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 parent = get_device(dev->parent);
Kay Sieversca22e562011-12-14 14:29:38 -08001086 kobj = get_device_parent(dev, parent);
Tetsuo Handa4f65ebc2018-05-07 19:10:31 +09001087 if (IS_ERR(kobj)) {
1088 error = PTR_ERR(kobj);
1089 goto parent_error;
1090 }
Kay Sieversca22e562011-12-14 14:29:38 -08001091 if (kobj)
1092 dev->kobj.parent = kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093
Yinghai Lu0d358f22008-02-19 03:20:41 -08001094 /* use parent numa_node */
Zhen Lei56f2de82015-08-25 12:08:22 +08001095 if (parent && (dev_to_node(dev) == NUMA_NO_NODE))
Yinghai Lu0d358f22008-02-19 03:20:41 -08001096 set_dev_node(dev, dev_to_node(parent));
1097
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 /* first, register with generic layer. */
Kay Sievers8a577ff2009-04-18 15:05:45 -07001099 /* we require the name to be set before, and pass NULL */
1100 error = kobject_add(&dev->kobj, dev->kobj.parent, NULL);
Ming Leicebf8fd2016-07-10 19:27:36 +08001101 if (error) {
1102 glue_dir = get_glue_dir(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 goto Error;
Ming Leicebf8fd2016-07-10 19:27:36 +08001104 }
Kay Sieversa7fd6702005-10-01 14:49:43 +02001105
Brian Walsh37022642006-08-14 22:43:19 -07001106 /* notify platform of device entry */
1107 if (platform_notify)
1108 platform_notify(dev);
1109
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07001110 error = device_create_file(dev, &dev_attr_uevent);
Cornelia Hucka306eea2006-09-22 11:37:13 +02001111 if (error)
1112 goto attrError;
Kay Sieversa7fd6702005-10-01 14:49:43 +02001113
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001114 error = device_add_class_symlinks(dev);
1115 if (error)
1116 goto SymlinkError;
Cornelia Huckdc0afa82007-07-09 11:39:18 -07001117 error = device_add_attrs(dev);
1118 if (error)
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07001119 goto AttrsError;
Cornelia Huckdc0afa82007-07-09 11:39:18 -07001120 error = bus_add_device(dev);
1121 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 goto BusError;
Alan Stern3b98aea2008-08-07 13:06:12 -04001123 error = dpm_sysfs_add(dev);
Rafael J. Wysocki57eee3d2008-03-12 00:59:38 +01001124 if (error)
Alan Stern3b98aea2008-08-07 13:06:12 -04001125 goto DPMError;
Harry Yang1af21b42017-10-05 10:54:00 -07001126 device_pm_add(dev);
Alan Sternec0676ee2008-12-05 14:10:31 -05001127
Sergey Klyaus0cd75042014-10-08 11:31:54 +04001128 if (MAJOR(dev->devt)) {
1129 error = device_create_file(dev, &dev_attr_dev);
1130 if (error)
1131 goto DevAttrError;
1132
1133 error = device_create_sys_dev_entry(dev);
1134 if (error)
1135 goto SysEntryError;
1136
1137 devtmpfs_create_node(dev);
1138 }
1139
Alan Sternec0676ee2008-12-05 14:10:31 -05001140 /* Notify clients of device addition. This call must come
majianpeng268863f2012-01-11 15:12:06 +00001141 * after dpm_sysfs_add() and before kobject_uevent().
Alan Sternec0676ee2008-12-05 14:10:31 -05001142 */
1143 if (dev->bus)
1144 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
1145 BUS_NOTIFY_ADD_DEVICE, dev);
1146
Cornelia Huck83b5fb4c2007-03-29 11:12:11 +02001147 kobject_uevent(&dev->kobj, KOBJ_ADD);
Alan Stern2023c612009-07-30 15:27:18 -04001148 bus_probe_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 if (parent)
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08001150 klist_add_tail(&dev->p->knode_parent,
1151 &parent->p->klist_children);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152
Greg Kroah-Hartman5d9fd162006-06-22 17:17:32 -07001153 if (dev->class) {
Kay Sieversca22e562011-12-14 14:29:38 -08001154 mutex_lock(&dev->class->p->mutex);
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02001155 /* tie the class to the device */
Tejun Heo5a3ceb82008-08-25 19:50:19 +02001156 klist_add_tail(&dev->knode_class,
Kay Sievers6b6e39a2010-11-15 23:13:18 +01001157 &dev->class->p->klist_devices);
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02001158
1159 /* notify any interfaces that the device is here */
Greg Kroah-Hartman184f1f72008-05-28 09:28:39 -07001160 list_for_each_entry(class_intf,
Kay Sieversca22e562011-12-14 14:29:38 -08001161 &dev->class->p->interfaces, node)
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02001162 if (class_intf->add_dev)
1163 class_intf->add_dev(dev, class_intf);
Kay Sieversca22e562011-12-14 14:29:38 -08001164 mutex_unlock(&dev->class->p->mutex);
Greg Kroah-Hartman5d9fd162006-06-22 17:17:32 -07001165 }
Greg Kroah-Hartmanc906a482008-05-30 10:45:12 -07001166done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 put_device(dev);
1168 return error;
Sergey Klyaus0cd75042014-10-08 11:31:54 +04001169 SysEntryError:
1170 if (MAJOR(dev->devt))
1171 device_remove_file(dev, &dev_attr_dev);
1172 DevAttrError:
1173 device_pm_remove(dev);
1174 dpm_sysfs_remove(dev);
Alan Stern3b98aea2008-08-07 13:06:12 -04001175 DPMError:
Rafael J. Wysocki57eee3d2008-03-12 00:59:38 +01001176 bus_remove_device(dev);
1177 BusError:
James Simmons82f0cf92007-02-21 17:44:51 +00001178 device_remove_attrs(dev);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07001179 AttrsError:
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001180 device_remove_class_symlinks(dev);
1181 SymlinkError:
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07001182 device_remove_file(dev, &dev_attr_uevent);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001183 attrError:
Kay Sievers312c0042005-11-16 09:00:00 +01001184 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
Ming Leicebf8fd2016-07-10 19:27:36 +08001185 glue_dir = get_glue_dir(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 kobject_del(&dev->kobj);
1187 Error:
Ming Leicebf8fd2016-07-10 19:27:36 +08001188 cleanup_glue_dir(dev, glue_dir);
Tetsuo Handa4f65ebc2018-05-07 19:10:31 +09001189parent_error:
Markus Elfring5f0163a2015-02-05 11:48:26 +01001190 put_device(parent);
Kay Sievers5c8563d2009-05-28 14:24:07 -07001191name_error:
1192 kfree(dev->p);
1193 dev->p = NULL;
Greg Kroah-Hartmanc906a482008-05-30 10:45:12 -07001194 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195}
David Graham White86df2682013-07-21 20:41:14 -04001196EXPORT_SYMBOL_GPL(device_add);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001199 * device_register - register a device with the system.
1200 * @dev: pointer to the device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001202 * This happens in two clean steps - initialize the device
1203 * and add it to the system. The two steps can be called
1204 * separately, but this is the easiest and most common.
1205 * I.e. you should only call the two helpers separately if
1206 * have a clearly defined need to use and refcount the device
1207 * before it is added to the hierarchy.
Cornelia Huck57394112008-09-03 18:26:40 +02001208 *
Alan Sternb10d5ef2012-01-17 11:39:00 -05001209 * For more information, see the kerneldoc for device_initialize()
1210 * and device_add().
1211 *
Cornelia Huck57394112008-09-03 18:26:40 +02001212 * NOTE: _Never_ directly free @dev after calling this function, even
1213 * if it returned an error! Always use put_device() to give up the
1214 * reference initialized in this function instead.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216int device_register(struct device *dev)
1217{
1218 device_initialize(dev);
1219 return device_add(dev);
1220}
David Graham White86df2682013-07-21 20:41:14 -04001221EXPORT_SYMBOL_GPL(device_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001224 * get_device - increment reference count for device.
1225 * @dev: device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001227 * This simply forwards the call to kobject_get(), though
1228 * we do take care to provide for the case that we get a NULL
1229 * pointer passed in.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001231struct device *get_device(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +02001233 return dev ? kobj_to_dev(kobject_get(&dev->kobj)) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234}
David Graham White86df2682013-07-21 20:41:14 -04001235EXPORT_SYMBOL_GPL(get_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001238 * put_device - decrement reference count.
1239 * @dev: device in question.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001241void put_device(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242{
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001243 /* might_sleep(); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 if (dev)
1245 kobject_put(&dev->kobj);
1246}
David Graham White86df2682013-07-21 20:41:14 -04001247EXPORT_SYMBOL_GPL(put_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001250 * device_del - delete device from system.
1251 * @dev: device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001253 * This is the first part of the device unregistration
1254 * sequence. This removes the device from the lists we control
1255 * from here, has it removed from the other driver model
1256 * subsystems it was added to in device_add(), and removes it
1257 * from the kobject hierarchy.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001259 * NOTE: this should be called manually _iff_ device_add() was
1260 * also called manually.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001262void device_del(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001264 struct device *parent = dev->parent;
Ming Leicebf8fd2016-07-10 19:27:36 +08001265 struct kobject *glue_dir = NULL;
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02001266 struct class_interface *class_intf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267
Alan Sternec0676ee2008-12-05 14:10:31 -05001268 /* Notify clients of device removal. This call must come
1269 * before dpm_sysfs_remove().
1270 */
1271 if (dev->bus)
1272 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
1273 BUS_NOTIFY_DEL_DEVICE, dev);
Alan Stern3b98aea2008-08-07 13:06:12 -04001274 dpm_sysfs_remove(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 if (parent)
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08001276 klist_del(&dev->p->knode_parent);
Dan Williamse105b8b2008-04-21 10:51:07 -07001277 if (MAJOR(dev->devt)) {
Kay Sievers2b2af542009-04-30 15:23:42 +02001278 devtmpfs_delete_node(dev);
Dan Williamse105b8b2008-04-21 10:51:07 -07001279 device_remove_sys_dev_entry(dev);
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07001280 device_remove_file(dev, &dev_attr_dev);
Dan Williamse105b8b2008-04-21 10:51:07 -07001281 }
Kay Sieversb9d9c822006-06-15 15:31:56 +02001282 if (dev->class) {
Kay Sieversda231fd2007-11-21 17:29:15 +01001283 device_remove_class_symlinks(dev);
Kay Sievers99ef3ef2006-09-14 11:23:28 +02001284
Kay Sieversca22e562011-12-14 14:29:38 -08001285 mutex_lock(&dev->class->p->mutex);
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02001286 /* notify any interfaces that the device is now gone */
Greg Kroah-Hartman184f1f72008-05-28 09:28:39 -07001287 list_for_each_entry(class_intf,
Kay Sieversca22e562011-12-14 14:29:38 -08001288 &dev->class->p->interfaces, node)
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02001289 if (class_intf->remove_dev)
1290 class_intf->remove_dev(dev, class_intf);
1291 /* remove the device from the class list */
Tejun Heo5a3ceb82008-08-25 19:50:19 +02001292 klist_del(&dev->knode_class);
Kay Sieversca22e562011-12-14 14:29:38 -08001293 mutex_unlock(&dev->class->p->mutex);
Kay Sieversb9d9c822006-06-15 15:31:56 +02001294 }
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07001295 device_remove_file(dev, &dev_attr_uevent);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07001296 device_remove_attrs(dev);
Benjamin Herrenschmidt28953532006-11-08 19:46:14 -08001297 bus_remove_device(dev);
LongX Zhang4b6d1f122012-10-25 00:21:28 +02001298 device_pm_remove(dev);
Grant Likelyd1c34142012-03-05 08:47:41 -07001299 driver_deferred_probe_del(dev);
Lukas Wunner478573c2016-07-28 02:25:41 +02001300 device_remove_properties(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301
1302 /* Notify the platform of the removal, in case they
1303 * need to do anything...
1304 */
1305 if (platform_notify_remove)
1306 platform_notify_remove(dev);
Joerg Roedel599bad32014-09-30 13:02:02 +02001307 if (dev->bus)
1308 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
1309 BUS_NOTIFY_REMOVED_DEVICE, dev);
Kay Sievers312c0042005-11-16 09:00:00 +01001310 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
Ming Leicebf8fd2016-07-10 19:27:36 +08001311 glue_dir = get_glue_dir(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 kobject_del(&dev->kobj);
Ming Leicebf8fd2016-07-10 19:27:36 +08001313 cleanup_glue_dir(dev, glue_dir);
Kay Sieversda231fd2007-11-21 17:29:15 +01001314 put_device(parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315}
David Graham White86df2682013-07-21 20:41:14 -04001316EXPORT_SYMBOL_GPL(device_del);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317
1318/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001319 * device_unregister - unregister device from system.
1320 * @dev: device going away.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001322 * We do this in two parts, like we do device_register(). First,
1323 * we remove it from all the subsystems with device_del(), then
1324 * we decrement the reference count via put_device(). If that
1325 * is the final reference count, the device will be cleaned up
1326 * via device_release() above. Otherwise, the structure will
1327 * stick around until the final reference to the device is dropped.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001329void device_unregister(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330{
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01001331 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 device_del(dev);
1333 put_device(dev);
1334}
David Graham White86df2682013-07-21 20:41:14 -04001335EXPORT_SYMBOL_GPL(device_unregister);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336
Andy Shevchenko3d060ae2015-07-27 18:04:00 +03001337static struct device *prev_device(struct klist_iter *i)
1338{
1339 struct klist_node *n = klist_prev(i);
1340 struct device *dev = NULL;
1341 struct device_private *p;
1342
1343 if (n) {
1344 p = to_device_private_parent(n);
1345 dev = p->device;
1346 }
1347 return dev;
1348}
1349
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001350static struct device *next_device(struct klist_iter *i)
mochel@digitalimplant.org36239572005-03-24 19:08:30 -08001351{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001352 struct klist_node *n = klist_next(i);
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08001353 struct device *dev = NULL;
1354 struct device_private *p;
1355
1356 if (n) {
1357 p = to_device_private_parent(n);
1358 dev = p->device;
1359 }
1360 return dev;
mochel@digitalimplant.org36239572005-03-24 19:08:30 -08001361}
1362
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363/**
Kay Sieverse454cea2009-09-18 23:01:12 +02001364 * device_get_devnode - path of device node file
Kay Sievers6fcf53a2009-04-30 15:23:42 +02001365 * @dev: device
Kay Sieverse454cea2009-09-18 23:01:12 +02001366 * @mode: returned file access mode
Kay Sievers3c2670e2013-04-06 09:56:00 -07001367 * @uid: returned file owner
1368 * @gid: returned file group
Kay Sievers6fcf53a2009-04-30 15:23:42 +02001369 * @tmp: possibly allocated string
1370 *
1371 * Return the relative path of a possible device node.
1372 * Non-default names may need to allocate a memory to compose
1373 * a name. This memory is returned in tmp and needs to be
1374 * freed by the caller.
1375 */
Kay Sieverse454cea2009-09-18 23:01:12 +02001376const char *device_get_devnode(struct device *dev,
Greg Kroah-Hartman4e4098a2013-04-11 11:43:29 -07001377 umode_t *mode, kuid_t *uid, kgid_t *gid,
Kay Sievers3c2670e2013-04-06 09:56:00 -07001378 const char **tmp)
Kay Sievers6fcf53a2009-04-30 15:23:42 +02001379{
1380 char *s;
1381
1382 *tmp = NULL;
1383
1384 /* the device type may provide a specific name */
Kay Sieverse454cea2009-09-18 23:01:12 +02001385 if (dev->type && dev->type->devnode)
Kay Sievers3c2670e2013-04-06 09:56:00 -07001386 *tmp = dev->type->devnode(dev, mode, uid, gid);
Kay Sievers6fcf53a2009-04-30 15:23:42 +02001387 if (*tmp)
1388 return *tmp;
1389
1390 /* the class may provide a specific name */
Kay Sieverse454cea2009-09-18 23:01:12 +02001391 if (dev->class && dev->class->devnode)
1392 *tmp = dev->class->devnode(dev, mode);
Kay Sievers6fcf53a2009-04-30 15:23:42 +02001393 if (*tmp)
1394 return *tmp;
1395
1396 /* return name without allocation, tmp == NULL */
1397 if (strchr(dev_name(dev), '!') == NULL)
1398 return dev_name(dev);
1399
1400 /* replace '!' in the name with '/' */
Rasmus Villemoesa29fd612015-06-25 15:02:33 -07001401 s = kstrdup(dev_name(dev), GFP_KERNEL);
1402 if (!s)
Kay Sievers6fcf53a2009-04-30 15:23:42 +02001403 return NULL;
Rasmus Villemoesa29fd612015-06-25 15:02:33 -07001404 strreplace(s, '!', '/');
1405 return *tmp = s;
Kay Sievers6fcf53a2009-04-30 15:23:42 +02001406}
1407
1408/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001409 * device_for_each_child - device child iterator.
1410 * @parent: parent struct device.
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001411 * @fn: function to be called for each device.
Robert P. J. Dayf8878dc2013-06-01 20:17:34 -04001412 * @data: data for the callback.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001414 * Iterate over @parent's child devices, and call @fn for each,
1415 * passing it @data.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001417 * We check the return of @fn each time. If it returns anything
1418 * other than 0, we break out and return that value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001420int device_for_each_child(struct device *parent, void *data,
1421 int (*fn)(struct device *dev, void *data))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422{
mochel@digitalimplant.org36239572005-03-24 19:08:30 -08001423 struct klist_iter i;
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001424 struct device *child;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 int error = 0;
1426
Greg Kroah-Hartman014c90db2009-04-15 16:00:12 -07001427 if (!parent->p)
1428 return 0;
1429
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08001430 klist_iter_init(&parent->p->klist_children, &i);
mochel@digitalimplant.org36239572005-03-24 19:08:30 -08001431 while ((child = next_device(&i)) && !error)
1432 error = fn(child, data);
1433 klist_iter_exit(&i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 return error;
1435}
David Graham White86df2682013-07-21 20:41:14 -04001436EXPORT_SYMBOL_GPL(device_for_each_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437
Cornelia Huck5ab69982006-11-16 15:42:07 +01001438/**
Andy Shevchenko3d060ae2015-07-27 18:04:00 +03001439 * device_for_each_child_reverse - device child iterator in reversed order.
1440 * @parent: parent struct device.
1441 * @fn: function to be called for each device.
1442 * @data: data for the callback.
1443 *
1444 * Iterate over @parent's child devices, and call @fn for each,
1445 * passing it @data.
1446 *
1447 * We check the return of @fn each time. If it returns anything
1448 * other than 0, we break out and return that value.
1449 */
1450int device_for_each_child_reverse(struct device *parent, void *data,
1451 int (*fn)(struct device *dev, void *data))
1452{
1453 struct klist_iter i;
1454 struct device *child;
1455 int error = 0;
1456
1457 if (!parent->p)
1458 return 0;
1459
1460 klist_iter_init(&parent->p->klist_children, &i);
1461 while ((child = prev_device(&i)) && !error)
1462 error = fn(child, data);
1463 klist_iter_exit(&i);
1464 return error;
1465}
1466EXPORT_SYMBOL_GPL(device_for_each_child_reverse);
1467
1468/**
Cornelia Huck5ab69982006-11-16 15:42:07 +01001469 * device_find_child - device iterator for locating a particular device.
1470 * @parent: parent struct device
Cornelia Huck5ab69982006-11-16 15:42:07 +01001471 * @match: Callback function to check device
Robert P. J. Dayf8878dc2013-06-01 20:17:34 -04001472 * @data: Data to pass to match function
Cornelia Huck5ab69982006-11-16 15:42:07 +01001473 *
1474 * This is similar to the device_for_each_child() function above, but it
1475 * returns a reference to a device that is 'found' for later use, as
1476 * determined by the @match callback.
1477 *
1478 * The callback should return 0 if the device doesn't match and non-zero
1479 * if it does. If the callback returns non-zero and a reference to the
1480 * current device can be obtained, this function will return to the caller
1481 * and not iterate over any more devices.
Federico Vagaa4e24002013-04-15 11:18:11 +02001482 *
1483 * NOTE: you will need to drop the reference with put_device() after use.
Cornelia Huck5ab69982006-11-16 15:42:07 +01001484 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001485struct device *device_find_child(struct device *parent, void *data,
1486 int (*match)(struct device *dev, void *data))
Cornelia Huck5ab69982006-11-16 15:42:07 +01001487{
1488 struct klist_iter i;
1489 struct device *child;
1490
1491 if (!parent)
1492 return NULL;
1493
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08001494 klist_iter_init(&parent->p->klist_children, &i);
Cornelia Huck5ab69982006-11-16 15:42:07 +01001495 while ((child = next_device(&i)))
1496 if (match(child, data) && get_device(child))
1497 break;
1498 klist_iter_exit(&i);
1499 return child;
1500}
David Graham White86df2682013-07-21 20:41:14 -04001501EXPORT_SYMBOL_GPL(device_find_child);
Cornelia Huck5ab69982006-11-16 15:42:07 +01001502
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503int __init devices_init(void)
1504{
Greg Kroah-Hartman881c6cfd2007-11-01 09:29:06 -06001505 devices_kset = kset_create_and_add("devices", &device_uevent_ops, NULL);
1506 if (!devices_kset)
1507 return -ENOMEM;
Dan Williamse105b8b2008-04-21 10:51:07 -07001508 dev_kobj = kobject_create_and_add("dev", NULL);
1509 if (!dev_kobj)
1510 goto dev_kobj_err;
1511 sysfs_dev_block_kobj = kobject_create_and_add("block", dev_kobj);
1512 if (!sysfs_dev_block_kobj)
1513 goto block_kobj_err;
1514 sysfs_dev_char_kobj = kobject_create_and_add("char", dev_kobj);
1515 if (!sysfs_dev_char_kobj)
1516 goto char_kobj_err;
1517
Greg Kroah-Hartman881c6cfd2007-11-01 09:29:06 -06001518 return 0;
Dan Williamse105b8b2008-04-21 10:51:07 -07001519
1520 char_kobj_err:
1521 kobject_put(sysfs_dev_block_kobj);
1522 block_kobj_err:
1523 kobject_put(dev_kobj);
1524 dev_kobj_err:
1525 kset_unregister(devices_kset);
1526 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527}
1528
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02001529static int device_check_offline(struct device *dev, void *not_used)
1530{
1531 int ret;
1532
1533 ret = device_for_each_child(dev, NULL, device_check_offline);
1534 if (ret)
1535 return ret;
1536
1537 return device_supports_offline(dev) && !dev->offline ? -EBUSY : 0;
1538}
1539
1540/**
1541 * device_offline - Prepare the device for hot-removal.
1542 * @dev: Device to be put offline.
1543 *
1544 * Execute the device bus type's .offline() callback, if present, to prepare
1545 * the device for a subsequent hot-removal. If that succeeds, the device must
1546 * not be used until either it is removed or its bus type's .online() callback
1547 * is executed.
1548 *
1549 * Call under device_hotplug_lock.
1550 */
1551int device_offline(struct device *dev)
1552{
1553 int ret;
1554
1555 if (dev->offline_disabled)
1556 return -EPERM;
1557
1558 ret = device_for_each_child(dev, NULL, device_check_offline);
1559 if (ret)
1560 return ret;
1561
1562 device_lock(dev);
1563 if (device_supports_offline(dev)) {
1564 if (dev->offline) {
1565 ret = 1;
1566 } else {
1567 ret = dev->bus->offline(dev);
1568 if (!ret) {
1569 kobject_uevent(&dev->kobj, KOBJ_OFFLINE);
1570 dev->offline = true;
1571 }
1572 }
1573 }
1574 device_unlock(dev);
1575
1576 return ret;
1577}
1578
1579/**
1580 * device_online - Put the device back online after successful device_offline().
1581 * @dev: Device to be put back online.
1582 *
1583 * If device_offline() has been successfully executed for @dev, but the device
1584 * has not been removed subsequently, execute its bus type's .online() callback
1585 * to indicate that the device can be used again.
1586 *
1587 * Call under device_hotplug_lock.
1588 */
1589int device_online(struct device *dev)
1590{
1591 int ret = 0;
1592
1593 device_lock(dev);
1594 if (device_supports_offline(dev)) {
1595 if (dev->offline) {
1596 ret = dev->bus->online(dev);
1597 if (!ret) {
1598 kobject_uevent(&dev->kobj, KOBJ_ONLINE);
1599 dev->offline = false;
1600 }
1601 } else {
1602 ret = 1;
1603 }
1604 }
1605 device_unlock(dev);
1606
1607 return ret;
1608}
1609
Karthigan Srinivasan7f100d12011-04-18 16:16:52 -05001610struct root_device {
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00001611 struct device dev;
1612 struct module *owner;
1613};
1614
Josh Triplett93058422012-11-18 21:27:55 -08001615static inline struct root_device *to_root_device(struct device *d)
Ferenc Wagner481e2072011-01-07 15:17:47 +01001616{
1617 return container_of(d, struct root_device, dev);
1618}
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00001619
1620static void root_device_release(struct device *dev)
1621{
1622 kfree(to_root_device(dev));
1623}
1624
1625/**
1626 * __root_device_register - allocate and register a root device
1627 * @name: root device name
1628 * @owner: owner module of the root device, usually THIS_MODULE
1629 *
1630 * This function allocates a root device and registers it
1631 * using device_register(). In order to free the returned
1632 * device, use root_device_unregister().
1633 *
1634 * Root devices are dummy devices which allow other devices
1635 * to be grouped under /sys/devices. Use this function to
1636 * allocate a root device and then use it as the parent of
1637 * any device which should appear under /sys/devices/{name}
1638 *
1639 * The /sys/devices/{name} directory will also contain a
1640 * 'module' symlink which points to the @owner directory
1641 * in sysfs.
1642 *
Jani Nikulaf0eae0e2010-03-11 18:11:45 +02001643 * Returns &struct device pointer on success, or ERR_PTR() on error.
1644 *
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00001645 * Note: You probably want to use root_device_register().
1646 */
1647struct device *__root_device_register(const char *name, struct module *owner)
1648{
1649 struct root_device *root;
1650 int err = -ENOMEM;
1651
1652 root = kzalloc(sizeof(struct root_device), GFP_KERNEL);
1653 if (!root)
1654 return ERR_PTR(err);
1655
Greg Kroah-Hartmanacc0e902009-06-02 15:39:55 -07001656 err = dev_set_name(&root->dev, "%s", name);
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00001657 if (err) {
1658 kfree(root);
1659 return ERR_PTR(err);
1660 }
1661
1662 root->dev.release = root_device_release;
1663
1664 err = device_register(&root->dev);
1665 if (err) {
1666 put_device(&root->dev);
1667 return ERR_PTR(err);
1668 }
1669
Christoph Egger1d9e8822010-05-17 16:57:58 +02001670#ifdef CONFIG_MODULES /* gotta find a "cleaner" way to do this */
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00001671 if (owner) {
1672 struct module_kobject *mk = &owner->mkobj;
1673
1674 err = sysfs_create_link(&root->dev.kobj, &mk->kobj, "module");
1675 if (err) {
1676 device_unregister(&root->dev);
1677 return ERR_PTR(err);
1678 }
1679 root->owner = owner;
1680 }
1681#endif
1682
1683 return &root->dev;
1684}
1685EXPORT_SYMBOL_GPL(__root_device_register);
1686
1687/**
1688 * root_device_unregister - unregister and free a root device
Randy Dunlap7cbcf222009-01-20 16:29:13 -08001689 * @dev: device going away
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00001690 *
1691 * This function unregisters and cleans up a device that was created by
1692 * root_device_register().
1693 */
1694void root_device_unregister(struct device *dev)
1695{
1696 struct root_device *root = to_root_device(dev);
1697
1698 if (root->owner)
1699 sysfs_remove_link(&root->dev.kobj, "module");
1700
1701 device_unregister(dev);
1702}
1703EXPORT_SYMBOL_GPL(root_device_unregister);
1704
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001705
1706static void device_create_release(struct device *dev)
1707{
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01001708 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001709 kfree(dev);
1710}
1711
Guenter Roeck39ef3112013-07-14 16:05:57 -07001712static struct device *
1713device_create_groups_vargs(struct class *class, struct device *parent,
1714 dev_t devt, void *drvdata,
1715 const struct attribute_group **groups,
1716 const char *fmt, va_list args)
1717{
1718 struct device *dev = NULL;
1719 int retval = -ENODEV;
1720
1721 if (class == NULL || IS_ERR(class))
1722 goto error;
1723
1724 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1725 if (!dev) {
1726 retval = -ENOMEM;
1727 goto error;
1728 }
1729
David Herrmannbbc780f2013-11-21 20:15:48 +01001730 device_initialize(dev);
Guenter Roeck39ef3112013-07-14 16:05:57 -07001731 dev->devt = devt;
1732 dev->class = class;
1733 dev->parent = parent;
1734 dev->groups = groups;
1735 dev->release = device_create_release;
1736 dev_set_drvdata(dev, drvdata);
1737
1738 retval = kobject_set_name_vargs(&dev->kobj, fmt, args);
1739 if (retval)
1740 goto error;
1741
David Herrmannbbc780f2013-11-21 20:15:48 +01001742 retval = device_add(dev);
Guenter Roeck39ef3112013-07-14 16:05:57 -07001743 if (retval)
1744 goto error;
1745
1746 return dev;
1747
1748error:
1749 put_device(dev);
1750 return ERR_PTR(retval);
1751}
1752
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001753/**
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07001754 * device_create_vargs - creates a device and registers it with sysfs
1755 * @class: pointer to the struct class that this device should be registered to
1756 * @parent: pointer to the parent struct device of this new device, if any
1757 * @devt: the dev_t for the char device to be added
1758 * @drvdata: the data to be added to the device for callbacks
1759 * @fmt: string for the device's name
1760 * @args: va_list for the device's name
1761 *
1762 * This function can be used by char device classes. A struct device
1763 * will be created in sysfs, registered to the specified class.
1764 *
1765 * A "dev" file will be created, showing the dev_t for the device, if
1766 * the dev_t is not 0,0.
1767 * If a pointer to a parent struct device is passed in, the newly created
1768 * struct device will be a child of that device in sysfs.
1769 * The pointer to the struct device will be returned from the call.
1770 * Any further sysfs files that might be required can be created using this
1771 * pointer.
1772 *
Jani Nikulaf0eae0e2010-03-11 18:11:45 +02001773 * Returns &struct device pointer on success, or ERR_PTR() on error.
1774 *
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07001775 * Note: the struct class passed to this function must have previously
1776 * been created with a call to class_create().
1777 */
1778struct device *device_create_vargs(struct class *class, struct device *parent,
1779 dev_t devt, void *drvdata, const char *fmt,
1780 va_list args)
1781{
Guenter Roeck39ef3112013-07-14 16:05:57 -07001782 return device_create_groups_vargs(class, parent, devt, drvdata, NULL,
1783 fmt, args);
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07001784}
1785EXPORT_SYMBOL_GPL(device_create_vargs);
1786
1787/**
Greg Kroah-Hartman4e106732008-07-21 20:03:34 -07001788 * device_create - creates a device and registers it with sysfs
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07001789 * @class: pointer to the struct class that this device should be registered to
1790 * @parent: pointer to the parent struct device of this new device, if any
1791 * @devt: the dev_t for the char device to be added
1792 * @drvdata: the data to be added to the device for callbacks
1793 * @fmt: string for the device's name
1794 *
1795 * This function can be used by char device classes. A struct device
1796 * will be created in sysfs, registered to the specified class.
1797 *
1798 * A "dev" file will be created, showing the dev_t for the device, if
1799 * the dev_t is not 0,0.
1800 * If a pointer to a parent struct device is passed in, the newly created
1801 * struct device will be a child of that device in sysfs.
1802 * The pointer to the struct device will be returned from the call.
1803 * Any further sysfs files that might be required can be created using this
1804 * pointer.
1805 *
Jani Nikulaf0eae0e2010-03-11 18:11:45 +02001806 * Returns &struct device pointer on success, or ERR_PTR() on error.
1807 *
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07001808 * Note: the struct class passed to this function must have previously
1809 * been created with a call to class_create().
1810 */
Greg Kroah-Hartman4e106732008-07-21 20:03:34 -07001811struct device *device_create(struct class *class, struct device *parent,
1812 dev_t devt, void *drvdata, const char *fmt, ...)
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07001813{
1814 va_list vargs;
1815 struct device *dev;
1816
1817 va_start(vargs, fmt);
1818 dev = device_create_vargs(class, parent, devt, drvdata, fmt, vargs);
1819 va_end(vargs);
1820 return dev;
1821}
Greg Kroah-Hartman4e106732008-07-21 20:03:34 -07001822EXPORT_SYMBOL_GPL(device_create);
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07001823
Guenter Roeck39ef3112013-07-14 16:05:57 -07001824/**
1825 * device_create_with_groups - creates a device and registers it with sysfs
1826 * @class: pointer to the struct class that this device should be registered to
1827 * @parent: pointer to the parent struct device of this new device, if any
1828 * @devt: the dev_t for the char device to be added
1829 * @drvdata: the data to be added to the device for callbacks
1830 * @groups: NULL-terminated list of attribute groups to be created
1831 * @fmt: string for the device's name
1832 *
1833 * This function can be used by char device classes. A struct device
1834 * will be created in sysfs, registered to the specified class.
1835 * Additional attributes specified in the groups parameter will also
1836 * be created automatically.
1837 *
1838 * A "dev" file will be created, showing the dev_t for the device, if
1839 * the dev_t is not 0,0.
1840 * If a pointer to a parent struct device is passed in, the newly created
1841 * struct device will be a child of that device in sysfs.
1842 * The pointer to the struct device will be returned from the call.
1843 * Any further sysfs files that might be required can be created using this
1844 * pointer.
1845 *
1846 * Returns &struct device pointer on success, or ERR_PTR() on error.
1847 *
1848 * Note: the struct class passed to this function must have previously
1849 * been created with a call to class_create().
1850 */
1851struct device *device_create_with_groups(struct class *class,
1852 struct device *parent, dev_t devt,
1853 void *drvdata,
1854 const struct attribute_group **groups,
1855 const char *fmt, ...)
1856{
1857 va_list vargs;
1858 struct device *dev;
1859
1860 va_start(vargs, fmt);
1861 dev = device_create_groups_vargs(class, parent, devt, drvdata, groups,
1862 fmt, vargs);
1863 va_end(vargs);
1864 return dev;
1865}
1866EXPORT_SYMBOL_GPL(device_create_with_groups);
1867
Michał Mirosław9f3b7952013-02-01 20:40:17 +01001868static int __match_devt(struct device *dev, const void *data)
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001869{
Michał Mirosław9f3b7952013-02-01 20:40:17 +01001870 const dev_t *devt = data;
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001871
Dave Youngcd354492008-01-28 16:56:11 +08001872 return dev->devt == *devt;
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001873}
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001874
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001875/**
1876 * device_destroy - removes a device that was created with device_create()
1877 * @class: pointer to the struct class that this device was registered with
1878 * @devt: the dev_t of the device that was previously registered
1879 *
1880 * This call unregisters and cleans up a device that was created with a
1881 * call to device_create().
1882 */
1883void device_destroy(struct class *class, dev_t devt)
1884{
1885 struct device *dev;
1886
Greg Kroah-Hartman695794a2008-05-22 17:21:08 -04001887 dev = class_find_device(class, NULL, &devt, __match_devt);
Dave Youngcd354492008-01-28 16:56:11 +08001888 if (dev) {
1889 put_device(dev);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001890 device_unregister(dev);
Dave Youngcd354492008-01-28 16:56:11 +08001891 }
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001892}
1893EXPORT_SYMBOL_GPL(device_destroy);
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001894
1895/**
1896 * device_rename - renames a device
1897 * @dev: the pointer to the struct device to be renamed
1898 * @new_name: the new name of the device
Eric W. Biederman030c1d22008-05-08 14:41:00 -07001899 *
1900 * It is the responsibility of the caller to provide mutual
1901 * exclusion between two different calls of device_rename
1902 * on the same device to ensure that new_name is valid and
1903 * won't conflict with other devices.
Michael Ellermanc6c0ac62010-11-25 09:44:07 +11001904 *
Timur Tabia5462512010-12-13 14:08:52 -06001905 * Note: Don't call this function. Currently, the networking layer calls this
1906 * function, but that will change. The following text from Kay Sievers offers
1907 * some insight:
1908 *
1909 * Renaming devices is racy at many levels, symlinks and other stuff are not
1910 * replaced atomically, and you get a "move" uevent, but it's not easy to
1911 * connect the event to the old and new device. Device nodes are not renamed at
1912 * all, there isn't even support for that in the kernel now.
1913 *
1914 * In the meantime, during renaming, your target name might be taken by another
1915 * driver, creating conflicts. Or the old name is taken directly after you
1916 * renamed it -- then you get events for the same DEVPATH, before you even see
1917 * the "move" event. It's just a mess, and nothing new should ever rely on
1918 * kernel device renaming. Besides that, it's not even implemented now for
1919 * other things than (driver-core wise very simple) network devices.
1920 *
1921 * We are currently about to change network renaming in udev to completely
1922 * disallow renaming of devices in the same namespace as the kernel uses,
1923 * because we can't solve the problems properly, that arise with swapping names
1924 * of multiple interfaces without races. Means, renaming of eth[0-9]* will only
1925 * be allowed to some other name than eth[0-9]*, for the aforementioned
1926 * reasons.
1927 *
1928 * Make up a "real" name in the driver before you register anything, or add
1929 * some other attributes for userspace to find the device, or use udev to add
1930 * symlinks -- but never rename kernel devices later, it's a complete mess. We
1931 * don't even want to get into that and try to implement the missing pieces in
1932 * the core. We really have other pieces to fix in the driver core mess. :)
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001933 */
Johannes Berg6937e8f2010-08-05 17:38:18 +02001934int device_rename(struct device *dev, const char *new_name)
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001935{
Tejun Heo4b30ee52013-09-11 22:29:06 -04001936 struct kobject *kobj = &dev->kobj;
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001937 char *old_device_name = NULL;
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001938 int error;
1939
1940 dev = get_device(dev);
1941 if (!dev)
1942 return -EINVAL;
1943
ethan.zhao69df7532013-10-13 22:12:35 +08001944 dev_dbg(dev, "renaming to %s\n", new_name);
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001945
Kay Sievers1fa5ae82009-01-25 15:17:37 +01001946 old_device_name = kstrdup(dev_name(dev), GFP_KERNEL);
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001947 if (!old_device_name) {
1948 error = -ENOMEM;
1949 goto out;
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001950 }
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001951
Eric W. Biedermanf349cf32010-03-30 11:31:29 -07001952 if (dev->class) {
Tejun Heo4b30ee52013-09-11 22:29:06 -04001953 error = sysfs_rename_link_ns(&dev->class->p->subsys.kobj,
1954 kobj, old_device_name,
1955 new_name, kobject_namespace(kobj));
Eric W. Biedermanf349cf32010-03-30 11:31:29 -07001956 if (error)
1957 goto out;
1958 }
Kay Sievers39aba962010-09-04 22:33:14 -07001959
Tejun Heo4b30ee52013-09-11 22:29:06 -04001960 error = kobject_rename(kobj, new_name);
Kay Sievers1fa5ae82009-01-25 15:17:37 +01001961 if (error)
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001962 goto out;
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001963
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001964out:
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001965 put_device(dev);
1966
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001967 kfree(old_device_name);
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001968
1969 return error;
1970}
Johannes Berga2807db2007-02-28 12:38:31 +01001971EXPORT_SYMBOL_GPL(device_rename);
Cornelia Huck8a824722006-11-20 17:07:51 +01001972
1973static int device_move_class_links(struct device *dev,
1974 struct device *old_parent,
1975 struct device *new_parent)
1976{
Greg Kroah-Hartmanf7f34612007-03-06 12:55:53 -08001977 int error = 0;
Cornelia Huck8a824722006-11-20 17:07:51 +01001978
Greg Kroah-Hartmanf7f34612007-03-06 12:55:53 -08001979 if (old_parent)
1980 sysfs_remove_link(&dev->kobj, "device");
1981 if (new_parent)
1982 error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
1983 "device");
1984 return error;
Cornelia Huck8a824722006-11-20 17:07:51 +01001985}
1986
1987/**
1988 * device_move - moves a device to a new parent
1989 * @dev: the pointer to the struct device to be moved
Cornelia Huckc744aeae2007-01-08 20:16:44 +01001990 * @new_parent: the new parent of the device (can by NULL)
Cornelia Huckffa6a702009-03-04 12:44:00 +01001991 * @dpm_order: how to reorder the dpm_list
Cornelia Huck8a824722006-11-20 17:07:51 +01001992 */
Cornelia Huckffa6a702009-03-04 12:44:00 +01001993int device_move(struct device *dev, struct device *new_parent,
1994 enum dpm_order dpm_order)
Cornelia Huck8a824722006-11-20 17:07:51 +01001995{
1996 int error;
1997 struct device *old_parent;
Cornelia Huckc744aeae2007-01-08 20:16:44 +01001998 struct kobject *new_parent_kobj;
Cornelia Huck8a824722006-11-20 17:07:51 +01001999
2000 dev = get_device(dev);
2001 if (!dev)
2002 return -EINVAL;
2003
Cornelia Huckffa6a702009-03-04 12:44:00 +01002004 device_pm_lock();
Cornelia Huck8a824722006-11-20 17:07:51 +01002005 new_parent = get_device(new_parent);
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002006 new_parent_kobj = get_device_parent(dev, new_parent);
Tetsuo Handa4f65ebc2018-05-07 19:10:31 +09002007 if (IS_ERR(new_parent_kobj)) {
2008 error = PTR_ERR(new_parent_kobj);
2009 put_device(new_parent);
2010 goto out;
2011 }
Cornelia Huck63b69712008-01-21 16:09:44 +01002012
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01002013 pr_debug("device: '%s': %s: moving to '%s'\n", dev_name(dev),
2014 __func__, new_parent ? dev_name(new_parent) : "<NULL>");
Cornelia Huckc744aeae2007-01-08 20:16:44 +01002015 error = kobject_move(&dev->kobj, new_parent_kobj);
Cornelia Huck8a824722006-11-20 17:07:51 +01002016 if (error) {
Cornelia Huck63b69712008-01-21 16:09:44 +01002017 cleanup_glue_dir(dev, new_parent_kobj);
Cornelia Huck8a824722006-11-20 17:07:51 +01002018 put_device(new_parent);
2019 goto out;
2020 }
2021 old_parent = dev->parent;
2022 dev->parent = new_parent;
2023 if (old_parent)
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08002024 klist_remove(&dev->p->knode_parent);
Yinghai Lu0d358f22008-02-19 03:20:41 -08002025 if (new_parent) {
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08002026 klist_add_tail(&dev->p->knode_parent,
2027 &new_parent->p->klist_children);
Yinghai Lu0d358f22008-02-19 03:20:41 -08002028 set_dev_node(dev, dev_to_node(new_parent));
2029 }
2030
Rabin Vincentbdd40342012-04-23 09:16:36 +02002031 if (dev->class) {
2032 error = device_move_class_links(dev, old_parent, new_parent);
2033 if (error) {
2034 /* We ignore errors on cleanup since we're hosed anyway... */
2035 device_move_class_links(dev, new_parent, old_parent);
2036 if (!kobject_move(&dev->kobj, &old_parent->kobj)) {
2037 if (new_parent)
2038 klist_remove(&dev->p->knode_parent);
2039 dev->parent = old_parent;
2040 if (old_parent) {
2041 klist_add_tail(&dev->p->knode_parent,
2042 &old_parent->p->klist_children);
2043 set_dev_node(dev, dev_to_node(old_parent));
2044 }
Yinghai Lu0d358f22008-02-19 03:20:41 -08002045 }
Rabin Vincentbdd40342012-04-23 09:16:36 +02002046 cleanup_glue_dir(dev, new_parent_kobj);
2047 put_device(new_parent);
2048 goto out;
Cornelia Huck8a824722006-11-20 17:07:51 +01002049 }
Cornelia Huck8a824722006-11-20 17:07:51 +01002050 }
Cornelia Huckffa6a702009-03-04 12:44:00 +01002051 switch (dpm_order) {
2052 case DPM_ORDER_NONE:
2053 break;
2054 case DPM_ORDER_DEV_AFTER_PARENT:
2055 device_pm_move_after(dev, new_parent);
Grygorii Strashko52cdbdd2015-07-27 20:43:01 +03002056 devices_kset_move_after(dev, new_parent);
Cornelia Huckffa6a702009-03-04 12:44:00 +01002057 break;
2058 case DPM_ORDER_PARENT_BEFORE_DEV:
2059 device_pm_move_before(new_parent, dev);
Grygorii Strashko52cdbdd2015-07-27 20:43:01 +03002060 devices_kset_move_before(new_parent, dev);
Cornelia Huckffa6a702009-03-04 12:44:00 +01002061 break;
2062 case DPM_ORDER_DEV_LAST:
2063 device_pm_move_last(dev);
Grygorii Strashko52cdbdd2015-07-27 20:43:01 +03002064 devices_kset_move_last(dev);
Cornelia Huckffa6a702009-03-04 12:44:00 +01002065 break;
2066 }
Rabin Vincentbdd40342012-04-23 09:16:36 +02002067
Cornelia Huck8a824722006-11-20 17:07:51 +01002068 put_device(old_parent);
2069out:
Cornelia Huckffa6a702009-03-04 12:44:00 +01002070 device_pm_unlock();
Cornelia Huck8a824722006-11-20 17:07:51 +01002071 put_device(dev);
2072 return error;
2073}
Cornelia Huck8a824722006-11-20 17:07:51 +01002074EXPORT_SYMBOL_GPL(device_move);
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08002075
2076/**
2077 * device_shutdown - call ->shutdown() on each device to shutdown.
2078 */
2079void device_shutdown(void)
2080{
Benson Leungf123db82013-09-24 20:05:11 -07002081 struct device *dev, *parent;
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08002082
Pingfan Liue6f55802018-07-19 13:14:58 +08002083 wait_for_device_probe();
2084 device_block_probing();
2085
Rafael J. Wysocki7ccf3b82019-10-09 01:29:10 +02002086 cpufreq_suspend();
2087
Hugh Daschbach62458382010-03-22 10:36:37 -07002088 spin_lock(&devices_kset->list_lock);
2089 /*
2090 * Walk the devices list backward, shutting down each in turn.
2091 * Beware that device unplug events may also start pulling
2092 * devices offline, even as the system is shutting down.
2093 */
2094 while (!list_empty(&devices_kset->list)) {
2095 dev = list_entry(devices_kset->list.prev, struct device,
2096 kobj.entry);
Ming Leid1c6c032012-06-22 18:01:40 +08002097
2098 /*
2099 * hold reference count of device's parent to
2100 * prevent it from being freed because parent's
2101 * lock is to be held
2102 */
Benson Leungf123db82013-09-24 20:05:11 -07002103 parent = get_device(dev->parent);
Hugh Daschbach62458382010-03-22 10:36:37 -07002104 get_device(dev);
2105 /*
2106 * Make sure the device is off the kset list, in the
2107 * event that dev->*->shutdown() doesn't remove it.
2108 */
2109 list_del_init(&dev->kobj.entry);
2110 spin_unlock(&devices_kset->list_lock);
Alan Sternfe6b91f2011-12-06 23:24:52 +01002111
Ming Leid1c6c032012-06-22 18:01:40 +08002112 /* hold lock to avoid race with probe/release */
Benson Leungf123db82013-09-24 20:05:11 -07002113 if (parent)
2114 device_lock(parent);
Ming Leid1c6c032012-06-22 18:01:40 +08002115 device_lock(dev);
2116
Alan Sternfe6b91f2011-12-06 23:24:52 +01002117 /* Don't allow any more runtime suspends */
2118 pm_runtime_get_noresume(dev);
2119 pm_runtime_barrier(dev);
Hugh Daschbach62458382010-03-22 10:36:37 -07002120
Josh Zimmerman5a1e1c62017-06-25 14:53:23 -07002121 if (dev->class && dev->class->shutdown) {
2122 if (initcall_debug)
2123 dev_info(dev, "shutdown\n");
2124 dev->class->shutdown(dev);
2125 } else if (dev->bus && dev->bus->shutdown) {
ShuoX Liu0246c4f2012-11-23 15:14:12 +08002126 if (initcall_debug)
2127 dev_info(dev, "shutdown\n");
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08002128 dev->bus->shutdown(dev);
2129 } else if (dev->driver && dev->driver->shutdown) {
ShuoX Liu0246c4f2012-11-23 15:14:12 +08002130 if (initcall_debug)
2131 dev_info(dev, "shutdown\n");
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08002132 dev->driver->shutdown(dev);
2133 }
Ming Leid1c6c032012-06-22 18:01:40 +08002134
2135 device_unlock(dev);
Benson Leungf123db82013-09-24 20:05:11 -07002136 if (parent)
2137 device_unlock(parent);
Ming Leid1c6c032012-06-22 18:01:40 +08002138
Hugh Daschbach62458382010-03-22 10:36:37 -07002139 put_device(dev);
Benson Leungf123db82013-09-24 20:05:11 -07002140 put_device(parent);
Hugh Daschbach62458382010-03-22 10:36:37 -07002141
2142 spin_lock(&devices_kset->list_lock);
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08002143 }
Hugh Daschbach62458382010-03-22 10:36:37 -07002144 spin_unlock(&devices_kset->list_lock);
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08002145}
Joe Perches99bcf212010-06-27 01:02:34 +00002146
2147/*
2148 * Device logging functions
2149 */
2150
2151#ifdef CONFIG_PRINTK
Joe Perches666f3552012-09-12 20:14:11 -07002152static int
2153create_syslog_header(const struct device *dev, char *hdr, size_t hdrlen)
Joe Perches99bcf212010-06-27 01:02:34 +00002154{
Kay Sieversc4e00da2012-05-03 02:29:59 +02002155 const char *subsys;
Joe Perches798efc62012-09-12 20:11:29 -07002156 size_t pos = 0;
Joe Perches99bcf212010-06-27 01:02:34 +00002157
Kay Sieversc4e00da2012-05-03 02:29:59 +02002158 if (dev->class)
2159 subsys = dev->class->name;
2160 else if (dev->bus)
2161 subsys = dev->bus->name;
2162 else
Joe Perches798efc62012-09-12 20:11:29 -07002163 return 0;
Kay Sieversc4e00da2012-05-03 02:29:59 +02002164
Joe Perches798efc62012-09-12 20:11:29 -07002165 pos += snprintf(hdr + pos, hdrlen - pos, "SUBSYSTEM=%s", subsys);
Ben Hutchings655e5b72014-08-26 00:34:44 -07002166 if (pos >= hdrlen)
2167 goto overflow;
Kay Sieversc4e00da2012-05-03 02:29:59 +02002168
2169 /*
2170 * Add device identifier DEVICE=:
2171 * b12:8 block dev_t
2172 * c127:3 char dev_t
2173 * n8 netdev ifindex
2174 * +sound:card0 subsystem:devname
2175 */
2176 if (MAJOR(dev->devt)) {
2177 char c;
2178
2179 if (strcmp(subsys, "block") == 0)
2180 c = 'b';
2181 else
2182 c = 'c';
Joe Perches798efc62012-09-12 20:11:29 -07002183 pos++;
2184 pos += snprintf(hdr + pos, hdrlen - pos,
2185 "DEVICE=%c%u:%u",
2186 c, MAJOR(dev->devt), MINOR(dev->devt));
Kay Sieversc4e00da2012-05-03 02:29:59 +02002187 } else if (strcmp(subsys, "net") == 0) {
2188 struct net_device *net = to_net_dev(dev);
2189
Joe Perches798efc62012-09-12 20:11:29 -07002190 pos++;
2191 pos += snprintf(hdr + pos, hdrlen - pos,
2192 "DEVICE=n%u", net->ifindex);
Kay Sieversc4e00da2012-05-03 02:29:59 +02002193 } else {
Joe Perches798efc62012-09-12 20:11:29 -07002194 pos++;
2195 pos += snprintf(hdr + pos, hdrlen - pos,
2196 "DEVICE=+%s:%s", subsys, dev_name(dev));
Kay Sieversc4e00da2012-05-03 02:29:59 +02002197 }
Jim Cromieaf7f2152012-07-19 13:46:21 -06002198
Ben Hutchings655e5b72014-08-26 00:34:44 -07002199 if (pos >= hdrlen)
2200 goto overflow;
2201
Joe Perches798efc62012-09-12 20:11:29 -07002202 return pos;
Ben Hutchings655e5b72014-08-26 00:34:44 -07002203
2204overflow:
2205 dev_WARN(dev, "device/subsystem name too long");
2206 return 0;
Joe Perches99bcf212010-06-27 01:02:34 +00002207}
Joe Perches798efc62012-09-12 20:11:29 -07002208
Joe Perches05e4e5b2012-09-12 20:13:37 -07002209int dev_vprintk_emit(int level, const struct device *dev,
2210 const char *fmt, va_list args)
2211{
2212 char hdr[128];
2213 size_t hdrlen;
2214
2215 hdrlen = create_syslog_header(dev, hdr, sizeof(hdr));
2216
2217 return vprintk_emit(0, level, hdrlen ? hdr : NULL, hdrlen, fmt, args);
2218}
2219EXPORT_SYMBOL(dev_vprintk_emit);
2220
2221int dev_printk_emit(int level, const struct device *dev, const char *fmt, ...)
2222{
2223 va_list args;
2224 int r;
2225
2226 va_start(args, fmt);
2227
2228 r = dev_vprintk_emit(level, dev, fmt, args);
2229
2230 va_end(args);
2231
2232 return r;
2233}
2234EXPORT_SYMBOL(dev_printk_emit);
2235
Joe Perchesd1f10522014-12-25 15:07:04 -08002236static void __dev_printk(const char *level, const struct device *dev,
Joe Perches798efc62012-09-12 20:11:29 -07002237 struct va_format *vaf)
2238{
Joe Perchesd1f10522014-12-25 15:07:04 -08002239 if (dev)
2240 dev_printk_emit(level[1] - '0', dev, "%s %s: %pV",
2241 dev_driver_string(dev), dev_name(dev), vaf);
2242 else
2243 printk("%s(NULL device *): %pV", level, vaf);
Joe Perches798efc62012-09-12 20:11:29 -07002244}
Joe Perches99bcf212010-06-27 01:02:34 +00002245
Joe Perchesd1f10522014-12-25 15:07:04 -08002246void dev_printk(const char *level, const struct device *dev,
2247 const char *fmt, ...)
Joe Perches99bcf212010-06-27 01:02:34 +00002248{
2249 struct va_format vaf;
2250 va_list args;
Joe Perches99bcf212010-06-27 01:02:34 +00002251
2252 va_start(args, fmt);
2253
2254 vaf.fmt = fmt;
2255 vaf.va = &args;
2256
Joe Perchesd1f10522014-12-25 15:07:04 -08002257 __dev_printk(level, dev, &vaf);
Joe Perches798efc62012-09-12 20:11:29 -07002258
Joe Perches99bcf212010-06-27 01:02:34 +00002259 va_end(args);
Joe Perches99bcf212010-06-27 01:02:34 +00002260}
2261EXPORT_SYMBOL(dev_printk);
2262
2263#define define_dev_printk_level(func, kern_level) \
Joe Perchesd1f10522014-12-25 15:07:04 -08002264void func(const struct device *dev, const char *fmt, ...) \
Joe Perches99bcf212010-06-27 01:02:34 +00002265{ \
2266 struct va_format vaf; \
2267 va_list args; \
Joe Perches99bcf212010-06-27 01:02:34 +00002268 \
2269 va_start(args, fmt); \
2270 \
2271 vaf.fmt = fmt; \
2272 vaf.va = &args; \
2273 \
Joe Perchesd1f10522014-12-25 15:07:04 -08002274 __dev_printk(kern_level, dev, &vaf); \
Joe Perches798efc62012-09-12 20:11:29 -07002275 \
Joe Perches99bcf212010-06-27 01:02:34 +00002276 va_end(args); \
Joe Perches99bcf212010-06-27 01:02:34 +00002277} \
2278EXPORT_SYMBOL(func);
2279
2280define_dev_printk_level(dev_emerg, KERN_EMERG);
2281define_dev_printk_level(dev_alert, KERN_ALERT);
2282define_dev_printk_level(dev_crit, KERN_CRIT);
2283define_dev_printk_level(dev_err, KERN_ERR);
2284define_dev_printk_level(dev_warn, KERN_WARNING);
2285define_dev_printk_level(dev_notice, KERN_NOTICE);
2286define_dev_printk_level(_dev_info, KERN_INFO);
2287
2288#endif
Rafael J. Wysocki97badf82015-04-03 23:23:37 +02002289
2290static inline bool fwnode_is_primary(struct fwnode_handle *fwnode)
2291{
2292 return fwnode && !IS_ERR(fwnode->secondary);
2293}
2294
2295/**
2296 * set_primary_fwnode - Change the primary firmware node of a given device.
2297 * @dev: Device to handle.
2298 * @fwnode: New primary firmware node of the device.
2299 *
2300 * Set the device's firmware node pointer to @fwnode, but if a secondary
2301 * firmware node of the device is present, preserve it.
2302 */
2303void set_primary_fwnode(struct device *dev, struct fwnode_handle *fwnode)
2304{
2305 if (fwnode) {
2306 struct fwnode_handle *fn = dev->fwnode;
2307
2308 if (fwnode_is_primary(fn))
2309 fn = fn->secondary;
2310
Mika Westerberg55f89a82015-11-30 17:11:39 +02002311 if (fn) {
2312 WARN_ON(fwnode->secondary);
2313 fwnode->secondary = fn;
2314 }
Rafael J. Wysocki97badf82015-04-03 23:23:37 +02002315 dev->fwnode = fwnode;
2316 } else {
2317 dev->fwnode = fwnode_is_primary(dev->fwnode) ?
2318 dev->fwnode->secondary : NULL;
2319 }
2320}
2321EXPORT_SYMBOL_GPL(set_primary_fwnode);
2322
2323/**
2324 * set_secondary_fwnode - Change the secondary firmware node of a given device.
2325 * @dev: Device to handle.
2326 * @fwnode: New secondary firmware node of the device.
2327 *
2328 * If a primary firmware node of the device is present, set its secondary
2329 * pointer to @fwnode. Otherwise, set the device's firmware node pointer to
2330 * @fwnode.
2331 */
2332void set_secondary_fwnode(struct device *dev, struct fwnode_handle *fwnode)
2333{
2334 if (fwnode)
2335 fwnode->secondary = ERR_PTR(-ENODEV);
2336
2337 if (fwnode_is_primary(dev->fwnode))
2338 dev->fwnode->secondary = fwnode;
2339 else
2340 dev->fwnode = fwnode;
2341}