rename target/linux/generic-2.6 to generic
[openwrt.git] / target / linux / generic / files-2.6.25 / 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 "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 rwlock_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 read_lock(&trigger_data->lock);
106 sprintf(buf, "%s\n", trigger_data->device_name);
107 read_unlock(&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 write_lock(&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(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 write_unlock(&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 read_lock(&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 read_unlock(&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[1024];
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[1023] = 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 write_lock(&trigger_data->lock);
203 trigger_data->mode = new_mode;
204 set_baseline_state(trigger_data);
205 write_unlock(&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 read_lock(&trigger_data->lock);
219 sprintf(buf, "%u\n", jiffies_to_msecs(trigger_data->interval));
220 read_unlock(&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 (*after && isspace(*after))
236 count++;
237
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);
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 = 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 write_lock(&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 write_unlock(&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 net_device_stats *dev_stats;
297 unsigned new_activity;
298
299 write_lock(&trigger_data->lock);
300
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);
304 goto no_restart;
305 }
306
307 dev_stats = trigger_data->net_dev->get_stats(trigger_data->net_dev);
308 new_activity =
309 ((trigger_data->mode & MODE_TX) ? dev_stats->tx_packets : 0) +
310 ((trigger_data->mode & MODE_RX) ? dev_stats->rx_packets : 0);
311
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 */
315
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);
322 }
323 } else {
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);
331 }
332 }
333
334 trigger_data->last_activity = new_activity;
335 mod_timer(&trigger_data->timer, jiffies + trigger_data->interval);
336
337 no_restart:
338 write_unlock(&trigger_data->lock);
339 }
340
341 static void netdev_trig_activate(struct led_classdev *led_cdev)
342 {
343 struct led_netdev_data *trigger_data;
344 int rc;
345
346 trigger_data = kzalloc(sizeof(struct led_netdev_data), GFP_KERNEL);
347 if (!trigger_data)
348 return;
349
350 rwlock_init(&trigger_data->lock);
351
352 trigger_data->notifier.notifier_call = netdev_trig_notify;
353 trigger_data->notifier.priority = 10;
354
355 setup_timer(&trigger_data->timer, netdev_trig_timer, (unsigned long) trigger_data);
356
357 trigger_data->led_cdev = led_cdev;
358 trigger_data->net_dev = NULL;
359 trigger_data->device_name[0] = 0;
360
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;
365
366 led_cdev->trigger_data = trigger_data;
367
368 rc = device_create_file(led_cdev->dev, &dev_attr_device_name);
369 if (rc)
370 goto err_out;
371 rc = device_create_file(led_cdev->dev, &dev_attr_mode);
372 if (rc)
373 goto err_out_device_name;
374 rc = device_create_file(led_cdev->dev, &dev_attr_interval);
375 if (rc)
376 goto err_out_mode;
377
378 register_netdevice_notifier(&trigger_data->notifier);
379 return;
380
381 err_out_mode:
382 device_remove_file(led_cdev->dev, &dev_attr_mode);
383 err_out_device_name:
384 device_remove_file(led_cdev->dev, &dev_attr_device_name);
385 err_out:
386 led_cdev->trigger_data = NULL;
387 kfree(trigger_data);
388 }
389
390 static void netdev_trig_deactivate(struct led_classdev *led_cdev)
391 {
392 struct led_netdev_data *trigger_data = led_cdev->trigger_data;
393
394 if (trigger_data) {
395 unregister_netdevice_notifier(&trigger_data->notifier);
396
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);
400
401 write_lock(&trigger_data->lock);
402
403 if (trigger_data->net_dev) {
404 dev_put(trigger_data->net_dev);
405 trigger_data->net_dev = NULL;
406 }
407
408 write_unlock(&trigger_data->lock);
409
410 del_timer_sync(&trigger_data->timer);
411
412 kfree(trigger_data);
413 }
414 }
415
416 static struct led_trigger netdev_led_trigger = {
417 .name = "netdev",
418 .activate = netdev_trig_activate,
419 .deactivate = netdev_trig_deactivate,
420 };
421
422 static int __init netdev_trig_init(void)
423 {
424 return led_trigger_register(&netdev_led_trigger);
425 }
426
427 static void __exit netdev_trig_exit(void)
428 {
429 led_trigger_unregister(&netdev_led_trigger);
430 }
431
432 module_init(netdev_trig_init);
433 module_exit(netdev_trig_exit);
434
435 MODULE_AUTHOR("Oliver Jowett <oliver@opencloud.com>");
436 MODULE_DESCRIPTION("Netdev LED trigger");
437 MODULE_LICENSE("GPL");
This page took 0.07247 seconds and 5 git commands to generate.