From patchwork Mon Mar 6 09:41:11 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Florian Eckert X-Patchwork-Id: 659610 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 36EB7C6FD19 for ; Mon, 6 Mar 2023 09:41:36 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230093AbjCFJle (ORCPT ); Mon, 6 Mar 2023 04:41:34 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53318 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230039AbjCFJld (ORCPT ); Mon, 6 Mar 2023 04:41:33 -0500 Received: from mxout70.expurgate.net (mxout70.expurgate.net [194.37.255.70]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EF1012331C; Mon, 6 Mar 2023 01:41:28 -0800 (PST) Received: from [127.0.0.1] (helo=localhost) by relay.expurgate.net with smtp (Exim 4.92) (envelope-from ) id 1pZ7LP-000Y0b-QL; Mon, 06 Mar 2023 10:41:23 +0100 Received: from [195.243.126.94] (helo=securemail.tdt.de) by relay.expurgate.net with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1pZ7LP-000Qiw-3s; Mon, 06 Mar 2023 10:41:23 +0100 Received: from securemail.tdt.de (localhost [127.0.0.1]) by securemail.tdt.de (Postfix) with ESMTP id AD60824004B; Mon, 6 Mar 2023 10:41:22 +0100 (CET) Received: from mail.dev.tdt.de (unknown [10.2.4.42]) by securemail.tdt.de (Postfix) with ESMTP id 24372240040; Mon, 6 Mar 2023 10:41:22 +0100 (CET) Received: from localhost.localdomain (unknown [10.2.3.40]) by mail.dev.tdt.de (Postfix) with ESMTPSA id 51CE52E30C; Mon, 6 Mar 2023 10:41:20 +0100 (CET) From: Florian Eckert To: u.kleine-koenig@pengutronix.de, gregkh@linuxfoundation.org, jirislaby@kernel.org, pavel@ucw.cz, lee@kernel.org Cc: linux-kernel@vger.kernel.org, linux-leds@vger.kernel.org, Eckert.Florian@googlemail.com Subject: [PATCH v8 1/3] tty: new helper function tty_get_mget Date: Mon, 6 Mar 2023 10:41:11 +0100 Message-ID: <20230306094113.273988-2-fe@dev.tdt.de> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20230306094113.273988-1-fe@dev.tdt.de> References: <20230306094113.273988-1-fe@dev.tdt.de> MIME-Version: 1.0 X-purgate: clean X-purgate-ID: 151534::1678095683-10E724CC-149116E7/0/0 X-purgate-type: clean Precedence: bulk List-ID: X-Mailing-List: linux-leds@vger.kernel.org For a given struct tty_struct, this provides the appropriate tty line state flags needed to add more modes to the ledtrig-tty trigger. The new function is then used to get via tty_tiocmget() the different tty line states. Signed-off-by: Florian Eckert --- drivers/tty/tty_io.c | 28 ++++++++++++++++++++++------ include/linux/tty.h | 1 + 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/drivers/tty/tty_io.c b/drivers/tty/tty_io.c index 3149114bf130..a068b03a0828 100644 --- a/drivers/tty/tty_io.c +++ b/drivers/tty/tty_io.c @@ -2493,6 +2493,24 @@ static int send_break(struct tty_struct *tty, unsigned int duration) return retval; } +/** + * tty_get_mget - get modem status + * @tty: tty device + * + * Obtain the modem status bits from the tty driver if the feature + * is supported. Return -%ENOTTY if it is not available. + */ +int tty_get_mget(struct tty_struct *tty) +{ + int retval = -ENOTTY; + + if (tty->ops->tiocmget) + retval = tty->ops->tiocmget(tty); + + return retval; +} +EXPORT_SYMBOL_GPL(tty_get_mget); + /** * tty_tiocmget - get modem status * @tty: tty device @@ -2505,14 +2523,12 @@ static int send_break(struct tty_struct *tty, unsigned int duration) */ static int tty_tiocmget(struct tty_struct *tty, int __user *p) { - int retval = -ENOTTY; + int retval; - if (tty->ops->tiocmget) { - retval = tty->ops->tiocmget(tty); + retval = tty_get_mget(tty); + if (retval >= 0) + retval = put_user(retval, p); - if (retval >= 0) - retval = put_user(retval, p); - } return retval; } diff --git a/include/linux/tty.h b/include/linux/tty.h index 730c3301d710..825186c0fec1 100644 --- a/include/linux/tty.h +++ b/include/linux/tty.h @@ -421,6 +421,7 @@ int tty_unthrottle_safe(struct tty_struct *tty); int tty_do_resize(struct tty_struct *tty, struct winsize *ws); int tty_get_icount(struct tty_struct *tty, struct serial_icounter_struct *icount); +int tty_get_mget(struct tty_struct *tty); int is_current_pgrp_orphaned(void); void tty_hangup(struct tty_struct *tty); void tty_vhangup(struct tty_struct *tty); From patchwork Mon Mar 6 09:41:12 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Florian Eckert X-Patchwork-Id: 659609 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 904D8C678D4 for ; Mon, 6 Mar 2023 09:41:38 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230039AbjCFJlg (ORCPT ); Mon, 6 Mar 2023 04:41:36 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53348 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230100AbjCFJlf (ORCPT ); Mon, 6 Mar 2023 04:41:35 -0500 Received: from mxout70.expurgate.net (mxout70.expurgate.net [194.37.255.70]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EF742233E9; Mon, 6 Mar 2023 01:41:28 -0800 (PST) Received: from [127.0.0.1] (helo=localhost) by relay.expurgate.net with smtp (Exim 4.92) (envelope-from ) id 1pZ7LQ-000Y0o-8v; Mon, 06 Mar 2023 10:41:24 +0100 Received: from [195.243.126.94] (helo=securemail.tdt.de) by relay.expurgate.net with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1pZ7LP-000Qj2-Gw; Mon, 06 Mar 2023 10:41:23 +0100 Received: from securemail.tdt.de (localhost [127.0.0.1]) by securemail.tdt.de (Postfix) with ESMTP id 1CCB3240040; Mon, 6 Mar 2023 10:41:23 +0100 (CET) Received: from mail.dev.tdt.de (unknown [10.2.4.42]) by securemail.tdt.de (Postfix) with ESMTP id 83431240049; Mon, 6 Mar 2023 10:41:22 +0100 (CET) Received: from localhost.localdomain (unknown [10.2.3.40]) by mail.dev.tdt.de (Postfix) with ESMTPSA id 2503520E0D; Mon, 6 Mar 2023 10:41:22 +0100 (CET) From: Florian Eckert To: u.kleine-koenig@pengutronix.de, gregkh@linuxfoundation.org, jirislaby@kernel.org, pavel@ucw.cz, lee@kernel.org Cc: linux-kernel@vger.kernel.org, linux-leds@vger.kernel.org, Eckert.Florian@googlemail.com Subject: [PATCH v8 2/3] trigger: ledtrig-tty: add additional modes Date: Mon, 6 Mar 2023 10:41:12 +0100 Message-ID: <20230306094113.273988-3-fe@dev.tdt.de> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20230306094113.273988-1-fe@dev.tdt.de> References: <20230306094113.273988-1-fe@dev.tdt.de> MIME-Version: 1.0 X-purgate-type: clean X-purgate-ID: 151534::1678095684-10E724CC-620D8BE6/0/0 X-purgate: clean Precedence: bulk List-ID: X-Mailing-List: linux-leds@vger.kernel.org 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//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 --- .../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: linux-leds@vger.kernel.org Description: Specifies the tty device name of the triggering tty + +What: /sys/class/leds//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 #include +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); From patchwork Mon Mar 6 09:41:13 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Florian Eckert X-Patchwork-Id: 660295 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 53197C61DA4 for ; Mon, 6 Mar 2023 09:41:34 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229740AbjCFJld (ORCPT ); Mon, 6 Mar 2023 04:41:33 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53276 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229749AbjCFJlc (ORCPT ); Mon, 6 Mar 2023 04:41:32 -0500 Received: from mxout70.expurgate.net (mxout70.expurgate.net [91.198.224.70]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 20C94233CB; Mon, 6 Mar 2023 01:41:28 -0800 (PST) Received: from [127.0.0.1] (helo=localhost) by relay.expurgate.net with smtp (Exim 4.92) (envelope-from ) id 1pZ7LQ-0002zt-Da; Mon, 06 Mar 2023 10:41:24 +0100 Received: from [195.243.126.94] (helo=securemail.tdt.de) by relay.expurgate.net with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1pZ7LP-000Ran-Mq; Mon, 06 Mar 2023 10:41:23 +0100 Received: from securemail.tdt.de (localhost [127.0.0.1]) by securemail.tdt.de (Postfix) with ESMTP id 53421240049; Mon, 6 Mar 2023 10:41:23 +0100 (CET) Received: from mail.dev.tdt.de (unknown [10.2.4.42]) by securemail.tdt.de (Postfix) with ESMTP id BB57A24004D; Mon, 6 Mar 2023 10:41:22 +0100 (CET) Received: from localhost.localdomain (unknown [10.2.3.40]) by mail.dev.tdt.de (Postfix) with ESMTPSA id 8346A2E30C; Mon, 6 Mar 2023 10:41:22 +0100 (CET) From: Florian Eckert To: u.kleine-koenig@pengutronix.de, gregkh@linuxfoundation.org, jirislaby@kernel.org, pavel@ucw.cz, lee@kernel.org Cc: linux-kernel@vger.kernel.org, linux-leds@vger.kernel.org, Eckert.Florian@googlemail.com Subject: [PATCH v8 3/3] ledtrig-tty: call correct brightness set function Date: Mon, 6 Mar 2023 10:41:13 +0100 Message-ID: <20230306094113.273988-4-fe@dev.tdt.de> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20230306094113.273988-1-fe@dev.tdt.de> References: <20230306094113.273988-1-fe@dev.tdt.de> MIME-Version: 1.0 X-purgate-ID: 151534::1678095684-535C951F-3B7F81B3/0/0 X-purgate-type: clean X-purgate: clean Precedence: bulk List-ID: X-Mailing-List: linux-leds@vger.kernel.org The problem is, however, that with LEDs that are connected directly to the SOC via a GPIO, the LED cannot be switched on or off. Because the 'leds-gpio' device driver does not need a delayed work. If we look at the callstack for the function 'led_set_brightness_sync', then at the end it is checked whether the LED device driver has the function 'brightness_set_blocking' implemented. This is not the case for the 'leds-gpio' device. The function 'brightness_set_blocking' returns LED devices -ENOTSUPP. This is not evaluated in 'ledtrig-tty' trigger. In order for the trigger to also work with LED that does not have the 'brightness_set_blocking' function implemented, we must therefore use the internal API of the LED subsystem and use the function 'led_set_brightness_nosleep'. Signed-off-by: Florian Eckert --- drivers/leds/trigger/ledtrig-tty.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/drivers/leds/trigger/ledtrig-tty.c b/drivers/leds/trigger/ledtrig-tty.c index d3407830fa69..132e809898a7 100644 --- a/drivers/leds/trigger/ledtrig-tty.c +++ b/drivers/leds/trigger/ledtrig-tty.c @@ -7,6 +7,8 @@ #include #include +#include "../leds.h" + enum tty_led_mode { TTY_LED_RXTX, TTY_LED_CTS, @@ -164,9 +166,9 @@ static void ledtrig_tty_flags(struct ledtrig_tty_data *trigger_data, unsigned in status = tty_get_mget(trigger_data->tty); if (status & flag) - led_set_brightness_sync(trigger_data->led_cdev, LED_ON); + led_set_brightness_nosleep(trigger_data->led_cdev, LED_ON); else - led_set_brightness_sync(trigger_data->led_cdev, LED_OFF); + led_set_brightness_nosleep(trigger_data->led_cdev, LED_OFF); } static void ledtrig_tty_work(struct work_struct *work) @@ -229,12 +231,12 @@ static void ledtrig_tty_work(struct work_struct *work) } if (icount.rx != trigger_data->rx || icount.tx != trigger_data->tx) { - led_set_brightness_sync(trigger_data->led_cdev, LED_ON); + led_set_brightness_nosleep(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); + led_set_brightness_nosleep(trigger_data->led_cdev, LED_OFF); } break; }