LCOV - code coverage report
Current view: top level - drivers/base - core.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 439 1638 26.8 %
Date: 2023-08-24 13:40:31 Functions: 50 177 28.2 %

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

Generated by: LCOV version 1.14