1 From 0ef50460f7f053bd2a911ec53e01bfda646a5574 Mon Sep 17 00:00:00 2001
2 From: Hayes Wang <hayeswang@realtek.com>
3 Date: Wed, 4 Nov 2020 10:19:22 +0800
4 Subject: [PATCH] net/usb/r8153_ecm: support ECM mode for RTL8153
6 commit c1aedf015ebdd0232757a66e2daccf1246bd609c upstream.
8 Support ECM mode based on cdc_ether with relative mii functions,
9 when CONFIG_USB_RTL8152 is not set, or the device is not supported
12 Both r8152 and r8153_ecm would check the return value of
13 rtl8152_get_version() in porbe(). If rtl8152_get_version()
14 return none zero value, the r8152 is used for the device
15 with vendor mode. Otherwise, the r8153_ecm is used for the
18 Signed-off-by: Hayes Wang <hayeswang@realtek.com>
19 Link: https://lore.kernel.org/r/1394712342-15778-392-Taiwan-albertk@realtek.com
20 Signed-off-by: Jakub Kicinski <kuba@kernel.org>
22 drivers/net/usb/Makefile | 2 +-
23 drivers/net/usb/r8152.c | 30 +------
24 drivers/net/usb/r8153_ecm.c | 162 ++++++++++++++++++++++++++++++++++++
25 include/linux/usb/r8152.h | 37 ++++++++
26 4 files changed, 204 insertions(+), 27 deletions(-)
27 create mode 100644 drivers/net/usb/r8153_ecm.c
28 create mode 100644 include/linux/usb/r8152.h
30 --- a/drivers/net/usb/Makefile
31 +++ b/drivers/net/usb/Makefile
32 @@ -13,7 +13,7 @@ obj-$(CONFIG_USB_LAN78XX) += lan78xx.o
33 obj-$(CONFIG_USB_NET_AX8817X) += asix.o
34 asix-y := asix_devices.o asix_common.o ax88172a.o
35 obj-$(CONFIG_USB_NET_AX88179_178A) += ax88179_178a.o
36 -obj-$(CONFIG_USB_NET_CDCETHER) += cdc_ether.o
37 +obj-$(CONFIG_USB_NET_CDCETHER) += cdc_ether.o r8153_ecm.o
38 obj-$(CONFIG_USB_NET_CDC_EEM) += cdc_eem.o
39 obj-$(CONFIG_USB_NET_DM9601) += dm9601.o
40 obj-$(CONFIG_USB_NET_SR9700) += sr9700.o
41 --- a/drivers/net/usb/r8152.c
42 +++ b/drivers/net/usb/r8152.c
44 #include <linux/acpi.h>
45 #include <linux/firmware.h>
46 #include <crypto/hash.h>
47 +#include <linux/usb/r8152.h>
49 /* Information for net-next */
50 #define NETNEXT_VERSION "11"
51 @@ -653,18 +654,6 @@ enum rtl_register_content {
53 #define INTR_LINK 0x0004
55 -#define RTL8152_REQT_READ 0xc0
56 -#define RTL8152_REQT_WRITE 0x40
57 -#define RTL8152_REQ_GET_REGS 0x05
58 -#define RTL8152_REQ_SET_REGS 0x05
60 -#define BYTE_EN_DWORD 0xff
61 -#define BYTE_EN_WORD 0x33
62 -#define BYTE_EN_BYTE 0x11
63 -#define BYTE_EN_SIX_BYTES 0x3f
64 -#define BYTE_EN_START_MASK 0x0f
65 -#define BYTE_EN_END_MASK 0xf0
67 #define RTL8153_MAX_PACKET 9216 /* 9K */
68 #define RTL8153_MAX_MTU (RTL8153_MAX_PACKET - VLAN_ETH_HLEN - \
70 @@ -689,21 +678,9 @@ enum rtl8152_flags {
74 -/* Define these values to match your device */
75 -#define VENDOR_ID_REALTEK 0x0bda
76 -#define VENDOR_ID_MICROSOFT 0x045e
77 -#define VENDOR_ID_SAMSUNG 0x04e8
78 -#define VENDOR_ID_LENOVO 0x17ef
79 -#define VENDOR_ID_LINKSYS 0x13b1
80 -#define VENDOR_ID_NVIDIA 0x0955
81 -#define VENDOR_ID_TPLINK 0x2357
83 #define DEVICE_ID_THINKPAD_THUNDERBOLT3_DOCK_GEN2 0x3082
84 #define DEVICE_ID_THINKPAD_USB_C_DOCK_GEN2 0xa387
86 -#define MCU_TYPE_PLA 0x0100
87 -#define MCU_TYPE_USB 0x0000
89 struct tally_counter {
92 @@ -6602,7 +6579,7 @@ static int rtl_fw_init(struct r8152 *tp)
96 -static u8 rtl_get_version(struct usb_interface *intf)
97 +u8 rtl8152_get_version(struct usb_interface *intf)
99 struct usb_device *udev = interface_to_usbdev(intf);
101 @@ -6660,12 +6637,13 @@ static u8 rtl_get_version(struct usb_int
105 +EXPORT_SYMBOL_GPL(rtl8152_get_version);
107 static int rtl8152_probe(struct usb_interface *intf,
108 const struct usb_device_id *id)
110 struct usb_device *udev = interface_to_usbdev(intf);
111 - u8 version = rtl_get_version(intf);
112 + u8 version = rtl8152_get_version(intf);
114 struct net_device *netdev;
117 +++ b/drivers/net/usb/r8153_ecm.c
119 +// SPDX-License-Identifier: GPL-2.0-or-later
120 +#include <linux/module.h>
121 +#include <linux/netdevice.h>
122 +#include <linux/mii.h>
123 +#include <linux/usb.h>
124 +#include <linux/usb/cdc.h>
125 +#include <linux/usb/usbnet.h>
126 +#include <linux/usb/r8152.h>
128 +#define OCP_BASE 0xe86c
130 +static int pla_read_word(struct usbnet *dev, u16 index)
132 + u16 byen = BYTE_EN_WORD;
133 + u8 shift = index & 2;
142 + ret = usbnet_read_cmd(dev, RTL8152_REQ_GET_REGS, RTL8152_REQT_READ, index,
143 + MCU_TYPE_PLA | byen, &tmp, sizeof(tmp));
147 + ret = __le32_to_cpu(tmp);
148 + ret >>= (shift * 8);
155 +static int pla_write_word(struct usbnet *dev, u16 index, u32 data)
158 + u16 byen = BYTE_EN_WORD;
159 + u8 shift = index & 2;
167 + mask <<= (shift * 8);
168 + data <<= (shift * 8);
173 + ret = usbnet_read_cmd(dev, RTL8152_REQ_GET_REGS, RTL8152_REQT_READ, index,
174 + MCU_TYPE_PLA | byen, &tmp, sizeof(tmp));
179 + data |= __le32_to_cpu(tmp) & ~mask;
180 + tmp = __cpu_to_le32(data);
182 + ret = usbnet_write_cmd(dev, RTL8152_REQ_SET_REGS, RTL8152_REQT_WRITE, index,
183 + MCU_TYPE_PLA | byen, &tmp, sizeof(tmp));
189 +static int r8153_ecm_mdio_read(struct net_device *netdev, int phy_id, int reg)
191 + struct usbnet *dev = netdev_priv(netdev);
194 + ret = pla_write_word(dev, OCP_BASE, 0xa000);
198 + ret = pla_read_word(dev, 0xb400 + reg * 2);
204 +static void r8153_ecm_mdio_write(struct net_device *netdev, int phy_id, int reg, int val)
206 + struct usbnet *dev = netdev_priv(netdev);
209 + ret = pla_write_word(dev, OCP_BASE, 0xa000);
213 + ret = pla_write_word(dev, 0xb400 + reg * 2, val);
216 +static int r8153_bind(struct usbnet *dev, struct usb_interface *intf)
220 + status = usbnet_cdc_bind(dev, intf);
224 + dev->mii.dev = dev->net;
225 + dev->mii.mdio_read = r8153_ecm_mdio_read;
226 + dev->mii.mdio_write = r8153_ecm_mdio_write;
227 + dev->mii.reg_num_mask = 0x1f;
228 + dev->mii.supports_gmii = 1;
233 +static const struct driver_info r8153_info = {
234 + .description = "RTL8153 ECM Device",
235 + .flags = FLAG_ETHER,
236 + .bind = r8153_bind,
237 + .unbind = usbnet_cdc_unbind,
238 + .status = usbnet_cdc_status,
239 + .manage_power = usbnet_manage_power,
242 +static const struct usb_device_id products[] = {
244 + USB_DEVICE_AND_INTERFACE_INFO(VENDOR_ID_REALTEK, 0x8153, USB_CLASS_COMM,
245 + USB_CDC_SUBCLASS_ETHERNET, USB_CDC_PROTO_NONE),
246 + .driver_info = (unsigned long)&r8153_info,
251 +MODULE_DEVICE_TABLE(usb, products);
253 +static int rtl8153_ecm_probe(struct usb_interface *intf,
254 + const struct usb_device_id *id)
256 +#if IS_REACHABLE(CONFIG_USB_RTL8152)
257 + if (rtl8152_get_version(intf))
261 + return usbnet_probe(intf, id);
264 +static struct usb_driver r8153_ecm_driver = {
265 + .name = "r8153_ecm",
266 + .id_table = products,
267 + .probe = rtl8153_ecm_probe,
268 + .disconnect = usbnet_disconnect,
269 + .suspend = usbnet_suspend,
270 + .resume = usbnet_resume,
271 + .reset_resume = usbnet_resume,
272 + .supports_autosuspend = 1,
273 + .disable_hub_initiated_lpm = 1,
276 +module_usb_driver(r8153_ecm_driver);
278 +MODULE_AUTHOR("Hayes Wang");
279 +MODULE_DESCRIPTION("Realtek USB ECM device");
280 +MODULE_LICENSE("GPL");
282 +++ b/include/linux/usb/r8152.h
284 +/* SPDX-License-Identifier: GPL-2.0-only */
286 + * Copyright (c) 2020 Realtek Semiconductor Corp. All rights reserved.
289 +#ifndef __LINUX_R8152_H
290 +#define __LINUX_R8152_H
292 +#define RTL8152_REQT_READ 0xc0
293 +#define RTL8152_REQT_WRITE 0x40
294 +#define RTL8152_REQ_GET_REGS 0x05
295 +#define RTL8152_REQ_SET_REGS 0x05
297 +#define BYTE_EN_DWORD 0xff
298 +#define BYTE_EN_WORD 0x33
299 +#define BYTE_EN_BYTE 0x11
300 +#define BYTE_EN_SIX_BYTES 0x3f
301 +#define BYTE_EN_START_MASK 0x0f
302 +#define BYTE_EN_END_MASK 0xf0
304 +#define MCU_TYPE_PLA 0x0100
305 +#define MCU_TYPE_USB 0x0000
307 +/* Define these values to match your device */
308 +#define VENDOR_ID_REALTEK 0x0bda
309 +#define VENDOR_ID_MICROSOFT 0x045e
310 +#define VENDOR_ID_SAMSUNG 0x04e8
311 +#define VENDOR_ID_LENOVO 0x17ef
312 +#define VENDOR_ID_LINKSYS 0x13b1
313 +#define VENDOR_ID_NVIDIA 0x0955
314 +#define VENDOR_ID_TPLINK 0x2357
316 +#if IS_REACHABLE(CONFIG_USB_RTL8152)
317 +extern u8 rtl8152_get_version(struct usb_interface *intf);
320 +#endif /* __LINUX_R8152_H */