blob: f0a2331c71a0d767b5952244e98501bd320fb3c5 [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. Wysocki9ed98952016-10-30 17:32:16 +0100197#else /* !CONFIG_SRCU */
198static DECLARE_RWSEM(device_links_lock);
199
200static inline void device_links_write_lock(void)
201{
202 down_write(&device_links_lock);
203}
204
205static inline void device_links_write_unlock(void)
206{
207 up_write(&device_links_lock);
208}
209
210int device_links_read_lock(void)
211{
212 down_read(&device_links_lock);
213 return 0;
214}
215
216void device_links_read_unlock(int not_used)
217{
218 up_read(&device_links_lock);
219}
Joel Fernandes (Google)c2fa1e12019-07-16 18:12:25 -0400220
221#ifdef CONFIG_DEBUG_LOCK_ALLOC
222int device_links_read_lock_held(void)
223{
224 return lockdep_is_held(&device_links_lock);
225}
226#endif
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100227#endif /* !CONFIG_SRCU */
228
Rafael J. Wysocki3d1cf432021-01-15 19:30:51 +0100229static bool device_is_ancestor(struct device *dev, struct device *target)
230{
231 while (target->parent) {
232 target = target->parent;
233 if (dev == target)
234 return true;
235 }
236 return false;
237}
238
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100239/**
240 * device_is_dependent - Check if one device depends on another one
241 * @dev: Device to check dependencies for.
242 * @target: Device to check against.
243 *
244 * Check if @target depends on @dev or any device dependent on it (its child or
245 * its consumer etc). Return 1 if that is the case or 0 otherwise.
246 */
Saravana Kannan7d34ca32020-06-09 18:19:33 -0700247int device_is_dependent(struct device *dev, void *target)
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100248{
249 struct device_link *link;
250 int ret;
251
Rafael J. Wysocki3d1cf432021-01-15 19:30:51 +0100252 /*
253 * The "ancestors" check is needed to catch the case when the target
254 * device has not been completely initialized yet and it is still
255 * missing from the list of children of its parent device.
256 */
257 if (dev == target || device_is_ancestor(dev, target))
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100258 return 1;
259
260 ret = device_for_each_child(dev, target, device_is_dependent);
261 if (ret)
262 return ret;
263
264 list_for_each_entry(link, &dev->links.consumers, s_node) {
Saravana Kannan4b9bbb22020-12-17 19:17:00 -0800265 if ((link->flags & ~DL_FLAG_INFERRED) ==
266 (DL_FLAG_SYNC_STATE_ONLY | DL_FLAG_MANAGED))
Saravana Kannan05ef9832019-10-28 15:00:22 -0700267 continue;
268
Benjamin Gaignarde16f4f32018-07-16 13:37:44 +0200269 if (link->consumer == target)
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100270 return 1;
271
272 ret = device_is_dependent(link->consumer, target);
273 if (ret)
274 break;
275 }
276 return ret;
277}
278
Rafael J. Wysocki515db262019-07-16 17:21:06 +0200279static void device_link_init_status(struct device_link *link,
280 struct device *consumer,
281 struct device *supplier)
282{
283 switch (supplier->links.status) {
284 case DL_DEV_PROBING:
285 switch (consumer->links.status) {
286 case DL_DEV_PROBING:
287 /*
288 * A consumer driver can create a link to a supplier
289 * that has not completed its probing yet as long as it
290 * knows that the supplier is already functional (for
291 * example, it has just acquired some resources from the
292 * supplier).
293 */
294 link->status = DL_STATE_CONSUMER_PROBE;
295 break;
296 default:
297 link->status = DL_STATE_DORMANT;
298 break;
299 }
300 break;
301 case DL_DEV_DRIVER_BOUND:
302 switch (consumer->links.status) {
303 case DL_DEV_PROBING:
304 link->status = DL_STATE_CONSUMER_PROBE;
305 break;
306 case DL_DEV_DRIVER_BOUND:
307 link->status = DL_STATE_ACTIVE;
308 break;
309 default:
310 link->status = DL_STATE_AVAILABLE;
311 break;
312 }
313 break;
314 case DL_DEV_UNBINDING:
315 link->status = DL_STATE_SUPPLIER_UNBIND;
316 break;
317 default:
318 link->status = DL_STATE_DORMANT;
319 break;
320 }
321}
322
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100323static int device_reorder_to_tail(struct device *dev, void *not_used)
324{
325 struct device_link *link;
326
327 /*
328 * Devices that have not been registered yet will be put to the ends
329 * of the lists during the registration, so skip them here.
330 */
331 if (device_is_registered(dev))
332 devices_kset_move_last(dev);
333
334 if (device_pm_initialized(dev))
335 device_pm_move_last(dev);
336
337 device_for_each_child(dev, NULL, device_reorder_to_tail);
Saravana Kannan05ef9832019-10-28 15:00:22 -0700338 list_for_each_entry(link, &dev->links.consumers, s_node) {
Saravana Kannan4b9bbb22020-12-17 19:17:00 -0800339 if ((link->flags & ~DL_FLAG_INFERRED) ==
340 (DL_FLAG_SYNC_STATE_ONLY | DL_FLAG_MANAGED))
Saravana Kannan05ef9832019-10-28 15:00:22 -0700341 continue;
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100342 device_reorder_to_tail(link->consumer, NULL);
Saravana Kannan05ef9832019-10-28 15:00:22 -0700343 }
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100344
345 return 0;
346}
347
348/**
Feng Kan494fd7b2018-04-10 16:57:06 -0700349 * device_pm_move_to_tail - Move set of devices to the end of device lists
350 * @dev: Device to move
351 *
352 * This is a device_reorder_to_tail() wrapper taking the requisite locks.
353 *
354 * It moves the @dev along with all of its children and all of its consumers
355 * to the ends of the device_kset and dpm_list, recursively.
356 */
357void device_pm_move_to_tail(struct device *dev)
358{
359 int idx;
360
361 idx = device_links_read_lock();
362 device_pm_lock();
363 device_reorder_to_tail(dev, NULL);
364 device_pm_unlock();
365 device_links_read_unlock(idx);
366}
367
Saravana Kannan287905e2020-05-21 12:17:58 -0700368#define to_devlink(dev) container_of((dev), struct device_link, link_dev)
369
370static ssize_t status_show(struct device *dev,
Joe Perches948b3ed2020-09-16 13:40:42 -0700371 struct device_attribute *attr, char *buf)
Saravana Kannan287905e2020-05-21 12:17:58 -0700372{
Joe Perches948b3ed2020-09-16 13:40:42 -0700373 const char *output;
Saravana Kannan287905e2020-05-21 12:17:58 -0700374
375 switch (to_devlink(dev)->status) {
376 case DL_STATE_NONE:
Joe Perches948b3ed2020-09-16 13:40:42 -0700377 output = "not tracked";
378 break;
Saravana Kannan287905e2020-05-21 12:17:58 -0700379 case DL_STATE_DORMANT:
Joe Perches948b3ed2020-09-16 13:40:42 -0700380 output = "dormant";
381 break;
Saravana Kannan287905e2020-05-21 12:17:58 -0700382 case DL_STATE_AVAILABLE:
Joe Perches948b3ed2020-09-16 13:40:42 -0700383 output = "available";
384 break;
Saravana Kannan287905e2020-05-21 12:17:58 -0700385 case DL_STATE_CONSUMER_PROBE:
Joe Perches948b3ed2020-09-16 13:40:42 -0700386 output = "consumer probing";
387 break;
Saravana Kannan287905e2020-05-21 12:17:58 -0700388 case DL_STATE_ACTIVE:
Joe Perches948b3ed2020-09-16 13:40:42 -0700389 output = "active";
390 break;
Saravana Kannan287905e2020-05-21 12:17:58 -0700391 case DL_STATE_SUPPLIER_UNBIND:
Joe Perches948b3ed2020-09-16 13:40:42 -0700392 output = "supplier unbinding";
393 break;
Saravana Kannan287905e2020-05-21 12:17:58 -0700394 default:
Joe Perches948b3ed2020-09-16 13:40:42 -0700395 output = "unknown";
396 break;
Saravana Kannan287905e2020-05-21 12:17:58 -0700397 }
Joe Perches948b3ed2020-09-16 13:40:42 -0700398
399 return sysfs_emit(buf, "%s\n", output);
Saravana Kannan287905e2020-05-21 12:17:58 -0700400}
401static DEVICE_ATTR_RO(status);
402
403static ssize_t auto_remove_on_show(struct device *dev,
404 struct device_attribute *attr, char *buf)
405{
406 struct device_link *link = to_devlink(dev);
Joe Perches973c3912020-09-16 13:40:40 -0700407 const char *output;
Saravana Kannan287905e2020-05-21 12:17:58 -0700408
409 if (link->flags & DL_FLAG_AUTOREMOVE_SUPPLIER)
Joe Perches973c3912020-09-16 13:40:40 -0700410 output = "supplier unbind";
Saravana Kannan287905e2020-05-21 12:17:58 -0700411 else if (link->flags & DL_FLAG_AUTOREMOVE_CONSUMER)
Joe Perches973c3912020-09-16 13:40:40 -0700412 output = "consumer unbind";
Saravana Kannan287905e2020-05-21 12:17:58 -0700413 else
Joe Perches973c3912020-09-16 13:40:40 -0700414 output = "never";
Saravana Kannan287905e2020-05-21 12:17:58 -0700415
Joe Perches973c3912020-09-16 13:40:40 -0700416 return sysfs_emit(buf, "%s\n", output);
Saravana Kannan287905e2020-05-21 12:17:58 -0700417}
418static DEVICE_ATTR_RO(auto_remove_on);
419
420static ssize_t runtime_pm_show(struct device *dev,
421 struct device_attribute *attr, char *buf)
422{
423 struct device_link *link = to_devlink(dev);
424
Joe Perchesaa838892020-09-16 13:40:39 -0700425 return sysfs_emit(buf, "%d\n", !!(link->flags & DL_FLAG_PM_RUNTIME));
Saravana Kannan287905e2020-05-21 12:17:58 -0700426}
427static DEVICE_ATTR_RO(runtime_pm);
428
429static ssize_t sync_state_only_show(struct device *dev,
430 struct device_attribute *attr, char *buf)
431{
432 struct device_link *link = to_devlink(dev);
433
Joe Perchesaa838892020-09-16 13:40:39 -0700434 return sysfs_emit(buf, "%d\n",
435 !!(link->flags & DL_FLAG_SYNC_STATE_ONLY));
Saravana Kannan287905e2020-05-21 12:17:58 -0700436}
437static DEVICE_ATTR_RO(sync_state_only);
438
439static struct attribute *devlink_attrs[] = {
440 &dev_attr_status.attr,
441 &dev_attr_auto_remove_on.attr,
442 &dev_attr_runtime_pm.attr,
443 &dev_attr_sync_state_only.attr,
444 NULL,
445};
446ATTRIBUTE_GROUPS(devlink);
447
Saravana Kannan843e6002020-07-16 14:45:23 -0700448static void device_link_free(struct device_link *link)
449{
450 while (refcount_dec_not_one(&link->rpm_active))
451 pm_runtime_put(link->supplier);
452
453 put_device(link->consumer);
454 put_device(link->supplier);
455 kfree(link);
456}
457
458#ifdef CONFIG_SRCU
459static void __device_link_free_srcu(struct rcu_head *rhead)
460{
461 device_link_free(container_of(rhead, struct device_link, rcu_head));
462}
463
Saravana Kannan287905e2020-05-21 12:17:58 -0700464static void devlink_dev_release(struct device *dev)
465{
Saravana Kannan843e6002020-07-16 14:45:23 -0700466 struct device_link *link = to_devlink(dev);
467
468 call_srcu(&device_links_srcu, &link->rcu_head, __device_link_free_srcu);
Saravana Kannan287905e2020-05-21 12:17:58 -0700469}
Saravana Kannan843e6002020-07-16 14:45:23 -0700470#else
471static void devlink_dev_release(struct device *dev)
472{
473 device_link_free(to_devlink(dev));
474}
475#endif
Saravana Kannan287905e2020-05-21 12:17:58 -0700476
477static struct class devlink_class = {
478 .name = "devlink",
479 .owner = THIS_MODULE,
480 .dev_groups = devlink_groups,
481 .dev_release = devlink_dev_release,
482};
483
484static int devlink_add_symlinks(struct device *dev,
485 struct class_interface *class_intf)
486{
487 int ret;
488 size_t len;
489 struct device_link *link = to_devlink(dev);
490 struct device *sup = link->supplier;
491 struct device *con = link->consumer;
492 char *buf;
493
Saravana Kannane020ff62021-01-10 09:54:07 -0800494 len = max(strlen(dev_bus_name(sup)) + strlen(dev_name(sup)),
495 strlen(dev_bus_name(con)) + strlen(dev_name(con)));
496 len += strlen(":");
Saravana Kannan287905e2020-05-21 12:17:58 -0700497 len += strlen("supplier:") + 1;
498 buf = kzalloc(len, GFP_KERNEL);
499 if (!buf)
500 return -ENOMEM;
501
502 ret = sysfs_create_link(&link->link_dev.kobj, &sup->kobj, "supplier");
503 if (ret)
504 goto out;
505
506 ret = sysfs_create_link(&link->link_dev.kobj, &con->kobj, "consumer");
507 if (ret)
508 goto err_con;
509
Saravana Kannane020ff62021-01-10 09:54:07 -0800510 snprintf(buf, len, "consumer:%s:%s", dev_bus_name(con), dev_name(con));
Saravana Kannan287905e2020-05-21 12:17:58 -0700511 ret = sysfs_create_link(&sup->kobj, &link->link_dev.kobj, buf);
512 if (ret)
513 goto err_con_dev;
514
Saravana Kannane020ff62021-01-10 09:54:07 -0800515 snprintf(buf, len, "supplier:%s:%s", dev_bus_name(sup), dev_name(sup));
Saravana Kannan287905e2020-05-21 12:17:58 -0700516 ret = sysfs_create_link(&con->kobj, &link->link_dev.kobj, buf);
517 if (ret)
518 goto err_sup_dev;
519
520 goto out;
521
522err_sup_dev:
Saravana Kannane020ff62021-01-10 09:54:07 -0800523 snprintf(buf, len, "consumer:%s:%s", dev_bus_name(con), dev_name(con));
Saravana Kannan287905e2020-05-21 12:17:58 -0700524 sysfs_remove_link(&sup->kobj, buf);
525err_con_dev:
526 sysfs_remove_link(&link->link_dev.kobj, "consumer");
527err_con:
528 sysfs_remove_link(&link->link_dev.kobj, "supplier");
529out:
530 kfree(buf);
531 return ret;
532}
533
534static void devlink_remove_symlinks(struct device *dev,
535 struct class_interface *class_intf)
536{
537 struct device_link *link = to_devlink(dev);
538 size_t len;
539 struct device *sup = link->supplier;
540 struct device *con = link->consumer;
541 char *buf;
542
543 sysfs_remove_link(&link->link_dev.kobj, "consumer");
544 sysfs_remove_link(&link->link_dev.kobj, "supplier");
545
Saravana Kannane020ff62021-01-10 09:54:07 -0800546 len = max(strlen(dev_bus_name(sup)) + strlen(dev_name(sup)),
547 strlen(dev_bus_name(con)) + strlen(dev_name(con)));
548 len += strlen(":");
Saravana Kannan287905e2020-05-21 12:17:58 -0700549 len += strlen("supplier:") + 1;
550 buf = kzalloc(len, GFP_KERNEL);
551 if (!buf) {
552 WARN(1, "Unable to properly free device link symlinks!\n");
553 return;
554 }
555
Saravana Kannane020ff62021-01-10 09:54:07 -0800556 snprintf(buf, len, "supplier:%s:%s", dev_bus_name(sup), dev_name(sup));
Saravana Kannan287905e2020-05-21 12:17:58 -0700557 sysfs_remove_link(&con->kobj, buf);
Saravana Kannane020ff62021-01-10 09:54:07 -0800558 snprintf(buf, len, "consumer:%s:%s", dev_bus_name(con), dev_name(con));
Saravana Kannan287905e2020-05-21 12:17:58 -0700559 sysfs_remove_link(&sup->kobj, buf);
560 kfree(buf);
561}
562
563static struct class_interface devlink_class_intf = {
564 .class = &devlink_class,
565 .add_dev = devlink_add_symlinks,
566 .remove_dev = devlink_remove_symlinks,
567};
568
569static int __init devlink_class_init(void)
570{
571 int ret;
572
573 ret = class_register(&devlink_class);
574 if (ret)
575 return ret;
576
577 ret = class_interface_register(&devlink_class_intf);
578 if (ret)
579 class_unregister(&devlink_class);
580
581 return ret;
582}
583postcore_initcall(devlink_class_init);
584
Rafael J. Wysocki515db262019-07-16 17:21:06 +0200585#define DL_MANAGED_LINK_FLAGS (DL_FLAG_AUTOREMOVE_CONSUMER | \
586 DL_FLAG_AUTOREMOVE_SUPPLIER | \
Saravana Kannan05ef9832019-10-28 15:00:22 -0700587 DL_FLAG_AUTOPROBE_CONSUMER | \
Saravana Kannan4b9bbb22020-12-17 19:17:00 -0800588 DL_FLAG_SYNC_STATE_ONLY | \
589 DL_FLAG_INFERRED)
Rafael J. Wysocki515db262019-07-16 17:21:06 +0200590
Rafael J. Wysockifb583c82019-07-30 11:28:57 +0200591#define DL_ADD_VALID_FLAGS (DL_MANAGED_LINK_FLAGS | DL_FLAG_STATELESS | \
592 DL_FLAG_PM_RUNTIME | DL_FLAG_RPM_ACTIVE)
593
Feng Kan494fd7b2018-04-10 16:57:06 -0700594/**
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100595 * device_link_add - Create a link between two devices.
596 * @consumer: Consumer end of the link.
597 * @supplier: Supplier end of the link.
598 * @flags: Link flags.
599 *
Rafael J. Wysocki21d5c572016-10-30 17:32:31 +0100600 * The caller is responsible for the proper synchronization of the link creation
601 * with runtime PM. First, setting the DL_FLAG_PM_RUNTIME flag will cause the
602 * runtime PM framework to take the link into account. Second, if the
603 * DL_FLAG_RPM_ACTIVE flag is set in addition to it, the supplier devices will
Thierry Redingd475f8e2020-11-27 11:46:30 +0100604 * be forced into the active meta state and reference-counted upon the creation
Rafael J. Wysocki21d5c572016-10-30 17:32:31 +0100605 * of the link. If DL_FLAG_PM_RUNTIME is not set, DL_FLAG_RPM_ACTIVE will be
606 * ignored.
607 *
Rafael J. Wysocki515db262019-07-16 17:21:06 +0200608 * If DL_FLAG_STATELESS is set in @flags, the caller of this function is
609 * expected to release the link returned by it directly with the help of either
610 * device_link_del() or device_link_remove().
Rafael J. Wysocki72175d42019-02-01 01:58:33 +0100611 *
612 * If that flag is not set, however, the caller of this function is handing the
613 * management of the link over to the driver core entirely and its return value
614 * can only be used to check whether or not the link is present. In that case,
615 * the DL_FLAG_AUTOREMOVE_CONSUMER and DL_FLAG_AUTOREMOVE_SUPPLIER device link
616 * flags can be used to indicate to the driver core when the link can be safely
617 * deleted. Namely, setting one of them in @flags indicates to the driver core
618 * that the link is not going to be used (by the given caller of this function)
619 * after unbinding the consumer or supplier driver, respectively, from its
620 * device, so the link can be deleted at that point. If none of them is set,
621 * the link will be maintained until one of the devices pointed to by it (either
622 * the consumer or the supplier) is unregistered.
Rafael J. Wysockic8d50982019-02-01 01:45:55 +0100623 *
Rafael J. Wysockie7dd4012019-02-01 01:59:42 +0100624 * Also, if DL_FLAG_STATELESS, DL_FLAG_AUTOREMOVE_CONSUMER and
625 * DL_FLAG_AUTOREMOVE_SUPPLIER are not set in @flags (that is, a persistent
626 * managed device link is being added), the DL_FLAG_AUTOPROBE_CONSUMER flag can
Thierry Redingd475f8e2020-11-27 11:46:30 +0100627 * be used to request the driver core to automatically probe for a consumer
Rafael J. Wysockie7dd4012019-02-01 01:59:42 +0100628 * driver after successfully binding a driver to the supplier device.
629 *
Rafael J. Wysocki515db262019-07-16 17:21:06 +0200630 * The combination of DL_FLAG_STATELESS and one of DL_FLAG_AUTOREMOVE_CONSUMER,
631 * DL_FLAG_AUTOREMOVE_SUPPLIER, or DL_FLAG_AUTOPROBE_CONSUMER set in @flags at
632 * the same time is invalid and will cause NULL to be returned upfront.
633 * However, if a device link between the given @consumer and @supplier pair
634 * exists already when this function is called for them, the existing link will
635 * be returned regardless of its current type and status (the link's flags may
636 * be modified then). The caller of this function is then expected to treat
637 * the link as though it has just been created, so (in particular) if
638 * DL_FLAG_STATELESS was passed in @flags, the link needs to be released
639 * explicitly when not needed any more (as stated above).
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100640 *
641 * A side effect of the link creation is re-ordering of dpm_list and the
642 * devices_kset list by moving the consumer device and all devices depending
643 * on it to the ends of these lists (that does not happen to devices that have
644 * not been registered when this function is called).
645 *
646 * The supplier device is required to be registered when this function is called
647 * and NULL will be returned if that is not the case. The consumer device need
Lukas Wunner64df1142016-12-04 13:10:04 +0100648 * not be registered, however.
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100649 */
650struct device_link *device_link_add(struct device *consumer,
651 struct device *supplier, u32 flags)
652{
653 struct device_link *link;
654
Rafael J. Wysockifb583c82019-07-30 11:28:57 +0200655 if (!consumer || !supplier || flags & ~DL_ADD_VALID_FLAGS ||
Rafael J. Wysocki515db262019-07-16 17:21:06 +0200656 (flags & DL_FLAG_STATELESS && flags & DL_MANAGED_LINK_FLAGS) ||
Saravana Kannan05ef9832019-10-28 15:00:22 -0700657 (flags & DL_FLAG_SYNC_STATE_ONLY &&
Saravana Kannan4b9bbb22020-12-17 19:17:00 -0800658 (flags & ~DL_FLAG_INFERRED) != DL_FLAG_SYNC_STATE_ONLY) ||
Rafael J. Wysockie7dd4012019-02-01 01:59:42 +0100659 (flags & DL_FLAG_AUTOPROBE_CONSUMER &&
660 flags & (DL_FLAG_AUTOREMOVE_CONSUMER |
661 DL_FLAG_AUTOREMOVE_SUPPLIER)))
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100662 return NULL;
663
Rafael J. Wysocki5db25c92019-02-01 01:47:53 +0100664 if (flags & DL_FLAG_PM_RUNTIME && flags & DL_FLAG_RPM_ACTIVE) {
665 if (pm_runtime_get_sync(supplier) < 0) {
666 pm_runtime_put_noidle(supplier);
667 return NULL;
668 }
Rafael J. Wysocki5db25c92019-02-01 01:47:53 +0100669 }
670
Rafael J. Wysocki515db262019-07-16 17:21:06 +0200671 if (!(flags & DL_FLAG_STATELESS))
672 flags |= DL_FLAG_MANAGED;
673
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100674 device_links_write_lock();
675 device_pm_lock();
676
677 /*
678 * If the supplier has not been fully registered yet or there is a
Saravana Kannan05ef9832019-10-28 15:00:22 -0700679 * reverse (non-SYNC_STATE_ONLY) dependency between the consumer and
680 * the supplier already in the graph, return NULL. If the link is a
681 * SYNC_STATE_ONLY link, we don't check for reverse dependencies
682 * because it only affects sync_state() callbacks.
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100683 */
684 if (!device_pm_initialized(supplier)
Saravana Kannan05ef9832019-10-28 15:00:22 -0700685 || (!(flags & DL_FLAG_SYNC_STATE_ONLY) &&
686 device_is_dependent(consumer, supplier))) {
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100687 link = NULL;
688 goto out;
689 }
690
Rafael J. Wysocki72175d42019-02-01 01:58:33 +0100691 /*
Saravana Kannanac66c5b2020-11-20 18:02:24 -0800692 * SYNC_STATE_ONLY links are useless once a consumer device has probed.
693 * So, only create it if the consumer hasn't probed yet.
694 */
695 if (flags & DL_FLAG_SYNC_STATE_ONLY &&
696 consumer->links.status != DL_DEV_NO_DRIVER &&
697 consumer->links.status != DL_DEV_PROBING) {
698 link = NULL;
699 goto out;
700 }
701
702 /*
Rafael J. Wysocki72175d42019-02-01 01:58:33 +0100703 * DL_FLAG_AUTOREMOVE_SUPPLIER indicates that the link will be needed
704 * longer than for DL_FLAG_AUTOREMOVE_CONSUMER and setting them both
705 * together doesn't make sense, so prefer DL_FLAG_AUTOREMOVE_SUPPLIER.
706 */
707 if (flags & DL_FLAG_AUTOREMOVE_SUPPLIER)
708 flags &= ~DL_FLAG_AUTOREMOVE_CONSUMER;
709
Rafael J. Wysockif265df52019-02-01 01:46:54 +0100710 list_for_each_entry(link, &supplier->links.consumers, s_node) {
711 if (link->consumer != consumer)
712 continue;
713
Saravana Kannan4b9bbb22020-12-17 19:17:00 -0800714 if (link->flags & DL_FLAG_INFERRED &&
715 !(flags & DL_FLAG_INFERRED))
716 link->flags &= ~DL_FLAG_INFERRED;
717
Rafael J. Wysockie2f3cd82019-02-01 01:49:14 +0100718 if (flags & DL_FLAG_PM_RUNTIME) {
719 if (!(link->flags & DL_FLAG_PM_RUNTIME)) {
Rafael J. Wysocki4c06c4e2019-02-12 13:08:10 +0100720 pm_runtime_new_link(consumer);
Rafael J. Wysockie2f3cd82019-02-01 01:49:14 +0100721 link->flags |= DL_FLAG_PM_RUNTIME;
722 }
723 if (flags & DL_FLAG_RPM_ACTIVE)
Rafael J. Wysocki36003d42019-02-19 17:53:26 +0100724 refcount_inc(&link->rpm_active);
Rafael J. Wysockie2f3cd82019-02-01 01:49:14 +0100725 }
726
Rafael J. Wysocki72175d42019-02-01 01:58:33 +0100727 if (flags & DL_FLAG_STATELESS) {
728 kref_get(&link->kref);
Saravana Kannan05ef9832019-10-28 15:00:22 -0700729 if (link->flags & DL_FLAG_SYNC_STATE_ONLY &&
Saravana Kannan44e96042020-05-19 21:36:26 -0700730 !(link->flags & DL_FLAG_STATELESS)) {
731 link->flags |= DL_FLAG_STATELESS;
Saravana Kannan05ef9832019-10-28 15:00:22 -0700732 goto reorder;
Saravana Kannan44e96042020-05-19 21:36:26 -0700733 } else {
734 link->flags |= DL_FLAG_STATELESS;
Saravana Kannan05ef9832019-10-28 15:00:22 -0700735 goto out;
Saravana Kannan44e96042020-05-19 21:36:26 -0700736 }
Rafael J. Wysocki72175d42019-02-01 01:58:33 +0100737 }
738
739 /*
740 * If the life time of the link following from the new flags is
741 * longer than indicated by the flags of the existing link,
742 * update the existing link to stay around longer.
743 */
744 if (flags & DL_FLAG_AUTOREMOVE_SUPPLIER) {
745 if (link->flags & DL_FLAG_AUTOREMOVE_CONSUMER) {
746 link->flags &= ~DL_FLAG_AUTOREMOVE_CONSUMER;
747 link->flags |= DL_FLAG_AUTOREMOVE_SUPPLIER;
748 }
749 } else if (!(flags & DL_FLAG_AUTOREMOVE_CONSUMER)) {
750 link->flags &= ~(DL_FLAG_AUTOREMOVE_CONSUMER |
751 DL_FLAG_AUTOREMOVE_SUPPLIER);
752 }
Rafael J. Wysocki515db262019-07-16 17:21:06 +0200753 if (!(link->flags & DL_FLAG_MANAGED)) {
754 kref_get(&link->kref);
755 link->flags |= DL_FLAG_MANAGED;
756 device_link_init_status(link, consumer, supplier);
757 }
Saravana Kannan05ef9832019-10-28 15:00:22 -0700758 if (link->flags & DL_FLAG_SYNC_STATE_ONLY &&
759 !(flags & DL_FLAG_SYNC_STATE_ONLY)) {
760 link->flags &= ~DL_FLAG_SYNC_STATE_ONLY;
761 goto reorder;
762 }
763
Rafael J. Wysockif265df52019-02-01 01:46:54 +0100764 goto out;
765 }
766
Rafael J. Wysocki21d5c572016-10-30 17:32:31 +0100767 link = kzalloc(sizeof(*link), GFP_KERNEL);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100768 if (!link)
769 goto out;
770
Rafael J. Wysockie2f3cd82019-02-01 01:49:14 +0100771 refcount_set(&link->rpm_active, 1);
772
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100773 get_device(supplier);
774 link->supplier = supplier;
775 INIT_LIST_HEAD(&link->s_node);
776 get_device(consumer);
777 link->consumer = consumer;
778 INIT_LIST_HEAD(&link->c_node);
779 link->flags = flags;
Lukas Wunneread18c22018-02-10 19:27:12 +0100780 kref_init(&link->kref);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100781
Saravana Kannan287905e2020-05-21 12:17:58 -0700782 link->link_dev.class = &devlink_class;
783 device_set_pm_not_required(&link->link_dev);
Saravana Kannane020ff62021-01-10 09:54:07 -0800784 dev_set_name(&link->link_dev, "%s:%s--%s:%s",
785 dev_bus_name(supplier), dev_name(supplier),
786 dev_bus_name(consumer), dev_name(consumer));
Saravana Kannan287905e2020-05-21 12:17:58 -0700787 if (device_register(&link->link_dev)) {
788 put_device(consumer);
789 put_device(supplier);
790 kfree(link);
791 link = NULL;
792 goto out;
793 }
794
795 if (flags & DL_FLAG_PM_RUNTIME) {
796 if (flags & DL_FLAG_RPM_ACTIVE)
797 refcount_inc(&link->rpm_active);
798
799 pm_runtime_new_link(consumer);
800 }
801
Lukas Wunner64df1142016-12-04 13:10:04 +0100802 /* Determine the initial link state. */
Rafael J. Wysocki515db262019-07-16 17:21:06 +0200803 if (flags & DL_FLAG_STATELESS)
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100804 link->status = DL_STATE_NONE;
Rafael J. Wysocki515db262019-07-16 17:21:06 +0200805 else
806 device_link_init_status(link, consumer, supplier);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100807
808 /*
Rafael J. Wysocki15cfb092019-02-01 01:50:39 +0100809 * Some callers expect the link creation during consumer driver probe to
810 * resume the supplier even without DL_FLAG_RPM_ACTIVE.
811 */
812 if (link->status == DL_STATE_CONSUMER_PROBE &&
813 flags & DL_FLAG_PM_RUNTIME)
814 pm_runtime_resume(supplier);
815
Saravana Kannan21c27f02020-05-18 23:30:00 -0700816 list_add_tail_rcu(&link->s_node, &supplier->links.consumers);
817 list_add_tail_rcu(&link->c_node, &consumer->links.suppliers);
818
Saravana Kannan05ef9832019-10-28 15:00:22 -0700819 if (flags & DL_FLAG_SYNC_STATE_ONLY) {
820 dev_dbg(consumer,
821 "Linked as a sync state only consumer to %s\n",
822 dev_name(supplier));
823 goto out;
824 }
Saravana Kannan21c27f02020-05-18 23:30:00 -0700825
Saravana Kannan05ef9832019-10-28 15:00:22 -0700826reorder:
Rafael J. Wysocki15cfb092019-02-01 01:50:39 +0100827 /*
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100828 * Move the consumer and all of the devices depending on it to the end
829 * of dpm_list and the devices_kset list.
830 *
831 * It is necessary to hold dpm_list locked throughout all that or else
832 * we may end up suspending with a wrong ordering of it.
833 */
834 device_reorder_to_tail(consumer, NULL);
835
Jerome Brunet8a4b3262018-12-21 17:23:41 +0100836 dev_dbg(consumer, "Linked as a consumer to %s\n", dev_name(supplier));
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100837
Saravana Kannan21c27f02020-05-18 23:30:00 -0700838out:
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100839 device_pm_unlock();
840 device_links_write_unlock();
Rafael J. Wysocki5db25c92019-02-01 01:47:53 +0100841
Rafael J. Wysockie2f3cd82019-02-01 01:49:14 +0100842 if ((flags & DL_FLAG_PM_RUNTIME && flags & DL_FLAG_RPM_ACTIVE) && !link)
Rafael J. Wysocki5db25c92019-02-01 01:47:53 +0100843 pm_runtime_put(supplier);
844
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100845 return link;
846}
847EXPORT_SYMBOL_GPL(device_link_add);
848
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100849#ifdef CONFIG_SRCU
Lukas Wunneread18c22018-02-10 19:27:12 +0100850static void __device_link_del(struct kref *kref)
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100851{
Lukas Wunneread18c22018-02-10 19:27:12 +0100852 struct device_link *link = container_of(kref, struct device_link, kref);
853
Jerome Brunet8a4b3262018-12-21 17:23:41 +0100854 dev_dbg(link->consumer, "Dropping the link to %s\n",
855 dev_name(link->supplier));
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100856
Rafael J. Wysockie0e398e2020-10-21 21:12:15 +0200857 pm_runtime_drop_link(link);
Rafael J. Wysockibaa88092016-10-30 17:32:43 +0100858
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100859 list_del_rcu(&link->s_node);
860 list_del_rcu(&link->c_node);
Saravana Kannan843e6002020-07-16 14:45:23 -0700861 device_unregister(&link->link_dev);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100862}
863#else /* !CONFIG_SRCU */
Lukas Wunneread18c22018-02-10 19:27:12 +0100864static void __device_link_del(struct kref *kref)
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100865{
Lukas Wunneread18c22018-02-10 19:27:12 +0100866 struct device_link *link = container_of(kref, struct device_link, kref);
867
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100868 dev_info(link->consumer, "Dropping the link to %s\n",
869 dev_name(link->supplier));
870
Rafael J. Wysockie0e398e2020-10-21 21:12:15 +0200871 pm_runtime_drop_link(link);
Lukas Wunner433986c2018-02-10 19:13:58 +0100872
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100873 list_del(&link->s_node);
874 list_del(&link->c_node);
Saravana Kannan843e6002020-07-16 14:45:23 -0700875 device_unregister(&link->link_dev);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100876}
877#endif /* !CONFIG_SRCU */
878
Rafael J. Wysocki72175d42019-02-01 01:58:33 +0100879static void device_link_put_kref(struct device_link *link)
880{
881 if (link->flags & DL_FLAG_STATELESS)
882 kref_put(&link->kref, __device_link_del);
883 else
884 WARN(1, "Unable to drop a managed device link reference\n");
885}
886
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100887/**
Rafael J. Wysocki72175d42019-02-01 01:58:33 +0100888 * device_link_del - Delete a stateless link between two devices.
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100889 * @link: Device link to delete.
890 *
891 * The caller must ensure proper synchronization of this function with runtime
Lukas Wunneread18c22018-02-10 19:27:12 +0100892 * PM. If the link was added multiple times, it needs to be deleted as often.
893 * Care is required for hotplugged devices: Their links are purged on removal
894 * and calling device_link_del() is then no longer allowed.
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100895 */
896void device_link_del(struct device_link *link)
897{
898 device_links_write_lock();
Rafael J. Wysocki72175d42019-02-01 01:58:33 +0100899 device_link_put_kref(link);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100900 device_links_write_unlock();
901}
902EXPORT_SYMBOL_GPL(device_link_del);
903
pascal pailletd8842212018-07-05 14:25:56 +0000904/**
Rafael J. Wysocki72175d42019-02-01 01:58:33 +0100905 * device_link_remove - Delete a stateless link between two devices.
pascal pailletd8842212018-07-05 14:25:56 +0000906 * @consumer: Consumer end of the link.
907 * @supplier: Supplier end of the link.
908 *
909 * The caller must ensure proper synchronization of this function with runtime
910 * PM.
911 */
912void device_link_remove(void *consumer, struct device *supplier)
913{
914 struct device_link *link;
915
916 if (WARN_ON(consumer == supplier))
917 return;
918
919 device_links_write_lock();
pascal pailletd8842212018-07-05 14:25:56 +0000920
921 list_for_each_entry(link, &supplier->links.consumers, s_node) {
922 if (link->consumer == consumer) {
Rafael J. Wysocki72175d42019-02-01 01:58:33 +0100923 device_link_put_kref(link);
pascal pailletd8842212018-07-05 14:25:56 +0000924 break;
925 }
926 }
927
pascal pailletd8842212018-07-05 14:25:56 +0000928 device_links_write_unlock();
929}
930EXPORT_SYMBOL_GPL(device_link_remove);
931
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100932static void device_links_missing_supplier(struct device *dev)
933{
934 struct device_link *link;
935
Saravana Kannan8c3e3152020-05-26 15:09:27 -0700936 list_for_each_entry(link, &dev->links.suppliers, c_node) {
937 if (link->status != DL_STATE_CONSUMER_PROBE)
938 continue;
939
940 if (link->supplier->links.status == DL_DEV_DRIVER_BOUND) {
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100941 WRITE_ONCE(link->status, DL_STATE_AVAILABLE);
Saravana Kannan8c3e3152020-05-26 15:09:27 -0700942 } else {
943 WARN_ON(!(link->flags & DL_FLAG_SYNC_STATE_ONLY));
944 WRITE_ONCE(link->status, DL_STATE_DORMANT);
945 }
946 }
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100947}
948
949/**
950 * device_links_check_suppliers - Check presence of supplier drivers.
951 * @dev: Consumer device.
952 *
953 * Check links from this device to any suppliers. Walk the list of the device's
954 * links to suppliers and see if all of them are available. If not, simply
955 * return -EPROBE_DEFER.
956 *
957 * We need to guarantee that the supplier will not go away after the check has
958 * been positive here. It only can go away in __device_release_driver() and
959 * that function checks the device's links to consumers. This means we need to
960 * mark the link as "consumer probe in progress" to make the supplier removal
961 * wait for us to complete (or bad things may happen).
962 *
Rafael J. Wysocki515db262019-07-16 17:21:06 +0200963 * Links without the DL_FLAG_MANAGED flag set are ignored.
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100964 */
965int device_links_check_suppliers(struct device *dev)
966{
967 struct device_link *link;
968 int ret = 0;
969
Saravana Kannane2ae9bc2019-09-04 14:11:21 -0700970 /*
971 * Device waiting for supplier to become available is not allowed to
972 * probe.
973 */
Saravana Kannan25ac86c2020-11-20 18:02:28 -0800974 mutex_lock(&fwnode_link_lock);
975 if (dev->fwnode && !list_empty(&dev->fwnode->suppliers) &&
976 !fw_devlink_is_permissive()) {
Saravana Kannan1f0dfa02020-12-17 19:16:59 -0800977 dev_dbg(dev, "probe deferral - wait for supplier %pfwP\n",
978 list_first_entry(&dev->fwnode->suppliers,
979 struct fwnode_link,
980 c_hook)->supplier);
Saravana Kannan25ac86c2020-11-20 18:02:28 -0800981 mutex_unlock(&fwnode_link_lock);
Saravana Kannane2ae9bc2019-09-04 14:11:21 -0700982 return -EPROBE_DEFER;
983 }
Saravana Kannan25ac86c2020-11-20 18:02:28 -0800984 mutex_unlock(&fwnode_link_lock);
Saravana Kannane2ae9bc2019-09-04 14:11:21 -0700985
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100986 device_links_write_lock();
987
988 list_for_each_entry(link, &dev->links.suppliers, c_node) {
Saravana Kannan8c3e3152020-05-26 15:09:27 -0700989 if (!(link->flags & DL_FLAG_MANAGED))
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100990 continue;
991
Saravana Kannan8c3e3152020-05-26 15:09:27 -0700992 if (link->status != DL_STATE_AVAILABLE &&
993 !(link->flags & DL_FLAG_SYNC_STATE_ONLY)) {
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100994 device_links_missing_supplier(dev);
Saravana Kannan1f0dfa02020-12-17 19:16:59 -0800995 dev_dbg(dev, "probe deferral - supplier %s not ready\n",
996 dev_name(link->supplier));
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100997 ret = -EPROBE_DEFER;
998 break;
999 }
1000 WRITE_ONCE(link->status, DL_STATE_CONSUMER_PROBE);
1001 }
1002 dev->links.status = DL_DEV_PROBING;
1003
1004 device_links_write_unlock();
1005 return ret;
1006}
1007
Saravana Kannan26e77702019-11-14 14:56:45 -08001008/**
1009 * __device_links_queue_sync_state - Queue a device for sync_state() callback
1010 * @dev: Device to call sync_state() on
1011 * @list: List head to queue the @dev on
1012 *
1013 * Queues a device for a sync_state() callback when the device links write lock
1014 * isn't held. This allows the sync_state() execution flow to use device links
1015 * APIs. The caller must ensure this function is called with
1016 * device_links_write_lock() held.
1017 *
1018 * This function does a get_device() to make sure the device is not freed while
1019 * on this list.
1020 *
1021 * So the caller must also ensure that device_links_flush_sync_list() is called
1022 * as soon as the caller releases device_links_write_lock(). This is necessary
1023 * to make sure the sync_state() is called in a timely fashion and the
1024 * put_device() is called on this device.
1025 */
1026static void __device_links_queue_sync_state(struct device *dev,
1027 struct list_head *list)
Saravana Kannanfc5a2512019-09-04 14:11:23 -07001028{
1029 struct device_link *link;
1030
Saravana Kannan77036162020-02-21 00:05:10 -08001031 if (!dev_has_sync_state(dev))
1032 return;
Saravana Kannanfc5a2512019-09-04 14:11:23 -07001033 if (dev->state_synced)
1034 return;
1035
1036 list_for_each_entry(link, &dev->links.consumers, s_node) {
1037 if (!(link->flags & DL_FLAG_MANAGED))
1038 continue;
1039 if (link->status != DL_STATE_ACTIVE)
1040 return;
1041 }
1042
Saravana Kannan26e77702019-11-14 14:56:45 -08001043 /*
1044 * Set the flag here to avoid adding the same device to a list more
1045 * than once. This can happen if new consumers get added to the device
1046 * and probed before the list is flushed.
1047 */
Saravana Kannanfc5a2512019-09-04 14:11:23 -07001048 dev->state_synced = true;
Saravana Kannan26e77702019-11-14 14:56:45 -08001049
Saravana Kannan3b052a32020-11-20 18:02:17 -08001050 if (WARN_ON(!list_empty(&dev->links.defer_sync)))
Saravana Kannan26e77702019-11-14 14:56:45 -08001051 return;
1052
1053 get_device(dev);
Saravana Kannan3b052a32020-11-20 18:02:17 -08001054 list_add_tail(&dev->links.defer_sync, list);
Saravana Kannan26e77702019-11-14 14:56:45 -08001055}
1056
1057/**
1058 * device_links_flush_sync_list - Call sync_state() on a list of devices
1059 * @list: List of devices to call sync_state() on
Saravana Kannan21eb93f2020-02-21 00:05:08 -08001060 * @dont_lock_dev: Device for which lock is already held by the caller
Saravana Kannan26e77702019-11-14 14:56:45 -08001061 *
1062 * Calls sync_state() on all the devices that have been queued for it. This
Saravana Kannan21eb93f2020-02-21 00:05:08 -08001063 * function is used in conjunction with __device_links_queue_sync_state(). The
1064 * @dont_lock_dev parameter is useful when this function is called from a
1065 * context where a device lock is already held.
Saravana Kannan26e77702019-11-14 14:56:45 -08001066 */
Saravana Kannan21eb93f2020-02-21 00:05:08 -08001067static void device_links_flush_sync_list(struct list_head *list,
1068 struct device *dont_lock_dev)
Saravana Kannan26e77702019-11-14 14:56:45 -08001069{
1070 struct device *dev, *tmp;
1071
Saravana Kannan3b052a32020-11-20 18:02:17 -08001072 list_for_each_entry_safe(dev, tmp, list, links.defer_sync) {
1073 list_del_init(&dev->links.defer_sync);
Saravana Kannan26e77702019-11-14 14:56:45 -08001074
Saravana Kannan21eb93f2020-02-21 00:05:08 -08001075 if (dev != dont_lock_dev)
1076 device_lock(dev);
Saravana Kannan26e77702019-11-14 14:56:45 -08001077
1078 if (dev->bus->sync_state)
1079 dev->bus->sync_state(dev);
1080 else if (dev->driver && dev->driver->sync_state)
1081 dev->driver->sync_state(dev);
1082
Saravana Kannan21eb93f2020-02-21 00:05:08 -08001083 if (dev != dont_lock_dev)
1084 device_unlock(dev);
Saravana Kannan26e77702019-11-14 14:56:45 -08001085
1086 put_device(dev);
1087 }
Saravana Kannanfc5a2512019-09-04 14:11:23 -07001088}
1089
1090void device_links_supplier_sync_state_pause(void)
1091{
1092 device_links_write_lock();
1093 defer_sync_state_count++;
1094 device_links_write_unlock();
1095}
1096
1097void device_links_supplier_sync_state_resume(void)
1098{
1099 struct device *dev, *tmp;
Saravana Kannan26e77702019-11-14 14:56:45 -08001100 LIST_HEAD(sync_list);
Saravana Kannanfc5a2512019-09-04 14:11:23 -07001101
1102 device_links_write_lock();
1103 if (!defer_sync_state_count) {
1104 WARN(true, "Unmatched sync_state pause/resume!");
1105 goto out;
1106 }
1107 defer_sync_state_count--;
1108 if (defer_sync_state_count)
1109 goto out;
1110
Saravana Kannan3b052a32020-11-20 18:02:17 -08001111 list_for_each_entry_safe(dev, tmp, &deferred_sync, links.defer_sync) {
Saravana Kannan26e77702019-11-14 14:56:45 -08001112 /*
1113 * Delete from deferred_sync list before queuing it to
Saravana Kannan3b052a32020-11-20 18:02:17 -08001114 * sync_list because defer_sync is used for both lists.
Saravana Kannan26e77702019-11-14 14:56:45 -08001115 */
Saravana Kannan3b052a32020-11-20 18:02:17 -08001116 list_del_init(&dev->links.defer_sync);
Saravana Kannan26e77702019-11-14 14:56:45 -08001117 __device_links_queue_sync_state(dev, &sync_list);
Saravana Kannanfc5a2512019-09-04 14:11:23 -07001118 }
1119out:
1120 device_links_write_unlock();
Saravana Kannan26e77702019-11-14 14:56:45 -08001121
Saravana Kannan21eb93f2020-02-21 00:05:08 -08001122 device_links_flush_sync_list(&sync_list, NULL);
Saravana Kannanfc5a2512019-09-04 14:11:23 -07001123}
1124
1125static int sync_state_resume_initcall(void)
1126{
1127 device_links_supplier_sync_state_resume();
1128 return 0;
1129}
1130late_initcall(sync_state_resume_initcall);
1131
1132static void __device_links_supplier_defer_sync(struct device *sup)
1133{
Saravana Kannan3b052a32020-11-20 18:02:17 -08001134 if (list_empty(&sup->links.defer_sync) && dev_has_sync_state(sup))
1135 list_add_tail(&sup->links.defer_sync, &deferred_sync);
Saravana Kannanfc5a2512019-09-04 14:11:23 -07001136}
1137
Saravana Kannan21c27f02020-05-18 23:30:00 -07001138static void device_link_drop_managed(struct device_link *link)
1139{
1140 link->flags &= ~DL_FLAG_MANAGED;
1141 WRITE_ONCE(link->status, DL_STATE_NONE);
1142 kref_put(&link->kref, __device_link_del);
1143}
1144
Saravana Kannanda6d6472020-05-21 12:18:00 -07001145static ssize_t waiting_for_supplier_show(struct device *dev,
1146 struct device_attribute *attr,
1147 char *buf)
1148{
1149 bool val;
1150
1151 device_lock(dev);
Saravana Kannan25ac86c2020-11-20 18:02:28 -08001152 val = !list_empty(&dev->fwnode->suppliers);
Saravana Kannanda6d6472020-05-21 12:18:00 -07001153 device_unlock(dev);
Joe Perchesaa838892020-09-16 13:40:39 -07001154 return sysfs_emit(buf, "%u\n", val);
Saravana Kannanda6d6472020-05-21 12:18:00 -07001155}
1156static DEVICE_ATTR_RO(waiting_for_supplier);
1157
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001158/**
Saravana Kannanb6f617d2021-03-02 13:11:31 -08001159 * device_links_force_bind - Prepares device to be force bound
1160 * @dev: Consumer device.
1161 *
1162 * device_bind_driver() force binds a device to a driver without calling any
1163 * driver probe functions. So the consumer really isn't going to wait for any
1164 * supplier before it's bound to the driver. We still want the device link
1165 * states to be sensible when this happens.
1166 *
1167 * In preparation for device_bind_driver(), this function goes through each
1168 * supplier device links and checks if the supplier is bound. If it is, then
1169 * the device link status is set to CONSUMER_PROBE. Otherwise, the device link
1170 * is dropped. Links without the DL_FLAG_MANAGED flag set are ignored.
1171 */
1172void device_links_force_bind(struct device *dev)
1173{
1174 struct device_link *link, *ln;
1175
1176 device_links_write_lock();
1177
1178 list_for_each_entry_safe(link, ln, &dev->links.suppliers, c_node) {
1179 if (!(link->flags & DL_FLAG_MANAGED))
1180 continue;
1181
1182 if (link->status != DL_STATE_AVAILABLE) {
1183 device_link_drop_managed(link);
1184 continue;
1185 }
1186 WRITE_ONCE(link->status, DL_STATE_CONSUMER_PROBE);
1187 }
1188 dev->links.status = DL_DEV_PROBING;
1189
1190 device_links_write_unlock();
1191}
1192
1193/**
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001194 * device_links_driver_bound - Update device links after probing its driver.
1195 * @dev: Device to update the links for.
1196 *
1197 * The probe has been successful, so update links from this device to any
1198 * consumers by changing their status to "available".
1199 *
1200 * Also change the status of @dev's links to suppliers to "active".
1201 *
Rafael J. Wysocki515db262019-07-16 17:21:06 +02001202 * Links without the DL_FLAG_MANAGED flag set are ignored.
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001203 */
1204void device_links_driver_bound(struct device *dev)
1205{
Saravana Kannan21c27f02020-05-18 23:30:00 -07001206 struct device_link *link, *ln;
Saravana Kannan26e77702019-11-14 14:56:45 -08001207 LIST_HEAD(sync_list);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001208
Saravana Kannanbcbbcfd2019-10-28 15:00:23 -07001209 /*
Saravana Kannan9528e0d2021-02-05 14:26:37 -08001210 * If a device binds successfully, it's expected to have created all
Saravana Kannanbcbbcfd2019-10-28 15:00:23 -07001211 * the device links it needs to or make new device links as it needs
Saravana Kannan9528e0d2021-02-05 14:26:37 -08001212 * them. So, fw_devlink no longer needs to create device links to any
1213 * of the device's suppliers.
1214 *
1215 * Also, if a child firmware node of this bound device is not added as
1216 * a device by now, assume it is never going to be added and make sure
1217 * other devices don't defer probe indefinitely by waiting for such a
1218 * child device.
Saravana Kannanbcbbcfd2019-10-28 15:00:23 -07001219 */
Saravana Kannan9528e0d2021-02-05 14:26:37 -08001220 if (dev->fwnode && dev->fwnode->dev == dev) {
1221 struct fwnode_handle *child;
Saravana Kannanf9aa4602020-11-20 18:02:31 -08001222 fwnode_links_purge_suppliers(dev->fwnode);
Saravana Kannan9528e0d2021-02-05 14:26:37 -08001223 fwnode_for_each_available_child_node(dev->fwnode, child)
1224 fw_devlink_purge_absent_suppliers(child);
1225 }
Saravana Kannanda6d6472020-05-21 12:18:00 -07001226 device_remove_file(dev, &dev_attr_waiting_for_supplier);
Saravana Kannanbcbbcfd2019-10-28 15:00:23 -07001227
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001228 device_links_write_lock();
1229
1230 list_for_each_entry(link, &dev->links.consumers, s_node) {
Rafael J. Wysocki515db262019-07-16 17:21:06 +02001231 if (!(link->flags & DL_FLAG_MANAGED))
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001232 continue;
1233
Rafael J. Wysocki15cfb092019-02-01 01:50:39 +01001234 /*
1235 * Links created during consumer probe may be in the "consumer
1236 * probe" state to start with if the supplier is still probing
1237 * when they are created and they may become "active" if the
1238 * consumer probe returns first. Skip them here.
1239 */
1240 if (link->status == DL_STATE_CONSUMER_PROBE ||
1241 link->status == DL_STATE_ACTIVE)
1242 continue;
1243
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001244 WARN_ON(link->status != DL_STATE_DORMANT);
1245 WRITE_ONCE(link->status, DL_STATE_AVAILABLE);
Rafael J. Wysockie7dd4012019-02-01 01:59:42 +01001246
1247 if (link->flags & DL_FLAG_AUTOPROBE_CONSUMER)
1248 driver_deferred_probe_add(link->consumer);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001249 }
1250
Saravana Kannan21eb93f2020-02-21 00:05:08 -08001251 if (defer_sync_state_count)
1252 __device_links_supplier_defer_sync(dev);
1253 else
1254 __device_links_queue_sync_state(dev, &sync_list);
1255
Saravana Kannan21c27f02020-05-18 23:30:00 -07001256 list_for_each_entry_safe(link, ln, &dev->links.suppliers, c_node) {
1257 struct device *supplier;
1258
Rafael J. Wysocki515db262019-07-16 17:21:06 +02001259 if (!(link->flags & DL_FLAG_MANAGED))
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001260 continue;
1261
Saravana Kannan21c27f02020-05-18 23:30:00 -07001262 supplier = link->supplier;
1263 if (link->flags & DL_FLAG_SYNC_STATE_ONLY) {
1264 /*
1265 * When DL_FLAG_SYNC_STATE_ONLY is set, it means no
1266 * other DL_MANAGED_LINK_FLAGS have been set. So, it's
1267 * save to drop the managed link completely.
1268 */
1269 device_link_drop_managed(link);
1270 } else {
1271 WARN_ON(link->status != DL_STATE_CONSUMER_PROBE);
1272 WRITE_ONCE(link->status, DL_STATE_ACTIVE);
1273 }
Saravana Kannanfc5a2512019-09-04 14:11:23 -07001274
Saravana Kannan21c27f02020-05-18 23:30:00 -07001275 /*
1276 * This needs to be done even for the deleted
1277 * DL_FLAG_SYNC_STATE_ONLY device link in case it was the last
1278 * device link that was preventing the supplier from getting a
1279 * sync_state() call.
1280 */
Saravana Kannanfc5a2512019-09-04 14:11:23 -07001281 if (defer_sync_state_count)
Saravana Kannan21c27f02020-05-18 23:30:00 -07001282 __device_links_supplier_defer_sync(supplier);
Saravana Kannanfc5a2512019-09-04 14:11:23 -07001283 else
Saravana Kannan21c27f02020-05-18 23:30:00 -07001284 __device_links_queue_sync_state(supplier, &sync_list);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001285 }
1286
1287 dev->links.status = DL_DEV_DRIVER_BOUND;
1288
1289 device_links_write_unlock();
Saravana Kannan26e77702019-11-14 14:56:45 -08001290
Saravana Kannan21eb93f2020-02-21 00:05:08 -08001291 device_links_flush_sync_list(&sync_list, dev);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001292}
1293
1294/**
1295 * __device_links_no_driver - Update links of a device without a driver.
1296 * @dev: Device without a drvier.
1297 *
1298 * Delete all non-persistent links from this device to any suppliers.
1299 *
1300 * Persistent links stay around, but their status is changed to "available",
1301 * unless they already are in the "supplier unbind in progress" state in which
1302 * case they need not be updated.
1303 *
Rafael J. Wysocki515db262019-07-16 17:21:06 +02001304 * Links without the DL_FLAG_MANAGED flag set are ignored.
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001305 */
1306static void __device_links_no_driver(struct device *dev)
1307{
1308 struct device_link *link, *ln;
1309
1310 list_for_each_entry_safe_reverse(link, ln, &dev->links.suppliers, c_node) {
Rafael J. Wysocki515db262019-07-16 17:21:06 +02001311 if (!(link->flags & DL_FLAG_MANAGED))
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001312 continue;
1313
Saravana Kannan8c3e3152020-05-26 15:09:27 -07001314 if (link->flags & DL_FLAG_AUTOREMOVE_CONSUMER) {
Rafael J. Wysocki515db262019-07-16 17:21:06 +02001315 device_link_drop_managed(link);
Saravana Kannan8c3e3152020-05-26 15:09:27 -07001316 continue;
1317 }
1318
1319 if (link->status != DL_STATE_CONSUMER_PROBE &&
1320 link->status != DL_STATE_ACTIVE)
1321 continue;
1322
1323 if (link->supplier->links.status == DL_DEV_DRIVER_BOUND) {
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001324 WRITE_ONCE(link->status, DL_STATE_AVAILABLE);
Saravana Kannan8c3e3152020-05-26 15:09:27 -07001325 } else {
1326 WARN_ON(!(link->flags & DL_FLAG_SYNC_STATE_ONLY));
1327 WRITE_ONCE(link->status, DL_STATE_DORMANT);
1328 }
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001329 }
1330
1331 dev->links.status = DL_DEV_NO_DRIVER;
1332}
1333
Rafael J. Wysocki15cfb092019-02-01 01:50:39 +01001334/**
1335 * device_links_no_driver - Update links after failing driver probe.
1336 * @dev: Device whose driver has just failed to probe.
1337 *
1338 * Clean up leftover links to consumers for @dev and invoke
1339 * %__device_links_no_driver() to update links to suppliers for it as
1340 * appropriate.
1341 *
Rafael J. Wysocki515db262019-07-16 17:21:06 +02001342 * Links without the DL_FLAG_MANAGED flag set are ignored.
Rafael J. Wysocki15cfb092019-02-01 01:50:39 +01001343 */
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001344void device_links_no_driver(struct device *dev)
1345{
Rafael J. Wysocki15cfb092019-02-01 01:50:39 +01001346 struct device_link *link;
1347
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001348 device_links_write_lock();
Rafael J. Wysocki15cfb092019-02-01 01:50:39 +01001349
1350 list_for_each_entry(link, &dev->links.consumers, s_node) {
Rafael J. Wysocki515db262019-07-16 17:21:06 +02001351 if (!(link->flags & DL_FLAG_MANAGED))
Rafael J. Wysocki15cfb092019-02-01 01:50:39 +01001352 continue;
1353
1354 /*
1355 * The probe has failed, so if the status of the link is
1356 * "consumer probe" or "active", it must have been added by
1357 * a probing consumer while this device was still probing.
1358 * Change its state to "dormant", as it represents a valid
1359 * relationship, but it is not functionally meaningful.
1360 */
1361 if (link->status == DL_STATE_CONSUMER_PROBE ||
1362 link->status == DL_STATE_ACTIVE)
1363 WRITE_ONCE(link->status, DL_STATE_DORMANT);
1364 }
1365
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001366 __device_links_no_driver(dev);
Rafael J. Wysocki15cfb092019-02-01 01:50:39 +01001367
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001368 device_links_write_unlock();
1369}
1370
1371/**
1372 * device_links_driver_cleanup - Update links after driver removal.
1373 * @dev: Device whose driver has just gone away.
1374 *
1375 * Update links to consumers for @dev by changing their status to "dormant" and
1376 * invoke %__device_links_no_driver() to update links to suppliers for it as
1377 * appropriate.
1378 *
Rafael J. Wysocki515db262019-07-16 17:21:06 +02001379 * Links without the DL_FLAG_MANAGED flag set are ignored.
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001380 */
1381void device_links_driver_cleanup(struct device *dev)
1382{
Rafael J. Wysockic8d50982019-02-01 01:45:55 +01001383 struct device_link *link, *ln;
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001384
1385 device_links_write_lock();
1386
Rafael J. Wysockic8d50982019-02-01 01:45:55 +01001387 list_for_each_entry_safe(link, ln, &dev->links.consumers, s_node) {
Rafael J. Wysocki515db262019-07-16 17:21:06 +02001388 if (!(link->flags & DL_FLAG_MANAGED))
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001389 continue;
1390
Vivek Gautame88728f2018-06-27 18:20:55 +05301391 WARN_ON(link->flags & DL_FLAG_AUTOREMOVE_CONSUMER);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001392 WARN_ON(link->status != DL_STATE_SUPPLIER_UNBIND);
Vivek Gautam1689cac2018-06-27 18:20:56 +05301393
1394 /*
1395 * autoremove the links between this @dev and its consumer
1396 * devices that are not active, i.e. where the link state
1397 * has moved to DL_STATE_SUPPLIER_UNBIND.
1398 */
1399 if (link->status == DL_STATE_SUPPLIER_UNBIND &&
1400 link->flags & DL_FLAG_AUTOREMOVE_SUPPLIER)
Rafael J. Wysocki515db262019-07-16 17:21:06 +02001401 device_link_drop_managed(link);
Vivek Gautam1689cac2018-06-27 18:20:56 +05301402
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001403 WRITE_ONCE(link->status, DL_STATE_DORMANT);
1404 }
1405
Saravana Kannan3b052a32020-11-20 18:02:17 -08001406 list_del_init(&dev->links.defer_sync);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001407 __device_links_no_driver(dev);
1408
1409 device_links_write_unlock();
1410}
1411
1412/**
1413 * device_links_busy - Check if there are any busy links to consumers.
1414 * @dev: Device to check.
1415 *
1416 * Check each consumer of the device and return 'true' if its link's status
1417 * is one of "consumer probe" or "active" (meaning that the given consumer is
1418 * probing right now or its driver is present). Otherwise, change the link
1419 * state to "supplier unbind" to prevent the consumer from being probed
1420 * successfully going forward.
1421 *
1422 * Return 'false' if there are no probing or active consumers.
1423 *
Rafael J. Wysocki515db262019-07-16 17:21:06 +02001424 * Links without the DL_FLAG_MANAGED flag set are ignored.
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001425 */
1426bool device_links_busy(struct device *dev)
1427{
1428 struct device_link *link;
1429 bool ret = false;
1430
1431 device_links_write_lock();
1432
1433 list_for_each_entry(link, &dev->links.consumers, s_node) {
Rafael J. Wysocki515db262019-07-16 17:21:06 +02001434 if (!(link->flags & DL_FLAG_MANAGED))
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001435 continue;
1436
1437 if (link->status == DL_STATE_CONSUMER_PROBE
1438 || link->status == DL_STATE_ACTIVE) {
1439 ret = true;
1440 break;
1441 }
1442 WRITE_ONCE(link->status, DL_STATE_SUPPLIER_UNBIND);
1443 }
1444
1445 dev->links.status = DL_DEV_UNBINDING;
1446
1447 device_links_write_unlock();
1448 return ret;
1449}
1450
1451/**
1452 * device_links_unbind_consumers - Force unbind consumers of the given device.
1453 * @dev: Device to unbind the consumers of.
1454 *
1455 * Walk the list of links to consumers for @dev and if any of them is in the
1456 * "consumer probe" state, wait for all device probes in progress to complete
1457 * and start over.
1458 *
1459 * If that's not the case, change the status of the link to "supplier unbind"
1460 * and check if the link was in the "active" state. If so, force the consumer
1461 * driver to unbind and start over (the consumer will not re-probe as we have
1462 * changed the state of the link already).
1463 *
Rafael J. Wysocki515db262019-07-16 17:21:06 +02001464 * Links without the DL_FLAG_MANAGED flag set are ignored.
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001465 */
1466void device_links_unbind_consumers(struct device *dev)
1467{
1468 struct device_link *link;
1469
1470 start:
1471 device_links_write_lock();
1472
1473 list_for_each_entry(link, &dev->links.consumers, s_node) {
1474 enum device_link_state status;
1475
Saravana Kannan05ef9832019-10-28 15:00:22 -07001476 if (!(link->flags & DL_FLAG_MANAGED) ||
1477 link->flags & DL_FLAG_SYNC_STATE_ONLY)
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001478 continue;
1479
1480 status = link->status;
1481 if (status == DL_STATE_CONSUMER_PROBE) {
1482 device_links_write_unlock();
1483
1484 wait_for_device_probe();
1485 goto start;
1486 }
1487 WRITE_ONCE(link->status, DL_STATE_SUPPLIER_UNBIND);
1488 if (status == DL_STATE_ACTIVE) {
1489 struct device *consumer = link->consumer;
1490
1491 get_device(consumer);
1492
1493 device_links_write_unlock();
1494
1495 device_release_driver_internal(consumer, NULL,
1496 consumer->parent);
1497 put_device(consumer);
1498 goto start;
1499 }
1500 }
1501
1502 device_links_write_unlock();
1503}
1504
1505/**
1506 * device_links_purge - Delete existing links to other devices.
1507 * @dev: Target device.
1508 */
1509static void device_links_purge(struct device *dev)
1510{
1511 struct device_link *link, *ln;
1512
Saravana Kannan287905e2020-05-21 12:17:58 -07001513 if (dev->class == &devlink_class)
1514 return;
1515
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001516 /*
1517 * Delete all of the remaining links from this device to any other
1518 * devices (either consumers or suppliers).
1519 */
1520 device_links_write_lock();
1521
1522 list_for_each_entry_safe_reverse(link, ln, &dev->links.suppliers, c_node) {
1523 WARN_ON(link->status == DL_STATE_ACTIVE);
Lukas Wunneread18c22018-02-10 19:27:12 +01001524 __device_link_del(&link->kref);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001525 }
1526
1527 list_for_each_entry_safe_reverse(link, ln, &dev->links.consumers, s_node) {
1528 WARN_ON(link->status != DL_STATE_DORMANT &&
1529 link->status != DL_STATE_NONE);
Lukas Wunneread18c22018-02-10 19:27:12 +01001530 __device_link_del(&link->kref);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001531 }
1532
1533 device_links_write_unlock();
1534}
1535
Saravana Kannanb90fb8f2020-12-17 19:17:01 -08001536#define FW_DEVLINK_FLAGS_PERMISSIVE (DL_FLAG_INFERRED | \
1537 DL_FLAG_SYNC_STATE_ONLY)
1538#define FW_DEVLINK_FLAGS_ON (DL_FLAG_INFERRED | \
1539 DL_FLAG_AUTOPROBE_CONSUMER)
1540#define FW_DEVLINK_FLAGS_RPM (FW_DEVLINK_FLAGS_ON | \
1541 DL_FLAG_PM_RUNTIME)
1542
Saravana Kannanea718c692021-03-02 13:11:32 -08001543static u32 fw_devlink_flags = FW_DEVLINK_FLAGS_ON;
Saravana Kannan42926ac2020-05-14 22:34:57 -07001544static int __init fw_devlink_setup(char *arg)
1545{
1546 if (!arg)
1547 return -EINVAL;
1548
1549 if (strcmp(arg, "off") == 0) {
1550 fw_devlink_flags = 0;
1551 } else if (strcmp(arg, "permissive") == 0) {
Saravana Kannanb90fb8f2020-12-17 19:17:01 -08001552 fw_devlink_flags = FW_DEVLINK_FLAGS_PERMISSIVE;
Saravana Kannan42926ac2020-05-14 22:34:57 -07001553 } else if (strcmp(arg, "on") == 0) {
Saravana Kannanb90fb8f2020-12-17 19:17:01 -08001554 fw_devlink_flags = FW_DEVLINK_FLAGS_ON;
Saravana Kannan42926ac2020-05-14 22:34:57 -07001555 } else if (strcmp(arg, "rpm") == 0) {
Saravana Kannanb90fb8f2020-12-17 19:17:01 -08001556 fw_devlink_flags = FW_DEVLINK_FLAGS_RPM;
Saravana Kannan42926ac2020-05-14 22:34:57 -07001557 }
1558 return 0;
1559}
1560early_param("fw_devlink", fw_devlink_setup);
1561
Saravana Kannan19d0f5f2021-02-05 14:26:39 -08001562static bool fw_devlink_strict;
1563static int __init fw_devlink_strict_setup(char *arg)
1564{
1565 return strtobool(arg, &fw_devlink_strict);
1566}
1567early_param("fw_devlink.strict", fw_devlink_strict_setup);
1568
Saravana Kannan42926ac2020-05-14 22:34:57 -07001569u32 fw_devlink_get_flags(void)
1570{
1571 return fw_devlink_flags;
1572}
1573
1574static bool fw_devlink_is_permissive(void)
1575{
Saravana Kannanb90fb8f2020-12-17 19:17:01 -08001576 return fw_devlink_flags == FW_DEVLINK_FLAGS_PERMISSIVE;
Saravana Kannan42926ac2020-05-14 22:34:57 -07001577}
1578
Saravana Kannan19d0f5f2021-02-05 14:26:39 -08001579bool fw_devlink_is_strict(void)
1580{
1581 return fw_devlink_strict && !fw_devlink_is_permissive();
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001582}
1583
Saravana Kannanc2c724c2020-11-20 18:02:27 -08001584static void fw_devlink_parse_fwnode(struct fwnode_handle *fwnode)
1585{
1586 if (fwnode->flags & FWNODE_FLAG_LINKS_ADDED)
1587 return;
1588
Saravana Kannan2d09e6e2020-11-20 18:02:32 -08001589 fwnode_call_int_op(fwnode, add_links);
Saravana Kannanc2c724c2020-11-20 18:02:27 -08001590 fwnode->flags |= FWNODE_FLAG_LINKS_ADDED;
1591}
1592
1593static void fw_devlink_parse_fwtree(struct fwnode_handle *fwnode)
1594{
1595 struct fwnode_handle *child = NULL;
1596
1597 fw_devlink_parse_fwnode(fwnode);
1598
1599 while ((child = fwnode_get_next_available_child_node(fwnode, child)))
1600 fw_devlink_parse_fwtree(child);
1601}
1602
Saravana Kannand46f3e32021-04-01 21:03:41 -07001603static void fw_devlink_relax_link(struct device_link *link)
1604{
1605 if (!(link->flags & DL_FLAG_INFERRED))
1606 return;
1607
1608 if (link->flags == (DL_FLAG_MANAGED | FW_DEVLINK_FLAGS_PERMISSIVE))
1609 return;
1610
1611 pm_runtime_drop_link(link);
1612 link->flags = DL_FLAG_MANAGED | FW_DEVLINK_FLAGS_PERMISSIVE;
1613 dev_dbg(link->consumer, "Relaxing link with %s\n",
1614 dev_name(link->supplier));
1615}
1616
1617static int fw_devlink_no_driver(struct device *dev, void *data)
1618{
1619 struct device_link *link = to_devlink(dev);
1620
1621 if (!link->supplier->can_match)
1622 fw_devlink_relax_link(link);
1623
1624 return 0;
1625}
1626
1627void fw_devlink_drivers_done(void)
1628{
1629 fw_devlink_drv_reg_done = true;
1630 device_links_write_lock();
1631 class_for_each_device(&devlink_class, NULL, NULL,
1632 fw_devlink_no_driver);
1633 device_links_write_unlock();
1634}
1635
1636static void fw_devlink_unblock_consumers(struct device *dev)
1637{
1638 struct device_link *link;
1639
1640 if (!fw_devlink_flags || fw_devlink_is_permissive())
1641 return;
1642
1643 device_links_write_lock();
1644 list_for_each_entry(link, &dev->links.consumers, s_node)
1645 fw_devlink_relax_link(link);
1646 device_links_write_unlock();
1647}
1648
Saravana Kannanf9aa4602020-11-20 18:02:31 -08001649/**
Saravana Kannanb0e2fa42020-12-17 19:17:02 -08001650 * fw_devlink_relax_cycle - Convert cyclic links to SYNC_STATE_ONLY links
1651 * @con: Device to check dependencies for.
1652 * @sup: Device to check against.
1653 *
1654 * Check if @sup depends on @con or any device dependent on it (its child or
1655 * its consumer etc). When such a cyclic dependency is found, convert all
1656 * device links created solely by fw_devlink into SYNC_STATE_ONLY device links.
1657 * This is the equivalent of doing fw_devlink=permissive just between the
1658 * devices in the cycle. We need to do this because, at this point, fw_devlink
1659 * can't tell which of these dependencies is not a real dependency.
1660 *
1661 * Return 1 if a cycle is found. Otherwise, return 0.
1662 */
kernel test robotc13b8272020-12-18 14:39:35 +08001663static int fw_devlink_relax_cycle(struct device *con, void *sup)
Saravana Kannanb0e2fa42020-12-17 19:17:02 -08001664{
1665 struct device_link *link;
1666 int ret;
1667
1668 if (con == sup)
1669 return 1;
1670
1671 ret = device_for_each_child(con, sup, fw_devlink_relax_cycle);
1672 if (ret)
1673 return ret;
1674
1675 list_for_each_entry(link, &con->links.consumers, s_node) {
1676 if ((link->flags & ~DL_FLAG_INFERRED) ==
1677 (DL_FLAG_SYNC_STATE_ONLY | DL_FLAG_MANAGED))
1678 continue;
1679
1680 if (!fw_devlink_relax_cycle(link->consumer, sup))
1681 continue;
1682
1683 ret = 1;
1684
Saravana Kannand46f3e32021-04-01 21:03:41 -07001685 fw_devlink_relax_link(link);
Saravana Kannanb0e2fa42020-12-17 19:17:02 -08001686 }
1687 return ret;
1688}
1689
1690/**
Saravana Kannanf9aa4602020-11-20 18:02:31 -08001691 * fw_devlink_create_devlink - Create a device link from a consumer to fwnode
Pierre-Louis Bossart37c52f72021-03-31 18:26:08 -05001692 * @con: consumer device for the device link
1693 * @sup_handle: fwnode handle of supplier
1694 * @flags: devlink flags
Saravana Kannanf9aa4602020-11-20 18:02:31 -08001695 *
1696 * This function will try to create a device link between the consumer device
1697 * @con and the supplier device represented by @sup_handle.
1698 *
1699 * The supplier has to be provided as a fwnode because incorrect cycles in
1700 * fwnode links can sometimes cause the supplier device to never be created.
1701 * This function detects such cases and returns an error if it cannot create a
1702 * device link from the consumer to a missing supplier.
1703 *
1704 * Returns,
1705 * 0 on successfully creating a device link
1706 * -EINVAL if the device link cannot be created as expected
1707 * -EAGAIN if the device link cannot be created right now, but it may be
1708 * possible to do that in the future
1709 */
1710static int fw_devlink_create_devlink(struct device *con,
1711 struct fwnode_handle *sup_handle, u32 flags)
1712{
1713 struct device *sup_dev;
1714 int ret = 0;
1715
1716 sup_dev = get_dev_from_fwnode(sup_handle);
1717 if (sup_dev) {
1718 /*
Saravana Kannan74c782c2021-02-05 14:26:41 -08001719 * If it's one of those drivers that don't actually bind to
1720 * their device using driver core, then don't wait on this
1721 * supplier device indefinitely.
1722 */
1723 if (sup_dev->links.status == DL_DEV_NO_DRIVER &&
1724 sup_handle->flags & FWNODE_FLAG_INITIALIZED) {
1725 ret = -EINVAL;
1726 goto out;
1727 }
1728
1729 /*
Saravana Kannanf9aa4602020-11-20 18:02:31 -08001730 * If this fails, it is due to cycles in device links. Just
1731 * give up on this link and treat it as invalid.
1732 */
Saravana Kannanb0e2fa42020-12-17 19:17:02 -08001733 if (!device_link_add(con, sup_dev, flags) &&
1734 !(flags & DL_FLAG_SYNC_STATE_ONLY)) {
1735 dev_info(con, "Fixing up cyclic dependency with %s\n",
1736 dev_name(sup_dev));
1737 device_links_write_lock();
1738 fw_devlink_relax_cycle(con, sup_dev);
1739 device_links_write_unlock();
1740 device_link_add(con, sup_dev,
1741 FW_DEVLINK_FLAGS_PERMISSIVE);
Saravana Kannanf9aa4602020-11-20 18:02:31 -08001742 ret = -EINVAL;
Saravana Kannanb0e2fa42020-12-17 19:17:02 -08001743 }
Saravana Kannanf9aa4602020-11-20 18:02:31 -08001744
1745 goto out;
1746 }
1747
Saravana Kannan74c782c2021-02-05 14:26:41 -08001748 /* Supplier that's already initialized without a struct device. */
1749 if (sup_handle->flags & FWNODE_FLAG_INITIALIZED)
1750 return -EINVAL;
1751
Saravana Kannanf9aa4602020-11-20 18:02:31 -08001752 /*
1753 * DL_FLAG_SYNC_STATE_ONLY doesn't block probing and supports
1754 * cycles. So cycle detection isn't necessary and shouldn't be
1755 * done.
1756 */
1757 if (flags & DL_FLAG_SYNC_STATE_ONLY)
1758 return -EAGAIN;
1759
1760 /*
1761 * If we can't find the supplier device from its fwnode, it might be
1762 * due to a cyclic dependency between fwnodes. Some of these cycles can
1763 * be broken by applying logic. Check for these types of cycles and
1764 * break them so that devices in the cycle probe properly.
1765 *
1766 * If the supplier's parent is dependent on the consumer, then
1767 * the consumer-supplier dependency is a false dependency. So,
1768 * treat it as an invalid link.
1769 */
1770 sup_dev = fwnode_get_next_parent_dev(sup_handle);
1771 if (sup_dev && device_is_dependent(con, sup_dev)) {
1772 dev_dbg(con, "Not linking to %pfwP - False link\n",
1773 sup_handle);
1774 ret = -EINVAL;
1775 } else {
1776 /*
1777 * Can't check for cycles or no cycles. So let's try
1778 * again later.
1779 */
1780 ret = -EAGAIN;
1781 }
1782
1783out:
1784 put_device(sup_dev);
1785 return ret;
1786}
1787
1788/**
1789 * __fw_devlink_link_to_consumers - Create device links to consumers of a device
Pierre-Louis Bossart37c52f72021-03-31 18:26:08 -05001790 * @dev: Device that needs to be linked to its consumers
Saravana Kannanf9aa4602020-11-20 18:02:31 -08001791 *
1792 * This function looks at all the consumer fwnodes of @dev and creates device
1793 * links between the consumer device and @dev (supplier).
1794 *
1795 * If the consumer device has not been added yet, then this function creates a
1796 * SYNC_STATE_ONLY link between @dev (supplier) and the closest ancestor device
1797 * of the consumer fwnode. This is necessary to make sure @dev doesn't get a
1798 * sync_state() callback before the real consumer device gets to be added and
1799 * then probed.
1800 *
1801 * Once device links are created from the real consumer to @dev (supplier), the
1802 * fwnode links are deleted.
1803 */
1804static void __fw_devlink_link_to_consumers(struct device *dev)
1805{
1806 struct fwnode_handle *fwnode = dev->fwnode;
1807 struct fwnode_link *link, *tmp;
1808
1809 list_for_each_entry_safe(link, tmp, &fwnode->consumers, s_hook) {
1810 u32 dl_flags = fw_devlink_get_flags();
1811 struct device *con_dev;
1812 bool own_link = true;
1813 int ret;
1814
1815 con_dev = get_dev_from_fwnode(link->consumer);
1816 /*
1817 * If consumer device is not available yet, make a "proxy"
1818 * SYNC_STATE_ONLY link from the consumer's parent device to
1819 * the supplier device. This is necessary to make sure the
1820 * supplier doesn't get a sync_state() callback before the real
1821 * consumer can create a device link to the supplier.
1822 *
1823 * This proxy link step is needed to handle the case where the
1824 * consumer's parent device is added before the supplier.
1825 */
1826 if (!con_dev) {
1827 con_dev = fwnode_get_next_parent_dev(link->consumer);
1828 /*
1829 * However, if the consumer's parent device is also the
1830 * parent of the supplier, don't create a
1831 * consumer-supplier link from the parent to its child
1832 * device. Such a dependency is impossible.
1833 */
1834 if (con_dev &&
1835 fwnode_is_ancestor_of(con_dev->fwnode, fwnode)) {
1836 put_device(con_dev);
1837 con_dev = NULL;
1838 } else {
1839 own_link = false;
Saravana Kannanb90fb8f2020-12-17 19:17:01 -08001840 dl_flags = FW_DEVLINK_FLAGS_PERMISSIVE;
Saravana Kannanf9aa4602020-11-20 18:02:31 -08001841 }
1842 }
1843
1844 if (!con_dev)
1845 continue;
1846
1847 ret = fw_devlink_create_devlink(con_dev, fwnode, dl_flags);
1848 put_device(con_dev);
1849 if (!own_link || ret == -EAGAIN)
1850 continue;
1851
1852 list_del(&link->s_hook);
1853 list_del(&link->c_hook);
1854 kfree(link);
1855 }
1856}
1857
1858/**
1859 * __fw_devlink_link_to_suppliers - Create device links to suppliers of a device
Pierre-Louis Bossart37c52f72021-03-31 18:26:08 -05001860 * @dev: The consumer device that needs to be linked to its suppliers
1861 * @fwnode: Root of the fwnode tree that is used to create device links
Saravana Kannanf9aa4602020-11-20 18:02:31 -08001862 *
1863 * This function looks at all the supplier fwnodes of fwnode tree rooted at
1864 * @fwnode and creates device links between @dev (consumer) and all the
1865 * supplier devices of the entire fwnode tree at @fwnode.
1866 *
1867 * The function creates normal (non-SYNC_STATE_ONLY) device links between @dev
1868 * and the real suppliers of @dev. Once these device links are created, the
1869 * fwnode links are deleted. When such device links are successfully created,
1870 * this function is called recursively on those supplier devices. This is
1871 * needed to detect and break some invalid cycles in fwnode links. See
1872 * fw_devlink_create_devlink() for more details.
1873 *
1874 * In addition, it also looks at all the suppliers of the entire fwnode tree
1875 * because some of the child devices of @dev that have not been added yet
1876 * (because @dev hasn't probed) might already have their suppliers added to
1877 * driver core. So, this function creates SYNC_STATE_ONLY device links between
1878 * @dev (consumer) and these suppliers to make sure they don't execute their
1879 * sync_state() callbacks before these child devices have a chance to create
1880 * their device links. The fwnode links that correspond to the child devices
1881 * aren't delete because they are needed later to create the device links
1882 * between the real consumer and supplier devices.
1883 */
1884static void __fw_devlink_link_to_suppliers(struct device *dev,
1885 struct fwnode_handle *fwnode)
1886{
1887 bool own_link = (dev->fwnode == fwnode);
1888 struct fwnode_link *link, *tmp;
1889 struct fwnode_handle *child = NULL;
1890 u32 dl_flags;
1891
1892 if (own_link)
1893 dl_flags = fw_devlink_get_flags();
1894 else
Saravana Kannanb90fb8f2020-12-17 19:17:01 -08001895 dl_flags = FW_DEVLINK_FLAGS_PERMISSIVE;
Saravana Kannanf9aa4602020-11-20 18:02:31 -08001896
1897 list_for_each_entry_safe(link, tmp, &fwnode->suppliers, c_hook) {
1898 int ret;
1899 struct device *sup_dev;
1900 struct fwnode_handle *sup = link->supplier;
1901
1902 ret = fw_devlink_create_devlink(dev, sup, dl_flags);
1903 if (!own_link || ret == -EAGAIN)
1904 continue;
1905
1906 list_del(&link->s_hook);
1907 list_del(&link->c_hook);
1908 kfree(link);
1909
1910 /* If no device link was created, nothing more to do. */
1911 if (ret)
1912 continue;
1913
1914 /*
1915 * If a device link was successfully created to a supplier, we
1916 * now need to try and link the supplier to all its suppliers.
1917 *
1918 * This is needed to detect and delete false dependencies in
1919 * fwnode links that haven't been converted to a device link
1920 * yet. See comments in fw_devlink_create_devlink() for more
1921 * details on the false dependency.
1922 *
1923 * Without deleting these false dependencies, some devices will
1924 * never probe because they'll keep waiting for their false
1925 * dependency fwnode links to be converted to device links.
1926 */
1927 sup_dev = get_dev_from_fwnode(sup);
1928 __fw_devlink_link_to_suppliers(sup_dev, sup_dev->fwnode);
1929 put_device(sup_dev);
1930 }
1931
1932 /*
1933 * Make "proxy" SYNC_STATE_ONLY device links to represent the needs of
1934 * all the descendants. This proxy link step is needed to handle the
1935 * case where the supplier is added before the consumer's parent device
1936 * (@dev).
1937 */
1938 while ((child = fwnode_get_next_available_child_node(fwnode, child)))
1939 __fw_devlink_link_to_suppliers(dev, child);
1940}
1941
Saravana Kannan5f5377e2020-05-14 22:34:58 -07001942static void fw_devlink_link_device(struct device *dev)
1943{
Saravana Kannanf9aa4602020-11-20 18:02:31 -08001944 struct fwnode_handle *fwnode = dev->fwnode;
Saravana Kannan5f5377e2020-05-14 22:34:58 -07001945
Saravana Kannanf9aa4602020-11-20 18:02:31 -08001946 if (!fw_devlink_flags)
1947 return;
Saravana Kannanc84b9092020-11-20 18:02:21 -08001948
Saravana Kannanf9aa4602020-11-20 18:02:31 -08001949 fw_devlink_parse_fwtree(fwnode);
1950
1951 mutex_lock(&fwnode_link_lock);
1952 __fw_devlink_link_to_consumers(dev);
1953 __fw_devlink_link_to_suppliers(dev, fwnode);
1954 mutex_unlock(&fwnode_link_lock);
Saravana Kannan716a7a22020-05-14 22:34:59 -07001955}
Saravana Kannanc84b9092020-11-20 18:02:21 -08001956
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001957/* Device links support end. */
1958
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001959int (*platform_notify)(struct device *dev) = NULL;
1960int (*platform_notify_remove)(struct device *dev) = NULL;
Dan Williamse105b8b2008-04-21 10:51:07 -07001961static struct kobject *dev_kobj;
1962struct kobject *sysfs_dev_char_kobj;
1963struct kobject *sysfs_dev_block_kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964
Rafael J. Wysocki5e33bc42013-08-28 21:41:01 +02001965static DEFINE_MUTEX(device_hotplug_lock);
1966
1967void lock_device_hotplug(void)
1968{
1969 mutex_lock(&device_hotplug_lock);
1970}
1971
1972void unlock_device_hotplug(void)
1973{
1974 mutex_unlock(&device_hotplug_lock);
1975}
1976
1977int lock_device_hotplug_sysfs(void)
1978{
1979 if (mutex_trylock(&device_hotplug_lock))
1980 return 0;
1981
1982 /* Avoid busy looping (5 ms of sleep should do). */
1983 msleep(5);
1984 return restart_syscall();
1985}
1986
Greg Kroah-Hartman4e886c22008-01-27 12:12:43 -08001987#ifdef CONFIG_BLOCK
1988static inline int device_is_not_partition(struct device *dev)
1989{
1990 return !(dev->type == &part_type);
1991}
1992#else
1993static inline int device_is_not_partition(struct device *dev)
1994{
1995 return 1;
1996}
1997#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998
Heikki Krogerus07de0e82018-11-09 17:21:34 +03001999static int
2000device_platform_notify(struct device *dev, enum kobject_action action)
2001{
Heikki Krogerus7847a142018-11-09 17:21:35 +03002002 int ret;
2003
2004 ret = acpi_platform_notify(dev, action);
2005 if (ret)
2006 return ret;
2007
Heikki Krogerus59abd832018-11-09 17:21:36 +03002008 ret = software_node_notify(dev, action);
2009 if (ret)
2010 return ret;
2011
Heikki Krogerus07de0e82018-11-09 17:21:34 +03002012 if (platform_notify && action == KOBJ_ADD)
2013 platform_notify(dev);
2014 else if (platform_notify_remove && action == KOBJ_REMOVE)
2015 platform_notify_remove(dev);
2016 return 0;
2017}
2018
Alan Stern3e956372006-06-16 17:10:48 -04002019/**
2020 * dev_driver_string - Return a device's driver name, if at all possible
2021 * @dev: struct device to get the name of
2022 *
2023 * Will return the device's driver's name if it is bound to a device. If
yan9169c012012-04-20 21:08:45 +08002024 * the device is not bound to a driver, it will return the name of the bus
Alan Stern3e956372006-06-16 17:10:48 -04002025 * it is attached to. If it is not attached to a bus either, an empty
2026 * string will be returned.
2027 */
Jean Delvarebf9ca692008-07-30 12:29:21 -07002028const char *dev_driver_string(const struct device *dev)
Alan Stern3e956372006-06-16 17:10:48 -04002029{
Alan Stern35899722009-12-04 11:06:57 -05002030 struct device_driver *drv;
2031
2032 /* dev->driver can change to NULL underneath us because of unbinding,
2033 * so be careful about accessing it. dev->bus and dev->class should
2034 * never change once they are set, so they don't need special care.
2035 */
Mark Rutland6aa7de02017-10-23 14:07:29 -07002036 drv = READ_ONCE(dev->driver);
Saravana Kannane020ff62021-01-10 09:54:07 -08002037 return drv ? drv->name : dev_bus_name(dev);
Alan Stern3e956372006-06-16 17:10:48 -04002038}
Matthew Wilcox310a9222006-09-23 23:35:04 -06002039EXPORT_SYMBOL(dev_driver_string);
Alan Stern3e956372006-06-16 17:10:48 -04002040
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041#define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr)
2042
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002043static ssize_t dev_attr_show(struct kobject *kobj, struct attribute *attr,
2044 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002046 struct device_attribute *dev_attr = to_dev_attr(attr);
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +02002047 struct device *dev = kobj_to_dev(kobj);
Dmitry Torokhov4a0c20b2005-04-29 01:23:47 -05002048 ssize_t ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049
2050 if (dev_attr->show)
Yani Ioannou54b6f352005-05-17 06:39:34 -04002051 ret = dev_attr->show(dev, dev_attr, buf);
Andrew Morton815d2d52008-03-04 15:09:07 -08002052 if (ret >= (ssize_t)PAGE_SIZE) {
Sergey Senozhatskya52668c2017-12-11 21:50:21 +09002053 printk("dev_attr_show: %pS returned bad count\n",
2054 dev_attr->show);
Andrew Morton815d2d52008-03-04 15:09:07 -08002055 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 return ret;
2057}
2058
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002059static ssize_t dev_attr_store(struct kobject *kobj, struct attribute *attr,
2060 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002062 struct device_attribute *dev_attr = to_dev_attr(attr);
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +02002063 struct device *dev = kobj_to_dev(kobj);
Dmitry Torokhov4a0c20b2005-04-29 01:23:47 -05002064 ssize_t ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065
2066 if (dev_attr->store)
Yani Ioannou54b6f352005-05-17 06:39:34 -04002067 ret = dev_attr->store(dev, dev_attr, buf, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 return ret;
2069}
2070
Emese Revfy52cf25d2010-01-19 02:58:23 +01002071static const struct sysfs_ops dev_sysfs_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 .show = dev_attr_show,
2073 .store = dev_attr_store,
2074};
2075
Kay Sieversca22e562011-12-14 14:29:38 -08002076#define to_ext_attr(x) container_of(x, struct dev_ext_attribute, attr)
2077
2078ssize_t device_store_ulong(struct device *dev,
2079 struct device_attribute *attr,
2080 const char *buf, size_t size)
2081{
2082 struct dev_ext_attribute *ea = to_ext_attr(attr);
Kaitao chengf88184b2018-11-06 08:34:54 -08002083 int ret;
2084 unsigned long new;
2085
2086 ret = kstrtoul(buf, 0, &new);
2087 if (ret)
2088 return ret;
Kay Sieversca22e562011-12-14 14:29:38 -08002089 *(unsigned long *)(ea->var) = new;
2090 /* Always return full write size even if we didn't consume all */
2091 return size;
2092}
2093EXPORT_SYMBOL_GPL(device_store_ulong);
2094
2095ssize_t device_show_ulong(struct device *dev,
2096 struct device_attribute *attr,
2097 char *buf)
2098{
2099 struct dev_ext_attribute *ea = to_ext_attr(attr);
Joe Perchesaa838892020-09-16 13:40:39 -07002100 return sysfs_emit(buf, "%lx\n", *(unsigned long *)(ea->var));
Kay Sieversca22e562011-12-14 14:29:38 -08002101}
2102EXPORT_SYMBOL_GPL(device_show_ulong);
2103
2104ssize_t device_store_int(struct device *dev,
2105 struct device_attribute *attr,
2106 const char *buf, size_t size)
2107{
2108 struct dev_ext_attribute *ea = to_ext_attr(attr);
Kaitao chengf88184b2018-11-06 08:34:54 -08002109 int ret;
2110 long new;
2111
2112 ret = kstrtol(buf, 0, &new);
2113 if (ret)
2114 return ret;
2115
2116 if (new > INT_MAX || new < INT_MIN)
Kay Sieversca22e562011-12-14 14:29:38 -08002117 return -EINVAL;
2118 *(int *)(ea->var) = new;
2119 /* Always return full write size even if we didn't consume all */
2120 return size;
2121}
2122EXPORT_SYMBOL_GPL(device_store_int);
2123
2124ssize_t device_show_int(struct device *dev,
2125 struct device_attribute *attr,
2126 char *buf)
2127{
2128 struct dev_ext_attribute *ea = to_ext_attr(attr);
2129
Joe Perchesaa838892020-09-16 13:40:39 -07002130 return sysfs_emit(buf, "%d\n", *(int *)(ea->var));
Kay Sieversca22e562011-12-14 14:29:38 -08002131}
2132EXPORT_SYMBOL_GPL(device_show_int);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133
Borislav Petkov91872392012-10-09 19:52:05 +02002134ssize_t device_store_bool(struct device *dev, struct device_attribute *attr,
2135 const char *buf, size_t size)
2136{
2137 struct dev_ext_attribute *ea = to_ext_attr(attr);
2138
2139 if (strtobool(buf, ea->var) < 0)
2140 return -EINVAL;
2141
2142 return size;
2143}
2144EXPORT_SYMBOL_GPL(device_store_bool);
2145
2146ssize_t device_show_bool(struct device *dev, struct device_attribute *attr,
2147 char *buf)
2148{
2149 struct dev_ext_attribute *ea = to_ext_attr(attr);
2150
Joe Perchesaa838892020-09-16 13:40:39 -07002151 return sysfs_emit(buf, "%d\n", *(bool *)(ea->var));
Borislav Petkov91872392012-10-09 19:52:05 +02002152}
2153EXPORT_SYMBOL_GPL(device_show_bool);
2154
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155/**
Robert P. J. Dayf8878dc2013-06-01 20:17:34 -04002156 * device_release - free device structure.
2157 * @kobj: device's kobject.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 *
Robert P. J. Dayf8878dc2013-06-01 20:17:34 -04002159 * This is called once the reference count for the object
2160 * reaches 0. We forward the call to the device's release
2161 * method, which should handle actually freeing the structure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002163static void device_release(struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +02002165 struct device *dev = kobj_to_dev(kobj);
Greg Kroah-Hartmanfb069a52008-12-16 12:23:36 -08002166 struct device_private *p = dev->p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167
Ming Leia525a3d2012-07-25 01:42:29 +08002168 /*
2169 * Some platform devices are driven without driver attached
2170 * and managed resources may have been acquired. Make sure
2171 * all resources are released.
2172 *
2173 * Drivers still can add resources into device after device
2174 * is deleted but alive, so release devres here to avoid
2175 * possible memory leak.
2176 */
2177 devres_release_all(dev);
2178
Jim Quinlane0d07272020-09-17 18:43:40 +02002179 kfree(dev->dma_range_map);
2180
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181 if (dev->release)
2182 dev->release(dev);
Kay Sieversf9f852d2006-10-07 21:54:55 +02002183 else if (dev->type && dev->type->release)
2184 dev->type->release(dev);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07002185 else if (dev->class && dev->class->dev_release)
2186 dev->class->dev_release(dev);
Arjan van de Venf810a5c2008-07-25 19:45:39 -07002187 else
Mauro Carvalho Chehab0c1bc6b2020-04-14 18:48:37 +02002188 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 +01002189 dev_name(dev));
Greg Kroah-Hartmanfb069a52008-12-16 12:23:36 -08002190 kfree(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191}
2192
Eric W. Biedermanbc451f22010-03-30 11:31:25 -07002193static const void *device_namespace(struct kobject *kobj)
2194{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +02002195 struct device *dev = kobj_to_dev(kobj);
Eric W. Biedermanbc451f22010-03-30 11:31:25 -07002196 const void *ns = NULL;
2197
2198 if (dev->class && dev->class->ns_type)
2199 ns = dev->class->namespace(dev);
2200
2201 return ns;
2202}
2203
Dmitry Torokhov9944e892018-07-20 21:56:50 +00002204static void device_get_ownership(struct kobject *kobj, kuid_t *uid, kgid_t *gid)
2205{
2206 struct device *dev = kobj_to_dev(kobj);
2207
2208 if (dev->class && dev->class->get_ownership)
2209 dev->class->get_ownership(dev, uid, gid);
2210}
2211
Greg Kroah-Hartman8f4afc42007-10-11 10:47:49 -06002212static struct kobj_type device_ktype = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213 .release = device_release,
2214 .sysfs_ops = &dev_sysfs_ops,
Eric W. Biedermanbc451f22010-03-30 11:31:25 -07002215 .namespace = device_namespace,
Dmitry Torokhov9944e892018-07-20 21:56:50 +00002216 .get_ownership = device_get_ownership,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217};
2218
2219
Kay Sievers312c0042005-11-16 09:00:00 +01002220static int dev_uevent_filter(struct kset *kset, struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221{
2222 struct kobj_type *ktype = get_ktype(kobj);
2223
Greg Kroah-Hartman8f4afc42007-10-11 10:47:49 -06002224 if (ktype == &device_ktype) {
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +02002225 struct device *dev = kobj_to_dev(kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 if (dev->bus)
2227 return 1;
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07002228 if (dev->class)
2229 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230 }
2231 return 0;
2232}
2233
Kay Sievers312c0042005-11-16 09:00:00 +01002234static const char *dev_uevent_name(struct kset *kset, struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +02002236 struct device *dev = kobj_to_dev(kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07002238 if (dev->bus)
2239 return dev->bus->name;
2240 if (dev->class)
2241 return dev->class->name;
2242 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243}
2244
Kay Sievers7eff2e72007-08-14 15:15:12 +02002245static int dev_uevent(struct kset *kset, struct kobject *kobj,
2246 struct kobj_uevent_env *env)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +02002248 struct device *dev = kobj_to_dev(kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249 int retval = 0;
2250
Kay Sievers6fcf53a2009-04-30 15:23:42 +02002251 /* add device node properties if present */
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07002252 if (MAJOR(dev->devt)) {
Kay Sievers6fcf53a2009-04-30 15:23:42 +02002253 const char *tmp;
2254 const char *name;
Al Viro2c9ede52011-07-23 20:24:48 -04002255 umode_t mode = 0;
Greg Kroah-Hartman4e4098a2013-04-11 11:43:29 -07002256 kuid_t uid = GLOBAL_ROOT_UID;
2257 kgid_t gid = GLOBAL_ROOT_GID;
Kay Sievers6fcf53a2009-04-30 15:23:42 +02002258
Kay Sievers7eff2e72007-08-14 15:15:12 +02002259 add_uevent_var(env, "MAJOR=%u", MAJOR(dev->devt));
2260 add_uevent_var(env, "MINOR=%u", MINOR(dev->devt));
Kay Sievers3c2670e2013-04-06 09:56:00 -07002261 name = device_get_devnode(dev, &mode, &uid, &gid, &tmp);
Kay Sievers6fcf53a2009-04-30 15:23:42 +02002262 if (name) {
2263 add_uevent_var(env, "DEVNAME=%s", name);
Kay Sieverse454cea2009-09-18 23:01:12 +02002264 if (mode)
2265 add_uevent_var(env, "DEVMODE=%#o", mode & 0777);
Greg Kroah-Hartman4e4098a2013-04-11 11:43:29 -07002266 if (!uid_eq(uid, GLOBAL_ROOT_UID))
2267 add_uevent_var(env, "DEVUID=%u", from_kuid(&init_user_ns, uid));
2268 if (!gid_eq(gid, GLOBAL_ROOT_GID))
2269 add_uevent_var(env, "DEVGID=%u", from_kgid(&init_user_ns, gid));
Kay Sievers3c2670e2013-04-06 09:56:00 -07002270 kfree(tmp);
Kay Sievers6fcf53a2009-04-30 15:23:42 +02002271 }
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07002272 }
2273
Kay Sievers414264f2007-03-12 21:08:57 +01002274 if (dev->type && dev->type->name)
Kay Sievers7eff2e72007-08-14 15:15:12 +02002275 add_uevent_var(env, "DEVTYPE=%s", dev->type->name);
Kay Sievers414264f2007-03-12 21:08:57 +01002276
Kay Sievers239378f2006-10-07 21:54:55 +02002277 if (dev->driver)
Kay Sievers7eff2e72007-08-14 15:15:12 +02002278 add_uevent_var(env, "DRIVER=%s", dev->driver->name);
Kay Sievers239378f2006-10-07 21:54:55 +02002279
Grant Likely07d57a32012-02-01 11:22:22 -07002280 /* Add common DT information about the device */
2281 of_device_uevent(dev, env);
2282
Kay Sievers7eff2e72007-08-14 15:15:12 +02002283 /* have the bus specific function add its stuff */
Kay Sievers312c0042005-11-16 09:00:00 +01002284 if (dev->bus && dev->bus->uevent) {
Kay Sievers7eff2e72007-08-14 15:15:12 +02002285 retval = dev->bus->uevent(dev, env);
Kay Sieversf9f852d2006-10-07 21:54:55 +02002286 if (retval)
Greg Kroah-Hartman7dc72b22007-11-28 23:49:41 -08002287 pr_debug("device: '%s': %s: bus uevent() returned %d\n",
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01002288 dev_name(dev), __func__, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289 }
2290
Kay Sievers7eff2e72007-08-14 15:15:12 +02002291 /* have the class specific function add its stuff */
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07002292 if (dev->class && dev->class->dev_uevent) {
Kay Sievers7eff2e72007-08-14 15:15:12 +02002293 retval = dev->class->dev_uevent(dev, env);
Kay Sieversf9f852d2006-10-07 21:54:55 +02002294 if (retval)
Greg Kroah-Hartman7dc72b22007-11-28 23:49:41 -08002295 pr_debug("device: '%s': %s: class uevent() "
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01002296 "returned %d\n", dev_name(dev),
Harvey Harrison2b3a3022008-03-04 16:41:05 -08002297 __func__, retval);
Kay Sieversf9f852d2006-10-07 21:54:55 +02002298 }
2299
Stefan Weileef35c22010-08-06 21:11:15 +02002300 /* have the device type specific function add its stuff */
Kay Sieversf9f852d2006-10-07 21:54:55 +02002301 if (dev->type && dev->type->uevent) {
Kay Sievers7eff2e72007-08-14 15:15:12 +02002302 retval = dev->type->uevent(dev, env);
Kay Sieversf9f852d2006-10-07 21:54:55 +02002303 if (retval)
Greg Kroah-Hartman7dc72b22007-11-28 23:49:41 -08002304 pr_debug("device: '%s': %s: dev_type uevent() "
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01002305 "returned %d\n", dev_name(dev),
Harvey Harrison2b3a3022008-03-04 16:41:05 -08002306 __func__, retval);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07002307 }
2308
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 return retval;
2310}
2311
Emese Revfy9cd43612009-12-31 14:52:51 +01002312static const struct kset_uevent_ops device_uevent_ops = {
Kay Sievers312c0042005-11-16 09:00:00 +01002313 .filter = dev_uevent_filter,
2314 .name = dev_uevent_name,
2315 .uevent = dev_uevent,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316};
2317
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07002318static ssize_t uevent_show(struct device *dev, struct device_attribute *attr,
Kay Sievers16574dc2007-04-06 01:40:38 +02002319 char *buf)
2320{
2321 struct kobject *top_kobj;
2322 struct kset *kset;
Kay Sievers7eff2e72007-08-14 15:15:12 +02002323 struct kobj_uevent_env *env = NULL;
Kay Sievers16574dc2007-04-06 01:40:38 +02002324 int i;
Joe Perches948b3ed2020-09-16 13:40:42 -07002325 int len = 0;
Kay Sievers16574dc2007-04-06 01:40:38 +02002326 int retval;
2327
2328 /* search the kset, the device belongs to */
2329 top_kobj = &dev->kobj;
Kay Sievers5c5daf62007-08-12 20:43:55 +02002330 while (!top_kobj->kset && top_kobj->parent)
2331 top_kobj = top_kobj->parent;
Kay Sievers16574dc2007-04-06 01:40:38 +02002332 if (!top_kobj->kset)
2333 goto out;
Kay Sievers5c5daf62007-08-12 20:43:55 +02002334
Kay Sievers16574dc2007-04-06 01:40:38 +02002335 kset = top_kobj->kset;
2336 if (!kset->uevent_ops || !kset->uevent_ops->uevent)
2337 goto out;
2338
2339 /* respect filter */
2340 if (kset->uevent_ops && kset->uevent_ops->filter)
2341 if (!kset->uevent_ops->filter(kset, &dev->kobj))
2342 goto out;
2343
Kay Sievers7eff2e72007-08-14 15:15:12 +02002344 env = kzalloc(sizeof(struct kobj_uevent_env), GFP_KERNEL);
2345 if (!env)
Greg Kroah-Hartmanc7308c82007-05-02 14:14:11 +02002346 return -ENOMEM;
2347
Kay Sievers16574dc2007-04-06 01:40:38 +02002348 /* let the kset specific function add its keys */
Kay Sievers7eff2e72007-08-14 15:15:12 +02002349 retval = kset->uevent_ops->uevent(kset, &dev->kobj, env);
Kay Sievers16574dc2007-04-06 01:40:38 +02002350 if (retval)
2351 goto out;
2352
2353 /* copy keys to file */
Kay Sievers7eff2e72007-08-14 15:15:12 +02002354 for (i = 0; i < env->envp_idx; i++)
Joe Perches948b3ed2020-09-16 13:40:42 -07002355 len += sysfs_emit_at(buf, len, "%s\n", env->envp[i]);
Kay Sievers16574dc2007-04-06 01:40:38 +02002356out:
Kay Sievers7eff2e72007-08-14 15:15:12 +02002357 kfree(env);
Joe Perches948b3ed2020-09-16 13:40:42 -07002358 return len;
Kay Sievers16574dc2007-04-06 01:40:38 +02002359}
2360
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07002361static ssize_t uevent_store(struct device *dev, struct device_attribute *attr,
Kay Sieversa7fd6702005-10-01 14:49:43 +02002362 const char *buf, size_t count)
2363{
Peter Rajnohadf44b472018-12-05 12:27:44 +01002364 int rc;
2365
2366 rc = kobject_synth_uevent(&dev->kobj, buf, count);
2367
2368 if (rc) {
Peter Rajnohaf36776f2017-05-09 15:22:30 +02002369 dev_err(dev, "uevent: failed to send synthetic uevent\n");
Peter Rajnohadf44b472018-12-05 12:27:44 +01002370 return rc;
2371 }
Kay Sievers60a96a52007-07-08 22:29:26 +02002372
Kay Sieversa7fd6702005-10-01 14:49:43 +02002373 return count;
2374}
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07002375static DEVICE_ATTR_RW(uevent);
Kay Sieversa7fd6702005-10-01 14:49:43 +02002376
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07002377static ssize_t online_show(struct device *dev, struct device_attribute *attr,
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02002378 char *buf)
2379{
2380 bool val;
2381
Rafael J. Wysocki5e33bc42013-08-28 21:41:01 +02002382 device_lock(dev);
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02002383 val = !dev->offline;
Rafael J. Wysocki5e33bc42013-08-28 21:41:01 +02002384 device_unlock(dev);
Joe Perchesaa838892020-09-16 13:40:39 -07002385 return sysfs_emit(buf, "%u\n", val);
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02002386}
2387
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07002388static ssize_t online_store(struct device *dev, struct device_attribute *attr,
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02002389 const char *buf, size_t count)
2390{
2391 bool val;
2392 int ret;
2393
2394 ret = strtobool(buf, &val);
2395 if (ret < 0)
2396 return ret;
2397
Rafael J. Wysocki5e33bc42013-08-28 21:41:01 +02002398 ret = lock_device_hotplug_sysfs();
2399 if (ret)
2400 return ret;
2401
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02002402 ret = val ? device_online(dev) : device_offline(dev);
2403 unlock_device_hotplug();
2404 return ret < 0 ? ret : count;
2405}
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07002406static DEVICE_ATTR_RW(online);
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02002407
Rajat Jain70f400d2021-05-24 10:18:11 -07002408static ssize_t removable_show(struct device *dev, struct device_attribute *attr,
2409 char *buf)
2410{
2411 const char *loc;
2412
2413 switch (dev->removable) {
2414 case DEVICE_REMOVABLE:
2415 loc = "removable";
2416 break;
2417 case DEVICE_FIXED:
2418 loc = "fixed";
2419 break;
2420 default:
2421 loc = "unknown";
2422 }
2423 return sysfs_emit(buf, "%s\n", loc);
2424}
2425static DEVICE_ATTR_RO(removable);
2426
Greg Kroah-Hartmanfa6fdb32013-08-08 15:22:55 -07002427int device_add_groups(struct device *dev, const struct attribute_group **groups)
Dmitry Torokhov621a1672007-03-10 01:37:34 -05002428{
Greg Kroah-Hartman3e9b2ba2013-08-21 13:47:50 -07002429 return sysfs_create_groups(&dev->kobj, groups);
Dmitry Torokhov621a1672007-03-10 01:37:34 -05002430}
Dmitry Torokhova7670d42017-07-19 17:24:31 -07002431EXPORT_SYMBOL_GPL(device_add_groups);
Dmitry Torokhov621a1672007-03-10 01:37:34 -05002432
Greg Kroah-Hartmanfa6fdb32013-08-08 15:22:55 -07002433void device_remove_groups(struct device *dev,
2434 const struct attribute_group **groups)
Dmitry Torokhov621a1672007-03-10 01:37:34 -05002435{
Greg Kroah-Hartman3e9b2ba2013-08-21 13:47:50 -07002436 sysfs_remove_groups(&dev->kobj, groups);
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -07002437}
Dmitry Torokhova7670d42017-07-19 17:24:31 -07002438EXPORT_SYMBOL_GPL(device_remove_groups);
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -07002439
Dmitry Torokhov57b8ff02017-07-19 17:24:33 -07002440union device_attr_group_devres {
2441 const struct attribute_group *group;
2442 const struct attribute_group **groups;
2443};
2444
2445static int devm_attr_group_match(struct device *dev, void *res, void *data)
2446{
2447 return ((union device_attr_group_devres *)res)->group == data;
2448}
2449
2450static void devm_attr_group_remove(struct device *dev, void *res)
2451{
2452 union device_attr_group_devres *devres = res;
2453 const struct attribute_group *group = devres->group;
2454
2455 dev_dbg(dev, "%s: removing group %p\n", __func__, group);
2456 sysfs_remove_group(&dev->kobj, group);
2457}
2458
2459static void devm_attr_groups_remove(struct device *dev, void *res)
2460{
2461 union device_attr_group_devres *devres = res;
2462 const struct attribute_group **groups = devres->groups;
2463
2464 dev_dbg(dev, "%s: removing groups %p\n", __func__, groups);
2465 sysfs_remove_groups(&dev->kobj, groups);
2466}
2467
2468/**
2469 * devm_device_add_group - given a device, create a managed attribute group
2470 * @dev: The device to create the group for
2471 * @grp: The attribute group to create
2472 *
2473 * This function creates a group for the first time. It will explicitly
2474 * warn and error if any of the attribute files being created already exist.
2475 *
2476 * Returns 0 on success or error code on failure.
2477 */
2478int devm_device_add_group(struct device *dev, const struct attribute_group *grp)
2479{
2480 union device_attr_group_devres *devres;
2481 int error;
2482
2483 devres = devres_alloc(devm_attr_group_remove,
2484 sizeof(*devres), GFP_KERNEL);
2485 if (!devres)
2486 return -ENOMEM;
2487
2488 error = sysfs_create_group(&dev->kobj, grp);
2489 if (error) {
2490 devres_free(devres);
2491 return error;
2492 }
2493
2494 devres->group = grp;
2495 devres_add(dev, devres);
2496 return 0;
2497}
2498EXPORT_SYMBOL_GPL(devm_device_add_group);
2499
2500/**
2501 * devm_device_remove_group: remove a managed group from a device
2502 * @dev: device to remove the group from
2503 * @grp: group to remove
2504 *
2505 * This function removes a group of attributes from a device. The attributes
2506 * previously have to have been created for this group, otherwise it will fail.
2507 */
2508void devm_device_remove_group(struct device *dev,
2509 const struct attribute_group *grp)
2510{
2511 WARN_ON(devres_release(dev, devm_attr_group_remove,
2512 devm_attr_group_match,
2513 /* cast away const */ (void *)grp));
2514}
2515EXPORT_SYMBOL_GPL(devm_device_remove_group);
2516
2517/**
2518 * devm_device_add_groups - create a bunch of managed attribute groups
2519 * @dev: The device to create the group for
2520 * @groups: The attribute groups to create, NULL terminated
2521 *
2522 * This function creates a bunch of managed attribute groups. If an error
2523 * occurs when creating a group, all previously created groups will be
2524 * removed, unwinding everything back to the original state when this
2525 * function was called. It will explicitly warn and error if any of the
2526 * attribute files being created already exist.
2527 *
2528 * Returns 0 on success or error code from sysfs_create_group on failure.
2529 */
2530int devm_device_add_groups(struct device *dev,
2531 const struct attribute_group **groups)
2532{
2533 union device_attr_group_devres *devres;
2534 int error;
2535
2536 devres = devres_alloc(devm_attr_groups_remove,
2537 sizeof(*devres), GFP_KERNEL);
2538 if (!devres)
2539 return -ENOMEM;
2540
2541 error = sysfs_create_groups(&dev->kobj, groups);
2542 if (error) {
2543 devres_free(devres);
2544 return error;
2545 }
2546
2547 devres->groups = groups;
2548 devres_add(dev, devres);
2549 return 0;
2550}
2551EXPORT_SYMBOL_GPL(devm_device_add_groups);
2552
2553/**
2554 * devm_device_remove_groups - remove a list of managed groups
2555 *
2556 * @dev: The device for the groups to be removed from
2557 * @groups: NULL terminated list of groups to be removed
2558 *
2559 * If groups is not NULL, remove the specified groups from the device.
2560 */
2561void devm_device_remove_groups(struct device *dev,
2562 const struct attribute_group **groups)
2563{
2564 WARN_ON(devres_release(dev, devm_attr_groups_remove,
2565 devm_attr_group_match,
2566 /* cast away const */ (void *)groups));
2567}
2568EXPORT_SYMBOL_GPL(devm_device_remove_groups);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07002570static int device_add_attrs(struct device *dev)
2571{
2572 struct class *class = dev->class;
Stephen Hemmingeraed65af2011-03-28 09:12:52 -07002573 const struct device_type *type = dev->type;
Dmitry Torokhov621a1672007-03-10 01:37:34 -05002574 int error;
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07002575
Dmitry Torokhov621a1672007-03-10 01:37:34 -05002576 if (class) {
Greg Kroah-Hartmand05a6f92013-07-14 16:05:58 -07002577 error = device_add_groups(dev, class->dev_groups);
Kay Sieversf9f852d2006-10-07 21:54:55 +02002578 if (error)
Dmitry Torokhov621a1672007-03-10 01:37:34 -05002579 return error;
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07002580 }
Kay Sieversf9f852d2006-10-07 21:54:55 +02002581
Dmitry Torokhov621a1672007-03-10 01:37:34 -05002582 if (type) {
2583 error = device_add_groups(dev, type->groups);
Kay Sieversf9f852d2006-10-07 21:54:55 +02002584 if (error)
Greg Kroah-Hartmana6b01de2013-10-05 18:19:30 -07002585 goto err_remove_class_groups;
Kay Sieversf9f852d2006-10-07 21:54:55 +02002586 }
2587
Dmitry Torokhov621a1672007-03-10 01:37:34 -05002588 error = device_add_groups(dev, dev->groups);
2589 if (error)
2590 goto err_remove_type_groups;
2591
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02002592 if (device_supports_offline(dev) && !dev->offline_disabled) {
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07002593 error = device_create_file(dev, &dev_attr_online);
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02002594 if (error)
Rafael J. Wysockiecfbf6f2013-12-12 06:11:02 +01002595 goto err_remove_dev_groups;
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02002596 }
2597
Saravana Kannan25ac86c2020-11-20 18:02:28 -08002598 if (fw_devlink_flags && !fw_devlink_is_permissive() && dev->fwnode) {
Saravana Kannanda6d6472020-05-21 12:18:00 -07002599 error = device_create_file(dev, &dev_attr_waiting_for_supplier);
2600 if (error)
2601 goto err_remove_dev_online;
2602 }
2603
Rajat Jain70f400d2021-05-24 10:18:11 -07002604 if (dev_removable_is_valid(dev)) {
2605 error = device_create_file(dev, &dev_attr_removable);
2606 if (error)
2607 goto err_remove_dev_waiting_for_supplier;
2608 }
2609
Dmitry Torokhov621a1672007-03-10 01:37:34 -05002610 return 0;
2611
Rajat Jain70f400d2021-05-24 10:18:11 -07002612 err_remove_dev_waiting_for_supplier:
2613 device_remove_file(dev, &dev_attr_waiting_for_supplier);
Saravana Kannanda6d6472020-05-21 12:18:00 -07002614 err_remove_dev_online:
2615 device_remove_file(dev, &dev_attr_online);
Rafael J. Wysockiecfbf6f2013-12-12 06:11:02 +01002616 err_remove_dev_groups:
2617 device_remove_groups(dev, dev->groups);
Dmitry Torokhov621a1672007-03-10 01:37:34 -05002618 err_remove_type_groups:
2619 if (type)
2620 device_remove_groups(dev, type->groups);
Greg Kroah-Hartmand05a6f92013-07-14 16:05:58 -07002621 err_remove_class_groups:
2622 if (class)
2623 device_remove_groups(dev, class->dev_groups);
Dmitry Torokhov621a1672007-03-10 01:37:34 -05002624
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07002625 return error;
2626}
2627
2628static void device_remove_attrs(struct device *dev)
2629{
2630 struct class *class = dev->class;
Stephen Hemmingeraed65af2011-03-28 09:12:52 -07002631 const struct device_type *type = dev->type;
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07002632
Rajat Jain70f400d2021-05-24 10:18:11 -07002633 device_remove_file(dev, &dev_attr_removable);
Saravana Kannanda6d6472020-05-21 12:18:00 -07002634 device_remove_file(dev, &dev_attr_waiting_for_supplier);
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07002635 device_remove_file(dev, &dev_attr_online);
Dmitry Torokhov621a1672007-03-10 01:37:34 -05002636 device_remove_groups(dev, dev->groups);
Kay Sieversf9f852d2006-10-07 21:54:55 +02002637
Dmitry Torokhov621a1672007-03-10 01:37:34 -05002638 if (type)
2639 device_remove_groups(dev, type->groups);
2640
Greg Kroah-Hartmana6b01de2013-10-05 18:19:30 -07002641 if (class)
Greg Kroah-Hartmand05a6f92013-07-14 16:05:58 -07002642 device_remove_groups(dev, class->dev_groups);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07002643}
2644
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07002645static ssize_t dev_show(struct device *dev, struct device_attribute *attr,
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07002646 char *buf)
2647{
2648 return print_dev_t(buf, dev->devt);
2649}
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07002650static DEVICE_ATTR_RO(dev);
Tejun Heoad6a1e12007-06-14 03:45:17 +09002651
Kay Sieversca22e562011-12-14 14:29:38 -08002652/* /sys/devices/ */
Greg Kroah-Hartman881c6cfd2007-11-01 09:29:06 -06002653struct kset *devices_kset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655/**
Grygorii Strashko52cdbdd2015-07-27 20:43:01 +03002656 * devices_kset_move_before - Move device in the devices_kset's list.
2657 * @deva: Device to move.
2658 * @devb: Device @deva should come before.
2659 */
2660static void devices_kset_move_before(struct device *deva, struct device *devb)
2661{
2662 if (!devices_kset)
2663 return;
2664 pr_debug("devices_kset: Moving %s before %s\n",
2665 dev_name(deva), dev_name(devb));
2666 spin_lock(&devices_kset->list_lock);
2667 list_move_tail(&deva->kobj.entry, &devb->kobj.entry);
2668 spin_unlock(&devices_kset->list_lock);
2669}
2670
2671/**
2672 * devices_kset_move_after - Move device in the devices_kset's list.
2673 * @deva: Device to move
2674 * @devb: Device @deva should come after.
2675 */
2676static void devices_kset_move_after(struct device *deva, struct device *devb)
2677{
2678 if (!devices_kset)
2679 return;
2680 pr_debug("devices_kset: Moving %s after %s\n",
2681 dev_name(deva), dev_name(devb));
2682 spin_lock(&devices_kset->list_lock);
2683 list_move(&deva->kobj.entry, &devb->kobj.entry);
2684 spin_unlock(&devices_kset->list_lock);
2685}
2686
2687/**
2688 * devices_kset_move_last - move the device to the end of devices_kset's list.
2689 * @dev: device to move
2690 */
2691void devices_kset_move_last(struct device *dev)
2692{
2693 if (!devices_kset)
2694 return;
2695 pr_debug("devices_kset: Moving %s to end of list\n", dev_name(dev));
2696 spin_lock(&devices_kset->list_lock);
2697 list_move_tail(&dev->kobj.entry, &devices_kset->list);
2698 spin_unlock(&devices_kset->list_lock);
2699}
2700
2701/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002702 * device_create_file - create sysfs attribute file for device.
2703 * @dev: device.
2704 * @attr: device attribute descriptor.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705 */
Phil Carmody26579ab2009-12-18 15:34:19 +02002706int device_create_file(struct device *dev,
2707 const struct device_attribute *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708{
2709 int error = 0;
Felipe Balbi8f46baa2013-02-20 10:31:42 +02002710
2711 if (dev) {
2712 WARN(((attr->attr.mode & S_IWUGO) && !attr->store),
dyoung@redhat.com97521972013-05-16 14:31:30 +08002713 "Attribute %s: write permission without 'store'\n",
2714 attr->attr.name);
Felipe Balbi8f46baa2013-02-20 10:31:42 +02002715 WARN(((attr->attr.mode & S_IRUGO) && !attr->show),
dyoung@redhat.com97521972013-05-16 14:31:30 +08002716 "Attribute %s: read permission without 'show'\n",
2717 attr->attr.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718 error = sysfs_create_file(&dev->kobj, &attr->attr);
Felipe Balbi8f46baa2013-02-20 10:31:42 +02002719 }
2720
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721 return error;
2722}
David Graham White86df2682013-07-21 20:41:14 -04002723EXPORT_SYMBOL_GPL(device_create_file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724
2725/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002726 * device_remove_file - remove sysfs attribute file.
2727 * @dev: device.
2728 * @attr: device attribute descriptor.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729 */
Phil Carmody26579ab2009-12-18 15:34:19 +02002730void device_remove_file(struct device *dev,
2731 const struct device_attribute *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732{
Cornelia Huck0c98b192008-01-31 10:39:38 +01002733 if (dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734 sysfs_remove_file(&dev->kobj, &attr->attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735}
David Graham White86df2682013-07-21 20:41:14 -04002736EXPORT_SYMBOL_GPL(device_remove_file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737
Greg Kroah-Hartman2589f1882006-09-19 09:39:19 -07002738/**
Tejun Heo6b0afc22014-02-03 14:03:01 -05002739 * device_remove_file_self - remove sysfs attribute file from its own method.
2740 * @dev: device.
2741 * @attr: device attribute descriptor.
2742 *
2743 * See kernfs_remove_self() for details.
2744 */
2745bool device_remove_file_self(struct device *dev,
2746 const struct device_attribute *attr)
2747{
2748 if (dev)
2749 return sysfs_remove_file_self(&dev->kobj, &attr->attr);
2750 else
2751 return false;
2752}
2753EXPORT_SYMBOL_GPL(device_remove_file_self);
2754
2755/**
Greg Kroah-Hartman2589f1882006-09-19 09:39:19 -07002756 * device_create_bin_file - create sysfs binary attribute file for device.
2757 * @dev: device.
2758 * @attr: device binary attribute descriptor.
2759 */
Phil Carmody66ecb922009-12-18 15:34:20 +02002760int device_create_bin_file(struct device *dev,
2761 const struct bin_attribute *attr)
Greg Kroah-Hartman2589f1882006-09-19 09:39:19 -07002762{
2763 int error = -EINVAL;
2764 if (dev)
2765 error = sysfs_create_bin_file(&dev->kobj, attr);
2766 return error;
2767}
2768EXPORT_SYMBOL_GPL(device_create_bin_file);
2769
2770/**
2771 * device_remove_bin_file - remove sysfs binary attribute file
2772 * @dev: device.
2773 * @attr: device binary attribute descriptor.
2774 */
Phil Carmody66ecb922009-12-18 15:34:20 +02002775void device_remove_bin_file(struct device *dev,
2776 const struct bin_attribute *attr)
Greg Kroah-Hartman2589f1882006-09-19 09:39:19 -07002777{
2778 if (dev)
2779 sysfs_remove_bin_file(&dev->kobj, attr);
2780}
2781EXPORT_SYMBOL_GPL(device_remove_bin_file);
2782
James Bottomley34bb61f2005-09-06 16:56:51 -07002783static void klist_children_get(struct klist_node *n)
2784{
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08002785 struct device_private *p = to_device_private_parent(n);
2786 struct device *dev = p->device;
James Bottomley34bb61f2005-09-06 16:56:51 -07002787
2788 get_device(dev);
2789}
2790
2791static void klist_children_put(struct klist_node *n)
2792{
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08002793 struct device_private *p = to_device_private_parent(n);
2794 struct device *dev = p->device;
James Bottomley34bb61f2005-09-06 16:56:51 -07002795
2796 put_device(dev);
2797}
2798
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002800 * device_initialize - init device structure.
2801 * @dev: device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802 *
Cornelia Huck57394112008-09-03 18:26:40 +02002803 * This prepares the device for use by other layers by initializing
2804 * its fields.
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002805 * It is the first half of device_register(), if called by
Cornelia Huck57394112008-09-03 18:26:40 +02002806 * that function, though it can also be called separately, so one
2807 * may use @dev's fields. In particular, get_device()/put_device()
2808 * may be used for reference counting of @dev after calling this
2809 * function.
2810 *
Alan Sternb10d5ef2012-01-17 11:39:00 -05002811 * All fields in @dev must be initialized by the caller to 0, except
2812 * for those explicitly set to some other value. The simplest
2813 * approach is to use kzalloc() to allocate the structure containing
2814 * @dev.
2815 *
Cornelia Huck57394112008-09-03 18:26:40 +02002816 * NOTE: Use put_device() to give up your reference instead of freeing
2817 * @dev directly once you have called this function.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819void device_initialize(struct device *dev)
2820{
Greg Kroah-Hartman881c6cfd2007-11-01 09:29:06 -06002821 dev->kobj.kset = devices_kset;
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -07002822 kobject_init(&dev->kobj, &device_ktype);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823 INIT_LIST_HEAD(&dev->dma_pools);
Thomas Gleixner31427882010-01-29 20:39:02 +00002824 mutex_init(&dev->mutex);
Dan Williams87a30e12019-07-17 18:08:26 -07002825#ifdef CONFIG_PROVE_LOCKING
2826 mutex_init(&dev->lockdep_mutex);
2827#endif
Peter Zijlstra1704f472010-03-19 01:37:42 +01002828 lockdep_set_novalidate_class(&dev->mutex);
Tejun Heo9ac78492007-01-20 16:00:26 +09002829 spin_lock_init(&dev->devres_lock);
2830 INIT_LIST_HEAD(&dev->devres_head);
Alan Stern3b98aea2008-08-07 13:06:12 -04002831 device_pm_init(dev);
Christoph Hellwig87348132006-12-06 20:32:33 -08002832 set_dev_node(dev, -1);
Jiang Liu4a7cc832015-07-09 16:00:44 +08002833#ifdef CONFIG_GENERIC_MSI_IRQ
2834 INIT_LIST_HEAD(&dev->msi_list);
2835#endif
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01002836 INIT_LIST_HEAD(&dev->links.consumers);
2837 INIT_LIST_HEAD(&dev->links.suppliers);
Saravana Kannan3b052a32020-11-20 18:02:17 -08002838 INIT_LIST_HEAD(&dev->links.defer_sync);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01002839 dev->links.status = DL_DEV_NO_DRIVER;
Christoph Hellwig6d4e9a82021-02-10 10:56:39 +01002840#if defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_DEVICE) || \
2841 defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU) || \
2842 defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU_ALL)
2843 dev->dma_coherent = dma_default_coherent;
2844#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845}
David Graham White86df2682013-07-21 20:41:14 -04002846EXPORT_SYMBOL_GPL(device_initialize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847
Tejun Heod73ce002013-03-12 11:30:05 -07002848struct kobject *virtual_device_parent(struct device *dev)
Greg Kroah-Hartmanf0ee61a2006-10-23 10:40:54 -07002849{
Kay Sievers86406242007-03-14 03:25:56 +01002850 static struct kobject *virtual_dir = NULL;
Greg Kroah-Hartmanf0ee61a2006-10-23 10:40:54 -07002851
Kay Sievers86406242007-03-14 03:25:56 +01002852 if (!virtual_dir)
Greg Kroah-Hartman4ff6abf2007-11-05 22:24:43 -08002853 virtual_dir = kobject_create_and_add("virtual",
Greg Kroah-Hartman881c6cfd2007-11-01 09:29:06 -06002854 &devices_kset->kobj);
Greg Kroah-Hartmanf0ee61a2006-10-23 10:40:54 -07002855
Kay Sievers86406242007-03-14 03:25:56 +01002856 return virtual_dir;
Greg Kroah-Hartmanf0ee61a2006-10-23 10:40:54 -07002857}
2858
Eric W. Biedermanbc451f22010-03-30 11:31:25 -07002859struct class_dir {
2860 struct kobject kobj;
2861 struct class *class;
2862};
2863
2864#define to_class_dir(obj) container_of(obj, struct class_dir, kobj)
2865
2866static void class_dir_release(struct kobject *kobj)
2867{
2868 struct class_dir *dir = to_class_dir(kobj);
2869 kfree(dir);
2870}
2871
2872static const
2873struct kobj_ns_type_operations *class_dir_child_ns_type(struct kobject *kobj)
2874{
2875 struct class_dir *dir = to_class_dir(kobj);
2876 return dir->class->ns_type;
2877}
2878
2879static struct kobj_type class_dir_ktype = {
2880 .release = class_dir_release,
2881 .sysfs_ops = &kobj_sysfs_ops,
2882 .child_ns_type = class_dir_child_ns_type
2883};
2884
2885static struct kobject *
2886class_dir_create_and_add(struct class *class, struct kobject *parent_kobj)
2887{
2888 struct class_dir *dir;
2889 int retval;
2890
2891 dir = kzalloc(sizeof(*dir), GFP_KERNEL);
2892 if (!dir)
Tetsuo Handa84d0c272018-05-07 19:10:31 +09002893 return ERR_PTR(-ENOMEM);
Eric W. Biedermanbc451f22010-03-30 11:31:25 -07002894
2895 dir->class = class;
2896 kobject_init(&dir->kobj, &class_dir_ktype);
2897
Kay Sievers6b6e39a2010-11-15 23:13:18 +01002898 dir->kobj.kset = &class->p->glue_dirs;
Eric W. Biedermanbc451f22010-03-30 11:31:25 -07002899
2900 retval = kobject_add(&dir->kobj, parent_kobj, "%s", class->name);
2901 if (retval < 0) {
2902 kobject_put(&dir->kobj);
Tetsuo Handa84d0c272018-05-07 19:10:31 +09002903 return ERR_PTR(retval);
Eric W. Biedermanbc451f22010-03-30 11:31:25 -07002904 }
2905 return &dir->kobj;
2906}
2907
Yijing Wange4a60d12014-11-07 12:05:49 +08002908static DEFINE_MUTEX(gdp_mutex);
Eric W. Biedermanbc451f22010-03-30 11:31:25 -07002909
Kay Sieversda231fd2007-11-21 17:29:15 +01002910static struct kobject *get_device_parent(struct device *dev,
2911 struct device *parent)
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +01002912{
Kay Sievers86406242007-03-14 03:25:56 +01002913 if (dev->class) {
2914 struct kobject *kobj = NULL;
2915 struct kobject *parent_kobj;
2916 struct kobject *k;
2917
Randy Dunlapead454f2010-09-24 14:36:49 -07002918#ifdef CONFIG_BLOCK
Kay Sievers39aba962010-09-04 22:33:14 -07002919 /* block disks show up in /sys/block */
Andi Kleene52eec12010-09-08 16:54:17 +02002920 if (sysfs_deprecated && dev->class == &block_class) {
Kay Sievers39aba962010-09-04 22:33:14 -07002921 if (parent && parent->class == &block_class)
2922 return &parent->kobj;
Kay Sievers6b6e39a2010-11-15 23:13:18 +01002923 return &block_class.p->subsys.kobj;
Kay Sievers39aba962010-09-04 22:33:14 -07002924 }
Randy Dunlapead454f2010-09-24 14:36:49 -07002925#endif
Andi Kleene52eec12010-09-08 16:54:17 +02002926
Kay Sievers86406242007-03-14 03:25:56 +01002927 /*
2928 * If we have no parent, we live in "virtual".
Kay Sievers0f4dafc2007-12-19 01:40:42 +01002929 * Class-devices with a non class-device as parent, live
2930 * in a "glue" directory to prevent namespace collisions.
Kay Sievers86406242007-03-14 03:25:56 +01002931 */
2932 if (parent == NULL)
2933 parent_kobj = virtual_device_parent(dev);
Eric W. Biederman24b14422010-07-24 22:43:35 -07002934 else if (parent->class && !dev->class->ns_type)
Kay Sievers86406242007-03-14 03:25:56 +01002935 return &parent->kobj;
2936 else
2937 parent_kobj = &parent->kobj;
2938
Tejun Heo77d3d7c2010-02-05 17:57:02 +09002939 mutex_lock(&gdp_mutex);
2940
Kay Sievers86406242007-03-14 03:25:56 +01002941 /* find our class-directory at the parent and reference it */
Kay Sievers6b6e39a2010-11-15 23:13:18 +01002942 spin_lock(&dev->class->p->glue_dirs.list_lock);
2943 list_for_each_entry(k, &dev->class->p->glue_dirs.list, entry)
Kay Sievers86406242007-03-14 03:25:56 +01002944 if (k->parent == parent_kobj) {
2945 kobj = kobject_get(k);
2946 break;
2947 }
Kay Sievers6b6e39a2010-11-15 23:13:18 +01002948 spin_unlock(&dev->class->p->glue_dirs.list_lock);
Tejun Heo77d3d7c2010-02-05 17:57:02 +09002949 if (kobj) {
2950 mutex_unlock(&gdp_mutex);
Kay Sievers86406242007-03-14 03:25:56 +01002951 return kobj;
Tejun Heo77d3d7c2010-02-05 17:57:02 +09002952 }
Kay Sievers86406242007-03-14 03:25:56 +01002953
2954 /* or create a new class-directory at the parent device */
Eric W. Biedermanbc451f22010-03-30 11:31:25 -07002955 k = class_dir_create_and_add(dev->class, parent_kobj);
Kay Sievers0f4dafc2007-12-19 01:40:42 +01002956 /* do not emit an uevent for this simple "glue" directory */
Tejun Heo77d3d7c2010-02-05 17:57:02 +09002957 mutex_unlock(&gdp_mutex);
Greg Kroah-Hartman43968d22007-11-05 22:24:43 -08002958 return k;
Kay Sievers86406242007-03-14 03:25:56 +01002959 }
2960
Kay Sieversca22e562011-12-14 14:29:38 -08002961 /* subsystems can specify a default root directory for their devices */
2962 if (!parent && dev->bus && dev->bus->dev_root)
2963 return &dev->bus->dev_root->kobj;
2964
Kay Sievers86406242007-03-14 03:25:56 +01002965 if (parent)
Cornelia Huckc744aeae2007-01-08 20:16:44 +01002966 return &parent->kobj;
2967 return NULL;
2968}
Kay Sieversda231fd2007-11-21 17:29:15 +01002969
Ming Leicebf8fd2016-07-10 19:27:36 +08002970static inline bool live_in_glue_dir(struct kobject *kobj,
2971 struct device *dev)
2972{
2973 if (!kobj || !dev->class ||
2974 kobj->kset != &dev->class->p->glue_dirs)
2975 return false;
2976 return true;
2977}
2978
2979static inline struct kobject *get_glue_dir(struct device *dev)
2980{
2981 return dev->kobj.parent;
2982}
2983
2984/*
2985 * make sure cleaning up dir as the last step, we need to make
2986 * sure .release handler of kobject is run with holding the
2987 * global lock
2988 */
Cornelia Huck63b69712008-01-21 16:09:44 +01002989static void cleanup_glue_dir(struct device *dev, struct kobject *glue_dir)
Kay Sieversda231fd2007-11-21 17:29:15 +01002990{
Muchun Songac434322019-07-27 11:21:22 +08002991 unsigned int ref;
2992
Kay Sievers0f4dafc2007-12-19 01:40:42 +01002993 /* see if we live in a "glue" directory */
Ming Leicebf8fd2016-07-10 19:27:36 +08002994 if (!live_in_glue_dir(glue_dir, dev))
Kay Sieversda231fd2007-11-21 17:29:15 +01002995 return;
2996
Yijing Wange4a60d12014-11-07 12:05:49 +08002997 mutex_lock(&gdp_mutex);
Muchun Songac434322019-07-27 11:21:22 +08002998 /**
2999 * There is a race condition between removing glue directory
3000 * and adding a new device under the glue directory.
3001 *
3002 * CPU1: CPU2:
3003 *
3004 * device_add()
3005 * get_device_parent()
3006 * class_dir_create_and_add()
3007 * kobject_add_internal()
3008 * create_dir() // create glue_dir
3009 *
3010 * device_add()
3011 * get_device_parent()
3012 * kobject_get() // get glue_dir
3013 *
3014 * device_del()
3015 * cleanup_glue_dir()
3016 * kobject_del(glue_dir)
3017 *
3018 * kobject_add()
3019 * kobject_add_internal()
3020 * create_dir() // in glue_dir
3021 * sysfs_create_dir_ns()
3022 * kernfs_create_dir_ns(sd)
3023 *
3024 * sysfs_remove_dir() // glue_dir->sd=NULL
3025 * sysfs_put() // free glue_dir->sd
3026 *
3027 * // sd is freed
3028 * kernfs_new_node(sd)
3029 * kernfs_get(glue_dir)
3030 * kernfs_add_one()
3031 * kernfs_put()
3032 *
3033 * Before CPU1 remove last child device under glue dir, if CPU2 add
3034 * a new device under glue dir, the glue_dir kobject reference count
3035 * will be increase to 2 in kobject_get(k). And CPU2 has been called
3036 * kernfs_create_dir_ns(). Meanwhile, CPU1 call sysfs_remove_dir()
3037 * and sysfs_put(). This result in glue_dir->sd is freed.
3038 *
3039 * Then the CPU2 will see a stale "empty" but still potentially used
3040 * glue dir around in kernfs_new_node().
3041 *
3042 * In order to avoid this happening, we also should make sure that
3043 * kernfs_node for glue_dir is released in CPU1 only when refcount
3044 * for glue_dir kobj is 1.
3045 */
3046 ref = kref_read(&glue_dir->kref);
3047 if (!kobject_has_children(glue_dir) && !--ref)
Benjamin Herrenschmidt726e4102018-07-10 10:29:10 +10003048 kobject_del(glue_dir);
Kay Sievers0f4dafc2007-12-19 01:40:42 +01003049 kobject_put(glue_dir);
Yijing Wange4a60d12014-11-07 12:05:49 +08003050 mutex_unlock(&gdp_mutex);
Kay Sieversda231fd2007-11-21 17:29:15 +01003051}
Cornelia Huck63b69712008-01-21 16:09:44 +01003052
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07003053static int device_add_class_symlinks(struct device *dev)
3054{
Benjamin Herrenschmidt5590f312015-02-18 11:25:18 +11003055 struct device_node *of_node = dev_of_node(dev);
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07003056 int error;
3057
Benjamin Herrenschmidt5590f312015-02-18 11:25:18 +11003058 if (of_node) {
Rob Herring0c3c2342017-10-04 14:04:01 -05003059 error = sysfs_create_link(&dev->kobj, of_node_kobj(of_node), "of_node");
Benjamin Herrenschmidt5590f312015-02-18 11:25:18 +11003060 if (error)
3061 dev_warn(dev, "Error %d creating of_node link\n",error);
3062 /* An error here doesn't warrant bringing down the device */
3063 }
3064
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07003065 if (!dev->class)
3066 return 0;
Kay Sieversda231fd2007-11-21 17:29:15 +01003067
Greg Kroah-Hartman1fbfee62008-05-28 09:28:39 -07003068 error = sysfs_create_link(&dev->kobj,
Kay Sievers6b6e39a2010-11-15 23:13:18 +01003069 &dev->class->p->subsys.kobj,
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07003070 "subsystem");
3071 if (error)
Benjamin Herrenschmidt5590f312015-02-18 11:25:18 +11003072 goto out_devnode;
Kay Sieversda231fd2007-11-21 17:29:15 +01003073
Greg Kroah-Hartman4e886c22008-01-27 12:12:43 -08003074 if (dev->parent && device_is_not_partition(dev)) {
Dmitry Torokhov4f01a752007-09-18 22:46:50 -07003075 error = sysfs_create_link(&dev->kobj, &dev->parent->kobj,
3076 "device");
3077 if (error)
Kay Sievers39aba962010-09-04 22:33:14 -07003078 goto out_subsys;
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07003079 }
Kay Sievers39aba962010-09-04 22:33:14 -07003080
Randy Dunlapead454f2010-09-24 14:36:49 -07003081#ifdef CONFIG_BLOCK
Kay Sievers39aba962010-09-04 22:33:14 -07003082 /* /sys/block has directories and does not need symlinks */
Andi Kleene52eec12010-09-08 16:54:17 +02003083 if (sysfs_deprecated && dev->class == &block_class)
Kay Sievers39aba962010-09-04 22:33:14 -07003084 return 0;
Randy Dunlapead454f2010-09-24 14:36:49 -07003085#endif
Kay Sievers39aba962010-09-04 22:33:14 -07003086
3087 /* link in the class directory pointing to the device */
Kay Sievers6b6e39a2010-11-15 23:13:18 +01003088 error = sysfs_create_link(&dev->class->p->subsys.kobj,
Kay Sievers39aba962010-09-04 22:33:14 -07003089 &dev->kobj, dev_name(dev));
3090 if (error)
3091 goto out_device;
3092
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07003093 return 0;
3094
Kay Sievers39aba962010-09-04 22:33:14 -07003095out_device:
3096 sysfs_remove_link(&dev->kobj, "device");
Kay Sieversda231fd2007-11-21 17:29:15 +01003097
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07003098out_subsys:
3099 sysfs_remove_link(&dev->kobj, "subsystem");
Benjamin Herrenschmidt5590f312015-02-18 11:25:18 +11003100out_devnode:
3101 sysfs_remove_link(&dev->kobj, "of_node");
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07003102 return error;
3103}
3104
3105static void device_remove_class_symlinks(struct device *dev)
3106{
Benjamin Herrenschmidt5590f312015-02-18 11:25:18 +11003107 if (dev_of_node(dev))
3108 sysfs_remove_link(&dev->kobj, "of_node");
3109
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07003110 if (!dev->class)
3111 return;
Kay Sieversda231fd2007-11-21 17:29:15 +01003112
Greg Kroah-Hartman4e886c22008-01-27 12:12:43 -08003113 if (dev->parent && device_is_not_partition(dev))
Kay Sieversda231fd2007-11-21 17:29:15 +01003114 sysfs_remove_link(&dev->kobj, "device");
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07003115 sysfs_remove_link(&dev->kobj, "subsystem");
Randy Dunlapead454f2010-09-24 14:36:49 -07003116#ifdef CONFIG_BLOCK
Andi Kleene52eec12010-09-08 16:54:17 +02003117 if (sysfs_deprecated && dev->class == &block_class)
Kay Sievers39aba962010-09-04 22:33:14 -07003118 return;
Randy Dunlapead454f2010-09-24 14:36:49 -07003119#endif
Kay Sievers6b6e39a2010-11-15 23:13:18 +01003120 sysfs_delete_link(&dev->class->p->subsys.kobj, &dev->kobj, dev_name(dev));
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07003121}
3122
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123/**
Stephen Rothwell413c2392008-05-30 10:16:40 +10003124 * dev_set_name - set a device name
3125 * @dev: device
Randy Dunlap46232362008-06-04 21:40:43 -07003126 * @fmt: format string for the device's name
Stephen Rothwell413c2392008-05-30 10:16:40 +10003127 */
3128int dev_set_name(struct device *dev, const char *fmt, ...)
3129{
3130 va_list vargs;
Kay Sievers1fa5ae82009-01-25 15:17:37 +01003131 int err;
Stephen Rothwell413c2392008-05-30 10:16:40 +10003132
3133 va_start(vargs, fmt);
Kay Sievers1fa5ae82009-01-25 15:17:37 +01003134 err = kobject_set_name_vargs(&dev->kobj, fmt, vargs);
Stephen Rothwell413c2392008-05-30 10:16:40 +10003135 va_end(vargs);
Kay Sievers1fa5ae82009-01-25 15:17:37 +01003136 return err;
Stephen Rothwell413c2392008-05-30 10:16:40 +10003137}
3138EXPORT_SYMBOL_GPL(dev_set_name);
3139
3140/**
Dan Williamse105b8b2008-04-21 10:51:07 -07003141 * device_to_dev_kobj - select a /sys/dev/ directory for the device
3142 * @dev: device
3143 *
3144 * By default we select char/ for new entries. Setting class->dev_obj
3145 * to NULL prevents an entry from being created. class->dev_kobj must
3146 * be set (or cleared) before any devices are registered to the class
3147 * otherwise device_create_sys_dev_entry() and
Peter Korsgaard0d4e293c2012-04-17 12:12:57 +02003148 * device_remove_sys_dev_entry() will disagree about the presence of
3149 * the link.
Dan Williamse105b8b2008-04-21 10:51:07 -07003150 */
3151static struct kobject *device_to_dev_kobj(struct device *dev)
3152{
3153 struct kobject *kobj;
3154
3155 if (dev->class)
3156 kobj = dev->class->dev_kobj;
3157 else
3158 kobj = sysfs_dev_char_kobj;
3159
3160 return kobj;
3161}
3162
3163static int device_create_sys_dev_entry(struct device *dev)
3164{
3165 struct kobject *kobj = device_to_dev_kobj(dev);
3166 int error = 0;
3167 char devt_str[15];
3168
3169 if (kobj) {
3170 format_dev_t(devt_str, dev->devt);
3171 error = sysfs_create_link(kobj, &dev->kobj, devt_str);
3172 }
3173
3174 return error;
3175}
3176
3177static void device_remove_sys_dev_entry(struct device *dev)
3178{
3179 struct kobject *kobj = device_to_dev_kobj(dev);
3180 char devt_str[15];
3181
3182 if (kobj) {
3183 format_dev_t(devt_str, dev->devt);
3184 sysfs_remove_link(kobj, devt_str);
3185 }
3186}
3187
Shaokun Zhang46d3a032018-07-15 18:08:56 +08003188static int device_private_init(struct device *dev)
Greg Kroah-Hartmanb4028432009-05-11 14:16:57 -07003189{
3190 dev->p = kzalloc(sizeof(*dev->p), GFP_KERNEL);
3191 if (!dev->p)
3192 return -ENOMEM;
3193 dev->p->device = dev;
3194 klist_init(&dev->p->klist_children, klist_children_get,
3195 klist_children_put);
Greg Kroah-Hartmanef8a3fd2012-03-08 12:17:22 -08003196 INIT_LIST_HEAD(&dev->p->deferred_probe);
Greg Kroah-Hartmanb4028432009-05-11 14:16:57 -07003197 return 0;
3198}
3199
Dan Williamse105b8b2008-04-21 10:51:07 -07003200/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08003201 * device_add - add device to device hierarchy.
3202 * @dev: device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08003204 * This is part 2 of device_register(), though may be called
3205 * separately _iff_ device_initialize() has been called separately.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206 *
Cornelia Huck57394112008-09-03 18:26:40 +02003207 * This adds @dev to the kobject hierarchy via kobject_add(), adds it
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08003208 * to the global and sibling lists for the device, then
3209 * adds it to the other relevant subsystems of the driver model.
Cornelia Huck57394112008-09-03 18:26:40 +02003210 *
Alan Sternb10d5ef2012-01-17 11:39:00 -05003211 * Do not call this routine or device_register() more than once for
3212 * any device structure. The driver model core is not designed to work
3213 * with devices that get unregistered and then spring back to life.
3214 * (Among other things, it's very hard to guarantee that all references
3215 * to the previous incarnation of @dev have been dropped.) Allocate
3216 * and register a fresh new struct device instead.
3217 *
Cornelia Huck57394112008-09-03 18:26:40 +02003218 * NOTE: _Never_ directly free @dev after calling this function, even
3219 * if it returned an error! Always use put_device() to give up your
3220 * reference instead.
Borislav Petkovaffada72019-04-18 19:41:56 +02003221 *
3222 * Rule of thumb is: if device_add() succeeds, you should call
3223 * device_del() when you want to get rid of it. If device_add() has
3224 * *not* succeeded, use *only* put_device() to drop the reference
3225 * count.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003226 */
3227int device_add(struct device *dev)
3228{
Viresh Kumar35dbf4e2017-03-17 12:24:22 +05303229 struct device *parent;
Kay Sieversca22e562011-12-14 14:29:38 -08003230 struct kobject *kobj;
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02003231 struct class_interface *class_intf;
Saravana Kannan5f5377e2020-05-14 22:34:58 -07003232 int error = -EINVAL;
Ming Leicebf8fd2016-07-10 19:27:36 +08003233 struct kobject *glue_dir = NULL;
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01003234
Linus Torvalds1da177e2005-04-16 15:20:36 -07003235 dev = get_device(dev);
Greg Kroah-Hartmanc906a482008-05-30 10:45:12 -07003236 if (!dev)
3237 goto done;
3238
Greg Kroah-Hartmanfb069a52008-12-16 12:23:36 -08003239 if (!dev->p) {
Greg Kroah-Hartmanb4028432009-05-11 14:16:57 -07003240 error = device_private_init(dev);
3241 if (error)
3242 goto done;
Greg Kroah-Hartmanfb069a52008-12-16 12:23:36 -08003243 }
Greg Kroah-Hartmanfb069a52008-12-16 12:23:36 -08003244
Kay Sievers1fa5ae82009-01-25 15:17:37 +01003245 /*
3246 * for statically allocated devices, which should all be converted
3247 * some day, we need to initialize the name. We prevent reading back
3248 * the name, and force the use of dev_name()
3249 */
3250 if (dev->init_name) {
Greg Kroah-Hartmanacc0e902009-06-02 15:39:55 -07003251 dev_set_name(dev, "%s", dev->init_name);
Kay Sievers1fa5ae82009-01-25 15:17:37 +01003252 dev->init_name = NULL;
3253 }
Greg Kroah-Hartmanc906a482008-05-30 10:45:12 -07003254
Kay Sieversca22e562011-12-14 14:29:38 -08003255 /* subsystems can specify simple device enumeration */
3256 if (!dev_name(dev) && dev->bus && dev->bus->dev_name)
3257 dev_set_name(dev, "%s%u", dev->bus->dev_name, dev->id);
3258
Thomas Gleixnere6309e72009-12-10 19:32:49 +00003259 if (!dev_name(dev)) {
3260 error = -EINVAL;
Kay Sievers5c8563d2009-05-28 14:24:07 -07003261 goto name_error;
Thomas Gleixnere6309e72009-12-10 19:32:49 +00003262 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01003264 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
Greg Kroah-Hartmanc205ef42006-08-07 22:19:37 -07003265
Linus Torvalds1da177e2005-04-16 15:20:36 -07003266 parent = get_device(dev->parent);
Kay Sieversca22e562011-12-14 14:29:38 -08003267 kobj = get_device_parent(dev, parent);
Tetsuo Handa84d0c272018-05-07 19:10:31 +09003268 if (IS_ERR(kobj)) {
3269 error = PTR_ERR(kobj);
3270 goto parent_error;
3271 }
Kay Sieversca22e562011-12-14 14:29:38 -08003272 if (kobj)
3273 dev->kobj.parent = kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003274
Yinghai Lu0d358f22008-02-19 03:20:41 -08003275 /* use parent numa_node */
Zhen Lei56f2de82015-08-25 12:08:22 +08003276 if (parent && (dev_to_node(dev) == NUMA_NO_NODE))
Yinghai Lu0d358f22008-02-19 03:20:41 -08003277 set_dev_node(dev, dev_to_node(parent));
3278
Linus Torvalds1da177e2005-04-16 15:20:36 -07003279 /* first, register with generic layer. */
Kay Sievers8a577ff2009-04-18 15:05:45 -07003280 /* we require the name to be set before, and pass NULL */
3281 error = kobject_add(&dev->kobj, dev->kobj.parent, NULL);
Ming Leicebf8fd2016-07-10 19:27:36 +08003282 if (error) {
3283 glue_dir = get_glue_dir(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003284 goto Error;
Ming Leicebf8fd2016-07-10 19:27:36 +08003285 }
Kay Sieversa7fd6702005-10-01 14:49:43 +02003286
Brian Walsh37022642006-08-14 22:43:19 -07003287 /* notify platform of device entry */
Heikki Krogerus07de0e82018-11-09 17:21:34 +03003288 error = device_platform_notify(dev, KOBJ_ADD);
3289 if (error)
3290 goto platform_error;
Brian Walsh37022642006-08-14 22:43:19 -07003291
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07003292 error = device_create_file(dev, &dev_attr_uevent);
Cornelia Hucka306eea2006-09-22 11:37:13 +02003293 if (error)
3294 goto attrError;
Kay Sieversa7fd6702005-10-01 14:49:43 +02003295
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07003296 error = device_add_class_symlinks(dev);
3297 if (error)
3298 goto SymlinkError;
Cornelia Huckdc0afa82007-07-09 11:39:18 -07003299 error = device_add_attrs(dev);
3300 if (error)
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07003301 goto AttrsError;
Cornelia Huckdc0afa82007-07-09 11:39:18 -07003302 error = bus_add_device(dev);
3303 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003304 goto BusError;
Alan Stern3b98aea2008-08-07 13:06:12 -04003305 error = dpm_sysfs_add(dev);
Rafael J. Wysocki57eee3d2008-03-12 00:59:38 +01003306 if (error)
Alan Stern3b98aea2008-08-07 13:06:12 -04003307 goto DPMError;
3308 device_pm_add(dev);
Alan Sternec0676ee2008-12-05 14:10:31 -05003309
Sergey Klyaus0cd75042014-10-08 11:31:54 +04003310 if (MAJOR(dev->devt)) {
3311 error = device_create_file(dev, &dev_attr_dev);
3312 if (error)
3313 goto DevAttrError;
3314
3315 error = device_create_sys_dev_entry(dev);
3316 if (error)
3317 goto SysEntryError;
3318
3319 devtmpfs_create_node(dev);
3320 }
3321
Alan Sternec0676ee2008-12-05 14:10:31 -05003322 /* Notify clients of device addition. This call must come
majianpeng268863f2012-01-11 15:12:06 +00003323 * after dpm_sysfs_add() and before kobject_uevent().
Alan Sternec0676ee2008-12-05 14:10:31 -05003324 */
3325 if (dev->bus)
3326 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
3327 BUS_NOTIFY_ADD_DEVICE, dev);
3328
Cornelia Huck83b5fb4c2007-03-29 11:12:11 +02003329 kobject_uevent(&dev->kobj, KOBJ_ADD);
Saravana Kannan372a67c2019-09-04 14:11:20 -07003330
Saravana Kannane2ae9bc2019-09-04 14:11:21 -07003331 /*
3332 * Check if any of the other devices (consumers) have been waiting for
3333 * this device (supplier) to be added so that they can create a device
3334 * link to it.
3335 *
3336 * This needs to happen after device_pm_add() because device_link_add()
3337 * requires the supplier be registered before it's called.
3338 *
Saravana Kannan2cd38fd2020-05-19 20:48:21 -07003339 * But this also needs to happen before bus_probe_device() to make sure
Saravana Kannane2ae9bc2019-09-04 14:11:21 -07003340 * waiting consumers can link to it before the driver is bound to the
3341 * device and the driver sync_state callback is called for this device.
3342 */
Saravana Kannan2cd38fd2020-05-19 20:48:21 -07003343 if (dev->fwnode && !dev->fwnode->dev) {
3344 dev->fwnode->dev = dev;
Saravana Kannan5f5377e2020-05-14 22:34:58 -07003345 fw_devlink_link_device(dev);
Saravana Kannan03324502019-10-28 15:00:24 -07003346 }
Saravana Kannane2ae9bc2019-09-04 14:11:21 -07003347
Alan Stern2023c612009-07-30 15:27:18 -04003348 bus_probe_device(dev);
Saravana Kannand46f3e32021-04-01 21:03:41 -07003349
3350 /*
3351 * If all driver registration is done and a newly added device doesn't
3352 * match with any driver, don't block its consumers from probing in
3353 * case the consumer device is able to operate without this supplier.
3354 */
3355 if (dev->fwnode && fw_devlink_drv_reg_done && !dev->can_match)
3356 fw_devlink_unblock_consumers(dev);
3357
Linus Torvalds1da177e2005-04-16 15:20:36 -07003358 if (parent)
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08003359 klist_add_tail(&dev->p->knode_parent,
3360 &parent->p->klist_children);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003361
Greg Kroah-Hartman5d9fd162006-06-22 17:17:32 -07003362 if (dev->class) {
Kay Sieversca22e562011-12-14 14:29:38 -08003363 mutex_lock(&dev->class->p->mutex);
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02003364 /* tie the class to the device */
Wei Yang570d0202019-01-18 10:34:59 +08003365 klist_add_tail(&dev->p->knode_class,
Kay Sievers6b6e39a2010-11-15 23:13:18 +01003366 &dev->class->p->klist_devices);
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02003367
3368 /* notify any interfaces that the device is here */
Greg Kroah-Hartman184f1f72008-05-28 09:28:39 -07003369 list_for_each_entry(class_intf,
Kay Sieversca22e562011-12-14 14:29:38 -08003370 &dev->class->p->interfaces, node)
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02003371 if (class_intf->add_dev)
3372 class_intf->add_dev(dev, class_intf);
Kay Sieversca22e562011-12-14 14:29:38 -08003373 mutex_unlock(&dev->class->p->mutex);
Greg Kroah-Hartman5d9fd162006-06-22 17:17:32 -07003374 }
Greg Kroah-Hartmanc906a482008-05-30 10:45:12 -07003375done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003376 put_device(dev);
3377 return error;
Sergey Klyaus0cd75042014-10-08 11:31:54 +04003378 SysEntryError:
3379 if (MAJOR(dev->devt))
3380 device_remove_file(dev, &dev_attr_dev);
3381 DevAttrError:
3382 device_pm_remove(dev);
3383 dpm_sysfs_remove(dev);
Alan Stern3b98aea2008-08-07 13:06:12 -04003384 DPMError:
Rafael J. Wysocki57eee3d2008-03-12 00:59:38 +01003385 bus_remove_device(dev);
3386 BusError:
James Simmons82f0cf92007-02-21 17:44:51 +00003387 device_remove_attrs(dev);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07003388 AttrsError:
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07003389 device_remove_class_symlinks(dev);
3390 SymlinkError:
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07003391 device_remove_file(dev, &dev_attr_uevent);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07003392 attrError:
Heikki Krogerus07de0e82018-11-09 17:21:34 +03003393 device_platform_notify(dev, KOBJ_REMOVE);
3394platform_error:
Kay Sievers312c0042005-11-16 09:00:00 +01003395 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
Ming Leicebf8fd2016-07-10 19:27:36 +08003396 glue_dir = get_glue_dir(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003397 kobject_del(&dev->kobj);
3398 Error:
Ming Leicebf8fd2016-07-10 19:27:36 +08003399 cleanup_glue_dir(dev, glue_dir);
Tetsuo Handa84d0c272018-05-07 19:10:31 +09003400parent_error:
Markus Elfring5f0163a2015-02-05 11:48:26 +01003401 put_device(parent);
Kay Sievers5c8563d2009-05-28 14:24:07 -07003402name_error:
3403 kfree(dev->p);
3404 dev->p = NULL;
Greg Kroah-Hartmanc906a482008-05-30 10:45:12 -07003405 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003406}
David Graham White86df2682013-07-21 20:41:14 -04003407EXPORT_SYMBOL_GPL(device_add);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003408
Linus Torvalds1da177e2005-04-16 15:20:36 -07003409/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08003410 * device_register - register a device with the system.
3411 * @dev: pointer to the device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07003412 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08003413 * This happens in two clean steps - initialize the device
3414 * and add it to the system. The two steps can be called
3415 * separately, but this is the easiest and most common.
3416 * I.e. you should only call the two helpers separately if
3417 * have a clearly defined need to use and refcount the device
3418 * before it is added to the hierarchy.
Cornelia Huck57394112008-09-03 18:26:40 +02003419 *
Alan Sternb10d5ef2012-01-17 11:39:00 -05003420 * For more information, see the kerneldoc for device_initialize()
3421 * and device_add().
3422 *
Cornelia Huck57394112008-09-03 18:26:40 +02003423 * NOTE: _Never_ directly free @dev after calling this function, even
3424 * if it returned an error! Always use put_device() to give up the
3425 * reference initialized in this function instead.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003426 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003427int device_register(struct device *dev)
3428{
3429 device_initialize(dev);
3430 return device_add(dev);
3431}
David Graham White86df2682013-07-21 20:41:14 -04003432EXPORT_SYMBOL_GPL(device_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003433
Linus Torvalds1da177e2005-04-16 15:20:36 -07003434/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08003435 * get_device - increment reference count for device.
3436 * @dev: device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003437 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08003438 * This simply forwards the call to kobject_get(), though
3439 * we do take care to provide for the case that we get a NULL
3440 * pointer passed in.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003441 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08003442struct device *get_device(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003443{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +02003444 return dev ? kobj_to_dev(kobject_get(&dev->kobj)) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003445}
David Graham White86df2682013-07-21 20:41:14 -04003446EXPORT_SYMBOL_GPL(get_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003447
Linus Torvalds1da177e2005-04-16 15:20:36 -07003448/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08003449 * put_device - decrement reference count.
3450 * @dev: device in question.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003451 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08003452void put_device(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003453{
Kay Sieversedfaa7c2007-05-21 22:08:01 +02003454 /* might_sleep(); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003455 if (dev)
3456 kobject_put(&dev->kobj);
3457}
David Graham White86df2682013-07-21 20:41:14 -04003458EXPORT_SYMBOL_GPL(put_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003459
Dan Williams00289cd2019-07-17 18:07:53 -07003460bool kill_device(struct device *dev)
3461{
3462 /*
3463 * Require the device lock and set the "dead" flag to guarantee that
3464 * the update behavior is consistent with the other bitfields near
3465 * it and that we cannot have an asynchronous probe routine trying
3466 * to run while we are tearing out the bus/class/sysfs from
3467 * underneath the device.
3468 */
3469 lockdep_assert_held(&dev->mutex);
3470
3471 if (dev->p->dead)
3472 return false;
3473 dev->p->dead = true;
3474 return true;
3475}
3476EXPORT_SYMBOL_GPL(kill_device);
3477
Linus Torvalds1da177e2005-04-16 15:20:36 -07003478/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08003479 * device_del - delete device from system.
3480 * @dev: device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003481 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08003482 * This is the first part of the device unregistration
3483 * sequence. This removes the device from the lists we control
3484 * from here, has it removed from the other driver model
3485 * subsystems it was added to in device_add(), and removes it
3486 * from the kobject hierarchy.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003487 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08003488 * NOTE: this should be called manually _iff_ device_add() was
3489 * also called manually.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003490 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08003491void device_del(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003492{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08003493 struct device *parent = dev->parent;
Ming Leicebf8fd2016-07-10 19:27:36 +08003494 struct kobject *glue_dir = NULL;
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02003495 struct class_interface *class_intf;
Oliver Neukumb8530012020-09-16 21:15:44 +02003496 unsigned int noio_flag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003497
Alexander Duyck3451a492019-01-22 10:39:10 -08003498 device_lock(dev);
Dan Williams00289cd2019-07-17 18:07:53 -07003499 kill_device(dev);
Alexander Duyck3451a492019-01-22 10:39:10 -08003500 device_unlock(dev);
3501
Saravana Kannan372a67c2019-09-04 14:11:20 -07003502 if (dev->fwnode && dev->fwnode->dev == dev)
3503 dev->fwnode->dev = NULL;
3504
Alan Sternec0676ee2008-12-05 14:10:31 -05003505 /* Notify clients of device removal. This call must come
3506 * before dpm_sysfs_remove().
3507 */
Oliver Neukumb8530012020-09-16 21:15:44 +02003508 noio_flag = memalloc_noio_save();
Alan Sternec0676ee2008-12-05 14:10:31 -05003509 if (dev->bus)
3510 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
3511 BUS_NOTIFY_DEL_DEVICE, dev);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01003512
Alan Stern3b98aea2008-08-07 13:06:12 -04003513 dpm_sysfs_remove(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003514 if (parent)
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08003515 klist_del(&dev->p->knode_parent);
Dan Williamse105b8b2008-04-21 10:51:07 -07003516 if (MAJOR(dev->devt)) {
Kay Sievers2b2af542009-04-30 15:23:42 +02003517 devtmpfs_delete_node(dev);
Dan Williamse105b8b2008-04-21 10:51:07 -07003518 device_remove_sys_dev_entry(dev);
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07003519 device_remove_file(dev, &dev_attr_dev);
Dan Williamse105b8b2008-04-21 10:51:07 -07003520 }
Kay Sieversb9d9c822006-06-15 15:31:56 +02003521 if (dev->class) {
Kay Sieversda231fd2007-11-21 17:29:15 +01003522 device_remove_class_symlinks(dev);
Kay Sievers99ef3ef2006-09-14 11:23:28 +02003523
Kay Sieversca22e562011-12-14 14:29:38 -08003524 mutex_lock(&dev->class->p->mutex);
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02003525 /* notify any interfaces that the device is now gone */
Greg Kroah-Hartman184f1f72008-05-28 09:28:39 -07003526 list_for_each_entry(class_intf,
Kay Sieversca22e562011-12-14 14:29:38 -08003527 &dev->class->p->interfaces, node)
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02003528 if (class_intf->remove_dev)
3529 class_intf->remove_dev(dev, class_intf);
3530 /* remove the device from the class list */
Wei Yang570d0202019-01-18 10:34:59 +08003531 klist_del(&dev->p->knode_class);
Kay Sieversca22e562011-12-14 14:29:38 -08003532 mutex_unlock(&dev->class->p->mutex);
Kay Sieversb9d9c822006-06-15 15:31:56 +02003533 }
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07003534 device_remove_file(dev, &dev_attr_uevent);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07003535 device_remove_attrs(dev);
Benjamin Herrenschmidt28953532006-11-08 19:46:14 -08003536 bus_remove_device(dev);
LongX Zhang4b6d1f122012-10-25 00:21:28 +02003537 device_pm_remove(dev);
Grant Likelyd1c34142012-03-05 08:47:41 -07003538 driver_deferred_probe_del(dev);
Heikki Krogerus07de0e82018-11-09 17:21:34 +03003539 device_platform_notify(dev, KOBJ_REMOVE);
Lukas Wunner478573c2016-07-28 02:25:41 +02003540 device_remove_properties(dev);
Jeffy Chen2ec16152017-10-20 20:01:01 +08003541 device_links_purge(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003542
Joerg Roedel599bad32014-09-30 13:02:02 +02003543 if (dev->bus)
3544 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
3545 BUS_NOTIFY_REMOVED_DEVICE, dev);
Kay Sievers312c0042005-11-16 09:00:00 +01003546 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
Ming Leicebf8fd2016-07-10 19:27:36 +08003547 glue_dir = get_glue_dir(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003548 kobject_del(&dev->kobj);
Ming Leicebf8fd2016-07-10 19:27:36 +08003549 cleanup_glue_dir(dev, glue_dir);
Oliver Neukumb8530012020-09-16 21:15:44 +02003550 memalloc_noio_restore(noio_flag);
Kay Sieversda231fd2007-11-21 17:29:15 +01003551 put_device(parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003552}
David Graham White86df2682013-07-21 20:41:14 -04003553EXPORT_SYMBOL_GPL(device_del);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003554
3555/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08003556 * device_unregister - unregister device from system.
3557 * @dev: device going away.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003558 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08003559 * We do this in two parts, like we do device_register(). First,
3560 * we remove it from all the subsystems with device_del(), then
3561 * we decrement the reference count via put_device(). If that
3562 * is the final reference count, the device will be cleaned up
3563 * via device_release() above. Otherwise, the structure will
3564 * stick around until the final reference to the device is dropped.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003565 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08003566void device_unregister(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003567{
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01003568 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003569 device_del(dev);
3570 put_device(dev);
3571}
David Graham White86df2682013-07-21 20:41:14 -04003572EXPORT_SYMBOL_GPL(device_unregister);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003573
Andy Shevchenko3d060ae2015-07-27 18:04:00 +03003574static struct device *prev_device(struct klist_iter *i)
3575{
3576 struct klist_node *n = klist_prev(i);
3577 struct device *dev = NULL;
3578 struct device_private *p;
3579
3580 if (n) {
3581 p = to_device_private_parent(n);
3582 dev = p->device;
3583 }
3584 return dev;
3585}
3586
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08003587static struct device *next_device(struct klist_iter *i)
mochel@digitalimplant.org36239572005-03-24 19:08:30 -08003588{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08003589 struct klist_node *n = klist_next(i);
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08003590 struct device *dev = NULL;
3591 struct device_private *p;
3592
3593 if (n) {
3594 p = to_device_private_parent(n);
3595 dev = p->device;
3596 }
3597 return dev;
mochel@digitalimplant.org36239572005-03-24 19:08:30 -08003598}
3599
Linus Torvalds1da177e2005-04-16 15:20:36 -07003600/**
Kay Sieverse454cea2009-09-18 23:01:12 +02003601 * device_get_devnode - path of device node file
Kay Sievers6fcf53a2009-04-30 15:23:42 +02003602 * @dev: device
Kay Sieverse454cea2009-09-18 23:01:12 +02003603 * @mode: returned file access mode
Kay Sievers3c2670e2013-04-06 09:56:00 -07003604 * @uid: returned file owner
3605 * @gid: returned file group
Kay Sievers6fcf53a2009-04-30 15:23:42 +02003606 * @tmp: possibly allocated string
3607 *
3608 * Return the relative path of a possible device node.
3609 * Non-default names may need to allocate a memory to compose
3610 * a name. This memory is returned in tmp and needs to be
3611 * freed by the caller.
3612 */
Kay Sieverse454cea2009-09-18 23:01:12 +02003613const char *device_get_devnode(struct device *dev,
Greg Kroah-Hartman4e4098a2013-04-11 11:43:29 -07003614 umode_t *mode, kuid_t *uid, kgid_t *gid,
Kay Sievers3c2670e2013-04-06 09:56:00 -07003615 const char **tmp)
Kay Sievers6fcf53a2009-04-30 15:23:42 +02003616{
3617 char *s;
3618
3619 *tmp = NULL;
3620
3621 /* the device type may provide a specific name */
Kay Sieverse454cea2009-09-18 23:01:12 +02003622 if (dev->type && dev->type->devnode)
Kay Sievers3c2670e2013-04-06 09:56:00 -07003623 *tmp = dev->type->devnode(dev, mode, uid, gid);
Kay Sievers6fcf53a2009-04-30 15:23:42 +02003624 if (*tmp)
3625 return *tmp;
3626
3627 /* the class may provide a specific name */
Kay Sieverse454cea2009-09-18 23:01:12 +02003628 if (dev->class && dev->class->devnode)
3629 *tmp = dev->class->devnode(dev, mode);
Kay Sievers6fcf53a2009-04-30 15:23:42 +02003630 if (*tmp)
3631 return *tmp;
3632
3633 /* return name without allocation, tmp == NULL */
3634 if (strchr(dev_name(dev), '!') == NULL)
3635 return dev_name(dev);
3636
3637 /* replace '!' in the name with '/' */
Rasmus Villemoesa29fd612015-06-25 15:02:33 -07003638 s = kstrdup(dev_name(dev), GFP_KERNEL);
3639 if (!s)
Kay Sievers6fcf53a2009-04-30 15:23:42 +02003640 return NULL;
Rasmus Villemoesa29fd612015-06-25 15:02:33 -07003641 strreplace(s, '!', '/');
3642 return *tmp = s;
Kay Sievers6fcf53a2009-04-30 15:23:42 +02003643}
3644
3645/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08003646 * device_for_each_child - device child iterator.
3647 * @parent: parent struct device.
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08003648 * @fn: function to be called for each device.
Robert P. J. Dayf8878dc2013-06-01 20:17:34 -04003649 * @data: data for the callback.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003650 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08003651 * Iterate over @parent's child devices, and call @fn for each,
3652 * passing it @data.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003653 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08003654 * We check the return of @fn each time. If it returns anything
3655 * other than 0, we break out and return that value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003656 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08003657int device_for_each_child(struct device *parent, void *data,
3658 int (*fn)(struct device *dev, void *data))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003659{
mochel@digitalimplant.org36239572005-03-24 19:08:30 -08003660 struct klist_iter i;
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08003661 struct device *child;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003662 int error = 0;
3663
Greg Kroah-Hartman014c90db2009-04-15 16:00:12 -07003664 if (!parent->p)
3665 return 0;
3666
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08003667 klist_iter_init(&parent->p->klist_children, &i);
Gimcuan Hui93ead7c2017-11-11 05:52:54 +00003668 while (!error && (child = next_device(&i)))
mochel@digitalimplant.org36239572005-03-24 19:08:30 -08003669 error = fn(child, data);
3670 klist_iter_exit(&i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003671 return error;
3672}
David Graham White86df2682013-07-21 20:41:14 -04003673EXPORT_SYMBOL_GPL(device_for_each_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003674
Cornelia Huck5ab69982006-11-16 15:42:07 +01003675/**
Andy Shevchenko3d060ae2015-07-27 18:04:00 +03003676 * device_for_each_child_reverse - device child iterator in reversed order.
3677 * @parent: parent struct device.
3678 * @fn: function to be called for each device.
3679 * @data: data for the callback.
3680 *
3681 * Iterate over @parent's child devices, and call @fn for each,
3682 * passing it @data.
3683 *
3684 * We check the return of @fn each time. If it returns anything
3685 * other than 0, we break out and return that value.
3686 */
3687int device_for_each_child_reverse(struct device *parent, void *data,
3688 int (*fn)(struct device *dev, void *data))
3689{
3690 struct klist_iter i;
3691 struct device *child;
3692 int error = 0;
3693
3694 if (!parent->p)
3695 return 0;
3696
3697 klist_iter_init(&parent->p->klist_children, &i);
3698 while ((child = prev_device(&i)) && !error)
3699 error = fn(child, data);
3700 klist_iter_exit(&i);
3701 return error;
3702}
3703EXPORT_SYMBOL_GPL(device_for_each_child_reverse);
3704
3705/**
Cornelia Huck5ab69982006-11-16 15:42:07 +01003706 * device_find_child - device iterator for locating a particular device.
3707 * @parent: parent struct device
Cornelia Huck5ab69982006-11-16 15:42:07 +01003708 * @match: Callback function to check device
Robert P. J. Dayf8878dc2013-06-01 20:17:34 -04003709 * @data: Data to pass to match function
Cornelia Huck5ab69982006-11-16 15:42:07 +01003710 *
3711 * This is similar to the device_for_each_child() function above, but it
3712 * returns a reference to a device that is 'found' for later use, as
3713 * determined by the @match callback.
3714 *
3715 * The callback should return 0 if the device doesn't match and non-zero
3716 * if it does. If the callback returns non-zero and a reference to the
3717 * current device can be obtained, this function will return to the caller
3718 * and not iterate over any more devices.
Federico Vagaa4e24002013-04-15 11:18:11 +02003719 *
3720 * NOTE: you will need to drop the reference with put_device() after use.
Cornelia Huck5ab69982006-11-16 15:42:07 +01003721 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08003722struct device *device_find_child(struct device *parent, void *data,
3723 int (*match)(struct device *dev, void *data))
Cornelia Huck5ab69982006-11-16 15:42:07 +01003724{
3725 struct klist_iter i;
3726 struct device *child;
3727
3728 if (!parent)
3729 return NULL;
3730
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08003731 klist_iter_init(&parent->p->klist_children, &i);
Cornelia Huck5ab69982006-11-16 15:42:07 +01003732 while ((child = next_device(&i)))
3733 if (match(child, data) && get_device(child))
3734 break;
3735 klist_iter_exit(&i);
3736 return child;
3737}
David Graham White86df2682013-07-21 20:41:14 -04003738EXPORT_SYMBOL_GPL(device_find_child);
Cornelia Huck5ab69982006-11-16 15:42:07 +01003739
Heikki Krogerusdad9bb02019-05-31 17:15:37 +03003740/**
3741 * device_find_child_by_name - device iterator for locating a child device.
3742 * @parent: parent struct device
3743 * @name: name of the child device
3744 *
3745 * This is similar to the device_find_child() function above, but it
3746 * returns a reference to a device that has the name @name.
3747 *
3748 * NOTE: you will need to drop the reference with put_device() after use.
3749 */
3750struct device *device_find_child_by_name(struct device *parent,
3751 const char *name)
3752{
3753 struct klist_iter i;
3754 struct device *child;
3755
3756 if (!parent)
3757 return NULL;
3758
3759 klist_iter_init(&parent->p->klist_children, &i);
3760 while ((child = next_device(&i)))
Dan Williamsc77f5202020-10-13 16:50:18 -07003761 if (sysfs_streq(dev_name(child), name) && get_device(child))
Heikki Krogerusdad9bb02019-05-31 17:15:37 +03003762 break;
3763 klist_iter_exit(&i);
3764 return child;
3765}
3766EXPORT_SYMBOL_GPL(device_find_child_by_name);
3767
Linus Torvalds1da177e2005-04-16 15:20:36 -07003768int __init devices_init(void)
3769{
Greg Kroah-Hartman881c6cfd2007-11-01 09:29:06 -06003770 devices_kset = kset_create_and_add("devices", &device_uevent_ops, NULL);
3771 if (!devices_kset)
3772 return -ENOMEM;
Dan Williamse105b8b2008-04-21 10:51:07 -07003773 dev_kobj = kobject_create_and_add("dev", NULL);
3774 if (!dev_kobj)
3775 goto dev_kobj_err;
3776 sysfs_dev_block_kobj = kobject_create_and_add("block", dev_kobj);
3777 if (!sysfs_dev_block_kobj)
3778 goto block_kobj_err;
3779 sysfs_dev_char_kobj = kobject_create_and_add("char", dev_kobj);
3780 if (!sysfs_dev_char_kobj)
3781 goto char_kobj_err;
3782
Greg Kroah-Hartman881c6cfd2007-11-01 09:29:06 -06003783 return 0;
Dan Williamse105b8b2008-04-21 10:51:07 -07003784
3785 char_kobj_err:
3786 kobject_put(sysfs_dev_block_kobj);
3787 block_kobj_err:
3788 kobject_put(dev_kobj);
3789 dev_kobj_err:
3790 kset_unregister(devices_kset);
3791 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003792}
3793
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02003794static int device_check_offline(struct device *dev, void *not_used)
3795{
3796 int ret;
3797
3798 ret = device_for_each_child(dev, NULL, device_check_offline);
3799 if (ret)
3800 return ret;
3801
3802 return device_supports_offline(dev) && !dev->offline ? -EBUSY : 0;
3803}
3804
3805/**
3806 * device_offline - Prepare the device for hot-removal.
3807 * @dev: Device to be put offline.
3808 *
3809 * Execute the device bus type's .offline() callback, if present, to prepare
3810 * the device for a subsequent hot-removal. If that succeeds, the device must
3811 * not be used until either it is removed or its bus type's .online() callback
3812 * is executed.
3813 *
3814 * Call under device_hotplug_lock.
3815 */
3816int device_offline(struct device *dev)
3817{
3818 int ret;
3819
3820 if (dev->offline_disabled)
3821 return -EPERM;
3822
3823 ret = device_for_each_child(dev, NULL, device_check_offline);
3824 if (ret)
3825 return ret;
3826
3827 device_lock(dev);
3828 if (device_supports_offline(dev)) {
3829 if (dev->offline) {
3830 ret = 1;
3831 } else {
3832 ret = dev->bus->offline(dev);
3833 if (!ret) {
3834 kobject_uevent(&dev->kobj, KOBJ_OFFLINE);
3835 dev->offline = true;
3836 }
3837 }
3838 }
3839 device_unlock(dev);
3840
3841 return ret;
3842}
3843
3844/**
3845 * device_online - Put the device back online after successful device_offline().
3846 * @dev: Device to be put back online.
3847 *
3848 * If device_offline() has been successfully executed for @dev, but the device
3849 * has not been removed subsequently, execute its bus type's .online() callback
3850 * to indicate that the device can be used again.
3851 *
3852 * Call under device_hotplug_lock.
3853 */
3854int device_online(struct device *dev)
3855{
3856 int ret = 0;
3857
3858 device_lock(dev);
3859 if (device_supports_offline(dev)) {
3860 if (dev->offline) {
3861 ret = dev->bus->online(dev);
3862 if (!ret) {
3863 kobject_uevent(&dev->kobj, KOBJ_ONLINE);
3864 dev->offline = false;
3865 }
3866 } else {
3867 ret = 1;
3868 }
3869 }
3870 device_unlock(dev);
3871
3872 return ret;
3873}
3874
Karthigan Srinivasan7f100d12011-04-18 16:16:52 -05003875struct root_device {
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00003876 struct device dev;
3877 struct module *owner;
3878};
3879
Josh Triplett93058422012-11-18 21:27:55 -08003880static inline struct root_device *to_root_device(struct device *d)
Ferenc Wagner481e2072011-01-07 15:17:47 +01003881{
3882 return container_of(d, struct root_device, dev);
3883}
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00003884
3885static void root_device_release(struct device *dev)
3886{
3887 kfree(to_root_device(dev));
3888}
3889
3890/**
3891 * __root_device_register - allocate and register a root device
3892 * @name: root device name
3893 * @owner: owner module of the root device, usually THIS_MODULE
3894 *
3895 * This function allocates a root device and registers it
3896 * using device_register(). In order to free the returned
3897 * device, use root_device_unregister().
3898 *
3899 * Root devices are dummy devices which allow other devices
3900 * to be grouped under /sys/devices. Use this function to
3901 * allocate a root device and then use it as the parent of
3902 * any device which should appear under /sys/devices/{name}
3903 *
3904 * The /sys/devices/{name} directory will also contain a
3905 * 'module' symlink which points to the @owner directory
3906 * in sysfs.
3907 *
Jani Nikulaf0eae0e2010-03-11 18:11:45 +02003908 * Returns &struct device pointer on success, or ERR_PTR() on error.
3909 *
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00003910 * Note: You probably want to use root_device_register().
3911 */
3912struct device *__root_device_register(const char *name, struct module *owner)
3913{
3914 struct root_device *root;
3915 int err = -ENOMEM;
3916
3917 root = kzalloc(sizeof(struct root_device), GFP_KERNEL);
3918 if (!root)
3919 return ERR_PTR(err);
3920
Greg Kroah-Hartmanacc0e902009-06-02 15:39:55 -07003921 err = dev_set_name(&root->dev, "%s", name);
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00003922 if (err) {
3923 kfree(root);
3924 return ERR_PTR(err);
3925 }
3926
3927 root->dev.release = root_device_release;
3928
3929 err = device_register(&root->dev);
3930 if (err) {
3931 put_device(&root->dev);
3932 return ERR_PTR(err);
3933 }
3934
Christoph Egger1d9e8822010-05-17 16:57:58 +02003935#ifdef CONFIG_MODULES /* gotta find a "cleaner" way to do this */
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00003936 if (owner) {
3937 struct module_kobject *mk = &owner->mkobj;
3938
3939 err = sysfs_create_link(&root->dev.kobj, &mk->kobj, "module");
3940 if (err) {
3941 device_unregister(&root->dev);
3942 return ERR_PTR(err);
3943 }
3944 root->owner = owner;
3945 }
3946#endif
3947
3948 return &root->dev;
3949}
3950EXPORT_SYMBOL_GPL(__root_device_register);
3951
3952/**
3953 * root_device_unregister - unregister and free a root device
Randy Dunlap7cbcf222009-01-20 16:29:13 -08003954 * @dev: device going away
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00003955 *
3956 * This function unregisters and cleans up a device that was created by
3957 * root_device_register().
3958 */
3959void root_device_unregister(struct device *dev)
3960{
3961 struct root_device *root = to_root_device(dev);
3962
3963 if (root->owner)
3964 sysfs_remove_link(&root->dev.kobj, "module");
3965
3966 device_unregister(dev);
3967}
3968EXPORT_SYMBOL_GPL(root_device_unregister);
3969
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07003970
3971static void device_create_release(struct device *dev)
3972{
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01003973 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07003974 kfree(dev);
3975}
3976
Mathieu Malaterre6a8b55d2018-05-05 21:57:41 +02003977static __printf(6, 0) struct device *
Guenter Roeck39ef3112013-07-14 16:05:57 -07003978device_create_groups_vargs(struct class *class, struct device *parent,
3979 dev_t devt, void *drvdata,
3980 const struct attribute_group **groups,
3981 const char *fmt, va_list args)
3982{
3983 struct device *dev = NULL;
3984 int retval = -ENODEV;
3985
3986 if (class == NULL || IS_ERR(class))
3987 goto error;
3988
3989 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
3990 if (!dev) {
3991 retval = -ENOMEM;
3992 goto error;
3993 }
3994
David Herrmannbbc780f2013-11-21 20:15:48 +01003995 device_initialize(dev);
Guenter Roeck39ef3112013-07-14 16:05:57 -07003996 dev->devt = devt;
3997 dev->class = class;
3998 dev->parent = parent;
3999 dev->groups = groups;
4000 dev->release = device_create_release;
4001 dev_set_drvdata(dev, drvdata);
4002
4003 retval = kobject_set_name_vargs(&dev->kobj, fmt, args);
4004 if (retval)
4005 goto error;
4006
David Herrmannbbc780f2013-11-21 20:15:48 +01004007 retval = device_add(dev);
Guenter Roeck39ef3112013-07-14 16:05:57 -07004008 if (retval)
4009 goto error;
4010
4011 return dev;
4012
4013error:
4014 put_device(dev);
4015 return ERR_PTR(retval);
4016}
4017
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07004018/**
Greg Kroah-Hartman4e106732008-07-21 20:03:34 -07004019 * device_create - creates a device and registers it with sysfs
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07004020 * @class: pointer to the struct class that this device should be registered to
4021 * @parent: pointer to the parent struct device of this new device, if any
4022 * @devt: the dev_t for the char device to be added
4023 * @drvdata: the data to be added to the device for callbacks
4024 * @fmt: string for the device's name
4025 *
4026 * This function can be used by char device classes. A struct device
4027 * will be created in sysfs, registered to the specified class.
4028 *
4029 * A "dev" file will be created, showing the dev_t for the device, if
4030 * the dev_t is not 0,0.
4031 * If a pointer to a parent struct device is passed in, the newly created
4032 * struct device will be a child of that device in sysfs.
4033 * The pointer to the struct device will be returned from the call.
4034 * Any further sysfs files that might be required can be created using this
4035 * pointer.
4036 *
Jani Nikulaf0eae0e2010-03-11 18:11:45 +02004037 * Returns &struct device pointer on success, or ERR_PTR() on error.
4038 *
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07004039 * Note: the struct class passed to this function must have previously
4040 * been created with a call to class_create().
4041 */
Greg Kroah-Hartman4e106732008-07-21 20:03:34 -07004042struct device *device_create(struct class *class, struct device *parent,
4043 dev_t devt, void *drvdata, const char *fmt, ...)
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07004044{
4045 va_list vargs;
4046 struct device *dev;
4047
4048 va_start(vargs, fmt);
Christoph Hellwig4c747462020-05-04 14:47:57 +02004049 dev = device_create_groups_vargs(class, parent, devt, drvdata, NULL,
4050 fmt, vargs);
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07004051 va_end(vargs);
4052 return dev;
4053}
Greg Kroah-Hartman4e106732008-07-21 20:03:34 -07004054EXPORT_SYMBOL_GPL(device_create);
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07004055
Guenter Roeck39ef3112013-07-14 16:05:57 -07004056/**
4057 * device_create_with_groups - creates a device and registers it with sysfs
4058 * @class: pointer to the struct class that this device should be registered to
4059 * @parent: pointer to the parent struct device of this new device, if any
4060 * @devt: the dev_t for the char device to be added
4061 * @drvdata: the data to be added to the device for callbacks
4062 * @groups: NULL-terminated list of attribute groups to be created
4063 * @fmt: string for the device's name
4064 *
4065 * This function can be used by char device classes. A struct device
4066 * will be created in sysfs, registered to the specified class.
4067 * Additional attributes specified in the groups parameter will also
4068 * be created automatically.
4069 *
4070 * A "dev" file will be created, showing the dev_t for the device, if
4071 * the dev_t is not 0,0.
4072 * If a pointer to a parent struct device is passed in, the newly created
4073 * struct device will be a child of that device in sysfs.
4074 * The pointer to the struct device will be returned from the call.
4075 * Any further sysfs files that might be required can be created using this
4076 * pointer.
4077 *
4078 * Returns &struct device pointer on success, or ERR_PTR() on error.
4079 *
4080 * Note: the struct class passed to this function must have previously
4081 * been created with a call to class_create().
4082 */
4083struct device *device_create_with_groups(struct class *class,
4084 struct device *parent, dev_t devt,
4085 void *drvdata,
4086 const struct attribute_group **groups,
4087 const char *fmt, ...)
4088{
4089 va_list vargs;
4090 struct device *dev;
4091
4092 va_start(vargs, fmt);
4093 dev = device_create_groups_vargs(class, parent, devt, drvdata, groups,
4094 fmt, vargs);
4095 va_end(vargs);
4096 return dev;
4097}
4098EXPORT_SYMBOL_GPL(device_create_with_groups);
4099
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01004100/**
4101 * device_destroy - removes a device that was created with device_create()
4102 * @class: pointer to the struct class that this device was registered with
4103 * @devt: the dev_t of the device that was previously registered
4104 *
4105 * This call unregisters and cleans up a device that was created with a
4106 * call to device_create().
4107 */
4108void device_destroy(struct class *class, dev_t devt)
4109{
4110 struct device *dev;
4111
Suzuki K Poulose4495dfd2019-07-23 23:18:35 +01004112 dev = class_find_device_by_devt(class, devt);
Dave Youngcd354492008-01-28 16:56:11 +08004113 if (dev) {
4114 put_device(dev);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07004115 device_unregister(dev);
Dave Youngcd354492008-01-28 16:56:11 +08004116 }
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07004117}
4118EXPORT_SYMBOL_GPL(device_destroy);
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07004119
4120/**
4121 * device_rename - renames a device
4122 * @dev: the pointer to the struct device to be renamed
4123 * @new_name: the new name of the device
Eric W. Biederman030c1d22008-05-08 14:41:00 -07004124 *
4125 * It is the responsibility of the caller to provide mutual
4126 * exclusion between two different calls of device_rename
4127 * on the same device to ensure that new_name is valid and
4128 * won't conflict with other devices.
Michael Ellermanc6c0ac62010-11-25 09:44:07 +11004129 *
Timur Tabia5462512010-12-13 14:08:52 -06004130 * Note: Don't call this function. Currently, the networking layer calls this
4131 * function, but that will change. The following text from Kay Sievers offers
4132 * some insight:
4133 *
4134 * Renaming devices is racy at many levels, symlinks and other stuff are not
4135 * replaced atomically, and you get a "move" uevent, but it's not easy to
4136 * connect the event to the old and new device. Device nodes are not renamed at
4137 * all, there isn't even support for that in the kernel now.
4138 *
4139 * In the meantime, during renaming, your target name might be taken by another
4140 * driver, creating conflicts. Or the old name is taken directly after you
4141 * renamed it -- then you get events for the same DEVPATH, before you even see
4142 * the "move" event. It's just a mess, and nothing new should ever rely on
4143 * kernel device renaming. Besides that, it's not even implemented now for
4144 * other things than (driver-core wise very simple) network devices.
4145 *
4146 * We are currently about to change network renaming in udev to completely
4147 * disallow renaming of devices in the same namespace as the kernel uses,
4148 * because we can't solve the problems properly, that arise with swapping names
4149 * of multiple interfaces without races. Means, renaming of eth[0-9]* will only
4150 * be allowed to some other name than eth[0-9]*, for the aforementioned
4151 * reasons.
4152 *
4153 * Make up a "real" name in the driver before you register anything, or add
4154 * some other attributes for userspace to find the device, or use udev to add
4155 * symlinks -- but never rename kernel devices later, it's a complete mess. We
4156 * don't even want to get into that and try to implement the missing pieces in
4157 * the core. We really have other pieces to fix in the driver core mess. :)
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07004158 */
Johannes Berg6937e8f2010-08-05 17:38:18 +02004159int device_rename(struct device *dev, const char *new_name)
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07004160{
Tejun Heo4b30ee52013-09-11 22:29:06 -04004161 struct kobject *kobj = &dev->kobj;
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07004162 char *old_device_name = NULL;
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07004163 int error;
4164
4165 dev = get_device(dev);
4166 if (!dev)
4167 return -EINVAL;
4168
ethan.zhao69df7532013-10-13 22:12:35 +08004169 dev_dbg(dev, "renaming to %s\n", new_name);
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07004170
Kay Sievers1fa5ae82009-01-25 15:17:37 +01004171 old_device_name = kstrdup(dev_name(dev), GFP_KERNEL);
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07004172 if (!old_device_name) {
4173 error = -ENOMEM;
4174 goto out;
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07004175 }
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07004176
Eric W. Biedermanf349cf32010-03-30 11:31:29 -07004177 if (dev->class) {
Tejun Heo4b30ee52013-09-11 22:29:06 -04004178 error = sysfs_rename_link_ns(&dev->class->p->subsys.kobj,
4179 kobj, old_device_name,
4180 new_name, kobject_namespace(kobj));
Eric W. Biedermanf349cf32010-03-30 11:31:29 -07004181 if (error)
4182 goto out;
4183 }
Kay Sievers39aba962010-09-04 22:33:14 -07004184
Tejun Heo4b30ee52013-09-11 22:29:06 -04004185 error = kobject_rename(kobj, new_name);
Kay Sievers1fa5ae82009-01-25 15:17:37 +01004186 if (error)
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07004187 goto out;
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07004188
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07004189out:
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07004190 put_device(dev);
4191
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07004192 kfree(old_device_name);
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07004193
4194 return error;
4195}
Johannes Berga2807db2007-02-28 12:38:31 +01004196EXPORT_SYMBOL_GPL(device_rename);
Cornelia Huck8a824722006-11-20 17:07:51 +01004197
4198static int device_move_class_links(struct device *dev,
4199 struct device *old_parent,
4200 struct device *new_parent)
4201{
Greg Kroah-Hartmanf7f34612007-03-06 12:55:53 -08004202 int error = 0;
Cornelia Huck8a824722006-11-20 17:07:51 +01004203
Greg Kroah-Hartmanf7f34612007-03-06 12:55:53 -08004204 if (old_parent)
4205 sysfs_remove_link(&dev->kobj, "device");
4206 if (new_parent)
4207 error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
4208 "device");
4209 return error;
Cornelia Huck8a824722006-11-20 17:07:51 +01004210}
4211
4212/**
4213 * device_move - moves a device to a new parent
4214 * @dev: the pointer to the struct device to be moved
Wolfram Sang13509862018-05-06 13:23:47 +02004215 * @new_parent: the new parent of the device (can be NULL)
Cornelia Huckffa6a702009-03-04 12:44:00 +01004216 * @dpm_order: how to reorder the dpm_list
Cornelia Huck8a824722006-11-20 17:07:51 +01004217 */
Cornelia Huckffa6a702009-03-04 12:44:00 +01004218int device_move(struct device *dev, struct device *new_parent,
4219 enum dpm_order dpm_order)
Cornelia Huck8a824722006-11-20 17:07:51 +01004220{
4221 int error;
4222 struct device *old_parent;
Cornelia Huckc744aeae2007-01-08 20:16:44 +01004223 struct kobject *new_parent_kobj;
Cornelia Huck8a824722006-11-20 17:07:51 +01004224
4225 dev = get_device(dev);
4226 if (!dev)
4227 return -EINVAL;
4228
Cornelia Huckffa6a702009-03-04 12:44:00 +01004229 device_pm_lock();
Cornelia Huck8a824722006-11-20 17:07:51 +01004230 new_parent = get_device(new_parent);
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08004231 new_parent_kobj = get_device_parent(dev, new_parent);
Tetsuo Handa84d0c272018-05-07 19:10:31 +09004232 if (IS_ERR(new_parent_kobj)) {
4233 error = PTR_ERR(new_parent_kobj);
4234 put_device(new_parent);
4235 goto out;
4236 }
Cornelia Huck63b69712008-01-21 16:09:44 +01004237
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01004238 pr_debug("device: '%s': %s: moving to '%s'\n", dev_name(dev),
4239 __func__, new_parent ? dev_name(new_parent) : "<NULL>");
Cornelia Huckc744aeae2007-01-08 20:16:44 +01004240 error = kobject_move(&dev->kobj, new_parent_kobj);
Cornelia Huck8a824722006-11-20 17:07:51 +01004241 if (error) {
Cornelia Huck63b69712008-01-21 16:09:44 +01004242 cleanup_glue_dir(dev, new_parent_kobj);
Cornelia Huck8a824722006-11-20 17:07:51 +01004243 put_device(new_parent);
4244 goto out;
4245 }
4246 old_parent = dev->parent;
4247 dev->parent = new_parent;
4248 if (old_parent)
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08004249 klist_remove(&dev->p->knode_parent);
Yinghai Lu0d358f22008-02-19 03:20:41 -08004250 if (new_parent) {
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08004251 klist_add_tail(&dev->p->knode_parent,
4252 &new_parent->p->klist_children);
Yinghai Lu0d358f22008-02-19 03:20:41 -08004253 set_dev_node(dev, dev_to_node(new_parent));
4254 }
4255
Rabin Vincentbdd40342012-04-23 09:16:36 +02004256 if (dev->class) {
4257 error = device_move_class_links(dev, old_parent, new_parent);
4258 if (error) {
4259 /* We ignore errors on cleanup since we're hosed anyway... */
4260 device_move_class_links(dev, new_parent, old_parent);
4261 if (!kobject_move(&dev->kobj, &old_parent->kobj)) {
4262 if (new_parent)
4263 klist_remove(&dev->p->knode_parent);
4264 dev->parent = old_parent;
4265 if (old_parent) {
4266 klist_add_tail(&dev->p->knode_parent,
4267 &old_parent->p->klist_children);
4268 set_dev_node(dev, dev_to_node(old_parent));
4269 }
Yinghai Lu0d358f22008-02-19 03:20:41 -08004270 }
Rabin Vincentbdd40342012-04-23 09:16:36 +02004271 cleanup_glue_dir(dev, new_parent_kobj);
4272 put_device(new_parent);
4273 goto out;
Cornelia Huck8a824722006-11-20 17:07:51 +01004274 }
Cornelia Huck8a824722006-11-20 17:07:51 +01004275 }
Cornelia Huckffa6a702009-03-04 12:44:00 +01004276 switch (dpm_order) {
4277 case DPM_ORDER_NONE:
4278 break;
4279 case DPM_ORDER_DEV_AFTER_PARENT:
4280 device_pm_move_after(dev, new_parent);
Grygorii Strashko52cdbdd2015-07-27 20:43:01 +03004281 devices_kset_move_after(dev, new_parent);
Cornelia Huckffa6a702009-03-04 12:44:00 +01004282 break;
4283 case DPM_ORDER_PARENT_BEFORE_DEV:
4284 device_pm_move_before(new_parent, dev);
Grygorii Strashko52cdbdd2015-07-27 20:43:01 +03004285 devices_kset_move_before(new_parent, dev);
Cornelia Huckffa6a702009-03-04 12:44:00 +01004286 break;
4287 case DPM_ORDER_DEV_LAST:
4288 device_pm_move_last(dev);
Grygorii Strashko52cdbdd2015-07-27 20:43:01 +03004289 devices_kset_move_last(dev);
Cornelia Huckffa6a702009-03-04 12:44:00 +01004290 break;
4291 }
Rabin Vincentbdd40342012-04-23 09:16:36 +02004292
Cornelia Huck8a824722006-11-20 17:07:51 +01004293 put_device(old_parent);
4294out:
Cornelia Huckffa6a702009-03-04 12:44:00 +01004295 device_pm_unlock();
Cornelia Huck8a824722006-11-20 17:07:51 +01004296 put_device(dev);
4297 return error;
4298}
Cornelia Huck8a824722006-11-20 17:07:51 +01004299EXPORT_SYMBOL_GPL(device_move);
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08004300
Christian Braunerb8f33e52020-02-27 04:37:15 +01004301static int device_attrs_change_owner(struct device *dev, kuid_t kuid,
4302 kgid_t kgid)
4303{
4304 struct kobject *kobj = &dev->kobj;
4305 struct class *class = dev->class;
4306 const struct device_type *type = dev->type;
4307 int error;
4308
4309 if (class) {
4310 /*
4311 * Change the device groups of the device class for @dev to
4312 * @kuid/@kgid.
4313 */
4314 error = sysfs_groups_change_owner(kobj, class->dev_groups, kuid,
4315 kgid);
4316 if (error)
4317 return error;
4318 }
4319
4320 if (type) {
4321 /*
4322 * Change the device groups of the device type for @dev to
4323 * @kuid/@kgid.
4324 */
4325 error = sysfs_groups_change_owner(kobj, type->groups, kuid,
4326 kgid);
4327 if (error)
4328 return error;
4329 }
4330
4331 /* Change the device groups of @dev to @kuid/@kgid. */
4332 error = sysfs_groups_change_owner(kobj, dev->groups, kuid, kgid);
4333 if (error)
4334 return error;
4335
4336 if (device_supports_offline(dev) && !dev->offline_disabled) {
4337 /* Change online device attributes of @dev to @kuid/@kgid. */
4338 error = sysfs_file_change_owner(kobj, dev_attr_online.attr.name,
4339 kuid, kgid);
4340 if (error)
4341 return error;
4342 }
4343
4344 return 0;
4345}
4346
4347/**
4348 * device_change_owner - change the owner of an existing device.
4349 * @dev: device.
4350 * @kuid: new owner's kuid
4351 * @kgid: new owner's kgid
4352 *
4353 * This changes the owner of @dev and its corresponding sysfs entries to
4354 * @kuid/@kgid. This function closely mirrors how @dev was added via driver
4355 * core.
4356 *
4357 * Returns 0 on success or error code on failure.
4358 */
4359int device_change_owner(struct device *dev, kuid_t kuid, kgid_t kgid)
4360{
4361 int error;
4362 struct kobject *kobj = &dev->kobj;
4363
4364 dev = get_device(dev);
4365 if (!dev)
4366 return -EINVAL;
4367
4368 /*
4369 * Change the kobject and the default attributes and groups of the
4370 * ktype associated with it to @kuid/@kgid.
4371 */
4372 error = sysfs_change_owner(kobj, kuid, kgid);
4373 if (error)
4374 goto out;
4375
4376 /*
4377 * Change the uevent file for @dev to the new owner. The uevent file
4378 * was created in a separate step when @dev got added and we mirror
4379 * that step here.
4380 */
4381 error = sysfs_file_change_owner(kobj, dev_attr_uevent.attr.name, kuid,
4382 kgid);
4383 if (error)
4384 goto out;
4385
4386 /*
4387 * Change the device groups, the device groups associated with the
4388 * device class, and the groups associated with the device type of @dev
4389 * to @kuid/@kgid.
4390 */
4391 error = device_attrs_change_owner(dev, kuid, kgid);
4392 if (error)
4393 goto out;
4394
Christian Brauner3b52fc52020-02-27 04:37:16 +01004395 error = dpm_sysfs_change_owner(dev, kuid, kgid);
4396 if (error)
4397 goto out;
4398
Christian Braunerb8f33e52020-02-27 04:37:15 +01004399#ifdef CONFIG_BLOCK
4400 if (sysfs_deprecated && dev->class == &block_class)
4401 goto out;
4402#endif
4403
4404 /*
4405 * Change the owner of the symlink located in the class directory of
4406 * the device class associated with @dev which points to the actual
4407 * directory entry for @dev to @kuid/@kgid. This ensures that the
4408 * symlink shows the same permissions as its target.
4409 */
4410 error = sysfs_link_change_owner(&dev->class->p->subsys.kobj, &dev->kobj,
4411 dev_name(dev), kuid, kgid);
4412 if (error)
4413 goto out;
4414
4415out:
4416 put_device(dev);
4417 return error;
4418}
4419EXPORT_SYMBOL_GPL(device_change_owner);
4420
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08004421/**
4422 * device_shutdown - call ->shutdown() on each device to shutdown.
4423 */
4424void device_shutdown(void)
4425{
Benson Leungf123db82013-09-24 20:05:11 -07004426 struct device *dev, *parent;
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08004427
Pingfan Liu3297c8f2018-07-19 13:14:58 +08004428 wait_for_device_probe();
4429 device_block_probing();
4430
Rafael J. Wysocki65650b32019-10-09 01:29:10 +02004431 cpufreq_suspend();
4432
Hugh Daschbach62458382010-03-22 10:36:37 -07004433 spin_lock(&devices_kset->list_lock);
4434 /*
4435 * Walk the devices list backward, shutting down each in turn.
4436 * Beware that device unplug events may also start pulling
4437 * devices offline, even as the system is shutting down.
4438 */
4439 while (!list_empty(&devices_kset->list)) {
4440 dev = list_entry(devices_kset->list.prev, struct device,
4441 kobj.entry);
Ming Leid1c6c032012-06-22 18:01:40 +08004442
4443 /*
4444 * hold reference count of device's parent to
4445 * prevent it from being freed because parent's
4446 * lock is to be held
4447 */
Benson Leungf123db82013-09-24 20:05:11 -07004448 parent = get_device(dev->parent);
Hugh Daschbach62458382010-03-22 10:36:37 -07004449 get_device(dev);
4450 /*
4451 * Make sure the device is off the kset list, in the
4452 * event that dev->*->shutdown() doesn't remove it.
4453 */
4454 list_del_init(&dev->kobj.entry);
4455 spin_unlock(&devices_kset->list_lock);
Alan Sternfe6b91f2011-12-06 23:24:52 +01004456
Ming Leid1c6c032012-06-22 18:01:40 +08004457 /* hold lock to avoid race with probe/release */
Benson Leungf123db82013-09-24 20:05:11 -07004458 if (parent)
4459 device_lock(parent);
Ming Leid1c6c032012-06-22 18:01:40 +08004460 device_lock(dev);
4461
Alan Sternfe6b91f2011-12-06 23:24:52 +01004462 /* Don't allow any more runtime suspends */
4463 pm_runtime_get_noresume(dev);
4464 pm_runtime_barrier(dev);
Hugh Daschbach62458382010-03-22 10:36:37 -07004465
Michal Suchanek75216212017-08-11 15:44:43 +02004466 if (dev->class && dev->class->shutdown_pre) {
Josh Zimmermanf77af152017-06-25 14:53:23 -07004467 if (initcall_debug)
Michal Suchanek75216212017-08-11 15:44:43 +02004468 dev_info(dev, "shutdown_pre\n");
4469 dev->class->shutdown_pre(dev);
4470 }
4471 if (dev->bus && dev->bus->shutdown) {
ShuoX Liu0246c4f2012-11-23 15:14:12 +08004472 if (initcall_debug)
4473 dev_info(dev, "shutdown\n");
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08004474 dev->bus->shutdown(dev);
4475 } else if (dev->driver && dev->driver->shutdown) {
ShuoX Liu0246c4f2012-11-23 15:14:12 +08004476 if (initcall_debug)
4477 dev_info(dev, "shutdown\n");
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08004478 dev->driver->shutdown(dev);
4479 }
Ming Leid1c6c032012-06-22 18:01:40 +08004480
4481 device_unlock(dev);
Benson Leungf123db82013-09-24 20:05:11 -07004482 if (parent)
4483 device_unlock(parent);
Ming Leid1c6c032012-06-22 18:01:40 +08004484
Hugh Daschbach62458382010-03-22 10:36:37 -07004485 put_device(dev);
Benson Leungf123db82013-09-24 20:05:11 -07004486 put_device(parent);
Hugh Daschbach62458382010-03-22 10:36:37 -07004487
4488 spin_lock(&devices_kset->list_lock);
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08004489 }
Hugh Daschbach62458382010-03-22 10:36:37 -07004490 spin_unlock(&devices_kset->list_lock);
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08004491}
Joe Perches99bcf212010-06-27 01:02:34 +00004492
4493/*
4494 * Device logging functions
4495 */
4496
4497#ifdef CONFIG_PRINTK
John Ogness74caba72020-09-21 13:24:45 +02064498static void
4499set_dev_info(const struct device *dev, struct dev_printk_info *dev_info)
Joe Perches99bcf212010-06-27 01:02:34 +00004500{
Kay Sieversc4e00da2012-05-03 02:29:59 +02004501 const char *subsys;
John Ogness74caba72020-09-21 13:24:45 +02064502
4503 memset(dev_info, 0, sizeof(*dev_info));
Joe Perches99bcf212010-06-27 01:02:34 +00004504
Kay Sieversc4e00da2012-05-03 02:29:59 +02004505 if (dev->class)
4506 subsys = dev->class->name;
4507 else if (dev->bus)
4508 subsys = dev->bus->name;
4509 else
John Ogness74caba72020-09-21 13:24:45 +02064510 return;
Kay Sieversc4e00da2012-05-03 02:29:59 +02004511
John Ogness74caba72020-09-21 13:24:45 +02064512 strscpy(dev_info->subsystem, subsys, sizeof(dev_info->subsystem));
Kay Sieversc4e00da2012-05-03 02:29:59 +02004513
4514 /*
4515 * Add device identifier DEVICE=:
4516 * b12:8 block dev_t
4517 * c127:3 char dev_t
4518 * n8 netdev ifindex
4519 * +sound:card0 subsystem:devname
4520 */
4521 if (MAJOR(dev->devt)) {
4522 char c;
4523
4524 if (strcmp(subsys, "block") == 0)
4525 c = 'b';
4526 else
4527 c = 'c';
John Ogness74caba72020-09-21 13:24:45 +02064528
4529 snprintf(dev_info->device, sizeof(dev_info->device),
4530 "%c%u:%u", c, MAJOR(dev->devt), MINOR(dev->devt));
Kay Sieversc4e00da2012-05-03 02:29:59 +02004531 } else if (strcmp(subsys, "net") == 0) {
4532 struct net_device *net = to_net_dev(dev);
4533
John Ogness74caba72020-09-21 13:24:45 +02064534 snprintf(dev_info->device, sizeof(dev_info->device),
4535 "n%u", net->ifindex);
Kay Sieversc4e00da2012-05-03 02:29:59 +02004536 } else {
John Ogness74caba72020-09-21 13:24:45 +02064537 snprintf(dev_info->device, sizeof(dev_info->device),
4538 "+%s:%s", subsys, dev_name(dev));
Kay Sieversc4e00da2012-05-03 02:29:59 +02004539 }
Joe Perches99bcf212010-06-27 01:02:34 +00004540}
Joe Perches798efc62012-09-12 20:11:29 -07004541
Joe Perches05e4e5b2012-09-12 20:13:37 -07004542int dev_vprintk_emit(int level, const struct device *dev,
4543 const char *fmt, va_list args)
4544{
John Ogness74caba72020-09-21 13:24:45 +02064545 struct dev_printk_info dev_info;
Joe Perches05e4e5b2012-09-12 20:13:37 -07004546
John Ogness74caba72020-09-21 13:24:45 +02064547 set_dev_info(dev, &dev_info);
Joe Perches05e4e5b2012-09-12 20:13:37 -07004548
John Ogness74caba72020-09-21 13:24:45 +02064549 return vprintk_emit(0, level, &dev_info, fmt, args);
Joe Perches05e4e5b2012-09-12 20:13:37 -07004550}
4551EXPORT_SYMBOL(dev_vprintk_emit);
4552
4553int dev_printk_emit(int level, const struct device *dev, const char *fmt, ...)
4554{
4555 va_list args;
4556 int r;
4557
4558 va_start(args, fmt);
4559
4560 r = dev_vprintk_emit(level, dev, fmt, args);
4561
4562 va_end(args);
4563
4564 return r;
4565}
4566EXPORT_SYMBOL(dev_printk_emit);
4567
Joe Perchesd1f10522014-12-25 15:07:04 -08004568static void __dev_printk(const char *level, const struct device *dev,
Joe Perches798efc62012-09-12 20:11:29 -07004569 struct va_format *vaf)
4570{
Joe Perchesd1f10522014-12-25 15:07:04 -08004571 if (dev)
4572 dev_printk_emit(level[1] - '0', dev, "%s %s: %pV",
4573 dev_driver_string(dev), dev_name(dev), vaf);
4574 else
4575 printk("%s(NULL device *): %pV", level, vaf);
Joe Perches798efc62012-09-12 20:11:29 -07004576}
Joe Perches99bcf212010-06-27 01:02:34 +00004577
Joe Perchesd1f10522014-12-25 15:07:04 -08004578void dev_printk(const char *level, const struct device *dev,
4579 const char *fmt, ...)
Joe Perches99bcf212010-06-27 01:02:34 +00004580{
4581 struct va_format vaf;
4582 va_list args;
Joe Perches99bcf212010-06-27 01:02:34 +00004583
4584 va_start(args, fmt);
4585
4586 vaf.fmt = fmt;
4587 vaf.va = &args;
4588
Joe Perchesd1f10522014-12-25 15:07:04 -08004589 __dev_printk(level, dev, &vaf);
Joe Perches798efc62012-09-12 20:11:29 -07004590
Joe Perches99bcf212010-06-27 01:02:34 +00004591 va_end(args);
Joe Perches99bcf212010-06-27 01:02:34 +00004592}
4593EXPORT_SYMBOL(dev_printk);
4594
4595#define define_dev_printk_level(func, kern_level) \
Joe Perchesd1f10522014-12-25 15:07:04 -08004596void func(const struct device *dev, const char *fmt, ...) \
Joe Perches99bcf212010-06-27 01:02:34 +00004597{ \
4598 struct va_format vaf; \
4599 va_list args; \
Joe Perches99bcf212010-06-27 01:02:34 +00004600 \
4601 va_start(args, fmt); \
4602 \
4603 vaf.fmt = fmt; \
4604 vaf.va = &args; \
4605 \
Joe Perchesd1f10522014-12-25 15:07:04 -08004606 __dev_printk(kern_level, dev, &vaf); \
Joe Perches798efc62012-09-12 20:11:29 -07004607 \
Joe Perches99bcf212010-06-27 01:02:34 +00004608 va_end(args); \
Joe Perches99bcf212010-06-27 01:02:34 +00004609} \
4610EXPORT_SYMBOL(func);
4611
Joe Perches663336e2018-05-09 08:15:46 -07004612define_dev_printk_level(_dev_emerg, KERN_EMERG);
4613define_dev_printk_level(_dev_alert, KERN_ALERT);
4614define_dev_printk_level(_dev_crit, KERN_CRIT);
4615define_dev_printk_level(_dev_err, KERN_ERR);
4616define_dev_printk_level(_dev_warn, KERN_WARNING);
4617define_dev_printk_level(_dev_notice, KERN_NOTICE);
Joe Perches99bcf212010-06-27 01:02:34 +00004618define_dev_printk_level(_dev_info, KERN_INFO);
4619
4620#endif
Rafael J. Wysocki97badf82015-04-03 23:23:37 +02004621
Andrzej Hajdaa787e542020-07-13 16:43:21 +02004622/**
4623 * dev_err_probe - probe error check and log helper
4624 * @dev: the pointer to the struct device
4625 * @err: error value to test
4626 * @fmt: printf-style format string
4627 * @...: arguments as specified in the format string
4628 *
4629 * This helper implements common pattern present in probe functions for error
4630 * checking: print debug or error message depending if the error value is
4631 * -EPROBE_DEFER and propagate error upwards.
Andrzej Hajdad090b702020-07-13 16:43:22 +02004632 * In case of -EPROBE_DEFER it sets also defer probe reason, which can be
4633 * checked later by reading devices_deferred debugfs attribute.
Mauro Carvalho Chehab074b3aa2020-09-09 11:53:43 +02004634 * It replaces code sequence::
4635 *
Andrzej Hajdaa787e542020-07-13 16:43:21 +02004636 * if (err != -EPROBE_DEFER)
4637 * dev_err(dev, ...);
4638 * else
4639 * dev_dbg(dev, ...);
4640 * return err;
Mauro Carvalho Chehab074b3aa2020-09-09 11:53:43 +02004641 *
4642 * with::
4643 *
Andrzej Hajdaa787e542020-07-13 16:43:21 +02004644 * return dev_err_probe(dev, err, ...);
4645 *
4646 * Returns @err.
4647 *
4648 */
4649int dev_err_probe(const struct device *dev, int err, const char *fmt, ...)
4650{
4651 struct va_format vaf;
4652 va_list args;
4653
4654 va_start(args, fmt);
4655 vaf.fmt = fmt;
4656 vaf.va = &args;
4657
Andrzej Hajdad090b702020-07-13 16:43:22 +02004658 if (err != -EPROBE_DEFER) {
Michał Mirosław693a8e92020-08-28 18:14:35 +02004659 dev_err(dev, "error %pe: %pV", ERR_PTR(err), &vaf);
Andrzej Hajdad090b702020-07-13 16:43:22 +02004660 } else {
4661 device_set_deferred_probe_reason(dev, &vaf);
Michał Mirosław693a8e92020-08-28 18:14:35 +02004662 dev_dbg(dev, "error %pe: %pV", ERR_PTR(err), &vaf);
Andrzej Hajdad090b702020-07-13 16:43:22 +02004663 }
Andrzej Hajdaa787e542020-07-13 16:43:21 +02004664
4665 va_end(args);
4666
4667 return err;
4668}
4669EXPORT_SYMBOL_GPL(dev_err_probe);
4670
Rafael J. Wysocki97badf82015-04-03 23:23:37 +02004671static inline bool fwnode_is_primary(struct fwnode_handle *fwnode)
4672{
4673 return fwnode && !IS_ERR(fwnode->secondary);
4674}
4675
4676/**
4677 * set_primary_fwnode - Change the primary firmware node of a given device.
4678 * @dev: Device to handle.
4679 * @fwnode: New primary firmware node of the device.
4680 *
4681 * Set the device's firmware node pointer to @fwnode, but if a secondary
4682 * firmware node of the device is present, preserve it.
Bard Liao3f7bdda2021-01-05 17:11:46 +08004683 *
4684 * Valid fwnode cases are:
4685 * - primary --> secondary --> -ENODEV
4686 * - primary --> NULL
4687 * - secondary --> -ENODEV
4688 * - NULL
Rafael J. Wysocki97badf82015-04-03 23:23:37 +02004689 */
4690void set_primary_fwnode(struct device *dev, struct fwnode_handle *fwnode)
4691{
Andy Shevchenko99aed922020-10-22 21:41:00 +03004692 struct device *parent = dev->parent;
Heikki Krogerusc15e1bd2020-08-21 13:53:42 +03004693 struct fwnode_handle *fn = dev->fwnode;
Rafael J. Wysocki97badf82015-04-03 23:23:37 +02004694
Heikki Krogerusc15e1bd2020-08-21 13:53:42 +03004695 if (fwnode) {
Rafael J. Wysocki97badf82015-04-03 23:23:37 +02004696 if (fwnode_is_primary(fn))
4697 fn = fn->secondary;
4698
Mika Westerberg55f89a82015-11-30 17:11:39 +02004699 if (fn) {
4700 WARN_ON(fwnode->secondary);
4701 fwnode->secondary = fn;
4702 }
Rafael J. Wysocki97badf82015-04-03 23:23:37 +02004703 dev->fwnode = fwnode;
4704 } else {
Heikki Krogerusc15e1bd2020-08-21 13:53:42 +03004705 if (fwnode_is_primary(fn)) {
4706 dev->fwnode = fn->secondary;
Bard Liao3f7bdda2021-01-05 17:11:46 +08004707 /* Set fn->secondary = NULL, so fn remains the primary fwnode */
Andy Shevchenko99aed922020-10-22 21:41:00 +03004708 if (!(parent && fn == parent->fwnode))
Bard Liao47f44692021-01-05 17:11:45 +08004709 fn->secondary = NULL;
Heikki Krogerusc15e1bd2020-08-21 13:53:42 +03004710 } else {
4711 dev->fwnode = NULL;
4712 }
Rafael J. Wysocki97badf82015-04-03 23:23:37 +02004713 }
4714}
4715EXPORT_SYMBOL_GPL(set_primary_fwnode);
4716
4717/**
4718 * set_secondary_fwnode - Change the secondary firmware node of a given device.
4719 * @dev: Device to handle.
4720 * @fwnode: New secondary firmware node of the device.
4721 *
4722 * If a primary firmware node of the device is present, set its secondary
4723 * pointer to @fwnode. Otherwise, set the device's firmware node pointer to
4724 * @fwnode.
4725 */
4726void set_secondary_fwnode(struct device *dev, struct fwnode_handle *fwnode)
4727{
4728 if (fwnode)
4729 fwnode->secondary = ERR_PTR(-ENODEV);
4730
4731 if (fwnode_is_primary(dev->fwnode))
4732 dev->fwnode->secondary = fwnode;
4733 else
4734 dev->fwnode = fwnode;
4735}
Andy Shevchenko96489ae2020-04-08 19:09:01 +03004736EXPORT_SYMBOL_GPL(set_secondary_fwnode);
Johan Hovold4e75e1d2017-06-06 17:59:00 +02004737
4738/**
4739 * device_set_of_node_from_dev - reuse device-tree node of another device
4740 * @dev: device whose device-tree node is being set
4741 * @dev2: device whose device-tree node is being reused
4742 *
4743 * Takes another reference to the new device-tree node after first dropping
4744 * any reference held to the old node.
4745 */
4746void device_set_of_node_from_dev(struct device *dev, const struct device *dev2)
4747{
4748 of_node_put(dev->of_node);
4749 dev->of_node = of_node_get(dev2->of_node);
4750 dev->of_node_reused = true;
4751}
4752EXPORT_SYMBOL_GPL(device_set_of_node_from_dev);
Suzuki K Poulose65b66682019-06-14 18:54:01 +01004753
Suzuki K Poulose6cda08a2019-07-23 23:18:32 +01004754int device_match_name(struct device *dev, const void *name)
4755{
4756 return sysfs_streq(dev_name(dev), name);
4757}
4758EXPORT_SYMBOL_GPL(device_match_name);
4759
Suzuki K Poulose65b66682019-06-14 18:54:01 +01004760int device_match_of_node(struct device *dev, const void *np)
4761{
4762 return dev->of_node == np;
4763}
4764EXPORT_SYMBOL_GPL(device_match_of_node);
Suzuki K Poulose67843bb2019-07-23 23:18:34 +01004765
4766int device_match_fwnode(struct device *dev, const void *fwnode)
4767{
4768 return dev_fwnode(dev) == fwnode;
4769}
4770EXPORT_SYMBOL_GPL(device_match_fwnode);
Suzuki K Poulose4495dfd2019-07-23 23:18:35 +01004771
4772int device_match_devt(struct device *dev, const void *pdevt)
4773{
4774 return dev->devt == *(dev_t *)pdevt;
4775}
4776EXPORT_SYMBOL_GPL(device_match_devt);
Suzuki K Poulose00500142019-07-23 23:18:36 +01004777
4778int device_match_acpi_dev(struct device *dev, const void *adev)
4779{
4780 return ACPI_COMPANION(dev) == adev;
4781}
4782EXPORT_SYMBOL(device_match_acpi_dev);
Suzuki K Poulose6bf85ba2019-07-23 23:18:37 +01004783
4784int device_match_any(struct device *dev, const void *unused)
4785{
4786 return 1;
4787}
4788EXPORT_SYMBOL_GPL(device_match_any);