blob: 988f34ce2eb04d91209240709a75bf4d7a8183e1 [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
67int device_links_read_lock(void)
68{
69 return srcu_read_lock(&device_links_srcu);
70}
71
72void device_links_read_unlock(int idx)
73{
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,
526 links.needs_suppliers)
527 if (!fwnode_call_int_op(dev->fwnode, add_links, dev))
528 list_del_init(&dev->links.needs_suppliers);
529 mutex_unlock(&wfs_lock);
530}
531
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100532static void device_link_free(struct device_link *link)
533{
Rafael J. Wysockia1fdbfb2019-02-01 01:52:45 +0100534 while (refcount_dec_not_one(&link->rpm_active))
535 pm_runtime_put(link->supplier);
536
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100537 put_device(link->consumer);
538 put_device(link->supplier);
539 kfree(link);
540}
541
542#ifdef CONFIG_SRCU
543static void __device_link_free_srcu(struct rcu_head *rhead)
544{
545 device_link_free(container_of(rhead, struct device_link, rcu_head));
546}
547
Lukas Wunneread18c22018-02-10 19:27:12 +0100548static void __device_link_del(struct kref *kref)
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100549{
Lukas Wunneread18c22018-02-10 19:27:12 +0100550 struct device_link *link = container_of(kref, struct device_link, kref);
551
Jerome Brunet8a4b3262018-12-21 17:23:41 +0100552 dev_dbg(link->consumer, "Dropping the link to %s\n",
553 dev_name(link->supplier));
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100554
Rafael J. Wysockibaa88092016-10-30 17:32:43 +0100555 if (link->flags & DL_FLAG_PM_RUNTIME)
556 pm_runtime_drop_link(link->consumer);
557
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100558 list_del_rcu(&link->s_node);
559 list_del_rcu(&link->c_node);
560 call_srcu(&device_links_srcu, &link->rcu_head, __device_link_free_srcu);
561}
562#else /* !CONFIG_SRCU */
Lukas Wunneread18c22018-02-10 19:27:12 +0100563static void __device_link_del(struct kref *kref)
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100564{
Lukas Wunneread18c22018-02-10 19:27:12 +0100565 struct device_link *link = container_of(kref, struct device_link, kref);
566
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100567 dev_info(link->consumer, "Dropping the link to %s\n",
568 dev_name(link->supplier));
569
Lukas Wunner433986c2018-02-10 19:13:58 +0100570 if (link->flags & DL_FLAG_PM_RUNTIME)
571 pm_runtime_drop_link(link->consumer);
572
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100573 list_del(&link->s_node);
574 list_del(&link->c_node);
575 device_link_free(link);
576}
577#endif /* !CONFIG_SRCU */
578
Rafael J. Wysocki72175d42019-02-01 01:58:33 +0100579static void device_link_put_kref(struct device_link *link)
580{
581 if (link->flags & DL_FLAG_STATELESS)
582 kref_put(&link->kref, __device_link_del);
583 else
584 WARN(1, "Unable to drop a managed device link reference\n");
585}
586
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100587/**
Rafael J. Wysocki72175d42019-02-01 01:58:33 +0100588 * device_link_del - Delete a stateless link between two devices.
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100589 * @link: Device link to delete.
590 *
591 * The caller must ensure proper synchronization of this function with runtime
Lukas Wunneread18c22018-02-10 19:27:12 +0100592 * PM. If the link was added multiple times, it needs to be deleted as often.
593 * Care is required for hotplugged devices: Their links are purged on removal
594 * and calling device_link_del() is then no longer allowed.
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100595 */
596void device_link_del(struct device_link *link)
597{
598 device_links_write_lock();
599 device_pm_lock();
Rafael J. Wysocki72175d42019-02-01 01:58:33 +0100600 device_link_put_kref(link);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100601 device_pm_unlock();
602 device_links_write_unlock();
603}
604EXPORT_SYMBOL_GPL(device_link_del);
605
pascal pailletd8842212018-07-05 14:25:56 +0000606/**
Rafael J. Wysocki72175d42019-02-01 01:58:33 +0100607 * device_link_remove - Delete a stateless link between two devices.
pascal pailletd8842212018-07-05 14:25:56 +0000608 * @consumer: Consumer end of the link.
609 * @supplier: Supplier end of the link.
610 *
611 * The caller must ensure proper synchronization of this function with runtime
612 * PM.
613 */
614void device_link_remove(void *consumer, struct device *supplier)
615{
616 struct device_link *link;
617
618 if (WARN_ON(consumer == supplier))
619 return;
620
621 device_links_write_lock();
622 device_pm_lock();
623
624 list_for_each_entry(link, &supplier->links.consumers, s_node) {
625 if (link->consumer == consumer) {
Rafael J. Wysocki72175d42019-02-01 01:58:33 +0100626 device_link_put_kref(link);
pascal pailletd8842212018-07-05 14:25:56 +0000627 break;
628 }
629 }
630
631 device_pm_unlock();
632 device_links_write_unlock();
633}
634EXPORT_SYMBOL_GPL(device_link_remove);
635
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100636static void device_links_missing_supplier(struct device *dev)
637{
638 struct device_link *link;
639
640 list_for_each_entry(link, &dev->links.suppliers, c_node)
641 if (link->status == DL_STATE_CONSUMER_PROBE)
642 WRITE_ONCE(link->status, DL_STATE_AVAILABLE);
643}
644
645/**
646 * device_links_check_suppliers - Check presence of supplier drivers.
647 * @dev: Consumer device.
648 *
649 * Check links from this device to any suppliers. Walk the list of the device's
650 * links to suppliers and see if all of them are available. If not, simply
651 * return -EPROBE_DEFER.
652 *
653 * We need to guarantee that the supplier will not go away after the check has
654 * been positive here. It only can go away in __device_release_driver() and
655 * that function checks the device's links to consumers. This means we need to
656 * mark the link as "consumer probe in progress" to make the supplier removal
657 * wait for us to complete (or bad things may happen).
658 *
Rafael J. Wysocki515db262019-07-16 17:21:06 +0200659 * Links without the DL_FLAG_MANAGED flag set are ignored.
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100660 */
661int device_links_check_suppliers(struct device *dev)
662{
663 struct device_link *link;
664 int ret = 0;
665
Saravana Kannane2ae9bc2019-09-04 14:11:21 -0700666 /*
667 * Device waiting for supplier to become available is not allowed to
668 * probe.
669 */
670 mutex_lock(&wfs_lock);
Saravana Kannanbcbbcfd2019-10-28 15:00:23 -0700671 if (!list_empty(&dev->links.needs_suppliers) &&
672 dev->links.need_for_probe) {
Saravana Kannane2ae9bc2019-09-04 14:11:21 -0700673 mutex_unlock(&wfs_lock);
674 return -EPROBE_DEFER;
675 }
676 mutex_unlock(&wfs_lock);
677
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100678 device_links_write_lock();
679
680 list_for_each_entry(link, &dev->links.suppliers, c_node) {
Saravana Kannan05ef9832019-10-28 15:00:22 -0700681 if (!(link->flags & DL_FLAG_MANAGED) ||
682 link->flags & DL_FLAG_SYNC_STATE_ONLY)
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100683 continue;
684
685 if (link->status != DL_STATE_AVAILABLE) {
686 device_links_missing_supplier(dev);
687 ret = -EPROBE_DEFER;
688 break;
689 }
690 WRITE_ONCE(link->status, DL_STATE_CONSUMER_PROBE);
691 }
692 dev->links.status = DL_DEV_PROBING;
693
694 device_links_write_unlock();
695 return ret;
696}
697
Saravana Kannan26e77702019-11-14 14:56:45 -0800698/**
699 * __device_links_queue_sync_state - Queue a device for sync_state() callback
700 * @dev: Device to call sync_state() on
701 * @list: List head to queue the @dev on
702 *
703 * Queues a device for a sync_state() callback when the device links write lock
704 * isn't held. This allows the sync_state() execution flow to use device links
705 * APIs. The caller must ensure this function is called with
706 * device_links_write_lock() held.
707 *
708 * This function does a get_device() to make sure the device is not freed while
709 * on this list.
710 *
711 * So the caller must also ensure that device_links_flush_sync_list() is called
712 * as soon as the caller releases device_links_write_lock(). This is necessary
713 * to make sure the sync_state() is called in a timely fashion and the
714 * put_device() is called on this device.
715 */
716static void __device_links_queue_sync_state(struct device *dev,
717 struct list_head *list)
Saravana Kannanfc5a2512019-09-04 14:11:23 -0700718{
719 struct device_link *link;
720
721 if (dev->state_synced)
722 return;
723
724 list_for_each_entry(link, &dev->links.consumers, s_node) {
725 if (!(link->flags & DL_FLAG_MANAGED))
726 continue;
727 if (link->status != DL_STATE_ACTIVE)
728 return;
729 }
730
Saravana Kannan26e77702019-11-14 14:56:45 -0800731 /*
732 * Set the flag here to avoid adding the same device to a list more
733 * than once. This can happen if new consumers get added to the device
734 * and probed before the list is flushed.
735 */
Saravana Kannanfc5a2512019-09-04 14:11:23 -0700736 dev->state_synced = true;
Saravana Kannan26e77702019-11-14 14:56:45 -0800737
738 if (WARN_ON(!list_empty(&dev->links.defer_sync)))
739 return;
740
741 get_device(dev);
742 list_add_tail(&dev->links.defer_sync, list);
743}
744
745/**
746 * device_links_flush_sync_list - Call sync_state() on a list of devices
747 * @list: List of devices to call sync_state() on
748 *
749 * Calls sync_state() on all the devices that have been queued for it. This
750 * function is used in conjunction with __device_links_queue_sync_state().
751 */
752static void device_links_flush_sync_list(struct list_head *list)
753{
754 struct device *dev, *tmp;
755
756 list_for_each_entry_safe(dev, tmp, list, links.defer_sync) {
757 list_del_init(&dev->links.defer_sync);
758
759 device_lock(dev);
760
761 if (dev->bus->sync_state)
762 dev->bus->sync_state(dev);
763 else if (dev->driver && dev->driver->sync_state)
764 dev->driver->sync_state(dev);
765
766 device_unlock(dev);
767
768 put_device(dev);
769 }
Saravana Kannanfc5a2512019-09-04 14:11:23 -0700770}
771
772void device_links_supplier_sync_state_pause(void)
773{
774 device_links_write_lock();
775 defer_sync_state_count++;
776 device_links_write_unlock();
777}
778
779void device_links_supplier_sync_state_resume(void)
780{
781 struct device *dev, *tmp;
Saravana Kannan26e77702019-11-14 14:56:45 -0800782 LIST_HEAD(sync_list);
Saravana Kannanfc5a2512019-09-04 14:11:23 -0700783
784 device_links_write_lock();
785 if (!defer_sync_state_count) {
786 WARN(true, "Unmatched sync_state pause/resume!");
787 goto out;
788 }
789 defer_sync_state_count--;
790 if (defer_sync_state_count)
791 goto out;
792
793 list_for_each_entry_safe(dev, tmp, &deferred_sync, links.defer_sync) {
Saravana Kannan26e77702019-11-14 14:56:45 -0800794 /*
795 * Delete from deferred_sync list before queuing it to
796 * sync_list because defer_sync is used for both lists.
797 */
Saravana Kannanfc5a2512019-09-04 14:11:23 -0700798 list_del_init(&dev->links.defer_sync);
Saravana Kannan26e77702019-11-14 14:56:45 -0800799 __device_links_queue_sync_state(dev, &sync_list);
Saravana Kannanfc5a2512019-09-04 14:11:23 -0700800 }
801out:
802 device_links_write_unlock();
Saravana Kannan26e77702019-11-14 14:56:45 -0800803
804 device_links_flush_sync_list(&sync_list);
Saravana Kannanfc5a2512019-09-04 14:11:23 -0700805}
806
807static int sync_state_resume_initcall(void)
808{
809 device_links_supplier_sync_state_resume();
810 return 0;
811}
812late_initcall(sync_state_resume_initcall);
813
814static void __device_links_supplier_defer_sync(struct device *sup)
815{
816 if (list_empty(&sup->links.defer_sync))
817 list_add_tail(&sup->links.defer_sync, &deferred_sync);
818}
819
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100820/**
821 * device_links_driver_bound - Update device links after probing its driver.
822 * @dev: Device to update the links for.
823 *
824 * The probe has been successful, so update links from this device to any
825 * consumers by changing their status to "available".
826 *
827 * Also change the status of @dev's links to suppliers to "active".
828 *
Rafael J. Wysocki515db262019-07-16 17:21:06 +0200829 * Links without the DL_FLAG_MANAGED flag set are ignored.
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100830 */
831void device_links_driver_bound(struct device *dev)
832{
833 struct device_link *link;
Saravana Kannan26e77702019-11-14 14:56:45 -0800834 LIST_HEAD(sync_list);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100835
Saravana Kannanbcbbcfd2019-10-28 15:00:23 -0700836 /*
837 * If a device probes successfully, it's expected to have created all
838 * the device links it needs to or make new device links as it needs
839 * them. So, it no longer needs to wait on any suppliers.
840 */
841 mutex_lock(&wfs_lock);
842 list_del_init(&dev->links.needs_suppliers);
843 mutex_unlock(&wfs_lock);
844
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100845 device_links_write_lock();
846
847 list_for_each_entry(link, &dev->links.consumers, s_node) {
Rafael J. Wysocki515db262019-07-16 17:21:06 +0200848 if (!(link->flags & DL_FLAG_MANAGED))
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100849 continue;
850
Rafael J. Wysocki15cfb092019-02-01 01:50:39 +0100851 /*
852 * Links created during consumer probe may be in the "consumer
853 * probe" state to start with if the supplier is still probing
854 * when they are created and they may become "active" if the
855 * consumer probe returns first. Skip them here.
856 */
857 if (link->status == DL_STATE_CONSUMER_PROBE ||
858 link->status == DL_STATE_ACTIVE)
859 continue;
860
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100861 WARN_ON(link->status != DL_STATE_DORMANT);
862 WRITE_ONCE(link->status, DL_STATE_AVAILABLE);
Rafael J. Wysockie7dd4012019-02-01 01:59:42 +0100863
864 if (link->flags & DL_FLAG_AUTOPROBE_CONSUMER)
865 driver_deferred_probe_add(link->consumer);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100866 }
867
868 list_for_each_entry(link, &dev->links.suppliers, c_node) {
Rafael J. Wysocki515db262019-07-16 17:21:06 +0200869 if (!(link->flags & DL_FLAG_MANAGED))
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100870 continue;
871
872 WARN_ON(link->status != DL_STATE_CONSUMER_PROBE);
873 WRITE_ONCE(link->status, DL_STATE_ACTIVE);
Saravana Kannanfc5a2512019-09-04 14:11:23 -0700874
875 if (defer_sync_state_count)
876 __device_links_supplier_defer_sync(link->supplier);
877 else
Saravana Kannan26e77702019-11-14 14:56:45 -0800878 __device_links_queue_sync_state(link->supplier,
879 &sync_list);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100880 }
881
882 dev->links.status = DL_DEV_DRIVER_BOUND;
883
884 device_links_write_unlock();
Saravana Kannan26e77702019-11-14 14:56:45 -0800885
886 device_links_flush_sync_list(&sync_list);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100887}
888
Rafael J. Wysocki515db262019-07-16 17:21:06 +0200889static void device_link_drop_managed(struct device_link *link)
890{
891 link->flags &= ~DL_FLAG_MANAGED;
892 WRITE_ONCE(link->status, DL_STATE_NONE);
893 kref_put(&link->kref, __device_link_del);
894}
895
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100896/**
897 * __device_links_no_driver - Update links of a device without a driver.
898 * @dev: Device without a drvier.
899 *
900 * Delete all non-persistent links from this device to any suppliers.
901 *
902 * Persistent links stay around, but their status is changed to "available",
903 * unless they already are in the "supplier unbind in progress" state in which
904 * case they need not be updated.
905 *
Rafael J. Wysocki515db262019-07-16 17:21:06 +0200906 * Links without the DL_FLAG_MANAGED flag set are ignored.
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100907 */
908static void __device_links_no_driver(struct device *dev)
909{
910 struct device_link *link, *ln;
911
912 list_for_each_entry_safe_reverse(link, ln, &dev->links.suppliers, c_node) {
Rafael J. Wysocki515db262019-07-16 17:21:06 +0200913 if (!(link->flags & DL_FLAG_MANAGED))
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100914 continue;
915
Vivek Gautame88728f2018-06-27 18:20:55 +0530916 if (link->flags & DL_FLAG_AUTOREMOVE_CONSUMER)
Rafael J. Wysocki515db262019-07-16 17:21:06 +0200917 device_link_drop_managed(link);
Rafael J. Wysocki15cfb092019-02-01 01:50:39 +0100918 else if (link->status == DL_STATE_CONSUMER_PROBE ||
919 link->status == DL_STATE_ACTIVE)
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100920 WRITE_ONCE(link->status, DL_STATE_AVAILABLE);
921 }
922
923 dev->links.status = DL_DEV_NO_DRIVER;
924}
925
Rafael J. Wysocki15cfb092019-02-01 01:50:39 +0100926/**
927 * device_links_no_driver - Update links after failing driver probe.
928 * @dev: Device whose driver has just failed to probe.
929 *
930 * Clean up leftover links to consumers for @dev and invoke
931 * %__device_links_no_driver() to update links to suppliers for it as
932 * appropriate.
933 *
Rafael J. Wysocki515db262019-07-16 17:21:06 +0200934 * Links without the DL_FLAG_MANAGED flag set are ignored.
Rafael J. Wysocki15cfb092019-02-01 01:50:39 +0100935 */
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100936void device_links_no_driver(struct device *dev)
937{
Rafael J. Wysocki15cfb092019-02-01 01:50:39 +0100938 struct device_link *link;
939
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100940 device_links_write_lock();
Rafael J. Wysocki15cfb092019-02-01 01:50:39 +0100941
942 list_for_each_entry(link, &dev->links.consumers, s_node) {
Rafael J. Wysocki515db262019-07-16 17:21:06 +0200943 if (!(link->flags & DL_FLAG_MANAGED))
Rafael J. Wysocki15cfb092019-02-01 01:50:39 +0100944 continue;
945
946 /*
947 * The probe has failed, so if the status of the link is
948 * "consumer probe" or "active", it must have been added by
949 * a probing consumer while this device was still probing.
950 * Change its state to "dormant", as it represents a valid
951 * relationship, but it is not functionally meaningful.
952 */
953 if (link->status == DL_STATE_CONSUMER_PROBE ||
954 link->status == DL_STATE_ACTIVE)
955 WRITE_ONCE(link->status, DL_STATE_DORMANT);
956 }
957
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100958 __device_links_no_driver(dev);
Rafael J. Wysocki15cfb092019-02-01 01:50:39 +0100959
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100960 device_links_write_unlock();
961}
962
963/**
964 * device_links_driver_cleanup - Update links after driver removal.
965 * @dev: Device whose driver has just gone away.
966 *
967 * Update links to consumers for @dev by changing their status to "dormant" and
968 * invoke %__device_links_no_driver() to update links to suppliers for it as
969 * appropriate.
970 *
Rafael J. Wysocki515db262019-07-16 17:21:06 +0200971 * Links without the DL_FLAG_MANAGED flag set are ignored.
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100972 */
973void device_links_driver_cleanup(struct device *dev)
974{
Rafael J. Wysockic8d50982019-02-01 01:45:55 +0100975 struct device_link *link, *ln;
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100976
977 device_links_write_lock();
978
Rafael J. Wysockic8d50982019-02-01 01:45:55 +0100979 list_for_each_entry_safe(link, ln, &dev->links.consumers, s_node) {
Rafael J. Wysocki515db262019-07-16 17:21:06 +0200980 if (!(link->flags & DL_FLAG_MANAGED))
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100981 continue;
982
Vivek Gautame88728f2018-06-27 18:20:55 +0530983 WARN_ON(link->flags & DL_FLAG_AUTOREMOVE_CONSUMER);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100984 WARN_ON(link->status != DL_STATE_SUPPLIER_UNBIND);
Vivek Gautam1689cac2018-06-27 18:20:56 +0530985
986 /*
987 * autoremove the links between this @dev and its consumer
988 * devices that are not active, i.e. where the link state
989 * has moved to DL_STATE_SUPPLIER_UNBIND.
990 */
991 if (link->status == DL_STATE_SUPPLIER_UNBIND &&
992 link->flags & DL_FLAG_AUTOREMOVE_SUPPLIER)
Rafael J. Wysocki515db262019-07-16 17:21:06 +0200993 device_link_drop_managed(link);
Vivek Gautam1689cac2018-06-27 18:20:56 +0530994
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100995 WRITE_ONCE(link->status, DL_STATE_DORMANT);
996 }
997
Saravana Kannanfc5a2512019-09-04 14:11:23 -0700998 list_del_init(&dev->links.defer_sync);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100999 __device_links_no_driver(dev);
1000
1001 device_links_write_unlock();
1002}
1003
1004/**
1005 * device_links_busy - Check if there are any busy links to consumers.
1006 * @dev: Device to check.
1007 *
1008 * Check each consumer of the device and return 'true' if its link's status
1009 * is one of "consumer probe" or "active" (meaning that the given consumer is
1010 * probing right now or its driver is present). Otherwise, change the link
1011 * state to "supplier unbind" to prevent the consumer from being probed
1012 * successfully going forward.
1013 *
1014 * Return 'false' if there are no probing or active consumers.
1015 *
Rafael J. Wysocki515db262019-07-16 17:21:06 +02001016 * Links without the DL_FLAG_MANAGED flag set are ignored.
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001017 */
1018bool device_links_busy(struct device *dev)
1019{
1020 struct device_link *link;
1021 bool ret = false;
1022
1023 device_links_write_lock();
1024
1025 list_for_each_entry(link, &dev->links.consumers, s_node) {
Rafael J. Wysocki515db262019-07-16 17:21:06 +02001026 if (!(link->flags & DL_FLAG_MANAGED))
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001027 continue;
1028
1029 if (link->status == DL_STATE_CONSUMER_PROBE
1030 || link->status == DL_STATE_ACTIVE) {
1031 ret = true;
1032 break;
1033 }
1034 WRITE_ONCE(link->status, DL_STATE_SUPPLIER_UNBIND);
1035 }
1036
1037 dev->links.status = DL_DEV_UNBINDING;
1038
1039 device_links_write_unlock();
1040 return ret;
1041}
1042
1043/**
1044 * device_links_unbind_consumers - Force unbind consumers of the given device.
1045 * @dev: Device to unbind the consumers of.
1046 *
1047 * Walk the list of links to consumers for @dev and if any of them is in the
1048 * "consumer probe" state, wait for all device probes in progress to complete
1049 * and start over.
1050 *
1051 * If that's not the case, change the status of the link to "supplier unbind"
1052 * and check if the link was in the "active" state. If so, force the consumer
1053 * driver to unbind and start over (the consumer will not re-probe as we have
1054 * changed the state of the link already).
1055 *
Rafael J. Wysocki515db262019-07-16 17:21:06 +02001056 * Links without the DL_FLAG_MANAGED flag set are ignored.
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001057 */
1058void device_links_unbind_consumers(struct device *dev)
1059{
1060 struct device_link *link;
1061
1062 start:
1063 device_links_write_lock();
1064
1065 list_for_each_entry(link, &dev->links.consumers, s_node) {
1066 enum device_link_state status;
1067
Saravana Kannan05ef9832019-10-28 15:00:22 -07001068 if (!(link->flags & DL_FLAG_MANAGED) ||
1069 link->flags & DL_FLAG_SYNC_STATE_ONLY)
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001070 continue;
1071
1072 status = link->status;
1073 if (status == DL_STATE_CONSUMER_PROBE) {
1074 device_links_write_unlock();
1075
1076 wait_for_device_probe();
1077 goto start;
1078 }
1079 WRITE_ONCE(link->status, DL_STATE_SUPPLIER_UNBIND);
1080 if (status == DL_STATE_ACTIVE) {
1081 struct device *consumer = link->consumer;
1082
1083 get_device(consumer);
1084
1085 device_links_write_unlock();
1086
1087 device_release_driver_internal(consumer, NULL,
1088 consumer->parent);
1089 put_device(consumer);
1090 goto start;
1091 }
1092 }
1093
1094 device_links_write_unlock();
1095}
1096
1097/**
1098 * device_links_purge - Delete existing links to other devices.
1099 * @dev: Target device.
1100 */
1101static void device_links_purge(struct device *dev)
1102{
1103 struct device_link *link, *ln;
1104
Saravana Kannane2ae9bc2019-09-04 14:11:21 -07001105 mutex_lock(&wfs_lock);
1106 list_del(&dev->links.needs_suppliers);
1107 mutex_unlock(&wfs_lock);
1108
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001109 /*
1110 * Delete all of the remaining links from this device to any other
1111 * devices (either consumers or suppliers).
1112 */
1113 device_links_write_lock();
1114
1115 list_for_each_entry_safe_reverse(link, ln, &dev->links.suppliers, c_node) {
1116 WARN_ON(link->status == DL_STATE_ACTIVE);
Lukas Wunneread18c22018-02-10 19:27:12 +01001117 __device_link_del(&link->kref);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001118 }
1119
1120 list_for_each_entry_safe_reverse(link, ln, &dev->links.consumers, s_node) {
1121 WARN_ON(link->status != DL_STATE_DORMANT &&
1122 link->status != DL_STATE_NONE);
Lukas Wunneread18c22018-02-10 19:27:12 +01001123 __device_link_del(&link->kref);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001124 }
1125
1126 device_links_write_unlock();
1127}
1128
1129/* Device links support end. */
1130
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001131int (*platform_notify)(struct device *dev) = NULL;
1132int (*platform_notify_remove)(struct device *dev) = NULL;
Dan Williamse105b8b2008-04-21 10:51:07 -07001133static struct kobject *dev_kobj;
1134struct kobject *sysfs_dev_char_kobj;
1135struct kobject *sysfs_dev_block_kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136
Rafael J. Wysocki5e33bc42013-08-28 21:41:01 +02001137static DEFINE_MUTEX(device_hotplug_lock);
1138
1139void lock_device_hotplug(void)
1140{
1141 mutex_lock(&device_hotplug_lock);
1142}
1143
1144void unlock_device_hotplug(void)
1145{
1146 mutex_unlock(&device_hotplug_lock);
1147}
1148
1149int lock_device_hotplug_sysfs(void)
1150{
1151 if (mutex_trylock(&device_hotplug_lock))
1152 return 0;
1153
1154 /* Avoid busy looping (5 ms of sleep should do). */
1155 msleep(5);
1156 return restart_syscall();
1157}
1158
Greg Kroah-Hartman4e886c22008-01-27 12:12:43 -08001159#ifdef CONFIG_BLOCK
1160static inline int device_is_not_partition(struct device *dev)
1161{
1162 return !(dev->type == &part_type);
1163}
1164#else
1165static inline int device_is_not_partition(struct device *dev)
1166{
1167 return 1;
1168}
1169#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170
Heikki Krogerus07de0e82018-11-09 17:21:34 +03001171static int
1172device_platform_notify(struct device *dev, enum kobject_action action)
1173{
Heikki Krogerus7847a142018-11-09 17:21:35 +03001174 int ret;
1175
1176 ret = acpi_platform_notify(dev, action);
1177 if (ret)
1178 return ret;
1179
Heikki Krogerus59abd832018-11-09 17:21:36 +03001180 ret = software_node_notify(dev, action);
1181 if (ret)
1182 return ret;
1183
Heikki Krogerus07de0e82018-11-09 17:21:34 +03001184 if (platform_notify && action == KOBJ_ADD)
1185 platform_notify(dev);
1186 else if (platform_notify_remove && action == KOBJ_REMOVE)
1187 platform_notify_remove(dev);
1188 return 0;
1189}
1190
Alan Stern3e956372006-06-16 17:10:48 -04001191/**
1192 * dev_driver_string - Return a device's driver name, if at all possible
1193 * @dev: struct device to get the name of
1194 *
1195 * Will return the device's driver's name if it is bound to a device. If
yan9169c012012-04-20 21:08:45 +08001196 * the device is not bound to a driver, it will return the name of the bus
Alan Stern3e956372006-06-16 17:10:48 -04001197 * it is attached to. If it is not attached to a bus either, an empty
1198 * string will be returned.
1199 */
Jean Delvarebf9ca692008-07-30 12:29:21 -07001200const char *dev_driver_string(const struct device *dev)
Alan Stern3e956372006-06-16 17:10:48 -04001201{
Alan Stern35899722009-12-04 11:06:57 -05001202 struct device_driver *drv;
1203
1204 /* dev->driver can change to NULL underneath us because of unbinding,
1205 * so be careful about accessing it. dev->bus and dev->class should
1206 * never change once they are set, so they don't need special care.
1207 */
Mark Rutland6aa7de02017-10-23 14:07:29 -07001208 drv = READ_ONCE(dev->driver);
Alan Stern35899722009-12-04 11:06:57 -05001209 return drv ? drv->name :
Jean Delvarea456b702007-03-09 16:33:10 +01001210 (dev->bus ? dev->bus->name :
1211 (dev->class ? dev->class->name : ""));
Alan Stern3e956372006-06-16 17:10:48 -04001212}
Matthew Wilcox310a9222006-09-23 23:35:04 -06001213EXPORT_SYMBOL(dev_driver_string);
Alan Stern3e956372006-06-16 17:10:48 -04001214
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215#define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr)
1216
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001217static ssize_t dev_attr_show(struct kobject *kobj, struct attribute *attr,
1218 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001220 struct device_attribute *dev_attr = to_dev_attr(attr);
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +02001221 struct device *dev = kobj_to_dev(kobj);
Dmitry Torokhov4a0c20b2005-04-29 01:23:47 -05001222 ssize_t ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223
1224 if (dev_attr->show)
Yani Ioannou54b6f352005-05-17 06:39:34 -04001225 ret = dev_attr->show(dev, dev_attr, buf);
Andrew Morton815d2d52008-03-04 15:09:07 -08001226 if (ret >= (ssize_t)PAGE_SIZE) {
Sergey Senozhatskya52668c2017-12-11 21:50:21 +09001227 printk("dev_attr_show: %pS returned bad count\n",
1228 dev_attr->show);
Andrew Morton815d2d52008-03-04 15:09:07 -08001229 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 return ret;
1231}
1232
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001233static ssize_t dev_attr_store(struct kobject *kobj, struct attribute *attr,
1234 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001236 struct device_attribute *dev_attr = to_dev_attr(attr);
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +02001237 struct device *dev = kobj_to_dev(kobj);
Dmitry Torokhov4a0c20b2005-04-29 01:23:47 -05001238 ssize_t ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239
1240 if (dev_attr->store)
Yani Ioannou54b6f352005-05-17 06:39:34 -04001241 ret = dev_attr->store(dev, dev_attr, buf, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 return ret;
1243}
1244
Emese Revfy52cf25d2010-01-19 02:58:23 +01001245static const struct sysfs_ops dev_sysfs_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 .show = dev_attr_show,
1247 .store = dev_attr_store,
1248};
1249
Kay Sieversca22e562011-12-14 14:29:38 -08001250#define to_ext_attr(x) container_of(x, struct dev_ext_attribute, attr)
1251
1252ssize_t device_store_ulong(struct device *dev,
1253 struct device_attribute *attr,
1254 const char *buf, size_t size)
1255{
1256 struct dev_ext_attribute *ea = to_ext_attr(attr);
Kaitao chengf88184b2018-11-06 08:34:54 -08001257 int ret;
1258 unsigned long new;
1259
1260 ret = kstrtoul(buf, 0, &new);
1261 if (ret)
1262 return ret;
Kay Sieversca22e562011-12-14 14:29:38 -08001263 *(unsigned long *)(ea->var) = new;
1264 /* Always return full write size even if we didn't consume all */
1265 return size;
1266}
1267EXPORT_SYMBOL_GPL(device_store_ulong);
1268
1269ssize_t device_show_ulong(struct device *dev,
1270 struct device_attribute *attr,
1271 char *buf)
1272{
1273 struct dev_ext_attribute *ea = to_ext_attr(attr);
1274 return snprintf(buf, PAGE_SIZE, "%lx\n", *(unsigned long *)(ea->var));
1275}
1276EXPORT_SYMBOL_GPL(device_show_ulong);
1277
1278ssize_t device_store_int(struct device *dev,
1279 struct device_attribute *attr,
1280 const char *buf, size_t size)
1281{
1282 struct dev_ext_attribute *ea = to_ext_attr(attr);
Kaitao chengf88184b2018-11-06 08:34:54 -08001283 int ret;
1284 long new;
1285
1286 ret = kstrtol(buf, 0, &new);
1287 if (ret)
1288 return ret;
1289
1290 if (new > INT_MAX || new < INT_MIN)
Kay Sieversca22e562011-12-14 14:29:38 -08001291 return -EINVAL;
1292 *(int *)(ea->var) = new;
1293 /* Always return full write size even if we didn't consume all */
1294 return size;
1295}
1296EXPORT_SYMBOL_GPL(device_store_int);
1297
1298ssize_t device_show_int(struct device *dev,
1299 struct device_attribute *attr,
1300 char *buf)
1301{
1302 struct dev_ext_attribute *ea = to_ext_attr(attr);
1303
1304 return snprintf(buf, PAGE_SIZE, "%d\n", *(int *)(ea->var));
1305}
1306EXPORT_SYMBOL_GPL(device_show_int);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307
Borislav Petkov91872392012-10-09 19:52:05 +02001308ssize_t device_store_bool(struct device *dev, struct device_attribute *attr,
1309 const char *buf, size_t size)
1310{
1311 struct dev_ext_attribute *ea = to_ext_attr(attr);
1312
1313 if (strtobool(buf, ea->var) < 0)
1314 return -EINVAL;
1315
1316 return size;
1317}
1318EXPORT_SYMBOL_GPL(device_store_bool);
1319
1320ssize_t device_show_bool(struct device *dev, struct device_attribute *attr,
1321 char *buf)
1322{
1323 struct dev_ext_attribute *ea = to_ext_attr(attr);
1324
1325 return snprintf(buf, PAGE_SIZE, "%d\n", *(bool *)(ea->var));
1326}
1327EXPORT_SYMBOL_GPL(device_show_bool);
1328
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329/**
Robert P. J. Dayf8878dc2013-06-01 20:17:34 -04001330 * device_release - free device structure.
1331 * @kobj: device's kobject.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 *
Robert P. J. Dayf8878dc2013-06-01 20:17:34 -04001333 * This is called once the reference count for the object
1334 * reaches 0. We forward the call to the device's release
1335 * method, which should handle actually freeing the structure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001337static void device_release(struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +02001339 struct device *dev = kobj_to_dev(kobj);
Greg Kroah-Hartmanfb069a52008-12-16 12:23:36 -08001340 struct device_private *p = dev->p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341
Ming Leia525a3d2012-07-25 01:42:29 +08001342 /*
1343 * Some platform devices are driven without driver attached
1344 * and managed resources may have been acquired. Make sure
1345 * all resources are released.
1346 *
1347 * Drivers still can add resources into device after device
1348 * is deleted but alive, so release devres here to avoid
1349 * possible memory leak.
1350 */
1351 devres_release_all(dev);
1352
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 if (dev->release)
1354 dev->release(dev);
Kay Sieversf9f852d2006-10-07 21:54:55 +02001355 else if (dev->type && dev->type->release)
1356 dev->type->release(dev);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07001357 else if (dev->class && dev->class->dev_release)
1358 dev->class->dev_release(dev);
Arjan van de Venf810a5c2008-07-25 19:45:39 -07001359 else
Ezequiel Garcia186bddb2018-12-03 13:44:35 -03001360 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 +01001361 dev_name(dev));
Greg Kroah-Hartmanfb069a52008-12-16 12:23:36 -08001362 kfree(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363}
1364
Eric W. Biedermanbc451f22010-03-30 11:31:25 -07001365static const void *device_namespace(struct kobject *kobj)
1366{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +02001367 struct device *dev = kobj_to_dev(kobj);
Eric W. Biedermanbc451f22010-03-30 11:31:25 -07001368 const void *ns = NULL;
1369
1370 if (dev->class && dev->class->ns_type)
1371 ns = dev->class->namespace(dev);
1372
1373 return ns;
1374}
1375
Dmitry Torokhov9944e892018-07-20 21:56:50 +00001376static void device_get_ownership(struct kobject *kobj, kuid_t *uid, kgid_t *gid)
1377{
1378 struct device *dev = kobj_to_dev(kobj);
1379
1380 if (dev->class && dev->class->get_ownership)
1381 dev->class->get_ownership(dev, uid, gid);
1382}
1383
Greg Kroah-Hartman8f4afc42007-10-11 10:47:49 -06001384static struct kobj_type device_ktype = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 .release = device_release,
1386 .sysfs_ops = &dev_sysfs_ops,
Eric W. Biedermanbc451f22010-03-30 11:31:25 -07001387 .namespace = device_namespace,
Dmitry Torokhov9944e892018-07-20 21:56:50 +00001388 .get_ownership = device_get_ownership,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389};
1390
1391
Kay Sievers312c0042005-11-16 09:00:00 +01001392static int dev_uevent_filter(struct kset *kset, struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393{
1394 struct kobj_type *ktype = get_ktype(kobj);
1395
Greg Kroah-Hartman8f4afc42007-10-11 10:47:49 -06001396 if (ktype == &device_ktype) {
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +02001397 struct device *dev = kobj_to_dev(kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 if (dev->bus)
1399 return 1;
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001400 if (dev->class)
1401 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 }
1403 return 0;
1404}
1405
Kay Sievers312c0042005-11-16 09:00:00 +01001406static const char *dev_uevent_name(struct kset *kset, struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +02001408 struct device *dev = kobj_to_dev(kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001410 if (dev->bus)
1411 return dev->bus->name;
1412 if (dev->class)
1413 return dev->class->name;
1414 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415}
1416
Kay Sievers7eff2e72007-08-14 15:15:12 +02001417static int dev_uevent(struct kset *kset, struct kobject *kobj,
1418 struct kobj_uevent_env *env)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +02001420 struct device *dev = kobj_to_dev(kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 int retval = 0;
1422
Kay Sievers6fcf53a2009-04-30 15:23:42 +02001423 /* add device node properties if present */
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001424 if (MAJOR(dev->devt)) {
Kay Sievers6fcf53a2009-04-30 15:23:42 +02001425 const char *tmp;
1426 const char *name;
Al Viro2c9ede52011-07-23 20:24:48 -04001427 umode_t mode = 0;
Greg Kroah-Hartman4e4098a2013-04-11 11:43:29 -07001428 kuid_t uid = GLOBAL_ROOT_UID;
1429 kgid_t gid = GLOBAL_ROOT_GID;
Kay Sievers6fcf53a2009-04-30 15:23:42 +02001430
Kay Sievers7eff2e72007-08-14 15:15:12 +02001431 add_uevent_var(env, "MAJOR=%u", MAJOR(dev->devt));
1432 add_uevent_var(env, "MINOR=%u", MINOR(dev->devt));
Kay Sievers3c2670e2013-04-06 09:56:00 -07001433 name = device_get_devnode(dev, &mode, &uid, &gid, &tmp);
Kay Sievers6fcf53a2009-04-30 15:23:42 +02001434 if (name) {
1435 add_uevent_var(env, "DEVNAME=%s", name);
Kay Sieverse454cea2009-09-18 23:01:12 +02001436 if (mode)
1437 add_uevent_var(env, "DEVMODE=%#o", mode & 0777);
Greg Kroah-Hartman4e4098a2013-04-11 11:43:29 -07001438 if (!uid_eq(uid, GLOBAL_ROOT_UID))
1439 add_uevent_var(env, "DEVUID=%u", from_kuid(&init_user_ns, uid));
1440 if (!gid_eq(gid, GLOBAL_ROOT_GID))
1441 add_uevent_var(env, "DEVGID=%u", from_kgid(&init_user_ns, gid));
Kay Sievers3c2670e2013-04-06 09:56:00 -07001442 kfree(tmp);
Kay Sievers6fcf53a2009-04-30 15:23:42 +02001443 }
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001444 }
1445
Kay Sievers414264f2007-03-12 21:08:57 +01001446 if (dev->type && dev->type->name)
Kay Sievers7eff2e72007-08-14 15:15:12 +02001447 add_uevent_var(env, "DEVTYPE=%s", dev->type->name);
Kay Sievers414264f2007-03-12 21:08:57 +01001448
Kay Sievers239378f2006-10-07 21:54:55 +02001449 if (dev->driver)
Kay Sievers7eff2e72007-08-14 15:15:12 +02001450 add_uevent_var(env, "DRIVER=%s", dev->driver->name);
Kay Sievers239378f2006-10-07 21:54:55 +02001451
Grant Likely07d57a32012-02-01 11:22:22 -07001452 /* Add common DT information about the device */
1453 of_device_uevent(dev, env);
1454
Kay Sievers7eff2e72007-08-14 15:15:12 +02001455 /* have the bus specific function add its stuff */
Kay Sievers312c0042005-11-16 09:00:00 +01001456 if (dev->bus && dev->bus->uevent) {
Kay Sievers7eff2e72007-08-14 15:15:12 +02001457 retval = dev->bus->uevent(dev, env);
Kay Sieversf9f852d2006-10-07 21:54:55 +02001458 if (retval)
Greg Kroah-Hartman7dc72b22007-11-28 23:49:41 -08001459 pr_debug("device: '%s': %s: bus uevent() returned %d\n",
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01001460 dev_name(dev), __func__, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 }
1462
Kay Sievers7eff2e72007-08-14 15:15:12 +02001463 /* have the class specific function add its stuff */
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07001464 if (dev->class && dev->class->dev_uevent) {
Kay Sievers7eff2e72007-08-14 15:15:12 +02001465 retval = dev->class->dev_uevent(dev, env);
Kay Sieversf9f852d2006-10-07 21:54:55 +02001466 if (retval)
Greg Kroah-Hartman7dc72b22007-11-28 23:49:41 -08001467 pr_debug("device: '%s': %s: class uevent() "
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01001468 "returned %d\n", dev_name(dev),
Harvey Harrison2b3a3022008-03-04 16:41:05 -08001469 __func__, retval);
Kay Sieversf9f852d2006-10-07 21:54:55 +02001470 }
1471
Stefan Weileef35c22010-08-06 21:11:15 +02001472 /* have the device type specific function add its stuff */
Kay Sieversf9f852d2006-10-07 21:54:55 +02001473 if (dev->type && dev->type->uevent) {
Kay Sievers7eff2e72007-08-14 15:15:12 +02001474 retval = dev->type->uevent(dev, env);
Kay Sieversf9f852d2006-10-07 21:54:55 +02001475 if (retval)
Greg Kroah-Hartman7dc72b22007-11-28 23:49:41 -08001476 pr_debug("device: '%s': %s: dev_type uevent() "
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01001477 "returned %d\n", dev_name(dev),
Harvey Harrison2b3a3022008-03-04 16:41:05 -08001478 __func__, retval);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07001479 }
1480
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 return retval;
1482}
1483
Emese Revfy9cd43612009-12-31 14:52:51 +01001484static const struct kset_uevent_ops device_uevent_ops = {
Kay Sievers312c0042005-11-16 09:00:00 +01001485 .filter = dev_uevent_filter,
1486 .name = dev_uevent_name,
1487 .uevent = dev_uevent,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488};
1489
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07001490static ssize_t uevent_show(struct device *dev, struct device_attribute *attr,
Kay Sievers16574dc2007-04-06 01:40:38 +02001491 char *buf)
1492{
1493 struct kobject *top_kobj;
1494 struct kset *kset;
Kay Sievers7eff2e72007-08-14 15:15:12 +02001495 struct kobj_uevent_env *env = NULL;
Kay Sievers16574dc2007-04-06 01:40:38 +02001496 int i;
1497 size_t count = 0;
1498 int retval;
1499
1500 /* search the kset, the device belongs to */
1501 top_kobj = &dev->kobj;
Kay Sievers5c5daf62007-08-12 20:43:55 +02001502 while (!top_kobj->kset && top_kobj->parent)
1503 top_kobj = top_kobj->parent;
Kay Sievers16574dc2007-04-06 01:40:38 +02001504 if (!top_kobj->kset)
1505 goto out;
Kay Sievers5c5daf62007-08-12 20:43:55 +02001506
Kay Sievers16574dc2007-04-06 01:40:38 +02001507 kset = top_kobj->kset;
1508 if (!kset->uevent_ops || !kset->uevent_ops->uevent)
1509 goto out;
1510
1511 /* respect filter */
1512 if (kset->uevent_ops && kset->uevent_ops->filter)
1513 if (!kset->uevent_ops->filter(kset, &dev->kobj))
1514 goto out;
1515
Kay Sievers7eff2e72007-08-14 15:15:12 +02001516 env = kzalloc(sizeof(struct kobj_uevent_env), GFP_KERNEL);
1517 if (!env)
Greg Kroah-Hartmanc7308c82007-05-02 14:14:11 +02001518 return -ENOMEM;
1519
Kay Sievers16574dc2007-04-06 01:40:38 +02001520 /* let the kset specific function add its keys */
Kay Sievers7eff2e72007-08-14 15:15:12 +02001521 retval = kset->uevent_ops->uevent(kset, &dev->kobj, env);
Kay Sievers16574dc2007-04-06 01:40:38 +02001522 if (retval)
1523 goto out;
1524
1525 /* copy keys to file */
Kay Sievers7eff2e72007-08-14 15:15:12 +02001526 for (i = 0; i < env->envp_idx; i++)
1527 count += sprintf(&buf[count], "%s\n", env->envp[i]);
Kay Sievers16574dc2007-04-06 01:40:38 +02001528out:
Kay Sievers7eff2e72007-08-14 15:15:12 +02001529 kfree(env);
Kay Sievers16574dc2007-04-06 01:40:38 +02001530 return count;
1531}
1532
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07001533static ssize_t uevent_store(struct device *dev, struct device_attribute *attr,
Kay Sieversa7fd6702005-10-01 14:49:43 +02001534 const char *buf, size_t count)
1535{
Peter Rajnohadf44b472018-12-05 12:27:44 +01001536 int rc;
1537
1538 rc = kobject_synth_uevent(&dev->kobj, buf, count);
1539
1540 if (rc) {
Peter Rajnohaf36776f2017-05-09 15:22:30 +02001541 dev_err(dev, "uevent: failed to send synthetic uevent\n");
Peter Rajnohadf44b472018-12-05 12:27:44 +01001542 return rc;
1543 }
Kay Sievers60a96a52007-07-08 22:29:26 +02001544
Kay Sieversa7fd6702005-10-01 14:49:43 +02001545 return count;
1546}
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07001547static DEVICE_ATTR_RW(uevent);
Kay Sieversa7fd6702005-10-01 14:49:43 +02001548
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07001549static ssize_t online_show(struct device *dev, struct device_attribute *attr,
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02001550 char *buf)
1551{
1552 bool val;
1553
Rafael J. Wysocki5e33bc42013-08-28 21:41:01 +02001554 device_lock(dev);
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02001555 val = !dev->offline;
Rafael J. Wysocki5e33bc42013-08-28 21:41:01 +02001556 device_unlock(dev);
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02001557 return sprintf(buf, "%u\n", val);
1558}
1559
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07001560static ssize_t online_store(struct device *dev, struct device_attribute *attr,
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02001561 const char *buf, size_t count)
1562{
1563 bool val;
1564 int ret;
1565
1566 ret = strtobool(buf, &val);
1567 if (ret < 0)
1568 return ret;
1569
Rafael J. Wysocki5e33bc42013-08-28 21:41:01 +02001570 ret = lock_device_hotplug_sysfs();
1571 if (ret)
1572 return ret;
1573
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02001574 ret = val ? device_online(dev) : device_offline(dev);
1575 unlock_device_hotplug();
1576 return ret < 0 ? ret : count;
1577}
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07001578static DEVICE_ATTR_RW(online);
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02001579
Greg Kroah-Hartmanfa6fdb32013-08-08 15:22:55 -07001580int device_add_groups(struct device *dev, const struct attribute_group **groups)
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001581{
Greg Kroah-Hartman3e9b2ba2013-08-21 13:47:50 -07001582 return sysfs_create_groups(&dev->kobj, groups);
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001583}
Dmitry Torokhova7670d42017-07-19 17:24:31 -07001584EXPORT_SYMBOL_GPL(device_add_groups);
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001585
Greg Kroah-Hartmanfa6fdb32013-08-08 15:22:55 -07001586void device_remove_groups(struct device *dev,
1587 const struct attribute_group **groups)
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001588{
Greg Kroah-Hartman3e9b2ba2013-08-21 13:47:50 -07001589 sysfs_remove_groups(&dev->kobj, groups);
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -07001590}
Dmitry Torokhova7670d42017-07-19 17:24:31 -07001591EXPORT_SYMBOL_GPL(device_remove_groups);
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -07001592
Dmitry Torokhov57b8ff02017-07-19 17:24:33 -07001593union device_attr_group_devres {
1594 const struct attribute_group *group;
1595 const struct attribute_group **groups;
1596};
1597
1598static int devm_attr_group_match(struct device *dev, void *res, void *data)
1599{
1600 return ((union device_attr_group_devres *)res)->group == data;
1601}
1602
1603static void devm_attr_group_remove(struct device *dev, void *res)
1604{
1605 union device_attr_group_devres *devres = res;
1606 const struct attribute_group *group = devres->group;
1607
1608 dev_dbg(dev, "%s: removing group %p\n", __func__, group);
1609 sysfs_remove_group(&dev->kobj, group);
1610}
1611
1612static void devm_attr_groups_remove(struct device *dev, void *res)
1613{
1614 union device_attr_group_devres *devres = res;
1615 const struct attribute_group **groups = devres->groups;
1616
1617 dev_dbg(dev, "%s: removing groups %p\n", __func__, groups);
1618 sysfs_remove_groups(&dev->kobj, groups);
1619}
1620
1621/**
1622 * devm_device_add_group - given a device, create a managed attribute group
1623 * @dev: The device to create the group for
1624 * @grp: The attribute group to create
1625 *
1626 * This function creates a group for the first time. It will explicitly
1627 * warn and error if any of the attribute files being created already exist.
1628 *
1629 * Returns 0 on success or error code on failure.
1630 */
1631int devm_device_add_group(struct device *dev, const struct attribute_group *grp)
1632{
1633 union device_attr_group_devres *devres;
1634 int error;
1635
1636 devres = devres_alloc(devm_attr_group_remove,
1637 sizeof(*devres), GFP_KERNEL);
1638 if (!devres)
1639 return -ENOMEM;
1640
1641 error = sysfs_create_group(&dev->kobj, grp);
1642 if (error) {
1643 devres_free(devres);
1644 return error;
1645 }
1646
1647 devres->group = grp;
1648 devres_add(dev, devres);
1649 return 0;
1650}
1651EXPORT_SYMBOL_GPL(devm_device_add_group);
1652
1653/**
1654 * devm_device_remove_group: remove a managed group from a device
1655 * @dev: device to remove the group from
1656 * @grp: group to remove
1657 *
1658 * This function removes a group of attributes from a device. The attributes
1659 * previously have to have been created for this group, otherwise it will fail.
1660 */
1661void devm_device_remove_group(struct device *dev,
1662 const struct attribute_group *grp)
1663{
1664 WARN_ON(devres_release(dev, devm_attr_group_remove,
1665 devm_attr_group_match,
1666 /* cast away const */ (void *)grp));
1667}
1668EXPORT_SYMBOL_GPL(devm_device_remove_group);
1669
1670/**
1671 * devm_device_add_groups - create a bunch of managed attribute groups
1672 * @dev: The device to create the group for
1673 * @groups: The attribute groups to create, NULL terminated
1674 *
1675 * This function creates a bunch of managed attribute groups. If an error
1676 * occurs when creating a group, all previously created groups will be
1677 * removed, unwinding everything back to the original state when this
1678 * function was called. It will explicitly warn and error if any of the
1679 * attribute files being created already exist.
1680 *
1681 * Returns 0 on success or error code from sysfs_create_group on failure.
1682 */
1683int devm_device_add_groups(struct device *dev,
1684 const struct attribute_group **groups)
1685{
1686 union device_attr_group_devres *devres;
1687 int error;
1688
1689 devres = devres_alloc(devm_attr_groups_remove,
1690 sizeof(*devres), GFP_KERNEL);
1691 if (!devres)
1692 return -ENOMEM;
1693
1694 error = sysfs_create_groups(&dev->kobj, groups);
1695 if (error) {
1696 devres_free(devres);
1697 return error;
1698 }
1699
1700 devres->groups = groups;
1701 devres_add(dev, devres);
1702 return 0;
1703}
1704EXPORT_SYMBOL_GPL(devm_device_add_groups);
1705
1706/**
1707 * devm_device_remove_groups - remove a list of managed groups
1708 *
1709 * @dev: The device for the groups to be removed from
1710 * @groups: NULL terminated list of groups to be removed
1711 *
1712 * If groups is not NULL, remove the specified groups from the device.
1713 */
1714void devm_device_remove_groups(struct device *dev,
1715 const struct attribute_group **groups)
1716{
1717 WARN_ON(devres_release(dev, devm_attr_groups_remove,
1718 devm_attr_group_match,
1719 /* cast away const */ (void *)groups));
1720}
1721EXPORT_SYMBOL_GPL(devm_device_remove_groups);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07001723static int device_add_attrs(struct device *dev)
1724{
1725 struct class *class = dev->class;
Stephen Hemmingeraed65af2011-03-28 09:12:52 -07001726 const struct device_type *type = dev->type;
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001727 int error;
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07001728
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001729 if (class) {
Greg Kroah-Hartmand05a6f92013-07-14 16:05:58 -07001730 error = device_add_groups(dev, class->dev_groups);
Kay Sieversf9f852d2006-10-07 21:54:55 +02001731 if (error)
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001732 return error;
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07001733 }
Kay Sieversf9f852d2006-10-07 21:54:55 +02001734
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001735 if (type) {
1736 error = device_add_groups(dev, type->groups);
Kay Sieversf9f852d2006-10-07 21:54:55 +02001737 if (error)
Greg Kroah-Hartmana6b01de2013-10-05 18:19:30 -07001738 goto err_remove_class_groups;
Kay Sieversf9f852d2006-10-07 21:54:55 +02001739 }
1740
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001741 error = device_add_groups(dev, dev->groups);
1742 if (error)
1743 goto err_remove_type_groups;
1744
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02001745 if (device_supports_offline(dev) && !dev->offline_disabled) {
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07001746 error = device_create_file(dev, &dev_attr_online);
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02001747 if (error)
Rafael J. Wysockiecfbf6f2013-12-12 06:11:02 +01001748 goto err_remove_dev_groups;
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02001749 }
1750
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001751 return 0;
1752
Rafael J. Wysockiecfbf6f2013-12-12 06:11:02 +01001753 err_remove_dev_groups:
1754 device_remove_groups(dev, dev->groups);
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001755 err_remove_type_groups:
1756 if (type)
1757 device_remove_groups(dev, type->groups);
Greg Kroah-Hartmand05a6f92013-07-14 16:05:58 -07001758 err_remove_class_groups:
1759 if (class)
1760 device_remove_groups(dev, class->dev_groups);
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001761
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07001762 return error;
1763}
1764
1765static void device_remove_attrs(struct device *dev)
1766{
1767 struct class *class = dev->class;
Stephen Hemmingeraed65af2011-03-28 09:12:52 -07001768 const struct device_type *type = dev->type;
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07001769
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07001770 device_remove_file(dev, &dev_attr_online);
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001771 device_remove_groups(dev, dev->groups);
Kay Sieversf9f852d2006-10-07 21:54:55 +02001772
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001773 if (type)
1774 device_remove_groups(dev, type->groups);
1775
Greg Kroah-Hartmana6b01de2013-10-05 18:19:30 -07001776 if (class)
Greg Kroah-Hartmand05a6f92013-07-14 16:05:58 -07001777 device_remove_groups(dev, class->dev_groups);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07001778}
1779
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07001780static ssize_t dev_show(struct device *dev, struct device_attribute *attr,
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001781 char *buf)
1782{
1783 return print_dev_t(buf, dev->devt);
1784}
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07001785static DEVICE_ATTR_RO(dev);
Tejun Heoad6a1e12007-06-14 03:45:17 +09001786
Kay Sieversca22e562011-12-14 14:29:38 -08001787/* /sys/devices/ */
Greg Kroah-Hartman881c6cfd2007-11-01 09:29:06 -06001788struct kset *devices_kset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790/**
Grygorii Strashko52cdbdd2015-07-27 20:43:01 +03001791 * devices_kset_move_before - Move device in the devices_kset's list.
1792 * @deva: Device to move.
1793 * @devb: Device @deva should come before.
1794 */
1795static void devices_kset_move_before(struct device *deva, struct device *devb)
1796{
1797 if (!devices_kset)
1798 return;
1799 pr_debug("devices_kset: Moving %s before %s\n",
1800 dev_name(deva), dev_name(devb));
1801 spin_lock(&devices_kset->list_lock);
1802 list_move_tail(&deva->kobj.entry, &devb->kobj.entry);
1803 spin_unlock(&devices_kset->list_lock);
1804}
1805
1806/**
1807 * devices_kset_move_after - Move device in the devices_kset's list.
1808 * @deva: Device to move
1809 * @devb: Device @deva should come after.
1810 */
1811static void devices_kset_move_after(struct device *deva, struct device *devb)
1812{
1813 if (!devices_kset)
1814 return;
1815 pr_debug("devices_kset: Moving %s after %s\n",
1816 dev_name(deva), dev_name(devb));
1817 spin_lock(&devices_kset->list_lock);
1818 list_move(&deva->kobj.entry, &devb->kobj.entry);
1819 spin_unlock(&devices_kset->list_lock);
1820}
1821
1822/**
1823 * devices_kset_move_last - move the device to the end of devices_kset's list.
1824 * @dev: device to move
1825 */
1826void devices_kset_move_last(struct device *dev)
1827{
1828 if (!devices_kset)
1829 return;
1830 pr_debug("devices_kset: Moving %s to end of list\n", dev_name(dev));
1831 spin_lock(&devices_kset->list_lock);
1832 list_move_tail(&dev->kobj.entry, &devices_kset->list);
1833 spin_unlock(&devices_kset->list_lock);
1834}
1835
1836/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001837 * device_create_file - create sysfs attribute file for device.
1838 * @dev: device.
1839 * @attr: device attribute descriptor.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 */
Phil Carmody26579ab2009-12-18 15:34:19 +02001841int device_create_file(struct device *dev,
1842 const struct device_attribute *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843{
1844 int error = 0;
Felipe Balbi8f46baa2013-02-20 10:31:42 +02001845
1846 if (dev) {
1847 WARN(((attr->attr.mode & S_IWUGO) && !attr->store),
dyoung@redhat.com97521972013-05-16 14:31:30 +08001848 "Attribute %s: write permission without 'store'\n",
1849 attr->attr.name);
Felipe Balbi8f46baa2013-02-20 10:31:42 +02001850 WARN(((attr->attr.mode & S_IRUGO) && !attr->show),
dyoung@redhat.com97521972013-05-16 14:31:30 +08001851 "Attribute %s: read permission without 'show'\n",
1852 attr->attr.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 error = sysfs_create_file(&dev->kobj, &attr->attr);
Felipe Balbi8f46baa2013-02-20 10:31:42 +02001854 }
1855
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 return error;
1857}
David Graham White86df2682013-07-21 20:41:14 -04001858EXPORT_SYMBOL_GPL(device_create_file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859
1860/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001861 * device_remove_file - remove sysfs attribute file.
1862 * @dev: device.
1863 * @attr: device attribute descriptor.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 */
Phil Carmody26579ab2009-12-18 15:34:19 +02001865void device_remove_file(struct device *dev,
1866 const struct device_attribute *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867{
Cornelia Huck0c98b192008-01-31 10:39:38 +01001868 if (dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 sysfs_remove_file(&dev->kobj, &attr->attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870}
David Graham White86df2682013-07-21 20:41:14 -04001871EXPORT_SYMBOL_GPL(device_remove_file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872
Greg Kroah-Hartman2589f1882006-09-19 09:39:19 -07001873/**
Tejun Heo6b0afc22014-02-03 14:03:01 -05001874 * device_remove_file_self - remove sysfs attribute file from its own method.
1875 * @dev: device.
1876 * @attr: device attribute descriptor.
1877 *
1878 * See kernfs_remove_self() for details.
1879 */
1880bool device_remove_file_self(struct device *dev,
1881 const struct device_attribute *attr)
1882{
1883 if (dev)
1884 return sysfs_remove_file_self(&dev->kobj, &attr->attr);
1885 else
1886 return false;
1887}
1888EXPORT_SYMBOL_GPL(device_remove_file_self);
1889
1890/**
Greg Kroah-Hartman2589f1882006-09-19 09:39:19 -07001891 * device_create_bin_file - create sysfs binary attribute file for device.
1892 * @dev: device.
1893 * @attr: device binary attribute descriptor.
1894 */
Phil Carmody66ecb922009-12-18 15:34:20 +02001895int device_create_bin_file(struct device *dev,
1896 const struct bin_attribute *attr)
Greg Kroah-Hartman2589f1882006-09-19 09:39:19 -07001897{
1898 int error = -EINVAL;
1899 if (dev)
1900 error = sysfs_create_bin_file(&dev->kobj, attr);
1901 return error;
1902}
1903EXPORT_SYMBOL_GPL(device_create_bin_file);
1904
1905/**
1906 * device_remove_bin_file - remove sysfs binary attribute file
1907 * @dev: device.
1908 * @attr: device binary attribute descriptor.
1909 */
Phil Carmody66ecb922009-12-18 15:34:20 +02001910void device_remove_bin_file(struct device *dev,
1911 const struct bin_attribute *attr)
Greg Kroah-Hartman2589f1882006-09-19 09:39:19 -07001912{
1913 if (dev)
1914 sysfs_remove_bin_file(&dev->kobj, attr);
1915}
1916EXPORT_SYMBOL_GPL(device_remove_bin_file);
1917
James Bottomley34bb61f2005-09-06 16:56:51 -07001918static void klist_children_get(struct klist_node *n)
1919{
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08001920 struct device_private *p = to_device_private_parent(n);
1921 struct device *dev = p->device;
James Bottomley34bb61f2005-09-06 16:56:51 -07001922
1923 get_device(dev);
1924}
1925
1926static void klist_children_put(struct klist_node *n)
1927{
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08001928 struct device_private *p = to_device_private_parent(n);
1929 struct device *dev = p->device;
James Bottomley34bb61f2005-09-06 16:56:51 -07001930
1931 put_device(dev);
1932}
1933
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001935 * device_initialize - init device structure.
1936 * @dev: device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 *
Cornelia Huck57394112008-09-03 18:26:40 +02001938 * This prepares the device for use by other layers by initializing
1939 * its fields.
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001940 * It is the first half of device_register(), if called by
Cornelia Huck57394112008-09-03 18:26:40 +02001941 * that function, though it can also be called separately, so one
1942 * may use @dev's fields. In particular, get_device()/put_device()
1943 * may be used for reference counting of @dev after calling this
1944 * function.
1945 *
Alan Sternb10d5ef2012-01-17 11:39:00 -05001946 * All fields in @dev must be initialized by the caller to 0, except
1947 * for those explicitly set to some other value. The simplest
1948 * approach is to use kzalloc() to allocate the structure containing
1949 * @dev.
1950 *
Cornelia Huck57394112008-09-03 18:26:40 +02001951 * NOTE: Use put_device() to give up your reference instead of freeing
1952 * @dev directly once you have called this function.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954void device_initialize(struct device *dev)
1955{
Greg Kroah-Hartman881c6cfd2007-11-01 09:29:06 -06001956 dev->kobj.kset = devices_kset;
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -07001957 kobject_init(&dev->kobj, &device_ktype);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 INIT_LIST_HEAD(&dev->dma_pools);
Thomas Gleixner31427882010-01-29 20:39:02 +00001959 mutex_init(&dev->mutex);
Dan Williams87a30e12019-07-17 18:08:26 -07001960#ifdef CONFIG_PROVE_LOCKING
1961 mutex_init(&dev->lockdep_mutex);
1962#endif
Peter Zijlstra1704f472010-03-19 01:37:42 +01001963 lockdep_set_novalidate_class(&dev->mutex);
Tejun Heo9ac78492007-01-20 16:00:26 +09001964 spin_lock_init(&dev->devres_lock);
1965 INIT_LIST_HEAD(&dev->devres_head);
Alan Stern3b98aea2008-08-07 13:06:12 -04001966 device_pm_init(dev);
Christoph Hellwig87348132006-12-06 20:32:33 -08001967 set_dev_node(dev, -1);
Jiang Liu4a7cc832015-07-09 16:00:44 +08001968#ifdef CONFIG_GENERIC_MSI_IRQ
1969 INIT_LIST_HEAD(&dev->msi_list);
1970#endif
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001971 INIT_LIST_HEAD(&dev->links.consumers);
1972 INIT_LIST_HEAD(&dev->links.suppliers);
Saravana Kannane2ae9bc2019-09-04 14:11:21 -07001973 INIT_LIST_HEAD(&dev->links.needs_suppliers);
Saravana Kannanfc5a2512019-09-04 14:11:23 -07001974 INIT_LIST_HEAD(&dev->links.defer_sync);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001975 dev->links.status = DL_DEV_NO_DRIVER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976}
David Graham White86df2682013-07-21 20:41:14 -04001977EXPORT_SYMBOL_GPL(device_initialize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978
Tejun Heod73ce002013-03-12 11:30:05 -07001979struct kobject *virtual_device_parent(struct device *dev)
Greg Kroah-Hartmanf0ee61a2006-10-23 10:40:54 -07001980{
Kay Sievers86406242007-03-14 03:25:56 +01001981 static struct kobject *virtual_dir = NULL;
Greg Kroah-Hartmanf0ee61a2006-10-23 10:40:54 -07001982
Kay Sievers86406242007-03-14 03:25:56 +01001983 if (!virtual_dir)
Greg Kroah-Hartman4ff6abf2007-11-05 22:24:43 -08001984 virtual_dir = kobject_create_and_add("virtual",
Greg Kroah-Hartman881c6cfd2007-11-01 09:29:06 -06001985 &devices_kset->kobj);
Greg Kroah-Hartmanf0ee61a2006-10-23 10:40:54 -07001986
Kay Sievers86406242007-03-14 03:25:56 +01001987 return virtual_dir;
Greg Kroah-Hartmanf0ee61a2006-10-23 10:40:54 -07001988}
1989
Eric W. Biedermanbc451f22010-03-30 11:31:25 -07001990struct class_dir {
1991 struct kobject kobj;
1992 struct class *class;
1993};
1994
1995#define to_class_dir(obj) container_of(obj, struct class_dir, kobj)
1996
1997static void class_dir_release(struct kobject *kobj)
1998{
1999 struct class_dir *dir = to_class_dir(kobj);
2000 kfree(dir);
2001}
2002
2003static const
2004struct kobj_ns_type_operations *class_dir_child_ns_type(struct kobject *kobj)
2005{
2006 struct class_dir *dir = to_class_dir(kobj);
2007 return dir->class->ns_type;
2008}
2009
2010static struct kobj_type class_dir_ktype = {
2011 .release = class_dir_release,
2012 .sysfs_ops = &kobj_sysfs_ops,
2013 .child_ns_type = class_dir_child_ns_type
2014};
2015
2016static struct kobject *
2017class_dir_create_and_add(struct class *class, struct kobject *parent_kobj)
2018{
2019 struct class_dir *dir;
2020 int retval;
2021
2022 dir = kzalloc(sizeof(*dir), GFP_KERNEL);
2023 if (!dir)
Tetsuo Handa84d0c272018-05-07 19:10:31 +09002024 return ERR_PTR(-ENOMEM);
Eric W. Biedermanbc451f22010-03-30 11:31:25 -07002025
2026 dir->class = class;
2027 kobject_init(&dir->kobj, &class_dir_ktype);
2028
Kay Sievers6b6e39a2010-11-15 23:13:18 +01002029 dir->kobj.kset = &class->p->glue_dirs;
Eric W. Biedermanbc451f22010-03-30 11:31:25 -07002030
2031 retval = kobject_add(&dir->kobj, parent_kobj, "%s", class->name);
2032 if (retval < 0) {
2033 kobject_put(&dir->kobj);
Tetsuo Handa84d0c272018-05-07 19:10:31 +09002034 return ERR_PTR(retval);
Eric W. Biedermanbc451f22010-03-30 11:31:25 -07002035 }
2036 return &dir->kobj;
2037}
2038
Yijing Wange4a60d12014-11-07 12:05:49 +08002039static DEFINE_MUTEX(gdp_mutex);
Eric W. Biedermanbc451f22010-03-30 11:31:25 -07002040
Kay Sieversda231fd2007-11-21 17:29:15 +01002041static struct kobject *get_device_parent(struct device *dev,
2042 struct device *parent)
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +01002043{
Kay Sievers86406242007-03-14 03:25:56 +01002044 if (dev->class) {
2045 struct kobject *kobj = NULL;
2046 struct kobject *parent_kobj;
2047 struct kobject *k;
2048
Randy Dunlapead454f2010-09-24 14:36:49 -07002049#ifdef CONFIG_BLOCK
Kay Sievers39aba962010-09-04 22:33:14 -07002050 /* block disks show up in /sys/block */
Andi Kleene52eec12010-09-08 16:54:17 +02002051 if (sysfs_deprecated && dev->class == &block_class) {
Kay Sievers39aba962010-09-04 22:33:14 -07002052 if (parent && parent->class == &block_class)
2053 return &parent->kobj;
Kay Sievers6b6e39a2010-11-15 23:13:18 +01002054 return &block_class.p->subsys.kobj;
Kay Sievers39aba962010-09-04 22:33:14 -07002055 }
Randy Dunlapead454f2010-09-24 14:36:49 -07002056#endif
Andi Kleene52eec12010-09-08 16:54:17 +02002057
Kay Sievers86406242007-03-14 03:25:56 +01002058 /*
2059 * If we have no parent, we live in "virtual".
Kay Sievers0f4dafc2007-12-19 01:40:42 +01002060 * Class-devices with a non class-device as parent, live
2061 * in a "glue" directory to prevent namespace collisions.
Kay Sievers86406242007-03-14 03:25:56 +01002062 */
2063 if (parent == NULL)
2064 parent_kobj = virtual_device_parent(dev);
Eric W. Biederman24b14422010-07-24 22:43:35 -07002065 else if (parent->class && !dev->class->ns_type)
Kay Sievers86406242007-03-14 03:25:56 +01002066 return &parent->kobj;
2067 else
2068 parent_kobj = &parent->kobj;
2069
Tejun Heo77d3d7c2010-02-05 17:57:02 +09002070 mutex_lock(&gdp_mutex);
2071
Kay Sievers86406242007-03-14 03:25:56 +01002072 /* find our class-directory at the parent and reference it */
Kay Sievers6b6e39a2010-11-15 23:13:18 +01002073 spin_lock(&dev->class->p->glue_dirs.list_lock);
2074 list_for_each_entry(k, &dev->class->p->glue_dirs.list, entry)
Kay Sievers86406242007-03-14 03:25:56 +01002075 if (k->parent == parent_kobj) {
2076 kobj = kobject_get(k);
2077 break;
2078 }
Kay Sievers6b6e39a2010-11-15 23:13:18 +01002079 spin_unlock(&dev->class->p->glue_dirs.list_lock);
Tejun Heo77d3d7c2010-02-05 17:57:02 +09002080 if (kobj) {
2081 mutex_unlock(&gdp_mutex);
Kay Sievers86406242007-03-14 03:25:56 +01002082 return kobj;
Tejun Heo77d3d7c2010-02-05 17:57:02 +09002083 }
Kay Sievers86406242007-03-14 03:25:56 +01002084
2085 /* or create a new class-directory at the parent device */
Eric W. Biedermanbc451f22010-03-30 11:31:25 -07002086 k = class_dir_create_and_add(dev->class, parent_kobj);
Kay Sievers0f4dafc2007-12-19 01:40:42 +01002087 /* do not emit an uevent for this simple "glue" directory */
Tejun Heo77d3d7c2010-02-05 17:57:02 +09002088 mutex_unlock(&gdp_mutex);
Greg Kroah-Hartman43968d22007-11-05 22:24:43 -08002089 return k;
Kay Sievers86406242007-03-14 03:25:56 +01002090 }
2091
Kay Sieversca22e562011-12-14 14:29:38 -08002092 /* subsystems can specify a default root directory for their devices */
2093 if (!parent && dev->bus && dev->bus->dev_root)
2094 return &dev->bus->dev_root->kobj;
2095
Kay Sievers86406242007-03-14 03:25:56 +01002096 if (parent)
Cornelia Huckc744aeae2007-01-08 20:16:44 +01002097 return &parent->kobj;
2098 return NULL;
2099}
Kay Sieversda231fd2007-11-21 17:29:15 +01002100
Ming Leicebf8fd2016-07-10 19:27:36 +08002101static inline bool live_in_glue_dir(struct kobject *kobj,
2102 struct device *dev)
2103{
2104 if (!kobj || !dev->class ||
2105 kobj->kset != &dev->class->p->glue_dirs)
2106 return false;
2107 return true;
2108}
2109
2110static inline struct kobject *get_glue_dir(struct device *dev)
2111{
2112 return dev->kobj.parent;
2113}
2114
2115/*
2116 * make sure cleaning up dir as the last step, we need to make
2117 * sure .release handler of kobject is run with holding the
2118 * global lock
2119 */
Cornelia Huck63b69712008-01-21 16:09:44 +01002120static void cleanup_glue_dir(struct device *dev, struct kobject *glue_dir)
Kay Sieversda231fd2007-11-21 17:29:15 +01002121{
Muchun Songac434322019-07-27 11:21:22 +08002122 unsigned int ref;
2123
Kay Sievers0f4dafc2007-12-19 01:40:42 +01002124 /* see if we live in a "glue" directory */
Ming Leicebf8fd2016-07-10 19:27:36 +08002125 if (!live_in_glue_dir(glue_dir, dev))
Kay Sieversda231fd2007-11-21 17:29:15 +01002126 return;
2127
Yijing Wange4a60d12014-11-07 12:05:49 +08002128 mutex_lock(&gdp_mutex);
Muchun Songac434322019-07-27 11:21:22 +08002129 /**
2130 * There is a race condition between removing glue directory
2131 * and adding a new device under the glue directory.
2132 *
2133 * CPU1: CPU2:
2134 *
2135 * device_add()
2136 * get_device_parent()
2137 * class_dir_create_and_add()
2138 * kobject_add_internal()
2139 * create_dir() // create glue_dir
2140 *
2141 * device_add()
2142 * get_device_parent()
2143 * kobject_get() // get glue_dir
2144 *
2145 * device_del()
2146 * cleanup_glue_dir()
2147 * kobject_del(glue_dir)
2148 *
2149 * kobject_add()
2150 * kobject_add_internal()
2151 * create_dir() // in glue_dir
2152 * sysfs_create_dir_ns()
2153 * kernfs_create_dir_ns(sd)
2154 *
2155 * sysfs_remove_dir() // glue_dir->sd=NULL
2156 * sysfs_put() // free glue_dir->sd
2157 *
2158 * // sd is freed
2159 * kernfs_new_node(sd)
2160 * kernfs_get(glue_dir)
2161 * kernfs_add_one()
2162 * kernfs_put()
2163 *
2164 * Before CPU1 remove last child device under glue dir, if CPU2 add
2165 * a new device under glue dir, the glue_dir kobject reference count
2166 * will be increase to 2 in kobject_get(k). And CPU2 has been called
2167 * kernfs_create_dir_ns(). Meanwhile, CPU1 call sysfs_remove_dir()
2168 * and sysfs_put(). This result in glue_dir->sd is freed.
2169 *
2170 * Then the CPU2 will see a stale "empty" but still potentially used
2171 * glue dir around in kernfs_new_node().
2172 *
2173 * In order to avoid this happening, we also should make sure that
2174 * kernfs_node for glue_dir is released in CPU1 only when refcount
2175 * for glue_dir kobj is 1.
2176 */
2177 ref = kref_read(&glue_dir->kref);
2178 if (!kobject_has_children(glue_dir) && !--ref)
Benjamin Herrenschmidt726e4102018-07-10 10:29:10 +10002179 kobject_del(glue_dir);
Kay Sievers0f4dafc2007-12-19 01:40:42 +01002180 kobject_put(glue_dir);
Yijing Wange4a60d12014-11-07 12:05:49 +08002181 mutex_unlock(&gdp_mutex);
Kay Sieversda231fd2007-11-21 17:29:15 +01002182}
Cornelia Huck63b69712008-01-21 16:09:44 +01002183
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07002184static int device_add_class_symlinks(struct device *dev)
2185{
Benjamin Herrenschmidt5590f312015-02-18 11:25:18 +11002186 struct device_node *of_node = dev_of_node(dev);
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07002187 int error;
2188
Benjamin Herrenschmidt5590f312015-02-18 11:25:18 +11002189 if (of_node) {
Rob Herring0c3c2342017-10-04 14:04:01 -05002190 error = sysfs_create_link(&dev->kobj, of_node_kobj(of_node), "of_node");
Benjamin Herrenschmidt5590f312015-02-18 11:25:18 +11002191 if (error)
2192 dev_warn(dev, "Error %d creating of_node link\n",error);
2193 /* An error here doesn't warrant bringing down the device */
2194 }
2195
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07002196 if (!dev->class)
2197 return 0;
Kay Sieversda231fd2007-11-21 17:29:15 +01002198
Greg Kroah-Hartman1fbfee62008-05-28 09:28:39 -07002199 error = sysfs_create_link(&dev->kobj,
Kay Sievers6b6e39a2010-11-15 23:13:18 +01002200 &dev->class->p->subsys.kobj,
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07002201 "subsystem");
2202 if (error)
Benjamin Herrenschmidt5590f312015-02-18 11:25:18 +11002203 goto out_devnode;
Kay Sieversda231fd2007-11-21 17:29:15 +01002204
Greg Kroah-Hartman4e886c22008-01-27 12:12:43 -08002205 if (dev->parent && device_is_not_partition(dev)) {
Dmitry Torokhov4f01a752007-09-18 22:46:50 -07002206 error = sysfs_create_link(&dev->kobj, &dev->parent->kobj,
2207 "device");
2208 if (error)
Kay Sievers39aba962010-09-04 22:33:14 -07002209 goto out_subsys;
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07002210 }
Kay Sievers39aba962010-09-04 22:33:14 -07002211
Randy Dunlapead454f2010-09-24 14:36:49 -07002212#ifdef CONFIG_BLOCK
Kay Sievers39aba962010-09-04 22:33:14 -07002213 /* /sys/block has directories and does not need symlinks */
Andi Kleene52eec12010-09-08 16:54:17 +02002214 if (sysfs_deprecated && dev->class == &block_class)
Kay Sievers39aba962010-09-04 22:33:14 -07002215 return 0;
Randy Dunlapead454f2010-09-24 14:36:49 -07002216#endif
Kay Sievers39aba962010-09-04 22:33:14 -07002217
2218 /* link in the class directory pointing to the device */
Kay Sievers6b6e39a2010-11-15 23:13:18 +01002219 error = sysfs_create_link(&dev->class->p->subsys.kobj,
Kay Sievers39aba962010-09-04 22:33:14 -07002220 &dev->kobj, dev_name(dev));
2221 if (error)
2222 goto out_device;
2223
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07002224 return 0;
2225
Kay Sievers39aba962010-09-04 22:33:14 -07002226out_device:
2227 sysfs_remove_link(&dev->kobj, "device");
Kay Sieversda231fd2007-11-21 17:29:15 +01002228
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07002229out_subsys:
2230 sysfs_remove_link(&dev->kobj, "subsystem");
Benjamin Herrenschmidt5590f312015-02-18 11:25:18 +11002231out_devnode:
2232 sysfs_remove_link(&dev->kobj, "of_node");
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07002233 return error;
2234}
2235
2236static void device_remove_class_symlinks(struct device *dev)
2237{
Benjamin Herrenschmidt5590f312015-02-18 11:25:18 +11002238 if (dev_of_node(dev))
2239 sysfs_remove_link(&dev->kobj, "of_node");
2240
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07002241 if (!dev->class)
2242 return;
Kay Sieversda231fd2007-11-21 17:29:15 +01002243
Greg Kroah-Hartman4e886c22008-01-27 12:12:43 -08002244 if (dev->parent && device_is_not_partition(dev))
Kay Sieversda231fd2007-11-21 17:29:15 +01002245 sysfs_remove_link(&dev->kobj, "device");
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07002246 sysfs_remove_link(&dev->kobj, "subsystem");
Randy Dunlapead454f2010-09-24 14:36:49 -07002247#ifdef CONFIG_BLOCK
Andi Kleene52eec12010-09-08 16:54:17 +02002248 if (sysfs_deprecated && dev->class == &block_class)
Kay Sievers39aba962010-09-04 22:33:14 -07002249 return;
Randy Dunlapead454f2010-09-24 14:36:49 -07002250#endif
Kay Sievers6b6e39a2010-11-15 23:13:18 +01002251 sysfs_delete_link(&dev->class->p->subsys.kobj, &dev->kobj, dev_name(dev));
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07002252}
2253
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254/**
Stephen Rothwell413c2392008-05-30 10:16:40 +10002255 * dev_set_name - set a device name
2256 * @dev: device
Randy Dunlap46232362008-06-04 21:40:43 -07002257 * @fmt: format string for the device's name
Stephen Rothwell413c2392008-05-30 10:16:40 +10002258 */
2259int dev_set_name(struct device *dev, const char *fmt, ...)
2260{
2261 va_list vargs;
Kay Sievers1fa5ae82009-01-25 15:17:37 +01002262 int err;
Stephen Rothwell413c2392008-05-30 10:16:40 +10002263
2264 va_start(vargs, fmt);
Kay Sievers1fa5ae82009-01-25 15:17:37 +01002265 err = kobject_set_name_vargs(&dev->kobj, fmt, vargs);
Stephen Rothwell413c2392008-05-30 10:16:40 +10002266 va_end(vargs);
Kay Sievers1fa5ae82009-01-25 15:17:37 +01002267 return err;
Stephen Rothwell413c2392008-05-30 10:16:40 +10002268}
2269EXPORT_SYMBOL_GPL(dev_set_name);
2270
2271/**
Dan Williamse105b8b2008-04-21 10:51:07 -07002272 * device_to_dev_kobj - select a /sys/dev/ directory for the device
2273 * @dev: device
2274 *
2275 * By default we select char/ for new entries. Setting class->dev_obj
2276 * to NULL prevents an entry from being created. class->dev_kobj must
2277 * be set (or cleared) before any devices are registered to the class
2278 * otherwise device_create_sys_dev_entry() and
Peter Korsgaard0d4e293c2012-04-17 12:12:57 +02002279 * device_remove_sys_dev_entry() will disagree about the presence of
2280 * the link.
Dan Williamse105b8b2008-04-21 10:51:07 -07002281 */
2282static struct kobject *device_to_dev_kobj(struct device *dev)
2283{
2284 struct kobject *kobj;
2285
2286 if (dev->class)
2287 kobj = dev->class->dev_kobj;
2288 else
2289 kobj = sysfs_dev_char_kobj;
2290
2291 return kobj;
2292}
2293
2294static int device_create_sys_dev_entry(struct device *dev)
2295{
2296 struct kobject *kobj = device_to_dev_kobj(dev);
2297 int error = 0;
2298 char devt_str[15];
2299
2300 if (kobj) {
2301 format_dev_t(devt_str, dev->devt);
2302 error = sysfs_create_link(kobj, &dev->kobj, devt_str);
2303 }
2304
2305 return error;
2306}
2307
2308static void device_remove_sys_dev_entry(struct device *dev)
2309{
2310 struct kobject *kobj = device_to_dev_kobj(dev);
2311 char devt_str[15];
2312
2313 if (kobj) {
2314 format_dev_t(devt_str, dev->devt);
2315 sysfs_remove_link(kobj, devt_str);
2316 }
2317}
2318
Shaokun Zhang46d3a032018-07-15 18:08:56 +08002319static int device_private_init(struct device *dev)
Greg Kroah-Hartmanb4028432009-05-11 14:16:57 -07002320{
2321 dev->p = kzalloc(sizeof(*dev->p), GFP_KERNEL);
2322 if (!dev->p)
2323 return -ENOMEM;
2324 dev->p->device = dev;
2325 klist_init(&dev->p->klist_children, klist_children_get,
2326 klist_children_put);
Greg Kroah-Hartmanef8a3fd2012-03-08 12:17:22 -08002327 INIT_LIST_HEAD(&dev->p->deferred_probe);
Greg Kroah-Hartmanb4028432009-05-11 14:16:57 -07002328 return 0;
2329}
2330
Dan Williamse105b8b2008-04-21 10:51:07 -07002331/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002332 * device_add - add device to device hierarchy.
2333 * @dev: device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002335 * This is part 2 of device_register(), though may be called
2336 * separately _iff_ device_initialize() has been called separately.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337 *
Cornelia Huck57394112008-09-03 18:26:40 +02002338 * This adds @dev to the kobject hierarchy via kobject_add(), adds it
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002339 * to the global and sibling lists for the device, then
2340 * adds it to the other relevant subsystems of the driver model.
Cornelia Huck57394112008-09-03 18:26:40 +02002341 *
Alan Sternb10d5ef2012-01-17 11:39:00 -05002342 * Do not call this routine or device_register() more than once for
2343 * any device structure. The driver model core is not designed to work
2344 * with devices that get unregistered and then spring back to life.
2345 * (Among other things, it's very hard to guarantee that all references
2346 * to the previous incarnation of @dev have been dropped.) Allocate
2347 * and register a fresh new struct device instead.
2348 *
Cornelia Huck57394112008-09-03 18:26:40 +02002349 * NOTE: _Never_ directly free @dev after calling this function, even
2350 * if it returned an error! Always use put_device() to give up your
2351 * reference instead.
Borislav Petkovaffada72019-04-18 19:41:56 +02002352 *
2353 * Rule of thumb is: if device_add() succeeds, you should call
2354 * device_del() when you want to get rid of it. If device_add() has
2355 * *not* succeeded, use *only* put_device() to drop the reference
2356 * count.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357 */
2358int device_add(struct device *dev)
2359{
Viresh Kumar35dbf4e2017-03-17 12:24:22 +05302360 struct device *parent;
Kay Sieversca22e562011-12-14 14:29:38 -08002361 struct kobject *kobj;
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02002362 struct class_interface *class_intf;
Saravana Kannan03324502019-10-28 15:00:24 -07002363 int error = -EINVAL, fw_ret;
Ming Leicebf8fd2016-07-10 19:27:36 +08002364 struct kobject *glue_dir = NULL;
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01002365
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366 dev = get_device(dev);
Greg Kroah-Hartmanc906a482008-05-30 10:45:12 -07002367 if (!dev)
2368 goto done;
2369
Greg Kroah-Hartmanfb069a52008-12-16 12:23:36 -08002370 if (!dev->p) {
Greg Kroah-Hartmanb4028432009-05-11 14:16:57 -07002371 error = device_private_init(dev);
2372 if (error)
2373 goto done;
Greg Kroah-Hartmanfb069a52008-12-16 12:23:36 -08002374 }
Greg Kroah-Hartmanfb069a52008-12-16 12:23:36 -08002375
Kay Sievers1fa5ae82009-01-25 15:17:37 +01002376 /*
2377 * for statically allocated devices, which should all be converted
2378 * some day, we need to initialize the name. We prevent reading back
2379 * the name, and force the use of dev_name()
2380 */
2381 if (dev->init_name) {
Greg Kroah-Hartmanacc0e902009-06-02 15:39:55 -07002382 dev_set_name(dev, "%s", dev->init_name);
Kay Sievers1fa5ae82009-01-25 15:17:37 +01002383 dev->init_name = NULL;
2384 }
Greg Kroah-Hartmanc906a482008-05-30 10:45:12 -07002385
Kay Sieversca22e562011-12-14 14:29:38 -08002386 /* subsystems can specify simple device enumeration */
2387 if (!dev_name(dev) && dev->bus && dev->bus->dev_name)
2388 dev_set_name(dev, "%s%u", dev->bus->dev_name, dev->id);
2389
Thomas Gleixnere6309e72009-12-10 19:32:49 +00002390 if (!dev_name(dev)) {
2391 error = -EINVAL;
Kay Sievers5c8563d2009-05-28 14:24:07 -07002392 goto name_error;
Thomas Gleixnere6309e72009-12-10 19:32:49 +00002393 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01002395 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
Greg Kroah-Hartmanc205ef42006-08-07 22:19:37 -07002396
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397 parent = get_device(dev->parent);
Kay Sieversca22e562011-12-14 14:29:38 -08002398 kobj = get_device_parent(dev, parent);
Tetsuo Handa84d0c272018-05-07 19:10:31 +09002399 if (IS_ERR(kobj)) {
2400 error = PTR_ERR(kobj);
2401 goto parent_error;
2402 }
Kay Sieversca22e562011-12-14 14:29:38 -08002403 if (kobj)
2404 dev->kobj.parent = kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405
Yinghai Lu0d358f22008-02-19 03:20:41 -08002406 /* use parent numa_node */
Zhen Lei56f2de82015-08-25 12:08:22 +08002407 if (parent && (dev_to_node(dev) == NUMA_NO_NODE))
Yinghai Lu0d358f22008-02-19 03:20:41 -08002408 set_dev_node(dev, dev_to_node(parent));
2409
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410 /* first, register with generic layer. */
Kay Sievers8a577ff2009-04-18 15:05:45 -07002411 /* we require the name to be set before, and pass NULL */
2412 error = kobject_add(&dev->kobj, dev->kobj.parent, NULL);
Ming Leicebf8fd2016-07-10 19:27:36 +08002413 if (error) {
2414 glue_dir = get_glue_dir(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415 goto Error;
Ming Leicebf8fd2016-07-10 19:27:36 +08002416 }
Kay Sieversa7fd6702005-10-01 14:49:43 +02002417
Brian Walsh37022642006-08-14 22:43:19 -07002418 /* notify platform of device entry */
Heikki Krogerus07de0e82018-11-09 17:21:34 +03002419 error = device_platform_notify(dev, KOBJ_ADD);
2420 if (error)
2421 goto platform_error;
Brian Walsh37022642006-08-14 22:43:19 -07002422
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07002423 error = device_create_file(dev, &dev_attr_uevent);
Cornelia Hucka306eea2006-09-22 11:37:13 +02002424 if (error)
2425 goto attrError;
Kay Sieversa7fd6702005-10-01 14:49:43 +02002426
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07002427 error = device_add_class_symlinks(dev);
2428 if (error)
2429 goto SymlinkError;
Cornelia Huckdc0afa82007-07-09 11:39:18 -07002430 error = device_add_attrs(dev);
2431 if (error)
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07002432 goto AttrsError;
Cornelia Huckdc0afa82007-07-09 11:39:18 -07002433 error = bus_add_device(dev);
2434 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435 goto BusError;
Alan Stern3b98aea2008-08-07 13:06:12 -04002436 error = dpm_sysfs_add(dev);
Rafael J. Wysocki57eee3d2008-03-12 00:59:38 +01002437 if (error)
Alan Stern3b98aea2008-08-07 13:06:12 -04002438 goto DPMError;
2439 device_pm_add(dev);
Alan Sternec0676ee2008-12-05 14:10:31 -05002440
Sergey Klyaus0cd75042014-10-08 11:31:54 +04002441 if (MAJOR(dev->devt)) {
2442 error = device_create_file(dev, &dev_attr_dev);
2443 if (error)
2444 goto DevAttrError;
2445
2446 error = device_create_sys_dev_entry(dev);
2447 if (error)
2448 goto SysEntryError;
2449
2450 devtmpfs_create_node(dev);
2451 }
2452
Alan Sternec0676ee2008-12-05 14:10:31 -05002453 /* Notify clients of device addition. This call must come
majianpeng268863f2012-01-11 15:12:06 +00002454 * after dpm_sysfs_add() and before kobject_uevent().
Alan Sternec0676ee2008-12-05 14:10:31 -05002455 */
2456 if (dev->bus)
2457 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
2458 BUS_NOTIFY_ADD_DEVICE, dev);
2459
Cornelia Huck83b5fb4c2007-03-29 11:12:11 +02002460 kobject_uevent(&dev->kobj, KOBJ_ADD);
Saravana Kannan372a67c2019-09-04 14:11:20 -07002461
2462 if (dev->fwnode && !dev->fwnode->dev)
2463 dev->fwnode->dev = dev;
2464
Saravana Kannane2ae9bc2019-09-04 14:11:21 -07002465 /*
2466 * Check if any of the other devices (consumers) have been waiting for
2467 * this device (supplier) to be added so that they can create a device
2468 * link to it.
2469 *
2470 * This needs to happen after device_pm_add() because device_link_add()
2471 * requires the supplier be registered before it's called.
2472 *
2473 * But this also needs to happe before bus_probe_device() to make sure
2474 * waiting consumers can link to it before the driver is bound to the
2475 * device and the driver sync_state callback is called for this device.
2476 */
2477 device_link_add_missing_supplier_links();
2478
Saravana Kannan03324502019-10-28 15:00:24 -07002479 if (fwnode_has_op(dev->fwnode, add_links)) {
2480 fw_ret = fwnode_call_int_op(dev->fwnode, add_links, dev);
2481 if (fw_ret == -ENODEV)
2482 device_link_wait_for_mandatory_supplier(dev);
2483 else if (fw_ret)
2484 device_link_wait_for_optional_supplier(dev);
2485 }
Saravana Kannane2ae9bc2019-09-04 14:11:21 -07002486
Alan Stern2023c612009-07-30 15:27:18 -04002487 bus_probe_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488 if (parent)
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08002489 klist_add_tail(&dev->p->knode_parent,
2490 &parent->p->klist_children);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491
Greg Kroah-Hartman5d9fd162006-06-22 17:17:32 -07002492 if (dev->class) {
Kay Sieversca22e562011-12-14 14:29:38 -08002493 mutex_lock(&dev->class->p->mutex);
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02002494 /* tie the class to the device */
Wei Yang570d0202019-01-18 10:34:59 +08002495 klist_add_tail(&dev->p->knode_class,
Kay Sievers6b6e39a2010-11-15 23:13:18 +01002496 &dev->class->p->klist_devices);
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02002497
2498 /* notify any interfaces that the device is here */
Greg Kroah-Hartman184f1f72008-05-28 09:28:39 -07002499 list_for_each_entry(class_intf,
Kay Sieversca22e562011-12-14 14:29:38 -08002500 &dev->class->p->interfaces, node)
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02002501 if (class_intf->add_dev)
2502 class_intf->add_dev(dev, class_intf);
Kay Sieversca22e562011-12-14 14:29:38 -08002503 mutex_unlock(&dev->class->p->mutex);
Greg Kroah-Hartman5d9fd162006-06-22 17:17:32 -07002504 }
Greg Kroah-Hartmanc906a482008-05-30 10:45:12 -07002505done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506 put_device(dev);
2507 return error;
Sergey Klyaus0cd75042014-10-08 11:31:54 +04002508 SysEntryError:
2509 if (MAJOR(dev->devt))
2510 device_remove_file(dev, &dev_attr_dev);
2511 DevAttrError:
2512 device_pm_remove(dev);
2513 dpm_sysfs_remove(dev);
Alan Stern3b98aea2008-08-07 13:06:12 -04002514 DPMError:
Rafael J. Wysocki57eee3d2008-03-12 00:59:38 +01002515 bus_remove_device(dev);
2516 BusError:
James Simmons82f0cf92007-02-21 17:44:51 +00002517 device_remove_attrs(dev);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07002518 AttrsError:
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07002519 device_remove_class_symlinks(dev);
2520 SymlinkError:
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07002521 device_remove_file(dev, &dev_attr_uevent);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07002522 attrError:
Heikki Krogerus07de0e82018-11-09 17:21:34 +03002523 device_platform_notify(dev, KOBJ_REMOVE);
2524platform_error:
Kay Sievers312c0042005-11-16 09:00:00 +01002525 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
Ming Leicebf8fd2016-07-10 19:27:36 +08002526 glue_dir = get_glue_dir(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527 kobject_del(&dev->kobj);
2528 Error:
Ming Leicebf8fd2016-07-10 19:27:36 +08002529 cleanup_glue_dir(dev, glue_dir);
Tetsuo Handa84d0c272018-05-07 19:10:31 +09002530parent_error:
Markus Elfring5f0163a2015-02-05 11:48:26 +01002531 put_device(parent);
Kay Sievers5c8563d2009-05-28 14:24:07 -07002532name_error:
2533 kfree(dev->p);
2534 dev->p = NULL;
Greg Kroah-Hartmanc906a482008-05-30 10:45:12 -07002535 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536}
David Graham White86df2682013-07-21 20:41:14 -04002537EXPORT_SYMBOL_GPL(device_add);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002540 * device_register - register a device with the system.
2541 * @dev: pointer to the device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002543 * This happens in two clean steps - initialize the device
2544 * and add it to the system. The two steps can be called
2545 * separately, but this is the easiest and most common.
2546 * I.e. you should only call the two helpers separately if
2547 * have a clearly defined need to use and refcount the device
2548 * before it is added to the hierarchy.
Cornelia Huck57394112008-09-03 18:26:40 +02002549 *
Alan Sternb10d5ef2012-01-17 11:39:00 -05002550 * For more information, see the kerneldoc for device_initialize()
2551 * and device_add().
2552 *
Cornelia Huck57394112008-09-03 18:26:40 +02002553 * NOTE: _Never_ directly free @dev after calling this function, even
2554 * if it returned an error! Always use put_device() to give up the
2555 * reference initialized in this function instead.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557int device_register(struct device *dev)
2558{
2559 device_initialize(dev);
2560 return device_add(dev);
2561}
David Graham White86df2682013-07-21 20:41:14 -04002562EXPORT_SYMBOL_GPL(device_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002565 * get_device - increment reference count for device.
2566 * @dev: device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002568 * This simply forwards the call to kobject_get(), though
2569 * we do take care to provide for the case that we get a NULL
2570 * pointer passed in.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002572struct device *get_device(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +02002574 return dev ? kobj_to_dev(kobject_get(&dev->kobj)) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575}
David Graham White86df2682013-07-21 20:41:14 -04002576EXPORT_SYMBOL_GPL(get_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002579 * put_device - decrement reference count.
2580 * @dev: device in question.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002582void put_device(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583{
Kay Sieversedfaa7c2007-05-21 22:08:01 +02002584 /* might_sleep(); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585 if (dev)
2586 kobject_put(&dev->kobj);
2587}
David Graham White86df2682013-07-21 20:41:14 -04002588EXPORT_SYMBOL_GPL(put_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589
Dan Williams00289cd2019-07-17 18:07:53 -07002590bool kill_device(struct device *dev)
2591{
2592 /*
2593 * Require the device lock and set the "dead" flag to guarantee that
2594 * the update behavior is consistent with the other bitfields near
2595 * it and that we cannot have an asynchronous probe routine trying
2596 * to run while we are tearing out the bus/class/sysfs from
2597 * underneath the device.
2598 */
2599 lockdep_assert_held(&dev->mutex);
2600
2601 if (dev->p->dead)
2602 return false;
2603 dev->p->dead = true;
2604 return true;
2605}
2606EXPORT_SYMBOL_GPL(kill_device);
2607
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002609 * device_del - delete device from system.
2610 * @dev: device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002612 * This is the first part of the device unregistration
2613 * sequence. This removes the device from the lists we control
2614 * from here, has it removed from the other driver model
2615 * subsystems it was added to in device_add(), and removes it
2616 * from the kobject hierarchy.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002618 * NOTE: this should be called manually _iff_ device_add() was
2619 * also called manually.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002621void device_del(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002623 struct device *parent = dev->parent;
Ming Leicebf8fd2016-07-10 19:27:36 +08002624 struct kobject *glue_dir = NULL;
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02002625 struct class_interface *class_intf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626
Alexander Duyck3451a492019-01-22 10:39:10 -08002627 device_lock(dev);
Dan Williams00289cd2019-07-17 18:07:53 -07002628 kill_device(dev);
Alexander Duyck3451a492019-01-22 10:39:10 -08002629 device_unlock(dev);
2630
Saravana Kannan372a67c2019-09-04 14:11:20 -07002631 if (dev->fwnode && dev->fwnode->dev == dev)
2632 dev->fwnode->dev = NULL;
2633
Alan Sternec0676ee2008-12-05 14:10:31 -05002634 /* Notify clients of device removal. This call must come
2635 * before dpm_sysfs_remove().
2636 */
2637 if (dev->bus)
2638 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
2639 BUS_NOTIFY_DEL_DEVICE, dev);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01002640
Alan Stern3b98aea2008-08-07 13:06:12 -04002641 dpm_sysfs_remove(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642 if (parent)
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08002643 klist_del(&dev->p->knode_parent);
Dan Williamse105b8b2008-04-21 10:51:07 -07002644 if (MAJOR(dev->devt)) {
Kay Sievers2b2af542009-04-30 15:23:42 +02002645 devtmpfs_delete_node(dev);
Dan Williamse105b8b2008-04-21 10:51:07 -07002646 device_remove_sys_dev_entry(dev);
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07002647 device_remove_file(dev, &dev_attr_dev);
Dan Williamse105b8b2008-04-21 10:51:07 -07002648 }
Kay Sieversb9d9c822006-06-15 15:31:56 +02002649 if (dev->class) {
Kay Sieversda231fd2007-11-21 17:29:15 +01002650 device_remove_class_symlinks(dev);
Kay Sievers99ef3ef2006-09-14 11:23:28 +02002651
Kay Sieversca22e562011-12-14 14:29:38 -08002652 mutex_lock(&dev->class->p->mutex);
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02002653 /* notify any interfaces that the device is now gone */
Greg Kroah-Hartman184f1f72008-05-28 09:28:39 -07002654 list_for_each_entry(class_intf,
Kay Sieversca22e562011-12-14 14:29:38 -08002655 &dev->class->p->interfaces, node)
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02002656 if (class_intf->remove_dev)
2657 class_intf->remove_dev(dev, class_intf);
2658 /* remove the device from the class list */
Wei Yang570d0202019-01-18 10:34:59 +08002659 klist_del(&dev->p->knode_class);
Kay Sieversca22e562011-12-14 14:29:38 -08002660 mutex_unlock(&dev->class->p->mutex);
Kay Sieversb9d9c822006-06-15 15:31:56 +02002661 }
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07002662 device_remove_file(dev, &dev_attr_uevent);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07002663 device_remove_attrs(dev);
Benjamin Herrenschmidt28953532006-11-08 19:46:14 -08002664 bus_remove_device(dev);
LongX Zhang4b6d1f122012-10-25 00:21:28 +02002665 device_pm_remove(dev);
Grant Likelyd1c34142012-03-05 08:47:41 -07002666 driver_deferred_probe_del(dev);
Heikki Krogerus07de0e82018-11-09 17:21:34 +03002667 device_platform_notify(dev, KOBJ_REMOVE);
Lukas Wunner478573c2016-07-28 02:25:41 +02002668 device_remove_properties(dev);
Jeffy Chen2ec16152017-10-20 20:01:01 +08002669 device_links_purge(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670
Joerg Roedel599bad32014-09-30 13:02:02 +02002671 if (dev->bus)
2672 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
2673 BUS_NOTIFY_REMOVED_DEVICE, dev);
Kay Sievers312c0042005-11-16 09:00:00 +01002674 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
Ming Leicebf8fd2016-07-10 19:27:36 +08002675 glue_dir = get_glue_dir(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676 kobject_del(&dev->kobj);
Ming Leicebf8fd2016-07-10 19:27:36 +08002677 cleanup_glue_dir(dev, glue_dir);
Kay Sieversda231fd2007-11-21 17:29:15 +01002678 put_device(parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679}
David Graham White86df2682013-07-21 20:41:14 -04002680EXPORT_SYMBOL_GPL(device_del);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681
2682/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002683 * device_unregister - unregister device from system.
2684 * @dev: device going away.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002686 * We do this in two parts, like we do device_register(). First,
2687 * we remove it from all the subsystems with device_del(), then
2688 * we decrement the reference count via put_device(). If that
2689 * is the final reference count, the device will be cleaned up
2690 * via device_release() above. Otherwise, the structure will
2691 * stick around until the final reference to the device is dropped.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002693void device_unregister(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694{
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01002695 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696 device_del(dev);
2697 put_device(dev);
2698}
David Graham White86df2682013-07-21 20:41:14 -04002699EXPORT_SYMBOL_GPL(device_unregister);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700
Andy Shevchenko3d060ae2015-07-27 18:04:00 +03002701static struct device *prev_device(struct klist_iter *i)
2702{
2703 struct klist_node *n = klist_prev(i);
2704 struct device *dev = NULL;
2705 struct device_private *p;
2706
2707 if (n) {
2708 p = to_device_private_parent(n);
2709 dev = p->device;
2710 }
2711 return dev;
2712}
2713
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002714static struct device *next_device(struct klist_iter *i)
mochel@digitalimplant.org36239572005-03-24 19:08:30 -08002715{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002716 struct klist_node *n = klist_next(i);
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08002717 struct device *dev = NULL;
2718 struct device_private *p;
2719
2720 if (n) {
2721 p = to_device_private_parent(n);
2722 dev = p->device;
2723 }
2724 return dev;
mochel@digitalimplant.org36239572005-03-24 19:08:30 -08002725}
2726
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727/**
Kay Sieverse454cea2009-09-18 23:01:12 +02002728 * device_get_devnode - path of device node file
Kay Sievers6fcf53a2009-04-30 15:23:42 +02002729 * @dev: device
Kay Sieverse454cea2009-09-18 23:01:12 +02002730 * @mode: returned file access mode
Kay Sievers3c2670e2013-04-06 09:56:00 -07002731 * @uid: returned file owner
2732 * @gid: returned file group
Kay Sievers6fcf53a2009-04-30 15:23:42 +02002733 * @tmp: possibly allocated string
2734 *
2735 * Return the relative path of a possible device node.
2736 * Non-default names may need to allocate a memory to compose
2737 * a name. This memory is returned in tmp and needs to be
2738 * freed by the caller.
2739 */
Kay Sieverse454cea2009-09-18 23:01:12 +02002740const char *device_get_devnode(struct device *dev,
Greg Kroah-Hartman4e4098a2013-04-11 11:43:29 -07002741 umode_t *mode, kuid_t *uid, kgid_t *gid,
Kay Sievers3c2670e2013-04-06 09:56:00 -07002742 const char **tmp)
Kay Sievers6fcf53a2009-04-30 15:23:42 +02002743{
2744 char *s;
2745
2746 *tmp = NULL;
2747
2748 /* the device type may provide a specific name */
Kay Sieverse454cea2009-09-18 23:01:12 +02002749 if (dev->type && dev->type->devnode)
Kay Sievers3c2670e2013-04-06 09:56:00 -07002750 *tmp = dev->type->devnode(dev, mode, uid, gid);
Kay Sievers6fcf53a2009-04-30 15:23:42 +02002751 if (*tmp)
2752 return *tmp;
2753
2754 /* the class may provide a specific name */
Kay Sieverse454cea2009-09-18 23:01:12 +02002755 if (dev->class && dev->class->devnode)
2756 *tmp = dev->class->devnode(dev, mode);
Kay Sievers6fcf53a2009-04-30 15:23:42 +02002757 if (*tmp)
2758 return *tmp;
2759
2760 /* return name without allocation, tmp == NULL */
2761 if (strchr(dev_name(dev), '!') == NULL)
2762 return dev_name(dev);
2763
2764 /* replace '!' in the name with '/' */
Rasmus Villemoesa29fd612015-06-25 15:02:33 -07002765 s = kstrdup(dev_name(dev), GFP_KERNEL);
2766 if (!s)
Kay Sievers6fcf53a2009-04-30 15:23:42 +02002767 return NULL;
Rasmus Villemoesa29fd612015-06-25 15:02:33 -07002768 strreplace(s, '!', '/');
2769 return *tmp = s;
Kay Sievers6fcf53a2009-04-30 15:23:42 +02002770}
2771
2772/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002773 * device_for_each_child - device child iterator.
2774 * @parent: parent struct device.
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002775 * @fn: function to be called for each device.
Robert P. J. Dayf8878dc2013-06-01 20:17:34 -04002776 * @data: data for the callback.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002778 * Iterate over @parent's child devices, and call @fn for each,
2779 * passing it @data.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002781 * We check the return of @fn each time. If it returns anything
2782 * other than 0, we break out and return that value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002784int device_for_each_child(struct device *parent, void *data,
2785 int (*fn)(struct device *dev, void *data))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786{
mochel@digitalimplant.org36239572005-03-24 19:08:30 -08002787 struct klist_iter i;
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002788 struct device *child;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789 int error = 0;
2790
Greg Kroah-Hartman014c90db2009-04-15 16:00:12 -07002791 if (!parent->p)
2792 return 0;
2793
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08002794 klist_iter_init(&parent->p->klist_children, &i);
Gimcuan Hui93ead7c2017-11-11 05:52:54 +00002795 while (!error && (child = next_device(&i)))
mochel@digitalimplant.org36239572005-03-24 19:08:30 -08002796 error = fn(child, data);
2797 klist_iter_exit(&i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798 return error;
2799}
David Graham White86df2682013-07-21 20:41:14 -04002800EXPORT_SYMBOL_GPL(device_for_each_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801
Cornelia Huck5ab69982006-11-16 15:42:07 +01002802/**
Andy Shevchenko3d060ae2015-07-27 18:04:00 +03002803 * device_for_each_child_reverse - device child iterator in reversed order.
2804 * @parent: parent struct device.
2805 * @fn: function to be called for each device.
2806 * @data: data for the callback.
2807 *
2808 * Iterate over @parent's child devices, and call @fn for each,
2809 * passing it @data.
2810 *
2811 * We check the return of @fn each time. If it returns anything
2812 * other than 0, we break out and return that value.
2813 */
2814int device_for_each_child_reverse(struct device *parent, void *data,
2815 int (*fn)(struct device *dev, void *data))
2816{
2817 struct klist_iter i;
2818 struct device *child;
2819 int error = 0;
2820
2821 if (!parent->p)
2822 return 0;
2823
2824 klist_iter_init(&parent->p->klist_children, &i);
2825 while ((child = prev_device(&i)) && !error)
2826 error = fn(child, data);
2827 klist_iter_exit(&i);
2828 return error;
2829}
2830EXPORT_SYMBOL_GPL(device_for_each_child_reverse);
2831
2832/**
Cornelia Huck5ab69982006-11-16 15:42:07 +01002833 * device_find_child - device iterator for locating a particular device.
2834 * @parent: parent struct device
Cornelia Huck5ab69982006-11-16 15:42:07 +01002835 * @match: Callback function to check device
Robert P. J. Dayf8878dc2013-06-01 20:17:34 -04002836 * @data: Data to pass to match function
Cornelia Huck5ab69982006-11-16 15:42:07 +01002837 *
2838 * This is similar to the device_for_each_child() function above, but it
2839 * returns a reference to a device that is 'found' for later use, as
2840 * determined by the @match callback.
2841 *
2842 * The callback should return 0 if the device doesn't match and non-zero
2843 * if it does. If the callback returns non-zero and a reference to the
2844 * current device can be obtained, this function will return to the caller
2845 * and not iterate over any more devices.
Federico Vagaa4e24002013-04-15 11:18:11 +02002846 *
2847 * NOTE: you will need to drop the reference with put_device() after use.
Cornelia Huck5ab69982006-11-16 15:42:07 +01002848 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002849struct device *device_find_child(struct device *parent, void *data,
2850 int (*match)(struct device *dev, void *data))
Cornelia Huck5ab69982006-11-16 15:42:07 +01002851{
2852 struct klist_iter i;
2853 struct device *child;
2854
2855 if (!parent)
2856 return NULL;
2857
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08002858 klist_iter_init(&parent->p->klist_children, &i);
Cornelia Huck5ab69982006-11-16 15:42:07 +01002859 while ((child = next_device(&i)))
2860 if (match(child, data) && get_device(child))
2861 break;
2862 klist_iter_exit(&i);
2863 return child;
2864}
David Graham White86df2682013-07-21 20:41:14 -04002865EXPORT_SYMBOL_GPL(device_find_child);
Cornelia Huck5ab69982006-11-16 15:42:07 +01002866
Heikki Krogerusdad9bb02019-05-31 17:15:37 +03002867/**
2868 * device_find_child_by_name - device iterator for locating a child device.
2869 * @parent: parent struct device
2870 * @name: name of the child device
2871 *
2872 * This is similar to the device_find_child() function above, but it
2873 * returns a reference to a device that has the name @name.
2874 *
2875 * NOTE: you will need to drop the reference with put_device() after use.
2876 */
2877struct device *device_find_child_by_name(struct device *parent,
2878 const char *name)
2879{
2880 struct klist_iter i;
2881 struct device *child;
2882
2883 if (!parent)
2884 return NULL;
2885
2886 klist_iter_init(&parent->p->klist_children, &i);
2887 while ((child = next_device(&i)))
2888 if (!strcmp(dev_name(child), name) && get_device(child))
2889 break;
2890 klist_iter_exit(&i);
2891 return child;
2892}
2893EXPORT_SYMBOL_GPL(device_find_child_by_name);
2894
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895int __init devices_init(void)
2896{
Greg Kroah-Hartman881c6cfd2007-11-01 09:29:06 -06002897 devices_kset = kset_create_and_add("devices", &device_uevent_ops, NULL);
2898 if (!devices_kset)
2899 return -ENOMEM;
Dan Williamse105b8b2008-04-21 10:51:07 -07002900 dev_kobj = kobject_create_and_add("dev", NULL);
2901 if (!dev_kobj)
2902 goto dev_kobj_err;
2903 sysfs_dev_block_kobj = kobject_create_and_add("block", dev_kobj);
2904 if (!sysfs_dev_block_kobj)
2905 goto block_kobj_err;
2906 sysfs_dev_char_kobj = kobject_create_and_add("char", dev_kobj);
2907 if (!sysfs_dev_char_kobj)
2908 goto char_kobj_err;
2909
Greg Kroah-Hartman881c6cfd2007-11-01 09:29:06 -06002910 return 0;
Dan Williamse105b8b2008-04-21 10:51:07 -07002911
2912 char_kobj_err:
2913 kobject_put(sysfs_dev_block_kobj);
2914 block_kobj_err:
2915 kobject_put(dev_kobj);
2916 dev_kobj_err:
2917 kset_unregister(devices_kset);
2918 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919}
2920
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02002921static int device_check_offline(struct device *dev, void *not_used)
2922{
2923 int ret;
2924
2925 ret = device_for_each_child(dev, NULL, device_check_offline);
2926 if (ret)
2927 return ret;
2928
2929 return device_supports_offline(dev) && !dev->offline ? -EBUSY : 0;
2930}
2931
2932/**
2933 * device_offline - Prepare the device for hot-removal.
2934 * @dev: Device to be put offline.
2935 *
2936 * Execute the device bus type's .offline() callback, if present, to prepare
2937 * the device for a subsequent hot-removal. If that succeeds, the device must
2938 * not be used until either it is removed or its bus type's .online() callback
2939 * is executed.
2940 *
2941 * Call under device_hotplug_lock.
2942 */
2943int device_offline(struct device *dev)
2944{
2945 int ret;
2946
2947 if (dev->offline_disabled)
2948 return -EPERM;
2949
2950 ret = device_for_each_child(dev, NULL, device_check_offline);
2951 if (ret)
2952 return ret;
2953
2954 device_lock(dev);
2955 if (device_supports_offline(dev)) {
2956 if (dev->offline) {
2957 ret = 1;
2958 } else {
2959 ret = dev->bus->offline(dev);
2960 if (!ret) {
2961 kobject_uevent(&dev->kobj, KOBJ_OFFLINE);
2962 dev->offline = true;
2963 }
2964 }
2965 }
2966 device_unlock(dev);
2967
2968 return ret;
2969}
2970
2971/**
2972 * device_online - Put the device back online after successful device_offline().
2973 * @dev: Device to be put back online.
2974 *
2975 * If device_offline() has been successfully executed for @dev, but the device
2976 * has not been removed subsequently, execute its bus type's .online() callback
2977 * to indicate that the device can be used again.
2978 *
2979 * Call under device_hotplug_lock.
2980 */
2981int device_online(struct device *dev)
2982{
2983 int ret = 0;
2984
2985 device_lock(dev);
2986 if (device_supports_offline(dev)) {
2987 if (dev->offline) {
2988 ret = dev->bus->online(dev);
2989 if (!ret) {
2990 kobject_uevent(&dev->kobj, KOBJ_ONLINE);
2991 dev->offline = false;
2992 }
2993 } else {
2994 ret = 1;
2995 }
2996 }
2997 device_unlock(dev);
2998
2999 return ret;
3000}
3001
Karthigan Srinivasan7f100d12011-04-18 16:16:52 -05003002struct root_device {
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00003003 struct device dev;
3004 struct module *owner;
3005};
3006
Josh Triplett93058422012-11-18 21:27:55 -08003007static inline struct root_device *to_root_device(struct device *d)
Ferenc Wagner481e2072011-01-07 15:17:47 +01003008{
3009 return container_of(d, struct root_device, dev);
3010}
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00003011
3012static void root_device_release(struct device *dev)
3013{
3014 kfree(to_root_device(dev));
3015}
3016
3017/**
3018 * __root_device_register - allocate and register a root device
3019 * @name: root device name
3020 * @owner: owner module of the root device, usually THIS_MODULE
3021 *
3022 * This function allocates a root device and registers it
3023 * using device_register(). In order to free the returned
3024 * device, use root_device_unregister().
3025 *
3026 * Root devices are dummy devices which allow other devices
3027 * to be grouped under /sys/devices. Use this function to
3028 * allocate a root device and then use it as the parent of
3029 * any device which should appear under /sys/devices/{name}
3030 *
3031 * The /sys/devices/{name} directory will also contain a
3032 * 'module' symlink which points to the @owner directory
3033 * in sysfs.
3034 *
Jani Nikulaf0eae0e2010-03-11 18:11:45 +02003035 * Returns &struct device pointer on success, or ERR_PTR() on error.
3036 *
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00003037 * Note: You probably want to use root_device_register().
3038 */
3039struct device *__root_device_register(const char *name, struct module *owner)
3040{
3041 struct root_device *root;
3042 int err = -ENOMEM;
3043
3044 root = kzalloc(sizeof(struct root_device), GFP_KERNEL);
3045 if (!root)
3046 return ERR_PTR(err);
3047
Greg Kroah-Hartmanacc0e902009-06-02 15:39:55 -07003048 err = dev_set_name(&root->dev, "%s", name);
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00003049 if (err) {
3050 kfree(root);
3051 return ERR_PTR(err);
3052 }
3053
3054 root->dev.release = root_device_release;
3055
3056 err = device_register(&root->dev);
3057 if (err) {
3058 put_device(&root->dev);
3059 return ERR_PTR(err);
3060 }
3061
Christoph Egger1d9e8822010-05-17 16:57:58 +02003062#ifdef CONFIG_MODULES /* gotta find a "cleaner" way to do this */
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00003063 if (owner) {
3064 struct module_kobject *mk = &owner->mkobj;
3065
3066 err = sysfs_create_link(&root->dev.kobj, &mk->kobj, "module");
3067 if (err) {
3068 device_unregister(&root->dev);
3069 return ERR_PTR(err);
3070 }
3071 root->owner = owner;
3072 }
3073#endif
3074
3075 return &root->dev;
3076}
3077EXPORT_SYMBOL_GPL(__root_device_register);
3078
3079/**
3080 * root_device_unregister - unregister and free a root device
Randy Dunlap7cbcf222009-01-20 16:29:13 -08003081 * @dev: device going away
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00003082 *
3083 * This function unregisters and cleans up a device that was created by
3084 * root_device_register().
3085 */
3086void root_device_unregister(struct device *dev)
3087{
3088 struct root_device *root = to_root_device(dev);
3089
3090 if (root->owner)
3091 sysfs_remove_link(&root->dev.kobj, "module");
3092
3093 device_unregister(dev);
3094}
3095EXPORT_SYMBOL_GPL(root_device_unregister);
3096
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07003097
3098static void device_create_release(struct device *dev)
3099{
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01003100 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07003101 kfree(dev);
3102}
3103
Mathieu Malaterre6a8b55d2018-05-05 21:57:41 +02003104static __printf(6, 0) struct device *
Guenter Roeck39ef3112013-07-14 16:05:57 -07003105device_create_groups_vargs(struct class *class, struct device *parent,
3106 dev_t devt, void *drvdata,
3107 const struct attribute_group **groups,
3108 const char *fmt, va_list args)
3109{
3110 struct device *dev = NULL;
3111 int retval = -ENODEV;
3112
3113 if (class == NULL || IS_ERR(class))
3114 goto error;
3115
3116 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
3117 if (!dev) {
3118 retval = -ENOMEM;
3119 goto error;
3120 }
3121
David Herrmannbbc780f2013-11-21 20:15:48 +01003122 device_initialize(dev);
Guenter Roeck39ef3112013-07-14 16:05:57 -07003123 dev->devt = devt;
3124 dev->class = class;
3125 dev->parent = parent;
3126 dev->groups = groups;
3127 dev->release = device_create_release;
3128 dev_set_drvdata(dev, drvdata);
3129
3130 retval = kobject_set_name_vargs(&dev->kobj, fmt, args);
3131 if (retval)
3132 goto error;
3133
David Herrmannbbc780f2013-11-21 20:15:48 +01003134 retval = device_add(dev);
Guenter Roeck39ef3112013-07-14 16:05:57 -07003135 if (retval)
3136 goto error;
3137
3138 return dev;
3139
3140error:
3141 put_device(dev);
3142 return ERR_PTR(retval);
3143}
3144
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07003145/**
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07003146 * device_create_vargs - creates a device and registers it with sysfs
3147 * @class: pointer to the struct class that this device should be registered to
3148 * @parent: pointer to the parent struct device of this new device, if any
3149 * @devt: the dev_t for the char device to be added
3150 * @drvdata: the data to be added to the device for callbacks
3151 * @fmt: string for the device's name
3152 * @args: va_list for the device's name
3153 *
3154 * This function can be used by char device classes. A struct device
3155 * will be created in sysfs, registered to the specified class.
3156 *
3157 * A "dev" file will be created, showing the dev_t for the device, if
3158 * the dev_t is not 0,0.
3159 * If a pointer to a parent struct device is passed in, the newly created
3160 * struct device will be a child of that device in sysfs.
3161 * The pointer to the struct device will be returned from the call.
3162 * Any further sysfs files that might be required can be created using this
3163 * pointer.
3164 *
Jani Nikulaf0eae0e2010-03-11 18:11:45 +02003165 * Returns &struct device pointer on success, or ERR_PTR() on error.
3166 *
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07003167 * Note: the struct class passed to this function must have previously
3168 * been created with a call to class_create().
3169 */
3170struct device *device_create_vargs(struct class *class, struct device *parent,
3171 dev_t devt, void *drvdata, const char *fmt,
3172 va_list args)
3173{
Guenter Roeck39ef3112013-07-14 16:05:57 -07003174 return device_create_groups_vargs(class, parent, devt, drvdata, NULL,
3175 fmt, args);
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07003176}
3177EXPORT_SYMBOL_GPL(device_create_vargs);
3178
3179/**
Greg Kroah-Hartman4e106732008-07-21 20:03:34 -07003180 * device_create - creates a device and registers it with sysfs
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07003181 * @class: pointer to the struct class that this device should be registered to
3182 * @parent: pointer to the parent struct device of this new device, if any
3183 * @devt: the dev_t for the char device to be added
3184 * @drvdata: the data to be added to the device for callbacks
3185 * @fmt: string for the device's name
3186 *
3187 * This function can be used by char device classes. A struct device
3188 * will be created in sysfs, registered to the specified class.
3189 *
3190 * A "dev" file will be created, showing the dev_t for the device, if
3191 * the dev_t is not 0,0.
3192 * If a pointer to a parent struct device is passed in, the newly created
3193 * struct device will be a child of that device in sysfs.
3194 * The pointer to the struct device will be returned from the call.
3195 * Any further sysfs files that might be required can be created using this
3196 * pointer.
3197 *
Jani Nikulaf0eae0e2010-03-11 18:11:45 +02003198 * Returns &struct device pointer on success, or ERR_PTR() on error.
3199 *
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07003200 * Note: the struct class passed to this function must have previously
3201 * been created with a call to class_create().
3202 */
Greg Kroah-Hartman4e106732008-07-21 20:03:34 -07003203struct device *device_create(struct class *class, struct device *parent,
3204 dev_t devt, void *drvdata, const char *fmt, ...)
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07003205{
3206 va_list vargs;
3207 struct device *dev;
3208
3209 va_start(vargs, fmt);
3210 dev = device_create_vargs(class, parent, devt, drvdata, fmt, vargs);
3211 va_end(vargs);
3212 return dev;
3213}
Greg Kroah-Hartman4e106732008-07-21 20:03:34 -07003214EXPORT_SYMBOL_GPL(device_create);
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07003215
Guenter Roeck39ef3112013-07-14 16:05:57 -07003216/**
3217 * device_create_with_groups - creates a device and registers it with sysfs
3218 * @class: pointer to the struct class that this device should be registered to
3219 * @parent: pointer to the parent struct device of this new device, if any
3220 * @devt: the dev_t for the char device to be added
3221 * @drvdata: the data to be added to the device for callbacks
3222 * @groups: NULL-terminated list of attribute groups to be created
3223 * @fmt: string for the device's name
3224 *
3225 * This function can be used by char device classes. A struct device
3226 * will be created in sysfs, registered to the specified class.
3227 * Additional attributes specified in the groups parameter will also
3228 * be created automatically.
3229 *
3230 * A "dev" file will be created, showing the dev_t for the device, if
3231 * the dev_t is not 0,0.
3232 * If a pointer to a parent struct device is passed in, the newly created
3233 * struct device will be a child of that device in sysfs.
3234 * The pointer to the struct device will be returned from the call.
3235 * Any further sysfs files that might be required can be created using this
3236 * pointer.
3237 *
3238 * Returns &struct device pointer on success, or ERR_PTR() on error.
3239 *
3240 * Note: the struct class passed to this function must have previously
3241 * been created with a call to class_create().
3242 */
3243struct device *device_create_with_groups(struct class *class,
3244 struct device *parent, dev_t devt,
3245 void *drvdata,
3246 const struct attribute_group **groups,
3247 const char *fmt, ...)
3248{
3249 va_list vargs;
3250 struct device *dev;
3251
3252 va_start(vargs, fmt);
3253 dev = device_create_groups_vargs(class, parent, devt, drvdata, groups,
3254 fmt, vargs);
3255 va_end(vargs);
3256 return dev;
3257}
3258EXPORT_SYMBOL_GPL(device_create_with_groups);
3259
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01003260/**
3261 * device_destroy - removes a device that was created with device_create()
3262 * @class: pointer to the struct class that this device was registered with
3263 * @devt: the dev_t of the device that was previously registered
3264 *
3265 * This call unregisters and cleans up a device that was created with a
3266 * call to device_create().
3267 */
3268void device_destroy(struct class *class, dev_t devt)
3269{
3270 struct device *dev;
3271
Suzuki K Poulose4495dfd2019-07-23 23:18:35 +01003272 dev = class_find_device_by_devt(class, devt);
Dave Youngcd354492008-01-28 16:56:11 +08003273 if (dev) {
3274 put_device(dev);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07003275 device_unregister(dev);
Dave Youngcd354492008-01-28 16:56:11 +08003276 }
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07003277}
3278EXPORT_SYMBOL_GPL(device_destroy);
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07003279
3280/**
3281 * device_rename - renames a device
3282 * @dev: the pointer to the struct device to be renamed
3283 * @new_name: the new name of the device
Eric W. Biederman030c1d22008-05-08 14:41:00 -07003284 *
3285 * It is the responsibility of the caller to provide mutual
3286 * exclusion between two different calls of device_rename
3287 * on the same device to ensure that new_name is valid and
3288 * won't conflict with other devices.
Michael Ellermanc6c0ac62010-11-25 09:44:07 +11003289 *
Timur Tabia5462512010-12-13 14:08:52 -06003290 * Note: Don't call this function. Currently, the networking layer calls this
3291 * function, but that will change. The following text from Kay Sievers offers
3292 * some insight:
3293 *
3294 * Renaming devices is racy at many levels, symlinks and other stuff are not
3295 * replaced atomically, and you get a "move" uevent, but it's not easy to
3296 * connect the event to the old and new device. Device nodes are not renamed at
3297 * all, there isn't even support for that in the kernel now.
3298 *
3299 * In the meantime, during renaming, your target name might be taken by another
3300 * driver, creating conflicts. Or the old name is taken directly after you
3301 * renamed it -- then you get events for the same DEVPATH, before you even see
3302 * the "move" event. It's just a mess, and nothing new should ever rely on
3303 * kernel device renaming. Besides that, it's not even implemented now for
3304 * other things than (driver-core wise very simple) network devices.
3305 *
3306 * We are currently about to change network renaming in udev to completely
3307 * disallow renaming of devices in the same namespace as the kernel uses,
3308 * because we can't solve the problems properly, that arise with swapping names
3309 * of multiple interfaces without races. Means, renaming of eth[0-9]* will only
3310 * be allowed to some other name than eth[0-9]*, for the aforementioned
3311 * reasons.
3312 *
3313 * Make up a "real" name in the driver before you register anything, or add
3314 * some other attributes for userspace to find the device, or use udev to add
3315 * symlinks -- but never rename kernel devices later, it's a complete mess. We
3316 * don't even want to get into that and try to implement the missing pieces in
3317 * the core. We really have other pieces to fix in the driver core mess. :)
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07003318 */
Johannes Berg6937e8f2010-08-05 17:38:18 +02003319int device_rename(struct device *dev, const char *new_name)
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07003320{
Tejun Heo4b30ee52013-09-11 22:29:06 -04003321 struct kobject *kobj = &dev->kobj;
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07003322 char *old_device_name = NULL;
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07003323 int error;
3324
3325 dev = get_device(dev);
3326 if (!dev)
3327 return -EINVAL;
3328
ethan.zhao69df7532013-10-13 22:12:35 +08003329 dev_dbg(dev, "renaming to %s\n", new_name);
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07003330
Kay Sievers1fa5ae82009-01-25 15:17:37 +01003331 old_device_name = kstrdup(dev_name(dev), GFP_KERNEL);
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07003332 if (!old_device_name) {
3333 error = -ENOMEM;
3334 goto out;
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07003335 }
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07003336
Eric W. Biedermanf349cf32010-03-30 11:31:29 -07003337 if (dev->class) {
Tejun Heo4b30ee52013-09-11 22:29:06 -04003338 error = sysfs_rename_link_ns(&dev->class->p->subsys.kobj,
3339 kobj, old_device_name,
3340 new_name, kobject_namespace(kobj));
Eric W. Biedermanf349cf32010-03-30 11:31:29 -07003341 if (error)
3342 goto out;
3343 }
Kay Sievers39aba962010-09-04 22:33:14 -07003344
Tejun Heo4b30ee52013-09-11 22:29:06 -04003345 error = kobject_rename(kobj, new_name);
Kay Sievers1fa5ae82009-01-25 15:17:37 +01003346 if (error)
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07003347 goto out;
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07003348
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07003349out:
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07003350 put_device(dev);
3351
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07003352 kfree(old_device_name);
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07003353
3354 return error;
3355}
Johannes Berga2807db2007-02-28 12:38:31 +01003356EXPORT_SYMBOL_GPL(device_rename);
Cornelia Huck8a824722006-11-20 17:07:51 +01003357
3358static int device_move_class_links(struct device *dev,
3359 struct device *old_parent,
3360 struct device *new_parent)
3361{
Greg Kroah-Hartmanf7f34612007-03-06 12:55:53 -08003362 int error = 0;
Cornelia Huck8a824722006-11-20 17:07:51 +01003363
Greg Kroah-Hartmanf7f34612007-03-06 12:55:53 -08003364 if (old_parent)
3365 sysfs_remove_link(&dev->kobj, "device");
3366 if (new_parent)
3367 error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
3368 "device");
3369 return error;
Cornelia Huck8a824722006-11-20 17:07:51 +01003370}
3371
3372/**
3373 * device_move - moves a device to a new parent
3374 * @dev: the pointer to the struct device to be moved
Wolfram Sang13509862018-05-06 13:23:47 +02003375 * @new_parent: the new parent of the device (can be NULL)
Cornelia Huckffa6a702009-03-04 12:44:00 +01003376 * @dpm_order: how to reorder the dpm_list
Cornelia Huck8a824722006-11-20 17:07:51 +01003377 */
Cornelia Huckffa6a702009-03-04 12:44:00 +01003378int device_move(struct device *dev, struct device *new_parent,
3379 enum dpm_order dpm_order)
Cornelia Huck8a824722006-11-20 17:07:51 +01003380{
3381 int error;
3382 struct device *old_parent;
Cornelia Huckc744aeae2007-01-08 20:16:44 +01003383 struct kobject *new_parent_kobj;
Cornelia Huck8a824722006-11-20 17:07:51 +01003384
3385 dev = get_device(dev);
3386 if (!dev)
3387 return -EINVAL;
3388
Cornelia Huckffa6a702009-03-04 12:44:00 +01003389 device_pm_lock();
Cornelia Huck8a824722006-11-20 17:07:51 +01003390 new_parent = get_device(new_parent);
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08003391 new_parent_kobj = get_device_parent(dev, new_parent);
Tetsuo Handa84d0c272018-05-07 19:10:31 +09003392 if (IS_ERR(new_parent_kobj)) {
3393 error = PTR_ERR(new_parent_kobj);
3394 put_device(new_parent);
3395 goto out;
3396 }
Cornelia Huck63b69712008-01-21 16:09:44 +01003397
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01003398 pr_debug("device: '%s': %s: moving to '%s'\n", dev_name(dev),
3399 __func__, new_parent ? dev_name(new_parent) : "<NULL>");
Cornelia Huckc744aeae2007-01-08 20:16:44 +01003400 error = kobject_move(&dev->kobj, new_parent_kobj);
Cornelia Huck8a824722006-11-20 17:07:51 +01003401 if (error) {
Cornelia Huck63b69712008-01-21 16:09:44 +01003402 cleanup_glue_dir(dev, new_parent_kobj);
Cornelia Huck8a824722006-11-20 17:07:51 +01003403 put_device(new_parent);
3404 goto out;
3405 }
3406 old_parent = dev->parent;
3407 dev->parent = new_parent;
3408 if (old_parent)
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08003409 klist_remove(&dev->p->knode_parent);
Yinghai Lu0d358f22008-02-19 03:20:41 -08003410 if (new_parent) {
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08003411 klist_add_tail(&dev->p->knode_parent,
3412 &new_parent->p->klist_children);
Yinghai Lu0d358f22008-02-19 03:20:41 -08003413 set_dev_node(dev, dev_to_node(new_parent));
3414 }
3415
Rabin Vincentbdd40342012-04-23 09:16:36 +02003416 if (dev->class) {
3417 error = device_move_class_links(dev, old_parent, new_parent);
3418 if (error) {
3419 /* We ignore errors on cleanup since we're hosed anyway... */
3420 device_move_class_links(dev, new_parent, old_parent);
3421 if (!kobject_move(&dev->kobj, &old_parent->kobj)) {
3422 if (new_parent)
3423 klist_remove(&dev->p->knode_parent);
3424 dev->parent = old_parent;
3425 if (old_parent) {
3426 klist_add_tail(&dev->p->knode_parent,
3427 &old_parent->p->klist_children);
3428 set_dev_node(dev, dev_to_node(old_parent));
3429 }
Yinghai Lu0d358f22008-02-19 03:20:41 -08003430 }
Rabin Vincentbdd40342012-04-23 09:16:36 +02003431 cleanup_glue_dir(dev, new_parent_kobj);
3432 put_device(new_parent);
3433 goto out;
Cornelia Huck8a824722006-11-20 17:07:51 +01003434 }
Cornelia Huck8a824722006-11-20 17:07:51 +01003435 }
Cornelia Huckffa6a702009-03-04 12:44:00 +01003436 switch (dpm_order) {
3437 case DPM_ORDER_NONE:
3438 break;
3439 case DPM_ORDER_DEV_AFTER_PARENT:
3440 device_pm_move_after(dev, new_parent);
Grygorii Strashko52cdbdd2015-07-27 20:43:01 +03003441 devices_kset_move_after(dev, new_parent);
Cornelia Huckffa6a702009-03-04 12:44:00 +01003442 break;
3443 case DPM_ORDER_PARENT_BEFORE_DEV:
3444 device_pm_move_before(new_parent, dev);
Grygorii Strashko52cdbdd2015-07-27 20:43:01 +03003445 devices_kset_move_before(new_parent, dev);
Cornelia Huckffa6a702009-03-04 12:44:00 +01003446 break;
3447 case DPM_ORDER_DEV_LAST:
3448 device_pm_move_last(dev);
Grygorii Strashko52cdbdd2015-07-27 20:43:01 +03003449 devices_kset_move_last(dev);
Cornelia Huckffa6a702009-03-04 12:44:00 +01003450 break;
3451 }
Rabin Vincentbdd40342012-04-23 09:16:36 +02003452
Cornelia Huck8a824722006-11-20 17:07:51 +01003453 put_device(old_parent);
3454out:
Cornelia Huckffa6a702009-03-04 12:44:00 +01003455 device_pm_unlock();
Cornelia Huck8a824722006-11-20 17:07:51 +01003456 put_device(dev);
3457 return error;
3458}
Cornelia Huck8a824722006-11-20 17:07:51 +01003459EXPORT_SYMBOL_GPL(device_move);
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08003460
Christian Braunerb8f33e52020-02-27 04:37:15 +01003461static int device_attrs_change_owner(struct device *dev, kuid_t kuid,
3462 kgid_t kgid)
3463{
3464 struct kobject *kobj = &dev->kobj;
3465 struct class *class = dev->class;
3466 const struct device_type *type = dev->type;
3467 int error;
3468
3469 if (class) {
3470 /*
3471 * Change the device groups of the device class for @dev to
3472 * @kuid/@kgid.
3473 */
3474 error = sysfs_groups_change_owner(kobj, class->dev_groups, kuid,
3475 kgid);
3476 if (error)
3477 return error;
3478 }
3479
3480 if (type) {
3481 /*
3482 * Change the device groups of the device type for @dev to
3483 * @kuid/@kgid.
3484 */
3485 error = sysfs_groups_change_owner(kobj, type->groups, kuid,
3486 kgid);
3487 if (error)
3488 return error;
3489 }
3490
3491 /* Change the device groups of @dev to @kuid/@kgid. */
3492 error = sysfs_groups_change_owner(kobj, dev->groups, kuid, kgid);
3493 if (error)
3494 return error;
3495
3496 if (device_supports_offline(dev) && !dev->offline_disabled) {
3497 /* Change online device attributes of @dev to @kuid/@kgid. */
3498 error = sysfs_file_change_owner(kobj, dev_attr_online.attr.name,
3499 kuid, kgid);
3500 if (error)
3501 return error;
3502 }
3503
3504 return 0;
3505}
3506
3507/**
3508 * device_change_owner - change the owner of an existing device.
3509 * @dev: device.
3510 * @kuid: new owner's kuid
3511 * @kgid: new owner's kgid
3512 *
3513 * This changes the owner of @dev and its corresponding sysfs entries to
3514 * @kuid/@kgid. This function closely mirrors how @dev was added via driver
3515 * core.
3516 *
3517 * Returns 0 on success or error code on failure.
3518 */
3519int device_change_owner(struct device *dev, kuid_t kuid, kgid_t kgid)
3520{
3521 int error;
3522 struct kobject *kobj = &dev->kobj;
3523
3524 dev = get_device(dev);
3525 if (!dev)
3526 return -EINVAL;
3527
3528 /*
3529 * Change the kobject and the default attributes and groups of the
3530 * ktype associated with it to @kuid/@kgid.
3531 */
3532 error = sysfs_change_owner(kobj, kuid, kgid);
3533 if (error)
3534 goto out;
3535
3536 /*
3537 * Change the uevent file for @dev to the new owner. The uevent file
3538 * was created in a separate step when @dev got added and we mirror
3539 * that step here.
3540 */
3541 error = sysfs_file_change_owner(kobj, dev_attr_uevent.attr.name, kuid,
3542 kgid);
3543 if (error)
3544 goto out;
3545
3546 /*
3547 * Change the device groups, the device groups associated with the
3548 * device class, and the groups associated with the device type of @dev
3549 * to @kuid/@kgid.
3550 */
3551 error = device_attrs_change_owner(dev, kuid, kgid);
3552 if (error)
3553 goto out;
3554
3555#ifdef CONFIG_BLOCK
3556 if (sysfs_deprecated && dev->class == &block_class)
3557 goto out;
3558#endif
3559
3560 /*
3561 * Change the owner of the symlink located in the class directory of
3562 * the device class associated with @dev which points to the actual
3563 * directory entry for @dev to @kuid/@kgid. This ensures that the
3564 * symlink shows the same permissions as its target.
3565 */
3566 error = sysfs_link_change_owner(&dev->class->p->subsys.kobj, &dev->kobj,
3567 dev_name(dev), kuid, kgid);
3568 if (error)
3569 goto out;
3570
3571out:
3572 put_device(dev);
3573 return error;
3574}
3575EXPORT_SYMBOL_GPL(device_change_owner);
3576
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08003577/**
3578 * device_shutdown - call ->shutdown() on each device to shutdown.
3579 */
3580void device_shutdown(void)
3581{
Benson Leungf123db82013-09-24 20:05:11 -07003582 struct device *dev, *parent;
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08003583
Pingfan Liu3297c8f2018-07-19 13:14:58 +08003584 wait_for_device_probe();
3585 device_block_probing();
3586
Rafael J. Wysocki65650b32019-10-09 01:29:10 +02003587 cpufreq_suspend();
3588
Hugh Daschbach62458382010-03-22 10:36:37 -07003589 spin_lock(&devices_kset->list_lock);
3590 /*
3591 * Walk the devices list backward, shutting down each in turn.
3592 * Beware that device unplug events may also start pulling
3593 * devices offline, even as the system is shutting down.
3594 */
3595 while (!list_empty(&devices_kset->list)) {
3596 dev = list_entry(devices_kset->list.prev, struct device,
3597 kobj.entry);
Ming Leid1c6c032012-06-22 18:01:40 +08003598
3599 /*
3600 * hold reference count of device's parent to
3601 * prevent it from being freed because parent's
3602 * lock is to be held
3603 */
Benson Leungf123db82013-09-24 20:05:11 -07003604 parent = get_device(dev->parent);
Hugh Daschbach62458382010-03-22 10:36:37 -07003605 get_device(dev);
3606 /*
3607 * Make sure the device is off the kset list, in the
3608 * event that dev->*->shutdown() doesn't remove it.
3609 */
3610 list_del_init(&dev->kobj.entry);
3611 spin_unlock(&devices_kset->list_lock);
Alan Sternfe6b91f2011-12-06 23:24:52 +01003612
Ming Leid1c6c032012-06-22 18:01:40 +08003613 /* hold lock to avoid race with probe/release */
Benson Leungf123db82013-09-24 20:05:11 -07003614 if (parent)
3615 device_lock(parent);
Ming Leid1c6c032012-06-22 18:01:40 +08003616 device_lock(dev);
3617
Alan Sternfe6b91f2011-12-06 23:24:52 +01003618 /* Don't allow any more runtime suspends */
3619 pm_runtime_get_noresume(dev);
3620 pm_runtime_barrier(dev);
Hugh Daschbach62458382010-03-22 10:36:37 -07003621
Michal Suchanek75216212017-08-11 15:44:43 +02003622 if (dev->class && dev->class->shutdown_pre) {
Josh Zimmermanf77af152017-06-25 14:53:23 -07003623 if (initcall_debug)
Michal Suchanek75216212017-08-11 15:44:43 +02003624 dev_info(dev, "shutdown_pre\n");
3625 dev->class->shutdown_pre(dev);
3626 }
3627 if (dev->bus && dev->bus->shutdown) {
ShuoX Liu0246c4f2012-11-23 15:14:12 +08003628 if (initcall_debug)
3629 dev_info(dev, "shutdown\n");
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08003630 dev->bus->shutdown(dev);
3631 } else if (dev->driver && dev->driver->shutdown) {
ShuoX Liu0246c4f2012-11-23 15:14:12 +08003632 if (initcall_debug)
3633 dev_info(dev, "shutdown\n");
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08003634 dev->driver->shutdown(dev);
3635 }
Ming Leid1c6c032012-06-22 18:01:40 +08003636
3637 device_unlock(dev);
Benson Leungf123db82013-09-24 20:05:11 -07003638 if (parent)
3639 device_unlock(parent);
Ming Leid1c6c032012-06-22 18:01:40 +08003640
Hugh Daschbach62458382010-03-22 10:36:37 -07003641 put_device(dev);
Benson Leungf123db82013-09-24 20:05:11 -07003642 put_device(parent);
Hugh Daschbach62458382010-03-22 10:36:37 -07003643
3644 spin_lock(&devices_kset->list_lock);
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08003645 }
Hugh Daschbach62458382010-03-22 10:36:37 -07003646 spin_unlock(&devices_kset->list_lock);
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08003647}
Joe Perches99bcf212010-06-27 01:02:34 +00003648
3649/*
3650 * Device logging functions
3651 */
3652
3653#ifdef CONFIG_PRINTK
Joe Perches666f3552012-09-12 20:14:11 -07003654static int
3655create_syslog_header(const struct device *dev, char *hdr, size_t hdrlen)
Joe Perches99bcf212010-06-27 01:02:34 +00003656{
Kay Sieversc4e00da2012-05-03 02:29:59 +02003657 const char *subsys;
Joe Perches798efc62012-09-12 20:11:29 -07003658 size_t pos = 0;
Joe Perches99bcf212010-06-27 01:02:34 +00003659
Kay Sieversc4e00da2012-05-03 02:29:59 +02003660 if (dev->class)
3661 subsys = dev->class->name;
3662 else if (dev->bus)
3663 subsys = dev->bus->name;
3664 else
Joe Perches798efc62012-09-12 20:11:29 -07003665 return 0;
Kay Sieversc4e00da2012-05-03 02:29:59 +02003666
Joe Perches798efc62012-09-12 20:11:29 -07003667 pos += snprintf(hdr + pos, hdrlen - pos, "SUBSYSTEM=%s", subsys);
Ben Hutchings655e5b72014-08-26 00:34:44 -07003668 if (pos >= hdrlen)
3669 goto overflow;
Kay Sieversc4e00da2012-05-03 02:29:59 +02003670
3671 /*
3672 * Add device identifier DEVICE=:
3673 * b12:8 block dev_t
3674 * c127:3 char dev_t
3675 * n8 netdev ifindex
3676 * +sound:card0 subsystem:devname
3677 */
3678 if (MAJOR(dev->devt)) {
3679 char c;
3680
3681 if (strcmp(subsys, "block") == 0)
3682 c = 'b';
3683 else
3684 c = 'c';
Joe Perches798efc62012-09-12 20:11:29 -07003685 pos++;
3686 pos += snprintf(hdr + pos, hdrlen - pos,
3687 "DEVICE=%c%u:%u",
3688 c, MAJOR(dev->devt), MINOR(dev->devt));
Kay Sieversc4e00da2012-05-03 02:29:59 +02003689 } else if (strcmp(subsys, "net") == 0) {
3690 struct net_device *net = to_net_dev(dev);
3691
Joe Perches798efc62012-09-12 20:11:29 -07003692 pos++;
3693 pos += snprintf(hdr + pos, hdrlen - pos,
3694 "DEVICE=n%u", net->ifindex);
Kay Sieversc4e00da2012-05-03 02:29:59 +02003695 } else {
Joe Perches798efc62012-09-12 20:11:29 -07003696 pos++;
3697 pos += snprintf(hdr + pos, hdrlen - pos,
3698 "DEVICE=+%s:%s", subsys, dev_name(dev));
Kay Sieversc4e00da2012-05-03 02:29:59 +02003699 }
Jim Cromieaf7f2152012-07-19 13:46:21 -06003700
Ben Hutchings655e5b72014-08-26 00:34:44 -07003701 if (pos >= hdrlen)
3702 goto overflow;
3703
Joe Perches798efc62012-09-12 20:11:29 -07003704 return pos;
Ben Hutchings655e5b72014-08-26 00:34:44 -07003705
3706overflow:
3707 dev_WARN(dev, "device/subsystem name too long");
3708 return 0;
Joe Perches99bcf212010-06-27 01:02:34 +00003709}
Joe Perches798efc62012-09-12 20:11:29 -07003710
Joe Perches05e4e5b2012-09-12 20:13:37 -07003711int dev_vprintk_emit(int level, const struct device *dev,
3712 const char *fmt, va_list args)
3713{
3714 char hdr[128];
3715 size_t hdrlen;
3716
3717 hdrlen = create_syslog_header(dev, hdr, sizeof(hdr));
3718
3719 return vprintk_emit(0, level, hdrlen ? hdr : NULL, hdrlen, fmt, args);
3720}
3721EXPORT_SYMBOL(dev_vprintk_emit);
3722
3723int dev_printk_emit(int level, const struct device *dev, const char *fmt, ...)
3724{
3725 va_list args;
3726 int r;
3727
3728 va_start(args, fmt);
3729
3730 r = dev_vprintk_emit(level, dev, fmt, args);
3731
3732 va_end(args);
3733
3734 return r;
3735}
3736EXPORT_SYMBOL(dev_printk_emit);
3737
Joe Perchesd1f10522014-12-25 15:07:04 -08003738static void __dev_printk(const char *level, const struct device *dev,
Joe Perches798efc62012-09-12 20:11:29 -07003739 struct va_format *vaf)
3740{
Joe Perchesd1f10522014-12-25 15:07:04 -08003741 if (dev)
3742 dev_printk_emit(level[1] - '0', dev, "%s %s: %pV",
3743 dev_driver_string(dev), dev_name(dev), vaf);
3744 else
3745 printk("%s(NULL device *): %pV", level, vaf);
Joe Perches798efc62012-09-12 20:11:29 -07003746}
Joe Perches99bcf212010-06-27 01:02:34 +00003747
Joe Perchesd1f10522014-12-25 15:07:04 -08003748void dev_printk(const char *level, const struct device *dev,
3749 const char *fmt, ...)
Joe Perches99bcf212010-06-27 01:02:34 +00003750{
3751 struct va_format vaf;
3752 va_list args;
Joe Perches99bcf212010-06-27 01:02:34 +00003753
3754 va_start(args, fmt);
3755
3756 vaf.fmt = fmt;
3757 vaf.va = &args;
3758
Joe Perchesd1f10522014-12-25 15:07:04 -08003759 __dev_printk(level, dev, &vaf);
Joe Perches798efc62012-09-12 20:11:29 -07003760
Joe Perches99bcf212010-06-27 01:02:34 +00003761 va_end(args);
Joe Perches99bcf212010-06-27 01:02:34 +00003762}
3763EXPORT_SYMBOL(dev_printk);
3764
3765#define define_dev_printk_level(func, kern_level) \
Joe Perchesd1f10522014-12-25 15:07:04 -08003766void func(const struct device *dev, const char *fmt, ...) \
Joe Perches99bcf212010-06-27 01:02:34 +00003767{ \
3768 struct va_format vaf; \
3769 va_list args; \
Joe Perches99bcf212010-06-27 01:02:34 +00003770 \
3771 va_start(args, fmt); \
3772 \
3773 vaf.fmt = fmt; \
3774 vaf.va = &args; \
3775 \
Joe Perchesd1f10522014-12-25 15:07:04 -08003776 __dev_printk(kern_level, dev, &vaf); \
Joe Perches798efc62012-09-12 20:11:29 -07003777 \
Joe Perches99bcf212010-06-27 01:02:34 +00003778 va_end(args); \
Joe Perches99bcf212010-06-27 01:02:34 +00003779} \
3780EXPORT_SYMBOL(func);
3781
Joe Perches663336e2018-05-09 08:15:46 -07003782define_dev_printk_level(_dev_emerg, KERN_EMERG);
3783define_dev_printk_level(_dev_alert, KERN_ALERT);
3784define_dev_printk_level(_dev_crit, KERN_CRIT);
3785define_dev_printk_level(_dev_err, KERN_ERR);
3786define_dev_printk_level(_dev_warn, KERN_WARNING);
3787define_dev_printk_level(_dev_notice, KERN_NOTICE);
Joe Perches99bcf212010-06-27 01:02:34 +00003788define_dev_printk_level(_dev_info, KERN_INFO);
3789
3790#endif
Rafael J. Wysocki97badf82015-04-03 23:23:37 +02003791
3792static inline bool fwnode_is_primary(struct fwnode_handle *fwnode)
3793{
3794 return fwnode && !IS_ERR(fwnode->secondary);
3795}
3796
3797/**
3798 * set_primary_fwnode - Change the primary firmware node of a given device.
3799 * @dev: Device to handle.
3800 * @fwnode: New primary firmware node of the device.
3801 *
3802 * Set the device's firmware node pointer to @fwnode, but if a secondary
3803 * firmware node of the device is present, preserve it.
3804 */
3805void set_primary_fwnode(struct device *dev, struct fwnode_handle *fwnode)
3806{
3807 if (fwnode) {
3808 struct fwnode_handle *fn = dev->fwnode;
3809
3810 if (fwnode_is_primary(fn))
3811 fn = fn->secondary;
3812
Mika Westerberg55f89a82015-11-30 17:11:39 +02003813 if (fn) {
3814 WARN_ON(fwnode->secondary);
3815 fwnode->secondary = fn;
3816 }
Rafael J. Wysocki97badf82015-04-03 23:23:37 +02003817 dev->fwnode = fwnode;
3818 } else {
3819 dev->fwnode = fwnode_is_primary(dev->fwnode) ?
3820 dev->fwnode->secondary : NULL;
3821 }
3822}
3823EXPORT_SYMBOL_GPL(set_primary_fwnode);
3824
3825/**
3826 * set_secondary_fwnode - Change the secondary firmware node of a given device.
3827 * @dev: Device to handle.
3828 * @fwnode: New secondary firmware node of the device.
3829 *
3830 * If a primary firmware node of the device is present, set its secondary
3831 * pointer to @fwnode. Otherwise, set the device's firmware node pointer to
3832 * @fwnode.
3833 */
3834void set_secondary_fwnode(struct device *dev, struct fwnode_handle *fwnode)
3835{
3836 if (fwnode)
3837 fwnode->secondary = ERR_PTR(-ENODEV);
3838
3839 if (fwnode_is_primary(dev->fwnode))
3840 dev->fwnode->secondary = fwnode;
3841 else
3842 dev->fwnode = fwnode;
3843}
Johan Hovold4e75e1d2017-06-06 17:59:00 +02003844
3845/**
3846 * device_set_of_node_from_dev - reuse device-tree node of another device
3847 * @dev: device whose device-tree node is being set
3848 * @dev2: device whose device-tree node is being reused
3849 *
3850 * Takes another reference to the new device-tree node after first dropping
3851 * any reference held to the old node.
3852 */
3853void device_set_of_node_from_dev(struct device *dev, const struct device *dev2)
3854{
3855 of_node_put(dev->of_node);
3856 dev->of_node = of_node_get(dev2->of_node);
3857 dev->of_node_reused = true;
3858}
3859EXPORT_SYMBOL_GPL(device_set_of_node_from_dev);
Suzuki K Poulose65b66682019-06-14 18:54:01 +01003860
Suzuki K Poulose6cda08a2019-07-23 23:18:32 +01003861int device_match_name(struct device *dev, const void *name)
3862{
3863 return sysfs_streq(dev_name(dev), name);
3864}
3865EXPORT_SYMBOL_GPL(device_match_name);
3866
Suzuki K Poulose65b66682019-06-14 18:54:01 +01003867int device_match_of_node(struct device *dev, const void *np)
3868{
3869 return dev->of_node == np;
3870}
3871EXPORT_SYMBOL_GPL(device_match_of_node);
Suzuki K Poulose67843bb2019-07-23 23:18:34 +01003872
3873int device_match_fwnode(struct device *dev, const void *fwnode)
3874{
3875 return dev_fwnode(dev) == fwnode;
3876}
3877EXPORT_SYMBOL_GPL(device_match_fwnode);
Suzuki K Poulose4495dfd2019-07-23 23:18:35 +01003878
3879int device_match_devt(struct device *dev, const void *pdevt)
3880{
3881 return dev->devt == *(dev_t *)pdevt;
3882}
3883EXPORT_SYMBOL_GPL(device_match_devt);
Suzuki K Poulose00500142019-07-23 23:18:36 +01003884
3885int device_match_acpi_dev(struct device *dev, const void *adev)
3886{
3887 return ACPI_COMPANION(dev) == adev;
3888}
3889EXPORT_SYMBOL(device_match_acpi_dev);
Suzuki K Poulose6bf85ba2019-07-23 23:18:37 +01003890
3891int device_match_any(struct device *dev, const void *unused)
3892{
3893 return 1;
3894}
3895EXPORT_SYMBOL_GPL(device_match_any);