[PATCH 1/2] serial: core: add LED trigger support

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

 



This patch adds initial LED trigger support for serial ports by
registering RX/TX/RXTX LED triggers for each port.

UART drivers now simply need to call uart_led_event() in their respective
receive and transmit routines to support the LED triggers.
---
 drivers/tty/serial/Kconfig       | 12 ++++++++
 drivers/tty/serial/Makefile      |  1 +
 drivers/tty/serial/serial_core.c |  7 +++++
 drivers/tty/serial/serial_leds.c | 64 ++++++++++++++++++++++++++++++++++++++++
 include/linux/serial_core.h      | 13 ++++++++
 include/linux/serial_leds.h      | 40 +++++++++++++++++++++++++
 6 files changed, 137 insertions(+)
 create mode 100644 drivers/tty/serial/serial_leds.c
 create mode 100644 include/linux/serial_leds.h

diff --git a/drivers/tty/serial/Kconfig b/drivers/tty/serial/Kconfig
index f38beb2..b178ac8 100644
--- a/drivers/tty/serial/Kconfig
+++ b/drivers/tty/serial/Kconfig
@@ -787,6 +787,18 @@ config SERIAL_CORE
 config SERIAL_CORE_CONSOLE
 	bool
 
+config SERIAL_LEDS
+	bool "Enable LED triggers for serial ports"
+	depends on LEDS_CLASS
+	select LEDS_TRIGGERS
+	---help---
+	  This option adds LED triggers for receiving and transmitting data
+	  from/to serial ports. Please note that not all drivers take
+	  advantage of this feature yet.
+
+	  Say Y here if you are working on a system with led-class supported
+	  LEDs and you want to use them as serial port activity indicators.
+
 config CONSOLE_POLL
 	bool
 
diff --git a/drivers/tty/serial/Makefile b/drivers/tty/serial/Makefile
index 5ab4111..e85b473 100644
--- a/drivers/tty/serial/Makefile
+++ b/drivers/tty/serial/Makefile
@@ -3,6 +3,7 @@
 #
 
 obj-$(CONFIG_SERIAL_CORE) += serial_core.o
+obj-$(CONFIG_SERIAL_LEDS) += serial_leds.o
 obj-$(CONFIG_SERIAL_21285) += 21285.o
 
 obj-$(CONFIG_SERIAL_EARLYCON) += earlycon.o
diff --git a/drivers/tty/serial/serial_core.c b/drivers/tty/serial/serial_core.c
index def5199..4a84f79 100644
--- a/drivers/tty/serial/serial_core.c
+++ b/drivers/tty/serial/serial_core.c
@@ -32,6 +32,7 @@
 #include <linux/device.h>
 #include <linux/serial.h> /* for serial_state and serial_icounter_struct */
 #include <linux/serial_core.h>
+#include <linux/serial_leds.h>
 #include <linux/delay.h>
 #include <linux/mutex.h>
 
@@ -2736,6 +2737,9 @@ int uart_add_one_port(struct uart_driver *drv, struct uart_port *uport)
 	 */
 	uport->flags &= ~UPF_DEAD;
 
+	/* Register LED triggers for port */
+	uart_led_register(drv, uport);
+
  out:
 	mutex_unlock(&port->mutex);
 	mutex_unlock(&port_mutex);
@@ -2797,6 +2801,9 @@ int uart_remove_one_port(struct uart_driver *drv, struct uart_port *uport)
 	if (uart_console(uport))
 		unregister_console(uport->cons);
 
+	/* Unregister LED triggers for port */
+	uart_led_unregister(uport);
+
 	/*
 	 * Free the port IO and memory resources, if any.
 	 */
diff --git a/drivers/tty/serial/serial_leds.c b/drivers/tty/serial/serial_leds.c
new file mode 100644
index 0000000..fe01fb3
--- /dev/null
+++ b/drivers/tty/serial/serial_leds.c
@@ -0,0 +1,64 @@
+/*
+ * Copyright 2016, Tobias Doerffel <tobias.doerffel@xxxxxxxxxxxxxx>
+ *
+ * This program is free software; you uart 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/leds.h>
+#include <linux/serial_leds.h>
+
+static unsigned long led_delay = 50;
+module_param(led_delay, ulong, 0644);
+MODULE_PARM_DESC(led_delay,
+		"blink delay time for activity leds (msecs, default: 50).");
+
+/* Trigger a LED event in response to a UART device event */
+void uart_led_event(struct uart_port *port, enum uart_led_event event)
+{
+	switch (event) {
+	case UART_LED_EVENT_TX:
+		if (led_delay) {
+			led_trigger_blink_oneshot(port->tx_led_trig,
+						  &led_delay, &led_delay, 1);
+			led_trigger_blink_oneshot(port->rxtx_led_trig,
+						  &led_delay, &led_delay, 1);
+		}
+		break;
+	case UART_LED_EVENT_RX:
+		if (led_delay) {
+			led_trigger_blink_oneshot(port->rx_led_trig,
+						  &led_delay, &led_delay, 1);
+			led_trigger_blink_oneshot(port->rxtx_led_trig,
+						  &led_delay, &led_delay, 1);
+		}
+		break;
+	}
+}
+EXPORT_SYMBOL_GPL(uart_led_event);
+
+void uart_led_register(struct uart_driver *drv, struct uart_port *port)
+{
+	snprintf(port->tx_led_trig_name, sizeof(port->tx_led_trig_name),
+		 "%s%d-tx", drv->dev_name, drv->tty_driver->name_base + port->line);
+	snprintf(port->rx_led_trig_name, sizeof(port->rx_led_trig_name),
+		 "%s%d-rx", drv->dev_name, drv->tty_driver->name_base + port->line);
+	snprintf(port->rxtx_led_trig_name, sizeof(port->rxtx_led_trig_name),
+		 "%s%d-rxtx", drv->dev_name, drv->tty_driver->name_base + port->line);
+
+	led_trigger_register_simple(port->tx_led_trig_name, &port->tx_led_trig);
+	led_trigger_register_simple(port->rx_led_trig_name, &port->rx_led_trig);
+	led_trigger_register_simple(port->rxtx_led_trig_name, &port->rxtx_led_trig);
+}
+EXPORT_SYMBOL_GPL(uart_led_register);
+
+void uart_led_unregister(struct uart_port *port)
+{
+	led_trigger_unregister_simple(port->tx_led_trig);
+	led_trigger_unregister_simple(port->rx_led_trig);
+	led_trigger_unregister_simple(port->rxtx_led_trig);
+}
+EXPORT_SYMBOL_GPL(uart_led_unregister);
+
diff --git a/include/linux/serial_core.h b/include/linux/serial_core.h
index 297d4fa..ace19f8 100644
--- a/include/linux/serial_core.h
+++ b/include/linux/serial_core.h
@@ -38,6 +38,11 @@
 #define uart_console(port)      ({ (void)port; 0; })
 #endif
 
+#define UART_DEV_NAME_LEN 32
+/* keep space for device name + "-tx"/"-rx"/"-rxtx" suffix and null terminator
+ */
+#define UART_LED_NAME_SZ	(UART_DEV_NAME_LEN + 6)
+
 struct uart_port;
 struct serial_struct;
 struct device;
@@ -246,6 +251,14 @@ struct uart_port {
 	struct attribute_group	*attr_group;		/* port specific attributes */
 	const struct attribute_group **tty_groups;	/* all attributes (serial core use only) */
 	struct serial_rs485     rs485;
+#ifdef CONFIG_SERIAL_LEDS
+	struct led_trigger *tx_led_trig;
+	char tx_led_trig_name[UART_LED_NAME_SZ];
+	struct led_trigger *rx_led_trig;
+	char rx_led_trig_name[UART_LED_NAME_SZ];
+	struct led_trigger *rxtx_led_trig;
+	char rxtx_led_trig_name[UART_LED_NAME_SZ];
+#endif
 	void			*private_data;		/* generic platform data pointer */
 };
 
diff --git a/include/linux/serial_leds.h b/include/linux/serial_leds.h
new file mode 100644
index 0000000..fc837f3
--- /dev/null
+++ b/include/linux/serial_leds.h
@@ -0,0 +1,40 @@
+/*
+ * Copyright 2016, Tobias Doerffel <tobias.doerffel@xxxxxxxxxxxxxx>
+ *
+ * This program is free software; you uart redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ */
+
+#ifndef _SERIAL_LEDS_H
+#define _SERIAL_LEDS_H
+
+#include <linux/serial_core.h>
+#include <linux/leds.h>
+
+enum uart_led_event {
+	UART_LED_EVENT_TX,
+	UART_LED_EVENT_RX,
+};
+
+#ifdef CONFIG_SERIAL_LEDS
+
+void uart_led_event(struct uart_port *port, enum uart_led_event event);
+void uart_led_register(struct uart_driver *drv, struct uart_port *port);
+void uart_led_unregister(struct uart_port *port);
+
+#else
+
+static inline void uart_led_event(struct uart_port *port, enum uart_led_event event)
+{
+}
+static inline void uart_led_register(struct uart_port *port)
+{
+}
+static inline void uart_led_unregister(struct uart_port *port)
+{
+}
+
+#endif
+
+#endif /* !_SERIAL_LEDS_H */
-- 
2.1.4

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



[Index of Archives]     [Kernel Newbies]     [Security]     [Netfilter]     [Bugtraq]     [Linux PPP]     [Linux FS]     [Yosemite News]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Samba]     [Video 4 Linux]     [Linmodem]     [Device Mapper]     [Linux Kernel for ARM]

  Powered by Linux