blob: 316df602709353a71eb0eae7979d50a2cbe3f6d9 [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);
98out:
99 mutex_unlock(&fwnode_link_lock);
100
101 return ret;
102}
103
104/**
105 * fwnode_links_purge_suppliers - Delete all supplier links of fwnode_handle.
106 * @fwnode: fwnode whose supplier links need to be deleted
107 *
108 * Deletes all supplier links connecting directly to @fwnode.
109 */
110static void fwnode_links_purge_suppliers(struct fwnode_handle *fwnode)
111{
112 struct fwnode_link *link, *tmp;
113
114 mutex_lock(&fwnode_link_lock);
115 list_for_each_entry_safe(link, tmp, &fwnode->suppliers, c_hook) {
116 list_del(&link->s_hook);
117 list_del(&link->c_hook);
118 kfree(link);
119 }
120 mutex_unlock(&fwnode_link_lock);
121}
122
123/**
124 * fwnode_links_purge_consumers - Delete all consumer links of fwnode_handle.
125 * @fwnode: fwnode whose consumer links need to be deleted
126 *
127 * Deletes all consumer links connecting directly to @fwnode.
128 */
129static void fwnode_links_purge_consumers(struct fwnode_handle *fwnode)
130{
131 struct fwnode_link *link, *tmp;
132
133 mutex_lock(&fwnode_link_lock);
134 list_for_each_entry_safe(link, tmp, &fwnode->consumers, s_hook) {
135 list_del(&link->s_hook);
136 list_del(&link->c_hook);
137 kfree(link);
138 }
139 mutex_unlock(&fwnode_link_lock);
140}
141
142/**
143 * fwnode_links_purge - Delete all links connected to a fwnode_handle.
144 * @fwnode: fwnode whose links needs to be deleted
145 *
146 * Deletes all links connecting directly to a fwnode.
147 */
148void fwnode_links_purge(struct fwnode_handle *fwnode)
149{
150 fwnode_links_purge_suppliers(fwnode);
151 fwnode_links_purge_consumers(fwnode);
152}
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100153
Saravana Kannan28ec3442021-05-05 17:44:22 -0700154void fw_devlink_purge_absent_suppliers(struct fwnode_handle *fwnode)
Saravana Kannan9528e0d2021-02-05 14:26:37 -0800155{
156 struct fwnode_handle *child;
157
158 /* Don't purge consumer links of an added child */
159 if (fwnode->dev)
160 return;
161
162 fwnode->flags |= FWNODE_FLAG_NOT_DEVICE;
163 fwnode_links_purge_consumers(fwnode);
164
165 fwnode_for_each_available_child_node(fwnode, child)
166 fw_devlink_purge_absent_suppliers(child);
167}
Saravana Kannan28ec3442021-05-05 17:44:22 -0700168EXPORT_SYMBOL_GPL(fw_devlink_purge_absent_suppliers);
Saravana Kannan9528e0d2021-02-05 14:26:37 -0800169
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100170#ifdef CONFIG_SRCU
171static DEFINE_MUTEX(device_links_lock);
172DEFINE_STATIC_SRCU(device_links_srcu);
173
174static inline void device_links_write_lock(void)
175{
176 mutex_lock(&device_links_lock);
177}
178
179static inline void device_links_write_unlock(void)
180{
181 mutex_unlock(&device_links_lock);
182}
183
Jules Irenge68464d72020-02-14 20:47:29 +0000184int device_links_read_lock(void) __acquires(&device_links_srcu)
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100185{
186 return srcu_read_lock(&device_links_srcu);
187}
188
Jules Irengeab7789c2020-02-14 20:47:30 +0000189void device_links_read_unlock(int idx) __releases(&device_links_srcu)
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100190{
191 srcu_read_unlock(&device_links_srcu, idx);
192}
Joel Fernandes (Google)c2fa1e12019-07-16 18:12:25 -0400193
194int device_links_read_lock_held(void)
195{
196 return srcu_read_lock_held(&device_links_srcu);
197}
Rafael J. Wysocki80dd33c2021-05-14 14:10:15 +0200198
199static void device_link_synchronize_removal(void)
200{
201 synchronize_srcu(&device_links_srcu);
202}
Rafael J. Wysocki0c871312021-05-14 14:11:19 +0200203
204static void device_link_remove_from_lists(struct device_link *link)
205{
206 list_del_rcu(&link->s_node);
207 list_del_rcu(&link->c_node);
208}
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100209#else /* !CONFIG_SRCU */
210static DECLARE_RWSEM(device_links_lock);
211
212static inline void device_links_write_lock(void)
213{
214 down_write(&device_links_lock);
215}
216
217static inline void device_links_write_unlock(void)
218{
219 up_write(&device_links_lock);
220}
221
222int device_links_read_lock(void)
223{
224 down_read(&device_links_lock);
225 return 0;
226}
227
228void device_links_read_unlock(int not_used)
229{
230 up_read(&device_links_lock);
231}
Joel Fernandes (Google)c2fa1e12019-07-16 18:12:25 -0400232
233#ifdef CONFIG_DEBUG_LOCK_ALLOC
234int device_links_read_lock_held(void)
235{
236 return lockdep_is_held(&device_links_lock);
237}
238#endif
Rafael J. Wysocki80dd33c2021-05-14 14:10:15 +0200239
240static inline void device_link_synchronize_removal(void)
241{
242}
Rafael J. Wysocki0c871312021-05-14 14:11:19 +0200243
244static void device_link_remove_from_lists(struct device_link *link)
245{
246 list_del(&link->s_node);
247 list_del(&link->c_node);
248}
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100249#endif /* !CONFIG_SRCU */
250
Rafael J. Wysocki3d1cf432021-01-15 19:30:51 +0100251static bool device_is_ancestor(struct device *dev, struct device *target)
252{
253 while (target->parent) {
254 target = target->parent;
255 if (dev == target)
256 return true;
257 }
258 return false;
259}
260
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100261/**
262 * device_is_dependent - Check if one device depends on another one
263 * @dev: Device to check dependencies for.
264 * @target: Device to check against.
265 *
266 * Check if @target depends on @dev or any device dependent on it (its child or
267 * its consumer etc). Return 1 if that is the case or 0 otherwise.
268 */
Saravana Kannan7d34ca32020-06-09 18:19:33 -0700269int device_is_dependent(struct device *dev, void *target)
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100270{
271 struct device_link *link;
272 int ret;
273
Rafael J. Wysocki3d1cf432021-01-15 19:30:51 +0100274 /*
275 * The "ancestors" check is needed to catch the case when the target
276 * device has not been completely initialized yet and it is still
277 * missing from the list of children of its parent device.
278 */
279 if (dev == target || device_is_ancestor(dev, target))
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100280 return 1;
281
282 ret = device_for_each_child(dev, target, device_is_dependent);
283 if (ret)
284 return ret;
285
286 list_for_each_entry(link, &dev->links.consumers, s_node) {
Saravana Kannan4b9bbb22020-12-17 19:17:00 -0800287 if ((link->flags & ~DL_FLAG_INFERRED) ==
288 (DL_FLAG_SYNC_STATE_ONLY | DL_FLAG_MANAGED))
Saravana Kannan05ef9832019-10-28 15:00:22 -0700289 continue;
290
Benjamin Gaignarde16f4f32018-07-16 13:37:44 +0200291 if (link->consumer == target)
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100292 return 1;
293
294 ret = device_is_dependent(link->consumer, target);
295 if (ret)
296 break;
297 }
298 return ret;
299}
300
Rafael J. Wysocki515db262019-07-16 17:21:06 +0200301static void device_link_init_status(struct device_link *link,
302 struct device *consumer,
303 struct device *supplier)
304{
305 switch (supplier->links.status) {
306 case DL_DEV_PROBING:
307 switch (consumer->links.status) {
308 case DL_DEV_PROBING:
309 /*
310 * A consumer driver can create a link to a supplier
311 * that has not completed its probing yet as long as it
312 * knows that the supplier is already functional (for
313 * example, it has just acquired some resources from the
314 * supplier).
315 */
316 link->status = DL_STATE_CONSUMER_PROBE;
317 break;
318 default:
319 link->status = DL_STATE_DORMANT;
320 break;
321 }
322 break;
323 case DL_DEV_DRIVER_BOUND:
324 switch (consumer->links.status) {
325 case DL_DEV_PROBING:
326 link->status = DL_STATE_CONSUMER_PROBE;
327 break;
328 case DL_DEV_DRIVER_BOUND:
329 link->status = DL_STATE_ACTIVE;
330 break;
331 default:
332 link->status = DL_STATE_AVAILABLE;
333 break;
334 }
335 break;
336 case DL_DEV_UNBINDING:
337 link->status = DL_STATE_SUPPLIER_UNBIND;
338 break;
339 default:
340 link->status = DL_STATE_DORMANT;
341 break;
342 }
343}
344
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100345static int device_reorder_to_tail(struct device *dev, void *not_used)
346{
347 struct device_link *link;
348
349 /*
350 * Devices that have not been registered yet will be put to the ends
351 * of the lists during the registration, so skip them here.
352 */
353 if (device_is_registered(dev))
354 devices_kset_move_last(dev);
355
356 if (device_pm_initialized(dev))
357 device_pm_move_last(dev);
358
359 device_for_each_child(dev, NULL, device_reorder_to_tail);
Saravana Kannan05ef9832019-10-28 15:00:22 -0700360 list_for_each_entry(link, &dev->links.consumers, s_node) {
Saravana Kannan4b9bbb22020-12-17 19:17:00 -0800361 if ((link->flags & ~DL_FLAG_INFERRED) ==
362 (DL_FLAG_SYNC_STATE_ONLY | DL_FLAG_MANAGED))
Saravana Kannan05ef9832019-10-28 15:00:22 -0700363 continue;
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100364 device_reorder_to_tail(link->consumer, NULL);
Saravana Kannan05ef9832019-10-28 15:00:22 -0700365 }
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100366
367 return 0;
368}
369
370/**
Feng Kan494fd7b2018-04-10 16:57:06 -0700371 * device_pm_move_to_tail - Move set of devices to the end of device lists
372 * @dev: Device to move
373 *
374 * This is a device_reorder_to_tail() wrapper taking the requisite locks.
375 *
376 * It moves the @dev along with all of its children and all of its consumers
377 * to the ends of the device_kset and dpm_list, recursively.
378 */
379void device_pm_move_to_tail(struct device *dev)
380{
381 int idx;
382
383 idx = device_links_read_lock();
384 device_pm_lock();
385 device_reorder_to_tail(dev, NULL);
386 device_pm_unlock();
387 device_links_read_unlock(idx);
388}
389
Saravana Kannan287905e2020-05-21 12:17:58 -0700390#define to_devlink(dev) container_of((dev), struct device_link, link_dev)
391
392static ssize_t status_show(struct device *dev,
Joe Perches948b3ed2020-09-16 13:40:42 -0700393 struct device_attribute *attr, char *buf)
Saravana Kannan287905e2020-05-21 12:17:58 -0700394{
Joe Perches948b3ed2020-09-16 13:40:42 -0700395 const char *output;
Saravana Kannan287905e2020-05-21 12:17:58 -0700396
397 switch (to_devlink(dev)->status) {
398 case DL_STATE_NONE:
Joe Perches948b3ed2020-09-16 13:40:42 -0700399 output = "not tracked";
400 break;
Saravana Kannan287905e2020-05-21 12:17:58 -0700401 case DL_STATE_DORMANT:
Joe Perches948b3ed2020-09-16 13:40:42 -0700402 output = "dormant";
403 break;
Saravana Kannan287905e2020-05-21 12:17:58 -0700404 case DL_STATE_AVAILABLE:
Joe Perches948b3ed2020-09-16 13:40:42 -0700405 output = "available";
406 break;
Saravana Kannan287905e2020-05-21 12:17:58 -0700407 case DL_STATE_CONSUMER_PROBE:
Joe Perches948b3ed2020-09-16 13:40:42 -0700408 output = "consumer probing";
409 break;
Saravana Kannan287905e2020-05-21 12:17:58 -0700410 case DL_STATE_ACTIVE:
Joe Perches948b3ed2020-09-16 13:40:42 -0700411 output = "active";
412 break;
Saravana Kannan287905e2020-05-21 12:17:58 -0700413 case DL_STATE_SUPPLIER_UNBIND:
Joe Perches948b3ed2020-09-16 13:40:42 -0700414 output = "supplier unbinding";
415 break;
Saravana Kannan287905e2020-05-21 12:17:58 -0700416 default:
Joe Perches948b3ed2020-09-16 13:40:42 -0700417 output = "unknown";
418 break;
Saravana Kannan287905e2020-05-21 12:17:58 -0700419 }
Joe Perches948b3ed2020-09-16 13:40:42 -0700420
421 return sysfs_emit(buf, "%s\n", output);
Saravana Kannan287905e2020-05-21 12:17:58 -0700422}
423static DEVICE_ATTR_RO(status);
424
425static ssize_t auto_remove_on_show(struct device *dev,
426 struct device_attribute *attr, char *buf)
427{
428 struct device_link *link = to_devlink(dev);
Joe Perches973c3912020-09-16 13:40:40 -0700429 const char *output;
Saravana Kannan287905e2020-05-21 12:17:58 -0700430
431 if (link->flags & DL_FLAG_AUTOREMOVE_SUPPLIER)
Joe Perches973c3912020-09-16 13:40:40 -0700432 output = "supplier unbind";
Saravana Kannan287905e2020-05-21 12:17:58 -0700433 else if (link->flags & DL_FLAG_AUTOREMOVE_CONSUMER)
Joe Perches973c3912020-09-16 13:40:40 -0700434 output = "consumer unbind";
Saravana Kannan287905e2020-05-21 12:17:58 -0700435 else
Joe Perches973c3912020-09-16 13:40:40 -0700436 output = "never";
Saravana Kannan287905e2020-05-21 12:17:58 -0700437
Joe Perches973c3912020-09-16 13:40:40 -0700438 return sysfs_emit(buf, "%s\n", output);
Saravana Kannan287905e2020-05-21 12:17:58 -0700439}
440static DEVICE_ATTR_RO(auto_remove_on);
441
442static ssize_t runtime_pm_show(struct device *dev,
443 struct device_attribute *attr, char *buf)
444{
445 struct device_link *link = to_devlink(dev);
446
Joe Perchesaa838892020-09-16 13:40:39 -0700447 return sysfs_emit(buf, "%d\n", !!(link->flags & DL_FLAG_PM_RUNTIME));
Saravana Kannan287905e2020-05-21 12:17:58 -0700448}
449static DEVICE_ATTR_RO(runtime_pm);
450
451static ssize_t sync_state_only_show(struct device *dev,
452 struct device_attribute *attr, char *buf)
453{
454 struct device_link *link = to_devlink(dev);
455
Joe Perchesaa838892020-09-16 13:40:39 -0700456 return sysfs_emit(buf, "%d\n",
457 !!(link->flags & DL_FLAG_SYNC_STATE_ONLY));
Saravana Kannan287905e2020-05-21 12:17:58 -0700458}
459static DEVICE_ATTR_RO(sync_state_only);
460
461static struct attribute *devlink_attrs[] = {
462 &dev_attr_status.attr,
463 &dev_attr_auto_remove_on.attr,
464 &dev_attr_runtime_pm.attr,
465 &dev_attr_sync_state_only.attr,
466 NULL,
467};
468ATTRIBUTE_GROUPS(devlink);
469
Rafael J. Wysocki80dd33c2021-05-14 14:10:15 +0200470static void device_link_release_fn(struct work_struct *work)
Saravana Kannan843e6002020-07-16 14:45:23 -0700471{
Rafael J. Wysocki80dd33c2021-05-14 14:10:15 +0200472 struct device_link *link = container_of(work, struct device_link, rm_work);
473
474 /* Ensure that all references to the link object have been dropped. */
475 device_link_synchronize_removal();
476
Saravana Kannan843e6002020-07-16 14:45:23 -0700477 while (refcount_dec_not_one(&link->rpm_active))
478 pm_runtime_put(link->supplier);
479
480 put_device(link->consumer);
481 put_device(link->supplier);
482 kfree(link);
483}
484
Saravana Kannan287905e2020-05-21 12:17:58 -0700485static void devlink_dev_release(struct device *dev)
486{
Saravana Kannan843e6002020-07-16 14:45:23 -0700487 struct device_link *link = to_devlink(dev);
488
Rafael J. Wysocki80dd33c2021-05-14 14:10:15 +0200489 INIT_WORK(&link->rm_work, device_link_release_fn);
490 /*
491 * It may take a while to complete this work because of the SRCU
492 * synchronization in device_link_release_fn() and if the consumer or
493 * supplier devices get deleted when it runs, so put it into the "long"
494 * workqueue.
495 */
496 queue_work(system_long_wq, &link->rm_work);
Saravana Kannan287905e2020-05-21 12:17:58 -0700497}
498
499static struct class devlink_class = {
500 .name = "devlink",
501 .owner = THIS_MODULE,
502 .dev_groups = devlink_groups,
503 .dev_release = devlink_dev_release,
504};
505
506static int devlink_add_symlinks(struct device *dev,
507 struct class_interface *class_intf)
508{
509 int ret;
510 size_t len;
511 struct device_link *link = to_devlink(dev);
512 struct device *sup = link->supplier;
513 struct device *con = link->consumer;
514 char *buf;
515
Saravana Kannane020ff62021-01-10 09:54:07 -0800516 len = max(strlen(dev_bus_name(sup)) + strlen(dev_name(sup)),
517 strlen(dev_bus_name(con)) + strlen(dev_name(con)));
518 len += strlen(":");
Saravana Kannan287905e2020-05-21 12:17:58 -0700519 len += strlen("supplier:") + 1;
520 buf = kzalloc(len, GFP_KERNEL);
521 if (!buf)
522 return -ENOMEM;
523
524 ret = sysfs_create_link(&link->link_dev.kobj, &sup->kobj, "supplier");
525 if (ret)
526 goto out;
527
528 ret = sysfs_create_link(&link->link_dev.kobj, &con->kobj, "consumer");
529 if (ret)
530 goto err_con;
531
Saravana Kannane020ff62021-01-10 09:54:07 -0800532 snprintf(buf, len, "consumer:%s:%s", dev_bus_name(con), dev_name(con));
Saravana Kannan287905e2020-05-21 12:17:58 -0700533 ret = sysfs_create_link(&sup->kobj, &link->link_dev.kobj, buf);
534 if (ret)
535 goto err_con_dev;
536
Saravana Kannane020ff62021-01-10 09:54:07 -0800537 snprintf(buf, len, "supplier:%s:%s", dev_bus_name(sup), dev_name(sup));
Saravana Kannan287905e2020-05-21 12:17:58 -0700538 ret = sysfs_create_link(&con->kobj, &link->link_dev.kobj, buf);
539 if (ret)
540 goto err_sup_dev;
541
542 goto out;
543
544err_sup_dev:
Saravana Kannane020ff62021-01-10 09:54:07 -0800545 snprintf(buf, len, "consumer:%s:%s", dev_bus_name(con), dev_name(con));
Saravana Kannan287905e2020-05-21 12:17:58 -0700546 sysfs_remove_link(&sup->kobj, buf);
547err_con_dev:
548 sysfs_remove_link(&link->link_dev.kobj, "consumer");
549err_con:
550 sysfs_remove_link(&link->link_dev.kobj, "supplier");
551out:
552 kfree(buf);
553 return ret;
554}
555
556static void devlink_remove_symlinks(struct device *dev,
557 struct class_interface *class_intf)
558{
559 struct device_link *link = to_devlink(dev);
560 size_t len;
561 struct device *sup = link->supplier;
562 struct device *con = link->consumer;
563 char *buf;
564
565 sysfs_remove_link(&link->link_dev.kobj, "consumer");
566 sysfs_remove_link(&link->link_dev.kobj, "supplier");
567
Saravana Kannane020ff62021-01-10 09:54:07 -0800568 len = max(strlen(dev_bus_name(sup)) + strlen(dev_name(sup)),
569 strlen(dev_bus_name(con)) + strlen(dev_name(con)));
570 len += strlen(":");
Saravana Kannan287905e2020-05-21 12:17:58 -0700571 len += strlen("supplier:") + 1;
572 buf = kzalloc(len, GFP_KERNEL);
573 if (!buf) {
574 WARN(1, "Unable to properly free device link symlinks!\n");
575 return;
576 }
577
Adrian Huntere64daad2021-07-16 14:44:07 +0300578 if (device_is_registered(con)) {
579 snprintf(buf, len, "supplier:%s:%s", dev_bus_name(sup), dev_name(sup));
580 sysfs_remove_link(&con->kobj, buf);
581 }
Saravana Kannane020ff62021-01-10 09:54:07 -0800582 snprintf(buf, len, "consumer:%s:%s", dev_bus_name(con), dev_name(con));
Saravana Kannan287905e2020-05-21 12:17:58 -0700583 sysfs_remove_link(&sup->kobj, buf);
584 kfree(buf);
585}
586
587static struct class_interface devlink_class_intf = {
588 .class = &devlink_class,
589 .add_dev = devlink_add_symlinks,
590 .remove_dev = devlink_remove_symlinks,
591};
592
593static int __init devlink_class_init(void)
594{
595 int ret;
596
597 ret = class_register(&devlink_class);
598 if (ret)
599 return ret;
600
601 ret = class_interface_register(&devlink_class_intf);
602 if (ret)
603 class_unregister(&devlink_class);
604
605 return ret;
606}
607postcore_initcall(devlink_class_init);
608
Rafael J. Wysocki515db262019-07-16 17:21:06 +0200609#define DL_MANAGED_LINK_FLAGS (DL_FLAG_AUTOREMOVE_CONSUMER | \
610 DL_FLAG_AUTOREMOVE_SUPPLIER | \
Saravana Kannan05ef9832019-10-28 15:00:22 -0700611 DL_FLAG_AUTOPROBE_CONSUMER | \
Saravana Kannan4b9bbb22020-12-17 19:17:00 -0800612 DL_FLAG_SYNC_STATE_ONLY | \
613 DL_FLAG_INFERRED)
Rafael J. Wysocki515db262019-07-16 17:21:06 +0200614
Rafael J. Wysockifb583c82019-07-30 11:28:57 +0200615#define DL_ADD_VALID_FLAGS (DL_MANAGED_LINK_FLAGS | DL_FLAG_STATELESS | \
616 DL_FLAG_PM_RUNTIME | DL_FLAG_RPM_ACTIVE)
617
Feng Kan494fd7b2018-04-10 16:57:06 -0700618/**
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100619 * device_link_add - Create a link between two devices.
620 * @consumer: Consumer end of the link.
621 * @supplier: Supplier end of the link.
622 * @flags: Link flags.
623 *
Rafael J. Wysocki21d5c572016-10-30 17:32:31 +0100624 * The caller is responsible for the proper synchronization of the link creation
625 * with runtime PM. First, setting the DL_FLAG_PM_RUNTIME flag will cause the
626 * runtime PM framework to take the link into account. Second, if the
627 * DL_FLAG_RPM_ACTIVE flag is set in addition to it, the supplier devices will
Thierry Redingd475f8e2020-11-27 11:46:30 +0100628 * be forced into the active meta state and reference-counted upon the creation
Rafael J. Wysocki21d5c572016-10-30 17:32:31 +0100629 * of the link. If DL_FLAG_PM_RUNTIME is not set, DL_FLAG_RPM_ACTIVE will be
630 * ignored.
631 *
Rafael J. Wysocki515db262019-07-16 17:21:06 +0200632 * If DL_FLAG_STATELESS is set in @flags, the caller of this function is
633 * expected to release the link returned by it directly with the help of either
634 * device_link_del() or device_link_remove().
Rafael J. Wysocki72175d42019-02-01 01:58:33 +0100635 *
636 * If that flag is not set, however, the caller of this function is handing the
637 * management of the link over to the driver core entirely and its return value
638 * can only be used to check whether or not the link is present. In that case,
639 * the DL_FLAG_AUTOREMOVE_CONSUMER and DL_FLAG_AUTOREMOVE_SUPPLIER device link
640 * flags can be used to indicate to the driver core when the link can be safely
641 * deleted. Namely, setting one of them in @flags indicates to the driver core
642 * that the link is not going to be used (by the given caller of this function)
643 * after unbinding the consumer or supplier driver, respectively, from its
644 * device, so the link can be deleted at that point. If none of them is set,
645 * the link will be maintained until one of the devices pointed to by it (either
646 * the consumer or the supplier) is unregistered.
Rafael J. Wysockic8d50982019-02-01 01:45:55 +0100647 *
Rafael J. Wysockie7dd4012019-02-01 01:59:42 +0100648 * Also, if DL_FLAG_STATELESS, DL_FLAG_AUTOREMOVE_CONSUMER and
649 * DL_FLAG_AUTOREMOVE_SUPPLIER are not set in @flags (that is, a persistent
650 * managed device link is being added), the DL_FLAG_AUTOPROBE_CONSUMER flag can
Thierry Redingd475f8e2020-11-27 11:46:30 +0100651 * be used to request the driver core to automatically probe for a consumer
Rafael J. Wysockie7dd4012019-02-01 01:59:42 +0100652 * driver after successfully binding a driver to the supplier device.
653 *
Rafael J. Wysocki515db262019-07-16 17:21:06 +0200654 * The combination of DL_FLAG_STATELESS and one of DL_FLAG_AUTOREMOVE_CONSUMER,
655 * DL_FLAG_AUTOREMOVE_SUPPLIER, or DL_FLAG_AUTOPROBE_CONSUMER set in @flags at
656 * the same time is invalid and will cause NULL to be returned upfront.
657 * However, if a device link between the given @consumer and @supplier pair
658 * exists already when this function is called for them, the existing link will
659 * be returned regardless of its current type and status (the link's flags may
660 * be modified then). The caller of this function is then expected to treat
661 * the link as though it has just been created, so (in particular) if
662 * DL_FLAG_STATELESS was passed in @flags, the link needs to be released
663 * explicitly when not needed any more (as stated above).
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100664 *
665 * A side effect of the link creation is re-ordering of dpm_list and the
666 * devices_kset list by moving the consumer device and all devices depending
667 * on it to the ends of these lists (that does not happen to devices that have
668 * not been registered when this function is called).
669 *
670 * The supplier device is required to be registered when this function is called
671 * and NULL will be returned if that is not the case. The consumer device need
Lukas Wunner64df1142016-12-04 13:10:04 +0100672 * not be registered, however.
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100673 */
674struct device_link *device_link_add(struct device *consumer,
675 struct device *supplier, u32 flags)
676{
677 struct device_link *link;
678
Rafael J. Wysockifb583c82019-07-30 11:28:57 +0200679 if (!consumer || !supplier || flags & ~DL_ADD_VALID_FLAGS ||
Rafael J. Wysocki515db262019-07-16 17:21:06 +0200680 (flags & DL_FLAG_STATELESS && flags & DL_MANAGED_LINK_FLAGS) ||
Saravana Kannan05ef9832019-10-28 15:00:22 -0700681 (flags & DL_FLAG_SYNC_STATE_ONLY &&
Saravana Kannan4b9bbb22020-12-17 19:17:00 -0800682 (flags & ~DL_FLAG_INFERRED) != DL_FLAG_SYNC_STATE_ONLY) ||
Rafael J. Wysockie7dd4012019-02-01 01:59:42 +0100683 (flags & DL_FLAG_AUTOPROBE_CONSUMER &&
684 flags & (DL_FLAG_AUTOREMOVE_CONSUMER |
685 DL_FLAG_AUTOREMOVE_SUPPLIER)))
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100686 return NULL;
687
Rafael J. Wysocki5db25c92019-02-01 01:47:53 +0100688 if (flags & DL_FLAG_PM_RUNTIME && flags & DL_FLAG_RPM_ACTIVE) {
689 if (pm_runtime_get_sync(supplier) < 0) {
690 pm_runtime_put_noidle(supplier);
691 return NULL;
692 }
Rafael J. Wysocki5db25c92019-02-01 01:47:53 +0100693 }
694
Rafael J. Wysocki515db262019-07-16 17:21:06 +0200695 if (!(flags & DL_FLAG_STATELESS))
696 flags |= DL_FLAG_MANAGED;
697
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100698 device_links_write_lock();
699 device_pm_lock();
700
701 /*
702 * If the supplier has not been fully registered yet or there is a
Saravana Kannan05ef9832019-10-28 15:00:22 -0700703 * reverse (non-SYNC_STATE_ONLY) dependency between the consumer and
704 * the supplier already in the graph, return NULL. If the link is a
705 * SYNC_STATE_ONLY link, we don't check for reverse dependencies
706 * because it only affects sync_state() callbacks.
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100707 */
708 if (!device_pm_initialized(supplier)
Saravana Kannan05ef9832019-10-28 15:00:22 -0700709 || (!(flags & DL_FLAG_SYNC_STATE_ONLY) &&
710 device_is_dependent(consumer, supplier))) {
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100711 link = NULL;
712 goto out;
713 }
714
Rafael J. Wysocki72175d42019-02-01 01:58:33 +0100715 /*
Saravana Kannanac66c5b2020-11-20 18:02:24 -0800716 * SYNC_STATE_ONLY links are useless once a consumer device has probed.
717 * So, only create it if the consumer hasn't probed yet.
718 */
719 if (flags & DL_FLAG_SYNC_STATE_ONLY &&
720 consumer->links.status != DL_DEV_NO_DRIVER &&
721 consumer->links.status != DL_DEV_PROBING) {
722 link = NULL;
723 goto out;
724 }
725
726 /*
Rafael J. Wysocki72175d42019-02-01 01:58:33 +0100727 * DL_FLAG_AUTOREMOVE_SUPPLIER indicates that the link will be needed
728 * longer than for DL_FLAG_AUTOREMOVE_CONSUMER and setting them both
729 * together doesn't make sense, so prefer DL_FLAG_AUTOREMOVE_SUPPLIER.
730 */
731 if (flags & DL_FLAG_AUTOREMOVE_SUPPLIER)
732 flags &= ~DL_FLAG_AUTOREMOVE_CONSUMER;
733
Rafael J. Wysockif265df52019-02-01 01:46:54 +0100734 list_for_each_entry(link, &supplier->links.consumers, s_node) {
735 if (link->consumer != consumer)
736 continue;
737
Saravana Kannan4b9bbb22020-12-17 19:17:00 -0800738 if (link->flags & DL_FLAG_INFERRED &&
739 !(flags & DL_FLAG_INFERRED))
740 link->flags &= ~DL_FLAG_INFERRED;
741
Rafael J. Wysockie2f3cd82019-02-01 01:49:14 +0100742 if (flags & DL_FLAG_PM_RUNTIME) {
743 if (!(link->flags & DL_FLAG_PM_RUNTIME)) {
Rafael J. Wysocki4c06c4e2019-02-12 13:08:10 +0100744 pm_runtime_new_link(consumer);
Rafael J. Wysockie2f3cd82019-02-01 01:49:14 +0100745 link->flags |= DL_FLAG_PM_RUNTIME;
746 }
747 if (flags & DL_FLAG_RPM_ACTIVE)
Rafael J. Wysocki36003d42019-02-19 17:53:26 +0100748 refcount_inc(&link->rpm_active);
Rafael J. Wysockie2f3cd82019-02-01 01:49:14 +0100749 }
750
Rafael J. Wysocki72175d42019-02-01 01:58:33 +0100751 if (flags & DL_FLAG_STATELESS) {
752 kref_get(&link->kref);
Saravana Kannan05ef9832019-10-28 15:00:22 -0700753 if (link->flags & DL_FLAG_SYNC_STATE_ONLY &&
Saravana Kannan44e96042020-05-19 21:36:26 -0700754 !(link->flags & DL_FLAG_STATELESS)) {
755 link->flags |= DL_FLAG_STATELESS;
Saravana Kannan05ef9832019-10-28 15:00:22 -0700756 goto reorder;
Saravana Kannan44e96042020-05-19 21:36:26 -0700757 } else {
758 link->flags |= DL_FLAG_STATELESS;
Saravana Kannan05ef9832019-10-28 15:00:22 -0700759 goto out;
Saravana Kannan44e96042020-05-19 21:36:26 -0700760 }
Rafael J. Wysocki72175d42019-02-01 01:58:33 +0100761 }
762
763 /*
764 * If the life time of the link following from the new flags is
765 * longer than indicated by the flags of the existing link,
766 * update the existing link to stay around longer.
767 */
768 if (flags & DL_FLAG_AUTOREMOVE_SUPPLIER) {
769 if (link->flags & DL_FLAG_AUTOREMOVE_CONSUMER) {
770 link->flags &= ~DL_FLAG_AUTOREMOVE_CONSUMER;
771 link->flags |= DL_FLAG_AUTOREMOVE_SUPPLIER;
772 }
773 } else if (!(flags & DL_FLAG_AUTOREMOVE_CONSUMER)) {
774 link->flags &= ~(DL_FLAG_AUTOREMOVE_CONSUMER |
775 DL_FLAG_AUTOREMOVE_SUPPLIER);
776 }
Rafael J. Wysocki515db262019-07-16 17:21:06 +0200777 if (!(link->flags & DL_FLAG_MANAGED)) {
778 kref_get(&link->kref);
779 link->flags |= DL_FLAG_MANAGED;
780 device_link_init_status(link, consumer, supplier);
781 }
Saravana Kannan05ef9832019-10-28 15:00:22 -0700782 if (link->flags & DL_FLAG_SYNC_STATE_ONLY &&
783 !(flags & DL_FLAG_SYNC_STATE_ONLY)) {
784 link->flags &= ~DL_FLAG_SYNC_STATE_ONLY;
785 goto reorder;
786 }
787
Rafael J. Wysockif265df52019-02-01 01:46:54 +0100788 goto out;
789 }
790
Rafael J. Wysocki21d5c572016-10-30 17:32:31 +0100791 link = kzalloc(sizeof(*link), GFP_KERNEL);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100792 if (!link)
793 goto out;
794
Rafael J. Wysockie2f3cd82019-02-01 01:49:14 +0100795 refcount_set(&link->rpm_active, 1);
796
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100797 get_device(supplier);
798 link->supplier = supplier;
799 INIT_LIST_HEAD(&link->s_node);
800 get_device(consumer);
801 link->consumer = consumer;
802 INIT_LIST_HEAD(&link->c_node);
803 link->flags = flags;
Lukas Wunneread18c22018-02-10 19:27:12 +0100804 kref_init(&link->kref);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100805
Saravana Kannan287905e2020-05-21 12:17:58 -0700806 link->link_dev.class = &devlink_class;
807 device_set_pm_not_required(&link->link_dev);
Saravana Kannane020ff62021-01-10 09:54:07 -0800808 dev_set_name(&link->link_dev, "%s:%s--%s:%s",
809 dev_bus_name(supplier), dev_name(supplier),
810 dev_bus_name(consumer), dev_name(consumer));
Saravana Kannan287905e2020-05-21 12:17:58 -0700811 if (device_register(&link->link_dev)) {
812 put_device(consumer);
813 put_device(supplier);
814 kfree(link);
815 link = NULL;
816 goto out;
817 }
818
819 if (flags & DL_FLAG_PM_RUNTIME) {
820 if (flags & DL_FLAG_RPM_ACTIVE)
821 refcount_inc(&link->rpm_active);
822
823 pm_runtime_new_link(consumer);
824 }
825
Lukas Wunner64df1142016-12-04 13:10:04 +0100826 /* Determine the initial link state. */
Rafael J. Wysocki515db262019-07-16 17:21:06 +0200827 if (flags & DL_FLAG_STATELESS)
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100828 link->status = DL_STATE_NONE;
Rafael J. Wysocki515db262019-07-16 17:21:06 +0200829 else
830 device_link_init_status(link, consumer, supplier);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100831
832 /*
Rafael J. Wysocki15cfb092019-02-01 01:50:39 +0100833 * Some callers expect the link creation during consumer driver probe to
834 * resume the supplier even without DL_FLAG_RPM_ACTIVE.
835 */
836 if (link->status == DL_STATE_CONSUMER_PROBE &&
837 flags & DL_FLAG_PM_RUNTIME)
838 pm_runtime_resume(supplier);
839
Saravana Kannan21c27f02020-05-18 23:30:00 -0700840 list_add_tail_rcu(&link->s_node, &supplier->links.consumers);
841 list_add_tail_rcu(&link->c_node, &consumer->links.suppliers);
842
Saravana Kannan05ef9832019-10-28 15:00:22 -0700843 if (flags & DL_FLAG_SYNC_STATE_ONLY) {
844 dev_dbg(consumer,
845 "Linked as a sync state only consumer to %s\n",
846 dev_name(supplier));
847 goto out;
848 }
Saravana Kannan21c27f02020-05-18 23:30:00 -0700849
Saravana Kannan05ef9832019-10-28 15:00:22 -0700850reorder:
Rafael J. Wysocki15cfb092019-02-01 01:50:39 +0100851 /*
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100852 * Move the consumer and all of the devices depending on it to the end
853 * of dpm_list and the devices_kset list.
854 *
855 * It is necessary to hold dpm_list locked throughout all that or else
856 * we may end up suspending with a wrong ordering of it.
857 */
858 device_reorder_to_tail(consumer, NULL);
859
Jerome Brunet8a4b3262018-12-21 17:23:41 +0100860 dev_dbg(consumer, "Linked as a consumer to %s\n", dev_name(supplier));
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100861
Saravana Kannan21c27f02020-05-18 23:30:00 -0700862out:
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100863 device_pm_unlock();
864 device_links_write_unlock();
Rafael J. Wysocki5db25c92019-02-01 01:47:53 +0100865
Rafael J. Wysockie2f3cd82019-02-01 01:49:14 +0100866 if ((flags & DL_FLAG_PM_RUNTIME && flags & DL_FLAG_RPM_ACTIVE) && !link)
Rafael J. Wysocki5db25c92019-02-01 01:47:53 +0100867 pm_runtime_put(supplier);
868
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100869 return link;
870}
871EXPORT_SYMBOL_GPL(device_link_add);
872
Lukas Wunneread18c22018-02-10 19:27:12 +0100873static void __device_link_del(struct kref *kref)
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100874{
Lukas Wunneread18c22018-02-10 19:27:12 +0100875 struct device_link *link = container_of(kref, struct device_link, kref);
876
Jerome Brunet8a4b3262018-12-21 17:23:41 +0100877 dev_dbg(link->consumer, "Dropping the link to %s\n",
878 dev_name(link->supplier));
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100879
Rafael J. Wysockie0e398e2020-10-21 21:12:15 +0200880 pm_runtime_drop_link(link);
Rafael J. Wysockibaa88092016-10-30 17:32:43 +0100881
Rafael J. Wysocki0c871312021-05-14 14:11:19 +0200882 device_link_remove_from_lists(link);
Saravana Kannan843e6002020-07-16 14:45:23 -0700883 device_unregister(&link->link_dev);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100884}
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100885
Rafael J. Wysocki72175d42019-02-01 01:58:33 +0100886static void device_link_put_kref(struct device_link *link)
887{
888 if (link->flags & DL_FLAG_STATELESS)
889 kref_put(&link->kref, __device_link_del);
Adrian Hunterbf259672021-08-06 16:04:41 +0300890 else if (!device_is_registered(link->consumer))
891 __device_link_del(&link->kref);
Rafael J. Wysocki72175d42019-02-01 01:58:33 +0100892 else
893 WARN(1, "Unable to drop a managed device link reference\n");
894}
895
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100896/**
Rafael J. Wysocki72175d42019-02-01 01:58:33 +0100897 * device_link_del - Delete a stateless link between two devices.
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100898 * @link: Device link to delete.
899 *
900 * The caller must ensure proper synchronization of this function with runtime
Lukas Wunneread18c22018-02-10 19:27:12 +0100901 * PM. If the link was added multiple times, it needs to be deleted as often.
902 * Care is required for hotplugged devices: Their links are purged on removal
903 * and calling device_link_del() is then no longer allowed.
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100904 */
905void device_link_del(struct device_link *link)
906{
907 device_links_write_lock();
Rafael J. Wysocki72175d42019-02-01 01:58:33 +0100908 device_link_put_kref(link);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100909 device_links_write_unlock();
910}
911EXPORT_SYMBOL_GPL(device_link_del);
912
pascal pailletd8842212018-07-05 14:25:56 +0000913/**
Rafael J. Wysocki72175d42019-02-01 01:58:33 +0100914 * device_link_remove - Delete a stateless link between two devices.
pascal pailletd8842212018-07-05 14:25:56 +0000915 * @consumer: Consumer end of the link.
916 * @supplier: Supplier end of the link.
917 *
918 * The caller must ensure proper synchronization of this function with runtime
919 * PM.
920 */
921void device_link_remove(void *consumer, struct device *supplier)
922{
923 struct device_link *link;
924
925 if (WARN_ON(consumer == supplier))
926 return;
927
928 device_links_write_lock();
pascal pailletd8842212018-07-05 14:25:56 +0000929
930 list_for_each_entry(link, &supplier->links.consumers, s_node) {
931 if (link->consumer == consumer) {
Rafael J. Wysocki72175d42019-02-01 01:58:33 +0100932 device_link_put_kref(link);
pascal pailletd8842212018-07-05 14:25:56 +0000933 break;
934 }
935 }
936
pascal pailletd8842212018-07-05 14:25:56 +0000937 device_links_write_unlock();
938}
939EXPORT_SYMBOL_GPL(device_link_remove);
940
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100941static void device_links_missing_supplier(struct device *dev)
942{
943 struct device_link *link;
944
Saravana Kannan8c3e3152020-05-26 15:09:27 -0700945 list_for_each_entry(link, &dev->links.suppliers, c_node) {
946 if (link->status != DL_STATE_CONSUMER_PROBE)
947 continue;
948
949 if (link->supplier->links.status == DL_DEV_DRIVER_BOUND) {
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100950 WRITE_ONCE(link->status, DL_STATE_AVAILABLE);
Saravana Kannan8c3e3152020-05-26 15:09:27 -0700951 } else {
952 WARN_ON(!(link->flags & DL_FLAG_SYNC_STATE_ONLY));
953 WRITE_ONCE(link->status, DL_STATE_DORMANT);
954 }
955 }
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100956}
957
958/**
959 * device_links_check_suppliers - Check presence of supplier drivers.
960 * @dev: Consumer device.
961 *
962 * Check links from this device to any suppliers. Walk the list of the device's
963 * links to suppliers and see if all of them are available. If not, simply
964 * return -EPROBE_DEFER.
965 *
966 * We need to guarantee that the supplier will not go away after the check has
967 * been positive here. It only can go away in __device_release_driver() and
968 * that function checks the device's links to consumers. This means we need to
969 * mark the link as "consumer probe in progress" to make the supplier removal
970 * wait for us to complete (or bad things may happen).
971 *
Rafael J. Wysocki515db262019-07-16 17:21:06 +0200972 * Links without the DL_FLAG_MANAGED flag set are ignored.
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100973 */
974int device_links_check_suppliers(struct device *dev)
975{
976 struct device_link *link;
977 int ret = 0;
978
Saravana Kannane2ae9bc2019-09-04 14:11:21 -0700979 /*
980 * Device waiting for supplier to become available is not allowed to
981 * probe.
982 */
Saravana Kannan25ac86c2020-11-20 18:02:28 -0800983 mutex_lock(&fwnode_link_lock);
984 if (dev->fwnode && !list_empty(&dev->fwnode->suppliers) &&
985 !fw_devlink_is_permissive()) {
Saravana Kannan1f0dfa02020-12-17 19:16:59 -0800986 dev_dbg(dev, "probe deferral - wait for supplier %pfwP\n",
987 list_first_entry(&dev->fwnode->suppliers,
988 struct fwnode_link,
989 c_hook)->supplier);
Saravana Kannan25ac86c2020-11-20 18:02:28 -0800990 mutex_unlock(&fwnode_link_lock);
Saravana Kannane2ae9bc2019-09-04 14:11:21 -0700991 return -EPROBE_DEFER;
992 }
Saravana Kannan25ac86c2020-11-20 18:02:28 -0800993 mutex_unlock(&fwnode_link_lock);
Saravana Kannane2ae9bc2019-09-04 14:11:21 -0700994
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100995 device_links_write_lock();
996
997 list_for_each_entry(link, &dev->links.suppliers, c_node) {
Saravana Kannan8c3e3152020-05-26 15:09:27 -0700998 if (!(link->flags & DL_FLAG_MANAGED))
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100999 continue;
1000
Saravana Kannan8c3e3152020-05-26 15:09:27 -07001001 if (link->status != DL_STATE_AVAILABLE &&
1002 !(link->flags & DL_FLAG_SYNC_STATE_ONLY)) {
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001003 device_links_missing_supplier(dev);
Saravana Kannan1f0dfa02020-12-17 19:16:59 -08001004 dev_dbg(dev, "probe deferral - supplier %s not ready\n",
1005 dev_name(link->supplier));
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001006 ret = -EPROBE_DEFER;
1007 break;
1008 }
1009 WRITE_ONCE(link->status, DL_STATE_CONSUMER_PROBE);
1010 }
1011 dev->links.status = DL_DEV_PROBING;
1012
1013 device_links_write_unlock();
1014 return ret;
1015}
1016
Saravana Kannan26e77702019-11-14 14:56:45 -08001017/**
1018 * __device_links_queue_sync_state - Queue a device for sync_state() callback
1019 * @dev: Device to call sync_state() on
1020 * @list: List head to queue the @dev on
1021 *
1022 * Queues a device for a sync_state() callback when the device links write lock
1023 * isn't held. This allows the sync_state() execution flow to use device links
1024 * APIs. The caller must ensure this function is called with
1025 * device_links_write_lock() held.
1026 *
1027 * This function does a get_device() to make sure the device is not freed while
1028 * on this list.
1029 *
1030 * So the caller must also ensure that device_links_flush_sync_list() is called
1031 * as soon as the caller releases device_links_write_lock(). This is necessary
1032 * to make sure the sync_state() is called in a timely fashion and the
1033 * put_device() is called on this device.
1034 */
1035static void __device_links_queue_sync_state(struct device *dev,
1036 struct list_head *list)
Saravana Kannanfc5a2512019-09-04 14:11:23 -07001037{
1038 struct device_link *link;
1039
Saravana Kannan77036162020-02-21 00:05:10 -08001040 if (!dev_has_sync_state(dev))
1041 return;
Saravana Kannanfc5a2512019-09-04 14:11:23 -07001042 if (dev->state_synced)
1043 return;
1044
1045 list_for_each_entry(link, &dev->links.consumers, s_node) {
1046 if (!(link->flags & DL_FLAG_MANAGED))
1047 continue;
1048 if (link->status != DL_STATE_ACTIVE)
1049 return;
1050 }
1051
Saravana Kannan26e77702019-11-14 14:56:45 -08001052 /*
1053 * Set the flag here to avoid adding the same device to a list more
1054 * than once. This can happen if new consumers get added to the device
1055 * and probed before the list is flushed.
1056 */
Saravana Kannanfc5a2512019-09-04 14:11:23 -07001057 dev->state_synced = true;
Saravana Kannan26e77702019-11-14 14:56:45 -08001058
Saravana Kannan3b052a32020-11-20 18:02:17 -08001059 if (WARN_ON(!list_empty(&dev->links.defer_sync)))
Saravana Kannan26e77702019-11-14 14:56:45 -08001060 return;
1061
1062 get_device(dev);
Saravana Kannan3b052a32020-11-20 18:02:17 -08001063 list_add_tail(&dev->links.defer_sync, list);
Saravana Kannan26e77702019-11-14 14:56:45 -08001064}
1065
1066/**
1067 * device_links_flush_sync_list - Call sync_state() on a list of devices
1068 * @list: List of devices to call sync_state() on
Saravana Kannan21eb93f2020-02-21 00:05:08 -08001069 * @dont_lock_dev: Device for which lock is already held by the caller
Saravana Kannan26e77702019-11-14 14:56:45 -08001070 *
1071 * Calls sync_state() on all the devices that have been queued for it. This
Saravana Kannan21eb93f2020-02-21 00:05:08 -08001072 * function is used in conjunction with __device_links_queue_sync_state(). The
1073 * @dont_lock_dev parameter is useful when this function is called from a
1074 * context where a device lock is already held.
Saravana Kannan26e77702019-11-14 14:56:45 -08001075 */
Saravana Kannan21eb93f2020-02-21 00:05:08 -08001076static void device_links_flush_sync_list(struct list_head *list,
1077 struct device *dont_lock_dev)
Saravana Kannan26e77702019-11-14 14:56:45 -08001078{
1079 struct device *dev, *tmp;
1080
Saravana Kannan3b052a32020-11-20 18:02:17 -08001081 list_for_each_entry_safe(dev, tmp, list, links.defer_sync) {
1082 list_del_init(&dev->links.defer_sync);
Saravana Kannan26e77702019-11-14 14:56:45 -08001083
Saravana Kannan21eb93f2020-02-21 00:05:08 -08001084 if (dev != dont_lock_dev)
1085 device_lock(dev);
Saravana Kannan26e77702019-11-14 14:56:45 -08001086
1087 if (dev->bus->sync_state)
1088 dev->bus->sync_state(dev);
1089 else if (dev->driver && dev->driver->sync_state)
1090 dev->driver->sync_state(dev);
1091
Saravana Kannan21eb93f2020-02-21 00:05:08 -08001092 if (dev != dont_lock_dev)
1093 device_unlock(dev);
Saravana Kannan26e77702019-11-14 14:56:45 -08001094
1095 put_device(dev);
1096 }
Saravana Kannanfc5a2512019-09-04 14:11:23 -07001097}
1098
1099void device_links_supplier_sync_state_pause(void)
1100{
1101 device_links_write_lock();
1102 defer_sync_state_count++;
1103 device_links_write_unlock();
1104}
1105
1106void device_links_supplier_sync_state_resume(void)
1107{
1108 struct device *dev, *tmp;
Saravana Kannan26e77702019-11-14 14:56:45 -08001109 LIST_HEAD(sync_list);
Saravana Kannanfc5a2512019-09-04 14:11:23 -07001110
1111 device_links_write_lock();
1112 if (!defer_sync_state_count) {
1113 WARN(true, "Unmatched sync_state pause/resume!");
1114 goto out;
1115 }
1116 defer_sync_state_count--;
1117 if (defer_sync_state_count)
1118 goto out;
1119
Saravana Kannan3b052a32020-11-20 18:02:17 -08001120 list_for_each_entry_safe(dev, tmp, &deferred_sync, links.defer_sync) {
Saravana Kannan26e77702019-11-14 14:56:45 -08001121 /*
1122 * Delete from deferred_sync list before queuing it to
Saravana Kannan3b052a32020-11-20 18:02:17 -08001123 * sync_list because defer_sync is used for both lists.
Saravana Kannan26e77702019-11-14 14:56:45 -08001124 */
Saravana Kannan3b052a32020-11-20 18:02:17 -08001125 list_del_init(&dev->links.defer_sync);
Saravana Kannan26e77702019-11-14 14:56:45 -08001126 __device_links_queue_sync_state(dev, &sync_list);
Saravana Kannanfc5a2512019-09-04 14:11:23 -07001127 }
1128out:
1129 device_links_write_unlock();
Saravana Kannan26e77702019-11-14 14:56:45 -08001130
Saravana Kannan21eb93f2020-02-21 00:05:08 -08001131 device_links_flush_sync_list(&sync_list, NULL);
Saravana Kannanfc5a2512019-09-04 14:11:23 -07001132}
1133
1134static int sync_state_resume_initcall(void)
1135{
1136 device_links_supplier_sync_state_resume();
1137 return 0;
1138}
1139late_initcall(sync_state_resume_initcall);
1140
1141static void __device_links_supplier_defer_sync(struct device *sup)
1142{
Saravana Kannan3b052a32020-11-20 18:02:17 -08001143 if (list_empty(&sup->links.defer_sync) && dev_has_sync_state(sup))
1144 list_add_tail(&sup->links.defer_sync, &deferred_sync);
Saravana Kannanfc5a2512019-09-04 14:11:23 -07001145}
1146
Saravana Kannan21c27f02020-05-18 23:30:00 -07001147static void device_link_drop_managed(struct device_link *link)
1148{
1149 link->flags &= ~DL_FLAG_MANAGED;
1150 WRITE_ONCE(link->status, DL_STATE_NONE);
1151 kref_put(&link->kref, __device_link_del);
1152}
1153
Saravana Kannanda6d6472020-05-21 12:18:00 -07001154static ssize_t waiting_for_supplier_show(struct device *dev,
1155 struct device_attribute *attr,
1156 char *buf)
1157{
1158 bool val;
1159
1160 device_lock(dev);
Saravana Kannan25ac86c2020-11-20 18:02:28 -08001161 val = !list_empty(&dev->fwnode->suppliers);
Saravana Kannanda6d6472020-05-21 12:18:00 -07001162 device_unlock(dev);
Joe Perchesaa838892020-09-16 13:40:39 -07001163 return sysfs_emit(buf, "%u\n", val);
Saravana Kannanda6d6472020-05-21 12:18:00 -07001164}
1165static DEVICE_ATTR_RO(waiting_for_supplier);
1166
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001167/**
Saravana Kannanb6f617d2021-03-02 13:11:31 -08001168 * device_links_force_bind - Prepares device to be force bound
1169 * @dev: Consumer device.
1170 *
1171 * device_bind_driver() force binds a device to a driver without calling any
1172 * driver probe functions. So the consumer really isn't going to wait for any
1173 * supplier before it's bound to the driver. We still want the device link
1174 * states to be sensible when this happens.
1175 *
1176 * In preparation for device_bind_driver(), this function goes through each
1177 * supplier device links and checks if the supplier is bound. If it is, then
1178 * the device link status is set to CONSUMER_PROBE. Otherwise, the device link
1179 * is dropped. Links without the DL_FLAG_MANAGED flag set are ignored.
1180 */
1181void device_links_force_bind(struct device *dev)
1182{
1183 struct device_link *link, *ln;
1184
1185 device_links_write_lock();
1186
1187 list_for_each_entry_safe(link, ln, &dev->links.suppliers, c_node) {
1188 if (!(link->flags & DL_FLAG_MANAGED))
1189 continue;
1190
1191 if (link->status != DL_STATE_AVAILABLE) {
1192 device_link_drop_managed(link);
1193 continue;
1194 }
1195 WRITE_ONCE(link->status, DL_STATE_CONSUMER_PROBE);
1196 }
1197 dev->links.status = DL_DEV_PROBING;
1198
1199 device_links_write_unlock();
1200}
1201
1202/**
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001203 * device_links_driver_bound - Update device links after probing its driver.
1204 * @dev: Device to update the links for.
1205 *
1206 * The probe has been successful, so update links from this device to any
1207 * consumers by changing their status to "available".
1208 *
1209 * Also change the status of @dev's links to suppliers to "active".
1210 *
Rafael J. Wysocki515db262019-07-16 17:21:06 +02001211 * Links without the DL_FLAG_MANAGED flag set are ignored.
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001212 */
1213void device_links_driver_bound(struct device *dev)
1214{
Saravana Kannan21c27f02020-05-18 23:30:00 -07001215 struct device_link *link, *ln;
Saravana Kannan26e77702019-11-14 14:56:45 -08001216 LIST_HEAD(sync_list);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001217
Saravana Kannanbcbbcfd2019-10-28 15:00:23 -07001218 /*
Saravana Kannan9528e0d2021-02-05 14:26:37 -08001219 * If a device binds successfully, it's expected to have created all
Saravana Kannanbcbbcfd2019-10-28 15:00:23 -07001220 * the device links it needs to or make new device links as it needs
Saravana Kannan9528e0d2021-02-05 14:26:37 -08001221 * them. So, fw_devlink no longer needs to create device links to any
1222 * of the device's suppliers.
1223 *
1224 * Also, if a child firmware node of this bound device is not added as
1225 * a device by now, assume it is never going to be added and make sure
1226 * other devices don't defer probe indefinitely by waiting for such a
1227 * child device.
Saravana Kannanbcbbcfd2019-10-28 15:00:23 -07001228 */
Saravana Kannan9528e0d2021-02-05 14:26:37 -08001229 if (dev->fwnode && dev->fwnode->dev == dev) {
1230 struct fwnode_handle *child;
Saravana Kannanf9aa4602020-11-20 18:02:31 -08001231 fwnode_links_purge_suppliers(dev->fwnode);
Saravana Kannan9528e0d2021-02-05 14:26:37 -08001232 fwnode_for_each_available_child_node(dev->fwnode, child)
1233 fw_devlink_purge_absent_suppliers(child);
1234 }
Saravana Kannanda6d6472020-05-21 12:18:00 -07001235 device_remove_file(dev, &dev_attr_waiting_for_supplier);
Saravana Kannanbcbbcfd2019-10-28 15:00:23 -07001236
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001237 device_links_write_lock();
1238
1239 list_for_each_entry(link, &dev->links.consumers, s_node) {
Rafael J. Wysocki515db262019-07-16 17:21:06 +02001240 if (!(link->flags & DL_FLAG_MANAGED))
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001241 continue;
1242
Rafael J. Wysocki15cfb092019-02-01 01:50:39 +01001243 /*
1244 * Links created during consumer probe may be in the "consumer
1245 * probe" state to start with if the supplier is still probing
1246 * when they are created and they may become "active" if the
1247 * consumer probe returns first. Skip them here.
1248 */
1249 if (link->status == DL_STATE_CONSUMER_PROBE ||
1250 link->status == DL_STATE_ACTIVE)
1251 continue;
1252
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001253 WARN_ON(link->status != DL_STATE_DORMANT);
1254 WRITE_ONCE(link->status, DL_STATE_AVAILABLE);
Rafael J. Wysockie7dd4012019-02-01 01:59:42 +01001255
1256 if (link->flags & DL_FLAG_AUTOPROBE_CONSUMER)
1257 driver_deferred_probe_add(link->consumer);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001258 }
1259
Saravana Kannan21eb93f2020-02-21 00:05:08 -08001260 if (defer_sync_state_count)
1261 __device_links_supplier_defer_sync(dev);
1262 else
1263 __device_links_queue_sync_state(dev, &sync_list);
1264
Saravana Kannan21c27f02020-05-18 23:30:00 -07001265 list_for_each_entry_safe(link, ln, &dev->links.suppliers, c_node) {
1266 struct device *supplier;
1267
Rafael J. Wysocki515db262019-07-16 17:21:06 +02001268 if (!(link->flags & DL_FLAG_MANAGED))
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001269 continue;
1270
Saravana Kannan21c27f02020-05-18 23:30:00 -07001271 supplier = link->supplier;
1272 if (link->flags & DL_FLAG_SYNC_STATE_ONLY) {
1273 /*
1274 * When DL_FLAG_SYNC_STATE_ONLY is set, it means no
1275 * other DL_MANAGED_LINK_FLAGS have been set. So, it's
1276 * save to drop the managed link completely.
1277 */
1278 device_link_drop_managed(link);
1279 } else {
1280 WARN_ON(link->status != DL_STATE_CONSUMER_PROBE);
1281 WRITE_ONCE(link->status, DL_STATE_ACTIVE);
1282 }
Saravana Kannanfc5a2512019-09-04 14:11:23 -07001283
Saravana Kannan21c27f02020-05-18 23:30:00 -07001284 /*
1285 * This needs to be done even for the deleted
1286 * DL_FLAG_SYNC_STATE_ONLY device link in case it was the last
1287 * device link that was preventing the supplier from getting a
1288 * sync_state() call.
1289 */
Saravana Kannanfc5a2512019-09-04 14:11:23 -07001290 if (defer_sync_state_count)
Saravana Kannan21c27f02020-05-18 23:30:00 -07001291 __device_links_supplier_defer_sync(supplier);
Saravana Kannanfc5a2512019-09-04 14:11:23 -07001292 else
Saravana Kannan21c27f02020-05-18 23:30:00 -07001293 __device_links_queue_sync_state(supplier, &sync_list);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001294 }
1295
1296 dev->links.status = DL_DEV_DRIVER_BOUND;
1297
1298 device_links_write_unlock();
Saravana Kannan26e77702019-11-14 14:56:45 -08001299
Saravana Kannan21eb93f2020-02-21 00:05:08 -08001300 device_links_flush_sync_list(&sync_list, dev);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001301}
1302
1303/**
1304 * __device_links_no_driver - Update links of a device without a driver.
1305 * @dev: Device without a drvier.
1306 *
1307 * Delete all non-persistent links from this device to any suppliers.
1308 *
1309 * Persistent links stay around, but their status is changed to "available",
1310 * unless they already are in the "supplier unbind in progress" state in which
1311 * case they need not be updated.
1312 *
Rafael J. Wysocki515db262019-07-16 17:21:06 +02001313 * Links without the DL_FLAG_MANAGED flag set are ignored.
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001314 */
1315static void __device_links_no_driver(struct device *dev)
1316{
1317 struct device_link *link, *ln;
1318
1319 list_for_each_entry_safe_reverse(link, ln, &dev->links.suppliers, c_node) {
Rafael J. Wysocki515db262019-07-16 17:21:06 +02001320 if (!(link->flags & DL_FLAG_MANAGED))
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001321 continue;
1322
Saravana Kannan8c3e3152020-05-26 15:09:27 -07001323 if (link->flags & DL_FLAG_AUTOREMOVE_CONSUMER) {
Rafael J. Wysocki515db262019-07-16 17:21:06 +02001324 device_link_drop_managed(link);
Saravana Kannan8c3e3152020-05-26 15:09:27 -07001325 continue;
1326 }
1327
1328 if (link->status != DL_STATE_CONSUMER_PROBE &&
1329 link->status != DL_STATE_ACTIVE)
1330 continue;
1331
1332 if (link->supplier->links.status == DL_DEV_DRIVER_BOUND) {
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001333 WRITE_ONCE(link->status, DL_STATE_AVAILABLE);
Saravana Kannan8c3e3152020-05-26 15:09:27 -07001334 } else {
1335 WARN_ON(!(link->flags & DL_FLAG_SYNC_STATE_ONLY));
1336 WRITE_ONCE(link->status, DL_STATE_DORMANT);
1337 }
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001338 }
1339
1340 dev->links.status = DL_DEV_NO_DRIVER;
1341}
1342
Rafael J. Wysocki15cfb092019-02-01 01:50:39 +01001343/**
1344 * device_links_no_driver - Update links after failing driver probe.
1345 * @dev: Device whose driver has just failed to probe.
1346 *
1347 * Clean up leftover links to consumers for @dev and invoke
1348 * %__device_links_no_driver() to update links to suppliers for it as
1349 * appropriate.
1350 *
Rafael J. Wysocki515db262019-07-16 17:21:06 +02001351 * Links without the DL_FLAG_MANAGED flag set are ignored.
Rafael J. Wysocki15cfb092019-02-01 01:50:39 +01001352 */
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001353void device_links_no_driver(struct device *dev)
1354{
Rafael J. Wysocki15cfb092019-02-01 01:50:39 +01001355 struct device_link *link;
1356
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001357 device_links_write_lock();
Rafael J. Wysocki15cfb092019-02-01 01:50:39 +01001358
1359 list_for_each_entry(link, &dev->links.consumers, s_node) {
Rafael J. Wysocki515db262019-07-16 17:21:06 +02001360 if (!(link->flags & DL_FLAG_MANAGED))
Rafael J. Wysocki15cfb092019-02-01 01:50:39 +01001361 continue;
1362
1363 /*
1364 * The probe has failed, so if the status of the link is
1365 * "consumer probe" or "active", it must have been added by
1366 * a probing consumer while this device was still probing.
1367 * Change its state to "dormant", as it represents a valid
1368 * relationship, but it is not functionally meaningful.
1369 */
1370 if (link->status == DL_STATE_CONSUMER_PROBE ||
1371 link->status == DL_STATE_ACTIVE)
1372 WRITE_ONCE(link->status, DL_STATE_DORMANT);
1373 }
1374
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001375 __device_links_no_driver(dev);
Rafael J. Wysocki15cfb092019-02-01 01:50:39 +01001376
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001377 device_links_write_unlock();
1378}
1379
1380/**
1381 * device_links_driver_cleanup - Update links after driver removal.
1382 * @dev: Device whose driver has just gone away.
1383 *
1384 * Update links to consumers for @dev by changing their status to "dormant" and
1385 * invoke %__device_links_no_driver() to update links to suppliers for it as
1386 * appropriate.
1387 *
Rafael J. Wysocki515db262019-07-16 17:21:06 +02001388 * Links without the DL_FLAG_MANAGED flag set are ignored.
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001389 */
1390void device_links_driver_cleanup(struct device *dev)
1391{
Rafael J. Wysockic8d50982019-02-01 01:45:55 +01001392 struct device_link *link, *ln;
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001393
1394 device_links_write_lock();
1395
Rafael J. Wysockic8d50982019-02-01 01:45:55 +01001396 list_for_each_entry_safe(link, ln, &dev->links.consumers, s_node) {
Rafael J. Wysocki515db262019-07-16 17:21:06 +02001397 if (!(link->flags & DL_FLAG_MANAGED))
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001398 continue;
1399
Vivek Gautame88728f2018-06-27 18:20:55 +05301400 WARN_ON(link->flags & DL_FLAG_AUTOREMOVE_CONSUMER);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001401 WARN_ON(link->status != DL_STATE_SUPPLIER_UNBIND);
Vivek Gautam1689cac2018-06-27 18:20:56 +05301402
1403 /*
1404 * autoremove the links between this @dev and its consumer
1405 * devices that are not active, i.e. where the link state
1406 * has moved to DL_STATE_SUPPLIER_UNBIND.
1407 */
1408 if (link->status == DL_STATE_SUPPLIER_UNBIND &&
1409 link->flags & DL_FLAG_AUTOREMOVE_SUPPLIER)
Rafael J. Wysocki515db262019-07-16 17:21:06 +02001410 device_link_drop_managed(link);
Vivek Gautam1689cac2018-06-27 18:20:56 +05301411
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001412 WRITE_ONCE(link->status, DL_STATE_DORMANT);
1413 }
1414
Saravana Kannan3b052a32020-11-20 18:02:17 -08001415 list_del_init(&dev->links.defer_sync);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001416 __device_links_no_driver(dev);
1417
1418 device_links_write_unlock();
1419}
1420
1421/**
1422 * device_links_busy - Check if there are any busy links to consumers.
1423 * @dev: Device to check.
1424 *
1425 * Check each consumer of the device and return 'true' if its link's status
1426 * is one of "consumer probe" or "active" (meaning that the given consumer is
1427 * probing right now or its driver is present). Otherwise, change the link
1428 * state to "supplier unbind" to prevent the consumer from being probed
1429 * successfully going forward.
1430 *
1431 * Return 'false' if there are no probing or active consumers.
1432 *
Rafael J. Wysocki515db262019-07-16 17:21:06 +02001433 * Links without the DL_FLAG_MANAGED flag set are ignored.
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001434 */
1435bool device_links_busy(struct device *dev)
1436{
1437 struct device_link *link;
1438 bool ret = false;
1439
1440 device_links_write_lock();
1441
1442 list_for_each_entry(link, &dev->links.consumers, s_node) {
Rafael J. Wysocki515db262019-07-16 17:21:06 +02001443 if (!(link->flags & DL_FLAG_MANAGED))
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001444 continue;
1445
1446 if (link->status == DL_STATE_CONSUMER_PROBE
1447 || link->status == DL_STATE_ACTIVE) {
1448 ret = true;
1449 break;
1450 }
1451 WRITE_ONCE(link->status, DL_STATE_SUPPLIER_UNBIND);
1452 }
1453
1454 dev->links.status = DL_DEV_UNBINDING;
1455
1456 device_links_write_unlock();
1457 return ret;
1458}
1459
1460/**
1461 * device_links_unbind_consumers - Force unbind consumers of the given device.
1462 * @dev: Device to unbind the consumers of.
1463 *
1464 * Walk the list of links to consumers for @dev and if any of them is in the
1465 * "consumer probe" state, wait for all device probes in progress to complete
1466 * and start over.
1467 *
1468 * If that's not the case, change the status of the link to "supplier unbind"
1469 * and check if the link was in the "active" state. If so, force the consumer
1470 * driver to unbind and start over (the consumer will not re-probe as we have
1471 * changed the state of the link already).
1472 *
Rafael J. Wysocki515db262019-07-16 17:21:06 +02001473 * Links without the DL_FLAG_MANAGED flag set are ignored.
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001474 */
1475void device_links_unbind_consumers(struct device *dev)
1476{
1477 struct device_link *link;
1478
1479 start:
1480 device_links_write_lock();
1481
1482 list_for_each_entry(link, &dev->links.consumers, s_node) {
1483 enum device_link_state status;
1484
Saravana Kannan05ef9832019-10-28 15:00:22 -07001485 if (!(link->flags & DL_FLAG_MANAGED) ||
1486 link->flags & DL_FLAG_SYNC_STATE_ONLY)
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001487 continue;
1488
1489 status = link->status;
1490 if (status == DL_STATE_CONSUMER_PROBE) {
1491 device_links_write_unlock();
1492
1493 wait_for_device_probe();
1494 goto start;
1495 }
1496 WRITE_ONCE(link->status, DL_STATE_SUPPLIER_UNBIND);
1497 if (status == DL_STATE_ACTIVE) {
1498 struct device *consumer = link->consumer;
1499
1500 get_device(consumer);
1501
1502 device_links_write_unlock();
1503
1504 device_release_driver_internal(consumer, NULL,
1505 consumer->parent);
1506 put_device(consumer);
1507 goto start;
1508 }
1509 }
1510
1511 device_links_write_unlock();
1512}
1513
1514/**
1515 * device_links_purge - Delete existing links to other devices.
1516 * @dev: Target device.
1517 */
1518static void device_links_purge(struct device *dev)
1519{
1520 struct device_link *link, *ln;
1521
Saravana Kannan287905e2020-05-21 12:17:58 -07001522 if (dev->class == &devlink_class)
1523 return;
1524
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001525 /*
1526 * Delete all of the remaining links from this device to any other
1527 * devices (either consumers or suppliers).
1528 */
1529 device_links_write_lock();
1530
1531 list_for_each_entry_safe_reverse(link, ln, &dev->links.suppliers, c_node) {
1532 WARN_ON(link->status == DL_STATE_ACTIVE);
Lukas Wunneread18c22018-02-10 19:27:12 +01001533 __device_link_del(&link->kref);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001534 }
1535
1536 list_for_each_entry_safe_reverse(link, ln, &dev->links.consumers, s_node) {
1537 WARN_ON(link->status != DL_STATE_DORMANT &&
1538 link->status != DL_STATE_NONE);
Lukas Wunneread18c22018-02-10 19:27:12 +01001539 __device_link_del(&link->kref);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001540 }
1541
1542 device_links_write_unlock();
1543}
1544
Saravana Kannanb90fb8f2020-12-17 19:17:01 -08001545#define FW_DEVLINK_FLAGS_PERMISSIVE (DL_FLAG_INFERRED | \
1546 DL_FLAG_SYNC_STATE_ONLY)
1547#define FW_DEVLINK_FLAGS_ON (DL_FLAG_INFERRED | \
1548 DL_FLAG_AUTOPROBE_CONSUMER)
1549#define FW_DEVLINK_FLAGS_RPM (FW_DEVLINK_FLAGS_ON | \
1550 DL_FLAG_PM_RUNTIME)
1551
Saravana Kannanea718c692021-03-02 13:11:32 -08001552static u32 fw_devlink_flags = FW_DEVLINK_FLAGS_ON;
Saravana Kannan42926ac2020-05-14 22:34:57 -07001553static int __init fw_devlink_setup(char *arg)
1554{
1555 if (!arg)
1556 return -EINVAL;
1557
1558 if (strcmp(arg, "off") == 0) {
1559 fw_devlink_flags = 0;
1560 } else if (strcmp(arg, "permissive") == 0) {
Saravana Kannanb90fb8f2020-12-17 19:17:01 -08001561 fw_devlink_flags = FW_DEVLINK_FLAGS_PERMISSIVE;
Saravana Kannan42926ac2020-05-14 22:34:57 -07001562 } else if (strcmp(arg, "on") == 0) {
Saravana Kannanb90fb8f2020-12-17 19:17:01 -08001563 fw_devlink_flags = FW_DEVLINK_FLAGS_ON;
Saravana Kannan42926ac2020-05-14 22:34:57 -07001564 } else if (strcmp(arg, "rpm") == 0) {
Saravana Kannanb90fb8f2020-12-17 19:17:01 -08001565 fw_devlink_flags = FW_DEVLINK_FLAGS_RPM;
Saravana Kannan42926ac2020-05-14 22:34:57 -07001566 }
1567 return 0;
1568}
1569early_param("fw_devlink", fw_devlink_setup);
1570
Saravana Kannan19d0f5f2021-02-05 14:26:39 -08001571static bool fw_devlink_strict;
1572static int __init fw_devlink_strict_setup(char *arg)
1573{
1574 return strtobool(arg, &fw_devlink_strict);
1575}
1576early_param("fw_devlink.strict", fw_devlink_strict_setup);
1577
Saravana Kannan42926ac2020-05-14 22:34:57 -07001578u32 fw_devlink_get_flags(void)
1579{
1580 return fw_devlink_flags;
1581}
1582
1583static bool fw_devlink_is_permissive(void)
1584{
Saravana Kannanb90fb8f2020-12-17 19:17:01 -08001585 return fw_devlink_flags == FW_DEVLINK_FLAGS_PERMISSIVE;
Saravana Kannan42926ac2020-05-14 22:34:57 -07001586}
1587
Saravana Kannan19d0f5f2021-02-05 14:26:39 -08001588bool fw_devlink_is_strict(void)
1589{
1590 return fw_devlink_strict && !fw_devlink_is_permissive();
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001591}
1592
Saravana Kannanc2c724c2020-11-20 18:02:27 -08001593static void fw_devlink_parse_fwnode(struct fwnode_handle *fwnode)
1594{
1595 if (fwnode->flags & FWNODE_FLAG_LINKS_ADDED)
1596 return;
1597
Saravana Kannan2d09e6e2020-11-20 18:02:32 -08001598 fwnode_call_int_op(fwnode, add_links);
Saravana Kannanc2c724c2020-11-20 18:02:27 -08001599 fwnode->flags |= FWNODE_FLAG_LINKS_ADDED;
1600}
1601
1602static void fw_devlink_parse_fwtree(struct fwnode_handle *fwnode)
1603{
1604 struct fwnode_handle *child = NULL;
1605
1606 fw_devlink_parse_fwnode(fwnode);
1607
1608 while ((child = fwnode_get_next_available_child_node(fwnode, child)))
1609 fw_devlink_parse_fwtree(child);
1610}
1611
Saravana Kannand46f3e32021-04-01 21:03:41 -07001612static void fw_devlink_relax_link(struct device_link *link)
1613{
1614 if (!(link->flags & DL_FLAG_INFERRED))
1615 return;
1616
1617 if (link->flags == (DL_FLAG_MANAGED | FW_DEVLINK_FLAGS_PERMISSIVE))
1618 return;
1619
1620 pm_runtime_drop_link(link);
1621 link->flags = DL_FLAG_MANAGED | FW_DEVLINK_FLAGS_PERMISSIVE;
1622 dev_dbg(link->consumer, "Relaxing link with %s\n",
1623 dev_name(link->supplier));
1624}
1625
1626static int fw_devlink_no_driver(struct device *dev, void *data)
1627{
1628 struct device_link *link = to_devlink(dev);
1629
1630 if (!link->supplier->can_match)
1631 fw_devlink_relax_link(link);
1632
1633 return 0;
1634}
1635
1636void fw_devlink_drivers_done(void)
1637{
1638 fw_devlink_drv_reg_done = true;
1639 device_links_write_lock();
1640 class_for_each_device(&devlink_class, NULL, NULL,
1641 fw_devlink_no_driver);
1642 device_links_write_unlock();
1643}
1644
1645static void fw_devlink_unblock_consumers(struct device *dev)
1646{
1647 struct device_link *link;
1648
1649 if (!fw_devlink_flags || fw_devlink_is_permissive())
1650 return;
1651
1652 device_links_write_lock();
1653 list_for_each_entry(link, &dev->links.consumers, s_node)
1654 fw_devlink_relax_link(link);
1655 device_links_write_unlock();
1656}
1657
Saravana Kannanf9aa4602020-11-20 18:02:31 -08001658/**
Saravana Kannanb0e2fa42020-12-17 19:17:02 -08001659 * fw_devlink_relax_cycle - Convert cyclic links to SYNC_STATE_ONLY links
1660 * @con: Device to check dependencies for.
1661 * @sup: Device to check against.
1662 *
1663 * Check if @sup depends on @con or any device dependent on it (its child or
1664 * its consumer etc). When such a cyclic dependency is found, convert all
1665 * device links created solely by fw_devlink into SYNC_STATE_ONLY device links.
1666 * This is the equivalent of doing fw_devlink=permissive just between the
1667 * devices in the cycle. We need to do this because, at this point, fw_devlink
1668 * can't tell which of these dependencies is not a real dependency.
1669 *
1670 * Return 1 if a cycle is found. Otherwise, return 0.
1671 */
kernel test robotc13b8272020-12-18 14:39:35 +08001672static int fw_devlink_relax_cycle(struct device *con, void *sup)
Saravana Kannanb0e2fa42020-12-17 19:17:02 -08001673{
1674 struct device_link *link;
1675 int ret;
1676
1677 if (con == sup)
1678 return 1;
1679
1680 ret = device_for_each_child(con, sup, fw_devlink_relax_cycle);
1681 if (ret)
1682 return ret;
1683
1684 list_for_each_entry(link, &con->links.consumers, s_node) {
1685 if ((link->flags & ~DL_FLAG_INFERRED) ==
1686 (DL_FLAG_SYNC_STATE_ONLY | DL_FLAG_MANAGED))
1687 continue;
1688
1689 if (!fw_devlink_relax_cycle(link->consumer, sup))
1690 continue;
1691
1692 ret = 1;
1693
Saravana Kannand46f3e32021-04-01 21:03:41 -07001694 fw_devlink_relax_link(link);
Saravana Kannanb0e2fa42020-12-17 19:17:02 -08001695 }
1696 return ret;
1697}
1698
1699/**
Saravana Kannanf9aa4602020-11-20 18:02:31 -08001700 * fw_devlink_create_devlink - Create a device link from a consumer to fwnode
Pierre-Louis Bossart37c52f72021-03-31 18:26:08 -05001701 * @con: consumer device for the device link
1702 * @sup_handle: fwnode handle of supplier
1703 * @flags: devlink flags
Saravana Kannanf9aa4602020-11-20 18:02:31 -08001704 *
1705 * This function will try to create a device link between the consumer device
1706 * @con and the supplier device represented by @sup_handle.
1707 *
1708 * The supplier has to be provided as a fwnode because incorrect cycles in
1709 * fwnode links can sometimes cause the supplier device to never be created.
1710 * This function detects such cases and returns an error if it cannot create a
1711 * device link from the consumer to a missing supplier.
1712 *
1713 * Returns,
1714 * 0 on successfully creating a device link
1715 * -EINVAL if the device link cannot be created as expected
1716 * -EAGAIN if the device link cannot be created right now, but it may be
1717 * possible to do that in the future
1718 */
1719static int fw_devlink_create_devlink(struct device *con,
1720 struct fwnode_handle *sup_handle, u32 flags)
1721{
1722 struct device *sup_dev;
1723 int ret = 0;
1724
1725 sup_dev = get_dev_from_fwnode(sup_handle);
1726 if (sup_dev) {
1727 /*
Saravana Kannan74c782c2021-02-05 14:26:41 -08001728 * If it's one of those drivers that don't actually bind to
1729 * their device using driver core, then don't wait on this
1730 * supplier device indefinitely.
1731 */
1732 if (sup_dev->links.status == DL_DEV_NO_DRIVER &&
1733 sup_handle->flags & FWNODE_FLAG_INITIALIZED) {
1734 ret = -EINVAL;
1735 goto out;
1736 }
1737
1738 /*
Saravana Kannanf9aa4602020-11-20 18:02:31 -08001739 * If this fails, it is due to cycles in device links. Just
1740 * give up on this link and treat it as invalid.
1741 */
Saravana Kannanb0e2fa42020-12-17 19:17:02 -08001742 if (!device_link_add(con, sup_dev, flags) &&
1743 !(flags & DL_FLAG_SYNC_STATE_ONLY)) {
1744 dev_info(con, "Fixing up cyclic dependency with %s\n",
1745 dev_name(sup_dev));
1746 device_links_write_lock();
1747 fw_devlink_relax_cycle(con, sup_dev);
1748 device_links_write_unlock();
1749 device_link_add(con, sup_dev,
1750 FW_DEVLINK_FLAGS_PERMISSIVE);
Saravana Kannanf9aa4602020-11-20 18:02:31 -08001751 ret = -EINVAL;
Saravana Kannanb0e2fa42020-12-17 19:17:02 -08001752 }
Saravana Kannanf9aa4602020-11-20 18:02:31 -08001753
1754 goto out;
1755 }
1756
Saravana Kannan74c782c2021-02-05 14:26:41 -08001757 /* Supplier that's already initialized without a struct device. */
1758 if (sup_handle->flags & FWNODE_FLAG_INITIALIZED)
1759 return -EINVAL;
1760
Saravana Kannanf9aa4602020-11-20 18:02:31 -08001761 /*
1762 * DL_FLAG_SYNC_STATE_ONLY doesn't block probing and supports
1763 * cycles. So cycle detection isn't necessary and shouldn't be
1764 * done.
1765 */
1766 if (flags & DL_FLAG_SYNC_STATE_ONLY)
1767 return -EAGAIN;
1768
1769 /*
1770 * If we can't find the supplier device from its fwnode, it might be
1771 * due to a cyclic dependency between fwnodes. Some of these cycles can
1772 * be broken by applying logic. Check for these types of cycles and
1773 * break them so that devices in the cycle probe properly.
1774 *
Saravana Kannan2de9d8e2021-09-15 10:09:37 -07001775 * If the supplier's parent is dependent on the consumer, then the
1776 * consumer and supplier have a cyclic dependency. Since fw_devlink
1777 * can't tell which of the inferred dependencies are incorrect, don't
1778 * enforce probe ordering between any of the devices in this cyclic
1779 * dependency. Do this by relaxing all the fw_devlink device links in
1780 * this cycle and by treating the fwnode link between the consumer and
1781 * the supplier as an invalid dependency.
Saravana Kannanf9aa4602020-11-20 18:02:31 -08001782 */
1783 sup_dev = fwnode_get_next_parent_dev(sup_handle);
1784 if (sup_dev && device_is_dependent(con, sup_dev)) {
Saravana Kannan2de9d8e2021-09-15 10:09:37 -07001785 dev_info(con, "Fixing up cyclic dependency with %pfwP (%s)\n",
1786 sup_handle, dev_name(sup_dev));
1787 device_links_write_lock();
1788 fw_devlink_relax_cycle(con, sup_dev);
1789 device_links_write_unlock();
Saravana Kannanf9aa4602020-11-20 18:02:31 -08001790 ret = -EINVAL;
1791 } else {
1792 /*
1793 * Can't check for cycles or no cycles. So let's try
1794 * again later.
1795 */
1796 ret = -EAGAIN;
1797 }
1798
1799out:
1800 put_device(sup_dev);
1801 return ret;
1802}
1803
1804/**
1805 * __fw_devlink_link_to_consumers - Create device links to consumers of a device
Pierre-Louis Bossart37c52f72021-03-31 18:26:08 -05001806 * @dev: Device that needs to be linked to its consumers
Saravana Kannanf9aa4602020-11-20 18:02:31 -08001807 *
1808 * This function looks at all the consumer fwnodes of @dev and creates device
1809 * links between the consumer device and @dev (supplier).
1810 *
1811 * If the consumer device has not been added yet, then this function creates a
1812 * SYNC_STATE_ONLY link between @dev (supplier) and the closest ancestor device
1813 * of the consumer fwnode. This is necessary to make sure @dev doesn't get a
1814 * sync_state() callback before the real consumer device gets to be added and
1815 * then probed.
1816 *
1817 * Once device links are created from the real consumer to @dev (supplier), the
1818 * fwnode links are deleted.
1819 */
1820static void __fw_devlink_link_to_consumers(struct device *dev)
1821{
1822 struct fwnode_handle *fwnode = dev->fwnode;
1823 struct fwnode_link *link, *tmp;
1824
1825 list_for_each_entry_safe(link, tmp, &fwnode->consumers, s_hook) {
1826 u32 dl_flags = fw_devlink_get_flags();
1827 struct device *con_dev;
1828 bool own_link = true;
1829 int ret;
1830
1831 con_dev = get_dev_from_fwnode(link->consumer);
1832 /*
1833 * If consumer device is not available yet, make a "proxy"
1834 * SYNC_STATE_ONLY link from the consumer's parent device to
1835 * the supplier device. This is necessary to make sure the
1836 * supplier doesn't get a sync_state() callback before the real
1837 * consumer can create a device link to the supplier.
1838 *
1839 * This proxy link step is needed to handle the case where the
1840 * consumer's parent device is added before the supplier.
1841 */
1842 if (!con_dev) {
1843 con_dev = fwnode_get_next_parent_dev(link->consumer);
1844 /*
1845 * However, if the consumer's parent device is also the
1846 * parent of the supplier, don't create a
1847 * consumer-supplier link from the parent to its child
1848 * device. Such a dependency is impossible.
1849 */
1850 if (con_dev &&
1851 fwnode_is_ancestor_of(con_dev->fwnode, fwnode)) {
1852 put_device(con_dev);
1853 con_dev = NULL;
1854 } else {
1855 own_link = false;
Saravana Kannanb90fb8f2020-12-17 19:17:01 -08001856 dl_flags = FW_DEVLINK_FLAGS_PERMISSIVE;
Saravana Kannanf9aa4602020-11-20 18:02:31 -08001857 }
1858 }
1859
1860 if (!con_dev)
1861 continue;
1862
1863 ret = fw_devlink_create_devlink(con_dev, fwnode, dl_flags);
1864 put_device(con_dev);
1865 if (!own_link || ret == -EAGAIN)
1866 continue;
1867
1868 list_del(&link->s_hook);
1869 list_del(&link->c_hook);
1870 kfree(link);
1871 }
1872}
1873
1874/**
1875 * __fw_devlink_link_to_suppliers - Create device links to suppliers of a device
Pierre-Louis Bossart37c52f72021-03-31 18:26:08 -05001876 * @dev: The consumer device that needs to be linked to its suppliers
1877 * @fwnode: Root of the fwnode tree that is used to create device links
Saravana Kannanf9aa4602020-11-20 18:02:31 -08001878 *
1879 * This function looks at all the supplier fwnodes of fwnode tree rooted at
1880 * @fwnode and creates device links between @dev (consumer) and all the
1881 * supplier devices of the entire fwnode tree at @fwnode.
1882 *
1883 * The function creates normal (non-SYNC_STATE_ONLY) device links between @dev
1884 * and the real suppliers of @dev. Once these device links are created, the
1885 * fwnode links are deleted. When such device links are successfully created,
1886 * this function is called recursively on those supplier devices. This is
1887 * needed to detect and break some invalid cycles in fwnode links. See
1888 * fw_devlink_create_devlink() for more details.
1889 *
1890 * In addition, it also looks at all the suppliers of the entire fwnode tree
1891 * because some of the child devices of @dev that have not been added yet
1892 * (because @dev hasn't probed) might already have their suppliers added to
1893 * driver core. So, this function creates SYNC_STATE_ONLY device links between
1894 * @dev (consumer) and these suppliers to make sure they don't execute their
1895 * sync_state() callbacks before these child devices have a chance to create
1896 * their device links. The fwnode links that correspond to the child devices
1897 * aren't delete because they are needed later to create the device links
1898 * between the real consumer and supplier devices.
1899 */
1900static void __fw_devlink_link_to_suppliers(struct device *dev,
1901 struct fwnode_handle *fwnode)
1902{
1903 bool own_link = (dev->fwnode == fwnode);
1904 struct fwnode_link *link, *tmp;
1905 struct fwnode_handle *child = NULL;
1906 u32 dl_flags;
1907
1908 if (own_link)
1909 dl_flags = fw_devlink_get_flags();
1910 else
Saravana Kannanb90fb8f2020-12-17 19:17:01 -08001911 dl_flags = FW_DEVLINK_FLAGS_PERMISSIVE;
Saravana Kannanf9aa4602020-11-20 18:02:31 -08001912
1913 list_for_each_entry_safe(link, tmp, &fwnode->suppliers, c_hook) {
1914 int ret;
1915 struct device *sup_dev;
1916 struct fwnode_handle *sup = link->supplier;
1917
1918 ret = fw_devlink_create_devlink(dev, sup, dl_flags);
1919 if (!own_link || ret == -EAGAIN)
1920 continue;
1921
1922 list_del(&link->s_hook);
1923 list_del(&link->c_hook);
1924 kfree(link);
1925
1926 /* If no device link was created, nothing more to do. */
1927 if (ret)
1928 continue;
1929
1930 /*
1931 * If a device link was successfully created to a supplier, we
1932 * now need to try and link the supplier to all its suppliers.
1933 *
1934 * This is needed to detect and delete false dependencies in
1935 * fwnode links that haven't been converted to a device link
1936 * yet. See comments in fw_devlink_create_devlink() for more
1937 * details on the false dependency.
1938 *
1939 * Without deleting these false dependencies, some devices will
1940 * never probe because they'll keep waiting for their false
1941 * dependency fwnode links to be converted to device links.
1942 */
1943 sup_dev = get_dev_from_fwnode(sup);
1944 __fw_devlink_link_to_suppliers(sup_dev, sup_dev->fwnode);
1945 put_device(sup_dev);
1946 }
1947
1948 /*
1949 * Make "proxy" SYNC_STATE_ONLY device links to represent the needs of
1950 * all the descendants. This proxy link step is needed to handle the
1951 * case where the supplier is added before the consumer's parent device
1952 * (@dev).
1953 */
1954 while ((child = fwnode_get_next_available_child_node(fwnode, child)))
1955 __fw_devlink_link_to_suppliers(dev, child);
1956}
1957
Saravana Kannan5f5377e2020-05-14 22:34:58 -07001958static void fw_devlink_link_device(struct device *dev)
1959{
Saravana Kannanf9aa4602020-11-20 18:02:31 -08001960 struct fwnode_handle *fwnode = dev->fwnode;
Saravana Kannan5f5377e2020-05-14 22:34:58 -07001961
Saravana Kannanf9aa4602020-11-20 18:02:31 -08001962 if (!fw_devlink_flags)
1963 return;
Saravana Kannanc84b9092020-11-20 18:02:21 -08001964
Saravana Kannanf9aa4602020-11-20 18:02:31 -08001965 fw_devlink_parse_fwtree(fwnode);
1966
1967 mutex_lock(&fwnode_link_lock);
1968 __fw_devlink_link_to_consumers(dev);
1969 __fw_devlink_link_to_suppliers(dev, fwnode);
1970 mutex_unlock(&fwnode_link_lock);
Saravana Kannan716a7a22020-05-14 22:34:59 -07001971}
Saravana Kannanc84b9092020-11-20 18:02:21 -08001972
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001973/* Device links support end. */
1974
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001975int (*platform_notify)(struct device *dev) = NULL;
1976int (*platform_notify_remove)(struct device *dev) = NULL;
Dan Williamse105b8b2008-04-21 10:51:07 -07001977static struct kobject *dev_kobj;
1978struct kobject *sysfs_dev_char_kobj;
1979struct kobject *sysfs_dev_block_kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980
Rafael J. Wysocki5e33bc42013-08-28 21:41:01 +02001981static DEFINE_MUTEX(device_hotplug_lock);
1982
1983void lock_device_hotplug(void)
1984{
1985 mutex_lock(&device_hotplug_lock);
1986}
1987
1988void unlock_device_hotplug(void)
1989{
1990 mutex_unlock(&device_hotplug_lock);
1991}
1992
1993int lock_device_hotplug_sysfs(void)
1994{
1995 if (mutex_trylock(&device_hotplug_lock))
1996 return 0;
1997
1998 /* Avoid busy looping (5 ms of sleep should do). */
1999 msleep(5);
2000 return restart_syscall();
2001}
2002
Greg Kroah-Hartman4e886c22008-01-27 12:12:43 -08002003#ifdef CONFIG_BLOCK
2004static inline int device_is_not_partition(struct device *dev)
2005{
2006 return !(dev->type == &part_type);
2007}
2008#else
2009static inline int device_is_not_partition(struct device *dev)
2010{
2011 return 1;
2012}
2013#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014
Rafael J. Wysockib2ebd9d2021-07-12 19:28:16 +02002015static void device_platform_notify(struct device *dev)
Heikki Krogerus07de0e82018-11-09 17:21:34 +03002016{
Rafael J. Wysockib2ebd9d2021-07-12 19:28:16 +02002017 acpi_device_notify(dev);
Heikki Krogerus7847a142018-11-09 17:21:35 +03002018
Rafael J. Wysockib2ebd9d2021-07-12 19:28:16 +02002019 software_node_notify(dev);
Heikki Krogerus7847a142018-11-09 17:21:35 +03002020
Rafael J. Wysockib2ebd9d2021-07-12 19:28:16 +02002021 if (platform_notify)
Heikki Krogerus07de0e82018-11-09 17:21:34 +03002022 platform_notify(dev);
Rafael J. Wysockib2ebd9d2021-07-12 19:28:16 +02002023}
2024
2025static void device_platform_notify_remove(struct device *dev)
2026{
2027 acpi_device_notify_remove(dev);
2028
2029 software_node_notify_remove(dev);
2030
2031 if (platform_notify_remove)
Heikki Krogerus07de0e82018-11-09 17:21:34 +03002032 platform_notify_remove(dev);
Heikki Krogerus07de0e82018-11-09 17:21:34 +03002033}
2034
Alan Stern3e956372006-06-16 17:10:48 -04002035/**
2036 * dev_driver_string - Return a device's driver name, if at all possible
2037 * @dev: struct device to get the name of
2038 *
2039 * Will return the device's driver's name if it is bound to a device. If
yan9169c012012-04-20 21:08:45 +08002040 * the device is not bound to a driver, it will return the name of the bus
Alan Stern3e956372006-06-16 17:10:48 -04002041 * it is attached to. If it is not attached to a bus either, an empty
2042 * string will be returned.
2043 */
Jean Delvarebf9ca692008-07-30 12:29:21 -07002044const char *dev_driver_string(const struct device *dev)
Alan Stern3e956372006-06-16 17:10:48 -04002045{
Alan Stern35899722009-12-04 11:06:57 -05002046 struct device_driver *drv;
2047
2048 /* dev->driver can change to NULL underneath us because of unbinding,
2049 * so be careful about accessing it. dev->bus and dev->class should
2050 * never change once they are set, so they don't need special care.
2051 */
Mark Rutland6aa7de02017-10-23 14:07:29 -07002052 drv = READ_ONCE(dev->driver);
Saravana Kannane020ff62021-01-10 09:54:07 -08002053 return drv ? drv->name : dev_bus_name(dev);
Alan Stern3e956372006-06-16 17:10:48 -04002054}
Matthew Wilcox310a9222006-09-23 23:35:04 -06002055EXPORT_SYMBOL(dev_driver_string);
Alan Stern3e956372006-06-16 17:10:48 -04002056
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057#define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr)
2058
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002059static ssize_t dev_attr_show(struct kobject *kobj, struct attribute *attr,
2060 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002062 struct device_attribute *dev_attr = to_dev_attr(attr);
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +02002063 struct device *dev = kobj_to_dev(kobj);
Dmitry Torokhov4a0c20b2005-04-29 01:23:47 -05002064 ssize_t ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065
2066 if (dev_attr->show)
Yani Ioannou54b6f352005-05-17 06:39:34 -04002067 ret = dev_attr->show(dev, dev_attr, buf);
Andrew Morton815d2d52008-03-04 15:09:07 -08002068 if (ret >= (ssize_t)PAGE_SIZE) {
Sergey Senozhatskya52668c2017-12-11 21:50:21 +09002069 printk("dev_attr_show: %pS returned bad count\n",
2070 dev_attr->show);
Andrew Morton815d2d52008-03-04 15:09:07 -08002071 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 return ret;
2073}
2074
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002075static ssize_t dev_attr_store(struct kobject *kobj, struct attribute *attr,
2076 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002078 struct device_attribute *dev_attr = to_dev_attr(attr);
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +02002079 struct device *dev = kobj_to_dev(kobj);
Dmitry Torokhov4a0c20b2005-04-29 01:23:47 -05002080 ssize_t ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081
2082 if (dev_attr->store)
Yani Ioannou54b6f352005-05-17 06:39:34 -04002083 ret = dev_attr->store(dev, dev_attr, buf, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 return ret;
2085}
2086
Emese Revfy52cf25d2010-01-19 02:58:23 +01002087static const struct sysfs_ops dev_sysfs_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 .show = dev_attr_show,
2089 .store = dev_attr_store,
2090};
2091
Kay Sieversca22e562011-12-14 14:29:38 -08002092#define to_ext_attr(x) container_of(x, struct dev_ext_attribute, attr)
2093
2094ssize_t device_store_ulong(struct device *dev,
2095 struct device_attribute *attr,
2096 const char *buf, size_t size)
2097{
2098 struct dev_ext_attribute *ea = to_ext_attr(attr);
Kaitao chengf88184b2018-11-06 08:34:54 -08002099 int ret;
2100 unsigned long new;
2101
2102 ret = kstrtoul(buf, 0, &new);
2103 if (ret)
2104 return ret;
Kay Sieversca22e562011-12-14 14:29:38 -08002105 *(unsigned long *)(ea->var) = new;
2106 /* Always return full write size even if we didn't consume all */
2107 return size;
2108}
2109EXPORT_SYMBOL_GPL(device_store_ulong);
2110
2111ssize_t device_show_ulong(struct device *dev,
2112 struct device_attribute *attr,
2113 char *buf)
2114{
2115 struct dev_ext_attribute *ea = to_ext_attr(attr);
Joe Perchesaa838892020-09-16 13:40:39 -07002116 return sysfs_emit(buf, "%lx\n", *(unsigned long *)(ea->var));
Kay Sieversca22e562011-12-14 14:29:38 -08002117}
2118EXPORT_SYMBOL_GPL(device_show_ulong);
2119
2120ssize_t device_store_int(struct device *dev,
2121 struct device_attribute *attr,
2122 const char *buf, size_t size)
2123{
2124 struct dev_ext_attribute *ea = to_ext_attr(attr);
Kaitao chengf88184b2018-11-06 08:34:54 -08002125 int ret;
2126 long new;
2127
2128 ret = kstrtol(buf, 0, &new);
2129 if (ret)
2130 return ret;
2131
2132 if (new > INT_MAX || new < INT_MIN)
Kay Sieversca22e562011-12-14 14:29:38 -08002133 return -EINVAL;
2134 *(int *)(ea->var) = new;
2135 /* Always return full write size even if we didn't consume all */
2136 return size;
2137}
2138EXPORT_SYMBOL_GPL(device_store_int);
2139
2140ssize_t device_show_int(struct device *dev,
2141 struct device_attribute *attr,
2142 char *buf)
2143{
2144 struct dev_ext_attribute *ea = to_ext_attr(attr);
2145
Joe Perchesaa838892020-09-16 13:40:39 -07002146 return sysfs_emit(buf, "%d\n", *(int *)(ea->var));
Kay Sieversca22e562011-12-14 14:29:38 -08002147}
2148EXPORT_SYMBOL_GPL(device_show_int);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149
Borislav Petkov91872392012-10-09 19:52:05 +02002150ssize_t device_store_bool(struct device *dev, struct device_attribute *attr,
2151 const char *buf, size_t size)
2152{
2153 struct dev_ext_attribute *ea = to_ext_attr(attr);
2154
2155 if (strtobool(buf, ea->var) < 0)
2156 return -EINVAL;
2157
2158 return size;
2159}
2160EXPORT_SYMBOL_GPL(device_store_bool);
2161
2162ssize_t device_show_bool(struct device *dev, struct device_attribute *attr,
2163 char *buf)
2164{
2165 struct dev_ext_attribute *ea = to_ext_attr(attr);
2166
Joe Perchesaa838892020-09-16 13:40:39 -07002167 return sysfs_emit(buf, "%d\n", *(bool *)(ea->var));
Borislav Petkov91872392012-10-09 19:52:05 +02002168}
2169EXPORT_SYMBOL_GPL(device_show_bool);
2170
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171/**
Robert P. J. Dayf8878dc2013-06-01 20:17:34 -04002172 * device_release - free device structure.
2173 * @kobj: device's kobject.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174 *
Robert P. J. Dayf8878dc2013-06-01 20:17:34 -04002175 * This is called once the reference count for the object
2176 * reaches 0. We forward the call to the device's release
2177 * method, which should handle actually freeing the structure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002179static void device_release(struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +02002181 struct device *dev = kobj_to_dev(kobj);
Greg Kroah-Hartmanfb069a52008-12-16 12:23:36 -08002182 struct device_private *p = dev->p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183
Ming Leia525a3d2012-07-25 01:42:29 +08002184 /*
2185 * Some platform devices are driven without driver attached
2186 * and managed resources may have been acquired. Make sure
2187 * all resources are released.
2188 *
2189 * Drivers still can add resources into device after device
2190 * is deleted but alive, so release devres here to avoid
2191 * possible memory leak.
2192 */
2193 devres_release_all(dev);
2194
Jim Quinlane0d07272020-09-17 18:43:40 +02002195 kfree(dev->dma_range_map);
2196
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 if (dev->release)
2198 dev->release(dev);
Kay Sieversf9f852d2006-10-07 21:54:55 +02002199 else if (dev->type && dev->type->release)
2200 dev->type->release(dev);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07002201 else if (dev->class && dev->class->dev_release)
2202 dev->class->dev_release(dev);
Arjan van de Venf810a5c2008-07-25 19:45:39 -07002203 else
Mauro Carvalho Chehab0c1bc6b2020-04-14 18:48:37 +02002204 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 +01002205 dev_name(dev));
Greg Kroah-Hartmanfb069a52008-12-16 12:23:36 -08002206 kfree(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207}
2208
Eric W. Biedermanbc451f22010-03-30 11:31:25 -07002209static const void *device_namespace(struct kobject *kobj)
2210{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +02002211 struct device *dev = kobj_to_dev(kobj);
Eric W. Biedermanbc451f22010-03-30 11:31:25 -07002212 const void *ns = NULL;
2213
2214 if (dev->class && dev->class->ns_type)
2215 ns = dev->class->namespace(dev);
2216
2217 return ns;
2218}
2219
Dmitry Torokhov9944e892018-07-20 21:56:50 +00002220static void device_get_ownership(struct kobject *kobj, kuid_t *uid, kgid_t *gid)
2221{
2222 struct device *dev = kobj_to_dev(kobj);
2223
2224 if (dev->class && dev->class->get_ownership)
2225 dev->class->get_ownership(dev, uid, gid);
2226}
2227
Greg Kroah-Hartman8f4afc42007-10-11 10:47:49 -06002228static struct kobj_type device_ktype = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229 .release = device_release,
2230 .sysfs_ops = &dev_sysfs_ops,
Eric W. Biedermanbc451f22010-03-30 11:31:25 -07002231 .namespace = device_namespace,
Dmitry Torokhov9944e892018-07-20 21:56:50 +00002232 .get_ownership = device_get_ownership,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233};
2234
2235
Kay Sievers312c0042005-11-16 09:00:00 +01002236static int dev_uevent_filter(struct kset *kset, struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237{
2238 struct kobj_type *ktype = get_ktype(kobj);
2239
Greg Kroah-Hartman8f4afc42007-10-11 10:47:49 -06002240 if (ktype == &device_ktype) {
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +02002241 struct device *dev = kobj_to_dev(kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242 if (dev->bus)
2243 return 1;
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07002244 if (dev->class)
2245 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 }
2247 return 0;
2248}
2249
Kay Sievers312c0042005-11-16 09:00:00 +01002250static const char *dev_uevent_name(struct kset *kset, struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +02002252 struct device *dev = kobj_to_dev(kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07002254 if (dev->bus)
2255 return dev->bus->name;
2256 if (dev->class)
2257 return dev->class->name;
2258 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259}
2260
Kay Sievers7eff2e72007-08-14 15:15:12 +02002261static int dev_uevent(struct kset *kset, struct kobject *kobj,
2262 struct kobj_uevent_env *env)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +02002264 struct device *dev = kobj_to_dev(kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 int retval = 0;
2266
Kay Sievers6fcf53a2009-04-30 15:23:42 +02002267 /* add device node properties if present */
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07002268 if (MAJOR(dev->devt)) {
Kay Sievers6fcf53a2009-04-30 15:23:42 +02002269 const char *tmp;
2270 const char *name;
Al Viro2c9ede52011-07-23 20:24:48 -04002271 umode_t mode = 0;
Greg Kroah-Hartman4e4098a2013-04-11 11:43:29 -07002272 kuid_t uid = GLOBAL_ROOT_UID;
2273 kgid_t gid = GLOBAL_ROOT_GID;
Kay Sievers6fcf53a2009-04-30 15:23:42 +02002274
Kay Sievers7eff2e72007-08-14 15:15:12 +02002275 add_uevent_var(env, "MAJOR=%u", MAJOR(dev->devt));
2276 add_uevent_var(env, "MINOR=%u", MINOR(dev->devt));
Kay Sievers3c2670e2013-04-06 09:56:00 -07002277 name = device_get_devnode(dev, &mode, &uid, &gid, &tmp);
Kay Sievers6fcf53a2009-04-30 15:23:42 +02002278 if (name) {
2279 add_uevent_var(env, "DEVNAME=%s", name);
Kay Sieverse454cea2009-09-18 23:01:12 +02002280 if (mode)
2281 add_uevent_var(env, "DEVMODE=%#o", mode & 0777);
Greg Kroah-Hartman4e4098a2013-04-11 11:43:29 -07002282 if (!uid_eq(uid, GLOBAL_ROOT_UID))
2283 add_uevent_var(env, "DEVUID=%u", from_kuid(&init_user_ns, uid));
2284 if (!gid_eq(gid, GLOBAL_ROOT_GID))
2285 add_uevent_var(env, "DEVGID=%u", from_kgid(&init_user_ns, gid));
Kay Sievers3c2670e2013-04-06 09:56:00 -07002286 kfree(tmp);
Kay Sievers6fcf53a2009-04-30 15:23:42 +02002287 }
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07002288 }
2289
Kay Sievers414264f2007-03-12 21:08:57 +01002290 if (dev->type && dev->type->name)
Kay Sievers7eff2e72007-08-14 15:15:12 +02002291 add_uevent_var(env, "DEVTYPE=%s", dev->type->name);
Kay Sievers414264f2007-03-12 21:08:57 +01002292
Kay Sievers239378f2006-10-07 21:54:55 +02002293 if (dev->driver)
Kay Sievers7eff2e72007-08-14 15:15:12 +02002294 add_uevent_var(env, "DRIVER=%s", dev->driver->name);
Kay Sievers239378f2006-10-07 21:54:55 +02002295
Grant Likely07d57a32012-02-01 11:22:22 -07002296 /* Add common DT information about the device */
2297 of_device_uevent(dev, env);
2298
Kay Sievers7eff2e72007-08-14 15:15:12 +02002299 /* have the bus specific function add its stuff */
Kay Sievers312c0042005-11-16 09:00:00 +01002300 if (dev->bus && dev->bus->uevent) {
Kay Sievers7eff2e72007-08-14 15:15:12 +02002301 retval = dev->bus->uevent(dev, env);
Kay Sieversf9f852d2006-10-07 21:54:55 +02002302 if (retval)
Greg Kroah-Hartman7dc72b22007-11-28 23:49:41 -08002303 pr_debug("device: '%s': %s: bus uevent() returned %d\n",
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01002304 dev_name(dev), __func__, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305 }
2306
Kay Sievers7eff2e72007-08-14 15:15:12 +02002307 /* have the class specific function add its stuff */
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07002308 if (dev->class && dev->class->dev_uevent) {
Kay Sievers7eff2e72007-08-14 15:15:12 +02002309 retval = dev->class->dev_uevent(dev, env);
Kay Sieversf9f852d2006-10-07 21:54:55 +02002310 if (retval)
Greg Kroah-Hartman7dc72b22007-11-28 23:49:41 -08002311 pr_debug("device: '%s': %s: class uevent() "
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01002312 "returned %d\n", dev_name(dev),
Harvey Harrison2b3a3022008-03-04 16:41:05 -08002313 __func__, retval);
Kay Sieversf9f852d2006-10-07 21:54:55 +02002314 }
2315
Stefan Weileef35c22010-08-06 21:11:15 +02002316 /* have the device type specific function add its stuff */
Kay Sieversf9f852d2006-10-07 21:54:55 +02002317 if (dev->type && dev->type->uevent) {
Kay Sievers7eff2e72007-08-14 15:15:12 +02002318 retval = dev->type->uevent(dev, env);
Kay Sieversf9f852d2006-10-07 21:54:55 +02002319 if (retval)
Greg Kroah-Hartman7dc72b22007-11-28 23:49:41 -08002320 pr_debug("device: '%s': %s: dev_type uevent() "
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01002321 "returned %d\n", dev_name(dev),
Harvey Harrison2b3a3022008-03-04 16:41:05 -08002322 __func__, retval);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07002323 }
2324
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325 return retval;
2326}
2327
Emese Revfy9cd43612009-12-31 14:52:51 +01002328static const struct kset_uevent_ops device_uevent_ops = {
Kay Sievers312c0042005-11-16 09:00:00 +01002329 .filter = dev_uevent_filter,
2330 .name = dev_uevent_name,
2331 .uevent = dev_uevent,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332};
2333
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07002334static ssize_t uevent_show(struct device *dev, struct device_attribute *attr,
Kay Sievers16574dc2007-04-06 01:40:38 +02002335 char *buf)
2336{
2337 struct kobject *top_kobj;
2338 struct kset *kset;
Kay Sievers7eff2e72007-08-14 15:15:12 +02002339 struct kobj_uevent_env *env = NULL;
Kay Sievers16574dc2007-04-06 01:40:38 +02002340 int i;
Joe Perches948b3ed2020-09-16 13:40:42 -07002341 int len = 0;
Kay Sievers16574dc2007-04-06 01:40:38 +02002342 int retval;
2343
2344 /* search the kset, the device belongs to */
2345 top_kobj = &dev->kobj;
Kay Sievers5c5daf62007-08-12 20:43:55 +02002346 while (!top_kobj->kset && top_kobj->parent)
2347 top_kobj = top_kobj->parent;
Kay Sievers16574dc2007-04-06 01:40:38 +02002348 if (!top_kobj->kset)
2349 goto out;
Kay Sievers5c5daf62007-08-12 20:43:55 +02002350
Kay Sievers16574dc2007-04-06 01:40:38 +02002351 kset = top_kobj->kset;
2352 if (!kset->uevent_ops || !kset->uevent_ops->uevent)
2353 goto out;
2354
2355 /* respect filter */
2356 if (kset->uevent_ops && kset->uevent_ops->filter)
2357 if (!kset->uevent_ops->filter(kset, &dev->kobj))
2358 goto out;
2359
Kay Sievers7eff2e72007-08-14 15:15:12 +02002360 env = kzalloc(sizeof(struct kobj_uevent_env), GFP_KERNEL);
2361 if (!env)
Greg Kroah-Hartmanc7308c82007-05-02 14:14:11 +02002362 return -ENOMEM;
2363
Kay Sievers16574dc2007-04-06 01:40:38 +02002364 /* let the kset specific function add its keys */
Kay Sievers7eff2e72007-08-14 15:15:12 +02002365 retval = kset->uevent_ops->uevent(kset, &dev->kobj, env);
Kay Sievers16574dc2007-04-06 01:40:38 +02002366 if (retval)
2367 goto out;
2368
2369 /* copy keys to file */
Kay Sievers7eff2e72007-08-14 15:15:12 +02002370 for (i = 0; i < env->envp_idx; i++)
Joe Perches948b3ed2020-09-16 13:40:42 -07002371 len += sysfs_emit_at(buf, len, "%s\n", env->envp[i]);
Kay Sievers16574dc2007-04-06 01:40:38 +02002372out:
Kay Sievers7eff2e72007-08-14 15:15:12 +02002373 kfree(env);
Joe Perches948b3ed2020-09-16 13:40:42 -07002374 return len;
Kay Sievers16574dc2007-04-06 01:40:38 +02002375}
2376
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07002377static ssize_t uevent_store(struct device *dev, struct device_attribute *attr,
Kay Sieversa7fd6702005-10-01 14:49:43 +02002378 const char *buf, size_t count)
2379{
Peter Rajnohadf44b472018-12-05 12:27:44 +01002380 int rc;
2381
2382 rc = kobject_synth_uevent(&dev->kobj, buf, count);
2383
2384 if (rc) {
Peter Rajnohaf36776f2017-05-09 15:22:30 +02002385 dev_err(dev, "uevent: failed to send synthetic uevent\n");
Peter Rajnohadf44b472018-12-05 12:27:44 +01002386 return rc;
2387 }
Kay Sievers60a96a52007-07-08 22:29:26 +02002388
Kay Sieversa7fd6702005-10-01 14:49:43 +02002389 return count;
2390}
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07002391static DEVICE_ATTR_RW(uevent);
Kay Sieversa7fd6702005-10-01 14:49:43 +02002392
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07002393static ssize_t online_show(struct device *dev, struct device_attribute *attr,
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02002394 char *buf)
2395{
2396 bool val;
2397
Rafael J. Wysocki5e33bc42013-08-28 21:41:01 +02002398 device_lock(dev);
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02002399 val = !dev->offline;
Rafael J. Wysocki5e33bc42013-08-28 21:41:01 +02002400 device_unlock(dev);
Joe Perchesaa838892020-09-16 13:40:39 -07002401 return sysfs_emit(buf, "%u\n", val);
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02002402}
2403
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07002404static ssize_t online_store(struct device *dev, struct device_attribute *attr,
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02002405 const char *buf, size_t count)
2406{
2407 bool val;
2408 int ret;
2409
2410 ret = strtobool(buf, &val);
2411 if (ret < 0)
2412 return ret;
2413
Rafael J. Wysocki5e33bc42013-08-28 21:41:01 +02002414 ret = lock_device_hotplug_sysfs();
2415 if (ret)
2416 return ret;
2417
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02002418 ret = val ? device_online(dev) : device_offline(dev);
2419 unlock_device_hotplug();
2420 return ret < 0 ? ret : count;
2421}
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07002422static DEVICE_ATTR_RW(online);
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02002423
Rajat Jain70f400d2021-05-24 10:18:11 -07002424static ssize_t removable_show(struct device *dev, struct device_attribute *attr,
2425 char *buf)
2426{
2427 const char *loc;
2428
2429 switch (dev->removable) {
2430 case DEVICE_REMOVABLE:
2431 loc = "removable";
2432 break;
2433 case DEVICE_FIXED:
2434 loc = "fixed";
2435 break;
2436 default:
2437 loc = "unknown";
2438 }
2439 return sysfs_emit(buf, "%s\n", loc);
2440}
2441static DEVICE_ATTR_RO(removable);
2442
Greg Kroah-Hartmanfa6fdb32013-08-08 15:22:55 -07002443int device_add_groups(struct device *dev, const struct attribute_group **groups)
Dmitry Torokhov621a1672007-03-10 01:37:34 -05002444{
Greg Kroah-Hartman3e9b2ba2013-08-21 13:47:50 -07002445 return sysfs_create_groups(&dev->kobj, groups);
Dmitry Torokhov621a1672007-03-10 01:37:34 -05002446}
Dmitry Torokhova7670d42017-07-19 17:24:31 -07002447EXPORT_SYMBOL_GPL(device_add_groups);
Dmitry Torokhov621a1672007-03-10 01:37:34 -05002448
Greg Kroah-Hartmanfa6fdb32013-08-08 15:22:55 -07002449void device_remove_groups(struct device *dev,
2450 const struct attribute_group **groups)
Dmitry Torokhov621a1672007-03-10 01:37:34 -05002451{
Greg Kroah-Hartman3e9b2ba2013-08-21 13:47:50 -07002452 sysfs_remove_groups(&dev->kobj, groups);
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -07002453}
Dmitry Torokhova7670d42017-07-19 17:24:31 -07002454EXPORT_SYMBOL_GPL(device_remove_groups);
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -07002455
Dmitry Torokhov57b8ff02017-07-19 17:24:33 -07002456union device_attr_group_devres {
2457 const struct attribute_group *group;
2458 const struct attribute_group **groups;
2459};
2460
2461static int devm_attr_group_match(struct device *dev, void *res, void *data)
2462{
2463 return ((union device_attr_group_devres *)res)->group == data;
2464}
2465
2466static void devm_attr_group_remove(struct device *dev, void *res)
2467{
2468 union device_attr_group_devres *devres = res;
2469 const struct attribute_group *group = devres->group;
2470
2471 dev_dbg(dev, "%s: removing group %p\n", __func__, group);
2472 sysfs_remove_group(&dev->kobj, group);
2473}
2474
2475static void devm_attr_groups_remove(struct device *dev, void *res)
2476{
2477 union device_attr_group_devres *devres = res;
2478 const struct attribute_group **groups = devres->groups;
2479
2480 dev_dbg(dev, "%s: removing groups %p\n", __func__, groups);
2481 sysfs_remove_groups(&dev->kobj, groups);
2482}
2483
2484/**
2485 * devm_device_add_group - given a device, create a managed attribute group
2486 * @dev: The device to create the group for
2487 * @grp: The attribute group to create
2488 *
2489 * This function creates a group for the first time. It will explicitly
2490 * warn and error if any of the attribute files being created already exist.
2491 *
2492 * Returns 0 on success or error code on failure.
2493 */
2494int devm_device_add_group(struct device *dev, const struct attribute_group *grp)
2495{
2496 union device_attr_group_devres *devres;
2497 int error;
2498
2499 devres = devres_alloc(devm_attr_group_remove,
2500 sizeof(*devres), GFP_KERNEL);
2501 if (!devres)
2502 return -ENOMEM;
2503
2504 error = sysfs_create_group(&dev->kobj, grp);
2505 if (error) {
2506 devres_free(devres);
2507 return error;
2508 }
2509
2510 devres->group = grp;
2511 devres_add(dev, devres);
2512 return 0;
2513}
2514EXPORT_SYMBOL_GPL(devm_device_add_group);
2515
2516/**
2517 * devm_device_remove_group: remove a managed group from a device
2518 * @dev: device to remove the group from
2519 * @grp: group to remove
2520 *
2521 * This function removes a group of attributes from a device. The attributes
2522 * previously have to have been created for this group, otherwise it will fail.
2523 */
2524void devm_device_remove_group(struct device *dev,
2525 const struct attribute_group *grp)
2526{
2527 WARN_ON(devres_release(dev, devm_attr_group_remove,
2528 devm_attr_group_match,
2529 /* cast away const */ (void *)grp));
2530}
2531EXPORT_SYMBOL_GPL(devm_device_remove_group);
2532
2533/**
2534 * devm_device_add_groups - create a bunch of managed attribute groups
2535 * @dev: The device to create the group for
2536 * @groups: The attribute groups to create, NULL terminated
2537 *
2538 * This function creates a bunch of managed attribute groups. If an error
2539 * occurs when creating a group, all previously created groups will be
2540 * removed, unwinding everything back to the original state when this
2541 * function was called. It will explicitly warn and error if any of the
2542 * attribute files being created already exist.
2543 *
2544 * Returns 0 on success or error code from sysfs_create_group on failure.
2545 */
2546int devm_device_add_groups(struct device *dev,
2547 const struct attribute_group **groups)
2548{
2549 union device_attr_group_devres *devres;
2550 int error;
2551
2552 devres = devres_alloc(devm_attr_groups_remove,
2553 sizeof(*devres), GFP_KERNEL);
2554 if (!devres)
2555 return -ENOMEM;
2556
2557 error = sysfs_create_groups(&dev->kobj, groups);
2558 if (error) {
2559 devres_free(devres);
2560 return error;
2561 }
2562
2563 devres->groups = groups;
2564 devres_add(dev, devres);
2565 return 0;
2566}
2567EXPORT_SYMBOL_GPL(devm_device_add_groups);
2568
2569/**
2570 * devm_device_remove_groups - remove a list of managed groups
2571 *
2572 * @dev: The device for the groups to be removed from
2573 * @groups: NULL terminated list of groups to be removed
2574 *
2575 * If groups is not NULL, remove the specified groups from the device.
2576 */
2577void devm_device_remove_groups(struct device *dev,
2578 const struct attribute_group **groups)
2579{
2580 WARN_ON(devres_release(dev, devm_attr_groups_remove,
2581 devm_attr_group_match,
2582 /* cast away const */ (void *)groups));
2583}
2584EXPORT_SYMBOL_GPL(devm_device_remove_groups);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07002586static int device_add_attrs(struct device *dev)
2587{
2588 struct class *class = dev->class;
Stephen Hemmingeraed65af2011-03-28 09:12:52 -07002589 const struct device_type *type = dev->type;
Dmitry Torokhov621a1672007-03-10 01:37:34 -05002590 int error;
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07002591
Dmitry Torokhov621a1672007-03-10 01:37:34 -05002592 if (class) {
Greg Kroah-Hartmand05a6f92013-07-14 16:05:58 -07002593 error = device_add_groups(dev, class->dev_groups);
Kay Sieversf9f852d2006-10-07 21:54:55 +02002594 if (error)
Dmitry Torokhov621a1672007-03-10 01:37:34 -05002595 return error;
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07002596 }
Kay Sieversf9f852d2006-10-07 21:54:55 +02002597
Dmitry Torokhov621a1672007-03-10 01:37:34 -05002598 if (type) {
2599 error = device_add_groups(dev, type->groups);
Kay Sieversf9f852d2006-10-07 21:54:55 +02002600 if (error)
Greg Kroah-Hartmana6b01de2013-10-05 18:19:30 -07002601 goto err_remove_class_groups;
Kay Sieversf9f852d2006-10-07 21:54:55 +02002602 }
2603
Dmitry Torokhov621a1672007-03-10 01:37:34 -05002604 error = device_add_groups(dev, dev->groups);
2605 if (error)
2606 goto err_remove_type_groups;
2607
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02002608 if (device_supports_offline(dev) && !dev->offline_disabled) {
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07002609 error = device_create_file(dev, &dev_attr_online);
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02002610 if (error)
Rafael J. Wysockiecfbf6f2013-12-12 06:11:02 +01002611 goto err_remove_dev_groups;
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02002612 }
2613
Saravana Kannan25ac86c2020-11-20 18:02:28 -08002614 if (fw_devlink_flags && !fw_devlink_is_permissive() && dev->fwnode) {
Saravana Kannanda6d6472020-05-21 12:18:00 -07002615 error = device_create_file(dev, &dev_attr_waiting_for_supplier);
2616 if (error)
2617 goto err_remove_dev_online;
2618 }
2619
Rajat Jain70f400d2021-05-24 10:18:11 -07002620 if (dev_removable_is_valid(dev)) {
2621 error = device_create_file(dev, &dev_attr_removable);
2622 if (error)
2623 goto err_remove_dev_waiting_for_supplier;
2624 }
2625
Dmitry Torokhov621a1672007-03-10 01:37:34 -05002626 return 0;
2627
Rajat Jain70f400d2021-05-24 10:18:11 -07002628 err_remove_dev_waiting_for_supplier:
2629 device_remove_file(dev, &dev_attr_waiting_for_supplier);
Saravana Kannanda6d6472020-05-21 12:18:00 -07002630 err_remove_dev_online:
2631 device_remove_file(dev, &dev_attr_online);
Rafael J. Wysockiecfbf6f2013-12-12 06:11:02 +01002632 err_remove_dev_groups:
2633 device_remove_groups(dev, dev->groups);
Dmitry Torokhov621a1672007-03-10 01:37:34 -05002634 err_remove_type_groups:
2635 if (type)
2636 device_remove_groups(dev, type->groups);
Greg Kroah-Hartmand05a6f92013-07-14 16:05:58 -07002637 err_remove_class_groups:
2638 if (class)
2639 device_remove_groups(dev, class->dev_groups);
Dmitry Torokhov621a1672007-03-10 01:37:34 -05002640
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07002641 return error;
2642}
2643
2644static void device_remove_attrs(struct device *dev)
2645{
2646 struct class *class = dev->class;
Stephen Hemmingeraed65af2011-03-28 09:12:52 -07002647 const struct device_type *type = dev->type;
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07002648
Rajat Jain70f400d2021-05-24 10:18:11 -07002649 device_remove_file(dev, &dev_attr_removable);
Saravana Kannanda6d6472020-05-21 12:18:00 -07002650 device_remove_file(dev, &dev_attr_waiting_for_supplier);
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07002651 device_remove_file(dev, &dev_attr_online);
Dmitry Torokhov621a1672007-03-10 01:37:34 -05002652 device_remove_groups(dev, dev->groups);
Kay Sieversf9f852d2006-10-07 21:54:55 +02002653
Dmitry Torokhov621a1672007-03-10 01:37:34 -05002654 if (type)
2655 device_remove_groups(dev, type->groups);
2656
Greg Kroah-Hartmana6b01de2013-10-05 18:19:30 -07002657 if (class)
Greg Kroah-Hartmand05a6f92013-07-14 16:05:58 -07002658 device_remove_groups(dev, class->dev_groups);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07002659}
2660
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07002661static ssize_t dev_show(struct device *dev, struct device_attribute *attr,
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07002662 char *buf)
2663{
2664 return print_dev_t(buf, dev->devt);
2665}
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07002666static DEVICE_ATTR_RO(dev);
Tejun Heoad6a1e12007-06-14 03:45:17 +09002667
Kay Sieversca22e562011-12-14 14:29:38 -08002668/* /sys/devices/ */
Greg Kroah-Hartman881c6cfd2007-11-01 09:29:06 -06002669struct kset *devices_kset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671/**
Grygorii Strashko52cdbdd2015-07-27 20:43:01 +03002672 * devices_kset_move_before - Move device in the devices_kset's list.
2673 * @deva: Device to move.
2674 * @devb: Device @deva should come before.
2675 */
2676static void devices_kset_move_before(struct device *deva, struct device *devb)
2677{
2678 if (!devices_kset)
2679 return;
2680 pr_debug("devices_kset: Moving %s before %s\n",
2681 dev_name(deva), dev_name(devb));
2682 spin_lock(&devices_kset->list_lock);
2683 list_move_tail(&deva->kobj.entry, &devb->kobj.entry);
2684 spin_unlock(&devices_kset->list_lock);
2685}
2686
2687/**
2688 * devices_kset_move_after - Move device in the devices_kset's list.
2689 * @deva: Device to move
2690 * @devb: Device @deva should come after.
2691 */
2692static void devices_kset_move_after(struct device *deva, struct device *devb)
2693{
2694 if (!devices_kset)
2695 return;
2696 pr_debug("devices_kset: Moving %s after %s\n",
2697 dev_name(deva), dev_name(devb));
2698 spin_lock(&devices_kset->list_lock);
2699 list_move(&deva->kobj.entry, &devb->kobj.entry);
2700 spin_unlock(&devices_kset->list_lock);
2701}
2702
2703/**
2704 * devices_kset_move_last - move the device to the end of devices_kset's list.
2705 * @dev: device to move
2706 */
2707void devices_kset_move_last(struct device *dev)
2708{
2709 if (!devices_kset)
2710 return;
2711 pr_debug("devices_kset: Moving %s to end of list\n", dev_name(dev));
2712 spin_lock(&devices_kset->list_lock);
2713 list_move_tail(&dev->kobj.entry, &devices_kset->list);
2714 spin_unlock(&devices_kset->list_lock);
2715}
2716
2717/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002718 * device_create_file - create sysfs attribute file for device.
2719 * @dev: device.
2720 * @attr: device attribute descriptor.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721 */
Phil Carmody26579ab2009-12-18 15:34:19 +02002722int device_create_file(struct device *dev,
2723 const struct device_attribute *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724{
2725 int error = 0;
Felipe Balbi8f46baa2013-02-20 10:31:42 +02002726
2727 if (dev) {
2728 WARN(((attr->attr.mode & S_IWUGO) && !attr->store),
dyoung@redhat.com97521972013-05-16 14:31:30 +08002729 "Attribute %s: write permission without 'store'\n",
2730 attr->attr.name);
Felipe Balbi8f46baa2013-02-20 10:31:42 +02002731 WARN(((attr->attr.mode & S_IRUGO) && !attr->show),
dyoung@redhat.com97521972013-05-16 14:31:30 +08002732 "Attribute %s: read permission without 'show'\n",
2733 attr->attr.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734 error = sysfs_create_file(&dev->kobj, &attr->attr);
Felipe Balbi8f46baa2013-02-20 10:31:42 +02002735 }
2736
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737 return error;
2738}
David Graham White86df2682013-07-21 20:41:14 -04002739EXPORT_SYMBOL_GPL(device_create_file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740
2741/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002742 * device_remove_file - remove sysfs attribute file.
2743 * @dev: device.
2744 * @attr: device attribute descriptor.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745 */
Phil Carmody26579ab2009-12-18 15:34:19 +02002746void device_remove_file(struct device *dev,
2747 const struct device_attribute *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748{
Cornelia Huck0c98b192008-01-31 10:39:38 +01002749 if (dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750 sysfs_remove_file(&dev->kobj, &attr->attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751}
David Graham White86df2682013-07-21 20:41:14 -04002752EXPORT_SYMBOL_GPL(device_remove_file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753
Greg Kroah-Hartman2589f1882006-09-19 09:39:19 -07002754/**
Tejun Heo6b0afc22014-02-03 14:03:01 -05002755 * device_remove_file_self - remove sysfs attribute file from its own method.
2756 * @dev: device.
2757 * @attr: device attribute descriptor.
2758 *
2759 * See kernfs_remove_self() for details.
2760 */
2761bool device_remove_file_self(struct device *dev,
2762 const struct device_attribute *attr)
2763{
2764 if (dev)
2765 return sysfs_remove_file_self(&dev->kobj, &attr->attr);
2766 else
2767 return false;
2768}
2769EXPORT_SYMBOL_GPL(device_remove_file_self);
2770
2771/**
Greg Kroah-Hartman2589f1882006-09-19 09:39:19 -07002772 * device_create_bin_file - create sysfs binary attribute file for device.
2773 * @dev: device.
2774 * @attr: device binary attribute descriptor.
2775 */
Phil Carmody66ecb922009-12-18 15:34:20 +02002776int device_create_bin_file(struct device *dev,
2777 const struct bin_attribute *attr)
Greg Kroah-Hartman2589f1882006-09-19 09:39:19 -07002778{
2779 int error = -EINVAL;
2780 if (dev)
2781 error = sysfs_create_bin_file(&dev->kobj, attr);
2782 return error;
2783}
2784EXPORT_SYMBOL_GPL(device_create_bin_file);
2785
2786/**
2787 * device_remove_bin_file - remove sysfs binary attribute file
2788 * @dev: device.
2789 * @attr: device binary attribute descriptor.
2790 */
Phil Carmody66ecb922009-12-18 15:34:20 +02002791void device_remove_bin_file(struct device *dev,
2792 const struct bin_attribute *attr)
Greg Kroah-Hartman2589f1882006-09-19 09:39:19 -07002793{
2794 if (dev)
2795 sysfs_remove_bin_file(&dev->kobj, attr);
2796}
2797EXPORT_SYMBOL_GPL(device_remove_bin_file);
2798
James Bottomley34bb61f2005-09-06 16:56:51 -07002799static void klist_children_get(struct klist_node *n)
2800{
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08002801 struct device_private *p = to_device_private_parent(n);
2802 struct device *dev = p->device;
James Bottomley34bb61f2005-09-06 16:56:51 -07002803
2804 get_device(dev);
2805}
2806
2807static void klist_children_put(struct klist_node *n)
2808{
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08002809 struct device_private *p = to_device_private_parent(n);
2810 struct device *dev = p->device;
James Bottomley34bb61f2005-09-06 16:56:51 -07002811
2812 put_device(dev);
2813}
2814
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002816 * device_initialize - init device structure.
2817 * @dev: device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818 *
Cornelia Huck57394112008-09-03 18:26:40 +02002819 * This prepares the device for use by other layers by initializing
2820 * its fields.
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002821 * It is the first half of device_register(), if called by
Cornelia Huck57394112008-09-03 18:26:40 +02002822 * that function, though it can also be called separately, so one
2823 * may use @dev's fields. In particular, get_device()/put_device()
2824 * may be used for reference counting of @dev after calling this
2825 * function.
2826 *
Alan Sternb10d5ef2012-01-17 11:39:00 -05002827 * All fields in @dev must be initialized by the caller to 0, except
2828 * for those explicitly set to some other value. The simplest
2829 * approach is to use kzalloc() to allocate the structure containing
2830 * @dev.
2831 *
Cornelia Huck57394112008-09-03 18:26:40 +02002832 * NOTE: Use put_device() to give up your reference instead of freeing
2833 * @dev directly once you have called this function.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835void device_initialize(struct device *dev)
2836{
Greg Kroah-Hartman881c6cfd2007-11-01 09:29:06 -06002837 dev->kobj.kset = devices_kset;
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -07002838 kobject_init(&dev->kobj, &device_ktype);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839 INIT_LIST_HEAD(&dev->dma_pools);
Thomas Gleixner31427882010-01-29 20:39:02 +00002840 mutex_init(&dev->mutex);
Dan Williams87a30e12019-07-17 18:08:26 -07002841#ifdef CONFIG_PROVE_LOCKING
2842 mutex_init(&dev->lockdep_mutex);
2843#endif
Peter Zijlstra1704f472010-03-19 01:37:42 +01002844 lockdep_set_novalidate_class(&dev->mutex);
Tejun Heo9ac78492007-01-20 16:00:26 +09002845 spin_lock_init(&dev->devres_lock);
2846 INIT_LIST_HEAD(&dev->devres_head);
Alan Stern3b98aea2008-08-07 13:06:12 -04002847 device_pm_init(dev);
Christoph Hellwig87348132006-12-06 20:32:33 -08002848 set_dev_node(dev, -1);
Jiang Liu4a7cc832015-07-09 16:00:44 +08002849#ifdef CONFIG_GENERIC_MSI_IRQ
Thomas Gleixner77e89af2021-07-29 23:51:47 +02002850 raw_spin_lock_init(&dev->msi_lock);
Jiang Liu4a7cc832015-07-09 16:00:44 +08002851 INIT_LIST_HEAD(&dev->msi_list);
2852#endif
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01002853 INIT_LIST_HEAD(&dev->links.consumers);
2854 INIT_LIST_HEAD(&dev->links.suppliers);
Saravana Kannan3b052a32020-11-20 18:02:17 -08002855 INIT_LIST_HEAD(&dev->links.defer_sync);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01002856 dev->links.status = DL_DEV_NO_DRIVER;
Christoph Hellwig6d4e9a82021-02-10 10:56:39 +01002857#if defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_DEVICE) || \
2858 defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU) || \
2859 defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU_ALL)
2860 dev->dma_coherent = dma_default_coherent;
2861#endif
Claire Chang69031f52021-06-19 11:40:34 +08002862#ifdef CONFIG_SWIOTLB
Will Deacon463e8622021-07-20 14:38:24 +01002863 dev->dma_io_tlb_mem = &io_tlb_default_mem;
Claire Chang69031f52021-06-19 11:40:34 +08002864#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865}
David Graham White86df2682013-07-21 20:41:14 -04002866EXPORT_SYMBOL_GPL(device_initialize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867
Tejun Heod73ce002013-03-12 11:30:05 -07002868struct kobject *virtual_device_parent(struct device *dev)
Greg Kroah-Hartmanf0ee61a2006-10-23 10:40:54 -07002869{
Kay Sievers86406242007-03-14 03:25:56 +01002870 static struct kobject *virtual_dir = NULL;
Greg Kroah-Hartmanf0ee61a2006-10-23 10:40:54 -07002871
Kay Sievers86406242007-03-14 03:25:56 +01002872 if (!virtual_dir)
Greg Kroah-Hartman4ff6abf2007-11-05 22:24:43 -08002873 virtual_dir = kobject_create_and_add("virtual",
Greg Kroah-Hartman881c6cfd2007-11-01 09:29:06 -06002874 &devices_kset->kobj);
Greg Kroah-Hartmanf0ee61a2006-10-23 10:40:54 -07002875
Kay Sievers86406242007-03-14 03:25:56 +01002876 return virtual_dir;
Greg Kroah-Hartmanf0ee61a2006-10-23 10:40:54 -07002877}
2878
Eric W. Biedermanbc451f22010-03-30 11:31:25 -07002879struct class_dir {
2880 struct kobject kobj;
2881 struct class *class;
2882};
2883
2884#define to_class_dir(obj) container_of(obj, struct class_dir, kobj)
2885
2886static void class_dir_release(struct kobject *kobj)
2887{
2888 struct class_dir *dir = to_class_dir(kobj);
2889 kfree(dir);
2890}
2891
2892static const
2893struct kobj_ns_type_operations *class_dir_child_ns_type(struct kobject *kobj)
2894{
2895 struct class_dir *dir = to_class_dir(kobj);
2896 return dir->class->ns_type;
2897}
2898
2899static struct kobj_type class_dir_ktype = {
2900 .release = class_dir_release,
2901 .sysfs_ops = &kobj_sysfs_ops,
2902 .child_ns_type = class_dir_child_ns_type
2903};
2904
2905static struct kobject *
2906class_dir_create_and_add(struct class *class, struct kobject *parent_kobj)
2907{
2908 struct class_dir *dir;
2909 int retval;
2910
2911 dir = kzalloc(sizeof(*dir), GFP_KERNEL);
2912 if (!dir)
Tetsuo Handa84d0c272018-05-07 19:10:31 +09002913 return ERR_PTR(-ENOMEM);
Eric W. Biedermanbc451f22010-03-30 11:31:25 -07002914
2915 dir->class = class;
2916 kobject_init(&dir->kobj, &class_dir_ktype);
2917
Kay Sievers6b6e39a2010-11-15 23:13:18 +01002918 dir->kobj.kset = &class->p->glue_dirs;
Eric W. Biedermanbc451f22010-03-30 11:31:25 -07002919
2920 retval = kobject_add(&dir->kobj, parent_kobj, "%s", class->name);
2921 if (retval < 0) {
2922 kobject_put(&dir->kobj);
Tetsuo Handa84d0c272018-05-07 19:10:31 +09002923 return ERR_PTR(retval);
Eric W. Biedermanbc451f22010-03-30 11:31:25 -07002924 }
2925 return &dir->kobj;
2926}
2927
Yijing Wange4a60d12014-11-07 12:05:49 +08002928static DEFINE_MUTEX(gdp_mutex);
Eric W. Biedermanbc451f22010-03-30 11:31:25 -07002929
Kay Sieversda231fd2007-11-21 17:29:15 +01002930static struct kobject *get_device_parent(struct device *dev,
2931 struct device *parent)
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +01002932{
Kay Sievers86406242007-03-14 03:25:56 +01002933 if (dev->class) {
2934 struct kobject *kobj = NULL;
2935 struct kobject *parent_kobj;
2936 struct kobject *k;
2937
Randy Dunlapead454f2010-09-24 14:36:49 -07002938#ifdef CONFIG_BLOCK
Kay Sievers39aba962010-09-04 22:33:14 -07002939 /* block disks show up in /sys/block */
Andi Kleene52eec12010-09-08 16:54:17 +02002940 if (sysfs_deprecated && dev->class == &block_class) {
Kay Sievers39aba962010-09-04 22:33:14 -07002941 if (parent && parent->class == &block_class)
2942 return &parent->kobj;
Kay Sievers6b6e39a2010-11-15 23:13:18 +01002943 return &block_class.p->subsys.kobj;
Kay Sievers39aba962010-09-04 22:33:14 -07002944 }
Randy Dunlapead454f2010-09-24 14:36:49 -07002945#endif
Andi Kleene52eec12010-09-08 16:54:17 +02002946
Kay Sievers86406242007-03-14 03:25:56 +01002947 /*
2948 * If we have no parent, we live in "virtual".
Kay Sievers0f4dafc2007-12-19 01:40:42 +01002949 * Class-devices with a non class-device as parent, live
2950 * in a "glue" directory to prevent namespace collisions.
Kay Sievers86406242007-03-14 03:25:56 +01002951 */
2952 if (parent == NULL)
2953 parent_kobj = virtual_device_parent(dev);
Eric W. Biederman24b14422010-07-24 22:43:35 -07002954 else if (parent->class && !dev->class->ns_type)
Kay Sievers86406242007-03-14 03:25:56 +01002955 return &parent->kobj;
2956 else
2957 parent_kobj = &parent->kobj;
2958
Tejun Heo77d3d7c2010-02-05 17:57:02 +09002959 mutex_lock(&gdp_mutex);
2960
Kay Sievers86406242007-03-14 03:25:56 +01002961 /* find our class-directory at the parent and reference it */
Kay Sievers6b6e39a2010-11-15 23:13:18 +01002962 spin_lock(&dev->class->p->glue_dirs.list_lock);
2963 list_for_each_entry(k, &dev->class->p->glue_dirs.list, entry)
Kay Sievers86406242007-03-14 03:25:56 +01002964 if (k->parent == parent_kobj) {
2965 kobj = kobject_get(k);
2966 break;
2967 }
Kay Sievers6b6e39a2010-11-15 23:13:18 +01002968 spin_unlock(&dev->class->p->glue_dirs.list_lock);
Tejun Heo77d3d7c2010-02-05 17:57:02 +09002969 if (kobj) {
2970 mutex_unlock(&gdp_mutex);
Kay Sievers86406242007-03-14 03:25:56 +01002971 return kobj;
Tejun Heo77d3d7c2010-02-05 17:57:02 +09002972 }
Kay Sievers86406242007-03-14 03:25:56 +01002973
2974 /* or create a new class-directory at the parent device */
Eric W. Biedermanbc451f22010-03-30 11:31:25 -07002975 k = class_dir_create_and_add(dev->class, parent_kobj);
Kay Sievers0f4dafc2007-12-19 01:40:42 +01002976 /* do not emit an uevent for this simple "glue" directory */
Tejun Heo77d3d7c2010-02-05 17:57:02 +09002977 mutex_unlock(&gdp_mutex);
Greg Kroah-Hartman43968d22007-11-05 22:24:43 -08002978 return k;
Kay Sievers86406242007-03-14 03:25:56 +01002979 }
2980
Kay Sieversca22e562011-12-14 14:29:38 -08002981 /* subsystems can specify a default root directory for their devices */
2982 if (!parent && dev->bus && dev->bus->dev_root)
2983 return &dev->bus->dev_root->kobj;
2984
Kay Sievers86406242007-03-14 03:25:56 +01002985 if (parent)
Cornelia Huckc744aeae2007-01-08 20:16:44 +01002986 return &parent->kobj;
2987 return NULL;
2988}
Kay Sieversda231fd2007-11-21 17:29:15 +01002989
Ming Leicebf8fd2016-07-10 19:27:36 +08002990static inline bool live_in_glue_dir(struct kobject *kobj,
2991 struct device *dev)
2992{
2993 if (!kobj || !dev->class ||
2994 kobj->kset != &dev->class->p->glue_dirs)
2995 return false;
2996 return true;
2997}
2998
2999static inline struct kobject *get_glue_dir(struct device *dev)
3000{
3001 return dev->kobj.parent;
3002}
3003
3004/*
3005 * make sure cleaning up dir as the last step, we need to make
3006 * sure .release handler of kobject is run with holding the
3007 * global lock
3008 */
Cornelia Huck63b69712008-01-21 16:09:44 +01003009static void cleanup_glue_dir(struct device *dev, struct kobject *glue_dir)
Kay Sieversda231fd2007-11-21 17:29:15 +01003010{
Muchun Songac434322019-07-27 11:21:22 +08003011 unsigned int ref;
3012
Kay Sievers0f4dafc2007-12-19 01:40:42 +01003013 /* see if we live in a "glue" directory */
Ming Leicebf8fd2016-07-10 19:27:36 +08003014 if (!live_in_glue_dir(glue_dir, dev))
Kay Sieversda231fd2007-11-21 17:29:15 +01003015 return;
3016
Yijing Wange4a60d12014-11-07 12:05:49 +08003017 mutex_lock(&gdp_mutex);
Muchun Songac434322019-07-27 11:21:22 +08003018 /**
3019 * There is a race condition between removing glue directory
3020 * and adding a new device under the glue directory.
3021 *
3022 * CPU1: CPU2:
3023 *
3024 * device_add()
3025 * get_device_parent()
3026 * class_dir_create_and_add()
3027 * kobject_add_internal()
3028 * create_dir() // create glue_dir
3029 *
3030 * device_add()
3031 * get_device_parent()
3032 * kobject_get() // get glue_dir
3033 *
3034 * device_del()
3035 * cleanup_glue_dir()
3036 * kobject_del(glue_dir)
3037 *
3038 * kobject_add()
3039 * kobject_add_internal()
3040 * create_dir() // in glue_dir
3041 * sysfs_create_dir_ns()
3042 * kernfs_create_dir_ns(sd)
3043 *
3044 * sysfs_remove_dir() // glue_dir->sd=NULL
3045 * sysfs_put() // free glue_dir->sd
3046 *
3047 * // sd is freed
3048 * kernfs_new_node(sd)
3049 * kernfs_get(glue_dir)
3050 * kernfs_add_one()
3051 * kernfs_put()
3052 *
3053 * Before CPU1 remove last child device under glue dir, if CPU2 add
3054 * a new device under glue dir, the glue_dir kobject reference count
3055 * will be increase to 2 in kobject_get(k). And CPU2 has been called
3056 * kernfs_create_dir_ns(). Meanwhile, CPU1 call sysfs_remove_dir()
3057 * and sysfs_put(). This result in glue_dir->sd is freed.
3058 *
3059 * Then the CPU2 will see a stale "empty" but still potentially used
3060 * glue dir around in kernfs_new_node().
3061 *
3062 * In order to avoid this happening, we also should make sure that
3063 * kernfs_node for glue_dir is released in CPU1 only when refcount
3064 * for glue_dir kobj is 1.
3065 */
3066 ref = kref_read(&glue_dir->kref);
3067 if (!kobject_has_children(glue_dir) && !--ref)
Benjamin Herrenschmidt726e4102018-07-10 10:29:10 +10003068 kobject_del(glue_dir);
Kay Sievers0f4dafc2007-12-19 01:40:42 +01003069 kobject_put(glue_dir);
Yijing Wange4a60d12014-11-07 12:05:49 +08003070 mutex_unlock(&gdp_mutex);
Kay Sieversda231fd2007-11-21 17:29:15 +01003071}
Cornelia Huck63b69712008-01-21 16:09:44 +01003072
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07003073static int device_add_class_symlinks(struct device *dev)
3074{
Benjamin Herrenschmidt5590f312015-02-18 11:25:18 +11003075 struct device_node *of_node = dev_of_node(dev);
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07003076 int error;
3077
Benjamin Herrenschmidt5590f312015-02-18 11:25:18 +11003078 if (of_node) {
Rob Herring0c3c2342017-10-04 14:04:01 -05003079 error = sysfs_create_link(&dev->kobj, of_node_kobj(of_node), "of_node");
Benjamin Herrenschmidt5590f312015-02-18 11:25:18 +11003080 if (error)
3081 dev_warn(dev, "Error %d creating of_node link\n",error);
3082 /* An error here doesn't warrant bringing down the device */
3083 }
3084
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07003085 if (!dev->class)
3086 return 0;
Kay Sieversda231fd2007-11-21 17:29:15 +01003087
Greg Kroah-Hartman1fbfee62008-05-28 09:28:39 -07003088 error = sysfs_create_link(&dev->kobj,
Kay Sievers6b6e39a2010-11-15 23:13:18 +01003089 &dev->class->p->subsys.kobj,
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07003090 "subsystem");
3091 if (error)
Benjamin Herrenschmidt5590f312015-02-18 11:25:18 +11003092 goto out_devnode;
Kay Sieversda231fd2007-11-21 17:29:15 +01003093
Greg Kroah-Hartman4e886c22008-01-27 12:12:43 -08003094 if (dev->parent && device_is_not_partition(dev)) {
Dmitry Torokhov4f01a752007-09-18 22:46:50 -07003095 error = sysfs_create_link(&dev->kobj, &dev->parent->kobj,
3096 "device");
3097 if (error)
Kay Sievers39aba962010-09-04 22:33:14 -07003098 goto out_subsys;
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07003099 }
Kay Sievers39aba962010-09-04 22:33:14 -07003100
Randy Dunlapead454f2010-09-24 14:36:49 -07003101#ifdef CONFIG_BLOCK
Kay Sievers39aba962010-09-04 22:33:14 -07003102 /* /sys/block has directories and does not need symlinks */
Andi Kleene52eec12010-09-08 16:54:17 +02003103 if (sysfs_deprecated && dev->class == &block_class)
Kay Sievers39aba962010-09-04 22:33:14 -07003104 return 0;
Randy Dunlapead454f2010-09-24 14:36:49 -07003105#endif
Kay Sievers39aba962010-09-04 22:33:14 -07003106
3107 /* link in the class directory pointing to the device */
Kay Sievers6b6e39a2010-11-15 23:13:18 +01003108 error = sysfs_create_link(&dev->class->p->subsys.kobj,
Kay Sievers39aba962010-09-04 22:33:14 -07003109 &dev->kobj, dev_name(dev));
3110 if (error)
3111 goto out_device;
3112
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07003113 return 0;
3114
Kay Sievers39aba962010-09-04 22:33:14 -07003115out_device:
3116 sysfs_remove_link(&dev->kobj, "device");
Kay Sieversda231fd2007-11-21 17:29:15 +01003117
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07003118out_subsys:
3119 sysfs_remove_link(&dev->kobj, "subsystem");
Benjamin Herrenschmidt5590f312015-02-18 11:25:18 +11003120out_devnode:
3121 sysfs_remove_link(&dev->kobj, "of_node");
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07003122 return error;
3123}
3124
3125static void device_remove_class_symlinks(struct device *dev)
3126{
Benjamin Herrenschmidt5590f312015-02-18 11:25:18 +11003127 if (dev_of_node(dev))
3128 sysfs_remove_link(&dev->kobj, "of_node");
3129
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07003130 if (!dev->class)
3131 return;
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))
Kay Sieversda231fd2007-11-21 17:29:15 +01003134 sysfs_remove_link(&dev->kobj, "device");
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07003135 sysfs_remove_link(&dev->kobj, "subsystem");
Randy Dunlapead454f2010-09-24 14:36:49 -07003136#ifdef CONFIG_BLOCK
Andi Kleene52eec12010-09-08 16:54:17 +02003137 if (sysfs_deprecated && dev->class == &block_class)
Kay Sievers39aba962010-09-04 22:33:14 -07003138 return;
Randy Dunlapead454f2010-09-24 14:36:49 -07003139#endif
Kay Sievers6b6e39a2010-11-15 23:13:18 +01003140 sysfs_delete_link(&dev->class->p->subsys.kobj, &dev->kobj, dev_name(dev));
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07003141}
3142
Linus Torvalds1da177e2005-04-16 15:20:36 -07003143/**
Stephen Rothwell413c2392008-05-30 10:16:40 +10003144 * dev_set_name - set a device name
3145 * @dev: device
Randy Dunlap46232362008-06-04 21:40:43 -07003146 * @fmt: format string for the device's name
Stephen Rothwell413c2392008-05-30 10:16:40 +10003147 */
3148int dev_set_name(struct device *dev, const char *fmt, ...)
3149{
3150 va_list vargs;
Kay Sievers1fa5ae82009-01-25 15:17:37 +01003151 int err;
Stephen Rothwell413c2392008-05-30 10:16:40 +10003152
3153 va_start(vargs, fmt);
Kay Sievers1fa5ae82009-01-25 15:17:37 +01003154 err = kobject_set_name_vargs(&dev->kobj, fmt, vargs);
Stephen Rothwell413c2392008-05-30 10:16:40 +10003155 va_end(vargs);
Kay Sievers1fa5ae82009-01-25 15:17:37 +01003156 return err;
Stephen Rothwell413c2392008-05-30 10:16:40 +10003157}
3158EXPORT_SYMBOL_GPL(dev_set_name);
3159
3160/**
Dan Williamse105b8b2008-04-21 10:51:07 -07003161 * device_to_dev_kobj - select a /sys/dev/ directory for the device
3162 * @dev: device
3163 *
3164 * By default we select char/ for new entries. Setting class->dev_obj
3165 * to NULL prevents an entry from being created. class->dev_kobj must
3166 * be set (or cleared) before any devices are registered to the class
3167 * otherwise device_create_sys_dev_entry() and
Peter Korsgaard0d4e293c2012-04-17 12:12:57 +02003168 * device_remove_sys_dev_entry() will disagree about the presence of
3169 * the link.
Dan Williamse105b8b2008-04-21 10:51:07 -07003170 */
3171static struct kobject *device_to_dev_kobj(struct device *dev)
3172{
3173 struct kobject *kobj;
3174
3175 if (dev->class)
3176 kobj = dev->class->dev_kobj;
3177 else
3178 kobj = sysfs_dev_char_kobj;
3179
3180 return kobj;
3181}
3182
3183static int device_create_sys_dev_entry(struct device *dev)
3184{
3185 struct kobject *kobj = device_to_dev_kobj(dev);
3186 int error = 0;
3187 char devt_str[15];
3188
3189 if (kobj) {
3190 format_dev_t(devt_str, dev->devt);
3191 error = sysfs_create_link(kobj, &dev->kobj, devt_str);
3192 }
3193
3194 return error;
3195}
3196
3197static void device_remove_sys_dev_entry(struct device *dev)
3198{
3199 struct kobject *kobj = device_to_dev_kobj(dev);
3200 char devt_str[15];
3201
3202 if (kobj) {
3203 format_dev_t(devt_str, dev->devt);
3204 sysfs_remove_link(kobj, devt_str);
3205 }
3206}
3207
Shaokun Zhang46d3a032018-07-15 18:08:56 +08003208static int device_private_init(struct device *dev)
Greg Kroah-Hartmanb4028432009-05-11 14:16:57 -07003209{
3210 dev->p = kzalloc(sizeof(*dev->p), GFP_KERNEL);
3211 if (!dev->p)
3212 return -ENOMEM;
3213 dev->p->device = dev;
3214 klist_init(&dev->p->klist_children, klist_children_get,
3215 klist_children_put);
Greg Kroah-Hartmanef8a3fd2012-03-08 12:17:22 -08003216 INIT_LIST_HEAD(&dev->p->deferred_probe);
Greg Kroah-Hartmanb4028432009-05-11 14:16:57 -07003217 return 0;
3218}
3219
Dan Williamse105b8b2008-04-21 10:51:07 -07003220/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08003221 * device_add - add device to device hierarchy.
3222 * @dev: device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003223 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08003224 * This is part 2 of device_register(), though may be called
3225 * separately _iff_ device_initialize() has been called separately.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003226 *
Cornelia Huck57394112008-09-03 18:26:40 +02003227 * This adds @dev to the kobject hierarchy via kobject_add(), adds it
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08003228 * to the global and sibling lists for the device, then
3229 * adds it to the other relevant subsystems of the driver model.
Cornelia Huck57394112008-09-03 18:26:40 +02003230 *
Alan Sternb10d5ef2012-01-17 11:39:00 -05003231 * Do not call this routine or device_register() more than once for
3232 * any device structure. The driver model core is not designed to work
3233 * with devices that get unregistered and then spring back to life.
3234 * (Among other things, it's very hard to guarantee that all references
3235 * to the previous incarnation of @dev have been dropped.) Allocate
3236 * and register a fresh new struct device instead.
3237 *
Cornelia Huck57394112008-09-03 18:26:40 +02003238 * NOTE: _Never_ directly free @dev after calling this function, even
3239 * if it returned an error! Always use put_device() to give up your
3240 * reference instead.
Borislav Petkovaffada72019-04-18 19:41:56 +02003241 *
3242 * Rule of thumb is: if device_add() succeeds, you should call
3243 * device_del() when you want to get rid of it. If device_add() has
3244 * *not* succeeded, use *only* put_device() to drop the reference
3245 * count.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246 */
3247int device_add(struct device *dev)
3248{
Viresh Kumar35dbf4e2017-03-17 12:24:22 +05303249 struct device *parent;
Kay Sieversca22e562011-12-14 14:29:38 -08003250 struct kobject *kobj;
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02003251 struct class_interface *class_intf;
Saravana Kannan5f5377e2020-05-14 22:34:58 -07003252 int error = -EINVAL;
Ming Leicebf8fd2016-07-10 19:27:36 +08003253 struct kobject *glue_dir = NULL;
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01003254
Linus Torvalds1da177e2005-04-16 15:20:36 -07003255 dev = get_device(dev);
Greg Kroah-Hartmanc906a482008-05-30 10:45:12 -07003256 if (!dev)
3257 goto done;
3258
Greg Kroah-Hartmanfb069a52008-12-16 12:23:36 -08003259 if (!dev->p) {
Greg Kroah-Hartmanb4028432009-05-11 14:16:57 -07003260 error = device_private_init(dev);
3261 if (error)
3262 goto done;
Greg Kroah-Hartmanfb069a52008-12-16 12:23:36 -08003263 }
Greg Kroah-Hartmanfb069a52008-12-16 12:23:36 -08003264
Kay Sievers1fa5ae82009-01-25 15:17:37 +01003265 /*
3266 * for statically allocated devices, which should all be converted
3267 * some day, we need to initialize the name. We prevent reading back
3268 * the name, and force the use of dev_name()
3269 */
3270 if (dev->init_name) {
Greg Kroah-Hartmanacc0e902009-06-02 15:39:55 -07003271 dev_set_name(dev, "%s", dev->init_name);
Kay Sievers1fa5ae82009-01-25 15:17:37 +01003272 dev->init_name = NULL;
3273 }
Greg Kroah-Hartmanc906a482008-05-30 10:45:12 -07003274
Kay Sieversca22e562011-12-14 14:29:38 -08003275 /* subsystems can specify simple device enumeration */
3276 if (!dev_name(dev) && dev->bus && dev->bus->dev_name)
3277 dev_set_name(dev, "%s%u", dev->bus->dev_name, dev->id);
3278
Thomas Gleixnere6309e72009-12-10 19:32:49 +00003279 if (!dev_name(dev)) {
3280 error = -EINVAL;
Kay Sievers5c8563d2009-05-28 14:24:07 -07003281 goto name_error;
Thomas Gleixnere6309e72009-12-10 19:32:49 +00003282 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01003284 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
Greg Kroah-Hartmanc205ef42006-08-07 22:19:37 -07003285
Linus Torvalds1da177e2005-04-16 15:20:36 -07003286 parent = get_device(dev->parent);
Kay Sieversca22e562011-12-14 14:29:38 -08003287 kobj = get_device_parent(dev, parent);
Tetsuo Handa84d0c272018-05-07 19:10:31 +09003288 if (IS_ERR(kobj)) {
3289 error = PTR_ERR(kobj);
3290 goto parent_error;
3291 }
Kay Sieversca22e562011-12-14 14:29:38 -08003292 if (kobj)
3293 dev->kobj.parent = kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003294
Yinghai Lu0d358f22008-02-19 03:20:41 -08003295 /* use parent numa_node */
Zhen Lei56f2de82015-08-25 12:08:22 +08003296 if (parent && (dev_to_node(dev) == NUMA_NO_NODE))
Yinghai Lu0d358f22008-02-19 03:20:41 -08003297 set_dev_node(dev, dev_to_node(parent));
3298
Linus Torvalds1da177e2005-04-16 15:20:36 -07003299 /* first, register with generic layer. */
Kay Sievers8a577ff2009-04-18 15:05:45 -07003300 /* we require the name to be set before, and pass NULL */
3301 error = kobject_add(&dev->kobj, dev->kobj.parent, NULL);
Ming Leicebf8fd2016-07-10 19:27:36 +08003302 if (error) {
3303 glue_dir = get_glue_dir(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003304 goto Error;
Ming Leicebf8fd2016-07-10 19:27:36 +08003305 }
Kay Sieversa7fd6702005-10-01 14:49:43 +02003306
Brian Walsh37022642006-08-14 22:43:19 -07003307 /* notify platform of device entry */
Rafael J. Wysockib2ebd9d2021-07-12 19:28:16 +02003308 device_platform_notify(dev);
Brian Walsh37022642006-08-14 22:43:19 -07003309
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07003310 error = device_create_file(dev, &dev_attr_uevent);
Cornelia Hucka306eea2006-09-22 11:37:13 +02003311 if (error)
3312 goto attrError;
Kay Sieversa7fd6702005-10-01 14:49:43 +02003313
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07003314 error = device_add_class_symlinks(dev);
3315 if (error)
3316 goto SymlinkError;
Cornelia Huckdc0afa82007-07-09 11:39:18 -07003317 error = device_add_attrs(dev);
3318 if (error)
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07003319 goto AttrsError;
Cornelia Huckdc0afa82007-07-09 11:39:18 -07003320 error = bus_add_device(dev);
3321 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003322 goto BusError;
Alan Stern3b98aea2008-08-07 13:06:12 -04003323 error = dpm_sysfs_add(dev);
Rafael J. Wysocki57eee3d2008-03-12 00:59:38 +01003324 if (error)
Alan Stern3b98aea2008-08-07 13:06:12 -04003325 goto DPMError;
3326 device_pm_add(dev);
Alan Sternec0676ee2008-12-05 14:10:31 -05003327
Sergey Klyaus0cd75042014-10-08 11:31:54 +04003328 if (MAJOR(dev->devt)) {
3329 error = device_create_file(dev, &dev_attr_dev);
3330 if (error)
3331 goto DevAttrError;
3332
3333 error = device_create_sys_dev_entry(dev);
3334 if (error)
3335 goto SysEntryError;
3336
3337 devtmpfs_create_node(dev);
3338 }
3339
Alan Sternec0676ee2008-12-05 14:10:31 -05003340 /* Notify clients of device addition. This call must come
majianpeng268863f2012-01-11 15:12:06 +00003341 * after dpm_sysfs_add() and before kobject_uevent().
Alan Sternec0676ee2008-12-05 14:10:31 -05003342 */
3343 if (dev->bus)
3344 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
3345 BUS_NOTIFY_ADD_DEVICE, dev);
3346
Cornelia Huck83b5fb4c2007-03-29 11:12:11 +02003347 kobject_uevent(&dev->kobj, KOBJ_ADD);
Saravana Kannan372a67c2019-09-04 14:11:20 -07003348
Saravana Kannane2ae9bc2019-09-04 14:11:21 -07003349 /*
3350 * Check if any of the other devices (consumers) have been waiting for
3351 * this device (supplier) to be added so that they can create a device
3352 * link to it.
3353 *
3354 * This needs to happen after device_pm_add() because device_link_add()
3355 * requires the supplier be registered before it's called.
3356 *
Saravana Kannan2cd38fd2020-05-19 20:48:21 -07003357 * But this also needs to happen before bus_probe_device() to make sure
Saravana Kannane2ae9bc2019-09-04 14:11:21 -07003358 * waiting consumers can link to it before the driver is bound to the
3359 * device and the driver sync_state callback is called for this device.
3360 */
Saravana Kannan2cd38fd2020-05-19 20:48:21 -07003361 if (dev->fwnode && !dev->fwnode->dev) {
3362 dev->fwnode->dev = dev;
Saravana Kannan5f5377e2020-05-14 22:34:58 -07003363 fw_devlink_link_device(dev);
Saravana Kannan03324502019-10-28 15:00:24 -07003364 }
Saravana Kannane2ae9bc2019-09-04 14:11:21 -07003365
Alan Stern2023c612009-07-30 15:27:18 -04003366 bus_probe_device(dev);
Saravana Kannand46f3e32021-04-01 21:03:41 -07003367
3368 /*
3369 * If all driver registration is done and a newly added device doesn't
3370 * match with any driver, don't block its consumers from probing in
3371 * case the consumer device is able to operate without this supplier.
3372 */
3373 if (dev->fwnode && fw_devlink_drv_reg_done && !dev->can_match)
3374 fw_devlink_unblock_consumers(dev);
3375
Linus Torvalds1da177e2005-04-16 15:20:36 -07003376 if (parent)
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08003377 klist_add_tail(&dev->p->knode_parent,
3378 &parent->p->klist_children);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003379
Greg Kroah-Hartman5d9fd162006-06-22 17:17:32 -07003380 if (dev->class) {
Kay Sieversca22e562011-12-14 14:29:38 -08003381 mutex_lock(&dev->class->p->mutex);
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02003382 /* tie the class to the device */
Wei Yang570d0202019-01-18 10:34:59 +08003383 klist_add_tail(&dev->p->knode_class,
Kay Sievers6b6e39a2010-11-15 23:13:18 +01003384 &dev->class->p->klist_devices);
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02003385
3386 /* notify any interfaces that the device is here */
Greg Kroah-Hartman184f1f72008-05-28 09:28:39 -07003387 list_for_each_entry(class_intf,
Kay Sieversca22e562011-12-14 14:29:38 -08003388 &dev->class->p->interfaces, node)
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02003389 if (class_intf->add_dev)
3390 class_intf->add_dev(dev, class_intf);
Kay Sieversca22e562011-12-14 14:29:38 -08003391 mutex_unlock(&dev->class->p->mutex);
Greg Kroah-Hartman5d9fd162006-06-22 17:17:32 -07003392 }
Greg Kroah-Hartmanc906a482008-05-30 10:45:12 -07003393done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003394 put_device(dev);
3395 return error;
Sergey Klyaus0cd75042014-10-08 11:31:54 +04003396 SysEntryError:
3397 if (MAJOR(dev->devt))
3398 device_remove_file(dev, &dev_attr_dev);
3399 DevAttrError:
3400 device_pm_remove(dev);
3401 dpm_sysfs_remove(dev);
Alan Stern3b98aea2008-08-07 13:06:12 -04003402 DPMError:
Rafael J. Wysocki57eee3d2008-03-12 00:59:38 +01003403 bus_remove_device(dev);
3404 BusError:
James Simmons82f0cf92007-02-21 17:44:51 +00003405 device_remove_attrs(dev);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07003406 AttrsError:
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07003407 device_remove_class_symlinks(dev);
3408 SymlinkError:
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07003409 device_remove_file(dev, &dev_attr_uevent);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07003410 attrError:
Rafael J. Wysockib2ebd9d2021-07-12 19:28:16 +02003411 device_platform_notify_remove(dev);
Kay Sievers312c0042005-11-16 09:00:00 +01003412 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
Ming Leicebf8fd2016-07-10 19:27:36 +08003413 glue_dir = get_glue_dir(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003414 kobject_del(&dev->kobj);
3415 Error:
Ming Leicebf8fd2016-07-10 19:27:36 +08003416 cleanup_glue_dir(dev, glue_dir);
Tetsuo Handa84d0c272018-05-07 19:10:31 +09003417parent_error:
Markus Elfring5f0163a2015-02-05 11:48:26 +01003418 put_device(parent);
Kay Sievers5c8563d2009-05-28 14:24:07 -07003419name_error:
3420 kfree(dev->p);
3421 dev->p = NULL;
Greg Kroah-Hartmanc906a482008-05-30 10:45:12 -07003422 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003423}
David Graham White86df2682013-07-21 20:41:14 -04003424EXPORT_SYMBOL_GPL(device_add);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003425
Linus Torvalds1da177e2005-04-16 15:20:36 -07003426/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08003427 * device_register - register a device with the system.
3428 * @dev: pointer to the device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07003429 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08003430 * This happens in two clean steps - initialize the device
3431 * and add it to the system. The two steps can be called
3432 * separately, but this is the easiest and most common.
3433 * I.e. you should only call the two helpers separately if
3434 * have a clearly defined need to use and refcount the device
3435 * before it is added to the hierarchy.
Cornelia Huck57394112008-09-03 18:26:40 +02003436 *
Alan Sternb10d5ef2012-01-17 11:39:00 -05003437 * For more information, see the kerneldoc for device_initialize()
3438 * and device_add().
3439 *
Cornelia Huck57394112008-09-03 18:26:40 +02003440 * NOTE: _Never_ directly free @dev after calling this function, even
3441 * if it returned an error! Always use put_device() to give up the
3442 * reference initialized in this function instead.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003443 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003444int device_register(struct device *dev)
3445{
3446 device_initialize(dev);
3447 return device_add(dev);
3448}
David Graham White86df2682013-07-21 20:41:14 -04003449EXPORT_SYMBOL_GPL(device_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003450
Linus Torvalds1da177e2005-04-16 15:20:36 -07003451/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08003452 * get_device - increment reference count for device.
3453 * @dev: device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003454 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08003455 * This simply forwards the call to kobject_get(), though
3456 * we do take care to provide for the case that we get a NULL
3457 * pointer passed in.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003458 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08003459struct device *get_device(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003460{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +02003461 return dev ? kobj_to_dev(kobject_get(&dev->kobj)) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003462}
David Graham White86df2682013-07-21 20:41:14 -04003463EXPORT_SYMBOL_GPL(get_device);
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 * put_device - decrement reference count.
3467 * @dev: device in question.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003468 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08003469void put_device(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003470{
Kay Sieversedfaa7c2007-05-21 22:08:01 +02003471 /* might_sleep(); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003472 if (dev)
3473 kobject_put(&dev->kobj);
3474}
David Graham White86df2682013-07-21 20:41:14 -04003475EXPORT_SYMBOL_GPL(put_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003476
Dan Williams00289cd2019-07-17 18:07:53 -07003477bool kill_device(struct device *dev)
3478{
3479 /*
3480 * Require the device lock and set the "dead" flag to guarantee that
3481 * the update behavior is consistent with the other bitfields near
3482 * it and that we cannot have an asynchronous probe routine trying
3483 * to run while we are tearing out the bus/class/sysfs from
3484 * underneath the device.
3485 */
Julian Wiedmann8c60a142021-05-12 16:10:54 +02003486 device_lock_assert(dev);
Dan Williams00289cd2019-07-17 18:07:53 -07003487
3488 if (dev->p->dead)
3489 return false;
3490 dev->p->dead = true;
3491 return true;
3492}
3493EXPORT_SYMBOL_GPL(kill_device);
3494
Linus Torvalds1da177e2005-04-16 15:20:36 -07003495/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08003496 * device_del - delete device from system.
3497 * @dev: device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003498 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08003499 * This is the first part of the device unregistration
3500 * sequence. This removes the device from the lists we control
3501 * from here, has it removed from the other driver model
3502 * subsystems it was added to in device_add(), and removes it
3503 * from the kobject hierarchy.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003504 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08003505 * NOTE: this should be called manually _iff_ device_add() was
3506 * also called manually.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003507 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08003508void device_del(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003509{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08003510 struct device *parent = dev->parent;
Ming Leicebf8fd2016-07-10 19:27:36 +08003511 struct kobject *glue_dir = NULL;
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02003512 struct class_interface *class_intf;
Oliver Neukumb8530012020-09-16 21:15:44 +02003513 unsigned int noio_flag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003514
Alexander Duyck3451a492019-01-22 10:39:10 -08003515 device_lock(dev);
Dan Williams00289cd2019-07-17 18:07:53 -07003516 kill_device(dev);
Alexander Duyck3451a492019-01-22 10:39:10 -08003517 device_unlock(dev);
3518
Saravana Kannan372a67c2019-09-04 14:11:20 -07003519 if (dev->fwnode && dev->fwnode->dev == dev)
3520 dev->fwnode->dev = NULL;
3521
Alan Sternec0676ee2008-12-05 14:10:31 -05003522 /* Notify clients of device removal. This call must come
3523 * before dpm_sysfs_remove().
3524 */
Oliver Neukumb8530012020-09-16 21:15:44 +02003525 noio_flag = memalloc_noio_save();
Alan Sternec0676ee2008-12-05 14:10:31 -05003526 if (dev->bus)
3527 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
3528 BUS_NOTIFY_DEL_DEVICE, dev);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01003529
Alan Stern3b98aea2008-08-07 13:06:12 -04003530 dpm_sysfs_remove(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003531 if (parent)
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08003532 klist_del(&dev->p->knode_parent);
Dan Williamse105b8b2008-04-21 10:51:07 -07003533 if (MAJOR(dev->devt)) {
Kay Sievers2b2af542009-04-30 15:23:42 +02003534 devtmpfs_delete_node(dev);
Dan Williamse105b8b2008-04-21 10:51:07 -07003535 device_remove_sys_dev_entry(dev);
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07003536 device_remove_file(dev, &dev_attr_dev);
Dan Williamse105b8b2008-04-21 10:51:07 -07003537 }
Kay Sieversb9d9c822006-06-15 15:31:56 +02003538 if (dev->class) {
Kay Sieversda231fd2007-11-21 17:29:15 +01003539 device_remove_class_symlinks(dev);
Kay Sievers99ef3ef2006-09-14 11:23:28 +02003540
Kay Sieversca22e562011-12-14 14:29:38 -08003541 mutex_lock(&dev->class->p->mutex);
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02003542 /* notify any interfaces that the device is now gone */
Greg Kroah-Hartman184f1f72008-05-28 09:28:39 -07003543 list_for_each_entry(class_intf,
Kay Sieversca22e562011-12-14 14:29:38 -08003544 &dev->class->p->interfaces, node)
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02003545 if (class_intf->remove_dev)
3546 class_intf->remove_dev(dev, class_intf);
3547 /* remove the device from the class list */
Wei Yang570d0202019-01-18 10:34:59 +08003548 klist_del(&dev->p->knode_class);
Kay Sieversca22e562011-12-14 14:29:38 -08003549 mutex_unlock(&dev->class->p->mutex);
Kay Sieversb9d9c822006-06-15 15:31:56 +02003550 }
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07003551 device_remove_file(dev, &dev_attr_uevent);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07003552 device_remove_attrs(dev);
Benjamin Herrenschmidt28953532006-11-08 19:46:14 -08003553 bus_remove_device(dev);
LongX Zhang4b6d1f122012-10-25 00:21:28 +02003554 device_pm_remove(dev);
Grant Likelyd1c34142012-03-05 08:47:41 -07003555 driver_deferred_probe_del(dev);
Rafael J. Wysockib2ebd9d2021-07-12 19:28:16 +02003556 device_platform_notify_remove(dev);
Lukas Wunner478573c2016-07-28 02:25:41 +02003557 device_remove_properties(dev);
Jeffy Chen2ec16152017-10-20 20:01:01 +08003558 device_links_purge(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003559
Joerg Roedel599bad32014-09-30 13:02:02 +02003560 if (dev->bus)
3561 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
3562 BUS_NOTIFY_REMOVED_DEVICE, dev);
Kay Sievers312c0042005-11-16 09:00:00 +01003563 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
Ming Leicebf8fd2016-07-10 19:27:36 +08003564 glue_dir = get_glue_dir(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003565 kobject_del(&dev->kobj);
Ming Leicebf8fd2016-07-10 19:27:36 +08003566 cleanup_glue_dir(dev, glue_dir);
Oliver Neukumb8530012020-09-16 21:15:44 +02003567 memalloc_noio_restore(noio_flag);
Kay Sieversda231fd2007-11-21 17:29:15 +01003568 put_device(parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003569}
David Graham White86df2682013-07-21 20:41:14 -04003570EXPORT_SYMBOL_GPL(device_del);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003571
3572/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08003573 * device_unregister - unregister device from system.
3574 * @dev: device going away.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003575 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08003576 * We do this in two parts, like we do device_register(). First,
3577 * we remove it from all the subsystems with device_del(), then
3578 * we decrement the reference count via put_device(). If that
3579 * is the final reference count, the device will be cleaned up
3580 * via device_release() above. Otherwise, the structure will
3581 * stick around until the final reference to the device is dropped.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003582 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08003583void device_unregister(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003584{
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01003585 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003586 device_del(dev);
3587 put_device(dev);
3588}
David Graham White86df2682013-07-21 20:41:14 -04003589EXPORT_SYMBOL_GPL(device_unregister);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003590
Andy Shevchenko3d060ae2015-07-27 18:04:00 +03003591static struct device *prev_device(struct klist_iter *i)
3592{
3593 struct klist_node *n = klist_prev(i);
3594 struct device *dev = NULL;
3595 struct device_private *p;
3596
3597 if (n) {
3598 p = to_device_private_parent(n);
3599 dev = p->device;
3600 }
3601 return dev;
3602}
3603
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08003604static struct device *next_device(struct klist_iter *i)
mochel@digitalimplant.org36239572005-03-24 19:08:30 -08003605{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08003606 struct klist_node *n = klist_next(i);
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08003607 struct device *dev = NULL;
3608 struct device_private *p;
3609
3610 if (n) {
3611 p = to_device_private_parent(n);
3612 dev = p->device;
3613 }
3614 return dev;
mochel@digitalimplant.org36239572005-03-24 19:08:30 -08003615}
3616
Linus Torvalds1da177e2005-04-16 15:20:36 -07003617/**
Kay Sieverse454cea2009-09-18 23:01:12 +02003618 * device_get_devnode - path of device node file
Kay Sievers6fcf53a2009-04-30 15:23:42 +02003619 * @dev: device
Kay Sieverse454cea2009-09-18 23:01:12 +02003620 * @mode: returned file access mode
Kay Sievers3c2670e2013-04-06 09:56:00 -07003621 * @uid: returned file owner
3622 * @gid: returned file group
Kay Sievers6fcf53a2009-04-30 15:23:42 +02003623 * @tmp: possibly allocated string
3624 *
3625 * Return the relative path of a possible device node.
3626 * Non-default names may need to allocate a memory to compose
3627 * a name. This memory is returned in tmp and needs to be
3628 * freed by the caller.
3629 */
Kay Sieverse454cea2009-09-18 23:01:12 +02003630const char *device_get_devnode(struct device *dev,
Greg Kroah-Hartman4e4098a2013-04-11 11:43:29 -07003631 umode_t *mode, kuid_t *uid, kgid_t *gid,
Kay Sievers3c2670e2013-04-06 09:56:00 -07003632 const char **tmp)
Kay Sievers6fcf53a2009-04-30 15:23:42 +02003633{
3634 char *s;
3635
3636 *tmp = NULL;
3637
3638 /* the device type may provide a specific name */
Kay Sieverse454cea2009-09-18 23:01:12 +02003639 if (dev->type && dev->type->devnode)
Kay Sievers3c2670e2013-04-06 09:56:00 -07003640 *tmp = dev->type->devnode(dev, mode, uid, gid);
Kay Sievers6fcf53a2009-04-30 15:23:42 +02003641 if (*tmp)
3642 return *tmp;
3643
3644 /* the class may provide a specific name */
Kay Sieverse454cea2009-09-18 23:01:12 +02003645 if (dev->class && dev->class->devnode)
3646 *tmp = dev->class->devnode(dev, mode);
Kay Sievers6fcf53a2009-04-30 15:23:42 +02003647 if (*tmp)
3648 return *tmp;
3649
3650 /* return name without allocation, tmp == NULL */
3651 if (strchr(dev_name(dev), '!') == NULL)
3652 return dev_name(dev);
3653
3654 /* replace '!' in the name with '/' */
Rasmus Villemoesa29fd612015-06-25 15:02:33 -07003655 s = kstrdup(dev_name(dev), GFP_KERNEL);
3656 if (!s)
Kay Sievers6fcf53a2009-04-30 15:23:42 +02003657 return NULL;
Rasmus Villemoesa29fd612015-06-25 15:02:33 -07003658 strreplace(s, '!', '/');
3659 return *tmp = s;
Kay Sievers6fcf53a2009-04-30 15:23:42 +02003660}
3661
3662/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08003663 * device_for_each_child - device child iterator.
3664 * @parent: parent struct device.
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08003665 * @fn: function to be called for each device.
Robert P. J. Dayf8878dc2013-06-01 20:17:34 -04003666 * @data: data for the callback.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003667 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08003668 * Iterate over @parent's child devices, and call @fn for each,
3669 * passing it @data.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003670 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08003671 * We check the return of @fn each time. If it returns anything
3672 * other than 0, we break out and return that value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003673 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08003674int device_for_each_child(struct device *parent, void *data,
3675 int (*fn)(struct device *dev, void *data))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003676{
mochel@digitalimplant.org36239572005-03-24 19:08:30 -08003677 struct klist_iter i;
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08003678 struct device *child;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003679 int error = 0;
3680
Greg Kroah-Hartman014c90db2009-04-15 16:00:12 -07003681 if (!parent->p)
3682 return 0;
3683
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08003684 klist_iter_init(&parent->p->klist_children, &i);
Gimcuan Hui93ead7c2017-11-11 05:52:54 +00003685 while (!error && (child = next_device(&i)))
mochel@digitalimplant.org36239572005-03-24 19:08:30 -08003686 error = fn(child, data);
3687 klist_iter_exit(&i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003688 return error;
3689}
David Graham White86df2682013-07-21 20:41:14 -04003690EXPORT_SYMBOL_GPL(device_for_each_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003691
Cornelia Huck5ab69982006-11-16 15:42:07 +01003692/**
Andy Shevchenko3d060ae2015-07-27 18:04:00 +03003693 * device_for_each_child_reverse - device child iterator in reversed order.
3694 * @parent: parent struct device.
3695 * @fn: function to be called for each device.
3696 * @data: data for the callback.
3697 *
3698 * Iterate over @parent's child devices, and call @fn for each,
3699 * passing it @data.
3700 *
3701 * We check the return of @fn each time. If it returns anything
3702 * other than 0, we break out and return that value.
3703 */
3704int device_for_each_child_reverse(struct device *parent, void *data,
3705 int (*fn)(struct device *dev, void *data))
3706{
3707 struct klist_iter i;
3708 struct device *child;
3709 int error = 0;
3710
3711 if (!parent->p)
3712 return 0;
3713
3714 klist_iter_init(&parent->p->klist_children, &i);
3715 while ((child = prev_device(&i)) && !error)
3716 error = fn(child, data);
3717 klist_iter_exit(&i);
3718 return error;
3719}
3720EXPORT_SYMBOL_GPL(device_for_each_child_reverse);
3721
3722/**
Cornelia Huck5ab69982006-11-16 15:42:07 +01003723 * device_find_child - device iterator for locating a particular device.
3724 * @parent: parent struct device
Cornelia Huck5ab69982006-11-16 15:42:07 +01003725 * @match: Callback function to check device
Robert P. J. Dayf8878dc2013-06-01 20:17:34 -04003726 * @data: Data to pass to match function
Cornelia Huck5ab69982006-11-16 15:42:07 +01003727 *
3728 * This is similar to the device_for_each_child() function above, but it
3729 * returns a reference to a device that is 'found' for later use, as
3730 * determined by the @match callback.
3731 *
3732 * The callback should return 0 if the device doesn't match and non-zero
3733 * if it does. If the callback returns non-zero and a reference to the
3734 * current device can be obtained, this function will return to the caller
3735 * and not iterate over any more devices.
Federico Vagaa4e24002013-04-15 11:18:11 +02003736 *
3737 * NOTE: you will need to drop the reference with put_device() after use.
Cornelia Huck5ab69982006-11-16 15:42:07 +01003738 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08003739struct device *device_find_child(struct device *parent, void *data,
3740 int (*match)(struct device *dev, void *data))
Cornelia Huck5ab69982006-11-16 15:42:07 +01003741{
3742 struct klist_iter i;
3743 struct device *child;
3744
3745 if (!parent)
3746 return NULL;
3747
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08003748 klist_iter_init(&parent->p->klist_children, &i);
Cornelia Huck5ab69982006-11-16 15:42:07 +01003749 while ((child = next_device(&i)))
3750 if (match(child, data) && get_device(child))
3751 break;
3752 klist_iter_exit(&i);
3753 return child;
3754}
David Graham White86df2682013-07-21 20:41:14 -04003755EXPORT_SYMBOL_GPL(device_find_child);
Cornelia Huck5ab69982006-11-16 15:42:07 +01003756
Heikki Krogerusdad9bb02019-05-31 17:15:37 +03003757/**
3758 * device_find_child_by_name - device iterator for locating a child device.
3759 * @parent: parent struct device
3760 * @name: name of the child device
3761 *
3762 * This is similar to the device_find_child() function above, but it
3763 * returns a reference to a device that has the name @name.
3764 *
3765 * NOTE: you will need to drop the reference with put_device() after use.
3766 */
3767struct device *device_find_child_by_name(struct device *parent,
3768 const char *name)
3769{
3770 struct klist_iter i;
3771 struct device *child;
3772
3773 if (!parent)
3774 return NULL;
3775
3776 klist_iter_init(&parent->p->klist_children, &i);
3777 while ((child = next_device(&i)))
Dan Williamsc77f5202020-10-13 16:50:18 -07003778 if (sysfs_streq(dev_name(child), name) && get_device(child))
Heikki Krogerusdad9bb02019-05-31 17:15:37 +03003779 break;
3780 klist_iter_exit(&i);
3781 return child;
3782}
3783EXPORT_SYMBOL_GPL(device_find_child_by_name);
3784
Linus Torvalds1da177e2005-04-16 15:20:36 -07003785int __init devices_init(void)
3786{
Greg Kroah-Hartman881c6cfd2007-11-01 09:29:06 -06003787 devices_kset = kset_create_and_add("devices", &device_uevent_ops, NULL);
3788 if (!devices_kset)
3789 return -ENOMEM;
Dan Williamse105b8b2008-04-21 10:51:07 -07003790 dev_kobj = kobject_create_and_add("dev", NULL);
3791 if (!dev_kobj)
3792 goto dev_kobj_err;
3793 sysfs_dev_block_kobj = kobject_create_and_add("block", dev_kobj);
3794 if (!sysfs_dev_block_kobj)
3795 goto block_kobj_err;
3796 sysfs_dev_char_kobj = kobject_create_and_add("char", dev_kobj);
3797 if (!sysfs_dev_char_kobj)
3798 goto char_kobj_err;
3799
Greg Kroah-Hartman881c6cfd2007-11-01 09:29:06 -06003800 return 0;
Dan Williamse105b8b2008-04-21 10:51:07 -07003801
3802 char_kobj_err:
3803 kobject_put(sysfs_dev_block_kobj);
3804 block_kobj_err:
3805 kobject_put(dev_kobj);
3806 dev_kobj_err:
3807 kset_unregister(devices_kset);
3808 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003809}
3810
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02003811static int device_check_offline(struct device *dev, void *not_used)
3812{
3813 int ret;
3814
3815 ret = device_for_each_child(dev, NULL, device_check_offline);
3816 if (ret)
3817 return ret;
3818
3819 return device_supports_offline(dev) && !dev->offline ? -EBUSY : 0;
3820}
3821
3822/**
3823 * device_offline - Prepare the device for hot-removal.
3824 * @dev: Device to be put offline.
3825 *
3826 * Execute the device bus type's .offline() callback, if present, to prepare
3827 * the device for a subsequent hot-removal. If that succeeds, the device must
3828 * not be used until either it is removed or its bus type's .online() callback
3829 * is executed.
3830 *
3831 * Call under device_hotplug_lock.
3832 */
3833int device_offline(struct device *dev)
3834{
3835 int ret;
3836
3837 if (dev->offline_disabled)
3838 return -EPERM;
3839
3840 ret = device_for_each_child(dev, NULL, device_check_offline);
3841 if (ret)
3842 return ret;
3843
3844 device_lock(dev);
3845 if (device_supports_offline(dev)) {
3846 if (dev->offline) {
3847 ret = 1;
3848 } else {
3849 ret = dev->bus->offline(dev);
3850 if (!ret) {
3851 kobject_uevent(&dev->kobj, KOBJ_OFFLINE);
3852 dev->offline = true;
3853 }
3854 }
3855 }
3856 device_unlock(dev);
3857
3858 return ret;
3859}
3860
3861/**
3862 * device_online - Put the device back online after successful device_offline().
3863 * @dev: Device to be put back online.
3864 *
3865 * If device_offline() has been successfully executed for @dev, but the device
3866 * has not been removed subsequently, execute its bus type's .online() callback
3867 * to indicate that the device can be used again.
3868 *
3869 * Call under device_hotplug_lock.
3870 */
3871int device_online(struct device *dev)
3872{
3873 int ret = 0;
3874
3875 device_lock(dev);
3876 if (device_supports_offline(dev)) {
3877 if (dev->offline) {
3878 ret = dev->bus->online(dev);
3879 if (!ret) {
3880 kobject_uevent(&dev->kobj, KOBJ_ONLINE);
3881 dev->offline = false;
3882 }
3883 } else {
3884 ret = 1;
3885 }
3886 }
3887 device_unlock(dev);
3888
3889 return ret;
3890}
3891
Karthigan Srinivasan7f100d12011-04-18 16:16:52 -05003892struct root_device {
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00003893 struct device dev;
3894 struct module *owner;
3895};
3896
Josh Triplett93058422012-11-18 21:27:55 -08003897static inline struct root_device *to_root_device(struct device *d)
Ferenc Wagner481e2072011-01-07 15:17:47 +01003898{
3899 return container_of(d, struct root_device, dev);
3900}
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00003901
3902static void root_device_release(struct device *dev)
3903{
3904 kfree(to_root_device(dev));
3905}
3906
3907/**
3908 * __root_device_register - allocate and register a root device
3909 * @name: root device name
3910 * @owner: owner module of the root device, usually THIS_MODULE
3911 *
3912 * This function allocates a root device and registers it
3913 * using device_register(). In order to free the returned
3914 * device, use root_device_unregister().
3915 *
3916 * Root devices are dummy devices which allow other devices
3917 * to be grouped under /sys/devices. Use this function to
3918 * allocate a root device and then use it as the parent of
3919 * any device which should appear under /sys/devices/{name}
3920 *
3921 * The /sys/devices/{name} directory will also contain a
3922 * 'module' symlink which points to the @owner directory
3923 * in sysfs.
3924 *
Jani Nikulaf0eae0e2010-03-11 18:11:45 +02003925 * Returns &struct device pointer on success, or ERR_PTR() on error.
3926 *
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00003927 * Note: You probably want to use root_device_register().
3928 */
3929struct device *__root_device_register(const char *name, struct module *owner)
3930{
3931 struct root_device *root;
3932 int err = -ENOMEM;
3933
3934 root = kzalloc(sizeof(struct root_device), GFP_KERNEL);
3935 if (!root)
3936 return ERR_PTR(err);
3937
Greg Kroah-Hartmanacc0e902009-06-02 15:39:55 -07003938 err = dev_set_name(&root->dev, "%s", name);
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00003939 if (err) {
3940 kfree(root);
3941 return ERR_PTR(err);
3942 }
3943
3944 root->dev.release = root_device_release;
3945
3946 err = device_register(&root->dev);
3947 if (err) {
3948 put_device(&root->dev);
3949 return ERR_PTR(err);
3950 }
3951
Christoph Egger1d9e8822010-05-17 16:57:58 +02003952#ifdef CONFIG_MODULES /* gotta find a "cleaner" way to do this */
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00003953 if (owner) {
3954 struct module_kobject *mk = &owner->mkobj;
3955
3956 err = sysfs_create_link(&root->dev.kobj, &mk->kobj, "module");
3957 if (err) {
3958 device_unregister(&root->dev);
3959 return ERR_PTR(err);
3960 }
3961 root->owner = owner;
3962 }
3963#endif
3964
3965 return &root->dev;
3966}
3967EXPORT_SYMBOL_GPL(__root_device_register);
3968
3969/**
3970 * root_device_unregister - unregister and free a root device
Randy Dunlap7cbcf222009-01-20 16:29:13 -08003971 * @dev: device going away
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00003972 *
3973 * This function unregisters and cleans up a device that was created by
3974 * root_device_register().
3975 */
3976void root_device_unregister(struct device *dev)
3977{
3978 struct root_device *root = to_root_device(dev);
3979
3980 if (root->owner)
3981 sysfs_remove_link(&root->dev.kobj, "module");
3982
3983 device_unregister(dev);
3984}
3985EXPORT_SYMBOL_GPL(root_device_unregister);
3986
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07003987
3988static void device_create_release(struct device *dev)
3989{
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01003990 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07003991 kfree(dev);
3992}
3993
Mathieu Malaterre6a8b55d2018-05-05 21:57:41 +02003994static __printf(6, 0) struct device *
Guenter Roeck39ef3112013-07-14 16:05:57 -07003995device_create_groups_vargs(struct class *class, struct device *parent,
3996 dev_t devt, void *drvdata,
3997 const struct attribute_group **groups,
3998 const char *fmt, va_list args)
3999{
4000 struct device *dev = NULL;
4001 int retval = -ENODEV;
4002
4003 if (class == NULL || IS_ERR(class))
4004 goto error;
4005
4006 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
4007 if (!dev) {
4008 retval = -ENOMEM;
4009 goto error;
4010 }
4011
David Herrmannbbc780f2013-11-21 20:15:48 +01004012 device_initialize(dev);
Guenter Roeck39ef3112013-07-14 16:05:57 -07004013 dev->devt = devt;
4014 dev->class = class;
4015 dev->parent = parent;
4016 dev->groups = groups;
4017 dev->release = device_create_release;
4018 dev_set_drvdata(dev, drvdata);
4019
4020 retval = kobject_set_name_vargs(&dev->kobj, fmt, args);
4021 if (retval)
4022 goto error;
4023
David Herrmannbbc780f2013-11-21 20:15:48 +01004024 retval = device_add(dev);
Guenter Roeck39ef3112013-07-14 16:05:57 -07004025 if (retval)
4026 goto error;
4027
4028 return dev;
4029
4030error:
4031 put_device(dev);
4032 return ERR_PTR(retval);
4033}
4034
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07004035/**
Greg Kroah-Hartman4e106732008-07-21 20:03:34 -07004036 * device_create - creates a device and registers it with sysfs
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07004037 * @class: pointer to the struct class that this device should be registered to
4038 * @parent: pointer to the parent struct device of this new device, if any
4039 * @devt: the dev_t for the char device to be added
4040 * @drvdata: the data to be added to the device for callbacks
4041 * @fmt: string for the device's name
4042 *
4043 * This function can be used by char device classes. A struct device
4044 * will be created in sysfs, registered to the specified class.
4045 *
4046 * A "dev" file will be created, showing the dev_t for the device, if
4047 * the dev_t is not 0,0.
4048 * If a pointer to a parent struct device is passed in, the newly created
4049 * struct device will be a child of that device in sysfs.
4050 * The pointer to the struct device will be returned from the call.
4051 * Any further sysfs files that might be required can be created using this
4052 * pointer.
4053 *
Jani Nikulaf0eae0e2010-03-11 18:11:45 +02004054 * Returns &struct device pointer on success, or ERR_PTR() on error.
4055 *
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07004056 * Note: the struct class passed to this function must have previously
4057 * been created with a call to class_create().
4058 */
Greg Kroah-Hartman4e106732008-07-21 20:03:34 -07004059struct device *device_create(struct class *class, struct device *parent,
4060 dev_t devt, void *drvdata, const char *fmt, ...)
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07004061{
4062 va_list vargs;
4063 struct device *dev;
4064
4065 va_start(vargs, fmt);
Christoph Hellwig4c747462020-05-04 14:47:57 +02004066 dev = device_create_groups_vargs(class, parent, devt, drvdata, NULL,
4067 fmt, vargs);
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07004068 va_end(vargs);
4069 return dev;
4070}
Greg Kroah-Hartman4e106732008-07-21 20:03:34 -07004071EXPORT_SYMBOL_GPL(device_create);
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07004072
Guenter Roeck39ef3112013-07-14 16:05:57 -07004073/**
4074 * device_create_with_groups - creates a device and registers it with sysfs
4075 * @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 * @groups: NULL-terminated list of attribute groups to be created
4080 * @fmt: string for the device's name
4081 *
4082 * This function can be used by char device classes. A struct device
4083 * will be created in sysfs, registered to the specified class.
4084 * Additional attributes specified in the groups parameter will also
4085 * be created automatically.
4086 *
4087 * A "dev" file will be created, showing the dev_t for the device, if
4088 * the dev_t is not 0,0.
4089 * If a pointer to a parent struct device is passed in, the newly created
4090 * struct device will be a child of that device in sysfs.
4091 * The pointer to the struct device will be returned from the call.
4092 * Any further sysfs files that might be required can be created using this
4093 * pointer.
4094 *
4095 * Returns &struct device pointer on success, or ERR_PTR() on error.
4096 *
4097 * Note: the struct class passed to this function must have previously
4098 * been created with a call to class_create().
4099 */
4100struct device *device_create_with_groups(struct class *class,
4101 struct device *parent, dev_t devt,
4102 void *drvdata,
4103 const struct attribute_group **groups,
4104 const char *fmt, ...)
4105{
4106 va_list vargs;
4107 struct device *dev;
4108
4109 va_start(vargs, fmt);
4110 dev = device_create_groups_vargs(class, parent, devt, drvdata, groups,
4111 fmt, vargs);
4112 va_end(vargs);
4113 return dev;
4114}
4115EXPORT_SYMBOL_GPL(device_create_with_groups);
4116
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01004117/**
4118 * device_destroy - removes a device that was created with device_create()
4119 * @class: pointer to the struct class that this device was registered with
4120 * @devt: the dev_t of the device that was previously registered
4121 *
4122 * This call unregisters and cleans up a device that was created with a
4123 * call to device_create().
4124 */
4125void device_destroy(struct class *class, dev_t devt)
4126{
4127 struct device *dev;
4128
Suzuki K Poulose4495dfd2019-07-23 23:18:35 +01004129 dev = class_find_device_by_devt(class, devt);
Dave Youngcd354492008-01-28 16:56:11 +08004130 if (dev) {
4131 put_device(dev);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07004132 device_unregister(dev);
Dave Youngcd354492008-01-28 16:56:11 +08004133 }
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07004134}
4135EXPORT_SYMBOL_GPL(device_destroy);
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07004136
4137/**
4138 * device_rename - renames a device
4139 * @dev: the pointer to the struct device to be renamed
4140 * @new_name: the new name of the device
Eric W. Biederman030c1d22008-05-08 14:41:00 -07004141 *
4142 * It is the responsibility of the caller to provide mutual
4143 * exclusion between two different calls of device_rename
4144 * on the same device to ensure that new_name is valid and
4145 * won't conflict with other devices.
Michael Ellermanc6c0ac62010-11-25 09:44:07 +11004146 *
Timur Tabia5462512010-12-13 14:08:52 -06004147 * Note: Don't call this function. Currently, the networking layer calls this
4148 * function, but that will change. The following text from Kay Sievers offers
4149 * some insight:
4150 *
4151 * Renaming devices is racy at many levels, symlinks and other stuff are not
4152 * replaced atomically, and you get a "move" uevent, but it's not easy to
4153 * connect the event to the old and new device. Device nodes are not renamed at
4154 * all, there isn't even support for that in the kernel now.
4155 *
4156 * In the meantime, during renaming, your target name might be taken by another
4157 * driver, creating conflicts. Or the old name is taken directly after you
4158 * renamed it -- then you get events for the same DEVPATH, before you even see
4159 * the "move" event. It's just a mess, and nothing new should ever rely on
4160 * kernel device renaming. Besides that, it's not even implemented now for
4161 * other things than (driver-core wise very simple) network devices.
4162 *
4163 * We are currently about to change network renaming in udev to completely
4164 * disallow renaming of devices in the same namespace as the kernel uses,
4165 * because we can't solve the problems properly, that arise with swapping names
4166 * of multiple interfaces without races. Means, renaming of eth[0-9]* will only
4167 * be allowed to some other name than eth[0-9]*, for the aforementioned
4168 * reasons.
4169 *
4170 * Make up a "real" name in the driver before you register anything, or add
4171 * some other attributes for userspace to find the device, or use udev to add
4172 * symlinks -- but never rename kernel devices later, it's a complete mess. We
4173 * don't even want to get into that and try to implement the missing pieces in
4174 * the core. We really have other pieces to fix in the driver core mess. :)
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07004175 */
Johannes Berg6937e8f2010-08-05 17:38:18 +02004176int device_rename(struct device *dev, const char *new_name)
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07004177{
Tejun Heo4b30ee52013-09-11 22:29:06 -04004178 struct kobject *kobj = &dev->kobj;
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07004179 char *old_device_name = NULL;
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07004180 int error;
4181
4182 dev = get_device(dev);
4183 if (!dev)
4184 return -EINVAL;
4185
ethan.zhao69df7532013-10-13 22:12:35 +08004186 dev_dbg(dev, "renaming to %s\n", new_name);
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07004187
Kay Sievers1fa5ae82009-01-25 15:17:37 +01004188 old_device_name = kstrdup(dev_name(dev), GFP_KERNEL);
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07004189 if (!old_device_name) {
4190 error = -ENOMEM;
4191 goto out;
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07004192 }
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07004193
Eric W. Biedermanf349cf32010-03-30 11:31:29 -07004194 if (dev->class) {
Tejun Heo4b30ee52013-09-11 22:29:06 -04004195 error = sysfs_rename_link_ns(&dev->class->p->subsys.kobj,
4196 kobj, old_device_name,
4197 new_name, kobject_namespace(kobj));
Eric W. Biedermanf349cf32010-03-30 11:31:29 -07004198 if (error)
4199 goto out;
4200 }
Kay Sievers39aba962010-09-04 22:33:14 -07004201
Tejun Heo4b30ee52013-09-11 22:29:06 -04004202 error = kobject_rename(kobj, new_name);
Kay Sievers1fa5ae82009-01-25 15:17:37 +01004203 if (error)
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07004204 goto out;
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07004205
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07004206out:
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07004207 put_device(dev);
4208
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07004209 kfree(old_device_name);
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07004210
4211 return error;
4212}
Johannes Berga2807db2007-02-28 12:38:31 +01004213EXPORT_SYMBOL_GPL(device_rename);
Cornelia Huck8a824722006-11-20 17:07:51 +01004214
4215static int device_move_class_links(struct device *dev,
4216 struct device *old_parent,
4217 struct device *new_parent)
4218{
Greg Kroah-Hartmanf7f34612007-03-06 12:55:53 -08004219 int error = 0;
Cornelia Huck8a824722006-11-20 17:07:51 +01004220
Greg Kroah-Hartmanf7f34612007-03-06 12:55:53 -08004221 if (old_parent)
4222 sysfs_remove_link(&dev->kobj, "device");
4223 if (new_parent)
4224 error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
4225 "device");
4226 return error;
Cornelia Huck8a824722006-11-20 17:07:51 +01004227}
4228
4229/**
4230 * device_move - moves a device to a new parent
4231 * @dev: the pointer to the struct device to be moved
Wolfram Sang13509862018-05-06 13:23:47 +02004232 * @new_parent: the new parent of the device (can be NULL)
Cornelia Huckffa6a702009-03-04 12:44:00 +01004233 * @dpm_order: how to reorder the dpm_list
Cornelia Huck8a824722006-11-20 17:07:51 +01004234 */
Cornelia Huckffa6a702009-03-04 12:44:00 +01004235int device_move(struct device *dev, struct device *new_parent,
4236 enum dpm_order dpm_order)
Cornelia Huck8a824722006-11-20 17:07:51 +01004237{
4238 int error;
4239 struct device *old_parent;
Cornelia Huckc744aeae2007-01-08 20:16:44 +01004240 struct kobject *new_parent_kobj;
Cornelia Huck8a824722006-11-20 17:07:51 +01004241
4242 dev = get_device(dev);
4243 if (!dev)
4244 return -EINVAL;
4245
Cornelia Huckffa6a702009-03-04 12:44:00 +01004246 device_pm_lock();
Cornelia Huck8a824722006-11-20 17:07:51 +01004247 new_parent = get_device(new_parent);
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08004248 new_parent_kobj = get_device_parent(dev, new_parent);
Tetsuo Handa84d0c272018-05-07 19:10:31 +09004249 if (IS_ERR(new_parent_kobj)) {
4250 error = PTR_ERR(new_parent_kobj);
4251 put_device(new_parent);
4252 goto out;
4253 }
Cornelia Huck63b69712008-01-21 16:09:44 +01004254
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01004255 pr_debug("device: '%s': %s: moving to '%s'\n", dev_name(dev),
4256 __func__, new_parent ? dev_name(new_parent) : "<NULL>");
Cornelia Huckc744aeae2007-01-08 20:16:44 +01004257 error = kobject_move(&dev->kobj, new_parent_kobj);
Cornelia Huck8a824722006-11-20 17:07:51 +01004258 if (error) {
Cornelia Huck63b69712008-01-21 16:09:44 +01004259 cleanup_glue_dir(dev, new_parent_kobj);
Cornelia Huck8a824722006-11-20 17:07:51 +01004260 put_device(new_parent);
4261 goto out;
4262 }
4263 old_parent = dev->parent;
4264 dev->parent = new_parent;
4265 if (old_parent)
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08004266 klist_remove(&dev->p->knode_parent);
Yinghai Lu0d358f22008-02-19 03:20:41 -08004267 if (new_parent) {
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08004268 klist_add_tail(&dev->p->knode_parent,
4269 &new_parent->p->klist_children);
Yinghai Lu0d358f22008-02-19 03:20:41 -08004270 set_dev_node(dev, dev_to_node(new_parent));
4271 }
4272
Rabin Vincentbdd40342012-04-23 09:16:36 +02004273 if (dev->class) {
4274 error = device_move_class_links(dev, old_parent, new_parent);
4275 if (error) {
4276 /* We ignore errors on cleanup since we're hosed anyway... */
4277 device_move_class_links(dev, new_parent, old_parent);
4278 if (!kobject_move(&dev->kobj, &old_parent->kobj)) {
4279 if (new_parent)
4280 klist_remove(&dev->p->knode_parent);
4281 dev->parent = old_parent;
4282 if (old_parent) {
4283 klist_add_tail(&dev->p->knode_parent,
4284 &old_parent->p->klist_children);
4285 set_dev_node(dev, dev_to_node(old_parent));
4286 }
Yinghai Lu0d358f22008-02-19 03:20:41 -08004287 }
Rabin Vincentbdd40342012-04-23 09:16:36 +02004288 cleanup_glue_dir(dev, new_parent_kobj);
4289 put_device(new_parent);
4290 goto out;
Cornelia Huck8a824722006-11-20 17:07:51 +01004291 }
Cornelia Huck8a824722006-11-20 17:07:51 +01004292 }
Cornelia Huckffa6a702009-03-04 12:44:00 +01004293 switch (dpm_order) {
4294 case DPM_ORDER_NONE:
4295 break;
4296 case DPM_ORDER_DEV_AFTER_PARENT:
4297 device_pm_move_after(dev, new_parent);
Grygorii Strashko52cdbdd2015-07-27 20:43:01 +03004298 devices_kset_move_after(dev, new_parent);
Cornelia Huckffa6a702009-03-04 12:44:00 +01004299 break;
4300 case DPM_ORDER_PARENT_BEFORE_DEV:
4301 device_pm_move_before(new_parent, dev);
Grygorii Strashko52cdbdd2015-07-27 20:43:01 +03004302 devices_kset_move_before(new_parent, dev);
Cornelia Huckffa6a702009-03-04 12:44:00 +01004303 break;
4304 case DPM_ORDER_DEV_LAST:
4305 device_pm_move_last(dev);
Grygorii Strashko52cdbdd2015-07-27 20:43:01 +03004306 devices_kset_move_last(dev);
Cornelia Huckffa6a702009-03-04 12:44:00 +01004307 break;
4308 }
Rabin Vincentbdd40342012-04-23 09:16:36 +02004309
Cornelia Huck8a824722006-11-20 17:07:51 +01004310 put_device(old_parent);
4311out:
Cornelia Huckffa6a702009-03-04 12:44:00 +01004312 device_pm_unlock();
Cornelia Huck8a824722006-11-20 17:07:51 +01004313 put_device(dev);
4314 return error;
4315}
Cornelia Huck8a824722006-11-20 17:07:51 +01004316EXPORT_SYMBOL_GPL(device_move);
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08004317
Christian Braunerb8f33e52020-02-27 04:37:15 +01004318static int device_attrs_change_owner(struct device *dev, kuid_t kuid,
4319 kgid_t kgid)
4320{
4321 struct kobject *kobj = &dev->kobj;
4322 struct class *class = dev->class;
4323 const struct device_type *type = dev->type;
4324 int error;
4325
4326 if (class) {
4327 /*
4328 * Change the device groups of the device class for @dev to
4329 * @kuid/@kgid.
4330 */
4331 error = sysfs_groups_change_owner(kobj, class->dev_groups, kuid,
4332 kgid);
4333 if (error)
4334 return error;
4335 }
4336
4337 if (type) {
4338 /*
4339 * Change the device groups of the device type for @dev to
4340 * @kuid/@kgid.
4341 */
4342 error = sysfs_groups_change_owner(kobj, type->groups, kuid,
4343 kgid);
4344 if (error)
4345 return error;
4346 }
4347
4348 /* Change the device groups of @dev to @kuid/@kgid. */
4349 error = sysfs_groups_change_owner(kobj, dev->groups, kuid, kgid);
4350 if (error)
4351 return error;
4352
4353 if (device_supports_offline(dev) && !dev->offline_disabled) {
4354 /* Change online device attributes of @dev to @kuid/@kgid. */
4355 error = sysfs_file_change_owner(kobj, dev_attr_online.attr.name,
4356 kuid, kgid);
4357 if (error)
4358 return error;
4359 }
4360
4361 return 0;
4362}
4363
4364/**
4365 * device_change_owner - change the owner of an existing device.
4366 * @dev: device.
4367 * @kuid: new owner's kuid
4368 * @kgid: new owner's kgid
4369 *
4370 * This changes the owner of @dev and its corresponding sysfs entries to
4371 * @kuid/@kgid. This function closely mirrors how @dev was added via driver
4372 * core.
4373 *
4374 * Returns 0 on success or error code on failure.
4375 */
4376int device_change_owner(struct device *dev, kuid_t kuid, kgid_t kgid)
4377{
4378 int error;
4379 struct kobject *kobj = &dev->kobj;
4380
4381 dev = get_device(dev);
4382 if (!dev)
4383 return -EINVAL;
4384
4385 /*
4386 * Change the kobject and the default attributes and groups of the
4387 * ktype associated with it to @kuid/@kgid.
4388 */
4389 error = sysfs_change_owner(kobj, kuid, kgid);
4390 if (error)
4391 goto out;
4392
4393 /*
4394 * Change the uevent file for @dev to the new owner. The uevent file
4395 * was created in a separate step when @dev got added and we mirror
4396 * that step here.
4397 */
4398 error = sysfs_file_change_owner(kobj, dev_attr_uevent.attr.name, kuid,
4399 kgid);
4400 if (error)
4401 goto out;
4402
4403 /*
4404 * Change the device groups, the device groups associated with the
4405 * device class, and the groups associated with the device type of @dev
4406 * to @kuid/@kgid.
4407 */
4408 error = device_attrs_change_owner(dev, kuid, kgid);
4409 if (error)
4410 goto out;
4411
Christian Brauner3b52fc52020-02-27 04:37:16 +01004412 error = dpm_sysfs_change_owner(dev, kuid, kgid);
4413 if (error)
4414 goto out;
4415
Christian Braunerb8f33e52020-02-27 04:37:15 +01004416#ifdef CONFIG_BLOCK
4417 if (sysfs_deprecated && dev->class == &block_class)
4418 goto out;
4419#endif
4420
4421 /*
4422 * Change the owner of the symlink located in the class directory of
4423 * the device class associated with @dev which points to the actual
4424 * directory entry for @dev to @kuid/@kgid. This ensures that the
4425 * symlink shows the same permissions as its target.
4426 */
4427 error = sysfs_link_change_owner(&dev->class->p->subsys.kobj, &dev->kobj,
4428 dev_name(dev), kuid, kgid);
4429 if (error)
4430 goto out;
4431
4432out:
4433 put_device(dev);
4434 return error;
4435}
4436EXPORT_SYMBOL_GPL(device_change_owner);
4437
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08004438/**
4439 * device_shutdown - call ->shutdown() on each device to shutdown.
4440 */
4441void device_shutdown(void)
4442{
Benson Leungf123db82013-09-24 20:05:11 -07004443 struct device *dev, *parent;
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08004444
Pingfan Liu3297c8f2018-07-19 13:14:58 +08004445 wait_for_device_probe();
4446 device_block_probing();
4447
Rafael J. Wysocki65650b32019-10-09 01:29:10 +02004448 cpufreq_suspend();
4449
Hugh Daschbach62458382010-03-22 10:36:37 -07004450 spin_lock(&devices_kset->list_lock);
4451 /*
4452 * Walk the devices list backward, shutting down each in turn.
4453 * Beware that device unplug events may also start pulling
4454 * devices offline, even as the system is shutting down.
4455 */
4456 while (!list_empty(&devices_kset->list)) {
4457 dev = list_entry(devices_kset->list.prev, struct device,
4458 kobj.entry);
Ming Leid1c6c032012-06-22 18:01:40 +08004459
4460 /*
4461 * hold reference count of device's parent to
4462 * prevent it from being freed because parent's
4463 * lock is to be held
4464 */
Benson Leungf123db82013-09-24 20:05:11 -07004465 parent = get_device(dev->parent);
Hugh Daschbach62458382010-03-22 10:36:37 -07004466 get_device(dev);
4467 /*
4468 * Make sure the device is off the kset list, in the
4469 * event that dev->*->shutdown() doesn't remove it.
4470 */
4471 list_del_init(&dev->kobj.entry);
4472 spin_unlock(&devices_kset->list_lock);
Alan Sternfe6b91f2011-12-06 23:24:52 +01004473
Ming Leid1c6c032012-06-22 18:01:40 +08004474 /* hold lock to avoid race with probe/release */
Benson Leungf123db82013-09-24 20:05:11 -07004475 if (parent)
4476 device_lock(parent);
Ming Leid1c6c032012-06-22 18:01:40 +08004477 device_lock(dev);
4478
Alan Sternfe6b91f2011-12-06 23:24:52 +01004479 /* Don't allow any more runtime suspends */
4480 pm_runtime_get_noresume(dev);
4481 pm_runtime_barrier(dev);
Hugh Daschbach62458382010-03-22 10:36:37 -07004482
Michal Suchanek75216212017-08-11 15:44:43 +02004483 if (dev->class && dev->class->shutdown_pre) {
Josh Zimmermanf77af152017-06-25 14:53:23 -07004484 if (initcall_debug)
Michal Suchanek75216212017-08-11 15:44:43 +02004485 dev_info(dev, "shutdown_pre\n");
4486 dev->class->shutdown_pre(dev);
4487 }
4488 if (dev->bus && dev->bus->shutdown) {
ShuoX Liu0246c4f2012-11-23 15:14:12 +08004489 if (initcall_debug)
4490 dev_info(dev, "shutdown\n");
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08004491 dev->bus->shutdown(dev);
4492 } else if (dev->driver && dev->driver->shutdown) {
ShuoX Liu0246c4f2012-11-23 15:14:12 +08004493 if (initcall_debug)
4494 dev_info(dev, "shutdown\n");
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08004495 dev->driver->shutdown(dev);
4496 }
Ming Leid1c6c032012-06-22 18:01:40 +08004497
4498 device_unlock(dev);
Benson Leungf123db82013-09-24 20:05:11 -07004499 if (parent)
4500 device_unlock(parent);
Ming Leid1c6c032012-06-22 18:01:40 +08004501
Hugh Daschbach62458382010-03-22 10:36:37 -07004502 put_device(dev);
Benson Leungf123db82013-09-24 20:05:11 -07004503 put_device(parent);
Hugh Daschbach62458382010-03-22 10:36:37 -07004504
4505 spin_lock(&devices_kset->list_lock);
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08004506 }
Hugh Daschbach62458382010-03-22 10:36:37 -07004507 spin_unlock(&devices_kset->list_lock);
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08004508}
Joe Perches99bcf212010-06-27 01:02:34 +00004509
4510/*
4511 * Device logging functions
4512 */
4513
4514#ifdef CONFIG_PRINTK
John Ogness74caba72020-09-21 13:24:45 +02064515static void
4516set_dev_info(const struct device *dev, struct dev_printk_info *dev_info)
Joe Perches99bcf212010-06-27 01:02:34 +00004517{
Kay Sieversc4e00da2012-05-03 02:29:59 +02004518 const char *subsys;
John Ogness74caba72020-09-21 13:24:45 +02064519
4520 memset(dev_info, 0, sizeof(*dev_info));
Joe Perches99bcf212010-06-27 01:02:34 +00004521
Kay Sieversc4e00da2012-05-03 02:29:59 +02004522 if (dev->class)
4523 subsys = dev->class->name;
4524 else if (dev->bus)
4525 subsys = dev->bus->name;
4526 else
John Ogness74caba72020-09-21 13:24:45 +02064527 return;
Kay Sieversc4e00da2012-05-03 02:29:59 +02004528
John Ogness74caba72020-09-21 13:24:45 +02064529 strscpy(dev_info->subsystem, subsys, sizeof(dev_info->subsystem));
Kay Sieversc4e00da2012-05-03 02:29:59 +02004530
4531 /*
4532 * Add device identifier DEVICE=:
4533 * b12:8 block dev_t
4534 * c127:3 char dev_t
4535 * n8 netdev ifindex
4536 * +sound:card0 subsystem:devname
4537 */
4538 if (MAJOR(dev->devt)) {
4539 char c;
4540
4541 if (strcmp(subsys, "block") == 0)
4542 c = 'b';
4543 else
4544 c = 'c';
John Ogness74caba72020-09-21 13:24:45 +02064545
4546 snprintf(dev_info->device, sizeof(dev_info->device),
4547 "%c%u:%u", c, MAJOR(dev->devt), MINOR(dev->devt));
Kay Sieversc4e00da2012-05-03 02:29:59 +02004548 } else if (strcmp(subsys, "net") == 0) {
4549 struct net_device *net = to_net_dev(dev);
4550
John Ogness74caba72020-09-21 13:24:45 +02064551 snprintf(dev_info->device, sizeof(dev_info->device),
4552 "n%u", net->ifindex);
Kay Sieversc4e00da2012-05-03 02:29:59 +02004553 } else {
John Ogness74caba72020-09-21 13:24:45 +02064554 snprintf(dev_info->device, sizeof(dev_info->device),
4555 "+%s:%s", subsys, dev_name(dev));
Kay Sieversc4e00da2012-05-03 02:29:59 +02004556 }
Joe Perches99bcf212010-06-27 01:02:34 +00004557}
Joe Perches798efc62012-09-12 20:11:29 -07004558
Joe Perches05e4e5b2012-09-12 20:13:37 -07004559int dev_vprintk_emit(int level, const struct device *dev,
4560 const char *fmt, va_list args)
4561{
John Ogness74caba72020-09-21 13:24:45 +02064562 struct dev_printk_info dev_info;
Joe Perches05e4e5b2012-09-12 20:13:37 -07004563
John Ogness74caba72020-09-21 13:24:45 +02064564 set_dev_info(dev, &dev_info);
Joe Perches05e4e5b2012-09-12 20:13:37 -07004565
John Ogness74caba72020-09-21 13:24:45 +02064566 return vprintk_emit(0, level, &dev_info, fmt, args);
Joe Perches05e4e5b2012-09-12 20:13:37 -07004567}
4568EXPORT_SYMBOL(dev_vprintk_emit);
4569
4570int dev_printk_emit(int level, const struct device *dev, const char *fmt, ...)
4571{
4572 va_list args;
4573 int r;
4574
4575 va_start(args, fmt);
4576
4577 r = dev_vprintk_emit(level, dev, fmt, args);
4578
4579 va_end(args);
4580
4581 return r;
4582}
4583EXPORT_SYMBOL(dev_printk_emit);
4584
Joe Perchesd1f10522014-12-25 15:07:04 -08004585static void __dev_printk(const char *level, const struct device *dev,
Joe Perches798efc62012-09-12 20:11:29 -07004586 struct va_format *vaf)
4587{
Joe Perchesd1f10522014-12-25 15:07:04 -08004588 if (dev)
4589 dev_printk_emit(level[1] - '0', dev, "%s %s: %pV",
4590 dev_driver_string(dev), dev_name(dev), vaf);
4591 else
4592 printk("%s(NULL device *): %pV", level, vaf);
Joe Perches798efc62012-09-12 20:11:29 -07004593}
Joe Perches99bcf212010-06-27 01:02:34 +00004594
Chris Downad7d61f2021-06-15 17:52:56 +01004595void _dev_printk(const char *level, const struct device *dev,
4596 const char *fmt, ...)
Joe Perches99bcf212010-06-27 01:02:34 +00004597{
4598 struct va_format vaf;
4599 va_list args;
Joe Perches99bcf212010-06-27 01:02:34 +00004600
4601 va_start(args, fmt);
4602
4603 vaf.fmt = fmt;
4604 vaf.va = &args;
4605
Joe Perchesd1f10522014-12-25 15:07:04 -08004606 __dev_printk(level, dev, &vaf);
Joe Perches798efc62012-09-12 20:11:29 -07004607
Joe Perches99bcf212010-06-27 01:02:34 +00004608 va_end(args);
Joe Perches99bcf212010-06-27 01:02:34 +00004609}
Chris Downad7d61f2021-06-15 17:52:56 +01004610EXPORT_SYMBOL(_dev_printk);
Joe Perches99bcf212010-06-27 01:02:34 +00004611
4612#define define_dev_printk_level(func, kern_level) \
Joe Perchesd1f10522014-12-25 15:07:04 -08004613void func(const struct device *dev, const char *fmt, ...) \
Joe Perches99bcf212010-06-27 01:02:34 +00004614{ \
4615 struct va_format vaf; \
4616 va_list args; \
Joe Perches99bcf212010-06-27 01:02:34 +00004617 \
4618 va_start(args, fmt); \
4619 \
4620 vaf.fmt = fmt; \
4621 vaf.va = &args; \
4622 \
Joe Perchesd1f10522014-12-25 15:07:04 -08004623 __dev_printk(kern_level, dev, &vaf); \
Joe Perches798efc62012-09-12 20:11:29 -07004624 \
Joe Perches99bcf212010-06-27 01:02:34 +00004625 va_end(args); \
Joe Perches99bcf212010-06-27 01:02:34 +00004626} \
4627EXPORT_SYMBOL(func);
4628
Joe Perches663336e2018-05-09 08:15:46 -07004629define_dev_printk_level(_dev_emerg, KERN_EMERG);
4630define_dev_printk_level(_dev_alert, KERN_ALERT);
4631define_dev_printk_level(_dev_crit, KERN_CRIT);
4632define_dev_printk_level(_dev_err, KERN_ERR);
4633define_dev_printk_level(_dev_warn, KERN_WARNING);
4634define_dev_printk_level(_dev_notice, KERN_NOTICE);
Joe Perches99bcf212010-06-27 01:02:34 +00004635define_dev_printk_level(_dev_info, KERN_INFO);
4636
4637#endif
Rafael J. Wysocki97badf82015-04-03 23:23:37 +02004638
Andrzej Hajdaa787e542020-07-13 16:43:21 +02004639/**
4640 * dev_err_probe - probe error check and log helper
4641 * @dev: the pointer to the struct device
4642 * @err: error value to test
4643 * @fmt: printf-style format string
4644 * @...: arguments as specified in the format string
4645 *
4646 * This helper implements common pattern present in probe functions for error
4647 * checking: print debug or error message depending if the error value is
4648 * -EPROBE_DEFER and propagate error upwards.
Andrzej Hajdad090b702020-07-13 16:43:22 +02004649 * In case of -EPROBE_DEFER it sets also defer probe reason, which can be
4650 * checked later by reading devices_deferred debugfs attribute.
Mauro Carvalho Chehab074b3aa2020-09-09 11:53:43 +02004651 * It replaces code sequence::
4652 *
Andrzej Hajdaa787e542020-07-13 16:43:21 +02004653 * if (err != -EPROBE_DEFER)
4654 * dev_err(dev, ...);
4655 * else
4656 * dev_dbg(dev, ...);
4657 * return err;
Mauro Carvalho Chehab074b3aa2020-09-09 11:53:43 +02004658 *
4659 * with::
4660 *
Andrzej Hajdaa787e542020-07-13 16:43:21 +02004661 * return dev_err_probe(dev, err, ...);
4662 *
4663 * Returns @err.
4664 *
4665 */
4666int dev_err_probe(const struct device *dev, int err, const char *fmt, ...)
4667{
4668 struct va_format vaf;
4669 va_list args;
4670
4671 va_start(args, fmt);
4672 vaf.fmt = fmt;
4673 vaf.va = &args;
4674
Andrzej Hajdad090b702020-07-13 16:43:22 +02004675 if (err != -EPROBE_DEFER) {
Michał Mirosław693a8e92020-08-28 18:14:35 +02004676 dev_err(dev, "error %pe: %pV", ERR_PTR(err), &vaf);
Andrzej Hajdad090b702020-07-13 16:43:22 +02004677 } else {
4678 device_set_deferred_probe_reason(dev, &vaf);
Michał Mirosław693a8e92020-08-28 18:14:35 +02004679 dev_dbg(dev, "error %pe: %pV", ERR_PTR(err), &vaf);
Andrzej Hajdad090b702020-07-13 16:43:22 +02004680 }
Andrzej Hajdaa787e542020-07-13 16:43:21 +02004681
4682 va_end(args);
4683
4684 return err;
4685}
4686EXPORT_SYMBOL_GPL(dev_err_probe);
4687
Rafael J. Wysocki97badf82015-04-03 23:23:37 +02004688static inline bool fwnode_is_primary(struct fwnode_handle *fwnode)
4689{
4690 return fwnode && !IS_ERR(fwnode->secondary);
4691}
4692
4693/**
4694 * set_primary_fwnode - Change the primary firmware node of a given device.
4695 * @dev: Device to handle.
4696 * @fwnode: New primary firmware node of the device.
4697 *
4698 * Set the device's firmware node pointer to @fwnode, but if a secondary
4699 * firmware node of the device is present, preserve it.
Bard Liao3f7bdda2021-01-05 17:11:46 +08004700 *
4701 * Valid fwnode cases are:
4702 * - primary --> secondary --> -ENODEV
4703 * - primary --> NULL
4704 * - secondary --> -ENODEV
4705 * - NULL
Rafael J. Wysocki97badf82015-04-03 23:23:37 +02004706 */
4707void set_primary_fwnode(struct device *dev, struct fwnode_handle *fwnode)
4708{
Andy Shevchenko99aed922020-10-22 21:41:00 +03004709 struct device *parent = dev->parent;
Heikki Krogerusc15e1bd2020-08-21 13:53:42 +03004710 struct fwnode_handle *fn = dev->fwnode;
Rafael J. Wysocki97badf82015-04-03 23:23:37 +02004711
Heikki Krogerusc15e1bd2020-08-21 13:53:42 +03004712 if (fwnode) {
Rafael J. Wysocki97badf82015-04-03 23:23:37 +02004713 if (fwnode_is_primary(fn))
4714 fn = fn->secondary;
4715
Mika Westerberg55f89a82015-11-30 17:11:39 +02004716 if (fn) {
4717 WARN_ON(fwnode->secondary);
4718 fwnode->secondary = fn;
4719 }
Rafael J. Wysocki97badf82015-04-03 23:23:37 +02004720 dev->fwnode = fwnode;
4721 } else {
Heikki Krogerusc15e1bd2020-08-21 13:53:42 +03004722 if (fwnode_is_primary(fn)) {
4723 dev->fwnode = fn->secondary;
Bard Liao3f7bdda2021-01-05 17:11:46 +08004724 /* Set fn->secondary = NULL, so fn remains the primary fwnode */
Andy Shevchenko99aed922020-10-22 21:41:00 +03004725 if (!(parent && fn == parent->fwnode))
Bard Liao47f44692021-01-05 17:11:45 +08004726 fn->secondary = NULL;
Heikki Krogerusc15e1bd2020-08-21 13:53:42 +03004727 } else {
4728 dev->fwnode = NULL;
4729 }
Rafael J. Wysocki97badf82015-04-03 23:23:37 +02004730 }
4731}
4732EXPORT_SYMBOL_GPL(set_primary_fwnode);
4733
4734/**
4735 * set_secondary_fwnode - Change the secondary firmware node of a given device.
4736 * @dev: Device to handle.
4737 * @fwnode: New secondary firmware node of the device.
4738 *
4739 * If a primary firmware node of the device is present, set its secondary
4740 * pointer to @fwnode. Otherwise, set the device's firmware node pointer to
4741 * @fwnode.
4742 */
4743void set_secondary_fwnode(struct device *dev, struct fwnode_handle *fwnode)
4744{
4745 if (fwnode)
4746 fwnode->secondary = ERR_PTR(-ENODEV);
4747
4748 if (fwnode_is_primary(dev->fwnode))
4749 dev->fwnode->secondary = fwnode;
4750 else
4751 dev->fwnode = fwnode;
4752}
Andy Shevchenko96489ae2020-04-08 19:09:01 +03004753EXPORT_SYMBOL_GPL(set_secondary_fwnode);
Johan Hovold4e75e1d2017-06-06 17:59:00 +02004754
4755/**
4756 * device_set_of_node_from_dev - reuse device-tree node of another device
4757 * @dev: device whose device-tree node is being set
4758 * @dev2: device whose device-tree node is being reused
4759 *
4760 * Takes another reference to the new device-tree node after first dropping
4761 * any reference held to the old node.
4762 */
4763void device_set_of_node_from_dev(struct device *dev, const struct device *dev2)
4764{
4765 of_node_put(dev->of_node);
4766 dev->of_node = of_node_get(dev2->of_node);
4767 dev->of_node_reused = true;
4768}
4769EXPORT_SYMBOL_GPL(device_set_of_node_from_dev);
Suzuki K Poulose65b66682019-06-14 18:54:01 +01004770
Ioana Ciornei43e76d42021-06-17 15:29:04 +03004771void device_set_node(struct device *dev, struct fwnode_handle *fwnode)
4772{
4773 dev->fwnode = fwnode;
4774 dev->of_node = to_of_node(fwnode);
4775}
4776EXPORT_SYMBOL_GPL(device_set_node);
4777
Suzuki K Poulose6cda08a2019-07-23 23:18:32 +01004778int device_match_name(struct device *dev, const void *name)
4779{
4780 return sysfs_streq(dev_name(dev), name);
4781}
4782EXPORT_SYMBOL_GPL(device_match_name);
4783
Suzuki K Poulose65b66682019-06-14 18:54:01 +01004784int device_match_of_node(struct device *dev, const void *np)
4785{
4786 return dev->of_node == np;
4787}
4788EXPORT_SYMBOL_GPL(device_match_of_node);
Suzuki K Poulose67843bb2019-07-23 23:18:34 +01004789
4790int device_match_fwnode(struct device *dev, const void *fwnode)
4791{
4792 return dev_fwnode(dev) == fwnode;
4793}
4794EXPORT_SYMBOL_GPL(device_match_fwnode);
Suzuki K Poulose4495dfd2019-07-23 23:18:35 +01004795
4796int device_match_devt(struct device *dev, const void *pdevt)
4797{
4798 return dev->devt == *(dev_t *)pdevt;
4799}
4800EXPORT_SYMBOL_GPL(device_match_devt);
Suzuki K Poulose00500142019-07-23 23:18:36 +01004801
4802int device_match_acpi_dev(struct device *dev, const void *adev)
4803{
4804 return ACPI_COMPANION(dev) == adev;
4805}
4806EXPORT_SYMBOL(device_match_acpi_dev);
Suzuki K Poulose6bf85ba2019-07-23 23:18:37 +01004807
4808int device_match_any(struct device *dev, const void *unused)
4809{
4810 return 1;
4811}
4812EXPORT_SYMBOL_GPL(device_match_any);