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