1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 *
4 * Generic Bluetooth USB driver
5 *
6 * Copyright (C) 2005-2008 Marcel Holtmann <marcel@holtmann.org>
7 */
8
9 #include <linux/dmi.h>
10 #include <linux/module.h>
11 #include <linux/usb.h>
12 #include <linux/usb/quirks.h>
13 #include <linux/firmware.h>
14 #include <linux/iopoll.h>
15 #include <linux/of_device.h>
16 #include <linux/of_irq.h>
17 #include <linux/suspend.h>
18 #include <linux/gpio/consumer.h>
19 #include <linux/debugfs.h>
20 #include <asm/unaligned.h>
21
22 #include <net/bluetooth/bluetooth.h>
23 #include <net/bluetooth/hci_core.h>
24
25 #include "btintel.h"
26 #include "btbcm.h"
27 #include "btrtl.h"
28 #include "btmtk.h"
29
30 #define VERSION "0.8"
31
32 static bool disable_scofix;
33 static bool force_scofix;
34 static bool enable_autosuspend = IS_ENABLED(CONFIG_BT_HCIBTUSB_AUTOSUSPEND);
35 static bool enable_poll_sync = IS_ENABLED(CONFIG_BT_HCIBTUSB_POLL_SYNC);
36 static bool reset = true;
37
38 static struct usb_driver btusb_driver;
39
40 #define BTUSB_IGNORE BIT(0)
41 #define BTUSB_DIGIANSWER BIT(1)
42 #define BTUSB_CSR BIT(2)
43 #define BTUSB_SNIFFER BIT(3)
44 #define BTUSB_BCM92035 BIT(4)
45 #define BTUSB_BROKEN_ISOC BIT(5)
46 #define BTUSB_WRONG_SCO_MTU BIT(6)
47 #define BTUSB_ATH3012 BIT(7)
48 #define BTUSB_INTEL_COMBINED BIT(8)
49 #define BTUSB_INTEL_BOOT BIT(9)
50 #define BTUSB_BCM_PATCHRAM BIT(10)
51 #define BTUSB_MARVELL BIT(11)
52 #define BTUSB_SWAVE BIT(12)
53 #define BTUSB_AMP BIT(13)
54 #define BTUSB_QCA_ROME BIT(14)
55 #define BTUSB_BCM_APPLE BIT(15)
56 #define BTUSB_REALTEK BIT(16)
57 #define BTUSB_BCM2045 BIT(17)
58 #define BTUSB_IFNUM_2 BIT(18)
59 #define BTUSB_CW6622 BIT(19)
60 #define BTUSB_MEDIATEK BIT(20)
61 #define BTUSB_WIDEBAND_SPEECH BIT(21)
62 #define BTUSB_VALID_LE_STATES BIT(22)
63 #define BTUSB_QCA_WCN6855 BIT(23)
64 #define BTUSB_INTEL_BROKEN_SHUTDOWN_LED BIT(24)
65 #define BTUSB_INTEL_BROKEN_INITIAL_NCMD BIT(25)
66 #define BTUSB_INTEL_NO_WBS_SUPPORT BIT(26)
67 #define BTUSB_ACTIONS_SEMI BIT(27)
68
69 static const struct usb_device_id btusb_table[] = {
70 /* Generic Bluetooth USB device */
71 { USB_DEVICE_INFO(0xe0, 0x01, 0x01) },
72
73 /* Generic Bluetooth AMP device */
74 { USB_DEVICE_INFO(0xe0, 0x01, 0x04), .driver_info = BTUSB_AMP },
75
76 /* Generic Bluetooth USB interface */
77 { USB_INTERFACE_INFO(0xe0, 0x01, 0x01) },
78
79 /* Apple-specific (Broadcom) devices */
80 { USB_VENDOR_AND_INTERFACE_INFO(0x05ac, 0xff, 0x01, 0x01),
81 .driver_info = BTUSB_BCM_APPLE | BTUSB_IFNUM_2 },
82
83 /* MediaTek MT76x0E */
84 { USB_DEVICE(0x0e8d, 0x763f) },
85
86 /* Broadcom SoftSailing reporting vendor specific */
87 { USB_DEVICE(0x0a5c, 0x21e1) },
88
89 /* Apple MacBookPro 7,1 */
90 { USB_DEVICE(0x05ac, 0x8213) },
91
92 /* Apple iMac11,1 */
93 { USB_DEVICE(0x05ac, 0x8215) },
94
95 /* Apple MacBookPro6,2 */
96 { USB_DEVICE(0x05ac, 0x8218) },
97
98 /* Apple MacBookAir3,1, MacBookAir3,2 */
99 { USB_DEVICE(0x05ac, 0x821b) },
100
101 /* Apple MacBookAir4,1 */
102 { USB_DEVICE(0x05ac, 0x821f) },
103
104 /* Apple MacBookPro8,2 */
105 { USB_DEVICE(0x05ac, 0x821a) },
106
107 /* Apple MacMini5,1 */
108 { USB_DEVICE(0x05ac, 0x8281) },
109
110 /* AVM BlueFRITZ! USB v2.0 */
111 { USB_DEVICE(0x057c, 0x3800), .driver_info = BTUSB_SWAVE },
112
113 /* Bluetooth Ultraport Module from IBM */
114 { USB_DEVICE(0x04bf, 0x030a) },
115
116 /* ALPS Modules with non-standard id */
117 { USB_DEVICE(0x044e, 0x3001) },
118 { USB_DEVICE(0x044e, 0x3002) },
119
120 /* Ericsson with non-standard id */
121 { USB_DEVICE(0x0bdb, 0x1002) },
122
123 /* Canyon CN-BTU1 with HID interfaces */
124 { USB_DEVICE(0x0c10, 0x0000) },
125
126 /* Broadcom BCM20702B0 (Dynex/Insignia) */
127 { USB_DEVICE(0x19ff, 0x0239), .driver_info = BTUSB_BCM_PATCHRAM },
128
129 /* Broadcom BCM43142A0 (Foxconn/Lenovo) */
130 { USB_VENDOR_AND_INTERFACE_INFO(0x105b, 0xff, 0x01, 0x01),
131 .driver_info = BTUSB_BCM_PATCHRAM },
132
133 /* Broadcom BCM920703 (HTC Vive) */
134 { USB_VENDOR_AND_INTERFACE_INFO(0x0bb4, 0xff, 0x01, 0x01),
135 .driver_info = BTUSB_BCM_PATCHRAM },
136
137 /* Foxconn - Hon Hai */
138 { USB_VENDOR_AND_INTERFACE_INFO(0x0489, 0xff, 0x01, 0x01),
139 .driver_info = BTUSB_BCM_PATCHRAM },
140
141 /* Lite-On Technology - Broadcom based */
142 { USB_VENDOR_AND_INTERFACE_INFO(0x04ca, 0xff, 0x01, 0x01),
143 .driver_info = BTUSB_BCM_PATCHRAM },
144
145 /* Broadcom devices with vendor specific id */
146 { USB_VENDOR_AND_INTERFACE_INFO(0x0a5c, 0xff, 0x01, 0x01),
147 .driver_info = BTUSB_BCM_PATCHRAM },
148
149 /* ASUSTek Computer - Broadcom based */
150 { USB_VENDOR_AND_INTERFACE_INFO(0x0b05, 0xff, 0x01, 0x01),
151 .driver_info = BTUSB_BCM_PATCHRAM },
152
153 /* Belkin F8065bf - Broadcom based */
154 { USB_VENDOR_AND_INTERFACE_INFO(0x050d, 0xff, 0x01, 0x01),
155 .driver_info = BTUSB_BCM_PATCHRAM },
156
157 /* IMC Networks - Broadcom based */
158 { USB_VENDOR_AND_INTERFACE_INFO(0x13d3, 0xff, 0x01, 0x01),
159 .driver_info = BTUSB_BCM_PATCHRAM },
160
161 /* Dell Computer - Broadcom based */
162 { USB_VENDOR_AND_INTERFACE_INFO(0x413c, 0xff, 0x01, 0x01),
163 .driver_info = BTUSB_BCM_PATCHRAM },
164
165 /* Toshiba Corp - Broadcom based */
166 { USB_VENDOR_AND_INTERFACE_INFO(0x0930, 0xff, 0x01, 0x01),
167 .driver_info = BTUSB_BCM_PATCHRAM },
168
169 /* Intel Bluetooth USB Bootloader (RAM module) */
170 { USB_DEVICE(0x8087, 0x0a5a),
171 .driver_info = BTUSB_INTEL_BOOT | BTUSB_BROKEN_ISOC },
172
173 { } /* Terminating entry */
174 };
175
176 MODULE_DEVICE_TABLE(usb, btusb_table);
177
178 static const struct usb_device_id quirks_table[] = {
179 /* CSR BlueCore devices */
180 { USB_DEVICE(0x0a12, 0x0001), .driver_info = BTUSB_CSR },
181
182 /* Broadcom BCM2033 without firmware */
183 { USB_DEVICE(0x0a5c, 0x2033), .driver_info = BTUSB_IGNORE },
184
185 /* Broadcom BCM2045 devices */
186 { USB_DEVICE(0x0a5c, 0x2045), .driver_info = BTUSB_BCM2045 },
187
188 /* Atheros 3011 with sflash firmware */
189 { USB_DEVICE(0x0489, 0xe027), .driver_info = BTUSB_IGNORE },
190 { USB_DEVICE(0x0489, 0xe03d), .driver_info = BTUSB_IGNORE },
191 { USB_DEVICE(0x04f2, 0xaff1), .driver_info = BTUSB_IGNORE },
192 { USB_DEVICE(0x0930, 0x0215), .driver_info = BTUSB_IGNORE },
193 { USB_DEVICE(0x0cf3, 0x3002), .driver_info = BTUSB_IGNORE },
194 { USB_DEVICE(0x0cf3, 0xe019), .driver_info = BTUSB_IGNORE },
195 { USB_DEVICE(0x13d3, 0x3304), .driver_info = BTUSB_IGNORE },
196
197 /* Atheros AR9285 Malbec with sflash firmware */
198 { USB_DEVICE(0x03f0, 0x311d), .driver_info = BTUSB_IGNORE },
199
200 /* Atheros 3012 with sflash firmware */
201 { USB_DEVICE(0x0489, 0xe04d), .driver_info = BTUSB_ATH3012 },
202 { USB_DEVICE(0x0489, 0xe04e), .driver_info = BTUSB_ATH3012 },
203 { USB_DEVICE(0x0489, 0xe056), .driver_info = BTUSB_ATH3012 },
204 { USB_DEVICE(0x0489, 0xe057), .driver_info = BTUSB_ATH3012 },
205 { USB_DEVICE(0x0489, 0xe05f), .driver_info = BTUSB_ATH3012 },
206 { USB_DEVICE(0x0489, 0xe076), .driver_info = BTUSB_ATH3012 },
207 { USB_DEVICE(0x0489, 0xe078), .driver_info = BTUSB_ATH3012 },
208 { USB_DEVICE(0x0489, 0xe095), .driver_info = BTUSB_ATH3012 },
209 { USB_DEVICE(0x04c5, 0x1330), .driver_info = BTUSB_ATH3012 },
210 { USB_DEVICE(0x04ca, 0x3004), .driver_info = BTUSB_ATH3012 },
211 { USB_DEVICE(0x04ca, 0x3005), .driver_info = BTUSB_ATH3012 },
212 { USB_DEVICE(0x04ca, 0x3006), .driver_info = BTUSB_ATH3012 },
213 { USB_DEVICE(0x04ca, 0x3007), .driver_info = BTUSB_ATH3012 },
214 { USB_DEVICE(0x04ca, 0x3008), .driver_info = BTUSB_ATH3012 },
215 { USB_DEVICE(0x04ca, 0x300b), .driver_info = BTUSB_ATH3012 },
216 { USB_DEVICE(0x04ca, 0x300d), .driver_info = BTUSB_ATH3012 },
217 { USB_DEVICE(0x04ca, 0x300f), .driver_info = BTUSB_ATH3012 },
218 { USB_DEVICE(0x04ca, 0x3010), .driver_info = BTUSB_ATH3012 },
219 { USB_DEVICE(0x04ca, 0x3014), .driver_info = BTUSB_ATH3012 },
220 { USB_DEVICE(0x04ca, 0x3018), .driver_info = BTUSB_ATH3012 },
221 { USB_DEVICE(0x0930, 0x0219), .driver_info = BTUSB_ATH3012 },
222 { USB_DEVICE(0x0930, 0x021c), .driver_info = BTUSB_ATH3012 },
223 { USB_DEVICE(0x0930, 0x0220), .driver_info = BTUSB_ATH3012 },
224 { USB_DEVICE(0x0930, 0x0227), .driver_info = BTUSB_ATH3012 },
225 { USB_DEVICE(0x0b05, 0x17d0), .driver_info = BTUSB_ATH3012 },
226 { USB_DEVICE(0x0cf3, 0x0036), .driver_info = BTUSB_ATH3012 },
227 { USB_DEVICE(0x0cf3, 0x3004), .driver_info = BTUSB_ATH3012 },
228 { USB_DEVICE(0x0cf3, 0x3008), .driver_info = BTUSB_ATH3012 },
229 { USB_DEVICE(0x0cf3, 0x311d), .driver_info = BTUSB_ATH3012 },
230 { USB_DEVICE(0x0cf3, 0x311e), .driver_info = BTUSB_ATH3012 },
231 { USB_DEVICE(0x0cf3, 0x311f), .driver_info = BTUSB_ATH3012 },
232 { USB_DEVICE(0x0cf3, 0x3121), .driver_info = BTUSB_ATH3012 },
233 { USB_DEVICE(0x0cf3, 0x817a), .driver_info = BTUSB_ATH3012 },
234 { USB_DEVICE(0x0cf3, 0x817b), .driver_info = BTUSB_ATH3012 },
235 { USB_DEVICE(0x0cf3, 0xe003), .driver_info = BTUSB_ATH3012 },
236 { USB_DEVICE(0x0cf3, 0xe004), .driver_info = BTUSB_ATH3012 },
237 { USB_DEVICE(0x0cf3, 0xe005), .driver_info = BTUSB_ATH3012 },
238 { USB_DEVICE(0x0cf3, 0xe006), .driver_info = BTUSB_ATH3012 },
239 { USB_DEVICE(0x13d3, 0x3362), .driver_info = BTUSB_ATH3012 },
240 { USB_DEVICE(0x13d3, 0x3375), .driver_info = BTUSB_ATH3012 },
241 { USB_DEVICE(0x13d3, 0x3393), .driver_info = BTUSB_ATH3012 },
242 { USB_DEVICE(0x13d3, 0x3395), .driver_info = BTUSB_ATH3012 },
243 { USB_DEVICE(0x13d3, 0x3402), .driver_info = BTUSB_ATH3012 },
244 { USB_DEVICE(0x13d3, 0x3408), .driver_info = BTUSB_ATH3012 },
245 { USB_DEVICE(0x13d3, 0x3423), .driver_info = BTUSB_ATH3012 },
246 { USB_DEVICE(0x13d3, 0x3432), .driver_info = BTUSB_ATH3012 },
247 { USB_DEVICE(0x13d3, 0x3472), .driver_info = BTUSB_ATH3012 },
248 { USB_DEVICE(0x13d3, 0x3474), .driver_info = BTUSB_ATH3012 },
249 { USB_DEVICE(0x13d3, 0x3487), .driver_info = BTUSB_ATH3012 },
250 { USB_DEVICE(0x13d3, 0x3490), .driver_info = BTUSB_ATH3012 },
251
252 /* Atheros AR5BBU12 with sflash firmware */
253 { USB_DEVICE(0x0489, 0xe02c), .driver_info = BTUSB_IGNORE },
254
255 /* Atheros AR5BBU12 with sflash firmware */
256 { USB_DEVICE(0x0489, 0xe036), .driver_info = BTUSB_ATH3012 },
257 { USB_DEVICE(0x0489, 0xe03c), .driver_info = BTUSB_ATH3012 },
258
259 /* QCA ROME chipset */
260 { USB_DEVICE(0x0cf3, 0x535b), .driver_info = BTUSB_QCA_ROME |
261 BTUSB_WIDEBAND_SPEECH },
262 { USB_DEVICE(0x0cf3, 0xe007), .driver_info = BTUSB_QCA_ROME |
263 BTUSB_WIDEBAND_SPEECH },
264 { USB_DEVICE(0x0cf3, 0xe009), .driver_info = BTUSB_QCA_ROME |
265 BTUSB_WIDEBAND_SPEECH },
266 { USB_DEVICE(0x0cf3, 0xe010), .driver_info = BTUSB_QCA_ROME |
267 BTUSB_WIDEBAND_SPEECH },
268 { USB_DEVICE(0x0cf3, 0xe300), .driver_info = BTUSB_QCA_ROME |
269 BTUSB_WIDEBAND_SPEECH },
270 { USB_DEVICE(0x0cf3, 0xe301), .driver_info = BTUSB_QCA_ROME |
271 BTUSB_WIDEBAND_SPEECH },
272 { USB_DEVICE(0x0cf3, 0xe360), .driver_info = BTUSB_QCA_ROME |
273 BTUSB_WIDEBAND_SPEECH },
274 { USB_DEVICE(0x0cf3, 0xe500), .driver_info = BTUSB_QCA_ROME |
275 BTUSB_WIDEBAND_SPEECH },
276 { USB_DEVICE(0x0489, 0xe092), .driver_info = BTUSB_QCA_ROME |
277 BTUSB_WIDEBAND_SPEECH },
278 { USB_DEVICE(0x0489, 0xe09f), .driver_info = BTUSB_QCA_ROME |
279 BTUSB_WIDEBAND_SPEECH },
280 { USB_DEVICE(0x0489, 0xe0a2), .driver_info = BTUSB_QCA_ROME |
281 BTUSB_WIDEBAND_SPEECH },
282 { USB_DEVICE(0x04ca, 0x3011), .driver_info = BTUSB_QCA_ROME |
283 BTUSB_WIDEBAND_SPEECH },
284 { USB_DEVICE(0x04ca, 0x3015), .driver_info = BTUSB_QCA_ROME |
285 BTUSB_WIDEBAND_SPEECH },
286 { USB_DEVICE(0x04ca, 0x3016), .driver_info = BTUSB_QCA_ROME |
287 BTUSB_WIDEBAND_SPEECH },
288 { USB_DEVICE(0x04ca, 0x301a), .driver_info = BTUSB_QCA_ROME |
289 BTUSB_WIDEBAND_SPEECH },
290 { USB_DEVICE(0x04ca, 0x3021), .driver_info = BTUSB_QCA_ROME |
291 BTUSB_WIDEBAND_SPEECH },
292 { USB_DEVICE(0x13d3, 0x3491), .driver_info = BTUSB_QCA_ROME |
293 BTUSB_WIDEBAND_SPEECH },
294 { USB_DEVICE(0x13d3, 0x3496), .driver_info = BTUSB_QCA_ROME |
295 BTUSB_WIDEBAND_SPEECH },
296 { USB_DEVICE(0x13d3, 0x3501), .driver_info = BTUSB_QCA_ROME |
297 BTUSB_WIDEBAND_SPEECH },
298
299 /* QCA WCN6855 chipset */
300 { USB_DEVICE(0x0cf3, 0xe600), .driver_info = BTUSB_QCA_WCN6855 |
301 BTUSB_WIDEBAND_SPEECH |
302 BTUSB_VALID_LE_STATES },
303 { USB_DEVICE(0x0489, 0xe0cc), .driver_info = BTUSB_QCA_WCN6855 |
304 BTUSB_WIDEBAND_SPEECH |
305 BTUSB_VALID_LE_STATES },
306 { USB_DEVICE(0x0489, 0xe0d6), .driver_info = BTUSB_QCA_WCN6855 |
307 BTUSB_WIDEBAND_SPEECH |
308 BTUSB_VALID_LE_STATES },
309 { USB_DEVICE(0x0489, 0xe0e3), .driver_info = BTUSB_QCA_WCN6855 |
310 BTUSB_WIDEBAND_SPEECH |
311 BTUSB_VALID_LE_STATES },
312 { USB_DEVICE(0x10ab, 0x9309), .driver_info = BTUSB_QCA_WCN6855 |
313 BTUSB_WIDEBAND_SPEECH |
314 BTUSB_VALID_LE_STATES },
315 { USB_DEVICE(0x10ab, 0x9409), .driver_info = BTUSB_QCA_WCN6855 |
316 BTUSB_WIDEBAND_SPEECH |
317 BTUSB_VALID_LE_STATES },
318 { USB_DEVICE(0x0489, 0xe0d0), .driver_info = BTUSB_QCA_WCN6855 |
319 BTUSB_WIDEBAND_SPEECH |
320 BTUSB_VALID_LE_STATES },
321 { USB_DEVICE(0x10ab, 0x9108), .driver_info = BTUSB_QCA_WCN6855 |
322 BTUSB_WIDEBAND_SPEECH |
323 BTUSB_VALID_LE_STATES },
324 { USB_DEVICE(0x10ab, 0x9109), .driver_info = BTUSB_QCA_WCN6855 |
325 BTUSB_WIDEBAND_SPEECH |
326 BTUSB_VALID_LE_STATES },
327 { USB_DEVICE(0x10ab, 0x9208), .driver_info = BTUSB_QCA_WCN6855 |
328 BTUSB_WIDEBAND_SPEECH |
329 BTUSB_VALID_LE_STATES },
330 { USB_DEVICE(0x10ab, 0x9209), .driver_info = BTUSB_QCA_WCN6855 |
331 BTUSB_WIDEBAND_SPEECH |
332 BTUSB_VALID_LE_STATES },
333 { USB_DEVICE(0x10ab, 0x9308), .driver_info = BTUSB_QCA_WCN6855 |
334 BTUSB_WIDEBAND_SPEECH |
335 BTUSB_VALID_LE_STATES },
336 { USB_DEVICE(0x10ab, 0x9408), .driver_info = BTUSB_QCA_WCN6855 |
337 BTUSB_WIDEBAND_SPEECH |
338 BTUSB_VALID_LE_STATES },
339 { USB_DEVICE(0x10ab, 0x9508), .driver_info = BTUSB_QCA_WCN6855 |
340 BTUSB_WIDEBAND_SPEECH |
341 BTUSB_VALID_LE_STATES },
342 { USB_DEVICE(0x10ab, 0x9509), .driver_info = BTUSB_QCA_WCN6855 |
343 BTUSB_WIDEBAND_SPEECH |
344 BTUSB_VALID_LE_STATES },
345 { USB_DEVICE(0x10ab, 0x9608), .driver_info = BTUSB_QCA_WCN6855 |
346 BTUSB_WIDEBAND_SPEECH |
347 BTUSB_VALID_LE_STATES },
348 { USB_DEVICE(0x10ab, 0x9609), .driver_info = BTUSB_QCA_WCN6855 |
349 BTUSB_WIDEBAND_SPEECH |
350 BTUSB_VALID_LE_STATES },
351 { USB_DEVICE(0x10ab, 0x9f09), .driver_info = BTUSB_QCA_WCN6855 |
352 BTUSB_WIDEBAND_SPEECH |
353 BTUSB_VALID_LE_STATES },
354 { USB_DEVICE(0x04ca, 0x3022), .driver_info = BTUSB_QCA_WCN6855 |
355 BTUSB_WIDEBAND_SPEECH |
356 BTUSB_VALID_LE_STATES },
357 { USB_DEVICE(0x0489, 0xe0c7), .driver_info = BTUSB_QCA_WCN6855 |
358 BTUSB_WIDEBAND_SPEECH |
359 BTUSB_VALID_LE_STATES },
360 { USB_DEVICE(0x0489, 0xe0c9), .driver_info = BTUSB_QCA_WCN6855 |
361 BTUSB_WIDEBAND_SPEECH |
362 BTUSB_VALID_LE_STATES },
363 { USB_DEVICE(0x0489, 0xe0ca), .driver_info = BTUSB_QCA_WCN6855 |
364 BTUSB_WIDEBAND_SPEECH |
365 BTUSB_VALID_LE_STATES },
366 { USB_DEVICE(0x0489, 0xe0cb), .driver_info = BTUSB_QCA_WCN6855 |
367 BTUSB_WIDEBAND_SPEECH |
368 BTUSB_VALID_LE_STATES },
369 { USB_DEVICE(0x0489, 0xe0ce), .driver_info = BTUSB_QCA_WCN6855 |
370 BTUSB_WIDEBAND_SPEECH |
371 BTUSB_VALID_LE_STATES },
372 { USB_DEVICE(0x0489, 0xe0de), .driver_info = BTUSB_QCA_WCN6855 |
373 BTUSB_WIDEBAND_SPEECH |
374 BTUSB_VALID_LE_STATES },
375 { USB_DEVICE(0x0489, 0xe0df), .driver_info = BTUSB_QCA_WCN6855 |
376 BTUSB_WIDEBAND_SPEECH |
377 BTUSB_VALID_LE_STATES },
378 { USB_DEVICE(0x0489, 0xe0e1), .driver_info = BTUSB_QCA_WCN6855 |
379 BTUSB_WIDEBAND_SPEECH |
380 BTUSB_VALID_LE_STATES },
381 { USB_DEVICE(0x0489, 0xe0ea), .driver_info = BTUSB_QCA_WCN6855 |
382 BTUSB_WIDEBAND_SPEECH |
383 BTUSB_VALID_LE_STATES },
384 { USB_DEVICE(0x0489, 0xe0ec), .driver_info = BTUSB_QCA_WCN6855 |
385 BTUSB_WIDEBAND_SPEECH |
386 BTUSB_VALID_LE_STATES },
387 { USB_DEVICE(0x04ca, 0x3023), .driver_info = BTUSB_QCA_WCN6855 |
388 BTUSB_WIDEBAND_SPEECH |
389 BTUSB_VALID_LE_STATES },
390 { USB_DEVICE(0x04ca, 0x3024), .driver_info = BTUSB_QCA_WCN6855 |
391 BTUSB_WIDEBAND_SPEECH |
392 BTUSB_VALID_LE_STATES },
393 { USB_DEVICE(0x04ca, 0x3a22), .driver_info = BTUSB_QCA_WCN6855 |
394 BTUSB_WIDEBAND_SPEECH |
395 BTUSB_VALID_LE_STATES },
396 { USB_DEVICE(0x04ca, 0x3a24), .driver_info = BTUSB_QCA_WCN6855 |
397 BTUSB_WIDEBAND_SPEECH |
398 BTUSB_VALID_LE_STATES },
399 { USB_DEVICE(0x04ca, 0x3a26), .driver_info = BTUSB_QCA_WCN6855 |
400 BTUSB_WIDEBAND_SPEECH |
401 BTUSB_VALID_LE_STATES },
402 { USB_DEVICE(0x04ca, 0x3a27), .driver_info = BTUSB_QCA_WCN6855 |
403 BTUSB_WIDEBAND_SPEECH |
404 BTUSB_VALID_LE_STATES },
405
406 /* QCA WCN785x chipset */
407 { USB_DEVICE(0x0cf3, 0xe700), .driver_info = BTUSB_QCA_WCN6855 |
408 BTUSB_WIDEBAND_SPEECH |
409 BTUSB_VALID_LE_STATES },
410
411 /* Broadcom BCM2035 */
412 { USB_DEVICE(0x0a5c, 0x2009), .driver_info = BTUSB_BCM92035 },
413 { USB_DEVICE(0x0a5c, 0x200a), .driver_info = BTUSB_WRONG_SCO_MTU },
414 { USB_DEVICE(0x0a5c, 0x2035), .driver_info = BTUSB_WRONG_SCO_MTU },
415
416 /* Broadcom BCM2045 */
417 { USB_DEVICE(0x0a5c, 0x2039), .driver_info = BTUSB_WRONG_SCO_MTU },
418 { USB_DEVICE(0x0a5c, 0x2101), .driver_info = BTUSB_WRONG_SCO_MTU },
419
420 /* IBM/Lenovo ThinkPad with Broadcom chip */
421 { USB_DEVICE(0x0a5c, 0x201e), .driver_info = BTUSB_WRONG_SCO_MTU },
422 { USB_DEVICE(0x0a5c, 0x2110), .driver_info = BTUSB_WRONG_SCO_MTU },
423
424 /* HP laptop with Broadcom chip */
425 { USB_DEVICE(0x03f0, 0x171d), .driver_info = BTUSB_WRONG_SCO_MTU },
426
427 /* Dell laptop with Broadcom chip */
428 { USB_DEVICE(0x413c, 0x8126), .driver_info = BTUSB_WRONG_SCO_MTU },
429
430 /* Dell Wireless 370 and 410 devices */
431 { USB_DEVICE(0x413c, 0x8152), .driver_info = BTUSB_WRONG_SCO_MTU },
432 { USB_DEVICE(0x413c, 0x8156), .driver_info = BTUSB_WRONG_SCO_MTU },
433
434 /* Belkin F8T012 and F8T013 devices */
435 { USB_DEVICE(0x050d, 0x0012), .driver_info = BTUSB_WRONG_SCO_MTU },
436 { USB_DEVICE(0x050d, 0x0013), .driver_info = BTUSB_WRONG_SCO_MTU },
437
438 /* Asus WL-BTD202 device */
439 { USB_DEVICE(0x0b05, 0x1715), .driver_info = BTUSB_WRONG_SCO_MTU },
440
441 /* Kensington Bluetooth USB adapter */
442 { USB_DEVICE(0x047d, 0x105e), .driver_info = BTUSB_WRONG_SCO_MTU },
443
444 /* RTX Telecom based adapters with buggy SCO support */
445 { USB_DEVICE(0x0400, 0x0807), .driver_info = BTUSB_BROKEN_ISOC },
446 { USB_DEVICE(0x0400, 0x080a), .driver_info = BTUSB_BROKEN_ISOC },
447
448 /* CONWISE Technology based adapters with buggy SCO support */
449 { USB_DEVICE(0x0e5e, 0x6622),
450 .driver_info = BTUSB_BROKEN_ISOC | BTUSB_CW6622},
451
452 /* Roper Class 1 Bluetooth Dongle (Silicon Wave based) */
453 { USB_DEVICE(0x1310, 0x0001), .driver_info = BTUSB_SWAVE },
454
455 /* Digianswer devices */
456 { USB_DEVICE(0x08fd, 0x0001), .driver_info = BTUSB_DIGIANSWER },
457 { USB_DEVICE(0x08fd, 0x0002), .driver_info = BTUSB_IGNORE },
458
459 /* CSR BlueCore Bluetooth Sniffer */
460 { USB_DEVICE(0x0a12, 0x0002),
461 .driver_info = BTUSB_SNIFFER | BTUSB_BROKEN_ISOC },
462
463 /* Frontline ComProbe Bluetooth Sniffer */
464 { USB_DEVICE(0x16d3, 0x0002),
465 .driver_info = BTUSB_SNIFFER | BTUSB_BROKEN_ISOC },
466
467 /* Marvell Bluetooth devices */
468 { USB_DEVICE(0x1286, 0x2044), .driver_info = BTUSB_MARVELL },
469 { USB_DEVICE(0x1286, 0x2046), .driver_info = BTUSB_MARVELL },
470 { USB_DEVICE(0x1286, 0x204e), .driver_info = BTUSB_MARVELL },
471
472 /* Intel Bluetooth devices */
473 { USB_DEVICE(0x8087, 0x0025), .driver_info = BTUSB_INTEL_COMBINED },
474 { USB_DEVICE(0x8087, 0x0026), .driver_info = BTUSB_INTEL_COMBINED },
475 { USB_DEVICE(0x8087, 0x0029), .driver_info = BTUSB_INTEL_COMBINED },
476 { USB_DEVICE(0x8087, 0x0032), .driver_info = BTUSB_INTEL_COMBINED },
477 { USB_DEVICE(0x8087, 0x0033), .driver_info = BTUSB_INTEL_COMBINED },
478 { USB_DEVICE(0x8087, 0x0035), .driver_info = BTUSB_INTEL_COMBINED },
479 { USB_DEVICE(0x8087, 0x0036), .driver_info = BTUSB_INTEL_COMBINED },
480 { USB_DEVICE(0x8087, 0x07da), .driver_info = BTUSB_CSR },
481 { USB_DEVICE(0x8087, 0x07dc), .driver_info = BTUSB_INTEL_COMBINED |
482 BTUSB_INTEL_NO_WBS_SUPPORT |
483 BTUSB_INTEL_BROKEN_INITIAL_NCMD |
484 BTUSB_INTEL_BROKEN_SHUTDOWN_LED },
485 { USB_DEVICE(0x8087, 0x0a2a), .driver_info = BTUSB_INTEL_COMBINED |
486 BTUSB_INTEL_NO_WBS_SUPPORT |
487 BTUSB_INTEL_BROKEN_SHUTDOWN_LED },
488 { USB_DEVICE(0x8087, 0x0a2b), .driver_info = BTUSB_INTEL_COMBINED },
489 { USB_DEVICE(0x8087, 0x0aa7), .driver_info = BTUSB_INTEL_COMBINED |
490 BTUSB_INTEL_BROKEN_SHUTDOWN_LED },
491 { USB_DEVICE(0x8087, 0x0aaa), .driver_info = BTUSB_INTEL_COMBINED },
492
493 /* Other Intel Bluetooth devices */
494 { USB_VENDOR_AND_INTERFACE_INFO(0x8087, 0xe0, 0x01, 0x01),
495 .driver_info = BTUSB_IGNORE },
496
497 /* Realtek 8821CE Bluetooth devices */
498 { USB_DEVICE(0x13d3, 0x3529), .driver_info = BTUSB_REALTEK |
499 BTUSB_WIDEBAND_SPEECH },
500
501 /* Realtek 8822CE Bluetooth devices */
502 { USB_DEVICE(0x0bda, 0xb00c), .driver_info = BTUSB_REALTEK |
503 BTUSB_WIDEBAND_SPEECH },
504 { USB_DEVICE(0x0bda, 0xc822), .driver_info = BTUSB_REALTEK |
505 BTUSB_WIDEBAND_SPEECH },
506
507 /* Realtek 8822CU Bluetooth devices */
508 { USB_DEVICE(0x13d3, 0x3549), .driver_info = BTUSB_REALTEK |
509 BTUSB_WIDEBAND_SPEECH },
510
511 /* Realtek 8852AE Bluetooth devices */
512 { USB_DEVICE(0x0bda, 0x2852), .driver_info = BTUSB_REALTEK |
513 BTUSB_WIDEBAND_SPEECH },
514 { USB_DEVICE(0x0bda, 0xc852), .driver_info = BTUSB_REALTEK |
515 BTUSB_WIDEBAND_SPEECH },
516 { USB_DEVICE(0x0bda, 0x385a), .driver_info = BTUSB_REALTEK |
517 BTUSB_WIDEBAND_SPEECH },
518 { USB_DEVICE(0x0bda, 0x4852), .driver_info = BTUSB_REALTEK |
519 BTUSB_WIDEBAND_SPEECH },
520 { USB_DEVICE(0x04c5, 0x165c), .driver_info = BTUSB_REALTEK |
521 BTUSB_WIDEBAND_SPEECH },
522 { USB_DEVICE(0x04ca, 0x4006), .driver_info = BTUSB_REALTEK |
523 BTUSB_WIDEBAND_SPEECH },
524 { USB_DEVICE(0x0cb8, 0xc549), .driver_info = BTUSB_REALTEK |
525 BTUSB_WIDEBAND_SPEECH },
526
527 /* Realtek 8852CE Bluetooth devices */
528 { USB_DEVICE(0x04ca, 0x4007), .driver_info = BTUSB_REALTEK |
529 BTUSB_WIDEBAND_SPEECH },
530 { USB_DEVICE(0x04c5, 0x1675), .driver_info = BTUSB_REALTEK |
531 BTUSB_WIDEBAND_SPEECH },
532 { USB_DEVICE(0x0cb8, 0xc558), .driver_info = BTUSB_REALTEK |
533 BTUSB_WIDEBAND_SPEECH },
534 { USB_DEVICE(0x13d3, 0x3587), .driver_info = BTUSB_REALTEK |
535 BTUSB_WIDEBAND_SPEECH },
536 { USB_DEVICE(0x13d3, 0x3586), .driver_info = BTUSB_REALTEK |
537 BTUSB_WIDEBAND_SPEECH },
538 { USB_DEVICE(0x13d3, 0x3592), .driver_info = BTUSB_REALTEK |
539 BTUSB_WIDEBAND_SPEECH },
540 { USB_DEVICE(0x0489, 0xe122), .driver_info = BTUSB_REALTEK |
541 BTUSB_WIDEBAND_SPEECH },
542
543 /* Realtek 8852BE Bluetooth devices */
544 { USB_DEVICE(0x0cb8, 0xc559), .driver_info = BTUSB_REALTEK |
545 BTUSB_WIDEBAND_SPEECH },
546 { USB_DEVICE(0x0bda, 0x4853), .driver_info = BTUSB_REALTEK |
547 BTUSB_WIDEBAND_SPEECH },
548 { USB_DEVICE(0x0bda, 0x887b), .driver_info = BTUSB_REALTEK |
549 BTUSB_WIDEBAND_SPEECH },
550 { USB_DEVICE(0x0bda, 0xb85b), .driver_info = BTUSB_REALTEK |
551 BTUSB_WIDEBAND_SPEECH },
552 { USB_DEVICE(0x13d3, 0x3570), .driver_info = BTUSB_REALTEK |
553 BTUSB_WIDEBAND_SPEECH },
554 { USB_DEVICE(0x13d3, 0x3571), .driver_info = BTUSB_REALTEK |
555 BTUSB_WIDEBAND_SPEECH },
556 { USB_DEVICE(0x13d3, 0x3591), .driver_info = BTUSB_REALTEK |
557 BTUSB_WIDEBAND_SPEECH },
558 { USB_DEVICE(0x0489, 0xe123), .driver_info = BTUSB_REALTEK |
559 BTUSB_WIDEBAND_SPEECH },
560 { USB_DEVICE(0x0489, 0xe125), .driver_info = BTUSB_REALTEK |
561 BTUSB_WIDEBAND_SPEECH },
562
563 /* Realtek Bluetooth devices */
564 { USB_VENDOR_AND_INTERFACE_INFO(0x0bda, 0xe0, 0x01, 0x01),
565 .driver_info = BTUSB_REALTEK },
566
567 /* MediaTek Bluetooth devices */
568 { USB_VENDOR_AND_INTERFACE_INFO(0x0e8d, 0xe0, 0x01, 0x01),
569 .driver_info = BTUSB_MEDIATEK |
570 BTUSB_WIDEBAND_SPEECH |
571 BTUSB_VALID_LE_STATES },
572
573 /* Additional MediaTek MT7615E Bluetooth devices */
574 { USB_DEVICE(0x13d3, 0x3560), .driver_info = BTUSB_MEDIATEK},
575
576 /* Additional MediaTek MT7663 Bluetooth devices */
577 { USB_DEVICE(0x043e, 0x310c), .driver_info = BTUSB_MEDIATEK |
578 BTUSB_WIDEBAND_SPEECH |
579 BTUSB_VALID_LE_STATES },
580 { USB_DEVICE(0x04ca, 0x3801), .driver_info = BTUSB_MEDIATEK |
581 BTUSB_WIDEBAND_SPEECH |
582 BTUSB_VALID_LE_STATES },
583
584 /* Additional MediaTek MT7668 Bluetooth devices */
585 { USB_DEVICE(0x043e, 0x3109), .driver_info = BTUSB_MEDIATEK |
586 BTUSB_WIDEBAND_SPEECH |
587 BTUSB_VALID_LE_STATES },
588
589 /* Additional MediaTek MT7921 Bluetooth devices */
590 { USB_DEVICE(0x0489, 0xe0c8), .driver_info = BTUSB_MEDIATEK |
591 BTUSB_WIDEBAND_SPEECH |
592 BTUSB_VALID_LE_STATES },
593 { USB_DEVICE(0x0489, 0xe0e0), .driver_info = BTUSB_MEDIATEK |
594 BTUSB_WIDEBAND_SPEECH |
595 BTUSB_VALID_LE_STATES },
596 { USB_DEVICE(0x0489, 0xe0f2), .driver_info = BTUSB_MEDIATEK |
597 BTUSB_WIDEBAND_SPEECH |
598 BTUSB_VALID_LE_STATES },
599 { USB_DEVICE(0x04ca, 0x3802), .driver_info = BTUSB_MEDIATEK |
600 BTUSB_WIDEBAND_SPEECH |
601 BTUSB_VALID_LE_STATES },
602 { USB_DEVICE(0x13d3, 0x3563), .driver_info = BTUSB_MEDIATEK |
603 BTUSB_WIDEBAND_SPEECH |
604 BTUSB_VALID_LE_STATES },
605 { USB_DEVICE(0x13d3, 0x3564), .driver_info = BTUSB_MEDIATEK |
606 BTUSB_WIDEBAND_SPEECH |
607 BTUSB_VALID_LE_STATES },
608 { USB_DEVICE(0x13d3, 0x3567), .driver_info = BTUSB_MEDIATEK |
609 BTUSB_WIDEBAND_SPEECH |
610 BTUSB_VALID_LE_STATES },
611 { USB_DEVICE(0x13d3, 0x3578), .driver_info = BTUSB_MEDIATEK |
612 BTUSB_WIDEBAND_SPEECH |
613 BTUSB_VALID_LE_STATES },
614 { USB_DEVICE(0x13d3, 0x3583), .driver_info = BTUSB_MEDIATEK |
615 BTUSB_WIDEBAND_SPEECH |
616 BTUSB_VALID_LE_STATES },
617 { USB_DEVICE(0x0489, 0xe0cd), .driver_info = BTUSB_MEDIATEK |
618 BTUSB_WIDEBAND_SPEECH |
619 BTUSB_VALID_LE_STATES },
620 { USB_DEVICE(0x0e8d, 0x0608), .driver_info = BTUSB_MEDIATEK |
621 BTUSB_WIDEBAND_SPEECH |
622 BTUSB_VALID_LE_STATES },
623
624 /* MediaTek MT7922A Bluetooth devices */
625 { USB_DEVICE(0x0489, 0xe0d8), .driver_info = BTUSB_MEDIATEK |
626 BTUSB_WIDEBAND_SPEECH |
627 BTUSB_VALID_LE_STATES },
628 { USB_DEVICE(0x0489, 0xe0d9), .driver_info = BTUSB_MEDIATEK |
629 BTUSB_WIDEBAND_SPEECH |
630 BTUSB_VALID_LE_STATES },
631 { USB_DEVICE(0x0489, 0xe0f5), .driver_info = BTUSB_MEDIATEK |
632 BTUSB_WIDEBAND_SPEECH |
633 BTUSB_VALID_LE_STATES },
634 { USB_DEVICE(0x13d3, 0x3568), .driver_info = BTUSB_MEDIATEK |
635 BTUSB_WIDEBAND_SPEECH |
636 BTUSB_VALID_LE_STATES },
637 { USB_DEVICE(0x0489, 0xe0e2), .driver_info = BTUSB_MEDIATEK |
638 BTUSB_WIDEBAND_SPEECH |
639 BTUSB_VALID_LE_STATES },
640 { USB_DEVICE(0x0489, 0xe0e4), .driver_info = BTUSB_MEDIATEK |
641 BTUSB_WIDEBAND_SPEECH |
642 BTUSB_VALID_LE_STATES },
643 { USB_DEVICE(0x0489, 0xe0f1), .driver_info = BTUSB_MEDIATEK |
644 BTUSB_WIDEBAND_SPEECH |
645 BTUSB_VALID_LE_STATES },
646 { USB_DEVICE(0x0489, 0xe0f2), .driver_info = BTUSB_MEDIATEK |
647 BTUSB_WIDEBAND_SPEECH |
648 BTUSB_VALID_LE_STATES },
649 { USB_DEVICE(0x0489, 0xe0f5), .driver_info = BTUSB_MEDIATEK |
650 BTUSB_WIDEBAND_SPEECH |
651 BTUSB_VALID_LE_STATES },
652 { USB_DEVICE(0x0489, 0xe0f6), .driver_info = BTUSB_MEDIATEK |
653 BTUSB_WIDEBAND_SPEECH |
654 BTUSB_VALID_LE_STATES },
655 { USB_DEVICE(0x0489, 0xe102), .driver_info = BTUSB_MEDIATEK |
656 BTUSB_WIDEBAND_SPEECH |
657 BTUSB_VALID_LE_STATES },
658 { USB_DEVICE(0x04ca, 0x3804), .driver_info = BTUSB_MEDIATEK |
659 BTUSB_WIDEBAND_SPEECH |
660 BTUSB_VALID_LE_STATES },
661
662 /* Additional Realtek 8723AE Bluetooth devices */
663 { USB_DEVICE(0x0930, 0x021d), .driver_info = BTUSB_REALTEK },
664 { USB_DEVICE(0x13d3, 0x3394), .driver_info = BTUSB_REALTEK },
665
666 /* Additional Realtek 8723BE Bluetooth devices */
667 { USB_DEVICE(0x0489, 0xe085), .driver_info = BTUSB_REALTEK },
668 { USB_DEVICE(0x0489, 0xe08b), .driver_info = BTUSB_REALTEK },
669 { USB_DEVICE(0x04f2, 0xb49f), .driver_info = BTUSB_REALTEK },
670 { USB_DEVICE(0x13d3, 0x3410), .driver_info = BTUSB_REALTEK },
671 { USB_DEVICE(0x13d3, 0x3416), .driver_info = BTUSB_REALTEK },
672 { USB_DEVICE(0x13d3, 0x3459), .driver_info = BTUSB_REALTEK },
673 { USB_DEVICE(0x13d3, 0x3494), .driver_info = BTUSB_REALTEK },
674
675 /* Additional Realtek 8723BU Bluetooth devices */
676 { USB_DEVICE(0x7392, 0xa611), .driver_info = BTUSB_REALTEK },
677
678 /* Additional Realtek 8723DE Bluetooth devices */
679 { USB_DEVICE(0x0bda, 0xb009), .driver_info = BTUSB_REALTEK },
680 { USB_DEVICE(0x2ff8, 0xb011), .driver_info = BTUSB_REALTEK },
681
682 /* Additional Realtek 8761BUV Bluetooth devices */
683 { USB_DEVICE(0x2357, 0x0604), .driver_info = BTUSB_REALTEK |
684 BTUSB_WIDEBAND_SPEECH },
685 { USB_DEVICE(0x0b05, 0x190e), .driver_info = BTUSB_REALTEK |
686 BTUSB_WIDEBAND_SPEECH },
687 { USB_DEVICE(0x2550, 0x8761), .driver_info = BTUSB_REALTEK |
688 BTUSB_WIDEBAND_SPEECH },
689 { USB_DEVICE(0x0bda, 0x8771), .driver_info = BTUSB_REALTEK |
690 BTUSB_WIDEBAND_SPEECH },
691 { USB_DEVICE(0x6655, 0x8771), .driver_info = BTUSB_REALTEK |
692 BTUSB_WIDEBAND_SPEECH },
693 { USB_DEVICE(0x7392, 0xc611), .driver_info = BTUSB_REALTEK |
694 BTUSB_WIDEBAND_SPEECH },
695 { USB_DEVICE(0x2b89, 0x8761), .driver_info = BTUSB_REALTEK |
696 BTUSB_WIDEBAND_SPEECH },
697
698 /* Additional Realtek 8821AE Bluetooth devices */
699 { USB_DEVICE(0x0b05, 0x17dc), .driver_info = BTUSB_REALTEK },
700 { USB_DEVICE(0x13d3, 0x3414), .driver_info = BTUSB_REALTEK },
701 { USB_DEVICE(0x13d3, 0x3458), .driver_info = BTUSB_REALTEK },
702 { USB_DEVICE(0x13d3, 0x3461), .driver_info = BTUSB_REALTEK },
703 { USB_DEVICE(0x13d3, 0x3462), .driver_info = BTUSB_REALTEK },
704
705 /* Additional Realtek 8822BE Bluetooth devices */
706 { USB_DEVICE(0x13d3, 0x3526), .driver_info = BTUSB_REALTEK },
707 { USB_DEVICE(0x0b05, 0x185c), .driver_info = BTUSB_REALTEK },
708
709 /* Additional Realtek 8822CE Bluetooth devices */
710 { USB_DEVICE(0x04ca, 0x4005), .driver_info = BTUSB_REALTEK |
711 BTUSB_WIDEBAND_SPEECH },
712 { USB_DEVICE(0x04c5, 0x161f), .driver_info = BTUSB_REALTEK |
713 BTUSB_WIDEBAND_SPEECH },
714 { USB_DEVICE(0x0b05, 0x18ef), .driver_info = BTUSB_REALTEK |
715 BTUSB_WIDEBAND_SPEECH },
716 { USB_DEVICE(0x13d3, 0x3548), .driver_info = BTUSB_REALTEK |
717 BTUSB_WIDEBAND_SPEECH },
718 { USB_DEVICE(0x13d3, 0x3549), .driver_info = BTUSB_REALTEK |
719 BTUSB_WIDEBAND_SPEECH },
720 { USB_DEVICE(0x13d3, 0x3553), .driver_info = BTUSB_REALTEK |
721 BTUSB_WIDEBAND_SPEECH },
722 { USB_DEVICE(0x13d3, 0x3555), .driver_info = BTUSB_REALTEK |
723 BTUSB_WIDEBAND_SPEECH },
724 { USB_DEVICE(0x2ff8, 0x3051), .driver_info = BTUSB_REALTEK |
725 BTUSB_WIDEBAND_SPEECH },
726 { USB_DEVICE(0x1358, 0xc123), .driver_info = BTUSB_REALTEK |
727 BTUSB_WIDEBAND_SPEECH },
728 { USB_DEVICE(0x0bda, 0xc123), .driver_info = BTUSB_REALTEK |
729 BTUSB_WIDEBAND_SPEECH },
730 { USB_DEVICE(0x0cb5, 0xc547), .driver_info = BTUSB_REALTEK |
731 BTUSB_WIDEBAND_SPEECH },
732
733 /* Actions Semiconductor ATS2851 based devices */
734 { USB_DEVICE(0x10d7, 0xb012), .driver_info = BTUSB_ACTIONS_SEMI },
735
736 /* Silicon Wave based devices */
737 { USB_DEVICE(0x0c10, 0x0000), .driver_info = BTUSB_SWAVE },
738
739 { } /* Terminating entry */
740 };
741
742 /* The Bluetooth USB module build into some devices needs to be reset on resume,
743 * this is a problem with the platform (likely shutting off all power) not with
744 * the module itself. So we use a DMI list to match known broken platforms.
745 */
746 static const struct dmi_system_id btusb_needs_reset_resume_table[] = {
747 {
748 /* Dell OptiPlex 3060 (QCA ROME device 0cf3:e007) */
749 .matches = {
750 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
751 DMI_MATCH(DMI_PRODUCT_NAME, "OptiPlex 3060"),
752 },
753 },
754 {
755 /* Dell XPS 9360 (QCA ROME device 0cf3:e300) */
756 .matches = {
757 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
758 DMI_MATCH(DMI_PRODUCT_NAME, "XPS 13 9360"),
759 },
760 },
761 {
762 /* Dell Inspiron 5565 (QCA ROME device 0cf3:e009) */
763 .matches = {
764 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
765 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 5565"),
766 },
767 },
768 {}
769 };
770
771 struct qca_dump_info {
772 /* fields for dump collection */
773 u16 id_vendor;
774 u16 id_product;
775 u32 fw_version;
776 u32 controller_id;
777 u32 ram_dump_size;
778 u16 ram_dump_seqno;
779 };
780
781 #define BTUSB_MAX_ISOC_FRAMES 10
782
783 #define BTUSB_INTR_RUNNING 0
784 #define BTUSB_BULK_RUNNING 1
785 #define BTUSB_ISOC_RUNNING 2
786 #define BTUSB_SUSPENDING 3
787 #define BTUSB_DID_ISO_RESUME 4
788 #define BTUSB_BOOTLOADER 5
789 #define BTUSB_DOWNLOADING 6
790 #define BTUSB_FIRMWARE_LOADED 7
791 #define BTUSB_FIRMWARE_FAILED 8
792 #define BTUSB_BOOTING 9
793 #define BTUSB_DIAG_RUNNING 10
794 #define BTUSB_OOB_WAKE_ENABLED 11
795 #define BTUSB_HW_RESET_ACTIVE 12
796 #define BTUSB_TX_WAIT_VND_EVT 13
797 #define BTUSB_WAKEUP_AUTOSUSPEND 14
798 #define BTUSB_USE_ALT3_FOR_WBS 15
799 #define BTUSB_ALT6_CONTINUOUS_TX 16
800 #define BTUSB_HW_SSR_ACTIVE 17
801
802 struct btusb_data {
803 struct hci_dev *hdev;
804 struct usb_device *udev;
805 struct usb_interface *intf;
806 struct usb_interface *isoc;
807 struct usb_interface *diag;
808 unsigned isoc_ifnum;
809
810 unsigned long flags;
811
812 bool poll_sync;
813 int intr_interval;
814 struct work_struct work;
815 struct work_struct waker;
816 struct delayed_work rx_work;
817
818 struct sk_buff_head acl_q;
819
820 struct usb_anchor deferred;
821 struct usb_anchor tx_anchor;
822 int tx_in_flight;
823 spinlock_t txlock;
824
825 struct usb_anchor intr_anchor;
826 struct usb_anchor bulk_anchor;
827 struct usb_anchor isoc_anchor;
828 struct usb_anchor diag_anchor;
829 struct usb_anchor ctrl_anchor;
830 spinlock_t rxlock;
831
832 struct sk_buff *evt_skb;
833 struct sk_buff *acl_skb;
834 struct sk_buff *sco_skb;
835
836 struct usb_endpoint_descriptor *intr_ep;
837 struct usb_endpoint_descriptor *bulk_tx_ep;
838 struct usb_endpoint_descriptor *bulk_rx_ep;
839 struct usb_endpoint_descriptor *isoc_tx_ep;
840 struct usb_endpoint_descriptor *isoc_rx_ep;
841 struct usb_endpoint_descriptor *diag_tx_ep;
842 struct usb_endpoint_descriptor *diag_rx_ep;
843
844 struct gpio_desc *reset_gpio;
845
846 __u8 cmdreq_type;
847 __u8 cmdreq;
848
849 unsigned int sco_num;
850 unsigned int air_mode;
851 bool usb_alt6_packet_flow;
852 int isoc_altsetting;
853 int suspend_count;
854
855 int (*recv_event)(struct hci_dev *hdev, struct sk_buff *skb);
856 int (*recv_acl)(struct hci_dev *hdev, struct sk_buff *skb);
857 int (*recv_bulk)(struct btusb_data *data, void *buffer, int count);
858
859 int (*setup_on_usb)(struct hci_dev *hdev);
860
861 int oob_wake_irq; /* irq for out-of-band wake-on-bt */
862 unsigned cmd_timeout_cnt;
863
864 struct qca_dump_info qca_dump;
865 };
866
btusb_reset(struct hci_dev * hdev)867 static void btusb_reset(struct hci_dev *hdev)
868 {
869 struct btusb_data *data;
870 int err;
871
872 if (hdev->reset) {
873 hdev->reset(hdev);
874 return;
875 }
876
877 data = hci_get_drvdata(hdev);
878 /* This is not an unbalanced PM reference since the device will reset */
879 err = usb_autopm_get_interface(data->intf);
880 if (err) {
881 bt_dev_err(hdev, "Failed usb_autopm_get_interface: %d", err);
882 return;
883 }
884
885 bt_dev_err(hdev, "Resetting usb device.");
886 usb_queue_reset_device(data->intf);
887 }
888
btusb_intel_cmd_timeout(struct hci_dev * hdev)889 static void btusb_intel_cmd_timeout(struct hci_dev *hdev)
890 {
891 struct btusb_data *data = hci_get_drvdata(hdev);
892 struct gpio_desc *reset_gpio = data->reset_gpio;
893 struct btintel_data *intel_data = hci_get_priv(hdev);
894
895 if (++data->cmd_timeout_cnt < 5)
896 return;
897
898 if (intel_data->acpi_reset_method) {
899 if (test_and_set_bit(INTEL_ACPI_RESET_ACTIVE, intel_data->flags)) {
900 bt_dev_err(hdev, "acpi: last reset failed ? Not resetting again");
901 return;
902 }
903
904 bt_dev_err(hdev, "Initiating acpi reset method");
905 /* If ACPI reset method fails, lets try with legacy GPIO
906 * toggling
907 */
908 if (!intel_data->acpi_reset_method(hdev)) {
909 return;
910 }
911 }
912
913 if (!reset_gpio) {
914 btusb_reset(hdev);
915 return;
916 }
917
918 /*
919 * Toggle the hard reset line if the platform provides one. The reset
920 * is going to yank the device off the USB and then replug. So doing
921 * once is enough. The cleanup is handled correctly on the way out
922 * (standard USB disconnect), and the new device is detected cleanly
923 * and bound to the driver again like it should be.
924 */
925 if (test_and_set_bit(BTUSB_HW_RESET_ACTIVE, &data->flags)) {
926 bt_dev_err(hdev, "last reset failed? Not resetting again");
927 return;
928 }
929
930 bt_dev_err(hdev, "Initiating HW reset via gpio");
931 gpiod_set_value_cansleep(reset_gpio, 1);
932 msleep(100);
933 gpiod_set_value_cansleep(reset_gpio, 0);
934 }
935
936 #define RTK_DEVCOREDUMP_CODE_MEMDUMP 0x01
937 #define RTK_DEVCOREDUMP_CODE_HW_ERR 0x02
938 #define RTK_DEVCOREDUMP_CODE_CMD_TIMEOUT 0x03
939
940 #define RTK_SUB_EVENT_CODE_COREDUMP 0x34
941
942 struct rtk_dev_coredump_hdr {
943 u8 type;
944 u8 code;
945 u8 reserved[2];
946 } __packed;
947
btusb_rtl_alloc_devcoredump(struct hci_dev * hdev,struct rtk_dev_coredump_hdr * hdr,u8 * buf,u32 len)948 static inline void btusb_rtl_alloc_devcoredump(struct hci_dev *hdev,
949 struct rtk_dev_coredump_hdr *hdr, u8 *buf, u32 len)
950 {
951 struct sk_buff *skb;
952
953 skb = alloc_skb(len + sizeof(*hdr), GFP_ATOMIC);
954 if (!skb)
955 return;
956
957 skb_put_data(skb, hdr, sizeof(*hdr));
958 if (len)
959 skb_put_data(skb, buf, len);
960
961 if (!hci_devcd_init(hdev, skb->len)) {
962 hci_devcd_append(hdev, skb);
963 hci_devcd_complete(hdev);
964 } else {
965 bt_dev_err(hdev, "RTL: Failed to generate devcoredump");
966 kfree_skb(skb);
967 }
968 }
969
btusb_rtl_cmd_timeout(struct hci_dev * hdev)970 static void btusb_rtl_cmd_timeout(struct hci_dev *hdev)
971 {
972 struct btusb_data *data = hci_get_drvdata(hdev);
973 struct gpio_desc *reset_gpio = data->reset_gpio;
974 struct rtk_dev_coredump_hdr hdr = {
975 .type = RTK_DEVCOREDUMP_CODE_CMD_TIMEOUT,
976 };
977
978 btusb_rtl_alloc_devcoredump(hdev, &hdr, NULL, 0);
979
980 if (++data->cmd_timeout_cnt < 5)
981 return;
982
983 if (!reset_gpio) {
984 btusb_reset(hdev);
985 return;
986 }
987
988 /* Toggle the hard reset line. The Realtek device is going to
989 * yank itself off the USB and then replug. The cleanup is handled
990 * correctly on the way out (standard USB disconnect), and the new
991 * device is detected cleanly and bound to the driver again like
992 * it should be.
993 */
994 if (test_and_set_bit(BTUSB_HW_RESET_ACTIVE, &data->flags)) {
995 bt_dev_err(hdev, "last reset failed? Not resetting again");
996 return;
997 }
998
999 bt_dev_err(hdev, "Reset Realtek device via gpio");
1000 gpiod_set_value_cansleep(reset_gpio, 1);
1001 msleep(200);
1002 gpiod_set_value_cansleep(reset_gpio, 0);
1003 }
1004
btusb_rtl_hw_error(struct hci_dev * hdev,u8 code)1005 static void btusb_rtl_hw_error(struct hci_dev *hdev, u8 code)
1006 {
1007 struct rtk_dev_coredump_hdr hdr = {
1008 .type = RTK_DEVCOREDUMP_CODE_HW_ERR,
1009 .code = code,
1010 };
1011
1012 bt_dev_err(hdev, "RTL: hw err, trigger devcoredump (%d)", code);
1013
1014 btusb_rtl_alloc_devcoredump(hdev, &hdr, NULL, 0);
1015 }
1016
btusb_qca_cmd_timeout(struct hci_dev * hdev)1017 static void btusb_qca_cmd_timeout(struct hci_dev *hdev)
1018 {
1019 struct btusb_data *data = hci_get_drvdata(hdev);
1020 struct gpio_desc *reset_gpio = data->reset_gpio;
1021
1022 if (test_bit(BTUSB_HW_SSR_ACTIVE, &data->flags)) {
1023 bt_dev_info(hdev, "Ramdump in progress, defer cmd_timeout");
1024 return;
1025 }
1026
1027 if (++data->cmd_timeout_cnt < 5)
1028 return;
1029
1030 if (reset_gpio) {
1031 bt_dev_err(hdev, "Reset qca device via bt_en gpio");
1032
1033 /* Toggle the hard reset line. The qca bt device is going to
1034 * yank itself off the USB and then replug. The cleanup is handled
1035 * correctly on the way out (standard USB disconnect), and the new
1036 * device is detected cleanly and bound to the driver again like
1037 * it should be.
1038 */
1039 if (test_and_set_bit(BTUSB_HW_RESET_ACTIVE, &data->flags)) {
1040 bt_dev_err(hdev, "last reset failed? Not resetting again");
1041 return;
1042 }
1043
1044 gpiod_set_value_cansleep(reset_gpio, 0);
1045 msleep(200);
1046 gpiod_set_value_cansleep(reset_gpio, 1);
1047
1048 return;
1049 }
1050
1051 btusb_reset(hdev);
1052 }
1053
btusb_free_frags(struct btusb_data * data)1054 static inline void btusb_free_frags(struct btusb_data *data)
1055 {
1056 unsigned long flags;
1057
1058 spin_lock_irqsave(&data->rxlock, flags);
1059
1060 dev_kfree_skb_irq(data->evt_skb);
1061 data->evt_skb = NULL;
1062
1063 dev_kfree_skb_irq(data->acl_skb);
1064 data->acl_skb = NULL;
1065
1066 dev_kfree_skb_irq(data->sco_skb);
1067 data->sco_skb = NULL;
1068
1069 spin_unlock_irqrestore(&data->rxlock, flags);
1070 }
1071
btusb_recv_event(struct btusb_data * data,struct sk_buff * skb)1072 static int btusb_recv_event(struct btusb_data *data, struct sk_buff *skb)
1073 {
1074 if (data->intr_interval) {
1075 /* Trigger dequeue immediatelly if an event is received */
1076 schedule_delayed_work(&data->rx_work, 0);
1077 }
1078
1079 return data->recv_event(data->hdev, skb);
1080 }
1081
btusb_recv_intr(struct btusb_data * data,void * buffer,int count)1082 static int btusb_recv_intr(struct btusb_data *data, void *buffer, int count)
1083 {
1084 struct sk_buff *skb;
1085 unsigned long flags;
1086 int err = 0;
1087
1088 spin_lock_irqsave(&data->rxlock, flags);
1089 skb = data->evt_skb;
1090
1091 while (count) {
1092 int len;
1093
1094 if (!skb) {
1095 skb = bt_skb_alloc(HCI_MAX_EVENT_SIZE, GFP_ATOMIC);
1096 if (!skb) {
1097 err = -ENOMEM;
1098 break;
1099 }
1100
1101 hci_skb_pkt_type(skb) = HCI_EVENT_PKT;
1102 hci_skb_expect(skb) = HCI_EVENT_HDR_SIZE;
1103 }
1104
1105 len = min_t(uint, hci_skb_expect(skb), count);
1106 skb_put_data(skb, buffer, len);
1107
1108 count -= len;
1109 buffer += len;
1110 hci_skb_expect(skb) -= len;
1111
1112 if (skb->len == HCI_EVENT_HDR_SIZE) {
1113 /* Complete event header */
1114 hci_skb_expect(skb) = hci_event_hdr(skb)->plen;
1115
1116 if (skb_tailroom(skb) < hci_skb_expect(skb)) {
1117 kfree_skb(skb);
1118 skb = NULL;
1119
1120 err = -EILSEQ;
1121 break;
1122 }
1123 }
1124
1125 if (!hci_skb_expect(skb)) {
1126 /* Complete frame */
1127 btusb_recv_event(data, skb);
1128 skb = NULL;
1129 }
1130 }
1131
1132 data->evt_skb = skb;
1133 spin_unlock_irqrestore(&data->rxlock, flags);
1134
1135 return err;
1136 }
1137
btusb_recv_acl(struct btusb_data * data,struct sk_buff * skb)1138 static int btusb_recv_acl(struct btusb_data *data, struct sk_buff *skb)
1139 {
1140 /* Only queue ACL packet if intr_interval is set as it means
1141 * force_poll_sync has been enabled.
1142 */
1143 if (!data->intr_interval)
1144 return data->recv_acl(data->hdev, skb);
1145
1146 skb_queue_tail(&data->acl_q, skb);
1147 schedule_delayed_work(&data->rx_work, data->intr_interval);
1148
1149 return 0;
1150 }
1151
btusb_recv_bulk(struct btusb_data * data,void * buffer,int count)1152 static int btusb_recv_bulk(struct btusb_data *data, void *buffer, int count)
1153 {
1154 struct sk_buff *skb;
1155 unsigned long flags;
1156 int err = 0;
1157
1158 spin_lock_irqsave(&data->rxlock, flags);
1159 skb = data->acl_skb;
1160
1161 while (count) {
1162 int len;
1163
1164 if (!skb) {
1165 skb = bt_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC);
1166 if (!skb) {
1167 err = -ENOMEM;
1168 break;
1169 }
1170
1171 hci_skb_pkt_type(skb) = HCI_ACLDATA_PKT;
1172 hci_skb_expect(skb) = HCI_ACL_HDR_SIZE;
1173 }
1174
1175 len = min_t(uint, hci_skb_expect(skb), count);
1176 skb_put_data(skb, buffer, len);
1177
1178 count -= len;
1179 buffer += len;
1180 hci_skb_expect(skb) -= len;
1181
1182 if (skb->len == HCI_ACL_HDR_SIZE) {
1183 __le16 dlen = hci_acl_hdr(skb)->dlen;
1184
1185 /* Complete ACL header */
1186 hci_skb_expect(skb) = __le16_to_cpu(dlen);
1187
1188 if (skb_tailroom(skb) < hci_skb_expect(skb)) {
1189 kfree_skb(skb);
1190 skb = NULL;
1191
1192 err = -EILSEQ;
1193 break;
1194 }
1195 }
1196
1197 if (!hci_skb_expect(skb)) {
1198 /* Complete frame */
1199 btusb_recv_acl(data, skb);
1200 skb = NULL;
1201 }
1202 }
1203
1204 data->acl_skb = skb;
1205 spin_unlock_irqrestore(&data->rxlock, flags);
1206
1207 return err;
1208 }
1209
btusb_validate_sco_handle(struct hci_dev * hdev,struct hci_sco_hdr * hdr)1210 static bool btusb_validate_sco_handle(struct hci_dev *hdev,
1211 struct hci_sco_hdr *hdr)
1212 {
1213 __u16 handle;
1214
1215 if (hci_dev_test_flag(hdev, HCI_USER_CHANNEL))
1216 // Can't validate, userspace controls everything.
1217 return true;
1218
1219 /*
1220 * USB isochronous transfers are not designed to be reliable and may
1221 * lose fragments. When this happens, the next first fragment
1222 * encountered might actually be a continuation fragment.
1223 * Validate the handle to detect it and drop it, or else the upper
1224 * layer will get garbage for a while.
1225 */
1226
1227 handle = hci_handle(__le16_to_cpu(hdr->handle));
1228
1229 switch (hci_conn_lookup_type(hdev, handle)) {
1230 case SCO_LINK:
1231 case ESCO_LINK:
1232 return true;
1233 default:
1234 return false;
1235 }
1236 }
1237
btusb_recv_isoc(struct btusb_data * data,void * buffer,int count)1238 static int btusb_recv_isoc(struct btusb_data *data, void *buffer, int count)
1239 {
1240 struct sk_buff *skb;
1241 unsigned long flags;
1242 int err = 0;
1243
1244 spin_lock_irqsave(&data->rxlock, flags);
1245 skb = data->sco_skb;
1246
1247 while (count) {
1248 int len;
1249
1250 if (!skb) {
1251 skb = bt_skb_alloc(HCI_MAX_SCO_SIZE, GFP_ATOMIC);
1252 if (!skb) {
1253 err = -ENOMEM;
1254 break;
1255 }
1256
1257 hci_skb_pkt_type(skb) = HCI_SCODATA_PKT;
1258 hci_skb_expect(skb) = HCI_SCO_HDR_SIZE;
1259 }
1260
1261 len = min_t(uint, hci_skb_expect(skb), count);
1262 skb_put_data(skb, buffer, len);
1263
1264 count -= len;
1265 buffer += len;
1266 hci_skb_expect(skb) -= len;
1267
1268 if (skb->len == HCI_SCO_HDR_SIZE) {
1269 /* Complete SCO header */
1270 struct hci_sco_hdr *hdr = hci_sco_hdr(skb);
1271
1272 hci_skb_expect(skb) = hdr->dlen;
1273
1274 if (skb_tailroom(skb) < hci_skb_expect(skb) ||
1275 !btusb_validate_sco_handle(data->hdev, hdr)) {
1276 kfree_skb(skb);
1277 skb = NULL;
1278
1279 err = -EILSEQ;
1280 break;
1281 }
1282 }
1283
1284 if (!hci_skb_expect(skb)) {
1285 /* Complete frame */
1286 hci_recv_frame(data->hdev, skb);
1287 skb = NULL;
1288 }
1289 }
1290
1291 data->sco_skb = skb;
1292 spin_unlock_irqrestore(&data->rxlock, flags);
1293
1294 return err;
1295 }
1296
btusb_intr_complete(struct urb * urb)1297 static void btusb_intr_complete(struct urb *urb)
1298 {
1299 struct hci_dev *hdev = urb->context;
1300 struct btusb_data *data = hci_get_drvdata(hdev);
1301 int err;
1302
1303 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
1304 urb->actual_length);
1305
1306 if (!test_bit(HCI_RUNNING, &hdev->flags))
1307 return;
1308
1309 if (urb->status == 0) {
1310 hdev->stat.byte_rx += urb->actual_length;
1311
1312 if (btusb_recv_intr(data, urb->transfer_buffer,
1313 urb->actual_length) < 0) {
1314 bt_dev_err(hdev, "corrupted event packet");
1315 hdev->stat.err_rx++;
1316 }
1317 } else if (urb->status == -ENOENT) {
1318 /* Avoid suspend failed when usb_kill_urb */
1319 return;
1320 }
1321
1322 if (!test_bit(BTUSB_INTR_RUNNING, &data->flags))
1323 return;
1324
1325 usb_mark_last_busy(data->udev);
1326 usb_anchor_urb(urb, &data->intr_anchor);
1327
1328 err = usb_submit_urb(urb, GFP_ATOMIC);
1329 if (err < 0) {
1330 /* -EPERM: urb is being killed;
1331 * -ENODEV: device got disconnected
1332 */
1333 if (err != -EPERM && err != -ENODEV)
1334 bt_dev_err(hdev, "urb %p failed to resubmit (%d)",
1335 urb, -err);
1336 if (err != -EPERM)
1337 hci_cmd_sync_cancel(hdev, -err);
1338 usb_unanchor_urb(urb);
1339 }
1340 }
1341
btusb_submit_intr_urb(struct hci_dev * hdev,gfp_t mem_flags)1342 static int btusb_submit_intr_urb(struct hci_dev *hdev, gfp_t mem_flags)
1343 {
1344 struct btusb_data *data = hci_get_drvdata(hdev);
1345 struct urb *urb;
1346 unsigned char *buf;
1347 unsigned int pipe;
1348 int err, size;
1349
1350 BT_DBG("%s", hdev->name);
1351
1352 if (!data->intr_ep)
1353 return -ENODEV;
1354
1355 urb = usb_alloc_urb(0, mem_flags);
1356 if (!urb)
1357 return -ENOMEM;
1358
1359 if (le16_to_cpu(data->udev->descriptor.idVendor) == 0x0a12 &&
1360 le16_to_cpu(data->udev->descriptor.idProduct) == 0x0001)
1361 /* Fake CSR devices don't seem to support sort-transter */
1362 size = le16_to_cpu(data->intr_ep->wMaxPacketSize);
1363 else
1364 /* Use maximum HCI Event size so the USB stack handles
1365 * ZPL/short-transfer automatically.
1366 */
1367 size = HCI_MAX_EVENT_SIZE;
1368
1369 buf = kmalloc(size, mem_flags);
1370 if (!buf) {
1371 usb_free_urb(urb);
1372 return -ENOMEM;
1373 }
1374
1375 pipe = usb_rcvintpipe(data->udev, data->intr_ep->bEndpointAddress);
1376
1377 usb_fill_int_urb(urb, data->udev, pipe, buf, size,
1378 btusb_intr_complete, hdev, data->intr_ep->bInterval);
1379
1380 urb->transfer_flags |= URB_FREE_BUFFER;
1381
1382 usb_anchor_urb(urb, &data->intr_anchor);
1383
1384 err = usb_submit_urb(urb, mem_flags);
1385 if (err < 0) {
1386 if (err != -EPERM && err != -ENODEV)
1387 bt_dev_err(hdev, "urb %p submission failed (%d)",
1388 urb, -err);
1389 if (err != -EPERM)
1390 hci_cmd_sync_cancel(hdev, -err);
1391 usb_unanchor_urb(urb);
1392 }
1393
1394 /* Only initialize intr_interval if URB poll sync is enabled */
1395 if (!data->poll_sync)
1396 goto done;
1397
1398 /* The units are frames (milliseconds) for full and low speed devices,
1399 * and microframes (1/8 millisecond) for highspeed and SuperSpeed
1400 * devices.
1401 *
1402 * This is done once on open/resume so it shouldn't change even if
1403 * force_poll_sync changes.
1404 */
1405 switch (urb->dev->speed) {
1406 case USB_SPEED_SUPER_PLUS:
1407 case USB_SPEED_SUPER: /* units are 125us */
1408 data->intr_interval = usecs_to_jiffies(urb->interval * 125);
1409 break;
1410 default:
1411 data->intr_interval = msecs_to_jiffies(urb->interval);
1412 break;
1413 }
1414
1415 done:
1416 usb_free_urb(urb);
1417
1418 return err;
1419 }
1420
btusb_bulk_complete(struct urb * urb)1421 static void btusb_bulk_complete(struct urb *urb)
1422 {
1423 struct hci_dev *hdev = urb->context;
1424 struct btusb_data *data = hci_get_drvdata(hdev);
1425 int err;
1426
1427 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
1428 urb->actual_length);
1429
1430 if (!test_bit(HCI_RUNNING, &hdev->flags))
1431 return;
1432
1433 if (urb->status == 0) {
1434 hdev->stat.byte_rx += urb->actual_length;
1435
1436 if (data->recv_bulk(data, urb->transfer_buffer,
1437 urb->actual_length) < 0) {
1438 bt_dev_err(hdev, "corrupted ACL packet");
1439 hdev->stat.err_rx++;
1440 }
1441 } else if (urb->status == -ENOENT) {
1442 /* Avoid suspend failed when usb_kill_urb */
1443 return;
1444 }
1445
1446 if (!test_bit(BTUSB_BULK_RUNNING, &data->flags))
1447 return;
1448
1449 usb_anchor_urb(urb, &data->bulk_anchor);
1450 usb_mark_last_busy(data->udev);
1451
1452 err = usb_submit_urb(urb, GFP_ATOMIC);
1453 if (err < 0) {
1454 /* -EPERM: urb is being killed;
1455 * -ENODEV: device got disconnected
1456 */
1457 if (err != -EPERM && err != -ENODEV)
1458 bt_dev_err(hdev, "urb %p failed to resubmit (%d)",
1459 urb, -err);
1460 usb_unanchor_urb(urb);
1461 }
1462 }
1463
btusb_submit_bulk_urb(struct hci_dev * hdev,gfp_t mem_flags)1464 static int btusb_submit_bulk_urb(struct hci_dev *hdev, gfp_t mem_flags)
1465 {
1466 struct btusb_data *data = hci_get_drvdata(hdev);
1467 struct urb *urb;
1468 unsigned char *buf;
1469 unsigned int pipe;
1470 int err, size = HCI_MAX_FRAME_SIZE;
1471
1472 BT_DBG("%s", hdev->name);
1473
1474 if (!data->bulk_rx_ep)
1475 return -ENODEV;
1476
1477 urb = usb_alloc_urb(0, mem_flags);
1478 if (!urb)
1479 return -ENOMEM;
1480
1481 buf = kmalloc(size, mem_flags);
1482 if (!buf) {
1483 usb_free_urb(urb);
1484 return -ENOMEM;
1485 }
1486
1487 pipe = usb_rcvbulkpipe(data->udev, data->bulk_rx_ep->bEndpointAddress);
1488
1489 usb_fill_bulk_urb(urb, data->udev, pipe, buf, size,
1490 btusb_bulk_complete, hdev);
1491
1492 urb->transfer_flags |= URB_FREE_BUFFER;
1493
1494 usb_mark_last_busy(data->udev);
1495 usb_anchor_urb(urb, &data->bulk_anchor);
1496
1497 err = usb_submit_urb(urb, mem_flags);
1498 if (err < 0) {
1499 if (err != -EPERM && err != -ENODEV)
1500 bt_dev_err(hdev, "urb %p submission failed (%d)",
1501 urb, -err);
1502 usb_unanchor_urb(urb);
1503 }
1504
1505 usb_free_urb(urb);
1506
1507 return err;
1508 }
1509
btusb_isoc_complete(struct urb * urb)1510 static void btusb_isoc_complete(struct urb *urb)
1511 {
1512 struct hci_dev *hdev = urb->context;
1513 struct btusb_data *data = hci_get_drvdata(hdev);
1514 int i, err;
1515
1516 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
1517 urb->actual_length);
1518
1519 if (!test_bit(HCI_RUNNING, &hdev->flags))
1520 return;
1521
1522 if (urb->status == 0) {
1523 for (i = 0; i < urb->number_of_packets; i++) {
1524 unsigned int offset = urb->iso_frame_desc[i].offset;
1525 unsigned int length = urb->iso_frame_desc[i].actual_length;
1526
1527 if (urb->iso_frame_desc[i].status)
1528 continue;
1529
1530 hdev->stat.byte_rx += length;
1531
1532 if (btusb_recv_isoc(data, urb->transfer_buffer + offset,
1533 length) < 0) {
1534 bt_dev_err(hdev, "corrupted SCO packet");
1535 hdev->stat.err_rx++;
1536 }
1537 }
1538 } else if (urb->status == -ENOENT) {
1539 /* Avoid suspend failed when usb_kill_urb */
1540 return;
1541 }
1542
1543 if (!test_bit(BTUSB_ISOC_RUNNING, &data->flags))
1544 return;
1545
1546 usb_anchor_urb(urb, &data->isoc_anchor);
1547
1548 err = usb_submit_urb(urb, GFP_ATOMIC);
1549 if (err < 0) {
1550 /* -EPERM: urb is being killed;
1551 * -ENODEV: device got disconnected
1552 */
1553 if (err != -EPERM && err != -ENODEV)
1554 bt_dev_err(hdev, "urb %p failed to resubmit (%d)",
1555 urb, -err);
1556 usb_unanchor_urb(urb);
1557 }
1558 }
1559
__fill_isoc_descriptor_msbc(struct urb * urb,int len,int mtu,struct btusb_data * data)1560 static inline void __fill_isoc_descriptor_msbc(struct urb *urb, int len,
1561 int mtu, struct btusb_data *data)
1562 {
1563 int i = 0, offset = 0;
1564 unsigned int interval;
1565
1566 BT_DBG("len %d mtu %d", len, mtu);
1567
1568 /* For mSBC ALT 6 settings some chips need to transmit the data
1569 * continuously without the zero length of USB packets.
1570 */
1571 if (test_bit(BTUSB_ALT6_CONTINUOUS_TX, &data->flags))
1572 goto ignore_usb_alt6_packet_flow;
1573
1574 /* For mSBC ALT 6 setting the host will send the packet at continuous
1575 * flow. As per core spec 5, vol 4, part B, table 2.1. For ALT setting
1576 * 6 the HCI PACKET INTERVAL should be 7.5ms for every usb packets.
1577 * To maintain the rate we send 63bytes of usb packets alternatively for
1578 * 7ms and 8ms to maintain the rate as 7.5ms.
1579 */
1580 if (data->usb_alt6_packet_flow) {
1581 interval = 7;
1582 data->usb_alt6_packet_flow = false;
1583 } else {
1584 interval = 6;
1585 data->usb_alt6_packet_flow = true;
1586 }
1587
1588 for (i = 0; i < interval; i++) {
1589 urb->iso_frame_desc[i].offset = offset;
1590 urb->iso_frame_desc[i].length = offset;
1591 }
1592
1593 ignore_usb_alt6_packet_flow:
1594 if (len && i < BTUSB_MAX_ISOC_FRAMES) {
1595 urb->iso_frame_desc[i].offset = offset;
1596 urb->iso_frame_desc[i].length = len;
1597 i++;
1598 }
1599
1600 urb->number_of_packets = i;
1601 }
1602
__fill_isoc_descriptor(struct urb * urb,int len,int mtu)1603 static inline void __fill_isoc_descriptor(struct urb *urb, int len, int mtu)
1604 {
1605 int i, offset = 0;
1606
1607 BT_DBG("len %d mtu %d", len, mtu);
1608
1609 for (i = 0; i < BTUSB_MAX_ISOC_FRAMES && len >= mtu;
1610 i++, offset += mtu, len -= mtu) {
1611 urb->iso_frame_desc[i].offset = offset;
1612 urb->iso_frame_desc[i].length = mtu;
1613 }
1614
1615 if (len && i < BTUSB_MAX_ISOC_FRAMES) {
1616 urb->iso_frame_desc[i].offset = offset;
1617 urb->iso_frame_desc[i].length = len;
1618 i++;
1619 }
1620
1621 urb->number_of_packets = i;
1622 }
1623
btusb_submit_isoc_urb(struct hci_dev * hdev,gfp_t mem_flags)1624 static int btusb_submit_isoc_urb(struct hci_dev *hdev, gfp_t mem_flags)
1625 {
1626 struct btusb_data *data = hci_get_drvdata(hdev);
1627 struct urb *urb;
1628 unsigned char *buf;
1629 unsigned int pipe;
1630 int err, size;
1631
1632 BT_DBG("%s", hdev->name);
1633
1634 if (!data->isoc_rx_ep)
1635 return -ENODEV;
1636
1637 urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, mem_flags);
1638 if (!urb)
1639 return -ENOMEM;
1640
1641 size = le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize) *
1642 BTUSB_MAX_ISOC_FRAMES;
1643
1644 buf = kmalloc(size, mem_flags);
1645 if (!buf) {
1646 usb_free_urb(urb);
1647 return -ENOMEM;
1648 }
1649
1650 pipe = usb_rcvisocpipe(data->udev, data->isoc_rx_ep->bEndpointAddress);
1651
1652 usb_fill_int_urb(urb, data->udev, pipe, buf, size, btusb_isoc_complete,
1653 hdev, data->isoc_rx_ep->bInterval);
1654
1655 urb->transfer_flags = URB_FREE_BUFFER | URB_ISO_ASAP;
1656
1657 __fill_isoc_descriptor(urb, size,
1658 le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize));
1659
1660 usb_anchor_urb(urb, &data->isoc_anchor);
1661
1662 err = usb_submit_urb(urb, mem_flags);
1663 if (err < 0) {
1664 if (err != -EPERM && err != -ENODEV)
1665 bt_dev_err(hdev, "urb %p submission failed (%d)",
1666 urb, -err);
1667 usb_unanchor_urb(urb);
1668 }
1669
1670 usb_free_urb(urb);
1671
1672 return err;
1673 }
1674
btusb_diag_complete(struct urb * urb)1675 static void btusb_diag_complete(struct urb *urb)
1676 {
1677 struct hci_dev *hdev = urb->context;
1678 struct btusb_data *data = hci_get_drvdata(hdev);
1679 int err;
1680
1681 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
1682 urb->actual_length);
1683
1684 if (urb->status == 0) {
1685 struct sk_buff *skb;
1686
1687 skb = bt_skb_alloc(urb->actual_length, GFP_ATOMIC);
1688 if (skb) {
1689 skb_put_data(skb, urb->transfer_buffer,
1690 urb->actual_length);
1691 hci_recv_diag(hdev, skb);
1692 }
1693 } else if (urb->status == -ENOENT) {
1694 /* Avoid suspend failed when usb_kill_urb */
1695 return;
1696 }
1697
1698 if (!test_bit(BTUSB_DIAG_RUNNING, &data->flags))
1699 return;
1700
1701 usb_anchor_urb(urb, &data->diag_anchor);
1702 usb_mark_last_busy(data->udev);
1703
1704 err = usb_submit_urb(urb, GFP_ATOMIC);
1705 if (err < 0) {
1706 /* -EPERM: urb is being killed;
1707 * -ENODEV: device got disconnected
1708 */
1709 if (err != -EPERM && err != -ENODEV)
1710 bt_dev_err(hdev, "urb %p failed to resubmit (%d)",
1711 urb, -err);
1712 usb_unanchor_urb(urb);
1713 }
1714 }
1715
btusb_submit_diag_urb(struct hci_dev * hdev,gfp_t mem_flags)1716 static int btusb_submit_diag_urb(struct hci_dev *hdev, gfp_t mem_flags)
1717 {
1718 struct btusb_data *data = hci_get_drvdata(hdev);
1719 struct urb *urb;
1720 unsigned char *buf;
1721 unsigned int pipe;
1722 int err, size = HCI_MAX_FRAME_SIZE;
1723
1724 BT_DBG("%s", hdev->name);
1725
1726 if (!data->diag_rx_ep)
1727 return -ENODEV;
1728
1729 urb = usb_alloc_urb(0, mem_flags);
1730 if (!urb)
1731 return -ENOMEM;
1732
1733 buf = kmalloc(size, mem_flags);
1734 if (!buf) {
1735 usb_free_urb(urb);
1736 return -ENOMEM;
1737 }
1738
1739 pipe = usb_rcvbulkpipe(data->udev, data->diag_rx_ep->bEndpointAddress);
1740
1741 usb_fill_bulk_urb(urb, data->udev, pipe, buf, size,
1742 btusb_diag_complete, hdev);
1743
1744 urb->transfer_flags |= URB_FREE_BUFFER;
1745
1746 usb_mark_last_busy(data->udev);
1747 usb_anchor_urb(urb, &data->diag_anchor);
1748
1749 err = usb_submit_urb(urb, mem_flags);
1750 if (err < 0) {
1751 if (err != -EPERM && err != -ENODEV)
1752 bt_dev_err(hdev, "urb %p submission failed (%d)",
1753 urb, -err);
1754 usb_unanchor_urb(urb);
1755 }
1756
1757 usb_free_urb(urb);
1758
1759 return err;
1760 }
1761
btusb_tx_complete(struct urb * urb)1762 static void btusb_tx_complete(struct urb *urb)
1763 {
1764 struct sk_buff *skb = urb->context;
1765 struct hci_dev *hdev = (struct hci_dev *)skb->dev;
1766 struct btusb_data *data = hci_get_drvdata(hdev);
1767 unsigned long flags;
1768
1769 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
1770 urb->actual_length);
1771
1772 if (!test_bit(HCI_RUNNING, &hdev->flags))
1773 goto done;
1774
1775 if (!urb->status) {
1776 hdev->stat.byte_tx += urb->transfer_buffer_length;
1777 } else {
1778 if (hci_skb_pkt_type(skb) == HCI_COMMAND_PKT)
1779 hci_cmd_sync_cancel(hdev, -urb->status);
1780 hdev->stat.err_tx++;
1781 }
1782
1783 done:
1784 spin_lock_irqsave(&data->txlock, flags);
1785 data->tx_in_flight--;
1786 spin_unlock_irqrestore(&data->txlock, flags);
1787
1788 kfree(urb->setup_packet);
1789
1790 kfree_skb(skb);
1791 }
1792
btusb_isoc_tx_complete(struct urb * urb)1793 static void btusb_isoc_tx_complete(struct urb *urb)
1794 {
1795 struct sk_buff *skb = urb->context;
1796 struct hci_dev *hdev = (struct hci_dev *)skb->dev;
1797
1798 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
1799 urb->actual_length);
1800
1801 if (!test_bit(HCI_RUNNING, &hdev->flags))
1802 goto done;
1803
1804 if (!urb->status)
1805 hdev->stat.byte_tx += urb->transfer_buffer_length;
1806 else
1807 hdev->stat.err_tx++;
1808
1809 done:
1810 kfree(urb->setup_packet);
1811
1812 kfree_skb(skb);
1813 }
1814
btusb_open(struct hci_dev * hdev)1815 static int btusb_open(struct hci_dev *hdev)
1816 {
1817 struct btusb_data *data = hci_get_drvdata(hdev);
1818 int err;
1819
1820 BT_DBG("%s", hdev->name);
1821
1822 err = usb_autopm_get_interface(data->intf);
1823 if (err < 0)
1824 return err;
1825
1826 /* Patching USB firmware files prior to starting any URBs of HCI path
1827 * It is more safe to use USB bulk channel for downloading USB patch
1828 */
1829 if (data->setup_on_usb) {
1830 err = data->setup_on_usb(hdev);
1831 if (err < 0)
1832 goto setup_fail;
1833 }
1834
1835 data->intf->needs_remote_wakeup = 1;
1836
1837 if (test_and_set_bit(BTUSB_INTR_RUNNING, &data->flags))
1838 goto done;
1839
1840 err = btusb_submit_intr_urb(hdev, GFP_KERNEL);
1841 if (err < 0)
1842 goto failed;
1843
1844 err = btusb_submit_bulk_urb(hdev, GFP_KERNEL);
1845 if (err < 0) {
1846 usb_kill_anchored_urbs(&data->intr_anchor);
1847 goto failed;
1848 }
1849
1850 set_bit(BTUSB_BULK_RUNNING, &data->flags);
1851 btusb_submit_bulk_urb(hdev, GFP_KERNEL);
1852
1853 if (data->diag) {
1854 if (!btusb_submit_diag_urb(hdev, GFP_KERNEL))
1855 set_bit(BTUSB_DIAG_RUNNING, &data->flags);
1856 }
1857
1858 done:
1859 usb_autopm_put_interface(data->intf);
1860 return 0;
1861
1862 failed:
1863 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
1864 setup_fail:
1865 usb_autopm_put_interface(data->intf);
1866 return err;
1867 }
1868
btusb_stop_traffic(struct btusb_data * data)1869 static void btusb_stop_traffic(struct btusb_data *data)
1870 {
1871 usb_kill_anchored_urbs(&data->intr_anchor);
1872 usb_kill_anchored_urbs(&data->bulk_anchor);
1873 usb_kill_anchored_urbs(&data->isoc_anchor);
1874 usb_kill_anchored_urbs(&data->diag_anchor);
1875 usb_kill_anchored_urbs(&data->ctrl_anchor);
1876 }
1877
btusb_close(struct hci_dev * hdev)1878 static int btusb_close(struct hci_dev *hdev)
1879 {
1880 struct btusb_data *data = hci_get_drvdata(hdev);
1881 int err;
1882
1883 BT_DBG("%s", hdev->name);
1884
1885 cancel_delayed_work(&data->rx_work);
1886 cancel_work_sync(&data->work);
1887 cancel_work_sync(&data->waker);
1888
1889 skb_queue_purge(&data->acl_q);
1890
1891 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1892 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
1893 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
1894 clear_bit(BTUSB_DIAG_RUNNING, &data->flags);
1895
1896 btusb_stop_traffic(data);
1897 btusb_free_frags(data);
1898
1899 err = usb_autopm_get_interface(data->intf);
1900 if (err < 0)
1901 goto failed;
1902
1903 data->intf->needs_remote_wakeup = 0;
1904
1905 /* Enable remote wake up for auto-suspend */
1906 if (test_bit(BTUSB_WAKEUP_AUTOSUSPEND, &data->flags))
1907 data->intf->needs_remote_wakeup = 1;
1908
1909 usb_autopm_put_interface(data->intf);
1910
1911 failed:
1912 usb_scuttle_anchored_urbs(&data->deferred);
1913 return 0;
1914 }
1915
btusb_flush(struct hci_dev * hdev)1916 static int btusb_flush(struct hci_dev *hdev)
1917 {
1918 struct btusb_data *data = hci_get_drvdata(hdev);
1919
1920 BT_DBG("%s", hdev->name);
1921
1922 cancel_delayed_work(&data->rx_work);
1923
1924 skb_queue_purge(&data->acl_q);
1925
1926 usb_kill_anchored_urbs(&data->tx_anchor);
1927 btusb_free_frags(data);
1928
1929 return 0;
1930 }
1931
alloc_ctrl_urb(struct hci_dev * hdev,struct sk_buff * skb)1932 static struct urb *alloc_ctrl_urb(struct hci_dev *hdev, struct sk_buff *skb)
1933 {
1934 struct btusb_data *data = hci_get_drvdata(hdev);
1935 struct usb_ctrlrequest *dr;
1936 struct urb *urb;
1937 unsigned int pipe;
1938
1939 urb = usb_alloc_urb(0, GFP_KERNEL);
1940 if (!urb)
1941 return ERR_PTR(-ENOMEM);
1942
1943 dr = kmalloc(sizeof(*dr), GFP_KERNEL);
1944 if (!dr) {
1945 usb_free_urb(urb);
1946 return ERR_PTR(-ENOMEM);
1947 }
1948
1949 dr->bRequestType = data->cmdreq_type;
1950 dr->bRequest = data->cmdreq;
1951 dr->wIndex = 0;
1952 dr->wValue = 0;
1953 dr->wLength = __cpu_to_le16(skb->len);
1954
1955 pipe = usb_sndctrlpipe(data->udev, 0x00);
1956
1957 usb_fill_control_urb(urb, data->udev, pipe, (void *)dr,
1958 skb->data, skb->len, btusb_tx_complete, skb);
1959
1960 skb->dev = (void *)hdev;
1961
1962 return urb;
1963 }
1964
alloc_bulk_urb(struct hci_dev * hdev,struct sk_buff * skb)1965 static struct urb *alloc_bulk_urb(struct hci_dev *hdev, struct sk_buff *skb)
1966 {
1967 struct btusb_data *data = hci_get_drvdata(hdev);
1968 struct urb *urb;
1969 unsigned int pipe;
1970
1971 if (!data->bulk_tx_ep)
1972 return ERR_PTR(-ENODEV);
1973
1974 urb = usb_alloc_urb(0, GFP_KERNEL);
1975 if (!urb)
1976 return ERR_PTR(-ENOMEM);
1977
1978 pipe = usb_sndbulkpipe(data->udev, data->bulk_tx_ep->bEndpointAddress);
1979
1980 usb_fill_bulk_urb(urb, data->udev, pipe,
1981 skb->data, skb->len, btusb_tx_complete, skb);
1982
1983 skb->dev = (void *)hdev;
1984
1985 return urb;
1986 }
1987
alloc_isoc_urb(struct hci_dev * hdev,struct sk_buff * skb)1988 static struct urb *alloc_isoc_urb(struct hci_dev *hdev, struct sk_buff *skb)
1989 {
1990 struct btusb_data *data = hci_get_drvdata(hdev);
1991 struct urb *urb;
1992 unsigned int pipe;
1993
1994 if (!data->isoc_tx_ep)
1995 return ERR_PTR(-ENODEV);
1996
1997 urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, GFP_KERNEL);
1998 if (!urb)
1999 return ERR_PTR(-ENOMEM);
2000
2001 pipe = usb_sndisocpipe(data->udev, data->isoc_tx_ep->bEndpointAddress);
2002
2003 usb_fill_int_urb(urb, data->udev, pipe,
2004 skb->data, skb->len, btusb_isoc_tx_complete,
2005 skb, data->isoc_tx_ep->bInterval);
2006
2007 urb->transfer_flags = URB_ISO_ASAP;
2008
2009 if (data->isoc_altsetting == 6)
2010 __fill_isoc_descriptor_msbc(urb, skb->len,
2011 le16_to_cpu(data->isoc_tx_ep->wMaxPacketSize),
2012 data);
2013 else
2014 __fill_isoc_descriptor(urb, skb->len,
2015 le16_to_cpu(data->isoc_tx_ep->wMaxPacketSize));
2016 skb->dev = (void *)hdev;
2017
2018 return urb;
2019 }
2020
submit_tx_urb(struct hci_dev * hdev,struct urb * urb)2021 static int submit_tx_urb(struct hci_dev *hdev, struct urb *urb)
2022 {
2023 struct btusb_data *data = hci_get_drvdata(hdev);
2024 int err;
2025
2026 usb_anchor_urb(urb, &data->tx_anchor);
2027
2028 err = usb_submit_urb(urb, GFP_KERNEL);
2029 if (err < 0) {
2030 if (err != -EPERM && err != -ENODEV)
2031 bt_dev_err(hdev, "urb %p submission failed (%d)",
2032 urb, -err);
2033 kfree(urb->setup_packet);
2034 usb_unanchor_urb(urb);
2035 } else {
2036 usb_mark_last_busy(data->udev);
2037 }
2038
2039 usb_free_urb(urb);
2040 return err;
2041 }
2042
submit_or_queue_tx_urb(struct hci_dev * hdev,struct urb * urb)2043 static int submit_or_queue_tx_urb(struct hci_dev *hdev, struct urb *urb)
2044 {
2045 struct btusb_data *data = hci_get_drvdata(hdev);
2046 unsigned long flags;
2047 bool suspending;
2048
2049 spin_lock_irqsave(&data->txlock, flags);
2050 suspending = test_bit(BTUSB_SUSPENDING, &data->flags);
2051 if (!suspending)
2052 data->tx_in_flight++;
2053 spin_unlock_irqrestore(&data->txlock, flags);
2054
2055 if (!suspending)
2056 return submit_tx_urb(hdev, urb);
2057
2058 usb_anchor_urb(urb, &data->deferred);
2059 schedule_work(&data->waker);
2060
2061 usb_free_urb(urb);
2062 return 0;
2063 }
2064
btusb_send_frame(struct hci_dev * hdev,struct sk_buff * skb)2065 static int btusb_send_frame(struct hci_dev *hdev, struct sk_buff *skb)
2066 {
2067 struct urb *urb;
2068
2069 BT_DBG("%s", hdev->name);
2070
2071 switch (hci_skb_pkt_type(skb)) {
2072 case HCI_COMMAND_PKT:
2073 urb = alloc_ctrl_urb(hdev, skb);
2074 if (IS_ERR(urb))
2075 return PTR_ERR(urb);
2076
2077 hdev->stat.cmd_tx++;
2078 return submit_or_queue_tx_urb(hdev, urb);
2079
2080 case HCI_ACLDATA_PKT:
2081 urb = alloc_bulk_urb(hdev, skb);
2082 if (IS_ERR(urb))
2083 return PTR_ERR(urb);
2084
2085 hdev->stat.acl_tx++;
2086 return submit_or_queue_tx_urb(hdev, urb);
2087
2088 case HCI_SCODATA_PKT:
2089 if (hci_conn_num(hdev, SCO_LINK) < 1)
2090 return -ENODEV;
2091
2092 urb = alloc_isoc_urb(hdev, skb);
2093 if (IS_ERR(urb))
2094 return PTR_ERR(urb);
2095
2096 hdev->stat.sco_tx++;
2097 return submit_tx_urb(hdev, urb);
2098
2099 case HCI_ISODATA_PKT:
2100 urb = alloc_bulk_urb(hdev, skb);
2101 if (IS_ERR(urb))
2102 return PTR_ERR(urb);
2103
2104 return submit_or_queue_tx_urb(hdev, urb);
2105 }
2106
2107 return -EILSEQ;
2108 }
2109
btusb_notify(struct hci_dev * hdev,unsigned int evt)2110 static void btusb_notify(struct hci_dev *hdev, unsigned int evt)
2111 {
2112 struct btusb_data *data = hci_get_drvdata(hdev);
2113
2114 BT_DBG("%s evt %d", hdev->name, evt);
2115
2116 if (hci_conn_num(hdev, SCO_LINK) != data->sco_num) {
2117 data->sco_num = hci_conn_num(hdev, SCO_LINK);
2118 data->air_mode = evt;
2119 schedule_work(&data->work);
2120 }
2121 }
2122
__set_isoc_interface(struct hci_dev * hdev,int altsetting)2123 static inline int __set_isoc_interface(struct hci_dev *hdev, int altsetting)
2124 {
2125 struct btusb_data *data = hci_get_drvdata(hdev);
2126 struct usb_interface *intf = data->isoc;
2127 struct usb_endpoint_descriptor *ep_desc;
2128 int i, err;
2129
2130 if (!data->isoc)
2131 return -ENODEV;
2132
2133 err = usb_set_interface(data->udev, data->isoc_ifnum, altsetting);
2134 if (err < 0) {
2135 bt_dev_err(hdev, "setting interface failed (%d)", -err);
2136 return err;
2137 }
2138
2139 data->isoc_altsetting = altsetting;
2140
2141 data->isoc_tx_ep = NULL;
2142 data->isoc_rx_ep = NULL;
2143
2144 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
2145 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
2146
2147 if (!data->isoc_tx_ep && usb_endpoint_is_isoc_out(ep_desc)) {
2148 data->isoc_tx_ep = ep_desc;
2149 continue;
2150 }
2151
2152 if (!data->isoc_rx_ep && usb_endpoint_is_isoc_in(ep_desc)) {
2153 data->isoc_rx_ep = ep_desc;
2154 continue;
2155 }
2156 }
2157
2158 if (!data->isoc_tx_ep || !data->isoc_rx_ep) {
2159 bt_dev_err(hdev, "invalid SCO descriptors");
2160 return -ENODEV;
2161 }
2162
2163 return 0;
2164 }
2165
btusb_switch_alt_setting(struct hci_dev * hdev,int new_alts)2166 static int btusb_switch_alt_setting(struct hci_dev *hdev, int new_alts)
2167 {
2168 struct btusb_data *data = hci_get_drvdata(hdev);
2169 int err;
2170
2171 if (data->isoc_altsetting != new_alts) {
2172 unsigned long flags;
2173
2174 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
2175 usb_kill_anchored_urbs(&data->isoc_anchor);
2176
2177 /* When isochronous alternate setting needs to be
2178 * changed, because SCO connection has been added
2179 * or removed, a packet fragment may be left in the
2180 * reassembling state. This could lead to wrongly
2181 * assembled fragments.
2182 *
2183 * Clear outstanding fragment when selecting a new
2184 * alternate setting.
2185 */
2186 spin_lock_irqsave(&data->rxlock, flags);
2187 dev_kfree_skb_irq(data->sco_skb);
2188 data->sco_skb = NULL;
2189 spin_unlock_irqrestore(&data->rxlock, flags);
2190
2191 err = __set_isoc_interface(hdev, new_alts);
2192 if (err < 0)
2193 return err;
2194 }
2195
2196 if (!test_and_set_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
2197 if (btusb_submit_isoc_urb(hdev, GFP_KERNEL) < 0)
2198 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
2199 else
2200 btusb_submit_isoc_urb(hdev, GFP_KERNEL);
2201 }
2202
2203 return 0;
2204 }
2205
btusb_find_altsetting(struct btusb_data * data,int alt)2206 static struct usb_host_interface *btusb_find_altsetting(struct btusb_data *data,
2207 int alt)
2208 {
2209 struct usb_interface *intf = data->isoc;
2210 int i;
2211
2212 BT_DBG("Looking for Alt no :%d", alt);
2213
2214 if (!intf)
2215 return NULL;
2216
2217 for (i = 0; i < intf->num_altsetting; i++) {
2218 if (intf->altsetting[i].desc.bAlternateSetting == alt)
2219 return &intf->altsetting[i];
2220 }
2221
2222 return NULL;
2223 }
2224
btusb_work(struct work_struct * work)2225 static void btusb_work(struct work_struct *work)
2226 {
2227 struct btusb_data *data = container_of(work, struct btusb_data, work);
2228 struct hci_dev *hdev = data->hdev;
2229 int new_alts = 0;
2230 int err;
2231
2232 if (data->sco_num > 0) {
2233 if (!test_bit(BTUSB_DID_ISO_RESUME, &data->flags)) {
2234 err = usb_autopm_get_interface(data->isoc ? data->isoc : data->intf);
2235 if (err < 0) {
2236 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
2237 usb_kill_anchored_urbs(&data->isoc_anchor);
2238 return;
2239 }
2240
2241 set_bit(BTUSB_DID_ISO_RESUME, &data->flags);
2242 }
2243
2244 if (data->air_mode == HCI_NOTIFY_ENABLE_SCO_CVSD) {
2245 if (hdev->voice_setting & 0x0020) {
2246 static const int alts[3] = { 2, 4, 5 };
2247
2248 new_alts = alts[data->sco_num - 1];
2249 } else {
2250 new_alts = data->sco_num;
2251 }
2252 } else if (data->air_mode == HCI_NOTIFY_ENABLE_SCO_TRANSP) {
2253 /* Bluetooth USB spec recommends alt 6 (63 bytes), but
2254 * many adapters do not support it. Alt 1 appears to
2255 * work for all adapters that do not have alt 6, and
2256 * which work with WBS at all. Some devices prefer
2257 * alt 3 (HCI payload >= 60 Bytes let air packet
2258 * data satisfy 60 bytes), requiring
2259 * MTU >= 3 (packets) * 25 (size) - 3 (headers) = 72
2260 * see also Core spec 5, vol 4, B 2.1.1 & Table 2.1.
2261 */
2262 if (btusb_find_altsetting(data, 6))
2263 new_alts = 6;
2264 else if (btusb_find_altsetting(data, 3) &&
2265 hdev->sco_mtu >= 72 &&
2266 test_bit(BTUSB_USE_ALT3_FOR_WBS, &data->flags))
2267 new_alts = 3;
2268 else
2269 new_alts = 1;
2270 }
2271
2272 if (btusb_switch_alt_setting(hdev, new_alts) < 0)
2273 bt_dev_err(hdev, "set USB alt:(%d) failed!", new_alts);
2274 } else {
2275 usb_kill_anchored_urbs(&data->isoc_anchor);
2276
2277 if (test_and_clear_bit(BTUSB_ISOC_RUNNING, &data->flags))
2278 __set_isoc_interface(hdev, 0);
2279
2280 if (test_and_clear_bit(BTUSB_DID_ISO_RESUME, &data->flags))
2281 usb_autopm_put_interface(data->isoc ? data->isoc : data->intf);
2282 }
2283 }
2284
btusb_waker(struct work_struct * work)2285 static void btusb_waker(struct work_struct *work)
2286 {
2287 struct btusb_data *data = container_of(work, struct btusb_data, waker);
2288 int err;
2289
2290 err = usb_autopm_get_interface(data->intf);
2291 if (err < 0)
2292 return;
2293
2294 usb_autopm_put_interface(data->intf);
2295 }
2296
btusb_rx_work(struct work_struct * work)2297 static void btusb_rx_work(struct work_struct *work)
2298 {
2299 struct btusb_data *data = container_of(work, struct btusb_data,
2300 rx_work.work);
2301 struct sk_buff *skb;
2302
2303 /* Dequeue ACL data received during the interval */
2304 while ((skb = skb_dequeue(&data->acl_q)))
2305 data->recv_acl(data->hdev, skb);
2306 }
2307
btusb_setup_bcm92035(struct hci_dev * hdev)2308 static int btusb_setup_bcm92035(struct hci_dev *hdev)
2309 {
2310 struct sk_buff *skb;
2311 u8 val = 0x00;
2312
2313 BT_DBG("%s", hdev->name);
2314
2315 skb = __hci_cmd_sync(hdev, 0xfc3b, 1, &val, HCI_INIT_TIMEOUT);
2316 if (IS_ERR(skb))
2317 bt_dev_err(hdev, "BCM92035 command failed (%ld)", PTR_ERR(skb));
2318 else
2319 kfree_skb(skb);
2320
2321 return 0;
2322 }
2323
btusb_setup_csr(struct hci_dev * hdev)2324 static int btusb_setup_csr(struct hci_dev *hdev)
2325 {
2326 struct btusb_data *data = hci_get_drvdata(hdev);
2327 u16 bcdDevice = le16_to_cpu(data->udev->descriptor.bcdDevice);
2328 struct hci_rp_read_local_version *rp;
2329 struct sk_buff *skb;
2330 bool is_fake = false;
2331 int ret;
2332
2333 BT_DBG("%s", hdev->name);
2334
2335 skb = __hci_cmd_sync(hdev, HCI_OP_READ_LOCAL_VERSION, 0, NULL,
2336 HCI_INIT_TIMEOUT);
2337 if (IS_ERR(skb)) {
2338 int err = PTR_ERR(skb);
2339 bt_dev_err(hdev, "CSR: Local version failed (%d)", err);
2340 return err;
2341 }
2342
2343 rp = skb_pull_data(skb, sizeof(*rp));
2344 if (!rp) {
2345 bt_dev_err(hdev, "CSR: Local version length mismatch");
2346 kfree_skb(skb);
2347 return -EIO;
2348 }
2349
2350 bt_dev_info(hdev, "CSR: Setting up dongle with HCI ver=%u rev=%04x",
2351 rp->hci_ver, le16_to_cpu(rp->hci_rev));
2352
2353 bt_dev_info(hdev, "LMP ver=%u subver=%04x; manufacturer=%u",
2354 rp->lmp_ver, le16_to_cpu(rp->lmp_subver),
2355 le16_to_cpu(rp->manufacturer));
2356
2357 /* Detect a wide host of Chinese controllers that aren't CSR.
2358 *
2359 * Known fake bcdDevices: 0x0100, 0x0134, 0x1915, 0x2520, 0x7558, 0x8891
2360 *
2361 * The main thing they have in common is that these are really popular low-cost
2362 * options that support newer Bluetooth versions but rely on heavy VID/PID
2363 * squatting of this poor old Bluetooth 1.1 device. Even sold as such.
2364 *
2365 * We detect actual CSR devices by checking that the HCI manufacturer code
2366 * is Cambridge Silicon Radio (10) and ensuring that LMP sub-version and
2367 * HCI rev values always match. As they both store the firmware number.
2368 */
2369 if (le16_to_cpu(rp->manufacturer) != 10 ||
2370 le16_to_cpu(rp->hci_rev) != le16_to_cpu(rp->lmp_subver))
2371 is_fake = true;
2372
2373 /* Known legit CSR firmware build numbers and their supported BT versions:
2374 * - 1.1 (0x1) -> 0x0073, 0x020d, 0x033c, 0x034e
2375 * - 1.2 (0x2) -> 0x04d9, 0x0529
2376 * - 2.0 (0x3) -> 0x07a6, 0x07ad, 0x0c5c
2377 * - 2.1 (0x4) -> 0x149c, 0x1735, 0x1899 (0x1899 is a BlueCore4-External)
2378 * - 4.0 (0x6) -> 0x1d86, 0x2031, 0x22bb
2379 *
2380 * e.g. Real CSR dongles with LMP subversion 0x73 are old enough that
2381 * support BT 1.1 only; so it's a dead giveaway when some
2382 * third-party BT 4.0 dongle reuses it.
2383 */
2384 else if (le16_to_cpu(rp->lmp_subver) <= 0x034e &&
2385 rp->hci_ver > BLUETOOTH_VER_1_1)
2386 is_fake = true;
2387
2388 else if (le16_to_cpu(rp->lmp_subver) <= 0x0529 &&
2389 rp->hci_ver > BLUETOOTH_VER_1_2)
2390 is_fake = true;
2391
2392 else if (le16_to_cpu(rp->lmp_subver) <= 0x0c5c &&
2393 rp->hci_ver > BLUETOOTH_VER_2_0)
2394 is_fake = true;
2395
2396 else if (le16_to_cpu(rp->lmp_subver) <= 0x1899 &&
2397 rp->hci_ver > BLUETOOTH_VER_2_1)
2398 is_fake = true;
2399
2400 else if (le16_to_cpu(rp->lmp_subver) <= 0x22bb &&
2401 rp->hci_ver > BLUETOOTH_VER_4_0)
2402 is_fake = true;
2403
2404 /* Other clones which beat all the above checks */
2405 else if (bcdDevice == 0x0134 &&
2406 le16_to_cpu(rp->lmp_subver) == 0x0c5c &&
2407 rp->hci_ver == BLUETOOTH_VER_2_0)
2408 is_fake = true;
2409
2410 if (is_fake) {
2411 bt_dev_warn(hdev, "CSR: Unbranded CSR clone detected; adding workarounds and force-suspending once...");
2412
2413 /* Generally these clones have big discrepancies between
2414 * advertised features and what's actually supported.
2415 * Probably will need to be expanded in the future;
2416 * without these the controller will lock up.
2417 */
2418 set_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks);
2419 set_bit(HCI_QUIRK_BROKEN_ERR_DATA_REPORTING, &hdev->quirks);
2420 set_bit(HCI_QUIRK_BROKEN_FILTER_CLEAR_ALL, &hdev->quirks);
2421 set_bit(HCI_QUIRK_NO_SUSPEND_NOTIFIER, &hdev->quirks);
2422
2423 /* Clear the reset quirk since this is not an actual
2424 * early Bluetooth 1.1 device from CSR.
2425 */
2426 clear_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
2427 clear_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
2428
2429 /*
2430 * Special workaround for these BT 4.0 chip clones, and potentially more:
2431 *
2432 * - 0x0134: a Barrot 8041a02 (HCI rev: 0x0810 sub: 0x1012)
2433 * - 0x7558: IC markings FR3191AHAL 749H15143 (HCI rev/sub-version: 0x0709)
2434 *
2435 * These controllers are really messed-up.
2436 *
2437 * 1. Their bulk RX endpoint will never report any data unless
2438 * the device was suspended at least once (yes, really).
2439 * 2. They will not wakeup when autosuspended and receiving data
2440 * on their bulk RX endpoint from e.g. a keyboard or mouse
2441 * (IOW remote-wakeup support is broken for the bulk endpoint).
2442 *
2443 * To fix 1. enable runtime-suspend, force-suspend the
2444 * HCI and then wake-it up by disabling runtime-suspend.
2445 *
2446 * To fix 2. clear the HCI's can_wake flag, this way the HCI
2447 * will still be autosuspended when it is not open.
2448 *
2449 * --
2450 *
2451 * Because these are widespread problems we prefer generic solutions; so
2452 * apply this initialization quirk to every controller that gets here,
2453 * it should be harmless. The alternative is to not work at all.
2454 */
2455 pm_runtime_allow(&data->udev->dev);
2456
2457 ret = pm_runtime_suspend(&data->udev->dev);
2458 if (ret >= 0)
2459 msleep(200);
2460 else
2461 bt_dev_warn(hdev, "CSR: Couldn't suspend the device for our Barrot 8041a02 receive-issue workaround");
2462
2463 pm_runtime_forbid(&data->udev->dev);
2464
2465 device_set_wakeup_capable(&data->udev->dev, false);
2466
2467 /* Re-enable autosuspend if this was requested */
2468 if (enable_autosuspend)
2469 usb_enable_autosuspend(data->udev);
2470 }
2471
2472 kfree_skb(skb);
2473
2474 return 0;
2475 }
2476
inject_cmd_complete(struct hci_dev * hdev,__u16 opcode)2477 static int inject_cmd_complete(struct hci_dev *hdev, __u16 opcode)
2478 {
2479 struct sk_buff *skb;
2480 struct hci_event_hdr *hdr;
2481 struct hci_ev_cmd_complete *evt;
2482
2483 skb = bt_skb_alloc(sizeof(*hdr) + sizeof(*evt) + 1, GFP_KERNEL);
2484 if (!skb)
2485 return -ENOMEM;
2486
2487 hdr = skb_put(skb, sizeof(*hdr));
2488 hdr->evt = HCI_EV_CMD_COMPLETE;
2489 hdr->plen = sizeof(*evt) + 1;
2490
2491 evt = skb_put(skb, sizeof(*evt));
2492 evt->ncmd = 0x01;
2493 evt->opcode = cpu_to_le16(opcode);
2494
2495 skb_put_u8(skb, 0x00);
2496
2497 hci_skb_pkt_type(skb) = HCI_EVENT_PKT;
2498
2499 return hci_recv_frame(hdev, skb);
2500 }
2501
btusb_recv_bulk_intel(struct btusb_data * data,void * buffer,int count)2502 static int btusb_recv_bulk_intel(struct btusb_data *data, void *buffer,
2503 int count)
2504 {
2505 struct hci_dev *hdev = data->hdev;
2506
2507 /* When the device is in bootloader mode, then it can send
2508 * events via the bulk endpoint. These events are treated the
2509 * same way as the ones received from the interrupt endpoint.
2510 */
2511 if (btintel_test_flag(hdev, INTEL_BOOTLOADER))
2512 return btusb_recv_intr(data, buffer, count);
2513
2514 return btusb_recv_bulk(data, buffer, count);
2515 }
2516
btusb_send_frame_intel(struct hci_dev * hdev,struct sk_buff * skb)2517 static int btusb_send_frame_intel(struct hci_dev *hdev, struct sk_buff *skb)
2518 {
2519 struct urb *urb;
2520
2521 BT_DBG("%s", hdev->name);
2522
2523 switch (hci_skb_pkt_type(skb)) {
2524 case HCI_COMMAND_PKT:
2525 if (btintel_test_flag(hdev, INTEL_BOOTLOADER)) {
2526 struct hci_command_hdr *cmd = (void *)skb->data;
2527 __u16 opcode = le16_to_cpu(cmd->opcode);
2528
2529 /* When in bootloader mode and the command 0xfc09
2530 * is received, it needs to be send down the
2531 * bulk endpoint. So allocate a bulk URB instead.
2532 */
2533 if (opcode == 0xfc09)
2534 urb = alloc_bulk_urb(hdev, skb);
2535 else
2536 urb = alloc_ctrl_urb(hdev, skb);
2537
2538 /* When the 0xfc01 command is issued to boot into
2539 * the operational firmware, it will actually not
2540 * send a command complete event. To keep the flow
2541 * control working inject that event here.
2542 */
2543 if (opcode == 0xfc01)
2544 inject_cmd_complete(hdev, opcode);
2545 } else {
2546 urb = alloc_ctrl_urb(hdev, skb);
2547 }
2548 if (IS_ERR(urb))
2549 return PTR_ERR(urb);
2550
2551 hdev->stat.cmd_tx++;
2552 return submit_or_queue_tx_urb(hdev, urb);
2553
2554 case HCI_ACLDATA_PKT:
2555 urb = alloc_bulk_urb(hdev, skb);
2556 if (IS_ERR(urb))
2557 return PTR_ERR(urb);
2558
2559 hdev->stat.acl_tx++;
2560 return submit_or_queue_tx_urb(hdev, urb);
2561
2562 case HCI_SCODATA_PKT:
2563 if (hci_conn_num(hdev, SCO_LINK) < 1)
2564 return -ENODEV;
2565
2566 urb = alloc_isoc_urb(hdev, skb);
2567 if (IS_ERR(urb))
2568 return PTR_ERR(urb);
2569
2570 hdev->stat.sco_tx++;
2571 return submit_tx_urb(hdev, urb);
2572
2573 case HCI_ISODATA_PKT:
2574 urb = alloc_bulk_urb(hdev, skb);
2575 if (IS_ERR(urb))
2576 return PTR_ERR(urb);
2577
2578 return submit_or_queue_tx_urb(hdev, urb);
2579 }
2580
2581 return -EILSEQ;
2582 }
2583
btusb_setup_realtek(struct hci_dev * hdev)2584 static int btusb_setup_realtek(struct hci_dev *hdev)
2585 {
2586 struct btusb_data *data = hci_get_drvdata(hdev);
2587 int ret;
2588
2589 ret = btrtl_setup_realtek(hdev);
2590
2591 if (btrealtek_test_flag(data->hdev, REALTEK_ALT6_CONTINUOUS_TX_CHIP))
2592 set_bit(BTUSB_ALT6_CONTINUOUS_TX, &data->flags);
2593
2594 return ret;
2595 }
2596
btusb_recv_event_realtek(struct hci_dev * hdev,struct sk_buff * skb)2597 static int btusb_recv_event_realtek(struct hci_dev *hdev, struct sk_buff *skb)
2598 {
2599 if (skb->data[0] == HCI_VENDOR_PKT && skb->data[2] == RTK_SUB_EVENT_CODE_COREDUMP) {
2600 struct rtk_dev_coredump_hdr hdr = {
2601 .code = RTK_DEVCOREDUMP_CODE_MEMDUMP,
2602 };
2603
2604 bt_dev_dbg(hdev, "RTL: received coredump vendor evt, len %u",
2605 skb->len);
2606
2607 btusb_rtl_alloc_devcoredump(hdev, &hdr, skb->data, skb->len);
2608 kfree_skb(skb);
2609
2610 return 0;
2611 }
2612
2613 return hci_recv_frame(hdev, skb);
2614 }
2615
2616 /* UHW CR mapping */
2617 #define MTK_BT_MISC 0x70002510
2618 #define MTK_BT_SUBSYS_RST 0x70002610
2619 #define MTK_UDMA_INT_STA_BT 0x74000024
2620 #define MTK_UDMA_INT_STA_BT1 0x74000308
2621 #define MTK_BT_WDT_STATUS 0x740003A0
2622 #define MTK_EP_RST_OPT 0x74011890
2623 #define MTK_EP_RST_IN_OUT_OPT 0x00010001
2624 #define MTK_BT_RST_DONE 0x00000100
2625 #define MTK_BT_RESET_REG_CONNV3 0x70028610
2626 #define MTK_BT_READ_DEV_ID 0x70010200
2627
2628
btusb_mtk_wmt_recv(struct urb * urb)2629 static void btusb_mtk_wmt_recv(struct urb *urb)
2630 {
2631 struct hci_dev *hdev = urb->context;
2632 struct btusb_data *data = hci_get_drvdata(hdev);
2633 struct sk_buff *skb;
2634 int err;
2635
2636 if (urb->status == 0 && urb->actual_length > 0) {
2637 hdev->stat.byte_rx += urb->actual_length;
2638
2639 /* WMT event shouldn't be fragmented and the size should be
2640 * less than HCI_WMT_MAX_EVENT_SIZE.
2641 */
2642 skb = bt_skb_alloc(HCI_WMT_MAX_EVENT_SIZE, GFP_ATOMIC);
2643 if (!skb) {
2644 hdev->stat.err_rx++;
2645 kfree(urb->setup_packet);
2646 return;
2647 }
2648
2649 hci_skb_pkt_type(skb) = HCI_EVENT_PKT;
2650 skb_put_data(skb, urb->transfer_buffer, urb->actual_length);
2651
2652 /* When someone waits for the WMT event, the skb is being cloned
2653 * and being processed the events from there then.
2654 */
2655 if (test_bit(BTUSB_TX_WAIT_VND_EVT, &data->flags)) {
2656 data->evt_skb = skb_clone(skb, GFP_ATOMIC);
2657 if (!data->evt_skb) {
2658 kfree_skb(skb);
2659 kfree(urb->setup_packet);
2660 return;
2661 }
2662 }
2663
2664 err = hci_recv_frame(hdev, skb);
2665 if (err < 0) {
2666 kfree_skb(data->evt_skb);
2667 data->evt_skb = NULL;
2668 kfree(urb->setup_packet);
2669 return;
2670 }
2671
2672 if (test_and_clear_bit(BTUSB_TX_WAIT_VND_EVT,
2673 &data->flags)) {
2674 /* Barrier to sync with other CPUs */
2675 smp_mb__after_atomic();
2676 wake_up_bit(&data->flags,
2677 BTUSB_TX_WAIT_VND_EVT);
2678 }
2679 kfree(urb->setup_packet);
2680 return;
2681 } else if (urb->status == -ENOENT) {
2682 /* Avoid suspend failed when usb_kill_urb */
2683 return;
2684 }
2685
2686 usb_mark_last_busy(data->udev);
2687
2688 /* The URB complete handler is still called with urb->actual_length = 0
2689 * when the event is not available, so we should keep re-submitting
2690 * URB until WMT event returns, Also, It's necessary to wait some time
2691 * between the two consecutive control URBs to relax the target device
2692 * to generate the event. Otherwise, the WMT event cannot return from
2693 * the device successfully.
2694 */
2695 udelay(500);
2696
2697 usb_anchor_urb(urb, &data->ctrl_anchor);
2698 err = usb_submit_urb(urb, GFP_ATOMIC);
2699 if (err < 0) {
2700 kfree(urb->setup_packet);
2701 /* -EPERM: urb is being killed;
2702 * -ENODEV: device got disconnected
2703 */
2704 if (err != -EPERM && err != -ENODEV)
2705 bt_dev_err(hdev, "urb %p failed to resubmit (%d)",
2706 urb, -err);
2707 usb_unanchor_urb(urb);
2708 }
2709 }
2710
btusb_mtk_submit_wmt_recv_urb(struct hci_dev * hdev)2711 static int btusb_mtk_submit_wmt_recv_urb(struct hci_dev *hdev)
2712 {
2713 struct btusb_data *data = hci_get_drvdata(hdev);
2714 struct usb_ctrlrequest *dr;
2715 unsigned char *buf;
2716 int err, size = 64;
2717 unsigned int pipe;
2718 struct urb *urb;
2719
2720 urb = usb_alloc_urb(0, GFP_KERNEL);
2721 if (!urb)
2722 return -ENOMEM;
2723
2724 dr = kmalloc(sizeof(*dr), GFP_KERNEL);
2725 if (!dr) {
2726 usb_free_urb(urb);
2727 return -ENOMEM;
2728 }
2729
2730 dr->bRequestType = USB_TYPE_VENDOR | USB_DIR_IN;
2731 dr->bRequest = 1;
2732 dr->wIndex = cpu_to_le16(0);
2733 dr->wValue = cpu_to_le16(48);
2734 dr->wLength = cpu_to_le16(size);
2735
2736 buf = kmalloc(size, GFP_KERNEL);
2737 if (!buf) {
2738 kfree(dr);
2739 usb_free_urb(urb);
2740 return -ENOMEM;
2741 }
2742
2743 pipe = usb_rcvctrlpipe(data->udev, 0);
2744
2745 usb_fill_control_urb(urb, data->udev, pipe, (void *)dr,
2746 buf, size, btusb_mtk_wmt_recv, hdev);
2747
2748 urb->transfer_flags |= URB_FREE_BUFFER;
2749
2750 usb_anchor_urb(urb, &data->ctrl_anchor);
2751 err = usb_submit_urb(urb, GFP_KERNEL);
2752 if (err < 0) {
2753 if (err != -EPERM && err != -ENODEV)
2754 bt_dev_err(hdev, "urb %p submission failed (%d)",
2755 urb, -err);
2756 usb_unanchor_urb(urb);
2757 }
2758
2759 usb_free_urb(urb);
2760
2761 return err;
2762 }
2763
btusb_mtk_hci_wmt_sync(struct hci_dev * hdev,struct btmtk_hci_wmt_params * wmt_params)2764 static int btusb_mtk_hci_wmt_sync(struct hci_dev *hdev,
2765 struct btmtk_hci_wmt_params *wmt_params)
2766 {
2767 struct btusb_data *data = hci_get_drvdata(hdev);
2768 struct btmtk_hci_wmt_evt_funcc *wmt_evt_funcc;
2769 u32 hlen, status = BTMTK_WMT_INVALID;
2770 struct btmtk_hci_wmt_evt *wmt_evt;
2771 struct btmtk_hci_wmt_cmd *wc;
2772 struct btmtk_wmt_hdr *hdr;
2773 int err;
2774
2775 /* Send the WMT command and wait until the WMT event returns */
2776 hlen = sizeof(*hdr) + wmt_params->dlen;
2777 if (hlen > 255)
2778 return -EINVAL;
2779
2780 wc = kzalloc(hlen, GFP_KERNEL);
2781 if (!wc)
2782 return -ENOMEM;
2783
2784 hdr = &wc->hdr;
2785 hdr->dir = 1;
2786 hdr->op = wmt_params->op;
2787 hdr->dlen = cpu_to_le16(wmt_params->dlen + 1);
2788 hdr->flag = wmt_params->flag;
2789 memcpy(wc->data, wmt_params->data, wmt_params->dlen);
2790
2791 set_bit(BTUSB_TX_WAIT_VND_EVT, &data->flags);
2792
2793 /* WMT cmd/event doesn't follow up the generic HCI cmd/event handling,
2794 * it needs constantly polling control pipe until the host received the
2795 * WMT event, thus, we should require to specifically acquire PM counter
2796 * on the USB to prevent the interface from entering auto suspended
2797 * while WMT cmd/event in progress.
2798 */
2799 err = usb_autopm_get_interface(data->intf);
2800 if (err < 0)
2801 goto err_free_wc;
2802
2803 err = __hci_cmd_send(hdev, 0xfc6f, hlen, wc);
2804
2805 if (err < 0) {
2806 clear_bit(BTUSB_TX_WAIT_VND_EVT, &data->flags);
2807 usb_autopm_put_interface(data->intf);
2808 goto err_free_wc;
2809 }
2810
2811 /* Submit control IN URB on demand to process the WMT event */
2812 err = btusb_mtk_submit_wmt_recv_urb(hdev);
2813
2814 usb_autopm_put_interface(data->intf);
2815
2816 if (err < 0)
2817 goto err_free_wc;
2818
2819 /* The vendor specific WMT commands are all answered by a vendor
2820 * specific event and will have the Command Status or Command
2821 * Complete as with usual HCI command flow control.
2822 *
2823 * After sending the command, wait for BTUSB_TX_WAIT_VND_EVT
2824 * state to be cleared. The driver specific event receive routine
2825 * will clear that state and with that indicate completion of the
2826 * WMT command.
2827 */
2828 err = wait_on_bit_timeout(&data->flags, BTUSB_TX_WAIT_VND_EVT,
2829 TASK_INTERRUPTIBLE, HCI_INIT_TIMEOUT);
2830 if (err == -EINTR) {
2831 bt_dev_err(hdev, "Execution of wmt command interrupted");
2832 clear_bit(BTUSB_TX_WAIT_VND_EVT, &data->flags);
2833 goto err_free_wc;
2834 }
2835
2836 if (err) {
2837 bt_dev_err(hdev, "Execution of wmt command timed out");
2838 clear_bit(BTUSB_TX_WAIT_VND_EVT, &data->flags);
2839 err = -ETIMEDOUT;
2840 goto err_free_wc;
2841 }
2842
2843 if (data->evt_skb == NULL)
2844 goto err_free_wc;
2845
2846 /* Parse and handle the return WMT event */
2847 wmt_evt = (struct btmtk_hci_wmt_evt *)data->evt_skb->data;
2848 if (wmt_evt->whdr.op != hdr->op) {
2849 bt_dev_err(hdev, "Wrong op received %d expected %d",
2850 wmt_evt->whdr.op, hdr->op);
2851 err = -EIO;
2852 goto err_free_skb;
2853 }
2854
2855 switch (wmt_evt->whdr.op) {
2856 case BTMTK_WMT_SEMAPHORE:
2857 if (wmt_evt->whdr.flag == 2)
2858 status = BTMTK_WMT_PATCH_UNDONE;
2859 else
2860 status = BTMTK_WMT_PATCH_DONE;
2861 break;
2862 case BTMTK_WMT_FUNC_CTRL:
2863 wmt_evt_funcc = (struct btmtk_hci_wmt_evt_funcc *)wmt_evt;
2864 if (be16_to_cpu(wmt_evt_funcc->status) == 0x404)
2865 status = BTMTK_WMT_ON_DONE;
2866 else if (be16_to_cpu(wmt_evt_funcc->status) == 0x420)
2867 status = BTMTK_WMT_ON_PROGRESS;
2868 else
2869 status = BTMTK_WMT_ON_UNDONE;
2870 break;
2871 case BTMTK_WMT_PATCH_DWNLD:
2872 if (wmt_evt->whdr.flag == 2)
2873 status = BTMTK_WMT_PATCH_DONE;
2874 else if (wmt_evt->whdr.flag == 1)
2875 status = BTMTK_WMT_PATCH_PROGRESS;
2876 else
2877 status = BTMTK_WMT_PATCH_UNDONE;
2878 break;
2879 }
2880
2881 if (wmt_params->status)
2882 *wmt_params->status = status;
2883
2884 err_free_skb:
2885 kfree_skb(data->evt_skb);
2886 data->evt_skb = NULL;
2887 err_free_wc:
2888 kfree(wc);
2889 return err;
2890 }
2891
btusb_mtk_func_query(struct hci_dev * hdev)2892 static int btusb_mtk_func_query(struct hci_dev *hdev)
2893 {
2894 struct btmtk_hci_wmt_params wmt_params;
2895 int status, err;
2896 u8 param = 0;
2897
2898 /* Query whether the function is enabled */
2899 wmt_params.op = BTMTK_WMT_FUNC_CTRL;
2900 wmt_params.flag = 4;
2901 wmt_params.dlen = sizeof(param);
2902 wmt_params.data = ¶m;
2903 wmt_params.status = &status;
2904
2905 err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
2906 if (err < 0) {
2907 bt_dev_err(hdev, "Failed to query function status (%d)", err);
2908 return err;
2909 }
2910
2911 return status;
2912 }
2913
btusb_mtk_uhw_reg_write(struct btusb_data * data,u32 reg,u32 val)2914 static int btusb_mtk_uhw_reg_write(struct btusb_data *data, u32 reg, u32 val)
2915 {
2916 struct hci_dev *hdev = data->hdev;
2917 int pipe, err;
2918 void *buf;
2919
2920 buf = kzalloc(4, GFP_KERNEL);
2921 if (!buf)
2922 return -ENOMEM;
2923
2924 put_unaligned_le32(val, buf);
2925
2926 pipe = usb_sndctrlpipe(data->udev, 0);
2927 err = usb_control_msg(data->udev, pipe, 0x02,
2928 0x5E,
2929 reg >> 16, reg & 0xffff,
2930 buf, 4, USB_CTRL_SET_TIMEOUT);
2931 if (err < 0) {
2932 bt_dev_err(hdev, "Failed to write uhw reg(%d)", err);
2933 goto err_free_buf;
2934 }
2935
2936 err_free_buf:
2937 kfree(buf);
2938
2939 return err;
2940 }
2941
btusb_mtk_uhw_reg_read(struct btusb_data * data,u32 reg,u32 * val)2942 static int btusb_mtk_uhw_reg_read(struct btusb_data *data, u32 reg, u32 *val)
2943 {
2944 struct hci_dev *hdev = data->hdev;
2945 int pipe, err;
2946 void *buf;
2947
2948 buf = kzalloc(4, GFP_KERNEL);
2949 if (!buf)
2950 return -ENOMEM;
2951
2952 pipe = usb_rcvctrlpipe(data->udev, 0);
2953 err = usb_control_msg(data->udev, pipe, 0x01,
2954 0xDE,
2955 reg >> 16, reg & 0xffff,
2956 buf, 4, USB_CTRL_SET_TIMEOUT);
2957 if (err < 0) {
2958 bt_dev_err(hdev, "Failed to read uhw reg(%d)", err);
2959 goto err_free_buf;
2960 }
2961
2962 *val = get_unaligned_le32(buf);
2963 bt_dev_dbg(hdev, "reg=%x, value=0x%08x", reg, *val);
2964
2965 err_free_buf:
2966 kfree(buf);
2967
2968 return err;
2969 }
2970
btusb_mtk_reg_read(struct btusb_data * data,u32 reg,u32 * val)2971 static int btusb_mtk_reg_read(struct btusb_data *data, u32 reg, u32 *val)
2972 {
2973 int pipe, err, size = sizeof(u32);
2974 void *buf;
2975
2976 buf = kzalloc(size, GFP_KERNEL);
2977 if (!buf)
2978 return -ENOMEM;
2979
2980 pipe = usb_rcvctrlpipe(data->udev, 0);
2981 err = usb_control_msg(data->udev, pipe, 0x63,
2982 USB_TYPE_VENDOR | USB_DIR_IN,
2983 reg >> 16, reg & 0xffff,
2984 buf, size, USB_CTRL_SET_TIMEOUT);
2985 if (err < 0)
2986 goto err_free_buf;
2987
2988 *val = get_unaligned_le32(buf);
2989
2990 err_free_buf:
2991 kfree(buf);
2992
2993 return err;
2994 }
2995
btusb_mtk_id_get(struct btusb_data * data,u32 reg,u32 * id)2996 static int btusb_mtk_id_get(struct btusb_data *data, u32 reg, u32 *id)
2997 {
2998 return btusb_mtk_reg_read(data, reg, id);
2999 }
3000
btusb_mtk_reset_done(struct hci_dev * hdev)3001 static u32 btusb_mtk_reset_done(struct hci_dev *hdev)
3002 {
3003 struct btusb_data *data = hci_get_drvdata(hdev);
3004 u32 val = 0;
3005
3006 btusb_mtk_uhw_reg_read(data, MTK_BT_MISC, &val);
3007
3008 return val & MTK_BT_RST_DONE;
3009 }
3010
btusb_mtk_reset(struct hci_dev * hdev,void * rst_data)3011 static int btusb_mtk_reset(struct hci_dev *hdev, void *rst_data)
3012 {
3013 struct btusb_data *data = hci_get_drvdata(hdev);
3014 struct btmediatek_data *mediatek;
3015 u32 val;
3016 int err;
3017
3018 /* It's MediaTek specific bluetooth reset mechanism via USB */
3019 if (test_and_set_bit(BTUSB_HW_RESET_ACTIVE, &data->flags)) {
3020 bt_dev_err(hdev, "last reset failed? Not resetting again");
3021 return -EBUSY;
3022 }
3023
3024 err = usb_autopm_get_interface(data->intf);
3025 if (err < 0)
3026 return err;
3027
3028 btusb_stop_traffic(data);
3029 usb_kill_anchored_urbs(&data->tx_anchor);
3030 mediatek = hci_get_priv(hdev);
3031
3032 if (mediatek->dev_id == 0x7925) {
3033 btusb_mtk_uhw_reg_read(data, MTK_BT_RESET_REG_CONNV3, &val);
3034 val |= (1 << 5);
3035 btusb_mtk_uhw_reg_write(data, MTK_BT_RESET_REG_CONNV3, val);
3036 btusb_mtk_uhw_reg_read(data, MTK_BT_RESET_REG_CONNV3, &val);
3037 val &= 0xFFFF00FF;
3038 val |= (1 << 13);
3039 btusb_mtk_uhw_reg_write(data, MTK_BT_RESET_REG_CONNV3, val);
3040 btusb_mtk_uhw_reg_write(data, MTK_EP_RST_OPT, 0x00010001);
3041 btusb_mtk_uhw_reg_read(data, MTK_BT_RESET_REG_CONNV3, &val);
3042 val |= (1 << 0);
3043 btusb_mtk_uhw_reg_write(data, MTK_BT_RESET_REG_CONNV3, val);
3044 btusb_mtk_uhw_reg_write(data, MTK_UDMA_INT_STA_BT, 0x000000FF);
3045 btusb_mtk_uhw_reg_read(data, MTK_UDMA_INT_STA_BT, &val);
3046 btusb_mtk_uhw_reg_write(data, MTK_UDMA_INT_STA_BT1, 0x000000FF);
3047 btusb_mtk_uhw_reg_read(data, MTK_UDMA_INT_STA_BT1, &val);
3048 msleep(100);
3049 } else {
3050 /* It's Device EndPoint Reset Option Register */
3051 bt_dev_dbg(hdev, "Initiating reset mechanism via uhw");
3052 btusb_mtk_uhw_reg_write(data, MTK_EP_RST_OPT, MTK_EP_RST_IN_OUT_OPT);
3053 btusb_mtk_uhw_reg_read(data, MTK_BT_WDT_STATUS, &val);
3054
3055 /* Reset the bluetooth chip via USB interface. */
3056 btusb_mtk_uhw_reg_write(data, MTK_BT_SUBSYS_RST, 1);
3057 btusb_mtk_uhw_reg_write(data, MTK_UDMA_INT_STA_BT, 0x000000FF);
3058 btusb_mtk_uhw_reg_read(data, MTK_UDMA_INT_STA_BT, &val);
3059 btusb_mtk_uhw_reg_write(data, MTK_UDMA_INT_STA_BT1, 0x000000FF);
3060 btusb_mtk_uhw_reg_read(data, MTK_UDMA_INT_STA_BT1, &val);
3061 /* MT7921 need to delay 20ms between toggle reset bit */
3062 msleep(20);
3063 btusb_mtk_uhw_reg_write(data, MTK_BT_SUBSYS_RST, 0);
3064 btusb_mtk_uhw_reg_read(data, MTK_BT_SUBSYS_RST, &val);
3065 }
3066
3067 err = readx_poll_timeout(btusb_mtk_reset_done, hdev, val,
3068 val & MTK_BT_RST_DONE, 20000, 1000000);
3069 if (err < 0)
3070 bt_dev_err(hdev, "Reset timeout");
3071
3072 btusb_mtk_id_get(data, 0x70010200, &val);
3073 if (!val)
3074 bt_dev_err(hdev, "Can't get device id, subsys reset fail.");
3075
3076 usb_queue_reset_device(data->intf);
3077
3078 clear_bit(BTUSB_HW_RESET_ACTIVE, &data->flags);
3079
3080 return err;
3081 }
3082
btusb_mtk_setup(struct hci_dev * hdev)3083 static int btusb_mtk_setup(struct hci_dev *hdev)
3084 {
3085 struct btusb_data *data = hci_get_drvdata(hdev);
3086 struct btmtk_hci_wmt_params wmt_params;
3087 ktime_t calltime, delta, rettime;
3088 struct btmtk_tci_sleep tci_sleep;
3089 unsigned long long duration;
3090 struct sk_buff *skb;
3091 const char *fwname;
3092 int err, status;
3093 u32 dev_id = 0;
3094 char fw_bin_name[64];
3095 u32 fw_version = 0;
3096 u8 param;
3097 struct btmediatek_data *mediatek;
3098
3099 calltime = ktime_get();
3100
3101 err = btusb_mtk_id_get(data, 0x80000008, &dev_id);
3102 if (err < 0) {
3103 bt_dev_err(hdev, "Failed to get device id (%d)", err);
3104 return err;
3105 }
3106
3107 if (!dev_id || dev_id != 0x7663) {
3108 err = btusb_mtk_id_get(data, 0x70010200, &dev_id);
3109 if (err < 0) {
3110 bt_dev_err(hdev, "Failed to get device id (%d)", err);
3111 return err;
3112 }
3113 err = btusb_mtk_id_get(data, 0x80021004, &fw_version);
3114 if (err < 0) {
3115 bt_dev_err(hdev, "Failed to get fw version (%d)", err);
3116 return err;
3117 }
3118 }
3119
3120 mediatek = hci_get_priv(hdev);
3121 mediatek->dev_id = dev_id;
3122 mediatek->reset_sync = btusb_mtk_reset;
3123
3124 err = btmtk_register_coredump(hdev, btusb_driver.name, fw_version);
3125 if (err < 0)
3126 bt_dev_err(hdev, "Failed to register coredump (%d)", err);
3127
3128 switch (dev_id) {
3129 case 0x7663:
3130 fwname = FIRMWARE_MT7663;
3131 break;
3132 case 0x7668:
3133 fwname = FIRMWARE_MT7668;
3134 break;
3135 case 0x7922:
3136 case 0x7961:
3137 case 0x7925:
3138 if (dev_id == 0x7925)
3139 snprintf(fw_bin_name, sizeof(fw_bin_name),
3140 "mediatek/mt%04x/BT_RAM_CODE_MT%04x_1_%x_hdr.bin",
3141 dev_id & 0xffff, dev_id & 0xffff, (fw_version & 0xff) + 1);
3142 else
3143 snprintf(fw_bin_name, sizeof(fw_bin_name),
3144 "mediatek/BT_RAM_CODE_MT%04x_1_%x_hdr.bin",
3145 dev_id & 0xffff, (fw_version & 0xff) + 1);
3146
3147 err = btmtk_setup_firmware_79xx(hdev, fw_bin_name,
3148 btusb_mtk_hci_wmt_sync);
3149 if (err < 0) {
3150 bt_dev_err(hdev, "Failed to set up firmware (%d)", err);
3151 return err;
3152 }
3153
3154 /* It's Device EndPoint Reset Option Register */
3155 btusb_mtk_uhw_reg_write(data, MTK_EP_RST_OPT, MTK_EP_RST_IN_OUT_OPT);
3156
3157 /* Enable Bluetooth protocol */
3158 param = 1;
3159 wmt_params.op = BTMTK_WMT_FUNC_CTRL;
3160 wmt_params.flag = 0;
3161 wmt_params.dlen = sizeof(param);
3162 wmt_params.data = ¶m;
3163 wmt_params.status = NULL;
3164
3165 err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
3166 if (err < 0) {
3167 bt_dev_err(hdev, "Failed to send wmt func ctrl (%d)", err);
3168 return err;
3169 }
3170
3171 hci_set_msft_opcode(hdev, 0xFD30);
3172 hci_set_aosp_capable(hdev);
3173 goto done;
3174 default:
3175 bt_dev_err(hdev, "Unsupported hardware variant (%08x)",
3176 dev_id);
3177 return -ENODEV;
3178 }
3179
3180 /* Query whether the firmware is already download */
3181 wmt_params.op = BTMTK_WMT_SEMAPHORE;
3182 wmt_params.flag = 1;
3183 wmt_params.dlen = 0;
3184 wmt_params.data = NULL;
3185 wmt_params.status = &status;
3186
3187 err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
3188 if (err < 0) {
3189 bt_dev_err(hdev, "Failed to query firmware status (%d)", err);
3190 return err;
3191 }
3192
3193 if (status == BTMTK_WMT_PATCH_DONE) {
3194 bt_dev_info(hdev, "firmware already downloaded");
3195 goto ignore_setup_fw;
3196 }
3197
3198 /* Setup a firmware which the device definitely requires */
3199 err = btmtk_setup_firmware(hdev, fwname,
3200 btusb_mtk_hci_wmt_sync);
3201 if (err < 0)
3202 return err;
3203
3204 ignore_setup_fw:
3205 err = readx_poll_timeout(btusb_mtk_func_query, hdev, status,
3206 status < 0 || status != BTMTK_WMT_ON_PROGRESS,
3207 2000, 5000000);
3208 /* -ETIMEDOUT happens */
3209 if (err < 0)
3210 return err;
3211
3212 /* The other errors happen in btusb_mtk_func_query */
3213 if (status < 0)
3214 return status;
3215
3216 if (status == BTMTK_WMT_ON_DONE) {
3217 bt_dev_info(hdev, "function already on");
3218 goto ignore_func_on;
3219 }
3220
3221 /* Enable Bluetooth protocol */
3222 param = 1;
3223 wmt_params.op = BTMTK_WMT_FUNC_CTRL;
3224 wmt_params.flag = 0;
3225 wmt_params.dlen = sizeof(param);
3226 wmt_params.data = ¶m;
3227 wmt_params.status = NULL;
3228
3229 err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
3230 if (err < 0) {
3231 bt_dev_err(hdev, "Failed to send wmt func ctrl (%d)", err);
3232 return err;
3233 }
3234
3235 ignore_func_on:
3236 /* Apply the low power environment setup */
3237 tci_sleep.mode = 0x5;
3238 tci_sleep.duration = cpu_to_le16(0x640);
3239 tci_sleep.host_duration = cpu_to_le16(0x640);
3240 tci_sleep.host_wakeup_pin = 0;
3241 tci_sleep.time_compensation = 0;
3242
3243 skb = __hci_cmd_sync(hdev, 0xfc7a, sizeof(tci_sleep), &tci_sleep,
3244 HCI_INIT_TIMEOUT);
3245 if (IS_ERR(skb)) {
3246 err = PTR_ERR(skb);
3247 bt_dev_err(hdev, "Failed to apply low power setting (%d)", err);
3248 return err;
3249 }
3250 kfree_skb(skb);
3251
3252 done:
3253 rettime = ktime_get();
3254 delta = ktime_sub(rettime, calltime);
3255 duration = (unsigned long long)ktime_to_ns(delta) >> 10;
3256
3257 bt_dev_info(hdev, "Device setup in %llu usecs", duration);
3258
3259 return 0;
3260 }
3261
btusb_mtk_shutdown(struct hci_dev * hdev)3262 static int btusb_mtk_shutdown(struct hci_dev *hdev)
3263 {
3264 struct btmtk_hci_wmt_params wmt_params;
3265 u8 param = 0;
3266 int err;
3267
3268 /* Disable the device */
3269 wmt_params.op = BTMTK_WMT_FUNC_CTRL;
3270 wmt_params.flag = 0;
3271 wmt_params.dlen = sizeof(param);
3272 wmt_params.data = ¶m;
3273 wmt_params.status = NULL;
3274
3275 err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
3276 if (err < 0) {
3277 bt_dev_err(hdev, "Failed to send wmt func ctrl (%d)", err);
3278 return err;
3279 }
3280
3281 return 0;
3282 }
3283
btusb_recv_acl_mtk(struct hci_dev * hdev,struct sk_buff * skb)3284 static int btusb_recv_acl_mtk(struct hci_dev *hdev, struct sk_buff *skb)
3285 {
3286 struct btusb_data *data = hci_get_drvdata(hdev);
3287 u16 handle = le16_to_cpu(hci_acl_hdr(skb)->handle);
3288
3289 switch (handle) {
3290 case 0xfc6f: /* Firmware dump from device */
3291 /* When the firmware hangs, the device can no longer
3292 * suspend and thus disable auto-suspend.
3293 */
3294 usb_disable_autosuspend(data->udev);
3295
3296 /* We need to forward the diagnostic packet to userspace daemon
3297 * for backward compatibility, so we have to clone the packet
3298 * extraly for the in-kernel coredump support.
3299 */
3300 if (IS_ENABLED(CONFIG_DEV_COREDUMP)) {
3301 struct sk_buff *skb_cd = skb_clone(skb, GFP_ATOMIC);
3302
3303 if (skb_cd)
3304 btmtk_process_coredump(hdev, skb_cd);
3305 }
3306
3307 fallthrough;
3308 case 0x05ff: /* Firmware debug logging 1 */
3309 case 0x05fe: /* Firmware debug logging 2 */
3310 return hci_recv_diag(hdev, skb);
3311 }
3312
3313 return hci_recv_frame(hdev, skb);
3314 }
3315
3316 #ifdef CONFIG_PM
3317 /* Configure an out-of-band gpio as wake-up pin, if specified in device tree */
marvell_config_oob_wake(struct hci_dev * hdev)3318 static int marvell_config_oob_wake(struct hci_dev *hdev)
3319 {
3320 struct sk_buff *skb;
3321 struct btusb_data *data = hci_get_drvdata(hdev);
3322 struct device *dev = &data->udev->dev;
3323 u16 pin, gap, opcode;
3324 int ret;
3325 u8 cmd[5];
3326
3327 /* Move on if no wakeup pin specified */
3328 if (of_property_read_u16(dev->of_node, "marvell,wakeup-pin", &pin) ||
3329 of_property_read_u16(dev->of_node, "marvell,wakeup-gap-ms", &gap))
3330 return 0;
3331
3332 /* Vendor specific command to configure a GPIO as wake-up pin */
3333 opcode = hci_opcode_pack(0x3F, 0x59);
3334 cmd[0] = opcode & 0xFF;
3335 cmd[1] = opcode >> 8;
3336 cmd[2] = 2; /* length of parameters that follow */
3337 cmd[3] = pin;
3338 cmd[4] = gap; /* time in ms, for which wakeup pin should be asserted */
3339
3340 skb = bt_skb_alloc(sizeof(cmd), GFP_KERNEL);
3341 if (!skb) {
3342 bt_dev_err(hdev, "%s: No memory", __func__);
3343 return -ENOMEM;
3344 }
3345
3346 skb_put_data(skb, cmd, sizeof(cmd));
3347 hci_skb_pkt_type(skb) = HCI_COMMAND_PKT;
3348
3349 ret = btusb_send_frame(hdev, skb);
3350 if (ret) {
3351 bt_dev_err(hdev, "%s: configuration failed", __func__);
3352 kfree_skb(skb);
3353 return ret;
3354 }
3355
3356 return 0;
3357 }
3358 #endif
3359
btusb_set_bdaddr_marvell(struct hci_dev * hdev,const bdaddr_t * bdaddr)3360 static int btusb_set_bdaddr_marvell(struct hci_dev *hdev,
3361 const bdaddr_t *bdaddr)
3362 {
3363 struct sk_buff *skb;
3364 u8 buf[8];
3365 long ret;
3366
3367 buf[0] = 0xfe;
3368 buf[1] = sizeof(bdaddr_t);
3369 memcpy(buf + 2, bdaddr, sizeof(bdaddr_t));
3370
3371 skb = __hci_cmd_sync(hdev, 0xfc22, sizeof(buf), buf, HCI_INIT_TIMEOUT);
3372 if (IS_ERR(skb)) {
3373 ret = PTR_ERR(skb);
3374 bt_dev_err(hdev, "changing Marvell device address failed (%ld)",
3375 ret);
3376 return ret;
3377 }
3378 kfree_skb(skb);
3379
3380 return 0;
3381 }
3382
btusb_set_bdaddr_ath3012(struct hci_dev * hdev,const bdaddr_t * bdaddr)3383 static int btusb_set_bdaddr_ath3012(struct hci_dev *hdev,
3384 const bdaddr_t *bdaddr)
3385 {
3386 struct sk_buff *skb;
3387 u8 buf[10];
3388 long ret;
3389
3390 buf[0] = 0x01;
3391 buf[1] = 0x01;
3392 buf[2] = 0x00;
3393 buf[3] = sizeof(bdaddr_t);
3394 memcpy(buf + 4, bdaddr, sizeof(bdaddr_t));
3395
3396 skb = __hci_cmd_sync(hdev, 0xfc0b, sizeof(buf), buf, HCI_INIT_TIMEOUT);
3397 if (IS_ERR(skb)) {
3398 ret = PTR_ERR(skb);
3399 bt_dev_err(hdev, "Change address command failed (%ld)", ret);
3400 return ret;
3401 }
3402 kfree_skb(skb);
3403
3404 return 0;
3405 }
3406
btusb_set_bdaddr_wcn6855(struct hci_dev * hdev,const bdaddr_t * bdaddr)3407 static int btusb_set_bdaddr_wcn6855(struct hci_dev *hdev,
3408 const bdaddr_t *bdaddr)
3409 {
3410 struct sk_buff *skb;
3411 u8 buf[6];
3412 long ret;
3413
3414 memcpy(buf, bdaddr, sizeof(bdaddr_t));
3415
3416 skb = __hci_cmd_sync_ev(hdev, 0xfc14, sizeof(buf), buf,
3417 HCI_EV_CMD_COMPLETE, HCI_INIT_TIMEOUT);
3418 if (IS_ERR(skb)) {
3419 ret = PTR_ERR(skb);
3420 bt_dev_err(hdev, "Change address command failed (%ld)", ret);
3421 return ret;
3422 }
3423 kfree_skb(skb);
3424
3425 return 0;
3426 }
3427
3428 #define QCA_MEMDUMP_ACL_HANDLE 0x2EDD
3429 #define QCA_MEMDUMP_SIZE_MAX 0x100000
3430 #define QCA_MEMDUMP_VSE_CLASS 0x01
3431 #define QCA_MEMDUMP_MSG_TYPE 0x08
3432 #define QCA_MEMDUMP_PKT_SIZE 248
3433 #define QCA_LAST_SEQUENCE_NUM 0xffff
3434
3435 struct qca_dump_hdr {
3436 u8 vse_class;
3437 u8 msg_type;
3438 __le16 seqno;
3439 u8 reserved;
3440 union {
3441 u8 data[0];
3442 struct {
3443 __le32 ram_dump_size;
3444 u8 data0[0];
3445 } __packed;
3446 };
3447 } __packed;
3448
3449
btusb_dump_hdr_qca(struct hci_dev * hdev,struct sk_buff * skb)3450 static void btusb_dump_hdr_qca(struct hci_dev *hdev, struct sk_buff *skb)
3451 {
3452 char buf[128];
3453 struct btusb_data *btdata = hci_get_drvdata(hdev);
3454
3455 snprintf(buf, sizeof(buf), "Controller Name: 0x%x\n",
3456 btdata->qca_dump.controller_id);
3457 skb_put_data(skb, buf, strlen(buf));
3458
3459 snprintf(buf, sizeof(buf), "Firmware Version: 0x%x\n",
3460 btdata->qca_dump.fw_version);
3461 skb_put_data(skb, buf, strlen(buf));
3462
3463 snprintf(buf, sizeof(buf), "Driver: %s\nVendor: qca\n",
3464 btusb_driver.name);
3465 skb_put_data(skb, buf, strlen(buf));
3466
3467 snprintf(buf, sizeof(buf), "VID: 0x%x\nPID:0x%x\n",
3468 btdata->qca_dump.id_vendor, btdata->qca_dump.id_product);
3469 skb_put_data(skb, buf, strlen(buf));
3470
3471 snprintf(buf, sizeof(buf), "Lmp Subversion: 0x%x\n",
3472 hdev->lmp_subver);
3473 skb_put_data(skb, buf, strlen(buf));
3474 }
3475
btusb_coredump_qca(struct hci_dev * hdev)3476 static void btusb_coredump_qca(struct hci_dev *hdev)
3477 {
3478 int err;
3479 static const u8 param[] = { 0x26 };
3480
3481 err = __hci_cmd_send(hdev, 0xfc0c, 1, param);
3482 if (err < 0)
3483 bt_dev_err(hdev, "%s: triggle crash failed (%d)", __func__, err);
3484 }
3485
3486 /*
3487 * ==0: not a dump pkt.
3488 * < 0: fails to handle a dump pkt
3489 * > 0: otherwise.
3490 */
handle_dump_pkt_qca(struct hci_dev * hdev,struct sk_buff * skb)3491 static int handle_dump_pkt_qca(struct hci_dev *hdev, struct sk_buff *skb)
3492 {
3493 int ret = 1;
3494 u8 pkt_type;
3495 u8 *sk_ptr;
3496 unsigned int sk_len;
3497 u16 seqno;
3498 u32 dump_size;
3499
3500 struct hci_event_hdr *event_hdr;
3501 struct hci_acl_hdr *acl_hdr;
3502 struct qca_dump_hdr *dump_hdr;
3503 struct btusb_data *btdata = hci_get_drvdata(hdev);
3504 struct usb_device *udev = btdata->udev;
3505
3506 pkt_type = hci_skb_pkt_type(skb);
3507 sk_ptr = skb->data;
3508 sk_len = skb->len;
3509
3510 if (pkt_type == HCI_ACLDATA_PKT) {
3511 acl_hdr = hci_acl_hdr(skb);
3512 if (le16_to_cpu(acl_hdr->handle) != QCA_MEMDUMP_ACL_HANDLE)
3513 return 0;
3514 sk_ptr += HCI_ACL_HDR_SIZE;
3515 sk_len -= HCI_ACL_HDR_SIZE;
3516 event_hdr = (struct hci_event_hdr *)sk_ptr;
3517 } else {
3518 event_hdr = hci_event_hdr(skb);
3519 }
3520
3521 if ((event_hdr->evt != HCI_VENDOR_PKT)
3522 || (event_hdr->plen != (sk_len - HCI_EVENT_HDR_SIZE)))
3523 return 0;
3524
3525 sk_ptr += HCI_EVENT_HDR_SIZE;
3526 sk_len -= HCI_EVENT_HDR_SIZE;
3527
3528 dump_hdr = (struct qca_dump_hdr *)sk_ptr;
3529 if ((sk_len < offsetof(struct qca_dump_hdr, data))
3530 || (dump_hdr->vse_class != QCA_MEMDUMP_VSE_CLASS)
3531 || (dump_hdr->msg_type != QCA_MEMDUMP_MSG_TYPE))
3532 return 0;
3533
3534 /*it is dump pkt now*/
3535 seqno = le16_to_cpu(dump_hdr->seqno);
3536 if (seqno == 0) {
3537 set_bit(BTUSB_HW_SSR_ACTIVE, &btdata->flags);
3538 dump_size = le32_to_cpu(dump_hdr->ram_dump_size);
3539 if (!dump_size || (dump_size > QCA_MEMDUMP_SIZE_MAX)) {
3540 ret = -EILSEQ;
3541 bt_dev_err(hdev, "Invalid memdump size(%u)",
3542 dump_size);
3543 goto out;
3544 }
3545
3546 ret = hci_devcd_init(hdev, dump_size);
3547 if (ret < 0) {
3548 bt_dev_err(hdev, "memdump init error(%d)", ret);
3549 goto out;
3550 }
3551
3552 btdata->qca_dump.ram_dump_size = dump_size;
3553 btdata->qca_dump.ram_dump_seqno = 0;
3554 sk_ptr += offsetof(struct qca_dump_hdr, data0);
3555 sk_len -= offsetof(struct qca_dump_hdr, data0);
3556
3557 usb_disable_autosuspend(udev);
3558 bt_dev_info(hdev, "%s memdump size(%u)\n",
3559 (pkt_type == HCI_ACLDATA_PKT) ? "ACL" : "event",
3560 dump_size);
3561 } else {
3562 sk_ptr += offsetof(struct qca_dump_hdr, data);
3563 sk_len -= offsetof(struct qca_dump_hdr, data);
3564 }
3565
3566 if (!btdata->qca_dump.ram_dump_size) {
3567 ret = -EINVAL;
3568 bt_dev_err(hdev, "memdump is not active");
3569 goto out;
3570 }
3571
3572 if ((seqno > btdata->qca_dump.ram_dump_seqno + 1) && (seqno != QCA_LAST_SEQUENCE_NUM)) {
3573 dump_size = QCA_MEMDUMP_PKT_SIZE * (seqno - btdata->qca_dump.ram_dump_seqno - 1);
3574 hci_devcd_append_pattern(hdev, 0x0, dump_size);
3575 bt_dev_err(hdev,
3576 "expected memdump seqno(%u) is not received(%u)\n",
3577 btdata->qca_dump.ram_dump_seqno, seqno);
3578 btdata->qca_dump.ram_dump_seqno = seqno;
3579 kfree_skb(skb);
3580 return ret;
3581 }
3582
3583 skb_pull(skb, skb->len - sk_len);
3584 hci_devcd_append(hdev, skb);
3585 btdata->qca_dump.ram_dump_seqno++;
3586 if (seqno == QCA_LAST_SEQUENCE_NUM) {
3587 bt_dev_info(hdev,
3588 "memdump done: pkts(%u), total(%u)\n",
3589 btdata->qca_dump.ram_dump_seqno, btdata->qca_dump.ram_dump_size);
3590
3591 hci_devcd_complete(hdev);
3592 goto out;
3593 }
3594 return ret;
3595
3596 out:
3597 if (btdata->qca_dump.ram_dump_size)
3598 usb_enable_autosuspend(udev);
3599 btdata->qca_dump.ram_dump_size = 0;
3600 btdata->qca_dump.ram_dump_seqno = 0;
3601 clear_bit(BTUSB_HW_SSR_ACTIVE, &btdata->flags);
3602
3603 if (ret < 0)
3604 kfree_skb(skb);
3605 return ret;
3606 }
3607
btusb_recv_acl_qca(struct hci_dev * hdev,struct sk_buff * skb)3608 static int btusb_recv_acl_qca(struct hci_dev *hdev, struct sk_buff *skb)
3609 {
3610 if (handle_dump_pkt_qca(hdev, skb))
3611 return 0;
3612 return hci_recv_frame(hdev, skb);
3613 }
3614
btusb_recv_evt_qca(struct hci_dev * hdev,struct sk_buff * skb)3615 static int btusb_recv_evt_qca(struct hci_dev *hdev, struct sk_buff *skb)
3616 {
3617 if (handle_dump_pkt_qca(hdev, skb))
3618 return 0;
3619 return hci_recv_frame(hdev, skb);
3620 }
3621
3622
3623 #define QCA_DFU_PACKET_LEN 4096
3624
3625 #define QCA_GET_TARGET_VERSION 0x09
3626 #define QCA_CHECK_STATUS 0x05
3627 #define QCA_DFU_DOWNLOAD 0x01
3628
3629 #define QCA_SYSCFG_UPDATED 0x40
3630 #define QCA_PATCH_UPDATED 0x80
3631 #define QCA_DFU_TIMEOUT 3000
3632 #define QCA_FLAG_MULTI_NVM 0x80
3633 #define QCA_BT_RESET_WAIT_MS 100
3634
3635 #define WCN6855_2_0_RAM_VERSION_GF 0x400c1200
3636 #define WCN6855_2_1_RAM_VERSION_GF 0x400c1211
3637
3638 struct qca_version {
3639 __le32 rom_version;
3640 __le32 patch_version;
3641 __le32 ram_version;
3642 __u8 chip_id;
3643 __u8 platform_id;
3644 __le16 flag;
3645 __u8 reserved[4];
3646 } __packed;
3647
3648 struct qca_rampatch_version {
3649 __le16 rom_version_high;
3650 __le16 rom_version_low;
3651 __le16 patch_version;
3652 } __packed;
3653
3654 struct qca_device_info {
3655 u32 rom_version;
3656 u8 rampatch_hdr; /* length of header in rampatch */
3657 u8 nvm_hdr; /* length of header in NVM */
3658 u8 ver_offset; /* offset of version structure in rampatch */
3659 };
3660
3661 static const struct qca_device_info qca_devices_table[] = {
3662 { 0x00000100, 20, 4, 8 }, /* Rome 1.0 */
3663 { 0x00000101, 20, 4, 8 }, /* Rome 1.1 */
3664 { 0x00000200, 28, 4, 16 }, /* Rome 2.0 */
3665 { 0x00000201, 28, 4, 16 }, /* Rome 2.1 */
3666 { 0x00000300, 28, 4, 16 }, /* Rome 3.0 */
3667 { 0x00000302, 28, 4, 16 }, /* Rome 3.2 */
3668 { 0x00130100, 40, 4, 16 }, /* WCN6855 1.0 */
3669 { 0x00130200, 40, 4, 16 }, /* WCN6855 2.0 */
3670 { 0x00130201, 40, 4, 16 }, /* WCN6855 2.1 */
3671 { 0x00190200, 40, 4, 16 }, /* WCN785x 2.0 */
3672 };
3673
btusb_qca_send_vendor_req(struct usb_device * udev,u8 request,void * data,u16 size)3674 static int btusb_qca_send_vendor_req(struct usb_device *udev, u8 request,
3675 void *data, u16 size)
3676 {
3677 int pipe, err;
3678 u8 *buf;
3679
3680 buf = kmalloc(size, GFP_KERNEL);
3681 if (!buf)
3682 return -ENOMEM;
3683
3684 /* Found some of USB hosts have IOT issues with ours so that we should
3685 * not wait until HCI layer is ready.
3686 */
3687 pipe = usb_rcvctrlpipe(udev, 0);
3688 err = usb_control_msg(udev, pipe, request, USB_TYPE_VENDOR | USB_DIR_IN,
3689 0, 0, buf, size, USB_CTRL_SET_TIMEOUT);
3690 if (err < 0) {
3691 dev_err(&udev->dev, "Failed to access otp area (%d)", err);
3692 goto done;
3693 }
3694
3695 memcpy(data, buf, size);
3696
3697 done:
3698 kfree(buf);
3699
3700 return err;
3701 }
3702
btusb_setup_qca_download_fw(struct hci_dev * hdev,const struct firmware * firmware,size_t hdr_size)3703 static int btusb_setup_qca_download_fw(struct hci_dev *hdev,
3704 const struct firmware *firmware,
3705 size_t hdr_size)
3706 {
3707 struct btusb_data *btdata = hci_get_drvdata(hdev);
3708 struct usb_device *udev = btdata->udev;
3709 size_t count, size, sent = 0;
3710 int pipe, len, err;
3711 u8 *buf;
3712
3713 buf = kmalloc(QCA_DFU_PACKET_LEN, GFP_KERNEL);
3714 if (!buf)
3715 return -ENOMEM;
3716
3717 count = firmware->size;
3718
3719 size = min_t(size_t, count, hdr_size);
3720 memcpy(buf, firmware->data, size);
3721
3722 /* USB patches should go down to controller through USB path
3723 * because binary format fits to go down through USB channel.
3724 * USB control path is for patching headers and USB bulk is for
3725 * patch body.
3726 */
3727 pipe = usb_sndctrlpipe(udev, 0);
3728 err = usb_control_msg(udev, pipe, QCA_DFU_DOWNLOAD, USB_TYPE_VENDOR,
3729 0, 0, buf, size, USB_CTRL_SET_TIMEOUT);
3730 if (err < 0) {
3731 bt_dev_err(hdev, "Failed to send headers (%d)", err);
3732 goto done;
3733 }
3734
3735 sent += size;
3736 count -= size;
3737
3738 /* ep2 need time to switch from function acl to function dfu,
3739 * so we add 20ms delay here.
3740 */
3741 msleep(20);
3742
3743 while (count) {
3744 size = min_t(size_t, count, QCA_DFU_PACKET_LEN);
3745
3746 memcpy(buf, firmware->data + sent, size);
3747
3748 pipe = usb_sndbulkpipe(udev, 0x02);
3749 err = usb_bulk_msg(udev, pipe, buf, size, &len,
3750 QCA_DFU_TIMEOUT);
3751 if (err < 0) {
3752 bt_dev_err(hdev, "Failed to send body at %zd of %zd (%d)",
3753 sent, firmware->size, err);
3754 break;
3755 }
3756
3757 if (size != len) {
3758 bt_dev_err(hdev, "Failed to get bulk buffer");
3759 err = -EILSEQ;
3760 break;
3761 }
3762
3763 sent += size;
3764 count -= size;
3765 }
3766
3767 done:
3768 kfree(buf);
3769 return err;
3770 }
3771
btusb_setup_qca_load_rampatch(struct hci_dev * hdev,struct qca_version * ver,const struct qca_device_info * info)3772 static int btusb_setup_qca_load_rampatch(struct hci_dev *hdev,
3773 struct qca_version *ver,
3774 const struct qca_device_info *info)
3775 {
3776 struct qca_rampatch_version *rver;
3777 const struct firmware *fw;
3778 u32 ver_rom, ver_patch, rver_rom;
3779 u16 rver_rom_low, rver_rom_high, rver_patch;
3780 char fwname[64];
3781 int err;
3782
3783 ver_rom = le32_to_cpu(ver->rom_version);
3784 ver_patch = le32_to_cpu(ver->patch_version);
3785
3786 snprintf(fwname, sizeof(fwname), "qca/rampatch_usb_%08x.bin", ver_rom);
3787
3788 err = request_firmware(&fw, fwname, &hdev->dev);
3789 if (err) {
3790 bt_dev_err(hdev, "failed to request rampatch file: %s (%d)",
3791 fwname, err);
3792 return err;
3793 }
3794
3795 bt_dev_info(hdev, "using rampatch file: %s", fwname);
3796
3797 rver = (struct qca_rampatch_version *)(fw->data + info->ver_offset);
3798 rver_rom_low = le16_to_cpu(rver->rom_version_low);
3799 rver_patch = le16_to_cpu(rver->patch_version);
3800
3801 if (ver_rom & ~0xffffU) {
3802 rver_rom_high = le16_to_cpu(rver->rom_version_high);
3803 rver_rom = rver_rom_high << 16 | rver_rom_low;
3804 } else {
3805 rver_rom = rver_rom_low;
3806 }
3807
3808 bt_dev_info(hdev, "QCA: patch rome 0x%x build 0x%x, "
3809 "firmware rome 0x%x build 0x%x",
3810 rver_rom, rver_patch, ver_rom, ver_patch);
3811
3812 if (rver_rom != ver_rom || rver_patch <= ver_patch) {
3813 bt_dev_err(hdev, "rampatch file version did not match with firmware");
3814 err = -EINVAL;
3815 goto done;
3816 }
3817
3818 err = btusb_setup_qca_download_fw(hdev, fw, info->rampatch_hdr);
3819
3820 done:
3821 release_firmware(fw);
3822
3823 return err;
3824 }
3825
btusb_generate_qca_nvm_name(char * fwname,size_t max_size,const struct qca_version * ver)3826 static void btusb_generate_qca_nvm_name(char *fwname, size_t max_size,
3827 const struct qca_version *ver)
3828 {
3829 u32 rom_version = le32_to_cpu(ver->rom_version);
3830 u16 flag = le16_to_cpu(ver->flag);
3831
3832 if (((flag >> 8) & 0xff) == QCA_FLAG_MULTI_NVM) {
3833 /* The board_id should be split into two bytes
3834 * The 1st byte is chip ID, and the 2nd byte is platform ID
3835 * For example, board ID 0x010A, 0x01 is platform ID. 0x0A is chip ID
3836 * we have several platforms, and platform IDs are continuously added
3837 * Platform ID:
3838 * 0x00 is for Mobile
3839 * 0x01 is for X86
3840 * 0x02 is for Automotive
3841 * 0x03 is for Consumer electronic
3842 */
3843 u16 board_id = (ver->chip_id << 8) + ver->platform_id;
3844 const char *variant;
3845
3846 switch (le32_to_cpu(ver->ram_version)) {
3847 case WCN6855_2_0_RAM_VERSION_GF:
3848 case WCN6855_2_1_RAM_VERSION_GF:
3849 variant = "_gf";
3850 break;
3851 default:
3852 variant = "";
3853 break;
3854 }
3855
3856 if (board_id == 0) {
3857 snprintf(fwname, max_size, "qca/nvm_usb_%08x%s.bin",
3858 rom_version, variant);
3859 } else {
3860 snprintf(fwname, max_size, "qca/nvm_usb_%08x%s_%04x.bin",
3861 rom_version, variant, board_id);
3862 }
3863 } else {
3864 snprintf(fwname, max_size, "qca/nvm_usb_%08x.bin",
3865 rom_version);
3866 }
3867
3868 }
3869
btusb_setup_qca_load_nvm(struct hci_dev * hdev,struct qca_version * ver,const struct qca_device_info * info)3870 static int btusb_setup_qca_load_nvm(struct hci_dev *hdev,
3871 struct qca_version *ver,
3872 const struct qca_device_info *info)
3873 {
3874 const struct firmware *fw;
3875 char fwname[64];
3876 int err;
3877
3878 btusb_generate_qca_nvm_name(fwname, sizeof(fwname), ver);
3879
3880 err = request_firmware(&fw, fwname, &hdev->dev);
3881 if (err) {
3882 bt_dev_err(hdev, "failed to request NVM file: %s (%d)",
3883 fwname, err);
3884 return err;
3885 }
3886
3887 bt_dev_info(hdev, "using NVM file: %s", fwname);
3888
3889 err = btusb_setup_qca_download_fw(hdev, fw, info->nvm_hdr);
3890
3891 release_firmware(fw);
3892
3893 return err;
3894 }
3895
3896 /* identify the ROM version and check whether patches are needed */
btusb_qca_need_patch(struct usb_device * udev)3897 static bool btusb_qca_need_patch(struct usb_device *udev)
3898 {
3899 struct qca_version ver;
3900
3901 if (btusb_qca_send_vendor_req(udev, QCA_GET_TARGET_VERSION, &ver,
3902 sizeof(ver)) < 0)
3903 return false;
3904 /* only low ROM versions need patches */
3905 return !(le32_to_cpu(ver.rom_version) & ~0xffffU);
3906 }
3907
btusb_setup_qca(struct hci_dev * hdev)3908 static int btusb_setup_qca(struct hci_dev *hdev)
3909 {
3910 struct btusb_data *btdata = hci_get_drvdata(hdev);
3911 struct usb_device *udev = btdata->udev;
3912 const struct qca_device_info *info = NULL;
3913 struct qca_version ver;
3914 u32 ver_rom;
3915 u8 status;
3916 int i, err;
3917
3918 err = btusb_qca_send_vendor_req(udev, QCA_GET_TARGET_VERSION, &ver,
3919 sizeof(ver));
3920 if (err < 0)
3921 return err;
3922
3923 ver_rom = le32_to_cpu(ver.rom_version);
3924
3925 for (i = 0; i < ARRAY_SIZE(qca_devices_table); i++) {
3926 if (ver_rom == qca_devices_table[i].rom_version)
3927 info = &qca_devices_table[i];
3928 }
3929 if (!info) {
3930 /* If the rom_version is not matched in the qca_devices_table
3931 * and the high ROM version is not zero, we assume this chip no
3932 * need to load the rampatch and nvm.
3933 */
3934 if (ver_rom & ~0xffffU)
3935 return 0;
3936
3937 bt_dev_err(hdev, "don't support firmware rome 0x%x", ver_rom);
3938 return -ENODEV;
3939 }
3940
3941 err = btusb_qca_send_vendor_req(udev, QCA_CHECK_STATUS, &status,
3942 sizeof(status));
3943 if (err < 0)
3944 return err;
3945
3946 if (!(status & QCA_PATCH_UPDATED)) {
3947 err = btusb_setup_qca_load_rampatch(hdev, &ver, info);
3948 if (err < 0)
3949 return err;
3950 }
3951
3952 err = btusb_qca_send_vendor_req(udev, QCA_GET_TARGET_VERSION, &ver,
3953 sizeof(ver));
3954 if (err < 0)
3955 return err;
3956
3957 btdata->qca_dump.fw_version = le32_to_cpu(ver.patch_version);
3958 btdata->qca_dump.controller_id = le32_to_cpu(ver.rom_version);
3959
3960 if (!(status & QCA_SYSCFG_UPDATED)) {
3961 err = btusb_setup_qca_load_nvm(hdev, &ver, info);
3962 if (err < 0)
3963 return err;
3964
3965 /* WCN6855 2.1 and later will reset to apply firmware downloaded here, so
3966 * wait ~100ms for reset Done then go ahead, otherwise, it maybe
3967 * cause potential enable failure.
3968 */
3969 if (info->rom_version >= 0x00130201)
3970 msleep(QCA_BT_RESET_WAIT_MS);
3971 }
3972
3973 /* Mark HCI_OP_ENHANCED_SETUP_SYNC_CONN as broken as it doesn't seem to
3974 * work with the likes of HSP/HFP mSBC.
3975 */
3976 set_bit(HCI_QUIRK_BROKEN_ENHANCED_SETUP_SYNC_CONN, &hdev->quirks);
3977
3978 return 0;
3979 }
3980
__set_diag_interface(struct hci_dev * hdev)3981 static inline int __set_diag_interface(struct hci_dev *hdev)
3982 {
3983 struct btusb_data *data = hci_get_drvdata(hdev);
3984 struct usb_interface *intf = data->diag;
3985 int i;
3986
3987 if (!data->diag)
3988 return -ENODEV;
3989
3990 data->diag_tx_ep = NULL;
3991 data->diag_rx_ep = NULL;
3992
3993 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
3994 struct usb_endpoint_descriptor *ep_desc;
3995
3996 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
3997
3998 if (!data->diag_tx_ep && usb_endpoint_is_bulk_out(ep_desc)) {
3999 data->diag_tx_ep = ep_desc;
4000 continue;
4001 }
4002
4003 if (!data->diag_rx_ep && usb_endpoint_is_bulk_in(ep_desc)) {
4004 data->diag_rx_ep = ep_desc;
4005 continue;
4006 }
4007 }
4008
4009 if (!data->diag_tx_ep || !data->diag_rx_ep) {
4010 bt_dev_err(hdev, "invalid diagnostic descriptors");
4011 return -ENODEV;
4012 }
4013
4014 return 0;
4015 }
4016
alloc_diag_urb(struct hci_dev * hdev,bool enable)4017 static struct urb *alloc_diag_urb(struct hci_dev *hdev, bool enable)
4018 {
4019 struct btusb_data *data = hci_get_drvdata(hdev);
4020 struct sk_buff *skb;
4021 struct urb *urb;
4022 unsigned int pipe;
4023
4024 if (!data->diag_tx_ep)
4025 return ERR_PTR(-ENODEV);
4026
4027 urb = usb_alloc_urb(0, GFP_KERNEL);
4028 if (!urb)
4029 return ERR_PTR(-ENOMEM);
4030
4031 skb = bt_skb_alloc(2, GFP_KERNEL);
4032 if (!skb) {
4033 usb_free_urb(urb);
4034 return ERR_PTR(-ENOMEM);
4035 }
4036
4037 skb_put_u8(skb, 0xf0);
4038 skb_put_u8(skb, enable);
4039
4040 pipe = usb_sndbulkpipe(data->udev, data->diag_tx_ep->bEndpointAddress);
4041
4042 usb_fill_bulk_urb(urb, data->udev, pipe,
4043 skb->data, skb->len, btusb_tx_complete, skb);
4044
4045 skb->dev = (void *)hdev;
4046
4047 return urb;
4048 }
4049
btusb_bcm_set_diag(struct hci_dev * hdev,bool enable)4050 static int btusb_bcm_set_diag(struct hci_dev *hdev, bool enable)
4051 {
4052 struct btusb_data *data = hci_get_drvdata(hdev);
4053 struct urb *urb;
4054
4055 if (!data->diag)
4056 return -ENODEV;
4057
4058 if (!test_bit(HCI_RUNNING, &hdev->flags))
4059 return -ENETDOWN;
4060
4061 urb = alloc_diag_urb(hdev, enable);
4062 if (IS_ERR(urb))
4063 return PTR_ERR(urb);
4064
4065 return submit_or_queue_tx_urb(hdev, urb);
4066 }
4067
4068 #ifdef CONFIG_PM
btusb_oob_wake_handler(int irq,void * priv)4069 static irqreturn_t btusb_oob_wake_handler(int irq, void *priv)
4070 {
4071 struct btusb_data *data = priv;
4072
4073 pm_wakeup_event(&data->udev->dev, 0);
4074 pm_system_wakeup();
4075
4076 /* Disable only if not already disabled (keep it balanced) */
4077 if (test_and_clear_bit(BTUSB_OOB_WAKE_ENABLED, &data->flags)) {
4078 disable_irq_nosync(irq);
4079 disable_irq_wake(irq);
4080 }
4081 return IRQ_HANDLED;
4082 }
4083
4084 static const struct of_device_id btusb_match_table[] = {
4085 { .compatible = "usb1286,204e" },
4086 { .compatible = "usbcf3,e300" }, /* QCA6174A */
4087 { .compatible = "usb4ca,301a" }, /* QCA6174A (Lite-On) */
4088 { }
4089 };
4090 MODULE_DEVICE_TABLE(of, btusb_match_table);
4091
4092 /* Use an oob wakeup pin? */
btusb_config_oob_wake(struct hci_dev * hdev)4093 static int btusb_config_oob_wake(struct hci_dev *hdev)
4094 {
4095 struct btusb_data *data = hci_get_drvdata(hdev);
4096 struct device *dev = &data->udev->dev;
4097 int irq, ret;
4098
4099 clear_bit(BTUSB_OOB_WAKE_ENABLED, &data->flags);
4100
4101 if (!of_match_device(btusb_match_table, dev))
4102 return 0;
4103
4104 /* Move on if no IRQ specified */
4105 irq = of_irq_get_byname(dev->of_node, "wakeup");
4106 if (irq <= 0) {
4107 bt_dev_dbg(hdev, "%s: no OOB Wakeup IRQ in DT", __func__);
4108 return 0;
4109 }
4110
4111 irq_set_status_flags(irq, IRQ_NOAUTOEN);
4112 ret = devm_request_irq(&hdev->dev, irq, btusb_oob_wake_handler,
4113 0, "OOB Wake-on-BT", data);
4114 if (ret) {
4115 bt_dev_err(hdev, "%s: IRQ request failed", __func__);
4116 return ret;
4117 }
4118
4119 ret = device_init_wakeup(dev, true);
4120 if (ret) {
4121 bt_dev_err(hdev, "%s: failed to init_wakeup", __func__);
4122 return ret;
4123 }
4124
4125 data->oob_wake_irq = irq;
4126 bt_dev_info(hdev, "OOB Wake-on-BT configured at IRQ %u", irq);
4127 return 0;
4128 }
4129 #endif
4130
btusb_check_needs_reset_resume(struct usb_interface * intf)4131 static void btusb_check_needs_reset_resume(struct usb_interface *intf)
4132 {
4133 if (dmi_check_system(btusb_needs_reset_resume_table))
4134 interface_to_usbdev(intf)->quirks |= USB_QUIRK_RESET_RESUME;
4135 }
4136
btusb_wakeup(struct hci_dev * hdev)4137 static bool btusb_wakeup(struct hci_dev *hdev)
4138 {
4139 struct btusb_data *data = hci_get_drvdata(hdev);
4140
4141 return device_may_wakeup(&data->udev->dev);
4142 }
4143
btusb_shutdown_qca(struct hci_dev * hdev)4144 static int btusb_shutdown_qca(struct hci_dev *hdev)
4145 {
4146 struct sk_buff *skb;
4147
4148 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
4149 if (IS_ERR(skb)) {
4150 bt_dev_err(hdev, "HCI reset during shutdown failed");
4151 return PTR_ERR(skb);
4152 }
4153 kfree_skb(skb);
4154
4155 return 0;
4156 }
4157
force_poll_sync_read(struct file * file,char __user * user_buf,size_t count,loff_t * ppos)4158 static ssize_t force_poll_sync_read(struct file *file, char __user *user_buf,
4159 size_t count, loff_t *ppos)
4160 {
4161 struct btusb_data *data = file->private_data;
4162 char buf[3];
4163
4164 buf[0] = data->poll_sync ? 'Y' : 'N';
4165 buf[1] = '\n';
4166 buf[2] = '\0';
4167 return simple_read_from_buffer(user_buf, count, ppos, buf, 2);
4168 }
4169
force_poll_sync_write(struct file * file,const char __user * user_buf,size_t count,loff_t * ppos)4170 static ssize_t force_poll_sync_write(struct file *file,
4171 const char __user *user_buf,
4172 size_t count, loff_t *ppos)
4173 {
4174 struct btusb_data *data = file->private_data;
4175 bool enable;
4176 int err;
4177
4178 err = kstrtobool_from_user(user_buf, count, &enable);
4179 if (err)
4180 return err;
4181
4182 /* Only allow changes while the adapter is down */
4183 if (test_bit(HCI_UP, &data->hdev->flags))
4184 return -EPERM;
4185
4186 if (data->poll_sync == enable)
4187 return -EALREADY;
4188
4189 data->poll_sync = enable;
4190
4191 return count;
4192 }
4193
4194 static const struct file_operations force_poll_sync_fops = {
4195 .open = simple_open,
4196 .read = force_poll_sync_read,
4197 .write = force_poll_sync_write,
4198 .llseek = default_llseek,
4199 };
4200
btusb_probe(struct usb_interface * intf,const struct usb_device_id * id)4201 static int btusb_probe(struct usb_interface *intf,
4202 const struct usb_device_id *id)
4203 {
4204 struct usb_endpoint_descriptor *ep_desc;
4205 struct gpio_desc *reset_gpio;
4206 struct btusb_data *data;
4207 struct hci_dev *hdev;
4208 unsigned ifnum_base;
4209 int i, err, priv_size;
4210
4211 BT_DBG("intf %p id %p", intf, id);
4212
4213 if ((id->driver_info & BTUSB_IFNUM_2) &&
4214 (intf->cur_altsetting->desc.bInterfaceNumber != 0) &&
4215 (intf->cur_altsetting->desc.bInterfaceNumber != 2))
4216 return -ENODEV;
4217
4218 ifnum_base = intf->cur_altsetting->desc.bInterfaceNumber;
4219
4220 if (!id->driver_info) {
4221 const struct usb_device_id *match;
4222
4223 match = usb_match_id(intf, quirks_table);
4224 if (match)
4225 id = match;
4226 }
4227
4228 if (id->driver_info == BTUSB_IGNORE)
4229 return -ENODEV;
4230
4231 if (id->driver_info & BTUSB_ATH3012) {
4232 struct usb_device *udev = interface_to_usbdev(intf);
4233
4234 /* Old firmware would otherwise let ath3k driver load
4235 * patch and sysconfig files
4236 */
4237 if (le16_to_cpu(udev->descriptor.bcdDevice) <= 0x0001 &&
4238 !btusb_qca_need_patch(udev))
4239 return -ENODEV;
4240 }
4241
4242 data = devm_kzalloc(&intf->dev, sizeof(*data), GFP_KERNEL);
4243 if (!data)
4244 return -ENOMEM;
4245
4246 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
4247 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
4248
4249 if (!data->intr_ep && usb_endpoint_is_int_in(ep_desc)) {
4250 data->intr_ep = ep_desc;
4251 continue;
4252 }
4253
4254 if (!data->bulk_tx_ep && usb_endpoint_is_bulk_out(ep_desc)) {
4255 data->bulk_tx_ep = ep_desc;
4256 continue;
4257 }
4258
4259 if (!data->bulk_rx_ep && usb_endpoint_is_bulk_in(ep_desc)) {
4260 data->bulk_rx_ep = ep_desc;
4261 continue;
4262 }
4263 }
4264
4265 if (!data->intr_ep || !data->bulk_tx_ep || !data->bulk_rx_ep)
4266 return -ENODEV;
4267
4268 if (id->driver_info & BTUSB_AMP) {
4269 data->cmdreq_type = USB_TYPE_CLASS | 0x01;
4270 data->cmdreq = 0x2b;
4271 } else {
4272 data->cmdreq_type = USB_TYPE_CLASS;
4273 data->cmdreq = 0x00;
4274 }
4275
4276 data->udev = interface_to_usbdev(intf);
4277 data->intf = intf;
4278
4279 INIT_WORK(&data->work, btusb_work);
4280 INIT_WORK(&data->waker, btusb_waker);
4281 INIT_DELAYED_WORK(&data->rx_work, btusb_rx_work);
4282
4283 skb_queue_head_init(&data->acl_q);
4284
4285 init_usb_anchor(&data->deferred);
4286 init_usb_anchor(&data->tx_anchor);
4287 spin_lock_init(&data->txlock);
4288
4289 init_usb_anchor(&data->intr_anchor);
4290 init_usb_anchor(&data->bulk_anchor);
4291 init_usb_anchor(&data->isoc_anchor);
4292 init_usb_anchor(&data->diag_anchor);
4293 init_usb_anchor(&data->ctrl_anchor);
4294 spin_lock_init(&data->rxlock);
4295
4296 priv_size = 0;
4297
4298 data->recv_event = hci_recv_frame;
4299 data->recv_bulk = btusb_recv_bulk;
4300
4301 if (id->driver_info & BTUSB_INTEL_COMBINED) {
4302 /* Allocate extra space for Intel device */
4303 priv_size += sizeof(struct btintel_data);
4304
4305 /* Override the rx handlers */
4306 data->recv_event = btintel_recv_event;
4307 data->recv_bulk = btusb_recv_bulk_intel;
4308 } else if (id->driver_info & BTUSB_REALTEK) {
4309 /* Allocate extra space for Realtek device */
4310 priv_size += sizeof(struct btrealtek_data);
4311
4312 data->recv_event = btusb_recv_event_realtek;
4313 } else if (id->driver_info & BTUSB_MEDIATEK) {
4314 /* Allocate extra space for Mediatek device */
4315 priv_size += sizeof(struct btmediatek_data);
4316 }
4317
4318 data->recv_acl = hci_recv_frame;
4319
4320 hdev = hci_alloc_dev_priv(priv_size);
4321 if (!hdev)
4322 return -ENOMEM;
4323
4324 hdev->bus = HCI_USB;
4325 hci_set_drvdata(hdev, data);
4326
4327 data->hdev = hdev;
4328
4329 SET_HCIDEV_DEV(hdev, &intf->dev);
4330
4331 reset_gpio = gpiod_get_optional(&data->udev->dev, "reset",
4332 GPIOD_OUT_LOW);
4333 if (IS_ERR(reset_gpio)) {
4334 err = PTR_ERR(reset_gpio);
4335 goto out_free_dev;
4336 } else if (reset_gpio) {
4337 data->reset_gpio = reset_gpio;
4338 }
4339
4340 hdev->open = btusb_open;
4341 hdev->close = btusb_close;
4342 hdev->flush = btusb_flush;
4343 hdev->send = btusb_send_frame;
4344 hdev->notify = btusb_notify;
4345 hdev->wakeup = btusb_wakeup;
4346
4347 #ifdef CONFIG_PM
4348 err = btusb_config_oob_wake(hdev);
4349 if (err)
4350 goto out_free_dev;
4351
4352 /* Marvell devices may need a specific chip configuration */
4353 if (id->driver_info & BTUSB_MARVELL && data->oob_wake_irq) {
4354 err = marvell_config_oob_wake(hdev);
4355 if (err)
4356 goto out_free_dev;
4357 }
4358 #endif
4359 if (id->driver_info & BTUSB_CW6622)
4360 set_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks);
4361
4362 if (id->driver_info & BTUSB_BCM2045)
4363 set_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks);
4364
4365 if (id->driver_info & BTUSB_BCM92035)
4366 hdev->setup = btusb_setup_bcm92035;
4367
4368 if (IS_ENABLED(CONFIG_BT_HCIBTUSB_BCM) &&
4369 (id->driver_info & BTUSB_BCM_PATCHRAM)) {
4370 hdev->manufacturer = 15;
4371 hdev->setup = btbcm_setup_patchram;
4372 hdev->set_diag = btusb_bcm_set_diag;
4373 hdev->set_bdaddr = btbcm_set_bdaddr;
4374
4375 /* Broadcom LM_DIAG Interface numbers are hardcoded */
4376 data->diag = usb_ifnum_to_if(data->udev, ifnum_base + 2);
4377 }
4378
4379 if (IS_ENABLED(CONFIG_BT_HCIBTUSB_BCM) &&
4380 (id->driver_info & BTUSB_BCM_APPLE)) {
4381 hdev->manufacturer = 15;
4382 hdev->setup = btbcm_setup_apple;
4383 hdev->set_diag = btusb_bcm_set_diag;
4384
4385 /* Broadcom LM_DIAG Interface numbers are hardcoded */
4386 data->diag = usb_ifnum_to_if(data->udev, ifnum_base + 2);
4387 }
4388
4389 /* Combined Intel Device setup to support multiple setup routine */
4390 if (id->driver_info & BTUSB_INTEL_COMBINED) {
4391 err = btintel_configure_setup(hdev, btusb_driver.name);
4392 if (err)
4393 goto out_free_dev;
4394
4395 /* Transport specific configuration */
4396 hdev->send = btusb_send_frame_intel;
4397 hdev->cmd_timeout = btusb_intel_cmd_timeout;
4398
4399 if (id->driver_info & BTUSB_INTEL_NO_WBS_SUPPORT)
4400 btintel_set_flag(hdev, INTEL_ROM_LEGACY_NO_WBS_SUPPORT);
4401
4402 if (id->driver_info & BTUSB_INTEL_BROKEN_INITIAL_NCMD)
4403 btintel_set_flag(hdev, INTEL_BROKEN_INITIAL_NCMD);
4404
4405 if (id->driver_info & BTUSB_INTEL_BROKEN_SHUTDOWN_LED)
4406 btintel_set_flag(hdev, INTEL_BROKEN_SHUTDOWN_LED);
4407 }
4408
4409 if (id->driver_info & BTUSB_MARVELL)
4410 hdev->set_bdaddr = btusb_set_bdaddr_marvell;
4411
4412 if (IS_ENABLED(CONFIG_BT_HCIBTUSB_MTK) &&
4413 (id->driver_info & BTUSB_MEDIATEK)) {
4414 hdev->setup = btusb_mtk_setup;
4415 hdev->shutdown = btusb_mtk_shutdown;
4416 hdev->manufacturer = 70;
4417 hdev->cmd_timeout = btmtk_reset_sync;
4418 hdev->set_bdaddr = btmtk_set_bdaddr;
4419 set_bit(HCI_QUIRK_BROKEN_ENHANCED_SETUP_SYNC_CONN, &hdev->quirks);
4420 set_bit(HCI_QUIRK_NON_PERSISTENT_SETUP, &hdev->quirks);
4421 data->recv_acl = btusb_recv_acl_mtk;
4422 }
4423
4424 if (id->driver_info & BTUSB_SWAVE) {
4425 set_bit(HCI_QUIRK_FIXUP_INQUIRY_MODE, &hdev->quirks);
4426 set_bit(HCI_QUIRK_BROKEN_LOCAL_COMMANDS, &hdev->quirks);
4427 }
4428
4429 if (id->driver_info & BTUSB_INTEL_BOOT) {
4430 hdev->manufacturer = 2;
4431 set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
4432 }
4433
4434 if (id->driver_info & BTUSB_ATH3012) {
4435 data->setup_on_usb = btusb_setup_qca;
4436 hdev->set_bdaddr = btusb_set_bdaddr_ath3012;
4437 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
4438 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
4439 }
4440
4441 if (id->driver_info & BTUSB_QCA_ROME) {
4442 data->setup_on_usb = btusb_setup_qca;
4443 hdev->shutdown = btusb_shutdown_qca;
4444 hdev->set_bdaddr = btusb_set_bdaddr_ath3012;
4445 hdev->cmd_timeout = btusb_qca_cmd_timeout;
4446 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
4447 btusb_check_needs_reset_resume(intf);
4448 }
4449
4450 if (id->driver_info & BTUSB_QCA_WCN6855) {
4451 data->qca_dump.id_vendor = id->idVendor;
4452 data->qca_dump.id_product = id->idProduct;
4453 data->recv_event = btusb_recv_evt_qca;
4454 data->recv_acl = btusb_recv_acl_qca;
4455 hci_devcd_register(hdev, btusb_coredump_qca, btusb_dump_hdr_qca, NULL);
4456 data->setup_on_usb = btusb_setup_qca;
4457 hdev->shutdown = btusb_shutdown_qca;
4458 hdev->set_bdaddr = btusb_set_bdaddr_wcn6855;
4459 hdev->cmd_timeout = btusb_qca_cmd_timeout;
4460 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
4461 hci_set_msft_opcode(hdev, 0xFD70);
4462 }
4463
4464 if (id->driver_info & BTUSB_AMP) {
4465 /* AMP controllers do not support SCO packets */
4466 data->isoc = NULL;
4467 } else {
4468 /* Interface orders are hardcoded in the specification */
4469 data->isoc = usb_ifnum_to_if(data->udev, ifnum_base + 1);
4470 data->isoc_ifnum = ifnum_base + 1;
4471 }
4472
4473 if (IS_ENABLED(CONFIG_BT_HCIBTUSB_RTL) &&
4474 (id->driver_info & BTUSB_REALTEK)) {
4475 btrtl_set_driver_name(hdev, btusb_driver.name);
4476 hdev->setup = btusb_setup_realtek;
4477 hdev->shutdown = btrtl_shutdown_realtek;
4478 hdev->cmd_timeout = btusb_rtl_cmd_timeout;
4479 hdev->hw_error = btusb_rtl_hw_error;
4480
4481 /* Realtek devices need to set remote wakeup on auto-suspend */
4482 set_bit(BTUSB_WAKEUP_AUTOSUSPEND, &data->flags);
4483 set_bit(BTUSB_USE_ALT3_FOR_WBS, &data->flags);
4484 }
4485
4486 if (id->driver_info & BTUSB_ACTIONS_SEMI) {
4487 /* Support is advertised, but not implemented */
4488 set_bit(HCI_QUIRK_BROKEN_ERR_DATA_REPORTING, &hdev->quirks);
4489 set_bit(HCI_QUIRK_BROKEN_READ_TRANSMIT_POWER, &hdev->quirks);
4490 set_bit(HCI_QUIRK_BROKEN_SET_RPA_TIMEOUT, &hdev->quirks);
4491 set_bit(HCI_QUIRK_BROKEN_EXT_SCAN, &hdev->quirks);
4492 set_bit(HCI_QUIRK_BROKEN_READ_ENC_KEY_SIZE, &hdev->quirks);
4493 set_bit(HCI_QUIRK_BROKEN_EXT_CREATE_CONN, &hdev->quirks);
4494 set_bit(HCI_QUIRK_BROKEN_WRITE_AUTH_PAYLOAD_TIMEOUT, &hdev->quirks);
4495 }
4496
4497 if (!reset)
4498 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
4499
4500 if (force_scofix || id->driver_info & BTUSB_WRONG_SCO_MTU) {
4501 if (!disable_scofix)
4502 set_bit(HCI_QUIRK_FIXUP_BUFFER_SIZE, &hdev->quirks);
4503 }
4504
4505 if (id->driver_info & BTUSB_BROKEN_ISOC)
4506 data->isoc = NULL;
4507
4508 if (id->driver_info & BTUSB_WIDEBAND_SPEECH)
4509 set_bit(HCI_QUIRK_WIDEBAND_SPEECH_SUPPORTED, &hdev->quirks);
4510
4511 if (id->driver_info & BTUSB_VALID_LE_STATES)
4512 set_bit(HCI_QUIRK_VALID_LE_STATES, &hdev->quirks);
4513
4514 if (id->driver_info & BTUSB_DIGIANSWER) {
4515 data->cmdreq_type = USB_TYPE_VENDOR;
4516 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
4517 }
4518
4519 if (id->driver_info & BTUSB_CSR) {
4520 struct usb_device *udev = data->udev;
4521 u16 bcdDevice = le16_to_cpu(udev->descriptor.bcdDevice);
4522
4523 /* Old firmware would otherwise execute USB reset */
4524 if (bcdDevice < 0x117)
4525 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
4526
4527 /* This must be set first in case we disable it for fakes */
4528 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
4529
4530 /* Fake CSR devices with broken commands */
4531 if (le16_to_cpu(udev->descriptor.idVendor) == 0x0a12 &&
4532 le16_to_cpu(udev->descriptor.idProduct) == 0x0001)
4533 hdev->setup = btusb_setup_csr;
4534 }
4535
4536 if (id->driver_info & BTUSB_SNIFFER) {
4537 struct usb_device *udev = data->udev;
4538
4539 /* New sniffer firmware has crippled HCI interface */
4540 if (le16_to_cpu(udev->descriptor.bcdDevice) > 0x997)
4541 set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
4542 }
4543
4544 if (id->driver_info & BTUSB_INTEL_BOOT) {
4545 /* A bug in the bootloader causes that interrupt interface is
4546 * only enabled after receiving SetInterface(0, AltSetting=0).
4547 */
4548 err = usb_set_interface(data->udev, 0, 0);
4549 if (err < 0) {
4550 BT_ERR("failed to set interface 0, alt 0 %d", err);
4551 goto out_free_dev;
4552 }
4553 }
4554
4555 if (data->isoc) {
4556 err = usb_driver_claim_interface(&btusb_driver,
4557 data->isoc, data);
4558 if (err < 0)
4559 goto out_free_dev;
4560 }
4561
4562 if (IS_ENABLED(CONFIG_BT_HCIBTUSB_BCM) && data->diag) {
4563 if (!usb_driver_claim_interface(&btusb_driver,
4564 data->diag, data))
4565 __set_diag_interface(hdev);
4566 else
4567 data->diag = NULL;
4568 }
4569
4570 if (enable_autosuspend)
4571 usb_enable_autosuspend(data->udev);
4572
4573 data->poll_sync = enable_poll_sync;
4574
4575 err = hci_register_dev(hdev);
4576 if (err < 0)
4577 goto out_free_dev;
4578
4579 usb_set_intfdata(intf, data);
4580
4581 debugfs_create_file("force_poll_sync", 0644, hdev->debugfs, data,
4582 &force_poll_sync_fops);
4583
4584 return 0;
4585
4586 out_free_dev:
4587 if (data->reset_gpio)
4588 gpiod_put(data->reset_gpio);
4589 hci_free_dev(hdev);
4590 return err;
4591 }
4592
btusb_disconnect(struct usb_interface * intf)4593 static void btusb_disconnect(struct usb_interface *intf)
4594 {
4595 struct btusb_data *data = usb_get_intfdata(intf);
4596 struct hci_dev *hdev;
4597
4598 BT_DBG("intf %p", intf);
4599
4600 if (!data)
4601 return;
4602
4603 hdev = data->hdev;
4604 usb_set_intfdata(data->intf, NULL);
4605
4606 if (data->isoc)
4607 usb_set_intfdata(data->isoc, NULL);
4608
4609 if (data->diag)
4610 usb_set_intfdata(data->diag, NULL);
4611
4612 hci_unregister_dev(hdev);
4613
4614 if (intf == data->intf) {
4615 if (data->isoc)
4616 usb_driver_release_interface(&btusb_driver, data->isoc);
4617 if (data->diag)
4618 usb_driver_release_interface(&btusb_driver, data->diag);
4619 } else if (intf == data->isoc) {
4620 if (data->diag)
4621 usb_driver_release_interface(&btusb_driver, data->diag);
4622 usb_driver_release_interface(&btusb_driver, data->intf);
4623 } else if (intf == data->diag) {
4624 usb_driver_release_interface(&btusb_driver, data->intf);
4625 if (data->isoc)
4626 usb_driver_release_interface(&btusb_driver, data->isoc);
4627 }
4628
4629 if (data->oob_wake_irq)
4630 device_init_wakeup(&data->udev->dev, false);
4631
4632 if (data->reset_gpio)
4633 gpiod_put(data->reset_gpio);
4634
4635 hci_free_dev(hdev);
4636 }
4637
4638 #ifdef CONFIG_PM
btusb_suspend(struct usb_interface * intf,pm_message_t message)4639 static int btusb_suspend(struct usb_interface *intf, pm_message_t message)
4640 {
4641 struct btusb_data *data = usb_get_intfdata(intf);
4642
4643 BT_DBG("intf %p", intf);
4644
4645 if (data->suspend_count++)
4646 return 0;
4647
4648 spin_lock_irq(&data->txlock);
4649 if (!(PMSG_IS_AUTO(message) && data->tx_in_flight)) {
4650 set_bit(BTUSB_SUSPENDING, &data->flags);
4651 spin_unlock_irq(&data->txlock);
4652 } else {
4653 spin_unlock_irq(&data->txlock);
4654 data->suspend_count--;
4655 return -EBUSY;
4656 }
4657
4658 cancel_work_sync(&data->work);
4659
4660 btusb_stop_traffic(data);
4661 usb_kill_anchored_urbs(&data->tx_anchor);
4662
4663 if (data->oob_wake_irq && device_may_wakeup(&data->udev->dev)) {
4664 set_bit(BTUSB_OOB_WAKE_ENABLED, &data->flags);
4665 enable_irq_wake(data->oob_wake_irq);
4666 enable_irq(data->oob_wake_irq);
4667 }
4668
4669 /* For global suspend, Realtek devices lose the loaded fw
4670 * in them. But for autosuspend, firmware should remain.
4671 * Actually, it depends on whether the usb host sends
4672 * set feature (enable wakeup) or not.
4673 */
4674 if (test_bit(BTUSB_WAKEUP_AUTOSUSPEND, &data->flags)) {
4675 if (PMSG_IS_AUTO(message) &&
4676 device_can_wakeup(&data->udev->dev))
4677 data->udev->do_remote_wakeup = 1;
4678 else if (!PMSG_IS_AUTO(message) &&
4679 !device_may_wakeup(&data->udev->dev)) {
4680 data->udev->do_remote_wakeup = 0;
4681 data->udev->reset_resume = 1;
4682 }
4683 }
4684
4685 return 0;
4686 }
4687
play_deferred(struct btusb_data * data)4688 static void play_deferred(struct btusb_data *data)
4689 {
4690 struct urb *urb;
4691 int err;
4692
4693 while ((urb = usb_get_from_anchor(&data->deferred))) {
4694 usb_anchor_urb(urb, &data->tx_anchor);
4695
4696 err = usb_submit_urb(urb, GFP_ATOMIC);
4697 if (err < 0) {
4698 if (err != -EPERM && err != -ENODEV)
4699 BT_ERR("%s urb %p submission failed (%d)",
4700 data->hdev->name, urb, -err);
4701 kfree(urb->setup_packet);
4702 usb_unanchor_urb(urb);
4703 usb_free_urb(urb);
4704 break;
4705 }
4706
4707 data->tx_in_flight++;
4708 usb_free_urb(urb);
4709 }
4710
4711 /* Cleanup the rest deferred urbs. */
4712 while ((urb = usb_get_from_anchor(&data->deferred))) {
4713 kfree(urb->setup_packet);
4714 usb_free_urb(urb);
4715 }
4716 }
4717
btusb_resume(struct usb_interface * intf)4718 static int btusb_resume(struct usb_interface *intf)
4719 {
4720 struct btusb_data *data = usb_get_intfdata(intf);
4721 struct hci_dev *hdev = data->hdev;
4722 int err = 0;
4723
4724 BT_DBG("intf %p", intf);
4725
4726 if (--data->suspend_count)
4727 return 0;
4728
4729 /* Disable only if not already disabled (keep it balanced) */
4730 if (test_and_clear_bit(BTUSB_OOB_WAKE_ENABLED, &data->flags)) {
4731 disable_irq(data->oob_wake_irq);
4732 disable_irq_wake(data->oob_wake_irq);
4733 }
4734
4735 if (!test_bit(HCI_RUNNING, &hdev->flags))
4736 goto done;
4737
4738 if (test_bit(BTUSB_INTR_RUNNING, &data->flags)) {
4739 err = btusb_submit_intr_urb(hdev, GFP_NOIO);
4740 if (err < 0) {
4741 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
4742 goto failed;
4743 }
4744 }
4745
4746 if (test_bit(BTUSB_BULK_RUNNING, &data->flags)) {
4747 err = btusb_submit_bulk_urb(hdev, GFP_NOIO);
4748 if (err < 0) {
4749 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
4750 goto failed;
4751 }
4752
4753 btusb_submit_bulk_urb(hdev, GFP_NOIO);
4754 }
4755
4756 if (test_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
4757 if (btusb_submit_isoc_urb(hdev, GFP_NOIO) < 0)
4758 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
4759 else
4760 btusb_submit_isoc_urb(hdev, GFP_NOIO);
4761 }
4762
4763 spin_lock_irq(&data->txlock);
4764 play_deferred(data);
4765 clear_bit(BTUSB_SUSPENDING, &data->flags);
4766 spin_unlock_irq(&data->txlock);
4767 schedule_work(&data->work);
4768
4769 return 0;
4770
4771 failed:
4772 usb_scuttle_anchored_urbs(&data->deferred);
4773 done:
4774 spin_lock_irq(&data->txlock);
4775 clear_bit(BTUSB_SUSPENDING, &data->flags);
4776 spin_unlock_irq(&data->txlock);
4777
4778 return err;
4779 }
4780 #endif
4781
4782 #ifdef CONFIG_DEV_COREDUMP
btusb_coredump(struct device * dev)4783 static void btusb_coredump(struct device *dev)
4784 {
4785 struct btusb_data *data = dev_get_drvdata(dev);
4786 struct hci_dev *hdev = data->hdev;
4787
4788 if (hdev->dump.coredump)
4789 hdev->dump.coredump(hdev);
4790 }
4791 #endif
4792
4793 static struct usb_driver btusb_driver = {
4794 .name = "btusb",
4795 .probe = btusb_probe,
4796 .disconnect = btusb_disconnect,
4797 #ifdef CONFIG_PM
4798 .suspend = btusb_suspend,
4799 .resume = btusb_resume,
4800 #endif
4801 .id_table = btusb_table,
4802 .supports_autosuspend = 1,
4803 .disable_hub_initiated_lpm = 1,
4804
4805 #ifdef CONFIG_DEV_COREDUMP
4806 .drvwrap = {
4807 .driver = {
4808 .coredump = btusb_coredump,
4809 },
4810 },
4811 #endif
4812 };
4813
4814 module_usb_driver(btusb_driver);
4815
4816 module_param(disable_scofix, bool, 0644);
4817 MODULE_PARM_DESC(disable_scofix, "Disable fixup of wrong SCO buffer size");
4818
4819 module_param(force_scofix, bool, 0644);
4820 MODULE_PARM_DESC(force_scofix, "Force fixup of wrong SCO buffers size");
4821
4822 module_param(enable_autosuspend, bool, 0644);
4823 MODULE_PARM_DESC(enable_autosuspend, "Enable USB autosuspend by default");
4824
4825 module_param(reset, bool, 0644);
4826 MODULE_PARM_DESC(reset, "Send HCI reset command on initialization");
4827
4828 MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
4829 MODULE_DESCRIPTION("Generic Bluetooth USB driver ver " VERSION);
4830 MODULE_VERSION(VERSION);
4831 MODULE_LICENSE("GPL");
4832