1 // SPDX-License-Identifier: GPL-2.0-or-later
4 * Generic Bluetooth USB driver
10 #include <linux/module.h>
11 #include <linux/usb.h>
12 #include <linux/usb/quirks.h>
13 #include <linux/firmware.h>
14 #include <linux/iopoll.h>
15 #include <linux/of_device.h>
16 #include <linux/of_irq.h>
17 #include <linux/suspend.h>
18 #include <linux/gpio/consumer.h>
19 #include <linux/debugfs.h>
20 #include <asm/unaligned.h>
22 #include <net/bluetooth/bluetooth.h>
23 #include <net/bluetooth/hci_core.h>
32 static bool disable_scofix;
33 static bool force_scofix;
34 static bool enable_autosuspend = IS_ENABLED(CONFIG_BT_HCIBTUSB_AUTOSUSPEND);
35 static bool reset = true;
37 static struct usb_driver btusb_driver;
39 #define BTUSB_IGNORE BIT(0)
40 #define BTUSB_DIGIANSWER BIT(1)
41 #define BTUSB_CSR BIT(2)
42 #define BTUSB_SNIFFER BIT(3)
43 #define BTUSB_BCM92035 BIT(4)
44 #define BTUSB_BROKEN_ISOC BIT(5)
45 #define BTUSB_WRONG_SCO_MTU BIT(6)
46 #define BTUSB_ATH3012 BIT(7)
47 #define BTUSB_INTEL_COMBINED BIT(8)
48 #define BTUSB_INTEL_BOOT BIT(9)
49 #define BTUSB_BCM_PATCHRAM BIT(10)
50 #define BTUSB_MARVELL BIT(11)
51 #define BTUSB_SWAVE BIT(12)
52 #define BTUSB_AMP BIT(13)
53 #define BTUSB_QCA_ROME BIT(14)
54 #define BTUSB_BCM_APPLE BIT(15)
55 #define BTUSB_REALTEK BIT(16)
56 #define BTUSB_BCM2045 BIT(17)
57 #define BTUSB_IFNUM_2 BIT(18)
58 #define BTUSB_CW6622 BIT(19)
59 #define BTUSB_MEDIATEK BIT(20)
60 #define BTUSB_WIDEBAND_SPEECH BIT(21)
61 #define BTUSB_VALID_LE_STATES BIT(22)
62 #define BTUSB_QCA_WCN6855 BIT(23)
63 #define BTUSB_INTEL_BROKEN_SHUTDOWN_LED BIT(24)
64 #define BTUSB_INTEL_BROKEN_INITIAL_NCMD BIT(25)
65 #define BTUSB_INTEL_NO_WBS_SUPPORT BIT(26)
67 static const struct usb_device_id btusb_table[] = {
68 /* Generic Bluetooth USB device */
69 { USB_DEVICE_INFO(0xe0, 0x01, 0x01) },
71 /* Generic Bluetooth AMP device */
72 { USB_DEVICE_INFO(0xe0, 0x01, 0x04), .driver_info = BTUSB_AMP },
74 /* Generic Bluetooth USB interface */
75 { USB_INTERFACE_INFO(0xe0, 0x01, 0x01) },
77 /* Apple-specific (Broadcom) devices */
78 { USB_VENDOR_AND_INTERFACE_INFO(0x05ac, 0xff, 0x01, 0x01),
79 .driver_info = BTUSB_BCM_APPLE | BTUSB_IFNUM_2 },
81 /* MediaTek MT76x0E */
82 { USB_DEVICE(0x0e8d, 0x763f) },
84 /* Broadcom SoftSailing reporting vendor specific */
85 { USB_DEVICE(0x0a5c, 0x21e1) },
87 /* Apple MacBookPro 7,1 */
88 { USB_DEVICE(0x05ac, 0x8213) },
91 { USB_DEVICE(0x05ac, 0x8215) },
93 /* Apple MacBookPro6,2 */
94 { USB_DEVICE(0x05ac, 0x8218) },
96 /* Apple MacBookAir3,1, MacBookAir3,2 */
97 { USB_DEVICE(0x05ac, 0x821b) },
99 /* Apple MacBookAir4,1 */
100 { USB_DEVICE(0x05ac, 0x821f) },
102 /* Apple MacBookPro8,2 */
103 { USB_DEVICE(0x05ac, 0x821a) },
105 /* Apple MacMini5,1 */
106 { USB_DEVICE(0x05ac, 0x8281) },
108 /* AVM BlueFRITZ! USB v2.0 */
109 { USB_DEVICE(0x057c, 0x3800), .driver_info = BTUSB_SWAVE },
111 /* Bluetooth Ultraport Module from IBM */
112 { USB_DEVICE(0x04bf, 0x030a) },
114 /* ALPS Modules with non-standard id */
115 { USB_DEVICE(0x044e, 0x3001) },
116 { USB_DEVICE(0x044e, 0x3002) },
118 /* Ericsson with non-standard id */
119 { USB_DEVICE(0x0bdb, 0x1002) },
121 /* Canyon CN-BTU1 with HID interfaces */
122 { USB_DEVICE(0x0c10, 0x0000) },
124 /* Broadcom BCM20702B0 (Dynex/Insignia) */
125 { USB_DEVICE(0x19ff, 0x0239), .driver_info = BTUSB_BCM_PATCHRAM },
127 /* Broadcom BCM43142A0 (Foxconn/Lenovo) */
128 { USB_VENDOR_AND_INTERFACE_INFO(0x105b, 0xff, 0x01, 0x01),
129 .driver_info = BTUSB_BCM_PATCHRAM },
131 /* Broadcom BCM920703 (HTC Vive) */
132 { USB_VENDOR_AND_INTERFACE_INFO(0x0bb4, 0xff, 0x01, 0x01),
133 .driver_info = BTUSB_BCM_PATCHRAM },
135 /* Foxconn - Hon Hai */
136 { USB_VENDOR_AND_INTERFACE_INFO(0x0489, 0xff, 0x01, 0x01),
137 .driver_info = BTUSB_BCM_PATCHRAM },
139 /* Lite-On Technology - Broadcom based */
140 { USB_VENDOR_AND_INTERFACE_INFO(0x04ca, 0xff, 0x01, 0x01),
141 .driver_info = BTUSB_BCM_PATCHRAM },
143 /* Broadcom devices with vendor specific id */
144 { USB_VENDOR_AND_INTERFACE_INFO(0x0a5c, 0xff, 0x01, 0x01),
145 .driver_info = BTUSB_BCM_PATCHRAM },
147 /* ASUSTek Computer - Broadcom based */
148 { USB_VENDOR_AND_INTERFACE_INFO(0x0b05, 0xff, 0x01, 0x01),
149 .driver_info = BTUSB_BCM_PATCHRAM },
151 /* Belkin F8065bf - Broadcom based */
152 { USB_VENDOR_AND_INTERFACE_INFO(0x050d, 0xff, 0x01, 0x01),
153 .driver_info = BTUSB_BCM_PATCHRAM },
155 /* IMC Networks - Broadcom based */
156 { USB_VENDOR_AND_INTERFACE_INFO(0x13d3, 0xff, 0x01, 0x01),
157 .driver_info = BTUSB_BCM_PATCHRAM },
159 /* Dell Computer - Broadcom based */
160 { USB_VENDOR_AND_INTERFACE_INFO(0x413c, 0xff, 0x01, 0x01),
161 .driver_info = BTUSB_BCM_PATCHRAM },
163 /* Toshiba Corp - Broadcom based */
164 { USB_VENDOR_AND_INTERFACE_INFO(0x0930, 0xff, 0x01, 0x01),
165 .driver_info = BTUSB_BCM_PATCHRAM },
167 /* Intel Bluetooth USB Bootloader (RAM module) */
168 { USB_DEVICE(0x8087, 0x0a5a),
169 .driver_info = BTUSB_INTEL_BOOT | BTUSB_BROKEN_ISOC },
171 { } /* Terminating entry */
174 MODULE_DEVICE_TABLE(usb, btusb_table);
176 static const struct usb_device_id blacklist_table[] = {
177 /* CSR BlueCore devices */
178 { USB_DEVICE(0x0a12, 0x0001), .driver_info = BTUSB_CSR },
180 /* Broadcom BCM2033 without firmware */
181 { USB_DEVICE(0x0a5c, 0x2033), .driver_info = BTUSB_IGNORE },
183 /* Broadcom BCM2045 devices */
184 { USB_DEVICE(0x0a5c, 0x2045), .driver_info = BTUSB_BCM2045 },
186 /* Atheros 3011 with sflash firmware */
187 { USB_DEVICE(0x0489, 0xe027), .driver_info = BTUSB_IGNORE },
188 { USB_DEVICE(0x0489, 0xe03d), .driver_info = BTUSB_IGNORE },
189 { USB_DEVICE(0x04f2, 0xaff1), .driver_info = BTUSB_IGNORE },
190 { USB_DEVICE(0x0930, 0x0215), .driver_info = BTUSB_IGNORE },
191 { USB_DEVICE(0x0cf3, 0x3002), .driver_info = BTUSB_IGNORE },
192 { USB_DEVICE(0x0cf3, 0xe019), .driver_info = BTUSB_IGNORE },
193 { USB_DEVICE(0x13d3, 0x3304), .driver_info = BTUSB_IGNORE },
195 /* Atheros AR9285 Malbec with sflash firmware */
196 { USB_DEVICE(0x03f0, 0x311d), .driver_info = BTUSB_IGNORE },
198 /* Atheros 3012 with sflash firmware */
199 { USB_DEVICE(0x0489, 0xe04d), .driver_info = BTUSB_ATH3012 },
200 { USB_DEVICE(0x0489, 0xe04e), .driver_info = BTUSB_ATH3012 },
201 { USB_DEVICE(0x0489, 0xe056), .driver_info = BTUSB_ATH3012 },
202 { USB_DEVICE(0x0489, 0xe057), .driver_info = BTUSB_ATH3012 },
203 { USB_DEVICE(0x0489, 0xe05f), .driver_info = BTUSB_ATH3012 },
204 { USB_DEVICE(0x0489, 0xe076), .driver_info = BTUSB_ATH3012 },
205 { USB_DEVICE(0x0489, 0xe078), .driver_info = BTUSB_ATH3012 },
206 { USB_DEVICE(0x0489, 0xe095), .driver_info = BTUSB_ATH3012 },
207 { USB_DEVICE(0x04c5, 0x1330), .driver_info = BTUSB_ATH3012 },
208 { USB_DEVICE(0x04ca, 0x3004), .driver_info = BTUSB_ATH3012 },
209 { USB_DEVICE(0x04ca, 0x3005), .driver_info = BTUSB_ATH3012 },
210 { USB_DEVICE(0x04ca, 0x3006), .driver_info = BTUSB_ATH3012 },
211 { USB_DEVICE(0x04ca, 0x3007), .driver_info = BTUSB_ATH3012 },
212 { USB_DEVICE(0x04ca, 0x3008), .driver_info = BTUSB_ATH3012 },
213 { USB_DEVICE(0x04ca, 0x300b), .driver_info = BTUSB_ATH3012 },
214 { USB_DEVICE(0x04ca, 0x300d), .driver_info = BTUSB_ATH3012 },
215 { USB_DEVICE(0x04ca, 0x300f), .driver_info = BTUSB_ATH3012 },
216 { USB_DEVICE(0x04ca, 0x3010), .driver_info = BTUSB_ATH3012 },
217 { USB_DEVICE(0x04ca, 0x3014), .driver_info = BTUSB_ATH3012 },
218 { USB_DEVICE(0x04ca, 0x3018), .driver_info = BTUSB_ATH3012 },
219 { USB_DEVICE(0x0930, 0x0219), .driver_info = BTUSB_ATH3012 },
220 { USB_DEVICE(0x0930, 0x021c), .driver_info = BTUSB_ATH3012 },
221 { USB_DEVICE(0x0930, 0x0220), .driver_info = BTUSB_ATH3012 },
222 { USB_DEVICE(0x0930, 0x0227), .driver_info = BTUSB_ATH3012 },
223 { USB_DEVICE(0x0b05, 0x17d0), .driver_info = BTUSB_ATH3012 },
224 { USB_DEVICE(0x0cf3, 0x0036), .driver_info = BTUSB_ATH3012 },
225 { USB_DEVICE(0x0cf3, 0x3004), .driver_info = BTUSB_ATH3012 },
226 { USB_DEVICE(0x0cf3, 0x3008), .driver_info = BTUSB_ATH3012 },
227 { USB_DEVICE(0x0cf3, 0x311d), .driver_info = BTUSB_ATH3012 },
228 { USB_DEVICE(0x0cf3, 0x311e), .driver_info = BTUSB_ATH3012 },
229 { USB_DEVICE(0x0cf3, 0x311f), .driver_info = BTUSB_ATH3012 },
230 { USB_DEVICE(0x0cf3, 0x3121), .driver_info = BTUSB_ATH3012 },
231 { USB_DEVICE(0x0cf3, 0x817a), .driver_info = BTUSB_ATH3012 },
232 { USB_DEVICE(0x0cf3, 0x817b), .driver_info = BTUSB_ATH3012 },
233 { USB_DEVICE(0x0cf3, 0xe003), .driver_info = BTUSB_ATH3012 },
234 { USB_DEVICE(0x0cf3, 0xe004), .driver_info = BTUSB_ATH3012 },
235 { USB_DEVICE(0x0cf3, 0xe005), .driver_info = BTUSB_ATH3012 },
236 { USB_DEVICE(0x0cf3, 0xe006), .driver_info = BTUSB_ATH3012 },
237 { USB_DEVICE(0x13d3, 0x3362), .driver_info = BTUSB_ATH3012 },
238 { USB_DEVICE(0x13d3, 0x3375), .driver_info = BTUSB_ATH3012 },
239 { USB_DEVICE(0x13d3, 0x3393), .driver_info = BTUSB_ATH3012 },
240 { USB_DEVICE(0x13d3, 0x3395), .driver_info = BTUSB_ATH3012 },
241 { USB_DEVICE(0x13d3, 0x3402), .driver_info = BTUSB_ATH3012 },
242 { USB_DEVICE(0x13d3, 0x3408), .driver_info = BTUSB_ATH3012 },
243 { USB_DEVICE(0x13d3, 0x3423), .driver_info = BTUSB_ATH3012 },
244 { USB_DEVICE(0x13d3, 0x3432), .driver_info = BTUSB_ATH3012 },
245 { USB_DEVICE(0x13d3, 0x3472), .driver_info = BTUSB_ATH3012 },
246 { USB_DEVICE(0x13d3, 0x3474), .driver_info = BTUSB_ATH3012 },
247 { USB_DEVICE(0x13d3, 0x3487), .driver_info = BTUSB_ATH3012 },
248 { USB_DEVICE(0x13d3, 0x3490), .driver_info = BTUSB_ATH3012 },
250 /* Atheros AR5BBU12 with sflash firmware */
251 { USB_DEVICE(0x0489, 0xe02c), .driver_info = BTUSB_IGNORE },
253 /* Atheros AR5BBU12 with sflash firmware */
254 { USB_DEVICE(0x0489, 0xe036), .driver_info = BTUSB_ATH3012 },
255 { USB_DEVICE(0x0489, 0xe03c), .driver_info = BTUSB_ATH3012 },
257 /* QCA ROME chipset */
258 { USB_DEVICE(0x0cf3, 0x535b), .driver_info = BTUSB_QCA_ROME |
259 BTUSB_WIDEBAND_SPEECH },
260 { USB_DEVICE(0x0cf3, 0xe007), .driver_info = BTUSB_QCA_ROME |
261 BTUSB_WIDEBAND_SPEECH },
262 { USB_DEVICE(0x0cf3, 0xe009), .driver_info = BTUSB_QCA_ROME |
263 BTUSB_WIDEBAND_SPEECH },
264 { USB_DEVICE(0x0cf3, 0xe010), .driver_info = BTUSB_QCA_ROME |
265 BTUSB_WIDEBAND_SPEECH },
266 { USB_DEVICE(0x0cf3, 0xe300), .driver_info = BTUSB_QCA_ROME |
267 BTUSB_WIDEBAND_SPEECH },
268 { USB_DEVICE(0x0cf3, 0xe301), .driver_info = BTUSB_QCA_ROME |
269 BTUSB_WIDEBAND_SPEECH },
270 { USB_DEVICE(0x0cf3, 0xe360), .driver_info = BTUSB_QCA_ROME |
271 BTUSB_WIDEBAND_SPEECH },
272 { USB_DEVICE(0x0cf3, 0xe500), .driver_info = BTUSB_QCA_ROME |
273 BTUSB_WIDEBAND_SPEECH },
274 { USB_DEVICE(0x0489, 0xe092), .driver_info = BTUSB_QCA_ROME |
275 BTUSB_WIDEBAND_SPEECH },
276 { USB_DEVICE(0x0489, 0xe09f), .driver_info = BTUSB_QCA_ROME |
277 BTUSB_WIDEBAND_SPEECH },
278 { USB_DEVICE(0x0489, 0xe0a2), .driver_info = BTUSB_QCA_ROME |
279 BTUSB_WIDEBAND_SPEECH },
280 { USB_DEVICE(0x04ca, 0x3011), .driver_info = BTUSB_QCA_ROME |
281 BTUSB_WIDEBAND_SPEECH },
282 { USB_DEVICE(0x04ca, 0x3015), .driver_info = BTUSB_QCA_ROME |
283 BTUSB_WIDEBAND_SPEECH },
284 { USB_DEVICE(0x04ca, 0x3016), .driver_info = BTUSB_QCA_ROME |
285 BTUSB_WIDEBAND_SPEECH },
286 { USB_DEVICE(0x04ca, 0x301a), .driver_info = BTUSB_QCA_ROME |
287 BTUSB_WIDEBAND_SPEECH },
288 { USB_DEVICE(0x04ca, 0x3021), .driver_info = BTUSB_QCA_ROME |
289 BTUSB_WIDEBAND_SPEECH },
290 { USB_DEVICE(0x13d3, 0x3491), .driver_info = BTUSB_QCA_ROME |
291 BTUSB_WIDEBAND_SPEECH },
292 { USB_DEVICE(0x13d3, 0x3496), .driver_info = BTUSB_QCA_ROME |
293 BTUSB_WIDEBAND_SPEECH },
294 { USB_DEVICE(0x13d3, 0x3501), .driver_info = BTUSB_QCA_ROME |
295 BTUSB_WIDEBAND_SPEECH },
297 /* QCA WCN6855 chipset */
298 { USB_DEVICE(0x0cf3, 0xe600), .driver_info = BTUSB_QCA_WCN6855 |
299 BTUSB_WIDEBAND_SPEECH |
300 BTUSB_VALID_LE_STATES },
301 { USB_DEVICE(0x0489, 0xe0cc), .driver_info = BTUSB_QCA_WCN6855 |
302 BTUSB_WIDEBAND_SPEECH |
303 BTUSB_VALID_LE_STATES },
304 { USB_DEVICE(0x0489, 0xe0d6), .driver_info = BTUSB_QCA_WCN6855 |
305 BTUSB_WIDEBAND_SPEECH |
306 BTUSB_VALID_LE_STATES },
307 { USB_DEVICE(0x0489, 0xe0e3), .driver_info = BTUSB_QCA_WCN6855 |
308 BTUSB_WIDEBAND_SPEECH |
309 BTUSB_VALID_LE_STATES },
310 { USB_DEVICE(0x10ab, 0x9309), .driver_info = BTUSB_QCA_WCN6855 |
311 BTUSB_WIDEBAND_SPEECH |
312 BTUSB_VALID_LE_STATES },
313 { USB_DEVICE(0x10ab, 0x9409), .driver_info = BTUSB_QCA_WCN6855 |
314 BTUSB_WIDEBAND_SPEECH |
315 BTUSB_VALID_LE_STATES },
316 { USB_DEVICE(0x0489, 0xe0d0), .driver_info = BTUSB_QCA_WCN6855 |
317 BTUSB_WIDEBAND_SPEECH |
318 BTUSB_VALID_LE_STATES },
320 /* QCA WCN785x chipset */
321 { USB_DEVICE(0x0cf3, 0xe700), .driver_info = BTUSB_QCA_WCN6855 |
322 BTUSB_WIDEBAND_SPEECH |
323 BTUSB_VALID_LE_STATES },
325 /* Broadcom BCM2035 */
326 { USB_DEVICE(0x0a5c, 0x2009), .driver_info = BTUSB_BCM92035 },
327 { USB_DEVICE(0x0a5c, 0x200a), .driver_info = BTUSB_WRONG_SCO_MTU },
328 { USB_DEVICE(0x0a5c, 0x2035), .driver_info = BTUSB_WRONG_SCO_MTU },
330 /* Broadcom BCM2045 */
331 { USB_DEVICE(0x0a5c, 0x2039), .driver_info = BTUSB_WRONG_SCO_MTU },
332 { USB_DEVICE(0x0a5c, 0x2101), .driver_info = BTUSB_WRONG_SCO_MTU },
334 /* IBM/Lenovo ThinkPad with Broadcom chip */
335 { USB_DEVICE(0x0a5c, 0x201e), .driver_info = BTUSB_WRONG_SCO_MTU },
336 { USB_DEVICE(0x0a5c, 0x2110), .driver_info = BTUSB_WRONG_SCO_MTU },
338 /* HP laptop with Broadcom chip */
339 { USB_DEVICE(0x03f0, 0x171d), .driver_info = BTUSB_WRONG_SCO_MTU },
341 /* Dell laptop with Broadcom chip */
342 { USB_DEVICE(0x413c, 0x8126), .driver_info = BTUSB_WRONG_SCO_MTU },
344 /* Dell Wireless 370 and 410 devices */
345 { USB_DEVICE(0x413c, 0x8152), .driver_info = BTUSB_WRONG_SCO_MTU },
346 { USB_DEVICE(0x413c, 0x8156), .driver_info = BTUSB_WRONG_SCO_MTU },
348 /* Belkin F8T012 and F8T013 devices */
349 { USB_DEVICE(0x050d, 0x0012), .driver_info = BTUSB_WRONG_SCO_MTU },
350 { USB_DEVICE(0x050d, 0x0013), .driver_info = BTUSB_WRONG_SCO_MTU },
352 /* Asus WL-BTD202 device */
353 { USB_DEVICE(0x0b05, 0x1715), .driver_info = BTUSB_WRONG_SCO_MTU },
355 /* Kensington Bluetooth USB adapter */
356 { USB_DEVICE(0x047d, 0x105e), .driver_info = BTUSB_WRONG_SCO_MTU },
358 /* RTX Telecom based adapters with buggy SCO support */
359 { USB_DEVICE(0x0400, 0x0807), .driver_info = BTUSB_BROKEN_ISOC },
360 { USB_DEVICE(0x0400, 0x080a), .driver_info = BTUSB_BROKEN_ISOC },
362 /* CONWISE Technology based adapters with buggy SCO support */
363 { USB_DEVICE(0x0e5e, 0x6622),
364 .driver_info = BTUSB_BROKEN_ISOC | BTUSB_CW6622},
366 /* Roper Class 1 Bluetooth Dongle (Silicon Wave based) */
367 { USB_DEVICE(0x1310, 0x0001), .driver_info = BTUSB_SWAVE },
369 /* Digianswer devices */
370 { USB_DEVICE(0x08fd, 0x0001), .driver_info = BTUSB_DIGIANSWER },
371 { USB_DEVICE(0x08fd, 0x0002), .driver_info = BTUSB_IGNORE },
373 /* CSR BlueCore Bluetooth Sniffer */
374 { USB_DEVICE(0x0a12, 0x0002),
375 .driver_info = BTUSB_SNIFFER | BTUSB_BROKEN_ISOC },
377 /* Frontline ComProbe Bluetooth Sniffer */
378 { USB_DEVICE(0x16d3, 0x0002),
379 .driver_info = BTUSB_SNIFFER | BTUSB_BROKEN_ISOC },
381 /* Marvell Bluetooth devices */
382 { USB_DEVICE(0x1286, 0x2044), .driver_info = BTUSB_MARVELL },
383 { USB_DEVICE(0x1286, 0x2046), .driver_info = BTUSB_MARVELL },
384 { USB_DEVICE(0x1286, 0x204e), .driver_info = BTUSB_MARVELL },
386 /* Intel Bluetooth devices */
387 { USB_DEVICE(0x8087, 0x0025), .driver_info = BTUSB_INTEL_COMBINED },
388 { USB_DEVICE(0x8087, 0x0026), .driver_info = BTUSB_INTEL_COMBINED },
389 { USB_DEVICE(0x8087, 0x0029), .driver_info = BTUSB_INTEL_COMBINED },
390 { USB_DEVICE(0x8087, 0x0032), .driver_info = BTUSB_INTEL_COMBINED },
391 { USB_DEVICE(0x8087, 0x0033), .driver_info = BTUSB_INTEL_COMBINED },
392 { USB_DEVICE(0x8087, 0x0035), .driver_info = BTUSB_INTEL_COMBINED },
393 { USB_DEVICE(0x8087, 0x07da), .driver_info = BTUSB_CSR },
394 { USB_DEVICE(0x8087, 0x07dc), .driver_info = BTUSB_INTEL_COMBINED |
395 BTUSB_INTEL_NO_WBS_SUPPORT |
396 BTUSB_INTEL_BROKEN_INITIAL_NCMD |
397 BTUSB_INTEL_BROKEN_SHUTDOWN_LED },
398 { USB_DEVICE(0x8087, 0x0a2a), .driver_info = BTUSB_INTEL_COMBINED |
399 BTUSB_INTEL_NO_WBS_SUPPORT |
400 BTUSB_INTEL_BROKEN_SHUTDOWN_LED },
401 { USB_DEVICE(0x8087, 0x0a2b), .driver_info = BTUSB_INTEL_COMBINED },
402 { USB_DEVICE(0x8087, 0x0aa7), .driver_info = BTUSB_INTEL_COMBINED |
403 BTUSB_INTEL_BROKEN_SHUTDOWN_LED },
404 { USB_DEVICE(0x8087, 0x0aaa), .driver_info = BTUSB_INTEL_COMBINED },
406 /* Other Intel Bluetooth devices */
407 { USB_VENDOR_AND_INTERFACE_INFO(0x8087, 0xe0, 0x01, 0x01),
408 .driver_info = BTUSB_IGNORE },
410 /* Realtek 8822CE Bluetooth devices */
411 { USB_DEVICE(0x0bda, 0xb00c), .driver_info = BTUSB_REALTEK |
412 BTUSB_WIDEBAND_SPEECH },
413 { USB_DEVICE(0x0bda, 0xc822), .driver_info = BTUSB_REALTEK |
414 BTUSB_WIDEBAND_SPEECH },
416 /* Realtek 8852AE Bluetooth devices */
417 { USB_DEVICE(0x0bda, 0x2852), .driver_info = BTUSB_REALTEK |
418 BTUSB_WIDEBAND_SPEECH },
419 { USB_DEVICE(0x0bda, 0xc852), .driver_info = BTUSB_REALTEK |
420 BTUSB_WIDEBAND_SPEECH },
421 { USB_DEVICE(0x0bda, 0x385a), .driver_info = BTUSB_REALTEK |
422 BTUSB_WIDEBAND_SPEECH },
423 { USB_DEVICE(0x0bda, 0x4852), .driver_info = BTUSB_REALTEK |
424 BTUSB_WIDEBAND_SPEECH },
425 { USB_DEVICE(0x04c5, 0x165c), .driver_info = BTUSB_REALTEK |
426 BTUSB_WIDEBAND_SPEECH },
427 { USB_DEVICE(0x04ca, 0x4006), .driver_info = BTUSB_REALTEK |
428 BTUSB_WIDEBAND_SPEECH },
430 /* Realtek 8852CE Bluetooth devices */
431 { USB_DEVICE(0x04ca, 0x4007), .driver_info = BTUSB_REALTEK |
432 BTUSB_WIDEBAND_SPEECH },
433 { USB_DEVICE(0x04c5, 0x1675), .driver_info = BTUSB_REALTEK |
434 BTUSB_WIDEBAND_SPEECH },
435 { USB_DEVICE(0x0cb8, 0xc558), .driver_info = BTUSB_REALTEK |
436 BTUSB_WIDEBAND_SPEECH },
437 { USB_DEVICE(0x13d3, 0x3587), .driver_info = BTUSB_REALTEK |
438 BTUSB_WIDEBAND_SPEECH },
439 { USB_DEVICE(0x13d3, 0x3586), .driver_info = BTUSB_REALTEK |
440 BTUSB_WIDEBAND_SPEECH },
442 /* Realtek Bluetooth devices */
443 { USB_VENDOR_AND_INTERFACE_INFO(0x0bda, 0xe0, 0x01, 0x01),
444 .driver_info = BTUSB_REALTEK },
446 /* MediaTek Bluetooth devices */
447 { USB_VENDOR_AND_INTERFACE_INFO(0x0e8d, 0xe0, 0x01, 0x01),
448 .driver_info = BTUSB_MEDIATEK |
449 BTUSB_WIDEBAND_SPEECH |
450 BTUSB_VALID_LE_STATES },
452 /* Additional MediaTek MT7615E Bluetooth devices */
453 { USB_DEVICE(0x13d3, 0x3560), .driver_info = BTUSB_MEDIATEK},
455 /* Additional MediaTek MT7663 Bluetooth devices */
456 { USB_DEVICE(0x043e, 0x310c), .driver_info = BTUSB_MEDIATEK |
457 BTUSB_WIDEBAND_SPEECH |
458 BTUSB_VALID_LE_STATES },
460 /* Additional MediaTek MT7668 Bluetooth devices */
461 { USB_DEVICE(0x043e, 0x3109), .driver_info = BTUSB_MEDIATEK |
462 BTUSB_WIDEBAND_SPEECH |
463 BTUSB_VALID_LE_STATES },
465 /* Additional MediaTek MT7921 Bluetooth devices */
466 { USB_DEVICE(0x0489, 0xe0c8), .driver_info = BTUSB_MEDIATEK |
467 BTUSB_WIDEBAND_SPEECH |
468 BTUSB_VALID_LE_STATES },
469 { USB_DEVICE(0x04ca, 0x3802), .driver_info = BTUSB_MEDIATEK |
470 BTUSB_WIDEBAND_SPEECH |
471 BTUSB_VALID_LE_STATES },
472 { USB_DEVICE(0x13d3, 0x3563), .driver_info = BTUSB_MEDIATEK |
473 BTUSB_WIDEBAND_SPEECH |
474 BTUSB_VALID_LE_STATES },
475 { USB_DEVICE(0x13d3, 0x3564), .driver_info = BTUSB_MEDIATEK |
476 BTUSB_WIDEBAND_SPEECH |
477 BTUSB_VALID_LE_STATES },
478 { USB_DEVICE(0x13d3, 0x3567), .driver_info = BTUSB_MEDIATEK |
479 BTUSB_WIDEBAND_SPEECH |
480 BTUSB_VALID_LE_STATES },
481 { USB_DEVICE(0x0489, 0xe0cd), .driver_info = BTUSB_MEDIATEK |
482 BTUSB_WIDEBAND_SPEECH |
483 BTUSB_VALID_LE_STATES },
485 /* MediaTek MT7922A Bluetooth devices */
486 { USB_DEVICE(0x0489, 0xe0d8), .driver_info = BTUSB_MEDIATEK |
487 BTUSB_WIDEBAND_SPEECH |
488 BTUSB_VALID_LE_STATES },
489 { USB_DEVICE(0x0489, 0xe0d9), .driver_info = BTUSB_MEDIATEK |
490 BTUSB_WIDEBAND_SPEECH |
491 BTUSB_VALID_LE_STATES },
492 { USB_DEVICE(0x13d3, 0x3568), .driver_info = BTUSB_MEDIATEK |
493 BTUSB_WIDEBAND_SPEECH |
494 BTUSB_VALID_LE_STATES },
495 { USB_DEVICE(0x0489, 0xe0e2), .driver_info = BTUSB_MEDIATEK |
496 BTUSB_WIDEBAND_SPEECH |
497 BTUSB_VALID_LE_STATES },
499 /* Additional Realtek 8723AE Bluetooth devices */
500 { USB_DEVICE(0x0930, 0x021d), .driver_info = BTUSB_REALTEK },
501 { USB_DEVICE(0x13d3, 0x3394), .driver_info = BTUSB_REALTEK },
503 /* Additional Realtek 8723BE Bluetooth devices */
504 { USB_DEVICE(0x0489, 0xe085), .driver_info = BTUSB_REALTEK },
505 { USB_DEVICE(0x0489, 0xe08b), .driver_info = BTUSB_REALTEK },
506 { USB_DEVICE(0x04f2, 0xb49f), .driver_info = BTUSB_REALTEK },
507 { USB_DEVICE(0x13d3, 0x3410), .driver_info = BTUSB_REALTEK },
508 { USB_DEVICE(0x13d3, 0x3416), .driver_info = BTUSB_REALTEK },
509 { USB_DEVICE(0x13d3, 0x3459), .driver_info = BTUSB_REALTEK },
510 { USB_DEVICE(0x13d3, 0x3494), .driver_info = BTUSB_REALTEK },
512 /* Additional Realtek 8723BU Bluetooth devices */
513 { USB_DEVICE(0x7392, 0xa611), .driver_info = BTUSB_REALTEK },
515 /* Additional Realtek 8723DE Bluetooth devices */
516 { USB_DEVICE(0x0bda, 0xb009), .driver_info = BTUSB_REALTEK },
517 { USB_DEVICE(0x2ff8, 0xb011), .driver_info = BTUSB_REALTEK },
519 /* Additional Realtek 8761B Bluetooth devices */
520 { USB_DEVICE(0x2357, 0x0604), .driver_info = BTUSB_REALTEK |
521 BTUSB_WIDEBAND_SPEECH },
523 /* Additional Realtek 8761BU Bluetooth devices */
524 { USB_DEVICE(0x0b05, 0x190e), .driver_info = BTUSB_REALTEK |
525 BTUSB_WIDEBAND_SPEECH },
526 { USB_DEVICE(0x2550, 0x8761), .driver_info = BTUSB_REALTEK |
527 BTUSB_WIDEBAND_SPEECH },
529 /* Additional Realtek 8761BUV Bluetooth devices */
530 { USB_DEVICE(0x0bda, 0x8771), .driver_info = BTUSB_REALTEK |
531 BTUSB_WIDEBAND_SPEECH },
533 /* Additional Realtek 8821AE Bluetooth devices */
534 { USB_DEVICE(0x0b05, 0x17dc), .driver_info = BTUSB_REALTEK },
535 { USB_DEVICE(0x13d3, 0x3414), .driver_info = BTUSB_REALTEK },
536 { USB_DEVICE(0x13d3, 0x3458), .driver_info = BTUSB_REALTEK },
537 { USB_DEVICE(0x13d3, 0x3461), .driver_info = BTUSB_REALTEK },
538 { USB_DEVICE(0x13d3, 0x3462), .driver_info = BTUSB_REALTEK },
540 /* Additional Realtek 8822BE Bluetooth devices */
541 { USB_DEVICE(0x13d3, 0x3526), .driver_info = BTUSB_REALTEK },
542 { USB_DEVICE(0x0b05, 0x185c), .driver_info = BTUSB_REALTEK },
544 /* Additional Realtek 8822CE Bluetooth devices */
545 { USB_DEVICE(0x04ca, 0x4005), .driver_info = BTUSB_REALTEK |
546 BTUSB_WIDEBAND_SPEECH },
547 { USB_DEVICE(0x04c5, 0x161f), .driver_info = BTUSB_REALTEK |
548 BTUSB_WIDEBAND_SPEECH },
549 { USB_DEVICE(0x0b05, 0x18ef), .driver_info = BTUSB_REALTEK |
550 BTUSB_WIDEBAND_SPEECH },
551 { USB_DEVICE(0x13d3, 0x3548), .driver_info = BTUSB_REALTEK |
552 BTUSB_WIDEBAND_SPEECH },
553 { USB_DEVICE(0x13d3, 0x3549), .driver_info = BTUSB_REALTEK |
554 BTUSB_WIDEBAND_SPEECH },
555 { USB_DEVICE(0x13d3, 0x3553), .driver_info = BTUSB_REALTEK |
556 BTUSB_WIDEBAND_SPEECH },
557 { USB_DEVICE(0x13d3, 0x3555), .driver_info = BTUSB_REALTEK |
558 BTUSB_WIDEBAND_SPEECH },
559 { USB_DEVICE(0x2ff8, 0x3051), .driver_info = BTUSB_REALTEK |
560 BTUSB_WIDEBAND_SPEECH },
561 { USB_DEVICE(0x1358, 0xc123), .driver_info = BTUSB_REALTEK |
562 BTUSB_WIDEBAND_SPEECH },
563 { USB_DEVICE(0x0bda, 0xc123), .driver_info = BTUSB_REALTEK |
564 BTUSB_WIDEBAND_SPEECH },
565 { USB_DEVICE(0x0cb5, 0xc547), .driver_info = BTUSB_REALTEK |
566 BTUSB_WIDEBAND_SPEECH },
568 /* Silicon Wave based devices */
569 { USB_DEVICE(0x0c10, 0x0000), .driver_info = BTUSB_SWAVE },
571 { } /* Terminating entry */
574 /* The Bluetooth USB module build into some devices needs to be reset on resume,
575 * this is a problem with the platform (likely shutting off all power) not with
576 * the module itself. So we use a DMI list to match known broken platforms.
578 static const struct dmi_system_id btusb_needs_reset_resume_table[] = {
580 /* Dell OptiPlex 3060 (QCA ROME device 0cf3:e007) */
582 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
583 DMI_MATCH(DMI_PRODUCT_NAME, "OptiPlex 3060"),
587 /* Dell XPS 9360 (QCA ROME device 0cf3:e300) */
589 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
590 DMI_MATCH(DMI_PRODUCT_NAME, "XPS 13 9360"),
594 /* Dell Inspiron 5565 (QCA ROME device 0cf3:e009) */
596 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
597 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 5565"),
603 #define BTUSB_MAX_ISOC_FRAMES 10
605 #define BTUSB_INTR_RUNNING 0
606 #define BTUSB_BULK_RUNNING 1
607 #define BTUSB_ISOC_RUNNING 2
608 #define BTUSB_SUSPENDING 3
609 #define BTUSB_DID_ISO_RESUME 4
610 #define BTUSB_BOOTLOADER 5
611 #define BTUSB_DOWNLOADING 6
612 #define BTUSB_FIRMWARE_LOADED 7
613 #define BTUSB_FIRMWARE_FAILED 8
614 #define BTUSB_BOOTING 9
615 #define BTUSB_DIAG_RUNNING 10
616 #define BTUSB_OOB_WAKE_ENABLED 11
617 #define BTUSB_HW_RESET_ACTIVE 12
618 #define BTUSB_TX_WAIT_VND_EVT 13
619 #define BTUSB_WAKEUP_AUTOSUSPEND 14
620 #define BTUSB_USE_ALT3_FOR_WBS 15
623 struct hci_dev *hdev;
624 struct usb_device *udev;
625 struct usb_interface *intf;
626 struct usb_interface *isoc;
627 struct usb_interface *diag;
634 struct work_struct work;
635 struct work_struct waker;
636 struct delayed_work rx_work;
638 struct sk_buff_head acl_q;
640 struct usb_anchor deferred;
641 struct usb_anchor tx_anchor;
645 struct usb_anchor intr_anchor;
646 struct usb_anchor bulk_anchor;
647 struct usb_anchor isoc_anchor;
648 struct usb_anchor diag_anchor;
649 struct usb_anchor ctrl_anchor;
652 struct sk_buff *evt_skb;
653 struct sk_buff *acl_skb;
654 struct sk_buff *sco_skb;
656 struct usb_endpoint_descriptor *intr_ep;
657 struct usb_endpoint_descriptor *bulk_tx_ep;
658 struct usb_endpoint_descriptor *bulk_rx_ep;
659 struct usb_endpoint_descriptor *isoc_tx_ep;
660 struct usb_endpoint_descriptor *isoc_rx_ep;
661 struct usb_endpoint_descriptor *diag_tx_ep;
662 struct usb_endpoint_descriptor *diag_rx_ep;
664 struct gpio_desc *reset_gpio;
669 unsigned int sco_num;
670 unsigned int air_mode;
671 bool usb_alt6_packet_flow;
675 int (*recv_event)(struct hci_dev *hdev, struct sk_buff *skb);
676 int (*recv_acl)(struct hci_dev *hdev, struct sk_buff *skb);
677 int (*recv_bulk)(struct btusb_data *data, void *buffer, int count);
679 int (*setup_on_usb)(struct hci_dev *hdev);
681 int oob_wake_irq; /* irq for out-of-band wake-on-bt */
682 unsigned cmd_timeout_cnt;
685 static void btusb_intel_cmd_timeout(struct hci_dev *hdev)
687 struct btusb_data *data = hci_get_drvdata(hdev);
688 struct gpio_desc *reset_gpio = data->reset_gpio;
690 if (++data->cmd_timeout_cnt < 5)
694 bt_dev_err(hdev, "No way to reset. Ignoring and continuing");
699 * Toggle the hard reset line if the platform provides one. The reset
700 * is going to yank the device off the USB and then replug. So doing
701 * once is enough. The cleanup is handled correctly on the way out
702 * (standard USB disconnect), and the new device is detected cleanly
703 * and bound to the driver again like it should be.
705 if (test_and_set_bit(BTUSB_HW_RESET_ACTIVE, &data->flags)) {
706 bt_dev_err(hdev, "last reset failed? Not resetting again");
710 bt_dev_err(hdev, "Initiating HW reset via gpio");
711 gpiod_set_value_cansleep(reset_gpio, 1);
713 gpiod_set_value_cansleep(reset_gpio, 0);
716 static void btusb_rtl_cmd_timeout(struct hci_dev *hdev)
718 struct btusb_data *data = hci_get_drvdata(hdev);
719 struct gpio_desc *reset_gpio = data->reset_gpio;
721 if (++data->cmd_timeout_cnt < 5)
725 bt_dev_err(hdev, "No gpio to reset Realtek device, ignoring");
729 /* Toggle the hard reset line. The Realtek device is going to
730 * yank itself off the USB and then replug. The cleanup is handled
731 * correctly on the way out (standard USB disconnect), and the new
732 * device is detected cleanly and bound to the driver again like
735 if (test_and_set_bit(BTUSB_HW_RESET_ACTIVE, &data->flags)) {
736 bt_dev_err(hdev, "last reset failed? Not resetting again");
740 bt_dev_err(hdev, "Reset Realtek device via gpio");
741 gpiod_set_value_cansleep(reset_gpio, 1);
743 gpiod_set_value_cansleep(reset_gpio, 0);
746 static void btusb_qca_cmd_timeout(struct hci_dev *hdev)
748 struct btusb_data *data = hci_get_drvdata(hdev);
749 struct gpio_desc *reset_gpio = data->reset_gpio;
752 if (++data->cmd_timeout_cnt < 5)
756 bt_dev_err(hdev, "Reset qca device via bt_en gpio");
758 /* Toggle the hard reset line. The qca bt device is going to
759 * yank itself off the USB and then replug. The cleanup is handled
760 * correctly on the way out (standard USB disconnect), and the new
761 * device is detected cleanly and bound to the driver again like
764 if (test_and_set_bit(BTUSB_HW_RESET_ACTIVE, &data->flags)) {
765 bt_dev_err(hdev, "last reset failed? Not resetting again");
769 gpiod_set_value_cansleep(reset_gpio, 0);
771 gpiod_set_value_cansleep(reset_gpio, 1);
776 bt_dev_err(hdev, "Multiple cmd timeouts seen. Resetting usb device.");
777 /* This is not an unbalanced PM reference since the device will reset */
778 err = usb_autopm_get_interface(data->intf);
780 usb_queue_reset_device(data->intf);
782 bt_dev_err(hdev, "Failed usb_autopm_get_interface with %d", err);
785 static inline void btusb_free_frags(struct btusb_data *data)
789 spin_lock_irqsave(&data->rxlock, flags);
791 kfree_skb(data->evt_skb);
792 data->evt_skb = NULL;
794 kfree_skb(data->acl_skb);
795 data->acl_skb = NULL;
797 kfree_skb(data->sco_skb);
798 data->sco_skb = NULL;
800 spin_unlock_irqrestore(&data->rxlock, flags);
803 static int btusb_recv_event(struct btusb_data *data, struct sk_buff *skb)
805 if (data->intr_interval) {
806 /* Trigger dequeue immediatelly if an event is received */
807 schedule_delayed_work(&data->rx_work, 0);
810 return data->recv_event(data->hdev, skb);
813 static int btusb_recv_intr(struct btusb_data *data, void *buffer, int count)
819 spin_lock_irqsave(&data->rxlock, flags);
826 skb = bt_skb_alloc(HCI_MAX_EVENT_SIZE, GFP_ATOMIC);
832 hci_skb_pkt_type(skb) = HCI_EVENT_PKT;
833 hci_skb_expect(skb) = HCI_EVENT_HDR_SIZE;
836 len = min_t(uint, hci_skb_expect(skb), count);
837 skb_put_data(skb, buffer, len);
841 hci_skb_expect(skb) -= len;
843 if (skb->len == HCI_EVENT_HDR_SIZE) {
844 /* Complete event header */
845 hci_skb_expect(skb) = hci_event_hdr(skb)->plen;
847 if (skb_tailroom(skb) < hci_skb_expect(skb)) {
856 if (!hci_skb_expect(skb)) {
858 btusb_recv_event(data, skb);
864 spin_unlock_irqrestore(&data->rxlock, flags);
869 static int btusb_recv_acl(struct btusb_data *data, struct sk_buff *skb)
871 /* Only queue ACL packet if intr_interval is set as it means
872 * force_poll_sync has been enabled.
874 if (!data->intr_interval)
875 return data->recv_acl(data->hdev, skb);
877 skb_queue_tail(&data->acl_q, skb);
878 schedule_delayed_work(&data->rx_work, data->intr_interval);
883 static int btusb_recv_bulk(struct btusb_data *data, void *buffer, int count)
889 spin_lock_irqsave(&data->rxlock, flags);
896 skb = bt_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC);
902 hci_skb_pkt_type(skb) = HCI_ACLDATA_PKT;
903 hci_skb_expect(skb) = HCI_ACL_HDR_SIZE;
906 len = min_t(uint, hci_skb_expect(skb), count);
907 skb_put_data(skb, buffer, len);
911 hci_skb_expect(skb) -= len;
913 if (skb->len == HCI_ACL_HDR_SIZE) {
914 __u16 handle = __le16_to_cpu(hci_acl_hdr(skb)->handle);
915 __le16 dlen = hci_acl_hdr(skb)->dlen;
918 /* Complete ACL header */
919 hci_skb_expect(skb) = __le16_to_cpu(dlen);
921 /* Detect if ISO packet has been sent over bulk */
922 if (hci_conn_num(data->hdev, ISO_LINK)) {
923 type = hci_conn_lookup_type(data->hdev,
925 if (type == ISO_LINK)
926 hci_skb_pkt_type(skb) = HCI_ISODATA_PKT;
929 if (skb_tailroom(skb) < hci_skb_expect(skb)) {
938 if (!hci_skb_expect(skb)) {
940 btusb_recv_acl(data, skb);
946 spin_unlock_irqrestore(&data->rxlock, flags);
951 static int btusb_recv_isoc(struct btusb_data *data, void *buffer, int count)
957 spin_lock_irqsave(&data->rxlock, flags);
964 skb = bt_skb_alloc(HCI_MAX_SCO_SIZE, GFP_ATOMIC);
970 hci_skb_pkt_type(skb) = HCI_SCODATA_PKT;
971 hci_skb_expect(skb) = HCI_SCO_HDR_SIZE;
974 len = min_t(uint, hci_skb_expect(skb), count);
975 skb_put_data(skb, buffer, len);
979 hci_skb_expect(skb) -= len;
981 if (skb->len == HCI_SCO_HDR_SIZE) {
982 /* Complete SCO header */
983 hci_skb_expect(skb) = hci_sco_hdr(skb)->dlen;
985 if (skb_tailroom(skb) < hci_skb_expect(skb)) {
994 if (!hci_skb_expect(skb)) {
996 hci_recv_frame(data->hdev, skb);
1001 data->sco_skb = skb;
1002 spin_unlock_irqrestore(&data->rxlock, flags);
1007 static void btusb_intr_complete(struct urb *urb)
1009 struct hci_dev *hdev = urb->context;
1010 struct btusb_data *data = hci_get_drvdata(hdev);
1013 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
1014 urb->actual_length);
1016 if (!test_bit(HCI_RUNNING, &hdev->flags))
1019 if (urb->status == 0) {
1020 hdev->stat.byte_rx += urb->actual_length;
1022 if (btusb_recv_intr(data, urb->transfer_buffer,
1023 urb->actual_length) < 0) {
1024 bt_dev_err(hdev, "corrupted event packet");
1025 hdev->stat.err_rx++;
1027 } else if (urb->status == -ENOENT) {
1028 /* Avoid suspend failed when usb_kill_urb */
1032 if (!test_bit(BTUSB_INTR_RUNNING, &data->flags))
1035 usb_mark_last_busy(data->udev);
1036 usb_anchor_urb(urb, &data->intr_anchor);
1038 err = usb_submit_urb(urb, GFP_ATOMIC);
1040 /* -EPERM: urb is being killed;
1041 * -ENODEV: device got disconnected
1043 if (err != -EPERM && err != -ENODEV)
1044 bt_dev_err(hdev, "urb %p failed to resubmit (%d)",
1047 hci_cmd_sync_cancel(hdev, -err);
1048 usb_unanchor_urb(urb);
1052 static int btusb_submit_intr_urb(struct hci_dev *hdev, gfp_t mem_flags)
1054 struct btusb_data *data = hci_get_drvdata(hdev);
1060 BT_DBG("%s", hdev->name);
1065 urb = usb_alloc_urb(0, mem_flags);
1069 size = le16_to_cpu(data->intr_ep->wMaxPacketSize);
1071 buf = kmalloc(size, mem_flags);
1077 pipe = usb_rcvintpipe(data->udev, data->intr_ep->bEndpointAddress);
1079 usb_fill_int_urb(urb, data->udev, pipe, buf, size,
1080 btusb_intr_complete, hdev, data->intr_ep->bInterval);
1082 urb->transfer_flags |= URB_FREE_BUFFER;
1084 usb_anchor_urb(urb, &data->intr_anchor);
1086 err = usb_submit_urb(urb, mem_flags);
1088 if (err != -EPERM && err != -ENODEV)
1089 bt_dev_err(hdev, "urb %p submission failed (%d)",
1092 hci_cmd_sync_cancel(hdev, -err);
1093 usb_unanchor_urb(urb);
1096 /* Only initialize intr_interval if URB poll sync is enabled */
1097 if (!data->poll_sync)
1100 /* The units are frames (milliseconds) for full and low speed devices,
1101 * and microframes (1/8 millisecond) for highspeed and SuperSpeed
1104 * This is done once on open/resume so it shouldn't change even if
1105 * force_poll_sync changes.
1107 switch (urb->dev->speed) {
1108 case USB_SPEED_SUPER_PLUS:
1109 case USB_SPEED_SUPER: /* units are 125us */
1110 data->intr_interval = usecs_to_jiffies(urb->interval * 125);
1113 data->intr_interval = msecs_to_jiffies(urb->interval);
1123 static void btusb_bulk_complete(struct urb *urb)
1125 struct hci_dev *hdev = urb->context;
1126 struct btusb_data *data = hci_get_drvdata(hdev);
1129 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
1130 urb->actual_length);
1132 if (!test_bit(HCI_RUNNING, &hdev->flags))
1135 if (urb->status == 0) {
1136 hdev->stat.byte_rx += urb->actual_length;
1138 if (data->recv_bulk(data, urb->transfer_buffer,
1139 urb->actual_length) < 0) {
1140 bt_dev_err(hdev, "corrupted ACL packet");
1141 hdev->stat.err_rx++;
1143 } else if (urb->status == -ENOENT) {
1144 /* Avoid suspend failed when usb_kill_urb */
1148 if (!test_bit(BTUSB_BULK_RUNNING, &data->flags))
1151 usb_anchor_urb(urb, &data->bulk_anchor);
1152 usb_mark_last_busy(data->udev);
1154 err = usb_submit_urb(urb, GFP_ATOMIC);
1156 /* -EPERM: urb is being killed;
1157 * -ENODEV: device got disconnected
1159 if (err != -EPERM && err != -ENODEV)
1160 bt_dev_err(hdev, "urb %p failed to resubmit (%d)",
1162 usb_unanchor_urb(urb);
1166 static int btusb_submit_bulk_urb(struct hci_dev *hdev, gfp_t mem_flags)
1168 struct btusb_data *data = hci_get_drvdata(hdev);
1172 int err, size = HCI_MAX_FRAME_SIZE;
1174 BT_DBG("%s", hdev->name);
1176 if (!data->bulk_rx_ep)
1179 urb = usb_alloc_urb(0, mem_flags);
1183 buf = kmalloc(size, mem_flags);
1189 pipe = usb_rcvbulkpipe(data->udev, data->bulk_rx_ep->bEndpointAddress);
1191 usb_fill_bulk_urb(urb, data->udev, pipe, buf, size,
1192 btusb_bulk_complete, hdev);
1194 urb->transfer_flags |= URB_FREE_BUFFER;
1196 usb_mark_last_busy(data->udev);
1197 usb_anchor_urb(urb, &data->bulk_anchor);
1199 err = usb_submit_urb(urb, mem_flags);
1201 if (err != -EPERM && err != -ENODEV)
1202 bt_dev_err(hdev, "urb %p submission failed (%d)",
1204 usb_unanchor_urb(urb);
1212 static void btusb_isoc_complete(struct urb *urb)
1214 struct hci_dev *hdev = urb->context;
1215 struct btusb_data *data = hci_get_drvdata(hdev);
1218 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
1219 urb->actual_length);
1221 if (!test_bit(HCI_RUNNING, &hdev->flags))
1224 if (urb->status == 0) {
1225 for (i = 0; i < urb->number_of_packets; i++) {
1226 unsigned int offset = urb->iso_frame_desc[i].offset;
1227 unsigned int length = urb->iso_frame_desc[i].actual_length;
1229 if (urb->iso_frame_desc[i].status)
1232 hdev->stat.byte_rx += length;
1234 if (btusb_recv_isoc(data, urb->transfer_buffer + offset,
1236 bt_dev_err(hdev, "corrupted SCO packet");
1237 hdev->stat.err_rx++;
1240 } else if (urb->status == -ENOENT) {
1241 /* Avoid suspend failed when usb_kill_urb */
1245 if (!test_bit(BTUSB_ISOC_RUNNING, &data->flags))
1248 usb_anchor_urb(urb, &data->isoc_anchor);
1250 err = usb_submit_urb(urb, GFP_ATOMIC);
1252 /* -EPERM: urb is being killed;
1253 * -ENODEV: device got disconnected
1255 if (err != -EPERM && err != -ENODEV)
1256 bt_dev_err(hdev, "urb %p failed to resubmit (%d)",
1258 usb_unanchor_urb(urb);
1262 static inline void __fill_isoc_descriptor_msbc(struct urb *urb, int len,
1263 int mtu, struct btusb_data *data)
1266 unsigned int interval;
1268 BT_DBG("len %d mtu %d", len, mtu);
1270 /* For mSBC ALT 6 setting the host will send the packet at continuous
1271 * flow. As per core spec 5, vol 4, part B, table 2.1. For ALT setting
1272 * 6 the HCI PACKET INTERVAL should be 7.5ms for every usb packets.
1273 * To maintain the rate we send 63bytes of usb packets alternatively for
1274 * 7ms and 8ms to maintain the rate as 7.5ms.
1276 if (data->usb_alt6_packet_flow) {
1278 data->usb_alt6_packet_flow = false;
1281 data->usb_alt6_packet_flow = true;
1284 for (i = 0; i < interval; i++) {
1285 urb->iso_frame_desc[i].offset = offset;
1286 urb->iso_frame_desc[i].length = offset;
1289 if (len && i < BTUSB_MAX_ISOC_FRAMES) {
1290 urb->iso_frame_desc[i].offset = offset;
1291 urb->iso_frame_desc[i].length = len;
1295 urb->number_of_packets = i;
1298 static inline void __fill_isoc_descriptor(struct urb *urb, int len, int mtu)
1302 BT_DBG("len %d mtu %d", len, mtu);
1304 for (i = 0; i < BTUSB_MAX_ISOC_FRAMES && len >= mtu;
1305 i++, offset += mtu, len -= mtu) {
1306 urb->iso_frame_desc[i].offset = offset;
1307 urb->iso_frame_desc[i].length = mtu;
1310 if (len && i < BTUSB_MAX_ISOC_FRAMES) {
1311 urb->iso_frame_desc[i].offset = offset;
1312 urb->iso_frame_desc[i].length = len;
1316 urb->number_of_packets = i;
1319 static int btusb_submit_isoc_urb(struct hci_dev *hdev, gfp_t mem_flags)
1321 struct btusb_data *data = hci_get_drvdata(hdev);
1327 BT_DBG("%s", hdev->name);
1329 if (!data->isoc_rx_ep)
1332 urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, mem_flags);
1336 size = le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize) *
1337 BTUSB_MAX_ISOC_FRAMES;
1339 buf = kmalloc(size, mem_flags);
1345 pipe = usb_rcvisocpipe(data->udev, data->isoc_rx_ep->bEndpointAddress);
1347 usb_fill_int_urb(urb, data->udev, pipe, buf, size, btusb_isoc_complete,
1348 hdev, data->isoc_rx_ep->bInterval);
1350 urb->transfer_flags = URB_FREE_BUFFER | URB_ISO_ASAP;
1352 __fill_isoc_descriptor(urb, size,
1353 le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize));
1355 usb_anchor_urb(urb, &data->isoc_anchor);
1357 err = usb_submit_urb(urb, mem_flags);
1359 if (err != -EPERM && err != -ENODEV)
1360 bt_dev_err(hdev, "urb %p submission failed (%d)",
1362 usb_unanchor_urb(urb);
1370 static void btusb_diag_complete(struct urb *urb)
1372 struct hci_dev *hdev = urb->context;
1373 struct btusb_data *data = hci_get_drvdata(hdev);
1376 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
1377 urb->actual_length);
1379 if (urb->status == 0) {
1380 struct sk_buff *skb;
1382 skb = bt_skb_alloc(urb->actual_length, GFP_ATOMIC);
1384 skb_put_data(skb, urb->transfer_buffer,
1385 urb->actual_length);
1386 hci_recv_diag(hdev, skb);
1388 } else if (urb->status == -ENOENT) {
1389 /* Avoid suspend failed when usb_kill_urb */
1393 if (!test_bit(BTUSB_DIAG_RUNNING, &data->flags))
1396 usb_anchor_urb(urb, &data->diag_anchor);
1397 usb_mark_last_busy(data->udev);
1399 err = usb_submit_urb(urb, GFP_ATOMIC);
1401 /* -EPERM: urb is being killed;
1402 * -ENODEV: device got disconnected
1404 if (err != -EPERM && err != -ENODEV)
1405 bt_dev_err(hdev, "urb %p failed to resubmit (%d)",
1407 usb_unanchor_urb(urb);
1411 static int btusb_submit_diag_urb(struct hci_dev *hdev, gfp_t mem_flags)
1413 struct btusb_data *data = hci_get_drvdata(hdev);
1417 int err, size = HCI_MAX_FRAME_SIZE;
1419 BT_DBG("%s", hdev->name);
1421 if (!data->diag_rx_ep)
1424 urb = usb_alloc_urb(0, mem_flags);
1428 buf = kmalloc(size, mem_flags);
1434 pipe = usb_rcvbulkpipe(data->udev, data->diag_rx_ep->bEndpointAddress);
1436 usb_fill_bulk_urb(urb, data->udev, pipe, buf, size,
1437 btusb_diag_complete, hdev);
1439 urb->transfer_flags |= URB_FREE_BUFFER;
1441 usb_mark_last_busy(data->udev);
1442 usb_anchor_urb(urb, &data->diag_anchor);
1444 err = usb_submit_urb(urb, mem_flags);
1446 if (err != -EPERM && err != -ENODEV)
1447 bt_dev_err(hdev, "urb %p submission failed (%d)",
1449 usb_unanchor_urb(urb);
1457 static void btusb_tx_complete(struct urb *urb)
1459 struct sk_buff *skb = urb->context;
1460 struct hci_dev *hdev = (struct hci_dev *)skb->dev;
1461 struct btusb_data *data = hci_get_drvdata(hdev);
1462 unsigned long flags;
1464 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
1465 urb->actual_length);
1467 if (!test_bit(HCI_RUNNING, &hdev->flags))
1471 hdev->stat.byte_tx += urb->transfer_buffer_length;
1473 if (hci_skb_pkt_type(skb) == HCI_COMMAND_PKT)
1474 hci_cmd_sync_cancel(hdev, -urb->status);
1475 hdev->stat.err_tx++;
1479 spin_lock_irqsave(&data->txlock, flags);
1480 data->tx_in_flight--;
1481 spin_unlock_irqrestore(&data->txlock, flags);
1483 kfree(urb->setup_packet);
1488 static void btusb_isoc_tx_complete(struct urb *urb)
1490 struct sk_buff *skb = urb->context;
1491 struct hci_dev *hdev = (struct hci_dev *)skb->dev;
1493 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
1494 urb->actual_length);
1496 if (!test_bit(HCI_RUNNING, &hdev->flags))
1500 hdev->stat.byte_tx += urb->transfer_buffer_length;
1502 hdev->stat.err_tx++;
1505 kfree(urb->setup_packet);
1510 static int btusb_open(struct hci_dev *hdev)
1512 struct btusb_data *data = hci_get_drvdata(hdev);
1515 BT_DBG("%s", hdev->name);
1517 err = usb_autopm_get_interface(data->intf);
1521 /* Patching USB firmware files prior to starting any URBs of HCI path
1522 * It is more safe to use USB bulk channel for downloading USB patch
1524 if (data->setup_on_usb) {
1525 err = data->setup_on_usb(hdev);
1530 data->intf->needs_remote_wakeup = 1;
1532 if (test_and_set_bit(BTUSB_INTR_RUNNING, &data->flags))
1535 err = btusb_submit_intr_urb(hdev, GFP_KERNEL);
1539 err = btusb_submit_bulk_urb(hdev, GFP_KERNEL);
1541 usb_kill_anchored_urbs(&data->intr_anchor);
1545 set_bit(BTUSB_BULK_RUNNING, &data->flags);
1546 btusb_submit_bulk_urb(hdev, GFP_KERNEL);
1549 if (!btusb_submit_diag_urb(hdev, GFP_KERNEL))
1550 set_bit(BTUSB_DIAG_RUNNING, &data->flags);
1554 usb_autopm_put_interface(data->intf);
1558 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
1560 usb_autopm_put_interface(data->intf);
1564 static void btusb_stop_traffic(struct btusb_data *data)
1566 usb_kill_anchored_urbs(&data->intr_anchor);
1567 usb_kill_anchored_urbs(&data->bulk_anchor);
1568 usb_kill_anchored_urbs(&data->isoc_anchor);
1569 usb_kill_anchored_urbs(&data->diag_anchor);
1570 usb_kill_anchored_urbs(&data->ctrl_anchor);
1573 static int btusb_close(struct hci_dev *hdev)
1575 struct btusb_data *data = hci_get_drvdata(hdev);
1578 BT_DBG("%s", hdev->name);
1580 cancel_delayed_work(&data->rx_work);
1581 cancel_work_sync(&data->work);
1582 cancel_work_sync(&data->waker);
1584 skb_queue_purge(&data->acl_q);
1586 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1587 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
1588 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
1589 clear_bit(BTUSB_DIAG_RUNNING, &data->flags);
1591 btusb_stop_traffic(data);
1592 btusb_free_frags(data);
1594 err = usb_autopm_get_interface(data->intf);
1598 data->intf->needs_remote_wakeup = 0;
1600 /* Enable remote wake up for auto-suspend */
1601 if (test_bit(BTUSB_WAKEUP_AUTOSUSPEND, &data->flags))
1602 data->intf->needs_remote_wakeup = 1;
1604 usb_autopm_put_interface(data->intf);
1607 usb_scuttle_anchored_urbs(&data->deferred);
1611 static int btusb_flush(struct hci_dev *hdev)
1613 struct btusb_data *data = hci_get_drvdata(hdev);
1615 BT_DBG("%s", hdev->name);
1617 cancel_delayed_work(&data->rx_work);
1619 skb_queue_purge(&data->acl_q);
1621 usb_kill_anchored_urbs(&data->tx_anchor);
1622 btusb_free_frags(data);
1627 static struct urb *alloc_ctrl_urb(struct hci_dev *hdev, struct sk_buff *skb)
1629 struct btusb_data *data = hci_get_drvdata(hdev);
1630 struct usb_ctrlrequest *dr;
1634 urb = usb_alloc_urb(0, GFP_KERNEL);
1636 return ERR_PTR(-ENOMEM);
1638 dr = kmalloc(sizeof(*dr), GFP_KERNEL);
1641 return ERR_PTR(-ENOMEM);
1644 dr->bRequestType = data->cmdreq_type;
1645 dr->bRequest = data->cmdreq;
1648 dr->wLength = __cpu_to_le16(skb->len);
1650 pipe = usb_sndctrlpipe(data->udev, 0x00);
1652 usb_fill_control_urb(urb, data->udev, pipe, (void *)dr,
1653 skb->data, skb->len, btusb_tx_complete, skb);
1655 skb->dev = (void *)hdev;
1660 static struct urb *alloc_bulk_urb(struct hci_dev *hdev, struct sk_buff *skb)
1662 struct btusb_data *data = hci_get_drvdata(hdev);
1666 if (!data->bulk_tx_ep)
1667 return ERR_PTR(-ENODEV);
1669 urb = usb_alloc_urb(0, GFP_KERNEL);
1671 return ERR_PTR(-ENOMEM);
1673 pipe = usb_sndbulkpipe(data->udev, data->bulk_tx_ep->bEndpointAddress);
1675 usb_fill_bulk_urb(urb, data->udev, pipe,
1676 skb->data, skb->len, btusb_tx_complete, skb);
1678 skb->dev = (void *)hdev;
1683 static struct urb *alloc_isoc_urb(struct hci_dev *hdev, struct sk_buff *skb)
1685 struct btusb_data *data = hci_get_drvdata(hdev);
1689 if (!data->isoc_tx_ep)
1690 return ERR_PTR(-ENODEV);
1692 urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, GFP_KERNEL);
1694 return ERR_PTR(-ENOMEM);
1696 pipe = usb_sndisocpipe(data->udev, data->isoc_tx_ep->bEndpointAddress);
1698 usb_fill_int_urb(urb, data->udev, pipe,
1699 skb->data, skb->len, btusb_isoc_tx_complete,
1700 skb, data->isoc_tx_ep->bInterval);
1702 urb->transfer_flags = URB_ISO_ASAP;
1704 if (data->isoc_altsetting == 6)
1705 __fill_isoc_descriptor_msbc(urb, skb->len,
1706 le16_to_cpu(data->isoc_tx_ep->wMaxPacketSize),
1709 __fill_isoc_descriptor(urb, skb->len,
1710 le16_to_cpu(data->isoc_tx_ep->wMaxPacketSize));
1711 skb->dev = (void *)hdev;
1716 static int submit_tx_urb(struct hci_dev *hdev, struct urb *urb)
1718 struct btusb_data *data = hci_get_drvdata(hdev);
1721 usb_anchor_urb(urb, &data->tx_anchor);
1723 err = usb_submit_urb(urb, GFP_KERNEL);
1725 if (err != -EPERM && err != -ENODEV)
1726 bt_dev_err(hdev, "urb %p submission failed (%d)",
1728 kfree(urb->setup_packet);
1729 usb_unanchor_urb(urb);
1731 usb_mark_last_busy(data->udev);
1738 static int submit_or_queue_tx_urb(struct hci_dev *hdev, struct urb *urb)
1740 struct btusb_data *data = hci_get_drvdata(hdev);
1741 unsigned long flags;
1744 spin_lock_irqsave(&data->txlock, flags);
1745 suspending = test_bit(BTUSB_SUSPENDING, &data->flags);
1747 data->tx_in_flight++;
1748 spin_unlock_irqrestore(&data->txlock, flags);
1751 return submit_tx_urb(hdev, urb);
1753 usb_anchor_urb(urb, &data->deferred);
1754 schedule_work(&data->waker);
1760 static int btusb_send_frame(struct hci_dev *hdev, struct sk_buff *skb)
1764 BT_DBG("%s", hdev->name);
1766 switch (hci_skb_pkt_type(skb)) {
1767 case HCI_COMMAND_PKT:
1768 urb = alloc_ctrl_urb(hdev, skb);
1770 return PTR_ERR(urb);
1772 hdev->stat.cmd_tx++;
1773 return submit_or_queue_tx_urb(hdev, urb);
1775 case HCI_ACLDATA_PKT:
1776 urb = alloc_bulk_urb(hdev, skb);
1778 return PTR_ERR(urb);
1780 hdev->stat.acl_tx++;
1781 return submit_or_queue_tx_urb(hdev, urb);
1783 case HCI_SCODATA_PKT:
1784 if (hci_conn_num(hdev, SCO_LINK) < 1)
1787 urb = alloc_isoc_urb(hdev, skb);
1789 return PTR_ERR(urb);
1791 hdev->stat.sco_tx++;
1792 return submit_tx_urb(hdev, urb);
1794 case HCI_ISODATA_PKT:
1795 urb = alloc_bulk_urb(hdev, skb);
1797 return PTR_ERR(urb);
1799 return submit_or_queue_tx_urb(hdev, urb);
1805 static void btusb_notify(struct hci_dev *hdev, unsigned int evt)
1807 struct btusb_data *data = hci_get_drvdata(hdev);
1809 BT_DBG("%s evt %d", hdev->name, evt);
1811 if (hci_conn_num(hdev, SCO_LINK) != data->sco_num) {
1812 data->sco_num = hci_conn_num(hdev, SCO_LINK);
1813 data->air_mode = evt;
1814 schedule_work(&data->work);
1818 static inline int __set_isoc_interface(struct hci_dev *hdev, int altsetting)
1820 struct btusb_data *data = hci_get_drvdata(hdev);
1821 struct usb_interface *intf = data->isoc;
1822 struct usb_endpoint_descriptor *ep_desc;
1828 err = usb_set_interface(data->udev, data->isoc_ifnum, altsetting);
1830 bt_dev_err(hdev, "setting interface failed (%d)", -err);
1834 data->isoc_altsetting = altsetting;
1836 data->isoc_tx_ep = NULL;
1837 data->isoc_rx_ep = NULL;
1839 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
1840 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
1842 if (!data->isoc_tx_ep && usb_endpoint_is_isoc_out(ep_desc)) {
1843 data->isoc_tx_ep = ep_desc;
1847 if (!data->isoc_rx_ep && usb_endpoint_is_isoc_in(ep_desc)) {
1848 data->isoc_rx_ep = ep_desc;
1853 if (!data->isoc_tx_ep || !data->isoc_rx_ep) {
1854 bt_dev_err(hdev, "invalid SCO descriptors");
1861 static int btusb_switch_alt_setting(struct hci_dev *hdev, int new_alts)
1863 struct btusb_data *data = hci_get_drvdata(hdev);
1866 if (data->isoc_altsetting != new_alts) {
1867 unsigned long flags;
1869 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1870 usb_kill_anchored_urbs(&data->isoc_anchor);
1872 /* When isochronous alternate setting needs to be
1873 * changed, because SCO connection has been added
1874 * or removed, a packet fragment may be left in the
1875 * reassembling state. This could lead to wrongly
1876 * assembled fragments.
1878 * Clear outstanding fragment when selecting a new
1879 * alternate setting.
1881 spin_lock_irqsave(&data->rxlock, flags);
1882 kfree_skb(data->sco_skb);
1883 data->sco_skb = NULL;
1884 spin_unlock_irqrestore(&data->rxlock, flags);
1886 err = __set_isoc_interface(hdev, new_alts);
1891 if (!test_and_set_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
1892 if (btusb_submit_isoc_urb(hdev, GFP_KERNEL) < 0)
1893 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1895 btusb_submit_isoc_urb(hdev, GFP_KERNEL);
1901 static struct usb_host_interface *btusb_find_altsetting(struct btusb_data *data,
1904 struct usb_interface *intf = data->isoc;
1907 BT_DBG("Looking for Alt no :%d", alt);
1912 for (i = 0; i < intf->num_altsetting; i++) {
1913 if (intf->altsetting[i].desc.bAlternateSetting == alt)
1914 return &intf->altsetting[i];
1920 static void btusb_work(struct work_struct *work)
1922 struct btusb_data *data = container_of(work, struct btusb_data, work);
1923 struct hci_dev *hdev = data->hdev;
1927 if (data->sco_num > 0) {
1928 if (!test_bit(BTUSB_DID_ISO_RESUME, &data->flags)) {
1929 err = usb_autopm_get_interface(data->isoc ? data->isoc : data->intf);
1931 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1932 usb_kill_anchored_urbs(&data->isoc_anchor);
1936 set_bit(BTUSB_DID_ISO_RESUME, &data->flags);
1939 if (data->air_mode == HCI_NOTIFY_ENABLE_SCO_CVSD) {
1940 if (hdev->voice_setting & 0x0020) {
1941 static const int alts[3] = { 2, 4, 5 };
1943 new_alts = alts[data->sco_num - 1];
1945 new_alts = data->sco_num;
1947 } else if (data->air_mode == HCI_NOTIFY_ENABLE_SCO_TRANSP) {
1948 /* Bluetooth USB spec recommends alt 6 (63 bytes), but
1949 * many adapters do not support it. Alt 1 appears to
1950 * work for all adapters that do not have alt 6, and
1951 * which work with WBS at all. Some devices prefer
1952 * alt 3 (HCI payload >= 60 Bytes let air packet
1953 * data satisfy 60 bytes), requiring
1954 * MTU >= 3 (packets) * 25 (size) - 3 (headers) = 72
1955 * see also Core spec 5, vol 4, B 2.1.1 & Table 2.1.
1957 if (btusb_find_altsetting(data, 6))
1959 else if (btusb_find_altsetting(data, 3) &&
1960 hdev->sco_mtu >= 72 &&
1961 test_bit(BTUSB_USE_ALT3_FOR_WBS, &data->flags))
1967 if (btusb_switch_alt_setting(hdev, new_alts) < 0)
1968 bt_dev_err(hdev, "set USB alt:(%d) failed!", new_alts);
1970 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1971 usb_kill_anchored_urbs(&data->isoc_anchor);
1973 __set_isoc_interface(hdev, 0);
1974 if (test_and_clear_bit(BTUSB_DID_ISO_RESUME, &data->flags))
1975 usb_autopm_put_interface(data->isoc ? data->isoc : data->intf);
1979 static void btusb_waker(struct work_struct *work)
1981 struct btusb_data *data = container_of(work, struct btusb_data, waker);
1984 err = usb_autopm_get_interface(data->intf);
1988 usb_autopm_put_interface(data->intf);
1991 static void btusb_rx_work(struct work_struct *work)
1993 struct btusb_data *data = container_of(work, struct btusb_data,
1995 struct sk_buff *skb;
1997 /* Dequeue ACL data received during the interval */
1998 while ((skb = skb_dequeue(&data->acl_q)))
1999 data->recv_acl(data->hdev, skb);
2002 static int btusb_setup_bcm92035(struct hci_dev *hdev)
2004 struct sk_buff *skb;
2007 BT_DBG("%s", hdev->name);
2009 skb = __hci_cmd_sync(hdev, 0xfc3b, 1, &val, HCI_INIT_TIMEOUT);
2011 bt_dev_err(hdev, "BCM92035 command failed (%ld)", PTR_ERR(skb));
2018 static int btusb_setup_csr(struct hci_dev *hdev)
2020 struct btusb_data *data = hci_get_drvdata(hdev);
2021 u16 bcdDevice = le16_to_cpu(data->udev->descriptor.bcdDevice);
2022 struct hci_rp_read_local_version *rp;
2023 struct sk_buff *skb;
2024 bool is_fake = false;
2027 BT_DBG("%s", hdev->name);
2029 skb = __hci_cmd_sync(hdev, HCI_OP_READ_LOCAL_VERSION, 0, NULL,
2032 int err = PTR_ERR(skb);
2033 bt_dev_err(hdev, "CSR: Local version failed (%d)", err);
2037 if (skb->len != sizeof(struct hci_rp_read_local_version)) {
2038 bt_dev_err(hdev, "CSR: Local version length mismatch");
2043 rp = (struct hci_rp_read_local_version *)skb->data;
2045 /* Detect a wide host of Chinese controllers that aren't CSR.
2047 * Known fake bcdDevices: 0x0100, 0x0134, 0x1915, 0x2520, 0x7558, 0x8891
2049 * The main thing they have in common is that these are really popular low-cost
2050 * options that support newer Bluetooth versions but rely on heavy VID/PID
2051 * squatting of this poor old Bluetooth 1.1 device. Even sold as such.
2053 * We detect actual CSR devices by checking that the HCI manufacturer code
2054 * is Cambridge Silicon Radio (10) and ensuring that LMP sub-version and
2055 * HCI rev values always match. As they both store the firmware number.
2057 if (le16_to_cpu(rp->manufacturer) != 10 ||
2058 le16_to_cpu(rp->hci_rev) != le16_to_cpu(rp->lmp_subver))
2061 /* Known legit CSR firmware build numbers and their supported BT versions:
2062 * - 1.1 (0x1) -> 0x0073, 0x020d, 0x033c, 0x034e
2063 * - 1.2 (0x2) -> 0x04d9, 0x0529
2064 * - 2.0 (0x3) -> 0x07a6, 0x07ad, 0x0c5c
2065 * - 2.1 (0x4) -> 0x149c, 0x1735, 0x1899 (0x1899 is a BlueCore4-External)
2066 * - 4.0 (0x6) -> 0x1d86, 0x2031, 0x22bb
2068 * e.g. Real CSR dongles with LMP subversion 0x73 are old enough that
2069 * support BT 1.1 only; so it's a dead giveaway when some
2070 * third-party BT 4.0 dongle reuses it.
2072 else if (le16_to_cpu(rp->lmp_subver) <= 0x034e &&
2073 le16_to_cpu(rp->hci_ver) > BLUETOOTH_VER_1_1)
2076 else if (le16_to_cpu(rp->lmp_subver) <= 0x0529 &&
2077 le16_to_cpu(rp->hci_ver) > BLUETOOTH_VER_1_2)
2080 else if (le16_to_cpu(rp->lmp_subver) <= 0x0c5c &&
2081 le16_to_cpu(rp->hci_ver) > BLUETOOTH_VER_2_0)
2084 else if (le16_to_cpu(rp->lmp_subver) <= 0x1899 &&
2085 le16_to_cpu(rp->hci_ver) > BLUETOOTH_VER_2_1)
2088 else if (le16_to_cpu(rp->lmp_subver) <= 0x22bb &&
2089 le16_to_cpu(rp->hci_ver) > BLUETOOTH_VER_4_0)
2092 /* Other clones which beat all the above checks */
2093 else if (bcdDevice == 0x0134 &&
2094 le16_to_cpu(rp->lmp_subver) == 0x0c5c &&
2095 le16_to_cpu(rp->hci_ver) == BLUETOOTH_VER_2_0)
2099 bt_dev_warn(hdev, "CSR: Unbranded CSR clone detected; adding workarounds and force-suspending once...");
2101 /* Generally these clones have big discrepancies between
2102 * advertised features and what's actually supported.
2103 * Probably will need to be expanded in the future;
2104 * without these the controller will lock up.
2106 set_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks);
2107 set_bit(HCI_QUIRK_BROKEN_FILTER_CLEAR_ALL, &hdev->quirks);
2108 set_bit(HCI_QUIRK_NO_SUSPEND_NOTIFIER, &hdev->quirks);
2110 /* Clear the reset quirk since this is not an actual
2111 * early Bluetooth 1.1 device from CSR.
2113 clear_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
2114 clear_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
2117 * Special workaround for these BT 4.0 chip clones, and potentially more:
2119 * - 0x0134: a Barrot 8041a02 (HCI rev: 0x0810 sub: 0x1012)
2120 * - 0x7558: IC markings FR3191AHAL 749H15143 (HCI rev/sub-version: 0x0709)
2122 * These controllers are really messed-up.
2124 * 1. Their bulk RX endpoint will never report any data unless
2125 * the device was suspended at least once (yes, really).
2126 * 2. They will not wakeup when autosuspended and receiving data
2127 * on their bulk RX endpoint from e.g. a keyboard or mouse
2128 * (IOW remote-wakeup support is broken for the bulk endpoint).
2130 * To fix 1. enable runtime-suspend, force-suspend the
2131 * HCI and then wake-it up by disabling runtime-suspend.
2133 * To fix 2. clear the HCI's can_wake flag, this way the HCI
2134 * will still be autosuspended when it is not open.
2138 * Because these are widespread problems we prefer generic solutions; so
2139 * apply this initialization quirk to every controller that gets here,
2140 * it should be harmless. The alternative is to not work at all.
2142 pm_runtime_allow(&data->udev->dev);
2144 ret = pm_runtime_suspend(&data->udev->dev);
2148 bt_dev_warn(hdev, "CSR: Couldn't suspend the device for our Barrot 8041a02 receive-issue workaround");
2150 pm_runtime_forbid(&data->udev->dev);
2152 device_set_wakeup_capable(&data->udev->dev, false);
2154 /* Re-enable autosuspend if this was requested */
2155 if (enable_autosuspend)
2156 usb_enable_autosuspend(data->udev);
2164 static int inject_cmd_complete(struct hci_dev *hdev, __u16 opcode)
2166 struct sk_buff *skb;
2167 struct hci_event_hdr *hdr;
2168 struct hci_ev_cmd_complete *evt;
2170 skb = bt_skb_alloc(sizeof(*hdr) + sizeof(*evt) + 1, GFP_KERNEL);
2174 hdr = skb_put(skb, sizeof(*hdr));
2175 hdr->evt = HCI_EV_CMD_COMPLETE;
2176 hdr->plen = sizeof(*evt) + 1;
2178 evt = skb_put(skb, sizeof(*evt));
2180 evt->opcode = cpu_to_le16(opcode);
2182 skb_put_u8(skb, 0x00);
2184 hci_skb_pkt_type(skb) = HCI_EVENT_PKT;
2186 return hci_recv_frame(hdev, skb);
2189 static int btusb_recv_bulk_intel(struct btusb_data *data, void *buffer,
2192 struct hci_dev *hdev = data->hdev;
2194 /* When the device is in bootloader mode, then it can send
2195 * events via the bulk endpoint. These events are treated the
2196 * same way as the ones received from the interrupt endpoint.
2198 if (btintel_test_flag(hdev, INTEL_BOOTLOADER))
2199 return btusb_recv_intr(data, buffer, count);
2201 return btusb_recv_bulk(data, buffer, count);
2204 static int btusb_recv_event_intel(struct hci_dev *hdev, struct sk_buff *skb)
2206 if (btintel_test_flag(hdev, INTEL_BOOTLOADER)) {
2207 struct hci_event_hdr *hdr = (void *)skb->data;
2209 if (skb->len > HCI_EVENT_HDR_SIZE && hdr->evt == 0xff &&
2211 const void *ptr = skb->data + HCI_EVENT_HDR_SIZE + 1;
2212 unsigned int len = skb->len - HCI_EVENT_HDR_SIZE - 1;
2214 switch (skb->data[2]) {
2216 /* When switching to the operational firmware
2217 * the device sends a vendor specific event
2218 * indicating that the bootup completed.
2220 btintel_bootup(hdev, ptr, len);
2223 /* When the firmware loading completes the
2224 * device sends out a vendor specific event
2225 * indicating the result of the firmware
2228 btintel_secure_send_result(hdev, ptr, len);
2234 return hci_recv_frame(hdev, skb);
2237 static int btusb_send_frame_intel(struct hci_dev *hdev, struct sk_buff *skb)
2241 BT_DBG("%s", hdev->name);
2243 switch (hci_skb_pkt_type(skb)) {
2244 case HCI_COMMAND_PKT:
2245 if (btintel_test_flag(hdev, INTEL_BOOTLOADER)) {
2246 struct hci_command_hdr *cmd = (void *)skb->data;
2247 __u16 opcode = le16_to_cpu(cmd->opcode);
2249 /* When in bootloader mode and the command 0xfc09
2250 * is received, it needs to be send down the
2251 * bulk endpoint. So allocate a bulk URB instead.
2253 if (opcode == 0xfc09)
2254 urb = alloc_bulk_urb(hdev, skb);
2256 urb = alloc_ctrl_urb(hdev, skb);
2258 /* When the 0xfc01 command is issued to boot into
2259 * the operational firmware, it will actually not
2260 * send a command complete event. To keep the flow
2261 * control working inject that event here.
2263 if (opcode == 0xfc01)
2264 inject_cmd_complete(hdev, opcode);
2266 urb = alloc_ctrl_urb(hdev, skb);
2269 return PTR_ERR(urb);
2271 hdev->stat.cmd_tx++;
2272 return submit_or_queue_tx_urb(hdev, urb);
2274 case HCI_ACLDATA_PKT:
2275 urb = alloc_bulk_urb(hdev, skb);
2277 return PTR_ERR(urb);
2279 hdev->stat.acl_tx++;
2280 return submit_or_queue_tx_urb(hdev, urb);
2282 case HCI_SCODATA_PKT:
2283 if (hci_conn_num(hdev, SCO_LINK) < 1)
2286 urb = alloc_isoc_urb(hdev, skb);
2288 return PTR_ERR(urb);
2290 hdev->stat.sco_tx++;
2291 return submit_tx_urb(hdev, urb);
2293 case HCI_ISODATA_PKT:
2294 urb = alloc_bulk_urb(hdev, skb);
2296 return PTR_ERR(urb);
2298 return submit_or_queue_tx_urb(hdev, urb);
2304 /* UHW CR mapping */
2305 #define MTK_BT_MISC 0x70002510
2306 #define MTK_BT_SUBSYS_RST 0x70002610
2307 #define MTK_UDMA_INT_STA_BT 0x74000024
2308 #define MTK_UDMA_INT_STA_BT1 0x74000308
2309 #define MTK_BT_WDT_STATUS 0x740003A0
2310 #define MTK_EP_RST_OPT 0x74011890
2311 #define MTK_EP_RST_IN_OUT_OPT 0x00010001
2312 #define MTK_BT_RST_DONE 0x00000100
2313 #define MTK_BT_RESET_WAIT_MS 100
2314 #define MTK_BT_RESET_NUM_TRIES 10
2316 static void btusb_mtk_wmt_recv(struct urb *urb)
2318 struct hci_dev *hdev = urb->context;
2319 struct btusb_data *data = hci_get_drvdata(hdev);
2320 struct sk_buff *skb;
2323 if (urb->status == 0 && urb->actual_length > 0) {
2324 hdev->stat.byte_rx += urb->actual_length;
2326 /* WMT event shouldn't be fragmented and the size should be
2327 * less than HCI_WMT_MAX_EVENT_SIZE.
2329 skb = bt_skb_alloc(HCI_WMT_MAX_EVENT_SIZE, GFP_ATOMIC);
2331 hdev->stat.err_rx++;
2332 kfree(urb->setup_packet);
2336 hci_skb_pkt_type(skb) = HCI_EVENT_PKT;
2337 skb_put_data(skb, urb->transfer_buffer, urb->actual_length);
2339 /* When someone waits for the WMT event, the skb is being cloned
2340 * and being processed the events from there then.
2342 if (test_bit(BTUSB_TX_WAIT_VND_EVT, &data->flags)) {
2343 data->evt_skb = skb_clone(skb, GFP_ATOMIC);
2344 if (!data->evt_skb) {
2346 kfree(urb->setup_packet);
2351 err = hci_recv_frame(hdev, skb);
2353 kfree_skb(data->evt_skb);
2354 data->evt_skb = NULL;
2355 kfree(urb->setup_packet);
2359 if (test_and_clear_bit(BTUSB_TX_WAIT_VND_EVT,
2361 /* Barrier to sync with other CPUs */
2362 smp_mb__after_atomic();
2363 wake_up_bit(&data->flags,
2364 BTUSB_TX_WAIT_VND_EVT);
2366 kfree(urb->setup_packet);
2368 } else if (urb->status == -ENOENT) {
2369 /* Avoid suspend failed when usb_kill_urb */
2373 usb_mark_last_busy(data->udev);
2375 /* The URB complete handler is still called with urb->actual_length = 0
2376 * when the event is not available, so we should keep re-submitting
2377 * URB until WMT event returns, Also, It's necessary to wait some time
2378 * between the two consecutive control URBs to relax the target device
2379 * to generate the event. Otherwise, the WMT event cannot return from
2380 * the device successfully.
2384 usb_anchor_urb(urb, &data->ctrl_anchor);
2385 err = usb_submit_urb(urb, GFP_ATOMIC);
2387 kfree(urb->setup_packet);
2388 /* -EPERM: urb is being killed;
2389 * -ENODEV: device got disconnected
2391 if (err != -EPERM && err != -ENODEV)
2392 bt_dev_err(hdev, "urb %p failed to resubmit (%d)",
2394 usb_unanchor_urb(urb);
2398 static int btusb_mtk_submit_wmt_recv_urb(struct hci_dev *hdev)
2400 struct btusb_data *data = hci_get_drvdata(hdev);
2401 struct usb_ctrlrequest *dr;
2407 urb = usb_alloc_urb(0, GFP_KERNEL);
2411 dr = kmalloc(sizeof(*dr), GFP_KERNEL);
2417 dr->bRequestType = USB_TYPE_VENDOR | USB_DIR_IN;
2419 dr->wIndex = cpu_to_le16(0);
2420 dr->wValue = cpu_to_le16(48);
2421 dr->wLength = cpu_to_le16(size);
2423 buf = kmalloc(size, GFP_KERNEL);
2430 pipe = usb_rcvctrlpipe(data->udev, 0);
2432 usb_fill_control_urb(urb, data->udev, pipe, (void *)dr,
2433 buf, size, btusb_mtk_wmt_recv, hdev);
2435 urb->transfer_flags |= URB_FREE_BUFFER;
2437 usb_anchor_urb(urb, &data->ctrl_anchor);
2438 err = usb_submit_urb(urb, GFP_KERNEL);
2440 if (err != -EPERM && err != -ENODEV)
2441 bt_dev_err(hdev, "urb %p submission failed (%d)",
2443 usb_unanchor_urb(urb);
2451 static int btusb_mtk_hci_wmt_sync(struct hci_dev *hdev,
2452 struct btmtk_hci_wmt_params *wmt_params)
2454 struct btusb_data *data = hci_get_drvdata(hdev);
2455 struct btmtk_hci_wmt_evt_funcc *wmt_evt_funcc;
2456 u32 hlen, status = BTMTK_WMT_INVALID;
2457 struct btmtk_hci_wmt_evt *wmt_evt;
2458 struct btmtk_hci_wmt_cmd *wc;
2459 struct btmtk_wmt_hdr *hdr;
2462 /* Send the WMT command and wait until the WMT event returns */
2463 hlen = sizeof(*hdr) + wmt_params->dlen;
2467 wc = kzalloc(hlen, GFP_KERNEL);
2473 hdr->op = wmt_params->op;
2474 hdr->dlen = cpu_to_le16(wmt_params->dlen + 1);
2475 hdr->flag = wmt_params->flag;
2476 memcpy(wc->data, wmt_params->data, wmt_params->dlen);
2478 set_bit(BTUSB_TX_WAIT_VND_EVT, &data->flags);
2480 err = __hci_cmd_send(hdev, 0xfc6f, hlen, wc);
2483 clear_bit(BTUSB_TX_WAIT_VND_EVT, &data->flags);
2487 /* Submit control IN URB on demand to process the WMT event */
2488 err = btusb_mtk_submit_wmt_recv_urb(hdev);
2492 /* The vendor specific WMT commands are all answered by a vendor
2493 * specific event and will have the Command Status or Command
2494 * Complete as with usual HCI command flow control.
2496 * After sending the command, wait for BTUSB_TX_WAIT_VND_EVT
2497 * state to be cleared. The driver specific event receive routine
2498 * will clear that state and with that indicate completion of the
2501 err = wait_on_bit_timeout(&data->flags, BTUSB_TX_WAIT_VND_EVT,
2502 TASK_INTERRUPTIBLE, HCI_INIT_TIMEOUT);
2503 if (err == -EINTR) {
2504 bt_dev_err(hdev, "Execution of wmt command interrupted");
2505 clear_bit(BTUSB_TX_WAIT_VND_EVT, &data->flags);
2510 bt_dev_err(hdev, "Execution of wmt command timed out");
2511 clear_bit(BTUSB_TX_WAIT_VND_EVT, &data->flags);
2516 /* Parse and handle the return WMT event */
2517 wmt_evt = (struct btmtk_hci_wmt_evt *)data->evt_skb->data;
2518 if (wmt_evt->whdr.op != hdr->op) {
2519 bt_dev_err(hdev, "Wrong op received %d expected %d",
2520 wmt_evt->whdr.op, hdr->op);
2525 switch (wmt_evt->whdr.op) {
2526 case BTMTK_WMT_SEMAPHORE:
2527 if (wmt_evt->whdr.flag == 2)
2528 status = BTMTK_WMT_PATCH_UNDONE;
2530 status = BTMTK_WMT_PATCH_DONE;
2532 case BTMTK_WMT_FUNC_CTRL:
2533 wmt_evt_funcc = (struct btmtk_hci_wmt_evt_funcc *)wmt_evt;
2534 if (be16_to_cpu(wmt_evt_funcc->status) == 0x404)
2535 status = BTMTK_WMT_ON_DONE;
2536 else if (be16_to_cpu(wmt_evt_funcc->status) == 0x420)
2537 status = BTMTK_WMT_ON_PROGRESS;
2539 status = BTMTK_WMT_ON_UNDONE;
2541 case BTMTK_WMT_PATCH_DWNLD:
2542 if (wmt_evt->whdr.flag == 2)
2543 status = BTMTK_WMT_PATCH_DONE;
2544 else if (wmt_evt->whdr.flag == 1)
2545 status = BTMTK_WMT_PATCH_PROGRESS;
2547 status = BTMTK_WMT_PATCH_UNDONE;
2551 if (wmt_params->status)
2552 *wmt_params->status = status;
2555 kfree_skb(data->evt_skb);
2556 data->evt_skb = NULL;
2562 static int btusb_mtk_func_query(struct hci_dev *hdev)
2564 struct btmtk_hci_wmt_params wmt_params;
2568 /* Query whether the function is enabled */
2569 wmt_params.op = BTMTK_WMT_FUNC_CTRL;
2570 wmt_params.flag = 4;
2571 wmt_params.dlen = sizeof(param);
2572 wmt_params.data = ¶m;
2573 wmt_params.status = &status;
2575 err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
2577 bt_dev_err(hdev, "Failed to query function status (%d)", err);
2584 static int btusb_mtk_uhw_reg_write(struct btusb_data *data, u32 reg, u32 val)
2586 struct hci_dev *hdev = data->hdev;
2590 buf = kzalloc(4, GFP_KERNEL);
2594 put_unaligned_le32(val, buf);
2596 pipe = usb_sndctrlpipe(data->udev, 0);
2597 err = usb_control_msg(data->udev, pipe, 0x02,
2599 reg >> 16, reg & 0xffff,
2600 buf, 4, USB_CTRL_SET_TIMEOUT);
2602 bt_dev_err(hdev, "Failed to write uhw reg(%d)", err);
2612 static int btusb_mtk_uhw_reg_read(struct btusb_data *data, u32 reg, u32 *val)
2614 struct hci_dev *hdev = data->hdev;
2618 buf = kzalloc(4, GFP_KERNEL);
2622 pipe = usb_rcvctrlpipe(data->udev, 0);
2623 err = usb_control_msg(data->udev, pipe, 0x01,
2625 reg >> 16, reg & 0xffff,
2626 buf, 4, USB_CTRL_SET_TIMEOUT);
2628 bt_dev_err(hdev, "Failed to read uhw reg(%d)", err);
2632 *val = get_unaligned_le32(buf);
2633 bt_dev_dbg(hdev, "reg=%x, value=0x%08x", reg, *val);
2641 static int btusb_mtk_reg_read(struct btusb_data *data, u32 reg, u32 *val)
2643 int pipe, err, size = sizeof(u32);
2646 buf = kzalloc(size, GFP_KERNEL);
2650 pipe = usb_rcvctrlpipe(data->udev, 0);
2651 err = usb_control_msg(data->udev, pipe, 0x63,
2652 USB_TYPE_VENDOR | USB_DIR_IN,
2653 reg >> 16, reg & 0xffff,
2654 buf, size, USB_CTRL_SET_TIMEOUT);
2658 *val = get_unaligned_le32(buf);
2666 static int btusb_mtk_id_get(struct btusb_data *data, u32 reg, u32 *id)
2668 return btusb_mtk_reg_read(data, reg, id);
2671 static int btusb_mtk_setup(struct hci_dev *hdev)
2673 struct btusb_data *data = hci_get_drvdata(hdev);
2674 struct btmtk_hci_wmt_params wmt_params;
2675 ktime_t calltime, delta, rettime;
2676 struct btmtk_tci_sleep tci_sleep;
2677 unsigned long long duration;
2678 struct sk_buff *skb;
2682 char fw_bin_name[64];
2686 calltime = ktime_get();
2688 err = btusb_mtk_id_get(data, 0x80000008, &dev_id);
2690 bt_dev_err(hdev, "Failed to get device id (%d)", err);
2695 err = btusb_mtk_id_get(data, 0x70010200, &dev_id);
2697 bt_dev_err(hdev, "Failed to get device id (%d)", err);
2700 err = btusb_mtk_id_get(data, 0x80021004, &fw_version);
2702 bt_dev_err(hdev, "Failed to get fw version (%d)", err);
2709 fwname = FIRMWARE_MT7663;
2712 fwname = FIRMWARE_MT7668;
2716 snprintf(fw_bin_name, sizeof(fw_bin_name),
2717 "mediatek/BT_RAM_CODE_MT%04x_1_%x_hdr.bin",
2718 dev_id & 0xffff, (fw_version & 0xff) + 1);
2719 err = btmtk_setup_firmware_79xx(hdev, fw_bin_name,
2720 btusb_mtk_hci_wmt_sync);
2722 bt_dev_err(hdev, "Failed to set up firmware (%d)", err);
2726 /* It's Device EndPoint Reset Option Register */
2727 btusb_mtk_uhw_reg_write(data, MTK_EP_RST_OPT, MTK_EP_RST_IN_OUT_OPT);
2729 /* Enable Bluetooth protocol */
2731 wmt_params.op = BTMTK_WMT_FUNC_CTRL;
2732 wmt_params.flag = 0;
2733 wmt_params.dlen = sizeof(param);
2734 wmt_params.data = ¶m;
2735 wmt_params.status = NULL;
2737 err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
2739 bt_dev_err(hdev, "Failed to send wmt func ctrl (%d)", err);
2743 hci_set_msft_opcode(hdev, 0xFD30);
2744 hci_set_aosp_capable(hdev);
2747 bt_dev_err(hdev, "Unsupported hardware variant (%08x)",
2752 /* Query whether the firmware is already download */
2753 wmt_params.op = BTMTK_WMT_SEMAPHORE;
2754 wmt_params.flag = 1;
2755 wmt_params.dlen = 0;
2756 wmt_params.data = NULL;
2757 wmt_params.status = &status;
2759 err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
2761 bt_dev_err(hdev, "Failed to query firmware status (%d)", err);
2765 if (status == BTMTK_WMT_PATCH_DONE) {
2766 bt_dev_info(hdev, "firmware already downloaded");
2767 goto ignore_setup_fw;
2770 /* Setup a firmware which the device definitely requires */
2771 err = btmtk_setup_firmware(hdev, fwname,
2772 btusb_mtk_hci_wmt_sync);
2777 err = readx_poll_timeout(btusb_mtk_func_query, hdev, status,
2778 status < 0 || status != BTMTK_WMT_ON_PROGRESS,
2780 /* -ETIMEDOUT happens */
2784 /* The other errors happen in btusb_mtk_func_query */
2788 if (status == BTMTK_WMT_ON_DONE) {
2789 bt_dev_info(hdev, "function already on");
2790 goto ignore_func_on;
2793 /* Enable Bluetooth protocol */
2795 wmt_params.op = BTMTK_WMT_FUNC_CTRL;
2796 wmt_params.flag = 0;
2797 wmt_params.dlen = sizeof(param);
2798 wmt_params.data = ¶m;
2799 wmt_params.status = NULL;
2801 err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
2803 bt_dev_err(hdev, "Failed to send wmt func ctrl (%d)", err);
2808 /* Apply the low power environment setup */
2809 tci_sleep.mode = 0x5;
2810 tci_sleep.duration = cpu_to_le16(0x640);
2811 tci_sleep.host_duration = cpu_to_le16(0x640);
2812 tci_sleep.host_wakeup_pin = 0;
2813 tci_sleep.time_compensation = 0;
2815 skb = __hci_cmd_sync(hdev, 0xfc7a, sizeof(tci_sleep), &tci_sleep,
2819 bt_dev_err(hdev, "Failed to apply low power setting (%d)", err);
2825 rettime = ktime_get();
2826 delta = ktime_sub(rettime, calltime);
2827 duration = (unsigned long long)ktime_to_ns(delta) >> 10;
2829 bt_dev_info(hdev, "Device setup in %llu usecs", duration);
2834 static int btusb_mtk_shutdown(struct hci_dev *hdev)
2836 struct btmtk_hci_wmt_params wmt_params;
2840 /* Disable the device */
2841 wmt_params.op = BTMTK_WMT_FUNC_CTRL;
2842 wmt_params.flag = 0;
2843 wmt_params.dlen = sizeof(param);
2844 wmt_params.data = ¶m;
2845 wmt_params.status = NULL;
2847 err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
2849 bt_dev_err(hdev, "Failed to send wmt func ctrl (%d)", err);
2856 static void btusb_mtk_cmd_timeout(struct hci_dev *hdev)
2858 struct btusb_data *data = hci_get_drvdata(hdev);
2862 /* It's MediaTek specific bluetooth reset mechanism via USB */
2863 if (test_and_set_bit(BTUSB_HW_RESET_ACTIVE, &data->flags)) {
2864 bt_dev_err(hdev, "last reset failed? Not resetting again");
2868 err = usb_autopm_get_interface(data->intf);
2872 btusb_stop_traffic(data);
2873 usb_kill_anchored_urbs(&data->tx_anchor);
2875 /* It's Device EndPoint Reset Option Register */
2876 bt_dev_dbg(hdev, "Initiating reset mechanism via uhw");
2877 btusb_mtk_uhw_reg_write(data, MTK_EP_RST_OPT, MTK_EP_RST_IN_OUT_OPT);
2878 btusb_mtk_uhw_reg_read(data, MTK_BT_WDT_STATUS, &val);
2880 /* Reset the bluetooth chip via USB interface. */
2881 btusb_mtk_uhw_reg_write(data, MTK_BT_SUBSYS_RST, 1);
2882 btusb_mtk_uhw_reg_write(data, MTK_UDMA_INT_STA_BT, 0x000000FF);
2883 btusb_mtk_uhw_reg_read(data, MTK_UDMA_INT_STA_BT, &val);
2884 btusb_mtk_uhw_reg_write(data, MTK_UDMA_INT_STA_BT1, 0x000000FF);
2885 btusb_mtk_uhw_reg_read(data, MTK_UDMA_INT_STA_BT1, &val);
2886 /* MT7921 need to delay 20ms between toggle reset bit */
2888 btusb_mtk_uhw_reg_write(data, MTK_BT_SUBSYS_RST, 0);
2889 btusb_mtk_uhw_reg_read(data, MTK_BT_SUBSYS_RST, &val);
2891 /* Poll the register until reset is completed */
2893 btusb_mtk_uhw_reg_read(data, MTK_BT_MISC, &val);
2894 if (val & MTK_BT_RST_DONE) {
2895 bt_dev_dbg(hdev, "Bluetooth Reset Successfully");
2899 bt_dev_dbg(hdev, "Polling Bluetooth Reset CR");
2901 msleep(MTK_BT_RESET_WAIT_MS);
2902 } while (retry < MTK_BT_RESET_NUM_TRIES);
2904 btusb_mtk_id_get(data, 0x70010200, &val);
2906 bt_dev_err(hdev, "Can't get device id, subsys reset fail.");
2908 usb_queue_reset_device(data->intf);
2910 clear_bit(BTUSB_HW_RESET_ACTIVE, &data->flags);
2913 static int btusb_recv_acl_mtk(struct hci_dev *hdev, struct sk_buff *skb)
2915 struct btusb_data *data = hci_get_drvdata(hdev);
2916 u16 handle = le16_to_cpu(hci_acl_hdr(skb)->handle);
2919 case 0xfc6f: /* Firmware dump from device */
2920 /* When the firmware hangs, the device can no longer
2921 * suspend and thus disable auto-suspend.
2923 usb_disable_autosuspend(data->udev);
2925 case 0x05ff: /* Firmware debug logging 1 */
2926 case 0x05fe: /* Firmware debug logging 2 */
2927 return hci_recv_diag(hdev, skb);
2930 return hci_recv_frame(hdev, skb);
2934 /* Configure an out-of-band gpio as wake-up pin, if specified in device tree */
2935 static int marvell_config_oob_wake(struct hci_dev *hdev)
2937 struct sk_buff *skb;
2938 struct btusb_data *data = hci_get_drvdata(hdev);
2939 struct device *dev = &data->udev->dev;
2940 u16 pin, gap, opcode;
2944 /* Move on if no wakeup pin specified */
2945 if (of_property_read_u16(dev->of_node, "marvell,wakeup-pin", &pin) ||
2946 of_property_read_u16(dev->of_node, "marvell,wakeup-gap-ms", &gap))
2949 /* Vendor specific command to configure a GPIO as wake-up pin */
2950 opcode = hci_opcode_pack(0x3F, 0x59);
2951 cmd[0] = opcode & 0xFF;
2952 cmd[1] = opcode >> 8;
2953 cmd[2] = 2; /* length of parameters that follow */
2955 cmd[4] = gap; /* time in ms, for which wakeup pin should be asserted */
2957 skb = bt_skb_alloc(sizeof(cmd), GFP_KERNEL);
2959 bt_dev_err(hdev, "%s: No memory", __func__);
2963 skb_put_data(skb, cmd, sizeof(cmd));
2964 hci_skb_pkt_type(skb) = HCI_COMMAND_PKT;
2966 ret = btusb_send_frame(hdev, skb);
2968 bt_dev_err(hdev, "%s: configuration failed", __func__);
2977 static int btusb_set_bdaddr_marvell(struct hci_dev *hdev,
2978 const bdaddr_t *bdaddr)
2980 struct sk_buff *skb;
2985 buf[1] = sizeof(bdaddr_t);
2986 memcpy(buf + 2, bdaddr, sizeof(bdaddr_t));
2988 skb = __hci_cmd_sync(hdev, 0xfc22, sizeof(buf), buf, HCI_INIT_TIMEOUT);
2991 bt_dev_err(hdev, "changing Marvell device address failed (%ld)",
3000 static int btusb_set_bdaddr_ath3012(struct hci_dev *hdev,
3001 const bdaddr_t *bdaddr)
3003 struct sk_buff *skb;
3010 buf[3] = sizeof(bdaddr_t);
3011 memcpy(buf + 4, bdaddr, sizeof(bdaddr_t));
3013 skb = __hci_cmd_sync(hdev, 0xfc0b, sizeof(buf), buf, HCI_INIT_TIMEOUT);
3016 bt_dev_err(hdev, "Change address command failed (%ld)", ret);
3024 static int btusb_set_bdaddr_wcn6855(struct hci_dev *hdev,
3025 const bdaddr_t *bdaddr)
3027 struct sk_buff *skb;
3031 memcpy(buf, bdaddr, sizeof(bdaddr_t));
3033 skb = __hci_cmd_sync_ev(hdev, 0xfc14, sizeof(buf), buf,
3034 HCI_EV_CMD_COMPLETE, HCI_INIT_TIMEOUT);
3037 bt_dev_err(hdev, "Change address command failed (%ld)", ret);
3045 #define QCA_DFU_PACKET_LEN 4096
3047 #define QCA_GET_TARGET_VERSION 0x09
3048 #define QCA_CHECK_STATUS 0x05
3049 #define QCA_DFU_DOWNLOAD 0x01
3051 #define QCA_SYSCFG_UPDATED 0x40
3052 #define QCA_PATCH_UPDATED 0x80
3053 #define QCA_DFU_TIMEOUT 3000
3054 #define QCA_FLAG_MULTI_NVM 0x80
3055 #define QCA_BT_RESET_WAIT_MS 100
3057 #define WCN6855_2_0_RAM_VERSION_GF 0x400c1200
3058 #define WCN6855_2_1_RAM_VERSION_GF 0x400c1211
3060 struct qca_version {
3062 __le32 patch_version;
3070 struct qca_rampatch_version {
3071 __le16 rom_version_high;
3072 __le16 rom_version_low;
3073 __le16 patch_version;
3076 struct qca_device_info {
3078 u8 rampatch_hdr; /* length of header in rampatch */
3079 u8 nvm_hdr; /* length of header in NVM */
3080 u8 ver_offset; /* offset of version structure in rampatch */
3083 static const struct qca_device_info qca_devices_table[] = {
3084 { 0x00000100, 20, 4, 8 }, /* Rome 1.0 */
3085 { 0x00000101, 20, 4, 8 }, /* Rome 1.1 */
3086 { 0x00000200, 28, 4, 16 }, /* Rome 2.0 */
3087 { 0x00000201, 28, 4, 16 }, /* Rome 2.1 */
3088 { 0x00000300, 28, 4, 16 }, /* Rome 3.0 */
3089 { 0x00000302, 28, 4, 16 }, /* Rome 3.2 */
3090 { 0x00130100, 40, 4, 16 }, /* WCN6855 1.0 */
3091 { 0x00130200, 40, 4, 16 }, /* WCN6855 2.0 */
3092 { 0x00130201, 40, 4, 16 }, /* WCN6855 2.1 */
3093 { 0x00190200, 40, 4, 16 }, /* WCN785x 2.0 */
3096 static int btusb_qca_send_vendor_req(struct usb_device *udev, u8 request,
3097 void *data, u16 size)
3102 buf = kmalloc(size, GFP_KERNEL);
3106 /* Found some of USB hosts have IOT issues with ours so that we should
3107 * not wait until HCI layer is ready.
3109 pipe = usb_rcvctrlpipe(udev, 0);
3110 err = usb_control_msg(udev, pipe, request, USB_TYPE_VENDOR | USB_DIR_IN,
3111 0, 0, buf, size, USB_CTRL_SET_TIMEOUT);
3113 dev_err(&udev->dev, "Failed to access otp area (%d)", err);
3117 memcpy(data, buf, size);
3125 static int btusb_setup_qca_download_fw(struct hci_dev *hdev,
3126 const struct firmware *firmware,
3129 struct btusb_data *btdata = hci_get_drvdata(hdev);
3130 struct usb_device *udev = btdata->udev;
3131 size_t count, size, sent = 0;
3135 buf = kmalloc(QCA_DFU_PACKET_LEN, GFP_KERNEL);
3139 count = firmware->size;
3141 size = min_t(size_t, count, hdr_size);
3142 memcpy(buf, firmware->data, size);
3144 /* USB patches should go down to controller through USB path
3145 * because binary format fits to go down through USB channel.
3146 * USB control path is for patching headers and USB bulk is for
3149 pipe = usb_sndctrlpipe(udev, 0);
3150 err = usb_control_msg(udev, pipe, QCA_DFU_DOWNLOAD, USB_TYPE_VENDOR,
3151 0, 0, buf, size, USB_CTRL_SET_TIMEOUT);
3153 bt_dev_err(hdev, "Failed to send headers (%d)", err);
3160 /* ep2 need time to switch from function acl to function dfu,
3161 * so we add 20ms delay here.
3166 size = min_t(size_t, count, QCA_DFU_PACKET_LEN);
3168 memcpy(buf, firmware->data + sent, size);
3170 pipe = usb_sndbulkpipe(udev, 0x02);
3171 err = usb_bulk_msg(udev, pipe, buf, size, &len,
3174 bt_dev_err(hdev, "Failed to send body at %zd of %zd (%d)",
3175 sent, firmware->size, err);
3180 bt_dev_err(hdev, "Failed to get bulk buffer");
3194 static int btusb_setup_qca_load_rampatch(struct hci_dev *hdev,
3195 struct qca_version *ver,
3196 const struct qca_device_info *info)
3198 struct qca_rampatch_version *rver;
3199 const struct firmware *fw;
3200 u32 ver_rom, ver_patch, rver_rom;
3201 u16 rver_rom_low, rver_rom_high, rver_patch;
3205 ver_rom = le32_to_cpu(ver->rom_version);
3206 ver_patch = le32_to_cpu(ver->patch_version);
3208 snprintf(fwname, sizeof(fwname), "qca/rampatch_usb_%08x.bin", ver_rom);
3210 err = request_firmware(&fw, fwname, &hdev->dev);
3212 bt_dev_err(hdev, "failed to request rampatch file: %s (%d)",
3217 bt_dev_info(hdev, "using rampatch file: %s", fwname);
3219 rver = (struct qca_rampatch_version *)(fw->data + info->ver_offset);
3220 rver_rom_low = le16_to_cpu(rver->rom_version_low);
3221 rver_patch = le16_to_cpu(rver->patch_version);
3223 if (ver_rom & ~0xffffU) {
3224 rver_rom_high = le16_to_cpu(rver->rom_version_high);
3225 rver_rom = le32_to_cpu(rver_rom_high << 16 | rver_rom_low);
3227 rver_rom = rver_rom_low;
3230 bt_dev_info(hdev, "QCA: patch rome 0x%x build 0x%x, "
3231 "firmware rome 0x%x build 0x%x",
3232 rver_rom, rver_patch, ver_rom, ver_patch);
3234 if (rver_rom != ver_rom || rver_patch <= ver_patch) {
3235 bt_dev_err(hdev, "rampatch file version did not match with firmware");
3240 err = btusb_setup_qca_download_fw(hdev, fw, info->rampatch_hdr);
3243 release_firmware(fw);
3248 static void btusb_generate_qca_nvm_name(char *fwname, size_t max_size,
3249 const struct qca_version *ver)
3251 u32 rom_version = le32_to_cpu(ver->rom_version);
3252 u16 flag = le16_to_cpu(ver->flag);
3254 if (((flag >> 8) & 0xff) == QCA_FLAG_MULTI_NVM) {
3255 /* The board_id should be split into two bytes
3256 * The 1st byte is chip ID, and the 2nd byte is platform ID
3257 * For example, board ID 0x010A, 0x01 is platform ID. 0x0A is chip ID
3258 * we have several platforms, and platform IDs are continuously added
3260 * 0x00 is for Mobile
3262 * 0x02 is for Automotive
3263 * 0x03 is for Consumer electronic
3265 u16 board_id = (ver->chip_id << 8) + ver->platform_id;
3266 const char *variant;
3268 switch (le32_to_cpu(ver->ram_version)) {
3269 case WCN6855_2_0_RAM_VERSION_GF:
3270 case WCN6855_2_1_RAM_VERSION_GF:
3278 if (board_id == 0) {
3279 snprintf(fwname, max_size, "qca/nvm_usb_%08x%s.bin",
3280 rom_version, variant);
3282 snprintf(fwname, max_size, "qca/nvm_usb_%08x%s_%04x.bin",
3283 rom_version, variant, board_id);
3286 snprintf(fwname, max_size, "qca/nvm_usb_%08x.bin",
3292 static int btusb_setup_qca_load_nvm(struct hci_dev *hdev,
3293 struct qca_version *ver,
3294 const struct qca_device_info *info)
3296 const struct firmware *fw;
3300 btusb_generate_qca_nvm_name(fwname, sizeof(fwname), ver);
3302 err = request_firmware(&fw, fwname, &hdev->dev);
3304 bt_dev_err(hdev, "failed to request NVM file: %s (%d)",
3309 bt_dev_info(hdev, "using NVM file: %s", fwname);
3311 err = btusb_setup_qca_download_fw(hdev, fw, info->nvm_hdr);
3313 release_firmware(fw);
3318 /* identify the ROM version and check whether patches are needed */
3319 static bool btusb_qca_need_patch(struct usb_device *udev)
3321 struct qca_version ver;
3323 if (btusb_qca_send_vendor_req(udev, QCA_GET_TARGET_VERSION, &ver,
3326 /* only low ROM versions need patches */
3327 return !(le32_to_cpu(ver.rom_version) & ~0xffffU);
3330 static int btusb_setup_qca(struct hci_dev *hdev)
3332 struct btusb_data *btdata = hci_get_drvdata(hdev);
3333 struct usb_device *udev = btdata->udev;
3334 const struct qca_device_info *info = NULL;
3335 struct qca_version ver;
3340 err = btusb_qca_send_vendor_req(udev, QCA_GET_TARGET_VERSION, &ver,
3345 ver_rom = le32_to_cpu(ver.rom_version);
3347 for (i = 0; i < ARRAY_SIZE(qca_devices_table); i++) {
3348 if (ver_rom == qca_devices_table[i].rom_version)
3349 info = &qca_devices_table[i];
3352 /* If the rom_version is not matched in the qca_devices_table
3353 * and the high ROM version is not zero, we assume this chip no
3354 * need to load the rampatch and nvm.
3356 if (ver_rom & ~0xffffU)
3359 bt_dev_err(hdev, "don't support firmware rome 0x%x", ver_rom);
3363 err = btusb_qca_send_vendor_req(udev, QCA_CHECK_STATUS, &status,
3368 if (!(status & QCA_PATCH_UPDATED)) {
3369 err = btusb_setup_qca_load_rampatch(hdev, &ver, info);
3374 err = btusb_qca_send_vendor_req(udev, QCA_GET_TARGET_VERSION, &ver,
3379 if (!(status & QCA_SYSCFG_UPDATED)) {
3380 err = btusb_setup_qca_load_nvm(hdev, &ver, info);
3384 /* WCN6855 2.1 and later will reset to apply firmware downloaded here, so
3385 * wait ~100ms for reset Done then go ahead, otherwise, it maybe
3386 * cause potential enable failure.
3388 if (info->rom_version >= 0x00130201)
3389 msleep(QCA_BT_RESET_WAIT_MS);
3392 /* Mark HCI_OP_ENHANCED_SETUP_SYNC_CONN as broken as it doesn't seem to
3393 * work with the likes of HSP/HFP mSBC.
3395 set_bit(HCI_QUIRK_BROKEN_ENHANCED_SETUP_SYNC_CONN, &hdev->quirks);
3400 static inline int __set_diag_interface(struct hci_dev *hdev)
3402 struct btusb_data *data = hci_get_drvdata(hdev);
3403 struct usb_interface *intf = data->diag;
3409 data->diag_tx_ep = NULL;
3410 data->diag_rx_ep = NULL;
3412 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
3413 struct usb_endpoint_descriptor *ep_desc;
3415 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
3417 if (!data->diag_tx_ep && usb_endpoint_is_bulk_out(ep_desc)) {
3418 data->diag_tx_ep = ep_desc;
3422 if (!data->diag_rx_ep && usb_endpoint_is_bulk_in(ep_desc)) {
3423 data->diag_rx_ep = ep_desc;
3428 if (!data->diag_tx_ep || !data->diag_rx_ep) {
3429 bt_dev_err(hdev, "invalid diagnostic descriptors");
3436 static struct urb *alloc_diag_urb(struct hci_dev *hdev, bool enable)
3438 struct btusb_data *data = hci_get_drvdata(hdev);
3439 struct sk_buff *skb;
3443 if (!data->diag_tx_ep)
3444 return ERR_PTR(-ENODEV);
3446 urb = usb_alloc_urb(0, GFP_KERNEL);
3448 return ERR_PTR(-ENOMEM);
3450 skb = bt_skb_alloc(2, GFP_KERNEL);
3453 return ERR_PTR(-ENOMEM);
3456 skb_put_u8(skb, 0xf0);
3457 skb_put_u8(skb, enable);
3459 pipe = usb_sndbulkpipe(data->udev, data->diag_tx_ep->bEndpointAddress);
3461 usb_fill_bulk_urb(urb, data->udev, pipe,
3462 skb->data, skb->len, btusb_tx_complete, skb);
3464 skb->dev = (void *)hdev;
3469 static int btusb_bcm_set_diag(struct hci_dev *hdev, bool enable)
3471 struct btusb_data *data = hci_get_drvdata(hdev);
3477 if (!test_bit(HCI_RUNNING, &hdev->flags))
3480 urb = alloc_diag_urb(hdev, enable);
3482 return PTR_ERR(urb);
3484 return submit_or_queue_tx_urb(hdev, urb);
3488 static irqreturn_t btusb_oob_wake_handler(int irq, void *priv)
3490 struct btusb_data *data = priv;
3492 pm_wakeup_event(&data->udev->dev, 0);
3495 /* Disable only if not already disabled (keep it balanced) */
3496 if (test_and_clear_bit(BTUSB_OOB_WAKE_ENABLED, &data->flags)) {
3497 disable_irq_nosync(irq);
3498 disable_irq_wake(irq);
3503 static const struct of_device_id btusb_match_table[] = {
3504 { .compatible = "usb1286,204e" },
3505 { .compatible = "usbcf3,e300" }, /* QCA6174A */
3506 { .compatible = "usb4ca,301a" }, /* QCA6174A (Lite-On) */
3509 MODULE_DEVICE_TABLE(of, btusb_match_table);
3511 /* Use an oob wakeup pin? */
3512 static int btusb_config_oob_wake(struct hci_dev *hdev)
3514 struct btusb_data *data = hci_get_drvdata(hdev);
3515 struct device *dev = &data->udev->dev;
3518 clear_bit(BTUSB_OOB_WAKE_ENABLED, &data->flags);
3520 if (!of_match_device(btusb_match_table, dev))
3523 /* Move on if no IRQ specified */
3524 irq = of_irq_get_byname(dev->of_node, "wakeup");
3526 bt_dev_dbg(hdev, "%s: no OOB Wakeup IRQ in DT", __func__);
3530 irq_set_status_flags(irq, IRQ_NOAUTOEN);
3531 ret = devm_request_irq(&hdev->dev, irq, btusb_oob_wake_handler,
3532 0, "OOB Wake-on-BT", data);
3534 bt_dev_err(hdev, "%s: IRQ request failed", __func__);
3538 ret = device_init_wakeup(dev, true);
3540 bt_dev_err(hdev, "%s: failed to init_wakeup", __func__);
3544 data->oob_wake_irq = irq;
3545 bt_dev_info(hdev, "OOB Wake-on-BT configured at IRQ %u", irq);
3550 static void btusb_check_needs_reset_resume(struct usb_interface *intf)
3552 if (dmi_check_system(btusb_needs_reset_resume_table))
3553 interface_to_usbdev(intf)->quirks |= USB_QUIRK_RESET_RESUME;
3556 static bool btusb_wakeup(struct hci_dev *hdev)
3558 struct btusb_data *data = hci_get_drvdata(hdev);
3560 return device_may_wakeup(&data->udev->dev);
3563 static int btusb_shutdown_qca(struct hci_dev *hdev)
3565 struct sk_buff *skb;
3567 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
3569 bt_dev_err(hdev, "HCI reset during shutdown failed");
3570 return PTR_ERR(skb);
3577 static ssize_t force_poll_sync_read(struct file *file, char __user *user_buf,
3578 size_t count, loff_t *ppos)
3580 struct btusb_data *data = file->private_data;
3583 buf[0] = data->poll_sync ? 'Y' : 'N';
3586 return simple_read_from_buffer(user_buf, count, ppos, buf, 2);
3589 static ssize_t force_poll_sync_write(struct file *file,
3590 const char __user *user_buf,
3591 size_t count, loff_t *ppos)
3593 struct btusb_data *data = file->private_data;
3597 err = kstrtobool_from_user(user_buf, count, &enable);
3601 /* Only allow changes while the adapter is down */
3602 if (test_bit(HCI_UP, &data->hdev->flags))
3605 if (data->poll_sync == enable)
3608 data->poll_sync = enable;
3613 static const struct file_operations force_poll_sync_fops = {
3614 .open = simple_open,
3615 .read = force_poll_sync_read,
3616 .write = force_poll_sync_write,
3617 .llseek = default_llseek,
3620 static int btusb_probe(struct usb_interface *intf,
3621 const struct usb_device_id *id)
3623 struct usb_endpoint_descriptor *ep_desc;
3624 struct gpio_desc *reset_gpio;
3625 struct btusb_data *data;
3626 struct hci_dev *hdev;
3627 unsigned ifnum_base;
3628 int i, err, priv_size;
3630 BT_DBG("intf %p id %p", intf, id);
3632 /* interface numbers are hardcoded in the spec */
3633 if (intf->cur_altsetting->desc.bInterfaceNumber != 0) {
3634 if (!(id->driver_info & BTUSB_IFNUM_2))
3636 if (intf->cur_altsetting->desc.bInterfaceNumber != 2)
3640 ifnum_base = intf->cur_altsetting->desc.bInterfaceNumber;
3642 if (!id->driver_info) {
3643 const struct usb_device_id *match;
3645 match = usb_match_id(intf, blacklist_table);
3650 if (id->driver_info == BTUSB_IGNORE)
3653 if (id->driver_info & BTUSB_ATH3012) {
3654 struct usb_device *udev = interface_to_usbdev(intf);
3656 /* Old firmware would otherwise let ath3k driver load
3657 * patch and sysconfig files
3659 if (le16_to_cpu(udev->descriptor.bcdDevice) <= 0x0001 &&
3660 !btusb_qca_need_patch(udev))
3664 data = devm_kzalloc(&intf->dev, sizeof(*data), GFP_KERNEL);
3668 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
3669 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
3671 if (!data->intr_ep && usb_endpoint_is_int_in(ep_desc)) {
3672 data->intr_ep = ep_desc;
3676 if (!data->bulk_tx_ep && usb_endpoint_is_bulk_out(ep_desc)) {
3677 data->bulk_tx_ep = ep_desc;
3681 if (!data->bulk_rx_ep && usb_endpoint_is_bulk_in(ep_desc)) {
3682 data->bulk_rx_ep = ep_desc;
3687 if (!data->intr_ep || !data->bulk_tx_ep || !data->bulk_rx_ep)
3690 if (id->driver_info & BTUSB_AMP) {
3691 data->cmdreq_type = USB_TYPE_CLASS | 0x01;
3692 data->cmdreq = 0x2b;
3694 data->cmdreq_type = USB_TYPE_CLASS;
3695 data->cmdreq = 0x00;
3698 data->udev = interface_to_usbdev(intf);
3701 INIT_WORK(&data->work, btusb_work);
3702 INIT_WORK(&data->waker, btusb_waker);
3703 INIT_DELAYED_WORK(&data->rx_work, btusb_rx_work);
3705 skb_queue_head_init(&data->acl_q);
3707 init_usb_anchor(&data->deferred);
3708 init_usb_anchor(&data->tx_anchor);
3709 spin_lock_init(&data->txlock);
3711 init_usb_anchor(&data->intr_anchor);
3712 init_usb_anchor(&data->bulk_anchor);
3713 init_usb_anchor(&data->isoc_anchor);
3714 init_usb_anchor(&data->diag_anchor);
3715 init_usb_anchor(&data->ctrl_anchor);
3716 spin_lock_init(&data->rxlock);
3720 data->recv_event = hci_recv_frame;
3721 data->recv_bulk = btusb_recv_bulk;
3723 if (id->driver_info & BTUSB_INTEL_COMBINED) {
3724 /* Allocate extra space for Intel device */
3725 priv_size += sizeof(struct btintel_data);
3727 /* Override the rx handlers */
3728 data->recv_event = btusb_recv_event_intel;
3729 data->recv_bulk = btusb_recv_bulk_intel;
3732 data->recv_acl = hci_recv_frame;
3734 hdev = hci_alloc_dev_priv(priv_size);
3738 hdev->bus = HCI_USB;
3739 hci_set_drvdata(hdev, data);
3741 if (id->driver_info & BTUSB_AMP)
3742 hdev->dev_type = HCI_AMP;
3744 hdev->dev_type = HCI_PRIMARY;
3748 SET_HCIDEV_DEV(hdev, &intf->dev);
3750 reset_gpio = gpiod_get_optional(&data->udev->dev, "reset",
3752 if (IS_ERR(reset_gpio)) {
3753 err = PTR_ERR(reset_gpio);
3755 } else if (reset_gpio) {
3756 data->reset_gpio = reset_gpio;
3759 hdev->open = btusb_open;
3760 hdev->close = btusb_close;
3761 hdev->flush = btusb_flush;
3762 hdev->send = btusb_send_frame;
3763 hdev->notify = btusb_notify;
3764 hdev->wakeup = btusb_wakeup;
3767 err = btusb_config_oob_wake(hdev);
3771 /* Marvell devices may need a specific chip configuration */
3772 if (id->driver_info & BTUSB_MARVELL && data->oob_wake_irq) {
3773 err = marvell_config_oob_wake(hdev);
3778 if (id->driver_info & BTUSB_CW6622)
3779 set_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks);
3781 if (id->driver_info & BTUSB_BCM2045)
3782 set_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks);
3784 if (id->driver_info & BTUSB_BCM92035)
3785 hdev->setup = btusb_setup_bcm92035;
3787 if (IS_ENABLED(CONFIG_BT_HCIBTUSB_BCM) &&
3788 (id->driver_info & BTUSB_BCM_PATCHRAM)) {
3789 hdev->manufacturer = 15;
3790 hdev->setup = btbcm_setup_patchram;
3791 hdev->set_diag = btusb_bcm_set_diag;
3792 hdev->set_bdaddr = btbcm_set_bdaddr;
3794 /* Broadcom LM_DIAG Interface numbers are hardcoded */
3795 data->diag = usb_ifnum_to_if(data->udev, ifnum_base + 2);
3798 if (IS_ENABLED(CONFIG_BT_HCIBTUSB_BCM) &&
3799 (id->driver_info & BTUSB_BCM_APPLE)) {
3800 hdev->manufacturer = 15;
3801 hdev->setup = btbcm_setup_apple;
3802 hdev->set_diag = btusb_bcm_set_diag;
3804 /* Broadcom LM_DIAG Interface numbers are hardcoded */
3805 data->diag = usb_ifnum_to_if(data->udev, ifnum_base + 2);
3808 /* Combined Intel Device setup to support multiple setup routine */
3809 if (id->driver_info & BTUSB_INTEL_COMBINED) {
3810 err = btintel_configure_setup(hdev);
3814 /* Transport specific configuration */
3815 hdev->send = btusb_send_frame_intel;
3816 hdev->cmd_timeout = btusb_intel_cmd_timeout;
3818 if (id->driver_info & BTUSB_INTEL_NO_WBS_SUPPORT)
3819 btintel_set_flag(hdev, INTEL_ROM_LEGACY_NO_WBS_SUPPORT);
3821 if (id->driver_info & BTUSB_INTEL_BROKEN_INITIAL_NCMD)
3822 btintel_set_flag(hdev, INTEL_BROKEN_INITIAL_NCMD);
3824 if (id->driver_info & BTUSB_INTEL_BROKEN_SHUTDOWN_LED)
3825 btintel_set_flag(hdev, INTEL_BROKEN_SHUTDOWN_LED);
3828 if (id->driver_info & BTUSB_MARVELL)
3829 hdev->set_bdaddr = btusb_set_bdaddr_marvell;
3831 if (IS_ENABLED(CONFIG_BT_HCIBTUSB_MTK) &&
3832 (id->driver_info & BTUSB_MEDIATEK)) {
3833 hdev->setup = btusb_mtk_setup;
3834 hdev->shutdown = btusb_mtk_shutdown;
3835 hdev->manufacturer = 70;
3836 hdev->cmd_timeout = btusb_mtk_cmd_timeout;
3837 hdev->set_bdaddr = btmtk_set_bdaddr;
3838 set_bit(HCI_QUIRK_BROKEN_ENHANCED_SETUP_SYNC_CONN, &hdev->quirks);
3839 set_bit(HCI_QUIRK_NON_PERSISTENT_SETUP, &hdev->quirks);
3840 data->recv_acl = btusb_recv_acl_mtk;
3843 if (id->driver_info & BTUSB_SWAVE) {
3844 set_bit(HCI_QUIRK_FIXUP_INQUIRY_MODE, &hdev->quirks);
3845 set_bit(HCI_QUIRK_BROKEN_LOCAL_COMMANDS, &hdev->quirks);
3848 if (id->driver_info & BTUSB_INTEL_BOOT) {
3849 hdev->manufacturer = 2;
3850 set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
3853 if (id->driver_info & BTUSB_ATH3012) {
3854 data->setup_on_usb = btusb_setup_qca;
3855 hdev->set_bdaddr = btusb_set_bdaddr_ath3012;
3856 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
3857 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
3860 if (id->driver_info & BTUSB_QCA_ROME) {
3861 data->setup_on_usb = btusb_setup_qca;
3862 hdev->set_bdaddr = btusb_set_bdaddr_ath3012;
3863 hdev->cmd_timeout = btusb_qca_cmd_timeout;
3864 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
3865 btusb_check_needs_reset_resume(intf);
3868 if (id->driver_info & BTUSB_QCA_WCN6855) {
3869 data->setup_on_usb = btusb_setup_qca;
3870 hdev->shutdown = btusb_shutdown_qca;
3871 hdev->set_bdaddr = btusb_set_bdaddr_wcn6855;
3872 hdev->cmd_timeout = btusb_qca_cmd_timeout;
3873 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
3874 hci_set_msft_opcode(hdev, 0xFD70);
3877 if (id->driver_info & BTUSB_AMP) {
3878 /* AMP controllers do not support SCO packets */
3881 /* Interface orders are hardcoded in the specification */
3882 data->isoc = usb_ifnum_to_if(data->udev, ifnum_base + 1);
3883 data->isoc_ifnum = ifnum_base + 1;
3886 if (IS_ENABLED(CONFIG_BT_HCIBTUSB_RTL) &&
3887 (id->driver_info & BTUSB_REALTEK)) {
3888 hdev->setup = btrtl_setup_realtek;
3889 hdev->shutdown = btrtl_shutdown_realtek;
3890 hdev->cmd_timeout = btusb_rtl_cmd_timeout;
3892 /* Realtek devices need to set remote wakeup on auto-suspend */
3893 set_bit(BTUSB_WAKEUP_AUTOSUSPEND, &data->flags);
3894 set_bit(BTUSB_USE_ALT3_FOR_WBS, &data->flags);
3898 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
3900 if (force_scofix || id->driver_info & BTUSB_WRONG_SCO_MTU) {
3901 if (!disable_scofix)
3902 set_bit(HCI_QUIRK_FIXUP_BUFFER_SIZE, &hdev->quirks);
3905 if (id->driver_info & BTUSB_BROKEN_ISOC)
3908 if (id->driver_info & BTUSB_WIDEBAND_SPEECH)
3909 set_bit(HCI_QUIRK_WIDEBAND_SPEECH_SUPPORTED, &hdev->quirks);
3911 if (id->driver_info & BTUSB_VALID_LE_STATES)
3912 set_bit(HCI_QUIRK_VALID_LE_STATES, &hdev->quirks);
3914 if (id->driver_info & BTUSB_DIGIANSWER) {
3915 data->cmdreq_type = USB_TYPE_VENDOR;
3916 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
3919 if (id->driver_info & BTUSB_CSR) {
3920 struct usb_device *udev = data->udev;
3921 u16 bcdDevice = le16_to_cpu(udev->descriptor.bcdDevice);
3923 /* Old firmware would otherwise execute USB reset */
3924 if (bcdDevice < 0x117)
3925 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
3927 /* This must be set first in case we disable it for fakes */
3928 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
3930 /* Fake CSR devices with broken commands */
3931 if (le16_to_cpu(udev->descriptor.idVendor) == 0x0a12 &&
3932 le16_to_cpu(udev->descriptor.idProduct) == 0x0001)
3933 hdev->setup = btusb_setup_csr;
3936 if (id->driver_info & BTUSB_SNIFFER) {
3937 struct usb_device *udev = data->udev;
3939 /* New sniffer firmware has crippled HCI interface */
3940 if (le16_to_cpu(udev->descriptor.bcdDevice) > 0x997)
3941 set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
3944 if (id->driver_info & BTUSB_INTEL_BOOT) {
3945 /* A bug in the bootloader causes that interrupt interface is
3946 * only enabled after receiving SetInterface(0, AltSetting=0).
3948 err = usb_set_interface(data->udev, 0, 0);
3950 BT_ERR("failed to set interface 0, alt 0 %d", err);
3956 err = usb_driver_claim_interface(&btusb_driver,
3962 if (IS_ENABLED(CONFIG_BT_HCIBTUSB_BCM) && data->diag) {
3963 if (!usb_driver_claim_interface(&btusb_driver,
3965 __set_diag_interface(hdev);
3970 if (enable_autosuspend)
3971 usb_enable_autosuspend(data->udev);
3973 err = hci_register_dev(hdev);
3977 usb_set_intfdata(intf, data);
3979 debugfs_create_file("force_poll_sync", 0644, hdev->debugfs, data,
3980 &force_poll_sync_fops);
3985 if (data->reset_gpio)
3986 gpiod_put(data->reset_gpio);
3991 static void btusb_disconnect(struct usb_interface *intf)
3993 struct btusb_data *data = usb_get_intfdata(intf);
3994 struct hci_dev *hdev;
3996 BT_DBG("intf %p", intf);
4002 usb_set_intfdata(data->intf, NULL);
4005 usb_set_intfdata(data->isoc, NULL);
4008 usb_set_intfdata(data->diag, NULL);
4010 hci_unregister_dev(hdev);
4012 if (intf == data->intf) {
4014 usb_driver_release_interface(&btusb_driver, data->isoc);
4016 usb_driver_release_interface(&btusb_driver, data->diag);
4017 } else if (intf == data->isoc) {
4019 usb_driver_release_interface(&btusb_driver, data->diag);
4020 usb_driver_release_interface(&btusb_driver, data->intf);
4021 } else if (intf == data->diag) {
4022 usb_driver_release_interface(&btusb_driver, data->intf);
4024 usb_driver_release_interface(&btusb_driver, data->isoc);
4027 if (data->oob_wake_irq)
4028 device_init_wakeup(&data->udev->dev, false);
4030 if (data->reset_gpio)
4031 gpiod_put(data->reset_gpio);
4037 static int btusb_suspend(struct usb_interface *intf, pm_message_t message)
4039 struct btusb_data *data = usb_get_intfdata(intf);
4041 BT_DBG("intf %p", intf);
4043 if (data->suspend_count++)
4046 spin_lock_irq(&data->txlock);
4047 if (!(PMSG_IS_AUTO(message) && data->tx_in_flight)) {
4048 set_bit(BTUSB_SUSPENDING, &data->flags);
4049 spin_unlock_irq(&data->txlock);
4051 spin_unlock_irq(&data->txlock);
4052 data->suspend_count--;
4056 cancel_work_sync(&data->work);
4058 btusb_stop_traffic(data);
4059 usb_kill_anchored_urbs(&data->tx_anchor);
4061 if (data->oob_wake_irq && device_may_wakeup(&data->udev->dev)) {
4062 set_bit(BTUSB_OOB_WAKE_ENABLED, &data->flags);
4063 enable_irq_wake(data->oob_wake_irq);
4064 enable_irq(data->oob_wake_irq);
4067 /* For global suspend, Realtek devices lose the loaded fw
4068 * in them. But for autosuspend, firmware should remain.
4069 * Actually, it depends on whether the usb host sends
4070 * set feature (enable wakeup) or not.
4072 if (test_bit(BTUSB_WAKEUP_AUTOSUSPEND, &data->flags)) {
4073 if (PMSG_IS_AUTO(message) &&
4074 device_can_wakeup(&data->udev->dev))
4075 data->udev->do_remote_wakeup = 1;
4076 else if (!PMSG_IS_AUTO(message) &&
4077 !device_may_wakeup(&data->udev->dev)) {
4078 data->udev->do_remote_wakeup = 0;
4079 data->udev->reset_resume = 1;
4086 static void play_deferred(struct btusb_data *data)
4091 while ((urb = usb_get_from_anchor(&data->deferred))) {
4092 usb_anchor_urb(urb, &data->tx_anchor);
4094 err = usb_submit_urb(urb, GFP_ATOMIC);
4096 if (err != -EPERM && err != -ENODEV)
4097 BT_ERR("%s urb %p submission failed (%d)",
4098 data->hdev->name, urb, -err);
4099 kfree(urb->setup_packet);
4100 usb_unanchor_urb(urb);
4105 data->tx_in_flight++;
4109 /* Cleanup the rest deferred urbs. */
4110 while ((urb = usb_get_from_anchor(&data->deferred))) {
4111 kfree(urb->setup_packet);
4116 static int btusb_resume(struct usb_interface *intf)
4118 struct btusb_data *data = usb_get_intfdata(intf);
4119 struct hci_dev *hdev = data->hdev;
4122 BT_DBG("intf %p", intf);
4124 if (--data->suspend_count)
4127 /* Disable only if not already disabled (keep it balanced) */
4128 if (test_and_clear_bit(BTUSB_OOB_WAKE_ENABLED, &data->flags)) {
4129 disable_irq(data->oob_wake_irq);
4130 disable_irq_wake(data->oob_wake_irq);
4133 if (!test_bit(HCI_RUNNING, &hdev->flags))
4136 if (test_bit(BTUSB_INTR_RUNNING, &data->flags)) {
4137 err = btusb_submit_intr_urb(hdev, GFP_NOIO);
4139 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
4144 if (test_bit(BTUSB_BULK_RUNNING, &data->flags)) {
4145 err = btusb_submit_bulk_urb(hdev, GFP_NOIO);
4147 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
4151 btusb_submit_bulk_urb(hdev, GFP_NOIO);
4154 if (test_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
4155 if (btusb_submit_isoc_urb(hdev, GFP_NOIO) < 0)
4156 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
4158 btusb_submit_isoc_urb(hdev, GFP_NOIO);
4161 spin_lock_irq(&data->txlock);
4162 play_deferred(data);
4163 clear_bit(BTUSB_SUSPENDING, &data->flags);
4164 spin_unlock_irq(&data->txlock);
4165 schedule_work(&data->work);
4170 usb_scuttle_anchored_urbs(&data->deferred);
4172 spin_lock_irq(&data->txlock);
4173 clear_bit(BTUSB_SUSPENDING, &data->flags);
4174 spin_unlock_irq(&data->txlock);
4180 static struct usb_driver btusb_driver = {
4182 .probe = btusb_probe,
4183 .disconnect = btusb_disconnect,
4185 .suspend = btusb_suspend,
4186 .resume = btusb_resume,
4188 .id_table = btusb_table,
4189 .supports_autosuspend = 1,
4190 .disable_hub_initiated_lpm = 1,
4193 module_usb_driver(btusb_driver);
4195 module_param(disable_scofix, bool, 0644);
4196 MODULE_PARM_DESC(disable_scofix, "Disable fixup of wrong SCO buffer size");
4198 module_param(force_scofix, bool, 0644);
4199 MODULE_PARM_DESC(force_scofix, "Force fixup of wrong SCO buffers size");
4201 module_param(enable_autosuspend, bool, 0644);
4202 MODULE_PARM_DESC(enable_autosuspend, "Enable USB autosuspend by default");
4204 module_param(reset, bool, 0644);
4205 MODULE_PARM_DESC(reset, "Send HCI reset command on initialization");
4208 MODULE_DESCRIPTION("Generic Bluetooth USB driver ver " VERSION);
4209 MODULE_VERSION(VERSION);
4210 MODULE_LICENSE("GPL");