Merge tag 'pm-6.16-rc5' of git://git.kernel.org/pub/scm/linux/kernel/git/rafael/linux-pm
[linux-2.6-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>
800fe5ec 19#include <linux/debugfs.h>
5f60d5f6 20#include <linux/unaligned.h>
5e23b923
MH
21
22#include <net/bluetooth/bluetooth.h>
23#include <net/bluetooth/hci_core.h>
04425292 24#include <net/bluetooth/hci_drv.h>
5e23b923 25
4185a0f5 26#include "btintel.h"
1df1f591 27#include "btbcm.h"
db33c77d 28#include "btrtl.h"
8c0d17b6 29#include "btmtk.h"
1df1f591 30
34dced9b 31#define VERSION "0.8"
cfeb4145 32
90ab5ee9
RR
33static bool disable_scofix;
34static bool force_scofix;
eff2d68c 35static bool enable_autosuspend = IS_ENABLED(CONFIG_BT_HCIBTUSB_AUTOSUSPEND);
bb1afb29 36static bool enable_poll_sync = IS_ENABLED(CONFIG_BT_HCIBTUSB_POLL_SYNC);
917a3337 37static bool reset = true;
cfeb4145
MH
38
39static struct usb_driver btusb_driver;
40
f95fd5f3
LAD
41#define BTUSB_IGNORE BIT(0)
42#define BTUSB_DIGIANSWER BIT(1)
43#define BTUSB_CSR BIT(2)
44#define BTUSB_SNIFFER BIT(3)
45#define BTUSB_BCM92035 BIT(4)
46#define BTUSB_BROKEN_ISOC BIT(5)
47#define BTUSB_WRONG_SCO_MTU BIT(6)
48#define BTUSB_ATH3012 BIT(7)
49#define BTUSB_INTEL_COMBINED BIT(8)
50#define BTUSB_INTEL_BOOT BIT(9)
51#define BTUSB_BCM_PATCHRAM BIT(10)
52#define BTUSB_MARVELL BIT(11)
53#define BTUSB_SWAVE BIT(12)
54#define BTUSB_AMP BIT(13)
55#define BTUSB_QCA_ROME BIT(14)
56#define BTUSB_BCM_APPLE BIT(15)
57#define BTUSB_REALTEK BIT(16)
58#define BTUSB_BCM2045 BIT(17)
59#define BTUSB_IFNUM_2 BIT(18)
60#define BTUSB_CW6622 BIT(19)
61#define BTUSB_MEDIATEK BIT(20)
62#define BTUSB_WIDEBAND_SPEECH BIT(21)
0fec656d 63#define BTUSB_INVALID_LE_STATES BIT(22)
f95fd5f3
LAD
64#define BTUSB_QCA_WCN6855 BIT(23)
65#define BTUSB_INTEL_BROKEN_SHUTDOWN_LED BIT(24)
66#define BTUSB_INTEL_BROKEN_INITIAL_NCMD BIT(25)
67#define BTUSB_INTEL_NO_WBS_SUPPORT BIT(26)
5d44f76f 68#define BTUSB_ACTIONS_SEMI BIT(27)
5e23b923 69
54265202 70static const struct usb_device_id btusb_table[] = {
5e23b923
MH
71 /* Generic Bluetooth USB device */
72 { USB_DEVICE_INFO(0xe0, 0x01, 0x01) },
73
893ba544
MH
74 /* Generic Bluetooth AMP device */
75 { USB_DEVICE_INFO(0xe0, 0x01, 0x04), .driver_info = BTUSB_AMP },
76
d63b2826
DD
77 /* Generic Bluetooth USB interface */
78 { USB_INTERFACE_INFO(0xe0, 0x01, 0x01) },
79
1fa6535f 80 /* Apple-specific (Broadcom) devices */
17b2772b 81 { USB_VENDOR_AND_INTERFACE_INFO(0x05ac, 0xff, 0x01, 0x01),
22f8e9db 82 .driver_info = BTUSB_BCM_APPLE | BTUSB_IFNUM_2 },
1fa6535f 83
178c059e
CYC
84 /* MediaTek MT76x0E */
85 { USB_DEVICE(0x0e8d, 0x763f) },
86
c510eae3 87 /* Broadcom SoftSailing reporting vendor specific */
2e8b5063 88 { USB_DEVICE(0x0a5c, 0x21e1) },
c510eae3 89
3cd01976
NI
90 /* Apple MacBookPro 7,1 */
91 { USB_DEVICE(0x05ac, 0x8213) },
92
0a79f674
CL
93 /* Apple iMac11,1 */
94 { USB_DEVICE(0x05ac, 0x8215) },
95
9c047157
NI
96 /* Apple MacBookPro6,2 */
97 { USB_DEVICE(0x05ac, 0x8218) },
98
3e3ede7d
EH
99 /* Apple MacBookAir3,1, MacBookAir3,2 */
100 { USB_DEVICE(0x05ac, 0x821b) },
101
a63b723d
PAVM
102 /* Apple MacBookAir4,1 */
103 { USB_DEVICE(0x05ac, 0x821f) },
104
88d377b6
MAP
105 /* Apple MacBookPro8,2 */
106 { USB_DEVICE(0x05ac, 0x821a) },
107
f78b6826
JK
108 /* Apple MacMini5,1 */
109 { USB_DEVICE(0x05ac, 0x8281) },
110
cfeb4145 111 /* AVM BlueFRITZ! USB v2.0 */
4fcef8ed 112 { USB_DEVICE(0x057c, 0x3800), .driver_info = BTUSB_SWAVE },
cfeb4145
MH
113
114 /* Bluetooth Ultraport Module from IBM */
115 { USB_DEVICE(0x04bf, 0x030a) },
116
117 /* ALPS Modules with non-standard id */
118 { USB_DEVICE(0x044e, 0x3001) },
119 { USB_DEVICE(0x044e, 0x3002) },
120
121 /* Ericsson with non-standard id */
122 { USB_DEVICE(0x0bdb, 0x1002) },
123
124 /* Canyon CN-BTU1 with HID interfaces */
7a9d4020 125 { USB_DEVICE(0x0c10, 0x0000) },
cfeb4145 126
d049f4e5
MH
127 /* Broadcom BCM20702B0 (Dynex/Insignia) */
128 { USB_DEVICE(0x19ff, 0x0239), .driver_info = BTUSB_BCM_PATCHRAM },
129
2faf71ce 130 /* Broadcom BCM43142A0 (Foxconn/Lenovo) */
628c26b4
DT
131 { USB_VENDOR_AND_INTERFACE_INFO(0x105b, 0xff, 0x01, 0x01),
132 .driver_info = BTUSB_BCM_PATCHRAM },
2faf71ce 133
a57bac43
CH
134 /* Broadcom BCM920703 (HTC Vive) */
135 { USB_VENDOR_AND_INTERFACE_INFO(0x0bb4, 0xff, 0x01, 0x01),
136 .driver_info = BTUSB_BCM_PATCHRAM },
137
98514036 138 /* Foxconn - Hon Hai */
6029ddc2
HS
139 { USB_VENDOR_AND_INTERFACE_INFO(0x0489, 0xff, 0x01, 0x01),
140 .driver_info = BTUSB_BCM_PATCHRAM },
98514036 141
8f0c304c
MD
142 /* Lite-On Technology - Broadcom based */
143 { USB_VENDOR_AND_INTERFACE_INFO(0x04ca, 0xff, 0x01, 0x01),
144 .driver_info = BTUSB_BCM_PATCHRAM },
145
0b880062 146 /* Broadcom devices with vendor specific id */
10d4c673
PG
147 { USB_VENDOR_AND_INTERFACE_INFO(0x0a5c, 0xff, 0x01, 0x01),
148 .driver_info = BTUSB_BCM_PATCHRAM },
92c385f4 149
c2aef6e8 150 /* ASUSTek Computer - Broadcom based */
9a5abdaa
RD
151 { USB_VENDOR_AND_INTERFACE_INFO(0x0b05, 0xff, 0x01, 0x01),
152 .driver_info = BTUSB_BCM_PATCHRAM },
c2aef6e8 153
5bcecf32 154 /* Belkin F8065bf - Broadcom based */
6331c686
MH
155 { USB_VENDOR_AND_INTERFACE_INFO(0x050d, 0xff, 0x01, 0x01),
156 .driver_info = BTUSB_BCM_PATCHRAM },
5bcecf32 157
9113bfd8 158 /* IMC Networks - Broadcom based */
6331c686
MH
159 { USB_VENDOR_AND_INTERFACE_INFO(0x13d3, 0xff, 0x01, 0x01),
160 .driver_info = BTUSB_BCM_PATCHRAM },
9113bfd8 161
fdfddc60
WJS
162 /* Dell Computer - Broadcom based */
163 { USB_VENDOR_AND_INTERFACE_INFO(0x413c, 0xff, 0x01, 0x01),
164 .driver_info = BTUSB_BCM_PATCHRAM },
165
1623d0bf
DT
166 /* Toshiba Corp - Broadcom based */
167 { USB_VENDOR_AND_INTERFACE_INFO(0x0930, 0xff, 0x01, 0x01),
168 .driver_info = BTUSB_BCM_PATCHRAM },
169
40df783d 170 /* Intel Bluetooth USB Bootloader (RAM module) */
d92f2df0
MH
171 { USB_DEVICE(0x8087, 0x0a5a),
172 .driver_info = BTUSB_INTEL_BOOT | BTUSB_BROKEN_ISOC },
40df783d 173
5e23b923
MH
174 { } /* Terminating entry */
175};
176
177MODULE_DEVICE_TABLE(usb, btusb_table);
178
d831e361 179static const struct usb_device_id quirks_table[] = {
cfeb4145
MH
180 /* CSR BlueCore devices */
181 { USB_DEVICE(0x0a12, 0x0001), .driver_info = BTUSB_CSR },
182
183 /* Broadcom BCM2033 without firmware */
184 { USB_DEVICE(0x0a5c, 0x2033), .driver_info = BTUSB_IGNORE },
185
6c9d435d
MH
186 /* Broadcom BCM2045 devices */
187 { USB_DEVICE(0x0a5c, 0x2045), .driver_info = BTUSB_BCM2045 },
188
be93112a 189 /* Atheros 3011 with sflash firmware */
0b880062
AS
190 { USB_DEVICE(0x0489, 0xe027), .driver_info = BTUSB_IGNORE },
191 { USB_DEVICE(0x0489, 0xe03d), .driver_info = BTUSB_IGNORE },
2eeff0b4 192 { USB_DEVICE(0x04f2, 0xaff1), .driver_info = BTUSB_IGNORE },
0b880062 193 { USB_DEVICE(0x0930, 0x0215), .driver_info = BTUSB_IGNORE },
be93112a 194 { USB_DEVICE(0x0cf3, 0x3002), .driver_info = BTUSB_IGNORE },
6eda541d 195 { USB_DEVICE(0x0cf3, 0xe019), .driver_info = BTUSB_IGNORE },
2a7bcccc 196 { USB_DEVICE(0x13d3, 0x3304), .driver_info = BTUSB_IGNORE },
be93112a 197
509e7861
CYC
198 /* Atheros AR9285 Malbec with sflash firmware */
199 { USB_DEVICE(0x03f0, 0x311d), .driver_info = BTUSB_IGNORE },
200
d9f51b51 201 /* Atheros 3012 with sflash firmware */
0b880062
AS
202 { USB_DEVICE(0x0489, 0xe04d), .driver_info = BTUSB_ATH3012 },
203 { USB_DEVICE(0x0489, 0xe04e), .driver_info = BTUSB_ATH3012 },
204 { USB_DEVICE(0x0489, 0xe056), .driver_info = BTUSB_ATH3012 },
205 { USB_DEVICE(0x0489, 0xe057), .driver_info = BTUSB_ATH3012 },
206 { USB_DEVICE(0x0489, 0xe05f), .driver_info = BTUSB_ATH3012 },
692c062e 207 { USB_DEVICE(0x0489, 0xe076), .driver_info = BTUSB_ATH3012 },
4b552bc9 208 { USB_DEVICE(0x0489, 0xe078), .driver_info = BTUSB_ATH3012 },
28c971d8 209 { USB_DEVICE(0x0489, 0xe095), .driver_info = BTUSB_ATH3012 },
0b880062
AS
210 { USB_DEVICE(0x04c5, 0x1330), .driver_info = BTUSB_ATH3012 },
211 { USB_DEVICE(0x04ca, 0x3004), .driver_info = BTUSB_ATH3012 },
212 { USB_DEVICE(0x04ca, 0x3005), .driver_info = BTUSB_ATH3012 },
213 { USB_DEVICE(0x04ca, 0x3006), .driver_info = BTUSB_ATH3012 },
1fb4e09a 214 { USB_DEVICE(0x04ca, 0x3007), .driver_info = BTUSB_ATH3012 },
0b880062
AS
215 { USB_DEVICE(0x04ca, 0x3008), .driver_info = BTUSB_ATH3012 },
216 { USB_DEVICE(0x04ca, 0x300b), .driver_info = BTUSB_ATH3012 },
7e730c7f 217 { USB_DEVICE(0x04ca, 0x300d), .driver_info = BTUSB_ATH3012 },
ec0810d2 218 { USB_DEVICE(0x04ca, 0x300f), .driver_info = BTUSB_ATH3012 },
134d3b35 219 { USB_DEVICE(0x04ca, 0x3010), .driver_info = BTUSB_ATH3012 },
81d90442 220 { USB_DEVICE(0x04ca, 0x3014), .driver_info = BTUSB_ATH3012 },
441ad62d 221 { USB_DEVICE(0x04ca, 0x3018), .driver_info = BTUSB_ATH3012 },
0b880062 222 { USB_DEVICE(0x0930, 0x0219), .driver_info = BTUSB_ATH3012 },
cd355ff0 223 { USB_DEVICE(0x0930, 0x021c), .driver_info = BTUSB_ATH3012 },
0b880062 224 { USB_DEVICE(0x0930, 0x0220), .driver_info = BTUSB_ATH3012 },
89d2975f 225 { USB_DEVICE(0x0930, 0x0227), .driver_info = BTUSB_ATH3012 },
a735f9e2 226 { USB_DEVICE(0x0b05, 0x17d0), .driver_info = BTUSB_ATH3012 },
d66629c1 227 { USB_DEVICE(0x0cf3, 0x0036), .driver_info = BTUSB_ATH3012 },
544a5916 228 { USB_DEVICE(0x0cf3, 0x3004), .driver_info = BTUSB_ATH3012 },
94a32d10 229 { USB_DEVICE(0x0cf3, 0x3008), .driver_info = BTUSB_ATH3012 },
07c0ea87 230 { USB_DEVICE(0x0cf3, 0x311d), .driver_info = BTUSB_ATH3012 },
b131237c 231 { USB_DEVICE(0x0cf3, 0x311e), .driver_info = BTUSB_ATH3012 },
1e56f1eb 232 { USB_DEVICE(0x0cf3, 0x311f), .driver_info = BTUSB_ATH3012 },
0b880062 233 { USB_DEVICE(0x0cf3, 0x3121), .driver_info = BTUSB_ATH3012 },
ebaf5795 234 { USB_DEVICE(0x0cf3, 0x817a), .driver_info = BTUSB_ATH3012 },
18e0afab 235 { USB_DEVICE(0x0cf3, 0x817b), .driver_info = BTUSB_ATH3012 },
0b880062 236 { USB_DEVICE(0x0cf3, 0xe003), .driver_info = BTUSB_ATH3012 },
ac71311e 237 { USB_DEVICE(0x0cf3, 0xe004), .driver_info = BTUSB_ATH3012 },
0a3658cc 238 { USB_DEVICE(0x0cf3, 0xe005), .driver_info = BTUSB_ATH3012 },
ca79f232 239 { USB_DEVICE(0x0cf3, 0xe006), .driver_info = BTUSB_ATH3012 },
0b880062
AS
240 { USB_DEVICE(0x13d3, 0x3362), .driver_info = BTUSB_ATH3012 },
241 { USB_DEVICE(0x13d3, 0x3375), .driver_info = BTUSB_ATH3012 },
eed307e2 242 { USB_DEVICE(0x13d3, 0x3393), .driver_info = BTUSB_ATH3012 },
609574eb 243 { USB_DEVICE(0x13d3, 0x3395), .driver_info = BTUSB_ATH3012 },
5b77a1f3 244 { USB_DEVICE(0x13d3, 0x3402), .driver_info = BTUSB_ATH3012 },
3bb30a7c 245 { USB_DEVICE(0x13d3, 0x3408), .driver_info = BTUSB_ATH3012 },
033efa92 246 { USB_DEVICE(0x13d3, 0x3423), .driver_info = BTUSB_ATH3012 },
fa2f1394 247 { USB_DEVICE(0x13d3, 0x3432), .driver_info = BTUSB_ATH3012 },
75c6aca4 248 { USB_DEVICE(0x13d3, 0x3472), .driver_info = BTUSB_ATH3012 },
0d0cef61 249 { USB_DEVICE(0x13d3, 0x3474), .driver_info = BTUSB_ATH3012 },
72f9f8b5 250 { USB_DEVICE(0x13d3, 0x3487), .driver_info = BTUSB_ATH3012 },
12d86896 251 { USB_DEVICE(0x13d3, 0x3490), .driver_info = BTUSB_ATH3012 },
d9f51b51 252
e9036e33
CYC
253 /* Atheros AR5BBU12 with sflash firmware */
254 { USB_DEVICE(0x0489, 0xe02c), .driver_info = BTUSB_IGNORE },
255
85d59726 256 /* Atheros AR5BBU12 with sflash firmware */
bc21fde2 257 { USB_DEVICE(0x0489, 0xe036), .driver_info = BTUSB_ATH3012 },
0b880062 258 { USB_DEVICE(0x0489, 0xe03c), .driver_info = BTUSB_ATH3012 },
85d59726 259
3267c884 260 /* QCA ROME chipset */
e99f38b8
RL
261 { USB_DEVICE(0x0cf3, 0x535b), .driver_info = BTUSB_QCA_ROME |
262 BTUSB_WIDEBAND_SPEECH },
263 { USB_DEVICE(0x0cf3, 0xe007), .driver_info = BTUSB_QCA_ROME |
264 BTUSB_WIDEBAND_SPEECH },
265 { USB_DEVICE(0x0cf3, 0xe009), .driver_info = BTUSB_QCA_ROME |
266 BTUSB_WIDEBAND_SPEECH },
267 { USB_DEVICE(0x0cf3, 0xe010), .driver_info = BTUSB_QCA_ROME |
268 BTUSB_WIDEBAND_SPEECH },
269 { USB_DEVICE(0x0cf3, 0xe300), .driver_info = BTUSB_QCA_ROME |
270 BTUSB_WIDEBAND_SPEECH },
271 { USB_DEVICE(0x0cf3, 0xe301), .driver_info = BTUSB_QCA_ROME |
272 BTUSB_WIDEBAND_SPEECH },
273 { USB_DEVICE(0x0cf3, 0xe360), .driver_info = BTUSB_QCA_ROME |
274 BTUSB_WIDEBAND_SPEECH },
0324d19c
DL
275 { USB_DEVICE(0x0cf3, 0xe500), .driver_info = BTUSB_QCA_ROME |
276 BTUSB_WIDEBAND_SPEECH },
e99f38b8
RL
277 { USB_DEVICE(0x0489, 0xe092), .driver_info = BTUSB_QCA_ROME |
278 BTUSB_WIDEBAND_SPEECH },
279 { USB_DEVICE(0x0489, 0xe09f), .driver_info = BTUSB_QCA_ROME |
280 BTUSB_WIDEBAND_SPEECH },
281 { USB_DEVICE(0x0489, 0xe0a2), .driver_info = BTUSB_QCA_ROME |
282 BTUSB_WIDEBAND_SPEECH },
283 { USB_DEVICE(0x04ca, 0x3011), .driver_info = BTUSB_QCA_ROME |
284 BTUSB_WIDEBAND_SPEECH },
285 { USB_DEVICE(0x04ca, 0x3015), .driver_info = BTUSB_QCA_ROME |
286 BTUSB_WIDEBAND_SPEECH },
287 { USB_DEVICE(0x04ca, 0x3016), .driver_info = BTUSB_QCA_ROME |
288 BTUSB_WIDEBAND_SPEECH },
289 { USB_DEVICE(0x04ca, 0x301a), .driver_info = BTUSB_QCA_ROME |
290 BTUSB_WIDEBAND_SPEECH },
291 { USB_DEVICE(0x04ca, 0x3021), .driver_info = BTUSB_QCA_ROME |
292 BTUSB_WIDEBAND_SPEECH },
293 { USB_DEVICE(0x13d3, 0x3491), .driver_info = BTUSB_QCA_ROME |
294 BTUSB_WIDEBAND_SPEECH },
295 { USB_DEVICE(0x13d3, 0x3496), .driver_info = BTUSB_QCA_ROME |
296 BTUSB_WIDEBAND_SPEECH },
297 { USB_DEVICE(0x13d3, 0x3501), .driver_info = BTUSB_QCA_ROME |
298 BTUSB_WIDEBAND_SPEECH },
3267c884 299
b40f58b9
RL
300 /* QCA WCN6855 chipset */
301 { USB_DEVICE(0x0cf3, 0xe600), .driver_info = BTUSB_QCA_WCN6855 |
0fec656d 302 BTUSB_WIDEBAND_SPEECH },
21a241b3 303 { USB_DEVICE(0x0489, 0xe0cc), .driver_info = BTUSB_QCA_WCN6855 |
0fec656d 304 BTUSB_WIDEBAND_SPEECH },
21a241b3 305 { USB_DEVICE(0x0489, 0xe0d6), .driver_info = BTUSB_QCA_WCN6855 |
0fec656d 306 BTUSB_WIDEBAND_SPEECH },
e8c42585 307 { USB_DEVICE(0x0489, 0xe0e3), .driver_info = BTUSB_QCA_WCN6855 |
0fec656d 308 BTUSB_WIDEBAND_SPEECH },
d2666be5 309 { USB_DEVICE(0x10ab, 0x9309), .driver_info = BTUSB_QCA_WCN6855 |
0fec656d 310 BTUSB_WIDEBAND_SPEECH },
d2666be5 311 { USB_DEVICE(0x10ab, 0x9409), .driver_info = BTUSB_QCA_WCN6855 |
0fec656d 312 BTUSB_WIDEBAND_SPEECH },
1cd563eb 313 { USB_DEVICE(0x0489, 0xe0d0), .driver_info = BTUSB_QCA_WCN6855 |
0fec656d 314 BTUSB_WIDEBAND_SPEECH },
ca2a9944 315 { USB_DEVICE(0x10ab, 0x9108), .driver_info = BTUSB_QCA_WCN6855 |
0fec656d 316 BTUSB_WIDEBAND_SPEECH },
ca2a9944 317 { USB_DEVICE(0x10ab, 0x9109), .driver_info = BTUSB_QCA_WCN6855 |
0fec656d 318 BTUSB_WIDEBAND_SPEECH },
ca2a9944 319 { USB_DEVICE(0x10ab, 0x9208), .driver_info = BTUSB_QCA_WCN6855 |
0fec656d 320 BTUSB_WIDEBAND_SPEECH },
ca2a9944 321 { USB_DEVICE(0x10ab, 0x9209), .driver_info = BTUSB_QCA_WCN6855 |
0fec656d 322 BTUSB_WIDEBAND_SPEECH },
ca2a9944 323 { USB_DEVICE(0x10ab, 0x9308), .driver_info = BTUSB_QCA_WCN6855 |
0fec656d 324 BTUSB_WIDEBAND_SPEECH },
ca2a9944 325 { USB_DEVICE(0x10ab, 0x9408), .driver_info = BTUSB_QCA_WCN6855 |
0fec656d 326 BTUSB_WIDEBAND_SPEECH },
ca2a9944 327 { USB_DEVICE(0x10ab, 0x9508), .driver_info = BTUSB_QCA_WCN6855 |
0fec656d 328 BTUSB_WIDEBAND_SPEECH },
ca2a9944 329 { USB_DEVICE(0x10ab, 0x9509), .driver_info = BTUSB_QCA_WCN6855 |
0fec656d 330 BTUSB_WIDEBAND_SPEECH },
ca2a9944 331 { USB_DEVICE(0x10ab, 0x9608), .driver_info = BTUSB_QCA_WCN6855 |
0fec656d 332 BTUSB_WIDEBAND_SPEECH },
ca2a9944 333 { USB_DEVICE(0x10ab, 0x9609), .driver_info = BTUSB_QCA_WCN6855 |
0fec656d 334 BTUSB_WIDEBAND_SPEECH },
ca2a9944 335 { USB_DEVICE(0x10ab, 0x9f09), .driver_info = BTUSB_QCA_WCN6855 |
0fec656d 336 BTUSB_WIDEBAND_SPEECH },
ca2a9944 337 { USB_DEVICE(0x04ca, 0x3022), .driver_info = BTUSB_QCA_WCN6855 |
0fec656d 338 BTUSB_WIDEBAND_SPEECH },
ca2a9944 339 { USB_DEVICE(0x0489, 0xe0c7), .driver_info = BTUSB_QCA_WCN6855 |
0fec656d 340 BTUSB_WIDEBAND_SPEECH },
ca2a9944 341 { USB_DEVICE(0x0489, 0xe0c9), .driver_info = BTUSB_QCA_WCN6855 |
0fec656d 342 BTUSB_WIDEBAND_SPEECH },
ca2a9944 343 { USB_DEVICE(0x0489, 0xe0ca), .driver_info = BTUSB_QCA_WCN6855 |
0fec656d 344 BTUSB_WIDEBAND_SPEECH },
ca2a9944 345 { USB_DEVICE(0x0489, 0xe0cb), .driver_info = BTUSB_QCA_WCN6855 |
0fec656d 346 BTUSB_WIDEBAND_SPEECH },
ca2a9944 347 { USB_DEVICE(0x0489, 0xe0ce), .driver_info = BTUSB_QCA_WCN6855 |
0fec656d 348 BTUSB_WIDEBAND_SPEECH },
ca2a9944 349 { USB_DEVICE(0x0489, 0xe0de), .driver_info = BTUSB_QCA_WCN6855 |
0fec656d 350 BTUSB_WIDEBAND_SPEECH },
ca2a9944 351 { USB_DEVICE(0x0489, 0xe0df), .driver_info = BTUSB_QCA_WCN6855 |
0fec656d 352 BTUSB_WIDEBAND_SPEECH },
ca2a9944 353 { USB_DEVICE(0x0489, 0xe0e1), .driver_info = BTUSB_QCA_WCN6855 |
0fec656d 354 BTUSB_WIDEBAND_SPEECH },
ca2a9944 355 { USB_DEVICE(0x0489, 0xe0ea), .driver_info = BTUSB_QCA_WCN6855 |
0fec656d 356 BTUSB_WIDEBAND_SPEECH },
ca2a9944 357 { USB_DEVICE(0x0489, 0xe0ec), .driver_info = BTUSB_QCA_WCN6855 |
0fec656d 358 BTUSB_WIDEBAND_SPEECH },
ca2a9944 359 { USB_DEVICE(0x04ca, 0x3023), .driver_info = BTUSB_QCA_WCN6855 |
0fec656d 360 BTUSB_WIDEBAND_SPEECH },
ca2a9944 361 { USB_DEVICE(0x04ca, 0x3024), .driver_info = BTUSB_QCA_WCN6855 |
0fec656d 362 BTUSB_WIDEBAND_SPEECH },
ca2a9944 363 { USB_DEVICE(0x04ca, 0x3a22), .driver_info = BTUSB_QCA_WCN6855 |
0fec656d 364 BTUSB_WIDEBAND_SPEECH },
ca2a9944 365 { USB_DEVICE(0x04ca, 0x3a24), .driver_info = BTUSB_QCA_WCN6855 |
0fec656d 366 BTUSB_WIDEBAND_SPEECH },
ca2a9944 367 { USB_DEVICE(0x04ca, 0x3a26), .driver_info = BTUSB_QCA_WCN6855 |
0fec656d 368 BTUSB_WIDEBAND_SPEECH },
ca2a9944 369 { USB_DEVICE(0x04ca, 0x3a27), .driver_info = BTUSB_QCA_WCN6855 |
0fec656d 370 BTUSB_WIDEBAND_SPEECH },
b40f58b9 371
46225947
ZH
372 /* QCA WCN785x chipset */
373 { USB_DEVICE(0x0cf3, 0xe700), .driver_info = BTUSB_QCA_WCN6855 |
0fec656d 374 BTUSB_WIDEBAND_SPEECH },
add1b165
AM
375 { USB_DEVICE(0x0489, 0xe0fc), .driver_info = BTUSB_QCA_WCN6855 |
376 BTUSB_WIDEBAND_SPEECH },
45f745dd
ZH
377 { USB_DEVICE(0x0489, 0xe0f3), .driver_info = BTUSB_QCA_WCN6855 |
378 BTUSB_WIDEBAND_SPEECH },
2dd1c1ee
ZH
379 { USB_DEVICE(0x0489, 0xe100), .driver_info = BTUSB_QCA_WCN6855 |
380 BTUSB_WIDEBAND_SPEECH },
381 { USB_DEVICE(0x0489, 0xe103), .driver_info = BTUSB_QCA_WCN6855 |
382 BTUSB_WIDEBAND_SPEECH },
383 { USB_DEVICE(0x0489, 0xe10a), .driver_info = BTUSB_QCA_WCN6855 |
384 BTUSB_WIDEBAND_SPEECH },
c7629ccf
DC
385 { USB_DEVICE(0x0489, 0xe10d), .driver_info = BTUSB_QCA_WCN6855 |
386 BTUSB_WIDEBAND_SPEECH },
2dd1c1ee
ZH
387 { USB_DEVICE(0x0489, 0xe11b), .driver_info = BTUSB_QCA_WCN6855 |
388 BTUSB_WIDEBAND_SPEECH },
389 { USB_DEVICE(0x0489, 0xe11c), .driver_info = BTUSB_QCA_WCN6855 |
390 BTUSB_WIDEBAND_SPEECH },
391 { USB_DEVICE(0x0489, 0xe11f), .driver_info = BTUSB_QCA_WCN6855 |
392 BTUSB_WIDEBAND_SPEECH },
393 { USB_DEVICE(0x0489, 0xe141), .driver_info = BTUSB_QCA_WCN6855 |
394 BTUSB_WIDEBAND_SPEECH },
395 { USB_DEVICE(0x0489, 0xe14a), .driver_info = BTUSB_QCA_WCN6855 |
396 BTUSB_WIDEBAND_SPEECH },
397 { USB_DEVICE(0x0489, 0xe14b), .driver_info = BTUSB_QCA_WCN6855 |
398 BTUSB_WIDEBAND_SPEECH },
399 { USB_DEVICE(0x0489, 0xe14d), .driver_info = BTUSB_QCA_WCN6855 |
400 BTUSB_WIDEBAND_SPEECH },
e69bcffc
ZH
401 { USB_DEVICE(0x13d3, 0x3623), .driver_info = BTUSB_QCA_WCN6855 |
402 BTUSB_WIDEBAND_SPEECH },
2dd1c1ee
ZH
403 { USB_DEVICE(0x13d3, 0x3624), .driver_info = BTUSB_QCA_WCN6855 |
404 BTUSB_WIDEBAND_SPEECH },
a6587d7e
MD
405 { USB_DEVICE(0x2c7c, 0x0130), .driver_info = BTUSB_QCA_WCN6855 |
406 BTUSB_WIDEBAND_SPEECH },
2dd1c1ee
ZH
407 { USB_DEVICE(0x2c7c, 0x0131), .driver_info = BTUSB_QCA_WCN6855 |
408 BTUSB_WIDEBAND_SPEECH },
409 { USB_DEVICE(0x2c7c, 0x0132), .driver_info = BTUSB_QCA_WCN6855 |
410 BTUSB_WIDEBAND_SPEECH },
46225947 411
cfeb4145 412 /* Broadcom BCM2035 */
7a9d4020 413 { USB_DEVICE(0x0a5c, 0x2009), .driver_info = BTUSB_BCM92035 },
0b880062
AS
414 { USB_DEVICE(0x0a5c, 0x200a), .driver_info = BTUSB_WRONG_SCO_MTU },
415 { USB_DEVICE(0x0a5c, 0x2035), .driver_info = BTUSB_WRONG_SCO_MTU },
cfeb4145
MH
416
417 /* Broadcom BCM2045 */
7a9d4020
MH
418 { USB_DEVICE(0x0a5c, 0x2039), .driver_info = BTUSB_WRONG_SCO_MTU },
419 { USB_DEVICE(0x0a5c, 0x2101), .driver_info = BTUSB_WRONG_SCO_MTU },
bdbef3d6 420
cfeb4145 421 /* IBM/Lenovo ThinkPad with Broadcom chip */
7a9d4020
MH
422 { USB_DEVICE(0x0a5c, 0x201e), .driver_info = BTUSB_WRONG_SCO_MTU },
423 { USB_DEVICE(0x0a5c, 0x2110), .driver_info = BTUSB_WRONG_SCO_MTU },
cfeb4145
MH
424
425 /* HP laptop with Broadcom chip */
7a9d4020 426 { USB_DEVICE(0x03f0, 0x171d), .driver_info = BTUSB_WRONG_SCO_MTU },
cfeb4145
MH
427
428 /* Dell laptop with Broadcom chip */
7a9d4020 429 { USB_DEVICE(0x413c, 0x8126), .driver_info = BTUSB_WRONG_SCO_MTU },
cfeb4145 430
5ddd4a60 431 /* Dell Wireless 370 and 410 devices */
7a9d4020 432 { USB_DEVICE(0x413c, 0x8152), .driver_info = BTUSB_WRONG_SCO_MTU },
5ddd4a60 433 { USB_DEVICE(0x413c, 0x8156), .driver_info = BTUSB_WRONG_SCO_MTU },
cfeb4145 434
7a9d4020
MH
435 /* Belkin F8T012 and F8T013 devices */
436 { USB_DEVICE(0x050d, 0x0012), .driver_info = BTUSB_WRONG_SCO_MTU },
437 { USB_DEVICE(0x050d, 0x0013), .driver_info = BTUSB_WRONG_SCO_MTU },
cfeb4145 438
5ddd4a60
MH
439 /* Asus WL-BTD202 device */
440 { USB_DEVICE(0x0b05, 0x1715), .driver_info = BTUSB_WRONG_SCO_MTU },
441
442 /* Kensington Bluetooth USB adapter */
443 { USB_DEVICE(0x047d, 0x105e), .driver_info = BTUSB_WRONG_SCO_MTU },
444
cfeb4145
MH
445 /* RTX Telecom based adapters with buggy SCO support */
446 { USB_DEVICE(0x0400, 0x0807), .driver_info = BTUSB_BROKEN_ISOC },
447 { USB_DEVICE(0x0400, 0x080a), .driver_info = BTUSB_BROKEN_ISOC },
448
449 /* CONWISE Technology based adapters with buggy SCO support */
418678b0
SJ
450 { USB_DEVICE(0x0e5e, 0x6622),
451 .driver_info = BTUSB_BROKEN_ISOC | BTUSB_CW6622},
cfeb4145 452
4fcef8ed 453 /* Roper Class 1 Bluetooth Dongle (Silicon Wave based) */
2eeac871 454 { USB_DEVICE(0x1310, 0x0001), .driver_info = BTUSB_SWAVE },
4fcef8ed 455
cfeb4145
MH
456 /* Digianswer devices */
457 { USB_DEVICE(0x08fd, 0x0001), .driver_info = BTUSB_DIGIANSWER },
458 { USB_DEVICE(0x08fd, 0x0002), .driver_info = BTUSB_IGNORE },
459
460 /* CSR BlueCore Bluetooth Sniffer */
4f64fa80
MH
461 { USB_DEVICE(0x0a12, 0x0002),
462 .driver_info = BTUSB_SNIFFER | BTUSB_BROKEN_ISOC },
cfeb4145
MH
463
464 /* Frontline ComProbe Bluetooth Sniffer */
4f64fa80
MH
465 { USB_DEVICE(0x16d3, 0x0002),
466 .driver_info = BTUSB_SNIFFER | BTUSB_BROKEN_ISOC },
cfeb4145 467
cb1ee89f
MH
468 /* Marvell Bluetooth devices */
469 { USB_DEVICE(0x1286, 0x2044), .driver_info = BTUSB_MARVELL },
470 { USB_DEVICE(0x1286, 0x2046), .driver_info = BTUSB_MARVELL },
1165df0e 471 { USB_DEVICE(0x1286, 0x204e), .driver_info = BTUSB_MARVELL },
cb1ee89f 472
d0ac9eb7 473 /* Intel Bluetooth devices */
3df4dfbe
THJA
474 { USB_DEVICE(0x8087, 0x0025), .driver_info = BTUSB_INTEL_COMBINED },
475 { USB_DEVICE(0x8087, 0x0026), .driver_info = BTUSB_INTEL_COMBINED },
476 { USB_DEVICE(0x8087, 0x0029), .driver_info = BTUSB_INTEL_COMBINED },
477 { USB_DEVICE(0x8087, 0x0032), .driver_info = BTUSB_INTEL_COMBINED },
478 { USB_DEVICE(0x8087, 0x0033), .driver_info = BTUSB_INTEL_COMBINED },
2ca57c80 479 { USB_DEVICE(0x8087, 0x0035), .driver_info = BTUSB_INTEL_COMBINED },
999a8a6b 480 { USB_DEVICE(0x8087, 0x0036), .driver_info = BTUSB_INTEL_COMBINED },
5c9f6a78 481 { USB_DEVICE(0x8087, 0x0037), .driver_info = BTUSB_INTEL_COMBINED },
a97258db 482 { USB_DEVICE(0x8087, 0x0038), .driver_info = BTUSB_INTEL_COMBINED },
f6dc9214 483 { USB_DEVICE(0x8087, 0x0039), .driver_info = BTUSB_INTEL_COMBINED },
407550fe 484 { USB_DEVICE(0x8087, 0x07da), .driver_info = BTUSB_CSR },
ea7c4c0e 485 { USB_DEVICE(0x8087, 0x07dc), .driver_info = BTUSB_INTEL_COMBINED |
55235304 486 BTUSB_INTEL_NO_WBS_SUPPORT |
95655456
THJA
487 BTUSB_INTEL_BROKEN_INITIAL_NCMD |
488 BTUSB_INTEL_BROKEN_SHUTDOWN_LED },
489 { USB_DEVICE(0x8087, 0x0a2a), .driver_info = BTUSB_INTEL_COMBINED |
55235304 490 BTUSB_INTEL_NO_WBS_SUPPORT |
95655456 491 BTUSB_INTEL_BROKEN_SHUTDOWN_LED },
3df4dfbe 492 { USB_DEVICE(0x8087, 0x0a2b), .driver_info = BTUSB_INTEL_COMBINED },
95655456
THJA
493 { USB_DEVICE(0x8087, 0x0aa7), .driver_info = BTUSB_INTEL_COMBINED |
494 BTUSB_INTEL_BROKEN_SHUTDOWN_LED },
3df4dfbe 495 { USB_DEVICE(0x8087, 0x0aaa), .driver_info = BTUSB_INTEL_COMBINED },
dffd30ee 496
d0ac9eb7
MH
497 /* Other Intel Bluetooth devices */
498 { USB_VENDOR_AND_INTERFACE_INFO(0x8087, 0xe0, 0x01, 0x01),
499 .driver_info = BTUSB_IGNORE },
ae8df494 500
1eec3b95
MC
501 /* Realtek 8821CE Bluetooth devices */
502 { USB_DEVICE(0x13d3, 0x3529), .driver_info = BTUSB_REALTEK |
503 BTUSB_WIDEBAND_SPEECH },
504
33bfd94a
JH
505 /* Realtek 8822CE Bluetooth devices */
506 { USB_DEVICE(0x0bda, 0xb00c), .driver_info = BTUSB_REALTEK |
4d96d3b0
LF
507 BTUSB_WIDEBAND_SPEECH },
508 { USB_DEVICE(0x0bda, 0xc822), .driver_info = BTUSB_REALTEK |
33bfd94a
JH
509 BTUSB_WIDEBAND_SPEECH },
510
6d0a4fe2
GL
511 /* Realtek 8822CU Bluetooth devices */
512 { USB_DEVICE(0x13d3, 0x3549), .driver_info = BTUSB_REALTEK |
513 BTUSB_WIDEBAND_SPEECH },
514
181a2631 515 /* Realtek 8851BE Bluetooth devices */
c4dbb1bd 516 { USB_DEVICE(0x0bda, 0xb850), .driver_info = BTUSB_REALTEK },
181a2631
GW
517 { USB_DEVICE(0x13d3, 0x3600), .driver_info = BTUSB_REALTEK },
518
0d484db6 519 /* Realtek 8852AE Bluetooth devices */
2e7b4a32
LF
520 { USB_DEVICE(0x0bda, 0x2852), .driver_info = BTUSB_REALTEK |
521 BTUSB_WIDEBAND_SPEECH },
0d484db6
MC
522 { USB_DEVICE(0x0bda, 0xc852), .driver_info = BTUSB_REALTEK |
523 BTUSB_WIDEBAND_SPEECH },
27fe097b
LF
524 { USB_DEVICE(0x0bda, 0x385a), .driver_info = BTUSB_REALTEK |
525 BTUSB_WIDEBAND_SPEECH },
d1dcbf61
LF
526 { USB_DEVICE(0x0bda, 0x4852), .driver_info = BTUSB_REALTEK |
527 BTUSB_WIDEBAND_SPEECH },
9bba1286
MC
528 { USB_DEVICE(0x04c5, 0x165c), .driver_info = BTUSB_REALTEK |
529 BTUSB_WIDEBAND_SPEECH },
d1dcbf61
LF
530 { USB_DEVICE(0x04ca, 0x4006), .driver_info = BTUSB_REALTEK |
531 BTUSB_WIDEBAND_SPEECH },
a0476f6a
LF
532 { USB_DEVICE(0x0cb8, 0xc549), .driver_info = BTUSB_REALTEK |
533 BTUSB_WIDEBAND_SPEECH },
0d484db6 534
c379c96c
HW
535 /* Realtek 8852CE Bluetooth devices */
536 { USB_DEVICE(0x04ca, 0x4007), .driver_info = BTUSB_REALTEK |
537 BTUSB_WIDEBAND_SPEECH },
893fa8bc
HW
538 { USB_DEVICE(0x04c5, 0x1675), .driver_info = BTUSB_REALTEK |
539 BTUSB_WIDEBAND_SPEECH },
5b75ee37
HW
540 { USB_DEVICE(0x0cb8, 0xc558), .driver_info = BTUSB_REALTEK |
541 BTUSB_WIDEBAND_SPEECH },
8f0054dd
HW
542 { USB_DEVICE(0x13d3, 0x3587), .driver_info = BTUSB_REALTEK |
543 BTUSB_WIDEBAND_SPEECH },
6ad353df
HW
544 { USB_DEVICE(0x13d3, 0x3586), .driver_info = BTUSB_REALTEK |
545 BTUSB_WIDEBAND_SPEECH },
c4ba5800
MC
546 { USB_DEVICE(0x13d3, 0x3592), .driver_info = BTUSB_REALTEK |
547 BTUSB_WIDEBAND_SPEECH },
bdf9557f
HW
548 { USB_DEVICE(0x0489, 0xe122), .driver_info = BTUSB_REALTEK |
549 BTUSB_WIDEBAND_SPEECH },
c379c96c 550
393b4916
AL
551 /* Realtek 8852BE Bluetooth devices */
552 { USB_DEVICE(0x0cb8, 0xc559), .driver_info = BTUSB_REALTEK |
553 BTUSB_WIDEBAND_SPEECH },
d1a5a7ee
W
554 { USB_DEVICE(0x0bda, 0x4853), .driver_info = BTUSB_REALTEK |
555 BTUSB_WIDEBAND_SPEECH },
730a1d1a
LF
556 { USB_DEVICE(0x0bda, 0x887b), .driver_info = BTUSB_REALTEK |
557 BTUSB_WIDEBAND_SPEECH },
da06ff1f
GW
558 { USB_DEVICE(0x0bda, 0xb85b), .driver_info = BTUSB_REALTEK |
559 BTUSB_WIDEBAND_SPEECH },
02be109d
MR
560 { USB_DEVICE(0x13d3, 0x3570), .driver_info = BTUSB_REALTEK |
561 BTUSB_WIDEBAND_SPEECH },
069f5342
LF
562 { USB_DEVICE(0x13d3, 0x3571), .driver_info = BTUSB_REALTEK |
563 BTUSB_WIDEBAND_SPEECH },
3600860a
JT
564 { USB_DEVICE(0x13d3, 0x3572), .driver_info = BTUSB_REALTEK |
565 BTUSB_WIDEBAND_SPEECH },
473a89b4
W
566 { USB_DEVICE(0x13d3, 0x3591), .driver_info = BTUSB_REALTEK |
567 BTUSB_WIDEBAND_SPEECH },
69b84ffc
HW
568 { USB_DEVICE(0x0489, 0xe123), .driver_info = BTUSB_REALTEK |
569 BTUSB_WIDEBAND_SPEECH },
295ef07a
HW
570 { USB_DEVICE(0x0489, 0xe125), .driver_info = BTUSB_REALTEK |
571 BTUSB_WIDEBAND_SPEECH },
393b4916 572
1fb99431
MC
573 /* Realtek 8852BT/8852BE-VT Bluetooth devices */
574 { USB_DEVICE(0x0bda, 0x8520), .driver_info = BTUSB_REALTEK |
575 BTUSB_WIDEBAND_SPEECH },
a30ce299
MC
576
577 /* Realtek 8922AE Bluetooth devices */
578 { USB_DEVICE(0x0bda, 0x8922), .driver_info = BTUSB_REALTEK |
579 BTUSB_WIDEBAND_SPEECH },
580 { USB_DEVICE(0x13d3, 0x3617), .driver_info = BTUSB_REALTEK |
581 BTUSB_WIDEBAND_SPEECH },
582 { USB_DEVICE(0x13d3, 0x3616), .driver_info = BTUSB_REALTEK |
583 BTUSB_WIDEBAND_SPEECH },
584 { USB_DEVICE(0x0489, 0xe130), .driver_info = BTUSB_REALTEK |
585 BTUSB_WIDEBAND_SPEECH },
586
a2698a9b
DD
587 /* Realtek Bluetooth devices */
588 { USB_VENDOR_AND_INTERFACE_INFO(0x0bda, 0xe0, 0x01, 0x01),
589 .driver_info = BTUSB_REALTEK },
590
a1c49c43
SW
591 /* MediaTek Bluetooth devices */
592 { USB_VENDOR_AND_INTERFACE_INFO(0x0e8d, 0xe0, 0x01, 0x01),
27e554a4 593 .driver_info = BTUSB_MEDIATEK |
0fec656d 594 BTUSB_WIDEBAND_SPEECH },
a1c49c43 595
3a567b95
CC
596 /* Additional MediaTek MT7615E Bluetooth devices */
597 { USB_DEVICE(0x13d3, 0x3560), .driver_info = BTUSB_MEDIATEK},
598
467e98cd
PD
599 /* Additional MediaTek MT7663 Bluetooth devices */
600 { USB_DEVICE(0x043e, 0x310c), .driver_info = BTUSB_MEDIATEK |
0fec656d 601 BTUSB_WIDEBAND_SPEECH },
13209415 602 { USB_DEVICE(0x04ca, 0x3801), .driver_info = BTUSB_MEDIATEK |
0fec656d 603 BTUSB_WIDEBAND_SPEECH },
467e98cd 604
37356827
FC
605 /* Additional MediaTek MT7668 Bluetooth devices */
606 { USB_DEVICE(0x043e, 0x3109), .driver_info = BTUSB_MEDIATEK |
0fec656d 607 BTUSB_WIDEBAND_SPEECH },
37356827 608
a94bc93a
JL
609 /* Additional MediaTek MT7920 Bluetooth devices */
610 { USB_DEVICE(0x0489, 0xe134), .driver_info = BTUSB_MEDIATEK |
611 BTUSB_WIDEBAND_SPEECH },
612 { USB_DEVICE(0x13d3, 0x3620), .driver_info = BTUSB_MEDIATEK |
613 BTUSB_WIDEBAND_SPEECH },
614 { USB_DEVICE(0x13d3, 0x3621), .driver_info = BTUSB_MEDIATEK |
615 BTUSB_WIDEBAND_SPEECH },
616 { USB_DEVICE(0x13d3, 0x3622), .driver_info = BTUSB_MEDIATEK |
617 BTUSB_WIDEBAND_SPEECH },
618
393dc5d1 619 /* Additional MediaTek MT7921 Bluetooth devices */
23fcb27b 620 { USB_DEVICE(0x0489, 0xe0c8), .driver_info = BTUSB_MEDIATEK |
0fec656d 621 BTUSB_WIDEBAND_SPEECH },
3487cda2 622 { USB_DEVICE(0x0489, 0xe0cd), .driver_info = BTUSB_MEDIATEK |
0fec656d 623 BTUSB_WIDEBAND_SPEECH },
7ac7267f 624 { USB_DEVICE(0x0489, 0xe0e0), .driver_info = BTUSB_MEDIATEK |
0fec656d 625 BTUSB_WIDEBAND_SPEECH },
83458a5f 626 { USB_DEVICE(0x0489, 0xe0f2), .driver_info = BTUSB_MEDIATEK |
0fec656d 627 BTUSB_WIDEBAND_SPEECH },
393dc5d1 628 { USB_DEVICE(0x04ca, 0x3802), .driver_info = BTUSB_MEDIATEK |
0fec656d 629 BTUSB_WIDEBAND_SPEECH },
3487cda2 630 { USB_DEVICE(0x0e8d, 0x0608), .driver_info = BTUSB_MEDIATEK |
0fec656d 631 BTUSB_WIDEBAND_SPEECH },
2cdff8ca 632 { USB_DEVICE(0x13d3, 0x3563), .driver_info = BTUSB_MEDIATEK |
0fec656d 633 BTUSB_WIDEBAND_SPEECH },
15957cab 634 { USB_DEVICE(0x13d3, 0x3564), .driver_info = BTUSB_MEDIATEK |
0fec656d 635 BTUSB_WIDEBAND_SPEECH },
c5e25bb5 636 { USB_DEVICE(0x13d3, 0x3567), .driver_info = BTUSB_MEDIATEK |
0fec656d 637 BTUSB_WIDEBAND_SPEECH },
4259cdbf
GW
638 { USB_DEVICE(0x13d3, 0x3576), .driver_info = BTUSB_MEDIATEK |
639 BTUSB_WIDEBAND_SPEECH },
a614ee94 640 { USB_DEVICE(0x13d3, 0x3578), .driver_info = BTUSB_MEDIATEK |
0fec656d 641 BTUSB_WIDEBAND_SPEECH },
f0ad26ee 642 { USB_DEVICE(0x13d3, 0x3583), .driver_info = BTUSB_MEDIATEK |
0fec656d 643 BTUSB_WIDEBAND_SPEECH },
129d3292 644 { USB_DEVICE(0x13d3, 0x3606), .driver_info = BTUSB_MEDIATEK |
0fec656d 645 BTUSB_WIDEBAND_SPEECH },
393dc5d1 646
8c0401b7
IM
647 /* MediaTek MT7922 Bluetooth devices */
648 { USB_DEVICE(0x13d3, 0x3585), .driver_info = BTUSB_MEDIATEK |
0fec656d 649 BTUSB_WIDEBAND_SPEECH },
45e7d389
AH
650 { USB_DEVICE(0x13d3, 0x3610), .driver_info = BTUSB_MEDIATEK |
651 BTUSB_WIDEBAND_SPEECH },
8c0401b7 652
69326274
AM
653 /* MediaTek MT7922A Bluetooth devices */
654 { USB_DEVICE(0x0489, 0xe0d8), .driver_info = BTUSB_MEDIATEK |
0fec656d 655 BTUSB_WIDEBAND_SPEECH },
69326274 656 { USB_DEVICE(0x0489, 0xe0d9), .driver_info = BTUSB_MEDIATEK |
0fec656d 657 BTUSB_WIDEBAND_SPEECH },
57117d72 658 { USB_DEVICE(0x0489, 0xe0e2), .driver_info = BTUSB_MEDIATEK |
0fec656d 659 BTUSB_WIDEBAND_SPEECH },
125ede3a 660 { USB_DEVICE(0x0489, 0xe0e4), .driver_info = BTUSB_MEDIATEK |
0fec656d 661 BTUSB_WIDEBAND_SPEECH },
0cefdaed 662 { USB_DEVICE(0x0489, 0xe0f1), .driver_info = BTUSB_MEDIATEK |
0fec656d 663 BTUSB_WIDEBAND_SPEECH },
13fcc94d 664 { USB_DEVICE(0x0489, 0xe0f2), .driver_info = BTUSB_MEDIATEK |
0fec656d 665 BTUSB_WIDEBAND_SPEECH },
e160a8f4 666 { USB_DEVICE(0x0489, 0xe0f5), .driver_info = BTUSB_MEDIATEK |
0fec656d 667 BTUSB_WIDEBAND_SPEECH },
b6cfa1c2 668 { USB_DEVICE(0x0489, 0xe0f6), .driver_info = BTUSB_MEDIATEK |
0fec656d 669 BTUSB_WIDEBAND_SPEECH },
bf809efd 670 { USB_DEVICE(0x0489, 0xe102), .driver_info = BTUSB_MEDIATEK |
0fec656d 671 BTUSB_WIDEBAND_SPEECH },
a88643b7
JL
672 { USB_DEVICE(0x0489, 0xe152), .driver_info = BTUSB_MEDIATEK |
673 BTUSB_WIDEBAND_SPEECH },
674 { USB_DEVICE(0x0489, 0xe153), .driver_info = BTUSB_MEDIATEK |
675 BTUSB_WIDEBAND_SPEECH },
59be4be8 676 { USB_DEVICE(0x04ca, 0x3804), .driver_info = BTUSB_MEDIATEK |
0fec656d 677 BTUSB_WIDEBAND_SPEECH },
3487cda2 678 { USB_DEVICE(0x04ca, 0x38e4), .driver_info = BTUSB_MEDIATEK |
0fec656d 679 BTUSB_WIDEBAND_SPEECH },
3487cda2 680 { USB_DEVICE(0x13d3, 0x3568), .driver_info = BTUSB_MEDIATEK |
0fec656d 681 BTUSB_WIDEBAND_SPEECH },
71d9d352
LS
682 { USB_DEVICE(0x13d3, 0x3584), .driver_info = BTUSB_MEDIATEK |
683 BTUSB_WIDEBAND_SPEECH },
3487cda2 684 { USB_DEVICE(0x13d3, 0x3605), .driver_info = BTUSB_MEDIATEK |
0fec656d 685 BTUSB_WIDEBAND_SPEECH },
3487cda2 686 { USB_DEVICE(0x13d3, 0x3607), .driver_info = BTUSB_MEDIATEK |
0fec656d 687 BTUSB_WIDEBAND_SPEECH },
3487cda2 688 { USB_DEVICE(0x13d3, 0x3614), .driver_info = BTUSB_MEDIATEK |
0fec656d 689 BTUSB_WIDEBAND_SPEECH },
3487cda2 690 { USB_DEVICE(0x13d3, 0x3615), .driver_info = BTUSB_MEDIATEK |
0fec656d 691 BTUSB_WIDEBAND_SPEECH },
3487cda2 692 { USB_DEVICE(0x35f5, 0x7922), .driver_info = BTUSB_MEDIATEK |
0fec656d 693 BTUSB_WIDEBAND_SPEECH },
560ff4bc
US
694
695 /* Additional MediaTek MT7925 Bluetooth devices */
faa5fd60
HQ
696 { USB_DEVICE(0x0489, 0xe111), .driver_info = BTUSB_MEDIATEK |
697 BTUSB_WIDEBAND_SPEECH },
129d3292 698 { USB_DEVICE(0x0489, 0xe113), .driver_info = BTUSB_MEDIATEK |
0fec656d 699 BTUSB_WIDEBAND_SPEECH },
39e4bfb4
MB
700 { USB_DEVICE(0x0489, 0xe118), .driver_info = BTUSB_MEDIATEK |
701 BTUSB_WIDEBAND_SPEECH },
702 { USB_DEVICE(0x0489, 0xe11e), .driver_info = BTUSB_MEDIATEK |
703 BTUSB_WIDEBAND_SPEECH },
679cb60f
JM
704 { USB_DEVICE(0x0489, 0xe124), .driver_info = BTUSB_MEDIATEK |
705 BTUSB_WIDEBAND_SPEECH },
a94bc93a
JL
706 { USB_DEVICE(0x0489, 0xe139), .driver_info = BTUSB_MEDIATEK |
707 BTUSB_WIDEBAND_SPEECH },
de7dcf9d
JL
708 { USB_DEVICE(0x0489, 0xe14f), .driver_info = BTUSB_MEDIATEK |
709 BTUSB_WIDEBAND_SPEECH },
710 { USB_DEVICE(0x0489, 0xe150), .driver_info = BTUSB_MEDIATEK |
711 BTUSB_WIDEBAND_SPEECH },
712 { USB_DEVICE(0x0489, 0xe151), .driver_info = BTUSB_MEDIATEK |
713 BTUSB_WIDEBAND_SPEECH },
560ff4bc 714 { USB_DEVICE(0x13d3, 0x3602), .driver_info = BTUSB_MEDIATEK |
0fec656d 715 BTUSB_WIDEBAND_SPEECH },
129d3292 716 { USB_DEVICE(0x13d3, 0x3603), .driver_info = BTUSB_MEDIATEK |
0fec656d 717 BTUSB_WIDEBAND_SPEECH },
f9685f31 718 { USB_DEVICE(0x13d3, 0x3604), .driver_info = BTUSB_MEDIATEK |
0fec656d 719 BTUSB_WIDEBAND_SPEECH },
cb45396f
PN
720 { USB_DEVICE(0x13d3, 0x3608), .driver_info = BTUSB_MEDIATEK |
721 BTUSB_WIDEBAND_SPEECH },
bbf56029
YM
722 { USB_DEVICE(0x13d3, 0x3613), .driver_info = BTUSB_MEDIATEK |
723 BTUSB_WIDEBAND_SPEECH },
f63f4011
EWW
724 { USB_DEVICE(0x13d3, 0x3628), .driver_info = BTUSB_MEDIATEK |
725 BTUSB_WIDEBAND_SPEECH },
5bd5c716
JL
726 { USB_DEVICE(0x13d3, 0x3630), .driver_info = BTUSB_MEDIATEK |
727 BTUSB_WIDEBAND_SPEECH },
69326274 728
a2698a9b
DD
729 /* Additional Realtek 8723AE Bluetooth devices */
730 { USB_DEVICE(0x0930, 0x021d), .driver_info = BTUSB_REALTEK },
731 { USB_DEVICE(0x13d3, 0x3394), .driver_info = BTUSB_REALTEK },
732
733 /* Additional Realtek 8723BE Bluetooth devices */
734 { USB_DEVICE(0x0489, 0xe085), .driver_info = BTUSB_REALTEK },
735 { USB_DEVICE(0x0489, 0xe08b), .driver_info = BTUSB_REALTEK },
cc68a041 736 { USB_DEVICE(0x04f2, 0xb49f), .driver_info = BTUSB_REALTEK },
a2698a9b
DD
737 { USB_DEVICE(0x13d3, 0x3410), .driver_info = BTUSB_REALTEK },
738 { USB_DEVICE(0x13d3, 0x3416), .driver_info = BTUSB_REALTEK },
739 { USB_DEVICE(0x13d3, 0x3459), .driver_info = BTUSB_REALTEK },
a81d72d2 740 { USB_DEVICE(0x13d3, 0x3494), .driver_info = BTUSB_REALTEK },
a2698a9b 741
a41e0796
VB
742 /* Additional Realtek 8723BU Bluetooth devices */
743 { USB_DEVICE(0x7392, 0xa611), .driver_info = BTUSB_REALTEK },
744
66d9975c 745 /* Additional Realtek 8723DE Bluetooth devices */
45ae68b8 746 { USB_DEVICE(0x0bda, 0xb009), .driver_info = BTUSB_REALTEK },
66d9975c
JHP
747 { USB_DEVICE(0x2ff8, 0xb011), .driver_info = BTUSB_REALTEK },
748
d8daa399 749 /* Additional Realtek 8761BUV Bluetooth devices */
4fd6d490
NF
750 { USB_DEVICE(0x2357, 0x0604), .driver_info = BTUSB_REALTEK |
751 BTUSB_WIDEBAND_SPEECH },
33404381
JT
752 { USB_DEVICE(0x0b05, 0x190e), .driver_info = BTUSB_REALTEK |
753 BTUSB_WIDEBAND_SPEECH },
6dfbe29f
HG
754 { USB_DEVICE(0x2550, 0x8761), .driver_info = BTUSB_REALTEK |
755 BTUSB_WIDEBAND_SPEECH },
c77a592b
IL
756 { USB_DEVICE(0x0bda, 0x8771), .driver_info = BTUSB_REALTEK |
757 BTUSB_WIDEBAND_SPEECH },
022b6101
DG
758 { USB_DEVICE(0x6655, 0x8771), .driver_info = BTUSB_REALTEK |
759 BTUSB_WIDEBAND_SPEECH },
c7577014
SS
760 { USB_DEVICE(0x7392, 0xc611), .driver_info = BTUSB_REALTEK |
761 BTUSB_WIDEBAND_SPEECH },
ac09bb3f
RV
762 { USB_DEVICE(0x2b89, 0x8761), .driver_info = BTUSB_REALTEK |
763 BTUSB_WIDEBAND_SPEECH },
c77a592b 764
a2698a9b
DD
765 /* Additional Realtek 8821AE Bluetooth devices */
766 { USB_DEVICE(0x0b05, 0x17dc), .driver_info = BTUSB_REALTEK },
767 { USB_DEVICE(0x13d3, 0x3414), .driver_info = BTUSB_REALTEK },
768 { USB_DEVICE(0x13d3, 0x3458), .driver_info = BTUSB_REALTEK },
769 { USB_DEVICE(0x13d3, 0x3461), .driver_info = BTUSB_REALTEK },
770 { USB_DEVICE(0x13d3, 0x3462), .driver_info = BTUSB_REALTEK },
771
fed03fe7 772 /* Additional Realtek 8822BE Bluetooth devices */
1cd2fabf 773 { USB_DEVICE(0x13d3, 0x3526), .driver_info = BTUSB_REALTEK },
fed03fe7
LF
774 { USB_DEVICE(0x0b05, 0x185c), .driver_info = BTUSB_REALTEK },
775
6d0762b1 776 /* Additional Realtek 8822CE Bluetooth devices */
73280f13
MC
777 { USB_DEVICE(0x04ca, 0x4005), .driver_info = BTUSB_REALTEK |
778 BTUSB_WIDEBAND_SPEECH },
779 { USB_DEVICE(0x04c5, 0x161f), .driver_info = BTUSB_REALTEK |
780 BTUSB_WIDEBAND_SPEECH },
781 { USB_DEVICE(0x0b05, 0x18ef), .driver_info = BTUSB_REALTEK |
782 BTUSB_WIDEBAND_SPEECH },
783 { USB_DEVICE(0x13d3, 0x3548), .driver_info = BTUSB_REALTEK |
784 BTUSB_WIDEBAND_SPEECH },
785 { USB_DEVICE(0x13d3, 0x3549), .driver_info = BTUSB_REALTEK |
786 BTUSB_WIDEBAND_SPEECH },
787 { USB_DEVICE(0x13d3, 0x3553), .driver_info = BTUSB_REALTEK |
788 BTUSB_WIDEBAND_SPEECH },
789 { USB_DEVICE(0x13d3, 0x3555), .driver_info = BTUSB_REALTEK |
790 BTUSB_WIDEBAND_SPEECH },
791 { USB_DEVICE(0x2ff8, 0x3051), .driver_info = BTUSB_REALTEK |
792 BTUSB_WIDEBAND_SPEECH },
f496297e
JP
793 { USB_DEVICE(0x1358, 0xc123), .driver_info = BTUSB_REALTEK |
794 BTUSB_WIDEBAND_SPEECH },
24d6a6d2
CW
795 { USB_DEVICE(0x0bda, 0xc123), .driver_info = BTUSB_REALTEK |
796 BTUSB_WIDEBAND_SPEECH },
3edc5782
RM
797 { USB_DEVICE(0x0cb5, 0xc547), .driver_info = BTUSB_REALTEK |
798 BTUSB_WIDEBAND_SPEECH },
6d0762b1 799
5d44f76f
MH
800 /* Actions Semiconductor ATS2851 based devices */
801 { USB_DEVICE(0x10d7, 0xb012), .driver_info = BTUSB_ACTIONS_SEMI },
802
4481c076
PP
803 /* Silicon Wave based devices */
804 { USB_DEVICE(0x0c10, 0x0000), .driver_info = BTUSB_SWAVE },
805
5e23b923
MH
806 { } /* Terminating entry */
807};
808
1fdb9269
HG
809/* The Bluetooth USB module build into some devices needs to be reset on resume,
810 * this is a problem with the platform (likely shutting off all power) not with
811 * the module itself. So we use a DMI list to match known broken platforms.
812 */
813static const struct dmi_system_id btusb_needs_reset_resume_table[] = {
814 {
0c6e5266 815 /* Dell OptiPlex 3060 (QCA ROME device 0cf3:e007) */
1fdb9269 816 .matches = {
0c6e5266
KHF
817 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
818 DMI_MATCH(DMI_PRODUCT_NAME, "OptiPlex 3060"),
1fdb9269
HG
819 },
820 },
596b07a9
HG
821 {
822 /* Dell XPS 9360 (QCA ROME device 0cf3:e300) */
823 .matches = {
824 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
825 DMI_MATCH(DMI_PRODUCT_NAME, "XPS 13 9360"),
826 },
827 },
939bc6c5
HG
828 {
829 /* Dell Inspiron 5565 (QCA ROME device 0cf3:e009) */
830 .matches = {
831 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
832 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 5565"),
833 },
834 },
1fdb9269
HG
835 {}
836};
837
20981ce2
TJ
838struct qca_dump_info {
839 /* fields for dump collection */
840 u16 id_vendor;
841 u16 id_product;
842 u32 fw_version;
843 u32 controller_id;
844 u32 ram_dump_size;
845 u16 ram_dump_seqno;
846};
847
9bfa35fe
MH
848#define BTUSB_MAX_ISOC_FRAMES 10
849
5e23b923
MH
850#define BTUSB_INTR_RUNNING 0
851#define BTUSB_BULK_RUNNING 1
9bfa35fe 852#define BTUSB_ISOC_RUNNING 2
7bee549e 853#define BTUSB_SUSPENDING 3
08b8b6c4 854#define BTUSB_DID_ISO_RESUME 4
cda0dd78
MH
855#define BTUSB_BOOTLOADER 5
856#define BTUSB_DOWNLOADING 6
ce6bb929 857#define BTUSB_FIRMWARE_LOADED 7
cda0dd78 858#define BTUSB_FIRMWARE_FAILED 8
ce6bb929 859#define BTUSB_BOOTING 9
61f5acea
HG
860#define BTUSB_DIAG_RUNNING 10
861#define BTUSB_OOB_WAKE_ENABLED 11
dc786b2c 862#define BTUSB_HW_RESET_ACTIVE 12
a1c49c43 863#define BTUSB_TX_WAIT_VND_EVT 13
8274db07 864#define BTUSB_WAKEUP_AUTOSUSPEND 14
55981d35 865#define BTUSB_USE_ALT3_FOR_WBS 15
a479e713 866#define BTUSB_ALT6_CONTINUOUS_TX 16
20981ce2 867#define BTUSB_HW_SSR_ACTIVE 17
5e23b923
MH
868
869struct btusb_data {
870 struct hci_dev *hdev;
871 struct usb_device *udev;
5fbcd260 872 struct usb_interface *intf;
9bfa35fe 873 struct usb_interface *isoc;
9d08f504 874 struct usb_interface *diag;
459232fc 875 unsigned isoc_ifnum;
5e23b923 876
5e23b923
MH
877 unsigned long flags;
878
800fe5ec
LAD
879 bool poll_sync;
880 int intr_interval;
881 struct work_struct work;
882 struct work_struct waker;
883 struct delayed_work rx_work;
884
885 struct sk_buff_head acl_q;
5e23b923 886
803b5836 887 struct usb_anchor deferred;
5e23b923 888 struct usb_anchor tx_anchor;
803b5836
MH
889 int tx_in_flight;
890 spinlock_t txlock;
891
5e23b923
MH
892 struct usb_anchor intr_anchor;
893 struct usb_anchor bulk_anchor;
9bfa35fe 894 struct usb_anchor isoc_anchor;
9d08f504 895 struct usb_anchor diag_anchor;
a1c49c43 896 struct usb_anchor ctrl_anchor;
803b5836
MH
897 spinlock_t rxlock;
898
899 struct sk_buff *evt_skb;
900 struct sk_buff *acl_skb;
901 struct sk_buff *sco_skb;
5e23b923
MH
902
903 struct usb_endpoint_descriptor *intr_ep;
904 struct usb_endpoint_descriptor *bulk_tx_ep;
905 struct usb_endpoint_descriptor *bulk_rx_ep;
9bfa35fe
MH
906 struct usb_endpoint_descriptor *isoc_tx_ep;
907 struct usb_endpoint_descriptor *isoc_rx_ep;
9d08f504
MH
908 struct usb_endpoint_descriptor *diag_tx_ep;
909 struct usb_endpoint_descriptor *diag_rx_ep;
9bfa35fe 910
dc786b2c
RJ
911 struct gpio_desc *reset_gpio;
912
7a9d4020 913 __u8 cmdreq_type;
893ba544 914 __u8 cmdreq;
7a9d4020 915
43c2e57f 916 unsigned int sco_num;
baac6276
SN
917 unsigned int air_mode;
918 bool usb_alt6_packet_flow;
9bfa35fe 919 int isoc_altsetting;
6a88adf2 920 int suspend_count;
2cbd3f5c 921
97307f51 922 int (*recv_event)(struct hci_dev *hdev, struct sk_buff *skb);
0b10c8c8 923 int (*recv_acl)(struct hci_dev *hdev, struct sk_buff *skb);
2cbd3f5c 924 int (*recv_bulk)(struct btusb_data *data, void *buffer, int count);
ace31982
KBYT
925
926 int (*setup_on_usb)(struct hci_dev *hdev);
fd913ef7 927
95f92928
CL
928 int (*suspend)(struct hci_dev *hdev);
929 int (*resume)(struct hci_dev *hdev);
cea1805f 930 int (*disconnect)(struct hci_dev *hdev);
95f92928 931
fd913ef7 932 int oob_wake_irq; /* irq for out-of-band wake-on-bt */
20981ce2
TJ
933
934 struct qca_dump_info qca_dump;
5e23b923
MH
935};
936
c9209b26
AP
937static void btusb_reset(struct hci_dev *hdev)
938{
939 struct btusb_data *data;
940 int err;
941
c9209b26
AP
942 data = hci_get_drvdata(hdev);
943 /* This is not an unbalanced PM reference since the device will reset */
944 err = usb_autopm_get_interface(data->intf);
945 if (err) {
946 bt_dev_err(hdev, "Failed usb_autopm_get_interface: %d", err);
947 return;
948 }
949
950 bt_dev_err(hdev, "Resetting usb device.");
951 usb_queue_reset_device(data->intf);
952}
953
f07d4780 954static void btusb_intel_reset(struct hci_dev *hdev)
dc786b2c
RJ
955{
956 struct btusb_data *data = hci_get_drvdata(hdev);
957 struct gpio_desc *reset_gpio = data->reset_gpio;
8f0a3786 958 struct btintel_data *intel_data = hci_get_priv(hdev);
dc786b2c 959
8f0a3786
K
960 if (intel_data->acpi_reset_method) {
961 if (test_and_set_bit(INTEL_ACPI_RESET_ACTIVE, intel_data->flags)) {
962 bt_dev_err(hdev, "acpi: last reset failed ? Not resetting again");
963 return;
964 }
965
966 bt_dev_err(hdev, "Initiating acpi reset method");
967 /* If ACPI reset method fails, lets try with legacy GPIO
968 * toggling
969 */
970 if (!intel_data->acpi_reset_method(hdev)) {
971 return;
972 }
973 }
974
dc786b2c 975 if (!reset_gpio) {
c9209b26 976 btusb_reset(hdev);
dc786b2c
RJ
977 return;
978 }
979
980 /*
981 * Toggle the hard reset line if the platform provides one. The reset
982 * is going to yank the device off the USB and then replug. So doing
983 * once is enough. The cleanup is handled correctly on the way out
984 * (standard USB disconnect), and the new device is detected cleanly
985 * and bound to the driver again like it should be.
986 */
987 if (test_and_set_bit(BTUSB_HW_RESET_ACTIVE, &data->flags)) {
988 bt_dev_err(hdev, "last reset failed? Not resetting again");
989 return;
990 }
991
992 bt_dev_err(hdev, "Initiating HW reset via gpio");
2de66bb8
RJ
993 gpiod_set_value_cansleep(reset_gpio, 1);
994 msleep(100);
995 gpiod_set_value_cansleep(reset_gpio, 0);
dc786b2c
RJ
996}
997
044014ce
HW
998#define RTK_DEVCOREDUMP_CODE_MEMDUMP 0x01
999#define RTK_DEVCOREDUMP_CODE_HW_ERR 0x02
1000#define RTK_DEVCOREDUMP_CODE_CMD_TIMEOUT 0x03
1001
1002#define RTK_SUB_EVENT_CODE_COREDUMP 0x34
1003
1004struct rtk_dev_coredump_hdr {
1005 u8 type;
1006 u8 code;
1007 u8 reserved[2];
1008} __packed;
1009
1010static inline void btusb_rtl_alloc_devcoredump(struct hci_dev *hdev,
1011 struct rtk_dev_coredump_hdr *hdr, u8 *buf, u32 len)
1012{
1013 struct sk_buff *skb;
1014
1015 skb = alloc_skb(len + sizeof(*hdr), GFP_ATOMIC);
1016 if (!skb)
1017 return;
1018
1019 skb_put_data(skb, hdr, sizeof(*hdr));
1020 if (len)
1021 skb_put_data(skb, buf, len);
1022
1023 if (!hci_devcd_init(hdev, skb->len)) {
1024 hci_devcd_append(hdev, skb);
1025 hci_devcd_complete(hdev);
1026 } else {
1027 bt_dev_err(hdev, "RTL: Failed to generate devcoredump");
1028 kfree_skb(skb);
1029 }
1030}
1031
f07d4780 1032static void btusb_rtl_reset(struct hci_dev *hdev)
d7ef0d1e
AL
1033{
1034 struct btusb_data *data = hci_get_drvdata(hdev);
1035 struct gpio_desc *reset_gpio = data->reset_gpio;
044014ce
HW
1036 struct rtk_dev_coredump_hdr hdr = {
1037 .type = RTK_DEVCOREDUMP_CODE_CMD_TIMEOUT,
1038 };
1039
1040 btusb_rtl_alloc_devcoredump(hdev, &hdr, NULL, 0);
d7ef0d1e 1041
d7ef0d1e 1042 if (!reset_gpio) {
c9209b26 1043 btusb_reset(hdev);
d7ef0d1e
AL
1044 return;
1045 }
1046
1047 /* Toggle the hard reset line. The Realtek device is going to
1048 * yank itself off the USB and then replug. The cleanup is handled
1049 * correctly on the way out (standard USB disconnect), and the new
1050 * device is detected cleanly and bound to the driver again like
1051 * it should be.
1052 */
1053 if (test_and_set_bit(BTUSB_HW_RESET_ACTIVE, &data->flags)) {
1054 bt_dev_err(hdev, "last reset failed? Not resetting again");
1055 return;
1056 }
1057
1058 bt_dev_err(hdev, "Reset Realtek device via gpio");
d7ef0d1e 1059 gpiod_set_value_cansleep(reset_gpio, 1);
34682110
MC
1060 msleep(200);
1061 gpiod_set_value_cansleep(reset_gpio, 0);
d7ef0d1e
AL
1062}
1063
044014ce
HW
1064static void btusb_rtl_hw_error(struct hci_dev *hdev, u8 code)
1065{
1066 struct rtk_dev_coredump_hdr hdr = {
1067 .type = RTK_DEVCOREDUMP_CODE_HW_ERR,
1068 .code = code,
1069 };
1070
1071 bt_dev_err(hdev, "RTL: hw err, trigger devcoredump (%d)", code);
1072
1073 btusb_rtl_alloc_devcoredump(hdev, &hdr, NULL, 0);
1074}
1075
f07d4780 1076static void btusb_qca_reset(struct hci_dev *hdev)
6933568a
APS
1077{
1078 struct btusb_data *data = hci_get_drvdata(hdev);
24ff62ae 1079 struct gpio_desc *reset_gpio = data->reset_gpio;
6933568a 1080
20981ce2 1081 if (test_bit(BTUSB_HW_SSR_ACTIVE, &data->flags)) {
f07d4780 1082 bt_dev_info(hdev, "Ramdump in progress, defer reset");
20981ce2
TJ
1083 return;
1084 }
1085
24ff62ae 1086 if (reset_gpio) {
1087 bt_dev_err(hdev, "Reset qca device via bt_en gpio");
1088
1089 /* Toggle the hard reset line. The qca bt device is going to
1090 * yank itself off the USB and then replug. The cleanup is handled
1091 * correctly on the way out (standard USB disconnect), and the new
1092 * device is detected cleanly and bound to the driver again like
1093 * it should be.
1094 */
1095 if (test_and_set_bit(BTUSB_HW_RESET_ACTIVE, &data->flags)) {
1096 bt_dev_err(hdev, "last reset failed? Not resetting again");
1097 return;
1098 }
1099
1100 gpiod_set_value_cansleep(reset_gpio, 0);
1101 msleep(200);
1102 gpiod_set_value_cansleep(reset_gpio, 1);
1103
1104 return;
1105 }
1106
c9209b26 1107 btusb_reset(hdev);
6933568a
APS
1108}
1109
803b5836
MH
1110static inline void btusb_free_frags(struct btusb_data *data)
1111{
1112 unsigned long flags;
1113
1114 spin_lock_irqsave(&data->rxlock, flags);
1115
b15a6bd3 1116 dev_kfree_skb_irq(data->evt_skb);
803b5836
MH
1117 data->evt_skb = NULL;
1118
b15a6bd3 1119 dev_kfree_skb_irq(data->acl_skb);
803b5836
MH
1120 data->acl_skb = NULL;
1121
b15a6bd3 1122 dev_kfree_skb_irq(data->sco_skb);
803b5836
MH
1123 data->sco_skb = NULL;
1124
1125 spin_unlock_irqrestore(&data->rxlock, flags);
1126}
1127
800fe5ec
LAD
1128static int btusb_recv_event(struct btusb_data *data, struct sk_buff *skb)
1129{
1130 if (data->intr_interval) {
e623e2a0 1131 /* Trigger dequeue immediately if an event is received */
800fe5ec
LAD
1132 schedule_delayed_work(&data->rx_work, 0);
1133 }
1134
1135 return data->recv_event(data->hdev, skb);
1136}
1137
1ffa4ad0
MH
1138static int btusb_recv_intr(struct btusb_data *data, void *buffer, int count)
1139{
803b5836 1140 struct sk_buff *skb;
a5e50d5b 1141 unsigned long flags;
803b5836
MH
1142 int err = 0;
1143
a5e50d5b 1144 spin_lock_irqsave(&data->rxlock, flags);
803b5836
MH
1145 skb = data->evt_skb;
1146
1147 while (count) {
1148 int len;
1149
1150 if (!skb) {
1151 skb = bt_skb_alloc(HCI_MAX_EVENT_SIZE, GFP_ATOMIC);
1152 if (!skb) {
1153 err = -ENOMEM;
1154 break;
1155 }
1156
618e8bc2
MH
1157 hci_skb_pkt_type(skb) = HCI_EVENT_PKT;
1158 hci_skb_expect(skb) = HCI_EVENT_HDR_SIZE;
803b5836
MH
1159 }
1160
618e8bc2 1161 len = min_t(uint, hci_skb_expect(skb), count);
59ae1d12 1162 skb_put_data(skb, buffer, len);
803b5836
MH
1163
1164 count -= len;
1165 buffer += len;
618e8bc2 1166 hci_skb_expect(skb) -= len;
803b5836
MH
1167
1168 if (skb->len == HCI_EVENT_HDR_SIZE) {
1169 /* Complete event header */
618e8bc2 1170 hci_skb_expect(skb) = hci_event_hdr(skb)->plen;
803b5836 1171
618e8bc2 1172 if (skb_tailroom(skb) < hci_skb_expect(skb)) {
803b5836
MH
1173 kfree_skb(skb);
1174 skb = NULL;
1175
1176 err = -EILSEQ;
1177 break;
1178 }
1179 }
1180
618e8bc2 1181 if (!hci_skb_expect(skb)) {
803b5836 1182 /* Complete frame */
800fe5ec 1183 btusb_recv_event(data, skb);
803b5836
MH
1184 skb = NULL;
1185 }
1186 }
1187
1188 data->evt_skb = skb;
a5e50d5b 1189 spin_unlock_irqrestore(&data->rxlock, flags);
803b5836
MH
1190
1191 return err;
1ffa4ad0
MH
1192}
1193
800fe5ec
LAD
1194static int btusb_recv_acl(struct btusb_data *data, struct sk_buff *skb)
1195{
1196 /* Only queue ACL packet if intr_interval is set as it means
1197 * force_poll_sync has been enabled.
1198 */
1199 if (!data->intr_interval)
1200 return data->recv_acl(data->hdev, skb);
1201
1202 skb_queue_tail(&data->acl_q, skb);
1203 schedule_delayed_work(&data->rx_work, data->intr_interval);
1204
1205 return 0;
1206}
1207
1ffa4ad0
MH
1208static int btusb_recv_bulk(struct btusb_data *data, void *buffer, int count)
1209{
803b5836 1210 struct sk_buff *skb;
a5e50d5b 1211 unsigned long flags;
803b5836
MH
1212 int err = 0;
1213
a5e50d5b 1214 spin_lock_irqsave(&data->rxlock, flags);
803b5836
MH
1215 skb = data->acl_skb;
1216
1217 while (count) {
1218 int len;
1219
1220 if (!skb) {
1221 skb = bt_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC);
1222 if (!skb) {
1223 err = -ENOMEM;
1224 break;
1225 }
1226
618e8bc2
MH
1227 hci_skb_pkt_type(skb) = HCI_ACLDATA_PKT;
1228 hci_skb_expect(skb) = HCI_ACL_HDR_SIZE;
803b5836
MH
1229 }
1230
618e8bc2 1231 len = min_t(uint, hci_skb_expect(skb), count);
59ae1d12 1232 skb_put_data(skb, buffer, len);
803b5836
MH
1233
1234 count -= len;
1235 buffer += len;
618e8bc2 1236 hci_skb_expect(skb) -= len;
803b5836
MH
1237
1238 if (skb->len == HCI_ACL_HDR_SIZE) {
1239 __le16 dlen = hci_acl_hdr(skb)->dlen;
1240
1241 /* Complete ACL header */
618e8bc2 1242 hci_skb_expect(skb) = __le16_to_cpu(dlen);
803b5836 1243
618e8bc2 1244 if (skb_tailroom(skb) < hci_skb_expect(skb)) {
803b5836
MH
1245 kfree_skb(skb);
1246 skb = NULL;
1247
1248 err = -EILSEQ;
1249 break;
1250 }
1251 }
1252
618e8bc2 1253 if (!hci_skb_expect(skb)) {
803b5836 1254 /* Complete frame */
800fe5ec 1255 btusb_recv_acl(data, skb);
803b5836
MH
1256 skb = NULL;
1257 }
1258 }
1259
1260 data->acl_skb = skb;
a5e50d5b 1261 spin_unlock_irqrestore(&data->rxlock, flags);
803b5836
MH
1262
1263 return err;
1ffa4ad0
MH
1264}
1265
b3fdb8c9
NC
1266static bool btusb_validate_sco_handle(struct hci_dev *hdev,
1267 struct hci_sco_hdr *hdr)
1268{
1269 __u16 handle;
1270
1271 if (hci_dev_test_flag(hdev, HCI_USER_CHANNEL))
1272 // Can't validate, userspace controls everything.
1273 return true;
1274
1275 /*
1276 * USB isochronous transfers are not designed to be reliable and may
1277 * lose fragments. When this happens, the next first fragment
1278 * encountered might actually be a continuation fragment.
1279 * Validate the handle to detect it and drop it, or else the upper
1280 * layer will get garbage for a while.
1281 */
1282
1283 handle = hci_handle(__le16_to_cpu(hdr->handle));
1284
1285 switch (hci_conn_lookup_type(hdev, handle)) {
1286 case SCO_LINK:
1287 case ESCO_LINK:
1288 return true;
1289 default:
1290 return false;
1291 }
1292}
1293
1ffa4ad0
MH
1294static int btusb_recv_isoc(struct btusb_data *data, void *buffer, int count)
1295{
803b5836 1296 struct sk_buff *skb;
a5e50d5b 1297 unsigned long flags;
803b5836
MH
1298 int err = 0;
1299
a5e50d5b 1300 spin_lock_irqsave(&data->rxlock, flags);
803b5836
MH
1301 skb = data->sco_skb;
1302
1303 while (count) {
1304 int len;
1305
1306 if (!skb) {
1307 skb = bt_skb_alloc(HCI_MAX_SCO_SIZE, GFP_ATOMIC);
1308 if (!skb) {
1309 err = -ENOMEM;
1310 break;
1311 }
1312
618e8bc2
MH
1313 hci_skb_pkt_type(skb) = HCI_SCODATA_PKT;
1314 hci_skb_expect(skb) = HCI_SCO_HDR_SIZE;
803b5836
MH
1315 }
1316
618e8bc2 1317 len = min_t(uint, hci_skb_expect(skb), count);
59ae1d12 1318 skb_put_data(skb, buffer, len);
803b5836
MH
1319
1320 count -= len;
1321 buffer += len;
618e8bc2 1322 hci_skb_expect(skb) -= len;
803b5836
MH
1323
1324 if (skb->len == HCI_SCO_HDR_SIZE) {
1325 /* Complete SCO header */
b3fdb8c9 1326 struct hci_sco_hdr *hdr = hci_sco_hdr(skb);
803b5836 1327
b3fdb8c9
NC
1328 hci_skb_expect(skb) = hdr->dlen;
1329
1330 if (skb_tailroom(skb) < hci_skb_expect(skb) ||
1331 !btusb_validate_sco_handle(data->hdev, hdr)) {
803b5836
MH
1332 kfree_skb(skb);
1333 skb = NULL;
1334
1335 err = -EILSEQ;
1336 break;
1337 }
1338 }
1339
618e8bc2 1340 if (!hci_skb_expect(skb)) {
803b5836
MH
1341 /* Complete frame */
1342 hci_recv_frame(data->hdev, skb);
1343 skb = NULL;
1344 }
1345 }
1346
1347 data->sco_skb = skb;
a5e50d5b 1348 spin_unlock_irqrestore(&data->rxlock, flags);
803b5836
MH
1349
1350 return err;
1ffa4ad0
MH
1351}
1352
5e23b923
MH
1353static void btusb_intr_complete(struct urb *urb)
1354{
1355 struct hci_dev *hdev = urb->context;
155961e8 1356 struct btusb_data *data = hci_get_drvdata(hdev);
5e23b923
MH
1357 int err;
1358
89e7533d
MH
1359 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
1360 urb->actual_length);
5e23b923
MH
1361
1362 if (!test_bit(HCI_RUNNING, &hdev->flags))
1363 return;
1364
1365 if (urb->status == 0) {
9bfa35fe
MH
1366 hdev->stat.byte_rx += urb->actual_length;
1367
1ffa4ad0
MH
1368 if (btusb_recv_intr(data, urb->transfer_buffer,
1369 urb->actual_length) < 0) {
2064ee33 1370 bt_dev_err(hdev, "corrupted event packet");
5e23b923
MH
1371 hdev->stat.err_rx++;
1372 }
85560c4a
CC
1373 } else if (urb->status == -ENOENT) {
1374 /* Avoid suspend failed when usb_kill_urb */
1375 return;
5e23b923
MH
1376 }
1377
1378 if (!test_bit(BTUSB_INTR_RUNNING, &data->flags))
1379 return;
1380
7bee549e 1381 usb_mark_last_busy(data->udev);
5e23b923
MH
1382 usb_anchor_urb(urb, &data->intr_anchor);
1383
1384 err = usb_submit_urb(urb, GFP_ATOMIC);
1385 if (err < 0) {
4935f1c1 1386 /* -EPERM: urb is being killed;
d98422cb
DR
1387 * -ENODEV: device got disconnected
1388 */
4935f1c1 1389 if (err != -EPERM && err != -ENODEV)
2064ee33
MH
1390 bt_dev_err(hdev, "urb %p failed to resubmit (%d)",
1391 urb, -err);
c97a747e
BB
1392 if (err != -EPERM)
1393 hci_cmd_sync_cancel(hdev, -err);
5e23b923
MH
1394 usb_unanchor_urb(urb);
1395 }
1396}
1397
2eda66f4 1398static int btusb_submit_intr_urb(struct hci_dev *hdev, gfp_t mem_flags)
5e23b923 1399{
155961e8 1400 struct btusb_data *data = hci_get_drvdata(hdev);
5e23b923
MH
1401 struct urb *urb;
1402 unsigned char *buf;
1403 unsigned int pipe;
1404 int err, size;
1405
1406 BT_DBG("%s", hdev->name);
1407
9bfa35fe
MH
1408 if (!data->intr_ep)
1409 return -ENODEV;
1410
2eda66f4 1411 urb = usb_alloc_urb(0, mem_flags);
5e23b923
MH
1412 if (!urb)
1413 return -ENOMEM;
1414
2c1dda2a
LAD
1415 if (le16_to_cpu(data->udev->descriptor.idVendor) == 0x0a12 &&
1416 le16_to_cpu(data->udev->descriptor.idProduct) == 0x0001)
1417 /* Fake CSR devices don't seem to support sort-transter */
1418 size = le16_to_cpu(data->intr_ep->wMaxPacketSize);
1419 else
1420 /* Use maximum HCI Event size so the USB stack handles
1421 * ZPL/short-transfer automatically.
1422 */
1423 size = HCI_MAX_EVENT_SIZE;
5e23b923 1424
2eda66f4 1425 buf = kmalloc(size, mem_flags);
5e23b923
MH
1426 if (!buf) {
1427 usb_free_urb(urb);
1428 return -ENOMEM;
1429 }
1430
1431 pipe = usb_rcvintpipe(data->udev, data->intr_ep->bEndpointAddress);
1432
1433 usb_fill_int_urb(urb, data->udev, pipe, buf, size,
89e7533d 1434 btusb_intr_complete, hdev, data->intr_ep->bInterval);
5e23b923
MH
1435
1436 urb->transfer_flags |= URB_FREE_BUFFER;
1437
1438 usb_anchor_urb(urb, &data->intr_anchor);
1439
2eda66f4 1440 err = usb_submit_urb(urb, mem_flags);
5e23b923 1441 if (err < 0) {
d4b8d1c9 1442 if (err != -EPERM && err != -ENODEV)
2064ee33
MH
1443 bt_dev_err(hdev, "urb %p submission failed (%d)",
1444 urb, -err);
c97a747e
BB
1445 if (err != -EPERM)
1446 hci_cmd_sync_cancel(hdev, -err);
5e23b923 1447 usb_unanchor_urb(urb);
5e23b923
MH
1448 }
1449
800fe5ec
LAD
1450 /* Only initialize intr_interval if URB poll sync is enabled */
1451 if (!data->poll_sync)
1452 goto done;
1453
1454 /* The units are frames (milliseconds) for full and low speed devices,
1455 * and microframes (1/8 millisecond) for highspeed and SuperSpeed
1456 * devices.
1457 *
1458 * This is done once on open/resume so it shouldn't change even if
1459 * force_poll_sync changes.
1460 */
1461 switch (urb->dev->speed) {
1462 case USB_SPEED_SUPER_PLUS:
1463 case USB_SPEED_SUPER: /* units are 125us */
1464 data->intr_interval = usecs_to_jiffies(urb->interval * 125);
1465 break;
1466 default:
1467 data->intr_interval = msecs_to_jiffies(urb->interval);
1468 break;
1469 }
1470
1471done:
5e23b923
MH
1472 usb_free_urb(urb);
1473
1474 return err;
1475}
1476
1477static void btusb_bulk_complete(struct urb *urb)
1478{
1479 struct hci_dev *hdev = urb->context;
155961e8 1480 struct btusb_data *data = hci_get_drvdata(hdev);
5e23b923
MH
1481 int err;
1482
89e7533d
MH
1483 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
1484 urb->actual_length);
5e23b923
MH
1485
1486 if (!test_bit(HCI_RUNNING, &hdev->flags))
1487 return;
1488
1489 if (urb->status == 0) {
9bfa35fe
MH
1490 hdev->stat.byte_rx += urb->actual_length;
1491
2cbd3f5c 1492 if (data->recv_bulk(data, urb->transfer_buffer,
1ffa4ad0 1493 urb->actual_length) < 0) {
2064ee33 1494 bt_dev_err(hdev, "corrupted ACL packet");
5e23b923
MH
1495 hdev->stat.err_rx++;
1496 }
85560c4a
CC
1497 } else if (urb->status == -ENOENT) {
1498 /* Avoid suspend failed when usb_kill_urb */
1499 return;
5e23b923
MH
1500 }
1501
1502 if (!test_bit(BTUSB_BULK_RUNNING, &data->flags))
1503 return;
1504
1505 usb_anchor_urb(urb, &data->bulk_anchor);
652fd781 1506 usb_mark_last_busy(data->udev);
5e23b923
MH
1507
1508 err = usb_submit_urb(urb, GFP_ATOMIC);
1509 if (err < 0) {
4935f1c1 1510 /* -EPERM: urb is being killed;
d98422cb
DR
1511 * -ENODEV: device got disconnected
1512 */
4935f1c1 1513 if (err != -EPERM && err != -ENODEV)
2064ee33
MH
1514 bt_dev_err(hdev, "urb %p failed to resubmit (%d)",
1515 urb, -err);
5e23b923
MH
1516 usb_unanchor_urb(urb);
1517 }
1518}
1519
2eda66f4 1520static int btusb_submit_bulk_urb(struct hci_dev *hdev, gfp_t mem_flags)
5e23b923 1521{
155961e8 1522 struct btusb_data *data = hci_get_drvdata(hdev);
5e23b923
MH
1523 struct urb *urb;
1524 unsigned char *buf;
1525 unsigned int pipe;
290ba200 1526 int err, size = HCI_MAX_FRAME_SIZE;
5e23b923
MH
1527
1528 BT_DBG("%s", hdev->name);
1529
9bfa35fe
MH
1530 if (!data->bulk_rx_ep)
1531 return -ENODEV;
1532
2eda66f4 1533 urb = usb_alloc_urb(0, mem_flags);
5e23b923
MH
1534 if (!urb)
1535 return -ENOMEM;
1536
2eda66f4 1537 buf = kmalloc(size, mem_flags);
5e23b923
MH
1538 if (!buf) {
1539 usb_free_urb(urb);
1540 return -ENOMEM;
1541 }
1542
1543 pipe = usb_rcvbulkpipe(data->udev, data->bulk_rx_ep->bEndpointAddress);
1544
89e7533d
MH
1545 usb_fill_bulk_urb(urb, data->udev, pipe, buf, size,
1546 btusb_bulk_complete, hdev);
5e23b923
MH
1547
1548 urb->transfer_flags |= URB_FREE_BUFFER;
1549
7bee549e 1550 usb_mark_last_busy(data->udev);
5e23b923
MH
1551 usb_anchor_urb(urb, &data->bulk_anchor);
1552
2eda66f4 1553 err = usb_submit_urb(urb, mem_flags);
5e23b923 1554 if (err < 0) {
d4b8d1c9 1555 if (err != -EPERM && err != -ENODEV)
2064ee33
MH
1556 bt_dev_err(hdev, "urb %p submission failed (%d)",
1557 urb, -err);
5e23b923 1558 usb_unanchor_urb(urb);
5e23b923
MH
1559 }
1560
1561 usb_free_urb(urb);
1562
1563 return err;
1564}
1565
9bfa35fe
MH
1566static void btusb_isoc_complete(struct urb *urb)
1567{
1568 struct hci_dev *hdev = urb->context;
155961e8 1569 struct btusb_data *data = hci_get_drvdata(hdev);
9bfa35fe
MH
1570 int i, err;
1571
89e7533d
MH
1572 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
1573 urb->actual_length);
9bfa35fe
MH
1574
1575 if (!test_bit(HCI_RUNNING, &hdev->flags))
1576 return;
1577
1578 if (urb->status == 0) {
1579 for (i = 0; i < urb->number_of_packets; i++) {
1580 unsigned int offset = urb->iso_frame_desc[i].offset;
1581 unsigned int length = urb->iso_frame_desc[i].actual_length;
1582
1583 if (urb->iso_frame_desc[i].status)
1584 continue;
1585
1586 hdev->stat.byte_rx += length;
1587
1ffa4ad0
MH
1588 if (btusb_recv_isoc(data, urb->transfer_buffer + offset,
1589 length) < 0) {
2064ee33 1590 bt_dev_err(hdev, "corrupted SCO packet");
9bfa35fe
MH
1591 hdev->stat.err_rx++;
1592 }
1593 }
85560c4a
CC
1594 } else if (urb->status == -ENOENT) {
1595 /* Avoid suspend failed when usb_kill_urb */
1596 return;
9bfa35fe
MH
1597 }
1598
1599 if (!test_bit(BTUSB_ISOC_RUNNING, &data->flags))
1600 return;
1601
1602 usb_anchor_urb(urb, &data->isoc_anchor);
1603
1604 err = usb_submit_urb(urb, GFP_ATOMIC);
1605 if (err < 0) {
4935f1c1 1606 /* -EPERM: urb is being killed;
d98422cb
DR
1607 * -ENODEV: device got disconnected
1608 */
4935f1c1 1609 if (err != -EPERM && err != -ENODEV)
2064ee33
MH
1610 bt_dev_err(hdev, "urb %p failed to resubmit (%d)",
1611 urb, -err);
9bfa35fe
MH
1612 usb_unanchor_urb(urb);
1613 }
1614}
1615
baac6276
SN
1616static inline void __fill_isoc_descriptor_msbc(struct urb *urb, int len,
1617 int mtu, struct btusb_data *data)
1618{
a479e713 1619 int i = 0, offset = 0;
baac6276
SN
1620 unsigned int interval;
1621
1622 BT_DBG("len %d mtu %d", len, mtu);
1623
a479e713
HW
1624 /* For mSBC ALT 6 settings some chips need to transmit the data
1625 * continuously without the zero length of USB packets.
1626 */
1627 if (test_bit(BTUSB_ALT6_CONTINUOUS_TX, &data->flags))
1628 goto ignore_usb_alt6_packet_flow;
1629
baac6276
SN
1630 /* For mSBC ALT 6 setting the host will send the packet at continuous
1631 * flow. As per core spec 5, vol 4, part B, table 2.1. For ALT setting
1632 * 6 the HCI PACKET INTERVAL should be 7.5ms for every usb packets.
1633 * To maintain the rate we send 63bytes of usb packets alternatively for
1634 * 7ms and 8ms to maintain the rate as 7.5ms.
1635 */
1636 if (data->usb_alt6_packet_flow) {
1637 interval = 7;
1638 data->usb_alt6_packet_flow = false;
1639 } else {
1640 interval = 6;
1641 data->usb_alt6_packet_flow = true;
1642 }
1643
1644 for (i = 0; i < interval; i++) {
1645 urb->iso_frame_desc[i].offset = offset;
1646 urb->iso_frame_desc[i].length = offset;
1647 }
1648
a479e713 1649ignore_usb_alt6_packet_flow:
baac6276
SN
1650 if (len && i < BTUSB_MAX_ISOC_FRAMES) {
1651 urb->iso_frame_desc[i].offset = offset;
1652 urb->iso_frame_desc[i].length = len;
1653 i++;
1654 }
1655
1656 urb->number_of_packets = i;
1657}
1658
42b16b3f 1659static inline void __fill_isoc_descriptor(struct urb *urb, int len, int mtu)
9bfa35fe
MH
1660{
1661 int i, offset = 0;
1662
1663 BT_DBG("len %d mtu %d", len, mtu);
1664
1665 for (i = 0; i < BTUSB_MAX_ISOC_FRAMES && len >= mtu;
1666 i++, offset += mtu, len -= mtu) {
1667 urb->iso_frame_desc[i].offset = offset;
1668 urb->iso_frame_desc[i].length = mtu;
1669 }
1670
1671 if (len && i < BTUSB_MAX_ISOC_FRAMES) {
1672 urb->iso_frame_desc[i].offset = offset;
1673 urb->iso_frame_desc[i].length = len;
1674 i++;
1675 }
1676
1677 urb->number_of_packets = i;
1678}
1679
2eda66f4 1680static int btusb_submit_isoc_urb(struct hci_dev *hdev, gfp_t mem_flags)
9bfa35fe 1681{
155961e8 1682 struct btusb_data *data = hci_get_drvdata(hdev);
9bfa35fe
MH
1683 struct urb *urb;
1684 unsigned char *buf;
1685 unsigned int pipe;
1686 int err, size;
1687
1688 BT_DBG("%s", hdev->name);
1689
1690 if (!data->isoc_rx_ep)
1691 return -ENODEV;
1692
2eda66f4 1693 urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, mem_flags);
9bfa35fe
MH
1694 if (!urb)
1695 return -ENOMEM;
1696
1697 size = le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize) *
1698 BTUSB_MAX_ISOC_FRAMES;
1699
2eda66f4 1700 buf = kmalloc(size, mem_flags);
9bfa35fe
MH
1701 if (!buf) {
1702 usb_free_urb(urb);
1703 return -ENOMEM;
1704 }
1705
1706 pipe = usb_rcvisocpipe(data->udev, data->isoc_rx_ep->bEndpointAddress);
1707
fa0fb93f 1708 usb_fill_int_urb(urb, data->udev, pipe, buf, size, btusb_isoc_complete,
89e7533d 1709 hdev, data->isoc_rx_ep->bInterval);
9bfa35fe 1710
89e7533d 1711 urb->transfer_flags = URB_FREE_BUFFER | URB_ISO_ASAP;
9bfa35fe
MH
1712
1713 __fill_isoc_descriptor(urb, size,
89e7533d 1714 le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize));
9bfa35fe
MH
1715
1716 usb_anchor_urb(urb, &data->isoc_anchor);
1717
2eda66f4 1718 err = usb_submit_urb(urb, mem_flags);
9bfa35fe 1719 if (err < 0) {
d4b8d1c9 1720 if (err != -EPERM && err != -ENODEV)
2064ee33
MH
1721 bt_dev_err(hdev, "urb %p submission failed (%d)",
1722 urb, -err);
9bfa35fe 1723 usb_unanchor_urb(urb);
9bfa35fe
MH
1724 }
1725
1726 usb_free_urb(urb);
1727
1728 return err;
1729}
1730
9d08f504
MH
1731static void btusb_diag_complete(struct urb *urb)
1732{
1733 struct hci_dev *hdev = urb->context;
1734 struct btusb_data *data = hci_get_drvdata(hdev);
1735 int err;
1736
1737 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
1738 urb->actual_length);
1739
1740 if (urb->status == 0) {
1741 struct sk_buff *skb;
1742
1743 skb = bt_skb_alloc(urb->actual_length, GFP_ATOMIC);
1744 if (skb) {
59ae1d12
JB
1745 skb_put_data(skb, urb->transfer_buffer,
1746 urb->actual_length);
9d08f504
MH
1747 hci_recv_diag(hdev, skb);
1748 }
1749 } else if (urb->status == -ENOENT) {
1750 /* Avoid suspend failed when usb_kill_urb */
1751 return;
1752 }
1753
1754 if (!test_bit(BTUSB_DIAG_RUNNING, &data->flags))
1755 return;
1756
1757 usb_anchor_urb(urb, &data->diag_anchor);
1758 usb_mark_last_busy(data->udev);
1759
1760 err = usb_submit_urb(urb, GFP_ATOMIC);
1761 if (err < 0) {
1762 /* -EPERM: urb is being killed;
d98422cb
DR
1763 * -ENODEV: device got disconnected
1764 */
9d08f504 1765 if (err != -EPERM && err != -ENODEV)
2064ee33
MH
1766 bt_dev_err(hdev, "urb %p failed to resubmit (%d)",
1767 urb, -err);
9d08f504
MH
1768 usb_unanchor_urb(urb);
1769 }
1770}
1771
1772static int btusb_submit_diag_urb(struct hci_dev *hdev, gfp_t mem_flags)
1773{
1774 struct btusb_data *data = hci_get_drvdata(hdev);
1775 struct urb *urb;
1776 unsigned char *buf;
1777 unsigned int pipe;
1778 int err, size = HCI_MAX_FRAME_SIZE;
1779
1780 BT_DBG("%s", hdev->name);
1781
1782 if (!data->diag_rx_ep)
1783 return -ENODEV;
1784
1785 urb = usb_alloc_urb(0, mem_flags);
1786 if (!urb)
1787 return -ENOMEM;
1788
1789 buf = kmalloc(size, mem_flags);
1790 if (!buf) {
1791 usb_free_urb(urb);
1792 return -ENOMEM;
1793 }
1794
1795 pipe = usb_rcvbulkpipe(data->udev, data->diag_rx_ep->bEndpointAddress);
1796
1797 usb_fill_bulk_urb(urb, data->udev, pipe, buf, size,
1798 btusb_diag_complete, hdev);
1799
1800 urb->transfer_flags |= URB_FREE_BUFFER;
1801
1802 usb_mark_last_busy(data->udev);
1803 usb_anchor_urb(urb, &data->diag_anchor);
1804
1805 err = usb_submit_urb(urb, mem_flags);
1806 if (err < 0) {
1807 if (err != -EPERM && err != -ENODEV)
2064ee33
MH
1808 bt_dev_err(hdev, "urb %p submission failed (%d)",
1809 urb, -err);
9d08f504
MH
1810 usb_unanchor_urb(urb);
1811 }
1812
1813 usb_free_urb(urb);
1814
1815 return err;
1816}
1817
5e23b923 1818static void btusb_tx_complete(struct urb *urb)
7bee549e
ON
1819{
1820 struct sk_buff *skb = urb->context;
89e7533d 1821 struct hci_dev *hdev = (struct hci_dev *)skb->dev;
155961e8 1822 struct btusb_data *data = hci_get_drvdata(hdev);
a5e50d5b 1823 unsigned long flags;
7bee549e 1824
89e7533d
MH
1825 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
1826 urb->actual_length);
7bee549e
ON
1827
1828 if (!test_bit(HCI_RUNNING, &hdev->flags))
1829 goto done;
1830
c97a747e 1831 if (!urb->status) {
7bee549e 1832 hdev->stat.byte_tx += urb->transfer_buffer_length;
c97a747e
BB
1833 } else {
1834 if (hci_skb_pkt_type(skb) == HCI_COMMAND_PKT)
1835 hci_cmd_sync_cancel(hdev, -urb->status);
7bee549e 1836 hdev->stat.err_tx++;
c97a747e 1837 }
7bee549e
ON
1838
1839done:
a5e50d5b 1840 spin_lock_irqsave(&data->txlock, flags);
7bee549e 1841 data->tx_in_flight--;
a5e50d5b 1842 spin_unlock_irqrestore(&data->txlock, flags);
7bee549e
ON
1843
1844 kfree(urb->setup_packet);
1845
1846 kfree_skb(skb);
1847}
1848
1849static void btusb_isoc_tx_complete(struct urb *urb)
5e23b923
MH
1850{
1851 struct sk_buff *skb = urb->context;
89e7533d 1852 struct hci_dev *hdev = (struct hci_dev *)skb->dev;
5e23b923 1853
89e7533d
MH
1854 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
1855 urb->actual_length);
5e23b923
MH
1856
1857 if (!test_bit(HCI_RUNNING, &hdev->flags))
1858 goto done;
1859
1860 if (!urb->status)
1861 hdev->stat.byte_tx += urb->transfer_buffer_length;
1862 else
1863 hdev->stat.err_tx++;
1864
1865done:
1866 kfree(urb->setup_packet);
1867
1868 kfree_skb(skb);
1869}
1870
1871static int btusb_open(struct hci_dev *hdev)
1872{
155961e8 1873 struct btusb_data *data = hci_get_drvdata(hdev);
5e23b923
MH
1874 int err;
1875
1876 BT_DBG("%s", hdev->name);
1877
c7e163fe
EH
1878 err = usb_autopm_get_interface(data->intf);
1879 if (err < 0)
1880 return err;
1881
ace31982
KBYT
1882 /* Patching USB firmware files prior to starting any URBs of HCI path
1883 * It is more safe to use USB bulk channel for downloading USB patch
1884 */
1885 if (data->setup_on_usb) {
1886 err = data->setup_on_usb(hdev);
eb50042f 1887 if (err < 0)
3d44a6fd 1888 goto setup_fail;
ace31982
KBYT
1889 }
1890
7bee549e
ON
1891 data->intf->needs_remote_wakeup = 1;
1892
5e23b923 1893 if (test_and_set_bit(BTUSB_INTR_RUNNING, &data->flags))
7bee549e 1894 goto done;
5e23b923 1895
2eda66f4 1896 err = btusb_submit_intr_urb(hdev, GFP_KERNEL);
43c2e57f
MH
1897 if (err < 0)
1898 goto failed;
1899
1900 err = btusb_submit_bulk_urb(hdev, GFP_KERNEL);
5e23b923 1901 if (err < 0) {
43c2e57f
MH
1902 usb_kill_anchored_urbs(&data->intr_anchor);
1903 goto failed;
5e23b923
MH
1904 }
1905
43c2e57f
MH
1906 set_bit(BTUSB_BULK_RUNNING, &data->flags);
1907 btusb_submit_bulk_urb(hdev, GFP_KERNEL);
1908
9d08f504
MH
1909 if (data->diag) {
1910 if (!btusb_submit_diag_urb(hdev, GFP_KERNEL))
1911 set_bit(BTUSB_DIAG_RUNNING, &data->flags);
1912 }
1913
7bee549e
ON
1914done:
1915 usb_autopm_put_interface(data->intf);
43c2e57f
MH
1916 return 0;
1917
1918failed:
1919 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
3d44a6fd 1920setup_fail:
7bee549e 1921 usb_autopm_put_interface(data->intf);
5e23b923
MH
1922 return err;
1923}
1924
7bee549e
ON
1925static void btusb_stop_traffic(struct btusb_data *data)
1926{
1927 usb_kill_anchored_urbs(&data->intr_anchor);
1928 usb_kill_anchored_urbs(&data->bulk_anchor);
1929 usb_kill_anchored_urbs(&data->isoc_anchor);
9d08f504 1930 usb_kill_anchored_urbs(&data->diag_anchor);
a1c49c43 1931 usb_kill_anchored_urbs(&data->ctrl_anchor);
7bee549e
ON
1932}
1933
5e23b923
MH
1934static int btusb_close(struct hci_dev *hdev)
1935{
155961e8 1936 struct btusb_data *data = hci_get_drvdata(hdev);
7bee549e 1937 int err;
5e23b923
MH
1938
1939 BT_DBG("%s", hdev->name);
1940
800fe5ec 1941 cancel_delayed_work(&data->rx_work);
e8c3c3d2 1942 cancel_work_sync(&data->work);
404291ac 1943 cancel_work_sync(&data->waker);
e8c3c3d2 1944
800fe5ec
LAD
1945 skb_queue_purge(&data->acl_q);
1946
9bfa35fe 1947 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
5e23b923 1948 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
5e23b923 1949 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
9d08f504 1950 clear_bit(BTUSB_DIAG_RUNNING, &data->flags);
7bee549e
ON
1951
1952 btusb_stop_traffic(data);
803b5836
MH
1953 btusb_free_frags(data);
1954
7bee549e
ON
1955 err = usb_autopm_get_interface(data->intf);
1956 if (err < 0)
7b8e2c1d 1957 goto failed;
7bee549e
ON
1958
1959 data->intf->needs_remote_wakeup = 0;
9e45524a
AL
1960
1961 /* Enable remote wake up for auto-suspend */
8274db07 1962 if (test_bit(BTUSB_WAKEUP_AUTOSUSPEND, &data->flags))
9e45524a
AL
1963 data->intf->needs_remote_wakeup = 1;
1964
7bee549e 1965 usb_autopm_put_interface(data->intf);
5e23b923 1966
7b8e2c1d
ON
1967failed:
1968 usb_scuttle_anchored_urbs(&data->deferred);
5e23b923
MH
1969 return 0;
1970}
1971
1972static int btusb_flush(struct hci_dev *hdev)
1973{
155961e8 1974 struct btusb_data *data = hci_get_drvdata(hdev);
5e23b923
MH
1975
1976 BT_DBG("%s", hdev->name);
1977
800fe5ec
LAD
1978 cancel_delayed_work(&data->rx_work);
1979
1980 skb_queue_purge(&data->acl_q);
1981
5e23b923 1982 usb_kill_anchored_urbs(&data->tx_anchor);
803b5836 1983 btusb_free_frags(data);
5e23b923
MH
1984
1985 return 0;
1986}
1987
047b2ec8 1988static struct urb *alloc_ctrl_urb(struct hci_dev *hdev, struct sk_buff *skb)
5e23b923 1989{
155961e8 1990 struct btusb_data *data = hci_get_drvdata(hdev);
5e23b923
MH
1991 struct usb_ctrlrequest *dr;
1992 struct urb *urb;
1993 unsigned int pipe;
5e23b923 1994
047b2ec8
MH
1995 urb = usb_alloc_urb(0, GFP_KERNEL);
1996 if (!urb)
1997 return ERR_PTR(-ENOMEM);
5e23b923 1998
047b2ec8
MH
1999 dr = kmalloc(sizeof(*dr), GFP_KERNEL);
2000 if (!dr) {
2001 usb_free_urb(urb);
2002 return ERR_PTR(-ENOMEM);
2003 }
5e23b923 2004
047b2ec8 2005 dr->bRequestType = data->cmdreq_type;
893ba544 2006 dr->bRequest = data->cmdreq;
047b2ec8
MH
2007 dr->wIndex = 0;
2008 dr->wValue = 0;
2009 dr->wLength = __cpu_to_le16(skb->len);
7bd8f09f 2010
047b2ec8 2011 pipe = usb_sndctrlpipe(data->udev, 0x00);
5e23b923 2012
89e7533d 2013 usb_fill_control_urb(urb, data->udev, pipe, (void *)dr,
047b2ec8 2014 skb->data, skb->len, btusb_tx_complete, skb);
5e23b923 2015
89e7533d 2016 skb->dev = (void *)hdev;
5e23b923 2017
047b2ec8
MH
2018 return urb;
2019}
5e23b923 2020
047b2ec8
MH
2021static struct urb *alloc_bulk_urb(struct hci_dev *hdev, struct sk_buff *skb)
2022{
2023 struct btusb_data *data = hci_get_drvdata(hdev);
2024 struct urb *urb;
2025 unsigned int pipe;
5e23b923 2026
047b2ec8
MH
2027 if (!data->bulk_tx_ep)
2028 return ERR_PTR(-ENODEV);
9bfa35fe 2029
047b2ec8
MH
2030 urb = usb_alloc_urb(0, GFP_KERNEL);
2031 if (!urb)
2032 return ERR_PTR(-ENOMEM);
5e23b923 2033
047b2ec8 2034 pipe = usb_sndbulkpipe(data->udev, data->bulk_tx_ep->bEndpointAddress);
5e23b923 2035
047b2ec8
MH
2036 usb_fill_bulk_urb(urb, data->udev, pipe,
2037 skb->data, skb->len, btusb_tx_complete, skb);
5e23b923 2038
89e7533d 2039 skb->dev = (void *)hdev;
5e23b923 2040
047b2ec8
MH
2041 return urb;
2042}
9bfa35fe 2043
047b2ec8
MH
2044static struct urb *alloc_isoc_urb(struct hci_dev *hdev, struct sk_buff *skb)
2045{
2046 struct btusb_data *data = hci_get_drvdata(hdev);
2047 struct urb *urb;
2048 unsigned int pipe;
9bfa35fe 2049
047b2ec8
MH
2050 if (!data->isoc_tx_ep)
2051 return ERR_PTR(-ENODEV);
9bfa35fe 2052
047b2ec8
MH
2053 urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, GFP_KERNEL);
2054 if (!urb)
2055 return ERR_PTR(-ENOMEM);
9bfa35fe 2056
047b2ec8 2057 pipe = usb_sndisocpipe(data->udev, data->isoc_tx_ep->bEndpointAddress);
9bfa35fe 2058
047b2ec8
MH
2059 usb_fill_int_urb(urb, data->udev, pipe,
2060 skb->data, skb->len, btusb_isoc_tx_complete,
2061 skb, data->isoc_tx_ep->bInterval);
9bfa35fe 2062
047b2ec8 2063 urb->transfer_flags = URB_ISO_ASAP;
5e23b923 2064
baac6276
SN
2065 if (data->isoc_altsetting == 6)
2066 __fill_isoc_descriptor_msbc(urb, skb->len,
2067 le16_to_cpu(data->isoc_tx_ep->wMaxPacketSize),
2068 data);
2069 else
2070 __fill_isoc_descriptor(urb, skb->len,
2071 le16_to_cpu(data->isoc_tx_ep->wMaxPacketSize));
89e7533d 2072 skb->dev = (void *)hdev;
047b2ec8
MH
2073
2074 return urb;
2075}
2076
2077static int submit_tx_urb(struct hci_dev *hdev, struct urb *urb)
2078{
2079 struct btusb_data *data = hci_get_drvdata(hdev);
2080 int err;
7bee549e 2081
5e23b923
MH
2082 usb_anchor_urb(urb, &data->tx_anchor);
2083
e9753eff 2084 err = usb_submit_urb(urb, GFP_KERNEL);
5e23b923 2085 if (err < 0) {
5a9b80e2 2086 if (err != -EPERM && err != -ENODEV)
2064ee33
MH
2087 bt_dev_err(hdev, "urb %p submission failed (%d)",
2088 urb, -err);
5e23b923
MH
2089 kfree(urb->setup_packet);
2090 usb_unanchor_urb(urb);
7bee549e
ON
2091 } else {
2092 usb_mark_last_busy(data->udev);
5e23b923
MH
2093 }
2094
54a8a79c 2095 usb_free_urb(urb);
5e23b923
MH
2096 return err;
2097}
2098
047b2ec8
MH
2099static int submit_or_queue_tx_urb(struct hci_dev *hdev, struct urb *urb)
2100{
2101 struct btusb_data *data = hci_get_drvdata(hdev);
2102 unsigned long flags;
2103 bool suspending;
2104
2105 spin_lock_irqsave(&data->txlock, flags);
2106 suspending = test_bit(BTUSB_SUSPENDING, &data->flags);
2107 if (!suspending)
2108 data->tx_in_flight++;
2109 spin_unlock_irqrestore(&data->txlock, flags);
2110
2111 if (!suspending)
2112 return submit_tx_urb(hdev, urb);
2113
2114 usb_anchor_urb(urb, &data->deferred);
2115 schedule_work(&data->waker);
2116
2117 usb_free_urb(urb);
2118 return 0;
2119}
2120
2121static int btusb_send_frame(struct hci_dev *hdev, struct sk_buff *skb)
2122{
2123 struct urb *urb;
2124
2125 BT_DBG("%s", hdev->name);
2126
618e8bc2 2127 switch (hci_skb_pkt_type(skb)) {
047b2ec8
MH
2128 case HCI_COMMAND_PKT:
2129 urb = alloc_ctrl_urb(hdev, skb);
2130 if (IS_ERR(urb))
2131 return PTR_ERR(urb);
2132
2133 hdev->stat.cmd_tx++;
2134 return submit_or_queue_tx_urb(hdev, urb);
2135
2136 case HCI_ACLDATA_PKT:
2137 urb = alloc_bulk_urb(hdev, skb);
2138 if (IS_ERR(urb))
2139 return PTR_ERR(urb);
2140
2141 hdev->stat.acl_tx++;
2142 return submit_or_queue_tx_urb(hdev, urb);
2143
2144 case HCI_SCODATA_PKT:
bd30e8d7
HC
2145 if (!hci_dev_test_flag(hdev, HCI_USER_CHANNEL) &&
2146 hci_conn_num(hdev, SCO_LINK) < 1)
047b2ec8
MH
2147 return -ENODEV;
2148
2149 urb = alloc_isoc_urb(hdev, skb);
2150 if (IS_ERR(urb))
2151 return PTR_ERR(urb);
2152
2153 hdev->stat.sco_tx++;
2154 return submit_tx_urb(hdev, urb);
70405f02
LAD
2155
2156 case HCI_ISODATA_PKT:
2157 urb = alloc_bulk_urb(hdev, skb);
2158 if (IS_ERR(urb))
2159 return PTR_ERR(urb);
2160
2161 return submit_or_queue_tx_urb(hdev, urb);
047b2ec8
MH
2162 }
2163
2164 return -EILSEQ;
2165}
2166
5e23b923
MH
2167static void btusb_notify(struct hci_dev *hdev, unsigned int evt)
2168{
155961e8 2169 struct btusb_data *data = hci_get_drvdata(hdev);
5e23b923
MH
2170
2171 BT_DBG("%s evt %d", hdev->name, evt);
2172
014f7bc7
MH
2173 if (hci_conn_num(hdev, SCO_LINK) != data->sco_num) {
2174 data->sco_num = hci_conn_num(hdev, SCO_LINK);
baac6276 2175 data->air_mode = evt;
43c2e57f 2176 schedule_work(&data->work);
a780efa8 2177 }
5e23b923
MH
2178}
2179
42b16b3f 2180static inline int __set_isoc_interface(struct hci_dev *hdev, int altsetting)
9bfa35fe 2181{
155961e8 2182 struct btusb_data *data = hci_get_drvdata(hdev);
9bfa35fe
MH
2183 struct usb_interface *intf = data->isoc;
2184 struct usb_endpoint_descriptor *ep_desc;
2185 int i, err;
2186
2187 if (!data->isoc)
2188 return -ENODEV;
2189
459232fc 2190 err = usb_set_interface(data->udev, data->isoc_ifnum, altsetting);
9bfa35fe 2191 if (err < 0) {
2064ee33 2192 bt_dev_err(hdev, "setting interface failed (%d)", -err);
9bfa35fe
MH
2193 return err;
2194 }
2195
2196 data->isoc_altsetting = altsetting;
2197
2198 data->isoc_tx_ep = NULL;
2199 data->isoc_rx_ep = NULL;
2200
2201 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
2202 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
2203
2204 if (!data->isoc_tx_ep && usb_endpoint_is_isoc_out(ep_desc)) {
2205 data->isoc_tx_ep = ep_desc;
2206 continue;
2207 }
2208
2209 if (!data->isoc_rx_ep && usb_endpoint_is_isoc_in(ep_desc)) {
2210 data->isoc_rx_ep = ep_desc;
2211 continue;
2212 }
2213 }
2214
2215 if (!data->isoc_tx_ep || !data->isoc_rx_ep) {
2064ee33 2216 bt_dev_err(hdev, "invalid SCO descriptors");
9bfa35fe
MH
2217 return -ENODEV;
2218 }
2219
2220 return 0;
2221}
2222
baac6276
SN
2223static int btusb_switch_alt_setting(struct hci_dev *hdev, int new_alts)
2224{
2225 struct btusb_data *data = hci_get_drvdata(hdev);
2226 int err;
2227
2228 if (data->isoc_altsetting != new_alts) {
2229 unsigned long flags;
2230
2231 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
2232 usb_kill_anchored_urbs(&data->isoc_anchor);
2233
2234 /* When isochronous alternate setting needs to be
2235 * changed, because SCO connection has been added
2236 * or removed, a packet fragment may be left in the
2237 * reassembling state. This could lead to wrongly
2238 * assembled fragments.
2239 *
2240 * Clear outstanding fragment when selecting a new
2241 * alternate setting.
2242 */
2243 spin_lock_irqsave(&data->rxlock, flags);
2a05334d 2244 dev_kfree_skb_irq(data->sco_skb);
baac6276
SN
2245 data->sco_skb = NULL;
2246 spin_unlock_irqrestore(&data->rxlock, flags);
2247
2248 err = __set_isoc_interface(hdev, new_alts);
2249 if (err < 0)
2250 return err;
2251 }
2252
2253 if (!test_and_set_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
2254 if (btusb_submit_isoc_urb(hdev, GFP_KERNEL) < 0)
2255 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
2256 else
2257 btusb_submit_isoc_urb(hdev, GFP_KERNEL);
2258 }
2259
2260 return 0;
2261}
2262
2263static struct usb_host_interface *btusb_find_altsetting(struct btusb_data *data,
2264 int alt)
2265{
2266 struct usb_interface *intf = data->isoc;
2267 int i;
2268
2269 BT_DBG("Looking for Alt no :%d", alt);
2270
fcd156ee
SN
2271 if (!intf)
2272 return NULL;
2273
baac6276
SN
2274 for (i = 0; i < intf->num_altsetting; i++) {
2275 if (intf->altsetting[i].desc.bAlternateSetting == alt)
2276 return &intf->altsetting[i];
2277 }
2278
2279 return NULL;
2280}
2281
5e23b923
MH
2282static void btusb_work(struct work_struct *work)
2283{
2284 struct btusb_data *data = container_of(work, struct btusb_data, work);
2285 struct hci_dev *hdev = data->hdev;
baac6276 2286 int new_alts = 0;
7bee549e 2287 int err;
5e23b923 2288
014f7bc7 2289 if (data->sco_num > 0) {
08b8b6c4 2290 if (!test_bit(BTUSB_DID_ISO_RESUME, &data->flags)) {
8efdd0cd 2291 err = usb_autopm_get_interface(data->isoc ? data->isoc : data->intf);
7bee549e
ON
2292 if (err < 0) {
2293 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
2294 usb_kill_anchored_urbs(&data->isoc_anchor);
2295 return;
2296 }
2297
08b8b6c4 2298 set_bit(BTUSB_DID_ISO_RESUME, &data->flags);
7bee549e 2299 }
f4001d28 2300
baac6276
SN
2301 if (data->air_mode == HCI_NOTIFY_ENABLE_SCO_CVSD) {
2302 if (hdev->voice_setting & 0x0020) {
2303 static const int alts[3] = { 2, 4, 5 };
f4001d28 2304
baac6276
SN
2305 new_alts = alts[data->sco_num - 1];
2306 } else {
2307 new_alts = data->sco_num;
2308 }
2309 } else if (data->air_mode == HCI_NOTIFY_ENABLE_SCO_TRANSP) {
517b6933
TP
2310 /* Bluetooth USB spec recommends alt 6 (63 bytes), but
2311 * many adapters do not support it. Alt 1 appears to
2312 * work for all adapters that do not have alt 6, and
55981d35
PV
2313 * which work with WBS at all. Some devices prefer
2314 * alt 3 (HCI payload >= 60 Bytes let air packet
2315 * data satisfy 60 bytes), requiring
2316 * MTU >= 3 (packets) * 25 (size) - 3 (headers) = 72
2317 * see also Core spec 5, vol 4, B 2.1.1 & Table 2.1.
517b6933 2318 */
55981d35
PV
2319 if (btusb_find_altsetting(data, 6))
2320 new_alts = 6;
2321 else if (btusb_find_altsetting(data, 3) &&
2322 hdev->sco_mtu >= 72 &&
2323 test_bit(BTUSB_USE_ALT3_FOR_WBS, &data->flags))
e848dbd3 2324 new_alts = 3;
55981d35
PV
2325 else
2326 new_alts = 1;
9bfa35fe 2327 }
baac6276
SN
2328
2329 if (btusb_switch_alt_setting(hdev, new_alts) < 0)
2330 bt_dev_err(hdev, "set USB alt:(%d) failed!", new_alts);
9bfa35fe 2331 } else {
9bfa35fe
MH
2332 usb_kill_anchored_urbs(&data->isoc_anchor);
2333
f9c5cbd5
CTN
2334 if (test_and_clear_bit(BTUSB_ISOC_RUNNING, &data->flags))
2335 __set_isoc_interface(hdev, 0);
2336
08b8b6c4 2337 if (test_and_clear_bit(BTUSB_DID_ISO_RESUME, &data->flags))
8efdd0cd 2338 usb_autopm_put_interface(data->isoc ? data->isoc : data->intf);
5e23b923
MH
2339 }
2340}
2341
7bee549e
ON
2342static void btusb_waker(struct work_struct *work)
2343{
2344 struct btusb_data *data = container_of(work, struct btusb_data, waker);
2345 int err;
2346
2347 err = usb_autopm_get_interface(data->intf);
2348 if (err < 0)
2349 return;
2350
2351 usb_autopm_put_interface(data->intf);
2352}
2353
800fe5ec
LAD
2354static void btusb_rx_work(struct work_struct *work)
2355{
2356 struct btusb_data *data = container_of(work, struct btusb_data,
2357 rx_work.work);
2358 struct sk_buff *skb;
2359
2360 /* Dequeue ACL data received during the interval */
2361 while ((skb = skb_dequeue(&data->acl_q)))
2362 data->recv_acl(data->hdev, skb);
2363}
2364
9f8f962c
MH
2365static int btusb_setup_bcm92035(struct hci_dev *hdev)
2366{
2367 struct sk_buff *skb;
2368 u8 val = 0x00;
2369
2370 BT_DBG("%s", hdev->name);
2371
2372 skb = __hci_cmd_sync(hdev, 0xfc3b, 1, &val, HCI_INIT_TIMEOUT);
2373 if (IS_ERR(skb))
2064ee33 2374 bt_dev_err(hdev, "BCM92035 command failed (%ld)", PTR_ERR(skb));
9f8f962c
MH
2375 else
2376 kfree_skb(skb);
2377
2378 return 0;
2379}
2380
81cac64b
MH
2381static int btusb_setup_csr(struct hci_dev *hdev)
2382{
d74e0ae7
HG
2383 struct btusb_data *data = hci_get_drvdata(hdev);
2384 u16 bcdDevice = le16_to_cpu(data->udev->descriptor.bcdDevice);
81cac64b
MH
2385 struct hci_rp_read_local_version *rp;
2386 struct sk_buff *skb;
cde1a8a9 2387 bool is_fake = false;
0671c066 2388 int ret;
81cac64b
MH
2389
2390 BT_DBG("%s", hdev->name);
2391
7cd84d72
MH
2392 skb = __hci_cmd_sync(hdev, HCI_OP_READ_LOCAL_VERSION, 0, NULL,
2393 HCI_INIT_TIMEOUT);
2394 if (IS_ERR(skb)) {
2395 int err = PTR_ERR(skb);
2064ee33 2396 bt_dev_err(hdev, "CSR: Local version failed (%d)", err);
7cd84d72
MH
2397 return err;
2398 }
2399
cb3648a7
LAD
2400 rp = skb_pull_data(skb, sizeof(*rp));
2401 if (!rp) {
2064ee33 2402 bt_dev_err(hdev, "CSR: Local version length mismatch");
7cd84d72
MH
2403 kfree_skb(skb);
2404 return -EIO;
2405 }
81cac64b 2406
cb3648a7
LAD
2407 bt_dev_info(hdev, "CSR: Setting up dongle with HCI ver=%u rev=%04x",
2408 rp->hci_ver, le16_to_cpu(rp->hci_rev));
81cac64b 2409
cb3648a7
LAD
2410 bt_dev_info(hdev, "LMP ver=%u subver=%04x; manufacturer=%u",
2411 rp->lmp_ver, le16_to_cpu(rp->lmp_subver),
2412 le16_to_cpu(rp->manufacturer));
955aebd4 2413
cde1a8a9
IFM
2414 /* Detect a wide host of Chinese controllers that aren't CSR.
2415 *
2416 * Known fake bcdDevices: 0x0100, 0x0134, 0x1915, 0x2520, 0x7558, 0x8891
2417 *
2418 * The main thing they have in common is that these are really popular low-cost
2419 * options that support newer Bluetooth versions but rely on heavy VID/PID
2420 * squatting of this poor old Bluetooth 1.1 device. Even sold as such.
2421 *
2422 * We detect actual CSR devices by checking that the HCI manufacturer code
2423 * is Cambridge Silicon Radio (10) and ensuring that LMP sub-version and
2424 * HCI rev values always match. As they both store the firmware number.
2425 */
6cafcd95 2426 if (le16_to_cpu(rp->manufacturer) != 10 ||
cde1a8a9
IFM
2427 le16_to_cpu(rp->hci_rev) != le16_to_cpu(rp->lmp_subver))
2428 is_fake = true;
2429
2430 /* Known legit CSR firmware build numbers and their supported BT versions:
2431 * - 1.1 (0x1) -> 0x0073, 0x020d, 0x033c, 0x034e
2432 * - 1.2 (0x2) -> 0x04d9, 0x0529
2433 * - 2.0 (0x3) -> 0x07a6, 0x07ad, 0x0c5c
2434 * - 2.1 (0x4) -> 0x149c, 0x1735, 0x1899 (0x1899 is a BlueCore4-External)
2435 * - 4.0 (0x6) -> 0x1d86, 0x2031, 0x22bb
2436 *
2437 * e.g. Real CSR dongles with LMP subversion 0x73 are old enough that
2438 * support BT 1.1 only; so it's a dead giveaway when some
2439 * third-party BT 4.0 dongle reuses it.
2440 */
2441 else if (le16_to_cpu(rp->lmp_subver) <= 0x034e &&
cb3648a7 2442 rp->hci_ver > BLUETOOTH_VER_1_1)
cde1a8a9
IFM
2443 is_fake = true;
2444
2445 else if (le16_to_cpu(rp->lmp_subver) <= 0x0529 &&
cb3648a7 2446 rp->hci_ver > BLUETOOTH_VER_1_2)
cde1a8a9
IFM
2447 is_fake = true;
2448
2449 else if (le16_to_cpu(rp->lmp_subver) <= 0x0c5c &&
cb3648a7 2450 rp->hci_ver > BLUETOOTH_VER_2_0)
cde1a8a9
IFM
2451 is_fake = true;
2452
2453 else if (le16_to_cpu(rp->lmp_subver) <= 0x1899 &&
cb3648a7 2454 rp->hci_ver > BLUETOOTH_VER_2_1)
cde1a8a9
IFM
2455 is_fake = true;
2456
2457 else if (le16_to_cpu(rp->lmp_subver) <= 0x22bb &&
cb3648a7 2458 rp->hci_ver > BLUETOOTH_VER_4_0)
cde1a8a9
IFM
2459 is_fake = true;
2460
d74e0ae7
HG
2461 /* Other clones which beat all the above checks */
2462 else if (bcdDevice == 0x0134 &&
2463 le16_to_cpu(rp->lmp_subver) == 0x0c5c &&
cb3648a7 2464 rp->hci_ver == BLUETOOTH_VER_2_0)
d74e0ae7
HG
2465 is_fake = true;
2466
cde1a8a9 2467 if (is_fake) {
f4292e2f 2468 bt_dev_warn(hdev, "CSR: Unbranded CSR clone detected; adding workarounds and force-suspending once...");
cde1a8a9
IFM
2469
2470 /* Generally these clones have big discrepancies between
2471 * advertised features and what's actually supported.
2472 * Probably will need to be expanded in the future;
2473 * without these the controller will lock up.
2474 */
2475 set_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks);
42d7731e 2476 set_bit(HCI_QUIRK_BROKEN_ERR_DATA_REPORTING, &hdev->quirks);
b3cf94c8
IFM
2477 set_bit(HCI_QUIRK_BROKEN_FILTER_CLEAR_ALL, &hdev->quirks);
2478 set_bit(HCI_QUIRK_NO_SUSPEND_NOTIFIER, &hdev->quirks);
1f04b0e5
PN
2479 set_bit(HCI_QUIRK_BROKEN_READ_VOICE_SETTING, &hdev->quirks);
2480 set_bit(HCI_QUIRK_BROKEN_READ_PAGE_SCAN_TYPE, &hdev->quirks);
cde1a8a9 2481
9641d343
MH
2482 /* Clear the reset quirk since this is not an actual
2483 * early Bluetooth 1.1 device from CSR.
2484 */
2485 clear_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
cde1a8a9 2486 clear_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
0671c066
HG
2487
2488 /*
f4292e2f
IFM
2489 * Special workaround for these BT 4.0 chip clones, and potentially more:
2490 *
b3cf94c8 2491 * - 0x0134: a Barrot 8041a02 (HCI rev: 0x0810 sub: 0x1012)
f4292e2f
IFM
2492 * - 0x7558: IC markings FR3191AHAL 749H15143 (HCI rev/sub-version: 0x0709)
2493 *
2494 * These controllers are really messed-up.
2495 *
2496 * 1. Their bulk RX endpoint will never report any data unless
aa17e8ce 2497 * the device was suspended at least once (yes, really).
0671c066 2498 * 2. They will not wakeup when autosuspended and receiving data
aa17e8ce
IFM
2499 * on their bulk RX endpoint from e.g. a keyboard or mouse
2500 * (IOW remote-wakeup support is broken for the bulk endpoint).
0671c066
HG
2501 *
2502 * To fix 1. enable runtime-suspend, force-suspend the
f4292e2f 2503 * HCI and then wake-it up by disabling runtime-suspend.
0671c066 2504 *
f4292e2f 2505 * To fix 2. clear the HCI's can_wake flag, this way the HCI
0671c066 2506 * will still be autosuspended when it is not open.
f4292e2f
IFM
2507 *
2508 * --
2509 *
2510 * Because these are widespread problems we prefer generic solutions; so
2511 * apply this initialization quirk to every controller that gets here,
2512 * it should be harmless. The alternative is to not work at all.
0671c066 2513 */
f4292e2f
IFM
2514 pm_runtime_allow(&data->udev->dev);
2515
2516 ret = pm_runtime_suspend(&data->udev->dev);
2517 if (ret >= 0)
2518 msleep(200);
2519 else
b3cf94c8 2520 bt_dev_warn(hdev, "CSR: Couldn't suspend the device for our Barrot 8041a02 receive-issue workaround");
f4292e2f
IFM
2521
2522 pm_runtime_forbid(&data->udev->dev);
2523
2524 device_set_wakeup_capable(&data->udev->dev, false);
2525
2526 /* Re-enable autosuspend if this was requested */
2527 if (enable_autosuspend)
2528 usb_enable_autosuspend(data->udev);
9641d343 2529 }
81cac64b
MH
2530
2531 kfree_skb(skb);
2532
9641d343 2533 return 0;
81cac64b
MH
2534}
2535
cda0dd78
MH
2536static int inject_cmd_complete(struct hci_dev *hdev, __u16 opcode)
2537{
2538 struct sk_buff *skb;
2539 struct hci_event_hdr *hdr;
2540 struct hci_ev_cmd_complete *evt;
2541
cf07e341 2542 skb = bt_skb_alloc(sizeof(*hdr) + sizeof(*evt) + 1, GFP_KERNEL);
cda0dd78
MH
2543 if (!skb)
2544 return -ENOMEM;
2545
4df864c1 2546 hdr = skb_put(skb, sizeof(*hdr));
cda0dd78
MH
2547 hdr->evt = HCI_EV_CMD_COMPLETE;
2548 hdr->plen = sizeof(*evt) + 1;
2549
4df864c1 2550 evt = skb_put(skb, sizeof(*evt));
cda0dd78
MH
2551 evt->ncmd = 0x01;
2552 evt->opcode = cpu_to_le16(opcode);
2553
634fef61 2554 skb_put_u8(skb, 0x00);
cda0dd78 2555
618e8bc2 2556 hci_skb_pkt_type(skb) = HCI_EVENT_PKT;
cda0dd78
MH
2557
2558 return hci_recv_frame(hdev, skb);
2559}
2560
2561static int btusb_recv_bulk_intel(struct btusb_data *data, void *buffer,
2562 int count)
2563{
019a1caa
THJA
2564 struct hci_dev *hdev = data->hdev;
2565
cda0dd78
MH
2566 /* When the device is in bootloader mode, then it can send
2567 * events via the bulk endpoint. These events are treated the
2568 * same way as the ones received from the interrupt endpoint.
2569 */
019a1caa 2570 if (btintel_test_flag(hdev, INTEL_BOOTLOADER))
cda0dd78
MH
2571 return btusb_recv_intr(data, buffer, count);
2572
2573 return btusb_recv_bulk(data, buffer, count);
2574}
2575
cda0dd78
MH
2576static int btusb_send_frame_intel(struct hci_dev *hdev, struct sk_buff *skb)
2577{
cda0dd78
MH
2578 struct urb *urb;
2579
2580 BT_DBG("%s", hdev->name);
2581
618e8bc2 2582 switch (hci_skb_pkt_type(skb)) {
cda0dd78 2583 case HCI_COMMAND_PKT:
019a1caa 2584 if (btintel_test_flag(hdev, INTEL_BOOTLOADER)) {
cda0dd78
MH
2585 struct hci_command_hdr *cmd = (void *)skb->data;
2586 __u16 opcode = le16_to_cpu(cmd->opcode);
2587
2588 /* When in bootloader mode and the command 0xfc09
2589 * is received, it needs to be send down the
2590 * bulk endpoint. So allocate a bulk URB instead.
2591 */
2592 if (opcode == 0xfc09)
2593 urb = alloc_bulk_urb(hdev, skb);
2594 else
2595 urb = alloc_ctrl_urb(hdev, skb);
2596
2597 /* When the 0xfc01 command is issued to boot into
2598 * the operational firmware, it will actually not
2599 * send a command complete event. To keep the flow
2600 * control working inject that event here.
2601 */
2602 if (opcode == 0xfc01)
2603 inject_cmd_complete(hdev, opcode);
2604 } else {
2605 urb = alloc_ctrl_urb(hdev, skb);
2606 }
2607 if (IS_ERR(urb))
2608 return PTR_ERR(urb);
2609
2610 hdev->stat.cmd_tx++;
2611 return submit_or_queue_tx_urb(hdev, urb);
2612
2613 case HCI_ACLDATA_PKT:
2614 urb = alloc_bulk_urb(hdev, skb);
2615 if (IS_ERR(urb))
2616 return PTR_ERR(urb);
2617
2618 hdev->stat.acl_tx++;
2619 return submit_or_queue_tx_urb(hdev, urb);
2620
2621 case HCI_SCODATA_PKT:
bd30e8d7
HC
2622 if (!hci_dev_test_flag(hdev, HCI_USER_CHANNEL) &&
2623 hci_conn_num(hdev, SCO_LINK) < 1)
cda0dd78
MH
2624 return -ENODEV;
2625
2626 urb = alloc_isoc_urb(hdev, skb);
2627 if (IS_ERR(urb))
2628 return PTR_ERR(urb);
2629
2630 hdev->stat.sco_tx++;
2631 return submit_tx_urb(hdev, urb);
70405f02
LAD
2632
2633 case HCI_ISODATA_PKT:
2634 urb = alloc_bulk_urb(hdev, skb);
2635 if (IS_ERR(urb))
2636 return PTR_ERR(urb);
2637
2638 return submit_or_queue_tx_urb(hdev, urb);
cda0dd78
MH
2639 }
2640
2641 return -EILSEQ;
2642}
2643
a479e713
HW
2644static int btusb_setup_realtek(struct hci_dev *hdev)
2645{
2646 struct btusb_data *data = hci_get_drvdata(hdev);
2647 int ret;
2648
2649 ret = btrtl_setup_realtek(hdev);
2650
2651 if (btrealtek_test_flag(data->hdev, REALTEK_ALT6_CONTINUOUS_TX_CHIP))
2652 set_bit(BTUSB_ALT6_CONTINUOUS_TX, &data->flags);
2653
2654 return ret;
2655}
2656
044014ce
HW
2657static int btusb_recv_event_realtek(struct hci_dev *hdev, struct sk_buff *skb)
2658{
2659 if (skb->data[0] == HCI_VENDOR_PKT && skb->data[2] == RTK_SUB_EVENT_CODE_COREDUMP) {
2660 struct rtk_dev_coredump_hdr hdr = {
2661 .code = RTK_DEVCOREDUMP_CODE_MEMDUMP,
2662 };
2663
2664 bt_dev_dbg(hdev, "RTL: received coredump vendor evt, len %u",
2665 skb->len);
2666
2667 btusb_rtl_alloc_devcoredump(hdev, &hdr, skb->data, skb->len);
2668 kfree_skb(skb);
2669
2670 return 0;
2671 }
2672
2673 return hci_recv_frame(hdev, skb);
2674}
2675
ceac1cb0
CL
2676static void btusb_mtk_claim_iso_intf(struct btusb_data *data)
2677{
2678 struct btmtk_data *btmtk_data = hci_get_priv(data->hdev);
2679 int err;
2680
e9087e82
DA
2681 /*
2682 * The function usb_driver_claim_interface() is documented to need
2683 * locks held if it's not called from a probe routine. The code here
2684 * is called from the hci_power_on workqueue, so grab the lock.
2685 */
2686 device_lock(&btmtk_data->isopkt_intf->dev);
ceac1cb0
CL
2687 err = usb_driver_claim_interface(&btusb_driver,
2688 btmtk_data->isopkt_intf, data);
e9087e82 2689 device_unlock(&btmtk_data->isopkt_intf->dev);
ceac1cb0
CL
2690 if (err < 0) {
2691 btmtk_data->isopkt_intf = NULL;
50bda84e 2692 bt_dev_err(data->hdev, "Failed to claim iso interface: %d", err);
ceac1cb0
CL
2693 return;
2694 }
2695
2696 set_bit(BTMTK_ISOPKT_OVER_INTR, &btmtk_data->flags);
61c5a3de 2697 init_usb_anchor(&btmtk_data->isopkt_anchor);
ceac1cb0
CL
2698}
2699
489304e6 2700static void btusb_mtk_release_iso_intf(struct hci_dev *hdev)
ceac1cb0 2701{
489304e6 2702 struct btmtk_data *btmtk_data = hci_get_priv(hdev);
ceac1cb0 2703
defc33b5 2704 if (test_bit(BTMTK_ISOPKT_OVER_INTR, &btmtk_data->flags)) {
ceac1cb0
CL
2705 usb_kill_anchored_urbs(&btmtk_data->isopkt_anchor);
2706 clear_bit(BTMTK_ISOPKT_RUNNING, &btmtk_data->flags);
2707
2708 dev_kfree_skb_irq(btmtk_data->isopkt_skb);
2709 btmtk_data->isopkt_skb = NULL;
2710 usb_set_intfdata(btmtk_data->isopkt_intf, NULL);
2711 usb_driver_release_interface(&btusb_driver,
2712 btmtk_data->isopkt_intf);
2713 }
2714
2715 clear_bit(BTMTK_ISOPKT_OVER_INTR, &btmtk_data->flags);
2716}
2717
489304e6
CL
2718static int btusb_mtk_disconnect(struct hci_dev *hdev)
2719{
2720 /* This function describes the specific additional steps taken by MediaTek
2721 * when Bluetooth usb driver's resume function is called.
2722 */
2723 btusb_mtk_release_iso_intf(hdev);
2724
2725 return 0;
2726}
2727
4c0c28f2
HQ
2728static int btusb_mtk_reset(struct hci_dev *hdev, void *rst_data)
2729{
2730 struct btusb_data *data = hci_get_drvdata(hdev);
d3e62360 2731 struct btmtk_data *btmtk_data = hci_get_priv(hdev);
4c0c28f2
HQ
2732 int err;
2733
2734 /* It's MediaTek specific bluetooth reset mechanism via USB */
5c5e8c52 2735 if (test_and_set_bit(BTMTK_HW_RESET_ACTIVE, &btmtk_data->flags)) {
4c0c28f2
HQ
2736 bt_dev_err(hdev, "last reset failed? Not resetting again");
2737 return -EBUSY;
2738 }
25b6d759 2739
4c0c28f2
HQ
2740 err = usb_autopm_get_interface(data->intf);
2741 if (err < 0)
2742 return err;
2743
defc33b5
CL
2744 /* Release MediaTek ISO data interface */
2745 btusb_mtk_release_iso_intf(hdev);
ceac1cb0 2746
4c0c28f2
HQ
2747 btusb_stop_traffic(data);
2748 usb_kill_anchored_urbs(&data->tx_anchor);
4c0c28f2 2749
5c5e8c52 2750 err = btmtk_usb_subsys_reset(hdev, btmtk_data->dev_id);
4c0c28f2
HQ
2751
2752 usb_queue_reset_device(data->intf);
5c5e8c52 2753 clear_bit(BTMTK_HW_RESET_ACTIVE, &btmtk_data->flags);
25b6d759
JC
2754
2755 return err;
2756}
2757
ceac1cb0
CL
2758static int btusb_send_frame_mtk(struct hci_dev *hdev, struct sk_buff *skb)
2759{
2760 struct urb *urb;
2761
2762 BT_DBG("%s", hdev->name);
2763
2764 if (hci_skb_pkt_type(skb) == HCI_ISODATA_PKT) {
2765 urb = alloc_mtk_intr_urb(hdev, skb, btusb_tx_complete);
2766 if (IS_ERR(urb))
2767 return PTR_ERR(urb);
2768
2769 return submit_or_queue_tx_urb(hdev, urb);
2770 } else {
2771 return btusb_send_frame(hdev, skb);
2772 }
2773}
2774
a1c49c43
SW
2775static int btusb_mtk_setup(struct hci_dev *hdev)
2776{
2777 struct btusb_data *data = hci_get_drvdata(hdev);
5c5e8c52 2778 struct btmtk_data *btmtk_data = hci_get_priv(hdev);
a1c49c43 2779
5c5e8c52
CL
2780 /* MediaTek WMT vendor cmd requiring below USB resources to
2781 * complete the handshake.
2782 */
2783 btmtk_data->drv_name = btusb_driver.name;
2784 btmtk_data->intf = data->intf;
2785 btmtk_data->udev = data->udev;
2786 btmtk_data->ctrl_anchor = &data->ctrl_anchor;
2787 btmtk_data->reset_sync = btusb_mtk_reset;
a1c49c43 2788
ceac1cb0 2789 /* Claim ISO data interface and endpoint */
defc33b5
CL
2790 if (!test_bit(BTMTK_ISOPKT_OVER_INTR, &btmtk_data->flags)) {
2791 btmtk_data->isopkt_intf = usb_ifnum_to_if(data->udev, MTK_ISO_IFNUM);
ceac1cb0 2792 btusb_mtk_claim_iso_intf(data);
defc33b5 2793 }
ceac1cb0 2794
5c5e8c52 2795 return btmtk_usb_setup(hdev);
a1c49c43
SW
2796}
2797
2798static int btusb_mtk_shutdown(struct hci_dev *hdev)
2799{
ad0c6f60
CL
2800 int ret;
2801
2802 ret = btmtk_usb_shutdown(hdev);
ceac1cb0 2803
defc33b5
CL
2804 /* Release MediaTek iso interface after shutdown */
2805 btusb_mtk_release_iso_intf(hdev);
ceac1cb0 2806
ad0c6f60 2807 return ret;
a1c49c43
SW
2808}
2809
a4ccc9e3
RJ
2810#ifdef CONFIG_PM
2811/* Configure an out-of-band gpio as wake-up pin, if specified in device tree */
2812static int marvell_config_oob_wake(struct hci_dev *hdev)
2813{
2814 struct sk_buff *skb;
2815 struct btusb_data *data = hci_get_drvdata(hdev);
2816 struct device *dev = &data->udev->dev;
2817 u16 pin, gap, opcode;
2818 int ret;
2819 u8 cmd[5];
2820
2821 /* Move on if no wakeup pin specified */
2822 if (of_property_read_u16(dev->of_node, "marvell,wakeup-pin", &pin) ||
2823 of_property_read_u16(dev->of_node, "marvell,wakeup-gap-ms", &gap))
2824 return 0;
2825
2826 /* Vendor specific command to configure a GPIO as wake-up pin */
2827 opcode = hci_opcode_pack(0x3F, 0x59);
2828 cmd[0] = opcode & 0xFF;
2829 cmd[1] = opcode >> 8;
2830 cmd[2] = 2; /* length of parameters that follow */
2831 cmd[3] = pin;
2832 cmd[4] = gap; /* time in ms, for which wakeup pin should be asserted */
2833
2834 skb = bt_skb_alloc(sizeof(cmd), GFP_KERNEL);
2835 if (!skb) {
d84fc2c9 2836 bt_dev_err(hdev, "%s: No memory", __func__);
a4ccc9e3
RJ
2837 return -ENOMEM;
2838 }
2839
59ae1d12 2840 skb_put_data(skb, cmd, sizeof(cmd));
a4ccc9e3
RJ
2841 hci_skb_pkt_type(skb) = HCI_COMMAND_PKT;
2842
2843 ret = btusb_send_frame(hdev, skb);
2844 if (ret) {
d84fc2c9 2845 bt_dev_err(hdev, "%s: configuration failed", __func__);
a4ccc9e3
RJ
2846 kfree_skb(skb);
2847 return ret;
2848 }
2849
2850 return 0;
2851}
2852#endif
2853
ae8df494
AK
2854static int btusb_set_bdaddr_marvell(struct hci_dev *hdev,
2855 const bdaddr_t *bdaddr)
2856{
2857 struct sk_buff *skb;
2858 u8 buf[8];
2859 long ret;
2860
2861 buf[0] = 0xfe;
2862 buf[1] = sizeof(bdaddr_t);
2863 memcpy(buf + 2, bdaddr, sizeof(bdaddr_t));
2864
2865 skb = __hci_cmd_sync(hdev, 0xfc22, sizeof(buf), buf, HCI_INIT_TIMEOUT);
2866 if (IS_ERR(skb)) {
2867 ret = PTR_ERR(skb);
2064ee33
MH
2868 bt_dev_err(hdev, "changing Marvell device address failed (%ld)",
2869 ret);
ae8df494
AK
2870 return ret;
2871 }
2872 kfree_skb(skb);
2873
2874 return 0;
2875}
2876
5859223e
TK
2877static int btusb_set_bdaddr_ath3012(struct hci_dev *hdev,
2878 const bdaddr_t *bdaddr)
2879{
2880 struct sk_buff *skb;
2881 u8 buf[10];
2882 long ret;
2883
2884 buf[0] = 0x01;
2885 buf[1] = 0x01;
2886 buf[2] = 0x00;
2887 buf[3] = sizeof(bdaddr_t);
2888 memcpy(buf + 4, bdaddr, sizeof(bdaddr_t));
2889
2890 skb = __hci_cmd_sync(hdev, 0xfc0b, sizeof(buf), buf, HCI_INIT_TIMEOUT);
2891 if (IS_ERR(skb)) {
2892 ret = PTR_ERR(skb);
2064ee33 2893 bt_dev_err(hdev, "Change address command failed (%ld)", ret);
5859223e
TK
2894 return ret;
2895 }
2896 kfree_skb(skb);
2897
2898 return 0;
2899}
2900
b40f58b9
RL
2901static int btusb_set_bdaddr_wcn6855(struct hci_dev *hdev,
2902 const bdaddr_t *bdaddr)
2903{
2904 struct sk_buff *skb;
2905 u8 buf[6];
2906 long ret;
2907
2908 memcpy(buf, bdaddr, sizeof(bdaddr_t));
2909
2910 skb = __hci_cmd_sync_ev(hdev, 0xfc14, sizeof(buf), buf,
2911 HCI_EV_CMD_COMPLETE, HCI_INIT_TIMEOUT);
2912 if (IS_ERR(skb)) {
2913 ret = PTR_ERR(skb);
2914 bt_dev_err(hdev, "Change address command failed (%ld)", ret);
2915 return ret;
2916 }
2917 kfree_skb(skb);
2918
2919 return 0;
2920}
2921
20981ce2
TJ
2922#define QCA_MEMDUMP_ACL_HANDLE 0x2EDD
2923#define QCA_MEMDUMP_SIZE_MAX 0x100000
2924#define QCA_MEMDUMP_VSE_CLASS 0x01
2925#define QCA_MEMDUMP_MSG_TYPE 0x08
2926#define QCA_MEMDUMP_PKT_SIZE 248
2927#define QCA_LAST_SEQUENCE_NUM 0xffff
2928
2929struct qca_dump_hdr {
2930 u8 vse_class;
2931 u8 msg_type;
2932 __le16 seqno;
2933 u8 reserved;
2934 union {
2935 u8 data[0];
2936 struct {
2937 __le32 ram_dump_size;
2938 u8 data0[0];
2939 } __packed;
2940 };
2941} __packed;
2942
2943
2944static void btusb_dump_hdr_qca(struct hci_dev *hdev, struct sk_buff *skb)
2945{
2946 char buf[128];
2947 struct btusb_data *btdata = hci_get_drvdata(hdev);
2948
2949 snprintf(buf, sizeof(buf), "Controller Name: 0x%x\n",
2950 btdata->qca_dump.controller_id);
2951 skb_put_data(skb, buf, strlen(buf));
2952
2953 snprintf(buf, sizeof(buf), "Firmware Version: 0x%x\n",
2954 btdata->qca_dump.fw_version);
2955 skb_put_data(skb, buf, strlen(buf));
2956
2957 snprintf(buf, sizeof(buf), "Driver: %s\nVendor: qca\n",
2958 btusb_driver.name);
2959 skb_put_data(skb, buf, strlen(buf));
2960
2961 snprintf(buf, sizeof(buf), "VID: 0x%x\nPID:0x%x\n",
2962 btdata->qca_dump.id_vendor, btdata->qca_dump.id_product);
2963 skb_put_data(skb, buf, strlen(buf));
2964
2965 snprintf(buf, sizeof(buf), "Lmp Subversion: 0x%x\n",
2966 hdev->lmp_subver);
2967 skb_put_data(skb, buf, strlen(buf));
2968}
2969
2970static void btusb_coredump_qca(struct hci_dev *hdev)
2971{
b23d98d4 2972 int err;
20981ce2 2973 static const u8 param[] = { 0x26 };
20981ce2 2974
b23d98d4
ZH
2975 err = __hci_cmd_send(hdev, 0xfc0c, 1, param);
2976 if (err < 0)
2977 bt_dev_err(hdev, "%s: triggle crash failed (%d)", __func__, err);
20981ce2
TJ
2978}
2979
0317b033 2980/* Return: 0 on success, negative errno on failure. */
20981ce2
TJ
2981static int handle_dump_pkt_qca(struct hci_dev *hdev, struct sk_buff *skb)
2982{
0317b033 2983 int ret = 0;
c27046c9 2984 unsigned int skip = 0;
20981ce2 2985 u8 pkt_type;
20981ce2
TJ
2986 u16 seqno;
2987 u32 dump_size;
2988
20981ce2
TJ
2989 struct qca_dump_hdr *dump_hdr;
2990 struct btusb_data *btdata = hci_get_drvdata(hdev);
2991 struct usb_device *udev = btdata->udev;
2992
2993 pkt_type = hci_skb_pkt_type(skb);
c27046c9
EWW
2994 skip = sizeof(struct hci_event_hdr);
2995 if (pkt_type == HCI_ACLDATA_PKT)
2996 skip += sizeof(struct hci_acl_hdr);
20981ce2 2997
c27046c9
EWW
2998 skb_pull(skb, skip);
2999 dump_hdr = (struct qca_dump_hdr *)skb->data;
20981ce2 3000
20981ce2
TJ
3001 seqno = le16_to_cpu(dump_hdr->seqno);
3002 if (seqno == 0) {
3003 set_bit(BTUSB_HW_SSR_ACTIVE, &btdata->flags);
3004 dump_size = le32_to_cpu(dump_hdr->ram_dump_size);
3005 if (!dump_size || (dump_size > QCA_MEMDUMP_SIZE_MAX)) {
3006 ret = -EILSEQ;
3007 bt_dev_err(hdev, "Invalid memdump size(%u)",
3008 dump_size);
3009 goto out;
3010 }
3011
3012 ret = hci_devcd_init(hdev, dump_size);
3013 if (ret < 0) {
3014 bt_dev_err(hdev, "memdump init error(%d)", ret);
3015 goto out;
3016 }
3017
3018 btdata->qca_dump.ram_dump_size = dump_size;
3019 btdata->qca_dump.ram_dump_seqno = 0;
c27046c9
EWW
3020
3021 skb_pull(skb, offsetof(struct qca_dump_hdr, data0));
20981ce2
TJ
3022
3023 usb_disable_autosuspend(udev);
3024 bt_dev_info(hdev, "%s memdump size(%u)\n",
3025 (pkt_type == HCI_ACLDATA_PKT) ? "ACL" : "event",
3026 dump_size);
3027 } else {
c27046c9 3028 skb_pull(skb, offsetof(struct qca_dump_hdr, data));
20981ce2
TJ
3029 }
3030
3031 if (!btdata->qca_dump.ram_dump_size) {
3032 ret = -EINVAL;
3033 bt_dev_err(hdev, "memdump is not active");
3034 goto out;
3035 }
3036
3037 if ((seqno > btdata->qca_dump.ram_dump_seqno + 1) && (seqno != QCA_LAST_SEQUENCE_NUM)) {
3038 dump_size = QCA_MEMDUMP_PKT_SIZE * (seqno - btdata->qca_dump.ram_dump_seqno - 1);
3039 hci_devcd_append_pattern(hdev, 0x0, dump_size);
3040 bt_dev_err(hdev,
3041 "expected memdump seqno(%u) is not received(%u)\n",
3042 btdata->qca_dump.ram_dump_seqno, seqno);
3043 btdata->qca_dump.ram_dump_seqno = seqno;
3044 kfree_skb(skb);
3045 return ret;
3046 }
3047
20981ce2
TJ
3048 hci_devcd_append(hdev, skb);
3049 btdata->qca_dump.ram_dump_seqno++;
3050 if (seqno == QCA_LAST_SEQUENCE_NUM) {
3051 bt_dev_info(hdev,
3052 "memdump done: pkts(%u), total(%u)\n",
3053 btdata->qca_dump.ram_dump_seqno, btdata->qca_dump.ram_dump_size);
3054
3055 hci_devcd_complete(hdev);
3056 goto out;
3057 }
3058 return ret;
3059
3060out:
3061 if (btdata->qca_dump.ram_dump_size)
3062 usb_enable_autosuspend(udev);
3063 btdata->qca_dump.ram_dump_size = 0;
3064 btdata->qca_dump.ram_dump_seqno = 0;
3065 clear_bit(BTUSB_HW_SSR_ACTIVE, &btdata->flags);
3066
3067 if (ret < 0)
3068 kfree_skb(skb);
3069 return ret;
3070}
3071
0317b033
EWW
3072/* Return: true if the ACL packet is a dump packet, false otherwise. */
3073static bool acl_pkt_is_dump_qca(struct hci_dev *hdev, struct sk_buff *skb)
3074{
0317b033
EWW
3075 struct hci_event_hdr *event_hdr;
3076 struct hci_acl_hdr *acl_hdr;
3077 struct qca_dump_hdr *dump_hdr;
c27046c9
EWW
3078 struct sk_buff *clone = skb_clone(skb, GFP_ATOMIC);
3079 bool is_dump = false;
0317b033 3080
c27046c9 3081 if (!clone)
0317b033
EWW
3082 return false;
3083
c27046c9
EWW
3084 acl_hdr = skb_pull_data(clone, sizeof(*acl_hdr));
3085 if (!acl_hdr || (le16_to_cpu(acl_hdr->handle) != QCA_MEMDUMP_ACL_HANDLE))
3086 goto out;
0317b033 3087
c27046c9
EWW
3088 event_hdr = skb_pull_data(clone, sizeof(*event_hdr));
3089 if (!event_hdr || (event_hdr->evt != HCI_VENDOR_PKT))
3090 goto out;
0317b033 3091
c27046c9
EWW
3092 dump_hdr = skb_pull_data(clone, sizeof(*dump_hdr));
3093 if (!dump_hdr || (dump_hdr->vse_class != QCA_MEMDUMP_VSE_CLASS) ||
3094 (dump_hdr->msg_type != QCA_MEMDUMP_MSG_TYPE))
3095 goto out;
0317b033 3096
c27046c9
EWW
3097 is_dump = true;
3098out:
3099 consume_skb(clone);
3100 return is_dump;
0317b033
EWW
3101}
3102
3103/* Return: true if the event packet is a dump packet, false otherwise. */
3104static bool evt_pkt_is_dump_qca(struct hci_dev *hdev, struct sk_buff *skb)
3105{
0317b033
EWW
3106 struct hci_event_hdr *event_hdr;
3107 struct qca_dump_hdr *dump_hdr;
c27046c9
EWW
3108 struct sk_buff *clone = skb_clone(skb, GFP_ATOMIC);
3109 bool is_dump = false;
0317b033 3110
c27046c9 3111 if (!clone)
0317b033
EWW
3112 return false;
3113
c27046c9
EWW
3114 event_hdr = skb_pull_data(clone, sizeof(*event_hdr));
3115 if (!event_hdr || (event_hdr->evt != HCI_VENDOR_PKT))
3116 goto out;
0317b033 3117
c27046c9
EWW
3118 dump_hdr = skb_pull_data(clone, sizeof(*dump_hdr));
3119 if (!dump_hdr || (dump_hdr->vse_class != QCA_MEMDUMP_VSE_CLASS) ||
3120 (dump_hdr->msg_type != QCA_MEMDUMP_MSG_TYPE))
3121 goto out;
0317b033 3122
c27046c9
EWW
3123 is_dump = true;
3124out:
3125 consume_skb(clone);
3126 return is_dump;
0317b033
EWW
3127}
3128
20981ce2
TJ
3129static int btusb_recv_acl_qca(struct hci_dev *hdev, struct sk_buff *skb)
3130{
0317b033
EWW
3131 if (acl_pkt_is_dump_qca(hdev, skb))
3132 return handle_dump_pkt_qca(hdev, skb);
20981ce2
TJ
3133 return hci_recv_frame(hdev, skb);
3134}
3135
3136static int btusb_recv_evt_qca(struct hci_dev *hdev, struct sk_buff *skb)
3137{
0317b033
EWW
3138 if (evt_pkt_is_dump_qca(hdev, skb))
3139 return handle_dump_pkt_qca(hdev, skb);
20981ce2
TJ
3140 return hci_recv_frame(hdev, skb);
3141}
3142
3143
3267c884
KBYT
3144#define QCA_DFU_PACKET_LEN 4096
3145
3146#define QCA_GET_TARGET_VERSION 0x09
3147#define QCA_CHECK_STATUS 0x05
3148#define QCA_DFU_DOWNLOAD 0x01
3149
3150#define QCA_SYSCFG_UPDATED 0x40
3151#define QCA_PATCH_UPDATED 0x80
3152#define QCA_DFU_TIMEOUT 3000
ef2862a1 3153#define QCA_FLAG_MULTI_NVM 0x80
599ece4f 3154#define QCA_BT_RESET_WAIT_MS 100
3267c884 3155
d6cba4e6
TJ
3156#define WCN6855_2_0_RAM_VERSION_GF 0x400c1200
3157#define WCN6855_2_1_RAM_VERSION_GF 0x400c1211
3158
3267c884
KBYT
3159struct qca_version {
3160 __le32 rom_version;
3161 __le32 patch_version;
3162 __le32 ram_version;
741268ad 3163 __u8 chip_id;
3164 __u8 platform_id;
ef2862a1 3165 __le16 flag;
3267c884
KBYT
3166 __u8 reserved[4];
3167} __packed;
3168
3169struct qca_rampatch_version {
b40f58b9
RL
3170 __le16 rom_version_high;
3171 __le16 rom_version_low;
3267c884
KBYT
3172 __le16 patch_version;
3173} __packed;
3174
3175struct qca_device_info {
bf906b3d
KBYT
3176 u32 rom_version;
3177 u8 rampatch_hdr; /* length of header in rampatch */
3178 u8 nvm_hdr; /* length of header in NVM */
3179 u8 ver_offset; /* offset of version structure in rampatch */
3267c884
KBYT
3180};
3181
3182static const struct qca_device_info qca_devices_table[] = {
b40f58b9
RL
3183 { 0x00000100, 20, 4, 8 }, /* Rome 1.0 */
3184 { 0x00000101, 20, 4, 8 }, /* Rome 1.1 */
3185 { 0x00000200, 28, 4, 16 }, /* Rome 2.0 */
3186 { 0x00000201, 28, 4, 16 }, /* Rome 2.1 */
3187 { 0x00000300, 28, 4, 16 }, /* Rome 3.0 */
3188 { 0x00000302, 28, 4, 16 }, /* Rome 3.2 */
3189 { 0x00130100, 40, 4, 16 }, /* WCN6855 1.0 */
3190 { 0x00130200, 40, 4, 16 }, /* WCN6855 2.0 */
d6cba4e6 3191 { 0x00130201, 40, 4, 16 }, /* WCN6855 2.1 */
46225947 3192 { 0x00190200, 40, 4, 16 }, /* WCN785x 2.0 */
3267c884
KBYT
3193};
3194
803cdb8c 3195static int btusb_qca_send_vendor_req(struct usb_device *udev, u8 request,
3267c884
KBYT
3196 void *data, u16 size)
3197{
3267c884
KBYT
3198 int pipe, err;
3199 u8 *buf;
3200
3201 buf = kmalloc(size, GFP_KERNEL);
3202 if (!buf)
3203 return -ENOMEM;
3204
3205 /* Found some of USB hosts have IOT issues with ours so that we should
3206 * not wait until HCI layer is ready.
3207 */
3208 pipe = usb_rcvctrlpipe(udev, 0);
3209 err = usb_control_msg(udev, pipe, request, USB_TYPE_VENDOR | USB_DIR_IN,
c48439fc 3210 0, 0, buf, size, USB_CTRL_GET_TIMEOUT);
3267c884 3211 if (err < 0) {
803cdb8c 3212 dev_err(&udev->dev, "Failed to access otp area (%d)", err);
3267c884
KBYT
3213 goto done;
3214 }
3215
3216 memcpy(data, buf, size);
3217
3218done:
3219 kfree(buf);
3220
3221 return err;
3222}
3223
3224static int btusb_setup_qca_download_fw(struct hci_dev *hdev,
3225 const struct firmware *firmware,
3226 size_t hdr_size)
3227{
3228 struct btusb_data *btdata = hci_get_drvdata(hdev);
3229 struct usb_device *udev = btdata->udev;
3230 size_t count, size, sent = 0;
3231 int pipe, len, err;
3232 u8 *buf;
3233
3234 buf = kmalloc(QCA_DFU_PACKET_LEN, GFP_KERNEL);
3235 if (!buf)
3236 return -ENOMEM;
3237
3238 count = firmware->size;
3239
3240 size = min_t(size_t, count, hdr_size);
3241 memcpy(buf, firmware->data, size);
3242
3243 /* USB patches should go down to controller through USB path
3244 * because binary format fits to go down through USB channel.
3245 * USB control path is for patching headers and USB bulk is for
3246 * patch body.
3247 */
3248 pipe = usb_sndctrlpipe(udev, 0);
3249 err = usb_control_msg(udev, pipe, QCA_DFU_DOWNLOAD, USB_TYPE_VENDOR,
3250 0, 0, buf, size, USB_CTRL_SET_TIMEOUT);
3251 if (err < 0) {
2064ee33 3252 bt_dev_err(hdev, "Failed to send headers (%d)", err);
3267c884
KBYT
3253 goto done;
3254 }
3255
3256 sent += size;
3257 count -= size;
3258
4f00bfb3
TJ
3259 /* ep2 need time to switch from function acl to function dfu,
3260 * so we add 20ms delay here.
3261 */
3262 msleep(20);
3263
3267c884
KBYT
3264 while (count) {
3265 size = min_t(size_t, count, QCA_DFU_PACKET_LEN);
3266
3267 memcpy(buf, firmware->data + sent, size);
3268
3269 pipe = usb_sndbulkpipe(udev, 0x02);
3270 err = usb_bulk_msg(udev, pipe, buf, size, &len,
3271 QCA_DFU_TIMEOUT);
3272 if (err < 0) {
2064ee33
MH
3273 bt_dev_err(hdev, "Failed to send body at %zd of %zd (%d)",
3274 sent, firmware->size, err);
3267c884
KBYT
3275 break;
3276 }
3277
3278 if (size != len) {
2064ee33 3279 bt_dev_err(hdev, "Failed to get bulk buffer");
3267c884
KBYT
3280 err = -EILSEQ;
3281 break;
3282 }
3283
3284 sent += size;
3285 count -= size;
3286 }
3287
3288done:
3289 kfree(buf);
3290 return err;
3291}
3292
3293static int btusb_setup_qca_load_rampatch(struct hci_dev *hdev,
3294 struct qca_version *ver,
3295 const struct qca_device_info *info)
3296{
3297 struct qca_rampatch_version *rver;
3298 const struct firmware *fw;
b40f58b9
RL
3299 u32 ver_rom, ver_patch, rver_rom;
3300 u16 rver_rom_low, rver_rom_high, rver_patch;
3267c884
KBYT
3301 char fwname[64];
3302 int err;
3303
bf906b3d
KBYT
3304 ver_rom = le32_to_cpu(ver->rom_version);
3305 ver_patch = le32_to_cpu(ver->patch_version);
3306
3307 snprintf(fwname, sizeof(fwname), "qca/rampatch_usb_%08x.bin", ver_rom);
3267c884
KBYT
3308
3309 err = request_firmware(&fw, fwname, &hdev->dev);
3310 if (err) {
2064ee33
MH
3311 bt_dev_err(hdev, "failed to request rampatch file: %s (%d)",
3312 fwname, err);
3267c884
KBYT
3313 return err;
3314 }
3315
2064ee33 3316 bt_dev_info(hdev, "using rampatch file: %s", fwname);
bf906b3d 3317
3267c884 3318 rver = (struct qca_rampatch_version *)(fw->data + info->ver_offset);
b40f58b9 3319 rver_rom_low = le16_to_cpu(rver->rom_version_low);
bf906b3d
KBYT
3320 rver_patch = le16_to_cpu(rver->patch_version);
3321
b40f58b9
RL
3322 if (ver_rom & ~0xffffU) {
3323 rver_rom_high = le16_to_cpu(rver->rom_version_high);
42d3b43e 3324 rver_rom = rver_rom_high << 16 | rver_rom_low;
b40f58b9
RL
3325 } else {
3326 rver_rom = rver_rom_low;
3327 }
3328
2064ee33
MH
3329 bt_dev_info(hdev, "QCA: patch rome 0x%x build 0x%x, "
3330 "firmware rome 0x%x build 0x%x",
3331 rver_rom, rver_patch, ver_rom, ver_patch);
3267c884 3332
bf906b3d 3333 if (rver_rom != ver_rom || rver_patch <= ver_patch) {
2064ee33 3334 bt_dev_err(hdev, "rampatch file version did not match with firmware");
3267c884
KBYT
3335 err = -EINVAL;
3336 goto done;
3337 }
3338
3339 err = btusb_setup_qca_download_fw(hdev, fw, info->rampatch_hdr);
3340
3341done:
3342 release_firmware(fw);
3343
3344 return err;
3345}
3346
d6cba4e6
TJ
3347static void btusb_generate_qca_nvm_name(char *fwname, size_t max_size,
3348 const struct qca_version *ver)
3349{
3350 u32 rom_version = le32_to_cpu(ver->rom_version);
3351 u16 flag = le16_to_cpu(ver->flag);
3352
3353 if (((flag >> 8) & 0xff) == QCA_FLAG_MULTI_NVM) {
741268ad 3354 /* The board_id should be split into two bytes
3355 * The 1st byte is chip ID, and the 2nd byte is platform ID
3356 * For example, board ID 0x010A, 0x01 is platform ID. 0x0A is chip ID
21a241b3 3357 * we have several platforms, and platform IDs are continuously added
741268ad 3358 * Platform ID:
3359 * 0x00 is for Mobile
3360 * 0x01 is for X86
3361 * 0x02 is for Automotive
3362 * 0x03 is for Consumer electronic
3363 */
3364 u16 board_id = (ver->chip_id << 8) + ver->platform_id;
d6cba4e6
TJ
3365 const char *variant;
3366
3367 switch (le32_to_cpu(ver->ram_version)) {
3368 case WCN6855_2_0_RAM_VERSION_GF:
3369 case WCN6855_2_1_RAM_VERSION_GF:
3370 variant = "_gf";
3371 break;
3372 default:
3373 variant = "";
3374 break;
3375 }
3376
3377 if (board_id == 0) {
3378 snprintf(fwname, max_size, "qca/nvm_usb_%08x%s.bin",
3379 rom_version, variant);
3380 } else {
3381 snprintf(fwname, max_size, "qca/nvm_usb_%08x%s_%04x.bin",
3382 rom_version, variant, board_id);
3383 }
3384 } else {
3385 snprintf(fwname, max_size, "qca/nvm_usb_%08x.bin",
3386 rom_version);
3387 }
3388
3389}
3390
3267c884
KBYT
3391static int btusb_setup_qca_load_nvm(struct hci_dev *hdev,
3392 struct qca_version *ver,
3393 const struct qca_device_info *info)
3394{
3395 const struct firmware *fw;
3396 char fwname[64];
3397 int err;
3398
d6cba4e6 3399 btusb_generate_qca_nvm_name(fwname, sizeof(fwname), ver);
3267c884
KBYT
3400
3401 err = request_firmware(&fw, fwname, &hdev->dev);
3402 if (err) {
2064ee33
MH
3403 bt_dev_err(hdev, "failed to request NVM file: %s (%d)",
3404 fwname, err);
3267c884
KBYT
3405 return err;
3406 }
3407
2064ee33 3408 bt_dev_info(hdev, "using NVM file: %s", fwname);
3267c884
KBYT
3409
3410 err = btusb_setup_qca_download_fw(hdev, fw, info->nvm_hdr);
3411
3412 release_firmware(fw);
3413
3414 return err;
3415}
3416
803cdb8c
TI
3417/* identify the ROM version and check whether patches are needed */
3418static bool btusb_qca_need_patch(struct usb_device *udev)
3419{
3420 struct qca_version ver;
3421
3422 if (btusb_qca_send_vendor_req(udev, QCA_GET_TARGET_VERSION, &ver,
3423 sizeof(ver)) < 0)
3424 return false;
3425 /* only low ROM versions need patches */
3426 return !(le32_to_cpu(ver.rom_version) & ~0xffffU);
3427}
3428
3267c884
KBYT
3429static int btusb_setup_qca(struct hci_dev *hdev)
3430{
803cdb8c
TI
3431 struct btusb_data *btdata = hci_get_drvdata(hdev);
3432 struct usb_device *udev = btdata->udev;
3267c884
KBYT
3433 const struct qca_device_info *info = NULL;
3434 struct qca_version ver;
bf906b3d 3435 u32 ver_rom;
3267c884
KBYT
3436 u8 status;
3437 int i, err;
3438
803cdb8c 3439 err = btusb_qca_send_vendor_req(udev, QCA_GET_TARGET_VERSION, &ver,
eb50042f 3440 sizeof(ver));
3267c884
KBYT
3441 if (err < 0)
3442 return err;
3443
bf906b3d 3444 ver_rom = le32_to_cpu(ver.rom_version);
803cdb8c 3445
3267c884 3446 for (i = 0; i < ARRAY_SIZE(qca_devices_table); i++) {
bf906b3d 3447 if (ver_rom == qca_devices_table[i].rom_version)
3267c884
KBYT
3448 info = &qca_devices_table[i];
3449 }
3450 if (!info) {
234f414e
HW
3451 /* If the rom_version is not matched in the qca_devices_table
3452 * and the high ROM version is not zero, we assume this chip no
3453 * need to load the rampatch and nvm.
3454 */
3455 if (ver_rom & ~0xffffU)
3456 return 0;
3457
2064ee33 3458 bt_dev_err(hdev, "don't support firmware rome 0x%x", ver_rom);
3267c884
KBYT
3459 return -ENODEV;
3460 }
3461
803cdb8c 3462 err = btusb_qca_send_vendor_req(udev, QCA_CHECK_STATUS, &status,
3267c884
KBYT
3463 sizeof(status));
3464 if (err < 0)
3465 return err;
3466
3467 if (!(status & QCA_PATCH_UPDATED)) {
3468 err = btusb_setup_qca_load_rampatch(hdev, &ver, info);
3469 if (err < 0)
3470 return err;
3471 }
3472
ef2862a1
TJ
3473 err = btusb_qca_send_vendor_req(udev, QCA_GET_TARGET_VERSION, &ver,
3474 sizeof(ver));
3475 if (err < 0)
3476 return err;
3477
20981ce2
TJ
3478 btdata->qca_dump.fw_version = le32_to_cpu(ver.patch_version);
3479 btdata->qca_dump.controller_id = le32_to_cpu(ver.rom_version);
3480
3267c884
KBYT
3481 if (!(status & QCA_SYSCFG_UPDATED)) {
3482 err = btusb_setup_qca_load_nvm(hdev, &ver, info);
3483 if (err < 0)
3484 return err;
599ece4f 3485
46225947 3486 /* WCN6855 2.1 and later will reset to apply firmware downloaded here, so
599ece4f
ZH
3487 * wait ~100ms for reset Done then go ahead, otherwise, it maybe
3488 * cause potential enable failure.
3489 */
46225947 3490 if (info->rom_version >= 0x00130201)
599ece4f 3491 msleep(QCA_BT_RESET_WAIT_MS);
3267c884
KBYT
3492 }
3493
d44e1dbd
LAD
3494 /* Mark HCI_OP_ENHANCED_SETUP_SYNC_CONN as broken as it doesn't seem to
3495 * work with the likes of HSP/HFP mSBC.
3496 */
3497 set_bit(HCI_QUIRK_BROKEN_ENHANCED_SETUP_SYNC_CONN, &hdev->quirks);
3498
3267c884
KBYT
3499 return 0;
3500}
3501
9d08f504
MH
3502static inline int __set_diag_interface(struct hci_dev *hdev)
3503{
3504 struct btusb_data *data = hci_get_drvdata(hdev);
3505 struct usb_interface *intf = data->diag;
3506 int i;
3507
3508 if (!data->diag)
3509 return -ENODEV;
3510
3511 data->diag_tx_ep = NULL;
3512 data->diag_rx_ep = NULL;
3513
3514 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
3515 struct usb_endpoint_descriptor *ep_desc;
3516
3517 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
3518
3519 if (!data->diag_tx_ep && usb_endpoint_is_bulk_out(ep_desc)) {
3520 data->diag_tx_ep = ep_desc;
3521 continue;
3522 }
3523
3524 if (!data->diag_rx_ep && usb_endpoint_is_bulk_in(ep_desc)) {
3525 data->diag_rx_ep = ep_desc;
3526 continue;
3527 }
3528 }
3529
3530 if (!data->diag_tx_ep || !data->diag_rx_ep) {
2064ee33 3531 bt_dev_err(hdev, "invalid diagnostic descriptors");
9d08f504
MH
3532 return -ENODEV;
3533 }
3534
3535 return 0;
3536}
3537
3538static struct urb *alloc_diag_urb(struct hci_dev *hdev, bool enable)
3539{
3540 struct btusb_data *data = hci_get_drvdata(hdev);
3541 struct sk_buff *skb;
3542 struct urb *urb;
3543 unsigned int pipe;
3544
3545 if (!data->diag_tx_ep)
3546 return ERR_PTR(-ENODEV);
3547
3548 urb = usb_alloc_urb(0, GFP_KERNEL);
3549 if (!urb)
3550 return ERR_PTR(-ENOMEM);
3551
3552 skb = bt_skb_alloc(2, GFP_KERNEL);
3553 if (!skb) {
3554 usb_free_urb(urb);
3555 return ERR_PTR(-ENOMEM);
3556 }
3557
634fef61
JB
3558 skb_put_u8(skb, 0xf0);
3559 skb_put_u8(skb, enable);
9d08f504
MH
3560
3561 pipe = usb_sndbulkpipe(data->udev, data->diag_tx_ep->bEndpointAddress);
3562
3563 usb_fill_bulk_urb(urb, data->udev, pipe,
3564 skb->data, skb->len, btusb_tx_complete, skb);
3565
3566 skb->dev = (void *)hdev;
3567
3568 return urb;
3569}
3570
3571static int btusb_bcm_set_diag(struct hci_dev *hdev, bool enable)
3572{
3573 struct btusb_data *data = hci_get_drvdata(hdev);
3574 struct urb *urb;
3575
3576 if (!data->diag)
3577 return -ENODEV;
3578
3579 if (!test_bit(HCI_RUNNING, &hdev->flags))
3580 return -ENETDOWN;
3581
3582 urb = alloc_diag_urb(hdev, enable);
3583 if (IS_ERR(urb))
3584 return PTR_ERR(urb);
3585
3586 return submit_or_queue_tx_urb(hdev, urb);
3587}
9d08f504 3588
fd913ef7
RJ
3589#ifdef CONFIG_PM
3590static irqreturn_t btusb_oob_wake_handler(int irq, void *priv)
3591{
3592 struct btusb_data *data = priv;
3593
3594 pm_wakeup_event(&data->udev->dev, 0);
017789f3 3595 pm_system_wakeup();
fd913ef7
RJ
3596
3597 /* Disable only if not already disabled (keep it balanced) */
3598 if (test_and_clear_bit(BTUSB_OOB_WAKE_ENABLED, &data->flags)) {
3599 disable_irq_nosync(irq);
3600 disable_irq_wake(irq);
3601 }
3602 return IRQ_HANDLED;
3603}
3604
3605static const struct of_device_id btusb_match_table[] = {
3606 { .compatible = "usb1286,204e" },
4c409af0
BN
3607 { .compatible = "usbcf3,e300" }, /* QCA6174A */
3608 { .compatible = "usb4ca,301a" }, /* QCA6174A (Lite-On) */
fd913ef7
RJ
3609 { }
3610};
3611MODULE_DEVICE_TABLE(of, btusb_match_table);
3612
3613/* Use an oob wakeup pin? */
3614static int btusb_config_oob_wake(struct hci_dev *hdev)
3615{
3616 struct btusb_data *data = hci_get_drvdata(hdev);
3617 struct device *dev = &data->udev->dev;
3618 int irq, ret;
3619
3620 clear_bit(BTUSB_OOB_WAKE_ENABLED, &data->flags);
3621
3622 if (!of_match_device(btusb_match_table, dev))
3623 return 0;
3624
3625 /* Move on if no IRQ specified */
3626 irq = of_irq_get_byname(dev->of_node, "wakeup");
3627 if (irq <= 0) {
3628 bt_dev_dbg(hdev, "%s: no OOB Wakeup IRQ in DT", __func__);
3629 return 0;
3630 }
3631
771acc7e 3632 irq_set_status_flags(irq, IRQ_NOAUTOEN);
fd913ef7
RJ
3633 ret = devm_request_irq(&hdev->dev, irq, btusb_oob_wake_handler,
3634 0, "OOB Wake-on-BT", data);
3635 if (ret) {
3636 bt_dev_err(hdev, "%s: IRQ request failed", __func__);
3637 return ret;
3638 }
3639
3640 ret = device_init_wakeup(dev, true);
3641 if (ret) {
3642 bt_dev_err(hdev, "%s: failed to init_wakeup", __func__);
3643 return ret;
3644 }
3645
3646 data->oob_wake_irq = irq;
fd913ef7
RJ
3647 bt_dev_info(hdev, "OOB Wake-on-BT configured at IRQ %u", irq);
3648 return 0;
3649}
3650#endif
3651
fc549102
HG
3652static void btusb_check_needs_reset_resume(struct usb_interface *intf)
3653{
3654 if (dmi_check_system(btusb_needs_reset_resume_table))
3655 interface_to_usbdev(intf)->quirks |= USB_QUIRK_RESET_RESUME;
3656}
3657
4539ca67 3658static bool btusb_wakeup(struct hci_dev *hdev)
b7d0bf11
APS
3659{
3660 struct btusb_data *data = hci_get_drvdata(hdev);
3661
4539ca67 3662 return device_may_wakeup(&data->udev->dev);
b7d0bf11
APS
3663}
3664
3b0d5250
TJ
3665static int btusb_shutdown_qca(struct hci_dev *hdev)
3666{
3667 struct sk_buff *skb;
3668
3669 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
3670 if (IS_ERR(skb)) {
3671 bt_dev_err(hdev, "HCI reset during shutdown failed");
3672 return PTR_ERR(skb);
3673 }
3674 kfree_skb(skb);
3675
3676 return 0;
3677}
3678
800fe5ec
LAD
3679static ssize_t force_poll_sync_read(struct file *file, char __user *user_buf,
3680 size_t count, loff_t *ppos)
3681{
3682 struct btusb_data *data = file->private_data;
3683 char buf[3];
3684
3685 buf[0] = data->poll_sync ? 'Y' : 'N';
3686 buf[1] = '\n';
3687 buf[2] = '\0';
3688 return simple_read_from_buffer(user_buf, count, ppos, buf, 2);
3689}
3690
3691static ssize_t force_poll_sync_write(struct file *file,
3692 const char __user *user_buf,
3693 size_t count, loff_t *ppos)
3694{
3695 struct btusb_data *data = file->private_data;
3696 bool enable;
3697 int err;
3698
3699 err = kstrtobool_from_user(user_buf, count, &enable);
3700 if (err)
3701 return err;
3702
3703 /* Only allow changes while the adapter is down */
3704 if (test_bit(HCI_UP, &data->hdev->flags))
3705 return -EPERM;
3706
3707 if (data->poll_sync == enable)
3708 return -EALREADY;
3709
3710 data->poll_sync = enable;
3711
3712 return count;
3713}
3714
3715static const struct file_operations force_poll_sync_fops = {
cbf85b9c 3716 .owner = THIS_MODULE,
800fe5ec
LAD
3717 .open = simple_open,
3718 .read = force_poll_sync_read,
3719 .write = force_poll_sync_write,
3720 .llseek = default_llseek,
3721};
3722
7d70989f
HC
3723#define BTUSB_HCI_DRV_OP_SUPPORTED_ALTSETTINGS \
3724 hci_opcode_pack(HCI_DRV_OGF_DRIVER_SPECIFIC, 0x0000)
3725#define BTUSB_HCI_DRV_SUPPORTED_ALTSETTINGS_SIZE 0
3726struct btusb_hci_drv_rp_supported_altsettings {
3727 __u8 num;
3728 __u8 altsettings[];
3729} __packed;
3730
3731#define BTUSB_HCI_DRV_OP_SWITCH_ALTSETTING \
3732 hci_opcode_pack(HCI_DRV_OGF_DRIVER_SPECIFIC, 0x0001)
3733#define BTUSB_HCI_DRV_SWITCH_ALTSETTING_SIZE 1
3734struct btusb_hci_drv_cmd_switch_altsetting {
3735 __u8 altsetting;
3736} __packed;
3737
04425292
HC
3738static const struct {
3739 u16 opcode;
3740 const char *desc;
3741} btusb_hci_drv_supported_commands[] = {
3742 /* Common commands */
3743 { HCI_DRV_OP_READ_INFO, "Read Info" },
7d70989f
HC
3744
3745 /* Driver specific commands */
3746 { BTUSB_HCI_DRV_OP_SUPPORTED_ALTSETTINGS, "Supported Altsettings" },
3747 { BTUSB_HCI_DRV_OP_SWITCH_ALTSETTING, "Switch Altsetting" },
04425292
HC
3748};
3749static int btusb_hci_drv_read_info(struct hci_dev *hdev, void *data,
3750 u16 data_len)
3751{
3752 struct hci_drv_rp_read_info *rp;
3753 size_t rp_size;
3754 int err, i;
3755 u16 opcode, num_supported_commands =
3756 ARRAY_SIZE(btusb_hci_drv_supported_commands);
3757
3758 rp_size = sizeof(*rp) + num_supported_commands * 2;
3759
3760 rp = kmalloc(rp_size, GFP_KERNEL);
3761 if (!rp)
3762 return -ENOMEM;
3763
3764 strscpy_pad(rp->driver_name, btusb_driver.name);
3765
3766 rp->num_supported_commands = cpu_to_le16(num_supported_commands);
3767 for (i = 0; i < num_supported_commands; i++) {
3768 opcode = btusb_hci_drv_supported_commands[i].opcode;
3769 bt_dev_info(hdev,
3770 "Supported HCI Drv command (0x%02x|0x%04x): %s",
3771 hci_opcode_ogf(opcode),
3772 hci_opcode_ocf(opcode),
3773 btusb_hci_drv_supported_commands[i].desc);
3774 rp->supported_commands[i] = cpu_to_le16(opcode);
3775 }
3776
3777 err = hci_drv_cmd_complete(hdev, HCI_DRV_OP_READ_INFO,
3778 HCI_DRV_STATUS_SUCCESS, rp, rp_size);
3779
3780 kfree(rp);
3781 return err;
3782}
3783
7d70989f
HC
3784static int btusb_hci_drv_supported_altsettings(struct hci_dev *hdev, void *data,
3785 u16 data_len)
3786{
3787 struct btusb_data *drvdata = hci_get_drvdata(hdev);
3788 struct btusb_hci_drv_rp_supported_altsettings *rp;
3789 size_t rp_size;
3790 int err;
3791 u8 i;
3792
3793 /* There are at most 7 alt (0 - 6) */
3794 rp = kmalloc(sizeof(*rp) + 7, GFP_KERNEL);
3795
3796 rp->num = 0;
3797 if (!drvdata->isoc)
3798 goto done;
3799
3800 for (i = 0; i <= 6; i++) {
3801 if (btusb_find_altsetting(drvdata, i))
3802 rp->altsettings[rp->num++] = i;
3803 }
3804
3805done:
3806 rp_size = sizeof(*rp) + rp->num;
3807
3808 err = hci_drv_cmd_complete(hdev, BTUSB_HCI_DRV_OP_SUPPORTED_ALTSETTINGS,
3809 HCI_DRV_STATUS_SUCCESS, rp, rp_size);
3810 kfree(rp);
3811 return err;
3812}
3813
3814static int btusb_hci_drv_switch_altsetting(struct hci_dev *hdev, void *data,
3815 u16 data_len)
3816{
3817 struct btusb_hci_drv_cmd_switch_altsetting *cmd = data;
3818 u8 status;
3819
3820 if (cmd->altsetting > 6) {
3821 status = HCI_DRV_STATUS_INVALID_PARAMETERS;
3822 } else {
3823 if (btusb_switch_alt_setting(hdev, cmd->altsetting))
3824 status = HCI_DRV_STATUS_UNSPECIFIED_ERROR;
3825 else
3826 status = HCI_DRV_STATUS_SUCCESS;
3827 }
3828
3829 return hci_drv_cmd_status(hdev, BTUSB_HCI_DRV_OP_SWITCH_ALTSETTING,
3830 status);
3831}
3832
04425292
HC
3833static const struct hci_drv_handler btusb_hci_drv_common_handlers[] = {
3834 { btusb_hci_drv_read_info, HCI_DRV_READ_INFO_SIZE },
3835};
3836
7d70989f
HC
3837static const struct hci_drv_handler btusb_hci_drv_specific_handlers[] = {
3838 { btusb_hci_drv_supported_altsettings,
3839 BTUSB_HCI_DRV_SUPPORTED_ALTSETTINGS_SIZE },
3840 { btusb_hci_drv_switch_altsetting,
3841 BTUSB_HCI_DRV_SWITCH_ALTSETTING_SIZE },
3842};
04425292
HC
3843
3844static struct hci_drv btusb_hci_drv = {
3845 .common_handler_count = ARRAY_SIZE(btusb_hci_drv_common_handlers),
3846 .common_handlers = btusb_hci_drv_common_handlers,
3847 .specific_handler_count = ARRAY_SIZE(btusb_hci_drv_specific_handlers),
3848 .specific_handlers = btusb_hci_drv_specific_handlers,
3849};
3850
5e23b923 3851static int btusb_probe(struct usb_interface *intf,
89e7533d 3852 const struct usb_device_id *id)
5e23b923
MH
3853{
3854 struct usb_endpoint_descriptor *ep_desc;
dc786b2c 3855 struct gpio_desc *reset_gpio;
5e23b923
MH
3856 struct btusb_data *data;
3857 struct hci_dev *hdev;
22f8e9db 3858 unsigned ifnum_base;
53492a66 3859 int i, err, priv_size;
5e23b923
MH
3860
3861 BT_DBG("intf %p id %p", intf, id);
3862
c13380a5 3863 if ((id->driver_info & BTUSB_IFNUM_2) &&
95b70154 3864 (intf->cur_altsetting->desc.bInterfaceNumber != 0) &&
c13380a5
TM
3865 (intf->cur_altsetting->desc.bInterfaceNumber != 2))
3866 return -ENODEV;
22f8e9db
MH
3867
3868 ifnum_base = intf->cur_altsetting->desc.bInterfaceNumber;
5e23b923
MH
3869
3870 if (!id->driver_info) {
3871 const struct usb_device_id *match;
89e7533d 3872
d831e361 3873 match = usb_match_id(intf, quirks_table);
5e23b923
MH
3874 if (match)
3875 id = match;
3876 }
3877
cfeb4145
MH
3878 if (id->driver_info == BTUSB_IGNORE)
3879 return -ENODEV;
3880
2d25f8b4
SL
3881 if (id->driver_info & BTUSB_ATH3012) {
3882 struct usb_device *udev = interface_to_usbdev(intf);
3883
3884 /* Old firmware would otherwise let ath3k driver load
d98422cb
DR
3885 * patch and sysconfig files
3886 */
803cdb8c
TI
3887 if (le16_to_cpu(udev->descriptor.bcdDevice) <= 0x0001 &&
3888 !btusb_qca_need_patch(udev))
2d25f8b4
SL
3889 return -ENODEV;
3890 }
3891
98921dbd 3892 data = devm_kzalloc(&intf->dev, sizeof(*data), GFP_KERNEL);
5e23b923
MH
3893 if (!data)
3894 return -ENOMEM;
3895
3896 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
3897 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
3898
3899 if (!data->intr_ep && usb_endpoint_is_int_in(ep_desc)) {
3900 data->intr_ep = ep_desc;
3901 continue;
3902 }
3903
3904 if (!data->bulk_tx_ep && usb_endpoint_is_bulk_out(ep_desc)) {
3905 data->bulk_tx_ep = ep_desc;
3906 continue;
3907 }
3908
3909 if (!data->bulk_rx_ep && usb_endpoint_is_bulk_in(ep_desc)) {
3910 data->bulk_rx_ep = ep_desc;
3911 continue;
3912 }
3913 }
3914
98921dbd 3915 if (!data->intr_ep || !data->bulk_tx_ep || !data->bulk_rx_ep)
5e23b923 3916 return -ENODEV;
5e23b923 3917
893ba544
MH
3918 if (id->driver_info & BTUSB_AMP) {
3919 data->cmdreq_type = USB_TYPE_CLASS | 0x01;
3920 data->cmdreq = 0x2b;
3921 } else {
3922 data->cmdreq_type = USB_TYPE_CLASS;
3923 data->cmdreq = 0x00;
3924 }
7a9d4020 3925
5e23b923 3926 data->udev = interface_to_usbdev(intf);
5fbcd260 3927 data->intf = intf;
5e23b923 3928
5e23b923 3929 INIT_WORK(&data->work, btusb_work);
7bee549e 3930 INIT_WORK(&data->waker, btusb_waker);
800fe5ec
LAD
3931 INIT_DELAYED_WORK(&data->rx_work, btusb_rx_work);
3932
3933 skb_queue_head_init(&data->acl_q);
3934
803b5836
MH
3935 init_usb_anchor(&data->deferred);
3936 init_usb_anchor(&data->tx_anchor);
7bee549e 3937 spin_lock_init(&data->txlock);
5e23b923 3938
5e23b923
MH
3939 init_usb_anchor(&data->intr_anchor);
3940 init_usb_anchor(&data->bulk_anchor);
9bfa35fe 3941 init_usb_anchor(&data->isoc_anchor);
9d08f504 3942 init_usb_anchor(&data->diag_anchor);
a1c49c43 3943 init_usb_anchor(&data->ctrl_anchor);
803b5836 3944 spin_lock_init(&data->rxlock);
5e23b923 3945
53492a66
THJA
3946 priv_size = 0;
3947
019a1caa
THJA
3948 data->recv_event = hci_recv_frame;
3949 data->recv_bulk = btusb_recv_bulk;
3950
53492a66
THJA
3951 if (id->driver_info & BTUSB_INTEL_COMBINED) {
3952 /* Allocate extra space for Intel device */
3953 priv_size += sizeof(struct btintel_data);
53492a66 3954
019a1caa 3955 /* Override the rx handlers */
123c2631 3956 data->recv_event = btintel_recv_event;
cda0dd78 3957 data->recv_bulk = btusb_recv_bulk_intel;
5b355944
HW
3958 } else if (id->driver_info & BTUSB_REALTEK) {
3959 /* Allocate extra space for Realtek device */
3960 priv_size += sizeof(struct btrealtek_data);
044014ce
HW
3961
3962 data->recv_event = btusb_recv_event_realtek;
4c92ae75
PT
3963 } else if (id->driver_info & BTUSB_MEDIATEK) {
3964 /* Allocate extra space for Mediatek device */
d3e62360 3965 priv_size += sizeof(struct btmtk_data);
cda0dd78 3966 }
2cbd3f5c 3967
0b10c8c8 3968 data->recv_acl = hci_recv_frame;
3969
53492a66 3970 hdev = hci_alloc_dev_priv(priv_size);
98921dbd 3971 if (!hdev)
5e23b923 3972 return -ENOMEM;
5e23b923 3973
c13854ce 3974 hdev->bus = HCI_USB;
155961e8 3975 hci_set_drvdata(hdev, data);
5e23b923
MH
3976
3977 data->hdev = hdev;
3978
3979 SET_HCIDEV_DEV(hdev, &intf->dev);
3980
dc786b2c
RJ
3981 reset_gpio = gpiod_get_optional(&data->udev->dev, "reset",
3982 GPIOD_OUT_LOW);
3983 if (IS_ERR(reset_gpio)) {
3984 err = PTR_ERR(reset_gpio);
3985 goto out_free_dev;
3986 } else if (reset_gpio) {
3987 data->reset_gpio = reset_gpio;
3988 }
3989
04425292
HC
3990 hdev->open = btusb_open;
3991 hdev->close = btusb_close;
3992 hdev->flush = btusb_flush;
3993 hdev->send = btusb_send_frame;
3994 hdev->notify = btusb_notify;
3995 hdev->wakeup = btusb_wakeup;
3996 hdev->hci_drv = &btusb_hci_drv;
9f8f962c 3997
fd913ef7
RJ
3998#ifdef CONFIG_PM
3999 err = btusb_config_oob_wake(hdev);
4000 if (err)
4001 goto out_free_dev;
a4ccc9e3
RJ
4002
4003 /* Marvell devices may need a specific chip configuration */
4004 if (id->driver_info & BTUSB_MARVELL && data->oob_wake_irq) {
4005 err = marvell_config_oob_wake(hdev);
4006 if (err)
4007 goto out_free_dev;
4008 }
fd913ef7 4009#endif
418678b0
SJ
4010 if (id->driver_info & BTUSB_CW6622)
4011 set_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks);
4012
6c9d435d
MH
4013 if (id->driver_info & BTUSB_BCM2045)
4014 set_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks);
4015
9f8f962c
MH
4016 if (id->driver_info & BTUSB_BCM92035)
4017 hdev->setup = btusb_setup_bcm92035;
5e23b923 4018
c0a21a52
MH
4019 if (IS_ENABLED(CONFIG_BT_HCIBTUSB_BCM) &&
4020 (id->driver_info & BTUSB_BCM_PATCHRAM)) {
49a5f782 4021 hdev->manufacturer = 15;
c2bfb100 4022 hdev->setup = btbcm_setup_patchram;
9d08f504 4023 hdev->set_diag = btusb_bcm_set_diag;
1df1f591 4024 hdev->set_bdaddr = btbcm_set_bdaddr;
9d08f504
MH
4025
4026 /* Broadcom LM_DIAG Interface numbers are hardcoded */
22f8e9db 4027 data->diag = usb_ifnum_to_if(data->udev, ifnum_base + 2);
abbaf50e 4028 }
10d4c673 4029
c0a21a52
MH
4030 if (IS_ENABLED(CONFIG_BT_HCIBTUSB_BCM) &&
4031 (id->driver_info & BTUSB_BCM_APPLE)) {
49a5f782 4032 hdev->manufacturer = 15;
c2bfb100 4033 hdev->setup = btbcm_setup_apple;
9d08f504
MH
4034 hdev->set_diag = btusb_bcm_set_diag;
4035
4036 /* Broadcom LM_DIAG Interface numbers are hardcoded */
22f8e9db 4037 data->diag = usb_ifnum_to_if(data->udev, ifnum_base + 2);
9d08f504 4038 }
17b2772b 4039
83f2dafe
THJA
4040 /* Combined Intel Device setup to support multiple setup routine */
4041 if (id->driver_info & BTUSB_INTEL_COMBINED) {
af395330 4042 err = btintel_configure_setup(hdev, btusb_driver.name);
83f2dafe
THJA
4043 if (err)
4044 goto out_free_dev;
4045
4046 /* Transport specific configuration */
019a1caa 4047 hdev->send = btusb_send_frame_intel;
f07d4780 4048 hdev->reset = btusb_intel_reset;
83f2dafe 4049
55235304
THJA
4050 if (id->driver_info & BTUSB_INTEL_NO_WBS_SUPPORT)
4051 btintel_set_flag(hdev, INTEL_ROM_LEGACY_NO_WBS_SUPPORT);
4052
ea7c4c0e
THJA
4053 if (id->driver_info & BTUSB_INTEL_BROKEN_INITIAL_NCMD)
4054 btintel_set_flag(hdev, INTEL_BROKEN_INITIAL_NCMD);
95655456
THJA
4055
4056 if (id->driver_info & BTUSB_INTEL_BROKEN_SHUTDOWN_LED)
4057 btintel_set_flag(hdev, INTEL_BROKEN_SHUTDOWN_LED);
cb8d6597 4058 }
dffd30ee 4059
ae8df494
AK
4060 if (id->driver_info & BTUSB_MARVELL)
4061 hdev->set_bdaddr = btusb_set_bdaddr_marvell;
4062
c0a21a52
MH
4063 if (IS_ENABLED(CONFIG_BT_HCIBTUSB_MTK) &&
4064 (id->driver_info & BTUSB_MEDIATEK)) {
a1c49c43
SW
4065 hdev->setup = btusb_mtk_setup;
4066 hdev->shutdown = btusb_mtk_shutdown;
4067 hdev->manufacturer = 70;
f07d4780 4068 hdev->reset = btmtk_reset_sync;
8c0d17b6 4069 hdev->set_bdaddr = btmtk_set_bdaddr;
ceac1cb0 4070 hdev->send = btusb_send_frame_mtk;
e11523e9 4071 set_bit(HCI_QUIRK_BROKEN_ENHANCED_SETUP_SYNC_CONN, &hdev->quirks);
a1c49c43 4072 set_bit(HCI_QUIRK_NON_PERSISTENT_SETUP, &hdev->quirks);
6dc22ab9 4073 data->recv_acl = btmtk_usb_recv_acl;
ceac1cb0
CL
4074 data->suspend = btmtk_usb_suspend;
4075 data->resume = btmtk_usb_resume;
489304e6 4076 data->disconnect = btusb_mtk_disconnect;
a1c49c43 4077 }
a1c49c43 4078
661cf88a
MH
4079 if (id->driver_info & BTUSB_SWAVE) {
4080 set_bit(HCI_QUIRK_FIXUP_INQUIRY_MODE, &hdev->quirks);
d57dbe77 4081 set_bit(HCI_QUIRK_BROKEN_LOCAL_COMMANDS, &hdev->quirks);
661cf88a 4082 }
d57dbe77 4083
e4c534bb
MH
4084 if (id->driver_info & BTUSB_INTEL_BOOT) {
4085 hdev->manufacturer = 2;
40df783d 4086 set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
e4c534bb 4087 }
40df783d 4088
79f0c87d 4089 if (id->driver_info & BTUSB_ATH3012) {
803cdb8c 4090 data->setup_on_usb = btusb_setup_qca;
5859223e 4091 hdev->set_bdaddr = btusb_set_bdaddr_ath3012;
3d50d51a 4092 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
79f0c87d
JP
4093 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
4094 }
5859223e 4095
3267c884
KBYT
4096 if (id->driver_info & BTUSB_QCA_ROME) {
4097 data->setup_on_usb = btusb_setup_qca;
187f8b64 4098 hdev->shutdown = btusb_shutdown_qca;
3267c884 4099 hdev->set_bdaddr = btusb_set_bdaddr_ath3012;
f07d4780 4100 hdev->reset = btusb_qca_reset;
96e58d36 4101 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
fc549102 4102 btusb_check_needs_reset_resume(intf);
3267c884
KBYT
4103 }
4104
b40f58b9 4105 if (id->driver_info & BTUSB_QCA_WCN6855) {
20981ce2
TJ
4106 data->qca_dump.id_vendor = id->idVendor;
4107 data->qca_dump.id_product = id->idProduct;
4108 data->recv_event = btusb_recv_evt_qca;
4109 data->recv_acl = btusb_recv_acl_qca;
4110 hci_devcd_register(hdev, btusb_coredump_qca, btusb_dump_hdr_qca, NULL);
b40f58b9 4111 data->setup_on_usb = btusb_setup_qca;
3b0d5250 4112 hdev->shutdown = btusb_shutdown_qca;
b40f58b9 4113 hdev->set_bdaddr = btusb_set_bdaddr_wcn6855;
f07d4780 4114 hdev->reset = btusb_qca_reset;
b40f58b9 4115 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
cbe6a044 4116 hci_set_msft_opcode(hdev, 0xFD70);
b40f58b9
RL
4117 }
4118
461f95f0
HW
4119 if (id->driver_info & BTUSB_AMP) {
4120 /* AMP controllers do not support SCO packets */
4121 data->isoc = NULL;
4122 } else {
4123 /* Interface orders are hardcoded in the specification */
4124 data->isoc = usb_ifnum_to_if(data->udev, ifnum_base + 1);
4125 data->isoc_ifnum = ifnum_base + 1;
4126 }
4127
42d22098
AB
4128 if (IS_ENABLED(CONFIG_BT_HCIBTUSB_RTL) &&
4129 (id->driver_info & BTUSB_REALTEK)) {
044014ce 4130 btrtl_set_driver_name(hdev, btusb_driver.name);
a479e713 4131 hdev->setup = btusb_setup_realtek;
7af3f558 4132 hdev->shutdown = btrtl_shutdown_realtek;
f07d4780 4133 hdev->reset = btusb_rtl_reset;
044014ce 4134 hdev->hw_error = btusb_rtl_hw_error;
04b8c814 4135
8274db07
MC
4136 /* Realtek devices need to set remote wakeup on auto-suspend */
4137 set_bit(BTUSB_WAKEUP_AUTOSUSPEND, &data->flags);
55981d35 4138 set_bit(BTUSB_USE_ALT3_FOR_WBS, &data->flags);
04b8c814 4139 }
a2698a9b 4140
5d44f76f
MH
4141 if (id->driver_info & BTUSB_ACTIONS_SEMI) {
4142 /* Support is advertised, but not implemented */
4143 set_bit(HCI_QUIRK_BROKEN_ERR_DATA_REPORTING, &hdev->quirks);
7c2b2d2d 4144 set_bit(HCI_QUIRK_BROKEN_READ_TRANSMIT_POWER, &hdev->quirks);
91b6d02d 4145 set_bit(HCI_QUIRK_BROKEN_SET_RPA_TIMEOUT, &hdev->quirks);
7c2b2d2d 4146 set_bit(HCI_QUIRK_BROKEN_EXT_SCAN, &hdev->quirks);
48201a3b 4147 set_bit(HCI_QUIRK_BROKEN_READ_ENC_KEY_SIZE, &hdev->quirks);
677a55ba
DP
4148 set_bit(HCI_QUIRK_BROKEN_EXT_CREATE_CONN, &hdev->quirks);
4149 set_bit(HCI_QUIRK_BROKEN_WRITE_AUTH_PAYLOAD_TIMEOUT, &hdev->quirks);
5d44f76f
MH
4150 }
4151
7a9d4020 4152 if (!reset)
a6c511c6 4153 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
cfeb4145
MH
4154
4155 if (force_scofix || id->driver_info & BTUSB_WRONG_SCO_MTU) {
4156 if (!disable_scofix)
4157 set_bit(HCI_QUIRK_FIXUP_BUFFER_SIZE, &hdev->quirks);
4158 }
4159
9bfa35fe
MH
4160 if (id->driver_info & BTUSB_BROKEN_ISOC)
4161 data->isoc = NULL;
4162
4b127bd5 4163 if (id->driver_info & BTUSB_WIDEBAND_SPEECH)
00bce3fb 4164 set_bit(HCI_QUIRK_WIDEBAND_SPEECH_SUPPORTED, &hdev->quirks);
4b127bd5 4165
0fec656d 4166 if (id->driver_info & BTUSB_INVALID_LE_STATES)
aae6b812 4167 set_bit(HCI_QUIRK_BROKEN_LE_STATES, &hdev->quirks);
aff8c489 4168
7a9d4020
MH
4169 if (id->driver_info & BTUSB_DIGIANSWER) {
4170 data->cmdreq_type = USB_TYPE_VENDOR;
a6c511c6 4171 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
7a9d4020
MH
4172 }
4173
4174 if (id->driver_info & BTUSB_CSR) {
4175 struct usb_device *udev = data->udev;
81cac64b 4176 u16 bcdDevice = le16_to_cpu(udev->descriptor.bcdDevice);
7a9d4020
MH
4177
4178 /* Old firmware would otherwise execute USB reset */
81cac64b 4179 if (bcdDevice < 0x117)
a6c511c6 4180 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
81cac64b 4181
cde1a8a9
IFM
4182 /* This must be set first in case we disable it for fakes */
4183 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
4184
81cac64b 4185 /* Fake CSR devices with broken commands */
cde1a8a9
IFM
4186 if (le16_to_cpu(udev->descriptor.idVendor) == 0x0a12 &&
4187 le16_to_cpu(udev->descriptor.idProduct) == 0x0001)
81cac64b 4188 hdev->setup = btusb_setup_csr;
7a9d4020
MH
4189 }
4190
cfeb4145 4191 if (id->driver_info & BTUSB_SNIFFER) {
9bfa35fe 4192 struct usb_device *udev = data->udev;
cfeb4145 4193
7a9d4020 4194 /* New sniffer firmware has crippled HCI interface */
cfeb4145
MH
4195 if (le16_to_cpu(udev->descriptor.bcdDevice) > 0x997)
4196 set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
4197 }
4198
3a5ef20c
MH
4199 if (id->driver_info & BTUSB_INTEL_BOOT) {
4200 /* A bug in the bootloader causes that interrupt interface is
4201 * only enabled after receiving SetInterface(0, AltSetting=0).
4202 */
4203 err = usb_set_interface(data->udev, 0, 0);
4204 if (err < 0) {
4205 BT_ERR("failed to set interface 0, alt 0 %d", err);
10ab133b 4206 goto out_free_dev;
3a5ef20c
MH
4207 }
4208 }
4209
9bfa35fe
MH
4210 if (data->isoc) {
4211 err = usb_driver_claim_interface(&btusb_driver,
89e7533d 4212 data->isoc, data);
10ab133b
RJ
4213 if (err < 0)
4214 goto out_free_dev;
9bfa35fe
MH
4215 }
4216
c0a21a52 4217 if (IS_ENABLED(CONFIG_BT_HCIBTUSB_BCM) && data->diag) {
9d08f504
MH
4218 if (!usb_driver_claim_interface(&btusb_driver,
4219 data->diag, data))
4220 __set_diag_interface(hdev);
4221 else
4222 data->diag = NULL;
4223 }
9d08f504 4224
89591522
HG
4225 if (enable_autosuspend)
4226 usb_enable_autosuspend(data->udev);
eff2d68c 4227
bb1afb29
LAD
4228 data->poll_sync = enable_poll_sync;
4229
5e23b923 4230 err = hci_register_dev(hdev);
10ab133b
RJ
4231 if (err < 0)
4232 goto out_free_dev;
5e23b923
MH
4233
4234 usb_set_intfdata(intf, data);
4235
800fe5ec
LAD
4236 debugfs_create_file("force_poll_sync", 0644, hdev->debugfs, data,
4237 &force_poll_sync_fops);
4238
5e23b923 4239 return 0;
10ab133b
RJ
4240
4241out_free_dev:
dc786b2c
RJ
4242 if (data->reset_gpio)
4243 gpiod_put(data->reset_gpio);
10ab133b
RJ
4244 hci_free_dev(hdev);
4245 return err;
5e23b923
MH
4246}
4247
4248static void btusb_disconnect(struct usb_interface *intf)
4249{
4250 struct btusb_data *data = usb_get_intfdata(intf);
4251 struct hci_dev *hdev;
4252
4253 BT_DBG("intf %p", intf);
4254
4255 if (!data)
4256 return;
4257
4258 hdev = data->hdev;
5fbcd260
MH
4259 usb_set_intfdata(data->intf, NULL);
4260
96ace551 4261 if (data->isoc)
5fbcd260 4262 usb_set_intfdata(data->isoc, NULL);
5e23b923 4263
9d08f504
MH
4264 if (data->diag)
4265 usb_set_intfdata(data->diag, NULL);
4266
cea1805f
CL
4267 if (data->disconnect)
4268 data->disconnect(hdev);
4269
5e23b923
MH
4270 hci_unregister_dev(hdev);
4271
9d08f504
MH
4272 if (intf == data->intf) {
4273 if (data->isoc)
4274 usb_driver_release_interface(&btusb_driver, data->isoc);
4275 if (data->diag)
4276 usb_driver_release_interface(&btusb_driver, data->diag);
4277 } else if (intf == data->isoc) {
4278 if (data->diag)
4279 usb_driver_release_interface(&btusb_driver, data->diag);
5fbcd260 4280 usb_driver_release_interface(&btusb_driver, data->intf);
9d08f504
MH
4281 } else if (intf == data->diag) {
4282 usb_driver_release_interface(&btusb_driver, data->intf);
4283 if (data->isoc)
4284 usb_driver_release_interface(&btusb_driver, data->isoc);
4285 }
5fbcd260 4286
fd913ef7
RJ
4287 if (data->oob_wake_irq)
4288 device_init_wakeup(&data->udev->dev, false);
4289
dc786b2c
RJ
4290 if (data->reset_gpio)
4291 gpiod_put(data->reset_gpio);
4292
5e23b923
MH
4293 hci_free_dev(hdev);
4294}
4295
7bee549e 4296#ifdef CONFIG_PM
6a88adf2
MH
4297static int btusb_suspend(struct usb_interface *intf, pm_message_t message)
4298{
4299 struct btusb_data *data = usb_get_intfdata(intf);
4300
4301 BT_DBG("intf %p", intf);
4302
61071229
LAD
4303 /* Don't auto-suspend if there are connections; external suspend calls
4304 * shall never fail.
4305 */
4306 if (PMSG_IS_AUTO(message) && hci_conn_count(data->hdev))
4e0a1d8b
LAD
4307 return -EBUSY;
4308
6a88adf2
MH
4309 if (data->suspend_count++)
4310 return 0;
4311
7bee549e 4312 spin_lock_irq(&data->txlock);
5b1b0b81 4313 if (!(PMSG_IS_AUTO(message) && data->tx_in_flight)) {
7bee549e
ON
4314 set_bit(BTUSB_SUSPENDING, &data->flags);
4315 spin_unlock_irq(&data->txlock);
4316 } else {
4317 spin_unlock_irq(&data->txlock);
4318 data->suspend_count--;
4319 return -EBUSY;
4320 }
4321
6a88adf2
MH
4322 cancel_work_sync(&data->work);
4323
95f92928
CL
4324 if (data->suspend)
4325 data->suspend(data->hdev);
4326
7bee549e 4327 btusb_stop_traffic(data);
6a88adf2
MH
4328 usb_kill_anchored_urbs(&data->tx_anchor);
4329
fd913ef7
RJ
4330 if (data->oob_wake_irq && device_may_wakeup(&data->udev->dev)) {
4331 set_bit(BTUSB_OOB_WAKE_ENABLED, &data->flags);
4332 enable_irq_wake(data->oob_wake_irq);
4333 enable_irq(data->oob_wake_irq);
4334 }
4335
9e45524a
AL
4336 /* For global suspend, Realtek devices lose the loaded fw
4337 * in them. But for autosuspend, firmware should remain.
4338 * Actually, it depends on whether the usb host sends
4339 * set feature (enable wakeup) or not.
4340 */
8274db07 4341 if (test_bit(BTUSB_WAKEUP_AUTOSUSPEND, &data->flags)) {
9e45524a
AL
4342 if (PMSG_IS_AUTO(message) &&
4343 device_can_wakeup(&data->udev->dev))
4344 data->udev->do_remote_wakeup = 1;
8274db07
MC
4345 else if (!PMSG_IS_AUTO(message) &&
4346 !device_may_wakeup(&data->udev->dev)) {
4347 data->udev->do_remote_wakeup = 0;
9e45524a 4348 data->udev->reset_resume = 1;
8274db07 4349 }
9e45524a
AL
4350 }
4351
6a88adf2
MH
4352 return 0;
4353}
4354
7bee549e
ON
4355static void play_deferred(struct btusb_data *data)
4356{
4357 struct urb *urb;
4358 int err;
4359
4360 while ((urb = usb_get_from_anchor(&data->deferred))) {
19cfe912
JC
4361 usb_anchor_urb(urb, &data->tx_anchor);
4362
7bee549e 4363 err = usb_submit_urb(urb, GFP_ATOMIC);
19cfe912
JC
4364 if (err < 0) {
4365 if (err != -EPERM && err != -ENODEV)
4366 BT_ERR("%s urb %p submission failed (%d)",
4367 data->hdev->name, urb, -err);
4368 kfree(urb->setup_packet);
4369 usb_unanchor_urb(urb);
4370 usb_free_urb(urb);
7bee549e 4371 break;
19cfe912 4372 }
7bee549e
ON
4373
4374 data->tx_in_flight++;
19cfe912
JC
4375 usb_free_urb(urb);
4376 }
4377
4378 /* Cleanup the rest deferred urbs. */
4379 while ((urb = usb_get_from_anchor(&data->deferred))) {
4380 kfree(urb->setup_packet);
4381 usb_free_urb(urb);
7bee549e 4382 }
7bee549e
ON
4383}
4384
6a88adf2
MH
4385static int btusb_resume(struct usb_interface *intf)
4386{
4387 struct btusb_data *data = usb_get_intfdata(intf);
4388 struct hci_dev *hdev = data->hdev;
7bee549e 4389 int err = 0;
6a88adf2
MH
4390
4391 BT_DBG("intf %p", intf);
4392
4393 if (--data->suspend_count)
4394 return 0;
4395
fd913ef7
RJ
4396 /* Disable only if not already disabled (keep it balanced) */
4397 if (test_and_clear_bit(BTUSB_OOB_WAKE_ENABLED, &data->flags)) {
4398 disable_irq(data->oob_wake_irq);
4399 disable_irq_wake(data->oob_wake_irq);
4400 }
4401
6a88adf2 4402 if (!test_bit(HCI_RUNNING, &hdev->flags))
7bee549e 4403 goto done;
6a88adf2
MH
4404
4405 if (test_bit(BTUSB_INTR_RUNNING, &data->flags)) {
4406 err = btusb_submit_intr_urb(hdev, GFP_NOIO);
4407 if (err < 0) {
4408 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
7bee549e 4409 goto failed;
6a88adf2
MH
4410 }
4411 }
4412
4413 if (test_bit(BTUSB_BULK_RUNNING, &data->flags)) {
43c2e57f
MH
4414 err = btusb_submit_bulk_urb(hdev, GFP_NOIO);
4415 if (err < 0) {
6a88adf2 4416 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
7bee549e
ON
4417 goto failed;
4418 }
4419
4420 btusb_submit_bulk_urb(hdev, GFP_NOIO);
6a88adf2
MH
4421 }
4422
4423 if (test_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
4424 if (btusb_submit_isoc_urb(hdev, GFP_NOIO) < 0)
4425 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
4426 else
4427 btusb_submit_isoc_urb(hdev, GFP_NOIO);
4428 }
4429
95f92928
CL
4430 if (data->resume)
4431 data->resume(hdev);
4432
7bee549e
ON
4433 spin_lock_irq(&data->txlock);
4434 play_deferred(data);
4435 clear_bit(BTUSB_SUSPENDING, &data->flags);
4436 spin_unlock_irq(&data->txlock);
4437 schedule_work(&data->work);
4438
6a88adf2 4439 return 0;
7bee549e
ON
4440
4441failed:
4442 usb_scuttle_anchored_urbs(&data->deferred);
4443done:
4444 spin_lock_irq(&data->txlock);
4445 clear_bit(BTUSB_SUSPENDING, &data->flags);
4446 spin_unlock_irq(&data->txlock);
4447
4448 return err;
6a88adf2 4449}
7bee549e 4450#endif
6a88adf2 4451
4f9c1a08
MM
4452#ifdef CONFIG_DEV_COREDUMP
4453static void btusb_coredump(struct device *dev)
4454{
4455 struct btusb_data *data = dev_get_drvdata(dev);
4456 struct hci_dev *hdev = data->hdev;
4457
4458 if (hdev->dump.coredump)
4459 hdev->dump.coredump(hdev);
4460}
4461#endif
4462
5e23b923
MH
4463static struct usb_driver btusb_driver = {
4464 .name = "btusb",
4465 .probe = btusb_probe,
4466 .disconnect = btusb_disconnect,
7bee549e 4467#ifdef CONFIG_PM
6a88adf2
MH
4468 .suspend = btusb_suspend,
4469 .resume = btusb_resume,
7bee549e 4470#endif
5e23b923 4471 .id_table = btusb_table,
7bee549e 4472 .supports_autosuspend = 1,
e1f12eb6 4473 .disable_hub_initiated_lpm = 1,
4f9c1a08
MM
4474
4475#ifdef CONFIG_DEV_COREDUMP
49a78b05
YD
4476 .driver = {
4477 .coredump = btusb_coredump,
4f9c1a08
MM
4478 },
4479#endif
5e23b923
MH
4480};
4481
93f1508c 4482module_usb_driver(btusb_driver);
5e23b923 4483
cfeb4145
MH
4484module_param(disable_scofix, bool, 0644);
4485MODULE_PARM_DESC(disable_scofix, "Disable fixup of wrong SCO buffer size");
4486
4487module_param(force_scofix, bool, 0644);
4488MODULE_PARM_DESC(force_scofix, "Force fixup of wrong SCO buffers size");
4489
eff2d68c
HG
4490module_param(enable_autosuspend, bool, 0644);
4491MODULE_PARM_DESC(enable_autosuspend, "Enable USB autosuspend by default");
4492
cfeb4145
MH
4493module_param(reset, bool, 0644);
4494MODULE_PARM_DESC(reset, "Send HCI reset command on initialization");
4495
5e23b923
MH
4496MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
4497MODULE_DESCRIPTION("Generic Bluetooth USB driver ver " VERSION);
4498MODULE_VERSION(VERSION);
4499MODULE_LICENSE("GPL");