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 0x01
40 #define BTUSB_DIGIANSWER 0x02
41 #define BTUSB_CSR 0x04
42 #define BTUSB_SNIFFER 0x08
43 #define BTUSB_BCM92035 0x10
44 #define BTUSB_BROKEN_ISOC 0x20
45 #define BTUSB_WRONG_SCO_MTU 0x40
46 #define BTUSB_ATH3012 0x80
47 #define BTUSB_INTEL_COMBINED 0x100
48 #define BTUSB_INTEL_BOOT 0x200
49 #define BTUSB_BCM_PATCHRAM 0x400
50 #define BTUSB_MARVELL 0x800
51 #define BTUSB_SWAVE 0x1000
52 #define BTUSB_AMP 0x4000
53 #define BTUSB_QCA_ROME 0x8000
54 #define BTUSB_BCM_APPLE 0x10000
55 #define BTUSB_REALTEK 0x20000
56 #define BTUSB_BCM2045 0x40000
57 #define BTUSB_IFNUM_2 0x80000
58 #define BTUSB_CW6622 0x100000
59 #define BTUSB_MEDIATEK 0x200000
60 #define BTUSB_WIDEBAND_SPEECH 0x400000
61 #define BTUSB_VALID_LE_STATES 0x800000
62 #define BTUSB_QCA_WCN6855 0x1000000
63 #define BTUSB_INTEL_BROKEN_SHUTDOWN_LED 0x2000000
64 #define BTUSB_INTEL_BROKEN_INITIAL_NCMD 0x4000000
66 static const struct usb_device_id btusb_table[] = {
67 /* Generic Bluetooth USB device */
68 { USB_DEVICE_INFO(0xe0, 0x01, 0x01) },
70 /* Generic Bluetooth AMP device */
71 { USB_DEVICE_INFO(0xe0, 0x01, 0x04), .driver_info = BTUSB_AMP },
73 /* Generic Bluetooth USB interface */
74 { USB_INTERFACE_INFO(0xe0, 0x01, 0x01) },
76 /* Apple-specific (Broadcom) devices */
77 { USB_VENDOR_AND_INTERFACE_INFO(0x05ac, 0xff, 0x01, 0x01),
78 .driver_info = BTUSB_BCM_APPLE | BTUSB_IFNUM_2 },
80 /* MediaTek MT76x0E */
81 { USB_DEVICE(0x0e8d, 0x763f) },
83 /* Broadcom SoftSailing reporting vendor specific */
84 { USB_DEVICE(0x0a5c, 0x21e1) },
86 /* Apple MacBookPro 7,1 */
87 { USB_DEVICE(0x05ac, 0x8213) },
90 { USB_DEVICE(0x05ac, 0x8215) },
92 /* Apple MacBookPro6,2 */
93 { USB_DEVICE(0x05ac, 0x8218) },
95 /* Apple MacBookAir3,1, MacBookAir3,2 */
96 { USB_DEVICE(0x05ac, 0x821b) },
98 /* Apple MacBookAir4,1 */
99 { USB_DEVICE(0x05ac, 0x821f) },
101 /* Apple MacBookPro8,2 */
102 { USB_DEVICE(0x05ac, 0x821a) },
104 /* Apple MacMini5,1 */
105 { USB_DEVICE(0x05ac, 0x8281) },
107 /* AVM BlueFRITZ! USB v2.0 */
108 { USB_DEVICE(0x057c, 0x3800), .driver_info = BTUSB_SWAVE },
110 /* Bluetooth Ultraport Module from IBM */
111 { USB_DEVICE(0x04bf, 0x030a) },
113 /* ALPS Modules with non-standard id */
114 { USB_DEVICE(0x044e, 0x3001) },
115 { USB_DEVICE(0x044e, 0x3002) },
117 /* Ericsson with non-standard id */
118 { USB_DEVICE(0x0bdb, 0x1002) },
120 /* Canyon CN-BTU1 with HID interfaces */
121 { USB_DEVICE(0x0c10, 0x0000) },
123 /* Broadcom BCM20702B0 (Dynex/Insignia) */
124 { USB_DEVICE(0x19ff, 0x0239), .driver_info = BTUSB_BCM_PATCHRAM },
126 /* Broadcom BCM43142A0 (Foxconn/Lenovo) */
127 { USB_VENDOR_AND_INTERFACE_INFO(0x105b, 0xff, 0x01, 0x01),
128 .driver_info = BTUSB_BCM_PATCHRAM },
130 /* Broadcom BCM920703 (HTC Vive) */
131 { USB_VENDOR_AND_INTERFACE_INFO(0x0bb4, 0xff, 0x01, 0x01),
132 .driver_info = BTUSB_BCM_PATCHRAM },
134 /* Foxconn - Hon Hai */
135 { USB_VENDOR_AND_INTERFACE_INFO(0x0489, 0xff, 0x01, 0x01),
136 .driver_info = BTUSB_BCM_PATCHRAM },
138 /* Lite-On Technology - Broadcom based */
139 { USB_VENDOR_AND_INTERFACE_INFO(0x04ca, 0xff, 0x01, 0x01),
140 .driver_info = BTUSB_BCM_PATCHRAM },
142 /* Broadcom devices with vendor specific id */
143 { USB_VENDOR_AND_INTERFACE_INFO(0x0a5c, 0xff, 0x01, 0x01),
144 .driver_info = BTUSB_BCM_PATCHRAM },
146 /* ASUSTek Computer - Broadcom based */
147 { USB_VENDOR_AND_INTERFACE_INFO(0x0b05, 0xff, 0x01, 0x01),
148 .driver_info = BTUSB_BCM_PATCHRAM },
150 /* Belkin F8065bf - Broadcom based */
151 { USB_VENDOR_AND_INTERFACE_INFO(0x050d, 0xff, 0x01, 0x01),
152 .driver_info = BTUSB_BCM_PATCHRAM },
154 /* IMC Networks - Broadcom based */
155 { USB_VENDOR_AND_INTERFACE_INFO(0x13d3, 0xff, 0x01, 0x01),
156 .driver_info = BTUSB_BCM_PATCHRAM },
158 /* Dell Computer - Broadcom based */
159 { USB_VENDOR_AND_INTERFACE_INFO(0x413c, 0xff, 0x01, 0x01),
160 .driver_info = BTUSB_BCM_PATCHRAM },
162 /* Toshiba Corp - Broadcom based */
163 { USB_VENDOR_AND_INTERFACE_INFO(0x0930, 0xff, 0x01, 0x01),
164 .driver_info = BTUSB_BCM_PATCHRAM },
166 /* Intel Bluetooth USB Bootloader (RAM module) */
167 { USB_DEVICE(0x8087, 0x0a5a),
168 .driver_info = BTUSB_INTEL_BOOT | BTUSB_BROKEN_ISOC },
170 { } /* Terminating entry */
173 MODULE_DEVICE_TABLE(usb, btusb_table);
175 static const struct usb_device_id blacklist_table[] = {
176 /* CSR BlueCore devices */
177 { USB_DEVICE(0x0a12, 0x0001), .driver_info = BTUSB_CSR },
179 /* Broadcom BCM2033 without firmware */
180 { USB_DEVICE(0x0a5c, 0x2033), .driver_info = BTUSB_IGNORE },
182 /* Broadcom BCM2045 devices */
183 { USB_DEVICE(0x0a5c, 0x2045), .driver_info = BTUSB_BCM2045 },
185 /* Atheros 3011 with sflash firmware */
186 { USB_DEVICE(0x0489, 0xe027), .driver_info = BTUSB_IGNORE },
187 { USB_DEVICE(0x0489, 0xe03d), .driver_info = BTUSB_IGNORE },
188 { USB_DEVICE(0x04f2, 0xaff1), .driver_info = BTUSB_IGNORE },
189 { USB_DEVICE(0x0930, 0x0215), .driver_info = BTUSB_IGNORE },
190 { USB_DEVICE(0x0cf3, 0x3002), .driver_info = BTUSB_IGNORE },
191 { USB_DEVICE(0x0cf3, 0xe019), .driver_info = BTUSB_IGNORE },
192 { USB_DEVICE(0x13d3, 0x3304), .driver_info = BTUSB_IGNORE },
194 /* Atheros AR9285 Malbec with sflash firmware */
195 { USB_DEVICE(0x03f0, 0x311d), .driver_info = BTUSB_IGNORE },
197 /* Atheros 3012 with sflash firmware */
198 { USB_DEVICE(0x0489, 0xe04d), .driver_info = BTUSB_ATH3012 },
199 { USB_DEVICE(0x0489, 0xe04e), .driver_info = BTUSB_ATH3012 },
200 { USB_DEVICE(0x0489, 0xe056), .driver_info = BTUSB_ATH3012 },
201 { USB_DEVICE(0x0489, 0xe057), .driver_info = BTUSB_ATH3012 },
202 { USB_DEVICE(0x0489, 0xe05f), .driver_info = BTUSB_ATH3012 },
203 { USB_DEVICE(0x0489, 0xe076), .driver_info = BTUSB_ATH3012 },
204 { USB_DEVICE(0x0489, 0xe078), .driver_info = BTUSB_ATH3012 },
205 { USB_DEVICE(0x0489, 0xe095), .driver_info = BTUSB_ATH3012 },
206 { USB_DEVICE(0x04c5, 0x1330), .driver_info = BTUSB_ATH3012 },
207 { USB_DEVICE(0x04ca, 0x3004), .driver_info = BTUSB_ATH3012 },
208 { USB_DEVICE(0x04ca, 0x3005), .driver_info = BTUSB_ATH3012 },
209 { USB_DEVICE(0x04ca, 0x3006), .driver_info = BTUSB_ATH3012 },
210 { USB_DEVICE(0x04ca, 0x3007), .driver_info = BTUSB_ATH3012 },
211 { USB_DEVICE(0x04ca, 0x3008), .driver_info = BTUSB_ATH3012 },
212 { USB_DEVICE(0x04ca, 0x300b), .driver_info = BTUSB_ATH3012 },
213 { USB_DEVICE(0x04ca, 0x300d), .driver_info = BTUSB_ATH3012 },
214 { USB_DEVICE(0x04ca, 0x300f), .driver_info = BTUSB_ATH3012 },
215 { USB_DEVICE(0x04ca, 0x3010), .driver_info = BTUSB_ATH3012 },
216 { USB_DEVICE(0x04ca, 0x3014), .driver_info = BTUSB_ATH3012 },
217 { USB_DEVICE(0x04ca, 0x3018), .driver_info = BTUSB_ATH3012 },
218 { USB_DEVICE(0x0930, 0x0219), .driver_info = BTUSB_ATH3012 },
219 { USB_DEVICE(0x0930, 0x021c), .driver_info = BTUSB_ATH3012 },
220 { USB_DEVICE(0x0930, 0x0220), .driver_info = BTUSB_ATH3012 },
221 { USB_DEVICE(0x0930, 0x0227), .driver_info = BTUSB_ATH3012 },
222 { USB_DEVICE(0x0b05, 0x17d0), .driver_info = BTUSB_ATH3012 },
223 { USB_DEVICE(0x0cf3, 0x0036), .driver_info = BTUSB_ATH3012 },
224 { USB_DEVICE(0x0cf3, 0x3004), .driver_info = BTUSB_ATH3012 },
225 { USB_DEVICE(0x0cf3, 0x3008), .driver_info = BTUSB_ATH3012 },
226 { USB_DEVICE(0x0cf3, 0x311d), .driver_info = BTUSB_ATH3012 },
227 { USB_DEVICE(0x0cf3, 0x311e), .driver_info = BTUSB_ATH3012 },
228 { USB_DEVICE(0x0cf3, 0x311f), .driver_info = BTUSB_ATH3012 },
229 { USB_DEVICE(0x0cf3, 0x3121), .driver_info = BTUSB_ATH3012 },
230 { USB_DEVICE(0x0cf3, 0x817a), .driver_info = BTUSB_ATH3012 },
231 { USB_DEVICE(0x0cf3, 0x817b), .driver_info = BTUSB_ATH3012 },
232 { USB_DEVICE(0x0cf3, 0xe003), .driver_info = BTUSB_ATH3012 },
233 { USB_DEVICE(0x0cf3, 0xe004), .driver_info = BTUSB_ATH3012 },
234 { USB_DEVICE(0x0cf3, 0xe005), .driver_info = BTUSB_ATH3012 },
235 { USB_DEVICE(0x0cf3, 0xe006), .driver_info = BTUSB_ATH3012 },
236 { USB_DEVICE(0x13d3, 0x3362), .driver_info = BTUSB_ATH3012 },
237 { USB_DEVICE(0x13d3, 0x3375), .driver_info = BTUSB_ATH3012 },
238 { USB_DEVICE(0x13d3, 0x3393), .driver_info = BTUSB_ATH3012 },
239 { USB_DEVICE(0x13d3, 0x3395), .driver_info = BTUSB_ATH3012 },
240 { USB_DEVICE(0x13d3, 0x3402), .driver_info = BTUSB_ATH3012 },
241 { USB_DEVICE(0x13d3, 0x3408), .driver_info = BTUSB_ATH3012 },
242 { USB_DEVICE(0x13d3, 0x3423), .driver_info = BTUSB_ATH3012 },
243 { USB_DEVICE(0x13d3, 0x3432), .driver_info = BTUSB_ATH3012 },
244 { USB_DEVICE(0x13d3, 0x3472), .driver_info = BTUSB_ATH3012 },
245 { USB_DEVICE(0x13d3, 0x3474), .driver_info = BTUSB_ATH3012 },
246 { USB_DEVICE(0x13d3, 0x3487), .driver_info = BTUSB_ATH3012 },
247 { USB_DEVICE(0x13d3, 0x3490), .driver_info = BTUSB_ATH3012 },
249 /* Atheros AR5BBU12 with sflash firmware */
250 { USB_DEVICE(0x0489, 0xe02c), .driver_info = BTUSB_IGNORE },
252 /* Atheros AR5BBU12 with sflash firmware */
253 { USB_DEVICE(0x0489, 0xe036), .driver_info = BTUSB_ATH3012 },
254 { USB_DEVICE(0x0489, 0xe03c), .driver_info = BTUSB_ATH3012 },
256 /* QCA ROME chipset */
257 { USB_DEVICE(0x0cf3, 0x535b), .driver_info = BTUSB_QCA_ROME |
258 BTUSB_WIDEBAND_SPEECH },
259 { USB_DEVICE(0x0cf3, 0xe007), .driver_info = BTUSB_QCA_ROME |
260 BTUSB_WIDEBAND_SPEECH },
261 { USB_DEVICE(0x0cf3, 0xe009), .driver_info = BTUSB_QCA_ROME |
262 BTUSB_WIDEBAND_SPEECH },
263 { USB_DEVICE(0x0cf3, 0xe010), .driver_info = BTUSB_QCA_ROME |
264 BTUSB_WIDEBAND_SPEECH },
265 { USB_DEVICE(0x0cf3, 0xe300), .driver_info = BTUSB_QCA_ROME |
266 BTUSB_WIDEBAND_SPEECH },
267 { USB_DEVICE(0x0cf3, 0xe301), .driver_info = BTUSB_QCA_ROME |
268 BTUSB_WIDEBAND_SPEECH },
269 { USB_DEVICE(0x0cf3, 0xe360), .driver_info = BTUSB_QCA_ROME |
270 BTUSB_WIDEBAND_SPEECH },
271 { USB_DEVICE(0x0cf3, 0xe500), .driver_info = BTUSB_QCA_ROME |
272 BTUSB_WIDEBAND_SPEECH },
273 { USB_DEVICE(0x0489, 0xe092), .driver_info = BTUSB_QCA_ROME |
274 BTUSB_WIDEBAND_SPEECH },
275 { USB_DEVICE(0x0489, 0xe09f), .driver_info = BTUSB_QCA_ROME |
276 BTUSB_WIDEBAND_SPEECH },
277 { USB_DEVICE(0x0489, 0xe0a2), .driver_info = BTUSB_QCA_ROME |
278 BTUSB_WIDEBAND_SPEECH },
279 { USB_DEVICE(0x04ca, 0x3011), .driver_info = BTUSB_QCA_ROME |
280 BTUSB_WIDEBAND_SPEECH },
281 { USB_DEVICE(0x04ca, 0x3015), .driver_info = BTUSB_QCA_ROME |
282 BTUSB_WIDEBAND_SPEECH },
283 { USB_DEVICE(0x04ca, 0x3016), .driver_info = BTUSB_QCA_ROME |
284 BTUSB_WIDEBAND_SPEECH },
285 { USB_DEVICE(0x04ca, 0x301a), .driver_info = BTUSB_QCA_ROME |
286 BTUSB_WIDEBAND_SPEECH },
287 { USB_DEVICE(0x04ca, 0x3021), .driver_info = BTUSB_QCA_ROME |
288 BTUSB_WIDEBAND_SPEECH },
289 { USB_DEVICE(0x13d3, 0x3491), .driver_info = BTUSB_QCA_ROME |
290 BTUSB_WIDEBAND_SPEECH },
291 { USB_DEVICE(0x13d3, 0x3496), .driver_info = BTUSB_QCA_ROME |
292 BTUSB_WIDEBAND_SPEECH },
293 { USB_DEVICE(0x13d3, 0x3501), .driver_info = BTUSB_QCA_ROME |
294 BTUSB_WIDEBAND_SPEECH },
296 /* QCA WCN6855 chipset */
297 { USB_DEVICE(0x0cf3, 0xe600), .driver_info = BTUSB_QCA_WCN6855 |
298 BTUSB_WIDEBAND_SPEECH |
299 BTUSB_VALID_LE_STATES },
300 { USB_DEVICE(0x0489, 0xe0cc), .driver_info = BTUSB_QCA_WCN6855 |
301 BTUSB_WIDEBAND_SPEECH |
302 BTUSB_VALID_LE_STATES },
303 { USB_DEVICE(0x0489, 0xe0d6), .driver_info = BTUSB_QCA_WCN6855 |
304 BTUSB_WIDEBAND_SPEECH |
305 BTUSB_VALID_LE_STATES },
306 { USB_DEVICE(0x0489, 0xe0e3), .driver_info = BTUSB_QCA_WCN6855 |
307 BTUSB_WIDEBAND_SPEECH |
308 BTUSB_VALID_LE_STATES },
309 { USB_DEVICE(0x10ab, 0x9309), .driver_info = BTUSB_QCA_WCN6855 |
310 BTUSB_WIDEBAND_SPEECH |
311 BTUSB_VALID_LE_STATES },
312 { USB_DEVICE(0x10ab, 0x9409), .driver_info = BTUSB_QCA_WCN6855 |
313 BTUSB_WIDEBAND_SPEECH |
314 BTUSB_VALID_LE_STATES },
315 { USB_DEVICE(0x0489, 0xe0d0), .driver_info = BTUSB_QCA_WCN6855 |
316 BTUSB_WIDEBAND_SPEECH |
317 BTUSB_VALID_LE_STATES },
319 /* Broadcom BCM2035 */
320 { USB_DEVICE(0x0a5c, 0x2009), .driver_info = BTUSB_BCM92035 },
321 { USB_DEVICE(0x0a5c, 0x200a), .driver_info = BTUSB_WRONG_SCO_MTU },
322 { USB_DEVICE(0x0a5c, 0x2035), .driver_info = BTUSB_WRONG_SCO_MTU },
324 /* Broadcom BCM2045 */
325 { USB_DEVICE(0x0a5c, 0x2039), .driver_info = BTUSB_WRONG_SCO_MTU },
326 { USB_DEVICE(0x0a5c, 0x2101), .driver_info = BTUSB_WRONG_SCO_MTU },
328 /* IBM/Lenovo ThinkPad with Broadcom chip */
329 { USB_DEVICE(0x0a5c, 0x201e), .driver_info = BTUSB_WRONG_SCO_MTU },
330 { USB_DEVICE(0x0a5c, 0x2110), .driver_info = BTUSB_WRONG_SCO_MTU },
332 /* HP laptop with Broadcom chip */
333 { USB_DEVICE(0x03f0, 0x171d), .driver_info = BTUSB_WRONG_SCO_MTU },
335 /* Dell laptop with Broadcom chip */
336 { USB_DEVICE(0x413c, 0x8126), .driver_info = BTUSB_WRONG_SCO_MTU },
338 /* Dell Wireless 370 and 410 devices */
339 { USB_DEVICE(0x413c, 0x8152), .driver_info = BTUSB_WRONG_SCO_MTU },
340 { USB_DEVICE(0x413c, 0x8156), .driver_info = BTUSB_WRONG_SCO_MTU },
342 /* Belkin F8T012 and F8T013 devices */
343 { USB_DEVICE(0x050d, 0x0012), .driver_info = BTUSB_WRONG_SCO_MTU },
344 { USB_DEVICE(0x050d, 0x0013), .driver_info = BTUSB_WRONG_SCO_MTU },
346 /* Asus WL-BTD202 device */
347 { USB_DEVICE(0x0b05, 0x1715), .driver_info = BTUSB_WRONG_SCO_MTU },
349 /* Kensington Bluetooth USB adapter */
350 { USB_DEVICE(0x047d, 0x105e), .driver_info = BTUSB_WRONG_SCO_MTU },
352 /* RTX Telecom based adapters with buggy SCO support */
353 { USB_DEVICE(0x0400, 0x0807), .driver_info = BTUSB_BROKEN_ISOC },
354 { USB_DEVICE(0x0400, 0x080a), .driver_info = BTUSB_BROKEN_ISOC },
356 /* CONWISE Technology based adapters with buggy SCO support */
357 { USB_DEVICE(0x0e5e, 0x6622),
358 .driver_info = BTUSB_BROKEN_ISOC | BTUSB_CW6622},
360 /* Roper Class 1 Bluetooth Dongle (Silicon Wave based) */
361 { USB_DEVICE(0x1310, 0x0001), .driver_info = BTUSB_SWAVE },
363 /* Digianswer devices */
364 { USB_DEVICE(0x08fd, 0x0001), .driver_info = BTUSB_DIGIANSWER },
365 { USB_DEVICE(0x08fd, 0x0002), .driver_info = BTUSB_IGNORE },
367 /* CSR BlueCore Bluetooth Sniffer */
368 { USB_DEVICE(0x0a12, 0x0002),
369 .driver_info = BTUSB_SNIFFER | BTUSB_BROKEN_ISOC },
371 /* Frontline ComProbe Bluetooth Sniffer */
372 { USB_DEVICE(0x16d3, 0x0002),
373 .driver_info = BTUSB_SNIFFER | BTUSB_BROKEN_ISOC },
375 /* Marvell Bluetooth devices */
376 { USB_DEVICE(0x1286, 0x2044), .driver_info = BTUSB_MARVELL },
377 { USB_DEVICE(0x1286, 0x2046), .driver_info = BTUSB_MARVELL },
378 { USB_DEVICE(0x1286, 0x204e), .driver_info = BTUSB_MARVELL },
380 /* Intel Bluetooth devices */
381 { USB_DEVICE(0x8087, 0x0025), .driver_info = BTUSB_INTEL_COMBINED },
382 { USB_DEVICE(0x8087, 0x0026), .driver_info = BTUSB_INTEL_COMBINED },
383 { USB_DEVICE(0x8087, 0x0029), .driver_info = BTUSB_INTEL_COMBINED },
384 { USB_DEVICE(0x8087, 0x0032), .driver_info = BTUSB_INTEL_COMBINED },
385 { USB_DEVICE(0x8087, 0x0033), .driver_info = BTUSB_INTEL_COMBINED },
386 { USB_DEVICE(0x8087, 0x07da), .driver_info = BTUSB_CSR },
387 { USB_DEVICE(0x8087, 0x07dc), .driver_info = BTUSB_INTEL_COMBINED |
388 BTUSB_INTEL_BROKEN_INITIAL_NCMD |
389 BTUSB_INTEL_BROKEN_SHUTDOWN_LED },
390 { USB_DEVICE(0x8087, 0x0a2a), .driver_info = BTUSB_INTEL_COMBINED |
391 BTUSB_INTEL_BROKEN_SHUTDOWN_LED },
392 { USB_DEVICE(0x8087, 0x0a2b), .driver_info = BTUSB_INTEL_COMBINED },
393 { USB_DEVICE(0x8087, 0x0aa7), .driver_info = BTUSB_INTEL_COMBINED |
394 BTUSB_INTEL_BROKEN_SHUTDOWN_LED },
395 { USB_DEVICE(0x8087, 0x0aaa), .driver_info = BTUSB_INTEL_COMBINED },
397 /* Other Intel Bluetooth devices */
398 { USB_VENDOR_AND_INTERFACE_INFO(0x8087, 0xe0, 0x01, 0x01),
399 .driver_info = BTUSB_IGNORE },
401 /* Realtek 8822CE Bluetooth devices */
402 { USB_DEVICE(0x0bda, 0xb00c), .driver_info = BTUSB_REALTEK |
403 BTUSB_WIDEBAND_SPEECH },
404 { USB_DEVICE(0x0bda, 0xc822), .driver_info = BTUSB_REALTEK |
405 BTUSB_WIDEBAND_SPEECH },
407 /* Realtek 8852AE Bluetooth devices */
408 { USB_DEVICE(0x0bda, 0xc852), .driver_info = BTUSB_REALTEK |
409 BTUSB_WIDEBAND_SPEECH },
410 { USB_DEVICE(0x0bda, 0x385a), .driver_info = BTUSB_REALTEK |
411 BTUSB_WIDEBAND_SPEECH },
412 { USB_DEVICE(0x0bda, 0x4852), .driver_info = BTUSB_REALTEK |
413 BTUSB_WIDEBAND_SPEECH },
414 { USB_DEVICE(0x04c5, 0x165c), .driver_info = BTUSB_REALTEK |
415 BTUSB_WIDEBAND_SPEECH },
416 { USB_DEVICE(0x04ca, 0x4006), .driver_info = BTUSB_REALTEK |
417 BTUSB_WIDEBAND_SPEECH },
419 /* Realtek Bluetooth devices */
420 { USB_VENDOR_AND_INTERFACE_INFO(0x0bda, 0xe0, 0x01, 0x01),
421 .driver_info = BTUSB_REALTEK },
423 /* MediaTek Bluetooth devices */
424 { USB_VENDOR_AND_INTERFACE_INFO(0x0e8d, 0xe0, 0x01, 0x01),
425 .driver_info = BTUSB_MEDIATEK |
426 BTUSB_WIDEBAND_SPEECH |
427 BTUSB_VALID_LE_STATES },
429 /* Additional MediaTek MT7615E Bluetooth devices */
430 { USB_DEVICE(0x13d3, 0x3560), .driver_info = BTUSB_MEDIATEK},
432 /* Additional MediaTek MT7668 Bluetooth devices */
433 { USB_DEVICE(0x043e, 0x3109), .driver_info = BTUSB_MEDIATEK |
434 BTUSB_WIDEBAND_SPEECH |
435 BTUSB_VALID_LE_STATES },
437 /* Additional MediaTek MT7921 Bluetooth devices */
438 { USB_DEVICE(0x04ca, 0x3802), .driver_info = BTUSB_MEDIATEK |
439 BTUSB_WIDEBAND_SPEECH |
440 BTUSB_VALID_LE_STATES },
441 { USB_DEVICE(0x13d3, 0x3563), .driver_info = BTUSB_MEDIATEK |
442 BTUSB_WIDEBAND_SPEECH |
443 BTUSB_VALID_LE_STATES },
444 { USB_DEVICE(0x13d3, 0x3564), .driver_info = BTUSB_MEDIATEK |
445 BTUSB_WIDEBAND_SPEECH |
446 BTUSB_VALID_LE_STATES },
447 { USB_DEVICE(0x0489, 0xe0cd), .driver_info = BTUSB_MEDIATEK |
448 BTUSB_WIDEBAND_SPEECH |
449 BTUSB_VALID_LE_STATES },
451 /* MediaTek MT7922A Bluetooth devices */
452 { USB_DEVICE(0x0489, 0xe0d8), .driver_info = BTUSB_MEDIATEK |
453 BTUSB_WIDEBAND_SPEECH |
454 BTUSB_VALID_LE_STATES },
455 { USB_DEVICE(0x0489, 0xe0d9), .driver_info = BTUSB_MEDIATEK |
456 BTUSB_WIDEBAND_SPEECH |
457 BTUSB_VALID_LE_STATES },
459 /* Additional Realtek 8723AE Bluetooth devices */
460 { USB_DEVICE(0x0930, 0x021d), .driver_info = BTUSB_REALTEK },
461 { USB_DEVICE(0x13d3, 0x3394), .driver_info = BTUSB_REALTEK },
463 /* Additional Realtek 8723BE Bluetooth devices */
464 { USB_DEVICE(0x0489, 0xe085), .driver_info = BTUSB_REALTEK },
465 { USB_DEVICE(0x0489, 0xe08b), .driver_info = BTUSB_REALTEK },
466 { USB_DEVICE(0x13d3, 0x3410), .driver_info = BTUSB_REALTEK },
467 { USB_DEVICE(0x13d3, 0x3416), .driver_info = BTUSB_REALTEK },
468 { USB_DEVICE(0x13d3, 0x3459), .driver_info = BTUSB_REALTEK },
469 { USB_DEVICE(0x13d3, 0x3494), .driver_info = BTUSB_REALTEK },
471 /* Additional Realtek 8723BU Bluetooth devices */
472 { USB_DEVICE(0x7392, 0xa611), .driver_info = BTUSB_REALTEK },
474 /* Additional Realtek 8723DE Bluetooth devices */
475 { USB_DEVICE(0x0bda, 0xb009), .driver_info = BTUSB_REALTEK },
476 { USB_DEVICE(0x2ff8, 0xb011), .driver_info = BTUSB_REALTEK },
478 /* Additional Realtek 8761B Bluetooth devices */
479 { USB_DEVICE(0x2357, 0x0604), .driver_info = BTUSB_REALTEK |
480 BTUSB_WIDEBAND_SPEECH },
482 /* Additional Realtek 8761BU Bluetooth devices */
483 { USB_DEVICE(0x0b05, 0x190e), .driver_info = BTUSB_REALTEK |
484 BTUSB_WIDEBAND_SPEECH },
486 /* Additional Realtek 8821AE Bluetooth devices */
487 { USB_DEVICE(0x0b05, 0x17dc), .driver_info = BTUSB_REALTEK },
488 { USB_DEVICE(0x13d3, 0x3414), .driver_info = BTUSB_REALTEK },
489 { USB_DEVICE(0x13d3, 0x3458), .driver_info = BTUSB_REALTEK },
490 { USB_DEVICE(0x13d3, 0x3461), .driver_info = BTUSB_REALTEK },
491 { USB_DEVICE(0x13d3, 0x3462), .driver_info = BTUSB_REALTEK },
493 /* Additional Realtek 8822BE Bluetooth devices */
494 { USB_DEVICE(0x13d3, 0x3526), .driver_info = BTUSB_REALTEK },
495 { USB_DEVICE(0x0b05, 0x185c), .driver_info = BTUSB_REALTEK },
497 /* Additional Realtek 8822CE Bluetooth devices */
498 { USB_DEVICE(0x04ca, 0x4005), .driver_info = BTUSB_REALTEK |
499 BTUSB_WIDEBAND_SPEECH },
500 { USB_DEVICE(0x04c5, 0x161f), .driver_info = BTUSB_REALTEK |
501 BTUSB_WIDEBAND_SPEECH },
502 { USB_DEVICE(0x0b05, 0x18ef), .driver_info = BTUSB_REALTEK |
503 BTUSB_WIDEBAND_SPEECH },
504 { USB_DEVICE(0x13d3, 0x3548), .driver_info = BTUSB_REALTEK |
505 BTUSB_WIDEBAND_SPEECH },
506 { USB_DEVICE(0x13d3, 0x3549), .driver_info = BTUSB_REALTEK |
507 BTUSB_WIDEBAND_SPEECH },
508 { USB_DEVICE(0x13d3, 0x3553), .driver_info = BTUSB_REALTEK |
509 BTUSB_WIDEBAND_SPEECH },
510 { USB_DEVICE(0x13d3, 0x3555), .driver_info = BTUSB_REALTEK |
511 BTUSB_WIDEBAND_SPEECH },
512 { USB_DEVICE(0x2ff8, 0x3051), .driver_info = BTUSB_REALTEK |
513 BTUSB_WIDEBAND_SPEECH },
514 { USB_DEVICE(0x1358, 0xc123), .driver_info = BTUSB_REALTEK |
515 BTUSB_WIDEBAND_SPEECH },
516 { USB_DEVICE(0x0bda, 0xc123), .driver_info = BTUSB_REALTEK |
517 BTUSB_WIDEBAND_SPEECH },
518 { USB_DEVICE(0x0cb5, 0xc547), .driver_info = BTUSB_REALTEK |
519 BTUSB_WIDEBAND_SPEECH },
521 /* Silicon Wave based devices */
522 { USB_DEVICE(0x0c10, 0x0000), .driver_info = BTUSB_SWAVE },
524 { } /* Terminating entry */
527 /* The Bluetooth USB module build into some devices needs to be reset on resume,
528 * this is a problem with the platform (likely shutting off all power) not with
529 * the module itself. So we use a DMI list to match known broken platforms.
531 static const struct dmi_system_id btusb_needs_reset_resume_table[] = {
533 /* Dell OptiPlex 3060 (QCA ROME device 0cf3:e007) */
535 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
536 DMI_MATCH(DMI_PRODUCT_NAME, "OptiPlex 3060"),
540 /* Dell XPS 9360 (QCA ROME device 0cf3:e300) */
542 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
543 DMI_MATCH(DMI_PRODUCT_NAME, "XPS 13 9360"),
547 /* Dell Inspiron 5565 (QCA ROME device 0cf3:e009) */
549 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
550 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 5565"),
556 #define BTUSB_MAX_ISOC_FRAMES 10
558 #define BTUSB_INTR_RUNNING 0
559 #define BTUSB_BULK_RUNNING 1
560 #define BTUSB_ISOC_RUNNING 2
561 #define BTUSB_SUSPENDING 3
562 #define BTUSB_DID_ISO_RESUME 4
563 #define BTUSB_BOOTLOADER 5
564 #define BTUSB_DOWNLOADING 6
565 #define BTUSB_FIRMWARE_LOADED 7
566 #define BTUSB_FIRMWARE_FAILED 8
567 #define BTUSB_BOOTING 9
568 #define BTUSB_DIAG_RUNNING 10
569 #define BTUSB_OOB_WAKE_ENABLED 11
570 #define BTUSB_HW_RESET_ACTIVE 12
571 #define BTUSB_TX_WAIT_VND_EVT 13
572 #define BTUSB_WAKEUP_AUTOSUSPEND 14
573 #define BTUSB_USE_ALT3_FOR_WBS 15
576 struct hci_dev *hdev;
577 struct usb_device *udev;
578 struct usb_interface *intf;
579 struct usb_interface *isoc;
580 struct usb_interface *diag;
587 struct work_struct work;
588 struct work_struct waker;
589 struct delayed_work rx_work;
591 struct sk_buff_head acl_q;
593 struct usb_anchor deferred;
594 struct usb_anchor tx_anchor;
598 struct usb_anchor intr_anchor;
599 struct usb_anchor bulk_anchor;
600 struct usb_anchor isoc_anchor;
601 struct usb_anchor diag_anchor;
602 struct usb_anchor ctrl_anchor;
605 struct sk_buff *evt_skb;
606 struct sk_buff *acl_skb;
607 struct sk_buff *sco_skb;
609 struct usb_endpoint_descriptor *intr_ep;
610 struct usb_endpoint_descriptor *bulk_tx_ep;
611 struct usb_endpoint_descriptor *bulk_rx_ep;
612 struct usb_endpoint_descriptor *isoc_tx_ep;
613 struct usb_endpoint_descriptor *isoc_rx_ep;
614 struct usb_endpoint_descriptor *diag_tx_ep;
615 struct usb_endpoint_descriptor *diag_rx_ep;
617 struct gpio_desc *reset_gpio;
622 unsigned int sco_num;
623 unsigned int air_mode;
624 bool usb_alt6_packet_flow;
628 int (*recv_event)(struct hci_dev *hdev, struct sk_buff *skb);
629 int (*recv_acl)(struct hci_dev *hdev, struct sk_buff *skb);
630 int (*recv_bulk)(struct btusb_data *data, void *buffer, int count);
632 int (*setup_on_usb)(struct hci_dev *hdev);
634 int oob_wake_irq; /* irq for out-of-band wake-on-bt */
635 unsigned cmd_timeout_cnt;
638 static void btusb_intel_cmd_timeout(struct hci_dev *hdev)
640 struct btusb_data *data = hci_get_drvdata(hdev);
641 struct gpio_desc *reset_gpio = data->reset_gpio;
643 if (++data->cmd_timeout_cnt < 5)
647 bt_dev_err(hdev, "No way to reset. Ignoring and continuing");
652 * Toggle the hard reset line if the platform provides one. The reset
653 * is going to yank the device off the USB and then replug. So doing
654 * once is enough. The cleanup is handled correctly on the way out
655 * (standard USB disconnect), and the new device is detected cleanly
656 * and bound to the driver again like it should be.
658 if (test_and_set_bit(BTUSB_HW_RESET_ACTIVE, &data->flags)) {
659 bt_dev_err(hdev, "last reset failed? Not resetting again");
663 bt_dev_err(hdev, "Initiating HW reset via gpio");
664 gpiod_set_value_cansleep(reset_gpio, 1);
666 gpiod_set_value_cansleep(reset_gpio, 0);
669 static void btusb_rtl_cmd_timeout(struct hci_dev *hdev)
671 struct btusb_data *data = hci_get_drvdata(hdev);
672 struct gpio_desc *reset_gpio = data->reset_gpio;
674 if (++data->cmd_timeout_cnt < 5)
678 bt_dev_err(hdev, "No gpio to reset Realtek device, ignoring");
682 /* Toggle the hard reset line. The Realtek device is going to
683 * yank itself off the USB and then replug. The cleanup is handled
684 * correctly on the way out (standard USB disconnect), and the new
685 * device is detected cleanly and bound to the driver again like
688 if (test_and_set_bit(BTUSB_HW_RESET_ACTIVE, &data->flags)) {
689 bt_dev_err(hdev, "last reset failed? Not resetting again");
693 bt_dev_err(hdev, "Reset Realtek device via gpio");
694 gpiod_set_value_cansleep(reset_gpio, 1);
696 gpiod_set_value_cansleep(reset_gpio, 0);
699 static void btusb_qca_cmd_timeout(struct hci_dev *hdev)
701 struct btusb_data *data = hci_get_drvdata(hdev);
702 struct gpio_desc *reset_gpio = data->reset_gpio;
705 if (++data->cmd_timeout_cnt < 5)
709 bt_dev_err(hdev, "Reset qca device via bt_en gpio");
711 /* Toggle the hard reset line. The qca bt 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 gpiod_set_value_cansleep(reset_gpio, 0);
724 gpiod_set_value_cansleep(reset_gpio, 1);
729 bt_dev_err(hdev, "Multiple cmd timeouts seen. Resetting usb device.");
730 /* This is not an unbalanced PM reference since the device will reset */
731 err = usb_autopm_get_interface(data->intf);
733 usb_queue_reset_device(data->intf);
735 bt_dev_err(hdev, "Failed usb_autopm_get_interface with %d", err);
738 static inline void btusb_free_frags(struct btusb_data *data)
742 spin_lock_irqsave(&data->rxlock, flags);
744 kfree_skb(data->evt_skb);
745 data->evt_skb = NULL;
747 kfree_skb(data->acl_skb);
748 data->acl_skb = NULL;
750 kfree_skb(data->sco_skb);
751 data->sco_skb = NULL;
753 spin_unlock_irqrestore(&data->rxlock, flags);
756 static int btusb_recv_event(struct btusb_data *data, struct sk_buff *skb)
758 if (data->intr_interval) {
759 /* Trigger dequeue immediatelly if an event is received */
760 schedule_delayed_work(&data->rx_work, 0);
763 return data->recv_event(data->hdev, skb);
766 static int btusb_recv_intr(struct btusb_data *data, void *buffer, int count)
772 spin_lock_irqsave(&data->rxlock, flags);
779 skb = bt_skb_alloc(HCI_MAX_EVENT_SIZE, GFP_ATOMIC);
785 hci_skb_pkt_type(skb) = HCI_EVENT_PKT;
786 hci_skb_expect(skb) = HCI_EVENT_HDR_SIZE;
789 len = min_t(uint, hci_skb_expect(skb), count);
790 skb_put_data(skb, buffer, len);
794 hci_skb_expect(skb) -= len;
796 if (skb->len == HCI_EVENT_HDR_SIZE) {
797 /* Complete event header */
798 hci_skb_expect(skb) = hci_event_hdr(skb)->plen;
800 if (skb_tailroom(skb) < hci_skb_expect(skb)) {
809 if (!hci_skb_expect(skb)) {
811 btusb_recv_event(data, skb);
817 spin_unlock_irqrestore(&data->rxlock, flags);
822 static int btusb_recv_acl(struct btusb_data *data, struct sk_buff *skb)
824 /* Only queue ACL packet if intr_interval is set as it means
825 * force_poll_sync has been enabled.
827 if (!data->intr_interval)
828 return data->recv_acl(data->hdev, skb);
830 skb_queue_tail(&data->acl_q, skb);
831 schedule_delayed_work(&data->rx_work, data->intr_interval);
836 static int btusb_recv_bulk(struct btusb_data *data, void *buffer, int count)
842 spin_lock_irqsave(&data->rxlock, flags);
849 skb = bt_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC);
855 hci_skb_pkt_type(skb) = HCI_ACLDATA_PKT;
856 hci_skb_expect(skb) = HCI_ACL_HDR_SIZE;
859 len = min_t(uint, hci_skb_expect(skb), count);
860 skb_put_data(skb, buffer, len);
864 hci_skb_expect(skb) -= len;
866 if (skb->len == HCI_ACL_HDR_SIZE) {
867 __le16 dlen = hci_acl_hdr(skb)->dlen;
869 /* Complete ACL header */
870 hci_skb_expect(skb) = __le16_to_cpu(dlen);
872 if (skb_tailroom(skb) < hci_skb_expect(skb)) {
881 if (!hci_skb_expect(skb)) {
883 btusb_recv_acl(data, skb);
889 spin_unlock_irqrestore(&data->rxlock, flags);
894 static int btusb_recv_isoc(struct btusb_data *data, void *buffer, int count)
900 spin_lock_irqsave(&data->rxlock, flags);
907 skb = bt_skb_alloc(HCI_MAX_SCO_SIZE, GFP_ATOMIC);
913 hci_skb_pkt_type(skb) = HCI_SCODATA_PKT;
914 hci_skb_expect(skb) = HCI_SCO_HDR_SIZE;
917 len = min_t(uint, hci_skb_expect(skb), count);
918 skb_put_data(skb, buffer, len);
922 hci_skb_expect(skb) -= len;
924 if (skb->len == HCI_SCO_HDR_SIZE) {
925 /* Complete SCO header */
926 hci_skb_expect(skb) = hci_sco_hdr(skb)->dlen;
928 if (skb_tailroom(skb) < hci_skb_expect(skb)) {
937 if (!hci_skb_expect(skb)) {
939 hci_recv_frame(data->hdev, skb);
945 spin_unlock_irqrestore(&data->rxlock, flags);
950 static void btusb_intr_complete(struct urb *urb)
952 struct hci_dev *hdev = urb->context;
953 struct btusb_data *data = hci_get_drvdata(hdev);
956 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
959 if (!test_bit(HCI_RUNNING, &hdev->flags))
962 if (urb->status == 0) {
963 hdev->stat.byte_rx += urb->actual_length;
965 if (btusb_recv_intr(data, urb->transfer_buffer,
966 urb->actual_length) < 0) {
967 bt_dev_err(hdev, "corrupted event packet");
970 } else if (urb->status == -ENOENT) {
971 /* Avoid suspend failed when usb_kill_urb */
975 if (!test_bit(BTUSB_INTR_RUNNING, &data->flags))
978 usb_mark_last_busy(data->udev);
979 usb_anchor_urb(urb, &data->intr_anchor);
981 err = usb_submit_urb(urb, GFP_ATOMIC);
983 /* -EPERM: urb is being killed;
984 * -ENODEV: device got disconnected
986 if (err != -EPERM && err != -ENODEV)
987 bt_dev_err(hdev, "urb %p failed to resubmit (%d)",
990 hci_cmd_sync_cancel(hdev, -err);
991 usb_unanchor_urb(urb);
995 static int btusb_submit_intr_urb(struct hci_dev *hdev, gfp_t mem_flags)
997 struct btusb_data *data = hci_get_drvdata(hdev);
1003 BT_DBG("%s", hdev->name);
1008 urb = usb_alloc_urb(0, mem_flags);
1012 size = le16_to_cpu(data->intr_ep->wMaxPacketSize);
1014 buf = kmalloc(size, mem_flags);
1020 pipe = usb_rcvintpipe(data->udev, data->intr_ep->bEndpointAddress);
1022 usb_fill_int_urb(urb, data->udev, pipe, buf, size,
1023 btusb_intr_complete, hdev, data->intr_ep->bInterval);
1025 urb->transfer_flags |= URB_FREE_BUFFER;
1027 usb_anchor_urb(urb, &data->intr_anchor);
1029 err = usb_submit_urb(urb, mem_flags);
1031 if (err != -EPERM && err != -ENODEV)
1032 bt_dev_err(hdev, "urb %p submission failed (%d)",
1035 hci_cmd_sync_cancel(hdev, -err);
1036 usb_unanchor_urb(urb);
1039 /* Only initialize intr_interval if URB poll sync is enabled */
1040 if (!data->poll_sync)
1043 /* The units are frames (milliseconds) for full and low speed devices,
1044 * and microframes (1/8 millisecond) for highspeed and SuperSpeed
1047 * This is done once on open/resume so it shouldn't change even if
1048 * force_poll_sync changes.
1050 switch (urb->dev->speed) {
1051 case USB_SPEED_SUPER_PLUS:
1052 case USB_SPEED_SUPER: /* units are 125us */
1053 data->intr_interval = usecs_to_jiffies(urb->interval * 125);
1056 data->intr_interval = msecs_to_jiffies(urb->interval);
1066 static void btusb_bulk_complete(struct urb *urb)
1068 struct hci_dev *hdev = urb->context;
1069 struct btusb_data *data = hci_get_drvdata(hdev);
1072 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
1073 urb->actual_length);
1075 if (!test_bit(HCI_RUNNING, &hdev->flags))
1078 if (urb->status == 0) {
1079 hdev->stat.byte_rx += urb->actual_length;
1081 if (data->recv_bulk(data, urb->transfer_buffer,
1082 urb->actual_length) < 0) {
1083 bt_dev_err(hdev, "corrupted ACL packet");
1084 hdev->stat.err_rx++;
1086 } else if (urb->status == -ENOENT) {
1087 /* Avoid suspend failed when usb_kill_urb */
1091 if (!test_bit(BTUSB_BULK_RUNNING, &data->flags))
1094 usb_anchor_urb(urb, &data->bulk_anchor);
1095 usb_mark_last_busy(data->udev);
1097 err = usb_submit_urb(urb, GFP_ATOMIC);
1099 /* -EPERM: urb is being killed;
1100 * -ENODEV: device got disconnected
1102 if (err != -EPERM && err != -ENODEV)
1103 bt_dev_err(hdev, "urb %p failed to resubmit (%d)",
1105 usb_unanchor_urb(urb);
1109 static int btusb_submit_bulk_urb(struct hci_dev *hdev, gfp_t mem_flags)
1111 struct btusb_data *data = hci_get_drvdata(hdev);
1115 int err, size = HCI_MAX_FRAME_SIZE;
1117 BT_DBG("%s", hdev->name);
1119 if (!data->bulk_rx_ep)
1122 urb = usb_alloc_urb(0, mem_flags);
1126 buf = kmalloc(size, mem_flags);
1132 pipe = usb_rcvbulkpipe(data->udev, data->bulk_rx_ep->bEndpointAddress);
1134 usb_fill_bulk_urb(urb, data->udev, pipe, buf, size,
1135 btusb_bulk_complete, hdev);
1137 urb->transfer_flags |= URB_FREE_BUFFER;
1139 usb_mark_last_busy(data->udev);
1140 usb_anchor_urb(urb, &data->bulk_anchor);
1142 err = usb_submit_urb(urb, mem_flags);
1144 if (err != -EPERM && err != -ENODEV)
1145 bt_dev_err(hdev, "urb %p submission failed (%d)",
1147 usb_unanchor_urb(urb);
1155 static void btusb_isoc_complete(struct urb *urb)
1157 struct hci_dev *hdev = urb->context;
1158 struct btusb_data *data = hci_get_drvdata(hdev);
1161 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
1162 urb->actual_length);
1164 if (!test_bit(HCI_RUNNING, &hdev->flags))
1167 if (urb->status == 0) {
1168 for (i = 0; i < urb->number_of_packets; i++) {
1169 unsigned int offset = urb->iso_frame_desc[i].offset;
1170 unsigned int length = urb->iso_frame_desc[i].actual_length;
1172 if (urb->iso_frame_desc[i].status)
1175 hdev->stat.byte_rx += length;
1177 if (btusb_recv_isoc(data, urb->transfer_buffer + offset,
1179 bt_dev_err(hdev, "corrupted SCO packet");
1180 hdev->stat.err_rx++;
1183 } else if (urb->status == -ENOENT) {
1184 /* Avoid suspend failed when usb_kill_urb */
1188 if (!test_bit(BTUSB_ISOC_RUNNING, &data->flags))
1191 usb_anchor_urb(urb, &data->isoc_anchor);
1193 err = usb_submit_urb(urb, GFP_ATOMIC);
1195 /* -EPERM: urb is being killed;
1196 * -ENODEV: device got disconnected
1198 if (err != -EPERM && err != -ENODEV)
1199 bt_dev_err(hdev, "urb %p failed to resubmit (%d)",
1201 usb_unanchor_urb(urb);
1205 static inline void __fill_isoc_descriptor_msbc(struct urb *urb, int len,
1206 int mtu, struct btusb_data *data)
1209 unsigned int interval;
1211 BT_DBG("len %d mtu %d", len, mtu);
1213 /* For mSBC ALT 6 setting the host will send the packet at continuous
1214 * flow. As per core spec 5, vol 4, part B, table 2.1. For ALT setting
1215 * 6 the HCI PACKET INTERVAL should be 7.5ms for every usb packets.
1216 * To maintain the rate we send 63bytes of usb packets alternatively for
1217 * 7ms and 8ms to maintain the rate as 7.5ms.
1219 if (data->usb_alt6_packet_flow) {
1221 data->usb_alt6_packet_flow = false;
1224 data->usb_alt6_packet_flow = true;
1227 for (i = 0; i < interval; i++) {
1228 urb->iso_frame_desc[i].offset = offset;
1229 urb->iso_frame_desc[i].length = offset;
1232 if (len && i < BTUSB_MAX_ISOC_FRAMES) {
1233 urb->iso_frame_desc[i].offset = offset;
1234 urb->iso_frame_desc[i].length = len;
1238 urb->number_of_packets = i;
1241 static inline void __fill_isoc_descriptor(struct urb *urb, int len, int mtu)
1245 BT_DBG("len %d mtu %d", len, mtu);
1247 for (i = 0; i < BTUSB_MAX_ISOC_FRAMES && len >= mtu;
1248 i++, offset += mtu, len -= mtu) {
1249 urb->iso_frame_desc[i].offset = offset;
1250 urb->iso_frame_desc[i].length = mtu;
1253 if (len && i < BTUSB_MAX_ISOC_FRAMES) {
1254 urb->iso_frame_desc[i].offset = offset;
1255 urb->iso_frame_desc[i].length = len;
1259 urb->number_of_packets = i;
1262 static int btusb_submit_isoc_urb(struct hci_dev *hdev, gfp_t mem_flags)
1264 struct btusb_data *data = hci_get_drvdata(hdev);
1270 BT_DBG("%s", hdev->name);
1272 if (!data->isoc_rx_ep)
1275 urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, mem_flags);
1279 size = le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize) *
1280 BTUSB_MAX_ISOC_FRAMES;
1282 buf = kmalloc(size, mem_flags);
1288 pipe = usb_rcvisocpipe(data->udev, data->isoc_rx_ep->bEndpointAddress);
1290 usb_fill_int_urb(urb, data->udev, pipe, buf, size, btusb_isoc_complete,
1291 hdev, data->isoc_rx_ep->bInterval);
1293 urb->transfer_flags = URB_FREE_BUFFER | URB_ISO_ASAP;
1295 __fill_isoc_descriptor(urb, size,
1296 le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize));
1298 usb_anchor_urb(urb, &data->isoc_anchor);
1300 err = usb_submit_urb(urb, mem_flags);
1302 if (err != -EPERM && err != -ENODEV)
1303 bt_dev_err(hdev, "urb %p submission failed (%d)",
1305 usb_unanchor_urb(urb);
1313 static void btusb_diag_complete(struct urb *urb)
1315 struct hci_dev *hdev = urb->context;
1316 struct btusb_data *data = hci_get_drvdata(hdev);
1319 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
1320 urb->actual_length);
1322 if (urb->status == 0) {
1323 struct sk_buff *skb;
1325 skb = bt_skb_alloc(urb->actual_length, GFP_ATOMIC);
1327 skb_put_data(skb, urb->transfer_buffer,
1328 urb->actual_length);
1329 hci_recv_diag(hdev, skb);
1331 } else if (urb->status == -ENOENT) {
1332 /* Avoid suspend failed when usb_kill_urb */
1336 if (!test_bit(BTUSB_DIAG_RUNNING, &data->flags))
1339 usb_anchor_urb(urb, &data->diag_anchor);
1340 usb_mark_last_busy(data->udev);
1342 err = usb_submit_urb(urb, GFP_ATOMIC);
1344 /* -EPERM: urb is being killed;
1345 * -ENODEV: device got disconnected
1347 if (err != -EPERM && err != -ENODEV)
1348 bt_dev_err(hdev, "urb %p failed to resubmit (%d)",
1350 usb_unanchor_urb(urb);
1354 static int btusb_submit_diag_urb(struct hci_dev *hdev, gfp_t mem_flags)
1356 struct btusb_data *data = hci_get_drvdata(hdev);
1360 int err, size = HCI_MAX_FRAME_SIZE;
1362 BT_DBG("%s", hdev->name);
1364 if (!data->diag_rx_ep)
1367 urb = usb_alloc_urb(0, mem_flags);
1371 buf = kmalloc(size, mem_flags);
1377 pipe = usb_rcvbulkpipe(data->udev, data->diag_rx_ep->bEndpointAddress);
1379 usb_fill_bulk_urb(urb, data->udev, pipe, buf, size,
1380 btusb_diag_complete, hdev);
1382 urb->transfer_flags |= URB_FREE_BUFFER;
1384 usb_mark_last_busy(data->udev);
1385 usb_anchor_urb(urb, &data->diag_anchor);
1387 err = usb_submit_urb(urb, mem_flags);
1389 if (err != -EPERM && err != -ENODEV)
1390 bt_dev_err(hdev, "urb %p submission failed (%d)",
1392 usb_unanchor_urb(urb);
1400 static void btusb_tx_complete(struct urb *urb)
1402 struct sk_buff *skb = urb->context;
1403 struct hci_dev *hdev = (struct hci_dev *)skb->dev;
1404 struct btusb_data *data = hci_get_drvdata(hdev);
1405 unsigned long flags;
1407 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
1408 urb->actual_length);
1410 if (!test_bit(HCI_RUNNING, &hdev->flags))
1414 hdev->stat.byte_tx += urb->transfer_buffer_length;
1416 if (hci_skb_pkt_type(skb) == HCI_COMMAND_PKT)
1417 hci_cmd_sync_cancel(hdev, -urb->status);
1418 hdev->stat.err_tx++;
1422 spin_lock_irqsave(&data->txlock, flags);
1423 data->tx_in_flight--;
1424 spin_unlock_irqrestore(&data->txlock, flags);
1426 kfree(urb->setup_packet);
1431 static void btusb_isoc_tx_complete(struct urb *urb)
1433 struct sk_buff *skb = urb->context;
1434 struct hci_dev *hdev = (struct hci_dev *)skb->dev;
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 hdev->stat.err_tx++;
1448 kfree(urb->setup_packet);
1453 static int btusb_open(struct hci_dev *hdev)
1455 struct btusb_data *data = hci_get_drvdata(hdev);
1458 BT_DBG("%s", hdev->name);
1460 err = usb_autopm_get_interface(data->intf);
1464 /* Patching USB firmware files prior to starting any URBs of HCI path
1465 * It is more safe to use USB bulk channel for downloading USB patch
1467 if (data->setup_on_usb) {
1468 err = data->setup_on_usb(hdev);
1473 data->intf->needs_remote_wakeup = 1;
1475 if (test_and_set_bit(BTUSB_INTR_RUNNING, &data->flags))
1478 err = btusb_submit_intr_urb(hdev, GFP_KERNEL);
1482 err = btusb_submit_bulk_urb(hdev, GFP_KERNEL);
1484 usb_kill_anchored_urbs(&data->intr_anchor);
1488 set_bit(BTUSB_BULK_RUNNING, &data->flags);
1489 btusb_submit_bulk_urb(hdev, GFP_KERNEL);
1492 if (!btusb_submit_diag_urb(hdev, GFP_KERNEL))
1493 set_bit(BTUSB_DIAG_RUNNING, &data->flags);
1497 usb_autopm_put_interface(data->intf);
1501 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
1503 usb_autopm_put_interface(data->intf);
1507 static void btusb_stop_traffic(struct btusb_data *data)
1509 usb_kill_anchored_urbs(&data->intr_anchor);
1510 usb_kill_anchored_urbs(&data->bulk_anchor);
1511 usb_kill_anchored_urbs(&data->isoc_anchor);
1512 usb_kill_anchored_urbs(&data->diag_anchor);
1513 usb_kill_anchored_urbs(&data->ctrl_anchor);
1516 static int btusb_close(struct hci_dev *hdev)
1518 struct btusb_data *data = hci_get_drvdata(hdev);
1521 BT_DBG("%s", hdev->name);
1523 cancel_delayed_work(&data->rx_work);
1524 cancel_work_sync(&data->work);
1525 cancel_work_sync(&data->waker);
1527 skb_queue_purge(&data->acl_q);
1529 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1530 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
1531 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
1532 clear_bit(BTUSB_DIAG_RUNNING, &data->flags);
1534 btusb_stop_traffic(data);
1535 btusb_free_frags(data);
1537 err = usb_autopm_get_interface(data->intf);
1541 data->intf->needs_remote_wakeup = 0;
1543 /* Enable remote wake up for auto-suspend */
1544 if (test_bit(BTUSB_WAKEUP_AUTOSUSPEND, &data->flags))
1545 data->intf->needs_remote_wakeup = 1;
1547 usb_autopm_put_interface(data->intf);
1550 usb_scuttle_anchored_urbs(&data->deferred);
1554 static int btusb_flush(struct hci_dev *hdev)
1556 struct btusb_data *data = hci_get_drvdata(hdev);
1558 BT_DBG("%s", hdev->name);
1560 cancel_delayed_work(&data->rx_work);
1562 skb_queue_purge(&data->acl_q);
1564 usb_kill_anchored_urbs(&data->tx_anchor);
1565 btusb_free_frags(data);
1570 static struct urb *alloc_ctrl_urb(struct hci_dev *hdev, struct sk_buff *skb)
1572 struct btusb_data *data = hci_get_drvdata(hdev);
1573 struct usb_ctrlrequest *dr;
1577 urb = usb_alloc_urb(0, GFP_KERNEL);
1579 return ERR_PTR(-ENOMEM);
1581 dr = kmalloc(sizeof(*dr), GFP_KERNEL);
1584 return ERR_PTR(-ENOMEM);
1587 dr->bRequestType = data->cmdreq_type;
1588 dr->bRequest = data->cmdreq;
1591 dr->wLength = __cpu_to_le16(skb->len);
1593 pipe = usb_sndctrlpipe(data->udev, 0x00);
1595 usb_fill_control_urb(urb, data->udev, pipe, (void *)dr,
1596 skb->data, skb->len, btusb_tx_complete, skb);
1598 skb->dev = (void *)hdev;
1603 static struct urb *alloc_bulk_urb(struct hci_dev *hdev, struct sk_buff *skb)
1605 struct btusb_data *data = hci_get_drvdata(hdev);
1609 if (!data->bulk_tx_ep)
1610 return ERR_PTR(-ENODEV);
1612 urb = usb_alloc_urb(0, GFP_KERNEL);
1614 return ERR_PTR(-ENOMEM);
1616 pipe = usb_sndbulkpipe(data->udev, data->bulk_tx_ep->bEndpointAddress);
1618 usb_fill_bulk_urb(urb, data->udev, pipe,
1619 skb->data, skb->len, btusb_tx_complete, skb);
1621 skb->dev = (void *)hdev;
1626 static struct urb *alloc_isoc_urb(struct hci_dev *hdev, struct sk_buff *skb)
1628 struct btusb_data *data = hci_get_drvdata(hdev);
1632 if (!data->isoc_tx_ep)
1633 return ERR_PTR(-ENODEV);
1635 urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, GFP_KERNEL);
1637 return ERR_PTR(-ENOMEM);
1639 pipe = usb_sndisocpipe(data->udev, data->isoc_tx_ep->bEndpointAddress);
1641 usb_fill_int_urb(urb, data->udev, pipe,
1642 skb->data, skb->len, btusb_isoc_tx_complete,
1643 skb, data->isoc_tx_ep->bInterval);
1645 urb->transfer_flags = URB_ISO_ASAP;
1647 if (data->isoc_altsetting == 6)
1648 __fill_isoc_descriptor_msbc(urb, skb->len,
1649 le16_to_cpu(data->isoc_tx_ep->wMaxPacketSize),
1652 __fill_isoc_descriptor(urb, skb->len,
1653 le16_to_cpu(data->isoc_tx_ep->wMaxPacketSize));
1654 skb->dev = (void *)hdev;
1659 static int submit_tx_urb(struct hci_dev *hdev, struct urb *urb)
1661 struct btusb_data *data = hci_get_drvdata(hdev);
1664 usb_anchor_urb(urb, &data->tx_anchor);
1666 err = usb_submit_urb(urb, GFP_KERNEL);
1668 if (err != -EPERM && err != -ENODEV)
1669 bt_dev_err(hdev, "urb %p submission failed (%d)",
1671 kfree(urb->setup_packet);
1672 usb_unanchor_urb(urb);
1674 usb_mark_last_busy(data->udev);
1681 static int submit_or_queue_tx_urb(struct hci_dev *hdev, struct urb *urb)
1683 struct btusb_data *data = hci_get_drvdata(hdev);
1684 unsigned long flags;
1687 spin_lock_irqsave(&data->txlock, flags);
1688 suspending = test_bit(BTUSB_SUSPENDING, &data->flags);
1690 data->tx_in_flight++;
1691 spin_unlock_irqrestore(&data->txlock, flags);
1694 return submit_tx_urb(hdev, urb);
1696 usb_anchor_urb(urb, &data->deferred);
1697 schedule_work(&data->waker);
1703 static int btusb_send_frame(struct hci_dev *hdev, struct sk_buff *skb)
1707 BT_DBG("%s", hdev->name);
1709 switch (hci_skb_pkt_type(skb)) {
1710 case HCI_COMMAND_PKT:
1711 urb = alloc_ctrl_urb(hdev, skb);
1713 return PTR_ERR(urb);
1715 hdev->stat.cmd_tx++;
1716 return submit_or_queue_tx_urb(hdev, urb);
1718 case HCI_ACLDATA_PKT:
1719 urb = alloc_bulk_urb(hdev, skb);
1721 return PTR_ERR(urb);
1723 hdev->stat.acl_tx++;
1724 return submit_or_queue_tx_urb(hdev, urb);
1726 case HCI_SCODATA_PKT:
1727 if (hci_conn_num(hdev, SCO_LINK) < 1)
1730 urb = alloc_isoc_urb(hdev, skb);
1732 return PTR_ERR(urb);
1734 hdev->stat.sco_tx++;
1735 return submit_tx_urb(hdev, urb);
1741 static void btusb_notify(struct hci_dev *hdev, unsigned int evt)
1743 struct btusb_data *data = hci_get_drvdata(hdev);
1745 BT_DBG("%s evt %d", hdev->name, evt);
1747 if (hci_conn_num(hdev, SCO_LINK) != data->sco_num) {
1748 data->sco_num = hci_conn_num(hdev, SCO_LINK);
1749 data->air_mode = evt;
1750 schedule_work(&data->work);
1754 static inline int __set_isoc_interface(struct hci_dev *hdev, int altsetting)
1756 struct btusb_data *data = hci_get_drvdata(hdev);
1757 struct usb_interface *intf = data->isoc;
1758 struct usb_endpoint_descriptor *ep_desc;
1764 err = usb_set_interface(data->udev, data->isoc_ifnum, altsetting);
1766 bt_dev_err(hdev, "setting interface failed (%d)", -err);
1770 data->isoc_altsetting = altsetting;
1772 data->isoc_tx_ep = NULL;
1773 data->isoc_rx_ep = NULL;
1775 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
1776 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
1778 if (!data->isoc_tx_ep && usb_endpoint_is_isoc_out(ep_desc)) {
1779 data->isoc_tx_ep = ep_desc;
1783 if (!data->isoc_rx_ep && usb_endpoint_is_isoc_in(ep_desc)) {
1784 data->isoc_rx_ep = ep_desc;
1789 if (!data->isoc_tx_ep || !data->isoc_rx_ep) {
1790 bt_dev_err(hdev, "invalid SCO descriptors");
1797 static int btusb_switch_alt_setting(struct hci_dev *hdev, int new_alts)
1799 struct btusb_data *data = hci_get_drvdata(hdev);
1802 if (data->isoc_altsetting != new_alts) {
1803 unsigned long flags;
1805 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1806 usb_kill_anchored_urbs(&data->isoc_anchor);
1808 /* When isochronous alternate setting needs to be
1809 * changed, because SCO connection has been added
1810 * or removed, a packet fragment may be left in the
1811 * reassembling state. This could lead to wrongly
1812 * assembled fragments.
1814 * Clear outstanding fragment when selecting a new
1815 * alternate setting.
1817 spin_lock_irqsave(&data->rxlock, flags);
1818 kfree_skb(data->sco_skb);
1819 data->sco_skb = NULL;
1820 spin_unlock_irqrestore(&data->rxlock, flags);
1822 err = __set_isoc_interface(hdev, new_alts);
1827 if (!test_and_set_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
1828 if (btusb_submit_isoc_urb(hdev, GFP_KERNEL) < 0)
1829 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1831 btusb_submit_isoc_urb(hdev, GFP_KERNEL);
1837 static struct usb_host_interface *btusb_find_altsetting(struct btusb_data *data,
1840 struct usb_interface *intf = data->isoc;
1843 BT_DBG("Looking for Alt no :%d", alt);
1848 for (i = 0; i < intf->num_altsetting; i++) {
1849 if (intf->altsetting[i].desc.bAlternateSetting == alt)
1850 return &intf->altsetting[i];
1856 static void btusb_work(struct work_struct *work)
1858 struct btusb_data *data = container_of(work, struct btusb_data, work);
1859 struct hci_dev *hdev = data->hdev;
1863 if (data->sco_num > 0) {
1864 if (!test_bit(BTUSB_DID_ISO_RESUME, &data->flags)) {
1865 err = usb_autopm_get_interface(data->isoc ? data->isoc : data->intf);
1867 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1868 usb_kill_anchored_urbs(&data->isoc_anchor);
1872 set_bit(BTUSB_DID_ISO_RESUME, &data->flags);
1875 if (data->air_mode == HCI_NOTIFY_ENABLE_SCO_CVSD) {
1876 if (hdev->voice_setting & 0x0020) {
1877 static const int alts[3] = { 2, 4, 5 };
1879 new_alts = alts[data->sco_num - 1];
1881 new_alts = data->sco_num;
1883 } else if (data->air_mode == HCI_NOTIFY_ENABLE_SCO_TRANSP) {
1884 /* Bluetooth USB spec recommends alt 6 (63 bytes), but
1885 * many adapters do not support it. Alt 1 appears to
1886 * work for all adapters that do not have alt 6, and
1887 * which work with WBS at all. Some devices prefer
1888 * alt 3 (HCI payload >= 60 Bytes let air packet
1889 * data satisfy 60 bytes), requiring
1890 * MTU >= 3 (packets) * 25 (size) - 3 (headers) = 72
1891 * see also Core spec 5, vol 4, B 2.1.1 & Table 2.1.
1893 if (btusb_find_altsetting(data, 6))
1895 else if (btusb_find_altsetting(data, 3) &&
1896 hdev->sco_mtu >= 72 &&
1897 test_bit(BTUSB_USE_ALT3_FOR_WBS, &data->flags))
1903 if (btusb_switch_alt_setting(hdev, new_alts) < 0)
1904 bt_dev_err(hdev, "set USB alt:(%d) failed!", new_alts);
1906 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1907 usb_kill_anchored_urbs(&data->isoc_anchor);
1909 __set_isoc_interface(hdev, 0);
1910 if (test_and_clear_bit(BTUSB_DID_ISO_RESUME, &data->flags))
1911 usb_autopm_put_interface(data->isoc ? data->isoc : data->intf);
1915 static void btusb_waker(struct work_struct *work)
1917 struct btusb_data *data = container_of(work, struct btusb_data, waker);
1920 err = usb_autopm_get_interface(data->intf);
1924 usb_autopm_put_interface(data->intf);
1927 static void btusb_rx_work(struct work_struct *work)
1929 struct btusb_data *data = container_of(work, struct btusb_data,
1931 struct sk_buff *skb;
1933 /* Dequeue ACL data received during the interval */
1934 while ((skb = skb_dequeue(&data->acl_q)))
1935 data->recv_acl(data->hdev, skb);
1938 static int btusb_setup_bcm92035(struct hci_dev *hdev)
1940 struct sk_buff *skb;
1943 BT_DBG("%s", hdev->name);
1945 skb = __hci_cmd_sync(hdev, 0xfc3b, 1, &val, HCI_INIT_TIMEOUT);
1947 bt_dev_err(hdev, "BCM92035 command failed (%ld)", PTR_ERR(skb));
1954 static int btusb_setup_csr(struct hci_dev *hdev)
1956 struct btusb_data *data = hci_get_drvdata(hdev);
1957 u16 bcdDevice = le16_to_cpu(data->udev->descriptor.bcdDevice);
1958 struct hci_rp_read_local_version *rp;
1959 struct sk_buff *skb;
1960 bool is_fake = false;
1963 BT_DBG("%s", hdev->name);
1965 skb = __hci_cmd_sync(hdev, HCI_OP_READ_LOCAL_VERSION, 0, NULL,
1968 int err = PTR_ERR(skb);
1969 bt_dev_err(hdev, "CSR: Local version failed (%d)", err);
1973 if (skb->len != sizeof(struct hci_rp_read_local_version)) {
1974 bt_dev_err(hdev, "CSR: Local version length mismatch");
1979 rp = (struct hci_rp_read_local_version *)skb->data;
1981 /* Detect a wide host of Chinese controllers that aren't CSR.
1983 * Known fake bcdDevices: 0x0100, 0x0134, 0x1915, 0x2520, 0x7558, 0x8891
1985 * The main thing they have in common is that these are really popular low-cost
1986 * options that support newer Bluetooth versions but rely on heavy VID/PID
1987 * squatting of this poor old Bluetooth 1.1 device. Even sold as such.
1989 * We detect actual CSR devices by checking that the HCI manufacturer code
1990 * is Cambridge Silicon Radio (10) and ensuring that LMP sub-version and
1991 * HCI rev values always match. As they both store the firmware number.
1993 if (le16_to_cpu(rp->manufacturer) != 10 ||
1994 le16_to_cpu(rp->hci_rev) != le16_to_cpu(rp->lmp_subver))
1997 /* Known legit CSR firmware build numbers and their supported BT versions:
1998 * - 1.1 (0x1) -> 0x0073, 0x020d, 0x033c, 0x034e
1999 * - 1.2 (0x2) -> 0x04d9, 0x0529
2000 * - 2.0 (0x3) -> 0x07a6, 0x07ad, 0x0c5c
2001 * - 2.1 (0x4) -> 0x149c, 0x1735, 0x1899 (0x1899 is a BlueCore4-External)
2002 * - 4.0 (0x6) -> 0x1d86, 0x2031, 0x22bb
2004 * e.g. Real CSR dongles with LMP subversion 0x73 are old enough that
2005 * support BT 1.1 only; so it's a dead giveaway when some
2006 * third-party BT 4.0 dongle reuses it.
2008 else if (le16_to_cpu(rp->lmp_subver) <= 0x034e &&
2009 le16_to_cpu(rp->hci_ver) > BLUETOOTH_VER_1_1)
2012 else if (le16_to_cpu(rp->lmp_subver) <= 0x0529 &&
2013 le16_to_cpu(rp->hci_ver) > BLUETOOTH_VER_1_2)
2016 else if (le16_to_cpu(rp->lmp_subver) <= 0x0c5c &&
2017 le16_to_cpu(rp->hci_ver) > BLUETOOTH_VER_2_0)
2020 else if (le16_to_cpu(rp->lmp_subver) <= 0x1899 &&
2021 le16_to_cpu(rp->hci_ver) > BLUETOOTH_VER_2_1)
2024 else if (le16_to_cpu(rp->lmp_subver) <= 0x22bb &&
2025 le16_to_cpu(rp->hci_ver) > BLUETOOTH_VER_4_0)
2028 /* Other clones which beat all the above checks */
2029 else if (bcdDevice == 0x0134 &&
2030 le16_to_cpu(rp->lmp_subver) == 0x0c5c &&
2031 le16_to_cpu(rp->hci_ver) == BLUETOOTH_VER_2_0)
2035 bt_dev_warn(hdev, "CSR: Unbranded CSR clone detected; adding workarounds and force-suspending once...");
2037 /* Generally these clones have big discrepancies between
2038 * advertised features and what's actually supported.
2039 * Probably will need to be expanded in the future;
2040 * without these the controller will lock up.
2042 set_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks);
2043 set_bit(HCI_QUIRK_BROKEN_ERR_DATA_REPORTING, &hdev->quirks);
2045 /* Clear the reset quirk since this is not an actual
2046 * early Bluetooth 1.1 device from CSR.
2048 clear_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
2049 clear_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
2052 * Special workaround for these BT 4.0 chip clones, and potentially more:
2054 * - 0x0134: a Barrot 8041a02 (HCI rev: 0x1012 sub: 0x0810)
2055 * - 0x7558: IC markings FR3191AHAL 749H15143 (HCI rev/sub-version: 0x0709)
2057 * These controllers are really messed-up.
2059 * 1. Their bulk RX endpoint will never report any data unless
2060 * the device was suspended at least once (yes, really).
2061 * 2. They will not wakeup when autosuspended and receiving data
2062 * on their bulk RX endpoint from e.g. a keyboard or mouse
2063 * (IOW remote-wakeup support is broken for the bulk endpoint).
2065 * To fix 1. enable runtime-suspend, force-suspend the
2066 * HCI and then wake-it up by disabling runtime-suspend.
2068 * To fix 2. clear the HCI's can_wake flag, this way the HCI
2069 * will still be autosuspended when it is not open.
2073 * Because these are widespread problems we prefer generic solutions; so
2074 * apply this initialization quirk to every controller that gets here,
2075 * it should be harmless. The alternative is to not work at all.
2077 pm_runtime_allow(&data->udev->dev);
2079 ret = pm_runtime_suspend(&data->udev->dev);
2083 bt_dev_err(hdev, "CSR: Failed to suspend the device for our Barrot 8041a02 receive-issue workaround");
2085 pm_runtime_forbid(&data->udev->dev);
2087 device_set_wakeup_capable(&data->udev->dev, false);
2089 /* Re-enable autosuspend if this was requested */
2090 if (enable_autosuspend)
2091 usb_enable_autosuspend(data->udev);
2099 static int inject_cmd_complete(struct hci_dev *hdev, __u16 opcode)
2101 struct sk_buff *skb;
2102 struct hci_event_hdr *hdr;
2103 struct hci_ev_cmd_complete *evt;
2105 skb = bt_skb_alloc(sizeof(*hdr) + sizeof(*evt) + 1, GFP_KERNEL);
2109 hdr = skb_put(skb, sizeof(*hdr));
2110 hdr->evt = HCI_EV_CMD_COMPLETE;
2111 hdr->plen = sizeof(*evt) + 1;
2113 evt = skb_put(skb, sizeof(*evt));
2115 evt->opcode = cpu_to_le16(opcode);
2117 skb_put_u8(skb, 0x00);
2119 hci_skb_pkt_type(skb) = HCI_EVENT_PKT;
2121 return hci_recv_frame(hdev, skb);
2124 static int btusb_recv_bulk_intel(struct btusb_data *data, void *buffer,
2127 struct hci_dev *hdev = data->hdev;
2129 /* When the device is in bootloader mode, then it can send
2130 * events via the bulk endpoint. These events are treated the
2131 * same way as the ones received from the interrupt endpoint.
2133 if (btintel_test_flag(hdev, INTEL_BOOTLOADER))
2134 return btusb_recv_intr(data, buffer, count);
2136 return btusb_recv_bulk(data, buffer, count);
2139 static int btusb_recv_event_intel(struct hci_dev *hdev, struct sk_buff *skb)
2141 if (btintel_test_flag(hdev, INTEL_BOOTLOADER)) {
2142 struct hci_event_hdr *hdr = (void *)skb->data;
2144 if (skb->len > HCI_EVENT_HDR_SIZE && hdr->evt == 0xff &&
2146 const void *ptr = skb->data + HCI_EVENT_HDR_SIZE + 1;
2147 unsigned int len = skb->len - HCI_EVENT_HDR_SIZE - 1;
2149 switch (skb->data[2]) {
2151 /* When switching to the operational firmware
2152 * the device sends a vendor specific event
2153 * indicating that the bootup completed.
2155 btintel_bootup(hdev, ptr, len);
2158 /* When the firmware loading completes the
2159 * device sends out a vendor specific event
2160 * indicating the result of the firmware
2163 btintel_secure_send_result(hdev, ptr, len);
2169 return hci_recv_frame(hdev, skb);
2172 static int btusb_send_frame_intel(struct hci_dev *hdev, struct sk_buff *skb)
2176 BT_DBG("%s", hdev->name);
2178 switch (hci_skb_pkt_type(skb)) {
2179 case HCI_COMMAND_PKT:
2180 if (btintel_test_flag(hdev, INTEL_BOOTLOADER)) {
2181 struct hci_command_hdr *cmd = (void *)skb->data;
2182 __u16 opcode = le16_to_cpu(cmd->opcode);
2184 /* When in bootloader mode and the command 0xfc09
2185 * is received, it needs to be send down the
2186 * bulk endpoint. So allocate a bulk URB instead.
2188 if (opcode == 0xfc09)
2189 urb = alloc_bulk_urb(hdev, skb);
2191 urb = alloc_ctrl_urb(hdev, skb);
2193 /* When the 0xfc01 command is issued to boot into
2194 * the operational firmware, it will actually not
2195 * send a command complete event. To keep the flow
2196 * control working inject that event here.
2198 if (opcode == 0xfc01)
2199 inject_cmd_complete(hdev, opcode);
2201 urb = alloc_ctrl_urb(hdev, skb);
2204 return PTR_ERR(urb);
2206 hdev->stat.cmd_tx++;
2207 return submit_or_queue_tx_urb(hdev, urb);
2209 case HCI_ACLDATA_PKT:
2210 urb = alloc_bulk_urb(hdev, skb);
2212 return PTR_ERR(urb);
2214 hdev->stat.acl_tx++;
2215 return submit_or_queue_tx_urb(hdev, urb);
2217 case HCI_SCODATA_PKT:
2218 if (hci_conn_num(hdev, SCO_LINK) < 1)
2221 urb = alloc_isoc_urb(hdev, skb);
2223 return PTR_ERR(urb);
2225 hdev->stat.sco_tx++;
2226 return submit_tx_urb(hdev, urb);
2232 /* UHW CR mapping */
2233 #define MTK_BT_MISC 0x70002510
2234 #define MTK_BT_SUBSYS_RST 0x70002610
2235 #define MTK_UDMA_INT_STA_BT 0x74000024
2236 #define MTK_UDMA_INT_STA_BT1 0x74000308
2237 #define MTK_BT_WDT_STATUS 0x740003A0
2238 #define MTK_EP_RST_OPT 0x74011890
2239 #define MTK_EP_RST_IN_OUT_OPT 0x00010001
2240 #define MTK_BT_RST_DONE 0x00000100
2241 #define MTK_BT_RESET_WAIT_MS 100
2242 #define MTK_BT_RESET_NUM_TRIES 10
2244 static void btusb_mtk_wmt_recv(struct urb *urb)
2246 struct hci_dev *hdev = urb->context;
2247 struct btusb_data *data = hci_get_drvdata(hdev);
2248 struct hci_event_hdr *hdr;
2249 struct sk_buff *skb;
2252 if (urb->status == 0 && urb->actual_length > 0) {
2253 hdev->stat.byte_rx += urb->actual_length;
2255 /* WMT event shouldn't be fragmented and the size should be
2256 * less than HCI_WMT_MAX_EVENT_SIZE.
2258 skb = bt_skb_alloc(HCI_WMT_MAX_EVENT_SIZE, GFP_ATOMIC);
2260 hdev->stat.err_rx++;
2261 kfree(urb->setup_packet);
2265 hci_skb_pkt_type(skb) = HCI_EVENT_PKT;
2266 skb_put_data(skb, urb->transfer_buffer, urb->actual_length);
2268 hdr = (void *)skb->data;
2269 /* Fix up the vendor event id with 0xff for vendor specific
2270 * instead of 0xe4 so that event send via monitoring socket can
2271 * be parsed properly.
2275 /* When someone waits for the WMT event, the skb is being cloned
2276 * and being processed the events from there then.
2278 if (test_bit(BTUSB_TX_WAIT_VND_EVT, &data->flags)) {
2279 data->evt_skb = skb_clone(skb, GFP_ATOMIC);
2280 if (!data->evt_skb) {
2282 kfree(urb->setup_packet);
2287 err = hci_recv_frame(hdev, skb);
2289 kfree_skb(data->evt_skb);
2290 data->evt_skb = NULL;
2291 kfree(urb->setup_packet);
2295 if (test_and_clear_bit(BTUSB_TX_WAIT_VND_EVT,
2297 /* Barrier to sync with other CPUs */
2298 smp_mb__after_atomic();
2299 wake_up_bit(&data->flags,
2300 BTUSB_TX_WAIT_VND_EVT);
2302 kfree(urb->setup_packet);
2304 } else if (urb->status == -ENOENT) {
2305 /* Avoid suspend failed when usb_kill_urb */
2309 usb_mark_last_busy(data->udev);
2311 /* The URB complete handler is still called with urb->actual_length = 0
2312 * when the event is not available, so we should keep re-submitting
2313 * URB until WMT event returns, Also, It's necessary to wait some time
2314 * between the two consecutive control URBs to relax the target device
2315 * to generate the event. Otherwise, the WMT event cannot return from
2316 * the device successfully.
2320 usb_anchor_urb(urb, &data->ctrl_anchor);
2321 err = usb_submit_urb(urb, GFP_ATOMIC);
2323 kfree(urb->setup_packet);
2324 /* -EPERM: urb is being killed;
2325 * -ENODEV: device got disconnected
2327 if (err != -EPERM && err != -ENODEV)
2328 bt_dev_err(hdev, "urb %p failed to resubmit (%d)",
2330 usb_unanchor_urb(urb);
2334 static int btusb_mtk_submit_wmt_recv_urb(struct hci_dev *hdev)
2336 struct btusb_data *data = hci_get_drvdata(hdev);
2337 struct usb_ctrlrequest *dr;
2343 urb = usb_alloc_urb(0, GFP_KERNEL);
2347 dr = kmalloc(sizeof(*dr), GFP_KERNEL);
2353 dr->bRequestType = USB_TYPE_VENDOR | USB_DIR_IN;
2355 dr->wIndex = cpu_to_le16(0);
2356 dr->wValue = cpu_to_le16(48);
2357 dr->wLength = cpu_to_le16(size);
2359 buf = kmalloc(size, GFP_KERNEL);
2366 pipe = usb_rcvctrlpipe(data->udev, 0);
2368 usb_fill_control_urb(urb, data->udev, pipe, (void *)dr,
2369 buf, size, btusb_mtk_wmt_recv, hdev);
2371 urb->transfer_flags |= URB_FREE_BUFFER;
2373 usb_anchor_urb(urb, &data->ctrl_anchor);
2374 err = usb_submit_urb(urb, GFP_KERNEL);
2376 if (err != -EPERM && err != -ENODEV)
2377 bt_dev_err(hdev, "urb %p submission failed (%d)",
2379 usb_unanchor_urb(urb);
2387 static int btusb_mtk_hci_wmt_sync(struct hci_dev *hdev,
2388 struct btmtk_hci_wmt_params *wmt_params)
2390 struct btusb_data *data = hci_get_drvdata(hdev);
2391 struct btmtk_hci_wmt_evt_funcc *wmt_evt_funcc;
2392 u32 hlen, status = BTMTK_WMT_INVALID;
2393 struct btmtk_hci_wmt_evt *wmt_evt;
2394 struct btmtk_hci_wmt_cmd *wc;
2395 struct btmtk_wmt_hdr *hdr;
2398 /* Send the WMT command and wait until the WMT event returns */
2399 hlen = sizeof(*hdr) + wmt_params->dlen;
2403 wc = kzalloc(hlen, GFP_KERNEL);
2409 hdr->op = wmt_params->op;
2410 hdr->dlen = cpu_to_le16(wmt_params->dlen + 1);
2411 hdr->flag = wmt_params->flag;
2412 memcpy(wc->data, wmt_params->data, wmt_params->dlen);
2414 set_bit(BTUSB_TX_WAIT_VND_EVT, &data->flags);
2416 err = __hci_cmd_send(hdev, 0xfc6f, hlen, wc);
2419 clear_bit(BTUSB_TX_WAIT_VND_EVT, &data->flags);
2423 /* Submit control IN URB on demand to process the WMT event */
2424 err = btusb_mtk_submit_wmt_recv_urb(hdev);
2428 /* The vendor specific WMT commands are all answered by a vendor
2429 * specific event and will have the Command Status or Command
2430 * Complete as with usual HCI command flow control.
2432 * After sending the command, wait for BTUSB_TX_WAIT_VND_EVT
2433 * state to be cleared. The driver specific event receive routine
2434 * will clear that state and with that indicate completion of the
2437 err = wait_on_bit_timeout(&data->flags, BTUSB_TX_WAIT_VND_EVT,
2438 TASK_INTERRUPTIBLE, HCI_INIT_TIMEOUT);
2439 if (err == -EINTR) {
2440 bt_dev_err(hdev, "Execution of wmt command interrupted");
2441 clear_bit(BTUSB_TX_WAIT_VND_EVT, &data->flags);
2446 bt_dev_err(hdev, "Execution of wmt command timed out");
2447 clear_bit(BTUSB_TX_WAIT_VND_EVT, &data->flags);
2452 /* Parse and handle the return WMT event */
2453 wmt_evt = (struct btmtk_hci_wmt_evt *)data->evt_skb->data;
2454 if (wmt_evt->whdr.op != hdr->op) {
2455 bt_dev_err(hdev, "Wrong op received %d expected %d",
2456 wmt_evt->whdr.op, hdr->op);
2461 switch (wmt_evt->whdr.op) {
2462 case BTMTK_WMT_SEMAPHORE:
2463 if (wmt_evt->whdr.flag == 2)
2464 status = BTMTK_WMT_PATCH_UNDONE;
2466 status = BTMTK_WMT_PATCH_DONE;
2468 case BTMTK_WMT_FUNC_CTRL:
2469 wmt_evt_funcc = (struct btmtk_hci_wmt_evt_funcc *)wmt_evt;
2470 if (be16_to_cpu(wmt_evt_funcc->status) == 0x404)
2471 status = BTMTK_WMT_ON_DONE;
2472 else if (be16_to_cpu(wmt_evt_funcc->status) == 0x420)
2473 status = BTMTK_WMT_ON_PROGRESS;
2475 status = BTMTK_WMT_ON_UNDONE;
2477 case BTMTK_WMT_PATCH_DWNLD:
2478 if (wmt_evt->whdr.flag == 2)
2479 status = BTMTK_WMT_PATCH_DONE;
2480 else if (wmt_evt->whdr.flag == 1)
2481 status = BTMTK_WMT_PATCH_PROGRESS;
2483 status = BTMTK_WMT_PATCH_UNDONE;
2487 if (wmt_params->status)
2488 *wmt_params->status = status;
2491 kfree_skb(data->evt_skb);
2492 data->evt_skb = NULL;
2498 static int btusb_mtk_func_query(struct hci_dev *hdev)
2500 struct btmtk_hci_wmt_params wmt_params;
2504 /* Query whether the function is enabled */
2505 wmt_params.op = BTMTK_WMT_FUNC_CTRL;
2506 wmt_params.flag = 4;
2507 wmt_params.dlen = sizeof(param);
2508 wmt_params.data = ¶m;
2509 wmt_params.status = &status;
2511 err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
2513 bt_dev_err(hdev, "Failed to query function status (%d)", err);
2520 static int btusb_mtk_uhw_reg_write(struct btusb_data *data, u32 reg, u32 val)
2522 struct hci_dev *hdev = data->hdev;
2526 buf = kzalloc(4, GFP_KERNEL);
2530 put_unaligned_le32(val, buf);
2532 pipe = usb_sndctrlpipe(data->udev, 0);
2533 err = usb_control_msg(data->udev, pipe, 0x02,
2535 reg >> 16, reg & 0xffff,
2536 buf, 4, USB_CTRL_SET_TIMEOUT);
2538 bt_dev_err(hdev, "Failed to write uhw reg(%d)", err);
2548 static int btusb_mtk_uhw_reg_read(struct btusb_data *data, u32 reg, u32 *val)
2550 struct hci_dev *hdev = data->hdev;
2554 buf = kzalloc(4, GFP_KERNEL);
2558 pipe = usb_rcvctrlpipe(data->udev, 0);
2559 err = usb_control_msg(data->udev, pipe, 0x01,
2561 reg >> 16, reg & 0xffff,
2562 buf, 4, USB_CTRL_SET_TIMEOUT);
2564 bt_dev_err(hdev, "Failed to read uhw reg(%d)", err);
2568 *val = get_unaligned_le32(buf);
2569 bt_dev_dbg(hdev, "reg=%x, value=0x%08x", reg, *val);
2577 static int btusb_mtk_reg_read(struct btusb_data *data, u32 reg, u32 *val)
2579 int pipe, err, size = sizeof(u32);
2582 buf = kzalloc(size, GFP_KERNEL);
2586 pipe = usb_rcvctrlpipe(data->udev, 0);
2587 err = usb_control_msg(data->udev, pipe, 0x63,
2588 USB_TYPE_VENDOR | USB_DIR_IN,
2589 reg >> 16, reg & 0xffff,
2590 buf, size, USB_CTRL_SET_TIMEOUT);
2594 *val = get_unaligned_le32(buf);
2602 static int btusb_mtk_id_get(struct btusb_data *data, u32 reg, u32 *id)
2604 return btusb_mtk_reg_read(data, reg, id);
2607 static int btusb_mtk_setup(struct hci_dev *hdev)
2609 struct btusb_data *data = hci_get_drvdata(hdev);
2610 struct btmtk_hci_wmt_params wmt_params;
2611 ktime_t calltime, delta, rettime;
2612 struct btmtk_tci_sleep tci_sleep;
2613 unsigned long long duration;
2614 struct sk_buff *skb;
2618 char fw_bin_name[64];
2622 calltime = ktime_get();
2624 err = btusb_mtk_id_get(data, 0x80000008, &dev_id);
2626 bt_dev_err(hdev, "Failed to get device id (%d)", err);
2631 err = btusb_mtk_id_get(data, 0x70010200, &dev_id);
2633 bt_dev_err(hdev, "Failed to get device id (%d)", err);
2636 err = btusb_mtk_id_get(data, 0x80021004, &fw_version);
2638 bt_dev_err(hdev, "Failed to get fw version (%d)", err);
2645 fwname = FIRMWARE_MT7663;
2648 fwname = FIRMWARE_MT7668;
2652 snprintf(fw_bin_name, sizeof(fw_bin_name),
2653 "mediatek/BT_RAM_CODE_MT%04x_1_%x_hdr.bin",
2654 dev_id & 0xffff, (fw_version & 0xff) + 1);
2655 err = btmtk_setup_firmware_79xx(hdev, fw_bin_name,
2656 btusb_mtk_hci_wmt_sync);
2658 bt_dev_err(hdev, "Failed to set up firmware (%d)", err);
2662 /* It's Device EndPoint Reset Option Register */
2663 btusb_mtk_uhw_reg_write(data, MTK_EP_RST_OPT, MTK_EP_RST_IN_OUT_OPT);
2665 /* Enable Bluetooth protocol */
2667 wmt_params.op = BTMTK_WMT_FUNC_CTRL;
2668 wmt_params.flag = 0;
2669 wmt_params.dlen = sizeof(param);
2670 wmt_params.data = ¶m;
2671 wmt_params.status = NULL;
2673 err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
2675 bt_dev_err(hdev, "Failed to send wmt func ctrl (%d)", err);
2679 hci_set_msft_opcode(hdev, 0xFD30);
2680 hci_set_aosp_capable(hdev);
2683 bt_dev_err(hdev, "Unsupported hardware variant (%08x)",
2688 /* Query whether the firmware is already download */
2689 wmt_params.op = BTMTK_WMT_SEMAPHORE;
2690 wmt_params.flag = 1;
2691 wmt_params.dlen = 0;
2692 wmt_params.data = NULL;
2693 wmt_params.status = &status;
2695 err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
2697 bt_dev_err(hdev, "Failed to query firmware status (%d)", err);
2701 if (status == BTMTK_WMT_PATCH_DONE) {
2702 bt_dev_info(hdev, "firmware already downloaded");
2703 goto ignore_setup_fw;
2706 /* Setup a firmware which the device definitely requires */
2707 err = btmtk_setup_firmware(hdev, fwname,
2708 btusb_mtk_hci_wmt_sync);
2713 err = readx_poll_timeout(btusb_mtk_func_query, hdev, status,
2714 status < 0 || status != BTMTK_WMT_ON_PROGRESS,
2716 /* -ETIMEDOUT happens */
2720 /* The other errors happen in btusb_mtk_func_query */
2724 if (status == BTMTK_WMT_ON_DONE) {
2725 bt_dev_info(hdev, "function already on");
2726 goto ignore_func_on;
2729 /* Enable Bluetooth protocol */
2731 wmt_params.op = BTMTK_WMT_FUNC_CTRL;
2732 wmt_params.flag = 0;
2733 wmt_params.dlen = sizeof(param);
2734 wmt_params.data = ¶m;
2735 wmt_params.status = NULL;
2737 err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
2739 bt_dev_err(hdev, "Failed to send wmt func ctrl (%d)", err);
2744 /* Apply the low power environment setup */
2745 tci_sleep.mode = 0x5;
2746 tci_sleep.duration = cpu_to_le16(0x640);
2747 tci_sleep.host_duration = cpu_to_le16(0x640);
2748 tci_sleep.host_wakeup_pin = 0;
2749 tci_sleep.time_compensation = 0;
2751 skb = __hci_cmd_sync(hdev, 0xfc7a, sizeof(tci_sleep), &tci_sleep,
2755 bt_dev_err(hdev, "Failed to apply low power setting (%d)", err);
2761 rettime = ktime_get();
2762 delta = ktime_sub(rettime, calltime);
2763 duration = (unsigned long long)ktime_to_ns(delta) >> 10;
2765 bt_dev_info(hdev, "Device setup in %llu usecs", duration);
2770 static int btusb_mtk_shutdown(struct hci_dev *hdev)
2772 struct btmtk_hci_wmt_params wmt_params;
2776 /* Disable the device */
2777 wmt_params.op = BTMTK_WMT_FUNC_CTRL;
2778 wmt_params.flag = 0;
2779 wmt_params.dlen = sizeof(param);
2780 wmt_params.data = ¶m;
2781 wmt_params.status = NULL;
2783 err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
2785 bt_dev_err(hdev, "Failed to send wmt func ctrl (%d)", err);
2792 static void btusb_mtk_cmd_timeout(struct hci_dev *hdev)
2794 struct btusb_data *data = hci_get_drvdata(hdev);
2798 /* It's MediaTek specific bluetooth reset mechanism via USB */
2799 if (test_and_set_bit(BTUSB_HW_RESET_ACTIVE, &data->flags)) {
2800 bt_dev_err(hdev, "last reset failed? Not resetting again");
2804 err = usb_autopm_get_interface(data->intf);
2808 btusb_stop_traffic(data);
2809 usb_kill_anchored_urbs(&data->tx_anchor);
2811 /* It's Device EndPoint Reset Option Register */
2812 bt_dev_dbg(hdev, "Initiating reset mechanism via uhw");
2813 btusb_mtk_uhw_reg_write(data, MTK_EP_RST_OPT, MTK_EP_RST_IN_OUT_OPT);
2814 btusb_mtk_uhw_reg_read(data, MTK_BT_WDT_STATUS, &val);
2816 /* Reset the bluetooth chip via USB interface. */
2817 btusb_mtk_uhw_reg_write(data, MTK_BT_SUBSYS_RST, 1);
2818 btusb_mtk_uhw_reg_write(data, MTK_UDMA_INT_STA_BT, 0x000000FF);
2819 btusb_mtk_uhw_reg_read(data, MTK_UDMA_INT_STA_BT, &val);
2820 btusb_mtk_uhw_reg_write(data, MTK_UDMA_INT_STA_BT1, 0x000000FF);
2821 btusb_mtk_uhw_reg_read(data, MTK_UDMA_INT_STA_BT1, &val);
2822 /* MT7921 need to delay 20ms between toggle reset bit */
2824 btusb_mtk_uhw_reg_write(data, MTK_BT_SUBSYS_RST, 0);
2825 btusb_mtk_uhw_reg_read(data, MTK_BT_SUBSYS_RST, &val);
2827 /* Poll the register until reset is completed */
2829 btusb_mtk_uhw_reg_read(data, MTK_BT_MISC, &val);
2830 if (val & MTK_BT_RST_DONE) {
2831 bt_dev_dbg(hdev, "Bluetooth Reset Successfully");
2835 bt_dev_dbg(hdev, "Polling Bluetooth Reset CR");
2837 msleep(MTK_BT_RESET_WAIT_MS);
2838 } while (retry < MTK_BT_RESET_NUM_TRIES);
2840 btusb_mtk_id_get(data, 0x70010200, &val);
2842 bt_dev_err(hdev, "Can't get device id, subsys reset fail.");
2844 usb_queue_reset_device(data->intf);
2846 clear_bit(BTUSB_HW_RESET_ACTIVE, &data->flags);
2849 static int btusb_recv_acl_mtk(struct hci_dev *hdev, struct sk_buff *skb)
2851 struct btusb_data *data = hci_get_drvdata(hdev);
2852 u16 handle = le16_to_cpu(hci_acl_hdr(skb)->handle);
2855 case 0xfc6f: /* Firmware dump from device */
2856 /* When the firmware hangs, the device can no longer
2857 * suspend and thus disable auto-suspend.
2859 usb_disable_autosuspend(data->udev);
2861 case 0x05ff: /* Firmware debug logging 1 */
2862 case 0x05fe: /* Firmware debug logging 2 */
2863 return hci_recv_diag(hdev, skb);
2866 return hci_recv_frame(hdev, skb);
2870 /* Configure an out-of-band gpio as wake-up pin, if specified in device tree */
2871 static int marvell_config_oob_wake(struct hci_dev *hdev)
2873 struct sk_buff *skb;
2874 struct btusb_data *data = hci_get_drvdata(hdev);
2875 struct device *dev = &data->udev->dev;
2876 u16 pin, gap, opcode;
2880 /* Move on if no wakeup pin specified */
2881 if (of_property_read_u16(dev->of_node, "marvell,wakeup-pin", &pin) ||
2882 of_property_read_u16(dev->of_node, "marvell,wakeup-gap-ms", &gap))
2885 /* Vendor specific command to configure a GPIO as wake-up pin */
2886 opcode = hci_opcode_pack(0x3F, 0x59);
2887 cmd[0] = opcode & 0xFF;
2888 cmd[1] = opcode >> 8;
2889 cmd[2] = 2; /* length of parameters that follow */
2891 cmd[4] = gap; /* time in ms, for which wakeup pin should be asserted */
2893 skb = bt_skb_alloc(sizeof(cmd), GFP_KERNEL);
2895 bt_dev_err(hdev, "%s: No memory", __func__);
2899 skb_put_data(skb, cmd, sizeof(cmd));
2900 hci_skb_pkt_type(skb) = HCI_COMMAND_PKT;
2902 ret = btusb_send_frame(hdev, skb);
2904 bt_dev_err(hdev, "%s: configuration failed", __func__);
2913 static int btusb_set_bdaddr_marvell(struct hci_dev *hdev,
2914 const bdaddr_t *bdaddr)
2916 struct sk_buff *skb;
2921 buf[1] = sizeof(bdaddr_t);
2922 memcpy(buf + 2, bdaddr, sizeof(bdaddr_t));
2924 skb = __hci_cmd_sync(hdev, 0xfc22, sizeof(buf), buf, HCI_INIT_TIMEOUT);
2927 bt_dev_err(hdev, "changing Marvell device address failed (%ld)",
2936 static int btusb_set_bdaddr_ath3012(struct hci_dev *hdev,
2937 const bdaddr_t *bdaddr)
2939 struct sk_buff *skb;
2946 buf[3] = sizeof(bdaddr_t);
2947 memcpy(buf + 4, bdaddr, sizeof(bdaddr_t));
2949 skb = __hci_cmd_sync(hdev, 0xfc0b, sizeof(buf), buf, HCI_INIT_TIMEOUT);
2952 bt_dev_err(hdev, "Change address command failed (%ld)", ret);
2960 static int btusb_set_bdaddr_wcn6855(struct hci_dev *hdev,
2961 const bdaddr_t *bdaddr)
2963 struct sk_buff *skb;
2967 memcpy(buf, bdaddr, sizeof(bdaddr_t));
2969 skb = __hci_cmd_sync_ev(hdev, 0xfc14, sizeof(buf), buf,
2970 HCI_EV_CMD_COMPLETE, HCI_INIT_TIMEOUT);
2973 bt_dev_err(hdev, "Change address command failed (%ld)", ret);
2981 #define QCA_DFU_PACKET_LEN 4096
2983 #define QCA_GET_TARGET_VERSION 0x09
2984 #define QCA_CHECK_STATUS 0x05
2985 #define QCA_DFU_DOWNLOAD 0x01
2987 #define QCA_SYSCFG_UPDATED 0x40
2988 #define QCA_PATCH_UPDATED 0x80
2989 #define QCA_DFU_TIMEOUT 3000
2990 #define QCA_FLAG_MULTI_NVM 0x80
2992 #define WCN6855_2_0_RAM_VERSION_GF 0x400c1200
2993 #define WCN6855_2_1_RAM_VERSION_GF 0x400c1211
2995 struct qca_version {
2997 __le32 patch_version;
3005 struct qca_rampatch_version {
3006 __le16 rom_version_high;
3007 __le16 rom_version_low;
3008 __le16 patch_version;
3011 struct qca_device_info {
3013 u8 rampatch_hdr; /* length of header in rampatch */
3014 u8 nvm_hdr; /* length of header in NVM */
3015 u8 ver_offset; /* offset of version structure in rampatch */
3018 static const struct qca_device_info qca_devices_table[] = {
3019 { 0x00000100, 20, 4, 8 }, /* Rome 1.0 */
3020 { 0x00000101, 20, 4, 8 }, /* Rome 1.1 */
3021 { 0x00000200, 28, 4, 16 }, /* Rome 2.0 */
3022 { 0x00000201, 28, 4, 16 }, /* Rome 2.1 */
3023 { 0x00000300, 28, 4, 16 }, /* Rome 3.0 */
3024 { 0x00000302, 28, 4, 16 }, /* Rome 3.2 */
3025 { 0x00130100, 40, 4, 16 }, /* WCN6855 1.0 */
3026 { 0x00130200, 40, 4, 16 }, /* WCN6855 2.0 */
3027 { 0x00130201, 40, 4, 16 }, /* WCN6855 2.1 */
3030 static int btusb_qca_send_vendor_req(struct usb_device *udev, u8 request,
3031 void *data, u16 size)
3036 buf = kmalloc(size, GFP_KERNEL);
3040 /* Found some of USB hosts have IOT issues with ours so that we should
3041 * not wait until HCI layer is ready.
3043 pipe = usb_rcvctrlpipe(udev, 0);
3044 err = usb_control_msg(udev, pipe, request, USB_TYPE_VENDOR | USB_DIR_IN,
3045 0, 0, buf, size, USB_CTRL_SET_TIMEOUT);
3047 dev_err(&udev->dev, "Failed to access otp area (%d)", err);
3051 memcpy(data, buf, size);
3059 static int btusb_setup_qca_download_fw(struct hci_dev *hdev,
3060 const struct firmware *firmware,
3063 struct btusb_data *btdata = hci_get_drvdata(hdev);
3064 struct usb_device *udev = btdata->udev;
3065 size_t count, size, sent = 0;
3069 buf = kmalloc(QCA_DFU_PACKET_LEN, GFP_KERNEL);
3073 count = firmware->size;
3075 size = min_t(size_t, count, hdr_size);
3076 memcpy(buf, firmware->data, size);
3078 /* USB patches should go down to controller through USB path
3079 * because binary format fits to go down through USB channel.
3080 * USB control path is for patching headers and USB bulk is for
3083 pipe = usb_sndctrlpipe(udev, 0);
3084 err = usb_control_msg(udev, pipe, QCA_DFU_DOWNLOAD, USB_TYPE_VENDOR,
3085 0, 0, buf, size, USB_CTRL_SET_TIMEOUT);
3087 bt_dev_err(hdev, "Failed to send headers (%d)", err);
3094 /* ep2 need time to switch from function acl to function dfu,
3095 * so we add 20ms delay here.
3100 size = min_t(size_t, count, QCA_DFU_PACKET_LEN);
3102 memcpy(buf, firmware->data + sent, size);
3104 pipe = usb_sndbulkpipe(udev, 0x02);
3105 err = usb_bulk_msg(udev, pipe, buf, size, &len,
3108 bt_dev_err(hdev, "Failed to send body at %zd of %zd (%d)",
3109 sent, firmware->size, err);
3114 bt_dev_err(hdev, "Failed to get bulk buffer");
3128 static int btusb_setup_qca_load_rampatch(struct hci_dev *hdev,
3129 struct qca_version *ver,
3130 const struct qca_device_info *info)
3132 struct qca_rampatch_version *rver;
3133 const struct firmware *fw;
3134 u32 ver_rom, ver_patch, rver_rom;
3135 u16 rver_rom_low, rver_rom_high, rver_patch;
3139 ver_rom = le32_to_cpu(ver->rom_version);
3140 ver_patch = le32_to_cpu(ver->patch_version);
3142 snprintf(fwname, sizeof(fwname), "qca/rampatch_usb_%08x.bin", ver_rom);
3144 err = request_firmware(&fw, fwname, &hdev->dev);
3146 bt_dev_err(hdev, "failed to request rampatch file: %s (%d)",
3151 bt_dev_info(hdev, "using rampatch file: %s", fwname);
3153 rver = (struct qca_rampatch_version *)(fw->data + info->ver_offset);
3154 rver_rom_low = le16_to_cpu(rver->rom_version_low);
3155 rver_patch = le16_to_cpu(rver->patch_version);
3157 if (ver_rom & ~0xffffU) {
3158 rver_rom_high = le16_to_cpu(rver->rom_version_high);
3159 rver_rom = le32_to_cpu(rver_rom_high << 16 | rver_rom_low);
3161 rver_rom = rver_rom_low;
3164 bt_dev_info(hdev, "QCA: patch rome 0x%x build 0x%x, "
3165 "firmware rome 0x%x build 0x%x",
3166 rver_rom, rver_patch, ver_rom, ver_patch);
3168 if (rver_rom != ver_rom || rver_patch <= ver_patch) {
3169 bt_dev_err(hdev, "rampatch file version did not match with firmware");
3174 err = btusb_setup_qca_download_fw(hdev, fw, info->rampatch_hdr);
3177 release_firmware(fw);
3182 static void btusb_generate_qca_nvm_name(char *fwname, size_t max_size,
3183 const struct qca_version *ver)
3185 u32 rom_version = le32_to_cpu(ver->rom_version);
3186 u16 flag = le16_to_cpu(ver->flag);
3188 if (((flag >> 8) & 0xff) == QCA_FLAG_MULTI_NVM) {
3189 /* The board_id should be split into two bytes
3190 * The 1st byte is chip ID, and the 2nd byte is platform ID
3191 * For example, board ID 0x010A, 0x01 is platform ID. 0x0A is chip ID
3192 * we have several platforms, and platform IDs are continuously added
3194 * 0x00 is for Mobile
3196 * 0x02 is for Automotive
3197 * 0x03 is for Consumer electronic
3199 u16 board_id = (ver->chip_id << 8) + ver->platform_id;
3200 const char *variant;
3202 switch (le32_to_cpu(ver->ram_version)) {
3203 case WCN6855_2_0_RAM_VERSION_GF:
3204 case WCN6855_2_1_RAM_VERSION_GF:
3212 if (board_id == 0) {
3213 snprintf(fwname, max_size, "qca/nvm_usb_%08x%s.bin",
3214 rom_version, variant);
3216 snprintf(fwname, max_size, "qca/nvm_usb_%08x%s_%04x.bin",
3217 rom_version, variant, board_id);
3220 snprintf(fwname, max_size, "qca/nvm_usb_%08x.bin",
3226 static int btusb_setup_qca_load_nvm(struct hci_dev *hdev,
3227 struct qca_version *ver,
3228 const struct qca_device_info *info)
3230 const struct firmware *fw;
3234 btusb_generate_qca_nvm_name(fwname, sizeof(fwname), ver);
3236 err = request_firmware(&fw, fwname, &hdev->dev);
3238 bt_dev_err(hdev, "failed to request NVM file: %s (%d)",
3243 bt_dev_info(hdev, "using NVM file: %s", fwname);
3245 err = btusb_setup_qca_download_fw(hdev, fw, info->nvm_hdr);
3247 release_firmware(fw);
3252 /* identify the ROM version and check whether patches are needed */
3253 static bool btusb_qca_need_patch(struct usb_device *udev)
3255 struct qca_version ver;
3257 if (btusb_qca_send_vendor_req(udev, QCA_GET_TARGET_VERSION, &ver,
3260 /* only low ROM versions need patches */
3261 return !(le32_to_cpu(ver.rom_version) & ~0xffffU);
3264 static int btusb_setup_qca(struct hci_dev *hdev)
3266 struct btusb_data *btdata = hci_get_drvdata(hdev);
3267 struct usb_device *udev = btdata->udev;
3268 const struct qca_device_info *info = NULL;
3269 struct qca_version ver;
3274 err = btusb_qca_send_vendor_req(udev, QCA_GET_TARGET_VERSION, &ver,
3279 ver_rom = le32_to_cpu(ver.rom_version);
3281 for (i = 0; i < ARRAY_SIZE(qca_devices_table); i++) {
3282 if (ver_rom == qca_devices_table[i].rom_version)
3283 info = &qca_devices_table[i];
3286 /* If the rom_version is not matched in the qca_devices_table
3287 * and the high ROM version is not zero, we assume this chip no
3288 * need to load the rampatch and nvm.
3290 if (ver_rom & ~0xffffU)
3293 bt_dev_err(hdev, "don't support firmware rome 0x%x", ver_rom);
3297 err = btusb_qca_send_vendor_req(udev, QCA_CHECK_STATUS, &status,
3302 if (!(status & QCA_PATCH_UPDATED)) {
3303 err = btusb_setup_qca_load_rampatch(hdev, &ver, info);
3308 err = btusb_qca_send_vendor_req(udev, QCA_GET_TARGET_VERSION, &ver,
3313 if (!(status & QCA_SYSCFG_UPDATED)) {
3314 err = btusb_setup_qca_load_nvm(hdev, &ver, info);
3322 static inline int __set_diag_interface(struct hci_dev *hdev)
3324 struct btusb_data *data = hci_get_drvdata(hdev);
3325 struct usb_interface *intf = data->diag;
3331 data->diag_tx_ep = NULL;
3332 data->diag_rx_ep = NULL;
3334 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
3335 struct usb_endpoint_descriptor *ep_desc;
3337 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
3339 if (!data->diag_tx_ep && usb_endpoint_is_bulk_out(ep_desc)) {
3340 data->diag_tx_ep = ep_desc;
3344 if (!data->diag_rx_ep && usb_endpoint_is_bulk_in(ep_desc)) {
3345 data->diag_rx_ep = ep_desc;
3350 if (!data->diag_tx_ep || !data->diag_rx_ep) {
3351 bt_dev_err(hdev, "invalid diagnostic descriptors");
3358 static struct urb *alloc_diag_urb(struct hci_dev *hdev, bool enable)
3360 struct btusb_data *data = hci_get_drvdata(hdev);
3361 struct sk_buff *skb;
3365 if (!data->diag_tx_ep)
3366 return ERR_PTR(-ENODEV);
3368 urb = usb_alloc_urb(0, GFP_KERNEL);
3370 return ERR_PTR(-ENOMEM);
3372 skb = bt_skb_alloc(2, GFP_KERNEL);
3375 return ERR_PTR(-ENOMEM);
3378 skb_put_u8(skb, 0xf0);
3379 skb_put_u8(skb, enable);
3381 pipe = usb_sndbulkpipe(data->udev, data->diag_tx_ep->bEndpointAddress);
3383 usb_fill_bulk_urb(urb, data->udev, pipe,
3384 skb->data, skb->len, btusb_tx_complete, skb);
3386 skb->dev = (void *)hdev;
3391 static int btusb_bcm_set_diag(struct hci_dev *hdev, bool enable)
3393 struct btusb_data *data = hci_get_drvdata(hdev);
3399 if (!test_bit(HCI_RUNNING, &hdev->flags))
3402 urb = alloc_diag_urb(hdev, enable);
3404 return PTR_ERR(urb);
3406 return submit_or_queue_tx_urb(hdev, urb);
3410 static irqreturn_t btusb_oob_wake_handler(int irq, void *priv)
3412 struct btusb_data *data = priv;
3414 pm_wakeup_event(&data->udev->dev, 0);
3417 /* Disable only if not already disabled (keep it balanced) */
3418 if (test_and_clear_bit(BTUSB_OOB_WAKE_ENABLED, &data->flags)) {
3419 disable_irq_nosync(irq);
3420 disable_irq_wake(irq);
3425 static const struct of_device_id btusb_match_table[] = {
3426 { .compatible = "usb1286,204e" },
3427 { .compatible = "usbcf3,e300" }, /* QCA6174A */
3428 { .compatible = "usb4ca,301a" }, /* QCA6174A (Lite-On) */
3431 MODULE_DEVICE_TABLE(of, btusb_match_table);
3433 /* Use an oob wakeup pin? */
3434 static int btusb_config_oob_wake(struct hci_dev *hdev)
3436 struct btusb_data *data = hci_get_drvdata(hdev);
3437 struct device *dev = &data->udev->dev;
3440 clear_bit(BTUSB_OOB_WAKE_ENABLED, &data->flags);
3442 if (!of_match_device(btusb_match_table, dev))
3445 /* Move on if no IRQ specified */
3446 irq = of_irq_get_byname(dev->of_node, "wakeup");
3448 bt_dev_dbg(hdev, "%s: no OOB Wakeup IRQ in DT", __func__);
3452 irq_set_status_flags(irq, IRQ_NOAUTOEN);
3453 ret = devm_request_irq(&hdev->dev, irq, btusb_oob_wake_handler,
3454 0, "OOB Wake-on-BT", data);
3456 bt_dev_err(hdev, "%s: IRQ request failed", __func__);
3460 ret = device_init_wakeup(dev, true);
3462 bt_dev_err(hdev, "%s: failed to init_wakeup", __func__);
3466 data->oob_wake_irq = irq;
3467 bt_dev_info(hdev, "OOB Wake-on-BT configured at IRQ %u", irq);
3472 static void btusb_check_needs_reset_resume(struct usb_interface *intf)
3474 if (dmi_check_system(btusb_needs_reset_resume_table))
3475 interface_to_usbdev(intf)->quirks |= USB_QUIRK_RESET_RESUME;
3478 static bool btusb_wakeup(struct hci_dev *hdev)
3480 struct btusb_data *data = hci_get_drvdata(hdev);
3482 return device_may_wakeup(&data->udev->dev);
3485 static int btusb_shutdown_qca(struct hci_dev *hdev)
3487 struct sk_buff *skb;
3489 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
3491 bt_dev_err(hdev, "HCI reset during shutdown failed");
3492 return PTR_ERR(skb);
3499 static ssize_t force_poll_sync_read(struct file *file, char __user *user_buf,
3500 size_t count, loff_t *ppos)
3502 struct btusb_data *data = file->private_data;
3505 buf[0] = data->poll_sync ? 'Y' : 'N';
3508 return simple_read_from_buffer(user_buf, count, ppos, buf, 2);
3511 static ssize_t force_poll_sync_write(struct file *file,
3512 const char __user *user_buf,
3513 size_t count, loff_t *ppos)
3515 struct btusb_data *data = file->private_data;
3519 err = kstrtobool_from_user(user_buf, count, &enable);
3523 /* Only allow changes while the adapter is down */
3524 if (test_bit(HCI_UP, &data->hdev->flags))
3527 if (data->poll_sync == enable)
3530 data->poll_sync = enable;
3535 static const struct file_operations force_poll_sync_fops = {
3536 .open = simple_open,
3537 .read = force_poll_sync_read,
3538 .write = force_poll_sync_write,
3539 .llseek = default_llseek,
3542 static int btusb_probe(struct usb_interface *intf,
3543 const struct usb_device_id *id)
3545 struct usb_endpoint_descriptor *ep_desc;
3546 struct gpio_desc *reset_gpio;
3547 struct btusb_data *data;
3548 struct hci_dev *hdev;
3549 unsigned ifnum_base;
3550 int i, err, priv_size;
3552 BT_DBG("intf %p id %p", intf, id);
3554 /* interface numbers are hardcoded in the spec */
3555 if (intf->cur_altsetting->desc.bInterfaceNumber != 0) {
3556 if (!(id->driver_info & BTUSB_IFNUM_2))
3558 if (intf->cur_altsetting->desc.bInterfaceNumber != 2)
3562 ifnum_base = intf->cur_altsetting->desc.bInterfaceNumber;
3564 if (!id->driver_info) {
3565 const struct usb_device_id *match;
3567 match = usb_match_id(intf, blacklist_table);
3572 if (id->driver_info == BTUSB_IGNORE)
3575 if (id->driver_info & BTUSB_ATH3012) {
3576 struct usb_device *udev = interface_to_usbdev(intf);
3578 /* Old firmware would otherwise let ath3k driver load
3579 * patch and sysconfig files
3581 if (le16_to_cpu(udev->descriptor.bcdDevice) <= 0x0001 &&
3582 !btusb_qca_need_patch(udev))
3586 data = devm_kzalloc(&intf->dev, sizeof(*data), GFP_KERNEL);
3590 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
3591 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
3593 if (!data->intr_ep && usb_endpoint_is_int_in(ep_desc)) {
3594 data->intr_ep = ep_desc;
3598 if (!data->bulk_tx_ep && usb_endpoint_is_bulk_out(ep_desc)) {
3599 data->bulk_tx_ep = ep_desc;
3603 if (!data->bulk_rx_ep && usb_endpoint_is_bulk_in(ep_desc)) {
3604 data->bulk_rx_ep = ep_desc;
3609 if (!data->intr_ep || !data->bulk_tx_ep || !data->bulk_rx_ep)
3612 if (id->driver_info & BTUSB_AMP) {
3613 data->cmdreq_type = USB_TYPE_CLASS | 0x01;
3614 data->cmdreq = 0x2b;
3616 data->cmdreq_type = USB_TYPE_CLASS;
3617 data->cmdreq = 0x00;
3620 data->udev = interface_to_usbdev(intf);
3623 INIT_WORK(&data->work, btusb_work);
3624 INIT_WORK(&data->waker, btusb_waker);
3625 INIT_DELAYED_WORK(&data->rx_work, btusb_rx_work);
3627 skb_queue_head_init(&data->acl_q);
3629 init_usb_anchor(&data->deferred);
3630 init_usb_anchor(&data->tx_anchor);
3631 spin_lock_init(&data->txlock);
3633 init_usb_anchor(&data->intr_anchor);
3634 init_usb_anchor(&data->bulk_anchor);
3635 init_usb_anchor(&data->isoc_anchor);
3636 init_usb_anchor(&data->diag_anchor);
3637 init_usb_anchor(&data->ctrl_anchor);
3638 spin_lock_init(&data->rxlock);
3642 data->recv_event = hci_recv_frame;
3643 data->recv_bulk = btusb_recv_bulk;
3645 if (id->driver_info & BTUSB_INTEL_COMBINED) {
3646 /* Allocate extra space for Intel device */
3647 priv_size += sizeof(struct btintel_data);
3649 /* Override the rx handlers */
3650 data->recv_event = btusb_recv_event_intel;
3651 data->recv_bulk = btusb_recv_bulk_intel;
3654 data->recv_acl = hci_recv_frame;
3656 hdev = hci_alloc_dev_priv(priv_size);
3660 hdev->bus = HCI_USB;
3661 hci_set_drvdata(hdev, data);
3663 if (id->driver_info & BTUSB_AMP)
3664 hdev->dev_type = HCI_AMP;
3666 hdev->dev_type = HCI_PRIMARY;
3670 SET_HCIDEV_DEV(hdev, &intf->dev);
3672 reset_gpio = gpiod_get_optional(&data->udev->dev, "reset",
3674 if (IS_ERR(reset_gpio)) {
3675 err = PTR_ERR(reset_gpio);
3677 } else if (reset_gpio) {
3678 data->reset_gpio = reset_gpio;
3681 hdev->open = btusb_open;
3682 hdev->close = btusb_close;
3683 hdev->flush = btusb_flush;
3684 hdev->send = btusb_send_frame;
3685 hdev->notify = btusb_notify;
3686 hdev->wakeup = btusb_wakeup;
3689 err = btusb_config_oob_wake(hdev);
3693 /* Marvell devices may need a specific chip configuration */
3694 if (id->driver_info & BTUSB_MARVELL && data->oob_wake_irq) {
3695 err = marvell_config_oob_wake(hdev);
3700 if (id->driver_info & BTUSB_CW6622)
3701 set_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks);
3703 if (id->driver_info & BTUSB_BCM2045)
3704 set_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks);
3706 if (id->driver_info & BTUSB_BCM92035)
3707 hdev->setup = btusb_setup_bcm92035;
3709 if (IS_ENABLED(CONFIG_BT_HCIBTUSB_BCM) &&
3710 (id->driver_info & BTUSB_BCM_PATCHRAM)) {
3711 hdev->manufacturer = 15;
3712 hdev->setup = btbcm_setup_patchram;
3713 hdev->set_diag = btusb_bcm_set_diag;
3714 hdev->set_bdaddr = btbcm_set_bdaddr;
3716 /* Broadcom LM_DIAG Interface numbers are hardcoded */
3717 data->diag = usb_ifnum_to_if(data->udev, ifnum_base + 2);
3720 if (IS_ENABLED(CONFIG_BT_HCIBTUSB_BCM) &&
3721 (id->driver_info & BTUSB_BCM_APPLE)) {
3722 hdev->manufacturer = 15;
3723 hdev->setup = btbcm_setup_apple;
3724 hdev->set_diag = btusb_bcm_set_diag;
3726 /* Broadcom LM_DIAG Interface numbers are hardcoded */
3727 data->diag = usb_ifnum_to_if(data->udev, ifnum_base + 2);
3730 /* Combined Intel Device setup to support multiple setup routine */
3731 if (id->driver_info & BTUSB_INTEL_COMBINED) {
3732 err = btintel_configure_setup(hdev);
3736 /* Transport specific configuration */
3737 hdev->send = btusb_send_frame_intel;
3738 hdev->cmd_timeout = btusb_intel_cmd_timeout;
3740 if (id->driver_info & BTUSB_INTEL_BROKEN_INITIAL_NCMD)
3741 btintel_set_flag(hdev, INTEL_BROKEN_INITIAL_NCMD);
3743 if (id->driver_info & BTUSB_INTEL_BROKEN_SHUTDOWN_LED)
3744 btintel_set_flag(hdev, INTEL_BROKEN_SHUTDOWN_LED);
3747 if (id->driver_info & BTUSB_MARVELL)
3748 hdev->set_bdaddr = btusb_set_bdaddr_marvell;
3750 if (IS_ENABLED(CONFIG_BT_HCIBTUSB_MTK) &&
3751 (id->driver_info & BTUSB_MEDIATEK)) {
3752 hdev->setup = btusb_mtk_setup;
3753 hdev->shutdown = btusb_mtk_shutdown;
3754 hdev->manufacturer = 70;
3755 hdev->cmd_timeout = btusb_mtk_cmd_timeout;
3756 hdev->set_bdaddr = btmtk_set_bdaddr;
3757 set_bit(HCI_QUIRK_NON_PERSISTENT_SETUP, &hdev->quirks);
3758 data->recv_acl = btusb_recv_acl_mtk;
3761 if (id->driver_info & BTUSB_SWAVE) {
3762 set_bit(HCI_QUIRK_FIXUP_INQUIRY_MODE, &hdev->quirks);
3763 set_bit(HCI_QUIRK_BROKEN_LOCAL_COMMANDS, &hdev->quirks);
3766 if (id->driver_info & BTUSB_INTEL_BOOT) {
3767 hdev->manufacturer = 2;
3768 set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
3771 if (id->driver_info & BTUSB_ATH3012) {
3772 data->setup_on_usb = btusb_setup_qca;
3773 hdev->set_bdaddr = btusb_set_bdaddr_ath3012;
3774 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
3775 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
3778 if (id->driver_info & BTUSB_QCA_ROME) {
3779 data->setup_on_usb = btusb_setup_qca;
3780 hdev->set_bdaddr = btusb_set_bdaddr_ath3012;
3781 hdev->cmd_timeout = btusb_qca_cmd_timeout;
3782 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
3783 btusb_check_needs_reset_resume(intf);
3786 if (id->driver_info & BTUSB_QCA_WCN6855) {
3787 data->setup_on_usb = btusb_setup_qca;
3788 hdev->shutdown = btusb_shutdown_qca;
3789 hdev->set_bdaddr = btusb_set_bdaddr_wcn6855;
3790 hdev->cmd_timeout = btusb_qca_cmd_timeout;
3791 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
3792 hci_set_msft_opcode(hdev, 0xFD70);
3795 if (id->driver_info & BTUSB_AMP) {
3796 /* AMP controllers do not support SCO packets */
3799 /* Interface orders are hardcoded in the specification */
3800 data->isoc = usb_ifnum_to_if(data->udev, ifnum_base + 1);
3801 data->isoc_ifnum = ifnum_base + 1;
3804 if (IS_ENABLED(CONFIG_BT_HCIBTUSB_RTL) &&
3805 (id->driver_info & BTUSB_REALTEK)) {
3806 hdev->setup = btrtl_setup_realtek;
3807 hdev->shutdown = btrtl_shutdown_realtek;
3808 hdev->cmd_timeout = btusb_rtl_cmd_timeout;
3810 /* Realtek devices need to set remote wakeup on auto-suspend */
3811 set_bit(BTUSB_WAKEUP_AUTOSUSPEND, &data->flags);
3812 set_bit(BTUSB_USE_ALT3_FOR_WBS, &data->flags);
3816 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
3818 if (force_scofix || id->driver_info & BTUSB_WRONG_SCO_MTU) {
3819 if (!disable_scofix)
3820 set_bit(HCI_QUIRK_FIXUP_BUFFER_SIZE, &hdev->quirks);
3823 if (id->driver_info & BTUSB_BROKEN_ISOC)
3826 if (id->driver_info & BTUSB_WIDEBAND_SPEECH)
3827 set_bit(HCI_QUIRK_WIDEBAND_SPEECH_SUPPORTED, &hdev->quirks);
3829 if (id->driver_info & BTUSB_VALID_LE_STATES)
3830 set_bit(HCI_QUIRK_VALID_LE_STATES, &hdev->quirks);
3832 if (id->driver_info & BTUSB_DIGIANSWER) {
3833 data->cmdreq_type = USB_TYPE_VENDOR;
3834 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
3837 if (id->driver_info & BTUSB_CSR) {
3838 struct usb_device *udev = data->udev;
3839 u16 bcdDevice = le16_to_cpu(udev->descriptor.bcdDevice);
3841 /* Old firmware would otherwise execute USB reset */
3842 if (bcdDevice < 0x117)
3843 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
3845 /* This must be set first in case we disable it for fakes */
3846 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
3848 /* Fake CSR devices with broken commands */
3849 if (le16_to_cpu(udev->descriptor.idVendor) == 0x0a12 &&
3850 le16_to_cpu(udev->descriptor.idProduct) == 0x0001)
3851 hdev->setup = btusb_setup_csr;
3854 if (id->driver_info & BTUSB_SNIFFER) {
3855 struct usb_device *udev = data->udev;
3857 /* New sniffer firmware has crippled HCI interface */
3858 if (le16_to_cpu(udev->descriptor.bcdDevice) > 0x997)
3859 set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
3862 if (id->driver_info & BTUSB_INTEL_BOOT) {
3863 /* A bug in the bootloader causes that interrupt interface is
3864 * only enabled after receiving SetInterface(0, AltSetting=0).
3866 err = usb_set_interface(data->udev, 0, 0);
3868 BT_ERR("failed to set interface 0, alt 0 %d", err);
3874 err = usb_driver_claim_interface(&btusb_driver,
3880 if (IS_ENABLED(CONFIG_BT_HCIBTUSB_BCM) && data->diag) {
3881 if (!usb_driver_claim_interface(&btusb_driver,
3883 __set_diag_interface(hdev);
3888 if (enable_autosuspend)
3889 usb_enable_autosuspend(data->udev);
3891 err = hci_register_dev(hdev);
3895 usb_set_intfdata(intf, data);
3897 debugfs_create_file("force_poll_sync", 0644, hdev->debugfs, data,
3898 &force_poll_sync_fops);
3903 if (data->reset_gpio)
3904 gpiod_put(data->reset_gpio);
3909 static void btusb_disconnect(struct usb_interface *intf)
3911 struct btusb_data *data = usb_get_intfdata(intf);
3912 struct hci_dev *hdev;
3914 BT_DBG("intf %p", intf);
3920 usb_set_intfdata(data->intf, NULL);
3923 usb_set_intfdata(data->isoc, NULL);
3926 usb_set_intfdata(data->diag, NULL);
3928 hci_unregister_dev(hdev);
3930 if (intf == data->intf) {
3932 usb_driver_release_interface(&btusb_driver, data->isoc);
3934 usb_driver_release_interface(&btusb_driver, data->diag);
3935 } else if (intf == data->isoc) {
3937 usb_driver_release_interface(&btusb_driver, data->diag);
3938 usb_driver_release_interface(&btusb_driver, data->intf);
3939 } else if (intf == data->diag) {
3940 usb_driver_release_interface(&btusb_driver, data->intf);
3942 usb_driver_release_interface(&btusb_driver, data->isoc);
3945 if (data->oob_wake_irq)
3946 device_init_wakeup(&data->udev->dev, false);
3948 if (data->reset_gpio)
3949 gpiod_put(data->reset_gpio);
3955 static int btusb_suspend(struct usb_interface *intf, pm_message_t message)
3957 struct btusb_data *data = usb_get_intfdata(intf);
3959 BT_DBG("intf %p", intf);
3961 if (data->suspend_count++)
3964 spin_lock_irq(&data->txlock);
3965 if (!(PMSG_IS_AUTO(message) && data->tx_in_flight)) {
3966 set_bit(BTUSB_SUSPENDING, &data->flags);
3967 spin_unlock_irq(&data->txlock);
3969 spin_unlock_irq(&data->txlock);
3970 data->suspend_count--;
3974 cancel_work_sync(&data->work);
3976 btusb_stop_traffic(data);
3977 usb_kill_anchored_urbs(&data->tx_anchor);
3979 if (data->oob_wake_irq && device_may_wakeup(&data->udev->dev)) {
3980 set_bit(BTUSB_OOB_WAKE_ENABLED, &data->flags);
3981 enable_irq_wake(data->oob_wake_irq);
3982 enable_irq(data->oob_wake_irq);
3985 /* For global suspend, Realtek devices lose the loaded fw
3986 * in them. But for autosuspend, firmware should remain.
3987 * Actually, it depends on whether the usb host sends
3988 * set feature (enable wakeup) or not.
3990 if (test_bit(BTUSB_WAKEUP_AUTOSUSPEND, &data->flags)) {
3991 if (PMSG_IS_AUTO(message) &&
3992 device_can_wakeup(&data->udev->dev))
3993 data->udev->do_remote_wakeup = 1;
3994 else if (!PMSG_IS_AUTO(message) &&
3995 !device_may_wakeup(&data->udev->dev)) {
3996 data->udev->do_remote_wakeup = 0;
3997 data->udev->reset_resume = 1;
4004 static void play_deferred(struct btusb_data *data)
4009 while ((urb = usb_get_from_anchor(&data->deferred))) {
4010 usb_anchor_urb(urb, &data->tx_anchor);
4012 err = usb_submit_urb(urb, GFP_ATOMIC);
4014 if (err != -EPERM && err != -ENODEV)
4015 BT_ERR("%s urb %p submission failed (%d)",
4016 data->hdev->name, urb, -err);
4017 kfree(urb->setup_packet);
4018 usb_unanchor_urb(urb);
4023 data->tx_in_flight++;
4027 /* Cleanup the rest deferred urbs. */
4028 while ((urb = usb_get_from_anchor(&data->deferred))) {
4029 kfree(urb->setup_packet);
4034 static int btusb_resume(struct usb_interface *intf)
4036 struct btusb_data *data = usb_get_intfdata(intf);
4037 struct hci_dev *hdev = data->hdev;
4040 BT_DBG("intf %p", intf);
4042 if (--data->suspend_count)
4045 /* Disable only if not already disabled (keep it balanced) */
4046 if (test_and_clear_bit(BTUSB_OOB_WAKE_ENABLED, &data->flags)) {
4047 disable_irq(data->oob_wake_irq);
4048 disable_irq_wake(data->oob_wake_irq);
4051 if (!test_bit(HCI_RUNNING, &hdev->flags))
4054 if (test_bit(BTUSB_INTR_RUNNING, &data->flags)) {
4055 err = btusb_submit_intr_urb(hdev, GFP_NOIO);
4057 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
4062 if (test_bit(BTUSB_BULK_RUNNING, &data->flags)) {
4063 err = btusb_submit_bulk_urb(hdev, GFP_NOIO);
4065 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
4069 btusb_submit_bulk_urb(hdev, GFP_NOIO);
4072 if (test_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
4073 if (btusb_submit_isoc_urb(hdev, GFP_NOIO) < 0)
4074 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
4076 btusb_submit_isoc_urb(hdev, GFP_NOIO);
4079 spin_lock_irq(&data->txlock);
4080 play_deferred(data);
4081 clear_bit(BTUSB_SUSPENDING, &data->flags);
4082 spin_unlock_irq(&data->txlock);
4083 schedule_work(&data->work);
4088 usb_scuttle_anchored_urbs(&data->deferred);
4090 spin_lock_irq(&data->txlock);
4091 clear_bit(BTUSB_SUSPENDING, &data->flags);
4092 spin_unlock_irq(&data->txlock);
4098 static struct usb_driver btusb_driver = {
4100 .probe = btusb_probe,
4101 .disconnect = btusb_disconnect,
4103 .suspend = btusb_suspend,
4104 .resume = btusb_resume,
4106 .id_table = btusb_table,
4107 .supports_autosuspend = 1,
4108 .disable_hub_initiated_lpm = 1,
4111 module_usb_driver(btusb_driver);
4113 module_param(disable_scofix, bool, 0644);
4114 MODULE_PARM_DESC(disable_scofix, "Disable fixup of wrong SCO buffer size");
4116 module_param(force_scofix, bool, 0644);
4117 MODULE_PARM_DESC(force_scofix, "Force fixup of wrong SCO buffers size");
4119 module_param(enable_autosuspend, bool, 0644);
4120 MODULE_PARM_DESC(enable_autosuspend, "Enable USB autosuspend by default");
4122 module_param(reset, bool, 0644);
4123 MODULE_PARM_DESC(reset, "Send HCI reset command on initialization");
4125 MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
4126 MODULE_DESCRIPTION("Generic Bluetooth USB driver ver " VERSION);
4127 MODULE_VERSION(VERSION);
4128 MODULE_LICENSE("GPL");