2 * LED Kernel Netdev Trigger
4 * Toggles the LED to reflect the link and traffic state of a named net device
6 * Copyright 2007 Oliver Jowett <oliver@opencloud.com>
8 * Derived from ledtrig-timer.c which is:
9 * Copyright 2005-2006 Openedhand Ltd.
10 * Author: Richard Purdie <rpurdie@openedhand.com>
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License version 2 as
14 * published by the Free Software Foundation.
18 #include <linux/module.h>
19 #include <linux/jiffies.h>
20 #include <linux/kernel.h>
21 #include <linux/init.h>
22 #include <linux/list.h>
23 #include <linux/spinlock.h>
24 #include <linux/device.h>
25 #include <linux/sysdev.h>
26 #include <linux/netdevice.h>
27 #include <linux/timer.h>
28 #include <linux/ctype.h>
29 #include <linux/leds.h>
30 #include <linux/version.h>
32 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,26)
33 #include <net/net_namespace.h>
39 * Configurable sysfs attributes:
41 * device_name - network device name to monitor
43 * interval - duration of LED blink, in milliseconds
45 * mode - either "none" (LED is off) or a space separated list of one or more of:
46 * link: LED's normal state reflects whether the link is up (has carrier) or not
47 * tx: LED blinks on transmitted data
48 * rx: LED blinks on receive data
52 * Simple link status LED:
53 * $ echo netdev >someled/trigger
54 * $ echo eth0 >someled/device_name
55 * $ echo link >someled/mode
57 * Ethernet-style link/activity LED:
58 * $ echo netdev >someled/trigger
59 * $ echo eth0 >someled/device_name
60 * $ echo "link tx rx" >someled/mode
62 * Modem-style tx/rx LEDs:
63 * $ echo netdev >led1/trigger
64 * $ echo ppp0 >led1/device_name
65 * $ echo tx >led1/mode
66 * $ echo netdev >led2/trigger
67 * $ echo ppp0 >led2/device_name
68 * $ echo rx >led2/mode
76 struct led_netdev_data
{
79 struct timer_list timer
;
80 struct notifier_block notifier
;
82 struct led_classdev
*led_cdev
;
83 struct net_device
*net_dev
;
85 char device_name
[IFNAMSIZ
];
89 unsigned last_activity
;
92 static void set_baseline_state(struct led_netdev_data
*trigger_data
)
94 if ((trigger_data
->mode
& MODE_LINK
) != 0 && trigger_data
->link_up
)
95 led_set_brightness(trigger_data
->led_cdev
, LED_FULL
);
97 led_set_brightness(trigger_data
->led_cdev
, LED_OFF
);
99 if ((trigger_data
->mode
& (MODE_TX
| MODE_RX
)) != 0 && trigger_data
->link_up
)
100 mod_timer(&trigger_data
->timer
, jiffies
+ trigger_data
->interval
);
102 del_timer(&trigger_data
->timer
);
105 static ssize_t
led_device_name_show(struct device
*dev
,
106 struct device_attribute
*attr
, char *buf
)
108 struct led_classdev
*led_cdev
= dev_get_drvdata(dev
);
109 struct led_netdev_data
*trigger_data
= led_cdev
->trigger_data
;
111 read_lock(&trigger_data
->lock
);
112 sprintf(buf
, "%s\n", trigger_data
->device_name
);
113 read_unlock(&trigger_data
->lock
);
115 return strlen(buf
) + 1;
118 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,21)
119 extern struct net init_net
;
122 static ssize_t
led_device_name_store(struct device
*dev
,
123 struct device_attribute
*attr
, const char *buf
, size_t size
)
125 struct led_classdev
*led_cdev
= dev_get_drvdata(dev
);
126 struct led_netdev_data
*trigger_data
= led_cdev
->trigger_data
;
128 if (size
< 0 || size
>= IFNAMSIZ
)
131 write_lock(&trigger_data
->lock
);
133 strcpy(trigger_data
->device_name
, buf
);
134 if (size
> 0 && trigger_data
->device_name
[size
-1] == '\n')
135 trigger_data
->device_name
[size
-1] = 0;
137 if (trigger_data
->device_name
[0] != 0) {
138 /* check for existing device to update from */
139 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,26)
140 trigger_data
->net_dev
= dev_get_by_name(&init_net
, trigger_data
->device_name
);
142 trigger_data
->net_dev
= dev_get_by_name(trigger_data
->device_name
);
144 if (trigger_data
->net_dev
!= NULL
)
145 trigger_data
->link_up
= (dev_get_flags(trigger_data
->net_dev
) & IFF_LOWER_UP
) != 0;
146 set_baseline_state(trigger_data
); /* updates LEDs, may start timers */
149 write_unlock(&trigger_data
->lock
);
153 static DEVICE_ATTR(device_name
, 0644, led_device_name_show
, led_device_name_store
);
155 static ssize_t
led_mode_show(struct device
*dev
,
156 struct device_attribute
*attr
, char *buf
)
158 struct led_classdev
*led_cdev
= dev_get_drvdata(dev
);
159 struct led_netdev_data
*trigger_data
= led_cdev
->trigger_data
;
161 read_lock(&trigger_data
->lock
);
163 if (trigger_data
->mode
== 0) {
164 strcpy(buf
, "none\n");
166 if (trigger_data
->mode
& MODE_LINK
)
167 strcat(buf
, "link ");
168 if (trigger_data
->mode
& MODE_TX
)
170 if (trigger_data
->mode
& MODE_RX
)
175 read_unlock(&trigger_data
->lock
);
177 return strlen(buf
)+1;
180 static ssize_t
led_mode_store(struct device
*dev
,
181 struct device_attribute
*attr
, const char *buf
, size_t size
)
183 struct led_classdev
*led_cdev
= dev_get_drvdata(dev
);
184 struct led_netdev_data
*trigger_data
= led_cdev
->trigger_data
;
189 /* take a copy since we don't want to trash the inbound buffer when using strsep */
190 strncpy(copybuf
, buf
, sizeof(copybuf
));
194 while ((token
= strsep(&p
, " \t\n")) != NULL
) {
201 if (!strcmp(token
, "none"))
203 else if (!strcmp(token
, "tx"))
205 else if (!strcmp(token
, "rx"))
207 else if (!strcmp(token
, "link"))
208 new_mode
|= MODE_LINK
;
216 write_lock(&trigger_data
->lock
);
217 trigger_data
->mode
= new_mode
;
218 set_baseline_state(trigger_data
);
219 write_unlock(&trigger_data
->lock
);
224 static DEVICE_ATTR(mode
, 0644, led_mode_show
, led_mode_store
);
226 static ssize_t
led_interval_show(struct device
*dev
,
227 struct device_attribute
*attr
, char *buf
)
229 struct led_classdev
*led_cdev
= dev_get_drvdata(dev
);
230 struct led_netdev_data
*trigger_data
= led_cdev
->trigger_data
;
232 read_lock(&trigger_data
->lock
);
233 sprintf(buf
, "%u\n", jiffies_to_msecs(trigger_data
->interval
));
234 read_unlock(&trigger_data
->lock
);
236 return strlen(buf
) + 1;
239 static ssize_t
led_interval_store(struct device
*dev
,
240 struct device_attribute
*attr
, const char *buf
, size_t size
)
242 struct led_classdev
*led_cdev
= dev_get_drvdata(dev
);
243 struct led_netdev_data
*trigger_data
= led_cdev
->trigger_data
;
246 unsigned long value
= simple_strtoul(buf
, &after
, 10);
247 size_t count
= after
- buf
;
249 if (*after
&& isspace(*after
))
252 /* impose some basic bounds on the timer interval */
253 if (count
== size
&& value
>= 5 && value
<= 10000) {
254 write_lock(&trigger_data
->lock
);
255 trigger_data
->interval
= msecs_to_jiffies(value
);
256 set_baseline_state(trigger_data
); // resets timer
257 write_unlock(&trigger_data
->lock
);
264 static DEVICE_ATTR(interval
, 0644, led_interval_show
, led_interval_store
);
266 static int netdev_trig_notify(struct notifier_block
*nb
,
270 struct net_device
*dev
= dv
;
271 struct led_netdev_data
*trigger_data
= container_of(nb
, struct led_netdev_data
, notifier
);
273 if (evt
!= NETDEV_UP
&& evt
!= NETDEV_DOWN
&& evt
!= NETDEV_CHANGE
&& evt
!= NETDEV_REGISTER
&& evt
!= NETDEV_UNREGISTER
)
276 write_lock(&trigger_data
->lock
);
278 if (strcmp(dev
->name
, trigger_data
->device_name
))
281 if (evt
== NETDEV_REGISTER
) {
282 if (trigger_data
->net_dev
!= NULL
)
283 dev_put(trigger_data
->net_dev
);
285 trigger_data
->net_dev
= dev
;
286 trigger_data
->link_up
= 0;
290 if (evt
== NETDEV_UNREGISTER
&& trigger_data
->net_dev
!= NULL
) {
291 dev_put(trigger_data
->net_dev
);
292 trigger_data
->net_dev
= NULL
;
296 /* UP / DOWN / CHANGE */
298 trigger_data
->link_up
= (evt
!= NETDEV_DOWN
&& netif_carrier_ok(dev
));
299 set_baseline_state(trigger_data
);
302 write_unlock(&trigger_data
->lock
);
306 /* here's the real work! */
307 static void netdev_trig_timer(unsigned long arg
)
309 struct led_netdev_data
*trigger_data
= (struct led_netdev_data
*)arg
;
310 struct net_device_stats
*dev_stats
;
311 unsigned new_activity
;
313 write_lock(&trigger_data
->lock
);
315 if (!trigger_data
->link_up
|| !trigger_data
->net_dev
|| (trigger_data
->mode
& (MODE_TX
| MODE_RX
)) == 0) {
316 /* we don't need to do timer work, just reflect link state. */
317 led_set_brightness(trigger_data
->led_cdev
, ((trigger_data
->mode
& MODE_LINK
) != 0 && trigger_data
->link_up
) ? LED_FULL
: LED_OFF
);
320 #ifdef CONFIG_COMPAT_NET_DEV_OPS
321 dev_stats
= trigger_data
->net_dev
->get_stats(trigger_data
->net_dev
);
323 dev_stats
= trigger_data
->net_dev
->netdev_ops
->ndo_get_stats(trigger_data
->net_dev
);
326 ((trigger_data
->mode
& MODE_TX
) ? dev_stats
->tx_packets
: 0) +
327 ((trigger_data
->mode
& MODE_RX
) ? dev_stats
->rx_packets
: 0);
329 if (trigger_data
->mode
& MODE_LINK
) {
330 /* base state is ON (link present) */
331 /* if there's no link, we don't get this far and the LED is off */
333 /* OFF -> ON always */
334 /* ON -> OFF on activity */
335 if (trigger_data
->led_cdev
->brightness
== LED_OFF
) {
336 led_set_brightness(trigger_data
->led_cdev
, LED_FULL
);
337 } else if (trigger_data
->last_activity
!= new_activity
) {
338 led_set_brightness(trigger_data
->led_cdev
, LED_OFF
);
341 /* base state is OFF */
342 /* ON -> OFF always */
343 /* OFF -> ON on activity */
344 if (trigger_data
->led_cdev
->brightness
== LED_FULL
) {
345 led_set_brightness(trigger_data
->led_cdev
, LED_OFF
);
346 } else if (trigger_data
->last_activity
!= new_activity
) {
347 led_set_brightness(trigger_data
->led_cdev
, LED_FULL
);
351 trigger_data
->last_activity
= new_activity
;
352 mod_timer(&trigger_data
->timer
, jiffies
+ trigger_data
->interval
);
355 write_unlock(&trigger_data
->lock
);
358 static void netdev_trig_activate(struct led_classdev
*led_cdev
)
360 struct led_netdev_data
*trigger_data
;
363 trigger_data
= kzalloc(sizeof(struct led_netdev_data
), GFP_KERNEL
);
367 rwlock_init(&trigger_data
->lock
);
369 trigger_data
->notifier
.notifier_call
= netdev_trig_notify
;
370 trigger_data
->notifier
.priority
= 10;
372 setup_timer(&trigger_data
->timer
, netdev_trig_timer
, (unsigned long) trigger_data
);
374 trigger_data
->led_cdev
= led_cdev
;
375 trigger_data
->net_dev
= NULL
;
376 trigger_data
->device_name
[0] = 0;
378 trigger_data
->mode
= 0;
379 trigger_data
->interval
= msecs_to_jiffies(50);
380 trigger_data
->link_up
= 0;
381 trigger_data
->last_activity
= 0;
383 led_cdev
->trigger_data
= trigger_data
;
385 rc
= device_create_file(led_cdev
->dev
, &dev_attr_device_name
);
388 rc
= device_create_file(led_cdev
->dev
, &dev_attr_mode
);
390 goto err_out_device_name
;
391 rc
= device_create_file(led_cdev
->dev
, &dev_attr_interval
);
395 register_netdevice_notifier(&trigger_data
->notifier
);
399 device_remove_file(led_cdev
->dev
, &dev_attr_mode
);
401 device_remove_file(led_cdev
->dev
, &dev_attr_device_name
);
403 led_cdev
->trigger_data
= NULL
;
407 static void netdev_trig_deactivate(struct led_classdev
*led_cdev
)
409 struct led_netdev_data
*trigger_data
= led_cdev
->trigger_data
;
412 unregister_netdevice_notifier(&trigger_data
->notifier
);
414 device_remove_file(led_cdev
->dev
, &dev_attr_device_name
);
415 device_remove_file(led_cdev
->dev
, &dev_attr_mode
);
416 device_remove_file(led_cdev
->dev
, &dev_attr_interval
);
418 write_lock(&trigger_data
->lock
);
420 if (trigger_data
->net_dev
) {
421 dev_put(trigger_data
->net_dev
);
422 trigger_data
->net_dev
= NULL
;
425 write_unlock(&trigger_data
->lock
);
427 del_timer_sync(&trigger_data
->timer
);
433 static struct led_trigger netdev_led_trigger
= {
435 .activate
= netdev_trig_activate
,
436 .deactivate
= netdev_trig_deactivate
,
439 static int __init
netdev_trig_init(void)
441 return led_trigger_register(&netdev_led_trigger
);
444 static void __exit
netdev_trig_exit(void)
446 led_trigger_unregister(&netdev_led_trigger
);
449 module_init(netdev_trig_init
);
450 module_exit(netdev_trig_exit
);
452 MODULE_AUTHOR("Oliver Jowett <oliver@opencloud.com>");
453 MODULE_DESCRIPTION("Netdev LED trigger");
454 MODULE_LICENSE("GPL");