blob: 9badd7f7fe62192136a6f4fccfad8d6c864f66a7 [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>
Greg Kroah-Hartman63967682013-08-27 10:24:15 -070030#include <linux/sysfs.h>
Christoph Hellwig6d4e9a82021-02-10 10:56:39 +010031#include <linux/dma-map-ops.h> /* for dma_default_coherent */
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
33#include "base.h"
34#include "power/power.h"
35
Andi Kleene52eec12010-09-08 16:54:17 +020036#ifdef CONFIG_SYSFS_DEPRECATED
37#ifdef CONFIG_SYSFS_DEPRECATED_V2
38long sysfs_deprecated = 1;
39#else
40long sysfs_deprecated = 0;
41#endif
Hanjun Guo3454bf92013-08-17 20:42:24 +080042static int __init sysfs_deprecated_setup(char *arg)
Andi Kleene52eec12010-09-08 16:54:17 +020043{
Jingoo Han34da5e62013-07-26 13:10:22 +090044 return kstrtol(arg, 10, &sysfs_deprecated);
Andi Kleene52eec12010-09-08 16:54:17 +020045}
46early_param("sysfs.deprecated", sysfs_deprecated_setup);
47#endif
48
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +010049/* Device links support. */
Saravana Kannanfc5a2512019-09-04 14:11:23 -070050static LIST_HEAD(deferred_sync);
51static unsigned int defer_sync_state_count = 1;
Saravana Kannan7b337cb2020-11-20 18:02:23 -080052static DEFINE_MUTEX(fwnode_link_lock);
Saravana Kannan25ac86c2020-11-20 18:02:28 -080053static bool fw_devlink_is_permissive(void);
Saravana Kannand46f3e32021-04-01 21:03:41 -070054static bool fw_devlink_drv_reg_done;
Saravana Kannan7b337cb2020-11-20 18:02:23 -080055
56/**
57 * fwnode_link_add - Create a link between two fwnode_handles.
58 * @con: Consumer end of the link.
59 * @sup: Supplier end of the link.
60 *
61 * Create a fwnode link between fwnode handles @con and @sup. The fwnode link
62 * represents the detail that the firmware lists @sup fwnode as supplying a
63 * resource to @con.
64 *
65 * The driver core will use the fwnode link to create a device link between the
66 * two device objects corresponding to @con and @sup when they are created. The
67 * driver core will automatically delete the fwnode link between @con and @sup
68 * after doing that.
69 *
70 * Attempts to create duplicate links between the same pair of fwnode handles
71 * are ignored and there is no reference counting.
72 */
73int fwnode_link_add(struct fwnode_handle *con, struct fwnode_handle *sup)
74{
75 struct fwnode_link *link;
76 int ret = 0;
77
78 mutex_lock(&fwnode_link_lock);
79
80 list_for_each_entry(link, &sup->consumers, s_hook)
81 if (link->consumer == con)
82 goto out;
83
84 link = kzalloc(sizeof(*link), GFP_KERNEL);
85 if (!link) {
86 ret = -ENOMEM;
87 goto out;
88 }
89
90 link->supplier = sup;
91 INIT_LIST_HEAD(&link->s_hook);
92 link->consumer = con;
93 INIT_LIST_HEAD(&link->c_hook);
94
95 list_add(&link->s_hook, &sup->consumers);
96 list_add(&link->c_hook, &con->suppliers);
97out:
98 mutex_unlock(&fwnode_link_lock);
99
100 return ret;
101}
102
103/**
104 * fwnode_links_purge_suppliers - Delete all supplier links of fwnode_handle.
105 * @fwnode: fwnode whose supplier links need to be deleted
106 *
107 * Deletes all supplier links connecting directly to @fwnode.
108 */
109static void fwnode_links_purge_suppliers(struct fwnode_handle *fwnode)
110{
111 struct fwnode_link *link, *tmp;
112
113 mutex_lock(&fwnode_link_lock);
114 list_for_each_entry_safe(link, tmp, &fwnode->suppliers, c_hook) {
115 list_del(&link->s_hook);
116 list_del(&link->c_hook);
117 kfree(link);
118 }
119 mutex_unlock(&fwnode_link_lock);
120}
121
122/**
123 * fwnode_links_purge_consumers - Delete all consumer links of fwnode_handle.
124 * @fwnode: fwnode whose consumer links need to be deleted
125 *
126 * Deletes all consumer links connecting directly to @fwnode.
127 */
128static void fwnode_links_purge_consumers(struct fwnode_handle *fwnode)
129{
130 struct fwnode_link *link, *tmp;
131
132 mutex_lock(&fwnode_link_lock);
133 list_for_each_entry_safe(link, tmp, &fwnode->consumers, s_hook) {
134 list_del(&link->s_hook);
135 list_del(&link->c_hook);
136 kfree(link);
137 }
138 mutex_unlock(&fwnode_link_lock);
139}
140
141/**
142 * fwnode_links_purge - Delete all links connected to a fwnode_handle.
143 * @fwnode: fwnode whose links needs to be deleted
144 *
145 * Deletes all links connecting directly to a fwnode.
146 */
147void fwnode_links_purge(struct fwnode_handle *fwnode)
148{
149 fwnode_links_purge_suppliers(fwnode);
150 fwnode_links_purge_consumers(fwnode);
151}
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100152
Saravana Kannan28ec3442021-05-05 17:44:22 -0700153void fw_devlink_purge_absent_suppliers(struct fwnode_handle *fwnode)
Saravana Kannan9528e0d2021-02-05 14:26:37 -0800154{
155 struct fwnode_handle *child;
156
157 /* Don't purge consumer links of an added child */
158 if (fwnode->dev)
159 return;
160
161 fwnode->flags |= FWNODE_FLAG_NOT_DEVICE;
162 fwnode_links_purge_consumers(fwnode);
163
164 fwnode_for_each_available_child_node(fwnode, child)
165 fw_devlink_purge_absent_suppliers(child);
166}
Saravana Kannan28ec3442021-05-05 17:44:22 -0700167EXPORT_SYMBOL_GPL(fw_devlink_purge_absent_suppliers);
Saravana Kannan9528e0d2021-02-05 14:26:37 -0800168
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100169#ifdef CONFIG_SRCU
170static DEFINE_MUTEX(device_links_lock);
171DEFINE_STATIC_SRCU(device_links_srcu);
172
173static inline void device_links_write_lock(void)
174{
175 mutex_lock(&device_links_lock);
176}
177
178static inline void device_links_write_unlock(void)
179{
180 mutex_unlock(&device_links_lock);
181}
182
Jules Irenge68464d72020-02-14 20:47:29 +0000183int device_links_read_lock(void) __acquires(&device_links_srcu)
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100184{
185 return srcu_read_lock(&device_links_srcu);
186}
187
Jules Irengeab7789c2020-02-14 20:47:30 +0000188void device_links_read_unlock(int idx) __releases(&device_links_srcu)
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100189{
190 srcu_read_unlock(&device_links_srcu, idx);
191}
Joel Fernandes (Google)c2fa1e12019-07-16 18:12:25 -0400192
193int device_links_read_lock_held(void)
194{
195 return srcu_read_lock_held(&device_links_srcu);
196}
Rafael J. Wysocki80dd33c2021-05-14 14:10:15 +0200197
198static void device_link_synchronize_removal(void)
199{
200 synchronize_srcu(&device_links_srcu);
201}
Rafael J. Wysocki0c871312021-05-14 14:11:19 +0200202
203static void device_link_remove_from_lists(struct device_link *link)
204{
205 list_del_rcu(&link->s_node);
206 list_del_rcu(&link->c_node);
207}
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100208#else /* !CONFIG_SRCU */
209static DECLARE_RWSEM(device_links_lock);
210
211static inline void device_links_write_lock(void)
212{
213 down_write(&device_links_lock);
214}
215
216static inline void device_links_write_unlock(void)
217{
218 up_write(&device_links_lock);
219}
220
221int device_links_read_lock(void)
222{
223 down_read(&device_links_lock);
224 return 0;
225}
226
227void device_links_read_unlock(int not_used)
228{
229 up_read(&device_links_lock);
230}
Joel Fernandes (Google)c2fa1e12019-07-16 18:12:25 -0400231
232#ifdef CONFIG_DEBUG_LOCK_ALLOC
233int device_links_read_lock_held(void)
234{
235 return lockdep_is_held(&device_links_lock);
236}
237#endif
Rafael J. Wysocki80dd33c2021-05-14 14:10:15 +0200238
239static inline void device_link_synchronize_removal(void)
240{
241}
Rafael J. Wysocki0c871312021-05-14 14:11:19 +0200242
243static void device_link_remove_from_lists(struct device_link *link)
244{
245 list_del(&link->s_node);
246 list_del(&link->c_node);
247}
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100248#endif /* !CONFIG_SRCU */
249
Rafael J. Wysocki3d1cf432021-01-15 19:30:51 +0100250static bool device_is_ancestor(struct device *dev, struct device *target)
251{
252 while (target->parent) {
253 target = target->parent;
254 if (dev == target)
255 return true;
256 }
257 return false;
258}
259
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100260/**
261 * device_is_dependent - Check if one device depends on another one
262 * @dev: Device to check dependencies for.
263 * @target: Device to check against.
264 *
265 * Check if @target depends on @dev or any device dependent on it (its child or
266 * its consumer etc). Return 1 if that is the case or 0 otherwise.
267 */
Saravana Kannan7d34ca32020-06-09 18:19:33 -0700268int device_is_dependent(struct device *dev, void *target)
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100269{
270 struct device_link *link;
271 int ret;
272
Rafael J. Wysocki3d1cf432021-01-15 19:30:51 +0100273 /*
274 * The "ancestors" check is needed to catch the case when the target
275 * device has not been completely initialized yet and it is still
276 * missing from the list of children of its parent device.
277 */
278 if (dev == target || device_is_ancestor(dev, target))
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100279 return 1;
280
281 ret = device_for_each_child(dev, target, device_is_dependent);
282 if (ret)
283 return ret;
284
285 list_for_each_entry(link, &dev->links.consumers, s_node) {
Saravana Kannan4b9bbb22020-12-17 19:17:00 -0800286 if ((link->flags & ~DL_FLAG_INFERRED) ==
287 (DL_FLAG_SYNC_STATE_ONLY | DL_FLAG_MANAGED))
Saravana Kannan05ef9832019-10-28 15:00:22 -0700288 continue;
289
Benjamin Gaignarde16f4f32018-07-16 13:37:44 +0200290 if (link->consumer == target)
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100291 return 1;
292
293 ret = device_is_dependent(link->consumer, target);
294 if (ret)
295 break;
296 }
297 return ret;
298}
299
Rafael J. Wysocki515db262019-07-16 17:21:06 +0200300static void device_link_init_status(struct device_link *link,
301 struct device *consumer,
302 struct device *supplier)
303{
304 switch (supplier->links.status) {
305 case DL_DEV_PROBING:
306 switch (consumer->links.status) {
307 case DL_DEV_PROBING:
308 /*
309 * A consumer driver can create a link to a supplier
310 * that has not completed its probing yet as long as it
311 * knows that the supplier is already functional (for
312 * example, it has just acquired some resources from the
313 * supplier).
314 */
315 link->status = DL_STATE_CONSUMER_PROBE;
316 break;
317 default:
318 link->status = DL_STATE_DORMANT;
319 break;
320 }
321 break;
322 case DL_DEV_DRIVER_BOUND:
323 switch (consumer->links.status) {
324 case DL_DEV_PROBING:
325 link->status = DL_STATE_CONSUMER_PROBE;
326 break;
327 case DL_DEV_DRIVER_BOUND:
328 link->status = DL_STATE_ACTIVE;
329 break;
330 default:
331 link->status = DL_STATE_AVAILABLE;
332 break;
333 }
334 break;
335 case DL_DEV_UNBINDING:
336 link->status = DL_STATE_SUPPLIER_UNBIND;
337 break;
338 default:
339 link->status = DL_STATE_DORMANT;
340 break;
341 }
342}
343
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100344static int device_reorder_to_tail(struct device *dev, void *not_used)
345{
346 struct device_link *link;
347
348 /*
349 * Devices that have not been registered yet will be put to the ends
350 * of the lists during the registration, so skip them here.
351 */
352 if (device_is_registered(dev))
353 devices_kset_move_last(dev);
354
355 if (device_pm_initialized(dev))
356 device_pm_move_last(dev);
357
358 device_for_each_child(dev, NULL, device_reorder_to_tail);
Saravana Kannan05ef9832019-10-28 15:00:22 -0700359 list_for_each_entry(link, &dev->links.consumers, s_node) {
Saravana Kannan4b9bbb22020-12-17 19:17:00 -0800360 if ((link->flags & ~DL_FLAG_INFERRED) ==
361 (DL_FLAG_SYNC_STATE_ONLY | DL_FLAG_MANAGED))
Saravana Kannan05ef9832019-10-28 15:00:22 -0700362 continue;
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100363 device_reorder_to_tail(link->consumer, NULL);
Saravana Kannan05ef9832019-10-28 15:00:22 -0700364 }
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100365
366 return 0;
367}
368
369/**
Feng Kan494fd7b2018-04-10 16:57:06 -0700370 * device_pm_move_to_tail - Move set of devices to the end of device lists
371 * @dev: Device to move
372 *
373 * This is a device_reorder_to_tail() wrapper taking the requisite locks.
374 *
375 * It moves the @dev along with all of its children and all of its consumers
376 * to the ends of the device_kset and dpm_list, recursively.
377 */
378void device_pm_move_to_tail(struct device *dev)
379{
380 int idx;
381
382 idx = device_links_read_lock();
383 device_pm_lock();
384 device_reorder_to_tail(dev, NULL);
385 device_pm_unlock();
386 device_links_read_unlock(idx);
387}
388
Saravana Kannan287905e2020-05-21 12:17:58 -0700389#define to_devlink(dev) container_of((dev), struct device_link, link_dev)
390
391static ssize_t status_show(struct device *dev,
Joe Perches948b3ed2020-09-16 13:40:42 -0700392 struct device_attribute *attr, char *buf)
Saravana Kannan287905e2020-05-21 12:17:58 -0700393{
Joe Perches948b3ed2020-09-16 13:40:42 -0700394 const char *output;
Saravana Kannan287905e2020-05-21 12:17:58 -0700395
396 switch (to_devlink(dev)->status) {
397 case DL_STATE_NONE:
Joe Perches948b3ed2020-09-16 13:40:42 -0700398 output = "not tracked";
399 break;
Saravana Kannan287905e2020-05-21 12:17:58 -0700400 case DL_STATE_DORMANT:
Joe Perches948b3ed2020-09-16 13:40:42 -0700401 output = "dormant";
402 break;
Saravana Kannan287905e2020-05-21 12:17:58 -0700403 case DL_STATE_AVAILABLE:
Joe Perches948b3ed2020-09-16 13:40:42 -0700404 output = "available";
405 break;
Saravana Kannan287905e2020-05-21 12:17:58 -0700406 case DL_STATE_CONSUMER_PROBE:
Joe Perches948b3ed2020-09-16 13:40:42 -0700407 output = "consumer probing";
408 break;
Saravana Kannan287905e2020-05-21 12:17:58 -0700409 case DL_STATE_ACTIVE:
Joe Perches948b3ed2020-09-16 13:40:42 -0700410 output = "active";
411 break;
Saravana Kannan287905e2020-05-21 12:17:58 -0700412 case DL_STATE_SUPPLIER_UNBIND:
Joe Perches948b3ed2020-09-16 13:40:42 -0700413 output = "supplier unbinding";
414 break;
Saravana Kannan287905e2020-05-21 12:17:58 -0700415 default:
Joe Perches948b3ed2020-09-16 13:40:42 -0700416 output = "unknown";
417 break;
Saravana Kannan287905e2020-05-21 12:17:58 -0700418 }
Joe Perches948b3ed2020-09-16 13:40:42 -0700419
420 return sysfs_emit(buf, "%s\n", output);
Saravana Kannan287905e2020-05-21 12:17:58 -0700421}
422static DEVICE_ATTR_RO(status);
423
424static ssize_t auto_remove_on_show(struct device *dev,
425 struct device_attribute *attr, char *buf)
426{
427 struct device_link *link = to_devlink(dev);
Joe Perches973c3912020-09-16 13:40:40 -0700428 const char *output;
Saravana Kannan287905e2020-05-21 12:17:58 -0700429
430 if (link->flags & DL_FLAG_AUTOREMOVE_SUPPLIER)
Joe Perches973c3912020-09-16 13:40:40 -0700431 output = "supplier unbind";
Saravana Kannan287905e2020-05-21 12:17:58 -0700432 else if (link->flags & DL_FLAG_AUTOREMOVE_CONSUMER)
Joe Perches973c3912020-09-16 13:40:40 -0700433 output = "consumer unbind";
Saravana Kannan287905e2020-05-21 12:17:58 -0700434 else
Joe Perches973c3912020-09-16 13:40:40 -0700435 output = "never";
Saravana Kannan287905e2020-05-21 12:17:58 -0700436
Joe Perches973c3912020-09-16 13:40:40 -0700437 return sysfs_emit(buf, "%s\n", output);
Saravana Kannan287905e2020-05-21 12:17:58 -0700438}
439static DEVICE_ATTR_RO(auto_remove_on);
440
441static ssize_t runtime_pm_show(struct device *dev,
442 struct device_attribute *attr, char *buf)
443{
444 struct device_link *link = to_devlink(dev);
445
Joe Perchesaa838892020-09-16 13:40:39 -0700446 return sysfs_emit(buf, "%d\n", !!(link->flags & DL_FLAG_PM_RUNTIME));
Saravana Kannan287905e2020-05-21 12:17:58 -0700447}
448static DEVICE_ATTR_RO(runtime_pm);
449
450static ssize_t sync_state_only_show(struct device *dev,
451 struct device_attribute *attr, char *buf)
452{
453 struct device_link *link = to_devlink(dev);
454
Joe Perchesaa838892020-09-16 13:40:39 -0700455 return sysfs_emit(buf, "%d\n",
456 !!(link->flags & DL_FLAG_SYNC_STATE_ONLY));
Saravana Kannan287905e2020-05-21 12:17:58 -0700457}
458static DEVICE_ATTR_RO(sync_state_only);
459
460static struct attribute *devlink_attrs[] = {
461 &dev_attr_status.attr,
462 &dev_attr_auto_remove_on.attr,
463 &dev_attr_runtime_pm.attr,
464 &dev_attr_sync_state_only.attr,
465 NULL,
466};
467ATTRIBUTE_GROUPS(devlink);
468
Rafael J. Wysocki80dd33c2021-05-14 14:10:15 +0200469static void device_link_release_fn(struct work_struct *work)
Saravana Kannan843e6002020-07-16 14:45:23 -0700470{
Rafael J. Wysocki80dd33c2021-05-14 14:10:15 +0200471 struct device_link *link = container_of(work, struct device_link, rm_work);
472
473 /* Ensure that all references to the link object have been dropped. */
474 device_link_synchronize_removal();
475
Saravana Kannan843e6002020-07-16 14:45:23 -0700476 while (refcount_dec_not_one(&link->rpm_active))
477 pm_runtime_put(link->supplier);
478
479 put_device(link->consumer);
480 put_device(link->supplier);
481 kfree(link);
482}
483
Saravana Kannan287905e2020-05-21 12:17:58 -0700484static void devlink_dev_release(struct device *dev)
485{
Saravana Kannan843e6002020-07-16 14:45:23 -0700486 struct device_link *link = to_devlink(dev);
487
Rafael J. Wysocki80dd33c2021-05-14 14:10:15 +0200488 INIT_WORK(&link->rm_work, device_link_release_fn);
489 /*
490 * It may take a while to complete this work because of the SRCU
491 * synchronization in device_link_release_fn() and if the consumer or
492 * supplier devices get deleted when it runs, so put it into the "long"
493 * workqueue.
494 */
495 queue_work(system_long_wq, &link->rm_work);
Saravana Kannan287905e2020-05-21 12:17:58 -0700496}
497
498static struct class devlink_class = {
499 .name = "devlink",
500 .owner = THIS_MODULE,
501 .dev_groups = devlink_groups,
502 .dev_release = devlink_dev_release,
503};
504
505static int devlink_add_symlinks(struct device *dev,
506 struct class_interface *class_intf)
507{
508 int ret;
509 size_t len;
510 struct device_link *link = to_devlink(dev);
511 struct device *sup = link->supplier;
512 struct device *con = link->consumer;
513 char *buf;
514
Saravana Kannane020ff62021-01-10 09:54:07 -0800515 len = max(strlen(dev_bus_name(sup)) + strlen(dev_name(sup)),
516 strlen(dev_bus_name(con)) + strlen(dev_name(con)));
517 len += strlen(":");
Saravana Kannan287905e2020-05-21 12:17:58 -0700518 len += strlen("supplier:") + 1;
519 buf = kzalloc(len, GFP_KERNEL);
520 if (!buf)
521 return -ENOMEM;
522
523 ret = sysfs_create_link(&link->link_dev.kobj, &sup->kobj, "supplier");
524 if (ret)
525 goto out;
526
527 ret = sysfs_create_link(&link->link_dev.kobj, &con->kobj, "consumer");
528 if (ret)
529 goto err_con;
530
Saravana Kannane020ff62021-01-10 09:54:07 -0800531 snprintf(buf, len, "consumer:%s:%s", dev_bus_name(con), dev_name(con));
Saravana Kannan287905e2020-05-21 12:17:58 -0700532 ret = sysfs_create_link(&sup->kobj, &link->link_dev.kobj, buf);
533 if (ret)
534 goto err_con_dev;
535
Saravana Kannane020ff62021-01-10 09:54:07 -0800536 snprintf(buf, len, "supplier:%s:%s", dev_bus_name(sup), dev_name(sup));
Saravana Kannan287905e2020-05-21 12:17:58 -0700537 ret = sysfs_create_link(&con->kobj, &link->link_dev.kobj, buf);
538 if (ret)
539 goto err_sup_dev;
540
541 goto out;
542
543err_sup_dev:
Saravana Kannane020ff62021-01-10 09:54:07 -0800544 snprintf(buf, len, "consumer:%s:%s", dev_bus_name(con), dev_name(con));
Saravana Kannan287905e2020-05-21 12:17:58 -0700545 sysfs_remove_link(&sup->kobj, buf);
546err_con_dev:
547 sysfs_remove_link(&link->link_dev.kobj, "consumer");
548err_con:
549 sysfs_remove_link(&link->link_dev.kobj, "supplier");
550out:
551 kfree(buf);
552 return ret;
553}
554
555static void devlink_remove_symlinks(struct device *dev,
556 struct class_interface *class_intf)
557{
558 struct device_link *link = to_devlink(dev);
559 size_t len;
560 struct device *sup = link->supplier;
561 struct device *con = link->consumer;
562 char *buf;
563
564 sysfs_remove_link(&link->link_dev.kobj, "consumer");
565 sysfs_remove_link(&link->link_dev.kobj, "supplier");
566
Saravana Kannane020ff62021-01-10 09:54:07 -0800567 len = max(strlen(dev_bus_name(sup)) + strlen(dev_name(sup)),
568 strlen(dev_bus_name(con)) + strlen(dev_name(con)));
569 len += strlen(":");
Saravana Kannan287905e2020-05-21 12:17:58 -0700570 len += strlen("supplier:") + 1;
571 buf = kzalloc(len, GFP_KERNEL);
572 if (!buf) {
573 WARN(1, "Unable to properly free device link symlinks!\n");
574 return;
575 }
576
Saravana Kannane020ff62021-01-10 09:54:07 -0800577 snprintf(buf, len, "supplier:%s:%s", dev_bus_name(sup), dev_name(sup));
Saravana Kannan287905e2020-05-21 12:17:58 -0700578 sysfs_remove_link(&con->kobj, buf);
Saravana Kannane020ff62021-01-10 09:54:07 -0800579 snprintf(buf, len, "consumer:%s:%s", dev_bus_name(con), dev_name(con));
Saravana Kannan287905e2020-05-21 12:17:58 -0700580 sysfs_remove_link(&sup->kobj, buf);
581 kfree(buf);
582}
583
584static struct class_interface devlink_class_intf = {
585 .class = &devlink_class,
586 .add_dev = devlink_add_symlinks,
587 .remove_dev = devlink_remove_symlinks,
588};
589
590static int __init devlink_class_init(void)
591{
592 int ret;
593
594 ret = class_register(&devlink_class);
595 if (ret)
596 return ret;
597
598 ret = class_interface_register(&devlink_class_intf);
599 if (ret)
600 class_unregister(&devlink_class);
601
602 return ret;
603}
604postcore_initcall(devlink_class_init);
605
Rafael J. Wysocki515db262019-07-16 17:21:06 +0200606#define DL_MANAGED_LINK_FLAGS (DL_FLAG_AUTOREMOVE_CONSUMER | \
607 DL_FLAG_AUTOREMOVE_SUPPLIER | \
Saravana Kannan05ef9832019-10-28 15:00:22 -0700608 DL_FLAG_AUTOPROBE_CONSUMER | \
Saravana Kannan4b9bbb22020-12-17 19:17:00 -0800609 DL_FLAG_SYNC_STATE_ONLY | \
610 DL_FLAG_INFERRED)
Rafael J. Wysocki515db262019-07-16 17:21:06 +0200611
Rafael J. Wysockifb583c82019-07-30 11:28:57 +0200612#define DL_ADD_VALID_FLAGS (DL_MANAGED_LINK_FLAGS | DL_FLAG_STATELESS | \
613 DL_FLAG_PM_RUNTIME | DL_FLAG_RPM_ACTIVE)
614
Feng Kan494fd7b2018-04-10 16:57:06 -0700615/**
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100616 * device_link_add - Create a link between two devices.
617 * @consumer: Consumer end of the link.
618 * @supplier: Supplier end of the link.
619 * @flags: Link flags.
620 *
Rafael J. Wysocki21d5c572016-10-30 17:32:31 +0100621 * The caller is responsible for the proper synchronization of the link creation
622 * with runtime PM. First, setting the DL_FLAG_PM_RUNTIME flag will cause the
623 * runtime PM framework to take the link into account. Second, if the
624 * DL_FLAG_RPM_ACTIVE flag is set in addition to it, the supplier devices will
Thierry Redingd475f8e2020-11-27 11:46:30 +0100625 * be forced into the active meta state and reference-counted upon the creation
Rafael J. Wysocki21d5c572016-10-30 17:32:31 +0100626 * of the link. If DL_FLAG_PM_RUNTIME is not set, DL_FLAG_RPM_ACTIVE will be
627 * ignored.
628 *
Rafael J. Wysocki515db262019-07-16 17:21:06 +0200629 * If DL_FLAG_STATELESS is set in @flags, the caller of this function is
630 * expected to release the link returned by it directly with the help of either
631 * device_link_del() or device_link_remove().
Rafael J. Wysocki72175d42019-02-01 01:58:33 +0100632 *
633 * If that flag is not set, however, the caller of this function is handing the
634 * management of the link over to the driver core entirely and its return value
635 * can only be used to check whether or not the link is present. In that case,
636 * the DL_FLAG_AUTOREMOVE_CONSUMER and DL_FLAG_AUTOREMOVE_SUPPLIER device link
637 * flags can be used to indicate to the driver core when the link can be safely
638 * deleted. Namely, setting one of them in @flags indicates to the driver core
639 * that the link is not going to be used (by the given caller of this function)
640 * after unbinding the consumer or supplier driver, respectively, from its
641 * device, so the link can be deleted at that point. If none of them is set,
642 * the link will be maintained until one of the devices pointed to by it (either
643 * the consumer or the supplier) is unregistered.
Rafael J. Wysockic8d50982019-02-01 01:45:55 +0100644 *
Rafael J. Wysockie7dd4012019-02-01 01:59:42 +0100645 * Also, if DL_FLAG_STATELESS, DL_FLAG_AUTOREMOVE_CONSUMER and
646 * DL_FLAG_AUTOREMOVE_SUPPLIER are not set in @flags (that is, a persistent
647 * managed device link is being added), the DL_FLAG_AUTOPROBE_CONSUMER flag can
Thierry Redingd475f8e2020-11-27 11:46:30 +0100648 * be used to request the driver core to automatically probe for a consumer
Rafael J. Wysockie7dd4012019-02-01 01:59:42 +0100649 * driver after successfully binding a driver to the supplier device.
650 *
Rafael J. Wysocki515db262019-07-16 17:21:06 +0200651 * The combination of DL_FLAG_STATELESS and one of DL_FLAG_AUTOREMOVE_CONSUMER,
652 * DL_FLAG_AUTOREMOVE_SUPPLIER, or DL_FLAG_AUTOPROBE_CONSUMER set in @flags at
653 * the same time is invalid and will cause NULL to be returned upfront.
654 * However, if a device link between the given @consumer and @supplier pair
655 * exists already when this function is called for them, the existing link will
656 * be returned regardless of its current type and status (the link's flags may
657 * be modified then). The caller of this function is then expected to treat
658 * the link as though it has just been created, so (in particular) if
659 * DL_FLAG_STATELESS was passed in @flags, the link needs to be released
660 * explicitly when not needed any more (as stated above).
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100661 *
662 * A side effect of the link creation is re-ordering of dpm_list and the
663 * devices_kset list by moving the consumer device and all devices depending
664 * on it to the ends of these lists (that does not happen to devices that have
665 * not been registered when this function is called).
666 *
667 * The supplier device is required to be registered when this function is called
668 * and NULL will be returned if that is not the case. The consumer device need
Lukas Wunner64df1142016-12-04 13:10:04 +0100669 * not be registered, however.
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100670 */
671struct device_link *device_link_add(struct device *consumer,
672 struct device *supplier, u32 flags)
673{
674 struct device_link *link;
675
Rafael J. Wysockifb583c82019-07-30 11:28:57 +0200676 if (!consumer || !supplier || flags & ~DL_ADD_VALID_FLAGS ||
Rafael J. Wysocki515db262019-07-16 17:21:06 +0200677 (flags & DL_FLAG_STATELESS && flags & DL_MANAGED_LINK_FLAGS) ||
Saravana Kannan05ef9832019-10-28 15:00:22 -0700678 (flags & DL_FLAG_SYNC_STATE_ONLY &&
Saravana Kannan4b9bbb22020-12-17 19:17:00 -0800679 (flags & ~DL_FLAG_INFERRED) != DL_FLAG_SYNC_STATE_ONLY) ||
Rafael J. Wysockie7dd4012019-02-01 01:59:42 +0100680 (flags & DL_FLAG_AUTOPROBE_CONSUMER &&
681 flags & (DL_FLAG_AUTOREMOVE_CONSUMER |
682 DL_FLAG_AUTOREMOVE_SUPPLIER)))
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100683 return NULL;
684
Rafael J. Wysocki5db25c92019-02-01 01:47:53 +0100685 if (flags & DL_FLAG_PM_RUNTIME && flags & DL_FLAG_RPM_ACTIVE) {
686 if (pm_runtime_get_sync(supplier) < 0) {
687 pm_runtime_put_noidle(supplier);
688 return NULL;
689 }
Rafael J. Wysocki5db25c92019-02-01 01:47:53 +0100690 }
691
Rafael J. Wysocki515db262019-07-16 17:21:06 +0200692 if (!(flags & DL_FLAG_STATELESS))
693 flags |= DL_FLAG_MANAGED;
694
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100695 device_links_write_lock();
696 device_pm_lock();
697
698 /*
699 * If the supplier has not been fully registered yet or there is a
Saravana Kannan05ef9832019-10-28 15:00:22 -0700700 * reverse (non-SYNC_STATE_ONLY) dependency between the consumer and
701 * the supplier already in the graph, return NULL. If the link is a
702 * SYNC_STATE_ONLY link, we don't check for reverse dependencies
703 * because it only affects sync_state() callbacks.
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100704 */
705 if (!device_pm_initialized(supplier)
Saravana Kannan05ef9832019-10-28 15:00:22 -0700706 || (!(flags & DL_FLAG_SYNC_STATE_ONLY) &&
707 device_is_dependent(consumer, supplier))) {
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100708 link = NULL;
709 goto out;
710 }
711
Rafael J. Wysocki72175d42019-02-01 01:58:33 +0100712 /*
Saravana Kannanac66c5b2020-11-20 18:02:24 -0800713 * SYNC_STATE_ONLY links are useless once a consumer device has probed.
714 * So, only create it if the consumer hasn't probed yet.
715 */
716 if (flags & DL_FLAG_SYNC_STATE_ONLY &&
717 consumer->links.status != DL_DEV_NO_DRIVER &&
718 consumer->links.status != DL_DEV_PROBING) {
719 link = NULL;
720 goto out;
721 }
722
723 /*
Rafael J. Wysocki72175d42019-02-01 01:58:33 +0100724 * DL_FLAG_AUTOREMOVE_SUPPLIER indicates that the link will be needed
725 * longer than for DL_FLAG_AUTOREMOVE_CONSUMER and setting them both
726 * together doesn't make sense, so prefer DL_FLAG_AUTOREMOVE_SUPPLIER.
727 */
728 if (flags & DL_FLAG_AUTOREMOVE_SUPPLIER)
729 flags &= ~DL_FLAG_AUTOREMOVE_CONSUMER;
730
Rafael J. Wysockif265df52019-02-01 01:46:54 +0100731 list_for_each_entry(link, &supplier->links.consumers, s_node) {
732 if (link->consumer != consumer)
733 continue;
734
Saravana Kannan4b9bbb22020-12-17 19:17:00 -0800735 if (link->flags & DL_FLAG_INFERRED &&
736 !(flags & DL_FLAG_INFERRED))
737 link->flags &= ~DL_FLAG_INFERRED;
738
Rafael J. Wysockie2f3cd82019-02-01 01:49:14 +0100739 if (flags & DL_FLAG_PM_RUNTIME) {
740 if (!(link->flags & DL_FLAG_PM_RUNTIME)) {
Rafael J. Wysocki4c06c4e2019-02-12 13:08:10 +0100741 pm_runtime_new_link(consumer);
Rafael J. Wysockie2f3cd82019-02-01 01:49:14 +0100742 link->flags |= DL_FLAG_PM_RUNTIME;
743 }
744 if (flags & DL_FLAG_RPM_ACTIVE)
Rafael J. Wysocki36003d42019-02-19 17:53:26 +0100745 refcount_inc(&link->rpm_active);
Rafael J. Wysockie2f3cd82019-02-01 01:49:14 +0100746 }
747
Rafael J. Wysocki72175d42019-02-01 01:58:33 +0100748 if (flags & DL_FLAG_STATELESS) {
749 kref_get(&link->kref);
Saravana Kannan05ef9832019-10-28 15:00:22 -0700750 if (link->flags & DL_FLAG_SYNC_STATE_ONLY &&
Saravana Kannan44e96042020-05-19 21:36:26 -0700751 !(link->flags & DL_FLAG_STATELESS)) {
752 link->flags |= DL_FLAG_STATELESS;
Saravana Kannan05ef9832019-10-28 15:00:22 -0700753 goto reorder;
Saravana Kannan44e96042020-05-19 21:36:26 -0700754 } else {
755 link->flags |= DL_FLAG_STATELESS;
Saravana Kannan05ef9832019-10-28 15:00:22 -0700756 goto out;
Saravana Kannan44e96042020-05-19 21:36:26 -0700757 }
Rafael J. Wysocki72175d42019-02-01 01:58:33 +0100758 }
759
760 /*
761 * If the life time of the link following from the new flags is
762 * longer than indicated by the flags of the existing link,
763 * update the existing link to stay around longer.
764 */
765 if (flags & DL_FLAG_AUTOREMOVE_SUPPLIER) {
766 if (link->flags & DL_FLAG_AUTOREMOVE_CONSUMER) {
767 link->flags &= ~DL_FLAG_AUTOREMOVE_CONSUMER;
768 link->flags |= DL_FLAG_AUTOREMOVE_SUPPLIER;
769 }
770 } else if (!(flags & DL_FLAG_AUTOREMOVE_CONSUMER)) {
771 link->flags &= ~(DL_FLAG_AUTOREMOVE_CONSUMER |
772 DL_FLAG_AUTOREMOVE_SUPPLIER);
773 }
Rafael J. Wysocki515db262019-07-16 17:21:06 +0200774 if (!(link->flags & DL_FLAG_MANAGED)) {
775 kref_get(&link->kref);
776 link->flags |= DL_FLAG_MANAGED;
777 device_link_init_status(link, consumer, supplier);
778 }
Saravana Kannan05ef9832019-10-28 15:00:22 -0700779 if (link->flags & DL_FLAG_SYNC_STATE_ONLY &&
780 !(flags & DL_FLAG_SYNC_STATE_ONLY)) {
781 link->flags &= ~DL_FLAG_SYNC_STATE_ONLY;
782 goto reorder;
783 }
784
Rafael J. Wysockif265df52019-02-01 01:46:54 +0100785 goto out;
786 }
787
Rafael J. Wysocki21d5c572016-10-30 17:32:31 +0100788 link = kzalloc(sizeof(*link), GFP_KERNEL);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100789 if (!link)
790 goto out;
791
Rafael J. Wysockie2f3cd82019-02-01 01:49:14 +0100792 refcount_set(&link->rpm_active, 1);
793
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100794 get_device(supplier);
795 link->supplier = supplier;
796 INIT_LIST_HEAD(&link->s_node);
797 get_device(consumer);
798 link->consumer = consumer;
799 INIT_LIST_HEAD(&link->c_node);
800 link->flags = flags;
Lukas Wunneread18c22018-02-10 19:27:12 +0100801 kref_init(&link->kref);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100802
Saravana Kannan287905e2020-05-21 12:17:58 -0700803 link->link_dev.class = &devlink_class;
804 device_set_pm_not_required(&link->link_dev);
Saravana Kannane020ff62021-01-10 09:54:07 -0800805 dev_set_name(&link->link_dev, "%s:%s--%s:%s",
806 dev_bus_name(supplier), dev_name(supplier),
807 dev_bus_name(consumer), dev_name(consumer));
Saravana Kannan287905e2020-05-21 12:17:58 -0700808 if (device_register(&link->link_dev)) {
809 put_device(consumer);
810 put_device(supplier);
811 kfree(link);
812 link = NULL;
813 goto out;
814 }
815
816 if (flags & DL_FLAG_PM_RUNTIME) {
817 if (flags & DL_FLAG_RPM_ACTIVE)
818 refcount_inc(&link->rpm_active);
819
820 pm_runtime_new_link(consumer);
821 }
822
Lukas Wunner64df1142016-12-04 13:10:04 +0100823 /* Determine the initial link state. */
Rafael J. Wysocki515db262019-07-16 17:21:06 +0200824 if (flags & DL_FLAG_STATELESS)
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100825 link->status = DL_STATE_NONE;
Rafael J. Wysocki515db262019-07-16 17:21:06 +0200826 else
827 device_link_init_status(link, consumer, supplier);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100828
829 /*
Rafael J. Wysocki15cfb092019-02-01 01:50:39 +0100830 * Some callers expect the link creation during consumer driver probe to
831 * resume the supplier even without DL_FLAG_RPM_ACTIVE.
832 */
833 if (link->status == DL_STATE_CONSUMER_PROBE &&
834 flags & DL_FLAG_PM_RUNTIME)
835 pm_runtime_resume(supplier);
836
Saravana Kannan21c27f02020-05-18 23:30:00 -0700837 list_add_tail_rcu(&link->s_node, &supplier->links.consumers);
838 list_add_tail_rcu(&link->c_node, &consumer->links.suppliers);
839
Saravana Kannan05ef9832019-10-28 15:00:22 -0700840 if (flags & DL_FLAG_SYNC_STATE_ONLY) {
841 dev_dbg(consumer,
842 "Linked as a sync state only consumer to %s\n",
843 dev_name(supplier));
844 goto out;
845 }
Saravana Kannan21c27f02020-05-18 23:30:00 -0700846
Saravana Kannan05ef9832019-10-28 15:00:22 -0700847reorder:
Rafael J. Wysocki15cfb092019-02-01 01:50:39 +0100848 /*
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100849 * Move the consumer and all of the devices depending on it to the end
850 * of dpm_list and the devices_kset list.
851 *
852 * It is necessary to hold dpm_list locked throughout all that or else
853 * we may end up suspending with a wrong ordering of it.
854 */
855 device_reorder_to_tail(consumer, NULL);
856
Jerome Brunet8a4b3262018-12-21 17:23:41 +0100857 dev_dbg(consumer, "Linked as a consumer to %s\n", dev_name(supplier));
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100858
Saravana Kannan21c27f02020-05-18 23:30:00 -0700859out:
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100860 device_pm_unlock();
861 device_links_write_unlock();
Rafael J. Wysocki5db25c92019-02-01 01:47:53 +0100862
Rafael J. Wysockie2f3cd82019-02-01 01:49:14 +0100863 if ((flags & DL_FLAG_PM_RUNTIME && flags & DL_FLAG_RPM_ACTIVE) && !link)
Rafael J. Wysocki5db25c92019-02-01 01:47:53 +0100864 pm_runtime_put(supplier);
865
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100866 return link;
867}
868EXPORT_SYMBOL_GPL(device_link_add);
869
Lukas Wunneread18c22018-02-10 19:27:12 +0100870static void __device_link_del(struct kref *kref)
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100871{
Lukas Wunneread18c22018-02-10 19:27:12 +0100872 struct device_link *link = container_of(kref, struct device_link, kref);
873
Jerome Brunet8a4b3262018-12-21 17:23:41 +0100874 dev_dbg(link->consumer, "Dropping the link to %s\n",
875 dev_name(link->supplier));
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100876
Rafael J. Wysockie0e398e2020-10-21 21:12:15 +0200877 pm_runtime_drop_link(link);
Rafael J. Wysockibaa88092016-10-30 17:32:43 +0100878
Rafael J. Wysocki0c871312021-05-14 14:11:19 +0200879 device_link_remove_from_lists(link);
Saravana Kannan843e6002020-07-16 14:45:23 -0700880 device_unregister(&link->link_dev);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100881}
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100882
Rafael J. Wysocki72175d42019-02-01 01:58:33 +0100883static void device_link_put_kref(struct device_link *link)
884{
885 if (link->flags & DL_FLAG_STATELESS)
886 kref_put(&link->kref, __device_link_del);
Adrian Hunterbf259672021-08-06 16:04:41 +0300887 else if (!device_is_registered(link->consumer))
888 __device_link_del(&link->kref);
Rafael J. Wysocki72175d42019-02-01 01:58:33 +0100889 else
890 WARN(1, "Unable to drop a managed device link reference\n");
891}
892
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100893/**
Rafael J. Wysocki72175d42019-02-01 01:58:33 +0100894 * device_link_del - Delete a stateless link between two devices.
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100895 * @link: Device link to delete.
896 *
897 * The caller must ensure proper synchronization of this function with runtime
Lukas Wunneread18c22018-02-10 19:27:12 +0100898 * PM. If the link was added multiple times, it needs to be deleted as often.
899 * Care is required for hotplugged devices: Their links are purged on removal
900 * and calling device_link_del() is then no longer allowed.
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100901 */
902void device_link_del(struct device_link *link)
903{
904 device_links_write_lock();
Rafael J. Wysocki72175d42019-02-01 01:58:33 +0100905 device_link_put_kref(link);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100906 device_links_write_unlock();
907}
908EXPORT_SYMBOL_GPL(device_link_del);
909
pascal pailletd8842212018-07-05 14:25:56 +0000910/**
Rafael J. Wysocki72175d42019-02-01 01:58:33 +0100911 * device_link_remove - Delete a stateless link between two devices.
pascal pailletd8842212018-07-05 14:25:56 +0000912 * @consumer: Consumer end of the link.
913 * @supplier: Supplier end of the link.
914 *
915 * The caller must ensure proper synchronization of this function with runtime
916 * PM.
917 */
918void device_link_remove(void *consumer, struct device *supplier)
919{
920 struct device_link *link;
921
922 if (WARN_ON(consumer == supplier))
923 return;
924
925 device_links_write_lock();
pascal pailletd8842212018-07-05 14:25:56 +0000926
927 list_for_each_entry(link, &supplier->links.consumers, s_node) {
928 if (link->consumer == consumer) {
Rafael J. Wysocki72175d42019-02-01 01:58:33 +0100929 device_link_put_kref(link);
pascal pailletd8842212018-07-05 14:25:56 +0000930 break;
931 }
932 }
933
pascal pailletd8842212018-07-05 14:25:56 +0000934 device_links_write_unlock();
935}
936EXPORT_SYMBOL_GPL(device_link_remove);
937
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100938static void device_links_missing_supplier(struct device *dev)
939{
940 struct device_link *link;
941
Saravana Kannan8c3e3152020-05-26 15:09:27 -0700942 list_for_each_entry(link, &dev->links.suppliers, c_node) {
943 if (link->status != DL_STATE_CONSUMER_PROBE)
944 continue;
945
946 if (link->supplier->links.status == DL_DEV_DRIVER_BOUND) {
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100947 WRITE_ONCE(link->status, DL_STATE_AVAILABLE);
Saravana Kannan8c3e3152020-05-26 15:09:27 -0700948 } else {
949 WARN_ON(!(link->flags & DL_FLAG_SYNC_STATE_ONLY));
950 WRITE_ONCE(link->status, DL_STATE_DORMANT);
951 }
952 }
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100953}
954
955/**
956 * device_links_check_suppliers - Check presence of supplier drivers.
957 * @dev: Consumer device.
958 *
959 * Check links from this device to any suppliers. Walk the list of the device's
960 * links to suppliers and see if all of them are available. If not, simply
961 * return -EPROBE_DEFER.
962 *
963 * We need to guarantee that the supplier will not go away after the check has
964 * been positive here. It only can go away in __device_release_driver() and
965 * that function checks the device's links to consumers. This means we need to
966 * mark the link as "consumer probe in progress" to make the supplier removal
967 * wait for us to complete (or bad things may happen).
968 *
Rafael J. Wysocki515db262019-07-16 17:21:06 +0200969 * Links without the DL_FLAG_MANAGED flag set are ignored.
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100970 */
971int device_links_check_suppliers(struct device *dev)
972{
973 struct device_link *link;
974 int ret = 0;
975
Saravana Kannane2ae9bc2019-09-04 14:11:21 -0700976 /*
977 * Device waiting for supplier to become available is not allowed to
978 * probe.
979 */
Saravana Kannan25ac86c2020-11-20 18:02:28 -0800980 mutex_lock(&fwnode_link_lock);
981 if (dev->fwnode && !list_empty(&dev->fwnode->suppliers) &&
982 !fw_devlink_is_permissive()) {
Saravana Kannan1f0dfa02020-12-17 19:16:59 -0800983 dev_dbg(dev, "probe deferral - wait for supplier %pfwP\n",
984 list_first_entry(&dev->fwnode->suppliers,
985 struct fwnode_link,
986 c_hook)->supplier);
Saravana Kannan25ac86c2020-11-20 18:02:28 -0800987 mutex_unlock(&fwnode_link_lock);
Saravana Kannane2ae9bc2019-09-04 14:11:21 -0700988 return -EPROBE_DEFER;
989 }
Saravana Kannan25ac86c2020-11-20 18:02:28 -0800990 mutex_unlock(&fwnode_link_lock);
Saravana Kannane2ae9bc2019-09-04 14:11:21 -0700991
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100992 device_links_write_lock();
993
994 list_for_each_entry(link, &dev->links.suppliers, c_node) {
Saravana Kannan8c3e3152020-05-26 15:09:27 -0700995 if (!(link->flags & DL_FLAG_MANAGED))
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100996 continue;
997
Saravana Kannan8c3e3152020-05-26 15:09:27 -0700998 if (link->status != DL_STATE_AVAILABLE &&
999 !(link->flags & DL_FLAG_SYNC_STATE_ONLY)) {
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001000 device_links_missing_supplier(dev);
Saravana Kannan1f0dfa02020-12-17 19:16:59 -08001001 dev_dbg(dev, "probe deferral - supplier %s not ready\n",
1002 dev_name(link->supplier));
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001003 ret = -EPROBE_DEFER;
1004 break;
1005 }
1006 WRITE_ONCE(link->status, DL_STATE_CONSUMER_PROBE);
1007 }
1008 dev->links.status = DL_DEV_PROBING;
1009
1010 device_links_write_unlock();
1011 return ret;
1012}
1013
Saravana Kannan26e77702019-11-14 14:56:45 -08001014/**
1015 * __device_links_queue_sync_state - Queue a device for sync_state() callback
1016 * @dev: Device to call sync_state() on
1017 * @list: List head to queue the @dev on
1018 *
1019 * Queues a device for a sync_state() callback when the device links write lock
1020 * isn't held. This allows the sync_state() execution flow to use device links
1021 * APIs. The caller must ensure this function is called with
1022 * device_links_write_lock() held.
1023 *
1024 * This function does a get_device() to make sure the device is not freed while
1025 * on this list.
1026 *
1027 * So the caller must also ensure that device_links_flush_sync_list() is called
1028 * as soon as the caller releases device_links_write_lock(). This is necessary
1029 * to make sure the sync_state() is called in a timely fashion and the
1030 * put_device() is called on this device.
1031 */
1032static void __device_links_queue_sync_state(struct device *dev,
1033 struct list_head *list)
Saravana Kannanfc5a2512019-09-04 14:11:23 -07001034{
1035 struct device_link *link;
1036
Saravana Kannan77036162020-02-21 00:05:10 -08001037 if (!dev_has_sync_state(dev))
1038 return;
Saravana Kannanfc5a2512019-09-04 14:11:23 -07001039 if (dev->state_synced)
1040 return;
1041
1042 list_for_each_entry(link, &dev->links.consumers, s_node) {
1043 if (!(link->flags & DL_FLAG_MANAGED))
1044 continue;
1045 if (link->status != DL_STATE_ACTIVE)
1046 return;
1047 }
1048
Saravana Kannan26e77702019-11-14 14:56:45 -08001049 /*
1050 * Set the flag here to avoid adding the same device to a list more
1051 * than once. This can happen if new consumers get added to the device
1052 * and probed before the list is flushed.
1053 */
Saravana Kannanfc5a2512019-09-04 14:11:23 -07001054 dev->state_synced = true;
Saravana Kannan26e77702019-11-14 14:56:45 -08001055
Saravana Kannan3b052a32020-11-20 18:02:17 -08001056 if (WARN_ON(!list_empty(&dev->links.defer_sync)))
Saravana Kannan26e77702019-11-14 14:56:45 -08001057 return;
1058
1059 get_device(dev);
Saravana Kannan3b052a32020-11-20 18:02:17 -08001060 list_add_tail(&dev->links.defer_sync, list);
Saravana Kannan26e77702019-11-14 14:56:45 -08001061}
1062
1063/**
1064 * device_links_flush_sync_list - Call sync_state() on a list of devices
1065 * @list: List of devices to call sync_state() on
Saravana Kannan21eb93f2020-02-21 00:05:08 -08001066 * @dont_lock_dev: Device for which lock is already held by the caller
Saravana Kannan26e77702019-11-14 14:56:45 -08001067 *
1068 * Calls sync_state() on all the devices that have been queued for it. This
Saravana Kannan21eb93f2020-02-21 00:05:08 -08001069 * function is used in conjunction with __device_links_queue_sync_state(). The
1070 * @dont_lock_dev parameter is useful when this function is called from a
1071 * context where a device lock is already held.
Saravana Kannan26e77702019-11-14 14:56:45 -08001072 */
Saravana Kannan21eb93f2020-02-21 00:05:08 -08001073static void device_links_flush_sync_list(struct list_head *list,
1074 struct device *dont_lock_dev)
Saravana Kannan26e77702019-11-14 14:56:45 -08001075{
1076 struct device *dev, *tmp;
1077
Saravana Kannan3b052a32020-11-20 18:02:17 -08001078 list_for_each_entry_safe(dev, tmp, list, links.defer_sync) {
1079 list_del_init(&dev->links.defer_sync);
Saravana Kannan26e77702019-11-14 14:56:45 -08001080
Saravana Kannan21eb93f2020-02-21 00:05:08 -08001081 if (dev != dont_lock_dev)
1082 device_lock(dev);
Saravana Kannan26e77702019-11-14 14:56:45 -08001083
1084 if (dev->bus->sync_state)
1085 dev->bus->sync_state(dev);
1086 else if (dev->driver && dev->driver->sync_state)
1087 dev->driver->sync_state(dev);
1088
Saravana Kannan21eb93f2020-02-21 00:05:08 -08001089 if (dev != dont_lock_dev)
1090 device_unlock(dev);
Saravana Kannan26e77702019-11-14 14:56:45 -08001091
1092 put_device(dev);
1093 }
Saravana Kannanfc5a2512019-09-04 14:11:23 -07001094}
1095
1096void device_links_supplier_sync_state_pause(void)
1097{
1098 device_links_write_lock();
1099 defer_sync_state_count++;
1100 device_links_write_unlock();
1101}
1102
1103void device_links_supplier_sync_state_resume(void)
1104{
1105 struct device *dev, *tmp;
Saravana Kannan26e77702019-11-14 14:56:45 -08001106 LIST_HEAD(sync_list);
Saravana Kannanfc5a2512019-09-04 14:11:23 -07001107
1108 device_links_write_lock();
1109 if (!defer_sync_state_count) {
1110 WARN(true, "Unmatched sync_state pause/resume!");
1111 goto out;
1112 }
1113 defer_sync_state_count--;
1114 if (defer_sync_state_count)
1115 goto out;
1116
Saravana Kannan3b052a32020-11-20 18:02:17 -08001117 list_for_each_entry_safe(dev, tmp, &deferred_sync, links.defer_sync) {
Saravana Kannan26e77702019-11-14 14:56:45 -08001118 /*
1119 * Delete from deferred_sync list before queuing it to
Saravana Kannan3b052a32020-11-20 18:02:17 -08001120 * sync_list because defer_sync is used for both lists.
Saravana Kannan26e77702019-11-14 14:56:45 -08001121 */
Saravana Kannan3b052a32020-11-20 18:02:17 -08001122 list_del_init(&dev->links.defer_sync);
Saravana Kannan26e77702019-11-14 14:56:45 -08001123 __device_links_queue_sync_state(dev, &sync_list);
Saravana Kannanfc5a2512019-09-04 14:11:23 -07001124 }
1125out:
1126 device_links_write_unlock();
Saravana Kannan26e77702019-11-14 14:56:45 -08001127
Saravana Kannan21eb93f2020-02-21 00:05:08 -08001128 device_links_flush_sync_list(&sync_list, NULL);
Saravana Kannanfc5a2512019-09-04 14:11:23 -07001129}
1130
1131static int sync_state_resume_initcall(void)
1132{
1133 device_links_supplier_sync_state_resume();
1134 return 0;
1135}
1136late_initcall(sync_state_resume_initcall);
1137
1138static void __device_links_supplier_defer_sync(struct device *sup)
1139{
Saravana Kannan3b052a32020-11-20 18:02:17 -08001140 if (list_empty(&sup->links.defer_sync) && dev_has_sync_state(sup))
1141 list_add_tail(&sup->links.defer_sync, &deferred_sync);
Saravana Kannanfc5a2512019-09-04 14:11:23 -07001142}
1143
Saravana Kannan21c27f02020-05-18 23:30:00 -07001144static void device_link_drop_managed(struct device_link *link)
1145{
1146 link->flags &= ~DL_FLAG_MANAGED;
1147 WRITE_ONCE(link->status, DL_STATE_NONE);
1148 kref_put(&link->kref, __device_link_del);
1149}
1150
Saravana Kannanda6d6472020-05-21 12:18:00 -07001151static ssize_t waiting_for_supplier_show(struct device *dev,
1152 struct device_attribute *attr,
1153 char *buf)
1154{
1155 bool val;
1156
1157 device_lock(dev);
Saravana Kannan25ac86c2020-11-20 18:02:28 -08001158 val = !list_empty(&dev->fwnode->suppliers);
Saravana Kannanda6d6472020-05-21 12:18:00 -07001159 device_unlock(dev);
Joe Perchesaa838892020-09-16 13:40:39 -07001160 return sysfs_emit(buf, "%u\n", val);
Saravana Kannanda6d6472020-05-21 12:18:00 -07001161}
1162static DEVICE_ATTR_RO(waiting_for_supplier);
1163
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001164/**
Saravana Kannanb6f617d2021-03-02 13:11:31 -08001165 * device_links_force_bind - Prepares device to be force bound
1166 * @dev: Consumer device.
1167 *
1168 * device_bind_driver() force binds a device to a driver without calling any
1169 * driver probe functions. So the consumer really isn't going to wait for any
1170 * supplier before it's bound to the driver. We still want the device link
1171 * states to be sensible when this happens.
1172 *
1173 * In preparation for device_bind_driver(), this function goes through each
1174 * supplier device links and checks if the supplier is bound. If it is, then
1175 * the device link status is set to CONSUMER_PROBE. Otherwise, the device link
1176 * is dropped. Links without the DL_FLAG_MANAGED flag set are ignored.
1177 */
1178void device_links_force_bind(struct device *dev)
1179{
1180 struct device_link *link, *ln;
1181
1182 device_links_write_lock();
1183
1184 list_for_each_entry_safe(link, ln, &dev->links.suppliers, c_node) {
1185 if (!(link->flags & DL_FLAG_MANAGED))
1186 continue;
1187
1188 if (link->status != DL_STATE_AVAILABLE) {
1189 device_link_drop_managed(link);
1190 continue;
1191 }
1192 WRITE_ONCE(link->status, DL_STATE_CONSUMER_PROBE);
1193 }
1194 dev->links.status = DL_DEV_PROBING;
1195
1196 device_links_write_unlock();
1197}
1198
1199/**
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001200 * device_links_driver_bound - Update device links after probing its driver.
1201 * @dev: Device to update the links for.
1202 *
1203 * The probe has been successful, so update links from this device to any
1204 * consumers by changing their status to "available".
1205 *
1206 * Also change the status of @dev's links to suppliers to "active".
1207 *
Rafael J. Wysocki515db262019-07-16 17:21:06 +02001208 * Links without the DL_FLAG_MANAGED flag set are ignored.
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001209 */
1210void device_links_driver_bound(struct device *dev)
1211{
Saravana Kannan21c27f02020-05-18 23:30:00 -07001212 struct device_link *link, *ln;
Saravana Kannan26e77702019-11-14 14:56:45 -08001213 LIST_HEAD(sync_list);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001214
Saravana Kannanbcbbcfd2019-10-28 15:00:23 -07001215 /*
Saravana Kannan9528e0d2021-02-05 14:26:37 -08001216 * If a device binds successfully, it's expected to have created all
Saravana Kannanbcbbcfd2019-10-28 15:00:23 -07001217 * the device links it needs to or make new device links as it needs
Saravana Kannan9528e0d2021-02-05 14:26:37 -08001218 * them. So, fw_devlink no longer needs to create device links to any
1219 * of the device's suppliers.
1220 *
1221 * Also, if a child firmware node of this bound device is not added as
1222 * a device by now, assume it is never going to be added and make sure
1223 * other devices don't defer probe indefinitely by waiting for such a
1224 * child device.
Saravana Kannanbcbbcfd2019-10-28 15:00:23 -07001225 */
Saravana Kannan9528e0d2021-02-05 14:26:37 -08001226 if (dev->fwnode && dev->fwnode->dev == dev) {
1227 struct fwnode_handle *child;
Saravana Kannanf9aa4602020-11-20 18:02:31 -08001228 fwnode_links_purge_suppliers(dev->fwnode);
Saravana Kannan9528e0d2021-02-05 14:26:37 -08001229 fwnode_for_each_available_child_node(dev->fwnode, child)
1230 fw_devlink_purge_absent_suppliers(child);
1231 }
Saravana Kannanda6d6472020-05-21 12:18:00 -07001232 device_remove_file(dev, &dev_attr_waiting_for_supplier);
Saravana Kannanbcbbcfd2019-10-28 15:00:23 -07001233
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001234 device_links_write_lock();
1235
1236 list_for_each_entry(link, &dev->links.consumers, s_node) {
Rafael J. Wysocki515db262019-07-16 17:21:06 +02001237 if (!(link->flags & DL_FLAG_MANAGED))
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001238 continue;
1239
Rafael J. Wysocki15cfb092019-02-01 01:50:39 +01001240 /*
1241 * Links created during consumer probe may be in the "consumer
1242 * probe" state to start with if the supplier is still probing
1243 * when they are created and they may become "active" if the
1244 * consumer probe returns first. Skip them here.
1245 */
1246 if (link->status == DL_STATE_CONSUMER_PROBE ||
1247 link->status == DL_STATE_ACTIVE)
1248 continue;
1249
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001250 WARN_ON(link->status != DL_STATE_DORMANT);
1251 WRITE_ONCE(link->status, DL_STATE_AVAILABLE);
Rafael J. Wysockie7dd4012019-02-01 01:59:42 +01001252
1253 if (link->flags & DL_FLAG_AUTOPROBE_CONSUMER)
1254 driver_deferred_probe_add(link->consumer);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001255 }
1256
Saravana Kannan21eb93f2020-02-21 00:05:08 -08001257 if (defer_sync_state_count)
1258 __device_links_supplier_defer_sync(dev);
1259 else
1260 __device_links_queue_sync_state(dev, &sync_list);
1261
Saravana Kannan21c27f02020-05-18 23:30:00 -07001262 list_for_each_entry_safe(link, ln, &dev->links.suppliers, c_node) {
1263 struct device *supplier;
1264
Rafael J. Wysocki515db262019-07-16 17:21:06 +02001265 if (!(link->flags & DL_FLAG_MANAGED))
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001266 continue;
1267
Saravana Kannan21c27f02020-05-18 23:30:00 -07001268 supplier = link->supplier;
1269 if (link->flags & DL_FLAG_SYNC_STATE_ONLY) {
1270 /*
1271 * When DL_FLAG_SYNC_STATE_ONLY is set, it means no
1272 * other DL_MANAGED_LINK_FLAGS have been set. So, it's
1273 * save to drop the managed link completely.
1274 */
1275 device_link_drop_managed(link);
1276 } else {
1277 WARN_ON(link->status != DL_STATE_CONSUMER_PROBE);
1278 WRITE_ONCE(link->status, DL_STATE_ACTIVE);
1279 }
Saravana Kannanfc5a2512019-09-04 14:11:23 -07001280
Saravana Kannan21c27f02020-05-18 23:30:00 -07001281 /*
1282 * This needs to be done even for the deleted
1283 * DL_FLAG_SYNC_STATE_ONLY device link in case it was the last
1284 * device link that was preventing the supplier from getting a
1285 * sync_state() call.
1286 */
Saravana Kannanfc5a2512019-09-04 14:11:23 -07001287 if (defer_sync_state_count)
Saravana Kannan21c27f02020-05-18 23:30:00 -07001288 __device_links_supplier_defer_sync(supplier);
Saravana Kannanfc5a2512019-09-04 14:11:23 -07001289 else
Saravana Kannan21c27f02020-05-18 23:30:00 -07001290 __device_links_queue_sync_state(supplier, &sync_list);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001291 }
1292
1293 dev->links.status = DL_DEV_DRIVER_BOUND;
1294
1295 device_links_write_unlock();
Saravana Kannan26e77702019-11-14 14:56:45 -08001296
Saravana Kannan21eb93f2020-02-21 00:05:08 -08001297 device_links_flush_sync_list(&sync_list, dev);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001298}
1299
1300/**
1301 * __device_links_no_driver - Update links of a device without a driver.
1302 * @dev: Device without a drvier.
1303 *
1304 * Delete all non-persistent links from this device to any suppliers.
1305 *
1306 * Persistent links stay around, but their status is changed to "available",
1307 * unless they already are in the "supplier unbind in progress" state in which
1308 * case they need not be updated.
1309 *
Rafael J. Wysocki515db262019-07-16 17:21:06 +02001310 * Links without the DL_FLAG_MANAGED flag set are ignored.
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001311 */
1312static void __device_links_no_driver(struct device *dev)
1313{
1314 struct device_link *link, *ln;
1315
1316 list_for_each_entry_safe_reverse(link, ln, &dev->links.suppliers, c_node) {
Rafael J. Wysocki515db262019-07-16 17:21:06 +02001317 if (!(link->flags & DL_FLAG_MANAGED))
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001318 continue;
1319
Saravana Kannan8c3e3152020-05-26 15:09:27 -07001320 if (link->flags & DL_FLAG_AUTOREMOVE_CONSUMER) {
Rafael J. Wysocki515db262019-07-16 17:21:06 +02001321 device_link_drop_managed(link);
Saravana Kannan8c3e3152020-05-26 15:09:27 -07001322 continue;
1323 }
1324
1325 if (link->status != DL_STATE_CONSUMER_PROBE &&
1326 link->status != DL_STATE_ACTIVE)
1327 continue;
1328
1329 if (link->supplier->links.status == DL_DEV_DRIVER_BOUND) {
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001330 WRITE_ONCE(link->status, DL_STATE_AVAILABLE);
Saravana Kannan8c3e3152020-05-26 15:09:27 -07001331 } else {
1332 WARN_ON(!(link->flags & DL_FLAG_SYNC_STATE_ONLY));
1333 WRITE_ONCE(link->status, DL_STATE_DORMANT);
1334 }
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001335 }
1336
1337 dev->links.status = DL_DEV_NO_DRIVER;
1338}
1339
Rafael J. Wysocki15cfb092019-02-01 01:50:39 +01001340/**
1341 * device_links_no_driver - Update links after failing driver probe.
1342 * @dev: Device whose driver has just failed to probe.
1343 *
1344 * Clean up leftover links to consumers for @dev and invoke
1345 * %__device_links_no_driver() to update links to suppliers for it as
1346 * appropriate.
1347 *
Rafael J. Wysocki515db262019-07-16 17:21:06 +02001348 * Links without the DL_FLAG_MANAGED flag set are ignored.
Rafael J. Wysocki15cfb092019-02-01 01:50:39 +01001349 */
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001350void device_links_no_driver(struct device *dev)
1351{
Rafael J. Wysocki15cfb092019-02-01 01:50:39 +01001352 struct device_link *link;
1353
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001354 device_links_write_lock();
Rafael J. Wysocki15cfb092019-02-01 01:50:39 +01001355
1356 list_for_each_entry(link, &dev->links.consumers, s_node) {
Rafael J. Wysocki515db262019-07-16 17:21:06 +02001357 if (!(link->flags & DL_FLAG_MANAGED))
Rafael J. Wysocki15cfb092019-02-01 01:50:39 +01001358 continue;
1359
1360 /*
1361 * The probe has failed, so if the status of the link is
1362 * "consumer probe" or "active", it must have been added by
1363 * a probing consumer while this device was still probing.
1364 * Change its state to "dormant", as it represents a valid
1365 * relationship, but it is not functionally meaningful.
1366 */
1367 if (link->status == DL_STATE_CONSUMER_PROBE ||
1368 link->status == DL_STATE_ACTIVE)
1369 WRITE_ONCE(link->status, DL_STATE_DORMANT);
1370 }
1371
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001372 __device_links_no_driver(dev);
Rafael J. Wysocki15cfb092019-02-01 01:50:39 +01001373
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001374 device_links_write_unlock();
1375}
1376
1377/**
1378 * device_links_driver_cleanup - Update links after driver removal.
1379 * @dev: Device whose driver has just gone away.
1380 *
1381 * Update links to consumers for @dev by changing their status to "dormant" and
1382 * invoke %__device_links_no_driver() to update links to suppliers for it as
1383 * appropriate.
1384 *
Rafael J. Wysocki515db262019-07-16 17:21:06 +02001385 * Links without the DL_FLAG_MANAGED flag set are ignored.
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001386 */
1387void device_links_driver_cleanup(struct device *dev)
1388{
Rafael J. Wysockic8d50982019-02-01 01:45:55 +01001389 struct device_link *link, *ln;
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001390
1391 device_links_write_lock();
1392
Rafael J. Wysockic8d50982019-02-01 01:45:55 +01001393 list_for_each_entry_safe(link, ln, &dev->links.consumers, s_node) {
Rafael J. Wysocki515db262019-07-16 17:21:06 +02001394 if (!(link->flags & DL_FLAG_MANAGED))
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001395 continue;
1396
Vivek Gautame88728f2018-06-27 18:20:55 +05301397 WARN_ON(link->flags & DL_FLAG_AUTOREMOVE_CONSUMER);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001398 WARN_ON(link->status != DL_STATE_SUPPLIER_UNBIND);
Vivek Gautam1689cac2018-06-27 18:20:56 +05301399
1400 /*
1401 * autoremove the links between this @dev and its consumer
1402 * devices that are not active, i.e. where the link state
1403 * has moved to DL_STATE_SUPPLIER_UNBIND.
1404 */
1405 if (link->status == DL_STATE_SUPPLIER_UNBIND &&
1406 link->flags & DL_FLAG_AUTOREMOVE_SUPPLIER)
Rafael J. Wysocki515db262019-07-16 17:21:06 +02001407 device_link_drop_managed(link);
Vivek Gautam1689cac2018-06-27 18:20:56 +05301408
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001409 WRITE_ONCE(link->status, DL_STATE_DORMANT);
1410 }
1411
Saravana Kannan3b052a32020-11-20 18:02:17 -08001412 list_del_init(&dev->links.defer_sync);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001413 __device_links_no_driver(dev);
1414
1415 device_links_write_unlock();
1416}
1417
1418/**
1419 * device_links_busy - Check if there are any busy links to consumers.
1420 * @dev: Device to check.
1421 *
1422 * Check each consumer of the device and return 'true' if its link's status
1423 * is one of "consumer probe" or "active" (meaning that the given consumer is
1424 * probing right now or its driver is present). Otherwise, change the link
1425 * state to "supplier unbind" to prevent the consumer from being probed
1426 * successfully going forward.
1427 *
1428 * Return 'false' if there are no probing or active consumers.
1429 *
Rafael J. Wysocki515db262019-07-16 17:21:06 +02001430 * Links without the DL_FLAG_MANAGED flag set are ignored.
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001431 */
1432bool device_links_busy(struct device *dev)
1433{
1434 struct device_link *link;
1435 bool ret = false;
1436
1437 device_links_write_lock();
1438
1439 list_for_each_entry(link, &dev->links.consumers, s_node) {
Rafael J. Wysocki515db262019-07-16 17:21:06 +02001440 if (!(link->flags & DL_FLAG_MANAGED))
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001441 continue;
1442
1443 if (link->status == DL_STATE_CONSUMER_PROBE
1444 || link->status == DL_STATE_ACTIVE) {
1445 ret = true;
1446 break;
1447 }
1448 WRITE_ONCE(link->status, DL_STATE_SUPPLIER_UNBIND);
1449 }
1450
1451 dev->links.status = DL_DEV_UNBINDING;
1452
1453 device_links_write_unlock();
1454 return ret;
1455}
1456
1457/**
1458 * device_links_unbind_consumers - Force unbind consumers of the given device.
1459 * @dev: Device to unbind the consumers of.
1460 *
1461 * Walk the list of links to consumers for @dev and if any of them is in the
1462 * "consumer probe" state, wait for all device probes in progress to complete
1463 * and start over.
1464 *
1465 * If that's not the case, change the status of the link to "supplier unbind"
1466 * and check if the link was in the "active" state. If so, force the consumer
1467 * driver to unbind and start over (the consumer will not re-probe as we have
1468 * changed the state of the link already).
1469 *
Rafael J. Wysocki515db262019-07-16 17:21:06 +02001470 * Links without the DL_FLAG_MANAGED flag set are ignored.
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001471 */
1472void device_links_unbind_consumers(struct device *dev)
1473{
1474 struct device_link *link;
1475
1476 start:
1477 device_links_write_lock();
1478
1479 list_for_each_entry(link, &dev->links.consumers, s_node) {
1480 enum device_link_state status;
1481
Saravana Kannan05ef9832019-10-28 15:00:22 -07001482 if (!(link->flags & DL_FLAG_MANAGED) ||
1483 link->flags & DL_FLAG_SYNC_STATE_ONLY)
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001484 continue;
1485
1486 status = link->status;
1487 if (status == DL_STATE_CONSUMER_PROBE) {
1488 device_links_write_unlock();
1489
1490 wait_for_device_probe();
1491 goto start;
1492 }
1493 WRITE_ONCE(link->status, DL_STATE_SUPPLIER_UNBIND);
1494 if (status == DL_STATE_ACTIVE) {
1495 struct device *consumer = link->consumer;
1496
1497 get_device(consumer);
1498
1499 device_links_write_unlock();
1500
1501 device_release_driver_internal(consumer, NULL,
1502 consumer->parent);
1503 put_device(consumer);
1504 goto start;
1505 }
1506 }
1507
1508 device_links_write_unlock();
1509}
1510
1511/**
1512 * device_links_purge - Delete existing links to other devices.
1513 * @dev: Target device.
1514 */
1515static void device_links_purge(struct device *dev)
1516{
1517 struct device_link *link, *ln;
1518
Saravana Kannan287905e2020-05-21 12:17:58 -07001519 if (dev->class == &devlink_class)
1520 return;
1521
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001522 /*
1523 * Delete all of the remaining links from this device to any other
1524 * devices (either consumers or suppliers).
1525 */
1526 device_links_write_lock();
1527
1528 list_for_each_entry_safe_reverse(link, ln, &dev->links.suppliers, c_node) {
1529 WARN_ON(link->status == DL_STATE_ACTIVE);
Lukas Wunneread18c22018-02-10 19:27:12 +01001530 __device_link_del(&link->kref);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001531 }
1532
1533 list_for_each_entry_safe_reverse(link, ln, &dev->links.consumers, s_node) {
1534 WARN_ON(link->status != DL_STATE_DORMANT &&
1535 link->status != DL_STATE_NONE);
Lukas Wunneread18c22018-02-10 19:27:12 +01001536 __device_link_del(&link->kref);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001537 }
1538
1539 device_links_write_unlock();
1540}
1541
Saravana Kannanb90fb8f2020-12-17 19:17:01 -08001542#define FW_DEVLINK_FLAGS_PERMISSIVE (DL_FLAG_INFERRED | \
1543 DL_FLAG_SYNC_STATE_ONLY)
1544#define FW_DEVLINK_FLAGS_ON (DL_FLAG_INFERRED | \
1545 DL_FLAG_AUTOPROBE_CONSUMER)
1546#define FW_DEVLINK_FLAGS_RPM (FW_DEVLINK_FLAGS_ON | \
1547 DL_FLAG_PM_RUNTIME)
1548
Saravana Kannanea718c692021-03-02 13:11:32 -08001549static u32 fw_devlink_flags = FW_DEVLINK_FLAGS_ON;
Saravana Kannan42926ac2020-05-14 22:34:57 -07001550static int __init fw_devlink_setup(char *arg)
1551{
1552 if (!arg)
1553 return -EINVAL;
1554
1555 if (strcmp(arg, "off") == 0) {
1556 fw_devlink_flags = 0;
1557 } else if (strcmp(arg, "permissive") == 0) {
Saravana Kannanb90fb8f2020-12-17 19:17:01 -08001558 fw_devlink_flags = FW_DEVLINK_FLAGS_PERMISSIVE;
Saravana Kannan42926ac2020-05-14 22:34:57 -07001559 } else if (strcmp(arg, "on") == 0) {
Saravana Kannanb90fb8f2020-12-17 19:17:01 -08001560 fw_devlink_flags = FW_DEVLINK_FLAGS_ON;
Saravana Kannan42926ac2020-05-14 22:34:57 -07001561 } else if (strcmp(arg, "rpm") == 0) {
Saravana Kannanb90fb8f2020-12-17 19:17:01 -08001562 fw_devlink_flags = FW_DEVLINK_FLAGS_RPM;
Saravana Kannan42926ac2020-05-14 22:34:57 -07001563 }
1564 return 0;
1565}
1566early_param("fw_devlink", fw_devlink_setup);
1567
Saravana Kannan19d0f5f2021-02-05 14:26:39 -08001568static bool fw_devlink_strict;
1569static int __init fw_devlink_strict_setup(char *arg)
1570{
1571 return strtobool(arg, &fw_devlink_strict);
1572}
1573early_param("fw_devlink.strict", fw_devlink_strict_setup);
1574
Saravana Kannan42926ac2020-05-14 22:34:57 -07001575u32 fw_devlink_get_flags(void)
1576{
1577 return fw_devlink_flags;
1578}
1579
1580static bool fw_devlink_is_permissive(void)
1581{
Saravana Kannanb90fb8f2020-12-17 19:17:01 -08001582 return fw_devlink_flags == FW_DEVLINK_FLAGS_PERMISSIVE;
Saravana Kannan42926ac2020-05-14 22:34:57 -07001583}
1584
Saravana Kannan19d0f5f2021-02-05 14:26:39 -08001585bool fw_devlink_is_strict(void)
1586{
1587 return fw_devlink_strict && !fw_devlink_is_permissive();
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001588}
1589
Saravana Kannanc2c724c2020-11-20 18:02:27 -08001590static void fw_devlink_parse_fwnode(struct fwnode_handle *fwnode)
1591{
1592 if (fwnode->flags & FWNODE_FLAG_LINKS_ADDED)
1593 return;
1594
Saravana Kannan2d09e6e2020-11-20 18:02:32 -08001595 fwnode_call_int_op(fwnode, add_links);
Saravana Kannanc2c724c2020-11-20 18:02:27 -08001596 fwnode->flags |= FWNODE_FLAG_LINKS_ADDED;
1597}
1598
1599static void fw_devlink_parse_fwtree(struct fwnode_handle *fwnode)
1600{
1601 struct fwnode_handle *child = NULL;
1602
1603 fw_devlink_parse_fwnode(fwnode);
1604
1605 while ((child = fwnode_get_next_available_child_node(fwnode, child)))
1606 fw_devlink_parse_fwtree(child);
1607}
1608
Saravana Kannand46f3e32021-04-01 21:03:41 -07001609static void fw_devlink_relax_link(struct device_link *link)
1610{
1611 if (!(link->flags & DL_FLAG_INFERRED))
1612 return;
1613
1614 if (link->flags == (DL_FLAG_MANAGED | FW_DEVLINK_FLAGS_PERMISSIVE))
1615 return;
1616
1617 pm_runtime_drop_link(link);
1618 link->flags = DL_FLAG_MANAGED | FW_DEVLINK_FLAGS_PERMISSIVE;
1619 dev_dbg(link->consumer, "Relaxing link with %s\n",
1620 dev_name(link->supplier));
1621}
1622
1623static int fw_devlink_no_driver(struct device *dev, void *data)
1624{
1625 struct device_link *link = to_devlink(dev);
1626
1627 if (!link->supplier->can_match)
1628 fw_devlink_relax_link(link);
1629
1630 return 0;
1631}
1632
1633void fw_devlink_drivers_done(void)
1634{
1635 fw_devlink_drv_reg_done = true;
1636 device_links_write_lock();
1637 class_for_each_device(&devlink_class, NULL, NULL,
1638 fw_devlink_no_driver);
1639 device_links_write_unlock();
1640}
1641
1642static void fw_devlink_unblock_consumers(struct device *dev)
1643{
1644 struct device_link *link;
1645
1646 if (!fw_devlink_flags || fw_devlink_is_permissive())
1647 return;
1648
1649 device_links_write_lock();
1650 list_for_each_entry(link, &dev->links.consumers, s_node)
1651 fw_devlink_relax_link(link);
1652 device_links_write_unlock();
1653}
1654
Saravana Kannanf9aa4602020-11-20 18:02:31 -08001655/**
Saravana Kannanb0e2fa42020-12-17 19:17:02 -08001656 * fw_devlink_relax_cycle - Convert cyclic links to SYNC_STATE_ONLY links
1657 * @con: Device to check dependencies for.
1658 * @sup: Device to check against.
1659 *
1660 * Check if @sup depends on @con or any device dependent on it (its child or
1661 * its consumer etc). When such a cyclic dependency is found, convert all
1662 * device links created solely by fw_devlink into SYNC_STATE_ONLY device links.
1663 * This is the equivalent of doing fw_devlink=permissive just between the
1664 * devices in the cycle. We need to do this because, at this point, fw_devlink
1665 * can't tell which of these dependencies is not a real dependency.
1666 *
1667 * Return 1 if a cycle is found. Otherwise, return 0.
1668 */
kernel test robotc13b8272020-12-18 14:39:35 +08001669static int fw_devlink_relax_cycle(struct device *con, void *sup)
Saravana Kannanb0e2fa42020-12-17 19:17:02 -08001670{
1671 struct device_link *link;
1672 int ret;
1673
1674 if (con == sup)
1675 return 1;
1676
1677 ret = device_for_each_child(con, sup, fw_devlink_relax_cycle);
1678 if (ret)
1679 return ret;
1680
1681 list_for_each_entry(link, &con->links.consumers, s_node) {
1682 if ((link->flags & ~DL_FLAG_INFERRED) ==
1683 (DL_FLAG_SYNC_STATE_ONLY | DL_FLAG_MANAGED))
1684 continue;
1685
1686 if (!fw_devlink_relax_cycle(link->consumer, sup))
1687 continue;
1688
1689 ret = 1;
1690
Saravana Kannand46f3e32021-04-01 21:03:41 -07001691 fw_devlink_relax_link(link);
Saravana Kannanb0e2fa42020-12-17 19:17:02 -08001692 }
1693 return ret;
1694}
1695
1696/**
Saravana Kannanf9aa4602020-11-20 18:02:31 -08001697 * fw_devlink_create_devlink - Create a device link from a consumer to fwnode
Pierre-Louis Bossart37c52f72021-03-31 18:26:08 -05001698 * @con: consumer device for the device link
1699 * @sup_handle: fwnode handle of supplier
1700 * @flags: devlink flags
Saravana Kannanf9aa4602020-11-20 18:02:31 -08001701 *
1702 * This function will try to create a device link between the consumer device
1703 * @con and the supplier device represented by @sup_handle.
1704 *
1705 * The supplier has to be provided as a fwnode because incorrect cycles in
1706 * fwnode links can sometimes cause the supplier device to never be created.
1707 * This function detects such cases and returns an error if it cannot create a
1708 * device link from the consumer to a missing supplier.
1709 *
1710 * Returns,
1711 * 0 on successfully creating a device link
1712 * -EINVAL if the device link cannot be created as expected
1713 * -EAGAIN if the device link cannot be created right now, but it may be
1714 * possible to do that in the future
1715 */
1716static int fw_devlink_create_devlink(struct device *con,
1717 struct fwnode_handle *sup_handle, u32 flags)
1718{
1719 struct device *sup_dev;
1720 int ret = 0;
1721
1722 sup_dev = get_dev_from_fwnode(sup_handle);
1723 if (sup_dev) {
1724 /*
Saravana Kannan74c782c2021-02-05 14:26:41 -08001725 * If it's one of those drivers that don't actually bind to
1726 * their device using driver core, then don't wait on this
1727 * supplier device indefinitely.
1728 */
1729 if (sup_dev->links.status == DL_DEV_NO_DRIVER &&
1730 sup_handle->flags & FWNODE_FLAG_INITIALIZED) {
1731 ret = -EINVAL;
1732 goto out;
1733 }
1734
1735 /*
Saravana Kannanf9aa4602020-11-20 18:02:31 -08001736 * If this fails, it is due to cycles in device links. Just
1737 * give up on this link and treat it as invalid.
1738 */
Saravana Kannanb0e2fa42020-12-17 19:17:02 -08001739 if (!device_link_add(con, sup_dev, flags) &&
1740 !(flags & DL_FLAG_SYNC_STATE_ONLY)) {
1741 dev_info(con, "Fixing up cyclic dependency with %s\n",
1742 dev_name(sup_dev));
1743 device_links_write_lock();
1744 fw_devlink_relax_cycle(con, sup_dev);
1745 device_links_write_unlock();
1746 device_link_add(con, sup_dev,
1747 FW_DEVLINK_FLAGS_PERMISSIVE);
Saravana Kannanf9aa4602020-11-20 18:02:31 -08001748 ret = -EINVAL;
Saravana Kannanb0e2fa42020-12-17 19:17:02 -08001749 }
Saravana Kannanf9aa4602020-11-20 18:02:31 -08001750
1751 goto out;
1752 }
1753
Saravana Kannan74c782c2021-02-05 14:26:41 -08001754 /* Supplier that's already initialized without a struct device. */
1755 if (sup_handle->flags & FWNODE_FLAG_INITIALIZED)
1756 return -EINVAL;
1757
Saravana Kannanf9aa4602020-11-20 18:02:31 -08001758 /*
1759 * DL_FLAG_SYNC_STATE_ONLY doesn't block probing and supports
1760 * cycles. So cycle detection isn't necessary and shouldn't be
1761 * done.
1762 */
1763 if (flags & DL_FLAG_SYNC_STATE_ONLY)
1764 return -EAGAIN;
1765
1766 /*
1767 * If we can't find the supplier device from its fwnode, it might be
1768 * due to a cyclic dependency between fwnodes. Some of these cycles can
1769 * be broken by applying logic. Check for these types of cycles and
1770 * break them so that devices in the cycle probe properly.
1771 *
1772 * If the supplier's parent is dependent on the consumer, then
1773 * the consumer-supplier dependency is a false dependency. So,
1774 * treat it as an invalid link.
1775 */
1776 sup_dev = fwnode_get_next_parent_dev(sup_handle);
1777 if (sup_dev && device_is_dependent(con, sup_dev)) {
1778 dev_dbg(con, "Not linking to %pfwP - False link\n",
1779 sup_handle);
1780 ret = -EINVAL;
1781 } else {
1782 /*
1783 * Can't check for cycles or no cycles. So let's try
1784 * again later.
1785 */
1786 ret = -EAGAIN;
1787 }
1788
1789out:
1790 put_device(sup_dev);
1791 return ret;
1792}
1793
1794/**
1795 * __fw_devlink_link_to_consumers - Create device links to consumers of a device
Pierre-Louis Bossart37c52f72021-03-31 18:26:08 -05001796 * @dev: Device that needs to be linked to its consumers
Saravana Kannanf9aa4602020-11-20 18:02:31 -08001797 *
1798 * This function looks at all the consumer fwnodes of @dev and creates device
1799 * links between the consumer device and @dev (supplier).
1800 *
1801 * If the consumer device has not been added yet, then this function creates a
1802 * SYNC_STATE_ONLY link between @dev (supplier) and the closest ancestor device
1803 * of the consumer fwnode. This is necessary to make sure @dev doesn't get a
1804 * sync_state() callback before the real consumer device gets to be added and
1805 * then probed.
1806 *
1807 * Once device links are created from the real consumer to @dev (supplier), the
1808 * fwnode links are deleted.
1809 */
1810static void __fw_devlink_link_to_consumers(struct device *dev)
1811{
1812 struct fwnode_handle *fwnode = dev->fwnode;
1813 struct fwnode_link *link, *tmp;
1814
1815 list_for_each_entry_safe(link, tmp, &fwnode->consumers, s_hook) {
1816 u32 dl_flags = fw_devlink_get_flags();
1817 struct device *con_dev;
1818 bool own_link = true;
1819 int ret;
1820
1821 con_dev = get_dev_from_fwnode(link->consumer);
1822 /*
1823 * If consumer device is not available yet, make a "proxy"
1824 * SYNC_STATE_ONLY link from the consumer's parent device to
1825 * the supplier device. This is necessary to make sure the
1826 * supplier doesn't get a sync_state() callback before the real
1827 * consumer can create a device link to the supplier.
1828 *
1829 * This proxy link step is needed to handle the case where the
1830 * consumer's parent device is added before the supplier.
1831 */
1832 if (!con_dev) {
1833 con_dev = fwnode_get_next_parent_dev(link->consumer);
1834 /*
1835 * However, if the consumer's parent device is also the
1836 * parent of the supplier, don't create a
1837 * consumer-supplier link from the parent to its child
1838 * device. Such a dependency is impossible.
1839 */
1840 if (con_dev &&
1841 fwnode_is_ancestor_of(con_dev->fwnode, fwnode)) {
1842 put_device(con_dev);
1843 con_dev = NULL;
1844 } else {
1845 own_link = false;
Saravana Kannanb90fb8f2020-12-17 19:17:01 -08001846 dl_flags = FW_DEVLINK_FLAGS_PERMISSIVE;
Saravana Kannanf9aa4602020-11-20 18:02:31 -08001847 }
1848 }
1849
1850 if (!con_dev)
1851 continue;
1852
1853 ret = fw_devlink_create_devlink(con_dev, fwnode, dl_flags);
1854 put_device(con_dev);
1855 if (!own_link || ret == -EAGAIN)
1856 continue;
1857
1858 list_del(&link->s_hook);
1859 list_del(&link->c_hook);
1860 kfree(link);
1861 }
1862}
1863
1864/**
1865 * __fw_devlink_link_to_suppliers - Create device links to suppliers of a device
Pierre-Louis Bossart37c52f72021-03-31 18:26:08 -05001866 * @dev: The consumer device that needs to be linked to its suppliers
1867 * @fwnode: Root of the fwnode tree that is used to create device links
Saravana Kannanf9aa4602020-11-20 18:02:31 -08001868 *
1869 * This function looks at all the supplier fwnodes of fwnode tree rooted at
1870 * @fwnode and creates device links between @dev (consumer) and all the
1871 * supplier devices of the entire fwnode tree at @fwnode.
1872 *
1873 * The function creates normal (non-SYNC_STATE_ONLY) device links between @dev
1874 * and the real suppliers of @dev. Once these device links are created, the
1875 * fwnode links are deleted. When such device links are successfully created,
1876 * this function is called recursively on those supplier devices. This is
1877 * needed to detect and break some invalid cycles in fwnode links. See
1878 * fw_devlink_create_devlink() for more details.
1879 *
1880 * In addition, it also looks at all the suppliers of the entire fwnode tree
1881 * because some of the child devices of @dev that have not been added yet
1882 * (because @dev hasn't probed) might already have their suppliers added to
1883 * driver core. So, this function creates SYNC_STATE_ONLY device links between
1884 * @dev (consumer) and these suppliers to make sure they don't execute their
1885 * sync_state() callbacks before these child devices have a chance to create
1886 * their device links. The fwnode links that correspond to the child devices
1887 * aren't delete because they are needed later to create the device links
1888 * between the real consumer and supplier devices.
1889 */
1890static void __fw_devlink_link_to_suppliers(struct device *dev,
1891 struct fwnode_handle *fwnode)
1892{
1893 bool own_link = (dev->fwnode == fwnode);
1894 struct fwnode_link *link, *tmp;
1895 struct fwnode_handle *child = NULL;
1896 u32 dl_flags;
1897
1898 if (own_link)
1899 dl_flags = fw_devlink_get_flags();
1900 else
Saravana Kannanb90fb8f2020-12-17 19:17:01 -08001901 dl_flags = FW_DEVLINK_FLAGS_PERMISSIVE;
Saravana Kannanf9aa4602020-11-20 18:02:31 -08001902
1903 list_for_each_entry_safe(link, tmp, &fwnode->suppliers, c_hook) {
1904 int ret;
1905 struct device *sup_dev;
1906 struct fwnode_handle *sup = link->supplier;
1907
1908 ret = fw_devlink_create_devlink(dev, sup, dl_flags);
1909 if (!own_link || ret == -EAGAIN)
1910 continue;
1911
1912 list_del(&link->s_hook);
1913 list_del(&link->c_hook);
1914 kfree(link);
1915
1916 /* If no device link was created, nothing more to do. */
1917 if (ret)
1918 continue;
1919
1920 /*
1921 * If a device link was successfully created to a supplier, we
1922 * now need to try and link the supplier to all its suppliers.
1923 *
1924 * This is needed to detect and delete false dependencies in
1925 * fwnode links that haven't been converted to a device link
1926 * yet. See comments in fw_devlink_create_devlink() for more
1927 * details on the false dependency.
1928 *
1929 * Without deleting these false dependencies, some devices will
1930 * never probe because they'll keep waiting for their false
1931 * dependency fwnode links to be converted to device links.
1932 */
1933 sup_dev = get_dev_from_fwnode(sup);
1934 __fw_devlink_link_to_suppliers(sup_dev, sup_dev->fwnode);
1935 put_device(sup_dev);
1936 }
1937
1938 /*
1939 * Make "proxy" SYNC_STATE_ONLY device links to represent the needs of
1940 * all the descendants. This proxy link step is needed to handle the
1941 * case where the supplier is added before the consumer's parent device
1942 * (@dev).
1943 */
1944 while ((child = fwnode_get_next_available_child_node(fwnode, child)))
1945 __fw_devlink_link_to_suppliers(dev, child);
1946}
1947
Saravana Kannan5f5377e2020-05-14 22:34:58 -07001948static void fw_devlink_link_device(struct device *dev)
1949{
Saravana Kannanf9aa4602020-11-20 18:02:31 -08001950 struct fwnode_handle *fwnode = dev->fwnode;
Saravana Kannan5f5377e2020-05-14 22:34:58 -07001951
Saravana Kannanf9aa4602020-11-20 18:02:31 -08001952 if (!fw_devlink_flags)
1953 return;
Saravana Kannanc84b9092020-11-20 18:02:21 -08001954
Saravana Kannanf9aa4602020-11-20 18:02:31 -08001955 fw_devlink_parse_fwtree(fwnode);
1956
1957 mutex_lock(&fwnode_link_lock);
1958 __fw_devlink_link_to_consumers(dev);
1959 __fw_devlink_link_to_suppliers(dev, fwnode);
1960 mutex_unlock(&fwnode_link_lock);
Saravana Kannan716a7a22020-05-14 22:34:59 -07001961}
Saravana Kannanc84b9092020-11-20 18:02:21 -08001962
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001963/* Device links support end. */
1964
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001965int (*platform_notify)(struct device *dev) = NULL;
1966int (*platform_notify_remove)(struct device *dev) = NULL;
Dan Williamse105b8b2008-04-21 10:51:07 -07001967static struct kobject *dev_kobj;
1968struct kobject *sysfs_dev_char_kobj;
1969struct kobject *sysfs_dev_block_kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970
Rafael J. Wysocki5e33bc42013-08-28 21:41:01 +02001971static DEFINE_MUTEX(device_hotplug_lock);
1972
1973void lock_device_hotplug(void)
1974{
1975 mutex_lock(&device_hotplug_lock);
1976}
1977
1978void unlock_device_hotplug(void)
1979{
1980 mutex_unlock(&device_hotplug_lock);
1981}
1982
1983int lock_device_hotplug_sysfs(void)
1984{
1985 if (mutex_trylock(&device_hotplug_lock))
1986 return 0;
1987
1988 /* Avoid busy looping (5 ms of sleep should do). */
1989 msleep(5);
1990 return restart_syscall();
1991}
1992
Greg Kroah-Hartman4e886c22008-01-27 12:12:43 -08001993#ifdef CONFIG_BLOCK
1994static inline int device_is_not_partition(struct device *dev)
1995{
1996 return !(dev->type == &part_type);
1997}
1998#else
1999static inline int device_is_not_partition(struct device *dev)
2000{
2001 return 1;
2002}
2003#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004
Heikki Krogerus07de0e82018-11-09 17:21:34 +03002005static int
2006device_platform_notify(struct device *dev, enum kobject_action action)
2007{
Heikki Krogerus7847a142018-11-09 17:21:35 +03002008 int ret;
2009
2010 ret = acpi_platform_notify(dev, action);
2011 if (ret)
2012 return ret;
2013
Heikki Krogerus59abd832018-11-09 17:21:36 +03002014 ret = software_node_notify(dev, action);
2015 if (ret)
2016 return ret;
2017
Heikki Krogerus07de0e82018-11-09 17:21:34 +03002018 if (platform_notify && action == KOBJ_ADD)
2019 platform_notify(dev);
2020 else if (platform_notify_remove && action == KOBJ_REMOVE)
2021 platform_notify_remove(dev);
2022 return 0;
2023}
2024
Alan Stern3e956372006-06-16 17:10:48 -04002025/**
2026 * dev_driver_string - Return a device's driver name, if at all possible
2027 * @dev: struct device to get the name of
2028 *
2029 * Will return the device's driver's name if it is bound to a device. If
yan9169c012012-04-20 21:08:45 +08002030 * the device is not bound to a driver, it will return the name of the bus
Alan Stern3e956372006-06-16 17:10:48 -04002031 * it is attached to. If it is not attached to a bus either, an empty
2032 * string will be returned.
2033 */
Jean Delvarebf9ca692008-07-30 12:29:21 -07002034const char *dev_driver_string(const struct device *dev)
Alan Stern3e956372006-06-16 17:10:48 -04002035{
Alan Stern35899722009-12-04 11:06:57 -05002036 struct device_driver *drv;
2037
2038 /* dev->driver can change to NULL underneath us because of unbinding,
2039 * so be careful about accessing it. dev->bus and dev->class should
2040 * never change once they are set, so they don't need special care.
2041 */
Mark Rutland6aa7de02017-10-23 14:07:29 -07002042 drv = READ_ONCE(dev->driver);
Saravana Kannane020ff62021-01-10 09:54:07 -08002043 return drv ? drv->name : dev_bus_name(dev);
Alan Stern3e956372006-06-16 17:10:48 -04002044}
Matthew Wilcox310a9222006-09-23 23:35:04 -06002045EXPORT_SYMBOL(dev_driver_string);
Alan Stern3e956372006-06-16 17:10:48 -04002046
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047#define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr)
2048
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002049static ssize_t dev_attr_show(struct kobject *kobj, struct attribute *attr,
2050 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002052 struct device_attribute *dev_attr = to_dev_attr(attr);
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +02002053 struct device *dev = kobj_to_dev(kobj);
Dmitry Torokhov4a0c20b2005-04-29 01:23:47 -05002054 ssize_t ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055
2056 if (dev_attr->show)
Yani Ioannou54b6f352005-05-17 06:39:34 -04002057 ret = dev_attr->show(dev, dev_attr, buf);
Andrew Morton815d2d52008-03-04 15:09:07 -08002058 if (ret >= (ssize_t)PAGE_SIZE) {
Sergey Senozhatskya52668c2017-12-11 21:50:21 +09002059 printk("dev_attr_show: %pS returned bad count\n",
2060 dev_attr->show);
Andrew Morton815d2d52008-03-04 15:09:07 -08002061 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 return ret;
2063}
2064
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002065static ssize_t dev_attr_store(struct kobject *kobj, struct attribute *attr,
2066 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002068 struct device_attribute *dev_attr = to_dev_attr(attr);
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +02002069 struct device *dev = kobj_to_dev(kobj);
Dmitry Torokhov4a0c20b2005-04-29 01:23:47 -05002070 ssize_t ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071
2072 if (dev_attr->store)
Yani Ioannou54b6f352005-05-17 06:39:34 -04002073 ret = dev_attr->store(dev, dev_attr, buf, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 return ret;
2075}
2076
Emese Revfy52cf25d2010-01-19 02:58:23 +01002077static const struct sysfs_ops dev_sysfs_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 .show = dev_attr_show,
2079 .store = dev_attr_store,
2080};
2081
Kay Sieversca22e562011-12-14 14:29:38 -08002082#define to_ext_attr(x) container_of(x, struct dev_ext_attribute, attr)
2083
2084ssize_t device_store_ulong(struct device *dev,
2085 struct device_attribute *attr,
2086 const char *buf, size_t size)
2087{
2088 struct dev_ext_attribute *ea = to_ext_attr(attr);
Kaitao chengf88184b2018-11-06 08:34:54 -08002089 int ret;
2090 unsigned long new;
2091
2092 ret = kstrtoul(buf, 0, &new);
2093 if (ret)
2094 return ret;
Kay Sieversca22e562011-12-14 14:29:38 -08002095 *(unsigned long *)(ea->var) = new;
2096 /* Always return full write size even if we didn't consume all */
2097 return size;
2098}
2099EXPORT_SYMBOL_GPL(device_store_ulong);
2100
2101ssize_t device_show_ulong(struct device *dev,
2102 struct device_attribute *attr,
2103 char *buf)
2104{
2105 struct dev_ext_attribute *ea = to_ext_attr(attr);
Joe Perchesaa838892020-09-16 13:40:39 -07002106 return sysfs_emit(buf, "%lx\n", *(unsigned long *)(ea->var));
Kay Sieversca22e562011-12-14 14:29:38 -08002107}
2108EXPORT_SYMBOL_GPL(device_show_ulong);
2109
2110ssize_t device_store_int(struct device *dev,
2111 struct device_attribute *attr,
2112 const char *buf, size_t size)
2113{
2114 struct dev_ext_attribute *ea = to_ext_attr(attr);
Kaitao chengf88184b2018-11-06 08:34:54 -08002115 int ret;
2116 long new;
2117
2118 ret = kstrtol(buf, 0, &new);
2119 if (ret)
2120 return ret;
2121
2122 if (new > INT_MAX || new < INT_MIN)
Kay Sieversca22e562011-12-14 14:29:38 -08002123 return -EINVAL;
2124 *(int *)(ea->var) = new;
2125 /* Always return full write size even if we didn't consume all */
2126 return size;
2127}
2128EXPORT_SYMBOL_GPL(device_store_int);
2129
2130ssize_t device_show_int(struct device *dev,
2131 struct device_attribute *attr,
2132 char *buf)
2133{
2134 struct dev_ext_attribute *ea = to_ext_attr(attr);
2135
Joe Perchesaa838892020-09-16 13:40:39 -07002136 return sysfs_emit(buf, "%d\n", *(int *)(ea->var));
Kay Sieversca22e562011-12-14 14:29:38 -08002137}
2138EXPORT_SYMBOL_GPL(device_show_int);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139
Borislav Petkov91872392012-10-09 19:52:05 +02002140ssize_t device_store_bool(struct device *dev, struct device_attribute *attr,
2141 const char *buf, size_t size)
2142{
2143 struct dev_ext_attribute *ea = to_ext_attr(attr);
2144
2145 if (strtobool(buf, ea->var) < 0)
2146 return -EINVAL;
2147
2148 return size;
2149}
2150EXPORT_SYMBOL_GPL(device_store_bool);
2151
2152ssize_t device_show_bool(struct device *dev, struct device_attribute *attr,
2153 char *buf)
2154{
2155 struct dev_ext_attribute *ea = to_ext_attr(attr);
2156
Joe Perchesaa838892020-09-16 13:40:39 -07002157 return sysfs_emit(buf, "%d\n", *(bool *)(ea->var));
Borislav Petkov91872392012-10-09 19:52:05 +02002158}
2159EXPORT_SYMBOL_GPL(device_show_bool);
2160
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161/**
Robert P. J. Dayf8878dc2013-06-01 20:17:34 -04002162 * device_release - free device structure.
2163 * @kobj: device's kobject.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 *
Robert P. J. Dayf8878dc2013-06-01 20:17:34 -04002165 * This is called once the reference count for the object
2166 * reaches 0. We forward the call to the device's release
2167 * method, which should handle actually freeing the structure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002169static void device_release(struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +02002171 struct device *dev = kobj_to_dev(kobj);
Greg Kroah-Hartmanfb069a52008-12-16 12:23:36 -08002172 struct device_private *p = dev->p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173
Ming Leia525a3d2012-07-25 01:42:29 +08002174 /*
2175 * Some platform devices are driven without driver attached
2176 * and managed resources may have been acquired. Make sure
2177 * all resources are released.
2178 *
2179 * Drivers still can add resources into device after device
2180 * is deleted but alive, so release devres here to avoid
2181 * possible memory leak.
2182 */
2183 devres_release_all(dev);
2184
Jim Quinlane0d07272020-09-17 18:43:40 +02002185 kfree(dev->dma_range_map);
2186
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 if (dev->release)
2188 dev->release(dev);
Kay Sieversf9f852d2006-10-07 21:54:55 +02002189 else if (dev->type && dev->type->release)
2190 dev->type->release(dev);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07002191 else if (dev->class && dev->class->dev_release)
2192 dev->class->dev_release(dev);
Arjan van de Venf810a5c2008-07-25 19:45:39 -07002193 else
Mauro Carvalho Chehab0c1bc6b2020-04-14 18:48:37 +02002194 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 +01002195 dev_name(dev));
Greg Kroah-Hartmanfb069a52008-12-16 12:23:36 -08002196 kfree(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197}
2198
Eric W. Biedermanbc451f22010-03-30 11:31:25 -07002199static const void *device_namespace(struct kobject *kobj)
2200{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +02002201 struct device *dev = kobj_to_dev(kobj);
Eric W. Biedermanbc451f22010-03-30 11:31:25 -07002202 const void *ns = NULL;
2203
2204 if (dev->class && dev->class->ns_type)
2205 ns = dev->class->namespace(dev);
2206
2207 return ns;
2208}
2209
Dmitry Torokhov9944e892018-07-20 21:56:50 +00002210static void device_get_ownership(struct kobject *kobj, kuid_t *uid, kgid_t *gid)
2211{
2212 struct device *dev = kobj_to_dev(kobj);
2213
2214 if (dev->class && dev->class->get_ownership)
2215 dev->class->get_ownership(dev, uid, gid);
2216}
2217
Greg Kroah-Hartman8f4afc42007-10-11 10:47:49 -06002218static struct kobj_type device_ktype = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219 .release = device_release,
2220 .sysfs_ops = &dev_sysfs_ops,
Eric W. Biedermanbc451f22010-03-30 11:31:25 -07002221 .namespace = device_namespace,
Dmitry Torokhov9944e892018-07-20 21:56:50 +00002222 .get_ownership = device_get_ownership,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223};
2224
2225
Kay Sievers312c0042005-11-16 09:00:00 +01002226static int dev_uevent_filter(struct kset *kset, struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227{
2228 struct kobj_type *ktype = get_ktype(kobj);
2229
Greg Kroah-Hartman8f4afc42007-10-11 10:47:49 -06002230 if (ktype == &device_ktype) {
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +02002231 struct device *dev = kobj_to_dev(kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232 if (dev->bus)
2233 return 1;
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07002234 if (dev->class)
2235 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236 }
2237 return 0;
2238}
2239
Kay Sievers312c0042005-11-16 09:00:00 +01002240static const char *dev_uevent_name(struct kset *kset, struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +02002242 struct device *dev = kobj_to_dev(kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07002244 if (dev->bus)
2245 return dev->bus->name;
2246 if (dev->class)
2247 return dev->class->name;
2248 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249}
2250
Kay Sievers7eff2e72007-08-14 15:15:12 +02002251static int dev_uevent(struct kset *kset, struct kobject *kobj,
2252 struct kobj_uevent_env *env)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +02002254 struct device *dev = kobj_to_dev(kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255 int retval = 0;
2256
Kay Sievers6fcf53a2009-04-30 15:23:42 +02002257 /* add device node properties if present */
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07002258 if (MAJOR(dev->devt)) {
Kay Sievers6fcf53a2009-04-30 15:23:42 +02002259 const char *tmp;
2260 const char *name;
Al Viro2c9ede52011-07-23 20:24:48 -04002261 umode_t mode = 0;
Greg Kroah-Hartman4e4098a2013-04-11 11:43:29 -07002262 kuid_t uid = GLOBAL_ROOT_UID;
2263 kgid_t gid = GLOBAL_ROOT_GID;
Kay Sievers6fcf53a2009-04-30 15:23:42 +02002264
Kay Sievers7eff2e72007-08-14 15:15:12 +02002265 add_uevent_var(env, "MAJOR=%u", MAJOR(dev->devt));
2266 add_uevent_var(env, "MINOR=%u", MINOR(dev->devt));
Kay Sievers3c2670e2013-04-06 09:56:00 -07002267 name = device_get_devnode(dev, &mode, &uid, &gid, &tmp);
Kay Sievers6fcf53a2009-04-30 15:23:42 +02002268 if (name) {
2269 add_uevent_var(env, "DEVNAME=%s", name);
Kay Sieverse454cea2009-09-18 23:01:12 +02002270 if (mode)
2271 add_uevent_var(env, "DEVMODE=%#o", mode & 0777);
Greg Kroah-Hartman4e4098a2013-04-11 11:43:29 -07002272 if (!uid_eq(uid, GLOBAL_ROOT_UID))
2273 add_uevent_var(env, "DEVUID=%u", from_kuid(&init_user_ns, uid));
2274 if (!gid_eq(gid, GLOBAL_ROOT_GID))
2275 add_uevent_var(env, "DEVGID=%u", from_kgid(&init_user_ns, gid));
Kay Sievers3c2670e2013-04-06 09:56:00 -07002276 kfree(tmp);
Kay Sievers6fcf53a2009-04-30 15:23:42 +02002277 }
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07002278 }
2279
Kay Sievers414264f2007-03-12 21:08:57 +01002280 if (dev->type && dev->type->name)
Kay Sievers7eff2e72007-08-14 15:15:12 +02002281 add_uevent_var(env, "DEVTYPE=%s", dev->type->name);
Kay Sievers414264f2007-03-12 21:08:57 +01002282
Kay Sievers239378f2006-10-07 21:54:55 +02002283 if (dev->driver)
Kay Sievers7eff2e72007-08-14 15:15:12 +02002284 add_uevent_var(env, "DRIVER=%s", dev->driver->name);
Kay Sievers239378f2006-10-07 21:54:55 +02002285
Grant Likely07d57a32012-02-01 11:22:22 -07002286 /* Add common DT information about the device */
2287 of_device_uevent(dev, env);
2288
Kay Sievers7eff2e72007-08-14 15:15:12 +02002289 /* have the bus specific function add its stuff */
Kay Sievers312c0042005-11-16 09:00:00 +01002290 if (dev->bus && dev->bus->uevent) {
Kay Sievers7eff2e72007-08-14 15:15:12 +02002291 retval = dev->bus->uevent(dev, env);
Kay Sieversf9f852d2006-10-07 21:54:55 +02002292 if (retval)
Greg Kroah-Hartman7dc72b22007-11-28 23:49:41 -08002293 pr_debug("device: '%s': %s: bus uevent() returned %d\n",
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01002294 dev_name(dev), __func__, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295 }
2296
Kay Sievers7eff2e72007-08-14 15:15:12 +02002297 /* have the class specific function add its stuff */
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07002298 if (dev->class && dev->class->dev_uevent) {
Kay Sievers7eff2e72007-08-14 15:15:12 +02002299 retval = dev->class->dev_uevent(dev, env);
Kay Sieversf9f852d2006-10-07 21:54:55 +02002300 if (retval)
Greg Kroah-Hartman7dc72b22007-11-28 23:49:41 -08002301 pr_debug("device: '%s': %s: class uevent() "
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01002302 "returned %d\n", dev_name(dev),
Harvey Harrison2b3a3022008-03-04 16:41:05 -08002303 __func__, retval);
Kay Sieversf9f852d2006-10-07 21:54:55 +02002304 }
2305
Stefan Weileef35c22010-08-06 21:11:15 +02002306 /* have the device type specific function add its stuff */
Kay Sieversf9f852d2006-10-07 21:54:55 +02002307 if (dev->type && dev->type->uevent) {
Kay Sievers7eff2e72007-08-14 15:15:12 +02002308 retval = dev->type->uevent(dev, env);
Kay Sieversf9f852d2006-10-07 21:54:55 +02002309 if (retval)
Greg Kroah-Hartman7dc72b22007-11-28 23:49:41 -08002310 pr_debug("device: '%s': %s: dev_type uevent() "
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01002311 "returned %d\n", dev_name(dev),
Harvey Harrison2b3a3022008-03-04 16:41:05 -08002312 __func__, retval);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07002313 }
2314
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315 return retval;
2316}
2317
Emese Revfy9cd43612009-12-31 14:52:51 +01002318static const struct kset_uevent_ops device_uevent_ops = {
Kay Sievers312c0042005-11-16 09:00:00 +01002319 .filter = dev_uevent_filter,
2320 .name = dev_uevent_name,
2321 .uevent = dev_uevent,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322};
2323
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07002324static ssize_t uevent_show(struct device *dev, struct device_attribute *attr,
Kay Sievers16574dc2007-04-06 01:40:38 +02002325 char *buf)
2326{
2327 struct kobject *top_kobj;
2328 struct kset *kset;
Kay Sievers7eff2e72007-08-14 15:15:12 +02002329 struct kobj_uevent_env *env = NULL;
Kay Sievers16574dc2007-04-06 01:40:38 +02002330 int i;
Joe Perches948b3ed2020-09-16 13:40:42 -07002331 int len = 0;
Kay Sievers16574dc2007-04-06 01:40:38 +02002332 int retval;
2333
2334 /* search the kset, the device belongs to */
2335 top_kobj = &dev->kobj;
Kay Sievers5c5daf62007-08-12 20:43:55 +02002336 while (!top_kobj->kset && top_kobj->parent)
2337 top_kobj = top_kobj->parent;
Kay Sievers16574dc2007-04-06 01:40:38 +02002338 if (!top_kobj->kset)
2339 goto out;
Kay Sievers5c5daf62007-08-12 20:43:55 +02002340
Kay Sievers16574dc2007-04-06 01:40:38 +02002341 kset = top_kobj->kset;
2342 if (!kset->uevent_ops || !kset->uevent_ops->uevent)
2343 goto out;
2344
2345 /* respect filter */
2346 if (kset->uevent_ops && kset->uevent_ops->filter)
2347 if (!kset->uevent_ops->filter(kset, &dev->kobj))
2348 goto out;
2349
Kay Sievers7eff2e72007-08-14 15:15:12 +02002350 env = kzalloc(sizeof(struct kobj_uevent_env), GFP_KERNEL);
2351 if (!env)
Greg Kroah-Hartmanc7308c82007-05-02 14:14:11 +02002352 return -ENOMEM;
2353
Kay Sievers16574dc2007-04-06 01:40:38 +02002354 /* let the kset specific function add its keys */
Kay Sievers7eff2e72007-08-14 15:15:12 +02002355 retval = kset->uevent_ops->uevent(kset, &dev->kobj, env);
Kay Sievers16574dc2007-04-06 01:40:38 +02002356 if (retval)
2357 goto out;
2358
2359 /* copy keys to file */
Kay Sievers7eff2e72007-08-14 15:15:12 +02002360 for (i = 0; i < env->envp_idx; i++)
Joe Perches948b3ed2020-09-16 13:40:42 -07002361 len += sysfs_emit_at(buf, len, "%s\n", env->envp[i]);
Kay Sievers16574dc2007-04-06 01:40:38 +02002362out:
Kay Sievers7eff2e72007-08-14 15:15:12 +02002363 kfree(env);
Joe Perches948b3ed2020-09-16 13:40:42 -07002364 return len;
Kay Sievers16574dc2007-04-06 01:40:38 +02002365}
2366
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07002367static ssize_t uevent_store(struct device *dev, struct device_attribute *attr,
Kay Sieversa7fd6702005-10-01 14:49:43 +02002368 const char *buf, size_t count)
2369{
Peter Rajnohadf44b472018-12-05 12:27:44 +01002370 int rc;
2371
2372 rc = kobject_synth_uevent(&dev->kobj, buf, count);
2373
2374 if (rc) {
Peter Rajnohaf36776f2017-05-09 15:22:30 +02002375 dev_err(dev, "uevent: failed to send synthetic uevent\n");
Peter Rajnohadf44b472018-12-05 12:27:44 +01002376 return rc;
2377 }
Kay Sievers60a96a52007-07-08 22:29:26 +02002378
Kay Sieversa7fd6702005-10-01 14:49:43 +02002379 return count;
2380}
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07002381static DEVICE_ATTR_RW(uevent);
Kay Sieversa7fd6702005-10-01 14:49:43 +02002382
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07002383static ssize_t online_show(struct device *dev, struct device_attribute *attr,
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02002384 char *buf)
2385{
2386 bool val;
2387
Rafael J. Wysocki5e33bc42013-08-28 21:41:01 +02002388 device_lock(dev);
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02002389 val = !dev->offline;
Rafael J. Wysocki5e33bc42013-08-28 21:41:01 +02002390 device_unlock(dev);
Joe Perchesaa838892020-09-16 13:40:39 -07002391 return sysfs_emit(buf, "%u\n", val);
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02002392}
2393
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07002394static ssize_t online_store(struct device *dev, struct device_attribute *attr,
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02002395 const char *buf, size_t count)
2396{
2397 bool val;
2398 int ret;
2399
2400 ret = strtobool(buf, &val);
2401 if (ret < 0)
2402 return ret;
2403
Rafael J. Wysocki5e33bc42013-08-28 21:41:01 +02002404 ret = lock_device_hotplug_sysfs();
2405 if (ret)
2406 return ret;
2407
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02002408 ret = val ? device_online(dev) : device_offline(dev);
2409 unlock_device_hotplug();
2410 return ret < 0 ? ret : count;
2411}
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07002412static DEVICE_ATTR_RW(online);
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02002413
Rajat Jain70f400d2021-05-24 10:18:11 -07002414static ssize_t removable_show(struct device *dev, struct device_attribute *attr,
2415 char *buf)
2416{
2417 const char *loc;
2418
2419 switch (dev->removable) {
2420 case DEVICE_REMOVABLE:
2421 loc = "removable";
2422 break;
2423 case DEVICE_FIXED:
2424 loc = "fixed";
2425 break;
2426 default:
2427 loc = "unknown";
2428 }
2429 return sysfs_emit(buf, "%s\n", loc);
2430}
2431static DEVICE_ATTR_RO(removable);
2432
Greg Kroah-Hartmanfa6fdb32013-08-08 15:22:55 -07002433int device_add_groups(struct device *dev, const struct attribute_group **groups)
Dmitry Torokhov621a1672007-03-10 01:37:34 -05002434{
Greg Kroah-Hartman3e9b2ba2013-08-21 13:47:50 -07002435 return sysfs_create_groups(&dev->kobj, groups);
Dmitry Torokhov621a1672007-03-10 01:37:34 -05002436}
Dmitry Torokhova7670d42017-07-19 17:24:31 -07002437EXPORT_SYMBOL_GPL(device_add_groups);
Dmitry Torokhov621a1672007-03-10 01:37:34 -05002438
Greg Kroah-Hartmanfa6fdb32013-08-08 15:22:55 -07002439void device_remove_groups(struct device *dev,
2440 const struct attribute_group **groups)
Dmitry Torokhov621a1672007-03-10 01:37:34 -05002441{
Greg Kroah-Hartman3e9b2ba2013-08-21 13:47:50 -07002442 sysfs_remove_groups(&dev->kobj, groups);
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -07002443}
Dmitry Torokhova7670d42017-07-19 17:24:31 -07002444EXPORT_SYMBOL_GPL(device_remove_groups);
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -07002445
Dmitry Torokhov57b8ff02017-07-19 17:24:33 -07002446union device_attr_group_devres {
2447 const struct attribute_group *group;
2448 const struct attribute_group **groups;
2449};
2450
2451static int devm_attr_group_match(struct device *dev, void *res, void *data)
2452{
2453 return ((union device_attr_group_devres *)res)->group == data;
2454}
2455
2456static void devm_attr_group_remove(struct device *dev, void *res)
2457{
2458 union device_attr_group_devres *devres = res;
2459 const struct attribute_group *group = devres->group;
2460
2461 dev_dbg(dev, "%s: removing group %p\n", __func__, group);
2462 sysfs_remove_group(&dev->kobj, group);
2463}
2464
2465static void devm_attr_groups_remove(struct device *dev, void *res)
2466{
2467 union device_attr_group_devres *devres = res;
2468 const struct attribute_group **groups = devres->groups;
2469
2470 dev_dbg(dev, "%s: removing groups %p\n", __func__, groups);
2471 sysfs_remove_groups(&dev->kobj, groups);
2472}
2473
2474/**
2475 * devm_device_add_group - given a device, create a managed attribute group
2476 * @dev: The device to create the group for
2477 * @grp: The attribute group to create
2478 *
2479 * This function creates a group for the first time. It will explicitly
2480 * warn and error if any of the attribute files being created already exist.
2481 *
2482 * Returns 0 on success or error code on failure.
2483 */
2484int devm_device_add_group(struct device *dev, const struct attribute_group *grp)
2485{
2486 union device_attr_group_devres *devres;
2487 int error;
2488
2489 devres = devres_alloc(devm_attr_group_remove,
2490 sizeof(*devres), GFP_KERNEL);
2491 if (!devres)
2492 return -ENOMEM;
2493
2494 error = sysfs_create_group(&dev->kobj, grp);
2495 if (error) {
2496 devres_free(devres);
2497 return error;
2498 }
2499
2500 devres->group = grp;
2501 devres_add(dev, devres);
2502 return 0;
2503}
2504EXPORT_SYMBOL_GPL(devm_device_add_group);
2505
2506/**
2507 * devm_device_remove_group: remove a managed group from a device
2508 * @dev: device to remove the group from
2509 * @grp: group to remove
2510 *
2511 * This function removes a group of attributes from a device. The attributes
2512 * previously have to have been created for this group, otherwise it will fail.
2513 */
2514void devm_device_remove_group(struct device *dev,
2515 const struct attribute_group *grp)
2516{
2517 WARN_ON(devres_release(dev, devm_attr_group_remove,
2518 devm_attr_group_match,
2519 /* cast away const */ (void *)grp));
2520}
2521EXPORT_SYMBOL_GPL(devm_device_remove_group);
2522
2523/**
2524 * devm_device_add_groups - create a bunch of managed attribute groups
2525 * @dev: The device to create the group for
2526 * @groups: The attribute groups to create, NULL terminated
2527 *
2528 * This function creates a bunch of managed attribute groups. If an error
2529 * occurs when creating a group, all previously created groups will be
2530 * removed, unwinding everything back to the original state when this
2531 * function was called. It will explicitly warn and error if any of the
2532 * attribute files being created already exist.
2533 *
2534 * Returns 0 on success or error code from sysfs_create_group on failure.
2535 */
2536int devm_device_add_groups(struct device *dev,
2537 const struct attribute_group **groups)
2538{
2539 union device_attr_group_devres *devres;
2540 int error;
2541
2542 devres = devres_alloc(devm_attr_groups_remove,
2543 sizeof(*devres), GFP_KERNEL);
2544 if (!devres)
2545 return -ENOMEM;
2546
2547 error = sysfs_create_groups(&dev->kobj, groups);
2548 if (error) {
2549 devres_free(devres);
2550 return error;
2551 }
2552
2553 devres->groups = groups;
2554 devres_add(dev, devres);
2555 return 0;
2556}
2557EXPORT_SYMBOL_GPL(devm_device_add_groups);
2558
2559/**
2560 * devm_device_remove_groups - remove a list of managed groups
2561 *
2562 * @dev: The device for the groups to be removed from
2563 * @groups: NULL terminated list of groups to be removed
2564 *
2565 * If groups is not NULL, remove the specified groups from the device.
2566 */
2567void devm_device_remove_groups(struct device *dev,
2568 const struct attribute_group **groups)
2569{
2570 WARN_ON(devres_release(dev, devm_attr_groups_remove,
2571 devm_attr_group_match,
2572 /* cast away const */ (void *)groups));
2573}
2574EXPORT_SYMBOL_GPL(devm_device_remove_groups);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07002576static int device_add_attrs(struct device *dev)
2577{
2578 struct class *class = dev->class;
Stephen Hemmingeraed65af2011-03-28 09:12:52 -07002579 const struct device_type *type = dev->type;
Dmitry Torokhov621a1672007-03-10 01:37:34 -05002580 int error;
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07002581
Dmitry Torokhov621a1672007-03-10 01:37:34 -05002582 if (class) {
Greg Kroah-Hartmand05a6f92013-07-14 16:05:58 -07002583 error = device_add_groups(dev, class->dev_groups);
Kay Sieversf9f852d2006-10-07 21:54:55 +02002584 if (error)
Dmitry Torokhov621a1672007-03-10 01:37:34 -05002585 return error;
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07002586 }
Kay Sieversf9f852d2006-10-07 21:54:55 +02002587
Dmitry Torokhov621a1672007-03-10 01:37:34 -05002588 if (type) {
2589 error = device_add_groups(dev, type->groups);
Kay Sieversf9f852d2006-10-07 21:54:55 +02002590 if (error)
Greg Kroah-Hartmana6b01de2013-10-05 18:19:30 -07002591 goto err_remove_class_groups;
Kay Sieversf9f852d2006-10-07 21:54:55 +02002592 }
2593
Dmitry Torokhov621a1672007-03-10 01:37:34 -05002594 error = device_add_groups(dev, dev->groups);
2595 if (error)
2596 goto err_remove_type_groups;
2597
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02002598 if (device_supports_offline(dev) && !dev->offline_disabled) {
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07002599 error = device_create_file(dev, &dev_attr_online);
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02002600 if (error)
Rafael J. Wysockiecfbf6f2013-12-12 06:11:02 +01002601 goto err_remove_dev_groups;
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02002602 }
2603
Saravana Kannan25ac86c2020-11-20 18:02:28 -08002604 if (fw_devlink_flags && !fw_devlink_is_permissive() && dev->fwnode) {
Saravana Kannanda6d6472020-05-21 12:18:00 -07002605 error = device_create_file(dev, &dev_attr_waiting_for_supplier);
2606 if (error)
2607 goto err_remove_dev_online;
2608 }
2609
Rajat Jain70f400d2021-05-24 10:18:11 -07002610 if (dev_removable_is_valid(dev)) {
2611 error = device_create_file(dev, &dev_attr_removable);
2612 if (error)
2613 goto err_remove_dev_waiting_for_supplier;
2614 }
2615
Dmitry Torokhov621a1672007-03-10 01:37:34 -05002616 return 0;
2617
Rajat Jain70f400d2021-05-24 10:18:11 -07002618 err_remove_dev_waiting_for_supplier:
2619 device_remove_file(dev, &dev_attr_waiting_for_supplier);
Saravana Kannanda6d6472020-05-21 12:18:00 -07002620 err_remove_dev_online:
2621 device_remove_file(dev, &dev_attr_online);
Rafael J. Wysockiecfbf6f2013-12-12 06:11:02 +01002622 err_remove_dev_groups:
2623 device_remove_groups(dev, dev->groups);
Dmitry Torokhov621a1672007-03-10 01:37:34 -05002624 err_remove_type_groups:
2625 if (type)
2626 device_remove_groups(dev, type->groups);
Greg Kroah-Hartmand05a6f92013-07-14 16:05:58 -07002627 err_remove_class_groups:
2628 if (class)
2629 device_remove_groups(dev, class->dev_groups);
Dmitry Torokhov621a1672007-03-10 01:37:34 -05002630
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07002631 return error;
2632}
2633
2634static void device_remove_attrs(struct device *dev)
2635{
2636 struct class *class = dev->class;
Stephen Hemmingeraed65af2011-03-28 09:12:52 -07002637 const struct device_type *type = dev->type;
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07002638
Rajat Jain70f400d2021-05-24 10:18:11 -07002639 device_remove_file(dev, &dev_attr_removable);
Saravana Kannanda6d6472020-05-21 12:18:00 -07002640 device_remove_file(dev, &dev_attr_waiting_for_supplier);
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07002641 device_remove_file(dev, &dev_attr_online);
Dmitry Torokhov621a1672007-03-10 01:37:34 -05002642 device_remove_groups(dev, dev->groups);
Kay Sieversf9f852d2006-10-07 21:54:55 +02002643
Dmitry Torokhov621a1672007-03-10 01:37:34 -05002644 if (type)
2645 device_remove_groups(dev, type->groups);
2646
Greg Kroah-Hartmana6b01de2013-10-05 18:19:30 -07002647 if (class)
Greg Kroah-Hartmand05a6f92013-07-14 16:05:58 -07002648 device_remove_groups(dev, class->dev_groups);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07002649}
2650
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07002651static ssize_t dev_show(struct device *dev, struct device_attribute *attr,
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07002652 char *buf)
2653{
2654 return print_dev_t(buf, dev->devt);
2655}
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07002656static DEVICE_ATTR_RO(dev);
Tejun Heoad6a1e12007-06-14 03:45:17 +09002657
Kay Sieversca22e562011-12-14 14:29:38 -08002658/* /sys/devices/ */
Greg Kroah-Hartman881c6cfd2007-11-01 09:29:06 -06002659struct kset *devices_kset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661/**
Grygorii Strashko52cdbdd2015-07-27 20:43:01 +03002662 * devices_kset_move_before - Move device in the devices_kset's list.
2663 * @deva: Device to move.
2664 * @devb: Device @deva should come before.
2665 */
2666static void devices_kset_move_before(struct device *deva, struct device *devb)
2667{
2668 if (!devices_kset)
2669 return;
2670 pr_debug("devices_kset: Moving %s before %s\n",
2671 dev_name(deva), dev_name(devb));
2672 spin_lock(&devices_kset->list_lock);
2673 list_move_tail(&deva->kobj.entry, &devb->kobj.entry);
2674 spin_unlock(&devices_kset->list_lock);
2675}
2676
2677/**
2678 * devices_kset_move_after - Move device in the devices_kset's list.
2679 * @deva: Device to move
2680 * @devb: Device @deva should come after.
2681 */
2682static void devices_kset_move_after(struct device *deva, struct device *devb)
2683{
2684 if (!devices_kset)
2685 return;
2686 pr_debug("devices_kset: Moving %s after %s\n",
2687 dev_name(deva), dev_name(devb));
2688 spin_lock(&devices_kset->list_lock);
2689 list_move(&deva->kobj.entry, &devb->kobj.entry);
2690 spin_unlock(&devices_kset->list_lock);
2691}
2692
2693/**
2694 * devices_kset_move_last - move the device to the end of devices_kset's list.
2695 * @dev: device to move
2696 */
2697void devices_kset_move_last(struct device *dev)
2698{
2699 if (!devices_kset)
2700 return;
2701 pr_debug("devices_kset: Moving %s to end of list\n", dev_name(dev));
2702 spin_lock(&devices_kset->list_lock);
2703 list_move_tail(&dev->kobj.entry, &devices_kset->list);
2704 spin_unlock(&devices_kset->list_lock);
2705}
2706
2707/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002708 * device_create_file - create sysfs attribute file for device.
2709 * @dev: device.
2710 * @attr: device attribute descriptor.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711 */
Phil Carmody26579ab2009-12-18 15:34:19 +02002712int device_create_file(struct device *dev,
2713 const struct device_attribute *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714{
2715 int error = 0;
Felipe Balbi8f46baa2013-02-20 10:31:42 +02002716
2717 if (dev) {
2718 WARN(((attr->attr.mode & S_IWUGO) && !attr->store),
dyoung@redhat.com97521972013-05-16 14:31:30 +08002719 "Attribute %s: write permission without 'store'\n",
2720 attr->attr.name);
Felipe Balbi8f46baa2013-02-20 10:31:42 +02002721 WARN(((attr->attr.mode & S_IRUGO) && !attr->show),
dyoung@redhat.com97521972013-05-16 14:31:30 +08002722 "Attribute %s: read permission without 'show'\n",
2723 attr->attr.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724 error = sysfs_create_file(&dev->kobj, &attr->attr);
Felipe Balbi8f46baa2013-02-20 10:31:42 +02002725 }
2726
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727 return error;
2728}
David Graham White86df2682013-07-21 20:41:14 -04002729EXPORT_SYMBOL_GPL(device_create_file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730
2731/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002732 * device_remove_file - remove sysfs attribute file.
2733 * @dev: device.
2734 * @attr: device attribute descriptor.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735 */
Phil Carmody26579ab2009-12-18 15:34:19 +02002736void device_remove_file(struct device *dev,
2737 const struct device_attribute *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738{
Cornelia Huck0c98b192008-01-31 10:39:38 +01002739 if (dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740 sysfs_remove_file(&dev->kobj, &attr->attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741}
David Graham White86df2682013-07-21 20:41:14 -04002742EXPORT_SYMBOL_GPL(device_remove_file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743
Greg Kroah-Hartman2589f1882006-09-19 09:39:19 -07002744/**
Tejun Heo6b0afc22014-02-03 14:03:01 -05002745 * device_remove_file_self - remove sysfs attribute file from its own method.
2746 * @dev: device.
2747 * @attr: device attribute descriptor.
2748 *
2749 * See kernfs_remove_self() for details.
2750 */
2751bool device_remove_file_self(struct device *dev,
2752 const struct device_attribute *attr)
2753{
2754 if (dev)
2755 return sysfs_remove_file_self(&dev->kobj, &attr->attr);
2756 else
2757 return false;
2758}
2759EXPORT_SYMBOL_GPL(device_remove_file_self);
2760
2761/**
Greg Kroah-Hartman2589f1882006-09-19 09:39:19 -07002762 * device_create_bin_file - create sysfs binary attribute file for device.
2763 * @dev: device.
2764 * @attr: device binary attribute descriptor.
2765 */
Phil Carmody66ecb922009-12-18 15:34:20 +02002766int device_create_bin_file(struct device *dev,
2767 const struct bin_attribute *attr)
Greg Kroah-Hartman2589f1882006-09-19 09:39:19 -07002768{
2769 int error = -EINVAL;
2770 if (dev)
2771 error = sysfs_create_bin_file(&dev->kobj, attr);
2772 return error;
2773}
2774EXPORT_SYMBOL_GPL(device_create_bin_file);
2775
2776/**
2777 * device_remove_bin_file - remove sysfs binary attribute file
2778 * @dev: device.
2779 * @attr: device binary attribute descriptor.
2780 */
Phil Carmody66ecb922009-12-18 15:34:20 +02002781void device_remove_bin_file(struct device *dev,
2782 const struct bin_attribute *attr)
Greg Kroah-Hartman2589f1882006-09-19 09:39:19 -07002783{
2784 if (dev)
2785 sysfs_remove_bin_file(&dev->kobj, attr);
2786}
2787EXPORT_SYMBOL_GPL(device_remove_bin_file);
2788
James Bottomley34bb61f2005-09-06 16:56:51 -07002789static void klist_children_get(struct klist_node *n)
2790{
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08002791 struct device_private *p = to_device_private_parent(n);
2792 struct device *dev = p->device;
James Bottomley34bb61f2005-09-06 16:56:51 -07002793
2794 get_device(dev);
2795}
2796
2797static void klist_children_put(struct klist_node *n)
2798{
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08002799 struct device_private *p = to_device_private_parent(n);
2800 struct device *dev = p->device;
James Bottomley34bb61f2005-09-06 16:56:51 -07002801
2802 put_device(dev);
2803}
2804
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002806 * device_initialize - init device structure.
2807 * @dev: device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808 *
Cornelia Huck57394112008-09-03 18:26:40 +02002809 * This prepares the device for use by other layers by initializing
2810 * its fields.
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002811 * It is the first half of device_register(), if called by
Cornelia Huck57394112008-09-03 18:26:40 +02002812 * that function, though it can also be called separately, so one
2813 * may use @dev's fields. In particular, get_device()/put_device()
2814 * may be used for reference counting of @dev after calling this
2815 * function.
2816 *
Alan Sternb10d5ef2012-01-17 11:39:00 -05002817 * All fields in @dev must be initialized by the caller to 0, except
2818 * for those explicitly set to some other value. The simplest
2819 * approach is to use kzalloc() to allocate the structure containing
2820 * @dev.
2821 *
Cornelia Huck57394112008-09-03 18:26:40 +02002822 * NOTE: Use put_device() to give up your reference instead of freeing
2823 * @dev directly once you have called this function.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825void device_initialize(struct device *dev)
2826{
Greg Kroah-Hartman881c6cfd2007-11-01 09:29:06 -06002827 dev->kobj.kset = devices_kset;
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -07002828 kobject_init(&dev->kobj, &device_ktype);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829 INIT_LIST_HEAD(&dev->dma_pools);
Thomas Gleixner31427882010-01-29 20:39:02 +00002830 mutex_init(&dev->mutex);
Dan Williams87a30e12019-07-17 18:08:26 -07002831#ifdef CONFIG_PROVE_LOCKING
2832 mutex_init(&dev->lockdep_mutex);
2833#endif
Peter Zijlstra1704f472010-03-19 01:37:42 +01002834 lockdep_set_novalidate_class(&dev->mutex);
Tejun Heo9ac78492007-01-20 16:00:26 +09002835 spin_lock_init(&dev->devres_lock);
2836 INIT_LIST_HEAD(&dev->devres_head);
Alan Stern3b98aea2008-08-07 13:06:12 -04002837 device_pm_init(dev);
Christoph Hellwig87348132006-12-06 20:32:33 -08002838 set_dev_node(dev, -1);
Jiang Liu4a7cc832015-07-09 16:00:44 +08002839#ifdef CONFIG_GENERIC_MSI_IRQ
2840 INIT_LIST_HEAD(&dev->msi_list);
2841#endif
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01002842 INIT_LIST_HEAD(&dev->links.consumers);
2843 INIT_LIST_HEAD(&dev->links.suppliers);
Saravana Kannan3b052a32020-11-20 18:02:17 -08002844 INIT_LIST_HEAD(&dev->links.defer_sync);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01002845 dev->links.status = DL_DEV_NO_DRIVER;
Christoph Hellwig6d4e9a82021-02-10 10:56:39 +01002846#if defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_DEVICE) || \
2847 defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU) || \
2848 defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU_ALL)
2849 dev->dma_coherent = dma_default_coherent;
2850#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851}
David Graham White86df2682013-07-21 20:41:14 -04002852EXPORT_SYMBOL_GPL(device_initialize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853
Tejun Heod73ce002013-03-12 11:30:05 -07002854struct kobject *virtual_device_parent(struct device *dev)
Greg Kroah-Hartmanf0ee61a2006-10-23 10:40:54 -07002855{
Kay Sievers86406242007-03-14 03:25:56 +01002856 static struct kobject *virtual_dir = NULL;
Greg Kroah-Hartmanf0ee61a2006-10-23 10:40:54 -07002857
Kay Sievers86406242007-03-14 03:25:56 +01002858 if (!virtual_dir)
Greg Kroah-Hartman4ff6abf2007-11-05 22:24:43 -08002859 virtual_dir = kobject_create_and_add("virtual",
Greg Kroah-Hartman881c6cfd2007-11-01 09:29:06 -06002860 &devices_kset->kobj);
Greg Kroah-Hartmanf0ee61a2006-10-23 10:40:54 -07002861
Kay Sievers86406242007-03-14 03:25:56 +01002862 return virtual_dir;
Greg Kroah-Hartmanf0ee61a2006-10-23 10:40:54 -07002863}
2864
Eric W. Biedermanbc451f22010-03-30 11:31:25 -07002865struct class_dir {
2866 struct kobject kobj;
2867 struct class *class;
2868};
2869
2870#define to_class_dir(obj) container_of(obj, struct class_dir, kobj)
2871
2872static void class_dir_release(struct kobject *kobj)
2873{
2874 struct class_dir *dir = to_class_dir(kobj);
2875 kfree(dir);
2876}
2877
2878static const
2879struct kobj_ns_type_operations *class_dir_child_ns_type(struct kobject *kobj)
2880{
2881 struct class_dir *dir = to_class_dir(kobj);
2882 return dir->class->ns_type;
2883}
2884
2885static struct kobj_type class_dir_ktype = {
2886 .release = class_dir_release,
2887 .sysfs_ops = &kobj_sysfs_ops,
2888 .child_ns_type = class_dir_child_ns_type
2889};
2890
2891static struct kobject *
2892class_dir_create_and_add(struct class *class, struct kobject *parent_kobj)
2893{
2894 struct class_dir *dir;
2895 int retval;
2896
2897 dir = kzalloc(sizeof(*dir), GFP_KERNEL);
2898 if (!dir)
Tetsuo Handa84d0c272018-05-07 19:10:31 +09002899 return ERR_PTR(-ENOMEM);
Eric W. Biedermanbc451f22010-03-30 11:31:25 -07002900
2901 dir->class = class;
2902 kobject_init(&dir->kobj, &class_dir_ktype);
2903
Kay Sievers6b6e39a2010-11-15 23:13:18 +01002904 dir->kobj.kset = &class->p->glue_dirs;
Eric W. Biedermanbc451f22010-03-30 11:31:25 -07002905
2906 retval = kobject_add(&dir->kobj, parent_kobj, "%s", class->name);
2907 if (retval < 0) {
2908 kobject_put(&dir->kobj);
Tetsuo Handa84d0c272018-05-07 19:10:31 +09002909 return ERR_PTR(retval);
Eric W. Biedermanbc451f22010-03-30 11:31:25 -07002910 }
2911 return &dir->kobj;
2912}
2913
Yijing Wange4a60d12014-11-07 12:05:49 +08002914static DEFINE_MUTEX(gdp_mutex);
Eric W. Biedermanbc451f22010-03-30 11:31:25 -07002915
Kay Sieversda231fd2007-11-21 17:29:15 +01002916static struct kobject *get_device_parent(struct device *dev,
2917 struct device *parent)
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +01002918{
Kay Sievers86406242007-03-14 03:25:56 +01002919 if (dev->class) {
2920 struct kobject *kobj = NULL;
2921 struct kobject *parent_kobj;
2922 struct kobject *k;
2923
Randy Dunlapead454f2010-09-24 14:36:49 -07002924#ifdef CONFIG_BLOCK
Kay Sievers39aba962010-09-04 22:33:14 -07002925 /* block disks show up in /sys/block */
Andi Kleene52eec12010-09-08 16:54:17 +02002926 if (sysfs_deprecated && dev->class == &block_class) {
Kay Sievers39aba962010-09-04 22:33:14 -07002927 if (parent && parent->class == &block_class)
2928 return &parent->kobj;
Kay Sievers6b6e39a2010-11-15 23:13:18 +01002929 return &block_class.p->subsys.kobj;
Kay Sievers39aba962010-09-04 22:33:14 -07002930 }
Randy Dunlapead454f2010-09-24 14:36:49 -07002931#endif
Andi Kleene52eec12010-09-08 16:54:17 +02002932
Kay Sievers86406242007-03-14 03:25:56 +01002933 /*
2934 * If we have no parent, we live in "virtual".
Kay Sievers0f4dafc2007-12-19 01:40:42 +01002935 * Class-devices with a non class-device as parent, live
2936 * in a "glue" directory to prevent namespace collisions.
Kay Sievers86406242007-03-14 03:25:56 +01002937 */
2938 if (parent == NULL)
2939 parent_kobj = virtual_device_parent(dev);
Eric W. Biederman24b14422010-07-24 22:43:35 -07002940 else if (parent->class && !dev->class->ns_type)
Kay Sievers86406242007-03-14 03:25:56 +01002941 return &parent->kobj;
2942 else
2943 parent_kobj = &parent->kobj;
2944
Tejun Heo77d3d7c2010-02-05 17:57:02 +09002945 mutex_lock(&gdp_mutex);
2946
Kay Sievers86406242007-03-14 03:25:56 +01002947 /* find our class-directory at the parent and reference it */
Kay Sievers6b6e39a2010-11-15 23:13:18 +01002948 spin_lock(&dev->class->p->glue_dirs.list_lock);
2949 list_for_each_entry(k, &dev->class->p->glue_dirs.list, entry)
Kay Sievers86406242007-03-14 03:25:56 +01002950 if (k->parent == parent_kobj) {
2951 kobj = kobject_get(k);
2952 break;
2953 }
Kay Sievers6b6e39a2010-11-15 23:13:18 +01002954 spin_unlock(&dev->class->p->glue_dirs.list_lock);
Tejun Heo77d3d7c2010-02-05 17:57:02 +09002955 if (kobj) {
2956 mutex_unlock(&gdp_mutex);
Kay Sievers86406242007-03-14 03:25:56 +01002957 return kobj;
Tejun Heo77d3d7c2010-02-05 17:57:02 +09002958 }
Kay Sievers86406242007-03-14 03:25:56 +01002959
2960 /* or create a new class-directory at the parent device */
Eric W. Biedermanbc451f22010-03-30 11:31:25 -07002961 k = class_dir_create_and_add(dev->class, parent_kobj);
Kay Sievers0f4dafc2007-12-19 01:40:42 +01002962 /* do not emit an uevent for this simple "glue" directory */
Tejun Heo77d3d7c2010-02-05 17:57:02 +09002963 mutex_unlock(&gdp_mutex);
Greg Kroah-Hartman43968d22007-11-05 22:24:43 -08002964 return k;
Kay Sievers86406242007-03-14 03:25:56 +01002965 }
2966
Kay Sieversca22e562011-12-14 14:29:38 -08002967 /* subsystems can specify a default root directory for their devices */
2968 if (!parent && dev->bus && dev->bus->dev_root)
2969 return &dev->bus->dev_root->kobj;
2970
Kay Sievers86406242007-03-14 03:25:56 +01002971 if (parent)
Cornelia Huckc744aeae2007-01-08 20:16:44 +01002972 return &parent->kobj;
2973 return NULL;
2974}
Kay Sieversda231fd2007-11-21 17:29:15 +01002975
Ming Leicebf8fd2016-07-10 19:27:36 +08002976static inline bool live_in_glue_dir(struct kobject *kobj,
2977 struct device *dev)
2978{
2979 if (!kobj || !dev->class ||
2980 kobj->kset != &dev->class->p->glue_dirs)
2981 return false;
2982 return true;
2983}
2984
2985static inline struct kobject *get_glue_dir(struct device *dev)
2986{
2987 return dev->kobj.parent;
2988}
2989
2990/*
2991 * make sure cleaning up dir as the last step, we need to make
2992 * sure .release handler of kobject is run with holding the
2993 * global lock
2994 */
Cornelia Huck63b69712008-01-21 16:09:44 +01002995static void cleanup_glue_dir(struct device *dev, struct kobject *glue_dir)
Kay Sieversda231fd2007-11-21 17:29:15 +01002996{
Muchun Songac434322019-07-27 11:21:22 +08002997 unsigned int ref;
2998
Kay Sievers0f4dafc2007-12-19 01:40:42 +01002999 /* see if we live in a "glue" directory */
Ming Leicebf8fd2016-07-10 19:27:36 +08003000 if (!live_in_glue_dir(glue_dir, dev))
Kay Sieversda231fd2007-11-21 17:29:15 +01003001 return;
3002
Yijing Wange4a60d12014-11-07 12:05:49 +08003003 mutex_lock(&gdp_mutex);
Muchun Songac434322019-07-27 11:21:22 +08003004 /**
3005 * There is a race condition between removing glue directory
3006 * and adding a new device under the glue directory.
3007 *
3008 * CPU1: CPU2:
3009 *
3010 * device_add()
3011 * get_device_parent()
3012 * class_dir_create_and_add()
3013 * kobject_add_internal()
3014 * create_dir() // create glue_dir
3015 *
3016 * device_add()
3017 * get_device_parent()
3018 * kobject_get() // get glue_dir
3019 *
3020 * device_del()
3021 * cleanup_glue_dir()
3022 * kobject_del(glue_dir)
3023 *
3024 * kobject_add()
3025 * kobject_add_internal()
3026 * create_dir() // in glue_dir
3027 * sysfs_create_dir_ns()
3028 * kernfs_create_dir_ns(sd)
3029 *
3030 * sysfs_remove_dir() // glue_dir->sd=NULL
3031 * sysfs_put() // free glue_dir->sd
3032 *
3033 * // sd is freed
3034 * kernfs_new_node(sd)
3035 * kernfs_get(glue_dir)
3036 * kernfs_add_one()
3037 * kernfs_put()
3038 *
3039 * Before CPU1 remove last child device under glue dir, if CPU2 add
3040 * a new device under glue dir, the glue_dir kobject reference count
3041 * will be increase to 2 in kobject_get(k). And CPU2 has been called
3042 * kernfs_create_dir_ns(). Meanwhile, CPU1 call sysfs_remove_dir()
3043 * and sysfs_put(). This result in glue_dir->sd is freed.
3044 *
3045 * Then the CPU2 will see a stale "empty" but still potentially used
3046 * glue dir around in kernfs_new_node().
3047 *
3048 * In order to avoid this happening, we also should make sure that
3049 * kernfs_node for glue_dir is released in CPU1 only when refcount
3050 * for glue_dir kobj is 1.
3051 */
3052 ref = kref_read(&glue_dir->kref);
3053 if (!kobject_has_children(glue_dir) && !--ref)
Benjamin Herrenschmidt726e4102018-07-10 10:29:10 +10003054 kobject_del(glue_dir);
Kay Sievers0f4dafc2007-12-19 01:40:42 +01003055 kobject_put(glue_dir);
Yijing Wange4a60d12014-11-07 12:05:49 +08003056 mutex_unlock(&gdp_mutex);
Kay Sieversda231fd2007-11-21 17:29:15 +01003057}
Cornelia Huck63b69712008-01-21 16:09:44 +01003058
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07003059static int device_add_class_symlinks(struct device *dev)
3060{
Benjamin Herrenschmidt5590f312015-02-18 11:25:18 +11003061 struct device_node *of_node = dev_of_node(dev);
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07003062 int error;
3063
Benjamin Herrenschmidt5590f312015-02-18 11:25:18 +11003064 if (of_node) {
Rob Herring0c3c2342017-10-04 14:04:01 -05003065 error = sysfs_create_link(&dev->kobj, of_node_kobj(of_node), "of_node");
Benjamin Herrenschmidt5590f312015-02-18 11:25:18 +11003066 if (error)
3067 dev_warn(dev, "Error %d creating of_node link\n",error);
3068 /* An error here doesn't warrant bringing down the device */
3069 }
3070
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07003071 if (!dev->class)
3072 return 0;
Kay Sieversda231fd2007-11-21 17:29:15 +01003073
Greg Kroah-Hartman1fbfee62008-05-28 09:28:39 -07003074 error = sysfs_create_link(&dev->kobj,
Kay Sievers6b6e39a2010-11-15 23:13:18 +01003075 &dev->class->p->subsys.kobj,
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07003076 "subsystem");
3077 if (error)
Benjamin Herrenschmidt5590f312015-02-18 11:25:18 +11003078 goto out_devnode;
Kay Sieversda231fd2007-11-21 17:29:15 +01003079
Greg Kroah-Hartman4e886c22008-01-27 12:12:43 -08003080 if (dev->parent && device_is_not_partition(dev)) {
Dmitry Torokhov4f01a752007-09-18 22:46:50 -07003081 error = sysfs_create_link(&dev->kobj, &dev->parent->kobj,
3082 "device");
3083 if (error)
Kay Sievers39aba962010-09-04 22:33:14 -07003084 goto out_subsys;
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07003085 }
Kay Sievers39aba962010-09-04 22:33:14 -07003086
Randy Dunlapead454f2010-09-24 14:36:49 -07003087#ifdef CONFIG_BLOCK
Kay Sievers39aba962010-09-04 22:33:14 -07003088 /* /sys/block has directories and does not need symlinks */
Andi Kleene52eec12010-09-08 16:54:17 +02003089 if (sysfs_deprecated && dev->class == &block_class)
Kay Sievers39aba962010-09-04 22:33:14 -07003090 return 0;
Randy Dunlapead454f2010-09-24 14:36:49 -07003091#endif
Kay Sievers39aba962010-09-04 22:33:14 -07003092
3093 /* link in the class directory pointing to the device */
Kay Sievers6b6e39a2010-11-15 23:13:18 +01003094 error = sysfs_create_link(&dev->class->p->subsys.kobj,
Kay Sievers39aba962010-09-04 22:33:14 -07003095 &dev->kobj, dev_name(dev));
3096 if (error)
3097 goto out_device;
3098
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07003099 return 0;
3100
Kay Sievers39aba962010-09-04 22:33:14 -07003101out_device:
3102 sysfs_remove_link(&dev->kobj, "device");
Kay Sieversda231fd2007-11-21 17:29:15 +01003103
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07003104out_subsys:
3105 sysfs_remove_link(&dev->kobj, "subsystem");
Benjamin Herrenschmidt5590f312015-02-18 11:25:18 +11003106out_devnode:
3107 sysfs_remove_link(&dev->kobj, "of_node");
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07003108 return error;
3109}
3110
3111static void device_remove_class_symlinks(struct device *dev)
3112{
Benjamin Herrenschmidt5590f312015-02-18 11:25:18 +11003113 if (dev_of_node(dev))
3114 sysfs_remove_link(&dev->kobj, "of_node");
3115
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07003116 if (!dev->class)
3117 return;
Kay Sieversda231fd2007-11-21 17:29:15 +01003118
Greg Kroah-Hartman4e886c22008-01-27 12:12:43 -08003119 if (dev->parent && device_is_not_partition(dev))
Kay Sieversda231fd2007-11-21 17:29:15 +01003120 sysfs_remove_link(&dev->kobj, "device");
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07003121 sysfs_remove_link(&dev->kobj, "subsystem");
Randy Dunlapead454f2010-09-24 14:36:49 -07003122#ifdef CONFIG_BLOCK
Andi Kleene52eec12010-09-08 16:54:17 +02003123 if (sysfs_deprecated && dev->class == &block_class)
Kay Sievers39aba962010-09-04 22:33:14 -07003124 return;
Randy Dunlapead454f2010-09-24 14:36:49 -07003125#endif
Kay Sievers6b6e39a2010-11-15 23:13:18 +01003126 sysfs_delete_link(&dev->class->p->subsys.kobj, &dev->kobj, dev_name(dev));
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07003127}
3128
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129/**
Stephen Rothwell413c2392008-05-30 10:16:40 +10003130 * dev_set_name - set a device name
3131 * @dev: device
Randy Dunlap46232362008-06-04 21:40:43 -07003132 * @fmt: format string for the device's name
Stephen Rothwell413c2392008-05-30 10:16:40 +10003133 */
3134int dev_set_name(struct device *dev, const char *fmt, ...)
3135{
3136 va_list vargs;
Kay Sievers1fa5ae82009-01-25 15:17:37 +01003137 int err;
Stephen Rothwell413c2392008-05-30 10:16:40 +10003138
3139 va_start(vargs, fmt);
Kay Sievers1fa5ae82009-01-25 15:17:37 +01003140 err = kobject_set_name_vargs(&dev->kobj, fmt, vargs);
Stephen Rothwell413c2392008-05-30 10:16:40 +10003141 va_end(vargs);
Kay Sievers1fa5ae82009-01-25 15:17:37 +01003142 return err;
Stephen Rothwell413c2392008-05-30 10:16:40 +10003143}
3144EXPORT_SYMBOL_GPL(dev_set_name);
3145
3146/**
Dan Williamse105b8b2008-04-21 10:51:07 -07003147 * device_to_dev_kobj - select a /sys/dev/ directory for the device
3148 * @dev: device
3149 *
3150 * By default we select char/ for new entries. Setting class->dev_obj
3151 * to NULL prevents an entry from being created. class->dev_kobj must
3152 * be set (or cleared) before any devices are registered to the class
3153 * otherwise device_create_sys_dev_entry() and
Peter Korsgaard0d4e293c2012-04-17 12:12:57 +02003154 * device_remove_sys_dev_entry() will disagree about the presence of
3155 * the link.
Dan Williamse105b8b2008-04-21 10:51:07 -07003156 */
3157static struct kobject *device_to_dev_kobj(struct device *dev)
3158{
3159 struct kobject *kobj;
3160
3161 if (dev->class)
3162 kobj = dev->class->dev_kobj;
3163 else
3164 kobj = sysfs_dev_char_kobj;
3165
3166 return kobj;
3167}
3168
3169static int device_create_sys_dev_entry(struct device *dev)
3170{
3171 struct kobject *kobj = device_to_dev_kobj(dev);
3172 int error = 0;
3173 char devt_str[15];
3174
3175 if (kobj) {
3176 format_dev_t(devt_str, dev->devt);
3177 error = sysfs_create_link(kobj, &dev->kobj, devt_str);
3178 }
3179
3180 return error;
3181}
3182
3183static void device_remove_sys_dev_entry(struct device *dev)
3184{
3185 struct kobject *kobj = device_to_dev_kobj(dev);
3186 char devt_str[15];
3187
3188 if (kobj) {
3189 format_dev_t(devt_str, dev->devt);
3190 sysfs_remove_link(kobj, devt_str);
3191 }
3192}
3193
Shaokun Zhang46d3a032018-07-15 18:08:56 +08003194static int device_private_init(struct device *dev)
Greg Kroah-Hartmanb4028432009-05-11 14:16:57 -07003195{
3196 dev->p = kzalloc(sizeof(*dev->p), GFP_KERNEL);
3197 if (!dev->p)
3198 return -ENOMEM;
3199 dev->p->device = dev;
3200 klist_init(&dev->p->klist_children, klist_children_get,
3201 klist_children_put);
Greg Kroah-Hartmanef8a3fd2012-03-08 12:17:22 -08003202 INIT_LIST_HEAD(&dev->p->deferred_probe);
Greg Kroah-Hartmanb4028432009-05-11 14:16:57 -07003203 return 0;
3204}
3205
Dan Williamse105b8b2008-04-21 10:51:07 -07003206/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08003207 * device_add - add device to device hierarchy.
3208 * @dev: device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08003210 * This is part 2 of device_register(), though may be called
3211 * separately _iff_ device_initialize() has been called separately.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003212 *
Cornelia Huck57394112008-09-03 18:26:40 +02003213 * This adds @dev to the kobject hierarchy via kobject_add(), adds it
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08003214 * to the global and sibling lists for the device, then
3215 * adds it to the other relevant subsystems of the driver model.
Cornelia Huck57394112008-09-03 18:26:40 +02003216 *
Alan Sternb10d5ef2012-01-17 11:39:00 -05003217 * Do not call this routine or device_register() more than once for
3218 * any device structure. The driver model core is not designed to work
3219 * with devices that get unregistered and then spring back to life.
3220 * (Among other things, it's very hard to guarantee that all references
3221 * to the previous incarnation of @dev have been dropped.) Allocate
3222 * and register a fresh new struct device instead.
3223 *
Cornelia Huck57394112008-09-03 18:26:40 +02003224 * NOTE: _Never_ directly free @dev after calling this function, even
3225 * if it returned an error! Always use put_device() to give up your
3226 * reference instead.
Borislav Petkovaffada72019-04-18 19:41:56 +02003227 *
3228 * Rule of thumb is: if device_add() succeeds, you should call
3229 * device_del() when you want to get rid of it. If device_add() has
3230 * *not* succeeded, use *only* put_device() to drop the reference
3231 * count.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003232 */
3233int device_add(struct device *dev)
3234{
Viresh Kumar35dbf4e2017-03-17 12:24:22 +05303235 struct device *parent;
Kay Sieversca22e562011-12-14 14:29:38 -08003236 struct kobject *kobj;
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02003237 struct class_interface *class_intf;
Saravana Kannan5f5377e2020-05-14 22:34:58 -07003238 int error = -EINVAL;
Ming Leicebf8fd2016-07-10 19:27:36 +08003239 struct kobject *glue_dir = NULL;
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01003240
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241 dev = get_device(dev);
Greg Kroah-Hartmanc906a482008-05-30 10:45:12 -07003242 if (!dev)
3243 goto done;
3244
Greg Kroah-Hartmanfb069a52008-12-16 12:23:36 -08003245 if (!dev->p) {
Greg Kroah-Hartmanb4028432009-05-11 14:16:57 -07003246 error = device_private_init(dev);
3247 if (error)
3248 goto done;
Greg Kroah-Hartmanfb069a52008-12-16 12:23:36 -08003249 }
Greg Kroah-Hartmanfb069a52008-12-16 12:23:36 -08003250
Kay Sievers1fa5ae82009-01-25 15:17:37 +01003251 /*
3252 * for statically allocated devices, which should all be converted
3253 * some day, we need to initialize the name. We prevent reading back
3254 * the name, and force the use of dev_name()
3255 */
3256 if (dev->init_name) {
Greg Kroah-Hartmanacc0e902009-06-02 15:39:55 -07003257 dev_set_name(dev, "%s", dev->init_name);
Kay Sievers1fa5ae82009-01-25 15:17:37 +01003258 dev->init_name = NULL;
3259 }
Greg Kroah-Hartmanc906a482008-05-30 10:45:12 -07003260
Kay Sieversca22e562011-12-14 14:29:38 -08003261 /* subsystems can specify simple device enumeration */
3262 if (!dev_name(dev) && dev->bus && dev->bus->dev_name)
3263 dev_set_name(dev, "%s%u", dev->bus->dev_name, dev->id);
3264
Thomas Gleixnere6309e72009-12-10 19:32:49 +00003265 if (!dev_name(dev)) {
3266 error = -EINVAL;
Kay Sievers5c8563d2009-05-28 14:24:07 -07003267 goto name_error;
Thomas Gleixnere6309e72009-12-10 19:32:49 +00003268 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003269
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01003270 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
Greg Kroah-Hartmanc205ef42006-08-07 22:19:37 -07003271
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272 parent = get_device(dev->parent);
Kay Sieversca22e562011-12-14 14:29:38 -08003273 kobj = get_device_parent(dev, parent);
Tetsuo Handa84d0c272018-05-07 19:10:31 +09003274 if (IS_ERR(kobj)) {
3275 error = PTR_ERR(kobj);
3276 goto parent_error;
3277 }
Kay Sieversca22e562011-12-14 14:29:38 -08003278 if (kobj)
3279 dev->kobj.parent = kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003280
Yinghai Lu0d358f22008-02-19 03:20:41 -08003281 /* use parent numa_node */
Zhen Lei56f2de82015-08-25 12:08:22 +08003282 if (parent && (dev_to_node(dev) == NUMA_NO_NODE))
Yinghai Lu0d358f22008-02-19 03:20:41 -08003283 set_dev_node(dev, dev_to_node(parent));
3284
Linus Torvalds1da177e2005-04-16 15:20:36 -07003285 /* first, register with generic layer. */
Kay Sievers8a577ff2009-04-18 15:05:45 -07003286 /* we require the name to be set before, and pass NULL */
3287 error = kobject_add(&dev->kobj, dev->kobj.parent, NULL);
Ming Leicebf8fd2016-07-10 19:27:36 +08003288 if (error) {
3289 glue_dir = get_glue_dir(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003290 goto Error;
Ming Leicebf8fd2016-07-10 19:27:36 +08003291 }
Kay Sieversa7fd6702005-10-01 14:49:43 +02003292
Brian Walsh37022642006-08-14 22:43:19 -07003293 /* notify platform of device entry */
Heikki Krogerus07de0e82018-11-09 17:21:34 +03003294 error = device_platform_notify(dev, KOBJ_ADD);
3295 if (error)
3296 goto platform_error;
Brian Walsh37022642006-08-14 22:43:19 -07003297
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07003298 error = device_create_file(dev, &dev_attr_uevent);
Cornelia Hucka306eea2006-09-22 11:37:13 +02003299 if (error)
3300 goto attrError;
Kay Sieversa7fd6702005-10-01 14:49:43 +02003301
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07003302 error = device_add_class_symlinks(dev);
3303 if (error)
3304 goto SymlinkError;
Cornelia Huckdc0afa82007-07-09 11:39:18 -07003305 error = device_add_attrs(dev);
3306 if (error)
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07003307 goto AttrsError;
Cornelia Huckdc0afa82007-07-09 11:39:18 -07003308 error = bus_add_device(dev);
3309 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003310 goto BusError;
Alan Stern3b98aea2008-08-07 13:06:12 -04003311 error = dpm_sysfs_add(dev);
Rafael J. Wysocki57eee3d2008-03-12 00:59:38 +01003312 if (error)
Alan Stern3b98aea2008-08-07 13:06:12 -04003313 goto DPMError;
3314 device_pm_add(dev);
Alan Sternec0676ee2008-12-05 14:10:31 -05003315
Sergey Klyaus0cd75042014-10-08 11:31:54 +04003316 if (MAJOR(dev->devt)) {
3317 error = device_create_file(dev, &dev_attr_dev);
3318 if (error)
3319 goto DevAttrError;
3320
3321 error = device_create_sys_dev_entry(dev);
3322 if (error)
3323 goto SysEntryError;
3324
3325 devtmpfs_create_node(dev);
3326 }
3327
Alan Sternec0676ee2008-12-05 14:10:31 -05003328 /* Notify clients of device addition. This call must come
majianpeng268863f2012-01-11 15:12:06 +00003329 * after dpm_sysfs_add() and before kobject_uevent().
Alan Sternec0676ee2008-12-05 14:10:31 -05003330 */
3331 if (dev->bus)
3332 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
3333 BUS_NOTIFY_ADD_DEVICE, dev);
3334
Cornelia Huck83b5fb4c2007-03-29 11:12:11 +02003335 kobject_uevent(&dev->kobj, KOBJ_ADD);
Saravana Kannan372a67c2019-09-04 14:11:20 -07003336
Saravana Kannane2ae9bc2019-09-04 14:11:21 -07003337 /*
3338 * Check if any of the other devices (consumers) have been waiting for
3339 * this device (supplier) to be added so that they can create a device
3340 * link to it.
3341 *
3342 * This needs to happen after device_pm_add() because device_link_add()
3343 * requires the supplier be registered before it's called.
3344 *
Saravana Kannan2cd38fd2020-05-19 20:48:21 -07003345 * But this also needs to happen before bus_probe_device() to make sure
Saravana Kannane2ae9bc2019-09-04 14:11:21 -07003346 * waiting consumers can link to it before the driver is bound to the
3347 * device and the driver sync_state callback is called for this device.
3348 */
Saravana Kannan2cd38fd2020-05-19 20:48:21 -07003349 if (dev->fwnode && !dev->fwnode->dev) {
3350 dev->fwnode->dev = dev;
Saravana Kannan5f5377e2020-05-14 22:34:58 -07003351 fw_devlink_link_device(dev);
Saravana Kannan03324502019-10-28 15:00:24 -07003352 }
Saravana Kannane2ae9bc2019-09-04 14:11:21 -07003353
Alan Stern2023c612009-07-30 15:27:18 -04003354 bus_probe_device(dev);
Saravana Kannand46f3e32021-04-01 21:03:41 -07003355
3356 /*
3357 * If all driver registration is done and a newly added device doesn't
3358 * match with any driver, don't block its consumers from probing in
3359 * case the consumer device is able to operate without this supplier.
3360 */
3361 if (dev->fwnode && fw_devlink_drv_reg_done && !dev->can_match)
3362 fw_devlink_unblock_consumers(dev);
3363
Linus Torvalds1da177e2005-04-16 15:20:36 -07003364 if (parent)
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08003365 klist_add_tail(&dev->p->knode_parent,
3366 &parent->p->klist_children);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003367
Greg Kroah-Hartman5d9fd162006-06-22 17:17:32 -07003368 if (dev->class) {
Kay Sieversca22e562011-12-14 14:29:38 -08003369 mutex_lock(&dev->class->p->mutex);
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02003370 /* tie the class to the device */
Wei Yang570d0202019-01-18 10:34:59 +08003371 klist_add_tail(&dev->p->knode_class,
Kay Sievers6b6e39a2010-11-15 23:13:18 +01003372 &dev->class->p->klist_devices);
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02003373
3374 /* notify any interfaces that the device is here */
Greg Kroah-Hartman184f1f72008-05-28 09:28:39 -07003375 list_for_each_entry(class_intf,
Kay Sieversca22e562011-12-14 14:29:38 -08003376 &dev->class->p->interfaces, node)
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02003377 if (class_intf->add_dev)
3378 class_intf->add_dev(dev, class_intf);
Kay Sieversca22e562011-12-14 14:29:38 -08003379 mutex_unlock(&dev->class->p->mutex);
Greg Kroah-Hartman5d9fd162006-06-22 17:17:32 -07003380 }
Greg Kroah-Hartmanc906a482008-05-30 10:45:12 -07003381done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003382 put_device(dev);
3383 return error;
Sergey Klyaus0cd75042014-10-08 11:31:54 +04003384 SysEntryError:
3385 if (MAJOR(dev->devt))
3386 device_remove_file(dev, &dev_attr_dev);
3387 DevAttrError:
3388 device_pm_remove(dev);
3389 dpm_sysfs_remove(dev);
Alan Stern3b98aea2008-08-07 13:06:12 -04003390 DPMError:
Rafael J. Wysocki57eee3d2008-03-12 00:59:38 +01003391 bus_remove_device(dev);
3392 BusError:
James Simmons82f0cf92007-02-21 17:44:51 +00003393 device_remove_attrs(dev);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07003394 AttrsError:
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07003395 device_remove_class_symlinks(dev);
3396 SymlinkError:
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07003397 device_remove_file(dev, &dev_attr_uevent);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07003398 attrError:
Heikki Krogerus07de0e82018-11-09 17:21:34 +03003399 device_platform_notify(dev, KOBJ_REMOVE);
3400platform_error:
Kay Sievers312c0042005-11-16 09:00:00 +01003401 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
Ming Leicebf8fd2016-07-10 19:27:36 +08003402 glue_dir = get_glue_dir(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003403 kobject_del(&dev->kobj);
3404 Error:
Ming Leicebf8fd2016-07-10 19:27:36 +08003405 cleanup_glue_dir(dev, glue_dir);
Tetsuo Handa84d0c272018-05-07 19:10:31 +09003406parent_error:
Markus Elfring5f0163a2015-02-05 11:48:26 +01003407 put_device(parent);
Kay Sievers5c8563d2009-05-28 14:24:07 -07003408name_error:
3409 kfree(dev->p);
3410 dev->p = NULL;
Greg Kroah-Hartmanc906a482008-05-30 10:45:12 -07003411 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003412}
David Graham White86df2682013-07-21 20:41:14 -04003413EXPORT_SYMBOL_GPL(device_add);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003414
Linus Torvalds1da177e2005-04-16 15:20:36 -07003415/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08003416 * device_register - register a device with the system.
3417 * @dev: pointer to the device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07003418 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08003419 * This happens in two clean steps - initialize the device
3420 * and add it to the system. The two steps can be called
3421 * separately, but this is the easiest and most common.
3422 * I.e. you should only call the two helpers separately if
3423 * have a clearly defined need to use and refcount the device
3424 * before it is added to the hierarchy.
Cornelia Huck57394112008-09-03 18:26:40 +02003425 *
Alan Sternb10d5ef2012-01-17 11:39:00 -05003426 * For more information, see the kerneldoc for device_initialize()
3427 * and device_add().
3428 *
Cornelia Huck57394112008-09-03 18:26:40 +02003429 * NOTE: _Never_ directly free @dev after calling this function, even
3430 * if it returned an error! Always use put_device() to give up the
3431 * reference initialized in this function instead.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003432 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003433int device_register(struct device *dev)
3434{
3435 device_initialize(dev);
3436 return device_add(dev);
3437}
David Graham White86df2682013-07-21 20:41:14 -04003438EXPORT_SYMBOL_GPL(device_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003439
Linus Torvalds1da177e2005-04-16 15:20:36 -07003440/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08003441 * get_device - increment reference count for device.
3442 * @dev: device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003443 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08003444 * This simply forwards the call to kobject_get(), though
3445 * we do take care to provide for the case that we get a NULL
3446 * pointer passed in.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003447 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08003448struct device *get_device(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003449{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +02003450 return dev ? kobj_to_dev(kobject_get(&dev->kobj)) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003451}
David Graham White86df2682013-07-21 20:41:14 -04003452EXPORT_SYMBOL_GPL(get_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003453
Linus Torvalds1da177e2005-04-16 15:20:36 -07003454/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08003455 * put_device - decrement reference count.
3456 * @dev: device in question.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003457 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08003458void put_device(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003459{
Kay Sieversedfaa7c2007-05-21 22:08:01 +02003460 /* might_sleep(); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003461 if (dev)
3462 kobject_put(&dev->kobj);
3463}
David Graham White86df2682013-07-21 20:41:14 -04003464EXPORT_SYMBOL_GPL(put_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003465
Dan Williams00289cd2019-07-17 18:07:53 -07003466bool kill_device(struct device *dev)
3467{
3468 /*
3469 * Require the device lock and set the "dead" flag to guarantee that
3470 * the update behavior is consistent with the other bitfields near
3471 * it and that we cannot have an asynchronous probe routine trying
3472 * to run while we are tearing out the bus/class/sysfs from
3473 * underneath the device.
3474 */
Julian Wiedmann8c60a142021-05-12 16:10:54 +02003475 device_lock_assert(dev);
Dan Williams00289cd2019-07-17 18:07:53 -07003476
3477 if (dev->p->dead)
3478 return false;
3479 dev->p->dead = true;
3480 return true;
3481}
3482EXPORT_SYMBOL_GPL(kill_device);
3483
Linus Torvalds1da177e2005-04-16 15:20:36 -07003484/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08003485 * device_del - delete device from system.
3486 * @dev: device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003487 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08003488 * This is the first part of the device unregistration
3489 * sequence. This removes the device from the lists we control
3490 * from here, has it removed from the other driver model
3491 * subsystems it was added to in device_add(), and removes it
3492 * from the kobject hierarchy.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003493 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08003494 * NOTE: this should be called manually _iff_ device_add() was
3495 * also called manually.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003496 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08003497void device_del(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003498{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08003499 struct device *parent = dev->parent;
Ming Leicebf8fd2016-07-10 19:27:36 +08003500 struct kobject *glue_dir = NULL;
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02003501 struct class_interface *class_intf;
Oliver Neukumb8530012020-09-16 21:15:44 +02003502 unsigned int noio_flag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003503
Alexander Duyck3451a492019-01-22 10:39:10 -08003504 device_lock(dev);
Dan Williams00289cd2019-07-17 18:07:53 -07003505 kill_device(dev);
Alexander Duyck3451a492019-01-22 10:39:10 -08003506 device_unlock(dev);
3507
Saravana Kannan372a67c2019-09-04 14:11:20 -07003508 if (dev->fwnode && dev->fwnode->dev == dev)
3509 dev->fwnode->dev = NULL;
3510
Alan Sternec0676ee2008-12-05 14:10:31 -05003511 /* Notify clients of device removal. This call must come
3512 * before dpm_sysfs_remove().
3513 */
Oliver Neukumb8530012020-09-16 21:15:44 +02003514 noio_flag = memalloc_noio_save();
Alan Sternec0676ee2008-12-05 14:10:31 -05003515 if (dev->bus)
3516 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
3517 BUS_NOTIFY_DEL_DEVICE, dev);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01003518
Alan Stern3b98aea2008-08-07 13:06:12 -04003519 dpm_sysfs_remove(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003520 if (parent)
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08003521 klist_del(&dev->p->knode_parent);
Dan Williamse105b8b2008-04-21 10:51:07 -07003522 if (MAJOR(dev->devt)) {
Kay Sievers2b2af542009-04-30 15:23:42 +02003523 devtmpfs_delete_node(dev);
Dan Williamse105b8b2008-04-21 10:51:07 -07003524 device_remove_sys_dev_entry(dev);
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07003525 device_remove_file(dev, &dev_attr_dev);
Dan Williamse105b8b2008-04-21 10:51:07 -07003526 }
Kay Sieversb9d9c822006-06-15 15:31:56 +02003527 if (dev->class) {
Kay Sieversda231fd2007-11-21 17:29:15 +01003528 device_remove_class_symlinks(dev);
Kay Sievers99ef3ef2006-09-14 11:23:28 +02003529
Kay Sieversca22e562011-12-14 14:29:38 -08003530 mutex_lock(&dev->class->p->mutex);
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02003531 /* notify any interfaces that the device is now gone */
Greg Kroah-Hartman184f1f72008-05-28 09:28:39 -07003532 list_for_each_entry(class_intf,
Kay Sieversca22e562011-12-14 14:29:38 -08003533 &dev->class->p->interfaces, node)
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02003534 if (class_intf->remove_dev)
3535 class_intf->remove_dev(dev, class_intf);
3536 /* remove the device from the class list */
Wei Yang570d0202019-01-18 10:34:59 +08003537 klist_del(&dev->p->knode_class);
Kay Sieversca22e562011-12-14 14:29:38 -08003538 mutex_unlock(&dev->class->p->mutex);
Kay Sieversb9d9c822006-06-15 15:31:56 +02003539 }
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07003540 device_remove_file(dev, &dev_attr_uevent);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07003541 device_remove_attrs(dev);
Benjamin Herrenschmidt28953532006-11-08 19:46:14 -08003542 bus_remove_device(dev);
LongX Zhang4b6d1f122012-10-25 00:21:28 +02003543 device_pm_remove(dev);
Grant Likelyd1c34142012-03-05 08:47:41 -07003544 driver_deferred_probe_del(dev);
Heikki Krogerus07de0e82018-11-09 17:21:34 +03003545 device_platform_notify(dev, KOBJ_REMOVE);
Lukas Wunner478573c2016-07-28 02:25:41 +02003546 device_remove_properties(dev);
Jeffy Chen2ec16152017-10-20 20:01:01 +08003547 device_links_purge(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003548
Joerg Roedel599bad32014-09-30 13:02:02 +02003549 if (dev->bus)
3550 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
3551 BUS_NOTIFY_REMOVED_DEVICE, dev);
Kay Sievers312c0042005-11-16 09:00:00 +01003552 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
Ming Leicebf8fd2016-07-10 19:27:36 +08003553 glue_dir = get_glue_dir(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003554 kobject_del(&dev->kobj);
Ming Leicebf8fd2016-07-10 19:27:36 +08003555 cleanup_glue_dir(dev, glue_dir);
Oliver Neukumb8530012020-09-16 21:15:44 +02003556 memalloc_noio_restore(noio_flag);
Kay Sieversda231fd2007-11-21 17:29:15 +01003557 put_device(parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003558}
David Graham White86df2682013-07-21 20:41:14 -04003559EXPORT_SYMBOL_GPL(device_del);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003560
3561/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08003562 * device_unregister - unregister device from system.
3563 * @dev: device going away.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003564 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08003565 * We do this in two parts, like we do device_register(). First,
3566 * we remove it from all the subsystems with device_del(), then
3567 * we decrement the reference count via put_device(). If that
3568 * is the final reference count, the device will be cleaned up
3569 * via device_release() above. Otherwise, the structure will
3570 * stick around until the final reference to the device is dropped.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003571 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08003572void device_unregister(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003573{
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01003574 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003575 device_del(dev);
3576 put_device(dev);
3577}
David Graham White86df2682013-07-21 20:41:14 -04003578EXPORT_SYMBOL_GPL(device_unregister);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003579
Andy Shevchenko3d060ae2015-07-27 18:04:00 +03003580static struct device *prev_device(struct klist_iter *i)
3581{
3582 struct klist_node *n = klist_prev(i);
3583 struct device *dev = NULL;
3584 struct device_private *p;
3585
3586 if (n) {
3587 p = to_device_private_parent(n);
3588 dev = p->device;
3589 }
3590 return dev;
3591}
3592
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08003593static struct device *next_device(struct klist_iter *i)
mochel@digitalimplant.org36239572005-03-24 19:08:30 -08003594{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08003595 struct klist_node *n = klist_next(i);
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08003596 struct device *dev = NULL;
3597 struct device_private *p;
3598
3599 if (n) {
3600 p = to_device_private_parent(n);
3601 dev = p->device;
3602 }
3603 return dev;
mochel@digitalimplant.org36239572005-03-24 19:08:30 -08003604}
3605
Linus Torvalds1da177e2005-04-16 15:20:36 -07003606/**
Kay Sieverse454cea2009-09-18 23:01:12 +02003607 * device_get_devnode - path of device node file
Kay Sievers6fcf53a2009-04-30 15:23:42 +02003608 * @dev: device
Kay Sieverse454cea2009-09-18 23:01:12 +02003609 * @mode: returned file access mode
Kay Sievers3c2670e2013-04-06 09:56:00 -07003610 * @uid: returned file owner
3611 * @gid: returned file group
Kay Sievers6fcf53a2009-04-30 15:23:42 +02003612 * @tmp: possibly allocated string
3613 *
3614 * Return the relative path of a possible device node.
3615 * Non-default names may need to allocate a memory to compose
3616 * a name. This memory is returned in tmp and needs to be
3617 * freed by the caller.
3618 */
Kay Sieverse454cea2009-09-18 23:01:12 +02003619const char *device_get_devnode(struct device *dev,
Greg Kroah-Hartman4e4098a2013-04-11 11:43:29 -07003620 umode_t *mode, kuid_t *uid, kgid_t *gid,
Kay Sievers3c2670e2013-04-06 09:56:00 -07003621 const char **tmp)
Kay Sievers6fcf53a2009-04-30 15:23:42 +02003622{
3623 char *s;
3624
3625 *tmp = NULL;
3626
3627 /* the device type may provide a specific name */
Kay Sieverse454cea2009-09-18 23:01:12 +02003628 if (dev->type && dev->type->devnode)
Kay Sievers3c2670e2013-04-06 09:56:00 -07003629 *tmp = dev->type->devnode(dev, mode, uid, gid);
Kay Sievers6fcf53a2009-04-30 15:23:42 +02003630 if (*tmp)
3631 return *tmp;
3632
3633 /* the class may provide a specific name */
Kay Sieverse454cea2009-09-18 23:01:12 +02003634 if (dev->class && dev->class->devnode)
3635 *tmp = dev->class->devnode(dev, mode);
Kay Sievers6fcf53a2009-04-30 15:23:42 +02003636 if (*tmp)
3637 return *tmp;
3638
3639 /* return name without allocation, tmp == NULL */
3640 if (strchr(dev_name(dev), '!') == NULL)
3641 return dev_name(dev);
3642
3643 /* replace '!' in the name with '/' */
Rasmus Villemoesa29fd612015-06-25 15:02:33 -07003644 s = kstrdup(dev_name(dev), GFP_KERNEL);
3645 if (!s)
Kay Sievers6fcf53a2009-04-30 15:23:42 +02003646 return NULL;
Rasmus Villemoesa29fd612015-06-25 15:02:33 -07003647 strreplace(s, '!', '/');
3648 return *tmp = s;
Kay Sievers6fcf53a2009-04-30 15:23:42 +02003649}
3650
3651/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08003652 * device_for_each_child - device child iterator.
3653 * @parent: parent struct device.
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08003654 * @fn: function to be called for each device.
Robert P. J. Dayf8878dc2013-06-01 20:17:34 -04003655 * @data: data for the callback.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003656 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08003657 * Iterate over @parent's child devices, and call @fn for each,
3658 * passing it @data.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003659 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08003660 * We check the return of @fn each time. If it returns anything
3661 * other than 0, we break out and return that value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003662 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08003663int device_for_each_child(struct device *parent, void *data,
3664 int (*fn)(struct device *dev, void *data))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003665{
mochel@digitalimplant.org36239572005-03-24 19:08:30 -08003666 struct klist_iter i;
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08003667 struct device *child;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003668 int error = 0;
3669
Greg Kroah-Hartman014c90db2009-04-15 16:00:12 -07003670 if (!parent->p)
3671 return 0;
3672
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08003673 klist_iter_init(&parent->p->klist_children, &i);
Gimcuan Hui93ead7c2017-11-11 05:52:54 +00003674 while (!error && (child = next_device(&i)))
mochel@digitalimplant.org36239572005-03-24 19:08:30 -08003675 error = fn(child, data);
3676 klist_iter_exit(&i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003677 return error;
3678}
David Graham White86df2682013-07-21 20:41:14 -04003679EXPORT_SYMBOL_GPL(device_for_each_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003680
Cornelia Huck5ab69982006-11-16 15:42:07 +01003681/**
Andy Shevchenko3d060ae2015-07-27 18:04:00 +03003682 * device_for_each_child_reverse - device child iterator in reversed order.
3683 * @parent: parent struct device.
3684 * @fn: function to be called for each device.
3685 * @data: data for the callback.
3686 *
3687 * Iterate over @parent's child devices, and call @fn for each,
3688 * passing it @data.
3689 *
3690 * We check the return of @fn each time. If it returns anything
3691 * other than 0, we break out and return that value.
3692 */
3693int device_for_each_child_reverse(struct device *parent, void *data,
3694 int (*fn)(struct device *dev, void *data))
3695{
3696 struct klist_iter i;
3697 struct device *child;
3698 int error = 0;
3699
3700 if (!parent->p)
3701 return 0;
3702
3703 klist_iter_init(&parent->p->klist_children, &i);
3704 while ((child = prev_device(&i)) && !error)
3705 error = fn(child, data);
3706 klist_iter_exit(&i);
3707 return error;
3708}
3709EXPORT_SYMBOL_GPL(device_for_each_child_reverse);
3710
3711/**
Cornelia Huck5ab69982006-11-16 15:42:07 +01003712 * device_find_child - device iterator for locating a particular device.
3713 * @parent: parent struct device
Cornelia Huck5ab69982006-11-16 15:42:07 +01003714 * @match: Callback function to check device
Robert P. J. Dayf8878dc2013-06-01 20:17:34 -04003715 * @data: Data to pass to match function
Cornelia Huck5ab69982006-11-16 15:42:07 +01003716 *
3717 * This is similar to the device_for_each_child() function above, but it
3718 * returns a reference to a device that is 'found' for later use, as
3719 * determined by the @match callback.
3720 *
3721 * The callback should return 0 if the device doesn't match and non-zero
3722 * if it does. If the callback returns non-zero and a reference to the
3723 * current device can be obtained, this function will return to the caller
3724 * and not iterate over any more devices.
Federico Vagaa4e24002013-04-15 11:18:11 +02003725 *
3726 * NOTE: you will need to drop the reference with put_device() after use.
Cornelia Huck5ab69982006-11-16 15:42:07 +01003727 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08003728struct device *device_find_child(struct device *parent, void *data,
3729 int (*match)(struct device *dev, void *data))
Cornelia Huck5ab69982006-11-16 15:42:07 +01003730{
3731 struct klist_iter i;
3732 struct device *child;
3733
3734 if (!parent)
3735 return NULL;
3736
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08003737 klist_iter_init(&parent->p->klist_children, &i);
Cornelia Huck5ab69982006-11-16 15:42:07 +01003738 while ((child = next_device(&i)))
3739 if (match(child, data) && get_device(child))
3740 break;
3741 klist_iter_exit(&i);
3742 return child;
3743}
David Graham White86df2682013-07-21 20:41:14 -04003744EXPORT_SYMBOL_GPL(device_find_child);
Cornelia Huck5ab69982006-11-16 15:42:07 +01003745
Heikki Krogerusdad9bb02019-05-31 17:15:37 +03003746/**
3747 * device_find_child_by_name - device iterator for locating a child device.
3748 * @parent: parent struct device
3749 * @name: name of the child device
3750 *
3751 * This is similar to the device_find_child() function above, but it
3752 * returns a reference to a device that has the name @name.
3753 *
3754 * NOTE: you will need to drop the reference with put_device() after use.
3755 */
3756struct device *device_find_child_by_name(struct device *parent,
3757 const char *name)
3758{
3759 struct klist_iter i;
3760 struct device *child;
3761
3762 if (!parent)
3763 return NULL;
3764
3765 klist_iter_init(&parent->p->klist_children, &i);
3766 while ((child = next_device(&i)))
Dan Williamsc77f5202020-10-13 16:50:18 -07003767 if (sysfs_streq(dev_name(child), name) && get_device(child))
Heikki Krogerusdad9bb02019-05-31 17:15:37 +03003768 break;
3769 klist_iter_exit(&i);
3770 return child;
3771}
3772EXPORT_SYMBOL_GPL(device_find_child_by_name);
3773
Linus Torvalds1da177e2005-04-16 15:20:36 -07003774int __init devices_init(void)
3775{
Greg Kroah-Hartman881c6cfd2007-11-01 09:29:06 -06003776 devices_kset = kset_create_and_add("devices", &device_uevent_ops, NULL);
3777 if (!devices_kset)
3778 return -ENOMEM;
Dan Williamse105b8b2008-04-21 10:51:07 -07003779 dev_kobj = kobject_create_and_add("dev", NULL);
3780 if (!dev_kobj)
3781 goto dev_kobj_err;
3782 sysfs_dev_block_kobj = kobject_create_and_add("block", dev_kobj);
3783 if (!sysfs_dev_block_kobj)
3784 goto block_kobj_err;
3785 sysfs_dev_char_kobj = kobject_create_and_add("char", dev_kobj);
3786 if (!sysfs_dev_char_kobj)
3787 goto char_kobj_err;
3788
Greg Kroah-Hartman881c6cfd2007-11-01 09:29:06 -06003789 return 0;
Dan Williamse105b8b2008-04-21 10:51:07 -07003790
3791 char_kobj_err:
3792 kobject_put(sysfs_dev_block_kobj);
3793 block_kobj_err:
3794 kobject_put(dev_kobj);
3795 dev_kobj_err:
3796 kset_unregister(devices_kset);
3797 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003798}
3799
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02003800static int device_check_offline(struct device *dev, void *not_used)
3801{
3802 int ret;
3803
3804 ret = device_for_each_child(dev, NULL, device_check_offline);
3805 if (ret)
3806 return ret;
3807
3808 return device_supports_offline(dev) && !dev->offline ? -EBUSY : 0;
3809}
3810
3811/**
3812 * device_offline - Prepare the device for hot-removal.
3813 * @dev: Device to be put offline.
3814 *
3815 * Execute the device bus type's .offline() callback, if present, to prepare
3816 * the device for a subsequent hot-removal. If that succeeds, the device must
3817 * not be used until either it is removed or its bus type's .online() callback
3818 * is executed.
3819 *
3820 * Call under device_hotplug_lock.
3821 */
3822int device_offline(struct device *dev)
3823{
3824 int ret;
3825
3826 if (dev->offline_disabled)
3827 return -EPERM;
3828
3829 ret = device_for_each_child(dev, NULL, device_check_offline);
3830 if (ret)
3831 return ret;
3832
3833 device_lock(dev);
3834 if (device_supports_offline(dev)) {
3835 if (dev->offline) {
3836 ret = 1;
3837 } else {
3838 ret = dev->bus->offline(dev);
3839 if (!ret) {
3840 kobject_uevent(&dev->kobj, KOBJ_OFFLINE);
3841 dev->offline = true;
3842 }
3843 }
3844 }
3845 device_unlock(dev);
3846
3847 return ret;
3848}
3849
3850/**
3851 * device_online - Put the device back online after successful device_offline().
3852 * @dev: Device to be put back online.
3853 *
3854 * If device_offline() has been successfully executed for @dev, but the device
3855 * has not been removed subsequently, execute its bus type's .online() callback
3856 * to indicate that the device can be used again.
3857 *
3858 * Call under device_hotplug_lock.
3859 */
3860int device_online(struct device *dev)
3861{
3862 int ret = 0;
3863
3864 device_lock(dev);
3865 if (device_supports_offline(dev)) {
3866 if (dev->offline) {
3867 ret = dev->bus->online(dev);
3868 if (!ret) {
3869 kobject_uevent(&dev->kobj, KOBJ_ONLINE);
3870 dev->offline = false;
3871 }
3872 } else {
3873 ret = 1;
3874 }
3875 }
3876 device_unlock(dev);
3877
3878 return ret;
3879}
3880
Karthigan Srinivasan7f100d12011-04-18 16:16:52 -05003881struct root_device {
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00003882 struct device dev;
3883 struct module *owner;
3884};
3885
Josh Triplett93058422012-11-18 21:27:55 -08003886static inline struct root_device *to_root_device(struct device *d)
Ferenc Wagner481e2072011-01-07 15:17:47 +01003887{
3888 return container_of(d, struct root_device, dev);
3889}
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00003890
3891static void root_device_release(struct device *dev)
3892{
3893 kfree(to_root_device(dev));
3894}
3895
3896/**
3897 * __root_device_register - allocate and register a root device
3898 * @name: root device name
3899 * @owner: owner module of the root device, usually THIS_MODULE
3900 *
3901 * This function allocates a root device and registers it
3902 * using device_register(). In order to free the returned
3903 * device, use root_device_unregister().
3904 *
3905 * Root devices are dummy devices which allow other devices
3906 * to be grouped under /sys/devices. Use this function to
3907 * allocate a root device and then use it as the parent of
3908 * any device which should appear under /sys/devices/{name}
3909 *
3910 * The /sys/devices/{name} directory will also contain a
3911 * 'module' symlink which points to the @owner directory
3912 * in sysfs.
3913 *
Jani Nikulaf0eae0e2010-03-11 18:11:45 +02003914 * Returns &struct device pointer on success, or ERR_PTR() on error.
3915 *
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00003916 * Note: You probably want to use root_device_register().
3917 */
3918struct device *__root_device_register(const char *name, struct module *owner)
3919{
3920 struct root_device *root;
3921 int err = -ENOMEM;
3922
3923 root = kzalloc(sizeof(struct root_device), GFP_KERNEL);
3924 if (!root)
3925 return ERR_PTR(err);
3926
Greg Kroah-Hartmanacc0e902009-06-02 15:39:55 -07003927 err = dev_set_name(&root->dev, "%s", name);
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00003928 if (err) {
3929 kfree(root);
3930 return ERR_PTR(err);
3931 }
3932
3933 root->dev.release = root_device_release;
3934
3935 err = device_register(&root->dev);
3936 if (err) {
3937 put_device(&root->dev);
3938 return ERR_PTR(err);
3939 }
3940
Christoph Egger1d9e8822010-05-17 16:57:58 +02003941#ifdef CONFIG_MODULES /* gotta find a "cleaner" way to do this */
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00003942 if (owner) {
3943 struct module_kobject *mk = &owner->mkobj;
3944
3945 err = sysfs_create_link(&root->dev.kobj, &mk->kobj, "module");
3946 if (err) {
3947 device_unregister(&root->dev);
3948 return ERR_PTR(err);
3949 }
3950 root->owner = owner;
3951 }
3952#endif
3953
3954 return &root->dev;
3955}
3956EXPORT_SYMBOL_GPL(__root_device_register);
3957
3958/**
3959 * root_device_unregister - unregister and free a root device
Randy Dunlap7cbcf222009-01-20 16:29:13 -08003960 * @dev: device going away
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00003961 *
3962 * This function unregisters and cleans up a device that was created by
3963 * root_device_register().
3964 */
3965void root_device_unregister(struct device *dev)
3966{
3967 struct root_device *root = to_root_device(dev);
3968
3969 if (root->owner)
3970 sysfs_remove_link(&root->dev.kobj, "module");
3971
3972 device_unregister(dev);
3973}
3974EXPORT_SYMBOL_GPL(root_device_unregister);
3975
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07003976
3977static void device_create_release(struct device *dev)
3978{
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01003979 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07003980 kfree(dev);
3981}
3982
Mathieu Malaterre6a8b55d2018-05-05 21:57:41 +02003983static __printf(6, 0) struct device *
Guenter Roeck39ef3112013-07-14 16:05:57 -07003984device_create_groups_vargs(struct class *class, struct device *parent,
3985 dev_t devt, void *drvdata,
3986 const struct attribute_group **groups,
3987 const char *fmt, va_list args)
3988{
3989 struct device *dev = NULL;
3990 int retval = -ENODEV;
3991
3992 if (class == NULL || IS_ERR(class))
3993 goto error;
3994
3995 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
3996 if (!dev) {
3997 retval = -ENOMEM;
3998 goto error;
3999 }
4000
David Herrmannbbc780f2013-11-21 20:15:48 +01004001 device_initialize(dev);
Guenter Roeck39ef3112013-07-14 16:05:57 -07004002 dev->devt = devt;
4003 dev->class = class;
4004 dev->parent = parent;
4005 dev->groups = groups;
4006 dev->release = device_create_release;
4007 dev_set_drvdata(dev, drvdata);
4008
4009 retval = kobject_set_name_vargs(&dev->kobj, fmt, args);
4010 if (retval)
4011 goto error;
4012
David Herrmannbbc780f2013-11-21 20:15:48 +01004013 retval = device_add(dev);
Guenter Roeck39ef3112013-07-14 16:05:57 -07004014 if (retval)
4015 goto error;
4016
4017 return dev;
4018
4019error:
4020 put_device(dev);
4021 return ERR_PTR(retval);
4022}
4023
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07004024/**
Greg Kroah-Hartman4e106732008-07-21 20:03:34 -07004025 * device_create - creates a device and registers it with sysfs
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07004026 * @class: pointer to the struct class that this device should be registered to
4027 * @parent: pointer to the parent struct device of this new device, if any
4028 * @devt: the dev_t for the char device to be added
4029 * @drvdata: the data to be added to the device for callbacks
4030 * @fmt: string for the device's name
4031 *
4032 * This function can be used by char device classes. A struct device
4033 * will be created in sysfs, registered to the specified class.
4034 *
4035 * A "dev" file will be created, showing the dev_t for the device, if
4036 * the dev_t is not 0,0.
4037 * If a pointer to a parent struct device is passed in, the newly created
4038 * struct device will be a child of that device in sysfs.
4039 * The pointer to the struct device will be returned from the call.
4040 * Any further sysfs files that might be required can be created using this
4041 * pointer.
4042 *
Jani Nikulaf0eae0e2010-03-11 18:11:45 +02004043 * Returns &struct device pointer on success, or ERR_PTR() on error.
4044 *
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07004045 * Note: the struct class passed to this function must have previously
4046 * been created with a call to class_create().
4047 */
Greg Kroah-Hartman4e106732008-07-21 20:03:34 -07004048struct device *device_create(struct class *class, struct device *parent,
4049 dev_t devt, void *drvdata, const char *fmt, ...)
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07004050{
4051 va_list vargs;
4052 struct device *dev;
4053
4054 va_start(vargs, fmt);
Christoph Hellwig4c747462020-05-04 14:47:57 +02004055 dev = device_create_groups_vargs(class, parent, devt, drvdata, NULL,
4056 fmt, vargs);
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07004057 va_end(vargs);
4058 return dev;
4059}
Greg Kroah-Hartman4e106732008-07-21 20:03:34 -07004060EXPORT_SYMBOL_GPL(device_create);
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07004061
Guenter Roeck39ef3112013-07-14 16:05:57 -07004062/**
4063 * device_create_with_groups - creates a device and registers it with sysfs
4064 * @class: pointer to the struct class that this device should be registered to
4065 * @parent: pointer to the parent struct device of this new device, if any
4066 * @devt: the dev_t for the char device to be added
4067 * @drvdata: the data to be added to the device for callbacks
4068 * @groups: NULL-terminated list of attribute groups to be created
4069 * @fmt: string for the device's name
4070 *
4071 * This function can be used by char device classes. A struct device
4072 * will be created in sysfs, registered to the specified class.
4073 * Additional attributes specified in the groups parameter will also
4074 * be created automatically.
4075 *
4076 * A "dev" file will be created, showing the dev_t for the device, if
4077 * the dev_t is not 0,0.
4078 * If a pointer to a parent struct device is passed in, the newly created
4079 * struct device will be a child of that device in sysfs.
4080 * The pointer to the struct device will be returned from the call.
4081 * Any further sysfs files that might be required can be created using this
4082 * pointer.
4083 *
4084 * Returns &struct device pointer on success, or ERR_PTR() on error.
4085 *
4086 * Note: the struct class passed to this function must have previously
4087 * been created with a call to class_create().
4088 */
4089struct device *device_create_with_groups(struct class *class,
4090 struct device *parent, dev_t devt,
4091 void *drvdata,
4092 const struct attribute_group **groups,
4093 const char *fmt, ...)
4094{
4095 va_list vargs;
4096 struct device *dev;
4097
4098 va_start(vargs, fmt);
4099 dev = device_create_groups_vargs(class, parent, devt, drvdata, groups,
4100 fmt, vargs);
4101 va_end(vargs);
4102 return dev;
4103}
4104EXPORT_SYMBOL_GPL(device_create_with_groups);
4105
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01004106/**
4107 * device_destroy - removes a device that was created with device_create()
4108 * @class: pointer to the struct class that this device was registered with
4109 * @devt: the dev_t of the device that was previously registered
4110 *
4111 * This call unregisters and cleans up a device that was created with a
4112 * call to device_create().
4113 */
4114void device_destroy(struct class *class, dev_t devt)
4115{
4116 struct device *dev;
4117
Suzuki K Poulose4495dfd2019-07-23 23:18:35 +01004118 dev = class_find_device_by_devt(class, devt);
Dave Youngcd354492008-01-28 16:56:11 +08004119 if (dev) {
4120 put_device(dev);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07004121 device_unregister(dev);
Dave Youngcd354492008-01-28 16:56:11 +08004122 }
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07004123}
4124EXPORT_SYMBOL_GPL(device_destroy);
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07004125
4126/**
4127 * device_rename - renames a device
4128 * @dev: the pointer to the struct device to be renamed
4129 * @new_name: the new name of the device
Eric W. Biederman030c1d22008-05-08 14:41:00 -07004130 *
4131 * It is the responsibility of the caller to provide mutual
4132 * exclusion between two different calls of device_rename
4133 * on the same device to ensure that new_name is valid and
4134 * won't conflict with other devices.
Michael Ellermanc6c0ac62010-11-25 09:44:07 +11004135 *
Timur Tabia5462512010-12-13 14:08:52 -06004136 * Note: Don't call this function. Currently, the networking layer calls this
4137 * function, but that will change. The following text from Kay Sievers offers
4138 * some insight:
4139 *
4140 * Renaming devices is racy at many levels, symlinks and other stuff are not
4141 * replaced atomically, and you get a "move" uevent, but it's not easy to
4142 * connect the event to the old and new device. Device nodes are not renamed at
4143 * all, there isn't even support for that in the kernel now.
4144 *
4145 * In the meantime, during renaming, your target name might be taken by another
4146 * driver, creating conflicts. Or the old name is taken directly after you
4147 * renamed it -- then you get events for the same DEVPATH, before you even see
4148 * the "move" event. It's just a mess, and nothing new should ever rely on
4149 * kernel device renaming. Besides that, it's not even implemented now for
4150 * other things than (driver-core wise very simple) network devices.
4151 *
4152 * We are currently about to change network renaming in udev to completely
4153 * disallow renaming of devices in the same namespace as the kernel uses,
4154 * because we can't solve the problems properly, that arise with swapping names
4155 * of multiple interfaces without races. Means, renaming of eth[0-9]* will only
4156 * be allowed to some other name than eth[0-9]*, for the aforementioned
4157 * reasons.
4158 *
4159 * Make up a "real" name in the driver before you register anything, or add
4160 * some other attributes for userspace to find the device, or use udev to add
4161 * symlinks -- but never rename kernel devices later, it's a complete mess. We
4162 * don't even want to get into that and try to implement the missing pieces in
4163 * the core. We really have other pieces to fix in the driver core mess. :)
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07004164 */
Johannes Berg6937e8f2010-08-05 17:38:18 +02004165int device_rename(struct device *dev, const char *new_name)
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07004166{
Tejun Heo4b30ee52013-09-11 22:29:06 -04004167 struct kobject *kobj = &dev->kobj;
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07004168 char *old_device_name = NULL;
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07004169 int error;
4170
4171 dev = get_device(dev);
4172 if (!dev)
4173 return -EINVAL;
4174
ethan.zhao69df7532013-10-13 22:12:35 +08004175 dev_dbg(dev, "renaming to %s\n", new_name);
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07004176
Kay Sievers1fa5ae82009-01-25 15:17:37 +01004177 old_device_name = kstrdup(dev_name(dev), GFP_KERNEL);
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07004178 if (!old_device_name) {
4179 error = -ENOMEM;
4180 goto out;
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07004181 }
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07004182
Eric W. Biedermanf349cf32010-03-30 11:31:29 -07004183 if (dev->class) {
Tejun Heo4b30ee52013-09-11 22:29:06 -04004184 error = sysfs_rename_link_ns(&dev->class->p->subsys.kobj,
4185 kobj, old_device_name,
4186 new_name, kobject_namespace(kobj));
Eric W. Biedermanf349cf32010-03-30 11:31:29 -07004187 if (error)
4188 goto out;
4189 }
Kay Sievers39aba962010-09-04 22:33:14 -07004190
Tejun Heo4b30ee52013-09-11 22:29:06 -04004191 error = kobject_rename(kobj, new_name);
Kay Sievers1fa5ae82009-01-25 15:17:37 +01004192 if (error)
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07004193 goto out;
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07004194
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07004195out:
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07004196 put_device(dev);
4197
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07004198 kfree(old_device_name);
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07004199
4200 return error;
4201}
Johannes Berga2807db2007-02-28 12:38:31 +01004202EXPORT_SYMBOL_GPL(device_rename);
Cornelia Huck8a824722006-11-20 17:07:51 +01004203
4204static int device_move_class_links(struct device *dev,
4205 struct device *old_parent,
4206 struct device *new_parent)
4207{
Greg Kroah-Hartmanf7f34612007-03-06 12:55:53 -08004208 int error = 0;
Cornelia Huck8a824722006-11-20 17:07:51 +01004209
Greg Kroah-Hartmanf7f34612007-03-06 12:55:53 -08004210 if (old_parent)
4211 sysfs_remove_link(&dev->kobj, "device");
4212 if (new_parent)
4213 error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
4214 "device");
4215 return error;
Cornelia Huck8a824722006-11-20 17:07:51 +01004216}
4217
4218/**
4219 * device_move - moves a device to a new parent
4220 * @dev: the pointer to the struct device to be moved
Wolfram Sang13509862018-05-06 13:23:47 +02004221 * @new_parent: the new parent of the device (can be NULL)
Cornelia Huckffa6a702009-03-04 12:44:00 +01004222 * @dpm_order: how to reorder the dpm_list
Cornelia Huck8a824722006-11-20 17:07:51 +01004223 */
Cornelia Huckffa6a702009-03-04 12:44:00 +01004224int device_move(struct device *dev, struct device *new_parent,
4225 enum dpm_order dpm_order)
Cornelia Huck8a824722006-11-20 17:07:51 +01004226{
4227 int error;
4228 struct device *old_parent;
Cornelia Huckc744aeae2007-01-08 20:16:44 +01004229 struct kobject *new_parent_kobj;
Cornelia Huck8a824722006-11-20 17:07:51 +01004230
4231 dev = get_device(dev);
4232 if (!dev)
4233 return -EINVAL;
4234
Cornelia Huckffa6a702009-03-04 12:44:00 +01004235 device_pm_lock();
Cornelia Huck8a824722006-11-20 17:07:51 +01004236 new_parent = get_device(new_parent);
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08004237 new_parent_kobj = get_device_parent(dev, new_parent);
Tetsuo Handa84d0c272018-05-07 19:10:31 +09004238 if (IS_ERR(new_parent_kobj)) {
4239 error = PTR_ERR(new_parent_kobj);
4240 put_device(new_parent);
4241 goto out;
4242 }
Cornelia Huck63b69712008-01-21 16:09:44 +01004243
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01004244 pr_debug("device: '%s': %s: moving to '%s'\n", dev_name(dev),
4245 __func__, new_parent ? dev_name(new_parent) : "<NULL>");
Cornelia Huckc744aeae2007-01-08 20:16:44 +01004246 error = kobject_move(&dev->kobj, new_parent_kobj);
Cornelia Huck8a824722006-11-20 17:07:51 +01004247 if (error) {
Cornelia Huck63b69712008-01-21 16:09:44 +01004248 cleanup_glue_dir(dev, new_parent_kobj);
Cornelia Huck8a824722006-11-20 17:07:51 +01004249 put_device(new_parent);
4250 goto out;
4251 }
4252 old_parent = dev->parent;
4253 dev->parent = new_parent;
4254 if (old_parent)
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08004255 klist_remove(&dev->p->knode_parent);
Yinghai Lu0d358f22008-02-19 03:20:41 -08004256 if (new_parent) {
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08004257 klist_add_tail(&dev->p->knode_parent,
4258 &new_parent->p->klist_children);
Yinghai Lu0d358f22008-02-19 03:20:41 -08004259 set_dev_node(dev, dev_to_node(new_parent));
4260 }
4261
Rabin Vincentbdd40342012-04-23 09:16:36 +02004262 if (dev->class) {
4263 error = device_move_class_links(dev, old_parent, new_parent);
4264 if (error) {
4265 /* We ignore errors on cleanup since we're hosed anyway... */
4266 device_move_class_links(dev, new_parent, old_parent);
4267 if (!kobject_move(&dev->kobj, &old_parent->kobj)) {
4268 if (new_parent)
4269 klist_remove(&dev->p->knode_parent);
4270 dev->parent = old_parent;
4271 if (old_parent) {
4272 klist_add_tail(&dev->p->knode_parent,
4273 &old_parent->p->klist_children);
4274 set_dev_node(dev, dev_to_node(old_parent));
4275 }
Yinghai Lu0d358f22008-02-19 03:20:41 -08004276 }
Rabin Vincentbdd40342012-04-23 09:16:36 +02004277 cleanup_glue_dir(dev, new_parent_kobj);
4278 put_device(new_parent);
4279 goto out;
Cornelia Huck8a824722006-11-20 17:07:51 +01004280 }
Cornelia Huck8a824722006-11-20 17:07:51 +01004281 }
Cornelia Huckffa6a702009-03-04 12:44:00 +01004282 switch (dpm_order) {
4283 case DPM_ORDER_NONE:
4284 break;
4285 case DPM_ORDER_DEV_AFTER_PARENT:
4286 device_pm_move_after(dev, new_parent);
Grygorii Strashko52cdbdd2015-07-27 20:43:01 +03004287 devices_kset_move_after(dev, new_parent);
Cornelia Huckffa6a702009-03-04 12:44:00 +01004288 break;
4289 case DPM_ORDER_PARENT_BEFORE_DEV:
4290 device_pm_move_before(new_parent, dev);
Grygorii Strashko52cdbdd2015-07-27 20:43:01 +03004291 devices_kset_move_before(new_parent, dev);
Cornelia Huckffa6a702009-03-04 12:44:00 +01004292 break;
4293 case DPM_ORDER_DEV_LAST:
4294 device_pm_move_last(dev);
Grygorii Strashko52cdbdd2015-07-27 20:43:01 +03004295 devices_kset_move_last(dev);
Cornelia Huckffa6a702009-03-04 12:44:00 +01004296 break;
4297 }
Rabin Vincentbdd40342012-04-23 09:16:36 +02004298
Cornelia Huck8a824722006-11-20 17:07:51 +01004299 put_device(old_parent);
4300out:
Cornelia Huckffa6a702009-03-04 12:44:00 +01004301 device_pm_unlock();
Cornelia Huck8a824722006-11-20 17:07:51 +01004302 put_device(dev);
4303 return error;
4304}
Cornelia Huck8a824722006-11-20 17:07:51 +01004305EXPORT_SYMBOL_GPL(device_move);
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08004306
Christian Braunerb8f33e52020-02-27 04:37:15 +01004307static int device_attrs_change_owner(struct device *dev, kuid_t kuid,
4308 kgid_t kgid)
4309{
4310 struct kobject *kobj = &dev->kobj;
4311 struct class *class = dev->class;
4312 const struct device_type *type = dev->type;
4313 int error;
4314
4315 if (class) {
4316 /*
4317 * Change the device groups of the device class for @dev to
4318 * @kuid/@kgid.
4319 */
4320 error = sysfs_groups_change_owner(kobj, class->dev_groups, kuid,
4321 kgid);
4322 if (error)
4323 return error;
4324 }
4325
4326 if (type) {
4327 /*
4328 * Change the device groups of the device type for @dev to
4329 * @kuid/@kgid.
4330 */
4331 error = sysfs_groups_change_owner(kobj, type->groups, kuid,
4332 kgid);
4333 if (error)
4334 return error;
4335 }
4336
4337 /* Change the device groups of @dev to @kuid/@kgid. */
4338 error = sysfs_groups_change_owner(kobj, dev->groups, kuid, kgid);
4339 if (error)
4340 return error;
4341
4342 if (device_supports_offline(dev) && !dev->offline_disabled) {
4343 /* Change online device attributes of @dev to @kuid/@kgid. */
4344 error = sysfs_file_change_owner(kobj, dev_attr_online.attr.name,
4345 kuid, kgid);
4346 if (error)
4347 return error;
4348 }
4349
4350 return 0;
4351}
4352
4353/**
4354 * device_change_owner - change the owner of an existing device.
4355 * @dev: device.
4356 * @kuid: new owner's kuid
4357 * @kgid: new owner's kgid
4358 *
4359 * This changes the owner of @dev and its corresponding sysfs entries to
4360 * @kuid/@kgid. This function closely mirrors how @dev was added via driver
4361 * core.
4362 *
4363 * Returns 0 on success or error code on failure.
4364 */
4365int device_change_owner(struct device *dev, kuid_t kuid, kgid_t kgid)
4366{
4367 int error;
4368 struct kobject *kobj = &dev->kobj;
4369
4370 dev = get_device(dev);
4371 if (!dev)
4372 return -EINVAL;
4373
4374 /*
4375 * Change the kobject and the default attributes and groups of the
4376 * ktype associated with it to @kuid/@kgid.
4377 */
4378 error = sysfs_change_owner(kobj, kuid, kgid);
4379 if (error)
4380 goto out;
4381
4382 /*
4383 * Change the uevent file for @dev to the new owner. The uevent file
4384 * was created in a separate step when @dev got added and we mirror
4385 * that step here.
4386 */
4387 error = sysfs_file_change_owner(kobj, dev_attr_uevent.attr.name, kuid,
4388 kgid);
4389 if (error)
4390 goto out;
4391
4392 /*
4393 * Change the device groups, the device groups associated with the
4394 * device class, and the groups associated with the device type of @dev
4395 * to @kuid/@kgid.
4396 */
4397 error = device_attrs_change_owner(dev, kuid, kgid);
4398 if (error)
4399 goto out;
4400
Christian Brauner3b52fc52020-02-27 04:37:16 +01004401 error = dpm_sysfs_change_owner(dev, kuid, kgid);
4402 if (error)
4403 goto out;
4404
Christian Braunerb8f33e52020-02-27 04:37:15 +01004405#ifdef CONFIG_BLOCK
4406 if (sysfs_deprecated && dev->class == &block_class)
4407 goto out;
4408#endif
4409
4410 /*
4411 * Change the owner of the symlink located in the class directory of
4412 * the device class associated with @dev which points to the actual
4413 * directory entry for @dev to @kuid/@kgid. This ensures that the
4414 * symlink shows the same permissions as its target.
4415 */
4416 error = sysfs_link_change_owner(&dev->class->p->subsys.kobj, &dev->kobj,
4417 dev_name(dev), kuid, kgid);
4418 if (error)
4419 goto out;
4420
4421out:
4422 put_device(dev);
4423 return error;
4424}
4425EXPORT_SYMBOL_GPL(device_change_owner);
4426
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08004427/**
4428 * device_shutdown - call ->shutdown() on each device to shutdown.
4429 */
4430void device_shutdown(void)
4431{
Benson Leungf123db82013-09-24 20:05:11 -07004432 struct device *dev, *parent;
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08004433
Pingfan Liu3297c8f2018-07-19 13:14:58 +08004434 wait_for_device_probe();
4435 device_block_probing();
4436
Rafael J. Wysocki65650b32019-10-09 01:29:10 +02004437 cpufreq_suspend();
4438
Hugh Daschbach62458382010-03-22 10:36:37 -07004439 spin_lock(&devices_kset->list_lock);
4440 /*
4441 * Walk the devices list backward, shutting down each in turn.
4442 * Beware that device unplug events may also start pulling
4443 * devices offline, even as the system is shutting down.
4444 */
4445 while (!list_empty(&devices_kset->list)) {
4446 dev = list_entry(devices_kset->list.prev, struct device,
4447 kobj.entry);
Ming Leid1c6c032012-06-22 18:01:40 +08004448
4449 /*
4450 * hold reference count of device's parent to
4451 * prevent it from being freed because parent's
4452 * lock is to be held
4453 */
Benson Leungf123db82013-09-24 20:05:11 -07004454 parent = get_device(dev->parent);
Hugh Daschbach62458382010-03-22 10:36:37 -07004455 get_device(dev);
4456 /*
4457 * Make sure the device is off the kset list, in the
4458 * event that dev->*->shutdown() doesn't remove it.
4459 */
4460 list_del_init(&dev->kobj.entry);
4461 spin_unlock(&devices_kset->list_lock);
Alan Sternfe6b91f2011-12-06 23:24:52 +01004462
Ming Leid1c6c032012-06-22 18:01:40 +08004463 /* hold lock to avoid race with probe/release */
Benson Leungf123db82013-09-24 20:05:11 -07004464 if (parent)
4465 device_lock(parent);
Ming Leid1c6c032012-06-22 18:01:40 +08004466 device_lock(dev);
4467
Alan Sternfe6b91f2011-12-06 23:24:52 +01004468 /* Don't allow any more runtime suspends */
4469 pm_runtime_get_noresume(dev);
4470 pm_runtime_barrier(dev);
Hugh Daschbach62458382010-03-22 10:36:37 -07004471
Michal Suchanek75216212017-08-11 15:44:43 +02004472 if (dev->class && dev->class->shutdown_pre) {
Josh Zimmermanf77af152017-06-25 14:53:23 -07004473 if (initcall_debug)
Michal Suchanek75216212017-08-11 15:44:43 +02004474 dev_info(dev, "shutdown_pre\n");
4475 dev->class->shutdown_pre(dev);
4476 }
4477 if (dev->bus && dev->bus->shutdown) {
ShuoX Liu0246c4f2012-11-23 15:14:12 +08004478 if (initcall_debug)
4479 dev_info(dev, "shutdown\n");
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08004480 dev->bus->shutdown(dev);
4481 } else if (dev->driver && dev->driver->shutdown) {
ShuoX Liu0246c4f2012-11-23 15:14:12 +08004482 if (initcall_debug)
4483 dev_info(dev, "shutdown\n");
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08004484 dev->driver->shutdown(dev);
4485 }
Ming Leid1c6c032012-06-22 18:01:40 +08004486
4487 device_unlock(dev);
Benson Leungf123db82013-09-24 20:05:11 -07004488 if (parent)
4489 device_unlock(parent);
Ming Leid1c6c032012-06-22 18:01:40 +08004490
Hugh Daschbach62458382010-03-22 10:36:37 -07004491 put_device(dev);
Benson Leungf123db82013-09-24 20:05:11 -07004492 put_device(parent);
Hugh Daschbach62458382010-03-22 10:36:37 -07004493
4494 spin_lock(&devices_kset->list_lock);
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08004495 }
Hugh Daschbach62458382010-03-22 10:36:37 -07004496 spin_unlock(&devices_kset->list_lock);
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08004497}
Joe Perches99bcf212010-06-27 01:02:34 +00004498
4499/*
4500 * Device logging functions
4501 */
4502
4503#ifdef CONFIG_PRINTK
John Ogness74caba72020-09-21 13:24:45 +02064504static void
4505set_dev_info(const struct device *dev, struct dev_printk_info *dev_info)
Joe Perches99bcf212010-06-27 01:02:34 +00004506{
Kay Sieversc4e00da2012-05-03 02:29:59 +02004507 const char *subsys;
John Ogness74caba72020-09-21 13:24:45 +02064508
4509 memset(dev_info, 0, sizeof(*dev_info));
Joe Perches99bcf212010-06-27 01:02:34 +00004510
Kay Sieversc4e00da2012-05-03 02:29:59 +02004511 if (dev->class)
4512 subsys = dev->class->name;
4513 else if (dev->bus)
4514 subsys = dev->bus->name;
4515 else
John Ogness74caba72020-09-21 13:24:45 +02064516 return;
Kay Sieversc4e00da2012-05-03 02:29:59 +02004517
John Ogness74caba72020-09-21 13:24:45 +02064518 strscpy(dev_info->subsystem, subsys, sizeof(dev_info->subsystem));
Kay Sieversc4e00da2012-05-03 02:29:59 +02004519
4520 /*
4521 * Add device identifier DEVICE=:
4522 * b12:8 block dev_t
4523 * c127:3 char dev_t
4524 * n8 netdev ifindex
4525 * +sound:card0 subsystem:devname
4526 */
4527 if (MAJOR(dev->devt)) {
4528 char c;
4529
4530 if (strcmp(subsys, "block") == 0)
4531 c = 'b';
4532 else
4533 c = 'c';
John Ogness74caba72020-09-21 13:24:45 +02064534
4535 snprintf(dev_info->device, sizeof(dev_info->device),
4536 "%c%u:%u", c, MAJOR(dev->devt), MINOR(dev->devt));
Kay Sieversc4e00da2012-05-03 02:29:59 +02004537 } else if (strcmp(subsys, "net") == 0) {
4538 struct net_device *net = to_net_dev(dev);
4539
John Ogness74caba72020-09-21 13:24:45 +02064540 snprintf(dev_info->device, sizeof(dev_info->device),
4541 "n%u", net->ifindex);
Kay Sieversc4e00da2012-05-03 02:29:59 +02004542 } else {
John Ogness74caba72020-09-21 13:24:45 +02064543 snprintf(dev_info->device, sizeof(dev_info->device),
4544 "+%s:%s", subsys, dev_name(dev));
Kay Sieversc4e00da2012-05-03 02:29:59 +02004545 }
Joe Perches99bcf212010-06-27 01:02:34 +00004546}
Joe Perches798efc62012-09-12 20:11:29 -07004547
Joe Perches05e4e5b2012-09-12 20:13:37 -07004548int dev_vprintk_emit(int level, const struct device *dev,
4549 const char *fmt, va_list args)
4550{
John Ogness74caba72020-09-21 13:24:45 +02064551 struct dev_printk_info dev_info;
Joe Perches05e4e5b2012-09-12 20:13:37 -07004552
John Ogness74caba72020-09-21 13:24:45 +02064553 set_dev_info(dev, &dev_info);
Joe Perches05e4e5b2012-09-12 20:13:37 -07004554
John Ogness74caba72020-09-21 13:24:45 +02064555 return vprintk_emit(0, level, &dev_info, fmt, args);
Joe Perches05e4e5b2012-09-12 20:13:37 -07004556}
4557EXPORT_SYMBOL(dev_vprintk_emit);
4558
4559int dev_printk_emit(int level, const struct device *dev, const char *fmt, ...)
4560{
4561 va_list args;
4562 int r;
4563
4564 va_start(args, fmt);
4565
4566 r = dev_vprintk_emit(level, dev, fmt, args);
4567
4568 va_end(args);
4569
4570 return r;
4571}
4572EXPORT_SYMBOL(dev_printk_emit);
4573
Joe Perchesd1f10522014-12-25 15:07:04 -08004574static void __dev_printk(const char *level, const struct device *dev,
Joe Perches798efc62012-09-12 20:11:29 -07004575 struct va_format *vaf)
4576{
Joe Perchesd1f10522014-12-25 15:07:04 -08004577 if (dev)
4578 dev_printk_emit(level[1] - '0', dev, "%s %s: %pV",
4579 dev_driver_string(dev), dev_name(dev), vaf);
4580 else
4581 printk("%s(NULL device *): %pV", level, vaf);
Joe Perches798efc62012-09-12 20:11:29 -07004582}
Joe Perches99bcf212010-06-27 01:02:34 +00004583
Joe Perchesd1f10522014-12-25 15:07:04 -08004584void dev_printk(const char *level, const struct device *dev,
4585 const char *fmt, ...)
Joe Perches99bcf212010-06-27 01:02:34 +00004586{
4587 struct va_format vaf;
4588 va_list args;
Joe Perches99bcf212010-06-27 01:02:34 +00004589
4590 va_start(args, fmt);
4591
4592 vaf.fmt = fmt;
4593 vaf.va = &args;
4594
Joe Perchesd1f10522014-12-25 15:07:04 -08004595 __dev_printk(level, dev, &vaf);
Joe Perches798efc62012-09-12 20:11:29 -07004596
Joe Perches99bcf212010-06-27 01:02:34 +00004597 va_end(args);
Joe Perches99bcf212010-06-27 01:02:34 +00004598}
4599EXPORT_SYMBOL(dev_printk);
4600
4601#define define_dev_printk_level(func, kern_level) \
Joe Perchesd1f10522014-12-25 15:07:04 -08004602void func(const struct device *dev, const char *fmt, ...) \
Joe Perches99bcf212010-06-27 01:02:34 +00004603{ \
4604 struct va_format vaf; \
4605 va_list args; \
Joe Perches99bcf212010-06-27 01:02:34 +00004606 \
4607 va_start(args, fmt); \
4608 \
4609 vaf.fmt = fmt; \
4610 vaf.va = &args; \
4611 \
Joe Perchesd1f10522014-12-25 15:07:04 -08004612 __dev_printk(kern_level, dev, &vaf); \
Joe Perches798efc62012-09-12 20:11:29 -07004613 \
Joe Perches99bcf212010-06-27 01:02:34 +00004614 va_end(args); \
Joe Perches99bcf212010-06-27 01:02:34 +00004615} \
4616EXPORT_SYMBOL(func);
4617
Joe Perches663336e2018-05-09 08:15:46 -07004618define_dev_printk_level(_dev_emerg, KERN_EMERG);
4619define_dev_printk_level(_dev_alert, KERN_ALERT);
4620define_dev_printk_level(_dev_crit, KERN_CRIT);
4621define_dev_printk_level(_dev_err, KERN_ERR);
4622define_dev_printk_level(_dev_warn, KERN_WARNING);
4623define_dev_printk_level(_dev_notice, KERN_NOTICE);
Joe Perches99bcf212010-06-27 01:02:34 +00004624define_dev_printk_level(_dev_info, KERN_INFO);
4625
4626#endif
Rafael J. Wysocki97badf82015-04-03 23:23:37 +02004627
Andrzej Hajdaa787e542020-07-13 16:43:21 +02004628/**
4629 * dev_err_probe - probe error check and log helper
4630 * @dev: the pointer to the struct device
4631 * @err: error value to test
4632 * @fmt: printf-style format string
4633 * @...: arguments as specified in the format string
4634 *
4635 * This helper implements common pattern present in probe functions for error
4636 * checking: print debug or error message depending if the error value is
4637 * -EPROBE_DEFER and propagate error upwards.
Andrzej Hajdad090b702020-07-13 16:43:22 +02004638 * In case of -EPROBE_DEFER it sets also defer probe reason, which can be
4639 * checked later by reading devices_deferred debugfs attribute.
Mauro Carvalho Chehab074b3aa2020-09-09 11:53:43 +02004640 * It replaces code sequence::
4641 *
Andrzej Hajdaa787e542020-07-13 16:43:21 +02004642 * if (err != -EPROBE_DEFER)
4643 * dev_err(dev, ...);
4644 * else
4645 * dev_dbg(dev, ...);
4646 * return err;
Mauro Carvalho Chehab074b3aa2020-09-09 11:53:43 +02004647 *
4648 * with::
4649 *
Andrzej Hajdaa787e542020-07-13 16:43:21 +02004650 * return dev_err_probe(dev, err, ...);
4651 *
4652 * Returns @err.
4653 *
4654 */
4655int dev_err_probe(const struct device *dev, int err, const char *fmt, ...)
4656{
4657 struct va_format vaf;
4658 va_list args;
4659
4660 va_start(args, fmt);
4661 vaf.fmt = fmt;
4662 vaf.va = &args;
4663
Andrzej Hajdad090b702020-07-13 16:43:22 +02004664 if (err != -EPROBE_DEFER) {
Michał Mirosław693a8e92020-08-28 18:14:35 +02004665 dev_err(dev, "error %pe: %pV", ERR_PTR(err), &vaf);
Andrzej Hajdad090b702020-07-13 16:43:22 +02004666 } else {
4667 device_set_deferred_probe_reason(dev, &vaf);
Michał Mirosław693a8e92020-08-28 18:14:35 +02004668 dev_dbg(dev, "error %pe: %pV", ERR_PTR(err), &vaf);
Andrzej Hajdad090b702020-07-13 16:43:22 +02004669 }
Andrzej Hajdaa787e542020-07-13 16:43:21 +02004670
4671 va_end(args);
4672
4673 return err;
4674}
4675EXPORT_SYMBOL_GPL(dev_err_probe);
4676
Rafael J. Wysocki97badf82015-04-03 23:23:37 +02004677static inline bool fwnode_is_primary(struct fwnode_handle *fwnode)
4678{
4679 return fwnode && !IS_ERR(fwnode->secondary);
4680}
4681
4682/**
4683 * set_primary_fwnode - Change the primary firmware node of a given device.
4684 * @dev: Device to handle.
4685 * @fwnode: New primary firmware node of the device.
4686 *
4687 * Set the device's firmware node pointer to @fwnode, but if a secondary
4688 * firmware node of the device is present, preserve it.
Bard Liao3f7bdda2021-01-05 17:11:46 +08004689 *
4690 * Valid fwnode cases are:
4691 * - primary --> secondary --> -ENODEV
4692 * - primary --> NULL
4693 * - secondary --> -ENODEV
4694 * - NULL
Rafael J. Wysocki97badf82015-04-03 23:23:37 +02004695 */
4696void set_primary_fwnode(struct device *dev, struct fwnode_handle *fwnode)
4697{
Andy Shevchenko99aed922020-10-22 21:41:00 +03004698 struct device *parent = dev->parent;
Heikki Krogerusc15e1bd2020-08-21 13:53:42 +03004699 struct fwnode_handle *fn = dev->fwnode;
Rafael J. Wysocki97badf82015-04-03 23:23:37 +02004700
Heikki Krogerusc15e1bd2020-08-21 13:53:42 +03004701 if (fwnode) {
Rafael J. Wysocki97badf82015-04-03 23:23:37 +02004702 if (fwnode_is_primary(fn))
4703 fn = fn->secondary;
4704
Mika Westerberg55f89a82015-11-30 17:11:39 +02004705 if (fn) {
4706 WARN_ON(fwnode->secondary);
4707 fwnode->secondary = fn;
4708 }
Rafael J. Wysocki97badf82015-04-03 23:23:37 +02004709 dev->fwnode = fwnode;
4710 } else {
Heikki Krogerusc15e1bd2020-08-21 13:53:42 +03004711 if (fwnode_is_primary(fn)) {
4712 dev->fwnode = fn->secondary;
Bard Liao3f7bdda2021-01-05 17:11:46 +08004713 /* Set fn->secondary = NULL, so fn remains the primary fwnode */
Andy Shevchenko99aed922020-10-22 21:41:00 +03004714 if (!(parent && fn == parent->fwnode))
Bard Liao47f44692021-01-05 17:11:45 +08004715 fn->secondary = NULL;
Heikki Krogerusc15e1bd2020-08-21 13:53:42 +03004716 } else {
4717 dev->fwnode = NULL;
4718 }
Rafael J. Wysocki97badf82015-04-03 23:23:37 +02004719 }
4720}
4721EXPORT_SYMBOL_GPL(set_primary_fwnode);
4722
4723/**
4724 * set_secondary_fwnode - Change the secondary firmware node of a given device.
4725 * @dev: Device to handle.
4726 * @fwnode: New secondary firmware node of the device.
4727 *
4728 * If a primary firmware node of the device is present, set its secondary
4729 * pointer to @fwnode. Otherwise, set the device's firmware node pointer to
4730 * @fwnode.
4731 */
4732void set_secondary_fwnode(struct device *dev, struct fwnode_handle *fwnode)
4733{
4734 if (fwnode)
4735 fwnode->secondary = ERR_PTR(-ENODEV);
4736
4737 if (fwnode_is_primary(dev->fwnode))
4738 dev->fwnode->secondary = fwnode;
4739 else
4740 dev->fwnode = fwnode;
4741}
Andy Shevchenko96489ae2020-04-08 19:09:01 +03004742EXPORT_SYMBOL_GPL(set_secondary_fwnode);
Johan Hovold4e75e1d2017-06-06 17:59:00 +02004743
4744/**
4745 * device_set_of_node_from_dev - reuse device-tree node of another device
4746 * @dev: device whose device-tree node is being set
4747 * @dev2: device whose device-tree node is being reused
4748 *
4749 * Takes another reference to the new device-tree node after first dropping
4750 * any reference held to the old node.
4751 */
4752void device_set_of_node_from_dev(struct device *dev, const struct device *dev2)
4753{
4754 of_node_put(dev->of_node);
4755 dev->of_node = of_node_get(dev2->of_node);
4756 dev->of_node_reused = true;
4757}
4758EXPORT_SYMBOL_GPL(device_set_of_node_from_dev);
Suzuki K Poulose65b66682019-06-14 18:54:01 +01004759
Ioana Ciornei43e76d42021-06-17 15:29:04 +03004760void device_set_node(struct device *dev, struct fwnode_handle *fwnode)
4761{
4762 dev->fwnode = fwnode;
4763 dev->of_node = to_of_node(fwnode);
4764}
4765EXPORT_SYMBOL_GPL(device_set_node);
4766
Suzuki K Poulose6cda08a2019-07-23 23:18:32 +01004767int device_match_name(struct device *dev, const void *name)
4768{
4769 return sysfs_streq(dev_name(dev), name);
4770}
4771EXPORT_SYMBOL_GPL(device_match_name);
4772
Suzuki K Poulose65b66682019-06-14 18:54:01 +01004773int device_match_of_node(struct device *dev, const void *np)
4774{
4775 return dev->of_node == np;
4776}
4777EXPORT_SYMBOL_GPL(device_match_of_node);
Suzuki K Poulose67843bb2019-07-23 23:18:34 +01004778
4779int device_match_fwnode(struct device *dev, const void *fwnode)
4780{
4781 return dev_fwnode(dev) == fwnode;
4782}
4783EXPORT_SYMBOL_GPL(device_match_fwnode);
Suzuki K Poulose4495dfd2019-07-23 23:18:35 +01004784
4785int device_match_devt(struct device *dev, const void *pdevt)
4786{
4787 return dev->devt == *(dev_t *)pdevt;
4788}
4789EXPORT_SYMBOL_GPL(device_match_devt);
Suzuki K Poulose00500142019-07-23 23:18:36 +01004790
4791int device_match_acpi_dev(struct device *dev, const void *adev)
4792{
4793 return ACPI_COMPANION(dev) == adev;
4794}
4795EXPORT_SYMBOL(device_match_acpi_dev);
Suzuki K Poulose6bf85ba2019-07-23 23:18:37 +01004796
4797int device_match_any(struct device *dev, const void *unused)
4798{
4799 return 1;
4800}
4801EXPORT_SYMBOL_GPL(device_match_any);