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