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
65 static const struct usb_device_id btusb_table[] = {
66 /* Generic Bluetooth USB device */
67 { USB_DEVICE_INFO(0xe0, 0x01, 0x01) },
69 /* Generic Bluetooth AMP device */
70 { USB_DEVICE_INFO(0xe0, 0x01, 0x04), .driver_info = BTUSB_AMP },
72 /* Generic Bluetooth USB interface */
73 { USB_INTERFACE_INFO(0xe0, 0x01, 0x01) },
75 /* Apple-specific (Broadcom) devices */
76 { USB_VENDOR_AND_INTERFACE_INFO(0x05ac, 0xff, 0x01, 0x01),
77 .driver_info = BTUSB_BCM_APPLE | BTUSB_IFNUM_2 },
79 /* MediaTek MT76x0E */
80 { USB_DEVICE(0x0e8d, 0x763f) },
82 /* Broadcom SoftSailing reporting vendor specific */
83 { USB_DEVICE(0x0a5c, 0x21e1) },
85 /* Apple MacBookPro 7,1 */
86 { USB_DEVICE(0x05ac, 0x8213) },
89 { USB_DEVICE(0x05ac, 0x8215) },
91 /* Apple MacBookPro6,2 */
92 { USB_DEVICE(0x05ac, 0x8218) },
94 /* Apple MacBookAir3,1, MacBookAir3,2 */
95 { USB_DEVICE(0x05ac, 0x821b) },
97 /* Apple MacBookAir4,1 */
98 { USB_DEVICE(0x05ac, 0x821f) },
100 /* Apple MacBookPro8,2 */
101 { USB_DEVICE(0x05ac, 0x821a) },
103 /* Apple MacMini5,1 */
104 { USB_DEVICE(0x05ac, 0x8281) },
106 /* AVM BlueFRITZ! USB v2.0 */
107 { USB_DEVICE(0x057c, 0x3800), .driver_info = BTUSB_SWAVE },
109 /* Bluetooth Ultraport Module from IBM */
110 { USB_DEVICE(0x04bf, 0x030a) },
112 /* ALPS Modules with non-standard id */
113 { USB_DEVICE(0x044e, 0x3001) },
114 { USB_DEVICE(0x044e, 0x3002) },
116 /* Ericsson with non-standard id */
117 { USB_DEVICE(0x0bdb, 0x1002) },
119 /* Canyon CN-BTU1 with HID interfaces */
120 { USB_DEVICE(0x0c10, 0x0000) },
122 /* Broadcom BCM20702B0 (Dynex/Insignia) */
123 { USB_DEVICE(0x19ff, 0x0239), .driver_info = BTUSB_BCM_PATCHRAM },
125 /* Broadcom BCM43142A0 (Foxconn/Lenovo) */
126 { USB_VENDOR_AND_INTERFACE_INFO(0x105b, 0xff, 0x01, 0x01),
127 .driver_info = BTUSB_BCM_PATCHRAM },
129 /* Broadcom BCM920703 (HTC Vive) */
130 { USB_VENDOR_AND_INTERFACE_INFO(0x0bb4, 0xff, 0x01, 0x01),
131 .driver_info = BTUSB_BCM_PATCHRAM },
133 /* Foxconn - Hon Hai */
134 { USB_VENDOR_AND_INTERFACE_INFO(0x0489, 0xff, 0x01, 0x01),
135 .driver_info = BTUSB_BCM_PATCHRAM },
137 /* Lite-On Technology - Broadcom based */
138 { USB_VENDOR_AND_INTERFACE_INFO(0x04ca, 0xff, 0x01, 0x01),
139 .driver_info = BTUSB_BCM_PATCHRAM },
141 /* Broadcom devices with vendor specific id */
142 { USB_VENDOR_AND_INTERFACE_INFO(0x0a5c, 0xff, 0x01, 0x01),
143 .driver_info = BTUSB_BCM_PATCHRAM },
145 /* ASUSTek Computer - Broadcom based */
146 { USB_VENDOR_AND_INTERFACE_INFO(0x0b05, 0xff, 0x01, 0x01),
147 .driver_info = BTUSB_BCM_PATCHRAM },
149 /* Belkin F8065bf - Broadcom based */
150 { USB_VENDOR_AND_INTERFACE_INFO(0x050d, 0xff, 0x01, 0x01),
151 .driver_info = BTUSB_BCM_PATCHRAM },
153 /* IMC Networks - Broadcom based */
154 { USB_VENDOR_AND_INTERFACE_INFO(0x13d3, 0xff, 0x01, 0x01),
155 .driver_info = BTUSB_BCM_PATCHRAM },
157 /* Dell Computer - Broadcom based */
158 { USB_VENDOR_AND_INTERFACE_INFO(0x413c, 0xff, 0x01, 0x01),
159 .driver_info = BTUSB_BCM_PATCHRAM },
161 /* Toshiba Corp - Broadcom based */
162 { USB_VENDOR_AND_INTERFACE_INFO(0x0930, 0xff, 0x01, 0x01),
163 .driver_info = BTUSB_BCM_PATCHRAM },
165 /* Intel Bluetooth USB Bootloader (RAM module) */
166 { USB_DEVICE(0x8087, 0x0a5a),
167 .driver_info = BTUSB_INTEL_BOOT | BTUSB_BROKEN_ISOC },
169 { } /* Terminating entry */
172 MODULE_DEVICE_TABLE(usb, btusb_table);
174 static const struct usb_device_id blacklist_table[] = {
175 /* CSR BlueCore devices */
176 { USB_DEVICE(0x0a12, 0x0001), .driver_info = BTUSB_CSR },
178 /* Broadcom BCM2033 without firmware */
179 { USB_DEVICE(0x0a5c, 0x2033), .driver_info = BTUSB_IGNORE },
181 /* Broadcom BCM2045 devices */
182 { USB_DEVICE(0x0a5c, 0x2045), .driver_info = BTUSB_BCM2045 },
184 /* Atheros 3011 with sflash firmware */
185 { USB_DEVICE(0x0489, 0xe027), .driver_info = BTUSB_IGNORE },
186 { USB_DEVICE(0x0489, 0xe03d), .driver_info = BTUSB_IGNORE },
187 { USB_DEVICE(0x04f2, 0xaff1), .driver_info = BTUSB_IGNORE },
188 { USB_DEVICE(0x0930, 0x0215), .driver_info = BTUSB_IGNORE },
189 { USB_DEVICE(0x0cf3, 0x3002), .driver_info = BTUSB_IGNORE },
190 { USB_DEVICE(0x0cf3, 0xe019), .driver_info = BTUSB_IGNORE },
191 { USB_DEVICE(0x13d3, 0x3304), .driver_info = BTUSB_IGNORE },
193 /* Atheros AR9285 Malbec with sflash firmware */
194 { USB_DEVICE(0x03f0, 0x311d), .driver_info = BTUSB_IGNORE },
196 /* Atheros 3012 with sflash firmware */
197 { USB_DEVICE(0x0489, 0xe04d), .driver_info = BTUSB_ATH3012 },
198 { USB_DEVICE(0x0489, 0xe04e), .driver_info = BTUSB_ATH3012 },
199 { USB_DEVICE(0x0489, 0xe056), .driver_info = BTUSB_ATH3012 },
200 { USB_DEVICE(0x0489, 0xe057), .driver_info = BTUSB_ATH3012 },
201 { USB_DEVICE(0x0489, 0xe05f), .driver_info = BTUSB_ATH3012 },
202 { USB_DEVICE(0x0489, 0xe076), .driver_info = BTUSB_ATH3012 },
203 { USB_DEVICE(0x0489, 0xe078), .driver_info = BTUSB_ATH3012 },
204 { USB_DEVICE(0x0489, 0xe095), .driver_info = BTUSB_ATH3012 },
205 { USB_DEVICE(0x04c5, 0x1330), .driver_info = BTUSB_ATH3012 },
206 { USB_DEVICE(0x04ca, 0x3004), .driver_info = BTUSB_ATH3012 },
207 { USB_DEVICE(0x04ca, 0x3005), .driver_info = BTUSB_ATH3012 },
208 { USB_DEVICE(0x04ca, 0x3006), .driver_info = BTUSB_ATH3012 },
209 { USB_DEVICE(0x04ca, 0x3007), .driver_info = BTUSB_ATH3012 },
210 { USB_DEVICE(0x04ca, 0x3008), .driver_info = BTUSB_ATH3012 },
211 { USB_DEVICE(0x04ca, 0x300b), .driver_info = BTUSB_ATH3012 },
212 { USB_DEVICE(0x04ca, 0x300d), .driver_info = BTUSB_ATH3012 },
213 { USB_DEVICE(0x04ca, 0x300f), .driver_info = BTUSB_ATH3012 },
214 { USB_DEVICE(0x04ca, 0x3010), .driver_info = BTUSB_ATH3012 },
215 { USB_DEVICE(0x04ca, 0x3014), .driver_info = BTUSB_ATH3012 },
216 { USB_DEVICE(0x04ca, 0x3018), .driver_info = BTUSB_ATH3012 },
217 { USB_DEVICE(0x0930, 0x0219), .driver_info = BTUSB_ATH3012 },
218 { USB_DEVICE(0x0930, 0x021c), .driver_info = BTUSB_ATH3012 },
219 { USB_DEVICE(0x0930, 0x0220), .driver_info = BTUSB_ATH3012 },
220 { USB_DEVICE(0x0930, 0x0227), .driver_info = BTUSB_ATH3012 },
221 { USB_DEVICE(0x0b05, 0x17d0), .driver_info = BTUSB_ATH3012 },
222 { USB_DEVICE(0x0cf3, 0x0036), .driver_info = BTUSB_ATH3012 },
223 { USB_DEVICE(0x0cf3, 0x3004), .driver_info = BTUSB_ATH3012 },
224 { USB_DEVICE(0x0cf3, 0x3008), .driver_info = BTUSB_ATH3012 },
225 { USB_DEVICE(0x0cf3, 0x311d), .driver_info = BTUSB_ATH3012 },
226 { USB_DEVICE(0x0cf3, 0x311e), .driver_info = BTUSB_ATH3012 },
227 { USB_DEVICE(0x0cf3, 0x311f), .driver_info = BTUSB_ATH3012 },
228 { USB_DEVICE(0x0cf3, 0x3121), .driver_info = BTUSB_ATH3012 },
229 { USB_DEVICE(0x0cf3, 0x817a), .driver_info = BTUSB_ATH3012 },
230 { USB_DEVICE(0x0cf3, 0x817b), .driver_info = BTUSB_ATH3012 },
231 { USB_DEVICE(0x0cf3, 0xe003), .driver_info = BTUSB_ATH3012 },
232 { USB_DEVICE(0x0cf3, 0xe004), .driver_info = BTUSB_ATH3012 },
233 { USB_DEVICE(0x0cf3, 0xe005), .driver_info = BTUSB_ATH3012 },
234 { USB_DEVICE(0x0cf3, 0xe006), .driver_info = BTUSB_ATH3012 },
235 { USB_DEVICE(0x13d3, 0x3362), .driver_info = BTUSB_ATH3012 },
236 { USB_DEVICE(0x13d3, 0x3375), .driver_info = BTUSB_ATH3012 },
237 { USB_DEVICE(0x13d3, 0x3393), .driver_info = BTUSB_ATH3012 },
238 { USB_DEVICE(0x13d3, 0x3395), .driver_info = BTUSB_ATH3012 },
239 { USB_DEVICE(0x13d3, 0x3402), .driver_info = BTUSB_ATH3012 },
240 { USB_DEVICE(0x13d3, 0x3408), .driver_info = BTUSB_ATH3012 },
241 { USB_DEVICE(0x13d3, 0x3423), .driver_info = BTUSB_ATH3012 },
242 { USB_DEVICE(0x13d3, 0x3432), .driver_info = BTUSB_ATH3012 },
243 { USB_DEVICE(0x13d3, 0x3472), .driver_info = BTUSB_ATH3012 },
244 { USB_DEVICE(0x13d3, 0x3474), .driver_info = BTUSB_ATH3012 },
245 { USB_DEVICE(0x13d3, 0x3487), .driver_info = BTUSB_ATH3012 },
246 { USB_DEVICE(0x13d3, 0x3490), .driver_info = BTUSB_ATH3012 },
248 /* Atheros AR5BBU12 with sflash firmware */
249 { USB_DEVICE(0x0489, 0xe02c), .driver_info = BTUSB_IGNORE },
251 /* Atheros AR5BBU12 with sflash firmware */
252 { USB_DEVICE(0x0489, 0xe036), .driver_info = BTUSB_ATH3012 },
253 { USB_DEVICE(0x0489, 0xe03c), .driver_info = BTUSB_ATH3012 },
255 /* QCA ROME chipset */
256 { USB_DEVICE(0x0cf3, 0x535b), .driver_info = BTUSB_QCA_ROME |
257 BTUSB_WIDEBAND_SPEECH },
258 { USB_DEVICE(0x0cf3, 0xe007), .driver_info = BTUSB_QCA_ROME |
259 BTUSB_WIDEBAND_SPEECH },
260 { USB_DEVICE(0x0cf3, 0xe009), .driver_info = BTUSB_QCA_ROME |
261 BTUSB_WIDEBAND_SPEECH },
262 { USB_DEVICE(0x0cf3, 0xe010), .driver_info = BTUSB_QCA_ROME |
263 BTUSB_WIDEBAND_SPEECH },
264 { USB_DEVICE(0x0cf3, 0xe300), .driver_info = BTUSB_QCA_ROME |
265 BTUSB_WIDEBAND_SPEECH },
266 { USB_DEVICE(0x0cf3, 0xe301), .driver_info = BTUSB_QCA_ROME |
267 BTUSB_WIDEBAND_SPEECH },
268 { USB_DEVICE(0x0cf3, 0xe360), .driver_info = BTUSB_QCA_ROME |
269 BTUSB_WIDEBAND_SPEECH },
270 { USB_DEVICE(0x0cf3, 0xe500), .driver_info = BTUSB_QCA_ROME |
271 BTUSB_WIDEBAND_SPEECH },
272 { USB_DEVICE(0x0489, 0xe092), .driver_info = BTUSB_QCA_ROME |
273 BTUSB_WIDEBAND_SPEECH },
274 { USB_DEVICE(0x0489, 0xe09f), .driver_info = BTUSB_QCA_ROME |
275 BTUSB_WIDEBAND_SPEECH },
276 { USB_DEVICE(0x0489, 0xe0a2), .driver_info = BTUSB_QCA_ROME |
277 BTUSB_WIDEBAND_SPEECH },
278 { USB_DEVICE(0x04ca, 0x3011), .driver_info = BTUSB_QCA_ROME |
279 BTUSB_WIDEBAND_SPEECH },
280 { USB_DEVICE(0x04ca, 0x3015), .driver_info = BTUSB_QCA_ROME |
281 BTUSB_WIDEBAND_SPEECH },
282 { USB_DEVICE(0x04ca, 0x3016), .driver_info = BTUSB_QCA_ROME |
283 BTUSB_WIDEBAND_SPEECH },
284 { USB_DEVICE(0x04ca, 0x301a), .driver_info = BTUSB_QCA_ROME |
285 BTUSB_WIDEBAND_SPEECH },
286 { USB_DEVICE(0x04ca, 0x3021), .driver_info = BTUSB_QCA_ROME |
287 BTUSB_WIDEBAND_SPEECH },
288 { USB_DEVICE(0x13d3, 0x3491), .driver_info = BTUSB_QCA_ROME |
289 BTUSB_WIDEBAND_SPEECH },
290 { USB_DEVICE(0x13d3, 0x3496), .driver_info = BTUSB_QCA_ROME |
291 BTUSB_WIDEBAND_SPEECH },
292 { USB_DEVICE(0x13d3, 0x3501), .driver_info = BTUSB_QCA_ROME |
293 BTUSB_WIDEBAND_SPEECH },
295 /* QCA WCN6855 chipset */
296 { USB_DEVICE(0x0cf3, 0xe600), .driver_info = BTUSB_QCA_WCN6855 |
297 BTUSB_WIDEBAND_SPEECH |
298 BTUSB_VALID_LE_STATES },
299 { USB_DEVICE(0x0489, 0xe0cc), .driver_info = BTUSB_QCA_WCN6855 |
300 BTUSB_WIDEBAND_SPEECH |
301 BTUSB_VALID_LE_STATES },
302 { USB_DEVICE(0x0489, 0xe0d6), .driver_info = BTUSB_QCA_WCN6855 |
303 BTUSB_WIDEBAND_SPEECH |
304 BTUSB_VALID_LE_STATES },
305 { USB_DEVICE(0x0489, 0xe0e3), .driver_info = BTUSB_QCA_WCN6855 |
306 BTUSB_WIDEBAND_SPEECH |
307 BTUSB_VALID_LE_STATES },
308 { USB_DEVICE(0x10ab, 0x9309), .driver_info = BTUSB_QCA_WCN6855 |
309 BTUSB_WIDEBAND_SPEECH |
310 BTUSB_VALID_LE_STATES },
311 { USB_DEVICE(0x10ab, 0x9409), .driver_info = BTUSB_QCA_WCN6855 |
312 BTUSB_WIDEBAND_SPEECH |
313 BTUSB_VALID_LE_STATES },
314 { USB_DEVICE(0x0489, 0xe0d0), .driver_info = BTUSB_QCA_WCN6855 |
315 BTUSB_WIDEBAND_SPEECH |
316 BTUSB_VALID_LE_STATES },
318 /* Broadcom BCM2035 */
319 { USB_DEVICE(0x0a5c, 0x2009), .driver_info = BTUSB_BCM92035 },
320 { USB_DEVICE(0x0a5c, 0x200a), .driver_info = BTUSB_WRONG_SCO_MTU },
321 { USB_DEVICE(0x0a5c, 0x2035), .driver_info = BTUSB_WRONG_SCO_MTU },
323 /* Broadcom BCM2045 */
324 { USB_DEVICE(0x0a5c, 0x2039), .driver_info = BTUSB_WRONG_SCO_MTU },
325 { USB_DEVICE(0x0a5c, 0x2101), .driver_info = BTUSB_WRONG_SCO_MTU },
327 /* IBM/Lenovo ThinkPad with Broadcom chip */
328 { USB_DEVICE(0x0a5c, 0x201e), .driver_info = BTUSB_WRONG_SCO_MTU },
329 { USB_DEVICE(0x0a5c, 0x2110), .driver_info = BTUSB_WRONG_SCO_MTU },
331 /* HP laptop with Broadcom chip */
332 { USB_DEVICE(0x03f0, 0x171d), .driver_info = BTUSB_WRONG_SCO_MTU },
334 /* Dell laptop with Broadcom chip */
335 { USB_DEVICE(0x413c, 0x8126), .driver_info = BTUSB_WRONG_SCO_MTU },
337 /* Dell Wireless 370 and 410 devices */
338 { USB_DEVICE(0x413c, 0x8152), .driver_info = BTUSB_WRONG_SCO_MTU },
339 { USB_DEVICE(0x413c, 0x8156), .driver_info = BTUSB_WRONG_SCO_MTU },
341 /* Belkin F8T012 and F8T013 devices */
342 { USB_DEVICE(0x050d, 0x0012), .driver_info = BTUSB_WRONG_SCO_MTU },
343 { USB_DEVICE(0x050d, 0x0013), .driver_info = BTUSB_WRONG_SCO_MTU },
345 /* Asus WL-BTD202 device */
346 { USB_DEVICE(0x0b05, 0x1715), .driver_info = BTUSB_WRONG_SCO_MTU },
348 /* Kensington Bluetooth USB adapter */
349 { USB_DEVICE(0x047d, 0x105e), .driver_info = BTUSB_WRONG_SCO_MTU },
351 /* RTX Telecom based adapters with buggy SCO support */
352 { USB_DEVICE(0x0400, 0x0807), .driver_info = BTUSB_BROKEN_ISOC },
353 { USB_DEVICE(0x0400, 0x080a), .driver_info = BTUSB_BROKEN_ISOC },
355 /* CONWISE Technology based adapters with buggy SCO support */
356 { USB_DEVICE(0x0e5e, 0x6622),
357 .driver_info = BTUSB_BROKEN_ISOC | BTUSB_CW6622},
359 /* Roper Class 1 Bluetooth Dongle (Silicon Wave based) */
360 { USB_DEVICE(0x1310, 0x0001), .driver_info = BTUSB_SWAVE },
362 /* Digianswer devices */
363 { USB_DEVICE(0x08fd, 0x0001), .driver_info = BTUSB_DIGIANSWER },
364 { USB_DEVICE(0x08fd, 0x0002), .driver_info = BTUSB_IGNORE },
366 /* CSR BlueCore Bluetooth Sniffer */
367 { USB_DEVICE(0x0a12, 0x0002),
368 .driver_info = BTUSB_SNIFFER | BTUSB_BROKEN_ISOC },
370 /* Frontline ComProbe Bluetooth Sniffer */
371 { USB_DEVICE(0x16d3, 0x0002),
372 .driver_info = BTUSB_SNIFFER | BTUSB_BROKEN_ISOC },
374 /* Marvell Bluetooth devices */
375 { USB_DEVICE(0x1286, 0x2044), .driver_info = BTUSB_MARVELL },
376 { USB_DEVICE(0x1286, 0x2046), .driver_info = BTUSB_MARVELL },
377 { USB_DEVICE(0x1286, 0x204e), .driver_info = BTUSB_MARVELL },
379 /* Intel Bluetooth devices */
380 { USB_DEVICE(0x8087, 0x0025), .driver_info = BTUSB_INTEL_COMBINED },
381 { USB_DEVICE(0x8087, 0x0026), .driver_info = BTUSB_INTEL_COMBINED },
382 { USB_DEVICE(0x8087, 0x0029), .driver_info = BTUSB_INTEL_COMBINED },
383 { USB_DEVICE(0x8087, 0x0032), .driver_info = BTUSB_INTEL_COMBINED },
384 { USB_DEVICE(0x8087, 0x0033), .driver_info = BTUSB_INTEL_COMBINED },
385 { USB_DEVICE(0x8087, 0x07da), .driver_info = BTUSB_CSR },
386 { USB_DEVICE(0x8087, 0x07dc), .driver_info = BTUSB_INTEL_COMBINED |
387 BTUSB_INTEL_BROKEN_INITIAL_NCMD |
388 BTUSB_INTEL_BROKEN_SHUTDOWN_LED },
389 { USB_DEVICE(0x8087, 0x0a2a), .driver_info = BTUSB_INTEL_COMBINED |
390 BTUSB_INTEL_BROKEN_SHUTDOWN_LED },
391 { USB_DEVICE(0x8087, 0x0a2b), .driver_info = BTUSB_INTEL_COMBINED },
392 { USB_DEVICE(0x8087, 0x0aa7), .driver_info = BTUSB_INTEL_COMBINED |
393 BTUSB_INTEL_BROKEN_SHUTDOWN_LED },
394 { USB_DEVICE(0x8087, 0x0aaa), .driver_info = BTUSB_INTEL_COMBINED },
396 /* Other Intel Bluetooth devices */
397 { USB_VENDOR_AND_INTERFACE_INFO(0x8087, 0xe0, 0x01, 0x01),
398 .driver_info = BTUSB_IGNORE },
400 /* Realtek 8822CE Bluetooth devices */
401 { USB_DEVICE(0x0bda, 0xb00c), .driver_info = BTUSB_REALTEK |
402 BTUSB_WIDEBAND_SPEECH },
403 { USB_DEVICE(0x0bda, 0xc822), .driver_info = BTUSB_REALTEK |
404 BTUSB_WIDEBAND_SPEECH },
406 /* Realtek 8852AE Bluetooth devices */
407 { USB_DEVICE(0x0bda, 0xc852), .driver_info = BTUSB_REALTEK |
408 BTUSB_WIDEBAND_SPEECH },
409 { USB_DEVICE(0x0bda, 0x385a), .driver_info = BTUSB_REALTEK |
410 BTUSB_WIDEBAND_SPEECH },
411 { USB_DEVICE(0x0bda, 0x4852), .driver_info = BTUSB_REALTEK |
412 BTUSB_WIDEBAND_SPEECH },
413 { USB_DEVICE(0x04c5, 0x165c), .driver_info = BTUSB_REALTEK |
414 BTUSB_WIDEBAND_SPEECH },
415 { USB_DEVICE(0x04ca, 0x4006), .driver_info = BTUSB_REALTEK |
416 BTUSB_WIDEBAND_SPEECH },
418 /* Realtek Bluetooth devices */
419 { USB_VENDOR_AND_INTERFACE_INFO(0x0bda, 0xe0, 0x01, 0x01),
420 .driver_info = BTUSB_REALTEK },
422 /* MediaTek Bluetooth devices */
423 { USB_VENDOR_AND_INTERFACE_INFO(0x0e8d, 0xe0, 0x01, 0x01),
424 .driver_info = BTUSB_MEDIATEK |
425 BTUSB_WIDEBAND_SPEECH |
426 BTUSB_VALID_LE_STATES },
428 /* Additional MediaTek MT7615E Bluetooth devices */
429 { USB_DEVICE(0x13d3, 0x3560), .driver_info = BTUSB_MEDIATEK},
431 /* Additional MediaTek MT7668 Bluetooth devices */
432 { USB_DEVICE(0x043e, 0x3109), .driver_info = BTUSB_MEDIATEK |
433 BTUSB_WIDEBAND_SPEECH |
434 BTUSB_VALID_LE_STATES },
436 /* Additional MediaTek MT7921 Bluetooth devices */
437 { USB_DEVICE(0x04ca, 0x3802), .driver_info = BTUSB_MEDIATEK |
438 BTUSB_WIDEBAND_SPEECH |
439 BTUSB_VALID_LE_STATES },
440 { USB_DEVICE(0x13d3, 0x3563), .driver_info = BTUSB_MEDIATEK |
441 BTUSB_WIDEBAND_SPEECH |
442 BTUSB_VALID_LE_STATES },
443 { USB_DEVICE(0x13d3, 0x3564), .driver_info = BTUSB_MEDIATEK |
444 BTUSB_WIDEBAND_SPEECH |
445 BTUSB_VALID_LE_STATES },
446 { USB_DEVICE(0x0489, 0xe0cd), .driver_info = BTUSB_MEDIATEK |
447 BTUSB_WIDEBAND_SPEECH |
448 BTUSB_VALID_LE_STATES },
450 /* MediaTek MT7922A Bluetooth devices */
451 { USB_DEVICE(0x0489, 0xe0d8), .driver_info = BTUSB_MEDIATEK |
452 BTUSB_WIDEBAND_SPEECH |
453 BTUSB_VALID_LE_STATES },
454 { USB_DEVICE(0x0489, 0xe0d9), .driver_info = BTUSB_MEDIATEK |
455 BTUSB_WIDEBAND_SPEECH |
456 BTUSB_VALID_LE_STATES },
458 /* Additional Realtek 8723AE Bluetooth devices */
459 { USB_DEVICE(0x0930, 0x021d), .driver_info = BTUSB_REALTEK },
460 { USB_DEVICE(0x13d3, 0x3394), .driver_info = BTUSB_REALTEK },
462 /* Additional Realtek 8723BE Bluetooth devices */
463 { USB_DEVICE(0x0489, 0xe085), .driver_info = BTUSB_REALTEK },
464 { USB_DEVICE(0x0489, 0xe08b), .driver_info = BTUSB_REALTEK },
465 { USB_DEVICE(0x13d3, 0x3410), .driver_info = BTUSB_REALTEK },
466 { USB_DEVICE(0x13d3, 0x3416), .driver_info = BTUSB_REALTEK },
467 { USB_DEVICE(0x13d3, 0x3459), .driver_info = BTUSB_REALTEK },
468 { USB_DEVICE(0x13d3, 0x3494), .driver_info = BTUSB_REALTEK },
470 /* Additional Realtek 8723BU Bluetooth devices */
471 { USB_DEVICE(0x7392, 0xa611), .driver_info = BTUSB_REALTEK },
473 /* Additional Realtek 8723DE Bluetooth devices */
474 { USB_DEVICE(0x0bda, 0xb009), .driver_info = BTUSB_REALTEK },
475 { USB_DEVICE(0x2ff8, 0xb011), .driver_info = BTUSB_REALTEK },
477 /* Additional Realtek 8761B Bluetooth devices */
478 { USB_DEVICE(0x2357, 0x0604), .driver_info = BTUSB_REALTEK |
479 BTUSB_WIDEBAND_SPEECH },
481 /* Additional Realtek 8761BU Bluetooth devices */
482 { USB_DEVICE(0x0b05, 0x190e), .driver_info = BTUSB_REALTEK |
483 BTUSB_WIDEBAND_SPEECH },
485 /* Additional Realtek 8821AE Bluetooth devices */
486 { USB_DEVICE(0x0b05, 0x17dc), .driver_info = BTUSB_REALTEK },
487 { USB_DEVICE(0x13d3, 0x3414), .driver_info = BTUSB_REALTEK },
488 { USB_DEVICE(0x13d3, 0x3458), .driver_info = BTUSB_REALTEK },
489 { USB_DEVICE(0x13d3, 0x3461), .driver_info = BTUSB_REALTEK },
490 { USB_DEVICE(0x13d3, 0x3462), .driver_info = BTUSB_REALTEK },
492 /* Additional Realtek 8822BE Bluetooth devices */
493 { USB_DEVICE(0x13d3, 0x3526), .driver_info = BTUSB_REALTEK },
494 { USB_DEVICE(0x0b05, 0x185c), .driver_info = BTUSB_REALTEK },
496 /* Additional Realtek 8822CE Bluetooth devices */
497 { USB_DEVICE(0x04ca, 0x4005), .driver_info = BTUSB_REALTEK |
498 BTUSB_WIDEBAND_SPEECH },
499 { USB_DEVICE(0x04c5, 0x161f), .driver_info = BTUSB_REALTEK |
500 BTUSB_WIDEBAND_SPEECH },
501 { USB_DEVICE(0x0b05, 0x18ef), .driver_info = BTUSB_REALTEK |
502 BTUSB_WIDEBAND_SPEECH },
503 { USB_DEVICE(0x13d3, 0x3548), .driver_info = BTUSB_REALTEK |
504 BTUSB_WIDEBAND_SPEECH },
505 { USB_DEVICE(0x13d3, 0x3549), .driver_info = BTUSB_REALTEK |
506 BTUSB_WIDEBAND_SPEECH },
507 { USB_DEVICE(0x13d3, 0x3553), .driver_info = BTUSB_REALTEK |
508 BTUSB_WIDEBAND_SPEECH },
509 { USB_DEVICE(0x13d3, 0x3555), .driver_info = BTUSB_REALTEK |
510 BTUSB_WIDEBAND_SPEECH },
511 { USB_DEVICE(0x2ff8, 0x3051), .driver_info = BTUSB_REALTEK |
512 BTUSB_WIDEBAND_SPEECH },
513 { USB_DEVICE(0x1358, 0xc123), .driver_info = BTUSB_REALTEK |
514 BTUSB_WIDEBAND_SPEECH },
515 { USB_DEVICE(0x0bda, 0xc123), .driver_info = BTUSB_REALTEK |
516 BTUSB_WIDEBAND_SPEECH },
517 { USB_DEVICE(0x0cb5, 0xc547), .driver_info = BTUSB_REALTEK |
518 BTUSB_WIDEBAND_SPEECH },
520 /* Silicon Wave based devices */
521 { USB_DEVICE(0x0c10, 0x0000), .driver_info = BTUSB_SWAVE },
523 { } /* Terminating entry */
526 /* The Bluetooth USB module build into some devices needs to be reset on resume,
527 * this is a problem with the platform (likely shutting off all power) not with
528 * the module itself. So we use a DMI list to match known broken platforms.
530 static const struct dmi_system_id btusb_needs_reset_resume_table[] = {
532 /* Dell OptiPlex 3060 (QCA ROME device 0cf3:e007) */
534 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
535 DMI_MATCH(DMI_PRODUCT_NAME, "OptiPlex 3060"),
539 /* Dell XPS 9360 (QCA ROME device 0cf3:e300) */
541 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
542 DMI_MATCH(DMI_PRODUCT_NAME, "XPS 13 9360"),
546 /* Dell Inspiron 5565 (QCA ROME device 0cf3:e009) */
548 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
549 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 5565"),
555 #define BTUSB_MAX_ISOC_FRAMES 10
557 #define BTUSB_INTR_RUNNING 0
558 #define BTUSB_BULK_RUNNING 1
559 #define BTUSB_ISOC_RUNNING 2
560 #define BTUSB_SUSPENDING 3
561 #define BTUSB_DID_ISO_RESUME 4
562 #define BTUSB_BOOTLOADER 5
563 #define BTUSB_DOWNLOADING 6
564 #define BTUSB_FIRMWARE_LOADED 7
565 #define BTUSB_FIRMWARE_FAILED 8
566 #define BTUSB_BOOTING 9
567 #define BTUSB_DIAG_RUNNING 10
568 #define BTUSB_OOB_WAKE_ENABLED 11
569 #define BTUSB_HW_RESET_ACTIVE 12
570 #define BTUSB_TX_WAIT_VND_EVT 13
571 #define BTUSB_WAKEUP_AUTOSUSPEND 14
572 #define BTUSB_USE_ALT3_FOR_WBS 15
575 struct hci_dev *hdev;
576 struct usb_device *udev;
577 struct usb_interface *intf;
578 struct usb_interface *isoc;
579 struct usb_interface *diag;
584 struct work_struct work;
585 struct work_struct waker;
587 struct usb_anchor deferred;
588 struct usb_anchor tx_anchor;
592 struct usb_anchor intr_anchor;
593 struct usb_anchor bulk_anchor;
594 struct usb_anchor isoc_anchor;
595 struct usb_anchor diag_anchor;
596 struct usb_anchor ctrl_anchor;
599 struct sk_buff *evt_skb;
600 struct sk_buff *acl_skb;
601 struct sk_buff *sco_skb;
603 struct usb_endpoint_descriptor *intr_ep;
604 struct usb_endpoint_descriptor *bulk_tx_ep;
605 struct usb_endpoint_descriptor *bulk_rx_ep;
606 struct usb_endpoint_descriptor *isoc_tx_ep;
607 struct usb_endpoint_descriptor *isoc_rx_ep;
608 struct usb_endpoint_descriptor *diag_tx_ep;
609 struct usb_endpoint_descriptor *diag_rx_ep;
611 struct gpio_desc *reset_gpio;
616 unsigned int sco_num;
617 unsigned int air_mode;
618 bool usb_alt6_packet_flow;
622 int (*recv_event)(struct hci_dev *hdev, struct sk_buff *skb);
623 int (*recv_acl)(struct hci_dev *hdev, struct sk_buff *skb);
624 int (*recv_bulk)(struct btusb_data *data, void *buffer, int count);
626 int (*setup_on_usb)(struct hci_dev *hdev);
628 int oob_wake_irq; /* irq for out-of-band wake-on-bt */
629 unsigned cmd_timeout_cnt;
632 static void btusb_intel_cmd_timeout(struct hci_dev *hdev)
634 struct btusb_data *data = hci_get_drvdata(hdev);
635 struct gpio_desc *reset_gpio = data->reset_gpio;
637 if (++data->cmd_timeout_cnt < 5)
641 bt_dev_err(hdev, "No way to reset. Ignoring and continuing");
646 * Toggle the hard reset line if the platform provides one. The reset
647 * is going to yank the device off the USB and then replug. So doing
648 * once is enough. The cleanup is handled correctly on the way out
649 * (standard USB disconnect), and the new device is detected cleanly
650 * and bound to the driver again like it should be.
652 if (test_and_set_bit(BTUSB_HW_RESET_ACTIVE, &data->flags)) {
653 bt_dev_err(hdev, "last reset failed? Not resetting again");
657 bt_dev_err(hdev, "Initiating HW reset via gpio");
658 gpiod_set_value_cansleep(reset_gpio, 1);
660 gpiod_set_value_cansleep(reset_gpio, 0);
663 static void btusb_rtl_cmd_timeout(struct hci_dev *hdev)
665 struct btusb_data *data = hci_get_drvdata(hdev);
666 struct gpio_desc *reset_gpio = data->reset_gpio;
668 if (++data->cmd_timeout_cnt < 5)
672 bt_dev_err(hdev, "No gpio to reset Realtek device, ignoring");
676 /* Toggle the hard reset line. The Realtek device is going to
677 * yank itself off the USB and then replug. The cleanup is handled
678 * correctly on the way out (standard USB disconnect), and the new
679 * device is detected cleanly and bound to the driver again like
682 if (test_and_set_bit(BTUSB_HW_RESET_ACTIVE, &data->flags)) {
683 bt_dev_err(hdev, "last reset failed? Not resetting again");
687 bt_dev_err(hdev, "Reset Realtek device via gpio");
688 gpiod_set_value_cansleep(reset_gpio, 1);
690 gpiod_set_value_cansleep(reset_gpio, 0);
693 static void btusb_qca_cmd_timeout(struct hci_dev *hdev)
695 struct btusb_data *data = hci_get_drvdata(hdev);
698 if (++data->cmd_timeout_cnt < 5)
701 bt_dev_err(hdev, "Multiple cmd timeouts seen. Resetting usb device.");
702 /* This is not an unbalanced PM reference since the device will reset */
703 err = usb_autopm_get_interface(data->intf);
705 usb_queue_reset_device(data->intf);
707 bt_dev_err(hdev, "Failed usb_autopm_get_interface with %d", err);
710 static inline void btusb_free_frags(struct btusb_data *data)
714 spin_lock_irqsave(&data->rxlock, flags);
716 kfree_skb(data->evt_skb);
717 data->evt_skb = NULL;
719 kfree_skb(data->acl_skb);
720 data->acl_skb = NULL;
722 kfree_skb(data->sco_skb);
723 data->sco_skb = NULL;
725 spin_unlock_irqrestore(&data->rxlock, flags);
728 static int btusb_recv_intr(struct btusb_data *data, void *buffer, int count)
734 spin_lock_irqsave(&data->rxlock, flags);
741 skb = bt_skb_alloc(HCI_MAX_EVENT_SIZE, GFP_ATOMIC);
747 hci_skb_pkt_type(skb) = HCI_EVENT_PKT;
748 hci_skb_expect(skb) = HCI_EVENT_HDR_SIZE;
751 len = min_t(uint, hci_skb_expect(skb), count);
752 skb_put_data(skb, buffer, len);
756 hci_skb_expect(skb) -= len;
758 if (skb->len == HCI_EVENT_HDR_SIZE) {
759 /* Complete event header */
760 hci_skb_expect(skb) = hci_event_hdr(skb)->plen;
762 if (skb_tailroom(skb) < hci_skb_expect(skb)) {
771 if (!hci_skb_expect(skb)) {
773 data->recv_event(data->hdev, skb);
779 spin_unlock_irqrestore(&data->rxlock, flags);
784 static int btusb_recv_bulk(struct btusb_data *data, void *buffer, int count)
790 spin_lock_irqsave(&data->rxlock, flags);
797 skb = bt_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC);
803 hci_skb_pkt_type(skb) = HCI_ACLDATA_PKT;
804 hci_skb_expect(skb) = HCI_ACL_HDR_SIZE;
807 len = min_t(uint, hci_skb_expect(skb), count);
808 skb_put_data(skb, buffer, len);
812 hci_skb_expect(skb) -= len;
814 if (skb->len == HCI_ACL_HDR_SIZE) {
815 __le16 dlen = hci_acl_hdr(skb)->dlen;
817 /* Complete ACL header */
818 hci_skb_expect(skb) = __le16_to_cpu(dlen);
820 if (skb_tailroom(skb) < hci_skb_expect(skb)) {
829 if (!hci_skb_expect(skb)) {
831 data->recv_acl(data->hdev, skb);
837 spin_unlock_irqrestore(&data->rxlock, flags);
842 static int btusb_recv_isoc(struct btusb_data *data, void *buffer, int count)
848 spin_lock_irqsave(&data->rxlock, flags);
855 skb = bt_skb_alloc(HCI_MAX_SCO_SIZE, GFP_ATOMIC);
861 hci_skb_pkt_type(skb) = HCI_SCODATA_PKT;
862 hci_skb_expect(skb) = HCI_SCO_HDR_SIZE;
865 len = min_t(uint, hci_skb_expect(skb), count);
866 skb_put_data(skb, buffer, len);
870 hci_skb_expect(skb) -= len;
872 if (skb->len == HCI_SCO_HDR_SIZE) {
873 /* Complete SCO header */
874 hci_skb_expect(skb) = hci_sco_hdr(skb)->dlen;
876 if (skb_tailroom(skb) < hci_skb_expect(skb)) {
885 if (!hci_skb_expect(skb)) {
887 hci_recv_frame(data->hdev, skb);
893 spin_unlock_irqrestore(&data->rxlock, flags);
898 static void btusb_intr_complete(struct urb *urb)
900 struct hci_dev *hdev = urb->context;
901 struct btusb_data *data = hci_get_drvdata(hdev);
904 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
907 if (!test_bit(HCI_RUNNING, &hdev->flags))
910 if (urb->status == 0) {
911 hdev->stat.byte_rx += urb->actual_length;
913 if (btusb_recv_intr(data, urb->transfer_buffer,
914 urb->actual_length) < 0) {
915 bt_dev_err(hdev, "corrupted event packet");
918 } else if (urb->status == -ENOENT) {
919 /* Avoid suspend failed when usb_kill_urb */
923 if (!test_bit(BTUSB_INTR_RUNNING, &data->flags))
926 usb_mark_last_busy(data->udev);
927 usb_anchor_urb(urb, &data->intr_anchor);
929 err = usb_submit_urb(urb, GFP_ATOMIC);
931 /* -EPERM: urb is being killed;
932 * -ENODEV: device got disconnected
934 if (err != -EPERM && err != -ENODEV)
935 bt_dev_err(hdev, "urb %p failed to resubmit (%d)",
937 usb_unanchor_urb(urb);
941 static int btusb_submit_intr_urb(struct hci_dev *hdev, gfp_t mem_flags)
943 struct btusb_data *data = hci_get_drvdata(hdev);
949 BT_DBG("%s", hdev->name);
954 urb = usb_alloc_urb(0, mem_flags);
958 size = le16_to_cpu(data->intr_ep->wMaxPacketSize);
960 buf = kmalloc(size, mem_flags);
966 pipe = usb_rcvintpipe(data->udev, data->intr_ep->bEndpointAddress);
968 usb_fill_int_urb(urb, data->udev, pipe, buf, size,
969 btusb_intr_complete, hdev, data->intr_ep->bInterval);
971 urb->transfer_flags |= URB_FREE_BUFFER;
973 usb_anchor_urb(urb, &data->intr_anchor);
975 err = usb_submit_urb(urb, mem_flags);
977 if (err != -EPERM && err != -ENODEV)
978 bt_dev_err(hdev, "urb %p submission failed (%d)",
980 usb_unanchor_urb(urb);
988 static void btusb_bulk_complete(struct urb *urb)
990 struct hci_dev *hdev = urb->context;
991 struct btusb_data *data = hci_get_drvdata(hdev);
994 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
997 if (!test_bit(HCI_RUNNING, &hdev->flags))
1000 if (urb->status == 0) {
1001 hdev->stat.byte_rx += urb->actual_length;
1003 if (data->recv_bulk(data, urb->transfer_buffer,
1004 urb->actual_length) < 0) {
1005 bt_dev_err(hdev, "corrupted ACL packet");
1006 hdev->stat.err_rx++;
1008 } else if (urb->status == -ENOENT) {
1009 /* Avoid suspend failed when usb_kill_urb */
1013 if (!test_bit(BTUSB_BULK_RUNNING, &data->flags))
1016 usb_anchor_urb(urb, &data->bulk_anchor);
1017 usb_mark_last_busy(data->udev);
1019 err = usb_submit_urb(urb, GFP_ATOMIC);
1021 /* -EPERM: urb is being killed;
1022 * -ENODEV: device got disconnected
1024 if (err != -EPERM && err != -ENODEV)
1025 bt_dev_err(hdev, "urb %p failed to resubmit (%d)",
1027 usb_unanchor_urb(urb);
1031 static int btusb_submit_bulk_urb(struct hci_dev *hdev, gfp_t mem_flags)
1033 struct btusb_data *data = hci_get_drvdata(hdev);
1037 int err, size = HCI_MAX_FRAME_SIZE;
1039 BT_DBG("%s", hdev->name);
1041 if (!data->bulk_rx_ep)
1044 urb = usb_alloc_urb(0, mem_flags);
1048 buf = kmalloc(size, mem_flags);
1054 pipe = usb_rcvbulkpipe(data->udev, data->bulk_rx_ep->bEndpointAddress);
1056 usb_fill_bulk_urb(urb, data->udev, pipe, buf, size,
1057 btusb_bulk_complete, hdev);
1059 urb->transfer_flags |= URB_FREE_BUFFER;
1061 usb_mark_last_busy(data->udev);
1062 usb_anchor_urb(urb, &data->bulk_anchor);
1064 err = usb_submit_urb(urb, mem_flags);
1066 if (err != -EPERM && err != -ENODEV)
1067 bt_dev_err(hdev, "urb %p submission failed (%d)",
1069 usb_unanchor_urb(urb);
1077 static void btusb_isoc_complete(struct urb *urb)
1079 struct hci_dev *hdev = urb->context;
1080 struct btusb_data *data = hci_get_drvdata(hdev);
1083 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
1084 urb->actual_length);
1086 if (!test_bit(HCI_RUNNING, &hdev->flags))
1089 if (urb->status == 0) {
1090 for (i = 0; i < urb->number_of_packets; i++) {
1091 unsigned int offset = urb->iso_frame_desc[i].offset;
1092 unsigned int length = urb->iso_frame_desc[i].actual_length;
1094 if (urb->iso_frame_desc[i].status)
1097 hdev->stat.byte_rx += length;
1099 if (btusb_recv_isoc(data, urb->transfer_buffer + offset,
1101 bt_dev_err(hdev, "corrupted SCO packet");
1102 hdev->stat.err_rx++;
1105 } else if (urb->status == -ENOENT) {
1106 /* Avoid suspend failed when usb_kill_urb */
1110 if (!test_bit(BTUSB_ISOC_RUNNING, &data->flags))
1113 usb_anchor_urb(urb, &data->isoc_anchor);
1115 err = usb_submit_urb(urb, GFP_ATOMIC);
1117 /* -EPERM: urb is being killed;
1118 * -ENODEV: device got disconnected
1120 if (err != -EPERM && err != -ENODEV)
1121 bt_dev_err(hdev, "urb %p failed to resubmit (%d)",
1123 usb_unanchor_urb(urb);
1127 static inline void __fill_isoc_descriptor_msbc(struct urb *urb, int len,
1128 int mtu, struct btusb_data *data)
1131 unsigned int interval;
1133 BT_DBG("len %d mtu %d", len, mtu);
1135 /* For mSBC ALT 6 setting the host will send the packet at continuous
1136 * flow. As per core spec 5, vol 4, part B, table 2.1. For ALT setting
1137 * 6 the HCI PACKET INTERVAL should be 7.5ms for every usb packets.
1138 * To maintain the rate we send 63bytes of usb packets alternatively for
1139 * 7ms and 8ms to maintain the rate as 7.5ms.
1141 if (data->usb_alt6_packet_flow) {
1143 data->usb_alt6_packet_flow = false;
1146 data->usb_alt6_packet_flow = true;
1149 for (i = 0; i < interval; i++) {
1150 urb->iso_frame_desc[i].offset = offset;
1151 urb->iso_frame_desc[i].length = offset;
1154 if (len && i < BTUSB_MAX_ISOC_FRAMES) {
1155 urb->iso_frame_desc[i].offset = offset;
1156 urb->iso_frame_desc[i].length = len;
1160 urb->number_of_packets = i;
1163 static inline void __fill_isoc_descriptor(struct urb *urb, int len, int mtu)
1167 BT_DBG("len %d mtu %d", len, mtu);
1169 for (i = 0; i < BTUSB_MAX_ISOC_FRAMES && len >= mtu;
1170 i++, offset += mtu, len -= mtu) {
1171 urb->iso_frame_desc[i].offset = offset;
1172 urb->iso_frame_desc[i].length = mtu;
1175 if (len && i < BTUSB_MAX_ISOC_FRAMES) {
1176 urb->iso_frame_desc[i].offset = offset;
1177 urb->iso_frame_desc[i].length = len;
1181 urb->number_of_packets = i;
1184 static int btusb_submit_isoc_urb(struct hci_dev *hdev, gfp_t mem_flags)
1186 struct btusb_data *data = hci_get_drvdata(hdev);
1192 BT_DBG("%s", hdev->name);
1194 if (!data->isoc_rx_ep)
1197 urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, mem_flags);
1201 size = le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize) *
1202 BTUSB_MAX_ISOC_FRAMES;
1204 buf = kmalloc(size, mem_flags);
1210 pipe = usb_rcvisocpipe(data->udev, data->isoc_rx_ep->bEndpointAddress);
1212 usb_fill_int_urb(urb, data->udev, pipe, buf, size, btusb_isoc_complete,
1213 hdev, data->isoc_rx_ep->bInterval);
1215 urb->transfer_flags = URB_FREE_BUFFER | URB_ISO_ASAP;
1217 __fill_isoc_descriptor(urb, size,
1218 le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize));
1220 usb_anchor_urb(urb, &data->isoc_anchor);
1222 err = usb_submit_urb(urb, mem_flags);
1224 if (err != -EPERM && err != -ENODEV)
1225 bt_dev_err(hdev, "urb %p submission failed (%d)",
1227 usb_unanchor_urb(urb);
1235 static void btusb_diag_complete(struct urb *urb)
1237 struct hci_dev *hdev = urb->context;
1238 struct btusb_data *data = hci_get_drvdata(hdev);
1241 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
1242 urb->actual_length);
1244 if (urb->status == 0) {
1245 struct sk_buff *skb;
1247 skb = bt_skb_alloc(urb->actual_length, GFP_ATOMIC);
1249 skb_put_data(skb, urb->transfer_buffer,
1250 urb->actual_length);
1251 hci_recv_diag(hdev, skb);
1253 } else if (urb->status == -ENOENT) {
1254 /* Avoid suspend failed when usb_kill_urb */
1258 if (!test_bit(BTUSB_DIAG_RUNNING, &data->flags))
1261 usb_anchor_urb(urb, &data->diag_anchor);
1262 usb_mark_last_busy(data->udev);
1264 err = usb_submit_urb(urb, GFP_ATOMIC);
1266 /* -EPERM: urb is being killed;
1267 * -ENODEV: device got disconnected
1269 if (err != -EPERM && err != -ENODEV)
1270 bt_dev_err(hdev, "urb %p failed to resubmit (%d)",
1272 usb_unanchor_urb(urb);
1276 static int btusb_submit_diag_urb(struct hci_dev *hdev, gfp_t mem_flags)
1278 struct btusb_data *data = hci_get_drvdata(hdev);
1282 int err, size = HCI_MAX_FRAME_SIZE;
1284 BT_DBG("%s", hdev->name);
1286 if (!data->diag_rx_ep)
1289 urb = usb_alloc_urb(0, mem_flags);
1293 buf = kmalloc(size, mem_flags);
1299 pipe = usb_rcvbulkpipe(data->udev, data->diag_rx_ep->bEndpointAddress);
1301 usb_fill_bulk_urb(urb, data->udev, pipe, buf, size,
1302 btusb_diag_complete, hdev);
1304 urb->transfer_flags |= URB_FREE_BUFFER;
1306 usb_mark_last_busy(data->udev);
1307 usb_anchor_urb(urb, &data->diag_anchor);
1309 err = usb_submit_urb(urb, mem_flags);
1311 if (err != -EPERM && err != -ENODEV)
1312 bt_dev_err(hdev, "urb %p submission failed (%d)",
1314 usb_unanchor_urb(urb);
1322 static void btusb_tx_complete(struct urb *urb)
1324 struct sk_buff *skb = urb->context;
1325 struct hci_dev *hdev = (struct hci_dev *)skb->dev;
1326 struct btusb_data *data = hci_get_drvdata(hdev);
1327 unsigned long flags;
1329 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
1330 urb->actual_length);
1332 if (!test_bit(HCI_RUNNING, &hdev->flags))
1336 hdev->stat.byte_tx += urb->transfer_buffer_length;
1338 hdev->stat.err_tx++;
1341 spin_lock_irqsave(&data->txlock, flags);
1342 data->tx_in_flight--;
1343 spin_unlock_irqrestore(&data->txlock, flags);
1345 kfree(urb->setup_packet);
1350 static void btusb_isoc_tx_complete(struct urb *urb)
1352 struct sk_buff *skb = urb->context;
1353 struct hci_dev *hdev = (struct hci_dev *)skb->dev;
1355 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
1356 urb->actual_length);
1358 if (!test_bit(HCI_RUNNING, &hdev->flags))
1362 hdev->stat.byte_tx += urb->transfer_buffer_length;
1364 hdev->stat.err_tx++;
1367 kfree(urb->setup_packet);
1372 static int btusb_open(struct hci_dev *hdev)
1374 struct btusb_data *data = hci_get_drvdata(hdev);
1377 BT_DBG("%s", hdev->name);
1379 err = usb_autopm_get_interface(data->intf);
1383 /* Patching USB firmware files prior to starting any URBs of HCI path
1384 * It is more safe to use USB bulk channel for downloading USB patch
1386 if (data->setup_on_usb) {
1387 err = data->setup_on_usb(hdev);
1392 data->intf->needs_remote_wakeup = 1;
1394 if (test_and_set_bit(BTUSB_INTR_RUNNING, &data->flags))
1397 err = btusb_submit_intr_urb(hdev, GFP_KERNEL);
1401 err = btusb_submit_bulk_urb(hdev, GFP_KERNEL);
1403 usb_kill_anchored_urbs(&data->intr_anchor);
1407 set_bit(BTUSB_BULK_RUNNING, &data->flags);
1408 btusb_submit_bulk_urb(hdev, GFP_KERNEL);
1411 if (!btusb_submit_diag_urb(hdev, GFP_KERNEL))
1412 set_bit(BTUSB_DIAG_RUNNING, &data->flags);
1416 usb_autopm_put_interface(data->intf);
1420 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
1422 usb_autopm_put_interface(data->intf);
1426 static void btusb_stop_traffic(struct btusb_data *data)
1428 usb_kill_anchored_urbs(&data->intr_anchor);
1429 usb_kill_anchored_urbs(&data->bulk_anchor);
1430 usb_kill_anchored_urbs(&data->isoc_anchor);
1431 usb_kill_anchored_urbs(&data->diag_anchor);
1432 usb_kill_anchored_urbs(&data->ctrl_anchor);
1435 static int btusb_close(struct hci_dev *hdev)
1437 struct btusb_data *data = hci_get_drvdata(hdev);
1440 BT_DBG("%s", hdev->name);
1442 cancel_work_sync(&data->work);
1443 cancel_work_sync(&data->waker);
1445 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1446 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
1447 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
1448 clear_bit(BTUSB_DIAG_RUNNING, &data->flags);
1450 btusb_stop_traffic(data);
1451 btusb_free_frags(data);
1453 err = usb_autopm_get_interface(data->intf);
1457 data->intf->needs_remote_wakeup = 0;
1459 /* Enable remote wake up for auto-suspend */
1460 if (test_bit(BTUSB_WAKEUP_AUTOSUSPEND, &data->flags))
1461 data->intf->needs_remote_wakeup = 1;
1463 usb_autopm_put_interface(data->intf);
1466 usb_scuttle_anchored_urbs(&data->deferred);
1470 static int btusb_flush(struct hci_dev *hdev)
1472 struct btusb_data *data = hci_get_drvdata(hdev);
1474 BT_DBG("%s", hdev->name);
1476 usb_kill_anchored_urbs(&data->tx_anchor);
1477 btusb_free_frags(data);
1482 static struct urb *alloc_ctrl_urb(struct hci_dev *hdev, struct sk_buff *skb)
1484 struct btusb_data *data = hci_get_drvdata(hdev);
1485 struct usb_ctrlrequest *dr;
1489 urb = usb_alloc_urb(0, GFP_KERNEL);
1491 return ERR_PTR(-ENOMEM);
1493 dr = kmalloc(sizeof(*dr), GFP_KERNEL);
1496 return ERR_PTR(-ENOMEM);
1499 dr->bRequestType = data->cmdreq_type;
1500 dr->bRequest = data->cmdreq;
1503 dr->wLength = __cpu_to_le16(skb->len);
1505 pipe = usb_sndctrlpipe(data->udev, 0x00);
1507 usb_fill_control_urb(urb, data->udev, pipe, (void *)dr,
1508 skb->data, skb->len, btusb_tx_complete, skb);
1510 skb->dev = (void *)hdev;
1515 static struct urb *alloc_bulk_urb(struct hci_dev *hdev, struct sk_buff *skb)
1517 struct btusb_data *data = hci_get_drvdata(hdev);
1521 if (!data->bulk_tx_ep)
1522 return ERR_PTR(-ENODEV);
1524 urb = usb_alloc_urb(0, GFP_KERNEL);
1526 return ERR_PTR(-ENOMEM);
1528 pipe = usb_sndbulkpipe(data->udev, data->bulk_tx_ep->bEndpointAddress);
1530 usb_fill_bulk_urb(urb, data->udev, pipe,
1531 skb->data, skb->len, btusb_tx_complete, skb);
1533 skb->dev = (void *)hdev;
1538 static struct urb *alloc_isoc_urb(struct hci_dev *hdev, struct sk_buff *skb)
1540 struct btusb_data *data = hci_get_drvdata(hdev);
1544 if (!data->isoc_tx_ep)
1545 return ERR_PTR(-ENODEV);
1547 urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, GFP_KERNEL);
1549 return ERR_PTR(-ENOMEM);
1551 pipe = usb_sndisocpipe(data->udev, data->isoc_tx_ep->bEndpointAddress);
1553 usb_fill_int_urb(urb, data->udev, pipe,
1554 skb->data, skb->len, btusb_isoc_tx_complete,
1555 skb, data->isoc_tx_ep->bInterval);
1557 urb->transfer_flags = URB_ISO_ASAP;
1559 if (data->isoc_altsetting == 6)
1560 __fill_isoc_descriptor_msbc(urb, skb->len,
1561 le16_to_cpu(data->isoc_tx_ep->wMaxPacketSize),
1564 __fill_isoc_descriptor(urb, skb->len,
1565 le16_to_cpu(data->isoc_tx_ep->wMaxPacketSize));
1566 skb->dev = (void *)hdev;
1571 static int submit_tx_urb(struct hci_dev *hdev, struct urb *urb)
1573 struct btusb_data *data = hci_get_drvdata(hdev);
1576 usb_anchor_urb(urb, &data->tx_anchor);
1578 err = usb_submit_urb(urb, GFP_KERNEL);
1580 if (err != -EPERM && err != -ENODEV)
1581 bt_dev_err(hdev, "urb %p submission failed (%d)",
1583 kfree(urb->setup_packet);
1584 usb_unanchor_urb(urb);
1586 usb_mark_last_busy(data->udev);
1593 static int submit_or_queue_tx_urb(struct hci_dev *hdev, struct urb *urb)
1595 struct btusb_data *data = hci_get_drvdata(hdev);
1596 unsigned long flags;
1599 spin_lock_irqsave(&data->txlock, flags);
1600 suspending = test_bit(BTUSB_SUSPENDING, &data->flags);
1602 data->tx_in_flight++;
1603 spin_unlock_irqrestore(&data->txlock, flags);
1606 return submit_tx_urb(hdev, urb);
1608 usb_anchor_urb(urb, &data->deferred);
1609 schedule_work(&data->waker);
1615 static int btusb_send_frame(struct hci_dev *hdev, struct sk_buff *skb)
1619 BT_DBG("%s", hdev->name);
1621 switch (hci_skb_pkt_type(skb)) {
1622 case HCI_COMMAND_PKT:
1623 urb = alloc_ctrl_urb(hdev, skb);
1625 return PTR_ERR(urb);
1627 hdev->stat.cmd_tx++;
1628 return submit_or_queue_tx_urb(hdev, urb);
1630 case HCI_ACLDATA_PKT:
1631 urb = alloc_bulk_urb(hdev, skb);
1633 return PTR_ERR(urb);
1635 hdev->stat.acl_tx++;
1636 return submit_or_queue_tx_urb(hdev, urb);
1638 case HCI_SCODATA_PKT:
1639 if (hci_conn_num(hdev, SCO_LINK) < 1)
1642 urb = alloc_isoc_urb(hdev, skb);
1644 return PTR_ERR(urb);
1646 hdev->stat.sco_tx++;
1647 return submit_tx_urb(hdev, urb);
1653 static void btusb_notify(struct hci_dev *hdev, unsigned int evt)
1655 struct btusb_data *data = hci_get_drvdata(hdev);
1657 BT_DBG("%s evt %d", hdev->name, evt);
1659 if (hci_conn_num(hdev, SCO_LINK) != data->sco_num) {
1660 data->sco_num = hci_conn_num(hdev, SCO_LINK);
1661 data->air_mode = evt;
1662 schedule_work(&data->work);
1666 static inline int __set_isoc_interface(struct hci_dev *hdev, int altsetting)
1668 struct btusb_data *data = hci_get_drvdata(hdev);
1669 struct usb_interface *intf = data->isoc;
1670 struct usb_endpoint_descriptor *ep_desc;
1676 err = usb_set_interface(data->udev, data->isoc_ifnum, altsetting);
1678 bt_dev_err(hdev, "setting interface failed (%d)", -err);
1682 data->isoc_altsetting = altsetting;
1684 data->isoc_tx_ep = NULL;
1685 data->isoc_rx_ep = NULL;
1687 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
1688 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
1690 if (!data->isoc_tx_ep && usb_endpoint_is_isoc_out(ep_desc)) {
1691 data->isoc_tx_ep = ep_desc;
1695 if (!data->isoc_rx_ep && usb_endpoint_is_isoc_in(ep_desc)) {
1696 data->isoc_rx_ep = ep_desc;
1701 if (!data->isoc_tx_ep || !data->isoc_rx_ep) {
1702 bt_dev_err(hdev, "invalid SCO descriptors");
1709 static int btusb_switch_alt_setting(struct hci_dev *hdev, int new_alts)
1711 struct btusb_data *data = hci_get_drvdata(hdev);
1714 if (data->isoc_altsetting != new_alts) {
1715 unsigned long flags;
1717 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1718 usb_kill_anchored_urbs(&data->isoc_anchor);
1720 /* When isochronous alternate setting needs to be
1721 * changed, because SCO connection has been added
1722 * or removed, a packet fragment may be left in the
1723 * reassembling state. This could lead to wrongly
1724 * assembled fragments.
1726 * Clear outstanding fragment when selecting a new
1727 * alternate setting.
1729 spin_lock_irqsave(&data->rxlock, flags);
1730 kfree_skb(data->sco_skb);
1731 data->sco_skb = NULL;
1732 spin_unlock_irqrestore(&data->rxlock, flags);
1734 err = __set_isoc_interface(hdev, new_alts);
1739 if (!test_and_set_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
1740 if (btusb_submit_isoc_urb(hdev, GFP_KERNEL) < 0)
1741 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1743 btusb_submit_isoc_urb(hdev, GFP_KERNEL);
1749 static struct usb_host_interface *btusb_find_altsetting(struct btusb_data *data,
1752 struct usb_interface *intf = data->isoc;
1755 BT_DBG("Looking for Alt no :%d", alt);
1760 for (i = 0; i < intf->num_altsetting; i++) {
1761 if (intf->altsetting[i].desc.bAlternateSetting == alt)
1762 return &intf->altsetting[i];
1768 static void btusb_work(struct work_struct *work)
1770 struct btusb_data *data = container_of(work, struct btusb_data, work);
1771 struct hci_dev *hdev = data->hdev;
1775 if (data->sco_num > 0) {
1776 if (!test_bit(BTUSB_DID_ISO_RESUME, &data->flags)) {
1777 err = usb_autopm_get_interface(data->isoc ? data->isoc : data->intf);
1779 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1780 usb_kill_anchored_urbs(&data->isoc_anchor);
1784 set_bit(BTUSB_DID_ISO_RESUME, &data->flags);
1787 if (data->air_mode == HCI_NOTIFY_ENABLE_SCO_CVSD) {
1788 if (hdev->voice_setting & 0x0020) {
1789 static const int alts[3] = { 2, 4, 5 };
1791 new_alts = alts[data->sco_num - 1];
1793 new_alts = data->sco_num;
1795 } else if (data->air_mode == HCI_NOTIFY_ENABLE_SCO_TRANSP) {
1796 /* Bluetooth USB spec recommends alt 6 (63 bytes), but
1797 * many adapters do not support it. Alt 1 appears to
1798 * work for all adapters that do not have alt 6, and
1799 * which work with WBS at all. Some devices prefer
1800 * alt 3 (HCI payload >= 60 Bytes let air packet
1801 * data satisfy 60 bytes), requiring
1802 * MTU >= 3 (packets) * 25 (size) - 3 (headers) = 72
1803 * see also Core spec 5, vol 4, B 2.1.1 & Table 2.1.
1805 if (btusb_find_altsetting(data, 6))
1807 else if (btusb_find_altsetting(data, 3) &&
1808 hdev->sco_mtu >= 72 &&
1809 test_bit(BTUSB_USE_ALT3_FOR_WBS, &data->flags))
1815 if (btusb_switch_alt_setting(hdev, new_alts) < 0)
1816 bt_dev_err(hdev, "set USB alt:(%d) failed!", new_alts);
1818 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1819 usb_kill_anchored_urbs(&data->isoc_anchor);
1821 __set_isoc_interface(hdev, 0);
1822 if (test_and_clear_bit(BTUSB_DID_ISO_RESUME, &data->flags))
1823 usb_autopm_put_interface(data->isoc ? data->isoc : data->intf);
1827 static void btusb_waker(struct work_struct *work)
1829 struct btusb_data *data = container_of(work, struct btusb_data, waker);
1832 err = usb_autopm_get_interface(data->intf);
1836 usb_autopm_put_interface(data->intf);
1839 static int btusb_setup_bcm92035(struct hci_dev *hdev)
1841 struct sk_buff *skb;
1844 BT_DBG("%s", hdev->name);
1846 skb = __hci_cmd_sync(hdev, 0xfc3b, 1, &val, HCI_INIT_TIMEOUT);
1848 bt_dev_err(hdev, "BCM92035 command failed (%ld)", PTR_ERR(skb));
1855 static int btusb_setup_csr(struct hci_dev *hdev)
1857 struct btusb_data *data = hci_get_drvdata(hdev);
1858 u16 bcdDevice = le16_to_cpu(data->udev->descriptor.bcdDevice);
1859 struct hci_rp_read_local_version *rp;
1860 struct sk_buff *skb;
1861 bool is_fake = false;
1864 BT_DBG("%s", hdev->name);
1866 skb = __hci_cmd_sync(hdev, HCI_OP_READ_LOCAL_VERSION, 0, NULL,
1869 int err = PTR_ERR(skb);
1870 bt_dev_err(hdev, "CSR: Local version failed (%d)", err);
1874 if (skb->len != sizeof(struct hci_rp_read_local_version)) {
1875 bt_dev_err(hdev, "CSR: Local version length mismatch");
1880 rp = (struct hci_rp_read_local_version *)skb->data;
1882 /* Detect a wide host of Chinese controllers that aren't CSR.
1884 * Known fake bcdDevices: 0x0100, 0x0134, 0x1915, 0x2520, 0x7558, 0x8891
1886 * The main thing they have in common is that these are really popular low-cost
1887 * options that support newer Bluetooth versions but rely on heavy VID/PID
1888 * squatting of this poor old Bluetooth 1.1 device. Even sold as such.
1890 * We detect actual CSR devices by checking that the HCI manufacturer code
1891 * is Cambridge Silicon Radio (10) and ensuring that LMP sub-version and
1892 * HCI rev values always match. As they both store the firmware number.
1894 if (le16_to_cpu(rp->manufacturer) != 10 ||
1895 le16_to_cpu(rp->hci_rev) != le16_to_cpu(rp->lmp_subver))
1898 /* Known legit CSR firmware build numbers and their supported BT versions:
1899 * - 1.1 (0x1) -> 0x0073, 0x020d, 0x033c, 0x034e
1900 * - 1.2 (0x2) -> 0x04d9, 0x0529
1901 * - 2.0 (0x3) -> 0x07a6, 0x07ad, 0x0c5c
1902 * - 2.1 (0x4) -> 0x149c, 0x1735, 0x1899 (0x1899 is a BlueCore4-External)
1903 * - 4.0 (0x6) -> 0x1d86, 0x2031, 0x22bb
1905 * e.g. Real CSR dongles with LMP subversion 0x73 are old enough that
1906 * support BT 1.1 only; so it's a dead giveaway when some
1907 * third-party BT 4.0 dongle reuses it.
1909 else if (le16_to_cpu(rp->lmp_subver) <= 0x034e &&
1910 le16_to_cpu(rp->hci_ver) > BLUETOOTH_VER_1_1)
1913 else if (le16_to_cpu(rp->lmp_subver) <= 0x0529 &&
1914 le16_to_cpu(rp->hci_ver) > BLUETOOTH_VER_1_2)
1917 else if (le16_to_cpu(rp->lmp_subver) <= 0x0c5c &&
1918 le16_to_cpu(rp->hci_ver) > BLUETOOTH_VER_2_0)
1921 else if (le16_to_cpu(rp->lmp_subver) <= 0x1899 &&
1922 le16_to_cpu(rp->hci_ver) > BLUETOOTH_VER_2_1)
1925 else if (le16_to_cpu(rp->lmp_subver) <= 0x22bb &&
1926 le16_to_cpu(rp->hci_ver) > BLUETOOTH_VER_4_0)
1929 /* Other clones which beat all the above checks */
1930 else if (bcdDevice == 0x0134 &&
1931 le16_to_cpu(rp->lmp_subver) == 0x0c5c &&
1932 le16_to_cpu(rp->hci_ver) == BLUETOOTH_VER_2_0)
1936 bt_dev_warn(hdev, "CSR: Unbranded CSR clone detected; adding workarounds and force-suspending once...");
1938 /* Generally these clones have big discrepancies between
1939 * advertised features and what's actually supported.
1940 * Probably will need to be expanded in the future;
1941 * without these the controller will lock up.
1943 set_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks);
1944 set_bit(HCI_QUIRK_BROKEN_ERR_DATA_REPORTING, &hdev->quirks);
1946 /* Clear the reset quirk since this is not an actual
1947 * early Bluetooth 1.1 device from CSR.
1949 clear_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
1950 clear_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
1953 * Special workaround for these BT 4.0 chip clones, and potentially more:
1955 * - 0x0134: a Barrot 8041a02 (HCI rev: 0x1012 sub: 0x0810)
1956 * - 0x7558: IC markings FR3191AHAL 749H15143 (HCI rev/sub-version: 0x0709)
1958 * These controllers are really messed-up.
1960 * 1. Their bulk RX endpoint will never report any data unless
1961 * the device was suspended at least once (yes, really).
1962 * 2. They will not wakeup when autosuspended and receiving data
1963 * on their bulk RX endpoint from e.g. a keyboard or mouse
1964 * (IOW remote-wakeup support is broken for the bulk endpoint).
1966 * To fix 1. enable runtime-suspend, force-suspend the
1967 * HCI and then wake-it up by disabling runtime-suspend.
1969 * To fix 2. clear the HCI's can_wake flag, this way the HCI
1970 * will still be autosuspended when it is not open.
1974 * Because these are widespread problems we prefer generic solutions; so
1975 * apply this initialization quirk to every controller that gets here,
1976 * it should be harmless. The alternative is to not work at all.
1978 pm_runtime_allow(&data->udev->dev);
1980 ret = pm_runtime_suspend(&data->udev->dev);
1984 bt_dev_err(hdev, "CSR: Failed to suspend the device for our Barrot 8041a02 receive-issue workaround");
1986 pm_runtime_forbid(&data->udev->dev);
1988 device_set_wakeup_capable(&data->udev->dev, false);
1990 /* Re-enable autosuspend if this was requested */
1991 if (enable_autosuspend)
1992 usb_enable_autosuspend(data->udev);
2000 static int inject_cmd_complete(struct hci_dev *hdev, __u16 opcode)
2002 struct sk_buff *skb;
2003 struct hci_event_hdr *hdr;
2004 struct hci_ev_cmd_complete *evt;
2006 skb = bt_skb_alloc(sizeof(*hdr) + sizeof(*evt) + 1, GFP_KERNEL);
2010 hdr = skb_put(skb, sizeof(*hdr));
2011 hdr->evt = HCI_EV_CMD_COMPLETE;
2012 hdr->plen = sizeof(*evt) + 1;
2014 evt = skb_put(skb, sizeof(*evt));
2016 evt->opcode = cpu_to_le16(opcode);
2018 skb_put_u8(skb, 0x00);
2020 hci_skb_pkt_type(skb) = HCI_EVENT_PKT;
2022 return hci_recv_frame(hdev, skb);
2025 static int btusb_recv_bulk_intel(struct btusb_data *data, void *buffer,
2028 struct hci_dev *hdev = data->hdev;
2030 /* When the device is in bootloader mode, then it can send
2031 * events via the bulk endpoint. These events are treated the
2032 * same way as the ones received from the interrupt endpoint.
2034 if (btintel_test_flag(hdev, INTEL_BOOTLOADER))
2035 return btusb_recv_intr(data, buffer, count);
2037 return btusb_recv_bulk(data, buffer, count);
2040 static int btusb_recv_event_intel(struct hci_dev *hdev, struct sk_buff *skb)
2042 if (btintel_test_flag(hdev, INTEL_BOOTLOADER)) {
2043 struct hci_event_hdr *hdr = (void *)skb->data;
2045 if (skb->len > HCI_EVENT_HDR_SIZE && hdr->evt == 0xff &&
2047 const void *ptr = skb->data + HCI_EVENT_HDR_SIZE + 1;
2048 unsigned int len = skb->len - HCI_EVENT_HDR_SIZE - 1;
2050 switch (skb->data[2]) {
2052 /* When switching to the operational firmware
2053 * the device sends a vendor specific event
2054 * indicating that the bootup completed.
2056 btintel_bootup(hdev, ptr, len);
2059 /* When the firmware loading completes the
2060 * device sends out a vendor specific event
2061 * indicating the result of the firmware
2064 btintel_secure_send_result(hdev, ptr, len);
2070 return hci_recv_frame(hdev, skb);
2073 static int btusb_send_frame_intel(struct hci_dev *hdev, struct sk_buff *skb)
2077 BT_DBG("%s", hdev->name);
2079 switch (hci_skb_pkt_type(skb)) {
2080 case HCI_COMMAND_PKT:
2081 if (btintel_test_flag(hdev, INTEL_BOOTLOADER)) {
2082 struct hci_command_hdr *cmd = (void *)skb->data;
2083 __u16 opcode = le16_to_cpu(cmd->opcode);
2085 /* When in bootloader mode and the command 0xfc09
2086 * is received, it needs to be send down the
2087 * bulk endpoint. So allocate a bulk URB instead.
2089 if (opcode == 0xfc09)
2090 urb = alloc_bulk_urb(hdev, skb);
2092 urb = alloc_ctrl_urb(hdev, skb);
2094 /* When the 0xfc01 command is issued to boot into
2095 * the operational firmware, it will actually not
2096 * send a command complete event. To keep the flow
2097 * control working inject that event here.
2099 if (opcode == 0xfc01)
2100 inject_cmd_complete(hdev, opcode);
2102 urb = alloc_ctrl_urb(hdev, skb);
2105 return PTR_ERR(urb);
2107 hdev->stat.cmd_tx++;
2108 return submit_or_queue_tx_urb(hdev, urb);
2110 case HCI_ACLDATA_PKT:
2111 urb = alloc_bulk_urb(hdev, skb);
2113 return PTR_ERR(urb);
2115 hdev->stat.acl_tx++;
2116 return submit_or_queue_tx_urb(hdev, urb);
2118 case HCI_SCODATA_PKT:
2119 if (hci_conn_num(hdev, SCO_LINK) < 1)
2122 urb = alloc_isoc_urb(hdev, skb);
2124 return PTR_ERR(urb);
2126 hdev->stat.sco_tx++;
2127 return submit_tx_urb(hdev, urb);
2133 /* UHW CR mapping */
2134 #define MTK_BT_MISC 0x70002510
2135 #define MTK_BT_SUBSYS_RST 0x70002610
2136 #define MTK_UDMA_INT_STA_BT 0x74000024
2137 #define MTK_UDMA_INT_STA_BT1 0x74000308
2138 #define MTK_BT_WDT_STATUS 0x740003A0
2139 #define MTK_EP_RST_OPT 0x74011890
2140 #define MTK_EP_RST_IN_OUT_OPT 0x00010001
2141 #define MTK_BT_RST_DONE 0x00000100
2142 #define MTK_BT_RESET_WAIT_MS 100
2143 #define MTK_BT_RESET_NUM_TRIES 10
2144 #define FIRMWARE_MT7663 "mediatek/mt7663pr2h.bin"
2145 #define FIRMWARE_MT7668 "mediatek/mt7668pr2h.bin"
2147 #define HCI_WMT_MAX_EVENT_SIZE 64
2148 /* It is for mt79xx download rom patch*/
2149 #define MTK_FW_ROM_PATCH_HEADER_SIZE 32
2150 #define MTK_FW_ROM_PATCH_GD_SIZE 64
2151 #define MTK_FW_ROM_PATCH_SEC_MAP_SIZE 64
2152 #define MTK_SEC_MAP_COMMON_SIZE 12
2153 #define MTK_SEC_MAP_NEED_SEND_SIZE 52
2156 BTMTK_WMT_PATCH_DWNLD = 0x1,
2157 BTMTK_WMT_FUNC_CTRL = 0x6,
2158 BTMTK_WMT_RST = 0x7,
2159 BTMTK_WMT_SEMAPHORE = 0x17,
2164 BTMTK_WMT_PATCH_UNDONE,
2165 BTMTK_WMT_PATCH_PROGRESS,
2166 BTMTK_WMT_PATCH_DONE,
2167 BTMTK_WMT_ON_UNDONE,
2169 BTMTK_WMT_ON_PROGRESS,
2172 struct btmtk_wmt_hdr {
2179 struct btmtk_hci_wmt_cmd {
2180 struct btmtk_wmt_hdr hdr;
2184 struct btmtk_hci_wmt_evt {
2185 struct hci_event_hdr hhdr;
2186 struct btmtk_wmt_hdr whdr;
2189 struct btmtk_hci_wmt_evt_funcc {
2190 struct btmtk_hci_wmt_evt hwhdr;
2194 struct btmtk_tci_sleep {
2197 __le16 host_duration;
2199 u8 time_compensation;
2202 struct btmtk_hci_wmt_params {
2210 struct btmtk_patch_header {
2218 struct btmtk_global_desc {
2225 struct btmtk_section_map {
2230 __le32 u4SecSpec[13];
2237 __le32 dlmodecrctype;
2244 static void btusb_mtk_wmt_recv(struct urb *urb)
2246 struct hci_dev *hdev = urb->context;
2247 struct btusb_data *data = hci_get_drvdata(hdev);
2248 struct hci_event_hdr *hdr;
2249 struct sk_buff *skb;
2252 if (urb->status == 0 && urb->actual_length > 0) {
2253 hdev->stat.byte_rx += urb->actual_length;
2255 /* WMT event shouldn't be fragmented and the size should be
2256 * less than HCI_WMT_MAX_EVENT_SIZE.
2258 skb = bt_skb_alloc(HCI_WMT_MAX_EVENT_SIZE, GFP_ATOMIC);
2260 hdev->stat.err_rx++;
2261 kfree(urb->setup_packet);
2265 hci_skb_pkt_type(skb) = HCI_EVENT_PKT;
2266 skb_put_data(skb, urb->transfer_buffer, urb->actual_length);
2268 hdr = (void *)skb->data;
2269 /* Fix up the vendor event id with 0xff for vendor specific
2270 * instead of 0xe4 so that event send via monitoring socket can
2271 * be parsed properly.
2275 /* When someone waits for the WMT event, the skb is being cloned
2276 * and being processed the events from there then.
2278 if (test_bit(BTUSB_TX_WAIT_VND_EVT, &data->flags)) {
2279 data->evt_skb = skb_clone(skb, GFP_ATOMIC);
2280 if (!data->evt_skb) {
2282 kfree(urb->setup_packet);
2287 err = hci_recv_frame(hdev, skb);
2289 kfree_skb(data->evt_skb);
2290 data->evt_skb = NULL;
2291 kfree(urb->setup_packet);
2295 if (test_and_clear_bit(BTUSB_TX_WAIT_VND_EVT,
2297 /* Barrier to sync with other CPUs */
2298 smp_mb__after_atomic();
2299 wake_up_bit(&data->flags,
2300 BTUSB_TX_WAIT_VND_EVT);
2302 kfree(urb->setup_packet);
2304 } else if (urb->status == -ENOENT) {
2305 /* Avoid suspend failed when usb_kill_urb */
2309 usb_mark_last_busy(data->udev);
2311 /* The URB complete handler is still called with urb->actual_length = 0
2312 * when the event is not available, so we should keep re-submitting
2313 * URB until WMT event returns, Also, It's necessary to wait some time
2314 * between the two consecutive control URBs to relax the target device
2315 * to generate the event. Otherwise, the WMT event cannot return from
2316 * the device successfully.
2320 usb_anchor_urb(urb, &data->ctrl_anchor);
2321 err = usb_submit_urb(urb, GFP_ATOMIC);
2323 kfree(urb->setup_packet);
2324 /* -EPERM: urb is being killed;
2325 * -ENODEV: device got disconnected
2327 if (err != -EPERM && err != -ENODEV)
2328 bt_dev_err(hdev, "urb %p failed to resubmit (%d)",
2330 usb_unanchor_urb(urb);
2334 static int btusb_mtk_submit_wmt_recv_urb(struct hci_dev *hdev)
2336 struct btusb_data *data = hci_get_drvdata(hdev);
2337 struct usb_ctrlrequest *dr;
2343 urb = usb_alloc_urb(0, GFP_KERNEL);
2347 dr = kmalloc(sizeof(*dr), GFP_KERNEL);
2353 dr->bRequestType = USB_TYPE_VENDOR | USB_DIR_IN;
2355 dr->wIndex = cpu_to_le16(0);
2356 dr->wValue = cpu_to_le16(48);
2357 dr->wLength = cpu_to_le16(size);
2359 buf = kmalloc(size, GFP_KERNEL);
2366 pipe = usb_rcvctrlpipe(data->udev, 0);
2368 usb_fill_control_urb(urb, data->udev, pipe, (void *)dr,
2369 buf, size, btusb_mtk_wmt_recv, hdev);
2371 urb->transfer_flags |= URB_FREE_BUFFER;
2373 usb_anchor_urb(urb, &data->ctrl_anchor);
2374 err = usb_submit_urb(urb, GFP_KERNEL);
2376 if (err != -EPERM && err != -ENODEV)
2377 bt_dev_err(hdev, "urb %p submission failed (%d)",
2379 usb_unanchor_urb(urb);
2387 static int btusb_mtk_hci_wmt_sync(struct hci_dev *hdev,
2388 struct btmtk_hci_wmt_params *wmt_params)
2390 struct btusb_data *data = hci_get_drvdata(hdev);
2391 struct btmtk_hci_wmt_evt_funcc *wmt_evt_funcc;
2392 u32 hlen, status = BTMTK_WMT_INVALID;
2393 struct btmtk_hci_wmt_evt *wmt_evt;
2394 struct btmtk_hci_wmt_cmd *wc;
2395 struct btmtk_wmt_hdr *hdr;
2398 /* Send the WMT command and wait until the WMT event returns */
2399 hlen = sizeof(*hdr) + wmt_params->dlen;
2403 wc = kzalloc(hlen, GFP_KERNEL);
2409 hdr->op = wmt_params->op;
2410 hdr->dlen = cpu_to_le16(wmt_params->dlen + 1);
2411 hdr->flag = wmt_params->flag;
2412 memcpy(wc->data, wmt_params->data, wmt_params->dlen);
2414 set_bit(BTUSB_TX_WAIT_VND_EVT, &data->flags);
2416 err = __hci_cmd_send(hdev, 0xfc6f, hlen, wc);
2419 clear_bit(BTUSB_TX_WAIT_VND_EVT, &data->flags);
2423 /* Submit control IN URB on demand to process the WMT event */
2424 err = btusb_mtk_submit_wmt_recv_urb(hdev);
2428 /* The vendor specific WMT commands are all answered by a vendor
2429 * specific event and will have the Command Status or Command
2430 * Complete as with usual HCI command flow control.
2432 * After sending the command, wait for BTUSB_TX_WAIT_VND_EVT
2433 * state to be cleared. The driver specific event receive routine
2434 * will clear that state and with that indicate completion of the
2437 err = wait_on_bit_timeout(&data->flags, BTUSB_TX_WAIT_VND_EVT,
2438 TASK_INTERRUPTIBLE, HCI_INIT_TIMEOUT);
2439 if (err == -EINTR) {
2440 bt_dev_err(hdev, "Execution of wmt command interrupted");
2441 clear_bit(BTUSB_TX_WAIT_VND_EVT, &data->flags);
2446 bt_dev_err(hdev, "Execution of wmt command timed out");
2447 clear_bit(BTUSB_TX_WAIT_VND_EVT, &data->flags);
2452 /* Parse and handle the return WMT event */
2453 wmt_evt = (struct btmtk_hci_wmt_evt *)data->evt_skb->data;
2454 if (wmt_evt->whdr.op != hdr->op) {
2455 bt_dev_err(hdev, "Wrong op received %d expected %d",
2456 wmt_evt->whdr.op, hdr->op);
2461 switch (wmt_evt->whdr.op) {
2462 case BTMTK_WMT_SEMAPHORE:
2463 if (wmt_evt->whdr.flag == 2)
2464 status = BTMTK_WMT_PATCH_UNDONE;
2466 status = BTMTK_WMT_PATCH_DONE;
2468 case BTMTK_WMT_FUNC_CTRL:
2469 wmt_evt_funcc = (struct btmtk_hci_wmt_evt_funcc *)wmt_evt;
2470 if (be16_to_cpu(wmt_evt_funcc->status) == 0x404)
2471 status = BTMTK_WMT_ON_DONE;
2472 else if (be16_to_cpu(wmt_evt_funcc->status) == 0x420)
2473 status = BTMTK_WMT_ON_PROGRESS;
2475 status = BTMTK_WMT_ON_UNDONE;
2477 case BTMTK_WMT_PATCH_DWNLD:
2478 if (wmt_evt->whdr.flag == 2)
2479 status = BTMTK_WMT_PATCH_DONE;
2480 else if (wmt_evt->whdr.flag == 1)
2481 status = BTMTK_WMT_PATCH_PROGRESS;
2483 status = BTMTK_WMT_PATCH_UNDONE;
2487 if (wmt_params->status)
2488 *wmt_params->status = status;
2491 kfree_skb(data->evt_skb);
2492 data->evt_skb = NULL;
2498 static int btusb_mtk_setup_firmware_79xx(struct hci_dev *hdev, const char *fwname)
2500 struct btmtk_hci_wmt_params wmt_params;
2501 struct btmtk_global_desc *globaldesc = NULL;
2502 struct btmtk_section_map *sectionmap;
2503 const struct firmware *fw;
2505 const u8 *fw_bin_ptr;
2506 int err, dlen, i, status;
2507 u8 flag, first_block, retry;
2508 u32 section_num, dl_size, section_offset;
2511 err = request_firmware(&fw, fwname, &hdev->dev);
2513 bt_dev_err(hdev, "Failed to load firmware file (%d)", err);
2518 fw_bin_ptr = fw_ptr;
2519 globaldesc = (struct btmtk_global_desc *)(fw_ptr + MTK_FW_ROM_PATCH_HEADER_SIZE);
2520 section_num = le32_to_cpu(globaldesc->section_num);
2522 for (i = 0; i < section_num; i++) {
2524 fw_ptr = fw_bin_ptr;
2525 sectionmap = (struct btmtk_section_map *)(fw_ptr + MTK_FW_ROM_PATCH_HEADER_SIZE +
2526 MTK_FW_ROM_PATCH_GD_SIZE + MTK_FW_ROM_PATCH_SEC_MAP_SIZE * i);
2528 section_offset = le32_to_cpu(sectionmap->secoffset);
2529 dl_size = le32_to_cpu(sectionmap->bin_info_spec.dlsize);
2534 cmd[0] = 0; /* 0 means legacy dl mode. */
2536 fw_ptr + MTK_FW_ROM_PATCH_HEADER_SIZE +
2537 MTK_FW_ROM_PATCH_GD_SIZE + MTK_FW_ROM_PATCH_SEC_MAP_SIZE * i +
2538 MTK_SEC_MAP_COMMON_SIZE,
2539 MTK_SEC_MAP_NEED_SEND_SIZE + 1);
2541 wmt_params.op = BTMTK_WMT_PATCH_DWNLD;
2542 wmt_params.status = &status;
2543 wmt_params.flag = 0;
2544 wmt_params.dlen = MTK_SEC_MAP_NEED_SEND_SIZE + 1;
2545 wmt_params.data = &cmd;
2547 err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
2549 bt_dev_err(hdev, "Failed to send wmt patch dwnld (%d)",
2551 goto err_release_fw;
2554 if (status == BTMTK_WMT_PATCH_UNDONE) {
2556 } else if (status == BTMTK_WMT_PATCH_PROGRESS) {
2559 } else if (status == BTMTK_WMT_PATCH_DONE) {
2562 bt_dev_err(hdev, "Failed wmt patch dwnld status (%d)",
2564 goto err_release_fw;
2568 fw_ptr += section_offset;
2569 wmt_params.op = BTMTK_WMT_PATCH_DWNLD;
2570 wmt_params.status = NULL;
2572 while (dl_size > 0) {
2573 dlen = min_t(int, 250, dl_size);
2574 if (first_block == 1) {
2577 } else if (dl_size - dlen <= 0) {
2583 wmt_params.flag = flag;
2584 wmt_params.dlen = dlen;
2585 wmt_params.data = fw_ptr;
2587 err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
2589 bt_dev_err(hdev, "Failed to send wmt patch dwnld (%d)",
2591 goto err_release_fw;
2601 /* Wait a few moments for firmware activation done */
2602 usleep_range(100000, 120000);
2605 release_firmware(fw);
2610 static int btusb_mtk_setup_firmware(struct hci_dev *hdev, const char *fwname)
2612 struct btmtk_hci_wmt_params wmt_params;
2613 const struct firmware *fw;
2619 err = request_firmware(&fw, fwname, &hdev->dev);
2621 bt_dev_err(hdev, "Failed to load firmware file (%d)", err);
2625 /* Power on data RAM the firmware relies on. */
2627 wmt_params.op = BTMTK_WMT_FUNC_CTRL;
2628 wmt_params.flag = 3;
2629 wmt_params.dlen = sizeof(param);
2630 wmt_params.data = ¶m;
2631 wmt_params.status = NULL;
2633 err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
2635 bt_dev_err(hdev, "Failed to power on data RAM (%d)", err);
2636 goto err_release_fw;
2642 /* The size of patch header is 30 bytes, should be skip */
2645 goto err_release_fw;
2652 wmt_params.op = BTMTK_WMT_PATCH_DWNLD;
2653 wmt_params.status = NULL;
2655 while (fw_size > 0) {
2656 dlen = min_t(int, 250, fw_size);
2658 /* Tell device the position in sequence */
2659 if (fw_size - dlen <= 0)
2661 else if (fw_size < fw->size - 30)
2664 wmt_params.flag = flag;
2665 wmt_params.dlen = dlen;
2666 wmt_params.data = fw_ptr;
2668 err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
2670 bt_dev_err(hdev, "Failed to send wmt patch dwnld (%d)",
2672 goto err_release_fw;
2679 wmt_params.op = BTMTK_WMT_RST;
2680 wmt_params.flag = 4;
2681 wmt_params.dlen = 0;
2682 wmt_params.data = NULL;
2683 wmt_params.status = NULL;
2685 /* Activate funciton the firmware providing to */
2686 err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
2688 bt_dev_err(hdev, "Failed to send wmt rst (%d)", err);
2689 goto err_release_fw;
2692 /* Wait a few moments for firmware activation done */
2693 usleep_range(10000, 12000);
2696 release_firmware(fw);
2701 static int btusb_mtk_func_query(struct hci_dev *hdev)
2703 struct btmtk_hci_wmt_params wmt_params;
2707 /* Query whether the function is enabled */
2708 wmt_params.op = BTMTK_WMT_FUNC_CTRL;
2709 wmt_params.flag = 4;
2710 wmt_params.dlen = sizeof(param);
2711 wmt_params.data = ¶m;
2712 wmt_params.status = &status;
2714 err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
2716 bt_dev_err(hdev, "Failed to query function status (%d)", err);
2723 static int btusb_mtk_uhw_reg_write(struct btusb_data *data, u32 reg, u32 val)
2725 struct hci_dev *hdev = data->hdev;
2729 buf = kzalloc(4, GFP_KERNEL);
2733 put_unaligned_le32(val, buf);
2735 pipe = usb_sndctrlpipe(data->udev, 0);
2736 err = usb_control_msg(data->udev, pipe, 0x02,
2738 reg >> 16, reg & 0xffff,
2739 buf, 4, USB_CTRL_SET_TIMEOUT);
2741 bt_dev_err(hdev, "Failed to write uhw reg(%d)", err);
2751 static int btusb_mtk_uhw_reg_read(struct btusb_data *data, u32 reg, u32 *val)
2753 struct hci_dev *hdev = data->hdev;
2757 buf = kzalloc(4, GFP_KERNEL);
2761 pipe = usb_rcvctrlpipe(data->udev, 0);
2762 err = usb_control_msg(data->udev, pipe, 0x01,
2764 reg >> 16, reg & 0xffff,
2765 buf, 4, USB_CTRL_SET_TIMEOUT);
2767 bt_dev_err(hdev, "Failed to read uhw reg(%d)", err);
2771 *val = get_unaligned_le32(buf);
2772 bt_dev_dbg(hdev, "reg=%x, value=0x%08x", reg, *val);
2780 static int btusb_mtk_reg_read(struct btusb_data *data, u32 reg, u32 *val)
2782 int pipe, err, size = sizeof(u32);
2785 buf = kzalloc(size, GFP_KERNEL);
2789 pipe = usb_rcvctrlpipe(data->udev, 0);
2790 err = usb_control_msg(data->udev, pipe, 0x63,
2791 USB_TYPE_VENDOR | USB_DIR_IN,
2792 reg >> 16, reg & 0xffff,
2793 buf, size, USB_CTRL_SET_TIMEOUT);
2797 *val = get_unaligned_le32(buf);
2805 static int btusb_mtk_id_get(struct btusb_data *data, u32 reg, u32 *id)
2807 return btusb_mtk_reg_read(data, reg, id);
2810 static int btusb_mtk_setup(struct hci_dev *hdev)
2812 struct btusb_data *data = hci_get_drvdata(hdev);
2813 struct btmtk_hci_wmt_params wmt_params;
2814 ktime_t calltime, delta, rettime;
2815 struct btmtk_tci_sleep tci_sleep;
2816 unsigned long long duration;
2817 struct sk_buff *skb;
2821 char fw_bin_name[64];
2825 calltime = ktime_get();
2827 err = btusb_mtk_id_get(data, 0x80000008, &dev_id);
2829 bt_dev_err(hdev, "Failed to get device id (%d)", err);
2834 err = btusb_mtk_id_get(data, 0x70010200, &dev_id);
2836 bt_dev_err(hdev, "Failed to get device id (%d)", err);
2839 err = btusb_mtk_id_get(data, 0x80021004, &fw_version);
2841 bt_dev_err(hdev, "Failed to get fw version (%d)", err);
2848 fwname = FIRMWARE_MT7663;
2851 fwname = FIRMWARE_MT7668;
2855 snprintf(fw_bin_name, sizeof(fw_bin_name),
2856 "mediatek/BT_RAM_CODE_MT%04x_1_%x_hdr.bin",
2857 dev_id & 0xffff, (fw_version & 0xff) + 1);
2858 err = btusb_mtk_setup_firmware_79xx(hdev, fw_bin_name);
2860 /* It's Device EndPoint Reset Option Register */
2861 btusb_mtk_uhw_reg_write(data, MTK_EP_RST_OPT, MTK_EP_RST_IN_OUT_OPT);
2863 /* Enable Bluetooth protocol */
2865 wmt_params.op = BTMTK_WMT_FUNC_CTRL;
2866 wmt_params.flag = 0;
2867 wmt_params.dlen = sizeof(param);
2868 wmt_params.data = ¶m;
2869 wmt_params.status = NULL;
2871 err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
2873 bt_dev_err(hdev, "Failed to send wmt func ctrl (%d)", err);
2877 hci_set_msft_opcode(hdev, 0xFD30);
2878 hci_set_aosp_capable(hdev);
2881 bt_dev_err(hdev, "Unsupported hardware variant (%08x)",
2886 /* Query whether the firmware is already download */
2887 wmt_params.op = BTMTK_WMT_SEMAPHORE;
2888 wmt_params.flag = 1;
2889 wmt_params.dlen = 0;
2890 wmt_params.data = NULL;
2891 wmt_params.status = &status;
2893 err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
2895 bt_dev_err(hdev, "Failed to query firmware status (%d)", err);
2899 if (status == BTMTK_WMT_PATCH_DONE) {
2900 bt_dev_info(hdev, "firmware already downloaded");
2901 goto ignore_setup_fw;
2904 /* Setup a firmware which the device definitely requires */
2905 err = btusb_mtk_setup_firmware(hdev, fwname);
2910 err = readx_poll_timeout(btusb_mtk_func_query, hdev, status,
2911 status < 0 || status != BTMTK_WMT_ON_PROGRESS,
2913 /* -ETIMEDOUT happens */
2917 /* The other errors happen in btusb_mtk_func_query */
2921 if (status == BTMTK_WMT_ON_DONE) {
2922 bt_dev_info(hdev, "function already on");
2923 goto ignore_func_on;
2926 /* Enable Bluetooth protocol */
2928 wmt_params.op = BTMTK_WMT_FUNC_CTRL;
2929 wmt_params.flag = 0;
2930 wmt_params.dlen = sizeof(param);
2931 wmt_params.data = ¶m;
2932 wmt_params.status = NULL;
2934 err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
2936 bt_dev_err(hdev, "Failed to send wmt func ctrl (%d)", err);
2941 /* Apply the low power environment setup */
2942 tci_sleep.mode = 0x5;
2943 tci_sleep.duration = cpu_to_le16(0x640);
2944 tci_sleep.host_duration = cpu_to_le16(0x640);
2945 tci_sleep.host_wakeup_pin = 0;
2946 tci_sleep.time_compensation = 0;
2948 skb = __hci_cmd_sync(hdev, 0xfc7a, sizeof(tci_sleep), &tci_sleep,
2952 bt_dev_err(hdev, "Failed to apply low power setting (%d)", err);
2958 rettime = ktime_get();
2959 delta = ktime_sub(rettime, calltime);
2960 duration = (unsigned long long)ktime_to_ns(delta) >> 10;
2962 bt_dev_info(hdev, "Device setup in %llu usecs", duration);
2967 static int btusb_mtk_shutdown(struct hci_dev *hdev)
2969 struct btmtk_hci_wmt_params wmt_params;
2973 /* Disable the device */
2974 wmt_params.op = BTMTK_WMT_FUNC_CTRL;
2975 wmt_params.flag = 0;
2976 wmt_params.dlen = sizeof(param);
2977 wmt_params.data = ¶m;
2978 wmt_params.status = NULL;
2980 err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
2982 bt_dev_err(hdev, "Failed to send wmt func ctrl (%d)", err);
2989 static void btusb_mtk_cmd_timeout(struct hci_dev *hdev)
2991 struct btusb_data *data = hci_get_drvdata(hdev);
2995 /* It's MediaTek specific bluetooth reset mechanism via USB */
2996 if (test_and_set_bit(BTUSB_HW_RESET_ACTIVE, &data->flags)) {
2997 bt_dev_err(hdev, "last reset failed? Not resetting again");
3001 err = usb_autopm_get_interface(data->intf);
3005 btusb_stop_traffic(data);
3006 usb_kill_anchored_urbs(&data->tx_anchor);
3008 /* It's Device EndPoint Reset Option Register */
3009 bt_dev_dbg(hdev, "Initiating reset mechanism via uhw");
3010 btusb_mtk_uhw_reg_write(data, MTK_EP_RST_OPT, MTK_EP_RST_IN_OUT_OPT);
3011 btusb_mtk_uhw_reg_read(data, MTK_BT_WDT_STATUS, &val);
3013 /* Reset the bluetooth chip via USB interface. */
3014 btusb_mtk_uhw_reg_write(data, MTK_BT_SUBSYS_RST, 1);
3015 btusb_mtk_uhw_reg_write(data, MTK_UDMA_INT_STA_BT, 0x000000FF);
3016 btusb_mtk_uhw_reg_read(data, MTK_UDMA_INT_STA_BT, &val);
3017 btusb_mtk_uhw_reg_write(data, MTK_UDMA_INT_STA_BT1, 0x000000FF);
3018 btusb_mtk_uhw_reg_read(data, MTK_UDMA_INT_STA_BT1, &val);
3019 /* MT7921 need to delay 20ms between toggle reset bit */
3021 btusb_mtk_uhw_reg_write(data, MTK_BT_SUBSYS_RST, 0);
3022 btusb_mtk_uhw_reg_read(data, MTK_BT_SUBSYS_RST, &val);
3024 /* Poll the register until reset is completed */
3026 btusb_mtk_uhw_reg_read(data, MTK_BT_MISC, &val);
3027 if (val & MTK_BT_RST_DONE) {
3028 bt_dev_dbg(hdev, "Bluetooth Reset Successfully");
3032 bt_dev_dbg(hdev, "Polling Bluetooth Reset CR");
3034 msleep(MTK_BT_RESET_WAIT_MS);
3035 } while (retry < MTK_BT_RESET_NUM_TRIES);
3037 btusb_mtk_id_get(data, 0x70010200, &val);
3039 bt_dev_err(hdev, "Can't get device id, subsys reset fail.");
3041 usb_queue_reset_device(data->intf);
3043 clear_bit(BTUSB_HW_RESET_ACTIVE, &data->flags);
3046 static int btusb_recv_acl_mtk(struct hci_dev *hdev, struct sk_buff *skb)
3048 struct btusb_data *data = hci_get_drvdata(hdev);
3049 u16 handle = le16_to_cpu(hci_acl_hdr(skb)->handle);
3052 case 0xfc6f: /* Firmware dump from device */
3053 /* When the firmware hangs, the device can no longer
3054 * suspend and thus disable auto-suspend.
3056 usb_disable_autosuspend(data->udev);
3058 case 0x05ff: /* Firmware debug logging 1 */
3059 case 0x05fe: /* Firmware debug logging 2 */
3060 return hci_recv_diag(hdev, skb);
3063 return hci_recv_frame(hdev, skb);
3066 MODULE_FIRMWARE(FIRMWARE_MT7663);
3067 MODULE_FIRMWARE(FIRMWARE_MT7668);
3070 /* Configure an out-of-band gpio as wake-up pin, if specified in device tree */
3071 static int marvell_config_oob_wake(struct hci_dev *hdev)
3073 struct sk_buff *skb;
3074 struct btusb_data *data = hci_get_drvdata(hdev);
3075 struct device *dev = &data->udev->dev;
3076 u16 pin, gap, opcode;
3080 /* Move on if no wakeup pin specified */
3081 if (of_property_read_u16(dev->of_node, "marvell,wakeup-pin", &pin) ||
3082 of_property_read_u16(dev->of_node, "marvell,wakeup-gap-ms", &gap))
3085 /* Vendor specific command to configure a GPIO as wake-up pin */
3086 opcode = hci_opcode_pack(0x3F, 0x59);
3087 cmd[0] = opcode & 0xFF;
3088 cmd[1] = opcode >> 8;
3089 cmd[2] = 2; /* length of parameters that follow */
3091 cmd[4] = gap; /* time in ms, for which wakeup pin should be asserted */
3093 skb = bt_skb_alloc(sizeof(cmd), GFP_KERNEL);
3095 bt_dev_err(hdev, "%s: No memory", __func__);
3099 skb_put_data(skb, cmd, sizeof(cmd));
3100 hci_skb_pkt_type(skb) = HCI_COMMAND_PKT;
3102 ret = btusb_send_frame(hdev, skb);
3104 bt_dev_err(hdev, "%s: configuration failed", __func__);
3113 static int btusb_set_bdaddr_marvell(struct hci_dev *hdev,
3114 const bdaddr_t *bdaddr)
3116 struct sk_buff *skb;
3121 buf[1] = sizeof(bdaddr_t);
3122 memcpy(buf + 2, bdaddr, sizeof(bdaddr_t));
3124 skb = __hci_cmd_sync(hdev, 0xfc22, sizeof(buf), buf, HCI_INIT_TIMEOUT);
3127 bt_dev_err(hdev, "changing Marvell device address failed (%ld)",
3136 static int btusb_set_bdaddr_ath3012(struct hci_dev *hdev,
3137 const bdaddr_t *bdaddr)
3139 struct sk_buff *skb;
3146 buf[3] = sizeof(bdaddr_t);
3147 memcpy(buf + 4, bdaddr, sizeof(bdaddr_t));
3149 skb = __hci_cmd_sync(hdev, 0xfc0b, sizeof(buf), buf, HCI_INIT_TIMEOUT);
3152 bt_dev_err(hdev, "Change address command failed (%ld)", ret);
3160 static int btusb_set_bdaddr_wcn6855(struct hci_dev *hdev,
3161 const bdaddr_t *bdaddr)
3163 struct sk_buff *skb;
3167 memcpy(buf, bdaddr, sizeof(bdaddr_t));
3169 skb = __hci_cmd_sync_ev(hdev, 0xfc14, sizeof(buf), buf,
3170 HCI_EV_CMD_COMPLETE, HCI_INIT_TIMEOUT);
3173 bt_dev_err(hdev, "Change address command failed (%ld)", ret);
3181 #define QCA_DFU_PACKET_LEN 4096
3183 #define QCA_GET_TARGET_VERSION 0x09
3184 #define QCA_CHECK_STATUS 0x05
3185 #define QCA_DFU_DOWNLOAD 0x01
3187 #define QCA_SYSCFG_UPDATED 0x40
3188 #define QCA_PATCH_UPDATED 0x80
3189 #define QCA_DFU_TIMEOUT 3000
3190 #define QCA_FLAG_MULTI_NVM 0x80
3192 struct qca_version {
3194 __le32 patch_version;
3201 struct qca_rampatch_version {
3202 __le16 rom_version_high;
3203 __le16 rom_version_low;
3204 __le16 patch_version;
3207 struct qca_device_info {
3209 u8 rampatch_hdr; /* length of header in rampatch */
3210 u8 nvm_hdr; /* length of header in NVM */
3211 u8 ver_offset; /* offset of version structure in rampatch */
3214 static const struct qca_device_info qca_devices_table[] = {
3215 { 0x00000100, 20, 4, 8 }, /* Rome 1.0 */
3216 { 0x00000101, 20, 4, 8 }, /* Rome 1.1 */
3217 { 0x00000200, 28, 4, 16 }, /* Rome 2.0 */
3218 { 0x00000201, 28, 4, 16 }, /* Rome 2.1 */
3219 { 0x00000300, 28, 4, 16 }, /* Rome 3.0 */
3220 { 0x00000302, 28, 4, 16 }, /* Rome 3.2 */
3221 { 0x00130100, 40, 4, 16 }, /* WCN6855 1.0 */
3222 { 0x00130200, 40, 4, 16 }, /* WCN6855 2.0 */
3225 static int btusb_qca_send_vendor_req(struct usb_device *udev, u8 request,
3226 void *data, u16 size)
3231 buf = kmalloc(size, GFP_KERNEL);
3235 /* Found some of USB hosts have IOT issues with ours so that we should
3236 * not wait until HCI layer is ready.
3238 pipe = usb_rcvctrlpipe(udev, 0);
3239 err = usb_control_msg(udev, pipe, request, USB_TYPE_VENDOR | USB_DIR_IN,
3240 0, 0, buf, size, USB_CTRL_SET_TIMEOUT);
3242 dev_err(&udev->dev, "Failed to access otp area (%d)", err);
3246 memcpy(data, buf, size);
3254 static int btusb_setup_qca_download_fw(struct hci_dev *hdev,
3255 const struct firmware *firmware,
3258 struct btusb_data *btdata = hci_get_drvdata(hdev);
3259 struct usb_device *udev = btdata->udev;
3260 size_t count, size, sent = 0;
3264 buf = kmalloc(QCA_DFU_PACKET_LEN, GFP_KERNEL);
3268 count = firmware->size;
3270 size = min_t(size_t, count, hdr_size);
3271 memcpy(buf, firmware->data, size);
3273 /* USB patches should go down to controller through USB path
3274 * because binary format fits to go down through USB channel.
3275 * USB control path is for patching headers and USB bulk is for
3278 pipe = usb_sndctrlpipe(udev, 0);
3279 err = usb_control_msg(udev, pipe, QCA_DFU_DOWNLOAD, USB_TYPE_VENDOR,
3280 0, 0, buf, size, USB_CTRL_SET_TIMEOUT);
3282 bt_dev_err(hdev, "Failed to send headers (%d)", err);
3289 /* ep2 need time to switch from function acl to function dfu,
3290 * so we add 20ms delay here.
3295 size = min_t(size_t, count, QCA_DFU_PACKET_LEN);
3297 memcpy(buf, firmware->data + sent, size);
3299 pipe = usb_sndbulkpipe(udev, 0x02);
3300 err = usb_bulk_msg(udev, pipe, buf, size, &len,
3303 bt_dev_err(hdev, "Failed to send body at %zd of %zd (%d)",
3304 sent, firmware->size, err);
3309 bt_dev_err(hdev, "Failed to get bulk buffer");
3323 static int btusb_setup_qca_load_rampatch(struct hci_dev *hdev,
3324 struct qca_version *ver,
3325 const struct qca_device_info *info)
3327 struct qca_rampatch_version *rver;
3328 const struct firmware *fw;
3329 u32 ver_rom, ver_patch, rver_rom;
3330 u16 rver_rom_low, rver_rom_high, rver_patch;
3334 ver_rom = le32_to_cpu(ver->rom_version);
3335 ver_patch = le32_to_cpu(ver->patch_version);
3337 snprintf(fwname, sizeof(fwname), "qca/rampatch_usb_%08x.bin", ver_rom);
3339 err = request_firmware(&fw, fwname, &hdev->dev);
3341 bt_dev_err(hdev, "failed to request rampatch file: %s (%d)",
3346 bt_dev_info(hdev, "using rampatch file: %s", fwname);
3348 rver = (struct qca_rampatch_version *)(fw->data + info->ver_offset);
3349 rver_rom_low = le16_to_cpu(rver->rom_version_low);
3350 rver_patch = le16_to_cpu(rver->patch_version);
3352 if (ver_rom & ~0xffffU) {
3353 rver_rom_high = le16_to_cpu(rver->rom_version_high);
3354 rver_rom = le32_to_cpu(rver_rom_high << 16 | rver_rom_low);
3356 rver_rom = rver_rom_low;
3359 bt_dev_info(hdev, "QCA: patch rome 0x%x build 0x%x, "
3360 "firmware rome 0x%x build 0x%x",
3361 rver_rom, rver_patch, ver_rom, ver_patch);
3363 if (rver_rom != ver_rom || rver_patch <= ver_patch) {
3364 bt_dev_err(hdev, "rampatch file version did not match with firmware");
3369 err = btusb_setup_qca_download_fw(hdev, fw, info->rampatch_hdr);
3372 release_firmware(fw);
3377 static int btusb_setup_qca_load_nvm(struct hci_dev *hdev,
3378 struct qca_version *ver,
3379 const struct qca_device_info *info)
3381 const struct firmware *fw;
3385 if (((ver->flag >> 8) & 0xff) == QCA_FLAG_MULTI_NVM) {
3386 /* if boardid equal 0, use default nvm without surfix */
3387 if (le16_to_cpu(ver->board_id) == 0x0) {
3388 snprintf(fwname, sizeof(fwname), "qca/nvm_usb_%08x.bin",
3389 le32_to_cpu(ver->rom_version));
3391 snprintf(fwname, sizeof(fwname), "qca/nvm_usb_%08x_%04x.bin",
3392 le32_to_cpu(ver->rom_version),
3393 le16_to_cpu(ver->board_id));
3396 snprintf(fwname, sizeof(fwname), "qca/nvm_usb_%08x.bin",
3397 le32_to_cpu(ver->rom_version));
3400 err = request_firmware(&fw, fwname, &hdev->dev);
3402 bt_dev_err(hdev, "failed to request NVM file: %s (%d)",
3407 bt_dev_info(hdev, "using NVM file: %s", fwname);
3409 err = btusb_setup_qca_download_fw(hdev, fw, info->nvm_hdr);
3411 release_firmware(fw);
3416 /* identify the ROM version and check whether patches are needed */
3417 static bool btusb_qca_need_patch(struct usb_device *udev)
3419 struct qca_version ver;
3421 if (btusb_qca_send_vendor_req(udev, QCA_GET_TARGET_VERSION, &ver,
3424 /* only low ROM versions need patches */
3425 return !(le32_to_cpu(ver.rom_version) & ~0xffffU);
3428 static int btusb_setup_qca(struct hci_dev *hdev)
3430 struct btusb_data *btdata = hci_get_drvdata(hdev);
3431 struct usb_device *udev = btdata->udev;
3432 const struct qca_device_info *info = NULL;
3433 struct qca_version ver;
3438 err = btusb_qca_send_vendor_req(udev, QCA_GET_TARGET_VERSION, &ver,
3443 ver_rom = le32_to_cpu(ver.rom_version);
3445 for (i = 0; i < ARRAY_SIZE(qca_devices_table); i++) {
3446 if (ver_rom == qca_devices_table[i].rom_version)
3447 info = &qca_devices_table[i];
3450 /* If the rom_version is not matched in the qca_devices_table
3451 * and the high ROM version is not zero, we assume this chip no
3452 * need to load the rampatch and nvm.
3454 if (ver_rom & ~0xffffU)
3457 bt_dev_err(hdev, "don't support firmware rome 0x%x", ver_rom);
3461 err = btusb_qca_send_vendor_req(udev, QCA_CHECK_STATUS, &status,
3466 if (!(status & QCA_PATCH_UPDATED)) {
3467 err = btusb_setup_qca_load_rampatch(hdev, &ver, info);
3472 err = btusb_qca_send_vendor_req(udev, QCA_GET_TARGET_VERSION, &ver,
3477 if (!(status & QCA_SYSCFG_UPDATED)) {
3478 err = btusb_setup_qca_load_nvm(hdev, &ver, info);
3486 static inline int __set_diag_interface(struct hci_dev *hdev)
3488 struct btusb_data *data = hci_get_drvdata(hdev);
3489 struct usb_interface *intf = data->diag;
3495 data->diag_tx_ep = NULL;
3496 data->diag_rx_ep = NULL;
3498 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
3499 struct usb_endpoint_descriptor *ep_desc;
3501 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
3503 if (!data->diag_tx_ep && usb_endpoint_is_bulk_out(ep_desc)) {
3504 data->diag_tx_ep = ep_desc;
3508 if (!data->diag_rx_ep && usb_endpoint_is_bulk_in(ep_desc)) {
3509 data->diag_rx_ep = ep_desc;
3514 if (!data->diag_tx_ep || !data->diag_rx_ep) {
3515 bt_dev_err(hdev, "invalid diagnostic descriptors");
3522 static struct urb *alloc_diag_urb(struct hci_dev *hdev, bool enable)
3524 struct btusb_data *data = hci_get_drvdata(hdev);
3525 struct sk_buff *skb;
3529 if (!data->diag_tx_ep)
3530 return ERR_PTR(-ENODEV);
3532 urb = usb_alloc_urb(0, GFP_KERNEL);
3534 return ERR_PTR(-ENOMEM);
3536 skb = bt_skb_alloc(2, GFP_KERNEL);
3539 return ERR_PTR(-ENOMEM);
3542 skb_put_u8(skb, 0xf0);
3543 skb_put_u8(skb, enable);
3545 pipe = usb_sndbulkpipe(data->udev, data->diag_tx_ep->bEndpointAddress);
3547 usb_fill_bulk_urb(urb, data->udev, pipe,
3548 skb->data, skb->len, btusb_tx_complete, skb);
3550 skb->dev = (void *)hdev;
3555 static int btusb_bcm_set_diag(struct hci_dev *hdev, bool enable)
3557 struct btusb_data *data = hci_get_drvdata(hdev);
3563 if (!test_bit(HCI_RUNNING, &hdev->flags))
3566 urb = alloc_diag_urb(hdev, enable);
3568 return PTR_ERR(urb);
3570 return submit_or_queue_tx_urb(hdev, urb);
3574 static irqreturn_t btusb_oob_wake_handler(int irq, void *priv)
3576 struct btusb_data *data = priv;
3578 pm_wakeup_event(&data->udev->dev, 0);
3581 /* Disable only if not already disabled (keep it balanced) */
3582 if (test_and_clear_bit(BTUSB_OOB_WAKE_ENABLED, &data->flags)) {
3583 disable_irq_nosync(irq);
3584 disable_irq_wake(irq);
3589 static const struct of_device_id btusb_match_table[] = {
3590 { .compatible = "usb1286,204e" },
3591 { .compatible = "usbcf3,e300" }, /* QCA6174A */
3592 { .compatible = "usb4ca,301a" }, /* QCA6174A (Lite-On) */
3595 MODULE_DEVICE_TABLE(of, btusb_match_table);
3597 /* Use an oob wakeup pin? */
3598 static int btusb_config_oob_wake(struct hci_dev *hdev)
3600 struct btusb_data *data = hci_get_drvdata(hdev);
3601 struct device *dev = &data->udev->dev;
3604 clear_bit(BTUSB_OOB_WAKE_ENABLED, &data->flags);
3606 if (!of_match_device(btusb_match_table, dev))
3609 /* Move on if no IRQ specified */
3610 irq = of_irq_get_byname(dev->of_node, "wakeup");
3612 bt_dev_dbg(hdev, "%s: no OOB Wakeup IRQ in DT", __func__);
3616 irq_set_status_flags(irq, IRQ_NOAUTOEN);
3617 ret = devm_request_irq(&hdev->dev, irq, btusb_oob_wake_handler,
3618 0, "OOB Wake-on-BT", data);
3620 bt_dev_err(hdev, "%s: IRQ request failed", __func__);
3624 ret = device_init_wakeup(dev, true);
3626 bt_dev_err(hdev, "%s: failed to init_wakeup", __func__);
3630 data->oob_wake_irq = irq;
3631 bt_dev_info(hdev, "OOB Wake-on-BT configured at IRQ %u", irq);
3636 static void btusb_check_needs_reset_resume(struct usb_interface *intf)
3638 if (dmi_check_system(btusb_needs_reset_resume_table))
3639 interface_to_usbdev(intf)->quirks |= USB_QUIRK_RESET_RESUME;
3642 static bool btusb_prevent_wake(struct hci_dev *hdev)
3644 struct btusb_data *data = hci_get_drvdata(hdev);
3646 return !device_may_wakeup(&data->udev->dev);
3649 static int btusb_shutdown_qca(struct hci_dev *hdev)
3651 struct sk_buff *skb;
3653 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
3655 bt_dev_err(hdev, "HCI reset during shutdown failed");
3656 return PTR_ERR(skb);
3663 static int btusb_probe(struct usb_interface *intf,
3664 const struct usb_device_id *id)
3666 struct usb_endpoint_descriptor *ep_desc;
3667 struct gpio_desc *reset_gpio;
3668 struct btusb_data *data;
3669 struct hci_dev *hdev;
3670 unsigned ifnum_base;
3671 int i, err, priv_size;
3673 BT_DBG("intf %p id %p", intf, id);
3675 /* interface numbers are hardcoded in the spec */
3676 if (intf->cur_altsetting->desc.bInterfaceNumber != 0) {
3677 if (!(id->driver_info & BTUSB_IFNUM_2))
3679 if (intf->cur_altsetting->desc.bInterfaceNumber != 2)
3683 ifnum_base = intf->cur_altsetting->desc.bInterfaceNumber;
3685 if (!id->driver_info) {
3686 const struct usb_device_id *match;
3688 match = usb_match_id(intf, blacklist_table);
3693 if (id->driver_info == BTUSB_IGNORE)
3696 if (id->driver_info & BTUSB_ATH3012) {
3697 struct usb_device *udev = interface_to_usbdev(intf);
3699 /* Old firmware would otherwise let ath3k driver load
3700 * patch and sysconfig files
3702 if (le16_to_cpu(udev->descriptor.bcdDevice) <= 0x0001 &&
3703 !btusb_qca_need_patch(udev))
3707 data = devm_kzalloc(&intf->dev, sizeof(*data), GFP_KERNEL);
3711 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
3712 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
3714 if (!data->intr_ep && usb_endpoint_is_int_in(ep_desc)) {
3715 data->intr_ep = ep_desc;
3719 if (!data->bulk_tx_ep && usb_endpoint_is_bulk_out(ep_desc)) {
3720 data->bulk_tx_ep = ep_desc;
3724 if (!data->bulk_rx_ep && usb_endpoint_is_bulk_in(ep_desc)) {
3725 data->bulk_rx_ep = ep_desc;
3730 if (!data->intr_ep || !data->bulk_tx_ep || !data->bulk_rx_ep)
3733 if (id->driver_info & BTUSB_AMP) {
3734 data->cmdreq_type = USB_TYPE_CLASS | 0x01;
3735 data->cmdreq = 0x2b;
3737 data->cmdreq_type = USB_TYPE_CLASS;
3738 data->cmdreq = 0x00;
3741 data->udev = interface_to_usbdev(intf);
3744 INIT_WORK(&data->work, btusb_work);
3745 INIT_WORK(&data->waker, btusb_waker);
3746 init_usb_anchor(&data->deferred);
3747 init_usb_anchor(&data->tx_anchor);
3748 spin_lock_init(&data->txlock);
3750 init_usb_anchor(&data->intr_anchor);
3751 init_usb_anchor(&data->bulk_anchor);
3752 init_usb_anchor(&data->isoc_anchor);
3753 init_usb_anchor(&data->diag_anchor);
3754 init_usb_anchor(&data->ctrl_anchor);
3755 spin_lock_init(&data->rxlock);
3759 data->recv_event = hci_recv_frame;
3760 data->recv_bulk = btusb_recv_bulk;
3762 if (id->driver_info & BTUSB_INTEL_COMBINED) {
3763 /* Allocate extra space for Intel device */
3764 priv_size += sizeof(struct btintel_data);
3766 /* Override the rx handlers */
3767 data->recv_event = btusb_recv_event_intel;
3768 data->recv_bulk = btusb_recv_bulk_intel;
3771 data->recv_acl = hci_recv_frame;
3773 hdev = hci_alloc_dev_priv(priv_size);
3777 hdev->bus = HCI_USB;
3778 hci_set_drvdata(hdev, data);
3780 if (id->driver_info & BTUSB_AMP)
3781 hdev->dev_type = HCI_AMP;
3783 hdev->dev_type = HCI_PRIMARY;
3787 SET_HCIDEV_DEV(hdev, &intf->dev);
3789 reset_gpio = gpiod_get_optional(&data->udev->dev, "reset",
3791 if (IS_ERR(reset_gpio)) {
3792 err = PTR_ERR(reset_gpio);
3794 } else if (reset_gpio) {
3795 data->reset_gpio = reset_gpio;
3798 hdev->open = btusb_open;
3799 hdev->close = btusb_close;
3800 hdev->flush = btusb_flush;
3801 hdev->send = btusb_send_frame;
3802 hdev->notify = btusb_notify;
3803 hdev->prevent_wake = btusb_prevent_wake;
3806 err = btusb_config_oob_wake(hdev);
3810 /* Marvell devices may need a specific chip configuration */
3811 if (id->driver_info & BTUSB_MARVELL && data->oob_wake_irq) {
3812 err = marvell_config_oob_wake(hdev);
3817 if (id->driver_info & BTUSB_CW6622)
3818 set_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks);
3820 if (id->driver_info & BTUSB_BCM2045)
3821 set_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks);
3823 if (id->driver_info & BTUSB_BCM92035)
3824 hdev->setup = btusb_setup_bcm92035;
3826 if (IS_ENABLED(CONFIG_BT_HCIBTUSB_BCM) &&
3827 (id->driver_info & BTUSB_BCM_PATCHRAM)) {
3828 hdev->manufacturer = 15;
3829 hdev->setup = btbcm_setup_patchram;
3830 hdev->set_diag = btusb_bcm_set_diag;
3831 hdev->set_bdaddr = btbcm_set_bdaddr;
3833 /* Broadcom LM_DIAG Interface numbers are hardcoded */
3834 data->diag = usb_ifnum_to_if(data->udev, ifnum_base + 2);
3837 if (IS_ENABLED(CONFIG_BT_HCIBTUSB_BCM) &&
3838 (id->driver_info & BTUSB_BCM_APPLE)) {
3839 hdev->manufacturer = 15;
3840 hdev->setup = btbcm_setup_apple;
3841 hdev->set_diag = btusb_bcm_set_diag;
3843 /* Broadcom LM_DIAG Interface numbers are hardcoded */
3844 data->diag = usb_ifnum_to_if(data->udev, ifnum_base + 2);
3847 /* Combined Intel Device setup to support multiple setup routine */
3848 if (id->driver_info & BTUSB_INTEL_COMBINED) {
3849 err = btintel_configure_setup(hdev);
3853 /* Transport specific configuration */
3854 hdev->send = btusb_send_frame_intel;
3855 hdev->cmd_timeout = btusb_intel_cmd_timeout;
3857 if (id->driver_info & BTUSB_INTEL_BROKEN_INITIAL_NCMD)
3858 btintel_set_flag(hdev, INTEL_BROKEN_INITIAL_NCMD);
3860 if (id->driver_info & BTUSB_INTEL_BROKEN_SHUTDOWN_LED)
3861 btintel_set_flag(hdev, INTEL_BROKEN_SHUTDOWN_LED);
3864 if (id->driver_info & BTUSB_MARVELL)
3865 hdev->set_bdaddr = btusb_set_bdaddr_marvell;
3867 if (IS_ENABLED(CONFIG_BT_HCIBTUSB_MTK) &&
3868 (id->driver_info & BTUSB_MEDIATEK)) {
3869 hdev->setup = btusb_mtk_setup;
3870 hdev->shutdown = btusb_mtk_shutdown;
3871 hdev->manufacturer = 70;
3872 hdev->cmd_timeout = btusb_mtk_cmd_timeout;
3873 set_bit(HCI_QUIRK_NON_PERSISTENT_SETUP, &hdev->quirks);
3874 data->recv_acl = btusb_recv_acl_mtk;
3877 if (id->driver_info & BTUSB_SWAVE) {
3878 set_bit(HCI_QUIRK_FIXUP_INQUIRY_MODE, &hdev->quirks);
3879 set_bit(HCI_QUIRK_BROKEN_LOCAL_COMMANDS, &hdev->quirks);
3882 if (id->driver_info & BTUSB_INTEL_BOOT) {
3883 hdev->manufacturer = 2;
3884 set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
3887 if (id->driver_info & BTUSB_ATH3012) {
3888 data->setup_on_usb = btusb_setup_qca;
3889 hdev->set_bdaddr = btusb_set_bdaddr_ath3012;
3890 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
3891 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
3894 if (id->driver_info & BTUSB_QCA_ROME) {
3895 data->setup_on_usb = btusb_setup_qca;
3896 hdev->set_bdaddr = btusb_set_bdaddr_ath3012;
3897 hdev->cmd_timeout = btusb_qca_cmd_timeout;
3898 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
3899 btusb_check_needs_reset_resume(intf);
3902 if (id->driver_info & BTUSB_QCA_WCN6855) {
3903 data->setup_on_usb = btusb_setup_qca;
3904 hdev->shutdown = btusb_shutdown_qca;
3905 hdev->set_bdaddr = btusb_set_bdaddr_wcn6855;
3906 hdev->cmd_timeout = btusb_qca_cmd_timeout;
3907 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
3908 hci_set_msft_opcode(hdev, 0xFD70);
3911 if (id->driver_info & BTUSB_AMP) {
3912 /* AMP controllers do not support SCO packets */
3915 /* Interface orders are hardcoded in the specification */
3916 data->isoc = usb_ifnum_to_if(data->udev, ifnum_base + 1);
3917 data->isoc_ifnum = ifnum_base + 1;
3920 if (IS_ENABLED(CONFIG_BT_HCIBTUSB_RTL) &&
3921 (id->driver_info & BTUSB_REALTEK)) {
3922 hdev->setup = btrtl_setup_realtek;
3923 hdev->shutdown = btrtl_shutdown_realtek;
3924 hdev->cmd_timeout = btusb_rtl_cmd_timeout;
3926 /* Realtek devices need to set remote wakeup on auto-suspend */
3927 set_bit(BTUSB_WAKEUP_AUTOSUSPEND, &data->flags);
3928 set_bit(BTUSB_USE_ALT3_FOR_WBS, &data->flags);
3932 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
3934 if (force_scofix || id->driver_info & BTUSB_WRONG_SCO_MTU) {
3935 if (!disable_scofix)
3936 set_bit(HCI_QUIRK_FIXUP_BUFFER_SIZE, &hdev->quirks);
3939 if (id->driver_info & BTUSB_BROKEN_ISOC)
3942 if (id->driver_info & BTUSB_WIDEBAND_SPEECH)
3943 set_bit(HCI_QUIRK_WIDEBAND_SPEECH_SUPPORTED, &hdev->quirks);
3945 if (id->driver_info & BTUSB_VALID_LE_STATES)
3946 set_bit(HCI_QUIRK_VALID_LE_STATES, &hdev->quirks);
3948 if (id->driver_info & BTUSB_DIGIANSWER) {
3949 data->cmdreq_type = USB_TYPE_VENDOR;
3950 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
3953 if (id->driver_info & BTUSB_CSR) {
3954 struct usb_device *udev = data->udev;
3955 u16 bcdDevice = le16_to_cpu(udev->descriptor.bcdDevice);
3957 /* Old firmware would otherwise execute USB reset */
3958 if (bcdDevice < 0x117)
3959 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
3961 /* This must be set first in case we disable it for fakes */
3962 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
3964 /* Fake CSR devices with broken commands */
3965 if (le16_to_cpu(udev->descriptor.idVendor) == 0x0a12 &&
3966 le16_to_cpu(udev->descriptor.idProduct) == 0x0001)
3967 hdev->setup = btusb_setup_csr;
3970 if (id->driver_info & BTUSB_SNIFFER) {
3971 struct usb_device *udev = data->udev;
3973 /* New sniffer firmware has crippled HCI interface */
3974 if (le16_to_cpu(udev->descriptor.bcdDevice) > 0x997)
3975 set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
3978 if (id->driver_info & BTUSB_INTEL_BOOT) {
3979 /* A bug in the bootloader causes that interrupt interface is
3980 * only enabled after receiving SetInterface(0, AltSetting=0).
3982 err = usb_set_interface(data->udev, 0, 0);
3984 BT_ERR("failed to set interface 0, alt 0 %d", err);
3990 err = usb_driver_claim_interface(&btusb_driver,
3996 if (IS_ENABLED(CONFIG_BT_HCIBTUSB_BCM) && data->diag) {
3997 if (!usb_driver_claim_interface(&btusb_driver,
3999 __set_diag_interface(hdev);
4004 if (enable_autosuspend)
4005 usb_enable_autosuspend(data->udev);
4007 err = hci_register_dev(hdev);
4011 usb_set_intfdata(intf, data);
4016 if (data->reset_gpio)
4017 gpiod_put(data->reset_gpio);
4022 static void btusb_disconnect(struct usb_interface *intf)
4024 struct btusb_data *data = usb_get_intfdata(intf);
4025 struct hci_dev *hdev;
4027 BT_DBG("intf %p", intf);
4033 usb_set_intfdata(data->intf, NULL);
4036 usb_set_intfdata(data->isoc, NULL);
4039 usb_set_intfdata(data->diag, NULL);
4041 hci_unregister_dev(hdev);
4043 if (intf == data->intf) {
4045 usb_driver_release_interface(&btusb_driver, data->isoc);
4047 usb_driver_release_interface(&btusb_driver, data->diag);
4048 } else if (intf == data->isoc) {
4050 usb_driver_release_interface(&btusb_driver, data->diag);
4051 usb_driver_release_interface(&btusb_driver, data->intf);
4052 } else if (intf == data->diag) {
4053 usb_driver_release_interface(&btusb_driver, data->intf);
4055 usb_driver_release_interface(&btusb_driver, data->isoc);
4058 if (data->oob_wake_irq)
4059 device_init_wakeup(&data->udev->dev, false);
4061 if (data->reset_gpio)
4062 gpiod_put(data->reset_gpio);
4068 static int btusb_suspend(struct usb_interface *intf, pm_message_t message)
4070 struct btusb_data *data = usb_get_intfdata(intf);
4072 BT_DBG("intf %p", intf);
4074 if (data->suspend_count++)
4077 spin_lock_irq(&data->txlock);
4078 if (!(PMSG_IS_AUTO(message) && data->tx_in_flight)) {
4079 set_bit(BTUSB_SUSPENDING, &data->flags);
4080 spin_unlock_irq(&data->txlock);
4082 spin_unlock_irq(&data->txlock);
4083 data->suspend_count--;
4087 cancel_work_sync(&data->work);
4089 btusb_stop_traffic(data);
4090 usb_kill_anchored_urbs(&data->tx_anchor);
4092 if (data->oob_wake_irq && device_may_wakeup(&data->udev->dev)) {
4093 set_bit(BTUSB_OOB_WAKE_ENABLED, &data->flags);
4094 enable_irq_wake(data->oob_wake_irq);
4095 enable_irq(data->oob_wake_irq);
4098 /* For global suspend, Realtek devices lose the loaded fw
4099 * in them. But for autosuspend, firmware should remain.
4100 * Actually, it depends on whether the usb host sends
4101 * set feature (enable wakeup) or not.
4103 if (test_bit(BTUSB_WAKEUP_AUTOSUSPEND, &data->flags)) {
4104 if (PMSG_IS_AUTO(message) &&
4105 device_can_wakeup(&data->udev->dev))
4106 data->udev->do_remote_wakeup = 1;
4107 else if (!PMSG_IS_AUTO(message) &&
4108 !device_may_wakeup(&data->udev->dev)) {
4109 data->udev->do_remote_wakeup = 0;
4110 data->udev->reset_resume = 1;
4117 static void play_deferred(struct btusb_data *data)
4122 while ((urb = usb_get_from_anchor(&data->deferred))) {
4123 usb_anchor_urb(urb, &data->tx_anchor);
4125 err = usb_submit_urb(urb, GFP_ATOMIC);
4127 if (err != -EPERM && err != -ENODEV)
4128 BT_ERR("%s urb %p submission failed (%d)",
4129 data->hdev->name, urb, -err);
4130 kfree(urb->setup_packet);
4131 usb_unanchor_urb(urb);
4136 data->tx_in_flight++;
4140 /* Cleanup the rest deferred urbs. */
4141 while ((urb = usb_get_from_anchor(&data->deferred))) {
4142 kfree(urb->setup_packet);
4147 static int btusb_resume(struct usb_interface *intf)
4149 struct btusb_data *data = usb_get_intfdata(intf);
4150 struct hci_dev *hdev = data->hdev;
4153 BT_DBG("intf %p", intf);
4155 if (--data->suspend_count)
4158 /* Disable only if not already disabled (keep it balanced) */
4159 if (test_and_clear_bit(BTUSB_OOB_WAKE_ENABLED, &data->flags)) {
4160 disable_irq(data->oob_wake_irq);
4161 disable_irq_wake(data->oob_wake_irq);
4164 if (!test_bit(HCI_RUNNING, &hdev->flags))
4167 if (test_bit(BTUSB_INTR_RUNNING, &data->flags)) {
4168 err = btusb_submit_intr_urb(hdev, GFP_NOIO);
4170 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
4175 if (test_bit(BTUSB_BULK_RUNNING, &data->flags)) {
4176 err = btusb_submit_bulk_urb(hdev, GFP_NOIO);
4178 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
4182 btusb_submit_bulk_urb(hdev, GFP_NOIO);
4185 if (test_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
4186 if (btusb_submit_isoc_urb(hdev, GFP_NOIO) < 0)
4187 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
4189 btusb_submit_isoc_urb(hdev, GFP_NOIO);
4192 spin_lock_irq(&data->txlock);
4193 play_deferred(data);
4194 clear_bit(BTUSB_SUSPENDING, &data->flags);
4195 spin_unlock_irq(&data->txlock);
4196 schedule_work(&data->work);
4201 usb_scuttle_anchored_urbs(&data->deferred);
4203 spin_lock_irq(&data->txlock);
4204 clear_bit(BTUSB_SUSPENDING, &data->flags);
4205 spin_unlock_irq(&data->txlock);
4211 static struct usb_driver btusb_driver = {
4213 .probe = btusb_probe,
4214 .disconnect = btusb_disconnect,
4216 .suspend = btusb_suspend,
4217 .resume = btusb_resume,
4219 .id_table = btusb_table,
4220 .supports_autosuspend = 1,
4221 .disable_hub_initiated_lpm = 1,
4224 module_usb_driver(btusb_driver);
4226 module_param(disable_scofix, bool, 0644);
4227 MODULE_PARM_DESC(disable_scofix, "Disable fixup of wrong SCO buffer size");
4229 module_param(force_scofix, bool, 0644);
4230 MODULE_PARM_DESC(force_scofix, "Force fixup of wrong SCO buffers size");
4232 module_param(enable_autosuspend, bool, 0644);
4233 MODULE_PARM_DESC(enable_autosuspend, "Enable USB autosuspend by default");
4235 module_param(reset, bool, 0644);
4236 MODULE_PARM_DESC(reset, "Send HCI reset command on initialization");
4238 MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
4239 MODULE_DESCRIPTION("Generic Bluetooth USB driver ver " VERSION);
4240 MODULE_VERSION(VERSION);
4241 MODULE_LICENSE("GPL");