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