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 <linux/debugfs.h>
20 #include <asm/unaligned.h>
22 #include <net/bluetooth/bluetooth.h>
23 #include <net/bluetooth/hci_core.h>
32 static bool disable_scofix;
33 static bool force_scofix;
34 static bool enable_autosuspend = IS_ENABLED(CONFIG_BT_HCIBTUSB_AUTOSUSPEND);
35 static bool reset = true;
37 static struct usb_driver btusb_driver;
39 #define BTUSB_IGNORE BIT(0)
40 #define BTUSB_DIGIANSWER BIT(1)
41 #define BTUSB_CSR BIT(2)
42 #define BTUSB_SNIFFER BIT(3)
43 #define BTUSB_BCM92035 BIT(4)
44 #define BTUSB_BROKEN_ISOC BIT(5)
45 #define BTUSB_WRONG_SCO_MTU BIT(6)
46 #define BTUSB_ATH3012 BIT(7)
47 #define BTUSB_INTEL_COMBINED BIT(8)
48 #define BTUSB_INTEL_BOOT BIT(9)
49 #define BTUSB_BCM_PATCHRAM BIT(10)
50 #define BTUSB_MARVELL BIT(11)
51 #define BTUSB_SWAVE BIT(12)
52 #define BTUSB_AMP BIT(13)
53 #define BTUSB_QCA_ROME BIT(14)
54 #define BTUSB_BCM_APPLE BIT(15)
55 #define BTUSB_REALTEK BIT(16)
56 #define BTUSB_BCM2045 BIT(17)
57 #define BTUSB_IFNUM_2 BIT(18)
58 #define BTUSB_CW6622 BIT(19)
59 #define BTUSB_MEDIATEK BIT(20)
60 #define BTUSB_WIDEBAND_SPEECH BIT(21)
61 #define BTUSB_VALID_LE_STATES BIT(22)
62 #define BTUSB_QCA_WCN6855 BIT(23)
63 #define BTUSB_INTEL_BROKEN_SHUTDOWN_LED BIT(24)
64 #define BTUSB_INTEL_BROKEN_INITIAL_NCMD BIT(25)
65 #define BTUSB_INTEL_NO_WBS_SUPPORT BIT(26)
67 static const struct usb_device_id btusb_table[] = {
68 /* Generic Bluetooth USB device */
69 { USB_DEVICE_INFO(0xe0, 0x01, 0x01) },
71 /* Generic Bluetooth AMP device */
72 { USB_DEVICE_INFO(0xe0, 0x01, 0x04), .driver_info = BTUSB_AMP },
74 /* Generic Bluetooth USB interface */
75 { USB_INTERFACE_INFO(0xe0, 0x01, 0x01) },
77 /* Apple-specific (Broadcom) devices */
78 { USB_VENDOR_AND_INTERFACE_INFO(0x05ac, 0xff, 0x01, 0x01),
79 .driver_info = BTUSB_BCM_APPLE | BTUSB_IFNUM_2 },
81 /* MediaTek MT76x0E */
82 { USB_DEVICE(0x0e8d, 0x763f) },
84 /* Broadcom SoftSailing reporting vendor specific */
85 { USB_DEVICE(0x0a5c, 0x21e1) },
87 /* Apple MacBookPro 7,1 */
88 { USB_DEVICE(0x05ac, 0x8213) },
91 { USB_DEVICE(0x05ac, 0x8215) },
93 /* Apple MacBookPro6,2 */
94 { USB_DEVICE(0x05ac, 0x8218) },
96 /* Apple MacBookAir3,1, MacBookAir3,2 */
97 { USB_DEVICE(0x05ac, 0x821b) },
99 /* Apple MacBookAir4,1 */
100 { USB_DEVICE(0x05ac, 0x821f) },
102 /* Apple MacBookPro8,2 */
103 { USB_DEVICE(0x05ac, 0x821a) },
105 /* Apple MacMini5,1 */
106 { USB_DEVICE(0x05ac, 0x8281) },
108 /* AVM BlueFRITZ! USB v2.0 */
109 { USB_DEVICE(0x057c, 0x3800), .driver_info = BTUSB_SWAVE },
111 /* Bluetooth Ultraport Module from IBM */
112 { USB_DEVICE(0x04bf, 0x030a) },
114 /* ALPS Modules with non-standard id */
115 { USB_DEVICE(0x044e, 0x3001) },
116 { USB_DEVICE(0x044e, 0x3002) },
118 /* Ericsson with non-standard id */
119 { USB_DEVICE(0x0bdb, 0x1002) },
121 /* Canyon CN-BTU1 with HID interfaces */
122 { USB_DEVICE(0x0c10, 0x0000) },
124 /* Broadcom BCM20702B0 (Dynex/Insignia) */
125 { USB_DEVICE(0x19ff, 0x0239), .driver_info = BTUSB_BCM_PATCHRAM },
127 /* Broadcom BCM43142A0 (Foxconn/Lenovo) */
128 { USB_VENDOR_AND_INTERFACE_INFO(0x105b, 0xff, 0x01, 0x01),
129 .driver_info = BTUSB_BCM_PATCHRAM },
131 /* Broadcom BCM920703 (HTC Vive) */
132 { USB_VENDOR_AND_INTERFACE_INFO(0x0bb4, 0xff, 0x01, 0x01),
133 .driver_info = BTUSB_BCM_PATCHRAM },
135 /* Foxconn - Hon Hai */
136 { USB_VENDOR_AND_INTERFACE_INFO(0x0489, 0xff, 0x01, 0x01),
137 .driver_info = BTUSB_BCM_PATCHRAM },
139 /* Lite-On Technology - Broadcom based */
140 { USB_VENDOR_AND_INTERFACE_INFO(0x04ca, 0xff, 0x01, 0x01),
141 .driver_info = BTUSB_BCM_PATCHRAM },
143 /* Broadcom devices with vendor specific id */
144 { USB_VENDOR_AND_INTERFACE_INFO(0x0a5c, 0xff, 0x01, 0x01),
145 .driver_info = BTUSB_BCM_PATCHRAM },
147 /* ASUSTek Computer - Broadcom based */
148 { USB_VENDOR_AND_INTERFACE_INFO(0x0b05, 0xff, 0x01, 0x01),
149 .driver_info = BTUSB_BCM_PATCHRAM },
151 /* Belkin F8065bf - Broadcom based */
152 { USB_VENDOR_AND_INTERFACE_INFO(0x050d, 0xff, 0x01, 0x01),
153 .driver_info = BTUSB_BCM_PATCHRAM },
155 /* IMC Networks - Broadcom based */
156 { USB_VENDOR_AND_INTERFACE_INFO(0x13d3, 0xff, 0x01, 0x01),
157 .driver_info = BTUSB_BCM_PATCHRAM },
159 /* Dell Computer - Broadcom based */
160 { USB_VENDOR_AND_INTERFACE_INFO(0x413c, 0xff, 0x01, 0x01),
161 .driver_info = BTUSB_BCM_PATCHRAM },
163 /* Toshiba Corp - Broadcom based */
164 { USB_VENDOR_AND_INTERFACE_INFO(0x0930, 0xff, 0x01, 0x01),
165 .driver_info = BTUSB_BCM_PATCHRAM },
167 /* Intel Bluetooth USB Bootloader (RAM module) */
168 { USB_DEVICE(0x8087, 0x0a5a),
169 .driver_info = BTUSB_INTEL_BOOT | BTUSB_BROKEN_ISOC },
171 { } /* Terminating entry */
174 MODULE_DEVICE_TABLE(usb, btusb_table);
176 static const struct usb_device_id blacklist_table[] = {
177 /* CSR BlueCore devices */
178 { USB_DEVICE(0x0a12, 0x0001), .driver_info = BTUSB_CSR },
180 /* Broadcom BCM2033 without firmware */
181 { USB_DEVICE(0x0a5c, 0x2033), .driver_info = BTUSB_IGNORE },
183 /* Broadcom BCM2045 devices */
184 { USB_DEVICE(0x0a5c, 0x2045), .driver_info = BTUSB_BCM2045 },
186 /* Atheros 3011 with sflash firmware */
187 { USB_DEVICE(0x0489, 0xe027), .driver_info = BTUSB_IGNORE },
188 { USB_DEVICE(0x0489, 0xe03d), .driver_info = BTUSB_IGNORE },
189 { USB_DEVICE(0x04f2, 0xaff1), .driver_info = BTUSB_IGNORE },
190 { USB_DEVICE(0x0930, 0x0215), .driver_info = BTUSB_IGNORE },
191 { USB_DEVICE(0x0cf3, 0x3002), .driver_info = BTUSB_IGNORE },
192 { USB_DEVICE(0x0cf3, 0xe019), .driver_info = BTUSB_IGNORE },
193 { USB_DEVICE(0x13d3, 0x3304), .driver_info = BTUSB_IGNORE },
195 /* Atheros AR9285 Malbec with sflash firmware */
196 { USB_DEVICE(0x03f0, 0x311d), .driver_info = BTUSB_IGNORE },
198 /* Atheros 3012 with sflash firmware */
199 { USB_DEVICE(0x0489, 0xe04d), .driver_info = BTUSB_ATH3012 },
200 { USB_DEVICE(0x0489, 0xe04e), .driver_info = BTUSB_ATH3012 },
201 { USB_DEVICE(0x0489, 0xe056), .driver_info = BTUSB_ATH3012 },
202 { USB_DEVICE(0x0489, 0xe057), .driver_info = BTUSB_ATH3012 },
203 { USB_DEVICE(0x0489, 0xe05f), .driver_info = BTUSB_ATH3012 },
204 { USB_DEVICE(0x0489, 0xe076), .driver_info = BTUSB_ATH3012 },
205 { USB_DEVICE(0x0489, 0xe078), .driver_info = BTUSB_ATH3012 },
206 { USB_DEVICE(0x0489, 0xe095), .driver_info = BTUSB_ATH3012 },
207 { USB_DEVICE(0x04c5, 0x1330), .driver_info = BTUSB_ATH3012 },
208 { USB_DEVICE(0x04ca, 0x3004), .driver_info = BTUSB_ATH3012 },
209 { USB_DEVICE(0x04ca, 0x3005), .driver_info = BTUSB_ATH3012 },
210 { USB_DEVICE(0x04ca, 0x3006), .driver_info = BTUSB_ATH3012 },
211 { USB_DEVICE(0x04ca, 0x3007), .driver_info = BTUSB_ATH3012 },
212 { USB_DEVICE(0x04ca, 0x3008), .driver_info = BTUSB_ATH3012 },
213 { USB_DEVICE(0x04ca, 0x300b), .driver_info = BTUSB_ATH3012 },
214 { USB_DEVICE(0x04ca, 0x300d), .driver_info = BTUSB_ATH3012 },
215 { USB_DEVICE(0x04ca, 0x300f), .driver_info = BTUSB_ATH3012 },
216 { USB_DEVICE(0x04ca, 0x3010), .driver_info = BTUSB_ATH3012 },
217 { USB_DEVICE(0x04ca, 0x3014), .driver_info = BTUSB_ATH3012 },
218 { USB_DEVICE(0x04ca, 0x3018), .driver_info = BTUSB_ATH3012 },
219 { USB_DEVICE(0x0930, 0x0219), .driver_info = BTUSB_ATH3012 },
220 { USB_DEVICE(0x0930, 0x021c), .driver_info = BTUSB_ATH3012 },
221 { USB_DEVICE(0x0930, 0x0220), .driver_info = BTUSB_ATH3012 },
222 { USB_DEVICE(0x0930, 0x0227), .driver_info = BTUSB_ATH3012 },
223 { USB_DEVICE(0x0b05, 0x17d0), .driver_info = BTUSB_ATH3012 },
224 { USB_DEVICE(0x0cf3, 0x0036), .driver_info = BTUSB_ATH3012 },
225 { USB_DEVICE(0x0cf3, 0x3004), .driver_info = BTUSB_ATH3012 },
226 { USB_DEVICE(0x0cf3, 0x3008), .driver_info = BTUSB_ATH3012 },
227 { USB_DEVICE(0x0cf3, 0x311d), .driver_info = BTUSB_ATH3012 },
228 { USB_DEVICE(0x0cf3, 0x311e), .driver_info = BTUSB_ATH3012 },
229 { USB_DEVICE(0x0cf3, 0x311f), .driver_info = BTUSB_ATH3012 },
230 { USB_DEVICE(0x0cf3, 0x3121), .driver_info = BTUSB_ATH3012 },
231 { USB_DEVICE(0x0cf3, 0x817a), .driver_info = BTUSB_ATH3012 },
232 { USB_DEVICE(0x0cf3, 0x817b), .driver_info = BTUSB_ATH3012 },
233 { USB_DEVICE(0x0cf3, 0xe003), .driver_info = BTUSB_ATH3012 },
234 { USB_DEVICE(0x0cf3, 0xe004), .driver_info = BTUSB_ATH3012 },
235 { USB_DEVICE(0x0cf3, 0xe005), .driver_info = BTUSB_ATH3012 },
236 { USB_DEVICE(0x0cf3, 0xe006), .driver_info = BTUSB_ATH3012 },
237 { USB_DEVICE(0x13d3, 0x3362), .driver_info = BTUSB_ATH3012 },
238 { USB_DEVICE(0x13d3, 0x3375), .driver_info = BTUSB_ATH3012 },
239 { USB_DEVICE(0x13d3, 0x3393), .driver_info = BTUSB_ATH3012 },
240 { USB_DEVICE(0x13d3, 0x3395), .driver_info = BTUSB_ATH3012 },
241 { USB_DEVICE(0x13d3, 0x3402), .driver_info = BTUSB_ATH3012 },
242 { USB_DEVICE(0x13d3, 0x3408), .driver_info = BTUSB_ATH3012 },
243 { USB_DEVICE(0x13d3, 0x3423), .driver_info = BTUSB_ATH3012 },
244 { USB_DEVICE(0x13d3, 0x3432), .driver_info = BTUSB_ATH3012 },
245 { USB_DEVICE(0x13d3, 0x3472), .driver_info = BTUSB_ATH3012 },
246 { USB_DEVICE(0x13d3, 0x3474), .driver_info = BTUSB_ATH3012 },
247 { USB_DEVICE(0x13d3, 0x3487), .driver_info = BTUSB_ATH3012 },
248 { USB_DEVICE(0x13d3, 0x3490), .driver_info = BTUSB_ATH3012 },
250 /* Atheros AR5BBU12 with sflash firmware */
251 { USB_DEVICE(0x0489, 0xe02c), .driver_info = BTUSB_IGNORE },
253 /* Atheros AR5BBU12 with sflash firmware */
254 { USB_DEVICE(0x0489, 0xe036), .driver_info = BTUSB_ATH3012 },
255 { USB_DEVICE(0x0489, 0xe03c), .driver_info = BTUSB_ATH3012 },
257 /* QCA ROME chipset */
258 { USB_DEVICE(0x0cf3, 0x535b), .driver_info = BTUSB_QCA_ROME |
259 BTUSB_WIDEBAND_SPEECH },
260 { USB_DEVICE(0x0cf3, 0xe007), .driver_info = BTUSB_QCA_ROME |
261 BTUSB_WIDEBAND_SPEECH },
262 { USB_DEVICE(0x0cf3, 0xe009), .driver_info = BTUSB_QCA_ROME |
263 BTUSB_WIDEBAND_SPEECH },
264 { USB_DEVICE(0x0cf3, 0xe010), .driver_info = BTUSB_QCA_ROME |
265 BTUSB_WIDEBAND_SPEECH },
266 { USB_DEVICE(0x0cf3, 0xe300), .driver_info = BTUSB_QCA_ROME |
267 BTUSB_WIDEBAND_SPEECH },
268 { USB_DEVICE(0x0cf3, 0xe301), .driver_info = BTUSB_QCA_ROME |
269 BTUSB_WIDEBAND_SPEECH },
270 { USB_DEVICE(0x0cf3, 0xe360), .driver_info = BTUSB_QCA_ROME |
271 BTUSB_WIDEBAND_SPEECH },
272 { USB_DEVICE(0x0cf3, 0xe500), .driver_info = BTUSB_QCA_ROME |
273 BTUSB_WIDEBAND_SPEECH },
274 { USB_DEVICE(0x0489, 0xe092), .driver_info = BTUSB_QCA_ROME |
275 BTUSB_WIDEBAND_SPEECH },
276 { USB_DEVICE(0x0489, 0xe09f), .driver_info = BTUSB_QCA_ROME |
277 BTUSB_WIDEBAND_SPEECH },
278 { USB_DEVICE(0x0489, 0xe0a2), .driver_info = BTUSB_QCA_ROME |
279 BTUSB_WIDEBAND_SPEECH },
280 { USB_DEVICE(0x04ca, 0x3011), .driver_info = BTUSB_QCA_ROME |
281 BTUSB_WIDEBAND_SPEECH },
282 { USB_DEVICE(0x04ca, 0x3015), .driver_info = BTUSB_QCA_ROME |
283 BTUSB_WIDEBAND_SPEECH },
284 { USB_DEVICE(0x04ca, 0x3016), .driver_info = BTUSB_QCA_ROME |
285 BTUSB_WIDEBAND_SPEECH },
286 { USB_DEVICE(0x04ca, 0x301a), .driver_info = BTUSB_QCA_ROME |
287 BTUSB_WIDEBAND_SPEECH },
288 { USB_DEVICE(0x04ca, 0x3021), .driver_info = BTUSB_QCA_ROME |
289 BTUSB_WIDEBAND_SPEECH },
290 { USB_DEVICE(0x13d3, 0x3491), .driver_info = BTUSB_QCA_ROME |
291 BTUSB_WIDEBAND_SPEECH },
292 { USB_DEVICE(0x13d3, 0x3496), .driver_info = BTUSB_QCA_ROME |
293 BTUSB_WIDEBAND_SPEECH },
294 { USB_DEVICE(0x13d3, 0x3501), .driver_info = BTUSB_QCA_ROME |
295 BTUSB_WIDEBAND_SPEECH },
297 /* QCA WCN6855 chipset */
298 { USB_DEVICE(0x0cf3, 0xe600), .driver_info = BTUSB_QCA_WCN6855 |
299 BTUSB_WIDEBAND_SPEECH |
300 BTUSB_VALID_LE_STATES },
301 { USB_DEVICE(0x0489, 0xe0cc), .driver_info = BTUSB_QCA_WCN6855 |
302 BTUSB_WIDEBAND_SPEECH |
303 BTUSB_VALID_LE_STATES },
304 { USB_DEVICE(0x0489, 0xe0d6), .driver_info = BTUSB_QCA_WCN6855 |
305 BTUSB_WIDEBAND_SPEECH |
306 BTUSB_VALID_LE_STATES },
307 { USB_DEVICE(0x0489, 0xe0e3), .driver_info = BTUSB_QCA_WCN6855 |
308 BTUSB_WIDEBAND_SPEECH |
309 BTUSB_VALID_LE_STATES },
310 { USB_DEVICE(0x10ab, 0x9309), .driver_info = BTUSB_QCA_WCN6855 |
311 BTUSB_WIDEBAND_SPEECH |
312 BTUSB_VALID_LE_STATES },
313 { USB_DEVICE(0x10ab, 0x9409), .driver_info = BTUSB_QCA_WCN6855 |
314 BTUSB_WIDEBAND_SPEECH |
315 BTUSB_VALID_LE_STATES },
316 { USB_DEVICE(0x0489, 0xe0d0), .driver_info = BTUSB_QCA_WCN6855 |
317 BTUSB_WIDEBAND_SPEECH |
318 BTUSB_VALID_LE_STATES },
320 /* QCA WCN785x chipset */
321 { USB_DEVICE(0x0cf3, 0xe700), .driver_info = BTUSB_QCA_WCN6855 |
322 BTUSB_WIDEBAND_SPEECH |
323 BTUSB_VALID_LE_STATES },
325 /* Broadcom BCM2035 */
326 { USB_DEVICE(0x0a5c, 0x2009), .driver_info = BTUSB_BCM92035 },
327 { USB_DEVICE(0x0a5c, 0x200a), .driver_info = BTUSB_WRONG_SCO_MTU },
328 { USB_DEVICE(0x0a5c, 0x2035), .driver_info = BTUSB_WRONG_SCO_MTU },
330 /* Broadcom BCM2045 */
331 { USB_DEVICE(0x0a5c, 0x2039), .driver_info = BTUSB_WRONG_SCO_MTU },
332 { USB_DEVICE(0x0a5c, 0x2101), .driver_info = BTUSB_WRONG_SCO_MTU },
334 /* IBM/Lenovo ThinkPad with Broadcom chip */
335 { USB_DEVICE(0x0a5c, 0x201e), .driver_info = BTUSB_WRONG_SCO_MTU },
336 { USB_DEVICE(0x0a5c, 0x2110), .driver_info = BTUSB_WRONG_SCO_MTU },
338 /* HP laptop with Broadcom chip */
339 { USB_DEVICE(0x03f0, 0x171d), .driver_info = BTUSB_WRONG_SCO_MTU },
341 /* Dell laptop with Broadcom chip */
342 { USB_DEVICE(0x413c, 0x8126), .driver_info = BTUSB_WRONG_SCO_MTU },
344 /* Dell Wireless 370 and 410 devices */
345 { USB_DEVICE(0x413c, 0x8152), .driver_info = BTUSB_WRONG_SCO_MTU },
346 { USB_DEVICE(0x413c, 0x8156), .driver_info = BTUSB_WRONG_SCO_MTU },
348 /* Belkin F8T012 and F8T013 devices */
349 { USB_DEVICE(0x050d, 0x0012), .driver_info = BTUSB_WRONG_SCO_MTU },
350 { USB_DEVICE(0x050d, 0x0013), .driver_info = BTUSB_WRONG_SCO_MTU },
352 /* Asus WL-BTD202 device */
353 { USB_DEVICE(0x0b05, 0x1715), .driver_info = BTUSB_WRONG_SCO_MTU },
355 /* Kensington Bluetooth USB adapter */
356 { USB_DEVICE(0x047d, 0x105e), .driver_info = BTUSB_WRONG_SCO_MTU },
358 /* RTX Telecom based adapters with buggy SCO support */
359 { USB_DEVICE(0x0400, 0x0807), .driver_info = BTUSB_BROKEN_ISOC },
360 { USB_DEVICE(0x0400, 0x080a), .driver_info = BTUSB_BROKEN_ISOC },
362 /* CONWISE Technology based adapters with buggy SCO support */
363 { USB_DEVICE(0x0e5e, 0x6622),
364 .driver_info = BTUSB_BROKEN_ISOC | BTUSB_CW6622},
366 /* Roper Class 1 Bluetooth Dongle (Silicon Wave based) */
367 { USB_DEVICE(0x1310, 0x0001), .driver_info = BTUSB_SWAVE },
369 /* Digianswer devices */
370 { USB_DEVICE(0x08fd, 0x0001), .driver_info = BTUSB_DIGIANSWER },
371 { USB_DEVICE(0x08fd, 0x0002), .driver_info = BTUSB_IGNORE },
373 /* CSR BlueCore Bluetooth Sniffer */
374 { USB_DEVICE(0x0a12, 0x0002),
375 .driver_info = BTUSB_SNIFFER | BTUSB_BROKEN_ISOC },
377 /* Frontline ComProbe Bluetooth Sniffer */
378 { USB_DEVICE(0x16d3, 0x0002),
379 .driver_info = BTUSB_SNIFFER | BTUSB_BROKEN_ISOC },
381 /* Marvell Bluetooth devices */
382 { USB_DEVICE(0x1286, 0x2044), .driver_info = BTUSB_MARVELL },
383 { USB_DEVICE(0x1286, 0x2046), .driver_info = BTUSB_MARVELL },
384 { USB_DEVICE(0x1286, 0x204e), .driver_info = BTUSB_MARVELL },
386 /* Intel Bluetooth devices */
387 { USB_DEVICE(0x8087, 0x0025), .driver_info = BTUSB_INTEL_COMBINED },
388 { USB_DEVICE(0x8087, 0x0026), .driver_info = BTUSB_INTEL_COMBINED },
389 { USB_DEVICE(0x8087, 0x0029), .driver_info = BTUSB_INTEL_COMBINED },
390 { USB_DEVICE(0x8087, 0x0032), .driver_info = BTUSB_INTEL_COMBINED },
391 { USB_DEVICE(0x8087, 0x0033), .driver_info = BTUSB_INTEL_COMBINED },
392 { USB_DEVICE(0x8087, 0x0035), .driver_info = BTUSB_INTEL_COMBINED },
393 { USB_DEVICE(0x8087, 0x07da), .driver_info = BTUSB_CSR },
394 { USB_DEVICE(0x8087, 0x07dc), .driver_info = BTUSB_INTEL_COMBINED |
395 BTUSB_INTEL_NO_WBS_SUPPORT |
396 BTUSB_INTEL_BROKEN_INITIAL_NCMD |
397 BTUSB_INTEL_BROKEN_SHUTDOWN_LED },
398 { USB_DEVICE(0x8087, 0x0a2a), .driver_info = BTUSB_INTEL_COMBINED |
399 BTUSB_INTEL_NO_WBS_SUPPORT |
400 BTUSB_INTEL_BROKEN_SHUTDOWN_LED },
401 { USB_DEVICE(0x8087, 0x0a2b), .driver_info = BTUSB_INTEL_COMBINED },
402 { USB_DEVICE(0x8087, 0x0aa7), .driver_info = BTUSB_INTEL_COMBINED |
403 BTUSB_INTEL_BROKEN_SHUTDOWN_LED },
404 { USB_DEVICE(0x8087, 0x0aaa), .driver_info = BTUSB_INTEL_COMBINED },
406 /* Other Intel Bluetooth devices */
407 { USB_VENDOR_AND_INTERFACE_INFO(0x8087, 0xe0, 0x01, 0x01),
408 .driver_info = BTUSB_IGNORE },
410 /* Realtek 8822CE Bluetooth devices */
411 { USB_DEVICE(0x0bda, 0xb00c), .driver_info = BTUSB_REALTEK |
412 BTUSB_WIDEBAND_SPEECH },
413 { USB_DEVICE(0x0bda, 0xc822), .driver_info = BTUSB_REALTEK |
414 BTUSB_WIDEBAND_SPEECH },
416 /* Realtek 8852AE Bluetooth devices */
417 { USB_DEVICE(0x0bda, 0x2852), .driver_info = BTUSB_REALTEK |
418 BTUSB_WIDEBAND_SPEECH },
419 { USB_DEVICE(0x0bda, 0xc852), .driver_info = BTUSB_REALTEK |
420 BTUSB_WIDEBAND_SPEECH },
421 { USB_DEVICE(0x0bda, 0x385a), .driver_info = BTUSB_REALTEK |
422 BTUSB_WIDEBAND_SPEECH },
423 { USB_DEVICE(0x0bda, 0x4852), .driver_info = BTUSB_REALTEK |
424 BTUSB_WIDEBAND_SPEECH },
425 { USB_DEVICE(0x04c5, 0x165c), .driver_info = BTUSB_REALTEK |
426 BTUSB_WIDEBAND_SPEECH },
427 { USB_DEVICE(0x04ca, 0x4006), .driver_info = BTUSB_REALTEK |
428 BTUSB_WIDEBAND_SPEECH },
430 /* Realtek Bluetooth devices */
431 { USB_VENDOR_AND_INTERFACE_INFO(0x0bda, 0xe0, 0x01, 0x01),
432 .driver_info = BTUSB_REALTEK },
434 /* MediaTek Bluetooth devices */
435 { USB_VENDOR_AND_INTERFACE_INFO(0x0e8d, 0xe0, 0x01, 0x01),
436 .driver_info = BTUSB_MEDIATEK |
437 BTUSB_WIDEBAND_SPEECH |
438 BTUSB_VALID_LE_STATES },
440 /* Additional MediaTek MT7615E Bluetooth devices */
441 { USB_DEVICE(0x13d3, 0x3560), .driver_info = BTUSB_MEDIATEK},
443 /* Additional MediaTek MT7663 Bluetooth devices */
444 { USB_DEVICE(0x043e, 0x310c), .driver_info = BTUSB_MEDIATEK |
445 BTUSB_WIDEBAND_SPEECH |
446 BTUSB_VALID_LE_STATES },
448 /* Additional MediaTek MT7668 Bluetooth devices */
449 { USB_DEVICE(0x043e, 0x3109), .driver_info = BTUSB_MEDIATEK |
450 BTUSB_WIDEBAND_SPEECH |
451 BTUSB_VALID_LE_STATES },
453 /* Additional MediaTek MT7921 Bluetooth devices */
454 { USB_DEVICE(0x0489, 0xe0c8), .driver_info = BTUSB_MEDIATEK |
455 BTUSB_WIDEBAND_SPEECH |
456 BTUSB_VALID_LE_STATES },
457 { USB_DEVICE(0x04ca, 0x3802), .driver_info = BTUSB_MEDIATEK |
458 BTUSB_WIDEBAND_SPEECH |
459 BTUSB_VALID_LE_STATES },
460 { USB_DEVICE(0x13d3, 0x3563), .driver_info = BTUSB_MEDIATEK |
461 BTUSB_WIDEBAND_SPEECH |
462 BTUSB_VALID_LE_STATES },
463 { USB_DEVICE(0x13d3, 0x3564), .driver_info = BTUSB_MEDIATEK |
464 BTUSB_WIDEBAND_SPEECH |
465 BTUSB_VALID_LE_STATES },
466 { USB_DEVICE(0x13d3, 0x3567), .driver_info = BTUSB_MEDIATEK |
467 BTUSB_WIDEBAND_SPEECH |
468 BTUSB_VALID_LE_STATES },
469 { USB_DEVICE(0x0489, 0xe0cd), .driver_info = BTUSB_MEDIATEK |
470 BTUSB_WIDEBAND_SPEECH |
471 BTUSB_VALID_LE_STATES },
473 /* MediaTek MT7922A Bluetooth devices */
474 { USB_DEVICE(0x0489, 0xe0d8), .driver_info = BTUSB_MEDIATEK |
475 BTUSB_WIDEBAND_SPEECH |
476 BTUSB_VALID_LE_STATES },
477 { USB_DEVICE(0x0489, 0xe0d9), .driver_info = BTUSB_MEDIATEK |
478 BTUSB_WIDEBAND_SPEECH |
479 BTUSB_VALID_LE_STATES },
481 /* Additional Realtek 8723AE Bluetooth devices */
482 { USB_DEVICE(0x0930, 0x021d), .driver_info = BTUSB_REALTEK },
483 { USB_DEVICE(0x13d3, 0x3394), .driver_info = BTUSB_REALTEK },
485 /* Additional Realtek 8723BE Bluetooth devices */
486 { USB_DEVICE(0x0489, 0xe085), .driver_info = BTUSB_REALTEK },
487 { USB_DEVICE(0x0489, 0xe08b), .driver_info = BTUSB_REALTEK },
488 { USB_DEVICE(0x04f2, 0xb49f), .driver_info = BTUSB_REALTEK },
489 { USB_DEVICE(0x13d3, 0x3410), .driver_info = BTUSB_REALTEK },
490 { USB_DEVICE(0x13d3, 0x3416), .driver_info = BTUSB_REALTEK },
491 { USB_DEVICE(0x13d3, 0x3459), .driver_info = BTUSB_REALTEK },
492 { USB_DEVICE(0x13d3, 0x3494), .driver_info = BTUSB_REALTEK },
494 /* Additional Realtek 8723BU Bluetooth devices */
495 { USB_DEVICE(0x7392, 0xa611), .driver_info = BTUSB_REALTEK },
497 /* Additional Realtek 8723DE Bluetooth devices */
498 { USB_DEVICE(0x0bda, 0xb009), .driver_info = BTUSB_REALTEK },
499 { USB_DEVICE(0x2ff8, 0xb011), .driver_info = BTUSB_REALTEK },
501 /* Additional Realtek 8761B Bluetooth devices */
502 { USB_DEVICE(0x2357, 0x0604), .driver_info = BTUSB_REALTEK |
503 BTUSB_WIDEBAND_SPEECH },
505 /* Additional Realtek 8761BU Bluetooth devices */
506 { USB_DEVICE(0x0b05, 0x190e), .driver_info = BTUSB_REALTEK |
507 BTUSB_WIDEBAND_SPEECH },
508 { USB_DEVICE(0x2550, 0x8761), .driver_info = BTUSB_REALTEK |
509 BTUSB_WIDEBAND_SPEECH },
511 /* Additional Realtek 8761BUV Bluetooth devices */
512 { USB_DEVICE(0x0bda, 0x8771), .driver_info = BTUSB_REALTEK |
513 BTUSB_WIDEBAND_SPEECH },
515 /* Additional Realtek 8821AE Bluetooth devices */
516 { USB_DEVICE(0x0b05, 0x17dc), .driver_info = BTUSB_REALTEK },
517 { USB_DEVICE(0x13d3, 0x3414), .driver_info = BTUSB_REALTEK },
518 { USB_DEVICE(0x13d3, 0x3458), .driver_info = BTUSB_REALTEK },
519 { USB_DEVICE(0x13d3, 0x3461), .driver_info = BTUSB_REALTEK },
520 { USB_DEVICE(0x13d3, 0x3462), .driver_info = BTUSB_REALTEK },
522 /* Additional Realtek 8822BE Bluetooth devices */
523 { USB_DEVICE(0x13d3, 0x3526), .driver_info = BTUSB_REALTEK },
524 { USB_DEVICE(0x0b05, 0x185c), .driver_info = BTUSB_REALTEK },
526 /* Additional Realtek 8822CE Bluetooth devices */
527 { USB_DEVICE(0x04ca, 0x4005), .driver_info = BTUSB_REALTEK |
528 BTUSB_WIDEBAND_SPEECH },
529 { USB_DEVICE(0x04c5, 0x161f), .driver_info = BTUSB_REALTEK |
530 BTUSB_WIDEBAND_SPEECH },
531 { USB_DEVICE(0x0b05, 0x18ef), .driver_info = BTUSB_REALTEK |
532 BTUSB_WIDEBAND_SPEECH },
533 { USB_DEVICE(0x13d3, 0x3548), .driver_info = BTUSB_REALTEK |
534 BTUSB_WIDEBAND_SPEECH },
535 { USB_DEVICE(0x13d3, 0x3549), .driver_info = BTUSB_REALTEK |
536 BTUSB_WIDEBAND_SPEECH },
537 { USB_DEVICE(0x13d3, 0x3553), .driver_info = BTUSB_REALTEK |
538 BTUSB_WIDEBAND_SPEECH },
539 { USB_DEVICE(0x13d3, 0x3555), .driver_info = BTUSB_REALTEK |
540 BTUSB_WIDEBAND_SPEECH },
541 { USB_DEVICE(0x2ff8, 0x3051), .driver_info = BTUSB_REALTEK |
542 BTUSB_WIDEBAND_SPEECH },
543 { USB_DEVICE(0x1358, 0xc123), .driver_info = BTUSB_REALTEK |
544 BTUSB_WIDEBAND_SPEECH },
545 { USB_DEVICE(0x0bda, 0xc123), .driver_info = BTUSB_REALTEK |
546 BTUSB_WIDEBAND_SPEECH },
547 { USB_DEVICE(0x0cb5, 0xc547), .driver_info = BTUSB_REALTEK |
548 BTUSB_WIDEBAND_SPEECH },
550 /* Silicon Wave based devices */
551 { USB_DEVICE(0x0c10, 0x0000), .driver_info = BTUSB_SWAVE },
553 { } /* Terminating entry */
556 /* The Bluetooth USB module build into some devices needs to be reset on resume,
557 * this is a problem with the platform (likely shutting off all power) not with
558 * the module itself. So we use a DMI list to match known broken platforms.
560 static const struct dmi_system_id btusb_needs_reset_resume_table[] = {
562 /* Dell OptiPlex 3060 (QCA ROME device 0cf3:e007) */
564 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
565 DMI_MATCH(DMI_PRODUCT_NAME, "OptiPlex 3060"),
569 /* Dell XPS 9360 (QCA ROME device 0cf3:e300) */
571 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
572 DMI_MATCH(DMI_PRODUCT_NAME, "XPS 13 9360"),
576 /* Dell Inspiron 5565 (QCA ROME device 0cf3:e009) */
578 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
579 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 5565"),
585 #define BTUSB_MAX_ISOC_FRAMES 10
587 #define BTUSB_INTR_RUNNING 0
588 #define BTUSB_BULK_RUNNING 1
589 #define BTUSB_ISOC_RUNNING 2
590 #define BTUSB_SUSPENDING 3
591 #define BTUSB_DID_ISO_RESUME 4
592 #define BTUSB_BOOTLOADER 5
593 #define BTUSB_DOWNLOADING 6
594 #define BTUSB_FIRMWARE_LOADED 7
595 #define BTUSB_FIRMWARE_FAILED 8
596 #define BTUSB_BOOTING 9
597 #define BTUSB_DIAG_RUNNING 10
598 #define BTUSB_OOB_WAKE_ENABLED 11
599 #define BTUSB_HW_RESET_ACTIVE 12
600 #define BTUSB_TX_WAIT_VND_EVT 13
601 #define BTUSB_WAKEUP_AUTOSUSPEND 14
602 #define BTUSB_USE_ALT3_FOR_WBS 15
605 struct hci_dev *hdev;
606 struct usb_device *udev;
607 struct usb_interface *intf;
608 struct usb_interface *isoc;
609 struct usb_interface *diag;
616 struct work_struct work;
617 struct work_struct waker;
618 struct delayed_work rx_work;
620 struct sk_buff_head acl_q;
622 struct usb_anchor deferred;
623 struct usb_anchor tx_anchor;
627 struct usb_anchor intr_anchor;
628 struct usb_anchor bulk_anchor;
629 struct usb_anchor isoc_anchor;
630 struct usb_anchor diag_anchor;
631 struct usb_anchor ctrl_anchor;
634 struct sk_buff *evt_skb;
635 struct sk_buff *acl_skb;
636 struct sk_buff *sco_skb;
638 struct usb_endpoint_descriptor *intr_ep;
639 struct usb_endpoint_descriptor *bulk_tx_ep;
640 struct usb_endpoint_descriptor *bulk_rx_ep;
641 struct usb_endpoint_descriptor *isoc_tx_ep;
642 struct usb_endpoint_descriptor *isoc_rx_ep;
643 struct usb_endpoint_descriptor *diag_tx_ep;
644 struct usb_endpoint_descriptor *diag_rx_ep;
646 struct gpio_desc *reset_gpio;
651 unsigned int sco_num;
652 unsigned int air_mode;
653 bool usb_alt6_packet_flow;
657 int (*recv_event)(struct hci_dev *hdev, struct sk_buff *skb);
658 int (*recv_acl)(struct hci_dev *hdev, struct sk_buff *skb);
659 int (*recv_bulk)(struct btusb_data *data, void *buffer, int count);
661 int (*setup_on_usb)(struct hci_dev *hdev);
663 int oob_wake_irq; /* irq for out-of-band wake-on-bt */
664 unsigned cmd_timeout_cnt;
667 static void btusb_intel_cmd_timeout(struct hci_dev *hdev)
669 struct btusb_data *data = hci_get_drvdata(hdev);
670 struct gpio_desc *reset_gpio = data->reset_gpio;
672 if (++data->cmd_timeout_cnt < 5)
676 bt_dev_err(hdev, "No way to reset. Ignoring and continuing");
681 * Toggle the hard reset line if the platform provides one. The reset
682 * is going to yank the device off the USB and then replug. So doing
683 * once is enough. The cleanup is handled correctly on the way out
684 * (standard USB disconnect), and the new device is detected cleanly
685 * and bound to the driver again like it should be.
687 if (test_and_set_bit(BTUSB_HW_RESET_ACTIVE, &data->flags)) {
688 bt_dev_err(hdev, "last reset failed? Not resetting again");
692 bt_dev_err(hdev, "Initiating HW reset via gpio");
693 gpiod_set_value_cansleep(reset_gpio, 1);
695 gpiod_set_value_cansleep(reset_gpio, 0);
698 static void btusb_rtl_cmd_timeout(struct hci_dev *hdev)
700 struct btusb_data *data = hci_get_drvdata(hdev);
701 struct gpio_desc *reset_gpio = data->reset_gpio;
703 if (++data->cmd_timeout_cnt < 5)
707 bt_dev_err(hdev, "No gpio to reset Realtek device, ignoring");
711 /* Toggle the hard reset line. The Realtek device is going to
712 * yank itself off the USB and then replug. The cleanup is handled
713 * correctly on the way out (standard USB disconnect), and the new
714 * device is detected cleanly and bound to the driver again like
717 if (test_and_set_bit(BTUSB_HW_RESET_ACTIVE, &data->flags)) {
718 bt_dev_err(hdev, "last reset failed? Not resetting again");
722 bt_dev_err(hdev, "Reset Realtek device via gpio");
723 gpiod_set_value_cansleep(reset_gpio, 1);
725 gpiod_set_value_cansleep(reset_gpio, 0);
728 static void btusb_qca_cmd_timeout(struct hci_dev *hdev)
730 struct btusb_data *data = hci_get_drvdata(hdev);
731 struct gpio_desc *reset_gpio = data->reset_gpio;
734 if (++data->cmd_timeout_cnt < 5)
738 bt_dev_err(hdev, "Reset qca device via bt_en gpio");
740 /* Toggle the hard reset line. The qca bt device is going to
741 * yank itself off the USB and then replug. The cleanup is handled
742 * correctly on the way out (standard USB disconnect), and the new
743 * device is detected cleanly and bound to the driver again like
746 if (test_and_set_bit(BTUSB_HW_RESET_ACTIVE, &data->flags)) {
747 bt_dev_err(hdev, "last reset failed? Not resetting again");
751 gpiod_set_value_cansleep(reset_gpio, 0);
753 gpiod_set_value_cansleep(reset_gpio, 1);
758 bt_dev_err(hdev, "Multiple cmd timeouts seen. Resetting usb device.");
759 /* This is not an unbalanced PM reference since the device will reset */
760 err = usb_autopm_get_interface(data->intf);
762 usb_queue_reset_device(data->intf);
764 bt_dev_err(hdev, "Failed usb_autopm_get_interface with %d", err);
767 static inline void btusb_free_frags(struct btusb_data *data)
771 spin_lock_irqsave(&data->rxlock, flags);
773 kfree_skb(data->evt_skb);
774 data->evt_skb = NULL;
776 kfree_skb(data->acl_skb);
777 data->acl_skb = NULL;
779 kfree_skb(data->sco_skb);
780 data->sco_skb = NULL;
782 spin_unlock_irqrestore(&data->rxlock, flags);
785 static int btusb_recv_event(struct btusb_data *data, struct sk_buff *skb)
787 if (data->intr_interval) {
788 /* Trigger dequeue immediatelly if an event is received */
789 schedule_delayed_work(&data->rx_work, 0);
792 return data->recv_event(data->hdev, skb);
795 static int btusb_recv_intr(struct btusb_data *data, void *buffer, int count)
801 spin_lock_irqsave(&data->rxlock, flags);
808 skb = bt_skb_alloc(HCI_MAX_EVENT_SIZE, GFP_ATOMIC);
814 hci_skb_pkt_type(skb) = HCI_EVENT_PKT;
815 hci_skb_expect(skb) = HCI_EVENT_HDR_SIZE;
818 len = min_t(uint, hci_skb_expect(skb), count);
819 skb_put_data(skb, buffer, len);
823 hci_skb_expect(skb) -= len;
825 if (skb->len == HCI_EVENT_HDR_SIZE) {
826 /* Complete event header */
827 hci_skb_expect(skb) = hci_event_hdr(skb)->plen;
829 if (skb_tailroom(skb) < hci_skb_expect(skb)) {
838 if (!hci_skb_expect(skb)) {
840 btusb_recv_event(data, skb);
846 spin_unlock_irqrestore(&data->rxlock, flags);
851 static int btusb_recv_acl(struct btusb_data *data, struct sk_buff *skb)
853 /* Only queue ACL packet if intr_interval is set as it means
854 * force_poll_sync has been enabled.
856 if (!data->intr_interval)
857 return data->recv_acl(data->hdev, skb);
859 skb_queue_tail(&data->acl_q, skb);
860 schedule_delayed_work(&data->rx_work, data->intr_interval);
865 static int btusb_recv_bulk(struct btusb_data *data, void *buffer, int count)
871 spin_lock_irqsave(&data->rxlock, flags);
878 skb = bt_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC);
884 hci_skb_pkt_type(skb) = HCI_ACLDATA_PKT;
885 hci_skb_expect(skb) = HCI_ACL_HDR_SIZE;
888 len = min_t(uint, hci_skb_expect(skb), count);
889 skb_put_data(skb, buffer, len);
893 hci_skb_expect(skb) -= len;
895 if (skb->len == HCI_ACL_HDR_SIZE) {
896 __le16 dlen = hci_acl_hdr(skb)->dlen;
898 /* Complete ACL header */
899 hci_skb_expect(skb) = __le16_to_cpu(dlen);
901 if (skb_tailroom(skb) < hci_skb_expect(skb)) {
910 if (!hci_skb_expect(skb)) {
912 btusb_recv_acl(data, skb);
918 spin_unlock_irqrestore(&data->rxlock, flags);
923 static int btusb_recv_isoc(struct btusb_data *data, void *buffer, int count)
929 spin_lock_irqsave(&data->rxlock, flags);
936 skb = bt_skb_alloc(HCI_MAX_SCO_SIZE, GFP_ATOMIC);
942 hci_skb_pkt_type(skb) = HCI_SCODATA_PKT;
943 hci_skb_expect(skb) = HCI_SCO_HDR_SIZE;
946 len = min_t(uint, hci_skb_expect(skb), count);
947 skb_put_data(skb, buffer, len);
951 hci_skb_expect(skb) -= len;
953 if (skb->len == HCI_SCO_HDR_SIZE) {
954 /* Complete SCO header */
955 hci_skb_expect(skb) = hci_sco_hdr(skb)->dlen;
957 if (skb_tailroom(skb) < hci_skb_expect(skb)) {
966 if (!hci_skb_expect(skb)) {
968 hci_recv_frame(data->hdev, skb);
974 spin_unlock_irqrestore(&data->rxlock, flags);
979 static void btusb_intr_complete(struct urb *urb)
981 struct hci_dev *hdev = urb->context;
982 struct btusb_data *data = hci_get_drvdata(hdev);
985 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
988 if (!test_bit(HCI_RUNNING, &hdev->flags))
991 if (urb->status == 0) {
992 hdev->stat.byte_rx += urb->actual_length;
994 if (btusb_recv_intr(data, urb->transfer_buffer,
995 urb->actual_length) < 0) {
996 bt_dev_err(hdev, "corrupted event packet");
999 } else if (urb->status == -ENOENT) {
1000 /* Avoid suspend failed when usb_kill_urb */
1004 if (!test_bit(BTUSB_INTR_RUNNING, &data->flags))
1007 usb_mark_last_busy(data->udev);
1008 usb_anchor_urb(urb, &data->intr_anchor);
1010 err = usb_submit_urb(urb, GFP_ATOMIC);
1012 /* -EPERM: urb is being killed;
1013 * -ENODEV: device got disconnected
1015 if (err != -EPERM && err != -ENODEV)
1016 bt_dev_err(hdev, "urb %p failed to resubmit (%d)",
1019 hci_cmd_sync_cancel(hdev, -err);
1020 usb_unanchor_urb(urb);
1024 static int btusb_submit_intr_urb(struct hci_dev *hdev, gfp_t mem_flags)
1026 struct btusb_data *data = hci_get_drvdata(hdev);
1032 BT_DBG("%s", hdev->name);
1037 urb = usb_alloc_urb(0, mem_flags);
1041 size = le16_to_cpu(data->intr_ep->wMaxPacketSize);
1043 buf = kmalloc(size, mem_flags);
1049 pipe = usb_rcvintpipe(data->udev, data->intr_ep->bEndpointAddress);
1051 usb_fill_int_urb(urb, data->udev, pipe, buf, size,
1052 btusb_intr_complete, hdev, data->intr_ep->bInterval);
1054 urb->transfer_flags |= URB_FREE_BUFFER;
1056 usb_anchor_urb(urb, &data->intr_anchor);
1058 err = usb_submit_urb(urb, mem_flags);
1060 if (err != -EPERM && err != -ENODEV)
1061 bt_dev_err(hdev, "urb %p submission failed (%d)",
1064 hci_cmd_sync_cancel(hdev, -err);
1065 usb_unanchor_urb(urb);
1068 /* Only initialize intr_interval if URB poll sync is enabled */
1069 if (!data->poll_sync)
1072 /* The units are frames (milliseconds) for full and low speed devices,
1073 * and microframes (1/8 millisecond) for highspeed and SuperSpeed
1076 * This is done once on open/resume so it shouldn't change even if
1077 * force_poll_sync changes.
1079 switch (urb->dev->speed) {
1080 case USB_SPEED_SUPER_PLUS:
1081 case USB_SPEED_SUPER: /* units are 125us */
1082 data->intr_interval = usecs_to_jiffies(urb->interval * 125);
1085 data->intr_interval = msecs_to_jiffies(urb->interval);
1095 static void btusb_bulk_complete(struct urb *urb)
1097 struct hci_dev *hdev = urb->context;
1098 struct btusb_data *data = hci_get_drvdata(hdev);
1101 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
1102 urb->actual_length);
1104 if (!test_bit(HCI_RUNNING, &hdev->flags))
1107 if (urb->status == 0) {
1108 hdev->stat.byte_rx += urb->actual_length;
1110 if (data->recv_bulk(data, urb->transfer_buffer,
1111 urb->actual_length) < 0) {
1112 bt_dev_err(hdev, "corrupted ACL packet");
1113 hdev->stat.err_rx++;
1115 } else if (urb->status == -ENOENT) {
1116 /* Avoid suspend failed when usb_kill_urb */
1120 if (!test_bit(BTUSB_BULK_RUNNING, &data->flags))
1123 usb_anchor_urb(urb, &data->bulk_anchor);
1124 usb_mark_last_busy(data->udev);
1126 err = usb_submit_urb(urb, GFP_ATOMIC);
1128 /* -EPERM: urb is being killed;
1129 * -ENODEV: device got disconnected
1131 if (err != -EPERM && err != -ENODEV)
1132 bt_dev_err(hdev, "urb %p failed to resubmit (%d)",
1134 usb_unanchor_urb(urb);
1138 static int btusb_submit_bulk_urb(struct hci_dev *hdev, gfp_t mem_flags)
1140 struct btusb_data *data = hci_get_drvdata(hdev);
1144 int err, size = HCI_MAX_FRAME_SIZE;
1146 BT_DBG("%s", hdev->name);
1148 if (!data->bulk_rx_ep)
1151 urb = usb_alloc_urb(0, mem_flags);
1155 buf = kmalloc(size, mem_flags);
1161 pipe = usb_rcvbulkpipe(data->udev, data->bulk_rx_ep->bEndpointAddress);
1163 usb_fill_bulk_urb(urb, data->udev, pipe, buf, size,
1164 btusb_bulk_complete, hdev);
1166 urb->transfer_flags |= URB_FREE_BUFFER;
1168 usb_mark_last_busy(data->udev);
1169 usb_anchor_urb(urb, &data->bulk_anchor);
1171 err = usb_submit_urb(urb, mem_flags);
1173 if (err != -EPERM && err != -ENODEV)
1174 bt_dev_err(hdev, "urb %p submission failed (%d)",
1176 usb_unanchor_urb(urb);
1184 static void btusb_isoc_complete(struct urb *urb)
1186 struct hci_dev *hdev = urb->context;
1187 struct btusb_data *data = hci_get_drvdata(hdev);
1190 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
1191 urb->actual_length);
1193 if (!test_bit(HCI_RUNNING, &hdev->flags))
1196 if (urb->status == 0) {
1197 for (i = 0; i < urb->number_of_packets; i++) {
1198 unsigned int offset = urb->iso_frame_desc[i].offset;
1199 unsigned int length = urb->iso_frame_desc[i].actual_length;
1201 if (urb->iso_frame_desc[i].status)
1204 hdev->stat.byte_rx += length;
1206 if (btusb_recv_isoc(data, urb->transfer_buffer + offset,
1208 bt_dev_err(hdev, "corrupted SCO packet");
1209 hdev->stat.err_rx++;
1212 } else if (urb->status == -ENOENT) {
1213 /* Avoid suspend failed when usb_kill_urb */
1217 if (!test_bit(BTUSB_ISOC_RUNNING, &data->flags))
1220 usb_anchor_urb(urb, &data->isoc_anchor);
1222 err = usb_submit_urb(urb, GFP_ATOMIC);
1224 /* -EPERM: urb is being killed;
1225 * -ENODEV: device got disconnected
1227 if (err != -EPERM && err != -ENODEV)
1228 bt_dev_err(hdev, "urb %p failed to resubmit (%d)",
1230 usb_unanchor_urb(urb);
1234 static inline void __fill_isoc_descriptor_msbc(struct urb *urb, int len,
1235 int mtu, struct btusb_data *data)
1238 unsigned int interval;
1240 BT_DBG("len %d mtu %d", len, mtu);
1242 /* For mSBC ALT 6 setting the host will send the packet at continuous
1243 * flow. As per core spec 5, vol 4, part B, table 2.1. For ALT setting
1244 * 6 the HCI PACKET INTERVAL should be 7.5ms for every usb packets.
1245 * To maintain the rate we send 63bytes of usb packets alternatively for
1246 * 7ms and 8ms to maintain the rate as 7.5ms.
1248 if (data->usb_alt6_packet_flow) {
1250 data->usb_alt6_packet_flow = false;
1253 data->usb_alt6_packet_flow = true;
1256 for (i = 0; i < interval; i++) {
1257 urb->iso_frame_desc[i].offset = offset;
1258 urb->iso_frame_desc[i].length = offset;
1261 if (len && i < BTUSB_MAX_ISOC_FRAMES) {
1262 urb->iso_frame_desc[i].offset = offset;
1263 urb->iso_frame_desc[i].length = len;
1267 urb->number_of_packets = i;
1270 static inline void __fill_isoc_descriptor(struct urb *urb, int len, int mtu)
1274 BT_DBG("len %d mtu %d", len, mtu);
1276 for (i = 0; i < BTUSB_MAX_ISOC_FRAMES && len >= mtu;
1277 i++, offset += mtu, len -= mtu) {
1278 urb->iso_frame_desc[i].offset = offset;
1279 urb->iso_frame_desc[i].length = mtu;
1282 if (len && i < BTUSB_MAX_ISOC_FRAMES) {
1283 urb->iso_frame_desc[i].offset = offset;
1284 urb->iso_frame_desc[i].length = len;
1288 urb->number_of_packets = i;
1291 static int btusb_submit_isoc_urb(struct hci_dev *hdev, gfp_t mem_flags)
1293 struct btusb_data *data = hci_get_drvdata(hdev);
1299 BT_DBG("%s", hdev->name);
1301 if (!data->isoc_rx_ep)
1304 urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, mem_flags);
1308 size = le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize) *
1309 BTUSB_MAX_ISOC_FRAMES;
1311 buf = kmalloc(size, mem_flags);
1317 pipe = usb_rcvisocpipe(data->udev, data->isoc_rx_ep->bEndpointAddress);
1319 usb_fill_int_urb(urb, data->udev, pipe, buf, size, btusb_isoc_complete,
1320 hdev, data->isoc_rx_ep->bInterval);
1322 urb->transfer_flags = URB_FREE_BUFFER | URB_ISO_ASAP;
1324 __fill_isoc_descriptor(urb, size,
1325 le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize));
1327 usb_anchor_urb(urb, &data->isoc_anchor);
1329 err = usb_submit_urb(urb, mem_flags);
1331 if (err != -EPERM && err != -ENODEV)
1332 bt_dev_err(hdev, "urb %p submission failed (%d)",
1334 usb_unanchor_urb(urb);
1342 static void btusb_diag_complete(struct urb *urb)
1344 struct hci_dev *hdev = urb->context;
1345 struct btusb_data *data = hci_get_drvdata(hdev);
1348 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
1349 urb->actual_length);
1351 if (urb->status == 0) {
1352 struct sk_buff *skb;
1354 skb = bt_skb_alloc(urb->actual_length, GFP_ATOMIC);
1356 skb_put_data(skb, urb->transfer_buffer,
1357 urb->actual_length);
1358 hci_recv_diag(hdev, skb);
1360 } else if (urb->status == -ENOENT) {
1361 /* Avoid suspend failed when usb_kill_urb */
1365 if (!test_bit(BTUSB_DIAG_RUNNING, &data->flags))
1368 usb_anchor_urb(urb, &data->diag_anchor);
1369 usb_mark_last_busy(data->udev);
1371 err = usb_submit_urb(urb, GFP_ATOMIC);
1373 /* -EPERM: urb is being killed;
1374 * -ENODEV: device got disconnected
1376 if (err != -EPERM && err != -ENODEV)
1377 bt_dev_err(hdev, "urb %p failed to resubmit (%d)",
1379 usb_unanchor_urb(urb);
1383 static int btusb_submit_diag_urb(struct hci_dev *hdev, gfp_t mem_flags)
1385 struct btusb_data *data = hci_get_drvdata(hdev);
1389 int err, size = HCI_MAX_FRAME_SIZE;
1391 BT_DBG("%s", hdev->name);
1393 if (!data->diag_rx_ep)
1396 urb = usb_alloc_urb(0, mem_flags);
1400 buf = kmalloc(size, mem_flags);
1406 pipe = usb_rcvbulkpipe(data->udev, data->diag_rx_ep->bEndpointAddress);
1408 usb_fill_bulk_urb(urb, data->udev, pipe, buf, size,
1409 btusb_diag_complete, hdev);
1411 urb->transfer_flags |= URB_FREE_BUFFER;
1413 usb_mark_last_busy(data->udev);
1414 usb_anchor_urb(urb, &data->diag_anchor);
1416 err = usb_submit_urb(urb, mem_flags);
1418 if (err != -EPERM && err != -ENODEV)
1419 bt_dev_err(hdev, "urb %p submission failed (%d)",
1421 usb_unanchor_urb(urb);
1429 static void btusb_tx_complete(struct urb *urb)
1431 struct sk_buff *skb = urb->context;
1432 struct hci_dev *hdev = (struct hci_dev *)skb->dev;
1433 struct btusb_data *data = hci_get_drvdata(hdev);
1434 unsigned long flags;
1436 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
1437 urb->actual_length);
1439 if (!test_bit(HCI_RUNNING, &hdev->flags))
1443 hdev->stat.byte_tx += urb->transfer_buffer_length;
1445 if (hci_skb_pkt_type(skb) == HCI_COMMAND_PKT)
1446 hci_cmd_sync_cancel(hdev, -urb->status);
1447 hdev->stat.err_tx++;
1451 spin_lock_irqsave(&data->txlock, flags);
1452 data->tx_in_flight--;
1453 spin_unlock_irqrestore(&data->txlock, flags);
1455 kfree(urb->setup_packet);
1460 static void btusb_isoc_tx_complete(struct urb *urb)
1462 struct sk_buff *skb = urb->context;
1463 struct hci_dev *hdev = (struct hci_dev *)skb->dev;
1465 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
1466 urb->actual_length);
1468 if (!test_bit(HCI_RUNNING, &hdev->flags))
1472 hdev->stat.byte_tx += urb->transfer_buffer_length;
1474 hdev->stat.err_tx++;
1477 kfree(urb->setup_packet);
1482 static int btusb_open(struct hci_dev *hdev)
1484 struct btusb_data *data = hci_get_drvdata(hdev);
1487 BT_DBG("%s", hdev->name);
1489 err = usb_autopm_get_interface(data->intf);
1493 /* Patching USB firmware files prior to starting any URBs of HCI path
1494 * It is more safe to use USB bulk channel for downloading USB patch
1496 if (data->setup_on_usb) {
1497 err = data->setup_on_usb(hdev);
1502 data->intf->needs_remote_wakeup = 1;
1504 if (test_and_set_bit(BTUSB_INTR_RUNNING, &data->flags))
1507 err = btusb_submit_intr_urb(hdev, GFP_KERNEL);
1511 err = btusb_submit_bulk_urb(hdev, GFP_KERNEL);
1513 usb_kill_anchored_urbs(&data->intr_anchor);
1517 set_bit(BTUSB_BULK_RUNNING, &data->flags);
1518 btusb_submit_bulk_urb(hdev, GFP_KERNEL);
1521 if (!btusb_submit_diag_urb(hdev, GFP_KERNEL))
1522 set_bit(BTUSB_DIAG_RUNNING, &data->flags);
1526 usb_autopm_put_interface(data->intf);
1530 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
1532 usb_autopm_put_interface(data->intf);
1536 static void btusb_stop_traffic(struct btusb_data *data)
1538 usb_kill_anchored_urbs(&data->intr_anchor);
1539 usb_kill_anchored_urbs(&data->bulk_anchor);
1540 usb_kill_anchored_urbs(&data->isoc_anchor);
1541 usb_kill_anchored_urbs(&data->diag_anchor);
1542 usb_kill_anchored_urbs(&data->ctrl_anchor);
1545 static int btusb_close(struct hci_dev *hdev)
1547 struct btusb_data *data = hci_get_drvdata(hdev);
1550 BT_DBG("%s", hdev->name);
1552 cancel_delayed_work(&data->rx_work);
1553 cancel_work_sync(&data->work);
1554 cancel_work_sync(&data->waker);
1556 skb_queue_purge(&data->acl_q);
1558 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1559 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
1560 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
1561 clear_bit(BTUSB_DIAG_RUNNING, &data->flags);
1563 btusb_stop_traffic(data);
1564 btusb_free_frags(data);
1566 err = usb_autopm_get_interface(data->intf);
1570 data->intf->needs_remote_wakeup = 0;
1572 /* Enable remote wake up for auto-suspend */
1573 if (test_bit(BTUSB_WAKEUP_AUTOSUSPEND, &data->flags))
1574 data->intf->needs_remote_wakeup = 1;
1576 usb_autopm_put_interface(data->intf);
1579 usb_scuttle_anchored_urbs(&data->deferred);
1583 static int btusb_flush(struct hci_dev *hdev)
1585 struct btusb_data *data = hci_get_drvdata(hdev);
1587 BT_DBG("%s", hdev->name);
1589 cancel_delayed_work(&data->rx_work);
1591 skb_queue_purge(&data->acl_q);
1593 usb_kill_anchored_urbs(&data->tx_anchor);
1594 btusb_free_frags(data);
1599 static struct urb *alloc_ctrl_urb(struct hci_dev *hdev, struct sk_buff *skb)
1601 struct btusb_data *data = hci_get_drvdata(hdev);
1602 struct usb_ctrlrequest *dr;
1606 urb = usb_alloc_urb(0, GFP_KERNEL);
1608 return ERR_PTR(-ENOMEM);
1610 dr = kmalloc(sizeof(*dr), GFP_KERNEL);
1613 return ERR_PTR(-ENOMEM);
1616 dr->bRequestType = data->cmdreq_type;
1617 dr->bRequest = data->cmdreq;
1620 dr->wLength = __cpu_to_le16(skb->len);
1622 pipe = usb_sndctrlpipe(data->udev, 0x00);
1624 usb_fill_control_urb(urb, data->udev, pipe, (void *)dr,
1625 skb->data, skb->len, btusb_tx_complete, skb);
1627 skb->dev = (void *)hdev;
1632 static struct urb *alloc_bulk_urb(struct hci_dev *hdev, struct sk_buff *skb)
1634 struct btusb_data *data = hci_get_drvdata(hdev);
1638 if (!data->bulk_tx_ep)
1639 return ERR_PTR(-ENODEV);
1641 urb = usb_alloc_urb(0, GFP_KERNEL);
1643 return ERR_PTR(-ENOMEM);
1645 pipe = usb_sndbulkpipe(data->udev, data->bulk_tx_ep->bEndpointAddress);
1647 usb_fill_bulk_urb(urb, data->udev, pipe,
1648 skb->data, skb->len, btusb_tx_complete, skb);
1650 skb->dev = (void *)hdev;
1655 static struct urb *alloc_isoc_urb(struct hci_dev *hdev, struct sk_buff *skb)
1657 struct btusb_data *data = hci_get_drvdata(hdev);
1661 if (!data->isoc_tx_ep)
1662 return ERR_PTR(-ENODEV);
1664 urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, GFP_KERNEL);
1666 return ERR_PTR(-ENOMEM);
1668 pipe = usb_sndisocpipe(data->udev, data->isoc_tx_ep->bEndpointAddress);
1670 usb_fill_int_urb(urb, data->udev, pipe,
1671 skb->data, skb->len, btusb_isoc_tx_complete,
1672 skb, data->isoc_tx_ep->bInterval);
1674 urb->transfer_flags = URB_ISO_ASAP;
1676 if (data->isoc_altsetting == 6)
1677 __fill_isoc_descriptor_msbc(urb, skb->len,
1678 le16_to_cpu(data->isoc_tx_ep->wMaxPacketSize),
1681 __fill_isoc_descriptor(urb, skb->len,
1682 le16_to_cpu(data->isoc_tx_ep->wMaxPacketSize));
1683 skb->dev = (void *)hdev;
1688 static int submit_tx_urb(struct hci_dev *hdev, struct urb *urb)
1690 struct btusb_data *data = hci_get_drvdata(hdev);
1693 usb_anchor_urb(urb, &data->tx_anchor);
1695 err = usb_submit_urb(urb, GFP_KERNEL);
1697 if (err != -EPERM && err != -ENODEV)
1698 bt_dev_err(hdev, "urb %p submission failed (%d)",
1700 kfree(urb->setup_packet);
1701 usb_unanchor_urb(urb);
1703 usb_mark_last_busy(data->udev);
1710 static int submit_or_queue_tx_urb(struct hci_dev *hdev, struct urb *urb)
1712 struct btusb_data *data = hci_get_drvdata(hdev);
1713 unsigned long flags;
1716 spin_lock_irqsave(&data->txlock, flags);
1717 suspending = test_bit(BTUSB_SUSPENDING, &data->flags);
1719 data->tx_in_flight++;
1720 spin_unlock_irqrestore(&data->txlock, flags);
1723 return submit_tx_urb(hdev, urb);
1725 usb_anchor_urb(urb, &data->deferred);
1726 schedule_work(&data->waker);
1732 static int btusb_send_frame(struct hci_dev *hdev, struct sk_buff *skb)
1736 BT_DBG("%s", hdev->name);
1738 switch (hci_skb_pkt_type(skb)) {
1739 case HCI_COMMAND_PKT:
1740 urb = alloc_ctrl_urb(hdev, skb);
1742 return PTR_ERR(urb);
1744 hdev->stat.cmd_tx++;
1745 return submit_or_queue_tx_urb(hdev, urb);
1747 case HCI_ACLDATA_PKT:
1748 urb = alloc_bulk_urb(hdev, skb);
1750 return PTR_ERR(urb);
1752 hdev->stat.acl_tx++;
1753 return submit_or_queue_tx_urb(hdev, urb);
1755 case HCI_SCODATA_PKT:
1756 if (hci_conn_num(hdev, SCO_LINK) < 1)
1759 urb = alloc_isoc_urb(hdev, skb);
1761 return PTR_ERR(urb);
1763 hdev->stat.sco_tx++;
1764 return submit_tx_urb(hdev, urb);
1770 static void btusb_notify(struct hci_dev *hdev, unsigned int evt)
1772 struct btusb_data *data = hci_get_drvdata(hdev);
1774 BT_DBG("%s evt %d", hdev->name, evt);
1776 if (hci_conn_num(hdev, SCO_LINK) != data->sco_num) {
1777 data->sco_num = hci_conn_num(hdev, SCO_LINK);
1778 data->air_mode = evt;
1779 schedule_work(&data->work);
1783 static inline int __set_isoc_interface(struct hci_dev *hdev, int altsetting)
1785 struct btusb_data *data = hci_get_drvdata(hdev);
1786 struct usb_interface *intf = data->isoc;
1787 struct usb_endpoint_descriptor *ep_desc;
1793 err = usb_set_interface(data->udev, data->isoc_ifnum, altsetting);
1795 bt_dev_err(hdev, "setting interface failed (%d)", -err);
1799 data->isoc_altsetting = altsetting;
1801 data->isoc_tx_ep = NULL;
1802 data->isoc_rx_ep = NULL;
1804 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
1805 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
1807 if (!data->isoc_tx_ep && usb_endpoint_is_isoc_out(ep_desc)) {
1808 data->isoc_tx_ep = ep_desc;
1812 if (!data->isoc_rx_ep && usb_endpoint_is_isoc_in(ep_desc)) {
1813 data->isoc_rx_ep = ep_desc;
1818 if (!data->isoc_tx_ep || !data->isoc_rx_ep) {
1819 bt_dev_err(hdev, "invalid SCO descriptors");
1826 static int btusb_switch_alt_setting(struct hci_dev *hdev, int new_alts)
1828 struct btusb_data *data = hci_get_drvdata(hdev);
1831 if (data->isoc_altsetting != new_alts) {
1832 unsigned long flags;
1834 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1835 usb_kill_anchored_urbs(&data->isoc_anchor);
1837 /* When isochronous alternate setting needs to be
1838 * changed, because SCO connection has been added
1839 * or removed, a packet fragment may be left in the
1840 * reassembling state. This could lead to wrongly
1841 * assembled fragments.
1843 * Clear outstanding fragment when selecting a new
1844 * alternate setting.
1846 spin_lock_irqsave(&data->rxlock, flags);
1847 kfree_skb(data->sco_skb);
1848 data->sco_skb = NULL;
1849 spin_unlock_irqrestore(&data->rxlock, flags);
1851 err = __set_isoc_interface(hdev, new_alts);
1856 if (!test_and_set_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
1857 if (btusb_submit_isoc_urb(hdev, GFP_KERNEL) < 0)
1858 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1860 btusb_submit_isoc_urb(hdev, GFP_KERNEL);
1866 static struct usb_host_interface *btusb_find_altsetting(struct btusb_data *data,
1869 struct usb_interface *intf = data->isoc;
1872 BT_DBG("Looking for Alt no :%d", alt);
1877 for (i = 0; i < intf->num_altsetting; i++) {
1878 if (intf->altsetting[i].desc.bAlternateSetting == alt)
1879 return &intf->altsetting[i];
1885 static void btusb_work(struct work_struct *work)
1887 struct btusb_data *data = container_of(work, struct btusb_data, work);
1888 struct hci_dev *hdev = data->hdev;
1892 if (data->sco_num > 0) {
1893 if (!test_bit(BTUSB_DID_ISO_RESUME, &data->flags)) {
1894 err = usb_autopm_get_interface(data->isoc ? data->isoc : data->intf);
1896 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1897 usb_kill_anchored_urbs(&data->isoc_anchor);
1901 set_bit(BTUSB_DID_ISO_RESUME, &data->flags);
1904 if (data->air_mode == HCI_NOTIFY_ENABLE_SCO_CVSD) {
1905 if (hdev->voice_setting & 0x0020) {
1906 static const int alts[3] = { 2, 4, 5 };
1908 new_alts = alts[data->sco_num - 1];
1910 new_alts = data->sco_num;
1912 } else if (data->air_mode == HCI_NOTIFY_ENABLE_SCO_TRANSP) {
1913 /* Bluetooth USB spec recommends alt 6 (63 bytes), but
1914 * many adapters do not support it. Alt 1 appears to
1915 * work for all adapters that do not have alt 6, and
1916 * which work with WBS at all. Some devices prefer
1917 * alt 3 (HCI payload >= 60 Bytes let air packet
1918 * data satisfy 60 bytes), requiring
1919 * MTU >= 3 (packets) * 25 (size) - 3 (headers) = 72
1920 * see also Core spec 5, vol 4, B 2.1.1 & Table 2.1.
1922 if (btusb_find_altsetting(data, 6))
1924 else if (btusb_find_altsetting(data, 3) &&
1925 hdev->sco_mtu >= 72 &&
1926 test_bit(BTUSB_USE_ALT3_FOR_WBS, &data->flags))
1932 if (btusb_switch_alt_setting(hdev, new_alts) < 0)
1933 bt_dev_err(hdev, "set USB alt:(%d) failed!", new_alts);
1935 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1936 usb_kill_anchored_urbs(&data->isoc_anchor);
1938 __set_isoc_interface(hdev, 0);
1939 if (test_and_clear_bit(BTUSB_DID_ISO_RESUME, &data->flags))
1940 usb_autopm_put_interface(data->isoc ? data->isoc : data->intf);
1944 static void btusb_waker(struct work_struct *work)
1946 struct btusb_data *data = container_of(work, struct btusb_data, waker);
1949 err = usb_autopm_get_interface(data->intf);
1953 usb_autopm_put_interface(data->intf);
1956 static void btusb_rx_work(struct work_struct *work)
1958 struct btusb_data *data = container_of(work, struct btusb_data,
1960 struct sk_buff *skb;
1962 /* Dequeue ACL data received during the interval */
1963 while ((skb = skb_dequeue(&data->acl_q)))
1964 data->recv_acl(data->hdev, skb);
1967 static int btusb_setup_bcm92035(struct hci_dev *hdev)
1969 struct sk_buff *skb;
1972 BT_DBG("%s", hdev->name);
1974 skb = __hci_cmd_sync(hdev, 0xfc3b, 1, &val, HCI_INIT_TIMEOUT);
1976 bt_dev_err(hdev, "BCM92035 command failed (%ld)", PTR_ERR(skb));
1983 static int btusb_setup_csr(struct hci_dev *hdev)
1985 struct btusb_data *data = hci_get_drvdata(hdev);
1986 u16 bcdDevice = le16_to_cpu(data->udev->descriptor.bcdDevice);
1987 struct hci_rp_read_local_version *rp;
1988 struct sk_buff *skb;
1989 bool is_fake = false;
1992 BT_DBG("%s", hdev->name);
1994 skb = __hci_cmd_sync(hdev, HCI_OP_READ_LOCAL_VERSION, 0, NULL,
1997 int err = PTR_ERR(skb);
1998 bt_dev_err(hdev, "CSR: Local version failed (%d)", err);
2002 if (skb->len != sizeof(struct hci_rp_read_local_version)) {
2003 bt_dev_err(hdev, "CSR: Local version length mismatch");
2008 rp = (struct hci_rp_read_local_version *)skb->data;
2010 /* Detect a wide host of Chinese controllers that aren't CSR.
2012 * Known fake bcdDevices: 0x0100, 0x0134, 0x1915, 0x2520, 0x7558, 0x8891
2014 * The main thing they have in common is that these are really popular low-cost
2015 * options that support newer Bluetooth versions but rely on heavy VID/PID
2016 * squatting of this poor old Bluetooth 1.1 device. Even sold as such.
2018 * We detect actual CSR devices by checking that the HCI manufacturer code
2019 * is Cambridge Silicon Radio (10) and ensuring that LMP sub-version and
2020 * HCI rev values always match. As they both store the firmware number.
2022 if (le16_to_cpu(rp->manufacturer) != 10 ||
2023 le16_to_cpu(rp->hci_rev) != le16_to_cpu(rp->lmp_subver))
2026 /* Known legit CSR firmware build numbers and their supported BT versions:
2027 * - 1.1 (0x1) -> 0x0073, 0x020d, 0x033c, 0x034e
2028 * - 1.2 (0x2) -> 0x04d9, 0x0529
2029 * - 2.0 (0x3) -> 0x07a6, 0x07ad, 0x0c5c
2030 * - 2.1 (0x4) -> 0x149c, 0x1735, 0x1899 (0x1899 is a BlueCore4-External)
2031 * - 4.0 (0x6) -> 0x1d86, 0x2031, 0x22bb
2033 * e.g. Real CSR dongles with LMP subversion 0x73 are old enough that
2034 * support BT 1.1 only; so it's a dead giveaway when some
2035 * third-party BT 4.0 dongle reuses it.
2037 else if (le16_to_cpu(rp->lmp_subver) <= 0x034e &&
2038 le16_to_cpu(rp->hci_ver) > BLUETOOTH_VER_1_1)
2041 else if (le16_to_cpu(rp->lmp_subver) <= 0x0529 &&
2042 le16_to_cpu(rp->hci_ver) > BLUETOOTH_VER_1_2)
2045 else if (le16_to_cpu(rp->lmp_subver) <= 0x0c5c &&
2046 le16_to_cpu(rp->hci_ver) > BLUETOOTH_VER_2_0)
2049 else if (le16_to_cpu(rp->lmp_subver) <= 0x1899 &&
2050 le16_to_cpu(rp->hci_ver) > BLUETOOTH_VER_2_1)
2053 else if (le16_to_cpu(rp->lmp_subver) <= 0x22bb &&
2054 le16_to_cpu(rp->hci_ver) > BLUETOOTH_VER_4_0)
2057 /* Other clones which beat all the above checks */
2058 else if (bcdDevice == 0x0134 &&
2059 le16_to_cpu(rp->lmp_subver) == 0x0c5c &&
2060 le16_to_cpu(rp->hci_ver) == BLUETOOTH_VER_2_0)
2064 bt_dev_warn(hdev, "CSR: Unbranded CSR clone detected; adding workarounds and force-suspending once...");
2066 /* Generally these clones have big discrepancies between
2067 * advertised features and what's actually supported.
2068 * Probably will need to be expanded in the future;
2069 * without these the controller will lock up.
2071 set_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks);
2072 set_bit(HCI_QUIRK_BROKEN_ERR_DATA_REPORTING, &hdev->quirks);
2073 set_bit(HCI_QUIRK_BROKEN_FILTER_CLEAR_ALL, &hdev->quirks);
2074 set_bit(HCI_QUIRK_NO_SUSPEND_NOTIFIER, &hdev->quirks);
2076 /* Clear the reset quirk since this is not an actual
2077 * early Bluetooth 1.1 device from CSR.
2079 clear_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
2080 clear_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
2083 * Special workaround for these BT 4.0 chip clones, and potentially more:
2085 * - 0x0134: a Barrot 8041a02 (HCI rev: 0x0810 sub: 0x1012)
2086 * - 0x7558: IC markings FR3191AHAL 749H15143 (HCI rev/sub-version: 0x0709)
2088 * These controllers are really messed-up.
2090 * 1. Their bulk RX endpoint will never report any data unless
2091 * the device was suspended at least once (yes, really).
2092 * 2. They will not wakeup when autosuspended and receiving data
2093 * on their bulk RX endpoint from e.g. a keyboard or mouse
2094 * (IOW remote-wakeup support is broken for the bulk endpoint).
2096 * To fix 1. enable runtime-suspend, force-suspend the
2097 * HCI and then wake-it up by disabling runtime-suspend.
2099 * To fix 2. clear the HCI's can_wake flag, this way the HCI
2100 * will still be autosuspended when it is not open.
2104 * Because these are widespread problems we prefer generic solutions; so
2105 * apply this initialization quirk to every controller that gets here,
2106 * it should be harmless. The alternative is to not work at all.
2108 pm_runtime_allow(&data->udev->dev);
2110 ret = pm_runtime_suspend(&data->udev->dev);
2114 bt_dev_warn(hdev, "CSR: Couldn't suspend the device for our Barrot 8041a02 receive-issue workaround");
2116 pm_runtime_forbid(&data->udev->dev);
2118 device_set_wakeup_capable(&data->udev->dev, false);
2120 /* Re-enable autosuspend if this was requested */
2121 if (enable_autosuspend)
2122 usb_enable_autosuspend(data->udev);
2130 static int inject_cmd_complete(struct hci_dev *hdev, __u16 opcode)
2132 struct sk_buff *skb;
2133 struct hci_event_hdr *hdr;
2134 struct hci_ev_cmd_complete *evt;
2136 skb = bt_skb_alloc(sizeof(*hdr) + sizeof(*evt) + 1, GFP_KERNEL);
2140 hdr = skb_put(skb, sizeof(*hdr));
2141 hdr->evt = HCI_EV_CMD_COMPLETE;
2142 hdr->plen = sizeof(*evt) + 1;
2144 evt = skb_put(skb, sizeof(*evt));
2146 evt->opcode = cpu_to_le16(opcode);
2148 skb_put_u8(skb, 0x00);
2150 hci_skb_pkt_type(skb) = HCI_EVENT_PKT;
2152 return hci_recv_frame(hdev, skb);
2155 static int btusb_recv_bulk_intel(struct btusb_data *data, void *buffer,
2158 struct hci_dev *hdev = data->hdev;
2160 /* When the device is in bootloader mode, then it can send
2161 * events via the bulk endpoint. These events are treated the
2162 * same way as the ones received from the interrupt endpoint.
2164 if (btintel_test_flag(hdev, INTEL_BOOTLOADER))
2165 return btusb_recv_intr(data, buffer, count);
2167 return btusb_recv_bulk(data, buffer, count);
2170 static int btusb_recv_event_intel(struct hci_dev *hdev, struct sk_buff *skb)
2172 if (btintel_test_flag(hdev, INTEL_BOOTLOADER)) {
2173 struct hci_event_hdr *hdr = (void *)skb->data;
2175 if (skb->len > HCI_EVENT_HDR_SIZE && hdr->evt == 0xff &&
2177 const void *ptr = skb->data + HCI_EVENT_HDR_SIZE + 1;
2178 unsigned int len = skb->len - HCI_EVENT_HDR_SIZE - 1;
2180 switch (skb->data[2]) {
2182 /* When switching to the operational firmware
2183 * the device sends a vendor specific event
2184 * indicating that the bootup completed.
2186 btintel_bootup(hdev, ptr, len);
2189 /* When the firmware loading completes the
2190 * device sends out a vendor specific event
2191 * indicating the result of the firmware
2194 btintel_secure_send_result(hdev, ptr, len);
2200 return hci_recv_frame(hdev, skb);
2203 static int btusb_send_frame_intel(struct hci_dev *hdev, struct sk_buff *skb)
2207 BT_DBG("%s", hdev->name);
2209 switch (hci_skb_pkt_type(skb)) {
2210 case HCI_COMMAND_PKT:
2211 if (btintel_test_flag(hdev, INTEL_BOOTLOADER)) {
2212 struct hci_command_hdr *cmd = (void *)skb->data;
2213 __u16 opcode = le16_to_cpu(cmd->opcode);
2215 /* When in bootloader mode and the command 0xfc09
2216 * is received, it needs to be send down the
2217 * bulk endpoint. So allocate a bulk URB instead.
2219 if (opcode == 0xfc09)
2220 urb = alloc_bulk_urb(hdev, skb);
2222 urb = alloc_ctrl_urb(hdev, skb);
2224 /* When the 0xfc01 command is issued to boot into
2225 * the operational firmware, it will actually not
2226 * send a command complete event. To keep the flow
2227 * control working inject that event here.
2229 if (opcode == 0xfc01)
2230 inject_cmd_complete(hdev, opcode);
2232 urb = alloc_ctrl_urb(hdev, skb);
2235 return PTR_ERR(urb);
2237 hdev->stat.cmd_tx++;
2238 return submit_or_queue_tx_urb(hdev, urb);
2240 case HCI_ACLDATA_PKT:
2241 urb = alloc_bulk_urb(hdev, skb);
2243 return PTR_ERR(urb);
2245 hdev->stat.acl_tx++;
2246 return submit_or_queue_tx_urb(hdev, urb);
2248 case HCI_SCODATA_PKT:
2249 if (hci_conn_num(hdev, SCO_LINK) < 1)
2252 urb = alloc_isoc_urb(hdev, skb);
2254 return PTR_ERR(urb);
2256 hdev->stat.sco_tx++;
2257 return submit_tx_urb(hdev, urb);
2263 /* UHW CR mapping */
2264 #define MTK_BT_MISC 0x70002510
2265 #define MTK_BT_SUBSYS_RST 0x70002610
2266 #define MTK_UDMA_INT_STA_BT 0x74000024
2267 #define MTK_UDMA_INT_STA_BT1 0x74000308
2268 #define MTK_BT_WDT_STATUS 0x740003A0
2269 #define MTK_EP_RST_OPT 0x74011890
2270 #define MTK_EP_RST_IN_OUT_OPT 0x00010001
2271 #define MTK_BT_RST_DONE 0x00000100
2272 #define MTK_BT_RESET_WAIT_MS 100
2273 #define MTK_BT_RESET_NUM_TRIES 10
2275 static void btusb_mtk_wmt_recv(struct urb *urb)
2277 struct hci_dev *hdev = urb->context;
2278 struct btusb_data *data = hci_get_drvdata(hdev);
2279 struct sk_buff *skb;
2282 if (urb->status == 0 && urb->actual_length > 0) {
2283 hdev->stat.byte_rx += urb->actual_length;
2285 /* WMT event shouldn't be fragmented and the size should be
2286 * less than HCI_WMT_MAX_EVENT_SIZE.
2288 skb = bt_skb_alloc(HCI_WMT_MAX_EVENT_SIZE, GFP_ATOMIC);
2290 hdev->stat.err_rx++;
2291 kfree(urb->setup_packet);
2295 hci_skb_pkt_type(skb) = HCI_EVENT_PKT;
2296 skb_put_data(skb, urb->transfer_buffer, urb->actual_length);
2298 /* When someone waits for the WMT event, the skb is being cloned
2299 * and being processed the events from there then.
2301 if (test_bit(BTUSB_TX_WAIT_VND_EVT, &data->flags)) {
2302 data->evt_skb = skb_clone(skb, GFP_ATOMIC);
2303 if (!data->evt_skb) {
2305 kfree(urb->setup_packet);
2310 err = hci_recv_frame(hdev, skb);
2312 kfree_skb(data->evt_skb);
2313 data->evt_skb = NULL;
2314 kfree(urb->setup_packet);
2318 if (test_and_clear_bit(BTUSB_TX_WAIT_VND_EVT,
2320 /* Barrier to sync with other CPUs */
2321 smp_mb__after_atomic();
2322 wake_up_bit(&data->flags,
2323 BTUSB_TX_WAIT_VND_EVT);
2325 kfree(urb->setup_packet);
2327 } else if (urb->status == -ENOENT) {
2328 /* Avoid suspend failed when usb_kill_urb */
2332 usb_mark_last_busy(data->udev);
2334 /* The URB complete handler is still called with urb->actual_length = 0
2335 * when the event is not available, so we should keep re-submitting
2336 * URB until WMT event returns, Also, It's necessary to wait some time
2337 * between the two consecutive control URBs to relax the target device
2338 * to generate the event. Otherwise, the WMT event cannot return from
2339 * the device successfully.
2343 usb_anchor_urb(urb, &data->ctrl_anchor);
2344 err = usb_submit_urb(urb, GFP_ATOMIC);
2346 kfree(urb->setup_packet);
2347 /* -EPERM: urb is being killed;
2348 * -ENODEV: device got disconnected
2350 if (err != -EPERM && err != -ENODEV)
2351 bt_dev_err(hdev, "urb %p failed to resubmit (%d)",
2353 usb_unanchor_urb(urb);
2357 static int btusb_mtk_submit_wmt_recv_urb(struct hci_dev *hdev)
2359 struct btusb_data *data = hci_get_drvdata(hdev);
2360 struct usb_ctrlrequest *dr;
2366 urb = usb_alloc_urb(0, GFP_KERNEL);
2370 dr = kmalloc(sizeof(*dr), GFP_KERNEL);
2376 dr->bRequestType = USB_TYPE_VENDOR | USB_DIR_IN;
2378 dr->wIndex = cpu_to_le16(0);
2379 dr->wValue = cpu_to_le16(48);
2380 dr->wLength = cpu_to_le16(size);
2382 buf = kmalloc(size, GFP_KERNEL);
2389 pipe = usb_rcvctrlpipe(data->udev, 0);
2391 usb_fill_control_urb(urb, data->udev, pipe, (void *)dr,
2392 buf, size, btusb_mtk_wmt_recv, hdev);
2394 urb->transfer_flags |= URB_FREE_BUFFER;
2396 usb_anchor_urb(urb, &data->ctrl_anchor);
2397 err = usb_submit_urb(urb, GFP_KERNEL);
2399 if (err != -EPERM && err != -ENODEV)
2400 bt_dev_err(hdev, "urb %p submission failed (%d)",
2402 usb_unanchor_urb(urb);
2410 static int btusb_mtk_hci_wmt_sync(struct hci_dev *hdev,
2411 struct btmtk_hci_wmt_params *wmt_params)
2413 struct btusb_data *data = hci_get_drvdata(hdev);
2414 struct btmtk_hci_wmt_evt_funcc *wmt_evt_funcc;
2415 u32 hlen, status = BTMTK_WMT_INVALID;
2416 struct btmtk_hci_wmt_evt *wmt_evt;
2417 struct btmtk_hci_wmt_cmd *wc;
2418 struct btmtk_wmt_hdr *hdr;
2421 /* Send the WMT command and wait until the WMT event returns */
2422 hlen = sizeof(*hdr) + wmt_params->dlen;
2426 wc = kzalloc(hlen, GFP_KERNEL);
2432 hdr->op = wmt_params->op;
2433 hdr->dlen = cpu_to_le16(wmt_params->dlen + 1);
2434 hdr->flag = wmt_params->flag;
2435 memcpy(wc->data, wmt_params->data, wmt_params->dlen);
2437 set_bit(BTUSB_TX_WAIT_VND_EVT, &data->flags);
2439 err = __hci_cmd_send(hdev, 0xfc6f, hlen, wc);
2442 clear_bit(BTUSB_TX_WAIT_VND_EVT, &data->flags);
2446 /* Submit control IN URB on demand to process the WMT event */
2447 err = btusb_mtk_submit_wmt_recv_urb(hdev);
2451 /* The vendor specific WMT commands are all answered by a vendor
2452 * specific event and will have the Command Status or Command
2453 * Complete as with usual HCI command flow control.
2455 * After sending the command, wait for BTUSB_TX_WAIT_VND_EVT
2456 * state to be cleared. The driver specific event receive routine
2457 * will clear that state and with that indicate completion of the
2460 err = wait_on_bit_timeout(&data->flags, BTUSB_TX_WAIT_VND_EVT,
2461 TASK_INTERRUPTIBLE, HCI_INIT_TIMEOUT);
2462 if (err == -EINTR) {
2463 bt_dev_err(hdev, "Execution of wmt command interrupted");
2464 clear_bit(BTUSB_TX_WAIT_VND_EVT, &data->flags);
2469 bt_dev_err(hdev, "Execution of wmt command timed out");
2470 clear_bit(BTUSB_TX_WAIT_VND_EVT, &data->flags);
2475 /* Parse and handle the return WMT event */
2476 wmt_evt = (struct btmtk_hci_wmt_evt *)data->evt_skb->data;
2477 if (wmt_evt->whdr.op != hdr->op) {
2478 bt_dev_err(hdev, "Wrong op received %d expected %d",
2479 wmt_evt->whdr.op, hdr->op);
2484 switch (wmt_evt->whdr.op) {
2485 case BTMTK_WMT_SEMAPHORE:
2486 if (wmt_evt->whdr.flag == 2)
2487 status = BTMTK_WMT_PATCH_UNDONE;
2489 status = BTMTK_WMT_PATCH_DONE;
2491 case BTMTK_WMT_FUNC_CTRL:
2492 wmt_evt_funcc = (struct btmtk_hci_wmt_evt_funcc *)wmt_evt;
2493 if (be16_to_cpu(wmt_evt_funcc->status) == 0x404)
2494 status = BTMTK_WMT_ON_DONE;
2495 else if (be16_to_cpu(wmt_evt_funcc->status) == 0x420)
2496 status = BTMTK_WMT_ON_PROGRESS;
2498 status = BTMTK_WMT_ON_UNDONE;
2500 case BTMTK_WMT_PATCH_DWNLD:
2501 if (wmt_evt->whdr.flag == 2)
2502 status = BTMTK_WMT_PATCH_DONE;
2503 else if (wmt_evt->whdr.flag == 1)
2504 status = BTMTK_WMT_PATCH_PROGRESS;
2506 status = BTMTK_WMT_PATCH_UNDONE;
2510 if (wmt_params->status)
2511 *wmt_params->status = status;
2514 kfree_skb(data->evt_skb);
2515 data->evt_skb = NULL;
2521 static int btusb_mtk_func_query(struct hci_dev *hdev)
2523 struct btmtk_hci_wmt_params wmt_params;
2527 /* Query whether the function is enabled */
2528 wmt_params.op = BTMTK_WMT_FUNC_CTRL;
2529 wmt_params.flag = 4;
2530 wmt_params.dlen = sizeof(param);
2531 wmt_params.data = ¶m;
2532 wmt_params.status = &status;
2534 err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
2536 bt_dev_err(hdev, "Failed to query function status (%d)", err);
2543 static int btusb_mtk_uhw_reg_write(struct btusb_data *data, u32 reg, u32 val)
2545 struct hci_dev *hdev = data->hdev;
2549 buf = kzalloc(4, GFP_KERNEL);
2553 put_unaligned_le32(val, buf);
2555 pipe = usb_sndctrlpipe(data->udev, 0);
2556 err = usb_control_msg(data->udev, pipe, 0x02,
2558 reg >> 16, reg & 0xffff,
2559 buf, 4, USB_CTRL_SET_TIMEOUT);
2561 bt_dev_err(hdev, "Failed to write uhw reg(%d)", err);
2571 static int btusb_mtk_uhw_reg_read(struct btusb_data *data, u32 reg, u32 *val)
2573 struct hci_dev *hdev = data->hdev;
2577 buf = kzalloc(4, GFP_KERNEL);
2581 pipe = usb_rcvctrlpipe(data->udev, 0);
2582 err = usb_control_msg(data->udev, pipe, 0x01,
2584 reg >> 16, reg & 0xffff,
2585 buf, 4, USB_CTRL_SET_TIMEOUT);
2587 bt_dev_err(hdev, "Failed to read uhw reg(%d)", err);
2591 *val = get_unaligned_le32(buf);
2592 bt_dev_dbg(hdev, "reg=%x, value=0x%08x", reg, *val);
2600 static int btusb_mtk_reg_read(struct btusb_data *data, u32 reg, u32 *val)
2602 int pipe, err, size = sizeof(u32);
2605 buf = kzalloc(size, GFP_KERNEL);
2609 pipe = usb_rcvctrlpipe(data->udev, 0);
2610 err = usb_control_msg(data->udev, pipe, 0x63,
2611 USB_TYPE_VENDOR | USB_DIR_IN,
2612 reg >> 16, reg & 0xffff,
2613 buf, size, USB_CTRL_SET_TIMEOUT);
2617 *val = get_unaligned_le32(buf);
2625 static int btusb_mtk_id_get(struct btusb_data *data, u32 reg, u32 *id)
2627 return btusb_mtk_reg_read(data, reg, id);
2630 static int btusb_mtk_setup(struct hci_dev *hdev)
2632 struct btusb_data *data = hci_get_drvdata(hdev);
2633 struct btmtk_hci_wmt_params wmt_params;
2634 ktime_t calltime, delta, rettime;
2635 struct btmtk_tci_sleep tci_sleep;
2636 unsigned long long duration;
2637 struct sk_buff *skb;
2641 char fw_bin_name[64];
2645 calltime = ktime_get();
2647 err = btusb_mtk_id_get(data, 0x80000008, &dev_id);
2649 bt_dev_err(hdev, "Failed to get device id (%d)", err);
2654 err = btusb_mtk_id_get(data, 0x70010200, &dev_id);
2656 bt_dev_err(hdev, "Failed to get device id (%d)", err);
2659 err = btusb_mtk_id_get(data, 0x80021004, &fw_version);
2661 bt_dev_err(hdev, "Failed to get fw version (%d)", err);
2668 fwname = FIRMWARE_MT7663;
2671 fwname = FIRMWARE_MT7668;
2675 snprintf(fw_bin_name, sizeof(fw_bin_name),
2676 "mediatek/BT_RAM_CODE_MT%04x_1_%x_hdr.bin",
2677 dev_id & 0xffff, (fw_version & 0xff) + 1);
2678 err = btmtk_setup_firmware_79xx(hdev, fw_bin_name,
2679 btusb_mtk_hci_wmt_sync);
2681 bt_dev_err(hdev, "Failed to set up firmware (%d)", err);
2685 /* It's Device EndPoint Reset Option Register */
2686 btusb_mtk_uhw_reg_write(data, MTK_EP_RST_OPT, MTK_EP_RST_IN_OUT_OPT);
2688 /* Enable Bluetooth protocol */
2690 wmt_params.op = BTMTK_WMT_FUNC_CTRL;
2691 wmt_params.flag = 0;
2692 wmt_params.dlen = sizeof(param);
2693 wmt_params.data = ¶m;
2694 wmt_params.status = NULL;
2696 err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
2698 bt_dev_err(hdev, "Failed to send wmt func ctrl (%d)", err);
2702 hci_set_msft_opcode(hdev, 0xFD30);
2703 hci_set_aosp_capable(hdev);
2706 bt_dev_err(hdev, "Unsupported hardware variant (%08x)",
2711 /* Query whether the firmware is already download */
2712 wmt_params.op = BTMTK_WMT_SEMAPHORE;
2713 wmt_params.flag = 1;
2714 wmt_params.dlen = 0;
2715 wmt_params.data = NULL;
2716 wmt_params.status = &status;
2718 err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
2720 bt_dev_err(hdev, "Failed to query firmware status (%d)", err);
2724 if (status == BTMTK_WMT_PATCH_DONE) {
2725 bt_dev_info(hdev, "firmware already downloaded");
2726 goto ignore_setup_fw;
2729 /* Setup a firmware which the device definitely requires */
2730 err = btmtk_setup_firmware(hdev, fwname,
2731 btusb_mtk_hci_wmt_sync);
2736 err = readx_poll_timeout(btusb_mtk_func_query, hdev, status,
2737 status < 0 || status != BTMTK_WMT_ON_PROGRESS,
2739 /* -ETIMEDOUT happens */
2743 /* The other errors happen in btusb_mtk_func_query */
2747 if (status == BTMTK_WMT_ON_DONE) {
2748 bt_dev_info(hdev, "function already on");
2749 goto ignore_func_on;
2752 /* Enable Bluetooth protocol */
2754 wmt_params.op = BTMTK_WMT_FUNC_CTRL;
2755 wmt_params.flag = 0;
2756 wmt_params.dlen = sizeof(param);
2757 wmt_params.data = ¶m;
2758 wmt_params.status = NULL;
2760 err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
2762 bt_dev_err(hdev, "Failed to send wmt func ctrl (%d)", err);
2767 /* Apply the low power environment setup */
2768 tci_sleep.mode = 0x5;
2769 tci_sleep.duration = cpu_to_le16(0x640);
2770 tci_sleep.host_duration = cpu_to_le16(0x640);
2771 tci_sleep.host_wakeup_pin = 0;
2772 tci_sleep.time_compensation = 0;
2774 skb = __hci_cmd_sync(hdev, 0xfc7a, sizeof(tci_sleep), &tci_sleep,
2778 bt_dev_err(hdev, "Failed to apply low power setting (%d)", err);
2784 rettime = ktime_get();
2785 delta = ktime_sub(rettime, calltime);
2786 duration = (unsigned long long)ktime_to_ns(delta) >> 10;
2788 bt_dev_info(hdev, "Device setup in %llu usecs", duration);
2793 static int btusb_mtk_shutdown(struct hci_dev *hdev)
2795 struct btmtk_hci_wmt_params wmt_params;
2799 /* Disable the device */
2800 wmt_params.op = BTMTK_WMT_FUNC_CTRL;
2801 wmt_params.flag = 0;
2802 wmt_params.dlen = sizeof(param);
2803 wmt_params.data = ¶m;
2804 wmt_params.status = NULL;
2806 err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
2808 bt_dev_err(hdev, "Failed to send wmt func ctrl (%d)", err);
2815 static void btusb_mtk_cmd_timeout(struct hci_dev *hdev)
2817 struct btusb_data *data = hci_get_drvdata(hdev);
2821 /* It's MediaTek specific bluetooth reset mechanism via USB */
2822 if (test_and_set_bit(BTUSB_HW_RESET_ACTIVE, &data->flags)) {
2823 bt_dev_err(hdev, "last reset failed? Not resetting again");
2827 err = usb_autopm_get_interface(data->intf);
2831 btusb_stop_traffic(data);
2832 usb_kill_anchored_urbs(&data->tx_anchor);
2834 /* It's Device EndPoint Reset Option Register */
2835 bt_dev_dbg(hdev, "Initiating reset mechanism via uhw");
2836 btusb_mtk_uhw_reg_write(data, MTK_EP_RST_OPT, MTK_EP_RST_IN_OUT_OPT);
2837 btusb_mtk_uhw_reg_read(data, MTK_BT_WDT_STATUS, &val);
2839 /* Reset the bluetooth chip via USB interface. */
2840 btusb_mtk_uhw_reg_write(data, MTK_BT_SUBSYS_RST, 1);
2841 btusb_mtk_uhw_reg_write(data, MTK_UDMA_INT_STA_BT, 0x000000FF);
2842 btusb_mtk_uhw_reg_read(data, MTK_UDMA_INT_STA_BT, &val);
2843 btusb_mtk_uhw_reg_write(data, MTK_UDMA_INT_STA_BT1, 0x000000FF);
2844 btusb_mtk_uhw_reg_read(data, MTK_UDMA_INT_STA_BT1, &val);
2845 /* MT7921 need to delay 20ms between toggle reset bit */
2847 btusb_mtk_uhw_reg_write(data, MTK_BT_SUBSYS_RST, 0);
2848 btusb_mtk_uhw_reg_read(data, MTK_BT_SUBSYS_RST, &val);
2850 /* Poll the register until reset is completed */
2852 btusb_mtk_uhw_reg_read(data, MTK_BT_MISC, &val);
2853 if (val & MTK_BT_RST_DONE) {
2854 bt_dev_dbg(hdev, "Bluetooth Reset Successfully");
2858 bt_dev_dbg(hdev, "Polling Bluetooth Reset CR");
2860 msleep(MTK_BT_RESET_WAIT_MS);
2861 } while (retry < MTK_BT_RESET_NUM_TRIES);
2863 btusb_mtk_id_get(data, 0x70010200, &val);
2865 bt_dev_err(hdev, "Can't get device id, subsys reset fail.");
2867 usb_queue_reset_device(data->intf);
2869 clear_bit(BTUSB_HW_RESET_ACTIVE, &data->flags);
2872 static int btusb_recv_acl_mtk(struct hci_dev *hdev, struct sk_buff *skb)
2874 struct btusb_data *data = hci_get_drvdata(hdev);
2875 u16 handle = le16_to_cpu(hci_acl_hdr(skb)->handle);
2878 case 0xfc6f: /* Firmware dump from device */
2879 /* When the firmware hangs, the device can no longer
2880 * suspend and thus disable auto-suspend.
2882 usb_disable_autosuspend(data->udev);
2884 case 0x05ff: /* Firmware debug logging 1 */
2885 case 0x05fe: /* Firmware debug logging 2 */
2886 return hci_recv_diag(hdev, skb);
2889 return hci_recv_frame(hdev, skb);
2893 /* Configure an out-of-band gpio as wake-up pin, if specified in device tree */
2894 static int marvell_config_oob_wake(struct hci_dev *hdev)
2896 struct sk_buff *skb;
2897 struct btusb_data *data = hci_get_drvdata(hdev);
2898 struct device *dev = &data->udev->dev;
2899 u16 pin, gap, opcode;
2903 /* Move on if no wakeup pin specified */
2904 if (of_property_read_u16(dev->of_node, "marvell,wakeup-pin", &pin) ||
2905 of_property_read_u16(dev->of_node, "marvell,wakeup-gap-ms", &gap))
2908 /* Vendor specific command to configure a GPIO as wake-up pin */
2909 opcode = hci_opcode_pack(0x3F, 0x59);
2910 cmd[0] = opcode & 0xFF;
2911 cmd[1] = opcode >> 8;
2912 cmd[2] = 2; /* length of parameters that follow */
2914 cmd[4] = gap; /* time in ms, for which wakeup pin should be asserted */
2916 skb = bt_skb_alloc(sizeof(cmd), GFP_KERNEL);
2918 bt_dev_err(hdev, "%s: No memory", __func__);
2922 skb_put_data(skb, cmd, sizeof(cmd));
2923 hci_skb_pkt_type(skb) = HCI_COMMAND_PKT;
2925 ret = btusb_send_frame(hdev, skb);
2927 bt_dev_err(hdev, "%s: configuration failed", __func__);
2936 static int btusb_set_bdaddr_marvell(struct hci_dev *hdev,
2937 const bdaddr_t *bdaddr)
2939 struct sk_buff *skb;
2944 buf[1] = sizeof(bdaddr_t);
2945 memcpy(buf + 2, bdaddr, sizeof(bdaddr_t));
2947 skb = __hci_cmd_sync(hdev, 0xfc22, sizeof(buf), buf, HCI_INIT_TIMEOUT);
2950 bt_dev_err(hdev, "changing Marvell device address failed (%ld)",
2959 static int btusb_set_bdaddr_ath3012(struct hci_dev *hdev,
2960 const bdaddr_t *bdaddr)
2962 struct sk_buff *skb;
2969 buf[3] = sizeof(bdaddr_t);
2970 memcpy(buf + 4, bdaddr, sizeof(bdaddr_t));
2972 skb = __hci_cmd_sync(hdev, 0xfc0b, sizeof(buf), buf, HCI_INIT_TIMEOUT);
2975 bt_dev_err(hdev, "Change address command failed (%ld)", ret);
2983 static int btusb_set_bdaddr_wcn6855(struct hci_dev *hdev,
2984 const bdaddr_t *bdaddr)
2986 struct sk_buff *skb;
2990 memcpy(buf, bdaddr, sizeof(bdaddr_t));
2992 skb = __hci_cmd_sync_ev(hdev, 0xfc14, sizeof(buf), buf,
2993 HCI_EV_CMD_COMPLETE, HCI_INIT_TIMEOUT);
2996 bt_dev_err(hdev, "Change address command failed (%ld)", ret);
3004 #define QCA_DFU_PACKET_LEN 4096
3006 #define QCA_GET_TARGET_VERSION 0x09
3007 #define QCA_CHECK_STATUS 0x05
3008 #define QCA_DFU_DOWNLOAD 0x01
3010 #define QCA_SYSCFG_UPDATED 0x40
3011 #define QCA_PATCH_UPDATED 0x80
3012 #define QCA_DFU_TIMEOUT 3000
3013 #define QCA_FLAG_MULTI_NVM 0x80
3014 #define QCA_BT_RESET_WAIT_MS 100
3016 #define WCN6855_2_0_RAM_VERSION_GF 0x400c1200
3017 #define WCN6855_2_1_RAM_VERSION_GF 0x400c1211
3019 struct qca_version {
3021 __le32 patch_version;
3029 struct qca_rampatch_version {
3030 __le16 rom_version_high;
3031 __le16 rom_version_low;
3032 __le16 patch_version;
3035 struct qca_device_info {
3037 u8 rampatch_hdr; /* length of header in rampatch */
3038 u8 nvm_hdr; /* length of header in NVM */
3039 u8 ver_offset; /* offset of version structure in rampatch */
3042 static const struct qca_device_info qca_devices_table[] = {
3043 { 0x00000100, 20, 4, 8 }, /* Rome 1.0 */
3044 { 0x00000101, 20, 4, 8 }, /* Rome 1.1 */
3045 { 0x00000200, 28, 4, 16 }, /* Rome 2.0 */
3046 { 0x00000201, 28, 4, 16 }, /* Rome 2.1 */
3047 { 0x00000300, 28, 4, 16 }, /* Rome 3.0 */
3048 { 0x00000302, 28, 4, 16 }, /* Rome 3.2 */
3049 { 0x00130100, 40, 4, 16 }, /* WCN6855 1.0 */
3050 { 0x00130200, 40, 4, 16 }, /* WCN6855 2.0 */
3051 { 0x00130201, 40, 4, 16 }, /* WCN6855 2.1 */
3052 { 0x00190200, 40, 4, 16 }, /* WCN785x 2.0 */
3055 static int btusb_qca_send_vendor_req(struct usb_device *udev, u8 request,
3056 void *data, u16 size)
3061 buf = kmalloc(size, GFP_KERNEL);
3065 /* Found some of USB hosts have IOT issues with ours so that we should
3066 * not wait until HCI layer is ready.
3068 pipe = usb_rcvctrlpipe(udev, 0);
3069 err = usb_control_msg(udev, pipe, request, USB_TYPE_VENDOR | USB_DIR_IN,
3070 0, 0, buf, size, USB_CTRL_SET_TIMEOUT);
3072 dev_err(&udev->dev, "Failed to access otp area (%d)", err);
3076 memcpy(data, buf, size);
3084 static int btusb_setup_qca_download_fw(struct hci_dev *hdev,
3085 const struct firmware *firmware,
3088 struct btusb_data *btdata = hci_get_drvdata(hdev);
3089 struct usb_device *udev = btdata->udev;
3090 size_t count, size, sent = 0;
3094 buf = kmalloc(QCA_DFU_PACKET_LEN, GFP_KERNEL);
3098 count = firmware->size;
3100 size = min_t(size_t, count, hdr_size);
3101 memcpy(buf, firmware->data, size);
3103 /* USB patches should go down to controller through USB path
3104 * because binary format fits to go down through USB channel.
3105 * USB control path is for patching headers and USB bulk is for
3108 pipe = usb_sndctrlpipe(udev, 0);
3109 err = usb_control_msg(udev, pipe, QCA_DFU_DOWNLOAD, USB_TYPE_VENDOR,
3110 0, 0, buf, size, USB_CTRL_SET_TIMEOUT);
3112 bt_dev_err(hdev, "Failed to send headers (%d)", err);
3119 /* ep2 need time to switch from function acl to function dfu,
3120 * so we add 20ms delay here.
3125 size = min_t(size_t, count, QCA_DFU_PACKET_LEN);
3127 memcpy(buf, firmware->data + sent, size);
3129 pipe = usb_sndbulkpipe(udev, 0x02);
3130 err = usb_bulk_msg(udev, pipe, buf, size, &len,
3133 bt_dev_err(hdev, "Failed to send body at %zd of %zd (%d)",
3134 sent, firmware->size, err);
3139 bt_dev_err(hdev, "Failed to get bulk buffer");
3153 static int btusb_setup_qca_load_rampatch(struct hci_dev *hdev,
3154 struct qca_version *ver,
3155 const struct qca_device_info *info)
3157 struct qca_rampatch_version *rver;
3158 const struct firmware *fw;
3159 u32 ver_rom, ver_patch, rver_rom;
3160 u16 rver_rom_low, rver_rom_high, rver_patch;
3164 ver_rom = le32_to_cpu(ver->rom_version);
3165 ver_patch = le32_to_cpu(ver->patch_version);
3167 snprintf(fwname, sizeof(fwname), "qca/rampatch_usb_%08x.bin", ver_rom);
3169 err = request_firmware(&fw, fwname, &hdev->dev);
3171 bt_dev_err(hdev, "failed to request rampatch file: %s (%d)",
3176 bt_dev_info(hdev, "using rampatch file: %s", fwname);
3178 rver = (struct qca_rampatch_version *)(fw->data + info->ver_offset);
3179 rver_rom_low = le16_to_cpu(rver->rom_version_low);
3180 rver_patch = le16_to_cpu(rver->patch_version);
3182 if (ver_rom & ~0xffffU) {
3183 rver_rom_high = le16_to_cpu(rver->rom_version_high);
3184 rver_rom = le32_to_cpu(rver_rom_high << 16 | rver_rom_low);
3186 rver_rom = rver_rom_low;
3189 bt_dev_info(hdev, "QCA: patch rome 0x%x build 0x%x, "
3190 "firmware rome 0x%x build 0x%x",
3191 rver_rom, rver_patch, ver_rom, ver_patch);
3193 if (rver_rom != ver_rom || rver_patch <= ver_patch) {
3194 bt_dev_err(hdev, "rampatch file version did not match with firmware");
3199 err = btusb_setup_qca_download_fw(hdev, fw, info->rampatch_hdr);
3202 release_firmware(fw);
3207 static void btusb_generate_qca_nvm_name(char *fwname, size_t max_size,
3208 const struct qca_version *ver)
3210 u32 rom_version = le32_to_cpu(ver->rom_version);
3211 u16 flag = le16_to_cpu(ver->flag);
3213 if (((flag >> 8) & 0xff) == QCA_FLAG_MULTI_NVM) {
3214 /* The board_id should be split into two bytes
3215 * The 1st byte is chip ID, and the 2nd byte is platform ID
3216 * For example, board ID 0x010A, 0x01 is platform ID. 0x0A is chip ID
3217 * we have several platforms, and platform IDs are continuously added
3219 * 0x00 is for Mobile
3221 * 0x02 is for Automotive
3222 * 0x03 is for Consumer electronic
3224 u16 board_id = (ver->chip_id << 8) + ver->platform_id;
3225 const char *variant;
3227 switch (le32_to_cpu(ver->ram_version)) {
3228 case WCN6855_2_0_RAM_VERSION_GF:
3229 case WCN6855_2_1_RAM_VERSION_GF:
3237 if (board_id == 0) {
3238 snprintf(fwname, max_size, "qca/nvm_usb_%08x%s.bin",
3239 rom_version, variant);
3241 snprintf(fwname, max_size, "qca/nvm_usb_%08x%s_%04x.bin",
3242 rom_version, variant, board_id);
3245 snprintf(fwname, max_size, "qca/nvm_usb_%08x.bin",
3251 static int btusb_setup_qca_load_nvm(struct hci_dev *hdev,
3252 struct qca_version *ver,
3253 const struct qca_device_info *info)
3255 const struct firmware *fw;
3259 btusb_generate_qca_nvm_name(fwname, sizeof(fwname), ver);
3261 err = request_firmware(&fw, fwname, &hdev->dev);
3263 bt_dev_err(hdev, "failed to request NVM file: %s (%d)",
3268 bt_dev_info(hdev, "using NVM file: %s", fwname);
3270 err = btusb_setup_qca_download_fw(hdev, fw, info->nvm_hdr);
3272 release_firmware(fw);
3277 /* identify the ROM version and check whether patches are needed */
3278 static bool btusb_qca_need_patch(struct usb_device *udev)
3280 struct qca_version ver;
3282 if (btusb_qca_send_vendor_req(udev, QCA_GET_TARGET_VERSION, &ver,
3285 /* only low ROM versions need patches */
3286 return !(le32_to_cpu(ver.rom_version) & ~0xffffU);
3289 static int btusb_setup_qca(struct hci_dev *hdev)
3291 struct btusb_data *btdata = hci_get_drvdata(hdev);
3292 struct usb_device *udev = btdata->udev;
3293 const struct qca_device_info *info = NULL;
3294 struct qca_version ver;
3299 err = btusb_qca_send_vendor_req(udev, QCA_GET_TARGET_VERSION, &ver,
3304 ver_rom = le32_to_cpu(ver.rom_version);
3306 for (i = 0; i < ARRAY_SIZE(qca_devices_table); i++) {
3307 if (ver_rom == qca_devices_table[i].rom_version)
3308 info = &qca_devices_table[i];
3311 /* If the rom_version is not matched in the qca_devices_table
3312 * and the high ROM version is not zero, we assume this chip no
3313 * need to load the rampatch and nvm.
3315 if (ver_rom & ~0xffffU)
3318 bt_dev_err(hdev, "don't support firmware rome 0x%x", ver_rom);
3322 err = btusb_qca_send_vendor_req(udev, QCA_CHECK_STATUS, &status,
3327 if (!(status & QCA_PATCH_UPDATED)) {
3328 err = btusb_setup_qca_load_rampatch(hdev, &ver, info);
3333 err = btusb_qca_send_vendor_req(udev, QCA_GET_TARGET_VERSION, &ver,
3338 if (!(status & QCA_SYSCFG_UPDATED)) {
3339 err = btusb_setup_qca_load_nvm(hdev, &ver, info);
3343 /* WCN6855 2.1 and later will reset to apply firmware downloaded here, so
3344 * wait ~100ms for reset Done then go ahead, otherwise, it maybe
3345 * cause potential enable failure.
3347 if (info->rom_version >= 0x00130201)
3348 msleep(QCA_BT_RESET_WAIT_MS);
3351 /* Mark HCI_OP_ENHANCED_SETUP_SYNC_CONN as broken as it doesn't seem to
3352 * work with the likes of HSP/HFP mSBC.
3354 set_bit(HCI_QUIRK_BROKEN_ENHANCED_SETUP_SYNC_CONN, &hdev->quirks);
3355 set_bit(HCI_QUIRK_BROKEN_ERR_DATA_REPORTING, &hdev->quirks);
3360 static inline int __set_diag_interface(struct hci_dev *hdev)
3362 struct btusb_data *data = hci_get_drvdata(hdev);
3363 struct usb_interface *intf = data->diag;
3369 data->diag_tx_ep = NULL;
3370 data->diag_rx_ep = NULL;
3372 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
3373 struct usb_endpoint_descriptor *ep_desc;
3375 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
3377 if (!data->diag_tx_ep && usb_endpoint_is_bulk_out(ep_desc)) {
3378 data->diag_tx_ep = ep_desc;
3382 if (!data->diag_rx_ep && usb_endpoint_is_bulk_in(ep_desc)) {
3383 data->diag_rx_ep = ep_desc;
3388 if (!data->diag_tx_ep || !data->diag_rx_ep) {
3389 bt_dev_err(hdev, "invalid diagnostic descriptors");
3396 static struct urb *alloc_diag_urb(struct hci_dev *hdev, bool enable)
3398 struct btusb_data *data = hci_get_drvdata(hdev);
3399 struct sk_buff *skb;
3403 if (!data->diag_tx_ep)
3404 return ERR_PTR(-ENODEV);
3406 urb = usb_alloc_urb(0, GFP_KERNEL);
3408 return ERR_PTR(-ENOMEM);
3410 skb = bt_skb_alloc(2, GFP_KERNEL);
3413 return ERR_PTR(-ENOMEM);
3416 skb_put_u8(skb, 0xf0);
3417 skb_put_u8(skb, enable);
3419 pipe = usb_sndbulkpipe(data->udev, data->diag_tx_ep->bEndpointAddress);
3421 usb_fill_bulk_urb(urb, data->udev, pipe,
3422 skb->data, skb->len, btusb_tx_complete, skb);
3424 skb->dev = (void *)hdev;
3429 static int btusb_bcm_set_diag(struct hci_dev *hdev, bool enable)
3431 struct btusb_data *data = hci_get_drvdata(hdev);
3437 if (!test_bit(HCI_RUNNING, &hdev->flags))
3440 urb = alloc_diag_urb(hdev, enable);
3442 return PTR_ERR(urb);
3444 return submit_or_queue_tx_urb(hdev, urb);
3448 static irqreturn_t btusb_oob_wake_handler(int irq, void *priv)
3450 struct btusb_data *data = priv;
3452 pm_wakeup_event(&data->udev->dev, 0);
3455 /* Disable only if not already disabled (keep it balanced) */
3456 if (test_and_clear_bit(BTUSB_OOB_WAKE_ENABLED, &data->flags)) {
3457 disable_irq_nosync(irq);
3458 disable_irq_wake(irq);
3463 static const struct of_device_id btusb_match_table[] = {
3464 { .compatible = "usb1286,204e" },
3465 { .compatible = "usbcf3,e300" }, /* QCA6174A */
3466 { .compatible = "usb4ca,301a" }, /* QCA6174A (Lite-On) */
3469 MODULE_DEVICE_TABLE(of, btusb_match_table);
3471 /* Use an oob wakeup pin? */
3472 static int btusb_config_oob_wake(struct hci_dev *hdev)
3474 struct btusb_data *data = hci_get_drvdata(hdev);
3475 struct device *dev = &data->udev->dev;
3478 clear_bit(BTUSB_OOB_WAKE_ENABLED, &data->flags);
3480 if (!of_match_device(btusb_match_table, dev))
3483 /* Move on if no IRQ specified */
3484 irq = of_irq_get_byname(dev->of_node, "wakeup");
3486 bt_dev_dbg(hdev, "%s: no OOB Wakeup IRQ in DT", __func__);
3490 irq_set_status_flags(irq, IRQ_NOAUTOEN);
3491 ret = devm_request_irq(&hdev->dev, irq, btusb_oob_wake_handler,
3492 0, "OOB Wake-on-BT", data);
3494 bt_dev_err(hdev, "%s: IRQ request failed", __func__);
3498 ret = device_init_wakeup(dev, true);
3500 bt_dev_err(hdev, "%s: failed to init_wakeup", __func__);
3504 data->oob_wake_irq = irq;
3505 bt_dev_info(hdev, "OOB Wake-on-BT configured at IRQ %u", irq);
3510 static void btusb_check_needs_reset_resume(struct usb_interface *intf)
3512 if (dmi_check_system(btusb_needs_reset_resume_table))
3513 interface_to_usbdev(intf)->quirks |= USB_QUIRK_RESET_RESUME;
3516 static bool btusb_wakeup(struct hci_dev *hdev)
3518 struct btusb_data *data = hci_get_drvdata(hdev);
3520 return device_may_wakeup(&data->udev->dev);
3523 static int btusb_shutdown_qca(struct hci_dev *hdev)
3525 struct sk_buff *skb;
3527 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
3529 bt_dev_err(hdev, "HCI reset during shutdown failed");
3530 return PTR_ERR(skb);
3537 static ssize_t force_poll_sync_read(struct file *file, char __user *user_buf,
3538 size_t count, loff_t *ppos)
3540 struct btusb_data *data = file->private_data;
3543 buf[0] = data->poll_sync ? 'Y' : 'N';
3546 return simple_read_from_buffer(user_buf, count, ppos, buf, 2);
3549 static ssize_t force_poll_sync_write(struct file *file,
3550 const char __user *user_buf,
3551 size_t count, loff_t *ppos)
3553 struct btusb_data *data = file->private_data;
3557 err = kstrtobool_from_user(user_buf, count, &enable);
3561 /* Only allow changes while the adapter is down */
3562 if (test_bit(HCI_UP, &data->hdev->flags))
3565 if (data->poll_sync == enable)
3568 data->poll_sync = enable;
3573 static const struct file_operations force_poll_sync_fops = {
3574 .open = simple_open,
3575 .read = force_poll_sync_read,
3576 .write = force_poll_sync_write,
3577 .llseek = default_llseek,
3580 static int btusb_probe(struct usb_interface *intf,
3581 const struct usb_device_id *id)
3583 struct usb_endpoint_descriptor *ep_desc;
3584 struct gpio_desc *reset_gpio;
3585 struct btusb_data *data;
3586 struct hci_dev *hdev;
3587 unsigned ifnum_base;
3588 int i, err, priv_size;
3590 BT_DBG("intf %p id %p", intf, id);
3592 /* interface numbers are hardcoded in the spec */
3593 if (intf->cur_altsetting->desc.bInterfaceNumber != 0) {
3594 if (!(id->driver_info & BTUSB_IFNUM_2))
3596 if (intf->cur_altsetting->desc.bInterfaceNumber != 2)
3600 ifnum_base = intf->cur_altsetting->desc.bInterfaceNumber;
3602 if (!id->driver_info) {
3603 const struct usb_device_id *match;
3605 match = usb_match_id(intf, blacklist_table);
3610 if (id->driver_info == BTUSB_IGNORE)
3613 if (id->driver_info & BTUSB_ATH3012) {
3614 struct usb_device *udev = interface_to_usbdev(intf);
3616 /* Old firmware would otherwise let ath3k driver load
3617 * patch and sysconfig files
3619 if (le16_to_cpu(udev->descriptor.bcdDevice) <= 0x0001 &&
3620 !btusb_qca_need_patch(udev))
3624 data = devm_kzalloc(&intf->dev, sizeof(*data), GFP_KERNEL);
3628 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
3629 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
3631 if (!data->intr_ep && usb_endpoint_is_int_in(ep_desc)) {
3632 data->intr_ep = ep_desc;
3636 if (!data->bulk_tx_ep && usb_endpoint_is_bulk_out(ep_desc)) {
3637 data->bulk_tx_ep = ep_desc;
3641 if (!data->bulk_rx_ep && usb_endpoint_is_bulk_in(ep_desc)) {
3642 data->bulk_rx_ep = ep_desc;
3647 if (!data->intr_ep || !data->bulk_tx_ep || !data->bulk_rx_ep)
3650 if (id->driver_info & BTUSB_AMP) {
3651 data->cmdreq_type = USB_TYPE_CLASS | 0x01;
3652 data->cmdreq = 0x2b;
3654 data->cmdreq_type = USB_TYPE_CLASS;
3655 data->cmdreq = 0x00;
3658 data->udev = interface_to_usbdev(intf);
3661 INIT_WORK(&data->work, btusb_work);
3662 INIT_WORK(&data->waker, btusb_waker);
3663 INIT_DELAYED_WORK(&data->rx_work, btusb_rx_work);
3665 skb_queue_head_init(&data->acl_q);
3667 init_usb_anchor(&data->deferred);
3668 init_usb_anchor(&data->tx_anchor);
3669 spin_lock_init(&data->txlock);
3671 init_usb_anchor(&data->intr_anchor);
3672 init_usb_anchor(&data->bulk_anchor);
3673 init_usb_anchor(&data->isoc_anchor);
3674 init_usb_anchor(&data->diag_anchor);
3675 init_usb_anchor(&data->ctrl_anchor);
3676 spin_lock_init(&data->rxlock);
3680 data->recv_event = hci_recv_frame;
3681 data->recv_bulk = btusb_recv_bulk;
3683 if (id->driver_info & BTUSB_INTEL_COMBINED) {
3684 /* Allocate extra space for Intel device */
3685 priv_size += sizeof(struct btintel_data);
3687 /* Override the rx handlers */
3688 data->recv_event = btusb_recv_event_intel;
3689 data->recv_bulk = btusb_recv_bulk_intel;
3692 data->recv_acl = hci_recv_frame;
3694 hdev = hci_alloc_dev_priv(priv_size);
3698 hdev->bus = HCI_USB;
3699 hci_set_drvdata(hdev, data);
3701 if (id->driver_info & BTUSB_AMP)
3702 hdev->dev_type = HCI_AMP;
3704 hdev->dev_type = HCI_PRIMARY;
3708 SET_HCIDEV_DEV(hdev, &intf->dev);
3710 reset_gpio = gpiod_get_optional(&data->udev->dev, "reset",
3712 if (IS_ERR(reset_gpio)) {
3713 err = PTR_ERR(reset_gpio);
3715 } else if (reset_gpio) {
3716 data->reset_gpio = reset_gpio;
3719 hdev->open = btusb_open;
3720 hdev->close = btusb_close;
3721 hdev->flush = btusb_flush;
3722 hdev->send = btusb_send_frame;
3723 hdev->notify = btusb_notify;
3724 hdev->wakeup = btusb_wakeup;
3727 err = btusb_config_oob_wake(hdev);
3731 /* Marvell devices may need a specific chip configuration */
3732 if (id->driver_info & BTUSB_MARVELL && data->oob_wake_irq) {
3733 err = marvell_config_oob_wake(hdev);
3738 if (id->driver_info & BTUSB_CW6622)
3739 set_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks);
3741 if (id->driver_info & BTUSB_BCM2045)
3742 set_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks);
3744 if (id->driver_info & BTUSB_BCM92035)
3745 hdev->setup = btusb_setup_bcm92035;
3747 if (IS_ENABLED(CONFIG_BT_HCIBTUSB_BCM) &&
3748 (id->driver_info & BTUSB_BCM_PATCHRAM)) {
3749 hdev->manufacturer = 15;
3750 hdev->setup = btbcm_setup_patchram;
3751 hdev->set_diag = btusb_bcm_set_diag;
3752 hdev->set_bdaddr = btbcm_set_bdaddr;
3754 /* Broadcom LM_DIAG Interface numbers are hardcoded */
3755 data->diag = usb_ifnum_to_if(data->udev, ifnum_base + 2);
3758 if (IS_ENABLED(CONFIG_BT_HCIBTUSB_BCM) &&
3759 (id->driver_info & BTUSB_BCM_APPLE)) {
3760 hdev->manufacturer = 15;
3761 hdev->setup = btbcm_setup_apple;
3762 hdev->set_diag = btusb_bcm_set_diag;
3764 /* Broadcom LM_DIAG Interface numbers are hardcoded */
3765 data->diag = usb_ifnum_to_if(data->udev, ifnum_base + 2);
3768 /* Combined Intel Device setup to support multiple setup routine */
3769 if (id->driver_info & BTUSB_INTEL_COMBINED) {
3770 err = btintel_configure_setup(hdev);
3774 /* Transport specific configuration */
3775 hdev->send = btusb_send_frame_intel;
3776 hdev->cmd_timeout = btusb_intel_cmd_timeout;
3778 if (id->driver_info & BTUSB_INTEL_NO_WBS_SUPPORT)
3779 btintel_set_flag(hdev, INTEL_ROM_LEGACY_NO_WBS_SUPPORT);
3781 if (id->driver_info & BTUSB_INTEL_BROKEN_INITIAL_NCMD)
3782 btintel_set_flag(hdev, INTEL_BROKEN_INITIAL_NCMD);
3784 if (id->driver_info & BTUSB_INTEL_BROKEN_SHUTDOWN_LED)
3785 btintel_set_flag(hdev, INTEL_BROKEN_SHUTDOWN_LED);
3788 if (id->driver_info & BTUSB_MARVELL)
3789 hdev->set_bdaddr = btusb_set_bdaddr_marvell;
3791 if (IS_ENABLED(CONFIG_BT_HCIBTUSB_MTK) &&
3792 (id->driver_info & BTUSB_MEDIATEK)) {
3793 hdev->setup = btusb_mtk_setup;
3794 hdev->shutdown = btusb_mtk_shutdown;
3795 hdev->manufacturer = 70;
3796 hdev->cmd_timeout = btusb_mtk_cmd_timeout;
3797 hdev->set_bdaddr = btmtk_set_bdaddr;
3798 set_bit(HCI_QUIRK_NON_PERSISTENT_SETUP, &hdev->quirks);
3799 data->recv_acl = btusb_recv_acl_mtk;
3802 if (id->driver_info & BTUSB_SWAVE) {
3803 set_bit(HCI_QUIRK_FIXUP_INQUIRY_MODE, &hdev->quirks);
3804 set_bit(HCI_QUIRK_BROKEN_LOCAL_COMMANDS, &hdev->quirks);
3807 if (id->driver_info & BTUSB_INTEL_BOOT) {
3808 hdev->manufacturer = 2;
3809 set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
3812 if (id->driver_info & BTUSB_ATH3012) {
3813 data->setup_on_usb = btusb_setup_qca;
3814 hdev->set_bdaddr = btusb_set_bdaddr_ath3012;
3815 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
3816 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
3819 if (id->driver_info & BTUSB_QCA_ROME) {
3820 data->setup_on_usb = btusb_setup_qca;
3821 hdev->set_bdaddr = btusb_set_bdaddr_ath3012;
3822 hdev->cmd_timeout = btusb_qca_cmd_timeout;
3823 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
3824 btusb_check_needs_reset_resume(intf);
3827 if (id->driver_info & BTUSB_QCA_WCN6855) {
3828 data->setup_on_usb = btusb_setup_qca;
3829 hdev->shutdown = btusb_shutdown_qca;
3830 hdev->set_bdaddr = btusb_set_bdaddr_wcn6855;
3831 hdev->cmd_timeout = btusb_qca_cmd_timeout;
3832 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
3833 hci_set_msft_opcode(hdev, 0xFD70);
3836 if (id->driver_info & BTUSB_AMP) {
3837 /* AMP controllers do not support SCO packets */
3840 /* Interface orders are hardcoded in the specification */
3841 data->isoc = usb_ifnum_to_if(data->udev, ifnum_base + 1);
3842 data->isoc_ifnum = ifnum_base + 1;
3845 if (IS_ENABLED(CONFIG_BT_HCIBTUSB_RTL) &&
3846 (id->driver_info & BTUSB_REALTEK)) {
3847 hdev->setup = btrtl_setup_realtek;
3848 hdev->shutdown = btrtl_shutdown_realtek;
3849 hdev->cmd_timeout = btusb_rtl_cmd_timeout;
3851 /* Realtek devices need to set remote wakeup on auto-suspend */
3852 set_bit(BTUSB_WAKEUP_AUTOSUSPEND, &data->flags);
3853 set_bit(BTUSB_USE_ALT3_FOR_WBS, &data->flags);
3857 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
3859 if (force_scofix || id->driver_info & BTUSB_WRONG_SCO_MTU) {
3860 if (!disable_scofix)
3861 set_bit(HCI_QUIRK_FIXUP_BUFFER_SIZE, &hdev->quirks);
3864 if (id->driver_info & BTUSB_BROKEN_ISOC)
3867 if (id->driver_info & BTUSB_WIDEBAND_SPEECH)
3868 set_bit(HCI_QUIRK_WIDEBAND_SPEECH_SUPPORTED, &hdev->quirks);
3870 if (id->driver_info & BTUSB_VALID_LE_STATES)
3871 set_bit(HCI_QUIRK_VALID_LE_STATES, &hdev->quirks);
3873 if (id->driver_info & BTUSB_DIGIANSWER) {
3874 data->cmdreq_type = USB_TYPE_VENDOR;
3875 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
3878 if (id->driver_info & BTUSB_CSR) {
3879 struct usb_device *udev = data->udev;
3880 u16 bcdDevice = le16_to_cpu(udev->descriptor.bcdDevice);
3882 /* Old firmware would otherwise execute USB reset */
3883 if (bcdDevice < 0x117)
3884 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
3886 /* This must be set first in case we disable it for fakes */
3887 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
3889 /* Fake CSR devices with broken commands */
3890 if (le16_to_cpu(udev->descriptor.idVendor) == 0x0a12 &&
3891 le16_to_cpu(udev->descriptor.idProduct) == 0x0001)
3892 hdev->setup = btusb_setup_csr;
3895 if (id->driver_info & BTUSB_SNIFFER) {
3896 struct usb_device *udev = data->udev;
3898 /* New sniffer firmware has crippled HCI interface */
3899 if (le16_to_cpu(udev->descriptor.bcdDevice) > 0x997)
3900 set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
3903 if (id->driver_info & BTUSB_INTEL_BOOT) {
3904 /* A bug in the bootloader causes that interrupt interface is
3905 * only enabled after receiving SetInterface(0, AltSetting=0).
3907 err = usb_set_interface(data->udev, 0, 0);
3909 BT_ERR("failed to set interface 0, alt 0 %d", err);
3915 err = usb_driver_claim_interface(&btusb_driver,
3921 if (IS_ENABLED(CONFIG_BT_HCIBTUSB_BCM) && data->diag) {
3922 if (!usb_driver_claim_interface(&btusb_driver,
3924 __set_diag_interface(hdev);
3929 if (enable_autosuspend)
3930 usb_enable_autosuspend(data->udev);
3932 err = hci_register_dev(hdev);
3936 usb_set_intfdata(intf, data);
3938 debugfs_create_file("force_poll_sync", 0644, hdev->debugfs, data,
3939 &force_poll_sync_fops);
3944 if (data->reset_gpio)
3945 gpiod_put(data->reset_gpio);
3950 static void btusb_disconnect(struct usb_interface *intf)
3952 struct btusb_data *data = usb_get_intfdata(intf);
3953 struct hci_dev *hdev;
3955 BT_DBG("intf %p", intf);
3961 usb_set_intfdata(data->intf, NULL);
3964 usb_set_intfdata(data->isoc, NULL);
3967 usb_set_intfdata(data->diag, NULL);
3969 hci_unregister_dev(hdev);
3971 if (intf == data->intf) {
3973 usb_driver_release_interface(&btusb_driver, data->isoc);
3975 usb_driver_release_interface(&btusb_driver, data->diag);
3976 } else if (intf == data->isoc) {
3978 usb_driver_release_interface(&btusb_driver, data->diag);
3979 usb_driver_release_interface(&btusb_driver, data->intf);
3980 } else if (intf == data->diag) {
3981 usb_driver_release_interface(&btusb_driver, data->intf);
3983 usb_driver_release_interface(&btusb_driver, data->isoc);
3986 if (data->oob_wake_irq)
3987 device_init_wakeup(&data->udev->dev, false);
3989 if (data->reset_gpio)
3990 gpiod_put(data->reset_gpio);
3996 static int btusb_suspend(struct usb_interface *intf, pm_message_t message)
3998 struct btusb_data *data = usb_get_intfdata(intf);
4000 BT_DBG("intf %p", intf);
4002 if (data->suspend_count++)
4005 spin_lock_irq(&data->txlock);
4006 if (!(PMSG_IS_AUTO(message) && data->tx_in_flight)) {
4007 set_bit(BTUSB_SUSPENDING, &data->flags);
4008 spin_unlock_irq(&data->txlock);
4010 spin_unlock_irq(&data->txlock);
4011 data->suspend_count--;
4015 cancel_work_sync(&data->work);
4017 btusb_stop_traffic(data);
4018 usb_kill_anchored_urbs(&data->tx_anchor);
4020 if (data->oob_wake_irq && device_may_wakeup(&data->udev->dev)) {
4021 set_bit(BTUSB_OOB_WAKE_ENABLED, &data->flags);
4022 enable_irq_wake(data->oob_wake_irq);
4023 enable_irq(data->oob_wake_irq);
4026 /* For global suspend, Realtek devices lose the loaded fw
4027 * in them. But for autosuspend, firmware should remain.
4028 * Actually, it depends on whether the usb host sends
4029 * set feature (enable wakeup) or not.
4031 if (test_bit(BTUSB_WAKEUP_AUTOSUSPEND, &data->flags)) {
4032 if (PMSG_IS_AUTO(message) &&
4033 device_can_wakeup(&data->udev->dev))
4034 data->udev->do_remote_wakeup = 1;
4035 else if (!PMSG_IS_AUTO(message) &&
4036 !device_may_wakeup(&data->udev->dev)) {
4037 data->udev->do_remote_wakeup = 0;
4038 data->udev->reset_resume = 1;
4045 static void play_deferred(struct btusb_data *data)
4050 while ((urb = usb_get_from_anchor(&data->deferred))) {
4051 usb_anchor_urb(urb, &data->tx_anchor);
4053 err = usb_submit_urb(urb, GFP_ATOMIC);
4055 if (err != -EPERM && err != -ENODEV)
4056 BT_ERR("%s urb %p submission failed (%d)",
4057 data->hdev->name, urb, -err);
4058 kfree(urb->setup_packet);
4059 usb_unanchor_urb(urb);
4064 data->tx_in_flight++;
4068 /* Cleanup the rest deferred urbs. */
4069 while ((urb = usb_get_from_anchor(&data->deferred))) {
4070 kfree(urb->setup_packet);
4075 static int btusb_resume(struct usb_interface *intf)
4077 struct btusb_data *data = usb_get_intfdata(intf);
4078 struct hci_dev *hdev = data->hdev;
4081 BT_DBG("intf %p", intf);
4083 if (--data->suspend_count)
4086 /* Disable only if not already disabled (keep it balanced) */
4087 if (test_and_clear_bit(BTUSB_OOB_WAKE_ENABLED, &data->flags)) {
4088 disable_irq(data->oob_wake_irq);
4089 disable_irq_wake(data->oob_wake_irq);
4092 if (!test_bit(HCI_RUNNING, &hdev->flags))
4095 if (test_bit(BTUSB_INTR_RUNNING, &data->flags)) {
4096 err = btusb_submit_intr_urb(hdev, GFP_NOIO);
4098 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
4103 if (test_bit(BTUSB_BULK_RUNNING, &data->flags)) {
4104 err = btusb_submit_bulk_urb(hdev, GFP_NOIO);
4106 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
4110 btusb_submit_bulk_urb(hdev, GFP_NOIO);
4113 if (test_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
4114 if (btusb_submit_isoc_urb(hdev, GFP_NOIO) < 0)
4115 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
4117 btusb_submit_isoc_urb(hdev, GFP_NOIO);
4120 spin_lock_irq(&data->txlock);
4121 play_deferred(data);
4122 clear_bit(BTUSB_SUSPENDING, &data->flags);
4123 spin_unlock_irq(&data->txlock);
4124 schedule_work(&data->work);
4129 usb_scuttle_anchored_urbs(&data->deferred);
4131 spin_lock_irq(&data->txlock);
4132 clear_bit(BTUSB_SUSPENDING, &data->flags);
4133 spin_unlock_irq(&data->txlock);
4139 static struct usb_driver btusb_driver = {
4141 .probe = btusb_probe,
4142 .disconnect = btusb_disconnect,
4144 .suspend = btusb_suspend,
4145 .resume = btusb_resume,
4147 .id_table = btusb_table,
4148 .supports_autosuspend = 1,
4149 .disable_hub_initiated_lpm = 1,
4152 module_usb_driver(btusb_driver);
4154 module_param(disable_scofix, bool, 0644);
4155 MODULE_PARM_DESC(disable_scofix, "Disable fixup of wrong SCO buffer size");
4157 module_param(force_scofix, bool, 0644);
4158 MODULE_PARM_DESC(force_scofix, "Force fixup of wrong SCO buffers size");
4160 module_param(enable_autosuspend, bool, 0644);
4161 MODULE_PARM_DESC(enable_autosuspend, "Enable USB autosuspend by default");
4163 module_param(reset, bool, 0644);
4164 MODULE_PARM_DESC(reset, "Send HCI reset command on initialization");
4166 MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
4167 MODULE_DESCRIPTION("Generic Bluetooth USB driver ver " VERSION);
4168 MODULE_VERSION(VERSION);
4169 MODULE_LICENSE("GPL");