38ddcb5714cbe56d595e8caf77051b7d3d687059
[openwrt/staging/wigyori.git] /
1 From ec51fbd1b8a2bca2948dede99c14ec63dc57ff6b Mon Sep 17 00:00:00 2001
2 From: Bjørn Mork <bjorn@mork.no>
3 Date: Fri, 6 Jan 2023 17:07:38 +0100
4 Subject: [PATCH] r8152: add USB device driver for config selection
5 MIME-Version: 1.0
6 Content-Type: text/plain; charset=UTF-8
7 Content-Transfer-Encoding: 8bit
8
9 Subclassing the generic USB device driver to override the
10 default configuration selection regardless of matching interface
11 drivers.
12
13 The r815x family devices expose a vendor specific function which
14 the r8152 interface driver wants to handle. This is the preferred
15 device mode. Additionally one or more USB class functions are
16 usually supported for hosts lacking a vendor specific driver. The
17 choice is USB configuration based, with one alternate function per
18 configuration.
19
20 Example device with both NCM and ECM alternate cfgs:
21
22 T: Bus=02 Lev=01 Prnt=01 Port=00 Cnt=01 Dev#= 4 Spd=5000 MxCh= 0
23 D: Ver= 3.20 Cls=00(>ifc ) Sub=00 Prot=00 MxPS= 9 #Cfgs= 3
24 P: Vendor=0bda ProdID=8156 Rev=31.00
25 S: Manufacturer=Realtek
26 S: Product=USB 10/100/1G/2.5G LAN
27 S: SerialNumber=001000001
28 C:* #Ifs= 1 Cfg#= 1 Atr=a0 MxPwr=256mA
29 I:* If#= 0 Alt= 0 #EPs= 3 Cls=ff(vend.) Sub=ff Prot=00 Driver=r8152
30 E: Ad=81(I) Atr=02(Bulk) MxPS=1024 Ivl=0ms
31 E: Ad=02(O) Atr=02(Bulk) MxPS=1024 Ivl=0ms
32 E: Ad=83(I) Atr=03(Int.) MxPS= 2 Ivl=128ms
33 C: #Ifs= 2 Cfg#= 2 Atr=a0 MxPwr=256mA
34 I: If#= 0 Alt= 0 #EPs= 1 Cls=02(comm.) Sub=0d Prot=00 Driver=
35 E: Ad=83(I) Atr=03(Int.) MxPS= 16 Ivl=128ms
36 I: If#= 1 Alt= 0 #EPs= 0 Cls=0a(data ) Sub=00 Prot=01 Driver=
37 I: If#= 1 Alt= 1 #EPs= 2 Cls=0a(data ) Sub=00 Prot=01 Driver=
38 E: Ad=81(I) Atr=02(Bulk) MxPS=1024 Ivl=0ms
39 E: Ad=02(O) Atr=02(Bulk) MxPS=1024 Ivl=0ms
40 C: #Ifs= 2 Cfg#= 3 Atr=a0 MxPwr=256mA
41 I: If#= 0 Alt= 0 #EPs= 1 Cls=02(comm.) Sub=06 Prot=00 Driver=
42 E: Ad=83(I) Atr=03(Int.) MxPS= 16 Ivl=128ms
43 I: If#= 1 Alt= 0 #EPs= 0 Cls=0a(data ) Sub=00 Prot=00 Driver=
44 I: If#= 1 Alt= 1 #EPs= 2 Cls=0a(data ) Sub=00 Prot=00 Driver=
45 E: Ad=81(I) Atr=02(Bulk) MxPS=1024 Ivl=0ms
46 E: Ad=02(O) Atr=02(Bulk) MxPS=1024 Ivl=0ms
47
48 A problem with this is that Linux will prefer class functions over
49 vendor specific functions. Using the above example, Linux defaults
50 to cfg #2, running the device in a sub-optimal NCM mode.
51
52 Previously we've attempted to work around the problem by
53 blacklisting the devices in the ECM class driver "cdc_ether", and
54 matching on the ECM class function in the vendor specific interface
55 driver. The latter has been used to switch back to the vendor
56 specific configuration when the driver is probed for a class
57 function.
58
59 This workaround has several issues;
60 - class driver blacklists is additional maintanence cruft in an
61 unrelated driver
62 - class driver blacklists prevents users from optionally running
63 the devices in class mode
64 - each device needs double match entries in the vendor driver
65 - the initial probing as a class function slows down device
66 discovery
67
68 Now these issues have become even worse with the introduction of
69 firmware supporting both NCM and ECM, where NCM ends up as the
70 default mode in Linux. To use the same workaround, we now have
71 to blacklist the devices in to two different class drivers and
72 add yet another match entry to the vendor specific driver.
73
74 This patch implements an alternative workaround strategy -
75 independent of the interface drivers. It avoids adding a
76 blacklist to the cdc_ncm driver and will let us remove the
77 existing blacklist from the cdc_ether driver.
78
79 As an additional bonus, removing the blacklists allow users to
80 select one of the other device modes if wanted.
81
82 Signed-off-by: Bjørn Mork <bjorn@mork.no>
83 Signed-off-by: David S. Miller <davem@davemloft.net>
84 ---
85 drivers/net/usb/r8152.c | 113 ++++++++++++++++++++++++++++------------
86 1 file changed, 81 insertions(+), 32 deletions(-)
87
88 --- a/drivers/net/usb/r8152.c
89 +++ b/drivers/net/usb/r8152.c
90 @@ -9625,6 +9625,9 @@ static int rtl8152_probe(struct usb_inte
91 if (version == RTL_VER_UNKNOWN)
92 return -ENODEV;
93
94 + if (intf->cur_altsetting->desc.bInterfaceClass != USB_CLASS_VENDOR_SPEC)
95 + return -ENODEV;
96 +
97 if (!rtl_vendor_mode(intf))
98 return -ENODEV;
99
100 @@ -9834,43 +9837,35 @@ static void rtl8152_disconnect(struct us
101 }
102 }
103
104 -#define REALTEK_USB_DEVICE(vend, prod) { \
105 - USB_DEVICE_INTERFACE_CLASS(vend, prod, USB_CLASS_VENDOR_SPEC), \
106 -}, \
107 -{ \
108 - USB_DEVICE_AND_INTERFACE_INFO(vend, prod, USB_CLASS_COMM, \
109 - USB_CDC_SUBCLASS_ETHERNET, USB_CDC_PROTO_NONE), \
110 -}
111
112 /* table of devices that work with this driver */
113 static const struct usb_device_id rtl8152_table[] = {
114 /* Realtek */
115 - REALTEK_USB_DEVICE(VENDOR_ID_REALTEK, 0x8050),
116 - REALTEK_USB_DEVICE(VENDOR_ID_REALTEK, 0x8053),
117 - REALTEK_USB_DEVICE(VENDOR_ID_REALTEK, 0x8152),
118 - REALTEK_USB_DEVICE(VENDOR_ID_REALTEK, 0x8153),
119 - REALTEK_USB_DEVICE(VENDOR_ID_REALTEK, 0x8155),
120 - REALTEK_USB_DEVICE(VENDOR_ID_REALTEK, 0x8156),
121 + { USB_DEVICE(VENDOR_ID_REALTEK, 0x8050) },
122 + { USB_DEVICE(VENDOR_ID_REALTEK, 0x8053) },
123 + { USB_DEVICE(VENDOR_ID_REALTEK, 0x8152) },
124 + { USB_DEVICE(VENDOR_ID_REALTEK, 0x8153) },
125 + { USB_DEVICE(VENDOR_ID_REALTEK, 0x8155) },
126 + { USB_DEVICE(VENDOR_ID_REALTEK, 0x8156) },
127
128 /* Microsoft */
129 - REALTEK_USB_DEVICE(VENDOR_ID_MICROSOFT, 0x07ab),
130 - REALTEK_USB_DEVICE(VENDOR_ID_MICROSOFT, 0x07c6),
131 - REALTEK_USB_DEVICE(VENDOR_ID_MICROSOFT, 0x0927),
132 - REALTEK_USB_DEVICE(VENDOR_ID_MICROSOFT, 0x0c5e),
133 - REALTEK_USB_DEVICE(VENDOR_ID_SAMSUNG, 0xa101),
134 - REALTEK_USB_DEVICE(VENDOR_ID_LENOVO, 0x304f),
135 - REALTEK_USB_DEVICE(VENDOR_ID_LENOVO, 0x3054),
136 - REALTEK_USB_DEVICE(VENDOR_ID_LENOVO, 0x3062),
137 - REALTEK_USB_DEVICE(VENDOR_ID_LENOVO, 0x3069),
138 - REALTEK_USB_DEVICE(VENDOR_ID_LENOVO, 0x3082),
139 - REALTEK_USB_DEVICE(VENDOR_ID_LENOVO, 0x7205),
140 - REALTEK_USB_DEVICE(VENDOR_ID_LENOVO, 0x720c),
141 - REALTEK_USB_DEVICE(VENDOR_ID_LENOVO, 0x7214),
142 - REALTEK_USB_DEVICE(VENDOR_ID_LENOVO, 0x721e),
143 - REALTEK_USB_DEVICE(VENDOR_ID_LENOVO, 0xa387),
144 - REALTEK_USB_DEVICE(VENDOR_ID_LINKSYS, 0x0041),
145 - REALTEK_USB_DEVICE(VENDOR_ID_NVIDIA, 0x09ff),
146 - REALTEK_USB_DEVICE(VENDOR_ID_TPLINK, 0x0601),
147 + { USB_DEVICE(VENDOR_ID_MICROSOFT, 0x07ab) },
148 + { USB_DEVICE(VENDOR_ID_MICROSOFT, 0x07c6) },
149 + { USB_DEVICE(VENDOR_ID_MICROSOFT, 0x0927) },
150 + { USB_DEVICE(VENDOR_ID_SAMSUNG, 0xa101) },
151 + { USB_DEVICE(VENDOR_ID_LENOVO, 0x304f) },
152 + { USB_DEVICE(VENDOR_ID_LENOVO, 0x3054) },
153 + { USB_DEVICE(VENDOR_ID_LENOVO, 0x3062) },
154 + { USB_DEVICE(VENDOR_ID_LENOVO, 0x3069) },
155 + { USB_DEVICE(VENDOR_ID_LENOVO, 0x3082) },
156 + { USB_DEVICE(VENDOR_ID_LENOVO, 0x7205) },
157 + { USB_DEVICE(VENDOR_ID_LENOVO, 0x720c) },
158 + { USB_DEVICE(VENDOR_ID_LENOVO, 0x7214) },
159 + { USB_DEVICE(VENDOR_ID_LENOVO, 0x721e) },
160 + { USB_DEVICE(VENDOR_ID_LENOVO, 0xa387) },
161 + { USB_DEVICE(VENDOR_ID_LINKSYS, 0x0041) },
162 + { USB_DEVICE(VENDOR_ID_NVIDIA, 0x09ff) },
163 + { USB_DEVICE(VENDOR_ID_TPLINK, 0x0601) },
164 {}
165 };
166
167 @@ -9890,7 +9885,61 @@ static struct usb_driver rtl8152_driver
168 .disable_hub_initiated_lpm = 1,
169 };
170
171 -module_usb_driver(rtl8152_driver);
172 +static int rtl8152_cfgselector_probe(struct usb_device *udev)
173 +{
174 + struct usb_host_config *c;
175 + int i, num_configs;
176 +
177 + /* The vendor mode is not always config #1, so to find it out. */
178 + c = udev->config;
179 + num_configs = udev->descriptor.bNumConfigurations;
180 + for (i = 0; i < num_configs; (i++, c++)) {
181 + struct usb_interface_descriptor *desc = NULL;
182 +
183 + if (!c->desc.bNumInterfaces)
184 + continue;
185 + desc = &c->intf_cache[0]->altsetting->desc;
186 + if (desc->bInterfaceClass == USB_CLASS_VENDOR_SPEC)
187 + break;
188 + }
189 +
190 + if (i == num_configs)
191 + return -ENODEV;
192 +
193 + if (usb_set_configuration(udev, c->desc.bConfigurationValue)) {
194 + dev_err(&udev->dev, "Failed to set configuration %d\n",
195 + c->desc.bConfigurationValue);
196 + return -ENODEV;
197 + }
198 +
199 + return 0;
200 +}
201 +
202 +static struct usb_device_driver rtl8152_cfgselector_driver = {
203 + .name = MODULENAME "-cfgselector",
204 + .probe = rtl8152_cfgselector_probe,
205 + .id_table = rtl8152_table,
206 + .generic_subclass = 1,
207 +};
208 +
209 +static int __init rtl8152_driver_init(void)
210 +{
211 + int ret;
212 +
213 + ret = usb_register_device_driver(&rtl8152_cfgselector_driver, THIS_MODULE);
214 + if (ret)
215 + return ret;
216 + return usb_register(&rtl8152_driver);
217 +}
218 +
219 +static void __exit rtl8152_driver_exit(void)
220 +{
221 + usb_deregister(&rtl8152_driver);
222 + usb_deregister_device_driver(&rtl8152_cfgselector_driver);
223 +}
224 +
225 +module_init(rtl8152_driver_init);
226 +module_exit(rtl8152_driver_exit);
227
228 MODULE_AUTHOR(DRIVER_AUTHOR);
229 MODULE_DESCRIPTION(DRIVER_DESC);