Skip to content

Commit 18764b8

Browse files
hkallweitdavem330
authored andcommitted
r8169: add support for LED's on RTL8168/RTL8101
This adds support for the LED's on most chip versions. Excluded are the old non-PCIe versions and RTL8125. RTL8125 has a different LED register layout, support for it will follow later. LED's can be controlled from userspace using the netdev LED trigger. Tested on RTL8168h. Note: The driver can't know which LED's are actually physically wired. Therefore not every LED device may represent a physically available LED. Signed-off-by: Heiner Kallweit <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent d7a39d3 commit 18764b8

File tree

4 files changed

+232
-0
lines changed

4 files changed

+232
-0
lines changed

drivers/net/ethernet/realtek/Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,7 @@ obj-$(CONFIG_8139CP) += 8139cp.o
77
obj-$(CONFIG_8139TOO) += 8139too.o
88
obj-$(CONFIG_ATP) += atp.o
99
r8169-objs += r8169_main.o r8169_firmware.o r8169_phy_config.o
10+
ifdef CONFIG_LEDS_TRIGGER_NETDEV
11+
r8169-objs += r8169_leds.o
12+
endif
1013
obj-$(CONFIG_R8169) += r8169.o

drivers/net/ethernet/realtek/r8169.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
* See MAINTAINERS file for support contact information.
99
*/
1010

11+
#include <linux/netdevice.h>
1112
#include <linux/types.h>
1213
#include <linux/phy.h>
1314

@@ -77,3 +78,9 @@ u16 rtl8168h_2_get_adc_bias_ioffset(struct rtl8169_private *tp);
7778
u8 rtl8168d_efuse_read(struct rtl8169_private *tp, int reg_addr);
7879
void r8169_hw_phy_config(struct rtl8169_private *tp, struct phy_device *phydev,
7980
enum mac_version ver);
81+
82+
void r8169_get_led_name(struct rtl8169_private *tp, int idx,
83+
char *buf, int buf_len);
84+
int rtl8168_get_led_mode(struct rtl8169_private *tp);
85+
int rtl8168_led_mod_ctrl(struct rtl8169_private *tp, u16 mask, u16 val);
86+
void rtl8168_init_leds(struct net_device *ndev);
Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
// SPDX-License-Identifier: GPL-2.0-only
2+
/* r8169_leds.c: Realtek 8169/8168/8101/8125 ethernet driver.
3+
*
4+
* Copyright (c) 2023 Heiner Kallweit <[email protected]>
5+
*
6+
* See MAINTAINERS file for support contact information.
7+
*/
8+
9+
#include <linux/leds.h>
10+
#include <linux/netdevice.h>
11+
#include <uapi/linux/uleds.h>
12+
13+
#include "r8169.h"
14+
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)
20+
21+
#define RTL8168_NUM_LEDS 3
22+
23+
#define RTL8168_SUPPORTED_MODES \
24+
(BIT(TRIGGER_NETDEV_LINK_1000) | BIT(TRIGGER_NETDEV_LINK_100) | \
25+
BIT(TRIGGER_NETDEV_LINK_10) | BIT(TRIGGER_NETDEV_RX) | \
26+
BIT(TRIGGER_NETDEV_TX))
27+
28+
struct r8169_led_classdev {
29+
struct led_classdev led;
30+
struct net_device *ndev;
31+
int index;
32+
};
33+
34+
#define lcdev_to_r8169_ldev(lcdev) container_of(lcdev, struct r8169_led_classdev, led)
35+
36+
static int rtl8168_led_hw_control_is_supported(struct led_classdev *led_cdev,
37+
unsigned long flags)
38+
{
39+
struct r8169_led_classdev *ldev = lcdev_to_r8169_ldev(led_cdev);
40+
struct rtl8169_private *tp = netdev_priv(ldev->ndev);
41+
int shift = ldev->index * 4;
42+
bool rx, tx;
43+
44+
if (flags & ~RTL8168_SUPPORTED_MODES)
45+
goto nosupp;
46+
47+
rx = flags & BIT(TRIGGER_NETDEV_RX);
48+
tx = flags & BIT(TRIGGER_NETDEV_TX);
49+
if (rx != tx)
50+
goto nosupp;
51+
52+
return 0;
53+
54+
nosupp:
55+
/* Switch LED off to indicate that mode isn't supported */
56+
rtl8168_led_mod_ctrl(tp, 0x000f << shift, 0);
57+
return -EOPNOTSUPP;
58+
}
59+
60+
static int rtl8168_led_hw_control_set(struct led_classdev *led_cdev,
61+
unsigned long flags)
62+
{
63+
struct r8169_led_classdev *ldev = lcdev_to_r8169_ldev(led_cdev);
64+
struct rtl8169_private *tp = netdev_priv(ldev->ndev);
65+
int shift = ldev->index * 4;
66+
u16 mode = 0;
67+
68+
if (flags & BIT(TRIGGER_NETDEV_LINK_10))
69+
mode |= RTL8168_LED_CTRL_LINK_10;
70+
if (flags & BIT(TRIGGER_NETDEV_LINK_100))
71+
mode |= RTL8168_LED_CTRL_LINK_100;
72+
if (flags & BIT(TRIGGER_NETDEV_LINK_1000))
73+
mode |= RTL8168_LED_CTRL_LINK_1000;
74+
if (flags & BIT(TRIGGER_NETDEV_TX))
75+
mode |= RTL8168_LED_CTRL_ACT;
76+
77+
return rtl8168_led_mod_ctrl(tp, 0x000f << shift, mode << shift);
78+
}
79+
80+
static int rtl8168_led_hw_control_get(struct led_classdev *led_cdev,
81+
unsigned long *flags)
82+
{
83+
struct r8169_led_classdev *ldev = lcdev_to_r8169_ldev(led_cdev);
84+
struct rtl8169_private *tp = netdev_priv(ldev->ndev);
85+
int shift = ldev->index * 4;
86+
int mode;
87+
88+
mode = rtl8168_get_led_mode(tp);
89+
if (mode < 0)
90+
return mode;
91+
92+
if (mode & RTL8168_LED_CTRL_OPTION2) {
93+
rtl8168_led_mod_ctrl(tp, RTL8168_LED_CTRL_OPTION2, 0);
94+
netdev_notice(ldev->ndev, "Deactivating unsupported Option2 LED mode\n");
95+
}
96+
97+
mode = (mode >> shift) & 0x000f;
98+
99+
if (mode & RTL8168_LED_CTRL_ACT)
100+
*flags |= BIT(TRIGGER_NETDEV_TX) | BIT(TRIGGER_NETDEV_RX);
101+
102+
if (mode & RTL8168_LED_CTRL_LINK_10)
103+
*flags |= BIT(TRIGGER_NETDEV_LINK_10);
104+
if (mode & RTL8168_LED_CTRL_LINK_100)
105+
*flags |= BIT(TRIGGER_NETDEV_LINK_100);
106+
if (mode & RTL8168_LED_CTRL_LINK_1000)
107+
*flags |= BIT(TRIGGER_NETDEV_LINK_1000);
108+
109+
return 0;
110+
}
111+
112+
static struct device *
113+
r8169_led_hw_control_get_device(struct led_classdev *led_cdev)
114+
{
115+
struct r8169_led_classdev *ldev = lcdev_to_r8169_ldev(led_cdev);
116+
117+
return &ldev->ndev->dev;
118+
}
119+
120+
static void rtl8168_setup_ldev(struct r8169_led_classdev *ldev,
121+
struct net_device *ndev, int index)
122+
{
123+
struct rtl8169_private *tp = netdev_priv(ndev);
124+
struct led_classdev *led_cdev = &ldev->led;
125+
char led_name[LED_MAX_NAME_SIZE];
126+
127+
ldev->ndev = ndev;
128+
ldev->index = index;
129+
130+
r8169_get_led_name(tp, index, led_name, LED_MAX_NAME_SIZE);
131+
led_cdev->name = led_name;
132+
led_cdev->default_trigger = "netdev";
133+
led_cdev->hw_control_trigger = "netdev";
134+
led_cdev->flags |= LED_RETAIN_AT_SHUTDOWN;
135+
led_cdev->hw_control_is_supported = rtl8168_led_hw_control_is_supported;
136+
led_cdev->hw_control_set = rtl8168_led_hw_control_set;
137+
led_cdev->hw_control_get = rtl8168_led_hw_control_get;
138+
led_cdev->hw_control_get_device = r8169_led_hw_control_get_device;
139+
140+
/* ignore errors */
141+
devm_led_classdev_register(&ndev->dev, led_cdev);
142+
}
143+
144+
void rtl8168_init_leds(struct net_device *ndev)
145+
{
146+
/* bind resource mgmt to netdev */
147+
struct device *dev = &ndev->dev;
148+
struct r8169_led_classdev *leds;
149+
int i;
150+
151+
leds = devm_kcalloc(dev, RTL8168_NUM_LEDS, sizeof(*leds), GFP_KERNEL);
152+
if (!leds)
153+
return;
154+
155+
for (i = 0; i < RTL8168_NUM_LEDS; i++)
156+
rtl8168_setup_ldev(leds + i, ndev, i);
157+
}

drivers/net/ethernet/realtek/r8169_main.c

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,7 @@ enum rtl8168_8101_registers {
285285
};
286286

287287
enum rtl8168_registers {
288+
LED_CTRL = 0x18,
288289
LED_FREQ = 0x1a,
289290
EEE_LED = 0x1b,
290291
ERIDR = 0x70,
@@ -616,6 +617,7 @@ struct rtl8169_private {
616617

617618
raw_spinlock_t config25_lock;
618619
raw_spinlock_t mac_ocp_lock;
620+
struct mutex led_lock; /* serialize LED ctrl RMW access */
619621

620622
raw_spinlock_t cfg9346_usage_lock;
621623
int cfg9346_usage_count;
@@ -788,6 +790,62 @@ static const struct rtl_cond name = { \
788790
\
789791
static bool name ## _check(struct rtl8169_private *tp)
790792

793+
int rtl8168_led_mod_ctrl(struct rtl8169_private *tp, u16 mask, u16 val)
794+
{
795+
struct device *dev = tp_to_dev(tp);
796+
int ret;
797+
798+
ret = pm_runtime_resume_and_get(dev);
799+
if (ret < 0)
800+
return ret;
801+
802+
mutex_lock(&tp->led_lock);
803+
RTL_W16(tp, LED_CTRL, (RTL_R16(tp, LED_CTRL) & ~mask) | val);
804+
mutex_unlock(&tp->led_lock);
805+
806+
pm_runtime_put_sync(dev);
807+
808+
return 0;
809+
}
810+
811+
int rtl8168_get_led_mode(struct rtl8169_private *tp)
812+
{
813+
struct device *dev = tp_to_dev(tp);
814+
int ret;
815+
816+
ret = pm_runtime_resume_and_get(dev);
817+
if (ret < 0)
818+
return ret;
819+
820+
ret = RTL_R16(tp, LED_CTRL);
821+
822+
pm_runtime_put_sync(dev);
823+
824+
return ret;
825+
}
826+
827+
void r8169_get_led_name(struct rtl8169_private *tp, int idx,
828+
char *buf, int buf_len)
829+
{
830+
struct pci_dev *pdev = tp->pci_dev;
831+
char pdom[8], pfun[8];
832+
int domain;
833+
834+
domain = pci_domain_nr(pdev->bus);
835+
if (domain)
836+
snprintf(pdom, sizeof(pdom), "P%d", domain);
837+
else
838+
pdom[0] = '\0';
839+
840+
if (pdev->multifunction)
841+
snprintf(pfun, sizeof(pfun), "f%d", PCI_FUNC(pdev->devfn));
842+
else
843+
pfun[0] = '\0';
844+
845+
snprintf(buf, buf_len, "en%sp%ds%d%s-%d::lan", pdom, pdev->bus->number,
846+
PCI_SLOT(pdev->devfn), pfun, idx);
847+
}
848+
791849
static void r8168fp_adjust_ocp_cmd(struct rtl8169_private *tp, u32 *cmd, int type)
792850
{
793851
/* based on RTL8168FP_OOBMAC_BASE in vendor driver */
@@ -5141,6 +5199,7 @@ static int rtl_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
51415199
raw_spin_lock_init(&tp->cfg9346_usage_lock);
51425200
raw_spin_lock_init(&tp->config25_lock);
51435201
raw_spin_lock_init(&tp->mac_ocp_lock);
5202+
mutex_init(&tp->led_lock);
51445203

51455204
dev->tstats = devm_netdev_alloc_pcpu_stats(&pdev->dev,
51465205
struct pcpu_sw_netstats);
@@ -5297,6 +5356,12 @@ static int rtl_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
52975356
if (rc)
52985357
return rc;
52995358

5359+
#if IS_REACHABLE(CONFIG_LEDS_CLASS) && IS_ENABLED(CONFIG_LEDS_TRIGGER_NETDEV)
5360+
if (tp->mac_version > RTL_GIGA_MAC_VER_06 &&
5361+
tp->mac_version < RTL_GIGA_MAC_VER_61)
5362+
rtl8168_init_leds(dev);
5363+
#endif
5364+
53005365
netdev_info(dev, "%s, %pM, XID %03x, IRQ %d\n",
53015366
rtl_chip_infos[chipset].name, dev->dev_addr, xid, tp->irq);
53025367

0 commit comments

Comments
 (0)