]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blob - drivers/bluetooth/btusb.c
scsi: lpfc: Fix some error codes in debugfs
[mirror_ubuntu-hirsute-kernel.git] / drivers / bluetooth / btusb.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 *
4 * Generic Bluetooth USB driver
5 *
6 * Copyright (C) 2005-2008 Marcel Holtmann <marcel@holtmann.org>
7 */
8
9 #include <linux/dmi.h>
10 #include <linux/module.h>
11 #include <linux/usb.h>
12 #include <linux/usb/quirks.h>
13 #include <linux/firmware.h>
14 #include <linux/iopoll.h>
15 #include <linux/of_device.h>
16 #include <linux/of_irq.h>
17 #include <linux/pci.h>
18 #include <linux/suspend.h>
19 #include <linux/gpio/consumer.h>
20 #include <asm/unaligned.h>
21
22 #include <net/bluetooth/bluetooth.h>
23 #include <net/bluetooth/hci_core.h>
24
25 #include "btintel.h"
26 #include "btbcm.h"
27 #include "btrtl.h"
28
29 #define VERSION "0.8"
30
31 static bool disable_scofix;
32 static bool force_scofix;
33 static bool enable_autosuspend = IS_ENABLED(CONFIG_BT_HCIBTUSB_AUTOSUSPEND);
34
35 static bool reset = true;
36
37 static struct usb_driver btusb_driver;
38
39 #define BTUSB_IGNORE 0x01
40 #define BTUSB_DIGIANSWER 0x02
41 #define BTUSB_CSR 0x04
42 #define BTUSB_SNIFFER 0x08
43 #define BTUSB_BCM92035 0x10
44 #define BTUSB_BROKEN_ISOC 0x20
45 #define BTUSB_WRONG_SCO_MTU 0x40
46 #define BTUSB_ATH3012 0x80
47 #define BTUSB_INTEL 0x100
48 #define BTUSB_INTEL_BOOT 0x200
49 #define BTUSB_BCM_PATCHRAM 0x400
50 #define BTUSB_MARVELL 0x800
51 #define BTUSB_SWAVE 0x1000
52 #define BTUSB_INTEL_NEW 0x2000
53 #define BTUSB_AMP 0x4000
54 #define BTUSB_QCA_ROME 0x8000
55 #define BTUSB_BCM_APPLE 0x10000
56 #define BTUSB_REALTEK 0x20000
57 #define BTUSB_BCM2045 0x40000
58 #define BTUSB_IFNUM_2 0x80000
59 #define BTUSB_CW6622 0x100000
60 #define BTUSB_MEDIATEK 0x200000
61 #define BTUSB_WIDEBAND_SPEECH 0x400000
62 #define BTUSB_VALID_LE_STATES 0x800000
63 #define BTUSB_QCA_WCN6855 0x1000000
64 #define BTUSB_INTEL_NEWGEN 0x2000000
65
66 static const struct usb_device_id btusb_table[] = {
67 /* Generic Bluetooth USB device */
68 { USB_DEVICE_INFO(0xe0, 0x01, 0x01) },
69
70 /* Generic Bluetooth AMP device */
71 { USB_DEVICE_INFO(0xe0, 0x01, 0x04), .driver_info = BTUSB_AMP },
72
73 /* Generic Bluetooth USB interface */
74 { USB_INTERFACE_INFO(0xe0, 0x01, 0x01) },
75
76 /* Apple-specific (Broadcom) devices */
77 { USB_VENDOR_AND_INTERFACE_INFO(0x05ac, 0xff, 0x01, 0x01),
78 .driver_info = BTUSB_BCM_APPLE | BTUSB_IFNUM_2 },
79
80 /* MediaTek MT76x0E */
81 { USB_DEVICE(0x0e8d, 0x763f) },
82
83 /* Broadcom SoftSailing reporting vendor specific */
84 { USB_DEVICE(0x0a5c, 0x21e1) },
85
86 /* Apple MacBookPro 7,1 */
87 { USB_DEVICE(0x05ac, 0x8213) },
88
89 /* Apple iMac11,1 */
90 { USB_DEVICE(0x05ac, 0x8215) },
91
92 /* Apple MacBookPro6,2 */
93 { USB_DEVICE(0x05ac, 0x8218) },
94
95 /* Apple MacBookAir3,1, MacBookAir3,2 */
96 { USB_DEVICE(0x05ac, 0x821b) },
97
98 /* Apple MacBookAir4,1 */
99 { USB_DEVICE(0x05ac, 0x821f) },
100
101 /* Apple MacBookPro8,2 */
102 { USB_DEVICE(0x05ac, 0x821a) },
103
104 /* Apple MacMini5,1 */
105 { USB_DEVICE(0x05ac, 0x8281) },
106
107 /* AVM BlueFRITZ! USB v2.0 */
108 { USB_DEVICE(0x057c, 0x3800), .driver_info = BTUSB_SWAVE },
109
110 /* Bluetooth Ultraport Module from IBM */
111 { USB_DEVICE(0x04bf, 0x030a) },
112
113 /* ALPS Modules with non-standard id */
114 { USB_DEVICE(0x044e, 0x3001) },
115 { USB_DEVICE(0x044e, 0x3002) },
116
117 /* Ericsson with non-standard id */
118 { USB_DEVICE(0x0bdb, 0x1002) },
119
120 /* Canyon CN-BTU1 with HID interfaces */
121 { USB_DEVICE(0x0c10, 0x0000) },
122
123 /* Broadcom BCM20702A0 */
124 { USB_DEVICE(0x413c, 0x8197) },
125
126 /* Broadcom BCM20702B0 (Dynex/Insignia) */
127 { USB_DEVICE(0x19ff, 0x0239), .driver_info = BTUSB_BCM_PATCHRAM },
128
129 /* Broadcom BCM43142A0 (Foxconn/Lenovo) */
130 { USB_VENDOR_AND_INTERFACE_INFO(0x105b, 0xff, 0x01, 0x01),
131 .driver_info = BTUSB_BCM_PATCHRAM },
132
133 /* Broadcom BCM920703 (HTC Vive) */
134 { USB_VENDOR_AND_INTERFACE_INFO(0x0bb4, 0xff, 0x01, 0x01),
135 .driver_info = BTUSB_BCM_PATCHRAM },
136
137 /* Foxconn - Hon Hai */
138 { USB_VENDOR_AND_INTERFACE_INFO(0x0489, 0xff, 0x01, 0x01),
139 .driver_info = BTUSB_BCM_PATCHRAM },
140
141 /* Lite-On Technology - Broadcom based */
142 { USB_VENDOR_AND_INTERFACE_INFO(0x04ca, 0xff, 0x01, 0x01),
143 .driver_info = BTUSB_BCM_PATCHRAM },
144
145 /* Broadcom devices with vendor specific id */
146 { USB_VENDOR_AND_INTERFACE_INFO(0x0a5c, 0xff, 0x01, 0x01),
147 .driver_info = BTUSB_BCM_PATCHRAM },
148
149 /* ASUSTek Computer - Broadcom based */
150 { USB_VENDOR_AND_INTERFACE_INFO(0x0b05, 0xff, 0x01, 0x01),
151 .driver_info = BTUSB_BCM_PATCHRAM },
152
153 /* Belkin F8065bf - Broadcom based */
154 { USB_VENDOR_AND_INTERFACE_INFO(0x050d, 0xff, 0x01, 0x01),
155 .driver_info = BTUSB_BCM_PATCHRAM },
156
157 /* IMC Networks - Broadcom based */
158 { USB_VENDOR_AND_INTERFACE_INFO(0x13d3, 0xff, 0x01, 0x01),
159 .driver_info = BTUSB_BCM_PATCHRAM },
160
161 /* Dell Computer - Broadcom based */
162 { USB_VENDOR_AND_INTERFACE_INFO(0x413c, 0xff, 0x01, 0x01),
163 .driver_info = BTUSB_BCM_PATCHRAM },
164
165 /* Toshiba Corp - Broadcom based */
166 { USB_VENDOR_AND_INTERFACE_INFO(0x0930, 0xff, 0x01, 0x01),
167 .driver_info = BTUSB_BCM_PATCHRAM },
168
169 /* Intel Bluetooth USB Bootloader (RAM module) */
170 { USB_DEVICE(0x8087, 0x0a5a),
171 .driver_info = BTUSB_INTEL_BOOT | BTUSB_BROKEN_ISOC },
172
173 { } /* Terminating entry */
174 };
175
176 MODULE_DEVICE_TABLE(usb, btusb_table);
177
178 static const struct usb_device_id blacklist_table[] = {
179 /* CSR BlueCore devices */
180 { USB_DEVICE(0x0a12, 0x0001), .driver_info = BTUSB_CSR },
181
182 /* Broadcom BCM2033 without firmware */
183 { USB_DEVICE(0x0a5c, 0x2033), .driver_info = BTUSB_IGNORE },
184
185 /* Broadcom BCM2045 devices */
186 { USB_DEVICE(0x0a5c, 0x2045), .driver_info = BTUSB_BCM2045 },
187
188 /* Atheros 3011 with sflash firmware */
189 { USB_DEVICE(0x0489, 0xe027), .driver_info = BTUSB_IGNORE },
190 { USB_DEVICE(0x0489, 0xe03d), .driver_info = BTUSB_IGNORE },
191 { USB_DEVICE(0x04f2, 0xaff1), .driver_info = BTUSB_IGNORE },
192 { USB_DEVICE(0x0930, 0x0215), .driver_info = BTUSB_IGNORE },
193 { USB_DEVICE(0x0cf3, 0x3002), .driver_info = BTUSB_IGNORE },
194 { USB_DEVICE(0x0cf3, 0xe019), .driver_info = BTUSB_IGNORE },
195 { USB_DEVICE(0x13d3, 0x3304), .driver_info = BTUSB_IGNORE },
196
197 /* Atheros AR9285 Malbec with sflash firmware */
198 { USB_DEVICE(0x03f0, 0x311d), .driver_info = BTUSB_IGNORE },
199
200 /* Atheros 3012 with sflash firmware */
201 { USB_DEVICE(0x0489, 0xe04d), .driver_info = BTUSB_ATH3012 },
202 { USB_DEVICE(0x0489, 0xe04e), .driver_info = BTUSB_ATH3012 },
203 { USB_DEVICE(0x0489, 0xe056), .driver_info = BTUSB_ATH3012 },
204 { USB_DEVICE(0x0489, 0xe057), .driver_info = BTUSB_ATH3012 },
205 { USB_DEVICE(0x0489, 0xe05f), .driver_info = BTUSB_ATH3012 },
206 { USB_DEVICE(0x0489, 0xe076), .driver_info = BTUSB_ATH3012 },
207 { USB_DEVICE(0x0489, 0xe078), .driver_info = BTUSB_ATH3012 },
208 { USB_DEVICE(0x0489, 0xe095), .driver_info = BTUSB_ATH3012 },
209 { USB_DEVICE(0x04c5, 0x1330), .driver_info = BTUSB_ATH3012 },
210 { USB_DEVICE(0x04ca, 0x3004), .driver_info = BTUSB_ATH3012 },
211 { USB_DEVICE(0x04ca, 0x3005), .driver_info = BTUSB_ATH3012 },
212 { USB_DEVICE(0x04ca, 0x3006), .driver_info = BTUSB_ATH3012 },
213 { USB_DEVICE(0x04ca, 0x3007), .driver_info = BTUSB_ATH3012 },
214 { USB_DEVICE(0x04ca, 0x3008), .driver_info = BTUSB_ATH3012 },
215 { USB_DEVICE(0x04ca, 0x300b), .driver_info = BTUSB_ATH3012 },
216 { USB_DEVICE(0x04ca, 0x300d), .driver_info = BTUSB_ATH3012 },
217 { USB_DEVICE(0x04ca, 0x300f), .driver_info = BTUSB_ATH3012 },
218 { USB_DEVICE(0x04ca, 0x3010), .driver_info = BTUSB_ATH3012 },
219 { USB_DEVICE(0x04ca, 0x3014), .driver_info = BTUSB_ATH3012 },
220 { USB_DEVICE(0x04ca, 0x3018), .driver_info = BTUSB_ATH3012 },
221 { USB_DEVICE(0x0930, 0x0219), .driver_info = BTUSB_ATH3012 },
222 { USB_DEVICE(0x0930, 0x021c), .driver_info = BTUSB_ATH3012 },
223 { USB_DEVICE(0x0930, 0x0220), .driver_info = BTUSB_ATH3012 },
224 { USB_DEVICE(0x0930, 0x0227), .driver_info = BTUSB_ATH3012 },
225 { USB_DEVICE(0x0b05, 0x17d0), .driver_info = BTUSB_ATH3012 },
226 { USB_DEVICE(0x0cf3, 0x0036), .driver_info = BTUSB_ATH3012 },
227 { USB_DEVICE(0x0cf3, 0x3004), .driver_info = BTUSB_ATH3012 },
228 { USB_DEVICE(0x0cf3, 0x3008), .driver_info = BTUSB_ATH3012 },
229 { USB_DEVICE(0x0cf3, 0x311d), .driver_info = BTUSB_ATH3012 },
230 { USB_DEVICE(0x0cf3, 0x311e), .driver_info = BTUSB_ATH3012 },
231 { USB_DEVICE(0x0cf3, 0x311f), .driver_info = BTUSB_ATH3012 },
232 { USB_DEVICE(0x0cf3, 0x3121), .driver_info = BTUSB_ATH3012 },
233 { USB_DEVICE(0x0cf3, 0x817a), .driver_info = BTUSB_ATH3012 },
234 { USB_DEVICE(0x0cf3, 0x817b), .driver_info = BTUSB_ATH3012 },
235 { USB_DEVICE(0x0cf3, 0xe003), .driver_info = BTUSB_ATH3012 },
236 { USB_DEVICE(0x0cf3, 0xe004), .driver_info = BTUSB_ATH3012 },
237 { USB_DEVICE(0x0cf3, 0xe005), .driver_info = BTUSB_ATH3012 },
238 { USB_DEVICE(0x0cf3, 0xe006), .driver_info = BTUSB_ATH3012 },
239 { USB_DEVICE(0x13d3, 0x3362), .driver_info = BTUSB_ATH3012 },
240 { USB_DEVICE(0x13d3, 0x3375), .driver_info = BTUSB_ATH3012 },
241 { USB_DEVICE(0x13d3, 0x3393), .driver_info = BTUSB_ATH3012 },
242 { USB_DEVICE(0x13d3, 0x3395), .driver_info = BTUSB_ATH3012 },
243 { USB_DEVICE(0x13d3, 0x3402), .driver_info = BTUSB_ATH3012 },
244 { USB_DEVICE(0x13d3, 0x3408), .driver_info = BTUSB_ATH3012 },
245 { USB_DEVICE(0x13d3, 0x3423), .driver_info = BTUSB_ATH3012 },
246 { USB_DEVICE(0x13d3, 0x3432), .driver_info = BTUSB_ATH3012 },
247 { USB_DEVICE(0x13d3, 0x3472), .driver_info = BTUSB_ATH3012 },
248 { USB_DEVICE(0x13d3, 0x3474), .driver_info = BTUSB_ATH3012 },
249 { USB_DEVICE(0x13d3, 0x3487), .driver_info = BTUSB_ATH3012 },
250 { USB_DEVICE(0x13d3, 0x3490), .driver_info = BTUSB_ATH3012 },
251
252 /* Atheros AR5BBU12 with sflash firmware */
253 { USB_DEVICE(0x0489, 0xe02c), .driver_info = BTUSB_IGNORE },
254
255 /* Atheros AR5BBU12 with sflash firmware */
256 { USB_DEVICE(0x0489, 0xe036), .driver_info = BTUSB_ATH3012 },
257 { USB_DEVICE(0x0489, 0xe03c), .driver_info = BTUSB_ATH3012 },
258
259 /* QCA ROME chipset */
260 { USB_DEVICE(0x0cf3, 0x535b), .driver_info = BTUSB_QCA_ROME |
261 BTUSB_WIDEBAND_SPEECH },
262 { USB_DEVICE(0x0cf3, 0xe007), .driver_info = BTUSB_QCA_ROME |
263 BTUSB_WIDEBAND_SPEECH },
264 { USB_DEVICE(0x0cf3, 0xe009), .driver_info = BTUSB_QCA_ROME |
265 BTUSB_WIDEBAND_SPEECH },
266 { USB_DEVICE(0x0cf3, 0xe010), .driver_info = BTUSB_QCA_ROME |
267 BTUSB_WIDEBAND_SPEECH },
268 { USB_DEVICE(0x0cf3, 0xe300), .driver_info = BTUSB_QCA_ROME |
269 BTUSB_WIDEBAND_SPEECH },
270 { USB_DEVICE(0x0cf3, 0xe301), .driver_info = BTUSB_QCA_ROME |
271 BTUSB_WIDEBAND_SPEECH },
272 { USB_DEVICE(0x0cf3, 0xe360), .driver_info = BTUSB_QCA_ROME |
273 BTUSB_WIDEBAND_SPEECH },
274 { USB_DEVICE(0x0489, 0xe092), .driver_info = BTUSB_QCA_ROME |
275 BTUSB_WIDEBAND_SPEECH },
276 { USB_DEVICE(0x0489, 0xe09f), .driver_info = BTUSB_QCA_ROME |
277 BTUSB_WIDEBAND_SPEECH },
278 { USB_DEVICE(0x0489, 0xe0a2), .driver_info = BTUSB_QCA_ROME |
279 BTUSB_WIDEBAND_SPEECH },
280 { USB_DEVICE(0x04ca, 0x3011), .driver_info = BTUSB_QCA_ROME |
281 BTUSB_WIDEBAND_SPEECH },
282 { USB_DEVICE(0x04ca, 0x3015), .driver_info = BTUSB_QCA_ROME |
283 BTUSB_WIDEBAND_SPEECH },
284 { USB_DEVICE(0x04ca, 0x3016), .driver_info = BTUSB_QCA_ROME |
285 BTUSB_WIDEBAND_SPEECH },
286 { USB_DEVICE(0x04ca, 0x301a), .driver_info = BTUSB_QCA_ROME |
287 BTUSB_WIDEBAND_SPEECH },
288 { USB_DEVICE(0x04ca, 0x3021), .driver_info = BTUSB_QCA_ROME |
289 BTUSB_WIDEBAND_SPEECH },
290 { USB_DEVICE(0x13d3, 0x3491), .driver_info = BTUSB_QCA_ROME |
291 BTUSB_WIDEBAND_SPEECH },
292 { USB_DEVICE(0x13d3, 0x3496), .driver_info = BTUSB_QCA_ROME |
293 BTUSB_WIDEBAND_SPEECH },
294 { USB_DEVICE(0x13d3, 0x3501), .driver_info = BTUSB_QCA_ROME |
295 BTUSB_WIDEBAND_SPEECH },
296
297 /* QCA WCN6855 chipset */
298 { USB_DEVICE(0x0cf3, 0xe600), .driver_info = BTUSB_QCA_WCN6855 |
299 BTUSB_WIDEBAND_SPEECH },
300
301 /* Broadcom BCM2035 */
302 { USB_DEVICE(0x0a5c, 0x2009), .driver_info = BTUSB_BCM92035 },
303 { USB_DEVICE(0x0a5c, 0x200a), .driver_info = BTUSB_WRONG_SCO_MTU },
304 { USB_DEVICE(0x0a5c, 0x2035), .driver_info = BTUSB_WRONG_SCO_MTU },
305
306 /* Broadcom BCM2045 */
307 { USB_DEVICE(0x0a5c, 0x2039), .driver_info = BTUSB_WRONG_SCO_MTU },
308 { USB_DEVICE(0x0a5c, 0x2101), .driver_info = BTUSB_WRONG_SCO_MTU },
309
310 /* IBM/Lenovo ThinkPad with Broadcom chip */
311 { USB_DEVICE(0x0a5c, 0x201e), .driver_info = BTUSB_WRONG_SCO_MTU },
312 { USB_DEVICE(0x0a5c, 0x2110), .driver_info = BTUSB_WRONG_SCO_MTU },
313
314 /* HP laptop with Broadcom chip */
315 { USB_DEVICE(0x03f0, 0x171d), .driver_info = BTUSB_WRONG_SCO_MTU },
316
317 /* Dell laptop with Broadcom chip */
318 { USB_DEVICE(0x413c, 0x8126), .driver_info = BTUSB_WRONG_SCO_MTU },
319
320 /* Dell Wireless 370 and 410 devices */
321 { USB_DEVICE(0x413c, 0x8152), .driver_info = BTUSB_WRONG_SCO_MTU },
322 { USB_DEVICE(0x413c, 0x8156), .driver_info = BTUSB_WRONG_SCO_MTU },
323
324 /* Belkin F8T012 and F8T013 devices */
325 { USB_DEVICE(0x050d, 0x0012), .driver_info = BTUSB_WRONG_SCO_MTU },
326 { USB_DEVICE(0x050d, 0x0013), .driver_info = BTUSB_WRONG_SCO_MTU },
327
328 /* Asus WL-BTD202 device */
329 { USB_DEVICE(0x0b05, 0x1715), .driver_info = BTUSB_WRONG_SCO_MTU },
330
331 /* Kensington Bluetooth USB adapter */
332 { USB_DEVICE(0x047d, 0x105e), .driver_info = BTUSB_WRONG_SCO_MTU },
333
334 /* RTX Telecom based adapters with buggy SCO support */
335 { USB_DEVICE(0x0400, 0x0807), .driver_info = BTUSB_BROKEN_ISOC },
336 { USB_DEVICE(0x0400, 0x080a), .driver_info = BTUSB_BROKEN_ISOC },
337
338 /* CONWISE Technology based adapters with buggy SCO support */
339 { USB_DEVICE(0x0e5e, 0x6622),
340 .driver_info = BTUSB_BROKEN_ISOC | BTUSB_CW6622},
341
342 /* Roper Class 1 Bluetooth Dongle (Silicon Wave based) */
343 { USB_DEVICE(0x1310, 0x0001), .driver_info = BTUSB_SWAVE },
344
345 /* Digianswer devices */
346 { USB_DEVICE(0x08fd, 0x0001), .driver_info = BTUSB_DIGIANSWER },
347 { USB_DEVICE(0x08fd, 0x0002), .driver_info = BTUSB_IGNORE },
348
349 /* CSR BlueCore Bluetooth Sniffer */
350 { USB_DEVICE(0x0a12, 0x0002),
351 .driver_info = BTUSB_SNIFFER | BTUSB_BROKEN_ISOC },
352
353 /* Frontline ComProbe Bluetooth Sniffer */
354 { USB_DEVICE(0x16d3, 0x0002),
355 .driver_info = BTUSB_SNIFFER | BTUSB_BROKEN_ISOC },
356
357 /* Marvell Bluetooth devices */
358 { USB_DEVICE(0x1286, 0x2044), .driver_info = BTUSB_MARVELL },
359 { USB_DEVICE(0x1286, 0x2046), .driver_info = BTUSB_MARVELL },
360 { USB_DEVICE(0x1286, 0x204e), .driver_info = BTUSB_MARVELL },
361
362 /* Intel Bluetooth devices */
363 { USB_DEVICE(0x8087, 0x0025), .driver_info = BTUSB_INTEL_NEW |
364 BTUSB_WIDEBAND_SPEECH |
365 BTUSB_VALID_LE_STATES },
366 { USB_DEVICE(0x8087, 0x0026), .driver_info = BTUSB_INTEL_NEW |
367 BTUSB_WIDEBAND_SPEECH },
368 { USB_DEVICE(0x8087, 0x0029), .driver_info = BTUSB_INTEL_NEW |
369 BTUSB_WIDEBAND_SPEECH },
370 { USB_DEVICE(0x8087, 0x0032), .driver_info = BTUSB_INTEL_NEWGEN |
371 BTUSB_WIDEBAND_SPEECH},
372 { USB_DEVICE(0x8087, 0x07da), .driver_info = BTUSB_CSR },
373 { USB_DEVICE(0x8087, 0x07dc), .driver_info = BTUSB_INTEL },
374 { USB_DEVICE(0x8087, 0x0a2a), .driver_info = BTUSB_INTEL },
375 { USB_DEVICE(0x8087, 0x0a2b), .driver_info = BTUSB_INTEL_NEW |
376 BTUSB_WIDEBAND_SPEECH },
377 { USB_DEVICE(0x8087, 0x0aa7), .driver_info = BTUSB_INTEL |
378 BTUSB_WIDEBAND_SPEECH },
379 { USB_DEVICE(0x8087, 0x0aaa), .driver_info = BTUSB_INTEL_NEW |
380 BTUSB_WIDEBAND_SPEECH |
381 BTUSB_VALID_LE_STATES },
382
383 /* Other Intel Bluetooth devices */
384 { USB_VENDOR_AND_INTERFACE_INFO(0x8087, 0xe0, 0x01, 0x01),
385 .driver_info = BTUSB_IGNORE },
386
387 /* Realtek 8822CE Bluetooth devices */
388 { USB_DEVICE(0x0bda, 0xb00c), .driver_info = BTUSB_REALTEK |
389 BTUSB_WIDEBAND_SPEECH },
390
391 /* Realtek 8852AE Bluetooth devices */
392 { USB_DEVICE(0x0bda, 0xc852), .driver_info = BTUSB_REALTEK |
393 BTUSB_WIDEBAND_SPEECH },
394
395 /* Realtek Bluetooth devices */
396 { USB_VENDOR_AND_INTERFACE_INFO(0x0bda, 0xe0, 0x01, 0x01),
397 .driver_info = BTUSB_REALTEK },
398
399 /* MediaTek Bluetooth devices */
400 { USB_VENDOR_AND_INTERFACE_INFO(0x0e8d, 0xe0, 0x01, 0x01),
401 .driver_info = BTUSB_MEDIATEK },
402
403 /* Additional MediaTek MT7615E Bluetooth devices */
404 { USB_DEVICE(0x13d3, 0x3560), .driver_info = BTUSB_MEDIATEK},
405
406 /* Additional Realtek 8723AE Bluetooth devices */
407 { USB_DEVICE(0x0930, 0x021d), .driver_info = BTUSB_REALTEK },
408 { USB_DEVICE(0x13d3, 0x3394), .driver_info = BTUSB_REALTEK },
409
410 /* Additional Realtek 8723BE Bluetooth devices */
411 { USB_DEVICE(0x0489, 0xe085), .driver_info = BTUSB_REALTEK },
412 { USB_DEVICE(0x0489, 0xe08b), .driver_info = BTUSB_REALTEK },
413 { USB_DEVICE(0x13d3, 0x3410), .driver_info = BTUSB_REALTEK },
414 { USB_DEVICE(0x13d3, 0x3416), .driver_info = BTUSB_REALTEK },
415 { USB_DEVICE(0x13d3, 0x3459), .driver_info = BTUSB_REALTEK },
416 { USB_DEVICE(0x13d3, 0x3494), .driver_info = BTUSB_REALTEK },
417
418 /* Additional Realtek 8723BU Bluetooth devices */
419 { USB_DEVICE(0x7392, 0xa611), .driver_info = BTUSB_REALTEK },
420
421 /* Additional Realtek 8723DE Bluetooth devices */
422 { USB_DEVICE(0x0bda, 0xb009), .driver_info = BTUSB_REALTEK },
423 { USB_DEVICE(0x2ff8, 0xb011), .driver_info = BTUSB_REALTEK },
424
425 /* Additional Realtek 8821AE Bluetooth devices */
426 { USB_DEVICE(0x0b05, 0x17dc), .driver_info = BTUSB_REALTEK },
427 { USB_DEVICE(0x13d3, 0x3414), .driver_info = BTUSB_REALTEK },
428 { USB_DEVICE(0x13d3, 0x3458), .driver_info = BTUSB_REALTEK },
429 { USB_DEVICE(0x13d3, 0x3461), .driver_info = BTUSB_REALTEK },
430 { USB_DEVICE(0x13d3, 0x3462), .driver_info = BTUSB_REALTEK },
431
432 /* Additional Realtek 8822BE Bluetooth devices */
433 { USB_DEVICE(0x13d3, 0x3526), .driver_info = BTUSB_REALTEK },
434 { USB_DEVICE(0x0b05, 0x185c), .driver_info = BTUSB_REALTEK },
435
436 /* Additional Realtek 8822CE Bluetooth devices */
437 { USB_DEVICE(0x04ca, 0x4005), .driver_info = BTUSB_REALTEK |
438 BTUSB_WIDEBAND_SPEECH },
439 { USB_DEVICE(0x04c5, 0x161f), .driver_info = BTUSB_REALTEK |
440 BTUSB_WIDEBAND_SPEECH },
441 { USB_DEVICE(0x0b05, 0x18ef), .driver_info = BTUSB_REALTEK |
442 BTUSB_WIDEBAND_SPEECH },
443 { USB_DEVICE(0x13d3, 0x3548), .driver_info = BTUSB_REALTEK |
444 BTUSB_WIDEBAND_SPEECH },
445 { USB_DEVICE(0x13d3, 0x3549), .driver_info = BTUSB_REALTEK |
446 BTUSB_WIDEBAND_SPEECH },
447 { USB_DEVICE(0x13d3, 0x3553), .driver_info = BTUSB_REALTEK |
448 BTUSB_WIDEBAND_SPEECH },
449 { USB_DEVICE(0x13d3, 0x3555), .driver_info = BTUSB_REALTEK |
450 BTUSB_WIDEBAND_SPEECH },
451 { USB_DEVICE(0x2ff8, 0x3051), .driver_info = BTUSB_REALTEK |
452 BTUSB_WIDEBAND_SPEECH },
453 { USB_DEVICE(0x1358, 0xc123), .driver_info = BTUSB_REALTEK |
454 BTUSB_WIDEBAND_SPEECH },
455 { USB_DEVICE(0x0bda, 0xc123), .driver_info = BTUSB_REALTEK |
456 BTUSB_WIDEBAND_SPEECH },
457
458 /* Silicon Wave based devices */
459 { USB_DEVICE(0x0c10, 0x0000), .driver_info = BTUSB_SWAVE },
460
461 { } /* Terminating entry */
462 };
463
464 /* The Bluetooth USB module build into some devices needs to be reset on resume,
465 * this is a problem with the platform (likely shutting off all power) not with
466 * the module itself. So we use a DMI list to match known broken platforms.
467 */
468 static const struct dmi_system_id btusb_needs_reset_resume_table[] = {
469 {
470 /* Dell OptiPlex 3060 (QCA ROME device 0cf3:e007) */
471 .matches = {
472 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
473 DMI_MATCH(DMI_PRODUCT_NAME, "OptiPlex 3060"),
474 },
475 },
476 {
477 /* Dell XPS 9360 (QCA ROME device 0cf3:e300) */
478 .matches = {
479 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
480 DMI_MATCH(DMI_PRODUCT_NAME, "XPS 13 9360"),
481 },
482 },
483 {
484 /* Dell Inspiron 5565 (QCA ROME device 0cf3:e009) */
485 .matches = {
486 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
487 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 5565"),
488 },
489 },
490 {}
491 };
492
493 #define BTUSB_MAX_ISOC_FRAMES 10
494
495 #define BTUSB_INTR_RUNNING 0
496 #define BTUSB_BULK_RUNNING 1
497 #define BTUSB_ISOC_RUNNING 2
498 #define BTUSB_SUSPENDING 3
499 #define BTUSB_DID_ISO_RESUME 4
500 #define BTUSB_BOOTLOADER 5
501 #define BTUSB_DOWNLOADING 6
502 #define BTUSB_FIRMWARE_LOADED 7
503 #define BTUSB_FIRMWARE_FAILED 8
504 #define BTUSB_BOOTING 9
505 #define BTUSB_DIAG_RUNNING 10
506 #define BTUSB_OOB_WAKE_ENABLED 11
507 #define BTUSB_HW_RESET_ACTIVE 12
508 #define BTUSB_TX_WAIT_VND_EVT 13
509 #define BTUSB_WAKEUP_DISABLE 14
510
511 struct btusb_data {
512 struct hci_dev *hdev;
513 struct usb_device *udev;
514 struct usb_interface *intf;
515 struct usb_interface *isoc;
516 struct usb_interface *diag;
517 unsigned isoc_ifnum;
518
519 unsigned long flags;
520
521 struct work_struct work;
522 struct work_struct waker;
523
524 struct usb_anchor deferred;
525 struct usb_anchor tx_anchor;
526 int tx_in_flight;
527 spinlock_t txlock;
528
529 struct usb_anchor intr_anchor;
530 struct usb_anchor bulk_anchor;
531 struct usb_anchor isoc_anchor;
532 struct usb_anchor diag_anchor;
533 struct usb_anchor ctrl_anchor;
534 spinlock_t rxlock;
535
536 struct sk_buff *evt_skb;
537 struct sk_buff *acl_skb;
538 struct sk_buff *sco_skb;
539
540 struct usb_endpoint_descriptor *intr_ep;
541 struct usb_endpoint_descriptor *bulk_tx_ep;
542 struct usb_endpoint_descriptor *bulk_rx_ep;
543 struct usb_endpoint_descriptor *isoc_tx_ep;
544 struct usb_endpoint_descriptor *isoc_rx_ep;
545 struct usb_endpoint_descriptor *diag_tx_ep;
546 struct usb_endpoint_descriptor *diag_rx_ep;
547
548 struct gpio_desc *reset_gpio;
549
550 __u8 cmdreq_type;
551 __u8 cmdreq;
552
553 unsigned int sco_num;
554 unsigned int air_mode;
555 bool usb_alt6_packet_flow;
556 int isoc_altsetting;
557 int suspend_count;
558
559 int (*recv_event)(struct hci_dev *hdev, struct sk_buff *skb);
560 int (*recv_bulk)(struct btusb_data *data, void *buffer, int count);
561
562 int (*setup_on_usb)(struct hci_dev *hdev);
563
564 int oob_wake_irq; /* irq for out-of-band wake-on-bt */
565 unsigned cmd_timeout_cnt;
566 };
567
568 static void btusb_intel_cmd_timeout(struct hci_dev *hdev)
569 {
570 struct btusb_data *data = hci_get_drvdata(hdev);
571 struct gpio_desc *reset_gpio = data->reset_gpio;
572
573 if (++data->cmd_timeout_cnt < 5)
574 return;
575
576 if (!reset_gpio) {
577 bt_dev_err(hdev, "No way to reset. Ignoring and continuing");
578 return;
579 }
580
581 /*
582 * Toggle the hard reset line if the platform provides one. The reset
583 * is going to yank the device off the USB and then replug. So doing
584 * once is enough. The cleanup is handled correctly on the way out
585 * (standard USB disconnect), and the new device is detected cleanly
586 * and bound to the driver again like it should be.
587 */
588 if (test_and_set_bit(BTUSB_HW_RESET_ACTIVE, &data->flags)) {
589 bt_dev_err(hdev, "last reset failed? Not resetting again");
590 return;
591 }
592
593 bt_dev_err(hdev, "Initiating HW reset via gpio");
594 gpiod_set_value_cansleep(reset_gpio, 1);
595 msleep(100);
596 gpiod_set_value_cansleep(reset_gpio, 0);
597 }
598
599 static void btusb_rtl_cmd_timeout(struct hci_dev *hdev)
600 {
601 struct btusb_data *data = hci_get_drvdata(hdev);
602 struct gpio_desc *reset_gpio = data->reset_gpio;
603
604 if (++data->cmd_timeout_cnt < 5)
605 return;
606
607 if (!reset_gpio) {
608 bt_dev_err(hdev, "No gpio to reset Realtek device, ignoring");
609 return;
610 }
611
612 /* Toggle the hard reset line. The Realtek device is going to
613 * yank itself off the USB and then replug. The cleanup is handled
614 * correctly on the way out (standard USB disconnect), and the new
615 * device is detected cleanly and bound to the driver again like
616 * it should be.
617 */
618 if (test_and_set_bit(BTUSB_HW_RESET_ACTIVE, &data->flags)) {
619 bt_dev_err(hdev, "last reset failed? Not resetting again");
620 return;
621 }
622
623 bt_dev_err(hdev, "Reset Realtek device via gpio");
624 gpiod_set_value_cansleep(reset_gpio, 1);
625 msleep(200);
626 gpiod_set_value_cansleep(reset_gpio, 0);
627 }
628
629 static void btusb_qca_cmd_timeout(struct hci_dev *hdev)
630 {
631 struct btusb_data *data = hci_get_drvdata(hdev);
632 int err;
633
634 if (++data->cmd_timeout_cnt < 5)
635 return;
636
637 bt_dev_err(hdev, "Multiple cmd timeouts seen. Resetting usb device.");
638 /* This is not an unbalanced PM reference since the device will reset */
639 err = usb_autopm_get_interface(data->intf);
640 if (!err)
641 usb_queue_reset_device(data->intf);
642 else
643 bt_dev_err(hdev, "Failed usb_autopm_get_interface with %d", err);
644 }
645
646 static inline void btusb_free_frags(struct btusb_data *data)
647 {
648 unsigned long flags;
649
650 spin_lock_irqsave(&data->rxlock, flags);
651
652 kfree_skb(data->evt_skb);
653 data->evt_skb = NULL;
654
655 kfree_skb(data->acl_skb);
656 data->acl_skb = NULL;
657
658 kfree_skb(data->sco_skb);
659 data->sco_skb = NULL;
660
661 spin_unlock_irqrestore(&data->rxlock, flags);
662 }
663
664 static int btusb_recv_intr(struct btusb_data *data, void *buffer, int count)
665 {
666 struct sk_buff *skb;
667 unsigned long flags;
668 int err = 0;
669
670 spin_lock_irqsave(&data->rxlock, flags);
671 skb = data->evt_skb;
672
673 while (count) {
674 int len;
675
676 if (!skb) {
677 skb = bt_skb_alloc(HCI_MAX_EVENT_SIZE, GFP_ATOMIC);
678 if (!skb) {
679 err = -ENOMEM;
680 break;
681 }
682
683 hci_skb_pkt_type(skb) = HCI_EVENT_PKT;
684 hci_skb_expect(skb) = HCI_EVENT_HDR_SIZE;
685 }
686
687 len = min_t(uint, hci_skb_expect(skb), count);
688 skb_put_data(skb, buffer, len);
689
690 count -= len;
691 buffer += len;
692 hci_skb_expect(skb) -= len;
693
694 if (skb->len == HCI_EVENT_HDR_SIZE) {
695 /* Complete event header */
696 hci_skb_expect(skb) = hci_event_hdr(skb)->plen;
697
698 if (skb_tailroom(skb) < hci_skb_expect(skb)) {
699 kfree_skb(skb);
700 skb = NULL;
701
702 err = -EILSEQ;
703 break;
704 }
705 }
706
707 if (!hci_skb_expect(skb)) {
708 /* Complete frame */
709 data->recv_event(data->hdev, skb);
710 skb = NULL;
711 }
712 }
713
714 data->evt_skb = skb;
715 spin_unlock_irqrestore(&data->rxlock, flags);
716
717 return err;
718 }
719
720 static int btusb_recv_bulk(struct btusb_data *data, void *buffer, int count)
721 {
722 struct sk_buff *skb;
723 unsigned long flags;
724 int err = 0;
725
726 spin_lock_irqsave(&data->rxlock, flags);
727 skb = data->acl_skb;
728
729 while (count) {
730 int len;
731
732 if (!skb) {
733 skb = bt_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC);
734 if (!skb) {
735 err = -ENOMEM;
736 break;
737 }
738
739 hci_skb_pkt_type(skb) = HCI_ACLDATA_PKT;
740 hci_skb_expect(skb) = HCI_ACL_HDR_SIZE;
741 }
742
743 len = min_t(uint, hci_skb_expect(skb), count);
744 skb_put_data(skb, buffer, len);
745
746 count -= len;
747 buffer += len;
748 hci_skb_expect(skb) -= len;
749
750 if (skb->len == HCI_ACL_HDR_SIZE) {
751 __le16 dlen = hci_acl_hdr(skb)->dlen;
752
753 /* Complete ACL header */
754 hci_skb_expect(skb) = __le16_to_cpu(dlen);
755
756 if (skb_tailroom(skb) < hci_skb_expect(skb)) {
757 kfree_skb(skb);
758 skb = NULL;
759
760 err = -EILSEQ;
761 break;
762 }
763 }
764
765 if (!hci_skb_expect(skb)) {
766 /* Complete frame */
767 hci_recv_frame(data->hdev, skb);
768 skb = NULL;
769 }
770 }
771
772 data->acl_skb = skb;
773 spin_unlock_irqrestore(&data->rxlock, flags);
774
775 return err;
776 }
777
778 static int btusb_recv_isoc(struct btusb_data *data, void *buffer, int count)
779 {
780 struct sk_buff *skb;
781 unsigned long flags;
782 int err = 0;
783
784 spin_lock_irqsave(&data->rxlock, flags);
785 skb = data->sco_skb;
786
787 while (count) {
788 int len;
789
790 if (!skb) {
791 skb = bt_skb_alloc(HCI_MAX_SCO_SIZE, GFP_ATOMIC);
792 if (!skb) {
793 err = -ENOMEM;
794 break;
795 }
796
797 hci_skb_pkt_type(skb) = HCI_SCODATA_PKT;
798 hci_skb_expect(skb) = HCI_SCO_HDR_SIZE;
799 }
800
801 len = min_t(uint, hci_skb_expect(skb), count);
802 skb_put_data(skb, buffer, len);
803
804 count -= len;
805 buffer += len;
806 hci_skb_expect(skb) -= len;
807
808 if (skb->len == HCI_SCO_HDR_SIZE) {
809 /* Complete SCO header */
810 hci_skb_expect(skb) = hci_sco_hdr(skb)->dlen;
811
812 if (skb_tailroom(skb) < hci_skb_expect(skb)) {
813 kfree_skb(skb);
814 skb = NULL;
815
816 err = -EILSEQ;
817 break;
818 }
819 }
820
821 if (!hci_skb_expect(skb)) {
822 /* Complete frame */
823 hci_recv_frame(data->hdev, skb);
824 skb = NULL;
825 }
826 }
827
828 data->sco_skb = skb;
829 spin_unlock_irqrestore(&data->rxlock, flags);
830
831 return err;
832 }
833
834 static void btusb_intr_complete(struct urb *urb)
835 {
836 struct hci_dev *hdev = urb->context;
837 struct btusb_data *data = hci_get_drvdata(hdev);
838 int err;
839
840 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
841 urb->actual_length);
842
843 if (!test_bit(HCI_RUNNING, &hdev->flags))
844 return;
845
846 if (urb->status == 0) {
847 hdev->stat.byte_rx += urb->actual_length;
848
849 if (btusb_recv_intr(data, urb->transfer_buffer,
850 urb->actual_length) < 0) {
851 bt_dev_err(hdev, "corrupted event packet");
852 hdev->stat.err_rx++;
853 }
854 } else if (urb->status == -ENOENT) {
855 /* Avoid suspend failed when usb_kill_urb */
856 return;
857 }
858
859 if (!test_bit(BTUSB_INTR_RUNNING, &data->flags))
860 return;
861
862 usb_mark_last_busy(data->udev);
863 usb_anchor_urb(urb, &data->intr_anchor);
864
865 err = usb_submit_urb(urb, GFP_ATOMIC);
866 if (err < 0) {
867 /* -EPERM: urb is being killed;
868 * -ENODEV: device got disconnected
869 */
870 if (err != -EPERM && err != -ENODEV)
871 bt_dev_err(hdev, "urb %p failed to resubmit (%d)",
872 urb, -err);
873 usb_unanchor_urb(urb);
874 }
875 }
876
877 static int btusb_submit_intr_urb(struct hci_dev *hdev, gfp_t mem_flags)
878 {
879 struct btusb_data *data = hci_get_drvdata(hdev);
880 struct urb *urb;
881 unsigned char *buf;
882 unsigned int pipe;
883 int err, size;
884
885 BT_DBG("%s", hdev->name);
886
887 if (!data->intr_ep)
888 return -ENODEV;
889
890 urb = usb_alloc_urb(0, mem_flags);
891 if (!urb)
892 return -ENOMEM;
893
894 size = le16_to_cpu(data->intr_ep->wMaxPacketSize);
895
896 buf = kmalloc(size, mem_flags);
897 if (!buf) {
898 usb_free_urb(urb);
899 return -ENOMEM;
900 }
901
902 pipe = usb_rcvintpipe(data->udev, data->intr_ep->bEndpointAddress);
903
904 usb_fill_int_urb(urb, data->udev, pipe, buf, size,
905 btusb_intr_complete, hdev, data->intr_ep->bInterval);
906
907 urb->transfer_flags |= URB_FREE_BUFFER;
908
909 usb_anchor_urb(urb, &data->intr_anchor);
910
911 err = usb_submit_urb(urb, mem_flags);
912 if (err < 0) {
913 if (err != -EPERM && err != -ENODEV)
914 bt_dev_err(hdev, "urb %p submission failed (%d)",
915 urb, -err);
916 usb_unanchor_urb(urb);
917 }
918
919 usb_free_urb(urb);
920
921 return err;
922 }
923
924 static void btusb_bulk_complete(struct urb *urb)
925 {
926 struct hci_dev *hdev = urb->context;
927 struct btusb_data *data = hci_get_drvdata(hdev);
928 int err;
929
930 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
931 urb->actual_length);
932
933 if (!test_bit(HCI_RUNNING, &hdev->flags))
934 return;
935
936 if (urb->status == 0) {
937 hdev->stat.byte_rx += urb->actual_length;
938
939 if (data->recv_bulk(data, urb->transfer_buffer,
940 urb->actual_length) < 0) {
941 bt_dev_err(hdev, "corrupted ACL packet");
942 hdev->stat.err_rx++;
943 }
944 } else if (urb->status == -ENOENT) {
945 /* Avoid suspend failed when usb_kill_urb */
946 return;
947 }
948
949 if (!test_bit(BTUSB_BULK_RUNNING, &data->flags))
950 return;
951
952 usb_anchor_urb(urb, &data->bulk_anchor);
953 usb_mark_last_busy(data->udev);
954
955 err = usb_submit_urb(urb, GFP_ATOMIC);
956 if (err < 0) {
957 /* -EPERM: urb is being killed;
958 * -ENODEV: device got disconnected
959 */
960 if (err != -EPERM && err != -ENODEV)
961 bt_dev_err(hdev, "urb %p failed to resubmit (%d)",
962 urb, -err);
963 usb_unanchor_urb(urb);
964 }
965 }
966
967 static int btusb_submit_bulk_urb(struct hci_dev *hdev, gfp_t mem_flags)
968 {
969 struct btusb_data *data = hci_get_drvdata(hdev);
970 struct urb *urb;
971 unsigned char *buf;
972 unsigned int pipe;
973 int err, size = HCI_MAX_FRAME_SIZE;
974
975 BT_DBG("%s", hdev->name);
976
977 if (!data->bulk_rx_ep)
978 return -ENODEV;
979
980 urb = usb_alloc_urb(0, mem_flags);
981 if (!urb)
982 return -ENOMEM;
983
984 buf = kmalloc(size, mem_flags);
985 if (!buf) {
986 usb_free_urb(urb);
987 return -ENOMEM;
988 }
989
990 pipe = usb_rcvbulkpipe(data->udev, data->bulk_rx_ep->bEndpointAddress);
991
992 usb_fill_bulk_urb(urb, data->udev, pipe, buf, size,
993 btusb_bulk_complete, hdev);
994
995 urb->transfer_flags |= URB_FREE_BUFFER;
996
997 usb_mark_last_busy(data->udev);
998 usb_anchor_urb(urb, &data->bulk_anchor);
999
1000 err = usb_submit_urb(urb, mem_flags);
1001 if (err < 0) {
1002 if (err != -EPERM && err != -ENODEV)
1003 bt_dev_err(hdev, "urb %p submission failed (%d)",
1004 urb, -err);
1005 usb_unanchor_urb(urb);
1006 }
1007
1008 usb_free_urb(urb);
1009
1010 return err;
1011 }
1012
1013 static void btusb_isoc_complete(struct urb *urb)
1014 {
1015 struct hci_dev *hdev = urb->context;
1016 struct btusb_data *data = hci_get_drvdata(hdev);
1017 int i, err;
1018
1019 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
1020 urb->actual_length);
1021
1022 if (!test_bit(HCI_RUNNING, &hdev->flags))
1023 return;
1024
1025 if (urb->status == 0) {
1026 for (i = 0; i < urb->number_of_packets; i++) {
1027 unsigned int offset = urb->iso_frame_desc[i].offset;
1028 unsigned int length = urb->iso_frame_desc[i].actual_length;
1029
1030 if (urb->iso_frame_desc[i].status)
1031 continue;
1032
1033 hdev->stat.byte_rx += length;
1034
1035 if (btusb_recv_isoc(data, urb->transfer_buffer + offset,
1036 length) < 0) {
1037 bt_dev_err(hdev, "corrupted SCO packet");
1038 hdev->stat.err_rx++;
1039 }
1040 }
1041 } else if (urb->status == -ENOENT) {
1042 /* Avoid suspend failed when usb_kill_urb */
1043 return;
1044 }
1045
1046 if (!test_bit(BTUSB_ISOC_RUNNING, &data->flags))
1047 return;
1048
1049 usb_anchor_urb(urb, &data->isoc_anchor);
1050
1051 err = usb_submit_urb(urb, GFP_ATOMIC);
1052 if (err < 0) {
1053 /* -EPERM: urb is being killed;
1054 * -ENODEV: device got disconnected
1055 */
1056 if (err != -EPERM && err != -ENODEV)
1057 bt_dev_err(hdev, "urb %p failed to resubmit (%d)",
1058 urb, -err);
1059 usb_unanchor_urb(urb);
1060 }
1061 }
1062
1063 static inline void __fill_isoc_descriptor_msbc(struct urb *urb, int len,
1064 int mtu, struct btusb_data *data)
1065 {
1066 int i, offset = 0;
1067 unsigned int interval;
1068
1069 BT_DBG("len %d mtu %d", len, mtu);
1070
1071 /* For mSBC ALT 6 setting the host will send the packet at continuous
1072 * flow. As per core spec 5, vol 4, part B, table 2.1. For ALT setting
1073 * 6 the HCI PACKET INTERVAL should be 7.5ms for every usb packets.
1074 * To maintain the rate we send 63bytes of usb packets alternatively for
1075 * 7ms and 8ms to maintain the rate as 7.5ms.
1076 */
1077 if (data->usb_alt6_packet_flow) {
1078 interval = 7;
1079 data->usb_alt6_packet_flow = false;
1080 } else {
1081 interval = 6;
1082 data->usb_alt6_packet_flow = true;
1083 }
1084
1085 for (i = 0; i < interval; i++) {
1086 urb->iso_frame_desc[i].offset = offset;
1087 urb->iso_frame_desc[i].length = offset;
1088 }
1089
1090 if (len && i < BTUSB_MAX_ISOC_FRAMES) {
1091 urb->iso_frame_desc[i].offset = offset;
1092 urb->iso_frame_desc[i].length = len;
1093 i++;
1094 }
1095
1096 urb->number_of_packets = i;
1097 }
1098
1099 static inline void __fill_isoc_descriptor(struct urb *urb, int len, int mtu)
1100 {
1101 int i, offset = 0;
1102
1103 BT_DBG("len %d mtu %d", len, mtu);
1104
1105 for (i = 0; i < BTUSB_MAX_ISOC_FRAMES && len >= mtu;
1106 i++, offset += mtu, len -= mtu) {
1107 urb->iso_frame_desc[i].offset = offset;
1108 urb->iso_frame_desc[i].length = mtu;
1109 }
1110
1111 if (len && i < BTUSB_MAX_ISOC_FRAMES) {
1112 urb->iso_frame_desc[i].offset = offset;
1113 urb->iso_frame_desc[i].length = len;
1114 i++;
1115 }
1116
1117 urb->number_of_packets = i;
1118 }
1119
1120 static int btusb_submit_isoc_urb(struct hci_dev *hdev, gfp_t mem_flags)
1121 {
1122 struct btusb_data *data = hci_get_drvdata(hdev);
1123 struct urb *urb;
1124 unsigned char *buf;
1125 unsigned int pipe;
1126 int err, size;
1127
1128 BT_DBG("%s", hdev->name);
1129
1130 if (!data->isoc_rx_ep)
1131 return -ENODEV;
1132
1133 urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, mem_flags);
1134 if (!urb)
1135 return -ENOMEM;
1136
1137 size = le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize) *
1138 BTUSB_MAX_ISOC_FRAMES;
1139
1140 buf = kmalloc(size, mem_flags);
1141 if (!buf) {
1142 usb_free_urb(urb);
1143 return -ENOMEM;
1144 }
1145
1146 pipe = usb_rcvisocpipe(data->udev, data->isoc_rx_ep->bEndpointAddress);
1147
1148 usb_fill_int_urb(urb, data->udev, pipe, buf, size, btusb_isoc_complete,
1149 hdev, data->isoc_rx_ep->bInterval);
1150
1151 urb->transfer_flags = URB_FREE_BUFFER | URB_ISO_ASAP;
1152
1153 __fill_isoc_descriptor(urb, size,
1154 le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize));
1155
1156 usb_anchor_urb(urb, &data->isoc_anchor);
1157
1158 err = usb_submit_urb(urb, mem_flags);
1159 if (err < 0) {
1160 if (err != -EPERM && err != -ENODEV)
1161 bt_dev_err(hdev, "urb %p submission failed (%d)",
1162 urb, -err);
1163 usb_unanchor_urb(urb);
1164 }
1165
1166 usb_free_urb(urb);
1167
1168 return err;
1169 }
1170
1171 static void btusb_diag_complete(struct urb *urb)
1172 {
1173 struct hci_dev *hdev = urb->context;
1174 struct btusb_data *data = hci_get_drvdata(hdev);
1175 int err;
1176
1177 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
1178 urb->actual_length);
1179
1180 if (urb->status == 0) {
1181 struct sk_buff *skb;
1182
1183 skb = bt_skb_alloc(urb->actual_length, GFP_ATOMIC);
1184 if (skb) {
1185 skb_put_data(skb, urb->transfer_buffer,
1186 urb->actual_length);
1187 hci_recv_diag(hdev, skb);
1188 }
1189 } else if (urb->status == -ENOENT) {
1190 /* Avoid suspend failed when usb_kill_urb */
1191 return;
1192 }
1193
1194 if (!test_bit(BTUSB_DIAG_RUNNING, &data->flags))
1195 return;
1196
1197 usb_anchor_urb(urb, &data->diag_anchor);
1198 usb_mark_last_busy(data->udev);
1199
1200 err = usb_submit_urb(urb, GFP_ATOMIC);
1201 if (err < 0) {
1202 /* -EPERM: urb is being killed;
1203 * -ENODEV: device got disconnected
1204 */
1205 if (err != -EPERM && err != -ENODEV)
1206 bt_dev_err(hdev, "urb %p failed to resubmit (%d)",
1207 urb, -err);
1208 usb_unanchor_urb(urb);
1209 }
1210 }
1211
1212 static int btusb_submit_diag_urb(struct hci_dev *hdev, gfp_t mem_flags)
1213 {
1214 struct btusb_data *data = hci_get_drvdata(hdev);
1215 struct urb *urb;
1216 unsigned char *buf;
1217 unsigned int pipe;
1218 int err, size = HCI_MAX_FRAME_SIZE;
1219
1220 BT_DBG("%s", hdev->name);
1221
1222 if (!data->diag_rx_ep)
1223 return -ENODEV;
1224
1225 urb = usb_alloc_urb(0, mem_flags);
1226 if (!urb)
1227 return -ENOMEM;
1228
1229 buf = kmalloc(size, mem_flags);
1230 if (!buf) {
1231 usb_free_urb(urb);
1232 return -ENOMEM;
1233 }
1234
1235 pipe = usb_rcvbulkpipe(data->udev, data->diag_rx_ep->bEndpointAddress);
1236
1237 usb_fill_bulk_urb(urb, data->udev, pipe, buf, size,
1238 btusb_diag_complete, hdev);
1239
1240 urb->transfer_flags |= URB_FREE_BUFFER;
1241
1242 usb_mark_last_busy(data->udev);
1243 usb_anchor_urb(urb, &data->diag_anchor);
1244
1245 err = usb_submit_urb(urb, mem_flags);
1246 if (err < 0) {
1247 if (err != -EPERM && err != -ENODEV)
1248 bt_dev_err(hdev, "urb %p submission failed (%d)",
1249 urb, -err);
1250 usb_unanchor_urb(urb);
1251 }
1252
1253 usb_free_urb(urb);
1254
1255 return err;
1256 }
1257
1258 static void btusb_tx_complete(struct urb *urb)
1259 {
1260 struct sk_buff *skb = urb->context;
1261 struct hci_dev *hdev = (struct hci_dev *)skb->dev;
1262 struct btusb_data *data = hci_get_drvdata(hdev);
1263 unsigned long flags;
1264
1265 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
1266 urb->actual_length);
1267
1268 if (!test_bit(HCI_RUNNING, &hdev->flags))
1269 goto done;
1270
1271 if (!urb->status)
1272 hdev->stat.byte_tx += urb->transfer_buffer_length;
1273 else
1274 hdev->stat.err_tx++;
1275
1276 done:
1277 spin_lock_irqsave(&data->txlock, flags);
1278 data->tx_in_flight--;
1279 spin_unlock_irqrestore(&data->txlock, flags);
1280
1281 kfree(urb->setup_packet);
1282
1283 kfree_skb(skb);
1284 }
1285
1286 static void btusb_isoc_tx_complete(struct urb *urb)
1287 {
1288 struct sk_buff *skb = urb->context;
1289 struct hci_dev *hdev = (struct hci_dev *)skb->dev;
1290
1291 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
1292 urb->actual_length);
1293
1294 if (!test_bit(HCI_RUNNING, &hdev->flags))
1295 goto done;
1296
1297 if (!urb->status)
1298 hdev->stat.byte_tx += urb->transfer_buffer_length;
1299 else
1300 hdev->stat.err_tx++;
1301
1302 done:
1303 kfree(urb->setup_packet);
1304
1305 kfree_skb(skb);
1306 }
1307
1308 static int btusb_open(struct hci_dev *hdev)
1309 {
1310 struct btusb_data *data = hci_get_drvdata(hdev);
1311 int err;
1312
1313 BT_DBG("%s", hdev->name);
1314
1315 err = usb_autopm_get_interface(data->intf);
1316 if (err < 0)
1317 return err;
1318
1319 /* Patching USB firmware files prior to starting any URBs of HCI path
1320 * It is more safe to use USB bulk channel for downloading USB patch
1321 */
1322 if (data->setup_on_usb) {
1323 err = data->setup_on_usb(hdev);
1324 if (err < 0)
1325 goto setup_fail;
1326 }
1327
1328 data->intf->needs_remote_wakeup = 1;
1329
1330 /* Disable device remote wakeup when host is suspended
1331 * For Realtek chips, global suspend without
1332 * SET_FEATURE (DEVICE_REMOTE_WAKEUP) can save more power in device.
1333 */
1334 if (test_bit(BTUSB_WAKEUP_DISABLE, &data->flags))
1335 device_wakeup_disable(&data->udev->dev);
1336
1337 if (test_and_set_bit(BTUSB_INTR_RUNNING, &data->flags))
1338 goto done;
1339
1340 err = btusb_submit_intr_urb(hdev, GFP_KERNEL);
1341 if (err < 0)
1342 goto failed;
1343
1344 err = btusb_submit_bulk_urb(hdev, GFP_KERNEL);
1345 if (err < 0) {
1346 usb_kill_anchored_urbs(&data->intr_anchor);
1347 goto failed;
1348 }
1349
1350 set_bit(BTUSB_BULK_RUNNING, &data->flags);
1351 btusb_submit_bulk_urb(hdev, GFP_KERNEL);
1352
1353 if (data->diag) {
1354 if (!btusb_submit_diag_urb(hdev, GFP_KERNEL))
1355 set_bit(BTUSB_DIAG_RUNNING, &data->flags);
1356 }
1357
1358 done:
1359 usb_autopm_put_interface(data->intf);
1360 return 0;
1361
1362 failed:
1363 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
1364 setup_fail:
1365 usb_autopm_put_interface(data->intf);
1366 return err;
1367 }
1368
1369 static void btusb_stop_traffic(struct btusb_data *data)
1370 {
1371 usb_kill_anchored_urbs(&data->intr_anchor);
1372 usb_kill_anchored_urbs(&data->bulk_anchor);
1373 usb_kill_anchored_urbs(&data->isoc_anchor);
1374 usb_kill_anchored_urbs(&data->diag_anchor);
1375 usb_kill_anchored_urbs(&data->ctrl_anchor);
1376 }
1377
1378 static int btusb_close(struct hci_dev *hdev)
1379 {
1380 struct btusb_data *data = hci_get_drvdata(hdev);
1381 int err;
1382
1383 BT_DBG("%s", hdev->name);
1384
1385 cancel_work_sync(&data->work);
1386 cancel_work_sync(&data->waker);
1387
1388 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1389 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
1390 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
1391 clear_bit(BTUSB_DIAG_RUNNING, &data->flags);
1392
1393 btusb_stop_traffic(data);
1394 btusb_free_frags(data);
1395
1396 err = usb_autopm_get_interface(data->intf);
1397 if (err < 0)
1398 goto failed;
1399
1400 data->intf->needs_remote_wakeup = 0;
1401
1402 /* Enable remote wake up for auto-suspend */
1403 if (test_bit(BTUSB_WAKEUP_DISABLE, &data->flags))
1404 data->intf->needs_remote_wakeup = 1;
1405
1406 usb_autopm_put_interface(data->intf);
1407
1408 failed:
1409 usb_scuttle_anchored_urbs(&data->deferred);
1410 return 0;
1411 }
1412
1413 static int btusb_flush(struct hci_dev *hdev)
1414 {
1415 struct btusb_data *data = hci_get_drvdata(hdev);
1416
1417 BT_DBG("%s", hdev->name);
1418
1419 usb_kill_anchored_urbs(&data->tx_anchor);
1420 btusb_free_frags(data);
1421
1422 return 0;
1423 }
1424
1425 static struct urb *alloc_ctrl_urb(struct hci_dev *hdev, struct sk_buff *skb)
1426 {
1427 struct btusb_data *data = hci_get_drvdata(hdev);
1428 struct usb_ctrlrequest *dr;
1429 struct urb *urb;
1430 unsigned int pipe;
1431
1432 urb = usb_alloc_urb(0, GFP_KERNEL);
1433 if (!urb)
1434 return ERR_PTR(-ENOMEM);
1435
1436 dr = kmalloc(sizeof(*dr), GFP_KERNEL);
1437 if (!dr) {
1438 usb_free_urb(urb);
1439 return ERR_PTR(-ENOMEM);
1440 }
1441
1442 dr->bRequestType = data->cmdreq_type;
1443 dr->bRequest = data->cmdreq;
1444 dr->wIndex = 0;
1445 dr->wValue = 0;
1446 dr->wLength = __cpu_to_le16(skb->len);
1447
1448 pipe = usb_sndctrlpipe(data->udev, 0x00);
1449
1450 usb_fill_control_urb(urb, data->udev, pipe, (void *)dr,
1451 skb->data, skb->len, btusb_tx_complete, skb);
1452
1453 skb->dev = (void *)hdev;
1454
1455 return urb;
1456 }
1457
1458 static struct urb *alloc_bulk_urb(struct hci_dev *hdev, struct sk_buff *skb)
1459 {
1460 struct btusb_data *data = hci_get_drvdata(hdev);
1461 struct urb *urb;
1462 unsigned int pipe;
1463
1464 if (!data->bulk_tx_ep)
1465 return ERR_PTR(-ENODEV);
1466
1467 urb = usb_alloc_urb(0, GFP_KERNEL);
1468 if (!urb)
1469 return ERR_PTR(-ENOMEM);
1470
1471 pipe = usb_sndbulkpipe(data->udev, data->bulk_tx_ep->bEndpointAddress);
1472
1473 usb_fill_bulk_urb(urb, data->udev, pipe,
1474 skb->data, skb->len, btusb_tx_complete, skb);
1475
1476 skb->dev = (void *)hdev;
1477
1478 return urb;
1479 }
1480
1481 static struct urb *alloc_isoc_urb(struct hci_dev *hdev, struct sk_buff *skb)
1482 {
1483 struct btusb_data *data = hci_get_drvdata(hdev);
1484 struct urb *urb;
1485 unsigned int pipe;
1486
1487 if (!data->isoc_tx_ep)
1488 return ERR_PTR(-ENODEV);
1489
1490 urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, GFP_KERNEL);
1491 if (!urb)
1492 return ERR_PTR(-ENOMEM);
1493
1494 pipe = usb_sndisocpipe(data->udev, data->isoc_tx_ep->bEndpointAddress);
1495
1496 usb_fill_int_urb(urb, data->udev, pipe,
1497 skb->data, skb->len, btusb_isoc_tx_complete,
1498 skb, data->isoc_tx_ep->bInterval);
1499
1500 urb->transfer_flags = URB_ISO_ASAP;
1501
1502 if (data->isoc_altsetting == 6)
1503 __fill_isoc_descriptor_msbc(urb, skb->len,
1504 le16_to_cpu(data->isoc_tx_ep->wMaxPacketSize),
1505 data);
1506 else
1507 __fill_isoc_descriptor(urb, skb->len,
1508 le16_to_cpu(data->isoc_tx_ep->wMaxPacketSize));
1509 skb->dev = (void *)hdev;
1510
1511 return urb;
1512 }
1513
1514 static int submit_tx_urb(struct hci_dev *hdev, struct urb *urb)
1515 {
1516 struct btusb_data *data = hci_get_drvdata(hdev);
1517 int err;
1518
1519 usb_anchor_urb(urb, &data->tx_anchor);
1520
1521 err = usb_submit_urb(urb, GFP_KERNEL);
1522 if (err < 0) {
1523 if (err != -EPERM && err != -ENODEV)
1524 bt_dev_err(hdev, "urb %p submission failed (%d)",
1525 urb, -err);
1526 kfree(urb->setup_packet);
1527 usb_unanchor_urb(urb);
1528 } else {
1529 usb_mark_last_busy(data->udev);
1530 }
1531
1532 usb_free_urb(urb);
1533 return err;
1534 }
1535
1536 static int submit_or_queue_tx_urb(struct hci_dev *hdev, struct urb *urb)
1537 {
1538 struct btusb_data *data = hci_get_drvdata(hdev);
1539 unsigned long flags;
1540 bool suspending;
1541
1542 spin_lock_irqsave(&data->txlock, flags);
1543 suspending = test_bit(BTUSB_SUSPENDING, &data->flags);
1544 if (!suspending)
1545 data->tx_in_flight++;
1546 spin_unlock_irqrestore(&data->txlock, flags);
1547
1548 if (!suspending)
1549 return submit_tx_urb(hdev, urb);
1550
1551 usb_anchor_urb(urb, &data->deferred);
1552 schedule_work(&data->waker);
1553
1554 usb_free_urb(urb);
1555 return 0;
1556 }
1557
1558 static int btusb_send_frame(struct hci_dev *hdev, struct sk_buff *skb)
1559 {
1560 struct urb *urb;
1561
1562 BT_DBG("%s", hdev->name);
1563
1564 switch (hci_skb_pkt_type(skb)) {
1565 case HCI_COMMAND_PKT:
1566 urb = alloc_ctrl_urb(hdev, skb);
1567 if (IS_ERR(urb))
1568 return PTR_ERR(urb);
1569
1570 hdev->stat.cmd_tx++;
1571 return submit_or_queue_tx_urb(hdev, urb);
1572
1573 case HCI_ACLDATA_PKT:
1574 urb = alloc_bulk_urb(hdev, skb);
1575 if (IS_ERR(urb))
1576 return PTR_ERR(urb);
1577
1578 hdev->stat.acl_tx++;
1579 return submit_or_queue_tx_urb(hdev, urb);
1580
1581 case HCI_SCODATA_PKT:
1582 if (hci_conn_num(hdev, SCO_LINK) < 1)
1583 return -ENODEV;
1584
1585 urb = alloc_isoc_urb(hdev, skb);
1586 if (IS_ERR(urb))
1587 return PTR_ERR(urb);
1588
1589 hdev->stat.sco_tx++;
1590 return submit_tx_urb(hdev, urb);
1591 }
1592
1593 return -EILSEQ;
1594 }
1595
1596 static void btusb_notify(struct hci_dev *hdev, unsigned int evt)
1597 {
1598 struct btusb_data *data = hci_get_drvdata(hdev);
1599
1600 BT_DBG("%s evt %d", hdev->name, evt);
1601
1602 if (hci_conn_num(hdev, SCO_LINK) != data->sco_num) {
1603 data->sco_num = hci_conn_num(hdev, SCO_LINK);
1604 data->air_mode = evt;
1605 schedule_work(&data->work);
1606 }
1607 }
1608
1609 static inline int __set_isoc_interface(struct hci_dev *hdev, int altsetting)
1610 {
1611 struct btusb_data *data = hci_get_drvdata(hdev);
1612 struct usb_interface *intf = data->isoc;
1613 struct usb_endpoint_descriptor *ep_desc;
1614 int i, err;
1615
1616 if (!data->isoc)
1617 return -ENODEV;
1618
1619 err = usb_set_interface(data->udev, data->isoc_ifnum, altsetting);
1620 if (err < 0) {
1621 bt_dev_err(hdev, "setting interface failed (%d)", -err);
1622 return err;
1623 }
1624
1625 data->isoc_altsetting = altsetting;
1626
1627 data->isoc_tx_ep = NULL;
1628 data->isoc_rx_ep = NULL;
1629
1630 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
1631 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
1632
1633 if (!data->isoc_tx_ep && usb_endpoint_is_isoc_out(ep_desc)) {
1634 data->isoc_tx_ep = ep_desc;
1635 continue;
1636 }
1637
1638 if (!data->isoc_rx_ep && usb_endpoint_is_isoc_in(ep_desc)) {
1639 data->isoc_rx_ep = ep_desc;
1640 continue;
1641 }
1642 }
1643
1644 if (!data->isoc_tx_ep || !data->isoc_rx_ep) {
1645 bt_dev_err(hdev, "invalid SCO descriptors");
1646 return -ENODEV;
1647 }
1648
1649 return 0;
1650 }
1651
1652 static int btusb_switch_alt_setting(struct hci_dev *hdev, int new_alts)
1653 {
1654 struct btusb_data *data = hci_get_drvdata(hdev);
1655 int err;
1656
1657 if (data->isoc_altsetting != new_alts) {
1658 unsigned long flags;
1659
1660 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1661 usb_kill_anchored_urbs(&data->isoc_anchor);
1662
1663 /* When isochronous alternate setting needs to be
1664 * changed, because SCO connection has been added
1665 * or removed, a packet fragment may be left in the
1666 * reassembling state. This could lead to wrongly
1667 * assembled fragments.
1668 *
1669 * Clear outstanding fragment when selecting a new
1670 * alternate setting.
1671 */
1672 spin_lock_irqsave(&data->rxlock, flags);
1673 kfree_skb(data->sco_skb);
1674 data->sco_skb = NULL;
1675 spin_unlock_irqrestore(&data->rxlock, flags);
1676
1677 err = __set_isoc_interface(hdev, new_alts);
1678 if (err < 0)
1679 return err;
1680 }
1681
1682 if (!test_and_set_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
1683 if (btusb_submit_isoc_urb(hdev, GFP_KERNEL) < 0)
1684 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1685 else
1686 btusb_submit_isoc_urb(hdev, GFP_KERNEL);
1687 }
1688
1689 return 0;
1690 }
1691
1692 static struct usb_host_interface *btusb_find_altsetting(struct btusb_data *data,
1693 int alt)
1694 {
1695 struct usb_interface *intf = data->isoc;
1696 int i;
1697
1698 BT_DBG("Looking for Alt no :%d", alt);
1699
1700 if (!intf)
1701 return NULL;
1702
1703 for (i = 0; i < intf->num_altsetting; i++) {
1704 if (intf->altsetting[i].desc.bAlternateSetting == alt)
1705 return &intf->altsetting[i];
1706 }
1707
1708 return NULL;
1709 }
1710
1711 static void btusb_work(struct work_struct *work)
1712 {
1713 struct btusb_data *data = container_of(work, struct btusb_data, work);
1714 struct hci_dev *hdev = data->hdev;
1715 int new_alts = 0;
1716 int err;
1717
1718 if (data->sco_num > 0) {
1719 if (!test_bit(BTUSB_DID_ISO_RESUME, &data->flags)) {
1720 err = usb_autopm_get_interface(data->isoc ? data->isoc : data->intf);
1721 if (err < 0) {
1722 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1723 usb_kill_anchored_urbs(&data->isoc_anchor);
1724 return;
1725 }
1726
1727 set_bit(BTUSB_DID_ISO_RESUME, &data->flags);
1728 }
1729
1730 if (data->air_mode == HCI_NOTIFY_ENABLE_SCO_CVSD) {
1731 if (hdev->voice_setting & 0x0020) {
1732 static const int alts[3] = { 2, 4, 5 };
1733
1734 new_alts = alts[data->sco_num - 1];
1735 } else {
1736 new_alts = data->sco_num;
1737 }
1738 } else if (data->air_mode == HCI_NOTIFY_ENABLE_SCO_TRANSP) {
1739 /* Bluetooth USB spec recommends alt 6 (63 bytes), but
1740 * many adapters do not support it. Alt 1 appears to
1741 * work for all adapters that do not have alt 6, and
1742 * which work with WBS at all.
1743 */
1744 new_alts = btusb_find_altsetting(data, 6) ? 6 : 1;
1745 }
1746
1747 if (btusb_switch_alt_setting(hdev, new_alts) < 0)
1748 bt_dev_err(hdev, "set USB alt:(%d) failed!", new_alts);
1749 } else {
1750 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1751 usb_kill_anchored_urbs(&data->isoc_anchor);
1752
1753 __set_isoc_interface(hdev, 0);
1754 if (test_and_clear_bit(BTUSB_DID_ISO_RESUME, &data->flags))
1755 usb_autopm_put_interface(data->isoc ? data->isoc : data->intf);
1756 }
1757 }
1758
1759 static void btusb_waker(struct work_struct *work)
1760 {
1761 struct btusb_data *data = container_of(work, struct btusb_data, waker);
1762 int err;
1763
1764 err = usb_autopm_get_interface(data->intf);
1765 if (err < 0)
1766 return;
1767
1768 usb_autopm_put_interface(data->intf);
1769 }
1770
1771 static int btusb_setup_bcm92035(struct hci_dev *hdev)
1772 {
1773 struct sk_buff *skb;
1774 u8 val = 0x00;
1775
1776 BT_DBG("%s", hdev->name);
1777
1778 skb = __hci_cmd_sync(hdev, 0xfc3b, 1, &val, HCI_INIT_TIMEOUT);
1779 if (IS_ERR(skb))
1780 bt_dev_err(hdev, "BCM92035 command failed (%ld)", PTR_ERR(skb));
1781 else
1782 kfree_skb(skb);
1783
1784 return 0;
1785 }
1786
1787 static int btusb_setup_csr(struct hci_dev *hdev)
1788 {
1789 struct btusb_data *data = hci_get_drvdata(hdev);
1790 u16 bcdDevice = le16_to_cpu(data->udev->descriptor.bcdDevice);
1791 struct hci_rp_read_local_version *rp;
1792 struct sk_buff *skb;
1793 bool is_fake = false;
1794 int ret;
1795
1796 BT_DBG("%s", hdev->name);
1797
1798 skb = __hci_cmd_sync(hdev, HCI_OP_READ_LOCAL_VERSION, 0, NULL,
1799 HCI_INIT_TIMEOUT);
1800 if (IS_ERR(skb)) {
1801 int err = PTR_ERR(skb);
1802 bt_dev_err(hdev, "CSR: Local version failed (%d)", err);
1803 return err;
1804 }
1805
1806 if (skb->len != sizeof(struct hci_rp_read_local_version)) {
1807 bt_dev_err(hdev, "CSR: Local version length mismatch");
1808 kfree_skb(skb);
1809 return -EIO;
1810 }
1811
1812 rp = (struct hci_rp_read_local_version *)skb->data;
1813
1814 /* Detect a wide host of Chinese controllers that aren't CSR.
1815 *
1816 * Known fake bcdDevices: 0x0100, 0x0134, 0x1915, 0x2520, 0x7558, 0x8891
1817 *
1818 * The main thing they have in common is that these are really popular low-cost
1819 * options that support newer Bluetooth versions but rely on heavy VID/PID
1820 * squatting of this poor old Bluetooth 1.1 device. Even sold as such.
1821 *
1822 * We detect actual CSR devices by checking that the HCI manufacturer code
1823 * is Cambridge Silicon Radio (10) and ensuring that LMP sub-version and
1824 * HCI rev values always match. As they both store the firmware number.
1825 */
1826 if (le16_to_cpu(rp->manufacturer) != 10 ||
1827 le16_to_cpu(rp->hci_rev) != le16_to_cpu(rp->lmp_subver))
1828 is_fake = true;
1829
1830 /* Known legit CSR firmware build numbers and their supported BT versions:
1831 * - 1.1 (0x1) -> 0x0073, 0x020d, 0x033c, 0x034e
1832 * - 1.2 (0x2) -> 0x04d9, 0x0529
1833 * - 2.0 (0x3) -> 0x07a6, 0x07ad, 0x0c5c
1834 * - 2.1 (0x4) -> 0x149c, 0x1735, 0x1899 (0x1899 is a BlueCore4-External)
1835 * - 4.0 (0x6) -> 0x1d86, 0x2031, 0x22bb
1836 *
1837 * e.g. Real CSR dongles with LMP subversion 0x73 are old enough that
1838 * support BT 1.1 only; so it's a dead giveaway when some
1839 * third-party BT 4.0 dongle reuses it.
1840 */
1841 else if (le16_to_cpu(rp->lmp_subver) <= 0x034e &&
1842 le16_to_cpu(rp->hci_ver) > BLUETOOTH_VER_1_1)
1843 is_fake = true;
1844
1845 else if (le16_to_cpu(rp->lmp_subver) <= 0x0529 &&
1846 le16_to_cpu(rp->hci_ver) > BLUETOOTH_VER_1_2)
1847 is_fake = true;
1848
1849 else if (le16_to_cpu(rp->lmp_subver) <= 0x0c5c &&
1850 le16_to_cpu(rp->hci_ver) > BLUETOOTH_VER_2_0)
1851 is_fake = true;
1852
1853 else if (le16_to_cpu(rp->lmp_subver) <= 0x1899 &&
1854 le16_to_cpu(rp->hci_ver) > BLUETOOTH_VER_2_1)
1855 is_fake = true;
1856
1857 else if (le16_to_cpu(rp->lmp_subver) <= 0x22bb &&
1858 le16_to_cpu(rp->hci_ver) > BLUETOOTH_VER_4_0)
1859 is_fake = true;
1860
1861 /* Other clones which beat all the above checks */
1862 else if (bcdDevice == 0x0134 &&
1863 le16_to_cpu(rp->lmp_subver) == 0x0c5c &&
1864 le16_to_cpu(rp->hci_ver) == BLUETOOTH_VER_2_0)
1865 is_fake = true;
1866
1867 if (is_fake) {
1868 bt_dev_warn(hdev, "CSR: Unbranded CSR clone detected; adding workarounds...");
1869
1870 /* Generally these clones have big discrepancies between
1871 * advertised features and what's actually supported.
1872 * Probably will need to be expanded in the future;
1873 * without these the controller will lock up.
1874 */
1875 set_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks);
1876 set_bit(HCI_QUIRK_BROKEN_ERR_DATA_REPORTING, &hdev->quirks);
1877
1878 /* Clear the reset quirk since this is not an actual
1879 * early Bluetooth 1.1 device from CSR.
1880 */
1881 clear_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
1882 clear_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
1883
1884 /*
1885 * Special workaround for clones with a Barrot 8041a02 chip,
1886 * these clones are really messed-up:
1887 * 1. Their bulk rx endpoint will never report any data unless
1888 * the device was suspended at least once (yes really).
1889 * 2. They will not wakeup when autosuspended and receiving data
1890 * on their bulk rx endpoint from e.g. a keyboard or mouse
1891 * (IOW remote-wakeup support is broken for the bulk endpoint).
1892 *
1893 * To fix 1. enable runtime-suspend, force-suspend the
1894 * hci and then wake-it up by disabling runtime-suspend.
1895 *
1896 * To fix 2. clear the hci's can_wake flag, this way the hci
1897 * will still be autosuspended when it is not open.
1898 */
1899 if (bcdDevice == 0x8891 &&
1900 le16_to_cpu(rp->lmp_subver) == 0x1012 &&
1901 le16_to_cpu(rp->hci_rev) == 0x0810 &&
1902 le16_to_cpu(rp->hci_ver) == BLUETOOTH_VER_4_0) {
1903 bt_dev_warn(hdev, "CSR: detected a fake CSR dongle using a Barrot 8041a02 chip, this chip is very buggy and may have issues\n");
1904
1905 pm_runtime_allow(&data->udev->dev);
1906
1907 ret = pm_runtime_suspend(&data->udev->dev);
1908 if (ret >= 0)
1909 msleep(200);
1910 else
1911 bt_dev_err(hdev, "Failed to suspend the device for Barrot 8041a02 receive-issue workaround\n");
1912
1913 pm_runtime_forbid(&data->udev->dev);
1914
1915 device_set_wakeup_capable(&data->udev->dev, false);
1916 /* Re-enable autosuspend if this was requested */
1917 if (enable_autosuspend)
1918 usb_enable_autosuspend(data->udev);
1919 }
1920 }
1921
1922 kfree_skb(skb);
1923
1924 return 0;
1925 }
1926
1927 static const struct firmware *btusb_setup_intel_get_fw(struct hci_dev *hdev,
1928 struct intel_version *ver)
1929 {
1930 const struct firmware *fw;
1931 char fwname[64];
1932 int ret;
1933
1934 snprintf(fwname, sizeof(fwname),
1935 "intel/ibt-hw-%x.%x.%x-fw-%x.%x.%x.%x.%x.bseq",
1936 ver->hw_platform, ver->hw_variant, ver->hw_revision,
1937 ver->fw_variant, ver->fw_revision, ver->fw_build_num,
1938 ver->fw_build_ww, ver->fw_build_yy);
1939
1940 ret = request_firmware(&fw, fwname, &hdev->dev);
1941 if (ret < 0) {
1942 if (ret == -EINVAL) {
1943 bt_dev_err(hdev, "Intel firmware file request failed (%d)",
1944 ret);
1945 return NULL;
1946 }
1947
1948 bt_dev_err(hdev, "failed to open Intel firmware file: %s (%d)",
1949 fwname, ret);
1950
1951 /* If the correct firmware patch file is not found, use the
1952 * default firmware patch file instead
1953 */
1954 snprintf(fwname, sizeof(fwname), "intel/ibt-hw-%x.%x.bseq",
1955 ver->hw_platform, ver->hw_variant);
1956 if (request_firmware(&fw, fwname, &hdev->dev) < 0) {
1957 bt_dev_err(hdev, "failed to open default fw file: %s",
1958 fwname);
1959 return NULL;
1960 }
1961 }
1962
1963 bt_dev_info(hdev, "Intel Bluetooth firmware file: %s", fwname);
1964
1965 return fw;
1966 }
1967
1968 static int btusb_setup_intel_patching(struct hci_dev *hdev,
1969 const struct firmware *fw,
1970 const u8 **fw_ptr, int *disable_patch)
1971 {
1972 struct sk_buff *skb;
1973 struct hci_command_hdr *cmd;
1974 const u8 *cmd_param;
1975 struct hci_event_hdr *evt = NULL;
1976 const u8 *evt_param = NULL;
1977 int remain = fw->size - (*fw_ptr - fw->data);
1978
1979 /* The first byte indicates the types of the patch command or event.
1980 * 0x01 means HCI command and 0x02 is HCI event. If the first bytes
1981 * in the current firmware buffer doesn't start with 0x01 or
1982 * the size of remain buffer is smaller than HCI command header,
1983 * the firmware file is corrupted and it should stop the patching
1984 * process.
1985 */
1986 if (remain > HCI_COMMAND_HDR_SIZE && *fw_ptr[0] != 0x01) {
1987 bt_dev_err(hdev, "Intel fw corrupted: invalid cmd read");
1988 return -EINVAL;
1989 }
1990 (*fw_ptr)++;
1991 remain--;
1992
1993 cmd = (struct hci_command_hdr *)(*fw_ptr);
1994 *fw_ptr += sizeof(*cmd);
1995 remain -= sizeof(*cmd);
1996
1997 /* Ensure that the remain firmware data is long enough than the length
1998 * of command parameter. If not, the firmware file is corrupted.
1999 */
2000 if (remain < cmd->plen) {
2001 bt_dev_err(hdev, "Intel fw corrupted: invalid cmd len");
2002 return -EFAULT;
2003 }
2004
2005 /* If there is a command that loads a patch in the firmware
2006 * file, then enable the patch upon success, otherwise just
2007 * disable the manufacturer mode, for example patch activation
2008 * is not required when the default firmware patch file is used
2009 * because there are no patch data to load.
2010 */
2011 if (*disable_patch && le16_to_cpu(cmd->opcode) == 0xfc8e)
2012 *disable_patch = 0;
2013
2014 cmd_param = *fw_ptr;
2015 *fw_ptr += cmd->plen;
2016 remain -= cmd->plen;
2017
2018 /* This reads the expected events when the above command is sent to the
2019 * device. Some vendor commands expects more than one events, for
2020 * example command status event followed by vendor specific event.
2021 * For this case, it only keeps the last expected event. so the command
2022 * can be sent with __hci_cmd_sync_ev() which returns the sk_buff of
2023 * last expected event.
2024 */
2025 while (remain > HCI_EVENT_HDR_SIZE && *fw_ptr[0] == 0x02) {
2026 (*fw_ptr)++;
2027 remain--;
2028
2029 evt = (struct hci_event_hdr *)(*fw_ptr);
2030 *fw_ptr += sizeof(*evt);
2031 remain -= sizeof(*evt);
2032
2033 if (remain < evt->plen) {
2034 bt_dev_err(hdev, "Intel fw corrupted: invalid evt len");
2035 return -EFAULT;
2036 }
2037
2038 evt_param = *fw_ptr;
2039 *fw_ptr += evt->plen;
2040 remain -= evt->plen;
2041 }
2042
2043 /* Every HCI commands in the firmware file has its correspond event.
2044 * If event is not found or remain is smaller than zero, the firmware
2045 * file is corrupted.
2046 */
2047 if (!evt || !evt_param || remain < 0) {
2048 bt_dev_err(hdev, "Intel fw corrupted: invalid evt read");
2049 return -EFAULT;
2050 }
2051
2052 skb = __hci_cmd_sync_ev(hdev, le16_to_cpu(cmd->opcode), cmd->plen,
2053 cmd_param, evt->evt, HCI_INIT_TIMEOUT);
2054 if (IS_ERR(skb)) {
2055 bt_dev_err(hdev, "sending Intel patch command (0x%4.4x) failed (%ld)",
2056 cmd->opcode, PTR_ERR(skb));
2057 return PTR_ERR(skb);
2058 }
2059
2060 /* It ensures that the returned event matches the event data read from
2061 * the firmware file. At fist, it checks the length and then
2062 * the contents of the event.
2063 */
2064 if (skb->len != evt->plen) {
2065 bt_dev_err(hdev, "mismatch event length (opcode 0x%4.4x)",
2066 le16_to_cpu(cmd->opcode));
2067 kfree_skb(skb);
2068 return -EFAULT;
2069 }
2070
2071 if (memcmp(skb->data, evt_param, evt->plen)) {
2072 bt_dev_err(hdev, "mismatch event parameter (opcode 0x%4.4x)",
2073 le16_to_cpu(cmd->opcode));
2074 kfree_skb(skb);
2075 return -EFAULT;
2076 }
2077 kfree_skb(skb);
2078
2079 return 0;
2080 }
2081
2082 static int btusb_setup_intel(struct hci_dev *hdev)
2083 {
2084 struct sk_buff *skb;
2085 const struct firmware *fw;
2086 const u8 *fw_ptr;
2087 int disable_patch, err;
2088 struct intel_version ver;
2089
2090 BT_DBG("%s", hdev->name);
2091
2092 /* The controller has a bug with the first HCI command sent to it
2093 * returning number of completed commands as zero. This would stall the
2094 * command processing in the Bluetooth core.
2095 *
2096 * As a workaround, send HCI Reset command first which will reset the
2097 * number of completed commands and allow normal command processing
2098 * from now on.
2099 */
2100 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
2101 if (IS_ERR(skb)) {
2102 bt_dev_err(hdev, "sending initial HCI reset command failed (%ld)",
2103 PTR_ERR(skb));
2104 return PTR_ERR(skb);
2105 }
2106 kfree_skb(skb);
2107
2108 /* Read Intel specific controller version first to allow selection of
2109 * which firmware file to load.
2110 *
2111 * The returned information are hardware variant and revision plus
2112 * firmware variant, revision and build number.
2113 */
2114 err = btintel_read_version(hdev, &ver);
2115 if (err)
2116 return err;
2117
2118 bt_dev_info(hdev, "read Intel version: %02x%02x%02x%02x%02x%02x%02x%02x%02x",
2119 ver.hw_platform, ver.hw_variant, ver.hw_revision,
2120 ver.fw_variant, ver.fw_revision, ver.fw_build_num,
2121 ver.fw_build_ww, ver.fw_build_yy, ver.fw_patch_num);
2122
2123 /* fw_patch_num indicates the version of patch the device currently
2124 * have. If there is no patch data in the device, it is always 0x00.
2125 * So, if it is other than 0x00, no need to patch the device again.
2126 */
2127 if (ver.fw_patch_num) {
2128 bt_dev_info(hdev, "Intel device is already patched. "
2129 "patch num: %02x", ver.fw_patch_num);
2130 goto complete;
2131 }
2132
2133 /* Opens the firmware patch file based on the firmware version read
2134 * from the controller. If it fails to open the matching firmware
2135 * patch file, it tries to open the default firmware patch file.
2136 * If no patch file is found, allow the device to operate without
2137 * a patch.
2138 */
2139 fw = btusb_setup_intel_get_fw(hdev, &ver);
2140 if (!fw)
2141 goto complete;
2142 fw_ptr = fw->data;
2143
2144 /* Enable the manufacturer mode of the controller.
2145 * Only while this mode is enabled, the driver can download the
2146 * firmware patch data and configuration parameters.
2147 */
2148 err = btintel_enter_mfg(hdev);
2149 if (err) {
2150 release_firmware(fw);
2151 return err;
2152 }
2153
2154 disable_patch = 1;
2155
2156 /* The firmware data file consists of list of Intel specific HCI
2157 * commands and its expected events. The first byte indicates the
2158 * type of the message, either HCI command or HCI event.
2159 *
2160 * It reads the command and its expected event from the firmware file,
2161 * and send to the controller. Once __hci_cmd_sync_ev() returns,
2162 * the returned event is compared with the event read from the firmware
2163 * file and it will continue until all the messages are downloaded to
2164 * the controller.
2165 *
2166 * Once the firmware patching is completed successfully,
2167 * the manufacturer mode is disabled with reset and activating the
2168 * downloaded patch.
2169 *
2170 * If the firmware patching fails, the manufacturer mode is
2171 * disabled with reset and deactivating the patch.
2172 *
2173 * If the default patch file is used, no reset is done when disabling
2174 * the manufacturer.
2175 */
2176 while (fw->size > fw_ptr - fw->data) {
2177 int ret;
2178
2179 ret = btusb_setup_intel_patching(hdev, fw, &fw_ptr,
2180 &disable_patch);
2181 if (ret < 0)
2182 goto exit_mfg_deactivate;
2183 }
2184
2185 release_firmware(fw);
2186
2187 if (disable_patch)
2188 goto exit_mfg_disable;
2189
2190 /* Patching completed successfully and disable the manufacturer mode
2191 * with reset and activate the downloaded firmware patches.
2192 */
2193 err = btintel_exit_mfg(hdev, true, true);
2194 if (err)
2195 return err;
2196
2197 /* Need build number for downloaded fw patches in
2198 * every power-on boot
2199 */
2200 err = btintel_read_version(hdev, &ver);
2201 if (err)
2202 return err;
2203 bt_dev_info(hdev, "Intel BT fw patch 0x%02x completed & activated",
2204 ver.fw_patch_num);
2205
2206 goto complete;
2207
2208 exit_mfg_disable:
2209 /* Disable the manufacturer mode without reset */
2210 err = btintel_exit_mfg(hdev, false, false);
2211 if (err)
2212 return err;
2213
2214 bt_dev_info(hdev, "Intel firmware patch completed");
2215
2216 goto complete;
2217
2218 exit_mfg_deactivate:
2219 release_firmware(fw);
2220
2221 /* Patching failed. Disable the manufacturer mode with reset and
2222 * deactivate the downloaded firmware patches.
2223 */
2224 err = btintel_exit_mfg(hdev, true, false);
2225 if (err)
2226 return err;
2227
2228 bt_dev_info(hdev, "Intel firmware patch completed and deactivated");
2229
2230 complete:
2231 /* Set the event mask for Intel specific vendor events. This enables
2232 * a few extra events that are useful during general operation.
2233 */
2234 btintel_set_event_mask_mfg(hdev, false);
2235
2236 btintel_check_bdaddr(hdev);
2237 return 0;
2238 }
2239
2240 static int inject_cmd_complete(struct hci_dev *hdev, __u16 opcode)
2241 {
2242 struct sk_buff *skb;
2243 struct hci_event_hdr *hdr;
2244 struct hci_ev_cmd_complete *evt;
2245
2246 skb = bt_skb_alloc(sizeof(*hdr) + sizeof(*evt) + 1, GFP_KERNEL);
2247 if (!skb)
2248 return -ENOMEM;
2249
2250 hdr = skb_put(skb, sizeof(*hdr));
2251 hdr->evt = HCI_EV_CMD_COMPLETE;
2252 hdr->plen = sizeof(*evt) + 1;
2253
2254 evt = skb_put(skb, sizeof(*evt));
2255 evt->ncmd = 0x01;
2256 evt->opcode = cpu_to_le16(opcode);
2257
2258 skb_put_u8(skb, 0x00);
2259
2260 hci_skb_pkt_type(skb) = HCI_EVENT_PKT;
2261
2262 return hci_recv_frame(hdev, skb);
2263 }
2264
2265 static int btusb_recv_bulk_intel(struct btusb_data *data, void *buffer,
2266 int count)
2267 {
2268 /* When the device is in bootloader mode, then it can send
2269 * events via the bulk endpoint. These events are treated the
2270 * same way as the ones received from the interrupt endpoint.
2271 */
2272 if (test_bit(BTUSB_BOOTLOADER, &data->flags))
2273 return btusb_recv_intr(data, buffer, count);
2274
2275 return btusb_recv_bulk(data, buffer, count);
2276 }
2277
2278 static void btusb_intel_bootup(struct btusb_data *data, const void *ptr,
2279 unsigned int len)
2280 {
2281 const struct intel_bootup *evt = ptr;
2282
2283 if (len != sizeof(*evt))
2284 return;
2285
2286 if (test_and_clear_bit(BTUSB_BOOTING, &data->flags))
2287 wake_up_bit(&data->flags, BTUSB_BOOTING);
2288 }
2289
2290 static void btusb_intel_secure_send_result(struct btusb_data *data,
2291 const void *ptr, unsigned int len)
2292 {
2293 const struct intel_secure_send_result *evt = ptr;
2294
2295 if (len != sizeof(*evt))
2296 return;
2297
2298 if (evt->result)
2299 set_bit(BTUSB_FIRMWARE_FAILED, &data->flags);
2300
2301 if (test_and_clear_bit(BTUSB_DOWNLOADING, &data->flags) &&
2302 test_bit(BTUSB_FIRMWARE_LOADED, &data->flags))
2303 wake_up_bit(&data->flags, BTUSB_DOWNLOADING);
2304 }
2305
2306 static int btusb_recv_event_intel(struct hci_dev *hdev, struct sk_buff *skb)
2307 {
2308 struct btusb_data *data = hci_get_drvdata(hdev);
2309
2310 if (test_bit(BTUSB_BOOTLOADER, &data->flags)) {
2311 struct hci_event_hdr *hdr = (void *)skb->data;
2312
2313 if (skb->len > HCI_EVENT_HDR_SIZE && hdr->evt == 0xff &&
2314 hdr->plen > 0) {
2315 const void *ptr = skb->data + HCI_EVENT_HDR_SIZE + 1;
2316 unsigned int len = skb->len - HCI_EVENT_HDR_SIZE - 1;
2317
2318 switch (skb->data[2]) {
2319 case 0x02:
2320 /* When switching to the operational firmware
2321 * the device sends a vendor specific event
2322 * indicating that the bootup completed.
2323 */
2324 btusb_intel_bootup(data, ptr, len);
2325 break;
2326 case 0x06:
2327 /* When the firmware loading completes the
2328 * device sends out a vendor specific event
2329 * indicating the result of the firmware
2330 * loading.
2331 */
2332 btusb_intel_secure_send_result(data, ptr, len);
2333 break;
2334 }
2335 }
2336 }
2337
2338 return hci_recv_frame(hdev, skb);
2339 }
2340
2341 static int btusb_send_frame_intel(struct hci_dev *hdev, struct sk_buff *skb)
2342 {
2343 struct btusb_data *data = hci_get_drvdata(hdev);
2344 struct urb *urb;
2345
2346 BT_DBG("%s", hdev->name);
2347
2348 switch (hci_skb_pkt_type(skb)) {
2349 case HCI_COMMAND_PKT:
2350 if (test_bit(BTUSB_BOOTLOADER, &data->flags)) {
2351 struct hci_command_hdr *cmd = (void *)skb->data;
2352 __u16 opcode = le16_to_cpu(cmd->opcode);
2353
2354 /* When in bootloader mode and the command 0xfc09
2355 * is received, it needs to be send down the
2356 * bulk endpoint. So allocate a bulk URB instead.
2357 */
2358 if (opcode == 0xfc09)
2359 urb = alloc_bulk_urb(hdev, skb);
2360 else
2361 urb = alloc_ctrl_urb(hdev, skb);
2362
2363 /* When the 0xfc01 command is issued to boot into
2364 * the operational firmware, it will actually not
2365 * send a command complete event. To keep the flow
2366 * control working inject that event here.
2367 */
2368 if (opcode == 0xfc01)
2369 inject_cmd_complete(hdev, opcode);
2370 } else {
2371 urb = alloc_ctrl_urb(hdev, skb);
2372 }
2373 if (IS_ERR(urb))
2374 return PTR_ERR(urb);
2375
2376 hdev->stat.cmd_tx++;
2377 return submit_or_queue_tx_urb(hdev, urb);
2378
2379 case HCI_ACLDATA_PKT:
2380 urb = alloc_bulk_urb(hdev, skb);
2381 if (IS_ERR(urb))
2382 return PTR_ERR(urb);
2383
2384 hdev->stat.acl_tx++;
2385 return submit_or_queue_tx_urb(hdev, urb);
2386
2387 case HCI_SCODATA_PKT:
2388 if (hci_conn_num(hdev, SCO_LINK) < 1)
2389 return -ENODEV;
2390
2391 urb = alloc_isoc_urb(hdev, skb);
2392 if (IS_ERR(urb))
2393 return PTR_ERR(urb);
2394
2395 hdev->stat.sco_tx++;
2396 return submit_tx_urb(hdev, urb);
2397 }
2398
2399 return -EILSEQ;
2400 }
2401
2402 static bool btusb_setup_intel_new_get_fw_name(struct intel_version *ver,
2403 struct intel_boot_params *params,
2404 char *fw_name, size_t len,
2405 const char *suffix)
2406 {
2407 switch (ver->hw_variant) {
2408 case 0x0b: /* SfP */
2409 case 0x0c: /* WsP */
2410 snprintf(fw_name, len, "intel/ibt-%u-%u.%s",
2411 le16_to_cpu(ver->hw_variant),
2412 le16_to_cpu(params->dev_revid),
2413 suffix);
2414 break;
2415 case 0x11: /* JfP */
2416 case 0x12: /* ThP */
2417 case 0x13: /* HrP */
2418 case 0x14: /* CcP */
2419 snprintf(fw_name, len, "intel/ibt-%u-%u-%u.%s",
2420 le16_to_cpu(ver->hw_variant),
2421 le16_to_cpu(ver->hw_revision),
2422 le16_to_cpu(ver->fw_revision),
2423 suffix);
2424 break;
2425 default:
2426 return false;
2427 }
2428 return true;
2429 }
2430
2431 static void btusb_setup_intel_newgen_get_fw_name(const struct intel_version_tlv *ver_tlv,
2432 char *fw_name, size_t len,
2433 const char *suffix)
2434 {
2435 /* The firmware file name for new generation controllers will be
2436 * ibt-<cnvi_top type+cnvi_top step>-<cnvr_top type+cnvr_top step>
2437 */
2438 snprintf(fw_name, len, "intel/ibt-%04x-%04x.%s",
2439 INTEL_CNVX_TOP_PACK_SWAB(INTEL_CNVX_TOP_TYPE(ver_tlv->cnvi_top),
2440 INTEL_CNVX_TOP_STEP(ver_tlv->cnvi_top)),
2441 INTEL_CNVX_TOP_PACK_SWAB(INTEL_CNVX_TOP_TYPE(ver_tlv->cnvr_top),
2442 INTEL_CNVX_TOP_STEP(ver_tlv->cnvr_top)),
2443 suffix);
2444 }
2445
2446 static int btusb_intel_download_firmware_newgen(struct hci_dev *hdev,
2447 struct intel_version_tlv *ver,
2448 u32 *boot_param)
2449 {
2450 const struct firmware *fw;
2451 char fwname[64];
2452 int err;
2453 struct btusb_data *data = hci_get_drvdata(hdev);
2454
2455 if (!ver || !boot_param)
2456 return -EINVAL;
2457
2458 /* The hardware platform number has a fixed value of 0x37 and
2459 * for now only accept this single value.
2460 */
2461 if (INTEL_HW_PLATFORM(ver->cnvi_bt) != 0x37) {
2462 bt_dev_err(hdev, "Unsupported Intel hardware platform (0x%2x)",
2463 INTEL_HW_PLATFORM(ver->cnvi_bt));
2464 return -EINVAL;
2465 }
2466
2467 /* The firmware variant determines if the device is in bootloader
2468 * mode or is running operational firmware. The value 0x03 identifies
2469 * the bootloader and the value 0x23 identifies the operational
2470 * firmware.
2471 *
2472 * When the operational firmware is already present, then only
2473 * the check for valid Bluetooth device address is needed. This
2474 * determines if the device will be added as configured or
2475 * unconfigured controller.
2476 *
2477 * It is not possible to use the Secure Boot Parameters in this
2478 * case since that command is only available in bootloader mode.
2479 */
2480 if (ver->img_type == 0x03) {
2481 clear_bit(BTUSB_BOOTLOADER, &data->flags);
2482 btintel_check_bdaddr(hdev);
2483 return 0;
2484 }
2485
2486 /* Check for supported iBT hardware variants of this firmware
2487 * loading method.
2488 *
2489 * This check has been put in place to ensure correct forward
2490 * compatibility options when newer hardware variants come along.
2491 */
2492 switch (INTEL_HW_VARIANT(ver->cnvi_bt)) {
2493 case 0x17: /* TyP */
2494 case 0x18: /* Slr */
2495 case 0x19: /* Slr-F */
2496 break;
2497 default:
2498 bt_dev_err(hdev, "Unsupported Intel hardware variant (0x%x)",
2499 INTEL_HW_VARIANT(ver->cnvi_bt));
2500 return -EINVAL;
2501 }
2502
2503 /* If the device is not in bootloader mode, then the only possible
2504 * choice is to return an error and abort the device initialization.
2505 */
2506 if (ver->img_type != 0x01) {
2507 bt_dev_err(hdev, "Unsupported Intel firmware variant (0x%x)",
2508 ver->img_type);
2509 return -ENODEV;
2510 }
2511
2512 /* It is required that every single firmware fragment is acknowledged
2513 * with a command complete event. If the boot parameters indicate
2514 * that this bootloader does not send them, then abort the setup.
2515 */
2516 if (ver->limited_cce != 0x00) {
2517 bt_dev_err(hdev, "Unsupported Intel firmware loading method (0x%x)",
2518 ver->limited_cce);
2519 return -EINVAL;
2520 }
2521
2522 /* Secure boot engine type should be either 1 (ECDSA) or 0 (RSA) */
2523 if (ver->sbe_type > 0x01) {
2524 bt_dev_err(hdev, "Unsupported Intel secure boot engine type (0x%x)",
2525 ver->sbe_type);
2526 return -EINVAL;
2527 }
2528
2529 /* If the OTP has no valid Bluetooth device address, then there will
2530 * also be no valid address for the operational firmware.
2531 */
2532 if (!bacmp(&ver->otp_bd_addr, BDADDR_ANY)) {
2533 bt_dev_info(hdev, "No device address configured");
2534 set_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks);
2535 }
2536
2537 btusb_setup_intel_newgen_get_fw_name(ver, fwname, sizeof(fwname), "sfi");
2538 err = request_firmware(&fw, fwname, &hdev->dev);
2539 if (err < 0) {
2540 bt_dev_err(hdev, "Failed to load Intel firmware file (%d)", err);
2541 return err;
2542 }
2543
2544 bt_dev_info(hdev, "Found device firmware: %s", fwname);
2545
2546 if (fw->size < 644) {
2547 bt_dev_err(hdev, "Invalid size of firmware file (%zu)",
2548 fw->size);
2549 err = -EBADF;
2550 goto done;
2551 }
2552
2553 set_bit(BTUSB_DOWNLOADING, &data->flags);
2554
2555 /* Start firmware downloading and get boot parameter */
2556 err = btintel_download_firmware_newgen(hdev, fw, boot_param,
2557 INTEL_HW_VARIANT(ver->cnvi_bt),
2558 ver->sbe_type);
2559 if (err < 0) {
2560 /* When FW download fails, send Intel Reset to retry
2561 * FW download.
2562 */
2563 btintel_reset_to_bootloader(hdev);
2564 goto done;
2565 }
2566 set_bit(BTUSB_FIRMWARE_LOADED, &data->flags);
2567
2568 bt_dev_info(hdev, "Waiting for firmware download to complete");
2569
2570 /* Before switching the device into operational mode and with that
2571 * booting the loaded firmware, wait for the bootloader notification
2572 * that all fragments have been successfully received.
2573 *
2574 * When the event processing receives the notification, then the
2575 * BTUSB_DOWNLOADING flag will be cleared.
2576 *
2577 * The firmware loading should not take longer than 5 seconds
2578 * and thus just timeout if that happens and fail the setup
2579 * of this device.
2580 */
2581 err = wait_on_bit_timeout(&data->flags, BTUSB_DOWNLOADING,
2582 TASK_INTERRUPTIBLE,
2583 msecs_to_jiffies(5000));
2584 if (err == -EINTR) {
2585 bt_dev_err(hdev, "Firmware loading interrupted");
2586 goto done;
2587 }
2588
2589 if (err) {
2590 bt_dev_err(hdev, "Firmware loading timeout");
2591 err = -ETIMEDOUT;
2592 btintel_reset_to_bootloader(hdev);
2593 goto done;
2594 }
2595
2596 if (test_bit(BTUSB_FIRMWARE_FAILED, &data->flags)) {
2597 bt_dev_err(hdev, "Firmware loading failed");
2598 err = -ENOEXEC;
2599 goto done;
2600 }
2601
2602 done:
2603 release_firmware(fw);
2604 return err;
2605 }
2606
2607 static int btusb_intel_download_firmware(struct hci_dev *hdev,
2608 struct intel_version *ver,
2609 struct intel_boot_params *params,
2610 u32 *boot_param)
2611 {
2612 const struct firmware *fw;
2613 char fwname[64];
2614 int err;
2615 struct btusb_data *data = hci_get_drvdata(hdev);
2616
2617 if (!ver || !params)
2618 return -EINVAL;
2619
2620 /* The hardware platform number has a fixed value of 0x37 and
2621 * for now only accept this single value.
2622 */
2623 if (ver->hw_platform != 0x37) {
2624 bt_dev_err(hdev, "Unsupported Intel hardware platform (%u)",
2625 ver->hw_platform);
2626 return -EINVAL;
2627 }
2628
2629 /* Check for supported iBT hardware variants of this firmware
2630 * loading method.
2631 *
2632 * This check has been put in place to ensure correct forward
2633 * compatibility options when newer hardware variants come along.
2634 */
2635 switch (ver->hw_variant) {
2636 case 0x0b: /* SfP */
2637 case 0x0c: /* WsP */
2638 case 0x11: /* JfP */
2639 case 0x12: /* ThP */
2640 case 0x13: /* HrP */
2641 case 0x14: /* CcP */
2642 break;
2643 default:
2644 bt_dev_err(hdev, "Unsupported Intel hardware variant (%u)",
2645 ver->hw_variant);
2646 return -EINVAL;
2647 }
2648
2649 btintel_version_info(hdev, ver);
2650
2651 /* The firmware variant determines if the device is in bootloader
2652 * mode or is running operational firmware. The value 0x06 identifies
2653 * the bootloader and the value 0x23 identifies the operational
2654 * firmware.
2655 *
2656 * When the operational firmware is already present, then only
2657 * the check for valid Bluetooth device address is needed. This
2658 * determines if the device will be added as configured or
2659 * unconfigured controller.
2660 *
2661 * It is not possible to use the Secure Boot Parameters in this
2662 * case since that command is only available in bootloader mode.
2663 */
2664 if (ver->fw_variant == 0x23) {
2665 clear_bit(BTUSB_BOOTLOADER, &data->flags);
2666 btintel_check_bdaddr(hdev);
2667 return 0;
2668 }
2669
2670 /* If the device is not in bootloader mode, then the only possible
2671 * choice is to return an error and abort the device initialization.
2672 */
2673 if (ver->fw_variant != 0x06) {
2674 bt_dev_err(hdev, "Unsupported Intel firmware variant (%u)",
2675 ver->fw_variant);
2676 return -ENODEV;
2677 }
2678
2679 /* Read the secure boot parameters to identify the operating
2680 * details of the bootloader.
2681 */
2682 err = btintel_read_boot_params(hdev, params);
2683 if (err)
2684 return err;
2685
2686 /* It is required that every single firmware fragment is acknowledged
2687 * with a command complete event. If the boot parameters indicate
2688 * that this bootloader does not send them, then abort the setup.
2689 */
2690 if (params->limited_cce != 0x00) {
2691 bt_dev_err(hdev, "Unsupported Intel firmware loading method (%u)",
2692 params->limited_cce);
2693 return -EINVAL;
2694 }
2695
2696 /* If the OTP has no valid Bluetooth device address, then there will
2697 * also be no valid address for the operational firmware.
2698 */
2699 if (!bacmp(&params->otp_bdaddr, BDADDR_ANY)) {
2700 bt_dev_info(hdev, "No device address configured");
2701 set_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks);
2702 }
2703
2704 /* With this Intel bootloader only the hardware variant and device
2705 * revision information are used to select the right firmware for SfP
2706 * and WsP.
2707 *
2708 * The firmware filename is ibt-<hw_variant>-<dev_revid>.sfi.
2709 *
2710 * Currently the supported hardware variants are:
2711 * 11 (0x0b) for iBT3.0 (LnP/SfP)
2712 * 12 (0x0c) for iBT3.5 (WsP)
2713 *
2714 * For ThP/JfP and for future SKU's, the FW name varies based on HW
2715 * variant, HW revision and FW revision, as these are dependent on CNVi
2716 * and RF Combination.
2717 *
2718 * 17 (0x11) for iBT3.5 (JfP)
2719 * 18 (0x12) for iBT3.5 (ThP)
2720 *
2721 * The firmware file name for these will be
2722 * ibt-<hw_variant>-<hw_revision>-<fw_revision>.sfi.
2723 *
2724 */
2725 err = btusb_setup_intel_new_get_fw_name(ver, params, fwname,
2726 sizeof(fwname), "sfi");
2727 if (!err) {
2728 bt_dev_err(hdev, "Unsupported Intel firmware naming");
2729 return -EINVAL;
2730 }
2731
2732 err = request_firmware(&fw, fwname, &hdev->dev);
2733 if (err < 0) {
2734 bt_dev_err(hdev, "Failed to load Intel firmware file (%d)", err);
2735 return err;
2736 }
2737
2738 bt_dev_info(hdev, "Found device firmware: %s", fwname);
2739
2740 if (fw->size < 644) {
2741 bt_dev_err(hdev, "Invalid size of firmware file (%zu)",
2742 fw->size);
2743 err = -EBADF;
2744 goto done;
2745 }
2746
2747 set_bit(BTUSB_DOWNLOADING, &data->flags);
2748
2749 /* Start firmware downloading and get boot parameter */
2750 err = btintel_download_firmware(hdev, fw, boot_param);
2751 if (err < 0) {
2752 /* When FW download fails, send Intel Reset to retry
2753 * FW download.
2754 */
2755 btintel_reset_to_bootloader(hdev);
2756 goto done;
2757 }
2758 set_bit(BTUSB_FIRMWARE_LOADED, &data->flags);
2759
2760 bt_dev_info(hdev, "Waiting for firmware download to complete");
2761
2762 /* Before switching the device into operational mode and with that
2763 * booting the loaded firmware, wait for the bootloader notification
2764 * that all fragments have been successfully received.
2765 *
2766 * When the event processing receives the notification, then the
2767 * BTUSB_DOWNLOADING flag will be cleared.
2768 *
2769 * The firmware loading should not take longer than 5 seconds
2770 * and thus just timeout if that happens and fail the setup
2771 * of this device.
2772 */
2773 err = wait_on_bit_timeout(&data->flags, BTUSB_DOWNLOADING,
2774 TASK_INTERRUPTIBLE,
2775 msecs_to_jiffies(5000));
2776 if (err == -EINTR) {
2777 bt_dev_err(hdev, "Firmware loading interrupted");
2778 goto done;
2779 }
2780
2781 if (err) {
2782 bt_dev_err(hdev, "Firmware loading timeout");
2783 err = -ETIMEDOUT;
2784 btintel_reset_to_bootloader(hdev);
2785 goto done;
2786 }
2787
2788 if (test_bit(BTUSB_FIRMWARE_FAILED, &data->flags)) {
2789 bt_dev_err(hdev, "Firmware loading failed");
2790 err = -ENOEXEC;
2791 goto done;
2792 }
2793
2794 done:
2795 release_firmware(fw);
2796 return err;
2797 }
2798
2799 static int btusb_setup_intel_new(struct hci_dev *hdev)
2800 {
2801 struct btusb_data *data = hci_get_drvdata(hdev);
2802 struct intel_version ver;
2803 struct intel_boot_params params;
2804 u32 boot_param;
2805 char ddcname[64];
2806 ktime_t calltime, delta, rettime;
2807 unsigned long long duration;
2808 int err;
2809 struct intel_debug_features features;
2810
2811 BT_DBG("%s", hdev->name);
2812
2813 /* Set the default boot parameter to 0x0 and it is updated to
2814 * SKU specific boot parameter after reading Intel_Write_Boot_Params
2815 * command while downloading the firmware.
2816 */
2817 boot_param = 0x00000000;
2818
2819 calltime = ktime_get();
2820
2821 /* Read the Intel version information to determine if the device
2822 * is in bootloader mode or if it already has operational firmware
2823 * loaded.
2824 */
2825 err = btintel_read_version(hdev, &ver);
2826 if (err) {
2827 bt_dev_err(hdev, "Intel Read version failed (%d)", err);
2828 btintel_reset_to_bootloader(hdev);
2829 return err;
2830 }
2831
2832 err = btusb_intel_download_firmware(hdev, &ver, &params, &boot_param);
2833 if (err)
2834 return err;
2835
2836 /* controller is already having an operational firmware */
2837 if (ver.fw_variant == 0x23)
2838 goto finish;
2839
2840 rettime = ktime_get();
2841 delta = ktime_sub(rettime, calltime);
2842 duration = (unsigned long long) ktime_to_ns(delta) >> 10;
2843
2844 bt_dev_info(hdev, "Firmware loaded in %llu usecs", duration);
2845
2846 calltime = ktime_get();
2847
2848 set_bit(BTUSB_BOOTING, &data->flags);
2849
2850 err = btintel_send_intel_reset(hdev, boot_param);
2851 if (err) {
2852 bt_dev_err(hdev, "Intel Soft Reset failed (%d)", err);
2853 btintel_reset_to_bootloader(hdev);
2854 return err;
2855 }
2856
2857 /* The bootloader will not indicate when the device is ready. This
2858 * is done by the operational firmware sending bootup notification.
2859 *
2860 * Booting into operational firmware should not take longer than
2861 * 1 second. However if that happens, then just fail the setup
2862 * since something went wrong.
2863 */
2864 bt_dev_info(hdev, "Waiting for device to boot");
2865
2866 err = wait_on_bit_timeout(&data->flags, BTUSB_BOOTING,
2867 TASK_INTERRUPTIBLE,
2868 msecs_to_jiffies(1000));
2869
2870 if (err == -EINTR) {
2871 bt_dev_err(hdev, "Device boot interrupted");
2872 return -EINTR;
2873 }
2874
2875 if (err) {
2876 bt_dev_err(hdev, "Device boot timeout");
2877 btintel_reset_to_bootloader(hdev);
2878 return -ETIMEDOUT;
2879 }
2880
2881 rettime = ktime_get();
2882 delta = ktime_sub(rettime, calltime);
2883 duration = (unsigned long long) ktime_to_ns(delta) >> 10;
2884
2885 bt_dev_info(hdev, "Device booted in %llu usecs", duration);
2886
2887 clear_bit(BTUSB_BOOTLOADER, &data->flags);
2888
2889 err = btusb_setup_intel_new_get_fw_name(&ver, &params, ddcname,
2890 sizeof(ddcname), "ddc");
2891
2892 if (!err) {
2893 bt_dev_err(hdev, "Unsupported Intel firmware naming");
2894 } else {
2895 /* Once the device is running in operational mode, it needs to
2896 * apply the device configuration (DDC) parameters.
2897 *
2898 * The device can work without DDC parameters, so even if it
2899 * fails to load the file, no need to fail the setup.
2900 */
2901 btintel_load_ddc_config(hdev, ddcname);
2902 }
2903
2904 /* Read the Intel supported features and if new exception formats
2905 * supported, need to load the additional DDC config to enable.
2906 */
2907 btintel_read_debug_features(hdev, &features);
2908
2909 /* Set DDC mask for available debug features */
2910 btintel_set_debug_features(hdev, &features);
2911
2912 /* Read the Intel version information after loading the FW */
2913 err = btintel_read_version(hdev, &ver);
2914 if (err)
2915 return err;
2916
2917 btintel_version_info(hdev, &ver);
2918
2919 finish:
2920 /* All Intel controllers that support the Microsoft vendor
2921 * extension are using 0xFC1E for VsMsftOpCode.
2922 */
2923 switch (ver.hw_variant) {
2924 case 0x12: /* ThP */
2925 hci_set_msft_opcode(hdev, 0xFC1E);
2926 break;
2927 }
2928
2929 /* Set the event mask for Intel specific vendor events. This enables
2930 * a few extra events that are useful during general operation. It
2931 * does not enable any debugging related events.
2932 *
2933 * The device will function correctly without these events enabled
2934 * and thus no need to fail the setup.
2935 */
2936 btintel_set_event_mask(hdev, false);
2937
2938 return 0;
2939 }
2940
2941 static int btusb_setup_intel_newgen(struct hci_dev *hdev)
2942 {
2943 struct btusb_data *data = hci_get_drvdata(hdev);
2944 u32 boot_param;
2945 char ddcname[64];
2946 ktime_t calltime, delta, rettime;
2947 unsigned long long duration;
2948 int err;
2949 struct intel_debug_features features;
2950 struct intel_version_tlv version;
2951
2952 bt_dev_dbg(hdev, "");
2953
2954 /* Set the default boot parameter to 0x0 and it is updated to
2955 * SKU specific boot parameter after reading Intel_Write_Boot_Params
2956 * command while downloading the firmware.
2957 */
2958 boot_param = 0x00000000;
2959
2960 calltime = ktime_get();
2961
2962 /* Read the Intel version information to determine if the device
2963 * is in bootloader mode or if it already has operational firmware
2964 * loaded.
2965 */
2966 err = btintel_read_version_tlv(hdev, &version);
2967 if (err) {
2968 bt_dev_err(hdev, "Intel Read version failed (%d)", err);
2969 btintel_reset_to_bootloader(hdev);
2970 return err;
2971 }
2972
2973 btintel_version_info_tlv(hdev, &version);
2974
2975 err = btusb_intel_download_firmware_newgen(hdev, &version, &boot_param);
2976 if (err)
2977 return err;
2978
2979 /* check if controller is already having an operational firmware */
2980 if (version.img_type == 0x03)
2981 goto finish;
2982
2983 rettime = ktime_get();
2984 delta = ktime_sub(rettime, calltime);
2985 duration = (unsigned long long)ktime_to_ns(delta) >> 10;
2986
2987 bt_dev_info(hdev, "Firmware loaded in %llu usecs", duration);
2988
2989 calltime = ktime_get();
2990
2991 set_bit(BTUSB_BOOTING, &data->flags);
2992
2993 err = btintel_send_intel_reset(hdev, boot_param);
2994 if (err) {
2995 bt_dev_err(hdev, "Intel Soft Reset failed (%d)", err);
2996 btintel_reset_to_bootloader(hdev);
2997 return err;
2998 }
2999
3000 /* The bootloader will not indicate when the device is ready. This
3001 * is done by the operational firmware sending bootup notification.
3002 *
3003 * Booting into operational firmware should not take longer than
3004 * 1 second. However if that happens, then just fail the setup
3005 * since something went wrong.
3006 */
3007 bt_dev_info(hdev, "Waiting for device to boot");
3008
3009 err = wait_on_bit_timeout(&data->flags, BTUSB_BOOTING,
3010 TASK_INTERRUPTIBLE,
3011 msecs_to_jiffies(1000));
3012
3013 if (err == -EINTR) {
3014 bt_dev_err(hdev, "Device boot interrupted");
3015 return -EINTR;
3016 }
3017
3018 if (err) {
3019 bt_dev_err(hdev, "Device boot timeout");
3020 btintel_reset_to_bootloader(hdev);
3021 return -ETIMEDOUT;
3022 }
3023
3024 rettime = ktime_get();
3025 delta = ktime_sub(rettime, calltime);
3026 duration = (unsigned long long)ktime_to_ns(delta) >> 10;
3027
3028 bt_dev_info(hdev, "Device booted in %llu usecs", duration);
3029
3030 clear_bit(BTUSB_BOOTLOADER, &data->flags);
3031
3032 btusb_setup_intel_newgen_get_fw_name(&version, ddcname, sizeof(ddcname),
3033 "ddc");
3034 /* Once the device is running in operational mode, it needs to
3035 * apply the device configuration (DDC) parameters.
3036 *
3037 * The device can work without DDC parameters, so even if it
3038 * fails to load the file, no need to fail the setup.
3039 */
3040 btintel_load_ddc_config(hdev, ddcname);
3041
3042 /* Read the Intel supported features and if new exception formats
3043 * supported, need to load the additional DDC config to enable.
3044 */
3045 btintel_read_debug_features(hdev, &features);
3046
3047 /* Set DDC mask for available debug features */
3048 btintel_set_debug_features(hdev, &features);
3049
3050 /* Read the Intel version information after loading the FW */
3051 err = btintel_read_version_tlv(hdev, &version);
3052 if (err)
3053 return err;
3054
3055 btintel_version_info_tlv(hdev, &version);
3056
3057 finish:
3058 /* Set the event mask for Intel specific vendor events. This enables
3059 * a few extra events that are useful during general operation. It
3060 * does not enable any debugging related events.
3061 *
3062 * The device will function correctly without these events enabled
3063 * and thus no need to fail the setup.
3064 */
3065 btintel_set_event_mask(hdev, false);
3066
3067 return 0;
3068 }
3069 static int btusb_shutdown_intel(struct hci_dev *hdev)
3070 {
3071 struct sk_buff *skb;
3072 long ret;
3073
3074 /* In the shutdown sequence where Bluetooth is turned off followed
3075 * by WiFi being turned off, turning WiFi back on causes issue with
3076 * the RF calibration.
3077 *
3078 * To ensure that any RF activity has been stopped, issue HCI Reset
3079 * command to clear all ongoing activity including advertising,
3080 * scanning etc.
3081 */
3082 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
3083 if (IS_ERR(skb)) {
3084 ret = PTR_ERR(skb);
3085 bt_dev_err(hdev, "HCI reset during shutdown failed");
3086 return ret;
3087 }
3088 kfree_skb(skb);
3089
3090 /* Some platforms have an issue with BT LED when the interface is
3091 * down or BT radio is turned off, which takes 5 seconds to BT LED
3092 * goes off. This command turns off the BT LED immediately.
3093 */
3094 skb = __hci_cmd_sync(hdev, 0xfc3f, 0, NULL, HCI_INIT_TIMEOUT);
3095 if (IS_ERR(skb)) {
3096 ret = PTR_ERR(skb);
3097 bt_dev_err(hdev, "turning off Intel device LED failed");
3098 return ret;
3099 }
3100 kfree_skb(skb);
3101
3102 return 0;
3103 }
3104
3105 static int btusb_shutdown_intel_new(struct hci_dev *hdev)
3106 {
3107 struct sk_buff *skb;
3108
3109 /* Send HCI Reset to the controller to stop any BT activity which
3110 * were triggered. This will help to save power and maintain the
3111 * sync b/w Host and controller
3112 */
3113 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
3114 if (IS_ERR(skb)) {
3115 bt_dev_err(hdev, "HCI reset during shutdown failed");
3116 return PTR_ERR(skb);
3117 }
3118 kfree_skb(skb);
3119
3120 return 0;
3121 }
3122
3123 #define FIRMWARE_MT7663 "mediatek/mt7663pr2h.bin"
3124 #define FIRMWARE_MT7668 "mediatek/mt7668pr2h.bin"
3125
3126 #define HCI_WMT_MAX_EVENT_SIZE 64
3127
3128 enum {
3129 BTMTK_WMT_PATCH_DWNLD = 0x1,
3130 BTMTK_WMT_FUNC_CTRL = 0x6,
3131 BTMTK_WMT_RST = 0x7,
3132 BTMTK_WMT_SEMAPHORE = 0x17,
3133 };
3134
3135 enum {
3136 BTMTK_WMT_INVALID,
3137 BTMTK_WMT_PATCH_UNDONE,
3138 BTMTK_WMT_PATCH_DONE,
3139 BTMTK_WMT_ON_UNDONE,
3140 BTMTK_WMT_ON_DONE,
3141 BTMTK_WMT_ON_PROGRESS,
3142 };
3143
3144 struct btmtk_wmt_hdr {
3145 u8 dir;
3146 u8 op;
3147 __le16 dlen;
3148 u8 flag;
3149 } __packed;
3150
3151 struct btmtk_hci_wmt_cmd {
3152 struct btmtk_wmt_hdr hdr;
3153 u8 data[256];
3154 } __packed;
3155
3156 struct btmtk_hci_wmt_evt {
3157 struct hci_event_hdr hhdr;
3158 struct btmtk_wmt_hdr whdr;
3159 } __packed;
3160
3161 struct btmtk_hci_wmt_evt_funcc {
3162 struct btmtk_hci_wmt_evt hwhdr;
3163 __be16 status;
3164 } __packed;
3165
3166 struct btmtk_tci_sleep {
3167 u8 mode;
3168 __le16 duration;
3169 __le16 host_duration;
3170 u8 host_wakeup_pin;
3171 u8 time_compensation;
3172 } __packed;
3173
3174 struct btmtk_hci_wmt_params {
3175 u8 op;
3176 u8 flag;
3177 u16 dlen;
3178 const void *data;
3179 u32 *status;
3180 };
3181
3182 static void btusb_mtk_wmt_recv(struct urb *urb)
3183 {
3184 struct hci_dev *hdev = urb->context;
3185 struct btusb_data *data = hci_get_drvdata(hdev);
3186 struct hci_event_hdr *hdr;
3187 struct sk_buff *skb;
3188 int err;
3189
3190 if (urb->status == 0 && urb->actual_length > 0) {
3191 hdev->stat.byte_rx += urb->actual_length;
3192
3193 /* WMT event shouldn't be fragmented and the size should be
3194 * less than HCI_WMT_MAX_EVENT_SIZE.
3195 */
3196 skb = bt_skb_alloc(HCI_WMT_MAX_EVENT_SIZE, GFP_ATOMIC);
3197 if (!skb) {
3198 hdev->stat.err_rx++;
3199 return;
3200 }
3201
3202 hci_skb_pkt_type(skb) = HCI_EVENT_PKT;
3203 skb_put_data(skb, urb->transfer_buffer, urb->actual_length);
3204
3205 hdr = (void *)skb->data;
3206 /* Fix up the vendor event id with 0xff for vendor specific
3207 * instead of 0xe4 so that event send via monitoring socket can
3208 * be parsed properly.
3209 */
3210 hdr->evt = 0xff;
3211
3212 /* When someone waits for the WMT event, the skb is being cloned
3213 * and being processed the events from there then.
3214 */
3215 if (test_bit(BTUSB_TX_WAIT_VND_EVT, &data->flags)) {
3216 data->evt_skb = skb_clone(skb, GFP_ATOMIC);
3217 if (!data->evt_skb) {
3218 kfree_skb(skb);
3219 return;
3220 }
3221 }
3222
3223 err = hci_recv_frame(hdev, skb);
3224 if (err < 0) {
3225 kfree_skb(data->evt_skb);
3226 data->evt_skb = NULL;
3227 return;
3228 }
3229
3230 if (test_and_clear_bit(BTUSB_TX_WAIT_VND_EVT,
3231 &data->flags)) {
3232 /* Barrier to sync with other CPUs */
3233 smp_mb__after_atomic();
3234 wake_up_bit(&data->flags,
3235 BTUSB_TX_WAIT_VND_EVT);
3236 }
3237 return;
3238 } else if (urb->status == -ENOENT) {
3239 /* Avoid suspend failed when usb_kill_urb */
3240 return;
3241 }
3242
3243 usb_mark_last_busy(data->udev);
3244
3245 /* The URB complete handler is still called with urb->actual_length = 0
3246 * when the event is not available, so we should keep re-submitting
3247 * URB until WMT event returns, Also, It's necessary to wait some time
3248 * between the two consecutive control URBs to relax the target device
3249 * to generate the event. Otherwise, the WMT event cannot return from
3250 * the device successfully.
3251 */
3252 udelay(100);
3253
3254 usb_anchor_urb(urb, &data->ctrl_anchor);
3255 err = usb_submit_urb(urb, GFP_ATOMIC);
3256 if (err < 0) {
3257 /* -EPERM: urb is being killed;
3258 * -ENODEV: device got disconnected
3259 */
3260 if (err != -EPERM && err != -ENODEV)
3261 bt_dev_err(hdev, "urb %p failed to resubmit (%d)",
3262 urb, -err);
3263 usb_unanchor_urb(urb);
3264 }
3265 }
3266
3267 static int btusb_mtk_submit_wmt_recv_urb(struct hci_dev *hdev)
3268 {
3269 struct btusb_data *data = hci_get_drvdata(hdev);
3270 struct usb_ctrlrequest *dr;
3271 unsigned char *buf;
3272 int err, size = 64;
3273 unsigned int pipe;
3274 struct urb *urb;
3275
3276 urb = usb_alloc_urb(0, GFP_KERNEL);
3277 if (!urb)
3278 return -ENOMEM;
3279
3280 dr = kmalloc(sizeof(*dr), GFP_KERNEL);
3281 if (!dr) {
3282 usb_free_urb(urb);
3283 return -ENOMEM;
3284 }
3285
3286 dr->bRequestType = USB_TYPE_VENDOR | USB_DIR_IN;
3287 dr->bRequest = 1;
3288 dr->wIndex = cpu_to_le16(0);
3289 dr->wValue = cpu_to_le16(48);
3290 dr->wLength = cpu_to_le16(size);
3291
3292 buf = kmalloc(size, GFP_KERNEL);
3293 if (!buf) {
3294 kfree(dr);
3295 usb_free_urb(urb);
3296 return -ENOMEM;
3297 }
3298
3299 pipe = usb_rcvctrlpipe(data->udev, 0);
3300
3301 usb_fill_control_urb(urb, data->udev, pipe, (void *)dr,
3302 buf, size, btusb_mtk_wmt_recv, hdev);
3303
3304 urb->transfer_flags |= URB_FREE_BUFFER;
3305
3306 usb_anchor_urb(urb, &data->ctrl_anchor);
3307 err = usb_submit_urb(urb, GFP_KERNEL);
3308 if (err < 0) {
3309 if (err != -EPERM && err != -ENODEV)
3310 bt_dev_err(hdev, "urb %p submission failed (%d)",
3311 urb, -err);
3312 usb_unanchor_urb(urb);
3313 }
3314
3315 usb_free_urb(urb);
3316
3317 return err;
3318 }
3319
3320 static int btusb_mtk_hci_wmt_sync(struct hci_dev *hdev,
3321 struct btmtk_hci_wmt_params *wmt_params)
3322 {
3323 struct btusb_data *data = hci_get_drvdata(hdev);
3324 struct btmtk_hci_wmt_evt_funcc *wmt_evt_funcc;
3325 u32 hlen, status = BTMTK_WMT_INVALID;
3326 struct btmtk_hci_wmt_evt *wmt_evt;
3327 struct btmtk_hci_wmt_cmd wc;
3328 struct btmtk_wmt_hdr *hdr;
3329 int err;
3330
3331 /* Submit control IN URB on demand to process the WMT event */
3332 err = btusb_mtk_submit_wmt_recv_urb(hdev);
3333 if (err < 0)
3334 return err;
3335
3336 /* Send the WMT command and wait until the WMT event returns */
3337 hlen = sizeof(*hdr) + wmt_params->dlen;
3338 if (hlen > 255)
3339 return -EINVAL;
3340
3341 hdr = (struct btmtk_wmt_hdr *)&wc;
3342 hdr->dir = 1;
3343 hdr->op = wmt_params->op;
3344 hdr->dlen = cpu_to_le16(wmt_params->dlen + 1);
3345 hdr->flag = wmt_params->flag;
3346 memcpy(wc.data, wmt_params->data, wmt_params->dlen);
3347
3348 set_bit(BTUSB_TX_WAIT_VND_EVT, &data->flags);
3349
3350 err = __hci_cmd_send(hdev, 0xfc6f, hlen, &wc);
3351
3352 if (err < 0) {
3353 clear_bit(BTUSB_TX_WAIT_VND_EVT, &data->flags);
3354 return err;
3355 }
3356
3357 /* The vendor specific WMT commands are all answered by a vendor
3358 * specific event and will have the Command Status or Command
3359 * Complete as with usual HCI command flow control.
3360 *
3361 * After sending the command, wait for BTUSB_TX_WAIT_VND_EVT
3362 * state to be cleared. The driver specific event receive routine
3363 * will clear that state and with that indicate completion of the
3364 * WMT command.
3365 */
3366 err = wait_on_bit_timeout(&data->flags, BTUSB_TX_WAIT_VND_EVT,
3367 TASK_INTERRUPTIBLE, HCI_INIT_TIMEOUT);
3368 if (err == -EINTR) {
3369 bt_dev_err(hdev, "Execution of wmt command interrupted");
3370 clear_bit(BTUSB_TX_WAIT_VND_EVT, &data->flags);
3371 return err;
3372 }
3373
3374 if (err) {
3375 bt_dev_err(hdev, "Execution of wmt command timed out");
3376 clear_bit(BTUSB_TX_WAIT_VND_EVT, &data->flags);
3377 return -ETIMEDOUT;
3378 }
3379
3380 /* Parse and handle the return WMT event */
3381 wmt_evt = (struct btmtk_hci_wmt_evt *)data->evt_skb->data;
3382 if (wmt_evt->whdr.op != hdr->op) {
3383 bt_dev_err(hdev, "Wrong op received %d expected %d",
3384 wmt_evt->whdr.op, hdr->op);
3385 err = -EIO;
3386 goto err_free_skb;
3387 }
3388
3389 switch (wmt_evt->whdr.op) {
3390 case BTMTK_WMT_SEMAPHORE:
3391 if (wmt_evt->whdr.flag == 2)
3392 status = BTMTK_WMT_PATCH_UNDONE;
3393 else
3394 status = BTMTK_WMT_PATCH_DONE;
3395 break;
3396 case BTMTK_WMT_FUNC_CTRL:
3397 wmt_evt_funcc = (struct btmtk_hci_wmt_evt_funcc *)wmt_evt;
3398 if (be16_to_cpu(wmt_evt_funcc->status) == 0x404)
3399 status = BTMTK_WMT_ON_DONE;
3400 else if (be16_to_cpu(wmt_evt_funcc->status) == 0x420)
3401 status = BTMTK_WMT_ON_PROGRESS;
3402 else
3403 status = BTMTK_WMT_ON_UNDONE;
3404 break;
3405 }
3406
3407 if (wmt_params->status)
3408 *wmt_params->status = status;
3409
3410 err_free_skb:
3411 kfree_skb(data->evt_skb);
3412 data->evt_skb = NULL;
3413
3414 return err;
3415 }
3416
3417 static int btusb_mtk_setup_firmware(struct hci_dev *hdev, const char *fwname)
3418 {
3419 struct btmtk_hci_wmt_params wmt_params;
3420 const struct firmware *fw;
3421 const u8 *fw_ptr;
3422 size_t fw_size;
3423 int err, dlen;
3424 u8 flag, param;
3425
3426 err = request_firmware(&fw, fwname, &hdev->dev);
3427 if (err < 0) {
3428 bt_dev_err(hdev, "Failed to load firmware file (%d)", err);
3429 return err;
3430 }
3431
3432 /* Power on data RAM the firmware relies on. */
3433 param = 1;
3434 wmt_params.op = BTMTK_WMT_FUNC_CTRL;
3435 wmt_params.flag = 3;
3436 wmt_params.dlen = sizeof(param);
3437 wmt_params.data = &param;
3438 wmt_params.status = NULL;
3439
3440 err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
3441 if (err < 0) {
3442 bt_dev_err(hdev, "Failed to power on data RAM (%d)", err);
3443 goto err_release_fw;
3444 }
3445
3446 fw_ptr = fw->data;
3447 fw_size = fw->size;
3448
3449 /* The size of patch header is 30 bytes, should be skip */
3450 if (fw_size < 30) {
3451 err = -EINVAL;
3452 goto err_release_fw;
3453 }
3454
3455 fw_size -= 30;
3456 fw_ptr += 30;
3457 flag = 1;
3458
3459 wmt_params.op = BTMTK_WMT_PATCH_DWNLD;
3460 wmt_params.status = NULL;
3461
3462 while (fw_size > 0) {
3463 dlen = min_t(int, 250, fw_size);
3464
3465 /* Tell deivice the position in sequence */
3466 if (fw_size - dlen <= 0)
3467 flag = 3;
3468 else if (fw_size < fw->size - 30)
3469 flag = 2;
3470
3471 wmt_params.flag = flag;
3472 wmt_params.dlen = dlen;
3473 wmt_params.data = fw_ptr;
3474
3475 err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
3476 if (err < 0) {
3477 bt_dev_err(hdev, "Failed to send wmt patch dwnld (%d)",
3478 err);
3479 goto err_release_fw;
3480 }
3481
3482 fw_size -= dlen;
3483 fw_ptr += dlen;
3484 }
3485
3486 wmt_params.op = BTMTK_WMT_RST;
3487 wmt_params.flag = 4;
3488 wmt_params.dlen = 0;
3489 wmt_params.data = NULL;
3490 wmt_params.status = NULL;
3491
3492 /* Activate funciton the firmware providing to */
3493 err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
3494 if (err < 0) {
3495 bt_dev_err(hdev, "Failed to send wmt rst (%d)", err);
3496 goto err_release_fw;
3497 }
3498
3499 /* Wait a few moments for firmware activation done */
3500 usleep_range(10000, 12000);
3501
3502 err_release_fw:
3503 release_firmware(fw);
3504
3505 return err;
3506 }
3507
3508 static int btusb_mtk_func_query(struct hci_dev *hdev)
3509 {
3510 struct btmtk_hci_wmt_params wmt_params;
3511 int status, err;
3512 u8 param = 0;
3513
3514 /* Query whether the function is enabled */
3515 wmt_params.op = BTMTK_WMT_FUNC_CTRL;
3516 wmt_params.flag = 4;
3517 wmt_params.dlen = sizeof(param);
3518 wmt_params.data = &param;
3519 wmt_params.status = &status;
3520
3521 err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
3522 if (err < 0) {
3523 bt_dev_err(hdev, "Failed to query function status (%d)", err);
3524 return err;
3525 }
3526
3527 return status;
3528 }
3529
3530 static int btusb_mtk_reg_read(struct btusb_data *data, u32 reg, u32 *val)
3531 {
3532 int pipe, err, size = sizeof(u32);
3533 void *buf;
3534
3535 buf = kzalloc(size, GFP_KERNEL);
3536 if (!buf)
3537 return -ENOMEM;
3538
3539 pipe = usb_rcvctrlpipe(data->udev, 0);
3540 err = usb_control_msg(data->udev, pipe, 0x63,
3541 USB_TYPE_VENDOR | USB_DIR_IN,
3542 reg >> 16, reg & 0xffff,
3543 buf, size, USB_CTRL_SET_TIMEOUT);
3544 if (err < 0)
3545 goto err_free_buf;
3546
3547 *val = get_unaligned_le32(buf);
3548
3549 err_free_buf:
3550 kfree(buf);
3551
3552 return err;
3553 }
3554
3555 static int btusb_mtk_id_get(struct btusb_data *data, u32 *id)
3556 {
3557 return btusb_mtk_reg_read(data, 0x80000008, id);
3558 }
3559
3560 static int btusb_mtk_setup(struct hci_dev *hdev)
3561 {
3562 struct btusb_data *data = hci_get_drvdata(hdev);
3563 struct btmtk_hci_wmt_params wmt_params;
3564 ktime_t calltime, delta, rettime;
3565 struct btmtk_tci_sleep tci_sleep;
3566 unsigned long long duration;
3567 struct sk_buff *skb;
3568 const char *fwname;
3569 int err, status;
3570 u32 dev_id;
3571 u8 param;
3572
3573 calltime = ktime_get();
3574
3575 err = btusb_mtk_id_get(data, &dev_id);
3576 if (err < 0) {
3577 bt_dev_err(hdev, "Failed to get device id (%d)", err);
3578 return err;
3579 }
3580
3581 switch (dev_id) {
3582 case 0x7663:
3583 fwname = FIRMWARE_MT7663;
3584 break;
3585 case 0x7668:
3586 fwname = FIRMWARE_MT7668;
3587 break;
3588 default:
3589 bt_dev_err(hdev, "Unsupported support hardware variant (%08x)",
3590 dev_id);
3591 return -ENODEV;
3592 }
3593
3594 /* Query whether the firmware is already download */
3595 wmt_params.op = BTMTK_WMT_SEMAPHORE;
3596 wmt_params.flag = 1;
3597 wmt_params.dlen = 0;
3598 wmt_params.data = NULL;
3599 wmt_params.status = &status;
3600
3601 err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
3602 if (err < 0) {
3603 bt_dev_err(hdev, "Failed to query firmware status (%d)", err);
3604 return err;
3605 }
3606
3607 if (status == BTMTK_WMT_PATCH_DONE) {
3608 bt_dev_info(hdev, "firmware already downloaded");
3609 goto ignore_setup_fw;
3610 }
3611
3612 /* Setup a firmware which the device definitely requires */
3613 err = btusb_mtk_setup_firmware(hdev, fwname);
3614 if (err < 0)
3615 return err;
3616
3617 ignore_setup_fw:
3618 err = readx_poll_timeout(btusb_mtk_func_query, hdev, status,
3619 status < 0 || status != BTMTK_WMT_ON_PROGRESS,
3620 2000, 5000000);
3621 /* -ETIMEDOUT happens */
3622 if (err < 0)
3623 return err;
3624
3625 /* The other errors happen in btusb_mtk_func_query */
3626 if (status < 0)
3627 return status;
3628
3629 if (status == BTMTK_WMT_ON_DONE) {
3630 bt_dev_info(hdev, "function already on");
3631 goto ignore_func_on;
3632 }
3633
3634 /* Enable Bluetooth protocol */
3635 param = 1;
3636 wmt_params.op = BTMTK_WMT_FUNC_CTRL;
3637 wmt_params.flag = 0;
3638 wmt_params.dlen = sizeof(param);
3639 wmt_params.data = &param;
3640 wmt_params.status = NULL;
3641
3642 err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
3643 if (err < 0) {
3644 bt_dev_err(hdev, "Failed to send wmt func ctrl (%d)", err);
3645 return err;
3646 }
3647
3648 ignore_func_on:
3649 /* Apply the low power environment setup */
3650 tci_sleep.mode = 0x5;
3651 tci_sleep.duration = cpu_to_le16(0x640);
3652 tci_sleep.host_duration = cpu_to_le16(0x640);
3653 tci_sleep.host_wakeup_pin = 0;
3654 tci_sleep.time_compensation = 0;
3655
3656 skb = __hci_cmd_sync(hdev, 0xfc7a, sizeof(tci_sleep), &tci_sleep,
3657 HCI_INIT_TIMEOUT);
3658 if (IS_ERR(skb)) {
3659 err = PTR_ERR(skb);
3660 bt_dev_err(hdev, "Failed to apply low power setting (%d)", err);
3661 return err;
3662 }
3663 kfree_skb(skb);
3664
3665 rettime = ktime_get();
3666 delta = ktime_sub(rettime, calltime);
3667 duration = (unsigned long long)ktime_to_ns(delta) >> 10;
3668
3669 bt_dev_info(hdev, "Device setup in %llu usecs", duration);
3670
3671 return 0;
3672 }
3673
3674 static int btusb_mtk_shutdown(struct hci_dev *hdev)
3675 {
3676 struct btmtk_hci_wmt_params wmt_params;
3677 u8 param = 0;
3678 int err;
3679
3680 /* Disable the device */
3681 wmt_params.op = BTMTK_WMT_FUNC_CTRL;
3682 wmt_params.flag = 0;
3683 wmt_params.dlen = sizeof(param);
3684 wmt_params.data = &param;
3685 wmt_params.status = NULL;
3686
3687 err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
3688 if (err < 0) {
3689 bt_dev_err(hdev, "Failed to send wmt func ctrl (%d)", err);
3690 return err;
3691 }
3692
3693 return 0;
3694 }
3695
3696 MODULE_FIRMWARE(FIRMWARE_MT7663);
3697 MODULE_FIRMWARE(FIRMWARE_MT7668);
3698
3699 #ifdef CONFIG_PM
3700 /* Configure an out-of-band gpio as wake-up pin, if specified in device tree */
3701 static int marvell_config_oob_wake(struct hci_dev *hdev)
3702 {
3703 struct sk_buff *skb;
3704 struct btusb_data *data = hci_get_drvdata(hdev);
3705 struct device *dev = &data->udev->dev;
3706 u16 pin, gap, opcode;
3707 int ret;
3708 u8 cmd[5];
3709
3710 /* Move on if no wakeup pin specified */
3711 if (of_property_read_u16(dev->of_node, "marvell,wakeup-pin", &pin) ||
3712 of_property_read_u16(dev->of_node, "marvell,wakeup-gap-ms", &gap))
3713 return 0;
3714
3715 /* Vendor specific command to configure a GPIO as wake-up pin */
3716 opcode = hci_opcode_pack(0x3F, 0x59);
3717 cmd[0] = opcode & 0xFF;
3718 cmd[1] = opcode >> 8;
3719 cmd[2] = 2; /* length of parameters that follow */
3720 cmd[3] = pin;
3721 cmd[4] = gap; /* time in ms, for which wakeup pin should be asserted */
3722
3723 skb = bt_skb_alloc(sizeof(cmd), GFP_KERNEL);
3724 if (!skb) {
3725 bt_dev_err(hdev, "%s: No memory\n", __func__);
3726 return -ENOMEM;
3727 }
3728
3729 skb_put_data(skb, cmd, sizeof(cmd));
3730 hci_skb_pkt_type(skb) = HCI_COMMAND_PKT;
3731
3732 ret = btusb_send_frame(hdev, skb);
3733 if (ret) {
3734 bt_dev_err(hdev, "%s: configuration failed\n", __func__);
3735 kfree_skb(skb);
3736 return ret;
3737 }
3738
3739 return 0;
3740 }
3741 #endif
3742
3743 static int btusb_set_bdaddr_marvell(struct hci_dev *hdev,
3744 const bdaddr_t *bdaddr)
3745 {
3746 struct sk_buff *skb;
3747 u8 buf[8];
3748 long ret;
3749
3750 buf[0] = 0xfe;
3751 buf[1] = sizeof(bdaddr_t);
3752 memcpy(buf + 2, bdaddr, sizeof(bdaddr_t));
3753
3754 skb = __hci_cmd_sync(hdev, 0xfc22, sizeof(buf), buf, HCI_INIT_TIMEOUT);
3755 if (IS_ERR(skb)) {
3756 ret = PTR_ERR(skb);
3757 bt_dev_err(hdev, "changing Marvell device address failed (%ld)",
3758 ret);
3759 return ret;
3760 }
3761 kfree_skb(skb);
3762
3763 return 0;
3764 }
3765
3766 #define BTUSB_EDGE_LED_COMMAND 0xfc77
3767
3768 static void btusb_edge_set_led(struct hci_dev *hdev, bool state)
3769 {
3770 struct sk_buff *skb;
3771 u8 config_led[] = { 0x09, 0x00, 0x01, 0x01 };
3772
3773 if (state)
3774 config_led[1] = 0x01;
3775
3776 skb = __hci_cmd_sync(hdev, BTUSB_EDGE_LED_COMMAND, sizeof(config_led), config_led, HCI_INIT_TIMEOUT);
3777 if (IS_ERR(skb))
3778 BT_ERR("%s fail to set LED (%ld)", hdev->name, PTR_ERR(skb));
3779 else
3780 kfree_skb(skb);
3781 }
3782
3783 static int btusb_edge_post_init(struct hci_dev *hdev)
3784 {
3785 btusb_edge_set_led(hdev, true);
3786 return 0;
3787 }
3788
3789 static int btusb_edge_shutdown(struct hci_dev *hdev)
3790 {
3791 btusb_edge_set_led(hdev, false);
3792 return 0;
3793 }
3794
3795 static int btusb_set_bdaddr_ath3012(struct hci_dev *hdev,
3796 const bdaddr_t *bdaddr)
3797 {
3798 struct sk_buff *skb;
3799 u8 buf[10];
3800 long ret;
3801
3802 buf[0] = 0x01;
3803 buf[1] = 0x01;
3804 buf[2] = 0x00;
3805 buf[3] = sizeof(bdaddr_t);
3806 memcpy(buf + 4, bdaddr, sizeof(bdaddr_t));
3807
3808 skb = __hci_cmd_sync(hdev, 0xfc0b, sizeof(buf), buf, HCI_INIT_TIMEOUT);
3809 if (IS_ERR(skb)) {
3810 ret = PTR_ERR(skb);
3811 bt_dev_err(hdev, "Change address command failed (%ld)", ret);
3812 return ret;
3813 }
3814 kfree_skb(skb);
3815
3816 return 0;
3817 }
3818
3819 static int btusb_set_bdaddr_wcn6855(struct hci_dev *hdev,
3820 const bdaddr_t *bdaddr)
3821 {
3822 struct sk_buff *skb;
3823 u8 buf[6];
3824 long ret;
3825
3826 memcpy(buf, bdaddr, sizeof(bdaddr_t));
3827
3828 skb = __hci_cmd_sync_ev(hdev, 0xfc14, sizeof(buf), buf,
3829 HCI_EV_CMD_COMPLETE, HCI_INIT_TIMEOUT);
3830 if (IS_ERR(skb)) {
3831 ret = PTR_ERR(skb);
3832 bt_dev_err(hdev, "Change address command failed (%ld)", ret);
3833 return ret;
3834 }
3835 kfree_skb(skb);
3836
3837 return 0;
3838 }
3839
3840 #define QCA_DFU_PACKET_LEN 4096
3841
3842 #define QCA_GET_TARGET_VERSION 0x09
3843 #define QCA_CHECK_STATUS 0x05
3844 #define QCA_DFU_DOWNLOAD 0x01
3845
3846 #define QCA_SYSCFG_UPDATED 0x40
3847 #define QCA_PATCH_UPDATED 0x80
3848 #define QCA_DFU_TIMEOUT 3000
3849 #define QCA_FLAG_MULTI_NVM 0x80
3850
3851 struct qca_version {
3852 __le32 rom_version;
3853 __le32 patch_version;
3854 __le32 ram_version;
3855 __le16 board_id;
3856 __le16 flag;
3857 __u8 reserved[4];
3858 } __packed;
3859
3860 struct qca_rampatch_version {
3861 __le16 rom_version_high;
3862 __le16 rom_version_low;
3863 __le16 patch_version;
3864 } __packed;
3865
3866 struct qca_device_info {
3867 u32 rom_version;
3868 u8 rampatch_hdr; /* length of header in rampatch */
3869 u8 nvm_hdr; /* length of header in NVM */
3870 u8 ver_offset; /* offset of version structure in rampatch */
3871 };
3872
3873 static const struct qca_device_info qca_devices_table[] = {
3874 { 0x00000100, 20, 4, 8 }, /* Rome 1.0 */
3875 { 0x00000101, 20, 4, 8 }, /* Rome 1.1 */
3876 { 0x00000200, 28, 4, 16 }, /* Rome 2.0 */
3877 { 0x00000201, 28, 4, 16 }, /* Rome 2.1 */
3878 { 0x00000300, 28, 4, 16 }, /* Rome 3.0 */
3879 { 0x00000302, 28, 4, 16 }, /* Rome 3.2 */
3880 { 0x00130100, 40, 4, 16 }, /* WCN6855 1.0 */
3881 { 0x00130200, 40, 4, 16 }, /* WCN6855 2.0 */
3882 };
3883
3884 static int btusb_qca_send_vendor_req(struct usb_device *udev, u8 request,
3885 void *data, u16 size)
3886 {
3887 int pipe, err;
3888 u8 *buf;
3889
3890 buf = kmalloc(size, GFP_KERNEL);
3891 if (!buf)
3892 return -ENOMEM;
3893
3894 /* Found some of USB hosts have IOT issues with ours so that we should
3895 * not wait until HCI layer is ready.
3896 */
3897 pipe = usb_rcvctrlpipe(udev, 0);
3898 err = usb_control_msg(udev, pipe, request, USB_TYPE_VENDOR | USB_DIR_IN,
3899 0, 0, buf, size, USB_CTRL_SET_TIMEOUT);
3900 if (err < 0) {
3901 dev_err(&udev->dev, "Failed to access otp area (%d)", err);
3902 goto done;
3903 }
3904
3905 memcpy(data, buf, size);
3906
3907 done:
3908 kfree(buf);
3909
3910 return err;
3911 }
3912
3913 static int btusb_setup_qca_download_fw(struct hci_dev *hdev,
3914 const struct firmware *firmware,
3915 size_t hdr_size)
3916 {
3917 struct btusb_data *btdata = hci_get_drvdata(hdev);
3918 struct usb_device *udev = btdata->udev;
3919 size_t count, size, sent = 0;
3920 int pipe, len, err;
3921 u8 *buf;
3922
3923 buf = kmalloc(QCA_DFU_PACKET_LEN, GFP_KERNEL);
3924 if (!buf)
3925 return -ENOMEM;
3926
3927 count = firmware->size;
3928
3929 size = min_t(size_t, count, hdr_size);
3930 memcpy(buf, firmware->data, size);
3931
3932 /* USB patches should go down to controller through USB path
3933 * because binary format fits to go down through USB channel.
3934 * USB control path is for patching headers and USB bulk is for
3935 * patch body.
3936 */
3937 pipe = usb_sndctrlpipe(udev, 0);
3938 err = usb_control_msg(udev, pipe, QCA_DFU_DOWNLOAD, USB_TYPE_VENDOR,
3939 0, 0, buf, size, USB_CTRL_SET_TIMEOUT);
3940 if (err < 0) {
3941 bt_dev_err(hdev, "Failed to send headers (%d)", err);
3942 goto done;
3943 }
3944
3945 sent += size;
3946 count -= size;
3947
3948 while (count) {
3949 size = min_t(size_t, count, QCA_DFU_PACKET_LEN);
3950
3951 memcpy(buf, firmware->data + sent, size);
3952
3953 pipe = usb_sndbulkpipe(udev, 0x02);
3954 err = usb_bulk_msg(udev, pipe, buf, size, &len,
3955 QCA_DFU_TIMEOUT);
3956 if (err < 0) {
3957 bt_dev_err(hdev, "Failed to send body at %zd of %zd (%d)",
3958 sent, firmware->size, err);
3959 break;
3960 }
3961
3962 if (size != len) {
3963 bt_dev_err(hdev, "Failed to get bulk buffer");
3964 err = -EILSEQ;
3965 break;
3966 }
3967
3968 sent += size;
3969 count -= size;
3970 }
3971
3972 done:
3973 kfree(buf);
3974 return err;
3975 }
3976
3977 static int btusb_setup_qca_load_rampatch(struct hci_dev *hdev,
3978 struct qca_version *ver,
3979 const struct qca_device_info *info)
3980 {
3981 struct qca_rampatch_version *rver;
3982 const struct firmware *fw;
3983 u32 ver_rom, ver_patch, rver_rom;
3984 u16 rver_rom_low, rver_rom_high, rver_patch;
3985 char fwname[64];
3986 int err;
3987
3988 ver_rom = le32_to_cpu(ver->rom_version);
3989 ver_patch = le32_to_cpu(ver->patch_version);
3990
3991 snprintf(fwname, sizeof(fwname), "qca/rampatch_usb_%08x.bin", ver_rom);
3992
3993 err = request_firmware(&fw, fwname, &hdev->dev);
3994 if (err) {
3995 bt_dev_err(hdev, "failed to request rampatch file: %s (%d)",
3996 fwname, err);
3997 return err;
3998 }
3999
4000 bt_dev_info(hdev, "using rampatch file: %s", fwname);
4001
4002 rver = (struct qca_rampatch_version *)(fw->data + info->ver_offset);
4003 rver_rom_low = le16_to_cpu(rver->rom_version_low);
4004 rver_patch = le16_to_cpu(rver->patch_version);
4005
4006 if (ver_rom & ~0xffffU) {
4007 rver_rom_high = le16_to_cpu(rver->rom_version_high);
4008 rver_rom = le32_to_cpu(rver_rom_high << 16 | rver_rom_low);
4009 } else {
4010 rver_rom = rver_rom_low;
4011 }
4012
4013 bt_dev_info(hdev, "QCA: patch rome 0x%x build 0x%x, "
4014 "firmware rome 0x%x build 0x%x",
4015 rver_rom, rver_patch, ver_rom, ver_patch);
4016
4017 if (rver_rom != ver_rom || rver_patch <= ver_patch) {
4018 bt_dev_err(hdev, "rampatch file version did not match with firmware");
4019 err = -EINVAL;
4020 goto done;
4021 }
4022
4023 err = btusb_setup_qca_download_fw(hdev, fw, info->rampatch_hdr);
4024
4025 done:
4026 release_firmware(fw);
4027
4028 return err;
4029 }
4030
4031 static int btusb_setup_qca_load_nvm(struct hci_dev *hdev,
4032 struct qca_version *ver,
4033 const struct qca_device_info *info)
4034 {
4035 const struct firmware *fw;
4036 char fwname[64];
4037 int err;
4038
4039 if (((ver->flag >> 8) & 0xff) == QCA_FLAG_MULTI_NVM) {
4040 snprintf(fwname, sizeof(fwname), "qca/nvm_usb_%08x_%04x.bin",
4041 le32_to_cpu(ver->rom_version),
4042 le16_to_cpu(ver->board_id));
4043 } else {
4044 snprintf(fwname, sizeof(fwname), "qca/nvm_usb_%08x.bin",
4045 le32_to_cpu(ver->rom_version));
4046 }
4047
4048 err = request_firmware(&fw, fwname, &hdev->dev);
4049 if (err) {
4050 bt_dev_err(hdev, "failed to request NVM file: %s (%d)",
4051 fwname, err);
4052 return err;
4053 }
4054
4055 bt_dev_info(hdev, "using NVM file: %s", fwname);
4056
4057 err = btusb_setup_qca_download_fw(hdev, fw, info->nvm_hdr);
4058
4059 release_firmware(fw);
4060
4061 return err;
4062 }
4063
4064 /* identify the ROM version and check whether patches are needed */
4065 static bool btusb_qca_need_patch(struct usb_device *udev)
4066 {
4067 struct qca_version ver;
4068
4069 if (btusb_qca_send_vendor_req(udev, QCA_GET_TARGET_VERSION, &ver,
4070 sizeof(ver)) < 0)
4071 return false;
4072 /* only low ROM versions need patches */
4073 return !(le32_to_cpu(ver.rom_version) & ~0xffffU);
4074 }
4075
4076 static int btusb_setup_qca(struct hci_dev *hdev)
4077 {
4078 struct btusb_data *btdata = hci_get_drvdata(hdev);
4079 struct usb_device *udev = btdata->udev;
4080 const struct qca_device_info *info = NULL;
4081 struct qca_version ver;
4082 u32 ver_rom;
4083 u8 status;
4084 int i, err;
4085
4086 err = btusb_qca_send_vendor_req(udev, QCA_GET_TARGET_VERSION, &ver,
4087 sizeof(ver));
4088 if (err < 0)
4089 return err;
4090
4091 ver_rom = le32_to_cpu(ver.rom_version);
4092
4093 for (i = 0; i < ARRAY_SIZE(qca_devices_table); i++) {
4094 if (ver_rom == qca_devices_table[i].rom_version)
4095 info = &qca_devices_table[i];
4096 }
4097 if (!info) {
4098 /* If the rom_version is not matched in the qca_devices_table
4099 * and the high ROM version is not zero, we assume this chip no
4100 * need to load the rampatch and nvm.
4101 */
4102 if (ver_rom & ~0xffffU)
4103 return 0;
4104
4105 bt_dev_err(hdev, "don't support firmware rome 0x%x", ver_rom);
4106 return -ENODEV;
4107 }
4108
4109 err = btusb_qca_send_vendor_req(udev, QCA_CHECK_STATUS, &status,
4110 sizeof(status));
4111 if (err < 0)
4112 return err;
4113
4114 if (!(status & QCA_PATCH_UPDATED)) {
4115 err = btusb_setup_qca_load_rampatch(hdev, &ver, info);
4116 if (err < 0)
4117 return err;
4118 }
4119
4120 err = btusb_qca_send_vendor_req(udev, QCA_GET_TARGET_VERSION, &ver,
4121 sizeof(ver));
4122 if (err < 0)
4123 return err;
4124
4125 if (!(status & QCA_SYSCFG_UPDATED)) {
4126 err = btusb_setup_qca_load_nvm(hdev, &ver, info);
4127 if (err < 0)
4128 return err;
4129 }
4130
4131 return 0;
4132 }
4133
4134 static inline int __set_diag_interface(struct hci_dev *hdev)
4135 {
4136 struct btusb_data *data = hci_get_drvdata(hdev);
4137 struct usb_interface *intf = data->diag;
4138 int i;
4139
4140 if (!data->diag)
4141 return -ENODEV;
4142
4143 data->diag_tx_ep = NULL;
4144 data->diag_rx_ep = NULL;
4145
4146 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
4147 struct usb_endpoint_descriptor *ep_desc;
4148
4149 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
4150
4151 if (!data->diag_tx_ep && usb_endpoint_is_bulk_out(ep_desc)) {
4152 data->diag_tx_ep = ep_desc;
4153 continue;
4154 }
4155
4156 if (!data->diag_rx_ep && usb_endpoint_is_bulk_in(ep_desc)) {
4157 data->diag_rx_ep = ep_desc;
4158 continue;
4159 }
4160 }
4161
4162 if (!data->diag_tx_ep || !data->diag_rx_ep) {
4163 bt_dev_err(hdev, "invalid diagnostic descriptors");
4164 return -ENODEV;
4165 }
4166
4167 return 0;
4168 }
4169
4170 static struct urb *alloc_diag_urb(struct hci_dev *hdev, bool enable)
4171 {
4172 struct btusb_data *data = hci_get_drvdata(hdev);
4173 struct sk_buff *skb;
4174 struct urb *urb;
4175 unsigned int pipe;
4176
4177 if (!data->diag_tx_ep)
4178 return ERR_PTR(-ENODEV);
4179
4180 urb = usb_alloc_urb(0, GFP_KERNEL);
4181 if (!urb)
4182 return ERR_PTR(-ENOMEM);
4183
4184 skb = bt_skb_alloc(2, GFP_KERNEL);
4185 if (!skb) {
4186 usb_free_urb(urb);
4187 return ERR_PTR(-ENOMEM);
4188 }
4189
4190 skb_put_u8(skb, 0xf0);
4191 skb_put_u8(skb, enable);
4192
4193 pipe = usb_sndbulkpipe(data->udev, data->diag_tx_ep->bEndpointAddress);
4194
4195 usb_fill_bulk_urb(urb, data->udev, pipe,
4196 skb->data, skb->len, btusb_tx_complete, skb);
4197
4198 skb->dev = (void *)hdev;
4199
4200 return urb;
4201 }
4202
4203 static int btusb_bcm_set_diag(struct hci_dev *hdev, bool enable)
4204 {
4205 struct btusb_data *data = hci_get_drvdata(hdev);
4206 struct urb *urb;
4207
4208 if (!data->diag)
4209 return -ENODEV;
4210
4211 if (!test_bit(HCI_RUNNING, &hdev->flags))
4212 return -ENETDOWN;
4213
4214 urb = alloc_diag_urb(hdev, enable);
4215 if (IS_ERR(urb))
4216 return PTR_ERR(urb);
4217
4218 return submit_or_queue_tx_urb(hdev, urb);
4219 }
4220
4221 #ifdef CONFIG_PM
4222 static irqreturn_t btusb_oob_wake_handler(int irq, void *priv)
4223 {
4224 struct btusb_data *data = priv;
4225
4226 pm_wakeup_event(&data->udev->dev, 0);
4227 pm_system_wakeup();
4228
4229 /* Disable only if not already disabled (keep it balanced) */
4230 if (test_and_clear_bit(BTUSB_OOB_WAKE_ENABLED, &data->flags)) {
4231 disable_irq_nosync(irq);
4232 disable_irq_wake(irq);
4233 }
4234 return IRQ_HANDLED;
4235 }
4236
4237 static const struct of_device_id btusb_match_table[] = {
4238 { .compatible = "usb1286,204e" },
4239 { .compatible = "usbcf3,e300" }, /* QCA6174A */
4240 { .compatible = "usb4ca,301a" }, /* QCA6174A (Lite-On) */
4241 { }
4242 };
4243 MODULE_DEVICE_TABLE(of, btusb_match_table);
4244
4245 /* Use an oob wakeup pin? */
4246 static int btusb_config_oob_wake(struct hci_dev *hdev)
4247 {
4248 struct btusb_data *data = hci_get_drvdata(hdev);
4249 struct device *dev = &data->udev->dev;
4250 int irq, ret;
4251
4252 clear_bit(BTUSB_OOB_WAKE_ENABLED, &data->flags);
4253
4254 if (!of_match_device(btusb_match_table, dev))
4255 return 0;
4256
4257 /* Move on if no IRQ specified */
4258 irq = of_irq_get_byname(dev->of_node, "wakeup");
4259 if (irq <= 0) {
4260 bt_dev_dbg(hdev, "%s: no OOB Wakeup IRQ in DT", __func__);
4261 return 0;
4262 }
4263
4264 irq_set_status_flags(irq, IRQ_NOAUTOEN);
4265 ret = devm_request_irq(&hdev->dev, irq, btusb_oob_wake_handler,
4266 0, "OOB Wake-on-BT", data);
4267 if (ret) {
4268 bt_dev_err(hdev, "%s: IRQ request failed", __func__);
4269 return ret;
4270 }
4271
4272 ret = device_init_wakeup(dev, true);
4273 if (ret) {
4274 bt_dev_err(hdev, "%s: failed to init_wakeup", __func__);
4275 return ret;
4276 }
4277
4278 data->oob_wake_irq = irq;
4279 bt_dev_info(hdev, "OOB Wake-on-BT configured at IRQ %u", irq);
4280 return 0;
4281 }
4282 #endif
4283
4284 static void btusb_check_needs_reset_resume(struct usb_interface *intf)
4285 {
4286 if (dmi_check_system(btusb_needs_reset_resume_table))
4287 interface_to_usbdev(intf)->quirks |= USB_QUIRK_RESET_RESUME;
4288 }
4289
4290 static bool btusb_prevent_wake(struct hci_dev *hdev)
4291 {
4292 struct btusb_data *data = hci_get_drvdata(hdev);
4293
4294 if (test_bit(BTUSB_WAKEUP_DISABLE, &data->flags))
4295 return true;
4296
4297 return !device_may_wakeup(&data->udev->dev);
4298 }
4299
4300 static int btusb_probe(struct usb_interface *intf,
4301 const struct usb_device_id *id)
4302 {
4303 struct usb_endpoint_descriptor *ep_desc;
4304 struct gpio_desc *reset_gpio;
4305 struct btusb_data *data;
4306 struct hci_dev *hdev;
4307 unsigned ifnum_base;
4308 int i, err;
4309
4310 BT_DBG("intf %p id %p", intf, id);
4311
4312 /* interface numbers are hardcoded in the spec */
4313 if (intf->cur_altsetting->desc.bInterfaceNumber != 0) {
4314 if (!(id->driver_info & BTUSB_IFNUM_2))
4315 return -ENODEV;
4316 if (intf->cur_altsetting->desc.bInterfaceNumber != 2)
4317 return -ENODEV;
4318 }
4319
4320 ifnum_base = intf->cur_altsetting->desc.bInterfaceNumber;
4321
4322 if (!id->driver_info) {
4323 const struct usb_device_id *match;
4324
4325 match = usb_match_id(intf, blacklist_table);
4326 if (match)
4327 id = match;
4328 }
4329
4330 if (id->driver_info == BTUSB_IGNORE)
4331 return -ENODEV;
4332
4333 if (id->driver_info & BTUSB_ATH3012) {
4334 struct usb_device *udev = interface_to_usbdev(intf);
4335
4336 /* Old firmware would otherwise let ath3k driver load
4337 * patch and sysconfig files
4338 */
4339 if (le16_to_cpu(udev->descriptor.bcdDevice) <= 0x0001 &&
4340 !btusb_qca_need_patch(udev))
4341 return -ENODEV;
4342 }
4343
4344 data = devm_kzalloc(&intf->dev, sizeof(*data), GFP_KERNEL);
4345 if (!data)
4346 return -ENOMEM;
4347
4348 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
4349 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
4350
4351 if (!data->intr_ep && usb_endpoint_is_int_in(ep_desc)) {
4352 data->intr_ep = ep_desc;
4353 continue;
4354 }
4355
4356 if (!data->bulk_tx_ep && usb_endpoint_is_bulk_out(ep_desc)) {
4357 data->bulk_tx_ep = ep_desc;
4358 continue;
4359 }
4360
4361 if (!data->bulk_rx_ep && usb_endpoint_is_bulk_in(ep_desc)) {
4362 data->bulk_rx_ep = ep_desc;
4363 continue;
4364 }
4365 }
4366
4367 if (!data->intr_ep || !data->bulk_tx_ep || !data->bulk_rx_ep)
4368 return -ENODEV;
4369
4370 if (id->driver_info & BTUSB_AMP) {
4371 data->cmdreq_type = USB_TYPE_CLASS | 0x01;
4372 data->cmdreq = 0x2b;
4373 } else {
4374 data->cmdreq_type = USB_TYPE_CLASS;
4375 data->cmdreq = 0x00;
4376 }
4377
4378 data->udev = interface_to_usbdev(intf);
4379 data->intf = intf;
4380
4381 INIT_WORK(&data->work, btusb_work);
4382 INIT_WORK(&data->waker, btusb_waker);
4383 init_usb_anchor(&data->deferred);
4384 init_usb_anchor(&data->tx_anchor);
4385 spin_lock_init(&data->txlock);
4386
4387 init_usb_anchor(&data->intr_anchor);
4388 init_usb_anchor(&data->bulk_anchor);
4389 init_usb_anchor(&data->isoc_anchor);
4390 init_usb_anchor(&data->diag_anchor);
4391 init_usb_anchor(&data->ctrl_anchor);
4392 spin_lock_init(&data->rxlock);
4393
4394 if (id->driver_info & BTUSB_INTEL_NEW) {
4395 data->recv_event = btusb_recv_event_intel;
4396 data->recv_bulk = btusb_recv_bulk_intel;
4397 set_bit(BTUSB_BOOTLOADER, &data->flags);
4398 } else {
4399 data->recv_event = hci_recv_frame;
4400 data->recv_bulk = btusb_recv_bulk;
4401 }
4402
4403 hdev = hci_alloc_dev();
4404 if (!hdev)
4405 return -ENOMEM;
4406
4407 hdev->bus = HCI_USB;
4408 hci_set_drvdata(hdev, data);
4409
4410 if (id->driver_info & BTUSB_AMP)
4411 hdev->dev_type = HCI_AMP;
4412 else
4413 hdev->dev_type = HCI_PRIMARY;
4414
4415 data->hdev = hdev;
4416
4417 SET_HCIDEV_DEV(hdev, &intf->dev);
4418
4419 reset_gpio = gpiod_get_optional(&data->udev->dev, "reset",
4420 GPIOD_OUT_LOW);
4421 if (IS_ERR(reset_gpio)) {
4422 err = PTR_ERR(reset_gpio);
4423 goto out_free_dev;
4424 } else if (reset_gpio) {
4425 data->reset_gpio = reset_gpio;
4426 }
4427
4428 hdev->open = btusb_open;
4429 hdev->close = btusb_close;
4430 hdev->flush = btusb_flush;
4431 hdev->send = btusb_send_frame;
4432 hdev->notify = btusb_notify;
4433 hdev->prevent_wake = btusb_prevent_wake;
4434
4435 #ifdef CONFIG_PM
4436 err = btusb_config_oob_wake(hdev);
4437 if (err)
4438 goto out_free_dev;
4439
4440 /* Marvell devices may need a specific chip configuration */
4441 if (id->driver_info & BTUSB_MARVELL && data->oob_wake_irq) {
4442 err = marvell_config_oob_wake(hdev);
4443 if (err)
4444 goto out_free_dev;
4445 }
4446 #endif
4447 if (id->driver_info & BTUSB_CW6622)
4448 set_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks);
4449
4450 if (id->driver_info & BTUSB_BCM2045)
4451 set_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks);
4452
4453 if (id->driver_info & BTUSB_BCM92035)
4454 hdev->setup = btusb_setup_bcm92035;
4455
4456 if (IS_ENABLED(CONFIG_BT_HCIBTUSB_BCM) &&
4457 (id->driver_info & BTUSB_BCM_PATCHRAM)) {
4458 hdev->manufacturer = 15;
4459 hdev->setup = btbcm_setup_patchram;
4460 hdev->set_diag = btusb_bcm_set_diag;
4461 hdev->set_bdaddr = btbcm_set_bdaddr;
4462
4463 /* Broadcom LM_DIAG Interface numbers are hardcoded */
4464 data->diag = usb_ifnum_to_if(data->udev, ifnum_base + 2);
4465 }
4466
4467 if (IS_ENABLED(CONFIG_BT_HCIBTUSB_BCM) &&
4468 (id->driver_info & BTUSB_BCM_APPLE)) {
4469 hdev->manufacturer = 15;
4470 hdev->setup = btbcm_setup_apple;
4471 hdev->set_diag = btusb_bcm_set_diag;
4472
4473 /* Broadcom LM_DIAG Interface numbers are hardcoded */
4474 data->diag = usb_ifnum_to_if(data->udev, ifnum_base + 2);
4475 }
4476
4477 if (id->driver_info & BTUSB_INTEL) {
4478 hdev->manufacturer = 2;
4479 hdev->setup = btusb_setup_intel;
4480 hdev->shutdown = btusb_shutdown_intel;
4481 hdev->set_diag = btintel_set_diag_mfg;
4482 hdev->set_bdaddr = btintel_set_bdaddr;
4483 hdev->cmd_timeout = btusb_intel_cmd_timeout;
4484 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
4485 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
4486 set_bit(HCI_QUIRK_NON_PERSISTENT_DIAG, &hdev->quirks);
4487 }
4488
4489 if (id->driver_info & BTUSB_INTEL_NEW) {
4490 hdev->manufacturer = 2;
4491 hdev->send = btusb_send_frame_intel;
4492 hdev->setup = btusb_setup_intel_new;
4493 hdev->shutdown = btusb_shutdown_intel_new;
4494 hdev->hw_error = btintel_hw_error;
4495 hdev->set_diag = btintel_set_diag;
4496 hdev->set_bdaddr = btintel_set_bdaddr;
4497 hdev->cmd_timeout = btusb_intel_cmd_timeout;
4498 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
4499 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
4500 set_bit(HCI_QUIRK_NON_PERSISTENT_DIAG, &hdev->quirks);
4501 }
4502
4503 if (id->driver_info & BTUSB_INTEL_NEWGEN) {
4504 hdev->manufacturer = 2;
4505 hdev->send = btusb_send_frame_intel;
4506 hdev->setup = btusb_setup_intel_newgen;
4507 hdev->shutdown = btusb_shutdown_intel_new;
4508 hdev->hw_error = btintel_hw_error;
4509 hdev->set_diag = btintel_set_diag;
4510 hdev->set_bdaddr = btintel_set_bdaddr;
4511 hdev->cmd_timeout = btusb_intel_cmd_timeout;
4512 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
4513 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
4514 set_bit(HCI_QUIRK_NON_PERSISTENT_DIAG, &hdev->quirks);
4515
4516 data->recv_event = btusb_recv_event_intel;
4517 data->recv_bulk = btusb_recv_bulk_intel;
4518 set_bit(BTUSB_BOOTLOADER, &data->flags);
4519 }
4520
4521 if (id->driver_info & BTUSB_MARVELL) {
4522 struct pci_dev *pdev;
4523 hdev->set_bdaddr = btusb_set_bdaddr_marvell;
4524 pdev = pci_get_subsys(PCI_ANY_ID, PCI_ANY_ID, 0x1028, 0x0720, NULL);
4525 if (!pdev)
4526 pdev = pci_get_subsys(PCI_ANY_ID, PCI_ANY_ID, 0x1028, 0x0733, NULL);
4527 if (pdev) {
4528 pci_dev_put(pdev);
4529 hdev->post_init = btusb_edge_post_init;
4530 hdev->shutdown = btusb_edge_shutdown;
4531 }
4532 }
4533
4534 if (IS_ENABLED(CONFIG_BT_HCIBTUSB_MTK) &&
4535 (id->driver_info & BTUSB_MEDIATEK)) {
4536 hdev->setup = btusb_mtk_setup;
4537 hdev->shutdown = btusb_mtk_shutdown;
4538 hdev->manufacturer = 70;
4539 set_bit(HCI_QUIRK_NON_PERSISTENT_SETUP, &hdev->quirks);
4540 }
4541
4542 if (id->driver_info & BTUSB_SWAVE) {
4543 set_bit(HCI_QUIRK_FIXUP_INQUIRY_MODE, &hdev->quirks);
4544 set_bit(HCI_QUIRK_BROKEN_LOCAL_COMMANDS, &hdev->quirks);
4545 }
4546
4547 if (id->driver_info & BTUSB_INTEL_BOOT) {
4548 hdev->manufacturer = 2;
4549 set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
4550 }
4551
4552 if (id->driver_info & BTUSB_ATH3012) {
4553 data->setup_on_usb = btusb_setup_qca;
4554 hdev->set_bdaddr = btusb_set_bdaddr_ath3012;
4555 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
4556 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
4557 }
4558
4559 if (id->driver_info & BTUSB_QCA_ROME) {
4560 data->setup_on_usb = btusb_setup_qca;
4561 hdev->set_bdaddr = btusb_set_bdaddr_ath3012;
4562 hdev->cmd_timeout = btusb_qca_cmd_timeout;
4563 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
4564 btusb_check_needs_reset_resume(intf);
4565 }
4566
4567 if (id->driver_info & BTUSB_QCA_WCN6855) {
4568 data->setup_on_usb = btusb_setup_qca;
4569 hdev->set_bdaddr = btusb_set_bdaddr_wcn6855;
4570 hdev->cmd_timeout = btusb_qca_cmd_timeout;
4571 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
4572 }
4573
4574 if (id->driver_info & BTUSB_AMP) {
4575 /* AMP controllers do not support SCO packets */
4576 data->isoc = NULL;
4577 } else {
4578 /* Interface orders are hardcoded in the specification */
4579 data->isoc = usb_ifnum_to_if(data->udev, ifnum_base + 1);
4580 data->isoc_ifnum = ifnum_base + 1;
4581 }
4582
4583 if (IS_ENABLED(CONFIG_BT_HCIBTUSB_RTL) &&
4584 (id->driver_info & BTUSB_REALTEK)) {
4585 hdev->setup = btrtl_setup_realtek;
4586 hdev->shutdown = btrtl_shutdown_realtek;
4587 hdev->cmd_timeout = btusb_rtl_cmd_timeout;
4588
4589 /* Realtek devices lose their updated firmware over global
4590 * suspend that means host doesn't send SET_FEATURE
4591 * (DEVICE_REMOTE_WAKEUP)
4592 */
4593 set_bit(BTUSB_WAKEUP_DISABLE, &data->flags);
4594 }
4595
4596 if (!reset)
4597 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
4598
4599 if (force_scofix || id->driver_info & BTUSB_WRONG_SCO_MTU) {
4600 if (!disable_scofix)
4601 set_bit(HCI_QUIRK_FIXUP_BUFFER_SIZE, &hdev->quirks);
4602 }
4603
4604 if (id->driver_info & BTUSB_BROKEN_ISOC)
4605 data->isoc = NULL;
4606
4607 if (id->driver_info & BTUSB_WIDEBAND_SPEECH)
4608 set_bit(HCI_QUIRK_WIDEBAND_SPEECH_SUPPORTED, &hdev->quirks);
4609
4610 if (id->driver_info & BTUSB_VALID_LE_STATES)
4611 set_bit(HCI_QUIRK_VALID_LE_STATES, &hdev->quirks);
4612
4613 if (id->driver_info & BTUSB_DIGIANSWER) {
4614 data->cmdreq_type = USB_TYPE_VENDOR;
4615 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
4616 }
4617
4618 if (id->driver_info & BTUSB_CSR) {
4619 struct usb_device *udev = data->udev;
4620 u16 bcdDevice = le16_to_cpu(udev->descriptor.bcdDevice);
4621
4622 /* Old firmware would otherwise execute USB reset */
4623 if (bcdDevice < 0x117)
4624 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
4625
4626 /* This must be set first in case we disable it for fakes */
4627 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
4628
4629 /* Fake CSR devices with broken commands */
4630 if (le16_to_cpu(udev->descriptor.idVendor) == 0x0a12 &&
4631 le16_to_cpu(udev->descriptor.idProduct) == 0x0001)
4632 hdev->setup = btusb_setup_csr;
4633 }
4634
4635 if (id->driver_info & BTUSB_SNIFFER) {
4636 struct usb_device *udev = data->udev;
4637
4638 /* New sniffer firmware has crippled HCI interface */
4639 if (le16_to_cpu(udev->descriptor.bcdDevice) > 0x997)
4640 set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
4641 }
4642
4643 if (id->driver_info & BTUSB_INTEL_BOOT) {
4644 /* A bug in the bootloader causes that interrupt interface is
4645 * only enabled after receiving SetInterface(0, AltSetting=0).
4646 */
4647 err = usb_set_interface(data->udev, 0, 0);
4648 if (err < 0) {
4649 BT_ERR("failed to set interface 0, alt 0 %d", err);
4650 goto out_free_dev;
4651 }
4652 }
4653
4654 if (data->isoc) {
4655 err = usb_driver_claim_interface(&btusb_driver,
4656 data->isoc, data);
4657 if (err < 0)
4658 goto out_free_dev;
4659 }
4660
4661 if (IS_ENABLED(CONFIG_BT_HCIBTUSB_BCM) && data->diag) {
4662 if (!usb_driver_claim_interface(&btusb_driver,
4663 data->diag, data))
4664 __set_diag_interface(hdev);
4665 else
4666 data->diag = NULL;
4667 }
4668
4669 if (enable_autosuspend)
4670 usb_enable_autosuspend(data->udev);
4671
4672 err = hci_register_dev(hdev);
4673 if (err < 0)
4674 goto out_free_dev;
4675
4676 usb_set_intfdata(intf, data);
4677
4678 return 0;
4679
4680 out_free_dev:
4681 if (data->reset_gpio)
4682 gpiod_put(data->reset_gpio);
4683 hci_free_dev(hdev);
4684 return err;
4685 }
4686
4687 static void btusb_disconnect(struct usb_interface *intf)
4688 {
4689 struct btusb_data *data = usb_get_intfdata(intf);
4690 struct hci_dev *hdev;
4691
4692 BT_DBG("intf %p", intf);
4693
4694 if (!data)
4695 return;
4696
4697 hdev = data->hdev;
4698 usb_set_intfdata(data->intf, NULL);
4699
4700 if (data->isoc)
4701 usb_set_intfdata(data->isoc, NULL);
4702
4703 if (data->diag)
4704 usb_set_intfdata(data->diag, NULL);
4705
4706 hci_unregister_dev(hdev);
4707
4708 if (intf == data->intf) {
4709 if (data->isoc)
4710 usb_driver_release_interface(&btusb_driver, data->isoc);
4711 if (data->diag)
4712 usb_driver_release_interface(&btusb_driver, data->diag);
4713 } else if (intf == data->isoc) {
4714 if (data->diag)
4715 usb_driver_release_interface(&btusb_driver, data->diag);
4716 usb_driver_release_interface(&btusb_driver, data->intf);
4717 } else if (intf == data->diag) {
4718 usb_driver_release_interface(&btusb_driver, data->intf);
4719 if (data->isoc)
4720 usb_driver_release_interface(&btusb_driver, data->isoc);
4721 }
4722
4723 if (data->oob_wake_irq)
4724 device_init_wakeup(&data->udev->dev, false);
4725
4726 if (data->reset_gpio)
4727 gpiod_put(data->reset_gpio);
4728
4729 hci_free_dev(hdev);
4730 }
4731
4732 #ifdef CONFIG_PM
4733 static int btusb_suspend(struct usb_interface *intf, pm_message_t message)
4734 {
4735 struct btusb_data *data = usb_get_intfdata(intf);
4736
4737 BT_DBG("intf %p", intf);
4738
4739 if (data->suspend_count++)
4740 return 0;
4741
4742 spin_lock_irq(&data->txlock);
4743 if (!(PMSG_IS_AUTO(message) && data->tx_in_flight)) {
4744 set_bit(BTUSB_SUSPENDING, &data->flags);
4745 spin_unlock_irq(&data->txlock);
4746 } else {
4747 spin_unlock_irq(&data->txlock);
4748 data->suspend_count--;
4749 return -EBUSY;
4750 }
4751
4752 cancel_work_sync(&data->work);
4753
4754 btusb_stop_traffic(data);
4755 usb_kill_anchored_urbs(&data->tx_anchor);
4756
4757 if (data->oob_wake_irq && device_may_wakeup(&data->udev->dev)) {
4758 set_bit(BTUSB_OOB_WAKE_ENABLED, &data->flags);
4759 enable_irq_wake(data->oob_wake_irq);
4760 enable_irq(data->oob_wake_irq);
4761 }
4762
4763 /* For global suspend, Realtek devices lose the loaded fw
4764 * in them. But for autosuspend, firmware should remain.
4765 * Actually, it depends on whether the usb host sends
4766 * set feature (enable wakeup) or not.
4767 */
4768 if (test_bit(BTUSB_WAKEUP_DISABLE, &data->flags)) {
4769 if (PMSG_IS_AUTO(message) &&
4770 device_can_wakeup(&data->udev->dev))
4771 data->udev->do_remote_wakeup = 1;
4772 else if (!PMSG_IS_AUTO(message))
4773 data->udev->reset_resume = 1;
4774 }
4775
4776 return 0;
4777 }
4778
4779 static void play_deferred(struct btusb_data *data)
4780 {
4781 struct urb *urb;
4782 int err;
4783
4784 while ((urb = usb_get_from_anchor(&data->deferred))) {
4785 usb_anchor_urb(urb, &data->tx_anchor);
4786
4787 err = usb_submit_urb(urb, GFP_ATOMIC);
4788 if (err < 0) {
4789 if (err != -EPERM && err != -ENODEV)
4790 BT_ERR("%s urb %p submission failed (%d)",
4791 data->hdev->name, urb, -err);
4792 kfree(urb->setup_packet);
4793 usb_unanchor_urb(urb);
4794 usb_free_urb(urb);
4795 break;
4796 }
4797
4798 data->tx_in_flight++;
4799 usb_free_urb(urb);
4800 }
4801
4802 /* Cleanup the rest deferred urbs. */
4803 while ((urb = usb_get_from_anchor(&data->deferred))) {
4804 kfree(urb->setup_packet);
4805 usb_free_urb(urb);
4806 }
4807 }
4808
4809 static int btusb_resume(struct usb_interface *intf)
4810 {
4811 struct btusb_data *data = usb_get_intfdata(intf);
4812 struct hci_dev *hdev = data->hdev;
4813 int err = 0;
4814
4815 BT_DBG("intf %p", intf);
4816
4817 if (--data->suspend_count)
4818 return 0;
4819
4820 /* Disable only if not already disabled (keep it balanced) */
4821 if (test_and_clear_bit(BTUSB_OOB_WAKE_ENABLED, &data->flags)) {
4822 disable_irq(data->oob_wake_irq);
4823 disable_irq_wake(data->oob_wake_irq);
4824 }
4825
4826 if (!test_bit(HCI_RUNNING, &hdev->flags))
4827 goto done;
4828
4829 if (test_bit(BTUSB_INTR_RUNNING, &data->flags)) {
4830 err = btusb_submit_intr_urb(hdev, GFP_NOIO);
4831 if (err < 0) {
4832 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
4833 goto failed;
4834 }
4835 }
4836
4837 if (test_bit(BTUSB_BULK_RUNNING, &data->flags)) {
4838 err = btusb_submit_bulk_urb(hdev, GFP_NOIO);
4839 if (err < 0) {
4840 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
4841 goto failed;
4842 }
4843
4844 btusb_submit_bulk_urb(hdev, GFP_NOIO);
4845 }
4846
4847 if (test_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
4848 if (btusb_submit_isoc_urb(hdev, GFP_NOIO) < 0)
4849 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
4850 else
4851 btusb_submit_isoc_urb(hdev, GFP_NOIO);
4852 }
4853
4854 spin_lock_irq(&data->txlock);
4855 play_deferred(data);
4856 clear_bit(BTUSB_SUSPENDING, &data->flags);
4857 spin_unlock_irq(&data->txlock);
4858 schedule_work(&data->work);
4859
4860 return 0;
4861
4862 failed:
4863 usb_scuttle_anchored_urbs(&data->deferred);
4864 done:
4865 spin_lock_irq(&data->txlock);
4866 clear_bit(BTUSB_SUSPENDING, &data->flags);
4867 spin_unlock_irq(&data->txlock);
4868
4869 return err;
4870 }
4871 #endif
4872
4873 static struct usb_driver btusb_driver = {
4874 .name = "btusb",
4875 .probe = btusb_probe,
4876 .disconnect = btusb_disconnect,
4877 #ifdef CONFIG_PM
4878 .suspend = btusb_suspend,
4879 .resume = btusb_resume,
4880 #endif
4881 .id_table = btusb_table,
4882 .supports_autosuspend = 1,
4883 .disable_hub_initiated_lpm = 1,
4884 };
4885
4886 module_usb_driver(btusb_driver);
4887
4888 module_param(disable_scofix, bool, 0644);
4889 MODULE_PARM_DESC(disable_scofix, "Disable fixup of wrong SCO buffer size");
4890
4891 module_param(force_scofix, bool, 0644);
4892 MODULE_PARM_DESC(force_scofix, "Force fixup of wrong SCO buffers size");
4893
4894 module_param(enable_autosuspend, bool, 0644);
4895 MODULE_PARM_DESC(enable_autosuspend, "Enable USB autosuspend by default");
4896
4897 module_param(reset, bool, 0644);
4898 MODULE_PARM_DESC(reset, "Send HCI reset command on initialization");
4899
4900 MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
4901 MODULE_DESCRIPTION("Generic Bluetooth USB driver ver " VERSION);
4902 MODULE_VERSION(VERSION);
4903 MODULE_LICENSE("GPL");