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