a5ab4618281861a2b1af2cd093850217d2c177bd
[openwrt/staging/wigyori.git] /
1 From f22f95b9ff1551c9bab13104131929f33d51f23f Mon Sep 17 00:00:00 2001
2 From: Christian Marangi <ansuelsmth@gmail.com>
3 Date: Mon, 19 Jun 2023 22:46:59 +0200
4 Subject: [PATCH 2/3] leds: trigger: netdev: add additional specific link
5 duplex mode
6
7 Add additional modes for specific link duplex. Use ethtool APIs to get the
8 current link duplex and enable the LED accordingly. Under netdev event
9 handler the rtnl lock is already held and is not needed to be set to
10 access ethtool APIs.
11
12 This is especially useful for PHY and Switch that supports LEDs hw
13 control for specific link duplex.
14
15 Add additional modes:
16 - half_duplex: Turn on LED when link is half duplex
17 - full_duplex: Turn on LED when link is full duplex
18
19 Signed-off-by: Christian Marangi <ansuelsmth@gmail.com>
20 Reviewed-by: Andrew Lunn <andrew@lunn.ch>
21 Acked-by: Lee Jones <lee@kernel.org>
22 Signed-off-by: Jakub Kicinski <kuba@kernel.org>
23 ---
24 drivers/leds/trigger/ledtrig-netdev.c | 27 +++++++++++++++++++++++++--
25 include/linux/leds.h | 2 ++
26 2 files changed, 27 insertions(+), 2 deletions(-)
27
28 --- a/drivers/leds/trigger/ledtrig-netdev.c
29 +++ b/drivers/leds/trigger/ledtrig-netdev.c
30 @@ -55,6 +55,7 @@ struct led_netdev_data {
31
32 unsigned long mode;
33 int link_speed;
34 + u8 duplex;
35
36 bool carrier_link_up;
37 bool hw_control;
38 @@ -98,6 +99,14 @@ static void set_baseline_state(struct le
39 trigger_data->link_speed == SPEED_1000)
40 blink_on = true;
41
42 + if (test_bit(TRIGGER_NETDEV_HALF_DUPLEX, &trigger_data->mode) &&
43 + trigger_data->duplex == DUPLEX_HALF)
44 + blink_on = true;
45 +
46 + if (test_bit(TRIGGER_NETDEV_FULL_DUPLEX, &trigger_data->mode) &&
47 + trigger_data->duplex == DUPLEX_FULL)
48 + blink_on = true;
49 +
50 if (blink_on)
51 led_set_brightness(led_cdev,
52 led_cdev->blink_brightness);
53 @@ -190,8 +199,10 @@ static void get_device_state(struct led_
54 if (!trigger_data->carrier_link_up)
55 return;
56
57 - if (!__ethtool_get_link_ksettings(trigger_data->net_dev, &cmd))
58 + if (!__ethtool_get_link_ksettings(trigger_data->net_dev, &cmd)) {
59 trigger_data->link_speed = cmd.base.speed;
60 + trigger_data->duplex = cmd.base.duplex;
61 + }
62 }
63
64 static ssize_t device_name_show(struct device *dev,
65 @@ -230,6 +241,7 @@ static int set_device_name(struct led_ne
66
67 trigger_data->carrier_link_up = false;
68 trigger_data->link_speed = SPEED_UNKNOWN;
69 + trigger_data->duplex = DUPLEX_UNKNOWN;
70 if (trigger_data->net_dev != NULL) {
71 rtnl_lock();
72 get_device_state(trigger_data);
73 @@ -274,6 +286,8 @@ static ssize_t netdev_led_attr_show(stru
74 case TRIGGER_NETDEV_LINK_10:
75 case TRIGGER_NETDEV_LINK_100:
76 case TRIGGER_NETDEV_LINK_1000:
77 + case TRIGGER_NETDEV_HALF_DUPLEX:
78 + case TRIGGER_NETDEV_FULL_DUPLEX:
79 case TRIGGER_NETDEV_TX:
80 case TRIGGER_NETDEV_RX:
81 bit = attr;
82 @@ -302,6 +316,8 @@ static ssize_t netdev_led_attr_store(str
83 case TRIGGER_NETDEV_LINK_10:
84 case TRIGGER_NETDEV_LINK_100:
85 case TRIGGER_NETDEV_LINK_1000:
86 + case TRIGGER_NETDEV_HALF_DUPLEX:
87 + case TRIGGER_NETDEV_FULL_DUPLEX:
88 case TRIGGER_NETDEV_TX:
89 case TRIGGER_NETDEV_RX:
90 bit = attr;
91 @@ -348,6 +364,8 @@ DEFINE_NETDEV_TRIGGER(link, TRIGGER_NETD
92 DEFINE_NETDEV_TRIGGER(link_10, TRIGGER_NETDEV_LINK_10);
93 DEFINE_NETDEV_TRIGGER(link_100, TRIGGER_NETDEV_LINK_100);
94 DEFINE_NETDEV_TRIGGER(link_1000, TRIGGER_NETDEV_LINK_1000);
95 +DEFINE_NETDEV_TRIGGER(half_duplex, TRIGGER_NETDEV_HALF_DUPLEX);
96 +DEFINE_NETDEV_TRIGGER(full_duplex, TRIGGER_NETDEV_FULL_DUPLEX);
97 DEFINE_NETDEV_TRIGGER(tx, TRIGGER_NETDEV_TX);
98 DEFINE_NETDEV_TRIGGER(rx, TRIGGER_NETDEV_RX);
99
100 @@ -394,6 +412,8 @@ static struct attribute *netdev_trig_att
101 &dev_attr_link_10.attr,
102 &dev_attr_link_100.attr,
103 &dev_attr_link_1000.attr,
104 + &dev_attr_full_duplex.attr,
105 + &dev_attr_half_duplex.attr,
106 &dev_attr_rx.attr,
107 &dev_attr_tx.attr,
108 &dev_attr_interval.attr,
109 @@ -425,6 +445,7 @@ static int netdev_trig_notify(struct not
110
111 trigger_data->carrier_link_up = false;
112 trigger_data->link_speed = SPEED_UNKNOWN;
113 + trigger_data->duplex = DUPLEX_UNKNOWN;
114 switch (evt) {
115 case NETDEV_CHANGENAME:
116 get_device_state(trigger_data);
117 @@ -487,7 +508,9 @@ static void netdev_trig_work(struct work
118 invert = test_bit(TRIGGER_NETDEV_LINK, &trigger_data->mode) ||
119 test_bit(TRIGGER_NETDEV_LINK_10, &trigger_data->mode) ||
120 test_bit(TRIGGER_NETDEV_LINK_100, &trigger_data->mode) ||
121 - test_bit(TRIGGER_NETDEV_LINK_1000, &trigger_data->mode);
122 + test_bit(TRIGGER_NETDEV_LINK_1000, &trigger_data->mode) ||
123 + test_bit(TRIGGER_NETDEV_HALF_DUPLEX, &trigger_data->mode) ||
124 + test_bit(TRIGGER_NETDEV_FULL_DUPLEX, &trigger_data->mode);
125 interval = jiffies_to_msecs(
126 atomic_read(&trigger_data->interval));
127 /* base state is ON (link present) */
128 --- a/include/linux/leds.h
129 +++ b/include/linux/leds.h
130 @@ -533,6 +533,8 @@ enum led_trigger_netdev_modes {
131 TRIGGER_NETDEV_LINK_10,
132 TRIGGER_NETDEV_LINK_100,
133 TRIGGER_NETDEV_LINK_1000,
134 + TRIGGER_NETDEV_HALF_DUPLEX,
135 + TRIGGER_NETDEV_FULL_DUPLEX,
136 TRIGGER_NETDEV_TX,
137 TRIGGER_NETDEV_RX,
138