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