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