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