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_AVM 0x1000
54 static const struct usb_device_id btusb_table[] = {
55 /* Generic Bluetooth USB device */
56 { USB_DEVICE_INFO(0xe0, 0x01, 0x01) },
58 /* Apple-specific (Broadcom) devices */
59 { USB_VENDOR_AND_INTERFACE_INFO(0x05ac, 0xff, 0x01, 0x01) },
61 /* MediaTek MT76x0E */
62 { USB_DEVICE(0x0e8d, 0x763f) },
64 /* Broadcom SoftSailing reporting vendor specific */
65 { USB_DEVICE(0x0a5c, 0x21e1) },
67 /* Apple MacBookPro 7,1 */
68 { USB_DEVICE(0x05ac, 0x8213) },
71 { USB_DEVICE(0x05ac, 0x8215) },
73 /* Apple MacBookPro6,2 */
74 { USB_DEVICE(0x05ac, 0x8218) },
76 /* Apple MacBookAir3,1, MacBookAir3,2 */
77 { USB_DEVICE(0x05ac, 0x821b) },
79 /* Apple MacBookAir4,1 */
80 { USB_DEVICE(0x05ac, 0x821f) },
82 /* Apple MacBookPro8,2 */
83 { USB_DEVICE(0x05ac, 0x821a) },
85 /* Apple MacMini5,1 */
86 { USB_DEVICE(0x05ac, 0x8281) },
88 /* AVM BlueFRITZ! USB v2.0 */
89 { USB_DEVICE(0x057c, 0x3800), .driver_info = BTUSB_AVM },
91 /* Bluetooth Ultraport Module from IBM */
92 { USB_DEVICE(0x04bf, 0x030a) },
94 /* ALPS Modules with non-standard id */
95 { USB_DEVICE(0x044e, 0x3001) },
96 { USB_DEVICE(0x044e, 0x3002) },
98 /* Ericsson with non-standard id */
99 { USB_DEVICE(0x0bdb, 0x1002) },
101 /* Canyon CN-BTU1 with HID interfaces */
102 { USB_DEVICE(0x0c10, 0x0000) },
104 /* Broadcom BCM20702A0 */
105 { USB_DEVICE(0x0489, 0xe042) },
106 { USB_DEVICE(0x04ca, 0x2003) },
107 { USB_DEVICE(0x0b05, 0x17b5) },
108 { USB_DEVICE(0x0b05, 0x17cb) },
109 { USB_DEVICE(0x413c, 0x8197) },
110 { USB_DEVICE(0x13d3, 0x3404),
111 .driver_info = BTUSB_BCM_PATCHRAM },
113 /* Foxconn - Hon Hai */
114 { USB_VENDOR_AND_INTERFACE_INFO(0x0489, 0xff, 0x01, 0x01),
115 .driver_info = BTUSB_BCM_PATCHRAM },
117 /* Broadcom devices with vendor specific id */
118 { USB_VENDOR_AND_INTERFACE_INFO(0x0a5c, 0xff, 0x01, 0x01),
119 .driver_info = BTUSB_BCM_PATCHRAM },
121 /* ASUSTek Computer - Broadcom based */
122 { USB_VENDOR_AND_INTERFACE_INFO(0x0b05, 0xff, 0x01, 0x01) },
124 /* Belkin F8065bf - Broadcom based */
125 { USB_VENDOR_AND_INTERFACE_INFO(0x050d, 0xff, 0x01, 0x01) },
127 /* IMC Networks - Broadcom based */
128 { USB_VENDOR_AND_INTERFACE_INFO(0x13d3, 0xff, 0x01, 0x01) },
130 /* Intel Bluetooth USB Bootloader (RAM module) */
131 { USB_DEVICE(0x8087, 0x0a5a),
132 .driver_info = BTUSB_INTEL_BOOT | BTUSB_BROKEN_ISOC },
134 { } /* Terminating entry */
137 MODULE_DEVICE_TABLE(usb, btusb_table);
139 static const struct usb_device_id blacklist_table[] = {
140 /* CSR BlueCore devices */
141 { USB_DEVICE(0x0a12, 0x0001), .driver_info = BTUSB_CSR },
143 /* Broadcom BCM2033 without firmware */
144 { USB_DEVICE(0x0a5c, 0x2033), .driver_info = BTUSB_IGNORE },
146 /* Atheros 3011 with sflash firmware */
147 { USB_DEVICE(0x0489, 0xe027), .driver_info = BTUSB_IGNORE },
148 { USB_DEVICE(0x0489, 0xe03d), .driver_info = BTUSB_IGNORE },
149 { USB_DEVICE(0x0930, 0x0215), .driver_info = BTUSB_IGNORE },
150 { USB_DEVICE(0x0cf3, 0x3002), .driver_info = BTUSB_IGNORE },
151 { USB_DEVICE(0x0cf3, 0xe019), .driver_info = BTUSB_IGNORE },
152 { USB_DEVICE(0x13d3, 0x3304), .driver_info = BTUSB_IGNORE },
154 /* Atheros AR9285 Malbec with sflash firmware */
155 { USB_DEVICE(0x03f0, 0x311d), .driver_info = BTUSB_IGNORE },
157 /* Atheros 3012 with sflash firmware */
158 { USB_DEVICE(0x0489, 0xe04d), .driver_info = BTUSB_ATH3012 },
159 { USB_DEVICE(0x0489, 0xe04e), .driver_info = BTUSB_ATH3012 },
160 { USB_DEVICE(0x0489, 0xe056), .driver_info = BTUSB_ATH3012 },
161 { USB_DEVICE(0x0489, 0xe057), .driver_info = BTUSB_ATH3012 },
162 { USB_DEVICE(0x0489, 0xe05f), .driver_info = BTUSB_ATH3012 },
163 { USB_DEVICE(0x0489, 0xe078), .driver_info = BTUSB_ATH3012 },
164 { USB_DEVICE(0x04c5, 0x1330), .driver_info = BTUSB_ATH3012 },
165 { USB_DEVICE(0x04ca, 0x3004), .driver_info = BTUSB_ATH3012 },
166 { USB_DEVICE(0x04ca, 0x3005), .driver_info = BTUSB_ATH3012 },
167 { USB_DEVICE(0x04ca, 0x3006), .driver_info = BTUSB_ATH3012 },
168 { USB_DEVICE(0x04ca, 0x3007), .driver_info = BTUSB_ATH3012 },
169 { USB_DEVICE(0x04ca, 0x3008), .driver_info = BTUSB_ATH3012 },
170 { USB_DEVICE(0x04ca, 0x300b), .driver_info = BTUSB_ATH3012 },
171 { USB_DEVICE(0x04ca, 0x3010), .driver_info = BTUSB_ATH3012 },
172 { USB_DEVICE(0x0930, 0x0219), .driver_info = BTUSB_ATH3012 },
173 { USB_DEVICE(0x0930, 0x0220), .driver_info = BTUSB_ATH3012 },
174 { USB_DEVICE(0x0930, 0x0227), .driver_info = BTUSB_ATH3012 },
175 { USB_DEVICE(0x0b05, 0x17d0), .driver_info = BTUSB_ATH3012 },
176 { USB_DEVICE(0x0cf3, 0x0036), .driver_info = BTUSB_ATH3012 },
177 { USB_DEVICE(0x0cf3, 0x3004), .driver_info = BTUSB_ATH3012 },
178 { USB_DEVICE(0x0cf3, 0x3008), .driver_info = BTUSB_ATH3012 },
179 { USB_DEVICE(0x0cf3, 0x311d), .driver_info = BTUSB_ATH3012 },
180 { USB_DEVICE(0x0cf3, 0x311e), .driver_info = BTUSB_ATH3012 },
181 { USB_DEVICE(0x0cf3, 0x311f), .driver_info = BTUSB_ATH3012 },
182 { USB_DEVICE(0x0cf3, 0x3121), .driver_info = BTUSB_ATH3012 },
183 { USB_DEVICE(0x0cf3, 0x817a), .driver_info = BTUSB_ATH3012 },
184 { USB_DEVICE(0x0cf3, 0xe003), .driver_info = BTUSB_ATH3012 },
185 { USB_DEVICE(0x0cf3, 0xe004), .driver_info = BTUSB_ATH3012 },
186 { USB_DEVICE(0x0cf3, 0xe005), .driver_info = BTUSB_ATH3012 },
187 { USB_DEVICE(0x13d3, 0x3362), .driver_info = BTUSB_ATH3012 },
188 { USB_DEVICE(0x13d3, 0x3375), .driver_info = BTUSB_ATH3012 },
189 { USB_DEVICE(0x13d3, 0x3393), .driver_info = BTUSB_ATH3012 },
190 { USB_DEVICE(0x13d3, 0x3402), .driver_info = BTUSB_ATH3012 },
191 { USB_DEVICE(0x13d3, 0x3408), .driver_info = BTUSB_ATH3012 },
192 { USB_DEVICE(0x13d3, 0x3432), .driver_info = BTUSB_ATH3012 },
194 /* Atheros AR5BBU12 with sflash firmware */
195 { USB_DEVICE(0x0489, 0xe02c), .driver_info = BTUSB_IGNORE },
197 /* Atheros AR5BBU12 with sflash firmware */
198 { USB_DEVICE(0x0489, 0xe036), .driver_info = BTUSB_ATH3012 },
199 { USB_DEVICE(0x0489, 0xe03c), .driver_info = BTUSB_ATH3012 },
201 /* Broadcom BCM2035 */
202 { USB_DEVICE(0x0a5c, 0x2009), .driver_info = BTUSB_BCM92035 },
203 { USB_DEVICE(0x0a5c, 0x200a), .driver_info = BTUSB_WRONG_SCO_MTU },
204 { USB_DEVICE(0x0a5c, 0x2035), .driver_info = BTUSB_WRONG_SCO_MTU },
206 /* Broadcom BCM2045 */
207 { USB_DEVICE(0x0a5c, 0x2039), .driver_info = BTUSB_WRONG_SCO_MTU },
208 { USB_DEVICE(0x0a5c, 0x2101), .driver_info = BTUSB_WRONG_SCO_MTU },
210 /* IBM/Lenovo ThinkPad with Broadcom chip */
211 { USB_DEVICE(0x0a5c, 0x201e), .driver_info = BTUSB_WRONG_SCO_MTU },
212 { USB_DEVICE(0x0a5c, 0x2110), .driver_info = BTUSB_WRONG_SCO_MTU },
214 /* HP laptop with Broadcom chip */
215 { USB_DEVICE(0x03f0, 0x171d), .driver_info = BTUSB_WRONG_SCO_MTU },
217 /* Dell laptop with Broadcom chip */
218 { USB_DEVICE(0x413c, 0x8126), .driver_info = BTUSB_WRONG_SCO_MTU },
220 /* Dell Wireless 370 and 410 devices */
221 { USB_DEVICE(0x413c, 0x8152), .driver_info = BTUSB_WRONG_SCO_MTU },
222 { USB_DEVICE(0x413c, 0x8156), .driver_info = BTUSB_WRONG_SCO_MTU },
224 /* Belkin F8T012 and F8T013 devices */
225 { USB_DEVICE(0x050d, 0x0012), .driver_info = BTUSB_WRONG_SCO_MTU },
226 { USB_DEVICE(0x050d, 0x0013), .driver_info = BTUSB_WRONG_SCO_MTU },
228 /* Asus WL-BTD202 device */
229 { USB_DEVICE(0x0b05, 0x1715), .driver_info = BTUSB_WRONG_SCO_MTU },
231 /* Kensington Bluetooth USB adapter */
232 { USB_DEVICE(0x047d, 0x105e), .driver_info = BTUSB_WRONG_SCO_MTU },
234 /* RTX Telecom based adapters with buggy SCO support */
235 { USB_DEVICE(0x0400, 0x0807), .driver_info = BTUSB_BROKEN_ISOC },
236 { USB_DEVICE(0x0400, 0x080a), .driver_info = BTUSB_BROKEN_ISOC },
238 /* CONWISE Technology based adapters with buggy SCO support */
239 { USB_DEVICE(0x0e5e, 0x6622), .driver_info = BTUSB_BROKEN_ISOC },
241 /* Digianswer devices */
242 { USB_DEVICE(0x08fd, 0x0001), .driver_info = BTUSB_DIGIANSWER },
243 { USB_DEVICE(0x08fd, 0x0002), .driver_info = BTUSB_IGNORE },
245 /* CSR BlueCore Bluetooth Sniffer */
246 { USB_DEVICE(0x0a12, 0x0002),
247 .driver_info = BTUSB_SNIFFER | BTUSB_BROKEN_ISOC },
249 /* Frontline ComProbe Bluetooth Sniffer */
250 { USB_DEVICE(0x16d3, 0x0002),
251 .driver_info = BTUSB_SNIFFER | BTUSB_BROKEN_ISOC },
253 /* Intel Bluetooth device */
254 { USB_DEVICE(0x8087, 0x07dc), .driver_info = BTUSB_INTEL },
255 { USB_DEVICE(0x8087, 0x0a2a), .driver_info = BTUSB_INTEL },
258 { USB_DEVICE(0x1286, 0x2044), .driver_info = BTUSB_MARVELL },
259 { USB_DEVICE(0x1286, 0x2046), .driver_info = BTUSB_MARVELL },
261 { } /* Terminating entry */
264 #define BTUSB_MAX_ISOC_FRAMES 10
266 #define BTUSB_INTR_RUNNING 0
267 #define BTUSB_BULK_RUNNING 1
268 #define BTUSB_ISOC_RUNNING 2
269 #define BTUSB_SUSPENDING 3
270 #define BTUSB_DID_ISO_RESUME 4
273 struct hci_dev *hdev;
274 struct usb_device *udev;
275 struct usb_interface *intf;
276 struct usb_interface *isoc;
280 struct work_struct work;
281 struct work_struct waker;
283 struct usb_anchor deferred;
284 struct usb_anchor tx_anchor;
288 struct usb_anchor intr_anchor;
289 struct usb_anchor bulk_anchor;
290 struct usb_anchor isoc_anchor;
293 struct sk_buff *evt_skb;
294 struct sk_buff *acl_skb;
295 struct sk_buff *sco_skb;
297 struct usb_endpoint_descriptor *intr_ep;
298 struct usb_endpoint_descriptor *bulk_tx_ep;
299 struct usb_endpoint_descriptor *bulk_rx_ep;
300 struct usb_endpoint_descriptor *isoc_tx_ep;
301 struct usb_endpoint_descriptor *isoc_rx_ep;
305 unsigned int sco_num;
309 int (*recv_bulk)(struct btusb_data *data, void *buffer, int count);
312 static inline void btusb_free_frags(struct btusb_data *data)
316 spin_lock_irqsave(&data->rxlock, flags);
318 kfree_skb(data->evt_skb);
319 data->evt_skb = NULL;
321 kfree_skb(data->acl_skb);
322 data->acl_skb = NULL;
324 kfree_skb(data->sco_skb);
325 data->sco_skb = NULL;
327 spin_unlock_irqrestore(&data->rxlock, flags);
330 static int btusb_recv_intr(struct btusb_data *data, void *buffer, int count)
335 spin_lock(&data->rxlock);
342 skb = bt_skb_alloc(HCI_MAX_EVENT_SIZE, GFP_ATOMIC);
348 bt_cb(skb)->pkt_type = HCI_EVENT_PKT;
349 bt_cb(skb)->expect = HCI_EVENT_HDR_SIZE;
352 len = min_t(uint, bt_cb(skb)->expect, count);
353 memcpy(skb_put(skb, len), buffer, len);
357 bt_cb(skb)->expect -= len;
359 if (skb->len == HCI_EVENT_HDR_SIZE) {
360 /* Complete event header */
361 bt_cb(skb)->expect = hci_event_hdr(skb)->plen;
363 if (skb_tailroom(skb) < bt_cb(skb)->expect) {
372 if (bt_cb(skb)->expect == 0) {
374 hci_recv_frame(data->hdev, skb);
380 spin_unlock(&data->rxlock);
385 static int btusb_recv_bulk(struct btusb_data *data, void *buffer, int count)
390 spin_lock(&data->rxlock);
397 skb = bt_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC);
403 bt_cb(skb)->pkt_type = HCI_ACLDATA_PKT;
404 bt_cb(skb)->expect = HCI_ACL_HDR_SIZE;
407 len = min_t(uint, bt_cb(skb)->expect, count);
408 memcpy(skb_put(skb, len), buffer, len);
412 bt_cb(skb)->expect -= len;
414 if (skb->len == HCI_ACL_HDR_SIZE) {
415 __le16 dlen = hci_acl_hdr(skb)->dlen;
417 /* Complete ACL header */
418 bt_cb(skb)->expect = __le16_to_cpu(dlen);
420 if (skb_tailroom(skb) < bt_cb(skb)->expect) {
429 if (bt_cb(skb)->expect == 0) {
431 hci_recv_frame(data->hdev, skb);
437 spin_unlock(&data->rxlock);
442 static int btusb_recv_isoc(struct btusb_data *data, void *buffer, int count)
447 spin_lock(&data->rxlock);
454 skb = bt_skb_alloc(HCI_MAX_SCO_SIZE, GFP_ATOMIC);
460 bt_cb(skb)->pkt_type = HCI_SCODATA_PKT;
461 bt_cb(skb)->expect = HCI_SCO_HDR_SIZE;
464 len = min_t(uint, bt_cb(skb)->expect, count);
465 memcpy(skb_put(skb, len), buffer, len);
469 bt_cb(skb)->expect -= len;
471 if (skb->len == HCI_SCO_HDR_SIZE) {
472 /* Complete SCO header */
473 bt_cb(skb)->expect = hci_sco_hdr(skb)->dlen;
475 if (skb_tailroom(skb) < bt_cb(skb)->expect) {
484 if (bt_cb(skb)->expect == 0) {
486 hci_recv_frame(data->hdev, skb);
492 spin_unlock(&data->rxlock);
497 static void btusb_intr_complete(struct urb *urb)
499 struct hci_dev *hdev = urb->context;
500 struct btusb_data *data = hci_get_drvdata(hdev);
503 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
506 if (!test_bit(HCI_RUNNING, &hdev->flags))
509 if (urb->status == 0) {
510 hdev->stat.byte_rx += urb->actual_length;
512 if (btusb_recv_intr(data, urb->transfer_buffer,
513 urb->actual_length) < 0) {
514 BT_ERR("%s corrupted event packet", hdev->name);
517 } else if (urb->status == -ENOENT) {
518 /* Avoid suspend failed when usb_kill_urb */
522 if (!test_bit(BTUSB_INTR_RUNNING, &data->flags))
525 usb_mark_last_busy(data->udev);
526 usb_anchor_urb(urb, &data->intr_anchor);
528 err = usb_submit_urb(urb, GFP_ATOMIC);
530 /* -EPERM: urb is being killed;
531 * -ENODEV: device got disconnected */
532 if (err != -EPERM && err != -ENODEV)
533 BT_ERR("%s urb %p failed to resubmit (%d)",
534 hdev->name, urb, -err);
535 usb_unanchor_urb(urb);
539 static int btusb_submit_intr_urb(struct hci_dev *hdev, gfp_t mem_flags)
541 struct btusb_data *data = hci_get_drvdata(hdev);
547 BT_DBG("%s", hdev->name);
552 urb = usb_alloc_urb(0, mem_flags);
556 size = le16_to_cpu(data->intr_ep->wMaxPacketSize);
558 buf = kmalloc(size, mem_flags);
564 pipe = usb_rcvintpipe(data->udev, data->intr_ep->bEndpointAddress);
566 usb_fill_int_urb(urb, data->udev, pipe, buf, size,
567 btusb_intr_complete, hdev, data->intr_ep->bInterval);
569 urb->transfer_flags |= URB_FREE_BUFFER;
571 usb_anchor_urb(urb, &data->intr_anchor);
573 err = usb_submit_urb(urb, mem_flags);
575 if (err != -EPERM && err != -ENODEV)
576 BT_ERR("%s urb %p submission failed (%d)",
577 hdev->name, urb, -err);
578 usb_unanchor_urb(urb);
586 static void btusb_bulk_complete(struct urb *urb)
588 struct hci_dev *hdev = urb->context;
589 struct btusb_data *data = hci_get_drvdata(hdev);
592 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
595 if (!test_bit(HCI_RUNNING, &hdev->flags))
598 if (urb->status == 0) {
599 hdev->stat.byte_rx += urb->actual_length;
601 if (data->recv_bulk(data, urb->transfer_buffer,
602 urb->actual_length) < 0) {
603 BT_ERR("%s corrupted ACL packet", hdev->name);
606 } else if (urb->status == -ENOENT) {
607 /* Avoid suspend failed when usb_kill_urb */
611 if (!test_bit(BTUSB_BULK_RUNNING, &data->flags))
614 usb_anchor_urb(urb, &data->bulk_anchor);
615 usb_mark_last_busy(data->udev);
617 err = usb_submit_urb(urb, GFP_ATOMIC);
619 /* -EPERM: urb is being killed;
620 * -ENODEV: device got disconnected */
621 if (err != -EPERM && err != -ENODEV)
622 BT_ERR("%s urb %p failed to resubmit (%d)",
623 hdev->name, urb, -err);
624 usb_unanchor_urb(urb);
628 static int btusb_submit_bulk_urb(struct hci_dev *hdev, gfp_t mem_flags)
630 struct btusb_data *data = hci_get_drvdata(hdev);
634 int err, size = HCI_MAX_FRAME_SIZE;
636 BT_DBG("%s", hdev->name);
638 if (!data->bulk_rx_ep)
641 urb = usb_alloc_urb(0, mem_flags);
645 buf = kmalloc(size, mem_flags);
651 pipe = usb_rcvbulkpipe(data->udev, data->bulk_rx_ep->bEndpointAddress);
653 usb_fill_bulk_urb(urb, data->udev, pipe, buf, size,
654 btusb_bulk_complete, hdev);
656 urb->transfer_flags |= URB_FREE_BUFFER;
658 usb_mark_last_busy(data->udev);
659 usb_anchor_urb(urb, &data->bulk_anchor);
661 err = usb_submit_urb(urb, mem_flags);
663 if (err != -EPERM && err != -ENODEV)
664 BT_ERR("%s urb %p submission failed (%d)",
665 hdev->name, urb, -err);
666 usb_unanchor_urb(urb);
674 static void btusb_isoc_complete(struct urb *urb)
676 struct hci_dev *hdev = urb->context;
677 struct btusb_data *data = hci_get_drvdata(hdev);
680 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
683 if (!test_bit(HCI_RUNNING, &hdev->flags))
686 if (urb->status == 0) {
687 for (i = 0; i < urb->number_of_packets; i++) {
688 unsigned int offset = urb->iso_frame_desc[i].offset;
689 unsigned int length = urb->iso_frame_desc[i].actual_length;
691 if (urb->iso_frame_desc[i].status)
694 hdev->stat.byte_rx += length;
696 if (btusb_recv_isoc(data, urb->transfer_buffer + offset,
698 BT_ERR("%s corrupted SCO packet", hdev->name);
702 } else if (urb->status == -ENOENT) {
703 /* Avoid suspend failed when usb_kill_urb */
707 if (!test_bit(BTUSB_ISOC_RUNNING, &data->flags))
710 usb_anchor_urb(urb, &data->isoc_anchor);
712 err = usb_submit_urb(urb, GFP_ATOMIC);
714 /* -EPERM: urb is being killed;
715 * -ENODEV: device got disconnected */
716 if (err != -EPERM && err != -ENODEV)
717 BT_ERR("%s urb %p failed to resubmit (%d)",
718 hdev->name, urb, -err);
719 usb_unanchor_urb(urb);
723 static inline void __fill_isoc_descriptor(struct urb *urb, int len, int mtu)
727 BT_DBG("len %d mtu %d", len, mtu);
729 for (i = 0; i < BTUSB_MAX_ISOC_FRAMES && len >= mtu;
730 i++, offset += mtu, len -= mtu) {
731 urb->iso_frame_desc[i].offset = offset;
732 urb->iso_frame_desc[i].length = mtu;
735 if (len && i < BTUSB_MAX_ISOC_FRAMES) {
736 urb->iso_frame_desc[i].offset = offset;
737 urb->iso_frame_desc[i].length = len;
741 urb->number_of_packets = i;
744 static int btusb_submit_isoc_urb(struct hci_dev *hdev, gfp_t mem_flags)
746 struct btusb_data *data = hci_get_drvdata(hdev);
752 BT_DBG("%s", hdev->name);
754 if (!data->isoc_rx_ep)
757 urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, mem_flags);
761 size = le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize) *
762 BTUSB_MAX_ISOC_FRAMES;
764 buf = kmalloc(size, mem_flags);
770 pipe = usb_rcvisocpipe(data->udev, data->isoc_rx_ep->bEndpointAddress);
772 usb_fill_int_urb(urb, data->udev, pipe, buf, size, btusb_isoc_complete,
773 hdev, data->isoc_rx_ep->bInterval);
775 urb->transfer_flags = URB_FREE_BUFFER | URB_ISO_ASAP;
777 __fill_isoc_descriptor(urb, size,
778 le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize));
780 usb_anchor_urb(urb, &data->isoc_anchor);
782 err = usb_submit_urb(urb, mem_flags);
784 if (err != -EPERM && err != -ENODEV)
785 BT_ERR("%s urb %p submission failed (%d)",
786 hdev->name, urb, -err);
787 usb_unanchor_urb(urb);
795 static void btusb_tx_complete(struct urb *urb)
797 struct sk_buff *skb = urb->context;
798 struct hci_dev *hdev = (struct hci_dev *)skb->dev;
799 struct btusb_data *data = hci_get_drvdata(hdev);
801 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
804 if (!test_bit(HCI_RUNNING, &hdev->flags))
808 hdev->stat.byte_tx += urb->transfer_buffer_length;
813 spin_lock(&data->txlock);
814 data->tx_in_flight--;
815 spin_unlock(&data->txlock);
817 kfree(urb->setup_packet);
822 static void btusb_isoc_tx_complete(struct urb *urb)
824 struct sk_buff *skb = urb->context;
825 struct hci_dev *hdev = (struct hci_dev *)skb->dev;
827 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
830 if (!test_bit(HCI_RUNNING, &hdev->flags))
834 hdev->stat.byte_tx += urb->transfer_buffer_length;
839 kfree(urb->setup_packet);
844 static int btusb_open(struct hci_dev *hdev)
846 struct btusb_data *data = hci_get_drvdata(hdev);
849 BT_DBG("%s", hdev->name);
851 err = usb_autopm_get_interface(data->intf);
855 data->intf->needs_remote_wakeup = 1;
857 if (test_and_set_bit(HCI_RUNNING, &hdev->flags))
860 if (test_and_set_bit(BTUSB_INTR_RUNNING, &data->flags))
863 err = btusb_submit_intr_urb(hdev, GFP_KERNEL);
867 err = btusb_submit_bulk_urb(hdev, GFP_KERNEL);
869 usb_kill_anchored_urbs(&data->intr_anchor);
873 set_bit(BTUSB_BULK_RUNNING, &data->flags);
874 btusb_submit_bulk_urb(hdev, GFP_KERNEL);
877 usb_autopm_put_interface(data->intf);
881 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
882 clear_bit(HCI_RUNNING, &hdev->flags);
883 usb_autopm_put_interface(data->intf);
887 static void btusb_stop_traffic(struct btusb_data *data)
889 usb_kill_anchored_urbs(&data->intr_anchor);
890 usb_kill_anchored_urbs(&data->bulk_anchor);
891 usb_kill_anchored_urbs(&data->isoc_anchor);
894 static int btusb_close(struct hci_dev *hdev)
896 struct btusb_data *data = hci_get_drvdata(hdev);
899 BT_DBG("%s", hdev->name);
901 if (!test_and_clear_bit(HCI_RUNNING, &hdev->flags))
904 cancel_work_sync(&data->work);
905 cancel_work_sync(&data->waker);
907 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
908 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
909 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
911 btusb_stop_traffic(data);
912 btusb_free_frags(data);
914 err = usb_autopm_get_interface(data->intf);
918 data->intf->needs_remote_wakeup = 0;
919 usb_autopm_put_interface(data->intf);
922 usb_scuttle_anchored_urbs(&data->deferred);
926 static int btusb_flush(struct hci_dev *hdev)
928 struct btusb_data *data = hci_get_drvdata(hdev);
930 BT_DBG("%s", hdev->name);
932 usb_kill_anchored_urbs(&data->tx_anchor);
933 btusb_free_frags(data);
938 static struct urb *alloc_ctrl_urb(struct hci_dev *hdev, struct sk_buff *skb)
940 struct btusb_data *data = hci_get_drvdata(hdev);
941 struct usb_ctrlrequest *dr;
945 urb = usb_alloc_urb(0, GFP_KERNEL);
947 return ERR_PTR(-ENOMEM);
949 dr = kmalloc(sizeof(*dr), GFP_KERNEL);
952 return ERR_PTR(-ENOMEM);
955 dr->bRequestType = data->cmdreq_type;
959 dr->wLength = __cpu_to_le16(skb->len);
961 pipe = usb_sndctrlpipe(data->udev, 0x00);
963 usb_fill_control_urb(urb, data->udev, pipe, (void *)dr,
964 skb->data, skb->len, btusb_tx_complete, skb);
966 skb->dev = (void *)hdev;
971 static struct urb *alloc_bulk_urb(struct hci_dev *hdev, struct sk_buff *skb)
973 struct btusb_data *data = hci_get_drvdata(hdev);
977 if (!data->bulk_tx_ep)
978 return ERR_PTR(-ENODEV);
980 urb = usb_alloc_urb(0, GFP_KERNEL);
982 return ERR_PTR(-ENOMEM);
984 pipe = usb_sndbulkpipe(data->udev, data->bulk_tx_ep->bEndpointAddress);
986 usb_fill_bulk_urb(urb, data->udev, pipe,
987 skb->data, skb->len, btusb_tx_complete, skb);
989 skb->dev = (void *)hdev;
994 static struct urb *alloc_isoc_urb(struct hci_dev *hdev, struct sk_buff *skb)
996 struct btusb_data *data = hci_get_drvdata(hdev);
1000 if (!data->isoc_tx_ep)
1001 return ERR_PTR(-ENODEV);
1003 urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, GFP_KERNEL);
1005 return ERR_PTR(-ENOMEM);
1007 pipe = usb_sndisocpipe(data->udev, data->isoc_tx_ep->bEndpointAddress);
1009 usb_fill_int_urb(urb, data->udev, pipe,
1010 skb->data, skb->len, btusb_isoc_tx_complete,
1011 skb, data->isoc_tx_ep->bInterval);
1013 urb->transfer_flags = URB_ISO_ASAP;
1015 __fill_isoc_descriptor(urb, skb->len,
1016 le16_to_cpu(data->isoc_tx_ep->wMaxPacketSize));
1018 skb->dev = (void *)hdev;
1023 static int submit_tx_urb(struct hci_dev *hdev, struct urb *urb)
1025 struct btusb_data *data = hci_get_drvdata(hdev);
1028 usb_anchor_urb(urb, &data->tx_anchor);
1030 err = usb_submit_urb(urb, GFP_KERNEL);
1032 if (err != -EPERM && err != -ENODEV)
1033 BT_ERR("%s urb %p submission failed (%d)",
1034 hdev->name, urb, -err);
1035 kfree(urb->setup_packet);
1036 usb_unanchor_urb(urb);
1038 usb_mark_last_busy(data->udev);
1045 static int submit_or_queue_tx_urb(struct hci_dev *hdev, struct urb *urb)
1047 struct btusb_data *data = hci_get_drvdata(hdev);
1048 unsigned long flags;
1051 spin_lock_irqsave(&data->txlock, flags);
1052 suspending = test_bit(BTUSB_SUSPENDING, &data->flags);
1054 data->tx_in_flight++;
1055 spin_unlock_irqrestore(&data->txlock, flags);
1058 return submit_tx_urb(hdev, urb);
1060 usb_anchor_urb(urb, &data->deferred);
1061 schedule_work(&data->waker);
1067 static int btusb_send_frame(struct hci_dev *hdev, struct sk_buff *skb)
1071 BT_DBG("%s", hdev->name);
1073 if (!test_bit(HCI_RUNNING, &hdev->flags))
1076 switch (bt_cb(skb)->pkt_type) {
1077 case HCI_COMMAND_PKT:
1078 urb = alloc_ctrl_urb(hdev, skb);
1080 return PTR_ERR(urb);
1082 hdev->stat.cmd_tx++;
1083 return submit_or_queue_tx_urb(hdev, urb);
1085 case HCI_ACLDATA_PKT:
1086 urb = alloc_bulk_urb(hdev, skb);
1088 return PTR_ERR(urb);
1090 hdev->stat.acl_tx++;
1091 return submit_or_queue_tx_urb(hdev, urb);
1093 case HCI_SCODATA_PKT:
1094 if (hci_conn_num(hdev, SCO_LINK) < 1)
1097 urb = alloc_isoc_urb(hdev, skb);
1099 return PTR_ERR(urb);
1101 hdev->stat.sco_tx++;
1102 return submit_tx_urb(hdev, urb);
1108 static void btusb_notify(struct hci_dev *hdev, unsigned int evt)
1110 struct btusb_data *data = hci_get_drvdata(hdev);
1112 BT_DBG("%s evt %d", hdev->name, evt);
1114 if (hci_conn_num(hdev, SCO_LINK) != data->sco_num) {
1115 data->sco_num = hci_conn_num(hdev, SCO_LINK);
1116 schedule_work(&data->work);
1120 static inline int __set_isoc_interface(struct hci_dev *hdev, int altsetting)
1122 struct btusb_data *data = hci_get_drvdata(hdev);
1123 struct usb_interface *intf = data->isoc;
1124 struct usb_endpoint_descriptor *ep_desc;
1130 err = usb_set_interface(data->udev, 1, altsetting);
1132 BT_ERR("%s setting interface failed (%d)", hdev->name, -err);
1136 data->isoc_altsetting = altsetting;
1138 data->isoc_tx_ep = NULL;
1139 data->isoc_rx_ep = NULL;
1141 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
1142 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
1144 if (!data->isoc_tx_ep && usb_endpoint_is_isoc_out(ep_desc)) {
1145 data->isoc_tx_ep = ep_desc;
1149 if (!data->isoc_rx_ep && usb_endpoint_is_isoc_in(ep_desc)) {
1150 data->isoc_rx_ep = ep_desc;
1155 if (!data->isoc_tx_ep || !data->isoc_rx_ep) {
1156 BT_ERR("%s invalid SCO descriptors", hdev->name);
1163 static void btusb_work(struct work_struct *work)
1165 struct btusb_data *data = container_of(work, struct btusb_data, work);
1166 struct hci_dev *hdev = data->hdev;
1170 if (data->sco_num > 0) {
1171 if (!test_bit(BTUSB_DID_ISO_RESUME, &data->flags)) {
1172 err = usb_autopm_get_interface(data->isoc ? data->isoc : data->intf);
1174 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1175 usb_kill_anchored_urbs(&data->isoc_anchor);
1179 set_bit(BTUSB_DID_ISO_RESUME, &data->flags);
1182 if (hdev->voice_setting & 0x0020) {
1183 static const int alts[3] = { 2, 4, 5 };
1185 new_alts = alts[data->sco_num - 1];
1187 new_alts = data->sco_num;
1190 if (data->isoc_altsetting != new_alts) {
1191 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1192 usb_kill_anchored_urbs(&data->isoc_anchor);
1194 if (__set_isoc_interface(hdev, new_alts) < 0)
1198 if (!test_and_set_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
1199 if (btusb_submit_isoc_urb(hdev, GFP_KERNEL) < 0)
1200 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1202 btusb_submit_isoc_urb(hdev, GFP_KERNEL);
1205 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1206 usb_kill_anchored_urbs(&data->isoc_anchor);
1208 __set_isoc_interface(hdev, 0);
1209 if (test_and_clear_bit(BTUSB_DID_ISO_RESUME, &data->flags))
1210 usb_autopm_put_interface(data->isoc ? data->isoc : data->intf);
1214 static void btusb_waker(struct work_struct *work)
1216 struct btusb_data *data = container_of(work, struct btusb_data, waker);
1219 err = usb_autopm_get_interface(data->intf);
1223 usb_autopm_put_interface(data->intf);
1226 static int btusb_setup_bcm92035(struct hci_dev *hdev)
1228 struct sk_buff *skb;
1231 BT_DBG("%s", hdev->name);
1233 skb = __hci_cmd_sync(hdev, 0xfc3b, 1, &val, HCI_INIT_TIMEOUT);
1235 BT_ERR("BCM92035 command failed (%ld)", -PTR_ERR(skb));
1242 static int btusb_setup_csr(struct hci_dev *hdev)
1244 struct hci_rp_read_local_version *rp;
1245 struct sk_buff *skb;
1248 BT_DBG("%s", hdev->name);
1250 skb = __hci_cmd_sync(hdev, HCI_OP_READ_LOCAL_VERSION, 0, NULL,
1253 BT_ERR("Reading local version failed (%ld)", -PTR_ERR(skb));
1254 return -PTR_ERR(skb);
1257 rp = (struct hci_rp_read_local_version *)skb->data;
1260 if (le16_to_cpu(rp->manufacturer) != 10) {
1261 /* Clear the reset quirk since this is not an actual
1262 * early Bluetooth 1.1 device from CSR.
1264 clear_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
1266 /* These fake CSR controllers have all a broken
1267 * stored link key handling and so just disable it.
1269 set_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY,
1274 ret = -bt_to_errno(rp->status);
1281 struct intel_version {
1294 static const struct firmware *btusb_setup_intel_get_fw(struct hci_dev *hdev,
1295 struct intel_version *ver)
1297 const struct firmware *fw;
1301 snprintf(fwname, sizeof(fwname),
1302 "intel/ibt-hw-%x.%x.%x-fw-%x.%x.%x.%x.%x.bseq",
1303 ver->hw_platform, ver->hw_variant, ver->hw_revision,
1304 ver->fw_variant, ver->fw_revision, ver->fw_build_num,
1305 ver->fw_build_ww, ver->fw_build_yy);
1307 ret = request_firmware(&fw, fwname, &hdev->dev);
1309 if (ret == -EINVAL) {
1310 BT_ERR("%s Intel firmware file request failed (%d)",
1315 BT_ERR("%s failed to open Intel firmware file: %s(%d)",
1316 hdev->name, fwname, ret);
1318 /* If the correct firmware patch file is not found, use the
1319 * default firmware patch file instead
1321 snprintf(fwname, sizeof(fwname), "intel/ibt-hw-%x.%x.bseq",
1322 ver->hw_platform, ver->hw_variant);
1323 if (request_firmware(&fw, fwname, &hdev->dev) < 0) {
1324 BT_ERR("%s failed to open default Intel fw file: %s",
1325 hdev->name, fwname);
1330 BT_INFO("%s: Intel Bluetooth firmware file: %s", hdev->name, fwname);
1335 static int btusb_setup_intel_patching(struct hci_dev *hdev,
1336 const struct firmware *fw,
1337 const u8 **fw_ptr, int *disable_patch)
1339 struct sk_buff *skb;
1340 struct hci_command_hdr *cmd;
1341 const u8 *cmd_param;
1342 struct hci_event_hdr *evt = NULL;
1343 const u8 *evt_param = NULL;
1344 int remain = fw->size - (*fw_ptr - fw->data);
1346 /* The first byte indicates the types of the patch command or event.
1347 * 0x01 means HCI command and 0x02 is HCI event. If the first bytes
1348 * in the current firmware buffer doesn't start with 0x01 or
1349 * the size of remain buffer is smaller than HCI command header,
1350 * the firmware file is corrupted and it should stop the patching
1353 if (remain > HCI_COMMAND_HDR_SIZE && *fw_ptr[0] != 0x01) {
1354 BT_ERR("%s Intel fw corrupted: invalid cmd read", hdev->name);
1360 cmd = (struct hci_command_hdr *)(*fw_ptr);
1361 *fw_ptr += sizeof(*cmd);
1362 remain -= sizeof(*cmd);
1364 /* Ensure that the remain firmware data is long enough than the length
1365 * of command parameter. If not, the firmware file is corrupted.
1367 if (remain < cmd->plen) {
1368 BT_ERR("%s Intel fw corrupted: invalid cmd len", hdev->name);
1372 /* If there is a command that loads a patch in the firmware
1373 * file, then enable the patch upon success, otherwise just
1374 * disable the manufacturer mode, for example patch activation
1375 * is not required when the default firmware patch file is used
1376 * because there are no patch data to load.
1378 if (*disable_patch && le16_to_cpu(cmd->opcode) == 0xfc8e)
1381 cmd_param = *fw_ptr;
1382 *fw_ptr += cmd->plen;
1383 remain -= cmd->plen;
1385 /* This reads the expected events when the above command is sent to the
1386 * device. Some vendor commands expects more than one events, for
1387 * example command status event followed by vendor specific event.
1388 * For this case, it only keeps the last expected event. so the command
1389 * can be sent with __hci_cmd_sync_ev() which returns the sk_buff of
1390 * last expected event.
1392 while (remain > HCI_EVENT_HDR_SIZE && *fw_ptr[0] == 0x02) {
1396 evt = (struct hci_event_hdr *)(*fw_ptr);
1397 *fw_ptr += sizeof(*evt);
1398 remain -= sizeof(*evt);
1400 if (remain < evt->plen) {
1401 BT_ERR("%s Intel fw corrupted: invalid evt len",
1406 evt_param = *fw_ptr;
1407 *fw_ptr += evt->plen;
1408 remain -= evt->plen;
1411 /* Every HCI commands in the firmware file has its correspond event.
1412 * If event is not found or remain is smaller than zero, the firmware
1413 * file is corrupted.
1415 if (!evt || !evt_param || remain < 0) {
1416 BT_ERR("%s Intel fw corrupted: invalid evt read", hdev->name);
1420 skb = __hci_cmd_sync_ev(hdev, le16_to_cpu(cmd->opcode), cmd->plen,
1421 cmd_param, evt->evt, HCI_INIT_TIMEOUT);
1423 BT_ERR("%s sending Intel patch command (0x%4.4x) failed (%ld)",
1424 hdev->name, cmd->opcode, PTR_ERR(skb));
1425 return PTR_ERR(skb);
1428 /* It ensures that the returned event matches the event data read from
1429 * the firmware file. At fist, it checks the length and then
1430 * the contents of the event.
1432 if (skb->len != evt->plen) {
1433 BT_ERR("%s mismatch event length (opcode 0x%4.4x)", hdev->name,
1434 le16_to_cpu(cmd->opcode));
1439 if (memcmp(skb->data, evt_param, evt->plen)) {
1440 BT_ERR("%s mismatch event parameter (opcode 0x%4.4x)",
1441 hdev->name, le16_to_cpu(cmd->opcode));
1450 #define BDADDR_INTEL (&(bdaddr_t) {{0x00, 0x8b, 0x9e, 0x19, 0x03, 0x00}})
1452 static int btusb_check_bdaddr_intel(struct hci_dev *hdev)
1454 struct sk_buff *skb;
1455 struct hci_rp_read_bd_addr *rp;
1457 skb = __hci_cmd_sync(hdev, HCI_OP_READ_BD_ADDR, 0, NULL,
1460 BT_ERR("%s reading Intel device address failed (%ld)",
1461 hdev->name, PTR_ERR(skb));
1462 return PTR_ERR(skb);
1465 if (skb->len != sizeof(*rp)) {
1466 BT_ERR("%s Intel device address length mismatch", hdev->name);
1471 rp = (struct hci_rp_read_bd_addr *)skb->data;
1473 BT_ERR("%s Intel device address result failed (%02x)",
1474 hdev->name, rp->status);
1476 return -bt_to_errno(rp->status);
1479 /* For some Intel based controllers, the default Bluetooth device
1480 * address 00:03:19:9E:8B:00 can be found. These controllers are
1481 * fully operational, but have the danger of duplicate addresses
1482 * and that in turn can cause problems with Bluetooth operation.
1484 if (!bacmp(&rp->bdaddr, BDADDR_INTEL)) {
1485 BT_ERR("%s found Intel default device address (%pMR)",
1486 hdev->name, &rp->bdaddr);
1487 set_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks);
1495 static int btusb_setup_intel(struct hci_dev *hdev)
1497 struct sk_buff *skb;
1498 const struct firmware *fw;
1501 struct intel_version *ver;
1503 const u8 mfg_enable[] = { 0x01, 0x00 };
1504 const u8 mfg_disable[] = { 0x00, 0x00 };
1505 const u8 mfg_reset_deactivate[] = { 0x00, 0x01 };
1506 const u8 mfg_reset_activate[] = { 0x00, 0x02 };
1508 BT_DBG("%s", hdev->name);
1510 /* The controller has a bug with the first HCI command sent to it
1511 * returning number of completed commands as zero. This would stall the
1512 * command processing in the Bluetooth core.
1514 * As a workaround, send HCI Reset command first which will reset the
1515 * number of completed commands and allow normal command processing
1518 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
1520 BT_ERR("%s sending initial HCI reset command failed (%ld)",
1521 hdev->name, PTR_ERR(skb));
1522 return PTR_ERR(skb);
1526 /* Read Intel specific controller version first to allow selection of
1527 * which firmware file to load.
1529 * The returned information are hardware variant and revision plus
1530 * firmware variant, revision and build number.
1532 skb = __hci_cmd_sync(hdev, 0xfc05, 0, NULL, HCI_INIT_TIMEOUT);
1534 BT_ERR("%s reading Intel fw version command failed (%ld)",
1535 hdev->name, PTR_ERR(skb));
1536 return PTR_ERR(skb);
1539 if (skb->len != sizeof(*ver)) {
1540 BT_ERR("%s Intel version event length mismatch", hdev->name);
1545 ver = (struct intel_version *)skb->data;
1547 BT_ERR("%s Intel fw version event failed (%02x)", hdev->name,
1550 return -bt_to_errno(ver->status);
1553 BT_INFO("%s: read Intel version: %02x%02x%02x%02x%02x%02x%02x%02x%02x",
1554 hdev->name, ver->hw_platform, ver->hw_variant,
1555 ver->hw_revision, ver->fw_variant, ver->fw_revision,
1556 ver->fw_build_num, ver->fw_build_ww, ver->fw_build_yy,
1559 /* fw_patch_num indicates the version of patch the device currently
1560 * have. If there is no patch data in the device, it is always 0x00.
1561 * So, if it is other than 0x00, no need to patch the deivce again.
1563 if (ver->fw_patch_num) {
1564 BT_INFO("%s: Intel device is already patched. patch num: %02x",
1565 hdev->name, ver->fw_patch_num);
1567 btusb_check_bdaddr_intel(hdev);
1571 /* Opens the firmware patch file based on the firmware version read
1572 * from the controller. If it fails to open the matching firmware
1573 * patch file, it tries to open the default firmware patch file.
1574 * If no patch file is found, allow the device to operate without
1577 fw = btusb_setup_intel_get_fw(hdev, ver);
1580 btusb_check_bdaddr_intel(hdev);
1585 /* This Intel specific command enables the manufacturer mode of the
1588 * Only while this mode is enabled, the driver can download the
1589 * firmware patch data and configuration parameters.
1591 skb = __hci_cmd_sync(hdev, 0xfc11, 2, mfg_enable, HCI_INIT_TIMEOUT);
1593 BT_ERR("%s entering Intel manufacturer mode failed (%ld)",
1594 hdev->name, PTR_ERR(skb));
1595 release_firmware(fw);
1596 return PTR_ERR(skb);
1600 u8 evt_status = skb->data[0];
1602 BT_ERR("%s enable Intel manufacturer mode event failed (%02x)",
1603 hdev->name, evt_status);
1605 release_firmware(fw);
1606 return -bt_to_errno(evt_status);
1612 /* The firmware data file consists of list of Intel specific HCI
1613 * commands and its expected events. The first byte indicates the
1614 * type of the message, either HCI command or HCI event.
1616 * It reads the command and its expected event from the firmware file,
1617 * and send to the controller. Once __hci_cmd_sync_ev() returns,
1618 * the returned event is compared with the event read from the firmware
1619 * file and it will continue until all the messages are downloaded to
1622 * Once the firmware patching is completed successfully,
1623 * the manufacturer mode is disabled with reset and activating the
1626 * If the firmware patching fails, the manufacturer mode is
1627 * disabled with reset and deactivating the patch.
1629 * If the default patch file is used, no reset is done when disabling
1632 while (fw->size > fw_ptr - fw->data) {
1635 ret = btusb_setup_intel_patching(hdev, fw, &fw_ptr,
1638 goto exit_mfg_deactivate;
1641 release_firmware(fw);
1644 goto exit_mfg_disable;
1646 /* Patching completed successfully and disable the manufacturer mode
1647 * with reset and activate the downloaded firmware patches.
1649 skb = __hci_cmd_sync(hdev, 0xfc11, sizeof(mfg_reset_activate),
1650 mfg_reset_activate, HCI_INIT_TIMEOUT);
1652 BT_ERR("%s exiting Intel manufacturer mode failed (%ld)",
1653 hdev->name, PTR_ERR(skb));
1654 return PTR_ERR(skb);
1658 BT_INFO("%s: Intel Bluetooth firmware patch completed and activated",
1661 btusb_check_bdaddr_intel(hdev);
1665 /* Disable the manufacturer mode without reset */
1666 skb = __hci_cmd_sync(hdev, 0xfc11, sizeof(mfg_disable), mfg_disable,
1669 BT_ERR("%s exiting Intel manufacturer mode failed (%ld)",
1670 hdev->name, PTR_ERR(skb));
1671 return PTR_ERR(skb);
1675 BT_INFO("%s: Intel Bluetooth firmware patch completed", hdev->name);
1677 btusb_check_bdaddr_intel(hdev);
1680 exit_mfg_deactivate:
1681 release_firmware(fw);
1683 /* Patching failed. Disable the manufacturer mode with reset and
1684 * deactivate the downloaded firmware patches.
1686 skb = __hci_cmd_sync(hdev, 0xfc11, sizeof(mfg_reset_deactivate),
1687 mfg_reset_deactivate, 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 deactivated",
1698 btusb_check_bdaddr_intel(hdev);
1702 static int btusb_set_bdaddr_intel(struct hci_dev *hdev, const bdaddr_t *bdaddr)
1704 struct sk_buff *skb;
1707 skb = __hci_cmd_sync(hdev, 0xfc31, 6, bdaddr, HCI_INIT_TIMEOUT);
1710 BT_ERR("%s: changing Intel device address failed (%ld)",
1719 static int btusb_set_bdaddr_marvell(struct hci_dev *hdev,
1720 const bdaddr_t *bdaddr)
1722 struct sk_buff *skb;
1727 buf[1] = sizeof(bdaddr_t);
1728 memcpy(buf + 2, bdaddr, sizeof(bdaddr_t));
1730 skb = __hci_cmd_sync(hdev, 0xfc22, sizeof(buf), buf, HCI_INIT_TIMEOUT);
1733 BT_ERR("%s: changing Marvell device address failed (%ld)",
1742 #define BDADDR_BCM20702A0 (&(bdaddr_t) {{0x00, 0xa0, 0x02, 0x70, 0x20, 0x00}})
1744 static int btusb_setup_bcm_patchram(struct hci_dev *hdev)
1746 struct btusb_data *data = hci_get_drvdata(hdev);
1747 struct usb_device *udev = data->udev;
1749 const struct firmware *fw;
1752 const struct hci_command_hdr *cmd;
1753 const u8 *cmd_param;
1755 struct sk_buff *skb;
1756 struct hci_rp_read_local_version *ver;
1757 struct hci_rp_read_bd_addr *bda;
1760 snprintf(fw_name, sizeof(fw_name), "brcm/%s-%04x-%04x.hcd",
1761 udev->product ? udev->product : "BCM",
1762 le16_to_cpu(udev->descriptor.idVendor),
1763 le16_to_cpu(udev->descriptor.idProduct));
1765 ret = request_firmware(&fw, fw_name, &hdev->dev);
1767 BT_INFO("%s: BCM: patch %s not found", hdev->name, fw_name);
1772 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
1775 BT_ERR("%s: HCI_OP_RESET failed (%ld)", hdev->name, ret);
1780 /* Read Local Version Info */
1781 skb = __hci_cmd_sync(hdev, HCI_OP_READ_LOCAL_VERSION, 0, NULL,
1785 BT_ERR("%s: HCI_OP_READ_LOCAL_VERSION failed (%ld)",
1790 if (skb->len != sizeof(*ver)) {
1791 BT_ERR("%s: HCI_OP_READ_LOCAL_VERSION event length mismatch",
1798 ver = (struct hci_rp_read_local_version *)skb->data;
1799 BT_INFO("%s: BCM: patching hci_ver=%02x hci_rev=%04x lmp_ver=%02x "
1800 "lmp_subver=%04x", hdev->name, ver->hci_ver, ver->hci_rev,
1801 ver->lmp_ver, ver->lmp_subver);
1804 /* Start Download */
1805 skb = __hci_cmd_sync(hdev, 0xfc2e, 0, NULL, HCI_INIT_TIMEOUT);
1808 BT_ERR("%s: BCM: Download Minidrv command failed (%ld)",
1814 /* 50 msec delay after Download Minidrv completes */
1820 while (fw_size >= sizeof(*cmd)) {
1821 cmd = (struct hci_command_hdr *)fw_ptr;
1822 fw_ptr += sizeof(*cmd);
1823 fw_size -= sizeof(*cmd);
1825 if (fw_size < cmd->plen) {
1826 BT_ERR("%s: BCM: patch %s is corrupted",
1827 hdev->name, fw_name);
1833 fw_ptr += cmd->plen;
1834 fw_size -= cmd->plen;
1836 opcode = le16_to_cpu(cmd->opcode);
1838 skb = __hci_cmd_sync(hdev, opcode, cmd->plen, cmd_param,
1842 BT_ERR("%s: BCM: patch command %04x failed (%ld)",
1843 hdev->name, opcode, ret);
1849 /* 250 msec delay after Launch Ram completes */
1854 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
1857 BT_ERR("%s: HCI_OP_RESET failed (%ld)", hdev->name, ret);
1862 /* Read Local Version Info */
1863 skb = __hci_cmd_sync(hdev, HCI_OP_READ_LOCAL_VERSION, 0, NULL,
1867 BT_ERR("%s: HCI_OP_READ_LOCAL_VERSION failed (%ld)",
1872 if (skb->len != sizeof(*ver)) {
1873 BT_ERR("%s: HCI_OP_READ_LOCAL_VERSION event length mismatch",
1880 ver = (struct hci_rp_read_local_version *)skb->data;
1881 BT_INFO("%s: BCM: firmware hci_ver=%02x hci_rev=%04x lmp_ver=%02x "
1882 "lmp_subver=%04x", hdev->name, ver->hci_ver, ver->hci_rev,
1883 ver->lmp_ver, ver->lmp_subver);
1886 /* Read BD Address */
1887 skb = __hci_cmd_sync(hdev, HCI_OP_READ_BD_ADDR, 0, NULL,
1891 BT_ERR("%s: HCI_OP_READ_BD_ADDR failed (%ld)",
1896 if (skb->len != sizeof(*bda)) {
1897 BT_ERR("%s: HCI_OP_READ_BD_ADDR event length mismatch",
1904 bda = (struct hci_rp_read_bd_addr *)skb->data;
1906 BT_ERR("%s: HCI_OP_READ_BD_ADDR error status (%02x)",
1907 hdev->name, bda->status);
1909 ret = -bt_to_errno(bda->status);
1913 /* The address 00:20:70:02:A0:00 indicates a BCM20702A0 controller
1914 * with no configured address.
1916 if (!bacmp(&bda->bdaddr, BDADDR_BCM20702A0)) {
1917 BT_INFO("%s: BCM: using default device address (%pMR)",
1918 hdev->name, &bda->bdaddr);
1919 set_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks);
1925 release_firmware(fw);
1930 static int btusb_set_bdaddr_bcm(struct hci_dev *hdev, const bdaddr_t *bdaddr)
1932 struct sk_buff *skb;
1935 skb = __hci_cmd_sync(hdev, 0xfc01, 6, bdaddr, HCI_INIT_TIMEOUT);
1938 BT_ERR("%s: BCM: Change address command failed (%ld)",
1947 static int btusb_set_bdaddr_ath3012(struct hci_dev *hdev,
1948 const bdaddr_t *bdaddr)
1950 struct sk_buff *skb;
1957 buf[3] = sizeof(bdaddr_t);
1958 memcpy(buf + 4, bdaddr, sizeof(bdaddr_t));
1960 skb = __hci_cmd_sync(hdev, 0xfc0b, sizeof(buf), buf, HCI_INIT_TIMEOUT);
1963 BT_ERR("%s: Change address command failed (%ld)",
1972 static int btusb_probe(struct usb_interface *intf,
1973 const struct usb_device_id *id)
1975 struct usb_endpoint_descriptor *ep_desc;
1976 struct btusb_data *data;
1977 struct hci_dev *hdev;
1980 BT_DBG("intf %p id %p", intf, id);
1982 /* interface numbers are hardcoded in the spec */
1983 if (intf->cur_altsetting->desc.bInterfaceNumber != 0)
1986 if (!id->driver_info) {
1987 const struct usb_device_id *match;
1989 match = usb_match_id(intf, blacklist_table);
1994 if (id->driver_info == BTUSB_IGNORE)
1997 if (id->driver_info & BTUSB_ATH3012) {
1998 struct usb_device *udev = interface_to_usbdev(intf);
2000 /* Old firmware would otherwise let ath3k driver load
2001 * patch and sysconfig files */
2002 if (le16_to_cpu(udev->descriptor.bcdDevice) <= 0x0001)
2006 data = devm_kzalloc(&intf->dev, sizeof(*data), GFP_KERNEL);
2010 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
2011 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
2013 if (!data->intr_ep && usb_endpoint_is_int_in(ep_desc)) {
2014 data->intr_ep = ep_desc;
2018 if (!data->bulk_tx_ep && usb_endpoint_is_bulk_out(ep_desc)) {
2019 data->bulk_tx_ep = ep_desc;
2023 if (!data->bulk_rx_ep && usb_endpoint_is_bulk_in(ep_desc)) {
2024 data->bulk_rx_ep = ep_desc;
2029 if (!data->intr_ep || !data->bulk_tx_ep || !data->bulk_rx_ep)
2032 data->cmdreq_type = USB_TYPE_CLASS;
2034 data->udev = interface_to_usbdev(intf);
2037 INIT_WORK(&data->work, btusb_work);
2038 INIT_WORK(&data->waker, btusb_waker);
2039 init_usb_anchor(&data->deferred);
2040 init_usb_anchor(&data->tx_anchor);
2041 spin_lock_init(&data->txlock);
2043 init_usb_anchor(&data->intr_anchor);
2044 init_usb_anchor(&data->bulk_anchor);
2045 init_usb_anchor(&data->isoc_anchor);
2046 spin_lock_init(&data->rxlock);
2048 data->recv_bulk = btusb_recv_bulk;
2050 hdev = hci_alloc_dev();
2054 hdev->bus = HCI_USB;
2055 hci_set_drvdata(hdev, data);
2059 SET_HCIDEV_DEV(hdev, &intf->dev);
2061 hdev->open = btusb_open;
2062 hdev->close = btusb_close;
2063 hdev->flush = btusb_flush;
2064 hdev->send = btusb_send_frame;
2065 hdev->notify = btusb_notify;
2067 if (id->driver_info & BTUSB_BCM92035)
2068 hdev->setup = btusb_setup_bcm92035;
2070 if (id->driver_info & BTUSB_BCM_PATCHRAM) {
2071 hdev->setup = btusb_setup_bcm_patchram;
2072 hdev->set_bdaddr = btusb_set_bdaddr_bcm;
2073 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
2076 if (id->driver_info & BTUSB_INTEL) {
2077 hdev->setup = btusb_setup_intel;
2078 hdev->set_bdaddr = btusb_set_bdaddr_intel;
2081 if (id->driver_info & BTUSB_MARVELL)
2082 hdev->set_bdaddr = btusb_set_bdaddr_marvell;
2084 if (id->driver_info & BTUSB_AVM)
2085 set_bit(HCI_QUIRK_BROKEN_LOCAL_COMMANDS, &hdev->quirks);
2087 if (id->driver_info & BTUSB_INTEL_BOOT)
2088 set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
2090 if (id->driver_info & BTUSB_ATH3012)
2091 hdev->set_bdaddr = btusb_set_bdaddr_ath3012;
2093 /* Interface numbers are hardcoded in the specification */
2094 data->isoc = usb_ifnum_to_if(data->udev, 1);
2097 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
2099 if (force_scofix || id->driver_info & BTUSB_WRONG_SCO_MTU) {
2100 if (!disable_scofix)
2101 set_bit(HCI_QUIRK_FIXUP_BUFFER_SIZE, &hdev->quirks);
2104 if (id->driver_info & BTUSB_BROKEN_ISOC)
2107 if (id->driver_info & BTUSB_DIGIANSWER) {
2108 data->cmdreq_type = USB_TYPE_VENDOR;
2109 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
2112 if (id->driver_info & BTUSB_CSR) {
2113 struct usb_device *udev = data->udev;
2114 u16 bcdDevice = le16_to_cpu(udev->descriptor.bcdDevice);
2116 /* Old firmware would otherwise execute USB reset */
2117 if (bcdDevice < 0x117)
2118 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
2120 /* Fake CSR devices with broken commands */
2121 if (bcdDevice <= 0x100)
2122 hdev->setup = btusb_setup_csr;
2125 if (id->driver_info & BTUSB_SNIFFER) {
2126 struct usb_device *udev = data->udev;
2128 /* New sniffer firmware has crippled HCI interface */
2129 if (le16_to_cpu(udev->descriptor.bcdDevice) > 0x997)
2130 set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
2133 if (id->driver_info & BTUSB_INTEL_BOOT) {
2134 /* A bug in the bootloader causes that interrupt interface is
2135 * only enabled after receiving SetInterface(0, AltSetting=0).
2137 err = usb_set_interface(data->udev, 0, 0);
2139 BT_ERR("failed to set interface 0, alt 0 %d", err);
2146 err = usb_driver_claim_interface(&btusb_driver,
2154 err = hci_register_dev(hdev);
2160 usb_set_intfdata(intf, data);
2165 static void btusb_disconnect(struct usb_interface *intf)
2167 struct btusb_data *data = usb_get_intfdata(intf);
2168 struct hci_dev *hdev;
2170 BT_DBG("intf %p", intf);
2176 usb_set_intfdata(data->intf, NULL);
2179 usb_set_intfdata(data->isoc, NULL);
2181 hci_unregister_dev(hdev);
2183 if (intf == data->isoc)
2184 usb_driver_release_interface(&btusb_driver, data->intf);
2185 else if (data->isoc)
2186 usb_driver_release_interface(&btusb_driver, data->isoc);
2188 btusb_free_frags(data);
2193 static int btusb_suspend(struct usb_interface *intf, pm_message_t message)
2195 struct btusb_data *data = usb_get_intfdata(intf);
2197 BT_DBG("intf %p", intf);
2199 if (data->suspend_count++)
2202 spin_lock_irq(&data->txlock);
2203 if (!(PMSG_IS_AUTO(message) && data->tx_in_flight)) {
2204 set_bit(BTUSB_SUSPENDING, &data->flags);
2205 spin_unlock_irq(&data->txlock);
2207 spin_unlock_irq(&data->txlock);
2208 data->suspend_count--;
2212 cancel_work_sync(&data->work);
2214 btusb_stop_traffic(data);
2215 usb_kill_anchored_urbs(&data->tx_anchor);
2220 static void play_deferred(struct btusb_data *data)
2225 while ((urb = usb_get_from_anchor(&data->deferred))) {
2226 err = usb_submit_urb(urb, GFP_ATOMIC);
2230 data->tx_in_flight++;
2232 usb_scuttle_anchored_urbs(&data->deferred);
2235 static int btusb_resume(struct usb_interface *intf)
2237 struct btusb_data *data = usb_get_intfdata(intf);
2238 struct hci_dev *hdev = data->hdev;
2241 BT_DBG("intf %p", intf);
2243 if (--data->suspend_count)
2246 if (!test_bit(HCI_RUNNING, &hdev->flags))
2249 if (test_bit(BTUSB_INTR_RUNNING, &data->flags)) {
2250 err = btusb_submit_intr_urb(hdev, GFP_NOIO);
2252 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
2257 if (test_bit(BTUSB_BULK_RUNNING, &data->flags)) {
2258 err = btusb_submit_bulk_urb(hdev, GFP_NOIO);
2260 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
2264 btusb_submit_bulk_urb(hdev, GFP_NOIO);
2267 if (test_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
2268 if (btusb_submit_isoc_urb(hdev, GFP_NOIO) < 0)
2269 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
2271 btusb_submit_isoc_urb(hdev, GFP_NOIO);
2274 spin_lock_irq(&data->txlock);
2275 play_deferred(data);
2276 clear_bit(BTUSB_SUSPENDING, &data->flags);
2277 spin_unlock_irq(&data->txlock);
2278 schedule_work(&data->work);
2283 usb_scuttle_anchored_urbs(&data->deferred);
2285 spin_lock_irq(&data->txlock);
2286 clear_bit(BTUSB_SUSPENDING, &data->flags);
2287 spin_unlock_irq(&data->txlock);
2293 static struct usb_driver btusb_driver = {
2295 .probe = btusb_probe,
2296 .disconnect = btusb_disconnect,
2298 .suspend = btusb_suspend,
2299 .resume = btusb_resume,
2301 .id_table = btusb_table,
2302 .supports_autosuspend = 1,
2303 .disable_hub_initiated_lpm = 1,
2306 module_usb_driver(btusb_driver);
2308 module_param(disable_scofix, bool, 0644);
2309 MODULE_PARM_DESC(disable_scofix, "Disable fixup of wrong SCO buffer size");
2311 module_param(force_scofix, bool, 0644);
2312 MODULE_PARM_DESC(force_scofix, "Force fixup of wrong SCO buffers size");
2314 module_param(reset, bool, 0644);
2315 MODULE_PARM_DESC(reset, "Send HCI reset command on initialization");
2317 MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
2318 MODULE_DESCRIPTION("Generic Bluetooth USB driver ver " VERSION);
2319 MODULE_VERSION(VERSION);
2320 MODULE_LICENSE("GPL");