This allows specifying USB ports that should be observed by a trigger
right after activating it. Example:

usb {
        gpios = <&gpio 0 GPIO_ACTIVE_HIGH>;
        linux,default-trigger = "usbport";
        usb-controllers = <&ohci>, <&ehci>, <&xhci USB_HCD_SHARED>;
        usb-ports = "1", "1", "1";
};

Signed-off-by: Rafał Miłecki <zaj...@gmail.com>
---
 drivers/leds/trigger/ledtrig-usbport.c | 72 ++++++++++++++++++++++++++++++++++
 1 file changed, 72 insertions(+)

diff --git a/drivers/leds/trigger/ledtrig-usbport.c 
b/drivers/leds/trigger/ledtrig-usbport.c
index eb20a8f..5724f63 100644
--- a/drivers/leds/trigger/ledtrig-usbport.c
+++ b/drivers/leds/trigger/ledtrig-usbport.c
@@ -12,8 +12,10 @@
 #include <linux/device.h>
 #include <linux/leds.h>
 #include <linux/module.h>
+#include <linux/of.h>
 #include <linux/slab.h>
 #include <linux/usb.h>
+#include <linux/usb/provider.h>
 #include "../leds.h"
 
 struct usbport_trig_port {
@@ -94,6 +96,75 @@ static bool usbport_trig_match(struct usbport_trig_data 
*usbport_data,
        return false;
 }
 
+static int usbport_trig_new_port(struct usbport_trig_data *usbport_data,
+                                int busnum, const char *suffix)
+{
+       struct usbport_trig_port *port;
+       size_t len;
+       int tmp;
+
+       tmp = busnum;
+       len = 1;
+       while (tmp >= 10) {
+               tmp /= 10;
+               len++;
+       }
+       len++; /* '-' */
+       len += strlen(suffix);
+       len++; /* '\0' */
+
+       port = kzalloc(sizeof(*port), GFP_KERNEL);
+       if (!port)
+               return -ENOMEM;
+
+       port->name = kzalloc(len, GFP_KERNEL);
+       if (!port->name) {
+               kfree(port);
+               return -ENOMEM;
+       }
+       snprintf(port->name, len, "%d-%s", busnum, suffix);
+
+       list_add_tail(&port->list, &usbport_data->ports);
+
+       return 0;
+}
+
+static int usbport_trig_fill(struct usbport_trig_data *usbport_data)
+{
+       struct device_node *np = usbport_data->led_cdev->dev->of_node;
+       struct of_phandle_args args;
+       int count, i;
+       int err = 0;
+
+       if (!np)
+               return -ENOENT;
+
+       count = of_property_count_strings(np, "usb-ports");
+       if (count < 0)
+               return count;
+
+       for (i = 0; i < count; i++) {
+               const char *port;
+               struct usb_hcd *hcd;
+
+               err = of_property_read_string_index(np, "usb-ports", i, &port);
+               if (err)
+                       continue;
+
+               err = of_parse_phandle_with_args(np, "usb-controllers", 
"#usb-cells", i, &args);
+               if (err)
+                       continue;
+
+               hcd = of_hcd_get_from_provider(&args);
+               if (!IS_ERR(hcd))
+                       usbport_trig_new_port(usbport_data, hcd->self.busnum, 
port);
+
+               of_node_put(args.np);
+       }
+
+       return err;
+}
+
 static int usbport_trig_notify(struct notifier_block *nb, unsigned long action,
                               void *data)
 {
@@ -129,6 +200,7 @@ static void usbport_trig_activate(struct led_classdev 
*led_cdev)
                return;
        usbport_data->led_cdev = led_cdev;
        INIT_LIST_HEAD(&usbport_data->ports);
+       usbport_trig_fill(usbport_data);
        usbport_data->nb.notifier_call = usbport_trig_notify,
        led_cdev->trigger_data = usbport_data;
 
-- 
1.8.4.5

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

Reply via email to