ledtrig-netdev: reset link status & stats after changing device_name
[openwrt/openwrt.git] / target / linux / generic / files / drivers / leds / ledtrig-netdev.c
1 /*
2 * LED Kernel Netdev Trigger
3 *
4 * Toggles the LED to reflect the link and traffic state of a named net device
5 *
6 * Copyright 2007 Oliver Jowett <oliver@opencloud.com>
7 *
8 * Derived from ledtrig-timer.c which is:
9 * Copyright 2005-2006 Openedhand Ltd.
10 * Author: Richard Purdie <rpurdie@openedhand.com>
11 *
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.
15 *
16 */
17
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/netdevice.h>
26 #include <linux/timer.h>
27 #include <linux/ctype.h>
28 #include <linux/leds.h>
29
30 #include "leds.h"
31
32 /*
33 * Configurable sysfs attributes:
34 *
35 * device_name - network device name to monitor
36 *
37 * interval - duration of LED blink, in milliseconds
38 *
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
43 *
44 * Some suggestions:
45 *
46 * Simple link status LED:
47 * $ echo netdev >someled/trigger
48 * $ echo eth0 >someled/device_name
49 * $ echo link >someled/mode
50 *
51 * Ethernet-style link/activity LED:
52 * $ echo netdev >someled/trigger
53 * $ echo eth0 >someled/device_name
54 * $ echo "link tx rx" >someled/mode
55 *
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
63 *
64 */
65
66 #define MODE_LINK 1
67 #define MODE_TX 2
68 #define MODE_RX 4
69
70 struct led_netdev_data {
71 spinlock_t lock;
72
73 struct timer_list timer;
74 struct notifier_block notifier;
75
76 struct led_classdev *led_cdev;
77 struct net_device *net_dev;
78
79 char device_name[IFNAMSIZ];
80 unsigned interval;
81 unsigned mode;
82 unsigned link_up;
83 unsigned last_activity;
84 };
85
86 static void set_baseline_state(struct led_netdev_data *trigger_data)
87 {
88 if ((trigger_data->mode & MODE_LINK) != 0 && trigger_data->link_up)
89 led_set_brightness(trigger_data->led_cdev, LED_FULL);
90 else
91 led_set_brightness(trigger_data->led_cdev, LED_OFF);
92
93 if ((trigger_data->mode & (MODE_TX | MODE_RX)) != 0 && trigger_data->link_up)
94 mod_timer(&trigger_data->timer, jiffies + trigger_data->interval);
95 }
96
97 static ssize_t led_device_name_show(struct device *dev,
98 struct device_attribute *attr, char *buf)
99 {
100 struct led_classdev *led_cdev = dev_get_drvdata(dev);
101 struct led_netdev_data *trigger_data = led_cdev->trigger_data;
102
103 spin_lock_bh(&trigger_data->lock);
104 sprintf(buf, "%s\n", trigger_data->device_name);
105 spin_unlock_bh(&trigger_data->lock);
106
107 return strlen(buf) + 1;
108 }
109
110 static ssize_t led_device_name_store(struct device *dev,
111 struct device_attribute *attr, const char *buf, size_t size)
112 {
113 struct led_classdev *led_cdev = dev_get_drvdata(dev);
114 struct led_netdev_data *trigger_data = led_cdev->trigger_data;
115
116 if (size < 0 || size >= IFNAMSIZ)
117 return -EINVAL;
118
119 spin_lock_bh(&trigger_data->lock);
120 del_timer_sync(&trigger_data->timer);
121
122 strcpy(trigger_data->device_name, buf);
123 if (size > 0 && trigger_data->device_name[size-1] == '\n')
124 trigger_data->device_name[size-1] = 0;
125 trigger_data->link_up = 0;
126 trigger_data->last_activity = 0;
127
128 if (trigger_data->device_name[0] != 0) {
129 /* check for existing device to update from */
130 trigger_data->net_dev = dev_get_by_name(&init_net, trigger_data->device_name);
131 if (trigger_data->net_dev != NULL)
132 trigger_data->link_up = (dev_get_flags(trigger_data->net_dev) & IFF_LOWER_UP) != 0;
133 }
134
135 set_baseline_state(trigger_data);
136 spin_unlock_bh(&trigger_data->lock);
137
138 return size;
139 }
140
141 static DEVICE_ATTR(device_name, 0644, led_device_name_show, led_device_name_store);
142
143 static ssize_t led_mode_show(struct device *dev,
144 struct device_attribute *attr, char *buf)
145 {
146 struct led_classdev *led_cdev = dev_get_drvdata(dev);
147 struct led_netdev_data *trigger_data = led_cdev->trigger_data;
148
149 spin_lock_bh(&trigger_data->lock);
150
151 if (trigger_data->mode == 0) {
152 strcpy(buf, "none\n");
153 } else {
154 if (trigger_data->mode & MODE_LINK)
155 strcat(buf, "link ");
156 if (trigger_data->mode & MODE_TX)
157 strcat(buf, "tx ");
158 if (trigger_data->mode & MODE_RX)
159 strcat(buf, "rx ");
160 strcat(buf, "\n");
161 }
162
163 spin_unlock_bh(&trigger_data->lock);
164
165 return strlen(buf)+1;
166 }
167
168 static ssize_t led_mode_store(struct device *dev,
169 struct device_attribute *attr, const char *buf, size_t size)
170 {
171 struct led_classdev *led_cdev = dev_get_drvdata(dev);
172 struct led_netdev_data *trigger_data = led_cdev->trigger_data;
173 char copybuf[128];
174 int new_mode = -1;
175 char *p, *token;
176
177 /* take a copy since we don't want to trash the inbound buffer when using strsep */
178 strncpy(copybuf, buf, sizeof(copybuf));
179 copybuf[sizeof(copybuf) - 1] = 0;
180 p = copybuf;
181
182 while ((token = strsep(&p, " \t\n")) != NULL) {
183 if (!*token)
184 continue;
185
186 if (new_mode == -1)
187 new_mode = 0;
188
189 if (!strcmp(token, "none"))
190 new_mode = 0;
191 else if (!strcmp(token, "tx"))
192 new_mode |= MODE_TX;
193 else if (!strcmp(token, "rx"))
194 new_mode |= MODE_RX;
195 else if (!strcmp(token, "link"))
196 new_mode |= MODE_LINK;
197 else
198 return -EINVAL;
199 }
200
201 if (new_mode == -1)
202 return -EINVAL;
203
204 spin_lock_bh(&trigger_data->lock);
205 del_timer_sync(&trigger_data->timer);
206
207 trigger_data->mode = new_mode;
208
209 set_baseline_state(trigger_data);
210 spin_unlock_bh(&trigger_data->lock);
211
212 return size;
213 }
214
215 static DEVICE_ATTR(mode, 0644, led_mode_show, led_mode_store);
216
217 static ssize_t led_interval_show(struct device *dev,
218 struct device_attribute *attr, char *buf)
219 {
220 struct led_classdev *led_cdev = dev_get_drvdata(dev);
221 struct led_netdev_data *trigger_data = led_cdev->trigger_data;
222
223 spin_lock_bh(&trigger_data->lock);
224 sprintf(buf, "%u\n", jiffies_to_msecs(trigger_data->interval));
225 spin_unlock_bh(&trigger_data->lock);
226
227 return strlen(buf) + 1;
228 }
229
230 static ssize_t led_interval_store(struct device *dev,
231 struct device_attribute *attr, const char *buf, size_t size)
232 {
233 struct led_classdev *led_cdev = dev_get_drvdata(dev);
234 struct led_netdev_data *trigger_data = led_cdev->trigger_data;
235 int ret = -EINVAL;
236 char *after;
237 unsigned long value = simple_strtoul(buf, &after, 10);
238 size_t count = after - buf;
239
240 if (isspace(*after))
241 count++;
242
243 /* impose some basic bounds on the timer interval */
244 if (count == size && value >= 5 && value <= 10000) {
245 spin_lock_bh(&trigger_data->lock);
246 del_timer_sync(&trigger_data->timer);
247
248 trigger_data->interval = msecs_to_jiffies(value);
249
250 set_baseline_state(trigger_data); /* resets timer */
251 spin_unlock_bh(&trigger_data->lock);
252
253 ret = count;
254 }
255
256 return ret;
257 }
258
259 static DEVICE_ATTR(interval, 0644, led_interval_show, led_interval_store);
260
261 static int netdev_trig_notify(struct notifier_block *nb,
262 unsigned long evt,
263 void *dv)
264 {
265 struct net_device *dev = netdev_notifier_info_to_dev((struct netdev_notifier_info *) dv);
266 struct led_netdev_data *trigger_data = container_of(nb, struct led_netdev_data, notifier);
267
268 if (evt != NETDEV_UP && evt != NETDEV_DOWN && evt != NETDEV_CHANGE && evt != NETDEV_REGISTER && evt != NETDEV_UNREGISTER)
269 return NOTIFY_DONE;
270
271 spin_lock_bh(&trigger_data->lock);
272 del_timer_sync(&trigger_data->timer);
273
274 if (strcmp(dev->name, trigger_data->device_name))
275 goto done;
276
277 if (evt == NETDEV_REGISTER) {
278 if (trigger_data->net_dev != NULL)
279 dev_put(trigger_data->net_dev);
280 dev_hold(dev);
281 trigger_data->net_dev = dev;
282 trigger_data->link_up = 0;
283 goto done;
284 }
285
286 if (evt == NETDEV_UNREGISTER && trigger_data->net_dev != NULL) {
287 dev_put(trigger_data->net_dev);
288 trigger_data->net_dev = NULL;
289 goto done;
290 }
291
292 /* UP / DOWN / CHANGE */
293
294 trigger_data->link_up = (evt != NETDEV_DOWN && netif_carrier_ok(dev));
295 set_baseline_state(trigger_data);
296
297 done:
298 spin_unlock_bh(&trigger_data->lock);
299 return NOTIFY_DONE;
300 }
301
302 /* here's the real work! */
303 static void netdev_trig_timer(unsigned long arg)
304 {
305 struct led_netdev_data *trigger_data = (struct led_netdev_data *)arg;
306 struct rtnl_link_stats64 *dev_stats;
307 unsigned new_activity;
308 struct rtnl_link_stats64 temp;
309
310 if (!trigger_data->link_up || !trigger_data->net_dev || (trigger_data->mode & (MODE_TX | MODE_RX)) == 0) {
311 /* we don't need to do timer work, just reflect link state. */
312 led_set_brightness(trigger_data->led_cdev, ((trigger_data->mode & MODE_LINK) != 0 && trigger_data->link_up) ? LED_FULL : LED_OFF);
313 return;
314 }
315
316 dev_stats = dev_get_stats(trigger_data->net_dev, &temp);
317 new_activity =
318 ((trigger_data->mode & MODE_TX) ? dev_stats->tx_packets : 0) +
319 ((trigger_data->mode & MODE_RX) ? dev_stats->rx_packets : 0);
320
321 if (trigger_data->mode & MODE_LINK) {
322 /* base state is ON (link present) */
323 /* if there's no link, we don't get this far and the LED is off */
324
325 /* OFF -> ON always */
326 /* ON -> OFF on activity */
327 if (trigger_data->led_cdev->brightness == LED_OFF) {
328 led_set_brightness(trigger_data->led_cdev, LED_FULL);
329 } else if (trigger_data->last_activity != new_activity) {
330 led_set_brightness(trigger_data->led_cdev, LED_OFF);
331 }
332 } else {
333 /* base state is OFF */
334 /* ON -> OFF always */
335 /* OFF -> ON on activity */
336 if (trigger_data->led_cdev->brightness == LED_FULL) {
337 led_set_brightness(trigger_data->led_cdev, LED_OFF);
338 } else if (trigger_data->last_activity != new_activity) {
339 led_set_brightness(trigger_data->led_cdev, LED_FULL);
340 }
341 }
342
343 trigger_data->last_activity = new_activity;
344 mod_timer(&trigger_data->timer, jiffies + trigger_data->interval);
345 }
346
347 static void netdev_trig_activate(struct led_classdev *led_cdev)
348 {
349 struct led_netdev_data *trigger_data;
350 int rc;
351
352 trigger_data = kzalloc(sizeof(struct led_netdev_data), GFP_KERNEL);
353 if (!trigger_data)
354 return;
355
356 spin_lock_init(&trigger_data->lock);
357
358 trigger_data->notifier.notifier_call = netdev_trig_notify;
359 trigger_data->notifier.priority = 10;
360
361 setup_timer(&trigger_data->timer, netdev_trig_timer, (unsigned long) trigger_data);
362
363 trigger_data->led_cdev = led_cdev;
364 trigger_data->net_dev = NULL;
365 trigger_data->device_name[0] = 0;
366
367 trigger_data->mode = 0;
368 trigger_data->interval = msecs_to_jiffies(50);
369 trigger_data->link_up = 0;
370 trigger_data->last_activity = 0;
371
372 led_cdev->trigger_data = trigger_data;
373
374 rc = device_create_file(led_cdev->dev, &dev_attr_device_name);
375 if (rc)
376 goto err_out;
377 rc = device_create_file(led_cdev->dev, &dev_attr_mode);
378 if (rc)
379 goto err_out_device_name;
380 rc = device_create_file(led_cdev->dev, &dev_attr_interval);
381 if (rc)
382 goto err_out_mode;
383
384 register_netdevice_notifier(&trigger_data->notifier);
385 return;
386
387 err_out_mode:
388 device_remove_file(led_cdev->dev, &dev_attr_mode);
389 err_out_device_name:
390 device_remove_file(led_cdev->dev, &dev_attr_device_name);
391 err_out:
392 led_cdev->trigger_data = NULL;
393 kfree(trigger_data);
394 }
395
396 static void netdev_trig_deactivate(struct led_classdev *led_cdev)
397 {
398 struct led_netdev_data *trigger_data = led_cdev->trigger_data;
399
400 if (trigger_data) {
401 unregister_netdevice_notifier(&trigger_data->notifier);
402
403 device_remove_file(led_cdev->dev, &dev_attr_device_name);
404 device_remove_file(led_cdev->dev, &dev_attr_mode);
405 device_remove_file(led_cdev->dev, &dev_attr_interval);
406
407 spin_lock_bh(&trigger_data->lock);
408 del_timer_sync(&trigger_data->timer);
409
410 if (trigger_data->net_dev) {
411 dev_put(trigger_data->net_dev);
412 trigger_data->net_dev = NULL;
413 }
414
415 spin_unlock_bh(&trigger_data->lock);
416
417 kfree(trigger_data);
418 }
419 }
420
421 static struct led_trigger netdev_led_trigger = {
422 .name = "netdev",
423 .activate = netdev_trig_activate,
424 .deactivate = netdev_trig_deactivate,
425 };
426
427 static int __init netdev_trig_init(void)
428 {
429 return led_trigger_register(&netdev_led_trigger);
430 }
431
432 static void __exit netdev_trig_exit(void)
433 {
434 led_trigger_unregister(&netdev_led_trigger);
435 }
436
437 module_init(netdev_trig_init);
438 module_exit(netdev_trig_exit);
439
440 MODULE_AUTHOR("Oliver Jowett <oliver@opencloud.com>");
441 MODULE_DESCRIPTION("Netdev LED trigger");
442 MODULE_LICENSE("GPL");