ledtrig-netdev: drop locking from timer callback function
[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
126 if (trigger_data->device_name[0] != 0) {
127 /* check for existing device to update from */
128 trigger_data->net_dev = dev_get_by_name(&init_net, trigger_data->device_name);
129 if (trigger_data->net_dev != NULL)
130 trigger_data->link_up = (dev_get_flags(trigger_data->net_dev) & IFF_LOWER_UP) != 0;
131 }
132
133 set_baseline_state(trigger_data);
134 spin_unlock_bh(&trigger_data->lock);
135
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 del_timer_sync(&trigger_data->timer);
204
205 trigger_data->mode = new_mode;
206
207 set_baseline_state(trigger_data);
208 spin_unlock_bh(&trigger_data->lock);
209
210 return size;
211 }
212
213 static DEVICE_ATTR(mode, 0644, led_mode_show, led_mode_store);
214
215 static ssize_t led_interval_show(struct device *dev,
216 struct device_attribute *attr, char *buf)
217 {
218 struct led_classdev *led_cdev = dev_get_drvdata(dev);
219 struct led_netdev_data *trigger_data = led_cdev->trigger_data;
220
221 spin_lock_bh(&trigger_data->lock);
222 sprintf(buf, "%u\n", jiffies_to_msecs(trigger_data->interval));
223 spin_unlock_bh(&trigger_data->lock);
224
225 return strlen(buf) + 1;
226 }
227
228 static ssize_t led_interval_store(struct device *dev,
229 struct device_attribute *attr, const char *buf, size_t size)
230 {
231 struct led_classdev *led_cdev = dev_get_drvdata(dev);
232 struct led_netdev_data *trigger_data = led_cdev->trigger_data;
233 int ret = -EINVAL;
234 char *after;
235 unsigned long value = simple_strtoul(buf, &after, 10);
236 size_t count = after - buf;
237
238 if (isspace(*after))
239 count++;
240
241 /* impose some basic bounds on the timer interval */
242 if (count == size && value >= 5 && value <= 10000) {
243 spin_lock_bh(&trigger_data->lock);
244 del_timer_sync(&trigger_data->timer);
245
246 trigger_data->interval = msecs_to_jiffies(value);
247
248 set_baseline_state(trigger_data); /* resets timer */
249 spin_unlock_bh(&trigger_data->lock);
250
251 ret = count;
252 }
253
254 return ret;
255 }
256
257 static DEVICE_ATTR(interval, 0644, led_interval_show, led_interval_store);
258
259 static int netdev_trig_notify(struct notifier_block *nb,
260 unsigned long evt,
261 void *dv)
262 {
263 struct net_device *dev = netdev_notifier_info_to_dev((struct netdev_notifier_info *) dv);
264 struct led_netdev_data *trigger_data = container_of(nb, struct led_netdev_data, notifier);
265
266 if (evt != NETDEV_UP && evt != NETDEV_DOWN && evt != NETDEV_CHANGE && evt != NETDEV_REGISTER && evt != NETDEV_UNREGISTER)
267 return NOTIFY_DONE;
268
269 spin_lock_bh(&trigger_data->lock);
270 del_timer_sync(&trigger_data->timer);
271
272 if (strcmp(dev->name, trigger_data->device_name))
273 goto done;
274
275 if (evt == NETDEV_REGISTER) {
276 if (trigger_data->net_dev != NULL)
277 dev_put(trigger_data->net_dev);
278 dev_hold(dev);
279 trigger_data->net_dev = dev;
280 trigger_data->link_up = 0;
281 goto done;
282 }
283
284 if (evt == NETDEV_UNREGISTER && trigger_data->net_dev != NULL) {
285 dev_put(trigger_data->net_dev);
286 trigger_data->net_dev = NULL;
287 goto done;
288 }
289
290 /* UP / DOWN / CHANGE */
291
292 trigger_data->link_up = (evt != NETDEV_DOWN && netif_carrier_ok(dev));
293 set_baseline_state(trigger_data);
294
295 done:
296 spin_unlock_bh(&trigger_data->lock);
297 return NOTIFY_DONE;
298 }
299
300 /* here's the real work! */
301 static void netdev_trig_timer(unsigned long arg)
302 {
303 struct led_netdev_data *trigger_data = (struct led_netdev_data *)arg;
304 struct rtnl_link_stats64 *dev_stats;
305 unsigned new_activity;
306 struct rtnl_link_stats64 temp;
307
308 if (!trigger_data->link_up || !trigger_data->net_dev || (trigger_data->mode & (MODE_TX | MODE_RX)) == 0) {
309 /* we don't need to do timer work, just reflect link state. */
310 led_set_brightness(trigger_data->led_cdev, ((trigger_data->mode & MODE_LINK) != 0 && trigger_data->link_up) ? LED_FULL : LED_OFF);
311 return;
312 }
313
314 dev_stats = dev_get_stats(trigger_data->net_dev, &temp);
315 new_activity =
316 ((trigger_data->mode & MODE_TX) ? dev_stats->tx_packets : 0) +
317 ((trigger_data->mode & MODE_RX) ? dev_stats->rx_packets : 0);
318
319 if (trigger_data->mode & MODE_LINK) {
320 /* base state is ON (link present) */
321 /* if there's no link, we don't get this far and the LED is off */
322
323 /* OFF -> ON always */
324 /* ON -> OFF on activity */
325 if (trigger_data->led_cdev->brightness == LED_OFF) {
326 led_set_brightness(trigger_data->led_cdev, LED_FULL);
327 } else if (trigger_data->last_activity != new_activity) {
328 led_set_brightness(trigger_data->led_cdev, LED_OFF);
329 }
330 } else {
331 /* base state is OFF */
332 /* ON -> OFF always */
333 /* OFF -> ON on activity */
334 if (trigger_data->led_cdev->brightness == LED_FULL) {
335 led_set_brightness(trigger_data->led_cdev, LED_OFF);
336 } else if (trigger_data->last_activity != new_activity) {
337 led_set_brightness(trigger_data->led_cdev, LED_FULL);
338 }
339 }
340
341 trigger_data->last_activity = new_activity;
342 mod_timer(&trigger_data->timer, jiffies + trigger_data->interval);
343 }
344
345 static void netdev_trig_activate(struct led_classdev *led_cdev)
346 {
347 struct led_netdev_data *trigger_data;
348 int rc;
349
350 trigger_data = kzalloc(sizeof(struct led_netdev_data), GFP_KERNEL);
351 if (!trigger_data)
352 return;
353
354 spin_lock_init(&trigger_data->lock);
355
356 trigger_data->notifier.notifier_call = netdev_trig_notify;
357 trigger_data->notifier.priority = 10;
358
359 setup_timer(&trigger_data->timer, netdev_trig_timer, (unsigned long) trigger_data);
360
361 trigger_data->led_cdev = led_cdev;
362 trigger_data->net_dev = NULL;
363 trigger_data->device_name[0] = 0;
364
365 trigger_data->mode = 0;
366 trigger_data->interval = msecs_to_jiffies(50);
367 trigger_data->link_up = 0;
368 trigger_data->last_activity = 0;
369
370 led_cdev->trigger_data = trigger_data;
371
372 rc = device_create_file(led_cdev->dev, &dev_attr_device_name);
373 if (rc)
374 goto err_out;
375 rc = device_create_file(led_cdev->dev, &dev_attr_mode);
376 if (rc)
377 goto err_out_device_name;
378 rc = device_create_file(led_cdev->dev, &dev_attr_interval);
379 if (rc)
380 goto err_out_mode;
381
382 register_netdevice_notifier(&trigger_data->notifier);
383 return;
384
385 err_out_mode:
386 device_remove_file(led_cdev->dev, &dev_attr_mode);
387 err_out_device_name:
388 device_remove_file(led_cdev->dev, &dev_attr_device_name);
389 err_out:
390 led_cdev->trigger_data = NULL;
391 kfree(trigger_data);
392 }
393
394 static void netdev_trig_deactivate(struct led_classdev *led_cdev)
395 {
396 struct led_netdev_data *trigger_data = led_cdev->trigger_data;
397
398 if (trigger_data) {
399 unregister_netdevice_notifier(&trigger_data->notifier);
400
401 device_remove_file(led_cdev->dev, &dev_attr_device_name);
402 device_remove_file(led_cdev->dev, &dev_attr_mode);
403 device_remove_file(led_cdev->dev, &dev_attr_interval);
404
405 spin_lock_bh(&trigger_data->lock);
406 del_timer_sync(&trigger_data->timer);
407
408 if (trigger_data->net_dev) {
409 dev_put(trigger_data->net_dev);
410 trigger_data->net_dev = NULL;
411 }
412
413 spin_unlock_bh(&trigger_data->lock);
414
415 kfree(trigger_data);
416 }
417 }
418
419 static struct led_trigger netdev_led_trigger = {
420 .name = "netdev",
421 .activate = netdev_trig_activate,
422 .deactivate = netdev_trig_deactivate,
423 };
424
425 static int __init netdev_trig_init(void)
426 {
427 return led_trigger_register(&netdev_led_trigger);
428 }
429
430 static void __exit netdev_trig_exit(void)
431 {
432 led_trigger_unregister(&netdev_led_trigger);
433 }
434
435 module_init(netdev_trig_init);
436 module_exit(netdev_trig_exit);
437
438 MODULE_AUTHOR("Oliver Jowett <oliver@opencloud.com>");
439 MODULE_DESCRIPTION("Netdev LED trigger");
440 MODULE_LICENSE("GPL");