1 // SPDX-License-Identifier: GPL-2.0-only
2 /* r8169_leds.c: Realtek 8169/8168/8101/8125 ethernet driver.
4 * Copyright (c) 2023 Heiner Kallweit <hkallweit1@gmail.com>
6 * See MAINTAINERS file for support contact information.
9 #include <linux/leds.h>
10 #include <linux/netdevice.h>
11 #include <uapi/linux/uleds.h>
15 #define RTL8168_LED_CTRL_OPTION2 BIT(15)
16 #define RTL8168_LED_CTRL_ACT BIT(3)
17 #define RTL8168_LED_CTRL_LINK_1000 BIT(2)
18 #define RTL8168_LED_CTRL_LINK_100 BIT(1)
19 #define RTL8168_LED_CTRL_LINK_10 BIT(0)
21 #define RTL8125_LED_CTRL_ACT BIT(9)
22 #define RTL8125_LED_CTRL_LINK_2500 BIT(5)
23 #define RTL8125_LED_CTRL_LINK_1000 BIT(3)
24 #define RTL8125_LED_CTRL_LINK_100 BIT(1)
25 #define RTL8125_LED_CTRL_LINK_10 BIT(0)
27 #define RTL8168_NUM_LEDS 3
28 #define RTL8125_NUM_LEDS 4
30 struct r8169_led_classdev
{
31 struct led_classdev led
;
32 struct net_device
*ndev
;
36 #define lcdev_to_r8169_ldev(lcdev) container_of(lcdev, struct r8169_led_classdev, led)
38 static bool r8169_trigger_mode_is_valid(unsigned long flags
)
42 if (flags
& BIT(TRIGGER_NETDEV_HALF_DUPLEX
))
44 if (flags
& BIT(TRIGGER_NETDEV_FULL_DUPLEX
))
47 rx
= flags
& BIT(TRIGGER_NETDEV_RX
);
48 tx
= flags
& BIT(TRIGGER_NETDEV_TX
);
53 static int rtl8168_led_hw_control_is_supported(struct led_classdev
*led_cdev
,
56 struct r8169_led_classdev
*ldev
= lcdev_to_r8169_ldev(led_cdev
);
57 struct rtl8169_private
*tp
= netdev_priv(ldev
->ndev
);
58 int shift
= ldev
->index
* 4;
60 if (!r8169_trigger_mode_is_valid(flags
)) {
61 /* Switch LED off to indicate that mode isn't supported */
62 rtl8168_led_mod_ctrl(tp
, 0x000f << shift
, 0);
69 static int rtl8168_led_hw_control_set(struct led_classdev
*led_cdev
,
72 struct r8169_led_classdev
*ldev
= lcdev_to_r8169_ldev(led_cdev
);
73 struct rtl8169_private
*tp
= netdev_priv(ldev
->ndev
);
74 int shift
= ldev
->index
* 4;
77 if (flags
& BIT(TRIGGER_NETDEV_LINK_10
))
78 mode
|= RTL8168_LED_CTRL_LINK_10
;
79 if (flags
& BIT(TRIGGER_NETDEV_LINK_100
))
80 mode
|= RTL8168_LED_CTRL_LINK_100
;
81 if (flags
& BIT(TRIGGER_NETDEV_LINK_1000
))
82 mode
|= RTL8168_LED_CTRL_LINK_1000
;
83 if (flags
& BIT(TRIGGER_NETDEV_TX
))
84 mode
|= RTL8168_LED_CTRL_ACT
;
86 return rtl8168_led_mod_ctrl(tp
, 0x000f << shift
, mode
<< shift
);
89 static int rtl8168_led_hw_control_get(struct led_classdev
*led_cdev
,
92 struct r8169_led_classdev
*ldev
= lcdev_to_r8169_ldev(led_cdev
);
93 struct rtl8169_private
*tp
= netdev_priv(ldev
->ndev
);
94 int shift
= ldev
->index
* 4;
97 mode
= rtl8168_get_led_mode(tp
);
101 if (mode
& RTL8168_LED_CTRL_OPTION2
) {
102 rtl8168_led_mod_ctrl(tp
, RTL8168_LED_CTRL_OPTION2
, 0);
103 netdev_notice(ldev
->ndev
, "Deactivating unsupported Option2 LED mode\n");
106 mode
= (mode
>> shift
) & 0x000f;
108 if (mode
& RTL8168_LED_CTRL_ACT
)
109 *flags
|= BIT(TRIGGER_NETDEV_TX
) | BIT(TRIGGER_NETDEV_RX
);
111 if (mode
& RTL8168_LED_CTRL_LINK_10
)
112 *flags
|= BIT(TRIGGER_NETDEV_LINK_10
);
113 if (mode
& RTL8168_LED_CTRL_LINK_100
)
114 *flags
|= BIT(TRIGGER_NETDEV_LINK_100
);
115 if (mode
& RTL8168_LED_CTRL_LINK_1000
)
116 *flags
|= BIT(TRIGGER_NETDEV_LINK_1000
);
121 static struct device
*
122 r8169_led_hw_control_get_device(struct led_classdev
*led_cdev
)
124 struct r8169_led_classdev
*ldev
= lcdev_to_r8169_ldev(led_cdev
);
126 return &ldev
->ndev
->dev
;
129 static void rtl8168_setup_ldev(struct r8169_led_classdev
*ldev
,
130 struct net_device
*ndev
, int index
)
132 struct rtl8169_private
*tp
= netdev_priv(ndev
);
133 struct led_classdev
*led_cdev
= &ldev
->led
;
134 char led_name
[LED_MAX_NAME_SIZE
];
139 r8169_get_led_name(tp
, index
, led_name
, LED_MAX_NAME_SIZE
);
140 led_cdev
->name
= led_name
;
141 led_cdev
->hw_control_trigger
= "netdev";
142 led_cdev
->flags
|= LED_RETAIN_AT_SHUTDOWN
;
143 led_cdev
->hw_control_is_supported
= rtl8168_led_hw_control_is_supported
;
144 led_cdev
->hw_control_set
= rtl8168_led_hw_control_set
;
145 led_cdev
->hw_control_get
= rtl8168_led_hw_control_get
;
146 led_cdev
->hw_control_get_device
= r8169_led_hw_control_get_device
;
149 led_classdev_register(&ndev
->dev
, led_cdev
);
152 struct r8169_led_classdev
*rtl8168_init_leds(struct net_device
*ndev
)
154 struct r8169_led_classdev
*leds
;
157 leds
= kcalloc(RTL8168_NUM_LEDS
+ 1, sizeof(*leds
), GFP_KERNEL
);
161 for (i
= 0; i
< RTL8168_NUM_LEDS
; i
++)
162 rtl8168_setup_ldev(leds
+ i
, ndev
, i
);
167 static int rtl8125_led_hw_control_is_supported(struct led_classdev
*led_cdev
,
170 struct r8169_led_classdev
*ldev
= lcdev_to_r8169_ldev(led_cdev
);
171 struct rtl8169_private
*tp
= netdev_priv(ldev
->ndev
);
173 if (!r8169_trigger_mode_is_valid(flags
)) {
174 /* Switch LED off to indicate that mode isn't supported */
175 rtl8125_set_led_mode(tp
, ldev
->index
, 0);
182 static int rtl8125_led_hw_control_set(struct led_classdev
*led_cdev
,
185 struct r8169_led_classdev
*ldev
= lcdev_to_r8169_ldev(led_cdev
);
186 struct rtl8169_private
*tp
= netdev_priv(ldev
->ndev
);
189 if (flags
& BIT(TRIGGER_NETDEV_LINK_10
))
190 mode
|= RTL8125_LED_CTRL_LINK_10
;
191 if (flags
& BIT(TRIGGER_NETDEV_LINK_100
))
192 mode
|= RTL8125_LED_CTRL_LINK_100
;
193 if (flags
& BIT(TRIGGER_NETDEV_LINK_1000
))
194 mode
|= RTL8125_LED_CTRL_LINK_1000
;
195 if (flags
& BIT(TRIGGER_NETDEV_LINK_2500
))
196 mode
|= RTL8125_LED_CTRL_LINK_2500
;
197 if (flags
& (BIT(TRIGGER_NETDEV_TX
) | BIT(TRIGGER_NETDEV_RX
)))
198 mode
|= RTL8125_LED_CTRL_ACT
;
200 return rtl8125_set_led_mode(tp
, ldev
->index
, mode
);
203 static int rtl8125_led_hw_control_get(struct led_classdev
*led_cdev
,
204 unsigned long *flags
)
206 struct r8169_led_classdev
*ldev
= lcdev_to_r8169_ldev(led_cdev
);
207 struct rtl8169_private
*tp
= netdev_priv(ldev
->ndev
);
210 mode
= rtl8125_get_led_mode(tp
, ldev
->index
);
214 if (mode
& RTL8125_LED_CTRL_LINK_10
)
215 *flags
|= BIT(TRIGGER_NETDEV_LINK_10
);
216 if (mode
& RTL8125_LED_CTRL_LINK_100
)
217 *flags
|= BIT(TRIGGER_NETDEV_LINK_100
);
218 if (mode
& RTL8125_LED_CTRL_LINK_1000
)
219 *flags
|= BIT(TRIGGER_NETDEV_LINK_1000
);
220 if (mode
& RTL8125_LED_CTRL_LINK_2500
)
221 *flags
|= BIT(TRIGGER_NETDEV_LINK_2500
);
222 if (mode
& RTL8125_LED_CTRL_ACT
)
223 *flags
|= BIT(TRIGGER_NETDEV_TX
) | BIT(TRIGGER_NETDEV_RX
);
228 static void rtl8125_setup_led_ldev(struct r8169_led_classdev
*ldev
,
229 struct net_device
*ndev
, int index
)
231 struct rtl8169_private
*tp
= netdev_priv(ndev
);
232 struct led_classdev
*led_cdev
= &ldev
->led
;
233 char led_name
[LED_MAX_NAME_SIZE
];
238 r8169_get_led_name(tp
, index
, led_name
, LED_MAX_NAME_SIZE
);
239 led_cdev
->name
= led_name
;
240 led_cdev
->hw_control_trigger
= "netdev";
241 led_cdev
->flags
|= LED_RETAIN_AT_SHUTDOWN
;
242 led_cdev
->hw_control_is_supported
= rtl8125_led_hw_control_is_supported
;
243 led_cdev
->hw_control_set
= rtl8125_led_hw_control_set
;
244 led_cdev
->hw_control_get
= rtl8125_led_hw_control_get
;
245 led_cdev
->hw_control_get_device
= r8169_led_hw_control_get_device
;
248 led_classdev_register(&ndev
->dev
, led_cdev
);
251 struct r8169_led_classdev
*rtl8125_init_leds(struct net_device
*ndev
)
253 struct r8169_led_classdev
*leds
;
256 leds
= kcalloc(RTL8125_NUM_LEDS
+ 1, sizeof(*leds
), GFP_KERNEL
);
260 for (i
= 0; i
< RTL8125_NUM_LEDS
; i
++)
261 rtl8125_setup_led_ldev(leds
+ i
, ndev
, i
);
266 void r8169_remove_leds(struct r8169_led_classdev
*leds
)
271 for (struct r8169_led_classdev
*l
= leds
; l
->ndev
; l
++)
272 led_classdev_unregister(&l
->led
);