1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright 2017 Ben Whitten <ben.whitten@gmail.com>
3 // Copyright 2007 Oliver Jowett <oliver@opencloud.com>
5 // LED Kernel Netdev Trigger
7 // Toggles the LED to reflect the link and traffic state of a named net device
9 // Derived from ledtrig-timer.c which is:
10 // Copyright 2005-2006 Openedhand Ltd.
11 // Author: Richard Purdie <rpurdie@openedhand.com>
13 #include <linux/atomic.h>
14 #include <linux/ctype.h>
15 #include <linux/device.h>
16 #include <linux/init.h>
17 #include <linux/jiffies.h>
18 #include <linux/kernel.h>
19 #include <linux/leds.h>
20 #include <linux/list.h>
21 #include <linux/module.h>
22 #include <linux/netdevice.h>
23 #include <linux/spinlock.h>
24 #include <linux/timer.h>
28 * Configurable sysfs attributes:
30 * device_name - network device name to monitor
31 * interval - duration of LED blink, in milliseconds
32 * link - LED's normal state reflects whether the link is up
33 * (has carrier) or not
34 * tx - LED blinks on transmitted data
35 * rx - LED blinks on receive data
39 struct led_netdev_data
{
42 struct delayed_work work
;
43 struct notifier_block notifier
;
45 struct led_classdev
*led_cdev
;
46 struct net_device
*net_dev
;
48 char device_name
[IFNAMSIZ
];
50 unsigned int last_activity
;
53 #define NETDEV_LED_LINK 0
54 #define NETDEV_LED_TX 1
55 #define NETDEV_LED_RX 2
56 #define NETDEV_LED_MODE_LINKUP 3
59 enum netdev_led_attr
{
65 static void set_baseline_state(struct led_netdev_data
*trigger_data
)
67 int current_brightness
;
68 struct led_classdev
*led_cdev
= trigger_data
->led_cdev
;
70 current_brightness
= led_cdev
->brightness
;
71 if (current_brightness
)
72 led_cdev
->blink_brightness
= current_brightness
;
73 if (!led_cdev
->blink_brightness
)
74 led_cdev
->blink_brightness
= led_cdev
->max_brightness
;
76 if (!test_bit(NETDEV_LED_MODE_LINKUP
, &trigger_data
->mode
))
77 led_set_brightness(led_cdev
, LED_OFF
);
79 if (test_bit(NETDEV_LED_LINK
, &trigger_data
->mode
))
80 led_set_brightness(led_cdev
,
81 led_cdev
->blink_brightness
);
83 led_set_brightness(led_cdev
, LED_OFF
);
85 /* If we are looking for RX/TX start periodically
88 if (test_bit(NETDEV_LED_TX
, &trigger_data
->mode
) ||
89 test_bit(NETDEV_LED_RX
, &trigger_data
->mode
))
90 schedule_delayed_work(&trigger_data
->work
, 0);
94 static ssize_t
device_name_show(struct device
*dev
,
95 struct device_attribute
*attr
, char *buf
)
97 struct led_netdev_data
*trigger_data
= led_trigger_get_drvdata(dev
);
100 spin_lock_bh(&trigger_data
->lock
);
101 len
= sprintf(buf
, "%s\n", trigger_data
->device_name
);
102 spin_unlock_bh(&trigger_data
->lock
);
107 static ssize_t
device_name_store(struct device
*dev
,
108 struct device_attribute
*attr
, const char *buf
,
111 struct led_netdev_data
*trigger_data
= led_trigger_get_drvdata(dev
);
113 if (size
>= IFNAMSIZ
)
116 cancel_delayed_work_sync(&trigger_data
->work
);
118 spin_lock_bh(&trigger_data
->lock
);
120 if (trigger_data
->net_dev
) {
121 dev_put(trigger_data
->net_dev
);
122 trigger_data
->net_dev
= NULL
;
125 strncpy(trigger_data
->device_name
, buf
, size
);
126 if (size
> 0 && trigger_data
->device_name
[size
- 1] == '\n')
127 trigger_data
->device_name
[size
- 1] = 0;
129 if (trigger_data
->device_name
[0] != 0)
130 trigger_data
->net_dev
=
131 dev_get_by_name(&init_net
, trigger_data
->device_name
);
133 clear_bit(NETDEV_LED_MODE_LINKUP
, &trigger_data
->mode
);
134 if (trigger_data
->net_dev
!= NULL
)
135 if (netif_carrier_ok(trigger_data
->net_dev
))
136 set_bit(NETDEV_LED_MODE_LINKUP
, &trigger_data
->mode
);
138 trigger_data
->last_activity
= 0;
140 set_baseline_state(trigger_data
);
141 spin_unlock_bh(&trigger_data
->lock
);
146 static DEVICE_ATTR_RW(device_name
);
148 static ssize_t
netdev_led_attr_show(struct device
*dev
, char *buf
,
149 enum netdev_led_attr attr
)
151 struct led_netdev_data
*trigger_data
= led_trigger_get_drvdata(dev
);
155 case NETDEV_ATTR_LINK
:
156 bit
= NETDEV_LED_LINK
;
168 return sprintf(buf
, "%u\n", test_bit(bit
, &trigger_data
->mode
));
171 static ssize_t
netdev_led_attr_store(struct device
*dev
, const char *buf
,
172 size_t size
, enum netdev_led_attr attr
)
174 struct led_netdev_data
*trigger_data
= led_trigger_get_drvdata(dev
);
179 ret
= kstrtoul(buf
, 0, &state
);
184 case NETDEV_ATTR_LINK
:
185 bit
= NETDEV_LED_LINK
;
197 cancel_delayed_work_sync(&trigger_data
->work
);
200 set_bit(bit
, &trigger_data
->mode
);
202 clear_bit(bit
, &trigger_data
->mode
);
204 set_baseline_state(trigger_data
);
209 static ssize_t
link_show(struct device
*dev
,
210 struct device_attribute
*attr
, char *buf
)
212 return netdev_led_attr_show(dev
, buf
, NETDEV_ATTR_LINK
);
215 static ssize_t
link_store(struct device
*dev
,
216 struct device_attribute
*attr
, const char *buf
, size_t size
)
218 return netdev_led_attr_store(dev
, buf
, size
, NETDEV_ATTR_LINK
);
221 static DEVICE_ATTR_RW(link
);
223 static ssize_t
tx_show(struct device
*dev
,
224 struct device_attribute
*attr
, char *buf
)
226 return netdev_led_attr_show(dev
, buf
, NETDEV_ATTR_TX
);
229 static ssize_t
tx_store(struct device
*dev
,
230 struct device_attribute
*attr
, const char *buf
, size_t size
)
232 return netdev_led_attr_store(dev
, buf
, size
, NETDEV_ATTR_TX
);
235 static DEVICE_ATTR_RW(tx
);
237 static ssize_t
rx_show(struct device
*dev
,
238 struct device_attribute
*attr
, char *buf
)
240 return netdev_led_attr_show(dev
, buf
, NETDEV_ATTR_RX
);
243 static ssize_t
rx_store(struct device
*dev
,
244 struct device_attribute
*attr
, const char *buf
, size_t size
)
246 return netdev_led_attr_store(dev
, buf
, size
, NETDEV_ATTR_RX
);
249 static DEVICE_ATTR_RW(rx
);
251 static ssize_t
interval_show(struct device
*dev
,
252 struct device_attribute
*attr
, char *buf
)
254 struct led_netdev_data
*trigger_data
= led_trigger_get_drvdata(dev
);
256 return sprintf(buf
, "%u\n",
257 jiffies_to_msecs(atomic_read(&trigger_data
->interval
)));
260 static ssize_t
interval_store(struct device
*dev
,
261 struct device_attribute
*attr
, const char *buf
,
264 struct led_netdev_data
*trigger_data
= led_trigger_get_drvdata(dev
);
268 ret
= kstrtoul(buf
, 0, &value
);
272 /* impose some basic bounds on the timer interval */
273 if (value
>= 5 && value
<= 10000) {
274 cancel_delayed_work_sync(&trigger_data
->work
);
276 atomic_set(&trigger_data
->interval
, msecs_to_jiffies(value
));
277 set_baseline_state(trigger_data
); /* resets timer */
283 static DEVICE_ATTR_RW(interval
);
285 static struct attribute
*netdev_trig_attrs
[] = {
286 &dev_attr_device_name
.attr
,
290 &dev_attr_interval
.attr
,
293 ATTRIBUTE_GROUPS(netdev_trig
);
295 static int netdev_trig_notify(struct notifier_block
*nb
,
296 unsigned long evt
, void *dv
)
298 struct net_device
*dev
=
299 netdev_notifier_info_to_dev((struct netdev_notifier_info
*)dv
);
300 struct led_netdev_data
*trigger_data
=
301 container_of(nb
, struct led_netdev_data
, notifier
);
303 if (evt
!= NETDEV_UP
&& evt
!= NETDEV_DOWN
&& evt
!= NETDEV_CHANGE
304 && evt
!= NETDEV_REGISTER
&& evt
!= NETDEV_UNREGISTER
305 && evt
!= NETDEV_CHANGENAME
)
308 if (strcmp(dev
->name
, trigger_data
->device_name
))
311 cancel_delayed_work_sync(&trigger_data
->work
);
313 spin_lock_bh(&trigger_data
->lock
);
315 clear_bit(NETDEV_LED_MODE_LINKUP
, &trigger_data
->mode
);
317 case NETDEV_REGISTER
:
318 if (trigger_data
->net_dev
)
319 dev_put(trigger_data
->net_dev
);
321 trigger_data
->net_dev
= dev
;
323 case NETDEV_CHANGENAME
:
324 case NETDEV_UNREGISTER
:
325 if (trigger_data
->net_dev
) {
326 dev_put(trigger_data
->net_dev
);
327 trigger_data
->net_dev
= NULL
;
332 if (netif_carrier_ok(dev
))
333 set_bit(NETDEV_LED_MODE_LINKUP
, &trigger_data
->mode
);
337 set_baseline_state(trigger_data
);
339 spin_unlock_bh(&trigger_data
->lock
);
344 /* here's the real work! */
345 static void netdev_trig_work(struct work_struct
*work
)
347 struct led_netdev_data
*trigger_data
=
348 container_of(work
, struct led_netdev_data
, work
.work
);
349 struct rtnl_link_stats64
*dev_stats
;
350 unsigned int new_activity
;
351 struct rtnl_link_stats64 temp
;
352 unsigned long interval
;
355 /* If we dont have a device, insure we are off */
356 if (!trigger_data
->net_dev
) {
357 led_set_brightness(trigger_data
->led_cdev
, LED_OFF
);
361 /* If we are not looking for RX/TX then return */
362 if (!test_bit(NETDEV_LED_TX
, &trigger_data
->mode
) &&
363 !test_bit(NETDEV_LED_RX
, &trigger_data
->mode
))
366 dev_stats
= dev_get_stats(trigger_data
->net_dev
, &temp
);
368 (test_bit(NETDEV_LED_TX
, &trigger_data
->mode
) ?
369 dev_stats
->tx_packets
: 0) +
370 (test_bit(NETDEV_LED_RX
, &trigger_data
->mode
) ?
371 dev_stats
->rx_packets
: 0);
373 if (trigger_data
->last_activity
!= new_activity
) {
374 led_stop_software_blink(trigger_data
->led_cdev
);
376 invert
= test_bit(NETDEV_LED_LINK
, &trigger_data
->mode
);
377 interval
= jiffies_to_msecs(
378 atomic_read(&trigger_data
->interval
));
379 /* base state is ON (link present) */
380 led_blink_set_oneshot(trigger_data
->led_cdev
,
384 trigger_data
->last_activity
= new_activity
;
387 schedule_delayed_work(&trigger_data
->work
,
388 (atomic_read(&trigger_data
->interval
)*2));
391 static int netdev_trig_activate(struct led_classdev
*led_cdev
)
393 struct led_netdev_data
*trigger_data
;
396 trigger_data
= kzalloc(sizeof(struct led_netdev_data
), GFP_KERNEL
);
400 spin_lock_init(&trigger_data
->lock
);
402 trigger_data
->notifier
.notifier_call
= netdev_trig_notify
;
403 trigger_data
->notifier
.priority
= 10;
405 INIT_DELAYED_WORK(&trigger_data
->work
, netdev_trig_work
);
407 trigger_data
->led_cdev
= led_cdev
;
408 trigger_data
->net_dev
= NULL
;
409 trigger_data
->device_name
[0] = 0;
411 trigger_data
->mode
= 0;
412 atomic_set(&trigger_data
->interval
, msecs_to_jiffies(50));
413 trigger_data
->last_activity
= 0;
415 led_set_trigger_data(led_cdev
, trigger_data
);
417 rc
= register_netdevice_notifier(&trigger_data
->notifier
);
424 static void netdev_trig_deactivate(struct led_classdev
*led_cdev
)
426 struct led_netdev_data
*trigger_data
= led_get_trigger_data(led_cdev
);
428 unregister_netdevice_notifier(&trigger_data
->notifier
);
430 cancel_delayed_work_sync(&trigger_data
->work
);
432 if (trigger_data
->net_dev
)
433 dev_put(trigger_data
->net_dev
);
438 static struct led_trigger netdev_led_trigger
= {
440 .activate
= netdev_trig_activate
,
441 .deactivate
= netdev_trig_deactivate
,
442 .groups
= netdev_trig_groups
,
445 static int __init
netdev_trig_init(void)
447 return led_trigger_register(&netdev_led_trigger
);
450 static void __exit
netdev_trig_exit(void)
452 led_trigger_unregister(&netdev_led_trigger
);
455 module_init(netdev_trig_init
);
456 module_exit(netdev_trig_exit
);
458 MODULE_AUTHOR("Ben Whitten <ben.whitten@gmail.com>");
459 MODULE_AUTHOR("Oliver Jowett <oliver@opencloud.com>");
460 MODULE_DESCRIPTION("Netdev LED trigger");
461 MODULE_LICENSE("GPL v2");