blob: 7bb957b11861190bffdfa3bd32a78fc8b271f6db [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>
Oliver Neukumb8530012020-09-16 21:15:44 +020029#include <linux/sched/mm.h>
Claire Chang69031f52021-06-19 11:40:34 +080030#include <linux/swiotlb.h>
Greg Kroah-Hartman63967682013-08-27 10:24:15 -070031#include <linux/sysfs.h>
Christoph Hellwig6d4e9a82021-02-10 10:56:39 +010032#include <linux/dma-map-ops.h> /* for dma_default_coherent */
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
34#include "base.h"
35#include "power/power.h"
36
Andi Kleene52eec12010-09-08 16:54:17 +020037#ifdef CONFIG_SYSFS_DEPRECATED
38#ifdef CONFIG_SYSFS_DEPRECATED_V2
39long sysfs_deprecated = 1;
40#else
41long sysfs_deprecated = 0;
42#endif
Hanjun Guo3454bf92013-08-17 20:42:24 +080043static int __init sysfs_deprecated_setup(char *arg)
Andi Kleene52eec12010-09-08 16:54:17 +020044{
Jingoo Han34da5e62013-07-26 13:10:22 +090045 return kstrtol(arg, 10, &sysfs_deprecated);
Andi Kleene52eec12010-09-08 16:54:17 +020046}
47early_param("sysfs.deprecated", sysfs_deprecated_setup);
48#endif
49
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +010050/* Device links support. */
Saravana Kannanfc5a2512019-09-04 14:11:23 -070051static LIST_HEAD(deferred_sync);
52static unsigned int defer_sync_state_count = 1;
Saravana Kannan7b337cb2020-11-20 18:02:23 -080053static DEFINE_MUTEX(fwnode_link_lock);
Saravana Kannan25ac86c2020-11-20 18:02:28 -080054static bool fw_devlink_is_permissive(void);
Saravana Kannand46f3e32021-04-01 21:03:41 -070055static bool fw_devlink_drv_reg_done;
Saravana Kannan7b337cb2020-11-20 18:02:23 -080056
57/**
58 * fwnode_link_add - Create a link between two fwnode_handles.
59 * @con: Consumer end of the link.
60 * @sup: Supplier end of the link.
61 *
62 * Create a fwnode link between fwnode handles @con and @sup. The fwnode link
63 * represents the detail that the firmware lists @sup fwnode as supplying a
64 * resource to @con.
65 *
66 * The driver core will use the fwnode link to create a device link between the
67 * two device objects corresponding to @con and @sup when they are created. The
68 * driver core will automatically delete the fwnode link between @con and @sup
69 * after doing that.
70 *
71 * Attempts to create duplicate links between the same pair of fwnode handles
72 * are ignored and there is no reference counting.
73 */
74int fwnode_link_add(struct fwnode_handle *con, struct fwnode_handle *sup)
75{
76 struct fwnode_link *link;
77 int ret = 0;
78
79 mutex_lock(&fwnode_link_lock);
80
81 list_for_each_entry(link, &sup->consumers, s_hook)
82 if (link->consumer == con)
83 goto out;
84
85 link = kzalloc(sizeof(*link), GFP_KERNEL);
86 if (!link) {
87 ret = -ENOMEM;
88 goto out;
89 }
90
91 link->supplier = sup;
92 INIT_LIST_HEAD(&link->s_hook);
93 link->consumer = con;
94 INIT_LIST_HEAD(&link->c_hook);
95
96 list_add(&link->s_hook, &sup->consumers);
97 list_add(&link->c_hook, &con->suppliers);
Saravana Kannanebd68232021-09-15 10:28:07 -070098 pr_debug("%pfwP Linked as a fwnode consumer to %pfwP\n",
99 con, sup);
Saravana Kannan7b337cb2020-11-20 18:02:23 -0800100out:
101 mutex_unlock(&fwnode_link_lock);
102
103 return ret;
104}
105
106/**
Saravana Kannan76f13082021-09-15 10:28:06 -0700107 * __fwnode_link_del - Delete a link between two fwnode_handles.
108 * @link: the fwnode_link to be deleted
109 *
110 * The fwnode_link_lock needs to be held when this function is called.
111 */
112static void __fwnode_link_del(struct fwnode_link *link)
113{
Saravana Kannanebd68232021-09-15 10:28:07 -0700114 pr_debug("%pfwP Dropping the fwnode link to %pfwP\n",
115 link->consumer, link->supplier);
Saravana Kannan76f13082021-09-15 10:28:06 -0700116 list_del(&link->s_hook);
117 list_del(&link->c_hook);
118 kfree(link);
119}
120
121/**
Saravana Kannan7b337cb2020-11-20 18:02:23 -0800122 * fwnode_links_purge_suppliers - Delete all supplier links of fwnode_handle.
123 * @fwnode: fwnode whose supplier links need to be deleted
124 *
125 * Deletes all supplier links connecting directly to @fwnode.
126 */
127static void fwnode_links_purge_suppliers(struct fwnode_handle *fwnode)
128{
129 struct fwnode_link *link, *tmp;
130
131 mutex_lock(&fwnode_link_lock);
Saravana Kannan76f13082021-09-15 10:28:06 -0700132 list_for_each_entry_safe(link, tmp, &fwnode->suppliers, c_hook)
133 __fwnode_link_del(link);
Saravana Kannan7b337cb2020-11-20 18:02:23 -0800134 mutex_unlock(&fwnode_link_lock);
135}
136
137/**
138 * fwnode_links_purge_consumers - Delete all consumer links of fwnode_handle.
139 * @fwnode: fwnode whose consumer links need to be deleted
140 *
141 * Deletes all consumer links connecting directly to @fwnode.
142 */
143static void fwnode_links_purge_consumers(struct fwnode_handle *fwnode)
144{
145 struct fwnode_link *link, *tmp;
146
147 mutex_lock(&fwnode_link_lock);
Saravana Kannan76f13082021-09-15 10:28:06 -0700148 list_for_each_entry_safe(link, tmp, &fwnode->consumers, s_hook)
149 __fwnode_link_del(link);
Saravana Kannan7b337cb2020-11-20 18:02:23 -0800150 mutex_unlock(&fwnode_link_lock);
151}
152
153/**
154 * fwnode_links_purge - Delete all links connected to a fwnode_handle.
155 * @fwnode: fwnode whose links needs to be deleted
156 *
157 * Deletes all links connecting directly to a fwnode.
158 */
159void fwnode_links_purge(struct fwnode_handle *fwnode)
160{
161 fwnode_links_purge_suppliers(fwnode);
162 fwnode_links_purge_consumers(fwnode);
163}
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100164
Saravana Kannan28ec3442021-05-05 17:44:22 -0700165void fw_devlink_purge_absent_suppliers(struct fwnode_handle *fwnode)
Saravana Kannan9528e0d2021-02-05 14:26:37 -0800166{
167 struct fwnode_handle *child;
168
169 /* Don't purge consumer links of an added child */
170 if (fwnode->dev)
171 return;
172
173 fwnode->flags |= FWNODE_FLAG_NOT_DEVICE;
174 fwnode_links_purge_consumers(fwnode);
175
176 fwnode_for_each_available_child_node(fwnode, child)
177 fw_devlink_purge_absent_suppliers(child);
178}
Saravana Kannan28ec3442021-05-05 17:44:22 -0700179EXPORT_SYMBOL_GPL(fw_devlink_purge_absent_suppliers);
Saravana Kannan9528e0d2021-02-05 14:26:37 -0800180
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100181#ifdef CONFIG_SRCU
182static DEFINE_MUTEX(device_links_lock);
183DEFINE_STATIC_SRCU(device_links_srcu);
184
185static inline void device_links_write_lock(void)
186{
187 mutex_lock(&device_links_lock);
188}
189
190static inline void device_links_write_unlock(void)
191{
192 mutex_unlock(&device_links_lock);
193}
194
Jules Irenge68464d72020-02-14 20:47:29 +0000195int device_links_read_lock(void) __acquires(&device_links_srcu)
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100196{
197 return srcu_read_lock(&device_links_srcu);
198}
199
Jules Irengeab7789c2020-02-14 20:47:30 +0000200void device_links_read_unlock(int idx) __releases(&device_links_srcu)
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100201{
202 srcu_read_unlock(&device_links_srcu, idx);
203}
Joel Fernandes (Google)c2fa1e12019-07-16 18:12:25 -0400204
205int device_links_read_lock_held(void)
206{
207 return srcu_read_lock_held(&device_links_srcu);
208}
Rafael J. Wysocki80dd33c2021-05-14 14:10:15 +0200209
210static void device_link_synchronize_removal(void)
211{
212 synchronize_srcu(&device_links_srcu);
213}
Rafael J. Wysocki0c871312021-05-14 14:11:19 +0200214
215static void device_link_remove_from_lists(struct device_link *link)
216{
217 list_del_rcu(&link->s_node);
218 list_del_rcu(&link->c_node);
219}
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100220#else /* !CONFIG_SRCU */
221static DECLARE_RWSEM(device_links_lock);
222
223static inline void device_links_write_lock(void)
224{
225 down_write(&device_links_lock);
226}
227
228static inline void device_links_write_unlock(void)
229{
230 up_write(&device_links_lock);
231}
232
233int device_links_read_lock(void)
234{
235 down_read(&device_links_lock);
236 return 0;
237}
238
239void device_links_read_unlock(int not_used)
240{
241 up_read(&device_links_lock);
242}
Joel Fernandes (Google)c2fa1e12019-07-16 18:12:25 -0400243
244#ifdef CONFIG_DEBUG_LOCK_ALLOC
245int device_links_read_lock_held(void)
246{
247 return lockdep_is_held(&device_links_lock);
248}
249#endif
Rafael J. Wysocki80dd33c2021-05-14 14:10:15 +0200250
251static inline void device_link_synchronize_removal(void)
252{
253}
Rafael J. Wysocki0c871312021-05-14 14:11:19 +0200254
255static void device_link_remove_from_lists(struct device_link *link)
256{
257 list_del(&link->s_node);
258 list_del(&link->c_node);
259}
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100260#endif /* !CONFIG_SRCU */
261
Rafael J. Wysocki3d1cf432021-01-15 19:30:51 +0100262static bool device_is_ancestor(struct device *dev, struct device *target)
263{
264 while (target->parent) {
265 target = target->parent;
266 if (dev == target)
267 return true;
268 }
269 return false;
270}
271
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100272/**
273 * device_is_dependent - Check if one device depends on another one
274 * @dev: Device to check dependencies for.
275 * @target: Device to check against.
276 *
277 * Check if @target depends on @dev or any device dependent on it (its child or
278 * its consumer etc). Return 1 if that is the case or 0 otherwise.
279 */
Saravana Kannan7d34ca32020-06-09 18:19:33 -0700280int device_is_dependent(struct device *dev, void *target)
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100281{
282 struct device_link *link;
283 int ret;
284
Rafael J. Wysocki3d1cf432021-01-15 19:30:51 +0100285 /*
286 * The "ancestors" check is needed to catch the case when the target
287 * device has not been completely initialized yet and it is still
288 * missing from the list of children of its parent device.
289 */
290 if (dev == target || device_is_ancestor(dev, target))
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100291 return 1;
292
293 ret = device_for_each_child(dev, target, device_is_dependent);
294 if (ret)
295 return ret;
296
297 list_for_each_entry(link, &dev->links.consumers, s_node) {
Saravana Kannan4b9bbb22020-12-17 19:17:00 -0800298 if ((link->flags & ~DL_FLAG_INFERRED) ==
299 (DL_FLAG_SYNC_STATE_ONLY | DL_FLAG_MANAGED))
Saravana Kannan05ef9832019-10-28 15:00:22 -0700300 continue;
301
Benjamin Gaignarde16f4f32018-07-16 13:37:44 +0200302 if (link->consumer == target)
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100303 return 1;
304
305 ret = device_is_dependent(link->consumer, target);
306 if (ret)
307 break;
308 }
309 return ret;
310}
311
Rafael J. Wysocki515db262019-07-16 17:21:06 +0200312static void device_link_init_status(struct device_link *link,
313 struct device *consumer,
314 struct device *supplier)
315{
316 switch (supplier->links.status) {
317 case DL_DEV_PROBING:
318 switch (consumer->links.status) {
319 case DL_DEV_PROBING:
320 /*
321 * A consumer driver can create a link to a supplier
322 * that has not completed its probing yet as long as it
323 * knows that the supplier is already functional (for
324 * example, it has just acquired some resources from the
325 * supplier).
326 */
327 link->status = DL_STATE_CONSUMER_PROBE;
328 break;
329 default:
330 link->status = DL_STATE_DORMANT;
331 break;
332 }
333 break;
334 case DL_DEV_DRIVER_BOUND:
335 switch (consumer->links.status) {
336 case DL_DEV_PROBING:
337 link->status = DL_STATE_CONSUMER_PROBE;
338 break;
339 case DL_DEV_DRIVER_BOUND:
340 link->status = DL_STATE_ACTIVE;
341 break;
342 default:
343 link->status = DL_STATE_AVAILABLE;
344 break;
345 }
346 break;
347 case DL_DEV_UNBINDING:
348 link->status = DL_STATE_SUPPLIER_UNBIND;
349 break;
350 default:
351 link->status = DL_STATE_DORMANT;
352 break;
353 }
354}
355
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100356static int device_reorder_to_tail(struct device *dev, void *not_used)
357{
358 struct device_link *link;
359
360 /*
361 * Devices that have not been registered yet will be put to the ends
362 * of the lists during the registration, so skip them here.
363 */
364 if (device_is_registered(dev))
365 devices_kset_move_last(dev);
366
367 if (device_pm_initialized(dev))
368 device_pm_move_last(dev);
369
370 device_for_each_child(dev, NULL, device_reorder_to_tail);
Saravana Kannan05ef9832019-10-28 15:00:22 -0700371 list_for_each_entry(link, &dev->links.consumers, s_node) {
Saravana Kannan4b9bbb22020-12-17 19:17:00 -0800372 if ((link->flags & ~DL_FLAG_INFERRED) ==
373 (DL_FLAG_SYNC_STATE_ONLY | DL_FLAG_MANAGED))
Saravana Kannan05ef9832019-10-28 15:00:22 -0700374 continue;
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100375 device_reorder_to_tail(link->consumer, NULL);
Saravana Kannan05ef9832019-10-28 15:00:22 -0700376 }
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100377
378 return 0;
379}
380
381/**
Feng Kan494fd7b2018-04-10 16:57:06 -0700382 * device_pm_move_to_tail - Move set of devices to the end of device lists
383 * @dev: Device to move
384 *
385 * This is a device_reorder_to_tail() wrapper taking the requisite locks.
386 *
387 * It moves the @dev along with all of its children and all of its consumers
388 * to the ends of the device_kset and dpm_list, recursively.
389 */
390void device_pm_move_to_tail(struct device *dev)
391{
392 int idx;
393
394 idx = device_links_read_lock();
395 device_pm_lock();
396 device_reorder_to_tail(dev, NULL);
397 device_pm_unlock();
398 device_links_read_unlock(idx);
399}
400
Saravana Kannan287905e2020-05-21 12:17:58 -0700401#define to_devlink(dev) container_of((dev), struct device_link, link_dev)
402
403static ssize_t status_show(struct device *dev,
Joe Perches948b3ed2020-09-16 13:40:42 -0700404 struct device_attribute *attr, char *buf)
Saravana Kannan287905e2020-05-21 12:17:58 -0700405{
Joe Perches948b3ed2020-09-16 13:40:42 -0700406 const char *output;
Saravana Kannan287905e2020-05-21 12:17:58 -0700407
408 switch (to_devlink(dev)->status) {
409 case DL_STATE_NONE:
Joe Perches948b3ed2020-09-16 13:40:42 -0700410 output = "not tracked";
411 break;
Saravana Kannan287905e2020-05-21 12:17:58 -0700412 case DL_STATE_DORMANT:
Joe Perches948b3ed2020-09-16 13:40:42 -0700413 output = "dormant";
414 break;
Saravana Kannan287905e2020-05-21 12:17:58 -0700415 case DL_STATE_AVAILABLE:
Joe Perches948b3ed2020-09-16 13:40:42 -0700416 output = "available";
417 break;
Saravana Kannan287905e2020-05-21 12:17:58 -0700418 case DL_STATE_CONSUMER_PROBE:
Joe Perches948b3ed2020-09-16 13:40:42 -0700419 output = "consumer probing";
420 break;
Saravana Kannan287905e2020-05-21 12:17:58 -0700421 case DL_STATE_ACTIVE:
Joe Perches948b3ed2020-09-16 13:40:42 -0700422 output = "active";
423 break;
Saravana Kannan287905e2020-05-21 12:17:58 -0700424 case DL_STATE_SUPPLIER_UNBIND:
Joe Perches948b3ed2020-09-16 13:40:42 -0700425 output = "supplier unbinding";
426 break;
Saravana Kannan287905e2020-05-21 12:17:58 -0700427 default:
Joe Perches948b3ed2020-09-16 13:40:42 -0700428 output = "unknown";
429 break;
Saravana Kannan287905e2020-05-21 12:17:58 -0700430 }
Joe Perches948b3ed2020-09-16 13:40:42 -0700431
432 return sysfs_emit(buf, "%s\n", output);
Saravana Kannan287905e2020-05-21 12:17:58 -0700433}
434static DEVICE_ATTR_RO(status);
435
436static ssize_t auto_remove_on_show(struct device *dev,
437 struct device_attribute *attr, char *buf)
438{
439 struct device_link *link = to_devlink(dev);
Joe Perches973c3912020-09-16 13:40:40 -0700440 const char *output;
Saravana Kannan287905e2020-05-21 12:17:58 -0700441
442 if (link->flags & DL_FLAG_AUTOREMOVE_SUPPLIER)
Joe Perches973c3912020-09-16 13:40:40 -0700443 output = "supplier unbind";
Saravana Kannan287905e2020-05-21 12:17:58 -0700444 else if (link->flags & DL_FLAG_AUTOREMOVE_CONSUMER)
Joe Perches973c3912020-09-16 13:40:40 -0700445 output = "consumer unbind";
Saravana Kannan287905e2020-05-21 12:17:58 -0700446 else
Joe Perches973c3912020-09-16 13:40:40 -0700447 output = "never";
Saravana Kannan287905e2020-05-21 12:17:58 -0700448
Joe Perches973c3912020-09-16 13:40:40 -0700449 return sysfs_emit(buf, "%s\n", output);
Saravana Kannan287905e2020-05-21 12:17:58 -0700450}
451static DEVICE_ATTR_RO(auto_remove_on);
452
453static ssize_t runtime_pm_show(struct device *dev,
454 struct device_attribute *attr, char *buf)
455{
456 struct device_link *link = to_devlink(dev);
457
Joe Perchesaa838892020-09-16 13:40:39 -0700458 return sysfs_emit(buf, "%d\n", !!(link->flags & DL_FLAG_PM_RUNTIME));
Saravana Kannan287905e2020-05-21 12:17:58 -0700459}
460static DEVICE_ATTR_RO(runtime_pm);
461
462static ssize_t sync_state_only_show(struct device *dev,
463 struct device_attribute *attr, char *buf)
464{
465 struct device_link *link = to_devlink(dev);
466
Joe Perchesaa838892020-09-16 13:40:39 -0700467 return sysfs_emit(buf, "%d\n",
468 !!(link->flags & DL_FLAG_SYNC_STATE_ONLY));
Saravana Kannan287905e2020-05-21 12:17:58 -0700469}
470static DEVICE_ATTR_RO(sync_state_only);
471
472static struct attribute *devlink_attrs[] = {
473 &dev_attr_status.attr,
474 &dev_attr_auto_remove_on.attr,
475 &dev_attr_runtime_pm.attr,
476 &dev_attr_sync_state_only.attr,
477 NULL,
478};
479ATTRIBUTE_GROUPS(devlink);
480
Rafael J. Wysocki80dd33c2021-05-14 14:10:15 +0200481static void device_link_release_fn(struct work_struct *work)
Saravana Kannan843e6002020-07-16 14:45:23 -0700482{
Rafael J. Wysocki80dd33c2021-05-14 14:10:15 +0200483 struct device_link *link = container_of(work, struct device_link, rm_work);
484
485 /* Ensure that all references to the link object have been dropped. */
486 device_link_synchronize_removal();
487
Rafael J. Wysockid1579e62021-12-10 17:10:13 +0100488 pm_runtime_release_supplier(link, true);
Saravana Kannan843e6002020-07-16 14:45:23 -0700489
490 put_device(link->consumer);
491 put_device(link->supplier);
492 kfree(link);
493}
494
Saravana Kannan287905e2020-05-21 12:17:58 -0700495static void devlink_dev_release(struct device *dev)
496{
Saravana Kannan843e6002020-07-16 14:45:23 -0700497 struct device_link *link = to_devlink(dev);
498
Rafael J. Wysocki80dd33c2021-05-14 14:10:15 +0200499 INIT_WORK(&link->rm_work, device_link_release_fn);
500 /*
501 * It may take a while to complete this work because of the SRCU
502 * synchronization in device_link_release_fn() and if the consumer or
503 * supplier devices get deleted when it runs, so put it into the "long"
504 * workqueue.
505 */
506 queue_work(system_long_wq, &link->rm_work);
Saravana Kannan287905e2020-05-21 12:17:58 -0700507}
508
509static struct class devlink_class = {
510 .name = "devlink",
511 .owner = THIS_MODULE,
512 .dev_groups = devlink_groups,
513 .dev_release = devlink_dev_release,
514};
515
516static int devlink_add_symlinks(struct device *dev,
517 struct class_interface *class_intf)
518{
519 int ret;
520 size_t len;
521 struct device_link *link = to_devlink(dev);
522 struct device *sup = link->supplier;
523 struct device *con = link->consumer;
524 char *buf;
525
Saravana Kannane020ff62021-01-10 09:54:07 -0800526 len = max(strlen(dev_bus_name(sup)) + strlen(dev_name(sup)),
527 strlen(dev_bus_name(con)) + strlen(dev_name(con)));
528 len += strlen(":");
Saravana Kannan287905e2020-05-21 12:17:58 -0700529 len += strlen("supplier:") + 1;
530 buf = kzalloc(len, GFP_KERNEL);
531 if (!buf)
532 return -ENOMEM;
533
534 ret = sysfs_create_link(&link->link_dev.kobj, &sup->kobj, "supplier");
535 if (ret)
536 goto out;
537
538 ret = sysfs_create_link(&link->link_dev.kobj, &con->kobj, "consumer");
539 if (ret)
540 goto err_con;
541
Saravana Kannane020ff62021-01-10 09:54:07 -0800542 snprintf(buf, len, "consumer:%s:%s", dev_bus_name(con), dev_name(con));
Saravana Kannan287905e2020-05-21 12:17:58 -0700543 ret = sysfs_create_link(&sup->kobj, &link->link_dev.kobj, buf);
544 if (ret)
545 goto err_con_dev;
546
Saravana Kannane020ff62021-01-10 09:54:07 -0800547 snprintf(buf, len, "supplier:%s:%s", dev_bus_name(sup), dev_name(sup));
Saravana Kannan287905e2020-05-21 12:17:58 -0700548 ret = sysfs_create_link(&con->kobj, &link->link_dev.kobj, buf);
549 if (ret)
550 goto err_sup_dev;
551
552 goto out;
553
554err_sup_dev:
Saravana Kannane020ff62021-01-10 09:54:07 -0800555 snprintf(buf, len, "consumer:%s:%s", dev_bus_name(con), dev_name(con));
Saravana Kannan287905e2020-05-21 12:17:58 -0700556 sysfs_remove_link(&sup->kobj, buf);
557err_con_dev:
558 sysfs_remove_link(&link->link_dev.kobj, "consumer");
559err_con:
560 sysfs_remove_link(&link->link_dev.kobj, "supplier");
561out:
562 kfree(buf);
563 return ret;
564}
565
566static void devlink_remove_symlinks(struct device *dev,
567 struct class_interface *class_intf)
568{
569 struct device_link *link = to_devlink(dev);
570 size_t len;
571 struct device *sup = link->supplier;
572 struct device *con = link->consumer;
573 char *buf;
574
575 sysfs_remove_link(&link->link_dev.kobj, "consumer");
576 sysfs_remove_link(&link->link_dev.kobj, "supplier");
577
Saravana Kannane020ff62021-01-10 09:54:07 -0800578 len = max(strlen(dev_bus_name(sup)) + strlen(dev_name(sup)),
579 strlen(dev_bus_name(con)) + strlen(dev_name(con)));
580 len += strlen(":");
Saravana Kannan287905e2020-05-21 12:17:58 -0700581 len += strlen("supplier:") + 1;
582 buf = kzalloc(len, GFP_KERNEL);
583 if (!buf) {
584 WARN(1, "Unable to properly free device link symlinks!\n");
585 return;
586 }
587
Adrian Huntere64daad2021-07-16 14:44:07 +0300588 if (device_is_registered(con)) {
589 snprintf(buf, len, "supplier:%s:%s", dev_bus_name(sup), dev_name(sup));
590 sysfs_remove_link(&con->kobj, buf);
591 }
Saravana Kannane020ff62021-01-10 09:54:07 -0800592 snprintf(buf, len, "consumer:%s:%s", dev_bus_name(con), dev_name(con));
Saravana Kannan287905e2020-05-21 12:17:58 -0700593 sysfs_remove_link(&sup->kobj, buf);
594 kfree(buf);
595}
596
597static struct class_interface devlink_class_intf = {
598 .class = &devlink_class,
599 .add_dev = devlink_add_symlinks,
600 .remove_dev = devlink_remove_symlinks,
601};
602
603static int __init devlink_class_init(void)
604{
605 int ret;
606
607 ret = class_register(&devlink_class);
608 if (ret)
609 return ret;
610
611 ret = class_interface_register(&devlink_class_intf);
612 if (ret)
613 class_unregister(&devlink_class);
614
615 return ret;
616}
617postcore_initcall(devlink_class_init);
618
Rafael J. Wysocki515db262019-07-16 17:21:06 +0200619#define DL_MANAGED_LINK_FLAGS (DL_FLAG_AUTOREMOVE_CONSUMER | \
620 DL_FLAG_AUTOREMOVE_SUPPLIER | \
Saravana Kannan05ef9832019-10-28 15:00:22 -0700621 DL_FLAG_AUTOPROBE_CONSUMER | \
Saravana Kannan4b9bbb22020-12-17 19:17:00 -0800622 DL_FLAG_SYNC_STATE_ONLY | \
623 DL_FLAG_INFERRED)
Rafael J. Wysocki515db262019-07-16 17:21:06 +0200624
Rafael J. Wysockifb583c82019-07-30 11:28:57 +0200625#define DL_ADD_VALID_FLAGS (DL_MANAGED_LINK_FLAGS | DL_FLAG_STATELESS | \
626 DL_FLAG_PM_RUNTIME | DL_FLAG_RPM_ACTIVE)
627
Feng Kan494fd7b2018-04-10 16:57:06 -0700628/**
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100629 * device_link_add - Create a link between two devices.
630 * @consumer: Consumer end of the link.
631 * @supplier: Supplier end of the link.
632 * @flags: Link flags.
633 *
Rafael J. Wysocki21d5c572016-10-30 17:32:31 +0100634 * The caller is responsible for the proper synchronization of the link creation
635 * with runtime PM. First, setting the DL_FLAG_PM_RUNTIME flag will cause the
636 * runtime PM framework to take the link into account. Second, if the
637 * DL_FLAG_RPM_ACTIVE flag is set in addition to it, the supplier devices will
Thierry Redingd475f8e2020-11-27 11:46:30 +0100638 * be forced into the active meta state and reference-counted upon the creation
Rafael J. Wysocki21d5c572016-10-30 17:32:31 +0100639 * of the link. If DL_FLAG_PM_RUNTIME is not set, DL_FLAG_RPM_ACTIVE will be
640 * ignored.
641 *
Rafael J. Wysocki515db262019-07-16 17:21:06 +0200642 * If DL_FLAG_STATELESS is set in @flags, the caller of this function is
643 * expected to release the link returned by it directly with the help of either
644 * device_link_del() or device_link_remove().
Rafael J. Wysocki72175d42019-02-01 01:58:33 +0100645 *
646 * If that flag is not set, however, the caller of this function is handing the
647 * management of the link over to the driver core entirely and its return value
648 * can only be used to check whether or not the link is present. In that case,
649 * the DL_FLAG_AUTOREMOVE_CONSUMER and DL_FLAG_AUTOREMOVE_SUPPLIER device link
650 * flags can be used to indicate to the driver core when the link can be safely
651 * deleted. Namely, setting one of them in @flags indicates to the driver core
652 * that the link is not going to be used (by the given caller of this function)
653 * after unbinding the consumer or supplier driver, respectively, from its
654 * device, so the link can be deleted at that point. If none of them is set,
655 * the link will be maintained until one of the devices pointed to by it (either
656 * the consumer or the supplier) is unregistered.
Rafael J. Wysockic8d50982019-02-01 01:45:55 +0100657 *
Rafael J. Wysockie7dd4012019-02-01 01:59:42 +0100658 * Also, if DL_FLAG_STATELESS, DL_FLAG_AUTOREMOVE_CONSUMER and
659 * DL_FLAG_AUTOREMOVE_SUPPLIER are not set in @flags (that is, a persistent
660 * managed device link is being added), the DL_FLAG_AUTOPROBE_CONSUMER flag can
Thierry Redingd475f8e2020-11-27 11:46:30 +0100661 * be used to request the driver core to automatically probe for a consumer
Rafael J. Wysockie7dd4012019-02-01 01:59:42 +0100662 * driver after successfully binding a driver to the supplier device.
663 *
Rafael J. Wysocki515db262019-07-16 17:21:06 +0200664 * The combination of DL_FLAG_STATELESS and one of DL_FLAG_AUTOREMOVE_CONSUMER,
665 * DL_FLAG_AUTOREMOVE_SUPPLIER, or DL_FLAG_AUTOPROBE_CONSUMER set in @flags at
666 * the same time is invalid and will cause NULL to be returned upfront.
667 * However, if a device link between the given @consumer and @supplier pair
668 * exists already when this function is called for them, the existing link will
669 * be returned regardless of its current type and status (the link's flags may
670 * be modified then). The caller of this function is then expected to treat
671 * the link as though it has just been created, so (in particular) if
672 * DL_FLAG_STATELESS was passed in @flags, the link needs to be released
673 * explicitly when not needed any more (as stated above).
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100674 *
675 * A side effect of the link creation is re-ordering of dpm_list and the
676 * devices_kset list by moving the consumer device and all devices depending
677 * on it to the ends of these lists (that does not happen to devices that have
678 * not been registered when this function is called).
679 *
680 * The supplier device is required to be registered when this function is called
681 * and NULL will be returned if that is not the case. The consumer device need
Lukas Wunner64df1142016-12-04 13:10:04 +0100682 * not be registered, however.
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100683 */
684struct device_link *device_link_add(struct device *consumer,
685 struct device *supplier, u32 flags)
686{
687 struct device_link *link;
688
Saravana Kannanf729a592021-09-29 12:05:49 -0700689 if (!consumer || !supplier || consumer == supplier ||
690 flags & ~DL_ADD_VALID_FLAGS ||
Rafael J. Wysocki515db262019-07-16 17:21:06 +0200691 (flags & DL_FLAG_STATELESS && flags & DL_MANAGED_LINK_FLAGS) ||
Saravana Kannan05ef9832019-10-28 15:00:22 -0700692 (flags & DL_FLAG_SYNC_STATE_ONLY &&
Saravana Kannan4b9bbb22020-12-17 19:17:00 -0800693 (flags & ~DL_FLAG_INFERRED) != DL_FLAG_SYNC_STATE_ONLY) ||
Rafael J. Wysockie7dd4012019-02-01 01:59:42 +0100694 (flags & DL_FLAG_AUTOPROBE_CONSUMER &&
695 flags & (DL_FLAG_AUTOREMOVE_CONSUMER |
696 DL_FLAG_AUTOREMOVE_SUPPLIER)))
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100697 return NULL;
698
Rafael J. Wysocki5db25c92019-02-01 01:47:53 +0100699 if (flags & DL_FLAG_PM_RUNTIME && flags & DL_FLAG_RPM_ACTIVE) {
700 if (pm_runtime_get_sync(supplier) < 0) {
701 pm_runtime_put_noidle(supplier);
702 return NULL;
703 }
Rafael J. Wysocki5db25c92019-02-01 01:47:53 +0100704 }
705
Rafael J. Wysocki515db262019-07-16 17:21:06 +0200706 if (!(flags & DL_FLAG_STATELESS))
707 flags |= DL_FLAG_MANAGED;
708
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100709 device_links_write_lock();
710 device_pm_lock();
711
712 /*
713 * If the supplier has not been fully registered yet or there is a
Saravana Kannan05ef9832019-10-28 15:00:22 -0700714 * reverse (non-SYNC_STATE_ONLY) dependency between the consumer and
715 * the supplier already in the graph, return NULL. If the link is a
716 * SYNC_STATE_ONLY link, we don't check for reverse dependencies
717 * because it only affects sync_state() callbacks.
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100718 */
719 if (!device_pm_initialized(supplier)
Saravana Kannan05ef9832019-10-28 15:00:22 -0700720 || (!(flags & DL_FLAG_SYNC_STATE_ONLY) &&
721 device_is_dependent(consumer, supplier))) {
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100722 link = NULL;
723 goto out;
724 }
725
Rafael J. Wysocki72175d42019-02-01 01:58:33 +0100726 /*
Saravana Kannanac66c5b2020-11-20 18:02:24 -0800727 * SYNC_STATE_ONLY links are useless once a consumer device has probed.
728 * So, only create it if the consumer hasn't probed yet.
729 */
730 if (flags & DL_FLAG_SYNC_STATE_ONLY &&
731 consumer->links.status != DL_DEV_NO_DRIVER &&
732 consumer->links.status != DL_DEV_PROBING) {
733 link = NULL;
734 goto out;
735 }
736
737 /*
Rafael J. Wysocki72175d42019-02-01 01:58:33 +0100738 * DL_FLAG_AUTOREMOVE_SUPPLIER indicates that the link will be needed
739 * longer than for DL_FLAG_AUTOREMOVE_CONSUMER and setting them both
740 * together doesn't make sense, so prefer DL_FLAG_AUTOREMOVE_SUPPLIER.
741 */
742 if (flags & DL_FLAG_AUTOREMOVE_SUPPLIER)
743 flags &= ~DL_FLAG_AUTOREMOVE_CONSUMER;
744
Rafael J. Wysockif265df52019-02-01 01:46:54 +0100745 list_for_each_entry(link, &supplier->links.consumers, s_node) {
746 if (link->consumer != consumer)
747 continue;
748
Saravana Kannan4b9bbb22020-12-17 19:17:00 -0800749 if (link->flags & DL_FLAG_INFERRED &&
750 !(flags & DL_FLAG_INFERRED))
751 link->flags &= ~DL_FLAG_INFERRED;
752
Rafael J. Wysockie2f3cd82019-02-01 01:49:14 +0100753 if (flags & DL_FLAG_PM_RUNTIME) {
754 if (!(link->flags & DL_FLAG_PM_RUNTIME)) {
Rafael J. Wysocki4c06c4e2019-02-12 13:08:10 +0100755 pm_runtime_new_link(consumer);
Rafael J. Wysockie2f3cd82019-02-01 01:49:14 +0100756 link->flags |= DL_FLAG_PM_RUNTIME;
757 }
758 if (flags & DL_FLAG_RPM_ACTIVE)
Rafael J. Wysocki36003d42019-02-19 17:53:26 +0100759 refcount_inc(&link->rpm_active);
Rafael J. Wysockie2f3cd82019-02-01 01:49:14 +0100760 }
761
Rafael J. Wysocki72175d42019-02-01 01:58:33 +0100762 if (flags & DL_FLAG_STATELESS) {
763 kref_get(&link->kref);
Saravana Kannan05ef9832019-10-28 15:00:22 -0700764 if (link->flags & DL_FLAG_SYNC_STATE_ONLY &&
Saravana Kannan44e96042020-05-19 21:36:26 -0700765 !(link->flags & DL_FLAG_STATELESS)) {
766 link->flags |= DL_FLAG_STATELESS;
Saravana Kannan05ef9832019-10-28 15:00:22 -0700767 goto reorder;
Saravana Kannan44e96042020-05-19 21:36:26 -0700768 } else {
769 link->flags |= DL_FLAG_STATELESS;
Saravana Kannan05ef9832019-10-28 15:00:22 -0700770 goto out;
Saravana Kannan44e96042020-05-19 21:36:26 -0700771 }
Rafael J. Wysocki72175d42019-02-01 01:58:33 +0100772 }
773
774 /*
775 * If the life time of the link following from the new flags is
776 * longer than indicated by the flags of the existing link,
777 * update the existing link to stay around longer.
778 */
779 if (flags & DL_FLAG_AUTOREMOVE_SUPPLIER) {
780 if (link->flags & DL_FLAG_AUTOREMOVE_CONSUMER) {
781 link->flags &= ~DL_FLAG_AUTOREMOVE_CONSUMER;
782 link->flags |= DL_FLAG_AUTOREMOVE_SUPPLIER;
783 }
784 } else if (!(flags & DL_FLAG_AUTOREMOVE_CONSUMER)) {
785 link->flags &= ~(DL_FLAG_AUTOREMOVE_CONSUMER |
786 DL_FLAG_AUTOREMOVE_SUPPLIER);
787 }
Rafael J. Wysocki515db262019-07-16 17:21:06 +0200788 if (!(link->flags & DL_FLAG_MANAGED)) {
789 kref_get(&link->kref);
790 link->flags |= DL_FLAG_MANAGED;
791 device_link_init_status(link, consumer, supplier);
792 }
Saravana Kannan05ef9832019-10-28 15:00:22 -0700793 if (link->flags & DL_FLAG_SYNC_STATE_ONLY &&
794 !(flags & DL_FLAG_SYNC_STATE_ONLY)) {
795 link->flags &= ~DL_FLAG_SYNC_STATE_ONLY;
796 goto reorder;
797 }
798
Rafael J. Wysockif265df52019-02-01 01:46:54 +0100799 goto out;
800 }
801
Rafael J. Wysocki21d5c572016-10-30 17:32:31 +0100802 link = kzalloc(sizeof(*link), GFP_KERNEL);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100803 if (!link)
804 goto out;
805
Rafael J. Wysockie2f3cd82019-02-01 01:49:14 +0100806 refcount_set(&link->rpm_active, 1);
807
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100808 get_device(supplier);
809 link->supplier = supplier;
810 INIT_LIST_HEAD(&link->s_node);
811 get_device(consumer);
812 link->consumer = consumer;
813 INIT_LIST_HEAD(&link->c_node);
814 link->flags = flags;
Lukas Wunneread18c22018-02-10 19:27:12 +0100815 kref_init(&link->kref);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100816
Saravana Kannan287905e2020-05-21 12:17:58 -0700817 link->link_dev.class = &devlink_class;
818 device_set_pm_not_required(&link->link_dev);
Saravana Kannane020ff62021-01-10 09:54:07 -0800819 dev_set_name(&link->link_dev, "%s:%s--%s:%s",
820 dev_bus_name(supplier), dev_name(supplier),
821 dev_bus_name(consumer), dev_name(consumer));
Saravana Kannan287905e2020-05-21 12:17:58 -0700822 if (device_register(&link->link_dev)) {
Yang Yingliangdf0a1812021-09-30 16:57:14 +0800823 put_device(&link->link_dev);
Saravana Kannan287905e2020-05-21 12:17:58 -0700824 link = NULL;
825 goto out;
826 }
827
828 if (flags & DL_FLAG_PM_RUNTIME) {
829 if (flags & DL_FLAG_RPM_ACTIVE)
830 refcount_inc(&link->rpm_active);
831
832 pm_runtime_new_link(consumer);
833 }
834
Lukas Wunner64df1142016-12-04 13:10:04 +0100835 /* Determine the initial link state. */
Rafael J. Wysocki515db262019-07-16 17:21:06 +0200836 if (flags & DL_FLAG_STATELESS)
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100837 link->status = DL_STATE_NONE;
Rafael J. Wysocki515db262019-07-16 17:21:06 +0200838 else
839 device_link_init_status(link, consumer, supplier);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100840
841 /*
Rafael J. Wysocki15cfb092019-02-01 01:50:39 +0100842 * Some callers expect the link creation during consumer driver probe to
843 * resume the supplier even without DL_FLAG_RPM_ACTIVE.
844 */
845 if (link->status == DL_STATE_CONSUMER_PROBE &&
846 flags & DL_FLAG_PM_RUNTIME)
847 pm_runtime_resume(supplier);
848
Saravana Kannan21c27f02020-05-18 23:30:00 -0700849 list_add_tail_rcu(&link->s_node, &supplier->links.consumers);
850 list_add_tail_rcu(&link->c_node, &consumer->links.suppliers);
851
Saravana Kannan05ef9832019-10-28 15:00:22 -0700852 if (flags & DL_FLAG_SYNC_STATE_ONLY) {
853 dev_dbg(consumer,
854 "Linked as a sync state only consumer to %s\n",
855 dev_name(supplier));
856 goto out;
857 }
Saravana Kannan21c27f02020-05-18 23:30:00 -0700858
Saravana Kannan05ef9832019-10-28 15:00:22 -0700859reorder:
Rafael J. Wysocki15cfb092019-02-01 01:50:39 +0100860 /*
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100861 * Move the consumer and all of the devices depending on it to the end
862 * of dpm_list and the devices_kset list.
863 *
864 * It is necessary to hold dpm_list locked throughout all that or else
865 * we may end up suspending with a wrong ordering of it.
866 */
867 device_reorder_to_tail(consumer, NULL);
868
Jerome Brunet8a4b3262018-12-21 17:23:41 +0100869 dev_dbg(consumer, "Linked as a consumer to %s\n", dev_name(supplier));
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100870
Saravana Kannan21c27f02020-05-18 23:30:00 -0700871out:
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100872 device_pm_unlock();
873 device_links_write_unlock();
Rafael J. Wysocki5db25c92019-02-01 01:47:53 +0100874
Rafael J. Wysockie2f3cd82019-02-01 01:49:14 +0100875 if ((flags & DL_FLAG_PM_RUNTIME && flags & DL_FLAG_RPM_ACTIVE) && !link)
Rafael J. Wysocki5db25c92019-02-01 01:47:53 +0100876 pm_runtime_put(supplier);
877
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100878 return link;
879}
880EXPORT_SYMBOL_GPL(device_link_add);
881
Lukas Wunneread18c22018-02-10 19:27:12 +0100882static void __device_link_del(struct kref *kref)
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100883{
Lukas Wunneread18c22018-02-10 19:27:12 +0100884 struct device_link *link = container_of(kref, struct device_link, kref);
885
Jerome Brunet8a4b3262018-12-21 17:23:41 +0100886 dev_dbg(link->consumer, "Dropping the link to %s\n",
887 dev_name(link->supplier));
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100888
Rafael J. Wysockie0e398e2020-10-21 21:12:15 +0200889 pm_runtime_drop_link(link);
Rafael J. Wysockibaa88092016-10-30 17:32:43 +0100890
Rafael J. Wysocki0c871312021-05-14 14:11:19 +0200891 device_link_remove_from_lists(link);
Saravana Kannan843e6002020-07-16 14:45:23 -0700892 device_unregister(&link->link_dev);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100893}
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100894
Rafael J. Wysocki72175d42019-02-01 01:58:33 +0100895static void device_link_put_kref(struct device_link *link)
896{
897 if (link->flags & DL_FLAG_STATELESS)
898 kref_put(&link->kref, __device_link_del);
Adrian Hunterbf259672021-08-06 16:04:41 +0300899 else if (!device_is_registered(link->consumer))
900 __device_link_del(&link->kref);
Rafael J. Wysocki72175d42019-02-01 01:58:33 +0100901 else
902 WARN(1, "Unable to drop a managed device link reference\n");
903}
904
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100905/**
Rafael J. Wysocki72175d42019-02-01 01:58:33 +0100906 * device_link_del - Delete a stateless link between two devices.
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100907 * @link: Device link to delete.
908 *
909 * The caller must ensure proper synchronization of this function with runtime
Lukas Wunneread18c22018-02-10 19:27:12 +0100910 * PM. If the link was added multiple times, it needs to be deleted as often.
911 * Care is required for hotplugged devices: Their links are purged on removal
912 * and calling device_link_del() is then no longer allowed.
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100913 */
914void device_link_del(struct device_link *link)
915{
916 device_links_write_lock();
Rafael J. Wysocki72175d42019-02-01 01:58:33 +0100917 device_link_put_kref(link);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100918 device_links_write_unlock();
919}
920EXPORT_SYMBOL_GPL(device_link_del);
921
pascal pailletd8842212018-07-05 14:25:56 +0000922/**
Rafael J. Wysocki72175d42019-02-01 01:58:33 +0100923 * device_link_remove - Delete a stateless link between two devices.
pascal pailletd8842212018-07-05 14:25:56 +0000924 * @consumer: Consumer end of the link.
925 * @supplier: Supplier end of the link.
926 *
927 * The caller must ensure proper synchronization of this function with runtime
928 * PM.
929 */
930void device_link_remove(void *consumer, struct device *supplier)
931{
932 struct device_link *link;
933
934 if (WARN_ON(consumer == supplier))
935 return;
936
937 device_links_write_lock();
pascal pailletd8842212018-07-05 14:25:56 +0000938
939 list_for_each_entry(link, &supplier->links.consumers, s_node) {
940 if (link->consumer == consumer) {
Rafael J. Wysocki72175d42019-02-01 01:58:33 +0100941 device_link_put_kref(link);
pascal pailletd8842212018-07-05 14:25:56 +0000942 break;
943 }
944 }
945
pascal pailletd8842212018-07-05 14:25:56 +0000946 device_links_write_unlock();
947}
948EXPORT_SYMBOL_GPL(device_link_remove);
949
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100950static void device_links_missing_supplier(struct device *dev)
951{
952 struct device_link *link;
953
Saravana Kannan8c3e3152020-05-26 15:09:27 -0700954 list_for_each_entry(link, &dev->links.suppliers, c_node) {
955 if (link->status != DL_STATE_CONSUMER_PROBE)
956 continue;
957
958 if (link->supplier->links.status == DL_DEV_DRIVER_BOUND) {
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100959 WRITE_ONCE(link->status, DL_STATE_AVAILABLE);
Saravana Kannan8c3e3152020-05-26 15:09:27 -0700960 } else {
961 WARN_ON(!(link->flags & DL_FLAG_SYNC_STATE_ONLY));
962 WRITE_ONCE(link->status, DL_STATE_DORMANT);
963 }
964 }
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100965}
966
967/**
968 * device_links_check_suppliers - Check presence of supplier drivers.
969 * @dev: Consumer device.
970 *
971 * Check links from this device to any suppliers. Walk the list of the device's
972 * links to suppliers and see if all of them are available. If not, simply
973 * return -EPROBE_DEFER.
974 *
975 * We need to guarantee that the supplier will not go away after the check has
976 * been positive here. It only can go away in __device_release_driver() and
977 * that function checks the device's links to consumers. This means we need to
978 * mark the link as "consumer probe in progress" to make the supplier removal
979 * wait for us to complete (or bad things may happen).
980 *
Rafael J. Wysocki515db262019-07-16 17:21:06 +0200981 * Links without the DL_FLAG_MANAGED flag set are ignored.
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100982 */
983int device_links_check_suppliers(struct device *dev)
984{
985 struct device_link *link;
986 int ret = 0;
Saravana Kannan68223ee2021-09-15 10:28:05 -0700987 struct fwnode_handle *sup_fw;
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100988
Saravana Kannane2ae9bc2019-09-04 14:11:21 -0700989 /*
990 * Device waiting for supplier to become available is not allowed to
991 * probe.
992 */
Saravana Kannan25ac86c2020-11-20 18:02:28 -0800993 mutex_lock(&fwnode_link_lock);
994 if (dev->fwnode && !list_empty(&dev->fwnode->suppliers) &&
995 !fw_devlink_is_permissive()) {
Saravana Kannan68223ee2021-09-15 10:28:05 -0700996 sup_fw = list_first_entry(&dev->fwnode->suppliers,
997 struct fwnode_link,
998 c_hook)->supplier;
999 dev_err_probe(dev, -EPROBE_DEFER, "wait for supplier %pfwP\n",
1000 sup_fw);
Saravana Kannan25ac86c2020-11-20 18:02:28 -08001001 mutex_unlock(&fwnode_link_lock);
Saravana Kannane2ae9bc2019-09-04 14:11:21 -07001002 return -EPROBE_DEFER;
1003 }
Saravana Kannan25ac86c2020-11-20 18:02:28 -08001004 mutex_unlock(&fwnode_link_lock);
Saravana Kannane2ae9bc2019-09-04 14:11:21 -07001005
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001006 device_links_write_lock();
1007
1008 list_for_each_entry(link, &dev->links.suppliers, c_node) {
Saravana Kannan8c3e3152020-05-26 15:09:27 -07001009 if (!(link->flags & DL_FLAG_MANAGED))
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001010 continue;
1011
Saravana Kannan8c3e3152020-05-26 15:09:27 -07001012 if (link->status != DL_STATE_AVAILABLE &&
1013 !(link->flags & DL_FLAG_SYNC_STATE_ONLY)) {
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001014 device_links_missing_supplier(dev);
Saravana Kannan68223ee2021-09-15 10:28:05 -07001015 dev_err_probe(dev, -EPROBE_DEFER,
1016 "supplier %s not ready\n",
1017 dev_name(link->supplier));
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001018 ret = -EPROBE_DEFER;
1019 break;
1020 }
1021 WRITE_ONCE(link->status, DL_STATE_CONSUMER_PROBE);
1022 }
1023 dev->links.status = DL_DEV_PROBING;
1024
1025 device_links_write_unlock();
1026 return ret;
1027}
1028
Saravana Kannan26e77702019-11-14 14:56:45 -08001029/**
1030 * __device_links_queue_sync_state - Queue a device for sync_state() callback
1031 * @dev: Device to call sync_state() on
1032 * @list: List head to queue the @dev on
1033 *
1034 * Queues a device for a sync_state() callback when the device links write lock
1035 * isn't held. This allows the sync_state() execution flow to use device links
1036 * APIs. The caller must ensure this function is called with
1037 * device_links_write_lock() held.
1038 *
1039 * This function does a get_device() to make sure the device is not freed while
1040 * on this list.
1041 *
1042 * So the caller must also ensure that device_links_flush_sync_list() is called
1043 * as soon as the caller releases device_links_write_lock(). This is necessary
1044 * to make sure the sync_state() is called in a timely fashion and the
1045 * put_device() is called on this device.
1046 */
1047static void __device_links_queue_sync_state(struct device *dev,
1048 struct list_head *list)
Saravana Kannanfc5a2512019-09-04 14:11:23 -07001049{
1050 struct device_link *link;
1051
Saravana Kannan77036162020-02-21 00:05:10 -08001052 if (!dev_has_sync_state(dev))
1053 return;
Saravana Kannanfc5a2512019-09-04 14:11:23 -07001054 if (dev->state_synced)
1055 return;
1056
1057 list_for_each_entry(link, &dev->links.consumers, s_node) {
1058 if (!(link->flags & DL_FLAG_MANAGED))
1059 continue;
1060 if (link->status != DL_STATE_ACTIVE)
1061 return;
1062 }
1063
Saravana Kannan26e77702019-11-14 14:56:45 -08001064 /*
1065 * Set the flag here to avoid adding the same device to a list more
1066 * than once. This can happen if new consumers get added to the device
1067 * and probed before the list is flushed.
1068 */
Saravana Kannanfc5a2512019-09-04 14:11:23 -07001069 dev->state_synced = true;
Saravana Kannan26e77702019-11-14 14:56:45 -08001070
Saravana Kannan3b052a32020-11-20 18:02:17 -08001071 if (WARN_ON(!list_empty(&dev->links.defer_sync)))
Saravana Kannan26e77702019-11-14 14:56:45 -08001072 return;
1073
1074 get_device(dev);
Saravana Kannan3b052a32020-11-20 18:02:17 -08001075 list_add_tail(&dev->links.defer_sync, list);
Saravana Kannan26e77702019-11-14 14:56:45 -08001076}
1077
1078/**
1079 * device_links_flush_sync_list - Call sync_state() on a list of devices
1080 * @list: List of devices to call sync_state() on
Saravana Kannan21eb93f2020-02-21 00:05:08 -08001081 * @dont_lock_dev: Device for which lock is already held by the caller
Saravana Kannan26e77702019-11-14 14:56:45 -08001082 *
1083 * Calls sync_state() on all the devices that have been queued for it. This
Saravana Kannan21eb93f2020-02-21 00:05:08 -08001084 * function is used in conjunction with __device_links_queue_sync_state(). The
1085 * @dont_lock_dev parameter is useful when this function is called from a
1086 * context where a device lock is already held.
Saravana Kannan26e77702019-11-14 14:56:45 -08001087 */
Saravana Kannan21eb93f2020-02-21 00:05:08 -08001088static void device_links_flush_sync_list(struct list_head *list,
1089 struct device *dont_lock_dev)
Saravana Kannan26e77702019-11-14 14:56:45 -08001090{
1091 struct device *dev, *tmp;
1092
Saravana Kannan3b052a32020-11-20 18:02:17 -08001093 list_for_each_entry_safe(dev, tmp, list, links.defer_sync) {
1094 list_del_init(&dev->links.defer_sync);
Saravana Kannan26e77702019-11-14 14:56:45 -08001095
Saravana Kannan21eb93f2020-02-21 00:05:08 -08001096 if (dev != dont_lock_dev)
1097 device_lock(dev);
Saravana Kannan26e77702019-11-14 14:56:45 -08001098
1099 if (dev->bus->sync_state)
1100 dev->bus->sync_state(dev);
1101 else if (dev->driver && dev->driver->sync_state)
1102 dev->driver->sync_state(dev);
1103
Saravana Kannan21eb93f2020-02-21 00:05:08 -08001104 if (dev != dont_lock_dev)
1105 device_unlock(dev);
Saravana Kannan26e77702019-11-14 14:56:45 -08001106
1107 put_device(dev);
1108 }
Saravana Kannanfc5a2512019-09-04 14:11:23 -07001109}
1110
1111void device_links_supplier_sync_state_pause(void)
1112{
1113 device_links_write_lock();
1114 defer_sync_state_count++;
1115 device_links_write_unlock();
1116}
1117
1118void device_links_supplier_sync_state_resume(void)
1119{
1120 struct device *dev, *tmp;
Saravana Kannan26e77702019-11-14 14:56:45 -08001121 LIST_HEAD(sync_list);
Saravana Kannanfc5a2512019-09-04 14:11:23 -07001122
1123 device_links_write_lock();
1124 if (!defer_sync_state_count) {
1125 WARN(true, "Unmatched sync_state pause/resume!");
1126 goto out;
1127 }
1128 defer_sync_state_count--;
1129 if (defer_sync_state_count)
1130 goto out;
1131
Saravana Kannan3b052a32020-11-20 18:02:17 -08001132 list_for_each_entry_safe(dev, tmp, &deferred_sync, links.defer_sync) {
Saravana Kannan26e77702019-11-14 14:56:45 -08001133 /*
1134 * Delete from deferred_sync list before queuing it to
Saravana Kannan3b052a32020-11-20 18:02:17 -08001135 * sync_list because defer_sync is used for both lists.
Saravana Kannan26e77702019-11-14 14:56:45 -08001136 */
Saravana Kannan3b052a32020-11-20 18:02:17 -08001137 list_del_init(&dev->links.defer_sync);
Saravana Kannan26e77702019-11-14 14:56:45 -08001138 __device_links_queue_sync_state(dev, &sync_list);
Saravana Kannanfc5a2512019-09-04 14:11:23 -07001139 }
1140out:
1141 device_links_write_unlock();
Saravana Kannan26e77702019-11-14 14:56:45 -08001142
Saravana Kannan21eb93f2020-02-21 00:05:08 -08001143 device_links_flush_sync_list(&sync_list, NULL);
Saravana Kannanfc5a2512019-09-04 14:11:23 -07001144}
1145
1146static int sync_state_resume_initcall(void)
1147{
1148 device_links_supplier_sync_state_resume();
1149 return 0;
1150}
1151late_initcall(sync_state_resume_initcall);
1152
1153static void __device_links_supplier_defer_sync(struct device *sup)
1154{
Saravana Kannan3b052a32020-11-20 18:02:17 -08001155 if (list_empty(&sup->links.defer_sync) && dev_has_sync_state(sup))
1156 list_add_tail(&sup->links.defer_sync, &deferred_sync);
Saravana Kannanfc5a2512019-09-04 14:11:23 -07001157}
1158
Saravana Kannan21c27f02020-05-18 23:30:00 -07001159static void device_link_drop_managed(struct device_link *link)
1160{
1161 link->flags &= ~DL_FLAG_MANAGED;
1162 WRITE_ONCE(link->status, DL_STATE_NONE);
1163 kref_put(&link->kref, __device_link_del);
1164}
1165
Saravana Kannanda6d6472020-05-21 12:18:00 -07001166static ssize_t waiting_for_supplier_show(struct device *dev,
1167 struct device_attribute *attr,
1168 char *buf)
1169{
1170 bool val;
1171
1172 device_lock(dev);
Saravana Kannan25ac86c2020-11-20 18:02:28 -08001173 val = !list_empty(&dev->fwnode->suppliers);
Saravana Kannanda6d6472020-05-21 12:18:00 -07001174 device_unlock(dev);
Joe Perchesaa838892020-09-16 13:40:39 -07001175 return sysfs_emit(buf, "%u\n", val);
Saravana Kannanda6d6472020-05-21 12:18:00 -07001176}
1177static DEVICE_ATTR_RO(waiting_for_supplier);
1178
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001179/**
Saravana Kannanb6f617d2021-03-02 13:11:31 -08001180 * device_links_force_bind - Prepares device to be force bound
1181 * @dev: Consumer device.
1182 *
1183 * device_bind_driver() force binds a device to a driver without calling any
1184 * driver probe functions. So the consumer really isn't going to wait for any
1185 * supplier before it's bound to the driver. We still want the device link
1186 * states to be sensible when this happens.
1187 *
1188 * In preparation for device_bind_driver(), this function goes through each
1189 * supplier device links and checks if the supplier is bound. If it is, then
1190 * the device link status is set to CONSUMER_PROBE. Otherwise, the device link
1191 * is dropped. Links without the DL_FLAG_MANAGED flag set are ignored.
1192 */
1193void device_links_force_bind(struct device *dev)
1194{
1195 struct device_link *link, *ln;
1196
1197 device_links_write_lock();
1198
1199 list_for_each_entry_safe(link, ln, &dev->links.suppliers, c_node) {
1200 if (!(link->flags & DL_FLAG_MANAGED))
1201 continue;
1202
1203 if (link->status != DL_STATE_AVAILABLE) {
1204 device_link_drop_managed(link);
1205 continue;
1206 }
1207 WRITE_ONCE(link->status, DL_STATE_CONSUMER_PROBE);
1208 }
1209 dev->links.status = DL_DEV_PROBING;
1210
1211 device_links_write_unlock();
1212}
1213
1214/**
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001215 * device_links_driver_bound - Update device links after probing its driver.
1216 * @dev: Device to update the links for.
1217 *
1218 * The probe has been successful, so update links from this device to any
1219 * consumers by changing their status to "available".
1220 *
1221 * Also change the status of @dev's links to suppliers to "active".
1222 *
Rafael J. Wysocki515db262019-07-16 17:21:06 +02001223 * Links without the DL_FLAG_MANAGED flag set are ignored.
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001224 */
1225void device_links_driver_bound(struct device *dev)
1226{
Saravana Kannan21c27f02020-05-18 23:30:00 -07001227 struct device_link *link, *ln;
Saravana Kannan26e77702019-11-14 14:56:45 -08001228 LIST_HEAD(sync_list);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001229
Saravana Kannanbcbbcfd2019-10-28 15:00:23 -07001230 /*
Saravana Kannan9528e0d2021-02-05 14:26:37 -08001231 * If a device binds successfully, it's expected to have created all
Saravana Kannanbcbbcfd2019-10-28 15:00:23 -07001232 * the device links it needs to or make new device links as it needs
Saravana Kannan9528e0d2021-02-05 14:26:37 -08001233 * them. So, fw_devlink no longer needs to create device links to any
1234 * of the device's suppliers.
1235 *
1236 * Also, if a child firmware node of this bound device is not added as
1237 * a device by now, assume it is never going to be added and make sure
1238 * other devices don't defer probe indefinitely by waiting for such a
1239 * child device.
Saravana Kannanbcbbcfd2019-10-28 15:00:23 -07001240 */
Saravana Kannan9528e0d2021-02-05 14:26:37 -08001241 if (dev->fwnode && dev->fwnode->dev == dev) {
1242 struct fwnode_handle *child;
Saravana Kannanf9aa4602020-11-20 18:02:31 -08001243 fwnode_links_purge_suppliers(dev->fwnode);
Saravana Kannan9528e0d2021-02-05 14:26:37 -08001244 fwnode_for_each_available_child_node(dev->fwnode, child)
1245 fw_devlink_purge_absent_suppliers(child);
1246 }
Saravana Kannanda6d6472020-05-21 12:18:00 -07001247 device_remove_file(dev, &dev_attr_waiting_for_supplier);
Saravana Kannanbcbbcfd2019-10-28 15:00:23 -07001248
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001249 device_links_write_lock();
1250
1251 list_for_each_entry(link, &dev->links.consumers, s_node) {
Rafael J. Wysocki515db262019-07-16 17:21:06 +02001252 if (!(link->flags & DL_FLAG_MANAGED))
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001253 continue;
1254
Rafael J. Wysocki15cfb092019-02-01 01:50:39 +01001255 /*
1256 * Links created during consumer probe may be in the "consumer
1257 * probe" state to start with if the supplier is still probing
1258 * when they are created and they may become "active" if the
1259 * consumer probe returns first. Skip them here.
1260 */
1261 if (link->status == DL_STATE_CONSUMER_PROBE ||
1262 link->status == DL_STATE_ACTIVE)
1263 continue;
1264
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001265 WARN_ON(link->status != DL_STATE_DORMANT);
1266 WRITE_ONCE(link->status, DL_STATE_AVAILABLE);
Rafael J. Wysockie7dd4012019-02-01 01:59:42 +01001267
1268 if (link->flags & DL_FLAG_AUTOPROBE_CONSUMER)
1269 driver_deferred_probe_add(link->consumer);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001270 }
1271
Saravana Kannan21eb93f2020-02-21 00:05:08 -08001272 if (defer_sync_state_count)
1273 __device_links_supplier_defer_sync(dev);
1274 else
1275 __device_links_queue_sync_state(dev, &sync_list);
1276
Saravana Kannan21c27f02020-05-18 23:30:00 -07001277 list_for_each_entry_safe(link, ln, &dev->links.suppliers, c_node) {
1278 struct device *supplier;
1279
Rafael J. Wysocki515db262019-07-16 17:21:06 +02001280 if (!(link->flags & DL_FLAG_MANAGED))
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001281 continue;
1282
Saravana Kannan21c27f02020-05-18 23:30:00 -07001283 supplier = link->supplier;
1284 if (link->flags & DL_FLAG_SYNC_STATE_ONLY) {
1285 /*
1286 * When DL_FLAG_SYNC_STATE_ONLY is set, it means no
1287 * other DL_MANAGED_LINK_FLAGS have been set. So, it's
1288 * save to drop the managed link completely.
1289 */
1290 device_link_drop_managed(link);
1291 } else {
1292 WARN_ON(link->status != DL_STATE_CONSUMER_PROBE);
1293 WRITE_ONCE(link->status, DL_STATE_ACTIVE);
1294 }
Saravana Kannanfc5a2512019-09-04 14:11:23 -07001295
Saravana Kannan21c27f02020-05-18 23:30:00 -07001296 /*
1297 * This needs to be done even for the deleted
1298 * DL_FLAG_SYNC_STATE_ONLY device link in case it was the last
1299 * device link that was preventing the supplier from getting a
1300 * sync_state() call.
1301 */
Saravana Kannanfc5a2512019-09-04 14:11:23 -07001302 if (defer_sync_state_count)
Saravana Kannan21c27f02020-05-18 23:30:00 -07001303 __device_links_supplier_defer_sync(supplier);
Saravana Kannanfc5a2512019-09-04 14:11:23 -07001304 else
Saravana Kannan21c27f02020-05-18 23:30:00 -07001305 __device_links_queue_sync_state(supplier, &sync_list);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001306 }
1307
1308 dev->links.status = DL_DEV_DRIVER_BOUND;
1309
1310 device_links_write_unlock();
Saravana Kannan26e77702019-11-14 14:56:45 -08001311
Saravana Kannan21eb93f2020-02-21 00:05:08 -08001312 device_links_flush_sync_list(&sync_list, dev);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001313}
1314
1315/**
1316 * __device_links_no_driver - Update links of a device without a driver.
1317 * @dev: Device without a drvier.
1318 *
1319 * Delete all non-persistent links from this device to any suppliers.
1320 *
1321 * Persistent links stay around, but their status is changed to "available",
1322 * unless they already are in the "supplier unbind in progress" state in which
1323 * case they need not be updated.
1324 *
Rafael J. Wysocki515db262019-07-16 17:21:06 +02001325 * Links without the DL_FLAG_MANAGED flag set are ignored.
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001326 */
1327static void __device_links_no_driver(struct device *dev)
1328{
1329 struct device_link *link, *ln;
1330
1331 list_for_each_entry_safe_reverse(link, ln, &dev->links.suppliers, c_node) {
Rafael J. Wysocki515db262019-07-16 17:21:06 +02001332 if (!(link->flags & DL_FLAG_MANAGED))
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001333 continue;
1334
Saravana Kannan8c3e3152020-05-26 15:09:27 -07001335 if (link->flags & DL_FLAG_AUTOREMOVE_CONSUMER) {
Rafael J. Wysocki515db262019-07-16 17:21:06 +02001336 device_link_drop_managed(link);
Saravana Kannan8c3e3152020-05-26 15:09:27 -07001337 continue;
1338 }
1339
1340 if (link->status != DL_STATE_CONSUMER_PROBE &&
1341 link->status != DL_STATE_ACTIVE)
1342 continue;
1343
1344 if (link->supplier->links.status == DL_DEV_DRIVER_BOUND) {
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001345 WRITE_ONCE(link->status, DL_STATE_AVAILABLE);
Saravana Kannan8c3e3152020-05-26 15:09:27 -07001346 } else {
1347 WARN_ON(!(link->flags & DL_FLAG_SYNC_STATE_ONLY));
1348 WRITE_ONCE(link->status, DL_STATE_DORMANT);
1349 }
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001350 }
1351
1352 dev->links.status = DL_DEV_NO_DRIVER;
1353}
1354
Rafael J. Wysocki15cfb092019-02-01 01:50:39 +01001355/**
1356 * device_links_no_driver - Update links after failing driver probe.
1357 * @dev: Device whose driver has just failed to probe.
1358 *
1359 * Clean up leftover links to consumers for @dev and invoke
1360 * %__device_links_no_driver() to update links to suppliers for it as
1361 * appropriate.
1362 *
Rafael J. Wysocki515db262019-07-16 17:21:06 +02001363 * Links without the DL_FLAG_MANAGED flag set are ignored.
Rafael J. Wysocki15cfb092019-02-01 01:50:39 +01001364 */
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001365void device_links_no_driver(struct device *dev)
1366{
Rafael J. Wysocki15cfb092019-02-01 01:50:39 +01001367 struct device_link *link;
1368
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001369 device_links_write_lock();
Rafael J. Wysocki15cfb092019-02-01 01:50:39 +01001370
1371 list_for_each_entry(link, &dev->links.consumers, s_node) {
Rafael J. Wysocki515db262019-07-16 17:21:06 +02001372 if (!(link->flags & DL_FLAG_MANAGED))
Rafael J. Wysocki15cfb092019-02-01 01:50:39 +01001373 continue;
1374
1375 /*
1376 * The probe has failed, so if the status of the link is
1377 * "consumer probe" or "active", it must have been added by
1378 * a probing consumer while this device was still probing.
1379 * Change its state to "dormant", as it represents a valid
1380 * relationship, but it is not functionally meaningful.
1381 */
1382 if (link->status == DL_STATE_CONSUMER_PROBE ||
1383 link->status == DL_STATE_ACTIVE)
1384 WRITE_ONCE(link->status, DL_STATE_DORMANT);
1385 }
1386
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001387 __device_links_no_driver(dev);
Rafael J. Wysocki15cfb092019-02-01 01:50:39 +01001388
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001389 device_links_write_unlock();
1390}
1391
1392/**
1393 * device_links_driver_cleanup - Update links after driver removal.
1394 * @dev: Device whose driver has just gone away.
1395 *
1396 * Update links to consumers for @dev by changing their status to "dormant" and
1397 * invoke %__device_links_no_driver() to update links to suppliers for it as
1398 * appropriate.
1399 *
Rafael J. Wysocki515db262019-07-16 17:21:06 +02001400 * Links without the DL_FLAG_MANAGED flag set are ignored.
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001401 */
1402void device_links_driver_cleanup(struct device *dev)
1403{
Rafael J. Wysockic8d50982019-02-01 01:45:55 +01001404 struct device_link *link, *ln;
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001405
1406 device_links_write_lock();
1407
Rafael J. Wysockic8d50982019-02-01 01:45:55 +01001408 list_for_each_entry_safe(link, ln, &dev->links.consumers, s_node) {
Rafael J. Wysocki515db262019-07-16 17:21:06 +02001409 if (!(link->flags & DL_FLAG_MANAGED))
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001410 continue;
1411
Vivek Gautame88728f2018-06-27 18:20:55 +05301412 WARN_ON(link->flags & DL_FLAG_AUTOREMOVE_CONSUMER);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001413 WARN_ON(link->status != DL_STATE_SUPPLIER_UNBIND);
Vivek Gautam1689cac2018-06-27 18:20:56 +05301414
1415 /*
1416 * autoremove the links between this @dev and its consumer
1417 * devices that are not active, i.e. where the link state
1418 * has moved to DL_STATE_SUPPLIER_UNBIND.
1419 */
1420 if (link->status == DL_STATE_SUPPLIER_UNBIND &&
1421 link->flags & DL_FLAG_AUTOREMOVE_SUPPLIER)
Rafael J. Wysocki515db262019-07-16 17:21:06 +02001422 device_link_drop_managed(link);
Vivek Gautam1689cac2018-06-27 18:20:56 +05301423
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001424 WRITE_ONCE(link->status, DL_STATE_DORMANT);
1425 }
1426
Saravana Kannan3b052a32020-11-20 18:02:17 -08001427 list_del_init(&dev->links.defer_sync);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001428 __device_links_no_driver(dev);
1429
1430 device_links_write_unlock();
1431}
1432
1433/**
1434 * device_links_busy - Check if there are any busy links to consumers.
1435 * @dev: Device to check.
1436 *
1437 * Check each consumer of the device and return 'true' if its link's status
1438 * is one of "consumer probe" or "active" (meaning that the given consumer is
1439 * probing right now or its driver is present). Otherwise, change the link
1440 * state to "supplier unbind" to prevent the consumer from being probed
1441 * successfully going forward.
1442 *
1443 * Return 'false' if there are no probing or active consumers.
1444 *
Rafael J. Wysocki515db262019-07-16 17:21:06 +02001445 * Links without the DL_FLAG_MANAGED flag set are ignored.
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001446 */
1447bool device_links_busy(struct device *dev)
1448{
1449 struct device_link *link;
1450 bool ret = false;
1451
1452 device_links_write_lock();
1453
1454 list_for_each_entry(link, &dev->links.consumers, s_node) {
Rafael J. Wysocki515db262019-07-16 17:21:06 +02001455 if (!(link->flags & DL_FLAG_MANAGED))
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001456 continue;
1457
1458 if (link->status == DL_STATE_CONSUMER_PROBE
1459 || link->status == DL_STATE_ACTIVE) {
1460 ret = true;
1461 break;
1462 }
1463 WRITE_ONCE(link->status, DL_STATE_SUPPLIER_UNBIND);
1464 }
1465
1466 dev->links.status = DL_DEV_UNBINDING;
1467
1468 device_links_write_unlock();
1469 return ret;
1470}
1471
1472/**
1473 * device_links_unbind_consumers - Force unbind consumers of the given device.
1474 * @dev: Device to unbind the consumers of.
1475 *
1476 * Walk the list of links to consumers for @dev and if any of them is in the
1477 * "consumer probe" state, wait for all device probes in progress to complete
1478 * and start over.
1479 *
1480 * If that's not the case, change the status of the link to "supplier unbind"
1481 * and check if the link was in the "active" state. If so, force the consumer
1482 * driver to unbind and start over (the consumer will not re-probe as we have
1483 * changed the state of the link already).
1484 *
Rafael J. Wysocki515db262019-07-16 17:21:06 +02001485 * Links without the DL_FLAG_MANAGED flag set are ignored.
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001486 */
1487void device_links_unbind_consumers(struct device *dev)
1488{
1489 struct device_link *link;
1490
1491 start:
1492 device_links_write_lock();
1493
1494 list_for_each_entry(link, &dev->links.consumers, s_node) {
1495 enum device_link_state status;
1496
Saravana Kannan05ef9832019-10-28 15:00:22 -07001497 if (!(link->flags & DL_FLAG_MANAGED) ||
1498 link->flags & DL_FLAG_SYNC_STATE_ONLY)
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001499 continue;
1500
1501 status = link->status;
1502 if (status == DL_STATE_CONSUMER_PROBE) {
1503 device_links_write_unlock();
1504
1505 wait_for_device_probe();
1506 goto start;
1507 }
1508 WRITE_ONCE(link->status, DL_STATE_SUPPLIER_UNBIND);
1509 if (status == DL_STATE_ACTIVE) {
1510 struct device *consumer = link->consumer;
1511
1512 get_device(consumer);
1513
1514 device_links_write_unlock();
1515
1516 device_release_driver_internal(consumer, NULL,
1517 consumer->parent);
1518 put_device(consumer);
1519 goto start;
1520 }
1521 }
1522
1523 device_links_write_unlock();
1524}
1525
1526/**
1527 * device_links_purge - Delete existing links to other devices.
1528 * @dev: Target device.
1529 */
1530static void device_links_purge(struct device *dev)
1531{
1532 struct device_link *link, *ln;
1533
Saravana Kannan287905e2020-05-21 12:17:58 -07001534 if (dev->class == &devlink_class)
1535 return;
1536
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001537 /*
1538 * Delete all of the remaining links from this device to any other
1539 * devices (either consumers or suppliers).
1540 */
1541 device_links_write_lock();
1542
1543 list_for_each_entry_safe_reverse(link, ln, &dev->links.suppliers, c_node) {
1544 WARN_ON(link->status == DL_STATE_ACTIVE);
Lukas Wunneread18c22018-02-10 19:27:12 +01001545 __device_link_del(&link->kref);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001546 }
1547
1548 list_for_each_entry_safe_reverse(link, ln, &dev->links.consumers, s_node) {
1549 WARN_ON(link->status != DL_STATE_DORMANT &&
1550 link->status != DL_STATE_NONE);
Lukas Wunneread18c22018-02-10 19:27:12 +01001551 __device_link_del(&link->kref);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001552 }
1553
1554 device_links_write_unlock();
1555}
1556
Saravana Kannanb90fb8f2020-12-17 19:17:01 -08001557#define FW_DEVLINK_FLAGS_PERMISSIVE (DL_FLAG_INFERRED | \
1558 DL_FLAG_SYNC_STATE_ONLY)
1559#define FW_DEVLINK_FLAGS_ON (DL_FLAG_INFERRED | \
1560 DL_FLAG_AUTOPROBE_CONSUMER)
1561#define FW_DEVLINK_FLAGS_RPM (FW_DEVLINK_FLAGS_ON | \
1562 DL_FLAG_PM_RUNTIME)
1563
Saravana Kannanea718c692021-03-02 13:11:32 -08001564static u32 fw_devlink_flags = FW_DEVLINK_FLAGS_ON;
Saravana Kannan42926ac2020-05-14 22:34:57 -07001565static int __init fw_devlink_setup(char *arg)
1566{
1567 if (!arg)
1568 return -EINVAL;
1569
1570 if (strcmp(arg, "off") == 0) {
1571 fw_devlink_flags = 0;
1572 } else if (strcmp(arg, "permissive") == 0) {
Saravana Kannanb90fb8f2020-12-17 19:17:01 -08001573 fw_devlink_flags = FW_DEVLINK_FLAGS_PERMISSIVE;
Saravana Kannan42926ac2020-05-14 22:34:57 -07001574 } else if (strcmp(arg, "on") == 0) {
Saravana Kannanb90fb8f2020-12-17 19:17:01 -08001575 fw_devlink_flags = FW_DEVLINK_FLAGS_ON;
Saravana Kannan42926ac2020-05-14 22:34:57 -07001576 } else if (strcmp(arg, "rpm") == 0) {
Saravana Kannanb90fb8f2020-12-17 19:17:01 -08001577 fw_devlink_flags = FW_DEVLINK_FLAGS_RPM;
Saravana Kannan42926ac2020-05-14 22:34:57 -07001578 }
1579 return 0;
1580}
1581early_param("fw_devlink", fw_devlink_setup);
1582
Saravana Kannan19d0f5f2021-02-05 14:26:39 -08001583static bool fw_devlink_strict;
1584static int __init fw_devlink_strict_setup(char *arg)
1585{
1586 return strtobool(arg, &fw_devlink_strict);
1587}
1588early_param("fw_devlink.strict", fw_devlink_strict_setup);
1589
Saravana Kannan42926ac2020-05-14 22:34:57 -07001590u32 fw_devlink_get_flags(void)
1591{
1592 return fw_devlink_flags;
1593}
1594
1595static bool fw_devlink_is_permissive(void)
1596{
Saravana Kannanb90fb8f2020-12-17 19:17:01 -08001597 return fw_devlink_flags == FW_DEVLINK_FLAGS_PERMISSIVE;
Saravana Kannan42926ac2020-05-14 22:34:57 -07001598}
1599
Saravana Kannan19d0f5f2021-02-05 14:26:39 -08001600bool fw_devlink_is_strict(void)
1601{
1602 return fw_devlink_strict && !fw_devlink_is_permissive();
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001603}
1604
Saravana Kannanc2c724c2020-11-20 18:02:27 -08001605static void fw_devlink_parse_fwnode(struct fwnode_handle *fwnode)
1606{
1607 if (fwnode->flags & FWNODE_FLAG_LINKS_ADDED)
1608 return;
1609
Saravana Kannan2d09e6e2020-11-20 18:02:32 -08001610 fwnode_call_int_op(fwnode, add_links);
Saravana Kannanc2c724c2020-11-20 18:02:27 -08001611 fwnode->flags |= FWNODE_FLAG_LINKS_ADDED;
1612}
1613
1614static void fw_devlink_parse_fwtree(struct fwnode_handle *fwnode)
1615{
1616 struct fwnode_handle *child = NULL;
1617
1618 fw_devlink_parse_fwnode(fwnode);
1619
1620 while ((child = fwnode_get_next_available_child_node(fwnode, child)))
1621 fw_devlink_parse_fwtree(child);
1622}
1623
Saravana Kannand46f3e32021-04-01 21:03:41 -07001624static void fw_devlink_relax_link(struct device_link *link)
1625{
1626 if (!(link->flags & DL_FLAG_INFERRED))
1627 return;
1628
1629 if (link->flags == (DL_FLAG_MANAGED | FW_DEVLINK_FLAGS_PERMISSIVE))
1630 return;
1631
1632 pm_runtime_drop_link(link);
1633 link->flags = DL_FLAG_MANAGED | FW_DEVLINK_FLAGS_PERMISSIVE;
1634 dev_dbg(link->consumer, "Relaxing link with %s\n",
1635 dev_name(link->supplier));
1636}
1637
1638static int fw_devlink_no_driver(struct device *dev, void *data)
1639{
1640 struct device_link *link = to_devlink(dev);
1641
1642 if (!link->supplier->can_match)
1643 fw_devlink_relax_link(link);
1644
1645 return 0;
1646}
1647
1648void fw_devlink_drivers_done(void)
1649{
1650 fw_devlink_drv_reg_done = true;
1651 device_links_write_lock();
1652 class_for_each_device(&devlink_class, NULL, NULL,
1653 fw_devlink_no_driver);
1654 device_links_write_unlock();
1655}
1656
1657static void fw_devlink_unblock_consumers(struct device *dev)
1658{
1659 struct device_link *link;
1660
1661 if (!fw_devlink_flags || fw_devlink_is_permissive())
1662 return;
1663
1664 device_links_write_lock();
1665 list_for_each_entry(link, &dev->links.consumers, s_node)
1666 fw_devlink_relax_link(link);
1667 device_links_write_unlock();
1668}
1669
Saravana Kannanf9aa4602020-11-20 18:02:31 -08001670/**
Saravana Kannanb0e2fa42020-12-17 19:17:02 -08001671 * fw_devlink_relax_cycle - Convert cyclic links to SYNC_STATE_ONLY links
1672 * @con: Device to check dependencies for.
1673 * @sup: Device to check against.
1674 *
1675 * Check if @sup depends on @con or any device dependent on it (its child or
1676 * its consumer etc). When such a cyclic dependency is found, convert all
1677 * device links created solely by fw_devlink into SYNC_STATE_ONLY device links.
1678 * This is the equivalent of doing fw_devlink=permissive just between the
1679 * devices in the cycle. We need to do this because, at this point, fw_devlink
1680 * can't tell which of these dependencies is not a real dependency.
1681 *
1682 * Return 1 if a cycle is found. Otherwise, return 0.
1683 */
kernel test robotc13b8272020-12-18 14:39:35 +08001684static int fw_devlink_relax_cycle(struct device *con, void *sup)
Saravana Kannanb0e2fa42020-12-17 19:17:02 -08001685{
1686 struct device_link *link;
1687 int ret;
1688
1689 if (con == sup)
1690 return 1;
1691
1692 ret = device_for_each_child(con, sup, fw_devlink_relax_cycle);
1693 if (ret)
1694 return ret;
1695
1696 list_for_each_entry(link, &con->links.consumers, s_node) {
1697 if ((link->flags & ~DL_FLAG_INFERRED) ==
1698 (DL_FLAG_SYNC_STATE_ONLY | DL_FLAG_MANAGED))
1699 continue;
1700
1701 if (!fw_devlink_relax_cycle(link->consumer, sup))
1702 continue;
1703
1704 ret = 1;
1705
Saravana Kannand46f3e32021-04-01 21:03:41 -07001706 fw_devlink_relax_link(link);
Saravana Kannanb0e2fa42020-12-17 19:17:02 -08001707 }
1708 return ret;
1709}
1710
1711/**
Saravana Kannanf9aa4602020-11-20 18:02:31 -08001712 * fw_devlink_create_devlink - Create a device link from a consumer to fwnode
Pierre-Louis Bossart37c52f72021-03-31 18:26:08 -05001713 * @con: consumer device for the device link
1714 * @sup_handle: fwnode handle of supplier
1715 * @flags: devlink flags
Saravana Kannanf9aa4602020-11-20 18:02:31 -08001716 *
1717 * This function will try to create a device link between the consumer device
1718 * @con and the supplier device represented by @sup_handle.
1719 *
1720 * The supplier has to be provided as a fwnode because incorrect cycles in
1721 * fwnode links can sometimes cause the supplier device to never be created.
1722 * This function detects such cases and returns an error if it cannot create a
1723 * device link from the consumer to a missing supplier.
1724 *
1725 * Returns,
1726 * 0 on successfully creating a device link
1727 * -EINVAL if the device link cannot be created as expected
1728 * -EAGAIN if the device link cannot be created right now, but it may be
1729 * possible to do that in the future
1730 */
1731static int fw_devlink_create_devlink(struct device *con,
1732 struct fwnode_handle *sup_handle, u32 flags)
1733{
1734 struct device *sup_dev;
1735 int ret = 0;
1736
Saravana Kannan55017652021-09-15 10:09:38 -07001737 /*
1738 * In some cases, a device P might also be a supplier to its child node
1739 * C. However, this would defer the probe of C until the probe of P
1740 * completes successfully. This is perfectly fine in the device driver
1741 * model. device_add() doesn't guarantee probe completion of the device
1742 * by the time it returns.
1743 *
1744 * However, there are a few drivers that assume C will finish probing
1745 * as soon as it's added and before P finishes probing. So, we provide
1746 * a flag to let fw_devlink know not to delay the probe of C until the
1747 * probe of P completes successfully.
1748 *
1749 * When such a flag is set, we can't create device links where P is the
1750 * supplier of C as that would delay the probe of C.
1751 */
1752 if (sup_handle->flags & FWNODE_FLAG_NEEDS_CHILD_BOUND_ON_ADD &&
1753 fwnode_is_ancestor_of(sup_handle, con->fwnode))
1754 return -EINVAL;
1755
Saravana Kannanf9aa4602020-11-20 18:02:31 -08001756 sup_dev = get_dev_from_fwnode(sup_handle);
1757 if (sup_dev) {
1758 /*
Saravana Kannan74c782c2021-02-05 14:26:41 -08001759 * If it's one of those drivers that don't actually bind to
1760 * their device using driver core, then don't wait on this
1761 * supplier device indefinitely.
1762 */
1763 if (sup_dev->links.status == DL_DEV_NO_DRIVER &&
1764 sup_handle->flags & FWNODE_FLAG_INITIALIZED) {
1765 ret = -EINVAL;
1766 goto out;
1767 }
1768
1769 /*
Saravana Kannanf9aa4602020-11-20 18:02:31 -08001770 * If this fails, it is due to cycles in device links. Just
1771 * give up on this link and treat it as invalid.
1772 */
Saravana Kannanb0e2fa42020-12-17 19:17:02 -08001773 if (!device_link_add(con, sup_dev, flags) &&
1774 !(flags & DL_FLAG_SYNC_STATE_ONLY)) {
1775 dev_info(con, "Fixing up cyclic dependency with %s\n",
1776 dev_name(sup_dev));
1777 device_links_write_lock();
1778 fw_devlink_relax_cycle(con, sup_dev);
1779 device_links_write_unlock();
1780 device_link_add(con, sup_dev,
1781 FW_DEVLINK_FLAGS_PERMISSIVE);
Saravana Kannanf9aa4602020-11-20 18:02:31 -08001782 ret = -EINVAL;
Saravana Kannanb0e2fa42020-12-17 19:17:02 -08001783 }
Saravana Kannanf9aa4602020-11-20 18:02:31 -08001784
1785 goto out;
1786 }
1787
Saravana Kannan74c782c2021-02-05 14:26:41 -08001788 /* Supplier that's already initialized without a struct device. */
1789 if (sup_handle->flags & FWNODE_FLAG_INITIALIZED)
1790 return -EINVAL;
1791
Saravana Kannanf9aa4602020-11-20 18:02:31 -08001792 /*
1793 * DL_FLAG_SYNC_STATE_ONLY doesn't block probing and supports
1794 * cycles. So cycle detection isn't necessary and shouldn't be
1795 * done.
1796 */
1797 if (flags & DL_FLAG_SYNC_STATE_ONLY)
1798 return -EAGAIN;
1799
1800 /*
1801 * If we can't find the supplier device from its fwnode, it might be
1802 * due to a cyclic dependency between fwnodes. Some of these cycles can
1803 * be broken by applying logic. Check for these types of cycles and
1804 * break them so that devices in the cycle probe properly.
1805 *
Saravana Kannan2de9d8e2021-09-15 10:09:37 -07001806 * If the supplier's parent is dependent on the consumer, then the
1807 * consumer and supplier have a cyclic dependency. Since fw_devlink
1808 * can't tell which of the inferred dependencies are incorrect, don't
1809 * enforce probe ordering between any of the devices in this cyclic
1810 * dependency. Do this by relaxing all the fw_devlink device links in
1811 * this cycle and by treating the fwnode link between the consumer and
1812 * the supplier as an invalid dependency.
Saravana Kannanf9aa4602020-11-20 18:02:31 -08001813 */
1814 sup_dev = fwnode_get_next_parent_dev(sup_handle);
1815 if (sup_dev && device_is_dependent(con, sup_dev)) {
Saravana Kannan2de9d8e2021-09-15 10:09:37 -07001816 dev_info(con, "Fixing up cyclic dependency with %pfwP (%s)\n",
1817 sup_handle, dev_name(sup_dev));
1818 device_links_write_lock();
1819 fw_devlink_relax_cycle(con, sup_dev);
1820 device_links_write_unlock();
Saravana Kannanf9aa4602020-11-20 18:02:31 -08001821 ret = -EINVAL;
1822 } else {
1823 /*
1824 * Can't check for cycles or no cycles. So let's try
1825 * again later.
1826 */
1827 ret = -EAGAIN;
1828 }
1829
1830out:
1831 put_device(sup_dev);
1832 return ret;
1833}
1834
1835/**
1836 * __fw_devlink_link_to_consumers - Create device links to consumers of a device
Pierre-Louis Bossart37c52f72021-03-31 18:26:08 -05001837 * @dev: Device that needs to be linked to its consumers
Saravana Kannanf9aa4602020-11-20 18:02:31 -08001838 *
1839 * This function looks at all the consumer fwnodes of @dev and creates device
1840 * links between the consumer device and @dev (supplier).
1841 *
1842 * If the consumer device has not been added yet, then this function creates a
1843 * SYNC_STATE_ONLY link between @dev (supplier) and the closest ancestor device
1844 * of the consumer fwnode. This is necessary to make sure @dev doesn't get a
1845 * sync_state() callback before the real consumer device gets to be added and
1846 * then probed.
1847 *
1848 * Once device links are created from the real consumer to @dev (supplier), the
1849 * fwnode links are deleted.
1850 */
1851static void __fw_devlink_link_to_consumers(struct device *dev)
1852{
1853 struct fwnode_handle *fwnode = dev->fwnode;
1854 struct fwnode_link *link, *tmp;
1855
1856 list_for_each_entry_safe(link, tmp, &fwnode->consumers, s_hook) {
1857 u32 dl_flags = fw_devlink_get_flags();
1858 struct device *con_dev;
1859 bool own_link = true;
1860 int ret;
1861
1862 con_dev = get_dev_from_fwnode(link->consumer);
1863 /*
1864 * If consumer device is not available yet, make a "proxy"
1865 * SYNC_STATE_ONLY link from the consumer's parent device to
1866 * the supplier device. This is necessary to make sure the
1867 * supplier doesn't get a sync_state() callback before the real
1868 * consumer can create a device link to the supplier.
1869 *
1870 * This proxy link step is needed to handle the case where the
1871 * consumer's parent device is added before the supplier.
1872 */
1873 if (!con_dev) {
1874 con_dev = fwnode_get_next_parent_dev(link->consumer);
1875 /*
1876 * However, if the consumer's parent device is also the
1877 * parent of the supplier, don't create a
1878 * consumer-supplier link from the parent to its child
1879 * device. Such a dependency is impossible.
1880 */
1881 if (con_dev &&
1882 fwnode_is_ancestor_of(con_dev->fwnode, fwnode)) {
1883 put_device(con_dev);
1884 con_dev = NULL;
1885 } else {
1886 own_link = false;
Saravana Kannanb90fb8f2020-12-17 19:17:01 -08001887 dl_flags = FW_DEVLINK_FLAGS_PERMISSIVE;
Saravana Kannanf9aa4602020-11-20 18:02:31 -08001888 }
1889 }
1890
1891 if (!con_dev)
1892 continue;
1893
1894 ret = fw_devlink_create_devlink(con_dev, fwnode, dl_flags);
1895 put_device(con_dev);
1896 if (!own_link || ret == -EAGAIN)
1897 continue;
1898
Saravana Kannan76f13082021-09-15 10:28:06 -07001899 __fwnode_link_del(link);
Saravana Kannanf9aa4602020-11-20 18:02:31 -08001900 }
1901}
1902
1903/**
1904 * __fw_devlink_link_to_suppliers - Create device links to suppliers of a device
Pierre-Louis Bossart37c52f72021-03-31 18:26:08 -05001905 * @dev: The consumer device that needs to be linked to its suppliers
1906 * @fwnode: Root of the fwnode tree that is used to create device links
Saravana Kannanf9aa4602020-11-20 18:02:31 -08001907 *
1908 * This function looks at all the supplier fwnodes of fwnode tree rooted at
1909 * @fwnode and creates device links between @dev (consumer) and all the
1910 * supplier devices of the entire fwnode tree at @fwnode.
1911 *
1912 * The function creates normal (non-SYNC_STATE_ONLY) device links between @dev
1913 * and the real suppliers of @dev. Once these device links are created, the
1914 * fwnode links are deleted. When such device links are successfully created,
1915 * this function is called recursively on those supplier devices. This is
1916 * needed to detect and break some invalid cycles in fwnode links. See
1917 * fw_devlink_create_devlink() for more details.
1918 *
1919 * In addition, it also looks at all the suppliers of the entire fwnode tree
1920 * because some of the child devices of @dev that have not been added yet
1921 * (because @dev hasn't probed) might already have their suppliers added to
1922 * driver core. So, this function creates SYNC_STATE_ONLY device links between
1923 * @dev (consumer) and these suppliers to make sure they don't execute their
1924 * sync_state() callbacks before these child devices have a chance to create
1925 * their device links. The fwnode links that correspond to the child devices
1926 * aren't delete because they are needed later to create the device links
1927 * between the real consumer and supplier devices.
1928 */
1929static void __fw_devlink_link_to_suppliers(struct device *dev,
1930 struct fwnode_handle *fwnode)
1931{
1932 bool own_link = (dev->fwnode == fwnode);
1933 struct fwnode_link *link, *tmp;
1934 struct fwnode_handle *child = NULL;
1935 u32 dl_flags;
1936
1937 if (own_link)
1938 dl_flags = fw_devlink_get_flags();
1939 else
Saravana Kannanb90fb8f2020-12-17 19:17:01 -08001940 dl_flags = FW_DEVLINK_FLAGS_PERMISSIVE;
Saravana Kannanf9aa4602020-11-20 18:02:31 -08001941
1942 list_for_each_entry_safe(link, tmp, &fwnode->suppliers, c_hook) {
1943 int ret;
1944 struct device *sup_dev;
1945 struct fwnode_handle *sup = link->supplier;
1946
1947 ret = fw_devlink_create_devlink(dev, sup, dl_flags);
1948 if (!own_link || ret == -EAGAIN)
1949 continue;
1950
Saravana Kannan76f13082021-09-15 10:28:06 -07001951 __fwnode_link_del(link);
Saravana Kannanf9aa4602020-11-20 18:02:31 -08001952
1953 /* If no device link was created, nothing more to do. */
1954 if (ret)
1955 continue;
1956
1957 /*
1958 * If a device link was successfully created to a supplier, we
1959 * now need to try and link the supplier to all its suppliers.
1960 *
1961 * This is needed to detect and delete false dependencies in
1962 * fwnode links that haven't been converted to a device link
1963 * yet. See comments in fw_devlink_create_devlink() for more
1964 * details on the false dependency.
1965 *
1966 * Without deleting these false dependencies, some devices will
1967 * never probe because they'll keep waiting for their false
1968 * dependency fwnode links to be converted to device links.
1969 */
1970 sup_dev = get_dev_from_fwnode(sup);
1971 __fw_devlink_link_to_suppliers(sup_dev, sup_dev->fwnode);
1972 put_device(sup_dev);
1973 }
1974
1975 /*
1976 * Make "proxy" SYNC_STATE_ONLY device links to represent the needs of
1977 * all the descendants. This proxy link step is needed to handle the
1978 * case where the supplier is added before the consumer's parent device
1979 * (@dev).
1980 */
1981 while ((child = fwnode_get_next_available_child_node(fwnode, child)))
1982 __fw_devlink_link_to_suppliers(dev, child);
1983}
1984
Saravana Kannan5f5377e2020-05-14 22:34:58 -07001985static void fw_devlink_link_device(struct device *dev)
1986{
Saravana Kannanf9aa4602020-11-20 18:02:31 -08001987 struct fwnode_handle *fwnode = dev->fwnode;
Saravana Kannan5f5377e2020-05-14 22:34:58 -07001988
Saravana Kannanf9aa4602020-11-20 18:02:31 -08001989 if (!fw_devlink_flags)
1990 return;
Saravana Kannanc84b9092020-11-20 18:02:21 -08001991
Saravana Kannanf9aa4602020-11-20 18:02:31 -08001992 fw_devlink_parse_fwtree(fwnode);
1993
1994 mutex_lock(&fwnode_link_lock);
1995 __fw_devlink_link_to_consumers(dev);
1996 __fw_devlink_link_to_suppliers(dev, fwnode);
1997 mutex_unlock(&fwnode_link_lock);
Saravana Kannan716a7a22020-05-14 22:34:59 -07001998}
Saravana Kannanc84b9092020-11-20 18:02:21 -08001999
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01002000/* Device links support end. */
2001
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002002int (*platform_notify)(struct device *dev) = NULL;
2003int (*platform_notify_remove)(struct device *dev) = NULL;
Dan Williamse105b8b2008-04-21 10:51:07 -07002004static struct kobject *dev_kobj;
2005struct kobject *sysfs_dev_char_kobj;
2006struct kobject *sysfs_dev_block_kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007
Rafael J. Wysocki5e33bc42013-08-28 21:41:01 +02002008static DEFINE_MUTEX(device_hotplug_lock);
2009
2010void lock_device_hotplug(void)
2011{
2012 mutex_lock(&device_hotplug_lock);
2013}
2014
2015void unlock_device_hotplug(void)
2016{
2017 mutex_unlock(&device_hotplug_lock);
2018}
2019
2020int lock_device_hotplug_sysfs(void)
2021{
2022 if (mutex_trylock(&device_hotplug_lock))
2023 return 0;
2024
2025 /* Avoid busy looping (5 ms of sleep should do). */
2026 msleep(5);
2027 return restart_syscall();
2028}
2029
Greg Kroah-Hartman4e886c22008-01-27 12:12:43 -08002030#ifdef CONFIG_BLOCK
2031static inline int device_is_not_partition(struct device *dev)
2032{
2033 return !(dev->type == &part_type);
2034}
2035#else
2036static inline int device_is_not_partition(struct device *dev)
2037{
2038 return 1;
2039}
2040#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041
Rafael J. Wysockib2ebd9d2021-07-12 19:28:16 +02002042static void device_platform_notify(struct device *dev)
Heikki Krogerus07de0e82018-11-09 17:21:34 +03002043{
Rafael J. Wysockib2ebd9d2021-07-12 19:28:16 +02002044 acpi_device_notify(dev);
Heikki Krogerus7847a142018-11-09 17:21:35 +03002045
Rafael J. Wysockib2ebd9d2021-07-12 19:28:16 +02002046 software_node_notify(dev);
Heikki Krogerus7847a142018-11-09 17:21:35 +03002047
Rafael J. Wysockib2ebd9d2021-07-12 19:28:16 +02002048 if (platform_notify)
Heikki Krogerus07de0e82018-11-09 17:21:34 +03002049 platform_notify(dev);
Rafael J. Wysockib2ebd9d2021-07-12 19:28:16 +02002050}
2051
2052static void device_platform_notify_remove(struct device *dev)
2053{
2054 acpi_device_notify_remove(dev);
2055
2056 software_node_notify_remove(dev);
2057
2058 if (platform_notify_remove)
Heikki Krogerus07de0e82018-11-09 17:21:34 +03002059 platform_notify_remove(dev);
Heikki Krogerus07de0e82018-11-09 17:21:34 +03002060}
2061
Alan Stern3e956372006-06-16 17:10:48 -04002062/**
2063 * dev_driver_string - Return a device's driver name, if at all possible
2064 * @dev: struct device to get the name of
2065 *
2066 * Will return the device's driver's name if it is bound to a device. If
yan9169c012012-04-20 21:08:45 +08002067 * the device is not bound to a driver, it will return the name of the bus
Alan Stern3e956372006-06-16 17:10:48 -04002068 * it is attached to. If it is not attached to a bus either, an empty
2069 * string will be returned.
2070 */
Jean Delvarebf9ca692008-07-30 12:29:21 -07002071const char *dev_driver_string(const struct device *dev)
Alan Stern3e956372006-06-16 17:10:48 -04002072{
Alan Stern35899722009-12-04 11:06:57 -05002073 struct device_driver *drv;
2074
2075 /* dev->driver can change to NULL underneath us because of unbinding,
2076 * so be careful about accessing it. dev->bus and dev->class should
2077 * never change once they are set, so they don't need special care.
2078 */
Mark Rutland6aa7de02017-10-23 14:07:29 -07002079 drv = READ_ONCE(dev->driver);
Saravana Kannane020ff62021-01-10 09:54:07 -08002080 return drv ? drv->name : dev_bus_name(dev);
Alan Stern3e956372006-06-16 17:10:48 -04002081}
Matthew Wilcox310a9222006-09-23 23:35:04 -06002082EXPORT_SYMBOL(dev_driver_string);
Alan Stern3e956372006-06-16 17:10:48 -04002083
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084#define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr)
2085
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002086static ssize_t dev_attr_show(struct kobject *kobj, struct attribute *attr,
2087 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002089 struct device_attribute *dev_attr = to_dev_attr(attr);
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +02002090 struct device *dev = kobj_to_dev(kobj);
Dmitry Torokhov4a0c20b2005-04-29 01:23:47 -05002091 ssize_t ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092
2093 if (dev_attr->show)
Yani Ioannou54b6f352005-05-17 06:39:34 -04002094 ret = dev_attr->show(dev, dev_attr, buf);
Andrew Morton815d2d52008-03-04 15:09:07 -08002095 if (ret >= (ssize_t)PAGE_SIZE) {
Sergey Senozhatskya52668c2017-12-11 21:50:21 +09002096 printk("dev_attr_show: %pS returned bad count\n",
2097 dev_attr->show);
Andrew Morton815d2d52008-03-04 15:09:07 -08002098 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 return ret;
2100}
2101
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002102static ssize_t dev_attr_store(struct kobject *kobj, struct attribute *attr,
2103 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002105 struct device_attribute *dev_attr = to_dev_attr(attr);
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +02002106 struct device *dev = kobj_to_dev(kobj);
Dmitry Torokhov4a0c20b2005-04-29 01:23:47 -05002107 ssize_t ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108
2109 if (dev_attr->store)
Yani Ioannou54b6f352005-05-17 06:39:34 -04002110 ret = dev_attr->store(dev, dev_attr, buf, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 return ret;
2112}
2113
Emese Revfy52cf25d2010-01-19 02:58:23 +01002114static const struct sysfs_ops dev_sysfs_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 .show = dev_attr_show,
2116 .store = dev_attr_store,
2117};
2118
Kay Sieversca22e562011-12-14 14:29:38 -08002119#define to_ext_attr(x) container_of(x, struct dev_ext_attribute, attr)
2120
2121ssize_t device_store_ulong(struct device *dev,
2122 struct device_attribute *attr,
2123 const char *buf, size_t size)
2124{
2125 struct dev_ext_attribute *ea = to_ext_attr(attr);
Kaitao chengf88184b2018-11-06 08:34:54 -08002126 int ret;
2127 unsigned long new;
2128
2129 ret = kstrtoul(buf, 0, &new);
2130 if (ret)
2131 return ret;
Kay Sieversca22e562011-12-14 14:29:38 -08002132 *(unsigned long *)(ea->var) = new;
2133 /* Always return full write size even if we didn't consume all */
2134 return size;
2135}
2136EXPORT_SYMBOL_GPL(device_store_ulong);
2137
2138ssize_t device_show_ulong(struct device *dev,
2139 struct device_attribute *attr,
2140 char *buf)
2141{
2142 struct dev_ext_attribute *ea = to_ext_attr(attr);
Joe Perchesaa838892020-09-16 13:40:39 -07002143 return sysfs_emit(buf, "%lx\n", *(unsigned long *)(ea->var));
Kay Sieversca22e562011-12-14 14:29:38 -08002144}
2145EXPORT_SYMBOL_GPL(device_show_ulong);
2146
2147ssize_t device_store_int(struct device *dev,
2148 struct device_attribute *attr,
2149 const char *buf, size_t size)
2150{
2151 struct dev_ext_attribute *ea = to_ext_attr(attr);
Kaitao chengf88184b2018-11-06 08:34:54 -08002152 int ret;
2153 long new;
2154
2155 ret = kstrtol(buf, 0, &new);
2156 if (ret)
2157 return ret;
2158
2159 if (new > INT_MAX || new < INT_MIN)
Kay Sieversca22e562011-12-14 14:29:38 -08002160 return -EINVAL;
2161 *(int *)(ea->var) = new;
2162 /* Always return full write size even if we didn't consume all */
2163 return size;
2164}
2165EXPORT_SYMBOL_GPL(device_store_int);
2166
2167ssize_t device_show_int(struct device *dev,
2168 struct device_attribute *attr,
2169 char *buf)
2170{
2171 struct dev_ext_attribute *ea = to_ext_attr(attr);
2172
Joe Perchesaa838892020-09-16 13:40:39 -07002173 return sysfs_emit(buf, "%d\n", *(int *)(ea->var));
Kay Sieversca22e562011-12-14 14:29:38 -08002174}
2175EXPORT_SYMBOL_GPL(device_show_int);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176
Borislav Petkov91872392012-10-09 19:52:05 +02002177ssize_t device_store_bool(struct device *dev, struct device_attribute *attr,
2178 const char *buf, size_t size)
2179{
2180 struct dev_ext_attribute *ea = to_ext_attr(attr);
2181
2182 if (strtobool(buf, ea->var) < 0)
2183 return -EINVAL;
2184
2185 return size;
2186}
2187EXPORT_SYMBOL_GPL(device_store_bool);
2188
2189ssize_t device_show_bool(struct device *dev, struct device_attribute *attr,
2190 char *buf)
2191{
2192 struct dev_ext_attribute *ea = to_ext_attr(attr);
2193
Joe Perchesaa838892020-09-16 13:40:39 -07002194 return sysfs_emit(buf, "%d\n", *(bool *)(ea->var));
Borislav Petkov91872392012-10-09 19:52:05 +02002195}
2196EXPORT_SYMBOL_GPL(device_show_bool);
2197
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198/**
Robert P. J. Dayf8878dc2013-06-01 20:17:34 -04002199 * device_release - free device structure.
2200 * @kobj: device's kobject.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 *
Robert P. J. Dayf8878dc2013-06-01 20:17:34 -04002202 * This is called once the reference count for the object
2203 * reaches 0. We forward the call to the device's release
2204 * method, which should handle actually freeing the structure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002206static void device_release(struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +02002208 struct device *dev = kobj_to_dev(kobj);
Greg Kroah-Hartmanfb069a52008-12-16 12:23:36 -08002209 struct device_private *p = dev->p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210
Ming Leia525a3d2012-07-25 01:42:29 +08002211 /*
2212 * Some platform devices are driven without driver attached
2213 * and managed resources may have been acquired. Make sure
2214 * all resources are released.
2215 *
2216 * Drivers still can add resources into device after device
2217 * is deleted but alive, so release devres here to avoid
2218 * possible memory leak.
2219 */
2220 devres_release_all(dev);
2221
Jim Quinlane0d07272020-09-17 18:43:40 +02002222 kfree(dev->dma_range_map);
2223
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224 if (dev->release)
2225 dev->release(dev);
Kay Sieversf9f852d2006-10-07 21:54:55 +02002226 else if (dev->type && dev->type->release)
2227 dev->type->release(dev);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07002228 else if (dev->class && dev->class->dev_release)
2229 dev->class->dev_release(dev);
Arjan van de Venf810a5c2008-07-25 19:45:39 -07002230 else
Mauro Carvalho Chehab0c1bc6b2020-04-14 18:48:37 +02002231 WARN(1, KERN_ERR "Device '%s' does not have a release() function, it is broken and must be fixed. See Documentation/core-api/kobject.rst.\n",
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01002232 dev_name(dev));
Greg Kroah-Hartmanfb069a52008-12-16 12:23:36 -08002233 kfree(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234}
2235
Eric W. Biedermanbc451f22010-03-30 11:31:25 -07002236static const void *device_namespace(struct kobject *kobj)
2237{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +02002238 struct device *dev = kobj_to_dev(kobj);
Eric W. Biedermanbc451f22010-03-30 11:31:25 -07002239 const void *ns = NULL;
2240
2241 if (dev->class && dev->class->ns_type)
2242 ns = dev->class->namespace(dev);
2243
2244 return ns;
2245}
2246
Dmitry Torokhov9944e892018-07-20 21:56:50 +00002247static void device_get_ownership(struct kobject *kobj, kuid_t *uid, kgid_t *gid)
2248{
2249 struct device *dev = kobj_to_dev(kobj);
2250
2251 if (dev->class && dev->class->get_ownership)
2252 dev->class->get_ownership(dev, uid, gid);
2253}
2254
Greg Kroah-Hartman8f4afc42007-10-11 10:47:49 -06002255static struct kobj_type device_ktype = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256 .release = device_release,
2257 .sysfs_ops = &dev_sysfs_ops,
Eric W. Biedermanbc451f22010-03-30 11:31:25 -07002258 .namespace = device_namespace,
Dmitry Torokhov9944e892018-07-20 21:56:50 +00002259 .get_ownership = device_get_ownership,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260};
2261
2262
Greg Kroah-Hartmancf6299b62021-12-27 17:39:24 +01002263static int dev_uevent_filter(struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264{
Wedson Almeida Filhoee6d3dd2021-12-24 23:13:45 +00002265 const struct kobj_type *ktype = get_ktype(kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266
Greg Kroah-Hartman8f4afc42007-10-11 10:47:49 -06002267 if (ktype == &device_ktype) {
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +02002268 struct device *dev = kobj_to_dev(kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 if (dev->bus)
2270 return 1;
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07002271 if (dev->class)
2272 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273 }
2274 return 0;
2275}
2276
Greg Kroah-Hartmancf6299b62021-12-27 17:39:24 +01002277static const char *dev_uevent_name(struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +02002279 struct device *dev = kobj_to_dev(kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07002281 if (dev->bus)
2282 return dev->bus->name;
2283 if (dev->class)
2284 return dev->class->name;
2285 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286}
2287
Greg Kroah-Hartmancf6299b62021-12-27 17:39:24 +01002288static int dev_uevent(struct kobject *kobj, struct kobj_uevent_env *env)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +02002290 struct device *dev = kobj_to_dev(kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291 int retval = 0;
2292
Kay Sievers6fcf53a2009-04-30 15:23:42 +02002293 /* add device node properties if present */
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07002294 if (MAJOR(dev->devt)) {
Kay Sievers6fcf53a2009-04-30 15:23:42 +02002295 const char *tmp;
2296 const char *name;
Al Viro2c9ede52011-07-23 20:24:48 -04002297 umode_t mode = 0;
Greg Kroah-Hartman4e4098a2013-04-11 11:43:29 -07002298 kuid_t uid = GLOBAL_ROOT_UID;
2299 kgid_t gid = GLOBAL_ROOT_GID;
Kay Sievers6fcf53a2009-04-30 15:23:42 +02002300
Kay Sievers7eff2e72007-08-14 15:15:12 +02002301 add_uevent_var(env, "MAJOR=%u", MAJOR(dev->devt));
2302 add_uevent_var(env, "MINOR=%u", MINOR(dev->devt));
Kay Sievers3c2670e2013-04-06 09:56:00 -07002303 name = device_get_devnode(dev, &mode, &uid, &gid, &tmp);
Kay Sievers6fcf53a2009-04-30 15:23:42 +02002304 if (name) {
2305 add_uevent_var(env, "DEVNAME=%s", name);
Kay Sieverse454cea2009-09-18 23:01:12 +02002306 if (mode)
2307 add_uevent_var(env, "DEVMODE=%#o", mode & 0777);
Greg Kroah-Hartman4e4098a2013-04-11 11:43:29 -07002308 if (!uid_eq(uid, GLOBAL_ROOT_UID))
2309 add_uevent_var(env, "DEVUID=%u", from_kuid(&init_user_ns, uid));
2310 if (!gid_eq(gid, GLOBAL_ROOT_GID))
2311 add_uevent_var(env, "DEVGID=%u", from_kgid(&init_user_ns, gid));
Kay Sievers3c2670e2013-04-06 09:56:00 -07002312 kfree(tmp);
Kay Sievers6fcf53a2009-04-30 15:23:42 +02002313 }
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07002314 }
2315
Kay Sievers414264f2007-03-12 21:08:57 +01002316 if (dev->type && dev->type->name)
Kay Sievers7eff2e72007-08-14 15:15:12 +02002317 add_uevent_var(env, "DEVTYPE=%s", dev->type->name);
Kay Sievers414264f2007-03-12 21:08:57 +01002318
Kay Sievers239378f2006-10-07 21:54:55 +02002319 if (dev->driver)
Kay Sievers7eff2e72007-08-14 15:15:12 +02002320 add_uevent_var(env, "DRIVER=%s", dev->driver->name);
Kay Sievers239378f2006-10-07 21:54:55 +02002321
Grant Likely07d57a32012-02-01 11:22:22 -07002322 /* Add common DT information about the device */
2323 of_device_uevent(dev, env);
2324
Kay Sievers7eff2e72007-08-14 15:15:12 +02002325 /* have the bus specific function add its stuff */
Kay Sievers312c0042005-11-16 09:00:00 +01002326 if (dev->bus && dev->bus->uevent) {
Kay Sievers7eff2e72007-08-14 15:15:12 +02002327 retval = dev->bus->uevent(dev, env);
Kay Sieversf9f852d2006-10-07 21:54:55 +02002328 if (retval)
Greg Kroah-Hartman7dc72b22007-11-28 23:49:41 -08002329 pr_debug("device: '%s': %s: bus uevent() returned %d\n",
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01002330 dev_name(dev), __func__, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331 }
2332
Kay Sievers7eff2e72007-08-14 15:15:12 +02002333 /* have the class specific function add its stuff */
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07002334 if (dev->class && dev->class->dev_uevent) {
Kay Sievers7eff2e72007-08-14 15:15:12 +02002335 retval = dev->class->dev_uevent(dev, env);
Kay Sieversf9f852d2006-10-07 21:54:55 +02002336 if (retval)
Greg Kroah-Hartman7dc72b22007-11-28 23:49:41 -08002337 pr_debug("device: '%s': %s: class uevent() "
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01002338 "returned %d\n", dev_name(dev),
Harvey Harrison2b3a3022008-03-04 16:41:05 -08002339 __func__, retval);
Kay Sieversf9f852d2006-10-07 21:54:55 +02002340 }
2341
Stefan Weileef35c22010-08-06 21:11:15 +02002342 /* have the device type specific function add its stuff */
Kay Sieversf9f852d2006-10-07 21:54:55 +02002343 if (dev->type && dev->type->uevent) {
Kay Sievers7eff2e72007-08-14 15:15:12 +02002344 retval = dev->type->uevent(dev, env);
Kay Sieversf9f852d2006-10-07 21:54:55 +02002345 if (retval)
Greg Kroah-Hartman7dc72b22007-11-28 23:49:41 -08002346 pr_debug("device: '%s': %s: dev_type uevent() "
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01002347 "returned %d\n", dev_name(dev),
Harvey Harrison2b3a3022008-03-04 16:41:05 -08002348 __func__, retval);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07002349 }
2350
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 return retval;
2352}
2353
Emese Revfy9cd43612009-12-31 14:52:51 +01002354static const struct kset_uevent_ops device_uevent_ops = {
Kay Sievers312c0042005-11-16 09:00:00 +01002355 .filter = dev_uevent_filter,
2356 .name = dev_uevent_name,
2357 .uevent = dev_uevent,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358};
2359
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07002360static ssize_t uevent_show(struct device *dev, struct device_attribute *attr,
Kay Sievers16574dc2007-04-06 01:40:38 +02002361 char *buf)
2362{
2363 struct kobject *top_kobj;
2364 struct kset *kset;
Kay Sievers7eff2e72007-08-14 15:15:12 +02002365 struct kobj_uevent_env *env = NULL;
Kay Sievers16574dc2007-04-06 01:40:38 +02002366 int i;
Joe Perches948b3ed2020-09-16 13:40:42 -07002367 int len = 0;
Kay Sievers16574dc2007-04-06 01:40:38 +02002368 int retval;
2369
2370 /* search the kset, the device belongs to */
2371 top_kobj = &dev->kobj;
Kay Sievers5c5daf62007-08-12 20:43:55 +02002372 while (!top_kobj->kset && top_kobj->parent)
2373 top_kobj = top_kobj->parent;
Kay Sievers16574dc2007-04-06 01:40:38 +02002374 if (!top_kobj->kset)
2375 goto out;
Kay Sievers5c5daf62007-08-12 20:43:55 +02002376
Kay Sievers16574dc2007-04-06 01:40:38 +02002377 kset = top_kobj->kset;
2378 if (!kset->uevent_ops || !kset->uevent_ops->uevent)
2379 goto out;
2380
2381 /* respect filter */
2382 if (kset->uevent_ops && kset->uevent_ops->filter)
Greg Kroah-Hartmancf6299b62021-12-27 17:39:24 +01002383 if (!kset->uevent_ops->filter(&dev->kobj))
Kay Sievers16574dc2007-04-06 01:40:38 +02002384 goto out;
2385
Kay Sievers7eff2e72007-08-14 15:15:12 +02002386 env = kzalloc(sizeof(struct kobj_uevent_env), GFP_KERNEL);
2387 if (!env)
Greg Kroah-Hartmanc7308c82007-05-02 14:14:11 +02002388 return -ENOMEM;
2389
Kay Sievers16574dc2007-04-06 01:40:38 +02002390 /* let the kset specific function add its keys */
Greg Kroah-Hartmancf6299b62021-12-27 17:39:24 +01002391 retval = kset->uevent_ops->uevent(&dev->kobj, env);
Kay Sievers16574dc2007-04-06 01:40:38 +02002392 if (retval)
2393 goto out;
2394
2395 /* copy keys to file */
Kay Sievers7eff2e72007-08-14 15:15:12 +02002396 for (i = 0; i < env->envp_idx; i++)
Joe Perches948b3ed2020-09-16 13:40:42 -07002397 len += sysfs_emit_at(buf, len, "%s\n", env->envp[i]);
Kay Sievers16574dc2007-04-06 01:40:38 +02002398out:
Kay Sievers7eff2e72007-08-14 15:15:12 +02002399 kfree(env);
Joe Perches948b3ed2020-09-16 13:40:42 -07002400 return len;
Kay Sievers16574dc2007-04-06 01:40:38 +02002401}
2402
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07002403static ssize_t uevent_store(struct device *dev, struct device_attribute *attr,
Kay Sieversa7fd6702005-10-01 14:49:43 +02002404 const char *buf, size_t count)
2405{
Peter Rajnohadf44b472018-12-05 12:27:44 +01002406 int rc;
2407
2408 rc = kobject_synth_uevent(&dev->kobj, buf, count);
2409
2410 if (rc) {
Peter Rajnohaf36776f2017-05-09 15:22:30 +02002411 dev_err(dev, "uevent: failed to send synthetic uevent\n");
Peter Rajnohadf44b472018-12-05 12:27:44 +01002412 return rc;
2413 }
Kay Sievers60a96a52007-07-08 22:29:26 +02002414
Kay Sieversa7fd6702005-10-01 14:49:43 +02002415 return count;
2416}
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07002417static DEVICE_ATTR_RW(uevent);
Kay Sieversa7fd6702005-10-01 14:49:43 +02002418
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07002419static ssize_t online_show(struct device *dev, struct device_attribute *attr,
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02002420 char *buf)
2421{
2422 bool val;
2423
Rafael J. Wysocki5e33bc42013-08-28 21:41:01 +02002424 device_lock(dev);
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02002425 val = !dev->offline;
Rafael J. Wysocki5e33bc42013-08-28 21:41:01 +02002426 device_unlock(dev);
Joe Perchesaa838892020-09-16 13:40:39 -07002427 return sysfs_emit(buf, "%u\n", val);
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02002428}
2429
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07002430static ssize_t online_store(struct device *dev, struct device_attribute *attr,
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02002431 const char *buf, size_t count)
2432{
2433 bool val;
2434 int ret;
2435
2436 ret = strtobool(buf, &val);
2437 if (ret < 0)
2438 return ret;
2439
Rafael J. Wysocki5e33bc42013-08-28 21:41:01 +02002440 ret = lock_device_hotplug_sysfs();
2441 if (ret)
2442 return ret;
2443
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02002444 ret = val ? device_online(dev) : device_offline(dev);
2445 unlock_device_hotplug();
2446 return ret < 0 ? ret : count;
2447}
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07002448static DEVICE_ATTR_RW(online);
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02002449
Rajat Jain70f400d2021-05-24 10:18:11 -07002450static ssize_t removable_show(struct device *dev, struct device_attribute *attr,
2451 char *buf)
2452{
2453 const char *loc;
2454
2455 switch (dev->removable) {
2456 case DEVICE_REMOVABLE:
2457 loc = "removable";
2458 break;
2459 case DEVICE_FIXED:
2460 loc = "fixed";
2461 break;
2462 default:
2463 loc = "unknown";
2464 }
2465 return sysfs_emit(buf, "%s\n", loc);
2466}
2467static DEVICE_ATTR_RO(removable);
2468
Greg Kroah-Hartmanfa6fdb32013-08-08 15:22:55 -07002469int device_add_groups(struct device *dev, const struct attribute_group **groups)
Dmitry Torokhov621a1672007-03-10 01:37:34 -05002470{
Greg Kroah-Hartman3e9b2ba2013-08-21 13:47:50 -07002471 return sysfs_create_groups(&dev->kobj, groups);
Dmitry Torokhov621a1672007-03-10 01:37:34 -05002472}
Dmitry Torokhova7670d42017-07-19 17:24:31 -07002473EXPORT_SYMBOL_GPL(device_add_groups);
Dmitry Torokhov621a1672007-03-10 01:37:34 -05002474
Greg Kroah-Hartmanfa6fdb32013-08-08 15:22:55 -07002475void device_remove_groups(struct device *dev,
2476 const struct attribute_group **groups)
Dmitry Torokhov621a1672007-03-10 01:37:34 -05002477{
Greg Kroah-Hartman3e9b2ba2013-08-21 13:47:50 -07002478 sysfs_remove_groups(&dev->kobj, groups);
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -07002479}
Dmitry Torokhova7670d42017-07-19 17:24:31 -07002480EXPORT_SYMBOL_GPL(device_remove_groups);
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -07002481
Dmitry Torokhov57b8ff02017-07-19 17:24:33 -07002482union device_attr_group_devres {
2483 const struct attribute_group *group;
2484 const struct attribute_group **groups;
2485};
2486
2487static int devm_attr_group_match(struct device *dev, void *res, void *data)
2488{
2489 return ((union device_attr_group_devres *)res)->group == data;
2490}
2491
2492static void devm_attr_group_remove(struct device *dev, void *res)
2493{
2494 union device_attr_group_devres *devres = res;
2495 const struct attribute_group *group = devres->group;
2496
2497 dev_dbg(dev, "%s: removing group %p\n", __func__, group);
2498 sysfs_remove_group(&dev->kobj, group);
2499}
2500
2501static void devm_attr_groups_remove(struct device *dev, void *res)
2502{
2503 union device_attr_group_devres *devres = res;
2504 const struct attribute_group **groups = devres->groups;
2505
2506 dev_dbg(dev, "%s: removing groups %p\n", __func__, groups);
2507 sysfs_remove_groups(&dev->kobj, groups);
2508}
2509
2510/**
2511 * devm_device_add_group - given a device, create a managed attribute group
2512 * @dev: The device to create the group for
2513 * @grp: The attribute group to create
2514 *
2515 * This function creates a group for the first time. It will explicitly
2516 * warn and error if any of the attribute files being created already exist.
2517 *
2518 * Returns 0 on success or error code on failure.
2519 */
2520int devm_device_add_group(struct device *dev, const struct attribute_group *grp)
2521{
2522 union device_attr_group_devres *devres;
2523 int error;
2524
2525 devres = devres_alloc(devm_attr_group_remove,
2526 sizeof(*devres), GFP_KERNEL);
2527 if (!devres)
2528 return -ENOMEM;
2529
2530 error = sysfs_create_group(&dev->kobj, grp);
2531 if (error) {
2532 devres_free(devres);
2533 return error;
2534 }
2535
2536 devres->group = grp;
2537 devres_add(dev, devres);
2538 return 0;
2539}
2540EXPORT_SYMBOL_GPL(devm_device_add_group);
2541
2542/**
2543 * devm_device_remove_group: remove a managed group from a device
2544 * @dev: device to remove the group from
2545 * @grp: group to remove
2546 *
2547 * This function removes a group of attributes from a device. The attributes
2548 * previously have to have been created for this group, otherwise it will fail.
2549 */
2550void devm_device_remove_group(struct device *dev,
2551 const struct attribute_group *grp)
2552{
2553 WARN_ON(devres_release(dev, devm_attr_group_remove,
2554 devm_attr_group_match,
2555 /* cast away const */ (void *)grp));
2556}
2557EXPORT_SYMBOL_GPL(devm_device_remove_group);
2558
2559/**
2560 * devm_device_add_groups - create a bunch of managed attribute groups
2561 * @dev: The device to create the group for
2562 * @groups: The attribute groups to create, NULL terminated
2563 *
2564 * This function creates a bunch of managed attribute groups. If an error
2565 * occurs when creating a group, all previously created groups will be
2566 * removed, unwinding everything back to the original state when this
2567 * function was called. It will explicitly warn and error if any of the
2568 * attribute files being created already exist.
2569 *
2570 * Returns 0 on success or error code from sysfs_create_group on failure.
2571 */
2572int devm_device_add_groups(struct device *dev,
2573 const struct attribute_group **groups)
2574{
2575 union device_attr_group_devres *devres;
2576 int error;
2577
2578 devres = devres_alloc(devm_attr_groups_remove,
2579 sizeof(*devres), GFP_KERNEL);
2580 if (!devres)
2581 return -ENOMEM;
2582
2583 error = sysfs_create_groups(&dev->kobj, groups);
2584 if (error) {
2585 devres_free(devres);
2586 return error;
2587 }
2588
2589 devres->groups = groups;
2590 devres_add(dev, devres);
2591 return 0;
2592}
2593EXPORT_SYMBOL_GPL(devm_device_add_groups);
2594
2595/**
2596 * devm_device_remove_groups - remove a list of managed groups
2597 *
2598 * @dev: The device for the groups to be removed from
2599 * @groups: NULL terminated list of groups to be removed
2600 *
2601 * If groups is not NULL, remove the specified groups from the device.
2602 */
2603void devm_device_remove_groups(struct device *dev,
2604 const struct attribute_group **groups)
2605{
2606 WARN_ON(devres_release(dev, devm_attr_groups_remove,
2607 devm_attr_group_match,
2608 /* cast away const */ (void *)groups));
2609}
2610EXPORT_SYMBOL_GPL(devm_device_remove_groups);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07002612static int device_add_attrs(struct device *dev)
2613{
2614 struct class *class = dev->class;
Stephen Hemmingeraed65af2011-03-28 09:12:52 -07002615 const struct device_type *type = dev->type;
Dmitry Torokhov621a1672007-03-10 01:37:34 -05002616 int error;
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07002617
Dmitry Torokhov621a1672007-03-10 01:37:34 -05002618 if (class) {
Greg Kroah-Hartmand05a6f92013-07-14 16:05:58 -07002619 error = device_add_groups(dev, class->dev_groups);
Kay Sieversf9f852d2006-10-07 21:54:55 +02002620 if (error)
Dmitry Torokhov621a1672007-03-10 01:37:34 -05002621 return error;
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07002622 }
Kay Sieversf9f852d2006-10-07 21:54:55 +02002623
Dmitry Torokhov621a1672007-03-10 01:37:34 -05002624 if (type) {
2625 error = device_add_groups(dev, type->groups);
Kay Sieversf9f852d2006-10-07 21:54:55 +02002626 if (error)
Greg Kroah-Hartmana6b01de2013-10-05 18:19:30 -07002627 goto err_remove_class_groups;
Kay Sieversf9f852d2006-10-07 21:54:55 +02002628 }
2629
Dmitry Torokhov621a1672007-03-10 01:37:34 -05002630 error = device_add_groups(dev, dev->groups);
2631 if (error)
2632 goto err_remove_type_groups;
2633
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02002634 if (device_supports_offline(dev) && !dev->offline_disabled) {
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07002635 error = device_create_file(dev, &dev_attr_online);
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02002636 if (error)
Rafael J. Wysockiecfbf6f2013-12-12 06:11:02 +01002637 goto err_remove_dev_groups;
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02002638 }
2639
Saravana Kannan25ac86c2020-11-20 18:02:28 -08002640 if (fw_devlink_flags && !fw_devlink_is_permissive() && dev->fwnode) {
Saravana Kannanda6d6472020-05-21 12:18:00 -07002641 error = device_create_file(dev, &dev_attr_waiting_for_supplier);
2642 if (error)
2643 goto err_remove_dev_online;
2644 }
2645
Rajat Jain70f400d2021-05-24 10:18:11 -07002646 if (dev_removable_is_valid(dev)) {
2647 error = device_create_file(dev, &dev_attr_removable);
2648 if (error)
2649 goto err_remove_dev_waiting_for_supplier;
2650 }
2651
Dmitry Torokhov621a1672007-03-10 01:37:34 -05002652 return 0;
2653
Rajat Jain70f400d2021-05-24 10:18:11 -07002654 err_remove_dev_waiting_for_supplier:
2655 device_remove_file(dev, &dev_attr_waiting_for_supplier);
Saravana Kannanda6d6472020-05-21 12:18:00 -07002656 err_remove_dev_online:
2657 device_remove_file(dev, &dev_attr_online);
Rafael J. Wysockiecfbf6f2013-12-12 06:11:02 +01002658 err_remove_dev_groups:
2659 device_remove_groups(dev, dev->groups);
Dmitry Torokhov621a1672007-03-10 01:37:34 -05002660 err_remove_type_groups:
2661 if (type)
2662 device_remove_groups(dev, type->groups);
Greg Kroah-Hartmand05a6f92013-07-14 16:05:58 -07002663 err_remove_class_groups:
2664 if (class)
2665 device_remove_groups(dev, class->dev_groups);
Dmitry Torokhov621a1672007-03-10 01:37:34 -05002666
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07002667 return error;
2668}
2669
2670static void device_remove_attrs(struct device *dev)
2671{
2672 struct class *class = dev->class;
Stephen Hemmingeraed65af2011-03-28 09:12:52 -07002673 const struct device_type *type = dev->type;
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07002674
Rajat Jain70f400d2021-05-24 10:18:11 -07002675 device_remove_file(dev, &dev_attr_removable);
Saravana Kannanda6d6472020-05-21 12:18:00 -07002676 device_remove_file(dev, &dev_attr_waiting_for_supplier);
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07002677 device_remove_file(dev, &dev_attr_online);
Dmitry Torokhov621a1672007-03-10 01:37:34 -05002678 device_remove_groups(dev, dev->groups);
Kay Sieversf9f852d2006-10-07 21:54:55 +02002679
Dmitry Torokhov621a1672007-03-10 01:37:34 -05002680 if (type)
2681 device_remove_groups(dev, type->groups);
2682
Greg Kroah-Hartmana6b01de2013-10-05 18:19:30 -07002683 if (class)
Greg Kroah-Hartmand05a6f92013-07-14 16:05:58 -07002684 device_remove_groups(dev, class->dev_groups);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07002685}
2686
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07002687static ssize_t dev_show(struct device *dev, struct device_attribute *attr,
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07002688 char *buf)
2689{
2690 return print_dev_t(buf, dev->devt);
2691}
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07002692static DEVICE_ATTR_RO(dev);
Tejun Heoad6a1e12007-06-14 03:45:17 +09002693
Kay Sieversca22e562011-12-14 14:29:38 -08002694/* /sys/devices/ */
Greg Kroah-Hartman881c6cfd2007-11-01 09:29:06 -06002695struct kset *devices_kset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697/**
Grygorii Strashko52cdbdd2015-07-27 20:43:01 +03002698 * devices_kset_move_before - Move device in the devices_kset's list.
2699 * @deva: Device to move.
2700 * @devb: Device @deva should come before.
2701 */
2702static void devices_kset_move_before(struct device *deva, struct device *devb)
2703{
2704 if (!devices_kset)
2705 return;
2706 pr_debug("devices_kset: Moving %s before %s\n",
2707 dev_name(deva), dev_name(devb));
2708 spin_lock(&devices_kset->list_lock);
2709 list_move_tail(&deva->kobj.entry, &devb->kobj.entry);
2710 spin_unlock(&devices_kset->list_lock);
2711}
2712
2713/**
2714 * devices_kset_move_after - Move device in the devices_kset's list.
2715 * @deva: Device to move
2716 * @devb: Device @deva should come after.
2717 */
2718static void devices_kset_move_after(struct device *deva, struct device *devb)
2719{
2720 if (!devices_kset)
2721 return;
2722 pr_debug("devices_kset: Moving %s after %s\n",
2723 dev_name(deva), dev_name(devb));
2724 spin_lock(&devices_kset->list_lock);
2725 list_move(&deva->kobj.entry, &devb->kobj.entry);
2726 spin_unlock(&devices_kset->list_lock);
2727}
2728
2729/**
2730 * devices_kset_move_last - move the device to the end of devices_kset's list.
2731 * @dev: device to move
2732 */
2733void devices_kset_move_last(struct device *dev)
2734{
2735 if (!devices_kset)
2736 return;
2737 pr_debug("devices_kset: Moving %s to end of list\n", dev_name(dev));
2738 spin_lock(&devices_kset->list_lock);
2739 list_move_tail(&dev->kobj.entry, &devices_kset->list);
2740 spin_unlock(&devices_kset->list_lock);
2741}
2742
2743/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002744 * device_create_file - create sysfs attribute file for device.
2745 * @dev: device.
2746 * @attr: device attribute descriptor.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747 */
Phil Carmody26579ab2009-12-18 15:34:19 +02002748int device_create_file(struct device *dev,
2749 const struct device_attribute *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750{
2751 int error = 0;
Felipe Balbi8f46baa2013-02-20 10:31:42 +02002752
2753 if (dev) {
2754 WARN(((attr->attr.mode & S_IWUGO) && !attr->store),
dyoung@redhat.com97521972013-05-16 14:31:30 +08002755 "Attribute %s: write permission without 'store'\n",
2756 attr->attr.name);
Felipe Balbi8f46baa2013-02-20 10:31:42 +02002757 WARN(((attr->attr.mode & S_IRUGO) && !attr->show),
dyoung@redhat.com97521972013-05-16 14:31:30 +08002758 "Attribute %s: read permission without 'show'\n",
2759 attr->attr.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760 error = sysfs_create_file(&dev->kobj, &attr->attr);
Felipe Balbi8f46baa2013-02-20 10:31:42 +02002761 }
2762
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763 return error;
2764}
David Graham White86df2682013-07-21 20:41:14 -04002765EXPORT_SYMBOL_GPL(device_create_file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766
2767/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002768 * device_remove_file - remove sysfs attribute file.
2769 * @dev: device.
2770 * @attr: device attribute descriptor.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771 */
Phil Carmody26579ab2009-12-18 15:34:19 +02002772void device_remove_file(struct device *dev,
2773 const struct device_attribute *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774{
Cornelia Huck0c98b192008-01-31 10:39:38 +01002775 if (dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776 sysfs_remove_file(&dev->kobj, &attr->attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777}
David Graham White86df2682013-07-21 20:41:14 -04002778EXPORT_SYMBOL_GPL(device_remove_file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779
Greg Kroah-Hartman2589f1882006-09-19 09:39:19 -07002780/**
Tejun Heo6b0afc22014-02-03 14:03:01 -05002781 * device_remove_file_self - remove sysfs attribute file from its own method.
2782 * @dev: device.
2783 * @attr: device attribute descriptor.
2784 *
2785 * See kernfs_remove_self() for details.
2786 */
2787bool device_remove_file_self(struct device *dev,
2788 const struct device_attribute *attr)
2789{
2790 if (dev)
2791 return sysfs_remove_file_self(&dev->kobj, &attr->attr);
2792 else
2793 return false;
2794}
2795EXPORT_SYMBOL_GPL(device_remove_file_self);
2796
2797/**
Greg Kroah-Hartman2589f1882006-09-19 09:39:19 -07002798 * device_create_bin_file - create sysfs binary attribute file for device.
2799 * @dev: device.
2800 * @attr: device binary attribute descriptor.
2801 */
Phil Carmody66ecb922009-12-18 15:34:20 +02002802int device_create_bin_file(struct device *dev,
2803 const struct bin_attribute *attr)
Greg Kroah-Hartman2589f1882006-09-19 09:39:19 -07002804{
2805 int error = -EINVAL;
2806 if (dev)
2807 error = sysfs_create_bin_file(&dev->kobj, attr);
2808 return error;
2809}
2810EXPORT_SYMBOL_GPL(device_create_bin_file);
2811
2812/**
2813 * device_remove_bin_file - remove sysfs binary attribute file
2814 * @dev: device.
2815 * @attr: device binary attribute descriptor.
2816 */
Phil Carmody66ecb922009-12-18 15:34:20 +02002817void device_remove_bin_file(struct device *dev,
2818 const struct bin_attribute *attr)
Greg Kroah-Hartman2589f1882006-09-19 09:39:19 -07002819{
2820 if (dev)
2821 sysfs_remove_bin_file(&dev->kobj, attr);
2822}
2823EXPORT_SYMBOL_GPL(device_remove_bin_file);
2824
James Bottomley34bb61f2005-09-06 16:56:51 -07002825static void klist_children_get(struct klist_node *n)
2826{
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08002827 struct device_private *p = to_device_private_parent(n);
2828 struct device *dev = p->device;
James Bottomley34bb61f2005-09-06 16:56:51 -07002829
2830 get_device(dev);
2831}
2832
2833static void klist_children_put(struct klist_node *n)
2834{
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08002835 struct device_private *p = to_device_private_parent(n);
2836 struct device *dev = p->device;
James Bottomley34bb61f2005-09-06 16:56:51 -07002837
2838 put_device(dev);
2839}
2840
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002842 * device_initialize - init device structure.
2843 * @dev: device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844 *
Cornelia Huck57394112008-09-03 18:26:40 +02002845 * This prepares the device for use by other layers by initializing
2846 * its fields.
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002847 * It is the first half of device_register(), if called by
Cornelia Huck57394112008-09-03 18:26:40 +02002848 * that function, though it can also be called separately, so one
2849 * may use @dev's fields. In particular, get_device()/put_device()
2850 * may be used for reference counting of @dev after calling this
2851 * function.
2852 *
Alan Sternb10d5ef2012-01-17 11:39:00 -05002853 * All fields in @dev must be initialized by the caller to 0, except
2854 * for those explicitly set to some other value. The simplest
2855 * approach is to use kzalloc() to allocate the structure containing
2856 * @dev.
2857 *
Cornelia Huck57394112008-09-03 18:26:40 +02002858 * NOTE: Use put_device() to give up your reference instead of freeing
2859 * @dev directly once you have called this function.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861void device_initialize(struct device *dev)
2862{
Greg Kroah-Hartman881c6cfd2007-11-01 09:29:06 -06002863 dev->kobj.kset = devices_kset;
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -07002864 kobject_init(&dev->kobj, &device_ktype);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865 INIT_LIST_HEAD(&dev->dma_pools);
Thomas Gleixner31427882010-01-29 20:39:02 +00002866 mutex_init(&dev->mutex);
Dan Williams87a30e12019-07-17 18:08:26 -07002867#ifdef CONFIG_PROVE_LOCKING
2868 mutex_init(&dev->lockdep_mutex);
2869#endif
Peter Zijlstra1704f472010-03-19 01:37:42 +01002870 lockdep_set_novalidate_class(&dev->mutex);
Tejun Heo9ac78492007-01-20 16:00:26 +09002871 spin_lock_init(&dev->devres_lock);
2872 INIT_LIST_HEAD(&dev->devres_head);
Alan Stern3b98aea2008-08-07 13:06:12 -04002873 device_pm_init(dev);
Max Gurtovoyd460d7f2021-10-04 16:34:52 +03002874 set_dev_node(dev, NUMA_NO_NODE);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01002875 INIT_LIST_HEAD(&dev->links.consumers);
2876 INIT_LIST_HEAD(&dev->links.suppliers);
Saravana Kannan3b052a32020-11-20 18:02:17 -08002877 INIT_LIST_HEAD(&dev->links.defer_sync);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01002878 dev->links.status = DL_DEV_NO_DRIVER;
Christoph Hellwig6d4e9a82021-02-10 10:56:39 +01002879#if defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_DEVICE) || \
2880 defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU) || \
2881 defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU_ALL)
2882 dev->dma_coherent = dma_default_coherent;
2883#endif
Claire Chang69031f52021-06-19 11:40:34 +08002884#ifdef CONFIG_SWIOTLB
Will Deacon463e8622021-07-20 14:38:24 +01002885 dev->dma_io_tlb_mem = &io_tlb_default_mem;
Claire Chang69031f52021-06-19 11:40:34 +08002886#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887}
David Graham White86df2682013-07-21 20:41:14 -04002888EXPORT_SYMBOL_GPL(device_initialize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889
Tejun Heod73ce002013-03-12 11:30:05 -07002890struct kobject *virtual_device_parent(struct device *dev)
Greg Kroah-Hartmanf0ee61a2006-10-23 10:40:54 -07002891{
Kay Sievers86406242007-03-14 03:25:56 +01002892 static struct kobject *virtual_dir = NULL;
Greg Kroah-Hartmanf0ee61a2006-10-23 10:40:54 -07002893
Kay Sievers86406242007-03-14 03:25:56 +01002894 if (!virtual_dir)
Greg Kroah-Hartman4ff6abf2007-11-05 22:24:43 -08002895 virtual_dir = kobject_create_and_add("virtual",
Greg Kroah-Hartman881c6cfd2007-11-01 09:29:06 -06002896 &devices_kset->kobj);
Greg Kroah-Hartmanf0ee61a2006-10-23 10:40:54 -07002897
Kay Sievers86406242007-03-14 03:25:56 +01002898 return virtual_dir;
Greg Kroah-Hartmanf0ee61a2006-10-23 10:40:54 -07002899}
2900
Eric W. Biedermanbc451f22010-03-30 11:31:25 -07002901struct class_dir {
2902 struct kobject kobj;
2903 struct class *class;
2904};
2905
2906#define to_class_dir(obj) container_of(obj, struct class_dir, kobj)
2907
2908static void class_dir_release(struct kobject *kobj)
2909{
2910 struct class_dir *dir = to_class_dir(kobj);
2911 kfree(dir);
2912}
2913
2914static const
2915struct kobj_ns_type_operations *class_dir_child_ns_type(struct kobject *kobj)
2916{
2917 struct class_dir *dir = to_class_dir(kobj);
2918 return dir->class->ns_type;
2919}
2920
2921static struct kobj_type class_dir_ktype = {
2922 .release = class_dir_release,
2923 .sysfs_ops = &kobj_sysfs_ops,
2924 .child_ns_type = class_dir_child_ns_type
2925};
2926
2927static struct kobject *
2928class_dir_create_and_add(struct class *class, struct kobject *parent_kobj)
2929{
2930 struct class_dir *dir;
2931 int retval;
2932
2933 dir = kzalloc(sizeof(*dir), GFP_KERNEL);
2934 if (!dir)
Tetsuo Handa84d0c272018-05-07 19:10:31 +09002935 return ERR_PTR(-ENOMEM);
Eric W. Biedermanbc451f22010-03-30 11:31:25 -07002936
2937 dir->class = class;
2938 kobject_init(&dir->kobj, &class_dir_ktype);
2939
Kay Sievers6b6e39a2010-11-15 23:13:18 +01002940 dir->kobj.kset = &class->p->glue_dirs;
Eric W. Biedermanbc451f22010-03-30 11:31:25 -07002941
2942 retval = kobject_add(&dir->kobj, parent_kobj, "%s", class->name);
2943 if (retval < 0) {
2944 kobject_put(&dir->kobj);
Tetsuo Handa84d0c272018-05-07 19:10:31 +09002945 return ERR_PTR(retval);
Eric W. Biedermanbc451f22010-03-30 11:31:25 -07002946 }
2947 return &dir->kobj;
2948}
2949
Yijing Wange4a60d12014-11-07 12:05:49 +08002950static DEFINE_MUTEX(gdp_mutex);
Eric W. Biedermanbc451f22010-03-30 11:31:25 -07002951
Kay Sieversda231fd2007-11-21 17:29:15 +01002952static struct kobject *get_device_parent(struct device *dev,
2953 struct device *parent)
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +01002954{
Kay Sievers86406242007-03-14 03:25:56 +01002955 if (dev->class) {
2956 struct kobject *kobj = NULL;
2957 struct kobject *parent_kobj;
2958 struct kobject *k;
2959
Randy Dunlapead454f2010-09-24 14:36:49 -07002960#ifdef CONFIG_BLOCK
Kay Sievers39aba962010-09-04 22:33:14 -07002961 /* block disks show up in /sys/block */
Andi Kleene52eec12010-09-08 16:54:17 +02002962 if (sysfs_deprecated && dev->class == &block_class) {
Kay Sievers39aba962010-09-04 22:33:14 -07002963 if (parent && parent->class == &block_class)
2964 return &parent->kobj;
Kay Sievers6b6e39a2010-11-15 23:13:18 +01002965 return &block_class.p->subsys.kobj;
Kay Sievers39aba962010-09-04 22:33:14 -07002966 }
Randy Dunlapead454f2010-09-24 14:36:49 -07002967#endif
Andi Kleene52eec12010-09-08 16:54:17 +02002968
Kay Sievers86406242007-03-14 03:25:56 +01002969 /*
2970 * If we have no parent, we live in "virtual".
Kay Sievers0f4dafc2007-12-19 01:40:42 +01002971 * Class-devices with a non class-device as parent, live
2972 * in a "glue" directory to prevent namespace collisions.
Kay Sievers86406242007-03-14 03:25:56 +01002973 */
2974 if (parent == NULL)
2975 parent_kobj = virtual_device_parent(dev);
Eric W. Biederman24b14422010-07-24 22:43:35 -07002976 else if (parent->class && !dev->class->ns_type)
Kay Sievers86406242007-03-14 03:25:56 +01002977 return &parent->kobj;
2978 else
2979 parent_kobj = &parent->kobj;
2980
Tejun Heo77d3d7c2010-02-05 17:57:02 +09002981 mutex_lock(&gdp_mutex);
2982
Kay Sievers86406242007-03-14 03:25:56 +01002983 /* find our class-directory at the parent and reference it */
Kay Sievers6b6e39a2010-11-15 23:13:18 +01002984 spin_lock(&dev->class->p->glue_dirs.list_lock);
2985 list_for_each_entry(k, &dev->class->p->glue_dirs.list, entry)
Kay Sievers86406242007-03-14 03:25:56 +01002986 if (k->parent == parent_kobj) {
2987 kobj = kobject_get(k);
2988 break;
2989 }
Kay Sievers6b6e39a2010-11-15 23:13:18 +01002990 spin_unlock(&dev->class->p->glue_dirs.list_lock);
Tejun Heo77d3d7c2010-02-05 17:57:02 +09002991 if (kobj) {
2992 mutex_unlock(&gdp_mutex);
Kay Sievers86406242007-03-14 03:25:56 +01002993 return kobj;
Tejun Heo77d3d7c2010-02-05 17:57:02 +09002994 }
Kay Sievers86406242007-03-14 03:25:56 +01002995
2996 /* or create a new class-directory at the parent device */
Eric W. Biedermanbc451f22010-03-30 11:31:25 -07002997 k = class_dir_create_and_add(dev->class, parent_kobj);
Kay Sievers0f4dafc2007-12-19 01:40:42 +01002998 /* do not emit an uevent for this simple "glue" directory */
Tejun Heo77d3d7c2010-02-05 17:57:02 +09002999 mutex_unlock(&gdp_mutex);
Greg Kroah-Hartman43968d22007-11-05 22:24:43 -08003000 return k;
Kay Sievers86406242007-03-14 03:25:56 +01003001 }
3002
Kay Sieversca22e562011-12-14 14:29:38 -08003003 /* subsystems can specify a default root directory for their devices */
3004 if (!parent && dev->bus && dev->bus->dev_root)
3005 return &dev->bus->dev_root->kobj;
3006
Kay Sievers86406242007-03-14 03:25:56 +01003007 if (parent)
Cornelia Huckc744aeae2007-01-08 20:16:44 +01003008 return &parent->kobj;
3009 return NULL;
3010}
Kay Sieversda231fd2007-11-21 17:29:15 +01003011
Ming Leicebf8fd2016-07-10 19:27:36 +08003012static inline bool live_in_glue_dir(struct kobject *kobj,
3013 struct device *dev)
3014{
3015 if (!kobj || !dev->class ||
3016 kobj->kset != &dev->class->p->glue_dirs)
3017 return false;
3018 return true;
3019}
3020
3021static inline struct kobject *get_glue_dir(struct device *dev)
3022{
3023 return dev->kobj.parent;
3024}
3025
Ingo Molnarf0832662021-08-29 09:18:53 +02003026/**
3027 * kobject_has_children - Returns whether a kobject has children.
3028 * @kobj: the object to test
3029 *
3030 * This will return whether a kobject has other kobjects as children.
3031 *
3032 * It does NOT account for the presence of attribute files, only sub
3033 * directories. It also assumes there is no concurrent addition or
3034 * removal of such children, and thus relies on external locking.
3035 */
3036static inline bool kobject_has_children(struct kobject *kobj)
3037{
3038 WARN_ON_ONCE(kref_read(&kobj->kref) == 0);
3039
3040 return kobj->sd && kobj->sd->dir.subdirs;
3041}
3042
Ming Leicebf8fd2016-07-10 19:27:36 +08003043/*
3044 * make sure cleaning up dir as the last step, we need to make
3045 * sure .release handler of kobject is run with holding the
3046 * global lock
3047 */
Cornelia Huck63b69712008-01-21 16:09:44 +01003048static void cleanup_glue_dir(struct device *dev, struct kobject *glue_dir)
Kay Sieversda231fd2007-11-21 17:29:15 +01003049{
Muchun Songac434322019-07-27 11:21:22 +08003050 unsigned int ref;
3051
Kay Sievers0f4dafc2007-12-19 01:40:42 +01003052 /* see if we live in a "glue" directory */
Ming Leicebf8fd2016-07-10 19:27:36 +08003053 if (!live_in_glue_dir(glue_dir, dev))
Kay Sieversda231fd2007-11-21 17:29:15 +01003054 return;
3055
Yijing Wange4a60d12014-11-07 12:05:49 +08003056 mutex_lock(&gdp_mutex);
Muchun Songac434322019-07-27 11:21:22 +08003057 /**
3058 * There is a race condition between removing glue directory
3059 * and adding a new device under the glue directory.
3060 *
3061 * CPU1: CPU2:
3062 *
3063 * device_add()
3064 * get_device_parent()
3065 * class_dir_create_and_add()
3066 * kobject_add_internal()
3067 * create_dir() // create glue_dir
3068 *
3069 * device_add()
3070 * get_device_parent()
3071 * kobject_get() // get glue_dir
3072 *
3073 * device_del()
3074 * cleanup_glue_dir()
3075 * kobject_del(glue_dir)
3076 *
3077 * kobject_add()
3078 * kobject_add_internal()
3079 * create_dir() // in glue_dir
3080 * sysfs_create_dir_ns()
3081 * kernfs_create_dir_ns(sd)
3082 *
3083 * sysfs_remove_dir() // glue_dir->sd=NULL
3084 * sysfs_put() // free glue_dir->sd
3085 *
3086 * // sd is freed
3087 * kernfs_new_node(sd)
3088 * kernfs_get(glue_dir)
3089 * kernfs_add_one()
3090 * kernfs_put()
3091 *
3092 * Before CPU1 remove last child device under glue dir, if CPU2 add
3093 * a new device under glue dir, the glue_dir kobject reference count
3094 * will be increase to 2 in kobject_get(k). And CPU2 has been called
3095 * kernfs_create_dir_ns(). Meanwhile, CPU1 call sysfs_remove_dir()
3096 * and sysfs_put(). This result in glue_dir->sd is freed.
3097 *
3098 * Then the CPU2 will see a stale "empty" but still potentially used
3099 * glue dir around in kernfs_new_node().
3100 *
3101 * In order to avoid this happening, we also should make sure that
3102 * kernfs_node for glue_dir is released in CPU1 only when refcount
3103 * for glue_dir kobj is 1.
3104 */
3105 ref = kref_read(&glue_dir->kref);
3106 if (!kobject_has_children(glue_dir) && !--ref)
Benjamin Herrenschmidt726e4102018-07-10 10:29:10 +10003107 kobject_del(glue_dir);
Kay Sievers0f4dafc2007-12-19 01:40:42 +01003108 kobject_put(glue_dir);
Yijing Wange4a60d12014-11-07 12:05:49 +08003109 mutex_unlock(&gdp_mutex);
Kay Sieversda231fd2007-11-21 17:29:15 +01003110}
Cornelia Huck63b69712008-01-21 16:09:44 +01003111
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07003112static int device_add_class_symlinks(struct device *dev)
3113{
Benjamin Herrenschmidt5590f312015-02-18 11:25:18 +11003114 struct device_node *of_node = dev_of_node(dev);
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07003115 int error;
3116
Benjamin Herrenschmidt5590f312015-02-18 11:25:18 +11003117 if (of_node) {
Rob Herring0c3c2342017-10-04 14:04:01 -05003118 error = sysfs_create_link(&dev->kobj, of_node_kobj(of_node), "of_node");
Benjamin Herrenschmidt5590f312015-02-18 11:25:18 +11003119 if (error)
3120 dev_warn(dev, "Error %d creating of_node link\n",error);
3121 /* An error here doesn't warrant bringing down the device */
3122 }
3123
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07003124 if (!dev->class)
3125 return 0;
Kay Sieversda231fd2007-11-21 17:29:15 +01003126
Greg Kroah-Hartman1fbfee62008-05-28 09:28:39 -07003127 error = sysfs_create_link(&dev->kobj,
Kay Sievers6b6e39a2010-11-15 23:13:18 +01003128 &dev->class->p->subsys.kobj,
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07003129 "subsystem");
3130 if (error)
Benjamin Herrenschmidt5590f312015-02-18 11:25:18 +11003131 goto out_devnode;
Kay Sieversda231fd2007-11-21 17:29:15 +01003132
Greg Kroah-Hartman4e886c22008-01-27 12:12:43 -08003133 if (dev->parent && device_is_not_partition(dev)) {
Dmitry Torokhov4f01a752007-09-18 22:46:50 -07003134 error = sysfs_create_link(&dev->kobj, &dev->parent->kobj,
3135 "device");
3136 if (error)
Kay Sievers39aba962010-09-04 22:33:14 -07003137 goto out_subsys;
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07003138 }
Kay Sievers39aba962010-09-04 22:33:14 -07003139
Randy Dunlapead454f2010-09-24 14:36:49 -07003140#ifdef CONFIG_BLOCK
Kay Sievers39aba962010-09-04 22:33:14 -07003141 /* /sys/block has directories and does not need symlinks */
Andi Kleene52eec12010-09-08 16:54:17 +02003142 if (sysfs_deprecated && dev->class == &block_class)
Kay Sievers39aba962010-09-04 22:33:14 -07003143 return 0;
Randy Dunlapead454f2010-09-24 14:36:49 -07003144#endif
Kay Sievers39aba962010-09-04 22:33:14 -07003145
3146 /* link in the class directory pointing to the device */
Kay Sievers6b6e39a2010-11-15 23:13:18 +01003147 error = sysfs_create_link(&dev->class->p->subsys.kobj,
Kay Sievers39aba962010-09-04 22:33:14 -07003148 &dev->kobj, dev_name(dev));
3149 if (error)
3150 goto out_device;
3151
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07003152 return 0;
3153
Kay Sievers39aba962010-09-04 22:33:14 -07003154out_device:
3155 sysfs_remove_link(&dev->kobj, "device");
Kay Sieversda231fd2007-11-21 17:29:15 +01003156
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07003157out_subsys:
3158 sysfs_remove_link(&dev->kobj, "subsystem");
Benjamin Herrenschmidt5590f312015-02-18 11:25:18 +11003159out_devnode:
3160 sysfs_remove_link(&dev->kobj, "of_node");
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07003161 return error;
3162}
3163
3164static void device_remove_class_symlinks(struct device *dev)
3165{
Benjamin Herrenschmidt5590f312015-02-18 11:25:18 +11003166 if (dev_of_node(dev))
3167 sysfs_remove_link(&dev->kobj, "of_node");
3168
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07003169 if (!dev->class)
3170 return;
Kay Sieversda231fd2007-11-21 17:29:15 +01003171
Greg Kroah-Hartman4e886c22008-01-27 12:12:43 -08003172 if (dev->parent && device_is_not_partition(dev))
Kay Sieversda231fd2007-11-21 17:29:15 +01003173 sysfs_remove_link(&dev->kobj, "device");
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07003174 sysfs_remove_link(&dev->kobj, "subsystem");
Randy Dunlapead454f2010-09-24 14:36:49 -07003175#ifdef CONFIG_BLOCK
Andi Kleene52eec12010-09-08 16:54:17 +02003176 if (sysfs_deprecated && dev->class == &block_class)
Kay Sievers39aba962010-09-04 22:33:14 -07003177 return;
Randy Dunlapead454f2010-09-24 14:36:49 -07003178#endif
Kay Sievers6b6e39a2010-11-15 23:13:18 +01003179 sysfs_delete_link(&dev->class->p->subsys.kobj, &dev->kobj, dev_name(dev));
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07003180}
3181
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182/**
Stephen Rothwell413c2392008-05-30 10:16:40 +10003183 * dev_set_name - set a device name
3184 * @dev: device
Randy Dunlap46232362008-06-04 21:40:43 -07003185 * @fmt: format string for the device's name
Stephen Rothwell413c2392008-05-30 10:16:40 +10003186 */
3187int dev_set_name(struct device *dev, const char *fmt, ...)
3188{
3189 va_list vargs;
Kay Sievers1fa5ae82009-01-25 15:17:37 +01003190 int err;
Stephen Rothwell413c2392008-05-30 10:16:40 +10003191
3192 va_start(vargs, fmt);
Kay Sievers1fa5ae82009-01-25 15:17:37 +01003193 err = kobject_set_name_vargs(&dev->kobj, fmt, vargs);
Stephen Rothwell413c2392008-05-30 10:16:40 +10003194 va_end(vargs);
Kay Sievers1fa5ae82009-01-25 15:17:37 +01003195 return err;
Stephen Rothwell413c2392008-05-30 10:16:40 +10003196}
3197EXPORT_SYMBOL_GPL(dev_set_name);
3198
3199/**
Dan Williamse105b8b2008-04-21 10:51:07 -07003200 * device_to_dev_kobj - select a /sys/dev/ directory for the device
3201 * @dev: device
3202 *
3203 * By default we select char/ for new entries. Setting class->dev_obj
3204 * to NULL prevents an entry from being created. class->dev_kobj must
3205 * be set (or cleared) before any devices are registered to the class
3206 * otherwise device_create_sys_dev_entry() and
Peter Korsgaard0d4e293c2012-04-17 12:12:57 +02003207 * device_remove_sys_dev_entry() will disagree about the presence of
3208 * the link.
Dan Williamse105b8b2008-04-21 10:51:07 -07003209 */
3210static struct kobject *device_to_dev_kobj(struct device *dev)
3211{
3212 struct kobject *kobj;
3213
3214 if (dev->class)
3215 kobj = dev->class->dev_kobj;
3216 else
3217 kobj = sysfs_dev_char_kobj;
3218
3219 return kobj;
3220}
3221
3222static int device_create_sys_dev_entry(struct device *dev)
3223{
3224 struct kobject *kobj = device_to_dev_kobj(dev);
3225 int error = 0;
3226 char devt_str[15];
3227
3228 if (kobj) {
3229 format_dev_t(devt_str, dev->devt);
3230 error = sysfs_create_link(kobj, &dev->kobj, devt_str);
3231 }
3232
3233 return error;
3234}
3235
3236static void device_remove_sys_dev_entry(struct device *dev)
3237{
3238 struct kobject *kobj = device_to_dev_kobj(dev);
3239 char devt_str[15];
3240
3241 if (kobj) {
3242 format_dev_t(devt_str, dev->devt);
3243 sysfs_remove_link(kobj, devt_str);
3244 }
3245}
3246
Shaokun Zhang46d3a032018-07-15 18:08:56 +08003247static int device_private_init(struct device *dev)
Greg Kroah-Hartmanb4028432009-05-11 14:16:57 -07003248{
3249 dev->p = kzalloc(sizeof(*dev->p), GFP_KERNEL);
3250 if (!dev->p)
3251 return -ENOMEM;
3252 dev->p->device = dev;
3253 klist_init(&dev->p->klist_children, klist_children_get,
3254 klist_children_put);
Greg Kroah-Hartmanef8a3fd2012-03-08 12:17:22 -08003255 INIT_LIST_HEAD(&dev->p->deferred_probe);
Greg Kroah-Hartmanb4028432009-05-11 14:16:57 -07003256 return 0;
3257}
3258
Dan Williamse105b8b2008-04-21 10:51:07 -07003259/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08003260 * device_add - add device to device hierarchy.
3261 * @dev: device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003262 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08003263 * This is part 2 of device_register(), though may be called
3264 * separately _iff_ device_initialize() has been called separately.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265 *
Cornelia Huck57394112008-09-03 18:26:40 +02003266 * This adds @dev to the kobject hierarchy via kobject_add(), adds it
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08003267 * to the global and sibling lists for the device, then
3268 * adds it to the other relevant subsystems of the driver model.
Cornelia Huck57394112008-09-03 18:26:40 +02003269 *
Alan Sternb10d5ef2012-01-17 11:39:00 -05003270 * Do not call this routine or device_register() more than once for
3271 * any device structure. The driver model core is not designed to work
3272 * with devices that get unregistered and then spring back to life.
3273 * (Among other things, it's very hard to guarantee that all references
3274 * to the previous incarnation of @dev have been dropped.) Allocate
3275 * and register a fresh new struct device instead.
3276 *
Cornelia Huck57394112008-09-03 18:26:40 +02003277 * NOTE: _Never_ directly free @dev after calling this function, even
3278 * if it returned an error! Always use put_device() to give up your
3279 * reference instead.
Borislav Petkovaffada72019-04-18 19:41:56 +02003280 *
3281 * Rule of thumb is: if device_add() succeeds, you should call
3282 * device_del() when you want to get rid of it. If device_add() has
3283 * *not* succeeded, use *only* put_device() to drop the reference
3284 * count.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003285 */
3286int device_add(struct device *dev)
3287{
Viresh Kumar35dbf4e2017-03-17 12:24:22 +05303288 struct device *parent;
Kay Sieversca22e562011-12-14 14:29:38 -08003289 struct kobject *kobj;
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02003290 struct class_interface *class_intf;
Saravana Kannan5f5377e2020-05-14 22:34:58 -07003291 int error = -EINVAL;
Ming Leicebf8fd2016-07-10 19:27:36 +08003292 struct kobject *glue_dir = NULL;
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01003293
Linus Torvalds1da177e2005-04-16 15:20:36 -07003294 dev = get_device(dev);
Greg Kroah-Hartmanc906a482008-05-30 10:45:12 -07003295 if (!dev)
3296 goto done;
3297
Greg Kroah-Hartmanfb069a52008-12-16 12:23:36 -08003298 if (!dev->p) {
Greg Kroah-Hartmanb4028432009-05-11 14:16:57 -07003299 error = device_private_init(dev);
3300 if (error)
3301 goto done;
Greg Kroah-Hartmanfb069a52008-12-16 12:23:36 -08003302 }
Greg Kroah-Hartmanfb069a52008-12-16 12:23:36 -08003303
Kay Sievers1fa5ae82009-01-25 15:17:37 +01003304 /*
3305 * for statically allocated devices, which should all be converted
3306 * some day, we need to initialize the name. We prevent reading back
3307 * the name, and force the use of dev_name()
3308 */
3309 if (dev->init_name) {
Greg Kroah-Hartmanacc0e902009-06-02 15:39:55 -07003310 dev_set_name(dev, "%s", dev->init_name);
Kay Sievers1fa5ae82009-01-25 15:17:37 +01003311 dev->init_name = NULL;
3312 }
Greg Kroah-Hartmanc906a482008-05-30 10:45:12 -07003313
Kay Sieversca22e562011-12-14 14:29:38 -08003314 /* subsystems can specify simple device enumeration */
3315 if (!dev_name(dev) && dev->bus && dev->bus->dev_name)
3316 dev_set_name(dev, "%s%u", dev->bus->dev_name, dev->id);
3317
Thomas Gleixnere6309e72009-12-10 19:32:49 +00003318 if (!dev_name(dev)) {
3319 error = -EINVAL;
Kay Sievers5c8563d2009-05-28 14:24:07 -07003320 goto name_error;
Thomas Gleixnere6309e72009-12-10 19:32:49 +00003321 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003322
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01003323 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
Greg Kroah-Hartmanc205ef42006-08-07 22:19:37 -07003324
Linus Torvalds1da177e2005-04-16 15:20:36 -07003325 parent = get_device(dev->parent);
Kay Sieversca22e562011-12-14 14:29:38 -08003326 kobj = get_device_parent(dev, parent);
Tetsuo Handa84d0c272018-05-07 19:10:31 +09003327 if (IS_ERR(kobj)) {
3328 error = PTR_ERR(kobj);
3329 goto parent_error;
3330 }
Kay Sieversca22e562011-12-14 14:29:38 -08003331 if (kobj)
3332 dev->kobj.parent = kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003333
Yinghai Lu0d358f22008-02-19 03:20:41 -08003334 /* use parent numa_node */
Zhen Lei56f2de82015-08-25 12:08:22 +08003335 if (parent && (dev_to_node(dev) == NUMA_NO_NODE))
Yinghai Lu0d358f22008-02-19 03:20:41 -08003336 set_dev_node(dev, dev_to_node(parent));
3337
Linus Torvalds1da177e2005-04-16 15:20:36 -07003338 /* first, register with generic layer. */
Kay Sievers8a577ff2009-04-18 15:05:45 -07003339 /* we require the name to be set before, and pass NULL */
3340 error = kobject_add(&dev->kobj, dev->kobj.parent, NULL);
Ming Leicebf8fd2016-07-10 19:27:36 +08003341 if (error) {
3342 glue_dir = get_glue_dir(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003343 goto Error;
Ming Leicebf8fd2016-07-10 19:27:36 +08003344 }
Kay Sieversa7fd6702005-10-01 14:49:43 +02003345
Brian Walsh37022642006-08-14 22:43:19 -07003346 /* notify platform of device entry */
Rafael J. Wysockib2ebd9d2021-07-12 19:28:16 +02003347 device_platform_notify(dev);
Brian Walsh37022642006-08-14 22:43:19 -07003348
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07003349 error = device_create_file(dev, &dev_attr_uevent);
Cornelia Hucka306eea2006-09-22 11:37:13 +02003350 if (error)
3351 goto attrError;
Kay Sieversa7fd6702005-10-01 14:49:43 +02003352
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07003353 error = device_add_class_symlinks(dev);
3354 if (error)
3355 goto SymlinkError;
Cornelia Huckdc0afa82007-07-09 11:39:18 -07003356 error = device_add_attrs(dev);
3357 if (error)
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07003358 goto AttrsError;
Cornelia Huckdc0afa82007-07-09 11:39:18 -07003359 error = bus_add_device(dev);
3360 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003361 goto BusError;
Alan Stern3b98aea2008-08-07 13:06:12 -04003362 error = dpm_sysfs_add(dev);
Rafael J. Wysocki57eee3d2008-03-12 00:59:38 +01003363 if (error)
Alan Stern3b98aea2008-08-07 13:06:12 -04003364 goto DPMError;
3365 device_pm_add(dev);
Alan Sternec0676ee2008-12-05 14:10:31 -05003366
Sergey Klyaus0cd75042014-10-08 11:31:54 +04003367 if (MAJOR(dev->devt)) {
3368 error = device_create_file(dev, &dev_attr_dev);
3369 if (error)
3370 goto DevAttrError;
3371
3372 error = device_create_sys_dev_entry(dev);
3373 if (error)
3374 goto SysEntryError;
3375
3376 devtmpfs_create_node(dev);
3377 }
3378
Alan Sternec0676ee2008-12-05 14:10:31 -05003379 /* Notify clients of device addition. This call must come
majianpeng268863f2012-01-11 15:12:06 +00003380 * after dpm_sysfs_add() and before kobject_uevent().
Alan Sternec0676ee2008-12-05 14:10:31 -05003381 */
3382 if (dev->bus)
3383 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
3384 BUS_NOTIFY_ADD_DEVICE, dev);
3385
Cornelia Huck83b5fb4c2007-03-29 11:12:11 +02003386 kobject_uevent(&dev->kobj, KOBJ_ADD);
Saravana Kannan372a67c2019-09-04 14:11:20 -07003387
Saravana Kannane2ae9bc2019-09-04 14:11:21 -07003388 /*
3389 * Check if any of the other devices (consumers) have been waiting for
3390 * this device (supplier) to be added so that they can create a device
3391 * link to it.
3392 *
3393 * This needs to happen after device_pm_add() because device_link_add()
3394 * requires the supplier be registered before it's called.
3395 *
Saravana Kannan2cd38fd2020-05-19 20:48:21 -07003396 * But this also needs to happen before bus_probe_device() to make sure
Saravana Kannane2ae9bc2019-09-04 14:11:21 -07003397 * waiting consumers can link to it before the driver is bound to the
3398 * device and the driver sync_state callback is called for this device.
3399 */
Saravana Kannan2cd38fd2020-05-19 20:48:21 -07003400 if (dev->fwnode && !dev->fwnode->dev) {
3401 dev->fwnode->dev = dev;
Saravana Kannan5f5377e2020-05-14 22:34:58 -07003402 fw_devlink_link_device(dev);
Saravana Kannan03324502019-10-28 15:00:24 -07003403 }
Saravana Kannane2ae9bc2019-09-04 14:11:21 -07003404
Alan Stern2023c612009-07-30 15:27:18 -04003405 bus_probe_device(dev);
Saravana Kannand46f3e32021-04-01 21:03:41 -07003406
3407 /*
3408 * If all driver registration is done and a newly added device doesn't
3409 * match with any driver, don't block its consumers from probing in
3410 * case the consumer device is able to operate without this supplier.
3411 */
3412 if (dev->fwnode && fw_devlink_drv_reg_done && !dev->can_match)
3413 fw_devlink_unblock_consumers(dev);
3414
Linus Torvalds1da177e2005-04-16 15:20:36 -07003415 if (parent)
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08003416 klist_add_tail(&dev->p->knode_parent,
3417 &parent->p->klist_children);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003418
Greg Kroah-Hartman5d9fd162006-06-22 17:17:32 -07003419 if (dev->class) {
Kay Sieversca22e562011-12-14 14:29:38 -08003420 mutex_lock(&dev->class->p->mutex);
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02003421 /* tie the class to the device */
Wei Yang570d0202019-01-18 10:34:59 +08003422 klist_add_tail(&dev->p->knode_class,
Kay Sievers6b6e39a2010-11-15 23:13:18 +01003423 &dev->class->p->klist_devices);
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02003424
3425 /* notify any interfaces that the device is here */
Greg Kroah-Hartman184f1f72008-05-28 09:28:39 -07003426 list_for_each_entry(class_intf,
Kay Sieversca22e562011-12-14 14:29:38 -08003427 &dev->class->p->interfaces, node)
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02003428 if (class_intf->add_dev)
3429 class_intf->add_dev(dev, class_intf);
Kay Sieversca22e562011-12-14 14:29:38 -08003430 mutex_unlock(&dev->class->p->mutex);
Greg Kroah-Hartman5d9fd162006-06-22 17:17:32 -07003431 }
Greg Kroah-Hartmanc906a482008-05-30 10:45:12 -07003432done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003433 put_device(dev);
3434 return error;
Sergey Klyaus0cd75042014-10-08 11:31:54 +04003435 SysEntryError:
3436 if (MAJOR(dev->devt))
3437 device_remove_file(dev, &dev_attr_dev);
3438 DevAttrError:
3439 device_pm_remove(dev);
3440 dpm_sysfs_remove(dev);
Alan Stern3b98aea2008-08-07 13:06:12 -04003441 DPMError:
Rafael J. Wysocki57eee3d2008-03-12 00:59:38 +01003442 bus_remove_device(dev);
3443 BusError:
James Simmons82f0cf92007-02-21 17:44:51 +00003444 device_remove_attrs(dev);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07003445 AttrsError:
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07003446 device_remove_class_symlinks(dev);
3447 SymlinkError:
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07003448 device_remove_file(dev, &dev_attr_uevent);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07003449 attrError:
Rafael J. Wysockib2ebd9d2021-07-12 19:28:16 +02003450 device_platform_notify_remove(dev);
Kay Sievers312c0042005-11-16 09:00:00 +01003451 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
Ming Leicebf8fd2016-07-10 19:27:36 +08003452 glue_dir = get_glue_dir(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003453 kobject_del(&dev->kobj);
3454 Error:
Ming Leicebf8fd2016-07-10 19:27:36 +08003455 cleanup_glue_dir(dev, glue_dir);
Tetsuo Handa84d0c272018-05-07 19:10:31 +09003456parent_error:
Markus Elfring5f0163a2015-02-05 11:48:26 +01003457 put_device(parent);
Kay Sievers5c8563d2009-05-28 14:24:07 -07003458name_error:
3459 kfree(dev->p);
3460 dev->p = NULL;
Greg Kroah-Hartmanc906a482008-05-30 10:45:12 -07003461 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003462}
David Graham White86df2682013-07-21 20:41:14 -04003463EXPORT_SYMBOL_GPL(device_add);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003464
Linus Torvalds1da177e2005-04-16 15:20:36 -07003465/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08003466 * device_register - register a device with the system.
3467 * @dev: pointer to the device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07003468 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08003469 * This happens in two clean steps - initialize the device
3470 * and add it to the system. The two steps can be called
3471 * separately, but this is the easiest and most common.
3472 * I.e. you should only call the two helpers separately if
3473 * have a clearly defined need to use and refcount the device
3474 * before it is added to the hierarchy.
Cornelia Huck57394112008-09-03 18:26:40 +02003475 *
Alan Sternb10d5ef2012-01-17 11:39:00 -05003476 * For more information, see the kerneldoc for device_initialize()
3477 * and device_add().
3478 *
Cornelia Huck57394112008-09-03 18:26:40 +02003479 * NOTE: _Never_ directly free @dev after calling this function, even
3480 * if it returned an error! Always use put_device() to give up the
3481 * reference initialized in this function instead.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003482 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003483int device_register(struct device *dev)
3484{
3485 device_initialize(dev);
3486 return device_add(dev);
3487}
David Graham White86df2682013-07-21 20:41:14 -04003488EXPORT_SYMBOL_GPL(device_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003489
Linus Torvalds1da177e2005-04-16 15:20:36 -07003490/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08003491 * get_device - increment reference count for device.
3492 * @dev: device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003493 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08003494 * This simply forwards the call to kobject_get(), though
3495 * we do take care to provide for the case that we get a NULL
3496 * pointer passed in.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003497 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08003498struct device *get_device(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003499{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +02003500 return dev ? kobj_to_dev(kobject_get(&dev->kobj)) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003501}
David Graham White86df2682013-07-21 20:41:14 -04003502EXPORT_SYMBOL_GPL(get_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003503
Linus Torvalds1da177e2005-04-16 15:20:36 -07003504/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08003505 * put_device - decrement reference count.
3506 * @dev: device in question.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003507 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08003508void put_device(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003509{
Kay Sieversedfaa7c2007-05-21 22:08:01 +02003510 /* might_sleep(); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003511 if (dev)
3512 kobject_put(&dev->kobj);
3513}
David Graham White86df2682013-07-21 20:41:14 -04003514EXPORT_SYMBOL_GPL(put_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003515
Dan Williams00289cd2019-07-17 18:07:53 -07003516bool kill_device(struct device *dev)
3517{
3518 /*
3519 * Require the device lock and set the "dead" flag to guarantee that
3520 * the update behavior is consistent with the other bitfields near
3521 * it and that we cannot have an asynchronous probe routine trying
3522 * to run while we are tearing out the bus/class/sysfs from
3523 * underneath the device.
3524 */
Julian Wiedmann8c60a142021-05-12 16:10:54 +02003525 device_lock_assert(dev);
Dan Williams00289cd2019-07-17 18:07:53 -07003526
3527 if (dev->p->dead)
3528 return false;
3529 dev->p->dead = true;
3530 return true;
3531}
3532EXPORT_SYMBOL_GPL(kill_device);
3533
Linus Torvalds1da177e2005-04-16 15:20:36 -07003534/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08003535 * device_del - delete device from system.
3536 * @dev: device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003537 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08003538 * This is the first part of the device unregistration
3539 * sequence. This removes the device from the lists we control
3540 * from here, has it removed from the other driver model
3541 * subsystems it was added to in device_add(), and removes it
3542 * from the kobject hierarchy.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003543 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08003544 * NOTE: this should be called manually _iff_ device_add() was
3545 * also called manually.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003546 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08003547void device_del(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003548{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08003549 struct device *parent = dev->parent;
Ming Leicebf8fd2016-07-10 19:27:36 +08003550 struct kobject *glue_dir = NULL;
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02003551 struct class_interface *class_intf;
Oliver Neukumb8530012020-09-16 21:15:44 +02003552 unsigned int noio_flag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003553
Alexander Duyck3451a492019-01-22 10:39:10 -08003554 device_lock(dev);
Dan Williams00289cd2019-07-17 18:07:53 -07003555 kill_device(dev);
Alexander Duyck3451a492019-01-22 10:39:10 -08003556 device_unlock(dev);
3557
Saravana Kannan372a67c2019-09-04 14:11:20 -07003558 if (dev->fwnode && dev->fwnode->dev == dev)
3559 dev->fwnode->dev = NULL;
3560
Alan Sternec0676ee2008-12-05 14:10:31 -05003561 /* Notify clients of device removal. This call must come
3562 * before dpm_sysfs_remove().
3563 */
Oliver Neukumb8530012020-09-16 21:15:44 +02003564 noio_flag = memalloc_noio_save();
Alan Sternec0676ee2008-12-05 14:10:31 -05003565 if (dev->bus)
3566 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
3567 BUS_NOTIFY_DEL_DEVICE, dev);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01003568
Alan Stern3b98aea2008-08-07 13:06:12 -04003569 dpm_sysfs_remove(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003570 if (parent)
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08003571 klist_del(&dev->p->knode_parent);
Dan Williamse105b8b2008-04-21 10:51:07 -07003572 if (MAJOR(dev->devt)) {
Kay Sievers2b2af542009-04-30 15:23:42 +02003573 devtmpfs_delete_node(dev);
Dan Williamse105b8b2008-04-21 10:51:07 -07003574 device_remove_sys_dev_entry(dev);
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07003575 device_remove_file(dev, &dev_attr_dev);
Dan Williamse105b8b2008-04-21 10:51:07 -07003576 }
Kay Sieversb9d9c822006-06-15 15:31:56 +02003577 if (dev->class) {
Kay Sieversda231fd2007-11-21 17:29:15 +01003578 device_remove_class_symlinks(dev);
Kay Sievers99ef3ef2006-09-14 11:23:28 +02003579
Kay Sieversca22e562011-12-14 14:29:38 -08003580 mutex_lock(&dev->class->p->mutex);
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02003581 /* notify any interfaces that the device is now gone */
Greg Kroah-Hartman184f1f72008-05-28 09:28:39 -07003582 list_for_each_entry(class_intf,
Kay Sieversca22e562011-12-14 14:29:38 -08003583 &dev->class->p->interfaces, node)
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02003584 if (class_intf->remove_dev)
3585 class_intf->remove_dev(dev, class_intf);
3586 /* remove the device from the class list */
Wei Yang570d0202019-01-18 10:34:59 +08003587 klist_del(&dev->p->knode_class);
Kay Sieversca22e562011-12-14 14:29:38 -08003588 mutex_unlock(&dev->class->p->mutex);
Kay Sieversb9d9c822006-06-15 15:31:56 +02003589 }
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07003590 device_remove_file(dev, &dev_attr_uevent);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07003591 device_remove_attrs(dev);
Benjamin Herrenschmidt28953532006-11-08 19:46:14 -08003592 bus_remove_device(dev);
LongX Zhang4b6d1f122012-10-25 00:21:28 +02003593 device_pm_remove(dev);
Grant Likelyd1c34142012-03-05 08:47:41 -07003594 driver_deferred_probe_del(dev);
Rafael J. Wysockib2ebd9d2021-07-12 19:28:16 +02003595 device_platform_notify_remove(dev);
Jeffy Chen2ec16152017-10-20 20:01:01 +08003596 device_links_purge(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003597
Joerg Roedel599bad32014-09-30 13:02:02 +02003598 if (dev->bus)
3599 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
3600 BUS_NOTIFY_REMOVED_DEVICE, dev);
Kay Sievers312c0042005-11-16 09:00:00 +01003601 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
Ming Leicebf8fd2016-07-10 19:27:36 +08003602 glue_dir = get_glue_dir(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003603 kobject_del(&dev->kobj);
Ming Leicebf8fd2016-07-10 19:27:36 +08003604 cleanup_glue_dir(dev, glue_dir);
Oliver Neukumb8530012020-09-16 21:15:44 +02003605 memalloc_noio_restore(noio_flag);
Kay Sieversda231fd2007-11-21 17:29:15 +01003606 put_device(parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003607}
David Graham White86df2682013-07-21 20:41:14 -04003608EXPORT_SYMBOL_GPL(device_del);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003609
3610/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08003611 * device_unregister - unregister device from system.
3612 * @dev: device going away.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003613 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08003614 * We do this in two parts, like we do device_register(). First,
3615 * we remove it from all the subsystems with device_del(), then
3616 * we decrement the reference count via put_device(). If that
3617 * is the final reference count, the device will be cleaned up
3618 * via device_release() above. Otherwise, the structure will
3619 * stick around until the final reference to the device is dropped.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003620 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08003621void device_unregister(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003622{
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01003623 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003624 device_del(dev);
3625 put_device(dev);
3626}
David Graham White86df2682013-07-21 20:41:14 -04003627EXPORT_SYMBOL_GPL(device_unregister);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003628
Andy Shevchenko3d060ae2015-07-27 18:04:00 +03003629static struct device *prev_device(struct klist_iter *i)
3630{
3631 struct klist_node *n = klist_prev(i);
3632 struct device *dev = NULL;
3633 struct device_private *p;
3634
3635 if (n) {
3636 p = to_device_private_parent(n);
3637 dev = p->device;
3638 }
3639 return dev;
3640}
3641
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08003642static struct device *next_device(struct klist_iter *i)
mochel@digitalimplant.org36239572005-03-24 19:08:30 -08003643{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08003644 struct klist_node *n = klist_next(i);
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08003645 struct device *dev = NULL;
3646 struct device_private *p;
3647
3648 if (n) {
3649 p = to_device_private_parent(n);
3650 dev = p->device;
3651 }
3652 return dev;
mochel@digitalimplant.org36239572005-03-24 19:08:30 -08003653}
3654
Linus Torvalds1da177e2005-04-16 15:20:36 -07003655/**
Kay Sieverse454cea2009-09-18 23:01:12 +02003656 * device_get_devnode - path of device node file
Kay Sievers6fcf53a2009-04-30 15:23:42 +02003657 * @dev: device
Kay Sieverse454cea2009-09-18 23:01:12 +02003658 * @mode: returned file access mode
Kay Sievers3c2670e2013-04-06 09:56:00 -07003659 * @uid: returned file owner
3660 * @gid: returned file group
Kay Sievers6fcf53a2009-04-30 15:23:42 +02003661 * @tmp: possibly allocated string
3662 *
3663 * Return the relative path of a possible device node.
3664 * Non-default names may need to allocate a memory to compose
3665 * a name. This memory is returned in tmp and needs to be
3666 * freed by the caller.
3667 */
Kay Sieverse454cea2009-09-18 23:01:12 +02003668const char *device_get_devnode(struct device *dev,
Greg Kroah-Hartman4e4098a2013-04-11 11:43:29 -07003669 umode_t *mode, kuid_t *uid, kgid_t *gid,
Kay Sievers3c2670e2013-04-06 09:56:00 -07003670 const char **tmp)
Kay Sievers6fcf53a2009-04-30 15:23:42 +02003671{
3672 char *s;
3673
3674 *tmp = NULL;
3675
3676 /* the device type may provide a specific name */
Kay Sieverse454cea2009-09-18 23:01:12 +02003677 if (dev->type && dev->type->devnode)
Kay Sievers3c2670e2013-04-06 09:56:00 -07003678 *tmp = dev->type->devnode(dev, mode, uid, gid);
Kay Sievers6fcf53a2009-04-30 15:23:42 +02003679 if (*tmp)
3680 return *tmp;
3681
3682 /* the class may provide a specific name */
Kay Sieverse454cea2009-09-18 23:01:12 +02003683 if (dev->class && dev->class->devnode)
3684 *tmp = dev->class->devnode(dev, mode);
Kay Sievers6fcf53a2009-04-30 15:23:42 +02003685 if (*tmp)
3686 return *tmp;
3687
3688 /* return name without allocation, tmp == NULL */
3689 if (strchr(dev_name(dev), '!') == NULL)
3690 return dev_name(dev);
3691
3692 /* replace '!' in the name with '/' */
Rasmus Villemoesa29fd612015-06-25 15:02:33 -07003693 s = kstrdup(dev_name(dev), GFP_KERNEL);
3694 if (!s)
Kay Sievers6fcf53a2009-04-30 15:23:42 +02003695 return NULL;
Rasmus Villemoesa29fd612015-06-25 15:02:33 -07003696 strreplace(s, '!', '/');
3697 return *tmp = s;
Kay Sievers6fcf53a2009-04-30 15:23:42 +02003698}
3699
3700/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08003701 * device_for_each_child - device child iterator.
3702 * @parent: parent struct device.
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08003703 * @fn: function to be called for each device.
Robert P. J. Dayf8878dc2013-06-01 20:17:34 -04003704 * @data: data for the callback.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003705 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08003706 * Iterate over @parent's child devices, and call @fn for each,
3707 * passing it @data.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003708 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08003709 * We check the return of @fn each time. If it returns anything
3710 * other than 0, we break out and return that value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003711 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08003712int device_for_each_child(struct device *parent, void *data,
3713 int (*fn)(struct device *dev, void *data))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003714{
mochel@digitalimplant.org36239572005-03-24 19:08:30 -08003715 struct klist_iter i;
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08003716 struct device *child;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003717 int error = 0;
3718
Greg Kroah-Hartman014c90db2009-04-15 16:00:12 -07003719 if (!parent->p)
3720 return 0;
3721
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08003722 klist_iter_init(&parent->p->klist_children, &i);
Gimcuan Hui93ead7c2017-11-11 05:52:54 +00003723 while (!error && (child = next_device(&i)))
mochel@digitalimplant.org36239572005-03-24 19:08:30 -08003724 error = fn(child, data);
3725 klist_iter_exit(&i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003726 return error;
3727}
David Graham White86df2682013-07-21 20:41:14 -04003728EXPORT_SYMBOL_GPL(device_for_each_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003729
Cornelia Huck5ab69982006-11-16 15:42:07 +01003730/**
Andy Shevchenko3d060ae2015-07-27 18:04:00 +03003731 * device_for_each_child_reverse - device child iterator in reversed order.
3732 * @parent: parent struct device.
3733 * @fn: function to be called for each device.
3734 * @data: data for the callback.
3735 *
3736 * Iterate over @parent's child devices, and call @fn for each,
3737 * passing it @data.
3738 *
3739 * We check the return of @fn each time. If it returns anything
3740 * other than 0, we break out and return that value.
3741 */
3742int device_for_each_child_reverse(struct device *parent, void *data,
3743 int (*fn)(struct device *dev, void *data))
3744{
3745 struct klist_iter i;
3746 struct device *child;
3747 int error = 0;
3748
3749 if (!parent->p)
3750 return 0;
3751
3752 klist_iter_init(&parent->p->klist_children, &i);
3753 while ((child = prev_device(&i)) && !error)
3754 error = fn(child, data);
3755 klist_iter_exit(&i);
3756 return error;
3757}
3758EXPORT_SYMBOL_GPL(device_for_each_child_reverse);
3759
3760/**
Cornelia Huck5ab69982006-11-16 15:42:07 +01003761 * device_find_child - device iterator for locating a particular device.
3762 * @parent: parent struct device
Cornelia Huck5ab69982006-11-16 15:42:07 +01003763 * @match: Callback function to check device
Robert P. J. Dayf8878dc2013-06-01 20:17:34 -04003764 * @data: Data to pass to match function
Cornelia Huck5ab69982006-11-16 15:42:07 +01003765 *
3766 * This is similar to the device_for_each_child() function above, but it
3767 * returns a reference to a device that is 'found' for later use, as
3768 * determined by the @match callback.
3769 *
3770 * The callback should return 0 if the device doesn't match and non-zero
3771 * if it does. If the callback returns non-zero and a reference to the
3772 * current device can be obtained, this function will return to the caller
3773 * and not iterate over any more devices.
Federico Vagaa4e24002013-04-15 11:18:11 +02003774 *
3775 * NOTE: you will need to drop the reference with put_device() after use.
Cornelia Huck5ab69982006-11-16 15:42:07 +01003776 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08003777struct device *device_find_child(struct device *parent, void *data,
3778 int (*match)(struct device *dev, void *data))
Cornelia Huck5ab69982006-11-16 15:42:07 +01003779{
3780 struct klist_iter i;
3781 struct device *child;
3782
3783 if (!parent)
3784 return NULL;
3785
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08003786 klist_iter_init(&parent->p->klist_children, &i);
Cornelia Huck5ab69982006-11-16 15:42:07 +01003787 while ((child = next_device(&i)))
3788 if (match(child, data) && get_device(child))
3789 break;
3790 klist_iter_exit(&i);
3791 return child;
3792}
David Graham White86df2682013-07-21 20:41:14 -04003793EXPORT_SYMBOL_GPL(device_find_child);
Cornelia Huck5ab69982006-11-16 15:42:07 +01003794
Heikki Krogerusdad9bb02019-05-31 17:15:37 +03003795/**
3796 * device_find_child_by_name - device iterator for locating a child device.
3797 * @parent: parent struct device
3798 * @name: name of the child device
3799 *
3800 * This is similar to the device_find_child() function above, but it
3801 * returns a reference to a device that has the name @name.
3802 *
3803 * NOTE: you will need to drop the reference with put_device() after use.
3804 */
3805struct device *device_find_child_by_name(struct device *parent,
3806 const char *name)
3807{
3808 struct klist_iter i;
3809 struct device *child;
3810
3811 if (!parent)
3812 return NULL;
3813
3814 klist_iter_init(&parent->p->klist_children, &i);
3815 while ((child = next_device(&i)))
Dan Williamsc77f5202020-10-13 16:50:18 -07003816 if (sysfs_streq(dev_name(child), name) && get_device(child))
Heikki Krogerusdad9bb02019-05-31 17:15:37 +03003817 break;
3818 klist_iter_exit(&i);
3819 return child;
3820}
3821EXPORT_SYMBOL_GPL(device_find_child_by_name);
3822
Linus Torvalds1da177e2005-04-16 15:20:36 -07003823int __init devices_init(void)
3824{
Greg Kroah-Hartman881c6cfd2007-11-01 09:29:06 -06003825 devices_kset = kset_create_and_add("devices", &device_uevent_ops, NULL);
3826 if (!devices_kset)
3827 return -ENOMEM;
Dan Williamse105b8b2008-04-21 10:51:07 -07003828 dev_kobj = kobject_create_and_add("dev", NULL);
3829 if (!dev_kobj)
3830 goto dev_kobj_err;
3831 sysfs_dev_block_kobj = kobject_create_and_add("block", dev_kobj);
3832 if (!sysfs_dev_block_kobj)
3833 goto block_kobj_err;
3834 sysfs_dev_char_kobj = kobject_create_and_add("char", dev_kobj);
3835 if (!sysfs_dev_char_kobj)
3836 goto char_kobj_err;
3837
Greg Kroah-Hartman881c6cfd2007-11-01 09:29:06 -06003838 return 0;
Dan Williamse105b8b2008-04-21 10:51:07 -07003839
3840 char_kobj_err:
3841 kobject_put(sysfs_dev_block_kobj);
3842 block_kobj_err:
3843 kobject_put(dev_kobj);
3844 dev_kobj_err:
3845 kset_unregister(devices_kset);
3846 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003847}
3848
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02003849static int device_check_offline(struct device *dev, void *not_used)
3850{
3851 int ret;
3852
3853 ret = device_for_each_child(dev, NULL, device_check_offline);
3854 if (ret)
3855 return ret;
3856
3857 return device_supports_offline(dev) && !dev->offline ? -EBUSY : 0;
3858}
3859
3860/**
3861 * device_offline - Prepare the device for hot-removal.
3862 * @dev: Device to be put offline.
3863 *
3864 * Execute the device bus type's .offline() callback, if present, to prepare
3865 * the device for a subsequent hot-removal. If that succeeds, the device must
3866 * not be used until either it is removed or its bus type's .online() callback
3867 * is executed.
3868 *
3869 * Call under device_hotplug_lock.
3870 */
3871int device_offline(struct device *dev)
3872{
3873 int ret;
3874
3875 if (dev->offline_disabled)
3876 return -EPERM;
3877
3878 ret = device_for_each_child(dev, NULL, device_check_offline);
3879 if (ret)
3880 return ret;
3881
3882 device_lock(dev);
3883 if (device_supports_offline(dev)) {
3884 if (dev->offline) {
3885 ret = 1;
3886 } else {
3887 ret = dev->bus->offline(dev);
3888 if (!ret) {
3889 kobject_uevent(&dev->kobj, KOBJ_OFFLINE);
3890 dev->offline = true;
3891 }
3892 }
3893 }
3894 device_unlock(dev);
3895
3896 return ret;
3897}
3898
3899/**
3900 * device_online - Put the device back online after successful device_offline().
3901 * @dev: Device to be put back online.
3902 *
3903 * If device_offline() has been successfully executed for @dev, but the device
3904 * has not been removed subsequently, execute its bus type's .online() callback
3905 * to indicate that the device can be used again.
3906 *
3907 * Call under device_hotplug_lock.
3908 */
3909int device_online(struct device *dev)
3910{
3911 int ret = 0;
3912
3913 device_lock(dev);
3914 if (device_supports_offline(dev)) {
3915 if (dev->offline) {
3916 ret = dev->bus->online(dev);
3917 if (!ret) {
3918 kobject_uevent(&dev->kobj, KOBJ_ONLINE);
3919 dev->offline = false;
3920 }
3921 } else {
3922 ret = 1;
3923 }
3924 }
3925 device_unlock(dev);
3926
3927 return ret;
3928}
3929
Karthigan Srinivasan7f100d12011-04-18 16:16:52 -05003930struct root_device {
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00003931 struct device dev;
3932 struct module *owner;
3933};
3934
Josh Triplett93058422012-11-18 21:27:55 -08003935static inline struct root_device *to_root_device(struct device *d)
Ferenc Wagner481e2072011-01-07 15:17:47 +01003936{
3937 return container_of(d, struct root_device, dev);
3938}
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00003939
3940static void root_device_release(struct device *dev)
3941{
3942 kfree(to_root_device(dev));
3943}
3944
3945/**
3946 * __root_device_register - allocate and register a root device
3947 * @name: root device name
3948 * @owner: owner module of the root device, usually THIS_MODULE
3949 *
3950 * This function allocates a root device and registers it
3951 * using device_register(). In order to free the returned
3952 * device, use root_device_unregister().
3953 *
3954 * Root devices are dummy devices which allow other devices
3955 * to be grouped under /sys/devices. Use this function to
3956 * allocate a root device and then use it as the parent of
3957 * any device which should appear under /sys/devices/{name}
3958 *
3959 * The /sys/devices/{name} directory will also contain a
3960 * 'module' symlink which points to the @owner directory
3961 * in sysfs.
3962 *
Jani Nikulaf0eae0e2010-03-11 18:11:45 +02003963 * Returns &struct device pointer on success, or ERR_PTR() on error.
3964 *
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00003965 * Note: You probably want to use root_device_register().
3966 */
3967struct device *__root_device_register(const char *name, struct module *owner)
3968{
3969 struct root_device *root;
3970 int err = -ENOMEM;
3971
3972 root = kzalloc(sizeof(struct root_device), GFP_KERNEL);
3973 if (!root)
3974 return ERR_PTR(err);
3975
Greg Kroah-Hartmanacc0e902009-06-02 15:39:55 -07003976 err = dev_set_name(&root->dev, "%s", name);
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00003977 if (err) {
3978 kfree(root);
3979 return ERR_PTR(err);
3980 }
3981
3982 root->dev.release = root_device_release;
3983
3984 err = device_register(&root->dev);
3985 if (err) {
3986 put_device(&root->dev);
3987 return ERR_PTR(err);
3988 }
3989
Christoph Egger1d9e8822010-05-17 16:57:58 +02003990#ifdef CONFIG_MODULES /* gotta find a "cleaner" way to do this */
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00003991 if (owner) {
3992 struct module_kobject *mk = &owner->mkobj;
3993
3994 err = sysfs_create_link(&root->dev.kobj, &mk->kobj, "module");
3995 if (err) {
3996 device_unregister(&root->dev);
3997 return ERR_PTR(err);
3998 }
3999 root->owner = owner;
4000 }
4001#endif
4002
4003 return &root->dev;
4004}
4005EXPORT_SYMBOL_GPL(__root_device_register);
4006
4007/**
4008 * root_device_unregister - unregister and free a root device
Randy Dunlap7cbcf222009-01-20 16:29:13 -08004009 * @dev: device going away
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00004010 *
4011 * This function unregisters and cleans up a device that was created by
4012 * root_device_register().
4013 */
4014void root_device_unregister(struct device *dev)
4015{
4016 struct root_device *root = to_root_device(dev);
4017
4018 if (root->owner)
4019 sysfs_remove_link(&root->dev.kobj, "module");
4020
4021 device_unregister(dev);
4022}
4023EXPORT_SYMBOL_GPL(root_device_unregister);
4024
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07004025
4026static void device_create_release(struct device *dev)
4027{
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01004028 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07004029 kfree(dev);
4030}
4031
Mathieu Malaterre6a8b55d2018-05-05 21:57:41 +02004032static __printf(6, 0) struct device *
Guenter Roeck39ef3112013-07-14 16:05:57 -07004033device_create_groups_vargs(struct class *class, struct device *parent,
4034 dev_t devt, void *drvdata,
4035 const struct attribute_group **groups,
4036 const char *fmt, va_list args)
4037{
4038 struct device *dev = NULL;
4039 int retval = -ENODEV;
4040
4041 if (class == NULL || IS_ERR(class))
4042 goto error;
4043
4044 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
4045 if (!dev) {
4046 retval = -ENOMEM;
4047 goto error;
4048 }
4049
David Herrmannbbc780f2013-11-21 20:15:48 +01004050 device_initialize(dev);
Guenter Roeck39ef3112013-07-14 16:05:57 -07004051 dev->devt = devt;
4052 dev->class = class;
4053 dev->parent = parent;
4054 dev->groups = groups;
4055 dev->release = device_create_release;
4056 dev_set_drvdata(dev, drvdata);
4057
4058 retval = kobject_set_name_vargs(&dev->kobj, fmt, args);
4059 if (retval)
4060 goto error;
4061
David Herrmannbbc780f2013-11-21 20:15:48 +01004062 retval = device_add(dev);
Guenter Roeck39ef3112013-07-14 16:05:57 -07004063 if (retval)
4064 goto error;
4065
4066 return dev;
4067
4068error:
4069 put_device(dev);
4070 return ERR_PTR(retval);
4071}
4072
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07004073/**
Greg Kroah-Hartman4e106732008-07-21 20:03:34 -07004074 * device_create - creates a device and registers it with sysfs
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07004075 * @class: pointer to the struct class that this device should be registered to
4076 * @parent: pointer to the parent struct device of this new device, if any
4077 * @devt: the dev_t for the char device to be added
4078 * @drvdata: the data to be added to the device for callbacks
4079 * @fmt: string for the device's name
4080 *
4081 * This function can be used by char device classes. A struct device
4082 * will be created in sysfs, registered to the specified class.
4083 *
4084 * A "dev" file will be created, showing the dev_t for the device, if
4085 * the dev_t is not 0,0.
4086 * If a pointer to a parent struct device is passed in, the newly created
4087 * struct device will be a child of that device in sysfs.
4088 * The pointer to the struct device will be returned from the call.
4089 * Any further sysfs files that might be required can be created using this
4090 * pointer.
4091 *
Jani Nikulaf0eae0e2010-03-11 18:11:45 +02004092 * Returns &struct device pointer on success, or ERR_PTR() on error.
4093 *
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07004094 * Note: the struct class passed to this function must have previously
4095 * been created with a call to class_create().
4096 */
Greg Kroah-Hartman4e106732008-07-21 20:03:34 -07004097struct device *device_create(struct class *class, struct device *parent,
4098 dev_t devt, void *drvdata, const char *fmt, ...)
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07004099{
4100 va_list vargs;
4101 struct device *dev;
4102
4103 va_start(vargs, fmt);
Christoph Hellwig4c747462020-05-04 14:47:57 +02004104 dev = device_create_groups_vargs(class, parent, devt, drvdata, NULL,
4105 fmt, vargs);
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07004106 va_end(vargs);
4107 return dev;
4108}
Greg Kroah-Hartman4e106732008-07-21 20:03:34 -07004109EXPORT_SYMBOL_GPL(device_create);
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07004110
Guenter Roeck39ef3112013-07-14 16:05:57 -07004111/**
4112 * device_create_with_groups - creates a device and registers it with sysfs
4113 * @class: pointer to the struct class that this device should be registered to
4114 * @parent: pointer to the parent struct device of this new device, if any
4115 * @devt: the dev_t for the char device to be added
4116 * @drvdata: the data to be added to the device for callbacks
4117 * @groups: NULL-terminated list of attribute groups to be created
4118 * @fmt: string for the device's name
4119 *
4120 * This function can be used by char device classes. A struct device
4121 * will be created in sysfs, registered to the specified class.
4122 * Additional attributes specified in the groups parameter will also
4123 * be created automatically.
4124 *
4125 * A "dev" file will be created, showing the dev_t for the device, if
4126 * the dev_t is not 0,0.
4127 * If a pointer to a parent struct device is passed in, the newly created
4128 * struct device will be a child of that device in sysfs.
4129 * The pointer to the struct device will be returned from the call.
4130 * Any further sysfs files that might be required can be created using this
4131 * pointer.
4132 *
4133 * Returns &struct device pointer on success, or ERR_PTR() on error.
4134 *
4135 * Note: the struct class passed to this function must have previously
4136 * been created with a call to class_create().
4137 */
4138struct device *device_create_with_groups(struct class *class,
4139 struct device *parent, dev_t devt,
4140 void *drvdata,
4141 const struct attribute_group **groups,
4142 const char *fmt, ...)
4143{
4144 va_list vargs;
4145 struct device *dev;
4146
4147 va_start(vargs, fmt);
4148 dev = device_create_groups_vargs(class, parent, devt, drvdata, groups,
4149 fmt, vargs);
4150 va_end(vargs);
4151 return dev;
4152}
4153EXPORT_SYMBOL_GPL(device_create_with_groups);
4154
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01004155/**
4156 * device_destroy - removes a device that was created with device_create()
4157 * @class: pointer to the struct class that this device was registered with
4158 * @devt: the dev_t of the device that was previously registered
4159 *
4160 * This call unregisters and cleans up a device that was created with a
4161 * call to device_create().
4162 */
4163void device_destroy(struct class *class, dev_t devt)
4164{
4165 struct device *dev;
4166
Suzuki K Poulose4495dfd2019-07-23 23:18:35 +01004167 dev = class_find_device_by_devt(class, devt);
Dave Youngcd354492008-01-28 16:56:11 +08004168 if (dev) {
4169 put_device(dev);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07004170 device_unregister(dev);
Dave Youngcd354492008-01-28 16:56:11 +08004171 }
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07004172}
4173EXPORT_SYMBOL_GPL(device_destroy);
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07004174
4175/**
4176 * device_rename - renames a device
4177 * @dev: the pointer to the struct device to be renamed
4178 * @new_name: the new name of the device
Eric W. Biederman030c1d22008-05-08 14:41:00 -07004179 *
4180 * It is the responsibility of the caller to provide mutual
4181 * exclusion between two different calls of device_rename
4182 * on the same device to ensure that new_name is valid and
4183 * won't conflict with other devices.
Michael Ellermanc6c0ac62010-11-25 09:44:07 +11004184 *
Timur Tabia5462512010-12-13 14:08:52 -06004185 * Note: Don't call this function. Currently, the networking layer calls this
4186 * function, but that will change. The following text from Kay Sievers offers
4187 * some insight:
4188 *
4189 * Renaming devices is racy at many levels, symlinks and other stuff are not
4190 * replaced atomically, and you get a "move" uevent, but it's not easy to
4191 * connect the event to the old and new device. Device nodes are not renamed at
4192 * all, there isn't even support for that in the kernel now.
4193 *
4194 * In the meantime, during renaming, your target name might be taken by another
4195 * driver, creating conflicts. Or the old name is taken directly after you
4196 * renamed it -- then you get events for the same DEVPATH, before you even see
4197 * the "move" event. It's just a mess, and nothing new should ever rely on
4198 * kernel device renaming. Besides that, it's not even implemented now for
4199 * other things than (driver-core wise very simple) network devices.
4200 *
4201 * We are currently about to change network renaming in udev to completely
4202 * disallow renaming of devices in the same namespace as the kernel uses,
4203 * because we can't solve the problems properly, that arise with swapping names
4204 * of multiple interfaces without races. Means, renaming of eth[0-9]* will only
4205 * be allowed to some other name than eth[0-9]*, for the aforementioned
4206 * reasons.
4207 *
4208 * Make up a "real" name in the driver before you register anything, or add
4209 * some other attributes for userspace to find the device, or use udev to add
4210 * symlinks -- but never rename kernel devices later, it's a complete mess. We
4211 * don't even want to get into that and try to implement the missing pieces in
4212 * the core. We really have other pieces to fix in the driver core mess. :)
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07004213 */
Johannes Berg6937e8f2010-08-05 17:38:18 +02004214int device_rename(struct device *dev, const char *new_name)
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07004215{
Tejun Heo4b30ee52013-09-11 22:29:06 -04004216 struct kobject *kobj = &dev->kobj;
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07004217 char *old_device_name = NULL;
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07004218 int error;
4219
4220 dev = get_device(dev);
4221 if (!dev)
4222 return -EINVAL;
4223
ethan.zhao69df7532013-10-13 22:12:35 +08004224 dev_dbg(dev, "renaming to %s\n", new_name);
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07004225
Kay Sievers1fa5ae82009-01-25 15:17:37 +01004226 old_device_name = kstrdup(dev_name(dev), GFP_KERNEL);
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07004227 if (!old_device_name) {
4228 error = -ENOMEM;
4229 goto out;
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07004230 }
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07004231
Eric W. Biedermanf349cf32010-03-30 11:31:29 -07004232 if (dev->class) {
Tejun Heo4b30ee52013-09-11 22:29:06 -04004233 error = sysfs_rename_link_ns(&dev->class->p->subsys.kobj,
4234 kobj, old_device_name,
4235 new_name, kobject_namespace(kobj));
Eric W. Biedermanf349cf32010-03-30 11:31:29 -07004236 if (error)
4237 goto out;
4238 }
Kay Sievers39aba962010-09-04 22:33:14 -07004239
Tejun Heo4b30ee52013-09-11 22:29:06 -04004240 error = kobject_rename(kobj, new_name);
Kay Sievers1fa5ae82009-01-25 15:17:37 +01004241 if (error)
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07004242 goto out;
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07004243
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07004244out:
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07004245 put_device(dev);
4246
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07004247 kfree(old_device_name);
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07004248
4249 return error;
4250}
Johannes Berga2807db2007-02-28 12:38:31 +01004251EXPORT_SYMBOL_GPL(device_rename);
Cornelia Huck8a824722006-11-20 17:07:51 +01004252
4253static int device_move_class_links(struct device *dev,
4254 struct device *old_parent,
4255 struct device *new_parent)
4256{
Greg Kroah-Hartmanf7f34612007-03-06 12:55:53 -08004257 int error = 0;
Cornelia Huck8a824722006-11-20 17:07:51 +01004258
Greg Kroah-Hartmanf7f34612007-03-06 12:55:53 -08004259 if (old_parent)
4260 sysfs_remove_link(&dev->kobj, "device");
4261 if (new_parent)
4262 error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
4263 "device");
4264 return error;
Cornelia Huck8a824722006-11-20 17:07:51 +01004265}
4266
4267/**
4268 * device_move - moves a device to a new parent
4269 * @dev: the pointer to the struct device to be moved
Wolfram Sang13509862018-05-06 13:23:47 +02004270 * @new_parent: the new parent of the device (can be NULL)
Cornelia Huckffa6a702009-03-04 12:44:00 +01004271 * @dpm_order: how to reorder the dpm_list
Cornelia Huck8a824722006-11-20 17:07:51 +01004272 */
Cornelia Huckffa6a702009-03-04 12:44:00 +01004273int device_move(struct device *dev, struct device *new_parent,
4274 enum dpm_order dpm_order)
Cornelia Huck8a824722006-11-20 17:07:51 +01004275{
4276 int error;
4277 struct device *old_parent;
Cornelia Huckc744aeae2007-01-08 20:16:44 +01004278 struct kobject *new_parent_kobj;
Cornelia Huck8a824722006-11-20 17:07:51 +01004279
4280 dev = get_device(dev);
4281 if (!dev)
4282 return -EINVAL;
4283
Cornelia Huckffa6a702009-03-04 12:44:00 +01004284 device_pm_lock();
Cornelia Huck8a824722006-11-20 17:07:51 +01004285 new_parent = get_device(new_parent);
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08004286 new_parent_kobj = get_device_parent(dev, new_parent);
Tetsuo Handa84d0c272018-05-07 19:10:31 +09004287 if (IS_ERR(new_parent_kobj)) {
4288 error = PTR_ERR(new_parent_kobj);
4289 put_device(new_parent);
4290 goto out;
4291 }
Cornelia Huck63b69712008-01-21 16:09:44 +01004292
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01004293 pr_debug("device: '%s': %s: moving to '%s'\n", dev_name(dev),
4294 __func__, new_parent ? dev_name(new_parent) : "<NULL>");
Cornelia Huckc744aeae2007-01-08 20:16:44 +01004295 error = kobject_move(&dev->kobj, new_parent_kobj);
Cornelia Huck8a824722006-11-20 17:07:51 +01004296 if (error) {
Cornelia Huck63b69712008-01-21 16:09:44 +01004297 cleanup_glue_dir(dev, new_parent_kobj);
Cornelia Huck8a824722006-11-20 17:07:51 +01004298 put_device(new_parent);
4299 goto out;
4300 }
4301 old_parent = dev->parent;
4302 dev->parent = new_parent;
4303 if (old_parent)
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08004304 klist_remove(&dev->p->knode_parent);
Yinghai Lu0d358f22008-02-19 03:20:41 -08004305 if (new_parent) {
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08004306 klist_add_tail(&dev->p->knode_parent,
4307 &new_parent->p->klist_children);
Yinghai Lu0d358f22008-02-19 03:20:41 -08004308 set_dev_node(dev, dev_to_node(new_parent));
4309 }
4310
Rabin Vincentbdd40342012-04-23 09:16:36 +02004311 if (dev->class) {
4312 error = device_move_class_links(dev, old_parent, new_parent);
4313 if (error) {
4314 /* We ignore errors on cleanup since we're hosed anyway... */
4315 device_move_class_links(dev, new_parent, old_parent);
4316 if (!kobject_move(&dev->kobj, &old_parent->kobj)) {
4317 if (new_parent)
4318 klist_remove(&dev->p->knode_parent);
4319 dev->parent = old_parent;
4320 if (old_parent) {
4321 klist_add_tail(&dev->p->knode_parent,
4322 &old_parent->p->klist_children);
4323 set_dev_node(dev, dev_to_node(old_parent));
4324 }
Yinghai Lu0d358f22008-02-19 03:20:41 -08004325 }
Rabin Vincentbdd40342012-04-23 09:16:36 +02004326 cleanup_glue_dir(dev, new_parent_kobj);
4327 put_device(new_parent);
4328 goto out;
Cornelia Huck8a824722006-11-20 17:07:51 +01004329 }
Cornelia Huck8a824722006-11-20 17:07:51 +01004330 }
Cornelia Huckffa6a702009-03-04 12:44:00 +01004331 switch (dpm_order) {
4332 case DPM_ORDER_NONE:
4333 break;
4334 case DPM_ORDER_DEV_AFTER_PARENT:
4335 device_pm_move_after(dev, new_parent);
Grygorii Strashko52cdbdd2015-07-27 20:43:01 +03004336 devices_kset_move_after(dev, new_parent);
Cornelia Huckffa6a702009-03-04 12:44:00 +01004337 break;
4338 case DPM_ORDER_PARENT_BEFORE_DEV:
4339 device_pm_move_before(new_parent, dev);
Grygorii Strashko52cdbdd2015-07-27 20:43:01 +03004340 devices_kset_move_before(new_parent, dev);
Cornelia Huckffa6a702009-03-04 12:44:00 +01004341 break;
4342 case DPM_ORDER_DEV_LAST:
4343 device_pm_move_last(dev);
Grygorii Strashko52cdbdd2015-07-27 20:43:01 +03004344 devices_kset_move_last(dev);
Cornelia Huckffa6a702009-03-04 12:44:00 +01004345 break;
4346 }
Rabin Vincentbdd40342012-04-23 09:16:36 +02004347
Cornelia Huck8a824722006-11-20 17:07:51 +01004348 put_device(old_parent);
4349out:
Cornelia Huckffa6a702009-03-04 12:44:00 +01004350 device_pm_unlock();
Cornelia Huck8a824722006-11-20 17:07:51 +01004351 put_device(dev);
4352 return error;
4353}
Cornelia Huck8a824722006-11-20 17:07:51 +01004354EXPORT_SYMBOL_GPL(device_move);
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08004355
Christian Braunerb8f33e52020-02-27 04:37:15 +01004356static int device_attrs_change_owner(struct device *dev, kuid_t kuid,
4357 kgid_t kgid)
4358{
4359 struct kobject *kobj = &dev->kobj;
4360 struct class *class = dev->class;
4361 const struct device_type *type = dev->type;
4362 int error;
4363
4364 if (class) {
4365 /*
4366 * Change the device groups of the device class for @dev to
4367 * @kuid/@kgid.
4368 */
4369 error = sysfs_groups_change_owner(kobj, class->dev_groups, kuid,
4370 kgid);
4371 if (error)
4372 return error;
4373 }
4374
4375 if (type) {
4376 /*
4377 * Change the device groups of the device type for @dev to
4378 * @kuid/@kgid.
4379 */
4380 error = sysfs_groups_change_owner(kobj, type->groups, kuid,
4381 kgid);
4382 if (error)
4383 return error;
4384 }
4385
4386 /* Change the device groups of @dev to @kuid/@kgid. */
4387 error = sysfs_groups_change_owner(kobj, dev->groups, kuid, kgid);
4388 if (error)
4389 return error;
4390
4391 if (device_supports_offline(dev) && !dev->offline_disabled) {
4392 /* Change online device attributes of @dev to @kuid/@kgid. */
4393 error = sysfs_file_change_owner(kobj, dev_attr_online.attr.name,
4394 kuid, kgid);
4395 if (error)
4396 return error;
4397 }
4398
4399 return 0;
4400}
4401
4402/**
4403 * device_change_owner - change the owner of an existing device.
4404 * @dev: device.
4405 * @kuid: new owner's kuid
4406 * @kgid: new owner's kgid
4407 *
4408 * This changes the owner of @dev and its corresponding sysfs entries to
4409 * @kuid/@kgid. This function closely mirrors how @dev was added via driver
4410 * core.
4411 *
4412 * Returns 0 on success or error code on failure.
4413 */
4414int device_change_owner(struct device *dev, kuid_t kuid, kgid_t kgid)
4415{
4416 int error;
4417 struct kobject *kobj = &dev->kobj;
4418
4419 dev = get_device(dev);
4420 if (!dev)
4421 return -EINVAL;
4422
4423 /*
4424 * Change the kobject and the default attributes and groups of the
4425 * ktype associated with it to @kuid/@kgid.
4426 */
4427 error = sysfs_change_owner(kobj, kuid, kgid);
4428 if (error)
4429 goto out;
4430
4431 /*
4432 * Change the uevent file for @dev to the new owner. The uevent file
4433 * was created in a separate step when @dev got added and we mirror
4434 * that step here.
4435 */
4436 error = sysfs_file_change_owner(kobj, dev_attr_uevent.attr.name, kuid,
4437 kgid);
4438 if (error)
4439 goto out;
4440
4441 /*
4442 * Change the device groups, the device groups associated with the
4443 * device class, and the groups associated with the device type of @dev
4444 * to @kuid/@kgid.
4445 */
4446 error = device_attrs_change_owner(dev, kuid, kgid);
4447 if (error)
4448 goto out;
4449
Christian Brauner3b52fc52020-02-27 04:37:16 +01004450 error = dpm_sysfs_change_owner(dev, kuid, kgid);
4451 if (error)
4452 goto out;
4453
Christian Braunerb8f33e52020-02-27 04:37:15 +01004454#ifdef CONFIG_BLOCK
4455 if (sysfs_deprecated && dev->class == &block_class)
4456 goto out;
4457#endif
4458
4459 /*
4460 * Change the owner of the symlink located in the class directory of
4461 * the device class associated with @dev which points to the actual
4462 * directory entry for @dev to @kuid/@kgid. This ensures that the
4463 * symlink shows the same permissions as its target.
4464 */
4465 error = sysfs_link_change_owner(&dev->class->p->subsys.kobj, &dev->kobj,
4466 dev_name(dev), kuid, kgid);
4467 if (error)
4468 goto out;
4469
4470out:
4471 put_device(dev);
4472 return error;
4473}
4474EXPORT_SYMBOL_GPL(device_change_owner);
4475
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08004476/**
4477 * device_shutdown - call ->shutdown() on each device to shutdown.
4478 */
4479void device_shutdown(void)
4480{
Benson Leungf123db82013-09-24 20:05:11 -07004481 struct device *dev, *parent;
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08004482
Pingfan Liu3297c8f2018-07-19 13:14:58 +08004483 wait_for_device_probe();
4484 device_block_probing();
4485
Rafael J. Wysocki65650b32019-10-09 01:29:10 +02004486 cpufreq_suspend();
4487
Hugh Daschbach62458382010-03-22 10:36:37 -07004488 spin_lock(&devices_kset->list_lock);
4489 /*
4490 * Walk the devices list backward, shutting down each in turn.
4491 * Beware that device unplug events may also start pulling
4492 * devices offline, even as the system is shutting down.
4493 */
4494 while (!list_empty(&devices_kset->list)) {
4495 dev = list_entry(devices_kset->list.prev, struct device,
4496 kobj.entry);
Ming Leid1c6c032012-06-22 18:01:40 +08004497
4498 /*
4499 * hold reference count of device's parent to
4500 * prevent it from being freed because parent's
4501 * lock is to be held
4502 */
Benson Leungf123db82013-09-24 20:05:11 -07004503 parent = get_device(dev->parent);
Hugh Daschbach62458382010-03-22 10:36:37 -07004504 get_device(dev);
4505 /*
4506 * Make sure the device is off the kset list, in the
4507 * event that dev->*->shutdown() doesn't remove it.
4508 */
4509 list_del_init(&dev->kobj.entry);
4510 spin_unlock(&devices_kset->list_lock);
Alan Sternfe6b91f2011-12-06 23:24:52 +01004511
Ming Leid1c6c032012-06-22 18:01:40 +08004512 /* hold lock to avoid race with probe/release */
Benson Leungf123db82013-09-24 20:05:11 -07004513 if (parent)
4514 device_lock(parent);
Ming Leid1c6c032012-06-22 18:01:40 +08004515 device_lock(dev);
4516
Alan Sternfe6b91f2011-12-06 23:24:52 +01004517 /* Don't allow any more runtime suspends */
4518 pm_runtime_get_noresume(dev);
4519 pm_runtime_barrier(dev);
Hugh Daschbach62458382010-03-22 10:36:37 -07004520
Michal Suchanek75216212017-08-11 15:44:43 +02004521 if (dev->class && dev->class->shutdown_pre) {
Josh Zimmermanf77af152017-06-25 14:53:23 -07004522 if (initcall_debug)
Michal Suchanek75216212017-08-11 15:44:43 +02004523 dev_info(dev, "shutdown_pre\n");
4524 dev->class->shutdown_pre(dev);
4525 }
4526 if (dev->bus && dev->bus->shutdown) {
ShuoX Liu0246c4f2012-11-23 15:14:12 +08004527 if (initcall_debug)
4528 dev_info(dev, "shutdown\n");
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08004529 dev->bus->shutdown(dev);
4530 } else if (dev->driver && dev->driver->shutdown) {
ShuoX Liu0246c4f2012-11-23 15:14:12 +08004531 if (initcall_debug)
4532 dev_info(dev, "shutdown\n");
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08004533 dev->driver->shutdown(dev);
4534 }
Ming Leid1c6c032012-06-22 18:01:40 +08004535
4536 device_unlock(dev);
Benson Leungf123db82013-09-24 20:05:11 -07004537 if (parent)
4538 device_unlock(parent);
Ming Leid1c6c032012-06-22 18:01:40 +08004539
Hugh Daschbach62458382010-03-22 10:36:37 -07004540 put_device(dev);
Benson Leungf123db82013-09-24 20:05:11 -07004541 put_device(parent);
Hugh Daschbach62458382010-03-22 10:36:37 -07004542
4543 spin_lock(&devices_kset->list_lock);
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08004544 }
Hugh Daschbach62458382010-03-22 10:36:37 -07004545 spin_unlock(&devices_kset->list_lock);
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08004546}
Joe Perches99bcf212010-06-27 01:02:34 +00004547
4548/*
4549 * Device logging functions
4550 */
4551
4552#ifdef CONFIG_PRINTK
John Ogness74caba72020-09-21 13:24:45 +02064553static void
4554set_dev_info(const struct device *dev, struct dev_printk_info *dev_info)
Joe Perches99bcf212010-06-27 01:02:34 +00004555{
Kay Sieversc4e00da2012-05-03 02:29:59 +02004556 const char *subsys;
John Ogness74caba72020-09-21 13:24:45 +02064557
4558 memset(dev_info, 0, sizeof(*dev_info));
Joe Perches99bcf212010-06-27 01:02:34 +00004559
Kay Sieversc4e00da2012-05-03 02:29:59 +02004560 if (dev->class)
4561 subsys = dev->class->name;
4562 else if (dev->bus)
4563 subsys = dev->bus->name;
4564 else
John Ogness74caba72020-09-21 13:24:45 +02064565 return;
Kay Sieversc4e00da2012-05-03 02:29:59 +02004566
John Ogness74caba72020-09-21 13:24:45 +02064567 strscpy(dev_info->subsystem, subsys, sizeof(dev_info->subsystem));
Kay Sieversc4e00da2012-05-03 02:29:59 +02004568
4569 /*
4570 * Add device identifier DEVICE=:
4571 * b12:8 block dev_t
4572 * c127:3 char dev_t
4573 * n8 netdev ifindex
4574 * +sound:card0 subsystem:devname
4575 */
4576 if (MAJOR(dev->devt)) {
4577 char c;
4578
4579 if (strcmp(subsys, "block") == 0)
4580 c = 'b';
4581 else
4582 c = 'c';
John Ogness74caba72020-09-21 13:24:45 +02064583
4584 snprintf(dev_info->device, sizeof(dev_info->device),
4585 "%c%u:%u", c, MAJOR(dev->devt), MINOR(dev->devt));
Kay Sieversc4e00da2012-05-03 02:29:59 +02004586 } else if (strcmp(subsys, "net") == 0) {
4587 struct net_device *net = to_net_dev(dev);
4588
John Ogness74caba72020-09-21 13:24:45 +02064589 snprintf(dev_info->device, sizeof(dev_info->device),
4590 "n%u", net->ifindex);
Kay Sieversc4e00da2012-05-03 02:29:59 +02004591 } else {
John Ogness74caba72020-09-21 13:24:45 +02064592 snprintf(dev_info->device, sizeof(dev_info->device),
4593 "+%s:%s", subsys, dev_name(dev));
Kay Sieversc4e00da2012-05-03 02:29:59 +02004594 }
Joe Perches99bcf212010-06-27 01:02:34 +00004595}
Joe Perches798efc62012-09-12 20:11:29 -07004596
Joe Perches05e4e5b2012-09-12 20:13:37 -07004597int dev_vprintk_emit(int level, const struct device *dev,
4598 const char *fmt, va_list args)
4599{
John Ogness74caba72020-09-21 13:24:45 +02064600 struct dev_printk_info dev_info;
Joe Perches05e4e5b2012-09-12 20:13:37 -07004601
John Ogness74caba72020-09-21 13:24:45 +02064602 set_dev_info(dev, &dev_info);
Joe Perches05e4e5b2012-09-12 20:13:37 -07004603
John Ogness74caba72020-09-21 13:24:45 +02064604 return vprintk_emit(0, level, &dev_info, fmt, args);
Joe Perches05e4e5b2012-09-12 20:13:37 -07004605}
4606EXPORT_SYMBOL(dev_vprintk_emit);
4607
4608int dev_printk_emit(int level, const struct device *dev, const char *fmt, ...)
4609{
4610 va_list args;
4611 int r;
4612
4613 va_start(args, fmt);
4614
4615 r = dev_vprintk_emit(level, dev, fmt, args);
4616
4617 va_end(args);
4618
4619 return r;
4620}
4621EXPORT_SYMBOL(dev_printk_emit);
4622
Joe Perchesd1f10522014-12-25 15:07:04 -08004623static void __dev_printk(const char *level, const struct device *dev,
Joe Perches798efc62012-09-12 20:11:29 -07004624 struct va_format *vaf)
4625{
Joe Perchesd1f10522014-12-25 15:07:04 -08004626 if (dev)
4627 dev_printk_emit(level[1] - '0', dev, "%s %s: %pV",
4628 dev_driver_string(dev), dev_name(dev), vaf);
4629 else
4630 printk("%s(NULL device *): %pV", level, vaf);
Joe Perches798efc62012-09-12 20:11:29 -07004631}
Joe Perches99bcf212010-06-27 01:02:34 +00004632
Chris Downad7d61f2021-06-15 17:52:56 +01004633void _dev_printk(const char *level, const struct device *dev,
4634 const char *fmt, ...)
Joe Perches99bcf212010-06-27 01:02:34 +00004635{
4636 struct va_format vaf;
4637 va_list args;
Joe Perches99bcf212010-06-27 01:02:34 +00004638
4639 va_start(args, fmt);
4640
4641 vaf.fmt = fmt;
4642 vaf.va = &args;
4643
Joe Perchesd1f10522014-12-25 15:07:04 -08004644 __dev_printk(level, dev, &vaf);
Joe Perches798efc62012-09-12 20:11:29 -07004645
Joe Perches99bcf212010-06-27 01:02:34 +00004646 va_end(args);
Joe Perches99bcf212010-06-27 01:02:34 +00004647}
Chris Downad7d61f2021-06-15 17:52:56 +01004648EXPORT_SYMBOL(_dev_printk);
Joe Perches99bcf212010-06-27 01:02:34 +00004649
4650#define define_dev_printk_level(func, kern_level) \
Joe Perchesd1f10522014-12-25 15:07:04 -08004651void func(const struct device *dev, const char *fmt, ...) \
Joe Perches99bcf212010-06-27 01:02:34 +00004652{ \
4653 struct va_format vaf; \
4654 va_list args; \
Joe Perches99bcf212010-06-27 01:02:34 +00004655 \
4656 va_start(args, fmt); \
4657 \
4658 vaf.fmt = fmt; \
4659 vaf.va = &args; \
4660 \
Joe Perchesd1f10522014-12-25 15:07:04 -08004661 __dev_printk(kern_level, dev, &vaf); \
Joe Perches798efc62012-09-12 20:11:29 -07004662 \
Joe Perches99bcf212010-06-27 01:02:34 +00004663 va_end(args); \
Joe Perches99bcf212010-06-27 01:02:34 +00004664} \
4665EXPORT_SYMBOL(func);
4666
Joe Perches663336e2018-05-09 08:15:46 -07004667define_dev_printk_level(_dev_emerg, KERN_EMERG);
4668define_dev_printk_level(_dev_alert, KERN_ALERT);
4669define_dev_printk_level(_dev_crit, KERN_CRIT);
4670define_dev_printk_level(_dev_err, KERN_ERR);
4671define_dev_printk_level(_dev_warn, KERN_WARNING);
4672define_dev_printk_level(_dev_notice, KERN_NOTICE);
Joe Perches99bcf212010-06-27 01:02:34 +00004673define_dev_printk_level(_dev_info, KERN_INFO);
4674
4675#endif
Rafael J. Wysocki97badf82015-04-03 23:23:37 +02004676
Andrzej Hajdaa787e542020-07-13 16:43:21 +02004677/**
4678 * dev_err_probe - probe error check and log helper
4679 * @dev: the pointer to the struct device
4680 * @err: error value to test
4681 * @fmt: printf-style format string
4682 * @...: arguments as specified in the format string
4683 *
4684 * This helper implements common pattern present in probe functions for error
4685 * checking: print debug or error message depending if the error value is
4686 * -EPROBE_DEFER and propagate error upwards.
Andrzej Hajdad090b702020-07-13 16:43:22 +02004687 * In case of -EPROBE_DEFER it sets also defer probe reason, which can be
4688 * checked later by reading devices_deferred debugfs attribute.
Mauro Carvalho Chehab074b3aa2020-09-09 11:53:43 +02004689 * It replaces code sequence::
4690 *
Andrzej Hajdaa787e542020-07-13 16:43:21 +02004691 * if (err != -EPROBE_DEFER)
4692 * dev_err(dev, ...);
4693 * else
4694 * dev_dbg(dev, ...);
4695 * return err;
Mauro Carvalho Chehab074b3aa2020-09-09 11:53:43 +02004696 *
4697 * with::
4698 *
Andrzej Hajdaa787e542020-07-13 16:43:21 +02004699 * return dev_err_probe(dev, err, ...);
4700 *
Douglas Anderson7065f922021-09-16 16:19:40 -07004701 * Note that it is deemed acceptable to use this function for error
4702 * prints during probe even if the @err is known to never be -EPROBE_DEFER.
4703 * The benefit compared to a normal dev_err() is the standardized format
4704 * of the error code and the fact that the error code is returned.
4705 *
Andrzej Hajdaa787e542020-07-13 16:43:21 +02004706 * Returns @err.
4707 *
4708 */
4709int dev_err_probe(const struct device *dev, int err, const char *fmt, ...)
4710{
4711 struct va_format vaf;
4712 va_list args;
4713
4714 va_start(args, fmt);
4715 vaf.fmt = fmt;
4716 vaf.va = &args;
4717
Andrzej Hajdad090b702020-07-13 16:43:22 +02004718 if (err != -EPROBE_DEFER) {
Michał Mirosław693a8e92020-08-28 18:14:35 +02004719 dev_err(dev, "error %pe: %pV", ERR_PTR(err), &vaf);
Andrzej Hajdad090b702020-07-13 16:43:22 +02004720 } else {
4721 device_set_deferred_probe_reason(dev, &vaf);
Michał Mirosław693a8e92020-08-28 18:14:35 +02004722 dev_dbg(dev, "error %pe: %pV", ERR_PTR(err), &vaf);
Andrzej Hajdad090b702020-07-13 16:43:22 +02004723 }
Andrzej Hajdaa787e542020-07-13 16:43:21 +02004724
4725 va_end(args);
4726
4727 return err;
4728}
4729EXPORT_SYMBOL_GPL(dev_err_probe);
4730
Rafael J. Wysocki97badf82015-04-03 23:23:37 +02004731static inline bool fwnode_is_primary(struct fwnode_handle *fwnode)
4732{
4733 return fwnode && !IS_ERR(fwnode->secondary);
4734}
4735
4736/**
4737 * set_primary_fwnode - Change the primary firmware node of a given device.
4738 * @dev: Device to handle.
4739 * @fwnode: New primary firmware node of the device.
4740 *
4741 * Set the device's firmware node pointer to @fwnode, but if a secondary
4742 * firmware node of the device is present, preserve it.
Bard Liao3f7bdda2021-01-05 17:11:46 +08004743 *
4744 * Valid fwnode cases are:
4745 * - primary --> secondary --> -ENODEV
4746 * - primary --> NULL
4747 * - secondary --> -ENODEV
4748 * - NULL
Rafael J. Wysocki97badf82015-04-03 23:23:37 +02004749 */
4750void set_primary_fwnode(struct device *dev, struct fwnode_handle *fwnode)
4751{
Andy Shevchenko99aed922020-10-22 21:41:00 +03004752 struct device *parent = dev->parent;
Heikki Krogerusc15e1bd2020-08-21 13:53:42 +03004753 struct fwnode_handle *fn = dev->fwnode;
Rafael J. Wysocki97badf82015-04-03 23:23:37 +02004754
Heikki Krogerusc15e1bd2020-08-21 13:53:42 +03004755 if (fwnode) {
Rafael J. Wysocki97badf82015-04-03 23:23:37 +02004756 if (fwnode_is_primary(fn))
4757 fn = fn->secondary;
4758
Mika Westerberg55f89a82015-11-30 17:11:39 +02004759 if (fn) {
4760 WARN_ON(fwnode->secondary);
4761 fwnode->secondary = fn;
4762 }
Rafael J. Wysocki97badf82015-04-03 23:23:37 +02004763 dev->fwnode = fwnode;
4764 } else {
Heikki Krogerusc15e1bd2020-08-21 13:53:42 +03004765 if (fwnode_is_primary(fn)) {
4766 dev->fwnode = fn->secondary;
Bard Liao3f7bdda2021-01-05 17:11:46 +08004767 /* Set fn->secondary = NULL, so fn remains the primary fwnode */
Andy Shevchenko99aed922020-10-22 21:41:00 +03004768 if (!(parent && fn == parent->fwnode))
Bard Liao47f44692021-01-05 17:11:45 +08004769 fn->secondary = NULL;
Heikki Krogerusc15e1bd2020-08-21 13:53:42 +03004770 } else {
4771 dev->fwnode = NULL;
4772 }
Rafael J. Wysocki97badf82015-04-03 23:23:37 +02004773 }
4774}
4775EXPORT_SYMBOL_GPL(set_primary_fwnode);
4776
4777/**
4778 * set_secondary_fwnode - Change the secondary firmware node of a given device.
4779 * @dev: Device to handle.
4780 * @fwnode: New secondary firmware node of the device.
4781 *
4782 * If a primary firmware node of the device is present, set its secondary
4783 * pointer to @fwnode. Otherwise, set the device's firmware node pointer to
4784 * @fwnode.
4785 */
4786void set_secondary_fwnode(struct device *dev, struct fwnode_handle *fwnode)
4787{
4788 if (fwnode)
4789 fwnode->secondary = ERR_PTR(-ENODEV);
4790
4791 if (fwnode_is_primary(dev->fwnode))
4792 dev->fwnode->secondary = fwnode;
4793 else
4794 dev->fwnode = fwnode;
4795}
Andy Shevchenko96489ae2020-04-08 19:09:01 +03004796EXPORT_SYMBOL_GPL(set_secondary_fwnode);
Johan Hovold4e75e1d2017-06-06 17:59:00 +02004797
4798/**
4799 * device_set_of_node_from_dev - reuse device-tree node of another device
4800 * @dev: device whose device-tree node is being set
4801 * @dev2: device whose device-tree node is being reused
4802 *
4803 * Takes another reference to the new device-tree node after first dropping
4804 * any reference held to the old node.
4805 */
4806void device_set_of_node_from_dev(struct device *dev, const struct device *dev2)
4807{
4808 of_node_put(dev->of_node);
4809 dev->of_node = of_node_get(dev2->of_node);
4810 dev->of_node_reused = true;
4811}
4812EXPORT_SYMBOL_GPL(device_set_of_node_from_dev);
Suzuki K Poulose65b66682019-06-14 18:54:01 +01004813
Ioana Ciornei43e76d42021-06-17 15:29:04 +03004814void device_set_node(struct device *dev, struct fwnode_handle *fwnode)
4815{
4816 dev->fwnode = fwnode;
4817 dev->of_node = to_of_node(fwnode);
4818}
4819EXPORT_SYMBOL_GPL(device_set_node);
4820
Suzuki K Poulose6cda08a2019-07-23 23:18:32 +01004821int device_match_name(struct device *dev, const void *name)
4822{
4823 return sysfs_streq(dev_name(dev), name);
4824}
4825EXPORT_SYMBOL_GPL(device_match_name);
4826
Suzuki K Poulose65b66682019-06-14 18:54:01 +01004827int device_match_of_node(struct device *dev, const void *np)
4828{
4829 return dev->of_node == np;
4830}
4831EXPORT_SYMBOL_GPL(device_match_of_node);
Suzuki K Poulose67843bb2019-07-23 23:18:34 +01004832
4833int device_match_fwnode(struct device *dev, const void *fwnode)
4834{
4835 return dev_fwnode(dev) == fwnode;
4836}
4837EXPORT_SYMBOL_GPL(device_match_fwnode);
Suzuki K Poulose4495dfd2019-07-23 23:18:35 +01004838
4839int device_match_devt(struct device *dev, const void *pdevt)
4840{
4841 return dev->devt == *(dev_t *)pdevt;
4842}
4843EXPORT_SYMBOL_GPL(device_match_devt);
Suzuki K Poulose00500142019-07-23 23:18:36 +01004844
4845int device_match_acpi_dev(struct device *dev, const void *adev)
4846{
4847 return ACPI_COMPANION(dev) == adev;
4848}
4849EXPORT_SYMBOL(device_match_acpi_dev);
Suzuki K Poulose6bf85ba2019-07-23 23:18:37 +01004850
Andy Shevchenkoa164ff52021-10-14 16:47:54 +03004851int device_match_acpi_handle(struct device *dev, const void *handle)
4852{
4853 return ACPI_HANDLE(dev) == handle;
4854}
4855EXPORT_SYMBOL(device_match_acpi_handle);
4856
Suzuki K Poulose6bf85ba2019-07-23 23:18:37 +01004857int device_match_any(struct device *dev, const void *unused)
4858{
4859 return 1;
4860}
4861EXPORT_SYMBOL_GPL(device_match_any);