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