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