Re: [PATCH v4] usb: of: add an api to get dr_mode by the phy node

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi,

> How about add a new function as follows to convert dr_mode from string to
> enum, then both Keikki's and my function will be shorter by calling it?
> 
> static enum usb_dr_mode of_usb_get_dr_mode_from_string(char *string)
> {
>          for (i = 0; i < ARRAY_SIZE(usb_dr_modes); i++)
>                  if (!strcmp(string, usb_dr_modes[i]))
>                          return i;
>          return USB_DR_MODE_UNKNOWN;
> }

I was thinking the same. Why not imlement the patch like so:

diff --git a/drivers/usb/common/common.c b/drivers/usb/common/common.c
index 673d530..cd9f6e9 100644
--- a/drivers/usb/common/common.c
+++ b/drivers/usb/common/common.c
@@ -106,25 +106,67 @@ static const char *const usb_dr_modes[] = {
        [USB_DR_MODE_OTG]               = "otg",
 };
 
+static enum usb_dr_mode find_dr_mode(const char *name)
+{
+       int i;
+
+       for (i = 0; i < ARRAY_SIZE(usb_dr_modes); i++)
+               if (!strcmp(name, usb_dr_modes[i]))
+                       return i;
+       return USB_DR_MODE_UNKNOWN;
+}
+
 enum usb_dr_mode usb_get_dr_mode(struct device *dev)
 {
        const char *dr_mode;
-       int err, i;
+       int err;
 
        err = device_property_read_string(dev, "dr_mode", &dr_mode);
        if (err < 0)
                return USB_DR_MODE_UNKNOWN;
-
-       for (i = 0; i < ARRAY_SIZE(usb_dr_modes); i++)
-               if (!strcmp(dr_mode, usb_dr_modes[i]))
-                       return i;
-
-       return USB_DR_MODE_UNKNOWN;
+       return find_dr_mode(dr_mode);
 }
 EXPORT_SYMBOL_GPL(usb_get_dr_mode);
 
 #ifdef CONFIG_OF
 /**
+ * of_usb_get_dr_mode_by_phy - Get dual role mode for the controller device
+ * which is associated with the given phy device_node
+ * @np:        Pointer to the given phy device_node
+ *
+ * In dts a usb controller associates with phy devices.  The function gets
+ * the string from property 'dr_mode' of the controller associated with the
+ * given phy device node, and returns the correspondig enum usb_dr_mode.
+ */
+enum usb_dr_mode of_usb_get_dr_mode_by_phy(struct device_node *phy_np)
+{
+       struct device_node *controller = NULL;
+       struct device_node *phy;
+       const char *dr_mode;
+       int index;
+       int err;
+
+       do {
+               controller = of_find_node_with_property(controller, "phys");
+               index = 0;
+               do {
+                       phy = of_parse_phandle(controller, "phys", index);
+                       of_node_put(phy);
+                       if (phy == phy_np)
+                               goto finish;
+                       index++;
+               } while (phy);
+       } while (controller);
+
+finish:
+       err = of_property_read_string(controller, "dr_mode", &dr_mode);
+       if (err < 0)
+               return USB_DR_MODE_UNKNOWN;
+       return find_dr_mode(dr_mode);
+}
+EXPORT_SYMBOL_GPL(of_usb_get_dr_mode_by_phy);
+
+/**
  * of_usb_host_tpl_support - to get if Targeted Peripheral List is supported
  * for given targeted hosts (non-PC hosts)
  * @np: Pointer to the given device_node


Cheers,

-- 
heikki
--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html



[Index of Archives]     [Linux Media]     [Linux Input]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]     [Linux SCSI]     [Old Linux USB Devel Archive]

  Powered by Linux