xref: /linux/drivers/leds/trigger/ledtrig-netdev.c (revision c060f8168bdf22aa986970955af99702d142dfbe)
1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright 2017 Ben Whitten <ben.whitten@gmail.com>
3 // Copyright 2007 Oliver Jowett <oliver@opencloud.com>
4 //
5 // LED Kernel Netdev Trigger
6 //
7 // Toggles the LED to reflect the link and traffic state of a named net device
8 //
9 // Derived from ledtrig-timer.c which is:
10 //  Copyright 2005-2006 Openedhand Ltd.
11 //  Author: Richard Purdie <rpurdie@openedhand.com>
12 
13 #include <linux/atomic.h>
14 #include <linux/ctype.h>
15 #include <linux/device.h>
16 #include <linux/ethtool.h>
17 #include <linux/init.h>
18 #include <linux/jiffies.h>
19 #include <linux/kernel.h>
20 #include <linux/leds.h>
21 #include <linux/linkmode.h>
22 #include <linux/list.h>
23 #include <linux/module.h>
24 #include <linux/netdevice.h>
25 #include <linux/mutex.h>
26 #include <linux/phy.h>
27 #include <linux/rtnetlink.h>
28 #include <linux/timer.h>
29 #include "../leds.h"
30 
31 #define NETDEV_LED_DEFAULT_INTERVAL	50
32 
33 /*
34  * Configurable sysfs attributes:
35  *
36  * device_name - network device name to monitor
37  * interval - duration of LED blink, in milliseconds
38  * link -  LED's normal state reflects whether the link is up
39  *         (has carrier) or not
40  * tx -  LED blinks on transmitted data
41  * rx -  LED blinks on receive data
42  * tx_err -  LED blinks on transmit error
43  * rx_err -  LED blinks on receive error
44  *
45  * Note: If the user selects a mode that is not supported by hw, default
46  * behavior is to fall back to software control of the LED. However not every
47  * hw supports software control. LED callbacks brightness_set() and
48  * brightness_set_blocking() are NULL in this case. hw_control_is_supported()
49  * should use available means supported by hw to inform the user that selected
50  * mode isn't supported by hw. This could be switching off the LED or any
51  * hw blink mode. If software control fallback isn't possible, we return
52  * -EOPNOTSUPP to the user, but still store the selected mode. This is needed
53  * in case an intermediate unsupported mode is necessary to switch from one
54  * supported mode to another.
55  */
56 
57 struct led_netdev_data {
58 	struct mutex lock;
59 
60 	struct delayed_work work;
61 	struct notifier_block notifier;
62 
63 	struct led_classdev *led_cdev;
64 	struct net_device *net_dev;
65 
66 	char device_name[IFNAMSIZ];
67 	atomic_t interval;
68 	unsigned int last_activity;
69 
70 	unsigned long mode;
71 	int link_speed;
72 	__ETHTOOL_DECLARE_LINK_MODE_MASK(supported_link_modes);
73 	u8 duplex;
74 
75 	bool carrier_link_up;
76 	bool hw_control;
77 };
78 
79 static const struct attribute_group netdev_trig_link_speed_attrs_group;
80 
81 static void set_baseline_state(struct led_netdev_data *trigger_data)
82 {
83 	int current_brightness;
84 	struct led_classdev *led_cdev = trigger_data->led_cdev;
85 
86 	/* Already validated, hw control is possible with the requested mode */
87 	if (trigger_data->hw_control) {
88 		led_cdev->hw_control_set(led_cdev, trigger_data->mode);
89 
90 		return;
91 	}
92 
93 	current_brightness = led_cdev->brightness;
94 	if (current_brightness)
95 		led_cdev->blink_brightness = current_brightness;
96 	if (!led_cdev->blink_brightness)
97 		led_cdev->blink_brightness = led_cdev->max_brightness;
98 
99 	if (!trigger_data->carrier_link_up) {
100 		led_set_brightness(led_cdev, LED_OFF);
101 	} else {
102 		bool blink_on = false;
103 
104 		if (test_bit(TRIGGER_NETDEV_LINK, &trigger_data->mode))
105 			blink_on = true;
106 
107 		if (test_bit(TRIGGER_NETDEV_LINK_10, &trigger_data->mode) &&
108 		    trigger_data->link_speed == SPEED_10)
109 			blink_on = true;
110 
111 		if (test_bit(TRIGGER_NETDEV_LINK_100, &trigger_data->mode) &&
112 		    trigger_data->link_speed == SPEED_100)
113 			blink_on = true;
114 
115 		if (test_bit(TRIGGER_NETDEV_LINK_1000, &trigger_data->mode) &&
116 		    trigger_data->link_speed == SPEED_1000)
117 			blink_on = true;
118 
119 		if (test_bit(TRIGGER_NETDEV_LINK_2500, &trigger_data->mode) &&
120 		    trigger_data->link_speed == SPEED_2500)
121 			blink_on = true;
122 
123 		if (test_bit(TRIGGER_NETDEV_LINK_5000, &trigger_data->mode) &&
124 		    trigger_data->link_speed == SPEED_5000)
125 			blink_on = true;
126 
127 		if (test_bit(TRIGGER_NETDEV_LINK_10000, &trigger_data->mode) &&
128 		    trigger_data->link_speed == SPEED_10000)
129 			blink_on = true;
130 
131 		if (test_bit(TRIGGER_NETDEV_HALF_DUPLEX, &trigger_data->mode) &&
132 		    trigger_data->duplex == DUPLEX_HALF)
133 			blink_on = true;
134 
135 		if (test_bit(TRIGGER_NETDEV_FULL_DUPLEX, &trigger_data->mode) &&
136 		    trigger_data->duplex == DUPLEX_FULL)
137 			blink_on = true;
138 
139 		if (blink_on)
140 			led_set_brightness(led_cdev,
141 					   led_cdev->blink_brightness);
142 		else
143 			led_set_brightness(led_cdev, LED_OFF);
144 
145 		/* If we are looking for RX/TX start periodically
146 		 * checking stats
147 		 */
148 		if (test_bit(TRIGGER_NETDEV_TX, &trigger_data->mode) ||
149 		    test_bit(TRIGGER_NETDEV_RX, &trigger_data->mode) ||
150 		    test_bit(TRIGGER_NETDEV_TX_ERR, &trigger_data->mode) ||
151 		    test_bit(TRIGGER_NETDEV_RX_ERR, &trigger_data->mode))
152 			schedule_delayed_work(&trigger_data->work, 0);
153 	}
154 }
155 
156 static bool supports_hw_control(struct led_classdev *led_cdev)
157 {
158 	if (!led_cdev->hw_control_get || !led_cdev->hw_control_set ||
159 	    !led_cdev->hw_control_is_supported)
160 		return false;
161 
162 	return !strcmp(led_cdev->hw_control_trigger, led_cdev->trigger->name);
163 }
164 
165 /*
166  * Validate the configured netdev is the same as the one associated with
167  * the LED driver in hw control.
168  */
169 static bool validate_net_dev(struct led_classdev *led_cdev,
170 			     struct net_device *net_dev)
171 {
172 	struct device *dev = led_cdev->hw_control_get_device(led_cdev);
173 	struct net_device *ndev;
174 
175 	if (!dev)
176 		return false;
177 
178 	ndev = to_net_dev(dev);
179 
180 	return ndev == net_dev;
181 }
182 
183 static bool can_hw_control(struct led_netdev_data *trigger_data)
184 {
185 	unsigned long default_interval = msecs_to_jiffies(NETDEV_LED_DEFAULT_INTERVAL);
186 	unsigned int interval = atomic_read(&trigger_data->interval);
187 	struct led_classdev *led_cdev = trigger_data->led_cdev;
188 	int ret;
189 
190 	if (!supports_hw_control(led_cdev))
191 		return false;
192 
193 	/*
194 	 * Interval must be set to the default
195 	 * value. Any different value is rejected if in hw
196 	 * control.
197 	 */
198 	if (interval != default_interval)
199 		return false;
200 
201 	/*
202 	 * net_dev must be set with hw control, otherwise no
203 	 * blinking can be happening and there is nothing to
204 	 * offloaded. Additionally, for hw control to be
205 	 * valid, the configured netdev must be the same as
206 	 * netdev associated to the LED.
207 	 */
208 	if (!validate_net_dev(led_cdev, trigger_data->net_dev))
209 		return false;
210 
211 	/* Check if the requested mode is supported */
212 	ret = led_cdev->hw_control_is_supported(led_cdev, trigger_data->mode);
213 	/* Fall back to software blinking if not supported */
214 	if (ret == -EOPNOTSUPP)
215 		return false;
216 	if (ret) {
217 		dev_warn(led_cdev->dev,
218 			 "Current mode check failed with error %d\n", ret);
219 		return false;
220 	}
221 
222 	return true;
223 }
224 
225 static void get_device_state(struct led_netdev_data *trigger_data)
226 {
227 	struct ethtool_link_ksettings cmd;
228 
229 	trigger_data->carrier_link_up = netif_carrier_ok(trigger_data->net_dev);
230 
231 	if (__ethtool_get_link_ksettings(trigger_data->net_dev, &cmd))
232 		return;
233 
234 	if (trigger_data->carrier_link_up) {
235 		trigger_data->link_speed = cmd.base.speed;
236 		trigger_data->duplex = cmd.base.duplex;
237 	}
238 
239 	/*
240 	 * Have a local copy of the link speed supported to avoid rtnl lock every time
241 	 * modes are refreshed on any change event
242 	 */
243 	linkmode_copy(trigger_data->supported_link_modes, cmd.link_modes.supported);
244 }
245 
246 static ssize_t device_name_show(struct device *dev,
247 				struct device_attribute *attr, char *buf)
248 {
249 	struct led_netdev_data *trigger_data = led_trigger_get_drvdata(dev);
250 	ssize_t len;
251 
252 	mutex_lock(&trigger_data->lock);
253 	len = sprintf(buf, "%s\n", trigger_data->device_name);
254 	mutex_unlock(&trigger_data->lock);
255 
256 	return len;
257 }
258 
259 static int set_device_name(struct led_netdev_data *trigger_data,
260 			   const char *name, size_t size)
261 {
262 	if (size >= IFNAMSIZ)
263 		return -EINVAL;
264 
265 	cancel_delayed_work_sync(&trigger_data->work);
266 
267 	/*
268 	 * Take RTNL lock before trigger_data lock to prevent potential
269 	 * deadlock with netdev notifier registration.
270 	 */
271 	rtnl_lock();
272 	mutex_lock(&trigger_data->lock);
273 
274 	if (trigger_data->net_dev) {
275 		dev_put(trigger_data->net_dev);
276 		trigger_data->net_dev = NULL;
277 	}
278 
279 	memcpy(trigger_data->device_name, name, size);
280 	trigger_data->device_name[size] = 0;
281 	if (size > 0 && trigger_data->device_name[size - 1] == '\n')
282 		trigger_data->device_name[size - 1] = 0;
283 
284 	if (trigger_data->device_name[0] != 0)
285 		trigger_data->net_dev =
286 		    dev_get_by_name(&init_net, trigger_data->device_name);
287 
288 	trigger_data->carrier_link_up = false;
289 	trigger_data->link_speed = SPEED_UNKNOWN;
290 	trigger_data->duplex = DUPLEX_UNKNOWN;
291 	if (trigger_data->net_dev)
292 		get_device_state(trigger_data);
293 
294 	trigger_data->last_activity = 0;
295 
296 	/* Skip if we're called from netdev_trig_activate() and hw_control is true */
297 	if (!trigger_data->hw_control || led_get_trigger_data(trigger_data->led_cdev))
298 		set_baseline_state(trigger_data);
299 
300 	mutex_unlock(&trigger_data->lock);
301 	rtnl_unlock();
302 
303 	return 0;
304 }
305 
306 static ssize_t device_name_store(struct device *dev,
307 				 struct device_attribute *attr, const char *buf,
308 				 size_t size)
309 {
310 	struct led_netdev_data *trigger_data = led_trigger_get_drvdata(dev);
311 	int ret;
312 
313 	ret = set_device_name(trigger_data, buf, size);
314 
315 	if (ret < 0)
316 		return ret;
317 
318 	/* Refresh link_speed visibility */
319 	sysfs_update_group(&dev->kobj, &netdev_trig_link_speed_attrs_group);
320 
321 	return size;
322 }
323 
324 static DEVICE_ATTR_RW(device_name);
325 
326 static ssize_t netdev_led_attr_show(struct device *dev, char *buf,
327 				    enum led_trigger_netdev_modes attr)
328 {
329 	struct led_netdev_data *trigger_data = led_trigger_get_drvdata(dev);
330 	int bit;
331 
332 	switch (attr) {
333 	case TRIGGER_NETDEV_LINK:
334 	case TRIGGER_NETDEV_LINK_10:
335 	case TRIGGER_NETDEV_LINK_100:
336 	case TRIGGER_NETDEV_LINK_1000:
337 	case TRIGGER_NETDEV_LINK_2500:
338 	case TRIGGER_NETDEV_LINK_5000:
339 	case TRIGGER_NETDEV_LINK_10000:
340 	case TRIGGER_NETDEV_HALF_DUPLEX:
341 	case TRIGGER_NETDEV_FULL_DUPLEX:
342 	case TRIGGER_NETDEV_TX:
343 	case TRIGGER_NETDEV_RX:
344 	case TRIGGER_NETDEV_TX_ERR:
345 	case TRIGGER_NETDEV_RX_ERR:
346 		bit = attr;
347 		break;
348 	default:
349 		return -EINVAL;
350 	}
351 
352 	return sprintf(buf, "%u\n", test_bit(bit, &trigger_data->mode));
353 }
354 
355 static ssize_t netdev_led_attr_store(struct device *dev, const char *buf,
356 				     size_t size, enum led_trigger_netdev_modes attr)
357 {
358 	struct led_netdev_data *trigger_data = led_trigger_get_drvdata(dev);
359 	struct led_classdev *led_cdev = trigger_data->led_cdev;
360 	unsigned long state, mode = trigger_data->mode;
361 	int ret;
362 	int bit;
363 
364 	ret = kstrtoul(buf, 0, &state);
365 	if (ret)
366 		return ret;
367 
368 	switch (attr) {
369 	case TRIGGER_NETDEV_LINK:
370 	case TRIGGER_NETDEV_LINK_10:
371 	case TRIGGER_NETDEV_LINK_100:
372 	case TRIGGER_NETDEV_LINK_1000:
373 	case TRIGGER_NETDEV_LINK_2500:
374 	case TRIGGER_NETDEV_LINK_5000:
375 	case TRIGGER_NETDEV_LINK_10000:
376 	case TRIGGER_NETDEV_HALF_DUPLEX:
377 	case TRIGGER_NETDEV_FULL_DUPLEX:
378 	case TRIGGER_NETDEV_TX:
379 	case TRIGGER_NETDEV_RX:
380 	case TRIGGER_NETDEV_TX_ERR:
381 	case TRIGGER_NETDEV_RX_ERR:
382 		bit = attr;
383 		break;
384 	default:
385 		return -EINVAL;
386 	}
387 
388 	if (state)
389 		set_bit(bit, &mode);
390 	else
391 		clear_bit(bit, &mode);
392 
393 	if (test_bit(TRIGGER_NETDEV_LINK, &mode) &&
394 	    (test_bit(TRIGGER_NETDEV_LINK_10, &mode) ||
395 	     test_bit(TRIGGER_NETDEV_LINK_100, &mode) ||
396 	     test_bit(TRIGGER_NETDEV_LINK_1000, &mode) ||
397 	     test_bit(TRIGGER_NETDEV_LINK_2500, &mode) ||
398 	     test_bit(TRIGGER_NETDEV_LINK_5000, &mode) ||
399 	     test_bit(TRIGGER_NETDEV_LINK_10000, &mode)))
400 		return -EINVAL;
401 
402 	cancel_delayed_work_sync(&trigger_data->work);
403 
404 	trigger_data->mode = mode;
405 	trigger_data->hw_control = can_hw_control(trigger_data);
406 
407 	if (!led_cdev->brightness_set && !led_cdev->brightness_set_blocking &&
408 	    !trigger_data->hw_control)
409 		return -EOPNOTSUPP;
410 
411 	set_baseline_state(trigger_data);
412 
413 	return size;
414 }
415 
416 #define DEFINE_NETDEV_TRIGGER(trigger_name, trigger) \
417 	static ssize_t trigger_name##_show(struct device *dev, \
418 		struct device_attribute *attr, char *buf) \
419 	{ \
420 		return netdev_led_attr_show(dev, buf, trigger); \
421 	} \
422 	static ssize_t trigger_name##_store(struct device *dev, \
423 		struct device_attribute *attr, const char *buf, size_t size) \
424 	{ \
425 		return netdev_led_attr_store(dev, buf, size, trigger); \
426 	} \
427 	static DEVICE_ATTR_RW(trigger_name)
428 
429 DEFINE_NETDEV_TRIGGER(link, TRIGGER_NETDEV_LINK);
430 DEFINE_NETDEV_TRIGGER(link_10, TRIGGER_NETDEV_LINK_10);
431 DEFINE_NETDEV_TRIGGER(link_100, TRIGGER_NETDEV_LINK_100);
432 DEFINE_NETDEV_TRIGGER(link_1000, TRIGGER_NETDEV_LINK_1000);
433 DEFINE_NETDEV_TRIGGER(link_2500, TRIGGER_NETDEV_LINK_2500);
434 DEFINE_NETDEV_TRIGGER(link_5000, TRIGGER_NETDEV_LINK_5000);
435 DEFINE_NETDEV_TRIGGER(link_10000, TRIGGER_NETDEV_LINK_10000);
436 DEFINE_NETDEV_TRIGGER(half_duplex, TRIGGER_NETDEV_HALF_DUPLEX);
437 DEFINE_NETDEV_TRIGGER(full_duplex, TRIGGER_NETDEV_FULL_DUPLEX);
438 DEFINE_NETDEV_TRIGGER(tx, TRIGGER_NETDEV_TX);
439 DEFINE_NETDEV_TRIGGER(rx, TRIGGER_NETDEV_RX);
440 DEFINE_NETDEV_TRIGGER(tx_err, TRIGGER_NETDEV_TX_ERR);
441 DEFINE_NETDEV_TRIGGER(rx_err, TRIGGER_NETDEV_RX_ERR);
442 
443 static ssize_t interval_show(struct device *dev,
444 			     struct device_attribute *attr, char *buf)
445 {
446 	struct led_netdev_data *trigger_data = led_trigger_get_drvdata(dev);
447 
448 	return sprintf(buf, "%u\n",
449 		       jiffies_to_msecs(atomic_read(&trigger_data->interval)));
450 }
451 
452 static ssize_t interval_store(struct device *dev,
453 			      struct device_attribute *attr, const char *buf,
454 			      size_t size)
455 {
456 	struct led_netdev_data *trigger_data = led_trigger_get_drvdata(dev);
457 	unsigned long value;
458 	int ret;
459 
460 	if (trigger_data->hw_control)
461 		return -EINVAL;
462 
463 	ret = kstrtoul(buf, 0, &value);
464 	if (ret)
465 		return ret;
466 
467 	/* impose some basic bounds on the timer interval */
468 	if (value >= 5 && value <= 10000) {
469 		cancel_delayed_work_sync(&trigger_data->work);
470 
471 		atomic_set(&trigger_data->interval, msecs_to_jiffies(value));
472 		set_baseline_state(trigger_data);	/* resets timer */
473 	}
474 
475 	return size;
476 }
477 
478 static DEVICE_ATTR_RW(interval);
479 
480 static ssize_t offloaded_show(struct device *dev,
481 			      struct device_attribute *attr, char *buf)
482 {
483 	struct led_netdev_data *trigger_data = led_trigger_get_drvdata(dev);
484 
485 	return sprintf(buf, "%d\n", trigger_data->hw_control);
486 }
487 
488 static DEVICE_ATTR_RO(offloaded);
489 
490 #define CHECK_LINK_MODE_ATTR(link_speed) \
491 	do { \
492 		if (attr == &dev_attr_link_##link_speed.attr && \
493 		    link_ksettings.base.speed == SPEED_##link_speed) \
494 			return attr->mode; \
495 	} while (0)
496 
497 static umode_t netdev_trig_link_speed_visible(struct kobject *kobj,
498 					      struct attribute *attr, int n)
499 {
500 	struct device *dev = kobj_to_dev(kobj);
501 	struct led_netdev_data *trigger_data;
502 	unsigned long *supported_link_modes;
503 	u32 mode;
504 
505 	trigger_data = led_trigger_get_drvdata(dev);
506 	supported_link_modes = trigger_data->supported_link_modes;
507 
508 	/*
509 	 * Search in the supported link mode mask a matching supported mode.
510 	 * Stop at the first matching entry as we care only to check if a particular
511 	 * speed is supported and not the kind.
512 	 */
513 	for_each_set_bit(mode, supported_link_modes, __ETHTOOL_LINK_MODE_MASK_NBITS) {
514 		struct ethtool_link_ksettings link_ksettings;
515 
516 		ethtool_params_from_link_mode(&link_ksettings, mode);
517 
518 		CHECK_LINK_MODE_ATTR(10);
519 		CHECK_LINK_MODE_ATTR(100);
520 		CHECK_LINK_MODE_ATTR(1000);
521 		CHECK_LINK_MODE_ATTR(2500);
522 		CHECK_LINK_MODE_ATTR(5000);
523 		CHECK_LINK_MODE_ATTR(10000);
524 	}
525 
526 	return 0;
527 }
528 
529 static struct attribute *netdev_trig_link_speed_attrs[] = {
530 	&dev_attr_link_10.attr,
531 	&dev_attr_link_100.attr,
532 	&dev_attr_link_1000.attr,
533 	&dev_attr_link_2500.attr,
534 	&dev_attr_link_5000.attr,
535 	&dev_attr_link_10000.attr,
536 	NULL
537 };
538 
539 static const struct attribute_group netdev_trig_link_speed_attrs_group = {
540 	.attrs = netdev_trig_link_speed_attrs,
541 	.is_visible = netdev_trig_link_speed_visible,
542 };
543 
544 static struct attribute *netdev_trig_attrs[] = {
545 	&dev_attr_device_name.attr,
546 	&dev_attr_link.attr,
547 	&dev_attr_full_duplex.attr,
548 	&dev_attr_half_duplex.attr,
549 	&dev_attr_rx.attr,
550 	&dev_attr_tx.attr,
551 	&dev_attr_rx_err.attr,
552 	&dev_attr_tx_err.attr,
553 	&dev_attr_interval.attr,
554 	&dev_attr_offloaded.attr,
555 	NULL
556 };
557 
558 static const struct attribute_group netdev_trig_attrs_group = {
559 	.attrs = netdev_trig_attrs,
560 };
561 
562 static const struct attribute_group *netdev_trig_groups[] = {
563 	&netdev_trig_attrs_group,
564 	&netdev_trig_link_speed_attrs_group,
565 	NULL,
566 };
567 
568 static int netdev_trig_notify(struct notifier_block *nb,
569 			      unsigned long evt, void *dv)
570 {
571 	struct net_device *dev =
572 		netdev_notifier_info_to_dev((struct netdev_notifier_info *)dv);
573 	struct led_netdev_data *trigger_data =
574 		container_of(nb, struct led_netdev_data, notifier);
575 	struct led_classdev *led_cdev = trigger_data->led_cdev;
576 
577 	if (evt != NETDEV_UP && evt != NETDEV_DOWN && evt != NETDEV_CHANGE
578 	    && evt != NETDEV_REGISTER && evt != NETDEV_UNREGISTER
579 	    && evt != NETDEV_CHANGENAME)
580 		return NOTIFY_DONE;
581 
582 	if (!(dev == trigger_data->net_dev ||
583 	      (evt == NETDEV_CHANGENAME && !strcmp(dev->name, trigger_data->device_name)) ||
584 	      (evt == NETDEV_REGISTER && !strcmp(dev->name, trigger_data->device_name))))
585 		return NOTIFY_DONE;
586 
587 	cancel_delayed_work_sync(&trigger_data->work);
588 
589 	mutex_lock(&trigger_data->lock);
590 
591 	trigger_data->carrier_link_up = false;
592 	trigger_data->link_speed = SPEED_UNKNOWN;
593 	trigger_data->duplex = DUPLEX_UNKNOWN;
594 	switch (evt) {
595 	case NETDEV_CHANGENAME:
596 	case NETDEV_REGISTER:
597 		dev_put(trigger_data->net_dev);
598 		dev_hold(dev);
599 		trigger_data->net_dev = dev;
600 		if (evt == NETDEV_CHANGENAME)
601 			get_device_state(trigger_data);
602 		break;
603 	case NETDEV_UNREGISTER:
604 		dev_put(trigger_data->net_dev);
605 		trigger_data->net_dev = NULL;
606 		break;
607 	case NETDEV_UP:
608 	case NETDEV_CHANGE:
609 		get_device_state(trigger_data);
610 		/* Refresh link_speed visibility */
611 		if (evt == NETDEV_CHANGE)
612 			sysfs_update_group(&led_cdev->dev->kobj,
613 					   &netdev_trig_link_speed_attrs_group);
614 		break;
615 	}
616 
617 	set_baseline_state(trigger_data);
618 
619 	mutex_unlock(&trigger_data->lock);
620 
621 	return NOTIFY_DONE;
622 }
623 
624 /* here's the real work! */
625 static void netdev_trig_work(struct work_struct *work)
626 {
627 	struct led_netdev_data *trigger_data =
628 		container_of(work, struct led_netdev_data, work.work);
629 	struct rtnl_link_stats64 *dev_stats;
630 	unsigned int new_activity;
631 	struct rtnl_link_stats64 temp;
632 	unsigned long interval;
633 	int invert;
634 
635 	/* If we dont have a device, insure we are off */
636 	if (!trigger_data->net_dev) {
637 		led_set_brightness(trigger_data->led_cdev, LED_OFF);
638 		return;
639 	}
640 
641 	/* If we are not looking for RX/TX then return  */
642 	if (!test_bit(TRIGGER_NETDEV_TX, &trigger_data->mode) &&
643 	    !test_bit(TRIGGER_NETDEV_RX, &trigger_data->mode) &&
644 	    !test_bit(TRIGGER_NETDEV_TX_ERR, &trigger_data->mode) &&
645 	    !test_bit(TRIGGER_NETDEV_RX_ERR, &trigger_data->mode))
646 		return;
647 
648 	dev_stats = dev_get_stats(trigger_data->net_dev, &temp);
649 	new_activity =
650 	    (test_bit(TRIGGER_NETDEV_TX, &trigger_data->mode) ?
651 		dev_stats->tx_packets : 0) +
652 	    (test_bit(TRIGGER_NETDEV_RX, &trigger_data->mode) ?
653 		dev_stats->rx_packets : 0) +
654 	    (test_bit(TRIGGER_NETDEV_TX_ERR, &trigger_data->mode) ?
655 		dev_stats->tx_errors : 0) +
656 	    (test_bit(TRIGGER_NETDEV_RX_ERR, &trigger_data->mode) ?
657 		dev_stats->rx_errors : 0);
658 
659 	if (trigger_data->last_activity != new_activity) {
660 		led_stop_software_blink(trigger_data->led_cdev);
661 
662 		invert = test_bit(TRIGGER_NETDEV_LINK, &trigger_data->mode) ||
663 			 test_bit(TRIGGER_NETDEV_LINK_10, &trigger_data->mode) ||
664 			 test_bit(TRIGGER_NETDEV_LINK_100, &trigger_data->mode) ||
665 			 test_bit(TRIGGER_NETDEV_LINK_1000, &trigger_data->mode) ||
666 			 test_bit(TRIGGER_NETDEV_LINK_2500, &trigger_data->mode) ||
667 			 test_bit(TRIGGER_NETDEV_LINK_5000, &trigger_data->mode) ||
668 			 test_bit(TRIGGER_NETDEV_LINK_10000, &trigger_data->mode) ||
669 			 test_bit(TRIGGER_NETDEV_HALF_DUPLEX, &trigger_data->mode) ||
670 			 test_bit(TRIGGER_NETDEV_FULL_DUPLEX, &trigger_data->mode);
671 		interval = jiffies_to_msecs(
672 				atomic_read(&trigger_data->interval));
673 		/* base state is ON (link present) */
674 		led_blink_set_oneshot(trigger_data->led_cdev,
675 				      &interval,
676 				      &interval,
677 				      invert);
678 		trigger_data->last_activity = new_activity;
679 	}
680 
681 	schedule_delayed_work(&trigger_data->work,
682 			(atomic_read(&trigger_data->interval)*2));
683 }
684 
685 static int netdev_trig_activate(struct led_classdev *led_cdev)
686 {
687 	struct led_netdev_data *trigger_data;
688 	unsigned long mode = 0;
689 	struct device *dev;
690 	int rc;
691 
692 	trigger_data = kzalloc(sizeof(struct led_netdev_data), GFP_KERNEL);
693 	if (!trigger_data)
694 		return -ENOMEM;
695 
696 	mutex_init(&trigger_data->lock);
697 
698 	trigger_data->notifier.notifier_call = netdev_trig_notify;
699 	trigger_data->notifier.priority = 10;
700 
701 	INIT_DELAYED_WORK(&trigger_data->work, netdev_trig_work);
702 
703 	trigger_data->led_cdev = led_cdev;
704 	trigger_data->net_dev = NULL;
705 	trigger_data->device_name[0] = 0;
706 
707 	trigger_data->mode = 0;
708 	atomic_set(&trigger_data->interval, msecs_to_jiffies(NETDEV_LED_DEFAULT_INTERVAL));
709 	trigger_data->last_activity = 0;
710 
711 	/* Check if hw control is active by default on the LED.
712 	 * Init already enabled mode in hw control.
713 	 */
714 	if (supports_hw_control(led_cdev)) {
715 		dev = led_cdev->hw_control_get_device(led_cdev);
716 		if (dev) {
717 			const char *name = dev_name(dev);
718 
719 			trigger_data->hw_control = true;
720 			set_device_name(trigger_data, name, strlen(name));
721 
722 			rc = led_cdev->hw_control_get(led_cdev, &mode);
723 			if (!rc)
724 				trigger_data->mode = mode;
725 		}
726 	}
727 
728 	led_set_trigger_data(led_cdev, trigger_data);
729 
730 	rc = register_netdevice_notifier(&trigger_data->notifier);
731 	if (rc)
732 		kfree(trigger_data);
733 
734 	return rc;
735 }
736 
737 static void netdev_trig_deactivate(struct led_classdev *led_cdev)
738 {
739 	struct led_netdev_data *trigger_data = led_get_trigger_data(led_cdev);
740 
741 	unregister_netdevice_notifier(&trigger_data->notifier);
742 
743 	cancel_delayed_work_sync(&trigger_data->work);
744 
745 	dev_put(trigger_data->net_dev);
746 
747 	kfree(trigger_data);
748 }
749 
750 static struct led_trigger netdev_led_trigger = {
751 	.name = "netdev",
752 	.activate = netdev_trig_activate,
753 	.deactivate = netdev_trig_deactivate,
754 	.groups = netdev_trig_groups,
755 };
756 
757 module_led_trigger(netdev_led_trigger);
758 
759 MODULE_AUTHOR("Ben Whitten <ben.whitten@gmail.com>");
760 MODULE_AUTHOR("Oliver Jowett <oliver@opencloud.com>");
761 MODULE_DESCRIPTION("Netdev LED trigger");
762 MODULE_LICENSE("GPL v2");
763 MODULE_ALIAS("ledtrig:netdev");
764