bna: remove oper_state_cbfn from struct bna_rxf
[linux/fpc-iii.git] / drivers / bluetooth / btusb.c
blob94c6c048130fe61be8173248bdb769eb7d50f84b
1 /*
3 * Generic Bluetooth USB driver
5 * Copyright (C) 2005-2008 Marcel Holtmann <marcel@holtmann.org>
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.
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.
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
24 #include <linux/module.h>
25 #include <linux/usb.h>
26 #include <linux/firmware.h>
27 #include <asm/unaligned.h>
29 #include <net/bluetooth/bluetooth.h>
30 #include <net/bluetooth/hci_core.h>
32 #include "btintel.h"
33 #include "btbcm.h"
34 #include "btrtl.h"
36 #define VERSION "0.8"
38 static bool disable_scofix;
39 static bool force_scofix;
41 static bool reset = true;
43 static struct usb_driver btusb_driver;
45 #define BTUSB_IGNORE 0x01
46 #define BTUSB_DIGIANSWER 0x02
47 #define BTUSB_CSR 0x04
48 #define BTUSB_SNIFFER 0x08
49 #define BTUSB_BCM92035 0x10
50 #define BTUSB_BROKEN_ISOC 0x20
51 #define BTUSB_WRONG_SCO_MTU 0x40
52 #define BTUSB_ATH3012 0x80
53 #define BTUSB_INTEL 0x100
54 #define BTUSB_INTEL_BOOT 0x200
55 #define BTUSB_BCM_PATCHRAM 0x400
56 #define BTUSB_MARVELL 0x800
57 #define BTUSB_SWAVE 0x1000
58 #define BTUSB_INTEL_NEW 0x2000
59 #define BTUSB_AMP 0x4000
60 #define BTUSB_QCA_ROME 0x8000
61 #define BTUSB_BCM_APPLE 0x10000
62 #define BTUSB_REALTEK 0x20000
64 static const struct usb_device_id btusb_table[] = {
65 /* Generic Bluetooth USB device */
66 { USB_DEVICE_INFO(0xe0, 0x01, 0x01) },
68 /* Generic Bluetooth AMP device */
69 { USB_DEVICE_INFO(0xe0, 0x01, 0x04), .driver_info = BTUSB_AMP },
71 /* Apple-specific (Broadcom) devices */
72 { USB_VENDOR_AND_INTERFACE_INFO(0x05ac, 0xff, 0x01, 0x01),
73 .driver_info = BTUSB_BCM_APPLE },
75 /* MediaTek MT76x0E */
76 { USB_DEVICE(0x0e8d, 0x763f) },
78 /* Broadcom SoftSailing reporting vendor specific */
79 { USB_DEVICE(0x0a5c, 0x21e1) },
81 /* Apple MacBookPro 7,1 */
82 { USB_DEVICE(0x05ac, 0x8213) },
84 /* Apple iMac11,1 */
85 { USB_DEVICE(0x05ac, 0x8215) },
87 /* Apple MacBookPro6,2 */
88 { USB_DEVICE(0x05ac, 0x8218) },
90 /* Apple MacBookAir3,1, MacBookAir3,2 */
91 { USB_DEVICE(0x05ac, 0x821b) },
93 /* Apple MacBookAir4,1 */
94 { USB_DEVICE(0x05ac, 0x821f) },
96 /* Apple MacBookPro8,2 */
97 { USB_DEVICE(0x05ac, 0x821a) },
99 /* Apple MacMini5,1 */
100 { USB_DEVICE(0x05ac, 0x8281) },
102 /* AVM BlueFRITZ! USB v2.0 */
103 { USB_DEVICE(0x057c, 0x3800), .driver_info = BTUSB_SWAVE },
105 /* Bluetooth Ultraport Module from IBM */
106 { USB_DEVICE(0x04bf, 0x030a) },
108 /* ALPS Modules with non-standard id */
109 { USB_DEVICE(0x044e, 0x3001) },
110 { USB_DEVICE(0x044e, 0x3002) },
112 /* Ericsson with non-standard id */
113 { USB_DEVICE(0x0bdb, 0x1002) },
115 /* Canyon CN-BTU1 with HID interfaces */
116 { USB_DEVICE(0x0c10, 0x0000) },
118 /* Broadcom BCM20702A0 */
119 { USB_DEVICE(0x413c, 0x8197) },
121 /* Broadcom BCM20702B0 (Dynex/Insignia) */
122 { USB_DEVICE(0x19ff, 0x0239), .driver_info = BTUSB_BCM_PATCHRAM },
124 /* Foxconn - Hon Hai */
125 { USB_VENDOR_AND_INTERFACE_INFO(0x0489, 0xff, 0x01, 0x01),
126 .driver_info = BTUSB_BCM_PATCHRAM },
128 /* Lite-On Technology - Broadcom based */
129 { USB_VENDOR_AND_INTERFACE_INFO(0x04ca, 0xff, 0x01, 0x01),
130 .driver_info = BTUSB_BCM_PATCHRAM },
132 /* Broadcom devices with vendor specific id */
133 { USB_VENDOR_AND_INTERFACE_INFO(0x0a5c, 0xff, 0x01, 0x01),
134 .driver_info = BTUSB_BCM_PATCHRAM },
136 /* ASUSTek Computer - Broadcom based */
137 { USB_VENDOR_AND_INTERFACE_INFO(0x0b05, 0xff, 0x01, 0x01),
138 .driver_info = BTUSB_BCM_PATCHRAM },
140 /* Belkin F8065bf - Broadcom based */
141 { USB_VENDOR_AND_INTERFACE_INFO(0x050d, 0xff, 0x01, 0x01),
142 .driver_info = BTUSB_BCM_PATCHRAM },
144 /* IMC Networks - Broadcom based */
145 { USB_VENDOR_AND_INTERFACE_INFO(0x13d3, 0xff, 0x01, 0x01),
146 .driver_info = BTUSB_BCM_PATCHRAM },
148 /* Intel Bluetooth USB Bootloader (RAM module) */
149 { USB_DEVICE(0x8087, 0x0a5a),
150 .driver_info = BTUSB_INTEL_BOOT | BTUSB_BROKEN_ISOC },
152 { } /* Terminating entry */
155 MODULE_DEVICE_TABLE(usb, btusb_table);
157 static const struct usb_device_id blacklist_table[] = {
158 /* CSR BlueCore devices */
159 { USB_DEVICE(0x0a12, 0x0001), .driver_info = BTUSB_CSR },
161 /* Broadcom BCM2033 without firmware */
162 { USB_DEVICE(0x0a5c, 0x2033), .driver_info = BTUSB_IGNORE },
164 /* Atheros 3011 with sflash firmware */
165 { USB_DEVICE(0x0489, 0xe027), .driver_info = BTUSB_IGNORE },
166 { USB_DEVICE(0x0489, 0xe03d), .driver_info = BTUSB_IGNORE },
167 { USB_DEVICE(0x04f2, 0xaff1), .driver_info = BTUSB_IGNORE },
168 { USB_DEVICE(0x0930, 0x0215), .driver_info = BTUSB_IGNORE },
169 { USB_DEVICE(0x0cf3, 0x3002), .driver_info = BTUSB_IGNORE },
170 { USB_DEVICE(0x0cf3, 0xe019), .driver_info = BTUSB_IGNORE },
171 { USB_DEVICE(0x13d3, 0x3304), .driver_info = BTUSB_IGNORE },
173 /* Atheros AR9285 Malbec with sflash firmware */
174 { USB_DEVICE(0x03f0, 0x311d), .driver_info = BTUSB_IGNORE },
176 /* Atheros 3012 with sflash firmware */
177 { USB_DEVICE(0x0489, 0xe04d), .driver_info = BTUSB_ATH3012 },
178 { USB_DEVICE(0x0489, 0xe04e), .driver_info = BTUSB_ATH3012 },
179 { USB_DEVICE(0x0489, 0xe056), .driver_info = BTUSB_ATH3012 },
180 { USB_DEVICE(0x0489, 0xe057), .driver_info = BTUSB_ATH3012 },
181 { USB_DEVICE(0x0489, 0xe05f), .driver_info = BTUSB_ATH3012 },
182 { USB_DEVICE(0x0489, 0xe078), .driver_info = BTUSB_ATH3012 },
183 { USB_DEVICE(0x04c5, 0x1330), .driver_info = BTUSB_ATH3012 },
184 { USB_DEVICE(0x04ca, 0x3004), .driver_info = BTUSB_ATH3012 },
185 { USB_DEVICE(0x04ca, 0x3005), .driver_info = BTUSB_ATH3012 },
186 { USB_DEVICE(0x04ca, 0x3006), .driver_info = BTUSB_ATH3012 },
187 { USB_DEVICE(0x04ca, 0x3007), .driver_info = BTUSB_ATH3012 },
188 { USB_DEVICE(0x04ca, 0x3008), .driver_info = BTUSB_ATH3012 },
189 { USB_DEVICE(0x04ca, 0x300b), .driver_info = BTUSB_ATH3012 },
190 { USB_DEVICE(0x04ca, 0x300f), .driver_info = BTUSB_ATH3012 },
191 { USB_DEVICE(0x04ca, 0x3010), .driver_info = BTUSB_ATH3012 },
192 { USB_DEVICE(0x0930, 0x0219), .driver_info = BTUSB_ATH3012 },
193 { USB_DEVICE(0x0930, 0x0220), .driver_info = BTUSB_ATH3012 },
194 { USB_DEVICE(0x0930, 0x0227), .driver_info = BTUSB_ATH3012 },
195 { USB_DEVICE(0x0b05, 0x17d0), .driver_info = BTUSB_ATH3012 },
196 { USB_DEVICE(0x0cf3, 0x0036), .driver_info = BTUSB_ATH3012 },
197 { USB_DEVICE(0x0cf3, 0x3004), .driver_info = BTUSB_ATH3012 },
198 { USB_DEVICE(0x0cf3, 0x3008), .driver_info = BTUSB_ATH3012 },
199 { USB_DEVICE(0x0cf3, 0x311d), .driver_info = BTUSB_ATH3012 },
200 { USB_DEVICE(0x0cf3, 0x311e), .driver_info = BTUSB_ATH3012 },
201 { USB_DEVICE(0x0cf3, 0x311f), .driver_info = BTUSB_ATH3012 },
202 { USB_DEVICE(0x0cf3, 0x3121), .driver_info = BTUSB_ATH3012 },
203 { USB_DEVICE(0x0cf3, 0x817a), .driver_info = BTUSB_ATH3012 },
204 { USB_DEVICE(0x0cf3, 0xe003), .driver_info = BTUSB_ATH3012 },
205 { USB_DEVICE(0x0cf3, 0xe004), .driver_info = BTUSB_ATH3012 },
206 { USB_DEVICE(0x0cf3, 0xe005), .driver_info = BTUSB_ATH3012 },
207 { USB_DEVICE(0x0cf3, 0xe006), .driver_info = BTUSB_ATH3012 },
208 { USB_DEVICE(0x13d3, 0x3362), .driver_info = BTUSB_ATH3012 },
209 { USB_DEVICE(0x13d3, 0x3375), .driver_info = BTUSB_ATH3012 },
210 { USB_DEVICE(0x13d3, 0x3393), .driver_info = BTUSB_ATH3012 },
211 { USB_DEVICE(0x13d3, 0x3402), .driver_info = BTUSB_ATH3012 },
212 { USB_DEVICE(0x13d3, 0x3408), .driver_info = BTUSB_ATH3012 },
213 { USB_DEVICE(0x13d3, 0x3423), .driver_info = BTUSB_ATH3012 },
214 { USB_DEVICE(0x13d3, 0x3432), .driver_info = BTUSB_ATH3012 },
216 /* Atheros AR5BBU12 with sflash firmware */
217 { USB_DEVICE(0x0489, 0xe02c), .driver_info = BTUSB_IGNORE },
219 /* Atheros AR5BBU12 with sflash firmware */
220 { USB_DEVICE(0x0489, 0xe036), .driver_info = BTUSB_ATH3012 },
221 { USB_DEVICE(0x0489, 0xe03c), .driver_info = BTUSB_ATH3012 },
223 /* QCA ROME chipset */
224 { USB_DEVICE(0x0cf3, 0xe007), .driver_info = BTUSB_QCA_ROME },
225 { USB_DEVICE(0x0cf3, 0xe300), .driver_info = BTUSB_QCA_ROME },
226 { USB_DEVICE(0x0cf3, 0xe360), .driver_info = BTUSB_QCA_ROME },
228 /* Broadcom BCM2035 */
229 { USB_DEVICE(0x0a5c, 0x2009), .driver_info = BTUSB_BCM92035 },
230 { USB_DEVICE(0x0a5c, 0x200a), .driver_info = BTUSB_WRONG_SCO_MTU },
231 { USB_DEVICE(0x0a5c, 0x2035), .driver_info = BTUSB_WRONG_SCO_MTU },
233 /* Broadcom BCM2045 */
234 { USB_DEVICE(0x0a5c, 0x2039), .driver_info = BTUSB_WRONG_SCO_MTU },
235 { USB_DEVICE(0x0a5c, 0x2101), .driver_info = BTUSB_WRONG_SCO_MTU },
237 /* IBM/Lenovo ThinkPad with Broadcom chip */
238 { USB_DEVICE(0x0a5c, 0x201e), .driver_info = BTUSB_WRONG_SCO_MTU },
239 { USB_DEVICE(0x0a5c, 0x2110), .driver_info = BTUSB_WRONG_SCO_MTU },
241 /* HP laptop with Broadcom chip */
242 { USB_DEVICE(0x03f0, 0x171d), .driver_info = BTUSB_WRONG_SCO_MTU },
244 /* Dell laptop with Broadcom chip */
245 { USB_DEVICE(0x413c, 0x8126), .driver_info = BTUSB_WRONG_SCO_MTU },
247 /* Dell Wireless 370 and 410 devices */
248 { USB_DEVICE(0x413c, 0x8152), .driver_info = BTUSB_WRONG_SCO_MTU },
249 { USB_DEVICE(0x413c, 0x8156), .driver_info = BTUSB_WRONG_SCO_MTU },
251 /* Belkin F8T012 and F8T013 devices */
252 { USB_DEVICE(0x050d, 0x0012), .driver_info = BTUSB_WRONG_SCO_MTU },
253 { USB_DEVICE(0x050d, 0x0013), .driver_info = BTUSB_WRONG_SCO_MTU },
255 /* Asus WL-BTD202 device */
256 { USB_DEVICE(0x0b05, 0x1715), .driver_info = BTUSB_WRONG_SCO_MTU },
258 /* Kensington Bluetooth USB adapter */
259 { USB_DEVICE(0x047d, 0x105e), .driver_info = BTUSB_WRONG_SCO_MTU },
261 /* RTX Telecom based adapters with buggy SCO support */
262 { USB_DEVICE(0x0400, 0x0807), .driver_info = BTUSB_BROKEN_ISOC },
263 { USB_DEVICE(0x0400, 0x080a), .driver_info = BTUSB_BROKEN_ISOC },
265 /* CONWISE Technology based adapters with buggy SCO support */
266 { USB_DEVICE(0x0e5e, 0x6622), .driver_info = BTUSB_BROKEN_ISOC },
268 /* Roper Class 1 Bluetooth Dongle (Silicon Wave based) */
269 { USB_DEVICE(0x1300, 0x0001), .driver_info = BTUSB_SWAVE },
271 /* Digianswer devices */
272 { USB_DEVICE(0x08fd, 0x0001), .driver_info = BTUSB_DIGIANSWER },
273 { USB_DEVICE(0x08fd, 0x0002), .driver_info = BTUSB_IGNORE },
275 /* CSR BlueCore Bluetooth Sniffer */
276 { USB_DEVICE(0x0a12, 0x0002),
277 .driver_info = BTUSB_SNIFFER | BTUSB_BROKEN_ISOC },
279 /* Frontline ComProbe Bluetooth Sniffer */
280 { USB_DEVICE(0x16d3, 0x0002),
281 .driver_info = BTUSB_SNIFFER | BTUSB_BROKEN_ISOC },
283 /* Marvell Bluetooth devices */
284 { USB_DEVICE(0x1286, 0x2044), .driver_info = BTUSB_MARVELL },
285 { USB_DEVICE(0x1286, 0x2046), .driver_info = BTUSB_MARVELL },
287 /* Intel Bluetooth devices */
288 { USB_DEVICE(0x8087, 0x07da), .driver_info = BTUSB_CSR },
289 { USB_DEVICE(0x8087, 0x07dc), .driver_info = BTUSB_INTEL },
290 { USB_DEVICE(0x8087, 0x0a2a), .driver_info = BTUSB_INTEL },
291 { USB_DEVICE(0x8087, 0x0a2b), .driver_info = BTUSB_INTEL_NEW },
293 /* Other Intel Bluetooth devices */
294 { USB_VENDOR_AND_INTERFACE_INFO(0x8087, 0xe0, 0x01, 0x01),
295 .driver_info = BTUSB_IGNORE },
297 /* Realtek Bluetooth devices */
298 { USB_VENDOR_AND_INTERFACE_INFO(0x0bda, 0xe0, 0x01, 0x01),
299 .driver_info = BTUSB_REALTEK },
301 /* Additional Realtek 8723AE Bluetooth devices */
302 { USB_DEVICE(0x0930, 0x021d), .driver_info = BTUSB_REALTEK },
303 { USB_DEVICE(0x13d3, 0x3394), .driver_info = BTUSB_REALTEK },
305 /* Additional Realtek 8723BE Bluetooth devices */
306 { USB_DEVICE(0x0489, 0xe085), .driver_info = BTUSB_REALTEK },
307 { USB_DEVICE(0x0489, 0xe08b), .driver_info = BTUSB_REALTEK },
308 { USB_DEVICE(0x13d3, 0x3410), .driver_info = BTUSB_REALTEK },
309 { USB_DEVICE(0x13d3, 0x3416), .driver_info = BTUSB_REALTEK },
310 { USB_DEVICE(0x13d3, 0x3459), .driver_info = BTUSB_REALTEK },
312 /* Additional Realtek 8821AE Bluetooth devices */
313 { USB_DEVICE(0x0b05, 0x17dc), .driver_info = BTUSB_REALTEK },
314 { USB_DEVICE(0x13d3, 0x3414), .driver_info = BTUSB_REALTEK },
315 { USB_DEVICE(0x13d3, 0x3458), .driver_info = BTUSB_REALTEK },
316 { USB_DEVICE(0x13d3, 0x3461), .driver_info = BTUSB_REALTEK },
317 { USB_DEVICE(0x13d3, 0x3462), .driver_info = BTUSB_REALTEK },
319 { } /* Terminating entry */
322 #define BTUSB_MAX_ISOC_FRAMES 10
324 #define BTUSB_INTR_RUNNING 0
325 #define BTUSB_BULK_RUNNING 1
326 #define BTUSB_ISOC_RUNNING 2
327 #define BTUSB_SUSPENDING 3
328 #define BTUSB_DID_ISO_RESUME 4
329 #define BTUSB_BOOTLOADER 5
330 #define BTUSB_DOWNLOADING 6
331 #define BTUSB_FIRMWARE_LOADED 7
332 #define BTUSB_FIRMWARE_FAILED 8
333 #define BTUSB_BOOTING 9
334 #define BTUSB_RESET_RESUME 10
336 struct btusb_data {
337 struct hci_dev *hdev;
338 struct usb_device *udev;
339 struct usb_interface *intf;
340 struct usb_interface *isoc;
342 unsigned long flags;
344 struct work_struct work;
345 struct work_struct waker;
347 struct usb_anchor deferred;
348 struct usb_anchor tx_anchor;
349 int tx_in_flight;
350 spinlock_t txlock;
352 struct usb_anchor intr_anchor;
353 struct usb_anchor bulk_anchor;
354 struct usb_anchor isoc_anchor;
355 spinlock_t rxlock;
357 struct sk_buff *evt_skb;
358 struct sk_buff *acl_skb;
359 struct sk_buff *sco_skb;
361 struct usb_endpoint_descriptor *intr_ep;
362 struct usb_endpoint_descriptor *bulk_tx_ep;
363 struct usb_endpoint_descriptor *bulk_rx_ep;
364 struct usb_endpoint_descriptor *isoc_tx_ep;
365 struct usb_endpoint_descriptor *isoc_rx_ep;
367 __u8 cmdreq_type;
368 __u8 cmdreq;
370 unsigned int sco_num;
371 int isoc_altsetting;
372 int suspend_count;
374 int (*recv_event)(struct hci_dev *hdev, struct sk_buff *skb);
375 int (*recv_bulk)(struct btusb_data *data, void *buffer, int count);
377 int (*setup_on_usb)(struct hci_dev *hdev);
380 static inline void btusb_free_frags(struct btusb_data *data)
382 unsigned long flags;
384 spin_lock_irqsave(&data->rxlock, flags);
386 kfree_skb(data->evt_skb);
387 data->evt_skb = NULL;
389 kfree_skb(data->acl_skb);
390 data->acl_skb = NULL;
392 kfree_skb(data->sco_skb);
393 data->sco_skb = NULL;
395 spin_unlock_irqrestore(&data->rxlock, flags);
398 static int btusb_recv_intr(struct btusb_data *data, void *buffer, int count)
400 struct sk_buff *skb;
401 int err = 0;
403 spin_lock(&data->rxlock);
404 skb = data->evt_skb;
406 while (count) {
407 int len;
409 if (!skb) {
410 skb = bt_skb_alloc(HCI_MAX_EVENT_SIZE, GFP_ATOMIC);
411 if (!skb) {
412 err = -ENOMEM;
413 break;
416 bt_cb(skb)->pkt_type = HCI_EVENT_PKT;
417 bt_cb(skb)->expect = HCI_EVENT_HDR_SIZE;
420 len = min_t(uint, bt_cb(skb)->expect, count);
421 memcpy(skb_put(skb, len), buffer, len);
423 count -= len;
424 buffer += len;
425 bt_cb(skb)->expect -= len;
427 if (skb->len == HCI_EVENT_HDR_SIZE) {
428 /* Complete event header */
429 bt_cb(skb)->expect = hci_event_hdr(skb)->plen;
431 if (skb_tailroom(skb) < bt_cb(skb)->expect) {
432 kfree_skb(skb);
433 skb = NULL;
435 err = -EILSEQ;
436 break;
440 if (bt_cb(skb)->expect == 0) {
441 /* Complete frame */
442 data->recv_event(data->hdev, skb);
443 skb = NULL;
447 data->evt_skb = skb;
448 spin_unlock(&data->rxlock);
450 return err;
453 static int btusb_recv_bulk(struct btusb_data *data, void *buffer, int count)
455 struct sk_buff *skb;
456 int err = 0;
458 spin_lock(&data->rxlock);
459 skb = data->acl_skb;
461 while (count) {
462 int len;
464 if (!skb) {
465 skb = bt_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC);
466 if (!skb) {
467 err = -ENOMEM;
468 break;
471 bt_cb(skb)->pkt_type = HCI_ACLDATA_PKT;
472 bt_cb(skb)->expect = HCI_ACL_HDR_SIZE;
475 len = min_t(uint, bt_cb(skb)->expect, count);
476 memcpy(skb_put(skb, len), buffer, len);
478 count -= len;
479 buffer += len;
480 bt_cb(skb)->expect -= len;
482 if (skb->len == HCI_ACL_HDR_SIZE) {
483 __le16 dlen = hci_acl_hdr(skb)->dlen;
485 /* Complete ACL header */
486 bt_cb(skb)->expect = __le16_to_cpu(dlen);
488 if (skb_tailroom(skb) < bt_cb(skb)->expect) {
489 kfree_skb(skb);
490 skb = NULL;
492 err = -EILSEQ;
493 break;
497 if (bt_cb(skb)->expect == 0) {
498 /* Complete frame */
499 hci_recv_frame(data->hdev, skb);
500 skb = NULL;
504 data->acl_skb = skb;
505 spin_unlock(&data->rxlock);
507 return err;
510 static int btusb_recv_isoc(struct btusb_data *data, void *buffer, int count)
512 struct sk_buff *skb;
513 int err = 0;
515 spin_lock(&data->rxlock);
516 skb = data->sco_skb;
518 while (count) {
519 int len;
521 if (!skb) {
522 skb = bt_skb_alloc(HCI_MAX_SCO_SIZE, GFP_ATOMIC);
523 if (!skb) {
524 err = -ENOMEM;
525 break;
528 bt_cb(skb)->pkt_type = HCI_SCODATA_PKT;
529 bt_cb(skb)->expect = HCI_SCO_HDR_SIZE;
532 len = min_t(uint, bt_cb(skb)->expect, count);
533 memcpy(skb_put(skb, len), buffer, len);
535 count -= len;
536 buffer += len;
537 bt_cb(skb)->expect -= len;
539 if (skb->len == HCI_SCO_HDR_SIZE) {
540 /* Complete SCO header */
541 bt_cb(skb)->expect = hci_sco_hdr(skb)->dlen;
543 if (skb_tailroom(skb) < bt_cb(skb)->expect) {
544 kfree_skb(skb);
545 skb = NULL;
547 err = -EILSEQ;
548 break;
552 if (bt_cb(skb)->expect == 0) {
553 /* Complete frame */
554 hci_recv_frame(data->hdev, skb);
555 skb = NULL;
559 data->sco_skb = skb;
560 spin_unlock(&data->rxlock);
562 return err;
565 static void btusb_intr_complete(struct urb *urb)
567 struct hci_dev *hdev = urb->context;
568 struct btusb_data *data = hci_get_drvdata(hdev);
569 int err;
571 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
572 urb->actual_length);
574 if (!test_bit(HCI_RUNNING, &hdev->flags))
575 return;
577 if (urb->status == 0) {
578 hdev->stat.byte_rx += urb->actual_length;
580 if (btusb_recv_intr(data, urb->transfer_buffer,
581 urb->actual_length) < 0) {
582 BT_ERR("%s corrupted event packet", hdev->name);
583 hdev->stat.err_rx++;
585 } else if (urb->status == -ENOENT) {
586 /* Avoid suspend failed when usb_kill_urb */
587 return;
590 if (!test_bit(BTUSB_INTR_RUNNING, &data->flags))
591 return;
593 usb_mark_last_busy(data->udev);
594 usb_anchor_urb(urb, &data->intr_anchor);
596 err = usb_submit_urb(urb, GFP_ATOMIC);
597 if (err < 0) {
598 /* -EPERM: urb is being killed;
599 * -ENODEV: device got disconnected */
600 if (err != -EPERM && err != -ENODEV)
601 BT_ERR("%s urb %p failed to resubmit (%d)",
602 hdev->name, urb, -err);
603 usb_unanchor_urb(urb);
607 static int btusb_submit_intr_urb(struct hci_dev *hdev, gfp_t mem_flags)
609 struct btusb_data *data = hci_get_drvdata(hdev);
610 struct urb *urb;
611 unsigned char *buf;
612 unsigned int pipe;
613 int err, size;
615 BT_DBG("%s", hdev->name);
617 if (!data->intr_ep)
618 return -ENODEV;
620 urb = usb_alloc_urb(0, mem_flags);
621 if (!urb)
622 return -ENOMEM;
624 size = le16_to_cpu(data->intr_ep->wMaxPacketSize);
626 buf = kmalloc(size, mem_flags);
627 if (!buf) {
628 usb_free_urb(urb);
629 return -ENOMEM;
632 pipe = usb_rcvintpipe(data->udev, data->intr_ep->bEndpointAddress);
634 usb_fill_int_urb(urb, data->udev, pipe, buf, size,
635 btusb_intr_complete, hdev, data->intr_ep->bInterval);
637 urb->transfer_flags |= URB_FREE_BUFFER;
639 usb_anchor_urb(urb, &data->intr_anchor);
641 err = usb_submit_urb(urb, mem_flags);
642 if (err < 0) {
643 if (err != -EPERM && err != -ENODEV)
644 BT_ERR("%s urb %p submission failed (%d)",
645 hdev->name, urb, -err);
646 usb_unanchor_urb(urb);
649 usb_free_urb(urb);
651 return err;
654 static void btusb_bulk_complete(struct urb *urb)
656 struct hci_dev *hdev = urb->context;
657 struct btusb_data *data = hci_get_drvdata(hdev);
658 int err;
660 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
661 urb->actual_length);
663 if (!test_bit(HCI_RUNNING, &hdev->flags))
664 return;
666 if (urb->status == 0) {
667 hdev->stat.byte_rx += urb->actual_length;
669 if (data->recv_bulk(data, urb->transfer_buffer,
670 urb->actual_length) < 0) {
671 BT_ERR("%s corrupted ACL packet", hdev->name);
672 hdev->stat.err_rx++;
674 } else if (urb->status == -ENOENT) {
675 /* Avoid suspend failed when usb_kill_urb */
676 return;
679 if (!test_bit(BTUSB_BULK_RUNNING, &data->flags))
680 return;
682 usb_anchor_urb(urb, &data->bulk_anchor);
683 usb_mark_last_busy(data->udev);
685 err = usb_submit_urb(urb, GFP_ATOMIC);
686 if (err < 0) {
687 /* -EPERM: urb is being killed;
688 * -ENODEV: device got disconnected */
689 if (err != -EPERM && err != -ENODEV)
690 BT_ERR("%s urb %p failed to resubmit (%d)",
691 hdev->name, urb, -err);
692 usb_unanchor_urb(urb);
696 static int btusb_submit_bulk_urb(struct hci_dev *hdev, gfp_t mem_flags)
698 struct btusb_data *data = hci_get_drvdata(hdev);
699 struct urb *urb;
700 unsigned char *buf;
701 unsigned int pipe;
702 int err, size = HCI_MAX_FRAME_SIZE;
704 BT_DBG("%s", hdev->name);
706 if (!data->bulk_rx_ep)
707 return -ENODEV;
709 urb = usb_alloc_urb(0, mem_flags);
710 if (!urb)
711 return -ENOMEM;
713 buf = kmalloc(size, mem_flags);
714 if (!buf) {
715 usb_free_urb(urb);
716 return -ENOMEM;
719 pipe = usb_rcvbulkpipe(data->udev, data->bulk_rx_ep->bEndpointAddress);
721 usb_fill_bulk_urb(urb, data->udev, pipe, buf, size,
722 btusb_bulk_complete, hdev);
724 urb->transfer_flags |= URB_FREE_BUFFER;
726 usb_mark_last_busy(data->udev);
727 usb_anchor_urb(urb, &data->bulk_anchor);
729 err = usb_submit_urb(urb, mem_flags);
730 if (err < 0) {
731 if (err != -EPERM && err != -ENODEV)
732 BT_ERR("%s urb %p submission failed (%d)",
733 hdev->name, urb, -err);
734 usb_unanchor_urb(urb);
737 usb_free_urb(urb);
739 return err;
742 static void btusb_isoc_complete(struct urb *urb)
744 struct hci_dev *hdev = urb->context;
745 struct btusb_data *data = hci_get_drvdata(hdev);
746 int i, err;
748 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
749 urb->actual_length);
751 if (!test_bit(HCI_RUNNING, &hdev->flags))
752 return;
754 if (urb->status == 0) {
755 for (i = 0; i < urb->number_of_packets; i++) {
756 unsigned int offset = urb->iso_frame_desc[i].offset;
757 unsigned int length = urb->iso_frame_desc[i].actual_length;
759 if (urb->iso_frame_desc[i].status)
760 continue;
762 hdev->stat.byte_rx += length;
764 if (btusb_recv_isoc(data, urb->transfer_buffer + offset,
765 length) < 0) {
766 BT_ERR("%s corrupted SCO packet", hdev->name);
767 hdev->stat.err_rx++;
770 } else if (urb->status == -ENOENT) {
771 /* Avoid suspend failed when usb_kill_urb */
772 return;
775 if (!test_bit(BTUSB_ISOC_RUNNING, &data->flags))
776 return;
778 usb_anchor_urb(urb, &data->isoc_anchor);
780 err = usb_submit_urb(urb, GFP_ATOMIC);
781 if (err < 0) {
782 /* -EPERM: urb is being killed;
783 * -ENODEV: device got disconnected */
784 if (err != -EPERM && err != -ENODEV)
785 BT_ERR("%s urb %p failed to resubmit (%d)",
786 hdev->name, urb, -err);
787 usb_unanchor_urb(urb);
791 static inline void __fill_isoc_descriptor(struct urb *urb, int len, int mtu)
793 int i, offset = 0;
795 BT_DBG("len %d mtu %d", len, mtu);
797 for (i = 0; i < BTUSB_MAX_ISOC_FRAMES && len >= mtu;
798 i++, offset += mtu, len -= mtu) {
799 urb->iso_frame_desc[i].offset = offset;
800 urb->iso_frame_desc[i].length = mtu;
803 if (len && i < BTUSB_MAX_ISOC_FRAMES) {
804 urb->iso_frame_desc[i].offset = offset;
805 urb->iso_frame_desc[i].length = len;
806 i++;
809 urb->number_of_packets = i;
812 static int btusb_submit_isoc_urb(struct hci_dev *hdev, gfp_t mem_flags)
814 struct btusb_data *data = hci_get_drvdata(hdev);
815 struct urb *urb;
816 unsigned char *buf;
817 unsigned int pipe;
818 int err, size;
820 BT_DBG("%s", hdev->name);
822 if (!data->isoc_rx_ep)
823 return -ENODEV;
825 urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, mem_flags);
826 if (!urb)
827 return -ENOMEM;
829 size = le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize) *
830 BTUSB_MAX_ISOC_FRAMES;
832 buf = kmalloc(size, mem_flags);
833 if (!buf) {
834 usb_free_urb(urb);
835 return -ENOMEM;
838 pipe = usb_rcvisocpipe(data->udev, data->isoc_rx_ep->bEndpointAddress);
840 usb_fill_int_urb(urb, data->udev, pipe, buf, size, btusb_isoc_complete,
841 hdev, data->isoc_rx_ep->bInterval);
843 urb->transfer_flags = URB_FREE_BUFFER | URB_ISO_ASAP;
845 __fill_isoc_descriptor(urb, size,
846 le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize));
848 usb_anchor_urb(urb, &data->isoc_anchor);
850 err = usb_submit_urb(urb, mem_flags);
851 if (err < 0) {
852 if (err != -EPERM && err != -ENODEV)
853 BT_ERR("%s urb %p submission failed (%d)",
854 hdev->name, urb, -err);
855 usb_unanchor_urb(urb);
858 usb_free_urb(urb);
860 return err;
863 static void btusb_tx_complete(struct urb *urb)
865 struct sk_buff *skb = urb->context;
866 struct hci_dev *hdev = (struct hci_dev *)skb->dev;
867 struct btusb_data *data = hci_get_drvdata(hdev);
869 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
870 urb->actual_length);
872 if (!test_bit(HCI_RUNNING, &hdev->flags))
873 goto done;
875 if (!urb->status)
876 hdev->stat.byte_tx += urb->transfer_buffer_length;
877 else
878 hdev->stat.err_tx++;
880 done:
881 spin_lock(&data->txlock);
882 data->tx_in_flight--;
883 spin_unlock(&data->txlock);
885 kfree(urb->setup_packet);
887 kfree_skb(skb);
890 static void btusb_isoc_tx_complete(struct urb *urb)
892 struct sk_buff *skb = urb->context;
893 struct hci_dev *hdev = (struct hci_dev *)skb->dev;
895 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
896 urb->actual_length);
898 if (!test_bit(HCI_RUNNING, &hdev->flags))
899 goto done;
901 if (!urb->status)
902 hdev->stat.byte_tx += urb->transfer_buffer_length;
903 else
904 hdev->stat.err_tx++;
906 done:
907 kfree(urb->setup_packet);
909 kfree_skb(skb);
912 static int btusb_open(struct hci_dev *hdev)
914 struct btusb_data *data = hci_get_drvdata(hdev);
915 int err;
917 BT_DBG("%s", hdev->name);
919 /* Patching USB firmware files prior to starting any URBs of HCI path
920 * It is more safe to use USB bulk channel for downloading USB patch
922 if (data->setup_on_usb) {
923 err = data->setup_on_usb(hdev);
924 if (err < 0)
925 return err;
928 err = usb_autopm_get_interface(data->intf);
929 if (err < 0)
930 return err;
932 data->intf->needs_remote_wakeup = 1;
934 if (test_and_set_bit(HCI_RUNNING, &hdev->flags))
935 goto done;
937 if (test_and_set_bit(BTUSB_INTR_RUNNING, &data->flags))
938 goto done;
940 err = btusb_submit_intr_urb(hdev, GFP_KERNEL);
941 if (err < 0)
942 goto failed;
944 err = btusb_submit_bulk_urb(hdev, GFP_KERNEL);
945 if (err < 0) {
946 usb_kill_anchored_urbs(&data->intr_anchor);
947 goto failed;
950 set_bit(BTUSB_BULK_RUNNING, &data->flags);
951 btusb_submit_bulk_urb(hdev, GFP_KERNEL);
953 done:
954 usb_autopm_put_interface(data->intf);
955 return 0;
957 failed:
958 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
959 clear_bit(HCI_RUNNING, &hdev->flags);
960 usb_autopm_put_interface(data->intf);
961 return err;
964 static void btusb_stop_traffic(struct btusb_data *data)
966 usb_kill_anchored_urbs(&data->intr_anchor);
967 usb_kill_anchored_urbs(&data->bulk_anchor);
968 usb_kill_anchored_urbs(&data->isoc_anchor);
971 static int btusb_close(struct hci_dev *hdev)
973 struct btusb_data *data = hci_get_drvdata(hdev);
974 int err;
976 BT_DBG("%s", hdev->name);
978 if (!test_and_clear_bit(HCI_RUNNING, &hdev->flags))
979 return 0;
981 cancel_work_sync(&data->work);
982 cancel_work_sync(&data->waker);
984 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
985 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
986 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
988 btusb_stop_traffic(data);
989 btusb_free_frags(data);
991 err = usb_autopm_get_interface(data->intf);
992 if (err < 0)
993 goto failed;
995 data->intf->needs_remote_wakeup = 0;
996 usb_autopm_put_interface(data->intf);
998 failed:
999 usb_scuttle_anchored_urbs(&data->deferred);
1000 return 0;
1003 static int btusb_flush(struct hci_dev *hdev)
1005 struct btusb_data *data = hci_get_drvdata(hdev);
1007 BT_DBG("%s", hdev->name);
1009 usb_kill_anchored_urbs(&data->tx_anchor);
1010 btusb_free_frags(data);
1012 return 0;
1015 static struct urb *alloc_ctrl_urb(struct hci_dev *hdev, struct sk_buff *skb)
1017 struct btusb_data *data = hci_get_drvdata(hdev);
1018 struct usb_ctrlrequest *dr;
1019 struct urb *urb;
1020 unsigned int pipe;
1022 urb = usb_alloc_urb(0, GFP_KERNEL);
1023 if (!urb)
1024 return ERR_PTR(-ENOMEM);
1026 dr = kmalloc(sizeof(*dr), GFP_KERNEL);
1027 if (!dr) {
1028 usb_free_urb(urb);
1029 return ERR_PTR(-ENOMEM);
1032 dr->bRequestType = data->cmdreq_type;
1033 dr->bRequest = data->cmdreq;
1034 dr->wIndex = 0;
1035 dr->wValue = 0;
1036 dr->wLength = __cpu_to_le16(skb->len);
1038 pipe = usb_sndctrlpipe(data->udev, 0x00);
1040 usb_fill_control_urb(urb, data->udev, pipe, (void *)dr,
1041 skb->data, skb->len, btusb_tx_complete, skb);
1043 skb->dev = (void *)hdev;
1045 return urb;
1048 static struct urb *alloc_bulk_urb(struct hci_dev *hdev, struct sk_buff *skb)
1050 struct btusb_data *data = hci_get_drvdata(hdev);
1051 struct urb *urb;
1052 unsigned int pipe;
1054 if (!data->bulk_tx_ep)
1055 return ERR_PTR(-ENODEV);
1057 urb = usb_alloc_urb(0, GFP_KERNEL);
1058 if (!urb)
1059 return ERR_PTR(-ENOMEM);
1061 pipe = usb_sndbulkpipe(data->udev, data->bulk_tx_ep->bEndpointAddress);
1063 usb_fill_bulk_urb(urb, data->udev, pipe,
1064 skb->data, skb->len, btusb_tx_complete, skb);
1066 skb->dev = (void *)hdev;
1068 return urb;
1071 static struct urb *alloc_isoc_urb(struct hci_dev *hdev, struct sk_buff *skb)
1073 struct btusb_data *data = hci_get_drvdata(hdev);
1074 struct urb *urb;
1075 unsigned int pipe;
1077 if (!data->isoc_tx_ep)
1078 return ERR_PTR(-ENODEV);
1080 urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, GFP_KERNEL);
1081 if (!urb)
1082 return ERR_PTR(-ENOMEM);
1084 pipe = usb_sndisocpipe(data->udev, data->isoc_tx_ep->bEndpointAddress);
1086 usb_fill_int_urb(urb, data->udev, pipe,
1087 skb->data, skb->len, btusb_isoc_tx_complete,
1088 skb, data->isoc_tx_ep->bInterval);
1090 urb->transfer_flags = URB_ISO_ASAP;
1092 __fill_isoc_descriptor(urb, skb->len,
1093 le16_to_cpu(data->isoc_tx_ep->wMaxPacketSize));
1095 skb->dev = (void *)hdev;
1097 return urb;
1100 static int submit_tx_urb(struct hci_dev *hdev, struct urb *urb)
1102 struct btusb_data *data = hci_get_drvdata(hdev);
1103 int err;
1105 usb_anchor_urb(urb, &data->tx_anchor);
1107 err = usb_submit_urb(urb, GFP_KERNEL);
1108 if (err < 0) {
1109 if (err != -EPERM && err != -ENODEV)
1110 BT_ERR("%s urb %p submission failed (%d)",
1111 hdev->name, urb, -err);
1112 kfree(urb->setup_packet);
1113 usb_unanchor_urb(urb);
1114 } else {
1115 usb_mark_last_busy(data->udev);
1118 usb_free_urb(urb);
1119 return err;
1122 static int submit_or_queue_tx_urb(struct hci_dev *hdev, struct urb *urb)
1124 struct btusb_data *data = hci_get_drvdata(hdev);
1125 unsigned long flags;
1126 bool suspending;
1128 spin_lock_irqsave(&data->txlock, flags);
1129 suspending = test_bit(BTUSB_SUSPENDING, &data->flags);
1130 if (!suspending)
1131 data->tx_in_flight++;
1132 spin_unlock_irqrestore(&data->txlock, flags);
1134 if (!suspending)
1135 return submit_tx_urb(hdev, urb);
1137 usb_anchor_urb(urb, &data->deferred);
1138 schedule_work(&data->waker);
1140 usb_free_urb(urb);
1141 return 0;
1144 static int btusb_send_frame(struct hci_dev *hdev, struct sk_buff *skb)
1146 struct urb *urb;
1148 BT_DBG("%s", hdev->name);
1150 if (!test_bit(HCI_RUNNING, &hdev->flags))
1151 return -EBUSY;
1153 switch (bt_cb(skb)->pkt_type) {
1154 case HCI_COMMAND_PKT:
1155 urb = alloc_ctrl_urb(hdev, skb);
1156 if (IS_ERR(urb))
1157 return PTR_ERR(urb);
1159 hdev->stat.cmd_tx++;
1160 return submit_or_queue_tx_urb(hdev, urb);
1162 case HCI_ACLDATA_PKT:
1163 urb = alloc_bulk_urb(hdev, skb);
1164 if (IS_ERR(urb))
1165 return PTR_ERR(urb);
1167 hdev->stat.acl_tx++;
1168 return submit_or_queue_tx_urb(hdev, urb);
1170 case HCI_SCODATA_PKT:
1171 if (hci_conn_num(hdev, SCO_LINK) < 1)
1172 return -ENODEV;
1174 urb = alloc_isoc_urb(hdev, skb);
1175 if (IS_ERR(urb))
1176 return PTR_ERR(urb);
1178 hdev->stat.sco_tx++;
1179 return submit_tx_urb(hdev, urb);
1182 return -EILSEQ;
1185 static void btusb_notify(struct hci_dev *hdev, unsigned int evt)
1187 struct btusb_data *data = hci_get_drvdata(hdev);
1189 BT_DBG("%s evt %d", hdev->name, evt);
1191 if (hci_conn_num(hdev, SCO_LINK) != data->sco_num) {
1192 data->sco_num = hci_conn_num(hdev, SCO_LINK);
1193 schedule_work(&data->work);
1197 static inline int __set_isoc_interface(struct hci_dev *hdev, int altsetting)
1199 struct btusb_data *data = hci_get_drvdata(hdev);
1200 struct usb_interface *intf = data->isoc;
1201 struct usb_endpoint_descriptor *ep_desc;
1202 int i, err;
1204 if (!data->isoc)
1205 return -ENODEV;
1207 err = usb_set_interface(data->udev, 1, altsetting);
1208 if (err < 0) {
1209 BT_ERR("%s setting interface failed (%d)", hdev->name, -err);
1210 return err;
1213 data->isoc_altsetting = altsetting;
1215 data->isoc_tx_ep = NULL;
1216 data->isoc_rx_ep = NULL;
1218 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
1219 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
1221 if (!data->isoc_tx_ep && usb_endpoint_is_isoc_out(ep_desc)) {
1222 data->isoc_tx_ep = ep_desc;
1223 continue;
1226 if (!data->isoc_rx_ep && usb_endpoint_is_isoc_in(ep_desc)) {
1227 data->isoc_rx_ep = ep_desc;
1228 continue;
1232 if (!data->isoc_tx_ep || !data->isoc_rx_ep) {
1233 BT_ERR("%s invalid SCO descriptors", hdev->name);
1234 return -ENODEV;
1237 return 0;
1240 static void btusb_work(struct work_struct *work)
1242 struct btusb_data *data = container_of(work, struct btusb_data, work);
1243 struct hci_dev *hdev = data->hdev;
1244 int new_alts;
1245 int err;
1247 if (data->sco_num > 0) {
1248 if (!test_bit(BTUSB_DID_ISO_RESUME, &data->flags)) {
1249 err = usb_autopm_get_interface(data->isoc ? data->isoc : data->intf);
1250 if (err < 0) {
1251 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1252 usb_kill_anchored_urbs(&data->isoc_anchor);
1253 return;
1256 set_bit(BTUSB_DID_ISO_RESUME, &data->flags);
1259 if (hdev->voice_setting & 0x0020) {
1260 static const int alts[3] = { 2, 4, 5 };
1262 new_alts = alts[data->sco_num - 1];
1263 } else {
1264 new_alts = data->sco_num;
1267 if (data->isoc_altsetting != new_alts) {
1268 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1269 usb_kill_anchored_urbs(&data->isoc_anchor);
1271 if (__set_isoc_interface(hdev, new_alts) < 0)
1272 return;
1275 if (!test_and_set_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
1276 if (btusb_submit_isoc_urb(hdev, GFP_KERNEL) < 0)
1277 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1278 else
1279 btusb_submit_isoc_urb(hdev, GFP_KERNEL);
1281 } else {
1282 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1283 usb_kill_anchored_urbs(&data->isoc_anchor);
1285 __set_isoc_interface(hdev, 0);
1286 if (test_and_clear_bit(BTUSB_DID_ISO_RESUME, &data->flags))
1287 usb_autopm_put_interface(data->isoc ? data->isoc : data->intf);
1291 static void btusb_waker(struct work_struct *work)
1293 struct btusb_data *data = container_of(work, struct btusb_data, waker);
1294 int err;
1296 err = usb_autopm_get_interface(data->intf);
1297 if (err < 0)
1298 return;
1300 usb_autopm_put_interface(data->intf);
1303 static struct sk_buff *btusb_read_local_version(struct hci_dev *hdev)
1305 struct sk_buff *skb;
1307 skb = __hci_cmd_sync(hdev, HCI_OP_READ_LOCAL_VERSION, 0, NULL,
1308 HCI_INIT_TIMEOUT);
1309 if (IS_ERR(skb)) {
1310 BT_ERR("%s: HCI_OP_READ_LOCAL_VERSION failed (%ld)",
1311 hdev->name, PTR_ERR(skb));
1312 return skb;
1315 if (skb->len != sizeof(struct hci_rp_read_local_version)) {
1316 BT_ERR("%s: HCI_OP_READ_LOCAL_VERSION event length mismatch",
1317 hdev->name);
1318 kfree_skb(skb);
1319 return ERR_PTR(-EIO);
1322 return skb;
1325 static int btusb_setup_bcm92035(struct hci_dev *hdev)
1327 struct sk_buff *skb;
1328 u8 val = 0x00;
1330 BT_DBG("%s", hdev->name);
1332 skb = __hci_cmd_sync(hdev, 0xfc3b, 1, &val, HCI_INIT_TIMEOUT);
1333 if (IS_ERR(skb))
1334 BT_ERR("BCM92035 command failed (%ld)", -PTR_ERR(skb));
1335 else
1336 kfree_skb(skb);
1338 return 0;
1341 static int btusb_setup_csr(struct hci_dev *hdev)
1343 struct hci_rp_read_local_version *rp;
1344 struct sk_buff *skb;
1345 int ret;
1347 BT_DBG("%s", hdev->name);
1349 skb = btusb_read_local_version(hdev);
1350 if (IS_ERR(skb))
1351 return -PTR_ERR(skb);
1353 rp = (struct hci_rp_read_local_version *)skb->data;
1355 if (!rp->status) {
1356 if (le16_to_cpu(rp->manufacturer) != 10) {
1357 /* Clear the reset quirk since this is not an actual
1358 * early Bluetooth 1.1 device from CSR.
1360 clear_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
1362 /* These fake CSR controllers have all a broken
1363 * stored link key handling and so just disable it.
1365 set_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY,
1366 &hdev->quirks);
1370 ret = -bt_to_errno(rp->status);
1372 kfree_skb(skb);
1374 return ret;
1377 static const struct firmware *btusb_setup_intel_get_fw(struct hci_dev *hdev,
1378 struct intel_version *ver)
1380 const struct firmware *fw;
1381 char fwname[64];
1382 int ret;
1384 snprintf(fwname, sizeof(fwname),
1385 "intel/ibt-hw-%x.%x.%x-fw-%x.%x.%x.%x.%x.bseq",
1386 ver->hw_platform, ver->hw_variant, ver->hw_revision,
1387 ver->fw_variant, ver->fw_revision, ver->fw_build_num,
1388 ver->fw_build_ww, ver->fw_build_yy);
1390 ret = request_firmware(&fw, fwname, &hdev->dev);
1391 if (ret < 0) {
1392 if (ret == -EINVAL) {
1393 BT_ERR("%s Intel firmware file request failed (%d)",
1394 hdev->name, ret);
1395 return NULL;
1398 BT_ERR("%s failed to open Intel firmware file: %s(%d)",
1399 hdev->name, fwname, ret);
1401 /* If the correct firmware patch file is not found, use the
1402 * default firmware patch file instead
1404 snprintf(fwname, sizeof(fwname), "intel/ibt-hw-%x.%x.bseq",
1405 ver->hw_platform, ver->hw_variant);
1406 if (request_firmware(&fw, fwname, &hdev->dev) < 0) {
1407 BT_ERR("%s failed to open default Intel fw file: %s",
1408 hdev->name, fwname);
1409 return NULL;
1413 BT_INFO("%s: Intel Bluetooth firmware file: %s", hdev->name, fwname);
1415 return fw;
1418 static int btusb_setup_intel_patching(struct hci_dev *hdev,
1419 const struct firmware *fw,
1420 const u8 **fw_ptr, int *disable_patch)
1422 struct sk_buff *skb;
1423 struct hci_command_hdr *cmd;
1424 const u8 *cmd_param;
1425 struct hci_event_hdr *evt = NULL;
1426 const u8 *evt_param = NULL;
1427 int remain = fw->size - (*fw_ptr - fw->data);
1429 /* The first byte indicates the types of the patch command or event.
1430 * 0x01 means HCI command and 0x02 is HCI event. If the first bytes
1431 * in the current firmware buffer doesn't start with 0x01 or
1432 * the size of remain buffer is smaller than HCI command header,
1433 * the firmware file is corrupted and it should stop the patching
1434 * process.
1436 if (remain > HCI_COMMAND_HDR_SIZE && *fw_ptr[0] != 0x01) {
1437 BT_ERR("%s Intel fw corrupted: invalid cmd read", hdev->name);
1438 return -EINVAL;
1440 (*fw_ptr)++;
1441 remain--;
1443 cmd = (struct hci_command_hdr *)(*fw_ptr);
1444 *fw_ptr += sizeof(*cmd);
1445 remain -= sizeof(*cmd);
1447 /* Ensure that the remain firmware data is long enough than the length
1448 * of command parameter. If not, the firmware file is corrupted.
1450 if (remain < cmd->plen) {
1451 BT_ERR("%s Intel fw corrupted: invalid cmd len", hdev->name);
1452 return -EFAULT;
1455 /* If there is a command that loads a patch in the firmware
1456 * file, then enable the patch upon success, otherwise just
1457 * disable the manufacturer mode, for example patch activation
1458 * is not required when the default firmware patch file is used
1459 * because there are no patch data to load.
1461 if (*disable_patch && le16_to_cpu(cmd->opcode) == 0xfc8e)
1462 *disable_patch = 0;
1464 cmd_param = *fw_ptr;
1465 *fw_ptr += cmd->plen;
1466 remain -= cmd->plen;
1468 /* This reads the expected events when the above command is sent to the
1469 * device. Some vendor commands expects more than one events, for
1470 * example command status event followed by vendor specific event.
1471 * For this case, it only keeps the last expected event. so the command
1472 * can be sent with __hci_cmd_sync_ev() which returns the sk_buff of
1473 * last expected event.
1475 while (remain > HCI_EVENT_HDR_SIZE && *fw_ptr[0] == 0x02) {
1476 (*fw_ptr)++;
1477 remain--;
1479 evt = (struct hci_event_hdr *)(*fw_ptr);
1480 *fw_ptr += sizeof(*evt);
1481 remain -= sizeof(*evt);
1483 if (remain < evt->plen) {
1484 BT_ERR("%s Intel fw corrupted: invalid evt len",
1485 hdev->name);
1486 return -EFAULT;
1489 evt_param = *fw_ptr;
1490 *fw_ptr += evt->plen;
1491 remain -= evt->plen;
1494 /* Every HCI commands in the firmware file has its correspond event.
1495 * If event is not found or remain is smaller than zero, the firmware
1496 * file is corrupted.
1498 if (!evt || !evt_param || remain < 0) {
1499 BT_ERR("%s Intel fw corrupted: invalid evt read", hdev->name);
1500 return -EFAULT;
1503 skb = __hci_cmd_sync_ev(hdev, le16_to_cpu(cmd->opcode), cmd->plen,
1504 cmd_param, evt->evt, HCI_INIT_TIMEOUT);
1505 if (IS_ERR(skb)) {
1506 BT_ERR("%s sending Intel patch command (0x%4.4x) failed (%ld)",
1507 hdev->name, cmd->opcode, PTR_ERR(skb));
1508 return PTR_ERR(skb);
1511 /* It ensures that the returned event matches the event data read from
1512 * the firmware file. At fist, it checks the length and then
1513 * the contents of the event.
1515 if (skb->len != evt->plen) {
1516 BT_ERR("%s mismatch event length (opcode 0x%4.4x)", hdev->name,
1517 le16_to_cpu(cmd->opcode));
1518 kfree_skb(skb);
1519 return -EFAULT;
1522 if (memcmp(skb->data, evt_param, evt->plen)) {
1523 BT_ERR("%s mismatch event parameter (opcode 0x%4.4x)",
1524 hdev->name, le16_to_cpu(cmd->opcode));
1525 kfree_skb(skb);
1526 return -EFAULT;
1528 kfree_skb(skb);
1530 return 0;
1533 static int btusb_setup_intel(struct hci_dev *hdev)
1535 struct sk_buff *skb;
1536 const struct firmware *fw;
1537 const u8 *fw_ptr;
1538 int disable_patch;
1539 struct intel_version *ver;
1541 const u8 mfg_enable[] = { 0x01, 0x00 };
1542 const u8 mfg_disable[] = { 0x00, 0x00 };
1543 const u8 mfg_reset_deactivate[] = { 0x00, 0x01 };
1544 const u8 mfg_reset_activate[] = { 0x00, 0x02 };
1546 BT_DBG("%s", hdev->name);
1548 /* The controller has a bug with the first HCI command sent to it
1549 * returning number of completed commands as zero. This would stall the
1550 * command processing in the Bluetooth core.
1552 * As a workaround, send HCI Reset command first which will reset the
1553 * number of completed commands and allow normal command processing
1554 * from now on.
1556 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
1557 if (IS_ERR(skb)) {
1558 BT_ERR("%s sending initial HCI reset command failed (%ld)",
1559 hdev->name, PTR_ERR(skb));
1560 return PTR_ERR(skb);
1562 kfree_skb(skb);
1564 /* Read Intel specific controller version first to allow selection of
1565 * which firmware file to load.
1567 * The returned information are hardware variant and revision plus
1568 * firmware variant, revision and build number.
1570 skb = __hci_cmd_sync(hdev, 0xfc05, 0, NULL, HCI_INIT_TIMEOUT);
1571 if (IS_ERR(skb)) {
1572 BT_ERR("%s reading Intel fw version command failed (%ld)",
1573 hdev->name, PTR_ERR(skb));
1574 return PTR_ERR(skb);
1577 if (skb->len != sizeof(*ver)) {
1578 BT_ERR("%s Intel version event length mismatch", hdev->name);
1579 kfree_skb(skb);
1580 return -EIO;
1583 ver = (struct intel_version *)skb->data;
1585 BT_INFO("%s: read Intel version: %02x%02x%02x%02x%02x%02x%02x%02x%02x",
1586 hdev->name, ver->hw_platform, ver->hw_variant,
1587 ver->hw_revision, ver->fw_variant, ver->fw_revision,
1588 ver->fw_build_num, ver->fw_build_ww, ver->fw_build_yy,
1589 ver->fw_patch_num);
1591 /* fw_patch_num indicates the version of patch the device currently
1592 * have. If there is no patch data in the device, it is always 0x00.
1593 * So, if it is other than 0x00, no need to patch the deivce again.
1595 if (ver->fw_patch_num) {
1596 BT_INFO("%s: Intel device is already patched. patch num: %02x",
1597 hdev->name, ver->fw_patch_num);
1598 kfree_skb(skb);
1599 btintel_check_bdaddr(hdev);
1600 return 0;
1603 /* Opens the firmware patch file based on the firmware version read
1604 * from the controller. If it fails to open the matching firmware
1605 * patch file, it tries to open the default firmware patch file.
1606 * If no patch file is found, allow the device to operate without
1607 * a patch.
1609 fw = btusb_setup_intel_get_fw(hdev, ver);
1610 if (!fw) {
1611 kfree_skb(skb);
1612 btintel_check_bdaddr(hdev);
1613 return 0;
1615 fw_ptr = fw->data;
1617 /* This Intel specific command enables the manufacturer mode of the
1618 * controller.
1620 * Only while this mode is enabled, the driver can download the
1621 * firmware patch data and configuration parameters.
1623 skb = __hci_cmd_sync(hdev, 0xfc11, 2, mfg_enable, HCI_INIT_TIMEOUT);
1624 if (IS_ERR(skb)) {
1625 BT_ERR("%s entering Intel manufacturer mode failed (%ld)",
1626 hdev->name, PTR_ERR(skb));
1627 release_firmware(fw);
1628 return PTR_ERR(skb);
1631 kfree_skb(skb);
1633 disable_patch = 1;
1635 /* The firmware data file consists of list of Intel specific HCI
1636 * commands and its expected events. The first byte indicates the
1637 * type of the message, either HCI command or HCI event.
1639 * It reads the command and its expected event from the firmware file,
1640 * and send to the controller. Once __hci_cmd_sync_ev() returns,
1641 * the returned event is compared with the event read from the firmware
1642 * file and it will continue until all the messages are downloaded to
1643 * the controller.
1645 * Once the firmware patching is completed successfully,
1646 * the manufacturer mode is disabled with reset and activating the
1647 * downloaded patch.
1649 * If the firmware patching fails, the manufacturer mode is
1650 * disabled with reset and deactivating the patch.
1652 * If the default patch file is used, no reset is done when disabling
1653 * the manufacturer.
1655 while (fw->size > fw_ptr - fw->data) {
1656 int ret;
1658 ret = btusb_setup_intel_patching(hdev, fw, &fw_ptr,
1659 &disable_patch);
1660 if (ret < 0)
1661 goto exit_mfg_deactivate;
1664 release_firmware(fw);
1666 if (disable_patch)
1667 goto exit_mfg_disable;
1669 /* Patching completed successfully and disable the manufacturer mode
1670 * with reset and activate the downloaded firmware patches.
1672 skb = __hci_cmd_sync(hdev, 0xfc11, sizeof(mfg_reset_activate),
1673 mfg_reset_activate, HCI_INIT_TIMEOUT);
1674 if (IS_ERR(skb)) {
1675 BT_ERR("%s exiting Intel manufacturer mode failed (%ld)",
1676 hdev->name, PTR_ERR(skb));
1677 return PTR_ERR(skb);
1679 kfree_skb(skb);
1681 BT_INFO("%s: Intel Bluetooth firmware patch completed and activated",
1682 hdev->name);
1684 btintel_check_bdaddr(hdev);
1685 return 0;
1687 exit_mfg_disable:
1688 /* Disable the manufacturer mode without reset */
1689 skb = __hci_cmd_sync(hdev, 0xfc11, sizeof(mfg_disable), mfg_disable,
1690 HCI_INIT_TIMEOUT);
1691 if (IS_ERR(skb)) {
1692 BT_ERR("%s exiting Intel manufacturer mode failed (%ld)",
1693 hdev->name, PTR_ERR(skb));
1694 return PTR_ERR(skb);
1696 kfree_skb(skb);
1698 BT_INFO("%s: Intel Bluetooth firmware patch completed", hdev->name);
1700 btintel_check_bdaddr(hdev);
1701 return 0;
1703 exit_mfg_deactivate:
1704 release_firmware(fw);
1706 /* Patching failed. Disable the manufacturer mode with reset and
1707 * deactivate the downloaded firmware patches.
1709 skb = __hci_cmd_sync(hdev, 0xfc11, sizeof(mfg_reset_deactivate),
1710 mfg_reset_deactivate, HCI_INIT_TIMEOUT);
1711 if (IS_ERR(skb)) {
1712 BT_ERR("%s exiting Intel manufacturer mode failed (%ld)",
1713 hdev->name, PTR_ERR(skb));
1714 return PTR_ERR(skb);
1716 kfree_skb(skb);
1718 BT_INFO("%s: Intel Bluetooth firmware patch completed and deactivated",
1719 hdev->name);
1721 btintel_check_bdaddr(hdev);
1722 return 0;
1725 static int inject_cmd_complete(struct hci_dev *hdev, __u16 opcode)
1727 struct sk_buff *skb;
1728 struct hci_event_hdr *hdr;
1729 struct hci_ev_cmd_complete *evt;
1731 skb = bt_skb_alloc(sizeof(*hdr) + sizeof(*evt) + 1, GFP_ATOMIC);
1732 if (!skb)
1733 return -ENOMEM;
1735 hdr = (struct hci_event_hdr *)skb_put(skb, sizeof(*hdr));
1736 hdr->evt = HCI_EV_CMD_COMPLETE;
1737 hdr->plen = sizeof(*evt) + 1;
1739 evt = (struct hci_ev_cmd_complete *)skb_put(skb, sizeof(*evt));
1740 evt->ncmd = 0x01;
1741 evt->opcode = cpu_to_le16(opcode);
1743 *skb_put(skb, 1) = 0x00;
1745 bt_cb(skb)->pkt_type = HCI_EVENT_PKT;
1747 return hci_recv_frame(hdev, skb);
1750 static int btusb_recv_bulk_intel(struct btusb_data *data, void *buffer,
1751 int count)
1753 /* When the device is in bootloader mode, then it can send
1754 * events via the bulk endpoint. These events are treated the
1755 * same way as the ones received from the interrupt endpoint.
1757 if (test_bit(BTUSB_BOOTLOADER, &data->flags))
1758 return btusb_recv_intr(data, buffer, count);
1760 return btusb_recv_bulk(data, buffer, count);
1763 static void btusb_intel_bootup(struct btusb_data *data, const void *ptr,
1764 unsigned int len)
1766 const struct intel_bootup *evt = ptr;
1768 if (len != sizeof(*evt))
1769 return;
1771 if (test_and_clear_bit(BTUSB_BOOTING, &data->flags)) {
1772 smp_mb__after_atomic();
1773 wake_up_bit(&data->flags, BTUSB_BOOTING);
1777 static void btusb_intel_secure_send_result(struct btusb_data *data,
1778 const void *ptr, unsigned int len)
1780 const struct intel_secure_send_result *evt = ptr;
1782 if (len != sizeof(*evt))
1783 return;
1785 if (evt->result)
1786 set_bit(BTUSB_FIRMWARE_FAILED, &data->flags);
1788 if (test_and_clear_bit(BTUSB_DOWNLOADING, &data->flags) &&
1789 test_bit(BTUSB_FIRMWARE_LOADED, &data->flags)) {
1790 smp_mb__after_atomic();
1791 wake_up_bit(&data->flags, BTUSB_DOWNLOADING);
1795 static int btusb_recv_event_intel(struct hci_dev *hdev, struct sk_buff *skb)
1797 struct btusb_data *data = hci_get_drvdata(hdev);
1799 if (test_bit(BTUSB_BOOTLOADER, &data->flags)) {
1800 struct hci_event_hdr *hdr = (void *)skb->data;
1802 if (skb->len > HCI_EVENT_HDR_SIZE && hdr->evt == 0xff &&
1803 hdr->plen > 0) {
1804 const void *ptr = skb->data + HCI_EVENT_HDR_SIZE + 1;
1805 unsigned int len = skb->len - HCI_EVENT_HDR_SIZE - 1;
1807 switch (skb->data[2]) {
1808 case 0x02:
1809 /* When switching to the operational firmware
1810 * the device sends a vendor specific event
1811 * indicating that the bootup completed.
1813 btusb_intel_bootup(data, ptr, len);
1814 break;
1815 case 0x06:
1816 /* When the firmware loading completes the
1817 * device sends out a vendor specific event
1818 * indicating the result of the firmware
1819 * loading.
1821 btusb_intel_secure_send_result(data, ptr, len);
1822 break;
1827 return hci_recv_frame(hdev, skb);
1830 static int btusb_send_frame_intel(struct hci_dev *hdev, struct sk_buff *skb)
1832 struct btusb_data *data = hci_get_drvdata(hdev);
1833 struct urb *urb;
1835 BT_DBG("%s", hdev->name);
1837 if (!test_bit(HCI_RUNNING, &hdev->flags))
1838 return -EBUSY;
1840 switch (bt_cb(skb)->pkt_type) {
1841 case HCI_COMMAND_PKT:
1842 if (test_bit(BTUSB_BOOTLOADER, &data->flags)) {
1843 struct hci_command_hdr *cmd = (void *)skb->data;
1844 __u16 opcode = le16_to_cpu(cmd->opcode);
1846 /* When in bootloader mode and the command 0xfc09
1847 * is received, it needs to be send down the
1848 * bulk endpoint. So allocate a bulk URB instead.
1850 if (opcode == 0xfc09)
1851 urb = alloc_bulk_urb(hdev, skb);
1852 else
1853 urb = alloc_ctrl_urb(hdev, skb);
1855 /* When the 0xfc01 command is issued to boot into
1856 * the operational firmware, it will actually not
1857 * send a command complete event. To keep the flow
1858 * control working inject that event here.
1860 if (opcode == 0xfc01)
1861 inject_cmd_complete(hdev, opcode);
1862 } else {
1863 urb = alloc_ctrl_urb(hdev, skb);
1865 if (IS_ERR(urb))
1866 return PTR_ERR(urb);
1868 hdev->stat.cmd_tx++;
1869 return submit_or_queue_tx_urb(hdev, urb);
1871 case HCI_ACLDATA_PKT:
1872 urb = alloc_bulk_urb(hdev, skb);
1873 if (IS_ERR(urb))
1874 return PTR_ERR(urb);
1876 hdev->stat.acl_tx++;
1877 return submit_or_queue_tx_urb(hdev, urb);
1879 case HCI_SCODATA_PKT:
1880 if (hci_conn_num(hdev, SCO_LINK) < 1)
1881 return -ENODEV;
1883 urb = alloc_isoc_urb(hdev, skb);
1884 if (IS_ERR(urb))
1885 return PTR_ERR(urb);
1887 hdev->stat.sco_tx++;
1888 return submit_tx_urb(hdev, urb);
1891 return -EILSEQ;
1894 static int btusb_intel_secure_send(struct hci_dev *hdev, u8 fragment_type,
1895 u32 plen, const void *param)
1897 while (plen > 0) {
1898 struct sk_buff *skb;
1899 u8 cmd_param[253], fragment_len = (plen > 252) ? 252 : plen;
1901 cmd_param[0] = fragment_type;
1902 memcpy(cmd_param + 1, param, fragment_len);
1904 skb = __hci_cmd_sync(hdev, 0xfc09, fragment_len + 1,
1905 cmd_param, HCI_INIT_TIMEOUT);
1906 if (IS_ERR(skb))
1907 return PTR_ERR(skb);
1909 kfree_skb(skb);
1911 plen -= fragment_len;
1912 param += fragment_len;
1915 return 0;
1918 static void btusb_intel_version_info(struct hci_dev *hdev,
1919 struct intel_version *ver)
1921 const char *variant;
1923 switch (ver->fw_variant) {
1924 case 0x06:
1925 variant = "Bootloader";
1926 break;
1927 case 0x23:
1928 variant = "Firmware";
1929 break;
1930 default:
1931 return;
1934 BT_INFO("%s: %s revision %u.%u build %u week %u %u", hdev->name,
1935 variant, ver->fw_revision >> 4, ver->fw_revision & 0x0f,
1936 ver->fw_build_num, ver->fw_build_ww, 2000 + ver->fw_build_yy);
1939 static int btusb_setup_intel_new(struct hci_dev *hdev)
1941 static const u8 reset_param[] = { 0x00, 0x01, 0x00, 0x01,
1942 0x00, 0x08, 0x04, 0x00 };
1943 struct btusb_data *data = hci_get_drvdata(hdev);
1944 struct sk_buff *skb;
1945 struct intel_version *ver;
1946 struct intel_boot_params *params;
1947 const struct firmware *fw;
1948 const u8 *fw_ptr;
1949 char fwname[64];
1950 ktime_t calltime, delta, rettime;
1951 unsigned long long duration;
1952 int err;
1954 BT_DBG("%s", hdev->name);
1956 calltime = ktime_get();
1958 /* Read the Intel version information to determine if the device
1959 * is in bootloader mode or if it already has operational firmware
1960 * loaded.
1962 skb = __hci_cmd_sync(hdev, 0xfc05, 0, NULL, HCI_INIT_TIMEOUT);
1963 if (IS_ERR(skb)) {
1964 BT_ERR("%s: Reading Intel version information failed (%ld)",
1965 hdev->name, PTR_ERR(skb));
1966 return PTR_ERR(skb);
1969 if (skb->len != sizeof(*ver)) {
1970 BT_ERR("%s: Intel version event size mismatch", hdev->name);
1971 kfree_skb(skb);
1972 return -EILSEQ;
1975 ver = (struct intel_version *)skb->data;
1977 /* The hardware platform number has a fixed value of 0x37 and
1978 * for now only accept this single value.
1980 if (ver->hw_platform != 0x37) {
1981 BT_ERR("%s: Unsupported Intel hardware platform (%u)",
1982 hdev->name, ver->hw_platform);
1983 kfree_skb(skb);
1984 return -EINVAL;
1987 /* At the moment only the hardware variant iBT 3.0 (LnP/SfP) is
1988 * supported by this firmware loading method. This check has been
1989 * put in place to ensure correct forward compatibility options
1990 * when newer hardware variants come along.
1992 if (ver->hw_variant != 0x0b) {
1993 BT_ERR("%s: Unsupported Intel hardware variant (%u)",
1994 hdev->name, ver->hw_variant);
1995 kfree_skb(skb);
1996 return -EINVAL;
1999 btusb_intel_version_info(hdev, ver);
2001 /* The firmware variant determines if the device is in bootloader
2002 * mode or is running operational firmware. The value 0x06 identifies
2003 * the bootloader and the value 0x23 identifies the operational
2004 * firmware.
2006 * When the operational firmware is already present, then only
2007 * the check for valid Bluetooth device address is needed. This
2008 * determines if the device will be added as configured or
2009 * unconfigured controller.
2011 * It is not possible to use the Secure Boot Parameters in this
2012 * case since that command is only available in bootloader mode.
2014 if (ver->fw_variant == 0x23) {
2015 kfree_skb(skb);
2016 clear_bit(BTUSB_BOOTLOADER, &data->flags);
2017 btintel_check_bdaddr(hdev);
2018 return 0;
2021 /* If the device is not in bootloader mode, then the only possible
2022 * choice is to return an error and abort the device initialization.
2024 if (ver->fw_variant != 0x06) {
2025 BT_ERR("%s: Unsupported Intel firmware variant (%u)",
2026 hdev->name, ver->fw_variant);
2027 kfree_skb(skb);
2028 return -ENODEV;
2031 kfree_skb(skb);
2033 /* Read the secure boot parameters to identify the operating
2034 * details of the bootloader.
2036 skb = __hci_cmd_sync(hdev, 0xfc0d, 0, NULL, HCI_INIT_TIMEOUT);
2037 if (IS_ERR(skb)) {
2038 BT_ERR("%s: Reading Intel boot parameters failed (%ld)",
2039 hdev->name, PTR_ERR(skb));
2040 return PTR_ERR(skb);
2043 if (skb->len != sizeof(*params)) {
2044 BT_ERR("%s: Intel boot parameters size mismatch", hdev->name);
2045 kfree_skb(skb);
2046 return -EILSEQ;
2049 params = (struct intel_boot_params *)skb->data;
2051 BT_INFO("%s: Device revision is %u", hdev->name,
2052 le16_to_cpu(params->dev_revid));
2054 BT_INFO("%s: Secure boot is %s", hdev->name,
2055 params->secure_boot ? "enabled" : "disabled");
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);
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.
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;
2072 /* If the OTP has no valid Bluetooth device address, then there will
2073 * also be no valid address for the operational firmware.
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);
2080 /* With this Intel bootloader only the hardware variant and device
2081 * revision information are used to select the right firmware.
2083 * Currently this bootloader support is limited to hardware variant
2084 * iBT 3.0 (LnP/SfP) which is identified by the value 11 (0x0b).
2086 snprintf(fwname, sizeof(fwname), "intel/ibt-11-%u.sfi",
2087 le16_to_cpu(params->dev_revid));
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;
2097 BT_INFO("%s: Found device firmware: %s", hdev->name, fwname);
2099 kfree_skb(skb);
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;
2108 set_bit(BTUSB_DOWNLOADING, &data->flags);
2110 /* Start the firmware download transaction with the Init fragment
2111 * represented by the 128 bytes of CSS header.
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;
2120 /* Send the 256 bytes of public key information from the firmware
2121 * as the PKey fragment.
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;
2130 /* Send the 256 bytes of signature information from the firmware
2131 * as the Sign fragment.
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;
2140 fw_ptr = fw->data + 644;
2142 while (fw_ptr - fw->data < fw->size) {
2143 struct hci_command_hdr *cmd = (void *)fw_ptr;
2144 u8 cmd_len;
2146 cmd_len = sizeof(*cmd) + cmd->plen;
2148 /* Send each command from the firmware data buffer as
2149 * a single Data fragment.
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;
2158 fw_ptr += cmd_len;
2161 set_bit(BTUSB_FIRMWARE_LOADED, &data->flags);
2163 BT_INFO("%s: Waiting for firmware download to complete", hdev->name);
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.
2169 * When the event processing receives the notification, then the
2170 * BTUSB_DOWNLOADING flag will be cleared.
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.
2176 err = wait_on_bit_timeout(&data->flags, BTUSB_DOWNLOADING,
2177 TASK_INTERRUPTIBLE,
2178 msecs_to_jiffies(5000));
2179 if (err == 1) {
2180 BT_ERR("%s: Firmware loading interrupted", hdev->name);
2181 err = -EINTR;
2182 goto done;
2185 if (err) {
2186 BT_ERR("%s: Firmware loading timeout", hdev->name);
2187 err = -ETIMEDOUT;
2188 goto done;
2191 if (test_bit(BTUSB_FIRMWARE_FAILED, &data->flags)) {
2192 BT_ERR("%s: Firmware loading failed", hdev->name);
2193 err = -ENOEXEC;
2194 goto done;
2197 rettime = ktime_get();
2198 delta = ktime_sub(rettime, calltime);
2199 duration = (unsigned long long) ktime_to_ns(delta) >> 10;
2201 BT_INFO("%s: Firmware loaded in %llu usecs", hdev->name, duration);
2203 done:
2204 release_firmware(fw);
2206 if (err < 0)
2207 return err;
2209 calltime = ktime_get();
2211 set_bit(BTUSB_BOOTING, &data->flags);
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);
2218 kfree_skb(skb);
2220 /* The bootloader will not indicate when the device is ready. This
2221 * is done by the operational firmware sending bootup notification.
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.
2227 BT_INFO("%s: Waiting for device to boot", hdev->name);
2229 err = wait_on_bit_timeout(&data->flags, BTUSB_BOOTING,
2230 TASK_INTERRUPTIBLE,
2231 msecs_to_jiffies(1000));
2233 if (err == 1) {
2234 BT_ERR("%s: Device boot interrupted", hdev->name);
2235 return -EINTR;
2238 if (err) {
2239 BT_ERR("%s: Device boot timeout", hdev->name);
2240 return -ETIMEDOUT;
2243 rettime = ktime_get();
2244 delta = ktime_sub(rettime, calltime);
2245 duration = (unsigned long long) ktime_to_ns(delta) >> 10;
2247 BT_INFO("%s: Device booted in %llu usecs", hdev->name, duration);
2249 clear_bit(BTUSB_BOOTLOADER, &data->flags);
2251 return 0;
2254 static void btusb_hw_error_intel(struct hci_dev *hdev, u8 code)
2256 struct sk_buff *skb;
2257 u8 type = 0x00;
2259 BT_ERR("%s: Hardware error 0x%2.2x", hdev->name, code);
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;
2267 kfree_skb(skb);
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;
2276 if (skb->len != 13) {
2277 BT_ERR("%s: Exception info size mismatch", hdev->name);
2278 kfree_skb(skb);
2279 return;
2282 BT_ERR("%s: Exception info %s", hdev->name, (char *)(skb->data + 1));
2284 kfree_skb(skb);
2287 static int btusb_shutdown_intel(struct hci_dev *hdev)
2289 struct sk_buff *skb;
2290 long ret;
2292 /* Some platforms have an issue with BT LED when the interface is
2293 * down or BT radio is turned off, which takes 5 seconds to BT LED
2294 * goes off. This command turns off the BT LED immediately.
2296 skb = __hci_cmd_sync(hdev, 0xfc3f, 0, NULL, HCI_INIT_TIMEOUT);
2297 if (IS_ERR(skb)) {
2298 ret = PTR_ERR(skb);
2299 BT_ERR("%s: turning off Intel device LED failed (%ld)",
2300 hdev->name, ret);
2301 return ret;
2303 kfree_skb(skb);
2305 return 0;
2308 static int btusb_set_bdaddr_marvell(struct hci_dev *hdev,
2309 const bdaddr_t *bdaddr)
2311 struct sk_buff *skb;
2312 u8 buf[8];
2313 long ret;
2315 buf[0] = 0xfe;
2316 buf[1] = sizeof(bdaddr_t);
2317 memcpy(buf + 2, bdaddr, sizeof(bdaddr_t));
2319 skb = __hci_cmd_sync(hdev, 0xfc22, sizeof(buf), buf, HCI_INIT_TIMEOUT);
2320 if (IS_ERR(skb)) {
2321 ret = PTR_ERR(skb);
2322 BT_ERR("%s: changing Marvell device address failed (%ld)",
2323 hdev->name, ret);
2324 return ret;
2326 kfree_skb(skb);
2328 return 0;
2331 static int btusb_set_bdaddr_ath3012(struct hci_dev *hdev,
2332 const bdaddr_t *bdaddr)
2334 struct sk_buff *skb;
2335 u8 buf[10];
2336 long ret;
2338 buf[0] = 0x01;
2339 buf[1] = 0x01;
2340 buf[2] = 0x00;
2341 buf[3] = sizeof(bdaddr_t);
2342 memcpy(buf + 4, bdaddr, sizeof(bdaddr_t));
2344 skb = __hci_cmd_sync(hdev, 0xfc0b, sizeof(buf), buf, HCI_INIT_TIMEOUT);
2345 if (IS_ERR(skb)) {
2346 ret = PTR_ERR(skb);
2347 BT_ERR("%s: Change address command failed (%ld)",
2348 hdev->name, ret);
2349 return ret;
2351 kfree_skb(skb);
2353 return 0;
2356 #define QCA_DFU_PACKET_LEN 4096
2358 #define QCA_GET_TARGET_VERSION 0x09
2359 #define QCA_CHECK_STATUS 0x05
2360 #define QCA_DFU_DOWNLOAD 0x01
2362 #define QCA_SYSCFG_UPDATED 0x40
2363 #define QCA_PATCH_UPDATED 0x80
2364 #define QCA_DFU_TIMEOUT 3000
2366 struct qca_version {
2367 __le32 rom_version;
2368 __le32 patch_version;
2369 __le32 ram_version;
2370 __le32 ref_clock;
2371 __u8 reserved[4];
2372 } __packed;
2374 struct qca_rampatch_version {
2375 __le16 rom_version;
2376 __le16 patch_version;
2377 } __packed;
2379 struct qca_device_info {
2380 u32 rom_version;
2381 u8 rampatch_hdr; /* length of header in rampatch */
2382 u8 nvm_hdr; /* length of header in NVM */
2383 u8 ver_offset; /* offset of version structure in rampatch */
2386 static const struct qca_device_info qca_devices_table[] = {
2387 { 0x00000100, 20, 4, 10 }, /* Rome 1.0 */
2388 { 0x00000101, 20, 4, 10 }, /* Rome 1.1 */
2389 { 0x00000200, 28, 4, 18 }, /* Rome 2.0 */
2390 { 0x00000201, 28, 4, 18 }, /* Rome 2.1 */
2391 { 0x00000300, 28, 4, 18 }, /* Rome 3.0 */
2392 { 0x00000302, 28, 4, 18 }, /* Rome 3.2 */
2395 static int btusb_qca_send_vendor_req(struct hci_dev *hdev, u8 request,
2396 void *data, u16 size)
2398 struct btusb_data *btdata = hci_get_drvdata(hdev);
2399 struct usb_device *udev = btdata->udev;
2400 int pipe, err;
2401 u8 *buf;
2403 buf = kmalloc(size, GFP_KERNEL);
2404 if (!buf)
2405 return -ENOMEM;
2407 /* Found some of USB hosts have IOT issues with ours so that we should
2408 * not wait until HCI layer is ready.
2410 pipe = usb_rcvctrlpipe(udev, 0);
2411 err = usb_control_msg(udev, pipe, request, USB_TYPE_VENDOR | USB_DIR_IN,
2412 0, 0, buf, size, USB_CTRL_SET_TIMEOUT);
2413 if (err < 0) {
2414 BT_ERR("%s: Failed to access otp area (%d)", hdev->name, err);
2415 goto done;
2418 memcpy(data, buf, size);
2420 done:
2421 kfree(buf);
2423 return err;
2426 static int btusb_setup_qca_download_fw(struct hci_dev *hdev,
2427 const struct firmware *firmware,
2428 size_t hdr_size)
2430 struct btusb_data *btdata = hci_get_drvdata(hdev);
2431 struct usb_device *udev = btdata->udev;
2432 size_t count, size, sent = 0;
2433 int pipe, len, err;
2434 u8 *buf;
2436 buf = kmalloc(QCA_DFU_PACKET_LEN, GFP_KERNEL);
2437 if (!buf)
2438 return -ENOMEM;
2440 count = firmware->size;
2442 size = min_t(size_t, count, hdr_size);
2443 memcpy(buf, firmware->data, size);
2445 /* USB patches should go down to controller through USB path
2446 * because binary format fits to go down through USB channel.
2447 * USB control path is for patching headers and USB bulk is for
2448 * patch body.
2450 pipe = usb_sndctrlpipe(udev, 0);
2451 err = usb_control_msg(udev, pipe, QCA_DFU_DOWNLOAD, USB_TYPE_VENDOR,
2452 0, 0, buf, size, USB_CTRL_SET_TIMEOUT);
2453 if (err < 0) {
2454 BT_ERR("%s: Failed to send headers (%d)", hdev->name, err);
2455 goto done;
2458 sent += size;
2459 count -= size;
2461 while (count) {
2462 size = min_t(size_t, count, QCA_DFU_PACKET_LEN);
2464 memcpy(buf, firmware->data + sent, size);
2466 pipe = usb_sndbulkpipe(udev, 0x02);
2467 err = usb_bulk_msg(udev, pipe, buf, size, &len,
2468 QCA_DFU_TIMEOUT);
2469 if (err < 0) {
2470 BT_ERR("%s: Failed to send body at %zd of %zd (%d)",
2471 hdev->name, sent, firmware->size, err);
2472 break;
2475 if (size != len) {
2476 BT_ERR("%s: Failed to get bulk buffer", hdev->name);
2477 err = -EILSEQ;
2478 break;
2481 sent += size;
2482 count -= size;
2485 done:
2486 kfree(buf);
2487 return err;
2490 static int btusb_setup_qca_load_rampatch(struct hci_dev *hdev,
2491 struct qca_version *ver,
2492 const struct qca_device_info *info)
2494 struct qca_rampatch_version *rver;
2495 const struct firmware *fw;
2496 u32 ver_rom, ver_patch;
2497 u16 rver_rom, rver_patch;
2498 char fwname[64];
2499 int err;
2501 ver_rom = le32_to_cpu(ver->rom_version);
2502 ver_patch = le32_to_cpu(ver->patch_version);
2504 snprintf(fwname, sizeof(fwname), "qca/rampatch_usb_%08x.bin", ver_rom);
2506 err = request_firmware(&fw, fwname, &hdev->dev);
2507 if (err) {
2508 BT_ERR("%s: failed to request rampatch file: %s (%d)",
2509 hdev->name, fwname, err);
2510 return err;
2513 BT_INFO("%s: using rampatch file: %s", hdev->name, fwname);
2515 rver = (struct qca_rampatch_version *)(fw->data + info->ver_offset);
2516 rver_rom = le16_to_cpu(rver->rom_version);
2517 rver_patch = le16_to_cpu(rver->patch_version);
2519 BT_INFO("%s: QCA: patch rome 0x%x build 0x%x, firmware rome 0x%x "
2520 "build 0x%x", hdev->name, rver_rom, rver_patch, ver_rom,
2521 ver_patch);
2523 if (rver_rom != ver_rom || rver_patch <= ver_patch) {
2524 BT_ERR("%s: rampatch file version did not match with firmware",
2525 hdev->name);
2526 err = -EINVAL;
2527 goto done;
2530 err = btusb_setup_qca_download_fw(hdev, fw, info->rampatch_hdr);
2532 done:
2533 release_firmware(fw);
2535 return err;
2538 static int btusb_setup_qca_load_nvm(struct hci_dev *hdev,
2539 struct qca_version *ver,
2540 const struct qca_device_info *info)
2542 const struct firmware *fw;
2543 char fwname[64];
2544 int err;
2546 snprintf(fwname, sizeof(fwname), "qca/nvm_usb_%08x.bin",
2547 le32_to_cpu(ver->rom_version));
2549 err = request_firmware(&fw, fwname, &hdev->dev);
2550 if (err) {
2551 BT_ERR("%s: failed to request NVM file: %s (%d)",
2552 hdev->name, fwname, err);
2553 return err;
2556 BT_INFO("%s: using NVM file: %s", hdev->name, fwname);
2558 err = btusb_setup_qca_download_fw(hdev, fw, info->nvm_hdr);
2560 release_firmware(fw);
2562 return err;
2565 static int btusb_setup_qca(struct hci_dev *hdev)
2567 const struct qca_device_info *info = NULL;
2568 struct qca_version ver;
2569 u32 ver_rom;
2570 u8 status;
2571 int i, err;
2573 err = btusb_qca_send_vendor_req(hdev, QCA_GET_TARGET_VERSION, &ver,
2574 sizeof(ver));
2575 if (err < 0)
2576 return err;
2578 ver_rom = le32_to_cpu(ver.rom_version);
2579 for (i = 0; i < ARRAY_SIZE(qca_devices_table); i++) {
2580 if (ver_rom == qca_devices_table[i].rom_version)
2581 info = &qca_devices_table[i];
2583 if (!info) {
2584 BT_ERR("%s: don't support firmware rome 0x%x", hdev->name,
2585 ver_rom);
2586 return -ENODEV;
2589 err = btusb_qca_send_vendor_req(hdev, QCA_CHECK_STATUS, &status,
2590 sizeof(status));
2591 if (err < 0)
2592 return err;
2594 if (!(status & QCA_PATCH_UPDATED)) {
2595 err = btusb_setup_qca_load_rampatch(hdev, &ver, info);
2596 if (err < 0)
2597 return err;
2600 if (!(status & QCA_SYSCFG_UPDATED)) {
2601 err = btusb_setup_qca_load_nvm(hdev, &ver, info);
2602 if (err < 0)
2603 return err;
2606 return 0;
2609 static int btusb_probe(struct usb_interface *intf,
2610 const struct usb_device_id *id)
2612 struct usb_endpoint_descriptor *ep_desc;
2613 struct btusb_data *data;
2614 struct hci_dev *hdev;
2615 int i, err;
2617 BT_DBG("intf %p id %p", intf, id);
2619 /* interface numbers are hardcoded in the spec */
2620 if (intf->cur_altsetting->desc.bInterfaceNumber != 0)
2621 return -ENODEV;
2623 if (!id->driver_info) {
2624 const struct usb_device_id *match;
2626 match = usb_match_id(intf, blacklist_table);
2627 if (match)
2628 id = match;
2631 if (id->driver_info == BTUSB_IGNORE)
2632 return -ENODEV;
2634 if (id->driver_info & BTUSB_ATH3012) {
2635 struct usb_device *udev = interface_to_usbdev(intf);
2637 /* Old firmware would otherwise let ath3k driver load
2638 * patch and sysconfig files */
2639 if (le16_to_cpu(udev->descriptor.bcdDevice) <= 0x0001)
2640 return -ENODEV;
2643 data = devm_kzalloc(&intf->dev, sizeof(*data), GFP_KERNEL);
2644 if (!data)
2645 return -ENOMEM;
2647 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
2648 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
2650 if (!data->intr_ep && usb_endpoint_is_int_in(ep_desc)) {
2651 data->intr_ep = ep_desc;
2652 continue;
2655 if (!data->bulk_tx_ep && usb_endpoint_is_bulk_out(ep_desc)) {
2656 data->bulk_tx_ep = ep_desc;
2657 continue;
2660 if (!data->bulk_rx_ep && usb_endpoint_is_bulk_in(ep_desc)) {
2661 data->bulk_rx_ep = ep_desc;
2662 continue;
2666 if (!data->intr_ep || !data->bulk_tx_ep || !data->bulk_rx_ep)
2667 return -ENODEV;
2669 if (id->driver_info & BTUSB_AMP) {
2670 data->cmdreq_type = USB_TYPE_CLASS | 0x01;
2671 data->cmdreq = 0x2b;
2672 } else {
2673 data->cmdreq_type = USB_TYPE_CLASS;
2674 data->cmdreq = 0x00;
2677 data->udev = interface_to_usbdev(intf);
2678 data->intf = intf;
2680 INIT_WORK(&data->work, btusb_work);
2681 INIT_WORK(&data->waker, btusb_waker);
2682 init_usb_anchor(&data->deferred);
2683 init_usb_anchor(&data->tx_anchor);
2684 spin_lock_init(&data->txlock);
2686 init_usb_anchor(&data->intr_anchor);
2687 init_usb_anchor(&data->bulk_anchor);
2688 init_usb_anchor(&data->isoc_anchor);
2689 spin_lock_init(&data->rxlock);
2691 if (id->driver_info & BTUSB_INTEL_NEW) {
2692 data->recv_event = btusb_recv_event_intel;
2693 data->recv_bulk = btusb_recv_bulk_intel;
2694 set_bit(BTUSB_BOOTLOADER, &data->flags);
2695 } else {
2696 data->recv_event = hci_recv_frame;
2697 data->recv_bulk = btusb_recv_bulk;
2700 hdev = hci_alloc_dev();
2701 if (!hdev)
2702 return -ENOMEM;
2704 hdev->bus = HCI_USB;
2705 hci_set_drvdata(hdev, data);
2707 if (id->driver_info & BTUSB_AMP)
2708 hdev->dev_type = HCI_AMP;
2709 else
2710 hdev->dev_type = HCI_BREDR;
2712 data->hdev = hdev;
2714 SET_HCIDEV_DEV(hdev, &intf->dev);
2716 hdev->open = btusb_open;
2717 hdev->close = btusb_close;
2718 hdev->flush = btusb_flush;
2719 hdev->send = btusb_send_frame;
2720 hdev->notify = btusb_notify;
2722 if (id->driver_info & BTUSB_BCM92035)
2723 hdev->setup = btusb_setup_bcm92035;
2725 #ifdef CONFIG_BT_HCIBTUSB_BCM
2726 if (id->driver_info & BTUSB_BCM_PATCHRAM) {
2727 hdev->setup = btbcm_setup_patchram;
2728 hdev->set_bdaddr = btbcm_set_bdaddr;
2731 if (id->driver_info & BTUSB_BCM_APPLE)
2732 hdev->setup = btbcm_setup_apple;
2733 #endif
2735 if (id->driver_info & BTUSB_INTEL) {
2736 hdev->setup = btusb_setup_intel;
2737 hdev->shutdown = btusb_shutdown_intel;
2738 hdev->set_bdaddr = btintel_set_bdaddr;
2739 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
2740 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
2743 if (id->driver_info & BTUSB_INTEL_NEW) {
2744 hdev->send = btusb_send_frame_intel;
2745 hdev->setup = btusb_setup_intel_new;
2746 hdev->hw_error = btusb_hw_error_intel;
2747 hdev->set_bdaddr = btintel_set_bdaddr;
2748 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
2751 if (id->driver_info & BTUSB_MARVELL)
2752 hdev->set_bdaddr = btusb_set_bdaddr_marvell;
2754 if (id->driver_info & BTUSB_SWAVE) {
2755 set_bit(HCI_QUIRK_FIXUP_INQUIRY_MODE, &hdev->quirks);
2756 set_bit(HCI_QUIRK_BROKEN_LOCAL_COMMANDS, &hdev->quirks);
2759 if (id->driver_info & BTUSB_INTEL_BOOT)
2760 set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
2762 if (id->driver_info & BTUSB_ATH3012) {
2763 hdev->set_bdaddr = btusb_set_bdaddr_ath3012;
2764 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
2765 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
2768 if (id->driver_info & BTUSB_QCA_ROME) {
2769 data->setup_on_usb = btusb_setup_qca;
2770 hdev->set_bdaddr = btusb_set_bdaddr_ath3012;
2773 #ifdef CONFIG_BT_HCIBTUSB_RTL
2774 if (id->driver_info & BTUSB_REALTEK) {
2775 hdev->setup = btrtl_setup_realtek;
2777 /* Realtek devices lose their updated firmware over suspend,
2778 * but the USB hub doesn't notice any status change.
2779 * Explicitly request a device reset on resume.
2781 set_bit(BTUSB_RESET_RESUME, &data->flags);
2783 #endif
2785 if (id->driver_info & BTUSB_AMP) {
2786 /* AMP controllers do not support SCO packets */
2787 data->isoc = NULL;
2788 } else {
2789 /* Interface numbers are hardcoded in the specification */
2790 data->isoc = usb_ifnum_to_if(data->udev, 1);
2793 if (!reset)
2794 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
2796 if (force_scofix || id->driver_info & BTUSB_WRONG_SCO_MTU) {
2797 if (!disable_scofix)
2798 set_bit(HCI_QUIRK_FIXUP_BUFFER_SIZE, &hdev->quirks);
2801 if (id->driver_info & BTUSB_BROKEN_ISOC)
2802 data->isoc = NULL;
2804 if (id->driver_info & BTUSB_DIGIANSWER) {
2805 data->cmdreq_type = USB_TYPE_VENDOR;
2806 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
2809 if (id->driver_info & BTUSB_CSR) {
2810 struct usb_device *udev = data->udev;
2811 u16 bcdDevice = le16_to_cpu(udev->descriptor.bcdDevice);
2813 /* Old firmware would otherwise execute USB reset */
2814 if (bcdDevice < 0x117)
2815 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
2817 /* Fake CSR devices with broken commands */
2818 if (bcdDevice <= 0x100)
2819 hdev->setup = btusb_setup_csr;
2821 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
2824 if (id->driver_info & BTUSB_SNIFFER) {
2825 struct usb_device *udev = data->udev;
2827 /* New sniffer firmware has crippled HCI interface */
2828 if (le16_to_cpu(udev->descriptor.bcdDevice) > 0x997)
2829 set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
2832 if (id->driver_info & BTUSB_INTEL_BOOT) {
2833 /* A bug in the bootloader causes that interrupt interface is
2834 * only enabled after receiving SetInterface(0, AltSetting=0).
2836 err = usb_set_interface(data->udev, 0, 0);
2837 if (err < 0) {
2838 BT_ERR("failed to set interface 0, alt 0 %d", err);
2839 hci_free_dev(hdev);
2840 return err;
2844 if (data->isoc) {
2845 err = usb_driver_claim_interface(&btusb_driver,
2846 data->isoc, data);
2847 if (err < 0) {
2848 hci_free_dev(hdev);
2849 return err;
2853 err = hci_register_dev(hdev);
2854 if (err < 0) {
2855 hci_free_dev(hdev);
2856 return err;
2859 usb_set_intfdata(intf, data);
2861 return 0;
2864 static void btusb_disconnect(struct usb_interface *intf)
2866 struct btusb_data *data = usb_get_intfdata(intf);
2867 struct hci_dev *hdev;
2869 BT_DBG("intf %p", intf);
2871 if (!data)
2872 return;
2874 hdev = data->hdev;
2875 usb_set_intfdata(data->intf, NULL);
2877 if (data->isoc)
2878 usb_set_intfdata(data->isoc, NULL);
2880 hci_unregister_dev(hdev);
2882 if (intf == data->isoc)
2883 usb_driver_release_interface(&btusb_driver, data->intf);
2884 else if (data->isoc)
2885 usb_driver_release_interface(&btusb_driver, data->isoc);
2887 hci_free_dev(hdev);
2890 #ifdef CONFIG_PM
2891 static int btusb_suspend(struct usb_interface *intf, pm_message_t message)
2893 struct btusb_data *data = usb_get_intfdata(intf);
2895 BT_DBG("intf %p", intf);
2897 if (data->suspend_count++)
2898 return 0;
2900 spin_lock_irq(&data->txlock);
2901 if (!(PMSG_IS_AUTO(message) && data->tx_in_flight)) {
2902 set_bit(BTUSB_SUSPENDING, &data->flags);
2903 spin_unlock_irq(&data->txlock);
2904 } else {
2905 spin_unlock_irq(&data->txlock);
2906 data->suspend_count--;
2907 return -EBUSY;
2910 cancel_work_sync(&data->work);
2912 btusb_stop_traffic(data);
2913 usb_kill_anchored_urbs(&data->tx_anchor);
2915 /* Optionally request a device reset on resume, but only when
2916 * wakeups are disabled. If wakeups are enabled we assume the
2917 * device will stay powered up throughout suspend.
2919 if (test_bit(BTUSB_RESET_RESUME, &data->flags) &&
2920 !device_may_wakeup(&data->udev->dev))
2921 data->udev->reset_resume = 1;
2923 return 0;
2926 static void play_deferred(struct btusb_data *data)
2928 struct urb *urb;
2929 int err;
2931 while ((urb = usb_get_from_anchor(&data->deferred))) {
2932 err = usb_submit_urb(urb, GFP_ATOMIC);
2933 if (err < 0)
2934 break;
2936 data->tx_in_flight++;
2938 usb_scuttle_anchored_urbs(&data->deferred);
2941 static int btusb_resume(struct usb_interface *intf)
2943 struct btusb_data *data = usb_get_intfdata(intf);
2944 struct hci_dev *hdev = data->hdev;
2945 int err = 0;
2947 BT_DBG("intf %p", intf);
2949 if (--data->suspend_count)
2950 return 0;
2952 if (!test_bit(HCI_RUNNING, &hdev->flags))
2953 goto done;
2955 if (test_bit(BTUSB_INTR_RUNNING, &data->flags)) {
2956 err = btusb_submit_intr_urb(hdev, GFP_NOIO);
2957 if (err < 0) {
2958 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
2959 goto failed;
2963 if (test_bit(BTUSB_BULK_RUNNING, &data->flags)) {
2964 err = btusb_submit_bulk_urb(hdev, GFP_NOIO);
2965 if (err < 0) {
2966 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
2967 goto failed;
2970 btusb_submit_bulk_urb(hdev, GFP_NOIO);
2973 if (test_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
2974 if (btusb_submit_isoc_urb(hdev, GFP_NOIO) < 0)
2975 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
2976 else
2977 btusb_submit_isoc_urb(hdev, GFP_NOIO);
2980 spin_lock_irq(&data->txlock);
2981 play_deferred(data);
2982 clear_bit(BTUSB_SUSPENDING, &data->flags);
2983 spin_unlock_irq(&data->txlock);
2984 schedule_work(&data->work);
2986 return 0;
2988 failed:
2989 usb_scuttle_anchored_urbs(&data->deferred);
2990 done:
2991 spin_lock_irq(&data->txlock);
2992 clear_bit(BTUSB_SUSPENDING, &data->flags);
2993 spin_unlock_irq(&data->txlock);
2995 return err;
2997 #endif
2999 static struct usb_driver btusb_driver = {
3000 .name = "btusb",
3001 .probe = btusb_probe,
3002 .disconnect = btusb_disconnect,
3003 #ifdef CONFIG_PM
3004 .suspend = btusb_suspend,
3005 .resume = btusb_resume,
3006 #endif
3007 .id_table = btusb_table,
3008 .supports_autosuspend = 1,
3009 .disable_hub_initiated_lpm = 1,
3012 module_usb_driver(btusb_driver);
3014 module_param(disable_scofix, bool, 0644);
3015 MODULE_PARM_DESC(disable_scofix, "Disable fixup of wrong SCO buffer size");
3017 module_param(force_scofix, bool, 0644);
3018 MODULE_PARM_DESC(force_scofix, "Force fixup of wrong SCO buffers size");
3020 module_param(reset, bool, 0644);
3021 MODULE_PARM_DESC(reset, "Send HCI reset command on initialization");
3023 MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
3024 MODULE_DESCRIPTION("Generic Bluetooth USB driver ver " VERSION);
3025 MODULE_VERSION(VERSION);
3026 MODULE_LICENSE("GPL");