kernel: fix possible mtd NULL pointer dereference
[openwrt/openwrt.git] / target / linux / generic / backport-5.10 / 782-v5.11-net-usb-r8153_ecm-support-ECM-mode-for-RTL8153.patch
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
5
6 commit c1aedf015ebdd0232757a66e2daccf1246bd609c upstream.
7
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
10 by r8152 driver.
11
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
16 device with ECM mode.
17
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>
21 ---
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
29
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
43 @@ -26,6 +26,7 @@
44 #include <linux/acpi.h>
45 #include <linux/firmware.h>
46 #include <crypto/hash.h>
47 +#include <linux/usb/r8152.h>
48
49 /* Information for net-next */
50 #define NETNEXT_VERSION "11"
51 @@ -653,18 +654,6 @@ enum rtl_register_content {
52
53 #define INTR_LINK 0x0004
54
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
59 -
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
66 -
67 #define RTL8153_MAX_PACKET 9216 /* 9K */
68 #define RTL8153_MAX_MTU (RTL8153_MAX_PACKET - VLAN_ETH_HLEN - \
69 ETH_FCS_LEN)
70 @@ -689,21 +678,9 @@ enum rtl8152_flags {
71 LENOVO_MACPASSTHRU,
72 };
73
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
82 -
83 #define DEVICE_ID_THINKPAD_THUNDERBOLT3_DOCK_GEN2 0x3082
84 #define DEVICE_ID_THINKPAD_USB_C_DOCK_GEN2 0xa387
85
86 -#define MCU_TYPE_PLA 0x0100
87 -#define MCU_TYPE_USB 0x0000
88 -
89 struct tally_counter {
90 __le64 tx_packets;
91 __le64 rx_packets;
92 @@ -6602,7 +6579,7 @@ static int rtl_fw_init(struct r8152 *tp)
93 return 0;
94 }
95
96 -static u8 rtl_get_version(struct usb_interface *intf)
97 +u8 rtl8152_get_version(struct usb_interface *intf)
98 {
99 struct usb_device *udev = interface_to_usbdev(intf);
100 u32 ocp_data = 0;
101 @@ -6660,12 +6637,13 @@ static u8 rtl_get_version(struct usb_int
102
103 return version;
104 }
105 +EXPORT_SYMBOL_GPL(rtl8152_get_version);
106
107 static int rtl8152_probe(struct usb_interface *intf,
108 const struct usb_device_id *id)
109 {
110 struct usb_device *udev = interface_to_usbdev(intf);
111 - u8 version = rtl_get_version(intf);
112 + u8 version = rtl8152_get_version(intf);
113 struct r8152 *tp;
114 struct net_device *netdev;
115 int ret;
116 --- /dev/null
117 +++ b/drivers/net/usb/r8153_ecm.c
118 @@ -0,0 +1,162 @@
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>
127 +
128 +#define OCP_BASE 0xe86c
129 +
130 +static int pla_read_word(struct usbnet *dev, u16 index)
131 +{
132 + u16 byen = BYTE_EN_WORD;
133 + u8 shift = index & 2;
134 + __le32 tmp;
135 + int ret;
136 +
137 + if (shift)
138 + byen <<= shift;
139 +
140 + index &= ~3;
141 +
142 + ret = usbnet_read_cmd(dev, RTL8152_REQ_GET_REGS, RTL8152_REQT_READ, index,
143 + MCU_TYPE_PLA | byen, &tmp, sizeof(tmp));
144 + if (ret < 0)
145 + goto out;
146 +
147 + ret = __le32_to_cpu(tmp);
148 + ret >>= (shift * 8);
149 + ret &= 0xffff;
150 +
151 +out:
152 + return ret;
153 +}
154 +
155 +static int pla_write_word(struct usbnet *dev, u16 index, u32 data)
156 +{
157 + u32 mask = 0xffff;
158 + u16 byen = BYTE_EN_WORD;
159 + u8 shift = index & 2;
160 + __le32 tmp;
161 + int ret;
162 +
163 + data &= mask;
164 +
165 + if (shift) {
166 + byen <<= shift;
167 + mask <<= (shift * 8);
168 + data <<= (shift * 8);
169 + }
170 +
171 + index &= ~3;
172 +
173 + ret = usbnet_read_cmd(dev, RTL8152_REQ_GET_REGS, RTL8152_REQT_READ, index,
174 + MCU_TYPE_PLA | byen, &tmp, sizeof(tmp));
175 +
176 + if (ret < 0)
177 + goto out;
178 +
179 + data |= __le32_to_cpu(tmp) & ~mask;
180 + tmp = __cpu_to_le32(data);
181 +
182 + ret = usbnet_write_cmd(dev, RTL8152_REQ_SET_REGS, RTL8152_REQT_WRITE, index,
183 + MCU_TYPE_PLA | byen, &tmp, sizeof(tmp));
184 +
185 +out:
186 + return ret;
187 +}
188 +
189 +static int r8153_ecm_mdio_read(struct net_device *netdev, int phy_id, int reg)
190 +{
191 + struct usbnet *dev = netdev_priv(netdev);
192 + int ret;
193 +
194 + ret = pla_write_word(dev, OCP_BASE, 0xa000);
195 + if (ret < 0)
196 + goto out;
197 +
198 + ret = pla_read_word(dev, 0xb400 + reg * 2);
199 +
200 +out:
201 + return ret;
202 +}
203 +
204 +static void r8153_ecm_mdio_write(struct net_device *netdev, int phy_id, int reg, int val)
205 +{
206 + struct usbnet *dev = netdev_priv(netdev);
207 + int ret;
208 +
209 + ret = pla_write_word(dev, OCP_BASE, 0xa000);
210 + if (ret < 0)
211 + return;
212 +
213 + ret = pla_write_word(dev, 0xb400 + reg * 2, val);
214 +}
215 +
216 +static int r8153_bind(struct usbnet *dev, struct usb_interface *intf)
217 +{
218 + int status;
219 +
220 + status = usbnet_cdc_bind(dev, intf);
221 + if (status < 0)
222 + return status;
223 +
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;
229 +
230 + return status;
231 +}
232 +
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,
240 +};
241 +
242 +static const struct usb_device_id products[] = {
243 +{
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,
247 +},
248 +
249 + { }, /* END */
250 +};
251 +MODULE_DEVICE_TABLE(usb, products);
252 +
253 +static int rtl8153_ecm_probe(struct usb_interface *intf,
254 + const struct usb_device_id *id)
255 +{
256 +#if IS_REACHABLE(CONFIG_USB_RTL8152)
257 + if (rtl8152_get_version(intf))
258 + return -ENODEV;
259 +#endif
260 +
261 + return usbnet_probe(intf, id);
262 +}
263 +
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,
274 +};
275 +
276 +module_usb_driver(r8153_ecm_driver);
277 +
278 +MODULE_AUTHOR("Hayes Wang");
279 +MODULE_DESCRIPTION("Realtek USB ECM device");
280 +MODULE_LICENSE("GPL");
281 --- /dev/null
282 +++ b/include/linux/usb/r8152.h
283 @@ -0,0 +1,37 @@
284 +/* SPDX-License-Identifier: GPL-2.0-only */
285 +/*
286 + * Copyright (c) 2020 Realtek Semiconductor Corp. All rights reserved.
287 + */
288 +
289 +#ifndef __LINUX_R8152_H
290 +#define __LINUX_R8152_H
291 +
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
296 +
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
303 +
304 +#define MCU_TYPE_PLA 0x0100
305 +#define MCU_TYPE_USB 0x0000
306 +
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
315 +
316 +#if IS_REACHABLE(CONFIG_USB_RTL8152)
317 +extern u8 rtl8152_get_version(struct usb_interface *intf);
318 +#endif
319 +
320 +#endif /* __LINUX_R8152_H */