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>
33 * Configurable sysfs attributes:
35 * device_name - network device name to monitor
37 * interval - duration of LED blink, in milliseconds
39 * mode - either "none" (LED is off) or a space separated list of one or more of:
40 * link: LED's normal state reflects whether the link is up (has carrier) or not
41 * tx: LED blinks on transmitted data
42 * rx: LED blinks on receive data
46 * Simple link status LED:
47 * $ echo netdev >someled/trigger
48 * $ echo eth0 >someled/device_name
49 * $ echo link >someled/mode
51 * Ethernet-style link/activity LED:
52 * $ echo netdev >someled/trigger
53 * $ echo eth0 >someled/device_name
54 * $ echo "link tx rx" >someled/mode
56 * Modem-style tx/rx LEDs:
57 * $ echo netdev >led1/trigger
58 * $ echo ppp0 >led1/device_name
59 * $ echo tx >led1/mode
60 * $ echo netdev >led2/trigger
61 * $ echo ppp0 >led2/device_name
62 * $ echo rx >led2/mode
70 struct led_netdev_data
{
73 struct timer_list timer
;
74 struct notifier_block notifier
;
76 struct led_classdev
*led_cdev
;
77 struct net_device
*net_dev
;
79 char device_name
[IFNAMSIZ
];
83 unsigned last_activity
;
86 static void set_baseline_state(struct led_netdev_data
*trigger_data
)
88 if ((trigger_data
->mode
& MODE_LINK
) != 0 && trigger_data
->link_up
)
89 led_set_brightness(trigger_data
->led_cdev
, LED_FULL
);
91 led_set_brightness(trigger_data
->led_cdev
, LED_OFF
);
93 if ((trigger_data
->mode
& (MODE_TX
| MODE_RX
)) != 0 && trigger_data
->link_up
)
94 mod_timer(&trigger_data
->timer
, jiffies
+ trigger_data
->interval
);
96 del_timer(&trigger_data
->timer
);
99 static ssize_t
led_device_name_show(struct device
*dev
,
100 struct device_attribute
*attr
, char *buf
)
102 struct led_classdev
*led_cdev
= dev_get_drvdata(dev
);
103 struct led_netdev_data
*trigger_data
= led_cdev
->trigger_data
;
105 read_lock(&trigger_data
->lock
);
106 sprintf(buf
, "%s\n", trigger_data
->device_name
);
107 read_unlock(&trigger_data
->lock
);
109 return strlen(buf
) + 1;
112 static ssize_t
led_device_name_store(struct device
*dev
,
113 struct device_attribute
*attr
, const char *buf
, size_t size
)
115 struct led_classdev
*led_cdev
= dev_get_drvdata(dev
);
116 struct led_netdev_data
*trigger_data
= led_cdev
->trigger_data
;
118 if (size
< 0 || size
>= IFNAMSIZ
)
121 write_lock(&trigger_data
->lock
);
123 strcpy(trigger_data
->device_name
, buf
);
124 if (size
> 0 && trigger_data
->device_name
[size
-1] == '\n')
125 trigger_data
->device_name
[size
-1] = 0;
127 if (trigger_data
->device_name
[0] != 0) {
128 /* check for existing device to update from */
129 trigger_data
->net_dev
= dev_get_by_name(trigger_data
->device_name
);
130 if (trigger_data
->net_dev
!= NULL
)
131 trigger_data
->link_up
= (dev_get_flags(trigger_data
->net_dev
) & IFF_LOWER_UP
) != 0;
132 set_baseline_state(trigger_data
); /* updates LEDs, may start timers */
135 write_unlock(&trigger_data
->lock
);
139 static DEVICE_ATTR(device_name
, 0644, led_device_name_show
, led_device_name_store
);
141 static ssize_t
led_mode_show(struct device
*dev
,
142 struct device_attribute
*attr
, char *buf
)
144 struct led_classdev
*led_cdev
= dev_get_drvdata(dev
);
145 struct led_netdev_data
*trigger_data
= led_cdev
->trigger_data
;
147 read_lock(&trigger_data
->lock
);
149 if (trigger_data
->mode
== 0) {
150 strcpy(buf
, "none\n");
152 if (trigger_data
->mode
& MODE_LINK
)
153 strcat(buf
, "link ");
154 if (trigger_data
->mode
& MODE_TX
)
156 if (trigger_data
->mode
& MODE_RX
)
161 read_unlock(&trigger_data
->lock
);
163 return strlen(buf
)+1;
166 static ssize_t
led_mode_store(struct device
*dev
,
167 struct device_attribute
*attr
, const char *buf
, size_t size
)
169 struct led_classdev
*led_cdev
= dev_get_drvdata(dev
);
170 struct led_netdev_data
*trigger_data
= led_cdev
->trigger_data
;
175 /* take a copy since we don't want to trash the inbound buffer when using strsep */
176 strncpy(copybuf
, buf
, sizeof(copybuf
));
180 while ((token
= strsep(&p
, " \t\n")) != NULL
) {
187 if (!strcmp(token
, "none"))
189 else if (!strcmp(token
, "tx"))
191 else if (!strcmp(token
, "rx"))
193 else if (!strcmp(token
, "link"))
194 new_mode
|= MODE_LINK
;
202 write_lock(&trigger_data
->lock
);
203 trigger_data
->mode
= new_mode
;
204 set_baseline_state(trigger_data
);
205 write_unlock(&trigger_data
->lock
);
210 static DEVICE_ATTR(mode
, 0644, led_mode_show
, led_mode_store
);
212 static ssize_t
led_interval_show(struct device
*dev
,
213 struct device_attribute
*attr
, char *buf
)
215 struct led_classdev
*led_cdev
= dev_get_drvdata(dev
);
216 struct led_netdev_data
*trigger_data
= led_cdev
->trigger_data
;
218 read_lock(&trigger_data
->lock
);
219 sprintf(buf
, "%u\n", jiffies_to_msecs(trigger_data
->interval
));
220 read_unlock(&trigger_data
->lock
);
222 return strlen(buf
) + 1;
225 static ssize_t
led_interval_store(struct device
*dev
,
226 struct device_attribute
*attr
, const char *buf
, size_t size
)
228 struct led_classdev
*led_cdev
= dev_get_drvdata(dev
);
229 struct led_netdev_data
*trigger_data
= led_cdev
->trigger_data
;
232 unsigned long value
= simple_strtoul(buf
, &after
, 10);
233 size_t count
= after
- buf
;
235 if (*after
&& isspace(*after
))
238 /* impose some basic bounds on the timer interval */
239 if (count
== size
&& value
>= 5 && value
<= 10000) {
240 write_lock(&trigger_data
->lock
);
241 trigger_data
->interval
= msecs_to_jiffies(value
);
242 set_baseline_state(trigger_data
); // resets timer
243 write_unlock(&trigger_data
->lock
);
250 static DEVICE_ATTR(interval
, 0644, led_interval_show
, led_interval_store
);
252 static int netdev_trig_notify(struct notifier_block
*nb
,
256 struct net_device
*dev
= dv
;
257 struct led_netdev_data
*trigger_data
= container_of(nb
, struct led_netdev_data
, notifier
);
259 if (evt
!= NETDEV_UP
&& evt
!= NETDEV_DOWN
&& evt
!= NETDEV_CHANGE
&& evt
!= NETDEV_REGISTER
&& evt
!= NETDEV_UNREGISTER
)
262 write_lock(&trigger_data
->lock
);
264 if (strcmp(dev
->name
, trigger_data
->device_name
))
267 if (evt
== NETDEV_REGISTER
) {
268 if (trigger_data
->net_dev
!= NULL
)
269 dev_put(trigger_data
->net_dev
);
271 trigger_data
->net_dev
= dev
;
272 trigger_data
->link_up
= 0;
276 if (evt
== NETDEV_UNREGISTER
&& trigger_data
->net_dev
!= NULL
) {
277 dev_put(trigger_data
->net_dev
);
278 trigger_data
->net_dev
= NULL
;
282 /* UP / DOWN / CHANGE */
284 trigger_data
->link_up
= (evt
!= NETDEV_DOWN
&& netif_carrier_ok(dev
));
285 set_baseline_state(trigger_data
);
288 write_unlock(&trigger_data
->lock
);
292 /* here's the real work! */
293 static void netdev_trig_timer(unsigned long arg
)
295 struct led_netdev_data
*trigger_data
= (struct led_netdev_data
*)arg
;
296 struct net_device_stats
*dev_stats
;
297 unsigned new_activity
;
299 write_lock(&trigger_data
->lock
);
301 if (!trigger_data
->link_up
|| !trigger_data
->net_dev
|| (trigger_data
->mode
& (MODE_TX
| MODE_RX
)) == 0) {
302 /* we don't need to do timer work, just reflect link state. */
303 led_set_brightness(trigger_data
->led_cdev
, ((trigger_data
->mode
& MODE_LINK
) != 0 && trigger_data
->link_up
) ? LED_FULL
: LED_OFF
);
307 dev_stats
= trigger_data
->net_dev
->get_stats(trigger_data
->net_dev
);
309 ((trigger_data
->mode
& MODE_TX
) ? dev_stats
->tx_packets
: 0) +
310 ((trigger_data
->mode
& MODE_RX
) ? dev_stats
->rx_packets
: 0);
312 if (trigger_data
->mode
& MODE_LINK
) {
313 /* base state is ON (link present) */
314 /* if there's no link, we don't get this far and the LED is off */
316 /* OFF -> ON always */
317 /* ON -> OFF on activity */
318 if (trigger_data
->led_cdev
->brightness
== LED_OFF
) {
319 led_set_brightness(trigger_data
->led_cdev
, LED_FULL
);
320 } else if (trigger_data
->last_activity
!= new_activity
) {
321 led_set_brightness(trigger_data
->led_cdev
, LED_OFF
);
324 /* base state is OFF */
325 /* ON -> OFF always */
326 /* OFF -> ON on activity */
327 if (trigger_data
->led_cdev
->brightness
== LED_FULL
) {
328 led_set_brightness(trigger_data
->led_cdev
, LED_OFF
);
329 } else if (trigger_data
->last_activity
!= new_activity
) {
330 led_set_brightness(trigger_data
->led_cdev
, LED_FULL
);
334 trigger_data
->last_activity
= new_activity
;
335 mod_timer(&trigger_data
->timer
, jiffies
+ trigger_data
->interval
);
338 write_unlock(&trigger_data
->lock
);
341 static void netdev_trig_activate(struct led_classdev
*led_cdev
)
343 struct led_netdev_data
*trigger_data
;
346 trigger_data
= kzalloc(sizeof(struct led_netdev_data
), GFP_KERNEL
);
350 rwlock_init(&trigger_data
->lock
);
352 trigger_data
->notifier
.notifier_call
= netdev_trig_notify
;
353 trigger_data
->notifier
.priority
= 10;
355 setup_timer(&trigger_data
->timer
, netdev_trig_timer
, (unsigned long) trigger_data
);
357 trigger_data
->led_cdev
= led_cdev
;
358 trigger_data
->net_dev
= NULL
;
359 trigger_data
->device_name
[0] = 0;
361 trigger_data
->mode
= 0;
362 trigger_data
->interval
= msecs_to_jiffies(50);
363 trigger_data
->link_up
= 0;
364 trigger_data
->last_activity
= 0;
366 led_cdev
->trigger_data
= trigger_data
;
368 rc
= device_create_file(led_cdev
->dev
, &dev_attr_device_name
);
371 rc
= device_create_file(led_cdev
->dev
, &dev_attr_mode
);
373 goto err_out_device_name
;
374 rc
= device_create_file(led_cdev
->dev
, &dev_attr_interval
);
378 register_netdevice_notifier(&trigger_data
->notifier
);
382 device_remove_file(led_cdev
->dev
, &dev_attr_mode
);
384 device_remove_file(led_cdev
->dev
, &dev_attr_device_name
);
386 led_cdev
->trigger_data
= NULL
;
390 static void netdev_trig_deactivate(struct led_classdev
*led_cdev
)
392 struct led_netdev_data
*trigger_data
= led_cdev
->trigger_data
;
395 unregister_netdevice_notifier(&trigger_data
->notifier
);
397 device_remove_file(led_cdev
->dev
, &dev_attr_device_name
);
398 device_remove_file(led_cdev
->dev
, &dev_attr_mode
);
399 device_remove_file(led_cdev
->dev
, &dev_attr_interval
);
401 write_lock(&trigger_data
->lock
);
403 if (trigger_data
->net_dev
) {
404 dev_put(trigger_data
->net_dev
);
405 trigger_data
->net_dev
= NULL
;
408 write_unlock(&trigger_data
->lock
);
410 del_timer_sync(&trigger_data
->timer
);
416 static struct led_trigger netdev_led_trigger
= {
418 .activate
= netdev_trig_activate
,
419 .deactivate
= netdev_trig_deactivate
,
422 static int __init
netdev_trig_init(void)
424 return led_trigger_register(&netdev_led_trigger
);
427 static void __exit
netdev_trig_exit(void)
429 led_trigger_unregister(&netdev_led_trigger
);
432 module_init(netdev_trig_init
);
433 module_exit(netdev_trig_exit
);
435 MODULE_AUTHOR("Oliver Jowett <oliver@opencloud.com>");
436 MODULE_DESCRIPTION("Netdev LED trigger");
437 MODULE_LICENSE("GPL");