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