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