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