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