2023-03-06 09:41:41

by Florian Eckert

[permalink] [raw]
Subject: [PATCH v8 0/3] leds: ledtrig-tty: add tty_led_mode xtension

Hello,

here commes v8 of this series to add additional tty_led_modes.

v8:
Changes compared to the v7 patchset with
[email protected] are.

* Update 'Documentation/ABI/testing/sysfs-class-led-trigger-tty'
with the suggested changes.
* Use a meaningfull name for the Rx/Tx enum.
* Do not use C++ comments style

Add a new patch 'ledtrig-tty-fix-brightness-set' to fix an issue with
LEDs that are connected via GPIOs to the SOC with the device driver
'leds-gpio'. In my view, the wrong function is being used here. LEDs
that do not need and do not have a delayed call do not work with the
current implementation. Therefore, the 'led_set_brightness_nosleep'
function must be used.


v7:
Changes compared to the v5 patchset with
[email protected] are.

Addressed review comments by Jiri Slaby are:

Thanks for the hint with the command 'make htmldocs SPHINXDIRS="admin-guide"'.
Unfortunately, I did not know that. I have now verified it also in the
browser. In my opinion, the list is now also displayed correctly in
the documentation.


v6:
Changes compared to the v5 patchset with
[email protected] are.

Addressed review comments by kernel test robot are:

* fix Documentation/ABI/testing/sysfs-class-led-trigger-tty:9:
WARNING: Unexpected indentation.
* fix Documentation/ABI/testing/sysfs-class-led-trigger-tty:9:
WARNING: Block quote ends without a blank line; unexpected unindent.

Thanks to Jiri Slaby, who gave me the crucial hint of what I need to fix,
to possibly make the 'Kernel test robot' happy.


v5:
Changes compared to the v4 patchset with
[email protected] are.

Sorry for the inconvenience, but I sent the wrong patch for
ledtrig-tty.c in v4. The v5 patchset now includes all the changes I
specified in the v4 patchset.


v4:
Changes compared to the v3 patchset with
[email protected] are.

Addressed review comments by Jiri Slaby are:

ledtrig-tty.c:
- Do not use __TTY_LED_MAX pattern us instead __TTY_LED_LAST = TTY_LED_RNG
- Move declartion and assignment into one singel line
- Use __TTY_LED_LAST pattern, to simplify tty_mode_show and
tty_mode_store handling


v3:
Changes compared to the v2 patchset with
[email protected] are.

Addressed review comments by Greg K-H are:

tty.h:
- Fix first comment line and remark -%ENOTTY for the new function
'tty_get_mget' to make a proper kernel doc.
- Add the return value -%ENOTTY again, I thought it was no longer needed.


v2:
Changes compared to the initial patchset with
[email protected] are.

Addressed review comments by Jiri Slaby are:

tty.h:
- Fix compilation error because of wrong rebaseing
- Remove empty lines
- Use new 'tty_get_mget' in 'tty_tiocmget'

ledtrig-tty.c:
- Update commit description
- Use enum for tty_led_mod in struct ledtrig_tty_date
- Rename sysfs file from 'mode' to 'tty_led_mode'
- Change tty_led_mode show function to use loop instead of switch/case
- Change tty_led_mode store function to use loop instead of switch/case
- Check return value of function tty_get_mget

Florian Eckert (3):
tty: new helper function tty_get_mget
trigger: ledtrig-tty: add additional modes
ledtrig-tty: call correct brightness set function

.../ABI/testing/sysfs-class-led-trigger-tty | 18 +++
drivers/leds/trigger/ledtrig-tty.c | 145 ++++++++++++++++--
drivers/tty/tty_io.c | 28 +++-
include/linux/tty.h | 1 +
4 files changed, 171 insertions(+), 21 deletions(-)

--
2.30.2



2023-03-06 09:41:44

by Florian Eckert

[permalink] [raw]
Subject: [PATCH v8 2/3] trigger: ledtrig-tty: add additional modes

Add additional modes to trigger the selected LED.
The following modes are supported:

Tx/Rx: Flash LED on data transmission (default)
CTS: DCE Ready to accept data from the DTE.
DSR: DCE is ready to receive and send data.
CAR: DCE is receiving a carrier from a remote DTE.
RNG: DCE has detected an incoming ring signal.

The mode can be changed for example with the following command:
echo "CTS" > /sys/class/leds/<led>/mode

This would turn on the LED, when the DTE(modem) signals the DCE that it
is ready to accept data.

Signed-off-by: Florian Eckert <[email protected]>
---
.../ABI/testing/sysfs-class-led-trigger-tty | 18 +++
drivers/leds/trigger/ledtrig-tty.c | 143 ++++++++++++++++--
2 files changed, 146 insertions(+), 15 deletions(-)

diff --git a/Documentation/ABI/testing/sysfs-class-led-trigger-tty b/Documentation/ABI/testing/sysfs-class-led-trigger-tty
index 2bf6b24e781b..2e4e4c009a21 100644
--- a/Documentation/ABI/testing/sysfs-class-led-trigger-tty
+++ b/Documentation/ABI/testing/sysfs-class-led-trigger-tty
@@ -4,3 +4,21 @@ KernelVersion: 5.10
Contact: [email protected]
Description:
Specifies the tty device name of the triggering tty
+
+What: /sys/class/leds/<led>/mode
+Date: January 2023
+KernelVersion: 6.3
+Description:
+ Specifies the mode to trigger the LED.
+ The following operating modes are supported:
+
+ ===== ============================================
+ Tx/Rx Flash LED on data transmission (default)
+ CTS DCE Ready to accept data from the DTE
+ DSR DCE is ready to receive and send data
+ CAR DCE is receiving a carrier from a remote DTE
+ RNG DCE has detected an incoming ring signal
+ ===== ============================================
+
+ In CTS/DSR/CAR/RNG modes, the LED is enabled when the
+ tty line is high.
\ No newline at end of file
diff --git a/drivers/leds/trigger/ledtrig-tty.c b/drivers/leds/trigger/ledtrig-tty.c
index f62db7e520b5..d3407830fa69 100644
--- a/drivers/leds/trigger/ledtrig-tty.c
+++ b/drivers/leds/trigger/ledtrig-tty.c
@@ -7,6 +7,15 @@
#include <linux/tty.h>
#include <uapi/linux/serial.h>

+enum tty_led_mode {
+ TTY_LED_RXTX,
+ TTY_LED_CTS,
+ TTY_LED_DSR,
+ TTY_LED_CAR,
+ TTY_LED_RNG,
+ _TTY_LED_LAST = TTY_LED_RNG
+};
+
struct ledtrig_tty_data {
struct led_classdev *led_cdev;
struct delayed_work dwork;
@@ -14,6 +23,15 @@ struct ledtrig_tty_data {
const char *ttyname;
struct tty_struct *tty;
int rx, tx;
+ enum tty_led_mode mode;
+};
+
+static const char * const mode[] = {
+ [TTY_LED_RXTX] = "Tx/Rx", /* Transmit and Receive data */
+ [TTY_LED_CTS] = "CTS", /* CTS clear to send */
+ [TTY_LED_DSR] = "DSR", /* DSR data set ready */
+ [TTY_LED_CAR] = "CAR", /* CAR data carrier detect (DCD) */
+ [TTY_LED_RNG] = "RNG", /* RNG ring indicator */
};

static void ledtrig_tty_restart(struct ledtrig_tty_data *trigger_data)
@@ -21,6 +39,70 @@ static void ledtrig_tty_restart(struct ledtrig_tty_data *trigger_data)
schedule_delayed_work(&trigger_data->dwork, 0);
}

+static ssize_t ledtrig_tty_mode_show(char *buf, enum tty_led_mode tty_mode)
+{
+ int len = 0;
+ int i;
+
+ for (i = 0; i <= _TTY_LED_LAST; i++) {
+ bool hit = tty_mode == i;
+ bool last = i == _TTY_LED_LAST;
+
+ len += sysfs_emit_at(buf, len, "%s%s%s%s",
+ hit ? "[" : "",
+ mode[i],
+ hit ? "]" : "",
+ last ? "" : " ");
+ }
+
+ len += sysfs_emit_at(buf, len, "\n");
+
+ return len;
+}
+
+static ssize_t tty_led_mode_show(struct device *dev,
+ struct device_attribute *attr, char *buf)
+{
+ struct ledtrig_tty_data *trigger_data = led_trigger_get_drvdata(dev);
+ enum tty_led_mode tty_mode;
+
+ mutex_lock(&trigger_data->mutex);
+ tty_mode = trigger_data->mode;
+ mutex_unlock(&trigger_data->mutex);
+
+ return ledtrig_tty_mode_show(buf, tty_mode);
+}
+
+static ssize_t tty_led_mode_store(struct device *dev,
+ struct device_attribute *attr, const char *buf,
+ size_t size)
+{
+ struct ledtrig_tty_data *trigger_data = led_trigger_get_drvdata(dev);
+ ssize_t ret = size;
+ int i;
+ enum tty_led_mode tty_mode = _TTY_LED_LAST;
+
+ /* Check for new line in string */
+ if (size > 0 && buf[size - 1] == '\n')
+ size -= 1;
+
+ for (i = 0; i <= _TTY_LED_LAST; i++)
+ if (strncmp(buf, mode[i], size) == 0) {
+ tty_mode = i;
+ break;
+ }
+
+ if (i > _TTY_LED_LAST)
+ return -EINVAL;
+
+ mutex_lock(&trigger_data->mutex);
+ trigger_data->mode = tty_mode;
+ mutex_unlock(&trigger_data->mutex);
+
+ return ret;
+}
+static DEVICE_ATTR_RW(tty_led_mode);
+
static ssize_t ttyname_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
@@ -76,6 +158,17 @@ static ssize_t ttyname_store(struct device *dev,
}
static DEVICE_ATTR_RW(ttyname);

+static void ledtrig_tty_flags(struct ledtrig_tty_data *trigger_data, unsigned int flag)
+{
+ unsigned int status;
+
+ status = tty_get_mget(trigger_data->tty);
+ if (status & flag)
+ led_set_brightness_sync(trigger_data->led_cdev, LED_ON);
+ else
+ led_set_brightness_sync(trigger_data->led_cdev, LED_OFF);
+}
+
static void ledtrig_tty_work(struct work_struct *work)
{
struct ledtrig_tty_data *trigger_data =
@@ -113,21 +206,37 @@ static void ledtrig_tty_work(struct work_struct *work)
trigger_data->tty = tty;
}

- ret = tty_get_icount(trigger_data->tty, &icount);
- if (ret) {
- dev_info(trigger_data->tty->dev, "Failed to get icount, stopped polling\n");
- mutex_unlock(&trigger_data->mutex);
- return;
- }
-
- if (icount.rx != trigger_data->rx ||
- icount.tx != trigger_data->tx) {
- led_set_brightness_sync(trigger_data->led_cdev, LED_ON);
-
- trigger_data->rx = icount.rx;
- trigger_data->tx = icount.tx;
- } else {
- led_set_brightness_sync(trigger_data->led_cdev, LED_OFF);
+ switch (trigger_data->mode) {
+ case TTY_LED_CTS:
+ ledtrig_tty_flags(trigger_data, TIOCM_CTS);
+ break;
+ case TTY_LED_DSR:
+ ledtrig_tty_flags(trigger_data, TIOCM_DSR);
+ break;
+ case TTY_LED_CAR:
+ ledtrig_tty_flags(trigger_data, TIOCM_CAR);
+ break;
+ case TTY_LED_RNG:
+ ledtrig_tty_flags(trigger_data, TIOCM_RNG);
+ break;
+ case TTY_LED_RXTX:
+ default:
+ ret = tty_get_icount(trigger_data->tty, &icount);
+ if (ret) {
+ dev_info(trigger_data->tty->dev, "Failed to get icount, stopped polling\n");
+ mutex_unlock(&trigger_data->mutex);
+ return;
+ }
+
+ if (icount.rx != trigger_data->rx || icount.tx != trigger_data->tx) {
+ led_set_brightness_sync(trigger_data->led_cdev, LED_ON);
+
+ trigger_data->rx = icount.rx;
+ trigger_data->tx = icount.tx;
+ } else {
+ led_set_brightness_sync(trigger_data->led_cdev, LED_OFF);
+ }
+ break;
}

out:
@@ -137,6 +246,7 @@ static void ledtrig_tty_work(struct work_struct *work)

static struct attribute *ledtrig_tty_attrs[] = {
&dev_attr_ttyname.attr,
+ &dev_attr_tty_led_mode.attr,
NULL
};
ATTRIBUTE_GROUPS(ledtrig_tty);
@@ -149,6 +259,9 @@ static int ledtrig_tty_activate(struct led_classdev *led_cdev)
if (!trigger_data)
return -ENOMEM;

+ /* Set default mode */
+ trigger_data->mode = TTY_LED_RXTX;
+
led_set_trigger_data(led_cdev, trigger_data);

INIT_DELAYED_WORK(&trigger_data->dwork, ledtrig_tty_work);
--
2.30.2


2023-03-06 09:45:49

by Jiri Slaby

[permalink] [raw]
Subject: Re: [PATCH v8 0/3] leds: ledtrig-tty: add tty_led_mode xtension

On 06. 03. 23, 10:41, Florian Eckert wrote:
> Hello,
>
> here commes v8 of this series to add additional tty_led_modes.

I think u r 2 fast. Uwe had some more comments on v7.

Don't be afraid to wait some days after the last comments ;). As it is,
it makes the review harder...

> v8:
> Changes compared to the v7 patchset with
> [email protected] are.
>
> * Update 'Documentation/ABI/testing/sysfs-class-led-trigger-tty'
> with the suggested changes.
> * Use a meaningfull name for the Rx/Tx enum.
> * Do not use C++ comments style
>
> Add a new patch 'ledtrig-tty-fix-brightness-set' to fix an issue with
> LEDs that are connected via GPIOs to the SOC with the device driver
> 'leds-gpio'. In my view, the wrong function is being used here. LEDs
> that do not need and do not have a delayed call do not work with the
> current implementation. Therefore, the 'led_set_brightness_nosleep'
> function must be used.
>
>
> v7:
> Changes compared to the v5 patchset with
> [email protected] are.
>
> Addressed review comments by Jiri Slaby are:
>
> Thanks for the hint with the command 'make htmldocs SPHINXDIRS="admin-guide"'.
> Unfortunately, I did not know that. I have now verified it also in the
> browser. In my opinion, the list is now also displayed correctly in
> the documentation.
>
>
> v6:
> Changes compared to the v5 patchset with
> [email protected] are.
>
> Addressed review comments by kernel test robot are:
>
> * fix Documentation/ABI/testing/sysfs-class-led-trigger-tty:9:
> WARNING: Unexpected indentation.
> * fix Documentation/ABI/testing/sysfs-class-led-trigger-tty:9:
> WARNING: Block quote ends without a blank line; unexpected unindent.
>
> Thanks to Jiri Slaby, who gave me the crucial hint of what I need to fix,
> to possibly make the 'Kernel test robot' happy.
>
>
> v5:
> Changes compared to the v4 patchset with
> [email protected] are.
>
> Sorry for the inconvenience, but I sent the wrong patch for
> ledtrig-tty.c in v4. The v5 patchset now includes all the changes I
> specified in the v4 patchset.
>
>
> v4:
> Changes compared to the v3 patchset with
> [email protected] are.
>
> Addressed review comments by Jiri Slaby are:
>
> ledtrig-tty.c:
> - Do not use __TTY_LED_MAX pattern us instead __TTY_LED_LAST = TTY_LED_RNG
> - Move declartion and assignment into one singel line
> - Use __TTY_LED_LAST pattern, to simplify tty_mode_show and
> tty_mode_store handling
>
>
> v3:
> Changes compared to the v2 patchset with
> [email protected] are.
>
> Addressed review comments by Greg K-H are:
>
> tty.h:
> - Fix first comment line and remark -%ENOTTY for the new function
> 'tty_get_mget' to make a proper kernel doc.
> - Add the return value -%ENOTTY again, I thought it was no longer needed.
>
>
> v2:
> Changes compared to the initial patchset with
> [email protected] are.
>
> Addressed review comments by Jiri Slaby are:
>
> tty.h:
> - Fix compilation error because of wrong rebaseing
> - Remove empty lines
> - Use new 'tty_get_mget' in 'tty_tiocmget'
>
> ledtrig-tty.c:
> - Update commit description
> - Use enum for tty_led_mod in struct ledtrig_tty_date
> - Rename sysfs file from 'mode' to 'tty_led_mode'
> - Change tty_led_mode show function to use loop instead of switch/case
> - Change tty_led_mode store function to use loop instead of switch/case
> - Check return value of function tty_get_mget
>
> Florian Eckert (3):
> tty: new helper function tty_get_mget
> trigger: ledtrig-tty: add additional modes
> ledtrig-tty: call correct brightness set function
>
> .../ABI/testing/sysfs-class-led-trigger-tty | 18 +++
> drivers/leds/trigger/ledtrig-tty.c | 145 ++++++++++++++++--
> drivers/tty/tty_io.c | 28 +++-
> include/linux/tty.h | 1 +
> 4 files changed, 171 insertions(+), 21 deletions(-)
>

--
js
suse labs


2023-03-06 09:50:08

by Florian Eckert

[permalink] [raw]
Subject: Re: [PATCH v8 0/3] leds: ledtrig-tty: add tty_led_mode xtension


>>
>> here commes v8 of this series to add additional tty_led_modes.
>
> I think u r 2 fast. Uwe had some more comments on v7.
>
> Don't be afraid to wait some days after the last comments ;). As it
> is, it makes the review harder...
>

That was probably a race condition ;-) This must have been received when
I sent the v8.