3 * Generic Bluetooth USB driver
5 * Copyright (C) 2005-2008 Marcel Holtmann <marcel@holtmann.org>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include <linux/dmi.h>
25 #include <linux/module.h>
26 #include <linux/usb.h>
27 #include <linux/usb/quirks.h>
28 #include <linux/firmware.h>
29 #include <linux/of_device.h>
30 #include <linux/of_irq.h>
31 #include <linux/suspend.h>
32 #include <asm/unaligned.h>
34 #include <net/bluetooth/bluetooth.h>
35 #include <net/bluetooth/hci_core.h>
43 static bool disable_scofix;
44 static bool force_scofix;
46 static bool reset = true;
48 static struct usb_driver btusb_driver;
50 #define BTUSB_IGNORE 0x01
51 #define BTUSB_DIGIANSWER 0x02
52 #define BTUSB_CSR 0x04
53 #define BTUSB_SNIFFER 0x08
54 #define BTUSB_BCM92035 0x10
55 #define BTUSB_BROKEN_ISOC 0x20
56 #define BTUSB_WRONG_SCO_MTU 0x40
57 #define BTUSB_ATH3012 0x80
58 #define BTUSB_INTEL 0x100
59 #define BTUSB_INTEL_BOOT 0x200
60 #define BTUSB_BCM_PATCHRAM 0x400
61 #define BTUSB_MARVELL 0x800
62 #define BTUSB_SWAVE 0x1000
63 #define BTUSB_INTEL_NEW 0x2000
64 #define BTUSB_AMP 0x4000
65 #define BTUSB_QCA_ROME 0x8000
66 #define BTUSB_BCM_APPLE 0x10000
67 #define BTUSB_REALTEK 0x20000
68 #define BTUSB_BCM2045 0x40000
69 #define BTUSB_IFNUM_2 0x80000
70 #define BTUSB_CW6622 0x100000
71 #define BTUSB_BCM_NO_PRODID 0x200000
73 static const struct usb_device_id btusb_table[] = {
74 /* Generic Bluetooth USB device */
75 { USB_DEVICE_INFO(0xe0, 0x01, 0x01) },
77 /* Generic Bluetooth AMP device */
78 { USB_DEVICE_INFO(0xe0, 0x01, 0x04), .driver_info = BTUSB_AMP },
80 /* Generic Bluetooth USB interface */
81 { USB_INTERFACE_INFO(0xe0, 0x01, 0x01) },
83 /* Apple-specific (Broadcom) devices */
84 { USB_VENDOR_AND_INTERFACE_INFO(0x05ac, 0xff, 0x01, 0x01),
85 .driver_info = BTUSB_BCM_APPLE | BTUSB_IFNUM_2 },
87 /* MediaTek MT76x0E */
88 { USB_DEVICE(0x0e8d, 0x763f) },
90 /* Broadcom SoftSailing reporting vendor specific */
91 { USB_DEVICE(0x0a5c, 0x21e1) },
93 /* Apple MacBookPro 7,1 */
94 { USB_DEVICE(0x05ac, 0x8213) },
97 { USB_DEVICE(0x05ac, 0x8215) },
99 /* Apple MacBookPro6,2 */
100 { USB_DEVICE(0x05ac, 0x8218) },
102 /* Apple MacBookAir3,1, MacBookAir3,2 */
103 { USB_DEVICE(0x05ac, 0x821b) },
105 /* Apple MacBookAir4,1 */
106 { USB_DEVICE(0x05ac, 0x821f) },
108 /* Apple MacBookPro8,2 */
109 { USB_DEVICE(0x05ac, 0x821a) },
111 /* Apple MacMini5,1 */
112 { USB_DEVICE(0x05ac, 0x8281) },
114 /* AVM BlueFRITZ! USB v2.0 */
115 { USB_DEVICE(0x057c, 0x3800), .driver_info = BTUSB_SWAVE },
117 /* Bluetooth Ultraport Module from IBM */
118 { USB_DEVICE(0x04bf, 0x030a) },
120 /* ALPS Modules with non-standard id */
121 { USB_DEVICE(0x044e, 0x3001) },
122 { USB_DEVICE(0x044e, 0x3002) },
124 /* Ericsson with non-standard id */
125 { USB_DEVICE(0x0bdb, 0x1002) },
127 /* Canyon CN-BTU1 with HID interfaces */
128 { USB_DEVICE(0x0c10, 0x0000) },
130 /* Broadcom BCM20702A0 */
131 { USB_DEVICE(0x413c, 0x8197) },
133 /* Broadcom BCM20702B0 (Dynex/Insignia) */
134 { USB_DEVICE(0x19ff, 0x0239), .driver_info = BTUSB_BCM_PATCHRAM },
136 /* Broadcom BCM43142A0 (Foxconn/Lenovo) */
137 { USB_VENDOR_AND_INTERFACE_INFO(0x105b, 0xff, 0x01, 0x01),
138 .driver_info = BTUSB_BCM_PATCHRAM },
140 /* Broadcom BCM920703 (HTC Vive) */
141 { USB_VENDOR_AND_INTERFACE_INFO(0x0bb4, 0xff, 0x01, 0x01),
142 .driver_info = BTUSB_BCM_PATCHRAM },
144 /* Foxconn - Hon Hai */
145 { USB_VENDOR_AND_INTERFACE_INFO(0x0489, 0xff, 0x01, 0x01),
146 .driver_info = BTUSB_BCM_PATCHRAM },
148 /* Lite-On Technology - Broadcom based */
149 { USB_VENDOR_AND_INTERFACE_INFO(0x04ca, 0xff, 0x01, 0x01),
150 .driver_info = BTUSB_BCM_PATCHRAM },
152 /* Broadcom devices with vendor specific id */
153 { USB_VENDOR_AND_INTERFACE_INFO(0x0a5c, 0xff, 0x01, 0x01),
154 .driver_info = BTUSB_BCM_PATCHRAM },
156 /* ASUSTek Computer - Broadcom based */
157 { USB_VENDOR_AND_INTERFACE_INFO(0x0b05, 0xff, 0x01, 0x01),
158 .driver_info = BTUSB_BCM_PATCHRAM },
160 /* Belkin F8065bf - Broadcom based */
161 { USB_VENDOR_AND_INTERFACE_INFO(0x050d, 0xff, 0x01, 0x01),
162 .driver_info = BTUSB_BCM_PATCHRAM },
164 /* IMC Networks - Broadcom based */
165 { USB_VENDOR_AND_INTERFACE_INFO(0x13d3, 0xff, 0x01, 0x01),
166 .driver_info = BTUSB_BCM_PATCHRAM },
168 /* Dell Computer - Broadcom based */
169 { USB_VENDOR_AND_INTERFACE_INFO(0x413c, 0xff, 0x01, 0x01),
170 .driver_info = BTUSB_BCM_PATCHRAM },
172 /* Toshiba Corp - Broadcom based */
173 { USB_VENDOR_AND_INTERFACE_INFO(0x0930, 0xff, 0x01, 0x01),
174 .driver_info = BTUSB_BCM_PATCHRAM },
176 /* Broadcom devices with missing product id */
177 { USB_DEVICE_AND_INTERFACE_INFO(0x0000, 0x0000, 0xff, 0x01, 0x01),
178 .driver_info = BTUSB_BCM_PATCHRAM | BTUSB_BCM_NO_PRODID },
180 /* Intel Bluetooth USB Bootloader (RAM module) */
181 { USB_DEVICE(0x8087, 0x0a5a),
182 .driver_info = BTUSB_INTEL_BOOT | BTUSB_BROKEN_ISOC },
184 { } /* Terminating entry */
187 MODULE_DEVICE_TABLE(usb, btusb_table);
189 static const struct usb_device_id blacklist_table[] = {
190 /* CSR BlueCore devices */
191 { USB_DEVICE(0x0a12, 0x0001), .driver_info = BTUSB_CSR },
193 /* Broadcom BCM2033 without firmware */
194 { USB_DEVICE(0x0a5c, 0x2033), .driver_info = BTUSB_IGNORE },
196 /* Broadcom BCM2045 devices */
197 { USB_DEVICE(0x0a5c, 0x2045), .driver_info = BTUSB_BCM2045 },
199 /* Atheros 3011 with sflash firmware */
200 { USB_DEVICE(0x0489, 0xe027), .driver_info = BTUSB_IGNORE },
201 { USB_DEVICE(0x0489, 0xe03d), .driver_info = BTUSB_IGNORE },
202 { USB_DEVICE(0x04f2, 0xaff1), .driver_info = BTUSB_IGNORE },
203 { USB_DEVICE(0x0930, 0x0215), .driver_info = BTUSB_IGNORE },
204 { USB_DEVICE(0x0cf3, 0x3002), .driver_info = BTUSB_IGNORE },
205 { USB_DEVICE(0x0cf3, 0xe019), .driver_info = BTUSB_IGNORE },
206 { USB_DEVICE(0x13d3, 0x3304), .driver_info = BTUSB_IGNORE },
208 /* Atheros AR9285 Malbec with sflash firmware */
209 { USB_DEVICE(0x03f0, 0x311d), .driver_info = BTUSB_IGNORE },
211 /* Atheros 3012 with sflash firmware */
212 { USB_DEVICE(0x0489, 0xe04d), .driver_info = BTUSB_ATH3012 },
213 { USB_DEVICE(0x0489, 0xe04e), .driver_info = BTUSB_ATH3012 },
214 { USB_DEVICE(0x0489, 0xe056), .driver_info = BTUSB_ATH3012 },
215 { USB_DEVICE(0x0489, 0xe057), .driver_info = BTUSB_ATH3012 },
216 { USB_DEVICE(0x0489, 0xe05f), .driver_info = BTUSB_ATH3012 },
217 { USB_DEVICE(0x0489, 0xe076), .driver_info = BTUSB_ATH3012 },
218 { USB_DEVICE(0x0489, 0xe078), .driver_info = BTUSB_ATH3012 },
219 { USB_DEVICE(0x0489, 0xe095), .driver_info = BTUSB_ATH3012 },
220 { USB_DEVICE(0x04c5, 0x1330), .driver_info = BTUSB_ATH3012 },
221 { USB_DEVICE(0x04ca, 0x3004), .driver_info = BTUSB_ATH3012 },
222 { USB_DEVICE(0x04ca, 0x3005), .driver_info = BTUSB_ATH3012 },
223 { USB_DEVICE(0x04ca, 0x3006), .driver_info = BTUSB_ATH3012 },
224 { USB_DEVICE(0x04ca, 0x3007), .driver_info = BTUSB_ATH3012 },
225 { USB_DEVICE(0x04ca, 0x3008), .driver_info = BTUSB_ATH3012 },
226 { USB_DEVICE(0x04ca, 0x300b), .driver_info = BTUSB_ATH3012 },
227 { USB_DEVICE(0x04ca, 0x300d), .driver_info = BTUSB_ATH3012 },
228 { USB_DEVICE(0x04ca, 0x300f), .driver_info = BTUSB_ATH3012 },
229 { USB_DEVICE(0x04ca, 0x3010), .driver_info = BTUSB_ATH3012 },
230 { USB_DEVICE(0x04ca, 0x3014), .driver_info = BTUSB_ATH3012 },
231 { USB_DEVICE(0x04ca, 0x3018), .driver_info = BTUSB_ATH3012 },
232 { USB_DEVICE(0x0930, 0x0219), .driver_info = BTUSB_ATH3012 },
233 { USB_DEVICE(0x0930, 0x021c), .driver_info = BTUSB_ATH3012 },
234 { USB_DEVICE(0x0930, 0x0220), .driver_info = BTUSB_ATH3012 },
235 { USB_DEVICE(0x0930, 0x0227), .driver_info = BTUSB_ATH3012 },
236 { USB_DEVICE(0x0b05, 0x17d0), .driver_info = BTUSB_ATH3012 },
237 { USB_DEVICE(0x0cf3, 0x0036), .driver_info = BTUSB_ATH3012 },
238 { USB_DEVICE(0x0cf3, 0x3004), .driver_info = BTUSB_ATH3012 },
239 { USB_DEVICE(0x0cf3, 0x3008), .driver_info = BTUSB_ATH3012 },
240 { USB_DEVICE(0x0cf3, 0x311d), .driver_info = BTUSB_ATH3012 },
241 { USB_DEVICE(0x0cf3, 0x311e), .driver_info = BTUSB_ATH3012 },
242 { USB_DEVICE(0x0cf3, 0x311f), .driver_info = BTUSB_ATH3012 },
243 { USB_DEVICE(0x0cf3, 0x3121), .driver_info = BTUSB_ATH3012 },
244 { USB_DEVICE(0x0cf3, 0x817a), .driver_info = BTUSB_ATH3012 },
245 { USB_DEVICE(0x0cf3, 0x817b), .driver_info = BTUSB_ATH3012 },
246 { USB_DEVICE(0x0cf3, 0xe003), .driver_info = BTUSB_ATH3012 },
247 { USB_DEVICE(0x0cf3, 0xe004), .driver_info = BTUSB_ATH3012 },
248 { USB_DEVICE(0x0cf3, 0xe005), .driver_info = BTUSB_ATH3012 },
249 { USB_DEVICE(0x0cf3, 0xe006), .driver_info = BTUSB_ATH3012 },
250 { USB_DEVICE(0x13d3, 0x3362), .driver_info = BTUSB_ATH3012 },
251 { USB_DEVICE(0x13d3, 0x3375), .driver_info = BTUSB_ATH3012 },
252 { USB_DEVICE(0x13d3, 0x3393), .driver_info = BTUSB_ATH3012 },
253 { USB_DEVICE(0x13d3, 0x3395), .driver_info = BTUSB_ATH3012 },
254 { USB_DEVICE(0x13d3, 0x3402), .driver_info = BTUSB_ATH3012 },
255 { USB_DEVICE(0x13d3, 0x3408), .driver_info = BTUSB_ATH3012 },
256 { USB_DEVICE(0x13d3, 0x3423), .driver_info = BTUSB_ATH3012 },
257 { USB_DEVICE(0x13d3, 0x3432), .driver_info = BTUSB_ATH3012 },
258 { USB_DEVICE(0x13d3, 0x3472), .driver_info = BTUSB_ATH3012 },
259 { USB_DEVICE(0x13d3, 0x3474), .driver_info = BTUSB_ATH3012 },
260 { USB_DEVICE(0x13d3, 0x3487), .driver_info = BTUSB_ATH3012 },
261 { USB_DEVICE(0x13d3, 0x3490), .driver_info = BTUSB_ATH3012 },
263 /* Atheros AR5BBU12 with sflash firmware */
264 { USB_DEVICE(0x0489, 0xe02c), .driver_info = BTUSB_IGNORE },
266 /* Atheros AR5BBU12 with sflash firmware */
267 { USB_DEVICE(0x0489, 0xe036), .driver_info = BTUSB_ATH3012 },
268 { USB_DEVICE(0x0489, 0xe03c), .driver_info = BTUSB_ATH3012 },
270 /* QCA ROME chipset */
271 { USB_DEVICE(0x0cf3, 0xe007), .driver_info = BTUSB_QCA_ROME },
272 { USB_DEVICE(0x0cf3, 0xe009), .driver_info = BTUSB_QCA_ROME },
273 { USB_DEVICE(0x0cf3, 0xe300), .driver_info = BTUSB_QCA_ROME },
274 { USB_DEVICE(0x0cf3, 0xe301), .driver_info = BTUSB_QCA_ROME },
275 { USB_DEVICE(0x0cf3, 0xe360), .driver_info = BTUSB_QCA_ROME },
276 { USB_DEVICE(0x0489, 0xe092), .driver_info = BTUSB_QCA_ROME },
277 { USB_DEVICE(0x0489, 0xe09f), .driver_info = BTUSB_QCA_ROME },
278 { USB_DEVICE(0x0489, 0xe0a2), .driver_info = BTUSB_QCA_ROME },
279 { USB_DEVICE(0x04ca, 0x3011), .driver_info = BTUSB_QCA_ROME },
280 { USB_DEVICE(0x04ca, 0x3015), .driver_info = BTUSB_QCA_ROME },
281 { USB_DEVICE(0x04ca, 0x3016), .driver_info = BTUSB_QCA_ROME },
282 { USB_DEVICE(0x04ca, 0x301a), .driver_info = BTUSB_QCA_ROME },
284 /* Broadcom BCM2035 */
285 { USB_DEVICE(0x0a5c, 0x2009), .driver_info = BTUSB_BCM92035 },
286 { USB_DEVICE(0x0a5c, 0x200a), .driver_info = BTUSB_WRONG_SCO_MTU },
287 { USB_DEVICE(0x0a5c, 0x2035), .driver_info = BTUSB_WRONG_SCO_MTU },
289 /* Broadcom BCM2045 */
290 { USB_DEVICE(0x0a5c, 0x2039), .driver_info = BTUSB_WRONG_SCO_MTU },
291 { USB_DEVICE(0x0a5c, 0x2101), .driver_info = BTUSB_WRONG_SCO_MTU },
293 /* IBM/Lenovo ThinkPad with Broadcom chip */
294 { USB_DEVICE(0x0a5c, 0x201e), .driver_info = BTUSB_WRONG_SCO_MTU },
295 { USB_DEVICE(0x0a5c, 0x2110), .driver_info = BTUSB_WRONG_SCO_MTU },
297 /* HP laptop with Broadcom chip */
298 { USB_DEVICE(0x03f0, 0x171d), .driver_info = BTUSB_WRONG_SCO_MTU },
300 /* Dell laptop with Broadcom chip */
301 { USB_DEVICE(0x413c, 0x8126), .driver_info = BTUSB_WRONG_SCO_MTU },
303 /* Dell Wireless 370 and 410 devices */
304 { USB_DEVICE(0x413c, 0x8152), .driver_info = BTUSB_WRONG_SCO_MTU },
305 { USB_DEVICE(0x413c, 0x8156), .driver_info = BTUSB_WRONG_SCO_MTU },
307 /* Belkin F8T012 and F8T013 devices */
308 { USB_DEVICE(0x050d, 0x0012), .driver_info = BTUSB_WRONG_SCO_MTU },
309 { USB_DEVICE(0x050d, 0x0013), .driver_info = BTUSB_WRONG_SCO_MTU },
311 /* Asus WL-BTD202 device */
312 { USB_DEVICE(0x0b05, 0x1715), .driver_info = BTUSB_WRONG_SCO_MTU },
314 /* Kensington Bluetooth USB adapter */
315 { USB_DEVICE(0x047d, 0x105e), .driver_info = BTUSB_WRONG_SCO_MTU },
317 /* RTX Telecom based adapters with buggy SCO support */
318 { USB_DEVICE(0x0400, 0x0807), .driver_info = BTUSB_BROKEN_ISOC },
319 { USB_DEVICE(0x0400, 0x080a), .driver_info = BTUSB_BROKEN_ISOC },
321 /* CONWISE Technology based adapters with buggy SCO support */
322 { USB_DEVICE(0x0e5e, 0x6622),
323 .driver_info = BTUSB_BROKEN_ISOC | BTUSB_CW6622},
325 /* Roper Class 1 Bluetooth Dongle (Silicon Wave based) */
326 { USB_DEVICE(0x1310, 0x0001), .driver_info = BTUSB_SWAVE },
328 /* Digianswer devices */
329 { USB_DEVICE(0x08fd, 0x0001), .driver_info = BTUSB_DIGIANSWER },
330 { USB_DEVICE(0x08fd, 0x0002), .driver_info = BTUSB_IGNORE },
332 /* CSR BlueCore Bluetooth Sniffer */
333 { USB_DEVICE(0x0a12, 0x0002),
334 .driver_info = BTUSB_SNIFFER | BTUSB_BROKEN_ISOC },
336 /* Frontline ComProbe Bluetooth Sniffer */
337 { USB_DEVICE(0x16d3, 0x0002),
338 .driver_info = BTUSB_SNIFFER | BTUSB_BROKEN_ISOC },
340 /* Marvell Bluetooth devices */
341 { USB_DEVICE(0x1286, 0x2044), .driver_info = BTUSB_MARVELL },
342 { USB_DEVICE(0x1286, 0x2046), .driver_info = BTUSB_MARVELL },
343 { USB_DEVICE(0x1286, 0x204e), .driver_info = BTUSB_MARVELL },
345 /* Intel Bluetooth devices */
346 { USB_DEVICE(0x8087, 0x0025), .driver_info = BTUSB_INTEL_NEW },
347 { USB_DEVICE(0x8087, 0x07da), .driver_info = BTUSB_CSR },
348 { USB_DEVICE(0x8087, 0x07dc), .driver_info = BTUSB_INTEL },
349 { USB_DEVICE(0x8087, 0x0a2a), .driver_info = BTUSB_INTEL },
350 { USB_DEVICE(0x8087, 0x0a2b), .driver_info = BTUSB_INTEL_NEW },
351 { USB_DEVICE(0x8087, 0x0aa7), .driver_info = BTUSB_INTEL },
352 { USB_DEVICE(0x8087, 0x0aaa), .driver_info = BTUSB_INTEL_NEW },
354 /* Other Intel Bluetooth devices */
355 { USB_VENDOR_AND_INTERFACE_INFO(0x8087, 0xe0, 0x01, 0x01),
356 .driver_info = BTUSB_IGNORE },
358 /* Realtek Bluetooth devices */
359 { USB_VENDOR_AND_INTERFACE_INFO(0x0bda, 0xe0, 0x01, 0x01),
360 .driver_info = BTUSB_REALTEK },
362 /* Additional Realtek 8723AE Bluetooth devices */
363 { USB_DEVICE(0x0930, 0x021d), .driver_info = BTUSB_REALTEK },
364 { USB_DEVICE(0x13d3, 0x3394), .driver_info = BTUSB_REALTEK },
366 /* Additional Realtek 8723BE Bluetooth devices */
367 { USB_DEVICE(0x0489, 0xe085), .driver_info = BTUSB_REALTEK },
368 { USB_DEVICE(0x0489, 0xe08b), .driver_info = BTUSB_REALTEK },
369 { USB_DEVICE(0x13d3, 0x3410), .driver_info = BTUSB_REALTEK },
370 { USB_DEVICE(0x13d3, 0x3416), .driver_info = BTUSB_REALTEK },
371 { USB_DEVICE(0x13d3, 0x3459), .driver_info = BTUSB_REALTEK },
372 { USB_DEVICE(0x13d3, 0x3494), .driver_info = BTUSB_REALTEK },
374 /* Additional Realtek 8723BU Bluetooth devices */
375 { USB_DEVICE(0x7392, 0xa611), .driver_info = BTUSB_REALTEK },
377 /* Additional Realtek 8723DE Bluetooth devices */
378 { USB_DEVICE(0x0bda, 0xb009), .driver_info = BTUSB_REALTEK },
379 { USB_DEVICE(0x2ff8, 0xb011), .driver_info = BTUSB_REALTEK },
381 /* Additional Realtek 8821AE Bluetooth devices */
382 { USB_DEVICE(0x0b05, 0x17dc), .driver_info = BTUSB_REALTEK },
383 { USB_DEVICE(0x13d3, 0x3414), .driver_info = BTUSB_REALTEK },
384 { USB_DEVICE(0x13d3, 0x3458), .driver_info = BTUSB_REALTEK },
385 { USB_DEVICE(0x13d3, 0x3461), .driver_info = BTUSB_REALTEK },
386 { USB_DEVICE(0x13d3, 0x3462), .driver_info = BTUSB_REALTEK },
388 /* Additional Realtek 8822BE Bluetooth devices */
389 { USB_DEVICE(0x0b05, 0x185c), .driver_info = BTUSB_REALTEK },
391 /* Additional Realtek 8822CE Bluetooth devices */
392 { USB_DEVICE(0x04ca, 0x4005), .driver_info = BTUSB_REALTEK },
394 /* Silicon Wave based devices */
395 { USB_DEVICE(0x0c10, 0x0000), .driver_info = BTUSB_SWAVE },
397 { } /* Terminating entry */
400 /* The Bluetooth USB module build into some devices needs to be reset on resume,
401 * this is a problem with the platform (likely shutting off all power) not with
402 * the module itself. So we use a DMI list to match known broken platforms.
404 static const struct dmi_system_id btusb_needs_reset_resume_table[] = {
406 /* Dell OptiPlex 3060 (QCA ROME device 0cf3:e007) */
408 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
409 DMI_MATCH(DMI_PRODUCT_NAME, "OptiPlex 3060"),
413 /* Dell XPS 9360 (QCA ROME device 0cf3:e300) */
415 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
416 DMI_MATCH(DMI_PRODUCT_NAME, "XPS 13 9360"),
422 #define BTUSB_MAX_ISOC_FRAMES 10
424 #define BTUSB_INTR_RUNNING 0
425 #define BTUSB_BULK_RUNNING 1
426 #define BTUSB_ISOC_RUNNING 2
427 #define BTUSB_SUSPENDING 3
428 #define BTUSB_DID_ISO_RESUME 4
429 #define BTUSB_BOOTLOADER 5
430 #define BTUSB_DOWNLOADING 6
431 #define BTUSB_FIRMWARE_LOADED 7
432 #define BTUSB_FIRMWARE_FAILED 8
433 #define BTUSB_BOOTING 9
434 #define BTUSB_DIAG_RUNNING 10
435 #define BTUSB_OOB_WAKE_ENABLED 11
438 struct hci_dev *hdev;
439 struct usb_device *udev;
440 struct usb_interface *intf;
441 struct usb_interface *isoc;
442 struct usb_interface *diag;
446 struct work_struct work;
447 struct work_struct waker;
449 struct usb_anchor deferred;
450 struct usb_anchor tx_anchor;
454 struct usb_anchor intr_anchor;
455 struct usb_anchor bulk_anchor;
456 struct usb_anchor isoc_anchor;
457 struct usb_anchor diag_anchor;
460 struct sk_buff *evt_skb;
461 struct sk_buff *acl_skb;
462 struct sk_buff *sco_skb;
464 struct usb_endpoint_descriptor *intr_ep;
465 struct usb_endpoint_descriptor *bulk_tx_ep;
466 struct usb_endpoint_descriptor *bulk_rx_ep;
467 struct usb_endpoint_descriptor *isoc_tx_ep;
468 struct usb_endpoint_descriptor *isoc_rx_ep;
469 struct usb_endpoint_descriptor *diag_tx_ep;
470 struct usb_endpoint_descriptor *diag_rx_ep;
475 unsigned int sco_num;
479 int (*recv_event)(struct hci_dev *hdev, struct sk_buff *skb);
480 int (*recv_bulk)(struct btusb_data *data, void *buffer, int count);
482 int (*setup_on_usb)(struct hci_dev *hdev);
484 int oob_wake_irq; /* irq for out-of-band wake-on-bt */
487 static inline void btusb_free_frags(struct btusb_data *data)
491 spin_lock_irqsave(&data->rxlock, flags);
493 kfree_skb(data->evt_skb);
494 data->evt_skb = NULL;
496 kfree_skb(data->acl_skb);
497 data->acl_skb = NULL;
499 kfree_skb(data->sco_skb);
500 data->sco_skb = NULL;
502 spin_unlock_irqrestore(&data->rxlock, flags);
505 static int btusb_recv_intr(struct btusb_data *data, void *buffer, int count)
510 spin_lock(&data->rxlock);
517 skb = bt_skb_alloc(HCI_MAX_EVENT_SIZE, GFP_ATOMIC);
523 hci_skb_pkt_type(skb) = HCI_EVENT_PKT;
524 hci_skb_expect(skb) = HCI_EVENT_HDR_SIZE;
527 len = min_t(uint, hci_skb_expect(skb), count);
528 skb_put_data(skb, buffer, len);
532 hci_skb_expect(skb) -= len;
534 if (skb->len == HCI_EVENT_HDR_SIZE) {
535 /* Complete event header */
536 hci_skb_expect(skb) = hci_event_hdr(skb)->plen;
538 if (skb_tailroom(skb) < hci_skb_expect(skb)) {
547 if (!hci_skb_expect(skb)) {
549 data->recv_event(data->hdev, skb);
555 spin_unlock(&data->rxlock);
560 static int btusb_recv_bulk(struct btusb_data *data, void *buffer, int count)
565 spin_lock(&data->rxlock);
572 skb = bt_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC);
578 hci_skb_pkt_type(skb) = HCI_ACLDATA_PKT;
579 hci_skb_expect(skb) = HCI_ACL_HDR_SIZE;
582 len = min_t(uint, hci_skb_expect(skb), count);
583 skb_put_data(skb, buffer, len);
587 hci_skb_expect(skb) -= len;
589 if (skb->len == HCI_ACL_HDR_SIZE) {
590 __le16 dlen = hci_acl_hdr(skb)->dlen;
592 /* Complete ACL header */
593 hci_skb_expect(skb) = __le16_to_cpu(dlen);
595 if (skb_tailroom(skb) < hci_skb_expect(skb)) {
604 if (!hci_skb_expect(skb)) {
606 hci_recv_frame(data->hdev, skb);
612 spin_unlock(&data->rxlock);
617 static int btusb_recv_isoc(struct btusb_data *data, void *buffer, int count)
622 spin_lock(&data->rxlock);
629 skb = bt_skb_alloc(HCI_MAX_SCO_SIZE, GFP_ATOMIC);
635 hci_skb_pkt_type(skb) = HCI_SCODATA_PKT;
636 hci_skb_expect(skb) = HCI_SCO_HDR_SIZE;
639 len = min_t(uint, hci_skb_expect(skb), count);
640 skb_put_data(skb, buffer, len);
644 hci_skb_expect(skb) -= len;
646 if (skb->len == HCI_SCO_HDR_SIZE) {
647 /* Complete SCO header */
648 hci_skb_expect(skb) = hci_sco_hdr(skb)->dlen;
650 if (skb_tailroom(skb) < hci_skb_expect(skb)) {
659 if (!hci_skb_expect(skb)) {
661 hci_recv_frame(data->hdev, skb);
667 spin_unlock(&data->rxlock);
672 static void btusb_intr_complete(struct urb *urb)
674 struct hci_dev *hdev = urb->context;
675 struct btusb_data *data = hci_get_drvdata(hdev);
678 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
681 if (!test_bit(HCI_RUNNING, &hdev->flags))
684 if (urb->status == 0) {
685 hdev->stat.byte_rx += urb->actual_length;
687 if (btusb_recv_intr(data, urb->transfer_buffer,
688 urb->actual_length) < 0) {
689 BT_ERR("%s corrupted event packet", hdev->name);
692 } else if (urb->status == -ENOENT) {
693 /* Avoid suspend failed when usb_kill_urb */
697 if (!test_bit(BTUSB_INTR_RUNNING, &data->flags))
700 usb_mark_last_busy(data->udev);
701 usb_anchor_urb(urb, &data->intr_anchor);
703 err = usb_submit_urb(urb, GFP_ATOMIC);
705 /* -EPERM: urb is being killed;
706 * -ENODEV: device got disconnected
708 if (err != -EPERM && err != -ENODEV)
709 BT_ERR("%s urb %p failed to resubmit (%d)",
710 hdev->name, urb, -err);
711 usb_unanchor_urb(urb);
715 static int btusb_submit_intr_urb(struct hci_dev *hdev, gfp_t mem_flags)
717 struct btusb_data *data = hci_get_drvdata(hdev);
723 BT_DBG("%s", hdev->name);
728 urb = usb_alloc_urb(0, mem_flags);
732 size = le16_to_cpu(data->intr_ep->wMaxPacketSize);
734 buf = kmalloc(size, mem_flags);
740 pipe = usb_rcvintpipe(data->udev, data->intr_ep->bEndpointAddress);
742 usb_fill_int_urb(urb, data->udev, pipe, buf, size,
743 btusb_intr_complete, hdev, data->intr_ep->bInterval);
745 urb->transfer_flags |= URB_FREE_BUFFER;
747 usb_anchor_urb(urb, &data->intr_anchor);
749 err = usb_submit_urb(urb, mem_flags);
751 if (err != -EPERM && err != -ENODEV)
752 BT_ERR("%s urb %p submission failed (%d)",
753 hdev->name, urb, -err);
754 usb_unanchor_urb(urb);
762 static void btusb_bulk_complete(struct urb *urb)
764 struct hci_dev *hdev = urb->context;
765 struct btusb_data *data = hci_get_drvdata(hdev);
768 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
771 if (!test_bit(HCI_RUNNING, &hdev->flags))
774 if (urb->status == 0) {
775 hdev->stat.byte_rx += urb->actual_length;
777 if (data->recv_bulk(data, urb->transfer_buffer,
778 urb->actual_length) < 0) {
779 BT_ERR("%s corrupted ACL packet", hdev->name);
782 } else if (urb->status == -ENOENT) {
783 /* Avoid suspend failed when usb_kill_urb */
787 if (!test_bit(BTUSB_BULK_RUNNING, &data->flags))
790 usb_anchor_urb(urb, &data->bulk_anchor);
791 usb_mark_last_busy(data->udev);
793 err = usb_submit_urb(urb, GFP_ATOMIC);
795 /* -EPERM: urb is being killed;
796 * -ENODEV: device got disconnected
798 if (err != -EPERM && err != -ENODEV)
799 BT_ERR("%s urb %p failed to resubmit (%d)",
800 hdev->name, urb, -err);
801 usb_unanchor_urb(urb);
805 static int btusb_submit_bulk_urb(struct hci_dev *hdev, gfp_t mem_flags)
807 struct btusb_data *data = hci_get_drvdata(hdev);
811 int err, size = HCI_MAX_FRAME_SIZE;
813 BT_DBG("%s", hdev->name);
815 if (!data->bulk_rx_ep)
818 urb = usb_alloc_urb(0, mem_flags);
822 buf = kmalloc(size, mem_flags);
828 pipe = usb_rcvbulkpipe(data->udev, data->bulk_rx_ep->bEndpointAddress);
830 usb_fill_bulk_urb(urb, data->udev, pipe, buf, size,
831 btusb_bulk_complete, hdev);
833 urb->transfer_flags |= URB_FREE_BUFFER;
835 usb_mark_last_busy(data->udev);
836 usb_anchor_urb(urb, &data->bulk_anchor);
838 err = usb_submit_urb(urb, mem_flags);
840 if (err != -EPERM && err != -ENODEV)
841 BT_ERR("%s urb %p submission failed (%d)",
842 hdev->name, urb, -err);
843 usb_unanchor_urb(urb);
851 static void btusb_isoc_complete(struct urb *urb)
853 struct hci_dev *hdev = urb->context;
854 struct btusb_data *data = hci_get_drvdata(hdev);
857 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
860 if (!test_bit(HCI_RUNNING, &hdev->flags))
863 if (urb->status == 0) {
864 for (i = 0; i < urb->number_of_packets; i++) {
865 unsigned int offset = urb->iso_frame_desc[i].offset;
866 unsigned int length = urb->iso_frame_desc[i].actual_length;
868 if (urb->iso_frame_desc[i].status)
871 hdev->stat.byte_rx += length;
873 if (btusb_recv_isoc(data, urb->transfer_buffer + offset,
875 BT_ERR("%s corrupted SCO packet", hdev->name);
879 } else if (urb->status == -ENOENT) {
880 /* Avoid suspend failed when usb_kill_urb */
884 if (!test_bit(BTUSB_ISOC_RUNNING, &data->flags))
887 usb_anchor_urb(urb, &data->isoc_anchor);
889 err = usb_submit_urb(urb, GFP_ATOMIC);
891 /* -EPERM: urb is being killed;
892 * -ENODEV: device got disconnected
894 if (err != -EPERM && err != -ENODEV)
895 BT_ERR("%s urb %p failed to resubmit (%d)",
896 hdev->name, urb, -err);
897 usb_unanchor_urb(urb);
901 static inline void __fill_isoc_descriptor(struct urb *urb, int len, int mtu)
905 BT_DBG("len %d mtu %d", len, mtu);
907 for (i = 0; i < BTUSB_MAX_ISOC_FRAMES && len >= mtu;
908 i++, offset += mtu, len -= mtu) {
909 urb->iso_frame_desc[i].offset = offset;
910 urb->iso_frame_desc[i].length = mtu;
913 if (len && i < BTUSB_MAX_ISOC_FRAMES) {
914 urb->iso_frame_desc[i].offset = offset;
915 urb->iso_frame_desc[i].length = len;
919 urb->number_of_packets = i;
922 static int btusb_submit_isoc_urb(struct hci_dev *hdev, gfp_t mem_flags)
924 struct btusb_data *data = hci_get_drvdata(hdev);
930 BT_DBG("%s", hdev->name);
932 if (!data->isoc_rx_ep)
935 urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, mem_flags);
939 size = le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize) *
940 BTUSB_MAX_ISOC_FRAMES;
942 buf = kmalloc(size, mem_flags);
948 pipe = usb_rcvisocpipe(data->udev, data->isoc_rx_ep->bEndpointAddress);
950 usb_fill_int_urb(urb, data->udev, pipe, buf, size, btusb_isoc_complete,
951 hdev, data->isoc_rx_ep->bInterval);
953 urb->transfer_flags = URB_FREE_BUFFER | URB_ISO_ASAP;
955 __fill_isoc_descriptor(urb, size,
956 le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize));
958 usb_anchor_urb(urb, &data->isoc_anchor);
960 err = usb_submit_urb(urb, mem_flags);
962 if (err != -EPERM && err != -ENODEV)
963 BT_ERR("%s urb %p submission failed (%d)",
964 hdev->name, urb, -err);
965 usb_unanchor_urb(urb);
973 static void btusb_diag_complete(struct urb *urb)
975 struct hci_dev *hdev = urb->context;
976 struct btusb_data *data = hci_get_drvdata(hdev);
979 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
982 if (urb->status == 0) {
985 skb = bt_skb_alloc(urb->actual_length, GFP_ATOMIC);
987 skb_put_data(skb, urb->transfer_buffer,
989 hci_recv_diag(hdev, skb);
991 } else if (urb->status == -ENOENT) {
992 /* Avoid suspend failed when usb_kill_urb */
996 if (!test_bit(BTUSB_DIAG_RUNNING, &data->flags))
999 usb_anchor_urb(urb, &data->diag_anchor);
1000 usb_mark_last_busy(data->udev);
1002 err = usb_submit_urb(urb, GFP_ATOMIC);
1004 /* -EPERM: urb is being killed;
1005 * -ENODEV: device got disconnected
1007 if (err != -EPERM && err != -ENODEV)
1008 BT_ERR("%s urb %p failed to resubmit (%d)",
1009 hdev->name, urb, -err);
1010 usb_unanchor_urb(urb);
1014 static int btusb_submit_diag_urb(struct hci_dev *hdev, gfp_t mem_flags)
1016 struct btusb_data *data = hci_get_drvdata(hdev);
1020 int err, size = HCI_MAX_FRAME_SIZE;
1022 BT_DBG("%s", hdev->name);
1024 if (!data->diag_rx_ep)
1027 urb = usb_alloc_urb(0, mem_flags);
1031 buf = kmalloc(size, mem_flags);
1037 pipe = usb_rcvbulkpipe(data->udev, data->diag_rx_ep->bEndpointAddress);
1039 usb_fill_bulk_urb(urb, data->udev, pipe, buf, size,
1040 btusb_diag_complete, hdev);
1042 urb->transfer_flags |= URB_FREE_BUFFER;
1044 usb_mark_last_busy(data->udev);
1045 usb_anchor_urb(urb, &data->diag_anchor);
1047 err = usb_submit_urb(urb, mem_flags);
1049 if (err != -EPERM && err != -ENODEV)
1050 BT_ERR("%s urb %p submission failed (%d)",
1051 hdev->name, urb, -err);
1052 usb_unanchor_urb(urb);
1060 static void btusb_tx_complete(struct urb *urb)
1062 struct sk_buff *skb = urb->context;
1063 struct hci_dev *hdev = (struct hci_dev *)skb->dev;
1064 struct btusb_data *data = hci_get_drvdata(hdev);
1066 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
1067 urb->actual_length);
1069 if (!test_bit(HCI_RUNNING, &hdev->flags))
1073 hdev->stat.byte_tx += urb->transfer_buffer_length;
1075 hdev->stat.err_tx++;
1078 spin_lock(&data->txlock);
1079 data->tx_in_flight--;
1080 spin_unlock(&data->txlock);
1082 kfree(urb->setup_packet);
1087 static void btusb_isoc_tx_complete(struct urb *urb)
1089 struct sk_buff *skb = urb->context;
1090 struct hci_dev *hdev = (struct hci_dev *)skb->dev;
1092 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
1093 urb->actual_length);
1095 if (!test_bit(HCI_RUNNING, &hdev->flags))
1099 hdev->stat.byte_tx += urb->transfer_buffer_length;
1101 hdev->stat.err_tx++;
1104 kfree(urb->setup_packet);
1109 static int btusb_open(struct hci_dev *hdev)
1111 struct btusb_data *data = hci_get_drvdata(hdev);
1114 BT_DBG("%s", hdev->name);
1116 err = usb_autopm_get_interface(data->intf);
1120 /* Patching USB firmware files prior to starting any URBs of HCI path
1121 * It is more safe to use USB bulk channel for downloading USB patch
1123 if (data->setup_on_usb) {
1124 err = data->setup_on_usb(hdev);
1129 data->intf->needs_remote_wakeup = 1;
1131 if (test_and_set_bit(BTUSB_INTR_RUNNING, &data->flags))
1134 err = btusb_submit_intr_urb(hdev, GFP_KERNEL);
1138 err = btusb_submit_bulk_urb(hdev, GFP_KERNEL);
1140 usb_kill_anchored_urbs(&data->intr_anchor);
1144 set_bit(BTUSB_BULK_RUNNING, &data->flags);
1145 btusb_submit_bulk_urb(hdev, GFP_KERNEL);
1148 if (!btusb_submit_diag_urb(hdev, GFP_KERNEL))
1149 set_bit(BTUSB_DIAG_RUNNING, &data->flags);
1153 usb_autopm_put_interface(data->intf);
1157 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
1159 usb_autopm_put_interface(data->intf);
1163 static void btusb_stop_traffic(struct btusb_data *data)
1165 usb_kill_anchored_urbs(&data->intr_anchor);
1166 usb_kill_anchored_urbs(&data->bulk_anchor);
1167 usb_kill_anchored_urbs(&data->isoc_anchor);
1168 usb_kill_anchored_urbs(&data->diag_anchor);
1171 static int btusb_close(struct hci_dev *hdev)
1173 struct btusb_data *data = hci_get_drvdata(hdev);
1176 BT_DBG("%s", hdev->name);
1178 cancel_work_sync(&data->work);
1179 cancel_work_sync(&data->waker);
1181 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1182 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
1183 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
1184 clear_bit(BTUSB_DIAG_RUNNING, &data->flags);
1186 btusb_stop_traffic(data);
1187 btusb_free_frags(data);
1189 err = usb_autopm_get_interface(data->intf);
1193 data->intf->needs_remote_wakeup = 0;
1194 usb_autopm_put_interface(data->intf);
1197 usb_scuttle_anchored_urbs(&data->deferred);
1201 static int btusb_flush(struct hci_dev *hdev)
1203 struct btusb_data *data = hci_get_drvdata(hdev);
1205 BT_DBG("%s", hdev->name);
1207 usb_kill_anchored_urbs(&data->tx_anchor);
1208 btusb_free_frags(data);
1213 static struct urb *alloc_ctrl_urb(struct hci_dev *hdev, struct sk_buff *skb)
1215 struct btusb_data *data = hci_get_drvdata(hdev);
1216 struct usb_ctrlrequest *dr;
1220 urb = usb_alloc_urb(0, GFP_KERNEL);
1222 return ERR_PTR(-ENOMEM);
1224 dr = kmalloc(sizeof(*dr), GFP_KERNEL);
1227 return ERR_PTR(-ENOMEM);
1230 dr->bRequestType = data->cmdreq_type;
1231 dr->bRequest = data->cmdreq;
1234 dr->wLength = __cpu_to_le16(skb->len);
1236 pipe = usb_sndctrlpipe(data->udev, 0x00);
1238 usb_fill_control_urb(urb, data->udev, pipe, (void *)dr,
1239 skb->data, skb->len, btusb_tx_complete, skb);
1241 skb->dev = (void *)hdev;
1246 static struct urb *alloc_bulk_urb(struct hci_dev *hdev, struct sk_buff *skb)
1248 struct btusb_data *data = hci_get_drvdata(hdev);
1252 if (!data->bulk_tx_ep)
1253 return ERR_PTR(-ENODEV);
1255 urb = usb_alloc_urb(0, GFP_KERNEL);
1257 return ERR_PTR(-ENOMEM);
1259 pipe = usb_sndbulkpipe(data->udev, data->bulk_tx_ep->bEndpointAddress);
1261 usb_fill_bulk_urb(urb, data->udev, pipe,
1262 skb->data, skb->len, btusb_tx_complete, skb);
1264 skb->dev = (void *)hdev;
1269 static struct urb *alloc_isoc_urb(struct hci_dev *hdev, struct sk_buff *skb)
1271 struct btusb_data *data = hci_get_drvdata(hdev);
1275 if (!data->isoc_tx_ep)
1276 return ERR_PTR(-ENODEV);
1278 urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, GFP_KERNEL);
1280 return ERR_PTR(-ENOMEM);
1282 pipe = usb_sndisocpipe(data->udev, data->isoc_tx_ep->bEndpointAddress);
1284 usb_fill_int_urb(urb, data->udev, pipe,
1285 skb->data, skb->len, btusb_isoc_tx_complete,
1286 skb, data->isoc_tx_ep->bInterval);
1288 urb->transfer_flags = URB_ISO_ASAP;
1290 __fill_isoc_descriptor(urb, skb->len,
1291 le16_to_cpu(data->isoc_tx_ep->wMaxPacketSize));
1293 skb->dev = (void *)hdev;
1298 static int submit_tx_urb(struct hci_dev *hdev, struct urb *urb)
1300 struct btusb_data *data = hci_get_drvdata(hdev);
1303 usb_anchor_urb(urb, &data->tx_anchor);
1305 err = usb_submit_urb(urb, GFP_KERNEL);
1307 if (err != -EPERM && err != -ENODEV)
1308 BT_ERR("%s urb %p submission failed (%d)",
1309 hdev->name, urb, -err);
1310 kfree(urb->setup_packet);
1311 usb_unanchor_urb(urb);
1313 usb_mark_last_busy(data->udev);
1320 static int submit_or_queue_tx_urb(struct hci_dev *hdev, struct urb *urb)
1322 struct btusb_data *data = hci_get_drvdata(hdev);
1323 unsigned long flags;
1326 spin_lock_irqsave(&data->txlock, flags);
1327 suspending = test_bit(BTUSB_SUSPENDING, &data->flags);
1329 data->tx_in_flight++;
1330 spin_unlock_irqrestore(&data->txlock, flags);
1333 return submit_tx_urb(hdev, urb);
1335 usb_anchor_urb(urb, &data->deferred);
1336 schedule_work(&data->waker);
1342 static int btusb_send_frame(struct hci_dev *hdev, struct sk_buff *skb)
1346 BT_DBG("%s", hdev->name);
1348 switch (hci_skb_pkt_type(skb)) {
1349 case HCI_COMMAND_PKT:
1350 urb = alloc_ctrl_urb(hdev, skb);
1352 return PTR_ERR(urb);
1354 hdev->stat.cmd_tx++;
1355 return submit_or_queue_tx_urb(hdev, urb);
1357 case HCI_ACLDATA_PKT:
1358 urb = alloc_bulk_urb(hdev, skb);
1360 return PTR_ERR(urb);
1362 hdev->stat.acl_tx++;
1363 return submit_or_queue_tx_urb(hdev, urb);
1365 case HCI_SCODATA_PKT:
1366 if (hci_conn_num(hdev, SCO_LINK) < 1)
1369 urb = alloc_isoc_urb(hdev, skb);
1371 return PTR_ERR(urb);
1373 hdev->stat.sco_tx++;
1374 return submit_tx_urb(hdev, urb);
1380 static void btusb_notify(struct hci_dev *hdev, unsigned int evt)
1382 struct btusb_data *data = hci_get_drvdata(hdev);
1384 BT_DBG("%s evt %d", hdev->name, evt);
1386 if (hci_conn_num(hdev, SCO_LINK) != data->sco_num) {
1387 data->sco_num = hci_conn_num(hdev, SCO_LINK);
1388 schedule_work(&data->work);
1392 static inline int __set_isoc_interface(struct hci_dev *hdev, int altsetting)
1394 struct btusb_data *data = hci_get_drvdata(hdev);
1395 struct usb_interface *intf = data->isoc;
1396 struct usb_endpoint_descriptor *ep_desc;
1402 err = usb_set_interface(data->udev, 1, altsetting);
1404 BT_ERR("%s setting interface failed (%d)", hdev->name, -err);
1408 data->isoc_altsetting = altsetting;
1410 data->isoc_tx_ep = NULL;
1411 data->isoc_rx_ep = NULL;
1413 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
1414 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
1416 if (!data->isoc_tx_ep && usb_endpoint_is_isoc_out(ep_desc)) {
1417 data->isoc_tx_ep = ep_desc;
1421 if (!data->isoc_rx_ep && usb_endpoint_is_isoc_in(ep_desc)) {
1422 data->isoc_rx_ep = ep_desc;
1427 if (!data->isoc_tx_ep || !data->isoc_rx_ep) {
1428 BT_ERR("%s invalid SCO descriptors", hdev->name);
1435 static void btusb_work(struct work_struct *work)
1437 struct btusb_data *data = container_of(work, struct btusb_data, work);
1438 struct hci_dev *hdev = data->hdev;
1442 if (data->sco_num > 0) {
1443 if (!test_bit(BTUSB_DID_ISO_RESUME, &data->flags)) {
1444 err = usb_autopm_get_interface(data->isoc ? data->isoc : data->intf);
1446 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1447 usb_kill_anchored_urbs(&data->isoc_anchor);
1451 set_bit(BTUSB_DID_ISO_RESUME, &data->flags);
1454 if (hdev->voice_setting & 0x0020) {
1455 static const int alts[3] = { 2, 4, 5 };
1457 new_alts = alts[data->sco_num - 1];
1459 new_alts = data->sco_num;
1462 if (data->isoc_altsetting != new_alts) {
1463 unsigned long flags;
1465 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1466 usb_kill_anchored_urbs(&data->isoc_anchor);
1468 /* When isochronous alternate setting needs to be
1469 * changed, because SCO connection has been added
1470 * or removed, a packet fragment may be left in the
1471 * reassembling state. This could lead to wrongly
1472 * assembled fragments.
1474 * Clear outstanding fragment when selecting a new
1475 * alternate setting.
1477 spin_lock_irqsave(&data->rxlock, flags);
1478 kfree_skb(data->sco_skb);
1479 data->sco_skb = NULL;
1480 spin_unlock_irqrestore(&data->rxlock, flags);
1482 if (__set_isoc_interface(hdev, new_alts) < 0)
1486 if (!test_and_set_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
1487 if (btusb_submit_isoc_urb(hdev, GFP_KERNEL) < 0)
1488 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1490 btusb_submit_isoc_urb(hdev, GFP_KERNEL);
1493 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1494 usb_kill_anchored_urbs(&data->isoc_anchor);
1496 __set_isoc_interface(hdev, 0);
1497 if (test_and_clear_bit(BTUSB_DID_ISO_RESUME, &data->flags))
1498 usb_autopm_put_interface(data->isoc ? data->isoc : data->intf);
1502 static void btusb_waker(struct work_struct *work)
1504 struct btusb_data *data = container_of(work, struct btusb_data, waker);
1507 err = usb_autopm_get_interface(data->intf);
1511 usb_autopm_put_interface(data->intf);
1514 static int btusb_setup_bcm92035(struct hci_dev *hdev)
1516 struct sk_buff *skb;
1519 BT_DBG("%s", hdev->name);
1521 skb = __hci_cmd_sync(hdev, 0xfc3b, 1, &val, HCI_INIT_TIMEOUT);
1523 BT_ERR("BCM92035 command failed (%ld)", -PTR_ERR(skb));
1530 static int btusb_setup_csr(struct hci_dev *hdev)
1532 struct hci_rp_read_local_version *rp;
1533 struct sk_buff *skb;
1535 BT_DBG("%s", hdev->name);
1537 skb = __hci_cmd_sync(hdev, HCI_OP_READ_LOCAL_VERSION, 0, NULL,
1540 int err = PTR_ERR(skb);
1541 BT_ERR("%s: CSR: Local version failed (%d)", hdev->name, err);
1545 if (skb->len != sizeof(struct hci_rp_read_local_version)) {
1546 BT_ERR("%s: CSR: Local version length mismatch", hdev->name);
1551 rp = (struct hci_rp_read_local_version *)skb->data;
1553 /* Detect controllers which aren't real CSR ones. */
1554 if (le16_to_cpu(rp->manufacturer) != 10 ||
1555 le16_to_cpu(rp->lmp_subver) == 0x0c5c) {
1556 /* Clear the reset quirk since this is not an actual
1557 * early Bluetooth 1.1 device from CSR.
1559 clear_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
1561 /* These fake CSR controllers have all a broken
1562 * stored link key handling and so just disable it.
1564 set_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks);
1572 static const struct firmware *btusb_setup_intel_get_fw(struct hci_dev *hdev,
1573 struct intel_version *ver)
1575 const struct firmware *fw;
1579 snprintf(fwname, sizeof(fwname),
1581 ver->hw_platform, ver->hw_variant, ver->hw_revision,
1582 ver->fw_variant, ver->fw_revision, ver->fw_build_num,
1583 ver->fw_build_ww, ver->fw_build_yy);
1585 ret = reject_firmware(&fw, fwname, &hdev->dev);
1587 if (ret == -EINVAL) {
1588 BT_ERR("%s Intel firmware file request failed (%d)",
1593 BT_ERR("%s failed to open Intel firmware file: %s(%d)",
1594 hdev->name, fwname, ret);
1596 /* If the correct firmware patch file is not found, use the
1597 * default firmware patch file instead
1599 snprintf(fwname, sizeof(fwname), "/*(DEBLOBBED)*/",
1600 ver->hw_platform, ver->hw_variant);
1601 if (reject_firmware(&fw, fwname, &hdev->dev) < 0) {
1602 BT_ERR("%s failed to open default Intel fw file: %s",
1603 hdev->name, fwname);
1608 BT_INFO("%s: Intel Bluetooth firmware file: %s", hdev->name, fwname);
1613 static int btusb_setup_intel_patching(struct hci_dev *hdev,
1614 const struct firmware *fw,
1615 const u8 **fw_ptr, int *disable_patch)
1617 struct sk_buff *skb;
1618 struct hci_command_hdr *cmd;
1619 const u8 *cmd_param;
1620 struct hci_event_hdr *evt = NULL;
1621 const u8 *evt_param = NULL;
1622 int remain = fw->size - (*fw_ptr - fw->data);
1624 /* The first byte indicates the types of the patch command or event.
1625 * 0x01 means HCI command and 0x02 is HCI event. If the first bytes
1626 * in the current firmware buffer doesn't start with 0x01 or
1627 * the size of remain buffer is smaller than HCI command header,
1628 * the firmware file is corrupted and it should stop the patching
1631 if (remain > HCI_COMMAND_HDR_SIZE && *fw_ptr[0] != 0x01) {
1632 BT_ERR("%s Intel fw corrupted: invalid cmd read", hdev->name);
1638 cmd = (struct hci_command_hdr *)(*fw_ptr);
1639 *fw_ptr += sizeof(*cmd);
1640 remain -= sizeof(*cmd);
1642 /* Ensure that the remain firmware data is long enough than the length
1643 * of command parameter. If not, the firmware file is corrupted.
1645 if (remain < cmd->plen) {
1646 BT_ERR("%s Intel fw corrupted: invalid cmd len", hdev->name);
1650 /* If there is a command that loads a patch in the firmware
1651 * file, then enable the patch upon success, otherwise just
1652 * disable the manufacturer mode, for example patch activation
1653 * is not required when the default firmware patch file is used
1654 * because there are no patch data to load.
1656 if (*disable_patch && le16_to_cpu(cmd->opcode) == 0xfc8e)
1659 cmd_param = *fw_ptr;
1660 *fw_ptr += cmd->plen;
1661 remain -= cmd->plen;
1663 /* This reads the expected events when the above command is sent to the
1664 * device. Some vendor commands expects more than one events, for
1665 * example command status event followed by vendor specific event.
1666 * For this case, it only keeps the last expected event. so the command
1667 * can be sent with __hci_cmd_sync_ev() which returns the sk_buff of
1668 * last expected event.
1670 while (remain > HCI_EVENT_HDR_SIZE && *fw_ptr[0] == 0x02) {
1674 evt = (struct hci_event_hdr *)(*fw_ptr);
1675 *fw_ptr += sizeof(*evt);
1676 remain -= sizeof(*evt);
1678 if (remain < evt->plen) {
1679 BT_ERR("%s Intel fw corrupted: invalid evt len",
1684 evt_param = *fw_ptr;
1685 *fw_ptr += evt->plen;
1686 remain -= evt->plen;
1689 /* Every HCI commands in the firmware file has its correspond event.
1690 * If event is not found or remain is smaller than zero, the firmware
1691 * file is corrupted.
1693 if (!evt || !evt_param || remain < 0) {
1694 BT_ERR("%s Intel fw corrupted: invalid evt read", hdev->name);
1698 skb = __hci_cmd_sync_ev(hdev, le16_to_cpu(cmd->opcode), cmd->plen,
1699 cmd_param, evt->evt, HCI_INIT_TIMEOUT);
1701 BT_ERR("%s sending Intel patch command (0x%4.4x) failed (%ld)",
1702 hdev->name, cmd->opcode, PTR_ERR(skb));
1703 return PTR_ERR(skb);
1706 /* It ensures that the returned event matches the event data read from
1707 * the firmware file. At fist, it checks the length and then
1708 * the contents of the event.
1710 if (skb->len != evt->plen) {
1711 BT_ERR("%s mismatch event length (opcode 0x%4.4x)", hdev->name,
1712 le16_to_cpu(cmd->opcode));
1717 if (memcmp(skb->data, evt_param, evt->plen)) {
1718 BT_ERR("%s mismatch event parameter (opcode 0x%4.4x)",
1719 hdev->name, le16_to_cpu(cmd->opcode));
1728 static int btusb_setup_intel(struct hci_dev *hdev)
1730 struct sk_buff *skb;
1731 const struct firmware *fw;
1733 int disable_patch, err;
1734 struct intel_version ver;
1736 BT_DBG("%s", hdev->name);
1738 /* The controller has a bug with the first HCI command sent to it
1739 * returning number of completed commands as zero. This would stall the
1740 * command processing in the Bluetooth core.
1742 * As a workaround, send HCI Reset command first which will reset the
1743 * number of completed commands and allow normal command processing
1746 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
1748 BT_ERR("%s sending initial HCI reset command failed (%ld)",
1749 hdev->name, PTR_ERR(skb));
1750 return PTR_ERR(skb);
1754 /* Read Intel specific controller version first to allow selection of
1755 * which firmware file to load.
1757 * The returned information are hardware variant and revision plus
1758 * firmware variant, revision and build number.
1760 err = btintel_read_version(hdev, &ver);
1764 BT_INFO("%s: read Intel version: %02x%02x%02x%02x%02x%02x%02x%02x%02x",
1765 hdev->name, ver.hw_platform, ver.hw_variant, ver.hw_revision,
1766 ver.fw_variant, ver.fw_revision, ver.fw_build_num,
1767 ver.fw_build_ww, ver.fw_build_yy, ver.fw_patch_num);
1769 /* fw_patch_num indicates the version of patch the device currently
1770 * have. If there is no patch data in the device, it is always 0x00.
1771 * So, if it is other than 0x00, no need to patch the device again.
1773 if (ver.fw_patch_num) {
1774 BT_INFO("%s: Intel device is already patched. patch num: %02x",
1775 hdev->name, ver.fw_patch_num);
1779 /* Opens the firmware patch file based on the firmware version read
1780 * from the controller. If it fails to open the matching firmware
1781 * patch file, it tries to open the default firmware patch file.
1782 * If no patch file is found, allow the device to operate without
1785 fw = btusb_setup_intel_get_fw(hdev, &ver);
1790 /* Enable the manufacturer mode of the controller.
1791 * Only while this mode is enabled, the driver can download the
1792 * firmware patch data and configuration parameters.
1794 err = btintel_enter_mfg(hdev);
1796 release_firmware(fw);
1802 /* The firmware data file consists of list of Intel specific HCI
1803 * commands and its expected events. The first byte indicates the
1804 * type of the message, either HCI command or HCI event.
1806 * It reads the command and its expected event from the firmware file,
1807 * and send to the controller. Once __hci_cmd_sync_ev() returns,
1808 * the returned event is compared with the event read from the firmware
1809 * file and it will continue until all the messages are downloaded to
1812 * Once the firmware patching is completed successfully,
1813 * the manufacturer mode is disabled with reset and activating the
1816 * If the firmware patching fails, the manufacturer mode is
1817 * disabled with reset and deactivating the patch.
1819 * If the default patch file is used, no reset is done when disabling
1822 while (fw->size > fw_ptr - fw->data) {
1825 ret = btusb_setup_intel_patching(hdev, fw, &fw_ptr,
1828 goto exit_mfg_deactivate;
1831 release_firmware(fw);
1834 goto exit_mfg_disable;
1836 /* Patching completed successfully and disable the manufacturer mode
1837 * with reset and activate the downloaded firmware patches.
1839 err = btintel_exit_mfg(hdev, true, true);
1843 BT_INFO("%s: Intel Bluetooth firmware patch completed and activated",
1849 /* Disable the manufacturer mode without reset */
1850 err = btintel_exit_mfg(hdev, false, false);
1854 BT_INFO("%s: Intel Bluetooth firmware patch completed", hdev->name);
1858 exit_mfg_deactivate:
1859 release_firmware(fw);
1861 /* Patching failed. Disable the manufacturer mode with reset and
1862 * deactivate the downloaded firmware patches.
1864 err = btintel_exit_mfg(hdev, true, false);
1868 BT_INFO("%s: Intel Bluetooth firmware patch completed and deactivated",
1872 /* Set the event mask for Intel specific vendor events. This enables
1873 * a few extra events that are useful during general operation.
1875 btintel_set_event_mask_mfg(hdev, false);
1877 btintel_check_bdaddr(hdev);
1881 static int inject_cmd_complete(struct hci_dev *hdev, __u16 opcode)
1883 struct sk_buff *skb;
1884 struct hci_event_hdr *hdr;
1885 struct hci_ev_cmd_complete *evt;
1887 skb = bt_skb_alloc(sizeof(*hdr) + sizeof(*evt) + 1, GFP_ATOMIC);
1891 hdr = skb_put(skb, sizeof(*hdr));
1892 hdr->evt = HCI_EV_CMD_COMPLETE;
1893 hdr->plen = sizeof(*evt) + 1;
1895 evt = skb_put(skb, sizeof(*evt));
1897 evt->opcode = cpu_to_le16(opcode);
1899 skb_put_u8(skb, 0x00);
1901 hci_skb_pkt_type(skb) = HCI_EVENT_PKT;
1903 return hci_recv_frame(hdev, skb);
1906 static int btusb_recv_bulk_intel(struct btusb_data *data, void *buffer,
1909 /* When the device is in bootloader mode, then it can send
1910 * events via the bulk endpoint. These events are treated the
1911 * same way as the ones received from the interrupt endpoint.
1913 if (test_bit(BTUSB_BOOTLOADER, &data->flags))
1914 return btusb_recv_intr(data, buffer, count);
1916 return btusb_recv_bulk(data, buffer, count);
1919 static void btusb_intel_bootup(struct btusb_data *data, const void *ptr,
1922 const struct intel_bootup *evt = ptr;
1924 if (len != sizeof(*evt))
1927 if (test_and_clear_bit(BTUSB_BOOTING, &data->flags)) {
1928 smp_mb__after_atomic();
1929 wake_up_bit(&data->flags, BTUSB_BOOTING);
1933 static void btusb_intel_secure_send_result(struct btusb_data *data,
1934 const void *ptr, unsigned int len)
1936 const struct intel_secure_send_result *evt = ptr;
1938 if (len != sizeof(*evt))
1942 set_bit(BTUSB_FIRMWARE_FAILED, &data->flags);
1944 if (test_and_clear_bit(BTUSB_DOWNLOADING, &data->flags) &&
1945 test_bit(BTUSB_FIRMWARE_LOADED, &data->flags)) {
1946 smp_mb__after_atomic();
1947 wake_up_bit(&data->flags, BTUSB_DOWNLOADING);
1951 static int btusb_recv_event_intel(struct hci_dev *hdev, struct sk_buff *skb)
1953 struct btusb_data *data = hci_get_drvdata(hdev);
1955 if (test_bit(BTUSB_BOOTLOADER, &data->flags)) {
1956 struct hci_event_hdr *hdr = (void *)skb->data;
1958 if (skb->len > HCI_EVENT_HDR_SIZE && hdr->evt == 0xff &&
1960 const void *ptr = skb->data + HCI_EVENT_HDR_SIZE + 1;
1961 unsigned int len = skb->len - HCI_EVENT_HDR_SIZE - 1;
1963 switch (skb->data[2]) {
1965 /* When switching to the operational firmware
1966 * the device sends a vendor specific event
1967 * indicating that the bootup completed.
1969 btusb_intel_bootup(data, ptr, len);
1972 /* When the firmware loading completes the
1973 * device sends out a vendor specific event
1974 * indicating the result of the firmware
1977 btusb_intel_secure_send_result(data, ptr, len);
1983 return hci_recv_frame(hdev, skb);
1986 static int btusb_send_frame_intel(struct hci_dev *hdev, struct sk_buff *skb)
1988 struct btusb_data *data = hci_get_drvdata(hdev);
1991 BT_DBG("%s", hdev->name);
1993 switch (hci_skb_pkt_type(skb)) {
1994 case HCI_COMMAND_PKT:
1995 if (test_bit(BTUSB_BOOTLOADER, &data->flags)) {
1996 struct hci_command_hdr *cmd = (void *)skb->data;
1997 __u16 opcode = le16_to_cpu(cmd->opcode);
1999 /* When in bootloader mode and the command 0xfc09
2000 * is received, it needs to be send down the
2001 * bulk endpoint. So allocate a bulk URB instead.
2003 if (opcode == 0xfc09)
2004 urb = alloc_bulk_urb(hdev, skb);
2006 urb = alloc_ctrl_urb(hdev, skb);
2008 /* When the 0xfc01 command is issued to boot into
2009 * the operational firmware, it will actually not
2010 * send a command complete event. To keep the flow
2011 * control working inject that event here.
2013 if (opcode == 0xfc01)
2014 inject_cmd_complete(hdev, opcode);
2016 urb = alloc_ctrl_urb(hdev, skb);
2019 return PTR_ERR(urb);
2021 hdev->stat.cmd_tx++;
2022 return submit_or_queue_tx_urb(hdev, urb);
2024 case HCI_ACLDATA_PKT:
2025 urb = alloc_bulk_urb(hdev, skb);
2027 return PTR_ERR(urb);
2029 hdev->stat.acl_tx++;
2030 return submit_or_queue_tx_urb(hdev, urb);
2032 case HCI_SCODATA_PKT:
2033 if (hci_conn_num(hdev, SCO_LINK) < 1)
2036 urb = alloc_isoc_urb(hdev, skb);
2038 return PTR_ERR(urb);
2040 hdev->stat.sco_tx++;
2041 return submit_tx_urb(hdev, urb);
2047 static int btusb_setup_intel_new(struct hci_dev *hdev)
2049 static const u8 reset_param[] = { 0x00, 0x01, 0x00, 0x01,
2050 0x00, 0x08, 0x04, 0x00 };
2051 struct btusb_data *data = hci_get_drvdata(hdev);
2052 struct sk_buff *skb;
2053 struct intel_version ver;
2054 struct intel_boot_params *params;
2055 const struct firmware *fw;
2059 ktime_t calltime, delta, rettime;
2060 unsigned long long duration;
2063 BT_DBG("%s", hdev->name);
2065 calltime = ktime_get();
2067 /* Read the Intel version information to determine if the device
2068 * is in bootloader mode or if it already has operational firmware
2071 err = btintel_read_version(hdev, &ver);
2075 /* The hardware platform number has a fixed value of 0x37 and
2076 * for now only accept this single value.
2078 if (ver.hw_platform != 0x37) {
2079 BT_ERR("%s: Unsupported Intel hardware platform (%u)",
2080 hdev->name, ver.hw_platform);
2084 /* Check for supported iBT hardware variants of this firmware
2087 * This check has been put in place to ensure correct forward
2088 * compatibility options when newer hardware variants come along.
2090 switch (ver.hw_variant) {
2091 case 0x0b: /* SfP */
2092 case 0x0c: /* WsP */
2093 case 0x11: /* JfP */
2094 case 0x12: /* ThP */
2097 BT_ERR("%s: Unsupported Intel hardware variant (%u)",
2098 hdev->name, ver.hw_variant);
2102 btintel_version_info(hdev, &ver);
2104 /* The firmware variant determines if the device is in bootloader
2105 * mode or is running operational firmware. The value 0x06 identifies
2106 * the bootloader and the value 0x23 identifies the operational
2109 * When the operational firmware is already present, then only
2110 * the check for valid Bluetooth device address is needed. This
2111 * determines if the device will be added as configured or
2112 * unconfigured controller.
2114 * It is not possible to use the Secure Boot Parameters in this
2115 * case since that command is only available in bootloader mode.
2117 if (ver.fw_variant == 0x23) {
2118 clear_bit(BTUSB_BOOTLOADER, &data->flags);
2119 btintel_check_bdaddr(hdev);
2123 /* If the device is not in bootloader mode, then the only possible
2124 * choice is to return an error and abort the device initialization.
2126 if (ver.fw_variant != 0x06) {
2127 BT_ERR("%s: Unsupported Intel firmware variant (%u)",
2128 hdev->name, ver.fw_variant);
2132 /* Read the secure boot parameters to identify the operating
2133 * details of the bootloader.
2135 skb = __hci_cmd_sync(hdev, 0xfc0d, 0, NULL, HCI_INIT_TIMEOUT);
2137 BT_ERR("%s: Reading Intel boot parameters failed (%ld)",
2138 hdev->name, PTR_ERR(skb));
2139 return PTR_ERR(skb);
2142 if (skb->len != sizeof(*params)) {
2143 BT_ERR("%s: Intel boot parameters size mismatch", hdev->name);
2148 params = (struct intel_boot_params *)skb->data;
2150 BT_INFO("%s: Device revision is %u", hdev->name,
2151 le16_to_cpu(params->dev_revid));
2153 BT_INFO("%s: Secure boot is %s", hdev->name,
2154 params->secure_boot ? "enabled" : "disabled");
2156 BT_INFO("%s: OTP lock is %s", hdev->name,
2157 params->otp_lock ? "enabled" : "disabled");
2159 BT_INFO("%s: API lock is %s", hdev->name,
2160 params->api_lock ? "enabled" : "disabled");
2162 BT_INFO("%s: Debug lock is %s", hdev->name,
2163 params->debug_lock ? "enabled" : "disabled");
2165 BT_INFO("%s: Minimum firmware build %u week %u %u", hdev->name,
2166 params->min_fw_build_nn, params->min_fw_build_cw,
2167 2000 + params->min_fw_build_yy);
2169 /* It is required that every single firmware fragment is acknowledged
2170 * with a command complete event. If the boot parameters indicate
2171 * that this bootloader does not send them, then abort the setup.
2173 if (params->limited_cce != 0x00) {
2174 BT_ERR("%s: Unsupported Intel firmware loading method (%u)",
2175 hdev->name, params->limited_cce);
2180 /* If the OTP has no valid Bluetooth device address, then there will
2181 * also be no valid address for the operational firmware.
2183 if (!bacmp(¶ms->otp_bdaddr, BDADDR_ANY)) {
2184 BT_INFO("%s: No device address configured", hdev->name);
2185 set_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks);
2188 /* With this Intel bootloader only the hardware variant and device
2189 * revision information are used to select the right firmware.
2191 * The firmware filename is ibt-<hw_variant>-<dev_revid>.sfi.
2193 * Currently the supported hardware variants are:
2194 * 11 (0x0b) for iBT3.0 (LnP/SfP)
2195 * 12 (0x0c) for iBT3.5 (WsP)
2196 * 17 (0x11) for iBT3.5 (JfP)
2197 * 18 (0x12) for iBT3.5 (ThP)
2199 snprintf(fwname, sizeof(fwname), "/*(DEBLOBBED)*/",
2200 le16_to_cpu(ver.hw_variant),
2201 le16_to_cpu(params->dev_revid));
2203 err = reject_firmware(&fw, fwname, &hdev->dev);
2205 BT_ERR("%s: Failed to load Intel firmware file (%d)",
2211 BT_INFO("%s: Found device firmware: %s", hdev->name, fwname);
2213 /* Save the DDC file name for later use to apply once the firmware
2214 * downloading is done.
2216 snprintf(fwname, sizeof(fwname), "intel/ibt-%u-%u.ddc",
2217 le16_to_cpu(ver.hw_variant),
2218 le16_to_cpu(params->dev_revid));
2222 if (fw->size < 644) {
2223 BT_ERR("%s: Invalid size of firmware file (%zu)",
2224 hdev->name, fw->size);
2229 set_bit(BTUSB_DOWNLOADING, &data->flags);
2231 /* Start the firmware download transaction with the Init fragment
2232 * represented by the 128 bytes of CSS header.
2234 err = btintel_secure_send(hdev, 0x00, 128, fw->data);
2236 BT_ERR("%s: Failed to send firmware header (%d)",
2241 /* Send the 256 bytes of public key information from the firmware
2242 * as the PKey fragment.
2244 err = btintel_secure_send(hdev, 0x03, 256, fw->data + 128);
2246 BT_ERR("%s: Failed to send firmware public key (%d)",
2251 /* Send the 256 bytes of signature information from the firmware
2252 * as the Sign fragment.
2254 err = btintel_secure_send(hdev, 0x02, 256, fw->data + 388);
2256 BT_ERR("%s: Failed to send firmware signature (%d)",
2261 fw_ptr = fw->data + 644;
2264 while (fw_ptr - fw->data < fw->size) {
2265 struct hci_command_hdr *cmd = (void *)(fw_ptr + frag_len);
2267 frag_len += sizeof(*cmd) + cmd->plen;
2269 /* The parameter length of the secure send command requires
2270 * a 4 byte alignment. It happens so that the firmware file
2271 * contains proper Intel_NOP commands to align the fragments
2274 * Send set of commands with 4 byte alignment from the
2275 * firmware data buffer as a single Data fragement.
2277 if (!(frag_len % 4)) {
2278 err = btintel_secure_send(hdev, 0x01, frag_len, fw_ptr);
2280 BT_ERR("%s: Failed to send firmware data (%d)",
2290 set_bit(BTUSB_FIRMWARE_LOADED, &data->flags);
2292 BT_INFO("%s: Waiting for firmware download to complete", hdev->name);
2294 /* Before switching the device into operational mode and with that
2295 * booting the loaded firmware, wait for the bootloader notification
2296 * that all fragments have been successfully received.
2298 * When the event processing receives the notification, then the
2299 * BTUSB_DOWNLOADING flag will be cleared.
2301 * The firmware loading should not take longer than 5 seconds
2302 * and thus just timeout if that happens and fail the setup
2305 err = wait_on_bit_timeout(&data->flags, BTUSB_DOWNLOADING,
2307 msecs_to_jiffies(5000));
2308 if (err == -EINTR) {
2309 BT_ERR("%s: Firmware loading interrupted", hdev->name);
2314 BT_ERR("%s: Firmware loading timeout", hdev->name);
2319 if (test_bit(BTUSB_FIRMWARE_FAILED, &data->flags)) {
2320 BT_ERR("%s: Firmware loading failed", hdev->name);
2325 rettime = ktime_get();
2326 delta = ktime_sub(rettime, calltime);
2327 duration = (unsigned long long) ktime_to_ns(delta) >> 10;
2329 BT_INFO("%s: Firmware loaded in %llu usecs", hdev->name, duration);
2332 release_firmware(fw);
2337 calltime = ktime_get();
2339 set_bit(BTUSB_BOOTING, &data->flags);
2341 skb = __hci_cmd_sync(hdev, 0xfc01, sizeof(reset_param), reset_param,
2344 return PTR_ERR(skb);
2348 /* The bootloader will not indicate when the device is ready. This
2349 * is done by the operational firmware sending bootup notification.
2351 * Booting into operational firmware should not take longer than
2352 * 1 second. However if that happens, then just fail the setup
2353 * since something went wrong.
2355 BT_INFO("%s: Waiting for device to boot", hdev->name);
2357 err = wait_on_bit_timeout(&data->flags, BTUSB_BOOTING,
2359 msecs_to_jiffies(1000));
2361 if (err == -EINTR) {
2362 BT_ERR("%s: Device boot interrupted", hdev->name);
2367 BT_ERR("%s: Device boot timeout", hdev->name);
2371 rettime = ktime_get();
2372 delta = ktime_sub(rettime, calltime);
2373 duration = (unsigned long long) ktime_to_ns(delta) >> 10;
2375 BT_INFO("%s: Device booted in %llu usecs", hdev->name, duration);
2377 clear_bit(BTUSB_BOOTLOADER, &data->flags);
2379 /* Once the device is running in operational mode, it needs to apply
2380 * the device configuration (DDC) parameters.
2382 * The device can work without DDC parameters, so even if it fails
2383 * to load the file, no need to fail the setup.
2385 btintel_load_ddc_config(hdev, fwname);
2387 /* Set the event mask for Intel specific vendor events. This enables
2388 * a few extra events that are useful during general operation. It
2389 * does not enable any debugging related events.
2391 * The device will function correctly without these events enabled
2392 * and thus no need to fail the setup.
2394 btintel_set_event_mask(hdev, false);
2399 static int btusb_shutdown_intel(struct hci_dev *hdev)
2401 struct sk_buff *skb;
2404 /* Some platforms have an issue with BT LED when the interface is
2405 * down or BT radio is turned off, which takes 5 seconds to BT LED
2406 * goes off. This command turns off the BT LED immediately.
2408 skb = __hci_cmd_sync(hdev, 0xfc3f, 0, NULL, HCI_INIT_TIMEOUT);
2411 BT_ERR("%s: turning off Intel device LED failed (%ld)",
2421 /* Configure an out-of-band gpio as wake-up pin, if specified in device tree */
2422 static int marvell_config_oob_wake(struct hci_dev *hdev)
2424 struct sk_buff *skb;
2425 struct btusb_data *data = hci_get_drvdata(hdev);
2426 struct device *dev = &data->udev->dev;
2427 u16 pin, gap, opcode;
2431 /* Move on if no wakeup pin specified */
2432 if (of_property_read_u16(dev->of_node, "marvell,wakeup-pin", &pin) ||
2433 of_property_read_u16(dev->of_node, "marvell,wakeup-gap-ms", &gap))
2436 /* Vendor specific command to configure a GPIO as wake-up pin */
2437 opcode = hci_opcode_pack(0x3F, 0x59);
2438 cmd[0] = opcode & 0xFF;
2439 cmd[1] = opcode >> 8;
2440 cmd[2] = 2; /* length of parameters that follow */
2442 cmd[4] = gap; /* time in ms, for which wakeup pin should be asserted */
2444 skb = bt_skb_alloc(sizeof(cmd), GFP_KERNEL);
2446 bt_dev_err(hdev, "%s: No memory\n", __func__);
2450 skb_put_data(skb, cmd, sizeof(cmd));
2451 hci_skb_pkt_type(skb) = HCI_COMMAND_PKT;
2453 ret = btusb_send_frame(hdev, skb);
2455 bt_dev_err(hdev, "%s: configuration failed\n", __func__);
2464 static int btusb_set_bdaddr_marvell(struct hci_dev *hdev,
2465 const bdaddr_t *bdaddr)
2467 struct sk_buff *skb;
2472 buf[1] = sizeof(bdaddr_t);
2473 memcpy(buf + 2, bdaddr, sizeof(bdaddr_t));
2475 skb = __hci_cmd_sync(hdev, 0xfc22, sizeof(buf), buf, HCI_INIT_TIMEOUT);
2478 BT_ERR("%s: changing Marvell device address failed (%ld)",
2487 static int btusb_set_bdaddr_ath3012(struct hci_dev *hdev,
2488 const bdaddr_t *bdaddr)
2490 struct sk_buff *skb;
2497 buf[3] = sizeof(bdaddr_t);
2498 memcpy(buf + 4, bdaddr, sizeof(bdaddr_t));
2500 skb = __hci_cmd_sync(hdev, 0xfc0b, sizeof(buf), buf, HCI_INIT_TIMEOUT);
2503 BT_ERR("%s: Change address command failed (%ld)",
2512 #define QCA_DFU_PACKET_LEN 4096
2514 #define QCA_GET_TARGET_VERSION 0x09
2515 #define QCA_CHECK_STATUS 0x05
2516 #define QCA_DFU_DOWNLOAD 0x01
2518 #define QCA_SYSCFG_UPDATED 0x40
2519 #define QCA_PATCH_UPDATED 0x80
2520 #define QCA_DFU_TIMEOUT 3000
2522 struct qca_version {
2524 __le32 patch_version;
2530 struct qca_rampatch_version {
2532 __le16 patch_version;
2535 struct qca_device_info {
2537 u8 rampatch_hdr; /* length of header in rampatch */
2538 u8 nvm_hdr; /* length of header in NVM */
2539 u8 ver_offset; /* offset of version structure in rampatch */
2542 static const struct qca_device_info qca_devices_table[] = {
2543 { 0x00000100, 20, 4, 10 }, /* Rome 1.0 */
2544 { 0x00000101, 20, 4, 10 }, /* Rome 1.1 */
2545 { 0x00000200, 28, 4, 18 }, /* Rome 2.0 */
2546 { 0x00000201, 28, 4, 18 }, /* Rome 2.1 */
2547 { 0x00000300, 28, 4, 18 }, /* Rome 3.0 */
2548 { 0x00000302, 28, 4, 18 }, /* Rome 3.2 */
2551 static int btusb_qca_send_vendor_req(struct usb_device *udev, u8 request,
2552 void *data, u16 size)
2557 buf = kmalloc(size, GFP_KERNEL);
2561 /* Found some of USB hosts have IOT issues with ours so that we should
2562 * not wait until HCI layer is ready.
2564 pipe = usb_rcvctrlpipe(udev, 0);
2565 err = usb_control_msg(udev, pipe, request, USB_TYPE_VENDOR | USB_DIR_IN,
2566 0, 0, buf, size, USB_CTRL_SET_TIMEOUT);
2568 dev_err(&udev->dev, "Failed to access otp area (%d)", err);
2572 memcpy(data, buf, size);
2580 static int btusb_setup_qca_download_fw(struct hci_dev *hdev,
2581 const struct firmware *firmware,
2584 struct btusb_data *btdata = hci_get_drvdata(hdev);
2585 struct usb_device *udev = btdata->udev;
2586 size_t count, size, sent = 0;
2590 buf = kmalloc(QCA_DFU_PACKET_LEN, GFP_KERNEL);
2594 count = firmware->size;
2596 size = min_t(size_t, count, hdr_size);
2597 memcpy(buf, firmware->data, size);
2599 /* USB patches should go down to controller through USB path
2600 * because binary format fits to go down through USB channel.
2601 * USB control path is for patching headers and USB bulk is for
2604 pipe = usb_sndctrlpipe(udev, 0);
2605 err = usb_control_msg(udev, pipe, QCA_DFU_DOWNLOAD, USB_TYPE_VENDOR,
2606 0, 0, buf, size, USB_CTRL_SET_TIMEOUT);
2608 BT_ERR("%s: Failed to send headers (%d)", hdev->name, err);
2615 /* ep2 need time to switch from function acl to function dfu,
2616 * so we add 20ms delay here.
2621 size = min_t(size_t, count, QCA_DFU_PACKET_LEN);
2623 memcpy(buf, firmware->data + sent, size);
2625 pipe = usb_sndbulkpipe(udev, 0x02);
2626 err = usb_bulk_msg(udev, pipe, buf, size, &len,
2629 BT_ERR("%s: Failed to send body at %zd of %zd (%d)",
2630 hdev->name, sent, firmware->size, err);
2635 BT_ERR("%s: Failed to get bulk buffer", hdev->name);
2649 static int btusb_setup_qca_load_rampatch(struct hci_dev *hdev,
2650 struct qca_version *ver,
2651 const struct qca_device_info *info)
2653 struct qca_rampatch_version *rver;
2654 const struct firmware *fw;
2655 u32 ver_rom, ver_patch;
2656 u16 rver_rom, rver_patch;
2660 ver_rom = le32_to_cpu(ver->rom_version);
2661 ver_patch = le32_to_cpu(ver->patch_version);
2663 snprintf(fwname, sizeof(fwname), "/*(DEBLOBBED)*/", ver_rom);
2665 err = reject_firmware(&fw, fwname, &hdev->dev);
2667 BT_ERR("%s: failed to request rampatch file: %s (%d)",
2668 hdev->name, fwname, err);
2672 BT_INFO("%s: using rampatch file: %s", hdev->name, fwname);
2674 rver = (struct qca_rampatch_version *)(fw->data + info->ver_offset);
2675 rver_rom = le16_to_cpu(rver->rom_version);
2676 rver_patch = le16_to_cpu(rver->patch_version);
2678 BT_INFO("%s: QCA: patch rome 0x%x build 0x%x, firmware rome 0x%x "
2679 "build 0x%x", hdev->name, rver_rom, rver_patch, ver_rom,
2682 if (rver_rom != ver_rom || rver_patch <= ver_patch) {
2683 BT_ERR("%s: rampatch file version did not match with firmware",
2689 err = btusb_setup_qca_download_fw(hdev, fw, info->rampatch_hdr);
2692 release_firmware(fw);
2697 static int btusb_setup_qca_load_nvm(struct hci_dev *hdev,
2698 struct qca_version *ver,
2699 const struct qca_device_info *info)
2701 const struct firmware *fw;
2705 snprintf(fwname, sizeof(fwname), "/*(DEBLOBBED)*/",
2706 le32_to_cpu(ver->rom_version));
2708 err = reject_firmware(&fw, fwname, &hdev->dev);
2710 BT_ERR("%s: failed to request NVM file: %s (%d)",
2711 hdev->name, fwname, err);
2715 BT_INFO("%s: using NVM file: %s", hdev->name, fwname);
2717 err = btusb_setup_qca_download_fw(hdev, fw, info->nvm_hdr);
2719 release_firmware(fw);
2724 /* identify the ROM version and check whether patches are needed */
2725 static bool btusb_qca_need_patch(struct usb_device *udev)
2727 struct qca_version ver;
2729 if (btusb_qca_send_vendor_req(udev, QCA_GET_TARGET_VERSION, &ver,
2732 /* only low ROM versions need patches */
2733 return !(le32_to_cpu(ver.rom_version) & ~0xffffU);
2736 static int btusb_setup_qca(struct hci_dev *hdev)
2738 struct btusb_data *btdata = hci_get_drvdata(hdev);
2739 struct usb_device *udev = btdata->udev;
2740 const struct qca_device_info *info = NULL;
2741 struct qca_version ver;
2746 err = btusb_qca_send_vendor_req(udev, QCA_GET_TARGET_VERSION, &ver,
2751 ver_rom = le32_to_cpu(ver.rom_version);
2752 /* Don't care about high ROM versions */
2753 if (ver_rom & ~0xffffU)
2756 for (i = 0; i < ARRAY_SIZE(qca_devices_table); i++) {
2757 if (ver_rom == qca_devices_table[i].rom_version)
2758 info = &qca_devices_table[i];
2761 BT_ERR("%s: don't support firmware rome 0x%x", hdev->name,
2766 err = btusb_qca_send_vendor_req(udev, QCA_CHECK_STATUS, &status,
2771 if (!(status & QCA_PATCH_UPDATED)) {
2772 err = btusb_setup_qca_load_rampatch(hdev, &ver, info);
2777 if (!(status & QCA_SYSCFG_UPDATED)) {
2778 err = btusb_setup_qca_load_nvm(hdev, &ver, info);
2786 #ifdef CONFIG_BT_HCIBTUSB_BCM
2787 static inline int __set_diag_interface(struct hci_dev *hdev)
2789 struct btusb_data *data = hci_get_drvdata(hdev);
2790 struct usb_interface *intf = data->diag;
2796 data->diag_tx_ep = NULL;
2797 data->diag_rx_ep = NULL;
2799 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
2800 struct usb_endpoint_descriptor *ep_desc;
2802 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
2804 if (!data->diag_tx_ep && usb_endpoint_is_bulk_out(ep_desc)) {
2805 data->diag_tx_ep = ep_desc;
2809 if (!data->diag_rx_ep && usb_endpoint_is_bulk_in(ep_desc)) {
2810 data->diag_rx_ep = ep_desc;
2815 if (!data->diag_tx_ep || !data->diag_rx_ep) {
2816 BT_ERR("%s invalid diagnostic descriptors", hdev->name);
2823 static struct urb *alloc_diag_urb(struct hci_dev *hdev, bool enable)
2825 struct btusb_data *data = hci_get_drvdata(hdev);
2826 struct sk_buff *skb;
2830 if (!data->diag_tx_ep)
2831 return ERR_PTR(-ENODEV);
2833 urb = usb_alloc_urb(0, GFP_KERNEL);
2835 return ERR_PTR(-ENOMEM);
2837 skb = bt_skb_alloc(2, GFP_KERNEL);
2840 return ERR_PTR(-ENOMEM);
2843 skb_put_u8(skb, 0xf0);
2844 skb_put_u8(skb, enable);
2846 pipe = usb_sndbulkpipe(data->udev, data->diag_tx_ep->bEndpointAddress);
2848 usb_fill_bulk_urb(urb, data->udev, pipe,
2849 skb->data, skb->len, btusb_tx_complete, skb);
2851 skb->dev = (void *)hdev;
2856 static int btusb_bcm_set_diag(struct hci_dev *hdev, bool enable)
2858 struct btusb_data *data = hci_get_drvdata(hdev);
2864 if (!test_bit(HCI_RUNNING, &hdev->flags))
2867 urb = alloc_diag_urb(hdev, enable);
2869 return PTR_ERR(urb);
2871 return submit_or_queue_tx_urb(hdev, urb);
2876 static irqreturn_t btusb_oob_wake_handler(int irq, void *priv)
2878 struct btusb_data *data = priv;
2880 pm_wakeup_event(&data->udev->dev, 0);
2883 /* Disable only if not already disabled (keep it balanced) */
2884 if (test_and_clear_bit(BTUSB_OOB_WAKE_ENABLED, &data->flags)) {
2885 disable_irq_nosync(irq);
2886 disable_irq_wake(irq);
2891 static const struct of_device_id btusb_match_table[] = {
2892 { .compatible = "usb1286,204e" },
2895 MODULE_DEVICE_TABLE(of, btusb_match_table);
2897 /* Use an oob wakeup pin? */
2898 static int btusb_config_oob_wake(struct hci_dev *hdev)
2900 struct btusb_data *data = hci_get_drvdata(hdev);
2901 struct device *dev = &data->udev->dev;
2904 clear_bit(BTUSB_OOB_WAKE_ENABLED, &data->flags);
2906 if (!of_match_device(btusb_match_table, dev))
2909 /* Move on if no IRQ specified */
2910 irq = of_irq_get_byname(dev->of_node, "wakeup");
2912 bt_dev_dbg(hdev, "%s: no OOB Wakeup IRQ in DT", __func__);
2916 irq_set_status_flags(irq, IRQ_NOAUTOEN);
2917 ret = devm_request_irq(&hdev->dev, irq, btusb_oob_wake_handler,
2918 0, "OOB Wake-on-BT", data);
2920 bt_dev_err(hdev, "%s: IRQ request failed", __func__);
2924 ret = device_init_wakeup(dev, true);
2926 bt_dev_err(hdev, "%s: failed to init_wakeup", __func__);
2930 data->oob_wake_irq = irq;
2931 bt_dev_info(hdev, "OOB Wake-on-BT configured at IRQ %u", irq);
2936 static void btusb_check_needs_reset_resume(struct usb_interface *intf)
2938 if (dmi_check_system(btusb_needs_reset_resume_table))
2939 interface_to_usbdev(intf)->quirks |= USB_QUIRK_RESET_RESUME;
2942 static int btusb_probe(struct usb_interface *intf,
2943 const struct usb_device_id *id)
2945 struct usb_endpoint_descriptor *ep_desc;
2946 struct btusb_data *data;
2947 struct hci_dev *hdev;
2948 unsigned ifnum_base;
2951 BT_DBG("intf %p id %p", intf, id);
2953 /* interface numbers are hardcoded in the spec */
2954 if (intf->cur_altsetting->desc.bInterfaceNumber != 0) {
2955 if (!(id->driver_info & BTUSB_IFNUM_2))
2957 if (intf->cur_altsetting->desc.bInterfaceNumber != 2)
2961 ifnum_base = intf->cur_altsetting->desc.bInterfaceNumber;
2963 if (!id->driver_info) {
2964 const struct usb_device_id *match;
2966 match = usb_match_id(intf, blacklist_table);
2971 if (id->driver_info == BTUSB_IGNORE)
2974 if (id->driver_info & BTUSB_BCM_NO_PRODID) {
2975 struct usb_device *udev = interface_to_usbdev(intf);
2977 /* For the broken Broadcom devices that show 0000:0000
2978 * as USB vendor and product information, check that the
2979 * manufacturer string identifies them as Broadcom based
2982 if (!udev->manufacturer ||
2983 strcmp(udev->manufacturer, "Broadcom Corp"))
2987 if (id->driver_info & BTUSB_ATH3012) {
2988 struct usb_device *udev = interface_to_usbdev(intf);
2990 /* Old firmware would otherwise let ath3k driver load
2991 * patch and sysconfig files
2993 if (le16_to_cpu(udev->descriptor.bcdDevice) <= 0x0001 &&
2994 !btusb_qca_need_patch(udev))
2998 data = devm_kzalloc(&intf->dev, sizeof(*data), GFP_KERNEL);
3002 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
3003 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
3005 if (!data->intr_ep && usb_endpoint_is_int_in(ep_desc)) {
3006 data->intr_ep = ep_desc;
3010 if (!data->bulk_tx_ep && usb_endpoint_is_bulk_out(ep_desc)) {
3011 data->bulk_tx_ep = ep_desc;
3015 if (!data->bulk_rx_ep && usb_endpoint_is_bulk_in(ep_desc)) {
3016 data->bulk_rx_ep = ep_desc;
3021 if (!data->intr_ep || !data->bulk_tx_ep || !data->bulk_rx_ep)
3024 if (id->driver_info & BTUSB_AMP) {
3025 data->cmdreq_type = USB_TYPE_CLASS | 0x01;
3026 data->cmdreq = 0x2b;
3028 data->cmdreq_type = USB_TYPE_CLASS;
3029 data->cmdreq = 0x00;
3032 data->udev = interface_to_usbdev(intf);
3035 INIT_WORK(&data->work, btusb_work);
3036 INIT_WORK(&data->waker, btusb_waker);
3037 init_usb_anchor(&data->deferred);
3038 init_usb_anchor(&data->tx_anchor);
3039 spin_lock_init(&data->txlock);
3041 init_usb_anchor(&data->intr_anchor);
3042 init_usb_anchor(&data->bulk_anchor);
3043 init_usb_anchor(&data->isoc_anchor);
3044 init_usb_anchor(&data->diag_anchor);
3045 spin_lock_init(&data->rxlock);
3047 if (id->driver_info & BTUSB_INTEL_NEW) {
3048 data->recv_event = btusb_recv_event_intel;
3049 data->recv_bulk = btusb_recv_bulk_intel;
3050 set_bit(BTUSB_BOOTLOADER, &data->flags);
3052 data->recv_event = hci_recv_frame;
3053 data->recv_bulk = btusb_recv_bulk;
3056 hdev = hci_alloc_dev();
3060 hdev->bus = HCI_USB;
3061 hci_set_drvdata(hdev, data);
3063 if (id->driver_info & BTUSB_AMP)
3064 hdev->dev_type = HCI_AMP;
3066 hdev->dev_type = HCI_PRIMARY;
3070 SET_HCIDEV_DEV(hdev, &intf->dev);
3072 hdev->open = btusb_open;
3073 hdev->close = btusb_close;
3074 hdev->flush = btusb_flush;
3075 hdev->send = btusb_send_frame;
3076 hdev->notify = btusb_notify;
3079 err = btusb_config_oob_wake(hdev);
3083 /* Marvell devices may need a specific chip configuration */
3084 if (id->driver_info & BTUSB_MARVELL && data->oob_wake_irq) {
3085 err = marvell_config_oob_wake(hdev);
3090 if (id->driver_info & BTUSB_CW6622)
3091 set_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks);
3093 if (id->driver_info & BTUSB_BCM2045)
3094 set_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks);
3096 if (id->driver_info & BTUSB_BCM92035)
3097 hdev->setup = btusb_setup_bcm92035;
3099 #ifdef CONFIG_BT_HCIBTUSB_BCM
3100 if (id->driver_info & BTUSB_BCM_PATCHRAM) {
3101 hdev->manufacturer = 15;
3102 hdev->setup = btbcm_setup_patchram;
3103 hdev->set_diag = btusb_bcm_set_diag;
3104 hdev->set_bdaddr = btbcm_set_bdaddr;
3106 /* Broadcom LM_DIAG Interface numbers are hardcoded */
3107 data->diag = usb_ifnum_to_if(data->udev, ifnum_base + 2);
3110 if (id->driver_info & BTUSB_BCM_APPLE) {
3111 hdev->manufacturer = 15;
3112 hdev->setup = btbcm_setup_apple;
3113 hdev->set_diag = btusb_bcm_set_diag;
3115 /* Broadcom LM_DIAG Interface numbers are hardcoded */
3116 data->diag = usb_ifnum_to_if(data->udev, ifnum_base + 2);
3120 if (id->driver_info & BTUSB_INTEL) {
3121 hdev->manufacturer = 2;
3122 hdev->setup = btusb_setup_intel;
3123 hdev->shutdown = btusb_shutdown_intel;
3124 hdev->set_diag = btintel_set_diag_mfg;
3125 hdev->set_bdaddr = btintel_set_bdaddr;
3126 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
3127 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
3128 set_bit(HCI_QUIRK_NON_PERSISTENT_DIAG, &hdev->quirks);
3131 if (id->driver_info & BTUSB_INTEL_NEW) {
3132 hdev->manufacturer = 2;
3133 hdev->send = btusb_send_frame_intel;
3134 hdev->setup = btusb_setup_intel_new;
3135 hdev->hw_error = btintel_hw_error;
3136 hdev->set_diag = btintel_set_diag;
3137 hdev->set_bdaddr = btintel_set_bdaddr;
3138 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
3139 set_bit(HCI_QUIRK_NON_PERSISTENT_DIAG, &hdev->quirks);
3142 if (id->driver_info & BTUSB_MARVELL)
3143 hdev->set_bdaddr = btusb_set_bdaddr_marvell;
3145 if (id->driver_info & BTUSB_SWAVE) {
3146 set_bit(HCI_QUIRK_FIXUP_INQUIRY_MODE, &hdev->quirks);
3147 set_bit(HCI_QUIRK_BROKEN_LOCAL_COMMANDS, &hdev->quirks);
3150 if (id->driver_info & BTUSB_INTEL_BOOT) {
3151 hdev->manufacturer = 2;
3152 set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
3155 if (id->driver_info & BTUSB_ATH3012) {
3156 data->setup_on_usb = btusb_setup_qca;
3157 hdev->set_bdaddr = btusb_set_bdaddr_ath3012;
3158 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
3159 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
3162 if (id->driver_info & BTUSB_QCA_ROME) {
3163 data->setup_on_usb = btusb_setup_qca;
3164 hdev->set_bdaddr = btusb_set_bdaddr_ath3012;
3165 btusb_check_needs_reset_resume(intf);
3168 #ifdef CONFIG_BT_HCIBTUSB_RTL
3169 if (id->driver_info & BTUSB_REALTEK) {
3170 hdev->setup = btrtl_setup_realtek;
3172 /* Realtek devices lose their updated firmware over suspend,
3173 * but the USB hub doesn't notice any status change.
3174 * Explicitly request a device reset on resume.
3176 interface_to_usbdev(intf)->quirks |= USB_QUIRK_RESET_RESUME;
3180 if (id->driver_info & BTUSB_AMP) {
3181 /* AMP controllers do not support SCO packets */
3184 /* Interface orders are hardcoded in the specification */
3185 data->isoc = usb_ifnum_to_if(data->udev, ifnum_base + 1);
3189 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
3191 if (force_scofix || id->driver_info & BTUSB_WRONG_SCO_MTU) {
3192 if (!disable_scofix)
3193 set_bit(HCI_QUIRK_FIXUP_BUFFER_SIZE, &hdev->quirks);
3196 if (id->driver_info & BTUSB_BROKEN_ISOC)
3199 if (id->driver_info & BTUSB_DIGIANSWER) {
3200 data->cmdreq_type = USB_TYPE_VENDOR;
3201 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
3204 if (id->driver_info & BTUSB_CSR) {
3205 struct usb_device *udev = data->udev;
3206 u16 bcdDevice = le16_to_cpu(udev->descriptor.bcdDevice);
3208 /* Old firmware would otherwise execute USB reset */
3209 if (bcdDevice < 0x117)
3210 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
3212 /* Fake CSR devices with broken commands */
3213 if (bcdDevice <= 0x100 || bcdDevice == 0x134)
3214 hdev->setup = btusb_setup_csr;
3216 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
3219 if (id->driver_info & BTUSB_SNIFFER) {
3220 struct usb_device *udev = data->udev;
3222 /* New sniffer firmware has crippled HCI interface */
3223 if (le16_to_cpu(udev->descriptor.bcdDevice) > 0x997)
3224 set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
3227 if (id->driver_info & BTUSB_INTEL_BOOT) {
3228 /* A bug in the bootloader causes that interrupt interface is
3229 * only enabled after receiving SetInterface(0, AltSetting=0).
3231 err = usb_set_interface(data->udev, 0, 0);
3233 BT_ERR("failed to set interface 0, alt 0 %d", err);
3239 err = usb_driver_claim_interface(&btusb_driver,
3245 #ifdef CONFIG_BT_HCIBTUSB_BCM
3247 if (!usb_driver_claim_interface(&btusb_driver,
3249 __set_diag_interface(hdev);
3255 err = hci_register_dev(hdev);
3259 usb_set_intfdata(intf, data);
3268 static void btusb_disconnect(struct usb_interface *intf)
3270 struct btusb_data *data = usb_get_intfdata(intf);
3271 struct hci_dev *hdev;
3273 BT_DBG("intf %p", intf);
3279 usb_set_intfdata(data->intf, NULL);
3282 usb_set_intfdata(data->isoc, NULL);
3285 usb_set_intfdata(data->diag, NULL);
3287 hci_unregister_dev(hdev);
3289 if (intf == data->intf) {
3291 usb_driver_release_interface(&btusb_driver, data->isoc);
3293 usb_driver_release_interface(&btusb_driver, data->diag);
3294 } else if (intf == data->isoc) {
3296 usb_driver_release_interface(&btusb_driver, data->diag);
3297 usb_driver_release_interface(&btusb_driver, data->intf);
3298 } else if (intf == data->diag) {
3299 usb_driver_release_interface(&btusb_driver, data->intf);
3301 usb_driver_release_interface(&btusb_driver, data->isoc);
3304 if (data->oob_wake_irq)
3305 device_init_wakeup(&data->udev->dev, false);
3311 static int btusb_suspend(struct usb_interface *intf, pm_message_t message)
3313 struct btusb_data *data = usb_get_intfdata(intf);
3315 BT_DBG("intf %p", intf);
3317 if (data->suspend_count++)
3320 spin_lock_irq(&data->txlock);
3321 if (!(PMSG_IS_AUTO(message) && data->tx_in_flight)) {
3322 set_bit(BTUSB_SUSPENDING, &data->flags);
3323 spin_unlock_irq(&data->txlock);
3325 spin_unlock_irq(&data->txlock);
3326 data->suspend_count--;
3330 cancel_work_sync(&data->work);
3332 btusb_stop_traffic(data);
3333 usb_kill_anchored_urbs(&data->tx_anchor);
3335 if (data->oob_wake_irq && device_may_wakeup(&data->udev->dev)) {
3336 set_bit(BTUSB_OOB_WAKE_ENABLED, &data->flags);
3337 enable_irq_wake(data->oob_wake_irq);
3338 enable_irq(data->oob_wake_irq);
3344 static void play_deferred(struct btusb_data *data)
3349 while ((urb = usb_get_from_anchor(&data->deferred))) {
3350 usb_anchor_urb(urb, &data->tx_anchor);
3352 err = usb_submit_urb(urb, GFP_ATOMIC);
3354 if (err != -EPERM && err != -ENODEV)
3355 BT_ERR("%s urb %p submission failed (%d)",
3356 data->hdev->name, urb, -err);
3357 kfree(urb->setup_packet);
3358 usb_unanchor_urb(urb);
3363 data->tx_in_flight++;
3367 /* Cleanup the rest deferred urbs. */
3368 while ((urb = usb_get_from_anchor(&data->deferred))) {
3369 kfree(urb->setup_packet);
3374 static int btusb_resume(struct usb_interface *intf)
3376 struct btusb_data *data = usb_get_intfdata(intf);
3377 struct hci_dev *hdev = data->hdev;
3380 BT_DBG("intf %p", intf);
3382 if (--data->suspend_count)
3385 /* Disable only if not already disabled (keep it balanced) */
3386 if (test_and_clear_bit(BTUSB_OOB_WAKE_ENABLED, &data->flags)) {
3387 disable_irq(data->oob_wake_irq);
3388 disable_irq_wake(data->oob_wake_irq);
3391 if (!test_bit(HCI_RUNNING, &hdev->flags))
3394 if (test_bit(BTUSB_INTR_RUNNING, &data->flags)) {
3395 err = btusb_submit_intr_urb(hdev, GFP_NOIO);
3397 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
3402 if (test_bit(BTUSB_BULK_RUNNING, &data->flags)) {
3403 err = btusb_submit_bulk_urb(hdev, GFP_NOIO);
3405 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
3409 btusb_submit_bulk_urb(hdev, GFP_NOIO);
3412 if (test_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
3413 if (btusb_submit_isoc_urb(hdev, GFP_NOIO) < 0)
3414 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
3416 btusb_submit_isoc_urb(hdev, GFP_NOIO);
3419 spin_lock_irq(&data->txlock);
3420 play_deferred(data);
3421 clear_bit(BTUSB_SUSPENDING, &data->flags);
3422 spin_unlock_irq(&data->txlock);
3423 schedule_work(&data->work);
3428 usb_scuttle_anchored_urbs(&data->deferred);
3430 spin_lock_irq(&data->txlock);
3431 clear_bit(BTUSB_SUSPENDING, &data->flags);
3432 spin_unlock_irq(&data->txlock);
3438 static struct usb_driver btusb_driver = {
3440 .probe = btusb_probe,
3441 .disconnect = btusb_disconnect,
3443 .suspend = btusb_suspend,
3444 .resume = btusb_resume,
3446 .id_table = btusb_table,
3447 .supports_autosuspend = 1,
3448 .disable_hub_initiated_lpm = 1,
3451 module_usb_driver(btusb_driver);
3453 module_param(disable_scofix, bool, 0644);
3454 MODULE_PARM_DESC(disable_scofix, "Disable fixup of wrong SCO buffer size");
3456 module_param(force_scofix, bool, 0644);
3457 MODULE_PARM_DESC(force_scofix, "Force fixup of wrong SCO buffers size");
3459 module_param(reset, bool, 0644);
3460 MODULE_PARM_DESC(reset, "Send HCI reset command on initialization");
3462 MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
3463 MODULE_DESCRIPTION("Generic Bluetooth USB driver ver " VERSION);
3464 MODULE_VERSION(VERSION);
3465 MODULE_LICENSE("GPL");