From: Pavel Machek <pavel@denx.de>
To: "Uwe Kleine-König" <u.kleine-koenig@pengutronix.de>
Cc: Jacek Anaszewski <jacek.anaszewski@gmail.com>,
Dan Murphy <dmurphy@ti.com>,
Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
Jiri Slaby <jslaby@suse.com>,
kernel@pengutronix.de, linux-kernel@vger.kernel.org,
Johan Hovold <johan@kernel.org>,
linux-leds@vger.kernel.org, linux-serial@vger.kernel.org
Subject: Re: [PATCH v9 3/3] leds: trigger: implement a tty trigger
Date: Tue, 20 Oct 2020 14:36:09 +0200 [thread overview]
Message-ID: <20201020123609.GD19856@duo.ucw.cz> (raw)
In-Reply-To: <20201018204022.910815-4-u.kleine-koenig@pengutronix.de>
[-- Attachment #1: Type: text/plain, Size: 3219 bytes --]
Hi!
> Usage is as follows:
>
> myled=ledname
> tty=ttyS0
>
> echo tty > /sys/class/leds/$myled/trigger
> echo $tty > /sys/class/leds/$myled/ttyname
>
> . When this new trigger is active it periodically checks the tty's
> statistics and when it changed since the last check the led is flashed
> once.
>
> Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
> ---
> +static ssize_t ttyname_show(struct device *dev,
> + struct device_attribute *attr, char *buf)
> +{
> + struct ledtrig_tty_data *trigger_data = led_trigger_get_drvdata(dev);
> + ssize_t len = 0;
Unused value. Not sure if some checker will complain.
> +static ssize_t ttyname_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);
> + char *ttyname;
> + ssize_t ret = size;
> + ledtrig_tty_halt(trigger_data);
> +
> + mutex_lock(&trigger_data->mutex);
> +
> + if (size > 0 && buf[size - 1] == '\n')
> + size -= 1;
> +
> + if (size) {
> + ttyname = kmemdup_nul(buf, size, GFP_KERNEL);
> + if (!ttyname) {
> + ret = -ENOMEM;
> + goto out_unlock;
If this happens, you return error to the user and
trigger_data->ttyname remains with the old value, but trigger is now
stopped. That is not exactly consistent.
> +static void ledtrig_tty_work(struct work_struct *work)
> +{
> + struct ledtrig_tty_data *trigger_data =
> + container_of(work, struct ledtrig_tty_data, dwork.work);
> + struct serial_icounter_struct icount;
> + int ret;
> +
> + mutex_lock(&trigger_data->mutex);
> +
> + BUG_ON(!trigger_data->ttyname);
> +
> + /* try to get the tty corresponding to $ttyname */
> + if (!trigger_data->tty) {
> + dev_t devno;
> + struct tty_struct *tty;
> + int ret;
> +
> + ret = tty_dev_name_to_number(trigger_data->ttyname, &devno);
> + if (ret < 0)
> + /*
> + * A device with this name might appear later, so keep
> + * retrying.
> + */
> + goto out;
Poll every 100 msec... Hmm.... Okay, I guess?
> + ret = tty_get_icount(trigger_data->tty, &icount);
> + if (ret) {
> + mutex_unlock(&trigger_data->mutex);
> + dev_info(trigger_data->tty->dev, "Failed to get icount, stopped polling\n");
> + mutex_unlock(&trigger_data->mutex);
Eh?
> + return;
> + }
> +
> + if (icount.rx != trigger_data->rx ||
> + icount.tx != trigger_data->tx) {
> + led_set_brightness(trigger_data->led_cdev, LED_ON);
Are you sure about LED_ON here? It should use current brightness
selected by brightness file...
> +static int ledtrig_tty_activate(struct led_classdev *led_cdev)
> +{
> + struct ledtrig_tty_data *trigger_data;
> +
> + trigger_data = kzalloc(sizeof(*trigger_data), GFP_KERNEL);
> + if (!trigger_data)
> + return -ENOMEM;
> +
> + led_set_trigger_data(led_cdev, trigger_data);
> +
> + INIT_DELAYED_WORK(&trigger_data->dwork, ledtrig_tty_work);
> + trigger_data->led_cdev = led_cdev;
> + mutex_init(&trigger_data->mutex);
How is this protected from concurrent access from sysfs?
Best regards,
Pavel
--
http://www.livejournal.com/~pavelmachek
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 195 bytes --]
prev parent reply other threads:[~2020-10-20 12:36 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-10-18 20:40 [PATCH v9 0/3] leds: trigger: implement a tty trigger Uwe Kleine-König
2020-10-18 20:40 ` [PATCH v9 1/3] tty: rename tty_kopen() and add new function tty_kopen_shared() Uwe Kleine-König
2020-10-20 12:25 ` Pavel Machek
2020-10-18 20:40 ` [PATCH v9 2/3] tty: new helper function tty_get_icount() Uwe Kleine-König
2020-10-20 12:25 ` Pavel Machek
2020-10-18 20:40 ` [PATCH v9 3/3] leds: trigger: implement a tty trigger Uwe Kleine-König
2020-10-20 12:36 ` Pavel Machek [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20201020123609.GD19856@duo.ucw.cz \
--to=pavel@denx.de \
--cc=dmurphy@ti.com \
--cc=gregkh@linuxfoundation.org \
--cc=jacek.anaszewski@gmail.com \
--cc=johan@kernel.org \
--cc=jslaby@suse.com \
--cc=kernel@pengutronix.de \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-leds@vger.kernel.org \
--cc=linux-serial@vger.kernel.org \
--cc=u.kleine-koenig@pengutronix.de \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).