1 // SPDX-License-Identifier: GPL-2.0-or-later
4 * Generic Bluetooth USB driver
6 * Copyright (C) 2005-2008 Marcel Holtmann <marcel@holtmann.org>
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 <asm/unaligned.h>
21 #include <net/bluetooth/bluetooth.h>
22 #include <net/bluetooth/hci_core.h>
30 static bool disable_scofix;
31 static bool force_scofix;
32 static bool enable_autosuspend = IS_ENABLED(CONFIG_BT_HCIBTUSB_AUTOSUSPEND);
34 static bool reset = true;
36 static struct usb_driver btusb_driver;
38 #define BTUSB_IGNORE 0x01
39 #define BTUSB_DIGIANSWER 0x02
40 #define BTUSB_CSR 0x04
41 #define BTUSB_SNIFFER 0x08
42 #define BTUSB_BCM92035 0x10
43 #define BTUSB_BROKEN_ISOC 0x20
44 #define BTUSB_WRONG_SCO_MTU 0x40
45 #define BTUSB_ATH3012 0x80
46 #define BTUSB_INTEL_COMBINED 0x100
47 #define BTUSB_INTEL_BOOT 0x200
48 #define BTUSB_BCM_PATCHRAM 0x400
49 #define BTUSB_MARVELL 0x800
50 #define BTUSB_SWAVE 0x1000
51 #define BTUSB_AMP 0x4000
52 #define BTUSB_QCA_ROME 0x8000
53 #define BTUSB_BCM_APPLE 0x10000
54 #define BTUSB_REALTEK 0x20000
55 #define BTUSB_BCM2045 0x40000
56 #define BTUSB_IFNUM_2 0x80000
57 #define BTUSB_CW6622 0x100000
58 #define BTUSB_MEDIATEK 0x200000
59 #define BTUSB_WIDEBAND_SPEECH 0x400000
60 #define BTUSB_VALID_LE_STATES 0x800000
61 #define BTUSB_QCA_WCN6855 0x1000000
62 #define BTUSB_INTEL_BROKEN_SHUTDOWN_LED 0x2000000
63 #define BTUSB_INTEL_BROKEN_INITIAL_NCMD 0x4000000
64 #define BTUSB_INTEL_NO_WBS_SUPPORT 0x8000000
66 static const struct usb_device_id btusb_table[] = {
67 /* Generic Bluetooth USB device */
68 { USB_DEVICE_INFO(0xe0, 0x01, 0x01) },
70 /* Generic Bluetooth AMP device */
71 { USB_DEVICE_INFO(0xe0, 0x01, 0x04), .driver_info = BTUSB_AMP },
73 /* Generic Bluetooth USB interface */
74 { USB_INTERFACE_INFO(0xe0, 0x01, 0x01) },
76 /* Apple-specific (Broadcom) devices */
77 { USB_VENDOR_AND_INTERFACE_INFO(0x05ac, 0xff, 0x01, 0x01),
78 .driver_info = BTUSB_BCM_APPLE | BTUSB_IFNUM_2 },
80 /* MediaTek MT76x0E */
81 { USB_DEVICE(0x0e8d, 0x763f) },
83 /* Broadcom SoftSailing reporting vendor specific */
84 { USB_DEVICE(0x0a5c, 0x21e1) },
86 /* Apple MacBookPro 7,1 */
87 { USB_DEVICE(0x05ac, 0x8213) },
90 { USB_DEVICE(0x05ac, 0x8215) },
92 /* Apple MacBookPro6,2 */
93 { USB_DEVICE(0x05ac, 0x8218) },
95 /* Apple MacBookAir3,1, MacBookAir3,2 */
96 { USB_DEVICE(0x05ac, 0x821b) },
98 /* Apple MacBookAir4,1 */
99 { USB_DEVICE(0x05ac, 0x821f) },
101 /* Apple MacBookPro8,2 */
102 { USB_DEVICE(0x05ac, 0x821a) },
104 /* Apple MacMini5,1 */
105 { USB_DEVICE(0x05ac, 0x8281) },
107 /* AVM BlueFRITZ! USB v2.0 */
108 { USB_DEVICE(0x057c, 0x3800), .driver_info = BTUSB_SWAVE },
110 /* Bluetooth Ultraport Module from IBM */
111 { USB_DEVICE(0x04bf, 0x030a) },
113 /* ALPS Modules with non-standard id */
114 { USB_DEVICE(0x044e, 0x3001) },
115 { USB_DEVICE(0x044e, 0x3002) },
117 /* Ericsson with non-standard id */
118 { USB_DEVICE(0x0bdb, 0x1002) },
120 /* Canyon CN-BTU1 with HID interfaces */
121 { USB_DEVICE(0x0c10, 0x0000) },
123 /* Broadcom BCM20702B0 (Dynex/Insignia) */
124 { USB_DEVICE(0x19ff, 0x0239), .driver_info = BTUSB_BCM_PATCHRAM },
126 /* Broadcom BCM43142A0 (Foxconn/Lenovo) */
127 { USB_VENDOR_AND_INTERFACE_INFO(0x105b, 0xff, 0x01, 0x01),
128 .driver_info = BTUSB_BCM_PATCHRAM },
130 /* Broadcom BCM920703 (HTC Vive) */
131 { USB_VENDOR_AND_INTERFACE_INFO(0x0bb4, 0xff, 0x01, 0x01),
132 .driver_info = BTUSB_BCM_PATCHRAM },
134 /* Foxconn - Hon Hai */
135 { USB_VENDOR_AND_INTERFACE_INFO(0x0489, 0xff, 0x01, 0x01),
136 .driver_info = BTUSB_BCM_PATCHRAM },
138 /* Lite-On Technology - Broadcom based */
139 { USB_VENDOR_AND_INTERFACE_INFO(0x04ca, 0xff, 0x01, 0x01),
140 .driver_info = BTUSB_BCM_PATCHRAM },
142 /* Broadcom devices with vendor specific id */
143 { USB_VENDOR_AND_INTERFACE_INFO(0x0a5c, 0xff, 0x01, 0x01),
144 .driver_info = BTUSB_BCM_PATCHRAM },
146 /* ASUSTek Computer - Broadcom based */
147 { USB_VENDOR_AND_INTERFACE_INFO(0x0b05, 0xff, 0x01, 0x01),
148 .driver_info = BTUSB_BCM_PATCHRAM },
150 /* Belkin F8065bf - Broadcom based */
151 { USB_VENDOR_AND_INTERFACE_INFO(0x050d, 0xff, 0x01, 0x01),
152 .driver_info = BTUSB_BCM_PATCHRAM },
154 /* IMC Networks - Broadcom based */
155 { USB_VENDOR_AND_INTERFACE_INFO(0x13d3, 0xff, 0x01, 0x01),
156 .driver_info = BTUSB_BCM_PATCHRAM },
158 /* Dell Computer - Broadcom based */
159 { USB_VENDOR_AND_INTERFACE_INFO(0x413c, 0xff, 0x01, 0x01),
160 .driver_info = BTUSB_BCM_PATCHRAM },
162 /* Toshiba Corp - Broadcom based */
163 { USB_VENDOR_AND_INTERFACE_INFO(0x0930, 0xff, 0x01, 0x01),
164 .driver_info = BTUSB_BCM_PATCHRAM },
166 /* Intel Bluetooth USB Bootloader (RAM module) */
167 { USB_DEVICE(0x8087, 0x0a5a),
168 .driver_info = BTUSB_INTEL_BOOT | BTUSB_BROKEN_ISOC },
170 { } /* Terminating entry */
173 MODULE_DEVICE_TABLE(usb, btusb_table);
175 static const struct usb_device_id blacklist_table[] = {
176 /* CSR BlueCore devices */
177 { USB_DEVICE(0x0a12, 0x0001), .driver_info = BTUSB_CSR },
179 /* Broadcom BCM2033 without firmware */
180 { USB_DEVICE(0x0a5c, 0x2033), .driver_info = BTUSB_IGNORE },
182 /* Broadcom BCM2045 devices */
183 { USB_DEVICE(0x0a5c, 0x2045), .driver_info = BTUSB_BCM2045 },
185 /* Atheros 3011 with sflash firmware */
186 { USB_DEVICE(0x0489, 0xe027), .driver_info = BTUSB_IGNORE },
187 { USB_DEVICE(0x0489, 0xe03d), .driver_info = BTUSB_IGNORE },
188 { USB_DEVICE(0x04f2, 0xaff1), .driver_info = BTUSB_IGNORE },
189 { USB_DEVICE(0x0930, 0x0215), .driver_info = BTUSB_IGNORE },
190 { USB_DEVICE(0x0cf3, 0x3002), .driver_info = BTUSB_IGNORE },
191 { USB_DEVICE(0x0cf3, 0xe019), .driver_info = BTUSB_IGNORE },
192 { USB_DEVICE(0x13d3, 0x3304), .driver_info = BTUSB_IGNORE },
194 /* Atheros AR9285 Malbec with sflash firmware */
195 { USB_DEVICE(0x03f0, 0x311d), .driver_info = BTUSB_IGNORE },
197 /* Atheros 3012 with sflash firmware */
198 { USB_DEVICE(0x0489, 0xe04d), .driver_info = BTUSB_ATH3012 },
199 { USB_DEVICE(0x0489, 0xe04e), .driver_info = BTUSB_ATH3012 },
200 { USB_DEVICE(0x0489, 0xe056), .driver_info = BTUSB_ATH3012 },
201 { USB_DEVICE(0x0489, 0xe057), .driver_info = BTUSB_ATH3012 },
202 { USB_DEVICE(0x0489, 0xe05f), .driver_info = BTUSB_ATH3012 },
203 { USB_DEVICE(0x0489, 0xe076), .driver_info = BTUSB_ATH3012 },
204 { USB_DEVICE(0x0489, 0xe078), .driver_info = BTUSB_ATH3012 },
205 { USB_DEVICE(0x0489, 0xe095), .driver_info = BTUSB_ATH3012 },
206 { USB_DEVICE(0x04c5, 0x1330), .driver_info = BTUSB_ATH3012 },
207 { USB_DEVICE(0x04ca, 0x3004), .driver_info = BTUSB_ATH3012 },
208 { USB_DEVICE(0x04ca, 0x3005), .driver_info = BTUSB_ATH3012 },
209 { USB_DEVICE(0x04ca, 0x3006), .driver_info = BTUSB_ATH3012 },
210 { USB_DEVICE(0x04ca, 0x3007), .driver_info = BTUSB_ATH3012 },
211 { USB_DEVICE(0x04ca, 0x3008), .driver_info = BTUSB_ATH3012 },
212 { USB_DEVICE(0x04ca, 0x300b), .driver_info = BTUSB_ATH3012 },
213 { USB_DEVICE(0x04ca, 0x300d), .driver_info = BTUSB_ATH3012 },
214 { USB_DEVICE(0x04ca, 0x300f), .driver_info = BTUSB_ATH3012 },
215 { USB_DEVICE(0x04ca, 0x3010), .driver_info = BTUSB_ATH3012 },
216 { USB_DEVICE(0x04ca, 0x3014), .driver_info = BTUSB_ATH3012 },
217 { USB_DEVICE(0x04ca, 0x3018), .driver_info = BTUSB_ATH3012 },
218 { USB_DEVICE(0x0930, 0x0219), .driver_info = BTUSB_ATH3012 },
219 { USB_DEVICE(0x0930, 0x021c), .driver_info = BTUSB_ATH3012 },
220 { USB_DEVICE(0x0930, 0x0220), .driver_info = BTUSB_ATH3012 },
221 { USB_DEVICE(0x0930, 0x0227), .driver_info = BTUSB_ATH3012 },
222 { USB_DEVICE(0x0b05, 0x17d0), .driver_info = BTUSB_ATH3012 },
223 { USB_DEVICE(0x0cf3, 0x0036), .driver_info = BTUSB_ATH3012 },
224 { USB_DEVICE(0x0cf3, 0x3004), .driver_info = BTUSB_ATH3012 },
225 { USB_DEVICE(0x0cf3, 0x3008), .driver_info = BTUSB_ATH3012 },
226 { USB_DEVICE(0x0cf3, 0x311d), .driver_info = BTUSB_ATH3012 },
227 { USB_DEVICE(0x0cf3, 0x311e), .driver_info = BTUSB_ATH3012 },
228 { USB_DEVICE(0x0cf3, 0x311f), .driver_info = BTUSB_ATH3012 },
229 { USB_DEVICE(0x0cf3, 0x3121), .driver_info = BTUSB_ATH3012 },
230 { USB_DEVICE(0x0cf3, 0x817a), .driver_info = BTUSB_ATH3012 },
231 { USB_DEVICE(0x0cf3, 0x817b), .driver_info = BTUSB_ATH3012 },
232 { USB_DEVICE(0x0cf3, 0xe003), .driver_info = BTUSB_ATH3012 },
233 { USB_DEVICE(0x0cf3, 0xe004), .driver_info = BTUSB_ATH3012 },
234 { USB_DEVICE(0x0cf3, 0xe005), .driver_info = BTUSB_ATH3012 },
235 { USB_DEVICE(0x0cf3, 0xe006), .driver_info = BTUSB_ATH3012 },
236 { USB_DEVICE(0x13d3, 0x3362), .driver_info = BTUSB_ATH3012 },
237 { USB_DEVICE(0x13d3, 0x3375), .driver_info = BTUSB_ATH3012 },
238 { USB_DEVICE(0x13d3, 0x3393), .driver_info = BTUSB_ATH3012 },
239 { USB_DEVICE(0x13d3, 0x3395), .driver_info = BTUSB_ATH3012 },
240 { USB_DEVICE(0x13d3, 0x3402), .driver_info = BTUSB_ATH3012 },
241 { USB_DEVICE(0x13d3, 0x3408), .driver_info = BTUSB_ATH3012 },
242 { USB_DEVICE(0x13d3, 0x3423), .driver_info = BTUSB_ATH3012 },
243 { USB_DEVICE(0x13d3, 0x3432), .driver_info = BTUSB_ATH3012 },
244 { USB_DEVICE(0x13d3, 0x3472), .driver_info = BTUSB_ATH3012 },
245 { USB_DEVICE(0x13d3, 0x3474), .driver_info = BTUSB_ATH3012 },
246 { USB_DEVICE(0x13d3, 0x3487), .driver_info = BTUSB_ATH3012 },
247 { USB_DEVICE(0x13d3, 0x3490), .driver_info = BTUSB_ATH3012 },
249 /* Atheros AR5BBU12 with sflash firmware */
250 { USB_DEVICE(0x0489, 0xe02c), .driver_info = BTUSB_IGNORE },
252 /* Atheros AR5BBU12 with sflash firmware */
253 { USB_DEVICE(0x0489, 0xe036), .driver_info = BTUSB_ATH3012 },
254 { USB_DEVICE(0x0489, 0xe03c), .driver_info = BTUSB_ATH3012 },
256 /* QCA ROME chipset */
257 { USB_DEVICE(0x0cf3, 0x535b), .driver_info = BTUSB_QCA_ROME |
258 BTUSB_WIDEBAND_SPEECH },
259 { USB_DEVICE(0x0cf3, 0xe007), .driver_info = BTUSB_QCA_ROME |
260 BTUSB_WIDEBAND_SPEECH },
261 { USB_DEVICE(0x0cf3, 0xe009), .driver_info = BTUSB_QCA_ROME |
262 BTUSB_WIDEBAND_SPEECH },
263 { USB_DEVICE(0x0cf3, 0xe010), .driver_info = BTUSB_QCA_ROME |
264 BTUSB_WIDEBAND_SPEECH },
265 { USB_DEVICE(0x0cf3, 0xe300), .driver_info = BTUSB_QCA_ROME |
266 BTUSB_WIDEBAND_SPEECH },
267 { USB_DEVICE(0x0cf3, 0xe301), .driver_info = BTUSB_QCA_ROME |
268 BTUSB_WIDEBAND_SPEECH },
269 { USB_DEVICE(0x0cf3, 0xe360), .driver_info = BTUSB_QCA_ROME |
270 BTUSB_WIDEBAND_SPEECH },
271 { USB_DEVICE(0x0cf3, 0xe500), .driver_info = BTUSB_QCA_ROME |
272 BTUSB_WIDEBAND_SPEECH },
273 { USB_DEVICE(0x0489, 0xe092), .driver_info = BTUSB_QCA_ROME |
274 BTUSB_WIDEBAND_SPEECH },
275 { USB_DEVICE(0x0489, 0xe09f), .driver_info = BTUSB_QCA_ROME |
276 BTUSB_WIDEBAND_SPEECH },
277 { USB_DEVICE(0x0489, 0xe0a2), .driver_info = BTUSB_QCA_ROME |
278 BTUSB_WIDEBAND_SPEECH },
279 { USB_DEVICE(0x04ca, 0x3011), .driver_info = BTUSB_QCA_ROME |
280 BTUSB_WIDEBAND_SPEECH },
281 { USB_DEVICE(0x04ca, 0x3015), .driver_info = BTUSB_QCA_ROME |
282 BTUSB_WIDEBAND_SPEECH },
283 { USB_DEVICE(0x04ca, 0x3016), .driver_info = BTUSB_QCA_ROME |
284 BTUSB_WIDEBAND_SPEECH },
285 { USB_DEVICE(0x04ca, 0x301a), .driver_info = BTUSB_QCA_ROME |
286 BTUSB_WIDEBAND_SPEECH },
287 { USB_DEVICE(0x04ca, 0x3021), .driver_info = BTUSB_QCA_ROME |
288 BTUSB_WIDEBAND_SPEECH },
289 { USB_DEVICE(0x13d3, 0x3491), .driver_info = BTUSB_QCA_ROME |
290 BTUSB_WIDEBAND_SPEECH },
291 { USB_DEVICE(0x13d3, 0x3496), .driver_info = BTUSB_QCA_ROME |
292 BTUSB_WIDEBAND_SPEECH },
293 { USB_DEVICE(0x13d3, 0x3501), .driver_info = BTUSB_QCA_ROME |
294 BTUSB_WIDEBAND_SPEECH },
296 /* QCA WCN6855 chipset */
297 { USB_DEVICE(0x0cf3, 0xe600), .driver_info = BTUSB_QCA_WCN6855 |
298 BTUSB_WIDEBAND_SPEECH |
299 BTUSB_VALID_LE_STATES },
300 { USB_DEVICE(0x0489, 0xe0cc), .driver_info = BTUSB_QCA_WCN6855 |
301 BTUSB_WIDEBAND_SPEECH |
302 BTUSB_VALID_LE_STATES },
303 { USB_DEVICE(0x0489, 0xe0d6), .driver_info = BTUSB_QCA_WCN6855 |
304 BTUSB_WIDEBAND_SPEECH |
305 BTUSB_VALID_LE_STATES },
306 { USB_DEVICE(0x0489, 0xe0e3), .driver_info = BTUSB_QCA_WCN6855 |
307 BTUSB_WIDEBAND_SPEECH |
308 BTUSB_VALID_LE_STATES },
309 { USB_DEVICE(0x10ab, 0x9309), .driver_info = BTUSB_QCA_WCN6855 |
310 BTUSB_WIDEBAND_SPEECH |
311 BTUSB_VALID_LE_STATES },
312 { USB_DEVICE(0x10ab, 0x9409), .driver_info = BTUSB_QCA_WCN6855 |
313 BTUSB_WIDEBAND_SPEECH |
314 BTUSB_VALID_LE_STATES },
315 { USB_DEVICE(0x0489, 0xe0d0), .driver_info = BTUSB_QCA_WCN6855 |
316 BTUSB_WIDEBAND_SPEECH |
317 BTUSB_VALID_LE_STATES },
319 /* Broadcom BCM2035 */
320 { USB_DEVICE(0x0a5c, 0x2009), .driver_info = BTUSB_BCM92035 },
321 { USB_DEVICE(0x0a5c, 0x200a), .driver_info = BTUSB_WRONG_SCO_MTU },
322 { USB_DEVICE(0x0a5c, 0x2035), .driver_info = BTUSB_WRONG_SCO_MTU },
324 /* Broadcom BCM2045 */
325 { USB_DEVICE(0x0a5c, 0x2039), .driver_info = BTUSB_WRONG_SCO_MTU },
326 { USB_DEVICE(0x0a5c, 0x2101), .driver_info = BTUSB_WRONG_SCO_MTU },
328 /* IBM/Lenovo ThinkPad with Broadcom chip */
329 { USB_DEVICE(0x0a5c, 0x201e), .driver_info = BTUSB_WRONG_SCO_MTU },
330 { USB_DEVICE(0x0a5c, 0x2110), .driver_info = BTUSB_WRONG_SCO_MTU },
332 /* HP laptop with Broadcom chip */
333 { USB_DEVICE(0x03f0, 0x171d), .driver_info = BTUSB_WRONG_SCO_MTU },
335 /* Dell laptop with Broadcom chip */
336 { USB_DEVICE(0x413c, 0x8126), .driver_info = BTUSB_WRONG_SCO_MTU },
338 /* Dell Wireless 370 and 410 devices */
339 { USB_DEVICE(0x413c, 0x8152), .driver_info = BTUSB_WRONG_SCO_MTU },
340 { USB_DEVICE(0x413c, 0x8156), .driver_info = BTUSB_WRONG_SCO_MTU },
342 /* Belkin F8T012 and F8T013 devices */
343 { USB_DEVICE(0x050d, 0x0012), .driver_info = BTUSB_WRONG_SCO_MTU },
344 { USB_DEVICE(0x050d, 0x0013), .driver_info = BTUSB_WRONG_SCO_MTU },
346 /* Asus WL-BTD202 device */
347 { USB_DEVICE(0x0b05, 0x1715), .driver_info = BTUSB_WRONG_SCO_MTU },
349 /* Kensington Bluetooth USB adapter */
350 { USB_DEVICE(0x047d, 0x105e), .driver_info = BTUSB_WRONG_SCO_MTU },
352 /* RTX Telecom based adapters with buggy SCO support */
353 { USB_DEVICE(0x0400, 0x0807), .driver_info = BTUSB_BROKEN_ISOC },
354 { USB_DEVICE(0x0400, 0x080a), .driver_info = BTUSB_BROKEN_ISOC },
356 /* CONWISE Technology based adapters with buggy SCO support */
357 { USB_DEVICE(0x0e5e, 0x6622),
358 .driver_info = BTUSB_BROKEN_ISOC | BTUSB_CW6622},
360 /* Roper Class 1 Bluetooth Dongle (Silicon Wave based) */
361 { USB_DEVICE(0x1310, 0x0001), .driver_info = BTUSB_SWAVE },
363 /* Digianswer devices */
364 { USB_DEVICE(0x08fd, 0x0001), .driver_info = BTUSB_DIGIANSWER },
365 { USB_DEVICE(0x08fd, 0x0002), .driver_info = BTUSB_IGNORE },
367 /* CSR BlueCore Bluetooth Sniffer */
368 { USB_DEVICE(0x0a12, 0x0002),
369 .driver_info = BTUSB_SNIFFER | BTUSB_BROKEN_ISOC },
371 /* Frontline ComProbe Bluetooth Sniffer */
372 { USB_DEVICE(0x16d3, 0x0002),
373 .driver_info = BTUSB_SNIFFER | BTUSB_BROKEN_ISOC },
375 /* Marvell Bluetooth devices */
376 { USB_DEVICE(0x1286, 0x2044), .driver_info = BTUSB_MARVELL },
377 { USB_DEVICE(0x1286, 0x2046), .driver_info = BTUSB_MARVELL },
378 { USB_DEVICE(0x1286, 0x204e), .driver_info = BTUSB_MARVELL },
380 /* Intel Bluetooth devices */
381 { USB_DEVICE(0x8087, 0x0025), .driver_info = BTUSB_INTEL_COMBINED },
382 { USB_DEVICE(0x8087, 0x0026), .driver_info = BTUSB_INTEL_COMBINED },
383 { USB_DEVICE(0x8087, 0x0029), .driver_info = BTUSB_INTEL_COMBINED },
384 { USB_DEVICE(0x8087, 0x0032), .driver_info = BTUSB_INTEL_COMBINED },
385 { USB_DEVICE(0x8087, 0x0033), .driver_info = BTUSB_INTEL_COMBINED },
386 { USB_DEVICE(0x8087, 0x07da), .driver_info = BTUSB_CSR },
387 { USB_DEVICE(0x8087, 0x07dc), .driver_info = BTUSB_INTEL_COMBINED |
388 BTUSB_INTEL_NO_WBS_SUPPORT |
389 BTUSB_INTEL_BROKEN_INITIAL_NCMD |
390 BTUSB_INTEL_BROKEN_SHUTDOWN_LED },
391 { USB_DEVICE(0x8087, 0x0a2a), .driver_info = BTUSB_INTEL_COMBINED |
392 BTUSB_INTEL_NO_WBS_SUPPORT |
393 BTUSB_INTEL_BROKEN_SHUTDOWN_LED },
394 { USB_DEVICE(0x8087, 0x0a2b), .driver_info = BTUSB_INTEL_COMBINED },
395 { USB_DEVICE(0x8087, 0x0aa7), .driver_info = BTUSB_INTEL_COMBINED |
396 BTUSB_INTEL_BROKEN_SHUTDOWN_LED },
397 { USB_DEVICE(0x8087, 0x0aaa), .driver_info = BTUSB_INTEL_COMBINED },
399 /* Other Intel Bluetooth devices */
400 { USB_VENDOR_AND_INTERFACE_INFO(0x8087, 0xe0, 0x01, 0x01),
401 .driver_info = BTUSB_IGNORE },
403 /* Realtek 8822CE Bluetooth devices */
404 { USB_DEVICE(0x0bda, 0xb00c), .driver_info = BTUSB_REALTEK |
405 BTUSB_WIDEBAND_SPEECH },
406 { USB_DEVICE(0x0bda, 0xc822), .driver_info = BTUSB_REALTEK |
407 BTUSB_WIDEBAND_SPEECH },
409 /* Realtek 8852AE Bluetooth devices */
410 { USB_DEVICE(0x0bda, 0x2852), .driver_info = BTUSB_REALTEK |
411 BTUSB_WIDEBAND_SPEECH },
412 { USB_DEVICE(0x0bda, 0xc852), .driver_info = BTUSB_REALTEK |
413 BTUSB_WIDEBAND_SPEECH },
414 { USB_DEVICE(0x0bda, 0x385a), .driver_info = BTUSB_REALTEK |
415 BTUSB_WIDEBAND_SPEECH },
416 { USB_DEVICE(0x0bda, 0x4852), .driver_info = BTUSB_REALTEK |
417 BTUSB_WIDEBAND_SPEECH },
418 { USB_DEVICE(0x04c5, 0x165c), .driver_info = BTUSB_REALTEK |
419 BTUSB_WIDEBAND_SPEECH },
420 { USB_DEVICE(0x04ca, 0x4006), .driver_info = BTUSB_REALTEK |
421 BTUSB_WIDEBAND_SPEECH },
423 /* Realtek Bluetooth devices */
424 { USB_VENDOR_AND_INTERFACE_INFO(0x0bda, 0xe0, 0x01, 0x01),
425 .driver_info = BTUSB_REALTEK },
427 /* MediaTek Bluetooth devices */
428 { USB_VENDOR_AND_INTERFACE_INFO(0x0e8d, 0xe0, 0x01, 0x01),
429 .driver_info = BTUSB_MEDIATEK |
430 BTUSB_WIDEBAND_SPEECH |
431 BTUSB_VALID_LE_STATES },
433 /* Additional MediaTek MT7615E Bluetooth devices */
434 { USB_DEVICE(0x13d3, 0x3560), .driver_info = BTUSB_MEDIATEK},
436 /* Additional MediaTek MT7668 Bluetooth devices */
437 { USB_DEVICE(0x043e, 0x3109), .driver_info = BTUSB_MEDIATEK |
438 BTUSB_WIDEBAND_SPEECH |
439 BTUSB_VALID_LE_STATES },
441 /* Additional MediaTek MT7921 Bluetooth devices */
442 { USB_DEVICE(0x04ca, 0x3802), .driver_info = BTUSB_MEDIATEK |
443 BTUSB_WIDEBAND_SPEECH |
444 BTUSB_VALID_LE_STATES },
445 { USB_DEVICE(0x13d3, 0x3563), .driver_info = BTUSB_MEDIATEK |
446 BTUSB_WIDEBAND_SPEECH |
447 BTUSB_VALID_LE_STATES },
448 { USB_DEVICE(0x13d3, 0x3564), .driver_info = BTUSB_MEDIATEK |
449 BTUSB_WIDEBAND_SPEECH |
450 BTUSB_VALID_LE_STATES },
451 { USB_DEVICE(0x0489, 0xe0cd), .driver_info = BTUSB_MEDIATEK |
452 BTUSB_WIDEBAND_SPEECH |
453 BTUSB_VALID_LE_STATES },
455 /* MediaTek MT7922A Bluetooth devices */
456 { USB_DEVICE(0x0489, 0xe0d8), .driver_info = BTUSB_MEDIATEK |
457 BTUSB_WIDEBAND_SPEECH |
458 BTUSB_VALID_LE_STATES },
459 { USB_DEVICE(0x0489, 0xe0d9), .driver_info = BTUSB_MEDIATEK |
460 BTUSB_WIDEBAND_SPEECH |
461 BTUSB_VALID_LE_STATES },
463 /* Additional Realtek 8723AE Bluetooth devices */
464 { USB_DEVICE(0x0930, 0x021d), .driver_info = BTUSB_REALTEK },
465 { USB_DEVICE(0x13d3, 0x3394), .driver_info = BTUSB_REALTEK },
467 /* Additional Realtek 8723BE Bluetooth devices */
468 { USB_DEVICE(0x0489, 0xe085), .driver_info = BTUSB_REALTEK },
469 { USB_DEVICE(0x0489, 0xe08b), .driver_info = BTUSB_REALTEK },
470 { USB_DEVICE(0x13d3, 0x3410), .driver_info = BTUSB_REALTEK },
471 { USB_DEVICE(0x13d3, 0x3416), .driver_info = BTUSB_REALTEK },
472 { USB_DEVICE(0x13d3, 0x3459), .driver_info = BTUSB_REALTEK },
473 { USB_DEVICE(0x13d3, 0x3494), .driver_info = BTUSB_REALTEK },
475 /* Additional Realtek 8723BU Bluetooth devices */
476 { USB_DEVICE(0x7392, 0xa611), .driver_info = BTUSB_REALTEK },
478 /* Additional Realtek 8723DE Bluetooth devices */
479 { USB_DEVICE(0x0bda, 0xb009), .driver_info = BTUSB_REALTEK },
480 { USB_DEVICE(0x2ff8, 0xb011), .driver_info = BTUSB_REALTEK },
482 /* Additional Realtek 8761B Bluetooth devices */
483 { USB_DEVICE(0x2357, 0x0604), .driver_info = BTUSB_REALTEK |
484 BTUSB_WIDEBAND_SPEECH },
486 /* Additional Realtek 8761BU Bluetooth devices */
487 { USB_DEVICE(0x0b05, 0x190e), .driver_info = BTUSB_REALTEK |
488 BTUSB_WIDEBAND_SPEECH },
489 { USB_DEVICE(0x2550, 0x8761), .driver_info = BTUSB_REALTEK |
490 BTUSB_WIDEBAND_SPEECH },
492 /* Additional Realtek 8821AE Bluetooth devices */
493 { USB_DEVICE(0x0b05, 0x17dc), .driver_info = BTUSB_REALTEK },
494 { USB_DEVICE(0x13d3, 0x3414), .driver_info = BTUSB_REALTEK },
495 { USB_DEVICE(0x13d3, 0x3458), .driver_info = BTUSB_REALTEK },
496 { USB_DEVICE(0x13d3, 0x3461), .driver_info = BTUSB_REALTEK },
497 { USB_DEVICE(0x13d3, 0x3462), .driver_info = BTUSB_REALTEK },
499 /* Additional Realtek 8822BE Bluetooth devices */
500 { USB_DEVICE(0x13d3, 0x3526), .driver_info = BTUSB_REALTEK },
501 { USB_DEVICE(0x0b05, 0x185c), .driver_info = BTUSB_REALTEK },
503 /* Additional Realtek 8822CE Bluetooth devices */
504 { USB_DEVICE(0x04ca, 0x4005), .driver_info = BTUSB_REALTEK |
505 BTUSB_WIDEBAND_SPEECH },
506 { USB_DEVICE(0x04c5, 0x161f), .driver_info = BTUSB_REALTEK |
507 BTUSB_WIDEBAND_SPEECH },
508 { USB_DEVICE(0x0b05, 0x18ef), .driver_info = BTUSB_REALTEK |
509 BTUSB_WIDEBAND_SPEECH },
510 { USB_DEVICE(0x13d3, 0x3548), .driver_info = BTUSB_REALTEK |
511 BTUSB_WIDEBAND_SPEECH },
512 { USB_DEVICE(0x13d3, 0x3549), .driver_info = BTUSB_REALTEK |
513 BTUSB_WIDEBAND_SPEECH },
514 { USB_DEVICE(0x13d3, 0x3553), .driver_info = BTUSB_REALTEK |
515 BTUSB_WIDEBAND_SPEECH },
516 { USB_DEVICE(0x13d3, 0x3555), .driver_info = BTUSB_REALTEK |
517 BTUSB_WIDEBAND_SPEECH },
518 { USB_DEVICE(0x2ff8, 0x3051), .driver_info = BTUSB_REALTEK |
519 BTUSB_WIDEBAND_SPEECH },
520 { USB_DEVICE(0x1358, 0xc123), .driver_info = BTUSB_REALTEK |
521 BTUSB_WIDEBAND_SPEECH },
522 { USB_DEVICE(0x0bda, 0xc123), .driver_info = BTUSB_REALTEK |
523 BTUSB_WIDEBAND_SPEECH },
524 { USB_DEVICE(0x0cb5, 0xc547), .driver_info = BTUSB_REALTEK |
525 BTUSB_WIDEBAND_SPEECH },
527 /* Silicon Wave based devices */
528 { USB_DEVICE(0x0c10, 0x0000), .driver_info = BTUSB_SWAVE },
530 { } /* Terminating entry */
533 /* The Bluetooth USB module build into some devices needs to be reset on resume,
534 * this is a problem with the platform (likely shutting off all power) not with
535 * the module itself. So we use a DMI list to match known broken platforms.
537 static const struct dmi_system_id btusb_needs_reset_resume_table[] = {
539 /* Dell OptiPlex 3060 (QCA ROME device 0cf3:e007) */
541 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
542 DMI_MATCH(DMI_PRODUCT_NAME, "OptiPlex 3060"),
546 /* Dell XPS 9360 (QCA ROME device 0cf3:e300) */
548 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
549 DMI_MATCH(DMI_PRODUCT_NAME, "XPS 13 9360"),
553 /* Dell Inspiron 5565 (QCA ROME device 0cf3:e009) */
555 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
556 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 5565"),
562 #define BTUSB_MAX_ISOC_FRAMES 10
564 #define BTUSB_INTR_RUNNING 0
565 #define BTUSB_BULK_RUNNING 1
566 #define BTUSB_ISOC_RUNNING 2
567 #define BTUSB_SUSPENDING 3
568 #define BTUSB_DID_ISO_RESUME 4
569 #define BTUSB_BOOTLOADER 5
570 #define BTUSB_DOWNLOADING 6
571 #define BTUSB_FIRMWARE_LOADED 7
572 #define BTUSB_FIRMWARE_FAILED 8
573 #define BTUSB_BOOTING 9
574 #define BTUSB_DIAG_RUNNING 10
575 #define BTUSB_OOB_WAKE_ENABLED 11
576 #define BTUSB_HW_RESET_ACTIVE 12
577 #define BTUSB_TX_WAIT_VND_EVT 13
578 #define BTUSB_WAKEUP_AUTOSUSPEND 14
579 #define BTUSB_USE_ALT3_FOR_WBS 15
582 struct hci_dev *hdev;
583 struct usb_device *udev;
584 struct usb_interface *intf;
585 struct usb_interface *isoc;
586 struct usb_interface *diag;
591 struct work_struct work;
592 struct work_struct waker;
594 struct usb_anchor deferred;
595 struct usb_anchor tx_anchor;
599 struct usb_anchor intr_anchor;
600 struct usb_anchor bulk_anchor;
601 struct usb_anchor isoc_anchor;
602 struct usb_anchor diag_anchor;
603 struct usb_anchor ctrl_anchor;
606 struct sk_buff *evt_skb;
607 struct sk_buff *acl_skb;
608 struct sk_buff *sco_skb;
610 struct usb_endpoint_descriptor *intr_ep;
611 struct usb_endpoint_descriptor *bulk_tx_ep;
612 struct usb_endpoint_descriptor *bulk_rx_ep;
613 struct usb_endpoint_descriptor *isoc_tx_ep;
614 struct usb_endpoint_descriptor *isoc_rx_ep;
615 struct usb_endpoint_descriptor *diag_tx_ep;
616 struct usb_endpoint_descriptor *diag_rx_ep;
618 struct gpio_desc *reset_gpio;
623 unsigned int sco_num;
624 unsigned int air_mode;
625 bool usb_alt6_packet_flow;
629 int (*recv_event)(struct hci_dev *hdev, struct sk_buff *skb);
630 int (*recv_acl)(struct hci_dev *hdev, struct sk_buff *skb);
631 int (*recv_bulk)(struct btusb_data *data, void *buffer, int count);
633 int (*setup_on_usb)(struct hci_dev *hdev);
635 int oob_wake_irq; /* irq for out-of-band wake-on-bt */
636 unsigned cmd_timeout_cnt;
639 static void btusb_intel_cmd_timeout(struct hci_dev *hdev)
641 struct btusb_data *data = hci_get_drvdata(hdev);
642 struct gpio_desc *reset_gpio = data->reset_gpio;
644 if (++data->cmd_timeout_cnt < 5)
648 bt_dev_err(hdev, "No way to reset. Ignoring and continuing");
653 * Toggle the hard reset line if the platform provides one. The reset
654 * is going to yank the device off the USB and then replug. So doing
655 * once is enough. The cleanup is handled correctly on the way out
656 * (standard USB disconnect), and the new device is detected cleanly
657 * and bound to the driver again like it should be.
659 if (test_and_set_bit(BTUSB_HW_RESET_ACTIVE, &data->flags)) {
660 bt_dev_err(hdev, "last reset failed? Not resetting again");
664 bt_dev_err(hdev, "Initiating HW reset via gpio");
665 gpiod_set_value_cansleep(reset_gpio, 1);
667 gpiod_set_value_cansleep(reset_gpio, 0);
670 static void btusb_rtl_cmd_timeout(struct hci_dev *hdev)
672 struct btusb_data *data = hci_get_drvdata(hdev);
673 struct gpio_desc *reset_gpio = data->reset_gpio;
675 if (++data->cmd_timeout_cnt < 5)
679 bt_dev_err(hdev, "No gpio to reset Realtek device, ignoring");
683 /* Toggle the hard reset line. The Realtek device is going to
684 * yank itself off the USB and then replug. The cleanup is handled
685 * correctly on the way out (standard USB disconnect), and the new
686 * device is detected cleanly and bound to the driver again like
689 if (test_and_set_bit(BTUSB_HW_RESET_ACTIVE, &data->flags)) {
690 bt_dev_err(hdev, "last reset failed? Not resetting again");
694 bt_dev_err(hdev, "Reset Realtek device via gpio");
695 gpiod_set_value_cansleep(reset_gpio, 1);
697 gpiod_set_value_cansleep(reset_gpio, 0);
700 static void btusb_qca_cmd_timeout(struct hci_dev *hdev)
702 struct btusb_data *data = hci_get_drvdata(hdev);
705 if (++data->cmd_timeout_cnt < 5)
708 bt_dev_err(hdev, "Multiple cmd timeouts seen. Resetting usb device.");
709 /* This is not an unbalanced PM reference since the device will reset */
710 err = usb_autopm_get_interface(data->intf);
712 usb_queue_reset_device(data->intf);
714 bt_dev_err(hdev, "Failed usb_autopm_get_interface with %d", err);
717 static inline void btusb_free_frags(struct btusb_data *data)
721 spin_lock_irqsave(&data->rxlock, flags);
723 kfree_skb(data->evt_skb);
724 data->evt_skb = NULL;
726 kfree_skb(data->acl_skb);
727 data->acl_skb = NULL;
729 kfree_skb(data->sco_skb);
730 data->sco_skb = NULL;
732 spin_unlock_irqrestore(&data->rxlock, flags);
735 static int btusb_recv_intr(struct btusb_data *data, void *buffer, int count)
741 spin_lock_irqsave(&data->rxlock, flags);
748 skb = bt_skb_alloc(HCI_MAX_EVENT_SIZE, GFP_ATOMIC);
754 hci_skb_pkt_type(skb) = HCI_EVENT_PKT;
755 hci_skb_expect(skb) = HCI_EVENT_HDR_SIZE;
758 len = min_t(uint, hci_skb_expect(skb), count);
759 skb_put_data(skb, buffer, len);
763 hci_skb_expect(skb) -= len;
765 if (skb->len == HCI_EVENT_HDR_SIZE) {
766 /* Complete event header */
767 hci_skb_expect(skb) = hci_event_hdr(skb)->plen;
769 if (skb_tailroom(skb) < hci_skb_expect(skb)) {
778 if (!hci_skb_expect(skb)) {
780 data->recv_event(data->hdev, skb);
786 spin_unlock_irqrestore(&data->rxlock, flags);
791 static int btusb_recv_bulk(struct btusb_data *data, void *buffer, int count)
797 spin_lock_irqsave(&data->rxlock, flags);
804 skb = bt_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC);
810 hci_skb_pkt_type(skb) = HCI_ACLDATA_PKT;
811 hci_skb_expect(skb) = HCI_ACL_HDR_SIZE;
814 len = min_t(uint, hci_skb_expect(skb), count);
815 skb_put_data(skb, buffer, len);
819 hci_skb_expect(skb) -= len;
821 if (skb->len == HCI_ACL_HDR_SIZE) {
822 __le16 dlen = hci_acl_hdr(skb)->dlen;
824 /* Complete ACL header */
825 hci_skb_expect(skb) = __le16_to_cpu(dlen);
827 if (skb_tailroom(skb) < hci_skb_expect(skb)) {
836 if (!hci_skb_expect(skb)) {
838 data->recv_acl(data->hdev, skb);
844 spin_unlock_irqrestore(&data->rxlock, flags);
849 static int btusb_recv_isoc(struct btusb_data *data, void *buffer, int count)
855 spin_lock_irqsave(&data->rxlock, flags);
862 skb = bt_skb_alloc(HCI_MAX_SCO_SIZE, GFP_ATOMIC);
868 hci_skb_pkt_type(skb) = HCI_SCODATA_PKT;
869 hci_skb_expect(skb) = HCI_SCO_HDR_SIZE;
872 len = min_t(uint, hci_skb_expect(skb), count);
873 skb_put_data(skb, buffer, len);
877 hci_skb_expect(skb) -= len;
879 if (skb->len == HCI_SCO_HDR_SIZE) {
880 /* Complete SCO header */
881 hci_skb_expect(skb) = hci_sco_hdr(skb)->dlen;
883 if (skb_tailroom(skb) < hci_skb_expect(skb)) {
892 if (!hci_skb_expect(skb)) {
894 hci_recv_frame(data->hdev, skb);
900 spin_unlock_irqrestore(&data->rxlock, flags);
905 static void btusb_intr_complete(struct urb *urb)
907 struct hci_dev *hdev = urb->context;
908 struct btusb_data *data = hci_get_drvdata(hdev);
911 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
914 if (!test_bit(HCI_RUNNING, &hdev->flags))
917 if (urb->status == 0) {
918 hdev->stat.byte_rx += urb->actual_length;
920 if (btusb_recv_intr(data, urb->transfer_buffer,
921 urb->actual_length) < 0) {
922 bt_dev_err(hdev, "corrupted event packet");
925 } else if (urb->status == -ENOENT) {
926 /* Avoid suspend failed when usb_kill_urb */
930 if (!test_bit(BTUSB_INTR_RUNNING, &data->flags))
933 usb_mark_last_busy(data->udev);
934 usb_anchor_urb(urb, &data->intr_anchor);
936 err = usb_submit_urb(urb, GFP_ATOMIC);
938 /* -EPERM: urb is being killed;
939 * -ENODEV: device got disconnected
941 if (err != -EPERM && err != -ENODEV)
942 bt_dev_err(hdev, "urb %p failed to resubmit (%d)",
944 usb_unanchor_urb(urb);
948 static int btusb_submit_intr_urb(struct hci_dev *hdev, gfp_t mem_flags)
950 struct btusb_data *data = hci_get_drvdata(hdev);
956 BT_DBG("%s", hdev->name);
961 urb = usb_alloc_urb(0, mem_flags);
965 size = le16_to_cpu(data->intr_ep->wMaxPacketSize);
967 buf = kmalloc(size, mem_flags);
973 pipe = usb_rcvintpipe(data->udev, data->intr_ep->bEndpointAddress);
975 usb_fill_int_urb(urb, data->udev, pipe, buf, size,
976 btusb_intr_complete, hdev, data->intr_ep->bInterval);
978 urb->transfer_flags |= URB_FREE_BUFFER;
980 usb_anchor_urb(urb, &data->intr_anchor);
982 err = usb_submit_urb(urb, mem_flags);
984 if (err != -EPERM && err != -ENODEV)
985 bt_dev_err(hdev, "urb %p submission failed (%d)",
987 usb_unanchor_urb(urb);
995 static void btusb_bulk_complete(struct urb *urb)
997 struct hci_dev *hdev = urb->context;
998 struct btusb_data *data = hci_get_drvdata(hdev);
1001 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
1002 urb->actual_length);
1004 if (!test_bit(HCI_RUNNING, &hdev->flags))
1007 if (urb->status == 0) {
1008 hdev->stat.byte_rx += urb->actual_length;
1010 if (data->recv_bulk(data, urb->transfer_buffer,
1011 urb->actual_length) < 0) {
1012 bt_dev_err(hdev, "corrupted ACL packet");
1013 hdev->stat.err_rx++;
1015 } else if (urb->status == -ENOENT) {
1016 /* Avoid suspend failed when usb_kill_urb */
1020 if (!test_bit(BTUSB_BULK_RUNNING, &data->flags))
1023 usb_anchor_urb(urb, &data->bulk_anchor);
1024 usb_mark_last_busy(data->udev);
1026 err = usb_submit_urb(urb, GFP_ATOMIC);
1028 /* -EPERM: urb is being killed;
1029 * -ENODEV: device got disconnected
1031 if (err != -EPERM && err != -ENODEV)
1032 bt_dev_err(hdev, "urb %p failed to resubmit (%d)",
1034 usb_unanchor_urb(urb);
1038 static int btusb_submit_bulk_urb(struct hci_dev *hdev, gfp_t mem_flags)
1040 struct btusb_data *data = hci_get_drvdata(hdev);
1044 int err, size = HCI_MAX_FRAME_SIZE;
1046 BT_DBG("%s", hdev->name);
1048 if (!data->bulk_rx_ep)
1051 urb = usb_alloc_urb(0, mem_flags);
1055 buf = kmalloc(size, mem_flags);
1061 pipe = usb_rcvbulkpipe(data->udev, data->bulk_rx_ep->bEndpointAddress);
1063 usb_fill_bulk_urb(urb, data->udev, pipe, buf, size,
1064 btusb_bulk_complete, hdev);
1066 urb->transfer_flags |= URB_FREE_BUFFER;
1068 usb_mark_last_busy(data->udev);
1069 usb_anchor_urb(urb, &data->bulk_anchor);
1071 err = usb_submit_urb(urb, mem_flags);
1073 if (err != -EPERM && err != -ENODEV)
1074 bt_dev_err(hdev, "urb %p submission failed (%d)",
1076 usb_unanchor_urb(urb);
1084 static void btusb_isoc_complete(struct urb *urb)
1086 struct hci_dev *hdev = urb->context;
1087 struct btusb_data *data = hci_get_drvdata(hdev);
1090 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
1091 urb->actual_length);
1093 if (!test_bit(HCI_RUNNING, &hdev->flags))
1096 if (urb->status == 0) {
1097 for (i = 0; i < urb->number_of_packets; i++) {
1098 unsigned int offset = urb->iso_frame_desc[i].offset;
1099 unsigned int length = urb->iso_frame_desc[i].actual_length;
1101 if (urb->iso_frame_desc[i].status)
1104 hdev->stat.byte_rx += length;
1106 if (btusb_recv_isoc(data, urb->transfer_buffer + offset,
1108 bt_dev_err(hdev, "corrupted SCO packet");
1109 hdev->stat.err_rx++;
1112 } else if (urb->status == -ENOENT) {
1113 /* Avoid suspend failed when usb_kill_urb */
1117 if (!test_bit(BTUSB_ISOC_RUNNING, &data->flags))
1120 usb_anchor_urb(urb, &data->isoc_anchor);
1122 err = usb_submit_urb(urb, GFP_ATOMIC);
1124 /* -EPERM: urb is being killed;
1125 * -ENODEV: device got disconnected
1127 if (err != -EPERM && err != -ENODEV)
1128 bt_dev_err(hdev, "urb %p failed to resubmit (%d)",
1130 usb_unanchor_urb(urb);
1134 static inline void __fill_isoc_descriptor_msbc(struct urb *urb, int len,
1135 int mtu, struct btusb_data *data)
1138 unsigned int interval;
1140 BT_DBG("len %d mtu %d", len, mtu);
1142 /* For mSBC ALT 6 setting the host will send the packet at continuous
1143 * flow. As per core spec 5, vol 4, part B, table 2.1. For ALT setting
1144 * 6 the HCI PACKET INTERVAL should be 7.5ms for every usb packets.
1145 * To maintain the rate we send 63bytes of usb packets alternatively for
1146 * 7ms and 8ms to maintain the rate as 7.5ms.
1148 if (data->usb_alt6_packet_flow) {
1150 data->usb_alt6_packet_flow = false;
1153 data->usb_alt6_packet_flow = true;
1156 for (i = 0; i < interval; i++) {
1157 urb->iso_frame_desc[i].offset = offset;
1158 urb->iso_frame_desc[i].length = offset;
1161 if (len && i < BTUSB_MAX_ISOC_FRAMES) {
1162 urb->iso_frame_desc[i].offset = offset;
1163 urb->iso_frame_desc[i].length = len;
1167 urb->number_of_packets = i;
1170 static inline void __fill_isoc_descriptor(struct urb *urb, int len, int mtu)
1174 BT_DBG("len %d mtu %d", len, mtu);
1176 for (i = 0; i < BTUSB_MAX_ISOC_FRAMES && len >= mtu;
1177 i++, offset += mtu, len -= mtu) {
1178 urb->iso_frame_desc[i].offset = offset;
1179 urb->iso_frame_desc[i].length = mtu;
1182 if (len && i < BTUSB_MAX_ISOC_FRAMES) {
1183 urb->iso_frame_desc[i].offset = offset;
1184 urb->iso_frame_desc[i].length = len;
1188 urb->number_of_packets = i;
1191 static int btusb_submit_isoc_urb(struct hci_dev *hdev, gfp_t mem_flags)
1193 struct btusb_data *data = hci_get_drvdata(hdev);
1199 BT_DBG("%s", hdev->name);
1201 if (!data->isoc_rx_ep)
1204 urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, mem_flags);
1208 size = le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize) *
1209 BTUSB_MAX_ISOC_FRAMES;
1211 buf = kmalloc(size, mem_flags);
1217 pipe = usb_rcvisocpipe(data->udev, data->isoc_rx_ep->bEndpointAddress);
1219 usb_fill_int_urb(urb, data->udev, pipe, buf, size, btusb_isoc_complete,
1220 hdev, data->isoc_rx_ep->bInterval);
1222 urb->transfer_flags = URB_FREE_BUFFER | URB_ISO_ASAP;
1224 __fill_isoc_descriptor(urb, size,
1225 le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize));
1227 usb_anchor_urb(urb, &data->isoc_anchor);
1229 err = usb_submit_urb(urb, mem_flags);
1231 if (err != -EPERM && err != -ENODEV)
1232 bt_dev_err(hdev, "urb %p submission failed (%d)",
1234 usb_unanchor_urb(urb);
1242 static void btusb_diag_complete(struct urb *urb)
1244 struct hci_dev *hdev = urb->context;
1245 struct btusb_data *data = hci_get_drvdata(hdev);
1248 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
1249 urb->actual_length);
1251 if (urb->status == 0) {
1252 struct sk_buff *skb;
1254 skb = bt_skb_alloc(urb->actual_length, GFP_ATOMIC);
1256 skb_put_data(skb, urb->transfer_buffer,
1257 urb->actual_length);
1258 hci_recv_diag(hdev, skb);
1260 } else if (urb->status == -ENOENT) {
1261 /* Avoid suspend failed when usb_kill_urb */
1265 if (!test_bit(BTUSB_DIAG_RUNNING, &data->flags))
1268 usb_anchor_urb(urb, &data->diag_anchor);
1269 usb_mark_last_busy(data->udev);
1271 err = usb_submit_urb(urb, GFP_ATOMIC);
1273 /* -EPERM: urb is being killed;
1274 * -ENODEV: device got disconnected
1276 if (err != -EPERM && err != -ENODEV)
1277 bt_dev_err(hdev, "urb %p failed to resubmit (%d)",
1279 usb_unanchor_urb(urb);
1283 static int btusb_submit_diag_urb(struct hci_dev *hdev, gfp_t mem_flags)
1285 struct btusb_data *data = hci_get_drvdata(hdev);
1289 int err, size = HCI_MAX_FRAME_SIZE;
1291 BT_DBG("%s", hdev->name);
1293 if (!data->diag_rx_ep)
1296 urb = usb_alloc_urb(0, mem_flags);
1300 buf = kmalloc(size, mem_flags);
1306 pipe = usb_rcvbulkpipe(data->udev, data->diag_rx_ep->bEndpointAddress);
1308 usb_fill_bulk_urb(urb, data->udev, pipe, buf, size,
1309 btusb_diag_complete, hdev);
1311 urb->transfer_flags |= URB_FREE_BUFFER;
1313 usb_mark_last_busy(data->udev);
1314 usb_anchor_urb(urb, &data->diag_anchor);
1316 err = usb_submit_urb(urb, mem_flags);
1318 if (err != -EPERM && err != -ENODEV)
1319 bt_dev_err(hdev, "urb %p submission failed (%d)",
1321 usb_unanchor_urb(urb);
1329 static void btusb_tx_complete(struct urb *urb)
1331 struct sk_buff *skb = urb->context;
1332 struct hci_dev *hdev = (struct hci_dev *)skb->dev;
1333 struct btusb_data *data = hci_get_drvdata(hdev);
1334 unsigned long flags;
1336 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
1337 urb->actual_length);
1339 if (!test_bit(HCI_RUNNING, &hdev->flags))
1343 hdev->stat.byte_tx += urb->transfer_buffer_length;
1345 hdev->stat.err_tx++;
1348 spin_lock_irqsave(&data->txlock, flags);
1349 data->tx_in_flight--;
1350 spin_unlock_irqrestore(&data->txlock, flags);
1352 kfree(urb->setup_packet);
1357 static void btusb_isoc_tx_complete(struct urb *urb)
1359 struct sk_buff *skb = urb->context;
1360 struct hci_dev *hdev = (struct hci_dev *)skb->dev;
1362 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
1363 urb->actual_length);
1365 if (!test_bit(HCI_RUNNING, &hdev->flags))
1369 hdev->stat.byte_tx += urb->transfer_buffer_length;
1371 hdev->stat.err_tx++;
1374 kfree(urb->setup_packet);
1379 static int btusb_open(struct hci_dev *hdev)
1381 struct btusb_data *data = hci_get_drvdata(hdev);
1384 BT_DBG("%s", hdev->name);
1386 err = usb_autopm_get_interface(data->intf);
1390 /* Patching USB firmware files prior to starting any URBs of HCI path
1391 * It is more safe to use USB bulk channel for downloading USB patch
1393 if (data->setup_on_usb) {
1394 err = data->setup_on_usb(hdev);
1399 data->intf->needs_remote_wakeup = 1;
1401 if (test_and_set_bit(BTUSB_INTR_RUNNING, &data->flags))
1404 err = btusb_submit_intr_urb(hdev, GFP_KERNEL);
1408 err = btusb_submit_bulk_urb(hdev, GFP_KERNEL);
1410 usb_kill_anchored_urbs(&data->intr_anchor);
1414 set_bit(BTUSB_BULK_RUNNING, &data->flags);
1415 btusb_submit_bulk_urb(hdev, GFP_KERNEL);
1418 if (!btusb_submit_diag_urb(hdev, GFP_KERNEL))
1419 set_bit(BTUSB_DIAG_RUNNING, &data->flags);
1423 usb_autopm_put_interface(data->intf);
1427 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
1429 usb_autopm_put_interface(data->intf);
1433 static void btusb_stop_traffic(struct btusb_data *data)
1435 usb_kill_anchored_urbs(&data->intr_anchor);
1436 usb_kill_anchored_urbs(&data->bulk_anchor);
1437 usb_kill_anchored_urbs(&data->isoc_anchor);
1438 usb_kill_anchored_urbs(&data->diag_anchor);
1439 usb_kill_anchored_urbs(&data->ctrl_anchor);
1442 static int btusb_close(struct hci_dev *hdev)
1444 struct btusb_data *data = hci_get_drvdata(hdev);
1447 BT_DBG("%s", hdev->name);
1449 cancel_work_sync(&data->work);
1450 cancel_work_sync(&data->waker);
1452 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1453 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
1454 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
1455 clear_bit(BTUSB_DIAG_RUNNING, &data->flags);
1457 btusb_stop_traffic(data);
1458 btusb_free_frags(data);
1460 err = usb_autopm_get_interface(data->intf);
1464 data->intf->needs_remote_wakeup = 0;
1466 /* Enable remote wake up for auto-suspend */
1467 if (test_bit(BTUSB_WAKEUP_AUTOSUSPEND, &data->flags))
1468 data->intf->needs_remote_wakeup = 1;
1470 usb_autopm_put_interface(data->intf);
1473 usb_scuttle_anchored_urbs(&data->deferred);
1477 static int btusb_flush(struct hci_dev *hdev)
1479 struct btusb_data *data = hci_get_drvdata(hdev);
1481 BT_DBG("%s", hdev->name);
1483 usb_kill_anchored_urbs(&data->tx_anchor);
1484 btusb_free_frags(data);
1489 static struct urb *alloc_ctrl_urb(struct hci_dev *hdev, struct sk_buff *skb)
1491 struct btusb_data *data = hci_get_drvdata(hdev);
1492 struct usb_ctrlrequest *dr;
1496 urb = usb_alloc_urb(0, GFP_KERNEL);
1498 return ERR_PTR(-ENOMEM);
1500 dr = kmalloc(sizeof(*dr), GFP_KERNEL);
1503 return ERR_PTR(-ENOMEM);
1506 dr->bRequestType = data->cmdreq_type;
1507 dr->bRequest = data->cmdreq;
1510 dr->wLength = __cpu_to_le16(skb->len);
1512 pipe = usb_sndctrlpipe(data->udev, 0x00);
1514 usb_fill_control_urb(urb, data->udev, pipe, (void *)dr,
1515 skb->data, skb->len, btusb_tx_complete, skb);
1517 skb->dev = (void *)hdev;
1522 static struct urb *alloc_bulk_urb(struct hci_dev *hdev, struct sk_buff *skb)
1524 struct btusb_data *data = hci_get_drvdata(hdev);
1528 if (!data->bulk_tx_ep)
1529 return ERR_PTR(-ENODEV);
1531 urb = usb_alloc_urb(0, GFP_KERNEL);
1533 return ERR_PTR(-ENOMEM);
1535 pipe = usb_sndbulkpipe(data->udev, data->bulk_tx_ep->bEndpointAddress);
1537 usb_fill_bulk_urb(urb, data->udev, pipe,
1538 skb->data, skb->len, btusb_tx_complete, skb);
1540 skb->dev = (void *)hdev;
1545 static struct urb *alloc_isoc_urb(struct hci_dev *hdev, struct sk_buff *skb)
1547 struct btusb_data *data = hci_get_drvdata(hdev);
1551 if (!data->isoc_tx_ep)
1552 return ERR_PTR(-ENODEV);
1554 urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, GFP_KERNEL);
1556 return ERR_PTR(-ENOMEM);
1558 pipe = usb_sndisocpipe(data->udev, data->isoc_tx_ep->bEndpointAddress);
1560 usb_fill_int_urb(urb, data->udev, pipe,
1561 skb->data, skb->len, btusb_isoc_tx_complete,
1562 skb, data->isoc_tx_ep->bInterval);
1564 urb->transfer_flags = URB_ISO_ASAP;
1566 if (data->isoc_altsetting == 6)
1567 __fill_isoc_descriptor_msbc(urb, skb->len,
1568 le16_to_cpu(data->isoc_tx_ep->wMaxPacketSize),
1571 __fill_isoc_descriptor(urb, skb->len,
1572 le16_to_cpu(data->isoc_tx_ep->wMaxPacketSize));
1573 skb->dev = (void *)hdev;
1578 static int submit_tx_urb(struct hci_dev *hdev, struct urb *urb)
1580 struct btusb_data *data = hci_get_drvdata(hdev);
1583 usb_anchor_urb(urb, &data->tx_anchor);
1585 err = usb_submit_urb(urb, GFP_KERNEL);
1587 if (err != -EPERM && err != -ENODEV)
1588 bt_dev_err(hdev, "urb %p submission failed (%d)",
1590 kfree(urb->setup_packet);
1591 usb_unanchor_urb(urb);
1593 usb_mark_last_busy(data->udev);
1600 static int submit_or_queue_tx_urb(struct hci_dev *hdev, struct urb *urb)
1602 struct btusb_data *data = hci_get_drvdata(hdev);
1603 unsigned long flags;
1606 spin_lock_irqsave(&data->txlock, flags);
1607 suspending = test_bit(BTUSB_SUSPENDING, &data->flags);
1609 data->tx_in_flight++;
1610 spin_unlock_irqrestore(&data->txlock, flags);
1613 return submit_tx_urb(hdev, urb);
1615 usb_anchor_urb(urb, &data->deferred);
1616 schedule_work(&data->waker);
1622 static int btusb_send_frame(struct hci_dev *hdev, struct sk_buff *skb)
1626 BT_DBG("%s", hdev->name);
1628 switch (hci_skb_pkt_type(skb)) {
1629 case HCI_COMMAND_PKT:
1630 urb = alloc_ctrl_urb(hdev, skb);
1632 return PTR_ERR(urb);
1634 hdev->stat.cmd_tx++;
1635 return submit_or_queue_tx_urb(hdev, urb);
1637 case HCI_ACLDATA_PKT:
1638 urb = alloc_bulk_urb(hdev, skb);
1640 return PTR_ERR(urb);
1642 hdev->stat.acl_tx++;
1643 return submit_or_queue_tx_urb(hdev, urb);
1645 case HCI_SCODATA_PKT:
1646 if (hci_conn_num(hdev, SCO_LINK) < 1)
1649 urb = alloc_isoc_urb(hdev, skb);
1651 return PTR_ERR(urb);
1653 hdev->stat.sco_tx++;
1654 return submit_tx_urb(hdev, urb);
1660 static void btusb_notify(struct hci_dev *hdev, unsigned int evt)
1662 struct btusb_data *data = hci_get_drvdata(hdev);
1664 BT_DBG("%s evt %d", hdev->name, evt);
1666 if (hci_conn_num(hdev, SCO_LINK) != data->sco_num) {
1667 data->sco_num = hci_conn_num(hdev, SCO_LINK);
1668 data->air_mode = evt;
1669 schedule_work(&data->work);
1673 static inline int __set_isoc_interface(struct hci_dev *hdev, int altsetting)
1675 struct btusb_data *data = hci_get_drvdata(hdev);
1676 struct usb_interface *intf = data->isoc;
1677 struct usb_endpoint_descriptor *ep_desc;
1683 err = usb_set_interface(data->udev, data->isoc_ifnum, altsetting);
1685 bt_dev_err(hdev, "setting interface failed (%d)", -err);
1689 data->isoc_altsetting = altsetting;
1691 data->isoc_tx_ep = NULL;
1692 data->isoc_rx_ep = NULL;
1694 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
1695 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
1697 if (!data->isoc_tx_ep && usb_endpoint_is_isoc_out(ep_desc)) {
1698 data->isoc_tx_ep = ep_desc;
1702 if (!data->isoc_rx_ep && usb_endpoint_is_isoc_in(ep_desc)) {
1703 data->isoc_rx_ep = ep_desc;
1708 if (!data->isoc_tx_ep || !data->isoc_rx_ep) {
1709 bt_dev_err(hdev, "invalid SCO descriptors");
1716 static int btusb_switch_alt_setting(struct hci_dev *hdev, int new_alts)
1718 struct btusb_data *data = hci_get_drvdata(hdev);
1721 if (data->isoc_altsetting != new_alts) {
1722 unsigned long flags;
1724 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1725 usb_kill_anchored_urbs(&data->isoc_anchor);
1727 /* When isochronous alternate setting needs to be
1728 * changed, because SCO connection has been added
1729 * or removed, a packet fragment may be left in the
1730 * reassembling state. This could lead to wrongly
1731 * assembled fragments.
1733 * Clear outstanding fragment when selecting a new
1734 * alternate setting.
1736 spin_lock_irqsave(&data->rxlock, flags);
1737 kfree_skb(data->sco_skb);
1738 data->sco_skb = NULL;
1739 spin_unlock_irqrestore(&data->rxlock, flags);
1741 err = __set_isoc_interface(hdev, new_alts);
1746 if (!test_and_set_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
1747 if (btusb_submit_isoc_urb(hdev, GFP_KERNEL) < 0)
1748 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1750 btusb_submit_isoc_urb(hdev, GFP_KERNEL);
1756 static struct usb_host_interface *btusb_find_altsetting(struct btusb_data *data,
1759 struct usb_interface *intf = data->isoc;
1762 BT_DBG("Looking for Alt no :%d", alt);
1767 for (i = 0; i < intf->num_altsetting; i++) {
1768 if (intf->altsetting[i].desc.bAlternateSetting == alt)
1769 return &intf->altsetting[i];
1775 static void btusb_work(struct work_struct *work)
1777 struct btusb_data *data = container_of(work, struct btusb_data, work);
1778 struct hci_dev *hdev = data->hdev;
1782 if (data->sco_num > 0) {
1783 if (!test_bit(BTUSB_DID_ISO_RESUME, &data->flags)) {
1784 err = usb_autopm_get_interface(data->isoc ? data->isoc : data->intf);
1786 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1787 usb_kill_anchored_urbs(&data->isoc_anchor);
1791 set_bit(BTUSB_DID_ISO_RESUME, &data->flags);
1794 if (data->air_mode == HCI_NOTIFY_ENABLE_SCO_CVSD) {
1795 if (hdev->voice_setting & 0x0020) {
1796 static const int alts[3] = { 2, 4, 5 };
1798 new_alts = alts[data->sco_num - 1];
1800 new_alts = data->sco_num;
1802 } else if (data->air_mode == HCI_NOTIFY_ENABLE_SCO_TRANSP) {
1803 /* Bluetooth USB spec recommends alt 6 (63 bytes), but
1804 * many adapters do not support it. Alt 1 appears to
1805 * work for all adapters that do not have alt 6, and
1806 * which work with WBS at all. Some devices prefer
1807 * alt 3 (HCI payload >= 60 Bytes let air packet
1808 * data satisfy 60 bytes), requiring
1809 * MTU >= 3 (packets) * 25 (size) - 3 (headers) = 72
1810 * see also Core spec 5, vol 4, B 2.1.1 & Table 2.1.
1812 if (btusb_find_altsetting(data, 6))
1814 else if (btusb_find_altsetting(data, 3) &&
1815 hdev->sco_mtu >= 72 &&
1816 test_bit(BTUSB_USE_ALT3_FOR_WBS, &data->flags))
1822 if (btusb_switch_alt_setting(hdev, new_alts) < 0)
1823 bt_dev_err(hdev, "set USB alt:(%d) failed!", new_alts);
1825 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1826 usb_kill_anchored_urbs(&data->isoc_anchor);
1828 __set_isoc_interface(hdev, 0);
1829 if (test_and_clear_bit(BTUSB_DID_ISO_RESUME, &data->flags))
1830 usb_autopm_put_interface(data->isoc ? data->isoc : data->intf);
1834 static void btusb_waker(struct work_struct *work)
1836 struct btusb_data *data = container_of(work, struct btusb_data, waker);
1839 err = usb_autopm_get_interface(data->intf);
1843 usb_autopm_put_interface(data->intf);
1846 static int btusb_setup_bcm92035(struct hci_dev *hdev)
1848 struct sk_buff *skb;
1851 BT_DBG("%s", hdev->name);
1853 skb = __hci_cmd_sync(hdev, 0xfc3b, 1, &val, HCI_INIT_TIMEOUT);
1855 bt_dev_err(hdev, "BCM92035 command failed (%ld)", PTR_ERR(skb));
1862 static int btusb_setup_csr(struct hci_dev *hdev)
1864 struct btusb_data *data = hci_get_drvdata(hdev);
1865 u16 bcdDevice = le16_to_cpu(data->udev->descriptor.bcdDevice);
1866 struct hci_rp_read_local_version *rp;
1867 struct sk_buff *skb;
1868 bool is_fake = false;
1871 BT_DBG("%s", hdev->name);
1873 skb = __hci_cmd_sync(hdev, HCI_OP_READ_LOCAL_VERSION, 0, NULL,
1876 int err = PTR_ERR(skb);
1877 bt_dev_err(hdev, "CSR: Local version failed (%d)", err);
1881 if (skb->len != sizeof(struct hci_rp_read_local_version)) {
1882 bt_dev_err(hdev, "CSR: Local version length mismatch");
1887 rp = (struct hci_rp_read_local_version *)skb->data;
1889 /* Detect a wide host of Chinese controllers that aren't CSR.
1891 * Known fake bcdDevices: 0x0100, 0x0134, 0x1915, 0x2520, 0x7558, 0x8891
1893 * The main thing they have in common is that these are really popular low-cost
1894 * options that support newer Bluetooth versions but rely on heavy VID/PID
1895 * squatting of this poor old Bluetooth 1.1 device. Even sold as such.
1897 * We detect actual CSR devices by checking that the HCI manufacturer code
1898 * is Cambridge Silicon Radio (10) and ensuring that LMP sub-version and
1899 * HCI rev values always match. As they both store the firmware number.
1901 if (le16_to_cpu(rp->manufacturer) != 10 ||
1902 le16_to_cpu(rp->hci_rev) != le16_to_cpu(rp->lmp_subver))
1905 /* Known legit CSR firmware build numbers and their supported BT versions:
1906 * - 1.1 (0x1) -> 0x0073, 0x020d, 0x033c, 0x034e
1907 * - 1.2 (0x2) -> 0x04d9, 0x0529
1908 * - 2.0 (0x3) -> 0x07a6, 0x07ad, 0x0c5c
1909 * - 2.1 (0x4) -> 0x149c, 0x1735, 0x1899 (0x1899 is a BlueCore4-External)
1910 * - 4.0 (0x6) -> 0x1d86, 0x2031, 0x22bb
1912 * e.g. Real CSR dongles with LMP subversion 0x73 are old enough that
1913 * support BT 1.1 only; so it's a dead giveaway when some
1914 * third-party BT 4.0 dongle reuses it.
1916 else if (le16_to_cpu(rp->lmp_subver) <= 0x034e &&
1917 le16_to_cpu(rp->hci_ver) > BLUETOOTH_VER_1_1)
1920 else if (le16_to_cpu(rp->lmp_subver) <= 0x0529 &&
1921 le16_to_cpu(rp->hci_ver) > BLUETOOTH_VER_1_2)
1924 else if (le16_to_cpu(rp->lmp_subver) <= 0x0c5c &&
1925 le16_to_cpu(rp->hci_ver) > BLUETOOTH_VER_2_0)
1928 else if (le16_to_cpu(rp->lmp_subver) <= 0x1899 &&
1929 le16_to_cpu(rp->hci_ver) > BLUETOOTH_VER_2_1)
1932 else if (le16_to_cpu(rp->lmp_subver) <= 0x22bb &&
1933 le16_to_cpu(rp->hci_ver) > BLUETOOTH_VER_4_0)
1936 /* Other clones which beat all the above checks */
1937 else if (bcdDevice == 0x0134 &&
1938 le16_to_cpu(rp->lmp_subver) == 0x0c5c &&
1939 le16_to_cpu(rp->hci_ver) == BLUETOOTH_VER_2_0)
1943 bt_dev_warn(hdev, "CSR: Unbranded CSR clone detected; adding workarounds and force-suspending once...");
1945 /* Generally these clones have big discrepancies between
1946 * advertised features and what's actually supported.
1947 * Probably will need to be expanded in the future;
1948 * without these the controller will lock up.
1950 set_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks);
1951 set_bit(HCI_QUIRK_BROKEN_ERR_DATA_REPORTING, &hdev->quirks);
1953 /* Clear the reset quirk since this is not an actual
1954 * early Bluetooth 1.1 device from CSR.
1956 clear_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
1957 clear_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
1960 * Special workaround for these BT 4.0 chip clones, and potentially more:
1962 * - 0x0134: a Barrot 8041a02 (HCI rev: 0x1012 sub: 0x0810)
1963 * - 0x7558: IC markings FR3191AHAL 749H15143 (HCI rev/sub-version: 0x0709)
1965 * These controllers are really messed-up.
1967 * 1. Their bulk RX endpoint will never report any data unless
1968 * the device was suspended at least once (yes, really).
1969 * 2. They will not wakeup when autosuspended and receiving data
1970 * on their bulk RX endpoint from e.g. a keyboard or mouse
1971 * (IOW remote-wakeup support is broken for the bulk endpoint).
1973 * To fix 1. enable runtime-suspend, force-suspend the
1974 * HCI and then wake-it up by disabling runtime-suspend.
1976 * To fix 2. clear the HCI's can_wake flag, this way the HCI
1977 * will still be autosuspended when it is not open.
1981 * Because these are widespread problems we prefer generic solutions; so
1982 * apply this initialization quirk to every controller that gets here,
1983 * it should be harmless. The alternative is to not work at all.
1985 pm_runtime_allow(&data->udev->dev);
1987 ret = pm_runtime_suspend(&data->udev->dev);
1991 bt_dev_err(hdev, "CSR: Failed to suspend the device for our Barrot 8041a02 receive-issue workaround");
1993 pm_runtime_forbid(&data->udev->dev);
1995 device_set_wakeup_capable(&data->udev->dev, false);
1997 /* Re-enable autosuspend if this was requested */
1998 if (enable_autosuspend)
1999 usb_enable_autosuspend(data->udev);
2007 static int inject_cmd_complete(struct hci_dev *hdev, __u16 opcode)
2009 struct sk_buff *skb;
2010 struct hci_event_hdr *hdr;
2011 struct hci_ev_cmd_complete *evt;
2013 skb = bt_skb_alloc(sizeof(*hdr) + sizeof(*evt) + 1, GFP_KERNEL);
2017 hdr = skb_put(skb, sizeof(*hdr));
2018 hdr->evt = HCI_EV_CMD_COMPLETE;
2019 hdr->plen = sizeof(*evt) + 1;
2021 evt = skb_put(skb, sizeof(*evt));
2023 evt->opcode = cpu_to_le16(opcode);
2025 skb_put_u8(skb, 0x00);
2027 hci_skb_pkt_type(skb) = HCI_EVENT_PKT;
2029 return hci_recv_frame(hdev, skb);
2032 static int btusb_recv_bulk_intel(struct btusb_data *data, void *buffer,
2035 struct hci_dev *hdev = data->hdev;
2037 /* When the device is in bootloader mode, then it can send
2038 * events via the bulk endpoint. These events are treated the
2039 * same way as the ones received from the interrupt endpoint.
2041 if (btintel_test_flag(hdev, INTEL_BOOTLOADER))
2042 return btusb_recv_intr(data, buffer, count);
2044 return btusb_recv_bulk(data, buffer, count);
2047 static int btusb_recv_event_intel(struct hci_dev *hdev, struct sk_buff *skb)
2049 if (btintel_test_flag(hdev, INTEL_BOOTLOADER)) {
2050 struct hci_event_hdr *hdr = (void *)skb->data;
2052 if (skb->len > HCI_EVENT_HDR_SIZE && hdr->evt == 0xff &&
2054 const void *ptr = skb->data + HCI_EVENT_HDR_SIZE + 1;
2055 unsigned int len = skb->len - HCI_EVENT_HDR_SIZE - 1;
2057 switch (skb->data[2]) {
2059 /* When switching to the operational firmware
2060 * the device sends a vendor specific event
2061 * indicating that the bootup completed.
2063 btintel_bootup(hdev, ptr, len);
2066 /* When the firmware loading completes the
2067 * device sends out a vendor specific event
2068 * indicating the result of the firmware
2071 btintel_secure_send_result(hdev, ptr, len);
2077 return hci_recv_frame(hdev, skb);
2080 static int btusb_send_frame_intel(struct hci_dev *hdev, struct sk_buff *skb)
2084 BT_DBG("%s", hdev->name);
2086 switch (hci_skb_pkt_type(skb)) {
2087 case HCI_COMMAND_PKT:
2088 if (btintel_test_flag(hdev, INTEL_BOOTLOADER)) {
2089 struct hci_command_hdr *cmd = (void *)skb->data;
2090 __u16 opcode = le16_to_cpu(cmd->opcode);
2092 /* When in bootloader mode and the command 0xfc09
2093 * is received, it needs to be send down the
2094 * bulk endpoint. So allocate a bulk URB instead.
2096 if (opcode == 0xfc09)
2097 urb = alloc_bulk_urb(hdev, skb);
2099 urb = alloc_ctrl_urb(hdev, skb);
2101 /* When the 0xfc01 command is issued to boot into
2102 * the operational firmware, it will actually not
2103 * send a command complete event. To keep the flow
2104 * control working inject that event here.
2106 if (opcode == 0xfc01)
2107 inject_cmd_complete(hdev, opcode);
2109 urb = alloc_ctrl_urb(hdev, skb);
2112 return PTR_ERR(urb);
2114 hdev->stat.cmd_tx++;
2115 return submit_or_queue_tx_urb(hdev, urb);
2117 case HCI_ACLDATA_PKT:
2118 urb = alloc_bulk_urb(hdev, skb);
2120 return PTR_ERR(urb);
2122 hdev->stat.acl_tx++;
2123 return submit_or_queue_tx_urb(hdev, urb);
2125 case HCI_SCODATA_PKT:
2126 if (hci_conn_num(hdev, SCO_LINK) < 1)
2129 urb = alloc_isoc_urb(hdev, skb);
2131 return PTR_ERR(urb);
2133 hdev->stat.sco_tx++;
2134 return submit_tx_urb(hdev, urb);
2140 /* UHW CR mapping */
2141 #define MTK_BT_MISC 0x70002510
2142 #define MTK_BT_SUBSYS_RST 0x70002610
2143 #define MTK_UDMA_INT_STA_BT 0x74000024
2144 #define MTK_UDMA_INT_STA_BT1 0x74000308
2145 #define MTK_BT_WDT_STATUS 0x740003A0
2146 #define MTK_EP_RST_OPT 0x74011890
2147 #define MTK_EP_RST_IN_OUT_OPT 0x00010001
2148 #define MTK_BT_RST_DONE 0x00000100
2149 #define MTK_BT_RESET_WAIT_MS 100
2150 #define MTK_BT_RESET_NUM_TRIES 10
2151 #define FIRMWARE_MT7663 "mediatek/mt7663pr2h.bin"
2152 #define FIRMWARE_MT7668 "mediatek/mt7668pr2h.bin"
2154 #define HCI_WMT_MAX_EVENT_SIZE 64
2155 /* It is for mt79xx download rom patch*/
2156 #define MTK_FW_ROM_PATCH_HEADER_SIZE 32
2157 #define MTK_FW_ROM_PATCH_GD_SIZE 64
2158 #define MTK_FW_ROM_PATCH_SEC_MAP_SIZE 64
2159 #define MTK_SEC_MAP_COMMON_SIZE 12
2160 #define MTK_SEC_MAP_NEED_SEND_SIZE 52
2163 BTMTK_WMT_PATCH_DWNLD = 0x1,
2164 BTMTK_WMT_FUNC_CTRL = 0x6,
2165 BTMTK_WMT_RST = 0x7,
2166 BTMTK_WMT_SEMAPHORE = 0x17,
2171 BTMTK_WMT_PATCH_UNDONE,
2172 BTMTK_WMT_PATCH_PROGRESS,
2173 BTMTK_WMT_PATCH_DONE,
2174 BTMTK_WMT_ON_UNDONE,
2176 BTMTK_WMT_ON_PROGRESS,
2179 struct btmtk_wmt_hdr {
2186 struct btmtk_hci_wmt_cmd {
2187 struct btmtk_wmt_hdr hdr;
2191 struct btmtk_hci_wmt_evt {
2192 struct hci_event_hdr hhdr;
2193 struct btmtk_wmt_hdr whdr;
2196 struct btmtk_hci_wmt_evt_funcc {
2197 struct btmtk_hci_wmt_evt hwhdr;
2201 struct btmtk_tci_sleep {
2204 __le16 host_duration;
2206 u8 time_compensation;
2209 struct btmtk_hci_wmt_params {
2217 struct btmtk_patch_header {
2225 struct btmtk_global_desc {
2232 struct btmtk_section_map {
2237 __le32 u4SecSpec[13];
2244 __le32 dlmodecrctype;
2251 static void btusb_mtk_wmt_recv(struct urb *urb)
2253 struct hci_dev *hdev = urb->context;
2254 struct btusb_data *data = hci_get_drvdata(hdev);
2255 struct hci_event_hdr *hdr;
2256 struct sk_buff *skb;
2259 if (urb->status == 0 && urb->actual_length > 0) {
2260 hdev->stat.byte_rx += urb->actual_length;
2262 /* WMT event shouldn't be fragmented and the size should be
2263 * less than HCI_WMT_MAX_EVENT_SIZE.
2265 skb = bt_skb_alloc(HCI_WMT_MAX_EVENT_SIZE, GFP_ATOMIC);
2267 hdev->stat.err_rx++;
2268 kfree(urb->setup_packet);
2272 hci_skb_pkt_type(skb) = HCI_EVENT_PKT;
2273 skb_put_data(skb, urb->transfer_buffer, urb->actual_length);
2275 hdr = (void *)skb->data;
2276 /* Fix up the vendor event id with 0xff for vendor specific
2277 * instead of 0xe4 so that event send via monitoring socket can
2278 * be parsed properly.
2282 /* When someone waits for the WMT event, the skb is being cloned
2283 * and being processed the events from there then.
2285 if (test_bit(BTUSB_TX_WAIT_VND_EVT, &data->flags)) {
2286 data->evt_skb = skb_clone(skb, GFP_ATOMIC);
2287 if (!data->evt_skb) {
2289 kfree(urb->setup_packet);
2294 err = hci_recv_frame(hdev, skb);
2296 kfree_skb(data->evt_skb);
2297 data->evt_skb = NULL;
2298 kfree(urb->setup_packet);
2302 if (test_and_clear_bit(BTUSB_TX_WAIT_VND_EVT,
2304 /* Barrier to sync with other CPUs */
2305 smp_mb__after_atomic();
2306 wake_up_bit(&data->flags,
2307 BTUSB_TX_WAIT_VND_EVT);
2309 kfree(urb->setup_packet);
2311 } else if (urb->status == -ENOENT) {
2312 /* Avoid suspend failed when usb_kill_urb */
2316 usb_mark_last_busy(data->udev);
2318 /* The URB complete handler is still called with urb->actual_length = 0
2319 * when the event is not available, so we should keep re-submitting
2320 * URB until WMT event returns, Also, It's necessary to wait some time
2321 * between the two consecutive control URBs to relax the target device
2322 * to generate the event. Otherwise, the WMT event cannot return from
2323 * the device successfully.
2327 usb_anchor_urb(urb, &data->ctrl_anchor);
2328 err = usb_submit_urb(urb, GFP_ATOMIC);
2330 kfree(urb->setup_packet);
2331 /* -EPERM: urb is being killed;
2332 * -ENODEV: device got disconnected
2334 if (err != -EPERM && err != -ENODEV)
2335 bt_dev_err(hdev, "urb %p failed to resubmit (%d)",
2337 usb_unanchor_urb(urb);
2341 static int btusb_mtk_submit_wmt_recv_urb(struct hci_dev *hdev)
2343 struct btusb_data *data = hci_get_drvdata(hdev);
2344 struct usb_ctrlrequest *dr;
2350 urb = usb_alloc_urb(0, GFP_KERNEL);
2354 dr = kmalloc(sizeof(*dr), GFP_KERNEL);
2360 dr->bRequestType = USB_TYPE_VENDOR | USB_DIR_IN;
2362 dr->wIndex = cpu_to_le16(0);
2363 dr->wValue = cpu_to_le16(48);
2364 dr->wLength = cpu_to_le16(size);
2366 buf = kmalloc(size, GFP_KERNEL);
2373 pipe = usb_rcvctrlpipe(data->udev, 0);
2375 usb_fill_control_urb(urb, data->udev, pipe, (void *)dr,
2376 buf, size, btusb_mtk_wmt_recv, hdev);
2378 urb->transfer_flags |= URB_FREE_BUFFER;
2380 usb_anchor_urb(urb, &data->ctrl_anchor);
2381 err = usb_submit_urb(urb, GFP_KERNEL);
2383 if (err != -EPERM && err != -ENODEV)
2384 bt_dev_err(hdev, "urb %p submission failed (%d)",
2386 usb_unanchor_urb(urb);
2394 static int btusb_mtk_hci_wmt_sync(struct hci_dev *hdev,
2395 struct btmtk_hci_wmt_params *wmt_params)
2397 struct btusb_data *data = hci_get_drvdata(hdev);
2398 struct btmtk_hci_wmt_evt_funcc *wmt_evt_funcc;
2399 u32 hlen, status = BTMTK_WMT_INVALID;
2400 struct btmtk_hci_wmt_evt *wmt_evt;
2401 struct btmtk_hci_wmt_cmd *wc;
2402 struct btmtk_wmt_hdr *hdr;
2405 /* Send the WMT command and wait until the WMT event returns */
2406 hlen = sizeof(*hdr) + wmt_params->dlen;
2410 wc = kzalloc(hlen, GFP_KERNEL);
2416 hdr->op = wmt_params->op;
2417 hdr->dlen = cpu_to_le16(wmt_params->dlen + 1);
2418 hdr->flag = wmt_params->flag;
2419 memcpy(wc->data, wmt_params->data, wmt_params->dlen);
2421 set_bit(BTUSB_TX_WAIT_VND_EVT, &data->flags);
2423 err = __hci_cmd_send(hdev, 0xfc6f, hlen, wc);
2426 clear_bit(BTUSB_TX_WAIT_VND_EVT, &data->flags);
2430 /* Submit control IN URB on demand to process the WMT event */
2431 err = btusb_mtk_submit_wmt_recv_urb(hdev);
2435 /* The vendor specific WMT commands are all answered by a vendor
2436 * specific event and will have the Command Status or Command
2437 * Complete as with usual HCI command flow control.
2439 * After sending the command, wait for BTUSB_TX_WAIT_VND_EVT
2440 * state to be cleared. The driver specific event receive routine
2441 * will clear that state and with that indicate completion of the
2444 err = wait_on_bit_timeout(&data->flags, BTUSB_TX_WAIT_VND_EVT,
2445 TASK_INTERRUPTIBLE, HCI_INIT_TIMEOUT);
2446 if (err == -EINTR) {
2447 bt_dev_err(hdev, "Execution of wmt command interrupted");
2448 clear_bit(BTUSB_TX_WAIT_VND_EVT, &data->flags);
2453 bt_dev_err(hdev, "Execution of wmt command timed out");
2454 clear_bit(BTUSB_TX_WAIT_VND_EVT, &data->flags);
2459 /* Parse and handle the return WMT event */
2460 wmt_evt = (struct btmtk_hci_wmt_evt *)data->evt_skb->data;
2461 if (wmt_evt->whdr.op != hdr->op) {
2462 bt_dev_err(hdev, "Wrong op received %d expected %d",
2463 wmt_evt->whdr.op, hdr->op);
2468 switch (wmt_evt->whdr.op) {
2469 case BTMTK_WMT_SEMAPHORE:
2470 if (wmt_evt->whdr.flag == 2)
2471 status = BTMTK_WMT_PATCH_UNDONE;
2473 status = BTMTK_WMT_PATCH_DONE;
2475 case BTMTK_WMT_FUNC_CTRL:
2476 wmt_evt_funcc = (struct btmtk_hci_wmt_evt_funcc *)wmt_evt;
2477 if (be16_to_cpu(wmt_evt_funcc->status) == 0x404)
2478 status = BTMTK_WMT_ON_DONE;
2479 else if (be16_to_cpu(wmt_evt_funcc->status) == 0x420)
2480 status = BTMTK_WMT_ON_PROGRESS;
2482 status = BTMTK_WMT_ON_UNDONE;
2484 case BTMTK_WMT_PATCH_DWNLD:
2485 if (wmt_evt->whdr.flag == 2)
2486 status = BTMTK_WMT_PATCH_DONE;
2487 else if (wmt_evt->whdr.flag == 1)
2488 status = BTMTK_WMT_PATCH_PROGRESS;
2490 status = BTMTK_WMT_PATCH_UNDONE;
2494 if (wmt_params->status)
2495 *wmt_params->status = status;
2498 kfree_skb(data->evt_skb);
2499 data->evt_skb = NULL;
2505 static int btusb_mtk_setup_firmware_79xx(struct hci_dev *hdev, const char *fwname)
2507 struct btmtk_hci_wmt_params wmt_params;
2508 struct btmtk_global_desc *globaldesc = NULL;
2509 struct btmtk_section_map *sectionmap;
2510 const struct firmware *fw;
2512 const u8 *fw_bin_ptr;
2513 int err, dlen, i, status;
2514 u8 flag, first_block, retry;
2515 u32 section_num, dl_size, section_offset;
2518 err = request_firmware(&fw, fwname, &hdev->dev);
2520 bt_dev_err(hdev, "Failed to load firmware file (%d)", err);
2525 fw_bin_ptr = fw_ptr;
2526 globaldesc = (struct btmtk_global_desc *)(fw_ptr + MTK_FW_ROM_PATCH_HEADER_SIZE);
2527 section_num = le32_to_cpu(globaldesc->section_num);
2529 for (i = 0; i < section_num; i++) {
2531 fw_ptr = fw_bin_ptr;
2532 sectionmap = (struct btmtk_section_map *)(fw_ptr + MTK_FW_ROM_PATCH_HEADER_SIZE +
2533 MTK_FW_ROM_PATCH_GD_SIZE + MTK_FW_ROM_PATCH_SEC_MAP_SIZE * i);
2535 section_offset = le32_to_cpu(sectionmap->secoffset);
2536 dl_size = le32_to_cpu(sectionmap->bin_info_spec.dlsize);
2541 cmd[0] = 0; /* 0 means legacy dl mode. */
2543 fw_ptr + MTK_FW_ROM_PATCH_HEADER_SIZE +
2544 MTK_FW_ROM_PATCH_GD_SIZE + MTK_FW_ROM_PATCH_SEC_MAP_SIZE * i +
2545 MTK_SEC_MAP_COMMON_SIZE,
2546 MTK_SEC_MAP_NEED_SEND_SIZE + 1);
2548 wmt_params.op = BTMTK_WMT_PATCH_DWNLD;
2549 wmt_params.status = &status;
2550 wmt_params.flag = 0;
2551 wmt_params.dlen = MTK_SEC_MAP_NEED_SEND_SIZE + 1;
2552 wmt_params.data = &cmd;
2554 err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
2556 bt_dev_err(hdev, "Failed to send wmt patch dwnld (%d)",
2558 goto err_release_fw;
2561 if (status == BTMTK_WMT_PATCH_UNDONE) {
2563 } else if (status == BTMTK_WMT_PATCH_PROGRESS) {
2566 } else if (status == BTMTK_WMT_PATCH_DONE) {
2569 bt_dev_err(hdev, "Failed wmt patch dwnld status (%d)",
2572 goto err_release_fw;
2576 fw_ptr += section_offset;
2577 wmt_params.op = BTMTK_WMT_PATCH_DWNLD;
2578 wmt_params.status = NULL;
2580 while (dl_size > 0) {
2581 dlen = min_t(int, 250, dl_size);
2582 if (first_block == 1) {
2585 } else if (dl_size - dlen <= 0) {
2591 wmt_params.flag = flag;
2592 wmt_params.dlen = dlen;
2593 wmt_params.data = fw_ptr;
2595 err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
2597 bt_dev_err(hdev, "Failed to send wmt patch dwnld (%d)",
2599 goto err_release_fw;
2609 /* Wait a few moments for firmware activation done */
2610 usleep_range(100000, 120000);
2613 release_firmware(fw);
2618 static int btusb_mtk_setup_firmware(struct hci_dev *hdev, const char *fwname)
2620 struct btmtk_hci_wmt_params wmt_params;
2621 const struct firmware *fw;
2627 err = request_firmware(&fw, fwname, &hdev->dev);
2629 bt_dev_err(hdev, "Failed to load firmware file (%d)", err);
2633 /* Power on data RAM the firmware relies on. */
2635 wmt_params.op = BTMTK_WMT_FUNC_CTRL;
2636 wmt_params.flag = 3;
2637 wmt_params.dlen = sizeof(param);
2638 wmt_params.data = ¶m;
2639 wmt_params.status = NULL;
2641 err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
2643 bt_dev_err(hdev, "Failed to power on data RAM (%d)", err);
2644 goto err_release_fw;
2650 /* The size of patch header is 30 bytes, should be skip */
2653 goto err_release_fw;
2660 wmt_params.op = BTMTK_WMT_PATCH_DWNLD;
2661 wmt_params.status = NULL;
2663 while (fw_size > 0) {
2664 dlen = min_t(int, 250, fw_size);
2666 /* Tell device the position in sequence */
2667 if (fw_size - dlen <= 0)
2669 else if (fw_size < fw->size - 30)
2672 wmt_params.flag = flag;
2673 wmt_params.dlen = dlen;
2674 wmt_params.data = fw_ptr;
2676 err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
2678 bt_dev_err(hdev, "Failed to send wmt patch dwnld (%d)",
2680 goto err_release_fw;
2687 wmt_params.op = BTMTK_WMT_RST;
2688 wmt_params.flag = 4;
2689 wmt_params.dlen = 0;
2690 wmt_params.data = NULL;
2691 wmt_params.status = NULL;
2693 /* Activate funciton the firmware providing to */
2694 err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
2696 bt_dev_err(hdev, "Failed to send wmt rst (%d)", err);
2697 goto err_release_fw;
2700 /* Wait a few moments for firmware activation done */
2701 usleep_range(10000, 12000);
2704 release_firmware(fw);
2709 static int btusb_mtk_func_query(struct hci_dev *hdev)
2711 struct btmtk_hci_wmt_params wmt_params;
2715 /* Query whether the function is enabled */
2716 wmt_params.op = BTMTK_WMT_FUNC_CTRL;
2717 wmt_params.flag = 4;
2718 wmt_params.dlen = sizeof(param);
2719 wmt_params.data = ¶m;
2720 wmt_params.status = &status;
2722 err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
2724 bt_dev_err(hdev, "Failed to query function status (%d)", err);
2731 static int btusb_mtk_uhw_reg_write(struct btusb_data *data, u32 reg, u32 val)
2733 struct hci_dev *hdev = data->hdev;
2737 buf = kzalloc(4, GFP_KERNEL);
2741 put_unaligned_le32(val, buf);
2743 pipe = usb_sndctrlpipe(data->udev, 0);
2744 err = usb_control_msg(data->udev, pipe, 0x02,
2746 reg >> 16, reg & 0xffff,
2747 buf, 4, USB_CTRL_SET_TIMEOUT);
2749 bt_dev_err(hdev, "Failed to write uhw reg(%d)", err);
2759 static int btusb_mtk_uhw_reg_read(struct btusb_data *data, u32 reg, u32 *val)
2761 struct hci_dev *hdev = data->hdev;
2765 buf = kzalloc(4, GFP_KERNEL);
2769 pipe = usb_rcvctrlpipe(data->udev, 0);
2770 err = usb_control_msg(data->udev, pipe, 0x01,
2772 reg >> 16, reg & 0xffff,
2773 buf, 4, USB_CTRL_SET_TIMEOUT);
2775 bt_dev_err(hdev, "Failed to read uhw reg(%d)", err);
2779 *val = get_unaligned_le32(buf);
2780 bt_dev_dbg(hdev, "reg=%x, value=0x%08x", reg, *val);
2788 static int btusb_mtk_reg_read(struct btusb_data *data, u32 reg, u32 *val)
2790 int pipe, err, size = sizeof(u32);
2793 buf = kzalloc(size, GFP_KERNEL);
2797 pipe = usb_rcvctrlpipe(data->udev, 0);
2798 err = usb_control_msg(data->udev, pipe, 0x63,
2799 USB_TYPE_VENDOR | USB_DIR_IN,
2800 reg >> 16, reg & 0xffff,
2801 buf, size, USB_CTRL_SET_TIMEOUT);
2805 *val = get_unaligned_le32(buf);
2813 static int btusb_mtk_id_get(struct btusb_data *data, u32 reg, u32 *id)
2815 return btusb_mtk_reg_read(data, reg, id);
2818 static int btusb_mtk_setup(struct hci_dev *hdev)
2820 struct btusb_data *data = hci_get_drvdata(hdev);
2821 struct btmtk_hci_wmt_params wmt_params;
2822 ktime_t calltime, delta, rettime;
2823 struct btmtk_tci_sleep tci_sleep;
2824 unsigned long long duration;
2825 struct sk_buff *skb;
2829 char fw_bin_name[64];
2833 calltime = ktime_get();
2835 err = btusb_mtk_id_get(data, 0x80000008, &dev_id);
2837 bt_dev_err(hdev, "Failed to get device id (%d)", err);
2842 err = btusb_mtk_id_get(data, 0x70010200, &dev_id);
2844 bt_dev_err(hdev, "Failed to get device id (%d)", err);
2847 err = btusb_mtk_id_get(data, 0x80021004, &fw_version);
2849 bt_dev_err(hdev, "Failed to get fw version (%d)", err);
2856 fwname = FIRMWARE_MT7663;
2859 fwname = FIRMWARE_MT7668;
2863 snprintf(fw_bin_name, sizeof(fw_bin_name),
2864 "mediatek/BT_RAM_CODE_MT%04x_1_%x_hdr.bin",
2865 dev_id & 0xffff, (fw_version & 0xff) + 1);
2866 err = btusb_mtk_setup_firmware_79xx(hdev, fw_bin_name);
2868 bt_dev_err(hdev, "Failed to set up firmware (%d)", err);
2872 /* It's Device EndPoint Reset Option Register */
2873 btusb_mtk_uhw_reg_write(data, MTK_EP_RST_OPT, MTK_EP_RST_IN_OUT_OPT);
2875 /* Enable Bluetooth protocol */
2877 wmt_params.op = BTMTK_WMT_FUNC_CTRL;
2878 wmt_params.flag = 0;
2879 wmt_params.dlen = sizeof(param);
2880 wmt_params.data = ¶m;
2881 wmt_params.status = NULL;
2883 err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
2885 bt_dev_err(hdev, "Failed to send wmt func ctrl (%d)", err);
2889 hci_set_msft_opcode(hdev, 0xFD30);
2890 hci_set_aosp_capable(hdev);
2893 bt_dev_err(hdev, "Unsupported hardware variant (%08x)",
2898 /* Query whether the firmware is already download */
2899 wmt_params.op = BTMTK_WMT_SEMAPHORE;
2900 wmt_params.flag = 1;
2901 wmt_params.dlen = 0;
2902 wmt_params.data = NULL;
2903 wmt_params.status = &status;
2905 err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
2907 bt_dev_err(hdev, "Failed to query firmware status (%d)", err);
2911 if (status == BTMTK_WMT_PATCH_DONE) {
2912 bt_dev_info(hdev, "firmware already downloaded");
2913 goto ignore_setup_fw;
2916 /* Setup a firmware which the device definitely requires */
2917 err = btusb_mtk_setup_firmware(hdev, fwname);
2922 err = readx_poll_timeout(btusb_mtk_func_query, hdev, status,
2923 status < 0 || status != BTMTK_WMT_ON_PROGRESS,
2925 /* -ETIMEDOUT happens */
2929 /* The other errors happen in btusb_mtk_func_query */
2933 if (status == BTMTK_WMT_ON_DONE) {
2934 bt_dev_info(hdev, "function already on");
2935 goto ignore_func_on;
2938 /* Enable Bluetooth protocol */
2940 wmt_params.op = BTMTK_WMT_FUNC_CTRL;
2941 wmt_params.flag = 0;
2942 wmt_params.dlen = sizeof(param);
2943 wmt_params.data = ¶m;
2944 wmt_params.status = NULL;
2946 err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
2948 bt_dev_err(hdev, "Failed to send wmt func ctrl (%d)", err);
2953 /* Apply the low power environment setup */
2954 tci_sleep.mode = 0x5;
2955 tci_sleep.duration = cpu_to_le16(0x640);
2956 tci_sleep.host_duration = cpu_to_le16(0x640);
2957 tci_sleep.host_wakeup_pin = 0;
2958 tci_sleep.time_compensation = 0;
2960 skb = __hci_cmd_sync(hdev, 0xfc7a, sizeof(tci_sleep), &tci_sleep,
2964 bt_dev_err(hdev, "Failed to apply low power setting (%d)", err);
2970 rettime = ktime_get();
2971 delta = ktime_sub(rettime, calltime);
2972 duration = (unsigned long long)ktime_to_ns(delta) >> 10;
2974 bt_dev_info(hdev, "Device setup in %llu usecs", duration);
2979 static int btusb_mtk_shutdown(struct hci_dev *hdev)
2981 struct btmtk_hci_wmt_params wmt_params;
2985 /* Disable the device */
2986 wmt_params.op = BTMTK_WMT_FUNC_CTRL;
2987 wmt_params.flag = 0;
2988 wmt_params.dlen = sizeof(param);
2989 wmt_params.data = ¶m;
2990 wmt_params.status = NULL;
2992 err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
2994 bt_dev_err(hdev, "Failed to send wmt func ctrl (%d)", err);
3001 static void btusb_mtk_cmd_timeout(struct hci_dev *hdev)
3003 struct btusb_data *data = hci_get_drvdata(hdev);
3007 /* It's MediaTek specific bluetooth reset mechanism via USB */
3008 if (test_and_set_bit(BTUSB_HW_RESET_ACTIVE, &data->flags)) {
3009 bt_dev_err(hdev, "last reset failed? Not resetting again");
3013 err = usb_autopm_get_interface(data->intf);
3017 btusb_stop_traffic(data);
3018 usb_kill_anchored_urbs(&data->tx_anchor);
3020 /* It's Device EndPoint Reset Option Register */
3021 bt_dev_dbg(hdev, "Initiating reset mechanism via uhw");
3022 btusb_mtk_uhw_reg_write(data, MTK_EP_RST_OPT, MTK_EP_RST_IN_OUT_OPT);
3023 btusb_mtk_uhw_reg_read(data, MTK_BT_WDT_STATUS, &val);
3025 /* Reset the bluetooth chip via USB interface. */
3026 btusb_mtk_uhw_reg_write(data, MTK_BT_SUBSYS_RST, 1);
3027 btusb_mtk_uhw_reg_write(data, MTK_UDMA_INT_STA_BT, 0x000000FF);
3028 btusb_mtk_uhw_reg_read(data, MTK_UDMA_INT_STA_BT, &val);
3029 btusb_mtk_uhw_reg_write(data, MTK_UDMA_INT_STA_BT1, 0x000000FF);
3030 btusb_mtk_uhw_reg_read(data, MTK_UDMA_INT_STA_BT1, &val);
3031 /* MT7921 need to delay 20ms between toggle reset bit */
3033 btusb_mtk_uhw_reg_write(data, MTK_BT_SUBSYS_RST, 0);
3034 btusb_mtk_uhw_reg_read(data, MTK_BT_SUBSYS_RST, &val);
3036 /* Poll the register until reset is completed */
3038 btusb_mtk_uhw_reg_read(data, MTK_BT_MISC, &val);
3039 if (val & MTK_BT_RST_DONE) {
3040 bt_dev_dbg(hdev, "Bluetooth Reset Successfully");
3044 bt_dev_dbg(hdev, "Polling Bluetooth Reset CR");
3046 msleep(MTK_BT_RESET_WAIT_MS);
3047 } while (retry < MTK_BT_RESET_NUM_TRIES);
3049 btusb_mtk_id_get(data, 0x70010200, &val);
3051 bt_dev_err(hdev, "Can't get device id, subsys reset fail.");
3053 usb_queue_reset_device(data->intf);
3055 clear_bit(BTUSB_HW_RESET_ACTIVE, &data->flags);
3058 static int btusb_recv_acl_mtk(struct hci_dev *hdev, struct sk_buff *skb)
3060 struct btusb_data *data = hci_get_drvdata(hdev);
3061 u16 handle = le16_to_cpu(hci_acl_hdr(skb)->handle);
3064 case 0xfc6f: /* Firmware dump from device */
3065 /* When the firmware hangs, the device can no longer
3066 * suspend and thus disable auto-suspend.
3068 usb_disable_autosuspend(data->udev);
3070 case 0x05ff: /* Firmware debug logging 1 */
3071 case 0x05fe: /* Firmware debug logging 2 */
3072 return hci_recv_diag(hdev, skb);
3075 return hci_recv_frame(hdev, skb);
3078 MODULE_FIRMWARE(FIRMWARE_MT7663);
3079 MODULE_FIRMWARE(FIRMWARE_MT7668);
3082 /* Configure an out-of-band gpio as wake-up pin, if specified in device tree */
3083 static int marvell_config_oob_wake(struct hci_dev *hdev)
3085 struct sk_buff *skb;
3086 struct btusb_data *data = hci_get_drvdata(hdev);
3087 struct device *dev = &data->udev->dev;
3088 u16 pin, gap, opcode;
3092 /* Move on if no wakeup pin specified */
3093 if (of_property_read_u16(dev->of_node, "marvell,wakeup-pin", &pin) ||
3094 of_property_read_u16(dev->of_node, "marvell,wakeup-gap-ms", &gap))
3097 /* Vendor specific command to configure a GPIO as wake-up pin */
3098 opcode = hci_opcode_pack(0x3F, 0x59);
3099 cmd[0] = opcode & 0xFF;
3100 cmd[1] = opcode >> 8;
3101 cmd[2] = 2; /* length of parameters that follow */
3103 cmd[4] = gap; /* time in ms, for which wakeup pin should be asserted */
3105 skb = bt_skb_alloc(sizeof(cmd), GFP_KERNEL);
3107 bt_dev_err(hdev, "%s: No memory", __func__);
3111 skb_put_data(skb, cmd, sizeof(cmd));
3112 hci_skb_pkt_type(skb) = HCI_COMMAND_PKT;
3114 ret = btusb_send_frame(hdev, skb);
3116 bt_dev_err(hdev, "%s: configuration failed", __func__);
3125 static int btusb_set_bdaddr_marvell(struct hci_dev *hdev,
3126 const bdaddr_t *bdaddr)
3128 struct sk_buff *skb;
3133 buf[1] = sizeof(bdaddr_t);
3134 memcpy(buf + 2, bdaddr, sizeof(bdaddr_t));
3136 skb = __hci_cmd_sync(hdev, 0xfc22, sizeof(buf), buf, HCI_INIT_TIMEOUT);
3139 bt_dev_err(hdev, "changing Marvell device address failed (%ld)",
3148 static int btusb_set_bdaddr_ath3012(struct hci_dev *hdev,
3149 const bdaddr_t *bdaddr)
3151 struct sk_buff *skb;
3158 buf[3] = sizeof(bdaddr_t);
3159 memcpy(buf + 4, bdaddr, sizeof(bdaddr_t));
3161 skb = __hci_cmd_sync(hdev, 0xfc0b, sizeof(buf), buf, HCI_INIT_TIMEOUT);
3164 bt_dev_err(hdev, "Change address command failed (%ld)", ret);
3172 static int btusb_set_bdaddr_wcn6855(struct hci_dev *hdev,
3173 const bdaddr_t *bdaddr)
3175 struct sk_buff *skb;
3179 memcpy(buf, bdaddr, sizeof(bdaddr_t));
3181 skb = __hci_cmd_sync_ev(hdev, 0xfc14, sizeof(buf), buf,
3182 HCI_EV_CMD_COMPLETE, HCI_INIT_TIMEOUT);
3185 bt_dev_err(hdev, "Change address command failed (%ld)", ret);
3193 #define QCA_DFU_PACKET_LEN 4096
3195 #define QCA_GET_TARGET_VERSION 0x09
3196 #define QCA_CHECK_STATUS 0x05
3197 #define QCA_DFU_DOWNLOAD 0x01
3199 #define QCA_SYSCFG_UPDATED 0x40
3200 #define QCA_PATCH_UPDATED 0x80
3201 #define QCA_DFU_TIMEOUT 3000
3202 #define QCA_FLAG_MULTI_NVM 0x80
3204 struct qca_version {
3206 __le32 patch_version;
3213 struct qca_rampatch_version {
3214 __le16 rom_version_high;
3215 __le16 rom_version_low;
3216 __le16 patch_version;
3219 struct qca_device_info {
3221 u8 rampatch_hdr; /* length of header in rampatch */
3222 u8 nvm_hdr; /* length of header in NVM */
3223 u8 ver_offset; /* offset of version structure in rampatch */
3226 static const struct qca_device_info qca_devices_table[] = {
3227 { 0x00000100, 20, 4, 8 }, /* Rome 1.0 */
3228 { 0x00000101, 20, 4, 8 }, /* Rome 1.1 */
3229 { 0x00000200, 28, 4, 16 }, /* Rome 2.0 */
3230 { 0x00000201, 28, 4, 16 }, /* Rome 2.1 */
3231 { 0x00000300, 28, 4, 16 }, /* Rome 3.0 */
3232 { 0x00000302, 28, 4, 16 }, /* Rome 3.2 */
3233 { 0x00130100, 40, 4, 16 }, /* WCN6855 1.0 */
3234 { 0x00130200, 40, 4, 16 }, /* WCN6855 2.0 */
3237 static int btusb_qca_send_vendor_req(struct usb_device *udev, u8 request,
3238 void *data, u16 size)
3243 buf = kmalloc(size, GFP_KERNEL);
3247 /* Found some of USB hosts have IOT issues with ours so that we should
3248 * not wait until HCI layer is ready.
3250 pipe = usb_rcvctrlpipe(udev, 0);
3251 err = usb_control_msg(udev, pipe, request, USB_TYPE_VENDOR | USB_DIR_IN,
3252 0, 0, buf, size, USB_CTRL_SET_TIMEOUT);
3254 dev_err(&udev->dev, "Failed to access otp area (%d)", err);
3258 memcpy(data, buf, size);
3266 static int btusb_setup_qca_download_fw(struct hci_dev *hdev,
3267 const struct firmware *firmware,
3270 struct btusb_data *btdata = hci_get_drvdata(hdev);
3271 struct usb_device *udev = btdata->udev;
3272 size_t count, size, sent = 0;
3276 buf = kmalloc(QCA_DFU_PACKET_LEN, GFP_KERNEL);
3280 count = firmware->size;
3282 size = min_t(size_t, count, hdr_size);
3283 memcpy(buf, firmware->data, size);
3285 /* USB patches should go down to controller through USB path
3286 * because binary format fits to go down through USB channel.
3287 * USB control path is for patching headers and USB bulk is for
3290 pipe = usb_sndctrlpipe(udev, 0);
3291 err = usb_control_msg(udev, pipe, QCA_DFU_DOWNLOAD, USB_TYPE_VENDOR,
3292 0, 0, buf, size, USB_CTRL_SET_TIMEOUT);
3294 bt_dev_err(hdev, "Failed to send headers (%d)", err);
3301 /* ep2 need time to switch from function acl to function dfu,
3302 * so we add 20ms delay here.
3307 size = min_t(size_t, count, QCA_DFU_PACKET_LEN);
3309 memcpy(buf, firmware->data + sent, size);
3311 pipe = usb_sndbulkpipe(udev, 0x02);
3312 err = usb_bulk_msg(udev, pipe, buf, size, &len,
3315 bt_dev_err(hdev, "Failed to send body at %zd of %zd (%d)",
3316 sent, firmware->size, err);
3321 bt_dev_err(hdev, "Failed to get bulk buffer");
3335 static int btusb_setup_qca_load_rampatch(struct hci_dev *hdev,
3336 struct qca_version *ver,
3337 const struct qca_device_info *info)
3339 struct qca_rampatch_version *rver;
3340 const struct firmware *fw;
3341 u32 ver_rom, ver_patch, rver_rom;
3342 u16 rver_rom_low, rver_rom_high, rver_patch;
3346 ver_rom = le32_to_cpu(ver->rom_version);
3347 ver_patch = le32_to_cpu(ver->patch_version);
3349 snprintf(fwname, sizeof(fwname), "qca/rampatch_usb_%08x.bin", ver_rom);
3351 err = request_firmware(&fw, fwname, &hdev->dev);
3353 bt_dev_err(hdev, "failed to request rampatch file: %s (%d)",
3358 bt_dev_info(hdev, "using rampatch file: %s", fwname);
3360 rver = (struct qca_rampatch_version *)(fw->data + info->ver_offset);
3361 rver_rom_low = le16_to_cpu(rver->rom_version_low);
3362 rver_patch = le16_to_cpu(rver->patch_version);
3364 if (ver_rom & ~0xffffU) {
3365 rver_rom_high = le16_to_cpu(rver->rom_version_high);
3366 rver_rom = le32_to_cpu(rver_rom_high << 16 | rver_rom_low);
3368 rver_rom = rver_rom_low;
3371 bt_dev_info(hdev, "QCA: patch rome 0x%x build 0x%x, "
3372 "firmware rome 0x%x build 0x%x",
3373 rver_rom, rver_patch, ver_rom, ver_patch);
3375 if (rver_rom != ver_rom || rver_patch <= ver_patch) {
3376 bt_dev_err(hdev, "rampatch file version did not match with firmware");
3381 err = btusb_setup_qca_download_fw(hdev, fw, info->rampatch_hdr);
3384 release_firmware(fw);
3389 static int btusb_setup_qca_load_nvm(struct hci_dev *hdev,
3390 struct qca_version *ver,
3391 const struct qca_device_info *info)
3393 const struct firmware *fw;
3397 if (((ver->flag >> 8) & 0xff) == QCA_FLAG_MULTI_NVM) {
3398 /* if boardid equal 0, use default nvm without surfix */
3399 if (le16_to_cpu(ver->board_id) == 0x0) {
3400 snprintf(fwname, sizeof(fwname), "qca/nvm_usb_%08x.bin",
3401 le32_to_cpu(ver->rom_version));
3403 snprintf(fwname, sizeof(fwname), "qca/nvm_usb_%08x_%04x.bin",
3404 le32_to_cpu(ver->rom_version),
3405 le16_to_cpu(ver->board_id));
3408 snprintf(fwname, sizeof(fwname), "qca/nvm_usb_%08x.bin",
3409 le32_to_cpu(ver->rom_version));
3412 err = request_firmware(&fw, fwname, &hdev->dev);
3414 bt_dev_err(hdev, "failed to request NVM file: %s (%d)",
3419 bt_dev_info(hdev, "using NVM file: %s", fwname);
3421 err = btusb_setup_qca_download_fw(hdev, fw, info->nvm_hdr);
3423 release_firmware(fw);
3428 /* identify the ROM version and check whether patches are needed */
3429 static bool btusb_qca_need_patch(struct usb_device *udev)
3431 struct qca_version ver;
3433 if (btusb_qca_send_vendor_req(udev, QCA_GET_TARGET_VERSION, &ver,
3436 /* only low ROM versions need patches */
3437 return !(le32_to_cpu(ver.rom_version) & ~0xffffU);
3440 static int btusb_setup_qca(struct hci_dev *hdev)
3442 struct btusb_data *btdata = hci_get_drvdata(hdev);
3443 struct usb_device *udev = btdata->udev;
3444 const struct qca_device_info *info = NULL;
3445 struct qca_version ver;
3450 err = btusb_qca_send_vendor_req(udev, QCA_GET_TARGET_VERSION, &ver,
3455 ver_rom = le32_to_cpu(ver.rom_version);
3457 for (i = 0; i < ARRAY_SIZE(qca_devices_table); i++) {
3458 if (ver_rom == qca_devices_table[i].rom_version)
3459 info = &qca_devices_table[i];
3462 /* If the rom_version is not matched in the qca_devices_table
3463 * and the high ROM version is not zero, we assume this chip no
3464 * need to load the rampatch and nvm.
3466 if (ver_rom & ~0xffffU)
3469 bt_dev_err(hdev, "don't support firmware rome 0x%x", ver_rom);
3473 err = btusb_qca_send_vendor_req(udev, QCA_CHECK_STATUS, &status,
3478 if (!(status & QCA_PATCH_UPDATED)) {
3479 err = btusb_setup_qca_load_rampatch(hdev, &ver, info);
3484 err = btusb_qca_send_vendor_req(udev, QCA_GET_TARGET_VERSION, &ver,
3489 if (!(status & QCA_SYSCFG_UPDATED)) {
3490 err = btusb_setup_qca_load_nvm(hdev, &ver, info);
3498 static inline int __set_diag_interface(struct hci_dev *hdev)
3500 struct btusb_data *data = hci_get_drvdata(hdev);
3501 struct usb_interface *intf = data->diag;
3507 data->diag_tx_ep = NULL;
3508 data->diag_rx_ep = NULL;
3510 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
3511 struct usb_endpoint_descriptor *ep_desc;
3513 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
3515 if (!data->diag_tx_ep && usb_endpoint_is_bulk_out(ep_desc)) {
3516 data->diag_tx_ep = ep_desc;
3520 if (!data->diag_rx_ep && usb_endpoint_is_bulk_in(ep_desc)) {
3521 data->diag_rx_ep = ep_desc;
3526 if (!data->diag_tx_ep || !data->diag_rx_ep) {
3527 bt_dev_err(hdev, "invalid diagnostic descriptors");
3534 static struct urb *alloc_diag_urb(struct hci_dev *hdev, bool enable)
3536 struct btusb_data *data = hci_get_drvdata(hdev);
3537 struct sk_buff *skb;
3541 if (!data->diag_tx_ep)
3542 return ERR_PTR(-ENODEV);
3544 urb = usb_alloc_urb(0, GFP_KERNEL);
3546 return ERR_PTR(-ENOMEM);
3548 skb = bt_skb_alloc(2, GFP_KERNEL);
3551 return ERR_PTR(-ENOMEM);
3554 skb_put_u8(skb, 0xf0);
3555 skb_put_u8(skb, enable);
3557 pipe = usb_sndbulkpipe(data->udev, data->diag_tx_ep->bEndpointAddress);
3559 usb_fill_bulk_urb(urb, data->udev, pipe,
3560 skb->data, skb->len, btusb_tx_complete, skb);
3562 skb->dev = (void *)hdev;
3567 static int btusb_bcm_set_diag(struct hci_dev *hdev, bool enable)
3569 struct btusb_data *data = hci_get_drvdata(hdev);
3575 if (!test_bit(HCI_RUNNING, &hdev->flags))
3578 urb = alloc_diag_urb(hdev, enable);
3580 return PTR_ERR(urb);
3582 return submit_or_queue_tx_urb(hdev, urb);
3586 static irqreturn_t btusb_oob_wake_handler(int irq, void *priv)
3588 struct btusb_data *data = priv;
3590 pm_wakeup_event(&data->udev->dev, 0);
3593 /* Disable only if not already disabled (keep it balanced) */
3594 if (test_and_clear_bit(BTUSB_OOB_WAKE_ENABLED, &data->flags)) {
3595 disable_irq_nosync(irq);
3596 disable_irq_wake(irq);
3601 static const struct of_device_id btusb_match_table[] = {
3602 { .compatible = "usb1286,204e" },
3603 { .compatible = "usbcf3,e300" }, /* QCA6174A */
3604 { .compatible = "usb4ca,301a" }, /* QCA6174A (Lite-On) */
3607 MODULE_DEVICE_TABLE(of, btusb_match_table);
3609 /* Use an oob wakeup pin? */
3610 static int btusb_config_oob_wake(struct hci_dev *hdev)
3612 struct btusb_data *data = hci_get_drvdata(hdev);
3613 struct device *dev = &data->udev->dev;
3616 clear_bit(BTUSB_OOB_WAKE_ENABLED, &data->flags);
3618 if (!of_match_device(btusb_match_table, dev))
3621 /* Move on if no IRQ specified */
3622 irq = of_irq_get_byname(dev->of_node, "wakeup");
3624 bt_dev_dbg(hdev, "%s: no OOB Wakeup IRQ in DT", __func__);
3628 irq_set_status_flags(irq, IRQ_NOAUTOEN);
3629 ret = devm_request_irq(&hdev->dev, irq, btusb_oob_wake_handler,
3630 0, "OOB Wake-on-BT", data);
3632 bt_dev_err(hdev, "%s: IRQ request failed", __func__);
3636 ret = device_init_wakeup(dev, true);
3638 bt_dev_err(hdev, "%s: failed to init_wakeup", __func__);
3642 data->oob_wake_irq = irq;
3643 bt_dev_info(hdev, "OOB Wake-on-BT configured at IRQ %u", irq);
3648 static void btusb_check_needs_reset_resume(struct usb_interface *intf)
3650 if (dmi_check_system(btusb_needs_reset_resume_table))
3651 interface_to_usbdev(intf)->quirks |= USB_QUIRK_RESET_RESUME;
3654 static bool btusb_prevent_wake(struct hci_dev *hdev)
3656 struct btusb_data *data = hci_get_drvdata(hdev);
3658 return !device_may_wakeup(&data->udev->dev);
3661 static int btusb_shutdown_qca(struct hci_dev *hdev)
3663 struct sk_buff *skb;
3665 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
3667 bt_dev_err(hdev, "HCI reset during shutdown failed");
3668 return PTR_ERR(skb);
3675 static int btusb_probe(struct usb_interface *intf,
3676 const struct usb_device_id *id)
3678 struct usb_endpoint_descriptor *ep_desc;
3679 struct gpio_desc *reset_gpio;
3680 struct btusb_data *data;
3681 struct hci_dev *hdev;
3682 unsigned ifnum_base;
3683 int i, err, priv_size;
3685 BT_DBG("intf %p id %p", intf, id);
3687 /* interface numbers are hardcoded in the spec */
3688 if (intf->cur_altsetting->desc.bInterfaceNumber != 0) {
3689 if (!(id->driver_info & BTUSB_IFNUM_2))
3691 if (intf->cur_altsetting->desc.bInterfaceNumber != 2)
3695 ifnum_base = intf->cur_altsetting->desc.bInterfaceNumber;
3697 if (!id->driver_info) {
3698 const struct usb_device_id *match;
3700 match = usb_match_id(intf, blacklist_table);
3705 if (id->driver_info == BTUSB_IGNORE)
3708 if (id->driver_info & BTUSB_ATH3012) {
3709 struct usb_device *udev = interface_to_usbdev(intf);
3711 /* Old firmware would otherwise let ath3k driver load
3712 * patch and sysconfig files
3714 if (le16_to_cpu(udev->descriptor.bcdDevice) <= 0x0001 &&
3715 !btusb_qca_need_patch(udev))
3719 data = devm_kzalloc(&intf->dev, sizeof(*data), GFP_KERNEL);
3723 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
3724 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
3726 if (!data->intr_ep && usb_endpoint_is_int_in(ep_desc)) {
3727 data->intr_ep = ep_desc;
3731 if (!data->bulk_tx_ep && usb_endpoint_is_bulk_out(ep_desc)) {
3732 data->bulk_tx_ep = ep_desc;
3736 if (!data->bulk_rx_ep && usb_endpoint_is_bulk_in(ep_desc)) {
3737 data->bulk_rx_ep = ep_desc;
3742 if (!data->intr_ep || !data->bulk_tx_ep || !data->bulk_rx_ep)
3745 if (id->driver_info & BTUSB_AMP) {
3746 data->cmdreq_type = USB_TYPE_CLASS | 0x01;
3747 data->cmdreq = 0x2b;
3749 data->cmdreq_type = USB_TYPE_CLASS;
3750 data->cmdreq = 0x00;
3753 data->udev = interface_to_usbdev(intf);
3756 INIT_WORK(&data->work, btusb_work);
3757 INIT_WORK(&data->waker, btusb_waker);
3758 init_usb_anchor(&data->deferred);
3759 init_usb_anchor(&data->tx_anchor);
3760 spin_lock_init(&data->txlock);
3762 init_usb_anchor(&data->intr_anchor);
3763 init_usb_anchor(&data->bulk_anchor);
3764 init_usb_anchor(&data->isoc_anchor);
3765 init_usb_anchor(&data->diag_anchor);
3766 init_usb_anchor(&data->ctrl_anchor);
3767 spin_lock_init(&data->rxlock);
3771 data->recv_event = hci_recv_frame;
3772 data->recv_bulk = btusb_recv_bulk;
3774 if (id->driver_info & BTUSB_INTEL_COMBINED) {
3775 /* Allocate extra space for Intel device */
3776 priv_size += sizeof(struct btintel_data);
3778 /* Override the rx handlers */
3779 data->recv_event = btusb_recv_event_intel;
3780 data->recv_bulk = btusb_recv_bulk_intel;
3783 data->recv_acl = hci_recv_frame;
3785 hdev = hci_alloc_dev_priv(priv_size);
3789 hdev->bus = HCI_USB;
3790 hci_set_drvdata(hdev, data);
3792 if (id->driver_info & BTUSB_AMP)
3793 hdev->dev_type = HCI_AMP;
3795 hdev->dev_type = HCI_PRIMARY;
3799 SET_HCIDEV_DEV(hdev, &intf->dev);
3801 reset_gpio = gpiod_get_optional(&data->udev->dev, "reset",
3803 if (IS_ERR(reset_gpio)) {
3804 err = PTR_ERR(reset_gpio);
3806 } else if (reset_gpio) {
3807 data->reset_gpio = reset_gpio;
3810 hdev->open = btusb_open;
3811 hdev->close = btusb_close;
3812 hdev->flush = btusb_flush;
3813 hdev->send = btusb_send_frame;
3814 hdev->notify = btusb_notify;
3815 hdev->prevent_wake = btusb_prevent_wake;
3818 err = btusb_config_oob_wake(hdev);
3822 /* Marvell devices may need a specific chip configuration */
3823 if (id->driver_info & BTUSB_MARVELL && data->oob_wake_irq) {
3824 err = marvell_config_oob_wake(hdev);
3829 if (id->driver_info & BTUSB_CW6622)
3830 set_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks);
3832 if (id->driver_info & BTUSB_BCM2045)
3833 set_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks);
3835 if (id->driver_info & BTUSB_BCM92035)
3836 hdev->setup = btusb_setup_bcm92035;
3838 if (IS_ENABLED(CONFIG_BT_HCIBTUSB_BCM) &&
3839 (id->driver_info & BTUSB_BCM_PATCHRAM)) {
3840 hdev->manufacturer = 15;
3841 hdev->setup = btbcm_setup_patchram;
3842 hdev->set_diag = btusb_bcm_set_diag;
3843 hdev->set_bdaddr = btbcm_set_bdaddr;
3845 /* Broadcom LM_DIAG Interface numbers are hardcoded */
3846 data->diag = usb_ifnum_to_if(data->udev, ifnum_base + 2);
3849 if (IS_ENABLED(CONFIG_BT_HCIBTUSB_BCM) &&
3850 (id->driver_info & BTUSB_BCM_APPLE)) {
3851 hdev->manufacturer = 15;
3852 hdev->setup = btbcm_setup_apple;
3853 hdev->set_diag = btusb_bcm_set_diag;
3855 /* Broadcom LM_DIAG Interface numbers are hardcoded */
3856 data->diag = usb_ifnum_to_if(data->udev, ifnum_base + 2);
3859 /* Combined Intel Device setup to support multiple setup routine */
3860 if (id->driver_info & BTUSB_INTEL_COMBINED) {
3861 err = btintel_configure_setup(hdev);
3865 /* Transport specific configuration */
3866 hdev->send = btusb_send_frame_intel;
3867 hdev->cmd_timeout = btusb_intel_cmd_timeout;
3869 if (id->driver_info & BTUSB_INTEL_NO_WBS_SUPPORT)
3870 btintel_set_flag(hdev, INTEL_ROM_LEGACY_NO_WBS_SUPPORT);
3872 if (id->driver_info & BTUSB_INTEL_BROKEN_INITIAL_NCMD)
3873 btintel_set_flag(hdev, INTEL_BROKEN_INITIAL_NCMD);
3875 if (id->driver_info & BTUSB_INTEL_BROKEN_SHUTDOWN_LED)
3876 btintel_set_flag(hdev, INTEL_BROKEN_SHUTDOWN_LED);
3879 if (id->driver_info & BTUSB_MARVELL)
3880 hdev->set_bdaddr = btusb_set_bdaddr_marvell;
3882 if (IS_ENABLED(CONFIG_BT_HCIBTUSB_MTK) &&
3883 (id->driver_info & BTUSB_MEDIATEK)) {
3884 hdev->setup = btusb_mtk_setup;
3885 hdev->shutdown = btusb_mtk_shutdown;
3886 hdev->manufacturer = 70;
3887 hdev->cmd_timeout = btusb_mtk_cmd_timeout;
3888 set_bit(HCI_QUIRK_NON_PERSISTENT_SETUP, &hdev->quirks);
3889 data->recv_acl = btusb_recv_acl_mtk;
3892 if (id->driver_info & BTUSB_SWAVE) {
3893 set_bit(HCI_QUIRK_FIXUP_INQUIRY_MODE, &hdev->quirks);
3894 set_bit(HCI_QUIRK_BROKEN_LOCAL_COMMANDS, &hdev->quirks);
3897 if (id->driver_info & BTUSB_INTEL_BOOT) {
3898 hdev->manufacturer = 2;
3899 set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
3902 if (id->driver_info & BTUSB_ATH3012) {
3903 data->setup_on_usb = btusb_setup_qca;
3904 hdev->set_bdaddr = btusb_set_bdaddr_ath3012;
3905 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
3906 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
3909 if (id->driver_info & BTUSB_QCA_ROME) {
3910 data->setup_on_usb = btusb_setup_qca;
3911 hdev->set_bdaddr = btusb_set_bdaddr_ath3012;
3912 hdev->cmd_timeout = btusb_qca_cmd_timeout;
3913 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
3914 btusb_check_needs_reset_resume(intf);
3917 if (id->driver_info & BTUSB_QCA_WCN6855) {
3918 data->setup_on_usb = btusb_setup_qca;
3919 hdev->shutdown = btusb_shutdown_qca;
3920 hdev->set_bdaddr = btusb_set_bdaddr_wcn6855;
3921 hdev->cmd_timeout = btusb_qca_cmd_timeout;
3922 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
3923 hci_set_msft_opcode(hdev, 0xFD70);
3926 if (id->driver_info & BTUSB_AMP) {
3927 /* AMP controllers do not support SCO packets */
3930 /* Interface orders are hardcoded in the specification */
3931 data->isoc = usb_ifnum_to_if(data->udev, ifnum_base + 1);
3932 data->isoc_ifnum = ifnum_base + 1;
3935 if (IS_ENABLED(CONFIG_BT_HCIBTUSB_RTL) &&
3936 (id->driver_info & BTUSB_REALTEK)) {
3937 hdev->setup = btrtl_setup_realtek;
3938 hdev->shutdown = btrtl_shutdown_realtek;
3939 hdev->cmd_timeout = btusb_rtl_cmd_timeout;
3941 /* Realtek devices need to set remote wakeup on auto-suspend */
3942 set_bit(BTUSB_WAKEUP_AUTOSUSPEND, &data->flags);
3943 set_bit(BTUSB_USE_ALT3_FOR_WBS, &data->flags);
3947 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
3949 if (force_scofix || id->driver_info & BTUSB_WRONG_SCO_MTU) {
3950 if (!disable_scofix)
3951 set_bit(HCI_QUIRK_FIXUP_BUFFER_SIZE, &hdev->quirks);
3954 if (id->driver_info & BTUSB_BROKEN_ISOC)
3957 if (id->driver_info & BTUSB_WIDEBAND_SPEECH)
3958 set_bit(HCI_QUIRK_WIDEBAND_SPEECH_SUPPORTED, &hdev->quirks);
3960 if (id->driver_info & BTUSB_VALID_LE_STATES)
3961 set_bit(HCI_QUIRK_VALID_LE_STATES, &hdev->quirks);
3963 if (id->driver_info & BTUSB_DIGIANSWER) {
3964 data->cmdreq_type = USB_TYPE_VENDOR;
3965 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
3968 if (id->driver_info & BTUSB_CSR) {
3969 struct usb_device *udev = data->udev;
3970 u16 bcdDevice = le16_to_cpu(udev->descriptor.bcdDevice);
3972 /* Old firmware would otherwise execute USB reset */
3973 if (bcdDevice < 0x117)
3974 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
3976 /* This must be set first in case we disable it for fakes */
3977 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
3979 /* Fake CSR devices with broken commands */
3980 if (le16_to_cpu(udev->descriptor.idVendor) == 0x0a12 &&
3981 le16_to_cpu(udev->descriptor.idProduct) == 0x0001)
3982 hdev->setup = btusb_setup_csr;
3985 if (id->driver_info & BTUSB_SNIFFER) {
3986 struct usb_device *udev = data->udev;
3988 /* New sniffer firmware has crippled HCI interface */
3989 if (le16_to_cpu(udev->descriptor.bcdDevice) > 0x997)
3990 set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
3993 if (id->driver_info & BTUSB_INTEL_BOOT) {
3994 /* A bug in the bootloader causes that interrupt interface is
3995 * only enabled after receiving SetInterface(0, AltSetting=0).
3997 err = usb_set_interface(data->udev, 0, 0);
3999 BT_ERR("failed to set interface 0, alt 0 %d", err);
4005 err = usb_driver_claim_interface(&btusb_driver,
4011 if (IS_ENABLED(CONFIG_BT_HCIBTUSB_BCM) && data->diag) {
4012 if (!usb_driver_claim_interface(&btusb_driver,
4014 __set_diag_interface(hdev);
4019 if (enable_autosuspend)
4020 usb_enable_autosuspend(data->udev);
4022 err = hci_register_dev(hdev);
4026 usb_set_intfdata(intf, data);
4031 if (data->reset_gpio)
4032 gpiod_put(data->reset_gpio);
4037 static void btusb_disconnect(struct usb_interface *intf)
4039 struct btusb_data *data = usb_get_intfdata(intf);
4040 struct hci_dev *hdev;
4042 BT_DBG("intf %p", intf);
4048 usb_set_intfdata(data->intf, NULL);
4051 usb_set_intfdata(data->isoc, NULL);
4054 usb_set_intfdata(data->diag, NULL);
4056 hci_unregister_dev(hdev);
4058 if (intf == data->intf) {
4060 usb_driver_release_interface(&btusb_driver, data->isoc);
4062 usb_driver_release_interface(&btusb_driver, data->diag);
4063 } else if (intf == data->isoc) {
4065 usb_driver_release_interface(&btusb_driver, data->diag);
4066 usb_driver_release_interface(&btusb_driver, data->intf);
4067 } else if (intf == data->diag) {
4068 usb_driver_release_interface(&btusb_driver, data->intf);
4070 usb_driver_release_interface(&btusb_driver, data->isoc);
4073 if (data->oob_wake_irq)
4074 device_init_wakeup(&data->udev->dev, false);
4076 if (data->reset_gpio)
4077 gpiod_put(data->reset_gpio);
4083 static int btusb_suspend(struct usb_interface *intf, pm_message_t message)
4085 struct btusb_data *data = usb_get_intfdata(intf);
4087 BT_DBG("intf %p", intf);
4089 if (data->suspend_count++)
4092 spin_lock_irq(&data->txlock);
4093 if (!(PMSG_IS_AUTO(message) && data->tx_in_flight)) {
4094 set_bit(BTUSB_SUSPENDING, &data->flags);
4095 spin_unlock_irq(&data->txlock);
4097 spin_unlock_irq(&data->txlock);
4098 data->suspend_count--;
4102 cancel_work_sync(&data->work);
4104 btusb_stop_traffic(data);
4105 usb_kill_anchored_urbs(&data->tx_anchor);
4107 if (data->oob_wake_irq && device_may_wakeup(&data->udev->dev)) {
4108 set_bit(BTUSB_OOB_WAKE_ENABLED, &data->flags);
4109 enable_irq_wake(data->oob_wake_irq);
4110 enable_irq(data->oob_wake_irq);
4113 /* For global suspend, Realtek devices lose the loaded fw
4114 * in them. But for autosuspend, firmware should remain.
4115 * Actually, it depends on whether the usb host sends
4116 * set feature (enable wakeup) or not.
4118 if (test_bit(BTUSB_WAKEUP_AUTOSUSPEND, &data->flags)) {
4119 if (PMSG_IS_AUTO(message) &&
4120 device_can_wakeup(&data->udev->dev))
4121 data->udev->do_remote_wakeup = 1;
4122 else if (!PMSG_IS_AUTO(message) &&
4123 !device_may_wakeup(&data->udev->dev)) {
4124 data->udev->do_remote_wakeup = 0;
4125 data->udev->reset_resume = 1;
4132 static void play_deferred(struct btusb_data *data)
4137 while ((urb = usb_get_from_anchor(&data->deferred))) {
4138 usb_anchor_urb(urb, &data->tx_anchor);
4140 err = usb_submit_urb(urb, GFP_ATOMIC);
4142 if (err != -EPERM && err != -ENODEV)
4143 BT_ERR("%s urb %p submission failed (%d)",
4144 data->hdev->name, urb, -err);
4145 kfree(urb->setup_packet);
4146 usb_unanchor_urb(urb);
4151 data->tx_in_flight++;
4155 /* Cleanup the rest deferred urbs. */
4156 while ((urb = usb_get_from_anchor(&data->deferred))) {
4157 kfree(urb->setup_packet);
4162 static int btusb_resume(struct usb_interface *intf)
4164 struct btusb_data *data = usb_get_intfdata(intf);
4165 struct hci_dev *hdev = data->hdev;
4168 BT_DBG("intf %p", intf);
4170 if (--data->suspend_count)
4173 /* Disable only if not already disabled (keep it balanced) */
4174 if (test_and_clear_bit(BTUSB_OOB_WAKE_ENABLED, &data->flags)) {
4175 disable_irq(data->oob_wake_irq);
4176 disable_irq_wake(data->oob_wake_irq);
4179 if (!test_bit(HCI_RUNNING, &hdev->flags))
4182 if (test_bit(BTUSB_INTR_RUNNING, &data->flags)) {
4183 err = btusb_submit_intr_urb(hdev, GFP_NOIO);
4185 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
4190 if (test_bit(BTUSB_BULK_RUNNING, &data->flags)) {
4191 err = btusb_submit_bulk_urb(hdev, GFP_NOIO);
4193 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
4197 btusb_submit_bulk_urb(hdev, GFP_NOIO);
4200 if (test_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
4201 if (btusb_submit_isoc_urb(hdev, GFP_NOIO) < 0)
4202 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
4204 btusb_submit_isoc_urb(hdev, GFP_NOIO);
4207 spin_lock_irq(&data->txlock);
4208 play_deferred(data);
4209 clear_bit(BTUSB_SUSPENDING, &data->flags);
4210 spin_unlock_irq(&data->txlock);
4211 schedule_work(&data->work);
4216 usb_scuttle_anchored_urbs(&data->deferred);
4218 spin_lock_irq(&data->txlock);
4219 clear_bit(BTUSB_SUSPENDING, &data->flags);
4220 spin_unlock_irq(&data->txlock);
4226 static struct usb_driver btusb_driver = {
4228 .probe = btusb_probe,
4229 .disconnect = btusb_disconnect,
4231 .suspend = btusb_suspend,
4232 .resume = btusb_resume,
4234 .id_table = btusb_table,
4235 .supports_autosuspend = 1,
4236 .disable_hub_initiated_lpm = 1,
4239 module_usb_driver(btusb_driver);
4241 module_param(disable_scofix, bool, 0644);
4242 MODULE_PARM_DESC(disable_scofix, "Disable fixup of wrong SCO buffer size");
4244 module_param(force_scofix, bool, 0644);
4245 MODULE_PARM_DESC(force_scofix, "Force fixup of wrong SCO buffers size");
4247 module_param(enable_autosuspend, bool, 0644);
4248 MODULE_PARM_DESC(enable_autosuspend, "Enable USB autosuspend by default");
4250 module_param(reset, bool, 0644);
4251 MODULE_PARM_DESC(reset, "Send HCI reset command on initialization");
4253 MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
4254 MODULE_DESCRIPTION("Generic Bluetooth USB driver ver " VERSION);
4255 MODULE_VERSION(VERSION);
4256 MODULE_LICENSE("GPL");