xref: /linux/drivers/bluetooth/btusb.c (revision 995231c820e3bd3633cb38bf4ea6f2541e1da331)
1 /*
2  *
3  *  Generic Bluetooth USB driver
4  *
5  *  Copyright (C) 2005-2008  Marcel Holtmann <marcel@holtmann.org>
6  *
7  *
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.
12  *
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.
17  *
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
21  *
22  */
23 
24 #include <linux/module.h>
25 #include <linux/usb.h>
26 #include <linux/firmware.h>
27 #include <linux/of_device.h>
28 #include <linux/of_irq.h>
29 #include <linux/suspend.h>
30 #include <asm/unaligned.h>
31 
32 #include <net/bluetooth/bluetooth.h>
33 #include <net/bluetooth/hci_core.h>
34 
35 #include "btintel.h"
36 #include "btbcm.h"
37 #include "btrtl.h"
38 
39 #define VERSION "0.8"
40 
41 static bool disable_scofix;
42 static bool force_scofix;
43 
44 static bool reset = true;
45 
46 static struct usb_driver btusb_driver;
47 
48 #define BTUSB_IGNORE		0x01
49 #define BTUSB_DIGIANSWER	0x02
50 #define BTUSB_CSR		0x04
51 #define BTUSB_SNIFFER		0x08
52 #define BTUSB_BCM92035		0x10
53 #define BTUSB_BROKEN_ISOC	0x20
54 #define BTUSB_WRONG_SCO_MTU	0x40
55 #define BTUSB_ATH3012		0x80
56 #define BTUSB_INTEL		0x100
57 #define BTUSB_INTEL_BOOT	0x200
58 #define BTUSB_BCM_PATCHRAM	0x400
59 #define BTUSB_MARVELL		0x800
60 #define BTUSB_SWAVE		0x1000
61 #define BTUSB_INTEL_NEW		0x2000
62 #define BTUSB_AMP		0x4000
63 #define BTUSB_QCA_ROME		0x8000
64 #define BTUSB_BCM_APPLE		0x10000
65 #define BTUSB_REALTEK		0x20000
66 #define BTUSB_BCM2045		0x40000
67 #define BTUSB_IFNUM_2		0x80000
68 #define BTUSB_CW6622		0x100000
69 
70 static const struct usb_device_id btusb_table[] = {
71 	/* Generic Bluetooth USB device */
72 	{ USB_DEVICE_INFO(0xe0, 0x01, 0x01) },
73 
74 	/* Generic Bluetooth AMP device */
75 	{ USB_DEVICE_INFO(0xe0, 0x01, 0x04), .driver_info = BTUSB_AMP },
76 
77 	/* Generic Bluetooth USB interface */
78 	{ USB_INTERFACE_INFO(0xe0, 0x01, 0x01) },
79 
80 	/* Apple-specific (Broadcom) devices */
81 	{ USB_VENDOR_AND_INTERFACE_INFO(0x05ac, 0xff, 0x01, 0x01),
82 	  .driver_info = BTUSB_BCM_APPLE | BTUSB_IFNUM_2 },
83 
84 	/* MediaTek MT76x0E */
85 	{ USB_DEVICE(0x0e8d, 0x763f) },
86 
87 	/* Broadcom SoftSailing reporting vendor specific */
88 	{ USB_DEVICE(0x0a5c, 0x21e1) },
89 
90 	/* Apple MacBookPro 7,1 */
91 	{ USB_DEVICE(0x05ac, 0x8213) },
92 
93 	/* Apple iMac11,1 */
94 	{ USB_DEVICE(0x05ac, 0x8215) },
95 
96 	/* Apple MacBookPro6,2 */
97 	{ USB_DEVICE(0x05ac, 0x8218) },
98 
99 	/* Apple MacBookAir3,1, MacBookAir3,2 */
100 	{ USB_DEVICE(0x05ac, 0x821b) },
101 
102 	/* Apple MacBookAir4,1 */
103 	{ USB_DEVICE(0x05ac, 0x821f) },
104 
105 	/* Apple MacBookPro8,2 */
106 	{ USB_DEVICE(0x05ac, 0x821a) },
107 
108 	/* Apple MacMini5,1 */
109 	{ USB_DEVICE(0x05ac, 0x8281) },
110 
111 	/* AVM BlueFRITZ! USB v2.0 */
112 	{ USB_DEVICE(0x057c, 0x3800), .driver_info = BTUSB_SWAVE },
113 
114 	/* Bluetooth Ultraport Module from IBM */
115 	{ USB_DEVICE(0x04bf, 0x030a) },
116 
117 	/* ALPS Modules with non-standard id */
118 	{ USB_DEVICE(0x044e, 0x3001) },
119 	{ USB_DEVICE(0x044e, 0x3002) },
120 
121 	/* Ericsson with non-standard id */
122 	{ USB_DEVICE(0x0bdb, 0x1002) },
123 
124 	/* Canyon CN-BTU1 with HID interfaces */
125 	{ USB_DEVICE(0x0c10, 0x0000) },
126 
127 	/* Broadcom BCM20702A0 */
128 	{ USB_DEVICE(0x413c, 0x8197) },
129 
130 	/* Broadcom BCM20702B0 (Dynex/Insignia) */
131 	{ USB_DEVICE(0x19ff, 0x0239), .driver_info = BTUSB_BCM_PATCHRAM },
132 
133 	/* Broadcom BCM43142A0 (Foxconn/Lenovo) */
134 	{ USB_VENDOR_AND_INTERFACE_INFO(0x105b, 0xff, 0x01, 0x01),
135 	  .driver_info = BTUSB_BCM_PATCHRAM },
136 
137 	/* Broadcom BCM920703 (HTC Vive) */
138 	{ USB_VENDOR_AND_INTERFACE_INFO(0x0bb4, 0xff, 0x01, 0x01),
139 	  .driver_info = BTUSB_BCM_PATCHRAM },
140 
141 	/* Foxconn - Hon Hai */
142 	{ USB_VENDOR_AND_INTERFACE_INFO(0x0489, 0xff, 0x01, 0x01),
143 	  .driver_info = BTUSB_BCM_PATCHRAM },
144 
145 	/* Lite-On Technology - Broadcom based */
146 	{ USB_VENDOR_AND_INTERFACE_INFO(0x04ca, 0xff, 0x01, 0x01),
147 	  .driver_info = BTUSB_BCM_PATCHRAM },
148 
149 	/* Broadcom devices with vendor specific id */
150 	{ USB_VENDOR_AND_INTERFACE_INFO(0x0a5c, 0xff, 0x01, 0x01),
151 	  .driver_info = BTUSB_BCM_PATCHRAM },
152 
153 	/* ASUSTek Computer - Broadcom based */
154 	{ USB_VENDOR_AND_INTERFACE_INFO(0x0b05, 0xff, 0x01, 0x01),
155 	  .driver_info = BTUSB_BCM_PATCHRAM },
156 
157 	/* Belkin F8065bf - Broadcom based */
158 	{ USB_VENDOR_AND_INTERFACE_INFO(0x050d, 0xff, 0x01, 0x01),
159 	  .driver_info = BTUSB_BCM_PATCHRAM },
160 
161 	/* IMC Networks - Broadcom based */
162 	{ USB_VENDOR_AND_INTERFACE_INFO(0x13d3, 0xff, 0x01, 0x01),
163 	  .driver_info = BTUSB_BCM_PATCHRAM },
164 
165 	/* Dell Computer - Broadcom based  */
166 	{ USB_VENDOR_AND_INTERFACE_INFO(0x413c, 0xff, 0x01, 0x01),
167 	  .driver_info = BTUSB_BCM_PATCHRAM },
168 
169 	/* Toshiba Corp - Broadcom based */
170 	{ USB_VENDOR_AND_INTERFACE_INFO(0x0930, 0xff, 0x01, 0x01),
171 	  .driver_info = BTUSB_BCM_PATCHRAM },
172 
173 	/* Intel Bluetooth USB Bootloader (RAM module) */
174 	{ USB_DEVICE(0x8087, 0x0a5a),
175 	  .driver_info = BTUSB_INTEL_BOOT | BTUSB_BROKEN_ISOC },
176 
177 	{ }	/* Terminating entry */
178 };
179 
180 MODULE_DEVICE_TABLE(usb, btusb_table);
181 
182 static const struct usb_device_id blacklist_table[] = {
183 	/* CSR BlueCore devices */
184 	{ USB_DEVICE(0x0a12, 0x0001), .driver_info = BTUSB_CSR },
185 
186 	/* Broadcom BCM2033 without firmware */
187 	{ USB_DEVICE(0x0a5c, 0x2033), .driver_info = BTUSB_IGNORE },
188 
189 	/* Broadcom BCM2045 devices */
190 	{ USB_DEVICE(0x0a5c, 0x2045), .driver_info = BTUSB_BCM2045 },
191 
192 	/* Atheros 3011 with sflash firmware */
193 	{ USB_DEVICE(0x0489, 0xe027), .driver_info = BTUSB_IGNORE },
194 	{ USB_DEVICE(0x0489, 0xe03d), .driver_info = BTUSB_IGNORE },
195 	{ USB_DEVICE(0x04f2, 0xaff1), .driver_info = BTUSB_IGNORE },
196 	{ USB_DEVICE(0x0930, 0x0215), .driver_info = BTUSB_IGNORE },
197 	{ USB_DEVICE(0x0cf3, 0x3002), .driver_info = BTUSB_IGNORE },
198 	{ USB_DEVICE(0x0cf3, 0xe019), .driver_info = BTUSB_IGNORE },
199 	{ USB_DEVICE(0x13d3, 0x3304), .driver_info = BTUSB_IGNORE },
200 
201 	/* Atheros AR9285 Malbec with sflash firmware */
202 	{ USB_DEVICE(0x03f0, 0x311d), .driver_info = BTUSB_IGNORE },
203 
204 	/* Atheros 3012 with sflash firmware */
205 	{ USB_DEVICE(0x0489, 0xe04d), .driver_info = BTUSB_ATH3012 },
206 	{ USB_DEVICE(0x0489, 0xe04e), .driver_info = BTUSB_ATH3012 },
207 	{ USB_DEVICE(0x0489, 0xe056), .driver_info = BTUSB_ATH3012 },
208 	{ USB_DEVICE(0x0489, 0xe057), .driver_info = BTUSB_ATH3012 },
209 	{ USB_DEVICE(0x0489, 0xe05f), .driver_info = BTUSB_ATH3012 },
210 	{ USB_DEVICE(0x0489, 0xe076), .driver_info = BTUSB_ATH3012 },
211 	{ USB_DEVICE(0x0489, 0xe078), .driver_info = BTUSB_ATH3012 },
212 	{ USB_DEVICE(0x0489, 0xe095), .driver_info = BTUSB_ATH3012 },
213 	{ USB_DEVICE(0x04c5, 0x1330), .driver_info = BTUSB_ATH3012 },
214 	{ USB_DEVICE(0x04ca, 0x3004), .driver_info = BTUSB_ATH3012 },
215 	{ USB_DEVICE(0x04ca, 0x3005), .driver_info = BTUSB_ATH3012 },
216 	{ USB_DEVICE(0x04ca, 0x3006), .driver_info = BTUSB_ATH3012 },
217 	{ USB_DEVICE(0x04ca, 0x3007), .driver_info = BTUSB_ATH3012 },
218 	{ USB_DEVICE(0x04ca, 0x3008), .driver_info = BTUSB_ATH3012 },
219 	{ USB_DEVICE(0x04ca, 0x300b), .driver_info = BTUSB_ATH3012 },
220 	{ USB_DEVICE(0x04ca, 0x300d), .driver_info = BTUSB_ATH3012 },
221 	{ USB_DEVICE(0x04ca, 0x300f), .driver_info = BTUSB_ATH3012 },
222 	{ USB_DEVICE(0x04ca, 0x3010), .driver_info = BTUSB_ATH3012 },
223 	{ USB_DEVICE(0x04ca, 0x3014), .driver_info = BTUSB_ATH3012 },
224 	{ USB_DEVICE(0x04ca, 0x3018), .driver_info = BTUSB_ATH3012 },
225 	{ USB_DEVICE(0x0930, 0x0219), .driver_info = BTUSB_ATH3012 },
226 	{ USB_DEVICE(0x0930, 0x021c), .driver_info = BTUSB_ATH3012 },
227 	{ USB_DEVICE(0x0930, 0x0220), .driver_info = BTUSB_ATH3012 },
228 	{ USB_DEVICE(0x0930, 0x0227), .driver_info = BTUSB_ATH3012 },
229 	{ USB_DEVICE(0x0b05, 0x17d0), .driver_info = BTUSB_ATH3012 },
230 	{ USB_DEVICE(0x0cf3, 0x0036), .driver_info = BTUSB_ATH3012 },
231 	{ USB_DEVICE(0x0cf3, 0x3004), .driver_info = BTUSB_ATH3012 },
232 	{ USB_DEVICE(0x0cf3, 0x3008), .driver_info = BTUSB_ATH3012 },
233 	{ USB_DEVICE(0x0cf3, 0x311d), .driver_info = BTUSB_ATH3012 },
234 	{ USB_DEVICE(0x0cf3, 0x311e), .driver_info = BTUSB_ATH3012 },
235 	{ USB_DEVICE(0x0cf3, 0x311f), .driver_info = BTUSB_ATH3012 },
236 	{ USB_DEVICE(0x0cf3, 0x3121), .driver_info = BTUSB_ATH3012 },
237 	{ USB_DEVICE(0x0cf3, 0x817a), .driver_info = BTUSB_ATH3012 },
238 	{ USB_DEVICE(0x0cf3, 0x817b), .driver_info = BTUSB_ATH3012 },
239 	{ USB_DEVICE(0x0cf3, 0xe003), .driver_info = BTUSB_ATH3012 },
240 	{ USB_DEVICE(0x0cf3, 0xe004), .driver_info = BTUSB_ATH3012 },
241 	{ USB_DEVICE(0x0cf3, 0xe005), .driver_info = BTUSB_ATH3012 },
242 	{ USB_DEVICE(0x0cf3, 0xe006), .driver_info = BTUSB_ATH3012 },
243 	{ USB_DEVICE(0x13d3, 0x3362), .driver_info = BTUSB_ATH3012 },
244 	{ USB_DEVICE(0x13d3, 0x3375), .driver_info = BTUSB_ATH3012 },
245 	{ USB_DEVICE(0x13d3, 0x3393), .driver_info = BTUSB_ATH3012 },
246 	{ USB_DEVICE(0x13d3, 0x3395), .driver_info = BTUSB_ATH3012 },
247 	{ USB_DEVICE(0x13d3, 0x3402), .driver_info = BTUSB_ATH3012 },
248 	{ USB_DEVICE(0x13d3, 0x3408), .driver_info = BTUSB_ATH3012 },
249 	{ USB_DEVICE(0x13d3, 0x3423), .driver_info = BTUSB_ATH3012 },
250 	{ USB_DEVICE(0x13d3, 0x3432), .driver_info = BTUSB_ATH3012 },
251 	{ USB_DEVICE(0x13d3, 0x3472), .driver_info = BTUSB_ATH3012 },
252 	{ USB_DEVICE(0x13d3, 0x3474), .driver_info = BTUSB_ATH3012 },
253 	{ USB_DEVICE(0x13d3, 0x3487), .driver_info = BTUSB_ATH3012 },
254 	{ USB_DEVICE(0x13d3, 0x3490), .driver_info = BTUSB_ATH3012 },
255 
256 	/* Atheros AR5BBU12 with sflash firmware */
257 	{ USB_DEVICE(0x0489, 0xe02c), .driver_info = BTUSB_IGNORE },
258 
259 	/* Atheros AR5BBU12 with sflash firmware */
260 	{ USB_DEVICE(0x0489, 0xe036), .driver_info = BTUSB_ATH3012 },
261 	{ USB_DEVICE(0x0489, 0xe03c), .driver_info = BTUSB_ATH3012 },
262 
263 	/* QCA ROME chipset */
264 	{ USB_DEVICE(0x0cf3, 0xe007), .driver_info = BTUSB_QCA_ROME },
265 	{ USB_DEVICE(0x0cf3, 0xe009), .driver_info = BTUSB_QCA_ROME },
266 	{ USB_DEVICE(0x0cf3, 0xe300), .driver_info = BTUSB_QCA_ROME },
267 	{ USB_DEVICE(0x0cf3, 0xe301), .driver_info = BTUSB_QCA_ROME },
268 	{ USB_DEVICE(0x0cf3, 0xe360), .driver_info = BTUSB_QCA_ROME },
269 	{ USB_DEVICE(0x0489, 0xe092), .driver_info = BTUSB_QCA_ROME },
270 	{ USB_DEVICE(0x0489, 0xe0a2), .driver_info = BTUSB_QCA_ROME },
271 	{ USB_DEVICE(0x04ca, 0x3011), .driver_info = BTUSB_QCA_ROME },
272 	{ USB_DEVICE(0x04ca, 0x3016), .driver_info = BTUSB_QCA_ROME },
273 
274 	/* Broadcom BCM2035 */
275 	{ USB_DEVICE(0x0a5c, 0x2009), .driver_info = BTUSB_BCM92035 },
276 	{ USB_DEVICE(0x0a5c, 0x200a), .driver_info = BTUSB_WRONG_SCO_MTU },
277 	{ USB_DEVICE(0x0a5c, 0x2035), .driver_info = BTUSB_WRONG_SCO_MTU },
278 
279 	/* Broadcom BCM2045 */
280 	{ USB_DEVICE(0x0a5c, 0x2039), .driver_info = BTUSB_WRONG_SCO_MTU },
281 	{ USB_DEVICE(0x0a5c, 0x2101), .driver_info = BTUSB_WRONG_SCO_MTU },
282 
283 	/* IBM/Lenovo ThinkPad with Broadcom chip */
284 	{ USB_DEVICE(0x0a5c, 0x201e), .driver_info = BTUSB_WRONG_SCO_MTU },
285 	{ USB_DEVICE(0x0a5c, 0x2110), .driver_info = BTUSB_WRONG_SCO_MTU },
286 
287 	/* HP laptop with Broadcom chip */
288 	{ USB_DEVICE(0x03f0, 0x171d), .driver_info = BTUSB_WRONG_SCO_MTU },
289 
290 	/* Dell laptop with Broadcom chip */
291 	{ USB_DEVICE(0x413c, 0x8126), .driver_info = BTUSB_WRONG_SCO_MTU },
292 
293 	/* Dell Wireless 370 and 410 devices */
294 	{ USB_DEVICE(0x413c, 0x8152), .driver_info = BTUSB_WRONG_SCO_MTU },
295 	{ USB_DEVICE(0x413c, 0x8156), .driver_info = BTUSB_WRONG_SCO_MTU },
296 
297 	/* Belkin F8T012 and F8T013 devices */
298 	{ USB_DEVICE(0x050d, 0x0012), .driver_info = BTUSB_WRONG_SCO_MTU },
299 	{ USB_DEVICE(0x050d, 0x0013), .driver_info = BTUSB_WRONG_SCO_MTU },
300 
301 	/* Asus WL-BTD202 device */
302 	{ USB_DEVICE(0x0b05, 0x1715), .driver_info = BTUSB_WRONG_SCO_MTU },
303 
304 	/* Kensington Bluetooth USB adapter */
305 	{ USB_DEVICE(0x047d, 0x105e), .driver_info = BTUSB_WRONG_SCO_MTU },
306 
307 	/* RTX Telecom based adapters with buggy SCO support */
308 	{ USB_DEVICE(0x0400, 0x0807), .driver_info = BTUSB_BROKEN_ISOC },
309 	{ USB_DEVICE(0x0400, 0x080a), .driver_info = BTUSB_BROKEN_ISOC },
310 
311 	/* CONWISE Technology based adapters with buggy SCO support */
312 	{ USB_DEVICE(0x0e5e, 0x6622),
313 	  .driver_info = BTUSB_BROKEN_ISOC | BTUSB_CW6622},
314 
315 	/* Roper Class 1 Bluetooth Dongle (Silicon Wave based) */
316 	{ USB_DEVICE(0x1310, 0x0001), .driver_info = BTUSB_SWAVE },
317 
318 	/* Digianswer devices */
319 	{ USB_DEVICE(0x08fd, 0x0001), .driver_info = BTUSB_DIGIANSWER },
320 	{ USB_DEVICE(0x08fd, 0x0002), .driver_info = BTUSB_IGNORE },
321 
322 	/* CSR BlueCore Bluetooth Sniffer */
323 	{ USB_DEVICE(0x0a12, 0x0002),
324 	  .driver_info = BTUSB_SNIFFER | BTUSB_BROKEN_ISOC },
325 
326 	/* Frontline ComProbe Bluetooth Sniffer */
327 	{ USB_DEVICE(0x16d3, 0x0002),
328 	  .driver_info = BTUSB_SNIFFER | BTUSB_BROKEN_ISOC },
329 
330 	/* Marvell Bluetooth devices */
331 	{ USB_DEVICE(0x1286, 0x2044), .driver_info = BTUSB_MARVELL },
332 	{ USB_DEVICE(0x1286, 0x2046), .driver_info = BTUSB_MARVELL },
333 	{ USB_DEVICE(0x1286, 0x204e), .driver_info = BTUSB_MARVELL },
334 
335 	/* Intel Bluetooth devices */
336 	{ USB_DEVICE(0x8087, 0x0025), .driver_info = BTUSB_INTEL_NEW },
337 	{ USB_DEVICE(0x8087, 0x07da), .driver_info = BTUSB_CSR },
338 	{ USB_DEVICE(0x8087, 0x07dc), .driver_info = BTUSB_INTEL },
339 	{ USB_DEVICE(0x8087, 0x0a2a), .driver_info = BTUSB_INTEL },
340 	{ USB_DEVICE(0x8087, 0x0a2b), .driver_info = BTUSB_INTEL_NEW },
341 	{ USB_DEVICE(0x8087, 0x0aa7), .driver_info = BTUSB_INTEL },
342 	{ USB_DEVICE(0x8087, 0x0aaa), .driver_info = BTUSB_INTEL_NEW },
343 
344 	/* Other Intel Bluetooth devices */
345 	{ USB_VENDOR_AND_INTERFACE_INFO(0x8087, 0xe0, 0x01, 0x01),
346 	  .driver_info = BTUSB_IGNORE },
347 
348 	/* Realtek Bluetooth devices */
349 	{ USB_VENDOR_AND_INTERFACE_INFO(0x0bda, 0xe0, 0x01, 0x01),
350 	  .driver_info = BTUSB_REALTEK },
351 
352 	/* Additional Realtek 8723AE Bluetooth devices */
353 	{ USB_DEVICE(0x0930, 0x021d), .driver_info = BTUSB_REALTEK },
354 	{ USB_DEVICE(0x13d3, 0x3394), .driver_info = BTUSB_REALTEK },
355 
356 	/* Additional Realtek 8723BE Bluetooth devices */
357 	{ USB_DEVICE(0x0489, 0xe085), .driver_info = BTUSB_REALTEK },
358 	{ USB_DEVICE(0x0489, 0xe08b), .driver_info = BTUSB_REALTEK },
359 	{ USB_DEVICE(0x13d3, 0x3410), .driver_info = BTUSB_REALTEK },
360 	{ USB_DEVICE(0x13d3, 0x3416), .driver_info = BTUSB_REALTEK },
361 	{ USB_DEVICE(0x13d3, 0x3459), .driver_info = BTUSB_REALTEK },
362 	{ USB_DEVICE(0x13d3, 0x3494), .driver_info = BTUSB_REALTEK },
363 
364 	/* Additional Realtek 8821AE Bluetooth devices */
365 	{ USB_DEVICE(0x0b05, 0x17dc), .driver_info = BTUSB_REALTEK },
366 	{ USB_DEVICE(0x13d3, 0x3414), .driver_info = BTUSB_REALTEK },
367 	{ USB_DEVICE(0x13d3, 0x3458), .driver_info = BTUSB_REALTEK },
368 	{ USB_DEVICE(0x13d3, 0x3461), .driver_info = BTUSB_REALTEK },
369 	{ USB_DEVICE(0x13d3, 0x3462), .driver_info = BTUSB_REALTEK },
370 
371 	/* Silicon Wave based devices */
372 	{ USB_DEVICE(0x0c10, 0x0000), .driver_info = BTUSB_SWAVE },
373 
374 	{ }	/* Terminating entry */
375 };
376 
377 #define BTUSB_MAX_ISOC_FRAMES	10
378 
379 #define BTUSB_INTR_RUNNING	0
380 #define BTUSB_BULK_RUNNING	1
381 #define BTUSB_ISOC_RUNNING	2
382 #define BTUSB_SUSPENDING	3
383 #define BTUSB_DID_ISO_RESUME	4
384 #define BTUSB_BOOTLOADER	5
385 #define BTUSB_DOWNLOADING	6
386 #define BTUSB_FIRMWARE_LOADED	7
387 #define BTUSB_FIRMWARE_FAILED	8
388 #define BTUSB_BOOTING		9
389 #define BTUSB_RESET_RESUME	10
390 #define BTUSB_DIAG_RUNNING	11
391 #define BTUSB_OOB_WAKE_ENABLED	12
392 
393 struct btusb_data {
394 	struct hci_dev       *hdev;
395 	struct usb_device    *udev;
396 	struct usb_interface *intf;
397 	struct usb_interface *isoc;
398 	struct usb_interface *diag;
399 
400 	unsigned long flags;
401 
402 	struct work_struct work;
403 	struct work_struct waker;
404 
405 	struct usb_anchor deferred;
406 	struct usb_anchor tx_anchor;
407 	int tx_in_flight;
408 	spinlock_t txlock;
409 
410 	struct usb_anchor intr_anchor;
411 	struct usb_anchor bulk_anchor;
412 	struct usb_anchor isoc_anchor;
413 	struct usb_anchor diag_anchor;
414 	spinlock_t rxlock;
415 
416 	struct sk_buff *evt_skb;
417 	struct sk_buff *acl_skb;
418 	struct sk_buff *sco_skb;
419 
420 	struct usb_endpoint_descriptor *intr_ep;
421 	struct usb_endpoint_descriptor *bulk_tx_ep;
422 	struct usb_endpoint_descriptor *bulk_rx_ep;
423 	struct usb_endpoint_descriptor *isoc_tx_ep;
424 	struct usb_endpoint_descriptor *isoc_rx_ep;
425 	struct usb_endpoint_descriptor *diag_tx_ep;
426 	struct usb_endpoint_descriptor *diag_rx_ep;
427 
428 	__u8 cmdreq_type;
429 	__u8 cmdreq;
430 
431 	unsigned int sco_num;
432 	int isoc_altsetting;
433 	int suspend_count;
434 
435 	int (*recv_event)(struct hci_dev *hdev, struct sk_buff *skb);
436 	int (*recv_bulk)(struct btusb_data *data, void *buffer, int count);
437 
438 	int (*setup_on_usb)(struct hci_dev *hdev);
439 
440 	int oob_wake_irq;   /* irq for out-of-band wake-on-bt */
441 };
442 
443 static inline void btusb_free_frags(struct btusb_data *data)
444 {
445 	unsigned long flags;
446 
447 	spin_lock_irqsave(&data->rxlock, flags);
448 
449 	kfree_skb(data->evt_skb);
450 	data->evt_skb = NULL;
451 
452 	kfree_skb(data->acl_skb);
453 	data->acl_skb = NULL;
454 
455 	kfree_skb(data->sco_skb);
456 	data->sco_skb = NULL;
457 
458 	spin_unlock_irqrestore(&data->rxlock, flags);
459 }
460 
461 static int btusb_recv_intr(struct btusb_data *data, void *buffer, int count)
462 {
463 	struct sk_buff *skb;
464 	int err = 0;
465 
466 	spin_lock(&data->rxlock);
467 	skb = data->evt_skb;
468 
469 	while (count) {
470 		int len;
471 
472 		if (!skb) {
473 			skb = bt_skb_alloc(HCI_MAX_EVENT_SIZE, GFP_ATOMIC);
474 			if (!skb) {
475 				err = -ENOMEM;
476 				break;
477 			}
478 
479 			hci_skb_pkt_type(skb) = HCI_EVENT_PKT;
480 			hci_skb_expect(skb) = HCI_EVENT_HDR_SIZE;
481 		}
482 
483 		len = min_t(uint, hci_skb_expect(skb), count);
484 		skb_put_data(skb, buffer, len);
485 
486 		count -= len;
487 		buffer += len;
488 		hci_skb_expect(skb) -= len;
489 
490 		if (skb->len == HCI_EVENT_HDR_SIZE) {
491 			/* Complete event header */
492 			hci_skb_expect(skb) = hci_event_hdr(skb)->plen;
493 
494 			if (skb_tailroom(skb) < hci_skb_expect(skb)) {
495 				kfree_skb(skb);
496 				skb = NULL;
497 
498 				err = -EILSEQ;
499 				break;
500 			}
501 		}
502 
503 		if (!hci_skb_expect(skb)) {
504 			/* Complete frame */
505 			data->recv_event(data->hdev, skb);
506 			skb = NULL;
507 		}
508 	}
509 
510 	data->evt_skb = skb;
511 	spin_unlock(&data->rxlock);
512 
513 	return err;
514 }
515 
516 static int btusb_recv_bulk(struct btusb_data *data, void *buffer, int count)
517 {
518 	struct sk_buff *skb;
519 	int err = 0;
520 
521 	spin_lock(&data->rxlock);
522 	skb = data->acl_skb;
523 
524 	while (count) {
525 		int len;
526 
527 		if (!skb) {
528 			skb = bt_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC);
529 			if (!skb) {
530 				err = -ENOMEM;
531 				break;
532 			}
533 
534 			hci_skb_pkt_type(skb) = HCI_ACLDATA_PKT;
535 			hci_skb_expect(skb) = HCI_ACL_HDR_SIZE;
536 		}
537 
538 		len = min_t(uint, hci_skb_expect(skb), count);
539 		skb_put_data(skb, buffer, len);
540 
541 		count -= len;
542 		buffer += len;
543 		hci_skb_expect(skb) -= len;
544 
545 		if (skb->len == HCI_ACL_HDR_SIZE) {
546 			__le16 dlen = hci_acl_hdr(skb)->dlen;
547 
548 			/* Complete ACL header */
549 			hci_skb_expect(skb) = __le16_to_cpu(dlen);
550 
551 			if (skb_tailroom(skb) < hci_skb_expect(skb)) {
552 				kfree_skb(skb);
553 				skb = NULL;
554 
555 				err = -EILSEQ;
556 				break;
557 			}
558 		}
559 
560 		if (!hci_skb_expect(skb)) {
561 			/* Complete frame */
562 			hci_recv_frame(data->hdev, skb);
563 			skb = NULL;
564 		}
565 	}
566 
567 	data->acl_skb = skb;
568 	spin_unlock(&data->rxlock);
569 
570 	return err;
571 }
572 
573 static int btusb_recv_isoc(struct btusb_data *data, void *buffer, int count)
574 {
575 	struct sk_buff *skb;
576 	int err = 0;
577 
578 	spin_lock(&data->rxlock);
579 	skb = data->sco_skb;
580 
581 	while (count) {
582 		int len;
583 
584 		if (!skb) {
585 			skb = bt_skb_alloc(HCI_MAX_SCO_SIZE, GFP_ATOMIC);
586 			if (!skb) {
587 				err = -ENOMEM;
588 				break;
589 			}
590 
591 			hci_skb_pkt_type(skb) = HCI_SCODATA_PKT;
592 			hci_skb_expect(skb) = HCI_SCO_HDR_SIZE;
593 		}
594 
595 		len = min_t(uint, hci_skb_expect(skb), count);
596 		skb_put_data(skb, buffer, len);
597 
598 		count -= len;
599 		buffer += len;
600 		hci_skb_expect(skb) -= len;
601 
602 		if (skb->len == HCI_SCO_HDR_SIZE) {
603 			/* Complete SCO header */
604 			hci_skb_expect(skb) = hci_sco_hdr(skb)->dlen;
605 
606 			if (skb_tailroom(skb) < hci_skb_expect(skb)) {
607 				kfree_skb(skb);
608 				skb = NULL;
609 
610 				err = -EILSEQ;
611 				break;
612 			}
613 		}
614 
615 		if (!hci_skb_expect(skb)) {
616 			/* Complete frame */
617 			hci_recv_frame(data->hdev, skb);
618 			skb = NULL;
619 		}
620 	}
621 
622 	data->sco_skb = skb;
623 	spin_unlock(&data->rxlock);
624 
625 	return err;
626 }
627 
628 static void btusb_intr_complete(struct urb *urb)
629 {
630 	struct hci_dev *hdev = urb->context;
631 	struct btusb_data *data = hci_get_drvdata(hdev);
632 	int err;
633 
634 	BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
635 	       urb->actual_length);
636 
637 	if (!test_bit(HCI_RUNNING, &hdev->flags))
638 		return;
639 
640 	if (urb->status == 0) {
641 		hdev->stat.byte_rx += urb->actual_length;
642 
643 		if (btusb_recv_intr(data, urb->transfer_buffer,
644 				    urb->actual_length) < 0) {
645 			BT_ERR("%s corrupted event packet", hdev->name);
646 			hdev->stat.err_rx++;
647 		}
648 	} else if (urb->status == -ENOENT) {
649 		/* Avoid suspend failed when usb_kill_urb */
650 		return;
651 	}
652 
653 	if (!test_bit(BTUSB_INTR_RUNNING, &data->flags))
654 		return;
655 
656 	usb_mark_last_busy(data->udev);
657 	usb_anchor_urb(urb, &data->intr_anchor);
658 
659 	err = usb_submit_urb(urb, GFP_ATOMIC);
660 	if (err < 0) {
661 		/* -EPERM: urb is being killed;
662 		 * -ENODEV: device got disconnected
663 		 */
664 		if (err != -EPERM && err != -ENODEV)
665 			BT_ERR("%s urb %p failed to resubmit (%d)",
666 			       hdev->name, urb, -err);
667 		usb_unanchor_urb(urb);
668 	}
669 }
670 
671 static int btusb_submit_intr_urb(struct hci_dev *hdev, gfp_t mem_flags)
672 {
673 	struct btusb_data *data = hci_get_drvdata(hdev);
674 	struct urb *urb;
675 	unsigned char *buf;
676 	unsigned int pipe;
677 	int err, size;
678 
679 	BT_DBG("%s", hdev->name);
680 
681 	if (!data->intr_ep)
682 		return -ENODEV;
683 
684 	urb = usb_alloc_urb(0, mem_flags);
685 	if (!urb)
686 		return -ENOMEM;
687 
688 	size = le16_to_cpu(data->intr_ep->wMaxPacketSize);
689 
690 	buf = kmalloc(size, mem_flags);
691 	if (!buf) {
692 		usb_free_urb(urb);
693 		return -ENOMEM;
694 	}
695 
696 	pipe = usb_rcvintpipe(data->udev, data->intr_ep->bEndpointAddress);
697 
698 	usb_fill_int_urb(urb, data->udev, pipe, buf, size,
699 			 btusb_intr_complete, hdev, data->intr_ep->bInterval);
700 
701 	urb->transfer_flags |= URB_FREE_BUFFER;
702 
703 	usb_anchor_urb(urb, &data->intr_anchor);
704 
705 	err = usb_submit_urb(urb, mem_flags);
706 	if (err < 0) {
707 		if (err != -EPERM && err != -ENODEV)
708 			BT_ERR("%s urb %p submission failed (%d)",
709 			       hdev->name, urb, -err);
710 		usb_unanchor_urb(urb);
711 	}
712 
713 	usb_free_urb(urb);
714 
715 	return err;
716 }
717 
718 static void btusb_bulk_complete(struct urb *urb)
719 {
720 	struct hci_dev *hdev = urb->context;
721 	struct btusb_data *data = hci_get_drvdata(hdev);
722 	int err;
723 
724 	BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
725 	       urb->actual_length);
726 
727 	if (!test_bit(HCI_RUNNING, &hdev->flags))
728 		return;
729 
730 	if (urb->status == 0) {
731 		hdev->stat.byte_rx += urb->actual_length;
732 
733 		if (data->recv_bulk(data, urb->transfer_buffer,
734 				    urb->actual_length) < 0) {
735 			BT_ERR("%s corrupted ACL packet", hdev->name);
736 			hdev->stat.err_rx++;
737 		}
738 	} else if (urb->status == -ENOENT) {
739 		/* Avoid suspend failed when usb_kill_urb */
740 		return;
741 	}
742 
743 	if (!test_bit(BTUSB_BULK_RUNNING, &data->flags))
744 		return;
745 
746 	usb_anchor_urb(urb, &data->bulk_anchor);
747 	usb_mark_last_busy(data->udev);
748 
749 	err = usb_submit_urb(urb, GFP_ATOMIC);
750 	if (err < 0) {
751 		/* -EPERM: urb is being killed;
752 		 * -ENODEV: device got disconnected
753 		 */
754 		if (err != -EPERM && err != -ENODEV)
755 			BT_ERR("%s urb %p failed to resubmit (%d)",
756 			       hdev->name, urb, -err);
757 		usb_unanchor_urb(urb);
758 	}
759 }
760 
761 static int btusb_submit_bulk_urb(struct hci_dev *hdev, gfp_t mem_flags)
762 {
763 	struct btusb_data *data = hci_get_drvdata(hdev);
764 	struct urb *urb;
765 	unsigned char *buf;
766 	unsigned int pipe;
767 	int err, size = HCI_MAX_FRAME_SIZE;
768 
769 	BT_DBG("%s", hdev->name);
770 
771 	if (!data->bulk_rx_ep)
772 		return -ENODEV;
773 
774 	urb = usb_alloc_urb(0, mem_flags);
775 	if (!urb)
776 		return -ENOMEM;
777 
778 	buf = kmalloc(size, mem_flags);
779 	if (!buf) {
780 		usb_free_urb(urb);
781 		return -ENOMEM;
782 	}
783 
784 	pipe = usb_rcvbulkpipe(data->udev, data->bulk_rx_ep->bEndpointAddress);
785 
786 	usb_fill_bulk_urb(urb, data->udev, pipe, buf, size,
787 			  btusb_bulk_complete, hdev);
788 
789 	urb->transfer_flags |= URB_FREE_BUFFER;
790 
791 	usb_mark_last_busy(data->udev);
792 	usb_anchor_urb(urb, &data->bulk_anchor);
793 
794 	err = usb_submit_urb(urb, mem_flags);
795 	if (err < 0) {
796 		if (err != -EPERM && err != -ENODEV)
797 			BT_ERR("%s urb %p submission failed (%d)",
798 			       hdev->name, urb, -err);
799 		usb_unanchor_urb(urb);
800 	}
801 
802 	usb_free_urb(urb);
803 
804 	return err;
805 }
806 
807 static void btusb_isoc_complete(struct urb *urb)
808 {
809 	struct hci_dev *hdev = urb->context;
810 	struct btusb_data *data = hci_get_drvdata(hdev);
811 	int i, err;
812 
813 	BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
814 	       urb->actual_length);
815 
816 	if (!test_bit(HCI_RUNNING, &hdev->flags))
817 		return;
818 
819 	if (urb->status == 0) {
820 		for (i = 0; i < urb->number_of_packets; i++) {
821 			unsigned int offset = urb->iso_frame_desc[i].offset;
822 			unsigned int length = urb->iso_frame_desc[i].actual_length;
823 
824 			if (urb->iso_frame_desc[i].status)
825 				continue;
826 
827 			hdev->stat.byte_rx += length;
828 
829 			if (btusb_recv_isoc(data, urb->transfer_buffer + offset,
830 					    length) < 0) {
831 				BT_ERR("%s corrupted SCO packet", hdev->name);
832 				hdev->stat.err_rx++;
833 			}
834 		}
835 	} else if (urb->status == -ENOENT) {
836 		/* Avoid suspend failed when usb_kill_urb */
837 		return;
838 	}
839 
840 	if (!test_bit(BTUSB_ISOC_RUNNING, &data->flags))
841 		return;
842 
843 	usb_anchor_urb(urb, &data->isoc_anchor);
844 
845 	err = usb_submit_urb(urb, GFP_ATOMIC);
846 	if (err < 0) {
847 		/* -EPERM: urb is being killed;
848 		 * -ENODEV: device got disconnected
849 		 */
850 		if (err != -EPERM && err != -ENODEV)
851 			BT_ERR("%s urb %p failed to resubmit (%d)",
852 			       hdev->name, urb, -err);
853 		usb_unanchor_urb(urb);
854 	}
855 }
856 
857 static inline void __fill_isoc_descriptor(struct urb *urb, int len, int mtu)
858 {
859 	int i, offset = 0;
860 
861 	BT_DBG("len %d mtu %d", len, mtu);
862 
863 	for (i = 0; i < BTUSB_MAX_ISOC_FRAMES && len >= mtu;
864 					i++, offset += mtu, len -= mtu) {
865 		urb->iso_frame_desc[i].offset = offset;
866 		urb->iso_frame_desc[i].length = mtu;
867 	}
868 
869 	if (len && i < BTUSB_MAX_ISOC_FRAMES) {
870 		urb->iso_frame_desc[i].offset = offset;
871 		urb->iso_frame_desc[i].length = len;
872 		i++;
873 	}
874 
875 	urb->number_of_packets = i;
876 }
877 
878 static int btusb_submit_isoc_urb(struct hci_dev *hdev, gfp_t mem_flags)
879 {
880 	struct btusb_data *data = hci_get_drvdata(hdev);
881 	struct urb *urb;
882 	unsigned char *buf;
883 	unsigned int pipe;
884 	int err, size;
885 
886 	BT_DBG("%s", hdev->name);
887 
888 	if (!data->isoc_rx_ep)
889 		return -ENODEV;
890 
891 	urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, mem_flags);
892 	if (!urb)
893 		return -ENOMEM;
894 
895 	size = le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize) *
896 						BTUSB_MAX_ISOC_FRAMES;
897 
898 	buf = kmalloc(size, mem_flags);
899 	if (!buf) {
900 		usb_free_urb(urb);
901 		return -ENOMEM;
902 	}
903 
904 	pipe = usb_rcvisocpipe(data->udev, data->isoc_rx_ep->bEndpointAddress);
905 
906 	usb_fill_int_urb(urb, data->udev, pipe, buf, size, btusb_isoc_complete,
907 			 hdev, data->isoc_rx_ep->bInterval);
908 
909 	urb->transfer_flags = URB_FREE_BUFFER | URB_ISO_ASAP;
910 
911 	__fill_isoc_descriptor(urb, size,
912 			       le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize));
913 
914 	usb_anchor_urb(urb, &data->isoc_anchor);
915 
916 	err = usb_submit_urb(urb, mem_flags);
917 	if (err < 0) {
918 		if (err != -EPERM && err != -ENODEV)
919 			BT_ERR("%s urb %p submission failed (%d)",
920 			       hdev->name, urb, -err);
921 		usb_unanchor_urb(urb);
922 	}
923 
924 	usb_free_urb(urb);
925 
926 	return err;
927 }
928 
929 static void btusb_diag_complete(struct urb *urb)
930 {
931 	struct hci_dev *hdev = urb->context;
932 	struct btusb_data *data = hci_get_drvdata(hdev);
933 	int err;
934 
935 	BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
936 	       urb->actual_length);
937 
938 	if (urb->status == 0) {
939 		struct sk_buff *skb;
940 
941 		skb = bt_skb_alloc(urb->actual_length, GFP_ATOMIC);
942 		if (skb) {
943 			skb_put_data(skb, urb->transfer_buffer,
944 				     urb->actual_length);
945 			hci_recv_diag(hdev, skb);
946 		}
947 	} else if (urb->status == -ENOENT) {
948 		/* Avoid suspend failed when usb_kill_urb */
949 		return;
950 	}
951 
952 	if (!test_bit(BTUSB_DIAG_RUNNING, &data->flags))
953 		return;
954 
955 	usb_anchor_urb(urb, &data->diag_anchor);
956 	usb_mark_last_busy(data->udev);
957 
958 	err = usb_submit_urb(urb, GFP_ATOMIC);
959 	if (err < 0) {
960 		/* -EPERM: urb is being killed;
961 		 * -ENODEV: device got disconnected
962 		 */
963 		if (err != -EPERM && err != -ENODEV)
964 			BT_ERR("%s urb %p failed to resubmit (%d)",
965 			       hdev->name, urb, -err);
966 		usb_unanchor_urb(urb);
967 	}
968 }
969 
970 static int btusb_submit_diag_urb(struct hci_dev *hdev, gfp_t mem_flags)
971 {
972 	struct btusb_data *data = hci_get_drvdata(hdev);
973 	struct urb *urb;
974 	unsigned char *buf;
975 	unsigned int pipe;
976 	int err, size = HCI_MAX_FRAME_SIZE;
977 
978 	BT_DBG("%s", hdev->name);
979 
980 	if (!data->diag_rx_ep)
981 		return -ENODEV;
982 
983 	urb = usb_alloc_urb(0, mem_flags);
984 	if (!urb)
985 		return -ENOMEM;
986 
987 	buf = kmalloc(size, mem_flags);
988 	if (!buf) {
989 		usb_free_urb(urb);
990 		return -ENOMEM;
991 	}
992 
993 	pipe = usb_rcvbulkpipe(data->udev, data->diag_rx_ep->bEndpointAddress);
994 
995 	usb_fill_bulk_urb(urb, data->udev, pipe, buf, size,
996 			  btusb_diag_complete, hdev);
997 
998 	urb->transfer_flags |= URB_FREE_BUFFER;
999 
1000 	usb_mark_last_busy(data->udev);
1001 	usb_anchor_urb(urb, &data->diag_anchor);
1002 
1003 	err = usb_submit_urb(urb, mem_flags);
1004 	if (err < 0) {
1005 		if (err != -EPERM && err != -ENODEV)
1006 			BT_ERR("%s urb %p submission failed (%d)",
1007 			       hdev->name, urb, -err);
1008 		usb_unanchor_urb(urb);
1009 	}
1010 
1011 	usb_free_urb(urb);
1012 
1013 	return err;
1014 }
1015 
1016 static void btusb_tx_complete(struct urb *urb)
1017 {
1018 	struct sk_buff *skb = urb->context;
1019 	struct hci_dev *hdev = (struct hci_dev *)skb->dev;
1020 	struct btusb_data *data = hci_get_drvdata(hdev);
1021 
1022 	BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
1023 	       urb->actual_length);
1024 
1025 	if (!test_bit(HCI_RUNNING, &hdev->flags))
1026 		goto done;
1027 
1028 	if (!urb->status)
1029 		hdev->stat.byte_tx += urb->transfer_buffer_length;
1030 	else
1031 		hdev->stat.err_tx++;
1032 
1033 done:
1034 	spin_lock(&data->txlock);
1035 	data->tx_in_flight--;
1036 	spin_unlock(&data->txlock);
1037 
1038 	kfree(urb->setup_packet);
1039 
1040 	kfree_skb(skb);
1041 }
1042 
1043 static void btusb_isoc_tx_complete(struct urb *urb)
1044 {
1045 	struct sk_buff *skb = urb->context;
1046 	struct hci_dev *hdev = (struct hci_dev *)skb->dev;
1047 
1048 	BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
1049 	       urb->actual_length);
1050 
1051 	if (!test_bit(HCI_RUNNING, &hdev->flags))
1052 		goto done;
1053 
1054 	if (!urb->status)
1055 		hdev->stat.byte_tx += urb->transfer_buffer_length;
1056 	else
1057 		hdev->stat.err_tx++;
1058 
1059 done:
1060 	kfree(urb->setup_packet);
1061 
1062 	kfree_skb(skb);
1063 }
1064 
1065 static int btusb_open(struct hci_dev *hdev)
1066 {
1067 	struct btusb_data *data = hci_get_drvdata(hdev);
1068 	int err;
1069 
1070 	BT_DBG("%s", hdev->name);
1071 
1072 	err = usb_autopm_get_interface(data->intf);
1073 	if (err < 0)
1074 		return err;
1075 
1076 	/* Patching USB firmware files prior to starting any URBs of HCI path
1077 	 * It is more safe to use USB bulk channel for downloading USB patch
1078 	 */
1079 	if (data->setup_on_usb) {
1080 		err = data->setup_on_usb(hdev);
1081 		if (err < 0)
1082 			return err;
1083 	}
1084 
1085 	data->intf->needs_remote_wakeup = 1;
1086 	/* device specific wakeup source enabled and required for USB
1087 	 * remote wakeup while host is suspended
1088 	 */
1089 	device_wakeup_enable(&data->udev->dev);
1090 
1091 	if (test_and_set_bit(BTUSB_INTR_RUNNING, &data->flags))
1092 		goto done;
1093 
1094 	err = btusb_submit_intr_urb(hdev, GFP_KERNEL);
1095 	if (err < 0)
1096 		goto failed;
1097 
1098 	err = btusb_submit_bulk_urb(hdev, GFP_KERNEL);
1099 	if (err < 0) {
1100 		usb_kill_anchored_urbs(&data->intr_anchor);
1101 		goto failed;
1102 	}
1103 
1104 	set_bit(BTUSB_BULK_RUNNING, &data->flags);
1105 	btusb_submit_bulk_urb(hdev, GFP_KERNEL);
1106 
1107 	if (data->diag) {
1108 		if (!btusb_submit_diag_urb(hdev, GFP_KERNEL))
1109 			set_bit(BTUSB_DIAG_RUNNING, &data->flags);
1110 	}
1111 
1112 done:
1113 	usb_autopm_put_interface(data->intf);
1114 	return 0;
1115 
1116 failed:
1117 	clear_bit(BTUSB_INTR_RUNNING, &data->flags);
1118 	usb_autopm_put_interface(data->intf);
1119 	return err;
1120 }
1121 
1122 static void btusb_stop_traffic(struct btusb_data *data)
1123 {
1124 	usb_kill_anchored_urbs(&data->intr_anchor);
1125 	usb_kill_anchored_urbs(&data->bulk_anchor);
1126 	usb_kill_anchored_urbs(&data->isoc_anchor);
1127 	usb_kill_anchored_urbs(&data->diag_anchor);
1128 }
1129 
1130 static int btusb_close(struct hci_dev *hdev)
1131 {
1132 	struct btusb_data *data = hci_get_drvdata(hdev);
1133 	int err;
1134 
1135 	BT_DBG("%s", hdev->name);
1136 
1137 	cancel_work_sync(&data->work);
1138 	cancel_work_sync(&data->waker);
1139 
1140 	clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1141 	clear_bit(BTUSB_BULK_RUNNING, &data->flags);
1142 	clear_bit(BTUSB_INTR_RUNNING, &data->flags);
1143 	clear_bit(BTUSB_DIAG_RUNNING, &data->flags);
1144 
1145 	btusb_stop_traffic(data);
1146 	btusb_free_frags(data);
1147 
1148 	err = usb_autopm_get_interface(data->intf);
1149 	if (err < 0)
1150 		goto failed;
1151 
1152 	data->intf->needs_remote_wakeup = 0;
1153 	device_wakeup_disable(&data->udev->dev);
1154 	usb_autopm_put_interface(data->intf);
1155 
1156 failed:
1157 	usb_scuttle_anchored_urbs(&data->deferred);
1158 	return 0;
1159 }
1160 
1161 static int btusb_flush(struct hci_dev *hdev)
1162 {
1163 	struct btusb_data *data = hci_get_drvdata(hdev);
1164 
1165 	BT_DBG("%s", hdev->name);
1166 
1167 	usb_kill_anchored_urbs(&data->tx_anchor);
1168 	btusb_free_frags(data);
1169 
1170 	return 0;
1171 }
1172 
1173 static struct urb *alloc_ctrl_urb(struct hci_dev *hdev, struct sk_buff *skb)
1174 {
1175 	struct btusb_data *data = hci_get_drvdata(hdev);
1176 	struct usb_ctrlrequest *dr;
1177 	struct urb *urb;
1178 	unsigned int pipe;
1179 
1180 	urb = usb_alloc_urb(0, GFP_KERNEL);
1181 	if (!urb)
1182 		return ERR_PTR(-ENOMEM);
1183 
1184 	dr = kmalloc(sizeof(*dr), GFP_KERNEL);
1185 	if (!dr) {
1186 		usb_free_urb(urb);
1187 		return ERR_PTR(-ENOMEM);
1188 	}
1189 
1190 	dr->bRequestType = data->cmdreq_type;
1191 	dr->bRequest     = data->cmdreq;
1192 	dr->wIndex       = 0;
1193 	dr->wValue       = 0;
1194 	dr->wLength      = __cpu_to_le16(skb->len);
1195 
1196 	pipe = usb_sndctrlpipe(data->udev, 0x00);
1197 
1198 	usb_fill_control_urb(urb, data->udev, pipe, (void *)dr,
1199 			     skb->data, skb->len, btusb_tx_complete, skb);
1200 
1201 	skb->dev = (void *)hdev;
1202 
1203 	return urb;
1204 }
1205 
1206 static struct urb *alloc_bulk_urb(struct hci_dev *hdev, struct sk_buff *skb)
1207 {
1208 	struct btusb_data *data = hci_get_drvdata(hdev);
1209 	struct urb *urb;
1210 	unsigned int pipe;
1211 
1212 	if (!data->bulk_tx_ep)
1213 		return ERR_PTR(-ENODEV);
1214 
1215 	urb = usb_alloc_urb(0, GFP_KERNEL);
1216 	if (!urb)
1217 		return ERR_PTR(-ENOMEM);
1218 
1219 	pipe = usb_sndbulkpipe(data->udev, data->bulk_tx_ep->bEndpointAddress);
1220 
1221 	usb_fill_bulk_urb(urb, data->udev, pipe,
1222 			  skb->data, skb->len, btusb_tx_complete, skb);
1223 
1224 	skb->dev = (void *)hdev;
1225 
1226 	return urb;
1227 }
1228 
1229 static struct urb *alloc_isoc_urb(struct hci_dev *hdev, struct sk_buff *skb)
1230 {
1231 	struct btusb_data *data = hci_get_drvdata(hdev);
1232 	struct urb *urb;
1233 	unsigned int pipe;
1234 
1235 	if (!data->isoc_tx_ep)
1236 		return ERR_PTR(-ENODEV);
1237 
1238 	urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, GFP_KERNEL);
1239 	if (!urb)
1240 		return ERR_PTR(-ENOMEM);
1241 
1242 	pipe = usb_sndisocpipe(data->udev, data->isoc_tx_ep->bEndpointAddress);
1243 
1244 	usb_fill_int_urb(urb, data->udev, pipe,
1245 			 skb->data, skb->len, btusb_isoc_tx_complete,
1246 			 skb, data->isoc_tx_ep->bInterval);
1247 
1248 	urb->transfer_flags  = URB_ISO_ASAP;
1249 
1250 	__fill_isoc_descriptor(urb, skb->len,
1251 			       le16_to_cpu(data->isoc_tx_ep->wMaxPacketSize));
1252 
1253 	skb->dev = (void *)hdev;
1254 
1255 	return urb;
1256 }
1257 
1258 static int submit_tx_urb(struct hci_dev *hdev, struct urb *urb)
1259 {
1260 	struct btusb_data *data = hci_get_drvdata(hdev);
1261 	int err;
1262 
1263 	usb_anchor_urb(urb, &data->tx_anchor);
1264 
1265 	err = usb_submit_urb(urb, GFP_KERNEL);
1266 	if (err < 0) {
1267 		if (err != -EPERM && err != -ENODEV)
1268 			BT_ERR("%s urb %p submission failed (%d)",
1269 			       hdev->name, urb, -err);
1270 		kfree(urb->setup_packet);
1271 		usb_unanchor_urb(urb);
1272 	} else {
1273 		usb_mark_last_busy(data->udev);
1274 	}
1275 
1276 	usb_free_urb(urb);
1277 	return err;
1278 }
1279 
1280 static int submit_or_queue_tx_urb(struct hci_dev *hdev, struct urb *urb)
1281 {
1282 	struct btusb_data *data = hci_get_drvdata(hdev);
1283 	unsigned long flags;
1284 	bool suspending;
1285 
1286 	spin_lock_irqsave(&data->txlock, flags);
1287 	suspending = test_bit(BTUSB_SUSPENDING, &data->flags);
1288 	if (!suspending)
1289 		data->tx_in_flight++;
1290 	spin_unlock_irqrestore(&data->txlock, flags);
1291 
1292 	if (!suspending)
1293 		return submit_tx_urb(hdev, urb);
1294 
1295 	usb_anchor_urb(urb, &data->deferred);
1296 	schedule_work(&data->waker);
1297 
1298 	usb_free_urb(urb);
1299 	return 0;
1300 }
1301 
1302 static int btusb_send_frame(struct hci_dev *hdev, struct sk_buff *skb)
1303 {
1304 	struct urb *urb;
1305 
1306 	BT_DBG("%s", hdev->name);
1307 
1308 	switch (hci_skb_pkt_type(skb)) {
1309 	case HCI_COMMAND_PKT:
1310 		urb = alloc_ctrl_urb(hdev, skb);
1311 		if (IS_ERR(urb))
1312 			return PTR_ERR(urb);
1313 
1314 		hdev->stat.cmd_tx++;
1315 		return submit_or_queue_tx_urb(hdev, urb);
1316 
1317 	case HCI_ACLDATA_PKT:
1318 		urb = alloc_bulk_urb(hdev, skb);
1319 		if (IS_ERR(urb))
1320 			return PTR_ERR(urb);
1321 
1322 		hdev->stat.acl_tx++;
1323 		return submit_or_queue_tx_urb(hdev, urb);
1324 
1325 	case HCI_SCODATA_PKT:
1326 		if (hci_conn_num(hdev, SCO_LINK) < 1)
1327 			return -ENODEV;
1328 
1329 		urb = alloc_isoc_urb(hdev, skb);
1330 		if (IS_ERR(urb))
1331 			return PTR_ERR(urb);
1332 
1333 		hdev->stat.sco_tx++;
1334 		return submit_tx_urb(hdev, urb);
1335 	}
1336 
1337 	return -EILSEQ;
1338 }
1339 
1340 static void btusb_notify(struct hci_dev *hdev, unsigned int evt)
1341 {
1342 	struct btusb_data *data = hci_get_drvdata(hdev);
1343 
1344 	BT_DBG("%s evt %d", hdev->name, evt);
1345 
1346 	if (hci_conn_num(hdev, SCO_LINK) != data->sco_num) {
1347 		data->sco_num = hci_conn_num(hdev, SCO_LINK);
1348 		schedule_work(&data->work);
1349 	}
1350 }
1351 
1352 static inline int __set_isoc_interface(struct hci_dev *hdev, int altsetting)
1353 {
1354 	struct btusb_data *data = hci_get_drvdata(hdev);
1355 	struct usb_interface *intf = data->isoc;
1356 	struct usb_endpoint_descriptor *ep_desc;
1357 	int i, err;
1358 
1359 	if (!data->isoc)
1360 		return -ENODEV;
1361 
1362 	err = usb_set_interface(data->udev, 1, altsetting);
1363 	if (err < 0) {
1364 		BT_ERR("%s setting interface failed (%d)", hdev->name, -err);
1365 		return err;
1366 	}
1367 
1368 	data->isoc_altsetting = altsetting;
1369 
1370 	data->isoc_tx_ep = NULL;
1371 	data->isoc_rx_ep = NULL;
1372 
1373 	for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
1374 		ep_desc = &intf->cur_altsetting->endpoint[i].desc;
1375 
1376 		if (!data->isoc_tx_ep && usb_endpoint_is_isoc_out(ep_desc)) {
1377 			data->isoc_tx_ep = ep_desc;
1378 			continue;
1379 		}
1380 
1381 		if (!data->isoc_rx_ep && usb_endpoint_is_isoc_in(ep_desc)) {
1382 			data->isoc_rx_ep = ep_desc;
1383 			continue;
1384 		}
1385 	}
1386 
1387 	if (!data->isoc_tx_ep || !data->isoc_rx_ep) {
1388 		BT_ERR("%s invalid SCO descriptors", hdev->name);
1389 		return -ENODEV;
1390 	}
1391 
1392 	return 0;
1393 }
1394 
1395 static void btusb_work(struct work_struct *work)
1396 {
1397 	struct btusb_data *data = container_of(work, struct btusb_data, work);
1398 	struct hci_dev *hdev = data->hdev;
1399 	int new_alts;
1400 	int err;
1401 
1402 	if (data->sco_num > 0) {
1403 		if (!test_bit(BTUSB_DID_ISO_RESUME, &data->flags)) {
1404 			err = usb_autopm_get_interface(data->isoc ? data->isoc : data->intf);
1405 			if (err < 0) {
1406 				clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1407 				usb_kill_anchored_urbs(&data->isoc_anchor);
1408 				return;
1409 			}
1410 
1411 			set_bit(BTUSB_DID_ISO_RESUME, &data->flags);
1412 		}
1413 
1414 		if (hdev->voice_setting & 0x0020) {
1415 			static const int alts[3] = { 2, 4, 5 };
1416 
1417 			new_alts = alts[data->sco_num - 1];
1418 		} else {
1419 			new_alts = data->sco_num;
1420 		}
1421 
1422 		if (data->isoc_altsetting != new_alts) {
1423 			unsigned long flags;
1424 
1425 			clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1426 			usb_kill_anchored_urbs(&data->isoc_anchor);
1427 
1428 			/* When isochronous alternate setting needs to be
1429 			 * changed, because SCO connection has been added
1430 			 * or removed, a packet fragment may be left in the
1431 			 * reassembling state. This could lead to wrongly
1432 			 * assembled fragments.
1433 			 *
1434 			 * Clear outstanding fragment when selecting a new
1435 			 * alternate setting.
1436 			 */
1437 			spin_lock_irqsave(&data->rxlock, flags);
1438 			kfree_skb(data->sco_skb);
1439 			data->sco_skb = NULL;
1440 			spin_unlock_irqrestore(&data->rxlock, flags);
1441 
1442 			if (__set_isoc_interface(hdev, new_alts) < 0)
1443 				return;
1444 		}
1445 
1446 		if (!test_and_set_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
1447 			if (btusb_submit_isoc_urb(hdev, GFP_KERNEL) < 0)
1448 				clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1449 			else
1450 				btusb_submit_isoc_urb(hdev, GFP_KERNEL);
1451 		}
1452 	} else {
1453 		clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1454 		usb_kill_anchored_urbs(&data->isoc_anchor);
1455 
1456 		__set_isoc_interface(hdev, 0);
1457 		if (test_and_clear_bit(BTUSB_DID_ISO_RESUME, &data->flags))
1458 			usb_autopm_put_interface(data->isoc ? data->isoc : data->intf);
1459 	}
1460 }
1461 
1462 static void btusb_waker(struct work_struct *work)
1463 {
1464 	struct btusb_data *data = container_of(work, struct btusb_data, waker);
1465 	int err;
1466 
1467 	err = usb_autopm_get_interface(data->intf);
1468 	if (err < 0)
1469 		return;
1470 
1471 	usb_autopm_put_interface(data->intf);
1472 }
1473 
1474 static int btusb_setup_bcm92035(struct hci_dev *hdev)
1475 {
1476 	struct sk_buff *skb;
1477 	u8 val = 0x00;
1478 
1479 	BT_DBG("%s", hdev->name);
1480 
1481 	skb = __hci_cmd_sync(hdev, 0xfc3b, 1, &val, HCI_INIT_TIMEOUT);
1482 	if (IS_ERR(skb))
1483 		BT_ERR("BCM92035 command failed (%ld)", -PTR_ERR(skb));
1484 	else
1485 		kfree_skb(skb);
1486 
1487 	return 0;
1488 }
1489 
1490 static int btusb_setup_csr(struct hci_dev *hdev)
1491 {
1492 	struct hci_rp_read_local_version *rp;
1493 	struct sk_buff *skb;
1494 
1495 	BT_DBG("%s", hdev->name);
1496 
1497 	skb = __hci_cmd_sync(hdev, HCI_OP_READ_LOCAL_VERSION, 0, NULL,
1498 			     HCI_INIT_TIMEOUT);
1499 	if (IS_ERR(skb)) {
1500 		int err = PTR_ERR(skb);
1501 		BT_ERR("%s: CSR: Local version failed (%d)", hdev->name, err);
1502 		return err;
1503 	}
1504 
1505 	if (skb->len != sizeof(struct hci_rp_read_local_version)) {
1506 		BT_ERR("%s: CSR: Local version length mismatch", hdev->name);
1507 		kfree_skb(skb);
1508 		return -EIO;
1509 	}
1510 
1511 	rp = (struct hci_rp_read_local_version *)skb->data;
1512 
1513 	/* Detect controllers which aren't real CSR ones. */
1514 	if (le16_to_cpu(rp->manufacturer) != 10 ||
1515 	    le16_to_cpu(rp->lmp_subver) == 0x0c5c) {
1516 		/* Clear the reset quirk since this is not an actual
1517 		 * early Bluetooth 1.1 device from CSR.
1518 		 */
1519 		clear_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
1520 
1521 		/* These fake CSR controllers have all a broken
1522 		 * stored link key handling and so just disable it.
1523 		 */
1524 		set_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks);
1525 	}
1526 
1527 	kfree_skb(skb);
1528 
1529 	return 0;
1530 }
1531 
1532 static const struct firmware *btusb_setup_intel_get_fw(struct hci_dev *hdev,
1533 						       struct intel_version *ver)
1534 {
1535 	const struct firmware *fw;
1536 	char fwname[64];
1537 	int ret;
1538 
1539 	snprintf(fwname, sizeof(fwname),
1540 		 "intel/ibt-hw-%x.%x.%x-fw-%x.%x.%x.%x.%x.bseq",
1541 		 ver->hw_platform, ver->hw_variant, ver->hw_revision,
1542 		 ver->fw_variant,  ver->fw_revision, ver->fw_build_num,
1543 		 ver->fw_build_ww, ver->fw_build_yy);
1544 
1545 	ret = request_firmware(&fw, fwname, &hdev->dev);
1546 	if (ret < 0) {
1547 		if (ret == -EINVAL) {
1548 			BT_ERR("%s Intel firmware file request failed (%d)",
1549 			       hdev->name, ret);
1550 			return NULL;
1551 		}
1552 
1553 		BT_ERR("%s failed to open Intel firmware file: %s(%d)",
1554 		       hdev->name, fwname, ret);
1555 
1556 		/* If the correct firmware patch file is not found, use the
1557 		 * default firmware patch file instead
1558 		 */
1559 		snprintf(fwname, sizeof(fwname), "intel/ibt-hw-%x.%x.bseq",
1560 			 ver->hw_platform, ver->hw_variant);
1561 		if (request_firmware(&fw, fwname, &hdev->dev) < 0) {
1562 			BT_ERR("%s failed to open default Intel fw file: %s",
1563 			       hdev->name, fwname);
1564 			return NULL;
1565 		}
1566 	}
1567 
1568 	BT_INFO("%s: Intel Bluetooth firmware file: %s", hdev->name, fwname);
1569 
1570 	return fw;
1571 }
1572 
1573 static int btusb_setup_intel_patching(struct hci_dev *hdev,
1574 				      const struct firmware *fw,
1575 				      const u8 **fw_ptr, int *disable_patch)
1576 {
1577 	struct sk_buff *skb;
1578 	struct hci_command_hdr *cmd;
1579 	const u8 *cmd_param;
1580 	struct hci_event_hdr *evt = NULL;
1581 	const u8 *evt_param = NULL;
1582 	int remain = fw->size - (*fw_ptr - fw->data);
1583 
1584 	/* The first byte indicates the types of the patch command or event.
1585 	 * 0x01 means HCI command and 0x02 is HCI event. If the first bytes
1586 	 * in the current firmware buffer doesn't start with 0x01 or
1587 	 * the size of remain buffer is smaller than HCI command header,
1588 	 * the firmware file is corrupted and it should stop the patching
1589 	 * process.
1590 	 */
1591 	if (remain > HCI_COMMAND_HDR_SIZE && *fw_ptr[0] != 0x01) {
1592 		BT_ERR("%s Intel fw corrupted: invalid cmd read", hdev->name);
1593 		return -EINVAL;
1594 	}
1595 	(*fw_ptr)++;
1596 	remain--;
1597 
1598 	cmd = (struct hci_command_hdr *)(*fw_ptr);
1599 	*fw_ptr += sizeof(*cmd);
1600 	remain -= sizeof(*cmd);
1601 
1602 	/* Ensure that the remain firmware data is long enough than the length
1603 	 * of command parameter. If not, the firmware file is corrupted.
1604 	 */
1605 	if (remain < cmd->plen) {
1606 		BT_ERR("%s Intel fw corrupted: invalid cmd len", hdev->name);
1607 		return -EFAULT;
1608 	}
1609 
1610 	/* If there is a command that loads a patch in the firmware
1611 	 * file, then enable the patch upon success, otherwise just
1612 	 * disable the manufacturer mode, for example patch activation
1613 	 * is not required when the default firmware patch file is used
1614 	 * because there are no patch data to load.
1615 	 */
1616 	if (*disable_patch && le16_to_cpu(cmd->opcode) == 0xfc8e)
1617 		*disable_patch = 0;
1618 
1619 	cmd_param = *fw_ptr;
1620 	*fw_ptr += cmd->plen;
1621 	remain -= cmd->plen;
1622 
1623 	/* This reads the expected events when the above command is sent to the
1624 	 * device. Some vendor commands expects more than one events, for
1625 	 * example command status event followed by vendor specific event.
1626 	 * For this case, it only keeps the last expected event. so the command
1627 	 * can be sent with __hci_cmd_sync_ev() which returns the sk_buff of
1628 	 * last expected event.
1629 	 */
1630 	while (remain > HCI_EVENT_HDR_SIZE && *fw_ptr[0] == 0x02) {
1631 		(*fw_ptr)++;
1632 		remain--;
1633 
1634 		evt = (struct hci_event_hdr *)(*fw_ptr);
1635 		*fw_ptr += sizeof(*evt);
1636 		remain -= sizeof(*evt);
1637 
1638 		if (remain < evt->plen) {
1639 			BT_ERR("%s Intel fw corrupted: invalid evt len",
1640 			       hdev->name);
1641 			return -EFAULT;
1642 		}
1643 
1644 		evt_param = *fw_ptr;
1645 		*fw_ptr += evt->plen;
1646 		remain -= evt->plen;
1647 	}
1648 
1649 	/* Every HCI commands in the firmware file has its correspond event.
1650 	 * If event is not found or remain is smaller than zero, the firmware
1651 	 * file is corrupted.
1652 	 */
1653 	if (!evt || !evt_param || remain < 0) {
1654 		BT_ERR("%s Intel fw corrupted: invalid evt read", hdev->name);
1655 		return -EFAULT;
1656 	}
1657 
1658 	skb = __hci_cmd_sync_ev(hdev, le16_to_cpu(cmd->opcode), cmd->plen,
1659 				cmd_param, evt->evt, HCI_INIT_TIMEOUT);
1660 	if (IS_ERR(skb)) {
1661 		BT_ERR("%s sending Intel patch command (0x%4.4x) failed (%ld)",
1662 		       hdev->name, cmd->opcode, PTR_ERR(skb));
1663 		return PTR_ERR(skb);
1664 	}
1665 
1666 	/* It ensures that the returned event matches the event data read from
1667 	 * the firmware file. At fist, it checks the length and then
1668 	 * the contents of the event.
1669 	 */
1670 	if (skb->len != evt->plen) {
1671 		BT_ERR("%s mismatch event length (opcode 0x%4.4x)", hdev->name,
1672 		       le16_to_cpu(cmd->opcode));
1673 		kfree_skb(skb);
1674 		return -EFAULT;
1675 	}
1676 
1677 	if (memcmp(skb->data, evt_param, evt->plen)) {
1678 		BT_ERR("%s mismatch event parameter (opcode 0x%4.4x)",
1679 		       hdev->name, le16_to_cpu(cmd->opcode));
1680 		kfree_skb(skb);
1681 		return -EFAULT;
1682 	}
1683 	kfree_skb(skb);
1684 
1685 	return 0;
1686 }
1687 
1688 static int btusb_setup_intel(struct hci_dev *hdev)
1689 {
1690 	struct sk_buff *skb;
1691 	const struct firmware *fw;
1692 	const u8 *fw_ptr;
1693 	int disable_patch, err;
1694 	struct intel_version ver;
1695 
1696 	BT_DBG("%s", hdev->name);
1697 
1698 	/* The controller has a bug with the first HCI command sent to it
1699 	 * returning number of completed commands as zero. This would stall the
1700 	 * command processing in the Bluetooth core.
1701 	 *
1702 	 * As a workaround, send HCI Reset command first which will reset the
1703 	 * number of completed commands and allow normal command processing
1704 	 * from now on.
1705 	 */
1706 	skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
1707 	if (IS_ERR(skb)) {
1708 		BT_ERR("%s sending initial HCI reset command failed (%ld)",
1709 		       hdev->name, PTR_ERR(skb));
1710 		return PTR_ERR(skb);
1711 	}
1712 	kfree_skb(skb);
1713 
1714 	/* Read Intel specific controller version first to allow selection of
1715 	 * which firmware file to load.
1716 	 *
1717 	 * The returned information are hardware variant and revision plus
1718 	 * firmware variant, revision and build number.
1719 	 */
1720 	err = btintel_read_version(hdev, &ver);
1721 	if (err)
1722 		return err;
1723 
1724 	BT_INFO("%s: read Intel version: %02x%02x%02x%02x%02x%02x%02x%02x%02x",
1725 		hdev->name, ver.hw_platform, ver.hw_variant, ver.hw_revision,
1726 		ver.fw_variant,  ver.fw_revision, ver.fw_build_num,
1727 		ver.fw_build_ww, ver.fw_build_yy, ver.fw_patch_num);
1728 
1729 	/* fw_patch_num indicates the version of patch the device currently
1730 	 * have. If there is no patch data in the device, it is always 0x00.
1731 	 * So, if it is other than 0x00, no need to patch the device again.
1732 	 */
1733 	if (ver.fw_patch_num) {
1734 		BT_INFO("%s: Intel device is already patched. patch num: %02x",
1735 			hdev->name, ver.fw_patch_num);
1736 		goto complete;
1737 	}
1738 
1739 	/* Opens the firmware patch file based on the firmware version read
1740 	 * from the controller. If it fails to open the matching firmware
1741 	 * patch file, it tries to open the default firmware patch file.
1742 	 * If no patch file is found, allow the device to operate without
1743 	 * a patch.
1744 	 */
1745 	fw = btusb_setup_intel_get_fw(hdev, &ver);
1746 	if (!fw)
1747 		goto complete;
1748 	fw_ptr = fw->data;
1749 
1750 	/* Enable the manufacturer mode of the controller.
1751 	 * Only while this mode is enabled, the driver can download the
1752 	 * firmware patch data and configuration parameters.
1753 	 */
1754 	err = btintel_enter_mfg(hdev);
1755 	if (err) {
1756 		release_firmware(fw);
1757 		return err;
1758 	}
1759 
1760 	disable_patch = 1;
1761 
1762 	/* The firmware data file consists of list of Intel specific HCI
1763 	 * commands and its expected events. The first byte indicates the
1764 	 * type of the message, either HCI command or HCI event.
1765 	 *
1766 	 * It reads the command and its expected event from the firmware file,
1767 	 * and send to the controller. Once __hci_cmd_sync_ev() returns,
1768 	 * the returned event is compared with the event read from the firmware
1769 	 * file and it will continue until all the messages are downloaded to
1770 	 * the controller.
1771 	 *
1772 	 * Once the firmware patching is completed successfully,
1773 	 * the manufacturer mode is disabled with reset and activating the
1774 	 * downloaded patch.
1775 	 *
1776 	 * If the firmware patching fails, the manufacturer mode is
1777 	 * disabled with reset and deactivating the patch.
1778 	 *
1779 	 * If the default patch file is used, no reset is done when disabling
1780 	 * the manufacturer.
1781 	 */
1782 	while (fw->size > fw_ptr - fw->data) {
1783 		int ret;
1784 
1785 		ret = btusb_setup_intel_patching(hdev, fw, &fw_ptr,
1786 						 &disable_patch);
1787 		if (ret < 0)
1788 			goto exit_mfg_deactivate;
1789 	}
1790 
1791 	release_firmware(fw);
1792 
1793 	if (disable_patch)
1794 		goto exit_mfg_disable;
1795 
1796 	/* Patching completed successfully and disable the manufacturer mode
1797 	 * with reset and activate the downloaded firmware patches.
1798 	 */
1799 	err = btintel_exit_mfg(hdev, true, true);
1800 	if (err)
1801 		return err;
1802 
1803 	BT_INFO("%s: Intel Bluetooth firmware patch completed and activated",
1804 		hdev->name);
1805 
1806 	goto complete;
1807 
1808 exit_mfg_disable:
1809 	/* Disable the manufacturer mode without reset */
1810 	err = btintel_exit_mfg(hdev, false, false);
1811 	if (err)
1812 		return err;
1813 
1814 	BT_INFO("%s: Intel Bluetooth firmware patch completed", hdev->name);
1815 
1816 	goto complete;
1817 
1818 exit_mfg_deactivate:
1819 	release_firmware(fw);
1820 
1821 	/* Patching failed. Disable the manufacturer mode with reset and
1822 	 * deactivate the downloaded firmware patches.
1823 	 */
1824 	err = btintel_exit_mfg(hdev, true, false);
1825 	if (err)
1826 		return err;
1827 
1828 	BT_INFO("%s: Intel Bluetooth firmware patch completed and deactivated",
1829 		hdev->name);
1830 
1831 complete:
1832 	/* Set the event mask for Intel specific vendor events. This enables
1833 	 * a few extra events that are useful during general operation.
1834 	 */
1835 	btintel_set_event_mask_mfg(hdev, false);
1836 
1837 	btintel_check_bdaddr(hdev);
1838 	return 0;
1839 }
1840 
1841 static int inject_cmd_complete(struct hci_dev *hdev, __u16 opcode)
1842 {
1843 	struct sk_buff *skb;
1844 	struct hci_event_hdr *hdr;
1845 	struct hci_ev_cmd_complete *evt;
1846 
1847 	skb = bt_skb_alloc(sizeof(*hdr) + sizeof(*evt) + 1, GFP_ATOMIC);
1848 	if (!skb)
1849 		return -ENOMEM;
1850 
1851 	hdr = skb_put(skb, sizeof(*hdr));
1852 	hdr->evt = HCI_EV_CMD_COMPLETE;
1853 	hdr->plen = sizeof(*evt) + 1;
1854 
1855 	evt = skb_put(skb, sizeof(*evt));
1856 	evt->ncmd = 0x01;
1857 	evt->opcode = cpu_to_le16(opcode);
1858 
1859 	skb_put_u8(skb, 0x00);
1860 
1861 	hci_skb_pkt_type(skb) = HCI_EVENT_PKT;
1862 
1863 	return hci_recv_frame(hdev, skb);
1864 }
1865 
1866 static int btusb_recv_bulk_intel(struct btusb_data *data, void *buffer,
1867 				 int count)
1868 {
1869 	/* When the device is in bootloader mode, then it can send
1870 	 * events via the bulk endpoint. These events are treated the
1871 	 * same way as the ones received from the interrupt endpoint.
1872 	 */
1873 	if (test_bit(BTUSB_BOOTLOADER, &data->flags))
1874 		return btusb_recv_intr(data, buffer, count);
1875 
1876 	return btusb_recv_bulk(data, buffer, count);
1877 }
1878 
1879 static void btusb_intel_bootup(struct btusb_data *data, const void *ptr,
1880 			       unsigned int len)
1881 {
1882 	const struct intel_bootup *evt = ptr;
1883 
1884 	if (len != sizeof(*evt))
1885 		return;
1886 
1887 	if (test_and_clear_bit(BTUSB_BOOTING, &data->flags)) {
1888 		smp_mb__after_atomic();
1889 		wake_up_bit(&data->flags, BTUSB_BOOTING);
1890 	}
1891 }
1892 
1893 static void btusb_intel_secure_send_result(struct btusb_data *data,
1894 					   const void *ptr, unsigned int len)
1895 {
1896 	const struct intel_secure_send_result *evt = ptr;
1897 
1898 	if (len != sizeof(*evt))
1899 		return;
1900 
1901 	if (evt->result)
1902 		set_bit(BTUSB_FIRMWARE_FAILED, &data->flags);
1903 
1904 	if (test_and_clear_bit(BTUSB_DOWNLOADING, &data->flags) &&
1905 	    test_bit(BTUSB_FIRMWARE_LOADED, &data->flags)) {
1906 		smp_mb__after_atomic();
1907 		wake_up_bit(&data->flags, BTUSB_DOWNLOADING);
1908 	}
1909 }
1910 
1911 static int btusb_recv_event_intel(struct hci_dev *hdev, struct sk_buff *skb)
1912 {
1913 	struct btusb_data *data = hci_get_drvdata(hdev);
1914 
1915 	if (test_bit(BTUSB_BOOTLOADER, &data->flags)) {
1916 		struct hci_event_hdr *hdr = (void *)skb->data;
1917 
1918 		if (skb->len > HCI_EVENT_HDR_SIZE && hdr->evt == 0xff &&
1919 		    hdr->plen > 0) {
1920 			const void *ptr = skb->data + HCI_EVENT_HDR_SIZE + 1;
1921 			unsigned int len = skb->len - HCI_EVENT_HDR_SIZE - 1;
1922 
1923 			switch (skb->data[2]) {
1924 			case 0x02:
1925 				/* When switching to the operational firmware
1926 				 * the device sends a vendor specific event
1927 				 * indicating that the bootup completed.
1928 				 */
1929 				btusb_intel_bootup(data, ptr, len);
1930 				break;
1931 			case 0x06:
1932 				/* When the firmware loading completes the
1933 				 * device sends out a vendor specific event
1934 				 * indicating the result of the firmware
1935 				 * loading.
1936 				 */
1937 				btusb_intel_secure_send_result(data, ptr, len);
1938 				break;
1939 			}
1940 		}
1941 	}
1942 
1943 	return hci_recv_frame(hdev, skb);
1944 }
1945 
1946 static int btusb_send_frame_intel(struct hci_dev *hdev, struct sk_buff *skb)
1947 {
1948 	struct btusb_data *data = hci_get_drvdata(hdev);
1949 	struct urb *urb;
1950 
1951 	BT_DBG("%s", hdev->name);
1952 
1953 	switch (hci_skb_pkt_type(skb)) {
1954 	case HCI_COMMAND_PKT:
1955 		if (test_bit(BTUSB_BOOTLOADER, &data->flags)) {
1956 			struct hci_command_hdr *cmd = (void *)skb->data;
1957 			__u16 opcode = le16_to_cpu(cmd->opcode);
1958 
1959 			/* When in bootloader mode and the command 0xfc09
1960 			 * is received, it needs to be send down the
1961 			 * bulk endpoint. So allocate a bulk URB instead.
1962 			 */
1963 			if (opcode == 0xfc09)
1964 				urb = alloc_bulk_urb(hdev, skb);
1965 			else
1966 				urb = alloc_ctrl_urb(hdev, skb);
1967 
1968 			/* When the 0xfc01 command is issued to boot into
1969 			 * the operational firmware, it will actually not
1970 			 * send a command complete event. To keep the flow
1971 			 * control working inject that event here.
1972 			 */
1973 			if (opcode == 0xfc01)
1974 				inject_cmd_complete(hdev, opcode);
1975 		} else {
1976 			urb = alloc_ctrl_urb(hdev, skb);
1977 		}
1978 		if (IS_ERR(urb))
1979 			return PTR_ERR(urb);
1980 
1981 		hdev->stat.cmd_tx++;
1982 		return submit_or_queue_tx_urb(hdev, urb);
1983 
1984 	case HCI_ACLDATA_PKT:
1985 		urb = alloc_bulk_urb(hdev, skb);
1986 		if (IS_ERR(urb))
1987 			return PTR_ERR(urb);
1988 
1989 		hdev->stat.acl_tx++;
1990 		return submit_or_queue_tx_urb(hdev, urb);
1991 
1992 	case HCI_SCODATA_PKT:
1993 		if (hci_conn_num(hdev, SCO_LINK) < 1)
1994 			return -ENODEV;
1995 
1996 		urb = alloc_isoc_urb(hdev, skb);
1997 		if (IS_ERR(urb))
1998 			return PTR_ERR(urb);
1999 
2000 		hdev->stat.sco_tx++;
2001 		return submit_tx_urb(hdev, urb);
2002 	}
2003 
2004 	return -EILSEQ;
2005 }
2006 
2007 static int btusb_setup_intel_new(struct hci_dev *hdev)
2008 {
2009 	static const u8 reset_param[] = { 0x00, 0x01, 0x00, 0x01,
2010 					  0x00, 0x08, 0x04, 0x00 };
2011 	struct btusb_data *data = hci_get_drvdata(hdev);
2012 	struct sk_buff *skb;
2013 	struct intel_version ver;
2014 	struct intel_boot_params *params;
2015 	const struct firmware *fw;
2016 	const u8 *fw_ptr;
2017 	u32 frag_len;
2018 	char fwname[64];
2019 	ktime_t calltime, delta, rettime;
2020 	unsigned long long duration;
2021 	int err;
2022 
2023 	BT_DBG("%s", hdev->name);
2024 
2025 	calltime = ktime_get();
2026 
2027 	/* Read the Intel version information to determine if the device
2028 	 * is in bootloader mode or if it already has operational firmware
2029 	 * loaded.
2030 	 */
2031 	err = btintel_read_version(hdev, &ver);
2032 	if (err)
2033 		return err;
2034 
2035 	/* The hardware platform number has a fixed value of 0x37 and
2036 	 * for now only accept this single value.
2037 	 */
2038 	if (ver.hw_platform != 0x37) {
2039 		BT_ERR("%s: Unsupported Intel hardware platform (%u)",
2040 		       hdev->name, ver.hw_platform);
2041 		return -EINVAL;
2042 	}
2043 
2044 	/* Check for supported iBT hardware variants of this firmware
2045 	 * loading method.
2046 	 *
2047 	 * This check has been put in place to ensure correct forward
2048 	 * compatibility options when newer hardware variants come along.
2049 	 */
2050 	switch (ver.hw_variant) {
2051 	case 0x0b:	/* SfP */
2052 	case 0x0c:	/* WsP */
2053 	case 0x11:	/* JfP */
2054 	case 0x12:	/* ThP */
2055 		break;
2056 	default:
2057 		BT_ERR("%s: Unsupported Intel hardware variant (%u)",
2058 		       hdev->name, ver.hw_variant);
2059 		return -EINVAL;
2060 	}
2061 
2062 	btintel_version_info(hdev, &ver);
2063 
2064 	/* The firmware variant determines if the device is in bootloader
2065 	 * mode or is running operational firmware. The value 0x06 identifies
2066 	 * the bootloader and the value 0x23 identifies the operational
2067 	 * firmware.
2068 	 *
2069 	 * When the operational firmware is already present, then only
2070 	 * the check for valid Bluetooth device address is needed. This
2071 	 * determines if the device will be added as configured or
2072 	 * unconfigured controller.
2073 	 *
2074 	 * It is not possible to use the Secure Boot Parameters in this
2075 	 * case since that command is only available in bootloader mode.
2076 	 */
2077 	if (ver.fw_variant == 0x23) {
2078 		clear_bit(BTUSB_BOOTLOADER, &data->flags);
2079 		btintel_check_bdaddr(hdev);
2080 		return 0;
2081 	}
2082 
2083 	/* If the device is not in bootloader mode, then the only possible
2084 	 * choice is to return an error and abort the device initialization.
2085 	 */
2086 	if (ver.fw_variant != 0x06) {
2087 		BT_ERR("%s: Unsupported Intel firmware variant (%u)",
2088 		       hdev->name, ver.fw_variant);
2089 		return -ENODEV;
2090 	}
2091 
2092 	/* Read the secure boot parameters to identify the operating
2093 	 * details of the bootloader.
2094 	 */
2095 	skb = __hci_cmd_sync(hdev, 0xfc0d, 0, NULL, HCI_INIT_TIMEOUT);
2096 	if (IS_ERR(skb)) {
2097 		BT_ERR("%s: Reading Intel boot parameters failed (%ld)",
2098 		       hdev->name, PTR_ERR(skb));
2099 		return PTR_ERR(skb);
2100 	}
2101 
2102 	if (skb->len != sizeof(*params)) {
2103 		BT_ERR("%s: Intel boot parameters size mismatch", hdev->name);
2104 		kfree_skb(skb);
2105 		return -EILSEQ;
2106 	}
2107 
2108 	params = (struct intel_boot_params *)skb->data;
2109 
2110 	BT_INFO("%s: Device revision is %u", hdev->name,
2111 		le16_to_cpu(params->dev_revid));
2112 
2113 	BT_INFO("%s: Secure boot is %s", hdev->name,
2114 		params->secure_boot ? "enabled" : "disabled");
2115 
2116 	BT_INFO("%s: OTP lock is %s", hdev->name,
2117 		params->otp_lock ? "enabled" : "disabled");
2118 
2119 	BT_INFO("%s: API lock is %s", hdev->name,
2120 		params->api_lock ? "enabled" : "disabled");
2121 
2122 	BT_INFO("%s: Debug lock is %s", hdev->name,
2123 		params->debug_lock ? "enabled" : "disabled");
2124 
2125 	BT_INFO("%s: Minimum firmware build %u week %u %u", hdev->name,
2126 		params->min_fw_build_nn, params->min_fw_build_cw,
2127 		2000 + params->min_fw_build_yy);
2128 
2129 	/* It is required that every single firmware fragment is acknowledged
2130 	 * with a command complete event. If the boot parameters indicate
2131 	 * that this bootloader does not send them, then abort the setup.
2132 	 */
2133 	if (params->limited_cce != 0x00) {
2134 		BT_ERR("%s: Unsupported Intel firmware loading method (%u)",
2135 		       hdev->name, params->limited_cce);
2136 		kfree_skb(skb);
2137 		return -EINVAL;
2138 	}
2139 
2140 	/* If the OTP has no valid Bluetooth device address, then there will
2141 	 * also be no valid address for the operational firmware.
2142 	 */
2143 	if (!bacmp(&params->otp_bdaddr, BDADDR_ANY)) {
2144 		BT_INFO("%s: No device address configured", hdev->name);
2145 		set_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks);
2146 	}
2147 
2148 	/* With this Intel bootloader only the hardware variant and device
2149 	 * revision information are used to select the right firmware.
2150 	 *
2151 	 * The firmware filename is ibt-<hw_variant>-<dev_revid>.sfi.
2152 	 *
2153 	 * Currently the supported hardware variants are:
2154 	 *   11 (0x0b) for iBT3.0 (LnP/SfP)
2155 	 *   12 (0x0c) for iBT3.5 (WsP)
2156 	 *   17 (0x11) for iBT3.5 (JfP)
2157 	 *   18 (0x12) for iBT3.5 (ThP)
2158 	 */
2159 	snprintf(fwname, sizeof(fwname), "intel/ibt-%u-%u.sfi",
2160 		 le16_to_cpu(ver.hw_variant),
2161 		 le16_to_cpu(params->dev_revid));
2162 
2163 	err = request_firmware(&fw, fwname, &hdev->dev);
2164 	if (err < 0) {
2165 		BT_ERR("%s: Failed to load Intel firmware file (%d)",
2166 		       hdev->name, err);
2167 		kfree_skb(skb);
2168 		return err;
2169 	}
2170 
2171 	BT_INFO("%s: Found device firmware: %s", hdev->name, fwname);
2172 
2173 	/* Save the DDC file name for later use to apply once the firmware
2174 	 * downloading is done.
2175 	 */
2176 	snprintf(fwname, sizeof(fwname), "intel/ibt-%u-%u.ddc",
2177 		 le16_to_cpu(ver.hw_variant),
2178 		 le16_to_cpu(params->dev_revid));
2179 
2180 	kfree_skb(skb);
2181 
2182 	if (fw->size < 644) {
2183 		BT_ERR("%s: Invalid size of firmware file (%zu)",
2184 		       hdev->name, fw->size);
2185 		err = -EBADF;
2186 		goto done;
2187 	}
2188 
2189 	set_bit(BTUSB_DOWNLOADING, &data->flags);
2190 
2191 	/* Start the firmware download transaction with the Init fragment
2192 	 * represented by the 128 bytes of CSS header.
2193 	 */
2194 	err = btintel_secure_send(hdev, 0x00, 128, fw->data);
2195 	if (err < 0) {
2196 		BT_ERR("%s: Failed to send firmware header (%d)",
2197 		       hdev->name, err);
2198 		goto done;
2199 	}
2200 
2201 	/* Send the 256 bytes of public key information from the firmware
2202 	 * as the PKey fragment.
2203 	 */
2204 	err = btintel_secure_send(hdev, 0x03, 256, fw->data + 128);
2205 	if (err < 0) {
2206 		BT_ERR("%s: Failed to send firmware public key (%d)",
2207 		       hdev->name, err);
2208 		goto done;
2209 	}
2210 
2211 	/* Send the 256 bytes of signature information from the firmware
2212 	 * as the Sign fragment.
2213 	 */
2214 	err = btintel_secure_send(hdev, 0x02, 256, fw->data + 388);
2215 	if (err < 0) {
2216 		BT_ERR("%s: Failed to send firmware signature (%d)",
2217 		       hdev->name, err);
2218 		goto done;
2219 	}
2220 
2221 	fw_ptr = fw->data + 644;
2222 	frag_len = 0;
2223 
2224 	while (fw_ptr - fw->data < fw->size) {
2225 		struct hci_command_hdr *cmd = (void *)(fw_ptr + frag_len);
2226 
2227 		frag_len += sizeof(*cmd) + cmd->plen;
2228 
2229 		/* The parameter length of the secure send command requires
2230 		 * a 4 byte alignment. It happens so that the firmware file
2231 		 * contains proper Intel_NOP commands to align the fragments
2232 		 * as needed.
2233 		 *
2234 		 * Send set of commands with 4 byte alignment from the
2235 		 * firmware data buffer as a single Data fragement.
2236 		 */
2237 		if (!(frag_len % 4)) {
2238 			err = btintel_secure_send(hdev, 0x01, frag_len, fw_ptr);
2239 			if (err < 0) {
2240 				BT_ERR("%s: Failed to send firmware data (%d)",
2241 				       hdev->name, err);
2242 				goto done;
2243 			}
2244 
2245 			fw_ptr += frag_len;
2246 			frag_len = 0;
2247 		}
2248 	}
2249 
2250 	set_bit(BTUSB_FIRMWARE_LOADED, &data->flags);
2251 
2252 	BT_INFO("%s: Waiting for firmware download to complete", hdev->name);
2253 
2254 	/* Before switching the device into operational mode and with that
2255 	 * booting the loaded firmware, wait for the bootloader notification
2256 	 * that all fragments have been successfully received.
2257 	 *
2258 	 * When the event processing receives the notification, then the
2259 	 * BTUSB_DOWNLOADING flag will be cleared.
2260 	 *
2261 	 * The firmware loading should not take longer than 5 seconds
2262 	 * and thus just timeout if that happens and fail the setup
2263 	 * of this device.
2264 	 */
2265 	err = wait_on_bit_timeout(&data->flags, BTUSB_DOWNLOADING,
2266 				  TASK_INTERRUPTIBLE,
2267 				  msecs_to_jiffies(5000));
2268 	if (err == -EINTR) {
2269 		BT_ERR("%s: Firmware loading interrupted", hdev->name);
2270 		goto done;
2271 	}
2272 
2273 	if (err) {
2274 		BT_ERR("%s: Firmware loading timeout", hdev->name);
2275 		err = -ETIMEDOUT;
2276 		goto done;
2277 	}
2278 
2279 	if (test_bit(BTUSB_FIRMWARE_FAILED, &data->flags)) {
2280 		BT_ERR("%s: Firmware loading failed", hdev->name);
2281 		err = -ENOEXEC;
2282 		goto done;
2283 	}
2284 
2285 	rettime = ktime_get();
2286 	delta = ktime_sub(rettime, calltime);
2287 	duration = (unsigned long long) ktime_to_ns(delta) >> 10;
2288 
2289 	BT_INFO("%s: Firmware loaded in %llu usecs", hdev->name, duration);
2290 
2291 done:
2292 	release_firmware(fw);
2293 
2294 	if (err < 0)
2295 		return err;
2296 
2297 	calltime = ktime_get();
2298 
2299 	set_bit(BTUSB_BOOTING, &data->flags);
2300 
2301 	skb = __hci_cmd_sync(hdev, 0xfc01, sizeof(reset_param), reset_param,
2302 			     HCI_INIT_TIMEOUT);
2303 	if (IS_ERR(skb))
2304 		return PTR_ERR(skb);
2305 
2306 	kfree_skb(skb);
2307 
2308 	/* The bootloader will not indicate when the device is ready. This
2309 	 * is done by the operational firmware sending bootup notification.
2310 	 *
2311 	 * Booting into operational firmware should not take longer than
2312 	 * 1 second. However if that happens, then just fail the setup
2313 	 * since something went wrong.
2314 	 */
2315 	BT_INFO("%s: Waiting for device to boot", hdev->name);
2316 
2317 	err = wait_on_bit_timeout(&data->flags, BTUSB_BOOTING,
2318 				  TASK_INTERRUPTIBLE,
2319 				  msecs_to_jiffies(1000));
2320 
2321 	if (err == -EINTR) {
2322 		BT_ERR("%s: Device boot interrupted", hdev->name);
2323 		return -EINTR;
2324 	}
2325 
2326 	if (err) {
2327 		BT_ERR("%s: Device boot timeout", hdev->name);
2328 		return -ETIMEDOUT;
2329 	}
2330 
2331 	rettime = ktime_get();
2332 	delta = ktime_sub(rettime, calltime);
2333 	duration = (unsigned long long) ktime_to_ns(delta) >> 10;
2334 
2335 	BT_INFO("%s: Device booted in %llu usecs", hdev->name, duration);
2336 
2337 	clear_bit(BTUSB_BOOTLOADER, &data->flags);
2338 
2339 	/* Once the device is running in operational mode, it needs to apply
2340 	 * the device configuration (DDC) parameters.
2341 	 *
2342 	 * The device can work without DDC parameters, so even if it fails
2343 	 * to load the file, no need to fail the setup.
2344 	 */
2345 	btintel_load_ddc_config(hdev, fwname);
2346 
2347 	/* Set the event mask for Intel specific vendor events. This enables
2348 	 * a few extra events that are useful during general operation. It
2349 	 * does not enable any debugging related events.
2350 	 *
2351 	 * The device will function correctly without these events enabled
2352 	 * and thus no need to fail the setup.
2353 	 */
2354 	btintel_set_event_mask(hdev, false);
2355 
2356 	return 0;
2357 }
2358 
2359 static int btusb_shutdown_intel(struct hci_dev *hdev)
2360 {
2361 	struct sk_buff *skb;
2362 	long ret;
2363 
2364 	/* Some platforms have an issue with BT LED when the interface is
2365 	 * down or BT radio is turned off, which takes 5 seconds to BT LED
2366 	 * goes off. This command turns off the BT LED immediately.
2367 	 */
2368 	skb = __hci_cmd_sync(hdev, 0xfc3f, 0, NULL, HCI_INIT_TIMEOUT);
2369 	if (IS_ERR(skb)) {
2370 		ret = PTR_ERR(skb);
2371 		BT_ERR("%s: turning off Intel device LED failed (%ld)",
2372 		       hdev->name, ret);
2373 		return ret;
2374 	}
2375 	kfree_skb(skb);
2376 
2377 	return 0;
2378 }
2379 
2380 #ifdef CONFIG_PM
2381 /* Configure an out-of-band gpio as wake-up pin, if specified in device tree */
2382 static int marvell_config_oob_wake(struct hci_dev *hdev)
2383 {
2384 	struct sk_buff *skb;
2385 	struct btusb_data *data = hci_get_drvdata(hdev);
2386 	struct device *dev = &data->udev->dev;
2387 	u16 pin, gap, opcode;
2388 	int ret;
2389 	u8 cmd[5];
2390 
2391 	/* Move on if no wakeup pin specified */
2392 	if (of_property_read_u16(dev->of_node, "marvell,wakeup-pin", &pin) ||
2393 	    of_property_read_u16(dev->of_node, "marvell,wakeup-gap-ms", &gap))
2394 		return 0;
2395 
2396 	/* Vendor specific command to configure a GPIO as wake-up pin */
2397 	opcode = hci_opcode_pack(0x3F, 0x59);
2398 	cmd[0] = opcode & 0xFF;
2399 	cmd[1] = opcode >> 8;
2400 	cmd[2] = 2; /* length of parameters that follow */
2401 	cmd[3] = pin;
2402 	cmd[4] = gap; /* time in ms, for which wakeup pin should be asserted */
2403 
2404 	skb = bt_skb_alloc(sizeof(cmd), GFP_KERNEL);
2405 	if (!skb) {
2406 		bt_dev_err(hdev, "%s: No memory\n", __func__);
2407 		return -ENOMEM;
2408 	}
2409 
2410 	skb_put_data(skb, cmd, sizeof(cmd));
2411 	hci_skb_pkt_type(skb) = HCI_COMMAND_PKT;
2412 
2413 	ret = btusb_send_frame(hdev, skb);
2414 	if (ret) {
2415 		bt_dev_err(hdev, "%s: configuration failed\n", __func__);
2416 		kfree_skb(skb);
2417 		return ret;
2418 	}
2419 
2420 	return 0;
2421 }
2422 #endif
2423 
2424 static int btusb_set_bdaddr_marvell(struct hci_dev *hdev,
2425 				    const bdaddr_t *bdaddr)
2426 {
2427 	struct sk_buff *skb;
2428 	u8 buf[8];
2429 	long ret;
2430 
2431 	buf[0] = 0xfe;
2432 	buf[1] = sizeof(bdaddr_t);
2433 	memcpy(buf + 2, bdaddr, sizeof(bdaddr_t));
2434 
2435 	skb = __hci_cmd_sync(hdev, 0xfc22, sizeof(buf), buf, HCI_INIT_TIMEOUT);
2436 	if (IS_ERR(skb)) {
2437 		ret = PTR_ERR(skb);
2438 		BT_ERR("%s: changing Marvell device address failed (%ld)",
2439 		       hdev->name, ret);
2440 		return ret;
2441 	}
2442 	kfree_skb(skb);
2443 
2444 	return 0;
2445 }
2446 
2447 static int btusb_set_bdaddr_ath3012(struct hci_dev *hdev,
2448 				    const bdaddr_t *bdaddr)
2449 {
2450 	struct sk_buff *skb;
2451 	u8 buf[10];
2452 	long ret;
2453 
2454 	buf[0] = 0x01;
2455 	buf[1] = 0x01;
2456 	buf[2] = 0x00;
2457 	buf[3] = sizeof(bdaddr_t);
2458 	memcpy(buf + 4, bdaddr, sizeof(bdaddr_t));
2459 
2460 	skb = __hci_cmd_sync(hdev, 0xfc0b, sizeof(buf), buf, HCI_INIT_TIMEOUT);
2461 	if (IS_ERR(skb)) {
2462 		ret = PTR_ERR(skb);
2463 		BT_ERR("%s: Change address command failed (%ld)",
2464 		       hdev->name, ret);
2465 		return ret;
2466 	}
2467 	kfree_skb(skb);
2468 
2469 	return 0;
2470 }
2471 
2472 #define QCA_DFU_PACKET_LEN	4096
2473 
2474 #define QCA_GET_TARGET_VERSION	0x09
2475 #define QCA_CHECK_STATUS	0x05
2476 #define QCA_DFU_DOWNLOAD	0x01
2477 
2478 #define QCA_SYSCFG_UPDATED	0x40
2479 #define QCA_PATCH_UPDATED	0x80
2480 #define QCA_DFU_TIMEOUT		3000
2481 
2482 struct qca_version {
2483 	__le32	rom_version;
2484 	__le32	patch_version;
2485 	__le32	ram_version;
2486 	__le32	ref_clock;
2487 	__u8	reserved[4];
2488 } __packed;
2489 
2490 struct qca_rampatch_version {
2491 	__le16	rom_version;
2492 	__le16	patch_version;
2493 } __packed;
2494 
2495 struct qca_device_info {
2496 	u32	rom_version;
2497 	u8	rampatch_hdr;	/* length of header in rampatch */
2498 	u8	nvm_hdr;	/* length of header in NVM */
2499 	u8	ver_offset;	/* offset of version structure in rampatch */
2500 };
2501 
2502 static const struct qca_device_info qca_devices_table[] = {
2503 	{ 0x00000100, 20, 4, 10 }, /* Rome 1.0 */
2504 	{ 0x00000101, 20, 4, 10 }, /* Rome 1.1 */
2505 	{ 0x00000200, 28, 4, 18 }, /* Rome 2.0 */
2506 	{ 0x00000201, 28, 4, 18 }, /* Rome 2.1 */
2507 	{ 0x00000300, 28, 4, 18 }, /* Rome 3.0 */
2508 	{ 0x00000302, 28, 4, 18 }, /* Rome 3.2 */
2509 };
2510 
2511 static int btusb_qca_send_vendor_req(struct hci_dev *hdev, u8 request,
2512 				     void *data, u16 size)
2513 {
2514 	struct btusb_data *btdata = hci_get_drvdata(hdev);
2515 	struct usb_device *udev = btdata->udev;
2516 	int pipe, err;
2517 	u8 *buf;
2518 
2519 	buf = kmalloc(size, GFP_KERNEL);
2520 	if (!buf)
2521 		return -ENOMEM;
2522 
2523 	/* Found some of USB hosts have IOT issues with ours so that we should
2524 	 * not wait until HCI layer is ready.
2525 	 */
2526 	pipe = usb_rcvctrlpipe(udev, 0);
2527 	err = usb_control_msg(udev, pipe, request, USB_TYPE_VENDOR | USB_DIR_IN,
2528 			      0, 0, buf, size, USB_CTRL_SET_TIMEOUT);
2529 	if (err < 0) {
2530 		BT_ERR("%s: Failed to access otp area (%d)", hdev->name, err);
2531 		goto done;
2532 	}
2533 
2534 	memcpy(data, buf, size);
2535 
2536 done:
2537 	kfree(buf);
2538 
2539 	return err;
2540 }
2541 
2542 static int btusb_setup_qca_download_fw(struct hci_dev *hdev,
2543 				       const struct firmware *firmware,
2544 				       size_t hdr_size)
2545 {
2546 	struct btusb_data *btdata = hci_get_drvdata(hdev);
2547 	struct usb_device *udev = btdata->udev;
2548 	size_t count, size, sent = 0;
2549 	int pipe, len, err;
2550 	u8 *buf;
2551 
2552 	buf = kmalloc(QCA_DFU_PACKET_LEN, GFP_KERNEL);
2553 	if (!buf)
2554 		return -ENOMEM;
2555 
2556 	count = firmware->size;
2557 
2558 	size = min_t(size_t, count, hdr_size);
2559 	memcpy(buf, firmware->data, size);
2560 
2561 	/* USB patches should go down to controller through USB path
2562 	 * because binary format fits to go down through USB channel.
2563 	 * USB control path is for patching headers and USB bulk is for
2564 	 * patch body.
2565 	 */
2566 	pipe = usb_sndctrlpipe(udev, 0);
2567 	err = usb_control_msg(udev, pipe, QCA_DFU_DOWNLOAD, USB_TYPE_VENDOR,
2568 			      0, 0, buf, size, USB_CTRL_SET_TIMEOUT);
2569 	if (err < 0) {
2570 		BT_ERR("%s: Failed to send headers (%d)", hdev->name, err);
2571 		goto done;
2572 	}
2573 
2574 	sent += size;
2575 	count -= size;
2576 
2577 	while (count) {
2578 		size = min_t(size_t, count, QCA_DFU_PACKET_LEN);
2579 
2580 		memcpy(buf, firmware->data + sent, size);
2581 
2582 		pipe = usb_sndbulkpipe(udev, 0x02);
2583 		err = usb_bulk_msg(udev, pipe, buf, size, &len,
2584 				   QCA_DFU_TIMEOUT);
2585 		if (err < 0) {
2586 			BT_ERR("%s: Failed to send body at %zd of %zd (%d)",
2587 			       hdev->name, sent, firmware->size, err);
2588 			break;
2589 		}
2590 
2591 		if (size != len) {
2592 			BT_ERR("%s: Failed to get bulk buffer", hdev->name);
2593 			err = -EILSEQ;
2594 			break;
2595 		}
2596 
2597 		sent  += size;
2598 		count -= size;
2599 	}
2600 
2601 done:
2602 	kfree(buf);
2603 	return err;
2604 }
2605 
2606 static int btusb_setup_qca_load_rampatch(struct hci_dev *hdev,
2607 					 struct qca_version *ver,
2608 					 const struct qca_device_info *info)
2609 {
2610 	struct qca_rampatch_version *rver;
2611 	const struct firmware *fw;
2612 	u32 ver_rom, ver_patch;
2613 	u16 rver_rom, rver_patch;
2614 	char fwname[64];
2615 	int err;
2616 
2617 	ver_rom = le32_to_cpu(ver->rom_version);
2618 	ver_patch = le32_to_cpu(ver->patch_version);
2619 
2620 	snprintf(fwname, sizeof(fwname), "qca/rampatch_usb_%08x.bin", ver_rom);
2621 
2622 	err = request_firmware(&fw, fwname, &hdev->dev);
2623 	if (err) {
2624 		BT_ERR("%s: failed to request rampatch file: %s (%d)",
2625 		       hdev->name, fwname, err);
2626 		return err;
2627 	}
2628 
2629 	BT_INFO("%s: using rampatch file: %s", hdev->name, fwname);
2630 
2631 	rver = (struct qca_rampatch_version *)(fw->data + info->ver_offset);
2632 	rver_rom = le16_to_cpu(rver->rom_version);
2633 	rver_patch = le16_to_cpu(rver->patch_version);
2634 
2635 	BT_INFO("%s: QCA: patch rome 0x%x build 0x%x, firmware rome 0x%x "
2636 		"build 0x%x", hdev->name, rver_rom, rver_patch, ver_rom,
2637 		ver_patch);
2638 
2639 	if (rver_rom != ver_rom || rver_patch <= ver_patch) {
2640 		BT_ERR("%s: rampatch file version did not match with firmware",
2641 		       hdev->name);
2642 		err = -EINVAL;
2643 		goto done;
2644 	}
2645 
2646 	err = btusb_setup_qca_download_fw(hdev, fw, info->rampatch_hdr);
2647 
2648 done:
2649 	release_firmware(fw);
2650 
2651 	return err;
2652 }
2653 
2654 static int btusb_setup_qca_load_nvm(struct hci_dev *hdev,
2655 				    struct qca_version *ver,
2656 				    const struct qca_device_info *info)
2657 {
2658 	const struct firmware *fw;
2659 	char fwname[64];
2660 	int err;
2661 
2662 	snprintf(fwname, sizeof(fwname), "qca/nvm_usb_%08x.bin",
2663 		 le32_to_cpu(ver->rom_version));
2664 
2665 	err = request_firmware(&fw, fwname, &hdev->dev);
2666 	if (err) {
2667 		BT_ERR("%s: failed to request NVM file: %s (%d)",
2668 		       hdev->name, fwname, err);
2669 		return err;
2670 	}
2671 
2672 	BT_INFO("%s: using NVM file: %s", hdev->name, fwname);
2673 
2674 	err = btusb_setup_qca_download_fw(hdev, fw, info->nvm_hdr);
2675 
2676 	release_firmware(fw);
2677 
2678 	return err;
2679 }
2680 
2681 static int btusb_setup_qca(struct hci_dev *hdev)
2682 {
2683 	const struct qca_device_info *info = NULL;
2684 	struct qca_version ver;
2685 	u32 ver_rom;
2686 	u8 status;
2687 	int i, err;
2688 
2689 	err = btusb_qca_send_vendor_req(hdev, QCA_GET_TARGET_VERSION, &ver,
2690 					sizeof(ver));
2691 	if (err < 0)
2692 		return err;
2693 
2694 	ver_rom = le32_to_cpu(ver.rom_version);
2695 	for (i = 0; i < ARRAY_SIZE(qca_devices_table); i++) {
2696 		if (ver_rom == qca_devices_table[i].rom_version)
2697 			info = &qca_devices_table[i];
2698 	}
2699 	if (!info) {
2700 		BT_ERR("%s: don't support firmware rome 0x%x", hdev->name,
2701 		       ver_rom);
2702 		return -ENODEV;
2703 	}
2704 
2705 	err = btusb_qca_send_vendor_req(hdev, QCA_CHECK_STATUS, &status,
2706 					sizeof(status));
2707 	if (err < 0)
2708 		return err;
2709 
2710 	if (!(status & QCA_PATCH_UPDATED)) {
2711 		err = btusb_setup_qca_load_rampatch(hdev, &ver, info);
2712 		if (err < 0)
2713 			return err;
2714 	}
2715 
2716 	if (!(status & QCA_SYSCFG_UPDATED)) {
2717 		err = btusb_setup_qca_load_nvm(hdev, &ver, info);
2718 		if (err < 0)
2719 			return err;
2720 	}
2721 
2722 	return 0;
2723 }
2724 
2725 #ifdef CONFIG_BT_HCIBTUSB_BCM
2726 static inline int __set_diag_interface(struct hci_dev *hdev)
2727 {
2728 	struct btusb_data *data = hci_get_drvdata(hdev);
2729 	struct usb_interface *intf = data->diag;
2730 	int i;
2731 
2732 	if (!data->diag)
2733 		return -ENODEV;
2734 
2735 	data->diag_tx_ep = NULL;
2736 	data->diag_rx_ep = NULL;
2737 
2738 	for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
2739 		struct usb_endpoint_descriptor *ep_desc;
2740 
2741 		ep_desc = &intf->cur_altsetting->endpoint[i].desc;
2742 
2743 		if (!data->diag_tx_ep && usb_endpoint_is_bulk_out(ep_desc)) {
2744 			data->diag_tx_ep = ep_desc;
2745 			continue;
2746 		}
2747 
2748 		if (!data->diag_rx_ep && usb_endpoint_is_bulk_in(ep_desc)) {
2749 			data->diag_rx_ep = ep_desc;
2750 			continue;
2751 		}
2752 	}
2753 
2754 	if (!data->diag_tx_ep || !data->diag_rx_ep) {
2755 		BT_ERR("%s invalid diagnostic descriptors", hdev->name);
2756 		return -ENODEV;
2757 	}
2758 
2759 	return 0;
2760 }
2761 
2762 static struct urb *alloc_diag_urb(struct hci_dev *hdev, bool enable)
2763 {
2764 	struct btusb_data *data = hci_get_drvdata(hdev);
2765 	struct sk_buff *skb;
2766 	struct urb *urb;
2767 	unsigned int pipe;
2768 
2769 	if (!data->diag_tx_ep)
2770 		return ERR_PTR(-ENODEV);
2771 
2772 	urb = usb_alloc_urb(0, GFP_KERNEL);
2773 	if (!urb)
2774 		return ERR_PTR(-ENOMEM);
2775 
2776 	skb = bt_skb_alloc(2, GFP_KERNEL);
2777 	if (!skb) {
2778 		usb_free_urb(urb);
2779 		return ERR_PTR(-ENOMEM);
2780 	}
2781 
2782 	skb_put_u8(skb, 0xf0);
2783 	skb_put_u8(skb, enable);
2784 
2785 	pipe = usb_sndbulkpipe(data->udev, data->diag_tx_ep->bEndpointAddress);
2786 
2787 	usb_fill_bulk_urb(urb, data->udev, pipe,
2788 			  skb->data, skb->len, btusb_tx_complete, skb);
2789 
2790 	skb->dev = (void *)hdev;
2791 
2792 	return urb;
2793 }
2794 
2795 static int btusb_bcm_set_diag(struct hci_dev *hdev, bool enable)
2796 {
2797 	struct btusb_data *data = hci_get_drvdata(hdev);
2798 	struct urb *urb;
2799 
2800 	if (!data->diag)
2801 		return -ENODEV;
2802 
2803 	if (!test_bit(HCI_RUNNING, &hdev->flags))
2804 		return -ENETDOWN;
2805 
2806 	urb = alloc_diag_urb(hdev, enable);
2807 	if (IS_ERR(urb))
2808 		return PTR_ERR(urb);
2809 
2810 	return submit_or_queue_tx_urb(hdev, urb);
2811 }
2812 #endif
2813 
2814 #ifdef CONFIG_PM
2815 static irqreturn_t btusb_oob_wake_handler(int irq, void *priv)
2816 {
2817 	struct btusb_data *data = priv;
2818 
2819 	pm_wakeup_event(&data->udev->dev, 0);
2820 	pm_system_wakeup();
2821 
2822 	/* Disable only if not already disabled (keep it balanced) */
2823 	if (test_and_clear_bit(BTUSB_OOB_WAKE_ENABLED, &data->flags)) {
2824 		disable_irq_nosync(irq);
2825 		disable_irq_wake(irq);
2826 	}
2827 	return IRQ_HANDLED;
2828 }
2829 
2830 static const struct of_device_id btusb_match_table[] = {
2831 	{ .compatible = "usb1286,204e" },
2832 	{ }
2833 };
2834 MODULE_DEVICE_TABLE(of, btusb_match_table);
2835 
2836 /* Use an oob wakeup pin? */
2837 static int btusb_config_oob_wake(struct hci_dev *hdev)
2838 {
2839 	struct btusb_data *data = hci_get_drvdata(hdev);
2840 	struct device *dev = &data->udev->dev;
2841 	int irq, ret;
2842 
2843 	clear_bit(BTUSB_OOB_WAKE_ENABLED, &data->flags);
2844 
2845 	if (!of_match_device(btusb_match_table, dev))
2846 		return 0;
2847 
2848 	/* Move on if no IRQ specified */
2849 	irq = of_irq_get_byname(dev->of_node, "wakeup");
2850 	if (irq <= 0) {
2851 		bt_dev_dbg(hdev, "%s: no OOB Wakeup IRQ in DT", __func__);
2852 		return 0;
2853 	}
2854 
2855 	ret = devm_request_irq(&hdev->dev, irq, btusb_oob_wake_handler,
2856 			       0, "OOB Wake-on-BT", data);
2857 	if (ret) {
2858 		bt_dev_err(hdev, "%s: IRQ request failed", __func__);
2859 		return ret;
2860 	}
2861 
2862 	ret = device_init_wakeup(dev, true);
2863 	if (ret) {
2864 		bt_dev_err(hdev, "%s: failed to init_wakeup", __func__);
2865 		return ret;
2866 	}
2867 
2868 	data->oob_wake_irq = irq;
2869 	disable_irq(irq);
2870 	bt_dev_info(hdev, "OOB Wake-on-BT configured at IRQ %u", irq);
2871 	return 0;
2872 }
2873 #endif
2874 
2875 static int btusb_probe(struct usb_interface *intf,
2876 		       const struct usb_device_id *id)
2877 {
2878 	struct usb_endpoint_descriptor *ep_desc;
2879 	struct btusb_data *data;
2880 	struct hci_dev *hdev;
2881 	unsigned ifnum_base;
2882 	int i, err;
2883 
2884 	BT_DBG("intf %p id %p", intf, id);
2885 
2886 	/* interface numbers are hardcoded in the spec */
2887 	if (intf->cur_altsetting->desc.bInterfaceNumber != 0) {
2888 		if (!(id->driver_info & BTUSB_IFNUM_2))
2889 			return -ENODEV;
2890 		if (intf->cur_altsetting->desc.bInterfaceNumber != 2)
2891 			return -ENODEV;
2892 	}
2893 
2894 	ifnum_base = intf->cur_altsetting->desc.bInterfaceNumber;
2895 
2896 	if (!id->driver_info) {
2897 		const struct usb_device_id *match;
2898 
2899 		match = usb_match_id(intf, blacklist_table);
2900 		if (match)
2901 			id = match;
2902 	}
2903 
2904 	if (id->driver_info == BTUSB_IGNORE)
2905 		return -ENODEV;
2906 
2907 	if (id->driver_info & BTUSB_ATH3012) {
2908 		struct usb_device *udev = interface_to_usbdev(intf);
2909 
2910 		/* Old firmware would otherwise let ath3k driver load
2911 		 * patch and sysconfig files
2912 		 */
2913 		if (le16_to_cpu(udev->descriptor.bcdDevice) <= 0x0001)
2914 			return -ENODEV;
2915 	}
2916 
2917 	data = devm_kzalloc(&intf->dev, sizeof(*data), GFP_KERNEL);
2918 	if (!data)
2919 		return -ENOMEM;
2920 
2921 	for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
2922 		ep_desc = &intf->cur_altsetting->endpoint[i].desc;
2923 
2924 		if (!data->intr_ep && usb_endpoint_is_int_in(ep_desc)) {
2925 			data->intr_ep = ep_desc;
2926 			continue;
2927 		}
2928 
2929 		if (!data->bulk_tx_ep && usb_endpoint_is_bulk_out(ep_desc)) {
2930 			data->bulk_tx_ep = ep_desc;
2931 			continue;
2932 		}
2933 
2934 		if (!data->bulk_rx_ep && usb_endpoint_is_bulk_in(ep_desc)) {
2935 			data->bulk_rx_ep = ep_desc;
2936 			continue;
2937 		}
2938 	}
2939 
2940 	if (!data->intr_ep || !data->bulk_tx_ep || !data->bulk_rx_ep)
2941 		return -ENODEV;
2942 
2943 	if (id->driver_info & BTUSB_AMP) {
2944 		data->cmdreq_type = USB_TYPE_CLASS | 0x01;
2945 		data->cmdreq = 0x2b;
2946 	} else {
2947 		data->cmdreq_type = USB_TYPE_CLASS;
2948 		data->cmdreq = 0x00;
2949 	}
2950 
2951 	data->udev = interface_to_usbdev(intf);
2952 	data->intf = intf;
2953 
2954 	INIT_WORK(&data->work, btusb_work);
2955 	INIT_WORK(&data->waker, btusb_waker);
2956 	init_usb_anchor(&data->deferred);
2957 	init_usb_anchor(&data->tx_anchor);
2958 	spin_lock_init(&data->txlock);
2959 
2960 	init_usb_anchor(&data->intr_anchor);
2961 	init_usb_anchor(&data->bulk_anchor);
2962 	init_usb_anchor(&data->isoc_anchor);
2963 	init_usb_anchor(&data->diag_anchor);
2964 	spin_lock_init(&data->rxlock);
2965 
2966 	if (id->driver_info & BTUSB_INTEL_NEW) {
2967 		data->recv_event = btusb_recv_event_intel;
2968 		data->recv_bulk = btusb_recv_bulk_intel;
2969 		set_bit(BTUSB_BOOTLOADER, &data->flags);
2970 	} else {
2971 		data->recv_event = hci_recv_frame;
2972 		data->recv_bulk = btusb_recv_bulk;
2973 	}
2974 
2975 	hdev = hci_alloc_dev();
2976 	if (!hdev)
2977 		return -ENOMEM;
2978 
2979 	hdev->bus = HCI_USB;
2980 	hci_set_drvdata(hdev, data);
2981 
2982 	if (id->driver_info & BTUSB_AMP)
2983 		hdev->dev_type = HCI_AMP;
2984 	else
2985 		hdev->dev_type = HCI_PRIMARY;
2986 
2987 	data->hdev = hdev;
2988 
2989 	SET_HCIDEV_DEV(hdev, &intf->dev);
2990 
2991 	hdev->open   = btusb_open;
2992 	hdev->close  = btusb_close;
2993 	hdev->flush  = btusb_flush;
2994 	hdev->send   = btusb_send_frame;
2995 	hdev->notify = btusb_notify;
2996 
2997 #ifdef CONFIG_PM
2998 	err = btusb_config_oob_wake(hdev);
2999 	if (err)
3000 		goto out_free_dev;
3001 
3002 	/* Marvell devices may need a specific chip configuration */
3003 	if (id->driver_info & BTUSB_MARVELL && data->oob_wake_irq) {
3004 		err = marvell_config_oob_wake(hdev);
3005 		if (err)
3006 			goto out_free_dev;
3007 	}
3008 #endif
3009 	if (id->driver_info & BTUSB_CW6622)
3010 		set_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks);
3011 
3012 	if (id->driver_info & BTUSB_BCM2045)
3013 		set_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks);
3014 
3015 	if (id->driver_info & BTUSB_BCM92035)
3016 		hdev->setup = btusb_setup_bcm92035;
3017 
3018 #ifdef CONFIG_BT_HCIBTUSB_BCM
3019 	if (id->driver_info & BTUSB_BCM_PATCHRAM) {
3020 		hdev->manufacturer = 15;
3021 		hdev->setup = btbcm_setup_patchram;
3022 		hdev->set_diag = btusb_bcm_set_diag;
3023 		hdev->set_bdaddr = btbcm_set_bdaddr;
3024 
3025 		/* Broadcom LM_DIAG Interface numbers are hardcoded */
3026 		data->diag = usb_ifnum_to_if(data->udev, ifnum_base + 2);
3027 	}
3028 
3029 	if (id->driver_info & BTUSB_BCM_APPLE) {
3030 		hdev->manufacturer = 15;
3031 		hdev->setup = btbcm_setup_apple;
3032 		hdev->set_diag = btusb_bcm_set_diag;
3033 
3034 		/* Broadcom LM_DIAG Interface numbers are hardcoded */
3035 		data->diag = usb_ifnum_to_if(data->udev, ifnum_base + 2);
3036 	}
3037 #endif
3038 
3039 	if (id->driver_info & BTUSB_INTEL) {
3040 		hdev->manufacturer = 2;
3041 		hdev->setup = btusb_setup_intel;
3042 		hdev->shutdown = btusb_shutdown_intel;
3043 		hdev->set_diag = btintel_set_diag_mfg;
3044 		hdev->set_bdaddr = btintel_set_bdaddr;
3045 		set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
3046 		set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
3047 		set_bit(HCI_QUIRK_NON_PERSISTENT_DIAG, &hdev->quirks);
3048 	}
3049 
3050 	if (id->driver_info & BTUSB_INTEL_NEW) {
3051 		hdev->manufacturer = 2;
3052 		hdev->send = btusb_send_frame_intel;
3053 		hdev->setup = btusb_setup_intel_new;
3054 		hdev->hw_error = btintel_hw_error;
3055 		hdev->set_diag = btintel_set_diag;
3056 		hdev->set_bdaddr = btintel_set_bdaddr;
3057 		set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
3058 		set_bit(HCI_QUIRK_NON_PERSISTENT_DIAG, &hdev->quirks);
3059 	}
3060 
3061 	if (id->driver_info & BTUSB_MARVELL)
3062 		hdev->set_bdaddr = btusb_set_bdaddr_marvell;
3063 
3064 	if (id->driver_info & BTUSB_SWAVE) {
3065 		set_bit(HCI_QUIRK_FIXUP_INQUIRY_MODE, &hdev->quirks);
3066 		set_bit(HCI_QUIRK_BROKEN_LOCAL_COMMANDS, &hdev->quirks);
3067 	}
3068 
3069 	if (id->driver_info & BTUSB_INTEL_BOOT) {
3070 		hdev->manufacturer = 2;
3071 		set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
3072 	}
3073 
3074 	if (id->driver_info & BTUSB_ATH3012) {
3075 		hdev->set_bdaddr = btusb_set_bdaddr_ath3012;
3076 		set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
3077 		set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
3078 	}
3079 
3080 	if (id->driver_info & BTUSB_QCA_ROME) {
3081 		data->setup_on_usb = btusb_setup_qca;
3082 		hdev->set_bdaddr = btusb_set_bdaddr_ath3012;
3083 
3084 		/* QCA Rome devices lose their updated firmware over suspend,
3085 		 * but the USB hub doesn't notice any status change.
3086 		 * Explicitly request a device reset on resume.
3087 		 */
3088 		set_bit(BTUSB_RESET_RESUME, &data->flags);
3089 	}
3090 
3091 #ifdef CONFIG_BT_HCIBTUSB_RTL
3092 	if (id->driver_info & BTUSB_REALTEK) {
3093 		hdev->setup = btrtl_setup_realtek;
3094 
3095 		/* Realtek devices lose their updated firmware over suspend,
3096 		 * but the USB hub doesn't notice any status change.
3097 		 * Explicitly request a device reset on resume.
3098 		 */
3099 		set_bit(BTUSB_RESET_RESUME, &data->flags);
3100 	}
3101 #endif
3102 
3103 	if (id->driver_info & BTUSB_AMP) {
3104 		/* AMP controllers do not support SCO packets */
3105 		data->isoc = NULL;
3106 	} else {
3107 		/* Interface orders are hardcoded in the specification */
3108 		data->isoc = usb_ifnum_to_if(data->udev, ifnum_base + 1);
3109 	}
3110 
3111 	if (!reset)
3112 		set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
3113 
3114 	if (force_scofix || id->driver_info & BTUSB_WRONG_SCO_MTU) {
3115 		if (!disable_scofix)
3116 			set_bit(HCI_QUIRK_FIXUP_BUFFER_SIZE, &hdev->quirks);
3117 	}
3118 
3119 	if (id->driver_info & BTUSB_BROKEN_ISOC)
3120 		data->isoc = NULL;
3121 
3122 	if (id->driver_info & BTUSB_DIGIANSWER) {
3123 		data->cmdreq_type = USB_TYPE_VENDOR;
3124 		set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
3125 	}
3126 
3127 	if (id->driver_info & BTUSB_CSR) {
3128 		struct usb_device *udev = data->udev;
3129 		u16 bcdDevice = le16_to_cpu(udev->descriptor.bcdDevice);
3130 
3131 		/* Old firmware would otherwise execute USB reset */
3132 		if (bcdDevice < 0x117)
3133 			set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
3134 
3135 		/* Fake CSR devices with broken commands */
3136 		if (bcdDevice <= 0x100 || bcdDevice == 0x134)
3137 			hdev->setup = btusb_setup_csr;
3138 
3139 		set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
3140 	}
3141 
3142 	if (id->driver_info & BTUSB_SNIFFER) {
3143 		struct usb_device *udev = data->udev;
3144 
3145 		/* New sniffer firmware has crippled HCI interface */
3146 		if (le16_to_cpu(udev->descriptor.bcdDevice) > 0x997)
3147 			set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
3148 	}
3149 
3150 	if (id->driver_info & BTUSB_INTEL_BOOT) {
3151 		/* A bug in the bootloader causes that interrupt interface is
3152 		 * only enabled after receiving SetInterface(0, AltSetting=0).
3153 		 */
3154 		err = usb_set_interface(data->udev, 0, 0);
3155 		if (err < 0) {
3156 			BT_ERR("failed to set interface 0, alt 0 %d", err);
3157 			goto out_free_dev;
3158 		}
3159 	}
3160 
3161 	if (data->isoc) {
3162 		err = usb_driver_claim_interface(&btusb_driver,
3163 						 data->isoc, data);
3164 		if (err < 0)
3165 			goto out_free_dev;
3166 	}
3167 
3168 #ifdef CONFIG_BT_HCIBTUSB_BCM
3169 	if (data->diag) {
3170 		if (!usb_driver_claim_interface(&btusb_driver,
3171 						data->diag, data))
3172 			__set_diag_interface(hdev);
3173 		else
3174 			data->diag = NULL;
3175 	}
3176 #endif
3177 
3178 	err = hci_register_dev(hdev);
3179 	if (err < 0)
3180 		goto out_free_dev;
3181 
3182 	usb_set_intfdata(intf, data);
3183 
3184 	return 0;
3185 
3186 out_free_dev:
3187 	hci_free_dev(hdev);
3188 	return err;
3189 }
3190 
3191 static void btusb_disconnect(struct usb_interface *intf)
3192 {
3193 	struct btusb_data *data = usb_get_intfdata(intf);
3194 	struct hci_dev *hdev;
3195 
3196 	BT_DBG("intf %p", intf);
3197 
3198 	if (!data)
3199 		return;
3200 
3201 	hdev = data->hdev;
3202 	usb_set_intfdata(data->intf, NULL);
3203 
3204 	if (data->isoc)
3205 		usb_set_intfdata(data->isoc, NULL);
3206 
3207 	if (data->diag)
3208 		usb_set_intfdata(data->diag, NULL);
3209 
3210 	hci_unregister_dev(hdev);
3211 
3212 	if (intf == data->intf) {
3213 		if (data->isoc)
3214 			usb_driver_release_interface(&btusb_driver, data->isoc);
3215 		if (data->diag)
3216 			usb_driver_release_interface(&btusb_driver, data->diag);
3217 	} else if (intf == data->isoc) {
3218 		if (data->diag)
3219 			usb_driver_release_interface(&btusb_driver, data->diag);
3220 		usb_driver_release_interface(&btusb_driver, data->intf);
3221 	} else if (intf == data->diag) {
3222 		usb_driver_release_interface(&btusb_driver, data->intf);
3223 		if (data->isoc)
3224 			usb_driver_release_interface(&btusb_driver, data->isoc);
3225 	}
3226 
3227 	if (data->oob_wake_irq)
3228 		device_init_wakeup(&data->udev->dev, false);
3229 
3230 	hci_free_dev(hdev);
3231 }
3232 
3233 #ifdef CONFIG_PM
3234 static int btusb_suspend(struct usb_interface *intf, pm_message_t message)
3235 {
3236 	struct btusb_data *data = usb_get_intfdata(intf);
3237 
3238 	BT_DBG("intf %p", intf);
3239 
3240 	if (data->suspend_count++)
3241 		return 0;
3242 
3243 	spin_lock_irq(&data->txlock);
3244 	if (!(PMSG_IS_AUTO(message) && data->tx_in_flight)) {
3245 		set_bit(BTUSB_SUSPENDING, &data->flags);
3246 		spin_unlock_irq(&data->txlock);
3247 	} else {
3248 		spin_unlock_irq(&data->txlock);
3249 		data->suspend_count--;
3250 		return -EBUSY;
3251 	}
3252 
3253 	cancel_work_sync(&data->work);
3254 
3255 	btusb_stop_traffic(data);
3256 	usb_kill_anchored_urbs(&data->tx_anchor);
3257 
3258 	if (data->oob_wake_irq && device_may_wakeup(&data->udev->dev)) {
3259 		set_bit(BTUSB_OOB_WAKE_ENABLED, &data->flags);
3260 		enable_irq_wake(data->oob_wake_irq);
3261 		enable_irq(data->oob_wake_irq);
3262 	}
3263 
3264 	/* Optionally request a device reset on resume, but only when
3265 	 * wakeups are disabled. If wakeups are enabled we assume the
3266 	 * device will stay powered up throughout suspend.
3267 	 */
3268 	if (test_bit(BTUSB_RESET_RESUME, &data->flags) &&
3269 	    !device_may_wakeup(&data->udev->dev))
3270 		data->udev->reset_resume = 1;
3271 
3272 	return 0;
3273 }
3274 
3275 static void play_deferred(struct btusb_data *data)
3276 {
3277 	struct urb *urb;
3278 	int err;
3279 
3280 	while ((urb = usb_get_from_anchor(&data->deferred))) {
3281 		usb_anchor_urb(urb, &data->tx_anchor);
3282 
3283 		err = usb_submit_urb(urb, GFP_ATOMIC);
3284 		if (err < 0) {
3285 			if (err != -EPERM && err != -ENODEV)
3286 				BT_ERR("%s urb %p submission failed (%d)",
3287 				       data->hdev->name, urb, -err);
3288 			kfree(urb->setup_packet);
3289 			usb_unanchor_urb(urb);
3290 			usb_free_urb(urb);
3291 			break;
3292 		}
3293 
3294 		data->tx_in_flight++;
3295 		usb_free_urb(urb);
3296 	}
3297 
3298 	/* Cleanup the rest deferred urbs. */
3299 	while ((urb = usb_get_from_anchor(&data->deferred))) {
3300 		kfree(urb->setup_packet);
3301 		usb_free_urb(urb);
3302 	}
3303 }
3304 
3305 static int btusb_resume(struct usb_interface *intf)
3306 {
3307 	struct btusb_data *data = usb_get_intfdata(intf);
3308 	struct hci_dev *hdev = data->hdev;
3309 	int err = 0;
3310 
3311 	BT_DBG("intf %p", intf);
3312 
3313 	if (--data->suspend_count)
3314 		return 0;
3315 
3316 	/* Disable only if not already disabled (keep it balanced) */
3317 	if (test_and_clear_bit(BTUSB_OOB_WAKE_ENABLED, &data->flags)) {
3318 		disable_irq(data->oob_wake_irq);
3319 		disable_irq_wake(data->oob_wake_irq);
3320 	}
3321 
3322 	if (!test_bit(HCI_RUNNING, &hdev->flags))
3323 		goto done;
3324 
3325 	if (test_bit(BTUSB_INTR_RUNNING, &data->flags)) {
3326 		err = btusb_submit_intr_urb(hdev, GFP_NOIO);
3327 		if (err < 0) {
3328 			clear_bit(BTUSB_INTR_RUNNING, &data->flags);
3329 			goto failed;
3330 		}
3331 	}
3332 
3333 	if (test_bit(BTUSB_BULK_RUNNING, &data->flags)) {
3334 		err = btusb_submit_bulk_urb(hdev, GFP_NOIO);
3335 		if (err < 0) {
3336 			clear_bit(BTUSB_BULK_RUNNING, &data->flags);
3337 			goto failed;
3338 		}
3339 
3340 		btusb_submit_bulk_urb(hdev, GFP_NOIO);
3341 	}
3342 
3343 	if (test_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
3344 		if (btusb_submit_isoc_urb(hdev, GFP_NOIO) < 0)
3345 			clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
3346 		else
3347 			btusb_submit_isoc_urb(hdev, GFP_NOIO);
3348 	}
3349 
3350 	spin_lock_irq(&data->txlock);
3351 	play_deferred(data);
3352 	clear_bit(BTUSB_SUSPENDING, &data->flags);
3353 	spin_unlock_irq(&data->txlock);
3354 	schedule_work(&data->work);
3355 
3356 	return 0;
3357 
3358 failed:
3359 	usb_scuttle_anchored_urbs(&data->deferred);
3360 done:
3361 	spin_lock_irq(&data->txlock);
3362 	clear_bit(BTUSB_SUSPENDING, &data->flags);
3363 	spin_unlock_irq(&data->txlock);
3364 
3365 	return err;
3366 }
3367 #endif
3368 
3369 static struct usb_driver btusb_driver = {
3370 	.name		= "btusb",
3371 	.probe		= btusb_probe,
3372 	.disconnect	= btusb_disconnect,
3373 #ifdef CONFIG_PM
3374 	.suspend	= btusb_suspend,
3375 	.resume		= btusb_resume,
3376 #endif
3377 	.id_table	= btusb_table,
3378 	.supports_autosuspend = 1,
3379 	.disable_hub_initiated_lpm = 1,
3380 };
3381 
3382 module_usb_driver(btusb_driver);
3383 
3384 module_param(disable_scofix, bool, 0644);
3385 MODULE_PARM_DESC(disable_scofix, "Disable fixup of wrong SCO buffer size");
3386 
3387 module_param(force_scofix, bool, 0644);
3388 MODULE_PARM_DESC(force_scofix, "Force fixup of wrong SCO buffers size");
3389 
3390 module_param(reset, bool, 0644);
3391 MODULE_PARM_DESC(reset, "Send HCI reset command on initialization");
3392 
3393 MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
3394 MODULE_DESCRIPTION("Generic Bluetooth USB driver ver " VERSION);
3395 MODULE_VERSION(VERSION);
3396 MODULE_LICENSE("GPL");
3397