1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright 2017 Ben Whitten <ben.whitten@gmail.com>
3 // Copyright 2007 Oliver Jowett <oliver@opencloud.com>
5 // LED Kernel Netdev Trigger
7 // Toggles the LED to reflect the link and traffic state of a named net device
9 // Derived from ledtrig-timer.c which is:
10 // Copyright 2005-2006 Openedhand Ltd.
11 // Author: Richard Purdie <rpurdie@openedhand.com>
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>
31 #define NETDEV_LED_DEFAULT_INTERVAL 50
34 * Configurable sysfs attributes:
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
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.
57 struct led_netdev_data
{
60 struct delayed_work work
;
61 struct notifier_block notifier
;
63 struct led_classdev
*led_cdev
;
64 struct net_device
*net_dev
;
66 char device_name
[IFNAMSIZ
];
68 unsigned int last_activity
;
72 __ETHTOOL_DECLARE_LINK_MODE_MASK(supported_link_modes
);
79 static const struct attribute_group netdev_trig_link_speed_attrs_group
;
81 static void set_baseline_state(struct led_netdev_data
*trigger_data
)
83 int current_brightness
;
84 struct led_classdev
*led_cdev
= trigger_data
->led_cdev
;
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
);
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
;
99 if (!trigger_data
->carrier_link_up
) {
100 led_set_brightness(led_cdev
, LED_OFF
);
102 bool blink_on
= false;
104 if (test_bit(TRIGGER_NETDEV_LINK
, &trigger_data
->mode
))
107 if (test_bit(TRIGGER_NETDEV_LINK_10
, &trigger_data
->mode
) &&
108 trigger_data
->link_speed
== SPEED_10
)
111 if (test_bit(TRIGGER_NETDEV_LINK_100
, &trigger_data
->mode
) &&
112 trigger_data
->link_speed
== SPEED_100
)
115 if (test_bit(TRIGGER_NETDEV_LINK_1000
, &trigger_data
->mode
) &&
116 trigger_data
->link_speed
== SPEED_1000
)
119 if (test_bit(TRIGGER_NETDEV_LINK_2500
, &trigger_data
->mode
) &&
120 trigger_data
->link_speed
== SPEED_2500
)
123 if (test_bit(TRIGGER_NETDEV_LINK_5000
, &trigger_data
->mode
) &&
124 trigger_data
->link_speed
== SPEED_5000
)
127 if (test_bit(TRIGGER_NETDEV_LINK_10000
, &trigger_data
->mode
) &&
128 trigger_data
->link_speed
== SPEED_10000
)
131 if (test_bit(TRIGGER_NETDEV_HALF_DUPLEX
, &trigger_data
->mode
) &&
132 trigger_data
->duplex
== DUPLEX_HALF
)
135 if (test_bit(TRIGGER_NETDEV_FULL_DUPLEX
, &trigger_data
->mode
) &&
136 trigger_data
->duplex
== DUPLEX_FULL
)
140 led_set_brightness(led_cdev
,
141 led_cdev
->blink_brightness
);
143 led_set_brightness(led_cdev
, LED_OFF
);
145 /* If we are looking for RX/TX start periodically
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);
156 static bool supports_hw_control(struct led_classdev
*led_cdev
)
158 if (!led_cdev
->hw_control_get
|| !led_cdev
->hw_control_set
||
159 !led_cdev
->hw_control_is_supported
)
162 return !strcmp(led_cdev
->hw_control_trigger
, led_cdev
->trigger
->name
);
166 * Validate the configured netdev is the same as the one associated with
167 * the LED driver in hw control.
169 static bool validate_net_dev(struct led_classdev
*led_cdev
,
170 struct net_device
*net_dev
)
172 struct device
*dev
= led_cdev
->hw_control_get_device(led_cdev
);
173 struct net_device
*ndev
;
178 ndev
= to_net_dev(dev
);
180 return ndev
== net_dev
;
183 static bool can_hw_control(struct led_netdev_data
*trigger_data
)
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
;
190 if (!supports_hw_control(led_cdev
))
194 * Interval must be set to the default
195 * value. Any different value is rejected if in hw
198 if (interval
!= default_interval
)
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.
208 if (!validate_net_dev(led_cdev
, trigger_data
->net_dev
))
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
)
217 dev_warn(led_cdev
->dev
,
218 "Current mode check failed with error %d\n", ret
);
225 static void get_device_state(struct led_netdev_data
*trigger_data
)
227 struct ethtool_link_ksettings cmd
;
229 trigger_data
->carrier_link_up
= netif_carrier_ok(trigger_data
->net_dev
);
231 if (__ethtool_get_link_ksettings(trigger_data
->net_dev
, &cmd
))
234 if (trigger_data
->carrier_link_up
) {
235 trigger_data
->link_speed
= cmd
.base
.speed
;
236 trigger_data
->duplex
= cmd
.base
.duplex
;
240 * Have a local copy of the link speed supported to avoid rtnl lock every time
241 * modes are refreshed on any change event
243 linkmode_copy(trigger_data
->supported_link_modes
, cmd
.link_modes
.supported
);
246 static ssize_t
device_name_show(struct device
*dev
,
247 struct device_attribute
*attr
, char *buf
)
249 struct led_netdev_data
*trigger_data
= led_trigger_get_drvdata(dev
);
252 mutex_lock(&trigger_data
->lock
);
253 len
= sprintf(buf
, "%s\n", trigger_data
->device_name
);
254 mutex_unlock(&trigger_data
->lock
);
259 static int set_device_name(struct led_netdev_data
*trigger_data
,
260 const char *name
, size_t size
)
262 if (size
>= IFNAMSIZ
)
265 cancel_delayed_work_sync(&trigger_data
->work
);
268 * Take RTNL lock before trigger_data lock to prevent potential
269 * deadlock with netdev notifier registration.
272 mutex_lock(&trigger_data
->lock
);
274 if (trigger_data
->net_dev
) {
275 dev_put(trigger_data
->net_dev
);
276 trigger_data
->net_dev
= NULL
;
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;
284 if (trigger_data
->device_name
[0] != 0)
285 trigger_data
->net_dev
=
286 dev_get_by_name(&init_net
, trigger_data
->device_name
);
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
);
294 trigger_data
->last_activity
= 0;
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
);
300 mutex_unlock(&trigger_data
->lock
);
306 static ssize_t
device_name_store(struct device
*dev
,
307 struct device_attribute
*attr
, const char *buf
,
310 struct led_netdev_data
*trigger_data
= led_trigger_get_drvdata(dev
);
313 ret
= set_device_name(trigger_data
, buf
, size
);
318 /* Refresh link_speed visibility */
319 sysfs_update_group(&dev
->kobj
, &netdev_trig_link_speed_attrs_group
);
324 static DEVICE_ATTR_RW(device_name
);
326 static ssize_t
netdev_led_attr_show(struct device
*dev
, char *buf
,
327 enum led_trigger_netdev_modes attr
)
329 struct led_netdev_data
*trigger_data
= led_trigger_get_drvdata(dev
);
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
:
352 return sprintf(buf
, "%u\n", test_bit(bit
, &trigger_data
->mode
));
355 static ssize_t
netdev_led_attr_store(struct device
*dev
, const char *buf
,
356 size_t size
, enum led_trigger_netdev_modes attr
)
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
;
364 ret
= kstrtoul(buf
, 0, &state
);
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
:
391 clear_bit(bit
, &mode
);
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
)))
402 cancel_delayed_work_sync(&trigger_data
->work
);
404 trigger_data
->mode
= mode
;
405 trigger_data
->hw_control
= can_hw_control(trigger_data
);
407 if (!led_cdev
->brightness_set
&& !led_cdev
->brightness_set_blocking
&&
408 !trigger_data
->hw_control
)
411 set_baseline_state(trigger_data
);
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) \
420 return netdev_led_attr_show(dev, buf, trigger); \
422 static ssize_t trigger_name##_store(struct device *dev, \
423 struct device_attribute *attr, const char *buf, size_t size) \
425 return netdev_led_attr_store(dev, buf, size, trigger); \
427 static DEVICE_ATTR_RW(trigger_name)
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
);
443 static ssize_t
interval_show(struct device
*dev
,
444 struct device_attribute
*attr
, char *buf
)
446 struct led_netdev_data
*trigger_data
= led_trigger_get_drvdata(dev
);
448 return sprintf(buf
, "%u\n",
449 jiffies_to_msecs(atomic_read(&trigger_data
->interval
)));
452 static ssize_t
interval_store(struct device
*dev
,
453 struct device_attribute
*attr
, const char *buf
,
456 struct led_netdev_data
*trigger_data
= led_trigger_get_drvdata(dev
);
460 if (trigger_data
->hw_control
)
463 ret
= kstrtoul(buf
, 0, &value
);
467 /* impose some basic bounds on the timer interval */
468 if (value
>= 5 && value
<= 10000) {
469 cancel_delayed_work_sync(&trigger_data
->work
);
471 atomic_set(&trigger_data
->interval
, msecs_to_jiffies(value
));
472 set_baseline_state(trigger_data
); /* resets timer */
478 static DEVICE_ATTR_RW(interval
);
480 static ssize_t
offloaded_show(struct device
*dev
,
481 struct device_attribute
*attr
, char *buf
)
483 struct led_netdev_data
*trigger_data
= led_trigger_get_drvdata(dev
);
485 return sprintf(buf
, "%d\n", trigger_data
->hw_control
);
488 static DEVICE_ATTR_RO(offloaded
);
490 #define CHECK_LINK_MODE_ATTR(link_speed) \
492 if (attr == &dev_attr_link_##link_speed.attr && \
493 link_ksettings.base.speed == SPEED_##link_speed) \
497 static umode_t
netdev_trig_link_speed_visible(struct kobject
*kobj
,
498 struct attribute
*attr
, int n
)
500 struct device
*dev
= kobj_to_dev(kobj
);
501 struct led_netdev_data
*trigger_data
;
502 unsigned long *supported_link_modes
;
505 trigger_data
= led_trigger_get_drvdata(dev
);
506 supported_link_modes
= trigger_data
->supported_link_modes
;
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.
513 for_each_set_bit(mode
, supported_link_modes
, __ETHTOOL_LINK_MODE_MASK_NBITS
) {
514 struct ethtool_link_ksettings link_ksettings
;
516 ethtool_params_from_link_mode(&link_ksettings
, mode
);
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);
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
,
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
,
544 static struct attribute
*netdev_trig_attrs
[] = {
545 &dev_attr_device_name
.attr
,
547 &dev_attr_full_duplex
.attr
,
548 &dev_attr_half_duplex
.attr
,
551 &dev_attr_rx_err
.attr
,
552 &dev_attr_tx_err
.attr
,
553 &dev_attr_interval
.attr
,
554 &dev_attr_offloaded
.attr
,
558 static const struct attribute_group netdev_trig_attrs_group
= {
559 .attrs
= netdev_trig_attrs
,
562 static const struct attribute_group
*netdev_trig_groups
[] = {
563 &netdev_trig_attrs_group
,
564 &netdev_trig_link_speed_attrs_group
,
568 static int netdev_trig_notify(struct notifier_block
*nb
,
569 unsigned long evt
, void *dv
)
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
;
577 if (evt
!= NETDEV_UP
&& evt
!= NETDEV_DOWN
&& evt
!= NETDEV_CHANGE
578 && evt
!= NETDEV_REGISTER
&& evt
!= NETDEV_UNREGISTER
579 && evt
!= NETDEV_CHANGENAME
)
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
))))
587 cancel_delayed_work_sync(&trigger_data
->work
);
589 mutex_lock(&trigger_data
->lock
);
591 trigger_data
->carrier_link_up
= false;
592 trigger_data
->link_speed
= SPEED_UNKNOWN
;
593 trigger_data
->duplex
= DUPLEX_UNKNOWN
;
595 case NETDEV_CHANGENAME
:
596 case NETDEV_REGISTER
:
597 dev_put(trigger_data
->net_dev
);
599 trigger_data
->net_dev
= dev
;
600 if (evt
== NETDEV_CHANGENAME
)
601 get_device_state(trigger_data
);
603 case NETDEV_UNREGISTER
:
604 dev_put(trigger_data
->net_dev
);
605 trigger_data
->net_dev
= NULL
;
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
);
617 set_baseline_state(trigger_data
);
619 mutex_unlock(&trigger_data
->lock
);
624 /* here's the real work! */
625 static void netdev_trig_work(struct work_struct
*work
)
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
;
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
);
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
))
648 dev_stats
= dev_get_stats(trigger_data
->net_dev
, &temp
);
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);
659 if (trigger_data
->last_activity
!= new_activity
) {
660 led_stop_software_blink(trigger_data
->led_cdev
);
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
,
678 trigger_data
->last_activity
= new_activity
;
681 schedule_delayed_work(&trigger_data
->work
,
682 (atomic_read(&trigger_data
->interval
)*2));
685 static int netdev_trig_activate(struct led_classdev
*led_cdev
)
687 struct led_netdev_data
*trigger_data
;
688 unsigned long mode
= 0;
692 trigger_data
= kzalloc(sizeof(struct led_netdev_data
), GFP_KERNEL
);
696 mutex_init(&trigger_data
->lock
);
698 trigger_data
->notifier
.notifier_call
= netdev_trig_notify
;
699 trigger_data
->notifier
.priority
= 10;
701 INIT_DELAYED_WORK(&trigger_data
->work
, netdev_trig_work
);
703 trigger_data
->led_cdev
= led_cdev
;
704 trigger_data
->net_dev
= NULL
;
705 trigger_data
->device_name
[0] = 0;
707 trigger_data
->mode
= 0;
708 atomic_set(&trigger_data
->interval
, msecs_to_jiffies(NETDEV_LED_DEFAULT_INTERVAL
));
709 trigger_data
->last_activity
= 0;
711 /* Check if hw control is active by default on the LED.
712 * Init already enabled mode in hw control.
714 if (supports_hw_control(led_cdev
)) {
715 dev
= led_cdev
->hw_control_get_device(led_cdev
);
717 const char *name
= dev_name(dev
);
719 trigger_data
->hw_control
= true;
720 set_device_name(trigger_data
, name
, strlen(name
));
722 rc
= led_cdev
->hw_control_get(led_cdev
, &mode
);
724 trigger_data
->mode
= mode
;
728 led_set_trigger_data(led_cdev
, trigger_data
);
730 rc
= register_netdevice_notifier(&trigger_data
->notifier
);
737 static void netdev_trig_deactivate(struct led_classdev
*led_cdev
)
739 struct led_netdev_data
*trigger_data
= led_get_trigger_data(led_cdev
);
741 unregister_netdevice_notifier(&trigger_data
->notifier
);
743 cancel_delayed_work_sync(&trigger_data
->work
);
745 dev_put(trigger_data
->net_dev
);
750 static struct led_trigger netdev_led_trigger
= {
752 .activate
= netdev_trig_activate
,
753 .deactivate
= netdev_trig_deactivate
,
754 .groups
= netdev_trig_groups
,
757 module_led_trigger(netdev_led_trigger
);
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");