blob: 4a8bf8cda52bc77243fcffb8af554309aeed01bb [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 Kannan9528e0d2021-02-05 14:26:37 -0800153static void fw_devlink_purge_absent_suppliers(struct fwnode_handle *fwnode)
154{
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}
167
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100168#ifdef CONFIG_SRCU
169static DEFINE_MUTEX(device_links_lock);
170DEFINE_STATIC_SRCU(device_links_srcu);
171
172static inline void device_links_write_lock(void)
173{
174 mutex_lock(&device_links_lock);
175}
176
177static inline void device_links_write_unlock(void)
178{
179 mutex_unlock(&device_links_lock);
180}
181
Jules Irenge68464d72020-02-14 20:47:29 +0000182int device_links_read_lock(void) __acquires(&device_links_srcu)
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100183{
184 return srcu_read_lock(&device_links_srcu);
185}
186
Jules Irengeab7789c2020-02-14 20:47:30 +0000187void device_links_read_unlock(int idx) __releases(&device_links_srcu)
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100188{
189 srcu_read_unlock(&device_links_srcu, idx);
190}
Joel Fernandes (Google)c2fa1e12019-07-16 18:12:25 -0400191
192int device_links_read_lock_held(void)
193{
194 return srcu_read_lock_held(&device_links_srcu);
195}
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100196#else /* !CONFIG_SRCU */
197static DECLARE_RWSEM(device_links_lock);
198
199static inline void device_links_write_lock(void)
200{
201 down_write(&device_links_lock);
202}
203
204static inline void device_links_write_unlock(void)
205{
206 up_write(&device_links_lock);
207}
208
209int device_links_read_lock(void)
210{
211 down_read(&device_links_lock);
212 return 0;
213}
214
215void device_links_read_unlock(int not_used)
216{
217 up_read(&device_links_lock);
218}
Joel Fernandes (Google)c2fa1e12019-07-16 18:12:25 -0400219
220#ifdef CONFIG_DEBUG_LOCK_ALLOC
221int device_links_read_lock_held(void)
222{
223 return lockdep_is_held(&device_links_lock);
224}
225#endif
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100226#endif /* !CONFIG_SRCU */
227
Rafael J. Wysocki3d1cf432021-01-15 19:30:51 +0100228static bool device_is_ancestor(struct device *dev, struct device *target)
229{
230 while (target->parent) {
231 target = target->parent;
232 if (dev == target)
233 return true;
234 }
235 return false;
236}
237
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100238/**
239 * device_is_dependent - Check if one device depends on another one
240 * @dev: Device to check dependencies for.
241 * @target: Device to check against.
242 *
243 * Check if @target depends on @dev or any device dependent on it (its child or
244 * its consumer etc). Return 1 if that is the case or 0 otherwise.
245 */
Saravana Kannan7d34ca32020-06-09 18:19:33 -0700246int device_is_dependent(struct device *dev, void *target)
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100247{
248 struct device_link *link;
249 int ret;
250
Rafael J. Wysocki3d1cf432021-01-15 19:30:51 +0100251 /*
252 * The "ancestors" check is needed to catch the case when the target
253 * device has not been completely initialized yet and it is still
254 * missing from the list of children of its parent device.
255 */
256 if (dev == target || device_is_ancestor(dev, target))
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100257 return 1;
258
259 ret = device_for_each_child(dev, target, device_is_dependent);
260 if (ret)
261 return ret;
262
263 list_for_each_entry(link, &dev->links.consumers, s_node) {
Saravana Kannan4b9bbb22020-12-17 19:17:00 -0800264 if ((link->flags & ~DL_FLAG_INFERRED) ==
265 (DL_FLAG_SYNC_STATE_ONLY | DL_FLAG_MANAGED))
Saravana Kannan05ef9832019-10-28 15:00:22 -0700266 continue;
267
Benjamin Gaignarde16f4f32018-07-16 13:37:44 +0200268 if (link->consumer == target)
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100269 return 1;
270
271 ret = device_is_dependent(link->consumer, target);
272 if (ret)
273 break;
274 }
275 return ret;
276}
277
Rafael J. Wysocki515db262019-07-16 17:21:06 +0200278static void device_link_init_status(struct device_link *link,
279 struct device *consumer,
280 struct device *supplier)
281{
282 switch (supplier->links.status) {
283 case DL_DEV_PROBING:
284 switch (consumer->links.status) {
285 case DL_DEV_PROBING:
286 /*
287 * A consumer driver can create a link to a supplier
288 * that has not completed its probing yet as long as it
289 * knows that the supplier is already functional (for
290 * example, it has just acquired some resources from the
291 * supplier).
292 */
293 link->status = DL_STATE_CONSUMER_PROBE;
294 break;
295 default:
296 link->status = DL_STATE_DORMANT;
297 break;
298 }
299 break;
300 case DL_DEV_DRIVER_BOUND:
301 switch (consumer->links.status) {
302 case DL_DEV_PROBING:
303 link->status = DL_STATE_CONSUMER_PROBE;
304 break;
305 case DL_DEV_DRIVER_BOUND:
306 link->status = DL_STATE_ACTIVE;
307 break;
308 default:
309 link->status = DL_STATE_AVAILABLE;
310 break;
311 }
312 break;
313 case DL_DEV_UNBINDING:
314 link->status = DL_STATE_SUPPLIER_UNBIND;
315 break;
316 default:
317 link->status = DL_STATE_DORMANT;
318 break;
319 }
320}
321
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100322static int device_reorder_to_tail(struct device *dev, void *not_used)
323{
324 struct device_link *link;
325
326 /*
327 * Devices that have not been registered yet will be put to the ends
328 * of the lists during the registration, so skip them here.
329 */
330 if (device_is_registered(dev))
331 devices_kset_move_last(dev);
332
333 if (device_pm_initialized(dev))
334 device_pm_move_last(dev);
335
336 device_for_each_child(dev, NULL, device_reorder_to_tail);
Saravana Kannan05ef9832019-10-28 15:00:22 -0700337 list_for_each_entry(link, &dev->links.consumers, s_node) {
Saravana Kannan4b9bbb22020-12-17 19:17:00 -0800338 if ((link->flags & ~DL_FLAG_INFERRED) ==
339 (DL_FLAG_SYNC_STATE_ONLY | DL_FLAG_MANAGED))
Saravana Kannan05ef9832019-10-28 15:00:22 -0700340 continue;
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100341 device_reorder_to_tail(link->consumer, NULL);
Saravana Kannan05ef9832019-10-28 15:00:22 -0700342 }
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100343
344 return 0;
345}
346
347/**
Feng Kan494fd7b2018-04-10 16:57:06 -0700348 * device_pm_move_to_tail - Move set of devices to the end of device lists
349 * @dev: Device to move
350 *
351 * This is a device_reorder_to_tail() wrapper taking the requisite locks.
352 *
353 * It moves the @dev along with all of its children and all of its consumers
354 * to the ends of the device_kset and dpm_list, recursively.
355 */
356void device_pm_move_to_tail(struct device *dev)
357{
358 int idx;
359
360 idx = device_links_read_lock();
361 device_pm_lock();
362 device_reorder_to_tail(dev, NULL);
363 device_pm_unlock();
364 device_links_read_unlock(idx);
365}
366
Saravana Kannan287905e2020-05-21 12:17:58 -0700367#define to_devlink(dev) container_of((dev), struct device_link, link_dev)
368
369static ssize_t status_show(struct device *dev,
Joe Perches948b3ed2020-09-16 13:40:42 -0700370 struct device_attribute *attr, char *buf)
Saravana Kannan287905e2020-05-21 12:17:58 -0700371{
Joe Perches948b3ed2020-09-16 13:40:42 -0700372 const char *output;
Saravana Kannan287905e2020-05-21 12:17:58 -0700373
374 switch (to_devlink(dev)->status) {
375 case DL_STATE_NONE:
Joe Perches948b3ed2020-09-16 13:40:42 -0700376 output = "not tracked";
377 break;
Saravana Kannan287905e2020-05-21 12:17:58 -0700378 case DL_STATE_DORMANT:
Joe Perches948b3ed2020-09-16 13:40:42 -0700379 output = "dormant";
380 break;
Saravana Kannan287905e2020-05-21 12:17:58 -0700381 case DL_STATE_AVAILABLE:
Joe Perches948b3ed2020-09-16 13:40:42 -0700382 output = "available";
383 break;
Saravana Kannan287905e2020-05-21 12:17:58 -0700384 case DL_STATE_CONSUMER_PROBE:
Joe Perches948b3ed2020-09-16 13:40:42 -0700385 output = "consumer probing";
386 break;
Saravana Kannan287905e2020-05-21 12:17:58 -0700387 case DL_STATE_ACTIVE:
Joe Perches948b3ed2020-09-16 13:40:42 -0700388 output = "active";
389 break;
Saravana Kannan287905e2020-05-21 12:17:58 -0700390 case DL_STATE_SUPPLIER_UNBIND:
Joe Perches948b3ed2020-09-16 13:40:42 -0700391 output = "supplier unbinding";
392 break;
Saravana Kannan287905e2020-05-21 12:17:58 -0700393 default:
Joe Perches948b3ed2020-09-16 13:40:42 -0700394 output = "unknown";
395 break;
Saravana Kannan287905e2020-05-21 12:17:58 -0700396 }
Joe Perches948b3ed2020-09-16 13:40:42 -0700397
398 return sysfs_emit(buf, "%s\n", output);
Saravana Kannan287905e2020-05-21 12:17:58 -0700399}
400static DEVICE_ATTR_RO(status);
401
402static ssize_t auto_remove_on_show(struct device *dev,
403 struct device_attribute *attr, char *buf)
404{
405 struct device_link *link = to_devlink(dev);
Joe Perches973c3912020-09-16 13:40:40 -0700406 const char *output;
Saravana Kannan287905e2020-05-21 12:17:58 -0700407
408 if (link->flags & DL_FLAG_AUTOREMOVE_SUPPLIER)
Joe Perches973c3912020-09-16 13:40:40 -0700409 output = "supplier unbind";
Saravana Kannan287905e2020-05-21 12:17:58 -0700410 else if (link->flags & DL_FLAG_AUTOREMOVE_CONSUMER)
Joe Perches973c3912020-09-16 13:40:40 -0700411 output = "consumer unbind";
Saravana Kannan287905e2020-05-21 12:17:58 -0700412 else
Joe Perches973c3912020-09-16 13:40:40 -0700413 output = "never";
Saravana Kannan287905e2020-05-21 12:17:58 -0700414
Joe Perches973c3912020-09-16 13:40:40 -0700415 return sysfs_emit(buf, "%s\n", output);
Saravana Kannan287905e2020-05-21 12:17:58 -0700416}
417static DEVICE_ATTR_RO(auto_remove_on);
418
419static ssize_t runtime_pm_show(struct device *dev,
420 struct device_attribute *attr, char *buf)
421{
422 struct device_link *link = to_devlink(dev);
423
Joe Perchesaa838892020-09-16 13:40:39 -0700424 return sysfs_emit(buf, "%d\n", !!(link->flags & DL_FLAG_PM_RUNTIME));
Saravana Kannan287905e2020-05-21 12:17:58 -0700425}
426static DEVICE_ATTR_RO(runtime_pm);
427
428static ssize_t sync_state_only_show(struct device *dev,
429 struct device_attribute *attr, char *buf)
430{
431 struct device_link *link = to_devlink(dev);
432
Joe Perchesaa838892020-09-16 13:40:39 -0700433 return sysfs_emit(buf, "%d\n",
434 !!(link->flags & DL_FLAG_SYNC_STATE_ONLY));
Saravana Kannan287905e2020-05-21 12:17:58 -0700435}
436static DEVICE_ATTR_RO(sync_state_only);
437
438static struct attribute *devlink_attrs[] = {
439 &dev_attr_status.attr,
440 &dev_attr_auto_remove_on.attr,
441 &dev_attr_runtime_pm.attr,
442 &dev_attr_sync_state_only.attr,
443 NULL,
444};
445ATTRIBUTE_GROUPS(devlink);
446
Saravana Kannan843e6002020-07-16 14:45:23 -0700447static void device_link_free(struct device_link *link)
448{
449 while (refcount_dec_not_one(&link->rpm_active))
450 pm_runtime_put(link->supplier);
451
452 put_device(link->consumer);
453 put_device(link->supplier);
454 kfree(link);
455}
456
457#ifdef CONFIG_SRCU
458static void __device_link_free_srcu(struct rcu_head *rhead)
459{
460 device_link_free(container_of(rhead, struct device_link, rcu_head));
461}
462
Saravana Kannan287905e2020-05-21 12:17:58 -0700463static void devlink_dev_release(struct device *dev)
464{
Saravana Kannan843e6002020-07-16 14:45:23 -0700465 struct device_link *link = to_devlink(dev);
466
467 call_srcu(&device_links_srcu, &link->rcu_head, __device_link_free_srcu);
Saravana Kannan287905e2020-05-21 12:17:58 -0700468}
Saravana Kannan843e6002020-07-16 14:45:23 -0700469#else
470static void devlink_dev_release(struct device *dev)
471{
472 device_link_free(to_devlink(dev));
473}
474#endif
Saravana Kannan287905e2020-05-21 12:17:58 -0700475
476static struct class devlink_class = {
477 .name = "devlink",
478 .owner = THIS_MODULE,
479 .dev_groups = devlink_groups,
480 .dev_release = devlink_dev_release,
481};
482
483static int devlink_add_symlinks(struct device *dev,
484 struct class_interface *class_intf)
485{
486 int ret;
487 size_t len;
488 struct device_link *link = to_devlink(dev);
489 struct device *sup = link->supplier;
490 struct device *con = link->consumer;
491 char *buf;
492
Saravana Kannane020ff62021-01-10 09:54:07 -0800493 len = max(strlen(dev_bus_name(sup)) + strlen(dev_name(sup)),
494 strlen(dev_bus_name(con)) + strlen(dev_name(con)));
495 len += strlen(":");
Saravana Kannan287905e2020-05-21 12:17:58 -0700496 len += strlen("supplier:") + 1;
497 buf = kzalloc(len, GFP_KERNEL);
498 if (!buf)
499 return -ENOMEM;
500
501 ret = sysfs_create_link(&link->link_dev.kobj, &sup->kobj, "supplier");
502 if (ret)
503 goto out;
504
505 ret = sysfs_create_link(&link->link_dev.kobj, &con->kobj, "consumer");
506 if (ret)
507 goto err_con;
508
Saravana Kannane020ff62021-01-10 09:54:07 -0800509 snprintf(buf, len, "consumer:%s:%s", dev_bus_name(con), dev_name(con));
Saravana Kannan287905e2020-05-21 12:17:58 -0700510 ret = sysfs_create_link(&sup->kobj, &link->link_dev.kobj, buf);
511 if (ret)
512 goto err_con_dev;
513
Saravana Kannane020ff62021-01-10 09:54:07 -0800514 snprintf(buf, len, "supplier:%s:%s", dev_bus_name(sup), dev_name(sup));
Saravana Kannan287905e2020-05-21 12:17:58 -0700515 ret = sysfs_create_link(&con->kobj, &link->link_dev.kobj, buf);
516 if (ret)
517 goto err_sup_dev;
518
519 goto out;
520
521err_sup_dev:
Saravana Kannane020ff62021-01-10 09:54:07 -0800522 snprintf(buf, len, "consumer:%s:%s", dev_bus_name(con), dev_name(con));
Saravana Kannan287905e2020-05-21 12:17:58 -0700523 sysfs_remove_link(&sup->kobj, buf);
524err_con_dev:
525 sysfs_remove_link(&link->link_dev.kobj, "consumer");
526err_con:
527 sysfs_remove_link(&link->link_dev.kobj, "supplier");
528out:
529 kfree(buf);
530 return ret;
531}
532
533static void devlink_remove_symlinks(struct device *dev,
534 struct class_interface *class_intf)
535{
536 struct device_link *link = to_devlink(dev);
537 size_t len;
538 struct device *sup = link->supplier;
539 struct device *con = link->consumer;
540 char *buf;
541
542 sysfs_remove_link(&link->link_dev.kobj, "consumer");
543 sysfs_remove_link(&link->link_dev.kobj, "supplier");
544
Saravana Kannane020ff62021-01-10 09:54:07 -0800545 len = max(strlen(dev_bus_name(sup)) + strlen(dev_name(sup)),
546 strlen(dev_bus_name(con)) + strlen(dev_name(con)));
547 len += strlen(":");
Saravana Kannan287905e2020-05-21 12:17:58 -0700548 len += strlen("supplier:") + 1;
549 buf = kzalloc(len, GFP_KERNEL);
550 if (!buf) {
551 WARN(1, "Unable to properly free device link symlinks!\n");
552 return;
553 }
554
Saravana Kannane020ff62021-01-10 09:54:07 -0800555 snprintf(buf, len, "supplier:%s:%s", dev_bus_name(sup), dev_name(sup));
Saravana Kannan287905e2020-05-21 12:17:58 -0700556 sysfs_remove_link(&con->kobj, buf);
Saravana Kannane020ff62021-01-10 09:54:07 -0800557 snprintf(buf, len, "consumer:%s:%s", dev_bus_name(con), dev_name(con));
Saravana Kannan287905e2020-05-21 12:17:58 -0700558 sysfs_remove_link(&sup->kobj, buf);
559 kfree(buf);
560}
561
562static struct class_interface devlink_class_intf = {
563 .class = &devlink_class,
564 .add_dev = devlink_add_symlinks,
565 .remove_dev = devlink_remove_symlinks,
566};
567
568static int __init devlink_class_init(void)
569{
570 int ret;
571
572 ret = class_register(&devlink_class);
573 if (ret)
574 return ret;
575
576 ret = class_interface_register(&devlink_class_intf);
577 if (ret)
578 class_unregister(&devlink_class);
579
580 return ret;
581}
582postcore_initcall(devlink_class_init);
583
Rafael J. Wysocki515db262019-07-16 17:21:06 +0200584#define DL_MANAGED_LINK_FLAGS (DL_FLAG_AUTOREMOVE_CONSUMER | \
585 DL_FLAG_AUTOREMOVE_SUPPLIER | \
Saravana Kannan05ef9832019-10-28 15:00:22 -0700586 DL_FLAG_AUTOPROBE_CONSUMER | \
Saravana Kannan4b9bbb22020-12-17 19:17:00 -0800587 DL_FLAG_SYNC_STATE_ONLY | \
588 DL_FLAG_INFERRED)
Rafael J. Wysocki515db262019-07-16 17:21:06 +0200589
Rafael J. Wysockifb583c82019-07-30 11:28:57 +0200590#define DL_ADD_VALID_FLAGS (DL_MANAGED_LINK_FLAGS | DL_FLAG_STATELESS | \
591 DL_FLAG_PM_RUNTIME | DL_FLAG_RPM_ACTIVE)
592
Feng Kan494fd7b2018-04-10 16:57:06 -0700593/**
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100594 * device_link_add - Create a link between two devices.
595 * @consumer: Consumer end of the link.
596 * @supplier: Supplier end of the link.
597 * @flags: Link flags.
598 *
Rafael J. Wysocki21d5c572016-10-30 17:32:31 +0100599 * The caller is responsible for the proper synchronization of the link creation
600 * with runtime PM. First, setting the DL_FLAG_PM_RUNTIME flag will cause the
601 * runtime PM framework to take the link into account. Second, if the
602 * DL_FLAG_RPM_ACTIVE flag is set in addition to it, the supplier devices will
Thierry Redingd475f8e2020-11-27 11:46:30 +0100603 * be forced into the active meta state and reference-counted upon the creation
Rafael J. Wysocki21d5c572016-10-30 17:32:31 +0100604 * of the link. If DL_FLAG_PM_RUNTIME is not set, DL_FLAG_RPM_ACTIVE will be
605 * ignored.
606 *
Rafael J. Wysocki515db262019-07-16 17:21:06 +0200607 * If DL_FLAG_STATELESS is set in @flags, the caller of this function is
608 * expected to release the link returned by it directly with the help of either
609 * device_link_del() or device_link_remove().
Rafael J. Wysocki72175d42019-02-01 01:58:33 +0100610 *
611 * If that flag is not set, however, the caller of this function is handing the
612 * management of the link over to the driver core entirely and its return value
613 * can only be used to check whether or not the link is present. In that case,
614 * the DL_FLAG_AUTOREMOVE_CONSUMER and DL_FLAG_AUTOREMOVE_SUPPLIER device link
615 * flags can be used to indicate to the driver core when the link can be safely
616 * deleted. Namely, setting one of them in @flags indicates to the driver core
617 * that the link is not going to be used (by the given caller of this function)
618 * after unbinding the consumer or supplier driver, respectively, from its
619 * device, so the link can be deleted at that point. If none of them is set,
620 * the link will be maintained until one of the devices pointed to by it (either
621 * the consumer or the supplier) is unregistered.
Rafael J. Wysockic8d50982019-02-01 01:45:55 +0100622 *
Rafael J. Wysockie7dd4012019-02-01 01:59:42 +0100623 * Also, if DL_FLAG_STATELESS, DL_FLAG_AUTOREMOVE_CONSUMER and
624 * DL_FLAG_AUTOREMOVE_SUPPLIER are not set in @flags (that is, a persistent
625 * managed device link is being added), the DL_FLAG_AUTOPROBE_CONSUMER flag can
Thierry Redingd475f8e2020-11-27 11:46:30 +0100626 * be used to request the driver core to automatically probe for a consumer
Rafael J. Wysockie7dd4012019-02-01 01:59:42 +0100627 * driver after successfully binding a driver to the supplier device.
628 *
Rafael J. Wysocki515db262019-07-16 17:21:06 +0200629 * The combination of DL_FLAG_STATELESS and one of DL_FLAG_AUTOREMOVE_CONSUMER,
630 * DL_FLAG_AUTOREMOVE_SUPPLIER, or DL_FLAG_AUTOPROBE_CONSUMER set in @flags at
631 * the same time is invalid and will cause NULL to be returned upfront.
632 * However, if a device link between the given @consumer and @supplier pair
633 * exists already when this function is called for them, the existing link will
634 * be returned regardless of its current type and status (the link's flags may
635 * be modified then). The caller of this function is then expected to treat
636 * the link as though it has just been created, so (in particular) if
637 * DL_FLAG_STATELESS was passed in @flags, the link needs to be released
638 * explicitly when not needed any more (as stated above).
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100639 *
640 * A side effect of the link creation is re-ordering of dpm_list and the
641 * devices_kset list by moving the consumer device and all devices depending
642 * on it to the ends of these lists (that does not happen to devices that have
643 * not been registered when this function is called).
644 *
645 * The supplier device is required to be registered when this function is called
646 * and NULL will be returned if that is not the case. The consumer device need
Lukas Wunner64df1142016-12-04 13:10:04 +0100647 * not be registered, however.
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100648 */
649struct device_link *device_link_add(struct device *consumer,
650 struct device *supplier, u32 flags)
651{
652 struct device_link *link;
653
Rafael J. Wysockifb583c82019-07-30 11:28:57 +0200654 if (!consumer || !supplier || flags & ~DL_ADD_VALID_FLAGS ||
Rafael J. Wysocki515db262019-07-16 17:21:06 +0200655 (flags & DL_FLAG_STATELESS && flags & DL_MANAGED_LINK_FLAGS) ||
Saravana Kannan05ef9832019-10-28 15:00:22 -0700656 (flags & DL_FLAG_SYNC_STATE_ONLY &&
Saravana Kannan4b9bbb22020-12-17 19:17:00 -0800657 (flags & ~DL_FLAG_INFERRED) != DL_FLAG_SYNC_STATE_ONLY) ||
Rafael J. Wysockie7dd4012019-02-01 01:59:42 +0100658 (flags & DL_FLAG_AUTOPROBE_CONSUMER &&
659 flags & (DL_FLAG_AUTOREMOVE_CONSUMER |
660 DL_FLAG_AUTOREMOVE_SUPPLIER)))
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100661 return NULL;
662
Rafael J. Wysocki5db25c92019-02-01 01:47:53 +0100663 if (flags & DL_FLAG_PM_RUNTIME && flags & DL_FLAG_RPM_ACTIVE) {
664 if (pm_runtime_get_sync(supplier) < 0) {
665 pm_runtime_put_noidle(supplier);
666 return NULL;
667 }
Rafael J. Wysocki5db25c92019-02-01 01:47:53 +0100668 }
669
Rafael J. Wysocki515db262019-07-16 17:21:06 +0200670 if (!(flags & DL_FLAG_STATELESS))
671 flags |= DL_FLAG_MANAGED;
672
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100673 device_links_write_lock();
674 device_pm_lock();
675
676 /*
677 * If the supplier has not been fully registered yet or there is a
Saravana Kannan05ef9832019-10-28 15:00:22 -0700678 * reverse (non-SYNC_STATE_ONLY) dependency between the consumer and
679 * the supplier already in the graph, return NULL. If the link is a
680 * SYNC_STATE_ONLY link, we don't check for reverse dependencies
681 * because it only affects sync_state() callbacks.
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100682 */
683 if (!device_pm_initialized(supplier)
Saravana Kannan05ef9832019-10-28 15:00:22 -0700684 || (!(flags & DL_FLAG_SYNC_STATE_ONLY) &&
685 device_is_dependent(consumer, supplier))) {
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100686 link = NULL;
687 goto out;
688 }
689
Rafael J. Wysocki72175d42019-02-01 01:58:33 +0100690 /*
Saravana Kannanac66c5b2020-11-20 18:02:24 -0800691 * SYNC_STATE_ONLY links are useless once a consumer device has probed.
692 * So, only create it if the consumer hasn't probed yet.
693 */
694 if (flags & DL_FLAG_SYNC_STATE_ONLY &&
695 consumer->links.status != DL_DEV_NO_DRIVER &&
696 consumer->links.status != DL_DEV_PROBING) {
697 link = NULL;
698 goto out;
699 }
700
701 /*
Rafael J. Wysocki72175d42019-02-01 01:58:33 +0100702 * DL_FLAG_AUTOREMOVE_SUPPLIER indicates that the link will be needed
703 * longer than for DL_FLAG_AUTOREMOVE_CONSUMER and setting them both
704 * together doesn't make sense, so prefer DL_FLAG_AUTOREMOVE_SUPPLIER.
705 */
706 if (flags & DL_FLAG_AUTOREMOVE_SUPPLIER)
707 flags &= ~DL_FLAG_AUTOREMOVE_CONSUMER;
708
Rafael J. Wysockif265df52019-02-01 01:46:54 +0100709 list_for_each_entry(link, &supplier->links.consumers, s_node) {
710 if (link->consumer != consumer)
711 continue;
712
Saravana Kannan4b9bbb22020-12-17 19:17:00 -0800713 if (link->flags & DL_FLAG_INFERRED &&
714 !(flags & DL_FLAG_INFERRED))
715 link->flags &= ~DL_FLAG_INFERRED;
716
Rafael J. Wysockie2f3cd82019-02-01 01:49:14 +0100717 if (flags & DL_FLAG_PM_RUNTIME) {
718 if (!(link->flags & DL_FLAG_PM_RUNTIME)) {
Rafael J. Wysocki4c06c4e2019-02-12 13:08:10 +0100719 pm_runtime_new_link(consumer);
Rafael J. Wysockie2f3cd82019-02-01 01:49:14 +0100720 link->flags |= DL_FLAG_PM_RUNTIME;
721 }
722 if (flags & DL_FLAG_RPM_ACTIVE)
Rafael J. Wysocki36003d42019-02-19 17:53:26 +0100723 refcount_inc(&link->rpm_active);
Rafael J. Wysockie2f3cd82019-02-01 01:49:14 +0100724 }
725
Rafael J. Wysocki72175d42019-02-01 01:58:33 +0100726 if (flags & DL_FLAG_STATELESS) {
727 kref_get(&link->kref);
Saravana Kannan05ef9832019-10-28 15:00:22 -0700728 if (link->flags & DL_FLAG_SYNC_STATE_ONLY &&
Saravana Kannan44e96042020-05-19 21:36:26 -0700729 !(link->flags & DL_FLAG_STATELESS)) {
730 link->flags |= DL_FLAG_STATELESS;
Saravana Kannan05ef9832019-10-28 15:00:22 -0700731 goto reorder;
Saravana Kannan44e96042020-05-19 21:36:26 -0700732 } else {
733 link->flags |= DL_FLAG_STATELESS;
Saravana Kannan05ef9832019-10-28 15:00:22 -0700734 goto out;
Saravana Kannan44e96042020-05-19 21:36:26 -0700735 }
Rafael J. Wysocki72175d42019-02-01 01:58:33 +0100736 }
737
738 /*
739 * If the life time of the link following from the new flags is
740 * longer than indicated by the flags of the existing link,
741 * update the existing link to stay around longer.
742 */
743 if (flags & DL_FLAG_AUTOREMOVE_SUPPLIER) {
744 if (link->flags & DL_FLAG_AUTOREMOVE_CONSUMER) {
745 link->flags &= ~DL_FLAG_AUTOREMOVE_CONSUMER;
746 link->flags |= DL_FLAG_AUTOREMOVE_SUPPLIER;
747 }
748 } else if (!(flags & DL_FLAG_AUTOREMOVE_CONSUMER)) {
749 link->flags &= ~(DL_FLAG_AUTOREMOVE_CONSUMER |
750 DL_FLAG_AUTOREMOVE_SUPPLIER);
751 }
Rafael J. Wysocki515db262019-07-16 17:21:06 +0200752 if (!(link->flags & DL_FLAG_MANAGED)) {
753 kref_get(&link->kref);
754 link->flags |= DL_FLAG_MANAGED;
755 device_link_init_status(link, consumer, supplier);
756 }
Saravana Kannan05ef9832019-10-28 15:00:22 -0700757 if (link->flags & DL_FLAG_SYNC_STATE_ONLY &&
758 !(flags & DL_FLAG_SYNC_STATE_ONLY)) {
759 link->flags &= ~DL_FLAG_SYNC_STATE_ONLY;
760 goto reorder;
761 }
762
Rafael J. Wysockif265df52019-02-01 01:46:54 +0100763 goto out;
764 }
765
Rafael J. Wysocki21d5c572016-10-30 17:32:31 +0100766 link = kzalloc(sizeof(*link), GFP_KERNEL);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100767 if (!link)
768 goto out;
769
Rafael J. Wysockie2f3cd82019-02-01 01:49:14 +0100770 refcount_set(&link->rpm_active, 1);
771
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100772 get_device(supplier);
773 link->supplier = supplier;
774 INIT_LIST_HEAD(&link->s_node);
775 get_device(consumer);
776 link->consumer = consumer;
777 INIT_LIST_HEAD(&link->c_node);
778 link->flags = flags;
Lukas Wunneread18c22018-02-10 19:27:12 +0100779 kref_init(&link->kref);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100780
Saravana Kannan287905e2020-05-21 12:17:58 -0700781 link->link_dev.class = &devlink_class;
782 device_set_pm_not_required(&link->link_dev);
Saravana Kannane020ff62021-01-10 09:54:07 -0800783 dev_set_name(&link->link_dev, "%s:%s--%s:%s",
784 dev_bus_name(supplier), dev_name(supplier),
785 dev_bus_name(consumer), dev_name(consumer));
Saravana Kannan287905e2020-05-21 12:17:58 -0700786 if (device_register(&link->link_dev)) {
787 put_device(consumer);
788 put_device(supplier);
789 kfree(link);
790 link = NULL;
791 goto out;
792 }
793
794 if (flags & DL_FLAG_PM_RUNTIME) {
795 if (flags & DL_FLAG_RPM_ACTIVE)
796 refcount_inc(&link->rpm_active);
797
798 pm_runtime_new_link(consumer);
799 }
800
Lukas Wunner64df1142016-12-04 13:10:04 +0100801 /* Determine the initial link state. */
Rafael J. Wysocki515db262019-07-16 17:21:06 +0200802 if (flags & DL_FLAG_STATELESS)
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100803 link->status = DL_STATE_NONE;
Rafael J. Wysocki515db262019-07-16 17:21:06 +0200804 else
805 device_link_init_status(link, consumer, supplier);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100806
807 /*
Rafael J. Wysocki15cfb092019-02-01 01:50:39 +0100808 * Some callers expect the link creation during consumer driver probe to
809 * resume the supplier even without DL_FLAG_RPM_ACTIVE.
810 */
811 if (link->status == DL_STATE_CONSUMER_PROBE &&
812 flags & DL_FLAG_PM_RUNTIME)
813 pm_runtime_resume(supplier);
814
Saravana Kannan21c27f02020-05-18 23:30:00 -0700815 list_add_tail_rcu(&link->s_node, &supplier->links.consumers);
816 list_add_tail_rcu(&link->c_node, &consumer->links.suppliers);
817
Saravana Kannan05ef9832019-10-28 15:00:22 -0700818 if (flags & DL_FLAG_SYNC_STATE_ONLY) {
819 dev_dbg(consumer,
820 "Linked as a sync state only consumer to %s\n",
821 dev_name(supplier));
822 goto out;
823 }
Saravana Kannan21c27f02020-05-18 23:30:00 -0700824
Saravana Kannan05ef9832019-10-28 15:00:22 -0700825reorder:
Rafael J. Wysocki15cfb092019-02-01 01:50:39 +0100826 /*
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100827 * Move the consumer and all of the devices depending on it to the end
828 * of dpm_list and the devices_kset list.
829 *
830 * It is necessary to hold dpm_list locked throughout all that or else
831 * we may end up suspending with a wrong ordering of it.
832 */
833 device_reorder_to_tail(consumer, NULL);
834
Jerome Brunet8a4b3262018-12-21 17:23:41 +0100835 dev_dbg(consumer, "Linked as a consumer to %s\n", dev_name(supplier));
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100836
Saravana Kannan21c27f02020-05-18 23:30:00 -0700837out:
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100838 device_pm_unlock();
839 device_links_write_unlock();
Rafael J. Wysocki5db25c92019-02-01 01:47:53 +0100840
Rafael J. Wysockie2f3cd82019-02-01 01:49:14 +0100841 if ((flags & DL_FLAG_PM_RUNTIME && flags & DL_FLAG_RPM_ACTIVE) && !link)
Rafael J. Wysocki5db25c92019-02-01 01:47:53 +0100842 pm_runtime_put(supplier);
843
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100844 return link;
845}
846EXPORT_SYMBOL_GPL(device_link_add);
847
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100848#ifdef CONFIG_SRCU
Lukas Wunneread18c22018-02-10 19:27:12 +0100849static void __device_link_del(struct kref *kref)
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100850{
Lukas Wunneread18c22018-02-10 19:27:12 +0100851 struct device_link *link = container_of(kref, struct device_link, kref);
852
Jerome Brunet8a4b3262018-12-21 17:23:41 +0100853 dev_dbg(link->consumer, "Dropping the link to %s\n",
854 dev_name(link->supplier));
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100855
Rafael J. Wysockie0e398e2020-10-21 21:12:15 +0200856 pm_runtime_drop_link(link);
Rafael J. Wysockibaa88092016-10-30 17:32:43 +0100857
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100858 list_del_rcu(&link->s_node);
859 list_del_rcu(&link->c_node);
Saravana Kannan843e6002020-07-16 14:45:23 -0700860 device_unregister(&link->link_dev);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100861}
862#else /* !CONFIG_SRCU */
Lukas Wunneread18c22018-02-10 19:27:12 +0100863static void __device_link_del(struct kref *kref)
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100864{
Lukas Wunneread18c22018-02-10 19:27:12 +0100865 struct device_link *link = container_of(kref, struct device_link, kref);
866
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100867 dev_info(link->consumer, "Dropping the link to %s\n",
868 dev_name(link->supplier));
869
Rafael J. Wysockie0e398e2020-10-21 21:12:15 +0200870 pm_runtime_drop_link(link);
Lukas Wunner433986c2018-02-10 19:13:58 +0100871
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100872 list_del(&link->s_node);
873 list_del(&link->c_node);
Saravana Kannan843e6002020-07-16 14:45:23 -0700874 device_unregister(&link->link_dev);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100875}
876#endif /* !CONFIG_SRCU */
877
Rafael J. Wysocki72175d42019-02-01 01:58:33 +0100878static void device_link_put_kref(struct device_link *link)
879{
880 if (link->flags & DL_FLAG_STATELESS)
881 kref_put(&link->kref, __device_link_del);
882 else
883 WARN(1, "Unable to drop a managed device link reference\n");
884}
885
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100886/**
Rafael J. Wysocki72175d42019-02-01 01:58:33 +0100887 * device_link_del - Delete a stateless link between two devices.
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100888 * @link: Device link to delete.
889 *
890 * The caller must ensure proper synchronization of this function with runtime
Lukas Wunneread18c22018-02-10 19:27:12 +0100891 * PM. If the link was added multiple times, it needs to be deleted as often.
892 * Care is required for hotplugged devices: Their links are purged on removal
893 * and calling device_link_del() is then no longer allowed.
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100894 */
895void device_link_del(struct device_link *link)
896{
897 device_links_write_lock();
Rafael J. Wysocki72175d42019-02-01 01:58:33 +0100898 device_link_put_kref(link);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100899 device_links_write_unlock();
900}
901EXPORT_SYMBOL_GPL(device_link_del);
902
pascal pailletd8842212018-07-05 14:25:56 +0000903/**
Rafael J. Wysocki72175d42019-02-01 01:58:33 +0100904 * device_link_remove - Delete a stateless link between two devices.
pascal pailletd8842212018-07-05 14:25:56 +0000905 * @consumer: Consumer end of the link.
906 * @supplier: Supplier end of the link.
907 *
908 * The caller must ensure proper synchronization of this function with runtime
909 * PM.
910 */
911void device_link_remove(void *consumer, struct device *supplier)
912{
913 struct device_link *link;
914
915 if (WARN_ON(consumer == supplier))
916 return;
917
918 device_links_write_lock();
pascal pailletd8842212018-07-05 14:25:56 +0000919
920 list_for_each_entry(link, &supplier->links.consumers, s_node) {
921 if (link->consumer == consumer) {
Rafael J. Wysocki72175d42019-02-01 01:58:33 +0100922 device_link_put_kref(link);
pascal pailletd8842212018-07-05 14:25:56 +0000923 break;
924 }
925 }
926
pascal pailletd8842212018-07-05 14:25:56 +0000927 device_links_write_unlock();
928}
929EXPORT_SYMBOL_GPL(device_link_remove);
930
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100931static void device_links_missing_supplier(struct device *dev)
932{
933 struct device_link *link;
934
Saravana Kannan8c3e3152020-05-26 15:09:27 -0700935 list_for_each_entry(link, &dev->links.suppliers, c_node) {
936 if (link->status != DL_STATE_CONSUMER_PROBE)
937 continue;
938
939 if (link->supplier->links.status == DL_DEV_DRIVER_BOUND) {
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100940 WRITE_ONCE(link->status, DL_STATE_AVAILABLE);
Saravana Kannan8c3e3152020-05-26 15:09:27 -0700941 } else {
942 WARN_ON(!(link->flags & DL_FLAG_SYNC_STATE_ONLY));
943 WRITE_ONCE(link->status, DL_STATE_DORMANT);
944 }
945 }
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100946}
947
948/**
949 * device_links_check_suppliers - Check presence of supplier drivers.
950 * @dev: Consumer device.
951 *
952 * Check links from this device to any suppliers. Walk the list of the device's
953 * links to suppliers and see if all of them are available. If not, simply
954 * return -EPROBE_DEFER.
955 *
956 * We need to guarantee that the supplier will not go away after the check has
957 * been positive here. It only can go away in __device_release_driver() and
958 * that function checks the device's links to consumers. This means we need to
959 * mark the link as "consumer probe in progress" to make the supplier removal
960 * wait for us to complete (or bad things may happen).
961 *
Rafael J. Wysocki515db262019-07-16 17:21:06 +0200962 * Links without the DL_FLAG_MANAGED flag set are ignored.
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100963 */
964int device_links_check_suppliers(struct device *dev)
965{
966 struct device_link *link;
967 int ret = 0;
968
Saravana Kannane2ae9bc2019-09-04 14:11:21 -0700969 /*
970 * Device waiting for supplier to become available is not allowed to
971 * probe.
972 */
Saravana Kannan25ac86c2020-11-20 18:02:28 -0800973 mutex_lock(&fwnode_link_lock);
974 if (dev->fwnode && !list_empty(&dev->fwnode->suppliers) &&
975 !fw_devlink_is_permissive()) {
Saravana Kannan1f0dfa02020-12-17 19:16:59 -0800976 dev_dbg(dev, "probe deferral - wait for supplier %pfwP\n",
977 list_first_entry(&dev->fwnode->suppliers,
978 struct fwnode_link,
979 c_hook)->supplier);
Saravana Kannan25ac86c2020-11-20 18:02:28 -0800980 mutex_unlock(&fwnode_link_lock);
Saravana Kannane2ae9bc2019-09-04 14:11:21 -0700981 return -EPROBE_DEFER;
982 }
Saravana Kannan25ac86c2020-11-20 18:02:28 -0800983 mutex_unlock(&fwnode_link_lock);
Saravana Kannane2ae9bc2019-09-04 14:11:21 -0700984
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100985 device_links_write_lock();
986
987 list_for_each_entry(link, &dev->links.suppliers, c_node) {
Saravana Kannan8c3e3152020-05-26 15:09:27 -0700988 if (!(link->flags & DL_FLAG_MANAGED))
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100989 continue;
990
Saravana Kannan8c3e3152020-05-26 15:09:27 -0700991 if (link->status != DL_STATE_AVAILABLE &&
992 !(link->flags & DL_FLAG_SYNC_STATE_ONLY)) {
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100993 device_links_missing_supplier(dev);
Saravana Kannan1f0dfa02020-12-17 19:16:59 -0800994 dev_dbg(dev, "probe deferral - supplier %s not ready\n",
995 dev_name(link->supplier));
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100996 ret = -EPROBE_DEFER;
997 break;
998 }
999 WRITE_ONCE(link->status, DL_STATE_CONSUMER_PROBE);
1000 }
1001 dev->links.status = DL_DEV_PROBING;
1002
1003 device_links_write_unlock();
1004 return ret;
1005}
1006
Saravana Kannan26e77702019-11-14 14:56:45 -08001007/**
1008 * __device_links_queue_sync_state - Queue a device for sync_state() callback
1009 * @dev: Device to call sync_state() on
1010 * @list: List head to queue the @dev on
1011 *
1012 * Queues a device for a sync_state() callback when the device links write lock
1013 * isn't held. This allows the sync_state() execution flow to use device links
1014 * APIs. The caller must ensure this function is called with
1015 * device_links_write_lock() held.
1016 *
1017 * This function does a get_device() to make sure the device is not freed while
1018 * on this list.
1019 *
1020 * So the caller must also ensure that device_links_flush_sync_list() is called
1021 * as soon as the caller releases device_links_write_lock(). This is necessary
1022 * to make sure the sync_state() is called in a timely fashion and the
1023 * put_device() is called on this device.
1024 */
1025static void __device_links_queue_sync_state(struct device *dev,
1026 struct list_head *list)
Saravana Kannanfc5a2512019-09-04 14:11:23 -07001027{
1028 struct device_link *link;
1029
Saravana Kannan77036162020-02-21 00:05:10 -08001030 if (!dev_has_sync_state(dev))
1031 return;
Saravana Kannanfc5a2512019-09-04 14:11:23 -07001032 if (dev->state_synced)
1033 return;
1034
1035 list_for_each_entry(link, &dev->links.consumers, s_node) {
1036 if (!(link->flags & DL_FLAG_MANAGED))
1037 continue;
1038 if (link->status != DL_STATE_ACTIVE)
1039 return;
1040 }
1041
Saravana Kannan26e77702019-11-14 14:56:45 -08001042 /*
1043 * Set the flag here to avoid adding the same device to a list more
1044 * than once. This can happen if new consumers get added to the device
1045 * and probed before the list is flushed.
1046 */
Saravana Kannanfc5a2512019-09-04 14:11:23 -07001047 dev->state_synced = true;
Saravana Kannan26e77702019-11-14 14:56:45 -08001048
Saravana Kannan3b052a32020-11-20 18:02:17 -08001049 if (WARN_ON(!list_empty(&dev->links.defer_sync)))
Saravana Kannan26e77702019-11-14 14:56:45 -08001050 return;
1051
1052 get_device(dev);
Saravana Kannan3b052a32020-11-20 18:02:17 -08001053 list_add_tail(&dev->links.defer_sync, list);
Saravana Kannan26e77702019-11-14 14:56:45 -08001054}
1055
1056/**
1057 * device_links_flush_sync_list - Call sync_state() on a list of devices
1058 * @list: List of devices to call sync_state() on
Saravana Kannan21eb93f2020-02-21 00:05:08 -08001059 * @dont_lock_dev: Device for which lock is already held by the caller
Saravana Kannan26e77702019-11-14 14:56:45 -08001060 *
1061 * Calls sync_state() on all the devices that have been queued for it. This
Saravana Kannan21eb93f2020-02-21 00:05:08 -08001062 * function is used in conjunction with __device_links_queue_sync_state(). The
1063 * @dont_lock_dev parameter is useful when this function is called from a
1064 * context where a device lock is already held.
Saravana Kannan26e77702019-11-14 14:56:45 -08001065 */
Saravana Kannan21eb93f2020-02-21 00:05:08 -08001066static void device_links_flush_sync_list(struct list_head *list,
1067 struct device *dont_lock_dev)
Saravana Kannan26e77702019-11-14 14:56:45 -08001068{
1069 struct device *dev, *tmp;
1070
Saravana Kannan3b052a32020-11-20 18:02:17 -08001071 list_for_each_entry_safe(dev, tmp, list, links.defer_sync) {
1072 list_del_init(&dev->links.defer_sync);
Saravana Kannan26e77702019-11-14 14:56:45 -08001073
Saravana Kannan21eb93f2020-02-21 00:05:08 -08001074 if (dev != dont_lock_dev)
1075 device_lock(dev);
Saravana Kannan26e77702019-11-14 14:56:45 -08001076
1077 if (dev->bus->sync_state)
1078 dev->bus->sync_state(dev);
1079 else if (dev->driver && dev->driver->sync_state)
1080 dev->driver->sync_state(dev);
1081
Saravana Kannan21eb93f2020-02-21 00:05:08 -08001082 if (dev != dont_lock_dev)
1083 device_unlock(dev);
Saravana Kannan26e77702019-11-14 14:56:45 -08001084
1085 put_device(dev);
1086 }
Saravana Kannanfc5a2512019-09-04 14:11:23 -07001087}
1088
1089void device_links_supplier_sync_state_pause(void)
1090{
1091 device_links_write_lock();
1092 defer_sync_state_count++;
1093 device_links_write_unlock();
1094}
1095
1096void device_links_supplier_sync_state_resume(void)
1097{
1098 struct device *dev, *tmp;
Saravana Kannan26e77702019-11-14 14:56:45 -08001099 LIST_HEAD(sync_list);
Saravana Kannanfc5a2512019-09-04 14:11:23 -07001100
1101 device_links_write_lock();
1102 if (!defer_sync_state_count) {
1103 WARN(true, "Unmatched sync_state pause/resume!");
1104 goto out;
1105 }
1106 defer_sync_state_count--;
1107 if (defer_sync_state_count)
1108 goto out;
1109
Saravana Kannan3b052a32020-11-20 18:02:17 -08001110 list_for_each_entry_safe(dev, tmp, &deferred_sync, links.defer_sync) {
Saravana Kannan26e77702019-11-14 14:56:45 -08001111 /*
1112 * Delete from deferred_sync list before queuing it to
Saravana Kannan3b052a32020-11-20 18:02:17 -08001113 * sync_list because defer_sync is used for both lists.
Saravana Kannan26e77702019-11-14 14:56:45 -08001114 */
Saravana Kannan3b052a32020-11-20 18:02:17 -08001115 list_del_init(&dev->links.defer_sync);
Saravana Kannan26e77702019-11-14 14:56:45 -08001116 __device_links_queue_sync_state(dev, &sync_list);
Saravana Kannanfc5a2512019-09-04 14:11:23 -07001117 }
1118out:
1119 device_links_write_unlock();
Saravana Kannan26e77702019-11-14 14:56:45 -08001120
Saravana Kannan21eb93f2020-02-21 00:05:08 -08001121 device_links_flush_sync_list(&sync_list, NULL);
Saravana Kannanfc5a2512019-09-04 14:11:23 -07001122}
1123
1124static int sync_state_resume_initcall(void)
1125{
1126 device_links_supplier_sync_state_resume();
1127 return 0;
1128}
1129late_initcall(sync_state_resume_initcall);
1130
1131static void __device_links_supplier_defer_sync(struct device *sup)
1132{
Saravana Kannan3b052a32020-11-20 18:02:17 -08001133 if (list_empty(&sup->links.defer_sync) && dev_has_sync_state(sup))
1134 list_add_tail(&sup->links.defer_sync, &deferred_sync);
Saravana Kannanfc5a2512019-09-04 14:11:23 -07001135}
1136
Saravana Kannan21c27f02020-05-18 23:30:00 -07001137static void device_link_drop_managed(struct device_link *link)
1138{
1139 link->flags &= ~DL_FLAG_MANAGED;
1140 WRITE_ONCE(link->status, DL_STATE_NONE);
1141 kref_put(&link->kref, __device_link_del);
1142}
1143
Saravana Kannanda6d6472020-05-21 12:18:00 -07001144static ssize_t waiting_for_supplier_show(struct device *dev,
1145 struct device_attribute *attr,
1146 char *buf)
1147{
1148 bool val;
1149
1150 device_lock(dev);
Saravana Kannan25ac86c2020-11-20 18:02:28 -08001151 val = !list_empty(&dev->fwnode->suppliers);
Saravana Kannanda6d6472020-05-21 12:18:00 -07001152 device_unlock(dev);
Joe Perchesaa838892020-09-16 13:40:39 -07001153 return sysfs_emit(buf, "%u\n", val);
Saravana Kannanda6d6472020-05-21 12:18:00 -07001154}
1155static DEVICE_ATTR_RO(waiting_for_supplier);
1156
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001157/**
Saravana Kannanb6f617d2021-03-02 13:11:31 -08001158 * device_links_force_bind - Prepares device to be force bound
1159 * @dev: Consumer device.
1160 *
1161 * device_bind_driver() force binds a device to a driver without calling any
1162 * driver probe functions. So the consumer really isn't going to wait for any
1163 * supplier before it's bound to the driver. We still want the device link
1164 * states to be sensible when this happens.
1165 *
1166 * In preparation for device_bind_driver(), this function goes through each
1167 * supplier device links and checks if the supplier is bound. If it is, then
1168 * the device link status is set to CONSUMER_PROBE. Otherwise, the device link
1169 * is dropped. Links without the DL_FLAG_MANAGED flag set are ignored.
1170 */
1171void device_links_force_bind(struct device *dev)
1172{
1173 struct device_link *link, *ln;
1174
1175 device_links_write_lock();
1176
1177 list_for_each_entry_safe(link, ln, &dev->links.suppliers, c_node) {
1178 if (!(link->flags & DL_FLAG_MANAGED))
1179 continue;
1180
1181 if (link->status != DL_STATE_AVAILABLE) {
1182 device_link_drop_managed(link);
1183 continue;
1184 }
1185 WRITE_ONCE(link->status, DL_STATE_CONSUMER_PROBE);
1186 }
1187 dev->links.status = DL_DEV_PROBING;
1188
1189 device_links_write_unlock();
1190}
1191
1192/**
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001193 * device_links_driver_bound - Update device links after probing its driver.
1194 * @dev: Device to update the links for.
1195 *
1196 * The probe has been successful, so update links from this device to any
1197 * consumers by changing their status to "available".
1198 *
1199 * Also change the status of @dev's links to suppliers to "active".
1200 *
Rafael J. Wysocki515db262019-07-16 17:21:06 +02001201 * Links without the DL_FLAG_MANAGED flag set are ignored.
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001202 */
1203void device_links_driver_bound(struct device *dev)
1204{
Saravana Kannan21c27f02020-05-18 23:30:00 -07001205 struct device_link *link, *ln;
Saravana Kannan26e77702019-11-14 14:56:45 -08001206 LIST_HEAD(sync_list);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001207
Saravana Kannanbcbbcfd2019-10-28 15:00:23 -07001208 /*
Saravana Kannan9528e0d2021-02-05 14:26:37 -08001209 * If a device binds successfully, it's expected to have created all
Saravana Kannanbcbbcfd2019-10-28 15:00:23 -07001210 * the device links it needs to or make new device links as it needs
Saravana Kannan9528e0d2021-02-05 14:26:37 -08001211 * them. So, fw_devlink no longer needs to create device links to any
1212 * of the device's suppliers.
1213 *
1214 * Also, if a child firmware node of this bound device is not added as
1215 * a device by now, assume it is never going to be added and make sure
1216 * other devices don't defer probe indefinitely by waiting for such a
1217 * child device.
Saravana Kannanbcbbcfd2019-10-28 15:00:23 -07001218 */
Saravana Kannan9528e0d2021-02-05 14:26:37 -08001219 if (dev->fwnode && dev->fwnode->dev == dev) {
1220 struct fwnode_handle *child;
Saravana Kannanf9aa4602020-11-20 18:02:31 -08001221 fwnode_links_purge_suppliers(dev->fwnode);
Saravana Kannan9528e0d2021-02-05 14:26:37 -08001222 fwnode_for_each_available_child_node(dev->fwnode, child)
1223 fw_devlink_purge_absent_suppliers(child);
1224 }
Saravana Kannanda6d6472020-05-21 12:18:00 -07001225 device_remove_file(dev, &dev_attr_waiting_for_supplier);
Saravana Kannanbcbbcfd2019-10-28 15:00:23 -07001226
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001227 device_links_write_lock();
1228
1229 list_for_each_entry(link, &dev->links.consumers, s_node) {
Rafael J. Wysocki515db262019-07-16 17:21:06 +02001230 if (!(link->flags & DL_FLAG_MANAGED))
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001231 continue;
1232
Rafael J. Wysocki15cfb092019-02-01 01:50:39 +01001233 /*
1234 * Links created during consumer probe may be in the "consumer
1235 * probe" state to start with if the supplier is still probing
1236 * when they are created and they may become "active" if the
1237 * consumer probe returns first. Skip them here.
1238 */
1239 if (link->status == DL_STATE_CONSUMER_PROBE ||
1240 link->status == DL_STATE_ACTIVE)
1241 continue;
1242
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001243 WARN_ON(link->status != DL_STATE_DORMANT);
1244 WRITE_ONCE(link->status, DL_STATE_AVAILABLE);
Rafael J. Wysockie7dd4012019-02-01 01:59:42 +01001245
1246 if (link->flags & DL_FLAG_AUTOPROBE_CONSUMER)
1247 driver_deferred_probe_add(link->consumer);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001248 }
1249
Saravana Kannan21eb93f2020-02-21 00:05:08 -08001250 if (defer_sync_state_count)
1251 __device_links_supplier_defer_sync(dev);
1252 else
1253 __device_links_queue_sync_state(dev, &sync_list);
1254
Saravana Kannan21c27f02020-05-18 23:30:00 -07001255 list_for_each_entry_safe(link, ln, &dev->links.suppliers, c_node) {
1256 struct device *supplier;
1257
Rafael J. Wysocki515db262019-07-16 17:21:06 +02001258 if (!(link->flags & DL_FLAG_MANAGED))
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001259 continue;
1260
Saravana Kannan21c27f02020-05-18 23:30:00 -07001261 supplier = link->supplier;
1262 if (link->flags & DL_FLAG_SYNC_STATE_ONLY) {
1263 /*
1264 * When DL_FLAG_SYNC_STATE_ONLY is set, it means no
1265 * other DL_MANAGED_LINK_FLAGS have been set. So, it's
1266 * save to drop the managed link completely.
1267 */
1268 device_link_drop_managed(link);
1269 } else {
1270 WARN_ON(link->status != DL_STATE_CONSUMER_PROBE);
1271 WRITE_ONCE(link->status, DL_STATE_ACTIVE);
1272 }
Saravana Kannanfc5a2512019-09-04 14:11:23 -07001273
Saravana Kannan21c27f02020-05-18 23:30:00 -07001274 /*
1275 * This needs to be done even for the deleted
1276 * DL_FLAG_SYNC_STATE_ONLY device link in case it was the last
1277 * device link that was preventing the supplier from getting a
1278 * sync_state() call.
1279 */
Saravana Kannanfc5a2512019-09-04 14:11:23 -07001280 if (defer_sync_state_count)
Saravana Kannan21c27f02020-05-18 23:30:00 -07001281 __device_links_supplier_defer_sync(supplier);
Saravana Kannanfc5a2512019-09-04 14:11:23 -07001282 else
Saravana Kannan21c27f02020-05-18 23:30:00 -07001283 __device_links_queue_sync_state(supplier, &sync_list);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001284 }
1285
1286 dev->links.status = DL_DEV_DRIVER_BOUND;
1287
1288 device_links_write_unlock();
Saravana Kannan26e77702019-11-14 14:56:45 -08001289
Saravana Kannan21eb93f2020-02-21 00:05:08 -08001290 device_links_flush_sync_list(&sync_list, dev);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001291}
1292
1293/**
1294 * __device_links_no_driver - Update links of a device without a driver.
1295 * @dev: Device without a drvier.
1296 *
1297 * Delete all non-persistent links from this device to any suppliers.
1298 *
1299 * Persistent links stay around, but their status is changed to "available",
1300 * unless they already are in the "supplier unbind in progress" state in which
1301 * case they need not be updated.
1302 *
Rafael J. Wysocki515db262019-07-16 17:21:06 +02001303 * Links without the DL_FLAG_MANAGED flag set are ignored.
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001304 */
1305static void __device_links_no_driver(struct device *dev)
1306{
1307 struct device_link *link, *ln;
1308
1309 list_for_each_entry_safe_reverse(link, ln, &dev->links.suppliers, c_node) {
Rafael J. Wysocki515db262019-07-16 17:21:06 +02001310 if (!(link->flags & DL_FLAG_MANAGED))
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001311 continue;
1312
Saravana Kannan8c3e3152020-05-26 15:09:27 -07001313 if (link->flags & DL_FLAG_AUTOREMOVE_CONSUMER) {
Rafael J. Wysocki515db262019-07-16 17:21:06 +02001314 device_link_drop_managed(link);
Saravana Kannan8c3e3152020-05-26 15:09:27 -07001315 continue;
1316 }
1317
1318 if (link->status != DL_STATE_CONSUMER_PROBE &&
1319 link->status != DL_STATE_ACTIVE)
1320 continue;
1321
1322 if (link->supplier->links.status == DL_DEV_DRIVER_BOUND) {
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001323 WRITE_ONCE(link->status, DL_STATE_AVAILABLE);
Saravana Kannan8c3e3152020-05-26 15:09:27 -07001324 } else {
1325 WARN_ON(!(link->flags & DL_FLAG_SYNC_STATE_ONLY));
1326 WRITE_ONCE(link->status, DL_STATE_DORMANT);
1327 }
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001328 }
1329
1330 dev->links.status = DL_DEV_NO_DRIVER;
1331}
1332
Rafael J. Wysocki15cfb092019-02-01 01:50:39 +01001333/**
1334 * device_links_no_driver - Update links after failing driver probe.
1335 * @dev: Device whose driver has just failed to probe.
1336 *
1337 * Clean up leftover links to consumers for @dev and invoke
1338 * %__device_links_no_driver() to update links to suppliers for it as
1339 * appropriate.
1340 *
Rafael J. Wysocki515db262019-07-16 17:21:06 +02001341 * Links without the DL_FLAG_MANAGED flag set are ignored.
Rafael J. Wysocki15cfb092019-02-01 01:50:39 +01001342 */
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001343void device_links_no_driver(struct device *dev)
1344{
Rafael J. Wysocki15cfb092019-02-01 01:50:39 +01001345 struct device_link *link;
1346
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001347 device_links_write_lock();
Rafael J. Wysocki15cfb092019-02-01 01:50:39 +01001348
1349 list_for_each_entry(link, &dev->links.consumers, s_node) {
Rafael J. Wysocki515db262019-07-16 17:21:06 +02001350 if (!(link->flags & DL_FLAG_MANAGED))
Rafael J. Wysocki15cfb092019-02-01 01:50:39 +01001351 continue;
1352
1353 /*
1354 * The probe has failed, so if the status of the link is
1355 * "consumer probe" or "active", it must have been added by
1356 * a probing consumer while this device was still probing.
1357 * Change its state to "dormant", as it represents a valid
1358 * relationship, but it is not functionally meaningful.
1359 */
1360 if (link->status == DL_STATE_CONSUMER_PROBE ||
1361 link->status == DL_STATE_ACTIVE)
1362 WRITE_ONCE(link->status, DL_STATE_DORMANT);
1363 }
1364
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001365 __device_links_no_driver(dev);
Rafael J. Wysocki15cfb092019-02-01 01:50:39 +01001366
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001367 device_links_write_unlock();
1368}
1369
1370/**
1371 * device_links_driver_cleanup - Update links after driver removal.
1372 * @dev: Device whose driver has just gone away.
1373 *
1374 * Update links to consumers for @dev by changing their status to "dormant" and
1375 * invoke %__device_links_no_driver() to update links to suppliers for it as
1376 * appropriate.
1377 *
Rafael J. Wysocki515db262019-07-16 17:21:06 +02001378 * Links without the DL_FLAG_MANAGED flag set are ignored.
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001379 */
1380void device_links_driver_cleanup(struct device *dev)
1381{
Rafael J. Wysockic8d50982019-02-01 01:45:55 +01001382 struct device_link *link, *ln;
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001383
1384 device_links_write_lock();
1385
Rafael J. Wysockic8d50982019-02-01 01:45:55 +01001386 list_for_each_entry_safe(link, ln, &dev->links.consumers, s_node) {
Rafael J. Wysocki515db262019-07-16 17:21:06 +02001387 if (!(link->flags & DL_FLAG_MANAGED))
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001388 continue;
1389
Vivek Gautame88728f2018-06-27 18:20:55 +05301390 WARN_ON(link->flags & DL_FLAG_AUTOREMOVE_CONSUMER);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001391 WARN_ON(link->status != DL_STATE_SUPPLIER_UNBIND);
Vivek Gautam1689cac2018-06-27 18:20:56 +05301392
1393 /*
1394 * autoremove the links between this @dev and its consumer
1395 * devices that are not active, i.e. where the link state
1396 * has moved to DL_STATE_SUPPLIER_UNBIND.
1397 */
1398 if (link->status == DL_STATE_SUPPLIER_UNBIND &&
1399 link->flags & DL_FLAG_AUTOREMOVE_SUPPLIER)
Rafael J. Wysocki515db262019-07-16 17:21:06 +02001400 device_link_drop_managed(link);
Vivek Gautam1689cac2018-06-27 18:20:56 +05301401
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001402 WRITE_ONCE(link->status, DL_STATE_DORMANT);
1403 }
1404
Saravana Kannan3b052a32020-11-20 18:02:17 -08001405 list_del_init(&dev->links.defer_sync);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001406 __device_links_no_driver(dev);
1407
1408 device_links_write_unlock();
1409}
1410
1411/**
1412 * device_links_busy - Check if there are any busy links to consumers.
1413 * @dev: Device to check.
1414 *
1415 * Check each consumer of the device and return 'true' if its link's status
1416 * is one of "consumer probe" or "active" (meaning that the given consumer is
1417 * probing right now or its driver is present). Otherwise, change the link
1418 * state to "supplier unbind" to prevent the consumer from being probed
1419 * successfully going forward.
1420 *
1421 * Return 'false' if there are no probing or active consumers.
1422 *
Rafael J. Wysocki515db262019-07-16 17:21:06 +02001423 * Links without the DL_FLAG_MANAGED flag set are ignored.
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001424 */
1425bool device_links_busy(struct device *dev)
1426{
1427 struct device_link *link;
1428 bool ret = false;
1429
1430 device_links_write_lock();
1431
1432 list_for_each_entry(link, &dev->links.consumers, s_node) {
Rafael J. Wysocki515db262019-07-16 17:21:06 +02001433 if (!(link->flags & DL_FLAG_MANAGED))
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001434 continue;
1435
1436 if (link->status == DL_STATE_CONSUMER_PROBE
1437 || link->status == DL_STATE_ACTIVE) {
1438 ret = true;
1439 break;
1440 }
1441 WRITE_ONCE(link->status, DL_STATE_SUPPLIER_UNBIND);
1442 }
1443
1444 dev->links.status = DL_DEV_UNBINDING;
1445
1446 device_links_write_unlock();
1447 return ret;
1448}
1449
1450/**
1451 * device_links_unbind_consumers - Force unbind consumers of the given device.
1452 * @dev: Device to unbind the consumers of.
1453 *
1454 * Walk the list of links to consumers for @dev and if any of them is in the
1455 * "consumer probe" state, wait for all device probes in progress to complete
1456 * and start over.
1457 *
1458 * If that's not the case, change the status of the link to "supplier unbind"
1459 * and check if the link was in the "active" state. If so, force the consumer
1460 * driver to unbind and start over (the consumer will not re-probe as we have
1461 * changed the state of the link already).
1462 *
Rafael J. Wysocki515db262019-07-16 17:21:06 +02001463 * Links without the DL_FLAG_MANAGED flag set are ignored.
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001464 */
1465void device_links_unbind_consumers(struct device *dev)
1466{
1467 struct device_link *link;
1468
1469 start:
1470 device_links_write_lock();
1471
1472 list_for_each_entry(link, &dev->links.consumers, s_node) {
1473 enum device_link_state status;
1474
Saravana Kannan05ef9832019-10-28 15:00:22 -07001475 if (!(link->flags & DL_FLAG_MANAGED) ||
1476 link->flags & DL_FLAG_SYNC_STATE_ONLY)
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001477 continue;
1478
1479 status = link->status;
1480 if (status == DL_STATE_CONSUMER_PROBE) {
1481 device_links_write_unlock();
1482
1483 wait_for_device_probe();
1484 goto start;
1485 }
1486 WRITE_ONCE(link->status, DL_STATE_SUPPLIER_UNBIND);
1487 if (status == DL_STATE_ACTIVE) {
1488 struct device *consumer = link->consumer;
1489
1490 get_device(consumer);
1491
1492 device_links_write_unlock();
1493
1494 device_release_driver_internal(consumer, NULL,
1495 consumer->parent);
1496 put_device(consumer);
1497 goto start;
1498 }
1499 }
1500
1501 device_links_write_unlock();
1502}
1503
1504/**
1505 * device_links_purge - Delete existing links to other devices.
1506 * @dev: Target device.
1507 */
1508static void device_links_purge(struct device *dev)
1509{
1510 struct device_link *link, *ln;
1511
Saravana Kannan287905e2020-05-21 12:17:58 -07001512 if (dev->class == &devlink_class)
1513 return;
1514
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001515 /*
1516 * Delete all of the remaining links from this device to any other
1517 * devices (either consumers or suppliers).
1518 */
1519 device_links_write_lock();
1520
1521 list_for_each_entry_safe_reverse(link, ln, &dev->links.suppliers, c_node) {
1522 WARN_ON(link->status == DL_STATE_ACTIVE);
Lukas Wunneread18c22018-02-10 19:27:12 +01001523 __device_link_del(&link->kref);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001524 }
1525
1526 list_for_each_entry_safe_reverse(link, ln, &dev->links.consumers, s_node) {
1527 WARN_ON(link->status != DL_STATE_DORMANT &&
1528 link->status != DL_STATE_NONE);
Lukas Wunneread18c22018-02-10 19:27:12 +01001529 __device_link_del(&link->kref);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001530 }
1531
1532 device_links_write_unlock();
1533}
1534
Saravana Kannanb90fb8f2020-12-17 19:17:01 -08001535#define FW_DEVLINK_FLAGS_PERMISSIVE (DL_FLAG_INFERRED | \
1536 DL_FLAG_SYNC_STATE_ONLY)
1537#define FW_DEVLINK_FLAGS_ON (DL_FLAG_INFERRED | \
1538 DL_FLAG_AUTOPROBE_CONSUMER)
1539#define FW_DEVLINK_FLAGS_RPM (FW_DEVLINK_FLAGS_ON | \
1540 DL_FLAG_PM_RUNTIME)
1541
Saravana Kannanea718c692021-03-02 13:11:32 -08001542static u32 fw_devlink_flags = FW_DEVLINK_FLAGS_ON;
Saravana Kannan42926ac2020-05-14 22:34:57 -07001543static int __init fw_devlink_setup(char *arg)
1544{
1545 if (!arg)
1546 return -EINVAL;
1547
1548 if (strcmp(arg, "off") == 0) {
1549 fw_devlink_flags = 0;
1550 } else if (strcmp(arg, "permissive") == 0) {
Saravana Kannanb90fb8f2020-12-17 19:17:01 -08001551 fw_devlink_flags = FW_DEVLINK_FLAGS_PERMISSIVE;
Saravana Kannan42926ac2020-05-14 22:34:57 -07001552 } else if (strcmp(arg, "on") == 0) {
Saravana Kannanb90fb8f2020-12-17 19:17:01 -08001553 fw_devlink_flags = FW_DEVLINK_FLAGS_ON;
Saravana Kannan42926ac2020-05-14 22:34:57 -07001554 } else if (strcmp(arg, "rpm") == 0) {
Saravana Kannanb90fb8f2020-12-17 19:17:01 -08001555 fw_devlink_flags = FW_DEVLINK_FLAGS_RPM;
Saravana Kannan42926ac2020-05-14 22:34:57 -07001556 }
1557 return 0;
1558}
1559early_param("fw_devlink", fw_devlink_setup);
1560
Saravana Kannan19d0f5f2021-02-05 14:26:39 -08001561static bool fw_devlink_strict;
1562static int __init fw_devlink_strict_setup(char *arg)
1563{
1564 return strtobool(arg, &fw_devlink_strict);
1565}
1566early_param("fw_devlink.strict", fw_devlink_strict_setup);
1567
Saravana Kannan42926ac2020-05-14 22:34:57 -07001568u32 fw_devlink_get_flags(void)
1569{
1570 return fw_devlink_flags;
1571}
1572
1573static bool fw_devlink_is_permissive(void)
1574{
Saravana Kannanb90fb8f2020-12-17 19:17:01 -08001575 return fw_devlink_flags == FW_DEVLINK_FLAGS_PERMISSIVE;
Saravana Kannan42926ac2020-05-14 22:34:57 -07001576}
1577
Saravana Kannan19d0f5f2021-02-05 14:26:39 -08001578bool fw_devlink_is_strict(void)
1579{
1580 return fw_devlink_strict && !fw_devlink_is_permissive();
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001581}
1582
Saravana Kannanc2c724c2020-11-20 18:02:27 -08001583static void fw_devlink_parse_fwnode(struct fwnode_handle *fwnode)
1584{
1585 if (fwnode->flags & FWNODE_FLAG_LINKS_ADDED)
1586 return;
1587
Saravana Kannan2d09e6e2020-11-20 18:02:32 -08001588 fwnode_call_int_op(fwnode, add_links);
Saravana Kannanc2c724c2020-11-20 18:02:27 -08001589 fwnode->flags |= FWNODE_FLAG_LINKS_ADDED;
1590}
1591
1592static void fw_devlink_parse_fwtree(struct fwnode_handle *fwnode)
1593{
1594 struct fwnode_handle *child = NULL;
1595
1596 fw_devlink_parse_fwnode(fwnode);
1597
1598 while ((child = fwnode_get_next_available_child_node(fwnode, child)))
1599 fw_devlink_parse_fwtree(child);
1600}
1601
Saravana Kannand46f3e32021-04-01 21:03:41 -07001602static void fw_devlink_relax_link(struct device_link *link)
1603{
1604 if (!(link->flags & DL_FLAG_INFERRED))
1605 return;
1606
1607 if (link->flags == (DL_FLAG_MANAGED | FW_DEVLINK_FLAGS_PERMISSIVE))
1608 return;
1609
1610 pm_runtime_drop_link(link);
1611 link->flags = DL_FLAG_MANAGED | FW_DEVLINK_FLAGS_PERMISSIVE;
1612 dev_dbg(link->consumer, "Relaxing link with %s\n",
1613 dev_name(link->supplier));
1614}
1615
1616static int fw_devlink_no_driver(struct device *dev, void *data)
1617{
1618 struct device_link *link = to_devlink(dev);
1619
1620 if (!link->supplier->can_match)
1621 fw_devlink_relax_link(link);
1622
1623 return 0;
1624}
1625
1626void fw_devlink_drivers_done(void)
1627{
1628 fw_devlink_drv_reg_done = true;
1629 device_links_write_lock();
1630 class_for_each_device(&devlink_class, NULL, NULL,
1631 fw_devlink_no_driver);
1632 device_links_write_unlock();
1633}
1634
1635static void fw_devlink_unblock_consumers(struct device *dev)
1636{
1637 struct device_link *link;
1638
1639 if (!fw_devlink_flags || fw_devlink_is_permissive())
1640 return;
1641
1642 device_links_write_lock();
1643 list_for_each_entry(link, &dev->links.consumers, s_node)
1644 fw_devlink_relax_link(link);
1645 device_links_write_unlock();
1646}
1647
Saravana Kannanf9aa4602020-11-20 18:02:31 -08001648/**
Saravana Kannanb0e2fa42020-12-17 19:17:02 -08001649 * fw_devlink_relax_cycle - Convert cyclic links to SYNC_STATE_ONLY links
1650 * @con: Device to check dependencies for.
1651 * @sup: Device to check against.
1652 *
1653 * Check if @sup depends on @con or any device dependent on it (its child or
1654 * its consumer etc). When such a cyclic dependency is found, convert all
1655 * device links created solely by fw_devlink into SYNC_STATE_ONLY device links.
1656 * This is the equivalent of doing fw_devlink=permissive just between the
1657 * devices in the cycle. We need to do this because, at this point, fw_devlink
1658 * can't tell which of these dependencies is not a real dependency.
1659 *
1660 * Return 1 if a cycle is found. Otherwise, return 0.
1661 */
kernel test robotc13b8272020-12-18 14:39:35 +08001662static int fw_devlink_relax_cycle(struct device *con, void *sup)
Saravana Kannanb0e2fa42020-12-17 19:17:02 -08001663{
1664 struct device_link *link;
1665 int ret;
1666
1667 if (con == sup)
1668 return 1;
1669
1670 ret = device_for_each_child(con, sup, fw_devlink_relax_cycle);
1671 if (ret)
1672 return ret;
1673
1674 list_for_each_entry(link, &con->links.consumers, s_node) {
1675 if ((link->flags & ~DL_FLAG_INFERRED) ==
1676 (DL_FLAG_SYNC_STATE_ONLY | DL_FLAG_MANAGED))
1677 continue;
1678
1679 if (!fw_devlink_relax_cycle(link->consumer, sup))
1680 continue;
1681
1682 ret = 1;
1683
Saravana Kannand46f3e32021-04-01 21:03:41 -07001684 fw_devlink_relax_link(link);
Saravana Kannanb0e2fa42020-12-17 19:17:02 -08001685 }
1686 return ret;
1687}
1688
1689/**
Saravana Kannanf9aa4602020-11-20 18:02:31 -08001690 * fw_devlink_create_devlink - Create a device link from a consumer to fwnode
Pierre-Louis Bossart37c52f72021-03-31 18:26:08 -05001691 * @con: consumer device for the device link
1692 * @sup_handle: fwnode handle of supplier
1693 * @flags: devlink flags
Saravana Kannanf9aa4602020-11-20 18:02:31 -08001694 *
1695 * This function will try to create a device link between the consumer device
1696 * @con and the supplier device represented by @sup_handle.
1697 *
1698 * The supplier has to be provided as a fwnode because incorrect cycles in
1699 * fwnode links can sometimes cause the supplier device to never be created.
1700 * This function detects such cases and returns an error if it cannot create a
1701 * device link from the consumer to a missing supplier.
1702 *
1703 * Returns,
1704 * 0 on successfully creating a device link
1705 * -EINVAL if the device link cannot be created as expected
1706 * -EAGAIN if the device link cannot be created right now, but it may be
1707 * possible to do that in the future
1708 */
1709static int fw_devlink_create_devlink(struct device *con,
1710 struct fwnode_handle *sup_handle, u32 flags)
1711{
1712 struct device *sup_dev;
1713 int ret = 0;
1714
1715 sup_dev = get_dev_from_fwnode(sup_handle);
1716 if (sup_dev) {
1717 /*
Saravana Kannan74c782c2021-02-05 14:26:41 -08001718 * If it's one of those drivers that don't actually bind to
1719 * their device using driver core, then don't wait on this
1720 * supplier device indefinitely.
1721 */
1722 if (sup_dev->links.status == DL_DEV_NO_DRIVER &&
1723 sup_handle->flags & FWNODE_FLAG_INITIALIZED) {
1724 ret = -EINVAL;
1725 goto out;
1726 }
1727
1728 /*
Saravana Kannanf9aa4602020-11-20 18:02:31 -08001729 * If this fails, it is due to cycles in device links. Just
1730 * give up on this link and treat it as invalid.
1731 */
Saravana Kannanb0e2fa42020-12-17 19:17:02 -08001732 if (!device_link_add(con, sup_dev, flags) &&
1733 !(flags & DL_FLAG_SYNC_STATE_ONLY)) {
1734 dev_info(con, "Fixing up cyclic dependency with %s\n",
1735 dev_name(sup_dev));
1736 device_links_write_lock();
1737 fw_devlink_relax_cycle(con, sup_dev);
1738 device_links_write_unlock();
1739 device_link_add(con, sup_dev,
1740 FW_DEVLINK_FLAGS_PERMISSIVE);
Saravana Kannanf9aa4602020-11-20 18:02:31 -08001741 ret = -EINVAL;
Saravana Kannanb0e2fa42020-12-17 19:17:02 -08001742 }
Saravana Kannanf9aa4602020-11-20 18:02:31 -08001743
1744 goto out;
1745 }
1746
Saravana Kannan74c782c2021-02-05 14:26:41 -08001747 /* Supplier that's already initialized without a struct device. */
1748 if (sup_handle->flags & FWNODE_FLAG_INITIALIZED)
1749 return -EINVAL;
1750
Saravana Kannanf9aa4602020-11-20 18:02:31 -08001751 /*
1752 * DL_FLAG_SYNC_STATE_ONLY doesn't block probing and supports
1753 * cycles. So cycle detection isn't necessary and shouldn't be
1754 * done.
1755 */
1756 if (flags & DL_FLAG_SYNC_STATE_ONLY)
1757 return -EAGAIN;
1758
1759 /*
1760 * If we can't find the supplier device from its fwnode, it might be
1761 * due to a cyclic dependency between fwnodes. Some of these cycles can
1762 * be broken by applying logic. Check for these types of cycles and
1763 * break them so that devices in the cycle probe properly.
1764 *
1765 * If the supplier's parent is dependent on the consumer, then
1766 * the consumer-supplier dependency is a false dependency. So,
1767 * treat it as an invalid link.
1768 */
1769 sup_dev = fwnode_get_next_parent_dev(sup_handle);
1770 if (sup_dev && device_is_dependent(con, sup_dev)) {
1771 dev_dbg(con, "Not linking to %pfwP - False link\n",
1772 sup_handle);
1773 ret = -EINVAL;
1774 } else {
1775 /*
1776 * Can't check for cycles or no cycles. So let's try
1777 * again later.
1778 */
1779 ret = -EAGAIN;
1780 }
1781
1782out:
1783 put_device(sup_dev);
1784 return ret;
1785}
1786
1787/**
1788 * __fw_devlink_link_to_consumers - Create device links to consumers of a device
Pierre-Louis Bossart37c52f72021-03-31 18:26:08 -05001789 * @dev: Device that needs to be linked to its consumers
Saravana Kannanf9aa4602020-11-20 18:02:31 -08001790 *
1791 * This function looks at all the consumer fwnodes of @dev and creates device
1792 * links between the consumer device and @dev (supplier).
1793 *
1794 * If the consumer device has not been added yet, then this function creates a
1795 * SYNC_STATE_ONLY link between @dev (supplier) and the closest ancestor device
1796 * of the consumer fwnode. This is necessary to make sure @dev doesn't get a
1797 * sync_state() callback before the real consumer device gets to be added and
1798 * then probed.
1799 *
1800 * Once device links are created from the real consumer to @dev (supplier), the
1801 * fwnode links are deleted.
1802 */
1803static void __fw_devlink_link_to_consumers(struct device *dev)
1804{
1805 struct fwnode_handle *fwnode = dev->fwnode;
1806 struct fwnode_link *link, *tmp;
1807
1808 list_for_each_entry_safe(link, tmp, &fwnode->consumers, s_hook) {
1809 u32 dl_flags = fw_devlink_get_flags();
1810 struct device *con_dev;
1811 bool own_link = true;
1812 int ret;
1813
1814 con_dev = get_dev_from_fwnode(link->consumer);
1815 /*
1816 * If consumer device is not available yet, make a "proxy"
1817 * SYNC_STATE_ONLY link from the consumer's parent device to
1818 * the supplier device. This is necessary to make sure the
1819 * supplier doesn't get a sync_state() callback before the real
1820 * consumer can create a device link to the supplier.
1821 *
1822 * This proxy link step is needed to handle the case where the
1823 * consumer's parent device is added before the supplier.
1824 */
1825 if (!con_dev) {
1826 con_dev = fwnode_get_next_parent_dev(link->consumer);
1827 /*
1828 * However, if the consumer's parent device is also the
1829 * parent of the supplier, don't create a
1830 * consumer-supplier link from the parent to its child
1831 * device. Such a dependency is impossible.
1832 */
1833 if (con_dev &&
1834 fwnode_is_ancestor_of(con_dev->fwnode, fwnode)) {
1835 put_device(con_dev);
1836 con_dev = NULL;
1837 } else {
1838 own_link = false;
Saravana Kannanb90fb8f2020-12-17 19:17:01 -08001839 dl_flags = FW_DEVLINK_FLAGS_PERMISSIVE;
Saravana Kannanf9aa4602020-11-20 18:02:31 -08001840 }
1841 }
1842
1843 if (!con_dev)
1844 continue;
1845
1846 ret = fw_devlink_create_devlink(con_dev, fwnode, dl_flags);
1847 put_device(con_dev);
1848 if (!own_link || ret == -EAGAIN)
1849 continue;
1850
1851 list_del(&link->s_hook);
1852 list_del(&link->c_hook);
1853 kfree(link);
1854 }
1855}
1856
1857/**
1858 * __fw_devlink_link_to_suppliers - Create device links to suppliers of a device
Pierre-Louis Bossart37c52f72021-03-31 18:26:08 -05001859 * @dev: The consumer device that needs to be linked to its suppliers
1860 * @fwnode: Root of the fwnode tree that is used to create device links
Saravana Kannanf9aa4602020-11-20 18:02:31 -08001861 *
1862 * This function looks at all the supplier fwnodes of fwnode tree rooted at
1863 * @fwnode and creates device links between @dev (consumer) and all the
1864 * supplier devices of the entire fwnode tree at @fwnode.
1865 *
1866 * The function creates normal (non-SYNC_STATE_ONLY) device links between @dev
1867 * and the real suppliers of @dev. Once these device links are created, the
1868 * fwnode links are deleted. When such device links are successfully created,
1869 * this function is called recursively on those supplier devices. This is
1870 * needed to detect and break some invalid cycles in fwnode links. See
1871 * fw_devlink_create_devlink() for more details.
1872 *
1873 * In addition, it also looks at all the suppliers of the entire fwnode tree
1874 * because some of the child devices of @dev that have not been added yet
1875 * (because @dev hasn't probed) might already have their suppliers added to
1876 * driver core. So, this function creates SYNC_STATE_ONLY device links between
1877 * @dev (consumer) and these suppliers to make sure they don't execute their
1878 * sync_state() callbacks before these child devices have a chance to create
1879 * their device links. The fwnode links that correspond to the child devices
1880 * aren't delete because they are needed later to create the device links
1881 * between the real consumer and supplier devices.
1882 */
1883static void __fw_devlink_link_to_suppliers(struct device *dev,
1884 struct fwnode_handle *fwnode)
1885{
1886 bool own_link = (dev->fwnode == fwnode);
1887 struct fwnode_link *link, *tmp;
1888 struct fwnode_handle *child = NULL;
1889 u32 dl_flags;
1890
1891 if (own_link)
1892 dl_flags = fw_devlink_get_flags();
1893 else
Saravana Kannanb90fb8f2020-12-17 19:17:01 -08001894 dl_flags = FW_DEVLINK_FLAGS_PERMISSIVE;
Saravana Kannanf9aa4602020-11-20 18:02:31 -08001895
1896 list_for_each_entry_safe(link, tmp, &fwnode->suppliers, c_hook) {
1897 int ret;
1898 struct device *sup_dev;
1899 struct fwnode_handle *sup = link->supplier;
1900
1901 ret = fw_devlink_create_devlink(dev, sup, dl_flags);
1902 if (!own_link || ret == -EAGAIN)
1903 continue;
1904
1905 list_del(&link->s_hook);
1906 list_del(&link->c_hook);
1907 kfree(link);
1908
1909 /* If no device link was created, nothing more to do. */
1910 if (ret)
1911 continue;
1912
1913 /*
1914 * If a device link was successfully created to a supplier, we
1915 * now need to try and link the supplier to all its suppliers.
1916 *
1917 * This is needed to detect and delete false dependencies in
1918 * fwnode links that haven't been converted to a device link
1919 * yet. See comments in fw_devlink_create_devlink() for more
1920 * details on the false dependency.
1921 *
1922 * Without deleting these false dependencies, some devices will
1923 * never probe because they'll keep waiting for their false
1924 * dependency fwnode links to be converted to device links.
1925 */
1926 sup_dev = get_dev_from_fwnode(sup);
1927 __fw_devlink_link_to_suppliers(sup_dev, sup_dev->fwnode);
1928 put_device(sup_dev);
1929 }
1930
1931 /*
1932 * Make "proxy" SYNC_STATE_ONLY device links to represent the needs of
1933 * all the descendants. This proxy link step is needed to handle the
1934 * case where the supplier is added before the consumer's parent device
1935 * (@dev).
1936 */
1937 while ((child = fwnode_get_next_available_child_node(fwnode, child)))
1938 __fw_devlink_link_to_suppliers(dev, child);
1939}
1940
Saravana Kannan5f5377e2020-05-14 22:34:58 -07001941static void fw_devlink_link_device(struct device *dev)
1942{
Saravana Kannanf9aa4602020-11-20 18:02:31 -08001943 struct fwnode_handle *fwnode = dev->fwnode;
Saravana Kannan5f5377e2020-05-14 22:34:58 -07001944
Saravana Kannanf9aa4602020-11-20 18:02:31 -08001945 if (!fw_devlink_flags)
1946 return;
Saravana Kannanc84b9092020-11-20 18:02:21 -08001947
Saravana Kannanf9aa4602020-11-20 18:02:31 -08001948 fw_devlink_parse_fwtree(fwnode);
1949
1950 mutex_lock(&fwnode_link_lock);
1951 __fw_devlink_link_to_consumers(dev);
1952 __fw_devlink_link_to_suppliers(dev, fwnode);
1953 mutex_unlock(&fwnode_link_lock);
Saravana Kannan716a7a22020-05-14 22:34:59 -07001954}
Saravana Kannanc84b9092020-11-20 18:02:21 -08001955
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001956/* Device links support end. */
1957
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001958int (*platform_notify)(struct device *dev) = NULL;
1959int (*platform_notify_remove)(struct device *dev) = NULL;
Dan Williamse105b8b2008-04-21 10:51:07 -07001960static struct kobject *dev_kobj;
1961struct kobject *sysfs_dev_char_kobj;
1962struct kobject *sysfs_dev_block_kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963
Rafael J. Wysocki5e33bc42013-08-28 21:41:01 +02001964static DEFINE_MUTEX(device_hotplug_lock);
1965
1966void lock_device_hotplug(void)
1967{
1968 mutex_lock(&device_hotplug_lock);
1969}
1970
1971void unlock_device_hotplug(void)
1972{
1973 mutex_unlock(&device_hotplug_lock);
1974}
1975
1976int lock_device_hotplug_sysfs(void)
1977{
1978 if (mutex_trylock(&device_hotplug_lock))
1979 return 0;
1980
1981 /* Avoid busy looping (5 ms of sleep should do). */
1982 msleep(5);
1983 return restart_syscall();
1984}
1985
Greg Kroah-Hartman4e886c22008-01-27 12:12:43 -08001986#ifdef CONFIG_BLOCK
1987static inline int device_is_not_partition(struct device *dev)
1988{
1989 return !(dev->type == &part_type);
1990}
1991#else
1992static inline int device_is_not_partition(struct device *dev)
1993{
1994 return 1;
1995}
1996#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997
Heikki Krogerus07de0e82018-11-09 17:21:34 +03001998static int
1999device_platform_notify(struct device *dev, enum kobject_action action)
2000{
Heikki Krogerus7847a142018-11-09 17:21:35 +03002001 int ret;
2002
2003 ret = acpi_platform_notify(dev, action);
2004 if (ret)
2005 return ret;
2006
Heikki Krogerus59abd832018-11-09 17:21:36 +03002007 ret = software_node_notify(dev, action);
2008 if (ret)
2009 return ret;
2010
Heikki Krogerus07de0e82018-11-09 17:21:34 +03002011 if (platform_notify && action == KOBJ_ADD)
2012 platform_notify(dev);
2013 else if (platform_notify_remove && action == KOBJ_REMOVE)
2014 platform_notify_remove(dev);
2015 return 0;
2016}
2017
Alan Stern3e956372006-06-16 17:10:48 -04002018/**
2019 * dev_driver_string - Return a device's driver name, if at all possible
2020 * @dev: struct device to get the name of
2021 *
2022 * Will return the device's driver's name if it is bound to a device. If
yan9169c012012-04-20 21:08:45 +08002023 * the device is not bound to a driver, it will return the name of the bus
Alan Stern3e956372006-06-16 17:10:48 -04002024 * it is attached to. If it is not attached to a bus either, an empty
2025 * string will be returned.
2026 */
Jean Delvarebf9ca692008-07-30 12:29:21 -07002027const char *dev_driver_string(const struct device *dev)
Alan Stern3e956372006-06-16 17:10:48 -04002028{
Alan Stern35899722009-12-04 11:06:57 -05002029 struct device_driver *drv;
2030
2031 /* dev->driver can change to NULL underneath us because of unbinding,
2032 * so be careful about accessing it. dev->bus and dev->class should
2033 * never change once they are set, so they don't need special care.
2034 */
Mark Rutland6aa7de02017-10-23 14:07:29 -07002035 drv = READ_ONCE(dev->driver);
Saravana Kannane020ff62021-01-10 09:54:07 -08002036 return drv ? drv->name : dev_bus_name(dev);
Alan Stern3e956372006-06-16 17:10:48 -04002037}
Matthew Wilcox310a9222006-09-23 23:35:04 -06002038EXPORT_SYMBOL(dev_driver_string);
Alan Stern3e956372006-06-16 17:10:48 -04002039
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040#define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr)
2041
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002042static ssize_t dev_attr_show(struct kobject *kobj, struct attribute *attr,
2043 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002045 struct device_attribute *dev_attr = to_dev_attr(attr);
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +02002046 struct device *dev = kobj_to_dev(kobj);
Dmitry Torokhov4a0c20b2005-04-29 01:23:47 -05002047 ssize_t ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048
2049 if (dev_attr->show)
Yani Ioannou54b6f352005-05-17 06:39:34 -04002050 ret = dev_attr->show(dev, dev_attr, buf);
Andrew Morton815d2d52008-03-04 15:09:07 -08002051 if (ret >= (ssize_t)PAGE_SIZE) {
Sergey Senozhatskya52668c2017-12-11 21:50:21 +09002052 printk("dev_attr_show: %pS returned bad count\n",
2053 dev_attr->show);
Andrew Morton815d2d52008-03-04 15:09:07 -08002054 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 return ret;
2056}
2057
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002058static ssize_t dev_attr_store(struct kobject *kobj, struct attribute *attr,
2059 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002061 struct device_attribute *dev_attr = to_dev_attr(attr);
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +02002062 struct device *dev = kobj_to_dev(kobj);
Dmitry Torokhov4a0c20b2005-04-29 01:23:47 -05002063 ssize_t ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064
2065 if (dev_attr->store)
Yani Ioannou54b6f352005-05-17 06:39:34 -04002066 ret = dev_attr->store(dev, dev_attr, buf, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 return ret;
2068}
2069
Emese Revfy52cf25d2010-01-19 02:58:23 +01002070static const struct sysfs_ops dev_sysfs_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 .show = dev_attr_show,
2072 .store = dev_attr_store,
2073};
2074
Kay Sieversca22e562011-12-14 14:29:38 -08002075#define to_ext_attr(x) container_of(x, struct dev_ext_attribute, attr)
2076
2077ssize_t device_store_ulong(struct device *dev,
2078 struct device_attribute *attr,
2079 const char *buf, size_t size)
2080{
2081 struct dev_ext_attribute *ea = to_ext_attr(attr);
Kaitao chengf88184b2018-11-06 08:34:54 -08002082 int ret;
2083 unsigned long new;
2084
2085 ret = kstrtoul(buf, 0, &new);
2086 if (ret)
2087 return ret;
Kay Sieversca22e562011-12-14 14:29:38 -08002088 *(unsigned long *)(ea->var) = new;
2089 /* Always return full write size even if we didn't consume all */
2090 return size;
2091}
2092EXPORT_SYMBOL_GPL(device_store_ulong);
2093
2094ssize_t device_show_ulong(struct device *dev,
2095 struct device_attribute *attr,
2096 char *buf)
2097{
2098 struct dev_ext_attribute *ea = to_ext_attr(attr);
Joe Perchesaa838892020-09-16 13:40:39 -07002099 return sysfs_emit(buf, "%lx\n", *(unsigned long *)(ea->var));
Kay Sieversca22e562011-12-14 14:29:38 -08002100}
2101EXPORT_SYMBOL_GPL(device_show_ulong);
2102
2103ssize_t device_store_int(struct device *dev,
2104 struct device_attribute *attr,
2105 const char *buf, size_t size)
2106{
2107 struct dev_ext_attribute *ea = to_ext_attr(attr);
Kaitao chengf88184b2018-11-06 08:34:54 -08002108 int ret;
2109 long new;
2110
2111 ret = kstrtol(buf, 0, &new);
2112 if (ret)
2113 return ret;
2114
2115 if (new > INT_MAX || new < INT_MIN)
Kay Sieversca22e562011-12-14 14:29:38 -08002116 return -EINVAL;
2117 *(int *)(ea->var) = new;
2118 /* Always return full write size even if we didn't consume all */
2119 return size;
2120}
2121EXPORT_SYMBOL_GPL(device_store_int);
2122
2123ssize_t device_show_int(struct device *dev,
2124 struct device_attribute *attr,
2125 char *buf)
2126{
2127 struct dev_ext_attribute *ea = to_ext_attr(attr);
2128
Joe Perchesaa838892020-09-16 13:40:39 -07002129 return sysfs_emit(buf, "%d\n", *(int *)(ea->var));
Kay Sieversca22e562011-12-14 14:29:38 -08002130}
2131EXPORT_SYMBOL_GPL(device_show_int);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132
Borislav Petkov91872392012-10-09 19:52:05 +02002133ssize_t device_store_bool(struct device *dev, struct device_attribute *attr,
2134 const char *buf, size_t size)
2135{
2136 struct dev_ext_attribute *ea = to_ext_attr(attr);
2137
2138 if (strtobool(buf, ea->var) < 0)
2139 return -EINVAL;
2140
2141 return size;
2142}
2143EXPORT_SYMBOL_GPL(device_store_bool);
2144
2145ssize_t device_show_bool(struct device *dev, struct device_attribute *attr,
2146 char *buf)
2147{
2148 struct dev_ext_attribute *ea = to_ext_attr(attr);
2149
Joe Perchesaa838892020-09-16 13:40:39 -07002150 return sysfs_emit(buf, "%d\n", *(bool *)(ea->var));
Borislav Petkov91872392012-10-09 19:52:05 +02002151}
2152EXPORT_SYMBOL_GPL(device_show_bool);
2153
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154/**
Robert P. J. Dayf8878dc2013-06-01 20:17:34 -04002155 * device_release - free device structure.
2156 * @kobj: device's kobject.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 *
Robert P. J. Dayf8878dc2013-06-01 20:17:34 -04002158 * This is called once the reference count for the object
2159 * reaches 0. We forward the call to the device's release
2160 * method, which should handle actually freeing the structure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002162static void device_release(struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +02002164 struct device *dev = kobj_to_dev(kobj);
Greg Kroah-Hartmanfb069a52008-12-16 12:23:36 -08002165 struct device_private *p = dev->p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166
Ming Leia525a3d2012-07-25 01:42:29 +08002167 /*
2168 * Some platform devices are driven without driver attached
2169 * and managed resources may have been acquired. Make sure
2170 * all resources are released.
2171 *
2172 * Drivers still can add resources into device after device
2173 * is deleted but alive, so release devres here to avoid
2174 * possible memory leak.
2175 */
2176 devres_release_all(dev);
2177
Jim Quinlane0d07272020-09-17 18:43:40 +02002178 kfree(dev->dma_range_map);
2179
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 if (dev->release)
2181 dev->release(dev);
Kay Sieversf9f852d2006-10-07 21:54:55 +02002182 else if (dev->type && dev->type->release)
2183 dev->type->release(dev);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07002184 else if (dev->class && dev->class->dev_release)
2185 dev->class->dev_release(dev);
Arjan van de Venf810a5c2008-07-25 19:45:39 -07002186 else
Mauro Carvalho Chehab0c1bc6b2020-04-14 18:48:37 +02002187 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 +01002188 dev_name(dev));
Greg Kroah-Hartmanfb069a52008-12-16 12:23:36 -08002189 kfree(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190}
2191
Eric W. Biedermanbc451f22010-03-30 11:31:25 -07002192static const void *device_namespace(struct kobject *kobj)
2193{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +02002194 struct device *dev = kobj_to_dev(kobj);
Eric W. Biedermanbc451f22010-03-30 11:31:25 -07002195 const void *ns = NULL;
2196
2197 if (dev->class && dev->class->ns_type)
2198 ns = dev->class->namespace(dev);
2199
2200 return ns;
2201}
2202
Dmitry Torokhov9944e892018-07-20 21:56:50 +00002203static void device_get_ownership(struct kobject *kobj, kuid_t *uid, kgid_t *gid)
2204{
2205 struct device *dev = kobj_to_dev(kobj);
2206
2207 if (dev->class && dev->class->get_ownership)
2208 dev->class->get_ownership(dev, uid, gid);
2209}
2210
Greg Kroah-Hartman8f4afc42007-10-11 10:47:49 -06002211static struct kobj_type device_ktype = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 .release = device_release,
2213 .sysfs_ops = &dev_sysfs_ops,
Eric W. Biedermanbc451f22010-03-30 11:31:25 -07002214 .namespace = device_namespace,
Dmitry Torokhov9944e892018-07-20 21:56:50 +00002215 .get_ownership = device_get_ownership,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216};
2217
2218
Kay Sievers312c0042005-11-16 09:00:00 +01002219static int dev_uevent_filter(struct kset *kset, struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220{
2221 struct kobj_type *ktype = get_ktype(kobj);
2222
Greg Kroah-Hartman8f4afc42007-10-11 10:47:49 -06002223 if (ktype == &device_ktype) {
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +02002224 struct device *dev = kobj_to_dev(kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225 if (dev->bus)
2226 return 1;
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07002227 if (dev->class)
2228 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229 }
2230 return 0;
2231}
2232
Kay Sievers312c0042005-11-16 09:00:00 +01002233static const char *dev_uevent_name(struct kset *kset, struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +02002235 struct device *dev = kobj_to_dev(kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07002237 if (dev->bus)
2238 return dev->bus->name;
2239 if (dev->class)
2240 return dev->class->name;
2241 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242}
2243
Kay Sievers7eff2e72007-08-14 15:15:12 +02002244static int dev_uevent(struct kset *kset, struct kobject *kobj,
2245 struct kobj_uevent_env *env)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +02002247 struct device *dev = kobj_to_dev(kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248 int retval = 0;
2249
Kay Sievers6fcf53a2009-04-30 15:23:42 +02002250 /* add device node properties if present */
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07002251 if (MAJOR(dev->devt)) {
Kay Sievers6fcf53a2009-04-30 15:23:42 +02002252 const char *tmp;
2253 const char *name;
Al Viro2c9ede52011-07-23 20:24:48 -04002254 umode_t mode = 0;
Greg Kroah-Hartman4e4098a2013-04-11 11:43:29 -07002255 kuid_t uid = GLOBAL_ROOT_UID;
2256 kgid_t gid = GLOBAL_ROOT_GID;
Kay Sievers6fcf53a2009-04-30 15:23:42 +02002257
Kay Sievers7eff2e72007-08-14 15:15:12 +02002258 add_uevent_var(env, "MAJOR=%u", MAJOR(dev->devt));
2259 add_uevent_var(env, "MINOR=%u", MINOR(dev->devt));
Kay Sievers3c2670e2013-04-06 09:56:00 -07002260 name = device_get_devnode(dev, &mode, &uid, &gid, &tmp);
Kay Sievers6fcf53a2009-04-30 15:23:42 +02002261 if (name) {
2262 add_uevent_var(env, "DEVNAME=%s", name);
Kay Sieverse454cea2009-09-18 23:01:12 +02002263 if (mode)
2264 add_uevent_var(env, "DEVMODE=%#o", mode & 0777);
Greg Kroah-Hartman4e4098a2013-04-11 11:43:29 -07002265 if (!uid_eq(uid, GLOBAL_ROOT_UID))
2266 add_uevent_var(env, "DEVUID=%u", from_kuid(&init_user_ns, uid));
2267 if (!gid_eq(gid, GLOBAL_ROOT_GID))
2268 add_uevent_var(env, "DEVGID=%u", from_kgid(&init_user_ns, gid));
Kay Sievers3c2670e2013-04-06 09:56:00 -07002269 kfree(tmp);
Kay Sievers6fcf53a2009-04-30 15:23:42 +02002270 }
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07002271 }
2272
Kay Sievers414264f2007-03-12 21:08:57 +01002273 if (dev->type && dev->type->name)
Kay Sievers7eff2e72007-08-14 15:15:12 +02002274 add_uevent_var(env, "DEVTYPE=%s", dev->type->name);
Kay Sievers414264f2007-03-12 21:08:57 +01002275
Kay Sievers239378f2006-10-07 21:54:55 +02002276 if (dev->driver)
Kay Sievers7eff2e72007-08-14 15:15:12 +02002277 add_uevent_var(env, "DRIVER=%s", dev->driver->name);
Kay Sievers239378f2006-10-07 21:54:55 +02002278
Grant Likely07d57a32012-02-01 11:22:22 -07002279 /* Add common DT information about the device */
2280 of_device_uevent(dev, env);
2281
Kay Sievers7eff2e72007-08-14 15:15:12 +02002282 /* have the bus specific function add its stuff */
Kay Sievers312c0042005-11-16 09:00:00 +01002283 if (dev->bus && dev->bus->uevent) {
Kay Sievers7eff2e72007-08-14 15:15:12 +02002284 retval = dev->bus->uevent(dev, env);
Kay Sieversf9f852d2006-10-07 21:54:55 +02002285 if (retval)
Greg Kroah-Hartman7dc72b22007-11-28 23:49:41 -08002286 pr_debug("device: '%s': %s: bus uevent() returned %d\n",
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01002287 dev_name(dev), __func__, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288 }
2289
Kay Sievers7eff2e72007-08-14 15:15:12 +02002290 /* have the class specific function add its stuff */
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07002291 if (dev->class && dev->class->dev_uevent) {
Kay Sievers7eff2e72007-08-14 15:15:12 +02002292 retval = dev->class->dev_uevent(dev, env);
Kay Sieversf9f852d2006-10-07 21:54:55 +02002293 if (retval)
Greg Kroah-Hartman7dc72b22007-11-28 23:49:41 -08002294 pr_debug("device: '%s': %s: class uevent() "
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01002295 "returned %d\n", dev_name(dev),
Harvey Harrison2b3a3022008-03-04 16:41:05 -08002296 __func__, retval);
Kay Sieversf9f852d2006-10-07 21:54:55 +02002297 }
2298
Stefan Weileef35c22010-08-06 21:11:15 +02002299 /* have the device type specific function add its stuff */
Kay Sieversf9f852d2006-10-07 21:54:55 +02002300 if (dev->type && dev->type->uevent) {
Kay Sievers7eff2e72007-08-14 15:15:12 +02002301 retval = dev->type->uevent(dev, env);
Kay Sieversf9f852d2006-10-07 21:54:55 +02002302 if (retval)
Greg Kroah-Hartman7dc72b22007-11-28 23:49:41 -08002303 pr_debug("device: '%s': %s: dev_type uevent() "
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01002304 "returned %d\n", dev_name(dev),
Harvey Harrison2b3a3022008-03-04 16:41:05 -08002305 __func__, retval);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07002306 }
2307
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 return retval;
2309}
2310
Emese Revfy9cd43612009-12-31 14:52:51 +01002311static const struct kset_uevent_ops device_uevent_ops = {
Kay Sievers312c0042005-11-16 09:00:00 +01002312 .filter = dev_uevent_filter,
2313 .name = dev_uevent_name,
2314 .uevent = dev_uevent,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315};
2316
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07002317static ssize_t uevent_show(struct device *dev, struct device_attribute *attr,
Kay Sievers16574dc2007-04-06 01:40:38 +02002318 char *buf)
2319{
2320 struct kobject *top_kobj;
2321 struct kset *kset;
Kay Sievers7eff2e72007-08-14 15:15:12 +02002322 struct kobj_uevent_env *env = NULL;
Kay Sievers16574dc2007-04-06 01:40:38 +02002323 int i;
Joe Perches948b3ed2020-09-16 13:40:42 -07002324 int len = 0;
Kay Sievers16574dc2007-04-06 01:40:38 +02002325 int retval;
2326
2327 /* search the kset, the device belongs to */
2328 top_kobj = &dev->kobj;
Kay Sievers5c5daf62007-08-12 20:43:55 +02002329 while (!top_kobj->kset && top_kobj->parent)
2330 top_kobj = top_kobj->parent;
Kay Sievers16574dc2007-04-06 01:40:38 +02002331 if (!top_kobj->kset)
2332 goto out;
Kay Sievers5c5daf62007-08-12 20:43:55 +02002333
Kay Sievers16574dc2007-04-06 01:40:38 +02002334 kset = top_kobj->kset;
2335 if (!kset->uevent_ops || !kset->uevent_ops->uevent)
2336 goto out;
2337
2338 /* respect filter */
2339 if (kset->uevent_ops && kset->uevent_ops->filter)
2340 if (!kset->uevent_ops->filter(kset, &dev->kobj))
2341 goto out;
2342
Kay Sievers7eff2e72007-08-14 15:15:12 +02002343 env = kzalloc(sizeof(struct kobj_uevent_env), GFP_KERNEL);
2344 if (!env)
Greg Kroah-Hartmanc7308c82007-05-02 14:14:11 +02002345 return -ENOMEM;
2346
Kay Sievers16574dc2007-04-06 01:40:38 +02002347 /* let the kset specific function add its keys */
Kay Sievers7eff2e72007-08-14 15:15:12 +02002348 retval = kset->uevent_ops->uevent(kset, &dev->kobj, env);
Kay Sievers16574dc2007-04-06 01:40:38 +02002349 if (retval)
2350 goto out;
2351
2352 /* copy keys to file */
Kay Sievers7eff2e72007-08-14 15:15:12 +02002353 for (i = 0; i < env->envp_idx; i++)
Joe Perches948b3ed2020-09-16 13:40:42 -07002354 len += sysfs_emit_at(buf, len, "%s\n", env->envp[i]);
Kay Sievers16574dc2007-04-06 01:40:38 +02002355out:
Kay Sievers7eff2e72007-08-14 15:15:12 +02002356 kfree(env);
Joe Perches948b3ed2020-09-16 13:40:42 -07002357 return len;
Kay Sievers16574dc2007-04-06 01:40:38 +02002358}
2359
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07002360static ssize_t uevent_store(struct device *dev, struct device_attribute *attr,
Kay Sieversa7fd6702005-10-01 14:49:43 +02002361 const char *buf, size_t count)
2362{
Peter Rajnohadf44b472018-12-05 12:27:44 +01002363 int rc;
2364
2365 rc = kobject_synth_uevent(&dev->kobj, buf, count);
2366
2367 if (rc) {
Peter Rajnohaf36776f2017-05-09 15:22:30 +02002368 dev_err(dev, "uevent: failed to send synthetic uevent\n");
Peter Rajnohadf44b472018-12-05 12:27:44 +01002369 return rc;
2370 }
Kay Sievers60a96a52007-07-08 22:29:26 +02002371
Kay Sieversa7fd6702005-10-01 14:49:43 +02002372 return count;
2373}
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07002374static DEVICE_ATTR_RW(uevent);
Kay Sieversa7fd6702005-10-01 14:49:43 +02002375
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07002376static ssize_t online_show(struct device *dev, struct device_attribute *attr,
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02002377 char *buf)
2378{
2379 bool val;
2380
Rafael J. Wysocki5e33bc42013-08-28 21:41:01 +02002381 device_lock(dev);
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02002382 val = !dev->offline;
Rafael J. Wysocki5e33bc42013-08-28 21:41:01 +02002383 device_unlock(dev);
Joe Perchesaa838892020-09-16 13:40:39 -07002384 return sysfs_emit(buf, "%u\n", val);
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02002385}
2386
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07002387static ssize_t online_store(struct device *dev, struct device_attribute *attr,
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02002388 const char *buf, size_t count)
2389{
2390 bool val;
2391 int ret;
2392
2393 ret = strtobool(buf, &val);
2394 if (ret < 0)
2395 return ret;
2396
Rafael J. Wysocki5e33bc42013-08-28 21:41:01 +02002397 ret = lock_device_hotplug_sysfs();
2398 if (ret)
2399 return ret;
2400
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02002401 ret = val ? device_online(dev) : device_offline(dev);
2402 unlock_device_hotplug();
2403 return ret < 0 ? ret : count;
2404}
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07002405static DEVICE_ATTR_RW(online);
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02002406
Greg Kroah-Hartmanfa6fdb32013-08-08 15:22:55 -07002407int device_add_groups(struct device *dev, const struct attribute_group **groups)
Dmitry Torokhov621a1672007-03-10 01:37:34 -05002408{
Greg Kroah-Hartman3e9b2ba2013-08-21 13:47:50 -07002409 return sysfs_create_groups(&dev->kobj, groups);
Dmitry Torokhov621a1672007-03-10 01:37:34 -05002410}
Dmitry Torokhova7670d42017-07-19 17:24:31 -07002411EXPORT_SYMBOL_GPL(device_add_groups);
Dmitry Torokhov621a1672007-03-10 01:37:34 -05002412
Greg Kroah-Hartmanfa6fdb32013-08-08 15:22:55 -07002413void device_remove_groups(struct device *dev,
2414 const struct attribute_group **groups)
Dmitry Torokhov621a1672007-03-10 01:37:34 -05002415{
Greg Kroah-Hartman3e9b2ba2013-08-21 13:47:50 -07002416 sysfs_remove_groups(&dev->kobj, groups);
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -07002417}
Dmitry Torokhova7670d42017-07-19 17:24:31 -07002418EXPORT_SYMBOL_GPL(device_remove_groups);
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -07002419
Dmitry Torokhov57b8ff02017-07-19 17:24:33 -07002420union device_attr_group_devres {
2421 const struct attribute_group *group;
2422 const struct attribute_group **groups;
2423};
2424
2425static int devm_attr_group_match(struct device *dev, void *res, void *data)
2426{
2427 return ((union device_attr_group_devres *)res)->group == data;
2428}
2429
2430static void devm_attr_group_remove(struct device *dev, void *res)
2431{
2432 union device_attr_group_devres *devres = res;
2433 const struct attribute_group *group = devres->group;
2434
2435 dev_dbg(dev, "%s: removing group %p\n", __func__, group);
2436 sysfs_remove_group(&dev->kobj, group);
2437}
2438
2439static void devm_attr_groups_remove(struct device *dev, void *res)
2440{
2441 union device_attr_group_devres *devres = res;
2442 const struct attribute_group **groups = devres->groups;
2443
2444 dev_dbg(dev, "%s: removing groups %p\n", __func__, groups);
2445 sysfs_remove_groups(&dev->kobj, groups);
2446}
2447
2448/**
2449 * devm_device_add_group - given a device, create a managed attribute group
2450 * @dev: The device to create the group for
2451 * @grp: The attribute group to create
2452 *
2453 * This function creates a group for the first time. It will explicitly
2454 * warn and error if any of the attribute files being created already exist.
2455 *
2456 * Returns 0 on success or error code on failure.
2457 */
2458int devm_device_add_group(struct device *dev, const struct attribute_group *grp)
2459{
2460 union device_attr_group_devres *devres;
2461 int error;
2462
2463 devres = devres_alloc(devm_attr_group_remove,
2464 sizeof(*devres), GFP_KERNEL);
2465 if (!devres)
2466 return -ENOMEM;
2467
2468 error = sysfs_create_group(&dev->kobj, grp);
2469 if (error) {
2470 devres_free(devres);
2471 return error;
2472 }
2473
2474 devres->group = grp;
2475 devres_add(dev, devres);
2476 return 0;
2477}
2478EXPORT_SYMBOL_GPL(devm_device_add_group);
2479
2480/**
2481 * devm_device_remove_group: remove a managed group from a device
2482 * @dev: device to remove the group from
2483 * @grp: group to remove
2484 *
2485 * This function removes a group of attributes from a device. The attributes
2486 * previously have to have been created for this group, otherwise it will fail.
2487 */
2488void devm_device_remove_group(struct device *dev,
2489 const struct attribute_group *grp)
2490{
2491 WARN_ON(devres_release(dev, devm_attr_group_remove,
2492 devm_attr_group_match,
2493 /* cast away const */ (void *)grp));
2494}
2495EXPORT_SYMBOL_GPL(devm_device_remove_group);
2496
2497/**
2498 * devm_device_add_groups - create a bunch of managed attribute groups
2499 * @dev: The device to create the group for
2500 * @groups: The attribute groups to create, NULL terminated
2501 *
2502 * This function creates a bunch of managed attribute groups. If an error
2503 * occurs when creating a group, all previously created groups will be
2504 * removed, unwinding everything back to the original state when this
2505 * function was called. It will explicitly warn and error if any of the
2506 * attribute files being created already exist.
2507 *
2508 * Returns 0 on success or error code from sysfs_create_group on failure.
2509 */
2510int devm_device_add_groups(struct device *dev,
2511 const struct attribute_group **groups)
2512{
2513 union device_attr_group_devres *devres;
2514 int error;
2515
2516 devres = devres_alloc(devm_attr_groups_remove,
2517 sizeof(*devres), GFP_KERNEL);
2518 if (!devres)
2519 return -ENOMEM;
2520
2521 error = sysfs_create_groups(&dev->kobj, groups);
2522 if (error) {
2523 devres_free(devres);
2524 return error;
2525 }
2526
2527 devres->groups = groups;
2528 devres_add(dev, devres);
2529 return 0;
2530}
2531EXPORT_SYMBOL_GPL(devm_device_add_groups);
2532
2533/**
2534 * devm_device_remove_groups - remove a list of managed groups
2535 *
2536 * @dev: The device for the groups to be removed from
2537 * @groups: NULL terminated list of groups to be removed
2538 *
2539 * If groups is not NULL, remove the specified groups from the device.
2540 */
2541void devm_device_remove_groups(struct device *dev,
2542 const struct attribute_group **groups)
2543{
2544 WARN_ON(devres_release(dev, devm_attr_groups_remove,
2545 devm_attr_group_match,
2546 /* cast away const */ (void *)groups));
2547}
2548EXPORT_SYMBOL_GPL(devm_device_remove_groups);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07002550static int device_add_attrs(struct device *dev)
2551{
2552 struct class *class = dev->class;
Stephen Hemmingeraed65af2011-03-28 09:12:52 -07002553 const struct device_type *type = dev->type;
Dmitry Torokhov621a1672007-03-10 01:37:34 -05002554 int error;
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07002555
Dmitry Torokhov621a1672007-03-10 01:37:34 -05002556 if (class) {
Greg Kroah-Hartmand05a6f92013-07-14 16:05:58 -07002557 error = device_add_groups(dev, class->dev_groups);
Kay Sieversf9f852d2006-10-07 21:54:55 +02002558 if (error)
Dmitry Torokhov621a1672007-03-10 01:37:34 -05002559 return error;
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07002560 }
Kay Sieversf9f852d2006-10-07 21:54:55 +02002561
Dmitry Torokhov621a1672007-03-10 01:37:34 -05002562 if (type) {
2563 error = device_add_groups(dev, type->groups);
Kay Sieversf9f852d2006-10-07 21:54:55 +02002564 if (error)
Greg Kroah-Hartmana6b01de2013-10-05 18:19:30 -07002565 goto err_remove_class_groups;
Kay Sieversf9f852d2006-10-07 21:54:55 +02002566 }
2567
Dmitry Torokhov621a1672007-03-10 01:37:34 -05002568 error = device_add_groups(dev, dev->groups);
2569 if (error)
2570 goto err_remove_type_groups;
2571
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02002572 if (device_supports_offline(dev) && !dev->offline_disabled) {
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07002573 error = device_create_file(dev, &dev_attr_online);
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02002574 if (error)
Rafael J. Wysockiecfbf6f2013-12-12 06:11:02 +01002575 goto err_remove_dev_groups;
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02002576 }
2577
Saravana Kannan25ac86c2020-11-20 18:02:28 -08002578 if (fw_devlink_flags && !fw_devlink_is_permissive() && dev->fwnode) {
Saravana Kannanda6d6472020-05-21 12:18:00 -07002579 error = device_create_file(dev, &dev_attr_waiting_for_supplier);
2580 if (error)
2581 goto err_remove_dev_online;
2582 }
2583
Dmitry Torokhov621a1672007-03-10 01:37:34 -05002584 return 0;
2585
Saravana Kannanda6d6472020-05-21 12:18:00 -07002586 err_remove_dev_online:
2587 device_remove_file(dev, &dev_attr_online);
Rafael J. Wysockiecfbf6f2013-12-12 06:11:02 +01002588 err_remove_dev_groups:
2589 device_remove_groups(dev, dev->groups);
Dmitry Torokhov621a1672007-03-10 01:37:34 -05002590 err_remove_type_groups:
2591 if (type)
2592 device_remove_groups(dev, type->groups);
Greg Kroah-Hartmand05a6f92013-07-14 16:05:58 -07002593 err_remove_class_groups:
2594 if (class)
2595 device_remove_groups(dev, class->dev_groups);
Dmitry Torokhov621a1672007-03-10 01:37:34 -05002596
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07002597 return error;
2598}
2599
2600static void device_remove_attrs(struct device *dev)
2601{
2602 struct class *class = dev->class;
Stephen Hemmingeraed65af2011-03-28 09:12:52 -07002603 const struct device_type *type = dev->type;
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07002604
Saravana Kannanda6d6472020-05-21 12:18:00 -07002605 device_remove_file(dev, &dev_attr_waiting_for_supplier);
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07002606 device_remove_file(dev, &dev_attr_online);
Dmitry Torokhov621a1672007-03-10 01:37:34 -05002607 device_remove_groups(dev, dev->groups);
Kay Sieversf9f852d2006-10-07 21:54:55 +02002608
Dmitry Torokhov621a1672007-03-10 01:37:34 -05002609 if (type)
2610 device_remove_groups(dev, type->groups);
2611
Greg Kroah-Hartmana6b01de2013-10-05 18:19:30 -07002612 if (class)
Greg Kroah-Hartmand05a6f92013-07-14 16:05:58 -07002613 device_remove_groups(dev, class->dev_groups);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07002614}
2615
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07002616static ssize_t dev_show(struct device *dev, struct device_attribute *attr,
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07002617 char *buf)
2618{
2619 return print_dev_t(buf, dev->devt);
2620}
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07002621static DEVICE_ATTR_RO(dev);
Tejun Heoad6a1e12007-06-14 03:45:17 +09002622
Kay Sieversca22e562011-12-14 14:29:38 -08002623/* /sys/devices/ */
Greg Kroah-Hartman881c6cfd2007-11-01 09:29:06 -06002624struct kset *devices_kset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626/**
Grygorii Strashko52cdbdd2015-07-27 20:43:01 +03002627 * devices_kset_move_before - Move device in the devices_kset's list.
2628 * @deva: Device to move.
2629 * @devb: Device @deva should come before.
2630 */
2631static void devices_kset_move_before(struct device *deva, struct device *devb)
2632{
2633 if (!devices_kset)
2634 return;
2635 pr_debug("devices_kset: Moving %s before %s\n",
2636 dev_name(deva), dev_name(devb));
2637 spin_lock(&devices_kset->list_lock);
2638 list_move_tail(&deva->kobj.entry, &devb->kobj.entry);
2639 spin_unlock(&devices_kset->list_lock);
2640}
2641
2642/**
2643 * devices_kset_move_after - Move device in the devices_kset's list.
2644 * @deva: Device to move
2645 * @devb: Device @deva should come after.
2646 */
2647static void devices_kset_move_after(struct device *deva, struct device *devb)
2648{
2649 if (!devices_kset)
2650 return;
2651 pr_debug("devices_kset: Moving %s after %s\n",
2652 dev_name(deva), dev_name(devb));
2653 spin_lock(&devices_kset->list_lock);
2654 list_move(&deva->kobj.entry, &devb->kobj.entry);
2655 spin_unlock(&devices_kset->list_lock);
2656}
2657
2658/**
2659 * devices_kset_move_last - move the device to the end of devices_kset's list.
2660 * @dev: device to move
2661 */
2662void devices_kset_move_last(struct device *dev)
2663{
2664 if (!devices_kset)
2665 return;
2666 pr_debug("devices_kset: Moving %s to end of list\n", dev_name(dev));
2667 spin_lock(&devices_kset->list_lock);
2668 list_move_tail(&dev->kobj.entry, &devices_kset->list);
2669 spin_unlock(&devices_kset->list_lock);
2670}
2671
2672/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002673 * device_create_file - create sysfs attribute file for device.
2674 * @dev: device.
2675 * @attr: device attribute descriptor.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676 */
Phil Carmody26579ab2009-12-18 15:34:19 +02002677int device_create_file(struct device *dev,
2678 const struct device_attribute *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679{
2680 int error = 0;
Felipe Balbi8f46baa2013-02-20 10:31:42 +02002681
2682 if (dev) {
2683 WARN(((attr->attr.mode & S_IWUGO) && !attr->store),
dyoung@redhat.com97521972013-05-16 14:31:30 +08002684 "Attribute %s: write permission without 'store'\n",
2685 attr->attr.name);
Felipe Balbi8f46baa2013-02-20 10:31:42 +02002686 WARN(((attr->attr.mode & S_IRUGO) && !attr->show),
dyoung@redhat.com97521972013-05-16 14:31:30 +08002687 "Attribute %s: read permission without 'show'\n",
2688 attr->attr.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689 error = sysfs_create_file(&dev->kobj, &attr->attr);
Felipe Balbi8f46baa2013-02-20 10:31:42 +02002690 }
2691
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692 return error;
2693}
David Graham White86df2682013-07-21 20:41:14 -04002694EXPORT_SYMBOL_GPL(device_create_file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695
2696/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002697 * device_remove_file - remove sysfs attribute file.
2698 * @dev: device.
2699 * @attr: device attribute descriptor.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700 */
Phil Carmody26579ab2009-12-18 15:34:19 +02002701void device_remove_file(struct device *dev,
2702 const struct device_attribute *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703{
Cornelia Huck0c98b192008-01-31 10:39:38 +01002704 if (dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705 sysfs_remove_file(&dev->kobj, &attr->attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706}
David Graham White86df2682013-07-21 20:41:14 -04002707EXPORT_SYMBOL_GPL(device_remove_file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708
Greg Kroah-Hartman2589f1882006-09-19 09:39:19 -07002709/**
Tejun Heo6b0afc22014-02-03 14:03:01 -05002710 * device_remove_file_self - remove sysfs attribute file from its own method.
2711 * @dev: device.
2712 * @attr: device attribute descriptor.
2713 *
2714 * See kernfs_remove_self() for details.
2715 */
2716bool device_remove_file_self(struct device *dev,
2717 const struct device_attribute *attr)
2718{
2719 if (dev)
2720 return sysfs_remove_file_self(&dev->kobj, &attr->attr);
2721 else
2722 return false;
2723}
2724EXPORT_SYMBOL_GPL(device_remove_file_self);
2725
2726/**
Greg Kroah-Hartman2589f1882006-09-19 09:39:19 -07002727 * device_create_bin_file - create sysfs binary attribute file for device.
2728 * @dev: device.
2729 * @attr: device binary attribute descriptor.
2730 */
Phil Carmody66ecb922009-12-18 15:34:20 +02002731int device_create_bin_file(struct device *dev,
2732 const struct bin_attribute *attr)
Greg Kroah-Hartman2589f1882006-09-19 09:39:19 -07002733{
2734 int error = -EINVAL;
2735 if (dev)
2736 error = sysfs_create_bin_file(&dev->kobj, attr);
2737 return error;
2738}
2739EXPORT_SYMBOL_GPL(device_create_bin_file);
2740
2741/**
2742 * device_remove_bin_file - remove sysfs binary attribute file
2743 * @dev: device.
2744 * @attr: device binary attribute descriptor.
2745 */
Phil Carmody66ecb922009-12-18 15:34:20 +02002746void device_remove_bin_file(struct device *dev,
2747 const struct bin_attribute *attr)
Greg Kroah-Hartman2589f1882006-09-19 09:39:19 -07002748{
2749 if (dev)
2750 sysfs_remove_bin_file(&dev->kobj, attr);
2751}
2752EXPORT_SYMBOL_GPL(device_remove_bin_file);
2753
James Bottomley34bb61f2005-09-06 16:56:51 -07002754static void klist_children_get(struct klist_node *n)
2755{
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08002756 struct device_private *p = to_device_private_parent(n);
2757 struct device *dev = p->device;
James Bottomley34bb61f2005-09-06 16:56:51 -07002758
2759 get_device(dev);
2760}
2761
2762static void klist_children_put(struct klist_node *n)
2763{
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08002764 struct device_private *p = to_device_private_parent(n);
2765 struct device *dev = p->device;
James Bottomley34bb61f2005-09-06 16:56:51 -07002766
2767 put_device(dev);
2768}
2769
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002771 * device_initialize - init device structure.
2772 * @dev: device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773 *
Cornelia Huck57394112008-09-03 18:26:40 +02002774 * This prepares the device for use by other layers by initializing
2775 * its fields.
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002776 * It is the first half of device_register(), if called by
Cornelia Huck57394112008-09-03 18:26:40 +02002777 * that function, though it can also be called separately, so one
2778 * may use @dev's fields. In particular, get_device()/put_device()
2779 * may be used for reference counting of @dev after calling this
2780 * function.
2781 *
Alan Sternb10d5ef2012-01-17 11:39:00 -05002782 * All fields in @dev must be initialized by the caller to 0, except
2783 * for those explicitly set to some other value. The simplest
2784 * approach is to use kzalloc() to allocate the structure containing
2785 * @dev.
2786 *
Cornelia Huck57394112008-09-03 18:26:40 +02002787 * NOTE: Use put_device() to give up your reference instead of freeing
2788 * @dev directly once you have called this function.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790void device_initialize(struct device *dev)
2791{
Greg Kroah-Hartman881c6cfd2007-11-01 09:29:06 -06002792 dev->kobj.kset = devices_kset;
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -07002793 kobject_init(&dev->kobj, &device_ktype);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794 INIT_LIST_HEAD(&dev->dma_pools);
Thomas Gleixner31427882010-01-29 20:39:02 +00002795 mutex_init(&dev->mutex);
Dan Williams87a30e12019-07-17 18:08:26 -07002796#ifdef CONFIG_PROVE_LOCKING
2797 mutex_init(&dev->lockdep_mutex);
2798#endif
Peter Zijlstra1704f472010-03-19 01:37:42 +01002799 lockdep_set_novalidate_class(&dev->mutex);
Tejun Heo9ac78492007-01-20 16:00:26 +09002800 spin_lock_init(&dev->devres_lock);
2801 INIT_LIST_HEAD(&dev->devres_head);
Alan Stern3b98aea2008-08-07 13:06:12 -04002802 device_pm_init(dev);
Christoph Hellwig87348132006-12-06 20:32:33 -08002803 set_dev_node(dev, -1);
Jiang Liu4a7cc832015-07-09 16:00:44 +08002804#ifdef CONFIG_GENERIC_MSI_IRQ
2805 INIT_LIST_HEAD(&dev->msi_list);
2806#endif
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01002807 INIT_LIST_HEAD(&dev->links.consumers);
2808 INIT_LIST_HEAD(&dev->links.suppliers);
Saravana Kannan3b052a32020-11-20 18:02:17 -08002809 INIT_LIST_HEAD(&dev->links.defer_sync);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01002810 dev->links.status = DL_DEV_NO_DRIVER;
Christoph Hellwig6d4e9a82021-02-10 10:56:39 +01002811#if defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_DEVICE) || \
2812 defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU) || \
2813 defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU_ALL)
2814 dev->dma_coherent = dma_default_coherent;
2815#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816}
David Graham White86df2682013-07-21 20:41:14 -04002817EXPORT_SYMBOL_GPL(device_initialize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818
Tejun Heod73ce002013-03-12 11:30:05 -07002819struct kobject *virtual_device_parent(struct device *dev)
Greg Kroah-Hartmanf0ee61a2006-10-23 10:40:54 -07002820{
Kay Sievers86406242007-03-14 03:25:56 +01002821 static struct kobject *virtual_dir = NULL;
Greg Kroah-Hartmanf0ee61a2006-10-23 10:40:54 -07002822
Kay Sievers86406242007-03-14 03:25:56 +01002823 if (!virtual_dir)
Greg Kroah-Hartman4ff6abf2007-11-05 22:24:43 -08002824 virtual_dir = kobject_create_and_add("virtual",
Greg Kroah-Hartman881c6cfd2007-11-01 09:29:06 -06002825 &devices_kset->kobj);
Greg Kroah-Hartmanf0ee61a2006-10-23 10:40:54 -07002826
Kay Sievers86406242007-03-14 03:25:56 +01002827 return virtual_dir;
Greg Kroah-Hartmanf0ee61a2006-10-23 10:40:54 -07002828}
2829
Eric W. Biedermanbc451f22010-03-30 11:31:25 -07002830struct class_dir {
2831 struct kobject kobj;
2832 struct class *class;
2833};
2834
2835#define to_class_dir(obj) container_of(obj, struct class_dir, kobj)
2836
2837static void class_dir_release(struct kobject *kobj)
2838{
2839 struct class_dir *dir = to_class_dir(kobj);
2840 kfree(dir);
2841}
2842
2843static const
2844struct kobj_ns_type_operations *class_dir_child_ns_type(struct kobject *kobj)
2845{
2846 struct class_dir *dir = to_class_dir(kobj);
2847 return dir->class->ns_type;
2848}
2849
2850static struct kobj_type class_dir_ktype = {
2851 .release = class_dir_release,
2852 .sysfs_ops = &kobj_sysfs_ops,
2853 .child_ns_type = class_dir_child_ns_type
2854};
2855
2856static struct kobject *
2857class_dir_create_and_add(struct class *class, struct kobject *parent_kobj)
2858{
2859 struct class_dir *dir;
2860 int retval;
2861
2862 dir = kzalloc(sizeof(*dir), GFP_KERNEL);
2863 if (!dir)
Tetsuo Handa84d0c272018-05-07 19:10:31 +09002864 return ERR_PTR(-ENOMEM);
Eric W. Biedermanbc451f22010-03-30 11:31:25 -07002865
2866 dir->class = class;
2867 kobject_init(&dir->kobj, &class_dir_ktype);
2868
Kay Sievers6b6e39a2010-11-15 23:13:18 +01002869 dir->kobj.kset = &class->p->glue_dirs;
Eric W. Biedermanbc451f22010-03-30 11:31:25 -07002870
2871 retval = kobject_add(&dir->kobj, parent_kobj, "%s", class->name);
2872 if (retval < 0) {
2873 kobject_put(&dir->kobj);
Tetsuo Handa84d0c272018-05-07 19:10:31 +09002874 return ERR_PTR(retval);
Eric W. Biedermanbc451f22010-03-30 11:31:25 -07002875 }
2876 return &dir->kobj;
2877}
2878
Yijing Wange4a60d12014-11-07 12:05:49 +08002879static DEFINE_MUTEX(gdp_mutex);
Eric W. Biedermanbc451f22010-03-30 11:31:25 -07002880
Kay Sieversda231fd2007-11-21 17:29:15 +01002881static struct kobject *get_device_parent(struct device *dev,
2882 struct device *parent)
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +01002883{
Kay Sievers86406242007-03-14 03:25:56 +01002884 if (dev->class) {
2885 struct kobject *kobj = NULL;
2886 struct kobject *parent_kobj;
2887 struct kobject *k;
2888
Randy Dunlapead454f2010-09-24 14:36:49 -07002889#ifdef CONFIG_BLOCK
Kay Sievers39aba962010-09-04 22:33:14 -07002890 /* block disks show up in /sys/block */
Andi Kleene52eec12010-09-08 16:54:17 +02002891 if (sysfs_deprecated && dev->class == &block_class) {
Kay Sievers39aba962010-09-04 22:33:14 -07002892 if (parent && parent->class == &block_class)
2893 return &parent->kobj;
Kay Sievers6b6e39a2010-11-15 23:13:18 +01002894 return &block_class.p->subsys.kobj;
Kay Sievers39aba962010-09-04 22:33:14 -07002895 }
Randy Dunlapead454f2010-09-24 14:36:49 -07002896#endif
Andi Kleene52eec12010-09-08 16:54:17 +02002897
Kay Sievers86406242007-03-14 03:25:56 +01002898 /*
2899 * If we have no parent, we live in "virtual".
Kay Sievers0f4dafc2007-12-19 01:40:42 +01002900 * Class-devices with a non class-device as parent, live
2901 * in a "glue" directory to prevent namespace collisions.
Kay Sievers86406242007-03-14 03:25:56 +01002902 */
2903 if (parent == NULL)
2904 parent_kobj = virtual_device_parent(dev);
Eric W. Biederman24b14422010-07-24 22:43:35 -07002905 else if (parent->class && !dev->class->ns_type)
Kay Sievers86406242007-03-14 03:25:56 +01002906 return &parent->kobj;
2907 else
2908 parent_kobj = &parent->kobj;
2909
Tejun Heo77d3d7c2010-02-05 17:57:02 +09002910 mutex_lock(&gdp_mutex);
2911
Kay Sievers86406242007-03-14 03:25:56 +01002912 /* find our class-directory at the parent and reference it */
Kay Sievers6b6e39a2010-11-15 23:13:18 +01002913 spin_lock(&dev->class->p->glue_dirs.list_lock);
2914 list_for_each_entry(k, &dev->class->p->glue_dirs.list, entry)
Kay Sievers86406242007-03-14 03:25:56 +01002915 if (k->parent == parent_kobj) {
2916 kobj = kobject_get(k);
2917 break;
2918 }
Kay Sievers6b6e39a2010-11-15 23:13:18 +01002919 spin_unlock(&dev->class->p->glue_dirs.list_lock);
Tejun Heo77d3d7c2010-02-05 17:57:02 +09002920 if (kobj) {
2921 mutex_unlock(&gdp_mutex);
Kay Sievers86406242007-03-14 03:25:56 +01002922 return kobj;
Tejun Heo77d3d7c2010-02-05 17:57:02 +09002923 }
Kay Sievers86406242007-03-14 03:25:56 +01002924
2925 /* or create a new class-directory at the parent device */
Eric W. Biedermanbc451f22010-03-30 11:31:25 -07002926 k = class_dir_create_and_add(dev->class, parent_kobj);
Kay Sievers0f4dafc2007-12-19 01:40:42 +01002927 /* do not emit an uevent for this simple "glue" directory */
Tejun Heo77d3d7c2010-02-05 17:57:02 +09002928 mutex_unlock(&gdp_mutex);
Greg Kroah-Hartman43968d22007-11-05 22:24:43 -08002929 return k;
Kay Sievers86406242007-03-14 03:25:56 +01002930 }
2931
Kay Sieversca22e562011-12-14 14:29:38 -08002932 /* subsystems can specify a default root directory for their devices */
2933 if (!parent && dev->bus && dev->bus->dev_root)
2934 return &dev->bus->dev_root->kobj;
2935
Kay Sievers86406242007-03-14 03:25:56 +01002936 if (parent)
Cornelia Huckc744aeae2007-01-08 20:16:44 +01002937 return &parent->kobj;
2938 return NULL;
2939}
Kay Sieversda231fd2007-11-21 17:29:15 +01002940
Ming Leicebf8fd2016-07-10 19:27:36 +08002941static inline bool live_in_glue_dir(struct kobject *kobj,
2942 struct device *dev)
2943{
2944 if (!kobj || !dev->class ||
2945 kobj->kset != &dev->class->p->glue_dirs)
2946 return false;
2947 return true;
2948}
2949
2950static inline struct kobject *get_glue_dir(struct device *dev)
2951{
2952 return dev->kobj.parent;
2953}
2954
2955/*
2956 * make sure cleaning up dir as the last step, we need to make
2957 * sure .release handler of kobject is run with holding the
2958 * global lock
2959 */
Cornelia Huck63b69712008-01-21 16:09:44 +01002960static void cleanup_glue_dir(struct device *dev, struct kobject *glue_dir)
Kay Sieversda231fd2007-11-21 17:29:15 +01002961{
Muchun Songac434322019-07-27 11:21:22 +08002962 unsigned int ref;
2963
Kay Sievers0f4dafc2007-12-19 01:40:42 +01002964 /* see if we live in a "glue" directory */
Ming Leicebf8fd2016-07-10 19:27:36 +08002965 if (!live_in_glue_dir(glue_dir, dev))
Kay Sieversda231fd2007-11-21 17:29:15 +01002966 return;
2967
Yijing Wange4a60d12014-11-07 12:05:49 +08002968 mutex_lock(&gdp_mutex);
Muchun Songac434322019-07-27 11:21:22 +08002969 /**
2970 * There is a race condition between removing glue directory
2971 * and adding a new device under the glue directory.
2972 *
2973 * CPU1: CPU2:
2974 *
2975 * device_add()
2976 * get_device_parent()
2977 * class_dir_create_and_add()
2978 * kobject_add_internal()
2979 * create_dir() // create glue_dir
2980 *
2981 * device_add()
2982 * get_device_parent()
2983 * kobject_get() // get glue_dir
2984 *
2985 * device_del()
2986 * cleanup_glue_dir()
2987 * kobject_del(glue_dir)
2988 *
2989 * kobject_add()
2990 * kobject_add_internal()
2991 * create_dir() // in glue_dir
2992 * sysfs_create_dir_ns()
2993 * kernfs_create_dir_ns(sd)
2994 *
2995 * sysfs_remove_dir() // glue_dir->sd=NULL
2996 * sysfs_put() // free glue_dir->sd
2997 *
2998 * // sd is freed
2999 * kernfs_new_node(sd)
3000 * kernfs_get(glue_dir)
3001 * kernfs_add_one()
3002 * kernfs_put()
3003 *
3004 * Before CPU1 remove last child device under glue dir, if CPU2 add
3005 * a new device under glue dir, the glue_dir kobject reference count
3006 * will be increase to 2 in kobject_get(k). And CPU2 has been called
3007 * kernfs_create_dir_ns(). Meanwhile, CPU1 call sysfs_remove_dir()
3008 * and sysfs_put(). This result in glue_dir->sd is freed.
3009 *
3010 * Then the CPU2 will see a stale "empty" but still potentially used
3011 * glue dir around in kernfs_new_node().
3012 *
3013 * In order to avoid this happening, we also should make sure that
3014 * kernfs_node for glue_dir is released in CPU1 only when refcount
3015 * for glue_dir kobj is 1.
3016 */
3017 ref = kref_read(&glue_dir->kref);
3018 if (!kobject_has_children(glue_dir) && !--ref)
Benjamin Herrenschmidt726e4102018-07-10 10:29:10 +10003019 kobject_del(glue_dir);
Kay Sievers0f4dafc2007-12-19 01:40:42 +01003020 kobject_put(glue_dir);
Yijing Wange4a60d12014-11-07 12:05:49 +08003021 mutex_unlock(&gdp_mutex);
Kay Sieversda231fd2007-11-21 17:29:15 +01003022}
Cornelia Huck63b69712008-01-21 16:09:44 +01003023
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07003024static int device_add_class_symlinks(struct device *dev)
3025{
Benjamin Herrenschmidt5590f312015-02-18 11:25:18 +11003026 struct device_node *of_node = dev_of_node(dev);
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07003027 int error;
3028
Benjamin Herrenschmidt5590f312015-02-18 11:25:18 +11003029 if (of_node) {
Rob Herring0c3c2342017-10-04 14:04:01 -05003030 error = sysfs_create_link(&dev->kobj, of_node_kobj(of_node), "of_node");
Benjamin Herrenschmidt5590f312015-02-18 11:25:18 +11003031 if (error)
3032 dev_warn(dev, "Error %d creating of_node link\n",error);
3033 /* An error here doesn't warrant bringing down the device */
3034 }
3035
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07003036 if (!dev->class)
3037 return 0;
Kay Sieversda231fd2007-11-21 17:29:15 +01003038
Greg Kroah-Hartman1fbfee62008-05-28 09:28:39 -07003039 error = sysfs_create_link(&dev->kobj,
Kay Sievers6b6e39a2010-11-15 23:13:18 +01003040 &dev->class->p->subsys.kobj,
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07003041 "subsystem");
3042 if (error)
Benjamin Herrenschmidt5590f312015-02-18 11:25:18 +11003043 goto out_devnode;
Kay Sieversda231fd2007-11-21 17:29:15 +01003044
Greg Kroah-Hartman4e886c22008-01-27 12:12:43 -08003045 if (dev->parent && device_is_not_partition(dev)) {
Dmitry Torokhov4f01a752007-09-18 22:46:50 -07003046 error = sysfs_create_link(&dev->kobj, &dev->parent->kobj,
3047 "device");
3048 if (error)
Kay Sievers39aba962010-09-04 22:33:14 -07003049 goto out_subsys;
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07003050 }
Kay Sievers39aba962010-09-04 22:33:14 -07003051
Randy Dunlapead454f2010-09-24 14:36:49 -07003052#ifdef CONFIG_BLOCK
Kay Sievers39aba962010-09-04 22:33:14 -07003053 /* /sys/block has directories and does not need symlinks */
Andi Kleene52eec12010-09-08 16:54:17 +02003054 if (sysfs_deprecated && dev->class == &block_class)
Kay Sievers39aba962010-09-04 22:33:14 -07003055 return 0;
Randy Dunlapead454f2010-09-24 14:36:49 -07003056#endif
Kay Sievers39aba962010-09-04 22:33:14 -07003057
3058 /* link in the class directory pointing to the device */
Kay Sievers6b6e39a2010-11-15 23:13:18 +01003059 error = sysfs_create_link(&dev->class->p->subsys.kobj,
Kay Sievers39aba962010-09-04 22:33:14 -07003060 &dev->kobj, dev_name(dev));
3061 if (error)
3062 goto out_device;
3063
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07003064 return 0;
3065
Kay Sievers39aba962010-09-04 22:33:14 -07003066out_device:
3067 sysfs_remove_link(&dev->kobj, "device");
Kay Sieversda231fd2007-11-21 17:29:15 +01003068
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07003069out_subsys:
3070 sysfs_remove_link(&dev->kobj, "subsystem");
Benjamin Herrenschmidt5590f312015-02-18 11:25:18 +11003071out_devnode:
3072 sysfs_remove_link(&dev->kobj, "of_node");
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07003073 return error;
3074}
3075
3076static void device_remove_class_symlinks(struct device *dev)
3077{
Benjamin Herrenschmidt5590f312015-02-18 11:25:18 +11003078 if (dev_of_node(dev))
3079 sysfs_remove_link(&dev->kobj, "of_node");
3080
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07003081 if (!dev->class)
3082 return;
Kay Sieversda231fd2007-11-21 17:29:15 +01003083
Greg Kroah-Hartman4e886c22008-01-27 12:12:43 -08003084 if (dev->parent && device_is_not_partition(dev))
Kay Sieversda231fd2007-11-21 17:29:15 +01003085 sysfs_remove_link(&dev->kobj, "device");
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07003086 sysfs_remove_link(&dev->kobj, "subsystem");
Randy Dunlapead454f2010-09-24 14:36:49 -07003087#ifdef CONFIG_BLOCK
Andi Kleene52eec12010-09-08 16:54:17 +02003088 if (sysfs_deprecated && dev->class == &block_class)
Kay Sievers39aba962010-09-04 22:33:14 -07003089 return;
Randy Dunlapead454f2010-09-24 14:36:49 -07003090#endif
Kay Sievers6b6e39a2010-11-15 23:13:18 +01003091 sysfs_delete_link(&dev->class->p->subsys.kobj, &dev->kobj, dev_name(dev));
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07003092}
3093
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094/**
Stephen Rothwell413c2392008-05-30 10:16:40 +10003095 * dev_set_name - set a device name
3096 * @dev: device
Randy Dunlap46232362008-06-04 21:40:43 -07003097 * @fmt: format string for the device's name
Stephen Rothwell413c2392008-05-30 10:16:40 +10003098 */
3099int dev_set_name(struct device *dev, const char *fmt, ...)
3100{
3101 va_list vargs;
Kay Sievers1fa5ae82009-01-25 15:17:37 +01003102 int err;
Stephen Rothwell413c2392008-05-30 10:16:40 +10003103
3104 va_start(vargs, fmt);
Kay Sievers1fa5ae82009-01-25 15:17:37 +01003105 err = kobject_set_name_vargs(&dev->kobj, fmt, vargs);
Stephen Rothwell413c2392008-05-30 10:16:40 +10003106 va_end(vargs);
Kay Sievers1fa5ae82009-01-25 15:17:37 +01003107 return err;
Stephen Rothwell413c2392008-05-30 10:16:40 +10003108}
3109EXPORT_SYMBOL_GPL(dev_set_name);
3110
3111/**
Dan Williamse105b8b2008-04-21 10:51:07 -07003112 * device_to_dev_kobj - select a /sys/dev/ directory for the device
3113 * @dev: device
3114 *
3115 * By default we select char/ for new entries. Setting class->dev_obj
3116 * to NULL prevents an entry from being created. class->dev_kobj must
3117 * be set (or cleared) before any devices are registered to the class
3118 * otherwise device_create_sys_dev_entry() and
Peter Korsgaard0d4e293c2012-04-17 12:12:57 +02003119 * device_remove_sys_dev_entry() will disagree about the presence of
3120 * the link.
Dan Williamse105b8b2008-04-21 10:51:07 -07003121 */
3122static struct kobject *device_to_dev_kobj(struct device *dev)
3123{
3124 struct kobject *kobj;
3125
3126 if (dev->class)
3127 kobj = dev->class->dev_kobj;
3128 else
3129 kobj = sysfs_dev_char_kobj;
3130
3131 return kobj;
3132}
3133
3134static int device_create_sys_dev_entry(struct device *dev)
3135{
3136 struct kobject *kobj = device_to_dev_kobj(dev);
3137 int error = 0;
3138 char devt_str[15];
3139
3140 if (kobj) {
3141 format_dev_t(devt_str, dev->devt);
3142 error = sysfs_create_link(kobj, &dev->kobj, devt_str);
3143 }
3144
3145 return error;
3146}
3147
3148static void device_remove_sys_dev_entry(struct device *dev)
3149{
3150 struct kobject *kobj = device_to_dev_kobj(dev);
3151 char devt_str[15];
3152
3153 if (kobj) {
3154 format_dev_t(devt_str, dev->devt);
3155 sysfs_remove_link(kobj, devt_str);
3156 }
3157}
3158
Shaokun Zhang46d3a032018-07-15 18:08:56 +08003159static int device_private_init(struct device *dev)
Greg Kroah-Hartmanb4028432009-05-11 14:16:57 -07003160{
3161 dev->p = kzalloc(sizeof(*dev->p), GFP_KERNEL);
3162 if (!dev->p)
3163 return -ENOMEM;
3164 dev->p->device = dev;
3165 klist_init(&dev->p->klist_children, klist_children_get,
3166 klist_children_put);
Greg Kroah-Hartmanef8a3fd2012-03-08 12:17:22 -08003167 INIT_LIST_HEAD(&dev->p->deferred_probe);
Greg Kroah-Hartmanb4028432009-05-11 14:16:57 -07003168 return 0;
3169}
3170
Dan Williamse105b8b2008-04-21 10:51:07 -07003171/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08003172 * device_add - add device to device hierarchy.
3173 * @dev: device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003174 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08003175 * This is part 2 of device_register(), though may be called
3176 * separately _iff_ device_initialize() has been called separately.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003177 *
Cornelia Huck57394112008-09-03 18:26:40 +02003178 * This adds @dev to the kobject hierarchy via kobject_add(), adds it
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08003179 * to the global and sibling lists for the device, then
3180 * adds it to the other relevant subsystems of the driver model.
Cornelia Huck57394112008-09-03 18:26:40 +02003181 *
Alan Sternb10d5ef2012-01-17 11:39:00 -05003182 * Do not call this routine or device_register() more than once for
3183 * any device structure. The driver model core is not designed to work
3184 * with devices that get unregistered and then spring back to life.
3185 * (Among other things, it's very hard to guarantee that all references
3186 * to the previous incarnation of @dev have been dropped.) Allocate
3187 * and register a fresh new struct device instead.
3188 *
Cornelia Huck57394112008-09-03 18:26:40 +02003189 * NOTE: _Never_ directly free @dev after calling this function, even
3190 * if it returned an error! Always use put_device() to give up your
3191 * reference instead.
Borislav Petkovaffada72019-04-18 19:41:56 +02003192 *
3193 * Rule of thumb is: if device_add() succeeds, you should call
3194 * device_del() when you want to get rid of it. If device_add() has
3195 * *not* succeeded, use *only* put_device() to drop the reference
3196 * count.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197 */
3198int device_add(struct device *dev)
3199{
Viresh Kumar35dbf4e2017-03-17 12:24:22 +05303200 struct device *parent;
Kay Sieversca22e562011-12-14 14:29:38 -08003201 struct kobject *kobj;
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02003202 struct class_interface *class_intf;
Saravana Kannan5f5377e2020-05-14 22:34:58 -07003203 int error = -EINVAL;
Ming Leicebf8fd2016-07-10 19:27:36 +08003204 struct kobject *glue_dir = NULL;
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01003205
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206 dev = get_device(dev);
Greg Kroah-Hartmanc906a482008-05-30 10:45:12 -07003207 if (!dev)
3208 goto done;
3209
Greg Kroah-Hartmanfb069a52008-12-16 12:23:36 -08003210 if (!dev->p) {
Greg Kroah-Hartmanb4028432009-05-11 14:16:57 -07003211 error = device_private_init(dev);
3212 if (error)
3213 goto done;
Greg Kroah-Hartmanfb069a52008-12-16 12:23:36 -08003214 }
Greg Kroah-Hartmanfb069a52008-12-16 12:23:36 -08003215
Kay Sievers1fa5ae82009-01-25 15:17:37 +01003216 /*
3217 * for statically allocated devices, which should all be converted
3218 * some day, we need to initialize the name. We prevent reading back
3219 * the name, and force the use of dev_name()
3220 */
3221 if (dev->init_name) {
Greg Kroah-Hartmanacc0e902009-06-02 15:39:55 -07003222 dev_set_name(dev, "%s", dev->init_name);
Kay Sievers1fa5ae82009-01-25 15:17:37 +01003223 dev->init_name = NULL;
3224 }
Greg Kroah-Hartmanc906a482008-05-30 10:45:12 -07003225
Kay Sieversca22e562011-12-14 14:29:38 -08003226 /* subsystems can specify simple device enumeration */
3227 if (!dev_name(dev) && dev->bus && dev->bus->dev_name)
3228 dev_set_name(dev, "%s%u", dev->bus->dev_name, dev->id);
3229
Thomas Gleixnere6309e72009-12-10 19:32:49 +00003230 if (!dev_name(dev)) {
3231 error = -EINVAL;
Kay Sievers5c8563d2009-05-28 14:24:07 -07003232 goto name_error;
Thomas Gleixnere6309e72009-12-10 19:32:49 +00003233 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01003235 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
Greg Kroah-Hartmanc205ef42006-08-07 22:19:37 -07003236
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237 parent = get_device(dev->parent);
Kay Sieversca22e562011-12-14 14:29:38 -08003238 kobj = get_device_parent(dev, parent);
Tetsuo Handa84d0c272018-05-07 19:10:31 +09003239 if (IS_ERR(kobj)) {
3240 error = PTR_ERR(kobj);
3241 goto parent_error;
3242 }
Kay Sieversca22e562011-12-14 14:29:38 -08003243 if (kobj)
3244 dev->kobj.parent = kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245
Yinghai Lu0d358f22008-02-19 03:20:41 -08003246 /* use parent numa_node */
Zhen Lei56f2de82015-08-25 12:08:22 +08003247 if (parent && (dev_to_node(dev) == NUMA_NO_NODE))
Yinghai Lu0d358f22008-02-19 03:20:41 -08003248 set_dev_node(dev, dev_to_node(parent));
3249
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250 /* first, register with generic layer. */
Kay Sievers8a577ff2009-04-18 15:05:45 -07003251 /* we require the name to be set before, and pass NULL */
3252 error = kobject_add(&dev->kobj, dev->kobj.parent, NULL);
Ming Leicebf8fd2016-07-10 19:27:36 +08003253 if (error) {
3254 glue_dir = get_glue_dir(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003255 goto Error;
Ming Leicebf8fd2016-07-10 19:27:36 +08003256 }
Kay Sieversa7fd6702005-10-01 14:49:43 +02003257
Brian Walsh37022642006-08-14 22:43:19 -07003258 /* notify platform of device entry */
Heikki Krogerus07de0e82018-11-09 17:21:34 +03003259 error = device_platform_notify(dev, KOBJ_ADD);
3260 if (error)
3261 goto platform_error;
Brian Walsh37022642006-08-14 22:43:19 -07003262
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07003263 error = device_create_file(dev, &dev_attr_uevent);
Cornelia Hucka306eea2006-09-22 11:37:13 +02003264 if (error)
3265 goto attrError;
Kay Sieversa7fd6702005-10-01 14:49:43 +02003266
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07003267 error = device_add_class_symlinks(dev);
3268 if (error)
3269 goto SymlinkError;
Cornelia Huckdc0afa82007-07-09 11:39:18 -07003270 error = device_add_attrs(dev);
3271 if (error)
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07003272 goto AttrsError;
Cornelia Huckdc0afa82007-07-09 11:39:18 -07003273 error = bus_add_device(dev);
3274 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275 goto BusError;
Alan Stern3b98aea2008-08-07 13:06:12 -04003276 error = dpm_sysfs_add(dev);
Rafael J. Wysocki57eee3d2008-03-12 00:59:38 +01003277 if (error)
Alan Stern3b98aea2008-08-07 13:06:12 -04003278 goto DPMError;
3279 device_pm_add(dev);
Alan Sternec0676ee2008-12-05 14:10:31 -05003280
Sergey Klyaus0cd75042014-10-08 11:31:54 +04003281 if (MAJOR(dev->devt)) {
3282 error = device_create_file(dev, &dev_attr_dev);
3283 if (error)
3284 goto DevAttrError;
3285
3286 error = device_create_sys_dev_entry(dev);
3287 if (error)
3288 goto SysEntryError;
3289
3290 devtmpfs_create_node(dev);
3291 }
3292
Alan Sternec0676ee2008-12-05 14:10:31 -05003293 /* Notify clients of device addition. This call must come
majianpeng268863f2012-01-11 15:12:06 +00003294 * after dpm_sysfs_add() and before kobject_uevent().
Alan Sternec0676ee2008-12-05 14:10:31 -05003295 */
3296 if (dev->bus)
3297 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
3298 BUS_NOTIFY_ADD_DEVICE, dev);
3299
Cornelia Huck83b5fb4c2007-03-29 11:12:11 +02003300 kobject_uevent(&dev->kobj, KOBJ_ADD);
Saravana Kannan372a67c2019-09-04 14:11:20 -07003301
Saravana Kannane2ae9bc2019-09-04 14:11:21 -07003302 /*
3303 * Check if any of the other devices (consumers) have been waiting for
3304 * this device (supplier) to be added so that they can create a device
3305 * link to it.
3306 *
3307 * This needs to happen after device_pm_add() because device_link_add()
3308 * requires the supplier be registered before it's called.
3309 *
Saravana Kannan2cd38fd2020-05-19 20:48:21 -07003310 * But this also needs to happen before bus_probe_device() to make sure
Saravana Kannane2ae9bc2019-09-04 14:11:21 -07003311 * waiting consumers can link to it before the driver is bound to the
3312 * device and the driver sync_state callback is called for this device.
3313 */
Saravana Kannan2cd38fd2020-05-19 20:48:21 -07003314 if (dev->fwnode && !dev->fwnode->dev) {
3315 dev->fwnode->dev = dev;
Saravana Kannan5f5377e2020-05-14 22:34:58 -07003316 fw_devlink_link_device(dev);
Saravana Kannan03324502019-10-28 15:00:24 -07003317 }
Saravana Kannane2ae9bc2019-09-04 14:11:21 -07003318
Alan Stern2023c612009-07-30 15:27:18 -04003319 bus_probe_device(dev);
Saravana Kannand46f3e32021-04-01 21:03:41 -07003320
3321 /*
3322 * If all driver registration is done and a newly added device doesn't
3323 * match with any driver, don't block its consumers from probing in
3324 * case the consumer device is able to operate without this supplier.
3325 */
3326 if (dev->fwnode && fw_devlink_drv_reg_done && !dev->can_match)
3327 fw_devlink_unblock_consumers(dev);
3328
Linus Torvalds1da177e2005-04-16 15:20:36 -07003329 if (parent)
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08003330 klist_add_tail(&dev->p->knode_parent,
3331 &parent->p->klist_children);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003332
Greg Kroah-Hartman5d9fd162006-06-22 17:17:32 -07003333 if (dev->class) {
Kay Sieversca22e562011-12-14 14:29:38 -08003334 mutex_lock(&dev->class->p->mutex);
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02003335 /* tie the class to the device */
Wei Yang570d0202019-01-18 10:34:59 +08003336 klist_add_tail(&dev->p->knode_class,
Kay Sievers6b6e39a2010-11-15 23:13:18 +01003337 &dev->class->p->klist_devices);
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02003338
3339 /* notify any interfaces that the device is here */
Greg Kroah-Hartman184f1f72008-05-28 09:28:39 -07003340 list_for_each_entry(class_intf,
Kay Sieversca22e562011-12-14 14:29:38 -08003341 &dev->class->p->interfaces, node)
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02003342 if (class_intf->add_dev)
3343 class_intf->add_dev(dev, class_intf);
Kay Sieversca22e562011-12-14 14:29:38 -08003344 mutex_unlock(&dev->class->p->mutex);
Greg Kroah-Hartman5d9fd162006-06-22 17:17:32 -07003345 }
Greg Kroah-Hartmanc906a482008-05-30 10:45:12 -07003346done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003347 put_device(dev);
3348 return error;
Sergey Klyaus0cd75042014-10-08 11:31:54 +04003349 SysEntryError:
3350 if (MAJOR(dev->devt))
3351 device_remove_file(dev, &dev_attr_dev);
3352 DevAttrError:
3353 device_pm_remove(dev);
3354 dpm_sysfs_remove(dev);
Alan Stern3b98aea2008-08-07 13:06:12 -04003355 DPMError:
Rafael J. Wysocki57eee3d2008-03-12 00:59:38 +01003356 bus_remove_device(dev);
3357 BusError:
James Simmons82f0cf92007-02-21 17:44:51 +00003358 device_remove_attrs(dev);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07003359 AttrsError:
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07003360 device_remove_class_symlinks(dev);
3361 SymlinkError:
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07003362 device_remove_file(dev, &dev_attr_uevent);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07003363 attrError:
Heikki Krogerus07de0e82018-11-09 17:21:34 +03003364 device_platform_notify(dev, KOBJ_REMOVE);
3365platform_error:
Kay Sievers312c0042005-11-16 09:00:00 +01003366 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
Ming Leicebf8fd2016-07-10 19:27:36 +08003367 glue_dir = get_glue_dir(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003368 kobject_del(&dev->kobj);
3369 Error:
Ming Leicebf8fd2016-07-10 19:27:36 +08003370 cleanup_glue_dir(dev, glue_dir);
Tetsuo Handa84d0c272018-05-07 19:10:31 +09003371parent_error:
Markus Elfring5f0163a2015-02-05 11:48:26 +01003372 put_device(parent);
Kay Sievers5c8563d2009-05-28 14:24:07 -07003373name_error:
3374 kfree(dev->p);
3375 dev->p = NULL;
Greg Kroah-Hartmanc906a482008-05-30 10:45:12 -07003376 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003377}
David Graham White86df2682013-07-21 20:41:14 -04003378EXPORT_SYMBOL_GPL(device_add);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003379
Linus Torvalds1da177e2005-04-16 15:20:36 -07003380/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08003381 * device_register - register a device with the system.
3382 * @dev: pointer to the device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07003383 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08003384 * This happens in two clean steps - initialize the device
3385 * and add it to the system. The two steps can be called
3386 * separately, but this is the easiest and most common.
3387 * I.e. you should only call the two helpers separately if
3388 * have a clearly defined need to use and refcount the device
3389 * before it is added to the hierarchy.
Cornelia Huck57394112008-09-03 18:26:40 +02003390 *
Alan Sternb10d5ef2012-01-17 11:39:00 -05003391 * For more information, see the kerneldoc for device_initialize()
3392 * and device_add().
3393 *
Cornelia Huck57394112008-09-03 18:26:40 +02003394 * NOTE: _Never_ directly free @dev after calling this function, even
3395 * if it returned an error! Always use put_device() to give up the
3396 * reference initialized in this function instead.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003397 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003398int device_register(struct device *dev)
3399{
3400 device_initialize(dev);
3401 return device_add(dev);
3402}
David Graham White86df2682013-07-21 20:41:14 -04003403EXPORT_SYMBOL_GPL(device_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003404
Linus Torvalds1da177e2005-04-16 15:20:36 -07003405/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08003406 * get_device - increment reference count for device.
3407 * @dev: device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003408 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08003409 * This simply forwards the call to kobject_get(), though
3410 * we do take care to provide for the case that we get a NULL
3411 * pointer passed in.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003412 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08003413struct device *get_device(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003414{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +02003415 return dev ? kobj_to_dev(kobject_get(&dev->kobj)) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003416}
David Graham White86df2682013-07-21 20:41:14 -04003417EXPORT_SYMBOL_GPL(get_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003418
Linus Torvalds1da177e2005-04-16 15:20:36 -07003419/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08003420 * put_device - decrement reference count.
3421 * @dev: device in question.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003422 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08003423void put_device(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003424{
Kay Sieversedfaa7c2007-05-21 22:08:01 +02003425 /* might_sleep(); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003426 if (dev)
3427 kobject_put(&dev->kobj);
3428}
David Graham White86df2682013-07-21 20:41:14 -04003429EXPORT_SYMBOL_GPL(put_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003430
Dan Williams00289cd2019-07-17 18:07:53 -07003431bool kill_device(struct device *dev)
3432{
3433 /*
3434 * Require the device lock and set the "dead" flag to guarantee that
3435 * the update behavior is consistent with the other bitfields near
3436 * it and that we cannot have an asynchronous probe routine trying
3437 * to run while we are tearing out the bus/class/sysfs from
3438 * underneath the device.
3439 */
3440 lockdep_assert_held(&dev->mutex);
3441
3442 if (dev->p->dead)
3443 return false;
3444 dev->p->dead = true;
3445 return true;
3446}
3447EXPORT_SYMBOL_GPL(kill_device);
3448
Linus Torvalds1da177e2005-04-16 15:20:36 -07003449/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08003450 * device_del - delete device from system.
3451 * @dev: device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003452 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08003453 * This is the first part of the device unregistration
3454 * sequence. This removes the device from the lists we control
3455 * from here, has it removed from the other driver model
3456 * subsystems it was added to in device_add(), and removes it
3457 * from the kobject hierarchy.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003458 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08003459 * NOTE: this should be called manually _iff_ device_add() was
3460 * also called manually.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003461 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08003462void device_del(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003463{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08003464 struct device *parent = dev->parent;
Ming Leicebf8fd2016-07-10 19:27:36 +08003465 struct kobject *glue_dir = NULL;
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02003466 struct class_interface *class_intf;
Oliver Neukumb8530012020-09-16 21:15:44 +02003467 unsigned int noio_flag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003468
Alexander Duyck3451a492019-01-22 10:39:10 -08003469 device_lock(dev);
Dan Williams00289cd2019-07-17 18:07:53 -07003470 kill_device(dev);
Alexander Duyck3451a492019-01-22 10:39:10 -08003471 device_unlock(dev);
3472
Saravana Kannan372a67c2019-09-04 14:11:20 -07003473 if (dev->fwnode && dev->fwnode->dev == dev)
3474 dev->fwnode->dev = NULL;
3475
Alan Sternec0676ee2008-12-05 14:10:31 -05003476 /* Notify clients of device removal. This call must come
3477 * before dpm_sysfs_remove().
3478 */
Oliver Neukumb8530012020-09-16 21:15:44 +02003479 noio_flag = memalloc_noio_save();
Alan Sternec0676ee2008-12-05 14:10:31 -05003480 if (dev->bus)
3481 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
3482 BUS_NOTIFY_DEL_DEVICE, dev);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01003483
Alan Stern3b98aea2008-08-07 13:06:12 -04003484 dpm_sysfs_remove(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003485 if (parent)
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08003486 klist_del(&dev->p->knode_parent);
Dan Williamse105b8b2008-04-21 10:51:07 -07003487 if (MAJOR(dev->devt)) {
Kay Sievers2b2af542009-04-30 15:23:42 +02003488 devtmpfs_delete_node(dev);
Dan Williamse105b8b2008-04-21 10:51:07 -07003489 device_remove_sys_dev_entry(dev);
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07003490 device_remove_file(dev, &dev_attr_dev);
Dan Williamse105b8b2008-04-21 10:51:07 -07003491 }
Kay Sieversb9d9c822006-06-15 15:31:56 +02003492 if (dev->class) {
Kay Sieversda231fd2007-11-21 17:29:15 +01003493 device_remove_class_symlinks(dev);
Kay Sievers99ef3ef2006-09-14 11:23:28 +02003494
Kay Sieversca22e562011-12-14 14:29:38 -08003495 mutex_lock(&dev->class->p->mutex);
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02003496 /* notify any interfaces that the device is now gone */
Greg Kroah-Hartman184f1f72008-05-28 09:28:39 -07003497 list_for_each_entry(class_intf,
Kay Sieversca22e562011-12-14 14:29:38 -08003498 &dev->class->p->interfaces, node)
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02003499 if (class_intf->remove_dev)
3500 class_intf->remove_dev(dev, class_intf);
3501 /* remove the device from the class list */
Wei Yang570d0202019-01-18 10:34:59 +08003502 klist_del(&dev->p->knode_class);
Kay Sieversca22e562011-12-14 14:29:38 -08003503 mutex_unlock(&dev->class->p->mutex);
Kay Sieversb9d9c822006-06-15 15:31:56 +02003504 }
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07003505 device_remove_file(dev, &dev_attr_uevent);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07003506 device_remove_attrs(dev);
Benjamin Herrenschmidt28953532006-11-08 19:46:14 -08003507 bus_remove_device(dev);
LongX Zhang4b6d1f122012-10-25 00:21:28 +02003508 device_pm_remove(dev);
Grant Likelyd1c34142012-03-05 08:47:41 -07003509 driver_deferred_probe_del(dev);
Heikki Krogerus07de0e82018-11-09 17:21:34 +03003510 device_platform_notify(dev, KOBJ_REMOVE);
Lukas Wunner478573c2016-07-28 02:25:41 +02003511 device_remove_properties(dev);
Jeffy Chen2ec16152017-10-20 20:01:01 +08003512 device_links_purge(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003513
Joerg Roedel599bad32014-09-30 13:02:02 +02003514 if (dev->bus)
3515 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
3516 BUS_NOTIFY_REMOVED_DEVICE, dev);
Kay Sievers312c0042005-11-16 09:00:00 +01003517 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
Ming Leicebf8fd2016-07-10 19:27:36 +08003518 glue_dir = get_glue_dir(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003519 kobject_del(&dev->kobj);
Ming Leicebf8fd2016-07-10 19:27:36 +08003520 cleanup_glue_dir(dev, glue_dir);
Oliver Neukumb8530012020-09-16 21:15:44 +02003521 memalloc_noio_restore(noio_flag);
Kay Sieversda231fd2007-11-21 17:29:15 +01003522 put_device(parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003523}
David Graham White86df2682013-07-21 20:41:14 -04003524EXPORT_SYMBOL_GPL(device_del);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003525
3526/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08003527 * device_unregister - unregister device from system.
3528 * @dev: device going away.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003529 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08003530 * We do this in two parts, like we do device_register(). First,
3531 * we remove it from all the subsystems with device_del(), then
3532 * we decrement the reference count via put_device(). If that
3533 * is the final reference count, the device will be cleaned up
3534 * via device_release() above. Otherwise, the structure will
3535 * stick around until the final reference to the device is dropped.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003536 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08003537void device_unregister(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003538{
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01003539 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003540 device_del(dev);
3541 put_device(dev);
3542}
David Graham White86df2682013-07-21 20:41:14 -04003543EXPORT_SYMBOL_GPL(device_unregister);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003544
Andy Shevchenko3d060ae2015-07-27 18:04:00 +03003545static struct device *prev_device(struct klist_iter *i)
3546{
3547 struct klist_node *n = klist_prev(i);
3548 struct device *dev = NULL;
3549 struct device_private *p;
3550
3551 if (n) {
3552 p = to_device_private_parent(n);
3553 dev = p->device;
3554 }
3555 return dev;
3556}
3557
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08003558static struct device *next_device(struct klist_iter *i)
mochel@digitalimplant.org36239572005-03-24 19:08:30 -08003559{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08003560 struct klist_node *n = klist_next(i);
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08003561 struct device *dev = NULL;
3562 struct device_private *p;
3563
3564 if (n) {
3565 p = to_device_private_parent(n);
3566 dev = p->device;
3567 }
3568 return dev;
mochel@digitalimplant.org36239572005-03-24 19:08:30 -08003569}
3570
Linus Torvalds1da177e2005-04-16 15:20:36 -07003571/**
Kay Sieverse454cea2009-09-18 23:01:12 +02003572 * device_get_devnode - path of device node file
Kay Sievers6fcf53a2009-04-30 15:23:42 +02003573 * @dev: device
Kay Sieverse454cea2009-09-18 23:01:12 +02003574 * @mode: returned file access mode
Kay Sievers3c2670e2013-04-06 09:56:00 -07003575 * @uid: returned file owner
3576 * @gid: returned file group
Kay Sievers6fcf53a2009-04-30 15:23:42 +02003577 * @tmp: possibly allocated string
3578 *
3579 * Return the relative path of a possible device node.
3580 * Non-default names may need to allocate a memory to compose
3581 * a name. This memory is returned in tmp and needs to be
3582 * freed by the caller.
3583 */
Kay Sieverse454cea2009-09-18 23:01:12 +02003584const char *device_get_devnode(struct device *dev,
Greg Kroah-Hartman4e4098a2013-04-11 11:43:29 -07003585 umode_t *mode, kuid_t *uid, kgid_t *gid,
Kay Sievers3c2670e2013-04-06 09:56:00 -07003586 const char **tmp)
Kay Sievers6fcf53a2009-04-30 15:23:42 +02003587{
3588 char *s;
3589
3590 *tmp = NULL;
3591
3592 /* the device type may provide a specific name */
Kay Sieverse454cea2009-09-18 23:01:12 +02003593 if (dev->type && dev->type->devnode)
Kay Sievers3c2670e2013-04-06 09:56:00 -07003594 *tmp = dev->type->devnode(dev, mode, uid, gid);
Kay Sievers6fcf53a2009-04-30 15:23:42 +02003595 if (*tmp)
3596 return *tmp;
3597
3598 /* the class may provide a specific name */
Kay Sieverse454cea2009-09-18 23:01:12 +02003599 if (dev->class && dev->class->devnode)
3600 *tmp = dev->class->devnode(dev, mode);
Kay Sievers6fcf53a2009-04-30 15:23:42 +02003601 if (*tmp)
3602 return *tmp;
3603
3604 /* return name without allocation, tmp == NULL */
3605 if (strchr(dev_name(dev), '!') == NULL)
3606 return dev_name(dev);
3607
3608 /* replace '!' in the name with '/' */
Rasmus Villemoesa29fd612015-06-25 15:02:33 -07003609 s = kstrdup(dev_name(dev), GFP_KERNEL);
3610 if (!s)
Kay Sievers6fcf53a2009-04-30 15:23:42 +02003611 return NULL;
Rasmus Villemoesa29fd612015-06-25 15:02:33 -07003612 strreplace(s, '!', '/');
3613 return *tmp = s;
Kay Sievers6fcf53a2009-04-30 15:23:42 +02003614}
3615
3616/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08003617 * device_for_each_child - device child iterator.
3618 * @parent: parent struct device.
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08003619 * @fn: function to be called for each device.
Robert P. J. Dayf8878dc2013-06-01 20:17:34 -04003620 * @data: data for the callback.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003621 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08003622 * Iterate over @parent's child devices, and call @fn for each,
3623 * passing it @data.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003624 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08003625 * We check the return of @fn each time. If it returns anything
3626 * other than 0, we break out and return that value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003627 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08003628int device_for_each_child(struct device *parent, void *data,
3629 int (*fn)(struct device *dev, void *data))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003630{
mochel@digitalimplant.org36239572005-03-24 19:08:30 -08003631 struct klist_iter i;
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08003632 struct device *child;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003633 int error = 0;
3634
Greg Kroah-Hartman014c90db2009-04-15 16:00:12 -07003635 if (!parent->p)
3636 return 0;
3637
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08003638 klist_iter_init(&parent->p->klist_children, &i);
Gimcuan Hui93ead7c2017-11-11 05:52:54 +00003639 while (!error && (child = next_device(&i)))
mochel@digitalimplant.org36239572005-03-24 19:08:30 -08003640 error = fn(child, data);
3641 klist_iter_exit(&i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003642 return error;
3643}
David Graham White86df2682013-07-21 20:41:14 -04003644EXPORT_SYMBOL_GPL(device_for_each_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003645
Cornelia Huck5ab69982006-11-16 15:42:07 +01003646/**
Andy Shevchenko3d060ae2015-07-27 18:04:00 +03003647 * device_for_each_child_reverse - device child iterator in reversed order.
3648 * @parent: parent struct device.
3649 * @fn: function to be called for each device.
3650 * @data: data for the callback.
3651 *
3652 * Iterate over @parent's child devices, and call @fn for each,
3653 * passing it @data.
3654 *
3655 * We check the return of @fn each time. If it returns anything
3656 * other than 0, we break out and return that value.
3657 */
3658int device_for_each_child_reverse(struct device *parent, void *data,
3659 int (*fn)(struct device *dev, void *data))
3660{
3661 struct klist_iter i;
3662 struct device *child;
3663 int error = 0;
3664
3665 if (!parent->p)
3666 return 0;
3667
3668 klist_iter_init(&parent->p->klist_children, &i);
3669 while ((child = prev_device(&i)) && !error)
3670 error = fn(child, data);
3671 klist_iter_exit(&i);
3672 return error;
3673}
3674EXPORT_SYMBOL_GPL(device_for_each_child_reverse);
3675
3676/**
Cornelia Huck5ab69982006-11-16 15:42:07 +01003677 * device_find_child - device iterator for locating a particular device.
3678 * @parent: parent struct device
Cornelia Huck5ab69982006-11-16 15:42:07 +01003679 * @match: Callback function to check device
Robert P. J. Dayf8878dc2013-06-01 20:17:34 -04003680 * @data: Data to pass to match function
Cornelia Huck5ab69982006-11-16 15:42:07 +01003681 *
3682 * This is similar to the device_for_each_child() function above, but it
3683 * returns a reference to a device that is 'found' for later use, as
3684 * determined by the @match callback.
3685 *
3686 * The callback should return 0 if the device doesn't match and non-zero
3687 * if it does. If the callback returns non-zero and a reference to the
3688 * current device can be obtained, this function will return to the caller
3689 * and not iterate over any more devices.
Federico Vagaa4e24002013-04-15 11:18:11 +02003690 *
3691 * NOTE: you will need to drop the reference with put_device() after use.
Cornelia Huck5ab69982006-11-16 15:42:07 +01003692 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08003693struct device *device_find_child(struct device *parent, void *data,
3694 int (*match)(struct device *dev, void *data))
Cornelia Huck5ab69982006-11-16 15:42:07 +01003695{
3696 struct klist_iter i;
3697 struct device *child;
3698
3699 if (!parent)
3700 return NULL;
3701
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08003702 klist_iter_init(&parent->p->klist_children, &i);
Cornelia Huck5ab69982006-11-16 15:42:07 +01003703 while ((child = next_device(&i)))
3704 if (match(child, data) && get_device(child))
3705 break;
3706 klist_iter_exit(&i);
3707 return child;
3708}
David Graham White86df2682013-07-21 20:41:14 -04003709EXPORT_SYMBOL_GPL(device_find_child);
Cornelia Huck5ab69982006-11-16 15:42:07 +01003710
Heikki Krogerusdad9bb02019-05-31 17:15:37 +03003711/**
3712 * device_find_child_by_name - device iterator for locating a child device.
3713 * @parent: parent struct device
3714 * @name: name of the child device
3715 *
3716 * This is similar to the device_find_child() function above, but it
3717 * returns a reference to a device that has the name @name.
3718 *
3719 * NOTE: you will need to drop the reference with put_device() after use.
3720 */
3721struct device *device_find_child_by_name(struct device *parent,
3722 const char *name)
3723{
3724 struct klist_iter i;
3725 struct device *child;
3726
3727 if (!parent)
3728 return NULL;
3729
3730 klist_iter_init(&parent->p->klist_children, &i);
3731 while ((child = next_device(&i)))
Dan Williamsc77f5202020-10-13 16:50:18 -07003732 if (sysfs_streq(dev_name(child), name) && get_device(child))
Heikki Krogerusdad9bb02019-05-31 17:15:37 +03003733 break;
3734 klist_iter_exit(&i);
3735 return child;
3736}
3737EXPORT_SYMBOL_GPL(device_find_child_by_name);
3738
Linus Torvalds1da177e2005-04-16 15:20:36 -07003739int __init devices_init(void)
3740{
Greg Kroah-Hartman881c6cfd2007-11-01 09:29:06 -06003741 devices_kset = kset_create_and_add("devices", &device_uevent_ops, NULL);
3742 if (!devices_kset)
3743 return -ENOMEM;
Dan Williamse105b8b2008-04-21 10:51:07 -07003744 dev_kobj = kobject_create_and_add("dev", NULL);
3745 if (!dev_kobj)
3746 goto dev_kobj_err;
3747 sysfs_dev_block_kobj = kobject_create_and_add("block", dev_kobj);
3748 if (!sysfs_dev_block_kobj)
3749 goto block_kobj_err;
3750 sysfs_dev_char_kobj = kobject_create_and_add("char", dev_kobj);
3751 if (!sysfs_dev_char_kobj)
3752 goto char_kobj_err;
3753
Greg Kroah-Hartman881c6cfd2007-11-01 09:29:06 -06003754 return 0;
Dan Williamse105b8b2008-04-21 10:51:07 -07003755
3756 char_kobj_err:
3757 kobject_put(sysfs_dev_block_kobj);
3758 block_kobj_err:
3759 kobject_put(dev_kobj);
3760 dev_kobj_err:
3761 kset_unregister(devices_kset);
3762 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003763}
3764
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02003765static int device_check_offline(struct device *dev, void *not_used)
3766{
3767 int ret;
3768
3769 ret = device_for_each_child(dev, NULL, device_check_offline);
3770 if (ret)
3771 return ret;
3772
3773 return device_supports_offline(dev) && !dev->offline ? -EBUSY : 0;
3774}
3775
3776/**
3777 * device_offline - Prepare the device for hot-removal.
3778 * @dev: Device to be put offline.
3779 *
3780 * Execute the device bus type's .offline() callback, if present, to prepare
3781 * the device for a subsequent hot-removal. If that succeeds, the device must
3782 * not be used until either it is removed or its bus type's .online() callback
3783 * is executed.
3784 *
3785 * Call under device_hotplug_lock.
3786 */
3787int device_offline(struct device *dev)
3788{
3789 int ret;
3790
3791 if (dev->offline_disabled)
3792 return -EPERM;
3793
3794 ret = device_for_each_child(dev, NULL, device_check_offline);
3795 if (ret)
3796 return ret;
3797
3798 device_lock(dev);
3799 if (device_supports_offline(dev)) {
3800 if (dev->offline) {
3801 ret = 1;
3802 } else {
3803 ret = dev->bus->offline(dev);
3804 if (!ret) {
3805 kobject_uevent(&dev->kobj, KOBJ_OFFLINE);
3806 dev->offline = true;
3807 }
3808 }
3809 }
3810 device_unlock(dev);
3811
3812 return ret;
3813}
3814
3815/**
3816 * device_online - Put the device back online after successful device_offline().
3817 * @dev: Device to be put back online.
3818 *
3819 * If device_offline() has been successfully executed for @dev, but the device
3820 * has not been removed subsequently, execute its bus type's .online() callback
3821 * to indicate that the device can be used again.
3822 *
3823 * Call under device_hotplug_lock.
3824 */
3825int device_online(struct device *dev)
3826{
3827 int ret = 0;
3828
3829 device_lock(dev);
3830 if (device_supports_offline(dev)) {
3831 if (dev->offline) {
3832 ret = dev->bus->online(dev);
3833 if (!ret) {
3834 kobject_uevent(&dev->kobj, KOBJ_ONLINE);
3835 dev->offline = false;
3836 }
3837 } else {
3838 ret = 1;
3839 }
3840 }
3841 device_unlock(dev);
3842
3843 return ret;
3844}
3845
Karthigan Srinivasan7f100d12011-04-18 16:16:52 -05003846struct root_device {
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00003847 struct device dev;
3848 struct module *owner;
3849};
3850
Josh Triplett93058422012-11-18 21:27:55 -08003851static inline struct root_device *to_root_device(struct device *d)
Ferenc Wagner481e2072011-01-07 15:17:47 +01003852{
3853 return container_of(d, struct root_device, dev);
3854}
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00003855
3856static void root_device_release(struct device *dev)
3857{
3858 kfree(to_root_device(dev));
3859}
3860
3861/**
3862 * __root_device_register - allocate and register a root device
3863 * @name: root device name
3864 * @owner: owner module of the root device, usually THIS_MODULE
3865 *
3866 * This function allocates a root device and registers it
3867 * using device_register(). In order to free the returned
3868 * device, use root_device_unregister().
3869 *
3870 * Root devices are dummy devices which allow other devices
3871 * to be grouped under /sys/devices. Use this function to
3872 * allocate a root device and then use it as the parent of
3873 * any device which should appear under /sys/devices/{name}
3874 *
3875 * The /sys/devices/{name} directory will also contain a
3876 * 'module' symlink which points to the @owner directory
3877 * in sysfs.
3878 *
Jani Nikulaf0eae0e2010-03-11 18:11:45 +02003879 * Returns &struct device pointer on success, or ERR_PTR() on error.
3880 *
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00003881 * Note: You probably want to use root_device_register().
3882 */
3883struct device *__root_device_register(const char *name, struct module *owner)
3884{
3885 struct root_device *root;
3886 int err = -ENOMEM;
3887
3888 root = kzalloc(sizeof(struct root_device), GFP_KERNEL);
3889 if (!root)
3890 return ERR_PTR(err);
3891
Greg Kroah-Hartmanacc0e902009-06-02 15:39:55 -07003892 err = dev_set_name(&root->dev, "%s", name);
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00003893 if (err) {
3894 kfree(root);
3895 return ERR_PTR(err);
3896 }
3897
3898 root->dev.release = root_device_release;
3899
3900 err = device_register(&root->dev);
3901 if (err) {
3902 put_device(&root->dev);
3903 return ERR_PTR(err);
3904 }
3905
Christoph Egger1d9e8822010-05-17 16:57:58 +02003906#ifdef CONFIG_MODULES /* gotta find a "cleaner" way to do this */
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00003907 if (owner) {
3908 struct module_kobject *mk = &owner->mkobj;
3909
3910 err = sysfs_create_link(&root->dev.kobj, &mk->kobj, "module");
3911 if (err) {
3912 device_unregister(&root->dev);
3913 return ERR_PTR(err);
3914 }
3915 root->owner = owner;
3916 }
3917#endif
3918
3919 return &root->dev;
3920}
3921EXPORT_SYMBOL_GPL(__root_device_register);
3922
3923/**
3924 * root_device_unregister - unregister and free a root device
Randy Dunlap7cbcf222009-01-20 16:29:13 -08003925 * @dev: device going away
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00003926 *
3927 * This function unregisters and cleans up a device that was created by
3928 * root_device_register().
3929 */
3930void root_device_unregister(struct device *dev)
3931{
3932 struct root_device *root = to_root_device(dev);
3933
3934 if (root->owner)
3935 sysfs_remove_link(&root->dev.kobj, "module");
3936
3937 device_unregister(dev);
3938}
3939EXPORT_SYMBOL_GPL(root_device_unregister);
3940
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07003941
3942static void device_create_release(struct device *dev)
3943{
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01003944 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07003945 kfree(dev);
3946}
3947
Mathieu Malaterre6a8b55d2018-05-05 21:57:41 +02003948static __printf(6, 0) struct device *
Guenter Roeck39ef3112013-07-14 16:05:57 -07003949device_create_groups_vargs(struct class *class, struct device *parent,
3950 dev_t devt, void *drvdata,
3951 const struct attribute_group **groups,
3952 const char *fmt, va_list args)
3953{
3954 struct device *dev = NULL;
3955 int retval = -ENODEV;
3956
3957 if (class == NULL || IS_ERR(class))
3958 goto error;
3959
3960 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
3961 if (!dev) {
3962 retval = -ENOMEM;
3963 goto error;
3964 }
3965
David Herrmannbbc780f2013-11-21 20:15:48 +01003966 device_initialize(dev);
Guenter Roeck39ef3112013-07-14 16:05:57 -07003967 dev->devt = devt;
3968 dev->class = class;
3969 dev->parent = parent;
3970 dev->groups = groups;
3971 dev->release = device_create_release;
3972 dev_set_drvdata(dev, drvdata);
3973
3974 retval = kobject_set_name_vargs(&dev->kobj, fmt, args);
3975 if (retval)
3976 goto error;
3977
David Herrmannbbc780f2013-11-21 20:15:48 +01003978 retval = device_add(dev);
Guenter Roeck39ef3112013-07-14 16:05:57 -07003979 if (retval)
3980 goto error;
3981
3982 return dev;
3983
3984error:
3985 put_device(dev);
3986 return ERR_PTR(retval);
3987}
3988
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07003989/**
Greg Kroah-Hartman4e106732008-07-21 20:03:34 -07003990 * device_create - creates a device and registers it with sysfs
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07003991 * @class: pointer to the struct class that this device should be registered to
3992 * @parent: pointer to the parent struct device of this new device, if any
3993 * @devt: the dev_t for the char device to be added
3994 * @drvdata: the data to be added to the device for callbacks
3995 * @fmt: string for the device's name
3996 *
3997 * This function can be used by char device classes. A struct device
3998 * will be created in sysfs, registered to the specified class.
3999 *
4000 * A "dev" file will be created, showing the dev_t for the device, if
4001 * the dev_t is not 0,0.
4002 * If a pointer to a parent struct device is passed in, the newly created
4003 * struct device will be a child of that device in sysfs.
4004 * The pointer to the struct device will be returned from the call.
4005 * Any further sysfs files that might be required can be created using this
4006 * pointer.
4007 *
Jani Nikulaf0eae0e2010-03-11 18:11:45 +02004008 * Returns &struct device pointer on success, or ERR_PTR() on error.
4009 *
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07004010 * Note: the struct class passed to this function must have previously
4011 * been created with a call to class_create().
4012 */
Greg Kroah-Hartman4e106732008-07-21 20:03:34 -07004013struct device *device_create(struct class *class, struct device *parent,
4014 dev_t devt, void *drvdata, const char *fmt, ...)
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07004015{
4016 va_list vargs;
4017 struct device *dev;
4018
4019 va_start(vargs, fmt);
Christoph Hellwig4c747462020-05-04 14:47:57 +02004020 dev = device_create_groups_vargs(class, parent, devt, drvdata, NULL,
4021 fmt, vargs);
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07004022 va_end(vargs);
4023 return dev;
4024}
Greg Kroah-Hartman4e106732008-07-21 20:03:34 -07004025EXPORT_SYMBOL_GPL(device_create);
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07004026
Guenter Roeck39ef3112013-07-14 16:05:57 -07004027/**
4028 * device_create_with_groups - creates a device and registers it with sysfs
4029 * @class: pointer to the struct class that this device should be registered to
4030 * @parent: pointer to the parent struct device of this new device, if any
4031 * @devt: the dev_t for the char device to be added
4032 * @drvdata: the data to be added to the device for callbacks
4033 * @groups: NULL-terminated list of attribute groups to be created
4034 * @fmt: string for the device's name
4035 *
4036 * This function can be used by char device classes. A struct device
4037 * will be created in sysfs, registered to the specified class.
4038 * Additional attributes specified in the groups parameter will also
4039 * be created automatically.
4040 *
4041 * A "dev" file will be created, showing the dev_t for the device, if
4042 * the dev_t is not 0,0.
4043 * If a pointer to a parent struct device is passed in, the newly created
4044 * struct device will be a child of that device in sysfs.
4045 * The pointer to the struct device will be returned from the call.
4046 * Any further sysfs files that might be required can be created using this
4047 * pointer.
4048 *
4049 * Returns &struct device pointer on success, or ERR_PTR() on error.
4050 *
4051 * Note: the struct class passed to this function must have previously
4052 * been created with a call to class_create().
4053 */
4054struct device *device_create_with_groups(struct class *class,
4055 struct device *parent, dev_t devt,
4056 void *drvdata,
4057 const struct attribute_group **groups,
4058 const char *fmt, ...)
4059{
4060 va_list vargs;
4061 struct device *dev;
4062
4063 va_start(vargs, fmt);
4064 dev = device_create_groups_vargs(class, parent, devt, drvdata, groups,
4065 fmt, vargs);
4066 va_end(vargs);
4067 return dev;
4068}
4069EXPORT_SYMBOL_GPL(device_create_with_groups);
4070
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01004071/**
4072 * device_destroy - removes a device that was created with device_create()
4073 * @class: pointer to the struct class that this device was registered with
4074 * @devt: the dev_t of the device that was previously registered
4075 *
4076 * This call unregisters and cleans up a device that was created with a
4077 * call to device_create().
4078 */
4079void device_destroy(struct class *class, dev_t devt)
4080{
4081 struct device *dev;
4082
Suzuki K Poulose4495dfd2019-07-23 23:18:35 +01004083 dev = class_find_device_by_devt(class, devt);
Dave Youngcd354492008-01-28 16:56:11 +08004084 if (dev) {
4085 put_device(dev);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07004086 device_unregister(dev);
Dave Youngcd354492008-01-28 16:56:11 +08004087 }
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07004088}
4089EXPORT_SYMBOL_GPL(device_destroy);
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07004090
4091/**
4092 * device_rename - renames a device
4093 * @dev: the pointer to the struct device to be renamed
4094 * @new_name: the new name of the device
Eric W. Biederman030c1d22008-05-08 14:41:00 -07004095 *
4096 * It is the responsibility of the caller to provide mutual
4097 * exclusion between two different calls of device_rename
4098 * on the same device to ensure that new_name is valid and
4099 * won't conflict with other devices.
Michael Ellermanc6c0ac62010-11-25 09:44:07 +11004100 *
Timur Tabia5462512010-12-13 14:08:52 -06004101 * Note: Don't call this function. Currently, the networking layer calls this
4102 * function, but that will change. The following text from Kay Sievers offers
4103 * some insight:
4104 *
4105 * Renaming devices is racy at many levels, symlinks and other stuff are not
4106 * replaced atomically, and you get a "move" uevent, but it's not easy to
4107 * connect the event to the old and new device. Device nodes are not renamed at
4108 * all, there isn't even support for that in the kernel now.
4109 *
4110 * In the meantime, during renaming, your target name might be taken by another
4111 * driver, creating conflicts. Or the old name is taken directly after you
4112 * renamed it -- then you get events for the same DEVPATH, before you even see
4113 * the "move" event. It's just a mess, and nothing new should ever rely on
4114 * kernel device renaming. Besides that, it's not even implemented now for
4115 * other things than (driver-core wise very simple) network devices.
4116 *
4117 * We are currently about to change network renaming in udev to completely
4118 * disallow renaming of devices in the same namespace as the kernel uses,
4119 * because we can't solve the problems properly, that arise with swapping names
4120 * of multiple interfaces without races. Means, renaming of eth[0-9]* will only
4121 * be allowed to some other name than eth[0-9]*, for the aforementioned
4122 * reasons.
4123 *
4124 * Make up a "real" name in the driver before you register anything, or add
4125 * some other attributes for userspace to find the device, or use udev to add
4126 * symlinks -- but never rename kernel devices later, it's a complete mess. We
4127 * don't even want to get into that and try to implement the missing pieces in
4128 * the core. We really have other pieces to fix in the driver core mess. :)
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07004129 */
Johannes Berg6937e8f2010-08-05 17:38:18 +02004130int device_rename(struct device *dev, const char *new_name)
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07004131{
Tejun Heo4b30ee52013-09-11 22:29:06 -04004132 struct kobject *kobj = &dev->kobj;
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07004133 char *old_device_name = NULL;
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07004134 int error;
4135
4136 dev = get_device(dev);
4137 if (!dev)
4138 return -EINVAL;
4139
ethan.zhao69df7532013-10-13 22:12:35 +08004140 dev_dbg(dev, "renaming to %s\n", new_name);
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07004141
Kay Sievers1fa5ae82009-01-25 15:17:37 +01004142 old_device_name = kstrdup(dev_name(dev), GFP_KERNEL);
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07004143 if (!old_device_name) {
4144 error = -ENOMEM;
4145 goto out;
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07004146 }
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07004147
Eric W. Biedermanf349cf32010-03-30 11:31:29 -07004148 if (dev->class) {
Tejun Heo4b30ee52013-09-11 22:29:06 -04004149 error = sysfs_rename_link_ns(&dev->class->p->subsys.kobj,
4150 kobj, old_device_name,
4151 new_name, kobject_namespace(kobj));
Eric W. Biedermanf349cf32010-03-30 11:31:29 -07004152 if (error)
4153 goto out;
4154 }
Kay Sievers39aba962010-09-04 22:33:14 -07004155
Tejun Heo4b30ee52013-09-11 22:29:06 -04004156 error = kobject_rename(kobj, new_name);
Kay Sievers1fa5ae82009-01-25 15:17:37 +01004157 if (error)
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07004158 goto out;
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07004159
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07004160out:
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07004161 put_device(dev);
4162
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07004163 kfree(old_device_name);
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07004164
4165 return error;
4166}
Johannes Berga2807db2007-02-28 12:38:31 +01004167EXPORT_SYMBOL_GPL(device_rename);
Cornelia Huck8a824722006-11-20 17:07:51 +01004168
4169static int device_move_class_links(struct device *dev,
4170 struct device *old_parent,
4171 struct device *new_parent)
4172{
Greg Kroah-Hartmanf7f34612007-03-06 12:55:53 -08004173 int error = 0;
Cornelia Huck8a824722006-11-20 17:07:51 +01004174
Greg Kroah-Hartmanf7f34612007-03-06 12:55:53 -08004175 if (old_parent)
4176 sysfs_remove_link(&dev->kobj, "device");
4177 if (new_parent)
4178 error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
4179 "device");
4180 return error;
Cornelia Huck8a824722006-11-20 17:07:51 +01004181}
4182
4183/**
4184 * device_move - moves a device to a new parent
4185 * @dev: the pointer to the struct device to be moved
Wolfram Sang13509862018-05-06 13:23:47 +02004186 * @new_parent: the new parent of the device (can be NULL)
Cornelia Huckffa6a702009-03-04 12:44:00 +01004187 * @dpm_order: how to reorder the dpm_list
Cornelia Huck8a824722006-11-20 17:07:51 +01004188 */
Cornelia Huckffa6a702009-03-04 12:44:00 +01004189int device_move(struct device *dev, struct device *new_parent,
4190 enum dpm_order dpm_order)
Cornelia Huck8a824722006-11-20 17:07:51 +01004191{
4192 int error;
4193 struct device *old_parent;
Cornelia Huckc744aeae2007-01-08 20:16:44 +01004194 struct kobject *new_parent_kobj;
Cornelia Huck8a824722006-11-20 17:07:51 +01004195
4196 dev = get_device(dev);
4197 if (!dev)
4198 return -EINVAL;
4199
Cornelia Huckffa6a702009-03-04 12:44:00 +01004200 device_pm_lock();
Cornelia Huck8a824722006-11-20 17:07:51 +01004201 new_parent = get_device(new_parent);
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08004202 new_parent_kobj = get_device_parent(dev, new_parent);
Tetsuo Handa84d0c272018-05-07 19:10:31 +09004203 if (IS_ERR(new_parent_kobj)) {
4204 error = PTR_ERR(new_parent_kobj);
4205 put_device(new_parent);
4206 goto out;
4207 }
Cornelia Huck63b69712008-01-21 16:09:44 +01004208
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01004209 pr_debug("device: '%s': %s: moving to '%s'\n", dev_name(dev),
4210 __func__, new_parent ? dev_name(new_parent) : "<NULL>");
Cornelia Huckc744aeae2007-01-08 20:16:44 +01004211 error = kobject_move(&dev->kobj, new_parent_kobj);
Cornelia Huck8a824722006-11-20 17:07:51 +01004212 if (error) {
Cornelia Huck63b69712008-01-21 16:09:44 +01004213 cleanup_glue_dir(dev, new_parent_kobj);
Cornelia Huck8a824722006-11-20 17:07:51 +01004214 put_device(new_parent);
4215 goto out;
4216 }
4217 old_parent = dev->parent;
4218 dev->parent = new_parent;
4219 if (old_parent)
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08004220 klist_remove(&dev->p->knode_parent);
Yinghai Lu0d358f22008-02-19 03:20:41 -08004221 if (new_parent) {
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08004222 klist_add_tail(&dev->p->knode_parent,
4223 &new_parent->p->klist_children);
Yinghai Lu0d358f22008-02-19 03:20:41 -08004224 set_dev_node(dev, dev_to_node(new_parent));
4225 }
4226
Rabin Vincentbdd40342012-04-23 09:16:36 +02004227 if (dev->class) {
4228 error = device_move_class_links(dev, old_parent, new_parent);
4229 if (error) {
4230 /* We ignore errors on cleanup since we're hosed anyway... */
4231 device_move_class_links(dev, new_parent, old_parent);
4232 if (!kobject_move(&dev->kobj, &old_parent->kobj)) {
4233 if (new_parent)
4234 klist_remove(&dev->p->knode_parent);
4235 dev->parent = old_parent;
4236 if (old_parent) {
4237 klist_add_tail(&dev->p->knode_parent,
4238 &old_parent->p->klist_children);
4239 set_dev_node(dev, dev_to_node(old_parent));
4240 }
Yinghai Lu0d358f22008-02-19 03:20:41 -08004241 }
Rabin Vincentbdd40342012-04-23 09:16:36 +02004242 cleanup_glue_dir(dev, new_parent_kobj);
4243 put_device(new_parent);
4244 goto out;
Cornelia Huck8a824722006-11-20 17:07:51 +01004245 }
Cornelia Huck8a824722006-11-20 17:07:51 +01004246 }
Cornelia Huckffa6a702009-03-04 12:44:00 +01004247 switch (dpm_order) {
4248 case DPM_ORDER_NONE:
4249 break;
4250 case DPM_ORDER_DEV_AFTER_PARENT:
4251 device_pm_move_after(dev, new_parent);
Grygorii Strashko52cdbdd2015-07-27 20:43:01 +03004252 devices_kset_move_after(dev, new_parent);
Cornelia Huckffa6a702009-03-04 12:44:00 +01004253 break;
4254 case DPM_ORDER_PARENT_BEFORE_DEV:
4255 device_pm_move_before(new_parent, dev);
Grygorii Strashko52cdbdd2015-07-27 20:43:01 +03004256 devices_kset_move_before(new_parent, dev);
Cornelia Huckffa6a702009-03-04 12:44:00 +01004257 break;
4258 case DPM_ORDER_DEV_LAST:
4259 device_pm_move_last(dev);
Grygorii Strashko52cdbdd2015-07-27 20:43:01 +03004260 devices_kset_move_last(dev);
Cornelia Huckffa6a702009-03-04 12:44:00 +01004261 break;
4262 }
Rabin Vincentbdd40342012-04-23 09:16:36 +02004263
Cornelia Huck8a824722006-11-20 17:07:51 +01004264 put_device(old_parent);
4265out:
Cornelia Huckffa6a702009-03-04 12:44:00 +01004266 device_pm_unlock();
Cornelia Huck8a824722006-11-20 17:07:51 +01004267 put_device(dev);
4268 return error;
4269}
Cornelia Huck8a824722006-11-20 17:07:51 +01004270EXPORT_SYMBOL_GPL(device_move);
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08004271
Christian Braunerb8f33e52020-02-27 04:37:15 +01004272static int device_attrs_change_owner(struct device *dev, kuid_t kuid,
4273 kgid_t kgid)
4274{
4275 struct kobject *kobj = &dev->kobj;
4276 struct class *class = dev->class;
4277 const struct device_type *type = dev->type;
4278 int error;
4279
4280 if (class) {
4281 /*
4282 * Change the device groups of the device class for @dev to
4283 * @kuid/@kgid.
4284 */
4285 error = sysfs_groups_change_owner(kobj, class->dev_groups, kuid,
4286 kgid);
4287 if (error)
4288 return error;
4289 }
4290
4291 if (type) {
4292 /*
4293 * Change the device groups of the device type for @dev to
4294 * @kuid/@kgid.
4295 */
4296 error = sysfs_groups_change_owner(kobj, type->groups, kuid,
4297 kgid);
4298 if (error)
4299 return error;
4300 }
4301
4302 /* Change the device groups of @dev to @kuid/@kgid. */
4303 error = sysfs_groups_change_owner(kobj, dev->groups, kuid, kgid);
4304 if (error)
4305 return error;
4306
4307 if (device_supports_offline(dev) && !dev->offline_disabled) {
4308 /* Change online device attributes of @dev to @kuid/@kgid. */
4309 error = sysfs_file_change_owner(kobj, dev_attr_online.attr.name,
4310 kuid, kgid);
4311 if (error)
4312 return error;
4313 }
4314
4315 return 0;
4316}
4317
4318/**
4319 * device_change_owner - change the owner of an existing device.
4320 * @dev: device.
4321 * @kuid: new owner's kuid
4322 * @kgid: new owner's kgid
4323 *
4324 * This changes the owner of @dev and its corresponding sysfs entries to
4325 * @kuid/@kgid. This function closely mirrors how @dev was added via driver
4326 * core.
4327 *
4328 * Returns 0 on success or error code on failure.
4329 */
4330int device_change_owner(struct device *dev, kuid_t kuid, kgid_t kgid)
4331{
4332 int error;
4333 struct kobject *kobj = &dev->kobj;
4334
4335 dev = get_device(dev);
4336 if (!dev)
4337 return -EINVAL;
4338
4339 /*
4340 * Change the kobject and the default attributes and groups of the
4341 * ktype associated with it to @kuid/@kgid.
4342 */
4343 error = sysfs_change_owner(kobj, kuid, kgid);
4344 if (error)
4345 goto out;
4346
4347 /*
4348 * Change the uevent file for @dev to the new owner. The uevent file
4349 * was created in a separate step when @dev got added and we mirror
4350 * that step here.
4351 */
4352 error = sysfs_file_change_owner(kobj, dev_attr_uevent.attr.name, kuid,
4353 kgid);
4354 if (error)
4355 goto out;
4356
4357 /*
4358 * Change the device groups, the device groups associated with the
4359 * device class, and the groups associated with the device type of @dev
4360 * to @kuid/@kgid.
4361 */
4362 error = device_attrs_change_owner(dev, kuid, kgid);
4363 if (error)
4364 goto out;
4365
Christian Brauner3b52fc52020-02-27 04:37:16 +01004366 error = dpm_sysfs_change_owner(dev, kuid, kgid);
4367 if (error)
4368 goto out;
4369
Christian Braunerb8f33e52020-02-27 04:37:15 +01004370#ifdef CONFIG_BLOCK
4371 if (sysfs_deprecated && dev->class == &block_class)
4372 goto out;
4373#endif
4374
4375 /*
4376 * Change the owner of the symlink located in the class directory of
4377 * the device class associated with @dev which points to the actual
4378 * directory entry for @dev to @kuid/@kgid. This ensures that the
4379 * symlink shows the same permissions as its target.
4380 */
4381 error = sysfs_link_change_owner(&dev->class->p->subsys.kobj, &dev->kobj,
4382 dev_name(dev), kuid, kgid);
4383 if (error)
4384 goto out;
4385
4386out:
4387 put_device(dev);
4388 return error;
4389}
4390EXPORT_SYMBOL_GPL(device_change_owner);
4391
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08004392/**
4393 * device_shutdown - call ->shutdown() on each device to shutdown.
4394 */
4395void device_shutdown(void)
4396{
Benson Leungf123db82013-09-24 20:05:11 -07004397 struct device *dev, *parent;
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08004398
Pingfan Liu3297c8f2018-07-19 13:14:58 +08004399 wait_for_device_probe();
4400 device_block_probing();
4401
Rafael J. Wysocki65650b32019-10-09 01:29:10 +02004402 cpufreq_suspend();
4403
Hugh Daschbach62458382010-03-22 10:36:37 -07004404 spin_lock(&devices_kset->list_lock);
4405 /*
4406 * Walk the devices list backward, shutting down each in turn.
4407 * Beware that device unplug events may also start pulling
4408 * devices offline, even as the system is shutting down.
4409 */
4410 while (!list_empty(&devices_kset->list)) {
4411 dev = list_entry(devices_kset->list.prev, struct device,
4412 kobj.entry);
Ming Leid1c6c032012-06-22 18:01:40 +08004413
4414 /*
4415 * hold reference count of device's parent to
4416 * prevent it from being freed because parent's
4417 * lock is to be held
4418 */
Benson Leungf123db82013-09-24 20:05:11 -07004419 parent = get_device(dev->parent);
Hugh Daschbach62458382010-03-22 10:36:37 -07004420 get_device(dev);
4421 /*
4422 * Make sure the device is off the kset list, in the
4423 * event that dev->*->shutdown() doesn't remove it.
4424 */
4425 list_del_init(&dev->kobj.entry);
4426 spin_unlock(&devices_kset->list_lock);
Alan Sternfe6b91f2011-12-06 23:24:52 +01004427
Ming Leid1c6c032012-06-22 18:01:40 +08004428 /* hold lock to avoid race with probe/release */
Benson Leungf123db82013-09-24 20:05:11 -07004429 if (parent)
4430 device_lock(parent);
Ming Leid1c6c032012-06-22 18:01:40 +08004431 device_lock(dev);
4432
Alan Sternfe6b91f2011-12-06 23:24:52 +01004433 /* Don't allow any more runtime suspends */
4434 pm_runtime_get_noresume(dev);
4435 pm_runtime_barrier(dev);
Hugh Daschbach62458382010-03-22 10:36:37 -07004436
Michal Suchanek75216212017-08-11 15:44:43 +02004437 if (dev->class && dev->class->shutdown_pre) {
Josh Zimmermanf77af152017-06-25 14:53:23 -07004438 if (initcall_debug)
Michal Suchanek75216212017-08-11 15:44:43 +02004439 dev_info(dev, "shutdown_pre\n");
4440 dev->class->shutdown_pre(dev);
4441 }
4442 if (dev->bus && dev->bus->shutdown) {
ShuoX Liu0246c4f2012-11-23 15:14:12 +08004443 if (initcall_debug)
4444 dev_info(dev, "shutdown\n");
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08004445 dev->bus->shutdown(dev);
4446 } else if (dev->driver && dev->driver->shutdown) {
ShuoX Liu0246c4f2012-11-23 15:14:12 +08004447 if (initcall_debug)
4448 dev_info(dev, "shutdown\n");
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08004449 dev->driver->shutdown(dev);
4450 }
Ming Leid1c6c032012-06-22 18:01:40 +08004451
4452 device_unlock(dev);
Benson Leungf123db82013-09-24 20:05:11 -07004453 if (parent)
4454 device_unlock(parent);
Ming Leid1c6c032012-06-22 18:01:40 +08004455
Hugh Daschbach62458382010-03-22 10:36:37 -07004456 put_device(dev);
Benson Leungf123db82013-09-24 20:05:11 -07004457 put_device(parent);
Hugh Daschbach62458382010-03-22 10:36:37 -07004458
4459 spin_lock(&devices_kset->list_lock);
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08004460 }
Hugh Daschbach62458382010-03-22 10:36:37 -07004461 spin_unlock(&devices_kset->list_lock);
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08004462}
Joe Perches99bcf212010-06-27 01:02:34 +00004463
4464/*
4465 * Device logging functions
4466 */
4467
4468#ifdef CONFIG_PRINTK
John Ogness74caba72020-09-21 13:24:45 +02064469static void
4470set_dev_info(const struct device *dev, struct dev_printk_info *dev_info)
Joe Perches99bcf212010-06-27 01:02:34 +00004471{
Kay Sieversc4e00da2012-05-03 02:29:59 +02004472 const char *subsys;
John Ogness74caba72020-09-21 13:24:45 +02064473
4474 memset(dev_info, 0, sizeof(*dev_info));
Joe Perches99bcf212010-06-27 01:02:34 +00004475
Kay Sieversc4e00da2012-05-03 02:29:59 +02004476 if (dev->class)
4477 subsys = dev->class->name;
4478 else if (dev->bus)
4479 subsys = dev->bus->name;
4480 else
John Ogness74caba72020-09-21 13:24:45 +02064481 return;
Kay Sieversc4e00da2012-05-03 02:29:59 +02004482
John Ogness74caba72020-09-21 13:24:45 +02064483 strscpy(dev_info->subsystem, subsys, sizeof(dev_info->subsystem));
Kay Sieversc4e00da2012-05-03 02:29:59 +02004484
4485 /*
4486 * Add device identifier DEVICE=:
4487 * b12:8 block dev_t
4488 * c127:3 char dev_t
4489 * n8 netdev ifindex
4490 * +sound:card0 subsystem:devname
4491 */
4492 if (MAJOR(dev->devt)) {
4493 char c;
4494
4495 if (strcmp(subsys, "block") == 0)
4496 c = 'b';
4497 else
4498 c = 'c';
John Ogness74caba72020-09-21 13:24:45 +02064499
4500 snprintf(dev_info->device, sizeof(dev_info->device),
4501 "%c%u:%u", c, MAJOR(dev->devt), MINOR(dev->devt));
Kay Sieversc4e00da2012-05-03 02:29:59 +02004502 } else if (strcmp(subsys, "net") == 0) {
4503 struct net_device *net = to_net_dev(dev);
4504
John Ogness74caba72020-09-21 13:24:45 +02064505 snprintf(dev_info->device, sizeof(dev_info->device),
4506 "n%u", net->ifindex);
Kay Sieversc4e00da2012-05-03 02:29:59 +02004507 } else {
John Ogness74caba72020-09-21 13:24:45 +02064508 snprintf(dev_info->device, sizeof(dev_info->device),
4509 "+%s:%s", subsys, dev_name(dev));
Kay Sieversc4e00da2012-05-03 02:29:59 +02004510 }
Joe Perches99bcf212010-06-27 01:02:34 +00004511}
Joe Perches798efc62012-09-12 20:11:29 -07004512
Joe Perches05e4e5b2012-09-12 20:13:37 -07004513int dev_vprintk_emit(int level, const struct device *dev,
4514 const char *fmt, va_list args)
4515{
John Ogness74caba72020-09-21 13:24:45 +02064516 struct dev_printk_info dev_info;
Joe Perches05e4e5b2012-09-12 20:13:37 -07004517
John Ogness74caba72020-09-21 13:24:45 +02064518 set_dev_info(dev, &dev_info);
Joe Perches05e4e5b2012-09-12 20:13:37 -07004519
John Ogness74caba72020-09-21 13:24:45 +02064520 return vprintk_emit(0, level, &dev_info, fmt, args);
Joe Perches05e4e5b2012-09-12 20:13:37 -07004521}
4522EXPORT_SYMBOL(dev_vprintk_emit);
4523
4524int dev_printk_emit(int level, const struct device *dev, const char *fmt, ...)
4525{
4526 va_list args;
4527 int r;
4528
4529 va_start(args, fmt);
4530
4531 r = dev_vprintk_emit(level, dev, fmt, args);
4532
4533 va_end(args);
4534
4535 return r;
4536}
4537EXPORT_SYMBOL(dev_printk_emit);
4538
Joe Perchesd1f10522014-12-25 15:07:04 -08004539static void __dev_printk(const char *level, const struct device *dev,
Joe Perches798efc62012-09-12 20:11:29 -07004540 struct va_format *vaf)
4541{
Joe Perchesd1f10522014-12-25 15:07:04 -08004542 if (dev)
4543 dev_printk_emit(level[1] - '0', dev, "%s %s: %pV",
4544 dev_driver_string(dev), dev_name(dev), vaf);
4545 else
4546 printk("%s(NULL device *): %pV", level, vaf);
Joe Perches798efc62012-09-12 20:11:29 -07004547}
Joe Perches99bcf212010-06-27 01:02:34 +00004548
Joe Perchesd1f10522014-12-25 15:07:04 -08004549void dev_printk(const char *level, const struct device *dev,
4550 const char *fmt, ...)
Joe Perches99bcf212010-06-27 01:02:34 +00004551{
4552 struct va_format vaf;
4553 va_list args;
Joe Perches99bcf212010-06-27 01:02:34 +00004554
4555 va_start(args, fmt);
4556
4557 vaf.fmt = fmt;
4558 vaf.va = &args;
4559
Joe Perchesd1f10522014-12-25 15:07:04 -08004560 __dev_printk(level, dev, &vaf);
Joe Perches798efc62012-09-12 20:11:29 -07004561
Joe Perches99bcf212010-06-27 01:02:34 +00004562 va_end(args);
Joe Perches99bcf212010-06-27 01:02:34 +00004563}
4564EXPORT_SYMBOL(dev_printk);
4565
4566#define define_dev_printk_level(func, kern_level) \
Joe Perchesd1f10522014-12-25 15:07:04 -08004567void func(const struct device *dev, const char *fmt, ...) \
Joe Perches99bcf212010-06-27 01:02:34 +00004568{ \
4569 struct va_format vaf; \
4570 va_list args; \
Joe Perches99bcf212010-06-27 01:02:34 +00004571 \
4572 va_start(args, fmt); \
4573 \
4574 vaf.fmt = fmt; \
4575 vaf.va = &args; \
4576 \
Joe Perchesd1f10522014-12-25 15:07:04 -08004577 __dev_printk(kern_level, dev, &vaf); \
Joe Perches798efc62012-09-12 20:11:29 -07004578 \
Joe Perches99bcf212010-06-27 01:02:34 +00004579 va_end(args); \
Joe Perches99bcf212010-06-27 01:02:34 +00004580} \
4581EXPORT_SYMBOL(func);
4582
Joe Perches663336e2018-05-09 08:15:46 -07004583define_dev_printk_level(_dev_emerg, KERN_EMERG);
4584define_dev_printk_level(_dev_alert, KERN_ALERT);
4585define_dev_printk_level(_dev_crit, KERN_CRIT);
4586define_dev_printk_level(_dev_err, KERN_ERR);
4587define_dev_printk_level(_dev_warn, KERN_WARNING);
4588define_dev_printk_level(_dev_notice, KERN_NOTICE);
Joe Perches99bcf212010-06-27 01:02:34 +00004589define_dev_printk_level(_dev_info, KERN_INFO);
4590
4591#endif
Rafael J. Wysocki97badf82015-04-03 23:23:37 +02004592
Andrzej Hajdaa787e542020-07-13 16:43:21 +02004593/**
4594 * dev_err_probe - probe error check and log helper
4595 * @dev: the pointer to the struct device
4596 * @err: error value to test
4597 * @fmt: printf-style format string
4598 * @...: arguments as specified in the format string
4599 *
4600 * This helper implements common pattern present in probe functions for error
4601 * checking: print debug or error message depending if the error value is
4602 * -EPROBE_DEFER and propagate error upwards.
Andrzej Hajdad090b702020-07-13 16:43:22 +02004603 * In case of -EPROBE_DEFER it sets also defer probe reason, which can be
4604 * checked later by reading devices_deferred debugfs attribute.
Mauro Carvalho Chehab074b3aa2020-09-09 11:53:43 +02004605 * It replaces code sequence::
4606 *
Andrzej Hajdaa787e542020-07-13 16:43:21 +02004607 * if (err != -EPROBE_DEFER)
4608 * dev_err(dev, ...);
4609 * else
4610 * dev_dbg(dev, ...);
4611 * return err;
Mauro Carvalho Chehab074b3aa2020-09-09 11:53:43 +02004612 *
4613 * with::
4614 *
Andrzej Hajdaa787e542020-07-13 16:43:21 +02004615 * return dev_err_probe(dev, err, ...);
4616 *
4617 * Returns @err.
4618 *
4619 */
4620int dev_err_probe(const struct device *dev, int err, const char *fmt, ...)
4621{
4622 struct va_format vaf;
4623 va_list args;
4624
4625 va_start(args, fmt);
4626 vaf.fmt = fmt;
4627 vaf.va = &args;
4628
Andrzej Hajdad090b702020-07-13 16:43:22 +02004629 if (err != -EPROBE_DEFER) {
Michał Mirosław693a8e92020-08-28 18:14:35 +02004630 dev_err(dev, "error %pe: %pV", ERR_PTR(err), &vaf);
Andrzej Hajdad090b702020-07-13 16:43:22 +02004631 } else {
4632 device_set_deferred_probe_reason(dev, &vaf);
Michał Mirosław693a8e92020-08-28 18:14:35 +02004633 dev_dbg(dev, "error %pe: %pV", ERR_PTR(err), &vaf);
Andrzej Hajdad090b702020-07-13 16:43:22 +02004634 }
Andrzej Hajdaa787e542020-07-13 16:43:21 +02004635
4636 va_end(args);
4637
4638 return err;
4639}
4640EXPORT_SYMBOL_GPL(dev_err_probe);
4641
Rafael J. Wysocki97badf82015-04-03 23:23:37 +02004642static inline bool fwnode_is_primary(struct fwnode_handle *fwnode)
4643{
4644 return fwnode && !IS_ERR(fwnode->secondary);
4645}
4646
4647/**
4648 * set_primary_fwnode - Change the primary firmware node of a given device.
4649 * @dev: Device to handle.
4650 * @fwnode: New primary firmware node of the device.
4651 *
4652 * Set the device's firmware node pointer to @fwnode, but if a secondary
4653 * firmware node of the device is present, preserve it.
Bard Liao3f7bdda2021-01-05 17:11:46 +08004654 *
4655 * Valid fwnode cases are:
4656 * - primary --> secondary --> -ENODEV
4657 * - primary --> NULL
4658 * - secondary --> -ENODEV
4659 * - NULL
Rafael J. Wysocki97badf82015-04-03 23:23:37 +02004660 */
4661void set_primary_fwnode(struct device *dev, struct fwnode_handle *fwnode)
4662{
Andy Shevchenko99aed922020-10-22 21:41:00 +03004663 struct device *parent = dev->parent;
Heikki Krogerusc15e1bd2020-08-21 13:53:42 +03004664 struct fwnode_handle *fn = dev->fwnode;
Rafael J. Wysocki97badf82015-04-03 23:23:37 +02004665
Heikki Krogerusc15e1bd2020-08-21 13:53:42 +03004666 if (fwnode) {
Rafael J. Wysocki97badf82015-04-03 23:23:37 +02004667 if (fwnode_is_primary(fn))
4668 fn = fn->secondary;
4669
Mika Westerberg55f89a82015-11-30 17:11:39 +02004670 if (fn) {
4671 WARN_ON(fwnode->secondary);
4672 fwnode->secondary = fn;
4673 }
Rafael J. Wysocki97badf82015-04-03 23:23:37 +02004674 dev->fwnode = fwnode;
4675 } else {
Heikki Krogerusc15e1bd2020-08-21 13:53:42 +03004676 if (fwnode_is_primary(fn)) {
4677 dev->fwnode = fn->secondary;
Bard Liao3f7bdda2021-01-05 17:11:46 +08004678 /* Set fn->secondary = NULL, so fn remains the primary fwnode */
Andy Shevchenko99aed922020-10-22 21:41:00 +03004679 if (!(parent && fn == parent->fwnode))
Bard Liao47f44692021-01-05 17:11:45 +08004680 fn->secondary = NULL;
Heikki Krogerusc15e1bd2020-08-21 13:53:42 +03004681 } else {
4682 dev->fwnode = NULL;
4683 }
Rafael J. Wysocki97badf82015-04-03 23:23:37 +02004684 }
4685}
4686EXPORT_SYMBOL_GPL(set_primary_fwnode);
4687
4688/**
4689 * set_secondary_fwnode - Change the secondary firmware node of a given device.
4690 * @dev: Device to handle.
4691 * @fwnode: New secondary firmware node of the device.
4692 *
4693 * If a primary firmware node of the device is present, set its secondary
4694 * pointer to @fwnode. Otherwise, set the device's firmware node pointer to
4695 * @fwnode.
4696 */
4697void set_secondary_fwnode(struct device *dev, struct fwnode_handle *fwnode)
4698{
4699 if (fwnode)
4700 fwnode->secondary = ERR_PTR(-ENODEV);
4701
4702 if (fwnode_is_primary(dev->fwnode))
4703 dev->fwnode->secondary = fwnode;
4704 else
4705 dev->fwnode = fwnode;
4706}
Andy Shevchenko96489ae2020-04-08 19:09:01 +03004707EXPORT_SYMBOL_GPL(set_secondary_fwnode);
Johan Hovold4e75e1d2017-06-06 17:59:00 +02004708
4709/**
4710 * device_set_of_node_from_dev - reuse device-tree node of another device
4711 * @dev: device whose device-tree node is being set
4712 * @dev2: device whose device-tree node is being reused
4713 *
4714 * Takes another reference to the new device-tree node after first dropping
4715 * any reference held to the old node.
4716 */
4717void device_set_of_node_from_dev(struct device *dev, const struct device *dev2)
4718{
4719 of_node_put(dev->of_node);
4720 dev->of_node = of_node_get(dev2->of_node);
4721 dev->of_node_reused = true;
4722}
4723EXPORT_SYMBOL_GPL(device_set_of_node_from_dev);
Suzuki K Poulose65b66682019-06-14 18:54:01 +01004724
Suzuki K Poulose6cda08a2019-07-23 23:18:32 +01004725int device_match_name(struct device *dev, const void *name)
4726{
4727 return sysfs_streq(dev_name(dev), name);
4728}
4729EXPORT_SYMBOL_GPL(device_match_name);
4730
Suzuki K Poulose65b66682019-06-14 18:54:01 +01004731int device_match_of_node(struct device *dev, const void *np)
4732{
4733 return dev->of_node == np;
4734}
4735EXPORT_SYMBOL_GPL(device_match_of_node);
Suzuki K Poulose67843bb2019-07-23 23:18:34 +01004736
4737int device_match_fwnode(struct device *dev, const void *fwnode)
4738{
4739 return dev_fwnode(dev) == fwnode;
4740}
4741EXPORT_SYMBOL_GPL(device_match_fwnode);
Suzuki K Poulose4495dfd2019-07-23 23:18:35 +01004742
4743int device_match_devt(struct device *dev, const void *pdevt)
4744{
4745 return dev->devt == *(dev_t *)pdevt;
4746}
4747EXPORT_SYMBOL_GPL(device_match_devt);
Suzuki K Poulose00500142019-07-23 23:18:36 +01004748
4749int device_match_acpi_dev(struct device *dev, const void *adev)
4750{
4751 return ACPI_COMPANION(dev) == adev;
4752}
4753EXPORT_SYMBOL(device_match_acpi_dev);
Suzuki K Poulose6bf85ba2019-07-23 23:18:37 +01004754
4755int device_match_any(struct device *dev, const void *unused)
4756{
4757 return 1;
4758}
4759EXPORT_SYMBOL_GPL(device_match_any);