blob: 2db62d98e39505b3466275d41bbb2c5a2cc9017e [file] [log] [blame]
Greg Kroah-Hartman989d42e2017-11-07 17:30:07 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * drivers/base/core.c - core driver model code (device registration, etc)
4 *
5 * Copyright (c) 2002-3 Patrick Mochel
6 * Copyright (c) 2002-3 Open Source Development Labs
Greg Kroah-Hartman64bb5d22006-06-28 16:19:58 -07007 * Copyright (c) 2006 Greg Kroah-Hartman <gregkh@suse.de>
8 * Copyright (c) 2006 Novell, Inc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 */
10
Heikki Krogerus7847a142018-11-09 17:21:35 +030011#include <linux/acpi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/device.h>
13#include <linux/err.h>
Rafael J. Wysocki97badf82015-04-03 23:23:37 +020014#include <linux/fwnode.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/init.h>
16#include <linux/module.h>
17#include <linux/slab.h>
18#include <linux/string.h>
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -070019#include <linux/kdev_t.h>
Benjamin Herrenschmidt116af372006-10-25 13:44:59 +100020#include <linux/notifier.h>
Grant Likely07d57a32012-02-01 11:22:22 -070021#include <linux/of.h>
22#include <linux/of_device.h>
Kay Sieversda231fd2007-11-21 17:29:15 +010023#include <linux/genhd.h>
Dave Youngf75b1c62008-05-28 09:28:39 -070024#include <linux/mutex.h>
Peter Chenaf8db152011-11-15 21:52:29 +010025#include <linux/pm_runtime.h>
Kay Sieversc4e00da2012-05-03 02:29:59 +020026#include <linux/netdevice.h>
Ingo Molnar174cd4b2017-02-02 19:15:33 +010027#include <linux/sched/signal.h>
Greg Kroah-Hartman63967682013-08-27 10:24:15 -070028#include <linux/sysfs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
30#include "base.h"
31#include "power/power.h"
32
Andi Kleene52eec12010-09-08 16:54:17 +020033#ifdef CONFIG_SYSFS_DEPRECATED
34#ifdef CONFIG_SYSFS_DEPRECATED_V2
35long sysfs_deprecated = 1;
36#else
37long sysfs_deprecated = 0;
38#endif
Hanjun Guo3454bf92013-08-17 20:42:24 +080039static int __init sysfs_deprecated_setup(char *arg)
Andi Kleene52eec12010-09-08 16:54:17 +020040{
Jingoo Han34da5e62013-07-26 13:10:22 +090041 return kstrtol(arg, 10, &sysfs_deprecated);
Andi Kleene52eec12010-09-08 16:54:17 +020042}
43early_param("sysfs.deprecated", sysfs_deprecated_setup);
44#endif
45
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +010046/* Device links support. */
47
48#ifdef CONFIG_SRCU
49static DEFINE_MUTEX(device_links_lock);
50DEFINE_STATIC_SRCU(device_links_srcu);
51
52static inline void device_links_write_lock(void)
53{
54 mutex_lock(&device_links_lock);
55}
56
57static inline void device_links_write_unlock(void)
58{
59 mutex_unlock(&device_links_lock);
60}
61
62int device_links_read_lock(void)
63{
64 return srcu_read_lock(&device_links_srcu);
65}
66
67void device_links_read_unlock(int idx)
68{
69 srcu_read_unlock(&device_links_srcu, idx);
70}
Joel Fernandes (Google)c2fa1e12019-07-16 18:12:25 -040071
72int device_links_read_lock_held(void)
73{
74 return srcu_read_lock_held(&device_links_srcu);
75}
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +010076#else /* !CONFIG_SRCU */
77static DECLARE_RWSEM(device_links_lock);
78
79static inline void device_links_write_lock(void)
80{
81 down_write(&device_links_lock);
82}
83
84static inline void device_links_write_unlock(void)
85{
86 up_write(&device_links_lock);
87}
88
89int device_links_read_lock(void)
90{
91 down_read(&device_links_lock);
92 return 0;
93}
94
95void device_links_read_unlock(int not_used)
96{
97 up_read(&device_links_lock);
98}
Joel Fernandes (Google)c2fa1e12019-07-16 18:12:25 -040099
100#ifdef CONFIG_DEBUG_LOCK_ALLOC
101int device_links_read_lock_held(void)
102{
103 return lockdep_is_held(&device_links_lock);
104}
105#endif
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100106#endif /* !CONFIG_SRCU */
107
108/**
109 * device_is_dependent - Check if one device depends on another one
110 * @dev: Device to check dependencies for.
111 * @target: Device to check against.
112 *
113 * Check if @target depends on @dev or any device dependent on it (its child or
114 * its consumer etc). Return 1 if that is the case or 0 otherwise.
115 */
116static int device_is_dependent(struct device *dev, void *target)
117{
118 struct device_link *link;
119 int ret;
120
Benjamin Gaignarde16f4f32018-07-16 13:37:44 +0200121 if (dev == target)
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100122 return 1;
123
124 ret = device_for_each_child(dev, target, device_is_dependent);
125 if (ret)
126 return ret;
127
128 list_for_each_entry(link, &dev->links.consumers, s_node) {
Benjamin Gaignarde16f4f32018-07-16 13:37:44 +0200129 if (link->consumer == target)
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100130 return 1;
131
132 ret = device_is_dependent(link->consumer, target);
133 if (ret)
134 break;
135 }
136 return ret;
137}
138
Rafael J. Wysocki515db262019-07-16 17:21:06 +0200139static void device_link_init_status(struct device_link *link,
140 struct device *consumer,
141 struct device *supplier)
142{
143 switch (supplier->links.status) {
144 case DL_DEV_PROBING:
145 switch (consumer->links.status) {
146 case DL_DEV_PROBING:
147 /*
148 * A consumer driver can create a link to a supplier
149 * that has not completed its probing yet as long as it
150 * knows that the supplier is already functional (for
151 * example, it has just acquired some resources from the
152 * supplier).
153 */
154 link->status = DL_STATE_CONSUMER_PROBE;
155 break;
156 default:
157 link->status = DL_STATE_DORMANT;
158 break;
159 }
160 break;
161 case DL_DEV_DRIVER_BOUND:
162 switch (consumer->links.status) {
163 case DL_DEV_PROBING:
164 link->status = DL_STATE_CONSUMER_PROBE;
165 break;
166 case DL_DEV_DRIVER_BOUND:
167 link->status = DL_STATE_ACTIVE;
168 break;
169 default:
170 link->status = DL_STATE_AVAILABLE;
171 break;
172 }
173 break;
174 case DL_DEV_UNBINDING:
175 link->status = DL_STATE_SUPPLIER_UNBIND;
176 break;
177 default:
178 link->status = DL_STATE_DORMANT;
179 break;
180 }
181}
182
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100183static int device_reorder_to_tail(struct device *dev, void *not_used)
184{
185 struct device_link *link;
186
187 /*
188 * Devices that have not been registered yet will be put to the ends
189 * of the lists during the registration, so skip them here.
190 */
191 if (device_is_registered(dev))
192 devices_kset_move_last(dev);
193
194 if (device_pm_initialized(dev))
195 device_pm_move_last(dev);
196
197 device_for_each_child(dev, NULL, device_reorder_to_tail);
198 list_for_each_entry(link, &dev->links.consumers, s_node)
199 device_reorder_to_tail(link->consumer, NULL);
200
201 return 0;
202}
203
204/**
Feng Kan494fd7b2018-04-10 16:57:06 -0700205 * device_pm_move_to_tail - Move set of devices to the end of device lists
206 * @dev: Device to move
207 *
208 * This is a device_reorder_to_tail() wrapper taking the requisite locks.
209 *
210 * It moves the @dev along with all of its children and all of its consumers
211 * to the ends of the device_kset and dpm_list, recursively.
212 */
213void device_pm_move_to_tail(struct device *dev)
214{
215 int idx;
216
217 idx = device_links_read_lock();
218 device_pm_lock();
219 device_reorder_to_tail(dev, NULL);
220 device_pm_unlock();
221 device_links_read_unlock(idx);
222}
223
Rafael J. Wysocki515db262019-07-16 17:21:06 +0200224#define DL_MANAGED_LINK_FLAGS (DL_FLAG_AUTOREMOVE_CONSUMER | \
225 DL_FLAG_AUTOREMOVE_SUPPLIER | \
226 DL_FLAG_AUTOPROBE_CONSUMER)
227
Rafael J. Wysockifb583c82019-07-30 11:28:57 +0200228#define DL_ADD_VALID_FLAGS (DL_MANAGED_LINK_FLAGS | DL_FLAG_STATELESS | \
229 DL_FLAG_PM_RUNTIME | DL_FLAG_RPM_ACTIVE)
230
Feng Kan494fd7b2018-04-10 16:57:06 -0700231/**
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100232 * device_link_add - Create a link between two devices.
233 * @consumer: Consumer end of the link.
234 * @supplier: Supplier end of the link.
235 * @flags: Link flags.
236 *
Rafael J. Wysocki21d5c572016-10-30 17:32:31 +0100237 * The caller is responsible for the proper synchronization of the link creation
238 * with runtime PM. First, setting the DL_FLAG_PM_RUNTIME flag will cause the
239 * runtime PM framework to take the link into account. Second, if the
240 * DL_FLAG_RPM_ACTIVE flag is set in addition to it, the supplier devices will
241 * be forced into the active metastate and reference-counted upon the creation
242 * of the link. If DL_FLAG_PM_RUNTIME is not set, DL_FLAG_RPM_ACTIVE will be
243 * ignored.
244 *
Rafael J. Wysocki515db262019-07-16 17:21:06 +0200245 * If DL_FLAG_STATELESS is set in @flags, the caller of this function is
246 * expected to release the link returned by it directly with the help of either
247 * device_link_del() or device_link_remove().
Rafael J. Wysocki72175d42019-02-01 01:58:33 +0100248 *
249 * If that flag is not set, however, the caller of this function is handing the
250 * management of the link over to the driver core entirely and its return value
251 * can only be used to check whether or not the link is present. In that case,
252 * the DL_FLAG_AUTOREMOVE_CONSUMER and DL_FLAG_AUTOREMOVE_SUPPLIER device link
253 * flags can be used to indicate to the driver core when the link can be safely
254 * deleted. Namely, setting one of them in @flags indicates to the driver core
255 * that the link is not going to be used (by the given caller of this function)
256 * after unbinding the consumer or supplier driver, respectively, from its
257 * device, so the link can be deleted at that point. If none of them is set,
258 * the link will be maintained until one of the devices pointed to by it (either
259 * the consumer or the supplier) is unregistered.
Rafael J. Wysockic8d50982019-02-01 01:45:55 +0100260 *
Rafael J. Wysockie7dd4012019-02-01 01:59:42 +0100261 * Also, if DL_FLAG_STATELESS, DL_FLAG_AUTOREMOVE_CONSUMER and
262 * DL_FLAG_AUTOREMOVE_SUPPLIER are not set in @flags (that is, a persistent
263 * managed device link is being added), the DL_FLAG_AUTOPROBE_CONSUMER flag can
264 * be used to request the driver core to automaticall probe for a consmer
265 * driver after successfully binding a driver to the supplier device.
266 *
Rafael J. Wysocki515db262019-07-16 17:21:06 +0200267 * The combination of DL_FLAG_STATELESS and one of DL_FLAG_AUTOREMOVE_CONSUMER,
268 * DL_FLAG_AUTOREMOVE_SUPPLIER, or DL_FLAG_AUTOPROBE_CONSUMER set in @flags at
269 * the same time is invalid and will cause NULL to be returned upfront.
270 * However, if a device link between the given @consumer and @supplier pair
271 * exists already when this function is called for them, the existing link will
272 * be returned regardless of its current type and status (the link's flags may
273 * be modified then). The caller of this function is then expected to treat
274 * the link as though it has just been created, so (in particular) if
275 * DL_FLAG_STATELESS was passed in @flags, the link needs to be released
276 * explicitly when not needed any more (as stated above).
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100277 *
278 * A side effect of the link creation is re-ordering of dpm_list and the
279 * devices_kset list by moving the consumer device and all devices depending
280 * on it to the ends of these lists (that does not happen to devices that have
281 * not been registered when this function is called).
282 *
283 * The supplier device is required to be registered when this function is called
284 * and NULL will be returned if that is not the case. The consumer device need
Lukas Wunner64df1142016-12-04 13:10:04 +0100285 * not be registered, however.
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100286 */
287struct device_link *device_link_add(struct device *consumer,
288 struct device *supplier, u32 flags)
289{
290 struct device_link *link;
291
Rafael J. Wysockifb583c82019-07-30 11:28:57 +0200292 if (!consumer || !supplier || flags & ~DL_ADD_VALID_FLAGS ||
Rafael J. Wysocki515db262019-07-16 17:21:06 +0200293 (flags & DL_FLAG_STATELESS && flags & DL_MANAGED_LINK_FLAGS) ||
Rafael J. Wysockie7dd4012019-02-01 01:59:42 +0100294 (flags & DL_FLAG_AUTOPROBE_CONSUMER &&
295 flags & (DL_FLAG_AUTOREMOVE_CONSUMER |
296 DL_FLAG_AUTOREMOVE_SUPPLIER)))
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100297 return NULL;
298
Rafael J. Wysocki5db25c92019-02-01 01:47:53 +0100299 if (flags & DL_FLAG_PM_RUNTIME && flags & DL_FLAG_RPM_ACTIVE) {
300 if (pm_runtime_get_sync(supplier) < 0) {
301 pm_runtime_put_noidle(supplier);
302 return NULL;
303 }
Rafael J. Wysocki5db25c92019-02-01 01:47:53 +0100304 }
305
Rafael J. Wysocki515db262019-07-16 17:21:06 +0200306 if (!(flags & DL_FLAG_STATELESS))
307 flags |= DL_FLAG_MANAGED;
308
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100309 device_links_write_lock();
310 device_pm_lock();
311
312 /*
313 * If the supplier has not been fully registered yet or there is a
314 * reverse dependency between the consumer and the supplier already in
315 * the graph, return NULL.
316 */
317 if (!device_pm_initialized(supplier)
318 || device_is_dependent(consumer, supplier)) {
319 link = NULL;
320 goto out;
321 }
322
Rafael J. Wysocki72175d42019-02-01 01:58:33 +0100323 /*
324 * DL_FLAG_AUTOREMOVE_SUPPLIER indicates that the link will be needed
325 * longer than for DL_FLAG_AUTOREMOVE_CONSUMER and setting them both
326 * together doesn't make sense, so prefer DL_FLAG_AUTOREMOVE_SUPPLIER.
327 */
328 if (flags & DL_FLAG_AUTOREMOVE_SUPPLIER)
329 flags &= ~DL_FLAG_AUTOREMOVE_CONSUMER;
330
Rafael J. Wysockif265df52019-02-01 01:46:54 +0100331 list_for_each_entry(link, &supplier->links.consumers, s_node) {
332 if (link->consumer != consumer)
333 continue;
334
Rafael J. Wysockie2f3cd82019-02-01 01:49:14 +0100335 if (flags & DL_FLAG_PM_RUNTIME) {
336 if (!(link->flags & DL_FLAG_PM_RUNTIME)) {
Rafael J. Wysocki4c06c4e2019-02-12 13:08:10 +0100337 pm_runtime_new_link(consumer);
Rafael J. Wysockie2f3cd82019-02-01 01:49:14 +0100338 link->flags |= DL_FLAG_PM_RUNTIME;
339 }
340 if (flags & DL_FLAG_RPM_ACTIVE)
Rafael J. Wysocki36003d42019-02-19 17:53:26 +0100341 refcount_inc(&link->rpm_active);
Rafael J. Wysockie2f3cd82019-02-01 01:49:14 +0100342 }
343
Rafael J. Wysocki72175d42019-02-01 01:58:33 +0100344 if (flags & DL_FLAG_STATELESS) {
Rafael J. Wysocki515db262019-07-16 17:21:06 +0200345 link->flags |= DL_FLAG_STATELESS;
Rafael J. Wysocki72175d42019-02-01 01:58:33 +0100346 kref_get(&link->kref);
347 goto out;
348 }
349
350 /*
351 * If the life time of the link following from the new flags is
352 * longer than indicated by the flags of the existing link,
353 * update the existing link to stay around longer.
354 */
355 if (flags & DL_FLAG_AUTOREMOVE_SUPPLIER) {
356 if (link->flags & DL_FLAG_AUTOREMOVE_CONSUMER) {
357 link->flags &= ~DL_FLAG_AUTOREMOVE_CONSUMER;
358 link->flags |= DL_FLAG_AUTOREMOVE_SUPPLIER;
359 }
360 } else if (!(flags & DL_FLAG_AUTOREMOVE_CONSUMER)) {
361 link->flags &= ~(DL_FLAG_AUTOREMOVE_CONSUMER |
362 DL_FLAG_AUTOREMOVE_SUPPLIER);
363 }
Rafael J. Wysocki515db262019-07-16 17:21:06 +0200364 if (!(link->flags & DL_FLAG_MANAGED)) {
365 kref_get(&link->kref);
366 link->flags |= DL_FLAG_MANAGED;
367 device_link_init_status(link, consumer, supplier);
368 }
Rafael J. Wysockif265df52019-02-01 01:46:54 +0100369 goto out;
370 }
371
Rafael J. Wysocki21d5c572016-10-30 17:32:31 +0100372 link = kzalloc(sizeof(*link), GFP_KERNEL);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100373 if (!link)
374 goto out;
375
Rafael J. Wysockie2f3cd82019-02-01 01:49:14 +0100376 refcount_set(&link->rpm_active, 1);
377
Rafael J. Wysockibaa88092016-10-30 17:32:43 +0100378 if (flags & DL_FLAG_PM_RUNTIME) {
Rafael J. Wysockie2f3cd82019-02-01 01:49:14 +0100379 if (flags & DL_FLAG_RPM_ACTIVE)
Rafael J. Wysocki36003d42019-02-19 17:53:26 +0100380 refcount_inc(&link->rpm_active);
Rafael J. Wysockie2f3cd82019-02-01 01:49:14 +0100381
Rafael J. Wysocki4c06c4e2019-02-12 13:08:10 +0100382 pm_runtime_new_link(consumer);
Rafael J. Wysocki21d5c572016-10-30 17:32:31 +0100383 }
Rafael J. Wysockie2f3cd82019-02-01 01:49:14 +0100384
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100385 get_device(supplier);
386 link->supplier = supplier;
387 INIT_LIST_HEAD(&link->s_node);
388 get_device(consumer);
389 link->consumer = consumer;
390 INIT_LIST_HEAD(&link->c_node);
391 link->flags = flags;
Lukas Wunneread18c22018-02-10 19:27:12 +0100392 kref_init(&link->kref);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100393
Lukas Wunner64df1142016-12-04 13:10:04 +0100394 /* Determine the initial link state. */
Rafael J. Wysocki515db262019-07-16 17:21:06 +0200395 if (flags & DL_FLAG_STATELESS)
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100396 link->status = DL_STATE_NONE;
Rafael J. Wysocki515db262019-07-16 17:21:06 +0200397 else
398 device_link_init_status(link, consumer, supplier);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100399
400 /*
Rafael J. Wysocki15cfb092019-02-01 01:50:39 +0100401 * Some callers expect the link creation during consumer driver probe to
402 * resume the supplier even without DL_FLAG_RPM_ACTIVE.
403 */
404 if (link->status == DL_STATE_CONSUMER_PROBE &&
405 flags & DL_FLAG_PM_RUNTIME)
406 pm_runtime_resume(supplier);
407
408 /*
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100409 * Move the consumer and all of the devices depending on it to the end
410 * of dpm_list and the devices_kset list.
411 *
412 * It is necessary to hold dpm_list locked throughout all that or else
413 * we may end up suspending with a wrong ordering of it.
414 */
415 device_reorder_to_tail(consumer, NULL);
416
417 list_add_tail_rcu(&link->s_node, &supplier->links.consumers);
418 list_add_tail_rcu(&link->c_node, &consumer->links.suppliers);
419
Jerome Brunet8a4b3262018-12-21 17:23:41 +0100420 dev_dbg(consumer, "Linked as a consumer to %s\n", dev_name(supplier));
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100421
422 out:
423 device_pm_unlock();
424 device_links_write_unlock();
Rafael J. Wysocki5db25c92019-02-01 01:47:53 +0100425
Rafael J. Wysockie2f3cd82019-02-01 01:49:14 +0100426 if ((flags & DL_FLAG_PM_RUNTIME && flags & DL_FLAG_RPM_ACTIVE) && !link)
Rafael J. Wysocki5db25c92019-02-01 01:47:53 +0100427 pm_runtime_put(supplier);
428
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100429 return link;
430}
431EXPORT_SYMBOL_GPL(device_link_add);
432
433static void device_link_free(struct device_link *link)
434{
Rafael J. Wysockia1fdbfb2019-02-01 01:52:45 +0100435 while (refcount_dec_not_one(&link->rpm_active))
436 pm_runtime_put(link->supplier);
437
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100438 put_device(link->consumer);
439 put_device(link->supplier);
440 kfree(link);
441}
442
443#ifdef CONFIG_SRCU
444static void __device_link_free_srcu(struct rcu_head *rhead)
445{
446 device_link_free(container_of(rhead, struct device_link, rcu_head));
447}
448
Lukas Wunneread18c22018-02-10 19:27:12 +0100449static void __device_link_del(struct kref *kref)
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100450{
Lukas Wunneread18c22018-02-10 19:27:12 +0100451 struct device_link *link = container_of(kref, struct device_link, kref);
452
Jerome Brunet8a4b3262018-12-21 17:23:41 +0100453 dev_dbg(link->consumer, "Dropping the link to %s\n",
454 dev_name(link->supplier));
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100455
Rafael J. Wysockibaa88092016-10-30 17:32:43 +0100456 if (link->flags & DL_FLAG_PM_RUNTIME)
457 pm_runtime_drop_link(link->consumer);
458
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100459 list_del_rcu(&link->s_node);
460 list_del_rcu(&link->c_node);
461 call_srcu(&device_links_srcu, &link->rcu_head, __device_link_free_srcu);
462}
463#else /* !CONFIG_SRCU */
Lukas Wunneread18c22018-02-10 19:27:12 +0100464static void __device_link_del(struct kref *kref)
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100465{
Lukas Wunneread18c22018-02-10 19:27:12 +0100466 struct device_link *link = container_of(kref, struct device_link, kref);
467
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100468 dev_info(link->consumer, "Dropping the link to %s\n",
469 dev_name(link->supplier));
470
Lukas Wunner433986c2018-02-10 19:13:58 +0100471 if (link->flags & DL_FLAG_PM_RUNTIME)
472 pm_runtime_drop_link(link->consumer);
473
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100474 list_del(&link->s_node);
475 list_del(&link->c_node);
476 device_link_free(link);
477}
478#endif /* !CONFIG_SRCU */
479
Rafael J. Wysocki72175d42019-02-01 01:58:33 +0100480static void device_link_put_kref(struct device_link *link)
481{
482 if (link->flags & DL_FLAG_STATELESS)
483 kref_put(&link->kref, __device_link_del);
484 else
485 WARN(1, "Unable to drop a managed device link reference\n");
486}
487
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100488/**
Rafael J. Wysocki72175d42019-02-01 01:58:33 +0100489 * device_link_del - Delete a stateless link between two devices.
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100490 * @link: Device link to delete.
491 *
492 * The caller must ensure proper synchronization of this function with runtime
Lukas Wunneread18c22018-02-10 19:27:12 +0100493 * PM. If the link was added multiple times, it needs to be deleted as often.
494 * Care is required for hotplugged devices: Their links are purged on removal
495 * and calling device_link_del() is then no longer allowed.
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100496 */
497void device_link_del(struct device_link *link)
498{
499 device_links_write_lock();
500 device_pm_lock();
Rafael J. Wysocki72175d42019-02-01 01:58:33 +0100501 device_link_put_kref(link);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100502 device_pm_unlock();
503 device_links_write_unlock();
504}
505EXPORT_SYMBOL_GPL(device_link_del);
506
pascal pailletd8842212018-07-05 14:25:56 +0000507/**
Rafael J. Wysocki72175d42019-02-01 01:58:33 +0100508 * device_link_remove - Delete a stateless link between two devices.
pascal pailletd8842212018-07-05 14:25:56 +0000509 * @consumer: Consumer end of the link.
510 * @supplier: Supplier end of the link.
511 *
512 * The caller must ensure proper synchronization of this function with runtime
513 * PM.
514 */
515void device_link_remove(void *consumer, struct device *supplier)
516{
517 struct device_link *link;
518
519 if (WARN_ON(consumer == supplier))
520 return;
521
522 device_links_write_lock();
523 device_pm_lock();
524
525 list_for_each_entry(link, &supplier->links.consumers, s_node) {
526 if (link->consumer == consumer) {
Rafael J. Wysocki72175d42019-02-01 01:58:33 +0100527 device_link_put_kref(link);
pascal pailletd8842212018-07-05 14:25:56 +0000528 break;
529 }
530 }
531
532 device_pm_unlock();
533 device_links_write_unlock();
534}
535EXPORT_SYMBOL_GPL(device_link_remove);
536
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100537static void device_links_missing_supplier(struct device *dev)
538{
539 struct device_link *link;
540
541 list_for_each_entry(link, &dev->links.suppliers, c_node)
542 if (link->status == DL_STATE_CONSUMER_PROBE)
543 WRITE_ONCE(link->status, DL_STATE_AVAILABLE);
544}
545
546/**
547 * device_links_check_suppliers - Check presence of supplier drivers.
548 * @dev: Consumer device.
549 *
550 * Check links from this device to any suppliers. Walk the list of the device's
551 * links to suppliers and see if all of them are available. If not, simply
552 * return -EPROBE_DEFER.
553 *
554 * We need to guarantee that the supplier will not go away after the check has
555 * been positive here. It only can go away in __device_release_driver() and
556 * that function checks the device's links to consumers. This means we need to
557 * mark the link as "consumer probe in progress" to make the supplier removal
558 * wait for us to complete (or bad things may happen).
559 *
Rafael J. Wysocki515db262019-07-16 17:21:06 +0200560 * Links without the DL_FLAG_MANAGED flag set are ignored.
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100561 */
562int device_links_check_suppliers(struct device *dev)
563{
564 struct device_link *link;
565 int ret = 0;
566
567 device_links_write_lock();
568
569 list_for_each_entry(link, &dev->links.suppliers, c_node) {
Rafael J. Wysocki515db262019-07-16 17:21:06 +0200570 if (!(link->flags & DL_FLAG_MANAGED))
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100571 continue;
572
573 if (link->status != DL_STATE_AVAILABLE) {
574 device_links_missing_supplier(dev);
575 ret = -EPROBE_DEFER;
576 break;
577 }
578 WRITE_ONCE(link->status, DL_STATE_CONSUMER_PROBE);
579 }
580 dev->links.status = DL_DEV_PROBING;
581
582 device_links_write_unlock();
583 return ret;
584}
585
586/**
587 * device_links_driver_bound - Update device links after probing its driver.
588 * @dev: Device to update the links for.
589 *
590 * The probe has been successful, so update links from this device to any
591 * consumers by changing their status to "available".
592 *
593 * Also change the status of @dev's links to suppliers to "active".
594 *
Rafael J. Wysocki515db262019-07-16 17:21:06 +0200595 * Links without the DL_FLAG_MANAGED flag set are ignored.
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100596 */
597void device_links_driver_bound(struct device *dev)
598{
599 struct device_link *link;
600
601 device_links_write_lock();
602
603 list_for_each_entry(link, &dev->links.consumers, s_node) {
Rafael J. Wysocki515db262019-07-16 17:21:06 +0200604 if (!(link->flags & DL_FLAG_MANAGED))
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100605 continue;
606
Rafael J. Wysocki15cfb092019-02-01 01:50:39 +0100607 /*
608 * Links created during consumer probe may be in the "consumer
609 * probe" state to start with if the supplier is still probing
610 * when they are created and they may become "active" if the
611 * consumer probe returns first. Skip them here.
612 */
613 if (link->status == DL_STATE_CONSUMER_PROBE ||
614 link->status == DL_STATE_ACTIVE)
615 continue;
616
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100617 WARN_ON(link->status != DL_STATE_DORMANT);
618 WRITE_ONCE(link->status, DL_STATE_AVAILABLE);
Rafael J. Wysockie7dd4012019-02-01 01:59:42 +0100619
620 if (link->flags & DL_FLAG_AUTOPROBE_CONSUMER)
621 driver_deferred_probe_add(link->consumer);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100622 }
623
624 list_for_each_entry(link, &dev->links.suppliers, c_node) {
Rafael J. Wysocki515db262019-07-16 17:21:06 +0200625 if (!(link->flags & DL_FLAG_MANAGED))
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100626 continue;
627
628 WARN_ON(link->status != DL_STATE_CONSUMER_PROBE);
629 WRITE_ONCE(link->status, DL_STATE_ACTIVE);
630 }
631
632 dev->links.status = DL_DEV_DRIVER_BOUND;
633
634 device_links_write_unlock();
635}
636
Rafael J. Wysocki515db262019-07-16 17:21:06 +0200637static void device_link_drop_managed(struct device_link *link)
638{
639 link->flags &= ~DL_FLAG_MANAGED;
640 WRITE_ONCE(link->status, DL_STATE_NONE);
641 kref_put(&link->kref, __device_link_del);
642}
643
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100644/**
645 * __device_links_no_driver - Update links of a device without a driver.
646 * @dev: Device without a drvier.
647 *
648 * Delete all non-persistent links from this device to any suppliers.
649 *
650 * Persistent links stay around, but their status is changed to "available",
651 * unless they already are in the "supplier unbind in progress" state in which
652 * case they need not be updated.
653 *
Rafael J. Wysocki515db262019-07-16 17:21:06 +0200654 * Links without the DL_FLAG_MANAGED flag set are ignored.
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100655 */
656static void __device_links_no_driver(struct device *dev)
657{
658 struct device_link *link, *ln;
659
660 list_for_each_entry_safe_reverse(link, ln, &dev->links.suppliers, c_node) {
Rafael J. Wysocki515db262019-07-16 17:21:06 +0200661 if (!(link->flags & DL_FLAG_MANAGED))
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100662 continue;
663
Vivek Gautame88728f2018-06-27 18:20:55 +0530664 if (link->flags & DL_FLAG_AUTOREMOVE_CONSUMER)
Rafael J. Wysocki515db262019-07-16 17:21:06 +0200665 device_link_drop_managed(link);
Rafael J. Wysocki15cfb092019-02-01 01:50:39 +0100666 else if (link->status == DL_STATE_CONSUMER_PROBE ||
667 link->status == DL_STATE_ACTIVE)
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100668 WRITE_ONCE(link->status, DL_STATE_AVAILABLE);
669 }
670
671 dev->links.status = DL_DEV_NO_DRIVER;
672}
673
Rafael J. Wysocki15cfb092019-02-01 01:50:39 +0100674/**
675 * device_links_no_driver - Update links after failing driver probe.
676 * @dev: Device whose driver has just failed to probe.
677 *
678 * Clean up leftover links to consumers for @dev and invoke
679 * %__device_links_no_driver() to update links to suppliers for it as
680 * appropriate.
681 *
Rafael J. Wysocki515db262019-07-16 17:21:06 +0200682 * Links without the DL_FLAG_MANAGED flag set are ignored.
Rafael J. Wysocki15cfb092019-02-01 01:50:39 +0100683 */
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100684void device_links_no_driver(struct device *dev)
685{
Rafael J. Wysocki15cfb092019-02-01 01:50:39 +0100686 struct device_link *link;
687
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100688 device_links_write_lock();
Rafael J. Wysocki15cfb092019-02-01 01:50:39 +0100689
690 list_for_each_entry(link, &dev->links.consumers, s_node) {
Rafael J. Wysocki515db262019-07-16 17:21:06 +0200691 if (!(link->flags & DL_FLAG_MANAGED))
Rafael J. Wysocki15cfb092019-02-01 01:50:39 +0100692 continue;
693
694 /*
695 * The probe has failed, so if the status of the link is
696 * "consumer probe" or "active", it must have been added by
697 * a probing consumer while this device was still probing.
698 * Change its state to "dormant", as it represents a valid
699 * relationship, but it is not functionally meaningful.
700 */
701 if (link->status == DL_STATE_CONSUMER_PROBE ||
702 link->status == DL_STATE_ACTIVE)
703 WRITE_ONCE(link->status, DL_STATE_DORMANT);
704 }
705
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100706 __device_links_no_driver(dev);
Rafael J. Wysocki15cfb092019-02-01 01:50:39 +0100707
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100708 device_links_write_unlock();
709}
710
711/**
712 * device_links_driver_cleanup - Update links after driver removal.
713 * @dev: Device whose driver has just gone away.
714 *
715 * Update links to consumers for @dev by changing their status to "dormant" and
716 * invoke %__device_links_no_driver() to update links to suppliers for it as
717 * appropriate.
718 *
Rafael J. Wysocki515db262019-07-16 17:21:06 +0200719 * Links without the DL_FLAG_MANAGED flag set are ignored.
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100720 */
721void device_links_driver_cleanup(struct device *dev)
722{
Rafael J. Wysockic8d50982019-02-01 01:45:55 +0100723 struct device_link *link, *ln;
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100724
725 device_links_write_lock();
726
Rafael J. Wysockic8d50982019-02-01 01:45:55 +0100727 list_for_each_entry_safe(link, ln, &dev->links.consumers, s_node) {
Rafael J. Wysocki515db262019-07-16 17:21:06 +0200728 if (!(link->flags & DL_FLAG_MANAGED))
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100729 continue;
730
Vivek Gautame88728f2018-06-27 18:20:55 +0530731 WARN_ON(link->flags & DL_FLAG_AUTOREMOVE_CONSUMER);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100732 WARN_ON(link->status != DL_STATE_SUPPLIER_UNBIND);
Vivek Gautam1689cac2018-06-27 18:20:56 +0530733
734 /*
735 * autoremove the links between this @dev and its consumer
736 * devices that are not active, i.e. where the link state
737 * has moved to DL_STATE_SUPPLIER_UNBIND.
738 */
739 if (link->status == DL_STATE_SUPPLIER_UNBIND &&
740 link->flags & DL_FLAG_AUTOREMOVE_SUPPLIER)
Rafael J. Wysocki515db262019-07-16 17:21:06 +0200741 device_link_drop_managed(link);
Vivek Gautam1689cac2018-06-27 18:20:56 +0530742
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100743 WRITE_ONCE(link->status, DL_STATE_DORMANT);
744 }
745
746 __device_links_no_driver(dev);
747
748 device_links_write_unlock();
749}
750
751/**
752 * device_links_busy - Check if there are any busy links to consumers.
753 * @dev: Device to check.
754 *
755 * Check each consumer of the device and return 'true' if its link's status
756 * is one of "consumer probe" or "active" (meaning that the given consumer is
757 * probing right now or its driver is present). Otherwise, change the link
758 * state to "supplier unbind" to prevent the consumer from being probed
759 * successfully going forward.
760 *
761 * Return 'false' if there are no probing or active consumers.
762 *
Rafael J. Wysocki515db262019-07-16 17:21:06 +0200763 * Links without the DL_FLAG_MANAGED flag set are ignored.
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100764 */
765bool device_links_busy(struct device *dev)
766{
767 struct device_link *link;
768 bool ret = false;
769
770 device_links_write_lock();
771
772 list_for_each_entry(link, &dev->links.consumers, s_node) {
Rafael J. Wysocki515db262019-07-16 17:21:06 +0200773 if (!(link->flags & DL_FLAG_MANAGED))
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100774 continue;
775
776 if (link->status == DL_STATE_CONSUMER_PROBE
777 || link->status == DL_STATE_ACTIVE) {
778 ret = true;
779 break;
780 }
781 WRITE_ONCE(link->status, DL_STATE_SUPPLIER_UNBIND);
782 }
783
784 dev->links.status = DL_DEV_UNBINDING;
785
786 device_links_write_unlock();
787 return ret;
788}
789
790/**
791 * device_links_unbind_consumers - Force unbind consumers of the given device.
792 * @dev: Device to unbind the consumers of.
793 *
794 * Walk the list of links to consumers for @dev and if any of them is in the
795 * "consumer probe" state, wait for all device probes in progress to complete
796 * and start over.
797 *
798 * If that's not the case, change the status of the link to "supplier unbind"
799 * and check if the link was in the "active" state. If so, force the consumer
800 * driver to unbind and start over (the consumer will not re-probe as we have
801 * changed the state of the link already).
802 *
Rafael J. Wysocki515db262019-07-16 17:21:06 +0200803 * Links without the DL_FLAG_MANAGED flag set are ignored.
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100804 */
805void device_links_unbind_consumers(struct device *dev)
806{
807 struct device_link *link;
808
809 start:
810 device_links_write_lock();
811
812 list_for_each_entry(link, &dev->links.consumers, s_node) {
813 enum device_link_state status;
814
Rafael J. Wysocki515db262019-07-16 17:21:06 +0200815 if (!(link->flags & DL_FLAG_MANAGED))
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100816 continue;
817
818 status = link->status;
819 if (status == DL_STATE_CONSUMER_PROBE) {
820 device_links_write_unlock();
821
822 wait_for_device_probe();
823 goto start;
824 }
825 WRITE_ONCE(link->status, DL_STATE_SUPPLIER_UNBIND);
826 if (status == DL_STATE_ACTIVE) {
827 struct device *consumer = link->consumer;
828
829 get_device(consumer);
830
831 device_links_write_unlock();
832
833 device_release_driver_internal(consumer, NULL,
834 consumer->parent);
835 put_device(consumer);
836 goto start;
837 }
838 }
839
840 device_links_write_unlock();
841}
842
843/**
844 * device_links_purge - Delete existing links to other devices.
845 * @dev: Target device.
846 */
847static void device_links_purge(struct device *dev)
848{
849 struct device_link *link, *ln;
850
851 /*
852 * Delete all of the remaining links from this device to any other
853 * devices (either consumers or suppliers).
854 */
855 device_links_write_lock();
856
857 list_for_each_entry_safe_reverse(link, ln, &dev->links.suppliers, c_node) {
858 WARN_ON(link->status == DL_STATE_ACTIVE);
Lukas Wunneread18c22018-02-10 19:27:12 +0100859 __device_link_del(&link->kref);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100860 }
861
862 list_for_each_entry_safe_reverse(link, ln, &dev->links.consumers, s_node) {
863 WARN_ON(link->status != DL_STATE_DORMANT &&
864 link->status != DL_STATE_NONE);
Lukas Wunneread18c22018-02-10 19:27:12 +0100865 __device_link_del(&link->kref);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100866 }
867
868 device_links_write_unlock();
869}
870
871/* Device links support end. */
872
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800873int (*platform_notify)(struct device *dev) = NULL;
874int (*platform_notify_remove)(struct device *dev) = NULL;
Dan Williamse105b8b2008-04-21 10:51:07 -0700875static struct kobject *dev_kobj;
876struct kobject *sysfs_dev_char_kobj;
877struct kobject *sysfs_dev_block_kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878
Rafael J. Wysocki5e33bc42013-08-28 21:41:01 +0200879static DEFINE_MUTEX(device_hotplug_lock);
880
881void lock_device_hotplug(void)
882{
883 mutex_lock(&device_hotplug_lock);
884}
885
886void unlock_device_hotplug(void)
887{
888 mutex_unlock(&device_hotplug_lock);
889}
890
891int lock_device_hotplug_sysfs(void)
892{
893 if (mutex_trylock(&device_hotplug_lock))
894 return 0;
895
896 /* Avoid busy looping (5 ms of sleep should do). */
897 msleep(5);
898 return restart_syscall();
899}
900
Greg Kroah-Hartman4e886c22008-01-27 12:12:43 -0800901#ifdef CONFIG_BLOCK
902static inline int device_is_not_partition(struct device *dev)
903{
904 return !(dev->type == &part_type);
905}
906#else
907static inline int device_is_not_partition(struct device *dev)
908{
909 return 1;
910}
911#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912
Heikki Krogerus07de0e82018-11-09 17:21:34 +0300913static int
914device_platform_notify(struct device *dev, enum kobject_action action)
915{
Heikki Krogerus7847a142018-11-09 17:21:35 +0300916 int ret;
917
918 ret = acpi_platform_notify(dev, action);
919 if (ret)
920 return ret;
921
Heikki Krogerus59abd832018-11-09 17:21:36 +0300922 ret = software_node_notify(dev, action);
923 if (ret)
924 return ret;
925
Heikki Krogerus07de0e82018-11-09 17:21:34 +0300926 if (platform_notify && action == KOBJ_ADD)
927 platform_notify(dev);
928 else if (platform_notify_remove && action == KOBJ_REMOVE)
929 platform_notify_remove(dev);
930 return 0;
931}
932
Alan Stern3e956372006-06-16 17:10:48 -0400933/**
934 * dev_driver_string - Return a device's driver name, if at all possible
935 * @dev: struct device to get the name of
936 *
937 * Will return the device's driver's name if it is bound to a device. If
yan9169c012012-04-20 21:08:45 +0800938 * the device is not bound to a driver, it will return the name of the bus
Alan Stern3e956372006-06-16 17:10:48 -0400939 * it is attached to. If it is not attached to a bus either, an empty
940 * string will be returned.
941 */
Jean Delvarebf9ca692008-07-30 12:29:21 -0700942const char *dev_driver_string(const struct device *dev)
Alan Stern3e956372006-06-16 17:10:48 -0400943{
Alan Stern35899722009-12-04 11:06:57 -0500944 struct device_driver *drv;
945
946 /* dev->driver can change to NULL underneath us because of unbinding,
947 * so be careful about accessing it. dev->bus and dev->class should
948 * never change once they are set, so they don't need special care.
949 */
Mark Rutland6aa7de02017-10-23 14:07:29 -0700950 drv = READ_ONCE(dev->driver);
Alan Stern35899722009-12-04 11:06:57 -0500951 return drv ? drv->name :
Jean Delvarea456b702007-03-09 16:33:10 +0100952 (dev->bus ? dev->bus->name :
953 (dev->class ? dev->class->name : ""));
Alan Stern3e956372006-06-16 17:10:48 -0400954}
Matthew Wilcox310a9222006-09-23 23:35:04 -0600955EXPORT_SYMBOL(dev_driver_string);
Alan Stern3e956372006-06-16 17:10:48 -0400956
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957#define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr)
958
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800959static ssize_t dev_attr_show(struct kobject *kobj, struct attribute *attr,
960 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800962 struct device_attribute *dev_attr = to_dev_attr(attr);
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200963 struct device *dev = kobj_to_dev(kobj);
Dmitry Torokhov4a0c20b2005-04-29 01:23:47 -0500964 ssize_t ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965
966 if (dev_attr->show)
Yani Ioannou54b6f352005-05-17 06:39:34 -0400967 ret = dev_attr->show(dev, dev_attr, buf);
Andrew Morton815d2d52008-03-04 15:09:07 -0800968 if (ret >= (ssize_t)PAGE_SIZE) {
Sergey Senozhatskya52668c2017-12-11 21:50:21 +0900969 printk("dev_attr_show: %pS returned bad count\n",
970 dev_attr->show);
Andrew Morton815d2d52008-03-04 15:09:07 -0800971 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 return ret;
973}
974
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800975static ssize_t dev_attr_store(struct kobject *kobj, struct attribute *attr,
976 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800978 struct device_attribute *dev_attr = to_dev_attr(attr);
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200979 struct device *dev = kobj_to_dev(kobj);
Dmitry Torokhov4a0c20b2005-04-29 01:23:47 -0500980 ssize_t ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981
982 if (dev_attr->store)
Yani Ioannou54b6f352005-05-17 06:39:34 -0400983 ret = dev_attr->store(dev, dev_attr, buf, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 return ret;
985}
986
Emese Revfy52cf25d2010-01-19 02:58:23 +0100987static const struct sysfs_ops dev_sysfs_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 .show = dev_attr_show,
989 .store = dev_attr_store,
990};
991
Kay Sieversca22e562011-12-14 14:29:38 -0800992#define to_ext_attr(x) container_of(x, struct dev_ext_attribute, attr)
993
994ssize_t device_store_ulong(struct device *dev,
995 struct device_attribute *attr,
996 const char *buf, size_t size)
997{
998 struct dev_ext_attribute *ea = to_ext_attr(attr);
Kaitao chengf88184b2018-11-06 08:34:54 -0800999 int ret;
1000 unsigned long new;
1001
1002 ret = kstrtoul(buf, 0, &new);
1003 if (ret)
1004 return ret;
Kay Sieversca22e562011-12-14 14:29:38 -08001005 *(unsigned long *)(ea->var) = new;
1006 /* Always return full write size even if we didn't consume all */
1007 return size;
1008}
1009EXPORT_SYMBOL_GPL(device_store_ulong);
1010
1011ssize_t device_show_ulong(struct device *dev,
1012 struct device_attribute *attr,
1013 char *buf)
1014{
1015 struct dev_ext_attribute *ea = to_ext_attr(attr);
1016 return snprintf(buf, PAGE_SIZE, "%lx\n", *(unsigned long *)(ea->var));
1017}
1018EXPORT_SYMBOL_GPL(device_show_ulong);
1019
1020ssize_t device_store_int(struct device *dev,
1021 struct device_attribute *attr,
1022 const char *buf, size_t size)
1023{
1024 struct dev_ext_attribute *ea = to_ext_attr(attr);
Kaitao chengf88184b2018-11-06 08:34:54 -08001025 int ret;
1026 long new;
1027
1028 ret = kstrtol(buf, 0, &new);
1029 if (ret)
1030 return ret;
1031
1032 if (new > INT_MAX || new < INT_MIN)
Kay Sieversca22e562011-12-14 14:29:38 -08001033 return -EINVAL;
1034 *(int *)(ea->var) = new;
1035 /* Always return full write size even if we didn't consume all */
1036 return size;
1037}
1038EXPORT_SYMBOL_GPL(device_store_int);
1039
1040ssize_t device_show_int(struct device *dev,
1041 struct device_attribute *attr,
1042 char *buf)
1043{
1044 struct dev_ext_attribute *ea = to_ext_attr(attr);
1045
1046 return snprintf(buf, PAGE_SIZE, "%d\n", *(int *)(ea->var));
1047}
1048EXPORT_SYMBOL_GPL(device_show_int);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049
Borislav Petkov91872392012-10-09 19:52:05 +02001050ssize_t device_store_bool(struct device *dev, struct device_attribute *attr,
1051 const char *buf, size_t size)
1052{
1053 struct dev_ext_attribute *ea = to_ext_attr(attr);
1054
1055 if (strtobool(buf, ea->var) < 0)
1056 return -EINVAL;
1057
1058 return size;
1059}
1060EXPORT_SYMBOL_GPL(device_store_bool);
1061
1062ssize_t device_show_bool(struct device *dev, struct device_attribute *attr,
1063 char *buf)
1064{
1065 struct dev_ext_attribute *ea = to_ext_attr(attr);
1066
1067 return snprintf(buf, PAGE_SIZE, "%d\n", *(bool *)(ea->var));
1068}
1069EXPORT_SYMBOL_GPL(device_show_bool);
1070
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071/**
Robert P. J. Dayf8878dc2013-06-01 20:17:34 -04001072 * device_release - free device structure.
1073 * @kobj: device's kobject.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 *
Robert P. J. Dayf8878dc2013-06-01 20:17:34 -04001075 * This is called once the reference count for the object
1076 * reaches 0. We forward the call to the device's release
1077 * method, which should handle actually freeing the structure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001079static void device_release(struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +02001081 struct device *dev = kobj_to_dev(kobj);
Greg Kroah-Hartmanfb069a52008-12-16 12:23:36 -08001082 struct device_private *p = dev->p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083
Ming Leia525a3d2012-07-25 01:42:29 +08001084 /*
1085 * Some platform devices are driven without driver attached
1086 * and managed resources may have been acquired. Make sure
1087 * all resources are released.
1088 *
1089 * Drivers still can add resources into device after device
1090 * is deleted but alive, so release devres here to avoid
1091 * possible memory leak.
1092 */
1093 devres_release_all(dev);
1094
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 if (dev->release)
1096 dev->release(dev);
Kay Sieversf9f852d2006-10-07 21:54:55 +02001097 else if (dev->type && dev->type->release)
1098 dev->type->release(dev);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07001099 else if (dev->class && dev->class->dev_release)
1100 dev->class->dev_release(dev);
Arjan van de Venf810a5c2008-07-25 19:45:39 -07001101 else
Ezequiel Garcia186bddb2018-12-03 13:44:35 -03001102 WARN(1, KERN_ERR "Device '%s' does not have a release() function, it is broken and must be fixed. See Documentation/kobject.txt.\n",
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01001103 dev_name(dev));
Greg Kroah-Hartmanfb069a52008-12-16 12:23:36 -08001104 kfree(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105}
1106
Eric W. Biedermanbc451f22010-03-30 11:31:25 -07001107static const void *device_namespace(struct kobject *kobj)
1108{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +02001109 struct device *dev = kobj_to_dev(kobj);
Eric W. Biedermanbc451f22010-03-30 11:31:25 -07001110 const void *ns = NULL;
1111
1112 if (dev->class && dev->class->ns_type)
1113 ns = dev->class->namespace(dev);
1114
1115 return ns;
1116}
1117
Dmitry Torokhov9944e892018-07-20 21:56:50 +00001118static void device_get_ownership(struct kobject *kobj, kuid_t *uid, kgid_t *gid)
1119{
1120 struct device *dev = kobj_to_dev(kobj);
1121
1122 if (dev->class && dev->class->get_ownership)
1123 dev->class->get_ownership(dev, uid, gid);
1124}
1125
Greg Kroah-Hartman8f4afc42007-10-11 10:47:49 -06001126static struct kobj_type device_ktype = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 .release = device_release,
1128 .sysfs_ops = &dev_sysfs_ops,
Eric W. Biedermanbc451f22010-03-30 11:31:25 -07001129 .namespace = device_namespace,
Dmitry Torokhov9944e892018-07-20 21:56:50 +00001130 .get_ownership = device_get_ownership,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131};
1132
1133
Kay Sievers312c0042005-11-16 09:00:00 +01001134static int dev_uevent_filter(struct kset *kset, struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135{
1136 struct kobj_type *ktype = get_ktype(kobj);
1137
Greg Kroah-Hartman8f4afc42007-10-11 10:47:49 -06001138 if (ktype == &device_ktype) {
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +02001139 struct device *dev = kobj_to_dev(kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 if (dev->bus)
1141 return 1;
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001142 if (dev->class)
1143 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 }
1145 return 0;
1146}
1147
Kay Sievers312c0042005-11-16 09:00:00 +01001148static const char *dev_uevent_name(struct kset *kset, struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +02001150 struct device *dev = kobj_to_dev(kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001152 if (dev->bus)
1153 return dev->bus->name;
1154 if (dev->class)
1155 return dev->class->name;
1156 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157}
1158
Kay Sievers7eff2e72007-08-14 15:15:12 +02001159static int dev_uevent(struct kset *kset, struct kobject *kobj,
1160 struct kobj_uevent_env *env)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +02001162 struct device *dev = kobj_to_dev(kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 int retval = 0;
1164
Kay Sievers6fcf53a2009-04-30 15:23:42 +02001165 /* add device node properties if present */
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001166 if (MAJOR(dev->devt)) {
Kay Sievers6fcf53a2009-04-30 15:23:42 +02001167 const char *tmp;
1168 const char *name;
Al Viro2c9ede52011-07-23 20:24:48 -04001169 umode_t mode = 0;
Greg Kroah-Hartman4e4098a2013-04-11 11:43:29 -07001170 kuid_t uid = GLOBAL_ROOT_UID;
1171 kgid_t gid = GLOBAL_ROOT_GID;
Kay Sievers6fcf53a2009-04-30 15:23:42 +02001172
Kay Sievers7eff2e72007-08-14 15:15:12 +02001173 add_uevent_var(env, "MAJOR=%u", MAJOR(dev->devt));
1174 add_uevent_var(env, "MINOR=%u", MINOR(dev->devt));
Kay Sievers3c2670e2013-04-06 09:56:00 -07001175 name = device_get_devnode(dev, &mode, &uid, &gid, &tmp);
Kay Sievers6fcf53a2009-04-30 15:23:42 +02001176 if (name) {
1177 add_uevent_var(env, "DEVNAME=%s", name);
Kay Sieverse454cea2009-09-18 23:01:12 +02001178 if (mode)
1179 add_uevent_var(env, "DEVMODE=%#o", mode & 0777);
Greg Kroah-Hartman4e4098a2013-04-11 11:43:29 -07001180 if (!uid_eq(uid, GLOBAL_ROOT_UID))
1181 add_uevent_var(env, "DEVUID=%u", from_kuid(&init_user_ns, uid));
1182 if (!gid_eq(gid, GLOBAL_ROOT_GID))
1183 add_uevent_var(env, "DEVGID=%u", from_kgid(&init_user_ns, gid));
Kay Sievers3c2670e2013-04-06 09:56:00 -07001184 kfree(tmp);
Kay Sievers6fcf53a2009-04-30 15:23:42 +02001185 }
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001186 }
1187
Kay Sievers414264f2007-03-12 21:08:57 +01001188 if (dev->type && dev->type->name)
Kay Sievers7eff2e72007-08-14 15:15:12 +02001189 add_uevent_var(env, "DEVTYPE=%s", dev->type->name);
Kay Sievers414264f2007-03-12 21:08:57 +01001190
Kay Sievers239378f2006-10-07 21:54:55 +02001191 if (dev->driver)
Kay Sievers7eff2e72007-08-14 15:15:12 +02001192 add_uevent_var(env, "DRIVER=%s", dev->driver->name);
Kay Sievers239378f2006-10-07 21:54:55 +02001193
Grant Likely07d57a32012-02-01 11:22:22 -07001194 /* Add common DT information about the device */
1195 of_device_uevent(dev, env);
1196
Kay Sievers7eff2e72007-08-14 15:15:12 +02001197 /* have the bus specific function add its stuff */
Kay Sievers312c0042005-11-16 09:00:00 +01001198 if (dev->bus && dev->bus->uevent) {
Kay Sievers7eff2e72007-08-14 15:15:12 +02001199 retval = dev->bus->uevent(dev, env);
Kay Sieversf9f852d2006-10-07 21:54:55 +02001200 if (retval)
Greg Kroah-Hartman7dc72b22007-11-28 23:49:41 -08001201 pr_debug("device: '%s': %s: bus uevent() returned %d\n",
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01001202 dev_name(dev), __func__, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 }
1204
Kay Sievers7eff2e72007-08-14 15:15:12 +02001205 /* have the class specific function add its stuff */
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07001206 if (dev->class && dev->class->dev_uevent) {
Kay Sievers7eff2e72007-08-14 15:15:12 +02001207 retval = dev->class->dev_uevent(dev, env);
Kay Sieversf9f852d2006-10-07 21:54:55 +02001208 if (retval)
Greg Kroah-Hartman7dc72b22007-11-28 23:49:41 -08001209 pr_debug("device: '%s': %s: class uevent() "
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01001210 "returned %d\n", dev_name(dev),
Harvey Harrison2b3a3022008-03-04 16:41:05 -08001211 __func__, retval);
Kay Sieversf9f852d2006-10-07 21:54:55 +02001212 }
1213
Stefan Weileef35c22010-08-06 21:11:15 +02001214 /* have the device type specific function add its stuff */
Kay Sieversf9f852d2006-10-07 21:54:55 +02001215 if (dev->type && dev->type->uevent) {
Kay Sievers7eff2e72007-08-14 15:15:12 +02001216 retval = dev->type->uevent(dev, env);
Kay Sieversf9f852d2006-10-07 21:54:55 +02001217 if (retval)
Greg Kroah-Hartman7dc72b22007-11-28 23:49:41 -08001218 pr_debug("device: '%s': %s: dev_type uevent() "
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01001219 "returned %d\n", dev_name(dev),
Harvey Harrison2b3a3022008-03-04 16:41:05 -08001220 __func__, retval);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07001221 }
1222
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 return retval;
1224}
1225
Emese Revfy9cd43612009-12-31 14:52:51 +01001226static const struct kset_uevent_ops device_uevent_ops = {
Kay Sievers312c0042005-11-16 09:00:00 +01001227 .filter = dev_uevent_filter,
1228 .name = dev_uevent_name,
1229 .uevent = dev_uevent,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230};
1231
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07001232static ssize_t uevent_show(struct device *dev, struct device_attribute *attr,
Kay Sievers16574dc2007-04-06 01:40:38 +02001233 char *buf)
1234{
1235 struct kobject *top_kobj;
1236 struct kset *kset;
Kay Sievers7eff2e72007-08-14 15:15:12 +02001237 struct kobj_uevent_env *env = NULL;
Kay Sievers16574dc2007-04-06 01:40:38 +02001238 int i;
1239 size_t count = 0;
1240 int retval;
1241
1242 /* search the kset, the device belongs to */
1243 top_kobj = &dev->kobj;
Kay Sievers5c5daf62007-08-12 20:43:55 +02001244 while (!top_kobj->kset && top_kobj->parent)
1245 top_kobj = top_kobj->parent;
Kay Sievers16574dc2007-04-06 01:40:38 +02001246 if (!top_kobj->kset)
1247 goto out;
Kay Sievers5c5daf62007-08-12 20:43:55 +02001248
Kay Sievers16574dc2007-04-06 01:40:38 +02001249 kset = top_kobj->kset;
1250 if (!kset->uevent_ops || !kset->uevent_ops->uevent)
1251 goto out;
1252
1253 /* respect filter */
1254 if (kset->uevent_ops && kset->uevent_ops->filter)
1255 if (!kset->uevent_ops->filter(kset, &dev->kobj))
1256 goto out;
1257
Kay Sievers7eff2e72007-08-14 15:15:12 +02001258 env = kzalloc(sizeof(struct kobj_uevent_env), GFP_KERNEL);
1259 if (!env)
Greg Kroah-Hartmanc7308c82007-05-02 14:14:11 +02001260 return -ENOMEM;
1261
Kay Sievers16574dc2007-04-06 01:40:38 +02001262 /* let the kset specific function add its keys */
Kay Sievers7eff2e72007-08-14 15:15:12 +02001263 retval = kset->uevent_ops->uevent(kset, &dev->kobj, env);
Kay Sievers16574dc2007-04-06 01:40:38 +02001264 if (retval)
1265 goto out;
1266
1267 /* copy keys to file */
Kay Sievers7eff2e72007-08-14 15:15:12 +02001268 for (i = 0; i < env->envp_idx; i++)
1269 count += sprintf(&buf[count], "%s\n", env->envp[i]);
Kay Sievers16574dc2007-04-06 01:40:38 +02001270out:
Kay Sievers7eff2e72007-08-14 15:15:12 +02001271 kfree(env);
Kay Sievers16574dc2007-04-06 01:40:38 +02001272 return count;
1273}
1274
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07001275static ssize_t uevent_store(struct device *dev, struct device_attribute *attr,
Kay Sieversa7fd6702005-10-01 14:49:43 +02001276 const char *buf, size_t count)
1277{
Peter Rajnohadf44b472018-12-05 12:27:44 +01001278 int rc;
1279
1280 rc = kobject_synth_uevent(&dev->kobj, buf, count);
1281
1282 if (rc) {
Peter Rajnohaf36776f2017-05-09 15:22:30 +02001283 dev_err(dev, "uevent: failed to send synthetic uevent\n");
Peter Rajnohadf44b472018-12-05 12:27:44 +01001284 return rc;
1285 }
Kay Sievers60a96a52007-07-08 22:29:26 +02001286
Kay Sieversa7fd6702005-10-01 14:49:43 +02001287 return count;
1288}
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07001289static DEVICE_ATTR_RW(uevent);
Kay Sieversa7fd6702005-10-01 14:49:43 +02001290
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07001291static ssize_t online_show(struct device *dev, struct device_attribute *attr,
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02001292 char *buf)
1293{
1294 bool val;
1295
Rafael J. Wysocki5e33bc42013-08-28 21:41:01 +02001296 device_lock(dev);
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02001297 val = !dev->offline;
Rafael J. Wysocki5e33bc42013-08-28 21:41:01 +02001298 device_unlock(dev);
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02001299 return sprintf(buf, "%u\n", val);
1300}
1301
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07001302static ssize_t online_store(struct device *dev, struct device_attribute *attr,
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02001303 const char *buf, size_t count)
1304{
1305 bool val;
1306 int ret;
1307
1308 ret = strtobool(buf, &val);
1309 if (ret < 0)
1310 return ret;
1311
Rafael J. Wysocki5e33bc42013-08-28 21:41:01 +02001312 ret = lock_device_hotplug_sysfs();
1313 if (ret)
1314 return ret;
1315
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02001316 ret = val ? device_online(dev) : device_offline(dev);
1317 unlock_device_hotplug();
1318 return ret < 0 ? ret : count;
1319}
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07001320static DEVICE_ATTR_RW(online);
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02001321
Greg Kroah-Hartmanfa6fdb32013-08-08 15:22:55 -07001322int device_add_groups(struct device *dev, const struct attribute_group **groups)
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001323{
Greg Kroah-Hartman3e9b2ba2013-08-21 13:47:50 -07001324 return sysfs_create_groups(&dev->kobj, groups);
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001325}
Dmitry Torokhova7670d42017-07-19 17:24:31 -07001326EXPORT_SYMBOL_GPL(device_add_groups);
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001327
Greg Kroah-Hartmanfa6fdb32013-08-08 15:22:55 -07001328void device_remove_groups(struct device *dev,
1329 const struct attribute_group **groups)
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001330{
Greg Kroah-Hartman3e9b2ba2013-08-21 13:47:50 -07001331 sysfs_remove_groups(&dev->kobj, groups);
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -07001332}
Dmitry Torokhova7670d42017-07-19 17:24:31 -07001333EXPORT_SYMBOL_GPL(device_remove_groups);
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -07001334
Dmitry Torokhov57b8ff02017-07-19 17:24:33 -07001335union device_attr_group_devres {
1336 const struct attribute_group *group;
1337 const struct attribute_group **groups;
1338};
1339
1340static int devm_attr_group_match(struct device *dev, void *res, void *data)
1341{
1342 return ((union device_attr_group_devres *)res)->group == data;
1343}
1344
1345static void devm_attr_group_remove(struct device *dev, void *res)
1346{
1347 union device_attr_group_devres *devres = res;
1348 const struct attribute_group *group = devres->group;
1349
1350 dev_dbg(dev, "%s: removing group %p\n", __func__, group);
1351 sysfs_remove_group(&dev->kobj, group);
1352}
1353
1354static void devm_attr_groups_remove(struct device *dev, void *res)
1355{
1356 union device_attr_group_devres *devres = res;
1357 const struct attribute_group **groups = devres->groups;
1358
1359 dev_dbg(dev, "%s: removing groups %p\n", __func__, groups);
1360 sysfs_remove_groups(&dev->kobj, groups);
1361}
1362
1363/**
1364 * devm_device_add_group - given a device, create a managed attribute group
1365 * @dev: The device to create the group for
1366 * @grp: The attribute group to create
1367 *
1368 * This function creates a group for the first time. It will explicitly
1369 * warn and error if any of the attribute files being created already exist.
1370 *
1371 * Returns 0 on success or error code on failure.
1372 */
1373int devm_device_add_group(struct device *dev, const struct attribute_group *grp)
1374{
1375 union device_attr_group_devres *devres;
1376 int error;
1377
1378 devres = devres_alloc(devm_attr_group_remove,
1379 sizeof(*devres), GFP_KERNEL);
1380 if (!devres)
1381 return -ENOMEM;
1382
1383 error = sysfs_create_group(&dev->kobj, grp);
1384 if (error) {
1385 devres_free(devres);
1386 return error;
1387 }
1388
1389 devres->group = grp;
1390 devres_add(dev, devres);
1391 return 0;
1392}
1393EXPORT_SYMBOL_GPL(devm_device_add_group);
1394
1395/**
1396 * devm_device_remove_group: remove a managed group from a device
1397 * @dev: device to remove the group from
1398 * @grp: group to remove
1399 *
1400 * This function removes a group of attributes from a device. The attributes
1401 * previously have to have been created for this group, otherwise it will fail.
1402 */
1403void devm_device_remove_group(struct device *dev,
1404 const struct attribute_group *grp)
1405{
1406 WARN_ON(devres_release(dev, devm_attr_group_remove,
1407 devm_attr_group_match,
1408 /* cast away const */ (void *)grp));
1409}
1410EXPORT_SYMBOL_GPL(devm_device_remove_group);
1411
1412/**
1413 * devm_device_add_groups - create a bunch of managed attribute groups
1414 * @dev: The device to create the group for
1415 * @groups: The attribute groups to create, NULL terminated
1416 *
1417 * This function creates a bunch of managed attribute groups. If an error
1418 * occurs when creating a group, all previously created groups will be
1419 * removed, unwinding everything back to the original state when this
1420 * function was called. It will explicitly warn and error if any of the
1421 * attribute files being created already exist.
1422 *
1423 * Returns 0 on success or error code from sysfs_create_group on failure.
1424 */
1425int devm_device_add_groups(struct device *dev,
1426 const struct attribute_group **groups)
1427{
1428 union device_attr_group_devres *devres;
1429 int error;
1430
1431 devres = devres_alloc(devm_attr_groups_remove,
1432 sizeof(*devres), GFP_KERNEL);
1433 if (!devres)
1434 return -ENOMEM;
1435
1436 error = sysfs_create_groups(&dev->kobj, groups);
1437 if (error) {
1438 devres_free(devres);
1439 return error;
1440 }
1441
1442 devres->groups = groups;
1443 devres_add(dev, devres);
1444 return 0;
1445}
1446EXPORT_SYMBOL_GPL(devm_device_add_groups);
1447
1448/**
1449 * devm_device_remove_groups - remove a list of managed groups
1450 *
1451 * @dev: The device for the groups to be removed from
1452 * @groups: NULL terminated list of groups to be removed
1453 *
1454 * If groups is not NULL, remove the specified groups from the device.
1455 */
1456void devm_device_remove_groups(struct device *dev,
1457 const struct attribute_group **groups)
1458{
1459 WARN_ON(devres_release(dev, devm_attr_groups_remove,
1460 devm_attr_group_match,
1461 /* cast away const */ (void *)groups));
1462}
1463EXPORT_SYMBOL_GPL(devm_device_remove_groups);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07001465static int device_add_attrs(struct device *dev)
1466{
1467 struct class *class = dev->class;
Stephen Hemmingeraed65af2011-03-28 09:12:52 -07001468 const struct device_type *type = dev->type;
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001469 int error;
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07001470
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001471 if (class) {
Greg Kroah-Hartmand05a6f92013-07-14 16:05:58 -07001472 error = device_add_groups(dev, class->dev_groups);
Kay Sieversf9f852d2006-10-07 21:54:55 +02001473 if (error)
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001474 return error;
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07001475 }
Kay Sieversf9f852d2006-10-07 21:54:55 +02001476
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001477 if (type) {
1478 error = device_add_groups(dev, type->groups);
Kay Sieversf9f852d2006-10-07 21:54:55 +02001479 if (error)
Greg Kroah-Hartmana6b01de2013-10-05 18:19:30 -07001480 goto err_remove_class_groups;
Kay Sieversf9f852d2006-10-07 21:54:55 +02001481 }
1482
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001483 error = device_add_groups(dev, dev->groups);
1484 if (error)
1485 goto err_remove_type_groups;
1486
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02001487 if (device_supports_offline(dev) && !dev->offline_disabled) {
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07001488 error = device_create_file(dev, &dev_attr_online);
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02001489 if (error)
Rafael J. Wysockiecfbf6f2013-12-12 06:11:02 +01001490 goto err_remove_dev_groups;
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02001491 }
1492
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001493 return 0;
1494
Rafael J. Wysockiecfbf6f2013-12-12 06:11:02 +01001495 err_remove_dev_groups:
1496 device_remove_groups(dev, dev->groups);
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001497 err_remove_type_groups:
1498 if (type)
1499 device_remove_groups(dev, type->groups);
Greg Kroah-Hartmand05a6f92013-07-14 16:05:58 -07001500 err_remove_class_groups:
1501 if (class)
1502 device_remove_groups(dev, class->dev_groups);
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001503
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07001504 return error;
1505}
1506
1507static void device_remove_attrs(struct device *dev)
1508{
1509 struct class *class = dev->class;
Stephen Hemmingeraed65af2011-03-28 09:12:52 -07001510 const struct device_type *type = dev->type;
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07001511
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07001512 device_remove_file(dev, &dev_attr_online);
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001513 device_remove_groups(dev, dev->groups);
Kay Sieversf9f852d2006-10-07 21:54:55 +02001514
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001515 if (type)
1516 device_remove_groups(dev, type->groups);
1517
Greg Kroah-Hartmana6b01de2013-10-05 18:19:30 -07001518 if (class)
Greg Kroah-Hartmand05a6f92013-07-14 16:05:58 -07001519 device_remove_groups(dev, class->dev_groups);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07001520}
1521
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07001522static ssize_t dev_show(struct device *dev, struct device_attribute *attr,
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001523 char *buf)
1524{
1525 return print_dev_t(buf, dev->devt);
1526}
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07001527static DEVICE_ATTR_RO(dev);
Tejun Heoad6a1e12007-06-14 03:45:17 +09001528
Kay Sieversca22e562011-12-14 14:29:38 -08001529/* /sys/devices/ */
Greg Kroah-Hartman881c6cfd2007-11-01 09:29:06 -06001530struct kset *devices_kset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532/**
Grygorii Strashko52cdbdd2015-07-27 20:43:01 +03001533 * devices_kset_move_before - Move device in the devices_kset's list.
1534 * @deva: Device to move.
1535 * @devb: Device @deva should come before.
1536 */
1537static void devices_kset_move_before(struct device *deva, struct device *devb)
1538{
1539 if (!devices_kset)
1540 return;
1541 pr_debug("devices_kset: Moving %s before %s\n",
1542 dev_name(deva), dev_name(devb));
1543 spin_lock(&devices_kset->list_lock);
1544 list_move_tail(&deva->kobj.entry, &devb->kobj.entry);
1545 spin_unlock(&devices_kset->list_lock);
1546}
1547
1548/**
1549 * devices_kset_move_after - Move device in the devices_kset's list.
1550 * @deva: Device to move
1551 * @devb: Device @deva should come after.
1552 */
1553static void devices_kset_move_after(struct device *deva, struct device *devb)
1554{
1555 if (!devices_kset)
1556 return;
1557 pr_debug("devices_kset: Moving %s after %s\n",
1558 dev_name(deva), dev_name(devb));
1559 spin_lock(&devices_kset->list_lock);
1560 list_move(&deva->kobj.entry, &devb->kobj.entry);
1561 spin_unlock(&devices_kset->list_lock);
1562}
1563
1564/**
1565 * devices_kset_move_last - move the device to the end of devices_kset's list.
1566 * @dev: device to move
1567 */
1568void devices_kset_move_last(struct device *dev)
1569{
1570 if (!devices_kset)
1571 return;
1572 pr_debug("devices_kset: Moving %s to end of list\n", dev_name(dev));
1573 spin_lock(&devices_kset->list_lock);
1574 list_move_tail(&dev->kobj.entry, &devices_kset->list);
1575 spin_unlock(&devices_kset->list_lock);
1576}
1577
1578/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001579 * device_create_file - create sysfs attribute file for device.
1580 * @dev: device.
1581 * @attr: device attribute descriptor.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 */
Phil Carmody26579ab2009-12-18 15:34:19 +02001583int device_create_file(struct device *dev,
1584 const struct device_attribute *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585{
1586 int error = 0;
Felipe Balbi8f46baa2013-02-20 10:31:42 +02001587
1588 if (dev) {
1589 WARN(((attr->attr.mode & S_IWUGO) && !attr->store),
dyoung@redhat.com97521972013-05-16 14:31:30 +08001590 "Attribute %s: write permission without 'store'\n",
1591 attr->attr.name);
Felipe Balbi8f46baa2013-02-20 10:31:42 +02001592 WARN(((attr->attr.mode & S_IRUGO) && !attr->show),
dyoung@redhat.com97521972013-05-16 14:31:30 +08001593 "Attribute %s: read permission without 'show'\n",
1594 attr->attr.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 error = sysfs_create_file(&dev->kobj, &attr->attr);
Felipe Balbi8f46baa2013-02-20 10:31:42 +02001596 }
1597
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 return error;
1599}
David Graham White86df2682013-07-21 20:41:14 -04001600EXPORT_SYMBOL_GPL(device_create_file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601
1602/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001603 * device_remove_file - remove sysfs attribute file.
1604 * @dev: device.
1605 * @attr: device attribute descriptor.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 */
Phil Carmody26579ab2009-12-18 15:34:19 +02001607void device_remove_file(struct device *dev,
1608 const struct device_attribute *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609{
Cornelia Huck0c98b192008-01-31 10:39:38 +01001610 if (dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 sysfs_remove_file(&dev->kobj, &attr->attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612}
David Graham White86df2682013-07-21 20:41:14 -04001613EXPORT_SYMBOL_GPL(device_remove_file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614
Greg Kroah-Hartman2589f1882006-09-19 09:39:19 -07001615/**
Tejun Heo6b0afc22014-02-03 14:03:01 -05001616 * device_remove_file_self - remove sysfs attribute file from its own method.
1617 * @dev: device.
1618 * @attr: device attribute descriptor.
1619 *
1620 * See kernfs_remove_self() for details.
1621 */
1622bool device_remove_file_self(struct device *dev,
1623 const struct device_attribute *attr)
1624{
1625 if (dev)
1626 return sysfs_remove_file_self(&dev->kobj, &attr->attr);
1627 else
1628 return false;
1629}
1630EXPORT_SYMBOL_GPL(device_remove_file_self);
1631
1632/**
Greg Kroah-Hartman2589f1882006-09-19 09:39:19 -07001633 * device_create_bin_file - create sysfs binary attribute file for device.
1634 * @dev: device.
1635 * @attr: device binary attribute descriptor.
1636 */
Phil Carmody66ecb922009-12-18 15:34:20 +02001637int device_create_bin_file(struct device *dev,
1638 const struct bin_attribute *attr)
Greg Kroah-Hartman2589f1882006-09-19 09:39:19 -07001639{
1640 int error = -EINVAL;
1641 if (dev)
1642 error = sysfs_create_bin_file(&dev->kobj, attr);
1643 return error;
1644}
1645EXPORT_SYMBOL_GPL(device_create_bin_file);
1646
1647/**
1648 * device_remove_bin_file - remove sysfs binary attribute file
1649 * @dev: device.
1650 * @attr: device binary attribute descriptor.
1651 */
Phil Carmody66ecb922009-12-18 15:34:20 +02001652void device_remove_bin_file(struct device *dev,
1653 const struct bin_attribute *attr)
Greg Kroah-Hartman2589f1882006-09-19 09:39:19 -07001654{
1655 if (dev)
1656 sysfs_remove_bin_file(&dev->kobj, attr);
1657}
1658EXPORT_SYMBOL_GPL(device_remove_bin_file);
1659
James Bottomley34bb61f2005-09-06 16:56:51 -07001660static void klist_children_get(struct klist_node *n)
1661{
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08001662 struct device_private *p = to_device_private_parent(n);
1663 struct device *dev = p->device;
James Bottomley34bb61f2005-09-06 16:56:51 -07001664
1665 get_device(dev);
1666}
1667
1668static void klist_children_put(struct klist_node *n)
1669{
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08001670 struct device_private *p = to_device_private_parent(n);
1671 struct device *dev = p->device;
James Bottomley34bb61f2005-09-06 16:56:51 -07001672
1673 put_device(dev);
1674}
1675
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001677 * device_initialize - init device structure.
1678 * @dev: device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 *
Cornelia Huck57394112008-09-03 18:26:40 +02001680 * This prepares the device for use by other layers by initializing
1681 * its fields.
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001682 * It is the first half of device_register(), if called by
Cornelia Huck57394112008-09-03 18:26:40 +02001683 * that function, though it can also be called separately, so one
1684 * may use @dev's fields. In particular, get_device()/put_device()
1685 * may be used for reference counting of @dev after calling this
1686 * function.
1687 *
Alan Sternb10d5ef2012-01-17 11:39:00 -05001688 * All fields in @dev must be initialized by the caller to 0, except
1689 * for those explicitly set to some other value. The simplest
1690 * approach is to use kzalloc() to allocate the structure containing
1691 * @dev.
1692 *
Cornelia Huck57394112008-09-03 18:26:40 +02001693 * NOTE: Use put_device() to give up your reference instead of freeing
1694 * @dev directly once you have called this function.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696void device_initialize(struct device *dev)
1697{
Greg Kroah-Hartman881c6cfd2007-11-01 09:29:06 -06001698 dev->kobj.kset = devices_kset;
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -07001699 kobject_init(&dev->kobj, &device_ktype);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 INIT_LIST_HEAD(&dev->dma_pools);
Thomas Gleixner31427882010-01-29 20:39:02 +00001701 mutex_init(&dev->mutex);
Dan Williams87a30e12019-07-17 18:08:26 -07001702#ifdef CONFIG_PROVE_LOCKING
1703 mutex_init(&dev->lockdep_mutex);
1704#endif
Peter Zijlstra1704f472010-03-19 01:37:42 +01001705 lockdep_set_novalidate_class(&dev->mutex);
Tejun Heo9ac78492007-01-20 16:00:26 +09001706 spin_lock_init(&dev->devres_lock);
1707 INIT_LIST_HEAD(&dev->devres_head);
Alan Stern3b98aea2008-08-07 13:06:12 -04001708 device_pm_init(dev);
Christoph Hellwig87348132006-12-06 20:32:33 -08001709 set_dev_node(dev, -1);
Jiang Liu4a7cc832015-07-09 16:00:44 +08001710#ifdef CONFIG_GENERIC_MSI_IRQ
1711 INIT_LIST_HEAD(&dev->msi_list);
1712#endif
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001713 INIT_LIST_HEAD(&dev->links.consumers);
1714 INIT_LIST_HEAD(&dev->links.suppliers);
1715 dev->links.status = DL_DEV_NO_DRIVER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716}
David Graham White86df2682013-07-21 20:41:14 -04001717EXPORT_SYMBOL_GPL(device_initialize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718
Tejun Heod73ce002013-03-12 11:30:05 -07001719struct kobject *virtual_device_parent(struct device *dev)
Greg Kroah-Hartmanf0ee61a2006-10-23 10:40:54 -07001720{
Kay Sievers86406242007-03-14 03:25:56 +01001721 static struct kobject *virtual_dir = NULL;
Greg Kroah-Hartmanf0ee61a2006-10-23 10:40:54 -07001722
Kay Sievers86406242007-03-14 03:25:56 +01001723 if (!virtual_dir)
Greg Kroah-Hartman4ff6abf2007-11-05 22:24:43 -08001724 virtual_dir = kobject_create_and_add("virtual",
Greg Kroah-Hartman881c6cfd2007-11-01 09:29:06 -06001725 &devices_kset->kobj);
Greg Kroah-Hartmanf0ee61a2006-10-23 10:40:54 -07001726
Kay Sievers86406242007-03-14 03:25:56 +01001727 return virtual_dir;
Greg Kroah-Hartmanf0ee61a2006-10-23 10:40:54 -07001728}
1729
Eric W. Biedermanbc451f22010-03-30 11:31:25 -07001730struct class_dir {
1731 struct kobject kobj;
1732 struct class *class;
1733};
1734
1735#define to_class_dir(obj) container_of(obj, struct class_dir, kobj)
1736
1737static void class_dir_release(struct kobject *kobj)
1738{
1739 struct class_dir *dir = to_class_dir(kobj);
1740 kfree(dir);
1741}
1742
1743static const
1744struct kobj_ns_type_operations *class_dir_child_ns_type(struct kobject *kobj)
1745{
1746 struct class_dir *dir = to_class_dir(kobj);
1747 return dir->class->ns_type;
1748}
1749
1750static struct kobj_type class_dir_ktype = {
1751 .release = class_dir_release,
1752 .sysfs_ops = &kobj_sysfs_ops,
1753 .child_ns_type = class_dir_child_ns_type
1754};
1755
1756static struct kobject *
1757class_dir_create_and_add(struct class *class, struct kobject *parent_kobj)
1758{
1759 struct class_dir *dir;
1760 int retval;
1761
1762 dir = kzalloc(sizeof(*dir), GFP_KERNEL);
1763 if (!dir)
Tetsuo Handa84d0c272018-05-07 19:10:31 +09001764 return ERR_PTR(-ENOMEM);
Eric W. Biedermanbc451f22010-03-30 11:31:25 -07001765
1766 dir->class = class;
1767 kobject_init(&dir->kobj, &class_dir_ktype);
1768
Kay Sievers6b6e39a2010-11-15 23:13:18 +01001769 dir->kobj.kset = &class->p->glue_dirs;
Eric W. Biedermanbc451f22010-03-30 11:31:25 -07001770
1771 retval = kobject_add(&dir->kobj, parent_kobj, "%s", class->name);
1772 if (retval < 0) {
1773 kobject_put(&dir->kobj);
Tetsuo Handa84d0c272018-05-07 19:10:31 +09001774 return ERR_PTR(retval);
Eric W. Biedermanbc451f22010-03-30 11:31:25 -07001775 }
1776 return &dir->kobj;
1777}
1778
Yijing Wange4a60d12014-11-07 12:05:49 +08001779static DEFINE_MUTEX(gdp_mutex);
Eric W. Biedermanbc451f22010-03-30 11:31:25 -07001780
Kay Sieversda231fd2007-11-21 17:29:15 +01001781static struct kobject *get_device_parent(struct device *dev,
1782 struct device *parent)
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +01001783{
Kay Sievers86406242007-03-14 03:25:56 +01001784 if (dev->class) {
1785 struct kobject *kobj = NULL;
1786 struct kobject *parent_kobj;
1787 struct kobject *k;
1788
Randy Dunlapead454f2010-09-24 14:36:49 -07001789#ifdef CONFIG_BLOCK
Kay Sievers39aba962010-09-04 22:33:14 -07001790 /* block disks show up in /sys/block */
Andi Kleene52eec12010-09-08 16:54:17 +02001791 if (sysfs_deprecated && dev->class == &block_class) {
Kay Sievers39aba962010-09-04 22:33:14 -07001792 if (parent && parent->class == &block_class)
1793 return &parent->kobj;
Kay Sievers6b6e39a2010-11-15 23:13:18 +01001794 return &block_class.p->subsys.kobj;
Kay Sievers39aba962010-09-04 22:33:14 -07001795 }
Randy Dunlapead454f2010-09-24 14:36:49 -07001796#endif
Andi Kleene52eec12010-09-08 16:54:17 +02001797
Kay Sievers86406242007-03-14 03:25:56 +01001798 /*
1799 * If we have no parent, we live in "virtual".
Kay Sievers0f4dafc2007-12-19 01:40:42 +01001800 * Class-devices with a non class-device as parent, live
1801 * in a "glue" directory to prevent namespace collisions.
Kay Sievers86406242007-03-14 03:25:56 +01001802 */
1803 if (parent == NULL)
1804 parent_kobj = virtual_device_parent(dev);
Eric W. Biederman24b14422010-07-24 22:43:35 -07001805 else if (parent->class && !dev->class->ns_type)
Kay Sievers86406242007-03-14 03:25:56 +01001806 return &parent->kobj;
1807 else
1808 parent_kobj = &parent->kobj;
1809
Tejun Heo77d3d7c2010-02-05 17:57:02 +09001810 mutex_lock(&gdp_mutex);
1811
Kay Sievers86406242007-03-14 03:25:56 +01001812 /* find our class-directory at the parent and reference it */
Kay Sievers6b6e39a2010-11-15 23:13:18 +01001813 spin_lock(&dev->class->p->glue_dirs.list_lock);
1814 list_for_each_entry(k, &dev->class->p->glue_dirs.list, entry)
Kay Sievers86406242007-03-14 03:25:56 +01001815 if (k->parent == parent_kobj) {
1816 kobj = kobject_get(k);
1817 break;
1818 }
Kay Sievers6b6e39a2010-11-15 23:13:18 +01001819 spin_unlock(&dev->class->p->glue_dirs.list_lock);
Tejun Heo77d3d7c2010-02-05 17:57:02 +09001820 if (kobj) {
1821 mutex_unlock(&gdp_mutex);
Kay Sievers86406242007-03-14 03:25:56 +01001822 return kobj;
Tejun Heo77d3d7c2010-02-05 17:57:02 +09001823 }
Kay Sievers86406242007-03-14 03:25:56 +01001824
1825 /* or create a new class-directory at the parent device */
Eric W. Biedermanbc451f22010-03-30 11:31:25 -07001826 k = class_dir_create_and_add(dev->class, parent_kobj);
Kay Sievers0f4dafc2007-12-19 01:40:42 +01001827 /* do not emit an uevent for this simple "glue" directory */
Tejun Heo77d3d7c2010-02-05 17:57:02 +09001828 mutex_unlock(&gdp_mutex);
Greg Kroah-Hartman43968d22007-11-05 22:24:43 -08001829 return k;
Kay Sievers86406242007-03-14 03:25:56 +01001830 }
1831
Kay Sieversca22e562011-12-14 14:29:38 -08001832 /* subsystems can specify a default root directory for their devices */
1833 if (!parent && dev->bus && dev->bus->dev_root)
1834 return &dev->bus->dev_root->kobj;
1835
Kay Sievers86406242007-03-14 03:25:56 +01001836 if (parent)
Cornelia Huckc744aeae2007-01-08 20:16:44 +01001837 return &parent->kobj;
1838 return NULL;
1839}
Kay Sieversda231fd2007-11-21 17:29:15 +01001840
Ming Leicebf8fd2016-07-10 19:27:36 +08001841static inline bool live_in_glue_dir(struct kobject *kobj,
1842 struct device *dev)
1843{
1844 if (!kobj || !dev->class ||
1845 kobj->kset != &dev->class->p->glue_dirs)
1846 return false;
1847 return true;
1848}
1849
1850static inline struct kobject *get_glue_dir(struct device *dev)
1851{
1852 return dev->kobj.parent;
1853}
1854
1855/*
1856 * make sure cleaning up dir as the last step, we need to make
1857 * sure .release handler of kobject is run with holding the
1858 * global lock
1859 */
Cornelia Huck63b69712008-01-21 16:09:44 +01001860static void cleanup_glue_dir(struct device *dev, struct kobject *glue_dir)
Kay Sieversda231fd2007-11-21 17:29:15 +01001861{
Muchun Songac434322019-07-27 11:21:22 +08001862 unsigned int ref;
1863
Kay Sievers0f4dafc2007-12-19 01:40:42 +01001864 /* see if we live in a "glue" directory */
Ming Leicebf8fd2016-07-10 19:27:36 +08001865 if (!live_in_glue_dir(glue_dir, dev))
Kay Sieversda231fd2007-11-21 17:29:15 +01001866 return;
1867
Yijing Wange4a60d12014-11-07 12:05:49 +08001868 mutex_lock(&gdp_mutex);
Muchun Songac434322019-07-27 11:21:22 +08001869 /**
1870 * There is a race condition between removing glue directory
1871 * and adding a new device under the glue directory.
1872 *
1873 * CPU1: CPU2:
1874 *
1875 * device_add()
1876 * get_device_parent()
1877 * class_dir_create_and_add()
1878 * kobject_add_internal()
1879 * create_dir() // create glue_dir
1880 *
1881 * device_add()
1882 * get_device_parent()
1883 * kobject_get() // get glue_dir
1884 *
1885 * device_del()
1886 * cleanup_glue_dir()
1887 * kobject_del(glue_dir)
1888 *
1889 * kobject_add()
1890 * kobject_add_internal()
1891 * create_dir() // in glue_dir
1892 * sysfs_create_dir_ns()
1893 * kernfs_create_dir_ns(sd)
1894 *
1895 * sysfs_remove_dir() // glue_dir->sd=NULL
1896 * sysfs_put() // free glue_dir->sd
1897 *
1898 * // sd is freed
1899 * kernfs_new_node(sd)
1900 * kernfs_get(glue_dir)
1901 * kernfs_add_one()
1902 * kernfs_put()
1903 *
1904 * Before CPU1 remove last child device under glue dir, if CPU2 add
1905 * a new device under glue dir, the glue_dir kobject reference count
1906 * will be increase to 2 in kobject_get(k). And CPU2 has been called
1907 * kernfs_create_dir_ns(). Meanwhile, CPU1 call sysfs_remove_dir()
1908 * and sysfs_put(). This result in glue_dir->sd is freed.
1909 *
1910 * Then the CPU2 will see a stale "empty" but still potentially used
1911 * glue dir around in kernfs_new_node().
1912 *
1913 * In order to avoid this happening, we also should make sure that
1914 * kernfs_node for glue_dir is released in CPU1 only when refcount
1915 * for glue_dir kobj is 1.
1916 */
1917 ref = kref_read(&glue_dir->kref);
1918 if (!kobject_has_children(glue_dir) && !--ref)
Benjamin Herrenschmidt726e4102018-07-10 10:29:10 +10001919 kobject_del(glue_dir);
Kay Sievers0f4dafc2007-12-19 01:40:42 +01001920 kobject_put(glue_dir);
Yijing Wange4a60d12014-11-07 12:05:49 +08001921 mutex_unlock(&gdp_mutex);
Kay Sieversda231fd2007-11-21 17:29:15 +01001922}
Cornelia Huck63b69712008-01-21 16:09:44 +01001923
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001924static int device_add_class_symlinks(struct device *dev)
1925{
Benjamin Herrenschmidt5590f312015-02-18 11:25:18 +11001926 struct device_node *of_node = dev_of_node(dev);
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001927 int error;
1928
Benjamin Herrenschmidt5590f312015-02-18 11:25:18 +11001929 if (of_node) {
Rob Herring0c3c2342017-10-04 14:04:01 -05001930 error = sysfs_create_link(&dev->kobj, of_node_kobj(of_node), "of_node");
Benjamin Herrenschmidt5590f312015-02-18 11:25:18 +11001931 if (error)
1932 dev_warn(dev, "Error %d creating of_node link\n",error);
1933 /* An error here doesn't warrant bringing down the device */
1934 }
1935
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001936 if (!dev->class)
1937 return 0;
Kay Sieversda231fd2007-11-21 17:29:15 +01001938
Greg Kroah-Hartman1fbfee62008-05-28 09:28:39 -07001939 error = sysfs_create_link(&dev->kobj,
Kay Sievers6b6e39a2010-11-15 23:13:18 +01001940 &dev->class->p->subsys.kobj,
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001941 "subsystem");
1942 if (error)
Benjamin Herrenschmidt5590f312015-02-18 11:25:18 +11001943 goto out_devnode;
Kay Sieversda231fd2007-11-21 17:29:15 +01001944
Greg Kroah-Hartman4e886c22008-01-27 12:12:43 -08001945 if (dev->parent && device_is_not_partition(dev)) {
Dmitry Torokhov4f01a752007-09-18 22:46:50 -07001946 error = sysfs_create_link(&dev->kobj, &dev->parent->kobj,
1947 "device");
1948 if (error)
Kay Sievers39aba962010-09-04 22:33:14 -07001949 goto out_subsys;
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001950 }
Kay Sievers39aba962010-09-04 22:33:14 -07001951
Randy Dunlapead454f2010-09-24 14:36:49 -07001952#ifdef CONFIG_BLOCK
Kay Sievers39aba962010-09-04 22:33:14 -07001953 /* /sys/block has directories and does not need symlinks */
Andi Kleene52eec12010-09-08 16:54:17 +02001954 if (sysfs_deprecated && dev->class == &block_class)
Kay Sievers39aba962010-09-04 22:33:14 -07001955 return 0;
Randy Dunlapead454f2010-09-24 14:36:49 -07001956#endif
Kay Sievers39aba962010-09-04 22:33:14 -07001957
1958 /* link in the class directory pointing to the device */
Kay Sievers6b6e39a2010-11-15 23:13:18 +01001959 error = sysfs_create_link(&dev->class->p->subsys.kobj,
Kay Sievers39aba962010-09-04 22:33:14 -07001960 &dev->kobj, dev_name(dev));
1961 if (error)
1962 goto out_device;
1963
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001964 return 0;
1965
Kay Sievers39aba962010-09-04 22:33:14 -07001966out_device:
1967 sysfs_remove_link(&dev->kobj, "device");
Kay Sieversda231fd2007-11-21 17:29:15 +01001968
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001969out_subsys:
1970 sysfs_remove_link(&dev->kobj, "subsystem");
Benjamin Herrenschmidt5590f312015-02-18 11:25:18 +11001971out_devnode:
1972 sysfs_remove_link(&dev->kobj, "of_node");
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001973 return error;
1974}
1975
1976static void device_remove_class_symlinks(struct device *dev)
1977{
Benjamin Herrenschmidt5590f312015-02-18 11:25:18 +11001978 if (dev_of_node(dev))
1979 sysfs_remove_link(&dev->kobj, "of_node");
1980
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001981 if (!dev->class)
1982 return;
Kay Sieversda231fd2007-11-21 17:29:15 +01001983
Greg Kroah-Hartman4e886c22008-01-27 12:12:43 -08001984 if (dev->parent && device_is_not_partition(dev))
Kay Sieversda231fd2007-11-21 17:29:15 +01001985 sysfs_remove_link(&dev->kobj, "device");
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001986 sysfs_remove_link(&dev->kobj, "subsystem");
Randy Dunlapead454f2010-09-24 14:36:49 -07001987#ifdef CONFIG_BLOCK
Andi Kleene52eec12010-09-08 16:54:17 +02001988 if (sysfs_deprecated && dev->class == &block_class)
Kay Sievers39aba962010-09-04 22:33:14 -07001989 return;
Randy Dunlapead454f2010-09-24 14:36:49 -07001990#endif
Kay Sievers6b6e39a2010-11-15 23:13:18 +01001991 sysfs_delete_link(&dev->class->p->subsys.kobj, &dev->kobj, dev_name(dev));
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001992}
1993
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994/**
Stephen Rothwell413c2392008-05-30 10:16:40 +10001995 * dev_set_name - set a device name
1996 * @dev: device
Randy Dunlap46232362008-06-04 21:40:43 -07001997 * @fmt: format string for the device's name
Stephen Rothwell413c2392008-05-30 10:16:40 +10001998 */
1999int dev_set_name(struct device *dev, const char *fmt, ...)
2000{
2001 va_list vargs;
Kay Sievers1fa5ae82009-01-25 15:17:37 +01002002 int err;
Stephen Rothwell413c2392008-05-30 10:16:40 +10002003
2004 va_start(vargs, fmt);
Kay Sievers1fa5ae82009-01-25 15:17:37 +01002005 err = kobject_set_name_vargs(&dev->kobj, fmt, vargs);
Stephen Rothwell413c2392008-05-30 10:16:40 +10002006 va_end(vargs);
Kay Sievers1fa5ae82009-01-25 15:17:37 +01002007 return err;
Stephen Rothwell413c2392008-05-30 10:16:40 +10002008}
2009EXPORT_SYMBOL_GPL(dev_set_name);
2010
2011/**
Dan Williamse105b8b2008-04-21 10:51:07 -07002012 * device_to_dev_kobj - select a /sys/dev/ directory for the device
2013 * @dev: device
2014 *
2015 * By default we select char/ for new entries. Setting class->dev_obj
2016 * to NULL prevents an entry from being created. class->dev_kobj must
2017 * be set (or cleared) before any devices are registered to the class
2018 * otherwise device_create_sys_dev_entry() and
Peter Korsgaard0d4e293c2012-04-17 12:12:57 +02002019 * device_remove_sys_dev_entry() will disagree about the presence of
2020 * the link.
Dan Williamse105b8b2008-04-21 10:51:07 -07002021 */
2022static struct kobject *device_to_dev_kobj(struct device *dev)
2023{
2024 struct kobject *kobj;
2025
2026 if (dev->class)
2027 kobj = dev->class->dev_kobj;
2028 else
2029 kobj = sysfs_dev_char_kobj;
2030
2031 return kobj;
2032}
2033
2034static int device_create_sys_dev_entry(struct device *dev)
2035{
2036 struct kobject *kobj = device_to_dev_kobj(dev);
2037 int error = 0;
2038 char devt_str[15];
2039
2040 if (kobj) {
2041 format_dev_t(devt_str, dev->devt);
2042 error = sysfs_create_link(kobj, &dev->kobj, devt_str);
2043 }
2044
2045 return error;
2046}
2047
2048static void device_remove_sys_dev_entry(struct device *dev)
2049{
2050 struct kobject *kobj = device_to_dev_kobj(dev);
2051 char devt_str[15];
2052
2053 if (kobj) {
2054 format_dev_t(devt_str, dev->devt);
2055 sysfs_remove_link(kobj, devt_str);
2056 }
2057}
2058
Shaokun Zhang46d3a032018-07-15 18:08:56 +08002059static int device_private_init(struct device *dev)
Greg Kroah-Hartmanb4028432009-05-11 14:16:57 -07002060{
2061 dev->p = kzalloc(sizeof(*dev->p), GFP_KERNEL);
2062 if (!dev->p)
2063 return -ENOMEM;
2064 dev->p->device = dev;
2065 klist_init(&dev->p->klist_children, klist_children_get,
2066 klist_children_put);
Greg Kroah-Hartmanef8a3fd2012-03-08 12:17:22 -08002067 INIT_LIST_HEAD(&dev->p->deferred_probe);
Greg Kroah-Hartmanb4028432009-05-11 14:16:57 -07002068 return 0;
2069}
2070
Dan Williamse105b8b2008-04-21 10:51:07 -07002071/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002072 * device_add - add device to device hierarchy.
2073 * @dev: device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002075 * This is part 2 of device_register(), though may be called
2076 * separately _iff_ device_initialize() has been called separately.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 *
Cornelia Huck57394112008-09-03 18:26:40 +02002078 * This adds @dev to the kobject hierarchy via kobject_add(), adds it
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002079 * to the global and sibling lists for the device, then
2080 * adds it to the other relevant subsystems of the driver model.
Cornelia Huck57394112008-09-03 18:26:40 +02002081 *
Alan Sternb10d5ef2012-01-17 11:39:00 -05002082 * Do not call this routine or device_register() more than once for
2083 * any device structure. The driver model core is not designed to work
2084 * with devices that get unregistered and then spring back to life.
2085 * (Among other things, it's very hard to guarantee that all references
2086 * to the previous incarnation of @dev have been dropped.) Allocate
2087 * and register a fresh new struct device instead.
2088 *
Cornelia Huck57394112008-09-03 18:26:40 +02002089 * NOTE: _Never_ directly free @dev after calling this function, even
2090 * if it returned an error! Always use put_device() to give up your
2091 * reference instead.
Borislav Petkovaffada72019-04-18 19:41:56 +02002092 *
2093 * Rule of thumb is: if device_add() succeeds, you should call
2094 * device_del() when you want to get rid of it. If device_add() has
2095 * *not* succeeded, use *only* put_device() to drop the reference
2096 * count.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 */
2098int device_add(struct device *dev)
2099{
Viresh Kumar35dbf4e2017-03-17 12:24:22 +05302100 struct device *parent;
Kay Sieversca22e562011-12-14 14:29:38 -08002101 struct kobject *kobj;
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02002102 struct class_interface *class_intf;
Greg Kroah-Hartmanc906a482008-05-30 10:45:12 -07002103 int error = -EINVAL;
Ming Leicebf8fd2016-07-10 19:27:36 +08002104 struct kobject *glue_dir = NULL;
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01002105
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 dev = get_device(dev);
Greg Kroah-Hartmanc906a482008-05-30 10:45:12 -07002107 if (!dev)
2108 goto done;
2109
Greg Kroah-Hartmanfb069a52008-12-16 12:23:36 -08002110 if (!dev->p) {
Greg Kroah-Hartmanb4028432009-05-11 14:16:57 -07002111 error = device_private_init(dev);
2112 if (error)
2113 goto done;
Greg Kroah-Hartmanfb069a52008-12-16 12:23:36 -08002114 }
Greg Kroah-Hartmanfb069a52008-12-16 12:23:36 -08002115
Kay Sievers1fa5ae82009-01-25 15:17:37 +01002116 /*
2117 * for statically allocated devices, which should all be converted
2118 * some day, we need to initialize the name. We prevent reading back
2119 * the name, and force the use of dev_name()
2120 */
2121 if (dev->init_name) {
Greg Kroah-Hartmanacc0e902009-06-02 15:39:55 -07002122 dev_set_name(dev, "%s", dev->init_name);
Kay Sievers1fa5ae82009-01-25 15:17:37 +01002123 dev->init_name = NULL;
2124 }
Greg Kroah-Hartmanc906a482008-05-30 10:45:12 -07002125
Kay Sieversca22e562011-12-14 14:29:38 -08002126 /* subsystems can specify simple device enumeration */
2127 if (!dev_name(dev) && dev->bus && dev->bus->dev_name)
2128 dev_set_name(dev, "%s%u", dev->bus->dev_name, dev->id);
2129
Thomas Gleixnere6309e72009-12-10 19:32:49 +00002130 if (!dev_name(dev)) {
2131 error = -EINVAL;
Kay Sievers5c8563d2009-05-28 14:24:07 -07002132 goto name_error;
Thomas Gleixnere6309e72009-12-10 19:32:49 +00002133 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01002135 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
Greg Kroah-Hartmanc205ef42006-08-07 22:19:37 -07002136
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 parent = get_device(dev->parent);
Kay Sieversca22e562011-12-14 14:29:38 -08002138 kobj = get_device_parent(dev, parent);
Tetsuo Handa84d0c272018-05-07 19:10:31 +09002139 if (IS_ERR(kobj)) {
2140 error = PTR_ERR(kobj);
2141 goto parent_error;
2142 }
Kay Sieversca22e562011-12-14 14:29:38 -08002143 if (kobj)
2144 dev->kobj.parent = kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145
Yinghai Lu0d358f22008-02-19 03:20:41 -08002146 /* use parent numa_node */
Zhen Lei56f2de82015-08-25 12:08:22 +08002147 if (parent && (dev_to_node(dev) == NUMA_NO_NODE))
Yinghai Lu0d358f22008-02-19 03:20:41 -08002148 set_dev_node(dev, dev_to_node(parent));
2149
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150 /* first, register with generic layer. */
Kay Sievers8a577ff2009-04-18 15:05:45 -07002151 /* we require the name to be set before, and pass NULL */
2152 error = kobject_add(&dev->kobj, dev->kobj.parent, NULL);
Ming Leicebf8fd2016-07-10 19:27:36 +08002153 if (error) {
2154 glue_dir = get_glue_dir(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 goto Error;
Ming Leicebf8fd2016-07-10 19:27:36 +08002156 }
Kay Sieversa7fd6702005-10-01 14:49:43 +02002157
Brian Walsh37022642006-08-14 22:43:19 -07002158 /* notify platform of device entry */
Heikki Krogerus07de0e82018-11-09 17:21:34 +03002159 error = device_platform_notify(dev, KOBJ_ADD);
2160 if (error)
2161 goto platform_error;
Brian Walsh37022642006-08-14 22:43:19 -07002162
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07002163 error = device_create_file(dev, &dev_attr_uevent);
Cornelia Hucka306eea2006-09-22 11:37:13 +02002164 if (error)
2165 goto attrError;
Kay Sieversa7fd6702005-10-01 14:49:43 +02002166
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07002167 error = device_add_class_symlinks(dev);
2168 if (error)
2169 goto SymlinkError;
Cornelia Huckdc0afa82007-07-09 11:39:18 -07002170 error = device_add_attrs(dev);
2171 if (error)
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07002172 goto AttrsError;
Cornelia Huckdc0afa82007-07-09 11:39:18 -07002173 error = bus_add_device(dev);
2174 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 goto BusError;
Alan Stern3b98aea2008-08-07 13:06:12 -04002176 error = dpm_sysfs_add(dev);
Rafael J. Wysocki57eee3d2008-03-12 00:59:38 +01002177 if (error)
Alan Stern3b98aea2008-08-07 13:06:12 -04002178 goto DPMError;
2179 device_pm_add(dev);
Alan Sternec0676ee2008-12-05 14:10:31 -05002180
Sergey Klyaus0cd75042014-10-08 11:31:54 +04002181 if (MAJOR(dev->devt)) {
2182 error = device_create_file(dev, &dev_attr_dev);
2183 if (error)
2184 goto DevAttrError;
2185
2186 error = device_create_sys_dev_entry(dev);
2187 if (error)
2188 goto SysEntryError;
2189
2190 devtmpfs_create_node(dev);
2191 }
2192
Alan Sternec0676ee2008-12-05 14:10:31 -05002193 /* Notify clients of device addition. This call must come
majianpeng268863f2012-01-11 15:12:06 +00002194 * after dpm_sysfs_add() and before kobject_uevent().
Alan Sternec0676ee2008-12-05 14:10:31 -05002195 */
2196 if (dev->bus)
2197 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
2198 BUS_NOTIFY_ADD_DEVICE, dev);
2199
Cornelia Huck83b5fb4c2007-03-29 11:12:11 +02002200 kobject_uevent(&dev->kobj, KOBJ_ADD);
Alan Stern2023c612009-07-30 15:27:18 -04002201 bus_probe_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 if (parent)
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08002203 klist_add_tail(&dev->p->knode_parent,
2204 &parent->p->klist_children);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205
Greg Kroah-Hartman5d9fd162006-06-22 17:17:32 -07002206 if (dev->class) {
Kay Sieversca22e562011-12-14 14:29:38 -08002207 mutex_lock(&dev->class->p->mutex);
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02002208 /* tie the class to the device */
Wei Yang570d0202019-01-18 10:34:59 +08002209 klist_add_tail(&dev->p->knode_class,
Kay Sievers6b6e39a2010-11-15 23:13:18 +01002210 &dev->class->p->klist_devices);
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02002211
2212 /* notify any interfaces that the device is here */
Greg Kroah-Hartman184f1f72008-05-28 09:28:39 -07002213 list_for_each_entry(class_intf,
Kay Sieversca22e562011-12-14 14:29:38 -08002214 &dev->class->p->interfaces, node)
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02002215 if (class_intf->add_dev)
2216 class_intf->add_dev(dev, class_intf);
Kay Sieversca22e562011-12-14 14:29:38 -08002217 mutex_unlock(&dev->class->p->mutex);
Greg Kroah-Hartman5d9fd162006-06-22 17:17:32 -07002218 }
Greg Kroah-Hartmanc906a482008-05-30 10:45:12 -07002219done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 put_device(dev);
2221 return error;
Sergey Klyaus0cd75042014-10-08 11:31:54 +04002222 SysEntryError:
2223 if (MAJOR(dev->devt))
2224 device_remove_file(dev, &dev_attr_dev);
2225 DevAttrError:
2226 device_pm_remove(dev);
2227 dpm_sysfs_remove(dev);
Alan Stern3b98aea2008-08-07 13:06:12 -04002228 DPMError:
Rafael J. Wysocki57eee3d2008-03-12 00:59:38 +01002229 bus_remove_device(dev);
2230 BusError:
James Simmons82f0cf92007-02-21 17:44:51 +00002231 device_remove_attrs(dev);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07002232 AttrsError:
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07002233 device_remove_class_symlinks(dev);
2234 SymlinkError:
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07002235 device_remove_file(dev, &dev_attr_uevent);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07002236 attrError:
Heikki Krogerus07de0e82018-11-09 17:21:34 +03002237 device_platform_notify(dev, KOBJ_REMOVE);
2238platform_error:
Kay Sievers312c0042005-11-16 09:00:00 +01002239 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
Ming Leicebf8fd2016-07-10 19:27:36 +08002240 glue_dir = get_glue_dir(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 kobject_del(&dev->kobj);
2242 Error:
Ming Leicebf8fd2016-07-10 19:27:36 +08002243 cleanup_glue_dir(dev, glue_dir);
Tetsuo Handa84d0c272018-05-07 19:10:31 +09002244parent_error:
Markus Elfring5f0163a2015-02-05 11:48:26 +01002245 put_device(parent);
Kay Sievers5c8563d2009-05-28 14:24:07 -07002246name_error:
2247 kfree(dev->p);
2248 dev->p = NULL;
Greg Kroah-Hartmanc906a482008-05-30 10:45:12 -07002249 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250}
David Graham White86df2682013-07-21 20:41:14 -04002251EXPORT_SYMBOL_GPL(device_add);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002254 * device_register - register a device with the system.
2255 * @dev: pointer to the device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002257 * This happens in two clean steps - initialize the device
2258 * and add it to the system. The two steps can be called
2259 * separately, but this is the easiest and most common.
2260 * I.e. you should only call the two helpers separately if
2261 * have a clearly defined need to use and refcount the device
2262 * before it is added to the hierarchy.
Cornelia Huck57394112008-09-03 18:26:40 +02002263 *
Alan Sternb10d5ef2012-01-17 11:39:00 -05002264 * For more information, see the kerneldoc for device_initialize()
2265 * and device_add().
2266 *
Cornelia Huck57394112008-09-03 18:26:40 +02002267 * NOTE: _Never_ directly free @dev after calling this function, even
2268 * if it returned an error! Always use put_device() to give up the
2269 * reference initialized in this function instead.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271int device_register(struct device *dev)
2272{
2273 device_initialize(dev);
2274 return device_add(dev);
2275}
David Graham White86df2682013-07-21 20:41:14 -04002276EXPORT_SYMBOL_GPL(device_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002279 * get_device - increment reference count for device.
2280 * @dev: device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002282 * This simply forwards the call to kobject_get(), though
2283 * we do take care to provide for the case that we get a NULL
2284 * pointer passed in.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002286struct device *get_device(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +02002288 return dev ? kobj_to_dev(kobject_get(&dev->kobj)) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289}
David Graham White86df2682013-07-21 20:41:14 -04002290EXPORT_SYMBOL_GPL(get_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002293 * put_device - decrement reference count.
2294 * @dev: device in question.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002296void put_device(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297{
Kay Sieversedfaa7c2007-05-21 22:08:01 +02002298 /* might_sleep(); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299 if (dev)
2300 kobject_put(&dev->kobj);
2301}
David Graham White86df2682013-07-21 20:41:14 -04002302EXPORT_SYMBOL_GPL(put_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303
Dan Williams00289cd2019-07-17 18:07:53 -07002304bool kill_device(struct device *dev)
2305{
2306 /*
2307 * Require the device lock and set the "dead" flag to guarantee that
2308 * the update behavior is consistent with the other bitfields near
2309 * it and that we cannot have an asynchronous probe routine trying
2310 * to run while we are tearing out the bus/class/sysfs from
2311 * underneath the device.
2312 */
2313 lockdep_assert_held(&dev->mutex);
2314
2315 if (dev->p->dead)
2316 return false;
2317 dev->p->dead = true;
2318 return true;
2319}
2320EXPORT_SYMBOL_GPL(kill_device);
2321
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002323 * device_del - delete device from system.
2324 * @dev: device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002326 * This is the first part of the device unregistration
2327 * sequence. This removes the device from the lists we control
2328 * from here, has it removed from the other driver model
2329 * subsystems it was added to in device_add(), and removes it
2330 * from the kobject hierarchy.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002332 * NOTE: this should be called manually _iff_ device_add() was
2333 * also called manually.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002335void device_del(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002337 struct device *parent = dev->parent;
Ming Leicebf8fd2016-07-10 19:27:36 +08002338 struct kobject *glue_dir = NULL;
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02002339 struct class_interface *class_intf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340
Alexander Duyck3451a492019-01-22 10:39:10 -08002341 device_lock(dev);
Dan Williams00289cd2019-07-17 18:07:53 -07002342 kill_device(dev);
Alexander Duyck3451a492019-01-22 10:39:10 -08002343 device_unlock(dev);
2344
Alan Sternec0676ee2008-12-05 14:10:31 -05002345 /* Notify clients of device removal. This call must come
2346 * before dpm_sysfs_remove().
2347 */
2348 if (dev->bus)
2349 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
2350 BUS_NOTIFY_DEL_DEVICE, dev);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01002351
Alan Stern3b98aea2008-08-07 13:06:12 -04002352 dpm_sysfs_remove(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353 if (parent)
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08002354 klist_del(&dev->p->knode_parent);
Dan Williamse105b8b2008-04-21 10:51:07 -07002355 if (MAJOR(dev->devt)) {
Kay Sievers2b2af542009-04-30 15:23:42 +02002356 devtmpfs_delete_node(dev);
Dan Williamse105b8b2008-04-21 10:51:07 -07002357 device_remove_sys_dev_entry(dev);
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07002358 device_remove_file(dev, &dev_attr_dev);
Dan Williamse105b8b2008-04-21 10:51:07 -07002359 }
Kay Sieversb9d9c822006-06-15 15:31:56 +02002360 if (dev->class) {
Kay Sieversda231fd2007-11-21 17:29:15 +01002361 device_remove_class_symlinks(dev);
Kay Sievers99ef3ef2006-09-14 11:23:28 +02002362
Kay Sieversca22e562011-12-14 14:29:38 -08002363 mutex_lock(&dev->class->p->mutex);
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02002364 /* notify any interfaces that the device is now gone */
Greg Kroah-Hartman184f1f72008-05-28 09:28:39 -07002365 list_for_each_entry(class_intf,
Kay Sieversca22e562011-12-14 14:29:38 -08002366 &dev->class->p->interfaces, node)
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02002367 if (class_intf->remove_dev)
2368 class_intf->remove_dev(dev, class_intf);
2369 /* remove the device from the class list */
Wei Yang570d0202019-01-18 10:34:59 +08002370 klist_del(&dev->p->knode_class);
Kay Sieversca22e562011-12-14 14:29:38 -08002371 mutex_unlock(&dev->class->p->mutex);
Kay Sieversb9d9c822006-06-15 15:31:56 +02002372 }
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07002373 device_remove_file(dev, &dev_attr_uevent);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07002374 device_remove_attrs(dev);
Benjamin Herrenschmidt28953532006-11-08 19:46:14 -08002375 bus_remove_device(dev);
LongX Zhang4b6d1f122012-10-25 00:21:28 +02002376 device_pm_remove(dev);
Grant Likelyd1c34142012-03-05 08:47:41 -07002377 driver_deferred_probe_del(dev);
Heikki Krogerus07de0e82018-11-09 17:21:34 +03002378 device_platform_notify(dev, KOBJ_REMOVE);
Lukas Wunner478573c2016-07-28 02:25:41 +02002379 device_remove_properties(dev);
Jeffy Chen2ec16152017-10-20 20:01:01 +08002380 device_links_purge(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381
Joerg Roedel599bad32014-09-30 13:02:02 +02002382 if (dev->bus)
2383 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
2384 BUS_NOTIFY_REMOVED_DEVICE, dev);
Kay Sievers312c0042005-11-16 09:00:00 +01002385 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
Ming Leicebf8fd2016-07-10 19:27:36 +08002386 glue_dir = get_glue_dir(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387 kobject_del(&dev->kobj);
Ming Leicebf8fd2016-07-10 19:27:36 +08002388 cleanup_glue_dir(dev, glue_dir);
Kay Sieversda231fd2007-11-21 17:29:15 +01002389 put_device(parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390}
David Graham White86df2682013-07-21 20:41:14 -04002391EXPORT_SYMBOL_GPL(device_del);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392
2393/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002394 * device_unregister - unregister device from system.
2395 * @dev: device going away.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002397 * We do this in two parts, like we do device_register(). First,
2398 * we remove it from all the subsystems with device_del(), then
2399 * we decrement the reference count via put_device(). If that
2400 * is the final reference count, the device will be cleaned up
2401 * via device_release() above. Otherwise, the structure will
2402 * stick around until the final reference to the device is dropped.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002404void device_unregister(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405{
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01002406 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407 device_del(dev);
2408 put_device(dev);
2409}
David Graham White86df2682013-07-21 20:41:14 -04002410EXPORT_SYMBOL_GPL(device_unregister);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411
Andy Shevchenko3d060ae2015-07-27 18:04:00 +03002412static struct device *prev_device(struct klist_iter *i)
2413{
2414 struct klist_node *n = klist_prev(i);
2415 struct device *dev = NULL;
2416 struct device_private *p;
2417
2418 if (n) {
2419 p = to_device_private_parent(n);
2420 dev = p->device;
2421 }
2422 return dev;
2423}
2424
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002425static struct device *next_device(struct klist_iter *i)
mochel@digitalimplant.org36239572005-03-24 19:08:30 -08002426{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002427 struct klist_node *n = klist_next(i);
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08002428 struct device *dev = NULL;
2429 struct device_private *p;
2430
2431 if (n) {
2432 p = to_device_private_parent(n);
2433 dev = p->device;
2434 }
2435 return dev;
mochel@digitalimplant.org36239572005-03-24 19:08:30 -08002436}
2437
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438/**
Kay Sieverse454cea2009-09-18 23:01:12 +02002439 * device_get_devnode - path of device node file
Kay Sievers6fcf53a2009-04-30 15:23:42 +02002440 * @dev: device
Kay Sieverse454cea2009-09-18 23:01:12 +02002441 * @mode: returned file access mode
Kay Sievers3c2670e2013-04-06 09:56:00 -07002442 * @uid: returned file owner
2443 * @gid: returned file group
Kay Sievers6fcf53a2009-04-30 15:23:42 +02002444 * @tmp: possibly allocated string
2445 *
2446 * Return the relative path of a possible device node.
2447 * Non-default names may need to allocate a memory to compose
2448 * a name. This memory is returned in tmp and needs to be
2449 * freed by the caller.
2450 */
Kay Sieverse454cea2009-09-18 23:01:12 +02002451const char *device_get_devnode(struct device *dev,
Greg Kroah-Hartman4e4098a2013-04-11 11:43:29 -07002452 umode_t *mode, kuid_t *uid, kgid_t *gid,
Kay Sievers3c2670e2013-04-06 09:56:00 -07002453 const char **tmp)
Kay Sievers6fcf53a2009-04-30 15:23:42 +02002454{
2455 char *s;
2456
2457 *tmp = NULL;
2458
2459 /* the device type may provide a specific name */
Kay Sieverse454cea2009-09-18 23:01:12 +02002460 if (dev->type && dev->type->devnode)
Kay Sievers3c2670e2013-04-06 09:56:00 -07002461 *tmp = dev->type->devnode(dev, mode, uid, gid);
Kay Sievers6fcf53a2009-04-30 15:23:42 +02002462 if (*tmp)
2463 return *tmp;
2464
2465 /* the class may provide a specific name */
Kay Sieverse454cea2009-09-18 23:01:12 +02002466 if (dev->class && dev->class->devnode)
2467 *tmp = dev->class->devnode(dev, mode);
Kay Sievers6fcf53a2009-04-30 15:23:42 +02002468 if (*tmp)
2469 return *tmp;
2470
2471 /* return name without allocation, tmp == NULL */
2472 if (strchr(dev_name(dev), '!') == NULL)
2473 return dev_name(dev);
2474
2475 /* replace '!' in the name with '/' */
Rasmus Villemoesa29fd612015-06-25 15:02:33 -07002476 s = kstrdup(dev_name(dev), GFP_KERNEL);
2477 if (!s)
Kay Sievers6fcf53a2009-04-30 15:23:42 +02002478 return NULL;
Rasmus Villemoesa29fd612015-06-25 15:02:33 -07002479 strreplace(s, '!', '/');
2480 return *tmp = s;
Kay Sievers6fcf53a2009-04-30 15:23:42 +02002481}
2482
2483/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002484 * device_for_each_child - device child iterator.
2485 * @parent: parent struct device.
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002486 * @fn: function to be called for each device.
Robert P. J. Dayf8878dc2013-06-01 20:17:34 -04002487 * @data: data for the callback.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002489 * Iterate over @parent's child devices, and call @fn for each,
2490 * passing it @data.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002492 * We check the return of @fn each time. If it returns anything
2493 * other than 0, we break out and return that value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002495int device_for_each_child(struct device *parent, void *data,
2496 int (*fn)(struct device *dev, void *data))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497{
mochel@digitalimplant.org36239572005-03-24 19:08:30 -08002498 struct klist_iter i;
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002499 struct device *child;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500 int error = 0;
2501
Greg Kroah-Hartman014c90db2009-04-15 16:00:12 -07002502 if (!parent->p)
2503 return 0;
2504
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08002505 klist_iter_init(&parent->p->klist_children, &i);
Gimcuan Hui93ead7c2017-11-11 05:52:54 +00002506 while (!error && (child = next_device(&i)))
mochel@digitalimplant.org36239572005-03-24 19:08:30 -08002507 error = fn(child, data);
2508 klist_iter_exit(&i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509 return error;
2510}
David Graham White86df2682013-07-21 20:41:14 -04002511EXPORT_SYMBOL_GPL(device_for_each_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512
Cornelia Huck5ab69982006-11-16 15:42:07 +01002513/**
Andy Shevchenko3d060ae2015-07-27 18:04:00 +03002514 * device_for_each_child_reverse - device child iterator in reversed order.
2515 * @parent: parent struct device.
2516 * @fn: function to be called for each device.
2517 * @data: data for the callback.
2518 *
2519 * Iterate over @parent's child devices, and call @fn for each,
2520 * passing it @data.
2521 *
2522 * We check the return of @fn each time. If it returns anything
2523 * other than 0, we break out and return that value.
2524 */
2525int device_for_each_child_reverse(struct device *parent, void *data,
2526 int (*fn)(struct device *dev, void *data))
2527{
2528 struct klist_iter i;
2529 struct device *child;
2530 int error = 0;
2531
2532 if (!parent->p)
2533 return 0;
2534
2535 klist_iter_init(&parent->p->klist_children, &i);
2536 while ((child = prev_device(&i)) && !error)
2537 error = fn(child, data);
2538 klist_iter_exit(&i);
2539 return error;
2540}
2541EXPORT_SYMBOL_GPL(device_for_each_child_reverse);
2542
2543/**
Cornelia Huck5ab69982006-11-16 15:42:07 +01002544 * device_find_child - device iterator for locating a particular device.
2545 * @parent: parent struct device
Cornelia Huck5ab69982006-11-16 15:42:07 +01002546 * @match: Callback function to check device
Robert P. J. Dayf8878dc2013-06-01 20:17:34 -04002547 * @data: Data to pass to match function
Cornelia Huck5ab69982006-11-16 15:42:07 +01002548 *
2549 * This is similar to the device_for_each_child() function above, but it
2550 * returns a reference to a device that is 'found' for later use, as
2551 * determined by the @match callback.
2552 *
2553 * The callback should return 0 if the device doesn't match and non-zero
2554 * if it does. If the callback returns non-zero and a reference to the
2555 * current device can be obtained, this function will return to the caller
2556 * and not iterate over any more devices.
Federico Vagaa4e24002013-04-15 11:18:11 +02002557 *
2558 * NOTE: you will need to drop the reference with put_device() after use.
Cornelia Huck5ab69982006-11-16 15:42:07 +01002559 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002560struct device *device_find_child(struct device *parent, void *data,
2561 int (*match)(struct device *dev, void *data))
Cornelia Huck5ab69982006-11-16 15:42:07 +01002562{
2563 struct klist_iter i;
2564 struct device *child;
2565
2566 if (!parent)
2567 return NULL;
2568
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08002569 klist_iter_init(&parent->p->klist_children, &i);
Cornelia Huck5ab69982006-11-16 15:42:07 +01002570 while ((child = next_device(&i)))
2571 if (match(child, data) && get_device(child))
2572 break;
2573 klist_iter_exit(&i);
2574 return child;
2575}
David Graham White86df2682013-07-21 20:41:14 -04002576EXPORT_SYMBOL_GPL(device_find_child);
Cornelia Huck5ab69982006-11-16 15:42:07 +01002577
Heikki Krogerusdad9bb02019-05-31 17:15:37 +03002578/**
2579 * device_find_child_by_name - device iterator for locating a child device.
2580 * @parent: parent struct device
2581 * @name: name of the child device
2582 *
2583 * This is similar to the device_find_child() function above, but it
2584 * returns a reference to a device that has the name @name.
2585 *
2586 * NOTE: you will need to drop the reference with put_device() after use.
2587 */
2588struct device *device_find_child_by_name(struct device *parent,
2589 const char *name)
2590{
2591 struct klist_iter i;
2592 struct device *child;
2593
2594 if (!parent)
2595 return NULL;
2596
2597 klist_iter_init(&parent->p->klist_children, &i);
2598 while ((child = next_device(&i)))
2599 if (!strcmp(dev_name(child), name) && get_device(child))
2600 break;
2601 klist_iter_exit(&i);
2602 return child;
2603}
2604EXPORT_SYMBOL_GPL(device_find_child_by_name);
2605
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606int __init devices_init(void)
2607{
Greg Kroah-Hartman881c6cfd2007-11-01 09:29:06 -06002608 devices_kset = kset_create_and_add("devices", &device_uevent_ops, NULL);
2609 if (!devices_kset)
2610 return -ENOMEM;
Dan Williamse105b8b2008-04-21 10:51:07 -07002611 dev_kobj = kobject_create_and_add("dev", NULL);
2612 if (!dev_kobj)
2613 goto dev_kobj_err;
2614 sysfs_dev_block_kobj = kobject_create_and_add("block", dev_kobj);
2615 if (!sysfs_dev_block_kobj)
2616 goto block_kobj_err;
2617 sysfs_dev_char_kobj = kobject_create_and_add("char", dev_kobj);
2618 if (!sysfs_dev_char_kobj)
2619 goto char_kobj_err;
2620
Greg Kroah-Hartman881c6cfd2007-11-01 09:29:06 -06002621 return 0;
Dan Williamse105b8b2008-04-21 10:51:07 -07002622
2623 char_kobj_err:
2624 kobject_put(sysfs_dev_block_kobj);
2625 block_kobj_err:
2626 kobject_put(dev_kobj);
2627 dev_kobj_err:
2628 kset_unregister(devices_kset);
2629 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630}
2631
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02002632static int device_check_offline(struct device *dev, void *not_used)
2633{
2634 int ret;
2635
2636 ret = device_for_each_child(dev, NULL, device_check_offline);
2637 if (ret)
2638 return ret;
2639
2640 return device_supports_offline(dev) && !dev->offline ? -EBUSY : 0;
2641}
2642
2643/**
2644 * device_offline - Prepare the device for hot-removal.
2645 * @dev: Device to be put offline.
2646 *
2647 * Execute the device bus type's .offline() callback, if present, to prepare
2648 * the device for a subsequent hot-removal. If that succeeds, the device must
2649 * not be used until either it is removed or its bus type's .online() callback
2650 * is executed.
2651 *
2652 * Call under device_hotplug_lock.
2653 */
2654int device_offline(struct device *dev)
2655{
2656 int ret;
2657
2658 if (dev->offline_disabled)
2659 return -EPERM;
2660
2661 ret = device_for_each_child(dev, NULL, device_check_offline);
2662 if (ret)
2663 return ret;
2664
2665 device_lock(dev);
2666 if (device_supports_offline(dev)) {
2667 if (dev->offline) {
2668 ret = 1;
2669 } else {
2670 ret = dev->bus->offline(dev);
2671 if (!ret) {
2672 kobject_uevent(&dev->kobj, KOBJ_OFFLINE);
2673 dev->offline = true;
2674 }
2675 }
2676 }
2677 device_unlock(dev);
2678
2679 return ret;
2680}
2681
2682/**
2683 * device_online - Put the device back online after successful device_offline().
2684 * @dev: Device to be put back online.
2685 *
2686 * If device_offline() has been successfully executed for @dev, but the device
2687 * has not been removed subsequently, execute its bus type's .online() callback
2688 * to indicate that the device can be used again.
2689 *
2690 * Call under device_hotplug_lock.
2691 */
2692int device_online(struct device *dev)
2693{
2694 int ret = 0;
2695
2696 device_lock(dev);
2697 if (device_supports_offline(dev)) {
2698 if (dev->offline) {
2699 ret = dev->bus->online(dev);
2700 if (!ret) {
2701 kobject_uevent(&dev->kobj, KOBJ_ONLINE);
2702 dev->offline = false;
2703 }
2704 } else {
2705 ret = 1;
2706 }
2707 }
2708 device_unlock(dev);
2709
2710 return ret;
2711}
2712
Karthigan Srinivasan7f100d12011-04-18 16:16:52 -05002713struct root_device {
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00002714 struct device dev;
2715 struct module *owner;
2716};
2717
Josh Triplett93058422012-11-18 21:27:55 -08002718static inline struct root_device *to_root_device(struct device *d)
Ferenc Wagner481e2072011-01-07 15:17:47 +01002719{
2720 return container_of(d, struct root_device, dev);
2721}
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00002722
2723static void root_device_release(struct device *dev)
2724{
2725 kfree(to_root_device(dev));
2726}
2727
2728/**
2729 * __root_device_register - allocate and register a root device
2730 * @name: root device name
2731 * @owner: owner module of the root device, usually THIS_MODULE
2732 *
2733 * This function allocates a root device and registers it
2734 * using device_register(). In order to free the returned
2735 * device, use root_device_unregister().
2736 *
2737 * Root devices are dummy devices which allow other devices
2738 * to be grouped under /sys/devices. Use this function to
2739 * allocate a root device and then use it as the parent of
2740 * any device which should appear under /sys/devices/{name}
2741 *
2742 * The /sys/devices/{name} directory will also contain a
2743 * 'module' symlink which points to the @owner directory
2744 * in sysfs.
2745 *
Jani Nikulaf0eae0e2010-03-11 18:11:45 +02002746 * Returns &struct device pointer on success, or ERR_PTR() on error.
2747 *
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00002748 * Note: You probably want to use root_device_register().
2749 */
2750struct device *__root_device_register(const char *name, struct module *owner)
2751{
2752 struct root_device *root;
2753 int err = -ENOMEM;
2754
2755 root = kzalloc(sizeof(struct root_device), GFP_KERNEL);
2756 if (!root)
2757 return ERR_PTR(err);
2758
Greg Kroah-Hartmanacc0e902009-06-02 15:39:55 -07002759 err = dev_set_name(&root->dev, "%s", name);
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00002760 if (err) {
2761 kfree(root);
2762 return ERR_PTR(err);
2763 }
2764
2765 root->dev.release = root_device_release;
2766
2767 err = device_register(&root->dev);
2768 if (err) {
2769 put_device(&root->dev);
2770 return ERR_PTR(err);
2771 }
2772
Christoph Egger1d9e8822010-05-17 16:57:58 +02002773#ifdef CONFIG_MODULES /* gotta find a "cleaner" way to do this */
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00002774 if (owner) {
2775 struct module_kobject *mk = &owner->mkobj;
2776
2777 err = sysfs_create_link(&root->dev.kobj, &mk->kobj, "module");
2778 if (err) {
2779 device_unregister(&root->dev);
2780 return ERR_PTR(err);
2781 }
2782 root->owner = owner;
2783 }
2784#endif
2785
2786 return &root->dev;
2787}
2788EXPORT_SYMBOL_GPL(__root_device_register);
2789
2790/**
2791 * root_device_unregister - unregister and free a root device
Randy Dunlap7cbcf222009-01-20 16:29:13 -08002792 * @dev: device going away
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00002793 *
2794 * This function unregisters and cleans up a device that was created by
2795 * root_device_register().
2796 */
2797void root_device_unregister(struct device *dev)
2798{
2799 struct root_device *root = to_root_device(dev);
2800
2801 if (root->owner)
2802 sysfs_remove_link(&root->dev.kobj, "module");
2803
2804 device_unregister(dev);
2805}
2806EXPORT_SYMBOL_GPL(root_device_unregister);
2807
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07002808
2809static void device_create_release(struct device *dev)
2810{
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01002811 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07002812 kfree(dev);
2813}
2814
Mathieu Malaterre6a8b55d2018-05-05 21:57:41 +02002815static __printf(6, 0) struct device *
Guenter Roeck39ef3112013-07-14 16:05:57 -07002816device_create_groups_vargs(struct class *class, struct device *parent,
2817 dev_t devt, void *drvdata,
2818 const struct attribute_group **groups,
2819 const char *fmt, va_list args)
2820{
2821 struct device *dev = NULL;
2822 int retval = -ENODEV;
2823
2824 if (class == NULL || IS_ERR(class))
2825 goto error;
2826
2827 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
2828 if (!dev) {
2829 retval = -ENOMEM;
2830 goto error;
2831 }
2832
David Herrmannbbc780f2013-11-21 20:15:48 +01002833 device_initialize(dev);
Guenter Roeck39ef3112013-07-14 16:05:57 -07002834 dev->devt = devt;
2835 dev->class = class;
2836 dev->parent = parent;
2837 dev->groups = groups;
2838 dev->release = device_create_release;
2839 dev_set_drvdata(dev, drvdata);
2840
2841 retval = kobject_set_name_vargs(&dev->kobj, fmt, args);
2842 if (retval)
2843 goto error;
2844
David Herrmannbbc780f2013-11-21 20:15:48 +01002845 retval = device_add(dev);
Guenter Roeck39ef3112013-07-14 16:05:57 -07002846 if (retval)
2847 goto error;
2848
2849 return dev;
2850
2851error:
2852 put_device(dev);
2853 return ERR_PTR(retval);
2854}
2855
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07002856/**
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07002857 * device_create_vargs - creates a device and registers it with sysfs
2858 * @class: pointer to the struct class that this device should be registered to
2859 * @parent: pointer to the parent struct device of this new device, if any
2860 * @devt: the dev_t for the char device to be added
2861 * @drvdata: the data to be added to the device for callbacks
2862 * @fmt: string for the device's name
2863 * @args: va_list for the device's name
2864 *
2865 * This function can be used by char device classes. A struct device
2866 * will be created in sysfs, registered to the specified class.
2867 *
2868 * A "dev" file will be created, showing the dev_t for the device, if
2869 * the dev_t is not 0,0.
2870 * If a pointer to a parent struct device is passed in, the newly created
2871 * struct device will be a child of that device in sysfs.
2872 * The pointer to the struct device will be returned from the call.
2873 * Any further sysfs files that might be required can be created using this
2874 * pointer.
2875 *
Jani Nikulaf0eae0e2010-03-11 18:11:45 +02002876 * Returns &struct device pointer on success, or ERR_PTR() on error.
2877 *
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07002878 * Note: the struct class passed to this function must have previously
2879 * been created with a call to class_create().
2880 */
2881struct device *device_create_vargs(struct class *class, struct device *parent,
2882 dev_t devt, void *drvdata, const char *fmt,
2883 va_list args)
2884{
Guenter Roeck39ef3112013-07-14 16:05:57 -07002885 return device_create_groups_vargs(class, parent, devt, drvdata, NULL,
2886 fmt, args);
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07002887}
2888EXPORT_SYMBOL_GPL(device_create_vargs);
2889
2890/**
Greg Kroah-Hartman4e106732008-07-21 20:03:34 -07002891 * device_create - creates a device and registers it with sysfs
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07002892 * @class: pointer to the struct class that this device should be registered to
2893 * @parent: pointer to the parent struct device of this new device, if any
2894 * @devt: the dev_t for the char device to be added
2895 * @drvdata: the data to be added to the device for callbacks
2896 * @fmt: string for the device's name
2897 *
2898 * This function can be used by char device classes. A struct device
2899 * will be created in sysfs, registered to the specified class.
2900 *
2901 * A "dev" file will be created, showing the dev_t for the device, if
2902 * the dev_t is not 0,0.
2903 * If a pointer to a parent struct device is passed in, the newly created
2904 * struct device will be a child of that device in sysfs.
2905 * The pointer to the struct device will be returned from the call.
2906 * Any further sysfs files that might be required can be created using this
2907 * pointer.
2908 *
Jani Nikulaf0eae0e2010-03-11 18:11:45 +02002909 * Returns &struct device pointer on success, or ERR_PTR() on error.
2910 *
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07002911 * Note: the struct class passed to this function must have previously
2912 * been created with a call to class_create().
2913 */
Greg Kroah-Hartman4e106732008-07-21 20:03:34 -07002914struct device *device_create(struct class *class, struct device *parent,
2915 dev_t devt, void *drvdata, const char *fmt, ...)
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07002916{
2917 va_list vargs;
2918 struct device *dev;
2919
2920 va_start(vargs, fmt);
2921 dev = device_create_vargs(class, parent, devt, drvdata, fmt, vargs);
2922 va_end(vargs);
2923 return dev;
2924}
Greg Kroah-Hartman4e106732008-07-21 20:03:34 -07002925EXPORT_SYMBOL_GPL(device_create);
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07002926
Guenter Roeck39ef3112013-07-14 16:05:57 -07002927/**
2928 * device_create_with_groups - creates a device and registers it with sysfs
2929 * @class: pointer to the struct class that this device should be registered to
2930 * @parent: pointer to the parent struct device of this new device, if any
2931 * @devt: the dev_t for the char device to be added
2932 * @drvdata: the data to be added to the device for callbacks
2933 * @groups: NULL-terminated list of attribute groups to be created
2934 * @fmt: string for the device's name
2935 *
2936 * This function can be used by char device classes. A struct device
2937 * will be created in sysfs, registered to the specified class.
2938 * Additional attributes specified in the groups parameter will also
2939 * be created automatically.
2940 *
2941 * A "dev" file will be created, showing the dev_t for the device, if
2942 * the dev_t is not 0,0.
2943 * If a pointer to a parent struct device is passed in, the newly created
2944 * struct device will be a child of that device in sysfs.
2945 * The pointer to the struct device will be returned from the call.
2946 * Any further sysfs files that might be required can be created using this
2947 * pointer.
2948 *
2949 * Returns &struct device pointer on success, or ERR_PTR() on error.
2950 *
2951 * Note: the struct class passed to this function must have previously
2952 * been created with a call to class_create().
2953 */
2954struct device *device_create_with_groups(struct class *class,
2955 struct device *parent, dev_t devt,
2956 void *drvdata,
2957 const struct attribute_group **groups,
2958 const char *fmt, ...)
2959{
2960 va_list vargs;
2961 struct device *dev;
2962
2963 va_start(vargs, fmt);
2964 dev = device_create_groups_vargs(class, parent, devt, drvdata, groups,
2965 fmt, vargs);
2966 va_end(vargs);
2967 return dev;
2968}
2969EXPORT_SYMBOL_GPL(device_create_with_groups);
2970
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01002971/**
2972 * device_destroy - removes a device that was created with device_create()
2973 * @class: pointer to the struct class that this device was registered with
2974 * @devt: the dev_t of the device that was previously registered
2975 *
2976 * This call unregisters and cleans up a device that was created with a
2977 * call to device_create().
2978 */
2979void device_destroy(struct class *class, dev_t devt)
2980{
2981 struct device *dev;
2982
Suzuki K Poulose4495dfd2019-07-23 23:18:35 +01002983 dev = class_find_device_by_devt(class, devt);
Dave Youngcd354492008-01-28 16:56:11 +08002984 if (dev) {
2985 put_device(dev);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07002986 device_unregister(dev);
Dave Youngcd354492008-01-28 16:56:11 +08002987 }
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07002988}
2989EXPORT_SYMBOL_GPL(device_destroy);
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07002990
2991/**
2992 * device_rename - renames a device
2993 * @dev: the pointer to the struct device to be renamed
2994 * @new_name: the new name of the device
Eric W. Biederman030c1d22008-05-08 14:41:00 -07002995 *
2996 * It is the responsibility of the caller to provide mutual
2997 * exclusion between two different calls of device_rename
2998 * on the same device to ensure that new_name is valid and
2999 * won't conflict with other devices.
Michael Ellermanc6c0ac62010-11-25 09:44:07 +11003000 *
Timur Tabia5462512010-12-13 14:08:52 -06003001 * Note: Don't call this function. Currently, the networking layer calls this
3002 * function, but that will change. The following text from Kay Sievers offers
3003 * some insight:
3004 *
3005 * Renaming devices is racy at many levels, symlinks and other stuff are not
3006 * replaced atomically, and you get a "move" uevent, but it's not easy to
3007 * connect the event to the old and new device. Device nodes are not renamed at
3008 * all, there isn't even support for that in the kernel now.
3009 *
3010 * In the meantime, during renaming, your target name might be taken by another
3011 * driver, creating conflicts. Or the old name is taken directly after you
3012 * renamed it -- then you get events for the same DEVPATH, before you even see
3013 * the "move" event. It's just a mess, and nothing new should ever rely on
3014 * kernel device renaming. Besides that, it's not even implemented now for
3015 * other things than (driver-core wise very simple) network devices.
3016 *
3017 * We are currently about to change network renaming in udev to completely
3018 * disallow renaming of devices in the same namespace as the kernel uses,
3019 * because we can't solve the problems properly, that arise with swapping names
3020 * of multiple interfaces without races. Means, renaming of eth[0-9]* will only
3021 * be allowed to some other name than eth[0-9]*, for the aforementioned
3022 * reasons.
3023 *
3024 * Make up a "real" name in the driver before you register anything, or add
3025 * some other attributes for userspace to find the device, or use udev to add
3026 * symlinks -- but never rename kernel devices later, it's a complete mess. We
3027 * don't even want to get into that and try to implement the missing pieces in
3028 * the core. We really have other pieces to fix in the driver core mess. :)
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07003029 */
Johannes Berg6937e8f2010-08-05 17:38:18 +02003030int device_rename(struct device *dev, const char *new_name)
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07003031{
Tejun Heo4b30ee52013-09-11 22:29:06 -04003032 struct kobject *kobj = &dev->kobj;
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07003033 char *old_device_name = NULL;
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07003034 int error;
3035
3036 dev = get_device(dev);
3037 if (!dev)
3038 return -EINVAL;
3039
ethan.zhao69df7532013-10-13 22:12:35 +08003040 dev_dbg(dev, "renaming to %s\n", new_name);
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07003041
Kay Sievers1fa5ae82009-01-25 15:17:37 +01003042 old_device_name = kstrdup(dev_name(dev), GFP_KERNEL);
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07003043 if (!old_device_name) {
3044 error = -ENOMEM;
3045 goto out;
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07003046 }
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07003047
Eric W. Biedermanf349cf32010-03-30 11:31:29 -07003048 if (dev->class) {
Tejun Heo4b30ee52013-09-11 22:29:06 -04003049 error = sysfs_rename_link_ns(&dev->class->p->subsys.kobj,
3050 kobj, old_device_name,
3051 new_name, kobject_namespace(kobj));
Eric W. Biedermanf349cf32010-03-30 11:31:29 -07003052 if (error)
3053 goto out;
3054 }
Kay Sievers39aba962010-09-04 22:33:14 -07003055
Tejun Heo4b30ee52013-09-11 22:29:06 -04003056 error = kobject_rename(kobj, new_name);
Kay Sievers1fa5ae82009-01-25 15:17:37 +01003057 if (error)
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07003058 goto out;
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07003059
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07003060out:
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07003061 put_device(dev);
3062
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07003063 kfree(old_device_name);
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07003064
3065 return error;
3066}
Johannes Berga2807db2007-02-28 12:38:31 +01003067EXPORT_SYMBOL_GPL(device_rename);
Cornelia Huck8a824722006-11-20 17:07:51 +01003068
3069static int device_move_class_links(struct device *dev,
3070 struct device *old_parent,
3071 struct device *new_parent)
3072{
Greg Kroah-Hartmanf7f34612007-03-06 12:55:53 -08003073 int error = 0;
Cornelia Huck8a824722006-11-20 17:07:51 +01003074
Greg Kroah-Hartmanf7f34612007-03-06 12:55:53 -08003075 if (old_parent)
3076 sysfs_remove_link(&dev->kobj, "device");
3077 if (new_parent)
3078 error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
3079 "device");
3080 return error;
Cornelia Huck8a824722006-11-20 17:07:51 +01003081}
3082
3083/**
3084 * device_move - moves a device to a new parent
3085 * @dev: the pointer to the struct device to be moved
Wolfram Sang13509862018-05-06 13:23:47 +02003086 * @new_parent: the new parent of the device (can be NULL)
Cornelia Huckffa6a702009-03-04 12:44:00 +01003087 * @dpm_order: how to reorder the dpm_list
Cornelia Huck8a824722006-11-20 17:07:51 +01003088 */
Cornelia Huckffa6a702009-03-04 12:44:00 +01003089int device_move(struct device *dev, struct device *new_parent,
3090 enum dpm_order dpm_order)
Cornelia Huck8a824722006-11-20 17:07:51 +01003091{
3092 int error;
3093 struct device *old_parent;
Cornelia Huckc744aeae2007-01-08 20:16:44 +01003094 struct kobject *new_parent_kobj;
Cornelia Huck8a824722006-11-20 17:07:51 +01003095
3096 dev = get_device(dev);
3097 if (!dev)
3098 return -EINVAL;
3099
Cornelia Huckffa6a702009-03-04 12:44:00 +01003100 device_pm_lock();
Cornelia Huck8a824722006-11-20 17:07:51 +01003101 new_parent = get_device(new_parent);
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08003102 new_parent_kobj = get_device_parent(dev, new_parent);
Tetsuo Handa84d0c272018-05-07 19:10:31 +09003103 if (IS_ERR(new_parent_kobj)) {
3104 error = PTR_ERR(new_parent_kobj);
3105 put_device(new_parent);
3106 goto out;
3107 }
Cornelia Huck63b69712008-01-21 16:09:44 +01003108
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01003109 pr_debug("device: '%s': %s: moving to '%s'\n", dev_name(dev),
3110 __func__, new_parent ? dev_name(new_parent) : "<NULL>");
Cornelia Huckc744aeae2007-01-08 20:16:44 +01003111 error = kobject_move(&dev->kobj, new_parent_kobj);
Cornelia Huck8a824722006-11-20 17:07:51 +01003112 if (error) {
Cornelia Huck63b69712008-01-21 16:09:44 +01003113 cleanup_glue_dir(dev, new_parent_kobj);
Cornelia Huck8a824722006-11-20 17:07:51 +01003114 put_device(new_parent);
3115 goto out;
3116 }
3117 old_parent = dev->parent;
3118 dev->parent = new_parent;
3119 if (old_parent)
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08003120 klist_remove(&dev->p->knode_parent);
Yinghai Lu0d358f22008-02-19 03:20:41 -08003121 if (new_parent) {
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08003122 klist_add_tail(&dev->p->knode_parent,
3123 &new_parent->p->klist_children);
Yinghai Lu0d358f22008-02-19 03:20:41 -08003124 set_dev_node(dev, dev_to_node(new_parent));
3125 }
3126
Rabin Vincentbdd40342012-04-23 09:16:36 +02003127 if (dev->class) {
3128 error = device_move_class_links(dev, old_parent, new_parent);
3129 if (error) {
3130 /* We ignore errors on cleanup since we're hosed anyway... */
3131 device_move_class_links(dev, new_parent, old_parent);
3132 if (!kobject_move(&dev->kobj, &old_parent->kobj)) {
3133 if (new_parent)
3134 klist_remove(&dev->p->knode_parent);
3135 dev->parent = old_parent;
3136 if (old_parent) {
3137 klist_add_tail(&dev->p->knode_parent,
3138 &old_parent->p->klist_children);
3139 set_dev_node(dev, dev_to_node(old_parent));
3140 }
Yinghai Lu0d358f22008-02-19 03:20:41 -08003141 }
Rabin Vincentbdd40342012-04-23 09:16:36 +02003142 cleanup_glue_dir(dev, new_parent_kobj);
3143 put_device(new_parent);
3144 goto out;
Cornelia Huck8a824722006-11-20 17:07:51 +01003145 }
Cornelia Huck8a824722006-11-20 17:07:51 +01003146 }
Cornelia Huckffa6a702009-03-04 12:44:00 +01003147 switch (dpm_order) {
3148 case DPM_ORDER_NONE:
3149 break;
3150 case DPM_ORDER_DEV_AFTER_PARENT:
3151 device_pm_move_after(dev, new_parent);
Grygorii Strashko52cdbdd2015-07-27 20:43:01 +03003152 devices_kset_move_after(dev, new_parent);
Cornelia Huckffa6a702009-03-04 12:44:00 +01003153 break;
3154 case DPM_ORDER_PARENT_BEFORE_DEV:
3155 device_pm_move_before(new_parent, dev);
Grygorii Strashko52cdbdd2015-07-27 20:43:01 +03003156 devices_kset_move_before(new_parent, dev);
Cornelia Huckffa6a702009-03-04 12:44:00 +01003157 break;
3158 case DPM_ORDER_DEV_LAST:
3159 device_pm_move_last(dev);
Grygorii Strashko52cdbdd2015-07-27 20:43:01 +03003160 devices_kset_move_last(dev);
Cornelia Huckffa6a702009-03-04 12:44:00 +01003161 break;
3162 }
Rabin Vincentbdd40342012-04-23 09:16:36 +02003163
Cornelia Huck8a824722006-11-20 17:07:51 +01003164 put_device(old_parent);
3165out:
Cornelia Huckffa6a702009-03-04 12:44:00 +01003166 device_pm_unlock();
Cornelia Huck8a824722006-11-20 17:07:51 +01003167 put_device(dev);
3168 return error;
3169}
Cornelia Huck8a824722006-11-20 17:07:51 +01003170EXPORT_SYMBOL_GPL(device_move);
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08003171
3172/**
3173 * device_shutdown - call ->shutdown() on each device to shutdown.
3174 */
3175void device_shutdown(void)
3176{
Benson Leungf123db82013-09-24 20:05:11 -07003177 struct device *dev, *parent;
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08003178
Pingfan Liu3297c8f2018-07-19 13:14:58 +08003179 wait_for_device_probe();
3180 device_block_probing();
3181
Hugh Daschbach62458382010-03-22 10:36:37 -07003182 spin_lock(&devices_kset->list_lock);
3183 /*
3184 * Walk the devices list backward, shutting down each in turn.
3185 * Beware that device unplug events may also start pulling
3186 * devices offline, even as the system is shutting down.
3187 */
3188 while (!list_empty(&devices_kset->list)) {
3189 dev = list_entry(devices_kset->list.prev, struct device,
3190 kobj.entry);
Ming Leid1c6c032012-06-22 18:01:40 +08003191
3192 /*
3193 * hold reference count of device's parent to
3194 * prevent it from being freed because parent's
3195 * lock is to be held
3196 */
Benson Leungf123db82013-09-24 20:05:11 -07003197 parent = get_device(dev->parent);
Hugh Daschbach62458382010-03-22 10:36:37 -07003198 get_device(dev);
3199 /*
3200 * Make sure the device is off the kset list, in the
3201 * event that dev->*->shutdown() doesn't remove it.
3202 */
3203 list_del_init(&dev->kobj.entry);
3204 spin_unlock(&devices_kset->list_lock);
Alan Sternfe6b91f2011-12-06 23:24:52 +01003205
Ming Leid1c6c032012-06-22 18:01:40 +08003206 /* hold lock to avoid race with probe/release */
Benson Leungf123db82013-09-24 20:05:11 -07003207 if (parent)
3208 device_lock(parent);
Ming Leid1c6c032012-06-22 18:01:40 +08003209 device_lock(dev);
3210
Alan Sternfe6b91f2011-12-06 23:24:52 +01003211 /* Don't allow any more runtime suspends */
3212 pm_runtime_get_noresume(dev);
3213 pm_runtime_barrier(dev);
Hugh Daschbach62458382010-03-22 10:36:37 -07003214
Michal Suchanek75216212017-08-11 15:44:43 +02003215 if (dev->class && dev->class->shutdown_pre) {
Josh Zimmermanf77af152017-06-25 14:53:23 -07003216 if (initcall_debug)
Michal Suchanek75216212017-08-11 15:44:43 +02003217 dev_info(dev, "shutdown_pre\n");
3218 dev->class->shutdown_pre(dev);
3219 }
3220 if (dev->bus && dev->bus->shutdown) {
ShuoX Liu0246c4f2012-11-23 15:14:12 +08003221 if (initcall_debug)
3222 dev_info(dev, "shutdown\n");
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08003223 dev->bus->shutdown(dev);
3224 } else if (dev->driver && dev->driver->shutdown) {
ShuoX Liu0246c4f2012-11-23 15:14:12 +08003225 if (initcall_debug)
3226 dev_info(dev, "shutdown\n");
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08003227 dev->driver->shutdown(dev);
3228 }
Ming Leid1c6c032012-06-22 18:01:40 +08003229
3230 device_unlock(dev);
Benson Leungf123db82013-09-24 20:05:11 -07003231 if (parent)
3232 device_unlock(parent);
Ming Leid1c6c032012-06-22 18:01:40 +08003233
Hugh Daschbach62458382010-03-22 10:36:37 -07003234 put_device(dev);
Benson Leungf123db82013-09-24 20:05:11 -07003235 put_device(parent);
Hugh Daschbach62458382010-03-22 10:36:37 -07003236
3237 spin_lock(&devices_kset->list_lock);
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08003238 }
Hugh Daschbach62458382010-03-22 10:36:37 -07003239 spin_unlock(&devices_kset->list_lock);
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08003240}
Joe Perches99bcf212010-06-27 01:02:34 +00003241
3242/*
3243 * Device logging functions
3244 */
3245
3246#ifdef CONFIG_PRINTK
Joe Perches666f3552012-09-12 20:14:11 -07003247static int
3248create_syslog_header(const struct device *dev, char *hdr, size_t hdrlen)
Joe Perches99bcf212010-06-27 01:02:34 +00003249{
Kay Sieversc4e00da2012-05-03 02:29:59 +02003250 const char *subsys;
Joe Perches798efc62012-09-12 20:11:29 -07003251 size_t pos = 0;
Joe Perches99bcf212010-06-27 01:02:34 +00003252
Kay Sieversc4e00da2012-05-03 02:29:59 +02003253 if (dev->class)
3254 subsys = dev->class->name;
3255 else if (dev->bus)
3256 subsys = dev->bus->name;
3257 else
Joe Perches798efc62012-09-12 20:11:29 -07003258 return 0;
Kay Sieversc4e00da2012-05-03 02:29:59 +02003259
Joe Perches798efc62012-09-12 20:11:29 -07003260 pos += snprintf(hdr + pos, hdrlen - pos, "SUBSYSTEM=%s", subsys);
Ben Hutchings655e5b72014-08-26 00:34:44 -07003261 if (pos >= hdrlen)
3262 goto overflow;
Kay Sieversc4e00da2012-05-03 02:29:59 +02003263
3264 /*
3265 * Add device identifier DEVICE=:
3266 * b12:8 block dev_t
3267 * c127:3 char dev_t
3268 * n8 netdev ifindex
3269 * +sound:card0 subsystem:devname
3270 */
3271 if (MAJOR(dev->devt)) {
3272 char c;
3273
3274 if (strcmp(subsys, "block") == 0)
3275 c = 'b';
3276 else
3277 c = 'c';
Joe Perches798efc62012-09-12 20:11:29 -07003278 pos++;
3279 pos += snprintf(hdr + pos, hdrlen - pos,
3280 "DEVICE=%c%u:%u",
3281 c, MAJOR(dev->devt), MINOR(dev->devt));
Kay Sieversc4e00da2012-05-03 02:29:59 +02003282 } else if (strcmp(subsys, "net") == 0) {
3283 struct net_device *net = to_net_dev(dev);
3284
Joe Perches798efc62012-09-12 20:11:29 -07003285 pos++;
3286 pos += snprintf(hdr + pos, hdrlen - pos,
3287 "DEVICE=n%u", net->ifindex);
Kay Sieversc4e00da2012-05-03 02:29:59 +02003288 } else {
Joe Perches798efc62012-09-12 20:11:29 -07003289 pos++;
3290 pos += snprintf(hdr + pos, hdrlen - pos,
3291 "DEVICE=+%s:%s", subsys, dev_name(dev));
Kay Sieversc4e00da2012-05-03 02:29:59 +02003292 }
Jim Cromieaf7f2152012-07-19 13:46:21 -06003293
Ben Hutchings655e5b72014-08-26 00:34:44 -07003294 if (pos >= hdrlen)
3295 goto overflow;
3296
Joe Perches798efc62012-09-12 20:11:29 -07003297 return pos;
Ben Hutchings655e5b72014-08-26 00:34:44 -07003298
3299overflow:
3300 dev_WARN(dev, "device/subsystem name too long");
3301 return 0;
Joe Perches99bcf212010-06-27 01:02:34 +00003302}
Joe Perches798efc62012-09-12 20:11:29 -07003303
Joe Perches05e4e5b2012-09-12 20:13:37 -07003304int dev_vprintk_emit(int level, const struct device *dev,
3305 const char *fmt, va_list args)
3306{
3307 char hdr[128];
3308 size_t hdrlen;
3309
3310 hdrlen = create_syslog_header(dev, hdr, sizeof(hdr));
3311
3312 return vprintk_emit(0, level, hdrlen ? hdr : NULL, hdrlen, fmt, args);
3313}
3314EXPORT_SYMBOL(dev_vprintk_emit);
3315
3316int dev_printk_emit(int level, const struct device *dev, const char *fmt, ...)
3317{
3318 va_list args;
3319 int r;
3320
3321 va_start(args, fmt);
3322
3323 r = dev_vprintk_emit(level, dev, fmt, args);
3324
3325 va_end(args);
3326
3327 return r;
3328}
3329EXPORT_SYMBOL(dev_printk_emit);
3330
Joe Perchesd1f10522014-12-25 15:07:04 -08003331static void __dev_printk(const char *level, const struct device *dev,
Joe Perches798efc62012-09-12 20:11:29 -07003332 struct va_format *vaf)
3333{
Joe Perchesd1f10522014-12-25 15:07:04 -08003334 if (dev)
3335 dev_printk_emit(level[1] - '0', dev, "%s %s: %pV",
3336 dev_driver_string(dev), dev_name(dev), vaf);
3337 else
3338 printk("%s(NULL device *): %pV", level, vaf);
Joe Perches798efc62012-09-12 20:11:29 -07003339}
Joe Perches99bcf212010-06-27 01:02:34 +00003340
Joe Perchesd1f10522014-12-25 15:07:04 -08003341void dev_printk(const char *level, const struct device *dev,
3342 const char *fmt, ...)
Joe Perches99bcf212010-06-27 01:02:34 +00003343{
3344 struct va_format vaf;
3345 va_list args;
Joe Perches99bcf212010-06-27 01:02:34 +00003346
3347 va_start(args, fmt);
3348
3349 vaf.fmt = fmt;
3350 vaf.va = &args;
3351
Joe Perchesd1f10522014-12-25 15:07:04 -08003352 __dev_printk(level, dev, &vaf);
Joe Perches798efc62012-09-12 20:11:29 -07003353
Joe Perches99bcf212010-06-27 01:02:34 +00003354 va_end(args);
Joe Perches99bcf212010-06-27 01:02:34 +00003355}
3356EXPORT_SYMBOL(dev_printk);
3357
3358#define define_dev_printk_level(func, kern_level) \
Joe Perchesd1f10522014-12-25 15:07:04 -08003359void func(const struct device *dev, const char *fmt, ...) \
Joe Perches99bcf212010-06-27 01:02:34 +00003360{ \
3361 struct va_format vaf; \
3362 va_list args; \
Joe Perches99bcf212010-06-27 01:02:34 +00003363 \
3364 va_start(args, fmt); \
3365 \
3366 vaf.fmt = fmt; \
3367 vaf.va = &args; \
3368 \
Joe Perchesd1f10522014-12-25 15:07:04 -08003369 __dev_printk(kern_level, dev, &vaf); \
Joe Perches798efc62012-09-12 20:11:29 -07003370 \
Joe Perches99bcf212010-06-27 01:02:34 +00003371 va_end(args); \
Joe Perches99bcf212010-06-27 01:02:34 +00003372} \
3373EXPORT_SYMBOL(func);
3374
Joe Perches663336e2018-05-09 08:15:46 -07003375define_dev_printk_level(_dev_emerg, KERN_EMERG);
3376define_dev_printk_level(_dev_alert, KERN_ALERT);
3377define_dev_printk_level(_dev_crit, KERN_CRIT);
3378define_dev_printk_level(_dev_err, KERN_ERR);
3379define_dev_printk_level(_dev_warn, KERN_WARNING);
3380define_dev_printk_level(_dev_notice, KERN_NOTICE);
Joe Perches99bcf212010-06-27 01:02:34 +00003381define_dev_printk_level(_dev_info, KERN_INFO);
3382
3383#endif
Rafael J. Wysocki97badf82015-04-03 23:23:37 +02003384
3385static inline bool fwnode_is_primary(struct fwnode_handle *fwnode)
3386{
3387 return fwnode && !IS_ERR(fwnode->secondary);
3388}
3389
3390/**
3391 * set_primary_fwnode - Change the primary firmware node of a given device.
3392 * @dev: Device to handle.
3393 * @fwnode: New primary firmware node of the device.
3394 *
3395 * Set the device's firmware node pointer to @fwnode, but if a secondary
3396 * firmware node of the device is present, preserve it.
3397 */
3398void set_primary_fwnode(struct device *dev, struct fwnode_handle *fwnode)
3399{
3400 if (fwnode) {
3401 struct fwnode_handle *fn = dev->fwnode;
3402
3403 if (fwnode_is_primary(fn))
3404 fn = fn->secondary;
3405
Mika Westerberg55f89a82015-11-30 17:11:39 +02003406 if (fn) {
3407 WARN_ON(fwnode->secondary);
3408 fwnode->secondary = fn;
3409 }
Rafael J. Wysocki97badf82015-04-03 23:23:37 +02003410 dev->fwnode = fwnode;
3411 } else {
3412 dev->fwnode = fwnode_is_primary(dev->fwnode) ?
3413 dev->fwnode->secondary : NULL;
3414 }
3415}
3416EXPORT_SYMBOL_GPL(set_primary_fwnode);
3417
3418/**
3419 * set_secondary_fwnode - Change the secondary firmware node of a given device.
3420 * @dev: Device to handle.
3421 * @fwnode: New secondary firmware node of the device.
3422 *
3423 * If a primary firmware node of the device is present, set its secondary
3424 * pointer to @fwnode. Otherwise, set the device's firmware node pointer to
3425 * @fwnode.
3426 */
3427void set_secondary_fwnode(struct device *dev, struct fwnode_handle *fwnode)
3428{
3429 if (fwnode)
3430 fwnode->secondary = ERR_PTR(-ENODEV);
3431
3432 if (fwnode_is_primary(dev->fwnode))
3433 dev->fwnode->secondary = fwnode;
3434 else
3435 dev->fwnode = fwnode;
3436}
Johan Hovold4e75e1d2017-06-06 17:59:00 +02003437
3438/**
3439 * device_set_of_node_from_dev - reuse device-tree node of another device
3440 * @dev: device whose device-tree node is being set
3441 * @dev2: device whose device-tree node is being reused
3442 *
3443 * Takes another reference to the new device-tree node after first dropping
3444 * any reference held to the old node.
3445 */
3446void device_set_of_node_from_dev(struct device *dev, const struct device *dev2)
3447{
3448 of_node_put(dev->of_node);
3449 dev->of_node = of_node_get(dev2->of_node);
3450 dev->of_node_reused = true;
3451}
3452EXPORT_SYMBOL_GPL(device_set_of_node_from_dev);
Suzuki K Poulose65b66682019-06-14 18:54:01 +01003453
Suzuki K Poulose6cda08a2019-07-23 23:18:32 +01003454int device_match_name(struct device *dev, const void *name)
3455{
3456 return sysfs_streq(dev_name(dev), name);
3457}
3458EXPORT_SYMBOL_GPL(device_match_name);
3459
Suzuki K Poulose65b66682019-06-14 18:54:01 +01003460int device_match_of_node(struct device *dev, const void *np)
3461{
3462 return dev->of_node == np;
3463}
3464EXPORT_SYMBOL_GPL(device_match_of_node);
Suzuki K Poulose67843bb2019-07-23 23:18:34 +01003465
3466int device_match_fwnode(struct device *dev, const void *fwnode)
3467{
3468 return dev_fwnode(dev) == fwnode;
3469}
3470EXPORT_SYMBOL_GPL(device_match_fwnode);
Suzuki K Poulose4495dfd2019-07-23 23:18:35 +01003471
3472int device_match_devt(struct device *dev, const void *pdevt)
3473{
3474 return dev->devt == *(dev_t *)pdevt;
3475}
3476EXPORT_SYMBOL_GPL(device_match_devt);
Suzuki K Poulose00500142019-07-23 23:18:36 +01003477
3478int device_match_acpi_dev(struct device *dev, const void *adev)
3479{
3480 return ACPI_COMPANION(dev) == adev;
3481}
3482EXPORT_SYMBOL(device_match_acpi_dev);
Suzuki K Poulose6bf85ba2019-07-23 23:18:37 +01003483
3484int device_match_any(struct device *dev, const void *unused)
3485{
3486 return 1;
3487}
3488EXPORT_SYMBOL_GPL(device_match_any);