1 // SPDX-License-Identifier: GPL-2.0
3 #include <linux/completion.h>
4 #include <linux/delay.h>
5 #include <linux/leds.h>
6 #include <linux/module.h>
7 #include <linux/slab.h>
9 #include <uapi/linux/serial.h>
11 #define LEDTRIG_TTY_INTERVAL 50
13 struct ledtrig_tty_data
{
14 struct led_classdev
*led_cdev
;
15 struct delayed_work dwork
;
16 struct completion sysfs
;
18 struct tty_struct
*tty
;
28 /* Indicates which state the LED should now display */
29 enum led_trigger_tty_state
{
35 enum led_trigger_tty_modes
{
44 static int ledtrig_tty_wait_for_completion(struct device
*dev
)
46 struct ledtrig_tty_data
*trigger_data
= led_trigger_get_drvdata(dev
);
49 ret
= wait_for_completion_timeout(&trigger_data
->sysfs
,
50 msecs_to_jiffies(LEDTRIG_TTY_INTERVAL
* 20));
57 static ssize_t
ttyname_show(struct device
*dev
,
58 struct device_attribute
*attr
, char *buf
)
60 struct ledtrig_tty_data
*trigger_data
= led_trigger_get_drvdata(dev
);
64 reinit_completion(&trigger_data
->sysfs
);
65 completion
= ledtrig_tty_wait_for_completion(dev
);
69 if (trigger_data
->ttyname
)
70 len
= sprintf(buf
, "%s\n", trigger_data
->ttyname
);
75 static ssize_t
ttyname_store(struct device
*dev
,
76 struct device_attribute
*attr
, const char *buf
,
79 struct ledtrig_tty_data
*trigger_data
= led_trigger_get_drvdata(dev
);
84 if (size
> 0 && buf
[size
- 1] == '\n')
88 ttyname
= kmemdup_nul(buf
, size
, GFP_KERNEL
);
95 reinit_completion(&trigger_data
->sysfs
);
96 completion
= ledtrig_tty_wait_for_completion(dev
);
100 kfree(trigger_data
->ttyname
);
101 tty_kref_put(trigger_data
->tty
);
102 trigger_data
->tty
= NULL
;
104 trigger_data
->ttyname
= ttyname
;
108 static DEVICE_ATTR_RW(ttyname
);
110 static ssize_t
ledtrig_tty_attr_show(struct device
*dev
, char *buf
,
111 enum led_trigger_tty_modes attr
)
113 struct ledtrig_tty_data
*trigger_data
= led_trigger_get_drvdata(dev
);
118 state
= trigger_data
->mode_rx
;
121 state
= trigger_data
->mode_tx
;
123 case TRIGGER_TTY_CTS
:
124 state
= trigger_data
->mode_cts
;
126 case TRIGGER_TTY_DSR
:
127 state
= trigger_data
->mode_dsr
;
129 case TRIGGER_TTY_DCD
:
130 state
= trigger_data
->mode_dcd
;
132 case TRIGGER_TTY_RNG
:
133 state
= trigger_data
->mode_rng
;
137 return sysfs_emit(buf
, "%u\n", state
);
140 static ssize_t
ledtrig_tty_attr_store(struct device
*dev
, const char *buf
,
141 size_t size
, enum led_trigger_tty_modes attr
)
143 struct ledtrig_tty_data
*trigger_data
= led_trigger_get_drvdata(dev
);
147 ret
= kstrtobool(buf
, &state
);
153 trigger_data
->mode_rx
= state
;
156 trigger_data
->mode_tx
= state
;
158 case TRIGGER_TTY_CTS
:
159 trigger_data
->mode_cts
= state
;
161 case TRIGGER_TTY_DSR
:
162 trigger_data
->mode_dsr
= state
;
164 case TRIGGER_TTY_DCD
:
165 trigger_data
->mode_dcd
= state
;
167 case TRIGGER_TTY_RNG
:
168 trigger_data
->mode_rng
= state
;
175 #define DEFINE_TTY_TRIGGER(trigger_name, trigger) \
176 static ssize_t trigger_name##_show(struct device *dev, \
177 struct device_attribute *attr, char *buf) \
179 return ledtrig_tty_attr_show(dev, buf, trigger); \
181 static ssize_t trigger_name##_store(struct device *dev, \
182 struct device_attribute *attr, const char *buf, size_t size) \
184 return ledtrig_tty_attr_store(dev, buf, size, trigger); \
186 static DEVICE_ATTR_RW(trigger_name)
188 DEFINE_TTY_TRIGGER(rx
, TRIGGER_TTY_RX
);
189 DEFINE_TTY_TRIGGER(tx
, TRIGGER_TTY_TX
);
190 DEFINE_TTY_TRIGGER(cts
, TRIGGER_TTY_CTS
);
191 DEFINE_TTY_TRIGGER(dsr
, TRIGGER_TTY_DSR
);
192 DEFINE_TTY_TRIGGER(dcd
, TRIGGER_TTY_DCD
);
193 DEFINE_TTY_TRIGGER(rng
, TRIGGER_TTY_RNG
);
195 static void ledtrig_tty_work(struct work_struct
*work
)
197 struct ledtrig_tty_data
*trigger_data
=
198 container_of(work
, struct ledtrig_tty_data
, dwork
.work
);
199 enum led_trigger_tty_state state
= TTY_LED_DISABLE
;
200 unsigned long interval
= LEDTRIG_TTY_INTERVAL
;
205 if (!trigger_data
->ttyname
)
208 /* try to get the tty corresponding to $ttyname */
209 if (!trigger_data
->tty
) {
211 struct tty_struct
*tty
;
214 ret
= tty_dev_name_to_number(trigger_data
->ttyname
, &devno
);
217 * A device with this name might appear later, so keep
222 tty
= tty_kopen_shared(devno
);
223 if (IS_ERR(tty
) || !tty
)
224 /* What to do? retry or abort */
227 trigger_data
->tty
= tty
;
230 status
= tty_get_tiocm(trigger_data
->tty
);
232 if (trigger_data
->mode_cts
) {
233 if (status
& TIOCM_CTS
)
234 state
= TTY_LED_ENABLE
;
237 if (trigger_data
->mode_dsr
) {
238 if (status
& TIOCM_DSR
)
239 state
= TTY_LED_ENABLE
;
242 if (trigger_data
->mode_dcd
) {
243 if (status
& TIOCM_CAR
)
244 state
= TTY_LED_ENABLE
;
247 if (trigger_data
->mode_rng
) {
248 if (status
& TIOCM_RNG
)
249 state
= TTY_LED_ENABLE
;
254 * The evaluation of rx/tx must be done after the evaluation
255 * of TIOCM_*, because rx/tx has priority.
257 if (trigger_data
->mode_rx
|| trigger_data
->mode_tx
) {
258 struct serial_icounter_struct icount
;
260 ret
= tty_get_icount(trigger_data
->tty
, &icount
);
264 if (trigger_data
->mode_tx
&& (icount
.tx
!= trigger_data
->tx
)) {
265 trigger_data
->tx
= icount
.tx
;
266 invert
= state
== TTY_LED_ENABLE
;
267 state
= TTY_LED_BLINK
;
270 if (trigger_data
->mode_rx
&& (icount
.rx
!= trigger_data
->rx
)) {
271 trigger_data
->rx
= icount
.rx
;
272 invert
= state
== TTY_LED_ENABLE
;
273 state
= TTY_LED_BLINK
;
280 led_blink_set_oneshot(trigger_data
->led_cdev
, &interval
,
284 led_set_brightness(trigger_data
->led_cdev
,
285 trigger_data
->led_cdev
->blink_brightness
);
287 case TTY_LED_DISABLE
:
290 led_set_brightness(trigger_data
->led_cdev
, LED_OFF
);
294 complete_all(&trigger_data
->sysfs
);
295 schedule_delayed_work(&trigger_data
->dwork
,
296 msecs_to_jiffies(LEDTRIG_TTY_INTERVAL
* 2));
299 static struct attribute
*ledtrig_tty_attrs
[] = {
300 &dev_attr_ttyname
.attr
,
309 ATTRIBUTE_GROUPS(ledtrig_tty
);
311 static int ledtrig_tty_activate(struct led_classdev
*led_cdev
)
313 struct ledtrig_tty_data
*trigger_data
;
315 trigger_data
= kzalloc(sizeof(*trigger_data
), GFP_KERNEL
);
319 /* Enable default rx/tx mode */
320 trigger_data
->mode_rx
= true;
321 trigger_data
->mode_tx
= true;
323 led_set_trigger_data(led_cdev
, trigger_data
);
325 INIT_DELAYED_WORK(&trigger_data
->dwork
, ledtrig_tty_work
);
326 trigger_data
->led_cdev
= led_cdev
;
327 init_completion(&trigger_data
->sysfs
);
329 schedule_delayed_work(&trigger_data
->dwork
, 0);
334 static void ledtrig_tty_deactivate(struct led_classdev
*led_cdev
)
336 struct ledtrig_tty_data
*trigger_data
= led_get_trigger_data(led_cdev
);
338 cancel_delayed_work_sync(&trigger_data
->dwork
);
340 kfree(trigger_data
->ttyname
);
341 tty_kref_put(trigger_data
->tty
);
342 trigger_data
->tty
= NULL
;
347 static struct led_trigger ledtrig_tty
= {
349 .activate
= ledtrig_tty_activate
,
350 .deactivate
= ledtrig_tty_deactivate
,
351 .groups
= ledtrig_tty_groups
,
353 module_led_trigger(ledtrig_tty
);
355 MODULE_AUTHOR("Uwe Kleine-König <u.kleine-koenig@pengutronix.de>");
356 MODULE_DESCRIPTION("UART LED trigger");
357 MODULE_LICENSE("GPL v2");