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>
28 #include <net/bluetooth/bluetooth.h>
29 #include <net/bluetooth/hci_core.h>
33 static bool disable_scofix;
34 static bool force_scofix;
36 static bool reset = 1;
38 static struct usb_driver btusb_driver;
40 #define BTUSB_IGNORE 0x01
41 #define BTUSB_DIGIANSWER 0x02
42 #define BTUSB_CSR 0x04
43 #define BTUSB_SNIFFER 0x08
44 #define BTUSB_BCM92035 0x10
45 #define BTUSB_BROKEN_ISOC 0x20
46 #define BTUSB_WRONG_SCO_MTU 0x40
47 #define BTUSB_ATH3012 0x80
48 #define BTUSB_INTEL 0x100
49 #define BTUSB_INTEL_BOOT 0x200
50 #define BTUSB_BCM_PATCHRAM 0x400
51 #define BTUSB_MARVELL 0x800
52 #define BTUSB_SWAVE 0x1000
53 #define BTUSB_INTEL_NEW 0x2000
55 static const struct usb_device_id btusb_table[] = {
56 /* Generic Bluetooth USB device */
57 { USB_DEVICE_INFO(0xe0, 0x01, 0x01) },
59 /* Apple-specific (Broadcom) devices */
60 { USB_VENDOR_AND_INTERFACE_INFO(0x05ac, 0xff, 0x01, 0x01) },
62 /* MediaTek MT76x0E */
63 { USB_DEVICE(0x0e8d, 0x763f) },
65 /* Broadcom SoftSailing reporting vendor specific */
66 { USB_DEVICE(0x0a5c, 0x21e1) },
68 /* Apple MacBookPro 7,1 */
69 { USB_DEVICE(0x05ac, 0x8213) },
72 { USB_DEVICE(0x05ac, 0x8215) },
74 /* Apple MacBookPro6,2 */
75 { USB_DEVICE(0x05ac, 0x8218) },
77 /* Apple MacBookAir3,1, MacBookAir3,2 */
78 { USB_DEVICE(0x05ac, 0x821b) },
80 /* Apple MacBookAir4,1 */
81 { USB_DEVICE(0x05ac, 0x821f) },
83 /* Apple MacBookPro8,2 */
84 { USB_DEVICE(0x05ac, 0x821a) },
86 /* Apple MacMini5,1 */
87 { USB_DEVICE(0x05ac, 0x8281) },
89 /* AVM BlueFRITZ! USB v2.0 */
90 { USB_DEVICE(0x057c, 0x3800), .driver_info = BTUSB_SWAVE },
92 /* Bluetooth Ultraport Module from IBM */
93 { USB_DEVICE(0x04bf, 0x030a) },
95 /* ALPS Modules with non-standard id */
96 { USB_DEVICE(0x044e, 0x3001) },
97 { USB_DEVICE(0x044e, 0x3002) },
99 /* Ericsson with non-standard id */
100 { USB_DEVICE(0x0bdb, 0x1002) },
102 /* Canyon CN-BTU1 with HID interfaces */
103 { USB_DEVICE(0x0c10, 0x0000) },
105 /* Broadcom BCM20702A0 */
106 { USB_DEVICE(0x0489, 0xe042) },
107 { USB_DEVICE(0x04ca, 0x2003) },
108 { USB_DEVICE(0x0b05, 0x17b5) },
109 { USB_DEVICE(0x0b05, 0x17cb) },
110 { USB_DEVICE(0x413c, 0x8197) },
111 { USB_DEVICE(0x13d3, 0x3404),
112 .driver_info = BTUSB_BCM_PATCHRAM },
114 /* Broadcom BCM20702B0 (Dynex/Insignia) */
115 { USB_DEVICE(0x19ff, 0x0239), .driver_info = BTUSB_BCM_PATCHRAM },
117 /* Foxconn - Hon Hai */
118 { USB_VENDOR_AND_INTERFACE_INFO(0x0489, 0xff, 0x01, 0x01),
119 .driver_info = BTUSB_BCM_PATCHRAM },
121 /* Broadcom devices with vendor specific id */
122 { USB_VENDOR_AND_INTERFACE_INFO(0x0a5c, 0xff, 0x01, 0x01),
123 .driver_info = BTUSB_BCM_PATCHRAM },
125 /* ASUSTek Computer - Broadcom based */
126 { USB_VENDOR_AND_INTERFACE_INFO(0x0b05, 0xff, 0x01, 0x01),
127 .driver_info = BTUSB_BCM_PATCHRAM },
129 /* Belkin F8065bf - Broadcom based */
130 { USB_VENDOR_AND_INTERFACE_INFO(0x050d, 0xff, 0x01, 0x01) },
132 /* IMC Networks - Broadcom based */
133 { USB_VENDOR_AND_INTERFACE_INFO(0x13d3, 0xff, 0x01, 0x01) },
135 /* Intel Bluetooth USB Bootloader (RAM module) */
136 { USB_DEVICE(0x8087, 0x0a5a),
137 .driver_info = BTUSB_INTEL_BOOT | BTUSB_BROKEN_ISOC },
139 { } /* Terminating entry */
142 MODULE_DEVICE_TABLE(usb, btusb_table);
144 static const struct usb_device_id blacklist_table[] = {
145 /* CSR BlueCore devices */
146 { USB_DEVICE(0x0a12, 0x0001), .driver_info = BTUSB_CSR },
148 /* Broadcom BCM2033 without firmware */
149 { USB_DEVICE(0x0a5c, 0x2033), .driver_info = BTUSB_IGNORE },
151 /* Atheros 3011 with sflash firmware */
152 { USB_DEVICE(0x0489, 0xe027), .driver_info = BTUSB_IGNORE },
153 { USB_DEVICE(0x0489, 0xe03d), .driver_info = BTUSB_IGNORE },
154 { USB_DEVICE(0x0930, 0x0215), .driver_info = BTUSB_IGNORE },
155 { USB_DEVICE(0x0cf3, 0x3002), .driver_info = BTUSB_IGNORE },
156 { USB_DEVICE(0x0cf3, 0xe019), .driver_info = BTUSB_IGNORE },
157 { USB_DEVICE(0x13d3, 0x3304), .driver_info = BTUSB_IGNORE },
159 /* Atheros AR9285 Malbec with sflash firmware */
160 { USB_DEVICE(0x03f0, 0x311d), .driver_info = BTUSB_IGNORE },
162 /* Atheros 3012 with sflash firmware */
163 { USB_DEVICE(0x0489, 0xe04d), .driver_info = BTUSB_ATH3012 },
164 { USB_DEVICE(0x0489, 0xe04e), .driver_info = BTUSB_ATH3012 },
165 { USB_DEVICE(0x0489, 0xe056), .driver_info = BTUSB_ATH3012 },
166 { USB_DEVICE(0x0489, 0xe057), .driver_info = BTUSB_ATH3012 },
167 { USB_DEVICE(0x0489, 0xe05f), .driver_info = BTUSB_ATH3012 },
168 { USB_DEVICE(0x0489, 0xe078), .driver_info = BTUSB_ATH3012 },
169 { USB_DEVICE(0x04c5, 0x1330), .driver_info = BTUSB_ATH3012 },
170 { USB_DEVICE(0x04ca, 0x3004), .driver_info = BTUSB_ATH3012 },
171 { USB_DEVICE(0x04ca, 0x3005), .driver_info = BTUSB_ATH3012 },
172 { USB_DEVICE(0x04ca, 0x3006), .driver_info = BTUSB_ATH3012 },
173 { USB_DEVICE(0x04ca, 0x3007), .driver_info = BTUSB_ATH3012 },
174 { USB_DEVICE(0x04ca, 0x3008), .driver_info = BTUSB_ATH3012 },
175 { USB_DEVICE(0x04ca, 0x300b), .driver_info = BTUSB_ATH3012 },
176 { USB_DEVICE(0x04ca, 0x3010), .driver_info = BTUSB_ATH3012 },
177 { USB_DEVICE(0x0930, 0x0219), .driver_info = BTUSB_ATH3012 },
178 { USB_DEVICE(0x0930, 0x0220), .driver_info = BTUSB_ATH3012 },
179 { USB_DEVICE(0x0930, 0x0227), .driver_info = BTUSB_ATH3012 },
180 { USB_DEVICE(0x0b05, 0x17d0), .driver_info = BTUSB_ATH3012 },
181 { USB_DEVICE(0x0cf3, 0x0036), .driver_info = BTUSB_ATH3012 },
182 { USB_DEVICE(0x0cf3, 0x3004), .driver_info = BTUSB_ATH3012 },
183 { USB_DEVICE(0x0cf3, 0x3008), .driver_info = BTUSB_ATH3012 },
184 { USB_DEVICE(0x0cf3, 0x311d), .driver_info = BTUSB_ATH3012 },
185 { USB_DEVICE(0x0cf3, 0x311e), .driver_info = BTUSB_ATH3012 },
186 { USB_DEVICE(0x0cf3, 0x311f), .driver_info = BTUSB_ATH3012 },
187 { USB_DEVICE(0x0cf3, 0x3121), .driver_info = BTUSB_ATH3012 },
188 { USB_DEVICE(0x0cf3, 0x817a), .driver_info = BTUSB_ATH3012 },
189 { USB_DEVICE(0x0cf3, 0xe003), .driver_info = BTUSB_ATH3012 },
190 { USB_DEVICE(0x0cf3, 0xe004), .driver_info = BTUSB_ATH3012 },
191 { USB_DEVICE(0x0cf3, 0xe005), .driver_info = BTUSB_ATH3012 },
192 { USB_DEVICE(0x13d3, 0x3362), .driver_info = BTUSB_ATH3012 },
193 { USB_DEVICE(0x13d3, 0x3375), .driver_info = BTUSB_ATH3012 },
194 { USB_DEVICE(0x13d3, 0x3393), .driver_info = BTUSB_ATH3012 },
195 { USB_DEVICE(0x13d3, 0x3402), .driver_info = BTUSB_ATH3012 },
196 { USB_DEVICE(0x13d3, 0x3408), .driver_info = BTUSB_ATH3012 },
197 { USB_DEVICE(0x13d3, 0x3423), .driver_info = BTUSB_ATH3012 },
198 { USB_DEVICE(0x13d3, 0x3432), .driver_info = BTUSB_ATH3012 },
200 /* Atheros AR5BBU12 with sflash firmware */
201 { USB_DEVICE(0x0489, 0xe02c), .driver_info = BTUSB_IGNORE },
203 /* Atheros AR5BBU12 with sflash firmware */
204 { USB_DEVICE(0x0489, 0xe036), .driver_info = BTUSB_ATH3012 },
205 { USB_DEVICE(0x0489, 0xe03c), .driver_info = BTUSB_ATH3012 },
207 /* Broadcom BCM2035 */
208 { USB_DEVICE(0x0a5c, 0x2009), .driver_info = BTUSB_BCM92035 },
209 { USB_DEVICE(0x0a5c, 0x200a), .driver_info = BTUSB_WRONG_SCO_MTU },
210 { USB_DEVICE(0x0a5c, 0x2035), .driver_info = BTUSB_WRONG_SCO_MTU },
212 /* Broadcom BCM2045 */
213 { USB_DEVICE(0x0a5c, 0x2039), .driver_info = BTUSB_WRONG_SCO_MTU },
214 { USB_DEVICE(0x0a5c, 0x2101), .driver_info = BTUSB_WRONG_SCO_MTU },
216 /* IBM/Lenovo ThinkPad with Broadcom chip */
217 { USB_DEVICE(0x0a5c, 0x201e), .driver_info = BTUSB_WRONG_SCO_MTU },
218 { USB_DEVICE(0x0a5c, 0x2110), .driver_info = BTUSB_WRONG_SCO_MTU },
220 /* HP laptop with Broadcom chip */
221 { USB_DEVICE(0x03f0, 0x171d), .driver_info = BTUSB_WRONG_SCO_MTU },
223 /* Dell laptop with Broadcom chip */
224 { USB_DEVICE(0x413c, 0x8126), .driver_info = BTUSB_WRONG_SCO_MTU },
226 /* Dell Wireless 370 and 410 devices */
227 { USB_DEVICE(0x413c, 0x8152), .driver_info = BTUSB_WRONG_SCO_MTU },
228 { USB_DEVICE(0x413c, 0x8156), .driver_info = BTUSB_WRONG_SCO_MTU },
230 /* Belkin F8T012 and F8T013 devices */
231 { USB_DEVICE(0x050d, 0x0012), .driver_info = BTUSB_WRONG_SCO_MTU },
232 { USB_DEVICE(0x050d, 0x0013), .driver_info = BTUSB_WRONG_SCO_MTU },
234 /* Asus WL-BTD202 device */
235 { USB_DEVICE(0x0b05, 0x1715), .driver_info = BTUSB_WRONG_SCO_MTU },
237 /* Kensington Bluetooth USB adapter */
238 { USB_DEVICE(0x047d, 0x105e), .driver_info = BTUSB_WRONG_SCO_MTU },
240 /* RTX Telecom based adapters with buggy SCO support */
241 { USB_DEVICE(0x0400, 0x0807), .driver_info = BTUSB_BROKEN_ISOC },
242 { USB_DEVICE(0x0400, 0x080a), .driver_info = BTUSB_BROKEN_ISOC },
244 /* CONWISE Technology based adapters with buggy SCO support */
245 { USB_DEVICE(0x0e5e, 0x6622), .driver_info = BTUSB_BROKEN_ISOC },
247 /* Roper Class 1 Bluetooth Dongle (Silicon Wave based) */
248 { USB_DEVICE(0x1300, 0x0001), .driver_info = BTUSB_SWAVE },
250 /* Digianswer devices */
251 { USB_DEVICE(0x08fd, 0x0001), .driver_info = BTUSB_DIGIANSWER },
252 { USB_DEVICE(0x08fd, 0x0002), .driver_info = BTUSB_IGNORE },
254 /* CSR BlueCore Bluetooth Sniffer */
255 { USB_DEVICE(0x0a12, 0x0002),
256 .driver_info = BTUSB_SNIFFER | BTUSB_BROKEN_ISOC },
258 /* Frontline ComProbe Bluetooth Sniffer */
259 { USB_DEVICE(0x16d3, 0x0002),
260 .driver_info = BTUSB_SNIFFER | BTUSB_BROKEN_ISOC },
262 /* Marvell Bluetooth devices */
263 { USB_DEVICE(0x1286, 0x2044), .driver_info = BTUSB_MARVELL },
264 { USB_DEVICE(0x1286, 0x2046), .driver_info = BTUSB_MARVELL },
266 /* Intel Bluetooth device */
267 { USB_DEVICE(0x8087, 0x07dc), .driver_info = BTUSB_INTEL },
268 { USB_DEVICE(0x8087, 0x0a2a), .driver_info = BTUSB_INTEL },
269 { USB_DEVICE(0x8087, 0x0a2b), .driver_info = BTUSB_INTEL_NEW },
272 { } /* Terminating entry */
275 #define BTUSB_MAX_ISOC_FRAMES 10
277 #define BTUSB_INTR_RUNNING 0
278 #define BTUSB_BULK_RUNNING 1
279 #define BTUSB_ISOC_RUNNING 2
280 #define BTUSB_SUSPENDING 3
281 #define BTUSB_DID_ISO_RESUME 4
282 #define BTUSB_BOOTLOADER 5
283 #define BTUSB_DOWNLOADING 6
284 #define BTUSB_FIRMWARE_LOADED 7
285 #define BTUSB_FIRMWARE_FAILED 8
286 #define BTUSB_BOOTING 9
289 struct hci_dev *hdev;
290 struct usb_device *udev;
291 struct usb_interface *intf;
292 struct usb_interface *isoc;
296 struct work_struct work;
297 struct work_struct waker;
299 struct usb_anchor deferred;
300 struct usb_anchor tx_anchor;
304 struct usb_anchor intr_anchor;
305 struct usb_anchor bulk_anchor;
306 struct usb_anchor isoc_anchor;
309 struct sk_buff *evt_skb;
310 struct sk_buff *acl_skb;
311 struct sk_buff *sco_skb;
313 struct usb_endpoint_descriptor *intr_ep;
314 struct usb_endpoint_descriptor *bulk_tx_ep;
315 struct usb_endpoint_descriptor *bulk_rx_ep;
316 struct usb_endpoint_descriptor *isoc_tx_ep;
317 struct usb_endpoint_descriptor *isoc_rx_ep;
321 unsigned int sco_num;
325 int (*recv_event)(struct hci_dev *hdev, struct sk_buff *skb);
326 int (*recv_bulk)(struct btusb_data *data, void *buffer, int count);
329 static inline void btusb_free_frags(struct btusb_data *data)
333 spin_lock_irqsave(&data->rxlock, flags);
335 kfree_skb(data->evt_skb);
336 data->evt_skb = NULL;
338 kfree_skb(data->acl_skb);
339 data->acl_skb = NULL;
341 kfree_skb(data->sco_skb);
342 data->sco_skb = NULL;
344 spin_unlock_irqrestore(&data->rxlock, flags);
347 static int btusb_recv_intr(struct btusb_data *data, void *buffer, int count)
352 spin_lock(&data->rxlock);
359 skb = bt_skb_alloc(HCI_MAX_EVENT_SIZE, GFP_ATOMIC);
365 bt_cb(skb)->pkt_type = HCI_EVENT_PKT;
366 bt_cb(skb)->expect = HCI_EVENT_HDR_SIZE;
369 len = min_t(uint, bt_cb(skb)->expect, count);
370 memcpy(skb_put(skb, len), buffer, len);
374 bt_cb(skb)->expect -= len;
376 if (skb->len == HCI_EVENT_HDR_SIZE) {
377 /* Complete event header */
378 bt_cb(skb)->expect = hci_event_hdr(skb)->plen;
380 if (skb_tailroom(skb) < bt_cb(skb)->expect) {
389 if (bt_cb(skb)->expect == 0) {
391 data->recv_event(data->hdev, skb);
397 spin_unlock(&data->rxlock);
402 static int btusb_recv_bulk(struct btusb_data *data, void *buffer, int count)
407 spin_lock(&data->rxlock);
414 skb = bt_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC);
420 bt_cb(skb)->pkt_type = HCI_ACLDATA_PKT;
421 bt_cb(skb)->expect = HCI_ACL_HDR_SIZE;
424 len = min_t(uint, bt_cb(skb)->expect, count);
425 memcpy(skb_put(skb, len), buffer, len);
429 bt_cb(skb)->expect -= len;
431 if (skb->len == HCI_ACL_HDR_SIZE) {
432 __le16 dlen = hci_acl_hdr(skb)->dlen;
434 /* Complete ACL header */
435 bt_cb(skb)->expect = __le16_to_cpu(dlen);
437 if (skb_tailroom(skb) < bt_cb(skb)->expect) {
446 if (bt_cb(skb)->expect == 0) {
448 hci_recv_frame(data->hdev, skb);
454 spin_unlock(&data->rxlock);
459 static int btusb_recv_isoc(struct btusb_data *data, void *buffer, int count)
464 spin_lock(&data->rxlock);
471 skb = bt_skb_alloc(HCI_MAX_SCO_SIZE, GFP_ATOMIC);
477 bt_cb(skb)->pkt_type = HCI_SCODATA_PKT;
478 bt_cb(skb)->expect = HCI_SCO_HDR_SIZE;
481 len = min_t(uint, bt_cb(skb)->expect, count);
482 memcpy(skb_put(skb, len), buffer, len);
486 bt_cb(skb)->expect -= len;
488 if (skb->len == HCI_SCO_HDR_SIZE) {
489 /* Complete SCO header */
490 bt_cb(skb)->expect = hci_sco_hdr(skb)->dlen;
492 if (skb_tailroom(skb) < bt_cb(skb)->expect) {
501 if (bt_cb(skb)->expect == 0) {
503 hci_recv_frame(data->hdev, skb);
509 spin_unlock(&data->rxlock);
514 static void btusb_intr_complete(struct urb *urb)
516 struct hci_dev *hdev = urb->context;
517 struct btusb_data *data = hci_get_drvdata(hdev);
520 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
523 if (!test_bit(HCI_RUNNING, &hdev->flags))
526 if (urb->status == 0) {
527 hdev->stat.byte_rx += urb->actual_length;
529 if (btusb_recv_intr(data, urb->transfer_buffer,
530 urb->actual_length) < 0) {
531 BT_ERR("%s corrupted event packet", hdev->name);
534 } else if (urb->status == -ENOENT) {
535 /* Avoid suspend failed when usb_kill_urb */
539 if (!test_bit(BTUSB_INTR_RUNNING, &data->flags))
542 usb_mark_last_busy(data->udev);
543 usb_anchor_urb(urb, &data->intr_anchor);
545 err = usb_submit_urb(urb, GFP_ATOMIC);
547 /* -EPERM: urb is being killed;
548 * -ENODEV: device got disconnected */
549 if (err != -EPERM && err != -ENODEV)
550 BT_ERR("%s urb %p failed to resubmit (%d)",
551 hdev->name, urb, -err);
552 usb_unanchor_urb(urb);
556 static int btusb_submit_intr_urb(struct hci_dev *hdev, gfp_t mem_flags)
558 struct btusb_data *data = hci_get_drvdata(hdev);
564 BT_DBG("%s", hdev->name);
569 urb = usb_alloc_urb(0, mem_flags);
573 size = le16_to_cpu(data->intr_ep->wMaxPacketSize);
575 buf = kmalloc(size, mem_flags);
581 pipe = usb_rcvintpipe(data->udev, data->intr_ep->bEndpointAddress);
583 usb_fill_int_urb(urb, data->udev, pipe, buf, size,
584 btusb_intr_complete, hdev, data->intr_ep->bInterval);
586 urb->transfer_flags |= URB_FREE_BUFFER;
588 usb_anchor_urb(urb, &data->intr_anchor);
590 err = usb_submit_urb(urb, mem_flags);
592 if (err != -EPERM && err != -ENODEV)
593 BT_ERR("%s urb %p submission failed (%d)",
594 hdev->name, urb, -err);
595 usb_unanchor_urb(urb);
603 static void btusb_bulk_complete(struct urb *urb)
605 struct hci_dev *hdev = urb->context;
606 struct btusb_data *data = hci_get_drvdata(hdev);
609 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
612 if (!test_bit(HCI_RUNNING, &hdev->flags))
615 if (urb->status == 0) {
616 hdev->stat.byte_rx += urb->actual_length;
618 if (data->recv_bulk(data, urb->transfer_buffer,
619 urb->actual_length) < 0) {
620 BT_ERR("%s corrupted ACL packet", hdev->name);
623 } else if (urb->status == -ENOENT) {
624 /* Avoid suspend failed when usb_kill_urb */
628 if (!test_bit(BTUSB_BULK_RUNNING, &data->flags))
631 usb_anchor_urb(urb, &data->bulk_anchor);
632 usb_mark_last_busy(data->udev);
634 err = usb_submit_urb(urb, GFP_ATOMIC);
636 /* -EPERM: urb is being killed;
637 * -ENODEV: device got disconnected */
638 if (err != -EPERM && err != -ENODEV)
639 BT_ERR("%s urb %p failed to resubmit (%d)",
640 hdev->name, urb, -err);
641 usb_unanchor_urb(urb);
645 static int btusb_submit_bulk_urb(struct hci_dev *hdev, gfp_t mem_flags)
647 struct btusb_data *data = hci_get_drvdata(hdev);
651 int err, size = HCI_MAX_FRAME_SIZE;
653 BT_DBG("%s", hdev->name);
655 if (!data->bulk_rx_ep)
658 urb = usb_alloc_urb(0, mem_flags);
662 buf = kmalloc(size, mem_flags);
668 pipe = usb_rcvbulkpipe(data->udev, data->bulk_rx_ep->bEndpointAddress);
670 usb_fill_bulk_urb(urb, data->udev, pipe, buf, size,
671 btusb_bulk_complete, hdev);
673 urb->transfer_flags |= URB_FREE_BUFFER;
675 usb_mark_last_busy(data->udev);
676 usb_anchor_urb(urb, &data->bulk_anchor);
678 err = usb_submit_urb(urb, mem_flags);
680 if (err != -EPERM && err != -ENODEV)
681 BT_ERR("%s urb %p submission failed (%d)",
682 hdev->name, urb, -err);
683 usb_unanchor_urb(urb);
691 static void btusb_isoc_complete(struct urb *urb)
693 struct hci_dev *hdev = urb->context;
694 struct btusb_data *data = hci_get_drvdata(hdev);
697 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
700 if (!test_bit(HCI_RUNNING, &hdev->flags))
703 if (urb->status == 0) {
704 for (i = 0; i < urb->number_of_packets; i++) {
705 unsigned int offset = urb->iso_frame_desc[i].offset;
706 unsigned int length = urb->iso_frame_desc[i].actual_length;
708 if (urb->iso_frame_desc[i].status)
711 hdev->stat.byte_rx += length;
713 if (btusb_recv_isoc(data, urb->transfer_buffer + offset,
715 BT_ERR("%s corrupted SCO packet", hdev->name);
719 } else if (urb->status == -ENOENT) {
720 /* Avoid suspend failed when usb_kill_urb */
724 if (!test_bit(BTUSB_ISOC_RUNNING, &data->flags))
727 usb_anchor_urb(urb, &data->isoc_anchor);
729 err = usb_submit_urb(urb, GFP_ATOMIC);
731 /* -EPERM: urb is being killed;
732 * -ENODEV: device got disconnected */
733 if (err != -EPERM && err != -ENODEV)
734 BT_ERR("%s urb %p failed to resubmit (%d)",
735 hdev->name, urb, -err);
736 usb_unanchor_urb(urb);
740 static inline void __fill_isoc_descriptor(struct urb *urb, int len, int mtu)
744 BT_DBG("len %d mtu %d", len, mtu);
746 for (i = 0; i < BTUSB_MAX_ISOC_FRAMES && len >= mtu;
747 i++, offset += mtu, len -= mtu) {
748 urb->iso_frame_desc[i].offset = offset;
749 urb->iso_frame_desc[i].length = mtu;
752 if (len && i < BTUSB_MAX_ISOC_FRAMES) {
753 urb->iso_frame_desc[i].offset = offset;
754 urb->iso_frame_desc[i].length = len;
758 urb->number_of_packets = i;
761 static int btusb_submit_isoc_urb(struct hci_dev *hdev, gfp_t mem_flags)
763 struct btusb_data *data = hci_get_drvdata(hdev);
769 BT_DBG("%s", hdev->name);
771 if (!data->isoc_rx_ep)
774 urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, mem_flags);
778 size = le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize) *
779 BTUSB_MAX_ISOC_FRAMES;
781 buf = kmalloc(size, mem_flags);
787 pipe = usb_rcvisocpipe(data->udev, data->isoc_rx_ep->bEndpointAddress);
789 usb_fill_int_urb(urb, data->udev, pipe, buf, size, btusb_isoc_complete,
790 hdev, data->isoc_rx_ep->bInterval);
792 urb->transfer_flags = URB_FREE_BUFFER | URB_ISO_ASAP;
794 __fill_isoc_descriptor(urb, size,
795 le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize));
797 usb_anchor_urb(urb, &data->isoc_anchor);
799 err = usb_submit_urb(urb, mem_flags);
801 if (err != -EPERM && err != -ENODEV)
802 BT_ERR("%s urb %p submission failed (%d)",
803 hdev->name, urb, -err);
804 usb_unanchor_urb(urb);
812 static void btusb_tx_complete(struct urb *urb)
814 struct sk_buff *skb = urb->context;
815 struct hci_dev *hdev = (struct hci_dev *)skb->dev;
816 struct btusb_data *data = hci_get_drvdata(hdev);
818 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
821 if (!test_bit(HCI_RUNNING, &hdev->flags))
825 hdev->stat.byte_tx += urb->transfer_buffer_length;
830 spin_lock(&data->txlock);
831 data->tx_in_flight--;
832 spin_unlock(&data->txlock);
834 kfree(urb->setup_packet);
839 static void btusb_isoc_tx_complete(struct urb *urb)
841 struct sk_buff *skb = urb->context;
842 struct hci_dev *hdev = (struct hci_dev *)skb->dev;
844 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
847 if (!test_bit(HCI_RUNNING, &hdev->flags))
851 hdev->stat.byte_tx += urb->transfer_buffer_length;
856 kfree(urb->setup_packet);
861 static int btusb_open(struct hci_dev *hdev)
863 struct btusb_data *data = hci_get_drvdata(hdev);
866 BT_DBG("%s", hdev->name);
868 err = usb_autopm_get_interface(data->intf);
872 data->intf->needs_remote_wakeup = 1;
874 if (test_and_set_bit(HCI_RUNNING, &hdev->flags))
877 if (test_and_set_bit(BTUSB_INTR_RUNNING, &data->flags))
880 err = btusb_submit_intr_urb(hdev, GFP_KERNEL);
884 err = btusb_submit_bulk_urb(hdev, GFP_KERNEL);
886 usb_kill_anchored_urbs(&data->intr_anchor);
890 set_bit(BTUSB_BULK_RUNNING, &data->flags);
891 btusb_submit_bulk_urb(hdev, GFP_KERNEL);
894 usb_autopm_put_interface(data->intf);
898 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
899 clear_bit(HCI_RUNNING, &hdev->flags);
900 usb_autopm_put_interface(data->intf);
904 static void btusb_stop_traffic(struct btusb_data *data)
906 usb_kill_anchored_urbs(&data->intr_anchor);
907 usb_kill_anchored_urbs(&data->bulk_anchor);
908 usb_kill_anchored_urbs(&data->isoc_anchor);
911 static int btusb_close(struct hci_dev *hdev)
913 struct btusb_data *data = hci_get_drvdata(hdev);
916 BT_DBG("%s", hdev->name);
918 if (!test_and_clear_bit(HCI_RUNNING, &hdev->flags))
921 cancel_work_sync(&data->work);
922 cancel_work_sync(&data->waker);
924 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
925 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
926 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
928 btusb_stop_traffic(data);
929 btusb_free_frags(data);
931 err = usb_autopm_get_interface(data->intf);
935 data->intf->needs_remote_wakeup = 0;
936 usb_autopm_put_interface(data->intf);
939 usb_scuttle_anchored_urbs(&data->deferred);
943 static int btusb_flush(struct hci_dev *hdev)
945 struct btusb_data *data = hci_get_drvdata(hdev);
947 BT_DBG("%s", hdev->name);
949 usb_kill_anchored_urbs(&data->tx_anchor);
950 btusb_free_frags(data);
955 static struct urb *alloc_ctrl_urb(struct hci_dev *hdev, struct sk_buff *skb)
957 struct btusb_data *data = hci_get_drvdata(hdev);
958 struct usb_ctrlrequest *dr;
962 urb = usb_alloc_urb(0, GFP_KERNEL);
964 return ERR_PTR(-ENOMEM);
966 dr = kmalloc(sizeof(*dr), GFP_KERNEL);
969 return ERR_PTR(-ENOMEM);
972 dr->bRequestType = data->cmdreq_type;
976 dr->wLength = __cpu_to_le16(skb->len);
978 pipe = usb_sndctrlpipe(data->udev, 0x00);
980 usb_fill_control_urb(urb, data->udev, pipe, (void *)dr,
981 skb->data, skb->len, btusb_tx_complete, skb);
983 skb->dev = (void *)hdev;
988 static struct urb *alloc_bulk_urb(struct hci_dev *hdev, struct sk_buff *skb)
990 struct btusb_data *data = hci_get_drvdata(hdev);
994 if (!data->bulk_tx_ep)
995 return ERR_PTR(-ENODEV);
997 urb = usb_alloc_urb(0, GFP_KERNEL);
999 return ERR_PTR(-ENOMEM);
1001 pipe = usb_sndbulkpipe(data->udev, data->bulk_tx_ep->bEndpointAddress);
1003 usb_fill_bulk_urb(urb, data->udev, pipe,
1004 skb->data, skb->len, btusb_tx_complete, skb);
1006 skb->dev = (void *)hdev;
1011 static struct urb *alloc_isoc_urb(struct hci_dev *hdev, struct sk_buff *skb)
1013 struct btusb_data *data = hci_get_drvdata(hdev);
1017 if (!data->isoc_tx_ep)
1018 return ERR_PTR(-ENODEV);
1020 urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, GFP_KERNEL);
1022 return ERR_PTR(-ENOMEM);
1024 pipe = usb_sndisocpipe(data->udev, data->isoc_tx_ep->bEndpointAddress);
1026 usb_fill_int_urb(urb, data->udev, pipe,
1027 skb->data, skb->len, btusb_isoc_tx_complete,
1028 skb, data->isoc_tx_ep->bInterval);
1030 urb->transfer_flags = URB_ISO_ASAP;
1032 __fill_isoc_descriptor(urb, skb->len,
1033 le16_to_cpu(data->isoc_tx_ep->wMaxPacketSize));
1035 skb->dev = (void *)hdev;
1040 static int submit_tx_urb(struct hci_dev *hdev, struct urb *urb)
1042 struct btusb_data *data = hci_get_drvdata(hdev);
1045 usb_anchor_urb(urb, &data->tx_anchor);
1047 err = usb_submit_urb(urb, GFP_KERNEL);
1049 if (err != -EPERM && err != -ENODEV)
1050 BT_ERR("%s urb %p submission failed (%d)",
1051 hdev->name, urb, -err);
1052 kfree(urb->setup_packet);
1053 usb_unanchor_urb(urb);
1055 usb_mark_last_busy(data->udev);
1062 static int submit_or_queue_tx_urb(struct hci_dev *hdev, struct urb *urb)
1064 struct btusb_data *data = hci_get_drvdata(hdev);
1065 unsigned long flags;
1068 spin_lock_irqsave(&data->txlock, flags);
1069 suspending = test_bit(BTUSB_SUSPENDING, &data->flags);
1071 data->tx_in_flight++;
1072 spin_unlock_irqrestore(&data->txlock, flags);
1075 return submit_tx_urb(hdev, urb);
1077 usb_anchor_urb(urb, &data->deferred);
1078 schedule_work(&data->waker);
1084 static int btusb_send_frame(struct hci_dev *hdev, struct sk_buff *skb)
1088 BT_DBG("%s", hdev->name);
1090 if (!test_bit(HCI_RUNNING, &hdev->flags))
1093 switch (bt_cb(skb)->pkt_type) {
1094 case HCI_COMMAND_PKT:
1095 urb = alloc_ctrl_urb(hdev, skb);
1097 return PTR_ERR(urb);
1099 hdev->stat.cmd_tx++;
1100 return submit_or_queue_tx_urb(hdev, urb);
1102 case HCI_ACLDATA_PKT:
1103 urb = alloc_bulk_urb(hdev, skb);
1105 return PTR_ERR(urb);
1107 hdev->stat.acl_tx++;
1108 return submit_or_queue_tx_urb(hdev, urb);
1110 case HCI_SCODATA_PKT:
1111 if (hci_conn_num(hdev, SCO_LINK) < 1)
1114 urb = alloc_isoc_urb(hdev, skb);
1116 return PTR_ERR(urb);
1118 hdev->stat.sco_tx++;
1119 return submit_tx_urb(hdev, urb);
1125 static void btusb_notify(struct hci_dev *hdev, unsigned int evt)
1127 struct btusb_data *data = hci_get_drvdata(hdev);
1129 BT_DBG("%s evt %d", hdev->name, evt);
1131 if (hci_conn_num(hdev, SCO_LINK) != data->sco_num) {
1132 data->sco_num = hci_conn_num(hdev, SCO_LINK);
1133 schedule_work(&data->work);
1137 static inline int __set_isoc_interface(struct hci_dev *hdev, int altsetting)
1139 struct btusb_data *data = hci_get_drvdata(hdev);
1140 struct usb_interface *intf = data->isoc;
1141 struct usb_endpoint_descriptor *ep_desc;
1147 err = usb_set_interface(data->udev, 1, altsetting);
1149 BT_ERR("%s setting interface failed (%d)", hdev->name, -err);
1153 data->isoc_altsetting = altsetting;
1155 data->isoc_tx_ep = NULL;
1156 data->isoc_rx_ep = NULL;
1158 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
1159 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
1161 if (!data->isoc_tx_ep && usb_endpoint_is_isoc_out(ep_desc)) {
1162 data->isoc_tx_ep = ep_desc;
1166 if (!data->isoc_rx_ep && usb_endpoint_is_isoc_in(ep_desc)) {
1167 data->isoc_rx_ep = ep_desc;
1172 if (!data->isoc_tx_ep || !data->isoc_rx_ep) {
1173 BT_ERR("%s invalid SCO descriptors", hdev->name);
1180 static void btusb_work(struct work_struct *work)
1182 struct btusb_data *data = container_of(work, struct btusb_data, work);
1183 struct hci_dev *hdev = data->hdev;
1187 if (data->sco_num > 0) {
1188 if (!test_bit(BTUSB_DID_ISO_RESUME, &data->flags)) {
1189 err = usb_autopm_get_interface(data->isoc ? data->isoc : data->intf);
1191 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1192 usb_kill_anchored_urbs(&data->isoc_anchor);
1196 set_bit(BTUSB_DID_ISO_RESUME, &data->flags);
1199 if (hdev->voice_setting & 0x0020) {
1200 static const int alts[3] = { 2, 4, 5 };
1202 new_alts = alts[data->sco_num - 1];
1204 new_alts = data->sco_num;
1207 if (data->isoc_altsetting != new_alts) {
1208 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1209 usb_kill_anchored_urbs(&data->isoc_anchor);
1211 if (__set_isoc_interface(hdev, new_alts) < 0)
1215 if (!test_and_set_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
1216 if (btusb_submit_isoc_urb(hdev, GFP_KERNEL) < 0)
1217 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1219 btusb_submit_isoc_urb(hdev, GFP_KERNEL);
1222 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1223 usb_kill_anchored_urbs(&data->isoc_anchor);
1225 __set_isoc_interface(hdev, 0);
1226 if (test_and_clear_bit(BTUSB_DID_ISO_RESUME, &data->flags))
1227 usb_autopm_put_interface(data->isoc ? data->isoc : data->intf);
1231 static void btusb_waker(struct work_struct *work)
1233 struct btusb_data *data = container_of(work, struct btusb_data, waker);
1236 err = usb_autopm_get_interface(data->intf);
1240 usb_autopm_put_interface(data->intf);
1243 static int btusb_setup_bcm92035(struct hci_dev *hdev)
1245 struct sk_buff *skb;
1248 BT_DBG("%s", hdev->name);
1250 skb = __hci_cmd_sync(hdev, 0xfc3b, 1, &val, HCI_INIT_TIMEOUT);
1252 BT_ERR("BCM92035 command failed (%ld)", -PTR_ERR(skb));
1259 static int btusb_setup_csr(struct hci_dev *hdev)
1261 struct hci_rp_read_local_version *rp;
1262 struct sk_buff *skb;
1265 BT_DBG("%s", hdev->name);
1267 skb = __hci_cmd_sync(hdev, HCI_OP_READ_LOCAL_VERSION, 0, NULL,
1270 BT_ERR("Reading local version failed (%ld)", -PTR_ERR(skb));
1271 return -PTR_ERR(skb);
1274 rp = (struct hci_rp_read_local_version *)skb->data;
1277 if (le16_to_cpu(rp->manufacturer) != 10) {
1278 /* Clear the reset quirk since this is not an actual
1279 * early Bluetooth 1.1 device from CSR.
1281 clear_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
1283 /* These fake CSR controllers have all a broken
1284 * stored link key handling and so just disable it.
1286 set_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY,
1291 ret = -bt_to_errno(rp->status);
1298 struct intel_version {
1311 struct intel_boot_params {
1323 bdaddr_t otp_bdaddr;
1324 __u8 min_fw_build_nn;
1325 __u8 min_fw_build_cw;
1326 __u8 min_fw_build_yy;
1328 __u8 unlocked_state;
1331 static const struct firmware *btusb_setup_intel_get_fw(struct hci_dev *hdev,
1332 struct intel_version *ver)
1334 const struct firmware *fw;
1338 snprintf(fwname, sizeof(fwname),
1339 "intel/ibt-hw-%x.%x.%x-fw-%x.%x.%x.%x.%x.bseq",
1340 ver->hw_platform, ver->hw_variant, ver->hw_revision,
1341 ver->fw_variant, ver->fw_revision, ver->fw_build_num,
1342 ver->fw_build_ww, ver->fw_build_yy);
1344 ret = request_firmware(&fw, fwname, &hdev->dev);
1346 if (ret == -EINVAL) {
1347 BT_ERR("%s Intel firmware file request failed (%d)",
1352 BT_ERR("%s failed to open Intel firmware file: %s(%d)",
1353 hdev->name, fwname, ret);
1355 /* If the correct firmware patch file is not found, use the
1356 * default firmware patch file instead
1358 snprintf(fwname, sizeof(fwname), "intel/ibt-hw-%x.%x.bseq",
1359 ver->hw_platform, ver->hw_variant);
1360 if (request_firmware(&fw, fwname, &hdev->dev) < 0) {
1361 BT_ERR("%s failed to open default Intel fw file: %s",
1362 hdev->name, fwname);
1367 BT_INFO("%s: Intel Bluetooth firmware file: %s", hdev->name, fwname);
1372 static int btusb_setup_intel_patching(struct hci_dev *hdev,
1373 const struct firmware *fw,
1374 const u8 **fw_ptr, int *disable_patch)
1376 struct sk_buff *skb;
1377 struct hci_command_hdr *cmd;
1378 const u8 *cmd_param;
1379 struct hci_event_hdr *evt = NULL;
1380 const u8 *evt_param = NULL;
1381 int remain = fw->size - (*fw_ptr - fw->data);
1383 /* The first byte indicates the types of the patch command or event.
1384 * 0x01 means HCI command and 0x02 is HCI event. If the first bytes
1385 * in the current firmware buffer doesn't start with 0x01 or
1386 * the size of remain buffer is smaller than HCI command header,
1387 * the firmware file is corrupted and it should stop the patching
1390 if (remain > HCI_COMMAND_HDR_SIZE && *fw_ptr[0] != 0x01) {
1391 BT_ERR("%s Intel fw corrupted: invalid cmd read", hdev->name);
1397 cmd = (struct hci_command_hdr *)(*fw_ptr);
1398 *fw_ptr += sizeof(*cmd);
1399 remain -= sizeof(*cmd);
1401 /* Ensure that the remain firmware data is long enough than the length
1402 * of command parameter. If not, the firmware file is corrupted.
1404 if (remain < cmd->plen) {
1405 BT_ERR("%s Intel fw corrupted: invalid cmd len", hdev->name);
1409 /* If there is a command that loads a patch in the firmware
1410 * file, then enable the patch upon success, otherwise just
1411 * disable the manufacturer mode, for example patch activation
1412 * is not required when the default firmware patch file is used
1413 * because there are no patch data to load.
1415 if (*disable_patch && le16_to_cpu(cmd->opcode) == 0xfc8e)
1418 cmd_param = *fw_ptr;
1419 *fw_ptr += cmd->plen;
1420 remain -= cmd->plen;
1422 /* This reads the expected events when the above command is sent to the
1423 * device. Some vendor commands expects more than one events, for
1424 * example command status event followed by vendor specific event.
1425 * For this case, it only keeps the last expected event. so the command
1426 * can be sent with __hci_cmd_sync_ev() which returns the sk_buff of
1427 * last expected event.
1429 while (remain > HCI_EVENT_HDR_SIZE && *fw_ptr[0] == 0x02) {
1433 evt = (struct hci_event_hdr *)(*fw_ptr);
1434 *fw_ptr += sizeof(*evt);
1435 remain -= sizeof(*evt);
1437 if (remain < evt->plen) {
1438 BT_ERR("%s Intel fw corrupted: invalid evt len",
1443 evt_param = *fw_ptr;
1444 *fw_ptr += evt->plen;
1445 remain -= evt->plen;
1448 /* Every HCI commands in the firmware file has its correspond event.
1449 * If event is not found or remain is smaller than zero, the firmware
1450 * file is corrupted.
1452 if (!evt || !evt_param || remain < 0) {
1453 BT_ERR("%s Intel fw corrupted: invalid evt read", hdev->name);
1457 skb = __hci_cmd_sync_ev(hdev, le16_to_cpu(cmd->opcode), cmd->plen,
1458 cmd_param, evt->evt, HCI_INIT_TIMEOUT);
1460 BT_ERR("%s sending Intel patch command (0x%4.4x) failed (%ld)",
1461 hdev->name, cmd->opcode, PTR_ERR(skb));
1462 return PTR_ERR(skb);
1465 /* It ensures that the returned event matches the event data read from
1466 * the firmware file. At fist, it checks the length and then
1467 * the contents of the event.
1469 if (skb->len != evt->plen) {
1470 BT_ERR("%s mismatch event length (opcode 0x%4.4x)", hdev->name,
1471 le16_to_cpu(cmd->opcode));
1476 if (memcmp(skb->data, evt_param, evt->plen)) {
1477 BT_ERR("%s mismatch event parameter (opcode 0x%4.4x)",
1478 hdev->name, le16_to_cpu(cmd->opcode));
1487 #define BDADDR_INTEL (&(bdaddr_t) {{0x00, 0x8b, 0x9e, 0x19, 0x03, 0x00}})
1489 static int btusb_check_bdaddr_intel(struct hci_dev *hdev)
1491 struct sk_buff *skb;
1492 struct hci_rp_read_bd_addr *rp;
1494 skb = __hci_cmd_sync(hdev, HCI_OP_READ_BD_ADDR, 0, NULL,
1497 BT_ERR("%s reading Intel device address failed (%ld)",
1498 hdev->name, PTR_ERR(skb));
1499 return PTR_ERR(skb);
1502 if (skb->len != sizeof(*rp)) {
1503 BT_ERR("%s Intel device address length mismatch", hdev->name);
1508 rp = (struct hci_rp_read_bd_addr *)skb->data;
1510 BT_ERR("%s Intel device address result failed (%02x)",
1511 hdev->name, rp->status);
1513 return -bt_to_errno(rp->status);
1516 /* For some Intel based controllers, the default Bluetooth device
1517 * address 00:03:19:9E:8B:00 can be found. These controllers are
1518 * fully operational, but have the danger of duplicate addresses
1519 * and that in turn can cause problems with Bluetooth operation.
1521 if (!bacmp(&rp->bdaddr, BDADDR_INTEL)) {
1522 BT_ERR("%s found Intel default device address (%pMR)",
1523 hdev->name, &rp->bdaddr);
1524 set_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks);
1532 static int btusb_setup_intel(struct hci_dev *hdev)
1534 struct sk_buff *skb;
1535 const struct firmware *fw;
1538 struct intel_version *ver;
1540 const u8 mfg_enable[] = { 0x01, 0x00 };
1541 const u8 mfg_disable[] = { 0x00, 0x00 };
1542 const u8 mfg_reset_deactivate[] = { 0x00, 0x01 };
1543 const u8 mfg_reset_activate[] = { 0x00, 0x02 };
1545 BT_DBG("%s", hdev->name);
1547 /* The controller has a bug with the first HCI command sent to it
1548 * returning number of completed commands as zero. This would stall the
1549 * command processing in the Bluetooth core.
1551 * As a workaround, send HCI Reset command first which will reset the
1552 * number of completed commands and allow normal command processing
1555 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
1557 BT_ERR("%s sending initial HCI reset command failed (%ld)",
1558 hdev->name, PTR_ERR(skb));
1559 return PTR_ERR(skb);
1563 /* Read Intel specific controller version first to allow selection of
1564 * which firmware file to load.
1566 * The returned information are hardware variant and revision plus
1567 * firmware variant, revision and build number.
1569 skb = __hci_cmd_sync(hdev, 0xfc05, 0, NULL, HCI_INIT_TIMEOUT);
1571 BT_ERR("%s reading Intel fw version command failed (%ld)",
1572 hdev->name, PTR_ERR(skb));
1573 return PTR_ERR(skb);
1576 if (skb->len != sizeof(*ver)) {
1577 BT_ERR("%s Intel version event length mismatch", hdev->name);
1582 ver = (struct intel_version *)skb->data;
1584 BT_ERR("%s Intel fw version event failed (%02x)", hdev->name,
1587 return -bt_to_errno(ver->status);
1590 BT_INFO("%s: read Intel version: %02x%02x%02x%02x%02x%02x%02x%02x%02x",
1591 hdev->name, ver->hw_platform, ver->hw_variant,
1592 ver->hw_revision, ver->fw_variant, ver->fw_revision,
1593 ver->fw_build_num, ver->fw_build_ww, ver->fw_build_yy,
1596 /* fw_patch_num indicates the version of patch the device currently
1597 * have. If there is no patch data in the device, it is always 0x00.
1598 * So, if it is other than 0x00, no need to patch the deivce again.
1600 if (ver->fw_patch_num) {
1601 BT_INFO("%s: Intel device is already patched. patch num: %02x",
1602 hdev->name, ver->fw_patch_num);
1604 btusb_check_bdaddr_intel(hdev);
1608 /* Opens the firmware patch file based on the firmware version read
1609 * from the controller. If it fails to open the matching firmware
1610 * patch file, it tries to open the default firmware patch file.
1611 * If no patch file is found, allow the device to operate without
1614 fw = btusb_setup_intel_get_fw(hdev, ver);
1617 btusb_check_bdaddr_intel(hdev);
1622 /* This Intel specific command enables the manufacturer mode of the
1625 * Only while this mode is enabled, the driver can download the
1626 * firmware patch data and configuration parameters.
1628 skb = __hci_cmd_sync(hdev, 0xfc11, 2, mfg_enable, HCI_INIT_TIMEOUT);
1630 BT_ERR("%s entering Intel manufacturer mode failed (%ld)",
1631 hdev->name, PTR_ERR(skb));
1632 release_firmware(fw);
1633 return PTR_ERR(skb);
1637 u8 evt_status = skb->data[0];
1639 BT_ERR("%s enable Intel manufacturer mode event failed (%02x)",
1640 hdev->name, evt_status);
1642 release_firmware(fw);
1643 return -bt_to_errno(evt_status);
1649 /* The firmware data file consists of list of Intel specific HCI
1650 * commands and its expected events. The first byte indicates the
1651 * type of the message, either HCI command or HCI event.
1653 * It reads the command and its expected event from the firmware file,
1654 * and send to the controller. Once __hci_cmd_sync_ev() returns,
1655 * the returned event is compared with the event read from the firmware
1656 * file and it will continue until all the messages are downloaded to
1659 * Once the firmware patching is completed successfully,
1660 * the manufacturer mode is disabled with reset and activating the
1663 * If the firmware patching fails, the manufacturer mode is
1664 * disabled with reset and deactivating the patch.
1666 * If the default patch file is used, no reset is done when disabling
1669 while (fw->size > fw_ptr - fw->data) {
1672 ret = btusb_setup_intel_patching(hdev, fw, &fw_ptr,
1675 goto exit_mfg_deactivate;
1678 release_firmware(fw);
1681 goto exit_mfg_disable;
1683 /* Patching completed successfully and disable the manufacturer mode
1684 * with reset and activate the downloaded firmware patches.
1686 skb = __hci_cmd_sync(hdev, 0xfc11, sizeof(mfg_reset_activate),
1687 mfg_reset_activate, HCI_INIT_TIMEOUT);
1689 BT_ERR("%s exiting Intel manufacturer mode failed (%ld)",
1690 hdev->name, PTR_ERR(skb));
1691 return PTR_ERR(skb);
1695 BT_INFO("%s: Intel Bluetooth firmware patch completed and activated",
1698 btusb_check_bdaddr_intel(hdev);
1702 /* Disable the manufacturer mode without reset */
1703 skb = __hci_cmd_sync(hdev, 0xfc11, sizeof(mfg_disable), mfg_disable,
1706 BT_ERR("%s exiting Intel manufacturer mode failed (%ld)",
1707 hdev->name, PTR_ERR(skb));
1708 return PTR_ERR(skb);
1712 BT_INFO("%s: Intel Bluetooth firmware patch completed", hdev->name);
1714 btusb_check_bdaddr_intel(hdev);
1717 exit_mfg_deactivate:
1718 release_firmware(fw);
1720 /* Patching failed. Disable the manufacturer mode with reset and
1721 * deactivate the downloaded firmware patches.
1723 skb = __hci_cmd_sync(hdev, 0xfc11, sizeof(mfg_reset_deactivate),
1724 mfg_reset_deactivate, HCI_INIT_TIMEOUT);
1726 BT_ERR("%s exiting Intel manufacturer mode failed (%ld)",
1727 hdev->name, PTR_ERR(skb));
1728 return PTR_ERR(skb);
1732 BT_INFO("%s: Intel Bluetooth firmware patch completed and deactivated",
1735 btusb_check_bdaddr_intel(hdev);
1739 static int inject_cmd_complete(struct hci_dev *hdev, __u16 opcode)
1741 struct sk_buff *skb;
1742 struct hci_event_hdr *hdr;
1743 struct hci_ev_cmd_complete *evt;
1745 skb = bt_skb_alloc(sizeof(*hdr) + sizeof(*evt) + 1, GFP_ATOMIC);
1749 hdr = (struct hci_event_hdr *)skb_put(skb, sizeof(*hdr));
1750 hdr->evt = HCI_EV_CMD_COMPLETE;
1751 hdr->plen = sizeof(*evt) + 1;
1753 evt = (struct hci_ev_cmd_complete *)skb_put(skb, sizeof(*evt));
1755 evt->opcode = cpu_to_le16(opcode);
1757 *skb_put(skb, 1) = 0x00;
1759 bt_cb(skb)->pkt_type = HCI_EVENT_PKT;
1761 return hci_recv_frame(hdev, skb);
1764 static int btusb_recv_bulk_intel(struct btusb_data *data, void *buffer,
1767 /* When the device is in bootloader mode, then it can send
1768 * events via the bulk endpoint. These events are treated the
1769 * same way as the ones received from the interrupt endpoint.
1771 if (test_bit(BTUSB_BOOTLOADER, &data->flags))
1772 return btusb_recv_intr(data, buffer, count);
1774 return btusb_recv_bulk(data, buffer, count);
1777 static int btusb_recv_event_intel(struct hci_dev *hdev, struct sk_buff *skb)
1779 struct btusb_data *data = hci_get_drvdata(hdev);
1781 if (test_bit(BTUSB_BOOTLOADER, &data->flags)) {
1782 struct hci_event_hdr *hdr = (void *)skb->data;
1784 /* When the firmware loading completes the device sends
1785 * out a vendor specific event indicating the result of
1786 * the firmware loading.
1788 if (skb->len == 7 && hdr->evt == 0xff && hdr->plen == 0x05 &&
1789 skb->data[2] == 0x06) {
1790 if (skb->data[3] != 0x00)
1791 test_bit(BTUSB_FIRMWARE_FAILED, &data->flags);
1793 if (test_and_clear_bit(BTUSB_DOWNLOADING,
1795 test_bit(BTUSB_FIRMWARE_LOADED, &data->flags))
1796 wake_up_interruptible(&hdev->req_wait_q);
1799 /* When switching to the operational firmware the device
1800 * sends a vendor specific event indicating that the bootup
1803 if (skb->len == 9 && hdr->evt == 0xff && hdr->plen == 0x07 &&
1804 skb->data[2] == 0x02) {
1805 if (test_and_clear_bit(BTUSB_BOOTING, &data->flags))
1806 wake_up_interruptible(&hdev->req_wait_q);
1810 return hci_recv_frame(hdev, skb);
1813 static int btusb_send_frame_intel(struct hci_dev *hdev, struct sk_buff *skb)
1815 struct btusb_data *data = hci_get_drvdata(hdev);
1818 BT_DBG("%s", hdev->name);
1820 if (!test_bit(HCI_RUNNING, &hdev->flags))
1823 switch (bt_cb(skb)->pkt_type) {
1824 case HCI_COMMAND_PKT:
1825 if (test_bit(BTUSB_BOOTLOADER, &data->flags)) {
1826 struct hci_command_hdr *cmd = (void *)skb->data;
1827 __u16 opcode = le16_to_cpu(cmd->opcode);
1829 /* When in bootloader mode and the command 0xfc09
1830 * is received, it needs to be send down the
1831 * bulk endpoint. So allocate a bulk URB instead.
1833 if (opcode == 0xfc09)
1834 urb = alloc_bulk_urb(hdev, skb);
1836 urb = alloc_ctrl_urb(hdev, skb);
1838 /* When the 0xfc01 command is issued to boot into
1839 * the operational firmware, it will actually not
1840 * send a command complete event. To keep the flow
1841 * control working inject that event here.
1843 if (opcode == 0xfc01)
1844 inject_cmd_complete(hdev, opcode);
1846 urb = alloc_ctrl_urb(hdev, skb);
1849 return PTR_ERR(urb);
1851 hdev->stat.cmd_tx++;
1852 return submit_or_queue_tx_urb(hdev, urb);
1854 case HCI_ACLDATA_PKT:
1855 urb = alloc_bulk_urb(hdev, skb);
1857 return PTR_ERR(urb);
1859 hdev->stat.acl_tx++;
1860 return submit_or_queue_tx_urb(hdev, urb);
1862 case HCI_SCODATA_PKT:
1863 if (hci_conn_num(hdev, SCO_LINK) < 1)
1866 urb = alloc_isoc_urb(hdev, skb);
1868 return PTR_ERR(urb);
1870 hdev->stat.sco_tx++;
1871 return submit_tx_urb(hdev, urb);
1877 static int btusb_intel_secure_send(struct hci_dev *hdev, u8 fragment_type,
1878 u32 plen, const void *param)
1881 struct sk_buff *skb;
1882 u8 cmd_param[253], fragment_len = (plen > 252) ? 252 : plen;
1884 cmd_param[0] = fragment_type;
1885 memcpy(cmd_param + 1, param, fragment_len);
1887 skb = __hci_cmd_sync(hdev, 0xfc09, fragment_len + 1,
1888 cmd_param, HCI_INIT_TIMEOUT);
1890 return PTR_ERR(skb);
1894 plen -= fragment_len;
1895 param += fragment_len;
1901 static void btusb_intel_version_info(struct hci_dev *hdev,
1902 struct intel_version *ver)
1904 const char *variant;
1906 switch (ver->fw_variant) {
1908 variant = "Bootloader";
1911 variant = "Firmware";
1917 BT_INFO("%s: %s revision %u.%u build %u week %u %u", hdev->name,
1918 variant, ver->fw_revision >> 4, ver->fw_revision & 0x0f,
1919 ver->fw_build_num, ver->fw_build_ww, 2000 + ver->fw_build_yy);
1922 static int btusb_setup_intel_new(struct hci_dev *hdev)
1924 static const u8 reset_param[] = { 0x00, 0x01, 0x00, 0x01,
1925 0x00, 0x08, 0x04, 0x00 };
1926 struct btusb_data *data = hci_get_drvdata(hdev);
1927 struct sk_buff *skb;
1928 struct intel_version *ver;
1929 struct intel_boot_params *params;
1930 const struct firmware *fw;
1933 ktime_t calltime, delta, rettime;
1934 unsigned long long duration;
1937 BT_DBG("%s", hdev->name);
1939 calltime = ktime_get();
1941 /* Read the Intel version information to determine if the device
1942 * is in bootloader mode or if it already has operational firmware
1945 skb = __hci_cmd_sync(hdev, 0xfc05, 0, NULL, HCI_INIT_TIMEOUT);
1947 BT_ERR("%s: Reading Intel version information failed (%ld)",
1948 hdev->name, PTR_ERR(skb));
1949 return PTR_ERR(skb);
1952 if (skb->len != sizeof(*ver)) {
1953 BT_ERR("%s: Intel version event size mismatch", hdev->name);
1958 ver = (struct intel_version *)skb->data;
1960 BT_ERR("%s: Intel version command failure (%02x)",
1961 hdev->name, ver->status);
1962 err = -bt_to_errno(ver->status);
1967 /* The hardware platform number has a fixed value of 0x37 and
1968 * for now only accept this single value.
1970 if (ver->hw_platform != 0x37) {
1971 BT_ERR("%s: Unsupported Intel hardware platform (%u)",
1972 hdev->name, ver->hw_platform);
1977 /* At the moment only the hardware variant iBT 3.0 (LnP/SfP) is
1978 * supported by this firmware loading method. This check has been
1979 * put in place to ensure correct forward compatibility options
1980 * when newer hardware variants come along.
1982 if (ver->hw_variant != 0x0b) {
1983 BT_ERR("%s: Unsupported Intel hardware variant (%u)",
1984 hdev->name, ver->hw_variant);
1989 btusb_intel_version_info(hdev, ver);
1991 /* The firmware variant determines if the device is in bootloader
1992 * mode or is running operational firmware. The value 0x06 identifies
1993 * the bootloader and the value 0x23 identifies the operational
1996 * When the operational firmware is already present, then only
1997 * the check for valid Bluetooth device address is needed. This
1998 * determines if the device will be added as configured or
1999 * unconfigured controller.
2001 * It is not possible to use the Secure Boot Parameters in this
2002 * case since that command is only available in bootloader mode.
2004 if (ver->fw_variant == 0x23) {
2006 clear_bit(BTUSB_BOOTLOADER, &data->flags);
2007 btusb_check_bdaddr_intel(hdev);
2011 /* If the device is not in bootloader mode, then the only possible
2012 * choice is to return an error and abort the device initialization.
2014 if (ver->fw_variant != 0x06) {
2015 BT_ERR("%s: Unsupported Intel firmware variant (%u)",
2016 hdev->name, ver->fw_variant);
2023 /* Read the secure boot parameters to identify the operating
2024 * details of the bootloader.
2026 skb = __hci_cmd_sync(hdev, 0xfc0d, 0, NULL, HCI_INIT_TIMEOUT);
2028 BT_ERR("%s: Reading Intel boot parameters failed (%ld)",
2029 hdev->name, PTR_ERR(skb));
2030 return PTR_ERR(skb);
2033 if (skb->len != sizeof(*params)) {
2034 BT_ERR("%s: Intel boot parameters size mismatch", hdev->name);
2039 params = (struct intel_boot_params *)skb->data;
2040 if (params->status) {
2041 BT_ERR("%s: Intel boot parameters command failure (%02x)",
2042 hdev->name, params->status);
2043 err = -bt_to_errno(params->status);
2048 BT_INFO("%s: Device revision is %u", hdev->name,
2049 le16_to_cpu(params->dev_revid));
2051 BT_INFO("%s: Secure boot is %s", hdev->name,
2052 params->secure_boot ? "enabled" : "disabled");
2054 BT_INFO("%s: Minimum firmware build %u week %u %u", hdev->name,
2055 params->min_fw_build_nn, params->min_fw_build_cw,
2056 2000 + params->min_fw_build_yy);
2058 /* It is required that every single firmware fragment is acknowledged
2059 * with a command complete event. If the boot parameters indicate
2060 * that this bootloader does not send them, then abort the setup.
2062 if (params->limited_cce != 0x00) {
2063 BT_ERR("%s: Unsupported Intel firmware loading method (%u)",
2064 hdev->name, params->limited_cce);
2069 /* If the OTP has no valid Bluetooth device address, then there will
2070 * also be no valid address for the operational firmware.
2072 if (!bacmp(¶ms->otp_bdaddr, BDADDR_ANY)) {
2073 BT_INFO("%s: No device address configured", hdev->name);
2074 set_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks);
2077 /* With this Intel bootloader only the hardware variant and device
2078 * revision information are used to select the right firmware.
2080 * Currently this bootloader support is limited to hardware variant
2081 * iBT 3.0 (LnP/SfP) which is identified by the value 11 (0x0b).
2083 snprintf(fwname, sizeof(fwname), "intel/ibt-11-%u.sfi",
2084 le16_to_cpu(params->dev_revid));
2086 err = request_firmware(&fw, fwname, &hdev->dev);
2088 BT_ERR("%s: Failed to load Intel firmware file (%d)",
2094 BT_INFO("%s: Found device firmware: %s", hdev->name, fwname);
2098 if (fw->size < 644) {
2099 BT_ERR("%s: Invalid size of firmware file (%zu)",
2100 hdev->name, fw->size);
2105 set_bit(BTUSB_DOWNLOADING, &data->flags);
2107 /* Start the firmware download transaction with the Init fragment
2108 * represented by the 128 bytes of CSS header.
2110 err = btusb_intel_secure_send(hdev, 0x00, 128, fw->data);
2112 BT_ERR("%s: Failed to send firmware header (%d)",
2117 /* Send the 256 bytes of public key information from the firmware
2118 * as the PKey fragment.
2120 err = btusb_intel_secure_send(hdev, 0x03, 256, fw->data + 128);
2122 BT_ERR("%s: Failed to send firmware public key (%d)",
2127 /* Send the 256 bytes of signature information from the firmware
2128 * as the Sign fragment.
2130 err = btusb_intel_secure_send(hdev, 0x02, 256, fw->data + 388);
2132 BT_ERR("%s: Failed to send firmware signature (%d)",
2137 fw_ptr = fw->data + 644;
2139 while (fw_ptr - fw->data < fw->size) {
2140 struct hci_command_hdr *cmd = (void *)fw_ptr;
2143 cmd_len = sizeof(*cmd) + cmd->plen;
2145 /* Send each command from the firmware data buffer as
2146 * a single Data fragment.
2148 err = btusb_intel_secure_send(hdev, 0x01, cmd_len, fw_ptr);
2150 BT_ERR("%s: Failed to send firmware data (%d)",
2158 set_bit(BTUSB_FIRMWARE_LOADED, &data->flags);
2160 /* Before switching the device into operational mode and with that
2161 * booting the loaded firmware, wait for the bootloader notification
2162 * that all fragments have been successfully received.
2164 * When the event processing receives the notification, then this
2165 * flag will be cleared. So just in case that happens really quickly,
2166 * check it first before adding the wait queue.
2168 if (test_bit(BTUSB_DOWNLOADING, &data->flags)) {
2169 DECLARE_WAITQUEUE(wait, current);
2170 signed long timeout;
2172 BT_INFO("%s: Waiting for firmware download to complete",
2175 add_wait_queue(&hdev->req_wait_q, &wait);
2176 set_current_state(TASK_INTERRUPTIBLE);
2178 /* The firmware loading should not take longer than 5 seconds
2179 * and thus just timeout if that happens and fail the setup
2182 timeout = schedule_timeout(msecs_to_jiffies(5000));
2184 remove_wait_queue(&hdev->req_wait_q, &wait);
2186 if (signal_pending(current)) {
2187 BT_ERR("%s: Firmware loading interrupted", hdev->name);
2193 BT_ERR("%s: Firmware loading timeout", hdev->name);
2199 if (test_bit(BTUSB_FIRMWARE_FAILED, &data->flags)) {
2200 BT_ERR("%s: Firmware loading failed", hdev->name);
2205 rettime = ktime_get();
2206 delta = ktime_sub(rettime, calltime);
2207 duration = (unsigned long long) ktime_to_ns(delta) >> 10;
2209 BT_INFO("%s: Firmware loaded in %llu usecs", hdev->name, duration);
2212 release_firmware(fw);
2217 calltime = ktime_get();
2219 set_bit(BTUSB_BOOTING, &data->flags);
2221 skb = __hci_cmd_sync(hdev, 0xfc01, sizeof(reset_param), reset_param,
2224 return PTR_ERR(skb);
2228 /* The bootloader will not indicate when the device is ready. This
2229 * is done by the operational firmware sending bootup notification.
2231 if (test_bit(BTUSB_BOOTING, &data->flags)) {
2232 DECLARE_WAITQUEUE(wait, current);
2233 signed long timeout;
2235 BT_INFO("%s: Waiting for device to boot", hdev->name);
2237 add_wait_queue(&hdev->req_wait_q, &wait);
2238 set_current_state(TASK_INTERRUPTIBLE);
2240 /* Booting into operational firmware should not take
2241 * longer than 1 second. However if that happens, then
2242 * just fail the setup since something went wrong.
2244 timeout = schedule_timeout(msecs_to_jiffies(1000));
2246 remove_wait_queue(&hdev->req_wait_q, &wait);
2248 if (signal_pending(current)) {
2249 BT_ERR("%s: Device boot interrupted", hdev->name);
2254 BT_ERR("%s: Device boot timeout", hdev->name);
2259 rettime = ktime_get();
2260 delta = ktime_sub(rettime, calltime);
2261 duration = (unsigned long long) ktime_to_ns(delta) >> 10;
2263 BT_INFO("%s: Device booted in %llu usecs", hdev->name, duration);
2265 clear_bit(BTUSB_BOOTLOADER, &data->flags);
2270 static void btusb_hw_error_intel(struct hci_dev *hdev, u8 code)
2272 struct sk_buff *skb;
2275 BT_ERR("%s: Hardware error 0x%2.2x", hdev->name, code);
2277 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
2279 BT_ERR("%s: Reset after hardware error failed (%ld)",
2280 hdev->name, PTR_ERR(skb));
2285 skb = __hci_cmd_sync(hdev, 0xfc22, 1, &type, HCI_INIT_TIMEOUT);
2287 BT_ERR("%s: Retrieving Intel exception info failed (%ld)",
2288 hdev->name, PTR_ERR(skb));
2292 if (skb->len != 13) {
2293 BT_ERR("%s: Exception info size mismatch", hdev->name);
2298 if (skb->data[0] != 0x00) {
2299 BT_ERR("%s: Exception info command failure (%02x)",
2300 hdev->name, skb->data[0]);
2305 BT_ERR("%s: Exception info %s", hdev->name, (char *)(skb->data + 1));
2310 static int btusb_set_bdaddr_intel(struct hci_dev *hdev, const bdaddr_t *bdaddr)
2312 struct sk_buff *skb;
2315 skb = __hci_cmd_sync(hdev, 0xfc31, 6, bdaddr, HCI_INIT_TIMEOUT);
2318 BT_ERR("%s: changing Intel device address failed (%ld)",
2327 static int btusb_set_bdaddr_marvell(struct hci_dev *hdev,
2328 const bdaddr_t *bdaddr)
2330 struct sk_buff *skb;
2335 buf[1] = sizeof(bdaddr_t);
2336 memcpy(buf + 2, bdaddr, sizeof(bdaddr_t));
2338 skb = __hci_cmd_sync(hdev, 0xfc22, sizeof(buf), buf, HCI_INIT_TIMEOUT);
2341 BT_ERR("%s: changing Marvell device address failed (%ld)",
2350 #define BDADDR_BCM20702A0 (&(bdaddr_t) {{0x00, 0xa0, 0x02, 0x70, 0x20, 0x00}})
2352 static int btusb_setup_bcm_patchram(struct hci_dev *hdev)
2354 struct btusb_data *data = hci_get_drvdata(hdev);
2355 struct usb_device *udev = data->udev;
2357 const struct firmware *fw;
2360 const struct hci_command_hdr *cmd;
2361 const u8 *cmd_param;
2363 struct sk_buff *skb;
2364 struct hci_rp_read_local_version *ver;
2365 struct hci_rp_read_bd_addr *bda;
2368 snprintf(fw_name, sizeof(fw_name), "brcm/%s-%04x-%04x.hcd",
2369 udev->product ? udev->product : "BCM",
2370 le16_to_cpu(udev->descriptor.idVendor),
2371 le16_to_cpu(udev->descriptor.idProduct));
2373 ret = request_firmware(&fw, fw_name, &hdev->dev);
2375 BT_INFO("%s: BCM: patch %s not found", hdev->name, fw_name);
2380 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
2383 BT_ERR("%s: HCI_OP_RESET failed (%ld)", hdev->name, ret);
2388 /* Read Local Version Info */
2389 skb = __hci_cmd_sync(hdev, HCI_OP_READ_LOCAL_VERSION, 0, NULL,
2393 BT_ERR("%s: HCI_OP_READ_LOCAL_VERSION failed (%ld)",
2398 if (skb->len != sizeof(*ver)) {
2399 BT_ERR("%s: HCI_OP_READ_LOCAL_VERSION event length mismatch",
2406 ver = (struct hci_rp_read_local_version *)skb->data;
2407 BT_INFO("%s: BCM: patching hci_ver=%02x hci_rev=%04x lmp_ver=%02x "
2408 "lmp_subver=%04x", hdev->name, ver->hci_ver, ver->hci_rev,
2409 ver->lmp_ver, ver->lmp_subver);
2412 /* Start Download */
2413 skb = __hci_cmd_sync(hdev, 0xfc2e, 0, NULL, HCI_INIT_TIMEOUT);
2416 BT_ERR("%s: BCM: Download Minidrv command failed (%ld)",
2422 /* 50 msec delay after Download Minidrv completes */
2428 while (fw_size >= sizeof(*cmd)) {
2429 cmd = (struct hci_command_hdr *)fw_ptr;
2430 fw_ptr += sizeof(*cmd);
2431 fw_size -= sizeof(*cmd);
2433 if (fw_size < cmd->plen) {
2434 BT_ERR("%s: BCM: patch %s is corrupted",
2435 hdev->name, fw_name);
2441 fw_ptr += cmd->plen;
2442 fw_size -= cmd->plen;
2444 opcode = le16_to_cpu(cmd->opcode);
2446 skb = __hci_cmd_sync(hdev, opcode, cmd->plen, cmd_param,
2450 BT_ERR("%s: BCM: patch command %04x failed (%ld)",
2451 hdev->name, opcode, ret);
2457 /* 250 msec delay after Launch Ram completes */
2462 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
2465 BT_ERR("%s: HCI_OP_RESET failed (%ld)", hdev->name, ret);
2470 /* Read Local Version Info */
2471 skb = __hci_cmd_sync(hdev, HCI_OP_READ_LOCAL_VERSION, 0, NULL,
2475 BT_ERR("%s: HCI_OP_READ_LOCAL_VERSION failed (%ld)",
2480 if (skb->len != sizeof(*ver)) {
2481 BT_ERR("%s: HCI_OP_READ_LOCAL_VERSION event length mismatch",
2488 ver = (struct hci_rp_read_local_version *)skb->data;
2489 BT_INFO("%s: BCM: firmware hci_ver=%02x hci_rev=%04x lmp_ver=%02x "
2490 "lmp_subver=%04x", hdev->name, ver->hci_ver, ver->hci_rev,
2491 ver->lmp_ver, ver->lmp_subver);
2494 /* Read BD Address */
2495 skb = __hci_cmd_sync(hdev, HCI_OP_READ_BD_ADDR, 0, NULL,
2499 BT_ERR("%s: HCI_OP_READ_BD_ADDR failed (%ld)",
2504 if (skb->len != sizeof(*bda)) {
2505 BT_ERR("%s: HCI_OP_READ_BD_ADDR event length mismatch",
2512 bda = (struct hci_rp_read_bd_addr *)skb->data;
2514 BT_ERR("%s: HCI_OP_READ_BD_ADDR error status (%02x)",
2515 hdev->name, bda->status);
2517 ret = -bt_to_errno(bda->status);
2521 /* The address 00:20:70:02:A0:00 indicates a BCM20702A0 controller
2522 * with no configured address.
2524 if (!bacmp(&bda->bdaddr, BDADDR_BCM20702A0)) {
2525 BT_INFO("%s: BCM: using default device address (%pMR)",
2526 hdev->name, &bda->bdaddr);
2527 set_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks);
2533 release_firmware(fw);
2538 static int btusb_set_bdaddr_bcm(struct hci_dev *hdev, const bdaddr_t *bdaddr)
2540 struct sk_buff *skb;
2543 skb = __hci_cmd_sync(hdev, 0xfc01, 6, bdaddr, HCI_INIT_TIMEOUT);
2546 BT_ERR("%s: BCM: Change address command failed (%ld)",
2555 static int btusb_set_bdaddr_ath3012(struct hci_dev *hdev,
2556 const bdaddr_t *bdaddr)
2558 struct sk_buff *skb;
2565 buf[3] = sizeof(bdaddr_t);
2566 memcpy(buf + 4, bdaddr, sizeof(bdaddr_t));
2568 skb = __hci_cmd_sync(hdev, 0xfc0b, sizeof(buf), buf, HCI_INIT_TIMEOUT);
2571 BT_ERR("%s: Change address command failed (%ld)",
2580 static int btusb_probe(struct usb_interface *intf,
2581 const struct usb_device_id *id)
2583 struct usb_endpoint_descriptor *ep_desc;
2584 struct btusb_data *data;
2585 struct hci_dev *hdev;
2588 BT_DBG("intf %p id %p", intf, id);
2590 /* interface numbers are hardcoded in the spec */
2591 if (intf->cur_altsetting->desc.bInterfaceNumber != 0)
2594 if (!id->driver_info) {
2595 const struct usb_device_id *match;
2597 match = usb_match_id(intf, blacklist_table);
2602 if (id->driver_info == BTUSB_IGNORE)
2605 if (id->driver_info & BTUSB_ATH3012) {
2606 struct usb_device *udev = interface_to_usbdev(intf);
2608 /* Old firmware would otherwise let ath3k driver load
2609 * patch and sysconfig files */
2610 if (le16_to_cpu(udev->descriptor.bcdDevice) <= 0x0001)
2614 data = devm_kzalloc(&intf->dev, sizeof(*data), GFP_KERNEL);
2618 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
2619 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
2621 if (!data->intr_ep && usb_endpoint_is_int_in(ep_desc)) {
2622 data->intr_ep = ep_desc;
2626 if (!data->bulk_tx_ep && usb_endpoint_is_bulk_out(ep_desc)) {
2627 data->bulk_tx_ep = ep_desc;
2631 if (!data->bulk_rx_ep && usb_endpoint_is_bulk_in(ep_desc)) {
2632 data->bulk_rx_ep = ep_desc;
2637 if (!data->intr_ep || !data->bulk_tx_ep || !data->bulk_rx_ep)
2640 data->cmdreq_type = USB_TYPE_CLASS;
2642 data->udev = interface_to_usbdev(intf);
2645 INIT_WORK(&data->work, btusb_work);
2646 INIT_WORK(&data->waker, btusb_waker);
2647 init_usb_anchor(&data->deferred);
2648 init_usb_anchor(&data->tx_anchor);
2649 spin_lock_init(&data->txlock);
2651 init_usb_anchor(&data->intr_anchor);
2652 init_usb_anchor(&data->bulk_anchor);
2653 init_usb_anchor(&data->isoc_anchor);
2654 spin_lock_init(&data->rxlock);
2656 if (id->driver_info & BTUSB_INTEL_NEW) {
2657 data->recv_event = btusb_recv_event_intel;
2658 data->recv_bulk = btusb_recv_bulk_intel;
2659 set_bit(BTUSB_BOOTLOADER, &data->flags);
2661 data->recv_event = hci_recv_frame;
2662 data->recv_bulk = btusb_recv_bulk;
2665 hdev = hci_alloc_dev();
2669 hdev->bus = HCI_USB;
2670 hci_set_drvdata(hdev, data);
2674 SET_HCIDEV_DEV(hdev, &intf->dev);
2676 hdev->open = btusb_open;
2677 hdev->close = btusb_close;
2678 hdev->flush = btusb_flush;
2679 hdev->send = btusb_send_frame;
2680 hdev->notify = btusb_notify;
2682 if (id->driver_info & BTUSB_BCM92035)
2683 hdev->setup = btusb_setup_bcm92035;
2685 if (id->driver_info & BTUSB_BCM_PATCHRAM) {
2686 hdev->setup = btusb_setup_bcm_patchram;
2687 hdev->set_bdaddr = btusb_set_bdaddr_bcm;
2688 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
2691 if (id->driver_info & BTUSB_INTEL) {
2692 hdev->setup = btusb_setup_intel;
2693 hdev->hw_error = btusb_hw_error_intel;
2694 hdev->set_bdaddr = btusb_set_bdaddr_intel;
2697 if (id->driver_info & BTUSB_INTEL_NEW) {
2698 hdev->send = btusb_send_frame_intel;
2699 hdev->setup = btusb_setup_intel_new;
2700 hdev->hw_error = btusb_hw_error_intel;
2701 hdev->set_bdaddr = btusb_set_bdaddr_intel;
2704 if (id->driver_info & BTUSB_MARVELL)
2705 hdev->set_bdaddr = btusb_set_bdaddr_marvell;
2707 if (id->driver_info & BTUSB_SWAVE) {
2708 set_bit(HCI_QUIRK_FIXUP_INQUIRY_MODE, &hdev->quirks);
2709 set_bit(HCI_QUIRK_BROKEN_LOCAL_COMMANDS, &hdev->quirks);
2712 if (id->driver_info & BTUSB_INTEL_BOOT)
2713 set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
2715 if (id->driver_info & BTUSB_ATH3012)
2716 hdev->set_bdaddr = btusb_set_bdaddr_ath3012;
2718 /* Interface numbers are hardcoded in the specification */
2719 data->isoc = usb_ifnum_to_if(data->udev, 1);
2722 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
2724 if (force_scofix || id->driver_info & BTUSB_WRONG_SCO_MTU) {
2725 if (!disable_scofix)
2726 set_bit(HCI_QUIRK_FIXUP_BUFFER_SIZE, &hdev->quirks);
2729 if (id->driver_info & BTUSB_BROKEN_ISOC)
2732 if (id->driver_info & BTUSB_DIGIANSWER) {
2733 data->cmdreq_type = USB_TYPE_VENDOR;
2734 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
2737 if (id->driver_info & BTUSB_CSR) {
2738 struct usb_device *udev = data->udev;
2739 u16 bcdDevice = le16_to_cpu(udev->descriptor.bcdDevice);
2741 /* Old firmware would otherwise execute USB reset */
2742 if (bcdDevice < 0x117)
2743 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
2745 /* Fake CSR devices with broken commands */
2746 if (bcdDevice <= 0x100)
2747 hdev->setup = btusb_setup_csr;
2750 if (id->driver_info & BTUSB_SNIFFER) {
2751 struct usb_device *udev = data->udev;
2753 /* New sniffer firmware has crippled HCI interface */
2754 if (le16_to_cpu(udev->descriptor.bcdDevice) > 0x997)
2755 set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
2758 if (id->driver_info & BTUSB_INTEL_BOOT) {
2759 /* A bug in the bootloader causes that interrupt interface is
2760 * only enabled after receiving SetInterface(0, AltSetting=0).
2762 err = usb_set_interface(data->udev, 0, 0);
2764 BT_ERR("failed to set interface 0, alt 0 %d", err);
2771 err = usb_driver_claim_interface(&btusb_driver,
2779 err = hci_register_dev(hdev);
2785 usb_set_intfdata(intf, data);
2790 static void btusb_disconnect(struct usb_interface *intf)
2792 struct btusb_data *data = usb_get_intfdata(intf);
2793 struct hci_dev *hdev;
2795 BT_DBG("intf %p", intf);
2801 usb_set_intfdata(data->intf, NULL);
2804 usb_set_intfdata(data->isoc, NULL);
2806 hci_unregister_dev(hdev);
2808 if (intf == data->isoc)
2809 usb_driver_release_interface(&btusb_driver, data->intf);
2810 else if (data->isoc)
2811 usb_driver_release_interface(&btusb_driver, data->isoc);
2817 static int btusb_suspend(struct usb_interface *intf, pm_message_t message)
2819 struct btusb_data *data = usb_get_intfdata(intf);
2821 BT_DBG("intf %p", intf);
2823 if (data->suspend_count++)
2826 spin_lock_irq(&data->txlock);
2827 if (!(PMSG_IS_AUTO(message) && data->tx_in_flight)) {
2828 set_bit(BTUSB_SUSPENDING, &data->flags);
2829 spin_unlock_irq(&data->txlock);
2831 spin_unlock_irq(&data->txlock);
2832 data->suspend_count--;
2836 cancel_work_sync(&data->work);
2838 btusb_stop_traffic(data);
2839 usb_kill_anchored_urbs(&data->tx_anchor);
2844 static void play_deferred(struct btusb_data *data)
2849 while ((urb = usb_get_from_anchor(&data->deferred))) {
2850 err = usb_submit_urb(urb, GFP_ATOMIC);
2854 data->tx_in_flight++;
2856 usb_scuttle_anchored_urbs(&data->deferred);
2859 static int btusb_resume(struct usb_interface *intf)
2861 struct btusb_data *data = usb_get_intfdata(intf);
2862 struct hci_dev *hdev = data->hdev;
2865 BT_DBG("intf %p", intf);
2867 if (--data->suspend_count)
2870 if (!test_bit(HCI_RUNNING, &hdev->flags))
2873 if (test_bit(BTUSB_INTR_RUNNING, &data->flags)) {
2874 err = btusb_submit_intr_urb(hdev, GFP_NOIO);
2876 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
2881 if (test_bit(BTUSB_BULK_RUNNING, &data->flags)) {
2882 err = btusb_submit_bulk_urb(hdev, GFP_NOIO);
2884 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
2888 btusb_submit_bulk_urb(hdev, GFP_NOIO);
2891 if (test_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
2892 if (btusb_submit_isoc_urb(hdev, GFP_NOIO) < 0)
2893 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
2895 btusb_submit_isoc_urb(hdev, GFP_NOIO);
2898 spin_lock_irq(&data->txlock);
2899 play_deferred(data);
2900 clear_bit(BTUSB_SUSPENDING, &data->flags);
2901 spin_unlock_irq(&data->txlock);
2902 schedule_work(&data->work);
2907 usb_scuttle_anchored_urbs(&data->deferred);
2909 spin_lock_irq(&data->txlock);
2910 clear_bit(BTUSB_SUSPENDING, &data->flags);
2911 spin_unlock_irq(&data->txlock);
2917 static struct usb_driver btusb_driver = {
2919 .probe = btusb_probe,
2920 .disconnect = btusb_disconnect,
2922 .suspend = btusb_suspend,
2923 .resume = btusb_resume,
2925 .id_table = btusb_table,
2926 .supports_autosuspend = 1,
2927 .disable_hub_initiated_lpm = 1,
2930 module_usb_driver(btusb_driver);
2932 module_param(disable_scofix, bool, 0644);
2933 MODULE_PARM_DESC(disable_scofix, "Disable fixup of wrong SCO buffer size");
2935 module_param(force_scofix, bool, 0644);
2936 MODULE_PARM_DESC(force_scofix, "Force fixup of wrong SCO buffers size");
2938 module_param(reset, bool, 0644);
2939 MODULE_PARM_DESC(reset, "Send HCI reset command on initialization");
2941 MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
2942 MODULE_DESCRIPTION("Generic Bluetooth USB driver ver " VERSION);
2943 MODULE_VERSION(VERSION);
2944 MODULE_LICENSE("GPL");