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