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