Received: by 2002:a05:7412:8521:b0:e2:908c:2ebd with SMTP id t33csp2088779rdf; Mon, 6 Nov 2023 04:36:11 -0800 (PST) X-Google-Smtp-Source: AGHT+IElk7UWKlXgqNvNBuI7FsN2zLW3VL7hMhixv5R9+wnm0F9cefz8xIdLQgGpY30qqPscF4tD X-Received: by 2002:a17:90a:31ca:b0:27d:c36:e12c with SMTP id j10-20020a17090a31ca00b0027d0c36e12cmr23619731pjf.9.1699274171053; Mon, 06 Nov 2023 04:36:11 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1699274171; cv=none; d=google.com; s=arc-20160816; b=uSa9L/SHS6PrmOKZO5x+fF/lVMWjQ0blNxeJwNEqg5MPU1KQQK4+BLi8DHDa8SrFUb fKMJ7zYsfSdPw5AMgTYx609ggUrXX3Qz+pOu3gVP31RHFrK8qd7ULpZvm6nwzN4BTwMN p3+7jBoOHr180wJ+wUzbdX43z7sLiCtFE8NZVaRoflxvqGwhQKErCT/koAPhSnHvLp8p drdBhFKK3M14VoblFwWMD/Q5xWZvYq8+V8ZNV6f96MIJSxf3ucE+v4IrlHrJXrlLqH9e IzOe1OZC0gRqJ/xmUWN2A8E+1EgVYdxmcvtvgYZm81HDamPkQ/ZdeHrzDn6xLeRe8lqA g38w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=bO12dMK8BWAM25cVTQtRKnqEGlFZmnHwbqx5kU+wHB4=; fh=IgHQrLPsdr1hVJosYJHJpHjj5JtHhVjUE8MVksLn+5s=; b=PtEEjagNHm7xWTGSp/3D01IPk3LfNIpH3t1z7bbm4vRwDSfSylKc8JSu2UgvCEB+Ey MiI2UDwBdrPARCdiwyya6A1zdFlH+TGTr/9rG9O0a3HdhTmswhDHf7JCOqQt57vF6skN YdZn2cwWXiqYS7XCuBoD+4JC12UBkuCFv0WQ6MaD/oPfvfRLJvZ0I+Rw92bl3aPDMps6 XvgrgUhiKXsErjjLTsOY697kHsdBZQJWAR3zd5oOOl76OHXetPHwkz9mxcSNzg6vPAZl /ggXPK2YYdzBueCs/AGfWdOqVhipRtRTcH/Y/3ZxCFoIo2ULhI/eFEUnPE3wkKJq6ory SJug== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from agentk.vger.email (agentk.vger.email. [23.128.96.32]) by mx.google.com with ESMTPS id t3-20020a170902d14300b001cc5f995bccsi7338646plt.182.2023.11.06.04.36.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 06 Nov 2023 04:36:11 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 as permitted sender) client-ip=23.128.96.32; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by agentk.vger.email (Postfix) with ESMTP id 3104C807C5EF; Mon, 6 Nov 2023 04:35:10 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at agentk.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231669AbjKFMen (ORCPT + 99 others); Mon, 6 Nov 2023 07:34:43 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35932 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231590AbjKFMej (ORCPT ); Mon, 6 Nov 2023 07:34:39 -0500 Received: from mxout70.expurgate.net (mxout70.expurgate.net [91.198.224.70]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0A094B7; Mon, 6 Nov 2023 04:34:35 -0800 (PST) Received: from [127.0.0.1] (helo=localhost) by relay.expurgate.net with smtp (Exim 4.92) (envelope-from ) id 1qzyoH-00CA0X-SH; Mon, 06 Nov 2023 13:34:29 +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 1qzyoH-00DGWh-1Q; Mon, 06 Nov 2023 13:34:29 +0100 Received: from securemail.tdt.de (localhost [127.0.0.1]) by securemail.tdt.de (Postfix) with ESMTP id 9F46124004D; Mon, 6 Nov 2023 13:34:28 +0100 (CET) Received: from mail.dev.tdt.de (unknown [10.2.4.42]) by securemail.tdt.de (Postfix) with ESMTP id F33C6240049; Mon, 6 Nov 2023 13:34:27 +0100 (CET) Received: from localhost.localdomain (unknown [10.2.3.40]) by mail.dev.tdt.de (Postfix) with ESMTPSA id 8FF67227EE; Mon, 6 Nov 2023 13:34:27 +0100 (CET) From: Florian Eckert To: Eckert.Florian@googlemail.com, gregkh@linuxfoundation.org, jirislaby@kernel.org, pavel@ucw.cz, lee@kernel.org, kabel@kernel.org, u.kleine-koenig@pengutronix.de, m.brock@vanmierlo.com Cc: linux-kernel@vger.kernel.org, linux-serial@vger.kernel.org, linux-leds@vger.kernel.org Subject: [Patch v7 5/6] leds: ledtrig-tty: make rx tx activitate configurable Date: Mon, 6 Nov 2023 13:34:14 +0100 Message-ID: <20231106123415.3365732-6-fe@dev.tdt.de> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20231106123415.3365732-1-fe@dev.tdt.de> References: <20231106123415.3365732-1-fe@dev.tdt.de> MIME-Version: 1.0 X-Spam-Status: No, score=-0.8 required=5.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on agentk.vger.email Content-Transfer-Encoding: quoted-printable X-purgate-ID: 151534::1699274069-CF6F53D8-D2756587/0/0 X-purgate: clean X-purgate-type: clean Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (agentk.vger.email [0.0.0.0]); Mon, 06 Nov 2023 04:35:10 -0800 (PST) Until now, the LED blinks when data is sent via the tty (rx/tx). This is not configurable. This change adds the possibility to make the indication for the direction of the transmitted data independently controllable via the new rx and tx sysfs entries. - rx: Signal reception (rx) of data on the named tty device. If set to 0, the LED will not blink on reception. If set to 1 (default), the LED will blink on reception. - tx: Signal transmission (tx) of data on the named tty device. If set to 0, the LED will not blink on transmission. If set to 1 (default), the LED will blink on transmission. This new sysfs entry are on by default. Thus the trigger behaves as before this change. Signed-off-by: Florian Eckert --- .../ABI/testing/sysfs-class-led-trigger-tty | 16 +++ drivers/leds/trigger/ledtrig-tty.c | 124 ++++++++++++++++-- 2 files changed, 130 insertions(+), 10 deletions(-) diff --git a/Documentation/ABI/testing/sysfs-class-led-trigger-tty b/Docu= mentation/ABI/testing/sysfs-class-led-trigger-tty index 2bf6b24e781b..504dece151b8 100644 --- a/Documentation/ABI/testing/sysfs-class-led-trigger-tty +++ b/Documentation/ABI/testing/sysfs-class-led-trigger-tty @@ -4,3 +4,19 @@ KernelVersion: 5.10 Contact: linux-leds@vger.kernel.org Description: Specifies the tty device name of the triggering tty + +What: /sys/class/leds//rx +Date: February 2024 +KernelVersion: 6.8 +Description: + Signal reception (rx) of data on the named tty device. + If set to 0, the LED will not blink on reception. + If set to 1 (default), the LED will blink on reception. + +What: /sys/class/leds//tx +Date: February 2024 +KernelVersion: 6.8 +Description: + Signal transmission (tx) of data on the named tty device. + If set to 0, the LED will not blink on transmission. + If set to 1 (default), the LED will blink on transmission. diff --git a/drivers/leds/trigger/ledtrig-tty.c b/drivers/leds/trigger/le= dtrig-tty.c index 3badf74fa420..1a40a78bf1ee 100644 --- a/drivers/leds/trigger/ledtrig-tty.c +++ b/drivers/leds/trigger/ledtrig-tty.c @@ -17,6 +17,19 @@ struct ledtrig_tty_data { const char *ttyname; struct tty_struct *tty; int rx, tx; + bool mode_rx; + bool mode_tx; +}; + +/* Indicates which state the LED should now display */ +enum led_trigger_tty_state { + TTY_LED_BLINK, + TTY_LED_DISABLE, +}; + +enum led_trigger_tty_modes { + TRIGGER_TTY_RX =3D 0, + TRIGGER_TTY_TX, }; =20 static int ledtrig_tty_waitforcompletion(struct device *dev) @@ -86,12 +99,82 @@ static ssize_t ttyname_store(struct device *dev, } static DEVICE_ATTR_RW(ttyname); =20 +static ssize_t ledtrig_tty_attr_show(struct device *dev, char *buf, + enum led_trigger_tty_modes attr) +{ + struct ledtrig_tty_data *trigger_data =3D led_trigger_get_drvdata(dev); + int completion; + bool state; + + reinit_completion(&trigger_data->sysfs); + completion =3D ledtrig_tty_waitforcompletion(dev); + if (completion < 0) + return completion; + + switch (attr) { + case TRIGGER_TTY_RX: + state =3D trigger_data->mode_rx; + break; + case TRIGGER_TTY_TX: + state =3D trigger_data->mode_tx; + break; + } + + return sysfs_emit(buf, "%u\n", state); +} + +static ssize_t ledtrig_tty_attr_store(struct device *dev, const char *bu= f, + size_t size, enum led_trigger_tty_modes attr) +{ + struct ledtrig_tty_data *trigger_data =3D led_trigger_get_drvdata(dev); + int completion; + bool state; + int ret; + + ret =3D kstrtobool(buf, &state); + if (ret) + return ret; + + reinit_completion(&trigger_data->sysfs); + completion =3D ledtrig_tty_waitforcompletion(dev); + if (completion < 0) + return completion; + + switch (attr) { + case TRIGGER_TTY_RX: + trigger_data->mode_rx =3D state; + break; + case TRIGGER_TTY_TX: + trigger_data->mode_tx =3D state; + break; + } + + return size; +} + +#define DEFINE_TTY_TRIGGER(trigger_name, trigger) \ + static ssize_t trigger_name##_show(struct device *dev, \ + struct device_attribute *attr, char *buf) \ + { \ + return ledtrig_tty_attr_show(dev, buf, trigger); \ + } \ + static ssize_t trigger_name##_store(struct device *dev, \ + struct device_attribute *attr, const char *buf, size_t size) \ + { \ + return ledtrig_tty_attr_store(dev, buf, size, trigger); \ + } \ + static DEVICE_ATTR_RW(trigger_name) + +DEFINE_TTY_TRIGGER(rx, TRIGGER_TTY_RX); +DEFINE_TTY_TRIGGER(tx, TRIGGER_TTY_TX); + static void ledtrig_tty_work(struct work_struct *work) { struct ledtrig_tty_data *trigger_data =3D container_of(work, struct ledtrig_tty_data, dwork.work); - struct serial_icounter_struct icount; struct led_classdev *led_cdev =3D trigger_data->led_cdev; + enum led_trigger_tty_state state =3D TTY_LED_DISABLE; + unsigned long interval =3D LEDTRIG_TTY_INTERVAL; int ret; =20 if (!trigger_data->ttyname) @@ -119,21 +202,36 @@ static void ledtrig_tty_work(struct work_struct *wo= rk) trigger_data->tty =3D tty; } =20 - ret =3D tty_get_icount(trigger_data->tty, &icount); - if (ret) - goto out; + if (trigger_data->mode_rx || trigger_data->mode_tx) { + struct serial_icounter_struct icount; =20 - if (icount.rx !=3D trigger_data->rx || - icount.tx !=3D trigger_data->tx) { - unsigned long interval =3D LEDTRIG_TTY_INTERVAL; + ret =3D tty_get_icount(trigger_data->tty, &icount); + if (ret) + goto out; =20 - led_blink_set_oneshot(led_cdev, &interval, &interval, 0); + if (trigger_data->mode_tx && (icount.tx !=3D trigger_data->tx)) { + trigger_data->tx =3D icount.tx; + state =3D TTY_LED_BLINK; + } =20 - trigger_data->rx =3D icount.rx; - trigger_data->tx =3D icount.tx; + if (trigger_data->mode_rx && (icount.rx !=3D trigger_data->rx)) { + trigger_data->rx =3D icount.rx; + state =3D TTY_LED_BLINK; + } } =20 out: + switch (state) { + case TTY_LED_BLINK: + led_blink_set_oneshot(led_cdev, &interval, &interval, 0); + break; + case TTY_LED_DISABLE: + fallthrough; + default: + led_set_brightness(led_cdev, LED_OFF); + break; + } + complete_all(&trigger_data->sysfs); schedule_delayed_work(&trigger_data->dwork, msecs_to_jiffies(LEDTRIG_TTY_INTERVAL * 2)); @@ -141,6 +239,8 @@ static void ledtrig_tty_work(struct work_struct *work= ) =20 static struct attribute *ledtrig_tty_attrs[] =3D { &dev_attr_ttyname.attr, + &dev_attr_rx.attr, + &dev_attr_tx.attr, NULL }; ATTRIBUTE_GROUPS(ledtrig_tty); @@ -153,6 +253,10 @@ static int ledtrig_tty_activate(struct led_classdev = *led_cdev) if (!trigger_data) return -ENOMEM; =20 + /* Enable default rx/tx mode */ + trigger_data->mode_rx =3D true; + trigger_data->mode_tx =3D true; + led_set_trigger_data(led_cdev, trigger_data); =20 INIT_DELAYED_WORK(&trigger_data->dwork, ledtrig_tty_work); --=20 2.30.2