blob: 2b454aae64b59291b0afb3d5858d1f87dc86177a [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>
Rafael J. Wysocki65650b32019-10-09 01:29:10 +020012#include <linux/cpufreq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/device.h>
14#include <linux/err.h>
Rafael J. Wysocki97badf82015-04-03 23:23:37 +020015#include <linux/fwnode.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/init.h>
17#include <linux/module.h>
18#include <linux/slab.h>
19#include <linux/string.h>
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -070020#include <linux/kdev_t.h>
Benjamin Herrenschmidt116af372006-10-25 13:44:59 +100021#include <linux/notifier.h>
Grant Likely07d57a32012-02-01 11:22:22 -070022#include <linux/of.h>
23#include <linux/of_device.h>
Kay Sieversda231fd2007-11-21 17:29:15 +010024#include <linux/genhd.h>
Dave Youngf75b1c62008-05-28 09:28:39 -070025#include <linux/mutex.h>
Peter Chenaf8db152011-11-15 21:52:29 +010026#include <linux/pm_runtime.h>
Kay Sieversc4e00da2012-05-03 02:29:59 +020027#include <linux/netdevice.h>
Ingo Molnar174cd4b2017-02-02 19:15:33 +010028#include <linux/sched/signal.h>
Greg Kroah-Hartman63967682013-08-27 10:24:15 -070029#include <linux/sysfs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
31#include "base.h"
32#include "power/power.h"
33
Andi Kleene52eec12010-09-08 16:54:17 +020034#ifdef CONFIG_SYSFS_DEPRECATED
35#ifdef CONFIG_SYSFS_DEPRECATED_V2
36long sysfs_deprecated = 1;
37#else
38long sysfs_deprecated = 0;
39#endif
Hanjun Guo3454bf92013-08-17 20:42:24 +080040static int __init sysfs_deprecated_setup(char *arg)
Andi Kleene52eec12010-09-08 16:54:17 +020041{
Jingoo Han34da5e62013-07-26 13:10:22 +090042 return kstrtol(arg, 10, &sysfs_deprecated);
Andi Kleene52eec12010-09-08 16:54:17 +020043}
44early_param("sysfs.deprecated", sysfs_deprecated_setup);
45#endif
46
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +010047/* Device links support. */
Saravana Kannane2ae9bc2019-09-04 14:11:21 -070048static LIST_HEAD(wait_for_suppliers);
49static DEFINE_MUTEX(wfs_lock);
Saravana Kannanfc5a2512019-09-04 14:11:23 -070050static LIST_HEAD(deferred_sync);
51static unsigned int defer_sync_state_count = 1;
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +010052
53#ifdef CONFIG_SRCU
54static DEFINE_MUTEX(device_links_lock);
55DEFINE_STATIC_SRCU(device_links_srcu);
56
57static inline void device_links_write_lock(void)
58{
59 mutex_lock(&device_links_lock);
60}
61
62static inline void device_links_write_unlock(void)
63{
64 mutex_unlock(&device_links_lock);
65}
66
Jules Irenge68464d72020-02-14 20:47:29 +000067int device_links_read_lock(void) __acquires(&device_links_srcu)
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +010068{
69 return srcu_read_lock(&device_links_srcu);
70}
71
Jules Irengeab7789c2020-02-14 20:47:30 +000072void device_links_read_unlock(int idx) __releases(&device_links_srcu)
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +010073{
74 srcu_read_unlock(&device_links_srcu, idx);
75}
Joel Fernandes (Google)c2fa1e12019-07-16 18:12:25 -040076
77int device_links_read_lock_held(void)
78{
79 return srcu_read_lock_held(&device_links_srcu);
80}
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +010081#else /* !CONFIG_SRCU */
82static DECLARE_RWSEM(device_links_lock);
83
84static inline void device_links_write_lock(void)
85{
86 down_write(&device_links_lock);
87}
88
89static inline void device_links_write_unlock(void)
90{
91 up_write(&device_links_lock);
92}
93
94int device_links_read_lock(void)
95{
96 down_read(&device_links_lock);
97 return 0;
98}
99
100void device_links_read_unlock(int not_used)
101{
102 up_read(&device_links_lock);
103}
Joel Fernandes (Google)c2fa1e12019-07-16 18:12:25 -0400104
105#ifdef CONFIG_DEBUG_LOCK_ALLOC
106int device_links_read_lock_held(void)
107{
108 return lockdep_is_held(&device_links_lock);
109}
110#endif
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100111#endif /* !CONFIG_SRCU */
112
113/**
114 * device_is_dependent - Check if one device depends on another one
115 * @dev: Device to check dependencies for.
116 * @target: Device to check against.
117 *
118 * Check if @target depends on @dev or any device dependent on it (its child or
119 * its consumer etc). Return 1 if that is the case or 0 otherwise.
120 */
121static int device_is_dependent(struct device *dev, void *target)
122{
123 struct device_link *link;
124 int ret;
125
Benjamin Gaignarde16f4f32018-07-16 13:37:44 +0200126 if (dev == target)
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100127 return 1;
128
129 ret = device_for_each_child(dev, target, device_is_dependent);
130 if (ret)
131 return ret;
132
133 list_for_each_entry(link, &dev->links.consumers, s_node) {
Saravana Kannan05ef9832019-10-28 15:00:22 -0700134 if (link->flags == (DL_FLAG_SYNC_STATE_ONLY | DL_FLAG_MANAGED))
135 continue;
136
Benjamin Gaignarde16f4f32018-07-16 13:37:44 +0200137 if (link->consumer == target)
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100138 return 1;
139
140 ret = device_is_dependent(link->consumer, target);
141 if (ret)
142 break;
143 }
144 return ret;
145}
146
Rafael J. Wysocki515db262019-07-16 17:21:06 +0200147static void device_link_init_status(struct device_link *link,
148 struct device *consumer,
149 struct device *supplier)
150{
151 switch (supplier->links.status) {
152 case DL_DEV_PROBING:
153 switch (consumer->links.status) {
154 case DL_DEV_PROBING:
155 /*
156 * A consumer driver can create a link to a supplier
157 * that has not completed its probing yet as long as it
158 * knows that the supplier is already functional (for
159 * example, it has just acquired some resources from the
160 * supplier).
161 */
162 link->status = DL_STATE_CONSUMER_PROBE;
163 break;
164 default:
165 link->status = DL_STATE_DORMANT;
166 break;
167 }
168 break;
169 case DL_DEV_DRIVER_BOUND:
170 switch (consumer->links.status) {
171 case DL_DEV_PROBING:
172 link->status = DL_STATE_CONSUMER_PROBE;
173 break;
174 case DL_DEV_DRIVER_BOUND:
175 link->status = DL_STATE_ACTIVE;
176 break;
177 default:
178 link->status = DL_STATE_AVAILABLE;
179 break;
180 }
181 break;
182 case DL_DEV_UNBINDING:
183 link->status = DL_STATE_SUPPLIER_UNBIND;
184 break;
185 default:
186 link->status = DL_STATE_DORMANT;
187 break;
188 }
189}
190
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100191static int device_reorder_to_tail(struct device *dev, void *not_used)
192{
193 struct device_link *link;
194
195 /*
196 * Devices that have not been registered yet will be put to the ends
197 * of the lists during the registration, so skip them here.
198 */
199 if (device_is_registered(dev))
200 devices_kset_move_last(dev);
201
202 if (device_pm_initialized(dev))
203 device_pm_move_last(dev);
204
205 device_for_each_child(dev, NULL, device_reorder_to_tail);
Saravana Kannan05ef9832019-10-28 15:00:22 -0700206 list_for_each_entry(link, &dev->links.consumers, s_node) {
207 if (link->flags == (DL_FLAG_SYNC_STATE_ONLY | DL_FLAG_MANAGED))
208 continue;
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100209 device_reorder_to_tail(link->consumer, NULL);
Saravana Kannan05ef9832019-10-28 15:00:22 -0700210 }
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100211
212 return 0;
213}
214
215/**
Feng Kan494fd7b2018-04-10 16:57:06 -0700216 * device_pm_move_to_tail - Move set of devices to the end of device lists
217 * @dev: Device to move
218 *
219 * This is a device_reorder_to_tail() wrapper taking the requisite locks.
220 *
221 * It moves the @dev along with all of its children and all of its consumers
222 * to the ends of the device_kset and dpm_list, recursively.
223 */
224void device_pm_move_to_tail(struct device *dev)
225{
226 int idx;
227
228 idx = device_links_read_lock();
229 device_pm_lock();
230 device_reorder_to_tail(dev, NULL);
231 device_pm_unlock();
232 device_links_read_unlock(idx);
233}
234
Rafael J. Wysocki515db262019-07-16 17:21:06 +0200235#define DL_MANAGED_LINK_FLAGS (DL_FLAG_AUTOREMOVE_CONSUMER | \
236 DL_FLAG_AUTOREMOVE_SUPPLIER | \
Saravana Kannan05ef9832019-10-28 15:00:22 -0700237 DL_FLAG_AUTOPROBE_CONSUMER | \
238 DL_FLAG_SYNC_STATE_ONLY)
Rafael J. Wysocki515db262019-07-16 17:21:06 +0200239
Rafael J. Wysockifb583c82019-07-30 11:28:57 +0200240#define DL_ADD_VALID_FLAGS (DL_MANAGED_LINK_FLAGS | DL_FLAG_STATELESS | \
241 DL_FLAG_PM_RUNTIME | DL_FLAG_RPM_ACTIVE)
242
Feng Kan494fd7b2018-04-10 16:57:06 -0700243/**
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100244 * device_link_add - Create a link between two devices.
245 * @consumer: Consumer end of the link.
246 * @supplier: Supplier end of the link.
247 * @flags: Link flags.
248 *
Rafael J. Wysocki21d5c572016-10-30 17:32:31 +0100249 * The caller is responsible for the proper synchronization of the link creation
250 * with runtime PM. First, setting the DL_FLAG_PM_RUNTIME flag will cause the
251 * runtime PM framework to take the link into account. Second, if the
252 * DL_FLAG_RPM_ACTIVE flag is set in addition to it, the supplier devices will
253 * be forced into the active metastate and reference-counted upon the creation
254 * of the link. If DL_FLAG_PM_RUNTIME is not set, DL_FLAG_RPM_ACTIVE will be
255 * ignored.
256 *
Rafael J. Wysocki515db262019-07-16 17:21:06 +0200257 * If DL_FLAG_STATELESS is set in @flags, the caller of this function is
258 * expected to release the link returned by it directly with the help of either
259 * device_link_del() or device_link_remove().
Rafael J. Wysocki72175d42019-02-01 01:58:33 +0100260 *
261 * If that flag is not set, however, the caller of this function is handing the
262 * management of the link over to the driver core entirely and its return value
263 * can only be used to check whether or not the link is present. In that case,
264 * the DL_FLAG_AUTOREMOVE_CONSUMER and DL_FLAG_AUTOREMOVE_SUPPLIER device link
265 * flags can be used to indicate to the driver core when the link can be safely
266 * deleted. Namely, setting one of them in @flags indicates to the driver core
267 * that the link is not going to be used (by the given caller of this function)
268 * after unbinding the consumer or supplier driver, respectively, from its
269 * device, so the link can be deleted at that point. If none of them is set,
270 * the link will be maintained until one of the devices pointed to by it (either
271 * the consumer or the supplier) is unregistered.
Rafael J. Wysockic8d50982019-02-01 01:45:55 +0100272 *
Rafael J. Wysockie7dd4012019-02-01 01:59:42 +0100273 * Also, if DL_FLAG_STATELESS, DL_FLAG_AUTOREMOVE_CONSUMER and
274 * DL_FLAG_AUTOREMOVE_SUPPLIER are not set in @flags (that is, a persistent
275 * managed device link is being added), the DL_FLAG_AUTOPROBE_CONSUMER flag can
276 * be used to request the driver core to automaticall probe for a consmer
277 * driver after successfully binding a driver to the supplier device.
278 *
Rafael J. Wysocki515db262019-07-16 17:21:06 +0200279 * The combination of DL_FLAG_STATELESS and one of DL_FLAG_AUTOREMOVE_CONSUMER,
280 * DL_FLAG_AUTOREMOVE_SUPPLIER, or DL_FLAG_AUTOPROBE_CONSUMER set in @flags at
281 * the same time is invalid and will cause NULL to be returned upfront.
282 * However, if a device link between the given @consumer and @supplier pair
283 * exists already when this function is called for them, the existing link will
284 * be returned regardless of its current type and status (the link's flags may
285 * be modified then). The caller of this function is then expected to treat
286 * the link as though it has just been created, so (in particular) if
287 * DL_FLAG_STATELESS was passed in @flags, the link needs to be released
288 * explicitly when not needed any more (as stated above).
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100289 *
290 * A side effect of the link creation is re-ordering of dpm_list and the
291 * devices_kset list by moving the consumer device and all devices depending
292 * on it to the ends of these lists (that does not happen to devices that have
293 * not been registered when this function is called).
294 *
295 * The supplier device is required to be registered when this function is called
296 * and NULL will be returned if that is not the case. The consumer device need
Lukas Wunner64df1142016-12-04 13:10:04 +0100297 * not be registered, however.
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100298 */
299struct device_link *device_link_add(struct device *consumer,
300 struct device *supplier, u32 flags)
301{
302 struct device_link *link;
303
Rafael J. Wysockifb583c82019-07-30 11:28:57 +0200304 if (!consumer || !supplier || flags & ~DL_ADD_VALID_FLAGS ||
Rafael J. Wysocki515db262019-07-16 17:21:06 +0200305 (flags & DL_FLAG_STATELESS && flags & DL_MANAGED_LINK_FLAGS) ||
Saravana Kannan05ef9832019-10-28 15:00:22 -0700306 (flags & DL_FLAG_SYNC_STATE_ONLY &&
307 flags != DL_FLAG_SYNC_STATE_ONLY) ||
Rafael J. Wysockie7dd4012019-02-01 01:59:42 +0100308 (flags & DL_FLAG_AUTOPROBE_CONSUMER &&
309 flags & (DL_FLAG_AUTOREMOVE_CONSUMER |
310 DL_FLAG_AUTOREMOVE_SUPPLIER)))
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100311 return NULL;
312
Rafael J. Wysocki5db25c92019-02-01 01:47:53 +0100313 if (flags & DL_FLAG_PM_RUNTIME && flags & DL_FLAG_RPM_ACTIVE) {
314 if (pm_runtime_get_sync(supplier) < 0) {
315 pm_runtime_put_noidle(supplier);
316 return NULL;
317 }
Rafael J. Wysocki5db25c92019-02-01 01:47:53 +0100318 }
319
Rafael J. Wysocki515db262019-07-16 17:21:06 +0200320 if (!(flags & DL_FLAG_STATELESS))
321 flags |= DL_FLAG_MANAGED;
322
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100323 device_links_write_lock();
324 device_pm_lock();
325
326 /*
327 * If the supplier has not been fully registered yet or there is a
Saravana Kannan05ef9832019-10-28 15:00:22 -0700328 * reverse (non-SYNC_STATE_ONLY) dependency between the consumer and
329 * the supplier already in the graph, return NULL. If the link is a
330 * SYNC_STATE_ONLY link, we don't check for reverse dependencies
331 * because it only affects sync_state() callbacks.
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100332 */
333 if (!device_pm_initialized(supplier)
Saravana Kannan05ef9832019-10-28 15:00:22 -0700334 || (!(flags & DL_FLAG_SYNC_STATE_ONLY) &&
335 device_is_dependent(consumer, supplier))) {
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100336 link = NULL;
337 goto out;
338 }
339
Rafael J. Wysocki72175d42019-02-01 01:58:33 +0100340 /*
341 * DL_FLAG_AUTOREMOVE_SUPPLIER indicates that the link will be needed
342 * longer than for DL_FLAG_AUTOREMOVE_CONSUMER and setting them both
343 * together doesn't make sense, so prefer DL_FLAG_AUTOREMOVE_SUPPLIER.
344 */
345 if (flags & DL_FLAG_AUTOREMOVE_SUPPLIER)
346 flags &= ~DL_FLAG_AUTOREMOVE_CONSUMER;
347
Rafael J. Wysockif265df52019-02-01 01:46:54 +0100348 list_for_each_entry(link, &supplier->links.consumers, s_node) {
349 if (link->consumer != consumer)
350 continue;
351
Rafael J. Wysockie2f3cd82019-02-01 01:49:14 +0100352 if (flags & DL_FLAG_PM_RUNTIME) {
353 if (!(link->flags & DL_FLAG_PM_RUNTIME)) {
Rafael J. Wysocki4c06c4e2019-02-12 13:08:10 +0100354 pm_runtime_new_link(consumer);
Rafael J. Wysockie2f3cd82019-02-01 01:49:14 +0100355 link->flags |= DL_FLAG_PM_RUNTIME;
356 }
357 if (flags & DL_FLAG_RPM_ACTIVE)
Rafael J. Wysocki36003d42019-02-19 17:53:26 +0100358 refcount_inc(&link->rpm_active);
Rafael J. Wysockie2f3cd82019-02-01 01:49:14 +0100359 }
360
Rafael J. Wysocki72175d42019-02-01 01:58:33 +0100361 if (flags & DL_FLAG_STATELESS) {
362 kref_get(&link->kref);
Saravana Kannan05ef9832019-10-28 15:00:22 -0700363 if (link->flags & DL_FLAG_SYNC_STATE_ONLY &&
364 !(link->flags & DL_FLAG_STATELESS)) {
365 link->flags |= DL_FLAG_STATELESS;
366 goto reorder;
367 } else {
368 goto out;
369 }
Rafael J. Wysocki72175d42019-02-01 01:58:33 +0100370 }
371
372 /*
373 * If the life time of the link following from the new flags is
374 * longer than indicated by the flags of the existing link,
375 * update the existing link to stay around longer.
376 */
377 if (flags & DL_FLAG_AUTOREMOVE_SUPPLIER) {
378 if (link->flags & DL_FLAG_AUTOREMOVE_CONSUMER) {
379 link->flags &= ~DL_FLAG_AUTOREMOVE_CONSUMER;
380 link->flags |= DL_FLAG_AUTOREMOVE_SUPPLIER;
381 }
382 } else if (!(flags & DL_FLAG_AUTOREMOVE_CONSUMER)) {
383 link->flags &= ~(DL_FLAG_AUTOREMOVE_CONSUMER |
384 DL_FLAG_AUTOREMOVE_SUPPLIER);
385 }
Rafael J. Wysocki515db262019-07-16 17:21:06 +0200386 if (!(link->flags & DL_FLAG_MANAGED)) {
387 kref_get(&link->kref);
388 link->flags |= DL_FLAG_MANAGED;
389 device_link_init_status(link, consumer, supplier);
390 }
Saravana Kannan05ef9832019-10-28 15:00:22 -0700391 if (link->flags & DL_FLAG_SYNC_STATE_ONLY &&
392 !(flags & DL_FLAG_SYNC_STATE_ONLY)) {
393 link->flags &= ~DL_FLAG_SYNC_STATE_ONLY;
394 goto reorder;
395 }
396
Rafael J. Wysockif265df52019-02-01 01:46:54 +0100397 goto out;
398 }
399
Rafael J. Wysocki21d5c572016-10-30 17:32:31 +0100400 link = kzalloc(sizeof(*link), GFP_KERNEL);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100401 if (!link)
402 goto out;
403
Rafael J. Wysockie2f3cd82019-02-01 01:49:14 +0100404 refcount_set(&link->rpm_active, 1);
405
Rafael J. Wysockibaa88092016-10-30 17:32:43 +0100406 if (flags & DL_FLAG_PM_RUNTIME) {
Rafael J. Wysockie2f3cd82019-02-01 01:49:14 +0100407 if (flags & DL_FLAG_RPM_ACTIVE)
Rafael J. Wysocki36003d42019-02-19 17:53:26 +0100408 refcount_inc(&link->rpm_active);
Rafael J. Wysockie2f3cd82019-02-01 01:49:14 +0100409
Rafael J. Wysocki4c06c4e2019-02-12 13:08:10 +0100410 pm_runtime_new_link(consumer);
Rafael J. Wysocki21d5c572016-10-30 17:32:31 +0100411 }
Rafael J. Wysockie2f3cd82019-02-01 01:49:14 +0100412
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100413 get_device(supplier);
414 link->supplier = supplier;
415 INIT_LIST_HEAD(&link->s_node);
416 get_device(consumer);
417 link->consumer = consumer;
418 INIT_LIST_HEAD(&link->c_node);
419 link->flags = flags;
Lukas Wunneread18c22018-02-10 19:27:12 +0100420 kref_init(&link->kref);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100421
Lukas Wunner64df1142016-12-04 13:10:04 +0100422 /* Determine the initial link state. */
Rafael J. Wysocki515db262019-07-16 17:21:06 +0200423 if (flags & DL_FLAG_STATELESS)
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100424 link->status = DL_STATE_NONE;
Rafael J. Wysocki515db262019-07-16 17:21:06 +0200425 else
426 device_link_init_status(link, consumer, supplier);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100427
428 /*
Rafael J. Wysocki15cfb092019-02-01 01:50:39 +0100429 * Some callers expect the link creation during consumer driver probe to
430 * resume the supplier even without DL_FLAG_RPM_ACTIVE.
431 */
432 if (link->status == DL_STATE_CONSUMER_PROBE &&
433 flags & DL_FLAG_PM_RUNTIME)
434 pm_runtime_resume(supplier);
435
Saravana Kannan05ef9832019-10-28 15:00:22 -0700436 if (flags & DL_FLAG_SYNC_STATE_ONLY) {
437 dev_dbg(consumer,
438 "Linked as a sync state only consumer to %s\n",
439 dev_name(supplier));
440 goto out;
441 }
442reorder:
Rafael J. Wysocki15cfb092019-02-01 01:50:39 +0100443 /*
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100444 * Move the consumer and all of the devices depending on it to the end
445 * of dpm_list and the devices_kset list.
446 *
447 * It is necessary to hold dpm_list locked throughout all that or else
448 * we may end up suspending with a wrong ordering of it.
449 */
450 device_reorder_to_tail(consumer, NULL);
451
452 list_add_tail_rcu(&link->s_node, &supplier->links.consumers);
453 list_add_tail_rcu(&link->c_node, &consumer->links.suppliers);
454
Jerome Brunet8a4b3262018-12-21 17:23:41 +0100455 dev_dbg(consumer, "Linked as a consumer to %s\n", dev_name(supplier));
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100456
457 out:
458 device_pm_unlock();
459 device_links_write_unlock();
Rafael J. Wysocki5db25c92019-02-01 01:47:53 +0100460
Rafael J. Wysockie2f3cd82019-02-01 01:49:14 +0100461 if ((flags & DL_FLAG_PM_RUNTIME && flags & DL_FLAG_RPM_ACTIVE) && !link)
Rafael J. Wysocki5db25c92019-02-01 01:47:53 +0100462 pm_runtime_put(supplier);
463
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100464 return link;
465}
466EXPORT_SYMBOL_GPL(device_link_add);
467
Saravana Kannane2ae9bc2019-09-04 14:11:21 -0700468/**
469 * device_link_wait_for_supplier - Add device to wait_for_suppliers list
470 * @consumer: Consumer device
471 *
472 * Marks the @consumer device as waiting for suppliers to become available by
473 * adding it to the wait_for_suppliers list. The consumer device will never be
474 * probed until it's removed from the wait_for_suppliers list.
475 *
476 * The caller is responsible for adding the links to the supplier devices once
477 * they are available and removing the @consumer device from the
478 * wait_for_suppliers list once links to all the suppliers have been created.
479 *
480 * This function is NOT meant to be called from the probe function of the
481 * consumer but rather from code that creates/adds the consumer device.
482 */
Saravana Kannanbcbbcfd2019-10-28 15:00:23 -0700483static void device_link_wait_for_supplier(struct device *consumer,
484 bool need_for_probe)
Saravana Kannane2ae9bc2019-09-04 14:11:21 -0700485{
486 mutex_lock(&wfs_lock);
487 list_add_tail(&consumer->links.needs_suppliers, &wait_for_suppliers);
Saravana Kannanbcbbcfd2019-10-28 15:00:23 -0700488 consumer->links.need_for_probe = need_for_probe;
Saravana Kannane2ae9bc2019-09-04 14:11:21 -0700489 mutex_unlock(&wfs_lock);
490}
491
Saravana Kannanbcbbcfd2019-10-28 15:00:23 -0700492static void device_link_wait_for_mandatory_supplier(struct device *consumer)
493{
494 device_link_wait_for_supplier(consumer, true);
495}
496
497static void device_link_wait_for_optional_supplier(struct device *consumer)
498{
499 device_link_wait_for_supplier(consumer, false);
500}
501
Saravana Kannane2ae9bc2019-09-04 14:11:21 -0700502/**
503 * device_link_add_missing_supplier_links - Add links from consumer devices to
504 * supplier devices, leaving any
505 * consumer with inactive suppliers on
506 * the wait_for_suppliers list
507 *
508 * Loops through all consumers waiting on suppliers and tries to add all their
509 * supplier links. If that succeeds, the consumer device is removed from
510 * wait_for_suppliers list. Otherwise, they are left in the wait_for_suppliers
511 * list. Devices left on the wait_for_suppliers list will not be probed.
512 *
513 * The fwnode add_links callback is expected to return 0 if it has found and
514 * added all the supplier links for the consumer device. It should return an
515 * error if it isn't able to do so.
516 *
517 * The caller of device_link_wait_for_supplier() is expected to call this once
518 * it's aware of potential suppliers becoming available.
519 */
520static void device_link_add_missing_supplier_links(void)
521{
522 struct device *dev, *tmp;
523
524 mutex_lock(&wfs_lock);
525 list_for_each_entry_safe(dev, tmp, &wait_for_suppliers,
Saravana Kannan1745d292020-02-21 17:40:34 -0800526 links.needs_suppliers) {
527 int ret = fwnode_call_int_op(dev->fwnode, add_links, dev);
528 if (!ret)
Saravana Kannane2ae9bc2019-09-04 14:11:21 -0700529 list_del_init(&dev->links.needs_suppliers);
Saravana Kannan1745d292020-02-21 17:40:34 -0800530 else if (ret != -ENODEV)
531 dev->links.need_for_probe = false;
532 }
Saravana Kannane2ae9bc2019-09-04 14:11:21 -0700533 mutex_unlock(&wfs_lock);
534}
535
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100536static void device_link_free(struct device_link *link)
537{
Rafael J. Wysockia1fdbfb2019-02-01 01:52:45 +0100538 while (refcount_dec_not_one(&link->rpm_active))
539 pm_runtime_put(link->supplier);
540
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100541 put_device(link->consumer);
542 put_device(link->supplier);
543 kfree(link);
544}
545
546#ifdef CONFIG_SRCU
547static void __device_link_free_srcu(struct rcu_head *rhead)
548{
549 device_link_free(container_of(rhead, struct device_link, rcu_head));
550}
551
Lukas Wunneread18c22018-02-10 19:27:12 +0100552static void __device_link_del(struct kref *kref)
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100553{
Lukas Wunneread18c22018-02-10 19:27:12 +0100554 struct device_link *link = container_of(kref, struct device_link, kref);
555
Jerome Brunet8a4b3262018-12-21 17:23:41 +0100556 dev_dbg(link->consumer, "Dropping the link to %s\n",
557 dev_name(link->supplier));
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100558
Rafael J. Wysockibaa88092016-10-30 17:32:43 +0100559 if (link->flags & DL_FLAG_PM_RUNTIME)
560 pm_runtime_drop_link(link->consumer);
561
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100562 list_del_rcu(&link->s_node);
563 list_del_rcu(&link->c_node);
564 call_srcu(&device_links_srcu, &link->rcu_head, __device_link_free_srcu);
565}
566#else /* !CONFIG_SRCU */
Lukas Wunneread18c22018-02-10 19:27:12 +0100567static void __device_link_del(struct kref *kref)
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100568{
Lukas Wunneread18c22018-02-10 19:27:12 +0100569 struct device_link *link = container_of(kref, struct device_link, kref);
570
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100571 dev_info(link->consumer, "Dropping the link to %s\n",
572 dev_name(link->supplier));
573
Lukas Wunner433986c2018-02-10 19:13:58 +0100574 if (link->flags & DL_FLAG_PM_RUNTIME)
575 pm_runtime_drop_link(link->consumer);
576
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100577 list_del(&link->s_node);
578 list_del(&link->c_node);
579 device_link_free(link);
580}
581#endif /* !CONFIG_SRCU */
582
Rafael J. Wysocki72175d42019-02-01 01:58:33 +0100583static void device_link_put_kref(struct device_link *link)
584{
585 if (link->flags & DL_FLAG_STATELESS)
586 kref_put(&link->kref, __device_link_del);
587 else
588 WARN(1, "Unable to drop a managed device link reference\n");
589}
590
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100591/**
Rafael J. Wysocki72175d42019-02-01 01:58:33 +0100592 * device_link_del - Delete a stateless link between two devices.
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100593 * @link: Device link to delete.
594 *
595 * The caller must ensure proper synchronization of this function with runtime
Lukas Wunneread18c22018-02-10 19:27:12 +0100596 * PM. If the link was added multiple times, it needs to be deleted as often.
597 * Care is required for hotplugged devices: Their links are purged on removal
598 * and calling device_link_del() is then no longer allowed.
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100599 */
600void device_link_del(struct device_link *link)
601{
602 device_links_write_lock();
603 device_pm_lock();
Rafael J. Wysocki72175d42019-02-01 01:58:33 +0100604 device_link_put_kref(link);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100605 device_pm_unlock();
606 device_links_write_unlock();
607}
608EXPORT_SYMBOL_GPL(device_link_del);
609
pascal pailletd8842212018-07-05 14:25:56 +0000610/**
Rafael J. Wysocki72175d42019-02-01 01:58:33 +0100611 * device_link_remove - Delete a stateless link between two devices.
pascal pailletd8842212018-07-05 14:25:56 +0000612 * @consumer: Consumer end of the link.
613 * @supplier: Supplier end of the link.
614 *
615 * The caller must ensure proper synchronization of this function with runtime
616 * PM.
617 */
618void device_link_remove(void *consumer, struct device *supplier)
619{
620 struct device_link *link;
621
622 if (WARN_ON(consumer == supplier))
623 return;
624
625 device_links_write_lock();
626 device_pm_lock();
627
628 list_for_each_entry(link, &supplier->links.consumers, s_node) {
629 if (link->consumer == consumer) {
Rafael J. Wysocki72175d42019-02-01 01:58:33 +0100630 device_link_put_kref(link);
pascal pailletd8842212018-07-05 14:25:56 +0000631 break;
632 }
633 }
634
635 device_pm_unlock();
636 device_links_write_unlock();
637}
638EXPORT_SYMBOL_GPL(device_link_remove);
639
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100640static void device_links_missing_supplier(struct device *dev)
641{
642 struct device_link *link;
643
644 list_for_each_entry(link, &dev->links.suppliers, c_node)
645 if (link->status == DL_STATE_CONSUMER_PROBE)
646 WRITE_ONCE(link->status, DL_STATE_AVAILABLE);
647}
648
649/**
650 * device_links_check_suppliers - Check presence of supplier drivers.
651 * @dev: Consumer device.
652 *
653 * Check links from this device to any suppliers. Walk the list of the device's
654 * links to suppliers and see if all of them are available. If not, simply
655 * return -EPROBE_DEFER.
656 *
657 * We need to guarantee that the supplier will not go away after the check has
658 * been positive here. It only can go away in __device_release_driver() and
659 * that function checks the device's links to consumers. This means we need to
660 * mark the link as "consumer probe in progress" to make the supplier removal
661 * wait for us to complete (or bad things may happen).
662 *
Rafael J. Wysocki515db262019-07-16 17:21:06 +0200663 * Links without the DL_FLAG_MANAGED flag set are ignored.
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100664 */
665int device_links_check_suppliers(struct device *dev)
666{
667 struct device_link *link;
668 int ret = 0;
669
Saravana Kannane2ae9bc2019-09-04 14:11:21 -0700670 /*
671 * Device waiting for supplier to become available is not allowed to
672 * probe.
673 */
674 mutex_lock(&wfs_lock);
Saravana Kannanbcbbcfd2019-10-28 15:00:23 -0700675 if (!list_empty(&dev->links.needs_suppliers) &&
676 dev->links.need_for_probe) {
Saravana Kannane2ae9bc2019-09-04 14:11:21 -0700677 mutex_unlock(&wfs_lock);
678 return -EPROBE_DEFER;
679 }
680 mutex_unlock(&wfs_lock);
681
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100682 device_links_write_lock();
683
684 list_for_each_entry(link, &dev->links.suppliers, c_node) {
Saravana Kannan05ef9832019-10-28 15:00:22 -0700685 if (!(link->flags & DL_FLAG_MANAGED) ||
686 link->flags & DL_FLAG_SYNC_STATE_ONLY)
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100687 continue;
688
689 if (link->status != DL_STATE_AVAILABLE) {
690 device_links_missing_supplier(dev);
691 ret = -EPROBE_DEFER;
692 break;
693 }
694 WRITE_ONCE(link->status, DL_STATE_CONSUMER_PROBE);
695 }
696 dev->links.status = DL_DEV_PROBING;
697
698 device_links_write_unlock();
699 return ret;
700}
701
Saravana Kannan26e77702019-11-14 14:56:45 -0800702/**
703 * __device_links_queue_sync_state - Queue a device for sync_state() callback
704 * @dev: Device to call sync_state() on
705 * @list: List head to queue the @dev on
706 *
707 * Queues a device for a sync_state() callback when the device links write lock
708 * isn't held. This allows the sync_state() execution flow to use device links
709 * APIs. The caller must ensure this function is called with
710 * device_links_write_lock() held.
711 *
712 * This function does a get_device() to make sure the device is not freed while
713 * on this list.
714 *
715 * So the caller must also ensure that device_links_flush_sync_list() is called
716 * as soon as the caller releases device_links_write_lock(). This is necessary
717 * to make sure the sync_state() is called in a timely fashion and the
718 * put_device() is called on this device.
719 */
720static void __device_links_queue_sync_state(struct device *dev,
721 struct list_head *list)
Saravana Kannanfc5a2512019-09-04 14:11:23 -0700722{
723 struct device_link *link;
724
Saravana Kannan77036162020-02-21 00:05:10 -0800725 if (!dev_has_sync_state(dev))
726 return;
Saravana Kannanfc5a2512019-09-04 14:11:23 -0700727 if (dev->state_synced)
728 return;
729
730 list_for_each_entry(link, &dev->links.consumers, s_node) {
731 if (!(link->flags & DL_FLAG_MANAGED))
732 continue;
733 if (link->status != DL_STATE_ACTIVE)
734 return;
735 }
736
Saravana Kannan26e77702019-11-14 14:56:45 -0800737 /*
738 * Set the flag here to avoid adding the same device to a list more
739 * than once. This can happen if new consumers get added to the device
740 * and probed before the list is flushed.
741 */
Saravana Kannanfc5a2512019-09-04 14:11:23 -0700742 dev->state_synced = true;
Saravana Kannan26e77702019-11-14 14:56:45 -0800743
744 if (WARN_ON(!list_empty(&dev->links.defer_sync)))
745 return;
746
747 get_device(dev);
748 list_add_tail(&dev->links.defer_sync, list);
749}
750
751/**
752 * device_links_flush_sync_list - Call sync_state() on a list of devices
753 * @list: List of devices to call sync_state() on
Saravana Kannan21eb93f2020-02-21 00:05:08 -0800754 * @dont_lock_dev: Device for which lock is already held by the caller
Saravana Kannan26e77702019-11-14 14:56:45 -0800755 *
756 * Calls sync_state() on all the devices that have been queued for it. This
Saravana Kannan21eb93f2020-02-21 00:05:08 -0800757 * function is used in conjunction with __device_links_queue_sync_state(). The
758 * @dont_lock_dev parameter is useful when this function is called from a
759 * context where a device lock is already held.
Saravana Kannan26e77702019-11-14 14:56:45 -0800760 */
Saravana Kannan21eb93f2020-02-21 00:05:08 -0800761static void device_links_flush_sync_list(struct list_head *list,
762 struct device *dont_lock_dev)
Saravana Kannan26e77702019-11-14 14:56:45 -0800763{
764 struct device *dev, *tmp;
765
766 list_for_each_entry_safe(dev, tmp, list, links.defer_sync) {
767 list_del_init(&dev->links.defer_sync);
768
Saravana Kannan21eb93f2020-02-21 00:05:08 -0800769 if (dev != dont_lock_dev)
770 device_lock(dev);
Saravana Kannan26e77702019-11-14 14:56:45 -0800771
772 if (dev->bus->sync_state)
773 dev->bus->sync_state(dev);
774 else if (dev->driver && dev->driver->sync_state)
775 dev->driver->sync_state(dev);
776
Saravana Kannan21eb93f2020-02-21 00:05:08 -0800777 if (dev != dont_lock_dev)
778 device_unlock(dev);
Saravana Kannan26e77702019-11-14 14:56:45 -0800779
780 put_device(dev);
781 }
Saravana Kannanfc5a2512019-09-04 14:11:23 -0700782}
783
784void device_links_supplier_sync_state_pause(void)
785{
786 device_links_write_lock();
787 defer_sync_state_count++;
788 device_links_write_unlock();
789}
790
791void device_links_supplier_sync_state_resume(void)
792{
793 struct device *dev, *tmp;
Saravana Kannan26e77702019-11-14 14:56:45 -0800794 LIST_HEAD(sync_list);
Saravana Kannanfc5a2512019-09-04 14:11:23 -0700795
796 device_links_write_lock();
797 if (!defer_sync_state_count) {
798 WARN(true, "Unmatched sync_state pause/resume!");
799 goto out;
800 }
801 defer_sync_state_count--;
802 if (defer_sync_state_count)
803 goto out;
804
805 list_for_each_entry_safe(dev, tmp, &deferred_sync, links.defer_sync) {
Saravana Kannan26e77702019-11-14 14:56:45 -0800806 /*
807 * Delete from deferred_sync list before queuing it to
808 * sync_list because defer_sync is used for both lists.
809 */
Saravana Kannanfc5a2512019-09-04 14:11:23 -0700810 list_del_init(&dev->links.defer_sync);
Saravana Kannan26e77702019-11-14 14:56:45 -0800811 __device_links_queue_sync_state(dev, &sync_list);
Saravana Kannanfc5a2512019-09-04 14:11:23 -0700812 }
813out:
814 device_links_write_unlock();
Saravana Kannan26e77702019-11-14 14:56:45 -0800815
Saravana Kannan21eb93f2020-02-21 00:05:08 -0800816 device_links_flush_sync_list(&sync_list, NULL);
Saravana Kannanfc5a2512019-09-04 14:11:23 -0700817}
818
819static int sync_state_resume_initcall(void)
820{
821 device_links_supplier_sync_state_resume();
822 return 0;
823}
824late_initcall(sync_state_resume_initcall);
825
826static void __device_links_supplier_defer_sync(struct device *sup)
827{
Saravana Kannan77036162020-02-21 00:05:10 -0800828 if (list_empty(&sup->links.defer_sync) && dev_has_sync_state(sup))
Saravana Kannanfc5a2512019-09-04 14:11:23 -0700829 list_add_tail(&sup->links.defer_sync, &deferred_sync);
830}
831
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100832/**
833 * device_links_driver_bound - Update device links after probing its driver.
834 * @dev: Device to update the links for.
835 *
836 * The probe has been successful, so update links from this device to any
837 * consumers by changing their status to "available".
838 *
839 * Also change the status of @dev's links to suppliers to "active".
840 *
Rafael J. Wysocki515db262019-07-16 17:21:06 +0200841 * Links without the DL_FLAG_MANAGED flag set are ignored.
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100842 */
843void device_links_driver_bound(struct device *dev)
844{
845 struct device_link *link;
Saravana Kannan26e77702019-11-14 14:56:45 -0800846 LIST_HEAD(sync_list);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100847
Saravana Kannanbcbbcfd2019-10-28 15:00:23 -0700848 /*
849 * If a device probes successfully, it's expected to have created all
850 * the device links it needs to or make new device links as it needs
851 * them. So, it no longer needs to wait on any suppliers.
852 */
853 mutex_lock(&wfs_lock);
854 list_del_init(&dev->links.needs_suppliers);
855 mutex_unlock(&wfs_lock);
856
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100857 device_links_write_lock();
858
859 list_for_each_entry(link, &dev->links.consumers, s_node) {
Rafael J. Wysocki515db262019-07-16 17:21:06 +0200860 if (!(link->flags & DL_FLAG_MANAGED))
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100861 continue;
862
Rafael J. Wysocki15cfb092019-02-01 01:50:39 +0100863 /*
864 * Links created during consumer probe may be in the "consumer
865 * probe" state to start with if the supplier is still probing
866 * when they are created and they may become "active" if the
867 * consumer probe returns first. Skip them here.
868 */
869 if (link->status == DL_STATE_CONSUMER_PROBE ||
870 link->status == DL_STATE_ACTIVE)
871 continue;
872
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100873 WARN_ON(link->status != DL_STATE_DORMANT);
874 WRITE_ONCE(link->status, DL_STATE_AVAILABLE);
Rafael J. Wysockie7dd4012019-02-01 01:59:42 +0100875
876 if (link->flags & DL_FLAG_AUTOPROBE_CONSUMER)
877 driver_deferred_probe_add(link->consumer);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100878 }
879
Saravana Kannan21eb93f2020-02-21 00:05:08 -0800880 if (defer_sync_state_count)
881 __device_links_supplier_defer_sync(dev);
882 else
883 __device_links_queue_sync_state(dev, &sync_list);
884
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100885 list_for_each_entry(link, &dev->links.suppliers, c_node) {
Rafael J. Wysocki515db262019-07-16 17:21:06 +0200886 if (!(link->flags & DL_FLAG_MANAGED))
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100887 continue;
888
889 WARN_ON(link->status != DL_STATE_CONSUMER_PROBE);
890 WRITE_ONCE(link->status, DL_STATE_ACTIVE);
Saravana Kannanfc5a2512019-09-04 14:11:23 -0700891
892 if (defer_sync_state_count)
893 __device_links_supplier_defer_sync(link->supplier);
894 else
Saravana Kannan26e77702019-11-14 14:56:45 -0800895 __device_links_queue_sync_state(link->supplier,
896 &sync_list);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100897 }
898
899 dev->links.status = DL_DEV_DRIVER_BOUND;
900
901 device_links_write_unlock();
Saravana Kannan26e77702019-11-14 14:56:45 -0800902
Saravana Kannan21eb93f2020-02-21 00:05:08 -0800903 device_links_flush_sync_list(&sync_list, dev);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100904}
905
Rafael J. Wysocki515db262019-07-16 17:21:06 +0200906static void device_link_drop_managed(struct device_link *link)
907{
908 link->flags &= ~DL_FLAG_MANAGED;
909 WRITE_ONCE(link->status, DL_STATE_NONE);
910 kref_put(&link->kref, __device_link_del);
911}
912
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100913/**
914 * __device_links_no_driver - Update links of a device without a driver.
915 * @dev: Device without a drvier.
916 *
917 * Delete all non-persistent links from this device to any suppliers.
918 *
919 * Persistent links stay around, but their status is changed to "available",
920 * unless they already are in the "supplier unbind in progress" state in which
921 * case they need not be updated.
922 *
Rafael J. Wysocki515db262019-07-16 17:21:06 +0200923 * Links without the DL_FLAG_MANAGED flag set are ignored.
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100924 */
925static void __device_links_no_driver(struct device *dev)
926{
927 struct device_link *link, *ln;
928
929 list_for_each_entry_safe_reverse(link, ln, &dev->links.suppliers, c_node) {
Rafael J. Wysocki515db262019-07-16 17:21:06 +0200930 if (!(link->flags & DL_FLAG_MANAGED))
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100931 continue;
932
Vivek Gautame88728f2018-06-27 18:20:55 +0530933 if (link->flags & DL_FLAG_AUTOREMOVE_CONSUMER)
Rafael J. Wysocki515db262019-07-16 17:21:06 +0200934 device_link_drop_managed(link);
Rafael J. Wysocki15cfb092019-02-01 01:50:39 +0100935 else if (link->status == DL_STATE_CONSUMER_PROBE ||
936 link->status == DL_STATE_ACTIVE)
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100937 WRITE_ONCE(link->status, DL_STATE_AVAILABLE);
938 }
939
940 dev->links.status = DL_DEV_NO_DRIVER;
941}
942
Rafael J. Wysocki15cfb092019-02-01 01:50:39 +0100943/**
944 * device_links_no_driver - Update links after failing driver probe.
945 * @dev: Device whose driver has just failed to probe.
946 *
947 * Clean up leftover links to consumers for @dev and invoke
948 * %__device_links_no_driver() to update links to suppliers for it as
949 * appropriate.
950 *
Rafael J. Wysocki515db262019-07-16 17:21:06 +0200951 * Links without the DL_FLAG_MANAGED flag set are ignored.
Rafael J. Wysocki15cfb092019-02-01 01:50:39 +0100952 */
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100953void device_links_no_driver(struct device *dev)
954{
Rafael J. Wysocki15cfb092019-02-01 01:50:39 +0100955 struct device_link *link;
956
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100957 device_links_write_lock();
Rafael J. Wysocki15cfb092019-02-01 01:50:39 +0100958
959 list_for_each_entry(link, &dev->links.consumers, s_node) {
Rafael J. Wysocki515db262019-07-16 17:21:06 +0200960 if (!(link->flags & DL_FLAG_MANAGED))
Rafael J. Wysocki15cfb092019-02-01 01:50:39 +0100961 continue;
962
963 /*
964 * The probe has failed, so if the status of the link is
965 * "consumer probe" or "active", it must have been added by
966 * a probing consumer while this device was still probing.
967 * Change its state to "dormant", as it represents a valid
968 * relationship, but it is not functionally meaningful.
969 */
970 if (link->status == DL_STATE_CONSUMER_PROBE ||
971 link->status == DL_STATE_ACTIVE)
972 WRITE_ONCE(link->status, DL_STATE_DORMANT);
973 }
974
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100975 __device_links_no_driver(dev);
Rafael J. Wysocki15cfb092019-02-01 01:50:39 +0100976
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100977 device_links_write_unlock();
978}
979
980/**
981 * device_links_driver_cleanup - Update links after driver removal.
982 * @dev: Device whose driver has just gone away.
983 *
984 * Update links to consumers for @dev by changing their status to "dormant" and
985 * invoke %__device_links_no_driver() to update links to suppliers for it as
986 * appropriate.
987 *
Rafael J. Wysocki515db262019-07-16 17:21:06 +0200988 * Links without the DL_FLAG_MANAGED flag set are ignored.
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100989 */
990void device_links_driver_cleanup(struct device *dev)
991{
Rafael J. Wysockic8d50982019-02-01 01:45:55 +0100992 struct device_link *link, *ln;
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100993
994 device_links_write_lock();
995
Rafael J. Wysockic8d50982019-02-01 01:45:55 +0100996 list_for_each_entry_safe(link, ln, &dev->links.consumers, s_node) {
Rafael J. Wysocki515db262019-07-16 17:21:06 +0200997 if (!(link->flags & DL_FLAG_MANAGED))
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100998 continue;
999
Vivek Gautame88728f2018-06-27 18:20:55 +05301000 WARN_ON(link->flags & DL_FLAG_AUTOREMOVE_CONSUMER);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001001 WARN_ON(link->status != DL_STATE_SUPPLIER_UNBIND);
Vivek Gautam1689cac2018-06-27 18:20:56 +05301002
1003 /*
1004 * autoremove the links between this @dev and its consumer
1005 * devices that are not active, i.e. where the link state
1006 * has moved to DL_STATE_SUPPLIER_UNBIND.
1007 */
1008 if (link->status == DL_STATE_SUPPLIER_UNBIND &&
1009 link->flags & DL_FLAG_AUTOREMOVE_SUPPLIER)
Rafael J. Wysocki515db262019-07-16 17:21:06 +02001010 device_link_drop_managed(link);
Vivek Gautam1689cac2018-06-27 18:20:56 +05301011
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001012 WRITE_ONCE(link->status, DL_STATE_DORMANT);
1013 }
1014
Saravana Kannanfc5a2512019-09-04 14:11:23 -07001015 list_del_init(&dev->links.defer_sync);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001016 __device_links_no_driver(dev);
1017
1018 device_links_write_unlock();
1019}
1020
1021/**
1022 * device_links_busy - Check if there are any busy links to consumers.
1023 * @dev: Device to check.
1024 *
1025 * Check each consumer of the device and return 'true' if its link's status
1026 * is one of "consumer probe" or "active" (meaning that the given consumer is
1027 * probing right now or its driver is present). Otherwise, change the link
1028 * state to "supplier unbind" to prevent the consumer from being probed
1029 * successfully going forward.
1030 *
1031 * Return 'false' if there are no probing or active consumers.
1032 *
Rafael J. Wysocki515db262019-07-16 17:21:06 +02001033 * Links without the DL_FLAG_MANAGED flag set are ignored.
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001034 */
1035bool device_links_busy(struct device *dev)
1036{
1037 struct device_link *link;
1038 bool ret = false;
1039
1040 device_links_write_lock();
1041
1042 list_for_each_entry(link, &dev->links.consumers, s_node) {
Rafael J. Wysocki515db262019-07-16 17:21:06 +02001043 if (!(link->flags & DL_FLAG_MANAGED))
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001044 continue;
1045
1046 if (link->status == DL_STATE_CONSUMER_PROBE
1047 || link->status == DL_STATE_ACTIVE) {
1048 ret = true;
1049 break;
1050 }
1051 WRITE_ONCE(link->status, DL_STATE_SUPPLIER_UNBIND);
1052 }
1053
1054 dev->links.status = DL_DEV_UNBINDING;
1055
1056 device_links_write_unlock();
1057 return ret;
1058}
1059
1060/**
1061 * device_links_unbind_consumers - Force unbind consumers of the given device.
1062 * @dev: Device to unbind the consumers of.
1063 *
1064 * Walk the list of links to consumers for @dev and if any of them is in the
1065 * "consumer probe" state, wait for all device probes in progress to complete
1066 * and start over.
1067 *
1068 * If that's not the case, change the status of the link to "supplier unbind"
1069 * and check if the link was in the "active" state. If so, force the consumer
1070 * driver to unbind and start over (the consumer will not re-probe as we have
1071 * changed the state of the link already).
1072 *
Rafael J. Wysocki515db262019-07-16 17:21:06 +02001073 * Links without the DL_FLAG_MANAGED flag set are ignored.
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001074 */
1075void device_links_unbind_consumers(struct device *dev)
1076{
1077 struct device_link *link;
1078
1079 start:
1080 device_links_write_lock();
1081
1082 list_for_each_entry(link, &dev->links.consumers, s_node) {
1083 enum device_link_state status;
1084
Saravana Kannan05ef9832019-10-28 15:00:22 -07001085 if (!(link->flags & DL_FLAG_MANAGED) ||
1086 link->flags & DL_FLAG_SYNC_STATE_ONLY)
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001087 continue;
1088
1089 status = link->status;
1090 if (status == DL_STATE_CONSUMER_PROBE) {
1091 device_links_write_unlock();
1092
1093 wait_for_device_probe();
1094 goto start;
1095 }
1096 WRITE_ONCE(link->status, DL_STATE_SUPPLIER_UNBIND);
1097 if (status == DL_STATE_ACTIVE) {
1098 struct device *consumer = link->consumer;
1099
1100 get_device(consumer);
1101
1102 device_links_write_unlock();
1103
1104 device_release_driver_internal(consumer, NULL,
1105 consumer->parent);
1106 put_device(consumer);
1107 goto start;
1108 }
1109 }
1110
1111 device_links_write_unlock();
1112}
1113
1114/**
1115 * device_links_purge - Delete existing links to other devices.
1116 * @dev: Target device.
1117 */
1118static void device_links_purge(struct device *dev)
1119{
1120 struct device_link *link, *ln;
1121
Saravana Kannane2ae9bc2019-09-04 14:11:21 -07001122 mutex_lock(&wfs_lock);
1123 list_del(&dev->links.needs_suppliers);
1124 mutex_unlock(&wfs_lock);
1125
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001126 /*
1127 * Delete all of the remaining links from this device to any other
1128 * devices (either consumers or suppliers).
1129 */
1130 device_links_write_lock();
1131
1132 list_for_each_entry_safe_reverse(link, ln, &dev->links.suppliers, c_node) {
1133 WARN_ON(link->status == DL_STATE_ACTIVE);
Lukas Wunneread18c22018-02-10 19:27:12 +01001134 __device_link_del(&link->kref);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001135 }
1136
1137 list_for_each_entry_safe_reverse(link, ln, &dev->links.consumers, s_node) {
1138 WARN_ON(link->status != DL_STATE_DORMANT &&
1139 link->status != DL_STATE_NONE);
Lukas Wunneread18c22018-02-10 19:27:12 +01001140 __device_link_del(&link->kref);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001141 }
1142
1143 device_links_write_unlock();
1144}
1145
Saravana Kannan42926ac2020-05-14 22:34:57 -07001146static u32 fw_devlink_flags = DL_FLAG_SYNC_STATE_ONLY;
1147static int __init fw_devlink_setup(char *arg)
1148{
1149 if (!arg)
1150 return -EINVAL;
1151
1152 if (strcmp(arg, "off") == 0) {
1153 fw_devlink_flags = 0;
1154 } else if (strcmp(arg, "permissive") == 0) {
1155 fw_devlink_flags = DL_FLAG_SYNC_STATE_ONLY;
1156 } else if (strcmp(arg, "on") == 0) {
1157 fw_devlink_flags = DL_FLAG_AUTOPROBE_CONSUMER;
1158 } else if (strcmp(arg, "rpm") == 0) {
1159 fw_devlink_flags = DL_FLAG_AUTOPROBE_CONSUMER |
1160 DL_FLAG_PM_RUNTIME;
1161 }
1162 return 0;
1163}
1164early_param("fw_devlink", fw_devlink_setup);
1165
1166u32 fw_devlink_get_flags(void)
1167{
1168 return fw_devlink_flags;
1169}
1170
1171static bool fw_devlink_is_permissive(void)
1172{
1173 return fw_devlink_flags == DL_FLAG_SYNC_STATE_ONLY;
1174}
1175
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001176/* Device links support end. */
1177
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001178int (*platform_notify)(struct device *dev) = NULL;
1179int (*platform_notify_remove)(struct device *dev) = NULL;
Dan Williamse105b8b2008-04-21 10:51:07 -07001180static struct kobject *dev_kobj;
1181struct kobject *sysfs_dev_char_kobj;
1182struct kobject *sysfs_dev_block_kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183
Rafael J. Wysocki5e33bc42013-08-28 21:41:01 +02001184static DEFINE_MUTEX(device_hotplug_lock);
1185
1186void lock_device_hotplug(void)
1187{
1188 mutex_lock(&device_hotplug_lock);
1189}
1190
1191void unlock_device_hotplug(void)
1192{
1193 mutex_unlock(&device_hotplug_lock);
1194}
1195
1196int lock_device_hotplug_sysfs(void)
1197{
1198 if (mutex_trylock(&device_hotplug_lock))
1199 return 0;
1200
1201 /* Avoid busy looping (5 ms of sleep should do). */
1202 msleep(5);
1203 return restart_syscall();
1204}
1205
Greg Kroah-Hartman4e886c22008-01-27 12:12:43 -08001206#ifdef CONFIG_BLOCK
1207static inline int device_is_not_partition(struct device *dev)
1208{
1209 return !(dev->type == &part_type);
1210}
1211#else
1212static inline int device_is_not_partition(struct device *dev)
1213{
1214 return 1;
1215}
1216#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217
Heikki Krogerus07de0e82018-11-09 17:21:34 +03001218static int
1219device_platform_notify(struct device *dev, enum kobject_action action)
1220{
Heikki Krogerus7847a142018-11-09 17:21:35 +03001221 int ret;
1222
1223 ret = acpi_platform_notify(dev, action);
1224 if (ret)
1225 return ret;
1226
Heikki Krogerus59abd832018-11-09 17:21:36 +03001227 ret = software_node_notify(dev, action);
1228 if (ret)
1229 return ret;
1230
Heikki Krogerus07de0e82018-11-09 17:21:34 +03001231 if (platform_notify && action == KOBJ_ADD)
1232 platform_notify(dev);
1233 else if (platform_notify_remove && action == KOBJ_REMOVE)
1234 platform_notify_remove(dev);
1235 return 0;
1236}
1237
Alan Stern3e956372006-06-16 17:10:48 -04001238/**
1239 * dev_driver_string - Return a device's driver name, if at all possible
1240 * @dev: struct device to get the name of
1241 *
1242 * Will return the device's driver's name if it is bound to a device. If
yan9169c012012-04-20 21:08:45 +08001243 * the device is not bound to a driver, it will return the name of the bus
Alan Stern3e956372006-06-16 17:10:48 -04001244 * it is attached to. If it is not attached to a bus either, an empty
1245 * string will be returned.
1246 */
Jean Delvarebf9ca692008-07-30 12:29:21 -07001247const char *dev_driver_string(const struct device *dev)
Alan Stern3e956372006-06-16 17:10:48 -04001248{
Alan Stern35899722009-12-04 11:06:57 -05001249 struct device_driver *drv;
1250
1251 /* dev->driver can change to NULL underneath us because of unbinding,
1252 * so be careful about accessing it. dev->bus and dev->class should
1253 * never change once they are set, so they don't need special care.
1254 */
Mark Rutland6aa7de02017-10-23 14:07:29 -07001255 drv = READ_ONCE(dev->driver);
Alan Stern35899722009-12-04 11:06:57 -05001256 return drv ? drv->name :
Jean Delvarea456b702007-03-09 16:33:10 +01001257 (dev->bus ? dev->bus->name :
1258 (dev->class ? dev->class->name : ""));
Alan Stern3e956372006-06-16 17:10:48 -04001259}
Matthew Wilcox310a9222006-09-23 23:35:04 -06001260EXPORT_SYMBOL(dev_driver_string);
Alan Stern3e956372006-06-16 17:10:48 -04001261
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262#define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr)
1263
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001264static ssize_t dev_attr_show(struct kobject *kobj, struct attribute *attr,
1265 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001267 struct device_attribute *dev_attr = to_dev_attr(attr);
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +02001268 struct device *dev = kobj_to_dev(kobj);
Dmitry Torokhov4a0c20b2005-04-29 01:23:47 -05001269 ssize_t ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270
1271 if (dev_attr->show)
Yani Ioannou54b6f352005-05-17 06:39:34 -04001272 ret = dev_attr->show(dev, dev_attr, buf);
Andrew Morton815d2d52008-03-04 15:09:07 -08001273 if (ret >= (ssize_t)PAGE_SIZE) {
Sergey Senozhatskya52668c2017-12-11 21:50:21 +09001274 printk("dev_attr_show: %pS returned bad count\n",
1275 dev_attr->show);
Andrew Morton815d2d52008-03-04 15:09:07 -08001276 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 return ret;
1278}
1279
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001280static ssize_t dev_attr_store(struct kobject *kobj, struct attribute *attr,
1281 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001283 struct device_attribute *dev_attr = to_dev_attr(attr);
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +02001284 struct device *dev = kobj_to_dev(kobj);
Dmitry Torokhov4a0c20b2005-04-29 01:23:47 -05001285 ssize_t ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286
1287 if (dev_attr->store)
Yani Ioannou54b6f352005-05-17 06:39:34 -04001288 ret = dev_attr->store(dev, dev_attr, buf, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 return ret;
1290}
1291
Emese Revfy52cf25d2010-01-19 02:58:23 +01001292static const struct sysfs_ops dev_sysfs_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 .show = dev_attr_show,
1294 .store = dev_attr_store,
1295};
1296
Kay Sieversca22e562011-12-14 14:29:38 -08001297#define to_ext_attr(x) container_of(x, struct dev_ext_attribute, attr)
1298
1299ssize_t device_store_ulong(struct device *dev,
1300 struct device_attribute *attr,
1301 const char *buf, size_t size)
1302{
1303 struct dev_ext_attribute *ea = to_ext_attr(attr);
Kaitao chengf88184b2018-11-06 08:34:54 -08001304 int ret;
1305 unsigned long new;
1306
1307 ret = kstrtoul(buf, 0, &new);
1308 if (ret)
1309 return ret;
Kay Sieversca22e562011-12-14 14:29:38 -08001310 *(unsigned long *)(ea->var) = new;
1311 /* Always return full write size even if we didn't consume all */
1312 return size;
1313}
1314EXPORT_SYMBOL_GPL(device_store_ulong);
1315
1316ssize_t device_show_ulong(struct device *dev,
1317 struct device_attribute *attr,
1318 char *buf)
1319{
1320 struct dev_ext_attribute *ea = to_ext_attr(attr);
1321 return snprintf(buf, PAGE_SIZE, "%lx\n", *(unsigned long *)(ea->var));
1322}
1323EXPORT_SYMBOL_GPL(device_show_ulong);
1324
1325ssize_t device_store_int(struct device *dev,
1326 struct device_attribute *attr,
1327 const char *buf, size_t size)
1328{
1329 struct dev_ext_attribute *ea = to_ext_attr(attr);
Kaitao chengf88184b2018-11-06 08:34:54 -08001330 int ret;
1331 long new;
1332
1333 ret = kstrtol(buf, 0, &new);
1334 if (ret)
1335 return ret;
1336
1337 if (new > INT_MAX || new < INT_MIN)
Kay Sieversca22e562011-12-14 14:29:38 -08001338 return -EINVAL;
1339 *(int *)(ea->var) = new;
1340 /* Always return full write size even if we didn't consume all */
1341 return size;
1342}
1343EXPORT_SYMBOL_GPL(device_store_int);
1344
1345ssize_t device_show_int(struct device *dev,
1346 struct device_attribute *attr,
1347 char *buf)
1348{
1349 struct dev_ext_attribute *ea = to_ext_attr(attr);
1350
1351 return snprintf(buf, PAGE_SIZE, "%d\n", *(int *)(ea->var));
1352}
1353EXPORT_SYMBOL_GPL(device_show_int);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354
Borislav Petkov91872392012-10-09 19:52:05 +02001355ssize_t device_store_bool(struct device *dev, struct device_attribute *attr,
1356 const char *buf, size_t size)
1357{
1358 struct dev_ext_attribute *ea = to_ext_attr(attr);
1359
1360 if (strtobool(buf, ea->var) < 0)
1361 return -EINVAL;
1362
1363 return size;
1364}
1365EXPORT_SYMBOL_GPL(device_store_bool);
1366
1367ssize_t device_show_bool(struct device *dev, struct device_attribute *attr,
1368 char *buf)
1369{
1370 struct dev_ext_attribute *ea = to_ext_attr(attr);
1371
1372 return snprintf(buf, PAGE_SIZE, "%d\n", *(bool *)(ea->var));
1373}
1374EXPORT_SYMBOL_GPL(device_show_bool);
1375
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376/**
Robert P. J. Dayf8878dc2013-06-01 20:17:34 -04001377 * device_release - free device structure.
1378 * @kobj: device's kobject.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 *
Robert P. J. Dayf8878dc2013-06-01 20:17:34 -04001380 * This is called once the reference count for the object
1381 * reaches 0. We forward the call to the device's release
1382 * method, which should handle actually freeing the structure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001384static void device_release(struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +02001386 struct device *dev = kobj_to_dev(kobj);
Greg Kroah-Hartmanfb069a52008-12-16 12:23:36 -08001387 struct device_private *p = dev->p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388
Ming Leia525a3d2012-07-25 01:42:29 +08001389 /*
1390 * Some platform devices are driven without driver attached
1391 * and managed resources may have been acquired. Make sure
1392 * all resources are released.
1393 *
1394 * Drivers still can add resources into device after device
1395 * is deleted but alive, so release devres here to avoid
1396 * possible memory leak.
1397 */
1398 devres_release_all(dev);
1399
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 if (dev->release)
1401 dev->release(dev);
Kay Sieversf9f852d2006-10-07 21:54:55 +02001402 else if (dev->type && dev->type->release)
1403 dev->type->release(dev);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07001404 else if (dev->class && dev->class->dev_release)
1405 dev->class->dev_release(dev);
Arjan van de Venf810a5c2008-07-25 19:45:39 -07001406 else
Ezequiel Garcia186bddb2018-12-03 13:44:35 -03001407 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 +01001408 dev_name(dev));
Greg Kroah-Hartmanfb069a52008-12-16 12:23:36 -08001409 kfree(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410}
1411
Eric W. Biedermanbc451f22010-03-30 11:31:25 -07001412static const void *device_namespace(struct kobject *kobj)
1413{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +02001414 struct device *dev = kobj_to_dev(kobj);
Eric W. Biedermanbc451f22010-03-30 11:31:25 -07001415 const void *ns = NULL;
1416
1417 if (dev->class && dev->class->ns_type)
1418 ns = dev->class->namespace(dev);
1419
1420 return ns;
1421}
1422
Dmitry Torokhov9944e892018-07-20 21:56:50 +00001423static void device_get_ownership(struct kobject *kobj, kuid_t *uid, kgid_t *gid)
1424{
1425 struct device *dev = kobj_to_dev(kobj);
1426
1427 if (dev->class && dev->class->get_ownership)
1428 dev->class->get_ownership(dev, uid, gid);
1429}
1430
Greg Kroah-Hartman8f4afc42007-10-11 10:47:49 -06001431static struct kobj_type device_ktype = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 .release = device_release,
1433 .sysfs_ops = &dev_sysfs_ops,
Eric W. Biedermanbc451f22010-03-30 11:31:25 -07001434 .namespace = device_namespace,
Dmitry Torokhov9944e892018-07-20 21:56:50 +00001435 .get_ownership = device_get_ownership,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436};
1437
1438
Kay Sievers312c0042005-11-16 09:00:00 +01001439static int dev_uevent_filter(struct kset *kset, struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440{
1441 struct kobj_type *ktype = get_ktype(kobj);
1442
Greg Kroah-Hartman8f4afc42007-10-11 10:47:49 -06001443 if (ktype == &device_ktype) {
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +02001444 struct device *dev = kobj_to_dev(kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 if (dev->bus)
1446 return 1;
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001447 if (dev->class)
1448 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 }
1450 return 0;
1451}
1452
Kay Sievers312c0042005-11-16 09:00:00 +01001453static const char *dev_uevent_name(struct kset *kset, struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +02001455 struct device *dev = kobj_to_dev(kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001457 if (dev->bus)
1458 return dev->bus->name;
1459 if (dev->class)
1460 return dev->class->name;
1461 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462}
1463
Kay Sievers7eff2e72007-08-14 15:15:12 +02001464static int dev_uevent(struct kset *kset, struct kobject *kobj,
1465 struct kobj_uevent_env *env)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +02001467 struct device *dev = kobj_to_dev(kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 int retval = 0;
1469
Kay Sievers6fcf53a2009-04-30 15:23:42 +02001470 /* add device node properties if present */
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001471 if (MAJOR(dev->devt)) {
Kay Sievers6fcf53a2009-04-30 15:23:42 +02001472 const char *tmp;
1473 const char *name;
Al Viro2c9ede52011-07-23 20:24:48 -04001474 umode_t mode = 0;
Greg Kroah-Hartman4e4098a2013-04-11 11:43:29 -07001475 kuid_t uid = GLOBAL_ROOT_UID;
1476 kgid_t gid = GLOBAL_ROOT_GID;
Kay Sievers6fcf53a2009-04-30 15:23:42 +02001477
Kay Sievers7eff2e72007-08-14 15:15:12 +02001478 add_uevent_var(env, "MAJOR=%u", MAJOR(dev->devt));
1479 add_uevent_var(env, "MINOR=%u", MINOR(dev->devt));
Kay Sievers3c2670e2013-04-06 09:56:00 -07001480 name = device_get_devnode(dev, &mode, &uid, &gid, &tmp);
Kay Sievers6fcf53a2009-04-30 15:23:42 +02001481 if (name) {
1482 add_uevent_var(env, "DEVNAME=%s", name);
Kay Sieverse454cea2009-09-18 23:01:12 +02001483 if (mode)
1484 add_uevent_var(env, "DEVMODE=%#o", mode & 0777);
Greg Kroah-Hartman4e4098a2013-04-11 11:43:29 -07001485 if (!uid_eq(uid, GLOBAL_ROOT_UID))
1486 add_uevent_var(env, "DEVUID=%u", from_kuid(&init_user_ns, uid));
1487 if (!gid_eq(gid, GLOBAL_ROOT_GID))
1488 add_uevent_var(env, "DEVGID=%u", from_kgid(&init_user_ns, gid));
Kay Sievers3c2670e2013-04-06 09:56:00 -07001489 kfree(tmp);
Kay Sievers6fcf53a2009-04-30 15:23:42 +02001490 }
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001491 }
1492
Kay Sievers414264f2007-03-12 21:08:57 +01001493 if (dev->type && dev->type->name)
Kay Sievers7eff2e72007-08-14 15:15:12 +02001494 add_uevent_var(env, "DEVTYPE=%s", dev->type->name);
Kay Sievers414264f2007-03-12 21:08:57 +01001495
Kay Sievers239378f2006-10-07 21:54:55 +02001496 if (dev->driver)
Kay Sievers7eff2e72007-08-14 15:15:12 +02001497 add_uevent_var(env, "DRIVER=%s", dev->driver->name);
Kay Sievers239378f2006-10-07 21:54:55 +02001498
Grant Likely07d57a32012-02-01 11:22:22 -07001499 /* Add common DT information about the device */
1500 of_device_uevent(dev, env);
1501
Kay Sievers7eff2e72007-08-14 15:15:12 +02001502 /* have the bus specific function add its stuff */
Kay Sievers312c0042005-11-16 09:00:00 +01001503 if (dev->bus && dev->bus->uevent) {
Kay Sievers7eff2e72007-08-14 15:15:12 +02001504 retval = dev->bus->uevent(dev, env);
Kay Sieversf9f852d2006-10-07 21:54:55 +02001505 if (retval)
Greg Kroah-Hartman7dc72b22007-11-28 23:49:41 -08001506 pr_debug("device: '%s': %s: bus uevent() returned %d\n",
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01001507 dev_name(dev), __func__, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 }
1509
Kay Sievers7eff2e72007-08-14 15:15:12 +02001510 /* have the class specific function add its stuff */
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07001511 if (dev->class && dev->class->dev_uevent) {
Kay Sievers7eff2e72007-08-14 15:15:12 +02001512 retval = dev->class->dev_uevent(dev, env);
Kay Sieversf9f852d2006-10-07 21:54:55 +02001513 if (retval)
Greg Kroah-Hartman7dc72b22007-11-28 23:49:41 -08001514 pr_debug("device: '%s': %s: class uevent() "
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01001515 "returned %d\n", dev_name(dev),
Harvey Harrison2b3a3022008-03-04 16:41:05 -08001516 __func__, retval);
Kay Sieversf9f852d2006-10-07 21:54:55 +02001517 }
1518
Stefan Weileef35c22010-08-06 21:11:15 +02001519 /* have the device type specific function add its stuff */
Kay Sieversf9f852d2006-10-07 21:54:55 +02001520 if (dev->type && dev->type->uevent) {
Kay Sievers7eff2e72007-08-14 15:15:12 +02001521 retval = dev->type->uevent(dev, env);
Kay Sieversf9f852d2006-10-07 21:54:55 +02001522 if (retval)
Greg Kroah-Hartman7dc72b22007-11-28 23:49:41 -08001523 pr_debug("device: '%s': %s: dev_type uevent() "
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01001524 "returned %d\n", dev_name(dev),
Harvey Harrison2b3a3022008-03-04 16:41:05 -08001525 __func__, retval);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07001526 }
1527
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 return retval;
1529}
1530
Emese Revfy9cd43612009-12-31 14:52:51 +01001531static const struct kset_uevent_ops device_uevent_ops = {
Kay Sievers312c0042005-11-16 09:00:00 +01001532 .filter = dev_uevent_filter,
1533 .name = dev_uevent_name,
1534 .uevent = dev_uevent,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535};
1536
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07001537static ssize_t uevent_show(struct device *dev, struct device_attribute *attr,
Kay Sievers16574dc2007-04-06 01:40:38 +02001538 char *buf)
1539{
1540 struct kobject *top_kobj;
1541 struct kset *kset;
Kay Sievers7eff2e72007-08-14 15:15:12 +02001542 struct kobj_uevent_env *env = NULL;
Kay Sievers16574dc2007-04-06 01:40:38 +02001543 int i;
1544 size_t count = 0;
1545 int retval;
1546
1547 /* search the kset, the device belongs to */
1548 top_kobj = &dev->kobj;
Kay Sievers5c5daf62007-08-12 20:43:55 +02001549 while (!top_kobj->kset && top_kobj->parent)
1550 top_kobj = top_kobj->parent;
Kay Sievers16574dc2007-04-06 01:40:38 +02001551 if (!top_kobj->kset)
1552 goto out;
Kay Sievers5c5daf62007-08-12 20:43:55 +02001553
Kay Sievers16574dc2007-04-06 01:40:38 +02001554 kset = top_kobj->kset;
1555 if (!kset->uevent_ops || !kset->uevent_ops->uevent)
1556 goto out;
1557
1558 /* respect filter */
1559 if (kset->uevent_ops && kset->uevent_ops->filter)
1560 if (!kset->uevent_ops->filter(kset, &dev->kobj))
1561 goto out;
1562
Kay Sievers7eff2e72007-08-14 15:15:12 +02001563 env = kzalloc(sizeof(struct kobj_uevent_env), GFP_KERNEL);
1564 if (!env)
Greg Kroah-Hartmanc7308c82007-05-02 14:14:11 +02001565 return -ENOMEM;
1566
Kay Sievers16574dc2007-04-06 01:40:38 +02001567 /* let the kset specific function add its keys */
Kay Sievers7eff2e72007-08-14 15:15:12 +02001568 retval = kset->uevent_ops->uevent(kset, &dev->kobj, env);
Kay Sievers16574dc2007-04-06 01:40:38 +02001569 if (retval)
1570 goto out;
1571
1572 /* copy keys to file */
Kay Sievers7eff2e72007-08-14 15:15:12 +02001573 for (i = 0; i < env->envp_idx; i++)
1574 count += sprintf(&buf[count], "%s\n", env->envp[i]);
Kay Sievers16574dc2007-04-06 01:40:38 +02001575out:
Kay Sievers7eff2e72007-08-14 15:15:12 +02001576 kfree(env);
Kay Sievers16574dc2007-04-06 01:40:38 +02001577 return count;
1578}
1579
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07001580static ssize_t uevent_store(struct device *dev, struct device_attribute *attr,
Kay Sieversa7fd6702005-10-01 14:49:43 +02001581 const char *buf, size_t count)
1582{
Peter Rajnohadf44b472018-12-05 12:27:44 +01001583 int rc;
1584
1585 rc = kobject_synth_uevent(&dev->kobj, buf, count);
1586
1587 if (rc) {
Peter Rajnohaf36776f2017-05-09 15:22:30 +02001588 dev_err(dev, "uevent: failed to send synthetic uevent\n");
Peter Rajnohadf44b472018-12-05 12:27:44 +01001589 return rc;
1590 }
Kay Sievers60a96a52007-07-08 22:29:26 +02001591
Kay Sieversa7fd6702005-10-01 14:49:43 +02001592 return count;
1593}
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07001594static DEVICE_ATTR_RW(uevent);
Kay Sieversa7fd6702005-10-01 14:49:43 +02001595
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07001596static ssize_t online_show(struct device *dev, struct device_attribute *attr,
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02001597 char *buf)
1598{
1599 bool val;
1600
Rafael J. Wysocki5e33bc42013-08-28 21:41:01 +02001601 device_lock(dev);
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02001602 val = !dev->offline;
Rafael J. Wysocki5e33bc42013-08-28 21:41:01 +02001603 device_unlock(dev);
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02001604 return sprintf(buf, "%u\n", val);
1605}
1606
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07001607static ssize_t online_store(struct device *dev, struct device_attribute *attr,
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02001608 const char *buf, size_t count)
1609{
1610 bool val;
1611 int ret;
1612
1613 ret = strtobool(buf, &val);
1614 if (ret < 0)
1615 return ret;
1616
Rafael J. Wysocki5e33bc42013-08-28 21:41:01 +02001617 ret = lock_device_hotplug_sysfs();
1618 if (ret)
1619 return ret;
1620
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02001621 ret = val ? device_online(dev) : device_offline(dev);
1622 unlock_device_hotplug();
1623 return ret < 0 ? ret : count;
1624}
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07001625static DEVICE_ATTR_RW(online);
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02001626
Greg Kroah-Hartmanfa6fdb32013-08-08 15:22:55 -07001627int device_add_groups(struct device *dev, const struct attribute_group **groups)
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001628{
Greg Kroah-Hartman3e9b2ba2013-08-21 13:47:50 -07001629 return sysfs_create_groups(&dev->kobj, groups);
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001630}
Dmitry Torokhova7670d42017-07-19 17:24:31 -07001631EXPORT_SYMBOL_GPL(device_add_groups);
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001632
Greg Kroah-Hartmanfa6fdb32013-08-08 15:22:55 -07001633void device_remove_groups(struct device *dev,
1634 const struct attribute_group **groups)
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001635{
Greg Kroah-Hartman3e9b2ba2013-08-21 13:47:50 -07001636 sysfs_remove_groups(&dev->kobj, groups);
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -07001637}
Dmitry Torokhova7670d42017-07-19 17:24:31 -07001638EXPORT_SYMBOL_GPL(device_remove_groups);
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -07001639
Dmitry Torokhov57b8ff02017-07-19 17:24:33 -07001640union device_attr_group_devres {
1641 const struct attribute_group *group;
1642 const struct attribute_group **groups;
1643};
1644
1645static int devm_attr_group_match(struct device *dev, void *res, void *data)
1646{
1647 return ((union device_attr_group_devres *)res)->group == data;
1648}
1649
1650static void devm_attr_group_remove(struct device *dev, void *res)
1651{
1652 union device_attr_group_devres *devres = res;
1653 const struct attribute_group *group = devres->group;
1654
1655 dev_dbg(dev, "%s: removing group %p\n", __func__, group);
1656 sysfs_remove_group(&dev->kobj, group);
1657}
1658
1659static void devm_attr_groups_remove(struct device *dev, void *res)
1660{
1661 union device_attr_group_devres *devres = res;
1662 const struct attribute_group **groups = devres->groups;
1663
1664 dev_dbg(dev, "%s: removing groups %p\n", __func__, groups);
1665 sysfs_remove_groups(&dev->kobj, groups);
1666}
1667
1668/**
1669 * devm_device_add_group - given a device, create a managed attribute group
1670 * @dev: The device to create the group for
1671 * @grp: The attribute group to create
1672 *
1673 * This function creates a group for the first time. It will explicitly
1674 * warn and error if any of the attribute files being created already exist.
1675 *
1676 * Returns 0 on success or error code on failure.
1677 */
1678int devm_device_add_group(struct device *dev, const struct attribute_group *grp)
1679{
1680 union device_attr_group_devres *devres;
1681 int error;
1682
1683 devres = devres_alloc(devm_attr_group_remove,
1684 sizeof(*devres), GFP_KERNEL);
1685 if (!devres)
1686 return -ENOMEM;
1687
1688 error = sysfs_create_group(&dev->kobj, grp);
1689 if (error) {
1690 devres_free(devres);
1691 return error;
1692 }
1693
1694 devres->group = grp;
1695 devres_add(dev, devres);
1696 return 0;
1697}
1698EXPORT_SYMBOL_GPL(devm_device_add_group);
1699
1700/**
1701 * devm_device_remove_group: remove a managed group from a device
1702 * @dev: device to remove the group from
1703 * @grp: group to remove
1704 *
1705 * This function removes a group of attributes from a device. The attributes
1706 * previously have to have been created for this group, otherwise it will fail.
1707 */
1708void devm_device_remove_group(struct device *dev,
1709 const struct attribute_group *grp)
1710{
1711 WARN_ON(devres_release(dev, devm_attr_group_remove,
1712 devm_attr_group_match,
1713 /* cast away const */ (void *)grp));
1714}
1715EXPORT_SYMBOL_GPL(devm_device_remove_group);
1716
1717/**
1718 * devm_device_add_groups - create a bunch of managed attribute groups
1719 * @dev: The device to create the group for
1720 * @groups: The attribute groups to create, NULL terminated
1721 *
1722 * This function creates a bunch of managed attribute groups. If an error
1723 * occurs when creating a group, all previously created groups will be
1724 * removed, unwinding everything back to the original state when this
1725 * function was called. It will explicitly warn and error if any of the
1726 * attribute files being created already exist.
1727 *
1728 * Returns 0 on success or error code from sysfs_create_group on failure.
1729 */
1730int devm_device_add_groups(struct device *dev,
1731 const struct attribute_group **groups)
1732{
1733 union device_attr_group_devres *devres;
1734 int error;
1735
1736 devres = devres_alloc(devm_attr_groups_remove,
1737 sizeof(*devres), GFP_KERNEL);
1738 if (!devres)
1739 return -ENOMEM;
1740
1741 error = sysfs_create_groups(&dev->kobj, groups);
1742 if (error) {
1743 devres_free(devres);
1744 return error;
1745 }
1746
1747 devres->groups = groups;
1748 devres_add(dev, devres);
1749 return 0;
1750}
1751EXPORT_SYMBOL_GPL(devm_device_add_groups);
1752
1753/**
1754 * devm_device_remove_groups - remove a list of managed groups
1755 *
1756 * @dev: The device for the groups to be removed from
1757 * @groups: NULL terminated list of groups to be removed
1758 *
1759 * If groups is not NULL, remove the specified groups from the device.
1760 */
1761void devm_device_remove_groups(struct device *dev,
1762 const struct attribute_group **groups)
1763{
1764 WARN_ON(devres_release(dev, devm_attr_groups_remove,
1765 devm_attr_group_match,
1766 /* cast away const */ (void *)groups));
1767}
1768EXPORT_SYMBOL_GPL(devm_device_remove_groups);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07001770static int device_add_attrs(struct device *dev)
1771{
1772 struct class *class = dev->class;
Stephen Hemmingeraed65af2011-03-28 09:12:52 -07001773 const struct device_type *type = dev->type;
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001774 int error;
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07001775
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001776 if (class) {
Greg Kroah-Hartmand05a6f92013-07-14 16:05:58 -07001777 error = device_add_groups(dev, class->dev_groups);
Kay Sieversf9f852d2006-10-07 21:54:55 +02001778 if (error)
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001779 return error;
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07001780 }
Kay Sieversf9f852d2006-10-07 21:54:55 +02001781
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001782 if (type) {
1783 error = device_add_groups(dev, type->groups);
Kay Sieversf9f852d2006-10-07 21:54:55 +02001784 if (error)
Greg Kroah-Hartmana6b01de2013-10-05 18:19:30 -07001785 goto err_remove_class_groups;
Kay Sieversf9f852d2006-10-07 21:54:55 +02001786 }
1787
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001788 error = device_add_groups(dev, dev->groups);
1789 if (error)
1790 goto err_remove_type_groups;
1791
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02001792 if (device_supports_offline(dev) && !dev->offline_disabled) {
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07001793 error = device_create_file(dev, &dev_attr_online);
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02001794 if (error)
Rafael J. Wysockiecfbf6f2013-12-12 06:11:02 +01001795 goto err_remove_dev_groups;
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02001796 }
1797
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001798 return 0;
1799
Rafael J. Wysockiecfbf6f2013-12-12 06:11:02 +01001800 err_remove_dev_groups:
1801 device_remove_groups(dev, dev->groups);
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001802 err_remove_type_groups:
1803 if (type)
1804 device_remove_groups(dev, type->groups);
Greg Kroah-Hartmand05a6f92013-07-14 16:05:58 -07001805 err_remove_class_groups:
1806 if (class)
1807 device_remove_groups(dev, class->dev_groups);
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001808
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07001809 return error;
1810}
1811
1812static void device_remove_attrs(struct device *dev)
1813{
1814 struct class *class = dev->class;
Stephen Hemmingeraed65af2011-03-28 09:12:52 -07001815 const struct device_type *type = dev->type;
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07001816
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07001817 device_remove_file(dev, &dev_attr_online);
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001818 device_remove_groups(dev, dev->groups);
Kay Sieversf9f852d2006-10-07 21:54:55 +02001819
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001820 if (type)
1821 device_remove_groups(dev, type->groups);
1822
Greg Kroah-Hartmana6b01de2013-10-05 18:19:30 -07001823 if (class)
Greg Kroah-Hartmand05a6f92013-07-14 16:05:58 -07001824 device_remove_groups(dev, class->dev_groups);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07001825}
1826
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07001827static ssize_t dev_show(struct device *dev, struct device_attribute *attr,
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001828 char *buf)
1829{
1830 return print_dev_t(buf, dev->devt);
1831}
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07001832static DEVICE_ATTR_RO(dev);
Tejun Heoad6a1e12007-06-14 03:45:17 +09001833
Kay Sieversca22e562011-12-14 14:29:38 -08001834/* /sys/devices/ */
Greg Kroah-Hartman881c6cfd2007-11-01 09:29:06 -06001835struct kset *devices_kset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837/**
Grygorii Strashko52cdbdd2015-07-27 20:43:01 +03001838 * devices_kset_move_before - Move device in the devices_kset's list.
1839 * @deva: Device to move.
1840 * @devb: Device @deva should come before.
1841 */
1842static void devices_kset_move_before(struct device *deva, struct device *devb)
1843{
1844 if (!devices_kset)
1845 return;
1846 pr_debug("devices_kset: Moving %s before %s\n",
1847 dev_name(deva), dev_name(devb));
1848 spin_lock(&devices_kset->list_lock);
1849 list_move_tail(&deva->kobj.entry, &devb->kobj.entry);
1850 spin_unlock(&devices_kset->list_lock);
1851}
1852
1853/**
1854 * devices_kset_move_after - Move device in the devices_kset's list.
1855 * @deva: Device to move
1856 * @devb: Device @deva should come after.
1857 */
1858static void devices_kset_move_after(struct device *deva, struct device *devb)
1859{
1860 if (!devices_kset)
1861 return;
1862 pr_debug("devices_kset: Moving %s after %s\n",
1863 dev_name(deva), dev_name(devb));
1864 spin_lock(&devices_kset->list_lock);
1865 list_move(&deva->kobj.entry, &devb->kobj.entry);
1866 spin_unlock(&devices_kset->list_lock);
1867}
1868
1869/**
1870 * devices_kset_move_last - move the device to the end of devices_kset's list.
1871 * @dev: device to move
1872 */
1873void devices_kset_move_last(struct device *dev)
1874{
1875 if (!devices_kset)
1876 return;
1877 pr_debug("devices_kset: Moving %s to end of list\n", dev_name(dev));
1878 spin_lock(&devices_kset->list_lock);
1879 list_move_tail(&dev->kobj.entry, &devices_kset->list);
1880 spin_unlock(&devices_kset->list_lock);
1881}
1882
1883/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001884 * device_create_file - create sysfs attribute file for device.
1885 * @dev: device.
1886 * @attr: device attribute descriptor.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 */
Phil Carmody26579ab2009-12-18 15:34:19 +02001888int device_create_file(struct device *dev,
1889 const struct device_attribute *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890{
1891 int error = 0;
Felipe Balbi8f46baa2013-02-20 10:31:42 +02001892
1893 if (dev) {
1894 WARN(((attr->attr.mode & S_IWUGO) && !attr->store),
dyoung@redhat.com97521972013-05-16 14:31:30 +08001895 "Attribute %s: write permission without 'store'\n",
1896 attr->attr.name);
Felipe Balbi8f46baa2013-02-20 10:31:42 +02001897 WARN(((attr->attr.mode & S_IRUGO) && !attr->show),
dyoung@redhat.com97521972013-05-16 14:31:30 +08001898 "Attribute %s: read permission without 'show'\n",
1899 attr->attr.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 error = sysfs_create_file(&dev->kobj, &attr->attr);
Felipe Balbi8f46baa2013-02-20 10:31:42 +02001901 }
1902
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 return error;
1904}
David Graham White86df2682013-07-21 20:41:14 -04001905EXPORT_SYMBOL_GPL(device_create_file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906
1907/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001908 * device_remove_file - remove sysfs attribute file.
1909 * @dev: device.
1910 * @attr: device attribute descriptor.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 */
Phil Carmody26579ab2009-12-18 15:34:19 +02001912void device_remove_file(struct device *dev,
1913 const struct device_attribute *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914{
Cornelia Huck0c98b192008-01-31 10:39:38 +01001915 if (dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 sysfs_remove_file(&dev->kobj, &attr->attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917}
David Graham White86df2682013-07-21 20:41:14 -04001918EXPORT_SYMBOL_GPL(device_remove_file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919
Greg Kroah-Hartman2589f1882006-09-19 09:39:19 -07001920/**
Tejun Heo6b0afc22014-02-03 14:03:01 -05001921 * device_remove_file_self - remove sysfs attribute file from its own method.
1922 * @dev: device.
1923 * @attr: device attribute descriptor.
1924 *
1925 * See kernfs_remove_self() for details.
1926 */
1927bool device_remove_file_self(struct device *dev,
1928 const struct device_attribute *attr)
1929{
1930 if (dev)
1931 return sysfs_remove_file_self(&dev->kobj, &attr->attr);
1932 else
1933 return false;
1934}
1935EXPORT_SYMBOL_GPL(device_remove_file_self);
1936
1937/**
Greg Kroah-Hartman2589f1882006-09-19 09:39:19 -07001938 * device_create_bin_file - create sysfs binary attribute file for device.
1939 * @dev: device.
1940 * @attr: device binary attribute descriptor.
1941 */
Phil Carmody66ecb922009-12-18 15:34:20 +02001942int device_create_bin_file(struct device *dev,
1943 const struct bin_attribute *attr)
Greg Kroah-Hartman2589f1882006-09-19 09:39:19 -07001944{
1945 int error = -EINVAL;
1946 if (dev)
1947 error = sysfs_create_bin_file(&dev->kobj, attr);
1948 return error;
1949}
1950EXPORT_SYMBOL_GPL(device_create_bin_file);
1951
1952/**
1953 * device_remove_bin_file - remove sysfs binary attribute file
1954 * @dev: device.
1955 * @attr: device binary attribute descriptor.
1956 */
Phil Carmody66ecb922009-12-18 15:34:20 +02001957void device_remove_bin_file(struct device *dev,
1958 const struct bin_attribute *attr)
Greg Kroah-Hartman2589f1882006-09-19 09:39:19 -07001959{
1960 if (dev)
1961 sysfs_remove_bin_file(&dev->kobj, attr);
1962}
1963EXPORT_SYMBOL_GPL(device_remove_bin_file);
1964
James Bottomley34bb61f2005-09-06 16:56:51 -07001965static void klist_children_get(struct klist_node *n)
1966{
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08001967 struct device_private *p = to_device_private_parent(n);
1968 struct device *dev = p->device;
James Bottomley34bb61f2005-09-06 16:56:51 -07001969
1970 get_device(dev);
1971}
1972
1973static void klist_children_put(struct klist_node *n)
1974{
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08001975 struct device_private *p = to_device_private_parent(n);
1976 struct device *dev = p->device;
James Bottomley34bb61f2005-09-06 16:56:51 -07001977
1978 put_device(dev);
1979}
1980
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001982 * device_initialize - init device structure.
1983 * @dev: device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 *
Cornelia Huck57394112008-09-03 18:26:40 +02001985 * This prepares the device for use by other layers by initializing
1986 * its fields.
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001987 * It is the first half of device_register(), if called by
Cornelia Huck57394112008-09-03 18:26:40 +02001988 * that function, though it can also be called separately, so one
1989 * may use @dev's fields. In particular, get_device()/put_device()
1990 * may be used for reference counting of @dev after calling this
1991 * function.
1992 *
Alan Sternb10d5ef2012-01-17 11:39:00 -05001993 * All fields in @dev must be initialized by the caller to 0, except
1994 * for those explicitly set to some other value. The simplest
1995 * approach is to use kzalloc() to allocate the structure containing
1996 * @dev.
1997 *
Cornelia Huck57394112008-09-03 18:26:40 +02001998 * NOTE: Use put_device() to give up your reference instead of freeing
1999 * @dev directly once you have called this function.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001void device_initialize(struct device *dev)
2002{
Greg Kroah-Hartman881c6cfd2007-11-01 09:29:06 -06002003 dev->kobj.kset = devices_kset;
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -07002004 kobject_init(&dev->kobj, &device_ktype);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 INIT_LIST_HEAD(&dev->dma_pools);
Thomas Gleixner31427882010-01-29 20:39:02 +00002006 mutex_init(&dev->mutex);
Dan Williams87a30e12019-07-17 18:08:26 -07002007#ifdef CONFIG_PROVE_LOCKING
2008 mutex_init(&dev->lockdep_mutex);
2009#endif
Peter Zijlstra1704f472010-03-19 01:37:42 +01002010 lockdep_set_novalidate_class(&dev->mutex);
Tejun Heo9ac78492007-01-20 16:00:26 +09002011 spin_lock_init(&dev->devres_lock);
2012 INIT_LIST_HEAD(&dev->devres_head);
Alan Stern3b98aea2008-08-07 13:06:12 -04002013 device_pm_init(dev);
Christoph Hellwig87348132006-12-06 20:32:33 -08002014 set_dev_node(dev, -1);
Jiang Liu4a7cc832015-07-09 16:00:44 +08002015#ifdef CONFIG_GENERIC_MSI_IRQ
2016 INIT_LIST_HEAD(&dev->msi_list);
2017#endif
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01002018 INIT_LIST_HEAD(&dev->links.consumers);
2019 INIT_LIST_HEAD(&dev->links.suppliers);
Saravana Kannane2ae9bc2019-09-04 14:11:21 -07002020 INIT_LIST_HEAD(&dev->links.needs_suppliers);
Saravana Kannanfc5a2512019-09-04 14:11:23 -07002021 INIT_LIST_HEAD(&dev->links.defer_sync);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01002022 dev->links.status = DL_DEV_NO_DRIVER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023}
David Graham White86df2682013-07-21 20:41:14 -04002024EXPORT_SYMBOL_GPL(device_initialize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025
Tejun Heod73ce002013-03-12 11:30:05 -07002026struct kobject *virtual_device_parent(struct device *dev)
Greg Kroah-Hartmanf0ee61a2006-10-23 10:40:54 -07002027{
Kay Sievers86406242007-03-14 03:25:56 +01002028 static struct kobject *virtual_dir = NULL;
Greg Kroah-Hartmanf0ee61a2006-10-23 10:40:54 -07002029
Kay Sievers86406242007-03-14 03:25:56 +01002030 if (!virtual_dir)
Greg Kroah-Hartman4ff6abf2007-11-05 22:24:43 -08002031 virtual_dir = kobject_create_and_add("virtual",
Greg Kroah-Hartman881c6cfd2007-11-01 09:29:06 -06002032 &devices_kset->kobj);
Greg Kroah-Hartmanf0ee61a2006-10-23 10:40:54 -07002033
Kay Sievers86406242007-03-14 03:25:56 +01002034 return virtual_dir;
Greg Kroah-Hartmanf0ee61a2006-10-23 10:40:54 -07002035}
2036
Eric W. Biedermanbc451f22010-03-30 11:31:25 -07002037struct class_dir {
2038 struct kobject kobj;
2039 struct class *class;
2040};
2041
2042#define to_class_dir(obj) container_of(obj, struct class_dir, kobj)
2043
2044static void class_dir_release(struct kobject *kobj)
2045{
2046 struct class_dir *dir = to_class_dir(kobj);
2047 kfree(dir);
2048}
2049
2050static const
2051struct kobj_ns_type_operations *class_dir_child_ns_type(struct kobject *kobj)
2052{
2053 struct class_dir *dir = to_class_dir(kobj);
2054 return dir->class->ns_type;
2055}
2056
2057static struct kobj_type class_dir_ktype = {
2058 .release = class_dir_release,
2059 .sysfs_ops = &kobj_sysfs_ops,
2060 .child_ns_type = class_dir_child_ns_type
2061};
2062
2063static struct kobject *
2064class_dir_create_and_add(struct class *class, struct kobject *parent_kobj)
2065{
2066 struct class_dir *dir;
2067 int retval;
2068
2069 dir = kzalloc(sizeof(*dir), GFP_KERNEL);
2070 if (!dir)
Tetsuo Handa84d0c272018-05-07 19:10:31 +09002071 return ERR_PTR(-ENOMEM);
Eric W. Biedermanbc451f22010-03-30 11:31:25 -07002072
2073 dir->class = class;
2074 kobject_init(&dir->kobj, &class_dir_ktype);
2075
Kay Sievers6b6e39a2010-11-15 23:13:18 +01002076 dir->kobj.kset = &class->p->glue_dirs;
Eric W. Biedermanbc451f22010-03-30 11:31:25 -07002077
2078 retval = kobject_add(&dir->kobj, parent_kobj, "%s", class->name);
2079 if (retval < 0) {
2080 kobject_put(&dir->kobj);
Tetsuo Handa84d0c272018-05-07 19:10:31 +09002081 return ERR_PTR(retval);
Eric W. Biedermanbc451f22010-03-30 11:31:25 -07002082 }
2083 return &dir->kobj;
2084}
2085
Yijing Wange4a60d12014-11-07 12:05:49 +08002086static DEFINE_MUTEX(gdp_mutex);
Eric W. Biedermanbc451f22010-03-30 11:31:25 -07002087
Kay Sieversda231fd2007-11-21 17:29:15 +01002088static struct kobject *get_device_parent(struct device *dev,
2089 struct device *parent)
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +01002090{
Kay Sievers86406242007-03-14 03:25:56 +01002091 if (dev->class) {
2092 struct kobject *kobj = NULL;
2093 struct kobject *parent_kobj;
2094 struct kobject *k;
2095
Randy Dunlapead454f2010-09-24 14:36:49 -07002096#ifdef CONFIG_BLOCK
Kay Sievers39aba962010-09-04 22:33:14 -07002097 /* block disks show up in /sys/block */
Andi Kleene52eec12010-09-08 16:54:17 +02002098 if (sysfs_deprecated && dev->class == &block_class) {
Kay Sievers39aba962010-09-04 22:33:14 -07002099 if (parent && parent->class == &block_class)
2100 return &parent->kobj;
Kay Sievers6b6e39a2010-11-15 23:13:18 +01002101 return &block_class.p->subsys.kobj;
Kay Sievers39aba962010-09-04 22:33:14 -07002102 }
Randy Dunlapead454f2010-09-24 14:36:49 -07002103#endif
Andi Kleene52eec12010-09-08 16:54:17 +02002104
Kay Sievers86406242007-03-14 03:25:56 +01002105 /*
2106 * If we have no parent, we live in "virtual".
Kay Sievers0f4dafc2007-12-19 01:40:42 +01002107 * Class-devices with a non class-device as parent, live
2108 * in a "glue" directory to prevent namespace collisions.
Kay Sievers86406242007-03-14 03:25:56 +01002109 */
2110 if (parent == NULL)
2111 parent_kobj = virtual_device_parent(dev);
Eric W. Biederman24b14422010-07-24 22:43:35 -07002112 else if (parent->class && !dev->class->ns_type)
Kay Sievers86406242007-03-14 03:25:56 +01002113 return &parent->kobj;
2114 else
2115 parent_kobj = &parent->kobj;
2116
Tejun Heo77d3d7c2010-02-05 17:57:02 +09002117 mutex_lock(&gdp_mutex);
2118
Kay Sievers86406242007-03-14 03:25:56 +01002119 /* find our class-directory at the parent and reference it */
Kay Sievers6b6e39a2010-11-15 23:13:18 +01002120 spin_lock(&dev->class->p->glue_dirs.list_lock);
2121 list_for_each_entry(k, &dev->class->p->glue_dirs.list, entry)
Kay Sievers86406242007-03-14 03:25:56 +01002122 if (k->parent == parent_kobj) {
2123 kobj = kobject_get(k);
2124 break;
2125 }
Kay Sievers6b6e39a2010-11-15 23:13:18 +01002126 spin_unlock(&dev->class->p->glue_dirs.list_lock);
Tejun Heo77d3d7c2010-02-05 17:57:02 +09002127 if (kobj) {
2128 mutex_unlock(&gdp_mutex);
Kay Sievers86406242007-03-14 03:25:56 +01002129 return kobj;
Tejun Heo77d3d7c2010-02-05 17:57:02 +09002130 }
Kay Sievers86406242007-03-14 03:25:56 +01002131
2132 /* or create a new class-directory at the parent device */
Eric W. Biedermanbc451f22010-03-30 11:31:25 -07002133 k = class_dir_create_and_add(dev->class, parent_kobj);
Kay Sievers0f4dafc2007-12-19 01:40:42 +01002134 /* do not emit an uevent for this simple "glue" directory */
Tejun Heo77d3d7c2010-02-05 17:57:02 +09002135 mutex_unlock(&gdp_mutex);
Greg Kroah-Hartman43968d22007-11-05 22:24:43 -08002136 return k;
Kay Sievers86406242007-03-14 03:25:56 +01002137 }
2138
Kay Sieversca22e562011-12-14 14:29:38 -08002139 /* subsystems can specify a default root directory for their devices */
2140 if (!parent && dev->bus && dev->bus->dev_root)
2141 return &dev->bus->dev_root->kobj;
2142
Kay Sievers86406242007-03-14 03:25:56 +01002143 if (parent)
Cornelia Huckc744aeae2007-01-08 20:16:44 +01002144 return &parent->kobj;
2145 return NULL;
2146}
Kay Sieversda231fd2007-11-21 17:29:15 +01002147
Ming Leicebf8fd2016-07-10 19:27:36 +08002148static inline bool live_in_glue_dir(struct kobject *kobj,
2149 struct device *dev)
2150{
2151 if (!kobj || !dev->class ||
2152 kobj->kset != &dev->class->p->glue_dirs)
2153 return false;
2154 return true;
2155}
2156
2157static inline struct kobject *get_glue_dir(struct device *dev)
2158{
2159 return dev->kobj.parent;
2160}
2161
2162/*
2163 * make sure cleaning up dir as the last step, we need to make
2164 * sure .release handler of kobject is run with holding the
2165 * global lock
2166 */
Cornelia Huck63b69712008-01-21 16:09:44 +01002167static void cleanup_glue_dir(struct device *dev, struct kobject *glue_dir)
Kay Sieversda231fd2007-11-21 17:29:15 +01002168{
Muchun Songac434322019-07-27 11:21:22 +08002169 unsigned int ref;
2170
Kay Sievers0f4dafc2007-12-19 01:40:42 +01002171 /* see if we live in a "glue" directory */
Ming Leicebf8fd2016-07-10 19:27:36 +08002172 if (!live_in_glue_dir(glue_dir, dev))
Kay Sieversda231fd2007-11-21 17:29:15 +01002173 return;
2174
Yijing Wange4a60d12014-11-07 12:05:49 +08002175 mutex_lock(&gdp_mutex);
Muchun Songac434322019-07-27 11:21:22 +08002176 /**
2177 * There is a race condition between removing glue directory
2178 * and adding a new device under the glue directory.
2179 *
2180 * CPU1: CPU2:
2181 *
2182 * device_add()
2183 * get_device_parent()
2184 * class_dir_create_and_add()
2185 * kobject_add_internal()
2186 * create_dir() // create glue_dir
2187 *
2188 * device_add()
2189 * get_device_parent()
2190 * kobject_get() // get glue_dir
2191 *
2192 * device_del()
2193 * cleanup_glue_dir()
2194 * kobject_del(glue_dir)
2195 *
2196 * kobject_add()
2197 * kobject_add_internal()
2198 * create_dir() // in glue_dir
2199 * sysfs_create_dir_ns()
2200 * kernfs_create_dir_ns(sd)
2201 *
2202 * sysfs_remove_dir() // glue_dir->sd=NULL
2203 * sysfs_put() // free glue_dir->sd
2204 *
2205 * // sd is freed
2206 * kernfs_new_node(sd)
2207 * kernfs_get(glue_dir)
2208 * kernfs_add_one()
2209 * kernfs_put()
2210 *
2211 * Before CPU1 remove last child device under glue dir, if CPU2 add
2212 * a new device under glue dir, the glue_dir kobject reference count
2213 * will be increase to 2 in kobject_get(k). And CPU2 has been called
2214 * kernfs_create_dir_ns(). Meanwhile, CPU1 call sysfs_remove_dir()
2215 * and sysfs_put(). This result in glue_dir->sd is freed.
2216 *
2217 * Then the CPU2 will see a stale "empty" but still potentially used
2218 * glue dir around in kernfs_new_node().
2219 *
2220 * In order to avoid this happening, we also should make sure that
2221 * kernfs_node for glue_dir is released in CPU1 only when refcount
2222 * for glue_dir kobj is 1.
2223 */
2224 ref = kref_read(&glue_dir->kref);
2225 if (!kobject_has_children(glue_dir) && !--ref)
Benjamin Herrenschmidt726e4102018-07-10 10:29:10 +10002226 kobject_del(glue_dir);
Kay Sievers0f4dafc2007-12-19 01:40:42 +01002227 kobject_put(glue_dir);
Yijing Wange4a60d12014-11-07 12:05:49 +08002228 mutex_unlock(&gdp_mutex);
Kay Sieversda231fd2007-11-21 17:29:15 +01002229}
Cornelia Huck63b69712008-01-21 16:09:44 +01002230
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07002231static int device_add_class_symlinks(struct device *dev)
2232{
Benjamin Herrenschmidt5590f312015-02-18 11:25:18 +11002233 struct device_node *of_node = dev_of_node(dev);
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07002234 int error;
2235
Benjamin Herrenschmidt5590f312015-02-18 11:25:18 +11002236 if (of_node) {
Rob Herring0c3c2342017-10-04 14:04:01 -05002237 error = sysfs_create_link(&dev->kobj, of_node_kobj(of_node), "of_node");
Benjamin Herrenschmidt5590f312015-02-18 11:25:18 +11002238 if (error)
2239 dev_warn(dev, "Error %d creating of_node link\n",error);
2240 /* An error here doesn't warrant bringing down the device */
2241 }
2242
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07002243 if (!dev->class)
2244 return 0;
Kay Sieversda231fd2007-11-21 17:29:15 +01002245
Greg Kroah-Hartman1fbfee62008-05-28 09:28:39 -07002246 error = sysfs_create_link(&dev->kobj,
Kay Sievers6b6e39a2010-11-15 23:13:18 +01002247 &dev->class->p->subsys.kobj,
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07002248 "subsystem");
2249 if (error)
Benjamin Herrenschmidt5590f312015-02-18 11:25:18 +11002250 goto out_devnode;
Kay Sieversda231fd2007-11-21 17:29:15 +01002251
Greg Kroah-Hartman4e886c22008-01-27 12:12:43 -08002252 if (dev->parent && device_is_not_partition(dev)) {
Dmitry Torokhov4f01a752007-09-18 22:46:50 -07002253 error = sysfs_create_link(&dev->kobj, &dev->parent->kobj,
2254 "device");
2255 if (error)
Kay Sievers39aba962010-09-04 22:33:14 -07002256 goto out_subsys;
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07002257 }
Kay Sievers39aba962010-09-04 22:33:14 -07002258
Randy Dunlapead454f2010-09-24 14:36:49 -07002259#ifdef CONFIG_BLOCK
Kay Sievers39aba962010-09-04 22:33:14 -07002260 /* /sys/block has directories and does not need symlinks */
Andi Kleene52eec12010-09-08 16:54:17 +02002261 if (sysfs_deprecated && dev->class == &block_class)
Kay Sievers39aba962010-09-04 22:33:14 -07002262 return 0;
Randy Dunlapead454f2010-09-24 14:36:49 -07002263#endif
Kay Sievers39aba962010-09-04 22:33:14 -07002264
2265 /* link in the class directory pointing to the device */
Kay Sievers6b6e39a2010-11-15 23:13:18 +01002266 error = sysfs_create_link(&dev->class->p->subsys.kobj,
Kay Sievers39aba962010-09-04 22:33:14 -07002267 &dev->kobj, dev_name(dev));
2268 if (error)
2269 goto out_device;
2270
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07002271 return 0;
2272
Kay Sievers39aba962010-09-04 22:33:14 -07002273out_device:
2274 sysfs_remove_link(&dev->kobj, "device");
Kay Sieversda231fd2007-11-21 17:29:15 +01002275
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07002276out_subsys:
2277 sysfs_remove_link(&dev->kobj, "subsystem");
Benjamin Herrenschmidt5590f312015-02-18 11:25:18 +11002278out_devnode:
2279 sysfs_remove_link(&dev->kobj, "of_node");
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07002280 return error;
2281}
2282
2283static void device_remove_class_symlinks(struct device *dev)
2284{
Benjamin Herrenschmidt5590f312015-02-18 11:25:18 +11002285 if (dev_of_node(dev))
2286 sysfs_remove_link(&dev->kobj, "of_node");
2287
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07002288 if (!dev->class)
2289 return;
Kay Sieversda231fd2007-11-21 17:29:15 +01002290
Greg Kroah-Hartman4e886c22008-01-27 12:12:43 -08002291 if (dev->parent && device_is_not_partition(dev))
Kay Sieversda231fd2007-11-21 17:29:15 +01002292 sysfs_remove_link(&dev->kobj, "device");
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07002293 sysfs_remove_link(&dev->kobj, "subsystem");
Randy Dunlapead454f2010-09-24 14:36:49 -07002294#ifdef CONFIG_BLOCK
Andi Kleene52eec12010-09-08 16:54:17 +02002295 if (sysfs_deprecated && dev->class == &block_class)
Kay Sievers39aba962010-09-04 22:33:14 -07002296 return;
Randy Dunlapead454f2010-09-24 14:36:49 -07002297#endif
Kay Sievers6b6e39a2010-11-15 23:13:18 +01002298 sysfs_delete_link(&dev->class->p->subsys.kobj, &dev->kobj, dev_name(dev));
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07002299}
2300
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301/**
Stephen Rothwell413c2392008-05-30 10:16:40 +10002302 * dev_set_name - set a device name
2303 * @dev: device
Randy Dunlap46232362008-06-04 21:40:43 -07002304 * @fmt: format string for the device's name
Stephen Rothwell413c2392008-05-30 10:16:40 +10002305 */
2306int dev_set_name(struct device *dev, const char *fmt, ...)
2307{
2308 va_list vargs;
Kay Sievers1fa5ae82009-01-25 15:17:37 +01002309 int err;
Stephen Rothwell413c2392008-05-30 10:16:40 +10002310
2311 va_start(vargs, fmt);
Kay Sievers1fa5ae82009-01-25 15:17:37 +01002312 err = kobject_set_name_vargs(&dev->kobj, fmt, vargs);
Stephen Rothwell413c2392008-05-30 10:16:40 +10002313 va_end(vargs);
Kay Sievers1fa5ae82009-01-25 15:17:37 +01002314 return err;
Stephen Rothwell413c2392008-05-30 10:16:40 +10002315}
2316EXPORT_SYMBOL_GPL(dev_set_name);
2317
2318/**
Dan Williamse105b8b2008-04-21 10:51:07 -07002319 * device_to_dev_kobj - select a /sys/dev/ directory for the device
2320 * @dev: device
2321 *
2322 * By default we select char/ for new entries. Setting class->dev_obj
2323 * to NULL prevents an entry from being created. class->dev_kobj must
2324 * be set (or cleared) before any devices are registered to the class
2325 * otherwise device_create_sys_dev_entry() and
Peter Korsgaard0d4e293c2012-04-17 12:12:57 +02002326 * device_remove_sys_dev_entry() will disagree about the presence of
2327 * the link.
Dan Williamse105b8b2008-04-21 10:51:07 -07002328 */
2329static struct kobject *device_to_dev_kobj(struct device *dev)
2330{
2331 struct kobject *kobj;
2332
2333 if (dev->class)
2334 kobj = dev->class->dev_kobj;
2335 else
2336 kobj = sysfs_dev_char_kobj;
2337
2338 return kobj;
2339}
2340
2341static int device_create_sys_dev_entry(struct device *dev)
2342{
2343 struct kobject *kobj = device_to_dev_kobj(dev);
2344 int error = 0;
2345 char devt_str[15];
2346
2347 if (kobj) {
2348 format_dev_t(devt_str, dev->devt);
2349 error = sysfs_create_link(kobj, &dev->kobj, devt_str);
2350 }
2351
2352 return error;
2353}
2354
2355static void device_remove_sys_dev_entry(struct device *dev)
2356{
2357 struct kobject *kobj = device_to_dev_kobj(dev);
2358 char devt_str[15];
2359
2360 if (kobj) {
2361 format_dev_t(devt_str, dev->devt);
2362 sysfs_remove_link(kobj, devt_str);
2363 }
2364}
2365
Shaokun Zhang46d3a032018-07-15 18:08:56 +08002366static int device_private_init(struct device *dev)
Greg Kroah-Hartmanb4028432009-05-11 14:16:57 -07002367{
2368 dev->p = kzalloc(sizeof(*dev->p), GFP_KERNEL);
2369 if (!dev->p)
2370 return -ENOMEM;
2371 dev->p->device = dev;
2372 klist_init(&dev->p->klist_children, klist_children_get,
2373 klist_children_put);
Greg Kroah-Hartmanef8a3fd2012-03-08 12:17:22 -08002374 INIT_LIST_HEAD(&dev->p->deferred_probe);
Greg Kroah-Hartmanb4028432009-05-11 14:16:57 -07002375 return 0;
2376}
2377
Dan Williamse105b8b2008-04-21 10:51:07 -07002378/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002379 * device_add - add device to device hierarchy.
2380 * @dev: device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002382 * This is part 2 of device_register(), though may be called
2383 * separately _iff_ device_initialize() has been called separately.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384 *
Cornelia Huck57394112008-09-03 18:26:40 +02002385 * This adds @dev to the kobject hierarchy via kobject_add(), adds it
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002386 * to the global and sibling lists for the device, then
2387 * adds it to the other relevant subsystems of the driver model.
Cornelia Huck57394112008-09-03 18:26:40 +02002388 *
Alan Sternb10d5ef2012-01-17 11:39:00 -05002389 * Do not call this routine or device_register() more than once for
2390 * any device structure. The driver model core is not designed to work
2391 * with devices that get unregistered and then spring back to life.
2392 * (Among other things, it's very hard to guarantee that all references
2393 * to the previous incarnation of @dev have been dropped.) Allocate
2394 * and register a fresh new struct device instead.
2395 *
Cornelia Huck57394112008-09-03 18:26:40 +02002396 * NOTE: _Never_ directly free @dev after calling this function, even
2397 * if it returned an error! Always use put_device() to give up your
2398 * reference instead.
Borislav Petkovaffada72019-04-18 19:41:56 +02002399 *
2400 * Rule of thumb is: if device_add() succeeds, you should call
2401 * device_del() when you want to get rid of it. If device_add() has
2402 * *not* succeeded, use *only* put_device() to drop the reference
2403 * count.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404 */
2405int device_add(struct device *dev)
2406{
Viresh Kumar35dbf4e2017-03-17 12:24:22 +05302407 struct device *parent;
Kay Sieversca22e562011-12-14 14:29:38 -08002408 struct kobject *kobj;
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02002409 struct class_interface *class_intf;
Saravana Kannan03324502019-10-28 15:00:24 -07002410 int error = -EINVAL, fw_ret;
Ming Leicebf8fd2016-07-10 19:27:36 +08002411 struct kobject *glue_dir = NULL;
Saravana Kannan4dbe1912020-03-20 21:54:48 -07002412 bool is_fwnode_dev = false;
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01002413
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414 dev = get_device(dev);
Greg Kroah-Hartmanc906a482008-05-30 10:45:12 -07002415 if (!dev)
2416 goto done;
2417
Greg Kroah-Hartmanfb069a52008-12-16 12:23:36 -08002418 if (!dev->p) {
Greg Kroah-Hartmanb4028432009-05-11 14:16:57 -07002419 error = device_private_init(dev);
2420 if (error)
2421 goto done;
Greg Kroah-Hartmanfb069a52008-12-16 12:23:36 -08002422 }
Greg Kroah-Hartmanfb069a52008-12-16 12:23:36 -08002423
Kay Sievers1fa5ae82009-01-25 15:17:37 +01002424 /*
2425 * for statically allocated devices, which should all be converted
2426 * some day, we need to initialize the name. We prevent reading back
2427 * the name, and force the use of dev_name()
2428 */
2429 if (dev->init_name) {
Greg Kroah-Hartmanacc0e902009-06-02 15:39:55 -07002430 dev_set_name(dev, "%s", dev->init_name);
Kay Sievers1fa5ae82009-01-25 15:17:37 +01002431 dev->init_name = NULL;
2432 }
Greg Kroah-Hartmanc906a482008-05-30 10:45:12 -07002433
Kay Sieversca22e562011-12-14 14:29:38 -08002434 /* subsystems can specify simple device enumeration */
2435 if (!dev_name(dev) && dev->bus && dev->bus->dev_name)
2436 dev_set_name(dev, "%s%u", dev->bus->dev_name, dev->id);
2437
Thomas Gleixnere6309e72009-12-10 19:32:49 +00002438 if (!dev_name(dev)) {
2439 error = -EINVAL;
Kay Sievers5c8563d2009-05-28 14:24:07 -07002440 goto name_error;
Thomas Gleixnere6309e72009-12-10 19:32:49 +00002441 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01002443 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
Greg Kroah-Hartmanc205ef42006-08-07 22:19:37 -07002444
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445 parent = get_device(dev->parent);
Kay Sieversca22e562011-12-14 14:29:38 -08002446 kobj = get_device_parent(dev, parent);
Tetsuo Handa84d0c272018-05-07 19:10:31 +09002447 if (IS_ERR(kobj)) {
2448 error = PTR_ERR(kobj);
2449 goto parent_error;
2450 }
Kay Sieversca22e562011-12-14 14:29:38 -08002451 if (kobj)
2452 dev->kobj.parent = kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453
Yinghai Lu0d358f22008-02-19 03:20:41 -08002454 /* use parent numa_node */
Zhen Lei56f2de82015-08-25 12:08:22 +08002455 if (parent && (dev_to_node(dev) == NUMA_NO_NODE))
Yinghai Lu0d358f22008-02-19 03:20:41 -08002456 set_dev_node(dev, dev_to_node(parent));
2457
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458 /* first, register with generic layer. */
Kay Sievers8a577ff2009-04-18 15:05:45 -07002459 /* we require the name to be set before, and pass NULL */
2460 error = kobject_add(&dev->kobj, dev->kobj.parent, NULL);
Ming Leicebf8fd2016-07-10 19:27:36 +08002461 if (error) {
2462 glue_dir = get_glue_dir(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463 goto Error;
Ming Leicebf8fd2016-07-10 19:27:36 +08002464 }
Kay Sieversa7fd6702005-10-01 14:49:43 +02002465
Brian Walsh37022642006-08-14 22:43:19 -07002466 /* notify platform of device entry */
Heikki Krogerus07de0e82018-11-09 17:21:34 +03002467 error = device_platform_notify(dev, KOBJ_ADD);
2468 if (error)
2469 goto platform_error;
Brian Walsh37022642006-08-14 22:43:19 -07002470
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07002471 error = device_create_file(dev, &dev_attr_uevent);
Cornelia Hucka306eea2006-09-22 11:37:13 +02002472 if (error)
2473 goto attrError;
Kay Sieversa7fd6702005-10-01 14:49:43 +02002474
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07002475 error = device_add_class_symlinks(dev);
2476 if (error)
2477 goto SymlinkError;
Cornelia Huckdc0afa82007-07-09 11:39:18 -07002478 error = device_add_attrs(dev);
2479 if (error)
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07002480 goto AttrsError;
Cornelia Huckdc0afa82007-07-09 11:39:18 -07002481 error = bus_add_device(dev);
2482 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483 goto BusError;
Alan Stern3b98aea2008-08-07 13:06:12 -04002484 error = dpm_sysfs_add(dev);
Rafael J. Wysocki57eee3d2008-03-12 00:59:38 +01002485 if (error)
Alan Stern3b98aea2008-08-07 13:06:12 -04002486 goto DPMError;
2487 device_pm_add(dev);
Alan Sternec0676ee2008-12-05 14:10:31 -05002488
Sergey Klyaus0cd75042014-10-08 11:31:54 +04002489 if (MAJOR(dev->devt)) {
2490 error = device_create_file(dev, &dev_attr_dev);
2491 if (error)
2492 goto DevAttrError;
2493
2494 error = device_create_sys_dev_entry(dev);
2495 if (error)
2496 goto SysEntryError;
2497
2498 devtmpfs_create_node(dev);
2499 }
2500
Alan Sternec0676ee2008-12-05 14:10:31 -05002501 /* Notify clients of device addition. This call must come
majianpeng268863f2012-01-11 15:12:06 +00002502 * after dpm_sysfs_add() and before kobject_uevent().
Alan Sternec0676ee2008-12-05 14:10:31 -05002503 */
2504 if (dev->bus)
2505 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
2506 BUS_NOTIFY_ADD_DEVICE, dev);
2507
Cornelia Huck83b5fb4c2007-03-29 11:12:11 +02002508 kobject_uevent(&dev->kobj, KOBJ_ADD);
Saravana Kannan372a67c2019-09-04 14:11:20 -07002509
Saravana Kannan4dbe1912020-03-20 21:54:48 -07002510 if (dev->fwnode && !dev->fwnode->dev) {
Saravana Kannan372a67c2019-09-04 14:11:20 -07002511 dev->fwnode->dev = dev;
Saravana Kannan4dbe1912020-03-20 21:54:48 -07002512 is_fwnode_dev = true;
2513 }
Saravana Kannan372a67c2019-09-04 14:11:20 -07002514
Saravana Kannane2ae9bc2019-09-04 14:11:21 -07002515 /*
2516 * Check if any of the other devices (consumers) have been waiting for
2517 * this device (supplier) to be added so that they can create a device
2518 * link to it.
2519 *
2520 * This needs to happen after device_pm_add() because device_link_add()
2521 * requires the supplier be registered before it's called.
2522 *
2523 * But this also needs to happe before bus_probe_device() to make sure
2524 * waiting consumers can link to it before the driver is bound to the
2525 * device and the driver sync_state callback is called for this device.
2526 */
2527 device_link_add_missing_supplier_links();
2528
Saravana Kannan4dbe1912020-03-20 21:54:48 -07002529 if (fw_devlink_flags && is_fwnode_dev &&
2530 fwnode_has_op(dev->fwnode, add_links)) {
Saravana Kannan03324502019-10-28 15:00:24 -07002531 fw_ret = fwnode_call_int_op(dev->fwnode, add_links, dev);
Saravana Kannan00b24752020-03-30 19:28:32 -07002532 if (fw_ret == -ENODEV && !fw_devlink_is_permissive())
Saravana Kannan03324502019-10-28 15:00:24 -07002533 device_link_wait_for_mandatory_supplier(dev);
2534 else if (fw_ret)
2535 device_link_wait_for_optional_supplier(dev);
2536 }
Saravana Kannane2ae9bc2019-09-04 14:11:21 -07002537
Alan Stern2023c612009-07-30 15:27:18 -04002538 bus_probe_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539 if (parent)
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08002540 klist_add_tail(&dev->p->knode_parent,
2541 &parent->p->klist_children);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542
Greg Kroah-Hartman5d9fd162006-06-22 17:17:32 -07002543 if (dev->class) {
Kay Sieversca22e562011-12-14 14:29:38 -08002544 mutex_lock(&dev->class->p->mutex);
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02002545 /* tie the class to the device */
Wei Yang570d0202019-01-18 10:34:59 +08002546 klist_add_tail(&dev->p->knode_class,
Kay Sievers6b6e39a2010-11-15 23:13:18 +01002547 &dev->class->p->klist_devices);
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02002548
2549 /* notify any interfaces that the device is here */
Greg Kroah-Hartman184f1f72008-05-28 09:28:39 -07002550 list_for_each_entry(class_intf,
Kay Sieversca22e562011-12-14 14:29:38 -08002551 &dev->class->p->interfaces, node)
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02002552 if (class_intf->add_dev)
2553 class_intf->add_dev(dev, class_intf);
Kay Sieversca22e562011-12-14 14:29:38 -08002554 mutex_unlock(&dev->class->p->mutex);
Greg Kroah-Hartman5d9fd162006-06-22 17:17:32 -07002555 }
Greg Kroah-Hartmanc906a482008-05-30 10:45:12 -07002556done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557 put_device(dev);
2558 return error;
Sergey Klyaus0cd75042014-10-08 11:31:54 +04002559 SysEntryError:
2560 if (MAJOR(dev->devt))
2561 device_remove_file(dev, &dev_attr_dev);
2562 DevAttrError:
2563 device_pm_remove(dev);
2564 dpm_sysfs_remove(dev);
Alan Stern3b98aea2008-08-07 13:06:12 -04002565 DPMError:
Rafael J. Wysocki57eee3d2008-03-12 00:59:38 +01002566 bus_remove_device(dev);
2567 BusError:
James Simmons82f0cf92007-02-21 17:44:51 +00002568 device_remove_attrs(dev);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07002569 AttrsError:
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07002570 device_remove_class_symlinks(dev);
2571 SymlinkError:
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07002572 device_remove_file(dev, &dev_attr_uevent);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07002573 attrError:
Heikki Krogerus07de0e82018-11-09 17:21:34 +03002574 device_platform_notify(dev, KOBJ_REMOVE);
2575platform_error:
Kay Sievers312c0042005-11-16 09:00:00 +01002576 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
Ming Leicebf8fd2016-07-10 19:27:36 +08002577 glue_dir = get_glue_dir(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578 kobject_del(&dev->kobj);
2579 Error:
Ming Leicebf8fd2016-07-10 19:27:36 +08002580 cleanup_glue_dir(dev, glue_dir);
Tetsuo Handa84d0c272018-05-07 19:10:31 +09002581parent_error:
Markus Elfring5f0163a2015-02-05 11:48:26 +01002582 put_device(parent);
Kay Sievers5c8563d2009-05-28 14:24:07 -07002583name_error:
2584 kfree(dev->p);
2585 dev->p = NULL;
Greg Kroah-Hartmanc906a482008-05-30 10:45:12 -07002586 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587}
David Graham White86df2682013-07-21 20:41:14 -04002588EXPORT_SYMBOL_GPL(device_add);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002591 * device_register - register a device with the system.
2592 * @dev: pointer to the device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002594 * This happens in two clean steps - initialize the device
2595 * and add it to the system. The two steps can be called
2596 * separately, but this is the easiest and most common.
2597 * I.e. you should only call the two helpers separately if
2598 * have a clearly defined need to use and refcount the device
2599 * before it is added to the hierarchy.
Cornelia Huck57394112008-09-03 18:26:40 +02002600 *
Alan Sternb10d5ef2012-01-17 11:39:00 -05002601 * For more information, see the kerneldoc for device_initialize()
2602 * and device_add().
2603 *
Cornelia Huck57394112008-09-03 18:26:40 +02002604 * NOTE: _Never_ directly free @dev after calling this function, even
2605 * if it returned an error! Always use put_device() to give up the
2606 * reference initialized in this function instead.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608int device_register(struct device *dev)
2609{
2610 device_initialize(dev);
2611 return device_add(dev);
2612}
David Graham White86df2682013-07-21 20:41:14 -04002613EXPORT_SYMBOL_GPL(device_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002616 * get_device - increment reference count for device.
2617 * @dev: device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002619 * This simply forwards the call to kobject_get(), though
2620 * we do take care to provide for the case that we get a NULL
2621 * pointer passed in.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002623struct device *get_device(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +02002625 return dev ? kobj_to_dev(kobject_get(&dev->kobj)) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626}
David Graham White86df2682013-07-21 20:41:14 -04002627EXPORT_SYMBOL_GPL(get_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002630 * put_device - decrement reference count.
2631 * @dev: device in question.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002633void put_device(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634{
Kay Sieversedfaa7c2007-05-21 22:08:01 +02002635 /* might_sleep(); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636 if (dev)
2637 kobject_put(&dev->kobj);
2638}
David Graham White86df2682013-07-21 20:41:14 -04002639EXPORT_SYMBOL_GPL(put_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640
Dan Williams00289cd2019-07-17 18:07:53 -07002641bool kill_device(struct device *dev)
2642{
2643 /*
2644 * Require the device lock and set the "dead" flag to guarantee that
2645 * the update behavior is consistent with the other bitfields near
2646 * it and that we cannot have an asynchronous probe routine trying
2647 * to run while we are tearing out the bus/class/sysfs from
2648 * underneath the device.
2649 */
2650 lockdep_assert_held(&dev->mutex);
2651
2652 if (dev->p->dead)
2653 return false;
2654 dev->p->dead = true;
2655 return true;
2656}
2657EXPORT_SYMBOL_GPL(kill_device);
2658
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002660 * device_del - delete device from system.
2661 * @dev: device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002663 * This is the first part of the device unregistration
2664 * sequence. This removes the device from the lists we control
2665 * from here, has it removed from the other driver model
2666 * subsystems it was added to in device_add(), and removes it
2667 * from the kobject hierarchy.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002669 * NOTE: this should be called manually _iff_ device_add() was
2670 * also called manually.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002672void device_del(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002674 struct device *parent = dev->parent;
Ming Leicebf8fd2016-07-10 19:27:36 +08002675 struct kobject *glue_dir = NULL;
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02002676 struct class_interface *class_intf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677
Alexander Duyck3451a492019-01-22 10:39:10 -08002678 device_lock(dev);
Dan Williams00289cd2019-07-17 18:07:53 -07002679 kill_device(dev);
Alexander Duyck3451a492019-01-22 10:39:10 -08002680 device_unlock(dev);
2681
Saravana Kannan372a67c2019-09-04 14:11:20 -07002682 if (dev->fwnode && dev->fwnode->dev == dev)
2683 dev->fwnode->dev = NULL;
2684
Alan Sternec0676ee2008-12-05 14:10:31 -05002685 /* Notify clients of device removal. This call must come
2686 * before dpm_sysfs_remove().
2687 */
2688 if (dev->bus)
2689 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
2690 BUS_NOTIFY_DEL_DEVICE, dev);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01002691
Alan Stern3b98aea2008-08-07 13:06:12 -04002692 dpm_sysfs_remove(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693 if (parent)
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08002694 klist_del(&dev->p->knode_parent);
Dan Williamse105b8b2008-04-21 10:51:07 -07002695 if (MAJOR(dev->devt)) {
Kay Sievers2b2af542009-04-30 15:23:42 +02002696 devtmpfs_delete_node(dev);
Dan Williamse105b8b2008-04-21 10:51:07 -07002697 device_remove_sys_dev_entry(dev);
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07002698 device_remove_file(dev, &dev_attr_dev);
Dan Williamse105b8b2008-04-21 10:51:07 -07002699 }
Kay Sieversb9d9c822006-06-15 15:31:56 +02002700 if (dev->class) {
Kay Sieversda231fd2007-11-21 17:29:15 +01002701 device_remove_class_symlinks(dev);
Kay Sievers99ef3ef2006-09-14 11:23:28 +02002702
Kay Sieversca22e562011-12-14 14:29:38 -08002703 mutex_lock(&dev->class->p->mutex);
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02002704 /* notify any interfaces that the device is now gone */
Greg Kroah-Hartman184f1f72008-05-28 09:28:39 -07002705 list_for_each_entry(class_intf,
Kay Sieversca22e562011-12-14 14:29:38 -08002706 &dev->class->p->interfaces, node)
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02002707 if (class_intf->remove_dev)
2708 class_intf->remove_dev(dev, class_intf);
2709 /* remove the device from the class list */
Wei Yang570d0202019-01-18 10:34:59 +08002710 klist_del(&dev->p->knode_class);
Kay Sieversca22e562011-12-14 14:29:38 -08002711 mutex_unlock(&dev->class->p->mutex);
Kay Sieversb9d9c822006-06-15 15:31:56 +02002712 }
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07002713 device_remove_file(dev, &dev_attr_uevent);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07002714 device_remove_attrs(dev);
Benjamin Herrenschmidt28953532006-11-08 19:46:14 -08002715 bus_remove_device(dev);
LongX Zhang4b6d1f122012-10-25 00:21:28 +02002716 device_pm_remove(dev);
Grant Likelyd1c34142012-03-05 08:47:41 -07002717 driver_deferred_probe_del(dev);
Heikki Krogerus07de0e82018-11-09 17:21:34 +03002718 device_platform_notify(dev, KOBJ_REMOVE);
Lukas Wunner478573c2016-07-28 02:25:41 +02002719 device_remove_properties(dev);
Jeffy Chen2ec16152017-10-20 20:01:01 +08002720 device_links_purge(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721
Joerg Roedel599bad32014-09-30 13:02:02 +02002722 if (dev->bus)
2723 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
2724 BUS_NOTIFY_REMOVED_DEVICE, dev);
Kay Sievers312c0042005-11-16 09:00:00 +01002725 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
Ming Leicebf8fd2016-07-10 19:27:36 +08002726 glue_dir = get_glue_dir(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727 kobject_del(&dev->kobj);
Ming Leicebf8fd2016-07-10 19:27:36 +08002728 cleanup_glue_dir(dev, glue_dir);
Kay Sieversda231fd2007-11-21 17:29:15 +01002729 put_device(parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730}
David Graham White86df2682013-07-21 20:41:14 -04002731EXPORT_SYMBOL_GPL(device_del);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732
2733/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002734 * device_unregister - unregister device from system.
2735 * @dev: device going away.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002737 * We do this in two parts, like we do device_register(). First,
2738 * we remove it from all the subsystems with device_del(), then
2739 * we decrement the reference count via put_device(). If that
2740 * is the final reference count, the device will be cleaned up
2741 * via device_release() above. Otherwise, the structure will
2742 * stick around until the final reference to the device is dropped.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002744void device_unregister(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745{
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01002746 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747 device_del(dev);
2748 put_device(dev);
2749}
David Graham White86df2682013-07-21 20:41:14 -04002750EXPORT_SYMBOL_GPL(device_unregister);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751
Andy Shevchenko3d060ae2015-07-27 18:04:00 +03002752static struct device *prev_device(struct klist_iter *i)
2753{
2754 struct klist_node *n = klist_prev(i);
2755 struct device *dev = NULL;
2756 struct device_private *p;
2757
2758 if (n) {
2759 p = to_device_private_parent(n);
2760 dev = p->device;
2761 }
2762 return dev;
2763}
2764
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002765static struct device *next_device(struct klist_iter *i)
mochel@digitalimplant.org36239572005-03-24 19:08:30 -08002766{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002767 struct klist_node *n = klist_next(i);
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08002768 struct device *dev = NULL;
2769 struct device_private *p;
2770
2771 if (n) {
2772 p = to_device_private_parent(n);
2773 dev = p->device;
2774 }
2775 return dev;
mochel@digitalimplant.org36239572005-03-24 19:08:30 -08002776}
2777
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778/**
Kay Sieverse454cea2009-09-18 23:01:12 +02002779 * device_get_devnode - path of device node file
Kay Sievers6fcf53a2009-04-30 15:23:42 +02002780 * @dev: device
Kay Sieverse454cea2009-09-18 23:01:12 +02002781 * @mode: returned file access mode
Kay Sievers3c2670e2013-04-06 09:56:00 -07002782 * @uid: returned file owner
2783 * @gid: returned file group
Kay Sievers6fcf53a2009-04-30 15:23:42 +02002784 * @tmp: possibly allocated string
2785 *
2786 * Return the relative path of a possible device node.
2787 * Non-default names may need to allocate a memory to compose
2788 * a name. This memory is returned in tmp and needs to be
2789 * freed by the caller.
2790 */
Kay Sieverse454cea2009-09-18 23:01:12 +02002791const char *device_get_devnode(struct device *dev,
Greg Kroah-Hartman4e4098a2013-04-11 11:43:29 -07002792 umode_t *mode, kuid_t *uid, kgid_t *gid,
Kay Sievers3c2670e2013-04-06 09:56:00 -07002793 const char **tmp)
Kay Sievers6fcf53a2009-04-30 15:23:42 +02002794{
2795 char *s;
2796
2797 *tmp = NULL;
2798
2799 /* the device type may provide a specific name */
Kay Sieverse454cea2009-09-18 23:01:12 +02002800 if (dev->type && dev->type->devnode)
Kay Sievers3c2670e2013-04-06 09:56:00 -07002801 *tmp = dev->type->devnode(dev, mode, uid, gid);
Kay Sievers6fcf53a2009-04-30 15:23:42 +02002802 if (*tmp)
2803 return *tmp;
2804
2805 /* the class may provide a specific name */
Kay Sieverse454cea2009-09-18 23:01:12 +02002806 if (dev->class && dev->class->devnode)
2807 *tmp = dev->class->devnode(dev, mode);
Kay Sievers6fcf53a2009-04-30 15:23:42 +02002808 if (*tmp)
2809 return *tmp;
2810
2811 /* return name without allocation, tmp == NULL */
2812 if (strchr(dev_name(dev), '!') == NULL)
2813 return dev_name(dev);
2814
2815 /* replace '!' in the name with '/' */
Rasmus Villemoesa29fd612015-06-25 15:02:33 -07002816 s = kstrdup(dev_name(dev), GFP_KERNEL);
2817 if (!s)
Kay Sievers6fcf53a2009-04-30 15:23:42 +02002818 return NULL;
Rasmus Villemoesa29fd612015-06-25 15:02:33 -07002819 strreplace(s, '!', '/');
2820 return *tmp = s;
Kay Sievers6fcf53a2009-04-30 15:23:42 +02002821}
2822
2823/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002824 * device_for_each_child - device child iterator.
2825 * @parent: parent struct device.
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002826 * @fn: function to be called for each device.
Robert P. J. Dayf8878dc2013-06-01 20:17:34 -04002827 * @data: data for the callback.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002829 * Iterate over @parent's child devices, and call @fn for each,
2830 * passing it @data.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002832 * We check the return of @fn each time. If it returns anything
2833 * other than 0, we break out and return that value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002835int device_for_each_child(struct device *parent, void *data,
2836 int (*fn)(struct device *dev, void *data))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837{
mochel@digitalimplant.org36239572005-03-24 19:08:30 -08002838 struct klist_iter i;
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002839 struct device *child;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840 int error = 0;
2841
Greg Kroah-Hartman014c90db2009-04-15 16:00:12 -07002842 if (!parent->p)
2843 return 0;
2844
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08002845 klist_iter_init(&parent->p->klist_children, &i);
Gimcuan Hui93ead7c2017-11-11 05:52:54 +00002846 while (!error && (child = next_device(&i)))
mochel@digitalimplant.org36239572005-03-24 19:08:30 -08002847 error = fn(child, data);
2848 klist_iter_exit(&i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849 return error;
2850}
David Graham White86df2682013-07-21 20:41:14 -04002851EXPORT_SYMBOL_GPL(device_for_each_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852
Cornelia Huck5ab69982006-11-16 15:42:07 +01002853/**
Andy Shevchenko3d060ae2015-07-27 18:04:00 +03002854 * device_for_each_child_reverse - device child iterator in reversed order.
2855 * @parent: parent struct device.
2856 * @fn: function to be called for each device.
2857 * @data: data for the callback.
2858 *
2859 * Iterate over @parent's child devices, and call @fn for each,
2860 * passing it @data.
2861 *
2862 * We check the return of @fn each time. If it returns anything
2863 * other than 0, we break out and return that value.
2864 */
2865int device_for_each_child_reverse(struct device *parent, void *data,
2866 int (*fn)(struct device *dev, void *data))
2867{
2868 struct klist_iter i;
2869 struct device *child;
2870 int error = 0;
2871
2872 if (!parent->p)
2873 return 0;
2874
2875 klist_iter_init(&parent->p->klist_children, &i);
2876 while ((child = prev_device(&i)) && !error)
2877 error = fn(child, data);
2878 klist_iter_exit(&i);
2879 return error;
2880}
2881EXPORT_SYMBOL_GPL(device_for_each_child_reverse);
2882
2883/**
Cornelia Huck5ab69982006-11-16 15:42:07 +01002884 * device_find_child - device iterator for locating a particular device.
2885 * @parent: parent struct device
Cornelia Huck5ab69982006-11-16 15:42:07 +01002886 * @match: Callback function to check device
Robert P. J. Dayf8878dc2013-06-01 20:17:34 -04002887 * @data: Data to pass to match function
Cornelia Huck5ab69982006-11-16 15:42:07 +01002888 *
2889 * This is similar to the device_for_each_child() function above, but it
2890 * returns a reference to a device that is 'found' for later use, as
2891 * determined by the @match callback.
2892 *
2893 * The callback should return 0 if the device doesn't match and non-zero
2894 * if it does. If the callback returns non-zero and a reference to the
2895 * current device can be obtained, this function will return to the caller
2896 * and not iterate over any more devices.
Federico Vagaa4e24002013-04-15 11:18:11 +02002897 *
2898 * NOTE: you will need to drop the reference with put_device() after use.
Cornelia Huck5ab69982006-11-16 15:42:07 +01002899 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002900struct device *device_find_child(struct device *parent, void *data,
2901 int (*match)(struct device *dev, void *data))
Cornelia Huck5ab69982006-11-16 15:42:07 +01002902{
2903 struct klist_iter i;
2904 struct device *child;
2905
2906 if (!parent)
2907 return NULL;
2908
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08002909 klist_iter_init(&parent->p->klist_children, &i);
Cornelia Huck5ab69982006-11-16 15:42:07 +01002910 while ((child = next_device(&i)))
2911 if (match(child, data) && get_device(child))
2912 break;
2913 klist_iter_exit(&i);
2914 return child;
2915}
David Graham White86df2682013-07-21 20:41:14 -04002916EXPORT_SYMBOL_GPL(device_find_child);
Cornelia Huck5ab69982006-11-16 15:42:07 +01002917
Heikki Krogerusdad9bb02019-05-31 17:15:37 +03002918/**
2919 * device_find_child_by_name - device iterator for locating a child device.
2920 * @parent: parent struct device
2921 * @name: name of the child device
2922 *
2923 * This is similar to the device_find_child() function above, but it
2924 * returns a reference to a device that has the name @name.
2925 *
2926 * NOTE: you will need to drop the reference with put_device() after use.
2927 */
2928struct device *device_find_child_by_name(struct device *parent,
2929 const char *name)
2930{
2931 struct klist_iter i;
2932 struct device *child;
2933
2934 if (!parent)
2935 return NULL;
2936
2937 klist_iter_init(&parent->p->klist_children, &i);
2938 while ((child = next_device(&i)))
2939 if (!strcmp(dev_name(child), name) && get_device(child))
2940 break;
2941 klist_iter_exit(&i);
2942 return child;
2943}
2944EXPORT_SYMBOL_GPL(device_find_child_by_name);
2945
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946int __init devices_init(void)
2947{
Greg Kroah-Hartman881c6cfd2007-11-01 09:29:06 -06002948 devices_kset = kset_create_and_add("devices", &device_uevent_ops, NULL);
2949 if (!devices_kset)
2950 return -ENOMEM;
Dan Williamse105b8b2008-04-21 10:51:07 -07002951 dev_kobj = kobject_create_and_add("dev", NULL);
2952 if (!dev_kobj)
2953 goto dev_kobj_err;
2954 sysfs_dev_block_kobj = kobject_create_and_add("block", dev_kobj);
2955 if (!sysfs_dev_block_kobj)
2956 goto block_kobj_err;
2957 sysfs_dev_char_kobj = kobject_create_and_add("char", dev_kobj);
2958 if (!sysfs_dev_char_kobj)
2959 goto char_kobj_err;
2960
Greg Kroah-Hartman881c6cfd2007-11-01 09:29:06 -06002961 return 0;
Dan Williamse105b8b2008-04-21 10:51:07 -07002962
2963 char_kobj_err:
2964 kobject_put(sysfs_dev_block_kobj);
2965 block_kobj_err:
2966 kobject_put(dev_kobj);
2967 dev_kobj_err:
2968 kset_unregister(devices_kset);
2969 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970}
2971
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02002972static int device_check_offline(struct device *dev, void *not_used)
2973{
2974 int ret;
2975
2976 ret = device_for_each_child(dev, NULL, device_check_offline);
2977 if (ret)
2978 return ret;
2979
2980 return device_supports_offline(dev) && !dev->offline ? -EBUSY : 0;
2981}
2982
2983/**
2984 * device_offline - Prepare the device for hot-removal.
2985 * @dev: Device to be put offline.
2986 *
2987 * Execute the device bus type's .offline() callback, if present, to prepare
2988 * the device for a subsequent hot-removal. If that succeeds, the device must
2989 * not be used until either it is removed or its bus type's .online() callback
2990 * is executed.
2991 *
2992 * Call under device_hotplug_lock.
2993 */
2994int device_offline(struct device *dev)
2995{
2996 int ret;
2997
2998 if (dev->offline_disabled)
2999 return -EPERM;
3000
3001 ret = device_for_each_child(dev, NULL, device_check_offline);
3002 if (ret)
3003 return ret;
3004
3005 device_lock(dev);
3006 if (device_supports_offline(dev)) {
3007 if (dev->offline) {
3008 ret = 1;
3009 } else {
3010 ret = dev->bus->offline(dev);
3011 if (!ret) {
3012 kobject_uevent(&dev->kobj, KOBJ_OFFLINE);
3013 dev->offline = true;
3014 }
3015 }
3016 }
3017 device_unlock(dev);
3018
3019 return ret;
3020}
3021
3022/**
3023 * device_online - Put the device back online after successful device_offline().
3024 * @dev: Device to be put back online.
3025 *
3026 * If device_offline() has been successfully executed for @dev, but the device
3027 * has not been removed subsequently, execute its bus type's .online() callback
3028 * to indicate that the device can be used again.
3029 *
3030 * Call under device_hotplug_lock.
3031 */
3032int device_online(struct device *dev)
3033{
3034 int ret = 0;
3035
3036 device_lock(dev);
3037 if (device_supports_offline(dev)) {
3038 if (dev->offline) {
3039 ret = dev->bus->online(dev);
3040 if (!ret) {
3041 kobject_uevent(&dev->kobj, KOBJ_ONLINE);
3042 dev->offline = false;
3043 }
3044 } else {
3045 ret = 1;
3046 }
3047 }
3048 device_unlock(dev);
3049
3050 return ret;
3051}
3052
Karthigan Srinivasan7f100d12011-04-18 16:16:52 -05003053struct root_device {
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00003054 struct device dev;
3055 struct module *owner;
3056};
3057
Josh Triplett93058422012-11-18 21:27:55 -08003058static inline struct root_device *to_root_device(struct device *d)
Ferenc Wagner481e2072011-01-07 15:17:47 +01003059{
3060 return container_of(d, struct root_device, dev);
3061}
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00003062
3063static void root_device_release(struct device *dev)
3064{
3065 kfree(to_root_device(dev));
3066}
3067
3068/**
3069 * __root_device_register - allocate and register a root device
3070 * @name: root device name
3071 * @owner: owner module of the root device, usually THIS_MODULE
3072 *
3073 * This function allocates a root device and registers it
3074 * using device_register(). In order to free the returned
3075 * device, use root_device_unregister().
3076 *
3077 * Root devices are dummy devices which allow other devices
3078 * to be grouped under /sys/devices. Use this function to
3079 * allocate a root device and then use it as the parent of
3080 * any device which should appear under /sys/devices/{name}
3081 *
3082 * The /sys/devices/{name} directory will also contain a
3083 * 'module' symlink which points to the @owner directory
3084 * in sysfs.
3085 *
Jani Nikulaf0eae0e2010-03-11 18:11:45 +02003086 * Returns &struct device pointer on success, or ERR_PTR() on error.
3087 *
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00003088 * Note: You probably want to use root_device_register().
3089 */
3090struct device *__root_device_register(const char *name, struct module *owner)
3091{
3092 struct root_device *root;
3093 int err = -ENOMEM;
3094
3095 root = kzalloc(sizeof(struct root_device), GFP_KERNEL);
3096 if (!root)
3097 return ERR_PTR(err);
3098
Greg Kroah-Hartmanacc0e902009-06-02 15:39:55 -07003099 err = dev_set_name(&root->dev, "%s", name);
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00003100 if (err) {
3101 kfree(root);
3102 return ERR_PTR(err);
3103 }
3104
3105 root->dev.release = root_device_release;
3106
3107 err = device_register(&root->dev);
3108 if (err) {
3109 put_device(&root->dev);
3110 return ERR_PTR(err);
3111 }
3112
Christoph Egger1d9e8822010-05-17 16:57:58 +02003113#ifdef CONFIG_MODULES /* gotta find a "cleaner" way to do this */
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00003114 if (owner) {
3115 struct module_kobject *mk = &owner->mkobj;
3116
3117 err = sysfs_create_link(&root->dev.kobj, &mk->kobj, "module");
3118 if (err) {
3119 device_unregister(&root->dev);
3120 return ERR_PTR(err);
3121 }
3122 root->owner = owner;
3123 }
3124#endif
3125
3126 return &root->dev;
3127}
3128EXPORT_SYMBOL_GPL(__root_device_register);
3129
3130/**
3131 * root_device_unregister - unregister and free a root device
Randy Dunlap7cbcf222009-01-20 16:29:13 -08003132 * @dev: device going away
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00003133 *
3134 * This function unregisters and cleans up a device that was created by
3135 * root_device_register().
3136 */
3137void root_device_unregister(struct device *dev)
3138{
3139 struct root_device *root = to_root_device(dev);
3140
3141 if (root->owner)
3142 sysfs_remove_link(&root->dev.kobj, "module");
3143
3144 device_unregister(dev);
3145}
3146EXPORT_SYMBOL_GPL(root_device_unregister);
3147
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07003148
3149static void device_create_release(struct device *dev)
3150{
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01003151 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07003152 kfree(dev);
3153}
3154
Mathieu Malaterre6a8b55d2018-05-05 21:57:41 +02003155static __printf(6, 0) struct device *
Guenter Roeck39ef3112013-07-14 16:05:57 -07003156device_create_groups_vargs(struct class *class, struct device *parent,
3157 dev_t devt, void *drvdata,
3158 const struct attribute_group **groups,
3159 const char *fmt, va_list args)
3160{
3161 struct device *dev = NULL;
3162 int retval = -ENODEV;
3163
3164 if (class == NULL || IS_ERR(class))
3165 goto error;
3166
3167 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
3168 if (!dev) {
3169 retval = -ENOMEM;
3170 goto error;
3171 }
3172
David Herrmannbbc780f2013-11-21 20:15:48 +01003173 device_initialize(dev);
Guenter Roeck39ef3112013-07-14 16:05:57 -07003174 dev->devt = devt;
3175 dev->class = class;
3176 dev->parent = parent;
3177 dev->groups = groups;
3178 dev->release = device_create_release;
3179 dev_set_drvdata(dev, drvdata);
3180
3181 retval = kobject_set_name_vargs(&dev->kobj, fmt, args);
3182 if (retval)
3183 goto error;
3184
David Herrmannbbc780f2013-11-21 20:15:48 +01003185 retval = device_add(dev);
Guenter Roeck39ef3112013-07-14 16:05:57 -07003186 if (retval)
3187 goto error;
3188
3189 return dev;
3190
3191error:
3192 put_device(dev);
3193 return ERR_PTR(retval);
3194}
3195
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07003196/**
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07003197 * device_create_vargs - creates a device and registers it with sysfs
3198 * @class: pointer to the struct class that this device should be registered to
3199 * @parent: pointer to the parent struct device of this new device, if any
3200 * @devt: the dev_t for the char device to be added
3201 * @drvdata: the data to be added to the device for callbacks
3202 * @fmt: string for the device's name
3203 * @args: va_list for the device's name
3204 *
3205 * This function can be used by char device classes. A struct device
3206 * will be created in sysfs, registered to the specified class.
3207 *
3208 * A "dev" file will be created, showing the dev_t for the device, if
3209 * the dev_t is not 0,0.
3210 * If a pointer to a parent struct device is passed in, the newly created
3211 * struct device will be a child of that device in sysfs.
3212 * The pointer to the struct device will be returned from the call.
3213 * Any further sysfs files that might be required can be created using this
3214 * pointer.
3215 *
Jani Nikulaf0eae0e2010-03-11 18:11:45 +02003216 * Returns &struct device pointer on success, or ERR_PTR() on error.
3217 *
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07003218 * Note: the struct class passed to this function must have previously
3219 * been created with a call to class_create().
3220 */
3221struct device *device_create_vargs(struct class *class, struct device *parent,
3222 dev_t devt, void *drvdata, const char *fmt,
3223 va_list args)
3224{
Guenter Roeck39ef3112013-07-14 16:05:57 -07003225 return device_create_groups_vargs(class, parent, devt, drvdata, NULL,
3226 fmt, args);
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07003227}
3228EXPORT_SYMBOL_GPL(device_create_vargs);
3229
3230/**
Greg Kroah-Hartman4e106732008-07-21 20:03:34 -07003231 * device_create - creates a device and registers it with sysfs
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07003232 * @class: pointer to the struct class that this device should be registered to
3233 * @parent: pointer to the parent struct device of this new device, if any
3234 * @devt: the dev_t for the char device to be added
3235 * @drvdata: the data to be added to the device for callbacks
3236 * @fmt: string for the device's name
3237 *
3238 * This function can be used by char device classes. A struct device
3239 * will be created in sysfs, registered to the specified class.
3240 *
3241 * A "dev" file will be created, showing the dev_t for the device, if
3242 * the dev_t is not 0,0.
3243 * If a pointer to a parent struct device is passed in, the newly created
3244 * struct device will be a child of that device in sysfs.
3245 * The pointer to the struct device will be returned from the call.
3246 * Any further sysfs files that might be required can be created using this
3247 * pointer.
3248 *
Jani Nikulaf0eae0e2010-03-11 18:11:45 +02003249 * Returns &struct device pointer on success, or ERR_PTR() on error.
3250 *
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07003251 * Note: the struct class passed to this function must have previously
3252 * been created with a call to class_create().
3253 */
Greg Kroah-Hartman4e106732008-07-21 20:03:34 -07003254struct device *device_create(struct class *class, struct device *parent,
3255 dev_t devt, void *drvdata, const char *fmt, ...)
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07003256{
3257 va_list vargs;
3258 struct device *dev;
3259
3260 va_start(vargs, fmt);
3261 dev = device_create_vargs(class, parent, devt, drvdata, fmt, vargs);
3262 va_end(vargs);
3263 return dev;
3264}
Greg Kroah-Hartman4e106732008-07-21 20:03:34 -07003265EXPORT_SYMBOL_GPL(device_create);
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07003266
Guenter Roeck39ef3112013-07-14 16:05:57 -07003267/**
3268 * device_create_with_groups - creates a device and registers it with sysfs
3269 * @class: pointer to the struct class that this device should be registered to
3270 * @parent: pointer to the parent struct device of this new device, if any
3271 * @devt: the dev_t for the char device to be added
3272 * @drvdata: the data to be added to the device for callbacks
3273 * @groups: NULL-terminated list of attribute groups to be created
3274 * @fmt: string for the device's name
3275 *
3276 * This function can be used by char device classes. A struct device
3277 * will be created in sysfs, registered to the specified class.
3278 * Additional attributes specified in the groups parameter will also
3279 * be created automatically.
3280 *
3281 * A "dev" file will be created, showing the dev_t for the device, if
3282 * the dev_t is not 0,0.
3283 * If a pointer to a parent struct device is passed in, the newly created
3284 * struct device will be a child of that device in sysfs.
3285 * The pointer to the struct device will be returned from the call.
3286 * Any further sysfs files that might be required can be created using this
3287 * pointer.
3288 *
3289 * Returns &struct device pointer on success, or ERR_PTR() on error.
3290 *
3291 * Note: the struct class passed to this function must have previously
3292 * been created with a call to class_create().
3293 */
3294struct device *device_create_with_groups(struct class *class,
3295 struct device *parent, dev_t devt,
3296 void *drvdata,
3297 const struct attribute_group **groups,
3298 const char *fmt, ...)
3299{
3300 va_list vargs;
3301 struct device *dev;
3302
3303 va_start(vargs, fmt);
3304 dev = device_create_groups_vargs(class, parent, devt, drvdata, groups,
3305 fmt, vargs);
3306 va_end(vargs);
3307 return dev;
3308}
3309EXPORT_SYMBOL_GPL(device_create_with_groups);
3310
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01003311/**
3312 * device_destroy - removes a device that was created with device_create()
3313 * @class: pointer to the struct class that this device was registered with
3314 * @devt: the dev_t of the device that was previously registered
3315 *
3316 * This call unregisters and cleans up a device that was created with a
3317 * call to device_create().
3318 */
3319void device_destroy(struct class *class, dev_t devt)
3320{
3321 struct device *dev;
3322
Suzuki K Poulose4495dfd2019-07-23 23:18:35 +01003323 dev = class_find_device_by_devt(class, devt);
Dave Youngcd354492008-01-28 16:56:11 +08003324 if (dev) {
3325 put_device(dev);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07003326 device_unregister(dev);
Dave Youngcd354492008-01-28 16:56:11 +08003327 }
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07003328}
3329EXPORT_SYMBOL_GPL(device_destroy);
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07003330
3331/**
3332 * device_rename - renames a device
3333 * @dev: the pointer to the struct device to be renamed
3334 * @new_name: the new name of the device
Eric W. Biederman030c1d22008-05-08 14:41:00 -07003335 *
3336 * It is the responsibility of the caller to provide mutual
3337 * exclusion between two different calls of device_rename
3338 * on the same device to ensure that new_name is valid and
3339 * won't conflict with other devices.
Michael Ellermanc6c0ac62010-11-25 09:44:07 +11003340 *
Timur Tabia5462512010-12-13 14:08:52 -06003341 * Note: Don't call this function. Currently, the networking layer calls this
3342 * function, but that will change. The following text from Kay Sievers offers
3343 * some insight:
3344 *
3345 * Renaming devices is racy at many levels, symlinks and other stuff are not
3346 * replaced atomically, and you get a "move" uevent, but it's not easy to
3347 * connect the event to the old and new device. Device nodes are not renamed at
3348 * all, there isn't even support for that in the kernel now.
3349 *
3350 * In the meantime, during renaming, your target name might be taken by another
3351 * driver, creating conflicts. Or the old name is taken directly after you
3352 * renamed it -- then you get events for the same DEVPATH, before you even see
3353 * the "move" event. It's just a mess, and nothing new should ever rely on
3354 * kernel device renaming. Besides that, it's not even implemented now for
3355 * other things than (driver-core wise very simple) network devices.
3356 *
3357 * We are currently about to change network renaming in udev to completely
3358 * disallow renaming of devices in the same namespace as the kernel uses,
3359 * because we can't solve the problems properly, that arise with swapping names
3360 * of multiple interfaces without races. Means, renaming of eth[0-9]* will only
3361 * be allowed to some other name than eth[0-9]*, for the aforementioned
3362 * reasons.
3363 *
3364 * Make up a "real" name in the driver before you register anything, or add
3365 * some other attributes for userspace to find the device, or use udev to add
3366 * symlinks -- but never rename kernel devices later, it's a complete mess. We
3367 * don't even want to get into that and try to implement the missing pieces in
3368 * the core. We really have other pieces to fix in the driver core mess. :)
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07003369 */
Johannes Berg6937e8f2010-08-05 17:38:18 +02003370int device_rename(struct device *dev, const char *new_name)
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07003371{
Tejun Heo4b30ee52013-09-11 22:29:06 -04003372 struct kobject *kobj = &dev->kobj;
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07003373 char *old_device_name = NULL;
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07003374 int error;
3375
3376 dev = get_device(dev);
3377 if (!dev)
3378 return -EINVAL;
3379
ethan.zhao69df7532013-10-13 22:12:35 +08003380 dev_dbg(dev, "renaming to %s\n", new_name);
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07003381
Kay Sievers1fa5ae82009-01-25 15:17:37 +01003382 old_device_name = kstrdup(dev_name(dev), GFP_KERNEL);
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07003383 if (!old_device_name) {
3384 error = -ENOMEM;
3385 goto out;
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07003386 }
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07003387
Eric W. Biedermanf349cf32010-03-30 11:31:29 -07003388 if (dev->class) {
Tejun Heo4b30ee52013-09-11 22:29:06 -04003389 error = sysfs_rename_link_ns(&dev->class->p->subsys.kobj,
3390 kobj, old_device_name,
3391 new_name, kobject_namespace(kobj));
Eric W. Biedermanf349cf32010-03-30 11:31:29 -07003392 if (error)
3393 goto out;
3394 }
Kay Sievers39aba962010-09-04 22:33:14 -07003395
Tejun Heo4b30ee52013-09-11 22:29:06 -04003396 error = kobject_rename(kobj, new_name);
Kay Sievers1fa5ae82009-01-25 15:17:37 +01003397 if (error)
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07003398 goto out;
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07003399
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07003400out:
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07003401 put_device(dev);
3402
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07003403 kfree(old_device_name);
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07003404
3405 return error;
3406}
Johannes Berga2807db2007-02-28 12:38:31 +01003407EXPORT_SYMBOL_GPL(device_rename);
Cornelia Huck8a824722006-11-20 17:07:51 +01003408
3409static int device_move_class_links(struct device *dev,
3410 struct device *old_parent,
3411 struct device *new_parent)
3412{
Greg Kroah-Hartmanf7f34612007-03-06 12:55:53 -08003413 int error = 0;
Cornelia Huck8a824722006-11-20 17:07:51 +01003414
Greg Kroah-Hartmanf7f34612007-03-06 12:55:53 -08003415 if (old_parent)
3416 sysfs_remove_link(&dev->kobj, "device");
3417 if (new_parent)
3418 error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
3419 "device");
3420 return error;
Cornelia Huck8a824722006-11-20 17:07:51 +01003421}
3422
3423/**
3424 * device_move - moves a device to a new parent
3425 * @dev: the pointer to the struct device to be moved
Wolfram Sang13509862018-05-06 13:23:47 +02003426 * @new_parent: the new parent of the device (can be NULL)
Cornelia Huckffa6a702009-03-04 12:44:00 +01003427 * @dpm_order: how to reorder the dpm_list
Cornelia Huck8a824722006-11-20 17:07:51 +01003428 */
Cornelia Huckffa6a702009-03-04 12:44:00 +01003429int device_move(struct device *dev, struct device *new_parent,
3430 enum dpm_order dpm_order)
Cornelia Huck8a824722006-11-20 17:07:51 +01003431{
3432 int error;
3433 struct device *old_parent;
Cornelia Huckc744aeae2007-01-08 20:16:44 +01003434 struct kobject *new_parent_kobj;
Cornelia Huck8a824722006-11-20 17:07:51 +01003435
3436 dev = get_device(dev);
3437 if (!dev)
3438 return -EINVAL;
3439
Cornelia Huckffa6a702009-03-04 12:44:00 +01003440 device_pm_lock();
Cornelia Huck8a824722006-11-20 17:07:51 +01003441 new_parent = get_device(new_parent);
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08003442 new_parent_kobj = get_device_parent(dev, new_parent);
Tetsuo Handa84d0c272018-05-07 19:10:31 +09003443 if (IS_ERR(new_parent_kobj)) {
3444 error = PTR_ERR(new_parent_kobj);
3445 put_device(new_parent);
3446 goto out;
3447 }
Cornelia Huck63b69712008-01-21 16:09:44 +01003448
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01003449 pr_debug("device: '%s': %s: moving to '%s'\n", dev_name(dev),
3450 __func__, new_parent ? dev_name(new_parent) : "<NULL>");
Cornelia Huckc744aeae2007-01-08 20:16:44 +01003451 error = kobject_move(&dev->kobj, new_parent_kobj);
Cornelia Huck8a824722006-11-20 17:07:51 +01003452 if (error) {
Cornelia Huck63b69712008-01-21 16:09:44 +01003453 cleanup_glue_dir(dev, new_parent_kobj);
Cornelia Huck8a824722006-11-20 17:07:51 +01003454 put_device(new_parent);
3455 goto out;
3456 }
3457 old_parent = dev->parent;
3458 dev->parent = new_parent;
3459 if (old_parent)
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08003460 klist_remove(&dev->p->knode_parent);
Yinghai Lu0d358f22008-02-19 03:20:41 -08003461 if (new_parent) {
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08003462 klist_add_tail(&dev->p->knode_parent,
3463 &new_parent->p->klist_children);
Yinghai Lu0d358f22008-02-19 03:20:41 -08003464 set_dev_node(dev, dev_to_node(new_parent));
3465 }
3466
Rabin Vincentbdd40342012-04-23 09:16:36 +02003467 if (dev->class) {
3468 error = device_move_class_links(dev, old_parent, new_parent);
3469 if (error) {
3470 /* We ignore errors on cleanup since we're hosed anyway... */
3471 device_move_class_links(dev, new_parent, old_parent);
3472 if (!kobject_move(&dev->kobj, &old_parent->kobj)) {
3473 if (new_parent)
3474 klist_remove(&dev->p->knode_parent);
3475 dev->parent = old_parent;
3476 if (old_parent) {
3477 klist_add_tail(&dev->p->knode_parent,
3478 &old_parent->p->klist_children);
3479 set_dev_node(dev, dev_to_node(old_parent));
3480 }
Yinghai Lu0d358f22008-02-19 03:20:41 -08003481 }
Rabin Vincentbdd40342012-04-23 09:16:36 +02003482 cleanup_glue_dir(dev, new_parent_kobj);
3483 put_device(new_parent);
3484 goto out;
Cornelia Huck8a824722006-11-20 17:07:51 +01003485 }
Cornelia Huck8a824722006-11-20 17:07:51 +01003486 }
Cornelia Huckffa6a702009-03-04 12:44:00 +01003487 switch (dpm_order) {
3488 case DPM_ORDER_NONE:
3489 break;
3490 case DPM_ORDER_DEV_AFTER_PARENT:
3491 device_pm_move_after(dev, new_parent);
Grygorii Strashko52cdbdd2015-07-27 20:43:01 +03003492 devices_kset_move_after(dev, new_parent);
Cornelia Huckffa6a702009-03-04 12:44:00 +01003493 break;
3494 case DPM_ORDER_PARENT_BEFORE_DEV:
3495 device_pm_move_before(new_parent, dev);
Grygorii Strashko52cdbdd2015-07-27 20:43:01 +03003496 devices_kset_move_before(new_parent, dev);
Cornelia Huckffa6a702009-03-04 12:44:00 +01003497 break;
3498 case DPM_ORDER_DEV_LAST:
3499 device_pm_move_last(dev);
Grygorii Strashko52cdbdd2015-07-27 20:43:01 +03003500 devices_kset_move_last(dev);
Cornelia Huckffa6a702009-03-04 12:44:00 +01003501 break;
3502 }
Rabin Vincentbdd40342012-04-23 09:16:36 +02003503
Cornelia Huck8a824722006-11-20 17:07:51 +01003504 put_device(old_parent);
3505out:
Cornelia Huckffa6a702009-03-04 12:44:00 +01003506 device_pm_unlock();
Cornelia Huck8a824722006-11-20 17:07:51 +01003507 put_device(dev);
3508 return error;
3509}
Cornelia Huck8a824722006-11-20 17:07:51 +01003510EXPORT_SYMBOL_GPL(device_move);
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08003511
Christian Braunerb8f33e52020-02-27 04:37:15 +01003512static int device_attrs_change_owner(struct device *dev, kuid_t kuid,
3513 kgid_t kgid)
3514{
3515 struct kobject *kobj = &dev->kobj;
3516 struct class *class = dev->class;
3517 const struct device_type *type = dev->type;
3518 int error;
3519
3520 if (class) {
3521 /*
3522 * Change the device groups of the device class for @dev to
3523 * @kuid/@kgid.
3524 */
3525 error = sysfs_groups_change_owner(kobj, class->dev_groups, kuid,
3526 kgid);
3527 if (error)
3528 return error;
3529 }
3530
3531 if (type) {
3532 /*
3533 * Change the device groups of the device type for @dev to
3534 * @kuid/@kgid.
3535 */
3536 error = sysfs_groups_change_owner(kobj, type->groups, kuid,
3537 kgid);
3538 if (error)
3539 return error;
3540 }
3541
3542 /* Change the device groups of @dev to @kuid/@kgid. */
3543 error = sysfs_groups_change_owner(kobj, dev->groups, kuid, kgid);
3544 if (error)
3545 return error;
3546
3547 if (device_supports_offline(dev) && !dev->offline_disabled) {
3548 /* Change online device attributes of @dev to @kuid/@kgid. */
3549 error = sysfs_file_change_owner(kobj, dev_attr_online.attr.name,
3550 kuid, kgid);
3551 if (error)
3552 return error;
3553 }
3554
3555 return 0;
3556}
3557
3558/**
3559 * device_change_owner - change the owner of an existing device.
3560 * @dev: device.
3561 * @kuid: new owner's kuid
3562 * @kgid: new owner's kgid
3563 *
3564 * This changes the owner of @dev and its corresponding sysfs entries to
3565 * @kuid/@kgid. This function closely mirrors how @dev was added via driver
3566 * core.
3567 *
3568 * Returns 0 on success or error code on failure.
3569 */
3570int device_change_owner(struct device *dev, kuid_t kuid, kgid_t kgid)
3571{
3572 int error;
3573 struct kobject *kobj = &dev->kobj;
3574
3575 dev = get_device(dev);
3576 if (!dev)
3577 return -EINVAL;
3578
3579 /*
3580 * Change the kobject and the default attributes and groups of the
3581 * ktype associated with it to @kuid/@kgid.
3582 */
3583 error = sysfs_change_owner(kobj, kuid, kgid);
3584 if (error)
3585 goto out;
3586
3587 /*
3588 * Change the uevent file for @dev to the new owner. The uevent file
3589 * was created in a separate step when @dev got added and we mirror
3590 * that step here.
3591 */
3592 error = sysfs_file_change_owner(kobj, dev_attr_uevent.attr.name, kuid,
3593 kgid);
3594 if (error)
3595 goto out;
3596
3597 /*
3598 * Change the device groups, the device groups associated with the
3599 * device class, and the groups associated with the device type of @dev
3600 * to @kuid/@kgid.
3601 */
3602 error = device_attrs_change_owner(dev, kuid, kgid);
3603 if (error)
3604 goto out;
3605
Christian Brauner3b52fc52020-02-27 04:37:16 +01003606 error = dpm_sysfs_change_owner(dev, kuid, kgid);
3607 if (error)
3608 goto out;
3609
Christian Braunerb8f33e52020-02-27 04:37:15 +01003610#ifdef CONFIG_BLOCK
3611 if (sysfs_deprecated && dev->class == &block_class)
3612 goto out;
3613#endif
3614
3615 /*
3616 * Change the owner of the symlink located in the class directory of
3617 * the device class associated with @dev which points to the actual
3618 * directory entry for @dev to @kuid/@kgid. This ensures that the
3619 * symlink shows the same permissions as its target.
3620 */
3621 error = sysfs_link_change_owner(&dev->class->p->subsys.kobj, &dev->kobj,
3622 dev_name(dev), kuid, kgid);
3623 if (error)
3624 goto out;
3625
3626out:
3627 put_device(dev);
3628 return error;
3629}
3630EXPORT_SYMBOL_GPL(device_change_owner);
3631
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08003632/**
3633 * device_shutdown - call ->shutdown() on each device to shutdown.
3634 */
3635void device_shutdown(void)
3636{
Benson Leungf123db82013-09-24 20:05:11 -07003637 struct device *dev, *parent;
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08003638
Pingfan Liu3297c8f2018-07-19 13:14:58 +08003639 wait_for_device_probe();
3640 device_block_probing();
3641
Rafael J. Wysocki65650b32019-10-09 01:29:10 +02003642 cpufreq_suspend();
3643
Hugh Daschbach62458382010-03-22 10:36:37 -07003644 spin_lock(&devices_kset->list_lock);
3645 /*
3646 * Walk the devices list backward, shutting down each in turn.
3647 * Beware that device unplug events may also start pulling
3648 * devices offline, even as the system is shutting down.
3649 */
3650 while (!list_empty(&devices_kset->list)) {
3651 dev = list_entry(devices_kset->list.prev, struct device,
3652 kobj.entry);
Ming Leid1c6c032012-06-22 18:01:40 +08003653
3654 /*
3655 * hold reference count of device's parent to
3656 * prevent it from being freed because parent's
3657 * lock is to be held
3658 */
Benson Leungf123db82013-09-24 20:05:11 -07003659 parent = get_device(dev->parent);
Hugh Daschbach62458382010-03-22 10:36:37 -07003660 get_device(dev);
3661 /*
3662 * Make sure the device is off the kset list, in the
3663 * event that dev->*->shutdown() doesn't remove it.
3664 */
3665 list_del_init(&dev->kobj.entry);
3666 spin_unlock(&devices_kset->list_lock);
Alan Sternfe6b91f2011-12-06 23:24:52 +01003667
Ming Leid1c6c032012-06-22 18:01:40 +08003668 /* hold lock to avoid race with probe/release */
Benson Leungf123db82013-09-24 20:05:11 -07003669 if (parent)
3670 device_lock(parent);
Ming Leid1c6c032012-06-22 18:01:40 +08003671 device_lock(dev);
3672
Alan Sternfe6b91f2011-12-06 23:24:52 +01003673 /* Don't allow any more runtime suspends */
3674 pm_runtime_get_noresume(dev);
3675 pm_runtime_barrier(dev);
Hugh Daschbach62458382010-03-22 10:36:37 -07003676
Michal Suchanek75216212017-08-11 15:44:43 +02003677 if (dev->class && dev->class->shutdown_pre) {
Josh Zimmermanf77af152017-06-25 14:53:23 -07003678 if (initcall_debug)
Michal Suchanek75216212017-08-11 15:44:43 +02003679 dev_info(dev, "shutdown_pre\n");
3680 dev->class->shutdown_pre(dev);
3681 }
3682 if (dev->bus && dev->bus->shutdown) {
ShuoX Liu0246c4f2012-11-23 15:14:12 +08003683 if (initcall_debug)
3684 dev_info(dev, "shutdown\n");
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08003685 dev->bus->shutdown(dev);
3686 } else if (dev->driver && dev->driver->shutdown) {
ShuoX Liu0246c4f2012-11-23 15:14:12 +08003687 if (initcall_debug)
3688 dev_info(dev, "shutdown\n");
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08003689 dev->driver->shutdown(dev);
3690 }
Ming Leid1c6c032012-06-22 18:01:40 +08003691
3692 device_unlock(dev);
Benson Leungf123db82013-09-24 20:05:11 -07003693 if (parent)
3694 device_unlock(parent);
Ming Leid1c6c032012-06-22 18:01:40 +08003695
Hugh Daschbach62458382010-03-22 10:36:37 -07003696 put_device(dev);
Benson Leungf123db82013-09-24 20:05:11 -07003697 put_device(parent);
Hugh Daschbach62458382010-03-22 10:36:37 -07003698
3699 spin_lock(&devices_kset->list_lock);
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08003700 }
Hugh Daschbach62458382010-03-22 10:36:37 -07003701 spin_unlock(&devices_kset->list_lock);
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08003702}
Joe Perches99bcf212010-06-27 01:02:34 +00003703
3704/*
3705 * Device logging functions
3706 */
3707
3708#ifdef CONFIG_PRINTK
Joe Perches666f3552012-09-12 20:14:11 -07003709static int
3710create_syslog_header(const struct device *dev, char *hdr, size_t hdrlen)
Joe Perches99bcf212010-06-27 01:02:34 +00003711{
Kay Sieversc4e00da2012-05-03 02:29:59 +02003712 const char *subsys;
Joe Perches798efc62012-09-12 20:11:29 -07003713 size_t pos = 0;
Joe Perches99bcf212010-06-27 01:02:34 +00003714
Kay Sieversc4e00da2012-05-03 02:29:59 +02003715 if (dev->class)
3716 subsys = dev->class->name;
3717 else if (dev->bus)
3718 subsys = dev->bus->name;
3719 else
Joe Perches798efc62012-09-12 20:11:29 -07003720 return 0;
Kay Sieversc4e00da2012-05-03 02:29:59 +02003721
Joe Perches798efc62012-09-12 20:11:29 -07003722 pos += snprintf(hdr + pos, hdrlen - pos, "SUBSYSTEM=%s", subsys);
Ben Hutchings655e5b72014-08-26 00:34:44 -07003723 if (pos >= hdrlen)
3724 goto overflow;
Kay Sieversc4e00da2012-05-03 02:29:59 +02003725
3726 /*
3727 * Add device identifier DEVICE=:
3728 * b12:8 block dev_t
3729 * c127:3 char dev_t
3730 * n8 netdev ifindex
3731 * +sound:card0 subsystem:devname
3732 */
3733 if (MAJOR(dev->devt)) {
3734 char c;
3735
3736 if (strcmp(subsys, "block") == 0)
3737 c = 'b';
3738 else
3739 c = 'c';
Joe Perches798efc62012-09-12 20:11:29 -07003740 pos++;
3741 pos += snprintf(hdr + pos, hdrlen - pos,
3742 "DEVICE=%c%u:%u",
3743 c, MAJOR(dev->devt), MINOR(dev->devt));
Kay Sieversc4e00da2012-05-03 02:29:59 +02003744 } else if (strcmp(subsys, "net") == 0) {
3745 struct net_device *net = to_net_dev(dev);
3746
Joe Perches798efc62012-09-12 20:11:29 -07003747 pos++;
3748 pos += snprintf(hdr + pos, hdrlen - pos,
3749 "DEVICE=n%u", net->ifindex);
Kay Sieversc4e00da2012-05-03 02:29:59 +02003750 } else {
Joe Perches798efc62012-09-12 20:11:29 -07003751 pos++;
3752 pos += snprintf(hdr + pos, hdrlen - pos,
3753 "DEVICE=+%s:%s", subsys, dev_name(dev));
Kay Sieversc4e00da2012-05-03 02:29:59 +02003754 }
Jim Cromieaf7f2152012-07-19 13:46:21 -06003755
Ben Hutchings655e5b72014-08-26 00:34:44 -07003756 if (pos >= hdrlen)
3757 goto overflow;
3758
Joe Perches798efc62012-09-12 20:11:29 -07003759 return pos;
Ben Hutchings655e5b72014-08-26 00:34:44 -07003760
3761overflow:
3762 dev_WARN(dev, "device/subsystem name too long");
3763 return 0;
Joe Perches99bcf212010-06-27 01:02:34 +00003764}
Joe Perches798efc62012-09-12 20:11:29 -07003765
Joe Perches05e4e5b2012-09-12 20:13:37 -07003766int dev_vprintk_emit(int level, const struct device *dev,
3767 const char *fmt, va_list args)
3768{
3769 char hdr[128];
3770 size_t hdrlen;
3771
3772 hdrlen = create_syslog_header(dev, hdr, sizeof(hdr));
3773
3774 return vprintk_emit(0, level, hdrlen ? hdr : NULL, hdrlen, fmt, args);
3775}
3776EXPORT_SYMBOL(dev_vprintk_emit);
3777
3778int dev_printk_emit(int level, const struct device *dev, const char *fmt, ...)
3779{
3780 va_list args;
3781 int r;
3782
3783 va_start(args, fmt);
3784
3785 r = dev_vprintk_emit(level, dev, fmt, args);
3786
3787 va_end(args);
3788
3789 return r;
3790}
3791EXPORT_SYMBOL(dev_printk_emit);
3792
Joe Perchesd1f10522014-12-25 15:07:04 -08003793static void __dev_printk(const char *level, const struct device *dev,
Joe Perches798efc62012-09-12 20:11:29 -07003794 struct va_format *vaf)
3795{
Joe Perchesd1f10522014-12-25 15:07:04 -08003796 if (dev)
3797 dev_printk_emit(level[1] - '0', dev, "%s %s: %pV",
3798 dev_driver_string(dev), dev_name(dev), vaf);
3799 else
3800 printk("%s(NULL device *): %pV", level, vaf);
Joe Perches798efc62012-09-12 20:11:29 -07003801}
Joe Perches99bcf212010-06-27 01:02:34 +00003802
Joe Perchesd1f10522014-12-25 15:07:04 -08003803void dev_printk(const char *level, const struct device *dev,
3804 const char *fmt, ...)
Joe Perches99bcf212010-06-27 01:02:34 +00003805{
3806 struct va_format vaf;
3807 va_list args;
Joe Perches99bcf212010-06-27 01:02:34 +00003808
3809 va_start(args, fmt);
3810
3811 vaf.fmt = fmt;
3812 vaf.va = &args;
3813
Joe Perchesd1f10522014-12-25 15:07:04 -08003814 __dev_printk(level, dev, &vaf);
Joe Perches798efc62012-09-12 20:11:29 -07003815
Joe Perches99bcf212010-06-27 01:02:34 +00003816 va_end(args);
Joe Perches99bcf212010-06-27 01:02:34 +00003817}
3818EXPORT_SYMBOL(dev_printk);
3819
3820#define define_dev_printk_level(func, kern_level) \
Joe Perchesd1f10522014-12-25 15:07:04 -08003821void func(const struct device *dev, const char *fmt, ...) \
Joe Perches99bcf212010-06-27 01:02:34 +00003822{ \
3823 struct va_format vaf; \
3824 va_list args; \
Joe Perches99bcf212010-06-27 01:02:34 +00003825 \
3826 va_start(args, fmt); \
3827 \
3828 vaf.fmt = fmt; \
3829 vaf.va = &args; \
3830 \
Joe Perchesd1f10522014-12-25 15:07:04 -08003831 __dev_printk(kern_level, dev, &vaf); \
Joe Perches798efc62012-09-12 20:11:29 -07003832 \
Joe Perches99bcf212010-06-27 01:02:34 +00003833 va_end(args); \
Joe Perches99bcf212010-06-27 01:02:34 +00003834} \
3835EXPORT_SYMBOL(func);
3836
Joe Perches663336e2018-05-09 08:15:46 -07003837define_dev_printk_level(_dev_emerg, KERN_EMERG);
3838define_dev_printk_level(_dev_alert, KERN_ALERT);
3839define_dev_printk_level(_dev_crit, KERN_CRIT);
3840define_dev_printk_level(_dev_err, KERN_ERR);
3841define_dev_printk_level(_dev_warn, KERN_WARNING);
3842define_dev_printk_level(_dev_notice, KERN_NOTICE);
Joe Perches99bcf212010-06-27 01:02:34 +00003843define_dev_printk_level(_dev_info, KERN_INFO);
3844
3845#endif
Rafael J. Wysocki97badf82015-04-03 23:23:37 +02003846
3847static inline bool fwnode_is_primary(struct fwnode_handle *fwnode)
3848{
3849 return fwnode && !IS_ERR(fwnode->secondary);
3850}
3851
3852/**
3853 * set_primary_fwnode - Change the primary firmware node of a given device.
3854 * @dev: Device to handle.
3855 * @fwnode: New primary firmware node of the device.
3856 *
3857 * Set the device's firmware node pointer to @fwnode, but if a secondary
3858 * firmware node of the device is present, preserve it.
3859 */
3860void set_primary_fwnode(struct device *dev, struct fwnode_handle *fwnode)
3861{
3862 if (fwnode) {
3863 struct fwnode_handle *fn = dev->fwnode;
3864
3865 if (fwnode_is_primary(fn))
3866 fn = fn->secondary;
3867
Mika Westerberg55f89a82015-11-30 17:11:39 +02003868 if (fn) {
3869 WARN_ON(fwnode->secondary);
3870 fwnode->secondary = fn;
3871 }
Rafael J. Wysocki97badf82015-04-03 23:23:37 +02003872 dev->fwnode = fwnode;
3873 } else {
3874 dev->fwnode = fwnode_is_primary(dev->fwnode) ?
3875 dev->fwnode->secondary : NULL;
3876 }
3877}
3878EXPORT_SYMBOL_GPL(set_primary_fwnode);
3879
3880/**
3881 * set_secondary_fwnode - Change the secondary firmware node of a given device.
3882 * @dev: Device to handle.
3883 * @fwnode: New secondary firmware node of the device.
3884 *
3885 * If a primary firmware node of the device is present, set its secondary
3886 * pointer to @fwnode. Otherwise, set the device's firmware node pointer to
3887 * @fwnode.
3888 */
3889void set_secondary_fwnode(struct device *dev, struct fwnode_handle *fwnode)
3890{
3891 if (fwnode)
3892 fwnode->secondary = ERR_PTR(-ENODEV);
3893
3894 if (fwnode_is_primary(dev->fwnode))
3895 dev->fwnode->secondary = fwnode;
3896 else
3897 dev->fwnode = fwnode;
3898}
Johan Hovold4e75e1d2017-06-06 17:59:00 +02003899
3900/**
3901 * device_set_of_node_from_dev - reuse device-tree node of another device
3902 * @dev: device whose device-tree node is being set
3903 * @dev2: device whose device-tree node is being reused
3904 *
3905 * Takes another reference to the new device-tree node after first dropping
3906 * any reference held to the old node.
3907 */
3908void device_set_of_node_from_dev(struct device *dev, const struct device *dev2)
3909{
3910 of_node_put(dev->of_node);
3911 dev->of_node = of_node_get(dev2->of_node);
3912 dev->of_node_reused = true;
3913}
3914EXPORT_SYMBOL_GPL(device_set_of_node_from_dev);
Suzuki K Poulose65b66682019-06-14 18:54:01 +01003915
Suzuki K Poulose6cda08a2019-07-23 23:18:32 +01003916int device_match_name(struct device *dev, const void *name)
3917{
3918 return sysfs_streq(dev_name(dev), name);
3919}
3920EXPORT_SYMBOL_GPL(device_match_name);
3921
Suzuki K Poulose65b66682019-06-14 18:54:01 +01003922int device_match_of_node(struct device *dev, const void *np)
3923{
3924 return dev->of_node == np;
3925}
3926EXPORT_SYMBOL_GPL(device_match_of_node);
Suzuki K Poulose67843bb2019-07-23 23:18:34 +01003927
3928int device_match_fwnode(struct device *dev, const void *fwnode)
3929{
3930 return dev_fwnode(dev) == fwnode;
3931}
3932EXPORT_SYMBOL_GPL(device_match_fwnode);
Suzuki K Poulose4495dfd2019-07-23 23:18:35 +01003933
3934int device_match_devt(struct device *dev, const void *pdevt)
3935{
3936 return dev->devt == *(dev_t *)pdevt;
3937}
3938EXPORT_SYMBOL_GPL(device_match_devt);
Suzuki K Poulose00500142019-07-23 23:18:36 +01003939
3940int device_match_acpi_dev(struct device *dev, const void *adev)
3941{
3942 return ACPI_COMPANION(dev) == adev;
3943}
3944EXPORT_SYMBOL(device_match_acpi_dev);
Suzuki K Poulose6bf85ba2019-07-23 23:18:37 +01003945
3946int device_match_any(struct device *dev, const void *unused)
3947{
3948 return 1;
3949}
3950EXPORT_SYMBOL_GPL(device_match_any);