Skip to content

Commit c1aedf0

Browse files
hayesorzkuba-moo
authored andcommitted
net/usb/r8153_ecm: support ECM mode for RTL8153
Support ECM mode based on cdc_ether with relative mii functions, when CONFIG_USB_RTL8152 is not set, or the device is not supported by r8152 driver. Both r8152 and r8153_ecm would check the return value of rtl8152_get_version() in porbe(). If rtl8152_get_version() return none zero value, the r8152 is used for the device with vendor mode. Otherwise, the r8153_ecm is used for the device with ECM mode. Signed-off-by: Hayes Wang <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Jakub Kicinski <[email protected]>
1 parent 3ffec6a commit c1aedf0

File tree

4 files changed

+204
-27
lines changed

4 files changed

+204
-27
lines changed

drivers/net/usb/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ obj-$(CONFIG_USB_LAN78XX) += lan78xx.o
1313
obj-$(CONFIG_USB_NET_AX8817X) += asix.o
1414
asix-y := asix_devices.o asix_common.o ax88172a.o
1515
obj-$(CONFIG_USB_NET_AX88179_178A) += ax88179_178a.o
16-
obj-$(CONFIG_USB_NET_CDCETHER) += cdc_ether.o
16+
obj-$(CONFIG_USB_NET_CDCETHER) += cdc_ether.o r8153_ecm.o
1717
obj-$(CONFIG_USB_NET_CDC_EEM) += cdc_eem.o
1818
obj-$(CONFIG_USB_NET_DM9601) += dm9601.o
1919
obj-$(CONFIG_USB_NET_SR9700) += sr9700.o

drivers/net/usb/r8152.c

Lines changed: 4 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include <linux/acpi.h>
2727
#include <linux/firmware.h>
2828
#include <crypto/hash.h>
29+
#include <linux/usb/r8152.h>
2930

3031
/* Information for net-next */
3132
#define NETNEXT_VERSION "11"
@@ -653,18 +654,6 @@ enum rtl_register_content {
653654

654655
#define INTR_LINK 0x0004
655656

656-
#define RTL8152_REQT_READ 0xc0
657-
#define RTL8152_REQT_WRITE 0x40
658-
#define RTL8152_REQ_GET_REGS 0x05
659-
#define RTL8152_REQ_SET_REGS 0x05
660-
661-
#define BYTE_EN_DWORD 0xff
662-
#define BYTE_EN_WORD 0x33
663-
#define BYTE_EN_BYTE 0x11
664-
#define BYTE_EN_SIX_BYTES 0x3f
665-
#define BYTE_EN_START_MASK 0x0f
666-
#define BYTE_EN_END_MASK 0xf0
667-
668657
#define RTL8153_MAX_PACKET 9216 /* 9K */
669658
#define RTL8153_MAX_MTU (RTL8153_MAX_PACKET - VLAN_ETH_HLEN - \
670659
ETH_FCS_LEN)
@@ -689,21 +678,9 @@ enum rtl8152_flags {
689678
LENOVO_MACPASSTHRU,
690679
};
691680

692-
/* Define these values to match your device */
693-
#define VENDOR_ID_REALTEK 0x0bda
694-
#define VENDOR_ID_MICROSOFT 0x045e
695-
#define VENDOR_ID_SAMSUNG 0x04e8
696-
#define VENDOR_ID_LENOVO 0x17ef
697-
#define VENDOR_ID_LINKSYS 0x13b1
698-
#define VENDOR_ID_NVIDIA 0x0955
699-
#define VENDOR_ID_TPLINK 0x2357
700-
701681
#define DEVICE_ID_THINKPAD_THUNDERBOLT3_DOCK_GEN2 0x3082
702682
#define DEVICE_ID_THINKPAD_USB_C_DOCK_GEN2 0xa387
703683

704-
#define MCU_TYPE_PLA 0x0100
705-
#define MCU_TYPE_USB 0x0000
706-
707684
struct tally_counter {
708685
__le64 tx_packets;
709686
__le64 rx_packets;
@@ -6621,7 +6598,7 @@ static int rtl_fw_init(struct r8152 *tp)
66216598
return 0;
66226599
}
66236600

6624-
static u8 rtl_get_version(struct usb_interface *intf)
6601+
u8 rtl8152_get_version(struct usb_interface *intf)
66256602
{
66266603
struct usb_device *udev = interface_to_usbdev(intf);
66276604
u32 ocp_data = 0;
@@ -6679,12 +6656,13 @@ static u8 rtl_get_version(struct usb_interface *intf)
66796656

66806657
return version;
66816658
}
6659+
EXPORT_SYMBOL_GPL(rtl8152_get_version);
66826660

66836661
static int rtl8152_probe(struct usb_interface *intf,
66846662
const struct usb_device_id *id)
66856663
{
66866664
struct usb_device *udev = interface_to_usbdev(intf);
6687-
u8 version = rtl_get_version(intf);
6665+
u8 version = rtl8152_get_version(intf);
66886666
struct r8152 *tp;
66896667
struct net_device *netdev;
66906668
int ret;

drivers/net/usb/r8153_ecm.c

Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
// SPDX-License-Identifier: GPL-2.0-or-later
2+
#include <linux/module.h>
3+
#include <linux/netdevice.h>
4+
#include <linux/mii.h>
5+
#include <linux/usb.h>
6+
#include <linux/usb/cdc.h>
7+
#include <linux/usb/usbnet.h>
8+
#include <linux/usb/r8152.h>
9+
10+
#define OCP_BASE 0xe86c
11+
12+
static int pla_read_word(struct usbnet *dev, u16 index)
13+
{
14+
u16 byen = BYTE_EN_WORD;
15+
u8 shift = index & 2;
16+
__le32 tmp;
17+
int ret;
18+
19+
if (shift)
20+
byen <<= shift;
21+
22+
index &= ~3;
23+
24+
ret = usbnet_read_cmd(dev, RTL8152_REQ_GET_REGS, RTL8152_REQT_READ, index,
25+
MCU_TYPE_PLA | byen, &tmp, sizeof(tmp));
26+
if (ret < 0)
27+
goto out;
28+
29+
ret = __le32_to_cpu(tmp);
30+
ret >>= (shift * 8);
31+
ret &= 0xffff;
32+
33+
out:
34+
return ret;
35+
}
36+
37+
static int pla_write_word(struct usbnet *dev, u16 index, u32 data)
38+
{
39+
u32 mask = 0xffff;
40+
u16 byen = BYTE_EN_WORD;
41+
u8 shift = index & 2;
42+
__le32 tmp;
43+
int ret;
44+
45+
data &= mask;
46+
47+
if (shift) {
48+
byen <<= shift;
49+
mask <<= (shift * 8);
50+
data <<= (shift * 8);
51+
}
52+
53+
index &= ~3;
54+
55+
ret = usbnet_read_cmd(dev, RTL8152_REQ_GET_REGS, RTL8152_REQT_READ, index,
56+
MCU_TYPE_PLA | byen, &tmp, sizeof(tmp));
57+
58+
if (ret < 0)
59+
goto out;
60+
61+
data |= __le32_to_cpu(tmp) & ~mask;
62+
tmp = __cpu_to_le32(data);
63+
64+
ret = usbnet_write_cmd(dev, RTL8152_REQ_SET_REGS, RTL8152_REQT_WRITE, index,
65+
MCU_TYPE_PLA | byen, &tmp, sizeof(tmp));
66+
67+
out:
68+
return ret;
69+
}
70+
71+
static int r8153_ecm_mdio_read(struct net_device *netdev, int phy_id, int reg)
72+
{
73+
struct usbnet *dev = netdev_priv(netdev);
74+
int ret;
75+
76+
ret = pla_write_word(dev, OCP_BASE, 0xa000);
77+
if (ret < 0)
78+
goto out;
79+
80+
ret = pla_read_word(dev, 0xb400 + reg * 2);
81+
82+
out:
83+
return ret;
84+
}
85+
86+
static void r8153_ecm_mdio_write(struct net_device *netdev, int phy_id, int reg, int val)
87+
{
88+
struct usbnet *dev = netdev_priv(netdev);
89+
int ret;
90+
91+
ret = pla_write_word(dev, OCP_BASE, 0xa000);
92+
if (ret < 0)
93+
return;
94+
95+
ret = pla_write_word(dev, 0xb400 + reg * 2, val);
96+
}
97+
98+
static int r8153_bind(struct usbnet *dev, struct usb_interface *intf)
99+
{
100+
int status;
101+
102+
status = usbnet_cdc_bind(dev, intf);
103+
if (status < 0)
104+
return status;
105+
106+
dev->mii.dev = dev->net;
107+
dev->mii.mdio_read = r8153_ecm_mdio_read;
108+
dev->mii.mdio_write = r8153_ecm_mdio_write;
109+
dev->mii.reg_num_mask = 0x1f;
110+
dev->mii.supports_gmii = 1;
111+
112+
return status;
113+
}
114+
115+
static const struct driver_info r8153_info = {
116+
.description = "RTL8153 ECM Device",
117+
.flags = FLAG_ETHER,
118+
.bind = r8153_bind,
119+
.unbind = usbnet_cdc_unbind,
120+
.status = usbnet_cdc_status,
121+
.manage_power = usbnet_manage_power,
122+
};
123+
124+
static const struct usb_device_id products[] = {
125+
{
126+
USB_DEVICE_AND_INTERFACE_INFO(VENDOR_ID_REALTEK, 0x8153, USB_CLASS_COMM,
127+
USB_CDC_SUBCLASS_ETHERNET, USB_CDC_PROTO_NONE),
128+
.driver_info = (unsigned long)&r8153_info,
129+
},
130+
131+
{ }, /* END */
132+
};
133+
MODULE_DEVICE_TABLE(usb, products);
134+
135+
static int rtl8153_ecm_probe(struct usb_interface *intf,
136+
const struct usb_device_id *id)
137+
{
138+
#if IS_REACHABLE(CONFIG_USB_RTL8152)
139+
if (rtl8152_get_version(intf))
140+
return -ENODEV;
141+
#endif
142+
143+
return usbnet_probe(intf, id);
144+
}
145+
146+
static struct usb_driver r8153_ecm_driver = {
147+
.name = "r8153_ecm",
148+
.id_table = products,
149+
.probe = rtl8153_ecm_probe,
150+
.disconnect = usbnet_disconnect,
151+
.suspend = usbnet_suspend,
152+
.resume = usbnet_resume,
153+
.reset_resume = usbnet_resume,
154+
.supports_autosuspend = 1,
155+
.disable_hub_initiated_lpm = 1,
156+
};
157+
158+
module_usb_driver(r8153_ecm_driver);
159+
160+
MODULE_AUTHOR("Hayes Wang");
161+
MODULE_DESCRIPTION("Realtek USB ECM device");
162+
MODULE_LICENSE("GPL");

include/linux/usb/r8152.h

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/* SPDX-License-Identifier: GPL-2.0-only */
2+
/*
3+
* Copyright (c) 2020 Realtek Semiconductor Corp. All rights reserved.
4+
*/
5+
6+
#ifndef __LINUX_R8152_H
7+
#define __LINUX_R8152_H
8+
9+
#define RTL8152_REQT_READ 0xc0
10+
#define RTL8152_REQT_WRITE 0x40
11+
#define RTL8152_REQ_GET_REGS 0x05
12+
#define RTL8152_REQ_SET_REGS 0x05
13+
14+
#define BYTE_EN_DWORD 0xff
15+
#define BYTE_EN_WORD 0x33
16+
#define BYTE_EN_BYTE 0x11
17+
#define BYTE_EN_SIX_BYTES 0x3f
18+
#define BYTE_EN_START_MASK 0x0f
19+
#define BYTE_EN_END_MASK 0xf0
20+
21+
#define MCU_TYPE_PLA 0x0100
22+
#define MCU_TYPE_USB 0x0000
23+
24+
/* Define these values to match your device */
25+
#define VENDOR_ID_REALTEK 0x0bda
26+
#define VENDOR_ID_MICROSOFT 0x045e
27+
#define VENDOR_ID_SAMSUNG 0x04e8
28+
#define VENDOR_ID_LENOVO 0x17ef
29+
#define VENDOR_ID_LINKSYS 0x13b1
30+
#define VENDOR_ID_NVIDIA 0x0955
31+
#define VENDOR_ID_TPLINK 0x2357
32+
33+
#if IS_REACHABLE(CONFIG_USB_RTL8152)
34+
extern u8 rtl8152_get_version(struct usb_interface *intf);
35+
#endif
36+
37+
#endif /* __LINUX_R8152_H */

0 commit comments

Comments
 (0)