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/pci.h>
18 #include <linux/suspend.h>
19 #include <linux/gpio/consumer.h>
20 #include <asm/unaligned.h>
22 #include <net/bluetooth/bluetooth.h>
23 #include <net/bluetooth/hci_core.h>
31 static bool disable_scofix
;
32 static bool force_scofix
;
33 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
65 #define BTUSB_INTEL_NO_WBS_SUPPORT 0x8000000
67 static const struct usb_device_id btusb_table
[] = {
68 /* Generic Bluetooth USB device */
69 { USB_DEVICE_INFO(0xe0, 0x01, 0x01) },
71 /* Generic Bluetooth AMP device */
72 { USB_DEVICE_INFO(0xe0, 0x01, 0x04), .driver_info
= BTUSB_AMP
},
74 /* Generic Bluetooth USB interface */
75 { USB_INTERFACE_INFO(0xe0, 0x01, 0x01) },
77 /* Apple-specific (Broadcom) devices */
78 { USB_VENDOR_AND_INTERFACE_INFO(0x05ac, 0xff, 0x01, 0x01),
79 .driver_info
= BTUSB_BCM_APPLE
| BTUSB_IFNUM_2
},
81 /* MediaTek MT76x0E */
82 { USB_DEVICE(0x0e8d, 0x763f) },
84 /* Broadcom SoftSailing reporting vendor specific */
85 { USB_DEVICE(0x0a5c, 0x21e1) },
87 /* Apple MacBookPro 7,1 */
88 { USB_DEVICE(0x05ac, 0x8213) },
91 { USB_DEVICE(0x05ac, 0x8215) },
93 /* Apple MacBookPro6,2 */
94 { USB_DEVICE(0x05ac, 0x8218) },
96 /* Apple MacBookAir3,1, MacBookAir3,2 */
97 { USB_DEVICE(0x05ac, 0x821b) },
99 /* Apple MacBookAir4,1 */
100 { USB_DEVICE(0x05ac, 0x821f) },
102 /* Apple MacBookPro8,2 */
103 { USB_DEVICE(0x05ac, 0x821a) },
105 /* Apple MacMini5,1 */
106 { USB_DEVICE(0x05ac, 0x8281) },
108 /* AVM BlueFRITZ! USB v2.0 */
109 { USB_DEVICE(0x057c, 0x3800), .driver_info
= BTUSB_SWAVE
},
111 /* Bluetooth Ultraport Module from IBM */
112 { USB_DEVICE(0x04bf, 0x030a) },
114 /* ALPS Modules with non-standard id */
115 { USB_DEVICE(0x044e, 0x3001) },
116 { USB_DEVICE(0x044e, 0x3002) },
118 /* Ericsson with non-standard id */
119 { USB_DEVICE(0x0bdb, 0x1002) },
121 /* Canyon CN-BTU1 with HID interfaces */
122 { USB_DEVICE(0x0c10, 0x0000) },
124 /* Broadcom BCM20702B0 (Dynex/Insignia) */
125 { USB_DEVICE(0x19ff, 0x0239), .driver_info
= BTUSB_BCM_PATCHRAM
},
127 /* Broadcom BCM43142A0 (Foxconn/Lenovo) */
128 { USB_VENDOR_AND_INTERFACE_INFO(0x105b, 0xff, 0x01, 0x01),
129 .driver_info
= BTUSB_BCM_PATCHRAM
},
131 /* Broadcom BCM920703 (HTC Vive) */
132 { USB_VENDOR_AND_INTERFACE_INFO(0x0bb4, 0xff, 0x01, 0x01),
133 .driver_info
= BTUSB_BCM_PATCHRAM
},
135 /* Foxconn - Hon Hai */
136 { USB_VENDOR_AND_INTERFACE_INFO(0x0489, 0xff, 0x01, 0x01),
137 .driver_info
= BTUSB_BCM_PATCHRAM
},
139 /* Lite-On Technology - Broadcom based */
140 { USB_VENDOR_AND_INTERFACE_INFO(0x04ca, 0xff, 0x01, 0x01),
141 .driver_info
= BTUSB_BCM_PATCHRAM
},
143 /* Broadcom devices with vendor specific id */
144 { USB_VENDOR_AND_INTERFACE_INFO(0x0a5c, 0xff, 0x01, 0x01),
145 .driver_info
= BTUSB_BCM_PATCHRAM
},
147 /* ASUSTek Computer - Broadcom based */
148 { USB_VENDOR_AND_INTERFACE_INFO(0x0b05, 0xff, 0x01, 0x01),
149 .driver_info
= BTUSB_BCM_PATCHRAM
},
151 /* Belkin F8065bf - Broadcom based */
152 { USB_VENDOR_AND_INTERFACE_INFO(0x050d, 0xff, 0x01, 0x01),
153 .driver_info
= BTUSB_BCM_PATCHRAM
},
155 /* IMC Networks - Broadcom based */
156 { USB_VENDOR_AND_INTERFACE_INFO(0x13d3, 0xff, 0x01, 0x01),
157 .driver_info
= BTUSB_BCM_PATCHRAM
},
159 /* Dell Computer - Broadcom based */
160 { USB_VENDOR_AND_INTERFACE_INFO(0x413c, 0xff, 0x01, 0x01),
161 .driver_info
= BTUSB_BCM_PATCHRAM
},
163 /* Toshiba Corp - Broadcom based */
164 { USB_VENDOR_AND_INTERFACE_INFO(0x0930, 0xff, 0x01, 0x01),
165 .driver_info
= BTUSB_BCM_PATCHRAM
},
167 /* Intel Bluetooth USB Bootloader (RAM module) */
168 { USB_DEVICE(0x8087, 0x0a5a),
169 .driver_info
= BTUSB_INTEL_BOOT
| BTUSB_BROKEN_ISOC
},
171 { } /* Terminating entry */
174 MODULE_DEVICE_TABLE(usb
, btusb_table
);
176 static const struct usb_device_id blacklist_table
[] = {
177 /* CSR BlueCore devices */
178 { USB_DEVICE(0x0a12, 0x0001), .driver_info
= BTUSB_CSR
},
180 /* Broadcom BCM2033 without firmware */
181 { USB_DEVICE(0x0a5c, 0x2033), .driver_info
= BTUSB_IGNORE
},
183 /* Broadcom BCM2045 devices */
184 { USB_DEVICE(0x0a5c, 0x2045), .driver_info
= BTUSB_BCM2045
},
186 /* Atheros 3011 with sflash firmware */
187 { USB_DEVICE(0x0489, 0xe027), .driver_info
= BTUSB_IGNORE
},
188 { USB_DEVICE(0x0489, 0xe03d), .driver_info
= BTUSB_IGNORE
},
189 { USB_DEVICE(0x04f2, 0xaff1), .driver_info
= BTUSB_IGNORE
},
190 { USB_DEVICE(0x0930, 0x0215), .driver_info
= BTUSB_IGNORE
},
191 { USB_DEVICE(0x0cf3, 0x3002), .driver_info
= BTUSB_IGNORE
},
192 { USB_DEVICE(0x0cf3, 0xe019), .driver_info
= BTUSB_IGNORE
},
193 { USB_DEVICE(0x13d3, 0x3304), .driver_info
= BTUSB_IGNORE
},
195 /* Atheros AR9285 Malbec with sflash firmware */
196 { USB_DEVICE(0x03f0, 0x311d), .driver_info
= BTUSB_IGNORE
},
198 /* Atheros 3012 with sflash firmware */
199 { USB_DEVICE(0x0489, 0xe04d), .driver_info
= BTUSB_ATH3012
},
200 { USB_DEVICE(0x0489, 0xe04e), .driver_info
= BTUSB_ATH3012
},
201 { USB_DEVICE(0x0489, 0xe056), .driver_info
= BTUSB_ATH3012
},
202 { USB_DEVICE(0x0489, 0xe057), .driver_info
= BTUSB_ATH3012
},
203 { USB_DEVICE(0x0489, 0xe05f), .driver_info
= BTUSB_ATH3012
},
204 { USB_DEVICE(0x0489, 0xe076), .driver_info
= BTUSB_ATH3012
},
205 { USB_DEVICE(0x0489, 0xe078), .driver_info
= BTUSB_ATH3012
},
206 { USB_DEVICE(0x0489, 0xe095), .driver_info
= BTUSB_ATH3012
},
207 { USB_DEVICE(0x04c5, 0x1330), .driver_info
= BTUSB_ATH3012
},
208 { USB_DEVICE(0x04ca, 0x3004), .driver_info
= BTUSB_ATH3012
},
209 { USB_DEVICE(0x04ca, 0x3005), .driver_info
= BTUSB_ATH3012
},
210 { USB_DEVICE(0x04ca, 0x3006), .driver_info
= BTUSB_ATH3012
},
211 { USB_DEVICE(0x04ca, 0x3007), .driver_info
= BTUSB_ATH3012
},
212 { USB_DEVICE(0x04ca, 0x3008), .driver_info
= BTUSB_ATH3012
},
213 { USB_DEVICE(0x04ca, 0x300b), .driver_info
= BTUSB_ATH3012
},
214 { USB_DEVICE(0x04ca, 0x300d), .driver_info
= BTUSB_ATH3012
},
215 { USB_DEVICE(0x04ca, 0x300f), .driver_info
= BTUSB_ATH3012
},
216 { USB_DEVICE(0x04ca, 0x3010), .driver_info
= BTUSB_ATH3012
},
217 { USB_DEVICE(0x04ca, 0x3014), .driver_info
= BTUSB_ATH3012
},
218 { USB_DEVICE(0x04ca, 0x3018), .driver_info
= BTUSB_ATH3012
},
219 { USB_DEVICE(0x0930, 0x0219), .driver_info
= BTUSB_ATH3012
},
220 { USB_DEVICE(0x0930, 0x021c), .driver_info
= BTUSB_ATH3012
},
221 { USB_DEVICE(0x0930, 0x0220), .driver_info
= BTUSB_ATH3012
},
222 { USB_DEVICE(0x0930, 0x0227), .driver_info
= BTUSB_ATH3012
},
223 { USB_DEVICE(0x0b05, 0x17d0), .driver_info
= BTUSB_ATH3012
},
224 { USB_DEVICE(0x0cf3, 0x0036), .driver_info
= BTUSB_ATH3012
},
225 { USB_DEVICE(0x0cf3, 0x3004), .driver_info
= BTUSB_ATH3012
},
226 { USB_DEVICE(0x0cf3, 0x3008), .driver_info
= BTUSB_ATH3012
},
227 { USB_DEVICE(0x0cf3, 0x311d), .driver_info
= BTUSB_ATH3012
},
228 { USB_DEVICE(0x0cf3, 0x311e), .driver_info
= BTUSB_ATH3012
},
229 { USB_DEVICE(0x0cf3, 0x311f), .driver_info
= BTUSB_ATH3012
},
230 { USB_DEVICE(0x0cf3, 0x3121), .driver_info
= BTUSB_ATH3012
},
231 { USB_DEVICE(0x0cf3, 0x817a), .driver_info
= BTUSB_ATH3012
},
232 { USB_DEVICE(0x0cf3, 0x817b), .driver_info
= BTUSB_ATH3012
},
233 { USB_DEVICE(0x0cf3, 0xe003), .driver_info
= BTUSB_ATH3012
},
234 { USB_DEVICE(0x0cf3, 0xe004), .driver_info
= BTUSB_ATH3012
},
235 { USB_DEVICE(0x0cf3, 0xe005), .driver_info
= BTUSB_ATH3012
},
236 { USB_DEVICE(0x0cf3, 0xe006), .driver_info
= BTUSB_ATH3012
},
237 { USB_DEVICE(0x13d3, 0x3362), .driver_info
= BTUSB_ATH3012
},
238 { USB_DEVICE(0x13d3, 0x3375), .driver_info
= BTUSB_ATH3012
},
239 { USB_DEVICE(0x13d3, 0x3393), .driver_info
= BTUSB_ATH3012
},
240 { USB_DEVICE(0x13d3, 0x3395), .driver_info
= BTUSB_ATH3012
},
241 { USB_DEVICE(0x13d3, 0x3402), .driver_info
= BTUSB_ATH3012
},
242 { USB_DEVICE(0x13d3, 0x3408), .driver_info
= BTUSB_ATH3012
},
243 { USB_DEVICE(0x13d3, 0x3423), .driver_info
= BTUSB_ATH3012
},
244 { USB_DEVICE(0x13d3, 0x3432), .driver_info
= BTUSB_ATH3012
},
245 { USB_DEVICE(0x13d3, 0x3472), .driver_info
= BTUSB_ATH3012
},
246 { USB_DEVICE(0x13d3, 0x3474), .driver_info
= BTUSB_ATH3012
},
247 { USB_DEVICE(0x13d3, 0x3487), .driver_info
= BTUSB_ATH3012
},
248 { USB_DEVICE(0x13d3, 0x3490), .driver_info
= BTUSB_ATH3012
},
250 /* Atheros AR5BBU12 with sflash firmware */
251 { USB_DEVICE(0x0489, 0xe02c), .driver_info
= BTUSB_IGNORE
},
253 /* Atheros AR5BBU12 with sflash firmware */
254 { USB_DEVICE(0x0489, 0xe036), .driver_info
= BTUSB_ATH3012
},
255 { USB_DEVICE(0x0489, 0xe03c), .driver_info
= BTUSB_ATH3012
},
257 /* QCA ROME chipset */
258 { USB_DEVICE(0x0cf3, 0x535b), .driver_info
= BTUSB_QCA_ROME
|
259 BTUSB_WIDEBAND_SPEECH
},
260 { USB_DEVICE(0x0cf3, 0xe007), .driver_info
= BTUSB_QCA_ROME
|
261 BTUSB_WIDEBAND_SPEECH
},
262 { USB_DEVICE(0x0cf3, 0xe009), .driver_info
= BTUSB_QCA_ROME
|
263 BTUSB_WIDEBAND_SPEECH
},
264 { USB_DEVICE(0x0cf3, 0xe010), .driver_info
= BTUSB_QCA_ROME
|
265 BTUSB_WIDEBAND_SPEECH
},
266 { USB_DEVICE(0x0cf3, 0xe300), .driver_info
= BTUSB_QCA_ROME
|
267 BTUSB_WIDEBAND_SPEECH
},
268 { USB_DEVICE(0x0cf3, 0xe301), .driver_info
= BTUSB_QCA_ROME
|
269 BTUSB_WIDEBAND_SPEECH
},
270 { USB_DEVICE(0x0cf3, 0xe360), .driver_info
= BTUSB_QCA_ROME
|
271 BTUSB_WIDEBAND_SPEECH
},
272 { USB_DEVICE(0x0cf3, 0xe500), .driver_info
= BTUSB_QCA_ROME
|
273 BTUSB_WIDEBAND_SPEECH
},
274 { USB_DEVICE(0x0489, 0xe092), .driver_info
= BTUSB_QCA_ROME
|
275 BTUSB_WIDEBAND_SPEECH
},
276 { USB_DEVICE(0x0489, 0xe09f), .driver_info
= BTUSB_QCA_ROME
|
277 BTUSB_WIDEBAND_SPEECH
},
278 { USB_DEVICE(0x0489, 0xe0a2), .driver_info
= BTUSB_QCA_ROME
|
279 BTUSB_WIDEBAND_SPEECH
},
280 { USB_DEVICE(0x04ca, 0x3011), .driver_info
= BTUSB_QCA_ROME
|
281 BTUSB_WIDEBAND_SPEECH
},
282 { USB_DEVICE(0x04ca, 0x3015), .driver_info
= BTUSB_QCA_ROME
|
283 BTUSB_WIDEBAND_SPEECH
},
284 { USB_DEVICE(0x04ca, 0x3016), .driver_info
= BTUSB_QCA_ROME
|
285 BTUSB_WIDEBAND_SPEECH
},
286 { USB_DEVICE(0x04ca, 0x301a), .driver_info
= BTUSB_QCA_ROME
|
287 BTUSB_WIDEBAND_SPEECH
},
288 { USB_DEVICE(0x04ca, 0x3021), .driver_info
= BTUSB_QCA_ROME
|
289 BTUSB_WIDEBAND_SPEECH
},
290 { USB_DEVICE(0x13d3, 0x3491), .driver_info
= BTUSB_QCA_ROME
|
291 BTUSB_WIDEBAND_SPEECH
},
292 { USB_DEVICE(0x13d3, 0x3496), .driver_info
= BTUSB_QCA_ROME
|
293 BTUSB_WIDEBAND_SPEECH
},
294 { USB_DEVICE(0x13d3, 0x3501), .driver_info
= BTUSB_QCA_ROME
|
295 BTUSB_WIDEBAND_SPEECH
},
297 /* QCA WCN6855 chipset */
298 { USB_DEVICE(0x0cf3, 0xe600), .driver_info
= BTUSB_QCA_WCN6855
|
299 BTUSB_WIDEBAND_SPEECH
|
300 BTUSB_VALID_LE_STATES
},
301 { USB_DEVICE(0x0489, 0xe0cc), .driver_info
= BTUSB_QCA_WCN6855
|
302 BTUSB_WIDEBAND_SPEECH
|
303 BTUSB_VALID_LE_STATES
},
304 { USB_DEVICE(0x0489, 0xe0d6), .driver_info
= BTUSB_QCA_WCN6855
|
305 BTUSB_WIDEBAND_SPEECH
|
306 BTUSB_VALID_LE_STATES
},
307 { USB_DEVICE(0x0489, 0xe0e3), .driver_info
= BTUSB_QCA_WCN6855
|
308 BTUSB_WIDEBAND_SPEECH
|
309 BTUSB_VALID_LE_STATES
},
310 { USB_DEVICE(0x10ab, 0x9309), .driver_info
= BTUSB_QCA_WCN6855
|
311 BTUSB_WIDEBAND_SPEECH
|
312 BTUSB_VALID_LE_STATES
},
313 { USB_DEVICE(0x10ab, 0x9409), .driver_info
= BTUSB_QCA_WCN6855
|
314 BTUSB_WIDEBAND_SPEECH
|
315 BTUSB_VALID_LE_STATES
},
316 { USB_DEVICE(0x0489, 0xe0d0), .driver_info
= BTUSB_QCA_WCN6855
|
317 BTUSB_WIDEBAND_SPEECH
|
318 BTUSB_VALID_LE_STATES
},
320 /* Broadcom BCM2035 */
321 { USB_DEVICE(0x0a5c, 0x2009), .driver_info
= BTUSB_BCM92035
},
322 { USB_DEVICE(0x0a5c, 0x200a), .driver_info
= BTUSB_WRONG_SCO_MTU
},
323 { USB_DEVICE(0x0a5c, 0x2035), .driver_info
= BTUSB_WRONG_SCO_MTU
},
325 /* Broadcom BCM2045 */
326 { USB_DEVICE(0x0a5c, 0x2039), .driver_info
= BTUSB_WRONG_SCO_MTU
},
327 { USB_DEVICE(0x0a5c, 0x2101), .driver_info
= BTUSB_WRONG_SCO_MTU
},
329 /* IBM/Lenovo ThinkPad with Broadcom chip */
330 { USB_DEVICE(0x0a5c, 0x201e), .driver_info
= BTUSB_WRONG_SCO_MTU
},
331 { USB_DEVICE(0x0a5c, 0x2110), .driver_info
= BTUSB_WRONG_SCO_MTU
},
333 /* HP laptop with Broadcom chip */
334 { USB_DEVICE(0x03f0, 0x171d), .driver_info
= BTUSB_WRONG_SCO_MTU
},
336 /* Dell laptop with Broadcom chip */
337 { USB_DEVICE(0x413c, 0x8126), .driver_info
= BTUSB_WRONG_SCO_MTU
},
339 /* Dell Wireless 370 and 410 devices */
340 { USB_DEVICE(0x413c, 0x8152), .driver_info
= BTUSB_WRONG_SCO_MTU
},
341 { USB_DEVICE(0x413c, 0x8156), .driver_info
= BTUSB_WRONG_SCO_MTU
},
343 /* Belkin F8T012 and F8T013 devices */
344 { USB_DEVICE(0x050d, 0x0012), .driver_info
= BTUSB_WRONG_SCO_MTU
},
345 { USB_DEVICE(0x050d, 0x0013), .driver_info
= BTUSB_WRONG_SCO_MTU
},
347 /* Asus WL-BTD202 device */
348 { USB_DEVICE(0x0b05, 0x1715), .driver_info
= BTUSB_WRONG_SCO_MTU
},
350 /* Kensington Bluetooth USB adapter */
351 { USB_DEVICE(0x047d, 0x105e), .driver_info
= BTUSB_WRONG_SCO_MTU
},
353 /* RTX Telecom based adapters with buggy SCO support */
354 { USB_DEVICE(0x0400, 0x0807), .driver_info
= BTUSB_BROKEN_ISOC
},
355 { USB_DEVICE(0x0400, 0x080a), .driver_info
= BTUSB_BROKEN_ISOC
},
357 /* CONWISE Technology based adapters with buggy SCO support */
358 { USB_DEVICE(0x0e5e, 0x6622),
359 .driver_info
= BTUSB_BROKEN_ISOC
| BTUSB_CW6622
},
361 /* Roper Class 1 Bluetooth Dongle (Silicon Wave based) */
362 { USB_DEVICE(0x1310, 0x0001), .driver_info
= BTUSB_SWAVE
},
364 /* Digianswer devices */
365 { USB_DEVICE(0x08fd, 0x0001), .driver_info
= BTUSB_DIGIANSWER
},
366 { USB_DEVICE(0x08fd, 0x0002), .driver_info
= BTUSB_IGNORE
},
368 /* CSR BlueCore Bluetooth Sniffer */
369 { USB_DEVICE(0x0a12, 0x0002),
370 .driver_info
= BTUSB_SNIFFER
| BTUSB_BROKEN_ISOC
},
372 /* Frontline ComProbe Bluetooth Sniffer */
373 { USB_DEVICE(0x16d3, 0x0002),
374 .driver_info
= BTUSB_SNIFFER
| BTUSB_BROKEN_ISOC
},
376 /* Marvell Bluetooth devices */
377 { USB_DEVICE(0x1286, 0x2044), .driver_info
= BTUSB_MARVELL
},
378 { USB_DEVICE(0x1286, 0x2046), .driver_info
= BTUSB_MARVELL
},
379 { USB_DEVICE(0x1286, 0x204e), .driver_info
= BTUSB_MARVELL
},
381 /* Intel Bluetooth devices */
382 { USB_DEVICE(0x8087, 0x0025), .driver_info
= BTUSB_INTEL_COMBINED
},
383 { USB_DEVICE(0x8087, 0x0026), .driver_info
= BTUSB_INTEL_COMBINED
},
384 { USB_DEVICE(0x8087, 0x0029), .driver_info
= BTUSB_INTEL_COMBINED
},
385 { USB_DEVICE(0x8087, 0x0032), .driver_info
= BTUSB_INTEL_COMBINED
},
386 { USB_DEVICE(0x8087, 0x0033), .driver_info
= BTUSB_INTEL_COMBINED
},
387 { USB_DEVICE(0x8087, 0x07da), .driver_info
= BTUSB_CSR
},
388 { USB_DEVICE(0x8087, 0x07dc), .driver_info
= BTUSB_INTEL_COMBINED
|
389 BTUSB_INTEL_NO_WBS_SUPPORT
|
390 BTUSB_INTEL_BROKEN_INITIAL_NCMD
|
391 BTUSB_INTEL_BROKEN_SHUTDOWN_LED
},
392 { USB_DEVICE(0x8087, 0x0a2a), .driver_info
= BTUSB_INTEL_COMBINED
|
393 BTUSB_INTEL_NO_WBS_SUPPORT
|
394 BTUSB_INTEL_BROKEN_SHUTDOWN_LED
},
395 { USB_DEVICE(0x8087, 0x0a2b), .driver_info
= BTUSB_INTEL_COMBINED
},
396 { USB_DEVICE(0x8087, 0x0aa7), .driver_info
= BTUSB_INTEL_COMBINED
|
397 BTUSB_INTEL_BROKEN_SHUTDOWN_LED
},
398 { USB_DEVICE(0x8087, 0x0aaa), .driver_info
= BTUSB_INTEL_COMBINED
},
400 /* Other Intel Bluetooth devices */
401 { USB_VENDOR_AND_INTERFACE_INFO(0x8087, 0xe0, 0x01, 0x01),
402 .driver_info
= BTUSB_IGNORE
},
404 /* Realtek 8822CE Bluetooth devices */
405 { USB_DEVICE(0x0bda, 0xb00c), .driver_info
= BTUSB_REALTEK
|
406 BTUSB_WIDEBAND_SPEECH
},
407 { USB_DEVICE(0x0bda, 0xc822), .driver_info
= BTUSB_REALTEK
|
408 BTUSB_WIDEBAND_SPEECH
},
410 /* Realtek 8852AE Bluetooth devices */
411 { USB_DEVICE(0x0bda, 0x2852), .driver_info
= BTUSB_REALTEK
|
412 BTUSB_WIDEBAND_SPEECH
},
413 { USB_DEVICE(0x0bda, 0xc852), .driver_info
= BTUSB_REALTEK
|
414 BTUSB_WIDEBAND_SPEECH
},
415 { USB_DEVICE(0x0bda, 0x385a), .driver_info
= BTUSB_REALTEK
|
416 BTUSB_WIDEBAND_SPEECH
},
417 { USB_DEVICE(0x0bda, 0x4852), .driver_info
= BTUSB_REALTEK
|
418 BTUSB_WIDEBAND_SPEECH
},
419 { USB_DEVICE(0x04c5, 0x165c), .driver_info
= BTUSB_REALTEK
|
420 BTUSB_WIDEBAND_SPEECH
},
421 { USB_DEVICE(0x04ca, 0x4006), .driver_info
= BTUSB_REALTEK
|
422 BTUSB_WIDEBAND_SPEECH
},
424 /* Realtek 8852CE Bluetooth devices */
425 { USB_DEVICE(0x04ca, 0x4007), .driver_info
= BTUSB_REALTEK
|
426 BTUSB_WIDEBAND_SPEECH
},
427 { USB_DEVICE(0x04c5, 0x1675), .driver_info
= BTUSB_REALTEK
|
428 BTUSB_WIDEBAND_SPEECH
},
430 /* Realtek Bluetooth devices */
431 { USB_VENDOR_AND_INTERFACE_INFO(0x0bda, 0xe0, 0x01, 0x01),
432 .driver_info
= BTUSB_REALTEK
},
434 /* MediaTek Bluetooth devices */
435 { USB_VENDOR_AND_INTERFACE_INFO(0x0e8d, 0xe0, 0x01, 0x01),
436 .driver_info
= BTUSB_MEDIATEK
|
437 BTUSB_WIDEBAND_SPEECH
|
438 BTUSB_VALID_LE_STATES
},
440 /* Additional MediaTek MT7615E Bluetooth devices */
441 { USB_DEVICE(0x13d3, 0x3560), .driver_info
= BTUSB_MEDIATEK
},
443 /* Additional MediaTek MT7668 Bluetooth devices */
444 { USB_DEVICE(0x043e, 0x3109), .driver_info
= BTUSB_MEDIATEK
|
445 BTUSB_WIDEBAND_SPEECH
|
446 BTUSB_VALID_LE_STATES
},
448 /* Additional MediaTek MT7921 Bluetooth devices */
449 { USB_DEVICE(0x0489, 0xe0c8), .driver_info
= BTUSB_MEDIATEK
|
450 BTUSB_WIDEBAND_SPEECH
|
451 BTUSB_VALID_LE_STATES
},
452 { USB_DEVICE(0x0489, 0xe0cd), .driver_info
= BTUSB_MEDIATEK
|
453 BTUSB_WIDEBAND_SPEECH
|
454 BTUSB_VALID_LE_STATES
},
455 { USB_DEVICE(0x04ca, 0x3802), .driver_info
= BTUSB_MEDIATEK
|
456 BTUSB_WIDEBAND_SPEECH
|
457 BTUSB_VALID_LE_STATES
},
458 { USB_DEVICE(0x13d3, 0x3563), .driver_info
= BTUSB_MEDIATEK
|
459 BTUSB_WIDEBAND_SPEECH
|
460 BTUSB_VALID_LE_STATES
},
461 { USB_DEVICE(0x13d3, 0x3564), .driver_info
= BTUSB_MEDIATEK
|
462 BTUSB_WIDEBAND_SPEECH
|
463 BTUSB_VALID_LE_STATES
},
464 { USB_DEVICE(0x13d3, 0x3567), .driver_info
= BTUSB_MEDIATEK
|
465 BTUSB_WIDEBAND_SPEECH
|
466 BTUSB_VALID_LE_STATES
},
467 { USB_DEVICE(0x0489, 0xe0cd), .driver_info
= BTUSB_MEDIATEK
|
468 BTUSB_WIDEBAND_SPEECH
|
469 BTUSB_VALID_LE_STATES
},
471 /* MediaTek MT7922A Bluetooth devices */
472 { USB_DEVICE(0x0489, 0xe0d8), .driver_info
= BTUSB_MEDIATEK
|
473 BTUSB_WIDEBAND_SPEECH
|
474 BTUSB_VALID_LE_STATES
},
475 { USB_DEVICE(0x0489, 0xe0d9), .driver_info
= BTUSB_MEDIATEK
|
476 BTUSB_WIDEBAND_SPEECH
|
477 BTUSB_VALID_LE_STATES
},
478 { USB_DEVICE(0x13d3, 0x3568), .driver_info
= BTUSB_MEDIATEK
|
479 BTUSB_WIDEBAND_SPEECH
|
480 BTUSB_VALID_LE_STATES
},
482 /* Additional Realtek 8723AE Bluetooth devices */
483 { USB_DEVICE(0x0930, 0x021d), .driver_info
= BTUSB_REALTEK
},
484 { USB_DEVICE(0x13d3, 0x3394), .driver_info
= BTUSB_REALTEK
},
486 /* Additional Realtek 8723BE Bluetooth devices */
487 { USB_DEVICE(0x0489, 0xe085), .driver_info
= BTUSB_REALTEK
},
488 { USB_DEVICE(0x0489, 0xe08b), .driver_info
= BTUSB_REALTEK
},
489 { USB_DEVICE(0x13d3, 0x3410), .driver_info
= BTUSB_REALTEK
},
490 { USB_DEVICE(0x13d3, 0x3416), .driver_info
= BTUSB_REALTEK
},
491 { USB_DEVICE(0x13d3, 0x3459), .driver_info
= BTUSB_REALTEK
},
492 { USB_DEVICE(0x13d3, 0x3494), .driver_info
= BTUSB_REALTEK
},
494 /* Additional Realtek 8723BU Bluetooth devices */
495 { USB_DEVICE(0x7392, 0xa611), .driver_info
= BTUSB_REALTEK
},
497 /* Additional Realtek 8723DE Bluetooth devices */
498 { USB_DEVICE(0x0bda, 0xb009), .driver_info
= BTUSB_REALTEK
},
499 { USB_DEVICE(0x2ff8, 0xb011), .driver_info
= BTUSB_REALTEK
},
501 /* Additional Realtek 8761B Bluetooth devices */
502 { USB_DEVICE(0x2357, 0x0604), .driver_info
= BTUSB_REALTEK
|
503 BTUSB_WIDEBAND_SPEECH
},
505 /* Additional Realtek 8761BU Bluetooth devices */
506 { USB_DEVICE(0x0b05, 0x190e), .driver_info
= BTUSB_REALTEK
|
507 BTUSB_WIDEBAND_SPEECH
},
508 { USB_DEVICE(0x2550, 0x8761), .driver_info
= BTUSB_REALTEK
|
509 BTUSB_WIDEBAND_SPEECH
},
511 /* Additional Realtek 8821AE Bluetooth devices */
512 { USB_DEVICE(0x0b05, 0x17dc), .driver_info
= BTUSB_REALTEK
},
513 { USB_DEVICE(0x13d3, 0x3414), .driver_info
= BTUSB_REALTEK
},
514 { USB_DEVICE(0x13d3, 0x3458), .driver_info
= BTUSB_REALTEK
},
515 { USB_DEVICE(0x13d3, 0x3461), .driver_info
= BTUSB_REALTEK
},
516 { USB_DEVICE(0x13d3, 0x3462), .driver_info
= BTUSB_REALTEK
},
518 /* Additional Realtek 8822BE Bluetooth devices */
519 { USB_DEVICE(0x13d3, 0x3526), .driver_info
= BTUSB_REALTEK
},
520 { USB_DEVICE(0x0b05, 0x185c), .driver_info
= BTUSB_REALTEK
},
522 /* Additional Realtek 8822CE Bluetooth devices */
523 { USB_DEVICE(0x04ca, 0x4005), .driver_info
= BTUSB_REALTEK
|
524 BTUSB_WIDEBAND_SPEECH
},
525 { USB_DEVICE(0x04c5, 0x161f), .driver_info
= BTUSB_REALTEK
|
526 BTUSB_WIDEBAND_SPEECH
},
527 { USB_DEVICE(0x0b05, 0x18ef), .driver_info
= BTUSB_REALTEK
|
528 BTUSB_WIDEBAND_SPEECH
},
529 { USB_DEVICE(0x13d3, 0x3548), .driver_info
= BTUSB_REALTEK
|
530 BTUSB_WIDEBAND_SPEECH
},
531 { USB_DEVICE(0x13d3, 0x3549), .driver_info
= BTUSB_REALTEK
|
532 BTUSB_WIDEBAND_SPEECH
},
533 { USB_DEVICE(0x13d3, 0x3553), .driver_info
= BTUSB_REALTEK
|
534 BTUSB_WIDEBAND_SPEECH
},
535 { USB_DEVICE(0x13d3, 0x3555), .driver_info
= BTUSB_REALTEK
|
536 BTUSB_WIDEBAND_SPEECH
},
537 { USB_DEVICE(0x2ff8, 0x3051), .driver_info
= BTUSB_REALTEK
|
538 BTUSB_WIDEBAND_SPEECH
},
539 { USB_DEVICE(0x1358, 0xc123), .driver_info
= BTUSB_REALTEK
|
540 BTUSB_WIDEBAND_SPEECH
},
541 { USB_DEVICE(0x0bda, 0xc123), .driver_info
= BTUSB_REALTEK
|
542 BTUSB_WIDEBAND_SPEECH
},
543 { USB_DEVICE(0x0cb5, 0xc547), .driver_info
= BTUSB_REALTEK
|
544 BTUSB_WIDEBAND_SPEECH
},
546 /* Silicon Wave based devices */
547 { USB_DEVICE(0x0c10, 0x0000), .driver_info
= BTUSB_SWAVE
},
549 { } /* Terminating entry */
552 /* The Bluetooth USB module build into some devices needs to be reset on resume,
553 * this is a problem with the platform (likely shutting off all power) not with
554 * the module itself. So we use a DMI list to match known broken platforms.
556 static const struct dmi_system_id btusb_needs_reset_resume_table
[] = {
558 /* Dell OptiPlex 3060 (QCA ROME device 0cf3:e007) */
560 DMI_MATCH(DMI_SYS_VENDOR
, "Dell Inc."),
561 DMI_MATCH(DMI_PRODUCT_NAME
, "OptiPlex 3060"),
565 /* Dell XPS 9360 (QCA ROME device 0cf3:e300) */
567 DMI_MATCH(DMI_SYS_VENDOR
, "Dell Inc."),
568 DMI_MATCH(DMI_PRODUCT_NAME
, "XPS 13 9360"),
572 /* Dell Inspiron 5565 (QCA ROME device 0cf3:e009) */
574 DMI_MATCH(DMI_SYS_VENDOR
, "Dell Inc."),
575 DMI_MATCH(DMI_PRODUCT_NAME
, "Inspiron 5565"),
581 #define BTUSB_MAX_ISOC_FRAMES 10
583 #define BTUSB_INTR_RUNNING 0
584 #define BTUSB_BULK_RUNNING 1
585 #define BTUSB_ISOC_RUNNING 2
586 #define BTUSB_SUSPENDING 3
587 #define BTUSB_DID_ISO_RESUME 4
588 #define BTUSB_BOOTLOADER 5
589 #define BTUSB_DOWNLOADING 6
590 #define BTUSB_FIRMWARE_LOADED 7
591 #define BTUSB_FIRMWARE_FAILED 8
592 #define BTUSB_BOOTING 9
593 #define BTUSB_DIAG_RUNNING 10
594 #define BTUSB_OOB_WAKE_ENABLED 11
595 #define BTUSB_HW_RESET_ACTIVE 12
596 #define BTUSB_TX_WAIT_VND_EVT 13
597 #define BTUSB_WAKEUP_AUTOSUSPEND 14
598 #define BTUSB_USE_ALT3_FOR_WBS 15
601 struct hci_dev
*hdev
;
602 struct usb_device
*udev
;
603 struct usb_interface
*intf
;
604 struct usb_interface
*isoc
;
605 struct usb_interface
*diag
;
610 struct work_struct work
;
611 struct work_struct waker
;
613 struct usb_anchor deferred
;
614 struct usb_anchor tx_anchor
;
618 struct usb_anchor intr_anchor
;
619 struct usb_anchor bulk_anchor
;
620 struct usb_anchor isoc_anchor
;
621 struct usb_anchor diag_anchor
;
622 struct usb_anchor ctrl_anchor
;
625 struct sk_buff
*evt_skb
;
626 struct sk_buff
*acl_skb
;
627 struct sk_buff
*sco_skb
;
629 struct usb_endpoint_descriptor
*intr_ep
;
630 struct usb_endpoint_descriptor
*bulk_tx_ep
;
631 struct usb_endpoint_descriptor
*bulk_rx_ep
;
632 struct usb_endpoint_descriptor
*isoc_tx_ep
;
633 struct usb_endpoint_descriptor
*isoc_rx_ep
;
634 struct usb_endpoint_descriptor
*diag_tx_ep
;
635 struct usb_endpoint_descriptor
*diag_rx_ep
;
637 struct gpio_desc
*reset_gpio
;
642 unsigned int sco_num
;
643 unsigned int air_mode
;
644 bool usb_alt6_packet_flow
;
648 int (*recv_event
)(struct hci_dev
*hdev
, struct sk_buff
*skb
);
649 int (*recv_acl
)(struct hci_dev
*hdev
, struct sk_buff
*skb
);
650 int (*recv_bulk
)(struct btusb_data
*data
, void *buffer
, int count
);
652 int (*setup_on_usb
)(struct hci_dev
*hdev
);
654 int oob_wake_irq
; /* irq for out-of-band wake-on-bt */
655 unsigned cmd_timeout_cnt
;
658 static void btusb_intel_cmd_timeout(struct hci_dev
*hdev
)
660 struct btusb_data
*data
= hci_get_drvdata(hdev
);
661 struct gpio_desc
*reset_gpio
= data
->reset_gpio
;
663 if (++data
->cmd_timeout_cnt
< 5)
667 bt_dev_err(hdev
, "No way to reset. Ignoring and continuing");
672 * Toggle the hard reset line if the platform provides one. The reset
673 * is going to yank the device off the USB and then replug. So doing
674 * once is enough. The cleanup is handled correctly on the way out
675 * (standard USB disconnect), and the new device is detected cleanly
676 * and bound to the driver again like it should be.
678 if (test_and_set_bit(BTUSB_HW_RESET_ACTIVE
, &data
->flags
)) {
679 bt_dev_err(hdev
, "last reset failed? Not resetting again");
683 bt_dev_err(hdev
, "Initiating HW reset via gpio");
684 gpiod_set_value_cansleep(reset_gpio
, 1);
686 gpiod_set_value_cansleep(reset_gpio
, 0);
689 static void btusb_rtl_cmd_timeout(struct hci_dev
*hdev
)
691 struct btusb_data
*data
= hci_get_drvdata(hdev
);
692 struct gpio_desc
*reset_gpio
= data
->reset_gpio
;
694 if (++data
->cmd_timeout_cnt
< 5)
698 bt_dev_err(hdev
, "No gpio to reset Realtek device, ignoring");
702 /* Toggle the hard reset line. The Realtek device is going to
703 * yank itself off the USB and then replug. The cleanup is handled
704 * correctly on the way out (standard USB disconnect), and the new
705 * device is detected cleanly and bound to the driver again like
708 if (test_and_set_bit(BTUSB_HW_RESET_ACTIVE
, &data
->flags
)) {
709 bt_dev_err(hdev
, "last reset failed? Not resetting again");
713 bt_dev_err(hdev
, "Reset Realtek device via gpio");
714 gpiod_set_value_cansleep(reset_gpio
, 1);
716 gpiod_set_value_cansleep(reset_gpio
, 0);
719 static void btusb_qca_cmd_timeout(struct hci_dev
*hdev
)
721 struct btusb_data
*data
= hci_get_drvdata(hdev
);
724 if (++data
->cmd_timeout_cnt
< 5)
727 bt_dev_err(hdev
, "Multiple cmd timeouts seen. Resetting usb device.");
728 /* This is not an unbalanced PM reference since the device will reset */
729 err
= usb_autopm_get_interface(data
->intf
);
731 usb_queue_reset_device(data
->intf
);
733 bt_dev_err(hdev
, "Failed usb_autopm_get_interface with %d", err
);
736 static inline void btusb_free_frags(struct btusb_data
*data
)
740 spin_lock_irqsave(&data
->rxlock
, flags
);
742 kfree_skb(data
->evt_skb
);
743 data
->evt_skb
= NULL
;
745 kfree_skb(data
->acl_skb
);
746 data
->acl_skb
= NULL
;
748 kfree_skb(data
->sco_skb
);
749 data
->sco_skb
= NULL
;
751 spin_unlock_irqrestore(&data
->rxlock
, flags
);
754 static int btusb_recv_intr(struct btusb_data
*data
, void *buffer
, int count
)
760 spin_lock_irqsave(&data
->rxlock
, flags
);
767 skb
= bt_skb_alloc(HCI_MAX_EVENT_SIZE
, GFP_ATOMIC
);
773 hci_skb_pkt_type(skb
) = HCI_EVENT_PKT
;
774 hci_skb_expect(skb
) = HCI_EVENT_HDR_SIZE
;
777 len
= min_t(uint
, hci_skb_expect(skb
), count
);
778 skb_put_data(skb
, buffer
, len
);
782 hci_skb_expect(skb
) -= len
;
784 if (skb
->len
== HCI_EVENT_HDR_SIZE
) {
785 /* Complete event header */
786 hci_skb_expect(skb
) = hci_event_hdr(skb
)->plen
;
788 if (skb_tailroom(skb
) < hci_skb_expect(skb
)) {
797 if (!hci_skb_expect(skb
)) {
799 data
->recv_event(data
->hdev
, skb
);
805 spin_unlock_irqrestore(&data
->rxlock
, flags
);
810 static int btusb_recv_bulk(struct btusb_data
*data
, void *buffer
, int count
)
816 spin_lock_irqsave(&data
->rxlock
, flags
);
823 skb
= bt_skb_alloc(HCI_MAX_FRAME_SIZE
, GFP_ATOMIC
);
829 hci_skb_pkt_type(skb
) = HCI_ACLDATA_PKT
;
830 hci_skb_expect(skb
) = HCI_ACL_HDR_SIZE
;
833 len
= min_t(uint
, hci_skb_expect(skb
), count
);
834 skb_put_data(skb
, buffer
, len
);
838 hci_skb_expect(skb
) -= len
;
840 if (skb
->len
== HCI_ACL_HDR_SIZE
) {
841 __le16 dlen
= hci_acl_hdr(skb
)->dlen
;
843 /* Complete ACL header */
844 hci_skb_expect(skb
) = __le16_to_cpu(dlen
);
846 if (skb_tailroom(skb
) < hci_skb_expect(skb
)) {
855 if (!hci_skb_expect(skb
)) {
857 data
->recv_acl(data
->hdev
, skb
);
863 spin_unlock_irqrestore(&data
->rxlock
, flags
);
868 static int btusb_recv_isoc(struct btusb_data
*data
, void *buffer
, int count
)
874 spin_lock_irqsave(&data
->rxlock
, flags
);
881 skb
= bt_skb_alloc(HCI_MAX_SCO_SIZE
, GFP_ATOMIC
);
887 hci_skb_pkt_type(skb
) = HCI_SCODATA_PKT
;
888 hci_skb_expect(skb
) = HCI_SCO_HDR_SIZE
;
891 len
= min_t(uint
, hci_skb_expect(skb
), count
);
892 skb_put_data(skb
, buffer
, len
);
896 hci_skb_expect(skb
) -= len
;
898 if (skb
->len
== HCI_SCO_HDR_SIZE
) {
899 /* Complete SCO header */
900 hci_skb_expect(skb
) = hci_sco_hdr(skb
)->dlen
;
902 if (skb_tailroom(skb
) < hci_skb_expect(skb
)) {
911 if (!hci_skb_expect(skb
)) {
913 hci_recv_frame(data
->hdev
, skb
);
919 spin_unlock_irqrestore(&data
->rxlock
, flags
);
924 static void btusb_intr_complete(struct urb
*urb
)
926 struct hci_dev
*hdev
= urb
->context
;
927 struct btusb_data
*data
= hci_get_drvdata(hdev
);
930 BT_DBG("%s urb %p status %d count %d", hdev
->name
, urb
, urb
->status
,
933 if (!test_bit(HCI_RUNNING
, &hdev
->flags
))
936 if (urb
->status
== 0) {
937 hdev
->stat
.byte_rx
+= urb
->actual_length
;
939 if (btusb_recv_intr(data
, urb
->transfer_buffer
,
940 urb
->actual_length
) < 0) {
941 bt_dev_err(hdev
, "corrupted event packet");
944 } else if (urb
->status
== -ENOENT
) {
945 /* Avoid suspend failed when usb_kill_urb */
949 if (!test_bit(BTUSB_INTR_RUNNING
, &data
->flags
))
952 usb_mark_last_busy(data
->udev
);
953 usb_anchor_urb(urb
, &data
->intr_anchor
);
955 err
= usb_submit_urb(urb
, GFP_ATOMIC
);
957 /* -EPERM: urb is being killed;
958 * -ENODEV: device got disconnected
960 if (err
!= -EPERM
&& err
!= -ENODEV
)
961 bt_dev_err(hdev
, "urb %p failed to resubmit (%d)",
963 usb_unanchor_urb(urb
);
967 static int btusb_submit_intr_urb(struct hci_dev
*hdev
, gfp_t mem_flags
)
969 struct btusb_data
*data
= hci_get_drvdata(hdev
);
975 BT_DBG("%s", hdev
->name
);
980 urb
= usb_alloc_urb(0, mem_flags
);
984 size
= le16_to_cpu(data
->intr_ep
->wMaxPacketSize
);
986 buf
= kmalloc(size
, mem_flags
);
992 pipe
= usb_rcvintpipe(data
->udev
, data
->intr_ep
->bEndpointAddress
);
994 usb_fill_int_urb(urb
, data
->udev
, pipe
, buf
, size
,
995 btusb_intr_complete
, hdev
, data
->intr_ep
->bInterval
);
997 urb
->transfer_flags
|= URB_FREE_BUFFER
;
999 usb_anchor_urb(urb
, &data
->intr_anchor
);
1001 err
= usb_submit_urb(urb
, mem_flags
);
1003 if (err
!= -EPERM
&& err
!= -ENODEV
)
1004 bt_dev_err(hdev
, "urb %p submission failed (%d)",
1006 usb_unanchor_urb(urb
);
1014 static void btusb_bulk_complete(struct urb
*urb
)
1016 struct hci_dev
*hdev
= urb
->context
;
1017 struct btusb_data
*data
= hci_get_drvdata(hdev
);
1020 BT_DBG("%s urb %p status %d count %d", hdev
->name
, urb
, urb
->status
,
1021 urb
->actual_length
);
1023 if (!test_bit(HCI_RUNNING
, &hdev
->flags
))
1026 if (urb
->status
== 0) {
1027 hdev
->stat
.byte_rx
+= urb
->actual_length
;
1029 if (data
->recv_bulk(data
, urb
->transfer_buffer
,
1030 urb
->actual_length
) < 0) {
1031 bt_dev_err(hdev
, "corrupted ACL packet");
1032 hdev
->stat
.err_rx
++;
1034 } else if (urb
->status
== -ENOENT
) {
1035 /* Avoid suspend failed when usb_kill_urb */
1039 if (!test_bit(BTUSB_BULK_RUNNING
, &data
->flags
))
1042 usb_anchor_urb(urb
, &data
->bulk_anchor
);
1043 usb_mark_last_busy(data
->udev
);
1045 err
= usb_submit_urb(urb
, GFP_ATOMIC
);
1047 /* -EPERM: urb is being killed;
1048 * -ENODEV: device got disconnected
1050 if (err
!= -EPERM
&& err
!= -ENODEV
)
1051 bt_dev_err(hdev
, "urb %p failed to resubmit (%d)",
1053 usb_unanchor_urb(urb
);
1057 static int btusb_submit_bulk_urb(struct hci_dev
*hdev
, gfp_t mem_flags
)
1059 struct btusb_data
*data
= hci_get_drvdata(hdev
);
1063 int err
, size
= HCI_MAX_FRAME_SIZE
;
1065 BT_DBG("%s", hdev
->name
);
1067 if (!data
->bulk_rx_ep
)
1070 urb
= usb_alloc_urb(0, mem_flags
);
1074 buf
= kmalloc(size
, mem_flags
);
1080 pipe
= usb_rcvbulkpipe(data
->udev
, data
->bulk_rx_ep
->bEndpointAddress
);
1082 usb_fill_bulk_urb(urb
, data
->udev
, pipe
, buf
, size
,
1083 btusb_bulk_complete
, hdev
);
1085 urb
->transfer_flags
|= URB_FREE_BUFFER
;
1087 usb_mark_last_busy(data
->udev
);
1088 usb_anchor_urb(urb
, &data
->bulk_anchor
);
1090 err
= usb_submit_urb(urb
, mem_flags
);
1092 if (err
!= -EPERM
&& err
!= -ENODEV
)
1093 bt_dev_err(hdev
, "urb %p submission failed (%d)",
1095 usb_unanchor_urb(urb
);
1103 static void btusb_isoc_complete(struct urb
*urb
)
1105 struct hci_dev
*hdev
= urb
->context
;
1106 struct btusb_data
*data
= hci_get_drvdata(hdev
);
1109 BT_DBG("%s urb %p status %d count %d", hdev
->name
, urb
, urb
->status
,
1110 urb
->actual_length
);
1112 if (!test_bit(HCI_RUNNING
, &hdev
->flags
))
1115 if (urb
->status
== 0) {
1116 for (i
= 0; i
< urb
->number_of_packets
; i
++) {
1117 unsigned int offset
= urb
->iso_frame_desc
[i
].offset
;
1118 unsigned int length
= urb
->iso_frame_desc
[i
].actual_length
;
1120 if (urb
->iso_frame_desc
[i
].status
)
1123 hdev
->stat
.byte_rx
+= length
;
1125 if (btusb_recv_isoc(data
, urb
->transfer_buffer
+ offset
,
1127 bt_dev_err(hdev
, "corrupted SCO packet");
1128 hdev
->stat
.err_rx
++;
1131 } else if (urb
->status
== -ENOENT
) {
1132 /* Avoid suspend failed when usb_kill_urb */
1136 if (!test_bit(BTUSB_ISOC_RUNNING
, &data
->flags
))
1139 usb_anchor_urb(urb
, &data
->isoc_anchor
);
1141 err
= usb_submit_urb(urb
, GFP_ATOMIC
);
1143 /* -EPERM: urb is being killed;
1144 * -ENODEV: device got disconnected
1146 if (err
!= -EPERM
&& err
!= -ENODEV
)
1147 bt_dev_err(hdev
, "urb %p failed to resubmit (%d)",
1149 usb_unanchor_urb(urb
);
1153 static inline void __fill_isoc_descriptor_msbc(struct urb
*urb
, int len
,
1154 int mtu
, struct btusb_data
*data
)
1157 unsigned int interval
;
1159 BT_DBG("len %d mtu %d", len
, mtu
);
1161 /* For mSBC ALT 6 setting the host will send the packet at continuous
1162 * flow. As per core spec 5, vol 4, part B, table 2.1. For ALT setting
1163 * 6 the HCI PACKET INTERVAL should be 7.5ms for every usb packets.
1164 * To maintain the rate we send 63bytes of usb packets alternatively for
1165 * 7ms and 8ms to maintain the rate as 7.5ms.
1167 if (data
->usb_alt6_packet_flow
) {
1169 data
->usb_alt6_packet_flow
= false;
1172 data
->usb_alt6_packet_flow
= true;
1175 for (i
= 0; i
< interval
; i
++) {
1176 urb
->iso_frame_desc
[i
].offset
= offset
;
1177 urb
->iso_frame_desc
[i
].length
= offset
;
1180 if (len
&& i
< BTUSB_MAX_ISOC_FRAMES
) {
1181 urb
->iso_frame_desc
[i
].offset
= offset
;
1182 urb
->iso_frame_desc
[i
].length
= len
;
1186 urb
->number_of_packets
= i
;
1189 static inline void __fill_isoc_descriptor(struct urb
*urb
, int len
, int mtu
)
1193 BT_DBG("len %d mtu %d", len
, mtu
);
1195 for (i
= 0; i
< BTUSB_MAX_ISOC_FRAMES
&& len
>= mtu
;
1196 i
++, offset
+= mtu
, len
-= mtu
) {
1197 urb
->iso_frame_desc
[i
].offset
= offset
;
1198 urb
->iso_frame_desc
[i
].length
= mtu
;
1201 if (len
&& i
< BTUSB_MAX_ISOC_FRAMES
) {
1202 urb
->iso_frame_desc
[i
].offset
= offset
;
1203 urb
->iso_frame_desc
[i
].length
= len
;
1207 urb
->number_of_packets
= i
;
1210 static int btusb_submit_isoc_urb(struct hci_dev
*hdev
, gfp_t mem_flags
)
1212 struct btusb_data
*data
= hci_get_drvdata(hdev
);
1218 BT_DBG("%s", hdev
->name
);
1220 if (!data
->isoc_rx_ep
)
1223 urb
= usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES
, mem_flags
);
1227 size
= le16_to_cpu(data
->isoc_rx_ep
->wMaxPacketSize
) *
1228 BTUSB_MAX_ISOC_FRAMES
;
1230 buf
= kmalloc(size
, mem_flags
);
1236 pipe
= usb_rcvisocpipe(data
->udev
, data
->isoc_rx_ep
->bEndpointAddress
);
1238 usb_fill_int_urb(urb
, data
->udev
, pipe
, buf
, size
, btusb_isoc_complete
,
1239 hdev
, data
->isoc_rx_ep
->bInterval
);
1241 urb
->transfer_flags
= URB_FREE_BUFFER
| URB_ISO_ASAP
;
1243 __fill_isoc_descriptor(urb
, size
,
1244 le16_to_cpu(data
->isoc_rx_ep
->wMaxPacketSize
));
1246 usb_anchor_urb(urb
, &data
->isoc_anchor
);
1248 err
= usb_submit_urb(urb
, mem_flags
);
1250 if (err
!= -EPERM
&& err
!= -ENODEV
)
1251 bt_dev_err(hdev
, "urb %p submission failed (%d)",
1253 usb_unanchor_urb(urb
);
1261 static void btusb_diag_complete(struct urb
*urb
)
1263 struct hci_dev
*hdev
= urb
->context
;
1264 struct btusb_data
*data
= hci_get_drvdata(hdev
);
1267 BT_DBG("%s urb %p status %d count %d", hdev
->name
, urb
, urb
->status
,
1268 urb
->actual_length
);
1270 if (urb
->status
== 0) {
1271 struct sk_buff
*skb
;
1273 skb
= bt_skb_alloc(urb
->actual_length
, GFP_ATOMIC
);
1275 skb_put_data(skb
, urb
->transfer_buffer
,
1276 urb
->actual_length
);
1277 hci_recv_diag(hdev
, skb
);
1279 } else if (urb
->status
== -ENOENT
) {
1280 /* Avoid suspend failed when usb_kill_urb */
1284 if (!test_bit(BTUSB_DIAG_RUNNING
, &data
->flags
))
1287 usb_anchor_urb(urb
, &data
->diag_anchor
);
1288 usb_mark_last_busy(data
->udev
);
1290 err
= usb_submit_urb(urb
, GFP_ATOMIC
);
1292 /* -EPERM: urb is being killed;
1293 * -ENODEV: device got disconnected
1295 if (err
!= -EPERM
&& err
!= -ENODEV
)
1296 bt_dev_err(hdev
, "urb %p failed to resubmit (%d)",
1298 usb_unanchor_urb(urb
);
1302 static int btusb_submit_diag_urb(struct hci_dev
*hdev
, gfp_t mem_flags
)
1304 struct btusb_data
*data
= hci_get_drvdata(hdev
);
1308 int err
, size
= HCI_MAX_FRAME_SIZE
;
1310 BT_DBG("%s", hdev
->name
);
1312 if (!data
->diag_rx_ep
)
1315 urb
= usb_alloc_urb(0, mem_flags
);
1319 buf
= kmalloc(size
, mem_flags
);
1325 pipe
= usb_rcvbulkpipe(data
->udev
, data
->diag_rx_ep
->bEndpointAddress
);
1327 usb_fill_bulk_urb(urb
, data
->udev
, pipe
, buf
, size
,
1328 btusb_diag_complete
, hdev
);
1330 urb
->transfer_flags
|= URB_FREE_BUFFER
;
1332 usb_mark_last_busy(data
->udev
);
1333 usb_anchor_urb(urb
, &data
->diag_anchor
);
1335 err
= usb_submit_urb(urb
, mem_flags
);
1337 if (err
!= -EPERM
&& err
!= -ENODEV
)
1338 bt_dev_err(hdev
, "urb %p submission failed (%d)",
1340 usb_unanchor_urb(urb
);
1348 static void btusb_tx_complete(struct urb
*urb
)
1350 struct sk_buff
*skb
= urb
->context
;
1351 struct hci_dev
*hdev
= (struct hci_dev
*)skb
->dev
;
1352 struct btusb_data
*data
= hci_get_drvdata(hdev
);
1353 unsigned long flags
;
1355 BT_DBG("%s urb %p status %d count %d", hdev
->name
, urb
, urb
->status
,
1356 urb
->actual_length
);
1358 if (!test_bit(HCI_RUNNING
, &hdev
->flags
))
1362 hdev
->stat
.byte_tx
+= urb
->transfer_buffer_length
;
1364 hdev
->stat
.err_tx
++;
1367 spin_lock_irqsave(&data
->txlock
, flags
);
1368 data
->tx_in_flight
--;
1369 spin_unlock_irqrestore(&data
->txlock
, flags
);
1371 kfree(urb
->setup_packet
);
1376 static void btusb_isoc_tx_complete(struct urb
*urb
)
1378 struct sk_buff
*skb
= urb
->context
;
1379 struct hci_dev
*hdev
= (struct hci_dev
*)skb
->dev
;
1381 BT_DBG("%s urb %p status %d count %d", hdev
->name
, urb
, urb
->status
,
1382 urb
->actual_length
);
1384 if (!test_bit(HCI_RUNNING
, &hdev
->flags
))
1388 hdev
->stat
.byte_tx
+= urb
->transfer_buffer_length
;
1390 hdev
->stat
.err_tx
++;
1393 kfree(urb
->setup_packet
);
1398 static int btusb_open(struct hci_dev
*hdev
)
1400 struct btusb_data
*data
= hci_get_drvdata(hdev
);
1403 BT_DBG("%s", hdev
->name
);
1405 err
= usb_autopm_get_interface(data
->intf
);
1409 /* Patching USB firmware files prior to starting any URBs of HCI path
1410 * It is more safe to use USB bulk channel for downloading USB patch
1412 if (data
->setup_on_usb
) {
1413 err
= data
->setup_on_usb(hdev
);
1418 data
->intf
->needs_remote_wakeup
= 1;
1420 if (test_and_set_bit(BTUSB_INTR_RUNNING
, &data
->flags
))
1423 err
= btusb_submit_intr_urb(hdev
, GFP_KERNEL
);
1427 err
= btusb_submit_bulk_urb(hdev
, GFP_KERNEL
);
1429 usb_kill_anchored_urbs(&data
->intr_anchor
);
1433 set_bit(BTUSB_BULK_RUNNING
, &data
->flags
);
1434 btusb_submit_bulk_urb(hdev
, GFP_KERNEL
);
1437 if (!btusb_submit_diag_urb(hdev
, GFP_KERNEL
))
1438 set_bit(BTUSB_DIAG_RUNNING
, &data
->flags
);
1442 usb_autopm_put_interface(data
->intf
);
1446 clear_bit(BTUSB_INTR_RUNNING
, &data
->flags
);
1448 usb_autopm_put_interface(data
->intf
);
1452 static void btusb_stop_traffic(struct btusb_data
*data
)
1454 usb_kill_anchored_urbs(&data
->intr_anchor
);
1455 usb_kill_anchored_urbs(&data
->bulk_anchor
);
1456 usb_kill_anchored_urbs(&data
->isoc_anchor
);
1457 usb_kill_anchored_urbs(&data
->diag_anchor
);
1458 usb_kill_anchored_urbs(&data
->ctrl_anchor
);
1461 static int btusb_close(struct hci_dev
*hdev
)
1463 struct btusb_data
*data
= hci_get_drvdata(hdev
);
1466 BT_DBG("%s", hdev
->name
);
1468 cancel_work_sync(&data
->work
);
1469 cancel_work_sync(&data
->waker
);
1471 clear_bit(BTUSB_ISOC_RUNNING
, &data
->flags
);
1472 clear_bit(BTUSB_BULK_RUNNING
, &data
->flags
);
1473 clear_bit(BTUSB_INTR_RUNNING
, &data
->flags
);
1474 clear_bit(BTUSB_DIAG_RUNNING
, &data
->flags
);
1476 btusb_stop_traffic(data
);
1477 btusb_free_frags(data
);
1479 err
= usb_autopm_get_interface(data
->intf
);
1483 data
->intf
->needs_remote_wakeup
= 0;
1485 /* Enable remote wake up for auto-suspend */
1486 if (test_bit(BTUSB_WAKEUP_AUTOSUSPEND
, &data
->flags
))
1487 data
->intf
->needs_remote_wakeup
= 1;
1489 usb_autopm_put_interface(data
->intf
);
1492 usb_scuttle_anchored_urbs(&data
->deferred
);
1496 static int btusb_flush(struct hci_dev
*hdev
)
1498 struct btusb_data
*data
= hci_get_drvdata(hdev
);
1500 BT_DBG("%s", hdev
->name
);
1502 usb_kill_anchored_urbs(&data
->tx_anchor
);
1503 btusb_free_frags(data
);
1508 static struct urb
*alloc_ctrl_urb(struct hci_dev
*hdev
, struct sk_buff
*skb
)
1510 struct btusb_data
*data
= hci_get_drvdata(hdev
);
1511 struct usb_ctrlrequest
*dr
;
1515 urb
= usb_alloc_urb(0, GFP_KERNEL
);
1517 return ERR_PTR(-ENOMEM
);
1519 dr
= kmalloc(sizeof(*dr
), GFP_KERNEL
);
1522 return ERR_PTR(-ENOMEM
);
1525 dr
->bRequestType
= data
->cmdreq_type
;
1526 dr
->bRequest
= data
->cmdreq
;
1529 dr
->wLength
= __cpu_to_le16(skb
->len
);
1531 pipe
= usb_sndctrlpipe(data
->udev
, 0x00);
1533 usb_fill_control_urb(urb
, data
->udev
, pipe
, (void *)dr
,
1534 skb
->data
, skb
->len
, btusb_tx_complete
, skb
);
1536 skb
->dev
= (void *)hdev
;
1541 static struct urb
*alloc_bulk_urb(struct hci_dev
*hdev
, struct sk_buff
*skb
)
1543 struct btusb_data
*data
= hci_get_drvdata(hdev
);
1547 if (!data
->bulk_tx_ep
)
1548 return ERR_PTR(-ENODEV
);
1550 urb
= usb_alloc_urb(0, GFP_KERNEL
);
1552 return ERR_PTR(-ENOMEM
);
1554 pipe
= usb_sndbulkpipe(data
->udev
, data
->bulk_tx_ep
->bEndpointAddress
);
1556 usb_fill_bulk_urb(urb
, data
->udev
, pipe
,
1557 skb
->data
, skb
->len
, btusb_tx_complete
, skb
);
1559 skb
->dev
= (void *)hdev
;
1564 static struct urb
*alloc_isoc_urb(struct hci_dev
*hdev
, struct sk_buff
*skb
)
1566 struct btusb_data
*data
= hci_get_drvdata(hdev
);
1570 if (!data
->isoc_tx_ep
)
1571 return ERR_PTR(-ENODEV
);
1573 urb
= usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES
, GFP_KERNEL
);
1575 return ERR_PTR(-ENOMEM
);
1577 pipe
= usb_sndisocpipe(data
->udev
, data
->isoc_tx_ep
->bEndpointAddress
);
1579 usb_fill_int_urb(urb
, data
->udev
, pipe
,
1580 skb
->data
, skb
->len
, btusb_isoc_tx_complete
,
1581 skb
, data
->isoc_tx_ep
->bInterval
);
1583 urb
->transfer_flags
= URB_ISO_ASAP
;
1585 if (data
->isoc_altsetting
== 6)
1586 __fill_isoc_descriptor_msbc(urb
, skb
->len
,
1587 le16_to_cpu(data
->isoc_tx_ep
->wMaxPacketSize
),
1590 __fill_isoc_descriptor(urb
, skb
->len
,
1591 le16_to_cpu(data
->isoc_tx_ep
->wMaxPacketSize
));
1592 skb
->dev
= (void *)hdev
;
1597 static int submit_tx_urb(struct hci_dev
*hdev
, struct urb
*urb
)
1599 struct btusb_data
*data
= hci_get_drvdata(hdev
);
1602 usb_anchor_urb(urb
, &data
->tx_anchor
);
1604 err
= usb_submit_urb(urb
, GFP_KERNEL
);
1606 if (err
!= -EPERM
&& err
!= -ENODEV
)
1607 bt_dev_err(hdev
, "urb %p submission failed (%d)",
1609 kfree(urb
->setup_packet
);
1610 usb_unanchor_urb(urb
);
1612 usb_mark_last_busy(data
->udev
);
1619 static int submit_or_queue_tx_urb(struct hci_dev
*hdev
, struct urb
*urb
)
1621 struct btusb_data
*data
= hci_get_drvdata(hdev
);
1622 unsigned long flags
;
1625 spin_lock_irqsave(&data
->txlock
, flags
);
1626 suspending
= test_bit(BTUSB_SUSPENDING
, &data
->flags
);
1628 data
->tx_in_flight
++;
1629 spin_unlock_irqrestore(&data
->txlock
, flags
);
1632 return submit_tx_urb(hdev
, urb
);
1634 usb_anchor_urb(urb
, &data
->deferred
);
1635 schedule_work(&data
->waker
);
1641 static int btusb_send_frame(struct hci_dev
*hdev
, struct sk_buff
*skb
)
1645 BT_DBG("%s", hdev
->name
);
1647 switch (hci_skb_pkt_type(skb
)) {
1648 case HCI_COMMAND_PKT
:
1649 urb
= alloc_ctrl_urb(hdev
, skb
);
1651 return PTR_ERR(urb
);
1653 hdev
->stat
.cmd_tx
++;
1654 return submit_or_queue_tx_urb(hdev
, urb
);
1656 case HCI_ACLDATA_PKT
:
1657 urb
= alloc_bulk_urb(hdev
, skb
);
1659 return PTR_ERR(urb
);
1661 hdev
->stat
.acl_tx
++;
1662 return submit_or_queue_tx_urb(hdev
, urb
);
1664 case HCI_SCODATA_PKT
:
1665 if (hci_conn_num(hdev
, SCO_LINK
) < 1)
1668 urb
= alloc_isoc_urb(hdev
, skb
);
1670 return PTR_ERR(urb
);
1672 hdev
->stat
.sco_tx
++;
1673 return submit_tx_urb(hdev
, urb
);
1679 static void btusb_notify(struct hci_dev
*hdev
, unsigned int evt
)
1681 struct btusb_data
*data
= hci_get_drvdata(hdev
);
1683 BT_DBG("%s evt %d", hdev
->name
, evt
);
1685 if (hci_conn_num(hdev
, SCO_LINK
) != data
->sco_num
) {
1686 data
->sco_num
= hci_conn_num(hdev
, SCO_LINK
);
1687 data
->air_mode
= evt
;
1688 schedule_work(&data
->work
);
1692 static inline int __set_isoc_interface(struct hci_dev
*hdev
, int altsetting
)
1694 struct btusb_data
*data
= hci_get_drvdata(hdev
);
1695 struct usb_interface
*intf
= data
->isoc
;
1696 struct usb_endpoint_descriptor
*ep_desc
;
1702 err
= usb_set_interface(data
->udev
, data
->isoc_ifnum
, altsetting
);
1704 bt_dev_err(hdev
, "setting interface failed (%d)", -err
);
1708 data
->isoc_altsetting
= altsetting
;
1710 data
->isoc_tx_ep
= NULL
;
1711 data
->isoc_rx_ep
= NULL
;
1713 for (i
= 0; i
< intf
->cur_altsetting
->desc
.bNumEndpoints
; i
++) {
1714 ep_desc
= &intf
->cur_altsetting
->endpoint
[i
].desc
;
1716 if (!data
->isoc_tx_ep
&& usb_endpoint_is_isoc_out(ep_desc
)) {
1717 data
->isoc_tx_ep
= ep_desc
;
1721 if (!data
->isoc_rx_ep
&& usb_endpoint_is_isoc_in(ep_desc
)) {
1722 data
->isoc_rx_ep
= ep_desc
;
1727 if (!data
->isoc_tx_ep
|| !data
->isoc_rx_ep
) {
1728 bt_dev_err(hdev
, "invalid SCO descriptors");
1735 static int btusb_switch_alt_setting(struct hci_dev
*hdev
, int new_alts
)
1737 struct btusb_data
*data
= hci_get_drvdata(hdev
);
1740 if (data
->isoc_altsetting
!= new_alts
) {
1741 unsigned long flags
;
1743 clear_bit(BTUSB_ISOC_RUNNING
, &data
->flags
);
1744 usb_kill_anchored_urbs(&data
->isoc_anchor
);
1746 /* When isochronous alternate setting needs to be
1747 * changed, because SCO connection has been added
1748 * or removed, a packet fragment may be left in the
1749 * reassembling state. This could lead to wrongly
1750 * assembled fragments.
1752 * Clear outstanding fragment when selecting a new
1753 * alternate setting.
1755 spin_lock_irqsave(&data
->rxlock
, flags
);
1756 kfree_skb(data
->sco_skb
);
1757 data
->sco_skb
= NULL
;
1758 spin_unlock_irqrestore(&data
->rxlock
, flags
);
1760 err
= __set_isoc_interface(hdev
, new_alts
);
1765 if (!test_and_set_bit(BTUSB_ISOC_RUNNING
, &data
->flags
)) {
1766 if (btusb_submit_isoc_urb(hdev
, GFP_KERNEL
) < 0)
1767 clear_bit(BTUSB_ISOC_RUNNING
, &data
->flags
);
1769 btusb_submit_isoc_urb(hdev
, GFP_KERNEL
);
1775 static struct usb_host_interface
*btusb_find_altsetting(struct btusb_data
*data
,
1778 struct usb_interface
*intf
= data
->isoc
;
1781 BT_DBG("Looking for Alt no :%d", alt
);
1786 for (i
= 0; i
< intf
->num_altsetting
; i
++) {
1787 if (intf
->altsetting
[i
].desc
.bAlternateSetting
== alt
)
1788 return &intf
->altsetting
[i
];
1794 static void btusb_work(struct work_struct
*work
)
1796 struct btusb_data
*data
= container_of(work
, struct btusb_data
, work
);
1797 struct hci_dev
*hdev
= data
->hdev
;
1801 if (data
->sco_num
> 0) {
1802 if (!test_bit(BTUSB_DID_ISO_RESUME
, &data
->flags
)) {
1803 err
= usb_autopm_get_interface(data
->isoc
? data
->isoc
: data
->intf
);
1805 clear_bit(BTUSB_ISOC_RUNNING
, &data
->flags
);
1806 usb_kill_anchored_urbs(&data
->isoc_anchor
);
1810 set_bit(BTUSB_DID_ISO_RESUME
, &data
->flags
);
1813 if (data
->air_mode
== HCI_NOTIFY_ENABLE_SCO_CVSD
) {
1814 if (hdev
->voice_setting
& 0x0020) {
1815 static const int alts
[3] = { 2, 4, 5 };
1817 new_alts
= alts
[data
->sco_num
- 1];
1819 new_alts
= data
->sco_num
;
1821 } else if (data
->air_mode
== HCI_NOTIFY_ENABLE_SCO_TRANSP
) {
1822 /* Bluetooth USB spec recommends alt 6 (63 bytes), but
1823 * many adapters do not support it. Alt 1 appears to
1824 * work for all adapters that do not have alt 6, and
1825 * which work with WBS at all. Some devices prefer
1826 * alt 3 (HCI payload >= 60 Bytes let air packet
1827 * data satisfy 60 bytes), requiring
1828 * MTU >= 3 (packets) * 25 (size) - 3 (headers) = 72
1829 * see also Core spec 5, vol 4, B 2.1.1 & Table 2.1.
1831 if (btusb_find_altsetting(data
, 6))
1833 else if (btusb_find_altsetting(data
, 3) &&
1834 hdev
->sco_mtu
>= 72 &&
1835 test_bit(BTUSB_USE_ALT3_FOR_WBS
, &data
->flags
))
1841 if (btusb_switch_alt_setting(hdev
, new_alts
) < 0)
1842 bt_dev_err(hdev
, "set USB alt:(%d) failed!", new_alts
);
1844 clear_bit(BTUSB_ISOC_RUNNING
, &data
->flags
);
1845 usb_kill_anchored_urbs(&data
->isoc_anchor
);
1847 __set_isoc_interface(hdev
, 0);
1848 if (test_and_clear_bit(BTUSB_DID_ISO_RESUME
, &data
->flags
))
1849 usb_autopm_put_interface(data
->isoc
? data
->isoc
: data
->intf
);
1853 static void btusb_waker(struct work_struct
*work
)
1855 struct btusb_data
*data
= container_of(work
, struct btusb_data
, waker
);
1858 err
= usb_autopm_get_interface(data
->intf
);
1862 usb_autopm_put_interface(data
->intf
);
1865 static int btusb_setup_bcm92035(struct hci_dev
*hdev
)
1867 struct sk_buff
*skb
;
1870 BT_DBG("%s", hdev
->name
);
1872 skb
= __hci_cmd_sync(hdev
, 0xfc3b, 1, &val
, HCI_INIT_TIMEOUT
);
1874 bt_dev_err(hdev
, "BCM92035 command failed (%ld)", PTR_ERR(skb
));
1881 static int btusb_setup_csr(struct hci_dev
*hdev
)
1883 struct btusb_data
*data
= hci_get_drvdata(hdev
);
1884 u16 bcdDevice
= le16_to_cpu(data
->udev
->descriptor
.bcdDevice
);
1885 struct hci_rp_read_local_version
*rp
;
1886 struct sk_buff
*skb
;
1887 bool is_fake
= false;
1890 BT_DBG("%s", hdev
->name
);
1892 skb
= __hci_cmd_sync(hdev
, HCI_OP_READ_LOCAL_VERSION
, 0, NULL
,
1895 int err
= PTR_ERR(skb
);
1896 bt_dev_err(hdev
, "CSR: Local version failed (%d)", err
);
1900 if (skb
->len
!= sizeof(struct hci_rp_read_local_version
)) {
1901 bt_dev_err(hdev
, "CSR: Local version length mismatch");
1906 rp
= (struct hci_rp_read_local_version
*)skb
->data
;
1908 /* Detect a wide host of Chinese controllers that aren't CSR.
1910 * Known fake bcdDevices: 0x0100, 0x0134, 0x1915, 0x2520, 0x7558, 0x8891
1912 * The main thing they have in common is that these are really popular low-cost
1913 * options that support newer Bluetooth versions but rely on heavy VID/PID
1914 * squatting of this poor old Bluetooth 1.1 device. Even sold as such.
1916 * We detect actual CSR devices by checking that the HCI manufacturer code
1917 * is Cambridge Silicon Radio (10) and ensuring that LMP sub-version and
1918 * HCI rev values always match. As they both store the firmware number.
1920 if (le16_to_cpu(rp
->manufacturer
) != 10 ||
1921 le16_to_cpu(rp
->hci_rev
) != le16_to_cpu(rp
->lmp_subver
))
1924 /* Known legit CSR firmware build numbers and their supported BT versions:
1925 * - 1.1 (0x1) -> 0x0073, 0x020d, 0x033c, 0x034e
1926 * - 1.2 (0x2) -> 0x04d9, 0x0529
1927 * - 2.0 (0x3) -> 0x07a6, 0x07ad, 0x0c5c
1928 * - 2.1 (0x4) -> 0x149c, 0x1735, 0x1899 (0x1899 is a BlueCore4-External)
1929 * - 4.0 (0x6) -> 0x1d86, 0x2031, 0x22bb
1931 * e.g. Real CSR dongles with LMP subversion 0x73 are old enough that
1932 * support BT 1.1 only; so it's a dead giveaway when some
1933 * third-party BT 4.0 dongle reuses it.
1935 else if (le16_to_cpu(rp
->lmp_subver
) <= 0x034e &&
1936 le16_to_cpu(rp
->hci_ver
) > BLUETOOTH_VER_1_1
)
1939 else if (le16_to_cpu(rp
->lmp_subver
) <= 0x0529 &&
1940 le16_to_cpu(rp
->hci_ver
) > BLUETOOTH_VER_1_2
)
1943 else if (le16_to_cpu(rp
->lmp_subver
) <= 0x0c5c &&
1944 le16_to_cpu(rp
->hci_ver
) > BLUETOOTH_VER_2_0
)
1947 else if (le16_to_cpu(rp
->lmp_subver
) <= 0x1899 &&
1948 le16_to_cpu(rp
->hci_ver
) > BLUETOOTH_VER_2_1
)
1951 else if (le16_to_cpu(rp
->lmp_subver
) <= 0x22bb &&
1952 le16_to_cpu(rp
->hci_ver
) > BLUETOOTH_VER_4_0
)
1955 /* Other clones which beat all the above checks */
1956 else if (bcdDevice
== 0x0134 &&
1957 le16_to_cpu(rp
->lmp_subver
) == 0x0c5c &&
1958 le16_to_cpu(rp
->hci_ver
) == BLUETOOTH_VER_2_0
)
1962 bt_dev_warn(hdev
, "CSR: Unbranded CSR clone detected; adding workarounds and force-suspending once...");
1964 /* Generally these clones have big discrepancies between
1965 * advertised features and what's actually supported.
1966 * Probably will need to be expanded in the future;
1967 * without these the controller will lock up.
1969 set_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY
, &hdev
->quirks
);
1970 set_bit(HCI_QUIRK_BROKEN_ERR_DATA_REPORTING
, &hdev
->quirks
);
1972 /* Clear the reset quirk since this is not an actual
1973 * early Bluetooth 1.1 device from CSR.
1975 clear_bit(HCI_QUIRK_RESET_ON_CLOSE
, &hdev
->quirks
);
1976 clear_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY
, &hdev
->quirks
);
1979 * Special workaround for these BT 4.0 chip clones, and potentially more:
1981 * - 0x0134: a Barrot 8041a02 (HCI rev: 0x1012 sub: 0x0810)
1982 * - 0x7558: IC markings FR3191AHAL 749H15143 (HCI rev/sub-version: 0x0709)
1984 * These controllers are really messed-up.
1986 * 1. Their bulk RX endpoint will never report any data unless
1987 * the device was suspended at least once (yes, really).
1988 * 2. They will not wakeup when autosuspended and receiving data
1989 * on their bulk RX endpoint from e.g. a keyboard or mouse
1990 * (IOW remote-wakeup support is broken for the bulk endpoint).
1992 * To fix 1. enable runtime-suspend, force-suspend the
1993 * HCI and then wake-it up by disabling runtime-suspend.
1995 * To fix 2. clear the HCI's can_wake flag, this way the HCI
1996 * will still be autosuspended when it is not open.
2000 * Because these are widespread problems we prefer generic solutions; so
2001 * apply this initialization quirk to every controller that gets here,
2002 * it should be harmless. The alternative is to not work at all.
2004 pm_runtime_allow(&data
->udev
->dev
);
2006 ret
= pm_runtime_suspend(&data
->udev
->dev
);
2010 bt_dev_err(hdev
, "CSR: Failed to suspend the device for our Barrot 8041a02 receive-issue workaround");
2012 pm_runtime_forbid(&data
->udev
->dev
);
2014 device_set_wakeup_capable(&data
->udev
->dev
, false);
2016 /* Re-enable autosuspend if this was requested */
2017 if (enable_autosuspend
)
2018 usb_enable_autosuspend(data
->udev
);
2026 static int inject_cmd_complete(struct hci_dev
*hdev
, __u16 opcode
)
2028 struct sk_buff
*skb
;
2029 struct hci_event_hdr
*hdr
;
2030 struct hci_ev_cmd_complete
*evt
;
2032 skb
= bt_skb_alloc(sizeof(*hdr
) + sizeof(*evt
) + 1, GFP_KERNEL
);
2036 hdr
= skb_put(skb
, sizeof(*hdr
));
2037 hdr
->evt
= HCI_EV_CMD_COMPLETE
;
2038 hdr
->plen
= sizeof(*evt
) + 1;
2040 evt
= skb_put(skb
, sizeof(*evt
));
2042 evt
->opcode
= cpu_to_le16(opcode
);
2044 skb_put_u8(skb
, 0x00);
2046 hci_skb_pkt_type(skb
) = HCI_EVENT_PKT
;
2048 return hci_recv_frame(hdev
, skb
);
2051 static int btusb_recv_bulk_intel(struct btusb_data
*data
, void *buffer
,
2054 struct hci_dev
*hdev
= data
->hdev
;
2056 /* When the device is in bootloader mode, then it can send
2057 * events via the bulk endpoint. These events are treated the
2058 * same way as the ones received from the interrupt endpoint.
2060 if (btintel_test_flag(hdev
, INTEL_BOOTLOADER
))
2061 return btusb_recv_intr(data
, buffer
, count
);
2063 return btusb_recv_bulk(data
, buffer
, count
);
2066 static int btusb_recv_event_intel(struct hci_dev
*hdev
, struct sk_buff
*skb
)
2068 if (btintel_test_flag(hdev
, INTEL_BOOTLOADER
)) {
2069 struct hci_event_hdr
*hdr
= (void *)skb
->data
;
2071 if (skb
->len
> HCI_EVENT_HDR_SIZE
&& hdr
->evt
== 0xff &&
2073 const void *ptr
= skb
->data
+ HCI_EVENT_HDR_SIZE
+ 1;
2074 unsigned int len
= skb
->len
- HCI_EVENT_HDR_SIZE
- 1;
2076 switch (skb
->data
[2]) {
2078 /* When switching to the operational firmware
2079 * the device sends a vendor specific event
2080 * indicating that the bootup completed.
2082 btintel_bootup(hdev
, ptr
, len
);
2085 /* When the firmware loading completes the
2086 * device sends out a vendor specific event
2087 * indicating the result of the firmware
2090 btintel_secure_send_result(hdev
, ptr
, len
);
2096 return hci_recv_frame(hdev
, skb
);
2099 static int btusb_send_frame_intel(struct hci_dev
*hdev
, struct sk_buff
*skb
)
2103 BT_DBG("%s", hdev
->name
);
2105 switch (hci_skb_pkt_type(skb
)) {
2106 case HCI_COMMAND_PKT
:
2107 if (btintel_test_flag(hdev
, INTEL_BOOTLOADER
)) {
2108 struct hci_command_hdr
*cmd
= (void *)skb
->data
;
2109 __u16 opcode
= le16_to_cpu(cmd
->opcode
);
2111 /* When in bootloader mode and the command 0xfc09
2112 * is received, it needs to be send down the
2113 * bulk endpoint. So allocate a bulk URB instead.
2115 if (opcode
== 0xfc09)
2116 urb
= alloc_bulk_urb(hdev
, skb
);
2118 urb
= alloc_ctrl_urb(hdev
, skb
);
2120 /* When the 0xfc01 command is issued to boot into
2121 * the operational firmware, it will actually not
2122 * send a command complete event. To keep the flow
2123 * control working inject that event here.
2125 if (opcode
== 0xfc01)
2126 inject_cmd_complete(hdev
, opcode
);
2128 urb
= alloc_ctrl_urb(hdev
, skb
);
2131 return PTR_ERR(urb
);
2133 hdev
->stat
.cmd_tx
++;
2134 return submit_or_queue_tx_urb(hdev
, urb
);
2136 case HCI_ACLDATA_PKT
:
2137 urb
= alloc_bulk_urb(hdev
, skb
);
2139 return PTR_ERR(urb
);
2141 hdev
->stat
.acl_tx
++;
2142 return submit_or_queue_tx_urb(hdev
, urb
);
2144 case HCI_SCODATA_PKT
:
2145 if (hci_conn_num(hdev
, SCO_LINK
) < 1)
2148 urb
= alloc_isoc_urb(hdev
, skb
);
2150 return PTR_ERR(urb
);
2152 hdev
->stat
.sco_tx
++;
2153 return submit_tx_urb(hdev
, urb
);
2159 /* UHW CR mapping */
2160 #define MTK_BT_MISC 0x70002510
2161 #define MTK_BT_SUBSYS_RST 0x70002610
2162 #define MTK_UDMA_INT_STA_BT 0x74000024
2163 #define MTK_UDMA_INT_STA_BT1 0x74000308
2164 #define MTK_BT_WDT_STATUS 0x740003A0
2165 #define MTK_EP_RST_OPT 0x74011890
2166 #define MTK_EP_RST_IN_OUT_OPT 0x00010001
2167 #define MTK_BT_RST_DONE 0x00000100
2168 #define MTK_BT_RESET_WAIT_MS 100
2169 #define MTK_BT_RESET_NUM_TRIES 10
2170 #define FIRMWARE_MT7663 "mediatek/mt7663pr2h.bin"
2171 #define FIRMWARE_MT7668 "mediatek/mt7668pr2h.bin"
2173 #define HCI_WMT_MAX_EVENT_SIZE 64
2174 /* It is for mt79xx download rom patch*/
2175 #define MTK_FW_ROM_PATCH_HEADER_SIZE 32
2176 #define MTK_FW_ROM_PATCH_GD_SIZE 64
2177 #define MTK_FW_ROM_PATCH_SEC_MAP_SIZE 64
2178 #define MTK_SEC_MAP_COMMON_SIZE 12
2179 #define MTK_SEC_MAP_NEED_SEND_SIZE 52
2182 BTMTK_WMT_PATCH_DWNLD
= 0x1,
2183 BTMTK_WMT_FUNC_CTRL
= 0x6,
2184 BTMTK_WMT_RST
= 0x7,
2185 BTMTK_WMT_SEMAPHORE
= 0x17,
2190 BTMTK_WMT_PATCH_UNDONE
,
2191 BTMTK_WMT_PATCH_PROGRESS
,
2192 BTMTK_WMT_PATCH_DONE
,
2193 BTMTK_WMT_ON_UNDONE
,
2195 BTMTK_WMT_ON_PROGRESS
,
2198 struct btmtk_wmt_hdr
{
2205 struct btmtk_hci_wmt_cmd
{
2206 struct btmtk_wmt_hdr hdr
;
2210 struct btmtk_hci_wmt_evt
{
2211 struct hci_event_hdr hhdr
;
2212 struct btmtk_wmt_hdr whdr
;
2215 struct btmtk_hci_wmt_evt_funcc
{
2216 struct btmtk_hci_wmt_evt hwhdr
;
2220 struct btmtk_tci_sleep
{
2223 __le16 host_duration
;
2225 u8 time_compensation
;
2228 struct btmtk_hci_wmt_params
{
2236 struct btmtk_patch_header
{
2244 struct btmtk_global_desc
{
2251 struct btmtk_section_map
{
2256 __le32 u4SecSpec
[13];
2263 __le32 dlmodecrctype
;
2270 static void btusb_mtk_wmt_recv(struct urb
*urb
)
2272 struct hci_dev
*hdev
= urb
->context
;
2273 struct btusb_data
*data
= hci_get_drvdata(hdev
);
2274 struct hci_event_hdr
*hdr
;
2275 struct sk_buff
*skb
;
2278 if (urb
->status
== 0 && urb
->actual_length
> 0) {
2279 hdev
->stat
.byte_rx
+= urb
->actual_length
;
2281 /* WMT event shouldn't be fragmented and the size should be
2282 * less than HCI_WMT_MAX_EVENT_SIZE.
2284 skb
= bt_skb_alloc(HCI_WMT_MAX_EVENT_SIZE
, GFP_ATOMIC
);
2286 hdev
->stat
.err_rx
++;
2287 kfree(urb
->setup_packet
);
2291 hci_skb_pkt_type(skb
) = HCI_EVENT_PKT
;
2292 skb_put_data(skb
, urb
->transfer_buffer
, urb
->actual_length
);
2294 hdr
= (void *)skb
->data
;
2295 /* Fix up the vendor event id with 0xff for vendor specific
2296 * instead of 0xe4 so that event send via monitoring socket can
2297 * be parsed properly.
2301 /* When someone waits for the WMT event, the skb is being cloned
2302 * and being processed the events from there then.
2304 if (test_bit(BTUSB_TX_WAIT_VND_EVT
, &data
->flags
)) {
2305 data
->evt_skb
= skb_clone(skb
, GFP_ATOMIC
);
2306 if (!data
->evt_skb
) {
2308 kfree(urb
->setup_packet
);
2313 err
= hci_recv_frame(hdev
, skb
);
2315 kfree_skb(data
->evt_skb
);
2316 data
->evt_skb
= NULL
;
2317 kfree(urb
->setup_packet
);
2321 if (test_and_clear_bit(BTUSB_TX_WAIT_VND_EVT
,
2323 /* Barrier to sync with other CPUs */
2324 smp_mb__after_atomic();
2325 wake_up_bit(&data
->flags
,
2326 BTUSB_TX_WAIT_VND_EVT
);
2328 kfree(urb
->setup_packet
);
2330 } else if (urb
->status
== -ENOENT
) {
2331 /* Avoid suspend failed when usb_kill_urb */
2335 usb_mark_last_busy(data
->udev
);
2337 /* The URB complete handler is still called with urb->actual_length = 0
2338 * when the event is not available, so we should keep re-submitting
2339 * URB until WMT event returns, Also, It's necessary to wait some time
2340 * between the two consecutive control URBs to relax the target device
2341 * to generate the event. Otherwise, the WMT event cannot return from
2342 * the device successfully.
2346 usb_anchor_urb(urb
, &data
->ctrl_anchor
);
2347 err
= usb_submit_urb(urb
, GFP_ATOMIC
);
2349 kfree(urb
->setup_packet
);
2350 /* -EPERM: urb is being killed;
2351 * -ENODEV: device got disconnected
2353 if (err
!= -EPERM
&& err
!= -ENODEV
)
2354 bt_dev_err(hdev
, "urb %p failed to resubmit (%d)",
2356 usb_unanchor_urb(urb
);
2360 static int btusb_mtk_submit_wmt_recv_urb(struct hci_dev
*hdev
)
2362 struct btusb_data
*data
= hci_get_drvdata(hdev
);
2363 struct usb_ctrlrequest
*dr
;
2369 urb
= usb_alloc_urb(0, GFP_KERNEL
);
2373 dr
= kmalloc(sizeof(*dr
), GFP_KERNEL
);
2379 dr
->bRequestType
= USB_TYPE_VENDOR
| USB_DIR_IN
;
2381 dr
->wIndex
= cpu_to_le16(0);
2382 dr
->wValue
= cpu_to_le16(48);
2383 dr
->wLength
= cpu_to_le16(size
);
2385 buf
= kmalloc(size
, GFP_KERNEL
);
2392 pipe
= usb_rcvctrlpipe(data
->udev
, 0);
2394 usb_fill_control_urb(urb
, data
->udev
, pipe
, (void *)dr
,
2395 buf
, size
, btusb_mtk_wmt_recv
, hdev
);
2397 urb
->transfer_flags
|= URB_FREE_BUFFER
;
2399 usb_anchor_urb(urb
, &data
->ctrl_anchor
);
2400 err
= usb_submit_urb(urb
, GFP_KERNEL
);
2402 if (err
!= -EPERM
&& err
!= -ENODEV
)
2403 bt_dev_err(hdev
, "urb %p submission failed (%d)",
2405 usb_unanchor_urb(urb
);
2413 static int btusb_mtk_hci_wmt_sync(struct hci_dev
*hdev
,
2414 struct btmtk_hci_wmt_params
*wmt_params
)
2416 struct btusb_data
*data
= hci_get_drvdata(hdev
);
2417 struct btmtk_hci_wmt_evt_funcc
*wmt_evt_funcc
;
2418 u32 hlen
, status
= BTMTK_WMT_INVALID
;
2419 struct btmtk_hci_wmt_evt
*wmt_evt
;
2420 struct btmtk_hci_wmt_cmd
*wc
;
2421 struct btmtk_wmt_hdr
*hdr
;
2424 /* Send the WMT command and wait until the WMT event returns */
2425 hlen
= sizeof(*hdr
) + wmt_params
->dlen
;
2429 wc
= kzalloc(hlen
, GFP_KERNEL
);
2435 hdr
->op
= wmt_params
->op
;
2436 hdr
->dlen
= cpu_to_le16(wmt_params
->dlen
+ 1);
2437 hdr
->flag
= wmt_params
->flag
;
2438 memcpy(wc
->data
, wmt_params
->data
, wmt_params
->dlen
);
2440 set_bit(BTUSB_TX_WAIT_VND_EVT
, &data
->flags
);
2442 err
= __hci_cmd_send(hdev
, 0xfc6f, hlen
, wc
);
2445 clear_bit(BTUSB_TX_WAIT_VND_EVT
, &data
->flags
);
2449 /* Submit control IN URB on demand to process the WMT event */
2450 err
= btusb_mtk_submit_wmt_recv_urb(hdev
);
2454 /* The vendor specific WMT commands are all answered by a vendor
2455 * specific event and will have the Command Status or Command
2456 * Complete as with usual HCI command flow control.
2458 * After sending the command, wait for BTUSB_TX_WAIT_VND_EVT
2459 * state to be cleared. The driver specific event receive routine
2460 * will clear that state and with that indicate completion of the
2463 err
= wait_on_bit_timeout(&data
->flags
, BTUSB_TX_WAIT_VND_EVT
,
2464 TASK_INTERRUPTIBLE
, HCI_INIT_TIMEOUT
);
2465 if (err
== -EINTR
) {
2466 bt_dev_err(hdev
, "Execution of wmt command interrupted");
2467 clear_bit(BTUSB_TX_WAIT_VND_EVT
, &data
->flags
);
2472 bt_dev_err(hdev
, "Execution of wmt command timed out");
2473 clear_bit(BTUSB_TX_WAIT_VND_EVT
, &data
->flags
);
2478 /* Parse and handle the return WMT event */
2479 wmt_evt
= (struct btmtk_hci_wmt_evt
*)data
->evt_skb
->data
;
2480 if (wmt_evt
->whdr
.op
!= hdr
->op
) {
2481 bt_dev_err(hdev
, "Wrong op received %d expected %d",
2482 wmt_evt
->whdr
.op
, hdr
->op
);
2487 switch (wmt_evt
->whdr
.op
) {
2488 case BTMTK_WMT_SEMAPHORE
:
2489 if (wmt_evt
->whdr
.flag
== 2)
2490 status
= BTMTK_WMT_PATCH_UNDONE
;
2492 status
= BTMTK_WMT_PATCH_DONE
;
2494 case BTMTK_WMT_FUNC_CTRL
:
2495 wmt_evt_funcc
= (struct btmtk_hci_wmt_evt_funcc
*)wmt_evt
;
2496 if (be16_to_cpu(wmt_evt_funcc
->status
) == 0x404)
2497 status
= BTMTK_WMT_ON_DONE
;
2498 else if (be16_to_cpu(wmt_evt_funcc
->status
) == 0x420)
2499 status
= BTMTK_WMT_ON_PROGRESS
;
2501 status
= BTMTK_WMT_ON_UNDONE
;
2503 case BTMTK_WMT_PATCH_DWNLD
:
2504 if (wmt_evt
->whdr
.flag
== 2)
2505 status
= BTMTK_WMT_PATCH_DONE
;
2506 else if (wmt_evt
->whdr
.flag
== 1)
2507 status
= BTMTK_WMT_PATCH_PROGRESS
;
2509 status
= BTMTK_WMT_PATCH_UNDONE
;
2513 if (wmt_params
->status
)
2514 *wmt_params
->status
= status
;
2517 kfree_skb(data
->evt_skb
);
2518 data
->evt_skb
= NULL
;
2524 static int btusb_mtk_setup_firmware_79xx(struct hci_dev
*hdev
, const char *fwname
)
2526 struct btmtk_hci_wmt_params wmt_params
;
2527 struct btmtk_global_desc
*globaldesc
= NULL
;
2528 struct btmtk_section_map
*sectionmap
;
2529 const struct firmware
*fw
;
2531 const u8
*fw_bin_ptr
;
2532 int err
, dlen
, i
, status
;
2533 u8 flag
, first_block
, retry
;
2534 u32 section_num
, dl_size
, section_offset
;
2537 err
= request_firmware(&fw
, fwname
, &hdev
->dev
);
2539 bt_dev_err(hdev
, "Failed to load firmware file (%d)", err
);
2544 fw_bin_ptr
= fw_ptr
;
2545 globaldesc
= (struct btmtk_global_desc
*)(fw_ptr
+ MTK_FW_ROM_PATCH_HEADER_SIZE
);
2546 section_num
= le32_to_cpu(globaldesc
->section_num
);
2548 for (i
= 0; i
< section_num
; i
++) {
2550 fw_ptr
= fw_bin_ptr
;
2551 sectionmap
= (struct btmtk_section_map
*)(fw_ptr
+ MTK_FW_ROM_PATCH_HEADER_SIZE
+
2552 MTK_FW_ROM_PATCH_GD_SIZE
+ MTK_FW_ROM_PATCH_SEC_MAP_SIZE
* i
);
2554 section_offset
= le32_to_cpu(sectionmap
->secoffset
);
2555 dl_size
= le32_to_cpu(sectionmap
->bin_info_spec
.dlsize
);
2560 cmd
[0] = 0; /* 0 means legacy dl mode. */
2562 fw_ptr
+ MTK_FW_ROM_PATCH_HEADER_SIZE
+
2563 MTK_FW_ROM_PATCH_GD_SIZE
+ MTK_FW_ROM_PATCH_SEC_MAP_SIZE
* i
+
2564 MTK_SEC_MAP_COMMON_SIZE
,
2565 MTK_SEC_MAP_NEED_SEND_SIZE
+ 1);
2567 wmt_params
.op
= BTMTK_WMT_PATCH_DWNLD
;
2568 wmt_params
.status
= &status
;
2569 wmt_params
.flag
= 0;
2570 wmt_params
.dlen
= MTK_SEC_MAP_NEED_SEND_SIZE
+ 1;
2571 wmt_params
.data
= &cmd
;
2573 err
= btusb_mtk_hci_wmt_sync(hdev
, &wmt_params
);
2575 bt_dev_err(hdev
, "Failed to send wmt patch dwnld (%d)",
2577 goto err_release_fw
;
2580 if (status
== BTMTK_WMT_PATCH_UNDONE
) {
2582 } else if (status
== BTMTK_WMT_PATCH_PROGRESS
) {
2585 } else if (status
== BTMTK_WMT_PATCH_DONE
) {
2588 bt_dev_err(hdev
, "Failed wmt patch dwnld status (%d)",
2591 goto err_release_fw
;
2595 fw_ptr
+= section_offset
;
2596 wmt_params
.op
= BTMTK_WMT_PATCH_DWNLD
;
2597 wmt_params
.status
= NULL
;
2599 while (dl_size
> 0) {
2600 dlen
= min_t(int, 250, dl_size
);
2601 if (first_block
== 1) {
2604 } else if (dl_size
- dlen
<= 0) {
2610 wmt_params
.flag
= flag
;
2611 wmt_params
.dlen
= dlen
;
2612 wmt_params
.data
= fw_ptr
;
2614 err
= btusb_mtk_hci_wmt_sync(hdev
, &wmt_params
);
2616 bt_dev_err(hdev
, "Failed to send wmt patch dwnld (%d)",
2618 goto err_release_fw
;
2628 /* Wait a few moments for firmware activation done */
2629 usleep_range(100000, 120000);
2632 release_firmware(fw
);
2637 static int btusb_mtk_setup_firmware(struct hci_dev
*hdev
, const char *fwname
)
2639 struct btmtk_hci_wmt_params wmt_params
;
2640 const struct firmware
*fw
;
2646 err
= request_firmware(&fw
, fwname
, &hdev
->dev
);
2648 bt_dev_err(hdev
, "Failed to load firmware file (%d)", err
);
2652 /* Power on data RAM the firmware relies on. */
2654 wmt_params
.op
= BTMTK_WMT_FUNC_CTRL
;
2655 wmt_params
.flag
= 3;
2656 wmt_params
.dlen
= sizeof(param
);
2657 wmt_params
.data
= ¶m
;
2658 wmt_params
.status
= NULL
;
2660 err
= btusb_mtk_hci_wmt_sync(hdev
, &wmt_params
);
2662 bt_dev_err(hdev
, "Failed to power on data RAM (%d)", err
);
2663 goto err_release_fw
;
2669 /* The size of patch header is 30 bytes, should be skip */
2672 goto err_release_fw
;
2679 wmt_params
.op
= BTMTK_WMT_PATCH_DWNLD
;
2680 wmt_params
.status
= NULL
;
2682 while (fw_size
> 0) {
2683 dlen
= min_t(int, 250, fw_size
);
2685 /* Tell device the position in sequence */
2686 if (fw_size
- dlen
<= 0)
2688 else if (fw_size
< fw
->size
- 30)
2691 wmt_params
.flag
= flag
;
2692 wmt_params
.dlen
= dlen
;
2693 wmt_params
.data
= fw_ptr
;
2695 err
= btusb_mtk_hci_wmt_sync(hdev
, &wmt_params
);
2697 bt_dev_err(hdev
, "Failed to send wmt patch dwnld (%d)",
2699 goto err_release_fw
;
2706 wmt_params
.op
= BTMTK_WMT_RST
;
2707 wmt_params
.flag
= 4;
2708 wmt_params
.dlen
= 0;
2709 wmt_params
.data
= NULL
;
2710 wmt_params
.status
= NULL
;
2712 /* Activate funciton the firmware providing to */
2713 err
= btusb_mtk_hci_wmt_sync(hdev
, &wmt_params
);
2715 bt_dev_err(hdev
, "Failed to send wmt rst (%d)", err
);
2716 goto err_release_fw
;
2719 /* Wait a few moments for firmware activation done */
2720 usleep_range(10000, 12000);
2723 release_firmware(fw
);
2728 static int btusb_mtk_func_query(struct hci_dev
*hdev
)
2730 struct btmtk_hci_wmt_params wmt_params
;
2734 /* Query whether the function is enabled */
2735 wmt_params
.op
= BTMTK_WMT_FUNC_CTRL
;
2736 wmt_params
.flag
= 4;
2737 wmt_params
.dlen
= sizeof(param
);
2738 wmt_params
.data
= ¶m
;
2739 wmt_params
.status
= &status
;
2741 err
= btusb_mtk_hci_wmt_sync(hdev
, &wmt_params
);
2743 bt_dev_err(hdev
, "Failed to query function status (%d)", err
);
2750 static int btusb_mtk_uhw_reg_write(struct btusb_data
*data
, u32 reg
, u32 val
)
2752 struct hci_dev
*hdev
= data
->hdev
;
2756 buf
= kzalloc(4, GFP_KERNEL
);
2760 put_unaligned_le32(val
, buf
);
2762 pipe
= usb_sndctrlpipe(data
->udev
, 0);
2763 err
= usb_control_msg(data
->udev
, pipe
, 0x02,
2765 reg
>> 16, reg
& 0xffff,
2766 buf
, 4, USB_CTRL_SET_TIMEOUT
);
2768 bt_dev_err(hdev
, "Failed to write uhw reg(%d)", err
);
2778 static int btusb_mtk_uhw_reg_read(struct btusb_data
*data
, u32 reg
, u32
*val
)
2780 struct hci_dev
*hdev
= data
->hdev
;
2784 buf
= kzalloc(4, GFP_KERNEL
);
2788 pipe
= usb_rcvctrlpipe(data
->udev
, 0);
2789 err
= usb_control_msg(data
->udev
, pipe
, 0x01,
2791 reg
>> 16, reg
& 0xffff,
2792 buf
, 4, USB_CTRL_SET_TIMEOUT
);
2794 bt_dev_err(hdev
, "Failed to read uhw reg(%d)", err
);
2798 *val
= get_unaligned_le32(buf
);
2799 bt_dev_dbg(hdev
, "reg=%x, value=0x%08x", reg
, *val
);
2807 static int btusb_mtk_reg_read(struct btusb_data
*data
, u32 reg
, u32
*val
)
2809 int pipe
, err
, size
= sizeof(u32
);
2812 buf
= kzalloc(size
, GFP_KERNEL
);
2816 pipe
= usb_rcvctrlpipe(data
->udev
, 0);
2817 err
= usb_control_msg(data
->udev
, pipe
, 0x63,
2818 USB_TYPE_VENDOR
| USB_DIR_IN
,
2819 reg
>> 16, reg
& 0xffff,
2820 buf
, size
, USB_CTRL_SET_TIMEOUT
);
2824 *val
= get_unaligned_le32(buf
);
2832 static int btusb_mtk_id_get(struct btusb_data
*data
, u32 reg
, u32
*id
)
2834 return btusb_mtk_reg_read(data
, reg
, id
);
2837 static int btusb_mtk_setup(struct hci_dev
*hdev
)
2839 struct btusb_data
*data
= hci_get_drvdata(hdev
);
2840 struct btmtk_hci_wmt_params wmt_params
;
2841 ktime_t calltime
, delta
, rettime
;
2842 struct btmtk_tci_sleep tci_sleep
;
2843 unsigned long long duration
;
2844 struct sk_buff
*skb
;
2848 char fw_bin_name
[64];
2852 calltime
= ktime_get();
2854 err
= btusb_mtk_id_get(data
, 0x80000008, &dev_id
);
2856 bt_dev_err(hdev
, "Failed to get device id (%d)", err
);
2861 err
= btusb_mtk_id_get(data
, 0x70010200, &dev_id
);
2863 bt_dev_err(hdev
, "Failed to get device id (%d)", err
);
2866 err
= btusb_mtk_id_get(data
, 0x80021004, &fw_version
);
2868 bt_dev_err(hdev
, "Failed to get fw version (%d)", err
);
2875 fwname
= FIRMWARE_MT7663
;
2878 fwname
= FIRMWARE_MT7668
;
2882 snprintf(fw_bin_name
, sizeof(fw_bin_name
),
2883 "mediatek/BT_RAM_CODE_MT%04x_1_%x_hdr.bin",
2884 dev_id
& 0xffff, (fw_version
& 0xff) + 1);
2885 err
= btusb_mtk_setup_firmware_79xx(hdev
, fw_bin_name
);
2887 bt_dev_err(hdev
, "Failed to set up firmware (%d)", err
);
2891 /* It's Device EndPoint Reset Option Register */
2892 btusb_mtk_uhw_reg_write(data
, MTK_EP_RST_OPT
, MTK_EP_RST_IN_OUT_OPT
);
2894 /* Enable Bluetooth protocol */
2896 wmt_params
.op
= BTMTK_WMT_FUNC_CTRL
;
2897 wmt_params
.flag
= 0;
2898 wmt_params
.dlen
= sizeof(param
);
2899 wmt_params
.data
= ¶m
;
2900 wmt_params
.status
= NULL
;
2902 err
= btusb_mtk_hci_wmt_sync(hdev
, &wmt_params
);
2904 bt_dev_err(hdev
, "Failed to send wmt func ctrl (%d)", err
);
2908 hci_set_msft_opcode(hdev
, 0xFD30);
2909 hci_set_aosp_capable(hdev
);
2912 bt_dev_err(hdev
, "Unsupported hardware variant (%08x)",
2917 /* Query whether the firmware is already download */
2918 wmt_params
.op
= BTMTK_WMT_SEMAPHORE
;
2919 wmt_params
.flag
= 1;
2920 wmt_params
.dlen
= 0;
2921 wmt_params
.data
= NULL
;
2922 wmt_params
.status
= &status
;
2924 err
= btusb_mtk_hci_wmt_sync(hdev
, &wmt_params
);
2926 bt_dev_err(hdev
, "Failed to query firmware status (%d)", err
);
2930 if (status
== BTMTK_WMT_PATCH_DONE
) {
2931 bt_dev_info(hdev
, "firmware already downloaded");
2932 goto ignore_setup_fw
;
2935 /* Setup a firmware which the device definitely requires */
2936 err
= btusb_mtk_setup_firmware(hdev
, fwname
);
2941 err
= readx_poll_timeout(btusb_mtk_func_query
, hdev
, status
,
2942 status
< 0 || status
!= BTMTK_WMT_ON_PROGRESS
,
2944 /* -ETIMEDOUT happens */
2948 /* The other errors happen in btusb_mtk_func_query */
2952 if (status
== BTMTK_WMT_ON_DONE
) {
2953 bt_dev_info(hdev
, "function already on");
2954 goto ignore_func_on
;
2957 /* Enable Bluetooth protocol */
2959 wmt_params
.op
= BTMTK_WMT_FUNC_CTRL
;
2960 wmt_params
.flag
= 0;
2961 wmt_params
.dlen
= sizeof(param
);
2962 wmt_params
.data
= ¶m
;
2963 wmt_params
.status
= NULL
;
2965 err
= btusb_mtk_hci_wmt_sync(hdev
, &wmt_params
);
2967 bt_dev_err(hdev
, "Failed to send wmt func ctrl (%d)", err
);
2972 /* Apply the low power environment setup */
2973 tci_sleep
.mode
= 0x5;
2974 tci_sleep
.duration
= cpu_to_le16(0x640);
2975 tci_sleep
.host_duration
= cpu_to_le16(0x640);
2976 tci_sleep
.host_wakeup_pin
= 0;
2977 tci_sleep
.time_compensation
= 0;
2979 skb
= __hci_cmd_sync(hdev
, 0xfc7a, sizeof(tci_sleep
), &tci_sleep
,
2983 bt_dev_err(hdev
, "Failed to apply low power setting (%d)", err
);
2989 rettime
= ktime_get();
2990 delta
= ktime_sub(rettime
, calltime
);
2991 duration
= (unsigned long long)ktime_to_ns(delta
) >> 10;
2993 bt_dev_info(hdev
, "Device setup in %llu usecs", duration
);
2998 static int btusb_mtk_shutdown(struct hci_dev
*hdev
)
3000 struct btmtk_hci_wmt_params wmt_params
;
3004 /* Disable the device */
3005 wmt_params
.op
= BTMTK_WMT_FUNC_CTRL
;
3006 wmt_params
.flag
= 0;
3007 wmt_params
.dlen
= sizeof(param
);
3008 wmt_params
.data
= ¶m
;
3009 wmt_params
.status
= NULL
;
3011 err
= btusb_mtk_hci_wmt_sync(hdev
, &wmt_params
);
3013 bt_dev_err(hdev
, "Failed to send wmt func ctrl (%d)", err
);
3020 static void btusb_mtk_cmd_timeout(struct hci_dev
*hdev
)
3022 struct btusb_data
*data
= hci_get_drvdata(hdev
);
3026 /* It's MediaTek specific bluetooth reset mechanism via USB */
3027 if (test_and_set_bit(BTUSB_HW_RESET_ACTIVE
, &data
->flags
)) {
3028 bt_dev_err(hdev
, "last reset failed? Not resetting again");
3032 err
= usb_autopm_get_interface(data
->intf
);
3036 btusb_stop_traffic(data
);
3037 usb_kill_anchored_urbs(&data
->tx_anchor
);
3039 /* It's Device EndPoint Reset Option Register */
3040 bt_dev_dbg(hdev
, "Initiating reset mechanism via uhw");
3041 btusb_mtk_uhw_reg_write(data
, MTK_EP_RST_OPT
, MTK_EP_RST_IN_OUT_OPT
);
3042 btusb_mtk_uhw_reg_read(data
, MTK_BT_WDT_STATUS
, &val
);
3044 /* Reset the bluetooth chip via USB interface. */
3045 btusb_mtk_uhw_reg_write(data
, MTK_BT_SUBSYS_RST
, 1);
3046 btusb_mtk_uhw_reg_write(data
, MTK_UDMA_INT_STA_BT
, 0x000000FF);
3047 btusb_mtk_uhw_reg_read(data
, MTK_UDMA_INT_STA_BT
, &val
);
3048 btusb_mtk_uhw_reg_write(data
, MTK_UDMA_INT_STA_BT1
, 0x000000FF);
3049 btusb_mtk_uhw_reg_read(data
, MTK_UDMA_INT_STA_BT1
, &val
);
3050 /* MT7921 need to delay 20ms between toggle reset bit */
3052 btusb_mtk_uhw_reg_write(data
, MTK_BT_SUBSYS_RST
, 0);
3053 btusb_mtk_uhw_reg_read(data
, MTK_BT_SUBSYS_RST
, &val
);
3055 /* Poll the register until reset is completed */
3057 btusb_mtk_uhw_reg_read(data
, MTK_BT_MISC
, &val
);
3058 if (val
& MTK_BT_RST_DONE
) {
3059 bt_dev_dbg(hdev
, "Bluetooth Reset Successfully");
3063 bt_dev_dbg(hdev
, "Polling Bluetooth Reset CR");
3065 msleep(MTK_BT_RESET_WAIT_MS
);
3066 } while (retry
< MTK_BT_RESET_NUM_TRIES
);
3068 btusb_mtk_id_get(data
, 0x70010200, &val
);
3070 bt_dev_err(hdev
, "Can't get device id, subsys reset fail.");
3072 usb_queue_reset_device(data
->intf
);
3074 clear_bit(BTUSB_HW_RESET_ACTIVE
, &data
->flags
);
3077 static int btusb_recv_acl_mtk(struct hci_dev
*hdev
, struct sk_buff
*skb
)
3079 struct btusb_data
*data
= hci_get_drvdata(hdev
);
3080 u16 handle
= le16_to_cpu(hci_acl_hdr(skb
)->handle
);
3083 case 0xfc6f: /* Firmware dump from device */
3084 /* When the firmware hangs, the device can no longer
3085 * suspend and thus disable auto-suspend.
3087 usb_disable_autosuspend(data
->udev
);
3089 case 0x05ff: /* Firmware debug logging 1 */
3090 case 0x05fe: /* Firmware debug logging 2 */
3091 return hci_recv_diag(hdev
, skb
);
3094 return hci_recv_frame(hdev
, skb
);
3097 MODULE_FIRMWARE(FIRMWARE_MT7663
);
3098 MODULE_FIRMWARE(FIRMWARE_MT7668
);
3101 /* Configure an out-of-band gpio as wake-up pin, if specified in device tree */
3102 static int marvell_config_oob_wake(struct hci_dev
*hdev
)
3104 struct sk_buff
*skb
;
3105 struct btusb_data
*data
= hci_get_drvdata(hdev
);
3106 struct device
*dev
= &data
->udev
->dev
;
3107 u16 pin
, gap
, opcode
;
3111 /* Move on if no wakeup pin specified */
3112 if (of_property_read_u16(dev
->of_node
, "marvell,wakeup-pin", &pin
) ||
3113 of_property_read_u16(dev
->of_node
, "marvell,wakeup-gap-ms", &gap
))
3116 /* Vendor specific command to configure a GPIO as wake-up pin */
3117 opcode
= hci_opcode_pack(0x3F, 0x59);
3118 cmd
[0] = opcode
& 0xFF;
3119 cmd
[1] = opcode
>> 8;
3120 cmd
[2] = 2; /* length of parameters that follow */
3122 cmd
[4] = gap
; /* time in ms, for which wakeup pin should be asserted */
3124 skb
= bt_skb_alloc(sizeof(cmd
), GFP_KERNEL
);
3126 bt_dev_err(hdev
, "%s: No memory", __func__
);
3130 skb_put_data(skb
, cmd
, sizeof(cmd
));
3131 hci_skb_pkt_type(skb
) = HCI_COMMAND_PKT
;
3133 ret
= btusb_send_frame(hdev
, skb
);
3135 bt_dev_err(hdev
, "%s: configuration failed", __func__
);
3144 static int btusb_set_bdaddr_marvell(struct hci_dev
*hdev
,
3145 const bdaddr_t
*bdaddr
)
3147 struct sk_buff
*skb
;
3152 buf
[1] = sizeof(bdaddr_t
);
3153 memcpy(buf
+ 2, bdaddr
, sizeof(bdaddr_t
));
3155 skb
= __hci_cmd_sync(hdev
, 0xfc22, sizeof(buf
), buf
, HCI_INIT_TIMEOUT
);
3158 bt_dev_err(hdev
, "changing Marvell device address failed (%ld)",
3167 #define BTUSB_EDGE_LED_COMMAND 0xfc77
3169 static void btusb_edge_set_led(struct hci_dev
*hdev
, bool state
)
3171 struct sk_buff
*skb
;
3172 u8 config_led
[] = { 0x09, 0x00, 0x01, 0x01 };
3175 config_led
[1] = 0x01;
3177 skb
= __hci_cmd_sync(hdev
, BTUSB_EDGE_LED_COMMAND
, sizeof(config_led
), config_led
, HCI_INIT_TIMEOUT
);
3179 BT_ERR("%s fail to set LED (%ld)", hdev
->name
, PTR_ERR(skb
));
3184 static int btusb_edge_post_init(struct hci_dev
*hdev
)
3186 btusb_edge_set_led(hdev
, true);
3190 static int btusb_edge_shutdown(struct hci_dev
*hdev
)
3192 btusb_edge_set_led(hdev
, false);
3196 static int btusb_set_bdaddr_ath3012(struct hci_dev
*hdev
,
3197 const bdaddr_t
*bdaddr
)
3199 struct sk_buff
*skb
;
3206 buf
[3] = sizeof(bdaddr_t
);
3207 memcpy(buf
+ 4, bdaddr
, sizeof(bdaddr_t
));
3209 skb
= __hci_cmd_sync(hdev
, 0xfc0b, sizeof(buf
), buf
, HCI_INIT_TIMEOUT
);
3212 bt_dev_err(hdev
, "Change address command failed (%ld)", ret
);
3220 static int btusb_set_bdaddr_wcn6855(struct hci_dev
*hdev
,
3221 const bdaddr_t
*bdaddr
)
3223 struct sk_buff
*skb
;
3227 memcpy(buf
, bdaddr
, sizeof(bdaddr_t
));
3229 skb
= __hci_cmd_sync_ev(hdev
, 0xfc14, sizeof(buf
), buf
,
3230 HCI_EV_CMD_COMPLETE
, HCI_INIT_TIMEOUT
);
3233 bt_dev_err(hdev
, "Change address command failed (%ld)", ret
);
3241 #define QCA_DFU_PACKET_LEN 4096
3243 #define QCA_GET_TARGET_VERSION 0x09
3244 #define QCA_CHECK_STATUS 0x05
3245 #define QCA_DFU_DOWNLOAD 0x01
3247 #define QCA_SYSCFG_UPDATED 0x40
3248 #define QCA_PATCH_UPDATED 0x80
3249 #define QCA_DFU_TIMEOUT 3000
3250 #define QCA_FLAG_MULTI_NVM 0x80
3251 #define QCA_BT_RESET_WAIT_MS 100
3253 #define WCN6855_2_0_RAM_VERSION_GF 0x400c1200
3254 #define WCN6855_2_1_RAM_VERSION_GF 0x400c1211
3256 struct qca_version
{
3258 __le32 patch_version
;
3266 struct qca_rampatch_version
{
3267 __le16 rom_version_high
;
3268 __le16 rom_version_low
;
3269 __le16 patch_version
;
3272 struct qca_device_info
{
3274 u8 rampatch_hdr
; /* length of header in rampatch */
3275 u8 nvm_hdr
; /* length of header in NVM */
3276 u8 ver_offset
; /* offset of version structure in rampatch */
3279 static const struct qca_device_info qca_devices_table
[] = {
3280 { 0x00000100, 20, 4, 8 }, /* Rome 1.0 */
3281 { 0x00000101, 20, 4, 8 }, /* Rome 1.1 */
3282 { 0x00000200, 28, 4, 16 }, /* Rome 2.0 */
3283 { 0x00000201, 28, 4, 16 }, /* Rome 2.1 */
3284 { 0x00000300, 28, 4, 16 }, /* Rome 3.0 */
3285 { 0x00000302, 28, 4, 16 }, /* Rome 3.2 */
3286 { 0x00130100, 40, 4, 16 }, /* WCN6855 1.0 */
3287 { 0x00130200, 40, 4, 16 }, /* WCN6855 2.0 */
3288 { 0x00130201, 40, 4, 16 }, /* WCN6855 2.1 */
3291 static int btusb_qca_send_vendor_req(struct usb_device
*udev
, u8 request
,
3292 void *data
, u16 size
)
3297 buf
= kmalloc(size
, GFP_KERNEL
);
3301 /* Found some of USB hosts have IOT issues with ours so that we should
3302 * not wait until HCI layer is ready.
3304 pipe
= usb_rcvctrlpipe(udev
, 0);
3305 err
= usb_control_msg(udev
, pipe
, request
, USB_TYPE_VENDOR
| USB_DIR_IN
,
3306 0, 0, buf
, size
, USB_CTRL_SET_TIMEOUT
);
3308 dev_err(&udev
->dev
, "Failed to access otp area (%d)", err
);
3312 memcpy(data
, buf
, size
);
3320 static int btusb_setup_qca_download_fw(struct hci_dev
*hdev
,
3321 const struct firmware
*firmware
,
3324 struct btusb_data
*btdata
= hci_get_drvdata(hdev
);
3325 struct usb_device
*udev
= btdata
->udev
;
3326 size_t count
, size
, sent
= 0;
3330 buf
= kmalloc(QCA_DFU_PACKET_LEN
, GFP_KERNEL
);
3334 count
= firmware
->size
;
3336 size
= min_t(size_t, count
, hdr_size
);
3337 memcpy(buf
, firmware
->data
, size
);
3339 /* USB patches should go down to controller through USB path
3340 * because binary format fits to go down through USB channel.
3341 * USB control path is for patching headers and USB bulk is for
3344 pipe
= usb_sndctrlpipe(udev
, 0);
3345 err
= usb_control_msg(udev
, pipe
, QCA_DFU_DOWNLOAD
, USB_TYPE_VENDOR
,
3346 0, 0, buf
, size
, USB_CTRL_SET_TIMEOUT
);
3348 bt_dev_err(hdev
, "Failed to send headers (%d)", err
);
3355 /* ep2 need time to switch from function acl to function dfu,
3356 * so we add 20ms delay here.
3361 size
= min_t(size_t, count
, QCA_DFU_PACKET_LEN
);
3363 memcpy(buf
, firmware
->data
+ sent
, size
);
3365 pipe
= usb_sndbulkpipe(udev
, 0x02);
3366 err
= usb_bulk_msg(udev
, pipe
, buf
, size
, &len
,
3369 bt_dev_err(hdev
, "Failed to send body at %zd of %zd (%d)",
3370 sent
, firmware
->size
, err
);
3375 bt_dev_err(hdev
, "Failed to get bulk buffer");
3389 static int btusb_setup_qca_load_rampatch(struct hci_dev
*hdev
,
3390 struct qca_version
*ver
,
3391 const struct qca_device_info
*info
)
3393 struct qca_rampatch_version
*rver
;
3394 const struct firmware
*fw
;
3395 u32 ver_rom
, ver_patch
, rver_rom
;
3396 u16 rver_rom_low
, rver_rom_high
, rver_patch
;
3400 ver_rom
= le32_to_cpu(ver
->rom_version
);
3401 ver_patch
= le32_to_cpu(ver
->patch_version
);
3403 snprintf(fwname
, sizeof(fwname
), "qca/rampatch_usb_%08x.bin", ver_rom
);
3405 err
= request_firmware(&fw
, fwname
, &hdev
->dev
);
3407 bt_dev_err(hdev
, "failed to request rampatch file: %s (%d)",
3412 bt_dev_info(hdev
, "using rampatch file: %s", fwname
);
3414 rver
= (struct qca_rampatch_version
*)(fw
->data
+ info
->ver_offset
);
3415 rver_rom_low
= le16_to_cpu(rver
->rom_version_low
);
3416 rver_patch
= le16_to_cpu(rver
->patch_version
);
3418 if (ver_rom
& ~0xffffU
) {
3419 rver_rom_high
= le16_to_cpu(rver
->rom_version_high
);
3420 rver_rom
= le32_to_cpu(rver_rom_high
<< 16 | rver_rom_low
);
3422 rver_rom
= rver_rom_low
;
3425 bt_dev_info(hdev
, "QCA: patch rome 0x%x build 0x%x, "
3426 "firmware rome 0x%x build 0x%x",
3427 rver_rom
, rver_patch
, ver_rom
, ver_patch
);
3429 if (rver_rom
!= ver_rom
|| rver_patch
<= ver_patch
) {
3430 bt_dev_err(hdev
, "rampatch file version did not match with firmware");
3435 err
= btusb_setup_qca_download_fw(hdev
, fw
, info
->rampatch_hdr
);
3438 release_firmware(fw
);
3443 static void btusb_generate_qca_nvm_name(char *fwname
, size_t max_size
,
3444 const struct qca_version
*ver
)
3446 u32 rom_version
= le32_to_cpu(ver
->rom_version
);
3447 u16 flag
= le16_to_cpu(ver
->flag
);
3449 if (((flag
>> 8) & 0xff) == QCA_FLAG_MULTI_NVM
) {
3450 /* The board_id should be split into two bytes
3451 * The 1st byte is chip ID, and the 2nd byte is platform ID
3452 * For example, board ID 0x010A, 0x01 is platform ID. 0x0A is chip ID
3453 * we have several platforms, and platform IDs are continuously added
3455 * 0x00 is for Mobile
3457 * 0x02 is for Automotive
3458 * 0x03 is for Consumer electronic
3460 u16 board_id
= (ver
->chip_id
<< 8) + ver
->platform_id
;
3461 const char *variant
;
3463 switch (le32_to_cpu(ver
->ram_version
)) {
3464 case WCN6855_2_0_RAM_VERSION_GF
:
3465 case WCN6855_2_1_RAM_VERSION_GF
:
3473 if (board_id
== 0) {
3474 snprintf(fwname
, max_size
, "qca/nvm_usb_%08x%s.bin",
3475 rom_version
, variant
);
3477 snprintf(fwname
, max_size
, "qca/nvm_usb_%08x%s_%04x.bin",
3478 rom_version
, variant
, board_id
);
3481 snprintf(fwname
, max_size
, "qca/nvm_usb_%08x.bin",
3487 static int btusb_setup_qca_load_nvm(struct hci_dev
*hdev
,
3488 struct qca_version
*ver
,
3489 const struct qca_device_info
*info
)
3491 const struct firmware
*fw
;
3495 btusb_generate_qca_nvm_name(fwname
, sizeof(fwname
), ver
);
3497 err
= request_firmware(&fw
, fwname
, &hdev
->dev
);
3499 bt_dev_err(hdev
, "failed to request NVM file: %s (%d)",
3504 bt_dev_info(hdev
, "using NVM file: %s", fwname
);
3506 err
= btusb_setup_qca_download_fw(hdev
, fw
, info
->nvm_hdr
);
3508 release_firmware(fw
);
3513 /* identify the ROM version and check whether patches are needed */
3514 static bool btusb_qca_need_patch(struct usb_device
*udev
)
3516 struct qca_version ver
;
3518 if (btusb_qca_send_vendor_req(udev
, QCA_GET_TARGET_VERSION
, &ver
,
3521 /* only low ROM versions need patches */
3522 return !(le32_to_cpu(ver
.rom_version
) & ~0xffffU
);
3525 static int btusb_setup_qca(struct hci_dev
*hdev
)
3527 struct btusb_data
*btdata
= hci_get_drvdata(hdev
);
3528 struct usb_device
*udev
= btdata
->udev
;
3529 const struct qca_device_info
*info
= NULL
;
3530 struct qca_version ver
;
3535 err
= btusb_qca_send_vendor_req(udev
, QCA_GET_TARGET_VERSION
, &ver
,
3540 ver_rom
= le32_to_cpu(ver
.rom_version
);
3542 for (i
= 0; i
< ARRAY_SIZE(qca_devices_table
); i
++) {
3543 if (ver_rom
== qca_devices_table
[i
].rom_version
)
3544 info
= &qca_devices_table
[i
];
3547 /* If the rom_version is not matched in the qca_devices_table
3548 * and the high ROM version is not zero, we assume this chip no
3549 * need to load the rampatch and nvm.
3551 if (ver_rom
& ~0xffffU
)
3554 bt_dev_err(hdev
, "don't support firmware rome 0x%x", ver_rom
);
3558 err
= btusb_qca_send_vendor_req(udev
, QCA_CHECK_STATUS
, &status
,
3563 if (!(status
& QCA_PATCH_UPDATED
)) {
3564 err
= btusb_setup_qca_load_rampatch(hdev
, &ver
, info
);
3569 err
= btusb_qca_send_vendor_req(udev
, QCA_GET_TARGET_VERSION
, &ver
,
3574 if (!(status
& QCA_SYSCFG_UPDATED
)) {
3575 err
= btusb_setup_qca_load_nvm(hdev
, &ver
, info
);
3579 /* WCN6855 2.1 will reset to apply firmware downloaded here, so
3580 * wait ~100ms for reset Done then go ahead, otherwise, it maybe
3581 * cause potential enable failure.
3583 if (info
->rom_version
== 0x00130201)
3584 msleep(QCA_BT_RESET_WAIT_MS
);
3590 static inline int __set_diag_interface(struct hci_dev
*hdev
)
3592 struct btusb_data
*data
= hci_get_drvdata(hdev
);
3593 struct usb_interface
*intf
= data
->diag
;
3599 data
->diag_tx_ep
= NULL
;
3600 data
->diag_rx_ep
= NULL
;
3602 for (i
= 0; i
< intf
->cur_altsetting
->desc
.bNumEndpoints
; i
++) {
3603 struct usb_endpoint_descriptor
*ep_desc
;
3605 ep_desc
= &intf
->cur_altsetting
->endpoint
[i
].desc
;
3607 if (!data
->diag_tx_ep
&& usb_endpoint_is_bulk_out(ep_desc
)) {
3608 data
->diag_tx_ep
= ep_desc
;
3612 if (!data
->diag_rx_ep
&& usb_endpoint_is_bulk_in(ep_desc
)) {
3613 data
->diag_rx_ep
= ep_desc
;
3618 if (!data
->diag_tx_ep
|| !data
->diag_rx_ep
) {
3619 bt_dev_err(hdev
, "invalid diagnostic descriptors");
3626 static struct urb
*alloc_diag_urb(struct hci_dev
*hdev
, bool enable
)
3628 struct btusb_data
*data
= hci_get_drvdata(hdev
);
3629 struct sk_buff
*skb
;
3633 if (!data
->diag_tx_ep
)
3634 return ERR_PTR(-ENODEV
);
3636 urb
= usb_alloc_urb(0, GFP_KERNEL
);
3638 return ERR_PTR(-ENOMEM
);
3640 skb
= bt_skb_alloc(2, GFP_KERNEL
);
3643 return ERR_PTR(-ENOMEM
);
3646 skb_put_u8(skb
, 0xf0);
3647 skb_put_u8(skb
, enable
);
3649 pipe
= usb_sndbulkpipe(data
->udev
, data
->diag_tx_ep
->bEndpointAddress
);
3651 usb_fill_bulk_urb(urb
, data
->udev
, pipe
,
3652 skb
->data
, skb
->len
, btusb_tx_complete
, skb
);
3654 skb
->dev
= (void *)hdev
;
3659 static int btusb_bcm_set_diag(struct hci_dev
*hdev
, bool enable
)
3661 struct btusb_data
*data
= hci_get_drvdata(hdev
);
3667 if (!test_bit(HCI_RUNNING
, &hdev
->flags
))
3670 urb
= alloc_diag_urb(hdev
, enable
);
3672 return PTR_ERR(urb
);
3674 return submit_or_queue_tx_urb(hdev
, urb
);
3678 static irqreturn_t
btusb_oob_wake_handler(int irq
, void *priv
)
3680 struct btusb_data
*data
= priv
;
3682 pm_wakeup_event(&data
->udev
->dev
, 0);
3685 /* Disable only if not already disabled (keep it balanced) */
3686 if (test_and_clear_bit(BTUSB_OOB_WAKE_ENABLED
, &data
->flags
)) {
3687 disable_irq_nosync(irq
);
3688 disable_irq_wake(irq
);
3693 static const struct of_device_id btusb_match_table
[] = {
3694 { .compatible
= "usb1286,204e" },
3695 { .compatible
= "usbcf3,e300" }, /* QCA6174A */
3696 { .compatible
= "usb4ca,301a" }, /* QCA6174A (Lite-On) */
3699 MODULE_DEVICE_TABLE(of
, btusb_match_table
);
3701 /* Use an oob wakeup pin? */
3702 static int btusb_config_oob_wake(struct hci_dev
*hdev
)
3704 struct btusb_data
*data
= hci_get_drvdata(hdev
);
3705 struct device
*dev
= &data
->udev
->dev
;
3708 clear_bit(BTUSB_OOB_WAKE_ENABLED
, &data
->flags
);
3710 if (!of_match_device(btusb_match_table
, dev
))
3713 /* Move on if no IRQ specified */
3714 irq
= of_irq_get_byname(dev
->of_node
, "wakeup");
3716 bt_dev_dbg(hdev
, "%s: no OOB Wakeup IRQ in DT", __func__
);
3720 irq_set_status_flags(irq
, IRQ_NOAUTOEN
);
3721 ret
= devm_request_irq(&hdev
->dev
, irq
, btusb_oob_wake_handler
,
3722 0, "OOB Wake-on-BT", data
);
3724 bt_dev_err(hdev
, "%s: IRQ request failed", __func__
);
3728 ret
= device_init_wakeup(dev
, true);
3730 bt_dev_err(hdev
, "%s: failed to init_wakeup", __func__
);
3734 data
->oob_wake_irq
= irq
;
3735 bt_dev_info(hdev
, "OOB Wake-on-BT configured at IRQ %u", irq
);
3740 static void btusb_check_needs_reset_resume(struct usb_interface
*intf
)
3742 if (dmi_check_system(btusb_needs_reset_resume_table
))
3743 interface_to_usbdev(intf
)->quirks
|= USB_QUIRK_RESET_RESUME
;
3746 static bool btusb_prevent_wake(struct hci_dev
*hdev
)
3748 struct btusb_data
*data
= hci_get_drvdata(hdev
);
3750 return !device_may_wakeup(&data
->udev
->dev
);
3753 static int btusb_shutdown_qca(struct hci_dev
*hdev
)
3755 struct sk_buff
*skb
;
3757 skb
= __hci_cmd_sync(hdev
, HCI_OP_RESET
, 0, NULL
, HCI_INIT_TIMEOUT
);
3759 bt_dev_err(hdev
, "HCI reset during shutdown failed");
3760 return PTR_ERR(skb
);
3767 static int btusb_probe(struct usb_interface
*intf
,
3768 const struct usb_device_id
*id
)
3770 struct usb_endpoint_descriptor
*ep_desc
;
3771 struct gpio_desc
*reset_gpio
;
3772 struct btusb_data
*data
;
3773 struct hci_dev
*hdev
;
3774 unsigned ifnum_base
;
3775 int i
, err
, priv_size
;
3777 BT_DBG("intf %p id %p", intf
, id
);
3779 /* interface numbers are hardcoded in the spec */
3780 if (intf
->cur_altsetting
->desc
.bInterfaceNumber
!= 0) {
3781 if (!(id
->driver_info
& BTUSB_IFNUM_2
))
3783 if (intf
->cur_altsetting
->desc
.bInterfaceNumber
!= 2)
3787 ifnum_base
= intf
->cur_altsetting
->desc
.bInterfaceNumber
;
3789 if (!id
->driver_info
) {
3790 const struct usb_device_id
*match
;
3792 match
= usb_match_id(intf
, blacklist_table
);
3797 if (id
->driver_info
== BTUSB_IGNORE
)
3800 if (id
->driver_info
& BTUSB_ATH3012
) {
3801 struct usb_device
*udev
= interface_to_usbdev(intf
);
3803 /* Old firmware would otherwise let ath3k driver load
3804 * patch and sysconfig files
3806 if (le16_to_cpu(udev
->descriptor
.bcdDevice
) <= 0x0001 &&
3807 !btusb_qca_need_patch(udev
))
3811 data
= devm_kzalloc(&intf
->dev
, sizeof(*data
), GFP_KERNEL
);
3815 for (i
= 0; i
< intf
->cur_altsetting
->desc
.bNumEndpoints
; i
++) {
3816 ep_desc
= &intf
->cur_altsetting
->endpoint
[i
].desc
;
3818 if (!data
->intr_ep
&& usb_endpoint_is_int_in(ep_desc
)) {
3819 data
->intr_ep
= ep_desc
;
3823 if (!data
->bulk_tx_ep
&& usb_endpoint_is_bulk_out(ep_desc
)) {
3824 data
->bulk_tx_ep
= ep_desc
;
3828 if (!data
->bulk_rx_ep
&& usb_endpoint_is_bulk_in(ep_desc
)) {
3829 data
->bulk_rx_ep
= ep_desc
;
3834 if (!data
->intr_ep
|| !data
->bulk_tx_ep
|| !data
->bulk_rx_ep
)
3837 if (id
->driver_info
& BTUSB_AMP
) {
3838 data
->cmdreq_type
= USB_TYPE_CLASS
| 0x01;
3839 data
->cmdreq
= 0x2b;
3841 data
->cmdreq_type
= USB_TYPE_CLASS
;
3842 data
->cmdreq
= 0x00;
3845 data
->udev
= interface_to_usbdev(intf
);
3848 INIT_WORK(&data
->work
, btusb_work
);
3849 INIT_WORK(&data
->waker
, btusb_waker
);
3850 init_usb_anchor(&data
->deferred
);
3851 init_usb_anchor(&data
->tx_anchor
);
3852 spin_lock_init(&data
->txlock
);
3854 init_usb_anchor(&data
->intr_anchor
);
3855 init_usb_anchor(&data
->bulk_anchor
);
3856 init_usb_anchor(&data
->isoc_anchor
);
3857 init_usb_anchor(&data
->diag_anchor
);
3858 init_usb_anchor(&data
->ctrl_anchor
);
3859 spin_lock_init(&data
->rxlock
);
3863 data
->recv_event
= hci_recv_frame
;
3864 data
->recv_bulk
= btusb_recv_bulk
;
3866 if (id
->driver_info
& BTUSB_INTEL_COMBINED
) {
3867 /* Allocate extra space for Intel device */
3868 priv_size
+= sizeof(struct btintel_data
);
3870 /* Override the rx handlers */
3871 data
->recv_event
= btusb_recv_event_intel
;
3872 data
->recv_bulk
= btusb_recv_bulk_intel
;
3875 data
->recv_acl
= hci_recv_frame
;
3877 hdev
= hci_alloc_dev_priv(priv_size
);
3881 hdev
->bus
= HCI_USB
;
3882 hci_set_drvdata(hdev
, data
);
3884 if (id
->driver_info
& BTUSB_AMP
)
3885 hdev
->dev_type
= HCI_AMP
;
3887 hdev
->dev_type
= HCI_PRIMARY
;
3891 SET_HCIDEV_DEV(hdev
, &intf
->dev
);
3893 reset_gpio
= gpiod_get_optional(&data
->udev
->dev
, "reset",
3895 if (IS_ERR(reset_gpio
)) {
3896 err
= PTR_ERR(reset_gpio
);
3898 } else if (reset_gpio
) {
3899 data
->reset_gpio
= reset_gpio
;
3902 hdev
->open
= btusb_open
;
3903 hdev
->close
= btusb_close
;
3904 hdev
->flush
= btusb_flush
;
3905 hdev
->send
= btusb_send_frame
;
3906 hdev
->notify
= btusb_notify
;
3907 hdev
->prevent_wake
= btusb_prevent_wake
;
3910 err
= btusb_config_oob_wake(hdev
);
3914 /* Marvell devices may need a specific chip configuration */
3915 if (id
->driver_info
& BTUSB_MARVELL
&& data
->oob_wake_irq
) {
3916 err
= marvell_config_oob_wake(hdev
);
3921 if (id
->driver_info
& BTUSB_CW6622
)
3922 set_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY
, &hdev
->quirks
);
3924 if (id
->driver_info
& BTUSB_BCM2045
)
3925 set_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY
, &hdev
->quirks
);
3927 if (id
->driver_info
& BTUSB_BCM92035
)
3928 hdev
->setup
= btusb_setup_bcm92035
;
3930 if (IS_ENABLED(CONFIG_BT_HCIBTUSB_BCM
) &&
3931 (id
->driver_info
& BTUSB_BCM_PATCHRAM
)) {
3932 hdev
->manufacturer
= 15;
3933 hdev
->setup
= btbcm_setup_patchram
;
3934 hdev
->set_diag
= btusb_bcm_set_diag
;
3935 hdev
->set_bdaddr
= btbcm_set_bdaddr
;
3937 /* Broadcom LM_DIAG Interface numbers are hardcoded */
3938 data
->diag
= usb_ifnum_to_if(data
->udev
, ifnum_base
+ 2);
3941 if (IS_ENABLED(CONFIG_BT_HCIBTUSB_BCM
) &&
3942 (id
->driver_info
& BTUSB_BCM_APPLE
)) {
3943 hdev
->manufacturer
= 15;
3944 hdev
->setup
= btbcm_setup_apple
;
3945 hdev
->set_diag
= btusb_bcm_set_diag
;
3947 /* Broadcom LM_DIAG Interface numbers are hardcoded */
3948 data
->diag
= usb_ifnum_to_if(data
->udev
, ifnum_base
+ 2);
3951 /* Combined Intel Device setup to support multiple setup routine */
3952 if (id
->driver_info
& BTUSB_INTEL_COMBINED
) {
3953 err
= btintel_configure_setup(hdev
);
3957 /* Transport specific configuration */
3958 hdev
->send
= btusb_send_frame_intel
;
3959 hdev
->cmd_timeout
= btusb_intel_cmd_timeout
;
3961 if (id
->driver_info
& BTUSB_INTEL_NO_WBS_SUPPORT
)
3962 btintel_set_flag(hdev
, INTEL_ROM_LEGACY_NO_WBS_SUPPORT
);
3964 if (id
->driver_info
& BTUSB_INTEL_BROKEN_INITIAL_NCMD
)
3965 btintel_set_flag(hdev
, INTEL_BROKEN_INITIAL_NCMD
);
3967 if (id
->driver_info
& BTUSB_INTEL_BROKEN_SHUTDOWN_LED
)
3968 btintel_set_flag(hdev
, INTEL_BROKEN_SHUTDOWN_LED
);
3971 if (id
->driver_info
& BTUSB_MARVELL
) {
3972 struct pci_dev
*pdev
;
3973 hdev
->set_bdaddr
= btusb_set_bdaddr_marvell
;
3974 pdev
= pci_get_subsys(PCI_ANY_ID
, PCI_ANY_ID
, 0x1028, 0x0720, NULL
);
3976 pdev
= pci_get_subsys(PCI_ANY_ID
, PCI_ANY_ID
, 0x1028, 0x0733, NULL
);
3979 hdev
->post_init
= btusb_edge_post_init
;
3980 hdev
->shutdown
= btusb_edge_shutdown
;
3984 if (IS_ENABLED(CONFIG_BT_HCIBTUSB_MTK
) &&
3985 (id
->driver_info
& BTUSB_MEDIATEK
)) {
3986 hdev
->setup
= btusb_mtk_setup
;
3987 hdev
->shutdown
= btusb_mtk_shutdown
;
3988 hdev
->manufacturer
= 70;
3989 hdev
->cmd_timeout
= btusb_mtk_cmd_timeout
;
3990 set_bit(HCI_QUIRK_NON_PERSISTENT_SETUP
, &hdev
->quirks
);
3991 data
->recv_acl
= btusb_recv_acl_mtk
;
3994 if (id
->driver_info
& BTUSB_SWAVE
) {
3995 set_bit(HCI_QUIRK_FIXUP_INQUIRY_MODE
, &hdev
->quirks
);
3996 set_bit(HCI_QUIRK_BROKEN_LOCAL_COMMANDS
, &hdev
->quirks
);
3999 if (id
->driver_info
& BTUSB_INTEL_BOOT
) {
4000 hdev
->manufacturer
= 2;
4001 set_bit(HCI_QUIRK_RAW_DEVICE
, &hdev
->quirks
);
4004 if (id
->driver_info
& BTUSB_ATH3012
) {
4005 data
->setup_on_usb
= btusb_setup_qca
;
4006 hdev
->set_bdaddr
= btusb_set_bdaddr_ath3012
;
4007 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY
, &hdev
->quirks
);
4008 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER
, &hdev
->quirks
);
4011 if (id
->driver_info
& BTUSB_QCA_ROME
) {
4012 data
->setup_on_usb
= btusb_setup_qca
;
4013 hdev
->set_bdaddr
= btusb_set_bdaddr_ath3012
;
4014 hdev
->cmd_timeout
= btusb_qca_cmd_timeout
;
4015 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY
, &hdev
->quirks
);
4016 btusb_check_needs_reset_resume(intf
);
4019 if (id
->driver_info
& BTUSB_QCA_WCN6855
) {
4020 data
->setup_on_usb
= btusb_setup_qca
;
4021 hdev
->shutdown
= btusb_shutdown_qca
;
4022 hdev
->set_bdaddr
= btusb_set_bdaddr_wcn6855
;
4023 hdev
->cmd_timeout
= btusb_qca_cmd_timeout
;
4024 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY
, &hdev
->quirks
);
4025 hci_set_msft_opcode(hdev
, 0xFD70);
4028 if (id
->driver_info
& BTUSB_AMP
) {
4029 /* AMP controllers do not support SCO packets */
4032 /* Interface orders are hardcoded in the specification */
4033 data
->isoc
= usb_ifnum_to_if(data
->udev
, ifnum_base
+ 1);
4034 data
->isoc_ifnum
= ifnum_base
+ 1;
4037 if (IS_ENABLED(CONFIG_BT_HCIBTUSB_RTL
) &&
4038 (id
->driver_info
& BTUSB_REALTEK
)) {
4039 hdev
->setup
= btrtl_setup_realtek
;
4040 hdev
->shutdown
= btrtl_shutdown_realtek
;
4041 hdev
->cmd_timeout
= btusb_rtl_cmd_timeout
;
4043 /* Realtek devices need to set remote wakeup on auto-suspend */
4044 set_bit(BTUSB_WAKEUP_AUTOSUSPEND
, &data
->flags
);
4045 set_bit(BTUSB_USE_ALT3_FOR_WBS
, &data
->flags
);
4049 set_bit(HCI_QUIRK_RESET_ON_CLOSE
, &hdev
->quirks
);
4051 if (force_scofix
|| id
->driver_info
& BTUSB_WRONG_SCO_MTU
) {
4052 if (!disable_scofix
)
4053 set_bit(HCI_QUIRK_FIXUP_BUFFER_SIZE
, &hdev
->quirks
);
4056 if (id
->driver_info
& BTUSB_BROKEN_ISOC
)
4059 if (id
->driver_info
& BTUSB_WIDEBAND_SPEECH
)
4060 set_bit(HCI_QUIRK_WIDEBAND_SPEECH_SUPPORTED
, &hdev
->quirks
);
4062 if (id
->driver_info
& BTUSB_VALID_LE_STATES
)
4063 set_bit(HCI_QUIRK_VALID_LE_STATES
, &hdev
->quirks
);
4065 if (id
->driver_info
& BTUSB_DIGIANSWER
) {
4066 data
->cmdreq_type
= USB_TYPE_VENDOR
;
4067 set_bit(HCI_QUIRK_RESET_ON_CLOSE
, &hdev
->quirks
);
4070 if (id
->driver_info
& BTUSB_CSR
) {
4071 struct usb_device
*udev
= data
->udev
;
4072 u16 bcdDevice
= le16_to_cpu(udev
->descriptor
.bcdDevice
);
4074 /* Old firmware would otherwise execute USB reset */
4075 if (bcdDevice
< 0x117)
4076 set_bit(HCI_QUIRK_RESET_ON_CLOSE
, &hdev
->quirks
);
4078 /* This must be set first in case we disable it for fakes */
4079 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY
, &hdev
->quirks
);
4081 /* Fake CSR devices with broken commands */
4082 if (le16_to_cpu(udev
->descriptor
.idVendor
) == 0x0a12 &&
4083 le16_to_cpu(udev
->descriptor
.idProduct
) == 0x0001)
4084 hdev
->setup
= btusb_setup_csr
;
4087 if (id
->driver_info
& BTUSB_SNIFFER
) {
4088 struct usb_device
*udev
= data
->udev
;
4090 /* New sniffer firmware has crippled HCI interface */
4091 if (le16_to_cpu(udev
->descriptor
.bcdDevice
) > 0x997)
4092 set_bit(HCI_QUIRK_RAW_DEVICE
, &hdev
->quirks
);
4095 if (id
->driver_info
& BTUSB_INTEL_BOOT
) {
4096 /* A bug in the bootloader causes that interrupt interface is
4097 * only enabled after receiving SetInterface(0, AltSetting=0).
4099 err
= usb_set_interface(data
->udev
, 0, 0);
4101 BT_ERR("failed to set interface 0, alt 0 %d", err
);
4107 err
= usb_driver_claim_interface(&btusb_driver
,
4113 if (IS_ENABLED(CONFIG_BT_HCIBTUSB_BCM
) && data
->diag
) {
4114 if (!usb_driver_claim_interface(&btusb_driver
,
4116 __set_diag_interface(hdev
);
4121 if (enable_autosuspend
)
4122 usb_enable_autosuspend(data
->udev
);
4124 err
= hci_register_dev(hdev
);
4128 usb_set_intfdata(intf
, data
);
4133 if (data
->reset_gpio
)
4134 gpiod_put(data
->reset_gpio
);
4139 static void btusb_disconnect(struct usb_interface
*intf
)
4141 struct btusb_data
*data
= usb_get_intfdata(intf
);
4142 struct hci_dev
*hdev
;
4144 BT_DBG("intf %p", intf
);
4150 usb_set_intfdata(data
->intf
, NULL
);
4153 usb_set_intfdata(data
->isoc
, NULL
);
4156 usb_set_intfdata(data
->diag
, NULL
);
4158 hci_unregister_dev(hdev
);
4160 if (intf
== data
->intf
) {
4162 usb_driver_release_interface(&btusb_driver
, data
->isoc
);
4164 usb_driver_release_interface(&btusb_driver
, data
->diag
);
4165 } else if (intf
== data
->isoc
) {
4167 usb_driver_release_interface(&btusb_driver
, data
->diag
);
4168 usb_driver_release_interface(&btusb_driver
, data
->intf
);
4169 } else if (intf
== data
->diag
) {
4170 usb_driver_release_interface(&btusb_driver
, data
->intf
);
4172 usb_driver_release_interface(&btusb_driver
, data
->isoc
);
4175 if (data
->oob_wake_irq
)
4176 device_init_wakeup(&data
->udev
->dev
, false);
4178 if (data
->reset_gpio
)
4179 gpiod_put(data
->reset_gpio
);
4185 static int btusb_suspend(struct usb_interface
*intf
, pm_message_t message
)
4187 struct btusb_data
*data
= usb_get_intfdata(intf
);
4189 BT_DBG("intf %p", intf
);
4191 if (data
->suspend_count
++)
4194 spin_lock_irq(&data
->txlock
);
4195 if (!(PMSG_IS_AUTO(message
) && data
->tx_in_flight
)) {
4196 set_bit(BTUSB_SUSPENDING
, &data
->flags
);
4197 spin_unlock_irq(&data
->txlock
);
4199 spin_unlock_irq(&data
->txlock
);
4200 data
->suspend_count
--;
4204 cancel_work_sync(&data
->work
);
4206 btusb_stop_traffic(data
);
4207 usb_kill_anchored_urbs(&data
->tx_anchor
);
4209 if (data
->oob_wake_irq
&& device_may_wakeup(&data
->udev
->dev
)) {
4210 set_bit(BTUSB_OOB_WAKE_ENABLED
, &data
->flags
);
4211 enable_irq_wake(data
->oob_wake_irq
);
4212 enable_irq(data
->oob_wake_irq
);
4215 /* For global suspend, Realtek devices lose the loaded fw
4216 * in them. But for autosuspend, firmware should remain.
4217 * Actually, it depends on whether the usb host sends
4218 * set feature (enable wakeup) or not.
4220 if (test_bit(BTUSB_WAKEUP_AUTOSUSPEND
, &data
->flags
)) {
4221 if (PMSG_IS_AUTO(message
) &&
4222 device_can_wakeup(&data
->udev
->dev
))
4223 data
->udev
->do_remote_wakeup
= 1;
4224 else if (!PMSG_IS_AUTO(message
) &&
4225 !device_may_wakeup(&data
->udev
->dev
)) {
4226 data
->udev
->do_remote_wakeup
= 0;
4227 data
->udev
->reset_resume
= 1;
4234 static void play_deferred(struct btusb_data
*data
)
4239 while ((urb
= usb_get_from_anchor(&data
->deferred
))) {
4240 usb_anchor_urb(urb
, &data
->tx_anchor
);
4242 err
= usb_submit_urb(urb
, GFP_ATOMIC
);
4244 if (err
!= -EPERM
&& err
!= -ENODEV
)
4245 BT_ERR("%s urb %p submission failed (%d)",
4246 data
->hdev
->name
, urb
, -err
);
4247 kfree(urb
->setup_packet
);
4248 usb_unanchor_urb(urb
);
4253 data
->tx_in_flight
++;
4257 /* Cleanup the rest deferred urbs. */
4258 while ((urb
= usb_get_from_anchor(&data
->deferred
))) {
4259 kfree(urb
->setup_packet
);
4264 static int btusb_resume(struct usb_interface
*intf
)
4266 struct btusb_data
*data
= usb_get_intfdata(intf
);
4267 struct hci_dev
*hdev
= data
->hdev
;
4270 BT_DBG("intf %p", intf
);
4272 if (--data
->suspend_count
)
4275 /* Disable only if not already disabled (keep it balanced) */
4276 if (test_and_clear_bit(BTUSB_OOB_WAKE_ENABLED
, &data
->flags
)) {
4277 disable_irq(data
->oob_wake_irq
);
4278 disable_irq_wake(data
->oob_wake_irq
);
4281 if (!test_bit(HCI_RUNNING
, &hdev
->flags
))
4284 if (test_bit(BTUSB_INTR_RUNNING
, &data
->flags
)) {
4285 err
= btusb_submit_intr_urb(hdev
, GFP_NOIO
);
4287 clear_bit(BTUSB_INTR_RUNNING
, &data
->flags
);
4292 if (test_bit(BTUSB_BULK_RUNNING
, &data
->flags
)) {
4293 err
= btusb_submit_bulk_urb(hdev
, GFP_NOIO
);
4295 clear_bit(BTUSB_BULK_RUNNING
, &data
->flags
);
4299 btusb_submit_bulk_urb(hdev
, GFP_NOIO
);
4302 if (test_bit(BTUSB_ISOC_RUNNING
, &data
->flags
)) {
4303 if (btusb_submit_isoc_urb(hdev
, GFP_NOIO
) < 0)
4304 clear_bit(BTUSB_ISOC_RUNNING
, &data
->flags
);
4306 btusb_submit_isoc_urb(hdev
, GFP_NOIO
);
4309 spin_lock_irq(&data
->txlock
);
4310 play_deferred(data
);
4311 clear_bit(BTUSB_SUSPENDING
, &data
->flags
);
4312 spin_unlock_irq(&data
->txlock
);
4313 schedule_work(&data
->work
);
4318 usb_scuttle_anchored_urbs(&data
->deferred
);
4320 spin_lock_irq(&data
->txlock
);
4321 clear_bit(BTUSB_SUSPENDING
, &data
->flags
);
4322 spin_unlock_irq(&data
->txlock
);
4328 static struct usb_driver btusb_driver
= {
4330 .probe
= btusb_probe
,
4331 .disconnect
= btusb_disconnect
,
4333 .suspend
= btusb_suspend
,
4334 .resume
= btusb_resume
,
4336 .id_table
= btusb_table
,
4337 .supports_autosuspend
= 1,
4338 .disable_hub_initiated_lpm
= 1,
4341 module_usb_driver(btusb_driver
);
4343 module_param(disable_scofix
, bool, 0644);
4344 MODULE_PARM_DESC(disable_scofix
, "Disable fixup of wrong SCO buffer size");
4346 module_param(force_scofix
, bool, 0644);
4347 MODULE_PARM_DESC(force_scofix
, "Force fixup of wrong SCO buffers size");
4349 module_param(enable_autosuspend
, bool, 0644);
4350 MODULE_PARM_DESC(enable_autosuspend
, "Enable USB autosuspend by default");
4352 module_param(reset
, bool, 0644);
4353 MODULE_PARM_DESC(reset
, "Send HCI reset command on initialization");
4355 MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
4356 MODULE_DESCRIPTION("Generic Bluetooth USB driver ver " VERSION
);
4357 MODULE_VERSION(VERSION
);
4358 MODULE_LICENSE("GPL");