Bluetooth: Don't use remote address type to decide IRK persistency
[deliverable/linux.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
2107 BT_INFO("%s: Minimum firmware build %u week %u %u", hdev->name,
2108 params->min_fw_build_nn, params->min_fw_build_cw,
2109 2000 + params->min_fw_build_yy);
2110
2111 /* It is required that every single firmware fragment is acknowledged
2112 * with a command complete event. If the boot parameters indicate
2113 * that this bootloader does not send them, then abort the setup.
2114 */
2115 if (params->limited_cce != 0x00) {
2116 BT_ERR("%s: Unsupported Intel firmware loading method (%u)",
2117 hdev->name, params->limited_cce);
2118 kfree_skb(skb);
2119 return -EINVAL;
2120 }
2121
2122 /* If the OTP has no valid Bluetooth device address, then there will
2123 * also be no valid address for the operational firmware.
2124 */
2125 if (!bacmp(&params->otp_bdaddr, BDADDR_ANY)) {
2126 BT_INFO("%s: No device address configured", hdev->name);
2127 set_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks);
2128 }
2129
2130 /* With this Intel bootloader only the hardware variant and device
2131 * revision information are used to select the right firmware.
2132 *
2133 * Currently this bootloader support is limited to hardware variant
2134 * iBT 3.0 (LnP/SfP) which is identified by the value 11 (0x0b).
2135 */
2136 snprintf(fwname, sizeof(fwname), "intel/ibt-11-%u.sfi",
2137 le16_to_cpu(params->dev_revid));
2138
2139 err = request_firmware(&fw, fwname, &hdev->dev);
2140 if (err < 0) {
2141 BT_ERR("%s: Failed to load Intel firmware file (%d)",
2142 hdev->name, err);
2143 kfree_skb(skb);
2144 return err;
2145 }
2146
2147 BT_INFO("%s: Found device firmware: %s", hdev->name, fwname);
2148
52cc9168
THJA
2149 /* Save the DDC file name for later use to apply once the firmware
2150 * downloading is done.
2151 */
2152 snprintf(fwname, sizeof(fwname), "intel/ibt-11-%u.ddc",
2153 le16_to_cpu(params->dev_revid));
2154
cda0dd78
MH
2155 kfree_skb(skb);
2156
2157 if (fw->size < 644) {
2158 BT_ERR("%s: Invalid size of firmware file (%zu)",
2159 hdev->name, fw->size);
2160 err = -EBADF;
2161 goto done;
2162 }
2163
2164 set_bit(BTUSB_DOWNLOADING, &data->flags);
2165
2166 /* Start the firmware download transaction with the Init fragment
2167 * represented by the 128 bytes of CSS header.
2168 */
09df123d 2169 err = btintel_secure_send(hdev, 0x00, 128, fw->data);
cda0dd78
MH
2170 if (err < 0) {
2171 BT_ERR("%s: Failed to send firmware header (%d)",
2172 hdev->name, err);
2173 goto done;
2174 }
2175
2176 /* Send the 256 bytes of public key information from the firmware
2177 * as the PKey fragment.
2178 */
09df123d 2179 err = btintel_secure_send(hdev, 0x03, 256, fw->data + 128);
cda0dd78
MH
2180 if (err < 0) {
2181 BT_ERR("%s: Failed to send firmware public key (%d)",
2182 hdev->name, err);
2183 goto done;
2184 }
2185
2186 /* Send the 256 bytes of signature information from the firmware
2187 * as the Sign fragment.
2188 */
09df123d 2189 err = btintel_secure_send(hdev, 0x02, 256, fw->data + 388);
cda0dd78
MH
2190 if (err < 0) {
2191 BT_ERR("%s: Failed to send firmware signature (%d)",
2192 hdev->name, err);
2193 goto done;
2194 }
2195
2196 fw_ptr = fw->data + 644;
e66890a9 2197 frag_len = 0;
cda0dd78
MH
2198
2199 while (fw_ptr - fw->data < fw->size) {
e66890a9 2200 struct hci_command_hdr *cmd = (void *)(fw_ptr + frag_len);
cda0dd78 2201
e66890a9 2202 frag_len += sizeof(*cmd) + cmd->plen;
cda0dd78 2203
5075edae 2204 /* The parameter length of the secure send command requires
e66890a9
MH
2205 * a 4 byte alignment. It happens so that the firmware file
2206 * contains proper Intel_NOP commands to align the fragments
2207 * as needed.
2208 *
2209 * Send set of commands with 4 byte alignment from the
2210 * firmware data buffer as a single Data fragement.
cda0dd78 2211 */
e66890a9 2212 if (!(frag_len % 4)) {
09df123d 2213 err = btintel_secure_send(hdev, 0x01, frag_len, fw_ptr);
e66890a9
MH
2214 if (err < 0) {
2215 BT_ERR("%s: Failed to send firmware data (%d)",
2216 hdev->name, err);
2217 goto done;
2218 }
cda0dd78 2219
e66890a9
MH
2220 fw_ptr += frag_len;
2221 frag_len = 0;
2222 }
cda0dd78
MH
2223 }
2224
ce6bb929
MH
2225 set_bit(BTUSB_FIRMWARE_LOADED, &data->flags);
2226
a087a98e
JH
2227 BT_INFO("%s: Waiting for firmware download to complete", hdev->name);
2228
cda0dd78
MH
2229 /* Before switching the device into operational mode and with that
2230 * booting the loaded firmware, wait for the bootloader notification
2231 * that all fragments have been successfully received.
2232 *
a087a98e
JH
2233 * When the event processing receives the notification, then the
2234 * BTUSB_DOWNLOADING flag will be cleared.
2235 *
2236 * The firmware loading should not take longer than 5 seconds
2237 * and thus just timeout if that happens and fail the setup
2238 * of this device.
cda0dd78 2239 */
129a7693
JH
2240 err = wait_on_bit_timeout(&data->flags, BTUSB_DOWNLOADING,
2241 TASK_INTERRUPTIBLE,
2242 msecs_to_jiffies(5000));
a087a98e
JH
2243 if (err == 1) {
2244 BT_ERR("%s: Firmware loading interrupted", hdev->name);
2245 err = -EINTR;
2246 goto done;
2247 }
cda0dd78 2248
a087a98e
JH
2249 if (err) {
2250 BT_ERR("%s: Firmware loading timeout", hdev->name);
2251 err = -ETIMEDOUT;
2252 goto done;
cda0dd78
MH
2253 }
2254
2255 if (test_bit(BTUSB_FIRMWARE_FAILED, &data->flags)) {
2256 BT_ERR("%s: Firmware loading failed", hdev->name);
2257 err = -ENOEXEC;
2258 goto done;
2259 }
2260
2261 rettime = ktime_get();
2262 delta = ktime_sub(rettime, calltime);
2263 duration = (unsigned long long) ktime_to_ns(delta) >> 10;
2264
2265 BT_INFO("%s: Firmware loaded in %llu usecs", hdev->name, duration);
2266
2267done:
2268 release_firmware(fw);
2269
2270 if (err < 0)
2271 return err;
2272
2273 calltime = ktime_get();
2274
2275 set_bit(BTUSB_BOOTING, &data->flags);
2276
2277 skb = __hci_cmd_sync(hdev, 0xfc01, sizeof(reset_param), reset_param,
2278 HCI_INIT_TIMEOUT);
2279 if (IS_ERR(skb))
2280 return PTR_ERR(skb);
2281
2282 kfree_skb(skb);
2283
2284 /* The bootloader will not indicate when the device is ready. This
2285 * is done by the operational firmware sending bootup notification.
fad70972
JH
2286 *
2287 * Booting into operational firmware should not take longer than
2288 * 1 second. However if that happens, then just fail the setup
2289 * since something went wrong.
cda0dd78 2290 */
fad70972 2291 BT_INFO("%s: Waiting for device to boot", hdev->name);
cda0dd78 2292
129a7693
JH
2293 err = wait_on_bit_timeout(&data->flags, BTUSB_BOOTING,
2294 TASK_INTERRUPTIBLE,
2295 msecs_to_jiffies(1000));
cda0dd78 2296
fad70972
JH
2297 if (err == 1) {
2298 BT_ERR("%s: Device boot interrupted", hdev->name);
2299 return -EINTR;
2300 }
cda0dd78 2301
fad70972
JH
2302 if (err) {
2303 BT_ERR("%s: Device boot timeout", hdev->name);
2304 return -ETIMEDOUT;
cda0dd78
MH
2305 }
2306
2307 rettime = ktime_get();
2308 delta = ktime_sub(rettime, calltime);
2309 duration = (unsigned long long) ktime_to_ns(delta) >> 10;
2310
2311 BT_INFO("%s: Device booted in %llu usecs", hdev->name, duration);
2312
2313 clear_bit(BTUSB_BOOTLOADER, &data->flags);
2314
52cc9168
THJA
2315 /* Once the device is running in operational mode, it needs to apply
2316 * the device configuration (DDC) parameters.
2317 *
2318 * The device can work without DDC parameters, so even if it fails
2319 * to load the file, no need to fail the setup.
2320 */
e924d3d6 2321 btintel_load_ddc_config(hdev, fwname);
52cc9168 2322
cda0dd78
MH
2323 return 0;
2324}
2325
bfbd45e9
THJA
2326static int btusb_shutdown_intel(struct hci_dev *hdev)
2327{
2328 struct sk_buff *skb;
2329 long ret;
2330
2331 /* Some platforms have an issue with BT LED when the interface is
2332 * down or BT radio is turned off, which takes 5 seconds to BT LED
2333 * goes off. This command turns off the BT LED immediately.
2334 */
2335 skb = __hci_cmd_sync(hdev, 0xfc3f, 0, NULL, HCI_INIT_TIMEOUT);
2336 if (IS_ERR(skb)) {
2337 ret = PTR_ERR(skb);
2338 BT_ERR("%s: turning off Intel device LED failed (%ld)",
2339 hdev->name, ret);
2340 return ret;
2341 }
2342 kfree_skb(skb);
2343
2344 return 0;
2345}
2346
ae8df494
AK
2347static int btusb_set_bdaddr_marvell(struct hci_dev *hdev,
2348 const bdaddr_t *bdaddr)
2349{
2350 struct sk_buff *skb;
2351 u8 buf[8];
2352 long ret;
2353
2354 buf[0] = 0xfe;
2355 buf[1] = sizeof(bdaddr_t);
2356 memcpy(buf + 2, bdaddr, sizeof(bdaddr_t));
2357
2358 skb = __hci_cmd_sync(hdev, 0xfc22, sizeof(buf), buf, HCI_INIT_TIMEOUT);
2359 if (IS_ERR(skb)) {
2360 ret = PTR_ERR(skb);
2361 BT_ERR("%s: changing Marvell device address failed (%ld)",
2362 hdev->name, ret);
2363 return ret;
2364 }
2365 kfree_skb(skb);
2366
2367 return 0;
2368}
2369
5859223e
TK
2370static int btusb_set_bdaddr_ath3012(struct hci_dev *hdev,
2371 const bdaddr_t *bdaddr)
2372{
2373 struct sk_buff *skb;
2374 u8 buf[10];
2375 long ret;
2376
2377 buf[0] = 0x01;
2378 buf[1] = 0x01;
2379 buf[2] = 0x00;
2380 buf[3] = sizeof(bdaddr_t);
2381 memcpy(buf + 4, bdaddr, sizeof(bdaddr_t));
2382
2383 skb = __hci_cmd_sync(hdev, 0xfc0b, sizeof(buf), buf, HCI_INIT_TIMEOUT);
2384 if (IS_ERR(skb)) {
2385 ret = PTR_ERR(skb);
2386 BT_ERR("%s: Change address command failed (%ld)",
2387 hdev->name, ret);
2388 return ret;
2389 }
2390 kfree_skb(skb);
2391
2392 return 0;
2393}
2394
3267c884
KBYT
2395#define QCA_DFU_PACKET_LEN 4096
2396
2397#define QCA_GET_TARGET_VERSION 0x09
2398#define QCA_CHECK_STATUS 0x05
2399#define QCA_DFU_DOWNLOAD 0x01
2400
2401#define QCA_SYSCFG_UPDATED 0x40
2402#define QCA_PATCH_UPDATED 0x80
2403#define QCA_DFU_TIMEOUT 3000
2404
2405struct qca_version {
2406 __le32 rom_version;
2407 __le32 patch_version;
2408 __le32 ram_version;
2409 __le32 ref_clock;
2410 __u8 reserved[4];
2411} __packed;
2412
2413struct qca_rampatch_version {
2414 __le16 rom_version;
2415 __le16 patch_version;
2416} __packed;
2417
2418struct qca_device_info {
bf906b3d
KBYT
2419 u32 rom_version;
2420 u8 rampatch_hdr; /* length of header in rampatch */
2421 u8 nvm_hdr; /* length of header in NVM */
2422 u8 ver_offset; /* offset of version structure in rampatch */
3267c884
KBYT
2423};
2424
2425static const struct qca_device_info qca_devices_table[] = {
2426 { 0x00000100, 20, 4, 10 }, /* Rome 1.0 */
2427 { 0x00000101, 20, 4, 10 }, /* Rome 1.1 */
7f6e6363 2428 { 0x00000200, 28, 4, 18 }, /* Rome 2.0 */
3267c884
KBYT
2429 { 0x00000201, 28, 4, 18 }, /* Rome 2.1 */
2430 { 0x00000300, 28, 4, 18 }, /* Rome 3.0 */
2431 { 0x00000302, 28, 4, 18 }, /* Rome 3.2 */
2432};
2433
2434static int btusb_qca_send_vendor_req(struct hci_dev *hdev, u8 request,
2435 void *data, u16 size)
2436{
2437 struct btusb_data *btdata = hci_get_drvdata(hdev);
2438 struct usb_device *udev = btdata->udev;
2439 int pipe, err;
2440 u8 *buf;
2441
2442 buf = kmalloc(size, GFP_KERNEL);
2443 if (!buf)
2444 return -ENOMEM;
2445
2446 /* Found some of USB hosts have IOT issues with ours so that we should
2447 * not wait until HCI layer is ready.
2448 */
2449 pipe = usb_rcvctrlpipe(udev, 0);
2450 err = usb_control_msg(udev, pipe, request, USB_TYPE_VENDOR | USB_DIR_IN,
2451 0, 0, buf, size, USB_CTRL_SET_TIMEOUT);
2452 if (err < 0) {
2453 BT_ERR("%s: Failed to access otp area (%d)", hdev->name, err);
2454 goto done;
2455 }
2456
2457 memcpy(data, buf, size);
2458
2459done:
2460 kfree(buf);
2461
2462 return err;
2463}
2464
2465static int btusb_setup_qca_download_fw(struct hci_dev *hdev,
2466 const struct firmware *firmware,
2467 size_t hdr_size)
2468{
2469 struct btusb_data *btdata = hci_get_drvdata(hdev);
2470 struct usb_device *udev = btdata->udev;
2471 size_t count, size, sent = 0;
2472 int pipe, len, err;
2473 u8 *buf;
2474
2475 buf = kmalloc(QCA_DFU_PACKET_LEN, GFP_KERNEL);
2476 if (!buf)
2477 return -ENOMEM;
2478
2479 count = firmware->size;
2480
2481 size = min_t(size_t, count, hdr_size);
2482 memcpy(buf, firmware->data, size);
2483
2484 /* USB patches should go down to controller through USB path
2485 * because binary format fits to go down through USB channel.
2486 * USB control path is for patching headers and USB bulk is for
2487 * patch body.
2488 */
2489 pipe = usb_sndctrlpipe(udev, 0);
2490 err = usb_control_msg(udev, pipe, QCA_DFU_DOWNLOAD, USB_TYPE_VENDOR,
2491 0, 0, buf, size, USB_CTRL_SET_TIMEOUT);
2492 if (err < 0) {
2493 BT_ERR("%s: Failed to send headers (%d)", hdev->name, err);
2494 goto done;
2495 }
2496
2497 sent += size;
2498 count -= size;
2499
2500 while (count) {
2501 size = min_t(size_t, count, QCA_DFU_PACKET_LEN);
2502
2503 memcpy(buf, firmware->data + sent, size);
2504
2505 pipe = usb_sndbulkpipe(udev, 0x02);
2506 err = usb_bulk_msg(udev, pipe, buf, size, &len,
2507 QCA_DFU_TIMEOUT);
2508 if (err < 0) {
2509 BT_ERR("%s: Failed to send body at %zd of %zd (%d)",
2510 hdev->name, sent, firmware->size, err);
2511 break;
2512 }
2513
2514 if (size != len) {
2515 BT_ERR("%s: Failed to get bulk buffer", hdev->name);
2516 err = -EILSEQ;
2517 break;
2518 }
2519
2520 sent += size;
2521 count -= size;
2522 }
2523
2524done:
2525 kfree(buf);
2526 return err;
2527}
2528
2529static int btusb_setup_qca_load_rampatch(struct hci_dev *hdev,
2530 struct qca_version *ver,
2531 const struct qca_device_info *info)
2532{
2533 struct qca_rampatch_version *rver;
2534 const struct firmware *fw;
bf906b3d
KBYT
2535 u32 ver_rom, ver_patch;
2536 u16 rver_rom, rver_patch;
3267c884
KBYT
2537 char fwname[64];
2538 int err;
2539
bf906b3d
KBYT
2540 ver_rom = le32_to_cpu(ver->rom_version);
2541 ver_patch = le32_to_cpu(ver->patch_version);
2542
2543 snprintf(fwname, sizeof(fwname), "qca/rampatch_usb_%08x.bin", ver_rom);
3267c884
KBYT
2544
2545 err = request_firmware(&fw, fwname, &hdev->dev);
2546 if (err) {
2547 BT_ERR("%s: failed to request rampatch file: %s (%d)",
2548 hdev->name, fwname, err);
2549 return err;
2550 }
2551
2552 BT_INFO("%s: using rampatch file: %s", hdev->name, fwname);
bf906b3d 2553
3267c884 2554 rver = (struct qca_rampatch_version *)(fw->data + info->ver_offset);
bf906b3d
KBYT
2555 rver_rom = le16_to_cpu(rver->rom_version);
2556 rver_patch = le16_to_cpu(rver->patch_version);
2557
3267c884 2558 BT_INFO("%s: QCA: patch rome 0x%x build 0x%x, firmware rome 0x%x "
bf906b3d
KBYT
2559 "build 0x%x", hdev->name, rver_rom, rver_patch, ver_rom,
2560 ver_patch);
3267c884 2561
bf906b3d 2562 if (rver_rom != ver_rom || rver_patch <= ver_patch) {
3267c884
KBYT
2563 BT_ERR("%s: rampatch file version did not match with firmware",
2564 hdev->name);
2565 err = -EINVAL;
2566 goto done;
2567 }
2568
2569 err = btusb_setup_qca_download_fw(hdev, fw, info->rampatch_hdr);
2570
2571done:
2572 release_firmware(fw);
2573
2574 return err;
2575}
2576
2577static int btusb_setup_qca_load_nvm(struct hci_dev *hdev,
2578 struct qca_version *ver,
2579 const struct qca_device_info *info)
2580{
2581 const struct firmware *fw;
2582 char fwname[64];
2583 int err;
2584
2585 snprintf(fwname, sizeof(fwname), "qca/nvm_usb_%08x.bin",
2586 le32_to_cpu(ver->rom_version));
2587
2588 err = request_firmware(&fw, fwname, &hdev->dev);
2589 if (err) {
2590 BT_ERR("%s: failed to request NVM file: %s (%d)",
2591 hdev->name, fwname, err);
2592 return err;
2593 }
2594
2595 BT_INFO("%s: using NVM file: %s", hdev->name, fwname);
2596
2597 err = btusb_setup_qca_download_fw(hdev, fw, info->nvm_hdr);
2598
2599 release_firmware(fw);
2600
2601 return err;
2602}
2603
2604static int btusb_setup_qca(struct hci_dev *hdev)
2605{
2606 const struct qca_device_info *info = NULL;
2607 struct qca_version ver;
bf906b3d 2608 u32 ver_rom;
3267c884
KBYT
2609 u8 status;
2610 int i, err;
2611
2612 err = btusb_qca_send_vendor_req(hdev, QCA_GET_TARGET_VERSION, &ver,
eb50042f 2613 sizeof(ver));
3267c884
KBYT
2614 if (err < 0)
2615 return err;
2616
bf906b3d 2617 ver_rom = le32_to_cpu(ver.rom_version);
3267c884 2618 for (i = 0; i < ARRAY_SIZE(qca_devices_table); i++) {
bf906b3d 2619 if (ver_rom == qca_devices_table[i].rom_version)
3267c884
KBYT
2620 info = &qca_devices_table[i];
2621 }
2622 if (!info) {
2623 BT_ERR("%s: don't support firmware rome 0x%x", hdev->name,
bf906b3d 2624 ver_rom);
3267c884
KBYT
2625 return -ENODEV;
2626 }
2627
2628 err = btusb_qca_send_vendor_req(hdev, QCA_CHECK_STATUS, &status,
2629 sizeof(status));
2630 if (err < 0)
2631 return err;
2632
2633 if (!(status & QCA_PATCH_UPDATED)) {
2634 err = btusb_setup_qca_load_rampatch(hdev, &ver, info);
2635 if (err < 0)
2636 return err;
2637 }
2638
2639 if (!(status & QCA_SYSCFG_UPDATED)) {
2640 err = btusb_setup_qca_load_nvm(hdev, &ver, info);
2641 if (err < 0)
2642 return err;
2643 }
2644
2645 return 0;
2646}
2647
9d08f504
MH
2648#ifdef CONFIG_BT_HCIBTUSB_BCM
2649static inline int __set_diag_interface(struct hci_dev *hdev)
2650{
2651 struct btusb_data *data = hci_get_drvdata(hdev);
2652 struct usb_interface *intf = data->diag;
2653 int i;
2654
2655 if (!data->diag)
2656 return -ENODEV;
2657
2658 data->diag_tx_ep = NULL;
2659 data->diag_rx_ep = NULL;
2660
2661 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
2662 struct usb_endpoint_descriptor *ep_desc;
2663
2664 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
2665
2666 if (!data->diag_tx_ep && usb_endpoint_is_bulk_out(ep_desc)) {
2667 data->diag_tx_ep = ep_desc;
2668 continue;
2669 }
2670
2671 if (!data->diag_rx_ep && usb_endpoint_is_bulk_in(ep_desc)) {
2672 data->diag_rx_ep = ep_desc;
2673 continue;
2674 }
2675 }
2676
2677 if (!data->diag_tx_ep || !data->diag_rx_ep) {
2678 BT_ERR("%s invalid diagnostic descriptors", hdev->name);
2679 return -ENODEV;
2680 }
2681
2682 return 0;
2683}
2684
2685static struct urb *alloc_diag_urb(struct hci_dev *hdev, bool enable)
2686{
2687 struct btusb_data *data = hci_get_drvdata(hdev);
2688 struct sk_buff *skb;
2689 struct urb *urb;
2690 unsigned int pipe;
2691
2692 if (!data->diag_tx_ep)
2693 return ERR_PTR(-ENODEV);
2694
2695 urb = usb_alloc_urb(0, GFP_KERNEL);
2696 if (!urb)
2697 return ERR_PTR(-ENOMEM);
2698
2699 skb = bt_skb_alloc(2, GFP_KERNEL);
2700 if (!skb) {
2701 usb_free_urb(urb);
2702 return ERR_PTR(-ENOMEM);
2703 }
2704
2705 *skb_put(skb, 1) = 0xf0;
2706 *skb_put(skb, 1) = enable;
2707
2708 pipe = usb_sndbulkpipe(data->udev, data->diag_tx_ep->bEndpointAddress);
2709
2710 usb_fill_bulk_urb(urb, data->udev, pipe,
2711 skb->data, skb->len, btusb_tx_complete, skb);
2712
2713 skb->dev = (void *)hdev;
2714
2715 return urb;
2716}
2717
2718static int btusb_bcm_set_diag(struct hci_dev *hdev, bool enable)
2719{
2720 struct btusb_data *data = hci_get_drvdata(hdev);
2721 struct urb *urb;
2722
2723 if (!data->diag)
2724 return -ENODEV;
2725
2726 if (!test_bit(HCI_RUNNING, &hdev->flags))
2727 return -ENETDOWN;
2728
2729 urb = alloc_diag_urb(hdev, enable);
2730 if (IS_ERR(urb))
2731 return PTR_ERR(urb);
2732
2733 return submit_or_queue_tx_urb(hdev, urb);
2734}
2735#endif
2736
5e23b923 2737static int btusb_probe(struct usb_interface *intf,
89e7533d 2738 const struct usb_device_id *id)
5e23b923
MH
2739{
2740 struct usb_endpoint_descriptor *ep_desc;
2741 struct btusb_data *data;
2742 struct hci_dev *hdev;
2743 int i, err;
2744
2745 BT_DBG("intf %p id %p", intf, id);
2746
cfeb4145 2747 /* interface numbers are hardcoded in the spec */
5e23b923
MH
2748 if (intf->cur_altsetting->desc.bInterfaceNumber != 0)
2749 return -ENODEV;
2750
2751 if (!id->driver_info) {
2752 const struct usb_device_id *match;
89e7533d 2753
5e23b923
MH
2754 match = usb_match_id(intf, blacklist_table);
2755 if (match)
2756 id = match;
2757 }
2758
cfeb4145
MH
2759 if (id->driver_info == BTUSB_IGNORE)
2760 return -ENODEV;
2761
2d25f8b4
SL
2762 if (id->driver_info & BTUSB_ATH3012) {
2763 struct usb_device *udev = interface_to_usbdev(intf);
2764
2765 /* Old firmware would otherwise let ath3k driver load
2766 * patch and sysconfig files */
2767 if (le16_to_cpu(udev->descriptor.bcdDevice) <= 0x0001)
2768 return -ENODEV;
2769 }
2770
98921dbd 2771 data = devm_kzalloc(&intf->dev, sizeof(*data), GFP_KERNEL);
5e23b923
MH
2772 if (!data)
2773 return -ENOMEM;
2774
2775 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
2776 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
2777
2778 if (!data->intr_ep && usb_endpoint_is_int_in(ep_desc)) {
2779 data->intr_ep = ep_desc;
2780 continue;
2781 }
2782
2783 if (!data->bulk_tx_ep && usb_endpoint_is_bulk_out(ep_desc)) {
2784 data->bulk_tx_ep = ep_desc;
2785 continue;
2786 }
2787
2788 if (!data->bulk_rx_ep && usb_endpoint_is_bulk_in(ep_desc)) {
2789 data->bulk_rx_ep = ep_desc;
2790 continue;
2791 }
2792 }
2793
98921dbd 2794 if (!data->intr_ep || !data->bulk_tx_ep || !data->bulk_rx_ep)
5e23b923 2795 return -ENODEV;
5e23b923 2796
893ba544
MH
2797 if (id->driver_info & BTUSB_AMP) {
2798 data->cmdreq_type = USB_TYPE_CLASS | 0x01;
2799 data->cmdreq = 0x2b;
2800 } else {
2801 data->cmdreq_type = USB_TYPE_CLASS;
2802 data->cmdreq = 0x00;
2803 }
7a9d4020 2804
5e23b923 2805 data->udev = interface_to_usbdev(intf);
5fbcd260 2806 data->intf = intf;
5e23b923 2807
5e23b923 2808 INIT_WORK(&data->work, btusb_work);
7bee549e 2809 INIT_WORK(&data->waker, btusb_waker);
803b5836
MH
2810 init_usb_anchor(&data->deferred);
2811 init_usb_anchor(&data->tx_anchor);
7bee549e 2812 spin_lock_init(&data->txlock);
5e23b923 2813
5e23b923
MH
2814 init_usb_anchor(&data->intr_anchor);
2815 init_usb_anchor(&data->bulk_anchor);
9bfa35fe 2816 init_usb_anchor(&data->isoc_anchor);
9d08f504 2817 init_usb_anchor(&data->diag_anchor);
803b5836 2818 spin_lock_init(&data->rxlock);
5e23b923 2819
cda0dd78
MH
2820 if (id->driver_info & BTUSB_INTEL_NEW) {
2821 data->recv_event = btusb_recv_event_intel;
2822 data->recv_bulk = btusb_recv_bulk_intel;
2823 set_bit(BTUSB_BOOTLOADER, &data->flags);
2824 } else {
2825 data->recv_event = hci_recv_frame;
2826 data->recv_bulk = btusb_recv_bulk;
2827 }
2cbd3f5c 2828
5e23b923 2829 hdev = hci_alloc_dev();
98921dbd 2830 if (!hdev)
5e23b923 2831 return -ENOMEM;
5e23b923 2832
c13854ce 2833 hdev->bus = HCI_USB;
155961e8 2834 hci_set_drvdata(hdev, data);
5e23b923 2835
893ba544
MH
2836 if (id->driver_info & BTUSB_AMP)
2837 hdev->dev_type = HCI_AMP;
2838 else
2839 hdev->dev_type = HCI_BREDR;
2840
5e23b923
MH
2841 data->hdev = hdev;
2842
2843 SET_HCIDEV_DEV(hdev, &intf->dev);
2844
9f8f962c
MH
2845 hdev->open = btusb_open;
2846 hdev->close = btusb_close;
2847 hdev->flush = btusb_flush;
2848 hdev->send = btusb_send_frame;
2849 hdev->notify = btusb_notify;
2850
2851 if (id->driver_info & BTUSB_BCM92035)
2852 hdev->setup = btusb_setup_bcm92035;
5e23b923 2853
c2bfb100 2854#ifdef CONFIG_BT_HCIBTUSB_BCM
abbaf50e 2855 if (id->driver_info & BTUSB_BCM_PATCHRAM) {
c2bfb100 2856 hdev->setup = btbcm_setup_patchram;
9d08f504 2857 hdev->set_diag = btusb_bcm_set_diag;
1df1f591 2858 hdev->set_bdaddr = btbcm_set_bdaddr;
9d08f504
MH
2859
2860 /* Broadcom LM_DIAG Interface numbers are hardcoded */
2861 data->diag = usb_ifnum_to_if(data->udev, 2);
abbaf50e 2862 }
10d4c673 2863
9d08f504 2864 if (id->driver_info & BTUSB_BCM_APPLE) {
c2bfb100 2865 hdev->setup = btbcm_setup_apple;
9d08f504
MH
2866 hdev->set_diag = btusb_bcm_set_diag;
2867
2868 /* Broadcom LM_DIAG Interface numbers are hardcoded */
2869 data->diag = usb_ifnum_to_if(data->udev, 2);
2870 }
c2bfb100 2871#endif
17b2772b 2872
cb8d6597 2873 if (id->driver_info & BTUSB_INTEL) {
dffd30ee 2874 hdev->setup = btusb_setup_intel;
bfbd45e9 2875 hdev->shutdown = btusb_shutdown_intel;
4185a0f5 2876 hdev->set_bdaddr = btintel_set_bdaddr;
c33fb9b4 2877 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
c1154842 2878 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
cb8d6597 2879 }
dffd30ee 2880
cda0dd78
MH
2881 if (id->driver_info & BTUSB_INTEL_NEW) {
2882 hdev->send = btusb_send_frame_intel;
2883 hdev->setup = btusb_setup_intel_new;
eeb6abe9 2884 hdev->hw_error = btintel_hw_error;
6d2e50d2 2885 hdev->set_diag = btintel_set_diag;
4185a0f5 2886 hdev->set_bdaddr = btintel_set_bdaddr;
b970c5ba 2887 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
cda0dd78
MH
2888 }
2889
ae8df494
AK
2890 if (id->driver_info & BTUSB_MARVELL)
2891 hdev->set_bdaddr = btusb_set_bdaddr_marvell;
2892
661cf88a
MH
2893 if (id->driver_info & BTUSB_SWAVE) {
2894 set_bit(HCI_QUIRK_FIXUP_INQUIRY_MODE, &hdev->quirks);
d57dbe77 2895 set_bit(HCI_QUIRK_BROKEN_LOCAL_COMMANDS, &hdev->quirks);
661cf88a 2896 }
d57dbe77 2897
40df783d
MH
2898 if (id->driver_info & BTUSB_INTEL_BOOT)
2899 set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
2900
79f0c87d 2901 if (id->driver_info & BTUSB_ATH3012) {
5859223e 2902 hdev->set_bdaddr = btusb_set_bdaddr_ath3012;
3d50d51a 2903 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
79f0c87d
JP
2904 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
2905 }
5859223e 2906
3267c884
KBYT
2907 if (id->driver_info & BTUSB_QCA_ROME) {
2908 data->setup_on_usb = btusb_setup_qca;
2909 hdev->set_bdaddr = btusb_set_bdaddr_ath3012;
2910 }
2911
db33c77d 2912#ifdef CONFIG_BT_HCIBTUSB_RTL
04b8c814 2913 if (id->driver_info & BTUSB_REALTEK) {
db33c77d 2914 hdev->setup = btrtl_setup_realtek;
04b8c814
DD
2915
2916 /* Realtek devices lose their updated firmware over suspend,
2917 * but the USB hub doesn't notice any status change.
2918 * Explicitly request a device reset on resume.
2919 */
2920 set_bit(BTUSB_RESET_RESUME, &data->flags);
2921 }
db33c77d 2922#endif
a2698a9b 2923
893ba544
MH
2924 if (id->driver_info & BTUSB_AMP) {
2925 /* AMP controllers do not support SCO packets */
2926 data->isoc = NULL;
2927 } else {
2928 /* Interface numbers are hardcoded in the specification */
2929 data->isoc = usb_ifnum_to_if(data->udev, 1);
2930 }
9bfa35fe 2931
7a9d4020 2932 if (!reset)
a6c511c6 2933 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
cfeb4145
MH
2934
2935 if (force_scofix || id->driver_info & BTUSB_WRONG_SCO_MTU) {
2936 if (!disable_scofix)
2937 set_bit(HCI_QUIRK_FIXUP_BUFFER_SIZE, &hdev->quirks);
2938 }
2939
9bfa35fe
MH
2940 if (id->driver_info & BTUSB_BROKEN_ISOC)
2941 data->isoc = NULL;
2942
7a9d4020
MH
2943 if (id->driver_info & BTUSB_DIGIANSWER) {
2944 data->cmdreq_type = USB_TYPE_VENDOR;
a6c511c6 2945 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
7a9d4020
MH
2946 }
2947
2948 if (id->driver_info & BTUSB_CSR) {
2949 struct usb_device *udev = data->udev;
81cac64b 2950 u16 bcdDevice = le16_to_cpu(udev->descriptor.bcdDevice);
7a9d4020
MH
2951
2952 /* Old firmware would otherwise execute USB reset */
81cac64b 2953 if (bcdDevice < 0x117)
a6c511c6 2954 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
81cac64b
MH
2955
2956 /* Fake CSR devices with broken commands */
6cafcd95 2957 if (bcdDevice <= 0x100 || bcdDevice == 0x134)
81cac64b 2958 hdev->setup = btusb_setup_csr;
49c989a0
JP
2959
2960 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
7a9d4020
MH
2961 }
2962
cfeb4145 2963 if (id->driver_info & BTUSB_SNIFFER) {
9bfa35fe 2964 struct usb_device *udev = data->udev;
cfeb4145 2965
7a9d4020 2966 /* New sniffer firmware has crippled HCI interface */
cfeb4145
MH
2967 if (le16_to_cpu(udev->descriptor.bcdDevice) > 0x997)
2968 set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
2969 }
2970
3a5ef20c
MH
2971 if (id->driver_info & BTUSB_INTEL_BOOT) {
2972 /* A bug in the bootloader causes that interrupt interface is
2973 * only enabled after receiving SetInterface(0, AltSetting=0).
2974 */
2975 err = usb_set_interface(data->udev, 0, 0);
2976 if (err < 0) {
2977 BT_ERR("failed to set interface 0, alt 0 %d", err);
2978 hci_free_dev(hdev);
2979 return err;
2980 }
2981 }
2982
9bfa35fe
MH
2983 if (data->isoc) {
2984 err = usb_driver_claim_interface(&btusb_driver,
89e7533d 2985 data->isoc, data);
9bfa35fe
MH
2986 if (err < 0) {
2987 hci_free_dev(hdev);
9bfa35fe
MH
2988 return err;
2989 }
2990 }
2991
9d08f504
MH
2992#ifdef CONFIG_BT_HCIBTUSB_BCM
2993 if (data->diag) {
2994 if (!usb_driver_claim_interface(&btusb_driver,
2995 data->diag, data))
2996 __set_diag_interface(hdev);
2997 else
2998 data->diag = NULL;
2999 }
3000#endif
3001
5e23b923
MH
3002 err = hci_register_dev(hdev);
3003 if (err < 0) {
3004 hci_free_dev(hdev);
5e23b923
MH
3005 return err;
3006 }
3007
3008 usb_set_intfdata(intf, data);
3009
3010 return 0;
3011}
3012
3013static void btusb_disconnect(struct usb_interface *intf)
3014{
3015 struct btusb_data *data = usb_get_intfdata(intf);
3016 struct hci_dev *hdev;
3017
3018 BT_DBG("intf %p", intf);
3019
3020 if (!data)
3021 return;
3022
3023 hdev = data->hdev;
5fbcd260
MH
3024 usb_set_intfdata(data->intf, NULL);
3025
3026 if (data->isoc)
3027 usb_set_intfdata(data->isoc, NULL);
5e23b923 3028
9d08f504
MH
3029 if (data->diag)
3030 usb_set_intfdata(data->diag, NULL);
3031
5e23b923
MH
3032 hci_unregister_dev(hdev);
3033
9d08f504
MH
3034 if (intf == data->intf) {
3035 if (data->isoc)
3036 usb_driver_release_interface(&btusb_driver, data->isoc);
3037 if (data->diag)
3038 usb_driver_release_interface(&btusb_driver, data->diag);
3039 } else if (intf == data->isoc) {
3040 if (data->diag)
3041 usb_driver_release_interface(&btusb_driver, data->diag);
5fbcd260 3042 usb_driver_release_interface(&btusb_driver, data->intf);
9d08f504
MH
3043 } else if (intf == data->diag) {
3044 usb_driver_release_interface(&btusb_driver, data->intf);
3045 if (data->isoc)
3046 usb_driver_release_interface(&btusb_driver, data->isoc);
3047 }
5fbcd260 3048
5e23b923
MH
3049 hci_free_dev(hdev);
3050}
3051
7bee549e 3052#ifdef CONFIG_PM
6a88adf2
MH
3053static int btusb_suspend(struct usb_interface *intf, pm_message_t message)
3054{
3055 struct btusb_data *data = usb_get_intfdata(intf);
3056
3057 BT_DBG("intf %p", intf);
3058
3059 if (data->suspend_count++)
3060 return 0;
3061
7bee549e 3062 spin_lock_irq(&data->txlock);
5b1b0b81 3063 if (!(PMSG_IS_AUTO(message) && data->tx_in_flight)) {
7bee549e
ON
3064 set_bit(BTUSB_SUSPENDING, &data->flags);
3065 spin_unlock_irq(&data->txlock);
3066 } else {
3067 spin_unlock_irq(&data->txlock);
3068 data->suspend_count--;
3069 return -EBUSY;
3070 }
3071
6a88adf2
MH
3072 cancel_work_sync(&data->work);
3073
7bee549e 3074 btusb_stop_traffic(data);
6a88adf2
MH
3075 usb_kill_anchored_urbs(&data->tx_anchor);
3076
04b8c814
DD
3077 /* Optionally request a device reset on resume, but only when
3078 * wakeups are disabled. If wakeups are enabled we assume the
3079 * device will stay powered up throughout suspend.
3080 */
3081 if (test_bit(BTUSB_RESET_RESUME, &data->flags) &&
3082 !device_may_wakeup(&data->udev->dev))
3083 data->udev->reset_resume = 1;
3084
6a88adf2
MH
3085 return 0;
3086}
3087
7bee549e
ON
3088static void play_deferred(struct btusb_data *data)
3089{
3090 struct urb *urb;
3091 int err;
3092
3093 while ((urb = usb_get_from_anchor(&data->deferred))) {
3094 err = usb_submit_urb(urb, GFP_ATOMIC);
3095 if (err < 0)
3096 break;
3097
3098 data->tx_in_flight++;
3099 }
3100 usb_scuttle_anchored_urbs(&data->deferred);
3101}
3102
6a88adf2
MH
3103static int btusb_resume(struct usb_interface *intf)
3104{
3105 struct btusb_data *data = usb_get_intfdata(intf);
3106 struct hci_dev *hdev = data->hdev;
7bee549e 3107 int err = 0;
6a88adf2
MH
3108
3109 BT_DBG("intf %p", intf);
3110
3111 if (--data->suspend_count)
3112 return 0;
3113
3114 if (!test_bit(HCI_RUNNING, &hdev->flags))
7bee549e 3115 goto done;
6a88adf2
MH
3116
3117 if (test_bit(BTUSB_INTR_RUNNING, &data->flags)) {
3118 err = btusb_submit_intr_urb(hdev, GFP_NOIO);
3119 if (err < 0) {
3120 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
7bee549e 3121 goto failed;
6a88adf2
MH
3122 }
3123 }
3124
3125 if (test_bit(BTUSB_BULK_RUNNING, &data->flags)) {
43c2e57f
MH
3126 err = btusb_submit_bulk_urb(hdev, GFP_NOIO);
3127 if (err < 0) {
6a88adf2 3128 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
7bee549e
ON
3129 goto failed;
3130 }
3131
3132 btusb_submit_bulk_urb(hdev, GFP_NOIO);
6a88adf2
MH
3133 }
3134
3135 if (test_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
3136 if (btusb_submit_isoc_urb(hdev, GFP_NOIO) < 0)
3137 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
3138 else
3139 btusb_submit_isoc_urb(hdev, GFP_NOIO);
3140 }
3141
7bee549e
ON
3142 spin_lock_irq(&data->txlock);
3143 play_deferred(data);
3144 clear_bit(BTUSB_SUSPENDING, &data->flags);
3145 spin_unlock_irq(&data->txlock);
3146 schedule_work(&data->work);
3147
6a88adf2 3148 return 0;
7bee549e
ON
3149
3150failed:
3151 usb_scuttle_anchored_urbs(&data->deferred);
3152done:
3153 spin_lock_irq(&data->txlock);
3154 clear_bit(BTUSB_SUSPENDING, &data->flags);
3155 spin_unlock_irq(&data->txlock);
3156
3157 return err;
6a88adf2 3158}
7bee549e 3159#endif
6a88adf2 3160
5e23b923
MH
3161static struct usb_driver btusb_driver = {
3162 .name = "btusb",
3163 .probe = btusb_probe,
3164 .disconnect = btusb_disconnect,
7bee549e 3165#ifdef CONFIG_PM
6a88adf2
MH
3166 .suspend = btusb_suspend,
3167 .resume = btusb_resume,
7bee549e 3168#endif
5e23b923 3169 .id_table = btusb_table,
7bee549e 3170 .supports_autosuspend = 1,
e1f12eb6 3171 .disable_hub_initiated_lpm = 1,
5e23b923
MH
3172};
3173
93f1508c 3174module_usb_driver(btusb_driver);
5e23b923 3175
cfeb4145
MH
3176module_param(disable_scofix, bool, 0644);
3177MODULE_PARM_DESC(disable_scofix, "Disable fixup of wrong SCO buffer size");
3178
3179module_param(force_scofix, bool, 0644);
3180MODULE_PARM_DESC(force_scofix, "Force fixup of wrong SCO buffers size");
3181
3182module_param(reset, bool, 0644);
3183MODULE_PARM_DESC(reset, "Send HCI reset command on initialization");
3184
5e23b923
MH
3185MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
3186MODULE_DESCRIPTION("Generic Bluetooth USB driver ver " VERSION);
3187MODULE_VERSION(VERSION);
3188MODULE_LICENSE("GPL");
This page took 0.78914 seconds and 5 git commands to generate.