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