[PATCH 2/2] leds: usb: Add LED trigger for USB host activity

From: Michal Sojka
Date: Fri Aug 22 2014 - 08:30:27 EST


With this patch, USB host activity can be signaled by blinking a LED.

This should work with all host controllers. Tested only with musb.

Signed-off-by: Michal Sojka <sojka@xxxxxxxxx>
---
drivers/leds/trigger/Kconfig | 8 ++++++
drivers/leds/trigger/Makefile | 1 +
drivers/leds/trigger/ledtrig-usbhost.c | 45 ++++++++++++++++++++++++++++++++++
drivers/usb/core/hcd.c | 3 +++
include/linux/leds.h | 6 +++++
5 files changed, 63 insertions(+)
create mode 100644 drivers/leds/trigger/ledtrig-usbhost.c

diff --git a/drivers/leds/trigger/Kconfig b/drivers/leds/trigger/Kconfig
index 9562963..528d736 100644
--- a/drivers/leds/trigger/Kconfig
+++ b/drivers/leds/trigger/Kconfig
@@ -49,6 +49,14 @@ config LEDS_TRIGGER_USBGADGET
This allows LEDs to be controlled by USB gadget activity.
If unsure, say Y.

+config LEDS_TRIGGER_USBHOST
+ bool "LED USB Host Trigger"
+ depends on USB
+ depends on LEDS_TRIGGERS
+ help
+ This allows LEDs to be controlled by USB host activity.
+ If unsure, say Y.
+
config LEDS_TRIGGER_HEARTBEAT
tristate "LED Heartbeat Trigger"
depends on LEDS_TRIGGERS
diff --git a/drivers/leds/trigger/Makefile b/drivers/leds/trigger/Makefile
index 45917c0..9e9ca19 100644
--- a/drivers/leds/trigger/Makefile
+++ b/drivers/leds/trigger/Makefile
@@ -9,3 +9,4 @@ obj-$(CONFIG_LEDS_TRIGGER_DEFAULT_ON) += ledtrig-default-on.o
obj-$(CONFIG_LEDS_TRIGGER_TRANSIENT) += ledtrig-transient.o
obj-$(CONFIG_LEDS_TRIGGER_CAMERA) += ledtrig-camera.o
obj-$(CONFIG_LEDS_TRIGGER_USBGADGET) += ledtrig-usbgadget.o
+obj-$(CONFIG_LEDS_TRIGGER_USBHOST) += ledtrig-usbhost.o
diff --git a/drivers/leds/trigger/ledtrig-usbhost.c b/drivers/leds/trigger/ledtrig-usbhost.c
new file mode 100644
index 0000000..0a12f67
--- /dev/null
+++ b/drivers/leds/trigger/ledtrig-usbhost.c
@@ -0,0 +1,45 @@
+/*
+ * LED Trigger for USB Host Activity
+ *
+ * Copyright 2014 Michal Sojka <sojka@xxxxxxxxx>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ */
+
+#include <linux/module.h>
+#include <linux/kernel.h>
+#include <linux/init.h>
+#include <linux/leds.h>
+
+#define BLINK_DELAY 30
+
+DEFINE_LED_TRIGGER(ledtrig_usbhost);
+static unsigned long usbhost_blink_delay = BLINK_DELAY;
+
+void ledtrig_usbhost_activity(void)
+{
+ led_trigger_blink_oneshot(ledtrig_usbhost,
+ &usbhost_blink_delay, &usbhost_blink_delay, 0);
+}
+EXPORT_SYMBOL(ledtrig_usbhost_activity);
+
+static int __init ledtrig_usbhost_init(void)
+{
+ led_trigger_register_simple("usb-host", &ledtrig_usbhost);
+ return 0;
+}
+
+static void __exit ledtrig_usbhost_exit(void)
+{
+ led_trigger_unregister_simple(ledtrig_usbhost);
+}
+
+module_init(ledtrig_usbhost_init);
+module_exit(ledtrig_usbhost_exit);
+
+MODULE_AUTHOR("Michal Sojka <sojka@xxxxxxxxx>");
+MODULE_DESCRIPTION("LED Trigger for USB Host Activity");
+MODULE_LICENSE("GPL");
diff --git a/drivers/usb/core/hcd.c b/drivers/usb/core/hcd.c
index 9c4e292..bcfea14 100644
--- a/drivers/usb/core/hcd.c
+++ b/drivers/usb/core/hcd.c
@@ -41,6 +41,7 @@
#include <linux/workqueue.h>
#include <linux/pm_runtime.h>
#include <linux/types.h>
+#include <linux/leds.h>

#include <linux/usb.h>
#include <linux/usb/hcd.h>
@@ -1664,6 +1665,8 @@ static void __usb_hcd_giveback_urb(struct urb *urb)
usbmon_urb_complete(&hcd->self, urb, status);
usb_anchor_suspend_wakeups(anchor);
usb_unanchor_urb(urb);
+ if (status == 0)
+ ledtrig_usbhost_activity();

/* pass ownership to the completion handler */
urb->status = status;
diff --git a/include/linux/leds.h b/include/linux/leds.h
index 5d9668e..5653495 100644
--- a/include/linux/leds.h
+++ b/include/linux/leds.h
@@ -224,6 +224,12 @@ extern void ledtrig_usbgadget_activity(void);
static inline void ledtrig_usbgadget_activity(void) {}
#endif

+#ifdef CONFIG_LEDS_TRIGGER_USBHOST
+extern void ledtrig_usbhost_activity(void);
+#else
+static inline void ledtrig_usbhost_activity(void) {}
+#endif
+
#if defined(CONFIG_LEDS_TRIGGER_CAMERA) || defined(CONFIG_LEDS_TRIGGER_CAMERA_MODULE)
extern void ledtrig_flash_ctrl(bool on);
extern void ledtrig_torch_ctrl(bool on);
--
2.1.0

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/