kernel: Replace ledtrig-netdev with upstream backport
[openwrt/openwrt.git] / target / linux / generic / backport-4.9 / 400-v4.16-leds-trigger-Introduce-a-NETDEV-trigger.patch
1 From 06f502f57d0d7728f9fa0f157ec5e4111ddb98f6 Mon Sep 17 00:00:00 2001
2 From: Ben Whitten <ben.whitten@gmail.com>
3 Date: Sun, 10 Dec 2017 21:17:55 +0000
4 Subject: [PATCH] leds: trigger: Introduce a NETDEV trigger
5
6 This commit introduces a NETDEV trigger for named device
7 activity. Available triggers are link, rx, and tx.
8
9 Signed-off-by: Ben Whitten <ben.whitten@gmail.com>
10 Acked-by: Pavel Machek <pavel@ucw.cz>
11 Signed-off-by: Jacek Anaszewski <jacek.anaszewski@gmail.com>
12 ---
13 .../ABI/testing/sysfs-class-led-trigger-netdev | 45 ++
14 drivers/leds/trigger/Kconfig | 7 +
15 drivers/leds/trigger/Makefile | 1 +
16 drivers/leds/trigger/ledtrig-netdev.c | 496 +++++++++++++++++++++
17 4 files changed, 549 insertions(+)
18 create mode 100644 Documentation/ABI/testing/sysfs-class-led-trigger-netdev
19 create mode 100644 drivers/leds/trigger/ledtrig-netdev.c
20
21 --- /dev/null
22 +++ b/Documentation/ABI/testing/sysfs-class-led-trigger-netdev
23 @@ -0,0 +1,45 @@
24 +What: /sys/class/leds/<led>/device_name
25 +Date: Dec 2017
26 +KernelVersion: 4.16
27 +Contact: linux-leds@vger.kernel.org
28 +Description:
29 + Specifies the network device name to monitor.
30 +
31 +What: /sys/class/leds/<led>/interval
32 +Date: Dec 2017
33 +KernelVersion: 4.16
34 +Contact: linux-leds@vger.kernel.org
35 +Description:
36 + Specifies the duration of the LED blink in milliseconds.
37 + Defaults to 50 ms.
38 +
39 +What: /sys/class/leds/<led>/link
40 +Date: Dec 2017
41 +KernelVersion: 4.16
42 +Contact: linux-leds@vger.kernel.org
43 +Description:
44 + Signal the link state of the named network device.
45 + If set to 0 (default), the LED's normal state is off.
46 + If set to 1, the LED's normal state reflects the link state
47 + of the named network device.
48 + Setting this value also immediately changes the LED state.
49 +
50 +What: /sys/class/leds/<led>/tx
51 +Date: Dec 2017
52 +KernelVersion: 4.16
53 +Contact: linux-leds@vger.kernel.org
54 +Description:
55 + Signal transmission of data on the named network device.
56 + If set to 0 (default), the LED will not blink on transmission.
57 + If set to 1, the LED will blink for the milliseconds specified
58 + in interval to signal transmission.
59 +
60 +What: /sys/class/leds/<led>/rx
61 +Date: Dec 2017
62 +KernelVersion: 4.16
63 +Contact: linux-leds@vger.kernel.org
64 +Description:
65 + Signal reception of data on the named network device.
66 + If set to 0 (default), the LED will not blink on reception.
67 + If set to 1, the LED will blink for the milliseconds specified
68 + in interval to signal reception.
69 --- a/drivers/leds/trigger/Kconfig
70 +++ b/drivers/leds/trigger/Kconfig
71 @@ -126,4 +126,11 @@ config LEDS_TRIGGER_PANIC
72 a different trigger.
73 If unsure, say Y.
74
75 +config LEDS_TRIGGER_NETDEV
76 + tristate "LED Netdev Trigger"
77 + depends on NET && LEDS_TRIGGERS
78 + help
79 + This allows LEDs to be controlled by network device activity.
80 + If unsure, say Y.
81 +
82 endif # LEDS_TRIGGERS
83 --- a/drivers/leds/trigger/Makefile
84 +++ b/drivers/leds/trigger/Makefile
85 @@ -10,3 +10,4 @@ obj-$(CONFIG_LEDS_TRIGGER_DEFAULT_ON) +=
86 obj-$(CONFIG_LEDS_TRIGGER_TRANSIENT) += ledtrig-transient.o
87 obj-$(CONFIG_LEDS_TRIGGER_CAMERA) += ledtrig-camera.o
88 obj-$(CONFIG_LEDS_TRIGGER_PANIC) += ledtrig-panic.o
89 +obj-$(CONFIG_LEDS_TRIGGER_NETDEV) += ledtrig-netdev.o
90 --- /dev/null
91 +++ b/drivers/leds/trigger/ledtrig-netdev.c
92 @@ -0,0 +1,496 @@
93 +// SPDX-License-Identifier: GPL-2.0
94 +// Copyright 2017 Ben Whitten <ben.whitten@gmail.com>
95 +// Copyright 2007 Oliver Jowett <oliver@opencloud.com>
96 +//
97 +// LED Kernel Netdev Trigger
98 +//
99 +// Toggles the LED to reflect the link and traffic state of a named net device
100 +//
101 +// Derived from ledtrig-timer.c which is:
102 +// Copyright 2005-2006 Openedhand Ltd.
103 +// Author: Richard Purdie <rpurdie@openedhand.com>
104 +
105 +#include <linux/atomic.h>
106 +#include <linux/ctype.h>
107 +#include <linux/device.h>
108 +#include <linux/init.h>
109 +#include <linux/jiffies.h>
110 +#include <linux/kernel.h>
111 +#include <linux/leds.h>
112 +#include <linux/list.h>
113 +#include <linux/module.h>
114 +#include <linux/netdevice.h>
115 +#include <linux/spinlock.h>
116 +#include <linux/timer.h>
117 +#include "../leds.h"
118 +
119 +/*
120 + * Configurable sysfs attributes:
121 + *
122 + * device_name - network device name to monitor
123 + * interval - duration of LED blink, in milliseconds
124 + * link - LED's normal state reflects whether the link is up
125 + * (has carrier) or not
126 + * tx - LED blinks on transmitted data
127 + * rx - LED blinks on receive data
128 + *
129 + */
130 +
131 +struct led_netdev_data {
132 + spinlock_t lock;
133 +
134 + struct delayed_work work;
135 + struct notifier_block notifier;
136 +
137 + struct led_classdev *led_cdev;
138 + struct net_device *net_dev;
139 +
140 + char device_name[IFNAMSIZ];
141 + atomic_t interval;
142 + unsigned int last_activity;
143 +
144 + unsigned long mode;
145 +#define NETDEV_LED_LINK 0
146 +#define NETDEV_LED_TX 1
147 +#define NETDEV_LED_RX 2
148 +#define NETDEV_LED_MODE_LINKUP 3
149 +};
150 +
151 +enum netdev_led_attr {
152 + NETDEV_ATTR_LINK,
153 + NETDEV_ATTR_TX,
154 + NETDEV_ATTR_RX
155 +};
156 +
157 +static void set_baseline_state(struct led_netdev_data *trigger_data)
158 +{
159 + int current_brightness;
160 + struct led_classdev *led_cdev = trigger_data->led_cdev;
161 +
162 + current_brightness = led_cdev->brightness;
163 + if (current_brightness)
164 + led_cdev->blink_brightness = current_brightness;
165 + if (!led_cdev->blink_brightness)
166 + led_cdev->blink_brightness = led_cdev->max_brightness;
167 +
168 + if (!test_bit(NETDEV_LED_MODE_LINKUP, &trigger_data->mode))
169 + led_set_brightness(led_cdev, LED_OFF);
170 + else {
171 + if (test_bit(NETDEV_LED_LINK, &trigger_data->mode))
172 + led_set_brightness(led_cdev,
173 + led_cdev->blink_brightness);
174 + else
175 + led_set_brightness(led_cdev, LED_OFF);
176 +
177 + /* If we are looking for RX/TX start periodically
178 + * checking stats
179 + */
180 + if (test_bit(NETDEV_LED_TX, &trigger_data->mode) ||
181 + test_bit(NETDEV_LED_RX, &trigger_data->mode))
182 + schedule_delayed_work(&trigger_data->work, 0);
183 + }
184 +}
185 +
186 +static ssize_t device_name_show(struct device *dev,
187 + struct device_attribute *attr, char *buf)
188 +{
189 + struct led_classdev *led_cdev = dev_get_drvdata(dev);
190 + struct led_netdev_data *trigger_data = led_cdev->trigger_data;
191 + ssize_t len;
192 +
193 + spin_lock_bh(&trigger_data->lock);
194 + len = sprintf(buf, "%s\n", trigger_data->device_name);
195 + spin_unlock_bh(&trigger_data->lock);
196 +
197 + return len;
198 +}
199 +
200 +static ssize_t device_name_store(struct device *dev,
201 + struct device_attribute *attr, const char *buf,
202 + size_t size)
203 +{
204 + struct led_classdev *led_cdev = dev_get_drvdata(dev);
205 + struct led_netdev_data *trigger_data = led_cdev->trigger_data;
206 +
207 + if (size >= IFNAMSIZ)
208 + return -EINVAL;
209 +
210 + cancel_delayed_work_sync(&trigger_data->work);
211 +
212 + spin_lock_bh(&trigger_data->lock);
213 +
214 + if (trigger_data->net_dev) {
215 + dev_put(trigger_data->net_dev);
216 + trigger_data->net_dev = NULL;
217 + }
218 +
219 + strncpy(trigger_data->device_name, buf, size);
220 + if (size > 0 && trigger_data->device_name[size - 1] == '\n')
221 + trigger_data->device_name[size - 1] = 0;
222 +
223 + if (trigger_data->device_name[0] != 0)
224 + trigger_data->net_dev =
225 + dev_get_by_name(&init_net, trigger_data->device_name);
226 +
227 + clear_bit(NETDEV_LED_MODE_LINKUP, &trigger_data->mode);
228 + if (trigger_data->net_dev != NULL)
229 + if (netif_carrier_ok(trigger_data->net_dev))
230 + set_bit(NETDEV_LED_MODE_LINKUP, &trigger_data->mode);
231 +
232 + trigger_data->last_activity = 0;
233 +
234 + set_baseline_state(trigger_data);
235 + spin_unlock_bh(&trigger_data->lock);
236 +
237 + return size;
238 +}
239 +
240 +static DEVICE_ATTR_RW(device_name);
241 +
242 +static ssize_t netdev_led_attr_show(struct device *dev, char *buf,
243 + enum netdev_led_attr attr)
244 +{
245 + struct led_classdev *led_cdev = dev_get_drvdata(dev);
246 + struct led_netdev_data *trigger_data = led_cdev->trigger_data;
247 + int bit;
248 +
249 + switch (attr) {
250 + case NETDEV_ATTR_LINK:
251 + bit = NETDEV_LED_LINK;
252 + break;
253 + case NETDEV_ATTR_TX:
254 + bit = NETDEV_LED_TX;
255 + break;
256 + case NETDEV_ATTR_RX:
257 + bit = NETDEV_LED_RX;
258 + break;
259 + default:
260 + return -EINVAL;
261 + }
262 +
263 + return sprintf(buf, "%u\n", test_bit(bit, &trigger_data->mode));
264 +}
265 +
266 +static ssize_t netdev_led_attr_store(struct device *dev, const char *buf,
267 + size_t size, enum netdev_led_attr attr)
268 +{
269 + struct led_classdev *led_cdev = dev_get_drvdata(dev);
270 + struct led_netdev_data *trigger_data = led_cdev->trigger_data;
271 + unsigned long state;
272 + int ret;
273 + int bit;
274 +
275 + ret = kstrtoul(buf, 0, &state);
276 + if (ret)
277 + return ret;
278 +
279 + switch (attr) {
280 + case NETDEV_ATTR_LINK:
281 + bit = NETDEV_LED_LINK;
282 + break;
283 + case NETDEV_ATTR_TX:
284 + bit = NETDEV_LED_TX;
285 + break;
286 + case NETDEV_ATTR_RX:
287 + bit = NETDEV_LED_RX;
288 + break;
289 + default:
290 + return -EINVAL;
291 + }
292 +
293 + cancel_delayed_work_sync(&trigger_data->work);
294 +
295 + if (state)
296 + set_bit(bit, &trigger_data->mode);
297 + else
298 + clear_bit(bit, &trigger_data->mode);
299 +
300 + set_baseline_state(trigger_data);
301 +
302 + return size;
303 +}
304 +
305 +static ssize_t link_show(struct device *dev,
306 + struct device_attribute *attr, char *buf)
307 +{
308 + return netdev_led_attr_show(dev, buf, NETDEV_ATTR_LINK);
309 +}
310 +
311 +static ssize_t link_store(struct device *dev,
312 + struct device_attribute *attr, const char *buf, size_t size)
313 +{
314 + return netdev_led_attr_store(dev, buf, size, NETDEV_ATTR_LINK);
315 +}
316 +
317 +static DEVICE_ATTR_RW(link);
318 +
319 +static ssize_t tx_show(struct device *dev,
320 + struct device_attribute *attr, char *buf)
321 +{
322 + return netdev_led_attr_show(dev, buf, NETDEV_ATTR_TX);
323 +}
324 +
325 +static ssize_t tx_store(struct device *dev,
326 + struct device_attribute *attr, const char *buf, size_t size)
327 +{
328 + return netdev_led_attr_store(dev, buf, size, NETDEV_ATTR_TX);
329 +}
330 +
331 +static DEVICE_ATTR_RW(tx);
332 +
333 +static ssize_t rx_show(struct device *dev,
334 + struct device_attribute *attr, char *buf)
335 +{
336 + return netdev_led_attr_show(dev, buf, NETDEV_ATTR_RX);
337 +}
338 +
339 +static ssize_t rx_store(struct device *dev,
340 + struct device_attribute *attr, const char *buf, size_t size)
341 +{
342 + return netdev_led_attr_store(dev, buf, size, NETDEV_ATTR_RX);
343 +}
344 +
345 +static DEVICE_ATTR_RW(rx);
346 +
347 +static ssize_t interval_show(struct device *dev,
348 + struct device_attribute *attr, char *buf)
349 +{
350 + struct led_classdev *led_cdev = dev_get_drvdata(dev);
351 + struct led_netdev_data *trigger_data = led_cdev->trigger_data;
352 +
353 + return sprintf(buf, "%u\n",
354 + jiffies_to_msecs(atomic_read(&trigger_data->interval)));
355 +}
356 +
357 +static ssize_t interval_store(struct device *dev,
358 + struct device_attribute *attr, const char *buf,
359 + size_t size)
360 +{
361 + struct led_classdev *led_cdev = dev_get_drvdata(dev);
362 + struct led_netdev_data *trigger_data = led_cdev->trigger_data;
363 + unsigned long value;
364 + int ret;
365 +
366 + ret = kstrtoul(buf, 0, &value);
367 + if (ret)
368 + return ret;
369 +
370 + /* impose some basic bounds on the timer interval */
371 + if (value >= 5 && value <= 10000) {
372 + cancel_delayed_work_sync(&trigger_data->work);
373 +
374 + atomic_set(&trigger_data->interval, msecs_to_jiffies(value));
375 + set_baseline_state(trigger_data); /* resets timer */
376 + }
377 +
378 + return size;
379 +}
380 +
381 +static DEVICE_ATTR_RW(interval);
382 +
383 +static int netdev_trig_notify(struct notifier_block *nb,
384 + unsigned long evt, void *dv)
385 +{
386 + struct net_device *dev =
387 + netdev_notifier_info_to_dev((struct netdev_notifier_info *)dv);
388 + struct led_netdev_data *trigger_data = container_of(nb,
389 + struct
390 + led_netdev_data,
391 + notifier);
392 +
393 + if (evt != NETDEV_UP && evt != NETDEV_DOWN && evt != NETDEV_CHANGE
394 + && evt != NETDEV_REGISTER && evt != NETDEV_UNREGISTER
395 + && evt != NETDEV_CHANGENAME)
396 + return NOTIFY_DONE;
397 +
398 + if (strcmp(dev->name, trigger_data->device_name))
399 + return NOTIFY_DONE;
400 +
401 + cancel_delayed_work_sync(&trigger_data->work);
402 +
403 + spin_lock_bh(&trigger_data->lock);
404 +
405 + clear_bit(NETDEV_LED_MODE_LINKUP, &trigger_data->mode);
406 + switch (evt) {
407 + case NETDEV_REGISTER:
408 + if (trigger_data->net_dev)
409 + dev_put(trigger_data->net_dev);
410 + dev_hold(dev);
411 + trigger_data->net_dev = dev;
412 + break;
413 + case NETDEV_CHANGENAME:
414 + case NETDEV_UNREGISTER:
415 + if (trigger_data->net_dev) {
416 + dev_put(trigger_data->net_dev);
417 + trigger_data->net_dev = NULL;
418 + }
419 + break;
420 + case NETDEV_UP:
421 + case NETDEV_CHANGE:
422 + if (netif_carrier_ok(dev))
423 + set_bit(NETDEV_LED_MODE_LINKUP, &trigger_data->mode);
424 + break;
425 + }
426 +
427 + set_baseline_state(trigger_data);
428 +
429 + spin_unlock_bh(&trigger_data->lock);
430 +
431 + return NOTIFY_DONE;
432 +}
433 +
434 +/* here's the real work! */
435 +static void netdev_trig_work(struct work_struct *work)
436 +{
437 + struct led_netdev_data *trigger_data = container_of(work,
438 + struct
439 + led_netdev_data,
440 + work.work);
441 + struct rtnl_link_stats64 *dev_stats;
442 + unsigned int new_activity;
443 + struct rtnl_link_stats64 temp;
444 + unsigned long interval;
445 + int invert;
446 +
447 + /* If we dont have a device, insure we are off */
448 + if (!trigger_data->net_dev) {
449 + led_set_brightness(trigger_data->led_cdev, LED_OFF);
450 + return;
451 + }
452 +
453 + /* If we are not looking for RX/TX then return */
454 + if (!test_bit(NETDEV_LED_TX, &trigger_data->mode) &&
455 + !test_bit(NETDEV_LED_RX, &trigger_data->mode))
456 + return;
457 +
458 + dev_stats = dev_get_stats(trigger_data->net_dev, &temp);
459 + new_activity =
460 + (test_bit(NETDEV_LED_TX, &trigger_data->mode) ?
461 + dev_stats->tx_packets : 0) +
462 + (test_bit(NETDEV_LED_RX, &trigger_data->mode) ?
463 + dev_stats->rx_packets : 0);
464 +
465 + if (trigger_data->last_activity != new_activity) {
466 + led_stop_software_blink(trigger_data->led_cdev);
467 +
468 + invert = test_bit(NETDEV_LED_LINK, &trigger_data->mode);
469 + interval = jiffies_to_msecs(
470 + atomic_read(&trigger_data->interval));
471 + /* base state is ON (link present) */
472 + led_blink_set_oneshot(trigger_data->led_cdev,
473 + &interval,
474 + &interval,
475 + invert);
476 + trigger_data->last_activity = new_activity;
477 + }
478 +
479 + schedule_delayed_work(&trigger_data->work,
480 + (atomic_read(&trigger_data->interval)*2));
481 +}
482 +
483 +static void netdev_trig_activate(struct led_classdev *led_cdev)
484 +{
485 + struct led_netdev_data *trigger_data;
486 + int rc;
487 +
488 + trigger_data = kzalloc(sizeof(struct led_netdev_data), GFP_KERNEL);
489 + if (!trigger_data)
490 + return;
491 +
492 + spin_lock_init(&trigger_data->lock);
493 +
494 + trigger_data->notifier.notifier_call = netdev_trig_notify;
495 + trigger_data->notifier.priority = 10;
496 +
497 + INIT_DELAYED_WORK(&trigger_data->work, netdev_trig_work);
498 +
499 + trigger_data->led_cdev = led_cdev;
500 + trigger_data->net_dev = NULL;
501 + trigger_data->device_name[0] = 0;
502 +
503 + trigger_data->mode = 0;
504 + atomic_set(&trigger_data->interval, msecs_to_jiffies(50));
505 + trigger_data->last_activity = 0;
506 +
507 + led_cdev->trigger_data = trigger_data;
508 +
509 + rc = device_create_file(led_cdev->dev, &dev_attr_device_name);
510 + if (rc)
511 + goto err_out;
512 + rc = device_create_file(led_cdev->dev, &dev_attr_link);
513 + if (rc)
514 + goto err_out_device_name;
515 + rc = device_create_file(led_cdev->dev, &dev_attr_rx);
516 + if (rc)
517 + goto err_out_link;
518 + rc = device_create_file(led_cdev->dev, &dev_attr_tx);
519 + if (rc)
520 + goto err_out_rx;
521 + rc = device_create_file(led_cdev->dev, &dev_attr_interval);
522 + if (rc)
523 + goto err_out_tx;
524 + rc = register_netdevice_notifier(&trigger_data->notifier);
525 + if (rc)
526 + goto err_out_interval;
527 + return;
528 +
529 +err_out_interval:
530 + device_remove_file(led_cdev->dev, &dev_attr_interval);
531 +err_out_tx:
532 + device_remove_file(led_cdev->dev, &dev_attr_tx);
533 +err_out_rx:
534 + device_remove_file(led_cdev->dev, &dev_attr_rx);
535 +err_out_link:
536 + device_remove_file(led_cdev->dev, &dev_attr_link);
537 +err_out_device_name:
538 + device_remove_file(led_cdev->dev, &dev_attr_device_name);
539 +err_out:
540 + led_cdev->trigger_data = NULL;
541 + kfree(trigger_data);
542 +}
543 +
544 +static void netdev_trig_deactivate(struct led_classdev *led_cdev)
545 +{
546 + struct led_netdev_data *trigger_data = led_cdev->trigger_data;
547 +
548 + if (trigger_data) {
549 + unregister_netdevice_notifier(&trigger_data->notifier);
550 +
551 + device_remove_file(led_cdev->dev, &dev_attr_device_name);
552 + device_remove_file(led_cdev->dev, &dev_attr_link);
553 + device_remove_file(led_cdev->dev, &dev_attr_rx);
554 + device_remove_file(led_cdev->dev, &dev_attr_tx);
555 + device_remove_file(led_cdev->dev, &dev_attr_interval);
556 +
557 + cancel_delayed_work_sync(&trigger_data->work);
558 +
559 + if (trigger_data->net_dev)
560 + dev_put(trigger_data->net_dev);
561 +
562 + kfree(trigger_data);
563 + }
564 +}
565 +
566 +static struct led_trigger netdev_led_trigger = {
567 + .name = "netdev",
568 + .activate = netdev_trig_activate,
569 + .deactivate = netdev_trig_deactivate,
570 +};
571 +
572 +static int __init netdev_trig_init(void)
573 +{
574 + return led_trigger_register(&netdev_led_trigger);
575 +}
576 +
577 +static void __exit netdev_trig_exit(void)
578 +{
579 + led_trigger_unregister(&netdev_led_trigger);
580 +}
581 +
582 +module_init(netdev_trig_init);
583 +module_exit(netdev_trig_exit);
584 +
585 +MODULE_AUTHOR("Ben Whitten <ben.whitten@gmail.com>");
586 +MODULE_AUTHOR("Oliver Jowett <oliver@opencloud.com>");
587 +MODULE_DESCRIPTION("Netdev LED trigger");
588 +MODULE_LICENSE("GPL v2");