<br><br>On Monday, May 3, 2021, Hans de Goede <<a href="mailto:hdegoede@redhat.com">hdegoede@redhat.com</a>> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Add a function to find a connector based on a fwnode.<br>
<br>
This will be used by the new drm_connector_oob_hotplug_<wbr>event()<br>
function which is added by the next patch in this patch-set.<br>
<br>
Changes in v2:<br>
- Complete rewrite to use a global connector list in drm_connector.c<br>
  rather then using a class-dev-iter in drm_sysfs.c<br>
<br>
Signed-off-by: Hans de Goede <<a href="mailto:hdegoede@redhat.com">hdegoede@redhat.com</a>><br>
---<br>
 drivers/gpu/drm/drm_connector.<wbr>c     | 50 +++++++++++++++++++++++++++++<br>
 drivers/gpu/drm/drm_crtc_<wbr>internal.h |  1 +<br>
 include/drm/drm_connector.h         |  8 +++++<br>
 3 files changed, 59 insertions(+)<br>
<br>
diff --git a/drivers/gpu/drm/drm_<wbr>connector.c b/drivers/gpu/drm/drm_<wbr>connector.c<br>
index 87c68563e6c3..ef759d6add81 100644<br>
--- a/drivers/gpu/drm/drm_<wbr>connector.c<br>
+++ b/drivers/gpu/drm/drm_<wbr>connector.c<br>
@@ -66,6 +66,14 @@<br>
  * support can instead use e.g. drm_helper_hpd_irq_event().<br>
  */<br>
<br>
+/*<br>
+ * Global connector list for drm_connector_find_by_fwnode()<wbr>.<br>
+ * Note drm_connector_[un]register() first take connector->lock and then<br>
+ * take the connector_list_lock.<br>
+ */<br>
+static DEFINE_MUTEX(connector_list_<wbr>lock);<br>
+static LIST_HEAD(connector_list);<br>
+<br>
 struct drm_conn_prop_enum_list {<br>
        int type;<br>
        const char *name;<br>
@@ -267,6 +275,7 @@ int drm_connector_init(struct drm_device *dev,<br>
                goto out_put_type_id;<br>
        }<br>
<br>
+       INIT_LIST_HEAD(&connector-><wbr>global_connector_list_entry);<br>
        INIT_LIST_HEAD(&connector-><wbr>probed_modes);<br>
        INIT_LIST_HEAD(&connector-><wbr>modes);<br>
        mutex_init(&connector->mutex);<br>
@@ -540,6 +549,9 @@ int drm_connector_register(struct drm_connector *connector)<br>
                drm_privacy_screen_register_<wbr>notifier(connector->privacy_<wbr>screen,<br>
                                           &connector->privacy_screen_<wbr>notifier);<br>
<br>
+       mutex_lock(&connector_list_<wbr>lock);<br>
+       list_add_tail(&connector-><wbr>global_connector_list_entry, &connector_list);<br>
+       mutex_unlock(&connector_list_<wbr>lock);<br>
        goto unlock;<br>
<br>
 err_debugfs:<br>
@@ -568,6 +580,10 @@ void drm_connector_unregister(<wbr>struct drm_connector *connector)<br>
                return;<br>
        }<br>
<br>
+       mutex_lock(&connector_list_<wbr>lock);<br>
+       list_del_init(&connector-><wbr>global_connector_list_entry);<br>
+       mutex_unlock(&connector_list_<wbr>lock);<br>
+<br>
        if (connector->privacy_screen)<br>
                drm_privacy_screen_unregister_<wbr>notifier(<br>
                                        connector->privacy_screen,<br>
@@ -2676,6 +2692,40 @@ int drm_mode_getconnector(struct drm_device *dev, void *data,<br>
        return ret;<br>
 }<br>
<br>
+/**<br>
+ * drm_connector_find_by_fwnode - Find a connector based on the associated fwnode<br>
+ * @fwnode: fwnode for which to find the matching drm_connector<br>
+ *<br>
+ * This functions looks up a drm_connector based on its associated fwnode. When<br>
+ * a connector is found a reference to the connector is returned. The caller must<br>
+ * call drm_connector_put() to release this reference when it is done with the<br>
+ * connector.<br>
+ *<br>
+ * Returns: A reference to the found connector or an ERR_PTR().<br>
+ */<br>
+struct drm_connector *drm_connector_find_by_fwnode(<wbr>struct fwnode_handle *fwnode)<br>
+{<br>
+       struct drm_connector *connector, *found = ERR_PTR(-ENODEV);<br>
+<br>
+       if (!fwnode)<br>
+               return ERR_PTR(-ENODEV);<br>
+<br>
+       mutex_lock(&connector_list_<wbr>lock);<br>
+<br>
+       list_for_each_entry(connector, &connector_list, global_connector_list_entry) {<br>
+               if (connector->fwnode == fwnode ||<br>
+                   (connector->fwnode && connector->fwnode->secondary == fwnode)) {<br>
+                       drm_connector_get(connector);<br>
+                       found = connector;<br>
+                       break;<br>
+               }<br>
+       }<br>
+<br>
+       mutex_unlock(&connector_list_<wbr>lock);<br>
+<br>
+       return found;</blockquote><div><br></div><div><br></div><div>If I am not mistaken you can replace this with</div><div><br></div><div>return list_entry_is_head();</div><div><br></div><div>call and remove additional Boolean variable.</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
+}<br>
+<br>
<br>
 /**<br>
  * DOC: Tile group<br>
diff --git a/drivers/gpu/drm/drm_crtc_<wbr>internal.h b/drivers/gpu/drm/drm_crtc_<wbr>internal.h<br>
index 54d4cf1233e9..6e28fc00a740 100644<br>
--- a/drivers/gpu/drm/drm_crtc_<wbr>internal.h<br>
+++ b/drivers/gpu/drm/drm_crtc_<wbr>internal.h<br>
@@ -185,6 +185,7 @@ int drm_connector_set_obj_prop(<wbr>struct drm_mode_object *obj,<br>
 int drm_connector_create_standard_<wbr>properties(struct drm_device *dev);<br>
 const char *drm_get_connector_force_name(<wbr>enum drm_connector_force force);<br>
 void drm_connector_free_work_fn(<wbr>struct work_struct *work);<br>
+struct drm_connector *drm_connector_find_by_fwnode(<wbr>struct fwnode_handle *fwnode);<br>
<br>
 /* IOCTL */<br>
 int drm_connector_property_set_<wbr>ioctl(struct drm_device *dev,<br>
diff --git a/include/drm/drm_connector.h b/include/drm/drm_connector.h<br>
index d20bfd7576ed..ae377354e48e 100644<br>
--- a/include/drm/drm_connector.h<br>
+++ b/include/drm/drm_connector.h<br>
@@ -1267,6 +1267,14 @@ struct drm_connector {<br>
         */<br>
        struct list_head head;<br>
<br>
+       /**<br>
+        * @global_connector_list_entry:<br>
+        *<br>
+        * Connector entry in the global connector-list, used by<br>
+        * drm_connector_find_by_fwnode()<wbr>.<br>
+        */<br>
+       struct list_head global_connector_list_entry;<br>
+<br>
        /** @base: base KMS object */<br>
        struct drm_mode_object base;<br>
 <br>
-- <br>
2.31.1<br>
<br>
</blockquote><br><br>-- <br>With Best Regards,<br>Andy Shevchenko<br><br><br>