1 // SPDX-License-Identifier: GPL-2.0-or-later
4 * Generic Bluetooth USB driver
6 * Copyright (C) 2005-2008 Marcel Holtmann <marcel@holtmann.org>
10 #include <linux/module.h>
11 #include <linux/usb.h>
12 #include <linux/usb/quirks.h>
13 #include <linux/firmware.h>
14 #include <linux/iopoll.h>
15 #include <linux/of_device.h>
16 #include <linux/of_irq.h>
17 #include <linux/suspend.h>
18 #include <linux/gpio/consumer.h>
19 #include <asm/unaligned.h>
21 #include <net/bluetooth/bluetooth.h>
22 #include <net/bluetooth/hci_core.h>
30 static bool disable_scofix;
31 static bool force_scofix;
32 static bool enable_autosuspend = IS_ENABLED(CONFIG_BT_HCIBTUSB_AUTOSUSPEND);
34 static bool reset = true;
36 static struct usb_driver btusb_driver;
38 #define BTUSB_IGNORE 0x01
39 #define BTUSB_DIGIANSWER 0x02
40 #define BTUSB_CSR 0x04
41 #define BTUSB_SNIFFER 0x08
42 #define BTUSB_BCM92035 0x10
43 #define BTUSB_BROKEN_ISOC 0x20
44 #define BTUSB_WRONG_SCO_MTU 0x40
45 #define BTUSB_ATH3012 0x80
46 #define BTUSB_INTEL 0x100
47 #define BTUSB_INTEL_BOOT 0x200
48 #define BTUSB_BCM_PATCHRAM 0x400
49 #define BTUSB_MARVELL 0x800
50 #define BTUSB_SWAVE 0x1000
51 #define BTUSB_INTEL_NEW 0x2000
52 #define BTUSB_AMP 0x4000
53 #define BTUSB_QCA_ROME 0x8000
54 #define BTUSB_BCM_APPLE 0x10000
55 #define BTUSB_REALTEK 0x20000
56 #define BTUSB_BCM2045 0x40000
57 #define BTUSB_IFNUM_2 0x80000
58 #define BTUSB_CW6622 0x100000
59 #define BTUSB_MEDIATEK 0x200000
61 static const struct usb_device_id btusb_table[] = {
62 /* Generic Bluetooth USB device */
63 { USB_DEVICE_INFO(0xe0, 0x01, 0x01) },
65 /* Generic Bluetooth AMP device */
66 { USB_DEVICE_INFO(0xe0, 0x01, 0x04), .driver_info = BTUSB_AMP },
68 /* Generic Bluetooth USB interface */
69 { USB_INTERFACE_INFO(0xe0, 0x01, 0x01) },
71 /* Apple-specific (Broadcom) devices */
72 { USB_VENDOR_AND_INTERFACE_INFO(0x05ac, 0xff, 0x01, 0x01),
73 .driver_info = BTUSB_BCM_APPLE | BTUSB_IFNUM_2 },
75 /* MediaTek MT76x0E */
76 { USB_DEVICE(0x0e8d, 0x763f) },
78 /* Broadcom SoftSailing reporting vendor specific */
79 { USB_DEVICE(0x0a5c, 0x21e1) },
81 /* Apple MacBookPro 7,1 */
82 { USB_DEVICE(0x05ac, 0x8213) },
85 { USB_DEVICE(0x05ac, 0x8215) },
87 /* Apple MacBookPro6,2 */
88 { USB_DEVICE(0x05ac, 0x8218) },
90 /* Apple MacBookAir3,1, MacBookAir3,2 */
91 { USB_DEVICE(0x05ac, 0x821b) },
93 /* Apple MacBookAir4,1 */
94 { USB_DEVICE(0x05ac, 0x821f) },
96 /* Apple MacBookPro8,2 */
97 { USB_DEVICE(0x05ac, 0x821a) },
99 /* Apple MacMini5,1 */
100 { USB_DEVICE(0x05ac, 0x8281) },
102 /* AVM BlueFRITZ! USB v2.0 */
103 { USB_DEVICE(0x057c, 0x3800), .driver_info = BTUSB_SWAVE },
105 /* Bluetooth Ultraport Module from IBM */
106 { USB_DEVICE(0x04bf, 0x030a) },
108 /* ALPS Modules with non-standard id */
109 { USB_DEVICE(0x044e, 0x3001) },
110 { USB_DEVICE(0x044e, 0x3002) },
112 /* Ericsson with non-standard id */
113 { USB_DEVICE(0x0bdb, 0x1002) },
115 /* Canyon CN-BTU1 with HID interfaces */
116 { USB_DEVICE(0x0c10, 0x0000) },
118 /* Broadcom BCM20702A0 */
119 { USB_DEVICE(0x413c, 0x8197) },
121 /* Broadcom BCM20702B0 (Dynex/Insignia) */
122 { USB_DEVICE(0x19ff, 0x0239), .driver_info = BTUSB_BCM_PATCHRAM },
124 /* Broadcom BCM43142A0 (Foxconn/Lenovo) */
125 { USB_VENDOR_AND_INTERFACE_INFO(0x105b, 0xff, 0x01, 0x01),
126 .driver_info = BTUSB_BCM_PATCHRAM },
128 /* Broadcom BCM920703 (HTC Vive) */
129 { USB_VENDOR_AND_INTERFACE_INFO(0x0bb4, 0xff, 0x01, 0x01),
130 .driver_info = BTUSB_BCM_PATCHRAM },
132 /* Foxconn - Hon Hai */
133 { USB_VENDOR_AND_INTERFACE_INFO(0x0489, 0xff, 0x01, 0x01),
134 .driver_info = BTUSB_BCM_PATCHRAM },
136 /* Lite-On Technology - Broadcom based */
137 { USB_VENDOR_AND_INTERFACE_INFO(0x04ca, 0xff, 0x01, 0x01),
138 .driver_info = BTUSB_BCM_PATCHRAM },
140 /* Broadcom devices with vendor specific id */
141 { USB_VENDOR_AND_INTERFACE_INFO(0x0a5c, 0xff, 0x01, 0x01),
142 .driver_info = BTUSB_BCM_PATCHRAM },
144 /* ASUSTek Computer - Broadcom based */
145 { USB_VENDOR_AND_INTERFACE_INFO(0x0b05, 0xff, 0x01, 0x01),
146 .driver_info = BTUSB_BCM_PATCHRAM },
148 /* Belkin F8065bf - Broadcom based */
149 { USB_VENDOR_AND_INTERFACE_INFO(0x050d, 0xff, 0x01, 0x01),
150 .driver_info = BTUSB_BCM_PATCHRAM },
152 /* IMC Networks - Broadcom based */
153 { USB_VENDOR_AND_INTERFACE_INFO(0x13d3, 0xff, 0x01, 0x01),
154 .driver_info = BTUSB_BCM_PATCHRAM },
156 /* Dell Computer - Broadcom based */
157 { USB_VENDOR_AND_INTERFACE_INFO(0x413c, 0xff, 0x01, 0x01),
158 .driver_info = BTUSB_BCM_PATCHRAM },
160 /* Toshiba Corp - Broadcom based */
161 { USB_VENDOR_AND_INTERFACE_INFO(0x0930, 0xff, 0x01, 0x01),
162 .driver_info = BTUSB_BCM_PATCHRAM },
164 /* Intel Bluetooth USB Bootloader (RAM module) */
165 { USB_DEVICE(0x8087, 0x0a5a),
166 .driver_info = BTUSB_INTEL_BOOT | BTUSB_BROKEN_ISOC },
168 { } /* Terminating entry */
171 MODULE_DEVICE_TABLE(usb, btusb_table);
173 static const struct usb_device_id blacklist_table[] = {
174 /* CSR BlueCore devices */
175 { USB_DEVICE(0x0a12, 0x0001), .driver_info = BTUSB_CSR },
177 /* Broadcom BCM2033 without firmware */
178 { USB_DEVICE(0x0a5c, 0x2033), .driver_info = BTUSB_IGNORE },
180 /* Broadcom BCM2045 devices */
181 { USB_DEVICE(0x0a5c, 0x2045), .driver_info = BTUSB_BCM2045 },
183 /* Atheros 3011 with sflash firmware */
184 { USB_DEVICE(0x0489, 0xe027), .driver_info = BTUSB_IGNORE },
185 { USB_DEVICE(0x0489, 0xe03d), .driver_info = BTUSB_IGNORE },
186 { USB_DEVICE(0x04f2, 0xaff1), .driver_info = BTUSB_IGNORE },
187 { USB_DEVICE(0x0930, 0x0215), .driver_info = BTUSB_IGNORE },
188 { USB_DEVICE(0x0cf3, 0x3002), .driver_info = BTUSB_IGNORE },
189 { USB_DEVICE(0x0cf3, 0xe019), .driver_info = BTUSB_IGNORE },
190 { USB_DEVICE(0x13d3, 0x3304), .driver_info = BTUSB_IGNORE },
192 /* Atheros AR9285 Malbec with sflash firmware */
193 { USB_DEVICE(0x03f0, 0x311d), .driver_info = BTUSB_IGNORE },
195 /* Atheros 3012 with sflash firmware */
196 { USB_DEVICE(0x0489, 0xe04d), .driver_info = BTUSB_ATH3012 },
197 { USB_DEVICE(0x0489, 0xe04e), .driver_info = BTUSB_ATH3012 },
198 { USB_DEVICE(0x0489, 0xe056), .driver_info = BTUSB_ATH3012 },
199 { USB_DEVICE(0x0489, 0xe057), .driver_info = BTUSB_ATH3012 },
200 { USB_DEVICE(0x0489, 0xe05f), .driver_info = BTUSB_ATH3012 },
201 { USB_DEVICE(0x0489, 0xe076), .driver_info = BTUSB_ATH3012 },
202 { USB_DEVICE(0x0489, 0xe078), .driver_info = BTUSB_ATH3012 },
203 { USB_DEVICE(0x0489, 0xe095), .driver_info = BTUSB_ATH3012 },
204 { USB_DEVICE(0x04c5, 0x1330), .driver_info = BTUSB_ATH3012 },
205 { USB_DEVICE(0x04ca, 0x3004), .driver_info = BTUSB_ATH3012 },
206 { USB_DEVICE(0x04ca, 0x3005), .driver_info = BTUSB_ATH3012 },
207 { USB_DEVICE(0x04ca, 0x3006), .driver_info = BTUSB_ATH3012 },
208 { USB_DEVICE(0x04ca, 0x3007), .driver_info = BTUSB_ATH3012 },
209 { USB_DEVICE(0x04ca, 0x3008), .driver_info = BTUSB_ATH3012 },
210 { USB_DEVICE(0x04ca, 0x300b), .driver_info = BTUSB_ATH3012 },
211 { USB_DEVICE(0x04ca, 0x300d), .driver_info = BTUSB_ATH3012 },
212 { USB_DEVICE(0x04ca, 0x300f), .driver_info = BTUSB_ATH3012 },
213 { USB_DEVICE(0x04ca, 0x3010), .driver_info = BTUSB_ATH3012 },
214 { USB_DEVICE(0x04ca, 0x3014), .driver_info = BTUSB_ATH3012 },
215 { USB_DEVICE(0x04ca, 0x3018), .driver_info = BTUSB_ATH3012 },
216 { USB_DEVICE(0x0930, 0x0219), .driver_info = BTUSB_ATH3012 },
217 { USB_DEVICE(0x0930, 0x021c), .driver_info = BTUSB_ATH3012 },
218 { USB_DEVICE(0x0930, 0x0220), .driver_info = BTUSB_ATH3012 },
219 { USB_DEVICE(0x0930, 0x0227), .driver_info = BTUSB_ATH3012 },
220 { USB_DEVICE(0x0b05, 0x17d0), .driver_info = BTUSB_ATH3012 },
221 { USB_DEVICE(0x0cf3, 0x0036), .driver_info = BTUSB_ATH3012 },
222 { USB_DEVICE(0x0cf3, 0x3004), .driver_info = BTUSB_ATH3012 },
223 { USB_DEVICE(0x0cf3, 0x3008), .driver_info = BTUSB_ATH3012 },
224 { USB_DEVICE(0x0cf3, 0x311d), .driver_info = BTUSB_ATH3012 },
225 { USB_DEVICE(0x0cf3, 0x311e), .driver_info = BTUSB_ATH3012 },
226 { USB_DEVICE(0x0cf3, 0x311f), .driver_info = BTUSB_ATH3012 },
227 { USB_DEVICE(0x0cf3, 0x3121), .driver_info = BTUSB_ATH3012 },
228 { USB_DEVICE(0x0cf3, 0x817a), .driver_info = BTUSB_ATH3012 },
229 { USB_DEVICE(0x0cf3, 0x817b), .driver_info = BTUSB_ATH3012 },
230 { USB_DEVICE(0x0cf3, 0xe003), .driver_info = BTUSB_ATH3012 },
231 { USB_DEVICE(0x0cf3, 0xe004), .driver_info = BTUSB_ATH3012 },
232 { USB_DEVICE(0x0cf3, 0xe005), .driver_info = BTUSB_ATH3012 },
233 { USB_DEVICE(0x0cf3, 0xe006), .driver_info = BTUSB_ATH3012 },
234 { USB_DEVICE(0x13d3, 0x3362), .driver_info = BTUSB_ATH3012 },
235 { USB_DEVICE(0x13d3, 0x3375), .driver_info = BTUSB_ATH3012 },
236 { USB_DEVICE(0x13d3, 0x3393), .driver_info = BTUSB_ATH3012 },
237 { USB_DEVICE(0x13d3, 0x3395), .driver_info = BTUSB_ATH3012 },
238 { USB_DEVICE(0x13d3, 0x3402), .driver_info = BTUSB_ATH3012 },
239 { USB_DEVICE(0x13d3, 0x3408), .driver_info = BTUSB_ATH3012 },
240 { USB_DEVICE(0x13d3, 0x3423), .driver_info = BTUSB_ATH3012 },
241 { USB_DEVICE(0x13d3, 0x3432), .driver_info = BTUSB_ATH3012 },
242 { USB_DEVICE(0x13d3, 0x3472), .driver_info = BTUSB_ATH3012 },
243 { USB_DEVICE(0x13d3, 0x3474), .driver_info = BTUSB_ATH3012 },
244 { USB_DEVICE(0x13d3, 0x3487), .driver_info = BTUSB_ATH3012 },
245 { USB_DEVICE(0x13d3, 0x3490), .driver_info = BTUSB_ATH3012 },
247 /* Atheros AR5BBU12 with sflash firmware */
248 { USB_DEVICE(0x0489, 0xe02c), .driver_info = BTUSB_IGNORE },
250 /* Atheros AR5BBU12 with sflash firmware */
251 { USB_DEVICE(0x0489, 0xe036), .driver_info = BTUSB_ATH3012 },
252 { USB_DEVICE(0x0489, 0xe03c), .driver_info = BTUSB_ATH3012 },
254 /* QCA ROME chipset */
255 { USB_DEVICE(0x0cf3, 0x535b), .driver_info = BTUSB_QCA_ROME },
256 { USB_DEVICE(0x0cf3, 0xe007), .driver_info = BTUSB_QCA_ROME },
257 { USB_DEVICE(0x0cf3, 0xe009), .driver_info = BTUSB_QCA_ROME },
258 { USB_DEVICE(0x0cf3, 0xe010), .driver_info = BTUSB_QCA_ROME },
259 { USB_DEVICE(0x0cf3, 0xe300), .driver_info = BTUSB_QCA_ROME },
260 { USB_DEVICE(0x0cf3, 0xe301), .driver_info = BTUSB_QCA_ROME },
261 { USB_DEVICE(0x0cf3, 0xe360), .driver_info = BTUSB_QCA_ROME },
262 { USB_DEVICE(0x0489, 0xe092), .driver_info = BTUSB_QCA_ROME },
263 { USB_DEVICE(0x0489, 0xe09f), .driver_info = BTUSB_QCA_ROME },
264 { USB_DEVICE(0x0489, 0xe0a2), .driver_info = BTUSB_QCA_ROME },
265 { USB_DEVICE(0x04ca, 0x3011), .driver_info = BTUSB_QCA_ROME },
266 { USB_DEVICE(0x04ca, 0x3015), .driver_info = BTUSB_QCA_ROME },
267 { USB_DEVICE(0x04ca, 0x3016), .driver_info = BTUSB_QCA_ROME },
268 { USB_DEVICE(0x04ca, 0x301a), .driver_info = BTUSB_QCA_ROME },
269 { USB_DEVICE(0x13d3, 0x3491), .driver_info = BTUSB_QCA_ROME },
270 { USB_DEVICE(0x13d3, 0x3496), .driver_info = BTUSB_QCA_ROME },
271 { USB_DEVICE(0x13d3, 0x3501), .driver_info = BTUSB_QCA_ROME },
273 /* Broadcom BCM2035 */
274 { USB_DEVICE(0x0a5c, 0x2009), .driver_info = BTUSB_BCM92035 },
275 { USB_DEVICE(0x0a5c, 0x200a), .driver_info = BTUSB_WRONG_SCO_MTU },
276 { USB_DEVICE(0x0a5c, 0x2035), .driver_info = BTUSB_WRONG_SCO_MTU },
278 /* Broadcom BCM2045 */
279 { USB_DEVICE(0x0a5c, 0x2039), .driver_info = BTUSB_WRONG_SCO_MTU },
280 { USB_DEVICE(0x0a5c, 0x2101), .driver_info = BTUSB_WRONG_SCO_MTU },
282 /* IBM/Lenovo ThinkPad with Broadcom chip */
283 { USB_DEVICE(0x0a5c, 0x201e), .driver_info = BTUSB_WRONG_SCO_MTU },
284 { USB_DEVICE(0x0a5c, 0x2110), .driver_info = BTUSB_WRONG_SCO_MTU },
286 /* HP laptop with Broadcom chip */
287 { USB_DEVICE(0x03f0, 0x171d), .driver_info = BTUSB_WRONG_SCO_MTU },
289 /* Dell laptop with Broadcom chip */
290 { USB_DEVICE(0x413c, 0x8126), .driver_info = BTUSB_WRONG_SCO_MTU },
292 /* Dell Wireless 370 and 410 devices */
293 { USB_DEVICE(0x413c, 0x8152), .driver_info = BTUSB_WRONG_SCO_MTU },
294 { USB_DEVICE(0x413c, 0x8156), .driver_info = BTUSB_WRONG_SCO_MTU },
296 /* Belkin F8T012 and F8T013 devices */
297 { USB_DEVICE(0x050d, 0x0012), .driver_info = BTUSB_WRONG_SCO_MTU },
298 { USB_DEVICE(0x050d, 0x0013), .driver_info = BTUSB_WRONG_SCO_MTU },
300 /* Asus WL-BTD202 device */
301 { USB_DEVICE(0x0b05, 0x1715), .driver_info = BTUSB_WRONG_SCO_MTU },
303 /* Kensington Bluetooth USB adapter */
304 { USB_DEVICE(0x047d, 0x105e), .driver_info = BTUSB_WRONG_SCO_MTU },
306 /* RTX Telecom based adapters with buggy SCO support */
307 { USB_DEVICE(0x0400, 0x0807), .driver_info = BTUSB_BROKEN_ISOC },
308 { USB_DEVICE(0x0400, 0x080a), .driver_info = BTUSB_BROKEN_ISOC },
310 /* CONWISE Technology based adapters with buggy SCO support */
311 { USB_DEVICE(0x0e5e, 0x6622),
312 .driver_info = BTUSB_BROKEN_ISOC | BTUSB_CW6622},
314 /* Roper Class 1 Bluetooth Dongle (Silicon Wave based) */
315 { USB_DEVICE(0x1310, 0x0001), .driver_info = BTUSB_SWAVE },
317 /* Digianswer devices */
318 { USB_DEVICE(0x08fd, 0x0001), .driver_info = BTUSB_DIGIANSWER },
319 { USB_DEVICE(0x08fd, 0x0002), .driver_info = BTUSB_IGNORE },
321 /* CSR BlueCore Bluetooth Sniffer */
322 { USB_DEVICE(0x0a12, 0x0002),
323 .driver_info = BTUSB_SNIFFER | BTUSB_BROKEN_ISOC },
325 /* Frontline ComProbe Bluetooth Sniffer */
326 { USB_DEVICE(0x16d3, 0x0002),
327 .driver_info = BTUSB_SNIFFER | BTUSB_BROKEN_ISOC },
329 /* Marvell Bluetooth devices */
330 { USB_DEVICE(0x1286, 0x2044), .driver_info = BTUSB_MARVELL },
331 { USB_DEVICE(0x1286, 0x2046), .driver_info = BTUSB_MARVELL },
332 { USB_DEVICE(0x1286, 0x204e), .driver_info = BTUSB_MARVELL },
334 /* Intel Bluetooth devices */
335 { USB_DEVICE(0x8087, 0x0025), .driver_info = BTUSB_INTEL_NEW },
336 { USB_DEVICE(0x8087, 0x0026), .driver_info = BTUSB_INTEL_NEW },
337 { USB_DEVICE(0x8087, 0x0029), .driver_info = BTUSB_INTEL_NEW },
338 { USB_DEVICE(0x8087, 0x07da), .driver_info = BTUSB_CSR },
339 { USB_DEVICE(0x8087, 0x07dc), .driver_info = BTUSB_INTEL },
340 { USB_DEVICE(0x8087, 0x0a2a), .driver_info = BTUSB_INTEL },
341 { USB_DEVICE(0x8087, 0x0a2b), .driver_info = BTUSB_INTEL_NEW },
342 { USB_DEVICE(0x8087, 0x0aa7), .driver_info = BTUSB_INTEL },
343 { USB_DEVICE(0x8087, 0x0aaa), .driver_info = BTUSB_INTEL_NEW },
345 /* Other Intel Bluetooth devices */
346 { USB_VENDOR_AND_INTERFACE_INFO(0x8087, 0xe0, 0x01, 0x01),
347 .driver_info = BTUSB_IGNORE },
349 /* Realtek Bluetooth devices */
350 { USB_VENDOR_AND_INTERFACE_INFO(0x0bda, 0xe0, 0x01, 0x01),
351 .driver_info = BTUSB_REALTEK },
353 /* MediaTek Bluetooth devices */
354 { USB_VENDOR_AND_INTERFACE_INFO(0x0e8d, 0xe0, 0x01, 0x01),
355 .driver_info = BTUSB_MEDIATEK },
357 /* Additional Realtek 8723AE Bluetooth devices */
358 { USB_DEVICE(0x0930, 0x021d), .driver_info = BTUSB_REALTEK },
359 { USB_DEVICE(0x13d3, 0x3394), .driver_info = BTUSB_REALTEK },
361 /* Additional Realtek 8723BE Bluetooth devices */
362 { USB_DEVICE(0x0489, 0xe085), .driver_info = BTUSB_REALTEK },
363 { USB_DEVICE(0x0489, 0xe08b), .driver_info = BTUSB_REALTEK },
364 { USB_DEVICE(0x13d3, 0x3410), .driver_info = BTUSB_REALTEK },
365 { USB_DEVICE(0x13d3, 0x3416), .driver_info = BTUSB_REALTEK },
366 { USB_DEVICE(0x13d3, 0x3459), .driver_info = BTUSB_REALTEK },
367 { USB_DEVICE(0x13d3, 0x3494), .driver_info = BTUSB_REALTEK },
369 /* Additional Realtek 8723BU Bluetooth devices */
370 { USB_DEVICE(0x7392, 0xa611), .driver_info = BTUSB_REALTEK },
372 /* Additional Realtek 8723DE Bluetooth devices */
373 { USB_DEVICE(0x0bda, 0xb009), .driver_info = BTUSB_REALTEK },
374 { USB_DEVICE(0x2ff8, 0xb011), .driver_info = BTUSB_REALTEK },
376 /* Additional Realtek 8821AE Bluetooth devices */
377 { USB_DEVICE(0x0b05, 0x17dc), .driver_info = BTUSB_REALTEK },
378 { USB_DEVICE(0x13d3, 0x3414), .driver_info = BTUSB_REALTEK },
379 { USB_DEVICE(0x13d3, 0x3458), .driver_info = BTUSB_REALTEK },
380 { USB_DEVICE(0x13d3, 0x3461), .driver_info = BTUSB_REALTEK },
381 { USB_DEVICE(0x13d3, 0x3462), .driver_info = BTUSB_REALTEK },
383 /* Additional Realtek 8822BE Bluetooth devices */
384 { USB_DEVICE(0x13d3, 0x3526), .driver_info = BTUSB_REALTEK },
385 { USB_DEVICE(0x0b05, 0x185c), .driver_info = BTUSB_REALTEK },
387 /* Additional Realtek 8822CE Bluetooth devices */
388 { USB_DEVICE(0x04ca, 0x4005), .driver_info = BTUSB_REALTEK },
390 /* Silicon Wave based devices */
391 { USB_DEVICE(0x0c10, 0x0000), .driver_info = BTUSB_SWAVE },
393 { } /* Terminating entry */
396 /* The Bluetooth USB module build into some devices needs to be reset on resume,
397 * this is a problem with the platform (likely shutting off all power) not with
398 * the module itself. So we use a DMI list to match known broken platforms.
400 static const struct dmi_system_id btusb_needs_reset_resume_table[] = {
402 /* Dell OptiPlex 3060 (QCA ROME device 0cf3:e007) */
404 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
405 DMI_MATCH(DMI_PRODUCT_NAME, "OptiPlex 3060"),
409 /* Dell XPS 9360 (QCA ROME device 0cf3:e300) */
411 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
412 DMI_MATCH(DMI_PRODUCT_NAME, "XPS 13 9360"),
416 /* Dell Inspiron 5565 (QCA ROME device 0cf3:e009) */
418 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
419 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 5565"),
425 #define BTUSB_MAX_ISOC_FRAMES 10
427 #define BTUSB_INTR_RUNNING 0
428 #define BTUSB_BULK_RUNNING 1
429 #define BTUSB_ISOC_RUNNING 2
430 #define BTUSB_SUSPENDING 3
431 #define BTUSB_DID_ISO_RESUME 4
432 #define BTUSB_BOOTLOADER 5
433 #define BTUSB_DOWNLOADING 6
434 #define BTUSB_FIRMWARE_LOADED 7
435 #define BTUSB_FIRMWARE_FAILED 8
436 #define BTUSB_BOOTING 9
437 #define BTUSB_DIAG_RUNNING 10
438 #define BTUSB_OOB_WAKE_ENABLED 11
439 #define BTUSB_HW_RESET_ACTIVE 12
440 #define BTUSB_TX_WAIT_VND_EVT 13
443 struct hci_dev *hdev;
444 struct usb_device *udev;
445 struct usb_interface *intf;
446 struct usb_interface *isoc;
447 struct usb_interface *diag;
452 struct work_struct work;
453 struct work_struct waker;
455 struct usb_anchor deferred;
456 struct usb_anchor tx_anchor;
460 struct usb_anchor intr_anchor;
461 struct usb_anchor bulk_anchor;
462 struct usb_anchor isoc_anchor;
463 struct usb_anchor diag_anchor;
464 struct usb_anchor ctrl_anchor;
467 struct sk_buff *evt_skb;
468 struct sk_buff *acl_skb;
469 struct sk_buff *sco_skb;
471 struct usb_endpoint_descriptor *intr_ep;
472 struct usb_endpoint_descriptor *bulk_tx_ep;
473 struct usb_endpoint_descriptor *bulk_rx_ep;
474 struct usb_endpoint_descriptor *isoc_tx_ep;
475 struct usb_endpoint_descriptor *isoc_rx_ep;
476 struct usb_endpoint_descriptor *diag_tx_ep;
477 struct usb_endpoint_descriptor *diag_rx_ep;
479 struct gpio_desc *reset_gpio;
484 unsigned int sco_num;
488 int (*recv_event)(struct hci_dev *hdev, struct sk_buff *skb);
489 int (*recv_bulk)(struct btusb_data *data, void *buffer, int count);
491 int (*setup_on_usb)(struct hci_dev *hdev);
493 int oob_wake_irq; /* irq for out-of-band wake-on-bt */
494 unsigned cmd_timeout_cnt;
498 static void btusb_intel_cmd_timeout(struct hci_dev *hdev)
500 struct btusb_data *data = hci_get_drvdata(hdev);
501 struct gpio_desc *reset_gpio = data->reset_gpio;
503 if (++data->cmd_timeout_cnt < 5)
507 bt_dev_err(hdev, "No way to reset. Ignoring and continuing");
512 * Toggle the hard reset line if the platform provides one. The reset
513 * is going to yank the device off the USB and then replug. So doing
514 * once is enough. The cleanup is handled correctly on the way out
515 * (standard USB disconnect), and the new device is detected cleanly
516 * and bound to the driver again like it should be.
518 if (test_and_set_bit(BTUSB_HW_RESET_ACTIVE, &data->flags)) {
519 bt_dev_err(hdev, "last reset failed? Not resetting again");
523 bt_dev_err(hdev, "Initiating HW reset via gpio");
524 gpiod_set_value_cansleep(reset_gpio, 1);
526 gpiod_set_value_cansleep(reset_gpio, 0);
529 static inline void btusb_free_frags(struct btusb_data *data)
533 spin_lock_irqsave(&data->rxlock, flags);
535 kfree_skb(data->evt_skb);
536 data->evt_skb = NULL;
538 kfree_skb(data->acl_skb);
539 data->acl_skb = NULL;
541 kfree_skb(data->sco_skb);
542 data->sco_skb = NULL;
544 spin_unlock_irqrestore(&data->rxlock, flags);
547 static int btusb_recv_intr(struct btusb_data *data, void *buffer, int count)
553 spin_lock_irqsave(&data->rxlock, flags);
560 skb = bt_skb_alloc(HCI_MAX_EVENT_SIZE, GFP_ATOMIC);
566 hci_skb_pkt_type(skb) = HCI_EVENT_PKT;
567 hci_skb_expect(skb) = HCI_EVENT_HDR_SIZE;
570 len = min_t(uint, hci_skb_expect(skb), count);
571 skb_put_data(skb, buffer, len);
575 hci_skb_expect(skb) -= len;
577 if (skb->len == HCI_EVENT_HDR_SIZE) {
578 /* Complete event header */
579 hci_skb_expect(skb) = hci_event_hdr(skb)->plen;
581 if (skb_tailroom(skb) < hci_skb_expect(skb)) {
590 if (!hci_skb_expect(skb)) {
592 data->recv_event(data->hdev, skb);
598 spin_unlock_irqrestore(&data->rxlock, flags);
603 static int btusb_recv_bulk(struct btusb_data *data, void *buffer, int count)
609 spin_lock_irqsave(&data->rxlock, flags);
616 skb = bt_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC);
622 hci_skb_pkt_type(skb) = HCI_ACLDATA_PKT;
623 hci_skb_expect(skb) = HCI_ACL_HDR_SIZE;
626 len = min_t(uint, hci_skb_expect(skb), count);
627 skb_put_data(skb, buffer, len);
631 hci_skb_expect(skb) -= len;
633 if (skb->len == HCI_ACL_HDR_SIZE) {
634 __le16 dlen = hci_acl_hdr(skb)->dlen;
636 /* Complete ACL header */
637 hci_skb_expect(skb) = __le16_to_cpu(dlen);
639 if (skb_tailroom(skb) < hci_skb_expect(skb)) {
648 if (!hci_skb_expect(skb)) {
650 hci_recv_frame(data->hdev, skb);
656 spin_unlock_irqrestore(&data->rxlock, flags);
661 static int btusb_recv_isoc(struct btusb_data *data, void *buffer, int count)
667 spin_lock_irqsave(&data->rxlock, flags);
674 skb = bt_skb_alloc(HCI_MAX_SCO_SIZE, GFP_ATOMIC);
680 hci_skb_pkt_type(skb) = HCI_SCODATA_PKT;
681 hci_skb_expect(skb) = HCI_SCO_HDR_SIZE;
684 len = min_t(uint, hci_skb_expect(skb), count);
685 skb_put_data(skb, buffer, len);
689 hci_skb_expect(skb) -= len;
691 if (skb->len == HCI_SCO_HDR_SIZE) {
692 /* Complete SCO header */
693 hci_skb_expect(skb) = hci_sco_hdr(skb)->dlen;
695 if (skb_tailroom(skb) < hci_skb_expect(skb)) {
704 if (!hci_skb_expect(skb)) {
706 hci_recv_frame(data->hdev, skb);
712 spin_unlock_irqrestore(&data->rxlock, flags);
717 static void btusb_intr_complete(struct urb *urb)
719 struct hci_dev *hdev = urb->context;
720 struct btusb_data *data = hci_get_drvdata(hdev);
723 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
726 if (!test_bit(HCI_RUNNING, &hdev->flags))
729 if (urb->status == 0) {
730 hdev->stat.byte_rx += urb->actual_length;
732 if (btusb_recv_intr(data, urb->transfer_buffer,
733 urb->actual_length) < 0) {
734 bt_dev_err(hdev, "corrupted event packet");
737 } else if (urb->status == -ENOENT) {
738 /* Avoid suspend failed when usb_kill_urb */
742 if (!test_bit(BTUSB_INTR_RUNNING, &data->flags))
745 usb_mark_last_busy(data->udev);
746 usb_anchor_urb(urb, &data->intr_anchor);
748 err = usb_submit_urb(urb, GFP_ATOMIC);
750 /* -EPERM: urb is being killed;
751 * -ENODEV: device got disconnected
753 if (err != -EPERM && err != -ENODEV)
754 bt_dev_err(hdev, "urb %p failed to resubmit (%d)",
756 usb_unanchor_urb(urb);
760 static int btusb_submit_intr_urb(struct hci_dev *hdev, gfp_t mem_flags)
762 struct btusb_data *data = hci_get_drvdata(hdev);
768 BT_DBG("%s", hdev->name);
773 urb = usb_alloc_urb(0, mem_flags);
777 size = le16_to_cpu(data->intr_ep->wMaxPacketSize);
779 buf = kmalloc(size, mem_flags);
785 pipe = usb_rcvintpipe(data->udev, data->intr_ep->bEndpointAddress);
787 usb_fill_int_urb(urb, data->udev, pipe, buf, size,
788 btusb_intr_complete, hdev, data->intr_ep->bInterval);
790 urb->transfer_flags |= URB_FREE_BUFFER;
792 usb_anchor_urb(urb, &data->intr_anchor);
794 err = usb_submit_urb(urb, mem_flags);
796 if (err != -EPERM && err != -ENODEV)
797 bt_dev_err(hdev, "urb %p submission failed (%d)",
799 usb_unanchor_urb(urb);
807 static void btusb_bulk_complete(struct urb *urb)
809 struct hci_dev *hdev = urb->context;
810 struct btusb_data *data = hci_get_drvdata(hdev);
813 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
816 if (!test_bit(HCI_RUNNING, &hdev->flags))
819 if (urb->status == 0) {
820 hdev->stat.byte_rx += urb->actual_length;
822 if (data->recv_bulk(data, urb->transfer_buffer,
823 urb->actual_length) < 0) {
824 bt_dev_err(hdev, "corrupted ACL packet");
827 } else if (urb->status == -ENOENT) {
828 /* Avoid suspend failed when usb_kill_urb */
832 if (!test_bit(BTUSB_BULK_RUNNING, &data->flags))
835 usb_anchor_urb(urb, &data->bulk_anchor);
836 usb_mark_last_busy(data->udev);
838 err = usb_submit_urb(urb, GFP_ATOMIC);
840 /* -EPERM: urb is being killed;
841 * -ENODEV: device got disconnected
843 if (err != -EPERM && err != -ENODEV)
844 bt_dev_err(hdev, "urb %p failed to resubmit (%d)",
846 usb_unanchor_urb(urb);
850 static int btusb_submit_bulk_urb(struct hci_dev *hdev, gfp_t mem_flags)
852 struct btusb_data *data = hci_get_drvdata(hdev);
856 int err, size = HCI_MAX_FRAME_SIZE;
858 BT_DBG("%s", hdev->name);
860 if (!data->bulk_rx_ep)
863 urb = usb_alloc_urb(0, mem_flags);
867 buf = kmalloc(size, mem_flags);
873 pipe = usb_rcvbulkpipe(data->udev, data->bulk_rx_ep->bEndpointAddress);
875 usb_fill_bulk_urb(urb, data->udev, pipe, buf, size,
876 btusb_bulk_complete, hdev);
878 urb->transfer_flags |= URB_FREE_BUFFER;
880 usb_mark_last_busy(data->udev);
881 usb_anchor_urb(urb, &data->bulk_anchor);
883 err = usb_submit_urb(urb, mem_flags);
885 if (err != -EPERM && err != -ENODEV)
886 bt_dev_err(hdev, "urb %p submission failed (%d)",
888 usb_unanchor_urb(urb);
896 static void btusb_isoc_complete(struct urb *urb)
898 struct hci_dev *hdev = urb->context;
899 struct btusb_data *data = hci_get_drvdata(hdev);
902 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
905 if (!test_bit(HCI_RUNNING, &hdev->flags))
908 if (urb->status == 0) {
909 for (i = 0; i < urb->number_of_packets; i++) {
910 unsigned int offset = urb->iso_frame_desc[i].offset;
911 unsigned int length = urb->iso_frame_desc[i].actual_length;
913 if (urb->iso_frame_desc[i].status)
916 hdev->stat.byte_rx += length;
918 if (btusb_recv_isoc(data, urb->transfer_buffer + offset,
920 bt_dev_err(hdev, "corrupted SCO packet");
924 } else if (urb->status == -ENOENT) {
925 /* Avoid suspend failed when usb_kill_urb */
929 if (!test_bit(BTUSB_ISOC_RUNNING, &data->flags))
932 usb_anchor_urb(urb, &data->isoc_anchor);
934 err = usb_submit_urb(urb, GFP_ATOMIC);
936 /* -EPERM: urb is being killed;
937 * -ENODEV: device got disconnected
939 if (err != -EPERM && err != -ENODEV)
940 bt_dev_err(hdev, "urb %p failed to resubmit (%d)",
942 usb_unanchor_urb(urb);
946 static inline void __fill_isoc_descriptor(struct urb *urb, int len, int mtu)
950 BT_DBG("len %d mtu %d", len, mtu);
952 for (i = 0; i < BTUSB_MAX_ISOC_FRAMES && len >= mtu;
953 i++, offset += mtu, len -= mtu) {
954 urb->iso_frame_desc[i].offset = offset;
955 urb->iso_frame_desc[i].length = mtu;
958 if (len && i < BTUSB_MAX_ISOC_FRAMES) {
959 urb->iso_frame_desc[i].offset = offset;
960 urb->iso_frame_desc[i].length = len;
964 urb->number_of_packets = i;
967 static int btusb_submit_isoc_urb(struct hci_dev *hdev, gfp_t mem_flags)
969 struct btusb_data *data = hci_get_drvdata(hdev);
975 BT_DBG("%s", hdev->name);
977 if (!data->isoc_rx_ep)
980 urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, mem_flags);
984 size = le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize) *
985 BTUSB_MAX_ISOC_FRAMES;
987 buf = kmalloc(size, mem_flags);
993 pipe = usb_rcvisocpipe(data->udev, data->isoc_rx_ep->bEndpointAddress);
995 usb_fill_int_urb(urb, data->udev, pipe, buf, size, btusb_isoc_complete,
996 hdev, data->isoc_rx_ep->bInterval);
998 urb->transfer_flags = URB_FREE_BUFFER | URB_ISO_ASAP;
1000 __fill_isoc_descriptor(urb, size,
1001 le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize));
1003 usb_anchor_urb(urb, &data->isoc_anchor);
1005 err = usb_submit_urb(urb, mem_flags);
1007 if (err != -EPERM && err != -ENODEV)
1008 bt_dev_err(hdev, "urb %p submission failed (%d)",
1010 usb_unanchor_urb(urb);
1018 static void btusb_diag_complete(struct urb *urb)
1020 struct hci_dev *hdev = urb->context;
1021 struct btusb_data *data = hci_get_drvdata(hdev);
1024 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
1025 urb->actual_length);
1027 if (urb->status == 0) {
1028 struct sk_buff *skb;
1030 skb = bt_skb_alloc(urb->actual_length, GFP_ATOMIC);
1032 skb_put_data(skb, urb->transfer_buffer,
1033 urb->actual_length);
1034 hci_recv_diag(hdev, skb);
1036 } else if (urb->status == -ENOENT) {
1037 /* Avoid suspend failed when usb_kill_urb */
1041 if (!test_bit(BTUSB_DIAG_RUNNING, &data->flags))
1044 usb_anchor_urb(urb, &data->diag_anchor);
1045 usb_mark_last_busy(data->udev);
1047 err = usb_submit_urb(urb, GFP_ATOMIC);
1049 /* -EPERM: urb is being killed;
1050 * -ENODEV: device got disconnected
1052 if (err != -EPERM && err != -ENODEV)
1053 bt_dev_err(hdev, "urb %p failed to resubmit (%d)",
1055 usb_unanchor_urb(urb);
1059 static int btusb_submit_diag_urb(struct hci_dev *hdev, gfp_t mem_flags)
1061 struct btusb_data *data = hci_get_drvdata(hdev);
1065 int err, size = HCI_MAX_FRAME_SIZE;
1067 BT_DBG("%s", hdev->name);
1069 if (!data->diag_rx_ep)
1072 urb = usb_alloc_urb(0, mem_flags);
1076 buf = kmalloc(size, mem_flags);
1082 pipe = usb_rcvbulkpipe(data->udev, data->diag_rx_ep->bEndpointAddress);
1084 usb_fill_bulk_urb(urb, data->udev, pipe, buf, size,
1085 btusb_diag_complete, hdev);
1087 urb->transfer_flags |= URB_FREE_BUFFER;
1089 usb_mark_last_busy(data->udev);
1090 usb_anchor_urb(urb, &data->diag_anchor);
1092 err = usb_submit_urb(urb, mem_flags);
1094 if (err != -EPERM && err != -ENODEV)
1095 bt_dev_err(hdev, "urb %p submission failed (%d)",
1097 usb_unanchor_urb(urb);
1105 static void btusb_tx_complete(struct urb *urb)
1107 struct sk_buff *skb = urb->context;
1108 struct hci_dev *hdev = (struct hci_dev *)skb->dev;
1109 struct btusb_data *data = hci_get_drvdata(hdev);
1110 unsigned long flags;
1112 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
1113 urb->actual_length);
1115 if (!test_bit(HCI_RUNNING, &hdev->flags))
1119 hdev->stat.byte_tx += urb->transfer_buffer_length;
1121 hdev->stat.err_tx++;
1124 spin_lock_irqsave(&data->txlock, flags);
1125 data->tx_in_flight--;
1126 spin_unlock_irqrestore(&data->txlock, flags);
1128 kfree(urb->setup_packet);
1133 static void btusb_isoc_tx_complete(struct urb *urb)
1135 struct sk_buff *skb = urb->context;
1136 struct hci_dev *hdev = (struct hci_dev *)skb->dev;
1138 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
1139 urb->actual_length);
1141 if (!test_bit(HCI_RUNNING, &hdev->flags))
1145 hdev->stat.byte_tx += urb->transfer_buffer_length;
1147 hdev->stat.err_tx++;
1150 kfree(urb->setup_packet);
1155 static int btusb_open(struct hci_dev *hdev)
1157 struct btusb_data *data = hci_get_drvdata(hdev);
1160 BT_DBG("%s", hdev->name);
1162 err = usb_autopm_get_interface(data->intf);
1166 /* Patching USB firmware files prior to starting any URBs of HCI path
1167 * It is more safe to use USB bulk channel for downloading USB patch
1169 if (data->setup_on_usb) {
1170 err = data->setup_on_usb(hdev);
1175 data->intf->needs_remote_wakeup = 1;
1177 if (test_and_set_bit(BTUSB_INTR_RUNNING, &data->flags))
1180 err = btusb_submit_intr_urb(hdev, GFP_KERNEL);
1184 err = btusb_submit_bulk_urb(hdev, GFP_KERNEL);
1186 usb_kill_anchored_urbs(&data->intr_anchor);
1190 set_bit(BTUSB_BULK_RUNNING, &data->flags);
1191 btusb_submit_bulk_urb(hdev, GFP_KERNEL);
1194 if (!btusb_submit_diag_urb(hdev, GFP_KERNEL))
1195 set_bit(BTUSB_DIAG_RUNNING, &data->flags);
1199 usb_autopm_put_interface(data->intf);
1203 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
1204 usb_autopm_put_interface(data->intf);
1208 static void btusb_stop_traffic(struct btusb_data *data)
1210 usb_kill_anchored_urbs(&data->intr_anchor);
1211 usb_kill_anchored_urbs(&data->bulk_anchor);
1212 usb_kill_anchored_urbs(&data->isoc_anchor);
1213 usb_kill_anchored_urbs(&data->diag_anchor);
1214 usb_kill_anchored_urbs(&data->ctrl_anchor);
1217 static int btusb_close(struct hci_dev *hdev)
1219 struct btusb_data *data = hci_get_drvdata(hdev);
1222 BT_DBG("%s", hdev->name);
1224 cancel_work_sync(&data->work);
1225 cancel_work_sync(&data->waker);
1227 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1228 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
1229 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
1230 clear_bit(BTUSB_DIAG_RUNNING, &data->flags);
1232 btusb_stop_traffic(data);
1233 btusb_free_frags(data);
1235 err = usb_autopm_get_interface(data->intf);
1239 data->intf->needs_remote_wakeup = 0;
1240 usb_autopm_put_interface(data->intf);
1243 usb_scuttle_anchored_urbs(&data->deferred);
1247 static int btusb_flush(struct hci_dev *hdev)
1249 struct btusb_data *data = hci_get_drvdata(hdev);
1251 BT_DBG("%s", hdev->name);
1253 usb_kill_anchored_urbs(&data->tx_anchor);
1254 btusb_free_frags(data);
1259 static struct urb *alloc_ctrl_urb(struct hci_dev *hdev, struct sk_buff *skb)
1261 struct btusb_data *data = hci_get_drvdata(hdev);
1262 struct usb_ctrlrequest *dr;
1266 urb = usb_alloc_urb(0, GFP_KERNEL);
1268 return ERR_PTR(-ENOMEM);
1270 dr = kmalloc(sizeof(*dr), GFP_KERNEL);
1273 return ERR_PTR(-ENOMEM);
1276 dr->bRequestType = data->cmdreq_type;
1277 dr->bRequest = data->cmdreq;
1280 dr->wLength = __cpu_to_le16(skb->len);
1282 pipe = usb_sndctrlpipe(data->udev, 0x00);
1284 usb_fill_control_urb(urb, data->udev, pipe, (void *)dr,
1285 skb->data, skb->len, btusb_tx_complete, skb);
1287 skb->dev = (void *)hdev;
1292 static struct urb *alloc_bulk_urb(struct hci_dev *hdev, struct sk_buff *skb)
1294 struct btusb_data *data = hci_get_drvdata(hdev);
1298 if (!data->bulk_tx_ep)
1299 return ERR_PTR(-ENODEV);
1301 urb = usb_alloc_urb(0, GFP_KERNEL);
1303 return ERR_PTR(-ENOMEM);
1305 pipe = usb_sndbulkpipe(data->udev, data->bulk_tx_ep->bEndpointAddress);
1307 usb_fill_bulk_urb(urb, data->udev, pipe,
1308 skb->data, skb->len, btusb_tx_complete, skb);
1310 skb->dev = (void *)hdev;
1315 static struct urb *alloc_isoc_urb(struct hci_dev *hdev, struct sk_buff *skb)
1317 struct btusb_data *data = hci_get_drvdata(hdev);
1321 if (!data->isoc_tx_ep)
1322 return ERR_PTR(-ENODEV);
1324 urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, GFP_KERNEL);
1326 return ERR_PTR(-ENOMEM);
1328 pipe = usb_sndisocpipe(data->udev, data->isoc_tx_ep->bEndpointAddress);
1330 usb_fill_int_urb(urb, data->udev, pipe,
1331 skb->data, skb->len, btusb_isoc_tx_complete,
1332 skb, data->isoc_tx_ep->bInterval);
1334 urb->transfer_flags = URB_ISO_ASAP;
1336 __fill_isoc_descriptor(urb, skb->len,
1337 le16_to_cpu(data->isoc_tx_ep->wMaxPacketSize));
1339 skb->dev = (void *)hdev;
1344 static int submit_tx_urb(struct hci_dev *hdev, struct urb *urb)
1346 struct btusb_data *data = hci_get_drvdata(hdev);
1349 usb_anchor_urb(urb, &data->tx_anchor);
1351 err = usb_submit_urb(urb, GFP_KERNEL);
1353 if (err != -EPERM && err != -ENODEV)
1354 bt_dev_err(hdev, "urb %p submission failed (%d)",
1356 kfree(urb->setup_packet);
1357 usb_unanchor_urb(urb);
1359 usb_mark_last_busy(data->udev);
1366 static int submit_or_queue_tx_urb(struct hci_dev *hdev, struct urb *urb)
1368 struct btusb_data *data = hci_get_drvdata(hdev);
1369 unsigned long flags;
1372 spin_lock_irqsave(&data->txlock, flags);
1373 suspending = test_bit(BTUSB_SUSPENDING, &data->flags);
1375 data->tx_in_flight++;
1376 spin_unlock_irqrestore(&data->txlock, flags);
1379 return submit_tx_urb(hdev, urb);
1381 usb_anchor_urb(urb, &data->deferred);
1382 schedule_work(&data->waker);
1388 static int btusb_send_frame(struct hci_dev *hdev, struct sk_buff *skb)
1392 BT_DBG("%s", hdev->name);
1394 switch (hci_skb_pkt_type(skb)) {
1395 case HCI_COMMAND_PKT:
1396 urb = alloc_ctrl_urb(hdev, skb);
1398 return PTR_ERR(urb);
1400 hdev->stat.cmd_tx++;
1401 return submit_or_queue_tx_urb(hdev, urb);
1403 case HCI_ACLDATA_PKT:
1404 urb = alloc_bulk_urb(hdev, skb);
1406 return PTR_ERR(urb);
1408 hdev->stat.acl_tx++;
1409 return submit_or_queue_tx_urb(hdev, urb);
1411 case HCI_SCODATA_PKT:
1412 if (hci_conn_num(hdev, SCO_LINK) < 1)
1415 urb = alloc_isoc_urb(hdev, skb);
1417 return PTR_ERR(urb);
1419 hdev->stat.sco_tx++;
1420 return submit_tx_urb(hdev, urb);
1426 static void btusb_notify(struct hci_dev *hdev, unsigned int evt)
1428 struct btusb_data *data = hci_get_drvdata(hdev);
1430 BT_DBG("%s evt %d", hdev->name, evt);
1432 if (hci_conn_num(hdev, SCO_LINK) != data->sco_num) {
1433 data->sco_num = hci_conn_num(hdev, SCO_LINK);
1434 schedule_work(&data->work);
1438 static inline int __set_isoc_interface(struct hci_dev *hdev, int altsetting)
1440 struct btusb_data *data = hci_get_drvdata(hdev);
1441 struct usb_interface *intf = data->isoc;
1442 struct usb_endpoint_descriptor *ep_desc;
1448 err = usb_set_interface(data->udev, data->isoc_ifnum, altsetting);
1450 bt_dev_err(hdev, "setting interface failed (%d)", -err);
1454 data->isoc_altsetting = altsetting;
1456 data->isoc_tx_ep = NULL;
1457 data->isoc_rx_ep = NULL;
1459 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
1460 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
1462 if (!data->isoc_tx_ep && usb_endpoint_is_isoc_out(ep_desc)) {
1463 data->isoc_tx_ep = ep_desc;
1467 if (!data->isoc_rx_ep && usb_endpoint_is_isoc_in(ep_desc)) {
1468 data->isoc_rx_ep = ep_desc;
1473 if (!data->isoc_tx_ep || !data->isoc_rx_ep) {
1474 bt_dev_err(hdev, "invalid SCO descriptors");
1481 static void btusb_work(struct work_struct *work)
1483 struct btusb_data *data = container_of(work, struct btusb_data, work);
1484 struct hci_dev *hdev = data->hdev;
1488 if (data->sco_num > 0) {
1489 if (!test_bit(BTUSB_DID_ISO_RESUME, &data->flags)) {
1490 err = usb_autopm_get_interface(data->isoc ? data->isoc : data->intf);
1492 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1493 usb_kill_anchored_urbs(&data->isoc_anchor);
1497 set_bit(BTUSB_DID_ISO_RESUME, &data->flags);
1500 if (hdev->voice_setting & 0x0020) {
1501 static const int alts[3] = { 2, 4, 5 };
1503 new_alts = alts[data->sco_num - 1];
1505 new_alts = data->sco_num;
1508 if (data->isoc_altsetting != new_alts) {
1509 unsigned long flags;
1511 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1512 usb_kill_anchored_urbs(&data->isoc_anchor);
1514 /* When isochronous alternate setting needs to be
1515 * changed, because SCO connection has been added
1516 * or removed, a packet fragment may be left in the
1517 * reassembling state. This could lead to wrongly
1518 * assembled fragments.
1520 * Clear outstanding fragment when selecting a new
1521 * alternate setting.
1523 spin_lock_irqsave(&data->rxlock, flags);
1524 kfree_skb(data->sco_skb);
1525 data->sco_skb = NULL;
1526 spin_unlock_irqrestore(&data->rxlock, flags);
1528 if (__set_isoc_interface(hdev, new_alts) < 0)
1532 if (!test_and_set_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
1533 if (btusb_submit_isoc_urb(hdev, GFP_KERNEL) < 0)
1534 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1536 btusb_submit_isoc_urb(hdev, GFP_KERNEL);
1539 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1540 usb_kill_anchored_urbs(&data->isoc_anchor);
1542 __set_isoc_interface(hdev, 0);
1543 if (test_and_clear_bit(BTUSB_DID_ISO_RESUME, &data->flags))
1544 usb_autopm_put_interface(data->isoc ? data->isoc : data->intf);
1548 static void btusb_waker(struct work_struct *work)
1550 struct btusb_data *data = container_of(work, struct btusb_data, waker);
1553 err = usb_autopm_get_interface(data->intf);
1557 usb_autopm_put_interface(data->intf);
1560 static int btusb_setup_bcm92035(struct hci_dev *hdev)
1562 struct sk_buff *skb;
1565 BT_DBG("%s", hdev->name);
1567 skb = __hci_cmd_sync(hdev, 0xfc3b, 1, &val, HCI_INIT_TIMEOUT);
1569 bt_dev_err(hdev, "BCM92035 command failed (%ld)", PTR_ERR(skb));
1576 static int btusb_setup_csr(struct hci_dev *hdev)
1578 struct hci_rp_read_local_version *rp;
1579 struct sk_buff *skb;
1581 BT_DBG("%s", hdev->name);
1583 skb = __hci_cmd_sync(hdev, HCI_OP_READ_LOCAL_VERSION, 0, NULL,
1586 int err = PTR_ERR(skb);
1587 bt_dev_err(hdev, "CSR: Local version failed (%d)", err);
1591 if (skb->len != sizeof(struct hci_rp_read_local_version)) {
1592 bt_dev_err(hdev, "CSR: Local version length mismatch");
1597 rp = (struct hci_rp_read_local_version *)skb->data;
1599 /* Detect controllers which aren't real CSR ones. */
1600 if (le16_to_cpu(rp->manufacturer) != 10 ||
1601 le16_to_cpu(rp->lmp_subver) == 0x0c5c) {
1602 /* Clear the reset quirk since this is not an actual
1603 * early Bluetooth 1.1 device from CSR.
1605 clear_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
1607 /* These fake CSR controllers have all a broken
1608 * stored link key handling and so just disable it.
1610 set_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks);
1618 static const struct firmware *btusb_setup_intel_get_fw(struct hci_dev *hdev,
1619 struct intel_version *ver)
1621 const struct firmware *fw;
1625 snprintf(fwname, sizeof(fwname),
1626 "intel/ibt-hw-%x.%x.%x-fw-%x.%x.%x.%x.%x.bseq",
1627 ver->hw_platform, ver->hw_variant, ver->hw_revision,
1628 ver->fw_variant, ver->fw_revision, ver->fw_build_num,
1629 ver->fw_build_ww, ver->fw_build_yy);
1631 ret = request_firmware(&fw, fwname, &hdev->dev);
1633 if (ret == -EINVAL) {
1634 bt_dev_err(hdev, "Intel firmware file request failed (%d)",
1639 bt_dev_err(hdev, "failed to open Intel firmware file: %s (%d)",
1642 /* If the correct firmware patch file is not found, use the
1643 * default firmware patch file instead
1645 snprintf(fwname, sizeof(fwname), "intel/ibt-hw-%x.%x.bseq",
1646 ver->hw_platform, ver->hw_variant);
1647 if (request_firmware(&fw, fwname, &hdev->dev) < 0) {
1648 bt_dev_err(hdev, "failed to open default fw file: %s",
1654 bt_dev_info(hdev, "Intel Bluetooth firmware file: %s", fwname);
1659 static int btusb_setup_intel_patching(struct hci_dev *hdev,
1660 const struct firmware *fw,
1661 const u8 **fw_ptr, int *disable_patch)
1663 struct sk_buff *skb;
1664 struct hci_command_hdr *cmd;
1665 const u8 *cmd_param;
1666 struct hci_event_hdr *evt = NULL;
1667 const u8 *evt_param = NULL;
1668 int remain = fw->size - (*fw_ptr - fw->data);
1670 /* The first byte indicates the types of the patch command or event.
1671 * 0x01 means HCI command and 0x02 is HCI event. If the first bytes
1672 * in the current firmware buffer doesn't start with 0x01 or
1673 * the size of remain buffer is smaller than HCI command header,
1674 * the firmware file is corrupted and it should stop the patching
1677 if (remain > HCI_COMMAND_HDR_SIZE && *fw_ptr[0] != 0x01) {
1678 bt_dev_err(hdev, "Intel fw corrupted: invalid cmd read");
1684 cmd = (struct hci_command_hdr *)(*fw_ptr);
1685 *fw_ptr += sizeof(*cmd);
1686 remain -= sizeof(*cmd);
1688 /* Ensure that the remain firmware data is long enough than the length
1689 * of command parameter. If not, the firmware file is corrupted.
1691 if (remain < cmd->plen) {
1692 bt_dev_err(hdev, "Intel fw corrupted: invalid cmd len");
1696 /* If there is a command that loads a patch in the firmware
1697 * file, then enable the patch upon success, otherwise just
1698 * disable the manufacturer mode, for example patch activation
1699 * is not required when the default firmware patch file is used
1700 * because there are no patch data to load.
1702 if (*disable_patch && le16_to_cpu(cmd->opcode) == 0xfc8e)
1705 cmd_param = *fw_ptr;
1706 *fw_ptr += cmd->plen;
1707 remain -= cmd->plen;
1709 /* This reads the expected events when the above command is sent to the
1710 * device. Some vendor commands expects more than one events, for
1711 * example command status event followed by vendor specific event.
1712 * For this case, it only keeps the last expected event. so the command
1713 * can be sent with __hci_cmd_sync_ev() which returns the sk_buff of
1714 * last expected event.
1716 while (remain > HCI_EVENT_HDR_SIZE && *fw_ptr[0] == 0x02) {
1720 evt = (struct hci_event_hdr *)(*fw_ptr);
1721 *fw_ptr += sizeof(*evt);
1722 remain -= sizeof(*evt);
1724 if (remain < evt->plen) {
1725 bt_dev_err(hdev, "Intel fw corrupted: invalid evt len");
1729 evt_param = *fw_ptr;
1730 *fw_ptr += evt->plen;
1731 remain -= evt->plen;
1734 /* Every HCI commands in the firmware file has its correspond event.
1735 * If event is not found or remain is smaller than zero, the firmware
1736 * file is corrupted.
1738 if (!evt || !evt_param || remain < 0) {
1739 bt_dev_err(hdev, "Intel fw corrupted: invalid evt read");
1743 skb = __hci_cmd_sync_ev(hdev, le16_to_cpu(cmd->opcode), cmd->plen,
1744 cmd_param, evt->evt, HCI_INIT_TIMEOUT);
1746 bt_dev_err(hdev, "sending Intel patch command (0x%4.4x) failed (%ld)",
1747 cmd->opcode, PTR_ERR(skb));
1748 return PTR_ERR(skb);
1751 /* It ensures that the returned event matches the event data read from
1752 * the firmware file. At fist, it checks the length and then
1753 * the contents of the event.
1755 if (skb->len != evt->plen) {
1756 bt_dev_err(hdev, "mismatch event length (opcode 0x%4.4x)",
1757 le16_to_cpu(cmd->opcode));
1762 if (memcmp(skb->data, evt_param, evt->plen)) {
1763 bt_dev_err(hdev, "mismatch event parameter (opcode 0x%4.4x)",
1764 le16_to_cpu(cmd->opcode));
1773 static int btusb_setup_intel(struct hci_dev *hdev)
1775 struct sk_buff *skb;
1776 const struct firmware *fw;
1778 int disable_patch, err;
1779 struct intel_version ver;
1781 BT_DBG("%s", hdev->name);
1783 /* The controller has a bug with the first HCI command sent to it
1784 * returning number of completed commands as zero. This would stall the
1785 * command processing in the Bluetooth core.
1787 * As a workaround, send HCI Reset command first which will reset the
1788 * number of completed commands and allow normal command processing
1791 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
1793 bt_dev_err(hdev, "sending initial HCI reset command failed (%ld)",
1795 return PTR_ERR(skb);
1799 /* Read Intel specific controller version first to allow selection of
1800 * which firmware file to load.
1802 * The returned information are hardware variant and revision plus
1803 * firmware variant, revision and build number.
1805 err = btintel_read_version(hdev, &ver);
1809 bt_dev_info(hdev, "read Intel version: %02x%02x%02x%02x%02x%02x%02x%02x%02x",
1810 ver.hw_platform, ver.hw_variant, ver.hw_revision,
1811 ver.fw_variant, ver.fw_revision, ver.fw_build_num,
1812 ver.fw_build_ww, ver.fw_build_yy, ver.fw_patch_num);
1814 /* fw_patch_num indicates the version of patch the device currently
1815 * have. If there is no patch data in the device, it is always 0x00.
1816 * So, if it is other than 0x00, no need to patch the device again.
1818 if (ver.fw_patch_num) {
1819 bt_dev_info(hdev, "Intel device is already patched. "
1820 "patch num: %02x", ver.fw_patch_num);
1824 /* Opens the firmware patch file based on the firmware version read
1825 * from the controller. If it fails to open the matching firmware
1826 * patch file, it tries to open the default firmware patch file.
1827 * If no patch file is found, allow the device to operate without
1830 fw = btusb_setup_intel_get_fw(hdev, &ver);
1835 /* Enable the manufacturer mode of the controller.
1836 * Only while this mode is enabled, the driver can download the
1837 * firmware patch data and configuration parameters.
1839 err = btintel_enter_mfg(hdev);
1841 release_firmware(fw);
1847 /* The firmware data file consists of list of Intel specific HCI
1848 * commands and its expected events. The first byte indicates the
1849 * type of the message, either HCI command or HCI event.
1851 * It reads the command and its expected event from the firmware file,
1852 * and send to the controller. Once __hci_cmd_sync_ev() returns,
1853 * the returned event is compared with the event read from the firmware
1854 * file and it will continue until all the messages are downloaded to
1857 * Once the firmware patching is completed successfully,
1858 * the manufacturer mode is disabled with reset and activating the
1861 * If the firmware patching fails, the manufacturer mode is
1862 * disabled with reset and deactivating the patch.
1864 * If the default patch file is used, no reset is done when disabling
1867 while (fw->size > fw_ptr - fw->data) {
1870 ret = btusb_setup_intel_patching(hdev, fw, &fw_ptr,
1873 goto exit_mfg_deactivate;
1876 release_firmware(fw);
1879 goto exit_mfg_disable;
1881 /* Patching completed successfully and disable the manufacturer mode
1882 * with reset and activate the downloaded firmware patches.
1884 err = btintel_exit_mfg(hdev, true, true);
1888 bt_dev_info(hdev, "Intel firmware patch completed and activated");
1893 /* Disable the manufacturer mode without reset */
1894 err = btintel_exit_mfg(hdev, false, false);
1898 bt_dev_info(hdev, "Intel firmware patch completed");
1902 exit_mfg_deactivate:
1903 release_firmware(fw);
1905 /* Patching failed. Disable the manufacturer mode with reset and
1906 * deactivate the downloaded firmware patches.
1908 err = btintel_exit_mfg(hdev, true, false);
1912 bt_dev_info(hdev, "Intel firmware patch completed and deactivated");
1915 /* Set the event mask for Intel specific vendor events. This enables
1916 * a few extra events that are useful during general operation.
1918 btintel_set_event_mask_mfg(hdev, false);
1920 btintel_check_bdaddr(hdev);
1924 static int inject_cmd_complete(struct hci_dev *hdev, __u16 opcode)
1926 struct sk_buff *skb;
1927 struct hci_event_hdr *hdr;
1928 struct hci_ev_cmd_complete *evt;
1930 skb = bt_skb_alloc(sizeof(*hdr) + sizeof(*evt) + 1, GFP_KERNEL);
1934 hdr = skb_put(skb, sizeof(*hdr));
1935 hdr->evt = HCI_EV_CMD_COMPLETE;
1936 hdr->plen = sizeof(*evt) + 1;
1938 evt = skb_put(skb, sizeof(*evt));
1940 evt->opcode = cpu_to_le16(opcode);
1942 skb_put_u8(skb, 0x00);
1944 hci_skb_pkt_type(skb) = HCI_EVENT_PKT;
1946 return hci_recv_frame(hdev, skb);
1949 static int btusb_recv_bulk_intel(struct btusb_data *data, void *buffer,
1952 /* When the device is in bootloader mode, then it can send
1953 * events via the bulk endpoint. These events are treated the
1954 * same way as the ones received from the interrupt endpoint.
1956 if (test_bit(BTUSB_BOOTLOADER, &data->flags))
1957 return btusb_recv_intr(data, buffer, count);
1959 return btusb_recv_bulk(data, buffer, count);
1962 static void btusb_intel_bootup(struct btusb_data *data, const void *ptr,
1965 const struct intel_bootup *evt = ptr;
1967 if (len != sizeof(*evt))
1970 if (test_and_clear_bit(BTUSB_BOOTING, &data->flags))
1971 wake_up_bit(&data->flags, BTUSB_BOOTING);
1974 static void btusb_intel_secure_send_result(struct btusb_data *data,
1975 const void *ptr, unsigned int len)
1977 const struct intel_secure_send_result *evt = ptr;
1979 if (len != sizeof(*evt))
1983 set_bit(BTUSB_FIRMWARE_FAILED, &data->flags);
1985 if (test_and_clear_bit(BTUSB_DOWNLOADING, &data->flags) &&
1986 test_bit(BTUSB_FIRMWARE_LOADED, &data->flags))
1987 wake_up_bit(&data->flags, BTUSB_DOWNLOADING);
1990 static int btusb_recv_event_intel(struct hci_dev *hdev, struct sk_buff *skb)
1992 struct btusb_data *data = hci_get_drvdata(hdev);
1994 if (test_bit(BTUSB_BOOTLOADER, &data->flags)) {
1995 struct hci_event_hdr *hdr = (void *)skb->data;
1997 if (skb->len > HCI_EVENT_HDR_SIZE && hdr->evt == 0xff &&
1999 const void *ptr = skb->data + HCI_EVENT_HDR_SIZE + 1;
2000 unsigned int len = skb->len - HCI_EVENT_HDR_SIZE - 1;
2002 switch (skb->data[2]) {
2004 /* When switching to the operational firmware
2005 * the device sends a vendor specific event
2006 * indicating that the bootup completed.
2008 btusb_intel_bootup(data, ptr, len);
2011 /* When the firmware loading completes the
2012 * device sends out a vendor specific event
2013 * indicating the result of the firmware
2016 btusb_intel_secure_send_result(data, ptr, len);
2022 return hci_recv_frame(hdev, skb);
2025 static int btusb_send_frame_intel(struct hci_dev *hdev, struct sk_buff *skb)
2027 struct btusb_data *data = hci_get_drvdata(hdev);
2030 BT_DBG("%s", hdev->name);
2032 switch (hci_skb_pkt_type(skb)) {
2033 case HCI_COMMAND_PKT:
2034 if (test_bit(BTUSB_BOOTLOADER, &data->flags)) {
2035 struct hci_command_hdr *cmd = (void *)skb->data;
2036 __u16 opcode = le16_to_cpu(cmd->opcode);
2038 /* When in bootloader mode and the command 0xfc09
2039 * is received, it needs to be send down the
2040 * bulk endpoint. So allocate a bulk URB instead.
2042 if (opcode == 0xfc09)
2043 urb = alloc_bulk_urb(hdev, skb);
2045 urb = alloc_ctrl_urb(hdev, skb);
2047 /* When the 0xfc01 command is issued to boot into
2048 * the operational firmware, it will actually not
2049 * send a command complete event. To keep the flow
2050 * control working inject that event here.
2052 if (opcode == 0xfc01)
2053 inject_cmd_complete(hdev, opcode);
2055 urb = alloc_ctrl_urb(hdev, skb);
2058 return PTR_ERR(urb);
2060 hdev->stat.cmd_tx++;
2061 return submit_or_queue_tx_urb(hdev, urb);
2063 case HCI_ACLDATA_PKT:
2064 urb = alloc_bulk_urb(hdev, skb);
2066 return PTR_ERR(urb);
2068 hdev->stat.acl_tx++;
2069 return submit_or_queue_tx_urb(hdev, urb);
2071 case HCI_SCODATA_PKT:
2072 if (hci_conn_num(hdev, SCO_LINK) < 1)
2075 urb = alloc_isoc_urb(hdev, skb);
2077 return PTR_ERR(urb);
2079 hdev->stat.sco_tx++;
2080 return submit_tx_urb(hdev, urb);
2086 static bool btusb_setup_intel_new_get_fw_name(struct intel_version *ver,
2087 struct intel_boot_params *params,
2088 char *fw_name, size_t len,
2091 switch (ver->hw_variant) {
2092 case 0x0b: /* SfP */
2093 case 0x0c: /* WsP */
2094 snprintf(fw_name, len, "intel/ibt-%u-%u.%s",
2095 le16_to_cpu(ver->hw_variant),
2096 le16_to_cpu(params->dev_revid),
2099 case 0x11: /* JfP */
2100 case 0x12: /* ThP */
2101 case 0x13: /* HrP */
2102 case 0x14: /* CcP */
2103 snprintf(fw_name, len, "intel/ibt-%u-%u-%u.%s",
2104 le16_to_cpu(ver->hw_variant),
2105 le16_to_cpu(ver->hw_revision),
2106 le16_to_cpu(ver->fw_revision),
2115 static int btusb_setup_intel_new(struct hci_dev *hdev)
2117 struct btusb_data *data = hci_get_drvdata(hdev);
2118 struct intel_version ver;
2119 struct intel_boot_params params;
2120 const struct firmware *fw;
2123 ktime_t calltime, delta, rettime;
2124 unsigned long long duration;
2127 BT_DBG("%s", hdev->name);
2129 /* Set the default boot parameter to 0x0 and it is updated to
2130 * SKU specific boot parameter after reading Intel_Write_Boot_Params
2131 * command while downloading the firmware.
2133 boot_param = 0x00000000;
2135 calltime = ktime_get();
2137 /* Read the Intel version information to determine if the device
2138 * is in bootloader mode or if it already has operational firmware
2141 err = btintel_read_version(hdev, &ver);
2145 /* The hardware platform number has a fixed value of 0x37 and
2146 * for now only accept this single value.
2148 if (ver.hw_platform != 0x37) {
2149 bt_dev_err(hdev, "Unsupported Intel hardware platform (%u)",
2154 /* Check for supported iBT hardware variants of this firmware
2157 * This check has been put in place to ensure correct forward
2158 * compatibility options when newer hardware variants come along.
2160 switch (ver.hw_variant) {
2161 case 0x0b: /* SfP */
2162 case 0x0c: /* WsP */
2163 case 0x11: /* JfP */
2164 case 0x12: /* ThP */
2165 case 0x13: /* HrP */
2166 case 0x14: /* CcP */
2169 bt_dev_err(hdev, "Unsupported Intel hardware variant (%u)",
2174 btintel_version_info(hdev, &ver);
2176 /* The firmware variant determines if the device is in bootloader
2177 * mode or is running operational firmware. The value 0x06 identifies
2178 * the bootloader and the value 0x23 identifies the operational
2181 * When the operational firmware is already present, then only
2182 * the check for valid Bluetooth device address is needed. This
2183 * determines if the device will be added as configured or
2184 * unconfigured controller.
2186 * It is not possible to use the Secure Boot Parameters in this
2187 * case since that command is only available in bootloader mode.
2189 if (ver.fw_variant == 0x23) {
2190 clear_bit(BTUSB_BOOTLOADER, &data->flags);
2191 btintel_check_bdaddr(hdev);
2195 /* If the device is not in bootloader mode, then the only possible
2196 * choice is to return an error and abort the device initialization.
2198 if (ver.fw_variant != 0x06) {
2199 bt_dev_err(hdev, "Unsupported Intel firmware variant (%u)",
2204 /* Read the secure boot parameters to identify the operating
2205 * details of the bootloader.
2207 err = btintel_read_boot_params(hdev, ¶ms);
2211 /* It is required that every single firmware fragment is acknowledged
2212 * with a command complete event. If the boot parameters indicate
2213 * that this bootloader does not send them, then abort the setup.
2215 if (params.limited_cce != 0x00) {
2216 bt_dev_err(hdev, "Unsupported Intel firmware loading method (%u)",
2217 params.limited_cce);
2221 /* If the OTP has no valid Bluetooth device address, then there will
2222 * also be no valid address for the operational firmware.
2224 if (!bacmp(¶ms.otp_bdaddr, BDADDR_ANY)) {
2225 bt_dev_info(hdev, "No device address configured");
2226 set_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks);
2229 /* With this Intel bootloader only the hardware variant and device
2230 * revision information are used to select the right firmware for SfP
2233 * The firmware filename is ibt-<hw_variant>-<dev_revid>.sfi.
2235 * Currently the supported hardware variants are:
2236 * 11 (0x0b) for iBT3.0 (LnP/SfP)
2237 * 12 (0x0c) for iBT3.5 (WsP)
2239 * For ThP/JfP and for future SKU's, the FW name varies based on HW
2240 * variant, HW revision and FW revision, as these are dependent on CNVi
2241 * and RF Combination.
2243 * 17 (0x11) for iBT3.5 (JfP)
2244 * 18 (0x12) for iBT3.5 (ThP)
2246 * The firmware file name for these will be
2247 * ibt-<hw_variant>-<hw_revision>-<fw_revision>.sfi.
2250 err = btusb_setup_intel_new_get_fw_name(&ver, ¶ms, fwname,
2251 sizeof(fwname), "sfi");
2253 bt_dev_err(hdev, "Unsupported Intel firmware naming");
2257 err = request_firmware(&fw, fwname, &hdev->dev);
2259 bt_dev_err(hdev, "Failed to load Intel firmware file (%d)", err);
2263 bt_dev_info(hdev, "Found device firmware: %s", fwname);
2265 /* Save the DDC file name for later use to apply once the firmware
2266 * downloading is done.
2268 err = btusb_setup_intel_new_get_fw_name(&ver, ¶ms, fwname,
2269 sizeof(fwname), "ddc");
2271 bt_dev_err(hdev, "Unsupported Intel firmware naming");
2275 if (fw->size < 644) {
2276 bt_dev_err(hdev, "Invalid size of firmware file (%zu)",
2282 set_bit(BTUSB_DOWNLOADING, &data->flags);
2284 /* Start firmware downloading and get boot parameter */
2285 err = btintel_download_firmware(hdev, fw, &boot_param);
2289 set_bit(BTUSB_FIRMWARE_LOADED, &data->flags);
2291 bt_dev_info(hdev, "Waiting for firmware download to complete");
2293 /* Before switching the device into operational mode and with that
2294 * booting the loaded firmware, wait for the bootloader notification
2295 * that all fragments have been successfully received.
2297 * When the event processing receives the notification, then the
2298 * BTUSB_DOWNLOADING flag will be cleared.
2300 * The firmware loading should not take longer than 5 seconds
2301 * and thus just timeout if that happens and fail the setup
2304 err = wait_on_bit_timeout(&data->flags, BTUSB_DOWNLOADING,
2306 msecs_to_jiffies(5000));
2307 if (err == -EINTR) {
2308 bt_dev_err(hdev, "Firmware loading interrupted");
2313 bt_dev_err(hdev, "Firmware loading timeout");
2318 if (test_bit(BTUSB_FIRMWARE_FAILED, &data->flags)) {
2319 bt_dev_err(hdev, "Firmware loading failed");
2324 rettime = ktime_get();
2325 delta = ktime_sub(rettime, calltime);
2326 duration = (unsigned long long) ktime_to_ns(delta) >> 10;
2328 bt_dev_info(hdev, "Firmware loaded in %llu usecs", duration);
2331 release_firmware(fw);
2336 calltime = ktime_get();
2338 set_bit(BTUSB_BOOTING, &data->flags);
2340 err = btintel_send_intel_reset(hdev, boot_param);
2344 /* The bootloader will not indicate when the device is ready. This
2345 * is done by the operational firmware sending bootup notification.
2347 * Booting into operational firmware should not take longer than
2348 * 1 second. However if that happens, then just fail the setup
2349 * since something went wrong.
2351 bt_dev_info(hdev, "Waiting for device to boot");
2353 err = wait_on_bit_timeout(&data->flags, BTUSB_BOOTING,
2355 msecs_to_jiffies(1000));
2357 if (err == -EINTR) {
2358 bt_dev_err(hdev, "Device boot interrupted");
2363 bt_dev_err(hdev, "Device boot timeout");
2367 rettime = ktime_get();
2368 delta = ktime_sub(rettime, calltime);
2369 duration = (unsigned long long) ktime_to_ns(delta) >> 10;
2371 bt_dev_info(hdev, "Device booted in %llu usecs", duration);
2373 clear_bit(BTUSB_BOOTLOADER, &data->flags);
2375 /* Once the device is running in operational mode, it needs to apply
2376 * the device configuration (DDC) parameters.
2378 * The device can work without DDC parameters, so even if it fails
2379 * to load the file, no need to fail the setup.
2381 btintel_load_ddc_config(hdev, fwname);
2383 /* Set the event mask for Intel specific vendor events. This enables
2384 * a few extra events that are useful during general operation. It
2385 * does not enable any debugging related events.
2387 * The device will function correctly without these events enabled
2388 * and thus no need to fail the setup.
2390 btintel_set_event_mask(hdev, false);
2395 static int btusb_shutdown_intel(struct hci_dev *hdev)
2397 struct sk_buff *skb;
2400 /* In the shutdown sequence where Bluetooth is turned off followed
2401 * by WiFi being turned off, turning WiFi back on causes issue with
2402 * the RF calibration.
2404 * To ensure that any RF activity has been stopped, issue HCI Reset
2405 * command to clear all ongoing activity including advertising,
2408 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
2411 bt_dev_err(hdev, "HCI reset during shutdown failed");
2416 /* Some platforms have an issue with BT LED when the interface is
2417 * down or BT radio is turned off, which takes 5 seconds to BT LED
2418 * goes off. This command turns off the BT LED immediately.
2420 skb = __hci_cmd_sync(hdev, 0xfc3f, 0, NULL, HCI_INIT_TIMEOUT);
2423 bt_dev_err(hdev, "turning off Intel device LED failed");
2431 static int btusb_shutdown_intel_new(struct hci_dev *hdev)
2433 struct sk_buff *skb;
2435 /* Send HCI Reset to the controller to stop any BT activity which
2436 * were triggered. This will help to save power and maintain the
2437 * sync b/w Host and controller
2439 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
2441 bt_dev_err(hdev, "HCI reset during shutdown failed");
2442 return PTR_ERR(skb);
2449 #ifdef CONFIG_BT_HCIBTUSB_MTK
2451 #define FIRMWARE_MT7663 "mediatek/mt7663pr2h.bin"
2452 #define FIRMWARE_MT7668 "mediatek/mt7668pr2h.bin"
2454 #define HCI_WMT_MAX_EVENT_SIZE 64
2457 BTMTK_WMT_PATCH_DWNLD = 0x1,
2458 BTMTK_WMT_FUNC_CTRL = 0x6,
2459 BTMTK_WMT_RST = 0x7,
2460 BTMTK_WMT_SEMAPHORE = 0x17,
2465 BTMTK_WMT_PATCH_UNDONE,
2466 BTMTK_WMT_PATCH_DONE,
2467 BTMTK_WMT_ON_UNDONE,
2469 BTMTK_WMT_ON_PROGRESS,
2472 struct btmtk_wmt_hdr {
2479 struct btmtk_hci_wmt_cmd {
2480 struct btmtk_wmt_hdr hdr;
2484 struct btmtk_hci_wmt_evt {
2485 struct hci_event_hdr hhdr;
2486 struct btmtk_wmt_hdr whdr;
2489 struct btmtk_hci_wmt_evt_funcc {
2490 struct btmtk_hci_wmt_evt hwhdr;
2494 struct btmtk_tci_sleep {
2497 __le16 host_duration;
2499 u8 time_compensation;
2502 struct btmtk_hci_wmt_params {
2510 static void btusb_mtk_wmt_recv(struct urb *urb)
2512 struct hci_dev *hdev = urb->context;
2513 struct btusb_data *data = hci_get_drvdata(hdev);
2514 struct hci_event_hdr *hdr;
2515 struct sk_buff *skb;
2518 if (urb->status == 0 && urb->actual_length > 0) {
2519 hdev->stat.byte_rx += urb->actual_length;
2521 /* WMT event shouldn't be fragmented and the size should be
2522 * less than HCI_WMT_MAX_EVENT_SIZE.
2524 skb = bt_skb_alloc(HCI_WMT_MAX_EVENT_SIZE, GFP_ATOMIC);
2526 hdev->stat.err_rx++;
2530 hci_skb_pkt_type(skb) = HCI_EVENT_PKT;
2531 skb_put_data(skb, urb->transfer_buffer, urb->actual_length);
2533 hdr = (void *)skb->data;
2534 /* Fix up the vendor event id with 0xff for vendor specific
2535 * instead of 0xe4 so that event send via monitoring socket can
2536 * be parsed properly.
2540 /* When someone waits for the WMT event, the skb is being cloned
2541 * and being processed the events from there then.
2543 if (test_bit(BTUSB_TX_WAIT_VND_EVT, &data->flags)) {
2544 data->evt_skb = skb_clone(skb, GFP_KERNEL);
2549 err = hci_recv_frame(hdev, skb);
2553 if (test_and_clear_bit(BTUSB_TX_WAIT_VND_EVT,
2555 /* Barrier to sync with other CPUs */
2556 smp_mb__after_atomic();
2557 wake_up_bit(&data->flags,
2558 BTUSB_TX_WAIT_VND_EVT);
2563 kfree_skb(data->evt_skb);
2564 data->evt_skb = NULL;
2566 } else if (urb->status == -ENOENT) {
2567 /* Avoid suspend failed when usb_kill_urb */
2571 usb_mark_last_busy(data->udev);
2573 /* The URB complete handler is still called with urb->actual_length = 0
2574 * when the event is not available, so we should keep re-submitting
2575 * URB until WMT event returns, Also, It's necessary to wait some time
2576 * between the two consecutive control URBs to relax the target device
2577 * to generate the event. Otherwise, the WMT event cannot return from
2578 * the device successfully.
2582 usb_anchor_urb(urb, &data->ctrl_anchor);
2583 err = usb_submit_urb(urb, GFP_ATOMIC);
2585 /* -EPERM: urb is being killed;
2586 * -ENODEV: device got disconnected
2588 if (err != -EPERM && err != -ENODEV)
2589 bt_dev_err(hdev, "urb %p failed to resubmit (%d)",
2591 usb_unanchor_urb(urb);
2595 static int btusb_mtk_submit_wmt_recv_urb(struct hci_dev *hdev)
2597 struct btusb_data *data = hci_get_drvdata(hdev);
2598 struct usb_ctrlrequest *dr;
2604 urb = usb_alloc_urb(0, GFP_KERNEL);
2608 dr = kmalloc(sizeof(*dr), GFP_KERNEL);
2614 dr->bRequestType = USB_TYPE_VENDOR | USB_DIR_IN;
2616 dr->wIndex = cpu_to_le16(0);
2617 dr->wValue = cpu_to_le16(48);
2618 dr->wLength = cpu_to_le16(size);
2620 buf = kmalloc(size, GFP_KERNEL);
2626 pipe = usb_rcvctrlpipe(data->udev, 0);
2628 usb_fill_control_urb(urb, data->udev, pipe, (void *)dr,
2629 buf, size, btusb_mtk_wmt_recv, hdev);
2631 urb->transfer_flags |= URB_FREE_BUFFER;
2633 usb_anchor_urb(urb, &data->ctrl_anchor);
2634 err = usb_submit_urb(urb, GFP_KERNEL);
2636 if (err != -EPERM && err != -ENODEV)
2637 bt_dev_err(hdev, "urb %p submission failed (%d)",
2639 usb_unanchor_urb(urb);
2647 static int btusb_mtk_hci_wmt_sync(struct hci_dev *hdev,
2648 struct btmtk_hci_wmt_params *wmt_params)
2650 struct btusb_data *data = hci_get_drvdata(hdev);
2651 struct btmtk_hci_wmt_evt_funcc *wmt_evt_funcc;
2652 u32 hlen, status = BTMTK_WMT_INVALID;
2653 struct btmtk_hci_wmt_evt *wmt_evt;
2654 struct btmtk_hci_wmt_cmd wc;
2655 struct btmtk_wmt_hdr *hdr;
2658 /* Submit control IN URB on demand to process the WMT event */
2659 err = btusb_mtk_submit_wmt_recv_urb(hdev);
2663 /* Send the WMT command and wait until the WMT event returns */
2664 hlen = sizeof(*hdr) + wmt_params->dlen;
2668 hdr = (struct btmtk_wmt_hdr *)&wc;
2670 hdr->op = wmt_params->op;
2671 hdr->dlen = cpu_to_le16(wmt_params->dlen + 1);
2672 hdr->flag = wmt_params->flag;
2673 memcpy(wc.data, wmt_params->data, wmt_params->dlen);
2675 set_bit(BTUSB_TX_WAIT_VND_EVT, &data->flags);
2677 err = __hci_cmd_send(hdev, 0xfc6f, hlen, &wc);
2680 clear_bit(BTUSB_TX_WAIT_VND_EVT, &data->flags);
2684 /* The vendor specific WMT commands are all answered by a vendor
2685 * specific event and will have the Command Status or Command
2686 * Complete as with usual HCI command flow control.
2688 * After sending the command, wait for BTUSB_TX_WAIT_VND_EVT
2689 * state to be cleared. The driver specific event receive routine
2690 * will clear that state and with that indicate completion of the
2693 err = wait_on_bit_timeout(&data->flags, BTUSB_TX_WAIT_VND_EVT,
2694 TASK_INTERRUPTIBLE, HCI_INIT_TIMEOUT);
2695 if (err == -EINTR) {
2696 bt_dev_err(hdev, "Execution of wmt command interrupted");
2697 clear_bit(BTUSB_TX_WAIT_VND_EVT, &data->flags);
2702 bt_dev_err(hdev, "Execution of wmt command timed out");
2703 clear_bit(BTUSB_TX_WAIT_VND_EVT, &data->flags);
2707 /* Parse and handle the return WMT event */
2708 wmt_evt = (struct btmtk_hci_wmt_evt *)data->evt_skb->data;
2709 if (wmt_evt->whdr.op != hdr->op) {
2710 bt_dev_err(hdev, "Wrong op received %d expected %d",
2711 wmt_evt->whdr.op, hdr->op);
2716 switch (wmt_evt->whdr.op) {
2717 case BTMTK_WMT_SEMAPHORE:
2718 if (wmt_evt->whdr.flag == 2)
2719 status = BTMTK_WMT_PATCH_UNDONE;
2721 status = BTMTK_WMT_PATCH_DONE;
2723 case BTMTK_WMT_FUNC_CTRL:
2724 wmt_evt_funcc = (struct btmtk_hci_wmt_evt_funcc *)wmt_evt;
2725 if (be16_to_cpu(wmt_evt_funcc->status) == 0x404)
2726 status = BTMTK_WMT_ON_DONE;
2727 else if (be16_to_cpu(wmt_evt_funcc->status) == 0x420)
2728 status = BTMTK_WMT_ON_PROGRESS;
2730 status = BTMTK_WMT_ON_UNDONE;
2734 if (wmt_params->status)
2735 *wmt_params->status = status;
2738 kfree_skb(data->evt_skb);
2739 data->evt_skb = NULL;
2744 static int btusb_mtk_setup_firmware(struct hci_dev *hdev, const char *fwname)
2746 struct btmtk_hci_wmt_params wmt_params;
2747 const struct firmware *fw;
2753 err = request_firmware(&fw, fwname, &hdev->dev);
2755 bt_dev_err(hdev, "Failed to load firmware file (%d)", err);
2762 /* The size of patch header is 30 bytes, should be skip */
2765 goto err_release_fw;
2772 wmt_params.op = BTMTK_WMT_PATCH_DWNLD;
2773 wmt_params.status = NULL;
2775 while (fw_size > 0) {
2776 dlen = min_t(int, 250, fw_size);
2778 /* Tell deivice the position in sequence */
2779 if (fw_size - dlen <= 0)
2781 else if (fw_size < fw->size - 30)
2784 wmt_params.flag = flag;
2785 wmt_params.dlen = dlen;
2786 wmt_params.data = fw_ptr;
2788 err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
2790 bt_dev_err(hdev, "Failed to send wmt patch dwnld (%d)",
2792 goto err_release_fw;
2799 wmt_params.op = BTMTK_WMT_RST;
2800 wmt_params.flag = 4;
2801 wmt_params.dlen = 0;
2802 wmt_params.data = NULL;
2803 wmt_params.status = NULL;
2805 /* Activate funciton the firmware providing to */
2806 err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
2808 bt_dev_err(hdev, "Failed to send wmt rst (%d)", err);
2812 /* Wait a few moments for firmware activation done */
2813 usleep_range(10000, 12000);
2816 release_firmware(fw);
2821 static int btusb_mtk_func_query(struct hci_dev *hdev)
2823 struct btmtk_hci_wmt_params wmt_params;
2827 /* Query whether the function is enabled */
2828 wmt_params.op = BTMTK_WMT_FUNC_CTRL;
2829 wmt_params.flag = 4;
2830 wmt_params.dlen = sizeof(param);
2831 wmt_params.data = ¶m;
2832 wmt_params.status = &status;
2834 err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
2836 bt_dev_err(hdev, "Failed to query function status (%d)", err);
2843 static int btusb_mtk_reg_read(struct btusb_data *data, u32 reg, u32 *val)
2845 int pipe, err, size = sizeof(u32);
2848 buf = kzalloc(size, GFP_KERNEL);
2852 pipe = usb_rcvctrlpipe(data->udev, 0);
2853 err = usb_control_msg(data->udev, pipe, 0x63,
2854 USB_TYPE_VENDOR | USB_DIR_IN,
2855 reg >> 16, reg & 0xffff,
2856 buf, size, USB_CTRL_SET_TIMEOUT);
2860 *val = get_unaligned_le32(buf);
2868 static int btusb_mtk_id_get(struct btusb_data *data, u32 *id)
2870 return btusb_mtk_reg_read(data, 0x80000008, id);
2873 static int btusb_mtk_setup(struct hci_dev *hdev)
2875 struct btusb_data *data = hci_get_drvdata(hdev);
2876 struct btmtk_hci_wmt_params wmt_params;
2877 ktime_t calltime, delta, rettime;
2878 struct btmtk_tci_sleep tci_sleep;
2879 unsigned long long duration;
2880 struct sk_buff *skb;
2886 calltime = ktime_get();
2888 err = btusb_mtk_id_get(data, &dev_id);
2890 bt_dev_err(hdev, "Failed to get device id (%d)", err);
2896 fwname = FIRMWARE_MT7663;
2899 fwname = FIRMWARE_MT7668;
2902 bt_dev_err(hdev, "Unsupported support hardware variant (%08x)",
2907 /* Query whether the firmware is already download */
2908 wmt_params.op = BTMTK_WMT_SEMAPHORE;
2909 wmt_params.flag = 1;
2910 wmt_params.dlen = 0;
2911 wmt_params.data = NULL;
2912 wmt_params.status = &status;
2914 err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
2916 bt_dev_err(hdev, "Failed to query firmware status (%d)", err);
2920 if (status == BTMTK_WMT_PATCH_DONE) {
2921 bt_dev_info(hdev, "firmware already downloaded");
2922 goto ignore_setup_fw;
2925 /* Setup a firmware which the device definitely requires */
2926 err = btusb_mtk_setup_firmware(hdev, fwname);
2931 err = readx_poll_timeout(btusb_mtk_func_query, hdev, status,
2932 status < 0 || status != BTMTK_WMT_ON_PROGRESS,
2934 /* -ETIMEDOUT happens */
2938 /* The other errors happen in btusb_mtk_func_query */
2942 if (status == BTMTK_WMT_ON_DONE) {
2943 bt_dev_info(hdev, "function already on");
2944 goto ignore_func_on;
2947 /* Enable Bluetooth protocol */
2949 wmt_params.op = BTMTK_WMT_FUNC_CTRL;
2950 wmt_params.flag = 0;
2951 wmt_params.dlen = sizeof(param);
2952 wmt_params.data = ¶m;
2953 wmt_params.status = NULL;
2955 err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
2957 bt_dev_err(hdev, "Failed to send wmt func ctrl (%d)", err);
2962 /* Apply the low power environment setup */
2963 tci_sleep.mode = 0x5;
2964 tci_sleep.duration = cpu_to_le16(0x640);
2965 tci_sleep.host_duration = cpu_to_le16(0x640);
2966 tci_sleep.host_wakeup_pin = 0;
2967 tci_sleep.time_compensation = 0;
2969 skb = __hci_cmd_sync(hdev, 0xfc7a, sizeof(tci_sleep), &tci_sleep,
2973 bt_dev_err(hdev, "Failed to apply low power setting (%d)", err);
2978 rettime = ktime_get();
2979 delta = ktime_sub(rettime, calltime);
2980 duration = (unsigned long long)ktime_to_ns(delta) >> 10;
2982 bt_dev_info(hdev, "Device setup in %llu usecs", duration);
2987 static int btusb_mtk_shutdown(struct hci_dev *hdev)
2989 struct btmtk_hci_wmt_params wmt_params;
2993 /* Disable the device */
2994 wmt_params.op = BTMTK_WMT_FUNC_CTRL;
2995 wmt_params.flag = 0;
2996 wmt_params.dlen = sizeof(param);
2997 wmt_params.data = ¶m;
2998 wmt_params.status = NULL;
3000 err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
3002 bt_dev_err(hdev, "Failed to send wmt func ctrl (%d)", err);
3009 MODULE_FIRMWARE(FIRMWARE_MT7663);
3010 MODULE_FIRMWARE(FIRMWARE_MT7668);
3014 /* Configure an out-of-band gpio as wake-up pin, if specified in device tree */
3015 static int marvell_config_oob_wake(struct hci_dev *hdev)
3017 struct sk_buff *skb;
3018 struct btusb_data *data = hci_get_drvdata(hdev);
3019 struct device *dev = &data->udev->dev;
3020 u16 pin, gap, opcode;
3024 /* Move on if no wakeup pin specified */
3025 if (of_property_read_u16(dev->of_node, "marvell,wakeup-pin", &pin) ||
3026 of_property_read_u16(dev->of_node, "marvell,wakeup-gap-ms", &gap))
3029 /* Vendor specific command to configure a GPIO as wake-up pin */
3030 opcode = hci_opcode_pack(0x3F, 0x59);
3031 cmd[0] = opcode & 0xFF;
3032 cmd[1] = opcode >> 8;
3033 cmd[2] = 2; /* length of parameters that follow */
3035 cmd[4] = gap; /* time in ms, for which wakeup pin should be asserted */
3037 skb = bt_skb_alloc(sizeof(cmd), GFP_KERNEL);
3039 bt_dev_err(hdev, "%s: No memory\n", __func__);
3043 skb_put_data(skb, cmd, sizeof(cmd));
3044 hci_skb_pkt_type(skb) = HCI_COMMAND_PKT;
3046 ret = btusb_send_frame(hdev, skb);
3048 bt_dev_err(hdev, "%s: configuration failed\n", __func__);
3057 static int btusb_set_bdaddr_marvell(struct hci_dev *hdev,
3058 const bdaddr_t *bdaddr)
3060 struct sk_buff *skb;
3065 buf[1] = sizeof(bdaddr_t);
3066 memcpy(buf + 2, bdaddr, sizeof(bdaddr_t));
3068 skb = __hci_cmd_sync(hdev, 0xfc22, sizeof(buf), buf, HCI_INIT_TIMEOUT);
3071 bt_dev_err(hdev, "changing Marvell device address failed (%ld)",
3080 static int btusb_set_bdaddr_ath3012(struct hci_dev *hdev,
3081 const bdaddr_t *bdaddr)
3083 struct sk_buff *skb;
3090 buf[3] = sizeof(bdaddr_t);
3091 memcpy(buf + 4, bdaddr, sizeof(bdaddr_t));
3093 skb = __hci_cmd_sync(hdev, 0xfc0b, sizeof(buf), buf, HCI_INIT_TIMEOUT);
3096 bt_dev_err(hdev, "Change address command failed (%ld)", ret);
3104 #define QCA_DFU_PACKET_LEN 4096
3106 #define QCA_GET_TARGET_VERSION 0x09
3107 #define QCA_CHECK_STATUS 0x05
3108 #define QCA_DFU_DOWNLOAD 0x01
3110 #define QCA_SYSCFG_UPDATED 0x40
3111 #define QCA_PATCH_UPDATED 0x80
3112 #define QCA_DFU_TIMEOUT 3000
3114 struct qca_version {
3116 __le32 patch_version;
3122 struct qca_rampatch_version {
3124 __le16 patch_version;
3127 struct qca_device_info {
3129 u8 rampatch_hdr; /* length of header in rampatch */
3130 u8 nvm_hdr; /* length of header in NVM */
3131 u8 ver_offset; /* offset of version structure in rampatch */
3134 static const struct qca_device_info qca_devices_table[] = {
3135 { 0x00000100, 20, 4, 10 }, /* Rome 1.0 */
3136 { 0x00000101, 20, 4, 10 }, /* Rome 1.1 */
3137 { 0x00000200, 28, 4, 18 }, /* Rome 2.0 */
3138 { 0x00000201, 28, 4, 18 }, /* Rome 2.1 */
3139 { 0x00000300, 28, 4, 18 }, /* Rome 3.0 */
3140 { 0x00000302, 28, 4, 18 }, /* Rome 3.2 */
3143 static int btusb_qca_send_vendor_req(struct usb_device *udev, u8 request,
3144 void *data, u16 size)
3149 buf = kmalloc(size, GFP_KERNEL);
3153 /* Found some of USB hosts have IOT issues with ours so that we should
3154 * not wait until HCI layer is ready.
3156 pipe = usb_rcvctrlpipe(udev, 0);
3157 err = usb_control_msg(udev, pipe, request, USB_TYPE_VENDOR | USB_DIR_IN,
3158 0, 0, buf, size, USB_CTRL_SET_TIMEOUT);
3160 dev_err(&udev->dev, "Failed to access otp area (%d)", err);
3164 memcpy(data, buf, size);
3172 static int btusb_setup_qca_download_fw(struct hci_dev *hdev,
3173 const struct firmware *firmware,
3176 struct btusb_data *btdata = hci_get_drvdata(hdev);
3177 struct usb_device *udev = btdata->udev;
3178 size_t count, size, sent = 0;
3182 buf = kmalloc(QCA_DFU_PACKET_LEN, GFP_KERNEL);
3186 count = firmware->size;
3188 size = min_t(size_t, count, hdr_size);
3189 memcpy(buf, firmware->data, size);
3191 /* USB patches should go down to controller through USB path
3192 * because binary format fits to go down through USB channel.
3193 * USB control path is for patching headers and USB bulk is for
3196 pipe = usb_sndctrlpipe(udev, 0);
3197 err = usb_control_msg(udev, pipe, QCA_DFU_DOWNLOAD, USB_TYPE_VENDOR,
3198 0, 0, buf, size, USB_CTRL_SET_TIMEOUT);
3200 bt_dev_err(hdev, "Failed to send headers (%d)", err);
3208 size = min_t(size_t, count, QCA_DFU_PACKET_LEN);
3210 memcpy(buf, firmware->data + sent, size);
3212 pipe = usb_sndbulkpipe(udev, 0x02);
3213 err = usb_bulk_msg(udev, pipe, buf, size, &len,
3216 bt_dev_err(hdev, "Failed to send body at %zd of %zd (%d)",
3217 sent, firmware->size, err);
3222 bt_dev_err(hdev, "Failed to get bulk buffer");
3236 static int btusb_setup_qca_load_rampatch(struct hci_dev *hdev,
3237 struct qca_version *ver,
3238 const struct qca_device_info *info)
3240 struct qca_rampatch_version *rver;
3241 const struct firmware *fw;
3242 u32 ver_rom, ver_patch;
3243 u16 rver_rom, rver_patch;
3247 ver_rom = le32_to_cpu(ver->rom_version);
3248 ver_patch = le32_to_cpu(ver->patch_version);
3250 snprintf(fwname, sizeof(fwname), "qca/rampatch_usb_%08x.bin", ver_rom);
3252 err = request_firmware(&fw, fwname, &hdev->dev);
3254 bt_dev_err(hdev, "failed to request rampatch file: %s (%d)",
3259 bt_dev_info(hdev, "using rampatch file: %s", fwname);
3261 rver = (struct qca_rampatch_version *)(fw->data + info->ver_offset);
3262 rver_rom = le16_to_cpu(rver->rom_version);
3263 rver_patch = le16_to_cpu(rver->patch_version);
3265 bt_dev_info(hdev, "QCA: patch rome 0x%x build 0x%x, "
3266 "firmware rome 0x%x build 0x%x",
3267 rver_rom, rver_patch, ver_rom, ver_patch);
3269 if (rver_rom != ver_rom || rver_patch <= ver_patch) {
3270 bt_dev_err(hdev, "rampatch file version did not match with firmware");
3275 err = btusb_setup_qca_download_fw(hdev, fw, info->rampatch_hdr);
3278 release_firmware(fw);
3283 static int btusb_setup_qca_load_nvm(struct hci_dev *hdev,
3284 struct qca_version *ver,
3285 const struct qca_device_info *info)
3287 const struct firmware *fw;
3291 snprintf(fwname, sizeof(fwname), "qca/nvm_usb_%08x.bin",
3292 le32_to_cpu(ver->rom_version));
3294 err = request_firmware(&fw, fwname, &hdev->dev);
3296 bt_dev_err(hdev, "failed to request NVM file: %s (%d)",
3301 bt_dev_info(hdev, "using NVM file: %s", fwname);
3303 err = btusb_setup_qca_download_fw(hdev, fw, info->nvm_hdr);
3305 release_firmware(fw);
3310 /* identify the ROM version and check whether patches are needed */
3311 static bool btusb_qca_need_patch(struct usb_device *udev)
3313 struct qca_version ver;
3315 if (btusb_qca_send_vendor_req(udev, QCA_GET_TARGET_VERSION, &ver,
3318 /* only low ROM versions need patches */
3319 return !(le32_to_cpu(ver.rom_version) & ~0xffffU);
3322 static int btusb_setup_qca(struct hci_dev *hdev)
3324 struct btusb_data *btdata = hci_get_drvdata(hdev);
3325 struct usb_device *udev = btdata->udev;
3326 const struct qca_device_info *info = NULL;
3327 struct qca_version ver;
3332 err = btusb_qca_send_vendor_req(udev, QCA_GET_TARGET_VERSION, &ver,
3337 ver_rom = le32_to_cpu(ver.rom_version);
3338 /* Don't care about high ROM versions */
3339 if (ver_rom & ~0xffffU)
3342 for (i = 0; i < ARRAY_SIZE(qca_devices_table); i++) {
3343 if (ver_rom == qca_devices_table[i].rom_version)
3344 info = &qca_devices_table[i];
3347 bt_dev_err(hdev, "don't support firmware rome 0x%x", ver_rom);
3351 err = btusb_qca_send_vendor_req(udev, QCA_CHECK_STATUS, &status,
3356 if (!(status & QCA_PATCH_UPDATED)) {
3357 err = btusb_setup_qca_load_rampatch(hdev, &ver, info);
3362 if (!(status & QCA_SYSCFG_UPDATED)) {
3363 err = btusb_setup_qca_load_nvm(hdev, &ver, info);
3371 #ifdef CONFIG_BT_HCIBTUSB_BCM
3372 static inline int __set_diag_interface(struct hci_dev *hdev)
3374 struct btusb_data *data = hci_get_drvdata(hdev);
3375 struct usb_interface *intf = data->diag;
3381 data->diag_tx_ep = NULL;
3382 data->diag_rx_ep = NULL;
3384 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
3385 struct usb_endpoint_descriptor *ep_desc;
3387 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
3389 if (!data->diag_tx_ep && usb_endpoint_is_bulk_out(ep_desc)) {
3390 data->diag_tx_ep = ep_desc;
3394 if (!data->diag_rx_ep && usb_endpoint_is_bulk_in(ep_desc)) {
3395 data->diag_rx_ep = ep_desc;
3400 if (!data->diag_tx_ep || !data->diag_rx_ep) {
3401 bt_dev_err(hdev, "invalid diagnostic descriptors");
3408 static struct urb *alloc_diag_urb(struct hci_dev *hdev, bool enable)
3410 struct btusb_data *data = hci_get_drvdata(hdev);
3411 struct sk_buff *skb;
3415 if (!data->diag_tx_ep)
3416 return ERR_PTR(-ENODEV);
3418 urb = usb_alloc_urb(0, GFP_KERNEL);
3420 return ERR_PTR(-ENOMEM);
3422 skb = bt_skb_alloc(2, GFP_KERNEL);
3425 return ERR_PTR(-ENOMEM);
3428 skb_put_u8(skb, 0xf0);
3429 skb_put_u8(skb, enable);
3431 pipe = usb_sndbulkpipe(data->udev, data->diag_tx_ep->bEndpointAddress);
3433 usb_fill_bulk_urb(urb, data->udev, pipe,
3434 skb->data, skb->len, btusb_tx_complete, skb);
3436 skb->dev = (void *)hdev;
3441 static int btusb_bcm_set_diag(struct hci_dev *hdev, bool enable)
3443 struct btusb_data *data = hci_get_drvdata(hdev);
3449 if (!test_bit(HCI_RUNNING, &hdev->flags))
3452 urb = alloc_diag_urb(hdev, enable);
3454 return PTR_ERR(urb);
3456 return submit_or_queue_tx_urb(hdev, urb);
3461 static irqreturn_t btusb_oob_wake_handler(int irq, void *priv)
3463 struct btusb_data *data = priv;
3465 pm_wakeup_event(&data->udev->dev, 0);
3468 /* Disable only if not already disabled (keep it balanced) */
3469 if (test_and_clear_bit(BTUSB_OOB_WAKE_ENABLED, &data->flags)) {
3470 disable_irq_nosync(irq);
3471 disable_irq_wake(irq);
3476 static const struct of_device_id btusb_match_table[] = {
3477 { .compatible = "usb1286,204e" },
3478 { .compatible = "usbcf3,e300" }, /* QCA6174A */
3479 { .compatible = "usb4ca,301a" }, /* QCA6174A (Lite-On) */
3482 MODULE_DEVICE_TABLE(of, btusb_match_table);
3484 /* Use an oob wakeup pin? */
3485 static int btusb_config_oob_wake(struct hci_dev *hdev)
3487 struct btusb_data *data = hci_get_drvdata(hdev);
3488 struct device *dev = &data->udev->dev;
3491 clear_bit(BTUSB_OOB_WAKE_ENABLED, &data->flags);
3493 if (!of_match_device(btusb_match_table, dev))
3496 /* Move on if no IRQ specified */
3497 irq = of_irq_get_byname(dev->of_node, "wakeup");
3499 bt_dev_dbg(hdev, "%s: no OOB Wakeup IRQ in DT", __func__);
3503 irq_set_status_flags(irq, IRQ_NOAUTOEN);
3504 ret = devm_request_irq(&hdev->dev, irq, btusb_oob_wake_handler,
3505 0, "OOB Wake-on-BT", data);
3507 bt_dev_err(hdev, "%s: IRQ request failed", __func__);
3511 ret = device_init_wakeup(dev, true);
3513 bt_dev_err(hdev, "%s: failed to init_wakeup", __func__);
3517 data->oob_wake_irq = irq;
3518 bt_dev_info(hdev, "OOB Wake-on-BT configured at IRQ %u", irq);
3523 static void btusb_check_needs_reset_resume(struct usb_interface *intf)
3525 if (dmi_check_system(btusb_needs_reset_resume_table))
3526 interface_to_usbdev(intf)->quirks |= USB_QUIRK_RESET_RESUME;
3529 static int btusb_probe(struct usb_interface *intf,
3530 const struct usb_device_id *id)
3532 struct usb_endpoint_descriptor *ep_desc;
3533 struct gpio_desc *reset_gpio;
3534 struct btusb_data *data;
3535 struct hci_dev *hdev;
3536 unsigned ifnum_base;
3539 BT_DBG("intf %p id %p", intf, id);
3541 /* interface numbers are hardcoded in the spec */
3542 if (intf->cur_altsetting->desc.bInterfaceNumber != 0) {
3543 if (!(id->driver_info & BTUSB_IFNUM_2))
3545 if (intf->cur_altsetting->desc.bInterfaceNumber != 2)
3549 ifnum_base = intf->cur_altsetting->desc.bInterfaceNumber;
3551 if (!id->driver_info) {
3552 const struct usb_device_id *match;
3554 match = usb_match_id(intf, blacklist_table);
3559 if (id->driver_info == BTUSB_IGNORE)
3562 if (id->driver_info & BTUSB_ATH3012) {
3563 struct usb_device *udev = interface_to_usbdev(intf);
3565 /* Old firmware would otherwise let ath3k driver load
3566 * patch and sysconfig files
3568 if (le16_to_cpu(udev->descriptor.bcdDevice) <= 0x0001 &&
3569 !btusb_qca_need_patch(udev))
3573 data = devm_kzalloc(&intf->dev, sizeof(*data), GFP_KERNEL);
3577 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
3578 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
3580 if (!data->intr_ep && usb_endpoint_is_int_in(ep_desc)) {
3581 data->intr_ep = ep_desc;
3585 if (!data->bulk_tx_ep && usb_endpoint_is_bulk_out(ep_desc)) {
3586 data->bulk_tx_ep = ep_desc;
3590 if (!data->bulk_rx_ep && usb_endpoint_is_bulk_in(ep_desc)) {
3591 data->bulk_rx_ep = ep_desc;
3596 if (!data->intr_ep || !data->bulk_tx_ep || !data->bulk_rx_ep)
3599 if (id->driver_info & BTUSB_AMP) {
3600 data->cmdreq_type = USB_TYPE_CLASS | 0x01;
3601 data->cmdreq = 0x2b;
3603 data->cmdreq_type = USB_TYPE_CLASS;
3604 data->cmdreq = 0x00;
3607 data->udev = interface_to_usbdev(intf);
3610 INIT_WORK(&data->work, btusb_work);
3611 INIT_WORK(&data->waker, btusb_waker);
3612 init_usb_anchor(&data->deferred);
3613 init_usb_anchor(&data->tx_anchor);
3614 spin_lock_init(&data->txlock);
3616 init_usb_anchor(&data->intr_anchor);
3617 init_usb_anchor(&data->bulk_anchor);
3618 init_usb_anchor(&data->isoc_anchor);
3619 init_usb_anchor(&data->diag_anchor);
3620 init_usb_anchor(&data->ctrl_anchor);
3621 spin_lock_init(&data->rxlock);
3623 if (id->driver_info & BTUSB_INTEL_NEW) {
3624 data->recv_event = btusb_recv_event_intel;
3625 data->recv_bulk = btusb_recv_bulk_intel;
3626 set_bit(BTUSB_BOOTLOADER, &data->flags);
3628 data->recv_event = hci_recv_frame;
3629 data->recv_bulk = btusb_recv_bulk;
3632 hdev = hci_alloc_dev();
3636 hdev->bus = HCI_USB;
3637 hci_set_drvdata(hdev, data);
3639 if (id->driver_info & BTUSB_AMP)
3640 hdev->dev_type = HCI_AMP;
3642 hdev->dev_type = HCI_PRIMARY;
3646 SET_HCIDEV_DEV(hdev, &intf->dev);
3648 reset_gpio = gpiod_get_optional(&data->udev->dev, "reset",
3650 if (IS_ERR(reset_gpio)) {
3651 err = PTR_ERR(reset_gpio);
3653 } else if (reset_gpio) {
3654 data->reset_gpio = reset_gpio;
3657 hdev->open = btusb_open;
3658 hdev->close = btusb_close;
3659 hdev->flush = btusb_flush;
3660 hdev->send = btusb_send_frame;
3661 hdev->notify = btusb_notify;
3664 err = btusb_config_oob_wake(hdev);
3668 /* Marvell devices may need a specific chip configuration */
3669 if (id->driver_info & BTUSB_MARVELL && data->oob_wake_irq) {
3670 err = marvell_config_oob_wake(hdev);
3675 if (id->driver_info & BTUSB_CW6622)
3676 set_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks);
3678 if (id->driver_info & BTUSB_BCM2045)
3679 set_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks);
3681 if (id->driver_info & BTUSB_BCM92035)
3682 hdev->setup = btusb_setup_bcm92035;
3684 #ifdef CONFIG_BT_HCIBTUSB_BCM
3685 if (id->driver_info & BTUSB_BCM_PATCHRAM) {
3686 hdev->manufacturer = 15;
3687 hdev->setup = btbcm_setup_patchram;
3688 hdev->set_diag = btusb_bcm_set_diag;
3689 hdev->set_bdaddr = btbcm_set_bdaddr;
3691 /* Broadcom LM_DIAG Interface numbers are hardcoded */
3692 data->diag = usb_ifnum_to_if(data->udev, ifnum_base + 2);
3695 if (id->driver_info & BTUSB_BCM_APPLE) {
3696 hdev->manufacturer = 15;
3697 hdev->setup = btbcm_setup_apple;
3698 hdev->set_diag = btusb_bcm_set_diag;
3700 /* Broadcom LM_DIAG Interface numbers are hardcoded */
3701 data->diag = usb_ifnum_to_if(data->udev, ifnum_base + 2);
3705 if (id->driver_info & BTUSB_INTEL) {
3706 hdev->manufacturer = 2;
3707 hdev->setup = btusb_setup_intel;
3708 hdev->shutdown = btusb_shutdown_intel;
3709 hdev->set_diag = btintel_set_diag_mfg;
3710 hdev->set_bdaddr = btintel_set_bdaddr;
3711 hdev->cmd_timeout = btusb_intel_cmd_timeout;
3712 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
3713 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
3714 set_bit(HCI_QUIRK_NON_PERSISTENT_DIAG, &hdev->quirks);
3717 if (id->driver_info & BTUSB_INTEL_NEW) {
3718 hdev->manufacturer = 2;
3719 hdev->send = btusb_send_frame_intel;
3720 hdev->setup = btusb_setup_intel_new;
3721 hdev->shutdown = btusb_shutdown_intel_new;
3722 hdev->hw_error = btintel_hw_error;
3723 hdev->set_diag = btintel_set_diag;
3724 hdev->set_bdaddr = btintel_set_bdaddr;
3725 hdev->cmd_timeout = btusb_intel_cmd_timeout;
3726 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
3727 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
3728 set_bit(HCI_QUIRK_NON_PERSISTENT_DIAG, &hdev->quirks);
3731 if (id->driver_info & BTUSB_MARVELL)
3732 hdev->set_bdaddr = btusb_set_bdaddr_marvell;
3734 #ifdef CONFIG_BT_HCIBTUSB_MTK
3735 if (id->driver_info & BTUSB_MEDIATEK) {
3736 hdev->setup = btusb_mtk_setup;
3737 hdev->shutdown = btusb_mtk_shutdown;
3738 hdev->manufacturer = 70;
3739 set_bit(HCI_QUIRK_NON_PERSISTENT_SETUP, &hdev->quirks);
3743 if (id->driver_info & BTUSB_SWAVE) {
3744 set_bit(HCI_QUIRK_FIXUP_INQUIRY_MODE, &hdev->quirks);
3745 set_bit(HCI_QUIRK_BROKEN_LOCAL_COMMANDS, &hdev->quirks);
3748 if (id->driver_info & BTUSB_INTEL_BOOT) {
3749 hdev->manufacturer = 2;
3750 set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
3753 if (id->driver_info & BTUSB_ATH3012) {
3754 data->setup_on_usb = btusb_setup_qca;
3755 hdev->set_bdaddr = btusb_set_bdaddr_ath3012;
3756 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
3757 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
3760 if (id->driver_info & BTUSB_QCA_ROME) {
3761 data->setup_on_usb = btusb_setup_qca;
3762 hdev->set_bdaddr = btusb_set_bdaddr_ath3012;
3763 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
3764 btusb_check_needs_reset_resume(intf);
3767 #ifdef CONFIG_BT_HCIBTUSB_RTL
3768 if (id->driver_info & BTUSB_REALTEK) {
3769 hdev->setup = btrtl_setup_realtek;
3770 hdev->shutdown = btrtl_shutdown_realtek;
3772 /* Realtek devices lose their updated firmware over suspend,
3773 * but the USB hub doesn't notice any status change.
3774 * Explicitly request a device reset on resume.
3776 interface_to_usbdev(intf)->quirks |= USB_QUIRK_RESET_RESUME;
3780 if (id->driver_info & BTUSB_AMP) {
3781 /* AMP controllers do not support SCO packets */
3784 /* Interface orders are hardcoded in the specification */
3785 data->isoc = usb_ifnum_to_if(data->udev, ifnum_base + 1);
3786 data->isoc_ifnum = ifnum_base + 1;
3790 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
3792 if (force_scofix || id->driver_info & BTUSB_WRONG_SCO_MTU) {
3793 if (!disable_scofix)
3794 set_bit(HCI_QUIRK_FIXUP_BUFFER_SIZE, &hdev->quirks);
3797 if (id->driver_info & BTUSB_BROKEN_ISOC)
3800 if (id->driver_info & BTUSB_DIGIANSWER) {
3801 data->cmdreq_type = USB_TYPE_VENDOR;
3802 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
3805 if (id->driver_info & BTUSB_CSR) {
3806 struct usb_device *udev = data->udev;
3807 u16 bcdDevice = le16_to_cpu(udev->descriptor.bcdDevice);
3809 /* Old firmware would otherwise execute USB reset */
3810 if (bcdDevice < 0x117)
3811 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
3813 /* Fake CSR devices with broken commands */
3814 if (bcdDevice <= 0x100 || bcdDevice == 0x134)
3815 hdev->setup = btusb_setup_csr;
3817 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
3820 if (id->driver_info & BTUSB_SNIFFER) {
3821 struct usb_device *udev = data->udev;
3823 /* New sniffer firmware has crippled HCI interface */
3824 if (le16_to_cpu(udev->descriptor.bcdDevice) > 0x997)
3825 set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
3828 if (id->driver_info & BTUSB_INTEL_BOOT) {
3829 /* A bug in the bootloader causes that interrupt interface is
3830 * only enabled after receiving SetInterface(0, AltSetting=0).
3832 err = usb_set_interface(data->udev, 0, 0);
3834 BT_ERR("failed to set interface 0, alt 0 %d", err);
3840 err = usb_driver_claim_interface(&btusb_driver,
3846 #ifdef CONFIG_BT_HCIBTUSB_BCM
3848 if (!usb_driver_claim_interface(&btusb_driver,
3850 __set_diag_interface(hdev);
3856 if (enable_autosuspend)
3857 usb_enable_autosuspend(data->udev);
3859 err = hci_register_dev(hdev);
3863 usb_set_intfdata(intf, data);
3868 if (data->reset_gpio)
3869 gpiod_put(data->reset_gpio);
3874 static void btusb_disconnect(struct usb_interface *intf)
3876 struct btusb_data *data = usb_get_intfdata(intf);
3877 struct hci_dev *hdev;
3879 BT_DBG("intf %p", intf);
3885 usb_set_intfdata(data->intf, NULL);
3888 usb_set_intfdata(data->isoc, NULL);
3891 usb_set_intfdata(data->diag, NULL);
3893 hci_unregister_dev(hdev);
3895 if (intf == data->intf) {
3897 usb_driver_release_interface(&btusb_driver, data->isoc);
3899 usb_driver_release_interface(&btusb_driver, data->diag);
3900 } else if (intf == data->isoc) {
3902 usb_driver_release_interface(&btusb_driver, data->diag);
3903 usb_driver_release_interface(&btusb_driver, data->intf);
3904 } else if (intf == data->diag) {
3905 usb_driver_release_interface(&btusb_driver, data->intf);
3907 usb_driver_release_interface(&btusb_driver, data->isoc);
3910 if (data->oob_wake_irq)
3911 device_init_wakeup(&data->udev->dev, false);
3913 if (data->reset_gpio)
3914 gpiod_put(data->reset_gpio);
3920 static int btusb_suspend(struct usb_interface *intf, pm_message_t message)
3922 struct btusb_data *data = usb_get_intfdata(intf);
3924 BT_DBG("intf %p", intf);
3926 if (data->suspend_count++)
3929 spin_lock_irq(&data->txlock);
3930 if (!(PMSG_IS_AUTO(message) && data->tx_in_flight)) {
3931 set_bit(BTUSB_SUSPENDING, &data->flags);
3932 spin_unlock_irq(&data->txlock);
3934 spin_unlock_irq(&data->txlock);
3935 data->suspend_count--;
3939 cancel_work_sync(&data->work);
3941 btusb_stop_traffic(data);
3942 usb_kill_anchored_urbs(&data->tx_anchor);
3944 if (data->oob_wake_irq && device_may_wakeup(&data->udev->dev)) {
3945 set_bit(BTUSB_OOB_WAKE_ENABLED, &data->flags);
3946 enable_irq_wake(data->oob_wake_irq);
3947 enable_irq(data->oob_wake_irq);
3953 static void play_deferred(struct btusb_data *data)
3958 while ((urb = usb_get_from_anchor(&data->deferred))) {
3959 usb_anchor_urb(urb, &data->tx_anchor);
3961 err = usb_submit_urb(urb, GFP_ATOMIC);
3963 if (err != -EPERM && err != -ENODEV)
3964 BT_ERR("%s urb %p submission failed (%d)",
3965 data->hdev->name, urb, -err);
3966 kfree(urb->setup_packet);
3967 usb_unanchor_urb(urb);
3972 data->tx_in_flight++;
3976 /* Cleanup the rest deferred urbs. */
3977 while ((urb = usb_get_from_anchor(&data->deferred))) {
3978 kfree(urb->setup_packet);
3983 static int btusb_resume(struct usb_interface *intf)
3985 struct btusb_data *data = usb_get_intfdata(intf);
3986 struct hci_dev *hdev = data->hdev;
3989 BT_DBG("intf %p", intf);
3991 if (--data->suspend_count)
3994 /* Disable only if not already disabled (keep it balanced) */
3995 if (test_and_clear_bit(BTUSB_OOB_WAKE_ENABLED, &data->flags)) {
3996 disable_irq(data->oob_wake_irq);
3997 disable_irq_wake(data->oob_wake_irq);
4000 if (!test_bit(HCI_RUNNING, &hdev->flags))
4003 if (test_bit(BTUSB_INTR_RUNNING, &data->flags)) {
4004 err = btusb_submit_intr_urb(hdev, GFP_NOIO);
4006 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
4011 if (test_bit(BTUSB_BULK_RUNNING, &data->flags)) {
4012 err = btusb_submit_bulk_urb(hdev, GFP_NOIO);
4014 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
4018 btusb_submit_bulk_urb(hdev, GFP_NOIO);
4021 if (test_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
4022 if (btusb_submit_isoc_urb(hdev, GFP_NOIO) < 0)
4023 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
4025 btusb_submit_isoc_urb(hdev, GFP_NOIO);
4028 spin_lock_irq(&data->txlock);
4029 play_deferred(data);
4030 clear_bit(BTUSB_SUSPENDING, &data->flags);
4031 spin_unlock_irq(&data->txlock);
4032 schedule_work(&data->work);
4037 usb_scuttle_anchored_urbs(&data->deferred);
4039 spin_lock_irq(&data->txlock);
4040 clear_bit(BTUSB_SUSPENDING, &data->flags);
4041 spin_unlock_irq(&data->txlock);
4047 static struct usb_driver btusb_driver = {
4049 .probe = btusb_probe,
4050 .disconnect = btusb_disconnect,
4052 .suspend = btusb_suspend,
4053 .resume = btusb_resume,
4055 .id_table = btusb_table,
4056 .supports_autosuspend = 1,
4057 .disable_hub_initiated_lpm = 1,
4060 module_usb_driver(btusb_driver);
4062 module_param(disable_scofix, bool, 0644);
4063 MODULE_PARM_DESC(disable_scofix, "Disable fixup of wrong SCO buffer size");
4065 module_param(force_scofix, bool, 0644);
4066 MODULE_PARM_DESC(force_scofix, "Force fixup of wrong SCO buffers size");
4068 module_param(enable_autosuspend, bool, 0644);
4069 MODULE_PARM_DESC(enable_autosuspend, "Enable USB autosuspend by default");
4071 module_param(reset, bool, 0644);
4072 MODULE_PARM_DESC(reset, "Send HCI reset command on initialization");
4074 MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
4075 MODULE_DESCRIPTION("Generic Bluetooth USB driver ver " VERSION);
4076 MODULE_VERSION(VERSION);
4077 MODULE_LICENSE("GPL");