]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/bluetooth/btusb.c
Bluetooth: Fix SMP channel registration for unconfigured controllers
[mirror_ubuntu-artful-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
7bee549e 31#define VERSION "0.6"
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
d57dbe77 52#define BTUSB_AVM 0x1000
5e23b923 53
54265202 54static const struct usb_device_id btusb_table[] = {
5e23b923
MH
55 /* Generic Bluetooth USB device */
56 { USB_DEVICE_INFO(0xe0, 0x01, 0x01) },
57
1fa6535f
HR
58 /* Apple-specific (Broadcom) devices */
59 { USB_VENDOR_AND_INTERFACE_INFO(0x05ac, 0xff, 0x01, 0x01) },
60
178c059e
CYC
61 /* MediaTek MT76x0E */
62 { USB_DEVICE(0x0e8d, 0x763f) },
63
c510eae3 64 /* Broadcom SoftSailing reporting vendor specific */
2e8b5063 65 { USB_DEVICE(0x0a5c, 0x21e1) },
c510eae3 66
3cd01976
NI
67 /* Apple MacBookPro 7,1 */
68 { USB_DEVICE(0x05ac, 0x8213) },
69
0a79f674
CL
70 /* Apple iMac11,1 */
71 { USB_DEVICE(0x05ac, 0x8215) },
72
9c047157
NI
73 /* Apple MacBookPro6,2 */
74 { USB_DEVICE(0x05ac, 0x8218) },
75
3e3ede7d
EH
76 /* Apple MacBookAir3,1, MacBookAir3,2 */
77 { USB_DEVICE(0x05ac, 0x821b) },
78
a63b723d
PAVM
79 /* Apple MacBookAir4,1 */
80 { USB_DEVICE(0x05ac, 0x821f) },
81
88d377b6
MAP
82 /* Apple MacBookPro8,2 */
83 { USB_DEVICE(0x05ac, 0x821a) },
84
f78b6826
JK
85 /* Apple MacMini5,1 */
86 { USB_DEVICE(0x05ac, 0x8281) },
87
cfeb4145 88 /* AVM BlueFRITZ! USB v2.0 */
d57dbe77 89 { USB_DEVICE(0x057c, 0x3800), .driver_info = BTUSB_AVM },
cfeb4145
MH
90
91 /* Bluetooth Ultraport Module from IBM */
92 { USB_DEVICE(0x04bf, 0x030a) },
93
94 /* ALPS Modules with non-standard id */
95 { USB_DEVICE(0x044e, 0x3001) },
96 { USB_DEVICE(0x044e, 0x3002) },
97
98 /* Ericsson with non-standard id */
99 { USB_DEVICE(0x0bdb, 0x1002) },
100
101 /* Canyon CN-BTU1 with HID interfaces */
7a9d4020 102 { USB_DEVICE(0x0c10, 0x0000) },
cfeb4145 103
d13431ca 104 /* Broadcom BCM20702A0 */
0b880062
AS
105 { USB_DEVICE(0x0489, 0xe042) },
106 { USB_DEVICE(0x04ca, 0x2003) },
1ee3ff61 107 { USB_DEVICE(0x0b05, 0x17b5) },
38a172be 108 { USB_DEVICE(0x0b05, 0x17cb) },
d13431ca 109 { USB_DEVICE(0x413c, 0x8197) },
a86c02ea
F
110 { USB_DEVICE(0x13d3, 0x3404),
111 .driver_info = BTUSB_BCM_PATCHRAM },
d13431ca 112
98514036 113 /* Foxconn - Hon Hai */
6029ddc2
HS
114 { USB_VENDOR_AND_INTERFACE_INFO(0x0489, 0xff, 0x01, 0x01),
115 .driver_info = BTUSB_BCM_PATCHRAM },
98514036 116
0b880062 117 /* Broadcom devices with vendor specific id */
10d4c673
PG
118 { USB_VENDOR_AND_INTERFACE_INFO(0x0a5c, 0xff, 0x01, 0x01),
119 .driver_info = BTUSB_BCM_PATCHRAM },
92c385f4 120
c2aef6e8
MH
121 /* ASUSTek Computer - Broadcom based */
122 { USB_VENDOR_AND_INTERFACE_INFO(0x0b05, 0xff, 0x01, 0x01) },
123
5bcecf32
KB
124 /* Belkin F8065bf - Broadcom based */
125 { USB_VENDOR_AND_INTERFACE_INFO(0x050d, 0xff, 0x01, 0x01) },
126
9113bfd8
JK
127 /* IMC Networks - Broadcom based */
128 { USB_VENDOR_AND_INTERFACE_INFO(0x13d3, 0xff, 0x01, 0x01) },
129
40df783d 130 /* Intel Bluetooth USB Bootloader (RAM module) */
d92f2df0
MH
131 { USB_DEVICE(0x8087, 0x0a5a),
132 .driver_info = BTUSB_INTEL_BOOT | BTUSB_BROKEN_ISOC },
40df783d 133
5e23b923
MH
134 { } /* Terminating entry */
135};
136
137MODULE_DEVICE_TABLE(usb, btusb_table);
138
54265202 139static const struct usb_device_id blacklist_table[] = {
cfeb4145
MH
140 /* CSR BlueCore devices */
141 { USB_DEVICE(0x0a12, 0x0001), .driver_info = BTUSB_CSR },
142
143 /* Broadcom BCM2033 without firmware */
144 { USB_DEVICE(0x0a5c, 0x2033), .driver_info = BTUSB_IGNORE },
145
be93112a 146 /* Atheros 3011 with sflash firmware */
0b880062
AS
147 { USB_DEVICE(0x0489, 0xe027), .driver_info = BTUSB_IGNORE },
148 { USB_DEVICE(0x0489, 0xe03d), .driver_info = BTUSB_IGNORE },
149 { USB_DEVICE(0x0930, 0x0215), .driver_info = BTUSB_IGNORE },
be93112a 150 { USB_DEVICE(0x0cf3, 0x3002), .driver_info = BTUSB_IGNORE },
6eda541d 151 { USB_DEVICE(0x0cf3, 0xe019), .driver_info = BTUSB_IGNORE },
2a7bcccc 152 { USB_DEVICE(0x13d3, 0x3304), .driver_info = BTUSB_IGNORE },
be93112a 153
509e7861
CYC
154 /* Atheros AR9285 Malbec with sflash firmware */
155 { USB_DEVICE(0x03f0, 0x311d), .driver_info = BTUSB_IGNORE },
156
d9f51b51 157 /* Atheros 3012 with sflash firmware */
0b880062
AS
158 { USB_DEVICE(0x0489, 0xe04d), .driver_info = BTUSB_ATH3012 },
159 { USB_DEVICE(0x0489, 0xe04e), .driver_info = BTUSB_ATH3012 },
160 { USB_DEVICE(0x0489, 0xe056), .driver_info = BTUSB_ATH3012 },
161 { USB_DEVICE(0x0489, 0xe057), .driver_info = BTUSB_ATH3012 },
162 { USB_DEVICE(0x0489, 0xe05f), .driver_info = BTUSB_ATH3012 },
4b552bc9 163 { USB_DEVICE(0x0489, 0xe078), .driver_info = BTUSB_ATH3012 },
0b880062
AS
164 { USB_DEVICE(0x04c5, 0x1330), .driver_info = BTUSB_ATH3012 },
165 { USB_DEVICE(0x04ca, 0x3004), .driver_info = BTUSB_ATH3012 },
166 { USB_DEVICE(0x04ca, 0x3005), .driver_info = BTUSB_ATH3012 },
167 { USB_DEVICE(0x04ca, 0x3006), .driver_info = BTUSB_ATH3012 },
1fb4e09a 168 { USB_DEVICE(0x04ca, 0x3007), .driver_info = BTUSB_ATH3012 },
0b880062
AS
169 { USB_DEVICE(0x04ca, 0x3008), .driver_info = BTUSB_ATH3012 },
170 { USB_DEVICE(0x04ca, 0x300b), .driver_info = BTUSB_ATH3012 },
134d3b35 171 { USB_DEVICE(0x04ca, 0x3010), .driver_info = BTUSB_ATH3012 },
0b880062
AS
172 { USB_DEVICE(0x0930, 0x0219), .driver_info = BTUSB_ATH3012 },
173 { USB_DEVICE(0x0930, 0x0220), .driver_info = BTUSB_ATH3012 },
89d2975f 174 { USB_DEVICE(0x0930, 0x0227), .driver_info = BTUSB_ATH3012 },
a735f9e2 175 { USB_DEVICE(0x0b05, 0x17d0), .driver_info = BTUSB_ATH3012 },
d66629c1 176 { USB_DEVICE(0x0cf3, 0x0036), .driver_info = BTUSB_ATH3012 },
2d25f8b4 177 { USB_DEVICE(0x0cf3, 0x3004), .driver_info = BTUSB_ATH3012 },
94a32d10 178 { USB_DEVICE(0x0cf3, 0x3008), .driver_info = BTUSB_ATH3012 },
07c0ea87 179 { USB_DEVICE(0x0cf3, 0x311d), .driver_info = BTUSB_ATH3012 },
b131237c 180 { USB_DEVICE(0x0cf3, 0x311e), .driver_info = BTUSB_ATH3012 },
1e56f1eb 181 { USB_DEVICE(0x0cf3, 0x311f), .driver_info = BTUSB_ATH3012 },
0b880062 182 { USB_DEVICE(0x0cf3, 0x3121), .driver_info = BTUSB_ATH3012 },
ebaf5795 183 { USB_DEVICE(0x0cf3, 0x817a), .driver_info = BTUSB_ATH3012 },
0b880062 184 { USB_DEVICE(0x0cf3, 0xe003), .driver_info = BTUSB_ATH3012 },
ac71311e 185 { USB_DEVICE(0x0cf3, 0xe004), .driver_info = BTUSB_ATH3012 },
0a3658cc 186 { USB_DEVICE(0x0cf3, 0xe005), .driver_info = BTUSB_ATH3012 },
0b880062
AS
187 { USB_DEVICE(0x13d3, 0x3362), .driver_info = BTUSB_ATH3012 },
188 { USB_DEVICE(0x13d3, 0x3375), .driver_info = BTUSB_ATH3012 },
eed307e2 189 { USB_DEVICE(0x13d3, 0x3393), .driver_info = BTUSB_ATH3012 },
5b77a1f3 190 { USB_DEVICE(0x13d3, 0x3402), .driver_info = BTUSB_ATH3012 },
3bb30a7c 191 { USB_DEVICE(0x13d3, 0x3408), .driver_info = BTUSB_ATH3012 },
fa2f1394 192 { USB_DEVICE(0x13d3, 0x3432), .driver_info = BTUSB_ATH3012 },
d9f51b51 193
e9036e33
CYC
194 /* Atheros AR5BBU12 with sflash firmware */
195 { USB_DEVICE(0x0489, 0xe02c), .driver_info = BTUSB_IGNORE },
196
85d59726 197 /* Atheros AR5BBU12 with sflash firmware */
bc21fde2 198 { USB_DEVICE(0x0489, 0xe036), .driver_info = BTUSB_ATH3012 },
0b880062 199 { USB_DEVICE(0x0489, 0xe03c), .driver_info = BTUSB_ATH3012 },
85d59726 200
cfeb4145 201 /* Broadcom BCM2035 */
7a9d4020 202 { USB_DEVICE(0x0a5c, 0x2009), .driver_info = BTUSB_BCM92035 },
0b880062
AS
203 { USB_DEVICE(0x0a5c, 0x200a), .driver_info = BTUSB_WRONG_SCO_MTU },
204 { USB_DEVICE(0x0a5c, 0x2035), .driver_info = BTUSB_WRONG_SCO_MTU },
cfeb4145
MH
205
206 /* Broadcom BCM2045 */
7a9d4020
MH
207 { USB_DEVICE(0x0a5c, 0x2039), .driver_info = BTUSB_WRONG_SCO_MTU },
208 { USB_DEVICE(0x0a5c, 0x2101), .driver_info = BTUSB_WRONG_SCO_MTU },
bdbef3d6 209
cfeb4145 210 /* IBM/Lenovo ThinkPad with Broadcom chip */
7a9d4020
MH
211 { USB_DEVICE(0x0a5c, 0x201e), .driver_info = BTUSB_WRONG_SCO_MTU },
212 { USB_DEVICE(0x0a5c, 0x2110), .driver_info = BTUSB_WRONG_SCO_MTU },
cfeb4145
MH
213
214 /* HP laptop with Broadcom chip */
7a9d4020 215 { USB_DEVICE(0x03f0, 0x171d), .driver_info = BTUSB_WRONG_SCO_MTU },
cfeb4145
MH
216
217 /* Dell laptop with Broadcom chip */
7a9d4020 218 { USB_DEVICE(0x413c, 0x8126), .driver_info = BTUSB_WRONG_SCO_MTU },
cfeb4145 219
5ddd4a60 220 /* Dell Wireless 370 and 410 devices */
7a9d4020 221 { USB_DEVICE(0x413c, 0x8152), .driver_info = BTUSB_WRONG_SCO_MTU },
5ddd4a60 222 { USB_DEVICE(0x413c, 0x8156), .driver_info = BTUSB_WRONG_SCO_MTU },
cfeb4145 223
7a9d4020
MH
224 /* Belkin F8T012 and F8T013 devices */
225 { USB_DEVICE(0x050d, 0x0012), .driver_info = BTUSB_WRONG_SCO_MTU },
226 { USB_DEVICE(0x050d, 0x0013), .driver_info = BTUSB_WRONG_SCO_MTU },
cfeb4145 227
5ddd4a60
MH
228 /* Asus WL-BTD202 device */
229 { USB_DEVICE(0x0b05, 0x1715), .driver_info = BTUSB_WRONG_SCO_MTU },
230
231 /* Kensington Bluetooth USB adapter */
232 { USB_DEVICE(0x047d, 0x105e), .driver_info = BTUSB_WRONG_SCO_MTU },
233
cfeb4145
MH
234 /* RTX Telecom based adapters with buggy SCO support */
235 { USB_DEVICE(0x0400, 0x0807), .driver_info = BTUSB_BROKEN_ISOC },
236 { USB_DEVICE(0x0400, 0x080a), .driver_info = BTUSB_BROKEN_ISOC },
237
238 /* CONWISE Technology based adapters with buggy SCO support */
239 { USB_DEVICE(0x0e5e, 0x6622), .driver_info = BTUSB_BROKEN_ISOC },
240
cfeb4145
MH
241 /* Digianswer devices */
242 { USB_DEVICE(0x08fd, 0x0001), .driver_info = BTUSB_DIGIANSWER },
243 { USB_DEVICE(0x08fd, 0x0002), .driver_info = BTUSB_IGNORE },
244
245 /* CSR BlueCore Bluetooth Sniffer */
4f64fa80
MH
246 { USB_DEVICE(0x0a12, 0x0002),
247 .driver_info = BTUSB_SNIFFER | BTUSB_BROKEN_ISOC },
cfeb4145
MH
248
249 /* Frontline ComProbe Bluetooth Sniffer */
4f64fa80
MH
250 { USB_DEVICE(0x16d3, 0x0002),
251 .driver_info = BTUSB_SNIFFER | BTUSB_BROKEN_ISOC },
cfeb4145 252
dffd30ee
THJA
253 /* Intel Bluetooth device */
254 { USB_DEVICE(0x8087, 0x07dc), .driver_info = BTUSB_INTEL },
ef4e5e4a 255 { USB_DEVICE(0x8087, 0x0a2a), .driver_info = BTUSB_INTEL },
dffd30ee 256
ae8df494
AK
257 /* Marvell device */
258 { USB_DEVICE(0x1286, 0x2044), .driver_info = BTUSB_MARVELL },
259 { USB_DEVICE(0x1286, 0x2046), .driver_info = BTUSB_MARVELL },
260
5e23b923
MH
261 { } /* Terminating entry */
262};
263
9bfa35fe
MH
264#define BTUSB_MAX_ISOC_FRAMES 10
265
5e23b923
MH
266#define BTUSB_INTR_RUNNING 0
267#define BTUSB_BULK_RUNNING 1
9bfa35fe 268#define BTUSB_ISOC_RUNNING 2
7bee549e 269#define BTUSB_SUSPENDING 3
08b8b6c4 270#define BTUSB_DID_ISO_RESUME 4
5e23b923
MH
271
272struct btusb_data {
273 struct hci_dev *hdev;
274 struct usb_device *udev;
5fbcd260 275 struct usb_interface *intf;
9bfa35fe 276 struct usb_interface *isoc;
5e23b923 277
5e23b923
MH
278 unsigned long flags;
279
280 struct work_struct work;
7bee549e 281 struct work_struct waker;
5e23b923 282
803b5836 283 struct usb_anchor deferred;
5e23b923 284 struct usb_anchor tx_anchor;
803b5836
MH
285 int tx_in_flight;
286 spinlock_t txlock;
287
5e23b923
MH
288 struct usb_anchor intr_anchor;
289 struct usb_anchor bulk_anchor;
9bfa35fe 290 struct usb_anchor isoc_anchor;
803b5836
MH
291 spinlock_t rxlock;
292
293 struct sk_buff *evt_skb;
294 struct sk_buff *acl_skb;
295 struct sk_buff *sco_skb;
5e23b923
MH
296
297 struct usb_endpoint_descriptor *intr_ep;
298 struct usb_endpoint_descriptor *bulk_tx_ep;
299 struct usb_endpoint_descriptor *bulk_rx_ep;
9bfa35fe
MH
300 struct usb_endpoint_descriptor *isoc_tx_ep;
301 struct usb_endpoint_descriptor *isoc_rx_ep;
302
7a9d4020
MH
303 __u8 cmdreq_type;
304
43c2e57f 305 unsigned int sco_num;
9bfa35fe 306 int isoc_altsetting;
6a88adf2 307 int suspend_count;
2cbd3f5c
MH
308
309 int (*recv_bulk)(struct btusb_data *data, void *buffer, int count);
5e23b923
MH
310};
311
803b5836
MH
312static inline void btusb_free_frags(struct btusb_data *data)
313{
314 unsigned long flags;
315
316 spin_lock_irqsave(&data->rxlock, flags);
317
318 kfree_skb(data->evt_skb);
319 data->evt_skb = NULL;
320
321 kfree_skb(data->acl_skb);
322 data->acl_skb = NULL;
323
324 kfree_skb(data->sco_skb);
325 data->sco_skb = NULL;
326
327 spin_unlock_irqrestore(&data->rxlock, flags);
328}
329
1ffa4ad0
MH
330static int btusb_recv_intr(struct btusb_data *data, void *buffer, int count)
331{
803b5836
MH
332 struct sk_buff *skb;
333 int err = 0;
334
335 spin_lock(&data->rxlock);
336 skb = data->evt_skb;
337
338 while (count) {
339 int len;
340
341 if (!skb) {
342 skb = bt_skb_alloc(HCI_MAX_EVENT_SIZE, GFP_ATOMIC);
343 if (!skb) {
344 err = -ENOMEM;
345 break;
346 }
347
348 bt_cb(skb)->pkt_type = HCI_EVENT_PKT;
349 bt_cb(skb)->expect = HCI_EVENT_HDR_SIZE;
350 }
351
352 len = min_t(uint, bt_cb(skb)->expect, count);
353 memcpy(skb_put(skb, len), buffer, len);
354
355 count -= len;
356 buffer += len;
357 bt_cb(skb)->expect -= len;
358
359 if (skb->len == HCI_EVENT_HDR_SIZE) {
360 /* Complete event header */
361 bt_cb(skb)->expect = hci_event_hdr(skb)->plen;
362
363 if (skb_tailroom(skb) < bt_cb(skb)->expect) {
364 kfree_skb(skb);
365 skb = NULL;
366
367 err = -EILSEQ;
368 break;
369 }
370 }
371
372 if (bt_cb(skb)->expect == 0) {
373 /* Complete frame */
374 hci_recv_frame(data->hdev, skb);
375 skb = NULL;
376 }
377 }
378
379 data->evt_skb = skb;
380 spin_unlock(&data->rxlock);
381
382 return err;
1ffa4ad0
MH
383}
384
385static int btusb_recv_bulk(struct btusb_data *data, void *buffer, int count)
386{
803b5836
MH
387 struct sk_buff *skb;
388 int err = 0;
389
390 spin_lock(&data->rxlock);
391 skb = data->acl_skb;
392
393 while (count) {
394 int len;
395
396 if (!skb) {
397 skb = bt_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC);
398 if (!skb) {
399 err = -ENOMEM;
400 break;
401 }
402
403 bt_cb(skb)->pkt_type = HCI_ACLDATA_PKT;
404 bt_cb(skb)->expect = HCI_ACL_HDR_SIZE;
405 }
406
407 len = min_t(uint, bt_cb(skb)->expect, count);
408 memcpy(skb_put(skb, len), buffer, len);
409
410 count -= len;
411 buffer += len;
412 bt_cb(skb)->expect -= len;
413
414 if (skb->len == HCI_ACL_HDR_SIZE) {
415 __le16 dlen = hci_acl_hdr(skb)->dlen;
416
417 /* Complete ACL header */
418 bt_cb(skb)->expect = __le16_to_cpu(dlen);
419
420 if (skb_tailroom(skb) < bt_cb(skb)->expect) {
421 kfree_skb(skb);
422 skb = NULL;
423
424 err = -EILSEQ;
425 break;
426 }
427 }
428
429 if (bt_cb(skb)->expect == 0) {
430 /* Complete frame */
431 hci_recv_frame(data->hdev, skb);
432 skb = NULL;
433 }
434 }
435
436 data->acl_skb = skb;
437 spin_unlock(&data->rxlock);
438
439 return err;
1ffa4ad0
MH
440}
441
442static int btusb_recv_isoc(struct btusb_data *data, void *buffer, int count)
443{
803b5836
MH
444 struct sk_buff *skb;
445 int err = 0;
446
447 spin_lock(&data->rxlock);
448 skb = data->sco_skb;
449
450 while (count) {
451 int len;
452
453 if (!skb) {
454 skb = bt_skb_alloc(HCI_MAX_SCO_SIZE, GFP_ATOMIC);
455 if (!skb) {
456 err = -ENOMEM;
457 break;
458 }
459
460 bt_cb(skb)->pkt_type = HCI_SCODATA_PKT;
461 bt_cb(skb)->expect = HCI_SCO_HDR_SIZE;
462 }
463
464 len = min_t(uint, bt_cb(skb)->expect, count);
465 memcpy(skb_put(skb, len), buffer, len);
466
467 count -= len;
468 buffer += len;
469 bt_cb(skb)->expect -= len;
470
471 if (skb->len == HCI_SCO_HDR_SIZE) {
472 /* Complete SCO header */
473 bt_cb(skb)->expect = hci_sco_hdr(skb)->dlen;
474
475 if (skb_tailroom(skb) < bt_cb(skb)->expect) {
476 kfree_skb(skb);
477 skb = NULL;
478
479 err = -EILSEQ;
480 break;
481 }
482 }
483
484 if (bt_cb(skb)->expect == 0) {
485 /* Complete frame */
486 hci_recv_frame(data->hdev, skb);
487 skb = NULL;
488 }
489 }
490
491 data->sco_skb = skb;
492 spin_unlock(&data->rxlock);
493
494 return err;
1ffa4ad0
MH
495}
496
5e23b923
MH
497static void btusb_intr_complete(struct urb *urb)
498{
499 struct hci_dev *hdev = urb->context;
155961e8 500 struct btusb_data *data = hci_get_drvdata(hdev);
5e23b923
MH
501 int err;
502
89e7533d
MH
503 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
504 urb->actual_length);
5e23b923
MH
505
506 if (!test_bit(HCI_RUNNING, &hdev->flags))
507 return;
508
509 if (urb->status == 0) {
9bfa35fe
MH
510 hdev->stat.byte_rx += urb->actual_length;
511
1ffa4ad0
MH
512 if (btusb_recv_intr(data, urb->transfer_buffer,
513 urb->actual_length) < 0) {
5e23b923
MH
514 BT_ERR("%s corrupted event packet", hdev->name);
515 hdev->stat.err_rx++;
516 }
85560c4a
CC
517 } else if (urb->status == -ENOENT) {
518 /* Avoid suspend failed when usb_kill_urb */
519 return;
5e23b923
MH
520 }
521
522 if (!test_bit(BTUSB_INTR_RUNNING, &data->flags))
523 return;
524
7bee549e 525 usb_mark_last_busy(data->udev);
5e23b923
MH
526 usb_anchor_urb(urb, &data->intr_anchor);
527
528 err = usb_submit_urb(urb, GFP_ATOMIC);
529 if (err < 0) {
4935f1c1
PB
530 /* -EPERM: urb is being killed;
531 * -ENODEV: device got disconnected */
532 if (err != -EPERM && err != -ENODEV)
61faddf6 533 BT_ERR("%s urb %p failed to resubmit (%d)",
89e7533d 534 hdev->name, urb, -err);
5e23b923
MH
535 usb_unanchor_urb(urb);
536 }
537}
538
2eda66f4 539static int btusb_submit_intr_urb(struct hci_dev *hdev, gfp_t mem_flags)
5e23b923 540{
155961e8 541 struct btusb_data *data = hci_get_drvdata(hdev);
5e23b923
MH
542 struct urb *urb;
543 unsigned char *buf;
544 unsigned int pipe;
545 int err, size;
546
547 BT_DBG("%s", hdev->name);
548
9bfa35fe
MH
549 if (!data->intr_ep)
550 return -ENODEV;
551
2eda66f4 552 urb = usb_alloc_urb(0, mem_flags);
5e23b923
MH
553 if (!urb)
554 return -ENOMEM;
555
556 size = le16_to_cpu(data->intr_ep->wMaxPacketSize);
557
2eda66f4 558 buf = kmalloc(size, mem_flags);
5e23b923
MH
559 if (!buf) {
560 usb_free_urb(urb);
561 return -ENOMEM;
562 }
563
564 pipe = usb_rcvintpipe(data->udev, data->intr_ep->bEndpointAddress);
565
566 usb_fill_int_urb(urb, data->udev, pipe, buf, size,
89e7533d 567 btusb_intr_complete, hdev, data->intr_ep->bInterval);
5e23b923
MH
568
569 urb->transfer_flags |= URB_FREE_BUFFER;
570
571 usb_anchor_urb(urb, &data->intr_anchor);
572
2eda66f4 573 err = usb_submit_urb(urb, mem_flags);
5e23b923 574 if (err < 0) {
d4b8d1c9
PB
575 if (err != -EPERM && err != -ENODEV)
576 BT_ERR("%s urb %p submission failed (%d)",
89e7533d 577 hdev->name, urb, -err);
5e23b923 578 usb_unanchor_urb(urb);
5e23b923
MH
579 }
580
581 usb_free_urb(urb);
582
583 return err;
584}
585
586static void btusb_bulk_complete(struct urb *urb)
587{
588 struct hci_dev *hdev = urb->context;
155961e8 589 struct btusb_data *data = hci_get_drvdata(hdev);
5e23b923
MH
590 int err;
591
89e7533d
MH
592 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
593 urb->actual_length);
5e23b923
MH
594
595 if (!test_bit(HCI_RUNNING, &hdev->flags))
596 return;
597
598 if (urb->status == 0) {
9bfa35fe
MH
599 hdev->stat.byte_rx += urb->actual_length;
600
2cbd3f5c 601 if (data->recv_bulk(data, urb->transfer_buffer,
1ffa4ad0 602 urb->actual_length) < 0) {
5e23b923
MH
603 BT_ERR("%s corrupted ACL packet", hdev->name);
604 hdev->stat.err_rx++;
605 }
85560c4a
CC
606 } else if (urb->status == -ENOENT) {
607 /* Avoid suspend failed when usb_kill_urb */
608 return;
5e23b923
MH
609 }
610
611 if (!test_bit(BTUSB_BULK_RUNNING, &data->flags))
612 return;
613
614 usb_anchor_urb(urb, &data->bulk_anchor);
652fd781 615 usb_mark_last_busy(data->udev);
5e23b923
MH
616
617 err = usb_submit_urb(urb, GFP_ATOMIC);
618 if (err < 0) {
4935f1c1
PB
619 /* -EPERM: urb is being killed;
620 * -ENODEV: device got disconnected */
621 if (err != -EPERM && err != -ENODEV)
61faddf6 622 BT_ERR("%s urb %p failed to resubmit (%d)",
89e7533d 623 hdev->name, urb, -err);
5e23b923
MH
624 usb_unanchor_urb(urb);
625 }
626}
627
2eda66f4 628static int btusb_submit_bulk_urb(struct hci_dev *hdev, gfp_t mem_flags)
5e23b923 629{
155961e8 630 struct btusb_data *data = hci_get_drvdata(hdev);
5e23b923
MH
631 struct urb *urb;
632 unsigned char *buf;
633 unsigned int pipe;
290ba200 634 int err, size = HCI_MAX_FRAME_SIZE;
5e23b923
MH
635
636 BT_DBG("%s", hdev->name);
637
9bfa35fe
MH
638 if (!data->bulk_rx_ep)
639 return -ENODEV;
640
2eda66f4 641 urb = usb_alloc_urb(0, mem_flags);
5e23b923
MH
642 if (!urb)
643 return -ENOMEM;
644
2eda66f4 645 buf = kmalloc(size, mem_flags);
5e23b923
MH
646 if (!buf) {
647 usb_free_urb(urb);
648 return -ENOMEM;
649 }
650
651 pipe = usb_rcvbulkpipe(data->udev, data->bulk_rx_ep->bEndpointAddress);
652
89e7533d
MH
653 usb_fill_bulk_urb(urb, data->udev, pipe, buf, size,
654 btusb_bulk_complete, hdev);
5e23b923
MH
655
656 urb->transfer_flags |= URB_FREE_BUFFER;
657
7bee549e 658 usb_mark_last_busy(data->udev);
5e23b923
MH
659 usb_anchor_urb(urb, &data->bulk_anchor);
660
2eda66f4 661 err = usb_submit_urb(urb, mem_flags);
5e23b923 662 if (err < 0) {
d4b8d1c9
PB
663 if (err != -EPERM && err != -ENODEV)
664 BT_ERR("%s urb %p submission failed (%d)",
89e7533d 665 hdev->name, urb, -err);
5e23b923 666 usb_unanchor_urb(urb);
5e23b923
MH
667 }
668
669 usb_free_urb(urb);
670
671 return err;
672}
673
9bfa35fe
MH
674static void btusb_isoc_complete(struct urb *urb)
675{
676 struct hci_dev *hdev = urb->context;
155961e8 677 struct btusb_data *data = hci_get_drvdata(hdev);
9bfa35fe
MH
678 int i, err;
679
89e7533d
MH
680 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
681 urb->actual_length);
9bfa35fe
MH
682
683 if (!test_bit(HCI_RUNNING, &hdev->flags))
684 return;
685
686 if (urb->status == 0) {
687 for (i = 0; i < urb->number_of_packets; i++) {
688 unsigned int offset = urb->iso_frame_desc[i].offset;
689 unsigned int length = urb->iso_frame_desc[i].actual_length;
690
691 if (urb->iso_frame_desc[i].status)
692 continue;
693
694 hdev->stat.byte_rx += length;
695
1ffa4ad0
MH
696 if (btusb_recv_isoc(data, urb->transfer_buffer + offset,
697 length) < 0) {
9bfa35fe
MH
698 BT_ERR("%s corrupted SCO packet", hdev->name);
699 hdev->stat.err_rx++;
700 }
701 }
85560c4a
CC
702 } else if (urb->status == -ENOENT) {
703 /* Avoid suspend failed when usb_kill_urb */
704 return;
9bfa35fe
MH
705 }
706
707 if (!test_bit(BTUSB_ISOC_RUNNING, &data->flags))
708 return;
709
710 usb_anchor_urb(urb, &data->isoc_anchor);
711
712 err = usb_submit_urb(urb, GFP_ATOMIC);
713 if (err < 0) {
4935f1c1
PB
714 /* -EPERM: urb is being killed;
715 * -ENODEV: device got disconnected */
716 if (err != -EPERM && err != -ENODEV)
61faddf6 717 BT_ERR("%s urb %p failed to resubmit (%d)",
89e7533d 718 hdev->name, urb, -err);
9bfa35fe
MH
719 usb_unanchor_urb(urb);
720 }
721}
722
42b16b3f 723static inline void __fill_isoc_descriptor(struct urb *urb, int len, int mtu)
9bfa35fe
MH
724{
725 int i, offset = 0;
726
727 BT_DBG("len %d mtu %d", len, mtu);
728
729 for (i = 0; i < BTUSB_MAX_ISOC_FRAMES && len >= mtu;
730 i++, offset += mtu, len -= mtu) {
731 urb->iso_frame_desc[i].offset = offset;
732 urb->iso_frame_desc[i].length = mtu;
733 }
734
735 if (len && i < BTUSB_MAX_ISOC_FRAMES) {
736 urb->iso_frame_desc[i].offset = offset;
737 urb->iso_frame_desc[i].length = len;
738 i++;
739 }
740
741 urb->number_of_packets = i;
742}
743
2eda66f4 744static int btusb_submit_isoc_urb(struct hci_dev *hdev, gfp_t mem_flags)
9bfa35fe 745{
155961e8 746 struct btusb_data *data = hci_get_drvdata(hdev);
9bfa35fe
MH
747 struct urb *urb;
748 unsigned char *buf;
749 unsigned int pipe;
750 int err, size;
751
752 BT_DBG("%s", hdev->name);
753
754 if (!data->isoc_rx_ep)
755 return -ENODEV;
756
2eda66f4 757 urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, mem_flags);
9bfa35fe
MH
758 if (!urb)
759 return -ENOMEM;
760
761 size = le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize) *
762 BTUSB_MAX_ISOC_FRAMES;
763
2eda66f4 764 buf = kmalloc(size, mem_flags);
9bfa35fe
MH
765 if (!buf) {
766 usb_free_urb(urb);
767 return -ENOMEM;
768 }
769
770 pipe = usb_rcvisocpipe(data->udev, data->isoc_rx_ep->bEndpointAddress);
771
fa0fb93f 772 usb_fill_int_urb(urb, data->udev, pipe, buf, size, btusb_isoc_complete,
89e7533d 773 hdev, data->isoc_rx_ep->bInterval);
9bfa35fe 774
89e7533d 775 urb->transfer_flags = URB_FREE_BUFFER | URB_ISO_ASAP;
9bfa35fe
MH
776
777 __fill_isoc_descriptor(urb, size,
89e7533d 778 le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize));
9bfa35fe
MH
779
780 usb_anchor_urb(urb, &data->isoc_anchor);
781
2eda66f4 782 err = usb_submit_urb(urb, mem_flags);
9bfa35fe 783 if (err < 0) {
d4b8d1c9
PB
784 if (err != -EPERM && err != -ENODEV)
785 BT_ERR("%s urb %p submission failed (%d)",
89e7533d 786 hdev->name, urb, -err);
9bfa35fe 787 usb_unanchor_urb(urb);
9bfa35fe
MH
788 }
789
790 usb_free_urb(urb);
791
792 return err;
793}
794
5e23b923 795static void btusb_tx_complete(struct urb *urb)
7bee549e
ON
796{
797 struct sk_buff *skb = urb->context;
89e7533d 798 struct hci_dev *hdev = (struct hci_dev *)skb->dev;
155961e8 799 struct btusb_data *data = hci_get_drvdata(hdev);
7bee549e 800
89e7533d
MH
801 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
802 urb->actual_length);
7bee549e
ON
803
804 if (!test_bit(HCI_RUNNING, &hdev->flags))
805 goto done;
806
807 if (!urb->status)
808 hdev->stat.byte_tx += urb->transfer_buffer_length;
809 else
810 hdev->stat.err_tx++;
811
812done:
813 spin_lock(&data->txlock);
814 data->tx_in_flight--;
815 spin_unlock(&data->txlock);
816
817 kfree(urb->setup_packet);
818
819 kfree_skb(skb);
820}
821
822static void btusb_isoc_tx_complete(struct urb *urb)
5e23b923
MH
823{
824 struct sk_buff *skb = urb->context;
89e7533d 825 struct hci_dev *hdev = (struct hci_dev *)skb->dev;
5e23b923 826
89e7533d
MH
827 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
828 urb->actual_length);
5e23b923
MH
829
830 if (!test_bit(HCI_RUNNING, &hdev->flags))
831 goto done;
832
833 if (!urb->status)
834 hdev->stat.byte_tx += urb->transfer_buffer_length;
835 else
836 hdev->stat.err_tx++;
837
838done:
839 kfree(urb->setup_packet);
840
841 kfree_skb(skb);
842}
843
844static int btusb_open(struct hci_dev *hdev)
845{
155961e8 846 struct btusb_data *data = hci_get_drvdata(hdev);
5e23b923
MH
847 int err;
848
849 BT_DBG("%s", hdev->name);
850
7bee549e
ON
851 err = usb_autopm_get_interface(data->intf);
852 if (err < 0)
853 return err;
854
855 data->intf->needs_remote_wakeup = 1;
856
5e23b923 857 if (test_and_set_bit(HCI_RUNNING, &hdev->flags))
7bee549e 858 goto done;
5e23b923
MH
859
860 if (test_and_set_bit(BTUSB_INTR_RUNNING, &data->flags))
7bee549e 861 goto done;
5e23b923 862
2eda66f4 863 err = btusb_submit_intr_urb(hdev, GFP_KERNEL);
43c2e57f
MH
864 if (err < 0)
865 goto failed;
866
867 err = btusb_submit_bulk_urb(hdev, GFP_KERNEL);
5e23b923 868 if (err < 0) {
43c2e57f
MH
869 usb_kill_anchored_urbs(&data->intr_anchor);
870 goto failed;
5e23b923
MH
871 }
872
43c2e57f
MH
873 set_bit(BTUSB_BULK_RUNNING, &data->flags);
874 btusb_submit_bulk_urb(hdev, GFP_KERNEL);
875
7bee549e
ON
876done:
877 usb_autopm_put_interface(data->intf);
43c2e57f
MH
878 return 0;
879
880failed:
881 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
882 clear_bit(HCI_RUNNING, &hdev->flags);
7bee549e 883 usb_autopm_put_interface(data->intf);
5e23b923
MH
884 return err;
885}
886
7bee549e
ON
887static void btusb_stop_traffic(struct btusb_data *data)
888{
889 usb_kill_anchored_urbs(&data->intr_anchor);
890 usb_kill_anchored_urbs(&data->bulk_anchor);
891 usb_kill_anchored_urbs(&data->isoc_anchor);
892}
893
5e23b923
MH
894static int btusb_close(struct hci_dev *hdev)
895{
155961e8 896 struct btusb_data *data = hci_get_drvdata(hdev);
7bee549e 897 int err;
5e23b923
MH
898
899 BT_DBG("%s", hdev->name);
900
901 if (!test_and_clear_bit(HCI_RUNNING, &hdev->flags))
902 return 0;
903
e8c3c3d2 904 cancel_work_sync(&data->work);
404291ac 905 cancel_work_sync(&data->waker);
e8c3c3d2 906
9bfa35fe 907 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
5e23b923 908 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
5e23b923 909 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
7bee549e
ON
910
911 btusb_stop_traffic(data);
803b5836
MH
912 btusb_free_frags(data);
913
7bee549e
ON
914 err = usb_autopm_get_interface(data->intf);
915 if (err < 0)
7b8e2c1d 916 goto failed;
7bee549e
ON
917
918 data->intf->needs_remote_wakeup = 0;
919 usb_autopm_put_interface(data->intf);
5e23b923 920
7b8e2c1d
ON
921failed:
922 usb_scuttle_anchored_urbs(&data->deferred);
5e23b923
MH
923 return 0;
924}
925
926static int btusb_flush(struct hci_dev *hdev)
927{
155961e8 928 struct btusb_data *data = hci_get_drvdata(hdev);
5e23b923
MH
929
930 BT_DBG("%s", hdev->name);
931
932 usb_kill_anchored_urbs(&data->tx_anchor);
803b5836 933 btusb_free_frags(data);
5e23b923
MH
934
935 return 0;
936}
937
047b2ec8 938static struct urb *alloc_ctrl_urb(struct hci_dev *hdev, struct sk_buff *skb)
5e23b923 939{
155961e8 940 struct btusb_data *data = hci_get_drvdata(hdev);
5e23b923
MH
941 struct usb_ctrlrequest *dr;
942 struct urb *urb;
943 unsigned int pipe;
5e23b923 944
047b2ec8
MH
945 urb = usb_alloc_urb(0, GFP_KERNEL);
946 if (!urb)
947 return ERR_PTR(-ENOMEM);
5e23b923 948
047b2ec8
MH
949 dr = kmalloc(sizeof(*dr), GFP_KERNEL);
950 if (!dr) {
951 usb_free_urb(urb);
952 return ERR_PTR(-ENOMEM);
953 }
5e23b923 954
047b2ec8
MH
955 dr->bRequestType = data->cmdreq_type;
956 dr->bRequest = 0;
957 dr->wIndex = 0;
958 dr->wValue = 0;
959 dr->wLength = __cpu_to_le16(skb->len);
7bd8f09f 960
047b2ec8 961 pipe = usb_sndctrlpipe(data->udev, 0x00);
5e23b923 962
89e7533d 963 usb_fill_control_urb(urb, data->udev, pipe, (void *)dr,
047b2ec8 964 skb->data, skb->len, btusb_tx_complete, skb);
5e23b923 965
89e7533d 966 skb->dev = (void *)hdev;
5e23b923 967
047b2ec8
MH
968 return urb;
969}
5e23b923 970
047b2ec8
MH
971static struct urb *alloc_bulk_urb(struct hci_dev *hdev, struct sk_buff *skb)
972{
973 struct btusb_data *data = hci_get_drvdata(hdev);
974 struct urb *urb;
975 unsigned int pipe;
5e23b923 976
047b2ec8
MH
977 if (!data->bulk_tx_ep)
978 return ERR_PTR(-ENODEV);
9bfa35fe 979
047b2ec8
MH
980 urb = usb_alloc_urb(0, GFP_KERNEL);
981 if (!urb)
982 return ERR_PTR(-ENOMEM);
5e23b923 983
047b2ec8 984 pipe = usb_sndbulkpipe(data->udev, data->bulk_tx_ep->bEndpointAddress);
5e23b923 985
047b2ec8
MH
986 usb_fill_bulk_urb(urb, data->udev, pipe,
987 skb->data, skb->len, btusb_tx_complete, skb);
5e23b923 988
89e7533d 989 skb->dev = (void *)hdev;
5e23b923 990
047b2ec8
MH
991 return urb;
992}
9bfa35fe 993
047b2ec8
MH
994static struct urb *alloc_isoc_urb(struct hci_dev *hdev, struct sk_buff *skb)
995{
996 struct btusb_data *data = hci_get_drvdata(hdev);
997 struct urb *urb;
998 unsigned int pipe;
9bfa35fe 999
047b2ec8
MH
1000 if (!data->isoc_tx_ep)
1001 return ERR_PTR(-ENODEV);
9bfa35fe 1002
047b2ec8
MH
1003 urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, GFP_KERNEL);
1004 if (!urb)
1005 return ERR_PTR(-ENOMEM);
9bfa35fe 1006
047b2ec8 1007 pipe = usb_sndisocpipe(data->udev, data->isoc_tx_ep->bEndpointAddress);
9bfa35fe 1008
047b2ec8
MH
1009 usb_fill_int_urb(urb, data->udev, pipe,
1010 skb->data, skb->len, btusb_isoc_tx_complete,
1011 skb, data->isoc_tx_ep->bInterval);
9bfa35fe 1012
047b2ec8 1013 urb->transfer_flags = URB_ISO_ASAP;
5e23b923 1014
047b2ec8
MH
1015 __fill_isoc_descriptor(urb, skb->len,
1016 le16_to_cpu(data->isoc_tx_ep->wMaxPacketSize));
5e23b923 1017
89e7533d 1018 skb->dev = (void *)hdev;
047b2ec8
MH
1019
1020 return urb;
1021}
1022
1023static int submit_tx_urb(struct hci_dev *hdev, struct urb *urb)
1024{
1025 struct btusb_data *data = hci_get_drvdata(hdev);
1026 int err;
7bee549e 1027
5e23b923
MH
1028 usb_anchor_urb(urb, &data->tx_anchor);
1029
e9753eff 1030 err = usb_submit_urb(urb, GFP_KERNEL);
5e23b923 1031 if (err < 0) {
5a9b80e2
PB
1032 if (err != -EPERM && err != -ENODEV)
1033 BT_ERR("%s urb %p submission failed (%d)",
89e7533d 1034 hdev->name, urb, -err);
5e23b923
MH
1035 kfree(urb->setup_packet);
1036 usb_unanchor_urb(urb);
7bee549e
ON
1037 } else {
1038 usb_mark_last_busy(data->udev);
5e23b923
MH
1039 }
1040
54a8a79c 1041 usb_free_urb(urb);
5e23b923
MH
1042 return err;
1043}
1044
047b2ec8
MH
1045static int submit_or_queue_tx_urb(struct hci_dev *hdev, struct urb *urb)
1046{
1047 struct btusb_data *data = hci_get_drvdata(hdev);
1048 unsigned long flags;
1049 bool suspending;
1050
1051 spin_lock_irqsave(&data->txlock, flags);
1052 suspending = test_bit(BTUSB_SUSPENDING, &data->flags);
1053 if (!suspending)
1054 data->tx_in_flight++;
1055 spin_unlock_irqrestore(&data->txlock, flags);
1056
1057 if (!suspending)
1058 return submit_tx_urb(hdev, urb);
1059
1060 usb_anchor_urb(urb, &data->deferred);
1061 schedule_work(&data->waker);
1062
1063 usb_free_urb(urb);
1064 return 0;
1065}
1066
1067static int btusb_send_frame(struct hci_dev *hdev, struct sk_buff *skb)
1068{
1069 struct urb *urb;
1070
1071 BT_DBG("%s", hdev->name);
1072
1073 if (!test_bit(HCI_RUNNING, &hdev->flags))
1074 return -EBUSY;
1075
1076 switch (bt_cb(skb)->pkt_type) {
1077 case HCI_COMMAND_PKT:
1078 urb = alloc_ctrl_urb(hdev, skb);
1079 if (IS_ERR(urb))
1080 return PTR_ERR(urb);
1081
1082 hdev->stat.cmd_tx++;
1083 return submit_or_queue_tx_urb(hdev, urb);
1084
1085 case HCI_ACLDATA_PKT:
1086 urb = alloc_bulk_urb(hdev, skb);
1087 if (IS_ERR(urb))
1088 return PTR_ERR(urb);
1089
1090 hdev->stat.acl_tx++;
1091 return submit_or_queue_tx_urb(hdev, urb);
1092
1093 case HCI_SCODATA_PKT:
1094 if (hci_conn_num(hdev, SCO_LINK) < 1)
1095 return -ENODEV;
1096
1097 urb = alloc_isoc_urb(hdev, skb);
1098 if (IS_ERR(urb))
1099 return PTR_ERR(urb);
1100
1101 hdev->stat.sco_tx++;
1102 return submit_tx_urb(hdev, urb);
1103 }
1104
1105 return -EILSEQ;
1106}
1107
5e23b923
MH
1108static void btusb_notify(struct hci_dev *hdev, unsigned int evt)
1109{
155961e8 1110 struct btusb_data *data = hci_get_drvdata(hdev);
5e23b923
MH
1111
1112 BT_DBG("%s evt %d", hdev->name, evt);
1113
014f7bc7
MH
1114 if (hci_conn_num(hdev, SCO_LINK) != data->sco_num) {
1115 data->sco_num = hci_conn_num(hdev, SCO_LINK);
43c2e57f 1116 schedule_work(&data->work);
a780efa8 1117 }
5e23b923
MH
1118}
1119
42b16b3f 1120static inline int __set_isoc_interface(struct hci_dev *hdev, int altsetting)
9bfa35fe 1121{
155961e8 1122 struct btusb_data *data = hci_get_drvdata(hdev);
9bfa35fe
MH
1123 struct usb_interface *intf = data->isoc;
1124 struct usb_endpoint_descriptor *ep_desc;
1125 int i, err;
1126
1127 if (!data->isoc)
1128 return -ENODEV;
1129
1130 err = usb_set_interface(data->udev, 1, altsetting);
1131 if (err < 0) {
1132 BT_ERR("%s setting interface failed (%d)", hdev->name, -err);
1133 return err;
1134 }
1135
1136 data->isoc_altsetting = altsetting;
1137
1138 data->isoc_tx_ep = NULL;
1139 data->isoc_rx_ep = NULL;
1140
1141 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
1142 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
1143
1144 if (!data->isoc_tx_ep && usb_endpoint_is_isoc_out(ep_desc)) {
1145 data->isoc_tx_ep = ep_desc;
1146 continue;
1147 }
1148
1149 if (!data->isoc_rx_ep && usb_endpoint_is_isoc_in(ep_desc)) {
1150 data->isoc_rx_ep = ep_desc;
1151 continue;
1152 }
1153 }
1154
1155 if (!data->isoc_tx_ep || !data->isoc_rx_ep) {
1156 BT_ERR("%s invalid SCO descriptors", hdev->name);
1157 return -ENODEV;
1158 }
1159
1160 return 0;
1161}
1162
5e23b923
MH
1163static void btusb_work(struct work_struct *work)
1164{
1165 struct btusb_data *data = container_of(work, struct btusb_data, work);
1166 struct hci_dev *hdev = data->hdev;
f4001d28 1167 int new_alts;
7bee549e 1168 int err;
5e23b923 1169
014f7bc7 1170 if (data->sco_num > 0) {
08b8b6c4 1171 if (!test_bit(BTUSB_DID_ISO_RESUME, &data->flags)) {
8efdd0cd 1172 err = usb_autopm_get_interface(data->isoc ? data->isoc : data->intf);
7bee549e
ON
1173 if (err < 0) {
1174 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1175 usb_kill_anchored_urbs(&data->isoc_anchor);
1176 return;
1177 }
1178
08b8b6c4 1179 set_bit(BTUSB_DID_ISO_RESUME, &data->flags);
7bee549e 1180 }
f4001d28
MA
1181
1182 if (hdev->voice_setting & 0x0020) {
1183 static const int alts[3] = { 2, 4, 5 };
89e7533d 1184
014f7bc7 1185 new_alts = alts[data->sco_num - 1];
f4001d28 1186 } else {
014f7bc7 1187 new_alts = data->sco_num;
f4001d28
MA
1188 }
1189
1190 if (data->isoc_altsetting != new_alts) {
9bfa35fe
MH
1191 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1192 usb_kill_anchored_urbs(&data->isoc_anchor);
1193
f4001d28 1194 if (__set_isoc_interface(hdev, new_alts) < 0)
9bfa35fe
MH
1195 return;
1196 }
1197
1198 if (!test_and_set_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
2eda66f4 1199 if (btusb_submit_isoc_urb(hdev, GFP_KERNEL) < 0)
9bfa35fe
MH
1200 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1201 else
2eda66f4 1202 btusb_submit_isoc_urb(hdev, GFP_KERNEL);
9bfa35fe
MH
1203 }
1204 } else {
1205 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1206 usb_kill_anchored_urbs(&data->isoc_anchor);
1207
1208 __set_isoc_interface(hdev, 0);
08b8b6c4 1209 if (test_and_clear_bit(BTUSB_DID_ISO_RESUME, &data->flags))
8efdd0cd 1210 usb_autopm_put_interface(data->isoc ? data->isoc : data->intf);
5e23b923
MH
1211 }
1212}
1213
7bee549e
ON
1214static void btusb_waker(struct work_struct *work)
1215{
1216 struct btusb_data *data = container_of(work, struct btusb_data, waker);
1217 int err;
1218
1219 err = usb_autopm_get_interface(data->intf);
1220 if (err < 0)
1221 return;
1222
1223 usb_autopm_put_interface(data->intf);
1224}
1225
9f8f962c
MH
1226static int btusb_setup_bcm92035(struct hci_dev *hdev)
1227{
1228 struct sk_buff *skb;
1229 u8 val = 0x00;
1230
1231 BT_DBG("%s", hdev->name);
1232
1233 skb = __hci_cmd_sync(hdev, 0xfc3b, 1, &val, HCI_INIT_TIMEOUT);
1234 if (IS_ERR(skb))
1235 BT_ERR("BCM92035 command failed (%ld)", -PTR_ERR(skb));
1236 else
1237 kfree_skb(skb);
1238
1239 return 0;
1240}
1241
81cac64b
MH
1242static int btusb_setup_csr(struct hci_dev *hdev)
1243{
1244 struct hci_rp_read_local_version *rp;
1245 struct sk_buff *skb;
1246 int ret;
1247
1248 BT_DBG("%s", hdev->name);
1249
1250 skb = __hci_cmd_sync(hdev, HCI_OP_READ_LOCAL_VERSION, 0, NULL,
1251 HCI_INIT_TIMEOUT);
1252 if (IS_ERR(skb)) {
1253 BT_ERR("Reading local version failed (%ld)", -PTR_ERR(skb));
1254 return -PTR_ERR(skb);
1255 }
1256
89e7533d 1257 rp = (struct hci_rp_read_local_version *)skb->data;
81cac64b
MH
1258
1259 if (!rp->status) {
1260 if (le16_to_cpu(rp->manufacturer) != 10) {
1261 /* Clear the reset quirk since this is not an actual
1262 * early Bluetooth 1.1 device from CSR.
1263 */
1264 clear_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
1265
1266 /* These fake CSR controllers have all a broken
1267 * stored link key handling and so just disable it.
1268 */
1269 set_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY,
1270 &hdev->quirks);
1271 }
1272 }
1273
1274 ret = -bt_to_errno(rp->status);
1275
1276 kfree_skb(skb);
1277
1278 return ret;
1279}
1280
dffd30ee
THJA
1281struct intel_version {
1282 u8 status;
1283 u8 hw_platform;
1284 u8 hw_variant;
1285 u8 hw_revision;
1286 u8 fw_variant;
1287 u8 fw_revision;
1288 u8 fw_build_num;
1289 u8 fw_build_ww;
1290 u8 fw_build_yy;
1291 u8 fw_patch_num;
1292} __packed;
1293
1294static const struct firmware *btusb_setup_intel_get_fw(struct hci_dev *hdev,
89e7533d 1295 struct intel_version *ver)
dffd30ee
THJA
1296{
1297 const struct firmware *fw;
1298 char fwname[64];
1299 int ret;
1300
1301 snprintf(fwname, sizeof(fwname),
1302 "intel/ibt-hw-%x.%x.%x-fw-%x.%x.%x.%x.%x.bseq",
1303 ver->hw_platform, ver->hw_variant, ver->hw_revision,
1304 ver->fw_variant, ver->fw_revision, ver->fw_build_num,
1305 ver->fw_build_ww, ver->fw_build_yy);
1306
1307 ret = request_firmware(&fw, fwname, &hdev->dev);
1308 if (ret < 0) {
1309 if (ret == -EINVAL) {
1310 BT_ERR("%s Intel firmware file request failed (%d)",
1311 hdev->name, ret);
1312 return NULL;
1313 }
1314
1315 BT_ERR("%s failed to open Intel firmware file: %s(%d)",
1316 hdev->name, fwname, ret);
1317
1318 /* If the correct firmware patch file is not found, use the
1319 * default firmware patch file instead
1320 */
1321 snprintf(fwname, sizeof(fwname), "intel/ibt-hw-%x.%x.bseq",
1322 ver->hw_platform, ver->hw_variant);
1323 if (request_firmware(&fw, fwname, &hdev->dev) < 0) {
1324 BT_ERR("%s failed to open default Intel fw file: %s",
1325 hdev->name, fwname);
1326 return NULL;
1327 }
1328 }
1329
1330 BT_INFO("%s: Intel Bluetooth firmware file: %s", hdev->name, fwname);
1331
1332 return fw;
1333}
1334
1335static int btusb_setup_intel_patching(struct hci_dev *hdev,
1336 const struct firmware *fw,
1337 const u8 **fw_ptr, int *disable_patch)
1338{
1339 struct sk_buff *skb;
1340 struct hci_command_hdr *cmd;
1341 const u8 *cmd_param;
1342 struct hci_event_hdr *evt = NULL;
1343 const u8 *evt_param = NULL;
1344 int remain = fw->size - (*fw_ptr - fw->data);
1345
1346 /* The first byte indicates the types of the patch command or event.
1347 * 0x01 means HCI command and 0x02 is HCI event. If the first bytes
1348 * in the current firmware buffer doesn't start with 0x01 or
1349 * the size of remain buffer is smaller than HCI command header,
1350 * the firmware file is corrupted and it should stop the patching
1351 * process.
1352 */
1353 if (remain > HCI_COMMAND_HDR_SIZE && *fw_ptr[0] != 0x01) {
1354 BT_ERR("%s Intel fw corrupted: invalid cmd read", hdev->name);
1355 return -EINVAL;
1356 }
1357 (*fw_ptr)++;
1358 remain--;
1359
1360 cmd = (struct hci_command_hdr *)(*fw_ptr);
1361 *fw_ptr += sizeof(*cmd);
1362 remain -= sizeof(*cmd);
1363
1364 /* Ensure that the remain firmware data is long enough than the length
1365 * of command parameter. If not, the firmware file is corrupted.
1366 */
1367 if (remain < cmd->plen) {
1368 BT_ERR("%s Intel fw corrupted: invalid cmd len", hdev->name);
1369 return -EFAULT;
1370 }
1371
1372 /* If there is a command that loads a patch in the firmware
1373 * file, then enable the patch upon success, otherwise just
1374 * disable the manufacturer mode, for example patch activation
1375 * is not required when the default firmware patch file is used
1376 * because there are no patch data to load.
1377 */
1378 if (*disable_patch && le16_to_cpu(cmd->opcode) == 0xfc8e)
1379 *disable_patch = 0;
1380
1381 cmd_param = *fw_ptr;
1382 *fw_ptr += cmd->plen;
1383 remain -= cmd->plen;
1384
1385 /* This reads the expected events when the above command is sent to the
1386 * device. Some vendor commands expects more than one events, for
1387 * example command status event followed by vendor specific event.
1388 * For this case, it only keeps the last expected event. so the command
1389 * can be sent with __hci_cmd_sync_ev() which returns the sk_buff of
1390 * last expected event.
1391 */
1392 while (remain > HCI_EVENT_HDR_SIZE && *fw_ptr[0] == 0x02) {
1393 (*fw_ptr)++;
1394 remain--;
1395
1396 evt = (struct hci_event_hdr *)(*fw_ptr);
1397 *fw_ptr += sizeof(*evt);
1398 remain -= sizeof(*evt);
1399
1400 if (remain < evt->plen) {
1401 BT_ERR("%s Intel fw corrupted: invalid evt len",
1402 hdev->name);
1403 return -EFAULT;
1404 }
1405
1406 evt_param = *fw_ptr;
1407 *fw_ptr += evt->plen;
1408 remain -= evt->plen;
1409 }
1410
1411 /* Every HCI commands in the firmware file has its correspond event.
1412 * If event is not found or remain is smaller than zero, the firmware
1413 * file is corrupted.
1414 */
1415 if (!evt || !evt_param || remain < 0) {
1416 BT_ERR("%s Intel fw corrupted: invalid evt read", hdev->name);
1417 return -EFAULT;
1418 }
1419
1420 skb = __hci_cmd_sync_ev(hdev, le16_to_cpu(cmd->opcode), cmd->plen,
1421 cmd_param, evt->evt, HCI_INIT_TIMEOUT);
1422 if (IS_ERR(skb)) {
1423 BT_ERR("%s sending Intel patch command (0x%4.4x) failed (%ld)",
1424 hdev->name, cmd->opcode, PTR_ERR(skb));
d9c78e97 1425 return PTR_ERR(skb);
dffd30ee
THJA
1426 }
1427
1428 /* It ensures that the returned event matches the event data read from
1429 * the firmware file. At fist, it checks the length and then
1430 * the contents of the event.
1431 */
1432 if (skb->len != evt->plen) {
1433 BT_ERR("%s mismatch event length (opcode 0x%4.4x)", hdev->name,
1434 le16_to_cpu(cmd->opcode));
1435 kfree_skb(skb);
1436 return -EFAULT;
1437 }
1438
1439 if (memcmp(skb->data, evt_param, evt->plen)) {
1440 BT_ERR("%s mismatch event parameter (opcode 0x%4.4x)",
1441 hdev->name, le16_to_cpu(cmd->opcode));
1442 kfree_skb(skb);
1443 return -EFAULT;
1444 }
1445 kfree_skb(skb);
1446
1447 return 0;
1448}
1449
40cb0984
MH
1450#define BDADDR_INTEL (&(bdaddr_t) {{0x00, 0x8b, 0x9e, 0x19, 0x03, 0x00}})
1451
1452static int btusb_check_bdaddr_intel(struct hci_dev *hdev)
1453{
1454 struct sk_buff *skb;
1455 struct hci_rp_read_bd_addr *rp;
1456
1457 skb = __hci_cmd_sync(hdev, HCI_OP_READ_BD_ADDR, 0, NULL,
1458 HCI_INIT_TIMEOUT);
1459 if (IS_ERR(skb)) {
1460 BT_ERR("%s reading Intel device address failed (%ld)",
1461 hdev->name, PTR_ERR(skb));
1462 return PTR_ERR(skb);
1463 }
1464
1465 if (skb->len != sizeof(*rp)) {
1466 BT_ERR("%s Intel device address length mismatch", hdev->name);
1467 kfree_skb(skb);
1468 return -EIO;
1469 }
1470
89e7533d 1471 rp = (struct hci_rp_read_bd_addr *)skb->data;
40cb0984
MH
1472 if (rp->status) {
1473 BT_ERR("%s Intel device address result failed (%02x)",
1474 hdev->name, rp->status);
1475 kfree_skb(skb);
1476 return -bt_to_errno(rp->status);
1477 }
1478
1479 /* For some Intel based controllers, the default Bluetooth device
1480 * address 00:03:19:9E:8B:00 can be found. These controllers are
1481 * fully operational, but have the danger of duplicate addresses
1482 * and that in turn can cause problems with Bluetooth operation.
1483 */
4739b5b1 1484 if (!bacmp(&rp->bdaddr, BDADDR_INTEL)) {
40cb0984
MH
1485 BT_ERR("%s found Intel default device address (%pMR)",
1486 hdev->name, &rp->bdaddr);
4739b5b1
MH
1487 set_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks);
1488 }
40cb0984
MH
1489
1490 kfree_skb(skb);
1491
1492 return 0;
1493}
1494
dffd30ee
THJA
1495static int btusb_setup_intel(struct hci_dev *hdev)
1496{
1497 struct sk_buff *skb;
1498 const struct firmware *fw;
1499 const u8 *fw_ptr;
1500 int disable_patch;
1501 struct intel_version *ver;
1502
1503 const u8 mfg_enable[] = { 0x01, 0x00 };
1504 const u8 mfg_disable[] = { 0x00, 0x00 };
1505 const u8 mfg_reset_deactivate[] = { 0x00, 0x01 };
1506 const u8 mfg_reset_activate[] = { 0x00, 0x02 };
1507
1508 BT_DBG("%s", hdev->name);
1509
1510 /* The controller has a bug with the first HCI command sent to it
1511 * returning number of completed commands as zero. This would stall the
1512 * command processing in the Bluetooth core.
1513 *
1514 * As a workaround, send HCI Reset command first which will reset the
1515 * number of completed commands and allow normal command processing
1516 * from now on.
1517 */
1518 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
1519 if (IS_ERR(skb)) {
1520 BT_ERR("%s sending initial HCI reset command failed (%ld)",
1521 hdev->name, PTR_ERR(skb));
d9c78e97 1522 return PTR_ERR(skb);
dffd30ee
THJA
1523 }
1524 kfree_skb(skb);
1525
1526 /* Read Intel specific controller version first to allow selection of
1527 * which firmware file to load.
1528 *
1529 * The returned information are hardware variant and revision plus
1530 * firmware variant, revision and build number.
1531 */
1532 skb = __hci_cmd_sync(hdev, 0xfc05, 0, NULL, HCI_INIT_TIMEOUT);
1533 if (IS_ERR(skb)) {
1534 BT_ERR("%s reading Intel fw version command failed (%ld)",
1535 hdev->name, PTR_ERR(skb));
d9c78e97 1536 return PTR_ERR(skb);
dffd30ee
THJA
1537 }
1538
1539 if (skb->len != sizeof(*ver)) {
1540 BT_ERR("%s Intel version event length mismatch", hdev->name);
1541 kfree_skb(skb);
1542 return -EIO;
1543 }
1544
1545 ver = (struct intel_version *)skb->data;
1546 if (ver->status) {
1547 BT_ERR("%s Intel fw version event failed (%02x)", hdev->name,
1548 ver->status);
1549 kfree_skb(skb);
1550 return -bt_to_errno(ver->status);
1551 }
1552
1553 BT_INFO("%s: read Intel version: %02x%02x%02x%02x%02x%02x%02x%02x%02x",
1554 hdev->name, ver->hw_platform, ver->hw_variant,
1555 ver->hw_revision, ver->fw_variant, ver->fw_revision,
1556 ver->fw_build_num, ver->fw_build_ww, ver->fw_build_yy,
1557 ver->fw_patch_num);
1558
1559 /* fw_patch_num indicates the version of patch the device currently
1560 * have. If there is no patch data in the device, it is always 0x00.
1561 * So, if it is other than 0x00, no need to patch the deivce again.
1562 */
1563 if (ver->fw_patch_num) {
1564 BT_INFO("%s: Intel device is already patched. patch num: %02x",
1565 hdev->name, ver->fw_patch_num);
1566 kfree_skb(skb);
40cb0984 1567 btusb_check_bdaddr_intel(hdev);
dffd30ee
THJA
1568 return 0;
1569 }
1570
1571 /* Opens the firmware patch file based on the firmware version read
1572 * from the controller. If it fails to open the matching firmware
1573 * patch file, it tries to open the default firmware patch file.
1574 * If no patch file is found, allow the device to operate without
1575 * a patch.
1576 */
1577 fw = btusb_setup_intel_get_fw(hdev, ver);
1578 if (!fw) {
1579 kfree_skb(skb);
40cb0984 1580 btusb_check_bdaddr_intel(hdev);
dffd30ee
THJA
1581 return 0;
1582 }
1583 fw_ptr = fw->data;
1584
1585 /* This Intel specific command enables the manufacturer mode of the
1586 * controller.
1587 *
1588 * Only while this mode is enabled, the driver can download the
1589 * firmware patch data and configuration parameters.
1590 */
1591 skb = __hci_cmd_sync(hdev, 0xfc11, 2, mfg_enable, HCI_INIT_TIMEOUT);
1592 if (IS_ERR(skb)) {
1593 BT_ERR("%s entering Intel manufacturer mode failed (%ld)",
1594 hdev->name, PTR_ERR(skb));
1595 release_firmware(fw);
d9c78e97 1596 return PTR_ERR(skb);
dffd30ee
THJA
1597 }
1598
1599 if (skb->data[0]) {
1600 u8 evt_status = skb->data[0];
89e7533d 1601
dffd30ee
THJA
1602 BT_ERR("%s enable Intel manufacturer mode event failed (%02x)",
1603 hdev->name, evt_status);
1604 kfree_skb(skb);
1605 release_firmware(fw);
1606 return -bt_to_errno(evt_status);
1607 }
1608 kfree_skb(skb);
1609
1610 disable_patch = 1;
1611
1612 /* The firmware data file consists of list of Intel specific HCI
1613 * commands and its expected events. The first byte indicates the
1614 * type of the message, either HCI command or HCI event.
1615 *
1616 * It reads the command and its expected event from the firmware file,
1617 * and send to the controller. Once __hci_cmd_sync_ev() returns,
1618 * the returned event is compared with the event read from the firmware
1619 * file and it will continue until all the messages are downloaded to
1620 * the controller.
1621 *
1622 * Once the firmware patching is completed successfully,
1623 * the manufacturer mode is disabled with reset and activating the
1624 * downloaded patch.
1625 *
1626 * If the firmware patching fails, the manufacturer mode is
1627 * disabled with reset and deactivating the patch.
1628 *
1629 * If the default patch file is used, no reset is done when disabling
1630 * the manufacturer.
1631 */
1632 while (fw->size > fw_ptr - fw->data) {
1633 int ret;
1634
1635 ret = btusb_setup_intel_patching(hdev, fw, &fw_ptr,
1636 &disable_patch);
1637 if (ret < 0)
1638 goto exit_mfg_deactivate;
1639 }
1640
1641 release_firmware(fw);
1642
1643 if (disable_patch)
1644 goto exit_mfg_disable;
1645
1646 /* Patching completed successfully and disable the manufacturer mode
1647 * with reset and activate the downloaded firmware patches.
1648 */
1649 skb = __hci_cmd_sync(hdev, 0xfc11, sizeof(mfg_reset_activate),
1650 mfg_reset_activate, HCI_INIT_TIMEOUT);
1651 if (IS_ERR(skb)) {
1652 BT_ERR("%s exiting Intel manufacturer mode failed (%ld)",
1653 hdev->name, PTR_ERR(skb));
d9c78e97 1654 return PTR_ERR(skb);
dffd30ee
THJA
1655 }
1656 kfree_skb(skb);
1657
1658 BT_INFO("%s: Intel Bluetooth firmware patch completed and activated",
1659 hdev->name);
1660
40cb0984 1661 btusb_check_bdaddr_intel(hdev);
dffd30ee
THJA
1662 return 0;
1663
1664exit_mfg_disable:
1665 /* Disable the manufacturer mode without reset */
1666 skb = __hci_cmd_sync(hdev, 0xfc11, sizeof(mfg_disable), mfg_disable,
1667 HCI_INIT_TIMEOUT);
1668 if (IS_ERR(skb)) {
1669 BT_ERR("%s exiting Intel manufacturer mode failed (%ld)",
1670 hdev->name, PTR_ERR(skb));
d9c78e97 1671 return PTR_ERR(skb);
dffd30ee
THJA
1672 }
1673 kfree_skb(skb);
1674
1675 BT_INFO("%s: Intel Bluetooth firmware patch completed", hdev->name);
40cb0984
MH
1676
1677 btusb_check_bdaddr_intel(hdev);
dffd30ee
THJA
1678 return 0;
1679
1680exit_mfg_deactivate:
1681 release_firmware(fw);
1682
1683 /* Patching failed. Disable the manufacturer mode with reset and
1684 * deactivate the downloaded firmware patches.
1685 */
1686 skb = __hci_cmd_sync(hdev, 0xfc11, sizeof(mfg_reset_deactivate),
1687 mfg_reset_deactivate, HCI_INIT_TIMEOUT);
1688 if (IS_ERR(skb)) {
1689 BT_ERR("%s exiting Intel manufacturer mode failed (%ld)",
1690 hdev->name, PTR_ERR(skb));
d9c78e97 1691 return PTR_ERR(skb);
dffd30ee
THJA
1692 }
1693 kfree_skb(skb);
1694
1695 BT_INFO("%s: Intel Bluetooth firmware patch completed and deactivated",
1696 hdev->name);
1697
40cb0984 1698 btusb_check_bdaddr_intel(hdev);
dffd30ee
THJA
1699 return 0;
1700}
1701
cb8d6597
MH
1702static int btusb_set_bdaddr_intel(struct hci_dev *hdev, const bdaddr_t *bdaddr)
1703{
1704 struct sk_buff *skb;
1705 long ret;
1706
1707 skb = __hci_cmd_sync(hdev, 0xfc31, 6, bdaddr, HCI_INIT_TIMEOUT);
1708 if (IS_ERR(skb)) {
1709 ret = PTR_ERR(skb);
1710 BT_ERR("%s: changing Intel device address failed (%ld)",
89e7533d 1711 hdev->name, ret);
cb8d6597
MH
1712 return ret;
1713 }
1714 kfree_skb(skb);
1715
1716 return 0;
1717}
1718
ae8df494
AK
1719static int btusb_set_bdaddr_marvell(struct hci_dev *hdev,
1720 const bdaddr_t *bdaddr)
1721{
1722 struct sk_buff *skb;
1723 u8 buf[8];
1724 long ret;
1725
1726 buf[0] = 0xfe;
1727 buf[1] = sizeof(bdaddr_t);
1728 memcpy(buf + 2, bdaddr, sizeof(bdaddr_t));
1729
1730 skb = __hci_cmd_sync(hdev, 0xfc22, sizeof(buf), buf, HCI_INIT_TIMEOUT);
1731 if (IS_ERR(skb)) {
1732 ret = PTR_ERR(skb);
1733 BT_ERR("%s: changing Marvell device address failed (%ld)",
1734 hdev->name, ret);
1735 return ret;
1736 }
1737 kfree_skb(skb);
1738
1739 return 0;
1740}
1741
c8abb73f
MH
1742#define BDADDR_BCM20702A0 (&(bdaddr_t) {{0x00, 0xa0, 0x02, 0x70, 0x20, 0x00}})
1743
10d4c673
PG
1744static int btusb_setup_bcm_patchram(struct hci_dev *hdev)
1745{
1746 struct btusb_data *data = hci_get_drvdata(hdev);
1747 struct usb_device *udev = data->udev;
1748 char fw_name[64];
1749 const struct firmware *fw;
1750 const u8 *fw_ptr;
1751 size_t fw_size;
1752 const struct hci_command_hdr *cmd;
1753 const u8 *cmd_param;
1754 u16 opcode;
1755 struct sk_buff *skb;
1756 struct hci_rp_read_local_version *ver;
c8abb73f 1757 struct hci_rp_read_bd_addr *bda;
10d4c673
PG
1758 long ret;
1759
1760 snprintf(fw_name, sizeof(fw_name), "brcm/%s-%04x-%04x.hcd",
1761 udev->product ? udev->product : "BCM",
1762 le16_to_cpu(udev->descriptor.idVendor),
1763 le16_to_cpu(udev->descriptor.idProduct));
1764
1765 ret = request_firmware(&fw, fw_name, &hdev->dev);
1766 if (ret < 0) {
c8abb73f 1767 BT_INFO("%s: BCM: patch %s not found", hdev->name, fw_name);
10d4c673
PG
1768 return 0;
1769 }
1770
1771 /* Reset */
1772 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
1773 if (IS_ERR(skb)) {
1774 ret = PTR_ERR(skb);
1775 BT_ERR("%s: HCI_OP_RESET failed (%ld)", hdev->name, ret);
1776 goto done;
1777 }
1778 kfree_skb(skb);
1779
1780 /* Read Local Version Info */
1781 skb = __hci_cmd_sync(hdev, HCI_OP_READ_LOCAL_VERSION, 0, NULL,
1782 HCI_INIT_TIMEOUT);
1783 if (IS_ERR(skb)) {
1784 ret = PTR_ERR(skb);
1785 BT_ERR("%s: HCI_OP_READ_LOCAL_VERSION failed (%ld)",
89e7533d 1786 hdev->name, ret);
10d4c673
PG
1787 goto done;
1788 }
1789
1790 if (skb->len != sizeof(*ver)) {
1791 BT_ERR("%s: HCI_OP_READ_LOCAL_VERSION event length mismatch",
89e7533d 1792 hdev->name);
10d4c673
PG
1793 kfree_skb(skb);
1794 ret = -EIO;
1795 goto done;
1796 }
1797
89e7533d 1798 ver = (struct hci_rp_read_local_version *)skb->data;
10d4c673
PG
1799 BT_INFO("%s: BCM: patching hci_ver=%02x hci_rev=%04x lmp_ver=%02x "
1800 "lmp_subver=%04x", hdev->name, ver->hci_ver, ver->hci_rev,
1801 ver->lmp_ver, ver->lmp_subver);
1802 kfree_skb(skb);
1803
1804 /* Start Download */
1805 skb = __hci_cmd_sync(hdev, 0xfc2e, 0, NULL, HCI_INIT_TIMEOUT);
1806 if (IS_ERR(skb)) {
1807 ret = PTR_ERR(skb);
1808 BT_ERR("%s: BCM: Download Minidrv command failed (%ld)",
89e7533d 1809 hdev->name, ret);
10d4c673
PG
1810 goto reset_fw;
1811 }
1812 kfree_skb(skb);
1813
1814 /* 50 msec delay after Download Minidrv completes */
1815 msleep(50);
1816
1817 fw_ptr = fw->data;
1818 fw_size = fw->size;
1819
1820 while (fw_size >= sizeof(*cmd)) {
89e7533d 1821 cmd = (struct hci_command_hdr *)fw_ptr;
10d4c673
PG
1822 fw_ptr += sizeof(*cmd);
1823 fw_size -= sizeof(*cmd);
1824
1825 if (fw_size < cmd->plen) {
1826 BT_ERR("%s: BCM: patch %s is corrupted",
89e7533d 1827 hdev->name, fw_name);
10d4c673
PG
1828 ret = -EINVAL;
1829 goto reset_fw;
1830 }
1831
1832 cmd_param = fw_ptr;
1833 fw_ptr += cmd->plen;
1834 fw_size -= cmd->plen;
1835
1836 opcode = le16_to_cpu(cmd->opcode);
1837
1838 skb = __hci_cmd_sync(hdev, opcode, cmd->plen, cmd_param,
1839 HCI_INIT_TIMEOUT);
1840 if (IS_ERR(skb)) {
1841 ret = PTR_ERR(skb);
1842 BT_ERR("%s: BCM: patch command %04x failed (%ld)",
89e7533d 1843 hdev->name, opcode, ret);
10d4c673
PG
1844 goto reset_fw;
1845 }
1846 kfree_skb(skb);
1847 }
1848
1849 /* 250 msec delay after Launch Ram completes */
1850 msleep(250);
1851
1852reset_fw:
1853 /* Reset */
1854 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
1855 if (IS_ERR(skb)) {
1856 ret = PTR_ERR(skb);
1857 BT_ERR("%s: HCI_OP_RESET failed (%ld)", hdev->name, ret);
1858 goto done;
1859 }
1860 kfree_skb(skb);
1861
1862 /* Read Local Version Info */
1863 skb = __hci_cmd_sync(hdev, HCI_OP_READ_LOCAL_VERSION, 0, NULL,
1864 HCI_INIT_TIMEOUT);
1865 if (IS_ERR(skb)) {
1866 ret = PTR_ERR(skb);
1867 BT_ERR("%s: HCI_OP_READ_LOCAL_VERSION failed (%ld)",
89e7533d 1868 hdev->name, ret);
10d4c673
PG
1869 goto done;
1870 }
1871
1872 if (skb->len != sizeof(*ver)) {
1873 BT_ERR("%s: HCI_OP_READ_LOCAL_VERSION event length mismatch",
89e7533d 1874 hdev->name);
10d4c673
PG
1875 kfree_skb(skb);
1876 ret = -EIO;
1877 goto done;
1878 }
1879
89e7533d 1880 ver = (struct hci_rp_read_local_version *)skb->data;
10d4c673
PG
1881 BT_INFO("%s: BCM: firmware hci_ver=%02x hci_rev=%04x lmp_ver=%02x "
1882 "lmp_subver=%04x", hdev->name, ver->hci_ver, ver->hci_rev,
1883 ver->lmp_ver, ver->lmp_subver);
1884 kfree_skb(skb);
1885
c8abb73f
MH
1886 /* Read BD Address */
1887 skb = __hci_cmd_sync(hdev, HCI_OP_READ_BD_ADDR, 0, NULL,
1888 HCI_INIT_TIMEOUT);
1889 if (IS_ERR(skb)) {
1890 ret = PTR_ERR(skb);
1891 BT_ERR("%s: HCI_OP_READ_BD_ADDR failed (%ld)",
89e7533d 1892 hdev->name, ret);
c8abb73f
MH
1893 goto done;
1894 }
1895
1896 if (skb->len != sizeof(*bda)) {
1897 BT_ERR("%s: HCI_OP_READ_BD_ADDR event length mismatch",
89e7533d 1898 hdev->name);
c8abb73f
MH
1899 kfree_skb(skb);
1900 ret = -EIO;
1901 goto done;
1902 }
1903
89e7533d 1904 bda = (struct hci_rp_read_bd_addr *)skb->data;
c8abb73f
MH
1905 if (bda->status) {
1906 BT_ERR("%s: HCI_OP_READ_BD_ADDR error status (%02x)",
1907 hdev->name, bda->status);
1908 kfree_skb(skb);
1909 ret = -bt_to_errno(bda->status);
1910 goto done;
1911 }
1912
1913 /* The address 00:20:70:02:A0:00 indicates a BCM20702A0 controller
1914 * with no configured address.
1915 */
849e5086 1916 if (!bacmp(&bda->bdaddr, BDADDR_BCM20702A0)) {
c8abb73f
MH
1917 BT_INFO("%s: BCM: using default device address (%pMR)",
1918 hdev->name, &bda->bdaddr);
849e5086
MH
1919 set_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks);
1920 }
c8abb73f
MH
1921
1922 kfree_skb(skb);
1923
10d4c673
PG
1924done:
1925 release_firmware(fw);
1926
1927 return ret;
1928}
1929
abbaf50e
MH
1930static int btusb_set_bdaddr_bcm(struct hci_dev *hdev, const bdaddr_t *bdaddr)
1931{
1932 struct sk_buff *skb;
1933 long ret;
1934
1935 skb = __hci_cmd_sync(hdev, 0xfc01, 6, bdaddr, HCI_INIT_TIMEOUT);
1936 if (IS_ERR(skb)) {
1937 ret = PTR_ERR(skb);
1938 BT_ERR("%s: BCM: Change address command failed (%ld)",
89e7533d 1939 hdev->name, ret);
abbaf50e
MH
1940 return ret;
1941 }
1942 kfree_skb(skb);
1943
1944 return 0;
1945}
1946
5859223e
TK
1947static int btusb_set_bdaddr_ath3012(struct hci_dev *hdev,
1948 const bdaddr_t *bdaddr)
1949{
1950 struct sk_buff *skb;
1951 u8 buf[10];
1952 long ret;
1953
1954 buf[0] = 0x01;
1955 buf[1] = 0x01;
1956 buf[2] = 0x00;
1957 buf[3] = sizeof(bdaddr_t);
1958 memcpy(buf + 4, bdaddr, sizeof(bdaddr_t));
1959
1960 skb = __hci_cmd_sync(hdev, 0xfc0b, sizeof(buf), buf, HCI_INIT_TIMEOUT);
1961 if (IS_ERR(skb)) {
1962 ret = PTR_ERR(skb);
1963 BT_ERR("%s: Change address command failed (%ld)",
1964 hdev->name, ret);
1965 return ret;
1966 }
1967 kfree_skb(skb);
1968
1969 return 0;
1970}
1971
5e23b923 1972static int btusb_probe(struct usb_interface *intf,
89e7533d 1973 const struct usb_device_id *id)
5e23b923
MH
1974{
1975 struct usb_endpoint_descriptor *ep_desc;
1976 struct btusb_data *data;
1977 struct hci_dev *hdev;
1978 int i, err;
1979
1980 BT_DBG("intf %p id %p", intf, id);
1981
cfeb4145 1982 /* interface numbers are hardcoded in the spec */
5e23b923
MH
1983 if (intf->cur_altsetting->desc.bInterfaceNumber != 0)
1984 return -ENODEV;
1985
1986 if (!id->driver_info) {
1987 const struct usb_device_id *match;
89e7533d 1988
5e23b923
MH
1989 match = usb_match_id(intf, blacklist_table);
1990 if (match)
1991 id = match;
1992 }
1993
cfeb4145
MH
1994 if (id->driver_info == BTUSB_IGNORE)
1995 return -ENODEV;
1996
2d25f8b4
SL
1997 if (id->driver_info & BTUSB_ATH3012) {
1998 struct usb_device *udev = interface_to_usbdev(intf);
1999
2000 /* Old firmware would otherwise let ath3k driver load
2001 * patch and sysconfig files */
2002 if (le16_to_cpu(udev->descriptor.bcdDevice) <= 0x0001)
2003 return -ENODEV;
2004 }
2005
98921dbd 2006 data = devm_kzalloc(&intf->dev, sizeof(*data), GFP_KERNEL);
5e23b923
MH
2007 if (!data)
2008 return -ENOMEM;
2009
2010 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
2011 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
2012
2013 if (!data->intr_ep && usb_endpoint_is_int_in(ep_desc)) {
2014 data->intr_ep = ep_desc;
2015 continue;
2016 }
2017
2018 if (!data->bulk_tx_ep && usb_endpoint_is_bulk_out(ep_desc)) {
2019 data->bulk_tx_ep = ep_desc;
2020 continue;
2021 }
2022
2023 if (!data->bulk_rx_ep && usb_endpoint_is_bulk_in(ep_desc)) {
2024 data->bulk_rx_ep = ep_desc;
2025 continue;
2026 }
2027 }
2028
98921dbd 2029 if (!data->intr_ep || !data->bulk_tx_ep || !data->bulk_rx_ep)
5e23b923 2030 return -ENODEV;
5e23b923 2031
7a9d4020
MH
2032 data->cmdreq_type = USB_TYPE_CLASS;
2033
5e23b923 2034 data->udev = interface_to_usbdev(intf);
5fbcd260 2035 data->intf = intf;
5e23b923 2036
5e23b923 2037 INIT_WORK(&data->work, btusb_work);
7bee549e 2038 INIT_WORK(&data->waker, btusb_waker);
803b5836
MH
2039 init_usb_anchor(&data->deferred);
2040 init_usb_anchor(&data->tx_anchor);
7bee549e 2041 spin_lock_init(&data->txlock);
5e23b923 2042
5e23b923
MH
2043 init_usb_anchor(&data->intr_anchor);
2044 init_usb_anchor(&data->bulk_anchor);
9bfa35fe 2045 init_usb_anchor(&data->isoc_anchor);
803b5836 2046 spin_lock_init(&data->rxlock);
5e23b923 2047
2cbd3f5c
MH
2048 data->recv_bulk = btusb_recv_bulk;
2049
5e23b923 2050 hdev = hci_alloc_dev();
98921dbd 2051 if (!hdev)
5e23b923 2052 return -ENOMEM;
5e23b923 2053
c13854ce 2054 hdev->bus = HCI_USB;
155961e8 2055 hci_set_drvdata(hdev, data);
5e23b923
MH
2056
2057 data->hdev = hdev;
2058
2059 SET_HCIDEV_DEV(hdev, &intf->dev);
2060
9f8f962c
MH
2061 hdev->open = btusb_open;
2062 hdev->close = btusb_close;
2063 hdev->flush = btusb_flush;
2064 hdev->send = btusb_send_frame;
2065 hdev->notify = btusb_notify;
2066
2067 if (id->driver_info & BTUSB_BCM92035)
2068 hdev->setup = btusb_setup_bcm92035;
5e23b923 2069
abbaf50e 2070 if (id->driver_info & BTUSB_BCM_PATCHRAM) {
10d4c673 2071 hdev->setup = btusb_setup_bcm_patchram;
abbaf50e 2072 hdev->set_bdaddr = btusb_set_bdaddr_bcm;
27c3fbe0 2073 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
abbaf50e 2074 }
10d4c673 2075
cb8d6597 2076 if (id->driver_info & BTUSB_INTEL) {
dffd30ee 2077 hdev->setup = btusb_setup_intel;
cb8d6597
MH
2078 hdev->set_bdaddr = btusb_set_bdaddr_intel;
2079 }
dffd30ee 2080
ae8df494
AK
2081 if (id->driver_info & BTUSB_MARVELL)
2082 hdev->set_bdaddr = btusb_set_bdaddr_marvell;
2083
d57dbe77
MH
2084 if (id->driver_info & BTUSB_AVM)
2085 set_bit(HCI_QUIRK_BROKEN_LOCAL_COMMANDS, &hdev->quirks);
2086
40df783d
MH
2087 if (id->driver_info & BTUSB_INTEL_BOOT)
2088 set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
2089
5859223e
TK
2090 if (id->driver_info & BTUSB_ATH3012)
2091 hdev->set_bdaddr = btusb_set_bdaddr_ath3012;
2092
7a9d4020 2093 /* Interface numbers are hardcoded in the specification */
9bfa35fe
MH
2094 data->isoc = usb_ifnum_to_if(data->udev, 1);
2095
7a9d4020 2096 if (!reset)
a6c511c6 2097 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
cfeb4145
MH
2098
2099 if (force_scofix || id->driver_info & BTUSB_WRONG_SCO_MTU) {
2100 if (!disable_scofix)
2101 set_bit(HCI_QUIRK_FIXUP_BUFFER_SIZE, &hdev->quirks);
2102 }
2103
9bfa35fe
MH
2104 if (id->driver_info & BTUSB_BROKEN_ISOC)
2105 data->isoc = NULL;
2106
7a9d4020
MH
2107 if (id->driver_info & BTUSB_DIGIANSWER) {
2108 data->cmdreq_type = USB_TYPE_VENDOR;
a6c511c6 2109 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
7a9d4020
MH
2110 }
2111
2112 if (id->driver_info & BTUSB_CSR) {
2113 struct usb_device *udev = data->udev;
81cac64b 2114 u16 bcdDevice = le16_to_cpu(udev->descriptor.bcdDevice);
7a9d4020
MH
2115
2116 /* Old firmware would otherwise execute USB reset */
81cac64b 2117 if (bcdDevice < 0x117)
a6c511c6 2118 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
81cac64b
MH
2119
2120 /* Fake CSR devices with broken commands */
2121 if (bcdDevice <= 0x100)
2122 hdev->setup = btusb_setup_csr;
7a9d4020
MH
2123 }
2124
cfeb4145 2125 if (id->driver_info & BTUSB_SNIFFER) {
9bfa35fe 2126 struct usb_device *udev = data->udev;
cfeb4145 2127
7a9d4020 2128 /* New sniffer firmware has crippled HCI interface */
cfeb4145
MH
2129 if (le16_to_cpu(udev->descriptor.bcdDevice) > 0x997)
2130 set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
2131 }
2132
3a5ef20c
MH
2133 if (id->driver_info & BTUSB_INTEL_BOOT) {
2134 /* A bug in the bootloader causes that interrupt interface is
2135 * only enabled after receiving SetInterface(0, AltSetting=0).
2136 */
2137 err = usb_set_interface(data->udev, 0, 0);
2138 if (err < 0) {
2139 BT_ERR("failed to set interface 0, alt 0 %d", err);
2140 hci_free_dev(hdev);
2141 return err;
2142 }
2143 }
2144
9bfa35fe
MH
2145 if (data->isoc) {
2146 err = usb_driver_claim_interface(&btusb_driver,
89e7533d 2147 data->isoc, data);
9bfa35fe
MH
2148 if (err < 0) {
2149 hci_free_dev(hdev);
9bfa35fe
MH
2150 return err;
2151 }
2152 }
2153
5e23b923
MH
2154 err = hci_register_dev(hdev);
2155 if (err < 0) {
2156 hci_free_dev(hdev);
5e23b923
MH
2157 return err;
2158 }
2159
2160 usb_set_intfdata(intf, data);
2161
2162 return 0;
2163}
2164
2165static void btusb_disconnect(struct usb_interface *intf)
2166{
2167 struct btusb_data *data = usb_get_intfdata(intf);
2168 struct hci_dev *hdev;
2169
2170 BT_DBG("intf %p", intf);
2171
2172 if (!data)
2173 return;
2174
2175 hdev = data->hdev;
5fbcd260
MH
2176 usb_set_intfdata(data->intf, NULL);
2177
2178 if (data->isoc)
2179 usb_set_intfdata(data->isoc, NULL);
5e23b923
MH
2180
2181 hci_unregister_dev(hdev);
2182
5fbcd260
MH
2183 if (intf == data->isoc)
2184 usb_driver_release_interface(&btusb_driver, data->intf);
2185 else if (data->isoc)
2186 usb_driver_release_interface(&btusb_driver, data->isoc);
2187
803b5836 2188 btusb_free_frags(data);
5e23b923
MH
2189 hci_free_dev(hdev);
2190}
2191
7bee549e 2192#ifdef CONFIG_PM
6a88adf2
MH
2193static int btusb_suspend(struct usb_interface *intf, pm_message_t message)
2194{
2195 struct btusb_data *data = usb_get_intfdata(intf);
2196
2197 BT_DBG("intf %p", intf);
2198
2199 if (data->suspend_count++)
2200 return 0;
2201
7bee549e 2202 spin_lock_irq(&data->txlock);
5b1b0b81 2203 if (!(PMSG_IS_AUTO(message) && data->tx_in_flight)) {
7bee549e
ON
2204 set_bit(BTUSB_SUSPENDING, &data->flags);
2205 spin_unlock_irq(&data->txlock);
2206 } else {
2207 spin_unlock_irq(&data->txlock);
2208 data->suspend_count--;
2209 return -EBUSY;
2210 }
2211
6a88adf2
MH
2212 cancel_work_sync(&data->work);
2213
7bee549e 2214 btusb_stop_traffic(data);
6a88adf2
MH
2215 usb_kill_anchored_urbs(&data->tx_anchor);
2216
6a88adf2
MH
2217 return 0;
2218}
2219
7bee549e
ON
2220static void play_deferred(struct btusb_data *data)
2221{
2222 struct urb *urb;
2223 int err;
2224
2225 while ((urb = usb_get_from_anchor(&data->deferred))) {
2226 err = usb_submit_urb(urb, GFP_ATOMIC);
2227 if (err < 0)
2228 break;
2229
2230 data->tx_in_flight++;
2231 }
2232 usb_scuttle_anchored_urbs(&data->deferred);
2233}
2234
6a88adf2
MH
2235static int btusb_resume(struct usb_interface *intf)
2236{
2237 struct btusb_data *data = usb_get_intfdata(intf);
2238 struct hci_dev *hdev = data->hdev;
7bee549e 2239 int err = 0;
6a88adf2
MH
2240
2241 BT_DBG("intf %p", intf);
2242
2243 if (--data->suspend_count)
2244 return 0;
2245
2246 if (!test_bit(HCI_RUNNING, &hdev->flags))
7bee549e 2247 goto done;
6a88adf2
MH
2248
2249 if (test_bit(BTUSB_INTR_RUNNING, &data->flags)) {
2250 err = btusb_submit_intr_urb(hdev, GFP_NOIO);
2251 if (err < 0) {
2252 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
7bee549e 2253 goto failed;
6a88adf2
MH
2254 }
2255 }
2256
2257 if (test_bit(BTUSB_BULK_RUNNING, &data->flags)) {
43c2e57f
MH
2258 err = btusb_submit_bulk_urb(hdev, GFP_NOIO);
2259 if (err < 0) {
6a88adf2 2260 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
7bee549e
ON
2261 goto failed;
2262 }
2263
2264 btusb_submit_bulk_urb(hdev, GFP_NOIO);
6a88adf2
MH
2265 }
2266
2267 if (test_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
2268 if (btusb_submit_isoc_urb(hdev, GFP_NOIO) < 0)
2269 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
2270 else
2271 btusb_submit_isoc_urb(hdev, GFP_NOIO);
2272 }
2273
7bee549e
ON
2274 spin_lock_irq(&data->txlock);
2275 play_deferred(data);
2276 clear_bit(BTUSB_SUSPENDING, &data->flags);
2277 spin_unlock_irq(&data->txlock);
2278 schedule_work(&data->work);
2279
6a88adf2 2280 return 0;
7bee549e
ON
2281
2282failed:
2283 usb_scuttle_anchored_urbs(&data->deferred);
2284done:
2285 spin_lock_irq(&data->txlock);
2286 clear_bit(BTUSB_SUSPENDING, &data->flags);
2287 spin_unlock_irq(&data->txlock);
2288
2289 return err;
6a88adf2 2290}
7bee549e 2291#endif
6a88adf2 2292
5e23b923
MH
2293static struct usb_driver btusb_driver = {
2294 .name = "btusb",
2295 .probe = btusb_probe,
2296 .disconnect = btusb_disconnect,
7bee549e 2297#ifdef CONFIG_PM
6a88adf2
MH
2298 .suspend = btusb_suspend,
2299 .resume = btusb_resume,
7bee549e 2300#endif
5e23b923 2301 .id_table = btusb_table,
7bee549e 2302 .supports_autosuspend = 1,
e1f12eb6 2303 .disable_hub_initiated_lpm = 1,
5e23b923
MH
2304};
2305
93f1508c 2306module_usb_driver(btusb_driver);
5e23b923 2307
cfeb4145
MH
2308module_param(disable_scofix, bool, 0644);
2309MODULE_PARM_DESC(disable_scofix, "Disable fixup of wrong SCO buffer size");
2310
2311module_param(force_scofix, bool, 0644);
2312MODULE_PARM_DESC(force_scofix, "Force fixup of wrong SCO buffers size");
2313
2314module_param(reset, bool, 0644);
2315MODULE_PARM_DESC(reset, "Send HCI reset command on initialization");
2316
5e23b923
MH
2317MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
2318MODULE_DESCRIPTION("Generic Bluetooth USB driver ver " VERSION);
2319MODULE_VERSION(VERSION);
2320MODULE_LICENSE("GPL");