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