net/x25: do not hold the cpu too long in x25_new_lci()
[linux-stable.git] / drivers / bluetooth / btusb.c
blobb8dffe937f4ff7df519ed2e106d6216cd910ffc3
1 /*
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>
37 #include "btintel.h"
38 #include "btbcm.h"
39 #include "btrtl.h"
41 #define VERSION "0.8"
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) },
96 /* Apple iMac11,1 */
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 /* Silicon Wave based devices */
392 { USB_DEVICE(0x0c10, 0x0000), .driver_info = BTUSB_SWAVE },
394 { } /* Terminating entry */
397 /* The Bluetooth USB module build into some devices needs to be reset on resume,
398 * this is a problem with the platform (likely shutting off all power) not with
399 * the module itself. So we use a DMI list to match known broken platforms.
401 static const struct dmi_system_id btusb_needs_reset_resume_table[] = {
403 /* Dell OptiPlex 3060 (QCA ROME device 0cf3:e007) */
404 .matches = {
405 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
406 DMI_MATCH(DMI_PRODUCT_NAME, "OptiPlex 3060"),
410 /* Dell XPS 9360 (QCA ROME device 0cf3:e300) */
411 .matches = {
412 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
413 DMI_MATCH(DMI_PRODUCT_NAME, "XPS 13 9360"),
419 #define BTUSB_MAX_ISOC_FRAMES 10
421 #define BTUSB_INTR_RUNNING 0
422 #define BTUSB_BULK_RUNNING 1
423 #define BTUSB_ISOC_RUNNING 2
424 #define BTUSB_SUSPENDING 3
425 #define BTUSB_DID_ISO_RESUME 4
426 #define BTUSB_BOOTLOADER 5
427 #define BTUSB_DOWNLOADING 6
428 #define BTUSB_FIRMWARE_LOADED 7
429 #define BTUSB_FIRMWARE_FAILED 8
430 #define BTUSB_BOOTING 9
431 #define BTUSB_DIAG_RUNNING 10
432 #define BTUSB_OOB_WAKE_ENABLED 11
434 struct btusb_data {
435 struct hci_dev *hdev;
436 struct usb_device *udev;
437 struct usb_interface *intf;
438 struct usb_interface *isoc;
439 struct usb_interface *diag;
441 unsigned long flags;
443 struct work_struct work;
444 struct work_struct waker;
446 struct usb_anchor deferred;
447 struct usb_anchor tx_anchor;
448 int tx_in_flight;
449 spinlock_t txlock;
451 struct usb_anchor intr_anchor;
452 struct usb_anchor bulk_anchor;
453 struct usb_anchor isoc_anchor;
454 struct usb_anchor diag_anchor;
455 spinlock_t rxlock;
457 struct sk_buff *evt_skb;
458 struct sk_buff *acl_skb;
459 struct sk_buff *sco_skb;
461 struct usb_endpoint_descriptor *intr_ep;
462 struct usb_endpoint_descriptor *bulk_tx_ep;
463 struct usb_endpoint_descriptor *bulk_rx_ep;
464 struct usb_endpoint_descriptor *isoc_tx_ep;
465 struct usb_endpoint_descriptor *isoc_rx_ep;
466 struct usb_endpoint_descriptor *diag_tx_ep;
467 struct usb_endpoint_descriptor *diag_rx_ep;
469 __u8 cmdreq_type;
470 __u8 cmdreq;
472 unsigned int sco_num;
473 int isoc_altsetting;
474 int suspend_count;
476 int (*recv_event)(struct hci_dev *hdev, struct sk_buff *skb);
477 int (*recv_bulk)(struct btusb_data *data, void *buffer, int count);
479 int (*setup_on_usb)(struct hci_dev *hdev);
481 int oob_wake_irq; /* irq for out-of-band wake-on-bt */
484 static inline void btusb_free_frags(struct btusb_data *data)
486 unsigned long flags;
488 spin_lock_irqsave(&data->rxlock, flags);
490 kfree_skb(data->evt_skb);
491 data->evt_skb = NULL;
493 kfree_skb(data->acl_skb);
494 data->acl_skb = NULL;
496 kfree_skb(data->sco_skb);
497 data->sco_skb = NULL;
499 spin_unlock_irqrestore(&data->rxlock, flags);
502 static int btusb_recv_intr(struct btusb_data *data, void *buffer, int count)
504 struct sk_buff *skb;
505 int err = 0;
507 spin_lock(&data->rxlock);
508 skb = data->evt_skb;
510 while (count) {
511 int len;
513 if (!skb) {
514 skb = bt_skb_alloc(HCI_MAX_EVENT_SIZE, GFP_ATOMIC);
515 if (!skb) {
516 err = -ENOMEM;
517 break;
520 hci_skb_pkt_type(skb) = HCI_EVENT_PKT;
521 hci_skb_expect(skb) = HCI_EVENT_HDR_SIZE;
524 len = min_t(uint, hci_skb_expect(skb), count);
525 skb_put_data(skb, buffer, len);
527 count -= len;
528 buffer += len;
529 hci_skb_expect(skb) -= len;
531 if (skb->len == HCI_EVENT_HDR_SIZE) {
532 /* Complete event header */
533 hci_skb_expect(skb) = hci_event_hdr(skb)->plen;
535 if (skb_tailroom(skb) < hci_skb_expect(skb)) {
536 kfree_skb(skb);
537 skb = NULL;
539 err = -EILSEQ;
540 break;
544 if (!hci_skb_expect(skb)) {
545 /* Complete frame */
546 data->recv_event(data->hdev, skb);
547 skb = NULL;
551 data->evt_skb = skb;
552 spin_unlock(&data->rxlock);
554 return err;
557 static int btusb_recv_bulk(struct btusb_data *data, void *buffer, int count)
559 struct sk_buff *skb;
560 int err = 0;
562 spin_lock(&data->rxlock);
563 skb = data->acl_skb;
565 while (count) {
566 int len;
568 if (!skb) {
569 skb = bt_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC);
570 if (!skb) {
571 err = -ENOMEM;
572 break;
575 hci_skb_pkt_type(skb) = HCI_ACLDATA_PKT;
576 hci_skb_expect(skb) = HCI_ACL_HDR_SIZE;
579 len = min_t(uint, hci_skb_expect(skb), count);
580 skb_put_data(skb, buffer, len);
582 count -= len;
583 buffer += len;
584 hci_skb_expect(skb) -= len;
586 if (skb->len == HCI_ACL_HDR_SIZE) {
587 __le16 dlen = hci_acl_hdr(skb)->dlen;
589 /* Complete ACL header */
590 hci_skb_expect(skb) = __le16_to_cpu(dlen);
592 if (skb_tailroom(skb) < hci_skb_expect(skb)) {
593 kfree_skb(skb);
594 skb = NULL;
596 err = -EILSEQ;
597 break;
601 if (!hci_skb_expect(skb)) {
602 /* Complete frame */
603 hci_recv_frame(data->hdev, skb);
604 skb = NULL;
608 data->acl_skb = skb;
609 spin_unlock(&data->rxlock);
611 return err;
614 static int btusb_recv_isoc(struct btusb_data *data, void *buffer, int count)
616 struct sk_buff *skb;
617 int err = 0;
619 spin_lock(&data->rxlock);
620 skb = data->sco_skb;
622 while (count) {
623 int len;
625 if (!skb) {
626 skb = bt_skb_alloc(HCI_MAX_SCO_SIZE, GFP_ATOMIC);
627 if (!skb) {
628 err = -ENOMEM;
629 break;
632 hci_skb_pkt_type(skb) = HCI_SCODATA_PKT;
633 hci_skb_expect(skb) = HCI_SCO_HDR_SIZE;
636 len = min_t(uint, hci_skb_expect(skb), count);
637 skb_put_data(skb, buffer, len);
639 count -= len;
640 buffer += len;
641 hci_skb_expect(skb) -= len;
643 if (skb->len == HCI_SCO_HDR_SIZE) {
644 /* Complete SCO header */
645 hci_skb_expect(skb) = hci_sco_hdr(skb)->dlen;
647 if (skb_tailroom(skb) < hci_skb_expect(skb)) {
648 kfree_skb(skb);
649 skb = NULL;
651 err = -EILSEQ;
652 break;
656 if (!hci_skb_expect(skb)) {
657 /* Complete frame */
658 hci_recv_frame(data->hdev, skb);
659 skb = NULL;
663 data->sco_skb = skb;
664 spin_unlock(&data->rxlock);
666 return err;
669 static void btusb_intr_complete(struct urb *urb)
671 struct hci_dev *hdev = urb->context;
672 struct btusb_data *data = hci_get_drvdata(hdev);
673 int err;
675 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
676 urb->actual_length);
678 if (!test_bit(HCI_RUNNING, &hdev->flags))
679 return;
681 if (urb->status == 0) {
682 hdev->stat.byte_rx += urb->actual_length;
684 if (btusb_recv_intr(data, urb->transfer_buffer,
685 urb->actual_length) < 0) {
686 BT_ERR("%s corrupted event packet", hdev->name);
687 hdev->stat.err_rx++;
689 } else if (urb->status == -ENOENT) {
690 /* Avoid suspend failed when usb_kill_urb */
691 return;
694 if (!test_bit(BTUSB_INTR_RUNNING, &data->flags))
695 return;
697 usb_mark_last_busy(data->udev);
698 usb_anchor_urb(urb, &data->intr_anchor);
700 err = usb_submit_urb(urb, GFP_ATOMIC);
701 if (err < 0) {
702 /* -EPERM: urb is being killed;
703 * -ENODEV: device got disconnected
705 if (err != -EPERM && err != -ENODEV)
706 BT_ERR("%s urb %p failed to resubmit (%d)",
707 hdev->name, urb, -err);
708 usb_unanchor_urb(urb);
712 static int btusb_submit_intr_urb(struct hci_dev *hdev, gfp_t mem_flags)
714 struct btusb_data *data = hci_get_drvdata(hdev);
715 struct urb *urb;
716 unsigned char *buf;
717 unsigned int pipe;
718 int err, size;
720 BT_DBG("%s", hdev->name);
722 if (!data->intr_ep)
723 return -ENODEV;
725 urb = usb_alloc_urb(0, mem_flags);
726 if (!urb)
727 return -ENOMEM;
729 size = le16_to_cpu(data->intr_ep->wMaxPacketSize);
731 buf = kmalloc(size, mem_flags);
732 if (!buf) {
733 usb_free_urb(urb);
734 return -ENOMEM;
737 pipe = usb_rcvintpipe(data->udev, data->intr_ep->bEndpointAddress);
739 usb_fill_int_urb(urb, data->udev, pipe, buf, size,
740 btusb_intr_complete, hdev, data->intr_ep->bInterval);
742 urb->transfer_flags |= URB_FREE_BUFFER;
744 usb_anchor_urb(urb, &data->intr_anchor);
746 err = usb_submit_urb(urb, mem_flags);
747 if (err < 0) {
748 if (err != -EPERM && err != -ENODEV)
749 BT_ERR("%s urb %p submission failed (%d)",
750 hdev->name, urb, -err);
751 usb_unanchor_urb(urb);
754 usb_free_urb(urb);
756 return err;
759 static void btusb_bulk_complete(struct urb *urb)
761 struct hci_dev *hdev = urb->context;
762 struct btusb_data *data = hci_get_drvdata(hdev);
763 int err;
765 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
766 urb->actual_length);
768 if (!test_bit(HCI_RUNNING, &hdev->flags))
769 return;
771 if (urb->status == 0) {
772 hdev->stat.byte_rx += urb->actual_length;
774 if (data->recv_bulk(data, urb->transfer_buffer,
775 urb->actual_length) < 0) {
776 BT_ERR("%s corrupted ACL packet", hdev->name);
777 hdev->stat.err_rx++;
779 } else if (urb->status == -ENOENT) {
780 /* Avoid suspend failed when usb_kill_urb */
781 return;
784 if (!test_bit(BTUSB_BULK_RUNNING, &data->flags))
785 return;
787 usb_anchor_urb(urb, &data->bulk_anchor);
788 usb_mark_last_busy(data->udev);
790 err = usb_submit_urb(urb, GFP_ATOMIC);
791 if (err < 0) {
792 /* -EPERM: urb is being killed;
793 * -ENODEV: device got disconnected
795 if (err != -EPERM && err != -ENODEV)
796 BT_ERR("%s urb %p failed to resubmit (%d)",
797 hdev->name, urb, -err);
798 usb_unanchor_urb(urb);
802 static int btusb_submit_bulk_urb(struct hci_dev *hdev, gfp_t mem_flags)
804 struct btusb_data *data = hci_get_drvdata(hdev);
805 struct urb *urb;
806 unsigned char *buf;
807 unsigned int pipe;
808 int err, size = HCI_MAX_FRAME_SIZE;
810 BT_DBG("%s", hdev->name);
812 if (!data->bulk_rx_ep)
813 return -ENODEV;
815 urb = usb_alloc_urb(0, mem_flags);
816 if (!urb)
817 return -ENOMEM;
819 buf = kmalloc(size, mem_flags);
820 if (!buf) {
821 usb_free_urb(urb);
822 return -ENOMEM;
825 pipe = usb_rcvbulkpipe(data->udev, data->bulk_rx_ep->bEndpointAddress);
827 usb_fill_bulk_urb(urb, data->udev, pipe, buf, size,
828 btusb_bulk_complete, hdev);
830 urb->transfer_flags |= URB_FREE_BUFFER;
832 usb_mark_last_busy(data->udev);
833 usb_anchor_urb(urb, &data->bulk_anchor);
835 err = usb_submit_urb(urb, mem_flags);
836 if (err < 0) {
837 if (err != -EPERM && err != -ENODEV)
838 BT_ERR("%s urb %p submission failed (%d)",
839 hdev->name, urb, -err);
840 usb_unanchor_urb(urb);
843 usb_free_urb(urb);
845 return err;
848 static void btusb_isoc_complete(struct urb *urb)
850 struct hci_dev *hdev = urb->context;
851 struct btusb_data *data = hci_get_drvdata(hdev);
852 int i, err;
854 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
855 urb->actual_length);
857 if (!test_bit(HCI_RUNNING, &hdev->flags))
858 return;
860 if (urb->status == 0) {
861 for (i = 0; i < urb->number_of_packets; i++) {
862 unsigned int offset = urb->iso_frame_desc[i].offset;
863 unsigned int length = urb->iso_frame_desc[i].actual_length;
865 if (urb->iso_frame_desc[i].status)
866 continue;
868 hdev->stat.byte_rx += length;
870 if (btusb_recv_isoc(data, urb->transfer_buffer + offset,
871 length) < 0) {
872 BT_ERR("%s corrupted SCO packet", hdev->name);
873 hdev->stat.err_rx++;
876 } else if (urb->status == -ENOENT) {
877 /* Avoid suspend failed when usb_kill_urb */
878 return;
881 if (!test_bit(BTUSB_ISOC_RUNNING, &data->flags))
882 return;
884 usb_anchor_urb(urb, &data->isoc_anchor);
886 err = usb_submit_urb(urb, GFP_ATOMIC);
887 if (err < 0) {
888 /* -EPERM: urb is being killed;
889 * -ENODEV: device got disconnected
891 if (err != -EPERM && err != -ENODEV)
892 BT_ERR("%s urb %p failed to resubmit (%d)",
893 hdev->name, urb, -err);
894 usb_unanchor_urb(urb);
898 static inline void __fill_isoc_descriptor(struct urb *urb, int len, int mtu)
900 int i, offset = 0;
902 BT_DBG("len %d mtu %d", len, mtu);
904 for (i = 0; i < BTUSB_MAX_ISOC_FRAMES && len >= mtu;
905 i++, offset += mtu, len -= mtu) {
906 urb->iso_frame_desc[i].offset = offset;
907 urb->iso_frame_desc[i].length = mtu;
910 if (len && i < BTUSB_MAX_ISOC_FRAMES) {
911 urb->iso_frame_desc[i].offset = offset;
912 urb->iso_frame_desc[i].length = len;
913 i++;
916 urb->number_of_packets = i;
919 static int btusb_submit_isoc_urb(struct hci_dev *hdev, gfp_t mem_flags)
921 struct btusb_data *data = hci_get_drvdata(hdev);
922 struct urb *urb;
923 unsigned char *buf;
924 unsigned int pipe;
925 int err, size;
927 BT_DBG("%s", hdev->name);
929 if (!data->isoc_rx_ep)
930 return -ENODEV;
932 urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, mem_flags);
933 if (!urb)
934 return -ENOMEM;
936 size = le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize) *
937 BTUSB_MAX_ISOC_FRAMES;
939 buf = kmalloc(size, mem_flags);
940 if (!buf) {
941 usb_free_urb(urb);
942 return -ENOMEM;
945 pipe = usb_rcvisocpipe(data->udev, data->isoc_rx_ep->bEndpointAddress);
947 usb_fill_int_urb(urb, data->udev, pipe, buf, size, btusb_isoc_complete,
948 hdev, data->isoc_rx_ep->bInterval);
950 urb->transfer_flags = URB_FREE_BUFFER | URB_ISO_ASAP;
952 __fill_isoc_descriptor(urb, size,
953 le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize));
955 usb_anchor_urb(urb, &data->isoc_anchor);
957 err = usb_submit_urb(urb, mem_flags);
958 if (err < 0) {
959 if (err != -EPERM && err != -ENODEV)
960 BT_ERR("%s urb %p submission failed (%d)",
961 hdev->name, urb, -err);
962 usb_unanchor_urb(urb);
965 usb_free_urb(urb);
967 return err;
970 static void btusb_diag_complete(struct urb *urb)
972 struct hci_dev *hdev = urb->context;
973 struct btusb_data *data = hci_get_drvdata(hdev);
974 int err;
976 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
977 urb->actual_length);
979 if (urb->status == 0) {
980 struct sk_buff *skb;
982 skb = bt_skb_alloc(urb->actual_length, GFP_ATOMIC);
983 if (skb) {
984 skb_put_data(skb, urb->transfer_buffer,
985 urb->actual_length);
986 hci_recv_diag(hdev, skb);
988 } else if (urb->status == -ENOENT) {
989 /* Avoid suspend failed when usb_kill_urb */
990 return;
993 if (!test_bit(BTUSB_DIAG_RUNNING, &data->flags))
994 return;
996 usb_anchor_urb(urb, &data->diag_anchor);
997 usb_mark_last_busy(data->udev);
999 err = usb_submit_urb(urb, GFP_ATOMIC);
1000 if (err < 0) {
1001 /* -EPERM: urb is being killed;
1002 * -ENODEV: device got disconnected
1004 if (err != -EPERM && err != -ENODEV)
1005 BT_ERR("%s urb %p failed to resubmit (%d)",
1006 hdev->name, urb, -err);
1007 usb_unanchor_urb(urb);
1011 static int btusb_submit_diag_urb(struct hci_dev *hdev, gfp_t mem_flags)
1013 struct btusb_data *data = hci_get_drvdata(hdev);
1014 struct urb *urb;
1015 unsigned char *buf;
1016 unsigned int pipe;
1017 int err, size = HCI_MAX_FRAME_SIZE;
1019 BT_DBG("%s", hdev->name);
1021 if (!data->diag_rx_ep)
1022 return -ENODEV;
1024 urb = usb_alloc_urb(0, mem_flags);
1025 if (!urb)
1026 return -ENOMEM;
1028 buf = kmalloc(size, mem_flags);
1029 if (!buf) {
1030 usb_free_urb(urb);
1031 return -ENOMEM;
1034 pipe = usb_rcvbulkpipe(data->udev, data->diag_rx_ep->bEndpointAddress);
1036 usb_fill_bulk_urb(urb, data->udev, pipe, buf, size,
1037 btusb_diag_complete, hdev);
1039 urb->transfer_flags |= URB_FREE_BUFFER;
1041 usb_mark_last_busy(data->udev);
1042 usb_anchor_urb(urb, &data->diag_anchor);
1044 err = usb_submit_urb(urb, mem_flags);
1045 if (err < 0) {
1046 if (err != -EPERM && err != -ENODEV)
1047 BT_ERR("%s urb %p submission failed (%d)",
1048 hdev->name, urb, -err);
1049 usb_unanchor_urb(urb);
1052 usb_free_urb(urb);
1054 return err;
1057 static void btusb_tx_complete(struct urb *urb)
1059 struct sk_buff *skb = urb->context;
1060 struct hci_dev *hdev = (struct hci_dev *)skb->dev;
1061 struct btusb_data *data = hci_get_drvdata(hdev);
1063 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
1064 urb->actual_length);
1066 if (!test_bit(HCI_RUNNING, &hdev->flags))
1067 goto done;
1069 if (!urb->status)
1070 hdev->stat.byte_tx += urb->transfer_buffer_length;
1071 else
1072 hdev->stat.err_tx++;
1074 done:
1075 spin_lock(&data->txlock);
1076 data->tx_in_flight--;
1077 spin_unlock(&data->txlock);
1079 kfree(urb->setup_packet);
1081 kfree_skb(skb);
1084 static void btusb_isoc_tx_complete(struct urb *urb)
1086 struct sk_buff *skb = urb->context;
1087 struct hci_dev *hdev = (struct hci_dev *)skb->dev;
1089 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
1090 urb->actual_length);
1092 if (!test_bit(HCI_RUNNING, &hdev->flags))
1093 goto done;
1095 if (!urb->status)
1096 hdev->stat.byte_tx += urb->transfer_buffer_length;
1097 else
1098 hdev->stat.err_tx++;
1100 done:
1101 kfree(urb->setup_packet);
1103 kfree_skb(skb);
1106 static int btusb_open(struct hci_dev *hdev)
1108 struct btusb_data *data = hci_get_drvdata(hdev);
1109 int err;
1111 BT_DBG("%s", hdev->name);
1113 err = usb_autopm_get_interface(data->intf);
1114 if (err < 0)
1115 return err;
1117 /* Patching USB firmware files prior to starting any URBs of HCI path
1118 * It is more safe to use USB bulk channel for downloading USB patch
1120 if (data->setup_on_usb) {
1121 err = data->setup_on_usb(hdev);
1122 if (err < 0)
1123 return err;
1126 data->intf->needs_remote_wakeup = 1;
1127 /* device specific wakeup source enabled and required for USB
1128 * remote wakeup while host is suspended
1130 device_wakeup_enable(&data->udev->dev);
1132 if (test_and_set_bit(BTUSB_INTR_RUNNING, &data->flags))
1133 goto done;
1135 err = btusb_submit_intr_urb(hdev, GFP_KERNEL);
1136 if (err < 0)
1137 goto failed;
1139 err = btusb_submit_bulk_urb(hdev, GFP_KERNEL);
1140 if (err < 0) {
1141 usb_kill_anchored_urbs(&data->intr_anchor);
1142 goto failed;
1145 set_bit(BTUSB_BULK_RUNNING, &data->flags);
1146 btusb_submit_bulk_urb(hdev, GFP_KERNEL);
1148 if (data->diag) {
1149 if (!btusb_submit_diag_urb(hdev, GFP_KERNEL))
1150 set_bit(BTUSB_DIAG_RUNNING, &data->flags);
1153 done:
1154 usb_autopm_put_interface(data->intf);
1155 return 0;
1157 failed:
1158 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
1159 usb_autopm_put_interface(data->intf);
1160 return err;
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);
1174 int err;
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);
1190 if (err < 0)
1191 goto failed;
1193 data->intf->needs_remote_wakeup = 0;
1194 device_wakeup_disable(&data->udev->dev);
1195 usb_autopm_put_interface(data->intf);
1197 failed:
1198 usb_scuttle_anchored_urbs(&data->deferred);
1199 return 0;
1202 static int btusb_flush(struct hci_dev *hdev)
1204 struct btusb_data *data = hci_get_drvdata(hdev);
1206 BT_DBG("%s", hdev->name);
1208 usb_kill_anchored_urbs(&data->tx_anchor);
1209 btusb_free_frags(data);
1211 return 0;
1214 static struct urb *alloc_ctrl_urb(struct hci_dev *hdev, struct sk_buff *skb)
1216 struct btusb_data *data = hci_get_drvdata(hdev);
1217 struct usb_ctrlrequest *dr;
1218 struct urb *urb;
1219 unsigned int pipe;
1221 urb = usb_alloc_urb(0, GFP_KERNEL);
1222 if (!urb)
1223 return ERR_PTR(-ENOMEM);
1225 dr = kmalloc(sizeof(*dr), GFP_KERNEL);
1226 if (!dr) {
1227 usb_free_urb(urb);
1228 return ERR_PTR(-ENOMEM);
1231 dr->bRequestType = data->cmdreq_type;
1232 dr->bRequest = data->cmdreq;
1233 dr->wIndex = 0;
1234 dr->wValue = 0;
1235 dr->wLength = __cpu_to_le16(skb->len);
1237 pipe = usb_sndctrlpipe(data->udev, 0x00);
1239 usb_fill_control_urb(urb, data->udev, pipe, (void *)dr,
1240 skb->data, skb->len, btusb_tx_complete, skb);
1242 skb->dev = (void *)hdev;
1244 return urb;
1247 static struct urb *alloc_bulk_urb(struct hci_dev *hdev, struct sk_buff *skb)
1249 struct btusb_data *data = hci_get_drvdata(hdev);
1250 struct urb *urb;
1251 unsigned int pipe;
1253 if (!data->bulk_tx_ep)
1254 return ERR_PTR(-ENODEV);
1256 urb = usb_alloc_urb(0, GFP_KERNEL);
1257 if (!urb)
1258 return ERR_PTR(-ENOMEM);
1260 pipe = usb_sndbulkpipe(data->udev, data->bulk_tx_ep->bEndpointAddress);
1262 usb_fill_bulk_urb(urb, data->udev, pipe,
1263 skb->data, skb->len, btusb_tx_complete, skb);
1265 skb->dev = (void *)hdev;
1267 return urb;
1270 static struct urb *alloc_isoc_urb(struct hci_dev *hdev, struct sk_buff *skb)
1272 struct btusb_data *data = hci_get_drvdata(hdev);
1273 struct urb *urb;
1274 unsigned int pipe;
1276 if (!data->isoc_tx_ep)
1277 return ERR_PTR(-ENODEV);
1279 urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, GFP_KERNEL);
1280 if (!urb)
1281 return ERR_PTR(-ENOMEM);
1283 pipe = usb_sndisocpipe(data->udev, data->isoc_tx_ep->bEndpointAddress);
1285 usb_fill_int_urb(urb, data->udev, pipe,
1286 skb->data, skb->len, btusb_isoc_tx_complete,
1287 skb, data->isoc_tx_ep->bInterval);
1289 urb->transfer_flags = URB_ISO_ASAP;
1291 __fill_isoc_descriptor(urb, skb->len,
1292 le16_to_cpu(data->isoc_tx_ep->wMaxPacketSize));
1294 skb->dev = (void *)hdev;
1296 return urb;
1299 static int submit_tx_urb(struct hci_dev *hdev, struct urb *urb)
1301 struct btusb_data *data = hci_get_drvdata(hdev);
1302 int err;
1304 usb_anchor_urb(urb, &data->tx_anchor);
1306 err = usb_submit_urb(urb, GFP_KERNEL);
1307 if (err < 0) {
1308 if (err != -EPERM && err != -ENODEV)
1309 BT_ERR("%s urb %p submission failed (%d)",
1310 hdev->name, urb, -err);
1311 kfree(urb->setup_packet);
1312 usb_unanchor_urb(urb);
1313 } else {
1314 usb_mark_last_busy(data->udev);
1317 usb_free_urb(urb);
1318 return err;
1321 static int submit_or_queue_tx_urb(struct hci_dev *hdev, struct urb *urb)
1323 struct btusb_data *data = hci_get_drvdata(hdev);
1324 unsigned long flags;
1325 bool suspending;
1327 spin_lock_irqsave(&data->txlock, flags);
1328 suspending = test_bit(BTUSB_SUSPENDING, &data->flags);
1329 if (!suspending)
1330 data->tx_in_flight++;
1331 spin_unlock_irqrestore(&data->txlock, flags);
1333 if (!suspending)
1334 return submit_tx_urb(hdev, urb);
1336 usb_anchor_urb(urb, &data->deferred);
1337 schedule_work(&data->waker);
1339 usb_free_urb(urb);
1340 return 0;
1343 static int btusb_send_frame(struct hci_dev *hdev, struct sk_buff *skb)
1345 struct urb *urb;
1347 BT_DBG("%s", hdev->name);
1349 switch (hci_skb_pkt_type(skb)) {
1350 case HCI_COMMAND_PKT:
1351 urb = alloc_ctrl_urb(hdev, skb);
1352 if (IS_ERR(urb))
1353 return PTR_ERR(urb);
1355 hdev->stat.cmd_tx++;
1356 return submit_or_queue_tx_urb(hdev, urb);
1358 case HCI_ACLDATA_PKT:
1359 urb = alloc_bulk_urb(hdev, skb);
1360 if (IS_ERR(urb))
1361 return PTR_ERR(urb);
1363 hdev->stat.acl_tx++;
1364 return submit_or_queue_tx_urb(hdev, urb);
1366 case HCI_SCODATA_PKT:
1367 if (hci_conn_num(hdev, SCO_LINK) < 1)
1368 return -ENODEV;
1370 urb = alloc_isoc_urb(hdev, skb);
1371 if (IS_ERR(urb))
1372 return PTR_ERR(urb);
1374 hdev->stat.sco_tx++;
1375 return submit_tx_urb(hdev, urb);
1378 return -EILSEQ;
1381 static void btusb_notify(struct hci_dev *hdev, unsigned int evt)
1383 struct btusb_data *data = hci_get_drvdata(hdev);
1385 BT_DBG("%s evt %d", hdev->name, evt);
1387 if (hci_conn_num(hdev, SCO_LINK) != data->sco_num) {
1388 data->sco_num = hci_conn_num(hdev, SCO_LINK);
1389 schedule_work(&data->work);
1393 static inline int __set_isoc_interface(struct hci_dev *hdev, int altsetting)
1395 struct btusb_data *data = hci_get_drvdata(hdev);
1396 struct usb_interface *intf = data->isoc;
1397 struct usb_endpoint_descriptor *ep_desc;
1398 int i, err;
1400 if (!data->isoc)
1401 return -ENODEV;
1403 err = usb_set_interface(data->udev, 1, altsetting);
1404 if (err < 0) {
1405 BT_ERR("%s setting interface failed (%d)", hdev->name, -err);
1406 return err;
1409 data->isoc_altsetting = altsetting;
1411 data->isoc_tx_ep = NULL;
1412 data->isoc_rx_ep = NULL;
1414 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
1415 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
1417 if (!data->isoc_tx_ep && usb_endpoint_is_isoc_out(ep_desc)) {
1418 data->isoc_tx_ep = ep_desc;
1419 continue;
1422 if (!data->isoc_rx_ep && usb_endpoint_is_isoc_in(ep_desc)) {
1423 data->isoc_rx_ep = ep_desc;
1424 continue;
1428 if (!data->isoc_tx_ep || !data->isoc_rx_ep) {
1429 BT_ERR("%s invalid SCO descriptors", hdev->name);
1430 return -ENODEV;
1433 return 0;
1436 static void btusb_work(struct work_struct *work)
1438 struct btusb_data *data = container_of(work, struct btusb_data, work);
1439 struct hci_dev *hdev = data->hdev;
1440 int new_alts;
1441 int err;
1443 if (data->sco_num > 0) {
1444 if (!test_bit(BTUSB_DID_ISO_RESUME, &data->flags)) {
1445 err = usb_autopm_get_interface(data->isoc ? data->isoc : data->intf);
1446 if (err < 0) {
1447 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1448 usb_kill_anchored_urbs(&data->isoc_anchor);
1449 return;
1452 set_bit(BTUSB_DID_ISO_RESUME, &data->flags);
1455 if (hdev->voice_setting & 0x0020) {
1456 static const int alts[3] = { 2, 4, 5 };
1458 new_alts = alts[data->sco_num - 1];
1459 } else {
1460 new_alts = data->sco_num;
1463 if (data->isoc_altsetting != new_alts) {
1464 unsigned long flags;
1466 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1467 usb_kill_anchored_urbs(&data->isoc_anchor);
1469 /* When isochronous alternate setting needs to be
1470 * changed, because SCO connection has been added
1471 * or removed, a packet fragment may be left in the
1472 * reassembling state. This could lead to wrongly
1473 * assembled fragments.
1475 * Clear outstanding fragment when selecting a new
1476 * alternate setting.
1478 spin_lock_irqsave(&data->rxlock, flags);
1479 kfree_skb(data->sco_skb);
1480 data->sco_skb = NULL;
1481 spin_unlock_irqrestore(&data->rxlock, flags);
1483 if (__set_isoc_interface(hdev, new_alts) < 0)
1484 return;
1487 if (!test_and_set_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
1488 if (btusb_submit_isoc_urb(hdev, GFP_KERNEL) < 0)
1489 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1490 else
1491 btusb_submit_isoc_urb(hdev, GFP_KERNEL);
1493 } else {
1494 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1495 usb_kill_anchored_urbs(&data->isoc_anchor);
1497 __set_isoc_interface(hdev, 0);
1498 if (test_and_clear_bit(BTUSB_DID_ISO_RESUME, &data->flags))
1499 usb_autopm_put_interface(data->isoc ? data->isoc : data->intf);
1503 static void btusb_waker(struct work_struct *work)
1505 struct btusb_data *data = container_of(work, struct btusb_data, waker);
1506 int err;
1508 err = usb_autopm_get_interface(data->intf);
1509 if (err < 0)
1510 return;
1512 usb_autopm_put_interface(data->intf);
1515 static int btusb_setup_bcm92035(struct hci_dev *hdev)
1517 struct sk_buff *skb;
1518 u8 val = 0x00;
1520 BT_DBG("%s", hdev->name);
1522 skb = __hci_cmd_sync(hdev, 0xfc3b, 1, &val, HCI_INIT_TIMEOUT);
1523 if (IS_ERR(skb))
1524 BT_ERR("BCM92035 command failed (%ld)", -PTR_ERR(skb));
1525 else
1526 kfree_skb(skb);
1528 return 0;
1531 static int btusb_setup_csr(struct hci_dev *hdev)
1533 struct hci_rp_read_local_version *rp;
1534 struct sk_buff *skb;
1536 BT_DBG("%s", hdev->name);
1538 skb = __hci_cmd_sync(hdev, HCI_OP_READ_LOCAL_VERSION, 0, NULL,
1539 HCI_INIT_TIMEOUT);
1540 if (IS_ERR(skb)) {
1541 int err = PTR_ERR(skb);
1542 BT_ERR("%s: CSR: Local version failed (%d)", hdev->name, err);
1543 return err;
1546 if (skb->len != sizeof(struct hci_rp_read_local_version)) {
1547 BT_ERR("%s: CSR: Local version length mismatch", hdev->name);
1548 kfree_skb(skb);
1549 return -EIO;
1552 rp = (struct hci_rp_read_local_version *)skb->data;
1554 /* Detect controllers which aren't real CSR ones. */
1555 if (le16_to_cpu(rp->manufacturer) != 10 ||
1556 le16_to_cpu(rp->lmp_subver) == 0x0c5c) {
1557 /* Clear the reset quirk since this is not an actual
1558 * early Bluetooth 1.1 device from CSR.
1560 clear_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
1562 /* These fake CSR controllers have all a broken
1563 * stored link key handling and so just disable it.
1565 set_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks);
1568 kfree_skb(skb);
1570 return 0;
1573 static const struct firmware *btusb_setup_intel_get_fw(struct hci_dev *hdev,
1574 struct intel_version *ver)
1576 const struct firmware *fw;
1577 char fwname[64];
1578 int ret;
1580 snprintf(fwname, sizeof(fwname),
1581 "intel/ibt-hw-%x.%x.%x-fw-%x.%x.%x.%x.%x.bseq",
1582 ver->hw_platform, ver->hw_variant, ver->hw_revision,
1583 ver->fw_variant, ver->fw_revision, ver->fw_build_num,
1584 ver->fw_build_ww, ver->fw_build_yy);
1586 ret = request_firmware(&fw, fwname, &hdev->dev);
1587 if (ret < 0) {
1588 if (ret == -EINVAL) {
1589 BT_ERR("%s Intel firmware file request failed (%d)",
1590 hdev->name, ret);
1591 return NULL;
1594 BT_ERR("%s failed to open Intel firmware file: %s(%d)",
1595 hdev->name, fwname, ret);
1597 /* If the correct firmware patch file is not found, use the
1598 * default firmware patch file instead
1600 snprintf(fwname, sizeof(fwname), "intel/ibt-hw-%x.%x.bseq",
1601 ver->hw_platform, ver->hw_variant);
1602 if (request_firmware(&fw, fwname, &hdev->dev) < 0) {
1603 BT_ERR("%s failed to open default Intel fw file: %s",
1604 hdev->name, fwname);
1605 return NULL;
1609 BT_INFO("%s: Intel Bluetooth firmware file: %s", hdev->name, fwname);
1611 return fw;
1614 static int btusb_setup_intel_patching(struct hci_dev *hdev,
1615 const struct firmware *fw,
1616 const u8 **fw_ptr, int *disable_patch)
1618 struct sk_buff *skb;
1619 struct hci_command_hdr *cmd;
1620 const u8 *cmd_param;
1621 struct hci_event_hdr *evt = NULL;
1622 const u8 *evt_param = NULL;
1623 int remain = fw->size - (*fw_ptr - fw->data);
1625 /* The first byte indicates the types of the patch command or event.
1626 * 0x01 means HCI command and 0x02 is HCI event. If the first bytes
1627 * in the current firmware buffer doesn't start with 0x01 or
1628 * the size of remain buffer is smaller than HCI command header,
1629 * the firmware file is corrupted and it should stop the patching
1630 * process.
1632 if (remain > HCI_COMMAND_HDR_SIZE && *fw_ptr[0] != 0x01) {
1633 BT_ERR("%s Intel fw corrupted: invalid cmd read", hdev->name);
1634 return -EINVAL;
1636 (*fw_ptr)++;
1637 remain--;
1639 cmd = (struct hci_command_hdr *)(*fw_ptr);
1640 *fw_ptr += sizeof(*cmd);
1641 remain -= sizeof(*cmd);
1643 /* Ensure that the remain firmware data is long enough than the length
1644 * of command parameter. If not, the firmware file is corrupted.
1646 if (remain < cmd->plen) {
1647 BT_ERR("%s Intel fw corrupted: invalid cmd len", hdev->name);
1648 return -EFAULT;
1651 /* If there is a command that loads a patch in the firmware
1652 * file, then enable the patch upon success, otherwise just
1653 * disable the manufacturer mode, for example patch activation
1654 * is not required when the default firmware patch file is used
1655 * because there are no patch data to load.
1657 if (*disable_patch && le16_to_cpu(cmd->opcode) == 0xfc8e)
1658 *disable_patch = 0;
1660 cmd_param = *fw_ptr;
1661 *fw_ptr += cmd->plen;
1662 remain -= cmd->plen;
1664 /* This reads the expected events when the above command is sent to the
1665 * device. Some vendor commands expects more than one events, for
1666 * example command status event followed by vendor specific event.
1667 * For this case, it only keeps the last expected event. so the command
1668 * can be sent with __hci_cmd_sync_ev() which returns the sk_buff of
1669 * last expected event.
1671 while (remain > HCI_EVENT_HDR_SIZE && *fw_ptr[0] == 0x02) {
1672 (*fw_ptr)++;
1673 remain--;
1675 evt = (struct hci_event_hdr *)(*fw_ptr);
1676 *fw_ptr += sizeof(*evt);
1677 remain -= sizeof(*evt);
1679 if (remain < evt->plen) {
1680 BT_ERR("%s Intel fw corrupted: invalid evt len",
1681 hdev->name);
1682 return -EFAULT;
1685 evt_param = *fw_ptr;
1686 *fw_ptr += evt->plen;
1687 remain -= evt->plen;
1690 /* Every HCI commands in the firmware file has its correspond event.
1691 * If event is not found or remain is smaller than zero, the firmware
1692 * file is corrupted.
1694 if (!evt || !evt_param || remain < 0) {
1695 BT_ERR("%s Intel fw corrupted: invalid evt read", hdev->name);
1696 return -EFAULT;
1699 skb = __hci_cmd_sync_ev(hdev, le16_to_cpu(cmd->opcode), cmd->plen,
1700 cmd_param, evt->evt, HCI_INIT_TIMEOUT);
1701 if (IS_ERR(skb)) {
1702 BT_ERR("%s sending Intel patch command (0x%4.4x) failed (%ld)",
1703 hdev->name, cmd->opcode, PTR_ERR(skb));
1704 return PTR_ERR(skb);
1707 /* It ensures that the returned event matches the event data read from
1708 * the firmware file. At fist, it checks the length and then
1709 * the contents of the event.
1711 if (skb->len != evt->plen) {
1712 BT_ERR("%s mismatch event length (opcode 0x%4.4x)", hdev->name,
1713 le16_to_cpu(cmd->opcode));
1714 kfree_skb(skb);
1715 return -EFAULT;
1718 if (memcmp(skb->data, evt_param, evt->plen)) {
1719 BT_ERR("%s mismatch event parameter (opcode 0x%4.4x)",
1720 hdev->name, le16_to_cpu(cmd->opcode));
1721 kfree_skb(skb);
1722 return -EFAULT;
1724 kfree_skb(skb);
1726 return 0;
1729 static int btusb_setup_intel(struct hci_dev *hdev)
1731 struct sk_buff *skb;
1732 const struct firmware *fw;
1733 const u8 *fw_ptr;
1734 int disable_patch, err;
1735 struct intel_version ver;
1737 BT_DBG("%s", hdev->name);
1739 /* The controller has a bug with the first HCI command sent to it
1740 * returning number of completed commands as zero. This would stall the
1741 * command processing in the Bluetooth core.
1743 * As a workaround, send HCI Reset command first which will reset the
1744 * number of completed commands and allow normal command processing
1745 * from now on.
1747 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
1748 if (IS_ERR(skb)) {
1749 BT_ERR("%s sending initial HCI reset command failed (%ld)",
1750 hdev->name, PTR_ERR(skb));
1751 return PTR_ERR(skb);
1753 kfree_skb(skb);
1755 /* Read Intel specific controller version first to allow selection of
1756 * which firmware file to load.
1758 * The returned information are hardware variant and revision plus
1759 * firmware variant, revision and build number.
1761 err = btintel_read_version(hdev, &ver);
1762 if (err)
1763 return err;
1765 BT_INFO("%s: read Intel version: %02x%02x%02x%02x%02x%02x%02x%02x%02x",
1766 hdev->name, ver.hw_platform, ver.hw_variant, ver.hw_revision,
1767 ver.fw_variant, ver.fw_revision, ver.fw_build_num,
1768 ver.fw_build_ww, ver.fw_build_yy, ver.fw_patch_num);
1770 /* fw_patch_num indicates the version of patch the device currently
1771 * have. If there is no patch data in the device, it is always 0x00.
1772 * So, if it is other than 0x00, no need to patch the device again.
1774 if (ver.fw_patch_num) {
1775 BT_INFO("%s: Intel device is already patched. patch num: %02x",
1776 hdev->name, ver.fw_patch_num);
1777 goto complete;
1780 /* Opens the firmware patch file based on the firmware version read
1781 * from the controller. If it fails to open the matching firmware
1782 * patch file, it tries to open the default firmware patch file.
1783 * If no patch file is found, allow the device to operate without
1784 * a patch.
1786 fw = btusb_setup_intel_get_fw(hdev, &ver);
1787 if (!fw)
1788 goto complete;
1789 fw_ptr = fw->data;
1791 /* Enable the manufacturer mode of the controller.
1792 * Only while this mode is enabled, the driver can download the
1793 * firmware patch data and configuration parameters.
1795 err = btintel_enter_mfg(hdev);
1796 if (err) {
1797 release_firmware(fw);
1798 return err;
1801 disable_patch = 1;
1803 /* The firmware data file consists of list of Intel specific HCI
1804 * commands and its expected events. The first byte indicates the
1805 * type of the message, either HCI command or HCI event.
1807 * It reads the command and its expected event from the firmware file,
1808 * and send to the controller. Once __hci_cmd_sync_ev() returns,
1809 * the returned event is compared with the event read from the firmware
1810 * file and it will continue until all the messages are downloaded to
1811 * the controller.
1813 * Once the firmware patching is completed successfully,
1814 * the manufacturer mode is disabled with reset and activating the
1815 * downloaded patch.
1817 * If the firmware patching fails, the manufacturer mode is
1818 * disabled with reset and deactivating the patch.
1820 * If the default patch file is used, no reset is done when disabling
1821 * the manufacturer.
1823 while (fw->size > fw_ptr - fw->data) {
1824 int ret;
1826 ret = btusb_setup_intel_patching(hdev, fw, &fw_ptr,
1827 &disable_patch);
1828 if (ret < 0)
1829 goto exit_mfg_deactivate;
1832 release_firmware(fw);
1834 if (disable_patch)
1835 goto exit_mfg_disable;
1837 /* Patching completed successfully and disable the manufacturer mode
1838 * with reset and activate the downloaded firmware patches.
1840 err = btintel_exit_mfg(hdev, true, true);
1841 if (err)
1842 return err;
1844 BT_INFO("%s: Intel Bluetooth firmware patch completed and activated",
1845 hdev->name);
1847 goto complete;
1849 exit_mfg_disable:
1850 /* Disable the manufacturer mode without reset */
1851 err = btintel_exit_mfg(hdev, false, false);
1852 if (err)
1853 return err;
1855 BT_INFO("%s: Intel Bluetooth firmware patch completed", hdev->name);
1857 goto complete;
1859 exit_mfg_deactivate:
1860 release_firmware(fw);
1862 /* Patching failed. Disable the manufacturer mode with reset and
1863 * deactivate the downloaded firmware patches.
1865 err = btintel_exit_mfg(hdev, true, false);
1866 if (err)
1867 return err;
1869 BT_INFO("%s: Intel Bluetooth firmware patch completed and deactivated",
1870 hdev->name);
1872 complete:
1873 /* Set the event mask for Intel specific vendor events. This enables
1874 * a few extra events that are useful during general operation.
1876 btintel_set_event_mask_mfg(hdev, false);
1878 btintel_check_bdaddr(hdev);
1879 return 0;
1882 static int inject_cmd_complete(struct hci_dev *hdev, __u16 opcode)
1884 struct sk_buff *skb;
1885 struct hci_event_hdr *hdr;
1886 struct hci_ev_cmd_complete *evt;
1888 skb = bt_skb_alloc(sizeof(*hdr) + sizeof(*evt) + 1, GFP_ATOMIC);
1889 if (!skb)
1890 return -ENOMEM;
1892 hdr = skb_put(skb, sizeof(*hdr));
1893 hdr->evt = HCI_EV_CMD_COMPLETE;
1894 hdr->plen = sizeof(*evt) + 1;
1896 evt = skb_put(skb, sizeof(*evt));
1897 evt->ncmd = 0x01;
1898 evt->opcode = cpu_to_le16(opcode);
1900 skb_put_u8(skb, 0x00);
1902 hci_skb_pkt_type(skb) = HCI_EVENT_PKT;
1904 return hci_recv_frame(hdev, skb);
1907 static int btusb_recv_bulk_intel(struct btusb_data *data, void *buffer,
1908 int count)
1910 /* When the device is in bootloader mode, then it can send
1911 * events via the bulk endpoint. These events are treated the
1912 * same way as the ones received from the interrupt endpoint.
1914 if (test_bit(BTUSB_BOOTLOADER, &data->flags))
1915 return btusb_recv_intr(data, buffer, count);
1917 return btusb_recv_bulk(data, buffer, count);
1920 static void btusb_intel_bootup(struct btusb_data *data, const void *ptr,
1921 unsigned int len)
1923 const struct intel_bootup *evt = ptr;
1925 if (len != sizeof(*evt))
1926 return;
1928 if (test_and_clear_bit(BTUSB_BOOTING, &data->flags)) {
1929 smp_mb__after_atomic();
1930 wake_up_bit(&data->flags, BTUSB_BOOTING);
1934 static void btusb_intel_secure_send_result(struct btusb_data *data,
1935 const void *ptr, unsigned int len)
1937 const struct intel_secure_send_result *evt = ptr;
1939 if (len != sizeof(*evt))
1940 return;
1942 if (evt->result)
1943 set_bit(BTUSB_FIRMWARE_FAILED, &data->flags);
1945 if (test_and_clear_bit(BTUSB_DOWNLOADING, &data->flags) &&
1946 test_bit(BTUSB_FIRMWARE_LOADED, &data->flags)) {
1947 smp_mb__after_atomic();
1948 wake_up_bit(&data->flags, BTUSB_DOWNLOADING);
1952 static int btusb_recv_event_intel(struct hci_dev *hdev, struct sk_buff *skb)
1954 struct btusb_data *data = hci_get_drvdata(hdev);
1956 if (test_bit(BTUSB_BOOTLOADER, &data->flags)) {
1957 struct hci_event_hdr *hdr = (void *)skb->data;
1959 if (skb->len > HCI_EVENT_HDR_SIZE && hdr->evt == 0xff &&
1960 hdr->plen > 0) {
1961 const void *ptr = skb->data + HCI_EVENT_HDR_SIZE + 1;
1962 unsigned int len = skb->len - HCI_EVENT_HDR_SIZE - 1;
1964 switch (skb->data[2]) {
1965 case 0x02:
1966 /* When switching to the operational firmware
1967 * the device sends a vendor specific event
1968 * indicating that the bootup completed.
1970 btusb_intel_bootup(data, ptr, len);
1971 break;
1972 case 0x06:
1973 /* When the firmware loading completes the
1974 * device sends out a vendor specific event
1975 * indicating the result of the firmware
1976 * loading.
1978 btusb_intel_secure_send_result(data, ptr, len);
1979 break;
1984 return hci_recv_frame(hdev, skb);
1987 static int btusb_send_frame_intel(struct hci_dev *hdev, struct sk_buff *skb)
1989 struct btusb_data *data = hci_get_drvdata(hdev);
1990 struct urb *urb;
1992 BT_DBG("%s", hdev->name);
1994 switch (hci_skb_pkt_type(skb)) {
1995 case HCI_COMMAND_PKT:
1996 if (test_bit(BTUSB_BOOTLOADER, &data->flags)) {
1997 struct hci_command_hdr *cmd = (void *)skb->data;
1998 __u16 opcode = le16_to_cpu(cmd->opcode);
2000 /* When in bootloader mode and the command 0xfc09
2001 * is received, it needs to be send down the
2002 * bulk endpoint. So allocate a bulk URB instead.
2004 if (opcode == 0xfc09)
2005 urb = alloc_bulk_urb(hdev, skb);
2006 else
2007 urb = alloc_ctrl_urb(hdev, skb);
2009 /* When the 0xfc01 command is issued to boot into
2010 * the operational firmware, it will actually not
2011 * send a command complete event. To keep the flow
2012 * control working inject that event here.
2014 if (opcode == 0xfc01)
2015 inject_cmd_complete(hdev, opcode);
2016 } else {
2017 urb = alloc_ctrl_urb(hdev, skb);
2019 if (IS_ERR(urb))
2020 return PTR_ERR(urb);
2022 hdev->stat.cmd_tx++;
2023 return submit_or_queue_tx_urb(hdev, urb);
2025 case HCI_ACLDATA_PKT:
2026 urb = alloc_bulk_urb(hdev, skb);
2027 if (IS_ERR(urb))
2028 return PTR_ERR(urb);
2030 hdev->stat.acl_tx++;
2031 return submit_or_queue_tx_urb(hdev, urb);
2033 case HCI_SCODATA_PKT:
2034 if (hci_conn_num(hdev, SCO_LINK) < 1)
2035 return -ENODEV;
2037 urb = alloc_isoc_urb(hdev, skb);
2038 if (IS_ERR(urb))
2039 return PTR_ERR(urb);
2041 hdev->stat.sco_tx++;
2042 return submit_tx_urb(hdev, urb);
2045 return -EILSEQ;
2048 static int btusb_setup_intel_new(struct hci_dev *hdev)
2050 static const u8 reset_param[] = { 0x00, 0x01, 0x00, 0x01,
2051 0x00, 0x08, 0x04, 0x00 };
2052 struct btusb_data *data = hci_get_drvdata(hdev);
2053 struct sk_buff *skb;
2054 struct intel_version ver;
2055 struct intel_boot_params *params;
2056 const struct firmware *fw;
2057 const u8 *fw_ptr;
2058 u32 frag_len;
2059 char fwname[64];
2060 ktime_t calltime, delta, rettime;
2061 unsigned long long duration;
2062 int err;
2064 BT_DBG("%s", hdev->name);
2066 calltime = ktime_get();
2068 /* Read the Intel version information to determine if the device
2069 * is in bootloader mode or if it already has operational firmware
2070 * loaded.
2072 err = btintel_read_version(hdev, &ver);
2073 if (err)
2074 return err;
2076 /* The hardware platform number has a fixed value of 0x37 and
2077 * for now only accept this single value.
2079 if (ver.hw_platform != 0x37) {
2080 BT_ERR("%s: Unsupported Intel hardware platform (%u)",
2081 hdev->name, ver.hw_platform);
2082 return -EINVAL;
2085 /* Check for supported iBT hardware variants of this firmware
2086 * loading method.
2088 * This check has been put in place to ensure correct forward
2089 * compatibility options when newer hardware variants come along.
2091 switch (ver.hw_variant) {
2092 case 0x0b: /* SfP */
2093 case 0x0c: /* WsP */
2094 case 0x11: /* JfP */
2095 case 0x12: /* ThP */
2096 break;
2097 default:
2098 BT_ERR("%s: Unsupported Intel hardware variant (%u)",
2099 hdev->name, ver.hw_variant);
2100 return -EINVAL;
2103 btintel_version_info(hdev, &ver);
2105 /* The firmware variant determines if the device is in bootloader
2106 * mode or is running operational firmware. The value 0x06 identifies
2107 * the bootloader and the value 0x23 identifies the operational
2108 * firmware.
2110 * When the operational firmware is already present, then only
2111 * the check for valid Bluetooth device address is needed. This
2112 * determines if the device will be added as configured or
2113 * unconfigured controller.
2115 * It is not possible to use the Secure Boot Parameters in this
2116 * case since that command is only available in bootloader mode.
2118 if (ver.fw_variant == 0x23) {
2119 clear_bit(BTUSB_BOOTLOADER, &data->flags);
2120 btintel_check_bdaddr(hdev);
2121 return 0;
2124 /* If the device is not in bootloader mode, then the only possible
2125 * choice is to return an error and abort the device initialization.
2127 if (ver.fw_variant != 0x06) {
2128 BT_ERR("%s: Unsupported Intel firmware variant (%u)",
2129 hdev->name, ver.fw_variant);
2130 return -ENODEV;
2133 /* Read the secure boot parameters to identify the operating
2134 * details of the bootloader.
2136 skb = __hci_cmd_sync(hdev, 0xfc0d, 0, NULL, HCI_INIT_TIMEOUT);
2137 if (IS_ERR(skb)) {
2138 BT_ERR("%s: Reading Intel boot parameters failed (%ld)",
2139 hdev->name, PTR_ERR(skb));
2140 return PTR_ERR(skb);
2143 if (skb->len != sizeof(*params)) {
2144 BT_ERR("%s: Intel boot parameters size mismatch", hdev->name);
2145 kfree_skb(skb);
2146 return -EILSEQ;
2149 params = (struct intel_boot_params *)skb->data;
2151 BT_INFO("%s: Device revision is %u", hdev->name,
2152 le16_to_cpu(params->dev_revid));
2154 BT_INFO("%s: Secure boot is %s", hdev->name,
2155 params->secure_boot ? "enabled" : "disabled");
2157 BT_INFO("%s: OTP lock is %s", hdev->name,
2158 params->otp_lock ? "enabled" : "disabled");
2160 BT_INFO("%s: API lock is %s", hdev->name,
2161 params->api_lock ? "enabled" : "disabled");
2163 BT_INFO("%s: Debug lock is %s", hdev->name,
2164 params->debug_lock ? "enabled" : "disabled");
2166 BT_INFO("%s: Minimum firmware build %u week %u %u", hdev->name,
2167 params->min_fw_build_nn, params->min_fw_build_cw,
2168 2000 + params->min_fw_build_yy);
2170 /* It is required that every single firmware fragment is acknowledged
2171 * with a command complete event. If the boot parameters indicate
2172 * that this bootloader does not send them, then abort the setup.
2174 if (params->limited_cce != 0x00) {
2175 BT_ERR("%s: Unsupported Intel firmware loading method (%u)",
2176 hdev->name, params->limited_cce);
2177 kfree_skb(skb);
2178 return -EINVAL;
2181 /* If the OTP has no valid Bluetooth device address, then there will
2182 * also be no valid address for the operational firmware.
2184 if (!bacmp(&params->otp_bdaddr, BDADDR_ANY)) {
2185 BT_INFO("%s: No device address configured", hdev->name);
2186 set_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks);
2189 /* With this Intel bootloader only the hardware variant and device
2190 * revision information are used to select the right firmware.
2192 * The firmware filename is ibt-<hw_variant>-<dev_revid>.sfi.
2194 * Currently the supported hardware variants are:
2195 * 11 (0x0b) for iBT3.0 (LnP/SfP)
2196 * 12 (0x0c) for iBT3.5 (WsP)
2197 * 17 (0x11) for iBT3.5 (JfP)
2198 * 18 (0x12) for iBT3.5 (ThP)
2200 snprintf(fwname, sizeof(fwname), "intel/ibt-%u-%u.sfi",
2201 le16_to_cpu(ver.hw_variant),
2202 le16_to_cpu(params->dev_revid));
2204 err = request_firmware(&fw, fwname, &hdev->dev);
2205 if (err < 0) {
2206 BT_ERR("%s: Failed to load Intel firmware file (%d)",
2207 hdev->name, err);
2208 kfree_skb(skb);
2209 return err;
2212 BT_INFO("%s: Found device firmware: %s", hdev->name, fwname);
2214 /* Save the DDC file name for later use to apply once the firmware
2215 * downloading is done.
2217 snprintf(fwname, sizeof(fwname), "intel/ibt-%u-%u.ddc",
2218 le16_to_cpu(ver.hw_variant),
2219 le16_to_cpu(params->dev_revid));
2221 kfree_skb(skb);
2223 if (fw->size < 644) {
2224 BT_ERR("%s: Invalid size of firmware file (%zu)",
2225 hdev->name, fw->size);
2226 err = -EBADF;
2227 goto done;
2230 set_bit(BTUSB_DOWNLOADING, &data->flags);
2232 /* Start the firmware download transaction with the Init fragment
2233 * represented by the 128 bytes of CSS header.
2235 err = btintel_secure_send(hdev, 0x00, 128, fw->data);
2236 if (err < 0) {
2237 BT_ERR("%s: Failed to send firmware header (%d)",
2238 hdev->name, err);
2239 goto done;
2242 /* Send the 256 bytes of public key information from the firmware
2243 * as the PKey fragment.
2245 err = btintel_secure_send(hdev, 0x03, 256, fw->data + 128);
2246 if (err < 0) {
2247 BT_ERR("%s: Failed to send firmware public key (%d)",
2248 hdev->name, err);
2249 goto done;
2252 /* Send the 256 bytes of signature information from the firmware
2253 * as the Sign fragment.
2255 err = btintel_secure_send(hdev, 0x02, 256, fw->data + 388);
2256 if (err < 0) {
2257 BT_ERR("%s: Failed to send firmware signature (%d)",
2258 hdev->name, err);
2259 goto done;
2262 fw_ptr = fw->data + 644;
2263 frag_len = 0;
2265 while (fw_ptr - fw->data < fw->size) {
2266 struct hci_command_hdr *cmd = (void *)(fw_ptr + frag_len);
2268 frag_len += sizeof(*cmd) + cmd->plen;
2270 /* The parameter length of the secure send command requires
2271 * a 4 byte alignment. It happens so that the firmware file
2272 * contains proper Intel_NOP commands to align the fragments
2273 * as needed.
2275 * Send set of commands with 4 byte alignment from the
2276 * firmware data buffer as a single Data fragement.
2278 if (!(frag_len % 4)) {
2279 err = btintel_secure_send(hdev, 0x01, frag_len, fw_ptr);
2280 if (err < 0) {
2281 BT_ERR("%s: Failed to send firmware data (%d)",
2282 hdev->name, err);
2283 goto done;
2286 fw_ptr += frag_len;
2287 frag_len = 0;
2291 set_bit(BTUSB_FIRMWARE_LOADED, &data->flags);
2293 BT_INFO("%s: Waiting for firmware download to complete", hdev->name);
2295 /* Before switching the device into operational mode and with that
2296 * booting the loaded firmware, wait for the bootloader notification
2297 * that all fragments have been successfully received.
2299 * When the event processing receives the notification, then the
2300 * BTUSB_DOWNLOADING flag will be cleared.
2302 * The firmware loading should not take longer than 5 seconds
2303 * and thus just timeout if that happens and fail the setup
2304 * of this device.
2306 err = wait_on_bit_timeout(&data->flags, BTUSB_DOWNLOADING,
2307 TASK_INTERRUPTIBLE,
2308 msecs_to_jiffies(5000));
2309 if (err == -EINTR) {
2310 BT_ERR("%s: Firmware loading interrupted", hdev->name);
2311 goto done;
2314 if (err) {
2315 BT_ERR("%s: Firmware loading timeout", hdev->name);
2316 err = -ETIMEDOUT;
2317 goto done;
2320 if (test_bit(BTUSB_FIRMWARE_FAILED, &data->flags)) {
2321 BT_ERR("%s: Firmware loading failed", hdev->name);
2322 err = -ENOEXEC;
2323 goto done;
2326 rettime = ktime_get();
2327 delta = ktime_sub(rettime, calltime);
2328 duration = (unsigned long long) ktime_to_ns(delta) >> 10;
2330 BT_INFO("%s: Firmware loaded in %llu usecs", hdev->name, duration);
2332 done:
2333 release_firmware(fw);
2335 if (err < 0)
2336 return err;
2338 calltime = ktime_get();
2340 set_bit(BTUSB_BOOTING, &data->flags);
2342 skb = __hci_cmd_sync(hdev, 0xfc01, sizeof(reset_param), reset_param,
2343 HCI_INIT_TIMEOUT);
2344 if (IS_ERR(skb))
2345 return PTR_ERR(skb);
2347 kfree_skb(skb);
2349 /* The bootloader will not indicate when the device is ready. This
2350 * is done by the operational firmware sending bootup notification.
2352 * Booting into operational firmware should not take longer than
2353 * 1 second. However if that happens, then just fail the setup
2354 * since something went wrong.
2356 BT_INFO("%s: Waiting for device to boot", hdev->name);
2358 err = wait_on_bit_timeout(&data->flags, BTUSB_BOOTING,
2359 TASK_INTERRUPTIBLE,
2360 msecs_to_jiffies(1000));
2362 if (err == -EINTR) {
2363 BT_ERR("%s: Device boot interrupted", hdev->name);
2364 return -EINTR;
2367 if (err) {
2368 BT_ERR("%s: Device boot timeout", hdev->name);
2369 return -ETIMEDOUT;
2372 rettime = ktime_get();
2373 delta = ktime_sub(rettime, calltime);
2374 duration = (unsigned long long) ktime_to_ns(delta) >> 10;
2376 BT_INFO("%s: Device booted in %llu usecs", hdev->name, duration);
2378 clear_bit(BTUSB_BOOTLOADER, &data->flags);
2380 /* Once the device is running in operational mode, it needs to apply
2381 * the device configuration (DDC) parameters.
2383 * The device can work without DDC parameters, so even if it fails
2384 * to load the file, no need to fail the setup.
2386 btintel_load_ddc_config(hdev, fwname);
2388 /* Set the event mask for Intel specific vendor events. This enables
2389 * a few extra events that are useful during general operation. It
2390 * does not enable any debugging related events.
2392 * The device will function correctly without these events enabled
2393 * and thus no need to fail the setup.
2395 btintel_set_event_mask(hdev, false);
2397 return 0;
2400 static int btusb_shutdown_intel(struct hci_dev *hdev)
2402 struct sk_buff *skb;
2403 long ret;
2405 /* Some platforms have an issue with BT LED when the interface is
2406 * down or BT radio is turned off, which takes 5 seconds to BT LED
2407 * goes off. This command turns off the BT LED immediately.
2409 skb = __hci_cmd_sync(hdev, 0xfc3f, 0, NULL, HCI_INIT_TIMEOUT);
2410 if (IS_ERR(skb)) {
2411 ret = PTR_ERR(skb);
2412 BT_ERR("%s: turning off Intel device LED failed (%ld)",
2413 hdev->name, ret);
2414 return ret;
2416 kfree_skb(skb);
2418 return 0;
2421 #ifdef CONFIG_PM
2422 /* Configure an out-of-band gpio as wake-up pin, if specified in device tree */
2423 static int marvell_config_oob_wake(struct hci_dev *hdev)
2425 struct sk_buff *skb;
2426 struct btusb_data *data = hci_get_drvdata(hdev);
2427 struct device *dev = &data->udev->dev;
2428 u16 pin, gap, opcode;
2429 int ret;
2430 u8 cmd[5];
2432 /* Move on if no wakeup pin specified */
2433 if (of_property_read_u16(dev->of_node, "marvell,wakeup-pin", &pin) ||
2434 of_property_read_u16(dev->of_node, "marvell,wakeup-gap-ms", &gap))
2435 return 0;
2437 /* Vendor specific command to configure a GPIO as wake-up pin */
2438 opcode = hci_opcode_pack(0x3F, 0x59);
2439 cmd[0] = opcode & 0xFF;
2440 cmd[1] = opcode >> 8;
2441 cmd[2] = 2; /* length of parameters that follow */
2442 cmd[3] = pin;
2443 cmd[4] = gap; /* time in ms, for which wakeup pin should be asserted */
2445 skb = bt_skb_alloc(sizeof(cmd), GFP_KERNEL);
2446 if (!skb) {
2447 bt_dev_err(hdev, "%s: No memory\n", __func__);
2448 return -ENOMEM;
2451 skb_put_data(skb, cmd, sizeof(cmd));
2452 hci_skb_pkt_type(skb) = HCI_COMMAND_PKT;
2454 ret = btusb_send_frame(hdev, skb);
2455 if (ret) {
2456 bt_dev_err(hdev, "%s: configuration failed\n", __func__);
2457 kfree_skb(skb);
2458 return ret;
2461 return 0;
2463 #endif
2465 static int btusb_set_bdaddr_marvell(struct hci_dev *hdev,
2466 const bdaddr_t *bdaddr)
2468 struct sk_buff *skb;
2469 u8 buf[8];
2470 long ret;
2472 buf[0] = 0xfe;
2473 buf[1] = sizeof(bdaddr_t);
2474 memcpy(buf + 2, bdaddr, sizeof(bdaddr_t));
2476 skb = __hci_cmd_sync(hdev, 0xfc22, sizeof(buf), buf, HCI_INIT_TIMEOUT);
2477 if (IS_ERR(skb)) {
2478 ret = PTR_ERR(skb);
2479 BT_ERR("%s: changing Marvell device address failed (%ld)",
2480 hdev->name, ret);
2481 return ret;
2483 kfree_skb(skb);
2485 return 0;
2488 static int btusb_set_bdaddr_ath3012(struct hci_dev *hdev,
2489 const bdaddr_t *bdaddr)
2491 struct sk_buff *skb;
2492 u8 buf[10];
2493 long ret;
2495 buf[0] = 0x01;
2496 buf[1] = 0x01;
2497 buf[2] = 0x00;
2498 buf[3] = sizeof(bdaddr_t);
2499 memcpy(buf + 4, bdaddr, sizeof(bdaddr_t));
2501 skb = __hci_cmd_sync(hdev, 0xfc0b, sizeof(buf), buf, HCI_INIT_TIMEOUT);
2502 if (IS_ERR(skb)) {
2503 ret = PTR_ERR(skb);
2504 BT_ERR("%s: Change address command failed (%ld)",
2505 hdev->name, ret);
2506 return ret;
2508 kfree_skb(skb);
2510 return 0;
2513 #define QCA_DFU_PACKET_LEN 4096
2515 #define QCA_GET_TARGET_VERSION 0x09
2516 #define QCA_CHECK_STATUS 0x05
2517 #define QCA_DFU_DOWNLOAD 0x01
2519 #define QCA_SYSCFG_UPDATED 0x40
2520 #define QCA_PATCH_UPDATED 0x80
2521 #define QCA_DFU_TIMEOUT 3000
2523 struct qca_version {
2524 __le32 rom_version;
2525 __le32 patch_version;
2526 __le32 ram_version;
2527 __le32 ref_clock;
2528 __u8 reserved[4];
2529 } __packed;
2531 struct qca_rampatch_version {
2532 __le16 rom_version;
2533 __le16 patch_version;
2534 } __packed;
2536 struct qca_device_info {
2537 u32 rom_version;
2538 u8 rampatch_hdr; /* length of header in rampatch */
2539 u8 nvm_hdr; /* length of header in NVM */
2540 u8 ver_offset; /* offset of version structure in rampatch */
2543 static const struct qca_device_info qca_devices_table[] = {
2544 { 0x00000100, 20, 4, 10 }, /* Rome 1.0 */
2545 { 0x00000101, 20, 4, 10 }, /* Rome 1.1 */
2546 { 0x00000200, 28, 4, 18 }, /* Rome 2.0 */
2547 { 0x00000201, 28, 4, 18 }, /* Rome 2.1 */
2548 { 0x00000300, 28, 4, 18 }, /* Rome 3.0 */
2549 { 0x00000302, 28, 4, 18 }, /* Rome 3.2 */
2552 static int btusb_qca_send_vendor_req(struct hci_dev *hdev, u8 request,
2553 void *data, u16 size)
2555 struct btusb_data *btdata = hci_get_drvdata(hdev);
2556 struct usb_device *udev = btdata->udev;
2557 int pipe, err;
2558 u8 *buf;
2560 buf = kmalloc(size, GFP_KERNEL);
2561 if (!buf)
2562 return -ENOMEM;
2564 /* Found some of USB hosts have IOT issues with ours so that we should
2565 * not wait until HCI layer is ready.
2567 pipe = usb_rcvctrlpipe(udev, 0);
2568 err = usb_control_msg(udev, pipe, request, USB_TYPE_VENDOR | USB_DIR_IN,
2569 0, 0, buf, size, USB_CTRL_SET_TIMEOUT);
2570 if (err < 0) {
2571 BT_ERR("%s: Failed to access otp area (%d)", hdev->name, err);
2572 goto done;
2575 memcpy(data, buf, size);
2577 done:
2578 kfree(buf);
2580 return err;
2583 static int btusb_setup_qca_download_fw(struct hci_dev *hdev,
2584 const struct firmware *firmware,
2585 size_t hdr_size)
2587 struct btusb_data *btdata = hci_get_drvdata(hdev);
2588 struct usb_device *udev = btdata->udev;
2589 size_t count, size, sent = 0;
2590 int pipe, len, err;
2591 u8 *buf;
2593 buf = kmalloc(QCA_DFU_PACKET_LEN, GFP_KERNEL);
2594 if (!buf)
2595 return -ENOMEM;
2597 count = firmware->size;
2599 size = min_t(size_t, count, hdr_size);
2600 memcpy(buf, firmware->data, size);
2602 /* USB patches should go down to controller through USB path
2603 * because binary format fits to go down through USB channel.
2604 * USB control path is for patching headers and USB bulk is for
2605 * patch body.
2607 pipe = usb_sndctrlpipe(udev, 0);
2608 err = usb_control_msg(udev, pipe, QCA_DFU_DOWNLOAD, USB_TYPE_VENDOR,
2609 0, 0, buf, size, USB_CTRL_SET_TIMEOUT);
2610 if (err < 0) {
2611 BT_ERR("%s: Failed to send headers (%d)", hdev->name, err);
2612 goto done;
2615 sent += size;
2616 count -= size;
2618 while (count) {
2619 size = min_t(size_t, count, QCA_DFU_PACKET_LEN);
2621 memcpy(buf, firmware->data + sent, size);
2623 pipe = usb_sndbulkpipe(udev, 0x02);
2624 err = usb_bulk_msg(udev, pipe, buf, size, &len,
2625 QCA_DFU_TIMEOUT);
2626 if (err < 0) {
2627 BT_ERR("%s: Failed to send body at %zd of %zd (%d)",
2628 hdev->name, sent, firmware->size, err);
2629 break;
2632 if (size != len) {
2633 BT_ERR("%s: Failed to get bulk buffer", hdev->name);
2634 err = -EILSEQ;
2635 break;
2638 sent += size;
2639 count -= size;
2642 done:
2643 kfree(buf);
2644 return err;
2647 static int btusb_setup_qca_load_rampatch(struct hci_dev *hdev,
2648 struct qca_version *ver,
2649 const struct qca_device_info *info)
2651 struct qca_rampatch_version *rver;
2652 const struct firmware *fw;
2653 u32 ver_rom, ver_patch;
2654 u16 rver_rom, rver_patch;
2655 char fwname[64];
2656 int err;
2658 ver_rom = le32_to_cpu(ver->rom_version);
2659 ver_patch = le32_to_cpu(ver->patch_version);
2661 snprintf(fwname, sizeof(fwname), "qca/rampatch_usb_%08x.bin", ver_rom);
2663 err = request_firmware(&fw, fwname, &hdev->dev);
2664 if (err) {
2665 BT_ERR("%s: failed to request rampatch file: %s (%d)",
2666 hdev->name, fwname, err);
2667 return err;
2670 BT_INFO("%s: using rampatch file: %s", hdev->name, fwname);
2672 rver = (struct qca_rampatch_version *)(fw->data + info->ver_offset);
2673 rver_rom = le16_to_cpu(rver->rom_version);
2674 rver_patch = le16_to_cpu(rver->patch_version);
2676 BT_INFO("%s: QCA: patch rome 0x%x build 0x%x, firmware rome 0x%x "
2677 "build 0x%x", hdev->name, rver_rom, rver_patch, ver_rom,
2678 ver_patch);
2680 if (rver_rom != ver_rom || rver_patch <= ver_patch) {
2681 BT_ERR("%s: rampatch file version did not match with firmware",
2682 hdev->name);
2683 err = -EINVAL;
2684 goto done;
2687 err = btusb_setup_qca_download_fw(hdev, fw, info->rampatch_hdr);
2689 done:
2690 release_firmware(fw);
2692 return err;
2695 static int btusb_setup_qca_load_nvm(struct hci_dev *hdev,
2696 struct qca_version *ver,
2697 const struct qca_device_info *info)
2699 const struct firmware *fw;
2700 char fwname[64];
2701 int err;
2703 snprintf(fwname, sizeof(fwname), "qca/nvm_usb_%08x.bin",
2704 le32_to_cpu(ver->rom_version));
2706 err = request_firmware(&fw, fwname, &hdev->dev);
2707 if (err) {
2708 BT_ERR("%s: failed to request NVM file: %s (%d)",
2709 hdev->name, fwname, err);
2710 return err;
2713 BT_INFO("%s: using NVM file: %s", hdev->name, fwname);
2715 err = btusb_setup_qca_download_fw(hdev, fw, info->nvm_hdr);
2717 release_firmware(fw);
2719 return err;
2722 static int btusb_setup_qca(struct hci_dev *hdev)
2724 const struct qca_device_info *info = NULL;
2725 struct qca_version ver;
2726 u32 ver_rom;
2727 u8 status;
2728 int i, err;
2730 err = btusb_qca_send_vendor_req(hdev, QCA_GET_TARGET_VERSION, &ver,
2731 sizeof(ver));
2732 if (err < 0)
2733 return err;
2735 ver_rom = le32_to_cpu(ver.rom_version);
2736 for (i = 0; i < ARRAY_SIZE(qca_devices_table); i++) {
2737 if (ver_rom == qca_devices_table[i].rom_version)
2738 info = &qca_devices_table[i];
2740 if (!info) {
2741 BT_ERR("%s: don't support firmware rome 0x%x", hdev->name,
2742 ver_rom);
2743 return -ENODEV;
2746 err = btusb_qca_send_vendor_req(hdev, QCA_CHECK_STATUS, &status,
2747 sizeof(status));
2748 if (err < 0)
2749 return err;
2751 if (!(status & QCA_PATCH_UPDATED)) {
2752 err = btusb_setup_qca_load_rampatch(hdev, &ver, info);
2753 if (err < 0)
2754 return err;
2757 if (!(status & QCA_SYSCFG_UPDATED)) {
2758 err = btusb_setup_qca_load_nvm(hdev, &ver, info);
2759 if (err < 0)
2760 return err;
2763 return 0;
2766 #ifdef CONFIG_BT_HCIBTUSB_BCM
2767 static inline int __set_diag_interface(struct hci_dev *hdev)
2769 struct btusb_data *data = hci_get_drvdata(hdev);
2770 struct usb_interface *intf = data->diag;
2771 int i;
2773 if (!data->diag)
2774 return -ENODEV;
2776 data->diag_tx_ep = NULL;
2777 data->diag_rx_ep = NULL;
2779 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
2780 struct usb_endpoint_descriptor *ep_desc;
2782 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
2784 if (!data->diag_tx_ep && usb_endpoint_is_bulk_out(ep_desc)) {
2785 data->diag_tx_ep = ep_desc;
2786 continue;
2789 if (!data->diag_rx_ep && usb_endpoint_is_bulk_in(ep_desc)) {
2790 data->diag_rx_ep = ep_desc;
2791 continue;
2795 if (!data->diag_tx_ep || !data->diag_rx_ep) {
2796 BT_ERR("%s invalid diagnostic descriptors", hdev->name);
2797 return -ENODEV;
2800 return 0;
2803 static struct urb *alloc_diag_urb(struct hci_dev *hdev, bool enable)
2805 struct btusb_data *data = hci_get_drvdata(hdev);
2806 struct sk_buff *skb;
2807 struct urb *urb;
2808 unsigned int pipe;
2810 if (!data->diag_tx_ep)
2811 return ERR_PTR(-ENODEV);
2813 urb = usb_alloc_urb(0, GFP_KERNEL);
2814 if (!urb)
2815 return ERR_PTR(-ENOMEM);
2817 skb = bt_skb_alloc(2, GFP_KERNEL);
2818 if (!skb) {
2819 usb_free_urb(urb);
2820 return ERR_PTR(-ENOMEM);
2823 skb_put_u8(skb, 0xf0);
2824 skb_put_u8(skb, enable);
2826 pipe = usb_sndbulkpipe(data->udev, data->diag_tx_ep->bEndpointAddress);
2828 usb_fill_bulk_urb(urb, data->udev, pipe,
2829 skb->data, skb->len, btusb_tx_complete, skb);
2831 skb->dev = (void *)hdev;
2833 return urb;
2836 static int btusb_bcm_set_diag(struct hci_dev *hdev, bool enable)
2838 struct btusb_data *data = hci_get_drvdata(hdev);
2839 struct urb *urb;
2841 if (!data->diag)
2842 return -ENODEV;
2844 if (!test_bit(HCI_RUNNING, &hdev->flags))
2845 return -ENETDOWN;
2847 urb = alloc_diag_urb(hdev, enable);
2848 if (IS_ERR(urb))
2849 return PTR_ERR(urb);
2851 return submit_or_queue_tx_urb(hdev, urb);
2853 #endif
2855 #ifdef CONFIG_PM
2856 static irqreturn_t btusb_oob_wake_handler(int irq, void *priv)
2858 struct btusb_data *data = priv;
2860 pm_wakeup_event(&data->udev->dev, 0);
2861 pm_system_wakeup();
2863 /* Disable only if not already disabled (keep it balanced) */
2864 if (test_and_clear_bit(BTUSB_OOB_WAKE_ENABLED, &data->flags)) {
2865 disable_irq_nosync(irq);
2866 disable_irq_wake(irq);
2868 return IRQ_HANDLED;
2871 static const struct of_device_id btusb_match_table[] = {
2872 { .compatible = "usb1286,204e" },
2875 MODULE_DEVICE_TABLE(of, btusb_match_table);
2877 /* Use an oob wakeup pin? */
2878 static int btusb_config_oob_wake(struct hci_dev *hdev)
2880 struct btusb_data *data = hci_get_drvdata(hdev);
2881 struct device *dev = &data->udev->dev;
2882 int irq, ret;
2884 clear_bit(BTUSB_OOB_WAKE_ENABLED, &data->flags);
2886 if (!of_match_device(btusb_match_table, dev))
2887 return 0;
2889 /* Move on if no IRQ specified */
2890 irq = of_irq_get_byname(dev->of_node, "wakeup");
2891 if (irq <= 0) {
2892 bt_dev_dbg(hdev, "%s: no OOB Wakeup IRQ in DT", __func__);
2893 return 0;
2896 ret = devm_request_irq(&hdev->dev, irq, btusb_oob_wake_handler,
2897 0, "OOB Wake-on-BT", data);
2898 if (ret) {
2899 bt_dev_err(hdev, "%s: IRQ request failed", __func__);
2900 return ret;
2903 ret = device_init_wakeup(dev, true);
2904 if (ret) {
2905 bt_dev_err(hdev, "%s: failed to init_wakeup", __func__);
2906 return ret;
2909 data->oob_wake_irq = irq;
2910 disable_irq(irq);
2911 bt_dev_info(hdev, "OOB Wake-on-BT configured at IRQ %u", irq);
2912 return 0;
2914 #endif
2916 static void btusb_check_needs_reset_resume(struct usb_interface *intf)
2918 if (dmi_check_system(btusb_needs_reset_resume_table))
2919 interface_to_usbdev(intf)->quirks |= USB_QUIRK_RESET_RESUME;
2922 static int btusb_probe(struct usb_interface *intf,
2923 const struct usb_device_id *id)
2925 struct usb_endpoint_descriptor *ep_desc;
2926 struct btusb_data *data;
2927 struct hci_dev *hdev;
2928 unsigned ifnum_base;
2929 int i, err;
2931 BT_DBG("intf %p id %p", intf, id);
2933 /* interface numbers are hardcoded in the spec */
2934 if (intf->cur_altsetting->desc.bInterfaceNumber != 0) {
2935 if (!(id->driver_info & BTUSB_IFNUM_2))
2936 return -ENODEV;
2937 if (intf->cur_altsetting->desc.bInterfaceNumber != 2)
2938 return -ENODEV;
2941 ifnum_base = intf->cur_altsetting->desc.bInterfaceNumber;
2943 if (!id->driver_info) {
2944 const struct usb_device_id *match;
2946 match = usb_match_id(intf, blacklist_table);
2947 if (match)
2948 id = match;
2951 if (id->driver_info == BTUSB_IGNORE)
2952 return -ENODEV;
2954 if (id->driver_info & BTUSB_BCM_NO_PRODID) {
2955 struct usb_device *udev = interface_to_usbdev(intf);
2957 /* For the broken Broadcom devices that show 0000:0000
2958 * as USB vendor and product information, check that the
2959 * manufacturer string identifies them as Broadcom based
2960 * devices.
2962 if (!udev->manufacturer ||
2963 strcmp(udev->manufacturer, "Broadcom Corp"))
2964 return -ENODEV;
2967 if (id->driver_info & BTUSB_ATH3012) {
2968 struct usb_device *udev = interface_to_usbdev(intf);
2970 /* Old firmware would otherwise let ath3k driver load
2971 * patch and sysconfig files
2973 if (le16_to_cpu(udev->descriptor.bcdDevice) <= 0x0001)
2974 return -ENODEV;
2977 data = devm_kzalloc(&intf->dev, sizeof(*data), GFP_KERNEL);
2978 if (!data)
2979 return -ENOMEM;
2981 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
2982 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
2984 if (!data->intr_ep && usb_endpoint_is_int_in(ep_desc)) {
2985 data->intr_ep = ep_desc;
2986 continue;
2989 if (!data->bulk_tx_ep && usb_endpoint_is_bulk_out(ep_desc)) {
2990 data->bulk_tx_ep = ep_desc;
2991 continue;
2994 if (!data->bulk_rx_ep && usb_endpoint_is_bulk_in(ep_desc)) {
2995 data->bulk_rx_ep = ep_desc;
2996 continue;
3000 if (!data->intr_ep || !data->bulk_tx_ep || !data->bulk_rx_ep)
3001 return -ENODEV;
3003 if (id->driver_info & BTUSB_AMP) {
3004 data->cmdreq_type = USB_TYPE_CLASS | 0x01;
3005 data->cmdreq = 0x2b;
3006 } else {
3007 data->cmdreq_type = USB_TYPE_CLASS;
3008 data->cmdreq = 0x00;
3011 data->udev = interface_to_usbdev(intf);
3012 data->intf = intf;
3014 INIT_WORK(&data->work, btusb_work);
3015 INIT_WORK(&data->waker, btusb_waker);
3016 init_usb_anchor(&data->deferred);
3017 init_usb_anchor(&data->tx_anchor);
3018 spin_lock_init(&data->txlock);
3020 init_usb_anchor(&data->intr_anchor);
3021 init_usb_anchor(&data->bulk_anchor);
3022 init_usb_anchor(&data->isoc_anchor);
3023 init_usb_anchor(&data->diag_anchor);
3024 spin_lock_init(&data->rxlock);
3026 if (id->driver_info & BTUSB_INTEL_NEW) {
3027 data->recv_event = btusb_recv_event_intel;
3028 data->recv_bulk = btusb_recv_bulk_intel;
3029 set_bit(BTUSB_BOOTLOADER, &data->flags);
3030 } else {
3031 data->recv_event = hci_recv_frame;
3032 data->recv_bulk = btusb_recv_bulk;
3035 hdev = hci_alloc_dev();
3036 if (!hdev)
3037 return -ENOMEM;
3039 hdev->bus = HCI_USB;
3040 hci_set_drvdata(hdev, data);
3042 if (id->driver_info & BTUSB_AMP)
3043 hdev->dev_type = HCI_AMP;
3044 else
3045 hdev->dev_type = HCI_PRIMARY;
3047 data->hdev = hdev;
3049 SET_HCIDEV_DEV(hdev, &intf->dev);
3051 hdev->open = btusb_open;
3052 hdev->close = btusb_close;
3053 hdev->flush = btusb_flush;
3054 hdev->send = btusb_send_frame;
3055 hdev->notify = btusb_notify;
3057 #ifdef CONFIG_PM
3058 err = btusb_config_oob_wake(hdev);
3059 if (err)
3060 goto out_free_dev;
3062 /* Marvell devices may need a specific chip configuration */
3063 if (id->driver_info & BTUSB_MARVELL && data->oob_wake_irq) {
3064 err = marvell_config_oob_wake(hdev);
3065 if (err)
3066 goto out_free_dev;
3068 #endif
3069 if (id->driver_info & BTUSB_CW6622)
3070 set_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks);
3072 if (id->driver_info & BTUSB_BCM2045)
3073 set_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks);
3075 if (id->driver_info & BTUSB_BCM92035)
3076 hdev->setup = btusb_setup_bcm92035;
3078 #ifdef CONFIG_BT_HCIBTUSB_BCM
3079 if (id->driver_info & BTUSB_BCM_PATCHRAM) {
3080 hdev->manufacturer = 15;
3081 hdev->setup = btbcm_setup_patchram;
3082 hdev->set_diag = btusb_bcm_set_diag;
3083 hdev->set_bdaddr = btbcm_set_bdaddr;
3085 /* Broadcom LM_DIAG Interface numbers are hardcoded */
3086 data->diag = usb_ifnum_to_if(data->udev, ifnum_base + 2);
3089 if (id->driver_info & BTUSB_BCM_APPLE) {
3090 hdev->manufacturer = 15;
3091 hdev->setup = btbcm_setup_apple;
3092 hdev->set_diag = btusb_bcm_set_diag;
3094 /* Broadcom LM_DIAG Interface numbers are hardcoded */
3095 data->diag = usb_ifnum_to_if(data->udev, ifnum_base + 2);
3097 #endif
3099 if (id->driver_info & BTUSB_INTEL) {
3100 hdev->manufacturer = 2;
3101 hdev->setup = btusb_setup_intel;
3102 hdev->shutdown = btusb_shutdown_intel;
3103 hdev->set_diag = btintel_set_diag_mfg;
3104 hdev->set_bdaddr = btintel_set_bdaddr;
3105 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
3106 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
3107 set_bit(HCI_QUIRK_NON_PERSISTENT_DIAG, &hdev->quirks);
3110 if (id->driver_info & BTUSB_INTEL_NEW) {
3111 hdev->manufacturer = 2;
3112 hdev->send = btusb_send_frame_intel;
3113 hdev->setup = btusb_setup_intel_new;
3114 hdev->hw_error = btintel_hw_error;
3115 hdev->set_diag = btintel_set_diag;
3116 hdev->set_bdaddr = btintel_set_bdaddr;
3117 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
3118 set_bit(HCI_QUIRK_NON_PERSISTENT_DIAG, &hdev->quirks);
3121 if (id->driver_info & BTUSB_MARVELL)
3122 hdev->set_bdaddr = btusb_set_bdaddr_marvell;
3124 if (id->driver_info & BTUSB_SWAVE) {
3125 set_bit(HCI_QUIRK_FIXUP_INQUIRY_MODE, &hdev->quirks);
3126 set_bit(HCI_QUIRK_BROKEN_LOCAL_COMMANDS, &hdev->quirks);
3129 if (id->driver_info & BTUSB_INTEL_BOOT) {
3130 hdev->manufacturer = 2;
3131 set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
3134 if (id->driver_info & BTUSB_ATH3012) {
3135 hdev->set_bdaddr = btusb_set_bdaddr_ath3012;
3136 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
3137 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
3140 if (id->driver_info & BTUSB_QCA_ROME) {
3141 data->setup_on_usb = btusb_setup_qca;
3142 hdev->set_bdaddr = btusb_set_bdaddr_ath3012;
3143 btusb_check_needs_reset_resume(intf);
3146 #ifdef CONFIG_BT_HCIBTUSB_RTL
3147 if (id->driver_info & BTUSB_REALTEK) {
3148 hdev->setup = btrtl_setup_realtek;
3150 /* Realtek devices lose their updated firmware over suspend,
3151 * but the USB hub doesn't notice any status change.
3152 * Explicitly request a device reset on resume.
3154 interface_to_usbdev(intf)->quirks |= USB_QUIRK_RESET_RESUME;
3156 #endif
3158 if (id->driver_info & BTUSB_AMP) {
3159 /* AMP controllers do not support SCO packets */
3160 data->isoc = NULL;
3161 } else {
3162 /* Interface orders are hardcoded in the specification */
3163 data->isoc = usb_ifnum_to_if(data->udev, ifnum_base + 1);
3166 if (!reset)
3167 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
3169 if (force_scofix || id->driver_info & BTUSB_WRONG_SCO_MTU) {
3170 if (!disable_scofix)
3171 set_bit(HCI_QUIRK_FIXUP_BUFFER_SIZE, &hdev->quirks);
3174 if (id->driver_info & BTUSB_BROKEN_ISOC)
3175 data->isoc = NULL;
3177 if (id->driver_info & BTUSB_DIGIANSWER) {
3178 data->cmdreq_type = USB_TYPE_VENDOR;
3179 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
3182 if (id->driver_info & BTUSB_CSR) {
3183 struct usb_device *udev = data->udev;
3184 u16 bcdDevice = le16_to_cpu(udev->descriptor.bcdDevice);
3186 /* Old firmware would otherwise execute USB reset */
3187 if (bcdDevice < 0x117)
3188 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
3190 /* Fake CSR devices with broken commands */
3191 if (bcdDevice <= 0x100 || bcdDevice == 0x134)
3192 hdev->setup = btusb_setup_csr;
3194 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
3197 if (id->driver_info & BTUSB_SNIFFER) {
3198 struct usb_device *udev = data->udev;
3200 /* New sniffer firmware has crippled HCI interface */
3201 if (le16_to_cpu(udev->descriptor.bcdDevice) > 0x997)
3202 set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
3205 if (id->driver_info & BTUSB_INTEL_BOOT) {
3206 /* A bug in the bootloader causes that interrupt interface is
3207 * only enabled after receiving SetInterface(0, AltSetting=0).
3209 err = usb_set_interface(data->udev, 0, 0);
3210 if (err < 0) {
3211 BT_ERR("failed to set interface 0, alt 0 %d", err);
3212 goto out_free_dev;
3216 if (data->isoc) {
3217 err = usb_driver_claim_interface(&btusb_driver,
3218 data->isoc, data);
3219 if (err < 0)
3220 goto out_free_dev;
3223 #ifdef CONFIG_BT_HCIBTUSB_BCM
3224 if (data->diag) {
3225 if (!usb_driver_claim_interface(&btusb_driver,
3226 data->diag, data))
3227 __set_diag_interface(hdev);
3228 else
3229 data->diag = NULL;
3231 #endif
3233 err = hci_register_dev(hdev);
3234 if (err < 0)
3235 goto out_free_dev;
3237 usb_set_intfdata(intf, data);
3239 return 0;
3241 out_free_dev:
3242 hci_free_dev(hdev);
3243 return err;
3246 static void btusb_disconnect(struct usb_interface *intf)
3248 struct btusb_data *data = usb_get_intfdata(intf);
3249 struct hci_dev *hdev;
3251 BT_DBG("intf %p", intf);
3253 if (!data)
3254 return;
3256 hdev = data->hdev;
3257 usb_set_intfdata(data->intf, NULL);
3259 if (data->isoc)
3260 usb_set_intfdata(data->isoc, NULL);
3262 if (data->diag)
3263 usb_set_intfdata(data->diag, NULL);
3265 hci_unregister_dev(hdev);
3267 if (intf == data->intf) {
3268 if (data->isoc)
3269 usb_driver_release_interface(&btusb_driver, data->isoc);
3270 if (data->diag)
3271 usb_driver_release_interface(&btusb_driver, data->diag);
3272 } else if (intf == data->isoc) {
3273 if (data->diag)
3274 usb_driver_release_interface(&btusb_driver, data->diag);
3275 usb_driver_release_interface(&btusb_driver, data->intf);
3276 } else if (intf == data->diag) {
3277 usb_driver_release_interface(&btusb_driver, data->intf);
3278 if (data->isoc)
3279 usb_driver_release_interface(&btusb_driver, data->isoc);
3282 if (data->oob_wake_irq)
3283 device_init_wakeup(&data->udev->dev, false);
3285 hci_free_dev(hdev);
3288 #ifdef CONFIG_PM
3289 static int btusb_suspend(struct usb_interface *intf, pm_message_t message)
3291 struct btusb_data *data = usb_get_intfdata(intf);
3293 BT_DBG("intf %p", intf);
3295 if (data->suspend_count++)
3296 return 0;
3298 spin_lock_irq(&data->txlock);
3299 if (!(PMSG_IS_AUTO(message) && data->tx_in_flight)) {
3300 set_bit(BTUSB_SUSPENDING, &data->flags);
3301 spin_unlock_irq(&data->txlock);
3302 } else {
3303 spin_unlock_irq(&data->txlock);
3304 data->suspend_count--;
3305 return -EBUSY;
3308 cancel_work_sync(&data->work);
3310 btusb_stop_traffic(data);
3311 usb_kill_anchored_urbs(&data->tx_anchor);
3313 if (data->oob_wake_irq && device_may_wakeup(&data->udev->dev)) {
3314 set_bit(BTUSB_OOB_WAKE_ENABLED, &data->flags);
3315 enable_irq_wake(data->oob_wake_irq);
3316 enable_irq(data->oob_wake_irq);
3319 return 0;
3322 static void play_deferred(struct btusb_data *data)
3324 struct urb *urb;
3325 int err;
3327 while ((urb = usb_get_from_anchor(&data->deferred))) {
3328 usb_anchor_urb(urb, &data->tx_anchor);
3330 err = usb_submit_urb(urb, GFP_ATOMIC);
3331 if (err < 0) {
3332 if (err != -EPERM && err != -ENODEV)
3333 BT_ERR("%s urb %p submission failed (%d)",
3334 data->hdev->name, urb, -err);
3335 kfree(urb->setup_packet);
3336 usb_unanchor_urb(urb);
3337 usb_free_urb(urb);
3338 break;
3341 data->tx_in_flight++;
3342 usb_free_urb(urb);
3345 /* Cleanup the rest deferred urbs. */
3346 while ((urb = usb_get_from_anchor(&data->deferred))) {
3347 kfree(urb->setup_packet);
3348 usb_free_urb(urb);
3352 static int btusb_resume(struct usb_interface *intf)
3354 struct btusb_data *data = usb_get_intfdata(intf);
3355 struct hci_dev *hdev = data->hdev;
3356 int err = 0;
3358 BT_DBG("intf %p", intf);
3360 if (--data->suspend_count)
3361 return 0;
3363 /* Disable only if not already disabled (keep it balanced) */
3364 if (test_and_clear_bit(BTUSB_OOB_WAKE_ENABLED, &data->flags)) {
3365 disable_irq(data->oob_wake_irq);
3366 disable_irq_wake(data->oob_wake_irq);
3369 if (!test_bit(HCI_RUNNING, &hdev->flags))
3370 goto done;
3372 if (test_bit(BTUSB_INTR_RUNNING, &data->flags)) {
3373 err = btusb_submit_intr_urb(hdev, GFP_NOIO);
3374 if (err < 0) {
3375 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
3376 goto failed;
3380 if (test_bit(BTUSB_BULK_RUNNING, &data->flags)) {
3381 err = btusb_submit_bulk_urb(hdev, GFP_NOIO);
3382 if (err < 0) {
3383 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
3384 goto failed;
3387 btusb_submit_bulk_urb(hdev, GFP_NOIO);
3390 if (test_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
3391 if (btusb_submit_isoc_urb(hdev, GFP_NOIO) < 0)
3392 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
3393 else
3394 btusb_submit_isoc_urb(hdev, GFP_NOIO);
3397 spin_lock_irq(&data->txlock);
3398 play_deferred(data);
3399 clear_bit(BTUSB_SUSPENDING, &data->flags);
3400 spin_unlock_irq(&data->txlock);
3401 schedule_work(&data->work);
3403 return 0;
3405 failed:
3406 usb_scuttle_anchored_urbs(&data->deferred);
3407 done:
3408 spin_lock_irq(&data->txlock);
3409 clear_bit(BTUSB_SUSPENDING, &data->flags);
3410 spin_unlock_irq(&data->txlock);
3412 return err;
3414 #endif
3416 static struct usb_driver btusb_driver = {
3417 .name = "btusb",
3418 .probe = btusb_probe,
3419 .disconnect = btusb_disconnect,
3420 #ifdef CONFIG_PM
3421 .suspend = btusb_suspend,
3422 .resume = btusb_resume,
3423 #endif
3424 .id_table = btusb_table,
3425 .supports_autosuspend = 1,
3426 .disable_hub_initiated_lpm = 1,
3429 module_usb_driver(btusb_driver);
3431 module_param(disable_scofix, bool, 0644);
3432 MODULE_PARM_DESC(disable_scofix, "Disable fixup of wrong SCO buffer size");
3434 module_param(force_scofix, bool, 0644);
3435 MODULE_PARM_DESC(force_scofix, "Force fixup of wrong SCO buffers size");
3437 module_param(reset, bool, 0644);
3438 MODULE_PARM_DESC(reset, "Send HCI reset command on initialization");
3440 MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
3441 MODULE_DESCRIPTION("Generic Bluetooth USB driver ver " VERSION);
3442 MODULE_VERSION(VERSION);
3443 MODULE_LICENSE("GPL");