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