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