Skip to content

Commit 5b755ca

Browse files
feckertlag-linaro
authored andcommitted
leds: ledtrig-tty: Make rx tx activitate configurable
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 <fe@dev.tdt.de> Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org> Link: https://lore.kernel.org/r/20231127110311.3583957-4-fe@dev.tdt.de Signed-off-by: Lee Jones <lee@kernel.org>
1 parent 76675f6 commit 5b755ca

File tree

2 files changed

+119
-11
lines changed

2 files changed

+119
-11
lines changed

Documentation/ABI/testing/sysfs-class-led-trigger-tty

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,19 @@ KernelVersion: 5.10
44
Contact: linux-leds@vger.kernel.org
55
Description:
66
Specifies the tty device name of the triggering tty
7+
8+
What: /sys/class/leds/<led>/rx
9+
Date: February 2024
10+
KernelVersion: 6.8
11+
Description:
12+
Signal reception (rx) of data on the named tty device.
13+
If set to 0, the LED will not blink on reception.
14+
If set to 1 (default), the LED will blink on reception.
15+
16+
What: /sys/class/leds/<led>/tx
17+
Date: February 2024
18+
KernelVersion: 6.8
19+
Description:
20+
Signal transmission (tx) of data on the named tty device.
21+
If set to 0, the LED will not blink on transmission.
22+
If set to 1 (default), the LED will blink on transmission.

drivers/leds/trigger/ledtrig-tty.c

Lines changed: 103 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,19 @@ struct ledtrig_tty_data {
1717
const char *ttyname;
1818
struct tty_struct *tty;
1919
int rx, tx;
20+
bool mode_rx;
21+
bool mode_tx;
22+
};
23+
24+
/* Indicates which state the LED should now display */
25+
enum led_trigger_tty_state {
26+
TTY_LED_BLINK,
27+
TTY_LED_DISABLE,
28+
};
29+
30+
enum led_trigger_tty_modes {
31+
TRIGGER_TTY_RX = 0,
32+
TRIGGER_TTY_TX,
2033
};
2134

2235
static int ledtrig_tty_wait_for_completion(struct device *dev)
@@ -85,11 +98,69 @@ static ssize_t ttyname_store(struct device *dev,
8598
}
8699
static DEVICE_ATTR_RW(ttyname);
87100

101+
static ssize_t ledtrig_tty_attr_show(struct device *dev, char *buf,
102+
enum led_trigger_tty_modes attr)
103+
{
104+
struct ledtrig_tty_data *trigger_data = led_trigger_get_drvdata(dev);
105+
bool state;
106+
107+
switch (attr) {
108+
case TRIGGER_TTY_RX:
109+
state = trigger_data->mode_rx;
110+
break;
111+
case TRIGGER_TTY_TX:
112+
state = trigger_data->mode_tx;
113+
break;
114+
}
115+
116+
return sysfs_emit(buf, "%u\n", state);
117+
}
118+
119+
static ssize_t ledtrig_tty_attr_store(struct device *dev, const char *buf,
120+
size_t size, enum led_trigger_tty_modes attr)
121+
{
122+
struct ledtrig_tty_data *trigger_data = led_trigger_get_drvdata(dev);
123+
bool state;
124+
int ret;
125+
126+
ret = kstrtobool(buf, &state);
127+
if (ret)
128+
return ret;
129+
130+
switch (attr) {
131+
case TRIGGER_TTY_RX:
132+
trigger_data->mode_rx = state;
133+
break;
134+
case TRIGGER_TTY_TX:
135+
trigger_data->mode_tx = state;
136+
break;
137+
}
138+
139+
return size;
140+
}
141+
142+
#define DEFINE_TTY_TRIGGER(trigger_name, trigger) \
143+
static ssize_t trigger_name##_show(struct device *dev, \
144+
struct device_attribute *attr, char *buf) \
145+
{ \
146+
return ledtrig_tty_attr_show(dev, buf, trigger); \
147+
} \
148+
static ssize_t trigger_name##_store(struct device *dev, \
149+
struct device_attribute *attr, const char *buf, size_t size) \
150+
{ \
151+
return ledtrig_tty_attr_store(dev, buf, size, trigger); \
152+
} \
153+
static DEVICE_ATTR_RW(trigger_name)
154+
155+
DEFINE_TTY_TRIGGER(rx, TRIGGER_TTY_RX);
156+
DEFINE_TTY_TRIGGER(tx, TRIGGER_TTY_TX);
157+
88158
static void ledtrig_tty_work(struct work_struct *work)
89159
{
90160
struct ledtrig_tty_data *trigger_data =
91161
container_of(work, struct ledtrig_tty_data, dwork.work);
92-
struct serial_icounter_struct icount;
162+
enum led_trigger_tty_state state = TTY_LED_DISABLE;
163+
unsigned long interval = LEDTRIG_TTY_INTERVAL;
93164
int ret;
94165

95166
if (!trigger_data->ttyname)
@@ -117,29 +188,46 @@ static void ledtrig_tty_work(struct work_struct *work)
117188
trigger_data->tty = tty;
118189
}
119190

120-
ret = tty_get_icount(trigger_data->tty, &icount);
121-
if (ret)
122-
goto out;
191+
if (trigger_data->mode_rx || trigger_data->mode_tx) {
192+
struct serial_icounter_struct icount;
123193

124-
if (icount.rx != trigger_data->rx ||
125-
icount.tx != trigger_data->tx) {
126-
unsigned long interval = LEDTRIG_TTY_INTERVAL;
194+
ret = tty_get_icount(trigger_data->tty, &icount);
195+
if (ret)
196+
goto out;
127197

128-
led_blink_set_oneshot(trigger_data->led_cdev, &interval,
129-
&interval, 0);
198+
if (trigger_data->mode_tx && (icount.tx != trigger_data->tx)) {
199+
trigger_data->tx = icount.tx;
200+
state = TTY_LED_BLINK;
201+
}
130202

131-
trigger_data->rx = icount.rx;
132-
trigger_data->tx = icount.tx;
203+
if (trigger_data->mode_rx && (icount.rx != trigger_data->rx)) {
204+
trigger_data->rx = icount.rx;
205+
state = TTY_LED_BLINK;
206+
}
133207
}
134208

135209
out:
210+
switch (state) {
211+
case TTY_LED_BLINK:
212+
led_blink_set_oneshot(trigger_data->led_cdev, &interval,
213+
&interval, 0);
214+
break;
215+
case TTY_LED_DISABLE:
216+
fallthrough;
217+
default:
218+
led_set_brightness(trigger_data->led_cdev, LED_OFF);
219+
break;
220+
}
221+
136222
complete_all(&trigger_data->sysfs);
137223
schedule_delayed_work(&trigger_data->dwork,
138224
msecs_to_jiffies(LEDTRIG_TTY_INTERVAL * 2));
139225
}
140226

141227
static struct attribute *ledtrig_tty_attrs[] = {
142228
&dev_attr_ttyname.attr,
229+
&dev_attr_rx.attr,
230+
&dev_attr_tx.attr,
143231
NULL
144232
};
145233
ATTRIBUTE_GROUPS(ledtrig_tty);
@@ -152,6 +240,10 @@ static int ledtrig_tty_activate(struct led_classdev *led_cdev)
152240
if (!trigger_data)
153241
return -ENOMEM;
154242

243+
/* Enable default rx/tx mode */
244+
trigger_data->mode_rx = true;
245+
trigger_data->mode_tx = true;
246+
155247
led_set_trigger_data(led_cdev, trigger_data);
156248

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

0 commit comments

Comments
 (0)