018392a9209bd4e6f2c204cb6669209ccc18a3e3
[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 else
96 del_timer(&trigger_data->timer);
97 }
98
99 static ssize_t led_device_name_show(struct device *dev,
100 struct device_attribute *attr, char *buf)
101 {
102 struct led_classdev *led_cdev = dev_get_drvdata(dev);
103 struct led_netdev_data *trigger_data = led_cdev->trigger_data;
104
105 spin_lock_bh(&trigger_data->lock);
106 sprintf(buf, "%s\n", trigger_data->device_name);
107 spin_unlock_bh(&trigger_data->lock);
108
109 return strlen(buf) + 1;
110 }
111
112 static ssize_t led_device_name_store(struct device *dev,
113 struct device_attribute *attr, const char *buf, size_t size)
114 {
115 struct led_classdev *led_cdev = dev_get_drvdata(dev);
116 struct led_netdev_data *trigger_data = led_cdev->trigger_data;
117
118 if (size < 0 || size >= IFNAMSIZ)
119 return -EINVAL;
120
121 spin_lock_bh(&trigger_data->lock);
122
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;
126
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(&init_net, 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 */
133 }
134
135 spin_unlock_bh(&trigger_data->lock);
136 return size;
137 }
138
139 static DEVICE_ATTR(device_name, 0644, led_device_name_show, led_device_name_store);
140
141 static ssize_t led_mode_show(struct device *dev,
142 struct device_attribute *attr, char *buf)
143 {
144 struct led_classdev *led_cdev = dev_get_drvdata(dev);
145 struct led_netdev_data *trigger_data = led_cdev->trigger_data;
146
147 spin_lock_bh(&trigger_data->lock);
148
149 if (trigger_data->mode == 0) {
150 strcpy(buf, "none\n");
151 } else {
152 if (trigger_data->mode & MODE_LINK)
153 strcat(buf, "link ");
154 if (trigger_data->mode & MODE_TX)
155 strcat(buf, "tx ");
156 if (trigger_data->mode & MODE_RX)
157 strcat(buf, "rx ");
158 strcat(buf, "\n");
159 }
160
161 spin_unlock_bh(&trigger_data->lock);
162
163 return strlen(buf)+1;
164 }
165
166 static ssize_t led_mode_store(struct device *dev,
167 struct device_attribute *attr, const char *buf, size_t size)
168 {
169 struct led_classdev *led_cdev = dev_get_drvdata(dev);
170 struct led_netdev_data *trigger_data = led_cdev->trigger_data;
171 char copybuf[128];
172 int new_mode = -1;
173 char *p, *token;
174
175 /* take a copy since we don't want to trash the inbound buffer when using strsep */
176 strncpy(copybuf, buf, sizeof(copybuf));
177 copybuf[sizeof(copybuf) - 1] = 0;
178 p = copybuf;
179
180 while ((token = strsep(&p, " \t\n")) != NULL) {
181 if (!*token)
182 continue;
183
184 if (new_mode == -1)
185 new_mode = 0;
186
187 if (!strcmp(token, "none"))
188 new_mode = 0;
189 else if (!strcmp(token, "tx"))
190 new_mode |= MODE_TX;
191 else if (!strcmp(token, "rx"))
192 new_mode |= MODE_RX;
193 else if (!strcmp(token, "link"))
194 new_mode |= MODE_LINK;
195 else
196 return -EINVAL;
197 }
198
199 if (new_mode == -1)
200 return -EINVAL;
201
202 spin_lock_bh(&trigger_data->lock);
203 trigger_data->mode = new_mode;
204 set_baseline_state(trigger_data);
205 spin_unlock_bh(&trigger_data->lock);
206
207 return size;
208 }
209
210 static DEVICE_ATTR(mode, 0644, led_mode_show, led_mode_store);
211
212 static ssize_t led_interval_show(struct device *dev,
213 struct device_attribute *attr, char *buf)
214 {
215 struct led_classdev *led_cdev = dev_get_drvdata(dev);
216 struct led_netdev_data *trigger_data = led_cdev->trigger_data;
217
218 spin_lock_bh(&trigger_data->lock);
219 sprintf(buf, "%u\n", jiffies_to_msecs(trigger_data->interval));
220 spin_unlock_bh(&trigger_data->lock);
221
222 return strlen(buf) + 1;
223 }
224
225 static ssize_t led_interval_store(struct device *dev,
226 struct device_attribute *attr, const char *buf, size_t size)
227 {
228 struct led_classdev *led_cdev = dev_get_drvdata(dev);
229 struct led_netdev_data *trigger_data = led_cdev->trigger_data;
230 int ret = -EINVAL;
231 char *after;
232 unsigned long value = simple_strtoul(buf, &after, 10);
233 size_t count = after - buf;
234
235 if (isspace(*after))
236 count++;
237
238 /* impose some basic bounds on the timer interval */
239 if (count == size && value >= 5 && value <= 10000) {
240 spin_lock_bh(&trigger_data->lock);
241 trigger_data->interval = msecs_to_jiffies(value);
242 set_baseline_state(trigger_data); /* resets timer */
243 spin_unlock_bh(&trigger_data->lock);
244 ret = count;
245 }
246
247 return ret;
248 }
249
250 static DEVICE_ATTR(interval, 0644, led_interval_show, led_interval_store);
251
252 static int netdev_trig_notify(struct notifier_block *nb,
253 unsigned long evt,
254 void *dv)
255 {
256 struct net_device *dev = netdev_notifier_info_to_dev((struct netdev_notifier_info *) dv);
257 struct led_netdev_data *trigger_data = container_of(nb, struct led_netdev_data, notifier);
258
259 if (evt != NETDEV_UP && evt != NETDEV_DOWN && evt != NETDEV_CHANGE && evt != NETDEV_REGISTER && evt != NETDEV_UNREGISTER)
260 return NOTIFY_DONE;
261
262 spin_lock_bh(&trigger_data->lock);
263
264 if (strcmp(dev->name, trigger_data->device_name))
265 goto done;
266
267 if (evt == NETDEV_REGISTER) {
268 if (trigger_data->net_dev != NULL)
269 dev_put(trigger_data->net_dev);
270 dev_hold(dev);
271 trigger_data->net_dev = dev;
272 trigger_data->link_up = 0;
273 goto done;
274 }
275
276 if (evt == NETDEV_UNREGISTER && trigger_data->net_dev != NULL) {
277 dev_put(trigger_data->net_dev);
278 trigger_data->net_dev = NULL;
279 goto done;
280 }
281
282 /* UP / DOWN / CHANGE */
283
284 trigger_data->link_up = (evt != NETDEV_DOWN && netif_carrier_ok(dev));
285 set_baseline_state(trigger_data);
286
287 done:
288 spin_unlock_bh(&trigger_data->lock);
289 return NOTIFY_DONE;
290 }
291
292 /* here's the real work! */
293 static void netdev_trig_timer(unsigned long arg)
294 {
295 struct led_netdev_data *trigger_data = (struct led_netdev_data *)arg;
296 struct rtnl_link_stats64 *dev_stats;
297 unsigned new_activity;
298 struct rtnl_link_stats64 temp;
299
300 spin_lock(&trigger_data->lock);
301
302 if (!trigger_data->link_up || !trigger_data->net_dev || (trigger_data->mode & (MODE_TX | MODE_RX)) == 0) {
303 /* we don't need to do timer work, just reflect link state. */
304 led_set_brightness(trigger_data->led_cdev, ((trigger_data->mode & MODE_LINK) != 0 && trigger_data->link_up) ? LED_FULL : LED_OFF);
305 goto no_restart;
306 }
307
308 dev_stats = dev_get_stats(trigger_data->net_dev, &temp);
309 new_activity =
310 ((trigger_data->mode & MODE_TX) ? dev_stats->tx_packets : 0) +
311 ((trigger_data->mode & MODE_RX) ? dev_stats->rx_packets : 0);
312
313 if (trigger_data->mode & MODE_LINK) {
314 /* base state is ON (link present) */
315 /* if there's no link, we don't get this far and the LED is off */
316
317 /* OFF -> ON always */
318 /* ON -> OFF on activity */
319 if (trigger_data->led_cdev->brightness == LED_OFF) {
320 led_set_brightness(trigger_data->led_cdev, LED_FULL);
321 } else if (trigger_data->last_activity != new_activity) {
322 led_set_brightness(trigger_data->led_cdev, LED_OFF);
323 }
324 } else {
325 /* base state is OFF */
326 /* ON -> OFF always */
327 /* OFF -> ON on activity */
328 if (trigger_data->led_cdev->brightness == LED_FULL) {
329 led_set_brightness(trigger_data->led_cdev, LED_OFF);
330 } else if (trigger_data->last_activity != new_activity) {
331 led_set_brightness(trigger_data->led_cdev, LED_FULL);
332 }
333 }
334
335 trigger_data->last_activity = new_activity;
336 mod_timer(&trigger_data->timer, jiffies + trigger_data->interval);
337
338 no_restart:
339 spin_unlock(&trigger_data->lock);
340 }
341
342 static void netdev_trig_activate(struct led_classdev *led_cdev)
343 {
344 struct led_netdev_data *trigger_data;
345 int rc;
346
347 trigger_data = kzalloc(sizeof(struct led_netdev_data), GFP_KERNEL);
348 if (!trigger_data)
349 return;
350
351 spin_lock_init(&trigger_data->lock);
352
353 trigger_data->notifier.notifier_call = netdev_trig_notify;
354 trigger_data->notifier.priority = 10;
355
356 setup_timer(&trigger_data->timer, netdev_trig_timer, (unsigned long) trigger_data);
357
358 trigger_data->led_cdev = led_cdev;
359 trigger_data->net_dev = NULL;
360 trigger_data->device_name[0] = 0;
361
362 trigger_data->mode = 0;
363 trigger_data->interval = msecs_to_jiffies(50);
364 trigger_data->link_up = 0;
365 trigger_data->last_activity = 0;
366
367 led_cdev->trigger_data = trigger_data;
368
369 rc = device_create_file(led_cdev->dev, &dev_attr_device_name);
370 if (rc)
371 goto err_out;
372 rc = device_create_file(led_cdev->dev, &dev_attr_mode);
373 if (rc)
374 goto err_out_device_name;
375 rc = device_create_file(led_cdev->dev, &dev_attr_interval);
376 if (rc)
377 goto err_out_mode;
378
379 register_netdevice_notifier(&trigger_data->notifier);
380 return;
381
382 err_out_mode:
383 device_remove_file(led_cdev->dev, &dev_attr_mode);
384 err_out_device_name:
385 device_remove_file(led_cdev->dev, &dev_attr_device_name);
386 err_out:
387 led_cdev->trigger_data = NULL;
388 kfree(trigger_data);
389 }
390
391 static void netdev_trig_deactivate(struct led_classdev *led_cdev)
392 {
393 struct led_netdev_data *trigger_data = led_cdev->trigger_data;
394
395 if (trigger_data) {
396 unregister_netdevice_notifier(&trigger_data->notifier);
397
398 device_remove_file(led_cdev->dev, &dev_attr_device_name);
399 device_remove_file(led_cdev->dev, &dev_attr_mode);
400 device_remove_file(led_cdev->dev, &dev_attr_interval);
401
402 spin_lock_bh(&trigger_data->lock);
403
404 if (trigger_data->net_dev) {
405 dev_put(trigger_data->net_dev);
406 trigger_data->net_dev = NULL;
407 }
408
409 spin_unlock_bh(&trigger_data->lock);
410
411 del_timer_sync(&trigger_data->timer);
412
413 kfree(trigger_data);
414 }
415 }
416
417 static struct led_trigger netdev_led_trigger = {
418 .name = "netdev",
419 .activate = netdev_trig_activate,
420 .deactivate = netdev_trig_deactivate,
421 };
422
423 static int __init netdev_trig_init(void)
424 {
425 return led_trigger_register(&netdev_led_trigger);
426 }
427
428 static void __exit netdev_trig_exit(void)
429 {
430 led_trigger_unregister(&netdev_led_trigger);
431 }
432
433 module_init(netdev_trig_init);
434 module_exit(netdev_trig_exit);
435
436 MODULE_AUTHOR("Oliver Jowett <oliver@opencloud.com>");
437 MODULE_DESCRIPTION("Netdev LED trigger");
438 MODULE_LICENSE("GPL");