1 // SPDX-License-Identifier: GPL-2.0
2 /*
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
7 * Copyright (c) 2006 Greg Kroah-Hartman <gregkh@suse.de>
8 * Copyright (c) 2006 Novell, Inc.
9 */
10
11 #include <linux/acpi.h>
12 #include <linux/cpufreq.h>
13 #include <linux/device.h>
14 #include <linux/err.h>
15 #include <linux/fwnode.h>
16 #include <linux/init.h>
17 #include <linux/module.h>
18 #include <linux/slab.h>
19 #include <linux/string.h>
20 #include <linux/kdev_t.h>
21 #include <linux/notifier.h>
22 #include <linux/of.h>
23 #include <linux/of_device.h>
24 #include <linux/blkdev.h>
25 #include <linux/mutex.h>
26 #include <linux/pm_runtime.h>
27 #include <linux/netdevice.h>
28 #include <linux/sched/signal.h>
29 #include <linux/sched/mm.h>
30 #include <linux/swiotlb.h>
31 #include <linux/sysfs.h>
32 #include <linux/dma-map-ops.h> /* for dma_default_coherent */
33
34 #include "base.h"
35 #include "physical_location.h"
36 #include "power/power.h"
37
38 #ifdef CONFIG_SYSFS_DEPRECATED
39 #ifdef CONFIG_SYSFS_DEPRECATED_V2
40 long sysfs_deprecated = 1;
41 #else
42 long sysfs_deprecated = 0;
43 #endif
sysfs_deprecated_setup(char * arg)44 static int __init sysfs_deprecated_setup(char *arg)
45 {
46 return kstrtol(arg, 10, &sysfs_deprecated);
47 }
48 early_param("sysfs.deprecated", sysfs_deprecated_setup);
49 #endif
50
51 /* Device links support. */
52 static LIST_HEAD(deferred_sync);
53 static unsigned int defer_sync_state_count = 1;
54 static DEFINE_MUTEX(fwnode_link_lock);
55 static bool fw_devlink_is_permissive(void);
56 static bool fw_devlink_drv_reg_done;
57
58 /**
59 * fwnode_link_add - Create a link between two fwnode_handles.
60 * @con: Consumer end of the link.
61 * @sup: Supplier end of the link.
62 *
63 * Create a fwnode link between fwnode handles @con and @sup. The fwnode link
64 * represents the detail that the firmware lists @sup fwnode as supplying a
65 * resource to @con.
66 *
67 * The driver core will use the fwnode link to create a device link between the
68 * two device objects corresponding to @con and @sup when they are created. The
69 * driver core will automatically delete the fwnode link between @con and @sup
70 * after doing that.
71 *
72 * Attempts to create duplicate links between the same pair of fwnode handles
73 * are ignored and there is no reference counting.
74 */
fwnode_link_add(struct fwnode_handle * con,struct fwnode_handle * sup)75 int fwnode_link_add(struct fwnode_handle *con, struct fwnode_handle *sup)
76 {
77 struct fwnode_link *link;
78 int ret = 0;
79
80 mutex_lock(&fwnode_link_lock);
81
82 list_for_each_entry(link, &sup->consumers, s_hook)
83 if (link->consumer == con)
84 goto out;
85
86 link = kzalloc(sizeof(*link), GFP_KERNEL);
87 if (!link) {
88 ret = -ENOMEM;
89 goto out;
90 }
91
92 link->supplier = sup;
93 INIT_LIST_HEAD(&link->s_hook);
94 link->consumer = con;
95 INIT_LIST_HEAD(&link->c_hook);
96
97 list_add(&link->s_hook, &sup->consumers);
98 list_add(&link->c_hook, &con->suppliers);
99 pr_debug("%pfwP Linked as a fwnode consumer to %pfwP\n",
100 con, sup);
101 out:
102 mutex_unlock(&fwnode_link_lock);
103
104 return ret;
105 }
106
107 /**
108 * __fwnode_link_del - Delete a link between two fwnode_handles.
109 * @link: the fwnode_link to be deleted
110 *
111 * The fwnode_link_lock needs to be held when this function is called.
112 */
__fwnode_link_del(struct fwnode_link * link)113 static void __fwnode_link_del(struct fwnode_link *link)
114 {
115 pr_debug("%pfwP Dropping the fwnode link to %pfwP\n",
116 link->consumer, link->supplier);
117 list_del(&link->s_hook);
118 list_del(&link->c_hook);
119 kfree(link);
120 }
121
122 /**
123 * fwnode_links_purge_suppliers - Delete all supplier links of fwnode_handle.
124 * @fwnode: fwnode whose supplier links need to be deleted
125 *
126 * Deletes all supplier links connecting directly to @fwnode.
127 */
fwnode_links_purge_suppliers(struct fwnode_handle * fwnode)128 static void fwnode_links_purge_suppliers(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->suppliers, c_hook)
134 __fwnode_link_del(link);
135 mutex_unlock(&fwnode_link_lock);
136 }
137
138 /**
139 * fwnode_links_purge_consumers - Delete all consumer links of fwnode_handle.
140 * @fwnode: fwnode whose consumer links need to be deleted
141 *
142 * Deletes all consumer links connecting directly to @fwnode.
143 */
fwnode_links_purge_consumers(struct fwnode_handle * fwnode)144 static void fwnode_links_purge_consumers(struct fwnode_handle *fwnode)
145 {
146 struct fwnode_link *link, *tmp;
147
148 mutex_lock(&fwnode_link_lock);
149 list_for_each_entry_safe(link, tmp, &fwnode->consumers, s_hook)
150 __fwnode_link_del(link);
151 mutex_unlock(&fwnode_link_lock);
152 }
153
154 /**
155 * fwnode_links_purge - Delete all links connected to a fwnode_handle.
156 * @fwnode: fwnode whose links needs to be deleted
157 *
158 * Deletes all links connecting directly to a fwnode.
159 */
fwnode_links_purge(struct fwnode_handle * fwnode)160 void fwnode_links_purge(struct fwnode_handle *fwnode)
161 {
162 fwnode_links_purge_suppliers(fwnode);
163 fwnode_links_purge_consumers(fwnode);
164 }
165
fw_devlink_purge_absent_suppliers(struct fwnode_handle * fwnode)166 void fw_devlink_purge_absent_suppliers(struct fwnode_handle *fwnode)
167 {
168 struct fwnode_handle *child;
169
170 /* Don't purge consumer links of an added child */
171 if (fwnode->dev)
172 return;
173
174 fwnode->flags |= FWNODE_FLAG_NOT_DEVICE;
175 fwnode_links_purge_consumers(fwnode);
176
177 fwnode_for_each_available_child_node(fwnode, child)
178 fw_devlink_purge_absent_suppliers(child);
179 }
180 EXPORT_SYMBOL_GPL(fw_devlink_purge_absent_suppliers);
181
182 #ifdef CONFIG_SRCU
183 static DEFINE_MUTEX(device_links_lock);
184 DEFINE_STATIC_SRCU(device_links_srcu);
185
device_links_write_lock(void)186 static inline void device_links_write_lock(void)
187 {
188 mutex_lock(&device_links_lock);
189 }
190
device_links_write_unlock(void)191 static inline void device_links_write_unlock(void)
192 {
193 mutex_unlock(&device_links_lock);
194 }
195
device_links_read_lock(void)196 int device_links_read_lock(void) __acquires(&device_links_srcu)
197 {
198 return srcu_read_lock(&device_links_srcu);
199 }
200
device_links_read_unlock(int idx)201 void device_links_read_unlock(int idx) __releases(&device_links_srcu)
202 {
203 srcu_read_unlock(&device_links_srcu, idx);
204 }
205
device_links_read_lock_held(void)206 int device_links_read_lock_held(void)
207 {
208 return srcu_read_lock_held(&device_links_srcu);
209 }
210
device_link_synchronize_removal(void)211 static void device_link_synchronize_removal(void)
212 {
213 synchronize_srcu(&device_links_srcu);
214 }
215
device_link_remove_from_lists(struct device_link * link)216 static void device_link_remove_from_lists(struct device_link *link)
217 {
218 list_del_rcu(&link->s_node);
219 list_del_rcu(&link->c_node);
220 }
221 #else /* !CONFIG_SRCU */
222 static DECLARE_RWSEM(device_links_lock);
223
device_links_write_lock(void)224 static inline void device_links_write_lock(void)
225 {
226 down_write(&device_links_lock);
227 }
228
device_links_write_unlock(void)229 static inline void device_links_write_unlock(void)
230 {
231 up_write(&device_links_lock);
232 }
233
device_links_read_lock(void)234 int device_links_read_lock(void)
235 {
236 down_read(&device_links_lock);
237 return 0;
238 }
239
device_links_read_unlock(int not_used)240 void device_links_read_unlock(int not_used)
241 {
242 up_read(&device_links_lock);
243 }
244
245 #ifdef CONFIG_DEBUG_LOCK_ALLOC
device_links_read_lock_held(void)246 int device_links_read_lock_held(void)
247 {
248 return lockdep_is_held(&device_links_lock);
249 }
250 #endif
251
device_link_synchronize_removal(void)252 static inline void device_link_synchronize_removal(void)
253 {
254 }
255
device_link_remove_from_lists(struct device_link * link)256 static void device_link_remove_from_lists(struct device_link *link)
257 {
258 list_del(&link->s_node);
259 list_del(&link->c_node);
260 }
261 #endif /* !CONFIG_SRCU */
262
device_is_ancestor(struct device * dev,struct device * target)263 static bool device_is_ancestor(struct device *dev, struct device *target)
264 {
265 while (target->parent) {
266 target = target->parent;
267 if (dev == target)
268 return true;
269 }
270 return false;
271 }
272
273 /**
274 * device_is_dependent - Check if one device depends on another one
275 * @dev: Device to check dependencies for.
276 * @target: Device to check against.
277 *
278 * Check if @target depends on @dev or any device dependent on it (its child or
279 * its consumer etc). Return 1 if that is the case or 0 otherwise.
280 */
device_is_dependent(struct device * dev,void * target)281 int device_is_dependent(struct device *dev, void *target)
282 {
283 struct device_link *link;
284 int ret;
285
286 /*
287 * The "ancestors" check is needed to catch the case when the target
288 * device has not been completely initialized yet and it is still
289 * missing from the list of children of its parent device.
290 */
291 if (dev == target || device_is_ancestor(dev, target))
292 return 1;
293
294 ret = device_for_each_child(dev, target, device_is_dependent);
295 if (ret)
296 return ret;
297
298 list_for_each_entry(link, &dev->links.consumers, s_node) {
299 if ((link->flags & ~DL_FLAG_INFERRED) ==
300 (DL_FLAG_SYNC_STATE_ONLY | DL_FLAG_MANAGED))
301 continue;
302
303 if (link->consumer == target)
304 return 1;
305
306 ret = device_is_dependent(link->consumer, target);
307 if (ret)
308 break;
309 }
310 return ret;
311 }
312
device_link_init_status(struct device_link * link,struct device * consumer,struct device * supplier)313 static void device_link_init_status(struct device_link *link,
314 struct device *consumer,
315 struct device *supplier)
316 {
317 switch (supplier->links.status) {
318 case DL_DEV_PROBING:
319 switch (consumer->links.status) {
320 case DL_DEV_PROBING:
321 /*
322 * A consumer driver can create a link to a supplier
323 * that has not completed its probing yet as long as it
324 * knows that the supplier is already functional (for
325 * example, it has just acquired some resources from the
326 * supplier).
327 */
328 link->status = DL_STATE_CONSUMER_PROBE;
329 break;
330 default:
331 link->status = DL_STATE_DORMANT;
332 break;
333 }
334 break;
335 case DL_DEV_DRIVER_BOUND:
336 switch (consumer->links.status) {
337 case DL_DEV_PROBING:
338 link->status = DL_STATE_CONSUMER_PROBE;
339 break;
340 case DL_DEV_DRIVER_BOUND:
341 link->status = DL_STATE_ACTIVE;
342 break;
343 default:
344 link->status = DL_STATE_AVAILABLE;
345 break;
346 }
347 break;
348 case DL_DEV_UNBINDING:
349 link->status = DL_STATE_SUPPLIER_UNBIND;
350 break;
351 default:
352 link->status = DL_STATE_DORMANT;
353 break;
354 }
355 }
356
device_reorder_to_tail(struct device * dev,void * not_used)357 static int device_reorder_to_tail(struct device *dev, void *not_used)
358 {
359 struct device_link *link;
360
361 /*
362 * Devices that have not been registered yet will be put to the ends
363 * of the lists during the registration, so skip them here.
364 */
365 if (device_is_registered(dev))
366 devices_kset_move_last(dev);
367
368 if (device_pm_initialized(dev))
369 device_pm_move_last(dev);
370
371 device_for_each_child(dev, NULL, device_reorder_to_tail);
372 list_for_each_entry(link, &dev->links.consumers, s_node) {
373 if ((link->flags & ~DL_FLAG_INFERRED) ==
374 (DL_FLAG_SYNC_STATE_ONLY | DL_FLAG_MANAGED))
375 continue;
376 device_reorder_to_tail(link->consumer, NULL);
377 }
378
379 return 0;
380 }
381
382 /**
383 * device_pm_move_to_tail - Move set of devices to the end of device lists
384 * @dev: Device to move
385 *
386 * This is a device_reorder_to_tail() wrapper taking the requisite locks.
387 *
388 * It moves the @dev along with all of its children and all of its consumers
389 * to the ends of the device_kset and dpm_list, recursively.
390 */
device_pm_move_to_tail(struct device * dev)391 void device_pm_move_to_tail(struct device *dev)
392 {
393 int idx;
394
395 idx = device_links_read_lock();
396 device_pm_lock();
397 device_reorder_to_tail(dev, NULL);
398 device_pm_unlock();
399 device_links_read_unlock(idx);
400 }
401
402 #define to_devlink(dev) container_of((dev), struct device_link, link_dev)
403
status_show(struct device * dev,struct device_attribute * attr,char * buf)404 static ssize_t status_show(struct device *dev,
405 struct device_attribute *attr, char *buf)
406 {
407 const char *output;
408
409 switch (to_devlink(dev)->status) {
410 case DL_STATE_NONE:
411 output = "not tracked";
412 break;
413 case DL_STATE_DORMANT:
414 output = "dormant";
415 break;
416 case DL_STATE_AVAILABLE:
417 output = "available";
418 break;
419 case DL_STATE_CONSUMER_PROBE:
420 output = "consumer probing";
421 break;
422 case DL_STATE_ACTIVE:
423 output = "active";
424 break;
425 case DL_STATE_SUPPLIER_UNBIND:
426 output = "supplier unbinding";
427 break;
428 default:
429 output = "unknown";
430 break;
431 }
432
433 return sysfs_emit(buf, "%s\n", output);
434 }
435 static DEVICE_ATTR_RO(status);
436
auto_remove_on_show(struct device * dev,struct device_attribute * attr,char * buf)437 static ssize_t auto_remove_on_show(struct device *dev,
438 struct device_attribute *attr, char *buf)
439 {
440 struct device_link *link = to_devlink(dev);
441 const char *output;
442
443 if (link->flags & DL_FLAG_AUTOREMOVE_SUPPLIER)
444 output = "supplier unbind";
445 else if (link->flags & DL_FLAG_AUTOREMOVE_CONSUMER)
446 output = "consumer unbind";
447 else
448 output = "never";
449
450 return sysfs_emit(buf, "%s\n", output);
451 }
452 static DEVICE_ATTR_RO(auto_remove_on);
453
runtime_pm_show(struct device * dev,struct device_attribute * attr,char * buf)454 static ssize_t runtime_pm_show(struct device *dev,
455 struct device_attribute *attr, char *buf)
456 {
457 struct device_link *link = to_devlink(dev);
458
459 return sysfs_emit(buf, "%d\n", !!(link->flags & DL_FLAG_PM_RUNTIME));
460 }
461 static DEVICE_ATTR_RO(runtime_pm);
462
sync_state_only_show(struct device * dev,struct device_attribute * attr,char * buf)463 static ssize_t sync_state_only_show(struct device *dev,
464 struct device_attribute *attr, char *buf)
465 {
466 struct device_link *link = to_devlink(dev);
467
468 return sysfs_emit(buf, "%d\n",
469 !!(link->flags & DL_FLAG_SYNC_STATE_ONLY));
470 }
471 static DEVICE_ATTR_RO(sync_state_only);
472
473 static struct attribute *devlink_attrs[] = {
474 &dev_attr_status.attr,
475 &dev_attr_auto_remove_on.attr,
476 &dev_attr_runtime_pm.attr,
477 &dev_attr_sync_state_only.attr,
478 NULL,
479 };
480 ATTRIBUTE_GROUPS(devlink);
481
device_link_release_fn(struct work_struct * work)482 static void device_link_release_fn(struct work_struct *work)
483 {
484 struct device_link *link = container_of(work, struct device_link, rm_work);
485
486 /* Ensure that all references to the link object have been dropped. */
487 device_link_synchronize_removal();
488
489 pm_runtime_release_supplier(link);
490 /*
491 * If supplier_preactivated is set, the link has been dropped between
492 * the pm_runtime_get_suppliers() and pm_runtime_put_suppliers() calls
493 * in __driver_probe_device(). In that case, drop the supplier's
494 * PM-runtime usage counter to remove the reference taken by
495 * pm_runtime_get_suppliers().
496 */
497 if (link->supplier_preactivated)
498 pm_runtime_put_noidle(link->supplier);
499
500 pm_request_idle(link->supplier);
501
502 put_device(link->consumer);
503 put_device(link->supplier);
504 kfree(link);
505 }
506
devlink_dev_release(struct device * dev)507 static void devlink_dev_release(struct device *dev)
508 {
509 struct device_link *link = to_devlink(dev);
510
511 INIT_WORK(&link->rm_work, device_link_release_fn);
512 /*
513 * It may take a while to complete this work because of the SRCU
514 * synchronization in device_link_release_fn() and if the consumer or
515 * supplier devices get deleted when it runs, so put it into the "long"
516 * workqueue.
517 */
518 queue_work(system_long_wq, &link->rm_work);
519 }
520
521 static struct class devlink_class = {
522 .name = "devlink",
523 .owner = THIS_MODULE,
524 .dev_groups = devlink_groups,
525 .dev_release = devlink_dev_release,
526 };
527
devlink_add_symlinks(struct device * dev,struct class_interface * class_intf)528 static int devlink_add_symlinks(struct device *dev,
529 struct class_interface *class_intf)
530 {
531 int ret;
532 size_t len;
533 struct device_link *link = to_devlink(dev);
534 struct device *sup = link->supplier;
535 struct device *con = link->consumer;
536 char *buf;
537
538 len = max(strlen(dev_bus_name(sup)) + strlen(dev_name(sup)),
539 strlen(dev_bus_name(con)) + strlen(dev_name(con)));
540 len += strlen(":");
541 len += strlen("supplier:") + 1;
542 buf = kzalloc(len, GFP_KERNEL);
543 if (!buf)
544 return -ENOMEM;
545
546 ret = sysfs_create_link(&link->link_dev.kobj, &sup->kobj, "supplier");
547 if (ret)
548 goto out;
549
550 ret = sysfs_create_link(&link->link_dev.kobj, &con->kobj, "consumer");
551 if (ret)
552 goto err_con;
553
554 snprintf(buf, len, "consumer:%s:%s", dev_bus_name(con), dev_name(con));
555 ret = sysfs_create_link(&sup->kobj, &link->link_dev.kobj, buf);
556 if (ret)
557 goto err_con_dev;
558
559 snprintf(buf, len, "supplier:%s:%s", dev_bus_name(sup), dev_name(sup));
560 ret = sysfs_create_link(&con->kobj, &link->link_dev.kobj, buf);
561 if (ret)
562 goto err_sup_dev;
563
564 goto out;
565
566 err_sup_dev:
567 snprintf(buf, len, "consumer:%s:%s", dev_bus_name(con), dev_name(con));
568 sysfs_remove_link(&sup->kobj, buf);
569 err_con_dev:
570 sysfs_remove_link(&link->link_dev.kobj, "consumer");
571 err_con:
572 sysfs_remove_link(&link->link_dev.kobj, "supplier");
573 out:
574 kfree(buf);
575 return ret;
576 }
577
devlink_remove_symlinks(struct device * dev,struct class_interface * class_intf)578 static void devlink_remove_symlinks(struct device *dev,
579 struct class_interface *class_intf)
580 {
581 struct device_link *link = to_devlink(dev);
582 size_t len;
583 struct device *sup = link->supplier;
584 struct device *con = link->consumer;
585 char *buf;
586
587 sysfs_remove_link(&link->link_dev.kobj, "consumer");
588 sysfs_remove_link(&link->link_dev.kobj, "supplier");
589
590 len = max(strlen(dev_bus_name(sup)) + strlen(dev_name(sup)),
591 strlen(dev_bus_name(con)) + strlen(dev_name(con)));
592 len += strlen(":");
593 len += strlen("supplier:") + 1;
594 buf = kzalloc(len, GFP_KERNEL);
595 if (!buf) {
596 WARN(1, "Unable to properly free device link symlinks!\n");
597 return;
598 }
599
600 if (device_is_registered(con)) {
601 snprintf(buf, len, "supplier:%s:%s", dev_bus_name(sup), dev_name(sup));
602 sysfs_remove_link(&con->kobj, buf);
603 }
604 snprintf(buf, len, "consumer:%s:%s", dev_bus_name(con), dev_name(con));
605 sysfs_remove_link(&sup->kobj, buf);
606 kfree(buf);
607 }
608
609 static struct class_interface devlink_class_intf = {
610 .class = &devlink_class,
611 .add_dev = devlink_add_symlinks,
612 .remove_dev = devlink_remove_symlinks,
613 };
614
devlink_class_init(void)615 static int __init devlink_class_init(void)
616 {
617 int ret;
618
619 ret = class_register(&devlink_class);
620 if (ret)
621 return ret;
622
623 ret = class_interface_register(&devlink_class_intf);
624 if (ret)
625 class_unregister(&devlink_class);
626
627 return ret;
628 }
629 postcore_initcall(devlink_class_init);
630
631 #define DL_MANAGED_LINK_FLAGS (DL_FLAG_AUTOREMOVE_CONSUMER | \
632 DL_FLAG_AUTOREMOVE_SUPPLIER | \
633 DL_FLAG_AUTOPROBE_CONSUMER | \
634 DL_FLAG_SYNC_STATE_ONLY | \
635 DL_FLAG_INFERRED)
636
637 #define DL_ADD_VALID_FLAGS (DL_MANAGED_LINK_FLAGS | DL_FLAG_STATELESS | \
638 DL_FLAG_PM_RUNTIME | DL_FLAG_RPM_ACTIVE)
639
640 /**
641 * device_link_add - Create a link between two devices.
642 * @consumer: Consumer end of the link.
643 * @supplier: Supplier end of the link.
644 * @flags: Link flags.
645 *
646 * The caller is responsible for the proper synchronization of the link creation
647 * with runtime PM. First, setting the DL_FLAG_PM_RUNTIME flag will cause the
648 * runtime PM framework to take the link into account. Second, if the
649 * DL_FLAG_RPM_ACTIVE flag is set in addition to it, the supplier devices will
650 * be forced into the active meta state and reference-counted upon the creation
651 * of the link. If DL_FLAG_PM_RUNTIME is not set, DL_FLAG_RPM_ACTIVE will be
652 * ignored.
653 *
654 * If DL_FLAG_STATELESS is set in @flags, the caller of this function is
655 * expected to release the link returned by it directly with the help of either
656 * device_link_del() or device_link_remove().
657 *
658 * If that flag is not set, however, the caller of this function is handing the
659 * management of the link over to the driver core entirely and its return value
660 * can only be used to check whether or not the link is present. In that case,
661 * the DL_FLAG_AUTOREMOVE_CONSUMER and DL_FLAG_AUTOREMOVE_SUPPLIER device link
662 * flags can be used to indicate to the driver core when the link can be safely
663 * deleted. Namely, setting one of them in @flags indicates to the driver core
664 * that the link is not going to be used (by the given caller of this function)
665 * after unbinding the consumer or supplier driver, respectively, from its
666 * device, so the link can be deleted at that point. If none of them is set,
667 * the link will be maintained until one of the devices pointed to by it (either
668 * the consumer or the supplier) is unregistered.
669 *
670 * Also, if DL_FLAG_STATELESS, DL_FLAG_AUTOREMOVE_CONSUMER and
671 * DL_FLAG_AUTOREMOVE_SUPPLIER are not set in @flags (that is, a persistent
672 * managed device link is being added), the DL_FLAG_AUTOPROBE_CONSUMER flag can
673 * be used to request the driver core to automatically probe for a consumer
674 * driver after successfully binding a driver to the supplier device.
675 *
676 * The combination of DL_FLAG_STATELESS and one of DL_FLAG_AUTOREMOVE_CONSUMER,
677 * DL_FLAG_AUTOREMOVE_SUPPLIER, or DL_FLAG_AUTOPROBE_CONSUMER set in @flags at
678 * the same time is invalid and will cause NULL to be returned upfront.
679 * However, if a device link between the given @consumer and @supplier pair
680 * exists already when this function is called for them, the existing link will
681 * be returned regardless of its current type and status (the link's flags may
682 * be modified then). The caller of this function is then expected to treat
683 * the link as though it has just been created, so (in particular) if
684 * DL_FLAG_STATELESS was passed in @flags, the link needs to be released
685 * explicitly when not needed any more (as stated above).
686 *
687 * A side effect of the link creation is re-ordering of dpm_list and the
688 * devices_kset list by moving the consumer device and all devices depending
689 * on it to the ends of these lists (that does not happen to devices that have
690 * not been registered when this function is called).
691 *
692 * The supplier device is required to be registered when this function is called
693 * and NULL will be returned if that is not the case. The consumer device need
694 * not be registered, however.
695 */
device_link_add(struct device * consumer,struct device * supplier,u32 flags)696 struct device_link *device_link_add(struct device *consumer,
697 struct device *supplier, u32 flags)
698 {
699 struct device_link *link;
700
701 if (!consumer || !supplier || consumer == supplier ||
702 flags & ~DL_ADD_VALID_FLAGS ||
703 (flags & DL_FLAG_STATELESS && flags & DL_MANAGED_LINK_FLAGS) ||
704 (flags & DL_FLAG_SYNC_STATE_ONLY &&
705 (flags & ~DL_FLAG_INFERRED) != DL_FLAG_SYNC_STATE_ONLY) ||
706 (flags & DL_FLAG_AUTOPROBE_CONSUMER &&
707 flags & (DL_FLAG_AUTOREMOVE_CONSUMER |
708 DL_FLAG_AUTOREMOVE_SUPPLIER)))
709 return NULL;
710
711 if (flags & DL_FLAG_PM_RUNTIME && flags & DL_FLAG_RPM_ACTIVE) {
712 if (pm_runtime_get_sync(supplier) < 0) {
713 pm_runtime_put_noidle(supplier);
714 return NULL;
715 }
716 }
717
718 if (!(flags & DL_FLAG_STATELESS))
719 flags |= DL_FLAG_MANAGED;
720
721 device_links_write_lock();
722 device_pm_lock();
723
724 /*
725 * If the supplier has not been fully registered yet or there is a
726 * reverse (non-SYNC_STATE_ONLY) dependency between the consumer and
727 * the supplier already in the graph, return NULL. If the link is a
728 * SYNC_STATE_ONLY link, we don't check for reverse dependencies
729 * because it only affects sync_state() callbacks.
730 */
731 if (!device_pm_initialized(supplier)
732 || (!(flags & DL_FLAG_SYNC_STATE_ONLY) &&
733 device_is_dependent(consumer, supplier))) {
734 link = NULL;
735 goto out;
736 }
737
738 /*
739 * SYNC_STATE_ONLY links are useless once a consumer device has probed.
740 * So, only create it if the consumer hasn't probed yet.
741 */
742 if (flags & DL_FLAG_SYNC_STATE_ONLY &&
743 consumer->links.status != DL_DEV_NO_DRIVER &&
744 consumer->links.status != DL_DEV_PROBING) {
745 link = NULL;
746 goto out;
747 }
748
749 /*
750 * DL_FLAG_AUTOREMOVE_SUPPLIER indicates that the link will be needed
751 * longer than for DL_FLAG_AUTOREMOVE_CONSUMER and setting them both
752 * together doesn't make sense, so prefer DL_FLAG_AUTOREMOVE_SUPPLIER.
753 */
754 if (flags & DL_FLAG_AUTOREMOVE_SUPPLIER)
755 flags &= ~DL_FLAG_AUTOREMOVE_CONSUMER;
756
757 list_for_each_entry(link, &supplier->links.consumers, s_node) {
758 if (link->consumer != consumer)
759 continue;
760
761 if (link->flags & DL_FLAG_INFERRED &&
762 !(flags & DL_FLAG_INFERRED))
763 link->flags &= ~DL_FLAG_INFERRED;
764
765 if (flags & DL_FLAG_PM_RUNTIME) {
766 if (!(link->flags & DL_FLAG_PM_RUNTIME)) {
767 pm_runtime_new_link(consumer);
768 link->flags |= DL_FLAG_PM_RUNTIME;
769 }
770 if (flags & DL_FLAG_RPM_ACTIVE)
771 refcount_inc(&link->rpm_active);
772 }
773
774 if (flags & DL_FLAG_STATELESS) {
775 kref_get(&link->kref);
776 if (link->flags & DL_FLAG_SYNC_STATE_ONLY &&
777 !(link->flags & DL_FLAG_STATELESS)) {
778 link->flags |= DL_FLAG_STATELESS;
779 goto reorder;
780 } else {
781 link->flags |= DL_FLAG_STATELESS;
782 goto out;
783 }
784 }
785
786 /*
787 * If the life time of the link following from the new flags is
788 * longer than indicated by the flags of the existing link,
789 * update the existing link to stay around longer.
790 */
791 if (flags & DL_FLAG_AUTOREMOVE_SUPPLIER) {
792 if (link->flags & DL_FLAG_AUTOREMOVE_CONSUMER) {
793 link->flags &= ~DL_FLAG_AUTOREMOVE_CONSUMER;
794 link->flags |= DL_FLAG_AUTOREMOVE_SUPPLIER;
795 }
796 } else if (!(flags & DL_FLAG_AUTOREMOVE_CONSUMER)) {
797 link->flags &= ~(DL_FLAG_AUTOREMOVE_CONSUMER |
798 DL_FLAG_AUTOREMOVE_SUPPLIER);
799 }
800 if (!(link->flags & DL_FLAG_MANAGED)) {
801 kref_get(&link->kref);
802 link->flags |= DL_FLAG_MANAGED;
803 device_link_init_status(link, consumer, supplier);
804 }
805 if (link->flags & DL_FLAG_SYNC_STATE_ONLY &&
806 !(flags & DL_FLAG_SYNC_STATE_ONLY)) {
807 link->flags &= ~DL_FLAG_SYNC_STATE_ONLY;
808 goto reorder;
809 }
810
811 goto out;
812 }
813
814 link = kzalloc(sizeof(*link), GFP_KERNEL);
815 if (!link)
816 goto out;
817
818 refcount_set(&link->rpm_active, 1);
819
820 get_device(supplier);
821 link->supplier = supplier;
822 INIT_LIST_HEAD(&link->s_node);
823 get_device(consumer);
824 link->consumer = consumer;
825 INIT_LIST_HEAD(&link->c_node);
826 link->flags = flags;
827 kref_init(&link->kref);
828
829 link->link_dev.class = &devlink_class;
830 device_set_pm_not_required(&link->link_dev);
831 dev_set_name(&link->link_dev, "%s:%s--%s:%s",
832 dev_bus_name(supplier), dev_name(supplier),
833 dev_bus_name(consumer), dev_name(consumer));
834 if (device_register(&link->link_dev)) {
835 put_device(&link->link_dev);
836 link = NULL;
837 goto out;
838 }
839
840 if (flags & DL_FLAG_PM_RUNTIME) {
841 if (flags & DL_FLAG_RPM_ACTIVE)
842 refcount_inc(&link->rpm_active);
843
844 pm_runtime_new_link(consumer);
845 }
846
847 /* Determine the initial link state. */
848 if (flags & DL_FLAG_STATELESS)
849 link->status = DL_STATE_NONE;
850 else
851 device_link_init_status(link, consumer, supplier);
852
853 /*
854 * Some callers expect the link creation during consumer driver probe to
855 * resume the supplier even without DL_FLAG_RPM_ACTIVE.
856 */
857 if (link->status == DL_STATE_CONSUMER_PROBE &&
858 flags & DL_FLAG_PM_RUNTIME)
859 pm_runtime_resume(supplier);
860
861 list_add_tail_rcu(&link->s_node, &supplier->links.consumers);
862 list_add_tail_rcu(&link->c_node, &consumer->links.suppliers);
863
864 if (flags & DL_FLAG_SYNC_STATE_ONLY) {
865 dev_dbg(consumer,
866 "Linked as a sync state only consumer to %s\n",
867 dev_name(supplier));
868 goto out;
869 }
870
871 reorder:
872 /*
873 * Move the consumer and all of the devices depending on it to the end
874 * of dpm_list and the devices_kset list.
875 *
876 * It is necessary to hold dpm_list locked throughout all that or else
877 * we may end up suspending with a wrong ordering of it.
878 */
879 device_reorder_to_tail(consumer, NULL);
880
881 dev_dbg(consumer, "Linked as a consumer to %s\n", dev_name(supplier));
882
883 out:
884 device_pm_unlock();
885 device_links_write_unlock();
886
887 if ((flags & DL_FLAG_PM_RUNTIME && flags & DL_FLAG_RPM_ACTIVE) && !link)
888 pm_runtime_put(supplier);
889
890 return link;
891 }
892 EXPORT_SYMBOL_GPL(device_link_add);
893
__device_link_del(struct kref * kref)894 static void __device_link_del(struct kref *kref)
895 {
896 struct device_link *link = container_of(kref, struct device_link, kref);
897
898 dev_dbg(link->consumer, "Dropping the link to %s\n",
899 dev_name(link->supplier));
900
901 pm_runtime_drop_link(link);
902
903 device_link_remove_from_lists(link);
904 device_unregister(&link->link_dev);
905 }
906
device_link_put_kref(struct device_link * link)907 static void device_link_put_kref(struct device_link *link)
908 {
909 if (link->flags & DL_FLAG_STATELESS)
910 kref_put(&link->kref, __device_link_del);
911 else if (!device_is_registered(link->consumer))
912 __device_link_del(&link->kref);
913 else
914 WARN(1, "Unable to drop a managed device link reference\n");
915 }
916
917 /**
918 * device_link_del - Delete a stateless link between two devices.
919 * @link: Device link to delete.
920 *
921 * The caller must ensure proper synchronization of this function with runtime
922 * PM. If the link was added multiple times, it needs to be deleted as often.
923 * Care is required for hotplugged devices: Their links are purged on removal
924 * and calling device_link_del() is then no longer allowed.
925 */
device_link_del(struct device_link * link)926 void device_link_del(struct device_link *link)
927 {
928 device_links_write_lock();
929 device_link_put_kref(link);
930 device_links_write_unlock();
931 }
932 EXPORT_SYMBOL_GPL(device_link_del);
933
934 /**
935 * device_link_remove - Delete a stateless link between two devices.
936 * @consumer: Consumer end of the link.
937 * @supplier: Supplier end of the link.
938 *
939 * The caller must ensure proper synchronization of this function with runtime
940 * PM.
941 */
device_link_remove(void * consumer,struct device * supplier)942 void device_link_remove(void *consumer, struct device *supplier)
943 {
944 struct device_link *link;
945
946 if (WARN_ON(consumer == supplier))
947 return;
948
949 device_links_write_lock();
950
951 list_for_each_entry(link, &supplier->links.consumers, s_node) {
952 if (link->consumer == consumer) {
953 device_link_put_kref(link);
954 break;
955 }
956 }
957
958 device_links_write_unlock();
959 }
960 EXPORT_SYMBOL_GPL(device_link_remove);
961
device_links_missing_supplier(struct device * dev)962 static void device_links_missing_supplier(struct device *dev)
963 {
964 struct device_link *link;
965
966 list_for_each_entry(link, &dev->links.suppliers, c_node) {
967 if (link->status != DL_STATE_CONSUMER_PROBE)
968 continue;
969
970 if (link->supplier->links.status == DL_DEV_DRIVER_BOUND) {
971 WRITE_ONCE(link->status, DL_STATE_AVAILABLE);
972 } else {
973 WARN_ON(!(link->flags & DL_FLAG_SYNC_STATE_ONLY));
974 WRITE_ONCE(link->status, DL_STATE_DORMANT);
975 }
976 }
977 }
978
979 /**
980 * device_links_check_suppliers - Check presence of supplier drivers.
981 * @dev: Consumer device.
982 *
983 * Check links from this device to any suppliers. Walk the list of the device's
984 * links to suppliers and see if all of them are available. If not, simply
985 * return -EPROBE_DEFER.
986 *
987 * We need to guarantee that the supplier will not go away after the check has
988 * been positive here. It only can go away in __device_release_driver() and
989 * that function checks the device's links to consumers. This means we need to
990 * mark the link as "consumer probe in progress" to make the supplier removal
991 * wait for us to complete (or bad things may happen).
992 *
993 * Links without the DL_FLAG_MANAGED flag set are ignored.
994 */
device_links_check_suppliers(struct device * dev)995 int device_links_check_suppliers(struct device *dev)
996 {
997 struct device_link *link;
998 int ret = 0;
999 struct fwnode_handle *sup_fw;
1000
1001 /*
1002 * Device waiting for supplier to become available is not allowed to
1003 * probe.
1004 */
1005 mutex_lock(&fwnode_link_lock);
1006 if (dev->fwnode && !list_empty(&dev->fwnode->suppliers) &&
1007 !fw_devlink_is_permissive()) {
1008 sup_fw = list_first_entry(&dev->fwnode->suppliers,
1009 struct fwnode_link,
1010 c_hook)->supplier;
1011 dev_err_probe(dev, -EPROBE_DEFER, "wait for supplier %pfwP\n",
1012 sup_fw);
1013 mutex_unlock(&fwnode_link_lock);
1014 return -EPROBE_DEFER;
1015 }
1016 mutex_unlock(&fwnode_link_lock);
1017
1018 device_links_write_lock();
1019
1020 list_for_each_entry(link, &dev->links.suppliers, c_node) {
1021 if (!(link->flags & DL_FLAG_MANAGED))
1022 continue;
1023
1024 if (link->status != DL_STATE_AVAILABLE &&
1025 !(link->flags & DL_FLAG_SYNC_STATE_ONLY)) {
1026 device_links_missing_supplier(dev);
1027 dev_err_probe(dev, -EPROBE_DEFER,
1028 "supplier %s not ready\n",
1029 dev_name(link->supplier));
1030 ret = -EPROBE_DEFER;
1031 break;
1032 }
1033 WRITE_ONCE(link->status, DL_STATE_CONSUMER_PROBE);
1034 }
1035 dev->links.status = DL_DEV_PROBING;
1036
1037 device_links_write_unlock();
1038 return ret;
1039 }
1040
1041 /**
1042 * __device_links_queue_sync_state - Queue a device for sync_state() callback
1043 * @dev: Device to call sync_state() on
1044 * @list: List head to queue the @dev on
1045 *
1046 * Queues a device for a sync_state() callback when the device links write lock
1047 * isn't held. This allows the sync_state() execution flow to use device links
1048 * APIs. The caller must ensure this function is called with
1049 * device_links_write_lock() held.
1050 *
1051 * This function does a get_device() to make sure the device is not freed while
1052 * on this list.
1053 *
1054 * So the caller must also ensure that device_links_flush_sync_list() is called
1055 * as soon as the caller releases device_links_write_lock(). This is necessary
1056 * to make sure the sync_state() is called in a timely fashion and the
1057 * put_device() is called on this device.
1058 */
__device_links_queue_sync_state(struct device * dev,struct list_head * list)1059 static void __device_links_queue_sync_state(struct device *dev,
1060 struct list_head *list)
1061 {
1062 struct device_link *link;
1063
1064 if (!dev_has_sync_state(dev))
1065 return;
1066 if (dev->state_synced)
1067 return;
1068
1069 list_for_each_entry(link, &dev->links.consumers, s_node) {
1070 if (!(link->flags & DL_FLAG_MANAGED))
1071 continue;
1072 if (link->status != DL_STATE_ACTIVE)
1073 return;
1074 }
1075
1076 /*
1077 * Set the flag here to avoid adding the same device to a list more
1078 * than once. This can happen if new consumers get added to the device
1079 * and probed before the list is flushed.
1080 */
1081 dev->state_synced = true;
1082
1083 if (WARN_ON(!list_empty(&dev->links.defer_sync)))
1084 return;
1085
1086 get_device(dev);
1087 list_add_tail(&dev->links.defer_sync, list);
1088 }
1089
1090 /**
1091 * device_links_flush_sync_list - Call sync_state() on a list of devices
1092 * @list: List of devices to call sync_state() on
1093 * @dont_lock_dev: Device for which lock is already held by the caller
1094 *
1095 * Calls sync_state() on all the devices that have been queued for it. This
1096 * function is used in conjunction with __device_links_queue_sync_state(). The
1097 * @dont_lock_dev parameter is useful when this function is called from a
1098 * context where a device lock is already held.
1099 */
device_links_flush_sync_list(struct list_head * list,struct device * dont_lock_dev)1100 static void device_links_flush_sync_list(struct list_head *list,
1101 struct device *dont_lock_dev)
1102 {
1103 struct device *dev, *tmp;
1104
1105 list_for_each_entry_safe(dev, tmp, list, links.defer_sync) {
1106 list_del_init(&dev->links.defer_sync);
1107
1108 if (dev != dont_lock_dev)
1109 device_lock(dev);
1110
1111 if (dev->bus->sync_state)
1112 dev->bus->sync_state(dev);
1113 else if (dev->driver && dev->driver->sync_state)
1114 dev->driver->sync_state(dev);
1115
1116 if (dev != dont_lock_dev)
1117 device_unlock(dev);
1118
1119 put_device(dev);
1120 }
1121 }
1122
device_links_supplier_sync_state_pause(void)1123 void device_links_supplier_sync_state_pause(void)
1124 {
1125 device_links_write_lock();
1126 defer_sync_state_count++;
1127 device_links_write_unlock();
1128 }
1129
device_links_supplier_sync_state_resume(void)1130 void device_links_supplier_sync_state_resume(void)
1131 {
1132 struct device *dev, *tmp;
1133 LIST_HEAD(sync_list);
1134
1135 device_links_write_lock();
1136 if (!defer_sync_state_count) {
1137 WARN(true, "Unmatched sync_state pause/resume!");
1138 goto out;
1139 }
1140 defer_sync_state_count--;
1141 if (defer_sync_state_count)
1142 goto out;
1143
1144 list_for_each_entry_safe(dev, tmp, &deferred_sync, links.defer_sync) {
1145 /*
1146 * Delete from deferred_sync list before queuing it to
1147 * sync_list because defer_sync is used for both lists.
1148 */
1149 list_del_init(&dev->links.defer_sync);
1150 __device_links_queue_sync_state(dev, &sync_list);
1151 }
1152 out:
1153 device_links_write_unlock();
1154
1155 device_links_flush_sync_list(&sync_list, NULL);
1156 }
1157
sync_state_resume_initcall(void)1158 static int sync_state_resume_initcall(void)
1159 {
1160 device_links_supplier_sync_state_resume();
1161 return 0;
1162 }
1163 late_initcall(sync_state_resume_initcall);
1164
__device_links_supplier_defer_sync(struct device * sup)1165 static void __device_links_supplier_defer_sync(struct device *sup)
1166 {
1167 if (list_empty(&sup->links.defer_sync) && dev_has_sync_state(sup))
1168 list_add_tail(&sup->links.defer_sync, &deferred_sync);
1169 }
1170
device_link_drop_managed(struct device_link * link)1171 static void device_link_drop_managed(struct device_link *link)
1172 {
1173 link->flags &= ~DL_FLAG_MANAGED;
1174 WRITE_ONCE(link->status, DL_STATE_NONE);
1175 kref_put(&link->kref, __device_link_del);
1176 }
1177
waiting_for_supplier_show(struct device * dev,struct device_attribute * attr,char * buf)1178 static ssize_t waiting_for_supplier_show(struct device *dev,
1179 struct device_attribute *attr,
1180 char *buf)
1181 {
1182 bool val;
1183
1184 device_lock(dev);
1185 val = !list_empty(&dev->fwnode->suppliers);
1186 device_unlock(dev);
1187 return sysfs_emit(buf, "%u\n", val);
1188 }
1189 static DEVICE_ATTR_RO(waiting_for_supplier);
1190
1191 /**
1192 * device_links_force_bind - Prepares device to be force bound
1193 * @dev: Consumer device.
1194 *
1195 * device_bind_driver() force binds a device to a driver without calling any
1196 * driver probe functions. So the consumer really isn't going to wait for any
1197 * supplier before it's bound to the driver. We still want the device link
1198 * states to be sensible when this happens.
1199 *
1200 * In preparation for device_bind_driver(), this function goes through each
1201 * supplier device links and checks if the supplier is bound. If it is, then
1202 * the device link status is set to CONSUMER_PROBE. Otherwise, the device link
1203 * is dropped. Links without the DL_FLAG_MANAGED flag set are ignored.
1204 */
device_links_force_bind(struct device * dev)1205 void device_links_force_bind(struct device *dev)
1206 {
1207 struct device_link *link, *ln;
1208
1209 device_links_write_lock();
1210
1211 list_for_each_entry_safe(link, ln, &dev->links.suppliers, c_node) {
1212 if (!(link->flags & DL_FLAG_MANAGED))
1213 continue;
1214
1215 if (link->status != DL_STATE_AVAILABLE) {
1216 device_link_drop_managed(link);
1217 continue;
1218 }
1219 WRITE_ONCE(link->status, DL_STATE_CONSUMER_PROBE);
1220 }
1221 dev->links.status = DL_DEV_PROBING;
1222
1223 device_links_write_unlock();
1224 }
1225
1226 /**
1227 * device_links_driver_bound - Update device links after probing its driver.
1228 * @dev: Device to update the links for.
1229 *
1230 * The probe has been successful, so update links from this device to any
1231 * consumers by changing their status to "available".
1232 *
1233 * Also change the status of @dev's links to suppliers to "active".
1234 *
1235 * Links without the DL_FLAG_MANAGED flag set are ignored.
1236 */
device_links_driver_bound(struct device * dev)1237 void device_links_driver_bound(struct device *dev)
1238 {
1239 struct device_link *link, *ln;
1240 LIST_HEAD(sync_list);
1241
1242 /*
1243 * If a device binds successfully, it's expected to have created all
1244 * the device links it needs to or make new device links as it needs
1245 * them. So, fw_devlink no longer needs to create device links to any
1246 * of the device's suppliers.
1247 *
1248 * Also, if a child firmware node of this bound device is not added as
1249 * a device by now, assume it is never going to be added and make sure
1250 * other devices don't defer probe indefinitely by waiting for such a
1251 * child device.
1252 */
1253 if (dev->fwnode && dev->fwnode->dev == dev) {
1254 struct fwnode_handle *child;
1255 fwnode_links_purge_suppliers(dev->fwnode);
1256 fwnode_for_each_available_child_node(dev->fwnode, child)
1257 fw_devlink_purge_absent_suppliers(child);
1258 }
1259 device_remove_file(dev, &dev_attr_waiting_for_supplier);
1260
1261 device_links_write_lock();
1262
1263 list_for_each_entry(link, &dev->links.consumers, s_node) {
1264 if (!(link->flags & DL_FLAG_MANAGED))
1265 continue;
1266
1267 /*
1268 * Links created during consumer probe may be in the "consumer
1269 * probe" state to start with if the supplier is still probing
1270 * when they are created and they may become "active" if the
1271 * consumer probe returns first. Skip them here.
1272 */
1273 if (link->status == DL_STATE_CONSUMER_PROBE ||
1274 link->status == DL_STATE_ACTIVE)
1275 continue;
1276
1277 WARN_ON(link->status != DL_STATE_DORMANT);
1278 WRITE_ONCE(link->status, DL_STATE_AVAILABLE);
1279
1280 if (link->flags & DL_FLAG_AUTOPROBE_CONSUMER)
1281 driver_deferred_probe_add(link->consumer);
1282 }
1283
1284 if (defer_sync_state_count)
1285 __device_links_supplier_defer_sync(dev);
1286 else
1287 __device_links_queue_sync_state(dev, &sync_list);
1288
1289 list_for_each_entry_safe(link, ln, &dev->links.suppliers, c_node) {
1290 struct device *supplier;
1291
1292 if (!(link->flags & DL_FLAG_MANAGED))
1293 continue;
1294
1295 supplier = link->supplier;
1296 if (link->flags & DL_FLAG_SYNC_STATE_ONLY) {
1297 /*
1298 * When DL_FLAG_SYNC_STATE_ONLY is set, it means no
1299 * other DL_MANAGED_LINK_FLAGS have been set. So, it's
1300 * save to drop the managed link completely.
1301 */
1302 device_link_drop_managed(link);
1303 } else {
1304 WARN_ON(link->status != DL_STATE_CONSUMER_PROBE);
1305 WRITE_ONCE(link->status, DL_STATE_ACTIVE);
1306 }
1307
1308 /*
1309 * This needs to be done even for the deleted
1310 * DL_FLAG_SYNC_STATE_ONLY device link in case it was the last
1311 * device link that was preventing the supplier from getting a
1312 * sync_state() call.
1313 */
1314 if (defer_sync_state_count)
1315 __device_links_supplier_defer_sync(supplier);
1316 else
1317 __device_links_queue_sync_state(supplier, &sync_list);
1318 }
1319
1320 dev->links.status = DL_DEV_DRIVER_BOUND;
1321
1322 device_links_write_unlock();
1323
1324 device_links_flush_sync_list(&sync_list, dev);
1325 }
1326
1327 /**
1328 * __device_links_no_driver - Update links of a device without a driver.
1329 * @dev: Device without a drvier.
1330 *
1331 * Delete all non-persistent links from this device to any suppliers.
1332 *
1333 * Persistent links stay around, but their status is changed to "available",
1334 * unless they already are in the "supplier unbind in progress" state in which
1335 * case they need not be updated.
1336 *
1337 * Links without the DL_FLAG_MANAGED flag set are ignored.
1338 */
__device_links_no_driver(struct device * dev)1339 static void __device_links_no_driver(struct device *dev)
1340 {
1341 struct device_link *link, *ln;
1342
1343 list_for_each_entry_safe_reverse(link, ln, &dev->links.suppliers, c_node) {
1344 if (!(link->flags & DL_FLAG_MANAGED))
1345 continue;
1346
1347 if (link->flags & DL_FLAG_AUTOREMOVE_CONSUMER) {
1348 device_link_drop_managed(link);
1349 continue;
1350 }
1351
1352 if (link->status != DL_STATE_CONSUMER_PROBE &&
1353 link->status != DL_STATE_ACTIVE)
1354 continue;
1355
1356 if (link->supplier->links.status == DL_DEV_DRIVER_BOUND) {
1357 WRITE_ONCE(link->status, DL_STATE_AVAILABLE);
1358 } else {
1359 WARN_ON(!(link->flags & DL_FLAG_SYNC_STATE_ONLY));
1360 WRITE_ONCE(link->status, DL_STATE_DORMANT);
1361 }
1362 }
1363
1364 dev->links.status = DL_DEV_NO_DRIVER;
1365 }
1366
1367 /**
1368 * device_links_no_driver - Update links after failing driver probe.
1369 * @dev: Device whose driver has just failed to probe.
1370 *
1371 * Clean up leftover links to consumers for @dev and invoke
1372 * %__device_links_no_driver() to update links to suppliers for it as
1373 * appropriate.
1374 *
1375 * Links without the DL_FLAG_MANAGED flag set are ignored.
1376 */
device_links_no_driver(struct device * dev)1377 void device_links_no_driver(struct device *dev)
1378 {
1379 struct device_link *link;
1380
1381 device_links_write_lock();
1382
1383 list_for_each_entry(link, &dev->links.consumers, s_node) {
1384 if (!(link->flags & DL_FLAG_MANAGED))
1385 continue;
1386
1387 /*
1388 * The probe has failed, so if the status of the link is
1389 * "consumer probe" or "active", it must have been added by
1390 * a probing consumer while this device was still probing.
1391 * Change its state to "dormant", as it represents a valid
1392 * relationship, but it is not functionally meaningful.
1393 */
1394 if (link->status == DL_STATE_CONSUMER_PROBE ||
1395 link->status == DL_STATE_ACTIVE)
1396 WRITE_ONCE(link->status, DL_STATE_DORMANT);
1397 }
1398
1399 __device_links_no_driver(dev);
1400
1401 device_links_write_unlock();
1402 }
1403
1404 /**
1405 * device_links_driver_cleanup - Update links after driver removal.
1406 * @dev: Device whose driver has just gone away.
1407 *
1408 * Update links to consumers for @dev by changing their status to "dormant" and
1409 * invoke %__device_links_no_driver() to update links to suppliers for it as
1410 * appropriate.
1411 *
1412 * Links without the DL_FLAG_MANAGED flag set are ignored.
1413 */
device_links_driver_cleanup(struct device * dev)1414 void device_links_driver_cleanup(struct device *dev)
1415 {
1416 struct device_link *link, *ln;
1417
1418 device_links_write_lock();
1419
1420 list_for_each_entry_safe(link, ln, &dev->links.consumers, s_node) {
1421 if (!(link->flags & DL_FLAG_MANAGED))
1422 continue;
1423
1424 WARN_ON(link->flags & DL_FLAG_AUTOREMOVE_CONSUMER);
1425 WARN_ON(link->status != DL_STATE_SUPPLIER_UNBIND);
1426
1427 /*
1428 * autoremove the links between this @dev and its consumer
1429 * devices that are not active, i.e. where the link state
1430 * has moved to DL_STATE_SUPPLIER_UNBIND.
1431 */
1432 if (link->status == DL_STATE_SUPPLIER_UNBIND &&
1433 link->flags & DL_FLAG_AUTOREMOVE_SUPPLIER)
1434 device_link_drop_managed(link);
1435
1436 WRITE_ONCE(link->status, DL_STATE_DORMANT);
1437 }
1438
1439 list_del_init(&dev->links.defer_sync);
1440 __device_links_no_driver(dev);
1441
1442 device_links_write_unlock();
1443 }
1444
1445 /**
1446 * device_links_busy - Check if there are any busy links to consumers.
1447 * @dev: Device to check.
1448 *
1449 * Check each consumer of the device and return 'true' if its link's status
1450 * is one of "consumer probe" or "active" (meaning that the given consumer is
1451 * probing right now or its driver is present). Otherwise, change the link
1452 * state to "supplier unbind" to prevent the consumer from being probed
1453 * successfully going forward.
1454 *
1455 * Return 'false' if there are no probing or active consumers.
1456 *
1457 * Links without the DL_FLAG_MANAGED flag set are ignored.
1458 */
device_links_busy(struct device * dev)1459 bool device_links_busy(struct device *dev)
1460 {
1461 struct device_link *link;
1462 bool ret = false;
1463
1464 device_links_write_lock();
1465
1466 list_for_each_entry(link, &dev->links.consumers, s_node) {
1467 if (!(link->flags & DL_FLAG_MANAGED))
1468 continue;
1469
1470 if (link->status == DL_STATE_CONSUMER_PROBE
1471 || link->status == DL_STATE_ACTIVE) {
1472 ret = true;
1473 break;
1474 }
1475 WRITE_ONCE(link->status, DL_STATE_SUPPLIER_UNBIND);
1476 }
1477
1478 dev->links.status = DL_DEV_UNBINDING;
1479
1480 device_links_write_unlock();
1481 return ret;
1482 }
1483
1484 /**
1485 * device_links_unbind_consumers - Force unbind consumers of the given device.
1486 * @dev: Device to unbind the consumers of.
1487 *
1488 * Walk the list of links to consumers for @dev and if any of them is in the
1489 * "consumer probe" state, wait for all device probes in progress to complete
1490 * and start over.
1491 *
1492 * If that's not the case, change the status of the link to "supplier unbind"
1493 * and check if the link was in the "active" state. If so, force the consumer
1494 * driver to unbind and start over (the consumer will not re-probe as we have
1495 * changed the state of the link already).
1496 *
1497 * Links without the DL_FLAG_MANAGED flag set are ignored.
1498 */
device_links_unbind_consumers(struct device * dev)1499 void device_links_unbind_consumers(struct device *dev)
1500 {
1501 struct device_link *link;
1502
1503 start:
1504 device_links_write_lock();
1505
1506 list_for_each_entry(link, &dev->links.consumers, s_node) {
1507 enum device_link_state status;
1508
1509 if (!(link->flags & DL_FLAG_MANAGED) ||
1510 link->flags & DL_FLAG_SYNC_STATE_ONLY)
1511 continue;
1512
1513 status = link->status;
1514 if (status == DL_STATE_CONSUMER_PROBE) {
1515 device_links_write_unlock();
1516
1517 wait_for_device_probe();
1518 goto start;
1519 }
1520 WRITE_ONCE(link->status, DL_STATE_SUPPLIER_UNBIND);
1521 if (status == DL_STATE_ACTIVE) {
1522 struct device *consumer = link->consumer;
1523
1524 get_device(consumer);
1525
1526 device_links_write_unlock();
1527
1528 device_release_driver_internal(consumer, NULL,
1529 consumer->parent);
1530 put_device(consumer);
1531 goto start;
1532 }
1533 }
1534
1535 device_links_write_unlock();
1536 }
1537
1538 /**
1539 * device_links_purge - Delete existing links to other devices.
1540 * @dev: Target device.
1541 */
device_links_purge(struct device * dev)1542 static void device_links_purge(struct device *dev)
1543 {
1544 struct device_link *link, *ln;
1545
1546 if (dev->class == &devlink_class)
1547 return;
1548
1549 /*
1550 * Delete all of the remaining links from this device to any other
1551 * devices (either consumers or suppliers).
1552 */
1553 device_links_write_lock();
1554
1555 list_for_each_entry_safe_reverse(link, ln, &dev->links.suppliers, c_node) {
1556 WARN_ON(link->status == DL_STATE_ACTIVE);
1557 __device_link_del(&link->kref);
1558 }
1559
1560 list_for_each_entry_safe_reverse(link, ln, &dev->links.consumers, s_node) {
1561 WARN_ON(link->status != DL_STATE_DORMANT &&
1562 link->status != DL_STATE_NONE);
1563 __device_link_del(&link->kref);
1564 }
1565
1566 device_links_write_unlock();
1567 }
1568
1569 #define FW_DEVLINK_FLAGS_PERMISSIVE (DL_FLAG_INFERRED | \
1570 DL_FLAG_SYNC_STATE_ONLY)
1571 #define FW_DEVLINK_FLAGS_ON (DL_FLAG_INFERRED | \
1572 DL_FLAG_AUTOPROBE_CONSUMER)
1573 #define FW_DEVLINK_FLAGS_RPM (FW_DEVLINK_FLAGS_ON | \
1574 DL_FLAG_PM_RUNTIME)
1575
1576 static u32 fw_devlink_flags = FW_DEVLINK_FLAGS_ON;
fw_devlink_setup(char * arg)1577 static int __init fw_devlink_setup(char *arg)
1578 {
1579 if (!arg)
1580 return -EINVAL;
1581
1582 if (strcmp(arg, "off") == 0) {
1583 fw_devlink_flags = 0;
1584 } else if (strcmp(arg, "permissive") == 0) {
1585 fw_devlink_flags = FW_DEVLINK_FLAGS_PERMISSIVE;
1586 } else if (strcmp(arg, "on") == 0) {
1587 fw_devlink_flags = FW_DEVLINK_FLAGS_ON;
1588 } else if (strcmp(arg, "rpm") == 0) {
1589 fw_devlink_flags = FW_DEVLINK_FLAGS_RPM;
1590 }
1591 return 0;
1592 }
1593 early_param("fw_devlink", fw_devlink_setup);
1594
1595 static bool fw_devlink_strict;
fw_devlink_strict_setup(char * arg)1596 static int __init fw_devlink_strict_setup(char *arg)
1597 {
1598 return strtobool(arg, &fw_devlink_strict);
1599 }
1600 early_param("fw_devlink.strict", fw_devlink_strict_setup);
1601
fw_devlink_get_flags(void)1602 u32 fw_devlink_get_flags(void)
1603 {
1604 return fw_devlink_flags;
1605 }
1606
fw_devlink_is_permissive(void)1607 static bool fw_devlink_is_permissive(void)
1608 {
1609 return fw_devlink_flags == FW_DEVLINK_FLAGS_PERMISSIVE;
1610 }
1611
fw_devlink_is_strict(void)1612 bool fw_devlink_is_strict(void)
1613 {
1614 return fw_devlink_strict && !fw_devlink_is_permissive();
1615 }
1616
fw_devlink_parse_fwnode(struct fwnode_handle * fwnode)1617 static void fw_devlink_parse_fwnode(struct fwnode_handle *fwnode)
1618 {
1619 if (fwnode->flags & FWNODE_FLAG_LINKS_ADDED)
1620 return;
1621
1622 fwnode_call_int_op(fwnode, add_links);
1623 fwnode->flags |= FWNODE_FLAG_LINKS_ADDED;
1624 }
1625
fw_devlink_parse_fwtree(struct fwnode_handle * fwnode)1626 static void fw_devlink_parse_fwtree(struct fwnode_handle *fwnode)
1627 {
1628 struct fwnode_handle *child = NULL;
1629
1630 fw_devlink_parse_fwnode(fwnode);
1631
1632 while ((child = fwnode_get_next_available_child_node(fwnode, child)))
1633 fw_devlink_parse_fwtree(child);
1634 }
1635
fw_devlink_relax_link(struct device_link * link)1636 static void fw_devlink_relax_link(struct device_link *link)
1637 {
1638 if (!(link->flags & DL_FLAG_INFERRED))
1639 return;
1640
1641 if (link->flags == (DL_FLAG_MANAGED | FW_DEVLINK_FLAGS_PERMISSIVE))
1642 return;
1643
1644 pm_runtime_drop_link(link);
1645 link->flags = DL_FLAG_MANAGED | FW_DEVLINK_FLAGS_PERMISSIVE;
1646 dev_dbg(link->consumer, "Relaxing link with %s\n",
1647 dev_name(link->supplier));
1648 }
1649
fw_devlink_no_driver(struct device * dev,void * data)1650 static int fw_devlink_no_driver(struct device *dev, void *data)
1651 {
1652 struct device_link *link = to_devlink(dev);
1653
1654 if (!link->supplier->can_match)
1655 fw_devlink_relax_link(link);
1656
1657 return 0;
1658 }
1659
fw_devlink_drivers_done(void)1660 void fw_devlink_drivers_done(void)
1661 {
1662 fw_devlink_drv_reg_done = true;
1663 device_links_write_lock();
1664 class_for_each_device(&devlink_class, NULL, NULL,
1665 fw_devlink_no_driver);
1666 device_links_write_unlock();
1667 }
1668
fw_devlink_unblock_consumers(struct device * dev)1669 static void fw_devlink_unblock_consumers(struct device *dev)
1670 {
1671 struct device_link *link;
1672
1673 if (!fw_devlink_flags || fw_devlink_is_permissive())
1674 return;
1675
1676 device_links_write_lock();
1677 list_for_each_entry(link, &dev->links.consumers, s_node)
1678 fw_devlink_relax_link(link);
1679 device_links_write_unlock();
1680 }
1681
1682 /**
1683 * fw_devlink_relax_cycle - Convert cyclic links to SYNC_STATE_ONLY links
1684 * @con: Device to check dependencies for.
1685 * @sup: Device to check against.
1686 *
1687 * Check if @sup depends on @con or any device dependent on it (its child or
1688 * its consumer etc). When such a cyclic dependency is found, convert all
1689 * device links created solely by fw_devlink into SYNC_STATE_ONLY device links.
1690 * This is the equivalent of doing fw_devlink=permissive just between the
1691 * devices in the cycle. We need to do this because, at this point, fw_devlink
1692 * can't tell which of these dependencies is not a real dependency.
1693 *
1694 * Return 1 if a cycle is found. Otherwise, return 0.
1695 */
fw_devlink_relax_cycle(struct device * con,void * sup)1696 static int fw_devlink_relax_cycle(struct device *con, void *sup)
1697 {
1698 struct device_link *link;
1699 int ret;
1700
1701 if (con == sup)
1702 return 1;
1703
1704 ret = device_for_each_child(con, sup, fw_devlink_relax_cycle);
1705 if (ret)
1706 return ret;
1707
1708 list_for_each_entry(link, &con->links.consumers, s_node) {
1709 if ((link->flags & ~DL_FLAG_INFERRED) ==
1710 (DL_FLAG_SYNC_STATE_ONLY | DL_FLAG_MANAGED))
1711 continue;
1712
1713 if (!fw_devlink_relax_cycle(link->consumer, sup))
1714 continue;
1715
1716 ret = 1;
1717
1718 fw_devlink_relax_link(link);
1719 }
1720 return ret;
1721 }
1722
1723 /**
1724 * fw_devlink_create_devlink - Create a device link from a consumer to fwnode
1725 * @con: consumer device for the device link
1726 * @sup_handle: fwnode handle of supplier
1727 * @flags: devlink flags
1728 *
1729 * This function will try to create a device link between the consumer device
1730 * @con and the supplier device represented by @sup_handle.
1731 *
1732 * The supplier has to be provided as a fwnode because incorrect cycles in
1733 * fwnode links can sometimes cause the supplier device to never be created.
1734 * This function detects such cases and returns an error if it cannot create a
1735 * device link from the consumer to a missing supplier.
1736 *
1737 * Returns,
1738 * 0 on successfully creating a device link
1739 * -EINVAL if the device link cannot be created as expected
1740 * -EAGAIN if the device link cannot be created right now, but it may be
1741 * possible to do that in the future
1742 */
fw_devlink_create_devlink(struct device * con,struct fwnode_handle * sup_handle,u32 flags)1743 static int fw_devlink_create_devlink(struct device *con,
1744 struct fwnode_handle *sup_handle, u32 flags)
1745 {
1746 struct device *sup_dev;
1747 int ret = 0;
1748
1749 /*
1750 * In some cases, a device P might also be a supplier to its child node
1751 * C. However, this would defer the probe of C until the probe of P
1752 * completes successfully. This is perfectly fine in the device driver
1753 * model. device_add() doesn't guarantee probe completion of the device
1754 * by the time it returns.
1755 *
1756 * However, there are a few drivers that assume C will finish probing
1757 * as soon as it's added and before P finishes probing. So, we provide
1758 * a flag to let fw_devlink know not to delay the probe of C until the
1759 * probe of P completes successfully.
1760 *
1761 * When such a flag is set, we can't create device links where P is the
1762 * supplier of C as that would delay the probe of C.
1763 */
1764 if (sup_handle->flags & FWNODE_FLAG_NEEDS_CHILD_BOUND_ON_ADD &&
1765 fwnode_is_ancestor_of(sup_handle, con->fwnode))
1766 return -EINVAL;
1767
1768 sup_dev = get_dev_from_fwnode(sup_handle);
1769 if (sup_dev) {
1770 /*
1771 * If it's one of those drivers that don't actually bind to
1772 * their device using driver core, then don't wait on this
1773 * supplier device indefinitely.
1774 */
1775 if (sup_dev->links.status == DL_DEV_NO_DRIVER &&
1776 sup_handle->flags & FWNODE_FLAG_INITIALIZED) {
1777 ret = -EINVAL;
1778 goto out;
1779 }
1780
1781 /*
1782 * If this fails, it is due to cycles in device links. Just
1783 * give up on this link and treat it as invalid.
1784 */
1785 if (!device_link_add(con, sup_dev, flags) &&
1786 !(flags & DL_FLAG_SYNC_STATE_ONLY)) {
1787 dev_info(con, "Fixing up cyclic dependency with %s\n",
1788 dev_name(sup_dev));
1789 device_links_write_lock();
1790 fw_devlink_relax_cycle(con, sup_dev);
1791 device_links_write_unlock();
1792 device_link_add(con, sup_dev,
1793 FW_DEVLINK_FLAGS_PERMISSIVE);
1794 ret = -EINVAL;
1795 }
1796
1797 goto out;
1798 }
1799
1800 /* Supplier that's already initialized without a struct device. */
1801 if (sup_handle->flags & FWNODE_FLAG_INITIALIZED)
1802 return -EINVAL;
1803
1804 /*
1805 * DL_FLAG_SYNC_STATE_ONLY doesn't block probing and supports
1806 * cycles. So cycle detection isn't necessary and shouldn't be
1807 * done.
1808 */
1809 if (flags & DL_FLAG_SYNC_STATE_ONLY)
1810 return -EAGAIN;
1811
1812 /*
1813 * If we can't find the supplier device from its fwnode, it might be
1814 * due to a cyclic dependency between fwnodes. Some of these cycles can
1815 * be broken by applying logic. Check for these types of cycles and
1816 * break them so that devices in the cycle probe properly.
1817 *
1818 * If the supplier's parent is dependent on the consumer, then the
1819 * consumer and supplier have a cyclic dependency. Since fw_devlink
1820 * can't tell which of the inferred dependencies are incorrect, don't
1821 * enforce probe ordering between any of the devices in this cyclic
1822 * dependency. Do this by relaxing all the fw_devlink device links in
1823 * this cycle and by treating the fwnode link between the consumer and
1824 * the supplier as an invalid dependency.
1825 */
1826 sup_dev = fwnode_get_next_parent_dev(sup_handle);
1827 if (sup_dev && device_is_dependent(con, sup_dev)) {
1828 dev_info(con, "Fixing up cyclic dependency with %pfwP (%s)\n",
1829 sup_handle, dev_name(sup_dev));
1830 device_links_write_lock();
1831 fw_devlink_relax_cycle(con, sup_dev);
1832 device_links_write_unlock();
1833 ret = -EINVAL;
1834 } else {
1835 /*
1836 * Can't check for cycles or no cycles. So let's try
1837 * again later.
1838 */
1839 ret = -EAGAIN;
1840 }
1841
1842 out:
1843 put_device(sup_dev);
1844 return ret;
1845 }
1846
1847 /**
1848 * __fw_devlink_link_to_consumers - Create device links to consumers of a device
1849 * @dev: Device that needs to be linked to its consumers
1850 *
1851 * This function looks at all the consumer fwnodes of @dev and creates device
1852 * links between the consumer device and @dev (supplier).
1853 *
1854 * If the consumer device has not been added yet, then this function creates a
1855 * SYNC_STATE_ONLY link between @dev (supplier) and the closest ancestor device
1856 * of the consumer fwnode. This is necessary to make sure @dev doesn't get a
1857 * sync_state() callback before the real consumer device gets to be added and
1858 * then probed.
1859 *
1860 * Once device links are created from the real consumer to @dev (supplier), the
1861 * fwnode links are deleted.
1862 */
__fw_devlink_link_to_consumers(struct device * dev)1863 static void __fw_devlink_link_to_consumers(struct device *dev)
1864 {
1865 struct fwnode_handle *fwnode = dev->fwnode;
1866 struct fwnode_link *link, *tmp;
1867
1868 list_for_each_entry_safe(link, tmp, &fwnode->consumers, s_hook) {
1869 u32 dl_flags = fw_devlink_get_flags();
1870 struct device *con_dev;
1871 bool own_link = true;
1872 int ret;
1873
1874 con_dev = get_dev_from_fwnode(link->consumer);
1875 /*
1876 * If consumer device is not available yet, make a "proxy"
1877 * SYNC_STATE_ONLY link from the consumer's parent device to
1878 * the supplier device. This is necessary to make sure the
1879 * supplier doesn't get a sync_state() callback before the real
1880 * consumer can create a device link to the supplier.
1881 *
1882 * This proxy link step is needed to handle the case where the
1883 * consumer's parent device is added before the supplier.
1884 */
1885 if (!con_dev) {
1886 con_dev = fwnode_get_next_parent_dev(link->consumer);
1887 /*
1888 * However, if the consumer's parent device is also the
1889 * parent of the supplier, don't create a
1890 * consumer-supplier link from the parent to its child
1891 * device. Such a dependency is impossible.
1892 */
1893 if (con_dev &&
1894 fwnode_is_ancestor_of(con_dev->fwnode, fwnode)) {
1895 put_device(con_dev);
1896 con_dev = NULL;
1897 } else {
1898 own_link = false;
1899 dl_flags = FW_DEVLINK_FLAGS_PERMISSIVE;
1900 }
1901 }
1902
1903 if (!con_dev)
1904 continue;
1905
1906 ret = fw_devlink_create_devlink(con_dev, fwnode, dl_flags);
1907 put_device(con_dev);
1908 if (!own_link || ret == -EAGAIN)
1909 continue;
1910
1911 __fwnode_link_del(link);
1912 }
1913 }
1914
1915 /**
1916 * __fw_devlink_link_to_suppliers - Create device links to suppliers of a device
1917 * @dev: The consumer device that needs to be linked to its suppliers
1918 * @fwnode: Root of the fwnode tree that is used to create device links
1919 *
1920 * This function looks at all the supplier fwnodes of fwnode tree rooted at
1921 * @fwnode and creates device links between @dev (consumer) and all the
1922 * supplier devices of the entire fwnode tree at @fwnode.
1923 *
1924 * The function creates normal (non-SYNC_STATE_ONLY) device links between @dev
1925 * and the real suppliers of @dev. Once these device links are created, the
1926 * fwnode links are deleted. When such device links are successfully created,
1927 * this function is called recursively on those supplier devices. This is
1928 * needed to detect and break some invalid cycles in fwnode links. See
1929 * fw_devlink_create_devlink() for more details.
1930 *
1931 * In addition, it also looks at all the suppliers of the entire fwnode tree
1932 * because some of the child devices of @dev that have not been added yet
1933 * (because @dev hasn't probed) might already have their suppliers added to
1934 * driver core. So, this function creates SYNC_STATE_ONLY device links between
1935 * @dev (consumer) and these suppliers to make sure they don't execute their
1936 * sync_state() callbacks before these child devices have a chance to create
1937 * their device links. The fwnode links that correspond to the child devices
1938 * aren't delete because they are needed later to create the device links
1939 * between the real consumer and supplier devices.
1940 */
__fw_devlink_link_to_suppliers(struct device * dev,struct fwnode_handle * fwnode)1941 static void __fw_devlink_link_to_suppliers(struct device *dev,
1942 struct fwnode_handle *fwnode)
1943 {
1944 bool own_link = (dev->fwnode == fwnode);
1945 struct fwnode_link *link, *tmp;
1946 struct fwnode_handle *child = NULL;
1947 u32 dl_flags;
1948
1949 if (own_link)
1950 dl_flags = fw_devlink_get_flags();
1951 else
1952 dl_flags = FW_DEVLINK_FLAGS_PERMISSIVE;
1953
1954 list_for_each_entry_safe(link, tmp, &fwnode->suppliers, c_hook) {
1955 int ret;
1956 struct device *sup_dev;
1957 struct fwnode_handle *sup = link->supplier;
1958
1959 ret = fw_devlink_create_devlink(dev, sup, dl_flags);
1960 if (!own_link || ret == -EAGAIN)
1961 continue;
1962
1963 __fwnode_link_del(link);
1964
1965 /* If no device link was created, nothing more to do. */
1966 if (ret)
1967 continue;
1968
1969 /*
1970 * If a device link was successfully created to a supplier, we
1971 * now need to try and link the supplier to all its suppliers.
1972 *
1973 * This is needed to detect and delete false dependencies in
1974 * fwnode links that haven't been converted to a device link
1975 * yet. See comments in fw_devlink_create_devlink() for more
1976 * details on the false dependency.
1977 *
1978 * Without deleting these false dependencies, some devices will
1979 * never probe because they'll keep waiting for their false
1980 * dependency fwnode links to be converted to device links.
1981 */
1982 sup_dev = get_dev_from_fwnode(sup);
1983 __fw_devlink_link_to_suppliers(sup_dev, sup_dev->fwnode);
1984 put_device(sup_dev);
1985 }
1986
1987 /*
1988 * Make "proxy" SYNC_STATE_ONLY device links to represent the needs of
1989 * all the descendants. This proxy link step is needed to handle the
1990 * case where the supplier is added before the consumer's parent device
1991 * (@dev).
1992 */
1993 while ((child = fwnode_get_next_available_child_node(fwnode, child)))
1994 __fw_devlink_link_to_suppliers(dev, child);
1995 }
1996
fw_devlink_link_device(struct device * dev)1997 static void fw_devlink_link_device(struct device *dev)
1998 {
1999 struct fwnode_handle *fwnode = dev->fwnode;
2000
2001 if (!fw_devlink_flags)
2002 return;
2003
2004 fw_devlink_parse_fwtree(fwnode);
2005
2006 mutex_lock(&fwnode_link_lock);
2007 __fw_devlink_link_to_consumers(dev);
2008 __fw_devlink_link_to_suppliers(dev, fwnode);
2009 mutex_unlock(&fwnode_link_lock);
2010 }
2011
2012 /* Device links support end. */
2013
2014 int (*platform_notify)(struct device *dev) = NULL;
2015 int (*platform_notify_remove)(struct device *dev) = NULL;
2016 static struct kobject *dev_kobj;
2017 struct kobject *sysfs_dev_char_kobj;
2018 struct kobject *sysfs_dev_block_kobj;
2019
2020 static DEFINE_MUTEX(device_hotplug_lock);
2021
lock_device_hotplug(void)2022 void lock_device_hotplug(void)
2023 {
2024 mutex_lock(&device_hotplug_lock);
2025 }
2026
unlock_device_hotplug(void)2027 void unlock_device_hotplug(void)
2028 {
2029 mutex_unlock(&device_hotplug_lock);
2030 }
2031
lock_device_hotplug_sysfs(void)2032 int lock_device_hotplug_sysfs(void)
2033 {
2034 if (mutex_trylock(&device_hotplug_lock))
2035 return 0;
2036
2037 /* Avoid busy looping (5 ms of sleep should do). */
2038 msleep(5);
2039 return restart_syscall();
2040 }
2041
2042 #ifdef CONFIG_BLOCK
device_is_not_partition(struct device * dev)2043 static inline int device_is_not_partition(struct device *dev)
2044 {
2045 return !(dev->type == &part_type);
2046 }
2047 #else
device_is_not_partition(struct device * dev)2048 static inline int device_is_not_partition(struct device *dev)
2049 {
2050 return 1;
2051 }
2052 #endif
2053
device_platform_notify(struct device * dev)2054 static void device_platform_notify(struct device *dev)
2055 {
2056 acpi_device_notify(dev);
2057
2058 software_node_notify(dev);
2059
2060 if (platform_notify)
2061 platform_notify(dev);
2062 }
2063
device_platform_notify_remove(struct device * dev)2064 static void device_platform_notify_remove(struct device *dev)
2065 {
2066 acpi_device_notify_remove(dev);
2067
2068 software_node_notify_remove(dev);
2069
2070 if (platform_notify_remove)
2071 platform_notify_remove(dev);
2072 }
2073
2074 /**
2075 * dev_driver_string - Return a device's driver name, if at all possible
2076 * @dev: struct device to get the name of
2077 *
2078 * Will return the device's driver's name if it is bound to a device. If
2079 * the device is not bound to a driver, it will return the name of the bus
2080 * it is attached to. If it is not attached to a bus either, an empty
2081 * string will be returned.
2082 */
dev_driver_string(const struct device * dev)2083 const char *dev_driver_string(const struct device *dev)
2084 {
2085 struct device_driver *drv;
2086
2087 /* dev->driver can change to NULL underneath us because of unbinding,
2088 * so be careful about accessing it. dev->bus and dev->class should
2089 * never change once they are set, so they don't need special care.
2090 */
2091 drv = READ_ONCE(dev->driver);
2092 return drv ? drv->name : dev_bus_name(dev);
2093 }
2094 EXPORT_SYMBOL(dev_driver_string);
2095
2096 #define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr)
2097
dev_attr_show(struct kobject * kobj,struct attribute * attr,char * buf)2098 static ssize_t dev_attr_show(struct kobject *kobj, struct attribute *attr,
2099 char *buf)
2100 {
2101 struct device_attribute *dev_attr = to_dev_attr(attr);
2102 struct device *dev = kobj_to_dev(kobj);
2103 ssize_t ret = -EIO;
2104
2105 if (dev_attr->show)
2106 ret = dev_attr->show(dev, dev_attr, buf);
2107 if (ret >= (ssize_t)PAGE_SIZE) {
2108 printk("dev_attr_show: %pS returned bad count\n",
2109 dev_attr->show);
2110 }
2111 return ret;
2112 }
2113
dev_attr_store(struct kobject * kobj,struct attribute * attr,const char * buf,size_t count)2114 static ssize_t dev_attr_store(struct kobject *kobj, struct attribute *attr,
2115 const char *buf, size_t count)
2116 {
2117 struct device_attribute *dev_attr = to_dev_attr(attr);
2118 struct device *dev = kobj_to_dev(kobj);
2119 ssize_t ret = -EIO;
2120
2121 if (dev_attr->store)
2122 ret = dev_attr->store(dev, dev_attr, buf, count);
2123 return ret;
2124 }
2125
2126 static const struct sysfs_ops dev_sysfs_ops = {
2127 .show = dev_attr_show,
2128 .store = dev_attr_store,
2129 };
2130
2131 #define to_ext_attr(x) container_of(x, struct dev_ext_attribute, attr)
2132
device_store_ulong(struct device * dev,struct device_attribute * attr,const char * buf,size_t size)2133 ssize_t device_store_ulong(struct device *dev,
2134 struct device_attribute *attr,
2135 const char *buf, size_t size)
2136 {
2137 struct dev_ext_attribute *ea = to_ext_attr(attr);
2138 int ret;
2139 unsigned long new;
2140
2141 ret = kstrtoul(buf, 0, &new);
2142 if (ret)
2143 return ret;
2144 *(unsigned long *)(ea->var) = new;
2145 /* Always return full write size even if we didn't consume all */
2146 return size;
2147 }
2148 EXPORT_SYMBOL_GPL(device_store_ulong);
2149
device_show_ulong(struct device * dev,struct device_attribute * attr,char * buf)2150 ssize_t device_show_ulong(struct device *dev,
2151 struct device_attribute *attr,
2152 char *buf)
2153 {
2154 struct dev_ext_attribute *ea = to_ext_attr(attr);
2155 return sysfs_emit(buf, "%lx\n", *(unsigned long *)(ea->var));
2156 }
2157 EXPORT_SYMBOL_GPL(device_show_ulong);
2158
device_store_int(struct device * dev,struct device_attribute * attr,const char * buf,size_t size)2159 ssize_t device_store_int(struct device *dev,
2160 struct device_attribute *attr,
2161 const char *buf, size_t size)
2162 {
2163 struct dev_ext_attribute *ea = to_ext_attr(attr);
2164 int ret;
2165 long new;
2166
2167 ret = kstrtol(buf, 0, &new);
2168 if (ret)
2169 return ret;
2170
2171 if (new > INT_MAX || new < INT_MIN)
2172 return -EINVAL;
2173 *(int *)(ea->var) = new;
2174 /* Always return full write size even if we didn't consume all */
2175 return size;
2176 }
2177 EXPORT_SYMBOL_GPL(device_store_int);
2178
device_show_int(struct device * dev,struct device_attribute * attr,char * buf)2179 ssize_t device_show_int(struct device *dev,
2180 struct device_attribute *attr,
2181 char *buf)
2182 {
2183 struct dev_ext_attribute *ea = to_ext_attr(attr);
2184
2185 return sysfs_emit(buf, "%d\n", *(int *)(ea->var));
2186 }
2187 EXPORT_SYMBOL_GPL(device_show_int);
2188
device_store_bool(struct device * dev,struct device_attribute * attr,const char * buf,size_t size)2189 ssize_t device_store_bool(struct device *dev, struct device_attribute *attr,
2190 const char *buf, size_t size)
2191 {
2192 struct dev_ext_attribute *ea = to_ext_attr(attr);
2193
2194 if (strtobool(buf, ea->var) < 0)
2195 return -EINVAL;
2196
2197 return size;
2198 }
2199 EXPORT_SYMBOL_GPL(device_store_bool);
2200
device_show_bool(struct device * dev,struct device_attribute * attr,char * buf)2201 ssize_t device_show_bool(struct device *dev, struct device_attribute *attr,
2202 char *buf)
2203 {
2204 struct dev_ext_attribute *ea = to_ext_attr(attr);
2205
2206 return sysfs_emit(buf, "%d\n", *(bool *)(ea->var));
2207 }
2208 EXPORT_SYMBOL_GPL(device_show_bool);
2209
2210 /**
2211 * device_release - free device structure.
2212 * @kobj: device's kobject.
2213 *
2214 * This is called once the reference count for the object
2215 * reaches 0. We forward the call to the device's release
2216 * method, which should handle actually freeing the structure.
2217 */
device_release(struct kobject * kobj)2218 static void device_release(struct kobject *kobj)
2219 {
2220 struct device *dev = kobj_to_dev(kobj);
2221 struct device_private *p = dev->p;
2222
2223 /*
2224 * Some platform devices are driven without driver attached
2225 * and managed resources may have been acquired. Make sure
2226 * all resources are released.
2227 *
2228 * Drivers still can add resources into device after device
2229 * is deleted but alive, so release devres here to avoid
2230 * possible memory leak.
2231 */
2232 devres_release_all(dev);
2233
2234 kfree(dev->dma_range_map);
2235
2236 if (dev->release)
2237 dev->release(dev);
2238 else if (dev->type && dev->type->release)
2239 dev->type->release(dev);
2240 else if (dev->class && dev->class->dev_release)
2241 dev->class->dev_release(dev);
2242 else
2243 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",
2244 dev_name(dev));
2245 kfree(p);
2246 }
2247
device_namespace(struct kobject * kobj)2248 static const void *device_namespace(struct kobject *kobj)
2249 {
2250 struct device *dev = kobj_to_dev(kobj);
2251 const void *ns = NULL;
2252
2253 if (dev->class && dev->class->ns_type)
2254 ns = dev->class->namespace(dev);
2255
2256 return ns;
2257 }
2258
device_get_ownership(struct kobject * kobj,kuid_t * uid,kgid_t * gid)2259 static void device_get_ownership(struct kobject *kobj, kuid_t *uid, kgid_t *gid)
2260 {
2261 struct device *dev = kobj_to_dev(kobj);
2262
2263 if (dev->class && dev->class->get_ownership)
2264 dev->class->get_ownership(dev, uid, gid);
2265 }
2266
2267 static struct kobj_type device_ktype = {
2268 .release = device_release,
2269 .sysfs_ops = &dev_sysfs_ops,
2270 .namespace = device_namespace,
2271 .get_ownership = device_get_ownership,
2272 };
2273
2274
dev_uevent_filter(struct kobject * kobj)2275 static int dev_uevent_filter(struct kobject *kobj)
2276 {
2277 const struct kobj_type *ktype = get_ktype(kobj);
2278
2279 if (ktype == &device_ktype) {
2280 struct device *dev = kobj_to_dev(kobj);
2281 if (dev->bus)
2282 return 1;
2283 if (dev->class)
2284 return 1;
2285 }
2286 return 0;
2287 }
2288
dev_uevent_name(struct kobject * kobj)2289 static const char *dev_uevent_name(struct kobject *kobj)
2290 {
2291 struct device *dev = kobj_to_dev(kobj);
2292
2293 if (dev->bus)
2294 return dev->bus->name;
2295 if (dev->class)
2296 return dev->class->name;
2297 return NULL;
2298 }
2299
dev_uevent(struct kobject * kobj,struct kobj_uevent_env * env)2300 static int dev_uevent(struct kobject *kobj, struct kobj_uevent_env *env)
2301 {
2302 struct device *dev = kobj_to_dev(kobj);
2303 int retval = 0;
2304
2305 /* add device node properties if present */
2306 if (MAJOR(dev->devt)) {
2307 const char *tmp;
2308 const char *name;
2309 umode_t mode = 0;
2310 kuid_t uid = GLOBAL_ROOT_UID;
2311 kgid_t gid = GLOBAL_ROOT_GID;
2312
2313 add_uevent_var(env, "MAJOR=%u", MAJOR(dev->devt));
2314 add_uevent_var(env, "MINOR=%u", MINOR(dev->devt));
2315 name = device_get_devnode(dev, &mode, &uid, &gid, &tmp);
2316 if (name) {
2317 add_uevent_var(env, "DEVNAME=%s", name);
2318 if (mode)
2319 add_uevent_var(env, "DEVMODE=%#o", mode & 0777);
2320 if (!uid_eq(uid, GLOBAL_ROOT_UID))
2321 add_uevent_var(env, "DEVUID=%u", from_kuid(&init_user_ns, uid));
2322 if (!gid_eq(gid, GLOBAL_ROOT_GID))
2323 add_uevent_var(env, "DEVGID=%u", from_kgid(&init_user_ns, gid));
2324 kfree(tmp);
2325 }
2326 }
2327
2328 if (dev->type && dev->type->name)
2329 add_uevent_var(env, "DEVTYPE=%s", dev->type->name);
2330
2331 if (dev->driver)
2332 add_uevent_var(env, "DRIVER=%s", dev->driver->name);
2333
2334 /* Add common DT information about the device */
2335 of_device_uevent(dev, env);
2336
2337 /* have the bus specific function add its stuff */
2338 if (dev->bus && dev->bus->uevent) {
2339 retval = dev->bus->uevent(dev, env);
2340 if (retval)
2341 pr_debug("device: '%s': %s: bus uevent() returned %d\n",
2342 dev_name(dev), __func__, retval);
2343 }
2344
2345 /* have the class specific function add its stuff */
2346 if (dev->class && dev->class->dev_uevent) {
2347 retval = dev->class->dev_uevent(dev, env);
2348 if (retval)
2349 pr_debug("device: '%s': %s: class uevent() "
2350 "returned %d\n", dev_name(dev),
2351 __func__, retval);
2352 }
2353
2354 /* have the device type specific function add its stuff */
2355 if (dev->type && dev->type->uevent) {
2356 retval = dev->type->uevent(dev, env);
2357 if (retval)
2358 pr_debug("device: '%s': %s: dev_type uevent() "
2359 "returned %d\n", dev_name(dev),
2360 __func__, retval);
2361 }
2362
2363 return retval;
2364 }
2365
2366 static const struct kset_uevent_ops device_uevent_ops = {
2367 .filter = dev_uevent_filter,
2368 .name = dev_uevent_name,
2369 .uevent = dev_uevent,
2370 };
2371
uevent_show(struct device * dev,struct device_attribute * attr,char * buf)2372 static ssize_t uevent_show(struct device *dev, struct device_attribute *attr,
2373 char *buf)
2374 {
2375 struct kobject *top_kobj;
2376 struct kset *kset;
2377 struct kobj_uevent_env *env = NULL;
2378 int i;
2379 int len = 0;
2380 int retval;
2381
2382 /* search the kset, the device belongs to */
2383 top_kobj = &dev->kobj;
2384 while (!top_kobj->kset && top_kobj->parent)
2385 top_kobj = top_kobj->parent;
2386 if (!top_kobj->kset)
2387 goto out;
2388
2389 kset = top_kobj->kset;
2390 if (!kset->uevent_ops || !kset->uevent_ops->uevent)
2391 goto out;
2392
2393 /* respect filter */
2394 if (kset->uevent_ops && kset->uevent_ops->filter)
2395 if (!kset->uevent_ops->filter(&dev->kobj))
2396 goto out;
2397
2398 env = kzalloc(sizeof(struct kobj_uevent_env), GFP_KERNEL);
2399 if (!env)
2400 return -ENOMEM;
2401
2402 /* let the kset specific function add its keys */
2403 retval = kset->uevent_ops->uevent(&dev->kobj, env);
2404 if (retval)
2405 goto out;
2406
2407 /* copy keys to file */
2408 for (i = 0; i < env->envp_idx; i++)
2409 len += sysfs_emit_at(buf, len, "%s\n", env->envp[i]);
2410 out:
2411 kfree(env);
2412 return len;
2413 }
2414
uevent_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)2415 static ssize_t uevent_store(struct device *dev, struct device_attribute *attr,
2416 const char *buf, size_t count)
2417 {
2418 int rc;
2419
2420 rc = kobject_synth_uevent(&dev->kobj, buf, count);
2421
2422 if (rc) {
2423 dev_err(dev, "uevent: failed to send synthetic uevent\n");
2424 return rc;
2425 }
2426
2427 return count;
2428 }
2429 static DEVICE_ATTR_RW(uevent);
2430
online_show(struct device * dev,struct device_attribute * attr,char * buf)2431 static ssize_t online_show(struct device *dev, struct device_attribute *attr,
2432 char *buf)
2433 {
2434 bool val;
2435
2436 device_lock(dev);
2437 val = !dev->offline;
2438 device_unlock(dev);
2439 return sysfs_emit(buf, "%u\n", val);
2440 }
2441
online_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)2442 static ssize_t online_store(struct device *dev, struct device_attribute *attr,
2443 const char *buf, size_t count)
2444 {
2445 bool val;
2446 int ret;
2447
2448 ret = strtobool(buf, &val);
2449 if (ret < 0)
2450 return ret;
2451
2452 ret = lock_device_hotplug_sysfs();
2453 if (ret)
2454 return ret;
2455
2456 ret = val ? device_online(dev) : device_offline(dev);
2457 unlock_device_hotplug();
2458 return ret < 0 ? ret : count;
2459 }
2460 static DEVICE_ATTR_RW(online);
2461
removable_show(struct device * dev,struct device_attribute * attr,char * buf)2462 static ssize_t removable_show(struct device *dev, struct device_attribute *attr,
2463 char *buf)
2464 {
2465 const char *loc;
2466
2467 switch (dev->removable) {
2468 case DEVICE_REMOVABLE:
2469 loc = "removable";
2470 break;
2471 case DEVICE_FIXED:
2472 loc = "fixed";
2473 break;
2474 default:
2475 loc = "unknown";
2476 }
2477 return sysfs_emit(buf, "%s\n", loc);
2478 }
2479 static DEVICE_ATTR_RO(removable);
2480
device_add_groups(struct device * dev,const struct attribute_group ** groups)2481 int device_add_groups(struct device *dev, const struct attribute_group **groups)
2482 {
2483 return sysfs_create_groups(&dev->kobj, groups);
2484 }
2485 EXPORT_SYMBOL_GPL(device_add_groups);
2486
device_remove_groups(struct device * dev,const struct attribute_group ** groups)2487 void device_remove_groups(struct device *dev,
2488 const struct attribute_group **groups)
2489 {
2490 sysfs_remove_groups(&dev->kobj, groups);
2491 }
2492 EXPORT_SYMBOL_GPL(device_remove_groups);
2493
2494 union device_attr_group_devres {
2495 const struct attribute_group *group;
2496 const struct attribute_group **groups;
2497 };
2498
devm_attr_group_match(struct device * dev,void * res,void * data)2499 static int devm_attr_group_match(struct device *dev, void *res, void *data)
2500 {
2501 return ((union device_attr_group_devres *)res)->group == data;
2502 }
2503
devm_attr_group_remove(struct device * dev,void * res)2504 static void devm_attr_group_remove(struct device *dev, void *res)
2505 {
2506 union device_attr_group_devres *devres = res;
2507 const struct attribute_group *group = devres->group;
2508
2509 dev_dbg(dev, "%s: removing group %p\n", __func__, group);
2510 sysfs_remove_group(&dev->kobj, group);
2511 }
2512
devm_attr_groups_remove(struct device * dev,void * res)2513 static void devm_attr_groups_remove(struct device *dev, void *res)
2514 {
2515 union device_attr_group_devres *devres = res;
2516 const struct attribute_group **groups = devres->groups;
2517
2518 dev_dbg(dev, "%s: removing groups %p\n", __func__, groups);
2519 sysfs_remove_groups(&dev->kobj, groups);
2520 }
2521
2522 /**
2523 * devm_device_add_group - given a device, create a managed attribute group
2524 * @dev: The device to create the group for
2525 * @grp: The attribute group to create
2526 *
2527 * This function creates a group for the first time. It will explicitly
2528 * warn and error if any of the attribute files being created already exist.
2529 *
2530 * Returns 0 on success or error code on failure.
2531 */
devm_device_add_group(struct device * dev,const struct attribute_group * grp)2532 int devm_device_add_group(struct device *dev, const struct attribute_group *grp)
2533 {
2534 union device_attr_group_devres *devres;
2535 int error;
2536
2537 devres = devres_alloc(devm_attr_group_remove,
2538 sizeof(*devres), GFP_KERNEL);
2539 if (!devres)
2540 return -ENOMEM;
2541
2542 error = sysfs_create_group(&dev->kobj, grp);
2543 if (error) {
2544 devres_free(devres);
2545 return error;
2546 }
2547
2548 devres->group = grp;
2549 devres_add(dev, devres);
2550 return 0;
2551 }
2552 EXPORT_SYMBOL_GPL(devm_device_add_group);
2553
2554 /**
2555 * devm_device_remove_group: remove a managed group from a device
2556 * @dev: device to remove the group from
2557 * @grp: group to remove
2558 *
2559 * This function removes a group of attributes from a device. The attributes
2560 * previously have to have been created for this group, otherwise it will fail.
2561 */
devm_device_remove_group(struct device * dev,const struct attribute_group * grp)2562 void devm_device_remove_group(struct device *dev,
2563 const struct attribute_group *grp)
2564 {
2565 WARN_ON(devres_release(dev, devm_attr_group_remove,
2566 devm_attr_group_match,
2567 /* cast away const */ (void *)grp));
2568 }
2569 EXPORT_SYMBOL_GPL(devm_device_remove_group);
2570
2571 /**
2572 * devm_device_add_groups - create a bunch of managed attribute groups
2573 * @dev: The device to create the group for
2574 * @groups: The attribute groups to create, NULL terminated
2575 *
2576 * This function creates a bunch of managed attribute groups. If an error
2577 * occurs when creating a group, all previously created groups will be
2578 * removed, unwinding everything back to the original state when this
2579 * function was called. It will explicitly warn and error if any of the
2580 * attribute files being created already exist.
2581 *
2582 * Returns 0 on success or error code from sysfs_create_group on failure.
2583 */
devm_device_add_groups(struct device * dev,const struct attribute_group ** groups)2584 int devm_device_add_groups(struct device *dev,
2585 const struct attribute_group **groups)
2586 {
2587 union device_attr_group_devres *devres;
2588 int error;
2589
2590 devres = devres_alloc(devm_attr_groups_remove,
2591 sizeof(*devres), GFP_KERNEL);
2592 if (!devres)
2593 return -ENOMEM;
2594
2595 error = sysfs_create_groups(&dev->kobj, groups);
2596 if (error) {
2597 devres_free(devres);
2598 return error;
2599 }
2600
2601 devres->groups = groups;
2602 devres_add(dev, devres);
2603 return 0;
2604 }
2605 EXPORT_SYMBOL_GPL(devm_device_add_groups);
2606
2607 /**
2608 * devm_device_remove_groups - remove a list of managed groups
2609 *
2610 * @dev: The device for the groups to be removed from
2611 * @groups: NULL terminated list of groups to be removed
2612 *
2613 * If groups is not NULL, remove the specified groups from the device.
2614 */
devm_device_remove_groups(struct device * dev,const struct attribute_group ** groups)2615 void devm_device_remove_groups(struct device *dev,
2616 const struct attribute_group **groups)
2617 {
2618 WARN_ON(devres_release(dev, devm_attr_groups_remove,
2619 devm_attr_group_match,
2620 /* cast away const */ (void *)groups));
2621 }
2622 EXPORT_SYMBOL_GPL(devm_device_remove_groups);
2623
device_add_attrs(struct device * dev)2624 static int device_add_attrs(struct device *dev)
2625 {
2626 struct class *class = dev->class;
2627 const struct device_type *type = dev->type;
2628 int error;
2629
2630 if (class) {
2631 error = device_add_groups(dev, class->dev_groups);
2632 if (error)
2633 return error;
2634 }
2635
2636 if (type) {
2637 error = device_add_groups(dev, type->groups);
2638 if (error)
2639 goto err_remove_class_groups;
2640 }
2641
2642 error = device_add_groups(dev, dev->groups);
2643 if (error)
2644 goto err_remove_type_groups;
2645
2646 if (device_supports_offline(dev) && !dev->offline_disabled) {
2647 error = device_create_file(dev, &dev_attr_online);
2648 if (error)
2649 goto err_remove_dev_groups;
2650 }
2651
2652 if (fw_devlink_flags && !fw_devlink_is_permissive() && dev->fwnode) {
2653 error = device_create_file(dev, &dev_attr_waiting_for_supplier);
2654 if (error)
2655 goto err_remove_dev_online;
2656 }
2657
2658 if (dev_removable_is_valid(dev)) {
2659 error = device_create_file(dev, &dev_attr_removable);
2660 if (error)
2661 goto err_remove_dev_waiting_for_supplier;
2662 }
2663
2664 if (dev_add_physical_location(dev)) {
2665 error = device_add_group(dev,
2666 &dev_attr_physical_location_group);
2667 if (error)
2668 goto err_remove_dev_removable;
2669 }
2670
2671 return 0;
2672
2673 err_remove_dev_removable:
2674 device_remove_file(dev, &dev_attr_removable);
2675 err_remove_dev_waiting_for_supplier:
2676 device_remove_file(dev, &dev_attr_waiting_for_supplier);
2677 err_remove_dev_online:
2678 device_remove_file(dev, &dev_attr_online);
2679 err_remove_dev_groups:
2680 device_remove_groups(dev, dev->groups);
2681 err_remove_type_groups:
2682 if (type)
2683 device_remove_groups(dev, type->groups);
2684 err_remove_class_groups:
2685 if (class)
2686 device_remove_groups(dev, class->dev_groups);
2687
2688 return error;
2689 }
2690
device_remove_attrs(struct device * dev)2691 static void device_remove_attrs(struct device *dev)
2692 {
2693 struct class *class = dev->class;
2694 const struct device_type *type = dev->type;
2695
2696 if (dev->physical_location) {
2697 device_remove_group(dev, &dev_attr_physical_location_group);
2698 kfree(dev->physical_location);
2699 }
2700
2701 device_remove_file(dev, &dev_attr_removable);
2702 device_remove_file(dev, &dev_attr_waiting_for_supplier);
2703 device_remove_file(dev, &dev_attr_online);
2704 device_remove_groups(dev, dev->groups);
2705
2706 if (type)
2707 device_remove_groups(dev, type->groups);
2708
2709 if (class)
2710 device_remove_groups(dev, class->dev_groups);
2711 }
2712
dev_show(struct device * dev,struct device_attribute * attr,char * buf)2713 static ssize_t dev_show(struct device *dev, struct device_attribute *attr,
2714 char *buf)
2715 {
2716 return print_dev_t(buf, dev->devt);
2717 }
2718 static DEVICE_ATTR_RO(dev);
2719
2720 /* /sys/devices/ */
2721 struct kset *devices_kset;
2722
2723 /**
2724 * devices_kset_move_before - Move device in the devices_kset's list.
2725 * @deva: Device to move.
2726 * @devb: Device @deva should come before.
2727 */
devices_kset_move_before(struct device * deva,struct device * devb)2728 static void devices_kset_move_before(struct device *deva, struct device *devb)
2729 {
2730 if (!devices_kset)
2731 return;
2732 pr_debug("devices_kset: Moving %s before %s\n",
2733 dev_name(deva), dev_name(devb));
2734 spin_lock(&devices_kset->list_lock);
2735 list_move_tail(&deva->kobj.entry, &devb->kobj.entry);
2736 spin_unlock(&devices_kset->list_lock);
2737 }
2738
2739 /**
2740 * devices_kset_move_after - Move device in the devices_kset's list.
2741 * @deva: Device to move
2742 * @devb: Device @deva should come after.
2743 */
devices_kset_move_after(struct device * deva,struct device * devb)2744 static void devices_kset_move_after(struct device *deva, struct device *devb)
2745 {
2746 if (!devices_kset)
2747 return;
2748 pr_debug("devices_kset: Moving %s after %s\n",
2749 dev_name(deva), dev_name(devb));
2750 spin_lock(&devices_kset->list_lock);
2751 list_move(&deva->kobj.entry, &devb->kobj.entry);
2752 spin_unlock(&devices_kset->list_lock);
2753 }
2754
2755 /**
2756 * devices_kset_move_last - move the device to the end of devices_kset's list.
2757 * @dev: device to move
2758 */
devices_kset_move_last(struct device * dev)2759 void devices_kset_move_last(struct device *dev)
2760 {
2761 if (!devices_kset)
2762 return;
2763 pr_debug("devices_kset: Moving %s to end of list\n", dev_name(dev));
2764 spin_lock(&devices_kset->list_lock);
2765 list_move_tail(&dev->kobj.entry, &devices_kset->list);
2766 spin_unlock(&devices_kset->list_lock);
2767 }
2768
2769 /**
2770 * device_create_file - create sysfs attribute file for device.
2771 * @dev: device.
2772 * @attr: device attribute descriptor.
2773 */
device_create_file(struct device * dev,const struct device_attribute * attr)2774 int device_create_file(struct device *dev,
2775 const struct device_attribute *attr)
2776 {
2777 int error = 0;
2778
2779 if (dev) {
2780 WARN(((attr->attr.mode & S_IWUGO) && !attr->store),
2781 "Attribute %s: write permission without 'store'\n",
2782 attr->attr.name);
2783 WARN(((attr->attr.mode & S_IRUGO) && !attr->show),
2784 "Attribute %s: read permission without 'show'\n",
2785 attr->attr.name);
2786 error = sysfs_create_file(&dev->kobj, &attr->attr);
2787 }
2788
2789 return error;
2790 }
2791 EXPORT_SYMBOL_GPL(device_create_file);
2792
2793 /**
2794 * device_remove_file - remove sysfs attribute file.
2795 * @dev: device.
2796 * @attr: device attribute descriptor.
2797 */
device_remove_file(struct device * dev,const struct device_attribute * attr)2798 void device_remove_file(struct device *dev,
2799 const struct device_attribute *attr)
2800 {
2801 if (dev)
2802 sysfs_remove_file(&dev->kobj, &attr->attr);
2803 }
2804 EXPORT_SYMBOL_GPL(device_remove_file);
2805
2806 /**
2807 * device_remove_file_self - remove sysfs attribute file from its own method.
2808 * @dev: device.
2809 * @attr: device attribute descriptor.
2810 *
2811 * See kernfs_remove_self() for details.
2812 */
device_remove_file_self(struct device * dev,const struct device_attribute * attr)2813 bool device_remove_file_self(struct device *dev,
2814 const struct device_attribute *attr)
2815 {
2816 if (dev)
2817 return sysfs_remove_file_self(&dev->kobj, &attr->attr);
2818 else
2819 return false;
2820 }
2821 EXPORT_SYMBOL_GPL(device_remove_file_self);
2822
2823 /**
2824 * device_create_bin_file - create sysfs binary attribute file for device.
2825 * @dev: device.
2826 * @attr: device binary attribute descriptor.
2827 */
device_create_bin_file(struct device * dev,const struct bin_attribute * attr)2828 int device_create_bin_file(struct device *dev,
2829 const struct bin_attribute *attr)
2830 {
2831 int error = -EINVAL;
2832 if (dev)
2833 error = sysfs_create_bin_file(&dev->kobj, attr);
2834 return error;
2835 }
2836 EXPORT_SYMBOL_GPL(device_create_bin_file);
2837
2838 /**
2839 * device_remove_bin_file - remove sysfs binary attribute file
2840 * @dev: device.
2841 * @attr: device binary attribute descriptor.
2842 */
device_remove_bin_file(struct device * dev,const struct bin_attribute * attr)2843 void device_remove_bin_file(struct device *dev,
2844 const struct bin_attribute *attr)
2845 {
2846 if (dev)
2847 sysfs_remove_bin_file(&dev->kobj, attr);
2848 }
2849 EXPORT_SYMBOL_GPL(device_remove_bin_file);
2850
klist_children_get(struct klist_node * n)2851 static void klist_children_get(struct klist_node *n)
2852 {
2853 struct device_private *p = to_device_private_parent(n);
2854 struct device *dev = p->device;
2855
2856 get_device(dev);
2857 }
2858
klist_children_put(struct klist_node * n)2859 static void klist_children_put(struct klist_node *n)
2860 {
2861 struct device_private *p = to_device_private_parent(n);
2862 struct device *dev = p->device;
2863
2864 put_device(dev);
2865 }
2866
2867 /**
2868 * device_initialize - init device structure.
2869 * @dev: device.
2870 *
2871 * This prepares the device for use by other layers by initializing
2872 * its fields.
2873 * It is the first half of device_register(), if called by
2874 * that function, though it can also be called separately, so one
2875 * may use @dev's fields. In particular, get_device()/put_device()
2876 * may be used for reference counting of @dev after calling this
2877 * function.
2878 *
2879 * All fields in @dev must be initialized by the caller to 0, except
2880 * for those explicitly set to some other value. The simplest
2881 * approach is to use kzalloc() to allocate the structure containing
2882 * @dev.
2883 *
2884 * NOTE: Use put_device() to give up your reference instead of freeing
2885 * @dev directly once you have called this function.
2886 */
device_initialize(struct device * dev)2887 void device_initialize(struct device *dev)
2888 {
2889 dev->kobj.kset = devices_kset;
2890 kobject_init(&dev->kobj, &device_ktype);
2891 INIT_LIST_HEAD(&dev->dma_pools);
2892 mutex_init(&dev->mutex);
2893 lockdep_set_novalidate_class(&dev->mutex);
2894 spin_lock_init(&dev->devres_lock);
2895 INIT_LIST_HEAD(&dev->devres_head);
2896 device_pm_init(dev);
2897 set_dev_node(dev, NUMA_NO_NODE);
2898 INIT_LIST_HEAD(&dev->links.consumers);
2899 INIT_LIST_HEAD(&dev->links.suppliers);
2900 INIT_LIST_HEAD(&dev->links.defer_sync);
2901 dev->links.status = DL_DEV_NO_DRIVER;
2902 #if defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_DEVICE) || \
2903 defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU) || \
2904 defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU_ALL)
2905 dev->dma_coherent = dma_default_coherent;
2906 #endif
2907 #ifdef CONFIG_SWIOTLB
2908 dev->dma_io_tlb_mem = &io_tlb_default_mem;
2909 #endif
2910 }
2911 EXPORT_SYMBOL_GPL(device_initialize);
2912
virtual_device_parent(struct device * dev)2913 struct kobject *virtual_device_parent(struct device *dev)
2914 {
2915 static struct kobject *virtual_dir = NULL;
2916
2917 if (!virtual_dir)
2918 virtual_dir = kobject_create_and_add("virtual",
2919 &devices_kset->kobj);
2920
2921 return virtual_dir;
2922 }
2923
2924 struct class_dir {
2925 struct kobject kobj;
2926 struct class *class;
2927 };
2928
2929 #define to_class_dir(obj) container_of(obj, struct class_dir, kobj)
2930
class_dir_release(struct kobject * kobj)2931 static void class_dir_release(struct kobject *kobj)
2932 {
2933 struct class_dir *dir = to_class_dir(kobj);
2934 kfree(dir);
2935 }
2936
2937 static const
class_dir_child_ns_type(struct kobject * kobj)2938 struct kobj_ns_type_operations *class_dir_child_ns_type(struct kobject *kobj)
2939 {
2940 struct class_dir *dir = to_class_dir(kobj);
2941 return dir->class->ns_type;
2942 }
2943
2944 static struct kobj_type class_dir_ktype = {
2945 .release = class_dir_release,
2946 .sysfs_ops = &kobj_sysfs_ops,
2947 .child_ns_type = class_dir_child_ns_type
2948 };
2949
2950 static struct kobject *
class_dir_create_and_add(struct class * class,struct kobject * parent_kobj)2951 class_dir_create_and_add(struct class *class, struct kobject *parent_kobj)
2952 {
2953 struct class_dir *dir;
2954 int retval;
2955
2956 dir = kzalloc(sizeof(*dir), GFP_KERNEL);
2957 if (!dir)
2958 return ERR_PTR(-ENOMEM);
2959
2960 dir->class = class;
2961 kobject_init(&dir->kobj, &class_dir_ktype);
2962
2963 dir->kobj.kset = &class->p->glue_dirs;
2964
2965 retval = kobject_add(&dir->kobj, parent_kobj, "%s", class->name);
2966 if (retval < 0) {
2967 kobject_put(&dir->kobj);
2968 return ERR_PTR(retval);
2969 }
2970 return &dir->kobj;
2971 }
2972
2973 static DEFINE_MUTEX(gdp_mutex);
2974
get_device_parent(struct device * dev,struct device * parent)2975 static struct kobject *get_device_parent(struct device *dev,
2976 struct device *parent)
2977 {
2978 if (dev->class) {
2979 struct kobject *kobj = NULL;
2980 struct kobject *parent_kobj;
2981 struct kobject *k;
2982
2983 #ifdef CONFIG_BLOCK
2984 /* block disks show up in /sys/block */
2985 if (sysfs_deprecated && dev->class == &block_class) {
2986 if (parent && parent->class == &block_class)
2987 return &parent->kobj;
2988 return &block_class.p->subsys.kobj;
2989 }
2990 #endif
2991
2992 /*
2993 * If we have no parent, we live in "virtual".
2994 * Class-devices with a non class-device as parent, live
2995 * in a "glue" directory to prevent namespace collisions.
2996 */
2997 if (parent == NULL)
2998 parent_kobj = virtual_device_parent(dev);
2999 else if (parent->class && !dev->class->ns_type)
3000 return &parent->kobj;
3001 else
3002 parent_kobj = &parent->kobj;
3003
3004 mutex_lock(&gdp_mutex);
3005
3006 /* find our class-directory at the parent and reference it */
3007 spin_lock(&dev->class->p->glue_dirs.list_lock);
3008 list_for_each_entry(k, &dev->class->p->glue_dirs.list, entry)
3009 if (k->parent == parent_kobj) {
3010 kobj = kobject_get(k);
3011 break;
3012 }
3013 spin_unlock(&dev->class->p->glue_dirs.list_lock);
3014 if (kobj) {
3015 mutex_unlock(&gdp_mutex);
3016 return kobj;
3017 }
3018
3019 /* or create a new class-directory at the parent device */
3020 k = class_dir_create_and_add(dev->class, parent_kobj);
3021 /* do not emit an uevent for this simple "glue" directory */
3022 mutex_unlock(&gdp_mutex);
3023 return k;
3024 }
3025
3026 /* subsystems can specify a default root directory for their devices */
3027 if (!parent && dev->bus && dev->bus->dev_root)
3028 return &dev->bus->dev_root->kobj;
3029
3030 if (parent)
3031 return &parent->kobj;
3032 return NULL;
3033 }
3034
live_in_glue_dir(struct kobject * kobj,struct device * dev)3035 static inline bool live_in_glue_dir(struct kobject *kobj,
3036 struct device *dev)
3037 {
3038 if (!kobj || !dev->class ||
3039 kobj->kset != &dev->class->p->glue_dirs)
3040 return false;
3041 return true;
3042 }
3043
get_glue_dir(struct device * dev)3044 static inline struct kobject *get_glue_dir(struct device *dev)
3045 {
3046 return dev->kobj.parent;
3047 }
3048
3049 /**
3050 * kobject_has_children - Returns whether a kobject has children.
3051 * @kobj: the object to test
3052 *
3053 * This will return whether a kobject has other kobjects as children.
3054 *
3055 * It does NOT account for the presence of attribute files, only sub
3056 * directories. It also assumes there is no concurrent addition or
3057 * removal of such children, and thus relies on external locking.
3058 */
kobject_has_children(struct kobject * kobj)3059 static inline bool kobject_has_children(struct kobject *kobj)
3060 {
3061 WARN_ON_ONCE(kref_read(&kobj->kref) == 0);
3062
3063 return kobj->sd && kobj->sd->dir.subdirs;
3064 }
3065
3066 /*
3067 * make sure cleaning up dir as the last step, we need to make
3068 * sure .release handler of kobject is run with holding the
3069 * global lock
3070 */
cleanup_glue_dir(struct device * dev,struct kobject * glue_dir)3071 static void cleanup_glue_dir(struct device *dev, struct kobject *glue_dir)
3072 {
3073 unsigned int ref;
3074
3075 /* see if we live in a "glue" directory */
3076 if (!live_in_glue_dir(glue_dir, dev))
3077 return;
3078
3079 mutex_lock(&gdp_mutex);
3080 /**
3081 * There is a race condition between removing glue directory
3082 * and adding a new device under the glue directory.
3083 *
3084 * CPU1: CPU2:
3085 *
3086 * device_add()
3087 * get_device_parent()
3088 * class_dir_create_and_add()
3089 * kobject_add_internal()
3090 * create_dir() // create glue_dir
3091 *
3092 * device_add()
3093 * get_device_parent()
3094 * kobject_get() // get glue_dir
3095 *
3096 * device_del()
3097 * cleanup_glue_dir()
3098 * kobject_del(glue_dir)
3099 *
3100 * kobject_add()
3101 * kobject_add_internal()
3102 * create_dir() // in glue_dir
3103 * sysfs_create_dir_ns()
3104 * kernfs_create_dir_ns(sd)
3105 *
3106 * sysfs_remove_dir() // glue_dir->sd=NULL
3107 * sysfs_put() // free glue_dir->sd
3108 *
3109 * // sd is freed
3110 * kernfs_new_node(sd)
3111 * kernfs_get(glue_dir)
3112 * kernfs_add_one()
3113 * kernfs_put()
3114 *
3115 * Before CPU1 remove last child device under glue dir, if CPU2 add
3116 * a new device under glue dir, the glue_dir kobject reference count
3117 * will be increase to 2 in kobject_get(k). And CPU2 has been called
3118 * kernfs_create_dir_ns(). Meanwhile, CPU1 call sysfs_remove_dir()
3119 * and sysfs_put(). This result in glue_dir->sd is freed.
3120 *
3121 * Then the CPU2 will see a stale "empty" but still potentially used
3122 * glue dir around in kernfs_new_node().
3123 *
3124 * In order to avoid this happening, we also should make sure that
3125 * kernfs_node for glue_dir is released in CPU1 only when refcount
3126 * for glue_dir kobj is 1.
3127 */
3128 ref = kref_read(&glue_dir->kref);
3129 if (!kobject_has_children(glue_dir) && !--ref)
3130 kobject_del(glue_dir);
3131 kobject_put(glue_dir);
3132 mutex_unlock(&gdp_mutex);
3133 }
3134
device_add_class_symlinks(struct device * dev)3135 static int device_add_class_symlinks(struct device *dev)
3136 {
3137 struct device_node *of_node = dev_of_node(dev);
3138 int error;
3139
3140 if (of_node) {
3141 error = sysfs_create_link(&dev->kobj, of_node_kobj(of_node), "of_node");
3142 if (error)
3143 dev_warn(dev, "Error %d creating of_node link\n",error);
3144 /* An error here doesn't warrant bringing down the device */
3145 }
3146
3147 if (!dev->class)
3148 return 0;
3149
3150 error = sysfs_create_link(&dev->kobj,
3151 &dev->class->p->subsys.kobj,
3152 "subsystem");
3153 if (error)
3154 goto out_devnode;
3155
3156 if (dev->parent && device_is_not_partition(dev)) {
3157 error = sysfs_create_link(&dev->kobj, &dev->parent->kobj,
3158 "device");
3159 if (error)
3160 goto out_subsys;
3161 }
3162
3163 #ifdef CONFIG_BLOCK
3164 /* /sys/block has directories and does not need symlinks */
3165 if (sysfs_deprecated && dev->class == &block_class)
3166 return 0;
3167 #endif
3168
3169 /* link in the class directory pointing to the device */
3170 error = sysfs_create_link(&dev->class->p->subsys.kobj,
3171 &dev->kobj, dev_name(dev));
3172 if (error)
3173 goto out_device;
3174
3175 return 0;
3176
3177 out_device:
3178 sysfs_remove_link(&dev->kobj, "device");
3179
3180 out_subsys:
3181 sysfs_remove_link(&dev->kobj, "subsystem");
3182 out_devnode:
3183 sysfs_remove_link(&dev->kobj, "of_node");
3184 return error;
3185 }
3186
device_remove_class_symlinks(struct device * dev)3187 static void device_remove_class_symlinks(struct device *dev)
3188 {
3189 if (dev_of_node(dev))
3190 sysfs_remove_link(&dev->kobj, "of_node");
3191
3192 if (!dev->class)
3193 return;
3194
3195 if (dev->parent && device_is_not_partition(dev))
3196 sysfs_remove_link(&dev->kobj, "device");
3197 sysfs_remove_link(&dev->kobj, "subsystem");
3198 #ifdef CONFIG_BLOCK
3199 if (sysfs_deprecated && dev->class == &block_class)
3200 return;
3201 #endif
3202 sysfs_delete_link(&dev->class->p->subsys.kobj, &dev->kobj, dev_name(dev));
3203 }
3204
3205 /**
3206 * dev_set_name - set a device name
3207 * @dev: device
3208 * @fmt: format string for the device's name
3209 */
dev_set_name(struct device * dev,const char * fmt,...)3210 int dev_set_name(struct device *dev, const char *fmt, ...)
3211 {
3212 va_list vargs;
3213 int err;
3214
3215 va_start(vargs, fmt);
3216 err = kobject_set_name_vargs(&dev->kobj, fmt, vargs);
3217 va_end(vargs);
3218 return err;
3219 }
3220 EXPORT_SYMBOL_GPL(dev_set_name);
3221
3222 /**
3223 * device_to_dev_kobj - select a /sys/dev/ directory for the device
3224 * @dev: device
3225 *
3226 * By default we select char/ for new entries. Setting class->dev_obj
3227 * to NULL prevents an entry from being created. class->dev_kobj must
3228 * be set (or cleared) before any devices are registered to the class
3229 * otherwise device_create_sys_dev_entry() and
3230 * device_remove_sys_dev_entry() will disagree about the presence of
3231 * the link.
3232 */
device_to_dev_kobj(struct device * dev)3233 static struct kobject *device_to_dev_kobj(struct device *dev)
3234 {
3235 struct kobject *kobj;
3236
3237 if (dev->class)
3238 kobj = dev->class->dev_kobj;
3239 else
3240 kobj = sysfs_dev_char_kobj;
3241
3242 return kobj;
3243 }
3244
device_create_sys_dev_entry(struct device * dev)3245 static int device_create_sys_dev_entry(struct device *dev)
3246 {
3247 struct kobject *kobj = device_to_dev_kobj(dev);
3248 int error = 0;
3249 char devt_str[15];
3250
3251 if (kobj) {
3252 format_dev_t(devt_str, dev->devt);
3253 error = sysfs_create_link(kobj, &dev->kobj, devt_str);
3254 }
3255
3256 return error;
3257 }
3258
device_remove_sys_dev_entry(struct device * dev)3259 static void device_remove_sys_dev_entry(struct device *dev)
3260 {
3261 struct kobject *kobj = device_to_dev_kobj(dev);
3262 char devt_str[15];
3263
3264 if (kobj) {
3265 format_dev_t(devt_str, dev->devt);
3266 sysfs_remove_link(kobj, devt_str);
3267 }
3268 }
3269
device_private_init(struct device * dev)3270 static int device_private_init(struct device *dev)
3271 {
3272 dev->p = kzalloc(sizeof(*dev->p), GFP_KERNEL);
3273 if (!dev->p)
3274 return -ENOMEM;
3275 dev->p->device = dev;
3276 klist_init(&dev->p->klist_children, klist_children_get,
3277 klist_children_put);
3278 INIT_LIST_HEAD(&dev->p->deferred_probe);
3279 return 0;
3280 }
3281
3282 /**
3283 * device_add - add device to device hierarchy.
3284 * @dev: device.
3285 *
3286 * This is part 2 of device_register(), though may be called
3287 * separately _iff_ device_initialize() has been called separately.
3288 *
3289 * This adds @dev to the kobject hierarchy via kobject_add(), adds it
3290 * to the global and sibling lists for the device, then
3291 * adds it to the other relevant subsystems of the driver model.
3292 *
3293 * Do not call this routine or device_register() more than once for
3294 * any device structure. The driver model core is not designed to work
3295 * with devices that get unregistered and then spring back to life.
3296 * (Among other things, it's very hard to guarantee that all references
3297 * to the previous incarnation of @dev have been dropped.) Allocate
3298 * and register a fresh new struct device instead.
3299 *
3300 * NOTE: _Never_ directly free @dev after calling this function, even
3301 * if it returned an error! Always use put_device() to give up your
3302 * reference instead.
3303 *
3304 * Rule of thumb is: if device_add() succeeds, you should call
3305 * device_del() when you want to get rid of it. If device_add() has
3306 * *not* succeeded, use *only* put_device() to drop the reference
3307 * count.
3308 */
device_add(struct device * dev)3309 int device_add(struct device *dev)
3310 {
3311 struct device *parent;
3312 struct kobject *kobj;
3313 struct class_interface *class_intf;
3314 int error = -EINVAL;
3315 struct kobject *glue_dir = NULL;
3316
3317 dev = get_device(dev);
3318 if (!dev)
3319 goto done;
3320
3321 if (!dev->p) {
3322 error = device_private_init(dev);
3323 if (error)
3324 goto done;
3325 }
3326
3327 /*
3328 * for statically allocated devices, which should all be converted
3329 * some day, we need to initialize the name. We prevent reading back
3330 * the name, and force the use of dev_name()
3331 */
3332 if (dev->init_name) {
3333 dev_set_name(dev, "%s", dev->init_name);
3334 dev->init_name = NULL;
3335 }
3336
3337 /* subsystems can specify simple device enumeration */
3338 if (!dev_name(dev) && dev->bus && dev->bus->dev_name)
3339 dev_set_name(dev, "%s%u", dev->bus->dev_name, dev->id);
3340
3341 if (!dev_name(dev)) {
3342 error = -EINVAL;
3343 goto name_error;
3344 }
3345
3346 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
3347
3348 parent = get_device(dev->parent);
3349 kobj = get_device_parent(dev, parent);
3350 if (IS_ERR(kobj)) {
3351 error = PTR_ERR(kobj);
3352 goto parent_error;
3353 }
3354 if (kobj)
3355 dev->kobj.parent = kobj;
3356
3357 /* use parent numa_node */
3358 if (parent && (dev_to_node(dev) == NUMA_NO_NODE))
3359 set_dev_node(dev, dev_to_node(parent));
3360
3361 /* first, register with generic layer. */
3362 /* we require the name to be set before, and pass NULL */
3363 error = kobject_add(&dev->kobj, dev->kobj.parent, NULL);
3364 if (error) {
3365 glue_dir = get_glue_dir(dev);
3366 goto Error;
3367 }
3368
3369 /* notify platform of device entry */
3370 device_platform_notify(dev);
3371
3372 error = device_create_file(dev, &dev_attr_uevent);
3373 if (error)
3374 goto attrError;
3375
3376 error = device_add_class_symlinks(dev);
3377 if (error)
3378 goto SymlinkError;
3379 error = device_add_attrs(dev);
3380 if (error)
3381 goto AttrsError;
3382 error = bus_add_device(dev);
3383 if (error)
3384 goto BusError;
3385 error = dpm_sysfs_add(dev);
3386 if (error)
3387 goto DPMError;
3388 device_pm_add(dev);
3389
3390 if (MAJOR(dev->devt)) {
3391 error = device_create_file(dev, &dev_attr_dev);
3392 if (error)
3393 goto DevAttrError;
3394
3395 error = device_create_sys_dev_entry(dev);
3396 if (error)
3397 goto SysEntryError;
3398
3399 devtmpfs_create_node(dev);
3400 }
3401
3402 /* Notify clients of device addition. This call must come
3403 * after dpm_sysfs_add() and before kobject_uevent().
3404 */
3405 if (dev->bus)
3406 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
3407 BUS_NOTIFY_ADD_DEVICE, dev);
3408
3409 kobject_uevent(&dev->kobj, KOBJ_ADD);
3410
3411 /*
3412 * Check if any of the other devices (consumers) have been waiting for
3413 * this device (supplier) to be added so that they can create a device
3414 * link to it.
3415 *
3416 * This needs to happen after device_pm_add() because device_link_add()
3417 * requires the supplier be registered before it's called.
3418 *
3419 * But this also needs to happen before bus_probe_device() to make sure
3420 * waiting consumers can link to it before the driver is bound to the
3421 * device and the driver sync_state callback is called for this device.
3422 */
3423 if (dev->fwnode && !dev->fwnode->dev) {
3424 dev->fwnode->dev = dev;
3425 fw_devlink_link_device(dev);
3426 }
3427
3428 bus_probe_device(dev);
3429
3430 /*
3431 * If all driver registration is done and a newly added device doesn't
3432 * match with any driver, don't block its consumers from probing in
3433 * case the consumer device is able to operate without this supplier.
3434 */
3435 if (dev->fwnode && fw_devlink_drv_reg_done && !dev->can_match)
3436 fw_devlink_unblock_consumers(dev);
3437
3438 if (parent)
3439 klist_add_tail(&dev->p->knode_parent,
3440 &parent->p->klist_children);
3441
3442 if (dev->class) {
3443 mutex_lock(&dev->class->p->mutex);
3444 /* tie the class to the device */
3445 klist_add_tail(&dev->p->knode_class,
3446 &dev->class->p->klist_devices);
3447
3448 /* notify any interfaces that the device is here */
3449 list_for_each_entry(class_intf,
3450 &dev->class->p->interfaces, node)
3451 if (class_intf->add_dev)
3452 class_intf->add_dev(dev, class_intf);
3453 mutex_unlock(&dev->class->p->mutex);
3454 }
3455 done:
3456 put_device(dev);
3457 return error;
3458 SysEntryError:
3459 if (MAJOR(dev->devt))
3460 device_remove_file(dev, &dev_attr_dev);
3461 DevAttrError:
3462 device_pm_remove(dev);
3463 dpm_sysfs_remove(dev);
3464 DPMError:
3465 bus_remove_device(dev);
3466 BusError:
3467 device_remove_attrs(dev);
3468 AttrsError:
3469 device_remove_class_symlinks(dev);
3470 SymlinkError:
3471 device_remove_file(dev, &dev_attr_uevent);
3472 attrError:
3473 device_platform_notify_remove(dev);
3474 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
3475 glue_dir = get_glue_dir(dev);
3476 kobject_del(&dev->kobj);
3477 Error:
3478 cleanup_glue_dir(dev, glue_dir);
3479 parent_error:
3480 put_device(parent);
3481 name_error:
3482 kfree(dev->p);
3483 dev->p = NULL;
3484 goto done;
3485 }
3486 EXPORT_SYMBOL_GPL(device_add);
3487
3488 /**
3489 * device_register - register a device with the system.
3490 * @dev: pointer to the device structure
3491 *
3492 * This happens in two clean steps - initialize the device
3493 * and add it to the system. The two steps can be called
3494 * separately, but this is the easiest and most common.
3495 * I.e. you should only call the two helpers separately if
3496 * have a clearly defined need to use and refcount the device
3497 * before it is added to the hierarchy.
3498 *
3499 * For more information, see the kerneldoc for device_initialize()
3500 * and device_add().
3501 *
3502 * NOTE: _Never_ directly free @dev after calling this function, even
3503 * if it returned an error! Always use put_device() to give up the
3504 * reference initialized in this function instead.
3505 */
device_register(struct device * dev)3506 int device_register(struct device *dev)
3507 {
3508 device_initialize(dev);
3509 return device_add(dev);
3510 }
3511 EXPORT_SYMBOL_GPL(device_register);
3512
3513 /**
3514 * get_device - increment reference count for device.
3515 * @dev: device.
3516 *
3517 * This simply forwards the call to kobject_get(), though
3518 * we do take care to provide for the case that we get a NULL
3519 * pointer passed in.
3520 */
get_device(struct device * dev)3521 struct device *get_device(struct device *dev)
3522 {
3523 return dev ? kobj_to_dev(kobject_get(&dev->kobj)) : NULL;
3524 }
3525 EXPORT_SYMBOL_GPL(get_device);
3526
3527 /**
3528 * put_device - decrement reference count.
3529 * @dev: device in question.
3530 */
put_device(struct device * dev)3531 void put_device(struct device *dev)
3532 {
3533 /* might_sleep(); */
3534 if (dev)
3535 kobject_put(&dev->kobj);
3536 }
3537 EXPORT_SYMBOL_GPL(put_device);
3538
kill_device(struct device * dev)3539 bool kill_device(struct device *dev)
3540 {
3541 /*
3542 * Require the device lock and set the "dead" flag to guarantee that
3543 * the update behavior is consistent with the other bitfields near
3544 * it and that we cannot have an asynchronous probe routine trying
3545 * to run while we are tearing out the bus/class/sysfs from
3546 * underneath the device.
3547 */
3548 device_lock_assert(dev);
3549
3550 if (dev->p->dead)
3551 return false;
3552 dev->p->dead = true;
3553 return true;
3554 }
3555 EXPORT_SYMBOL_GPL(kill_device);
3556
3557 /**
3558 * device_del - delete device from system.
3559 * @dev: device.
3560 *
3561 * This is the first part of the device unregistration
3562 * sequence. This removes the device from the lists we control
3563 * from here, has it removed from the other driver model
3564 * subsystems it was added to in device_add(), and removes it
3565 * from the kobject hierarchy.
3566 *
3567 * NOTE: this should be called manually _iff_ device_add() was
3568 * also called manually.
3569 */
device_del(struct device * dev)3570 void device_del(struct device *dev)
3571 {
3572 struct device *parent = dev->parent;
3573 struct kobject *glue_dir = NULL;
3574 struct class_interface *class_intf;
3575 unsigned int noio_flag;
3576
3577 device_lock(dev);
3578 kill_device(dev);
3579 device_unlock(dev);
3580
3581 if (dev->fwnode && dev->fwnode->dev == dev)
3582 dev->fwnode->dev = NULL;
3583
3584 /* Notify clients of device removal. This call must come
3585 * before dpm_sysfs_remove().
3586 */
3587 noio_flag = memalloc_noio_save();
3588 if (dev->bus)
3589 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
3590 BUS_NOTIFY_DEL_DEVICE, dev);
3591
3592 dpm_sysfs_remove(dev);
3593 if (parent)
3594 klist_del(&dev->p->knode_parent);
3595 if (MAJOR(dev->devt)) {
3596 devtmpfs_delete_node(dev);
3597 device_remove_sys_dev_entry(dev);
3598 device_remove_file(dev, &dev_attr_dev);
3599 }
3600 if (dev->class) {
3601 device_remove_class_symlinks(dev);
3602
3603 mutex_lock(&dev->class->p->mutex);
3604 /* notify any interfaces that the device is now gone */
3605 list_for_each_entry(class_intf,
3606 &dev->class->p->interfaces, node)
3607 if (class_intf->remove_dev)
3608 class_intf->remove_dev(dev, class_intf);
3609 /* remove the device from the class list */
3610 klist_del(&dev->p->knode_class);
3611 mutex_unlock(&dev->class->p->mutex);
3612 }
3613 device_remove_file(dev, &dev_attr_uevent);
3614 device_remove_attrs(dev);
3615 bus_remove_device(dev);
3616 device_pm_remove(dev);
3617 driver_deferred_probe_del(dev);
3618 device_platform_notify_remove(dev);
3619 device_links_purge(dev);
3620
3621 if (dev->bus)
3622 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
3623 BUS_NOTIFY_REMOVED_DEVICE, dev);
3624 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
3625 glue_dir = get_glue_dir(dev);
3626 kobject_del(&dev->kobj);
3627 cleanup_glue_dir(dev, glue_dir);
3628 memalloc_noio_restore(noio_flag);
3629 put_device(parent);
3630 }
3631 EXPORT_SYMBOL_GPL(device_del);
3632
3633 /**
3634 * device_unregister - unregister device from system.
3635 * @dev: device going away.
3636 *
3637 * We do this in two parts, like we do device_register(). First,
3638 * we remove it from all the subsystems with device_del(), then
3639 * we decrement the reference count via put_device(). If that
3640 * is the final reference count, the device will be cleaned up
3641 * via device_release() above. Otherwise, the structure will
3642 * stick around until the final reference to the device is dropped.
3643 */
device_unregister(struct device * dev)3644 void device_unregister(struct device *dev)
3645 {
3646 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
3647 device_del(dev);
3648 put_device(dev);
3649 }
3650 EXPORT_SYMBOL_GPL(device_unregister);
3651
prev_device(struct klist_iter * i)3652 static struct device *prev_device(struct klist_iter *i)
3653 {
3654 struct klist_node *n = klist_prev(i);
3655 struct device *dev = NULL;
3656 struct device_private *p;
3657
3658 if (n) {
3659 p = to_device_private_parent(n);
3660 dev = p->device;
3661 }
3662 return dev;
3663 }
3664
next_device(struct klist_iter * i)3665 static struct device *next_device(struct klist_iter *i)
3666 {
3667 struct klist_node *n = klist_next(i);
3668 struct device *dev = NULL;
3669 struct device_private *p;
3670
3671 if (n) {
3672 p = to_device_private_parent(n);
3673 dev = p->device;
3674 }
3675 return dev;
3676 }
3677
3678 /**
3679 * device_get_devnode - path of device node file
3680 * @dev: device
3681 * @mode: returned file access mode
3682 * @uid: returned file owner
3683 * @gid: returned file group
3684 * @tmp: possibly allocated string
3685 *
3686 * Return the relative path of a possible device node.
3687 * Non-default names may need to allocate a memory to compose
3688 * a name. This memory is returned in tmp and needs to be
3689 * freed by the caller.
3690 */
device_get_devnode(struct device * dev,umode_t * mode,kuid_t * uid,kgid_t * gid,const char ** tmp)3691 const char *device_get_devnode(struct device *dev,
3692 umode_t *mode, kuid_t *uid, kgid_t *gid,
3693 const char **tmp)
3694 {
3695 char *s;
3696
3697 *tmp = NULL;
3698
3699 /* the device type may provide a specific name */
3700 if (dev->type && dev->type->devnode)
3701 *tmp = dev->type->devnode(dev, mode, uid, gid);
3702 if (*tmp)
3703 return *tmp;
3704
3705 /* the class may provide a specific name */
3706 if (dev->class && dev->class->devnode)
3707 *tmp = dev->class->devnode(dev, mode);
3708 if (*tmp)
3709 return *tmp;
3710
3711 /* return name without allocation, tmp == NULL */
3712 if (strchr(dev_name(dev), '!') == NULL)
3713 return dev_name(dev);
3714
3715 /* replace '!' in the name with '/' */
3716 s = kstrdup(dev_name(dev), GFP_KERNEL);
3717 if (!s)
3718 return NULL;
3719 strreplace(s, '!', '/');
3720 return *tmp = s;
3721 }
3722
3723 /**
3724 * device_for_each_child - device child iterator.
3725 * @parent: parent struct device.
3726 * @fn: function to be called for each device.
3727 * @data: data for the callback.
3728 *
3729 * Iterate over @parent's child devices, and call @fn for each,
3730 * passing it @data.
3731 *
3732 * We check the return of @fn each time. If it returns anything
3733 * other than 0, we break out and return that value.
3734 */
device_for_each_child(struct device * parent,void * data,int (* fn)(struct device * dev,void * data))3735 int device_for_each_child(struct device *parent, void *data,
3736 int (*fn)(struct device *dev, void *data))
3737 {
3738 struct klist_iter i;
3739 struct device *child;
3740 int error = 0;
3741
3742 if (!parent->p)
3743 return 0;
3744
3745 klist_iter_init(&parent->p->klist_children, &i);
3746 while (!error && (child = next_device(&i)))
3747 error = fn(child, data);
3748 klist_iter_exit(&i);
3749 return error;
3750 }
3751 EXPORT_SYMBOL_GPL(device_for_each_child);
3752
3753 /**
3754 * device_for_each_child_reverse - device child iterator in reversed order.
3755 * @parent: parent struct device.
3756 * @fn: function to be called for each device.
3757 * @data: data for the callback.
3758 *
3759 * Iterate over @parent's child devices, and call @fn for each,
3760 * passing it @data.
3761 *
3762 * We check the return of @fn each time. If it returns anything
3763 * other than 0, we break out and return that value.
3764 */
device_for_each_child_reverse(struct device * parent,void * data,int (* fn)(struct device * dev,void * data))3765 int device_for_each_child_reverse(struct device *parent, void *data,
3766 int (*fn)(struct device *dev, void *data))
3767 {
3768 struct klist_iter i;
3769 struct device *child;
3770 int error = 0;
3771
3772 if (!parent->p)
3773 return 0;
3774
3775 klist_iter_init(&parent->p->klist_children, &i);
3776 while ((child = prev_device(&i)) && !error)
3777 error = fn(child, data);
3778 klist_iter_exit(&i);
3779 return error;
3780 }
3781 EXPORT_SYMBOL_GPL(device_for_each_child_reverse);
3782
3783 /**
3784 * device_find_child - device iterator for locating a particular device.
3785 * @parent: parent struct device
3786 * @match: Callback function to check device
3787 * @data: Data to pass to match function
3788 *
3789 * This is similar to the device_for_each_child() function above, but it
3790 * returns a reference to a device that is 'found' for later use, as
3791 * determined by the @match callback.
3792 *
3793 * The callback should return 0 if the device doesn't match and non-zero
3794 * if it does. If the callback returns non-zero and a reference to the
3795 * current device can be obtained, this function will return to the caller
3796 * and not iterate over any more devices.
3797 *
3798 * NOTE: you will need to drop the reference with put_device() after use.
3799 */
device_find_child(struct device * parent,void * data,int (* match)(struct device * dev,void * data))3800 struct device *device_find_child(struct device *parent, void *data,
3801 int (*match)(struct device *dev, void *data))
3802 {
3803 struct klist_iter i;
3804 struct device *child;
3805
3806 if (!parent)
3807 return NULL;
3808
3809 klist_iter_init(&parent->p->klist_children, &i);
3810 while ((child = next_device(&i)))
3811 if (match(child, data) && get_device(child))
3812 break;
3813 klist_iter_exit(&i);
3814 return child;
3815 }
3816 EXPORT_SYMBOL_GPL(device_find_child);
3817
3818 /**
3819 * device_find_child_by_name - device iterator for locating a child device.
3820 * @parent: parent struct device
3821 * @name: name of the child device
3822 *
3823 * This is similar to the device_find_child() function above, but it
3824 * returns a reference to a device that has the name @name.
3825 *
3826 * NOTE: you will need to drop the reference with put_device() after use.
3827 */
device_find_child_by_name(struct device * parent,const char * name)3828 struct device *device_find_child_by_name(struct device *parent,
3829 const char *name)
3830 {
3831 struct klist_iter i;
3832 struct device *child;
3833
3834 if (!parent)
3835 return NULL;
3836
3837 klist_iter_init(&parent->p->klist_children, &i);
3838 while ((child = next_device(&i)))
3839 if (sysfs_streq(dev_name(child), name) && get_device(child))
3840 break;
3841 klist_iter_exit(&i);
3842 return child;
3843 }
3844 EXPORT_SYMBOL_GPL(device_find_child_by_name);
3845
devices_init(void)3846 int __init devices_init(void)
3847 {
3848 devices_kset = kset_create_and_add("devices", &device_uevent_ops, NULL);
3849 if (!devices_kset)
3850 return -ENOMEM;
3851 dev_kobj = kobject_create_and_add("dev", NULL);
3852 if (!dev_kobj)
3853 goto dev_kobj_err;
3854 sysfs_dev_block_kobj = kobject_create_and_add("block", dev_kobj);
3855 if (!sysfs_dev_block_kobj)
3856 goto block_kobj_err;
3857 sysfs_dev_char_kobj = kobject_create_and_add("char", dev_kobj);
3858 if (!sysfs_dev_char_kobj)
3859 goto char_kobj_err;
3860
3861 return 0;
3862
3863 char_kobj_err:
3864 kobject_put(sysfs_dev_block_kobj);
3865 block_kobj_err:
3866 kobject_put(dev_kobj);
3867 dev_kobj_err:
3868 kset_unregister(devices_kset);
3869 return -ENOMEM;
3870 }
3871
device_check_offline(struct device * dev,void * not_used)3872 static int device_check_offline(struct device *dev, void *not_used)
3873 {
3874 int ret;
3875
3876 ret = device_for_each_child(dev, NULL, device_check_offline);
3877 if (ret)
3878 return ret;
3879
3880 return device_supports_offline(dev) && !dev->offline ? -EBUSY : 0;
3881 }
3882
3883 /**
3884 * device_offline - Prepare the device for hot-removal.
3885 * @dev: Device to be put offline.
3886 *
3887 * Execute the device bus type's .offline() callback, if present, to prepare
3888 * the device for a subsequent hot-removal. If that succeeds, the device must
3889 * not be used until either it is removed or its bus type's .online() callback
3890 * is executed.
3891 *
3892 * Call under device_hotplug_lock.
3893 */
device_offline(struct device * dev)3894 int device_offline(struct device *dev)
3895 {
3896 int ret;
3897
3898 if (dev->offline_disabled)
3899 return -EPERM;
3900
3901 ret = device_for_each_child(dev, NULL, device_check_offline);
3902 if (ret)
3903 return ret;
3904
3905 device_lock(dev);
3906 if (device_supports_offline(dev)) {
3907 if (dev->offline) {
3908 ret = 1;
3909 } else {
3910 ret = dev->bus->offline(dev);
3911 if (!ret) {
3912 kobject_uevent(&dev->kobj, KOBJ_OFFLINE);
3913 dev->offline = true;
3914 }
3915 }
3916 }
3917 device_unlock(dev);
3918
3919 return ret;
3920 }
3921
3922 /**
3923 * device_online - Put the device back online after successful device_offline().
3924 * @dev: Device to be put back online.
3925 *
3926 * If device_offline() has been successfully executed for @dev, but the device
3927 * has not been removed subsequently, execute its bus type's .online() callback
3928 * to indicate that the device can be used again.
3929 *
3930 * Call under device_hotplug_lock.
3931 */
device_online(struct device * dev)3932 int device_online(struct device *dev)
3933 {
3934 int ret = 0;
3935
3936 device_lock(dev);
3937 if (device_supports_offline(dev)) {
3938 if (dev->offline) {
3939 ret = dev->bus->online(dev);
3940 if (!ret) {
3941 kobject_uevent(&dev->kobj, KOBJ_ONLINE);
3942 dev->offline = false;
3943 }
3944 } else {
3945 ret = 1;
3946 }
3947 }
3948 device_unlock(dev);
3949
3950 return ret;
3951 }
3952
3953 struct root_device {
3954 struct device dev;
3955 struct module *owner;
3956 };
3957
to_root_device(struct device * d)3958 static inline struct root_device *to_root_device(struct device *d)
3959 {
3960 return container_of(d, struct root_device, dev);
3961 }
3962
root_device_release(struct device * dev)3963 static void root_device_release(struct device *dev)
3964 {
3965 kfree(to_root_device(dev));
3966 }
3967
3968 /**
3969 * __root_device_register - allocate and register a root device
3970 * @name: root device name
3971 * @owner: owner module of the root device, usually THIS_MODULE
3972 *
3973 * This function allocates a root device and registers it
3974 * using device_register(). In order to free the returned
3975 * device, use root_device_unregister().
3976 *
3977 * Root devices are dummy devices which allow other devices
3978 * to be grouped under /sys/devices. Use this function to
3979 * allocate a root device and then use it as the parent of
3980 * any device which should appear under /sys/devices/{name}
3981 *
3982 * The /sys/devices/{name} directory will also contain a
3983 * 'module' symlink which points to the @owner directory
3984 * in sysfs.
3985 *
3986 * Returns &struct device pointer on success, or ERR_PTR() on error.
3987 *
3988 * Note: You probably want to use root_device_register().
3989 */
__root_device_register(const char * name,struct module * owner)3990 struct device *__root_device_register(const char *name, struct module *owner)
3991 {
3992 struct root_device *root;
3993 int err = -ENOMEM;
3994
3995 root = kzalloc(sizeof(struct root_device), GFP_KERNEL);
3996 if (!root)
3997 return ERR_PTR(err);
3998
3999 err = dev_set_name(&root->dev, "%s", name);
4000 if (err) {
4001 kfree(root);
4002 return ERR_PTR(err);
4003 }
4004
4005 root->dev.release = root_device_release;
4006
4007 err = device_register(&root->dev);
4008 if (err) {
4009 put_device(&root->dev);
4010 return ERR_PTR(err);
4011 }
4012
4013 #ifdef CONFIG_MODULES /* gotta find a "cleaner" way to do this */
4014 if (owner) {
4015 struct module_kobject *mk = &owner->mkobj;
4016
4017 err = sysfs_create_link(&root->dev.kobj, &mk->kobj, "module");
4018 if (err) {
4019 device_unregister(&root->dev);
4020 return ERR_PTR(err);
4021 }
4022 root->owner = owner;
4023 }
4024 #endif
4025
4026 return &root->dev;
4027 }
4028 EXPORT_SYMBOL_GPL(__root_device_register);
4029
4030 /**
4031 * root_device_unregister - unregister and free a root device
4032 * @dev: device going away
4033 *
4034 * This function unregisters and cleans up a device that was created by
4035 * root_device_register().
4036 */
root_device_unregister(struct device * dev)4037 void root_device_unregister(struct device *dev)
4038 {
4039 struct root_device *root = to_root_device(dev);
4040
4041 if (root->owner)
4042 sysfs_remove_link(&root->dev.kobj, "module");
4043
4044 device_unregister(dev);
4045 }
4046 EXPORT_SYMBOL_GPL(root_device_unregister);
4047
4048
device_create_release(struct device * dev)4049 static void device_create_release(struct device *dev)
4050 {
4051 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
4052 kfree(dev);
4053 }
4054
4055 static __printf(6, 0) struct device *
device_create_groups_vargs(struct class * class,struct device * parent,dev_t devt,void * drvdata,const struct attribute_group ** groups,const char * fmt,va_list args)4056 device_create_groups_vargs(struct class *class, struct device *parent,
4057 dev_t devt, void *drvdata,
4058 const struct attribute_group **groups,
4059 const char *fmt, va_list args)
4060 {
4061 struct device *dev = NULL;
4062 int retval = -ENODEV;
4063
4064 if (class == NULL || IS_ERR(class))
4065 goto error;
4066
4067 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
4068 if (!dev) {
4069 retval = -ENOMEM;
4070 goto error;
4071 }
4072
4073 device_initialize(dev);
4074 dev->devt = devt;
4075 dev->class = class;
4076 dev->parent = parent;
4077 dev->groups = groups;
4078 dev->release = device_create_release;
4079 dev_set_drvdata(dev, drvdata);
4080
4081 retval = kobject_set_name_vargs(&dev->kobj, fmt, args);
4082 if (retval)
4083 goto error;
4084
4085 retval = device_add(dev);
4086 if (retval)
4087 goto error;
4088
4089 return dev;
4090
4091 error:
4092 put_device(dev);
4093 return ERR_PTR(retval);
4094 }
4095
4096 /**
4097 * device_create - creates a device and registers it with sysfs
4098 * @class: pointer to the struct class that this device should be registered to
4099 * @parent: pointer to the parent struct device of this new device, if any
4100 * @devt: the dev_t for the char device to be added
4101 * @drvdata: the data to be added to the device for callbacks
4102 * @fmt: string for the device's name
4103 *
4104 * This function can be used by char device classes. A struct device
4105 * will be created in sysfs, registered to the specified class.
4106 *
4107 * A "dev" file will be created, showing the dev_t for the device, if
4108 * the dev_t is not 0,0.
4109 * If a pointer to a parent struct device is passed in, the newly created
4110 * struct device will be a child of that device in sysfs.
4111 * The pointer to the struct device will be returned from the call.
4112 * Any further sysfs files that might be required can be created using this
4113 * pointer.
4114 *
4115 * Returns &struct device pointer on success, or ERR_PTR() on error.
4116 *
4117 * Note: the struct class passed to this function must have previously
4118 * been created with a call to class_create().
4119 */
device_create(struct class * class,struct device * parent,dev_t devt,void * drvdata,const char * fmt,...)4120 struct device *device_create(struct class *class, struct device *parent,
4121 dev_t devt, void *drvdata, const char *fmt, ...)
4122 {
4123 va_list vargs;
4124 struct device *dev;
4125
4126 va_start(vargs, fmt);
4127 dev = device_create_groups_vargs(class, parent, devt, drvdata, NULL,
4128 fmt, vargs);
4129 va_end(vargs);
4130 return dev;
4131 }
4132 EXPORT_SYMBOL_GPL(device_create);
4133
4134 /**
4135 * device_create_with_groups - creates a device and registers it with sysfs
4136 * @class: pointer to the struct class that this device should be registered to
4137 * @parent: pointer to the parent struct device of this new device, if any
4138 * @devt: the dev_t for the char device to be added
4139 * @drvdata: the data to be added to the device for callbacks
4140 * @groups: NULL-terminated list of attribute groups to be created
4141 * @fmt: string for the device's name
4142 *
4143 * This function can be used by char device classes. A struct device
4144 * will be created in sysfs, registered to the specified class.
4145 * Additional attributes specified in the groups parameter will also
4146 * be created automatically.
4147 *
4148 * A "dev" file will be created, showing the dev_t for the device, if
4149 * the dev_t is not 0,0.
4150 * If a pointer to a parent struct device is passed in, the newly created
4151 * struct device will be a child of that device in sysfs.
4152 * The pointer to the struct device will be returned from the call.
4153 * Any further sysfs files that might be required can be created using this
4154 * pointer.
4155 *
4156 * Returns &struct device pointer on success, or ERR_PTR() on error.
4157 *
4158 * Note: the struct class passed to this function must have previously
4159 * been created with a call to class_create().
4160 */
device_create_with_groups(struct class * class,struct device * parent,dev_t devt,void * drvdata,const struct attribute_group ** groups,const char * fmt,...)4161 struct device *device_create_with_groups(struct class *class,
4162 struct device *parent, dev_t devt,
4163 void *drvdata,
4164 const struct attribute_group **groups,
4165 const char *fmt, ...)
4166 {
4167 va_list vargs;
4168 struct device *dev;
4169
4170 va_start(vargs, fmt);
4171 dev = device_create_groups_vargs(class, parent, devt, drvdata, groups,
4172 fmt, vargs);
4173 va_end(vargs);
4174 return dev;
4175 }
4176 EXPORT_SYMBOL_GPL(device_create_with_groups);
4177
4178 /**
4179 * device_destroy - removes a device that was created with device_create()
4180 * @class: pointer to the struct class that this device was registered with
4181 * @devt: the dev_t of the device that was previously registered
4182 *
4183 * This call unregisters and cleans up a device that was created with a
4184 * call to device_create().
4185 */
device_destroy(struct class * class,dev_t devt)4186 void device_destroy(struct class *class, dev_t devt)
4187 {
4188 struct device *dev;
4189
4190 dev = class_find_device_by_devt(class, devt);
4191 if (dev) {
4192 put_device(dev);
4193 device_unregister(dev);
4194 }
4195 }
4196 EXPORT_SYMBOL_GPL(device_destroy);
4197
4198 /**
4199 * device_rename - renames a device
4200 * @dev: the pointer to the struct device to be renamed
4201 * @new_name: the new name of the device
4202 *
4203 * It is the responsibility of the caller to provide mutual
4204 * exclusion between two different calls of device_rename
4205 * on the same device to ensure that new_name is valid and
4206 * won't conflict with other devices.
4207 *
4208 * Note: Don't call this function. Currently, the networking layer calls this
4209 * function, but that will change. The following text from Kay Sievers offers
4210 * some insight:
4211 *
4212 * Renaming devices is racy at many levels, symlinks and other stuff are not
4213 * replaced atomically, and you get a "move" uevent, but it's not easy to
4214 * connect the event to the old and new device. Device nodes are not renamed at
4215 * all, there isn't even support for that in the kernel now.
4216 *
4217 * In the meantime, during renaming, your target name might be taken by another
4218 * driver, creating conflicts. Or the old name is taken directly after you
4219 * renamed it -- then you get events for the same DEVPATH, before you even see
4220 * the "move" event. It's just a mess, and nothing new should ever rely on
4221 * kernel device renaming. Besides that, it's not even implemented now for
4222 * other things than (driver-core wise very simple) network devices.
4223 *
4224 * We are currently about to change network renaming in udev to completely
4225 * disallow renaming of devices in the same namespace as the kernel uses,
4226 * because we can't solve the problems properly, that arise with swapping names
4227 * of multiple interfaces without races. Means, renaming of eth[0-9]* will only
4228 * be allowed to some other name than eth[0-9]*, for the aforementioned
4229 * reasons.
4230 *
4231 * Make up a "real" name in the driver before you register anything, or add
4232 * some other attributes for userspace to find the device, or use udev to add
4233 * symlinks -- but never rename kernel devices later, it's a complete mess. We
4234 * don't even want to get into that and try to implement the missing pieces in
4235 * the core. We really have other pieces to fix in the driver core mess. :)
4236 */
device_rename(struct device * dev,const char * new_name)4237 int device_rename(struct device *dev, const char *new_name)
4238 {
4239 struct kobject *kobj = &dev->kobj;
4240 char *old_device_name = NULL;
4241 int error;
4242
4243 dev = get_device(dev);
4244 if (!dev)
4245 return -EINVAL;
4246
4247 dev_dbg(dev, "renaming to %s\n", new_name);
4248
4249 old_device_name = kstrdup(dev_name(dev), GFP_KERNEL);
4250 if (!old_device_name) {
4251 error = -ENOMEM;
4252 goto out;
4253 }
4254
4255 if (dev->class) {
4256 error = sysfs_rename_link_ns(&dev->class->p->subsys.kobj,
4257 kobj, old_device_name,
4258 new_name, kobject_namespace(kobj));
4259 if (error)
4260 goto out;
4261 }
4262
4263 error = kobject_rename(kobj, new_name);
4264 if (error)
4265 goto out;
4266
4267 out:
4268 put_device(dev);
4269
4270 kfree(old_device_name);
4271
4272 return error;
4273 }
4274 EXPORT_SYMBOL_GPL(device_rename);
4275
device_move_class_links(struct device * dev,struct device * old_parent,struct device * new_parent)4276 static int device_move_class_links(struct device *dev,
4277 struct device *old_parent,
4278 struct device *new_parent)
4279 {
4280 int error = 0;
4281
4282 if (old_parent)
4283 sysfs_remove_link(&dev->kobj, "device");
4284 if (new_parent)
4285 error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
4286 "device");
4287 return error;
4288 }
4289
4290 /**
4291 * device_move - moves a device to a new parent
4292 * @dev: the pointer to the struct device to be moved
4293 * @new_parent: the new parent of the device (can be NULL)
4294 * @dpm_order: how to reorder the dpm_list
4295 */
device_move(struct device * dev,struct device * new_parent,enum dpm_order dpm_order)4296 int device_move(struct device *dev, struct device *new_parent,
4297 enum dpm_order dpm_order)
4298 {
4299 int error;
4300 struct device *old_parent;
4301 struct kobject *new_parent_kobj;
4302
4303 dev = get_device(dev);
4304 if (!dev)
4305 return -EINVAL;
4306
4307 device_pm_lock();
4308 new_parent = get_device(new_parent);
4309 new_parent_kobj = get_device_parent(dev, new_parent);
4310 if (IS_ERR(new_parent_kobj)) {
4311 error = PTR_ERR(new_parent_kobj);
4312 put_device(new_parent);
4313 goto out;
4314 }
4315
4316 pr_debug("device: '%s': %s: moving to '%s'\n", dev_name(dev),
4317 __func__, new_parent ? dev_name(new_parent) : "<NULL>");
4318 error = kobject_move(&dev->kobj, new_parent_kobj);
4319 if (error) {
4320 cleanup_glue_dir(dev, new_parent_kobj);
4321 put_device(new_parent);
4322 goto out;
4323 }
4324 old_parent = dev->parent;
4325 dev->parent = new_parent;
4326 if (old_parent)
4327 klist_remove(&dev->p->knode_parent);
4328 if (new_parent) {
4329 klist_add_tail(&dev->p->knode_parent,
4330 &new_parent->p->klist_children);
4331 set_dev_node(dev, dev_to_node(new_parent));
4332 }
4333
4334 if (dev->class) {
4335 error = device_move_class_links(dev, old_parent, new_parent);
4336 if (error) {
4337 /* We ignore errors on cleanup since we're hosed anyway... */
4338 device_move_class_links(dev, new_parent, old_parent);
4339 if (!kobject_move(&dev->kobj, &old_parent->kobj)) {
4340 if (new_parent)
4341 klist_remove(&dev->p->knode_parent);
4342 dev->parent = old_parent;
4343 if (old_parent) {
4344 klist_add_tail(&dev->p->knode_parent,
4345 &old_parent->p->klist_children);
4346 set_dev_node(dev, dev_to_node(old_parent));
4347 }
4348 }
4349 cleanup_glue_dir(dev, new_parent_kobj);
4350 put_device(new_parent);
4351 goto out;
4352 }
4353 }
4354 switch (dpm_order) {
4355 case DPM_ORDER_NONE:
4356 break;
4357 case DPM_ORDER_DEV_AFTER_PARENT:
4358 device_pm_move_after(dev, new_parent);
4359 devices_kset_move_after(dev, new_parent);
4360 break;
4361 case DPM_ORDER_PARENT_BEFORE_DEV:
4362 device_pm_move_before(new_parent, dev);
4363 devices_kset_move_before(new_parent, dev);
4364 break;
4365 case DPM_ORDER_DEV_LAST:
4366 device_pm_move_last(dev);
4367 devices_kset_move_last(dev);
4368 break;
4369 }
4370
4371 put_device(old_parent);
4372 out:
4373 device_pm_unlock();
4374 put_device(dev);
4375 return error;
4376 }
4377 EXPORT_SYMBOL_GPL(device_move);
4378
device_attrs_change_owner(struct device * dev,kuid_t kuid,kgid_t kgid)4379 static int device_attrs_change_owner(struct device *dev, kuid_t kuid,
4380 kgid_t kgid)
4381 {
4382 struct kobject *kobj = &dev->kobj;
4383 struct class *class = dev->class;
4384 const struct device_type *type = dev->type;
4385 int error;
4386
4387 if (class) {
4388 /*
4389 * Change the device groups of the device class for @dev to
4390 * @kuid/@kgid.
4391 */
4392 error = sysfs_groups_change_owner(kobj, class->dev_groups, kuid,
4393 kgid);
4394 if (error)
4395 return error;
4396 }
4397
4398 if (type) {
4399 /*
4400 * Change the device groups of the device type for @dev to
4401 * @kuid/@kgid.
4402 */
4403 error = sysfs_groups_change_owner(kobj, type->groups, kuid,
4404 kgid);
4405 if (error)
4406 return error;
4407 }
4408
4409 /* Change the device groups of @dev to @kuid/@kgid. */
4410 error = sysfs_groups_change_owner(kobj, dev->groups, kuid, kgid);
4411 if (error)
4412 return error;
4413
4414 if (device_supports_offline(dev) && !dev->offline_disabled) {
4415 /* Change online device attributes of @dev to @kuid/@kgid. */
4416 error = sysfs_file_change_owner(kobj, dev_attr_online.attr.name,
4417 kuid, kgid);
4418 if (error)
4419 return error;
4420 }
4421
4422 return 0;
4423 }
4424
4425 /**
4426 * device_change_owner - change the owner of an existing device.
4427 * @dev: device.
4428 * @kuid: new owner's kuid
4429 * @kgid: new owner's kgid
4430 *
4431 * This changes the owner of @dev and its corresponding sysfs entries to
4432 * @kuid/@kgid. This function closely mirrors how @dev was added via driver
4433 * core.
4434 *
4435 * Returns 0 on success or error code on failure.
4436 */
device_change_owner(struct device * dev,kuid_t kuid,kgid_t kgid)4437 int device_change_owner(struct device *dev, kuid_t kuid, kgid_t kgid)
4438 {
4439 int error;
4440 struct kobject *kobj = &dev->kobj;
4441
4442 dev = get_device(dev);
4443 if (!dev)
4444 return -EINVAL;
4445
4446 /*
4447 * Change the kobject and the default attributes and groups of the
4448 * ktype associated with it to @kuid/@kgid.
4449 */
4450 error = sysfs_change_owner(kobj, kuid, kgid);
4451 if (error)
4452 goto out;
4453
4454 /*
4455 * Change the uevent file for @dev to the new owner. The uevent file
4456 * was created in a separate step when @dev got added and we mirror
4457 * that step here.
4458 */
4459 error = sysfs_file_change_owner(kobj, dev_attr_uevent.attr.name, kuid,
4460 kgid);
4461 if (error)
4462 goto out;
4463
4464 /*
4465 * Change the device groups, the device groups associated with the
4466 * device class, and the groups associated with the device type of @dev
4467 * to @kuid/@kgid.
4468 */
4469 error = device_attrs_change_owner(dev, kuid, kgid);
4470 if (error)
4471 goto out;
4472
4473 error = dpm_sysfs_change_owner(dev, kuid, kgid);
4474 if (error)
4475 goto out;
4476
4477 #ifdef CONFIG_BLOCK
4478 if (sysfs_deprecated && dev->class == &block_class)
4479 goto out;
4480 #endif
4481
4482 /*
4483 * Change the owner of the symlink located in the class directory of
4484 * the device class associated with @dev which points to the actual
4485 * directory entry for @dev to @kuid/@kgid. This ensures that the
4486 * symlink shows the same permissions as its target.
4487 */
4488 error = sysfs_link_change_owner(&dev->class->p->subsys.kobj, &dev->kobj,
4489 dev_name(dev), kuid, kgid);
4490 if (error)
4491 goto out;
4492
4493 out:
4494 put_device(dev);
4495 return error;
4496 }
4497 EXPORT_SYMBOL_GPL(device_change_owner);
4498
4499 /**
4500 * device_shutdown - call ->shutdown() on each device to shutdown.
4501 */
device_shutdown(void)4502 void device_shutdown(void)
4503 {
4504 struct device *dev, *parent;
4505
4506 wait_for_device_probe();
4507 device_block_probing();
4508
4509 cpufreq_suspend();
4510
4511 spin_lock(&devices_kset->list_lock);
4512 /*
4513 * Walk the devices list backward, shutting down each in turn.
4514 * Beware that device unplug events may also start pulling
4515 * devices offline, even as the system is shutting down.
4516 */
4517 while (!list_empty(&devices_kset->list)) {
4518 dev = list_entry(devices_kset->list.prev, struct device,
4519 kobj.entry);
4520
4521 /*
4522 * hold reference count of device's parent to
4523 * prevent it from being freed because parent's
4524 * lock is to be held
4525 */
4526 parent = get_device(dev->parent);
4527 get_device(dev);
4528 /*
4529 * Make sure the device is off the kset list, in the
4530 * event that dev->*->shutdown() doesn't remove it.
4531 */
4532 list_del_init(&dev->kobj.entry);
4533 spin_unlock(&devices_kset->list_lock);
4534
4535 /* hold lock to avoid race with probe/release */
4536 if (parent)
4537 device_lock(parent);
4538 device_lock(dev);
4539
4540 /* Don't allow any more runtime suspends */
4541 pm_runtime_get_noresume(dev);
4542 pm_runtime_barrier(dev);
4543
4544 if (dev->class && dev->class->shutdown_pre) {
4545 if (initcall_debug)
4546 dev_info(dev, "shutdown_pre\n");
4547 dev->class->shutdown_pre(dev);
4548 }
4549 if (dev->bus && dev->bus->shutdown) {
4550 if (initcall_debug)
4551 dev_info(dev, "shutdown\n");
4552 dev->bus->shutdown(dev);
4553 } else if (dev->driver && dev->driver->shutdown) {
4554 if (initcall_debug)
4555 dev_info(dev, "shutdown\n");
4556 dev->driver->shutdown(dev);
4557 }
4558
4559 device_unlock(dev);
4560 if (parent)
4561 device_unlock(parent);
4562
4563 put_device(dev);
4564 put_device(parent);
4565
4566 spin_lock(&devices_kset->list_lock);
4567 }
4568 spin_unlock(&devices_kset->list_lock);
4569 }
4570
4571 /*
4572 * Device logging functions
4573 */
4574
4575 #ifdef CONFIG_PRINTK
4576 static void
set_dev_info(const struct device * dev,struct dev_printk_info * dev_info)4577 set_dev_info(const struct device *dev, struct dev_printk_info *dev_info)
4578 {
4579 const char *subsys;
4580
4581 memset(dev_info, 0, sizeof(*dev_info));
4582
4583 if (dev->class)
4584 subsys = dev->class->name;
4585 else if (dev->bus)
4586 subsys = dev->bus->name;
4587 else
4588 return;
4589
4590 strscpy(dev_info->subsystem, subsys, sizeof(dev_info->subsystem));
4591
4592 /*
4593 * Add device identifier DEVICE=:
4594 * b12:8 block dev_t
4595 * c127:3 char dev_t
4596 * n8 netdev ifindex
4597 * +sound:card0 subsystem:devname
4598 */
4599 if (MAJOR(dev->devt)) {
4600 char c;
4601
4602 if (strcmp(subsys, "block") == 0)
4603 c = 'b';
4604 else
4605 c = 'c';
4606
4607 snprintf(dev_info->device, sizeof(dev_info->device),
4608 "%c%u:%u", c, MAJOR(dev->devt), MINOR(dev->devt));
4609 } else if (strcmp(subsys, "net") == 0) {
4610 struct net_device *net = to_net_dev(dev);
4611
4612 snprintf(dev_info->device, sizeof(dev_info->device),
4613 "n%u", net->ifindex);
4614 } else {
4615 snprintf(dev_info->device, sizeof(dev_info->device),
4616 "+%s:%s", subsys, dev_name(dev));
4617 }
4618 }
4619
dev_vprintk_emit(int level,const struct device * dev,const char * fmt,va_list args)4620 int dev_vprintk_emit(int level, const struct device *dev,
4621 const char *fmt, va_list args)
4622 {
4623 struct dev_printk_info dev_info;
4624
4625 set_dev_info(dev, &dev_info);
4626
4627 return vprintk_emit(0, level, &dev_info, fmt, args);
4628 }
4629 EXPORT_SYMBOL(dev_vprintk_emit);
4630
dev_printk_emit(int level,const struct device * dev,const char * fmt,...)4631 int dev_printk_emit(int level, const struct device *dev, const char *fmt, ...)
4632 {
4633 va_list args;
4634 int r;
4635
4636 va_start(args, fmt);
4637
4638 r = dev_vprintk_emit(level, dev, fmt, args);
4639
4640 va_end(args);
4641
4642 return r;
4643 }
4644 EXPORT_SYMBOL(dev_printk_emit);
4645
__dev_printk(const char * level,const struct device * dev,struct va_format * vaf)4646 static void __dev_printk(const char *level, const struct device *dev,
4647 struct va_format *vaf)
4648 {
4649 if (dev)
4650 dev_printk_emit(level[1] - '0', dev, "%s %s: %pV",
4651 dev_driver_string(dev), dev_name(dev), vaf);
4652 else
4653 printk("%s(NULL device *): %pV", level, vaf);
4654 }
4655
_dev_printk(const char * level,const struct device * dev,const char * fmt,...)4656 void _dev_printk(const char *level, const struct device *dev,
4657 const char *fmt, ...)
4658 {
4659 struct va_format vaf;
4660 va_list args;
4661
4662 va_start(args, fmt);
4663
4664 vaf.fmt = fmt;
4665 vaf.va = &args;
4666
4667 __dev_printk(level, dev, &vaf);
4668
4669 va_end(args);
4670 }
4671 EXPORT_SYMBOL(_dev_printk);
4672
4673 #define define_dev_printk_level(func, kern_level) \
4674 void func(const struct device *dev, const char *fmt, ...) \
4675 { \
4676 struct va_format vaf; \
4677 va_list args; \
4678 \
4679 va_start(args, fmt); \
4680 \
4681 vaf.fmt = fmt; \
4682 vaf.va = &args; \
4683 \
4684 __dev_printk(kern_level, dev, &vaf); \
4685 \
4686 va_end(args); \
4687 } \
4688 EXPORT_SYMBOL(func);
4689
4690 define_dev_printk_level(_dev_emerg, KERN_EMERG);
4691 define_dev_printk_level(_dev_alert, KERN_ALERT);
4692 define_dev_printk_level(_dev_crit, KERN_CRIT);
4693 define_dev_printk_level(_dev_err, KERN_ERR);
4694 define_dev_printk_level(_dev_warn, KERN_WARNING);
4695 define_dev_printk_level(_dev_notice, KERN_NOTICE);
4696 define_dev_printk_level(_dev_info, KERN_INFO);
4697
4698 #endif
4699
4700 /**
4701 * dev_err_probe - probe error check and log helper
4702 * @dev: the pointer to the struct device
4703 * @err: error value to test
4704 * @fmt: printf-style format string
4705 * @...: arguments as specified in the format string
4706 *
4707 * This helper implements common pattern present in probe functions for error
4708 * checking: print debug or error message depending if the error value is
4709 * -EPROBE_DEFER and propagate error upwards.
4710 * In case of -EPROBE_DEFER it sets also defer probe reason, which can be
4711 * checked later by reading devices_deferred debugfs attribute.
4712 * It replaces code sequence::
4713 *
4714 * if (err != -EPROBE_DEFER)
4715 * dev_err(dev, ...);
4716 * else
4717 * dev_dbg(dev, ...);
4718 * return err;
4719 *
4720 * with::
4721 *
4722 * return dev_err_probe(dev, err, ...);
4723 *
4724 * Note that it is deemed acceptable to use this function for error
4725 * prints during probe even if the @err is known to never be -EPROBE_DEFER.
4726 * The benefit compared to a normal dev_err() is the standardized format
4727 * of the error code and the fact that the error code is returned.
4728 *
4729 * Returns @err.
4730 *
4731 */
dev_err_probe(const struct device * dev,int err,const char * fmt,...)4732 int dev_err_probe(const struct device *dev, int err, const char *fmt, ...)
4733 {
4734 struct va_format vaf;
4735 va_list args;
4736
4737 va_start(args, fmt);
4738 vaf.fmt = fmt;
4739 vaf.va = &args;
4740
4741 if (err != -EPROBE_DEFER) {
4742 dev_err(dev, "error %pe: %pV", ERR_PTR(err), &vaf);
4743 } else {
4744 device_set_deferred_probe_reason(dev, &vaf);
4745 dev_dbg(dev, "error %pe: %pV", ERR_PTR(err), &vaf);
4746 }
4747
4748 va_end(args);
4749
4750 return err;
4751 }
4752 EXPORT_SYMBOL_GPL(dev_err_probe);
4753
fwnode_is_primary(struct fwnode_handle * fwnode)4754 static inline bool fwnode_is_primary(struct fwnode_handle *fwnode)
4755 {
4756 return fwnode && !IS_ERR(fwnode->secondary);
4757 }
4758
4759 /**
4760 * set_primary_fwnode - Change the primary firmware node of a given device.
4761 * @dev: Device to handle.
4762 * @fwnode: New primary firmware node of the device.
4763 *
4764 * Set the device's firmware node pointer to @fwnode, but if a secondary
4765 * firmware node of the device is present, preserve it.
4766 *
4767 * Valid fwnode cases are:
4768 * - primary --> secondary --> -ENODEV
4769 * - primary --> NULL
4770 * - secondary --> -ENODEV
4771 * - NULL
4772 */
set_primary_fwnode(struct device * dev,struct fwnode_handle * fwnode)4773 void set_primary_fwnode(struct device *dev, struct fwnode_handle *fwnode)
4774 {
4775 struct device *parent = dev->parent;
4776 struct fwnode_handle *fn = dev->fwnode;
4777
4778 if (fwnode) {
4779 if (fwnode_is_primary(fn))
4780 fn = fn->secondary;
4781
4782 if (fn) {
4783 WARN_ON(fwnode->secondary);
4784 fwnode->secondary = fn;
4785 }
4786 dev->fwnode = fwnode;
4787 } else {
4788 if (fwnode_is_primary(fn)) {
4789 dev->fwnode = fn->secondary;
4790 /* Set fn->secondary = NULL, so fn remains the primary fwnode */
4791 if (!(parent && fn == parent->fwnode))
4792 fn->secondary = NULL;
4793 } else {
4794 dev->fwnode = NULL;
4795 }
4796 }
4797 }
4798 EXPORT_SYMBOL_GPL(set_primary_fwnode);
4799
4800 /**
4801 * set_secondary_fwnode - Change the secondary firmware node of a given device.
4802 * @dev: Device to handle.
4803 * @fwnode: New secondary firmware node of the device.
4804 *
4805 * If a primary firmware node of the device is present, set its secondary
4806 * pointer to @fwnode. Otherwise, set the device's firmware node pointer to
4807 * @fwnode.
4808 */
set_secondary_fwnode(struct device * dev,struct fwnode_handle * fwnode)4809 void set_secondary_fwnode(struct device *dev, struct fwnode_handle *fwnode)
4810 {
4811 if (fwnode)
4812 fwnode->secondary = ERR_PTR(-ENODEV);
4813
4814 if (fwnode_is_primary(dev->fwnode))
4815 dev->fwnode->secondary = fwnode;
4816 else
4817 dev->fwnode = fwnode;
4818 }
4819 EXPORT_SYMBOL_GPL(set_secondary_fwnode);
4820
4821 /**
4822 * device_set_of_node_from_dev - reuse device-tree node of another device
4823 * @dev: device whose device-tree node is being set
4824 * @dev2: device whose device-tree node is being reused
4825 *
4826 * Takes another reference to the new device-tree node after first dropping
4827 * any reference held to the old node.
4828 */
device_set_of_node_from_dev(struct device * dev,const struct device * dev2)4829 void device_set_of_node_from_dev(struct device *dev, const struct device *dev2)
4830 {
4831 of_node_put(dev->of_node);
4832 dev->of_node = of_node_get(dev2->of_node);
4833 dev->of_node_reused = true;
4834 }
4835 EXPORT_SYMBOL_GPL(device_set_of_node_from_dev);
4836
device_set_node(struct device * dev,struct fwnode_handle * fwnode)4837 void device_set_node(struct device *dev, struct fwnode_handle *fwnode)
4838 {
4839 dev->fwnode = fwnode;
4840 dev->of_node = to_of_node(fwnode);
4841 }
4842 EXPORT_SYMBOL_GPL(device_set_node);
4843
device_match_name(struct device * dev,const void * name)4844 int device_match_name(struct device *dev, const void *name)
4845 {
4846 return sysfs_streq(dev_name(dev), name);
4847 }
4848 EXPORT_SYMBOL_GPL(device_match_name);
4849
device_match_of_node(struct device * dev,const void * np)4850 int device_match_of_node(struct device *dev, const void *np)
4851 {
4852 return dev->of_node == np;
4853 }
4854 EXPORT_SYMBOL_GPL(device_match_of_node);
4855
device_match_fwnode(struct device * dev,const void * fwnode)4856 int device_match_fwnode(struct device *dev, const void *fwnode)
4857 {
4858 return dev_fwnode(dev) == fwnode;
4859 }
4860 EXPORT_SYMBOL_GPL(device_match_fwnode);
4861
device_match_devt(struct device * dev,const void * pdevt)4862 int device_match_devt(struct device *dev, const void *pdevt)
4863 {
4864 return dev->devt == *(dev_t *)pdevt;
4865 }
4866 EXPORT_SYMBOL_GPL(device_match_devt);
4867
device_match_acpi_dev(struct device * dev,const void * adev)4868 int device_match_acpi_dev(struct device *dev, const void *adev)
4869 {
4870 return ACPI_COMPANION(dev) == adev;
4871 }
4872 EXPORT_SYMBOL(device_match_acpi_dev);
4873
device_match_acpi_handle(struct device * dev,const void * handle)4874 int device_match_acpi_handle(struct device *dev, const void *handle)
4875 {
4876 return ACPI_HANDLE(dev) == handle;
4877 }
4878 EXPORT_SYMBOL(device_match_acpi_handle);
4879
device_match_any(struct device * dev,const void * unused)4880 int device_match_any(struct device *dev, const void *unused)
4881 {
4882 return 1;
4883 }
4884 EXPORT_SYMBOL_GPL(device_match_any);
4885