ar71xx: change firmware image name for th TP-Link boards
[openwrt.git] / target / linux / generic-2.6 / files-2.6.31 / 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 #ifdef CONFIG_COMPAT_NET_DEV_OPS
321 dev_stats = trigger_data->net_dev->get_stats(trigger_data->net_dev);
322 #else
323 dev_stats = trigger_data->net_dev->netdev_ops->ndo_get_stats(trigger_data->net_dev);
324 #endif
325 new_activity =
326 ((trigger_data->mode & MODE_TX) ? dev_stats->tx_packets : 0) +
327 ((trigger_data->mode & MODE_RX) ? dev_stats->rx_packets : 0);
328
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 */
332
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);
339 }
340 } else {
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);
348 }
349 }
350
351 trigger_data->last_activity = new_activity;
352 mod_timer(&trigger_data->timer, jiffies + trigger_data->interval);
353
354 no_restart:
355 write_unlock(&trigger_data->lock);
356 }
357
358 static void netdev_trig_activate(struct led_classdev *led_cdev)
359 {
360 struct led_netdev_data *trigger_data;
361 int rc;
362
363 trigger_data = kzalloc(sizeof(struct led_netdev_data), GFP_KERNEL);
364 if (!trigger_data)
365 return;
366
367 rwlock_init(&trigger_data->lock);
368
369 trigger_data->notifier.notifier_call = netdev_trig_notify;
370 trigger_data->notifier.priority = 10;
371
372 setup_timer(&trigger_data->timer, netdev_trig_timer, (unsigned long) trigger_data);
373
374 trigger_data->led_cdev = led_cdev;
375 trigger_data->net_dev = NULL;
376 trigger_data->device_name[0] = 0;
377
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;
382
383 led_cdev->trigger_data = trigger_data;
384
385 rc = device_create_file(led_cdev->dev, &dev_attr_device_name);
386 if (rc)
387 goto err_out;
388 rc = device_create_file(led_cdev->dev, &dev_attr_mode);
389 if (rc)
390 goto err_out_device_name;
391 rc = device_create_file(led_cdev->dev, &dev_attr_interval);
392 if (rc)
393 goto err_out_mode;
394
395 register_netdevice_notifier(&trigger_data->notifier);
396 return;
397
398 err_out_mode:
399 device_remove_file(led_cdev->dev, &dev_attr_mode);
400 err_out_device_name:
401 device_remove_file(led_cdev->dev, &dev_attr_device_name);
402 err_out:
403 led_cdev->trigger_data = NULL;
404 kfree(trigger_data);
405 }
406
407 static void netdev_trig_deactivate(struct led_classdev *led_cdev)
408 {
409 struct led_netdev_data *trigger_data = led_cdev->trigger_data;
410
411 if (trigger_data) {
412 unregister_netdevice_notifier(&trigger_data->notifier);
413
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);
417
418 write_lock(&trigger_data->lock);
419
420 if (trigger_data->net_dev) {
421 dev_put(trigger_data->net_dev);
422 trigger_data->net_dev = NULL;
423 }
424
425 write_unlock(&trigger_data->lock);
426
427 del_timer_sync(&trigger_data->timer);
428
429 kfree(trigger_data);
430 }
431 }
432
433 static struct led_trigger netdev_led_trigger = {
434 .name = "netdev",
435 .activate = netdev_trig_activate,
436 .deactivate = netdev_trig_deactivate,
437 };
438
439 static int __init netdev_trig_init(void)
440 {
441 return led_trigger_register(&netdev_led_trigger);
442 }
443
444 static void __exit netdev_trig_exit(void)
445 {
446 led_trigger_unregister(&netdev_led_trigger);
447 }
448
449 module_init(netdev_trig_init);
450 module_exit(netdev_trig_exit);
451
452 MODULE_AUTHOR("Oliver Jowett <oliver@opencloud.com>");
453 MODULE_DESCRIPTION("Netdev LED trigger");
454 MODULE_LICENSE("GPL");
This page took 0.081643 seconds and 5 git commands to generate.