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